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.
11 # include "irnode_t.h"
12 # include "irgraph_t.h"
13 # include "irmode_t.h"
15 # include "ircons_t.h"
20 # include "dbginfo_t.h"
21 # include "iropt_dbg.h"
22 # include "irflag_t.h"
24 # include "firmstat.h"
27 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
29 #define MAX_PROJ IMAX(pn_Load_max, pn_Store_max)
34 typedef struct _walk_env_t {
35 struct obstack obst; /**< list of all stores */
40 * flags for Load/Store
43 LDST_VISITED = 1 /**< if set, this Load/Store is already visited */
49 typedef struct _ldst_info_t {
50 ir_node *projs[MAX_PROJ]; /**< list of Proj's of this node */
51 ir_node *exc_block; /**< the exception block if available */
52 int exc_idx; /**< predecessor index in the exception block */
53 unsigned flags; /**< flags */
57 * flags for control flow
60 BLOCK_HAS_COND = 1, /**< Block has conditional control flow */
61 BLOCK_HAS_EXC = 2 /**< Block has exceptionl control flow */
67 typedef struct _block_info_t {
68 unsigned flags; /**< flags for the block */
72 * walker, clears all links first
74 static void init_links(ir_node *n, void *env)
76 set_irn_link(n, NULL);
80 * get the Load/Store info of a node
82 static ldst_info_t *get_ldst_info(ir_node *node, walk_env_t *env)
84 ldst_info_t *info = get_irn_link(node);
87 info = obstack_alloc(&env->obst, sizeof(*info));
89 memset(info, 0, sizeof(*info));
91 set_irn_link(node, info);
97 * get the Block info of a node
99 static block_info_t *get_block_info(ir_node *node, walk_env_t *env)
101 block_info_t *info = get_irn_link(node);
104 info = obstack_alloc(&env->obst, sizeof(*info));
106 memset(info, 0, sizeof(*info));
108 set_irn_link(node, info);
114 * update the projection info for a Load/Store
116 static int update_projs(ldst_info_t *info, ir_node *proj)
118 long nr = get_Proj_proj(proj);
120 assert(0 <= nr && nr <= MAX_PROJ && "Wrong proj from LoadStore");
122 if (info->projs[nr]) {
123 /* there is already one, do CSE */
124 exchange(proj, info->projs[nr]);
128 info->projs[nr] = proj;
134 * update the exception block info for a Load/Store
136 static int update_exc(ldst_info_t *info, ir_node *block, int pos)
138 assert(info->exc_block == NULL && "more than one exception block found");
140 info->exc_block = block;
145 #define get_irn_out_n(node) (unsigned)get_irn_link(node)
146 #define set_irn_out_n(node, n) set_irn_link(adr, (void *)(n))
149 * walker, collects all Load/Store/Proj nodes
151 * walks form Start -> End
153 static void collect_nodes(ir_node *node, void *env)
155 ir_op *op = get_irn_op(node);
157 ldst_info_t *ldst_info;
158 walk_env_t *wenv = env;
164 pred = get_Proj_pred(node);
165 op = get_irn_op(pred);
168 ldst_info = get_ldst_info(pred, wenv);
170 wenv->changes |= update_projs(ldst_info, node);
172 if ((ldst_info->flags & LDST_VISITED) == 0) {
173 adr = get_Load_ptr(pred);
174 set_irn_out_n(adr, get_irn_out_n(adr) + 1);
176 ldst_info->flags |= LDST_VISITED;
179 else if (op == op_Store) {
180 ldst_info = get_ldst_info(pred, wenv);
182 wenv->changes |= update_projs(ldst_info, node);
184 if ((ldst_info->flags & LDST_VISITED) == 0) {
185 adr = get_Store_ptr(pred);
186 set_irn_out_n(adr, get_irn_out_n(adr) + 1);
188 ldst_info->flags |= LDST_VISITED;
192 else if (op == op_Block) { /* check, if it's an exception block */
195 for (i = 0, n = get_Block_n_cfgpreds(node); i < n; ++i) {
197 block_info_t *bl_info;
199 pred = skip_Proj(get_Block_cfgpred(node, i));
201 /* ignore Bad predecessors, they will be removed later */
205 pred_block = get_nodes_block(pred);
206 bl_info = get_block_info(pred_block, wenv);
208 if (is_fragile_op(pred))
209 bl_info->flags |= BLOCK_HAS_EXC;
210 else if (is_forking_op(pred))
211 bl_info->flags |= BLOCK_HAS_COND;
213 if (get_irn_op(pred) == op_Load || get_irn_op(pred) == op_Store) {
214 ldst_info = get_ldst_info(pred, wenv);
216 wenv->changes |= update_exc(ldst_info, node, i);
225 static int optimize_load(ir_node *load)
227 ldst_info_t *info = get_irn_link(load);
228 ir_mode *load_mode = get_Load_mode(load);
229 ir_node *pred, *mem, *ptr;
232 /* the address of the load to be optimized */
233 ptr = get_Load_ptr(load);
236 * Check if we can remove the exception form a Load:
237 * this can be done, if the address is from an Sel(Alloc) and
238 * the Sel type is a subtype of the alloc type.
240 * This optimizes some often used OO constructs,
241 * like x = new O; x->t;
243 if (info->projs[pn_Load_X_except]) {
244 if (get_irn_op(ptr) == op_Sel) {
245 ir_node *mem = get_Sel_mem(ptr);
247 if (get_irn_op(mem) == op_Alloc) {
248 /* ok, check the types */
249 entity *ent = get_Sel_entity(ptr);
250 type *s_type = get_entity_type(ent);
251 type *a_type = get_Alloc_type(mem);
253 if (is_subclass_of(s_type, a_type)) {
254 /* ok, condition met: there can't be an exception because
255 * alloc guarantees that enough memory was allocated */
257 exchange(info->projs[pn_Load_X_except], new_Bad());
258 info->projs[pn_Load_X_except] = NULL;
262 else if (get_irn_op(ptr) == op_Alloc) {
263 /* simple case: a direct load after an Alloc. Firm Alloc throw
264 * an exception in case of out-of-memory. So, there is no way for an
265 * exception in this load.
266 * This code is constructed by the "exception lowering" in the Jack compiler.
268 exchange(info->projs[pn_Load_X_except], new_Bad());
269 info->projs[pn_Load_X_except] = NULL;
273 /* do NOT touch volatile loads for now */
274 if (get_Load_volatility(load) == volatility_is_volatile)
277 if (! info->projs[pn_Load_res] && ! info->projs[pn_Load_X_except]) {
278 /* a Load which value is neither used nor exception checked, remove it */
279 mem = get_Load_mem(load);
280 exchange(info->projs[pn_Load_M], mem);
285 /* the mem of the Load. Must still be returned after optimization */
286 mem = get_Load_mem(load);
288 if ((get_irn_opcode(ptr) == iro_SymConst) && (get_SymConst_kind(ptr) == symconst_addr_ent)) {
289 entity *ent = get_SymConst_entity(ptr);
291 assert(mode_is_reference(get_irn_mode(ptr)));
293 if ((allocation_static == get_entity_allocation(ent)) &&
294 (visibility_external_allocated != get_entity_visibility(ent))) {
295 /* a static allocation that is not external: there should be NO exception
298 /* no exception, clear the info field as it might be checked later again */
299 if (info->projs[pn_Load_X_except]) {
300 exchange(info->projs[pn_Load_X_except], new_Bad());
301 info->projs[pn_Load_X_except] = NULL;
304 if (variability_constant == get_entity_variability(ent)
305 && is_atomic_entity(ent)) { /* Might not be atomic after
306 lowering of Sels. In this
307 case we could also load, but
308 it's more complicated. */
309 /* more simpler case: we load the content of a constant value:
310 * replace it by the constant itself
314 if (info->projs[pn_Load_M])
315 exchange(info->projs[pn_Load_M], mem);
318 if (info->projs[pn_Load_res])
319 exchange(info->projs[pn_Load_res], copy_const_value(get_atomic_ent_value(ent)));
324 /* we changed the irg, but try further */
329 /* Check, if the address of this load is used more than once.
330 * If not, this load cannot be removed in any case. */
331 if (get_irn_out_n(ptr) <= 1)
334 /* follow the memory chain as long as there are only Loads */
335 for (pred = skip_Proj(mem); ; pred = skip_Proj(get_Load_mem(pred))) {
338 * BEWARE: one might think that checking the modes is useless, because
339 * if the pointers are identical, they refer to the same object.
340 * This is only true in strong typed languages, not is C were the following
341 * is possible a = *(type1 *)p; b = *(type2 *)p ...
344 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
345 get_irn_mode(get_Store_value(pred)) == load_mode) {
346 ldst_info_t *pred_info = get_irn_link(pred);
349 * a Load immediately after a Store -- a read after write.
350 * We may remove the Load, if both Load & Store does not have an exception handler
351 * OR they are in the same block. In the latter case the Load cannot
352 * throw an exception when the previous Store was quiet.
354 * Why we need to check for Store Exc? If the Store cannot be executed (ROM)
355 * the exception handler might simply jump into the load block :-(
356 * We could make it a little bit better if we would know that the exception
357 * handler of the Store jumps directly to the end...
359 if ((!pred_info->projs[pn_Store_X_except] && !info->projs[pn_Load_X_except]) ||
360 get_nodes_block(load) == get_nodes_block(pred)) {
361 DBG_OPT_RAW(pred, load);
362 exchange( info->projs[pn_Load_res], get_Store_value(pred) );
364 if (info->projs[pn_Load_M])
365 exchange(info->projs[pn_Load_M], mem);
368 if (info->projs[pn_Load_X_except])
369 exchange( info->projs[pn_Load_X_except], new_Bad());
373 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
374 get_Load_mode(pred) == load_mode) {
376 * a Load after a Load -- a read after read.
377 * We may remove the second Load, if it does not have an exception handler
378 * OR they are in the same block. In the later case the Load cannot
379 * throw an exception when the previous Load was quiet.
381 * Here, there is no need to check if the previos Load has an exception hander because
382 * they would have exact the same exception...
384 if (! info->projs[pn_Load_X_except] || get_nodes_block(load) == get_nodes_block(pred)) {
385 ldst_info_t *pred_info = get_irn_link(pred);
387 DBG_OPT_RAR(pred, load);
389 if (pred_info->projs[pn_Load_res]) {
390 /* we need a data proj from the previous load for this optimization */
391 exchange( info->projs[pn_Load_res], pred_info->projs[pn_Load_res] );
392 if (info->projs[pn_Load_M])
393 exchange(info->projs[pn_Load_M], mem);
396 if (info->projs[pn_Load_res]) {
397 set_Proj_pred(info->projs[pn_Load_res], pred);
398 set_nodes_block(info->projs[pn_Load_res], get_nodes_block(pred));
400 if (info->projs[pn_Load_M]) {
401 /* Actually, this if should not be necessary. Construct the Loads
403 exchange(info->projs[pn_Load_M], mem);
408 if (info->projs[pn_Load_X_except])
409 exchange(info->projs[pn_Load_X_except], new_Bad());
415 /* follow only Load chains */
416 if (get_irn_op(pred) != op_Load)
425 static int optimize_store(ir_node *store)
427 ldst_info_t *info = get_irn_link(store);
428 ir_node *pred, *mem, *ptr, *value, *block;
432 if (get_Store_volatility(store) == volatility_is_volatile)
436 * BEWARE: one might think that checking the modes is useless, because
437 * if the pointers are identical, they refer to the same object.
438 * This is only true in strong typed languages, not is C were the following
439 * is possible *(type1 *)p = a; *(type2 *)p = b ...
442 ptr = get_Store_ptr(store);
444 /* Check, if the address of this load is used more than once.
445 * If not, this load cannot be removed in any case. */
446 if (get_irn_out_n(ptr) <= 1)
449 block = get_nodes_block(store);
450 mem = get_Store_mem(store);
451 value = get_Store_value(store);
452 mode = get_irn_mode(value);
454 /* follow the memory chain as long as there are only Loads */
455 for (pred = skip_Proj(mem); ; pred = skip_Proj(get_Load_mem(pred))) {
456 ldst_info_t *pred_info = get_irn_link(pred);
458 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
459 get_nodes_block(pred) == block && get_irn_mode(get_Store_value(pred)) == mode) {
461 * a Store after a Store in the same block -- a write after write.
462 * We may remove the first Store, if it does not have an exception handler.
464 * TODO: What, if both have the same exception handler ???
466 if (get_Store_volatility(pred) != volatility_is_volatile && !pred_info->projs[pn_Store_X_except]) {
467 DBG_OPT_WAW(pred, store);
468 exchange( pred_info->projs[pn_Store_M], get_Store_mem(pred) );
472 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
473 value == pred_info->projs[pn_Load_res]) {
475 * a Store of a value after a Load -- a write after read.
476 * We may remove the second Store, if it does not have an exception handler.
478 if (! info->projs[pn_Store_X_except]) {
479 DBG_OPT_WAR(pred, store);
480 exchange( info->projs[pn_Store_M], mem );
485 /* follow only Load chains */
486 if (get_irn_op(pred) != op_Load)
493 * walker, optimizes Phi after Stores:
494 * Does the following optimization:
496 * val1 val2 val3 val1 val2 val3
504 * This removes the number of stores and allows for predicated execution.
505 * Moves Stores back to the end of a function which may be bad
507 * Is only allowed if the predecessor blocks have only one successor.
509 static int optimize_phi(ir_node *phi, void *env)
511 walk_env_t *wenv = env;
513 ir_node *store, *ptr, *block, *phiM, *phiD, *exc, *projM;
515 ir_node **inM, **inD;
519 block_info_t *bl_info;
521 /* Must be a memory Phi */
522 if (get_irn_mode(phi) != mode_M)
525 n = get_Phi_n_preds(phi);
529 store = skip_Proj(get_Phi_pred(phi, 0));
530 if (get_irn_op(store) != op_Store)
533 /* abort on bad blocks */
534 if (is_Bad(get_nodes_block(store)))
537 /* check if the block has only one output */
538 bl_info = get_irn_link(get_nodes_block(store));
542 /* this is the address of the store */
543 ptr = get_Store_ptr(store);
544 mode = get_irn_mode(get_Store_value(store));
545 info = get_irn_link(store);
546 exc = info->exc_block;
548 for (i = 1; i < n; ++i) {
549 ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
551 if (get_irn_op(pred) != op_Store)
554 if (mode != get_irn_mode(get_Store_value(pred)) || ptr != get_Store_ptr(pred))
557 info = get_irn_link(pred);
559 /* check, if all stores have the same exception flow */
560 if (exc != info->exc_block)
563 /* abort on bad blocks */
564 if (is_Bad(get_nodes_block(store)))
567 /* check if the block has only one output */
568 bl_info = get_irn_link(get_nodes_block(store));
574 * ok, when we are here, we found all predecessors of a Phi that
575 * are Stores to the same address. That means whatever we do before
576 * we enter the block of the Phi, we do a Store.
577 * So, we can move the store to the current block:
579 * val1 val2 val3 val1 val2 val3
581 * | Str | | Str | | Str | \ | /
587 * Is only allowed if the predecessor blocks have only one successor.
590 /* first step: collect all inputs */
591 NEW_ARR_A(ir_node *, inM, n);
592 NEW_ARR_A(ir_node *, inD, n);
593 NEW_ARR_A(int, idx, n);
595 for (i = 0; i < n; ++i) {
596 ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
597 info = get_irn_link(pred);
599 inM[i] = get_Store_mem(pred);
600 inD[i] = get_Store_value(pred);
601 idx[i] = info->exc_idx;
603 block = get_nodes_block(phi);
605 /* second step: create a new memory Phi */
606 phiM = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inM, mode_M);
608 /* third step: create a new data Phi */
609 phiD = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inD, mode);
611 /* fourth step: create the Store */
612 store = new_rd_Store(db, current_ir_graph, block, phiM, ptr, phiD);
613 projM = new_rd_Proj(NULL, current_ir_graph, block, store, mode_M, pn_Store_M);
615 info = get_ldst_info(store, wenv);
616 info->projs[pn_Store_M] = projM;
618 /* fifths step: repair exception flow */
620 ir_node *projX = new_rd_Proj(NULL, current_ir_graph, block, store, mode_X, pn_Store_X_except);
622 info->projs[pn_Store_X_except] = projX;
623 info->exc_block = exc;
624 info->exc_idx = idx[0];
626 for (i = 0; i < n; ++i) {
627 set_Block_cfgpred(exc, idx[i], projX);
631 /* the exception block should be optimized as some inputs are identical now */
635 /* sixt step: replace old Phi */
636 exchange(phi, projM);
642 * walker, collects all Load/Store/Proj nodes
644 static void do_load_store_optimize(ir_node *n, void *env)
646 walk_env_t *wenv = env;
648 switch (get_irn_opcode(n)) {
651 wenv->changes |= optimize_load(n);
655 wenv->changes |= optimize_store(n);
659 wenv->changes |= optimize_phi(n, env);
667 * do the load store optimization
669 void optimize_load_store(ir_graph *irg)
673 assert(get_irg_phase_state(irg) != phase_building);
675 if (!get_opt_redundant_LoadStore())
678 obstack_init(&env.obst);
681 /* init the links, then collect Loads/Stores/Proj's in lists */
682 irg_walk_graph(irg, init_links, collect_nodes, &env);
684 /* now we have collected enough information, optimize */
685 irg_walk_graph(irg, NULL, do_load_store_optimize, &env);
687 obstack_free(&env.obst, NULL);
689 /* Handle graph state */
691 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
692 set_irg_outs_inconsistent(current_ir_graph);
694 /* is this really needed: Yes, as exception block may get bad but this might be tested */
695 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
696 set_irg_dom_inconsistent(current_ir_graph);