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 */
42 typedef struct _ldst_info_t {
43 ir_node *projs[MAX_PROJ]; /**< list of Proj's of this node */
44 ir_node *exc_block; /**< the exception block if available */
45 int exc_idx; /**< predecessor index in the exception block */
49 * flags for control flow
52 BLOCK_HAS_COND = 1, /**< Block has conditional control flow */
53 BLOCK_HAS_EXC = 2 /**< Block has exceptionl control flow */
59 typedef struct _block_info_t {
60 unsigned flags; /**< flags for the block */
64 * walker, clears all links first
66 static void init_links(ir_node *n, void *env)
68 set_irn_link(n, NULL);
72 * get the Load/Store info of a node
74 static ldst_info_t *get_ldst_info(ir_node *node, walk_env_t *env)
76 ldst_info_t *info = get_irn_link(node);
79 info = obstack_alloc(&env->obst, sizeof(*info));
81 memset(info, 0, sizeof(*info));
83 set_irn_link(node, info);
89 * get the Block info of a node
91 static block_info_t *get_block_info(ir_node *node, walk_env_t *env)
93 block_info_t *info = get_irn_link(node);
96 info = obstack_alloc(&env->obst, sizeof(*info));
98 memset(info, 0, sizeof(*info));
100 set_irn_link(node, info);
106 * update the projection info for a Load/Store
108 static int update_projs(ldst_info_t *info, ir_node *proj)
110 long nr = get_Proj_proj(proj);
112 assert(0 <= nr && nr <= MAX_PROJ && "Wrong proj from LoadStore");
114 if (info->projs[nr]) {
115 /* there is already one, do CSE */
116 exchange(proj, info->projs[nr]);
120 info->projs[nr] = proj;
126 * update the exception block info for a Load/Store
128 static int update_exc(ldst_info_t *info, ir_node *block, int pos)
130 assert(info->exc_block == NULL && "more than one exception block found");
132 info->exc_block = block;
138 * walker, collects all Load/Store/Proj nodes
140 * walks form Start -> End
142 static void collect_nodes(ir_node *node, void *env)
145 ldst_info_t *ldst_info;
146 walk_env_t *wenv = env;
148 if (get_irn_op(node) == op_Proj) {
149 pred = get_Proj_pred(node);
151 if (get_irn_op(pred) == op_Load || get_irn_op(pred) == op_Store) {
152 ldst_info = get_ldst_info(pred, wenv);
154 wenv->changes |= update_projs(ldst_info, node);
157 else if (get_irn_op(node) == op_Block) { /* check, if it's an exception block */
160 for (i = 0, n = get_Block_n_cfgpreds(node); i < n; ++i) {
162 block_info_t *bl_info;
164 pred = skip_Proj(get_Block_cfgpred(node, i));
166 /* ignore Bad predecessors, they will be removed later */
170 pred_block = get_nodes_block(pred);
171 bl_info = get_block_info(pred_block, wenv);
173 if (is_fragile_op(pred))
174 bl_info->flags |= BLOCK_HAS_EXC;
175 else if (is_forking_op(pred))
176 bl_info->flags |= BLOCK_HAS_COND;
178 if (get_irn_op(pred) == op_Load || get_irn_op(pred) == op_Store) {
179 ldst_info = get_ldst_info(pred, wenv);
181 wenv->changes |= update_exc(ldst_info, node, i);
190 static int optimize_load(ir_node *load)
192 ldst_info_t *info = get_irn_link(load);
193 ir_mode *load_mode = get_Load_mode(load);
194 ir_node *pred, *mem, *ptr;
197 /* do NOT touch volatile loads for now */
198 if (get_Load_volatility(load) == volatility_is_volatile)
201 if (! info->projs[pn_Load_res] && ! info->projs[pn_Load_X_except]) {
202 /* a Load which value is neither used nor exception checked, remove it */
203 mem = get_Load_mem(load);
204 exchange(info->projs[pn_Load_M], mem);
209 /* the address of the load to be optimized */
210 ptr = get_Load_ptr(load);
212 /* the mem of the load. Must still be returned after optimization */
213 mem = get_Load_mem(load);
215 if ((get_irn_opcode(ptr) == iro_SymConst) && (get_SymConst_kind(ptr) == symconst_addr_ent)) {
216 entity *ent = get_SymConst_entity(ptr);
218 assert(mode_is_reference(get_irn_mode(ptr)));
220 if ((allocation_static == get_entity_allocation(ent)) &&
221 (visibility_external_allocated != get_entity_visibility(ent))) {
222 /* a static allocation that is not external: there should be NO exception
225 /* no exception, clear the info field as it might be checked later again */
226 if (info->projs[pn_Load_X_except]) {
227 exchange(info->projs[pn_Load_X_except], new_Bad());
228 info->projs[pn_Load_X_except] = NULL;
231 if (variability_constant == get_entity_variability(ent)
232 && is_atomic_entity(ent)) { /* Might not be atomic after
233 lowering of Sels. In this
234 case we could also load, but
235 it's more complicated. */
236 /* more simpler case: we load the content of a constant value:
237 * replace it by the constant itself
241 if (info->projs[pn_Load_M])
242 exchange(info->projs[pn_Load_M], mem);
245 if (info->projs[pn_Load_res])
246 exchange(info->projs[pn_Load_res], copy_const_value(get_atomic_ent_value(ent)));
251 /* we changed the irg, but try further */
256 /* follow the memory chain as long as there are only Loads */
257 for (pred = skip_Proj(mem); ; pred = skip_Proj(get_Load_mem(pred))) {
260 * BEWARE: one might think that checking the modes is useless, because
261 * if the pointers are identical, they refer to the same object.
262 * This is only true in strong typed languages, not is C were the following
263 * is possible a = *(type1 *)p; b = *(type2 *)p ...
266 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
267 get_irn_mode(get_Store_value(pred)) == load_mode) {
268 ldst_info_t *pred_info = get_irn_link(pred);
271 * a load immediately after a store -- a read after write.
272 * We may remove the Load, if both load & Store does not have an exception handler
273 * OR they are in the same block. In the latter case the Load cannot
274 * throw an exception when the previous Store was quiet.
276 * Why we need to check for Store Exc? If the Store cannot be executed (ROM)
277 * the exception handler might simply jump into the load block :-(
278 * We could make it a little bit better if we would know that the exception
279 * handler of teh Store jumps directly to the end...
281 if ((!pred_info->projs[pn_Store_X_except] && !info->projs[pn_Load_X_except]) ||
282 get_nodes_block(load) == get_nodes_block(pred)) {
283 DBG_OPT_RAW(pred, load);
284 exchange( info->projs[pn_Load_res], get_Store_value(pred) );
286 if (info->projs[pn_Load_M])
287 exchange(info->projs[pn_Load_M], mem);
290 if (info->projs[pn_Load_X_except])
291 exchange( info->projs[pn_Load_X_except], new_Bad());
295 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
296 get_Load_mode(pred) == load_mode) {
298 * a load after a load -- a read after read.
299 * We may remove the second Load, if it does not have an exception handler
300 * OR they are in the same block. In the later case the Load cannot
301 * throw an exception when the previous Load was quiet.
303 * Here, there is no need to check if the previos load has an exception hander because
304 * they would have exact the same exception...
306 if (! info->projs[pn_Load_X_except] || get_nodes_block(load) == get_nodes_block(pred)) {
307 ldst_info_t *pred_info = get_irn_link(pred);
309 DBG_OPT_RAR(pred, load);
311 if (pred_info->projs[pn_Load_res]) {
312 /* we need a data proj from the previous load for this optimization */
313 exchange( info->projs[pn_Load_res], pred_info->projs[pn_Load_res] );
314 if (info->projs[pn_Load_M])
315 exchange(info->projs[pn_Load_M], mem);
318 if (info->projs[pn_Load_res]) {
319 set_Proj_pred(info->projs[pn_Load_res], pred);
320 set_nodes_block(info->projs[pn_Load_res], get_nodes_block(pred));
322 if (info->projs[pn_Load_M]) {
323 /* Actually, this if should not be necessary. Construct the Loads
325 exchange(info->projs[pn_Load_M], mem);
330 if (info->projs[pn_Load_X_except])
331 exchange(info->projs[pn_Load_X_except], new_Bad());
337 /* follow only load chains */
338 if (get_irn_op(pred) != op_Load)
347 static int optimize_store(ir_node *store)
349 ldst_info_t *info = get_irn_link(store);
350 ir_node *pred, *mem, *ptr, *value, *block;
354 if (get_Store_volatility(store) == volatility_is_volatile)
358 * BEWARE: one might think that checking the modes is useless, because
359 * if the pointers are identical, they refer to the same object.
360 * This is only true in strong typed languages, not is C were the following
361 * is possible *(type1 *)p = a; *(type2 *)p = b ...
364 block = get_nodes_block(store);
365 ptr = get_Store_ptr(store);
366 mem = get_Store_mem(store);
367 value = get_Store_value(store);
369 mode = get_irn_mode(value);
371 /* follow the memory chain as long as there are only Loads */
372 for (pred = skip_Proj(mem); ; pred = skip_Proj(get_Load_mem(pred))) {
373 ldst_info_t *pred_info = get_irn_link(pred);
375 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
376 get_nodes_block(pred) == block && get_irn_mode(get_Store_value(pred)) == mode) {
378 * a store after a store in the same block -- a write after write.
379 * We may remove the first Store, if it does not have an exception handler.
381 * TODO: What, if both have the same exception handler ???
383 if (get_Store_volatility(pred) != volatility_is_volatile && !pred_info->projs[pn_Store_X_except]) {
384 DBG_OPT_WAW(pred, store);
385 exchange( pred_info->projs[pn_Store_M], get_Store_mem(pred) );
389 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
390 value == pred_info->projs[pn_Load_res]) {
392 * a store of a value after a load -- a write after read.
393 * We may remove the second Store, if it does not have an exception handler.
395 if (! info->projs[pn_Store_X_except]) {
396 DBG_OPT_WAR(pred, store);
397 exchange( info->projs[pn_Store_M], mem );
402 /* follow only load chains */
403 if (get_irn_op(pred) != op_Load)
410 * walker, optimizes Phi after Stores:
411 * Does the following optimization:
413 * val1 val2 val3 val1 val2 val3
421 * This removes the number of stores and allows for predicated execution.
422 * Moves Stores back to the end of a function which may be bad
424 * Is only allowed if the predecessor blocks have only one successor.
426 static int optimize_phi(ir_node *phi, void *env)
428 walk_env_t *wenv = env;
430 ir_node *store, *ptr, *block, *phiM, *phiD, *exc, *projM;
432 ir_node **inM, **inD;
436 block_info_t *bl_info;
438 /* Must be a memory Phi */
439 if (get_irn_mode(phi) != mode_M)
442 n = get_Phi_n_preds(phi);
446 store = skip_Proj(get_Phi_pred(phi, 0));
447 if (get_irn_op(store) != op_Store)
450 /* abort on bad blocks */
451 if (is_Bad(get_nodes_block(store)))
454 /* check if the block has only one output */
455 bl_info = get_irn_link(get_nodes_block(store));
459 /* this is the address of the store */
460 ptr = get_Store_ptr(store);
461 mode = get_irn_mode(get_Store_value(store));
462 info = get_irn_link(store);
463 exc = info->exc_block;
465 for (i = 1; i < n; ++i) {
466 ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
468 if (get_irn_op(pred) != op_Store)
471 if (mode != get_irn_mode(get_Store_value(pred)) || ptr != get_Store_ptr(pred))
474 info = get_irn_link(pred);
476 /* check, if all stores have the same exception flow */
477 if (exc != info->exc_block)
480 /* abort on bad blocks */
481 if (is_Bad(get_nodes_block(store)))
484 /* check if the block has only one output */
485 bl_info = get_irn_link(get_nodes_block(store));
491 * ok, when we are here, we found all predecessors of a Phi that
492 * are Stores to the same address. That means whatever we do before
493 * we enter the block of the Phi, we do a Store.
494 * So, we can move the store to the current block:
496 * val1 val2 val3 val1 val2 val3
498 * | Str | | Str | | Str | \ | /
504 * Is only allowed if the predecessor blocks have only one successor.
507 /* first step: collect all inputs */
508 NEW_ARR_A(ir_node *, inM, n);
509 NEW_ARR_A(ir_node *, inD, n);
510 NEW_ARR_A(int, idx, n);
512 for (i = 0; i < n; ++i) {
513 ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
514 info = get_irn_link(pred);
516 inM[i] = get_Store_mem(pred);
517 inD[i] = get_Store_value(pred);
518 idx[i] = info->exc_idx;
520 block = get_nodes_block(phi);
522 /* second step: create a new memory Phi */
523 phiM = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inM, mode_M);
525 /* third step: create a new data Phi */
526 phiD = new_rd_Phi(get_irn_dbg_info(phi), current_ir_graph, block, n, inD, mode);
528 /* fourth step: create the Store */
529 store = new_rd_Store(db, current_ir_graph, block, phiM, ptr, phiD);
530 projM = new_rd_Proj(NULL, current_ir_graph, block, store, mode_M, pn_Store_M);
532 info = get_ldst_info(store, wenv);
533 info->projs[pn_Store_M] = projM;
535 /* fifths step: repair exception flow */
537 ir_node *projX = new_rd_Proj(NULL, current_ir_graph, block, store, mode_X, pn_Store_X_except);
539 info->projs[pn_Store_X_except] = projX;
540 info->exc_block = exc;
541 info->exc_idx = idx[0];
543 for (i = 0; i < n; ++i) {
544 set_Block_cfgpred(exc, idx[i], projX);
548 /* the exception block should be optimized as some inputs are identical now */
552 /* sixt step: replace old Phi */
553 exchange(phi, projM);
559 * walker, collects all Load/Store/Proj nodes
561 static void do_load_store_optimize(ir_node *n, void *env)
563 walk_env_t *wenv = env;
565 switch (get_irn_opcode(n)) {
568 wenv->changes |= optimize_load(n);
572 wenv->changes |= optimize_store(n);
576 wenv->changes |= optimize_phi(n, env);
584 * do the load store optimization
586 void optimize_load_store(ir_graph *irg)
590 assert(get_irg_phase_state(irg) != phase_building);
592 if (!get_opt_redundant_LoadStore())
595 obstack_init(&env.obst);
598 /* init the links, then collect Loads/Stores/Proj's in lists */
599 irg_walk_graph(irg, init_links, collect_nodes, &env);
601 /* now we have collected enough information, optimize */
602 irg_walk_graph(irg, NULL, do_load_store_optimize, &env);
604 obstack_free(&env.obst, NULL);
606 /* Handle graph state */
608 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
609 set_irg_outs_inconsistent(current_ir_graph);
611 /* is this really needed: Yes, as exception block may get bad but this might be tested */
612 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
613 set_irg_dom_inconsistent(current_ir_graph);