3 * File name: ir/opt/ldstopt.c
4 * Purpose: load store optimizations
8 * Copyright: (c) 1998-2004 Universitä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"
23 # include "firmstat.h"
26 #define IMAX(a,b) ((a) > (b) ? (a) : (b))
28 #define MAX_PROJ IMAX(pn_Load_max, pn_Store_max)
33 typedef struct _walk_env_t {
34 struct obstack obst; /**< list of all stores */
41 typedef struct _ldst_info_t {
42 ir_node *projs[MAX_PROJ]; /**< list of Proj's of this node */
46 * walker, clears all links first
48 static void init_links(ir_node *n, void *env)
50 set_irn_link(n, NULL);
54 * get the Load/Store info of a node
56 static ldst_info_t *get_info(ir_node *node, walk_env_t *env)
58 ldst_info_t *info = get_irn_link(node);
61 info = obstack_alloc(&env->obst, sizeof(*info));
63 memset(info, 0, sizeof(*info));
65 set_irn_link(node, info);
71 * update the info for a Load/Store
73 static void update_projs(ldst_info_t *info, ir_node *proj)
75 long nr = get_Proj_proj(proj);
77 assert(0 <= nr && nr <= MAX_PROJ && "Wrong proj from LoadStore");
79 if (info->projs[nr]) {
80 /* there is already one, do CSE */
81 exchange(proj, info->projs[nr]);
84 info->projs[nr] = proj;
88 * walker, collects all Load/Store/Proj nodes
90 static void collect_nodes(ir_node *n, void *env)
94 walk_env_t *wenv = env;
96 if (get_irn_op(n) == op_Proj) {
97 pred = get_Proj_pred(n);
99 if (get_irn_op(pred) == op_Load || get_irn_op(pred) == op_Store) {
100 info = get_info(pred, wenv);
102 update_projs(info, n);
110 static int optimize_load(ir_node *load)
112 ldst_info_t *info = get_irn_link(load);
113 ir_mode *load_mode = get_Load_mode(load);
114 ir_node *pred, *mem, *ptr;
117 if (get_Load_volatility(load) == volatility_is_volatile)
121 * BEWARE: one might think that checking the modes is useless, because
122 * if the pointers are identical, they refer to the same object.
123 * This is only true in strong typed languages, not is C were the following
124 * is possible a = *(type1 *)p; b = *(type2 *)p ...
127 ptr = get_Load_ptr(load);
128 mem = get_Load_mem(load);
129 pred = skip_Proj(mem);
131 if (! info->projs[pn_Load_res] && ! info->projs[pn_Load_X_except]) {
132 /* a Load which value is neither used nor exception checked, remove it */
133 exchange(info->projs[pn_Load_M], mem);
136 else if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
137 get_irn_mode(get_Store_value(pred)) == load_mode) {
139 * a load immediately after a store -- a read after write.
140 * We may remove the Load, if it does not have an exception handler
141 * OR they are in the same block. In the latter case the Load cannot
142 * throw an exception when the previous Store was quiet.
144 if (! info->projs[pn_Load_X_except] || get_nodes_block(load) == get_nodes_block(pred)) {
145 exchange( info->projs[pn_Load_res], get_Store_value(pred) );
146 exchange( info->projs[pn_Load_M], mem );
149 if (info->projs[pn_Load_X_except])
150 exchange( info->projs[pn_Load_X_except], new_Bad());
154 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
155 get_Load_mode(pred) == load_mode) {
157 * a load immediately after a load -- a read after read.
158 * We may remove the second Load, if it does not have an exception handler
159 * OR they are in the same block. In the latter case the Load cannot
160 * throw an exception when the previous Load was quiet.
162 if (! info->projs[pn_Load_X_except] || get_nodes_block(load) == get_nodes_block(pred)) {
163 ldst_info_t *pred_info = get_irn_link(pred);
165 if (pred_info->projs[pn_Load_res]) {
166 /* we need a data proj from the previous load for this optimization */
167 exchange( info->projs[pn_Load_res], pred_info->projs[pn_Load_res] );
168 exchange( info->projs[pn_Load_M], mem );
171 set_Proj_pred(info->projs[pn_Load_res], pred);
172 set_nodes_block(info->projs[pn_Load_res], get_nodes_block(pred));
173 exchange( info->projs[pn_Load_M], mem );
177 if (info->projs[pn_Load_X_except])
178 exchange( info->projs[pn_Load_X_except], new_Bad());
189 static int optimize_store(ir_node *store)
191 ldst_info_t *info = get_irn_link(store);
192 ir_node *pred, *mem, *ptr, *value, *block;
194 ldst_info_t *pred_info;
197 if (get_Store_volatility(store) == volatility_is_volatile)
201 * BEWARE: one might think that checking the modes is useless, because
202 * if the pointers are identical, they refer to the same object.
203 * This is only true in strong typed languages, not is C were the following
204 * is possible *(type1 *)p = a; *(type2 *)p = b ...
207 block = get_nodes_block(store);
208 ptr = get_Store_ptr(store);
209 mem = get_Store_mem(store);
210 value = get_Store_value(store);
211 pred = skip_Proj(mem);
212 mode = get_irn_mode(value);
214 pred_info = get_irn_link(pred);
216 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
217 get_nodes_block(pred) == block && get_irn_mode(get_Store_value(pred)) == mode) {
219 * a store immediately after a store in the same block -- a write after write.
220 * We may remove the first Store, if it does not have an exception handler.
222 * TODO: What, if both have the same exception handler ???
224 if (get_Store_volatility(pred) != volatility_is_volatile && !pred_info->projs[pn_Store_X_except]) {
225 exchange( pred_info->projs[pn_Store_M], get_Store_mem(pred) );
229 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
230 value == pred_info->projs[pn_Load_res]) {
232 * a store a value immediately after a load -- a write after read.
233 * We may remove the second Store, if it does not have an exception handler.
235 if (! info->projs[pn_Store_X_except]) {
236 exchange( info->projs[pn_Store_M], mem );
244 * walker, collects all Load/Store/Proj nodes
246 static void do_load_store_optimize(ir_node *n, void *env)
248 walk_env_t *wenv = env;
250 switch (get_irn_opcode(n)) {
253 wenv->changes |= optimize_load(n);
257 wenv->changes |= optimize_store(n);
266 * do the load store optimization
268 void optimize_load_store(ir_graph *irg)
272 assert(get_irg_phase_state(irg) != phase_building);
274 if (!get_opt_redundant_LoadStore())
277 obstack_init(&env.obst);
280 /* init the links, then collect Loads/Stores/Proj's in lists */
281 irg_walk_graph(irg, init_links, collect_nodes, &env);
283 /* now we have collected enough information, optimize */
284 irg_walk_graph(irg, NULL, do_load_store_optimize, &env);
286 obstack_free(&env.obst, NULL);
288 /* Handle graph state */
290 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
291 set_irg_outs_inconsistent(current_ir_graph);
293 /* is this really needed: Yes, as exception block may get bad but this might be tested */
294 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
295 set_irg_dom_inconsistent(current_ir_graph);