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 if (info->projs[pn_Load_res]) {
172 set_Proj_pred(info->projs[pn_Load_res], pred);
173 set_nodes_block(info->projs[pn_Load_res], get_nodes_block(pred));
175 exchange(info->projs[pn_Load_M], mem);
179 if (info->projs[pn_Load_X_except])
180 exchange(info->projs[pn_Load_X_except], new_Bad());
191 static int optimize_store(ir_node *store)
193 ldst_info_t *info = get_irn_link(store);
194 ir_node *pred, *mem, *ptr, *value, *block;
196 ldst_info_t *pred_info;
199 if (get_Store_volatility(store) == volatility_is_volatile)
203 * BEWARE: one might think that checking the modes is useless, because
204 * if the pointers are identical, they refer to the same object.
205 * This is only true in strong typed languages, not is C were the following
206 * is possible *(type1 *)p = a; *(type2 *)p = b ...
209 block = get_nodes_block(store);
210 ptr = get_Store_ptr(store);
211 mem = get_Store_mem(store);
212 value = get_Store_value(store);
213 pred = skip_Proj(mem);
214 mode = get_irn_mode(value);
216 pred_info = get_irn_link(pred);
218 if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
219 get_nodes_block(pred) == block && get_irn_mode(get_Store_value(pred)) == mode) {
221 * a store immediately after a store in the same block -- a write after write.
222 * We may remove the first Store, if it does not have an exception handler.
224 * TODO: What, if both have the same exception handler ???
226 if (get_Store_volatility(pred) != volatility_is_volatile && !pred_info->projs[pn_Store_X_except]) {
227 exchange( pred_info->projs[pn_Store_M], get_Store_mem(pred) );
231 else if (get_irn_op(pred) == op_Load && get_Load_ptr(pred) == ptr &&
232 value == pred_info->projs[pn_Load_res]) {
234 * a store a value immediately after a load -- a write after read.
235 * We may remove the second Store, if it does not have an exception handler.
237 if (! info->projs[pn_Store_X_except]) {
238 exchange( info->projs[pn_Store_M], mem );
246 * walker, collects all Load/Store/Proj nodes
248 static void do_load_store_optimize(ir_node *n, void *env)
250 walk_env_t *wenv = env;
252 switch (get_irn_opcode(n)) {
255 wenv->changes |= optimize_load(n);
259 wenv->changes |= optimize_store(n);
268 * do the load store optimization
270 void optimize_load_store(ir_graph *irg)
274 assert(get_irg_phase_state(irg) != phase_building);
276 if (!get_opt_redundant_LoadStore())
279 obstack_init(&env.obst);
282 /* init the links, then collect Loads/Stores/Proj's in lists */
283 irg_walk_graph(irg, init_links, collect_nodes, &env);
285 /* now we have collected enough information, optimize */
286 irg_walk_graph(irg, NULL, do_load_store_optimize, &env);
288 obstack_free(&env.obst, NULL);
290 /* Handle graph state */
292 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
293 set_irg_outs_inconsistent(current_ir_graph);
295 /* is this really needed: Yes, as exception block may get bad but this might be tested */
296 if (get_irg_dom_state(current_ir_graph) == dom_consistent)
297 set_irg_dom_inconsistent(current_ir_graph);