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