Added phi handler
[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
22 #undef is_NoMem
23 #define is_NoMem(irn) (get_irn_op(irn) == op_NoMem)
24
25 static int be_is_NoReg(be_abi_irg_t *babi, const ir_node *irn) {
26         if (be_abi_get_callee_save_irn(babi, &ia32_gp_regs[REG_XXX]) == irn ||
27                 be_abi_get_callee_save_irn(babi, &ia32_fp_regs[REG_XXXX]) == irn)
28         {
29                 return 1;
30         }
31
32         return 0;
33 }
34
35 /**
36  * creates a unique ident by adding a number to a tag
37  *
38  * @param tag   the tag string, must contain a %d if a number
39  *              should be added
40  */
41 static ident *unique_id(const char *tag)
42 {
43         static unsigned id = 0;
44         char str[256];
45
46         snprintf(str, sizeof(str), tag, ++id);
47         return new_id_from_str(str);
48 }
49
50
51
52 /**
53  * Transforms a SymConst.
54  *
55  * @param mod     the debug module
56  * @param block   the block the new node should belong to
57  * @param node    the ir SymConst node
58  * @param mode    mode of the SymConst
59  * @return the created ia32 Const node
60  */
61 static ir_node *gen_SymConst(ia32_transform_env_t *env) {
62         ir_node  *cnst;
63         dbg_info *dbg   = env->dbg;
64         ir_mode  *mode  = env->mode;
65         ir_graph *irg   = env->irg;
66         ir_node  *block = env->block;
67
68         cnst = new_rd_ia32_Const(dbg, irg, block, mode);
69         set_ia32_Const_attr(cnst, env->irn);
70         return cnst;
71 }
72
73 /**
74  * Get a primitive type for a mode.
75  */
76 static ir_type *get_prim_type(pmap *types, ir_mode *mode)
77 {
78         pmap_entry *e = pmap_find(types, mode);
79         ir_type *res;
80
81         if (! e) {
82                 char buf[64];
83                 snprintf(buf, sizeof(buf), "prim_type_%s", get_mode_name(mode));
84                 res = new_type_primitive(new_id_from_str(buf), mode);
85                 pmap_insert(types, mode, res);
86         }
87         else
88                 res = e->value;
89         return res;
90 }
91
92 /**
93  * Get an entity that is initialized with a tarval
94  */
95 static entity *get_entity_for_tv(ia32_code_gen_t *cg, ir_node *cnst)
96 {
97         tarval *tv    = get_Const_tarval(cnst);
98         pmap_entry *e = pmap_find(cg->tv_ent, tv);
99         entity *res;
100         ir_graph *rem;
101
102         if (! e) {
103                 ir_mode *mode = get_irn_mode(cnst);
104                 ir_type *tp = get_Const_type(cnst);
105                 if (tp == firm_unknown_type)
106                         tp = get_prim_type(cg->types, mode);
107
108                 res = new_entity(get_glob_type(), unique_id("ia32FloatCnst_%u"), tp);
109
110                 set_entity_ld_ident(res, get_entity_ident(res));
111                 set_entity_visibility(res, visibility_local);
112                 set_entity_variability(res, variability_constant);
113                 set_entity_allocation(res, allocation_static);
114
115                  /* we create a new entity here: It's initialization must resist on the
116                     const code irg */
117                 rem = current_ir_graph;
118                 current_ir_graph = get_const_code_irg();
119                 set_atomic_ent_value(res, new_Const_type(tv, tp));
120                 current_ir_graph = rem;
121         }
122         else
123                 res = e->value;
124         return res;
125 }
126
127 /**
128  * Transforms a Const.
129  *
130  * @param mod     the debug module
131  * @param block   the block the new node should belong to
132  * @param node    the ir Const node
133  * @param mode    mode of the Const
134  * @return the created ia32 Const node
135  */
136 static ir_node *gen_Const(ia32_transform_env_t *env) {
137         ir_node *cnst;
138         symconst_symbol sym;
139         ir_graph *irg   = env->irg;
140         ir_node  *block = env->block;
141         ir_node  *node  = env->irn;
142         dbg_info *dbg   = env->dbg;
143         ir_mode  *mode  = env->mode;
144
145         if (mode_is_float(mode)) {
146                 sym.entity_p = get_entity_for_tv(env->cg, node);
147
148                 cnst = new_rd_SymConst(dbg, irg, block, sym, symconst_addr_ent);
149                 env->irn = cnst;
150                 cnst = gen_SymConst(env);
151         }
152         else {
153                 cnst = new_rd_ia32_Const(dbg, irg, block, get_irn_mode(node));
154                 set_ia32_Const_attr(cnst, node);
155         }
156         return cnst;
157 }
158
159
160
161 /**
162  * Transforms (all) Const's into ia32_Const and places them in the
163  * block where they are used (or in the cfg-pred Block in case of Phi's).
164  * Additionally all reference nodes are changed into mode_Is nodes.
165  */
166 void ia32_place_consts_set_modes(ir_node *irn, void *env) {
167         ia32_code_gen_t      *cg = env;
168         ia32_transform_env_t  tenv;
169         ir_mode              *mode;
170         ir_node              *pred, *cnst;
171         int                   i;
172         opcode                opc;
173
174         if (is_Block(irn))
175                 return;
176
177         mode = get_irn_mode(irn);
178
179         /* transform all reference nodes into mode_Is nodes */
180         if (mode_is_reference(mode)) {
181                 mode = mode_Is;
182                 set_irn_mode(irn, mode);
183         }
184
185         tenv.block    = get_nodes_block(irn);
186         tenv.cg       = cg;
187         tenv.irg      = cg->irg;
188         tenv.mod      = cg->mod;
189
190         /* Loop over all predecessors and check for Sym/Const nodes */
191         for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
192                 pred      = get_irn_n(irn, i);
193                 cnst      = NULL;
194                 opc       = get_irn_opcode(pred);
195                 tenv.irn  = pred;
196                 tenv.mode = get_irn_mode(pred);
197                 tenv.dbg  = get_irn_dbg_info(pred);
198
199                 /* If it's a Phi, then we need to create the */
200                 /* new Const in it's predecessor block       */
201                 if (is_Phi(irn)) {
202                         tenv.block = get_Block_cfgpred_block(get_nodes_block(irn), i);
203                 }
204
205                 /* put the const into the block where the original const was */
206                 if (! cg->opt.placecnst) {
207                         tenv.block = get_nodes_block(pred);
208                 }
209
210                 switch (opc) {
211                         case iro_Const:
212                                 cnst = gen_Const(&tenv);
213                                 break;
214                         case iro_SymConst:
215                                 cnst = gen_SymConst(&tenv);
216                                 break;
217                         default:
218                                 break;
219                 }
220
221                 /* if we found a const, then set it */
222                 if (cnst) {
223                         set_irn_n(irn, i, cnst);
224                 }
225         }
226 }
227
228
229 /******************************************************************
230  *              _     _                   __  __           _
231  *     /\      | |   | |                 |  \/  |         | |
232  *    /  \   __| | __| |_ __ ___  ___ ___| \  / | ___   __| | ___
233  *   / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
234  *  / ____ \ (_| | (_| | | |  __/\__ \__ \ |  | | (_) | (_| |  __/
235  * /_/    \_\__,_|\__,_|_|  \___||___/___/_|  |_|\___/ \__,_|\___|
236  *
237  ******************************************************************/
238
239 static int node_is_comm(const ir_node *irn) {
240         return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
241 }
242
243 static int ia32_get_irn_n_edges(const ir_node *irn) {
244         const ir_edge_t *edge;
245         int cnt = 0;
246
247         foreach_out_edge(irn, edge) {
248                 cnt++;
249         }
250
251         return cnt;
252 }
253
254 /**
255  * Returns the first mode_M Proj connected to irn.
256  */
257 static ir_node *get_mem_proj(const ir_node *irn) {
258         const ir_edge_t *edge;
259         ir_node         *src;
260
261         assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
262
263         foreach_out_edge(irn, edge) {
264                 src = get_edge_src_irn(edge);
265
266                 assert(is_Proj(src) && "Proj expected");
267
268                 if (get_irn_mode(src) == mode_M)
269                         return src;
270         }
271
272         return NULL;
273 }
274
275 /**
276  * Returns the first Proj with mode != mode_M connected to irn.
277  */
278 static ir_node *get_res_proj(const ir_node *irn) {
279         const ir_edge_t *edge;
280         ir_node         *src;
281
282         assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
283
284         foreach_out_edge(irn, edge) {
285                 src = get_edge_src_irn(edge);
286
287                 assert(is_Proj(src) && "Proj expected");
288
289                 if (get_irn_mode(src) != mode_M)
290                         return src;
291         }
292
293         return NULL;
294 }
295
296 /**
297  * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
298  *
299  * @param pred       The node to be checked
300  * @param is_op_func The check-function
301  * @return 1 if conditions are fulfilled, 0 otherwise
302  */
303 static int pred_is_specific_node(const ir_node *pred, int (*is_op_func)(const ir_node *n)) {
304         if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
305                 return 1;
306         }
307
308         return 0;
309 }
310
311 /**
312  * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
313  * and if the predecessor is in block bl.
314  *
315  * @param bl         The block
316  * @param pred       The node to be checked
317  * @param is_op_func The check-function
318  * @return 1 if conditions are fulfilled, 0 otherwise
319  */
320 static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
321         int (*is_op_func)(const ir_node *n))
322 {
323         if (is_Proj(pred)) {
324                 pred = get_Proj_pred(pred);
325                 if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
326                         return 1;
327                 }
328         }
329
330         return 0;
331 }
332
333
334
335 /**
336  * Checks if irn is a candidate for address calculation or address mode.
337  *
338  * address calculation (AC):
339  * - none of the operand must be a Load  within the same block OR
340  * - all Loads must have more than one user                    OR
341  * - the irn has a frame entity (it's a former FrameAddr)
342  *
343  * address mode (AM):
344  * - at least one operand has to be a Load within the same block AND
345  * - the load must not have other users than the irn             AND
346  * - the irn must not have a frame entity set
347  *
348  * @param block       The block the Loads must/not be in
349  * @param irn         The irn to check
350  * @param check_addr  1 if to check for address calculation, 0 otherwise
351  * return 1 if irn is a candidate for AC or AM, 0 otherwise
352  */
353 static int is_candidate(const ir_node *block, const ir_node *irn, int check_addr) {
354         ir_node *in;
355         int      n, is_cand = check_addr;
356
357         in = get_irn_n(irn, 2);
358
359         if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
360                 n         = ia32_get_irn_n_edges(in);
361                 is_cand   = check_addr ? (n == 1 ? 0 : is_cand) : (n == 1 ? 1 : is_cand);
362         }
363
364         in = get_irn_n(irn, 3);
365
366         if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
367                 n         = ia32_get_irn_n_edges(in);
368                 is_cand   = check_addr ? (n == 1 ? 0 : is_cand) : (n == 1 ? 1 : is_cand);
369         }
370
371         is_cand = get_ia32_frame_ent(irn) ? (check_addr ? 1 : 0) : is_cand;
372
373         return is_cand;
374 }
375
376 /**
377  * Compares the base and index addr and the load/store entities
378  * and returns 1 if they are equal.
379  */
380 static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
381                                                                         const ir_node *addr_b, const ir_node *addr_i)
382 {
383         int     is_equal = (addr_b == get_irn_n(load, 0)) && (addr_i == get_irn_n(load, 1));
384         entity *lent     = get_ia32_frame_ent(load);
385         entity *sent     = get_ia32_frame_ent(store);
386
387         /* are both entities set and equal? */
388         is_equal = lent && sent && (lent == sent);
389
390         /* are the load and the store of the same mode? */
391         is_equal = get_ia32_ls_mode(load) == get_ia32_ls_mode(store);
392
393         return is_equal;
394 }
395
396
397
398 /**
399  * Folds Add or Sub to LEA if possible
400  */
401 static ir_node *fold_addr(be_abi_irg_t *babi, ir_node *irn, firm_dbg_module_t *mod, ir_node *noreg) {
402         ir_graph *irg       = get_irn_irg(irn);
403         dbg_info *dbg       = get_irn_dbg_info(irn);
404         ir_node  *block     = get_nodes_block(irn);
405         ir_node  *res       = irn;
406         char     *offs      = NULL;
407         char     *offs_cnst = NULL;
408         char     *offs_lea  = NULL;
409         int       scale     = 0;
410         int       isadd     = 0;
411         int       dolea     = 0;
412         ir_node  *left, *right, *temp;
413         ir_node  *base, *index;
414         ia32_am_flavour_t am_flav;
415
416         if (is_ia32_Add(irn))
417                 isadd = 1;
418
419         left  = get_irn_n(irn, 2);
420         right = get_irn_n(irn, 3);
421
422         /* "normalize" arguments in case of add with two operands */
423         if  (isadd && ! be_is_NoReg(babi, right)) {
424                 /* put LEA == ia32_am_O as right operand */
425                 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
426                         set_irn_n(irn, 2, right);
427                         set_irn_n(irn, 3, left);
428                         temp  = left;
429                         left  = right;
430                         right = temp;
431                 }
432
433                 /* put LEA != ia32_am_O as left operand */
434                 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
435                         set_irn_n(irn, 2, right);
436                         set_irn_n(irn, 3, left);
437                         temp  = left;
438                         left  = right;
439                         right = temp;
440                 }
441
442                 /* put SHL as left operand iff left is NOT a LEA */
443                 if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
444                         set_irn_n(irn, 2, right);
445                         set_irn_n(irn, 3, left);
446                         temp  = left;
447                         left  = right;
448                         right = temp;
449                 }
450         }
451
452         base    = left;
453         index   = noreg;
454         offs    = NULL;
455         scale   = 0;
456         am_flav = 0;
457
458         /* check if operand is either const */
459         if (get_ia32_cnst(irn)) {
460                 DBG((mod, LEVEL_1, "\tfound op with imm"));
461
462                 offs_cnst = get_ia32_cnst(irn);
463                 dolea     = 1;
464         }
465
466         /* determine the operand which needs to be checked */
467         if (be_is_NoReg(babi, right)) {
468                 temp = left;
469         }
470         else {
471                 temp = right;
472         }
473
474         /* check if right operand is AMConst (LEA with ia32_am_O) */
475         if (is_ia32_Lea(temp) && get_ia32_am_flavour(temp) == ia32_am_O) {
476                 DBG((mod, LEVEL_1, "\tgot op with LEA am_O"));
477
478                 offs_lea = get_ia32_am_offs(temp);
479                 dolea    = 1;
480         }
481
482         if (isadd) {
483                 /* default for add -> make right operand to index */
484                 index = right;
485                 dolea = 1;
486
487                 DBG((mod, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
488
489                 /* determine the operand which needs to be checked */
490                 temp = left;
491                 if (is_ia32_Lea(left)) {
492                         temp = right;
493                 }
494
495                 /* check for SHL 1,2,3 */
496                 if (pred_is_specific_node(temp, is_ia32_Shl)) {
497                         temp = get_Proj_pred(temp);
498
499                         if (get_ia32_Immop_tarval(temp)) {
500                                 scale = get_tarval_long(get_ia32_Immop_tarval(temp));
501
502                                 if (scale <= 3) {
503                                         index = get_irn_n(temp, 2);
504
505                                         DBG((mod, LEVEL_1, "\tgot scaled index %+F\n", index));
506                                 }
507                         }
508                 }
509
510                 /* fix base */
511                 if (! be_is_NoReg(babi, index)) {
512                         /* if we have index, but left == right -> no base */
513                         if (left == right) {
514                                 base = noreg;
515                         }
516                         else if (! is_ia32_Lea(left) && (index != right)) {
517                                 /* index != right -> we found a good Shl           */
518                                 /* left  != LEA   -> this Shl was the left operand */
519                                 /* -> base is right operand                        */
520                                 base = right;
521                         }
522                 }
523         }
524
525         /* Try to assimilate a LEA as left operand */
526         if (is_ia32_Lea(left) && (get_ia32_am_flavour(left) != ia32_am_O)) {
527                 am_flav = get_ia32_am_flavour(left);
528
529                 /* If we have an Add with a real right operand (not NoReg) and  */
530                 /* the LEA contains already an index calculation then we create */
531                 /* a new LEA.                                                   */
532                 /* If the LEA contains already a frame_entity then we also      */
533                 /* create a new one  otherwise we would loose it.               */
534                 if ((isadd && !be_is_NoReg(babi, index) && (am_flav & ia32_am_I)) ||
535                         get_ia32_frame_ent(left))
536                 {
537                         DBG((mod, LEVEL_1, "\tleave old LEA, creating new one\n"));
538                 }
539                 else {
540                         DBG((mod, LEVEL_1, "\tgot LEA as left operand ... assimilating\n"));
541                         offs  = get_ia32_am_offs(left);
542                         base  = get_irn_n(left, 0);
543                         index = get_irn_n(left, 1);
544                         scale = get_ia32_am_scale(left);
545                 }
546         }
547
548         /* ok, we can create a new LEA */
549         if (dolea) {
550                 res = new_rd_ia32_Lea(dbg, irg, block, base, index, mode_Is);
551
552                 /* add the old offset of a previous LEA */
553                 if (offs) {
554                         add_ia32_am_offs(res, offs);
555                 }
556
557                 /* add the new offset */
558                 if (isadd) {
559                         if (offs_cnst) {
560                                 add_ia32_am_offs(res, offs_cnst);
561                         }
562                         if (offs_lea) {
563                                 add_ia32_am_offs(res, offs_lea);
564                         }
565                 }
566                 else {
567                         /* either lea_O-cnst, -cnst or -lea_O  */
568                         if (offs_cnst) {
569                                 if (offs_lea) {
570                                         add_ia32_am_offs(res, offs_lea);
571                                 }
572
573                                 sub_ia32_am_offs(res, offs_cnst);
574                         }
575                         else {
576                                 sub_ia32_am_offs(res, offs_lea);
577                         }
578                 }
579
580                 /* copy the frame entity (could be set in case of Add */
581                 /* which was a FrameAddr) */
582                 set_ia32_frame_ent(res, get_ia32_frame_ent(irn));
583
584                 if (is_ia32_use_frame(irn))
585                         set_ia32_use_frame(res);
586
587                 /* set scale */
588                 set_ia32_am_scale(res, scale);
589
590                 am_flav = ia32_am_N;
591                 /* determine new am flavour */
592                 if (offs || offs_cnst || offs_lea) {
593                         am_flav |= ia32_O;
594                 }
595                 if (! be_is_NoReg(babi, base)) {
596                         am_flav |= ia32_B;
597                 }
598                 if (! be_is_NoReg(babi, index)) {
599                         am_flav |= ia32_I;
600                 }
601                 if (scale > 0) {
602                         am_flav |= ia32_S;
603                 }
604                 set_ia32_am_flavour(res, am_flav);
605
606                 set_ia32_op_type(res, ia32_AddrModeS);
607
608                 DBG((mod, LEVEL_1, "\tLEA [%+F + %+F * %d + %s]\n", base, index, scale, get_ia32_am_offs(res)));
609
610                 /* get the result Proj of the Add/Sub */
611                 irn = get_res_proj(irn);
612
613                 assert(irn && "Couldn't find result proj");
614
615                 /* exchange the old op with the new LEA */
616                 exchange(irn, res);
617         }
618
619         return res;
620 }
621
622 /**
623  * Optimizes a pattern around irn to address mode if possible.
624  */
625 void ia32_optimize_am(ir_node *irn, void *env) {
626         ia32_code_gen_t   *cg   = env;
627         firm_dbg_module_t *mod  = cg->mod;
628         ir_node           *res  = irn;
629         be_abi_irg_t      *babi = cg->birg->abi;
630         dbg_info          *dbg;
631         ir_mode           *mode;
632         ir_node           *block, *noreg_gp, *noreg_fp;
633         ir_node           *left, *right, *temp;
634         ir_node           *store, *load, *mem_proj;
635         ir_node           *succ, *addr_b, *addr_i;
636         int                check_am_src = 0;
637
638         if (! is_ia32_irn(irn))
639                 return;
640
641         dbg      = get_irn_dbg_info(irn);
642         mode     = get_irn_mode(irn);
643         block    = get_nodes_block(irn);
644         noreg_gp = ia32_new_NoReg_gp(cg);
645         noreg_fp = ia32_new_NoReg_fp(cg);
646
647         DBG((mod, LEVEL_1, "checking for AM\n"));
648
649         /* 1st part: check for address calculations and transform the into Lea */
650
651         /* Following cases can occur:                                  */
652         /* - Sub (l, imm) -> LEA [base - offset]                       */
653         /* - Sub (l, r == LEA with ia32_am_O)   -> LEA [base - offset] */
654         /* - Add (l, imm) -> LEA [base + offset]                       */
655         /* - Add (l, r == LEA with ia32_am_O)  -> LEA [base + offset]  */
656         /* - Add (l == LEA with ia32_am_O, r)  -> LEA [base + offset]  */
657         /* - Add (l, r) -> LEA [base + index * scale]                  */
658         /*              with scale > 1 iff l/r == shl (1,2,3)          */
659
660         if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
661                 left  = get_irn_n(irn, 2);
662                 right = get_irn_n(irn, 3);
663
664             /* Do not try to create a LEA if one of the operands is a Load. */
665                 /* check is irn is a candidate for address calculation */
666                 if (is_candidate(block, irn, 1)) {
667                         DBG((mod, LEVEL_1, "\tfound address calculation candidate %+F ... ", irn));
668                         res = fold_addr(babi, irn, mod, noreg_gp);
669
670                         if (res == irn)
671                                 DB((mod, LEVEL_1, "transformed into %+F\n", res));
672                         else
673                                 DB((mod, LEVEL_1, "not transformed\n"));
674                 }
675         }
676
677         /* 2nd part: fold following patterns:                                               */
678         /* - Load  -> LEA into Load  } TODO: If the LEA is used by more than one Load/Store */
679         /* - Store -> LEA into Store }       it might be better to keep the LEA             */
680         /* - op -> Load into AMop with am_Source                                            */
681         /*   conditions:                                                                    */
682         /*     - op is am_Source capable AND                                                */
683         /*     - the Load is only used by this op AND                                       */
684         /*     - the Load is in the same block                                              */
685         /* - Store -> op -> Load  into AMop with am_Dest                                    */
686         /*   conditions:                                                                    */
687         /*     - op is am_Dest capable AND                                                  */
688         /*     - the Store uses the same address as the Load AND                            */
689         /*     - the Load is only used by this op AND                                       */
690         /*     - the Load and Store are in the same block AND                               */
691         /*     - nobody else uses the result of the op                                      */
692
693         if ((res == irn) && (get_ia32_am_support(irn) != ia32_am_None) && !is_ia32_Lea(irn)) {
694                 /* 1st: check for Load/Store -> LEA   */
695                 if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn)) {
696                         left = get_irn_n(irn, 0);
697
698                         if (is_ia32_Lea(left)) {
699                                 DBG((mod, LEVEL_1, "\nmerging %+F into %+F\n", left, irn));
700
701                                 /* get the AM attributes from the LEA */
702                                 add_ia32_am_offs(irn, get_ia32_am_offs(left));
703                                 set_ia32_am_scale(irn, get_ia32_am_scale(left));
704                                 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
705
706                                 set_ia32_op_type(irn, is_ia32_Ld(irn) ? ia32_AddrModeS : ia32_AddrModeD);
707
708                                 /* set base and index */
709                                 set_irn_n(irn, 0, get_irn_n(left, 0));
710                                 set_irn_n(irn, 1, get_irn_n(left, 1));
711                         }
712                 }
713                 /* check if the node is an address mode candidate */
714                 else if (is_candidate(block, irn, 0)) {
715                         DBG((mod, LEVEL_1, "\tfound address mode candidate %+F ... ", irn));
716
717                         left  = get_irn_n(irn, 2);
718                         if (get_irn_arity(irn) == 4) {
719                                 /* it's an "unary" operation */
720                                 right = left;
721                         }
722                         else {
723                                 right = get_irn_n(irn, 3);
724                         }
725
726                         /* normalize commutative ops */
727                         if (node_is_comm(irn)) {
728                                 /* Assure that right operand is always a Load if there is one    */
729                                 /* because non-commutative ops can only use Dest AM if the right */
730                                 /* operand is a load, so we only need to check right operand.    */
731                                 if (pred_is_specific_nodeblock(block, left, is_ia32_Ld))
732                                 {
733                                         set_irn_n(irn, 2, right);
734                                         set_irn_n(irn, 3, left);
735
736                                         temp  = left;
737                                         left  = right;
738                                         right = temp;
739                                 }
740                         }
741
742                         /* check for Store -> op -> Load */
743
744                         /* Store -> op -> Load optimization is only possible if supported by op */
745                         /* and if right operand is a Load                                       */
746                         if ((get_ia32_am_support(irn) & ia32_am_Dest) &&
747                                  pred_is_specific_nodeblock(block, right, is_ia32_Ld))
748                         {
749
750                                 /* An address mode capable op always has a result Proj.                  */
751                                 /* If this Proj is used by more than one other node, we don't need to    */
752                                 /* check further, otherwise we check for Store and remember the address, */
753                                 /* the Store points to. */
754
755                                 succ = get_res_proj(irn);
756                                 assert(succ && "Couldn't find result proj");
757
758                                 addr_b = NULL;
759                                 addr_i = NULL;
760                                 store  = NULL;
761
762                                 /* now check for users and Store */
763                                 if (ia32_get_irn_n_edges(succ) == 1) {
764                                         succ = get_edge_src_irn(get_irn_out_edge_first(succ));
765
766                                         if (is_ia32_fStore(succ) || is_ia32_Store(succ)) {
767                                                 store  = succ;
768                                                 addr_b = get_irn_n(store, 0);
769
770                                                 /* Could be that the Store is connected to the address    */
771                                                 /* calculating LEA while the Load is already transformed. */
772                                                 if (is_ia32_Lea(addr_b)) {
773                                                         succ   = addr_b;
774                                                         addr_b = get_irn_n(succ, 0);
775                                                         addr_i = get_irn_n(succ, 1);
776                                                 }
777                                                 else {
778                                                         addr_i = noreg_gp;
779                                                 }
780                                         }
781                                 }
782
783                                 if (store) {
784                                         /* we found a Store as single user: Now check for Load */
785
786                                         /* Extra check for commutative ops with two Loads */
787                                         /* -> put the interesting Load right              */
788                                         if (node_is_comm(irn) &&
789                                                 pred_is_specific_nodeblock(block, left, is_ia32_Ld))
790                                         {
791                                                 if ((addr_b == get_irn_n(get_Proj_pred(left), 0)) &&
792                                                         (addr_i == get_irn_n(get_Proj_pred(left), 1)))
793                                                 {
794                                                         /* We exchange left and right, so it's easier to kill     */
795                                                         /* the correct Load later and to handle unary operations. */
796                                                         set_irn_n(irn, 2, right);
797                                                         set_irn_n(irn, 3, left);
798
799                                                         temp  = left;
800                                                         left  = right;
801                                                         right = temp;
802                                                 }
803                                         }
804
805                                         /* skip the Proj for easier access */
806                                         load = get_Proj_pred(right);
807
808                                         /* Compare Load and Store address */
809                                         if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
810                                                 /* Right Load is from same address, so we can */
811                                                 /* disconnect the Load and Store here        */
812
813                                                 /* set new base, index and attributes */
814                                                 set_irn_n(irn, 0, addr_b);
815                                                 set_irn_n(irn, 1, addr_i);
816                                                 add_ia32_am_offs(irn, get_ia32_am_offs(load));
817                                                 set_ia32_am_scale(irn, get_ia32_am_scale(load));
818                                                 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
819                                                 set_ia32_op_type(irn, ia32_AddrModeD);
820                                                 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
821                                                 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
822
823                                                 if (is_ia32_use_frame(load))
824                                                         set_ia32_use_frame(irn);
825
826                                                 /* connect to Load memory and disconnect Load */
827                                                 if (get_irn_arity(irn) == 5) {
828                                                         /* binary AMop */
829                                                         set_irn_n(irn, 4, get_irn_n(load, 2));
830                                                         set_irn_n(irn, 3, noreg_gp);
831                                                 }
832                                                 else {
833                                                         /* unary AMop */
834                                                         set_irn_n(irn, 3, get_irn_n(load, 2));
835                                                         set_irn_n(irn, 2, noreg_gp);
836                                                 }
837
838                                                 /* connect the memory Proj of the Store to the op */
839                                                 mem_proj = get_mem_proj(store);
840                                                 set_Proj_pred(mem_proj, irn);
841                                                 set_Proj_proj(mem_proj, 1);
842
843                                                 DB((mod, LEVEL_1, "merged with %+F and %+F into dest AM\n", load, store));
844                                         }
845                                 } /* if (store) */
846                                 else if (get_ia32_am_support(irn) & ia32_am_Source) {
847                                         /* There was no store, check if we still can optimize for source address mode */
848                                         check_am_src = 1;
849                                 }
850                         } /* if (support AM Dest) */
851                         else if (get_ia32_am_support(irn) & ia32_am_Source) {
852                                 /* op doesn't support am AM Dest -> check for AM Source */
853                                 check_am_src = 1;
854                         }
855
856                         /* normalize commutative ops */
857                         if (node_is_comm(irn)) {
858                                 /* Assure that left operand is always a Load if there is one */
859                                 /* because non-commutative ops can only use Source AM if the */
860                                 /* left operand is a Load, so we only need to check the left */
861                                 /* operand afterwards.                                       */
862                                 if (pred_is_specific_nodeblock(block, right, is_ia32_Ld))       {
863                                         set_irn_n(irn, 2, right);
864                                         set_irn_n(irn, 3, left);
865
866                                         temp  = left;
867                                         left  = right;
868                                         right = temp;
869                                 }
870                         }
871
872                         /* optimize op -> Load iff Load is only used by this op   */
873                         /* and left operand is a Load which only used by this irn */
874                         if (check_am_src                                        &&
875                                 pred_is_specific_nodeblock(block, left, is_ia32_Ld) &&
876                                 (ia32_get_irn_n_edges(left) == 1))
877                         {
878                                 left = get_Proj_pred(left);
879
880                                 addr_b = get_irn_n(left, 0);
881                                 addr_i = get_irn_n(left, 1);
882
883                                 /* set new base, index and attributes */
884                                 set_irn_n(irn, 0, addr_b);
885                                 set_irn_n(irn, 1, addr_i);
886                                 add_ia32_am_offs(irn, get_ia32_am_offs(left));
887                                 set_ia32_am_scale(irn, get_ia32_am_scale(left));
888                                 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
889                                 set_ia32_op_type(irn, ia32_AddrModeS);
890                                 set_ia32_frame_ent(irn, get_ia32_frame_ent(left));
891                                 set_ia32_ls_mode(irn, get_ia32_ls_mode(left));
892
893                                 if (is_ia32_use_frame(left))
894                                         set_ia32_use_frame(irn);
895
896                                 /* connect to Load memory */
897                                 if (get_irn_arity(irn) == 5) {
898                                         /* binary AMop */
899                                         set_irn_n(irn, 4, get_irn_n(left, 2));
900                                 }
901                                 else {
902                                         /* unary AMop */
903                                         set_irn_n(irn, 3, get_irn_n(left, 2));
904                                 }
905
906                                 /* disconnect from Load */
907                                 set_irn_n(irn, 2, noreg_gp);
908
909                                 /* If Load has a memory Proj, connect it to the op */
910                                 mem_proj = get_mem_proj(left);
911                                 if (mem_proj) {
912                                         set_Proj_pred(mem_proj, irn);
913                                         set_Proj_proj(mem_proj, 1);
914                                 }
915
916                                 DB((mod, LEVEL_1, "merged with %+F into source AM\n", left));
917                         }
918                 }
919         }
920 }