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