Fixed Push with immediate creation
[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                 FP_USED(env->cg);
88                 if (USE_SSE2(env->cg))
89                         cnst = new_rd_ia32_xConst(dbg, irg, block, get_irg_no_mem(irg), mode);
90                 else
91                         cnst = new_rd_ia32_vfConst(dbg, irg, block, get_irg_no_mem(irg), mode);
92         }
93         else
94                 cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), mode);
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_irg_no_mem(irg), 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 & IA32_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                 DBG_OPT_CJMP(irn);
399
400                 set_irn_op(irn, op_ia32_CJmpAM);
401
402                 DB((cg->mod, LEVEL_1, "%+F\n", irn));
403         }
404 }
405
406 /**
407  * Creates a Push from Store(IncSP(gp_reg_size))
408  */
409 static void ia32_create_Push(ir_node *irn, ia32_code_gen_t *cg) {
410         ir_node *sp  = get_irn_n(irn, 0);
411         ir_node *val, *next, *push, *bl, *proj_M, *proj_res, *old_proj_M;
412         const ir_edge_t *edge;
413
414         if (get_ia32_am_offs(irn) || !be_is_IncSP(sp))
415                 return;
416
417         if (arch_get_irn_register(cg->arch_env, get_irn_n(irn, 1)) !=
418                 &ia32_gp_regs[REG_GP_NOREG])
419                 return;
420
421         val = get_irn_n(irn, 2);
422         if (mode_is_float(get_irn_mode(val)))
423                 return;
424
425         if (be_get_IncSP_direction(sp) != be_stack_dir_expand ||
426                 be_get_IncSP_offset(sp) != get_mode_size_bytes(ia32_reg_classes[CLASS_ia32_gp].mode))
427                 return;
428
429         /* ok, translate into Push */
430         edge = get_irn_out_edge_first(irn);
431         old_proj_M = get_edge_src_irn(edge);
432
433         next = sched_next(irn);
434         sched_remove(irn);
435         sched_remove(sp);
436
437         bl   = get_nodes_block(irn);
438         push = new_rd_ia32_Push(NULL, current_ir_graph, bl,
439                 be_get_IncSP_pred(sp), val, be_get_IncSP_mem(sp));
440         proj_res = new_r_Proj(current_ir_graph, bl, push, get_irn_mode(sp), pn_ia32_Push_stack);
441         proj_M   = new_r_Proj(current_ir_graph, bl, push, mode_M, pn_ia32_Push_M);
442
443         /* copy a possible constant from the store */
444         set_ia32_id_cnst(push, get_ia32_id_cnst(irn));
445         set_ia32_immop_type(push, get_ia32_immop_type(irn));
446
447         /* the push must have SP out register */
448         arch_set_irn_register(cg->arch_env, push, arch_get_irn_register(cg->arch_env, sp));
449
450         exchange(old_proj_M, proj_M);
451         exchange(sp, proj_res);
452         sched_add_before(next, push);
453         sched_add_after(push, proj_res);
454 }
455
456 /**
457  * Creates a Pop from IncSP(Load(sp))
458  */
459 static void ia32_create_Pop(ir_node *irn, ia32_code_gen_t *cg) {
460         ir_node *old_proj_M = be_get_IncSP_mem(irn);
461         ir_node *load = skip_Proj(old_proj_M);
462         ir_node *old_proj_res = NULL;
463         ir_node *bl, *pop, *next, *proj_res, *proj_sp, *proj_M;
464         const ir_edge_t *edge;
465         const arch_register_t *reg, *sp;
466
467         if (! is_ia32_Load(load) || get_ia32_am_offs(load))
468                 return;
469
470         if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 1)) !=
471                 &ia32_gp_regs[REG_GP_NOREG])
472                 return;
473         if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 0)) != cg->isa->arch_isa.sp)
474                 return;
475
476         /* ok, translate into pop */
477         foreach_out_edge(load, edge) {
478                 ir_node *succ = get_edge_src_irn(edge);
479                 if (succ != old_proj_M) {
480                         old_proj_res = succ;
481                         break;
482                 }
483         }
484         if (! old_proj_res) {
485                 assert(0);
486                 return; /* should not happen */
487         }
488
489         bl = get_nodes_block(load);
490
491         /* IncSP is typically scheduled after the load, so remove it first */
492         sched_remove(irn);
493         next = sched_next(old_proj_res);
494         sched_remove(old_proj_res);
495         sched_remove(load);
496
497         reg = arch_get_irn_register(cg->arch_env, load);
498         sp  = arch_get_irn_register(cg->arch_env, irn);
499
500         pop      = new_rd_ia32_Pop(NULL, current_ir_graph, bl, get_irn_n(irn, 0), get_irn_n(load, 2));
501         proj_res = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(old_proj_res), pn_ia32_Pop_res);
502         proj_sp  = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(irn), pn_ia32_Pop_stack);
503         proj_M   = new_r_Proj(current_ir_graph, bl, pop, mode_M, pn_ia32_Pop_M);
504
505         exchange(old_proj_M, proj_M);
506         exchange(old_proj_res, proj_res);
507         exchange(irn, proj_sp);
508
509         arch_set_irn_register(cg->arch_env, proj_res, reg);
510         arch_set_irn_register(cg->arch_env, proj_sp, sp);
511
512         sched_add_before(next, proj_sp);
513         sched_add_before(proj_sp, proj_res);
514         sched_add_before(proj_res,pop);
515 }
516
517 /**
518  * Tries to optimize two following IncSP.
519  */
520 static void ia32_optimize_IncSP(ir_node *irn, ia32_code_gen_t *cg) {
521         ir_node *prev = be_get_IncSP_pred(irn);
522         int real_uses = get_irn_n_edges(prev);
523
524         if (be_is_IncSP(prev) && real_uses == 1) {
525                 /* first IncSP has only one IncSP user, kill the first one */
526                 unsigned       prev_offs = be_get_IncSP_offset(prev);
527                 be_stack_dir_t prev_dir  = be_get_IncSP_direction(prev);
528                 unsigned       curr_offs = be_get_IncSP_offset(irn);
529                 be_stack_dir_t curr_dir  = be_get_IncSP_direction(irn);
530
531                 int new_ofs = prev_offs * (prev_dir == be_stack_dir_expand ? -1 : +1) +
532                                     curr_offs * (curr_dir == be_stack_dir_expand ? -1 : +1);
533
534                 if (new_ofs < 0) {
535                         new_ofs  = -new_ofs;
536                         curr_dir = be_stack_dir_expand;
537                 }
538                 else
539                         curr_dir = be_stack_dir_shrink;
540                 be_set_IncSP_offset(prev, 0);
541                 be_set_IncSP_offset(irn, (unsigned)new_ofs);
542                 be_set_IncSP_direction(irn, curr_dir);
543
544                 /* Omit the optimized IncSP */
545                 be_set_IncSP_pred(irn, be_get_IncSP_pred(prev));
546         }
547 }
548
549 /**
550  * Performs Peephole Optimizations.
551  */
552 void ia32_peephole_optimization(ir_node *irn, void *env) {
553         ia32_code_gen_t *cg = env;
554
555         if (is_ia32_TestJmp(irn))
556                 ia32_optimize_TestJmp(irn, cg);
557         else if (is_ia32_CondJmp(irn))
558                 ia32_optimize_CondJmp(irn, cg);
559         else if (be_is_IncSP(irn))
560                 ia32_optimize_IncSP(irn, cg);
561         else if (is_ia32_Store(irn))
562                 ia32_create_Push(irn, cg);
563 }
564
565
566
567 /******************************************************************
568  *              _     _                   __  __           _
569  *     /\      | |   | |                 |  \/  |         | |
570  *    /  \   __| | __| |_ __ ___  ___ ___| \  / | ___   __| | ___
571  *   / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
572  *  / ____ \ (_| | (_| | | |  __/\__ \__ \ |  | | (_) | (_| |  __/
573  * /_/    \_\__,_|\__,_|_|  \___||___/___/_|  |_|\___/ \__,_|\___|
574  *
575  ******************************************************************/
576
577 static int node_is_ia32_comm(const ir_node *irn) {
578         return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
579 }
580
581 static int ia32_get_irn_n_edges(const ir_node *irn) {
582         const ir_edge_t *edge;
583         int cnt = 0;
584
585         foreach_out_edge(irn, edge) {
586                 cnt++;
587         }
588
589         return cnt;
590 }
591
592 /**
593  * Returns the first mode_M Proj connected to irn.
594  */
595 static ir_node *get_mem_proj(const ir_node *irn) {
596         const ir_edge_t *edge;
597         ir_node         *src;
598
599         assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
600
601         foreach_out_edge(irn, edge) {
602                 src = get_edge_src_irn(edge);
603
604                 assert(is_Proj(src) && "Proj expected");
605
606                 if (get_irn_mode(src) == mode_M)
607                         return src;
608         }
609
610         return NULL;
611 }
612
613 /**
614  * Returns the first Proj with mode != mode_M connected to irn.
615  */
616 static ir_node *get_res_proj(const ir_node *irn) {
617         const ir_edge_t *edge;
618         ir_node         *src;
619
620         assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
621
622         foreach_out_edge(irn, edge) {
623                 src = get_edge_src_irn(edge);
624
625                 assert(is_Proj(src) && "Proj expected");
626
627                 if (get_irn_mode(src) != mode_M)
628                         return src;
629         }
630
631         return NULL;
632 }
633
634 /**
635  * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
636  *
637  * @param pred       The node to be checked
638  * @param is_op_func The check-function
639  * @return 1 if conditions are fulfilled, 0 otherwise
640  */
641 static int pred_is_specific_node(const ir_node *pred, is_op_func_t *is_op_func) {
642         if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
643                 return 1;
644         }
645
646         return 0;
647 }
648
649 /**
650  * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
651  * and if the predecessor is in block bl.
652  *
653  * @param bl         The block
654  * @param pred       The node to be checked
655  * @param is_op_func The check-function
656  * @return 1 if conditions are fulfilled, 0 otherwise
657  */
658 static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
659         int (*is_op_func)(const ir_node *n))
660 {
661         if (is_Proj(pred)) {
662                 pred = get_Proj_pred(pred);
663                 if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
664                         return 1;
665                 }
666         }
667
668         return 0;
669 }
670
671
672
673 /**
674  * Checks if irn is a candidate for address calculation or address mode.
675  *
676  * address calculation (AC):
677  * - none of the operand must be a Load  within the same block OR
678  * - all Loads must have more than one user                    OR
679  * - the irn has a frame entity (it's a former FrameAddr)
680  *
681  * address mode (AM):
682  * - at least one operand has to be a Load within the same block AND
683  * - the load must not have other users than the irn             AND
684  * - the irn must not have a frame entity set
685  *
686  * @param block       The block the Loads must/not be in
687  * @param irn         The irn to check
688  * @param check_addr  1 if to check for address calculation, 0 otherwise
689  * return 1 if irn is a candidate for AC or AM, 0 otherwise
690  */
691 static int is_candidate(const ir_node *block, const ir_node *irn, int check_addr) {
692         ir_node *in;
693         int      n, is_cand = check_addr;
694
695         in = get_irn_n(irn, 2);
696
697         if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
698                 n         = ia32_get_irn_n_edges(in);
699                 is_cand   = check_addr ? (n == 1 ? 0 : is_cand) : (n == 1 ? 1 : is_cand);
700         }
701
702         in = get_irn_n(irn, 3);
703
704         if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
705                 n         = ia32_get_irn_n_edges(in);
706                 is_cand   = check_addr ? (n == 1 ? 0 : is_cand) : (n == 1 ? 1 : is_cand);
707         }
708
709         is_cand = get_ia32_frame_ent(irn) ? (check_addr ? 1 : 0) : is_cand;
710
711         return is_cand;
712 }
713
714 /**
715  * Compares the base and index addr and the load/store entities
716  * and returns 1 if they are equal.
717  */
718 static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
719                                                                         const ir_node *addr_b, const ir_node *addr_i)
720 {
721         int     is_equal = (addr_b == get_irn_n(load, 0)) && (addr_i == get_irn_n(load, 1));
722         entity *lent     = get_ia32_frame_ent(load);
723         entity *sent     = get_ia32_frame_ent(store);
724         ident  *lid      = get_ia32_am_sc(load);
725         ident  *sid      = get_ia32_am_sc(store);
726         char   *loffs    = get_ia32_am_offs(load);
727         char   *soffs    = get_ia32_am_offs(store);
728
729         /* are both entities set and equal? */
730         if (is_equal && (lent || sent))
731                 is_equal = lent && sent && (lent == sent);
732
733         /* are address mode idents set and equal? */
734         if (is_equal && (lid || sid))
735                 is_equal = lid && sid && (lid == sid);
736
737         /* are offsets set and equal */
738         if (is_equal && (loffs || soffs))
739                 is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;
740
741         /* are the load and the store of the same mode? */
742         is_equal = is_equal ? get_ia32_ls_mode(load) == get_ia32_ls_mode(store) : 0;
743
744         return is_equal;
745 }
746
747 typedef enum _ia32_take_lea_attr {
748         IA32_LEA_ATTR_NONE  = 0,
749         IA32_LEA_ATTR_BASE  = (1 << 0),
750         IA32_LEA_ATTR_INDEX = (1 << 1),
751         IA32_LEA_ATTR_OFFS  = (1 << 2),
752         IA32_LEA_ATTR_SCALE = (1 << 3),
753         IA32_LEA_ATTR_AMSC  = (1 << 4),
754         IA32_LEA_ATTR_FENT  = (1 << 5)
755 } ia32_take_lea_attr;
756
757 /**
758  * Decides if we have to keep the LEA operand or if we can assimilate it.
759  */
760 static int do_new_lea(ir_node *irn, ir_node *base, ir_node *index, ir_node *lea,
761                 int have_am_sc, ia32_code_gen_t *cg)
762 {
763         ir_node *lea_base = get_irn_n(lea, 0);
764         ir_node *lea_idx  = get_irn_n(lea, 1);
765         entity  *irn_ent  = get_ia32_frame_ent(irn);
766         entity  *lea_ent  = get_ia32_frame_ent(lea);
767         int      ret_val  = 0;
768         int      is_noreg_base  = be_is_NoReg(cg, base);
769         int      is_noreg_index = be_is_NoReg(cg, index);
770         ia32_am_flavour_t am_flav = get_ia32_am_flavour(lea);
771
772         /* If the Add and the LEA both have a different frame entity set: keep */
773         if (irn_ent && lea_ent && (irn_ent != lea_ent))
774                 return IA32_LEA_ATTR_NONE;
775         else if (! irn_ent && lea_ent)
776                 ret_val |= IA32_LEA_ATTR_FENT;
777
778         /* If the Add and the LEA both have already an address mode symconst: keep */
779         if (have_am_sc && get_ia32_am_sc(lea))
780                 return IA32_LEA_ATTR_NONE;
781         else if (get_ia32_am_sc(lea))
782                 ret_val |= IA32_LEA_ATTR_AMSC;
783
784         /* Check the different base-index combinations */
785
786         if (! is_noreg_base && ! is_noreg_index) {
787                 /* Assimilate if base is the lea and the LEA is just a Base + Offset calculation */
788                 if ((base == lea) && ! (am_flav & ia32_I ? 1 : 0)) {
789                         if (am_flav & ia32_O)
790                                 ret_val |= IA32_LEA_ATTR_OFFS;
791
792                         ret_val |= IA32_LEA_ATTR_BASE;
793                 }
794                 else
795                         return IA32_LEA_ATTR_NONE;
796         }
797         else if (! is_noreg_base && is_noreg_index) {
798                 /* Base is set but index not */
799                 if (base == lea) {
800                         /* Base points to LEA: assimilate everything */
801                         if (am_flav & ia32_O)
802                                 ret_val |= IA32_LEA_ATTR_OFFS;
803                         if (am_flav & ia32_S)
804                                 ret_val |= IA32_LEA_ATTR_SCALE;
805                         if (am_flav & ia32_I)
806                                 ret_val |= IA32_LEA_ATTR_INDEX;
807
808                         ret_val |= IA32_LEA_ATTR_BASE;
809                 }
810                 else if (am_flav & ia32_B ? 0 : 1) {
811                         /* Base is not the LEA but the LEA is an index only calculation: assimilate */
812                         if (am_flav & ia32_O)
813                                 ret_val |= IA32_LEA_ATTR_OFFS;
814                         if (am_flav & ia32_S)
815                                 ret_val |= IA32_LEA_ATTR_SCALE;
816
817                         ret_val |= IA32_LEA_ATTR_INDEX;
818                 }
819                 else
820                         return IA32_LEA_ATTR_NONE;
821         }
822         else if (is_noreg_base && ! is_noreg_index) {
823                 /* Index is set but not base */
824                 if (index == lea) {
825                         /* Index points to LEA: assimilate everything */
826                         if (am_flav & ia32_O)
827                                 ret_val |= IA32_LEA_ATTR_OFFS;
828                         if (am_flav & ia32_S)
829                                 ret_val |= IA32_LEA_ATTR_SCALE;
830                         if (am_flav & ia32_B)
831                                 ret_val |= IA32_LEA_ATTR_BASE;
832
833                         ret_val |= IA32_LEA_ATTR_INDEX;
834                 }
835                 else if (am_flav & ia32_I ? 0 : 1) {
836                         /* Index is not the LEA but the LEA is a base only calculation: assimilate */
837                         if (am_flav & ia32_O)
838                                 ret_val |= IA32_LEA_ATTR_OFFS;
839                         if (am_flav & ia32_S)
840                                 ret_val |= IA32_LEA_ATTR_SCALE;
841
842                         ret_val |= IA32_LEA_ATTR_BASE;
843                 }
844                 else
845                         return IA32_LEA_ATTR_NONE;
846         }
847         else {
848                 assert(0 && "There must have been set base or index");
849         }
850
851         return ret_val;
852 }
853
854
855 /**
856  * Folds Add or Sub to LEA if possible
857  */
858 static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn, ir_node *noreg) {
859         ir_graph   *irg        = get_irn_irg(irn);
860         dbg_info   *dbg        = get_irn_dbg_info(irn);
861         ir_node    *block      = get_nodes_block(irn);
862         ir_node    *res        = irn;
863         ir_node    *shift      = NULL;
864         ir_node    *lea_o      = NULL;
865         ir_node    *lea        = NULL;
866         char       *offs       = NULL;
867         const char *offs_cnst  = NULL;
868         char       *offs_lea   = NULL;
869         int         scale      = 0;
870         int         isadd      = 0;
871         int         dolea      = 0;
872         int         have_am_sc = 0;
873         int         am_sc_sign = 0;
874         ident      *am_sc      = NULL;
875         entity     *lea_ent    = NULL;
876         ir_node    *left, *right, *temp;
877         ir_node    *base, *index;
878         ia32_am_flavour_t am_flav;
879         DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
880
881         if (is_ia32_Add(irn))
882                 isadd = 1;
883
884         left  = get_irn_n(irn, 2);
885         right = get_irn_n(irn, 3);
886
887         /* "normalize" arguments in case of add with two operands */
888         if  (isadd && ! be_is_NoReg(cg, right)) {
889                 /* put LEA == ia32_am_O as right operand */
890                 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
891                         set_irn_n(irn, 2, right);
892                         set_irn_n(irn, 3, left);
893                         temp  = left;
894                         left  = right;
895                         right = temp;
896                 }
897
898                 /* put LEA != ia32_am_O as left operand */
899                 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
900                         set_irn_n(irn, 2, right);
901                         set_irn_n(irn, 3, left);
902                         temp  = left;
903                         left  = right;
904                         right = temp;
905                 }
906
907                 /* put SHL as left operand iff left is NOT a LEA */
908                 if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
909                         set_irn_n(irn, 2, right);
910                         set_irn_n(irn, 3, left);
911                         temp  = left;
912                         left  = right;
913                         right = temp;
914                 }
915         }
916
917         base    = left;
918         index   = noreg;
919         offs    = NULL;
920         scale   = 0;
921         am_flav = 0;
922
923         /* check for operation with immediate */
924         if (is_ia32_ImmConst(irn)) {
925                 DBG((mod, LEVEL_1, "\tfound op with imm const"));
926
927                 offs_cnst = get_ia32_cnst(irn);
928                 dolea     = 1;
929         }
930         else if (is_ia32_ImmSymConst(irn)) {
931                 DBG((mod, LEVEL_1, "\tfound op with imm symconst"));
932
933                 have_am_sc = 1;
934                 dolea      = 1;
935                 am_sc      = get_ia32_id_cnst(irn);
936                 am_sc_sign = is_ia32_am_sc_sign(irn);
937         }
938
939         /* determine the operand which needs to be checked */
940         if (be_is_NoReg(cg, right)) {
941                 temp = left;
942         }
943         else {
944                 temp = right;
945         }
946
947         /* check if right operand is AMConst (LEA with ia32_am_O)  */
948         /* but we can only eat it up if there is no other symconst */
949         /* because the linker won't accept two symconsts           */
950         if (! have_am_sc && is_ia32_Lea(temp) && get_ia32_am_flavour(temp) == ia32_am_O) {
951                 DBG((mod, LEVEL_1, "\tgot op with LEA am_O"));
952
953                 offs_lea   = get_ia32_am_offs(temp);
954                 am_sc      = get_ia32_am_sc(temp);
955                 am_sc_sign = is_ia32_am_sc_sign(temp);
956                 have_am_sc = 1;
957                 dolea      = 1;
958                 lea_o      = temp;
959         }
960
961         if (isadd) {
962                 /* default for add -> make right operand to index */
963                 index = right;
964                 dolea = 1;
965
966                 DBG((mod, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
967
968                 /* determine the operand which needs to be checked */
969                 temp = left;
970                 if (is_ia32_Lea(left)) {
971                         temp = right;
972                 }
973
974                 /* check for SHL 1,2,3 */
975                 if (pred_is_specific_node(temp, is_ia32_Shl)) {
976                         temp  = get_Proj_pred(temp);
977                         shift = temp;
978
979                         if (get_ia32_Immop_tarval(temp)) {
980                                 scale = get_tarval_long(get_ia32_Immop_tarval(temp));
981
982                                 if (scale <= 3) {
983                                         index = get_irn_n(temp, 2);
984
985                                         DBG((mod, LEVEL_1, "\tgot scaled index %+F\n", index));
986                                 }
987                                 else {
988                                         scale = 0;
989                                         shift = NULL;
990                                 }
991                         }
992                 }
993
994                 /* fix base */
995                 if (! be_is_NoReg(cg, index)) {
996                         /* if we have index, but left == right -> no base */
997                         if (left == right) {
998                                 base = noreg;
999                         }
1000                         else if (! is_ia32_Lea(left) && (index != right)) {
1001                                 /* index != right -> we found a good Shl           */
1002                                 /* left  != LEA   -> this Shl was the left operand */
1003                                 /* -> base is right operand                        */
1004                                 base = right;
1005                         }
1006                 }
1007         }
1008
1009         /* Try to assimilate a LEA as left operand */
1010         if (is_ia32_Lea(left) && (get_ia32_am_flavour(left) != ia32_am_O)) {
1011                 /* check if we can assimilate the LEA */
1012                 int take_attr = do_new_lea(irn, base, index, left, have_am_sc, cg);
1013
1014                 if (take_attr == IA32_LEA_ATTR_NONE) {
1015                         DBG((mod, LEVEL_1, "\tleave old LEA, creating new one\n"));
1016                 }
1017                 else {
1018                         DBG((mod, LEVEL_1, "\tgot LEA as left operand ... assimilating\n"));
1019                         lea = left; /* for statistics */
1020
1021                         if (take_attr & IA32_LEA_ATTR_OFFS)
1022                                 offs = get_ia32_am_offs(left);
1023
1024                         if (take_attr & IA32_LEA_ATTR_AMSC) {
1025                                 am_sc      = get_ia32_am_sc(left);
1026                                 have_am_sc = 1;
1027                                 am_sc_sign = is_ia32_am_sc_sign(left);
1028                         }
1029
1030                         if (take_attr & IA32_LEA_ATTR_SCALE)
1031                                 scale = get_ia32_am_scale(left);
1032
1033                         if (take_attr & IA32_LEA_ATTR_BASE)
1034                                 base = get_irn_n(left, 0);
1035
1036                         if (take_attr & IA32_LEA_ATTR_INDEX)
1037                                 index = get_irn_n(left, 1);
1038
1039                         if (take_attr & IA32_LEA_ATTR_FENT)
1040                                 lea_ent = get_ia32_frame_ent(left);
1041                 }
1042         }
1043
1044         /* ok, we can create a new LEA */
1045         if (dolea) {
1046                 res = new_rd_ia32_Lea(dbg, irg, block, base, index, mode_Is);
1047
1048                 /* add the old offset of a previous LEA */
1049                 if (offs) {
1050                         add_ia32_am_offs(res, offs);
1051                 }
1052
1053                 /* add the new offset */
1054                 if (isadd) {
1055                         if (offs_cnst) {
1056                                 add_ia32_am_offs(res, offs_cnst);
1057                         }
1058                         if (offs_lea) {
1059                                 add_ia32_am_offs(res, offs_lea);
1060                         }
1061                 }
1062                 else {
1063                         /* either lea_O-cnst, -cnst or -lea_O  */
1064                         if (offs_cnst) {
1065                                 if (offs_lea) {
1066                                         add_ia32_am_offs(res, offs_lea);
1067                                 }
1068
1069                                 sub_ia32_am_offs(res, offs_cnst);
1070                         }
1071                         else {
1072                                 sub_ia32_am_offs(res, offs_lea);
1073                         }
1074                 }
1075
1076                 /* set the address mode symconst */
1077                 if (have_am_sc) {
1078                         set_ia32_am_sc(res, am_sc);
1079                         if (am_sc_sign)
1080                                 set_ia32_am_sc_sign(res);
1081                 }
1082
1083                 /* copy the frame entity (could be set in case of Add */
1084                 /* which was a FrameAddr) */
1085                 if (lea_ent)
1086                         set_ia32_frame_ent(res, lea_ent);
1087                 else
1088                         set_ia32_frame_ent(res, get_ia32_frame_ent(irn));
1089
1090                 if (get_ia32_frame_ent(res))
1091                         set_ia32_use_frame(res);
1092
1093                 /* set scale */
1094                 set_ia32_am_scale(res, scale);
1095
1096                 am_flav = ia32_am_N;
1097                 /* determine new am flavour */
1098                 if (offs || offs_cnst || offs_lea) {
1099                         am_flav |= ia32_O;
1100                 }
1101                 if (! be_is_NoReg(cg, base)) {
1102                         am_flav |= ia32_B;
1103                 }
1104                 if (! be_is_NoReg(cg, index)) {
1105                         am_flav |= ia32_I;
1106                 }
1107                 if (scale > 0) {
1108                         am_flav |= ia32_S;
1109                 }
1110                 set_ia32_am_flavour(res, am_flav);
1111
1112                 set_ia32_op_type(res, ia32_AddrModeS);
1113
1114                 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
1115
1116                 DBG((mod, LEVEL_1, "\tLEA [%+F + %+F * %d + %s]\n", base, index, scale, get_ia32_am_offs(res)));
1117
1118                 /* we will exchange it, report here before the Proj is created */
1119                 if (shift && lea && lea_o)
1120                         DBG_OPT_LEA4(irn, lea_o, lea, shift, res);
1121                 else if (shift && lea)
1122                         DBG_OPT_LEA3(irn, lea, shift, res);
1123                 else if (shift && lea_o)
1124                         DBG_OPT_LEA3(irn, lea_o, shift, res);
1125                 else if (lea && lea_o)
1126                         DBG_OPT_LEA3(irn, lea_o, lea, res);
1127                 else if (shift)
1128                         DBG_OPT_LEA2(irn, shift, res);
1129                 else if (lea)
1130                         DBG_OPT_LEA2(irn, lea, res);
1131                 else if (lea_o)
1132                         DBG_OPT_LEA2(irn, lea_o, res);
1133                 else
1134                         DBG_OPT_LEA1(irn, res);
1135
1136                 /* get the result Proj of the Add/Sub */
1137                 irn = get_res_proj(irn);
1138
1139                 assert(irn && "Couldn't find result proj");
1140
1141                 /* exchange the old op with the new LEA */
1142                 exchange(irn, res);
1143         }
1144
1145         return res;
1146 }
1147
1148
1149 /**
1150  * Merges a Load/Store node with a LEA.
1151  * @param irn The Load/Store node
1152  * @param lea The LEA
1153  */
1154 static void merge_loadstore_lea(ir_node *irn, ir_node *lea) {
1155         entity *irn_ent = get_ia32_frame_ent(irn);
1156         entity *lea_ent = get_ia32_frame_ent(lea);
1157
1158         /* If the irn and the LEA both have a different frame entity set: do not merge */
1159         if (irn_ent && lea_ent && (irn_ent != lea_ent))
1160                 return;
1161         else if (! irn_ent && lea_ent) {
1162                 set_ia32_frame_ent(irn, lea_ent);
1163                 set_ia32_use_frame(irn);
1164         }
1165
1166         /* get the AM attributes from the LEA */
1167         add_ia32_am_offs(irn, get_ia32_am_offs(lea));
1168         set_ia32_am_scale(irn, get_ia32_am_scale(lea));
1169         set_ia32_am_flavour(irn, get_ia32_am_flavour(lea));
1170
1171         set_ia32_am_sc(irn, get_ia32_am_sc(lea));
1172         if (is_ia32_am_sc_sign(lea))
1173                 set_ia32_am_sc_sign(irn);
1174
1175         set_ia32_op_type(irn, is_ia32_Ld(irn) ? ia32_AddrModeS : ia32_AddrModeD);
1176
1177         /* set base and index */
1178         set_irn_n(irn, 0, get_irn_n(lea, 0));
1179         set_irn_n(irn, 1, get_irn_n(lea, 1));
1180
1181         /* clear remat flag */
1182         set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1183
1184         if (is_ia32_Ld(irn))
1185                 DBG_OPT_LOAD_LEA(lea, irn);
1186         else
1187                 DBG_OPT_STORE_LEA(lea, irn);
1188
1189 }
1190
1191 /**
1192  * Sets new_right index of irn to right and new_left index to left.
1193  * Also exchange left and right
1194  */
1195 static void exchange_left_right(ir_node *irn, ir_node **left, ir_node **right, int new_left, int new_right) {
1196         ir_node *temp;
1197
1198         set_irn_n(irn, new_right, *right);
1199         set_irn_n(irn, new_left, *left);
1200
1201         temp   = *left;
1202         *left  = *right;
1203         *right = temp;
1204
1205         /* this is only needed for Compares, but currently ALL nodes
1206          * have this attribute :-) */
1207         set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1208 }
1209
1210 /**
1211  * Optimizes a pattern around irn to address mode if possible.
1212  */
1213 void ia32_optimize_am(ir_node *irn, void *env) {
1214         ia32_code_gen_t   *cg   = env;
1215         ir_node           *res  = irn;
1216         dbg_info          *dbg;
1217         ir_mode           *mode;
1218         ir_node           *block, *noreg_gp, *noreg_fp;
1219         ir_node           *left, *right, *temp;
1220         ir_node           *store, *load, *mem_proj;
1221         ir_node           *succ, *addr_b, *addr_i;
1222         int               check_am_src = 0;
1223         int               need_exchange_on_fail = 0;
1224         DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
1225
1226         if (! is_ia32_irn(irn))
1227                 return;
1228
1229         dbg      = get_irn_dbg_info(irn);
1230         mode     = get_irn_mode(irn);
1231         block    = get_nodes_block(irn);
1232         noreg_gp = ia32_new_NoReg_gp(cg);
1233         noreg_fp = ia32_new_NoReg_fp(cg);
1234
1235         DBG((mod, LEVEL_1, "checking for AM\n"));
1236
1237         /* 1st part: check for address calculations and transform the into Lea */
1238
1239         /* Following cases can occur:                                  */
1240         /* - Sub (l, imm) -> LEA [base - offset]                       */
1241         /* - Sub (l, r == LEA with ia32_am_O)   -> LEA [base - offset] */
1242         /* - Add (l, imm) -> LEA [base + offset]                       */
1243         /* - Add (l, r == LEA with ia32_am_O)  -> LEA [base + offset]  */
1244         /* - Add (l == LEA with ia32_am_O, r)  -> LEA [base + offset]  */
1245         /* - Add (l, r) -> LEA [base + index * scale]                  */
1246         /*              with scale > 1 iff l/r == shl (1,2,3)          */
1247
1248         if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
1249                 left  = get_irn_n(irn, 2);
1250                 right = get_irn_n(irn, 3);
1251
1252             /* Do not try to create a LEA if one of the operands is a Load. */
1253                 /* check is irn is a candidate for address calculation */
1254                 if (is_candidate(block, irn, 1)) {
1255                         DBG((mod, LEVEL_1, "\tfound address calculation candidate %+F ... ", irn));
1256                         res = fold_addr(cg, irn, noreg_gp);
1257
1258                         if (res == irn)
1259                                 DB((mod, LEVEL_1, "transformed into %+F\n", res));
1260                         else
1261                                 DB((mod, LEVEL_1, "not transformed\n"));
1262                 }
1263         }
1264
1265         /* 2nd part: fold following patterns:                                               */
1266         /* - Load  -> LEA into Load  } TODO: If the LEA is used by more than one Load/Store */
1267         /* - Store -> LEA into Store }       it might be better to keep the LEA             */
1268         /* - op -> Load into AMop with am_Source                                            */
1269         /*   conditions:                                                                    */
1270         /*     - op is am_Source capable AND                                                */
1271         /*     - the Load is only used by this op AND                                       */
1272         /*     - the Load is in the same block                                              */
1273         /* - Store -> op -> Load  into AMop with am_Dest                                    */
1274         /*   conditions:                                                                    */
1275         /*     - op is am_Dest capable AND                                                  */
1276         /*     - the Store uses the same address as the Load AND                            */
1277         /*     - the Load is only used by this op AND                                       */
1278         /*     - the Load and Store are in the same block AND                               */
1279         /*     - nobody else uses the result of the op                                      */
1280
1281         if ((res == irn) && (get_ia32_am_support(irn) != ia32_am_None) && !is_ia32_Lea(irn)) {
1282                 /* 1st: check for Load/Store -> LEA   */
1283                 if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn)) {
1284                         left = get_irn_n(irn, 0);
1285
1286                         if (is_ia32_Lea(left)) {
1287                                 const ir_edge_t *edge, *ne;
1288                                 ir_node *src;
1289
1290                                 /* merge all Loads/Stores connected to this LEA with the LEA */
1291                                 foreach_out_edge_safe(left, edge, ne) {
1292                                         src = get_edge_src_irn(edge);
1293
1294                                         if (src && (is_ia32_Ld(src) || is_ia32_St(src) || is_ia32_Store8Bit(src))) {
1295                                                 DBG((mod, LEVEL_1, "\nmerging %+F into %+F\n", left, irn));
1296                                                 merge_loadstore_lea(src, left);
1297                                         }
1298                                 }
1299                         }
1300                 }
1301                 /* check if the node is an address mode candidate */
1302                 else if (is_candidate(block, irn, 0)) {
1303                         DBG((mod, LEVEL_1, "\tfound address mode candidate %+F ... ", irn));
1304
1305                         left  = get_irn_n(irn, 2);
1306                         if (get_irn_arity(irn) == 4) {
1307                                 /* it's an "unary" operation */
1308                                 right = left;
1309                         }
1310                         else {
1311                                 right = get_irn_n(irn, 3);
1312                         }
1313
1314                         /* normalize commutative ops */
1315                         if (node_is_ia32_comm(irn)) {
1316                                 /* Assure that right operand is always a Load if there is one    */
1317                                 /* because non-commutative ops can only use Dest AM if the right */
1318                                 /* operand is a load, so we only need to check right operand.    */
1319                                 if (pred_is_specific_nodeblock(block, left, is_ia32_Ld))
1320                                 {
1321                                         exchange_left_right(irn, &left, &right, 3, 2);
1322                                         need_exchange_on_fail = 1;
1323                                 }
1324                         }
1325
1326                         /* check for Store -> op -> Load */
1327
1328                         /* Store -> op -> Load optimization is only possible if supported by op */
1329                         /* and if right operand is a Load                                       */
1330                         if ((get_ia32_am_support(irn) & ia32_am_Dest) &&
1331                                  pred_is_specific_nodeblock(block, right, is_ia32_Ld))
1332                         {
1333
1334                                 /* An address mode capable op always has a result Proj.                  */
1335                                 /* If this Proj is used by more than one other node, we don't need to    */
1336                                 /* check further, otherwise we check for Store and remember the address, */
1337                                 /* the Store points to. */
1338
1339                                 succ = get_res_proj(irn);
1340                                 assert(succ && "Couldn't find result proj");
1341
1342                                 addr_b = NULL;
1343                                 addr_i = NULL;
1344                                 store  = NULL;
1345
1346                                 /* now check for users and Store */
1347                                 if (ia32_get_irn_n_edges(succ) == 1) {
1348                                         succ = get_edge_src_irn(get_irn_out_edge_first(succ));
1349
1350                                         if (is_ia32_xStore(succ) || is_ia32_Store(succ)) {
1351                                                 store  = succ;
1352                                                 addr_b = get_irn_n(store, 0);
1353                                                 addr_i = get_irn_n(store, 1);
1354                                         }
1355                                 }
1356
1357                                 if (store) {
1358                                         /* we found a Store as single user: Now check for Load */
1359
1360                                         /* Extra check for commutative ops with two Loads */
1361                                         /* -> put the interesting Load right              */
1362                                         if (node_is_ia32_comm(irn) &&
1363                                                 pred_is_specific_nodeblock(block, left, is_ia32_Ld))
1364                                         {
1365                                                 if ((addr_b == get_irn_n(get_Proj_pred(left), 0)) &&
1366                                                         (addr_i == get_irn_n(get_Proj_pred(left), 1)))
1367                                                 {
1368                                                         /* We exchange left and right, so it's easier to kill     */
1369                                                         /* the correct Load later and to handle unary operations. */
1370                                                         set_irn_n(irn, 2, right);
1371                                                         set_irn_n(irn, 3, left);
1372
1373                                                         temp  = left;
1374                                                         left  = right;
1375                                                         right = temp;
1376
1377                                                         /* this is only needed for Compares, but currently ALL nodes
1378                                                          * have this attribute :-) */
1379                                                         set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1380                                                 }
1381                                         }
1382
1383                                         /* skip the Proj for easier access */
1384                                         load = get_Proj_pred(right);
1385
1386                                         /* Compare Load and Store address */
1387                                         if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1388                                                 /* Right Load is from same address, so we can */
1389                                                 /* disconnect the Load and Store here        */
1390
1391                                                 /* set new base, index and attributes */
1392                                                 set_irn_n(irn, 0, addr_b);
1393                                                 set_irn_n(irn, 1, addr_i);
1394                                                 add_ia32_am_offs(irn, get_ia32_am_offs(load));
1395                                                 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1396                                                 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1397                                                 set_ia32_op_type(irn, ia32_AddrModeD);
1398                                                 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1399                                                 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1400
1401                                                 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1402                                                 if (is_ia32_am_sc_sign(load))
1403                                                         set_ia32_am_sc_sign(irn);
1404
1405                                                 if (is_ia32_use_frame(load))
1406                                                         set_ia32_use_frame(irn);
1407
1408                                                 /* connect to Load memory and disconnect Load */
1409                                                 if (get_irn_arity(irn) == 5) {
1410                                                         /* binary AMop */
1411                                                         set_irn_n(irn, 4, get_irn_n(load, 2));
1412                                                         set_irn_n(irn, 3, noreg_gp);
1413                                                 }
1414                                                 else {
1415                                                         /* unary AMop */
1416                                                         set_irn_n(irn, 3, get_irn_n(load, 2));
1417                                                         set_irn_n(irn, 2, noreg_gp);
1418                                                 }
1419
1420                                                 /* connect the memory Proj of the Store to the op */
1421                                                 mem_proj = get_mem_proj(store);
1422                                                 set_Proj_pred(mem_proj, irn);
1423                                                 set_Proj_proj(mem_proj, 1);
1424
1425                                                 /* clear remat flag */
1426                                                 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1427
1428                                                 DBG_OPT_AM_D(load, store, irn);
1429
1430                                                 DB((mod, LEVEL_1, "merged with %+F and %+F into dest AM\n", load, store));
1431                                         }
1432                                 } /* if (store) */
1433                                 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1434                                         /* There was no store, check if we still can optimize for source address mode */
1435                                         check_am_src = 1;
1436                                 }
1437                         } /* if (support AM Dest) */
1438                         else if (get_ia32_am_support(irn) & ia32_am_Source) {
1439                                 /* op doesn't support am AM Dest -> check for AM Source */
1440                                 check_am_src = 1;
1441                         }
1442
1443                         /* was exchanged but optimize failed: exchange back */
1444                         if (check_am_src && need_exchange_on_fail)
1445                                 exchange_left_right(irn, &left, &right, 3, 2);
1446
1447                         need_exchange_on_fail = 0;
1448
1449                         /* normalize commutative ops */
1450                         if (check_am_src && node_is_ia32_comm(irn)) {
1451                                 /* Assure that left operand is always a Load if there is one */
1452                                 /* because non-commutative ops can only use Source AM if the */
1453                                 /* left operand is a Load, so we only need to check the left */
1454                                 /* operand afterwards.                                       */
1455                                 if (pred_is_specific_nodeblock(block, right, is_ia32_Ld))       {
1456                                         exchange_left_right(irn, &left, &right, 3, 2);
1457                                         need_exchange_on_fail = 1;
1458                                 }
1459                         }
1460
1461                         /* optimize op -> Load iff Load is only used by this op   */
1462                         /* and left operand is a Load which only used by this irn */
1463                         if (check_am_src                                        &&
1464                                 pred_is_specific_nodeblock(block, left, is_ia32_Ld) &&
1465                                 (ia32_get_irn_n_edges(left) == 1))
1466                         {
1467                                 left = get_Proj_pred(left);
1468
1469                                 addr_b = get_irn_n(left, 0);
1470                                 addr_i = get_irn_n(left, 1);
1471
1472                                 /* set new base, index and attributes */
1473                                 set_irn_n(irn, 0, addr_b);
1474                                 set_irn_n(irn, 1, addr_i);
1475                                 add_ia32_am_offs(irn, get_ia32_am_offs(left));
1476                                 set_ia32_am_scale(irn, get_ia32_am_scale(left));
1477                                 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
1478                                 set_ia32_op_type(irn, ia32_AddrModeS);
1479                                 set_ia32_frame_ent(irn, get_ia32_frame_ent(left));
1480                                 set_ia32_ls_mode(irn, get_ia32_ls_mode(left));
1481
1482                                 set_ia32_am_sc(irn, get_ia32_am_sc(left));
1483                                 if (is_ia32_am_sc_sign(left))
1484                                         set_ia32_am_sc_sign(irn);
1485
1486                                 /* clear remat flag */
1487                                 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1488
1489                                 if (is_ia32_use_frame(left))
1490                                         set_ia32_use_frame(irn);
1491
1492                                 /* connect to Load memory */
1493                                 if (get_irn_arity(irn) == 5) {
1494                                         /* binary AMop */
1495                                         set_irn_n(irn, 4, get_irn_n(left, 2));
1496
1497                                         /* this is only needed for Compares, but currently ALL nodes
1498                                          * have this attribute :-) */
1499                                         set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1500
1501                                         /* disconnect from Load */
1502                                         /* (make second op -> first, set second in to noreg) */
1503                                         set_irn_n(irn, 2, get_irn_n(irn, 3));
1504                                         set_irn_n(irn, 3, noreg_gp);
1505                                 }
1506                                 else {
1507                                         /* unary AMop */
1508                                         set_irn_n(irn, 3, get_irn_n(left, 2));
1509
1510                                         /* disconnect from Load */
1511                                         set_irn_n(irn, 2, noreg_gp);
1512                                 }
1513
1514                                 DBG_OPT_AM_S(left, irn);
1515
1516                                 /* If Load has a memory Proj, connect it to the op */
1517                                 mem_proj = get_mem_proj(left);
1518                                 if (mem_proj) {
1519                                         set_Proj_pred(mem_proj, irn);
1520                                         set_Proj_proj(mem_proj, 1);
1521                                 }
1522
1523                                 DB((mod, LEVEL_1, "merged with %+F into source AM\n", left));
1524                         }
1525                         else {
1526                                 /* was exchanged but optimize failed: exchange back */
1527                                 if (need_exchange_on_fail)
1528                                         exchange_left_right(irn, &left, &right, 3, 2);
1529                         }
1530                 }
1531         }
1532 }