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