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