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