bcd7315e717e30384546b83354d99416931b16af
[libfirm] / ir / be / ia32 / ia32_optimize.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "irnode.h"
6 #include "irprog_t.h"
7 #include "ircons.h"
8 #include "firm_types.h"
9 #include "iredges.h"
10 #include "tv.h"
11 #include "irgmod.h"
12
13 #include "../be_t.h"
14 #include "../beabi.h"
15 #include "../benode_t.h"
16 #include "../besched_t.h"
17
18 #include "ia32_new_nodes.h"
19 #include "bearch_ia32_t.h"
20 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
21 #include "ia32_transform.h"
22 #include "ia32_dbg_stat.h"
23
24 #undef is_NoMem
25 #define is_NoMem(irn) (get_irn_op(irn) == op_NoMem)
26
27 typedef int is_op_func_t(const ir_node *n);
28
29 /**
30  * checks if a node represents the NOREG value
31  */
32 static int be_is_NoReg(ia32_code_gen_t *cg, const ir_node *irn) {
33   be_abi_irg_t *babi = cg->birg->abi;
34         const arch_register_t *fp_noreg = USE_SSE2(cg) ?
35                 &ia32_xmm_regs[REG_XMM_NOREG] : &ia32_vfp_regs[REG_VFP_NOREG];
36
37         return (be_abi_get_callee_save_irn(babi, &ia32_gp_regs[REG_GP_NOREG]) == irn) ||
38                (be_abi_get_callee_save_irn(babi, fp_noreg) == irn);
39 }
40
41
42
43 /*************************************************
44  *   _____                _              _
45  *  / ____|              | |            | |
46  * | |     ___  _ __  ___| |_ __ _ _ __ | |_ ___
47  * | |    / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
48  * | |___| (_) | | | \__ \ || (_| | | | | |_\__ \
49  *  \_____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
50  *
51  *************************************************/
52
53 /**
54  * creates a unique ident by adding a number to a tag
55  *
56  * @param tag   the tag string, must contain a %d if a number
57  *              should be added
58  */
59 static ident *unique_id(const char *tag)
60 {
61         static unsigned id = 0;
62         char str[256];
63
64         snprintf(str, sizeof(str), tag, ++id);
65         return new_id_from_str(str);
66 }
67
68
69
70 /**
71  * Transforms a SymConst.
72  *
73  * @param mod     the debug module
74  * @param block   the block the new node should belong to
75  * @param node    the ir SymConst node
76  * @param mode    mode of the SymConst
77  * @return the created ia32 Const node
78  */
79 static ir_node *gen_SymConst(ia32_transform_env_t *env) {
80         ir_node  *cnst;
81         dbg_info *dbg   = env->dbg;
82         ir_mode  *mode  = env->mode;
83         ir_graph *irg   = env->irg;
84         ir_node  *block = env->block;
85
86         if (mode_is_float(mode)) {
87                 FP_USED(env->cg);
88                 if (USE_SSE2(env->cg))
89                         cnst = new_rd_ia32_xConst(dbg, irg, block, get_irg_no_mem(irg), mode);
90                 else
91                         cnst = new_rd_ia32_vfConst(dbg, irg, block, get_irg_no_mem(irg), mode);
92         }
93         else
94                 cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), mode);
95         set_ia32_Const_attr(cnst, env->irn);
96         return cnst;
97 }
98
99 /**
100  * Get a primitive type for a mode.
101  */
102 static ir_type *get_prim_type(pmap *types, ir_mode *mode)
103 {
104         pmap_entry *e = pmap_find(types, mode);
105         ir_type *res;
106
107         if (! e) {
108                 char buf[64];
109                 snprintf(buf, sizeof(buf), "prim_type_%s", get_mode_name(mode));
110                 res = new_type_primitive(new_id_from_str(buf), mode);
111                 pmap_insert(types, mode, res);
112         }
113         else
114                 res = e->value;
115         return res;
116 }
117
118 /**
119  * Get an entity that is initialized with a tarval
120  */
121 static entity *get_entity_for_tv(ia32_code_gen_t *cg, ir_node *cnst)
122 {
123         tarval *tv    = get_Const_tarval(cnst);
124         pmap_entry *e = pmap_find(cg->isa->tv_ent, tv);
125         entity *res;
126         ir_graph *rem;
127
128         if (! e) {
129                 ir_mode *mode = get_irn_mode(cnst);
130                 ir_type *tp = get_Const_type(cnst);
131                 if (tp == firm_unknown_type)
132                         tp = get_prim_type(cg->isa->types, mode);
133
134                 res = new_entity(get_glob_type(), unique_id("ia32FloatCnst_%u"), tp);
135
136                 set_entity_ld_ident(res, get_entity_ident(res));
137                 set_entity_visibility(res, visibility_local);
138                 set_entity_variability(res, variability_constant);
139                 set_entity_allocation(res, allocation_static);
140
141                  /* we create a new entity here: It's initialization must resist on the
142                     const code irg */
143                 rem = current_ir_graph;
144                 current_ir_graph = get_const_code_irg();
145                 set_atomic_ent_value(res, new_Const_type(tv, tp));
146                 current_ir_graph = rem;
147
148                 pmap_insert(cg->isa->tv_ent, tv, res);
149         }
150         else
151                 res = e->value;
152         return res;
153 }
154
155 /**
156  * Transforms a Const.
157  *
158  * @param mod     the debug module
159  * @param block   the block the new node should belong to
160  * @param node    the ir Const node
161  * @param mode    mode of the Const
162  * @return the created ia32 Const node
163  */
164 static ir_node *gen_Const(ia32_transform_env_t *env) {
165         ir_node *cnst;
166         symconst_symbol sym;
167         ir_graph *irg   = env->irg;
168         ir_node  *block = env->block;
169         ir_node  *node  = env->irn;
170         dbg_info *dbg   = env->dbg;
171         ir_mode  *mode  = env->mode;
172
173         if (mode_is_float(mode)) {
174                 FP_USED(env->cg);
175                 if (! USE_SSE2(env->cg)) {
176                         cnst_classify_t clss = classify_Const(node);
177
178                         if (clss == CNST_NULL)
179                                 return new_rd_ia32_vfldz(dbg, irg, block, mode);
180                         else if (clss == CNST_ONE)
181                                 return new_rd_ia32_vfld1(dbg, irg, block, mode);
182                 }
183                 sym.entity_p = get_entity_for_tv(env->cg, node);
184
185                 cnst = new_rd_SymConst(dbg, irg, block, sym, symconst_addr_ent);
186                 env->irn = cnst;
187                 cnst = gen_SymConst(env);
188         }
189         else {
190                 cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), get_irn_mode(node));
191                 set_ia32_Const_attr(cnst, node);
192         }
193         return cnst;
194 }
195
196
197
198 /**
199  * Transforms (all) Const's into ia32_Const and places them in the
200  * block where they are used (or in the cfg-pred Block in case of Phi's).
201  * Additionally all reference nodes are changed into mode_Is nodes.
202  */
203 void ia32_place_consts_set_modes(ir_node *irn, void *env) {
204         ia32_code_gen_t      *cg = env;
205         ia32_transform_env_t  tenv;
206         ir_mode              *mode;
207         ir_node              *pred, *cnst;
208         int                   i;
209         opcode                opc;
210
211         if (is_Block(irn))
212                 return;
213
214         mode = get_irn_mode(irn);
215
216         /* transform all reference nodes into mode_Is nodes */
217         if (mode_is_reference(mode)) {
218                 mode = mode_Is;
219                 set_irn_mode(irn, mode);
220         }
221
222         tenv.block    = get_nodes_block(irn);
223         tenv.cg       = cg;
224         tenv.irg      = cg->irg;
225         DEBUG_ONLY(tenv.mod      = cg->mod;)
226
227         /* Loop over all predecessors and check for Sym/Const nodes */
228         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
229                 pred      = get_irn_n(irn, i);
230                 cnst      = NULL;
231                 opc       = get_irn_opcode(pred);
232                 tenv.irn  = pred;
233                 tenv.mode = get_irn_mode(pred);
234                 tenv.dbg  = get_irn_dbg_info(pred);
235
236                 /* If it's a Phi, then we need to create the */
237                 /* new Const in it's predecessor block       */
238                 if (is_Phi(irn)) {
239                         tenv.block = get_Block_cfgpred_block(get_nodes_block(irn), i);
240                 }
241
242                 /* put the const into the block where the original const was */
243                 if (! (cg->opt & IA32_OPT_PLACECNST)) {
244                         tenv.block = get_nodes_block(pred);
245                 }
246
247                 switch (opc) {
248                         case iro_Const:
249                                 cnst = gen_Const(&tenv);
250                                 break;
251                         case iro_SymConst:
252                                 cnst = gen_SymConst(&tenv);
253                                 break;
254                         default:
255                                 break;
256                 }
257
258                 /* if we found a const, then set it */
259                 if (cnst) {
260                         set_irn_n(irn, i, cnst);
261                 }
262         }
263 }
264
265
266
267 /********************************************************************************************************
268  *  _____                _           _         ____        _   _           _          _   _
269  * |  __ \              | |         | |       / __ \      | | (_)         (_)        | | (_)
270  * | |__) |__  ___ _ __ | |__   ___ | | ___  | |  | |_ __ | |_ _ _ __ ___  _ ______ _| |_ _  ___  _ __
271  * |  ___/ _ \/ _ \ '_ \| '_ \ / _ \| |/ _ \ | |  | | '_ \| __| | '_ ` _ \| |_  / _` | __| |/ _ \| '_ \
272  * | |  |  __/  __/ |_) | | | | (_) | |  __/ | |__| | |_) | |_| | | | | | | |/ / (_| | |_| | (_) | | | |
273  * |_|   \___|\___| .__/|_| |_|\___/|_|\___|  \____/| .__/ \__|_|_| |_| |_|_/___\__,_|\__|_|\___/|_| |_|
274  *                | |                               | |
275  *                |_|                               |_|
276  ********************************************************************************************************/
277
278 /**
279  * NOTE: THESE PEEPHOLE OPTIMIZATIONS MUST BE CALLED AFTER SCHEDULING AND REGISTER ALLOCATION.
280  */
281
282 static int ia32_cnst_compare(ir_node *n1, ir_node *n2) {
283         return get_ia32_id_cnst(n1) == get_ia32_id_cnst(n2);
284 }
285
286 /**
287  * Checks for potential CJmp/CJmpAM optimization candidates.
288  */
289 static ir_node *ia32_determine_cjmp_cand(ir_node *irn, is_op_func_t *is_op_func) {
290         ir_node *cand = NULL;
291         ir_node *prev = sched_prev(irn);
292
293         if (is_Block(prev)) {
294                 if (get_Block_n_cfgpreds(prev) == 1)
295                         prev = get_Block_cfgpred(prev, 0);
296                 else
297                         prev = NULL;
298         }
299
300         /* The predecessor must be a ProjX. */
301         if (prev && is_Proj(prev) && get_irn_mode(prev) == mode_X) {
302                 prev = get_Proj_pred(prev);
303
304                 if (is_op_func(prev))
305                         cand = prev;
306         }
307
308         return cand;
309 }
310
311 static int is_TestJmp_cand(const ir_node *irn) {
312         return is_ia32_TestJmp(irn) || is_ia32_And(irn);
313 }
314
315 /**
316  * Checks if two consecutive arguments of cand matches
317  * the two arguments of irn (TestJmp).
318  */
319 static int is_TestJmp_replacement(ir_node *cand, ir_node *irn) {
320         ir_node *in1       = get_irn_n(irn, 0);
321         ir_node *in2       = get_irn_n(irn, 1);
322         int      i, n      = get_irn_arity(cand);
323         int      same_args = 0;
324
325         for (i = 0; i < n - 1; i++) {
326                 if (get_irn_n(cand, i)     == in1 &&
327                         get_irn_n(cand, i + 1) == in2)
328                 {
329                         same_args = 1;
330                         break;
331                 }
332         }
333
334         if (same_args)
335                 return ia32_cnst_compare(cand, irn);
336
337         return 0;
338 }
339
340 /**
341  * Tries to replace a TestJmp by a CJmp or CJmpAM (in case of And)
342  */
343 static void ia32_optimize_TestJmp(ir_node *irn, ia32_code_gen_t *cg) {
344         ir_node *cand    = ia32_determine_cjmp_cand(irn, is_TestJmp_cand);
345         int      replace = 0;
346
347         /* we found a possible candidate */
348         replace = cand ? is_TestJmp_replacement(cand, irn) : 0;
349
350         if (replace) {
351                 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
352
353                 if (is_ia32_And(cand))
354                         set_irn_op(irn, op_ia32_CJmpAM);
355                 else
356                         set_irn_op(irn, op_ia32_CJmp);
357
358                 DB((cg->mod, LEVEL_1, "%+F\n", irn));
359         }
360 }
361
362 static int is_CondJmp_cand(const ir_node *irn) {
363         return is_ia32_CondJmp(irn) || is_ia32_Sub(irn);
364 }
365
366 /**
367  * Checks if the arguments of cand are the same of irn.
368  */
369 static int is_CondJmp_replacement(ir_node *cand, ir_node *irn) {
370         int i, n      = get_irn_arity(cand);
371         int same_args = 1;
372
373         for (i = 0; i < n; i++) {
374                 if (get_irn_n(cand, i) != get_irn_n(irn, i)) {
375                         same_args = 0;
376                         break;
377                 }
378         }
379
380         if (same_args)
381                 return ia32_cnst_compare(cand, irn);
382
383         return 0;
384 }
385
386 /**
387  * Tries to replace a CondJmp by a CJmpAM
388  */
389 static void ia32_optimize_CondJmp(ir_node *irn, ia32_code_gen_t *cg) {
390         ir_node *cand    = ia32_determine_cjmp_cand(irn, is_CondJmp_cand);
391         int      replace = 0;
392
393         /* we found a possible candidate */
394         replace = cand ? is_CondJmp_replacement(cand, irn) : 0;
395
396         if (replace) {
397                 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
398                 DBG_OPT_CJMP(irn);
399
400                 set_irn_op(irn, op_ia32_CJmpAM);
401
402                 DB((cg->mod, LEVEL_1, "%+F\n", irn));
403         }
404 }
405
406 /**
407  * Creates a Push from Store(IncSP(gp_reg_size))
408  */
409 static void ia32_create_Push(ir_node *irn, ia32_code_gen_t *cg) {
410         ir_node *sp  = get_irn_n(irn, 0);
411         ir_node *val, *next, *push, *bl, *proj_M, *proj_res, *old_proj_M;
412         const ir_edge_t *edge;
413
414         if (get_ia32_am_offs(irn) || !be_is_IncSP(sp))
415                 return;
416
417         if (arch_get_irn_register(cg->arch_env, get_irn_n(irn, 1)) !=
418                 &ia32_gp_regs[REG_GP_NOREG])
419                 return;
420
421         val = get_irn_n(irn, 2);
422         if (mode_is_float(get_irn_mode(val)))
423                 return;
424
425         if (be_get_IncSP_direction(sp) != be_stack_dir_expand ||
426                 be_get_IncSP_offset(sp) != get_mode_size_bytes(ia32_reg_classes[CLASS_ia32_gp].mode))
427                 return;
428
429         /* ok, translate into Push */
430         edge = get_irn_out_edge_first(irn);
431         old_proj_M = get_edge_src_irn(edge);
432
433         next = sched_next(irn);
434         sched_remove(irn);
435         sched_remove(sp);
436
437         bl   = get_nodes_block(irn);
438         push = new_rd_ia32_Push(NULL, current_ir_graph, bl,
439                 be_get_IncSP_pred(sp), val, be_get_IncSP_mem(sp));
440         proj_res = new_r_Proj(current_ir_graph, bl, push, get_irn_mode(sp), pn_ia32_Push_stack);
441         proj_M   = new_r_Proj(current_ir_graph, bl, push, mode_M, pn_ia32_Push_M);
442
443         /* the push must have SP out register */
444         arch_set_irn_register(cg->arch_env, push, arch_get_irn_register(cg->arch_env, sp));
445
446         exchange(old_proj_M, proj_M);
447         exchange(sp, proj_res);
448         sched_add_before(next, push);
449         sched_add_after(push, proj_res);
450 }
451
452 /**
453  * Creates a Pop from IncSP(Load(sp))
454  */
455 static void ia32_create_Pop(ir_node *irn, ia32_code_gen_t *cg) {
456         ir_node *old_proj_M = be_get_IncSP_mem(irn);
457         ir_node *load = skip_Proj(old_proj_M);
458         ir_node *old_proj_res = NULL;
459         ir_node *bl, *pop, *next, *proj_res, *proj_sp, *proj_M;
460         const ir_edge_t *edge;
461         const arch_register_t *reg, *sp;
462
463         if (! is_ia32_Load(load) || get_ia32_am_offs(load))
464                 return;
465
466         if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 1)) !=
467                 &ia32_gp_regs[REG_GP_NOREG])
468                 return;
469         if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 0)) != cg->isa->arch_isa.sp)
470                 return;
471
472         /* ok, translate into pop */
473         foreach_out_edge(load, edge) {
474                 ir_node *succ = get_edge_src_irn(edge);
475                 if (succ != old_proj_M) {
476                         old_proj_res = succ;
477                         break;
478                 }
479         }
480         if (! old_proj_res) {
481                 assert(0);
482                 return; /* should not happen */
483         }
484
485         bl = get_nodes_block(load);
486
487         /* IncSP is typically scheduled after the load, so remove it first */
488         sched_remove(irn);
489         next = sched_next(old_proj_res);
490         sched_remove(old_proj_res);
491         sched_remove(load);
492
493         reg = arch_get_irn_register(cg->arch_env, load);
494         sp  = arch_get_irn_register(cg->arch_env, irn);
495
496         pop      = new_rd_ia32_Pop(NULL, current_ir_graph, bl, get_irn_n(irn, 0), get_irn_n(load, 2));
497         proj_res = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(old_proj_res), pn_ia32_Pop_res);
498         proj_sp  = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(irn), pn_ia32_Pop_stack);
499         proj_M   = new_r_Proj(current_ir_graph, bl, pop, mode_M, pn_ia32_Pop_M);
500
501         exchange(old_proj_M, proj_M);
502         exchange(old_proj_res, proj_res);
503         exchange(irn, proj_sp);
504
505         arch_set_irn_register(cg->arch_env, proj_res, reg);
506         arch_set_irn_register(cg->arch_env, proj_sp, sp);
507
508         sched_add_before(next, proj_sp);
509         sched_add_before(proj_sp, proj_res);
510         sched_add_before(proj_res,pop);
511 }
512
513 /**
514  * Tries to optimize two following IncSP.
515  */
516 static void ia32_optimize_IncSP(ir_node *irn, ia32_code_gen_t *cg) {
517         ir_node *prev = be_get_IncSP_pred(irn);
518         int real_uses = get_irn_n_edges(prev);
519
520         if (be_is_IncSP(prev) && real_uses == 1) {
521                 /* first IncSP has only one IncSP user, kill the first one */
522                 unsigned       prev_offs = be_get_IncSP_offset(prev);
523                 be_stack_dir_t prev_dir  = be_get_IncSP_direction(prev);
524                 unsigned       curr_offs = be_get_IncSP_offset(irn);
525                 be_stack_dir_t curr_dir  = be_get_IncSP_direction(irn);
526
527                 int new_ofs = prev_offs * (prev_dir == be_stack_dir_expand ? -1 : +1) +
528                                     curr_offs * (curr_dir == be_stack_dir_expand ? -1 : +1);
529
530                 if (new_ofs < 0) {
531                         new_ofs  = -new_ofs;
532                         curr_dir = be_stack_dir_expand;
533                 }
534                 else
535                         curr_dir = be_stack_dir_shrink;
536                 be_set_IncSP_offset(prev, 0);
537                 be_set_IncSP_offset(irn, (unsigned)new_ofs);
538                 be_set_IncSP_direction(irn, curr_dir);
539
540                 /* Omit the optimized IncSP */
541                 be_set_IncSP_pred(irn, be_get_IncSP_pred(prev));
542         }
543 }
544
545 /**
546  * Performs Peephole Optimizations.
547  */
548 void ia32_peephole_optimization(ir_node *irn, void *env) {
549         ia32_code_gen_t *cg = env;
550
551         if (is_ia32_TestJmp(irn))
552                 ia32_optimize_TestJmp(irn, cg);
553         else if (is_ia32_CondJmp(irn))
554                 ia32_optimize_CondJmp(irn, cg);
555         else if (be_is_IncSP(irn))
556                 ia32_optimize_IncSP(irn, cg);
557 }
558
559
560
561 /******************************************************************
562  *              _     _                   __  __           _
563  *     /\      | |   | |                 |  \/  |         | |
564  *    /  \   __| | __| |_ __ ___  ___ ___| \  / | ___   __| | ___
565  *   / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
566  *  / ____ \ (_| | (_| | | |  __/\__ \__ \ |  | | (_) | (_| |  __/
567  * /_/    \_\__,_|\__,_|_|  \___||___/___/_|  |_|\___/ \__,_|\___|
568  *
569  ******************************************************************/
570
571 static int node_is_ia32_comm(const ir_node *irn) {
572         return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
573 }
574
575 static int ia32_get_irn_n_edges(const ir_node *irn) {
576         const ir_edge_t *edge;
577         int cnt = 0;
578
579         foreach_out_edge(irn, edge) {
580                 cnt++;
581         }
582
583         return cnt;
584 }
585
586 /**
587  * Returns the first mode_M Proj connected to irn.
588  */
589 static ir_node *get_mem_proj(const ir_node *irn) {
590         const ir_edge_t *edge;
591         ir_node         *src;
592
593         assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
594
595         foreach_out_edge(irn, edge) {
596                 src = get_edge_src_irn(edge);
597
598                 assert(is_Proj(src) && "Proj expected");
599
600                 if (get_irn_mode(src) == mode_M)
601                         return src;
602         }
603
604         return NULL;
605 }
606
607 /**
608  * Returns the first Proj with mode != mode_M connected to irn.
609  */
610 static ir_node *get_res_proj(const ir_node *irn) {
611         const ir_edge_t *edge;
612         ir_node         *src;
613
614         assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
615
616         foreach_out_edge(irn, edge) {
617                 src = get_edge_src_irn(edge);
618
619                 assert(is_Proj(src) && "Proj expected");
620
621                 if (get_irn_mode(src) != mode_M)
622                         return src;
623         }
624
625         return NULL;
626 }
627
628 /**
629  * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
630  *
631  * @param pred       The node to be checked
632  * @param is_op_func The check-function
633  * @return 1 if conditions are fulfilled, 0 otherwise
634  */
635 static int pred_is_specific_node(const ir_node *pred, is_op_func_t *is_op_func) {
636         if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
637                 return 1;
638         }
639
640         return 0;
641 }
642
643 /**
644  * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
645  * and if the predecessor is in block bl.
646  *
647  * @param bl         The block
648  * @param pred       The node to be checked
649  * @param is_op_func The check-function
650  * @return 1 if conditions are fulfilled, 0 otherwise
651  */
652 static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
653         int (*is_op_func)(const ir_node *n))
654 {
655         if (is_Proj(pred)) {
656                 pred = get_Proj_pred(pred);
657                 if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
658                         return 1;
659                 }
660         }
661
662         return 0;
663 }
664
665
666
667 /**
668  * Checks if irn is a candidate for address calculation or address mode.
669  *
670  * address calculation (AC):
671  * - none of the operand must be a Load  within the same block OR
672  * - all Loads must have more than one user                    OR
673  * - the irn has a frame entity (it's a former FrameAddr)
674  *
675  * address mode (AM):
676  * - at least one operand has to be a Load within the same block AND
677  * - the load must not have other users than the irn             AND
678  * - the irn must not have a frame entity set
679  *
680  * @param block       The block the Loads must/not be in
681  * @param irn         The irn to check
682  * @param check_addr  1 if to check for address calculation, 0 otherwise
683  * return 1 if irn is a candidate for AC or AM, 0 otherwise
684  */
685 static int is_candidate(const ir_node *block, const ir_node *irn, int check_addr) {
686         ir_node *in;
687         int      n, is_cand = check_addr;
688
689         in = get_irn_n(irn, 2);
690
691         if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
692                 n         = ia32_get_irn_n_edges(in);
693                 is_cand   = check_addr ? (n == 1 ? 0 : is_cand) : (n == 1 ? 1 : is_cand);
694         }
695
696         in = get_irn_n(irn, 3);
697
698         if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
699                 n         = ia32_get_irn_n_edges(in);
700                 is_cand   = check_addr ? (n == 1 ? 0 : is_cand) : (n == 1 ? 1 : is_cand);
701         }
702
703         is_cand = get_ia32_frame_ent(irn) ? (check_addr ? 1 : 0) : is_cand;
704
705         return is_cand;
706 }
707
708 /**
709  * Compares the base and index addr and the load/store entities
710  * and returns 1 if they are equal.
711  */
712 static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
713                                                                         const ir_node *addr_b, const ir_node *addr_i)
714 {
715         int     is_equal = (addr_b == get_irn_n(load, 0)) && (addr_i == get_irn_n(load, 1));
716         entity *lent     = get_ia32_frame_ent(load);
717         entity *sent     = get_ia32_frame_ent(store);
718         ident  *lid      = get_ia32_am_sc(load);
719         ident  *sid      = get_ia32_am_sc(store);
720         char   *loffs    = get_ia32_am_offs(load);
721         char   *soffs    = get_ia32_am_offs(store);
722
723         /* are both entities set and equal? */
724         if (is_equal && (lent || sent))
725                 is_equal = lent && sent && (lent == sent);
726
727         /* are address mode idents set and equal? */
728         if (is_equal && (lid || sid))
729                 is_equal = lid && sid && (lid == sid);
730
731         /* are offsets set and equal */
732         if (is_equal && (loffs || soffs))
733                 is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;
734
735         /* are the load and the store of the same mode? */
736         is_equal = is_equal ? get_ia32_ls_mode(load) == get_ia32_ls_mode(store) : 0;
737
738         return is_equal;
739 }
740
741 typedef enum _ia32_take_lea_attr {
742         IA32_LEA_ATTR_NONE  = 0,
743         IA32_LEA_ATTR_BASE  = (1 << 0),
744         IA32_LEA_ATTR_INDEX = (1 << 1),
745         IA32_LEA_ATTR_OFFS  = (1 << 2),
746         IA32_LEA_ATTR_SCALE = (1 << 3),
747         IA32_LEA_ATTR_AMSC  = (1 << 4),
748         IA32_LEA_ATTR_FENT  = (1 << 5)
749 } ia32_take_lea_attr;
750
751 /**
752  * Decides if we have to keep the LEA operand or if we can assimilate it.
753  */
754 static int do_new_lea(ir_node *irn, ir_node *base, ir_node *index, ir_node *lea,
755                 int have_am_sc, ia32_code_gen_t *cg)
756 {
757         ir_node *lea_base = get_irn_n(lea, 0);
758         ir_node *lea_idx  = get_irn_n(lea, 1);
759         entity  *irn_ent  = get_ia32_frame_ent(irn);
760         entity  *lea_ent  = get_ia32_frame_ent(lea);
761         int      ret_val  = 0;
762         int      is_noreg_base  = be_is_NoReg(cg, base);
763         int      is_noreg_index = be_is_NoReg(cg, index);
764         ia32_am_flavour_t am_flav = get_ia32_am_flavour(lea);
765
766         /* If the Add and the LEA both have a different frame entity set: keep */
767         if (irn_ent && lea_ent && (irn_ent != lea_ent))
768                 return IA32_LEA_ATTR_NONE;
769         else if (! irn_ent && lea_ent)
770                 ret_val |= IA32_LEA_ATTR_FENT;
771
772         /* If the Add and the LEA both have already an address mode symconst: keep */
773         if (have_am_sc && get_ia32_am_sc(lea))
774                 return IA32_LEA_ATTR_NONE;
775         else if (get_ia32_am_sc(lea))
776                 ret_val |= IA32_LEA_ATTR_AMSC;
777
778         /* Check the different base-index combinations */
779
780         if (! is_noreg_base && ! is_noreg_index) {
781                 /* Assimilate if base is the lea and the LEA is just a Base + Offset calculation */
782                 if ((base == lea) && ! (am_flav & ia32_I ? 1 : 0)) {
783                         if (am_flav & ia32_O)
784                                 ret_val |= IA32_LEA_ATTR_OFFS;
785
786                         ret_val |= IA32_LEA_ATTR_BASE;
787                 }
788                 else
789                         return IA32_LEA_ATTR_NONE;
790         }
791         else if (! is_noreg_base && is_noreg_index) {
792                 /* Base is set but index not */
793                 if (base == lea) {
794                         /* Base points to LEA: assimilate everything */
795                         if (am_flav & ia32_O)
796                                 ret_val |= IA32_LEA_ATTR_OFFS;
797                         if (am_flav & ia32_S)
798                                 ret_val |= IA32_LEA_ATTR_SCALE;
799                         if (am_flav & ia32_I)
800                                 ret_val |= IA32_LEA_ATTR_INDEX;
801
802                         ret_val |= IA32_LEA_ATTR_BASE;
803                 }
804                 else if (am_flav & ia32_B ? 0 : 1) {
805                         /* Base is not the LEA but the LEA is an index only calculation: assimilate */
806                         if (am_flav & ia32_O)
807                                 ret_val |= IA32_LEA_ATTR_OFFS;
808                         if (am_flav & ia32_S)
809                                 ret_val |= IA32_LEA_ATTR_SCALE;
810
811                         ret_val |= IA32_LEA_ATTR_INDEX;
812                 }
813                 else
814                         return IA32_LEA_ATTR_NONE;
815         }
816         else if (is_noreg_base && ! is_noreg_index) {
817                 /* Index is set but not base */
818                 if (index == lea) {
819                         /* Index points to LEA: assimilate everything */
820                         if (am_flav & ia32_O)
821                                 ret_val |= IA32_LEA_ATTR_OFFS;
822                         if (am_flav & ia32_S)
823                                 ret_val |= IA32_LEA_ATTR_SCALE;
824                         if (am_flav & ia32_B)
825                                 ret_val |= IA32_LEA_ATTR_BASE;
826
827                         ret_val |= IA32_LEA_ATTR_INDEX;
828                 }
829                 else if (am_flav & ia32_I ? 0 : 1) {
830                         /* Index is not the LEA but the LEA is a base only calculation: assimilate */
831                         if (am_flav & ia32_O)
832                                 ret_val |= IA32_LEA_ATTR_OFFS;
833                         if (am_flav & ia32_S)
834                                 ret_val |= IA32_LEA_ATTR_SCALE;
835
836                         ret_val |= IA32_LEA_ATTR_BASE;
837                 }
838                 else
839                         return IA32_LEA_ATTR_NONE;
840         }
841         else {
842                 assert(0 && "There must have been set base or index");
843         }
844
845         return ret_val;
846 }
847
848
849 /**
850  * Folds Add or Sub to LEA if possible
851  */
852 static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn, ir_node *noreg) {
853         ir_graph   *irg        = get_irn_irg(irn);
854         dbg_info   *dbg        = get_irn_dbg_info(irn);
855         ir_node    *block      = get_nodes_block(irn);
856         ir_node    *res        = irn;
857         ir_node    *shift      = NULL;
858         ir_node    *lea_o      = NULL;
859         ir_node    *lea        = NULL;
860         char       *offs       = NULL;
861         const char *offs_cnst  = NULL;
862         char       *offs_lea   = NULL;
863         int         scale      = 0;
864         int         isadd      = 0;
865         int         dolea      = 0;
866         int         have_am_sc = 0;
867         int         am_sc_sign = 0;
868         ident      *am_sc      = NULL;
869         entity     *lea_ent    = NULL;
870         ir_node    *left, *right, *temp;
871         ir_node    *base, *index;
872         ia32_am_flavour_t am_flav;
873         DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
874
875         if (is_ia32_Add(irn))
876                 isadd = 1;
877
878         left  = get_irn_n(irn, 2);
879         right = get_irn_n(irn, 3);
880
881         /* "normalize" arguments in case of add with two operands */
882         if  (isadd && ! be_is_NoReg(cg, right)) {
883                 /* put LEA == ia32_am_O as right operand */
884                 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
885                         set_irn_n(irn, 2, right);
886                         set_irn_n(irn, 3, left);
887                         temp  = left;
888                         left  = right;
889                         right = temp;
890                 }
891
892                 /* put LEA != ia32_am_O as left operand */
893                 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
894                         set_irn_n(irn, 2, right);
895                         set_irn_n(irn, 3, left);
896                         temp  = left;
897                         left  = right;
898                         right = temp;
899                 }
900
901                 /* put SHL as left operand iff left is NOT a LEA */
902                 if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
903                         set_irn_n(irn, 2, right);
904                         set_irn_n(irn, 3, left);
905                         temp  = left;
906                         left  = right;
907                         right = temp;
908                 }
909         }
910
911         base    = left;
912         index   = noreg;
913         offs    = NULL;
914         scale   = 0;
915         am_flav = 0;
916
917         /* check for operation with immediate */
918         if (is_ia32_ImmConst(irn)) {
919                 DBG((mod, LEVEL_1, "\tfound op with imm const"));
920
921                 offs_cnst = get_ia32_cnst(irn);
922                 dolea     = 1;
923         }
924         else if (is_ia32_ImmSymConst(irn)) {
925                 DBG((mod, LEVEL_1, "\tfound op with imm symconst"));
926
927                 have_am_sc = 1;
928                 dolea      = 1;
929                 am_sc      = get_ia32_id_cnst(irn);
930                 am_sc_sign = is_ia32_am_sc_sign(irn);
931         }
932
933         /* determine the operand which needs to be checked */
934         if (be_is_NoReg(cg, right)) {
935                 temp = left;
936         }
937         else {
938                 temp = right;
939         }
940
941         /* check if right operand is AMConst (LEA with ia32_am_O)  */
942         /* but we can only eat it up if there is no other symconst */
943         /* because the linker won't accept two symconsts           */
944         if (! have_am_sc && is_ia32_Lea(temp) && get_ia32_am_flavour(temp) == ia32_am_O) {
945                 DBG((mod, LEVEL_1, "\tgot op with LEA am_O"));
946
947                 offs_lea   = get_ia32_am_offs(temp);
948                 am_sc      = get_ia32_am_sc(temp);
949                 am_sc_sign = is_ia32_am_sc_sign(temp);
950                 have_am_sc = 1;
951                 dolea      = 1;
952                 lea_o      = temp;
953         }
954
955         if (isadd) {
956                 /* default for add -> make right operand to index */
957                 index = right;
958                 dolea = 1;
959
960                 DBG((mod, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
961
962                 /* determine the operand which needs to be checked */
963                 temp = left;
964                 if (is_ia32_Lea(left)) {
965                         temp = right;
966                 }
967
968                 /* check for SHL 1,2,3 */
969                 if (pred_is_specific_node(temp, is_ia32_Shl)) {
970                         temp  = get_Proj_pred(temp);
971                         shift = temp;
972
973                         if (get_ia32_Immop_tarval(temp)) {
974                                 scale = get_tarval_long(get_ia32_Immop_tarval(temp));
975
976                                 if (scale <= 3) {
977                                         index = get_irn_n(temp, 2);
978
979                                         DBG((mod, LEVEL_1, "\tgot scaled index %+F\n", index));
980                                 }
981                                 else {
982                                         scale = 0;
983                                         shift = NULL;
984                                 }
985                         }
986                 }
987
988                 /* fix base */
989                 if (! be_is_NoReg(cg, index)) {
990                         /* if we have index, but left == right -> no base */
991                         if (left == right) {
992                                 base = noreg;
993                         }
994                         else if (! is_ia32_Lea(left) && (index != right)) {
995                                 /* index != right -> we found a good Shl           */
996                                 /* left  != LEA   -> this Shl was the left operand */
997                                 /* -> base is right operand                        */
998                                 base = right;
999                         }
1000                 }
1001         }
1002
1003         /* Try to assimilate a LEA as left operand */
1004         if (is_ia32_Lea(left) && (get_ia32_am_flavour(left) != ia32_am_O)) {
1005                 /* check if we can assimilate the LEA */
1006                 int take_attr = do_new_lea(irn, base, index, left, have_am_sc, cg);
1007
1008                 if (take_attr == IA32_LEA_ATTR_NONE) {
1009                         DBG((mod, LEVEL_1, "\tleave old LEA, creating new one\n"));
1010                 }
1011                 else {
1012                         DBG((mod, LEVEL_1, "\tgot LEA as left operand ... assimilating\n"));
1013                         lea = left; /* for statistics */
1014
1015                         if (take_attr & IA32_LEA_ATTR_OFFS)
1016                                 offs = get_ia32_am_offs(left);
1017
1018                         if (take_attr & IA32_LEA_ATTR_AMSC) {
1019                                 am_sc      = get_ia32_am_sc(left);
1020                                 have_am_sc = 1;
1021                                 am_sc_sign = is_ia32_am_sc_sign(left);
1022                         }
1023
1024                         if (take_attr & IA32_LEA_ATTR_SCALE)
1025                                 scale = get_ia32_am_scale(left);
1026
1027                         if (take_attr & IA32_LEA_ATTR_BASE)
1028                                 base = get_irn_n(left, 0);
1029
1030                         if (take_attr & IA32_LEA_ATTR_INDEX)
1031                                 index = get_irn_n(left, 1);
1032
1033                         if (take_attr & IA32_LEA_ATTR_FENT)
1034                                 lea_ent = get_ia32_frame_ent(left);
1035                 }
1036         }
1037
1038         /* ok, we can create a new LEA */
1039         if (dolea) {
1040                 res = new_rd_ia32_Lea(dbg, irg, block, base, index, mode_Is);
1041
1042                 /* add the old offset of a previous LEA */
1043                 if (offs) {
1044                         add_ia32_am_offs(res, offs);
1045                 }
1046
1047                 /* add the new offset */
1048                 if (isadd) {
1049                         if (offs_cnst) {
1050                                 add_ia32_am_offs(res, offs_cnst);
1051                         }
1052                         if (offs_lea) {
1053                                 add_ia32_am_offs(res, offs_lea);
1054                         }
1055                 }
1056                 else {
1057                         /* either lea_O-cnst, -cnst or -lea_O  */
1058                         if (offs_cnst) {
1059                                 if (offs_lea) {
1060                                         add_ia32_am_offs(res, offs_lea);
1061                                 }
1062
1063                                 sub_ia32_am_offs(res, offs_cnst);
1064                         }
1065                         else {
1066                                 sub_ia32_am_offs(res, offs_lea);
1067                         }
1068                 }
1069
1070                 /* set the address mode symconst */
1071                 if (have_am_sc) {
1072                         set_ia32_am_sc(res, am_sc);
1073                         if (am_sc_sign)
1074                                 set_ia32_am_sc_sign(res);
1075                 }
1076
1077                 /* copy the frame entity (could be set in case of Add */
1078                 /* which was a FrameAddr) */
1079                 if (lea_ent)
1080                         set_ia32_frame_ent(res, lea_ent);
1081                 else
1082                         set_ia32_frame_ent(res, get_ia32_frame_ent(irn));
1083
1084                 if (get_ia32_frame_ent(res))
1085                         set_ia32_use_frame(res);
1086
1087                 /* set scale */
1088                 set_ia32_am_scale(res, scale);
1089
1090                 am_flav = ia32_am_N;
1091                 /* determine new am flavour */
1092                 if (offs || offs_cnst || offs_lea) {
1093                         am_flav |= ia32_O;
1094                 }
1095                 if (! be_is_NoReg(cg, base)) {
1096                         am_flav |= ia32_B;
1097                 }
1098                 if (! be_is_NoReg(cg, index)) {
1099                         am_flav |= ia32_I;
1100                 }
1101                 if (scale > 0) {
1102                         am_flav |= ia32_S;
1103                 }
1104                 set_ia32_am_flavour(res, am_flav);
1105
1106                 set_ia32_op_type(res, ia32_AddrModeS);
1107
1108                 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
1109
1110                 DBG((mod, LEVEL_1, "\tLEA [%+F + %+F * %d + %s]\n", base, index, scale, get_ia32_am_offs(res)));
1111
1112                 /* we will exchange it, report here before the Proj is created */
1113                 if (shift && lea && lea_o)
1114                         DBG_OPT_LEA4(irn, lea_o, lea, shift, res);
1115                 else if (shift && lea)
1116                         DBG_OPT_LEA3(irn, lea, shift, res);
1117                 else if (shift && lea_o)
1118                         DBG_OPT_LEA3(irn, lea_o, shift, res);
1119                 else if (lea && lea_o)
1120                         DBG_OPT_LEA3(irn, lea_o, lea, res);
1121                 else if (shift)
1122                         DBG_OPT_LEA2(irn, shift, res);
1123                 else if (lea)
1124                         DBG_OPT_LEA2(irn, lea, res);
1125                 else if (lea_o)
1126                         DBG_OPT_LEA2(irn, lea_o, res);
1127                 else
1128                         DBG_OPT_LEA1(irn, res);
1129
1130                 /* get the result Proj of the Add/Sub */
1131                 irn = get_res_proj(irn);
1132
1133                 assert(irn && "Couldn't find result proj");
1134
1135                 /* exchange the old op with the new LEA */
1136                 exchange(irn, res);
1137         }
1138
1139         return res;
1140 }
1141
1142
1143 /**
1144  * Merges a Load/Store node with a LEA.
1145  * @param irn The Load/Store node
1146  * @param lea The LEA
1147  */
1148 static void merge_loadstore_lea(ir_node *irn, ir_node *lea) {
1149         entity *irn_ent = get_ia32_frame_ent(irn);
1150         entity *lea_ent = get_ia32_frame_ent(lea);
1151
1152         /* If the irn and the LEA both have a different frame entity set: do not merge */
1153         if (irn_ent && lea_ent && (irn_ent != lea_ent))
1154                 return;
1155         else if (! irn_ent && lea_ent) {
1156                 set_ia32_frame_ent(irn, lea_ent);
1157                 set_ia32_use_frame(irn);
1158         }
1159
1160         /* get the AM attributes from the LEA */
1161         add_ia32_am_offs(irn, get_ia32_am_offs(lea));
1162         set_ia32_am_scale(irn, get_ia32_am_scale(lea));
1163         set_ia32_am_flavour(irn, get_ia32_am_flavour(lea));
1164
1165         set_ia32_am_sc(irn, get_ia32_am_sc(lea));
1166         if (is_ia32_am_sc_sign(lea))
1167                 set_ia32_am_sc_sign(irn);
1168
1169         set_ia32_op_type(irn, is_ia32_Ld(irn) ? ia32_AddrModeS : ia32_AddrModeD);
1170
1171         /* set base and index */
1172         set_irn_n(irn, 0, get_irn_n(lea, 0));
1173         set_irn_n(irn, 1, get_irn_n(lea, 1));
1174
1175         /* clear remat flag */
1176         set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1177
1178         if (is_ia32_Ld(irn))
1179                 DBG_OPT_LOAD_LEA(lea, irn);
1180         else
1181                 DBG_OPT_STORE_LEA(lea, irn);
1182
1183 }
1184
1185 /**
1186  * Optimizes a pattern around irn to address mode if possible.
1187  */
1188 void ia32_optimize_am(ir_node *irn, void *env) {
1189         ia32_code_gen_t   *cg   = env;
1190         ir_node           *res  = irn;
1191         dbg_info          *dbg;
1192         ir_mode           *mode;
1193         ir_node           *block, *noreg_gp, *noreg_fp;
1194         ir_node           *left, *right, *temp;
1195         ir_node           *store, *load, *mem_proj;
1196         ir_node           *succ, *addr_b, *addr_i;
1197         int                check_am_src = 0;
1198         DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
1199
1200         if (! is_ia32_irn(irn))
1201                 return;
1202
1203         dbg      = get_irn_dbg_info(irn);
1204         mode     = get_irn_mode(irn);
1205         block    = get_nodes_block(irn);
1206         noreg_gp = ia32_new_NoReg_gp(cg);
1207         noreg_fp = ia32_new_NoReg_fp(cg);
1208
1209         DBG((mod, LEVEL_1, "checking for AM\n"));
1210
1211         /* 1st part: check for address calculations and transform the into Lea */
1212
1213         /* Following cases can occur:                                  */
1214         /* - Sub (l, imm) -> LEA [base - offset]                       */
1215         /* - Sub (l, r == LEA with ia32_am_O)   -> LEA [base - offset] */
1216         /* - Add (l, imm) -> LEA [base + offset]                       */
1217         /* - Add (l, r == LEA with ia32_am_O)  -> LEA [base + offset]  */
1218         /* - Add (l == LEA with ia32_am_O, r)  -> LEA [base + offset]  */
1219         /* - Add (l, r) -> LEA [base + index * scale]                  */
1220         /*              with scale > 1 iff l/r == shl (1,2,3)          */
1221
1222         if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
1223                 left  = get_irn_n(irn, 2);
1224                 right = get_irn_n(irn, 3);
1225
1226             /* Do not try to create a LEA if one of the operands is a Load. */
1227                 /* check is irn is a candidate for address calculation */
1228                 if (is_candidate(block, irn, 1)) {
1229                         DBG((mod, LEVEL_1, "\tfound address calculation candidate %+F ... ", irn));
1230                         res = fold_addr(cg, irn, noreg_gp);
1231
1232                         if (res == irn)
1233                                 DB((mod, LEVEL_1, "transformed into %+F\n", res));
1234                         else
1235                                 DB((mod, LEVEL_1, "not transformed\n"));
1236                 }
1237         }
1238
1239         /* 2nd part: fold following patterns:                                               */
1240         /* - Load  -> LEA into Load  } TODO: If the LEA is used by more than one Load/Store */
1241         /* - Store -> LEA into Store }       it might be better to keep the LEA             */
1242         /* - op -> Load into AMop with am_Source                                            */
1243         /*   conditions:                                                                    */
1244         /*     - op is am_Source capable AND                                                */
1245         /*     - the Load is only used by this op AND                                       */
1246         /*     - the Load is in the same block                                              */
1247         /* - Store -> op -> Load  into AMop with am_Dest                                    */
1248         /*   conditions:                                                                    */
1249         /*     - op is am_Dest capable AND                                                  */
1250         /*     - the Store uses the same address as the Load AND                            */
1251         /*     - the Load is only used by this op AND                                       */
1252         /*     - the Load and Store are in the same block AND                               */
1253         /*     - nobody else uses the result of the op                                      */
1254
1255         if ((res == irn) && (get_ia32_am_support(irn) != ia32_am_None) && !is_ia32_Lea(irn)) {
1256                 /* 1st: check for Load/Store -> LEA   */
1257                 if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn)) {
1258                         left = get_irn_n(irn, 0);
1259
1260                         if (is_ia32_Lea(left)) {
1261                                 const ir_edge_t *edge, *ne;
1262                                 ir_node *src;
1263
1264                                 /* merge all Loads/Stores connected to this LEA with the LEA */
1265                                 foreach_out_edge_safe(left, edge, ne) {
1266                                         src = get_edge_src_irn(edge);
1267
1268                                         if (src && (is_ia32_Ld(src) || is_ia32_St(src) || is_ia32_Store8Bit(src))) {
1269                                                 DBG((mod, LEVEL_1, "\nmerging %+F into %+F\n", left, irn));
1270                                                 merge_loadstore_lea(src, left);
1271                                         }
1272                                 }
1273                         }
1274                 }
1275                 /* check if the node is an address mode candidate */
1276                 else if (is_candidate(block, irn, 0)) {
1277                         DBG((mod, LEVEL_1, "\tfound address mode candidate %+F ... ", irn));
1278
1279                         left  = get_irn_n(irn, 2);
1280                         if (get_irn_arity(irn) == 4) {
1281                                 /* it's an "unary" operation */
1282                                 right = left;
1283                         }
1284                         else {
1285                                 right = get_irn_n(irn, 3);
1286                         }
1287
1288                         /* normalize commutative ops */
1289                         if (node_is_ia32_comm(irn)) {
1290                                 /* Assure that right operand is always a Load if there is one    */
1291                                 /* because non-commutative ops can only use Dest AM if the right */
1292                                 /* operand is a load, so we only need to check right operand.    */
1293                                 if (pred_is_specific_nodeblock(block, left, is_ia32_Ld))
1294                                 {
1295                                         set_irn_n(irn, 2, right);
1296                                         set_irn_n(irn, 3, left);
1297
1298                                         temp  = left;
1299                                         left  = right;
1300                                         right = temp;
1301
1302                                         /* this is only needed for Compares, but currently ALL nodes
1303                                          * have this attribute :-) */
1304                                         set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1305                                 }
1306                         }
1307
1308                         /* check for Store -> op -> Load */
1309
1310                         /* Store -> op -> Load optimization is only possible if supported by op */
1311                         /* and if right operand is a Load                                       */
1312                         if ((get_ia32_am_support(irn) & ia32_am_Dest) &&
1313                                  pred_is_specific_nodeblock(block, right, is_ia32_Ld))
1314                         {
1315
1316                                 /* An address mode capable op always has a result Proj.                  */
1317                                 /* If this Proj is used by more than one other node, we don't need to    */
1318                                 /* check further, otherwise we check for Store and remember the address, */
1319                                 /* the Store points to. */
1320
1321                                 succ = get_res_proj(irn);
1322                                 assert(succ && "Couldn't find result proj");
1323
1324                                 addr_b = NULL;
1325                                 addr_i = NULL;
1326                                 store  = NULL;
1327
1328                                 /* now check for users and Store */
1329                                 if (ia32_get_irn_n_edges(succ) == 1) {
1330                                         succ = get_edge_src_irn(get_irn_out_edge_first(succ));
1331
1332                                         if (is_ia32_xStore(succ) || is_ia32_Store(succ)) {
1333                                                 store  = succ;
1334                                                 addr_b = get_irn_n(store, 0);
1335                                                 addr_i = get_irn_n(store, 1);
1336                                         }
1337                                 }
1338
1339                                 if (store) {
1340                                         /* we found a Store as single user: Now check for Load */
1341
1342                                         /* Extra check for commutative ops with two Loads */
1343                                         /* -> put the interesting Load right              */
1344                                         if (node_is_ia32_comm(irn) &&
1345                                                 pred_is_specific_nodeblock(block, left, is_ia32_Ld))
1346                                         {
1347                                                 if ((addr_b == get_irn_n(get_Proj_pred(left), 0)) &&
1348                                                         (addr_i == get_irn_n(get_Proj_pred(left), 1)))
1349                                                 {
1350                                                         /* We exchange left and right, so it's easier to kill     */
1351                                                         /* the correct Load later and to handle unary operations. */
1352                                                         set_irn_n(irn, 2, right);
1353                                                         set_irn_n(irn, 3, left);
1354
1355                                                         temp  = left;
1356                                                         left  = right;
1357                                                         right = temp;
1358
1359                                                         /* this is only needed for Compares, but currently ALL nodes
1360                                                          * have this attribute :-) */
1361                                                         set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1362                                                 }
1363                                         }
1364
1365                                         /* skip the Proj for easier access */
1366                                         load = get_Proj_pred(right);
1367
1368                                         /* Compare Load and Store address */
1369                                         if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1370                                                 /* Right Load is from same address, so we can */
1371                                                 /* disconnect the Load and Store here        */
1372
1373                                                 /* set new base, index and attributes */
1374                                                 set_irn_n(irn, 0, addr_b);
1375                                                 set_irn_n(irn, 1, addr_i);
1376                                                 add_ia32_am_offs(irn, get_ia32_am_offs(load));
1377                                                 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1378                                                 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1379                                                 set_ia32_op_type(irn, ia32_AddrModeD);
1380                                                 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1381                                                 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1382
1383                                                 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1384                                                 if (is_ia32_am_sc_sign(load))
1385                                                         set_ia32_am_sc_sign(irn);
1386
1387                                                 if (is_ia32_use_frame(load))
1388                                                         set_ia32_use_frame(irn);
1389
1390                                                 /* connect to Load memory and disconnect Load */
1391                                                 if (get_irn_arity(irn) == 5) {
1392                                                         /* binary AMop */
1393                                                         set_irn_n(irn, 4, get_irn_n(load, 2));
1394                                                         set_irn_n(irn, 3, noreg_gp);
1395                                                 }
1396                                                 else {
1397                                                         /* unary AMop */
1398                                                         set_irn_n(irn, 3, get_irn_n(load, 2));
1399                                                         set_irn_n(irn, 2, noreg_gp);
1400                                                 }
1401
1402                                                 /* connect the memory Proj of the Store to the op */
1403                                                 mem_proj = get_mem_proj(store);
1404                                                 set_Proj_pred(mem_proj, irn);
1405                                                 set_Proj_proj(mem_proj, 1);
1406
1407                                                 /* clear remat flag */
1408                                                 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1409
1410                                                 DBG_OPT_AM_D(load, store, irn);
1411
1412                                                 DB((mod, LEVEL_1, "merged with %+F and %+F into dest AM\n", load, store));
1413                                         }
1414                                 } /* if (store) */
1415                                 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1416                                         /* There was no store, check if we still can optimize for source address mode */
1417                                         check_am_src = 1;
1418                                 }
1419                         } /* if (support AM Dest) */
1420                         else if (get_ia32_am_support(irn) & ia32_am_Source) {
1421                                 /* op doesn't support am AM Dest -> check for AM Source */
1422                                 check_am_src = 1;
1423                         }
1424
1425                         /* normalize commutative ops */
1426                         if (check_am_src && node_is_ia32_comm(irn)) {
1427                                 /* Assure that left operand is always a Load if there is one */
1428                                 /* because non-commutative ops can only use Source AM if the */
1429                                 /* left operand is a Load, so we only need to check the left */
1430                                 /* operand afterwards.                                       */
1431                                 if (pred_is_specific_nodeblock(block, right, is_ia32_Ld))       {
1432                                         set_irn_n(irn, 2, right);
1433                                         set_irn_n(irn, 3, left);
1434
1435                                         temp  = left;
1436                                         left  = right;
1437                                         right = temp;
1438
1439                                         /* this is only needed for Compares, but currently ALL nodes
1440                                          * have this attribute :-) */
1441                                         set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1442                                 }
1443                         }
1444
1445                         /* optimize op -> Load iff Load is only used by this op   */
1446                         /* and left operand is a Load which only used by this irn */
1447                         if (check_am_src                                        &&
1448                                 pred_is_specific_nodeblock(block, left, is_ia32_Ld) &&
1449                                 (ia32_get_irn_n_edges(left) == 1))
1450                         {
1451                                 left = get_Proj_pred(left);
1452
1453                                 addr_b = get_irn_n(left, 0);
1454                                 addr_i = get_irn_n(left, 1);
1455
1456                                 /* set new base, index and attributes */
1457                                 set_irn_n(irn, 0, addr_b);
1458                                 set_irn_n(irn, 1, addr_i);
1459                                 add_ia32_am_offs(irn, get_ia32_am_offs(left));
1460                                 set_ia32_am_scale(irn, get_ia32_am_scale(left));
1461                                 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
1462                                 set_ia32_op_type(irn, ia32_AddrModeS);
1463                                 set_ia32_frame_ent(irn, get_ia32_frame_ent(left));
1464                                 set_ia32_ls_mode(irn, get_ia32_ls_mode(left));
1465
1466                                 set_ia32_am_sc(irn, get_ia32_am_sc(left));
1467                                 if (is_ia32_am_sc_sign(left))
1468                                         set_ia32_am_sc_sign(irn);
1469
1470                                 /* clear remat flag */
1471                                 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1472
1473                                 if (is_ia32_use_frame(left))
1474                                         set_ia32_use_frame(irn);
1475
1476                                 /* connect to Load memory */
1477                                 if (get_irn_arity(irn) == 5) {
1478                                         /* binary AMop */
1479                                         set_irn_n(irn, 4, get_irn_n(left, 2));
1480
1481                                         /* this is only needed for Compares, but currently ALL nodes
1482                                          * have this attribute :-) */
1483                                         set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1484
1485                                         /* disconnect from Load */
1486                                         /* (make second op -> first, set second in to noreg) */
1487                                         set_irn_n(irn, 2, get_irn_n(irn, 3));
1488                                         set_irn_n(irn, 3, noreg_gp);
1489                                 }
1490                                 else {
1491                                         /* unary AMop */
1492                                         set_irn_n(irn, 3, get_irn_n(left, 2));
1493
1494                                         /* disconnect from Load */
1495                                         set_irn_n(irn, 2, noreg_gp);
1496                                 }
1497
1498                                 DBG_OPT_AM_S(left, irn);
1499
1500                                 /* If Load has a memory Proj, connect it to the op */
1501                                 mem_proj = get_mem_proj(left);
1502                                 if (mem_proj) {
1503                                         set_Proj_pred(mem_proj, irn);
1504                                         set_Proj_proj(mem_proj, 1);
1505                                 }
1506
1507                                 DB((mod, LEVEL_1, "merged with %+F into source AM\n", left));
1508                         }
1509                 }
1510         }
1511 }