3 * File name: ir/be/ia32/ia32_optimize.c
4 * Purpose: Implements several optimizations for IA32
5 * Author: Christian Wuerdig
7 * Copyright: (c) 2006 Universität Karlsruhe
8 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
18 #include "firm_types.h"
27 #include "../benode_t.h"
28 #include "../besched_t.h"
30 #include "ia32_new_nodes.h"
31 #include "bearch_ia32_t.h"
32 #include "gen_ia32_regalloc_if.h" /* the generated interface (register type and class defenitions) */
33 #include "ia32_transform.h"
34 #include "ia32_dbg_stat.h"
37 IA32_AM_CAND_NONE = 0,
38 IA32_AM_CAND_LEFT = 1,
39 IA32_AM_CAND_RIGHT = 2,
44 #define is_NoMem(irn) (get_irn_op(irn) == op_NoMem)
46 typedef int is_op_func_t(const ir_node *n);
49 * checks if a node represents the NOREG value
51 static int be_is_NoReg(ia32_code_gen_t *cg, const ir_node *irn) {
52 be_abi_irg_t *babi = cg->birg->abi;
53 const arch_register_t *fp_noreg = USE_SSE2(cg) ?
54 &ia32_xmm_regs[REG_XMM_NOREG] : &ia32_vfp_regs[REG_VFP_NOREG];
56 return (be_abi_get_callee_save_irn(babi, &ia32_gp_regs[REG_GP_NOREG]) == irn) ||
57 (be_abi_get_callee_save_irn(babi, fp_noreg) == irn);
62 /*************************************************
65 * | | ___ _ __ ___| |_ __ _ _ __ | |_ ___
66 * | | / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
67 * | |___| (_) | | | \__ \ || (_| | | | | |_\__ \
68 * \_____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
70 *************************************************/
73 * creates a unique ident by adding a number to a tag
75 * @param tag the tag string, must contain a %d if a number
78 static ident *unique_id(const char *tag)
80 static unsigned id = 0;
83 snprintf(str, sizeof(str), tag, ++id);
84 return new_id_from_str(str);
90 * Transforms a SymConst.
92 * @param mod the debug module
93 * @param block the block the new node should belong to
94 * @param node the ir SymConst node
95 * @param mode mode of the SymConst
96 * @return the created ia32 Const node
98 static ir_node *gen_SymConst(ia32_transform_env_t *env) {
100 dbg_info *dbg = env->dbg;
101 ir_mode *mode = env->mode;
102 ir_graph *irg = env->irg;
103 ir_node *block = env->block;
105 if (mode_is_float(mode)) {
107 if (USE_SSE2(env->cg))
108 cnst = new_rd_ia32_xConst(dbg, irg, block, get_irg_no_mem(irg), mode);
110 cnst = new_rd_ia32_vfConst(dbg, irg, block, get_irg_no_mem(irg), mode);
113 cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), mode);
115 set_ia32_Const_attr(cnst, env->irn);
121 * Get a primitive type for a mode.
123 static ir_type *get_prim_type(pmap *types, ir_mode *mode)
125 pmap_entry *e = pmap_find(types, mode);
130 snprintf(buf, sizeof(buf), "prim_type_%s", get_mode_name(mode));
131 res = new_type_primitive(new_id_from_str(buf), mode);
132 pmap_insert(types, mode, res);
140 * Get an entity that is initialized with a tarval
142 static entity *get_entity_for_tv(ia32_code_gen_t *cg, ir_node *cnst)
144 tarval *tv = get_Const_tarval(cnst);
145 pmap_entry *e = pmap_find(cg->isa->tv_ent, tv);
150 ir_mode *mode = get_irn_mode(cnst);
151 ir_type *tp = get_Const_type(cnst);
152 if (tp == firm_unknown_type)
153 tp = get_prim_type(cg->isa->types, mode);
155 res = new_entity(get_glob_type(), unique_id(".LC%u"), tp);
157 set_entity_ld_ident(res, get_entity_ident(res));
158 set_entity_visibility(res, visibility_local);
159 set_entity_variability(res, variability_constant);
160 set_entity_allocation(res, allocation_static);
162 /* we create a new entity here: It's initialization must resist on the
164 rem = current_ir_graph;
165 current_ir_graph = get_const_code_irg();
166 set_atomic_ent_value(res, new_Const_type(tv, tp));
167 current_ir_graph = rem;
169 pmap_insert(cg->isa->tv_ent, tv, res);
177 * Transforms a Const.
179 * @param mod the debug module
180 * @param block the block the new node should belong to
181 * @param node the ir Const node
182 * @param mode mode of the Const
183 * @return the created ia32 Const node
185 static ir_node *gen_Const(ia32_transform_env_t *env) {
188 ir_graph *irg = env->irg;
189 ir_node *block = env->block;
190 ir_node *node = env->irn;
191 dbg_info *dbg = env->dbg;
192 ir_mode *mode = env->mode;
194 if (mode_is_float(mode)) {
196 if (! USE_SSE2(env->cg)) {
197 cnst_classify_t clss = classify_Const(node);
199 if (clss == CNST_NULL)
200 return new_rd_ia32_vfldz(dbg, irg, block, mode);
201 else if (clss == CNST_ONE)
202 return new_rd_ia32_vfld1(dbg, irg, block, mode);
204 sym.entity_p = get_entity_for_tv(env->cg, node);
206 cnst = new_rd_SymConst(dbg, irg, block, sym, symconst_addr_ent);
208 cnst = gen_SymConst(env);
211 cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), get_irn_mode(node));
212 set_ia32_Const_attr(cnst, node);
220 * Transforms (all) Const's into ia32_Const and places them in the
221 * block where they are used (or in the cfg-pred Block in case of Phi's).
222 * Additionally all reference nodes are changed into mode_Is nodes.
224 void ia32_place_consts_set_modes(ir_node *irn, void *env) {
225 ia32_code_gen_t *cg = env;
226 ia32_transform_env_t tenv;
228 ir_node *pred, *cnst;
235 mode = get_irn_mode(irn);
237 /* transform all reference nodes into mode_Is nodes */
238 if (mode_is_reference(mode)) {
240 set_irn_mode(irn, mode);
243 tenv.block = get_nodes_block(irn);
246 DEBUG_ONLY(tenv.mod = cg->mod;)
248 /* Loop over all predecessors and check for Sym/Const nodes */
249 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
250 pred = get_irn_n(irn, i);
252 opc = get_irn_opcode(pred);
254 tenv.mode = get_irn_mode(pred);
255 tenv.dbg = get_irn_dbg_info(pred);
257 /* If it's a Phi, then we need to create the */
258 /* new Const in it's predecessor block */
260 tenv.block = get_Block_cfgpred_block(get_nodes_block(irn), i);
263 /* put the const into the block where the original const was */
264 if (! (cg->opt & IA32_OPT_PLACECNST)) {
265 tenv.block = get_nodes_block(pred);
270 cnst = gen_Const(&tenv);
273 cnst = gen_SymConst(&tenv);
279 /* if we found a const, then set it */
281 set_irn_n(irn, i, cnst);
288 /********************************************************************************************************
289 * _____ _ _ ____ _ _ _ _ _
290 * | __ \ | | | | / __ \ | | (_) (_) | | (_)
291 * | |__) |__ ___ _ __ | |__ ___ | | ___ | | | |_ __ | |_ _ _ __ ___ _ ______ _| |_ _ ___ _ __
292 * | ___/ _ \/ _ \ '_ \| '_ \ / _ \| |/ _ \ | | | | '_ \| __| | '_ ` _ \| |_ / _` | __| |/ _ \| '_ \
293 * | | | __/ __/ |_) | | | | (_) | | __/ | |__| | |_) | |_| | | | | | | |/ / (_| | |_| | (_) | | | |
294 * |_| \___|\___| .__/|_| |_|\___/|_|\___| \____/| .__/ \__|_|_| |_| |_|_/___\__,_|\__|_|\___/|_| |_|
297 ********************************************************************************************************/
300 * NOTE: THESE PEEPHOLE OPTIMIZATIONS MUST BE CALLED AFTER SCHEDULING AND REGISTER ALLOCATION.
303 static int ia32_cnst_compare(ir_node *n1, ir_node *n2) {
304 return get_ia32_id_cnst(n1) == get_ia32_id_cnst(n2);
308 * Checks for potential CJmp/CJmpAM optimization candidates.
310 static ir_node *ia32_determine_cjmp_cand(ir_node *irn, is_op_func_t *is_op_func) {
311 ir_node *cand = NULL;
312 ir_node *prev = sched_prev(irn);
314 if (is_Block(prev)) {
315 if (get_Block_n_cfgpreds(prev) == 1)
316 prev = get_Block_cfgpred(prev, 0);
321 /* The predecessor must be a ProjX. */
322 if (prev && is_Proj(prev) && get_irn_mode(prev) == mode_X) {
323 prev = get_Proj_pred(prev);
325 if (is_op_func(prev))
332 static int is_TestJmp_cand(const ir_node *irn) {
333 return is_ia32_TestJmp(irn) || is_ia32_And(irn);
337 * Checks if two consecutive arguments of cand matches
338 * the two arguments of irn (TestJmp).
340 static int is_TestJmp_replacement(ir_node *cand, ir_node *irn) {
341 ir_node *in1 = get_irn_n(irn, 0);
342 ir_node *in2 = get_irn_n(irn, 1);
343 int i, n = get_irn_arity(cand);
346 for (i = 0; i < n - 1; i++) {
347 if (get_irn_n(cand, i) == in1 &&
348 get_irn_n(cand, i + 1) == in2)
356 return ia32_cnst_compare(cand, irn);
362 * Tries to replace a TestJmp by a CJmp or CJmpAM (in case of And)
364 static void ia32_optimize_TestJmp(ir_node *irn, ia32_code_gen_t *cg) {
365 ir_node *cand = ia32_determine_cjmp_cand(irn, is_TestJmp_cand);
368 /* we found a possible candidate */
369 replace = cand ? is_TestJmp_replacement(cand, irn) : 0;
372 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
374 if (is_ia32_And(cand))
375 set_irn_op(irn, op_ia32_CJmpAM);
377 set_irn_op(irn, op_ia32_CJmp);
379 DB((cg->mod, LEVEL_1, "%+F\n", irn));
383 static int is_CondJmp_cand(const ir_node *irn) {
384 return is_ia32_CondJmp(irn) || is_ia32_Sub(irn);
388 * Checks if the arguments of cand are the same of irn.
390 static int is_CondJmp_replacement(ir_node *cand, ir_node *irn) {
391 int i, n = get_irn_arity(cand);
394 for (i = 0; i < n; i++) {
395 if (get_irn_n(cand, i) != get_irn_n(irn, i)) {
402 return ia32_cnst_compare(cand, irn);
408 * Tries to replace a CondJmp by a CJmpAM
410 static void ia32_optimize_CondJmp(ir_node *irn, ia32_code_gen_t *cg) {
411 ir_node *cand = ia32_determine_cjmp_cand(irn, is_CondJmp_cand);
414 /* we found a possible candidate */
415 replace = cand ? is_CondJmp_replacement(cand, irn) : 0;
418 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
421 set_irn_op(irn, op_ia32_CJmpAM);
423 DB((cg->mod, LEVEL_1, "%+F\n", irn));
428 * Creates a Push from Store(IncSP(gp_reg_size))
430 static void ia32_create_Push(ir_node *irn, ia32_code_gen_t *cg) {
431 ir_node *sp = get_irn_n(irn, 0);
432 ir_node *val, *next, *push, *bl, *proj_M, *proj_res, *old_proj_M;
433 const ir_edge_t *edge;
435 if (get_ia32_am_offs(irn) || !be_is_IncSP(sp))
438 if (arch_get_irn_register(cg->arch_env, get_irn_n(irn, 1)) !=
439 &ia32_gp_regs[REG_GP_NOREG])
442 val = get_irn_n(irn, 2);
443 if (mode_is_float(get_irn_mode(val)))
446 if (be_get_IncSP_direction(sp) != be_stack_dir_expand ||
447 be_get_IncSP_offset(sp) != get_mode_size_bytes(ia32_reg_classes[CLASS_ia32_gp].mode))
450 /* ok, translate into Push */
451 edge = get_irn_out_edge_first(irn);
452 old_proj_M = get_edge_src_irn(edge);
454 next = sched_next(irn);
458 bl = get_nodes_block(irn);
459 push = new_rd_ia32_Push(NULL, current_ir_graph, bl,
460 be_get_IncSP_pred(sp), val, be_get_IncSP_mem(sp));
461 proj_res = new_r_Proj(current_ir_graph, bl, push, get_irn_mode(sp), pn_ia32_Push_stack);
462 proj_M = new_r_Proj(current_ir_graph, bl, push, mode_M, pn_ia32_Push_M);
464 /* copy a possible constant from the store */
465 set_ia32_id_cnst(push, get_ia32_id_cnst(irn));
466 set_ia32_immop_type(push, get_ia32_immop_type(irn));
468 /* the push must have SP out register */
469 arch_set_irn_register(cg->arch_env, push, arch_get_irn_register(cg->arch_env, sp));
471 exchange(old_proj_M, proj_M);
472 exchange(sp, proj_res);
473 sched_add_before(next, push);
474 sched_add_after(push, proj_res);
478 * Creates a Pop from IncSP(Load(sp))
480 static void ia32_create_Pop(ir_node *irn, ia32_code_gen_t *cg) {
481 ir_node *old_proj_M = be_get_IncSP_mem(irn);
482 ir_node *load = skip_Proj(old_proj_M);
483 ir_node *old_proj_res = NULL;
484 ir_node *bl, *pop, *next, *proj_res, *proj_sp, *proj_M;
485 const ir_edge_t *edge;
486 const arch_register_t *reg, *sp;
488 if (! is_ia32_Load(load) || get_ia32_am_offs(load))
491 if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 1)) !=
492 &ia32_gp_regs[REG_GP_NOREG])
494 if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 0)) != cg->isa->arch_isa.sp)
497 /* ok, translate into pop */
498 foreach_out_edge(load, edge) {
499 ir_node *succ = get_edge_src_irn(edge);
500 if (succ != old_proj_M) {
505 if (! old_proj_res) {
507 return; /* should not happen */
510 bl = get_nodes_block(load);
512 /* IncSP is typically scheduled after the load, so remove it first */
514 next = sched_next(old_proj_res);
515 sched_remove(old_proj_res);
518 reg = arch_get_irn_register(cg->arch_env, load);
519 sp = arch_get_irn_register(cg->arch_env, irn);
521 pop = new_rd_ia32_Pop(NULL, current_ir_graph, bl, get_irn_n(irn, 0), get_irn_n(load, 2));
522 proj_res = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(old_proj_res), pn_ia32_Pop_res);
523 proj_sp = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(irn), pn_ia32_Pop_stack);
524 proj_M = new_r_Proj(current_ir_graph, bl, pop, mode_M, pn_ia32_Pop_M);
526 exchange(old_proj_M, proj_M);
527 exchange(old_proj_res, proj_res);
528 exchange(irn, proj_sp);
530 arch_set_irn_register(cg->arch_env, proj_res, reg);
531 arch_set_irn_register(cg->arch_env, proj_sp, sp);
533 sched_add_before(next, proj_sp);
534 sched_add_before(proj_sp, proj_res);
535 sched_add_before(proj_res,pop);
539 * Tries to optimize two following IncSP.
541 static void ia32_optimize_IncSP(ir_node *irn, ia32_code_gen_t *cg) {
542 ir_node *prev = be_get_IncSP_pred(irn);
543 int real_uses = get_irn_n_edges(prev);
545 if (be_is_IncSP(prev) && real_uses == 1) {
546 /* first IncSP has only one IncSP user, kill the first one */
547 unsigned prev_offs = be_get_IncSP_offset(prev);
548 be_stack_dir_t prev_dir = be_get_IncSP_direction(prev);
549 unsigned curr_offs = be_get_IncSP_offset(irn);
550 be_stack_dir_t curr_dir = be_get_IncSP_direction(irn);
552 int new_ofs = prev_offs * (prev_dir == be_stack_dir_expand ? -1 : +1) +
553 curr_offs * (curr_dir == be_stack_dir_expand ? -1 : +1);
557 curr_dir = be_stack_dir_expand;
560 curr_dir = be_stack_dir_shrink;
561 be_set_IncSP_offset(prev, 0);
562 be_set_IncSP_offset(irn, (unsigned)new_ofs);
563 be_set_IncSP_direction(irn, curr_dir);
565 /* Omit the optimized IncSP */
566 be_set_IncSP_pred(irn, be_get_IncSP_pred(prev));
571 * Performs Peephole Optimizations.
573 void ia32_peephole_optimization(ir_node *irn, void *env) {
574 ia32_code_gen_t *cg = env;
576 if (is_ia32_TestJmp(irn))
577 ia32_optimize_TestJmp(irn, cg);
578 else if (is_ia32_CondJmp(irn))
579 ia32_optimize_CondJmp(irn, cg);
580 /* seems to be buggy when using Pushes */
581 // else if (be_is_IncSP(irn))
582 // ia32_optimize_IncSP(irn, cg);
583 else if (is_ia32_Store(irn))
584 ia32_create_Push(irn, cg);
589 /******************************************************************
591 * /\ | | | | | \/ | | |
592 * / \ __| | __| |_ __ ___ ___ ___| \ / | ___ __| | ___
593 * / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
594 * / ____ \ (_| | (_| | | | __/\__ \__ \ | | | (_) | (_| | __/
595 * /_/ \_\__,_|\__,_|_| \___||___/___/_| |_|\___/ \__,_|\___|
597 ******************************************************************/
604 static int node_is_ia32_comm(const ir_node *irn) {
605 return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
608 static int ia32_get_irn_n_edges(const ir_node *irn) {
609 const ir_edge_t *edge;
612 foreach_out_edge(irn, edge) {
620 * Returns the first mode_M Proj connected to irn.
622 static ir_node *get_mem_proj(const ir_node *irn) {
623 const ir_edge_t *edge;
626 assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
628 foreach_out_edge(irn, edge) {
629 src = get_edge_src_irn(edge);
631 assert(is_Proj(src) && "Proj expected");
633 if (get_irn_mode(src) == mode_M)
641 * Returns the first Proj with mode != mode_M connected to irn.
643 static ir_node *get_res_proj(const ir_node *irn) {
644 const ir_edge_t *edge;
647 assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
649 foreach_out_edge(irn, edge) {
650 src = get_edge_src_irn(edge);
652 assert(is_Proj(src) && "Proj expected");
654 if (get_irn_mode(src) != mode_M)
662 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
664 * @param pred The node to be checked
665 * @param is_op_func The check-function
666 * @return 1 if conditions are fulfilled, 0 otherwise
668 static int pred_is_specific_node(const ir_node *pred, is_op_func_t *is_op_func) {
669 if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
677 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
678 * and if the predecessor is in block bl.
680 * @param bl The block
681 * @param pred The node to be checked
682 * @param is_op_func The check-function
683 * @return 1 if conditions are fulfilled, 0 otherwise
685 static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
686 int (*is_op_func)(const ir_node *n))
689 pred = get_Proj_pred(pred);
690 if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
699 * Checks if irn is a candidate for address calculation.
701 * - none of the operand must be a Load within the same block OR
702 * - all Loads must have more than one user OR
703 * - the irn has a frame entity (it's a former FrameAddr)
705 * @param block The block the Loads must/mustnot be in
706 * @param irn The irn to check
707 * return 1 if irn is a candidate, 0 otherwise
709 static int is_addr_candidate(const ir_node *block, const ir_node *irn) {
710 ir_node *in, *left, *right;
713 left = get_irn_n(irn, 2);
714 right = get_irn_n(irn, 3);
718 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
719 n = ia32_get_irn_n_edges(in);
720 is_cand = (n == 1) ? 0 : is_cand; /* load with only one user: don't create LEA */
725 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
726 n = ia32_get_irn_n_edges(in);
727 is_cand = (n == 1) ? 0 : is_cand; /* load with only one user: don't create LEA */
730 is_cand = get_ia32_frame_ent(irn) ? 1 : is_cand;
736 * Checks if irn is a candidate for address mode.
739 * - at least one operand has to be a Load within the same block AND
740 * - the load must not have other users than the irn AND
741 * - the irn must not have a frame entity set
743 * @param h The height information of the irg
744 * @param block The block the Loads must/mustnot be in
745 * @param irn The irn to check
746 * return 0 if irn is no candidate, 1 if left load can be used, 2 if right one, 3 for both
748 static ia32_am_cand_t is_am_candidate(heights_t *h, const ir_node *block, ir_node *irn) {
749 ir_node *in, *load, *other, *left, *right;
750 int n, is_cand = 0, cand;
752 if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn) || is_ia32_vfild(irn) || is_ia32_vfist(irn))
755 left = get_irn_n(irn, 2);
756 right = get_irn_n(irn, 3);
760 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
761 n = ia32_get_irn_n_edges(in);
762 is_cand = (n == 1) ? 1 : is_cand; /* load with more than one user: no AM */
764 load = get_Proj_pred(in);
767 /* If there is a data dependency of other irn from load: cannot use AM */
768 if (get_nodes_block(other) == block) {
769 other = skip_Proj(other);
770 is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
774 cand = is_cand ? IA32_AM_CAND_LEFT : IA32_AM_CAND_NONE;
778 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
779 n = ia32_get_irn_n_edges(in);
780 is_cand = (n == 1) ? 1 : is_cand; /* load with more than one user: no AM */
782 load = get_Proj_pred(in);
785 /* If there is a data dependency of other irn from load: cannot use load */
786 if (get_nodes_block(other) == block) {
787 other = skip_Proj(other);
788 is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
792 cand = is_cand ? (cand | IA32_AM_CAND_RIGHT) : cand;
794 /* if the irn has a frame entity: we do not use address mode */
795 return get_ia32_frame_ent(irn) ? IA32_AM_CAND_NONE : cand;
799 * Compares the base and index addr and the load/store entities
800 * and returns 1 if they are equal.
802 static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
803 const ir_node *addr_b, const ir_node *addr_i)
805 int is_equal = (addr_b == get_irn_n(load, 0)) && (addr_i == get_irn_n(load, 1));
806 entity *lent = get_ia32_frame_ent(load);
807 entity *sent = get_ia32_frame_ent(store);
808 ident *lid = get_ia32_am_sc(load);
809 ident *sid = get_ia32_am_sc(store);
810 char *loffs = get_ia32_am_offs(load);
811 char *soffs = get_ia32_am_offs(store);
813 /* are both entities set and equal? */
814 if (is_equal && (lent || sent))
815 is_equal = lent && sent && (lent == sent);
817 /* are address mode idents set and equal? */
818 if (is_equal && (lid || sid))
819 is_equal = lid && sid && (lid == sid);
821 /* are offsets set and equal */
822 if (is_equal && (loffs || soffs))
823 is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;
825 /* are the load and the store of the same mode? */
826 is_equal = is_equal ? get_ia32_ls_mode(load) == get_ia32_ls_mode(store) : 0;
831 typedef enum _ia32_take_lea_attr {
832 IA32_LEA_ATTR_NONE = 0,
833 IA32_LEA_ATTR_BASE = (1 << 0),
834 IA32_LEA_ATTR_INDEX = (1 << 1),
835 IA32_LEA_ATTR_OFFS = (1 << 2),
836 IA32_LEA_ATTR_SCALE = (1 << 3),
837 IA32_LEA_ATTR_AMSC = (1 << 4),
838 IA32_LEA_ATTR_FENT = (1 << 5)
839 } ia32_take_lea_attr;
842 * Decides if we have to keep the LEA operand or if we can assimilate it.
844 static int do_new_lea(ir_node *irn, ir_node *base, ir_node *index, ir_node *lea,
845 int have_am_sc, ia32_code_gen_t *cg)
847 ir_node *lea_base = get_irn_n(lea, 0);
848 ir_node *lea_idx = get_irn_n(lea, 1);
849 entity *irn_ent = get_ia32_frame_ent(irn);
850 entity *lea_ent = get_ia32_frame_ent(lea);
852 int is_noreg_base = be_is_NoReg(cg, base);
853 int is_noreg_index = be_is_NoReg(cg, index);
854 ia32_am_flavour_t am_flav = get_ia32_am_flavour(lea);
856 /* If the Add and the LEA both have a different frame entity set: keep */
857 if (irn_ent && lea_ent && (irn_ent != lea_ent))
858 return IA32_LEA_ATTR_NONE;
859 else if (! irn_ent && lea_ent)
860 ret_val |= IA32_LEA_ATTR_FENT;
862 /* If the Add and the LEA both have already an address mode symconst: keep */
863 if (have_am_sc && get_ia32_am_sc(lea))
864 return IA32_LEA_ATTR_NONE;
865 else if (get_ia32_am_sc(lea))
866 ret_val |= IA32_LEA_ATTR_AMSC;
868 /* Check the different base-index combinations */
870 if (! is_noreg_base && ! is_noreg_index) {
871 /* Assimilate if base is the lea and the LEA is just a Base + Offset calculation */
872 if ((base == lea) && ! (am_flav & ia32_I ? 1 : 0)) {
873 if (am_flav & ia32_O)
874 ret_val |= IA32_LEA_ATTR_OFFS;
876 ret_val |= IA32_LEA_ATTR_BASE;
879 return IA32_LEA_ATTR_NONE;
881 else if (! is_noreg_base && is_noreg_index) {
882 /* Base is set but index not */
884 /* Base points to LEA: assimilate everything */
885 if (am_flav & ia32_O)
886 ret_val |= IA32_LEA_ATTR_OFFS;
887 if (am_flav & ia32_S)
888 ret_val |= IA32_LEA_ATTR_SCALE;
889 if (am_flav & ia32_I)
890 ret_val |= IA32_LEA_ATTR_INDEX;
892 ret_val |= IA32_LEA_ATTR_BASE;
894 else if (am_flav & ia32_B ? 0 : 1) {
895 /* Base is not the LEA but the LEA is an index only calculation: assimilate */
896 if (am_flav & ia32_O)
897 ret_val |= IA32_LEA_ATTR_OFFS;
898 if (am_flav & ia32_S)
899 ret_val |= IA32_LEA_ATTR_SCALE;
901 ret_val |= IA32_LEA_ATTR_INDEX;
904 return IA32_LEA_ATTR_NONE;
906 else if (is_noreg_base && ! is_noreg_index) {
907 /* Index is set but not base */
909 /* Index points to LEA: assimilate everything */
910 if (am_flav & ia32_O)
911 ret_val |= IA32_LEA_ATTR_OFFS;
912 if (am_flav & ia32_S)
913 ret_val |= IA32_LEA_ATTR_SCALE;
914 if (am_flav & ia32_B)
915 ret_val |= IA32_LEA_ATTR_BASE;
917 ret_val |= IA32_LEA_ATTR_INDEX;
919 else if (am_flav & ia32_I ? 0 : 1) {
920 /* Index is not the LEA but the LEA is a base only calculation: assimilate */
921 if (am_flav & ia32_O)
922 ret_val |= IA32_LEA_ATTR_OFFS;
923 if (am_flav & ia32_S)
924 ret_val |= IA32_LEA_ATTR_SCALE;
926 ret_val |= IA32_LEA_ATTR_BASE;
929 return IA32_LEA_ATTR_NONE;
932 assert(0 && "There must have been set base or index");
940 * Folds Add or Sub to LEA if possible
942 static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn, ir_node *noreg) {
943 ir_graph *irg = get_irn_irg(irn);
944 dbg_info *dbg = get_irn_dbg_info(irn);
945 ir_node *block = get_nodes_block(irn);
947 ir_node *shift = NULL;
948 ir_node *lea_o = NULL;
951 const char *offs_cnst = NULL;
952 char *offs_lea = NULL;
959 entity *lea_ent = NULL;
960 ir_node *left, *right, *temp;
961 ir_node *base, *index;
962 ia32_am_flavour_t am_flav;
963 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
965 if (is_ia32_Add(irn))
968 left = get_irn_n(irn, 2);
969 right = get_irn_n(irn, 3);
971 /* "normalize" arguments in case of add with two operands */
972 if (isadd && ! be_is_NoReg(cg, right)) {
973 /* put LEA == ia32_am_O as right operand */
974 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
975 set_irn_n(irn, 2, right);
976 set_irn_n(irn, 3, left);
982 /* put LEA != ia32_am_O as left operand */
983 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
984 set_irn_n(irn, 2, right);
985 set_irn_n(irn, 3, left);
991 /* put SHL as left operand iff left is NOT a LEA */
992 if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
993 set_irn_n(irn, 2, right);
994 set_irn_n(irn, 3, left);
1007 /* check for operation with immediate */
1008 if (is_ia32_ImmConst(irn)) {
1009 DBG((mod, LEVEL_1, "\tfound op with imm const"));
1011 offs_cnst = get_ia32_cnst(irn);
1014 else if (is_ia32_ImmSymConst(irn)) {
1015 DBG((mod, LEVEL_1, "\tfound op with imm symconst"));
1019 am_sc = get_ia32_id_cnst(irn);
1020 am_sc_sign = is_ia32_am_sc_sign(irn);
1023 /* determine the operand which needs to be checked */
1024 temp = be_is_NoReg(cg, right) ? left : right;
1026 /* check if right operand is AMConst (LEA with ia32_am_O) */
1027 /* but we can only eat it up if there is no other symconst */
1028 /* because the linker won't accept two symconsts */
1029 if (! have_am_sc && is_ia32_Lea(temp) && get_ia32_am_flavour(temp) == ia32_am_O) {
1030 DBG((mod, LEVEL_1, "\tgot op with LEA am_O"));
1032 offs_lea = get_ia32_am_offs(temp);
1033 am_sc = get_ia32_am_sc(temp);
1034 am_sc_sign = is_ia32_am_sc_sign(temp);
1044 /* default for add -> make right operand to index */
1048 DBG((mod, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
1050 /* determine the operand which needs to be checked */
1052 if (is_ia32_Lea(left)) {
1056 /* check for SHL 1,2,3 */
1057 if (pred_is_specific_node(temp, is_ia32_Shl)) {
1058 temp = get_Proj_pred(temp);
1061 if (get_ia32_Immop_tarval(temp)) {
1062 scale = get_tarval_long(get_ia32_Immop_tarval(temp));
1065 index = get_irn_n(temp, 2);
1067 DBG((mod, LEVEL_1, "\tgot scaled index %+F\n", index));
1077 if (! be_is_NoReg(cg, index)) {
1078 /* if we have index, but left == right -> no base */
1079 if (left == right) {
1082 else if (! is_ia32_Lea(left) && (index != right)) {
1083 /* index != right -> we found a good Shl */
1084 /* left != LEA -> this Shl was the left operand */
1085 /* -> base is right operand */
1086 base = (right == lea_o) ? noreg : right;
1091 /* Try to assimilate a LEA as left operand */
1092 if (is_ia32_Lea(left) && (get_ia32_am_flavour(left) != ia32_am_O)) {
1093 /* check if we can assimilate the LEA */
1094 int take_attr = do_new_lea(irn, base, index, left, have_am_sc, cg);
1096 if (take_attr == IA32_LEA_ATTR_NONE) {
1097 DBG((mod, LEVEL_1, "\tleave old LEA, creating new one\n"));
1100 DBG((mod, LEVEL_1, "\tgot LEA as left operand ... assimilating\n"));
1101 lea = left; /* for statistics */
1103 if (take_attr & IA32_LEA_ATTR_OFFS)
1104 offs = get_ia32_am_offs(left);
1106 if (take_attr & IA32_LEA_ATTR_AMSC) {
1107 am_sc = get_ia32_am_sc(left);
1109 am_sc_sign = is_ia32_am_sc_sign(left);
1112 if (take_attr & IA32_LEA_ATTR_SCALE)
1113 scale = get_ia32_am_scale(left);
1115 if (take_attr & IA32_LEA_ATTR_BASE)
1116 base = get_irn_n(left, 0);
1118 if (take_attr & IA32_LEA_ATTR_INDEX)
1119 index = get_irn_n(left, 1);
1121 if (take_attr & IA32_LEA_ATTR_FENT)
1122 lea_ent = get_ia32_frame_ent(left);
1126 /* ok, we can create a new LEA */
1128 res = new_rd_ia32_Lea(dbg, irg, block, base, index, mode_Is);
1130 /* add the old offset of a previous LEA */
1132 add_ia32_am_offs(res, offs);
1135 /* add the new offset */
1138 add_ia32_am_offs(res, offs_cnst);
1141 add_ia32_am_offs(res, offs_lea);
1145 /* either lea_O-cnst, -cnst or -lea_O */
1148 add_ia32_am_offs(res, offs_lea);
1151 sub_ia32_am_offs(res, offs_cnst);
1154 sub_ia32_am_offs(res, offs_lea);
1158 /* set the address mode symconst */
1160 set_ia32_am_sc(res, am_sc);
1162 set_ia32_am_sc_sign(res);
1165 /* copy the frame entity (could be set in case of Add */
1166 /* which was a FrameAddr) */
1168 set_ia32_frame_ent(res, lea_ent);
1170 set_ia32_frame_ent(res, get_ia32_frame_ent(irn));
1172 if (get_ia32_frame_ent(res))
1173 set_ia32_use_frame(res);
1176 set_ia32_am_scale(res, scale);
1178 am_flav = ia32_am_N;
1179 /* determine new am flavour */
1180 if (offs || offs_cnst || offs_lea || have_am_sc) {
1183 if (! be_is_NoReg(cg, base)) {
1186 if (! be_is_NoReg(cg, index)) {
1192 set_ia32_am_flavour(res, am_flav);
1194 set_ia32_op_type(res, ia32_AddrModeS);
1196 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
1198 DBG((mod, LEVEL_1, "\tLEA [%+F + %+F * %d + %s]\n", base, index, scale, get_ia32_am_offs(res)));
1200 /* we will exchange it, report here before the Proj is created */
1201 if (shift && lea && lea_o)
1202 DBG_OPT_LEA4(irn, lea_o, lea, shift, res);
1203 else if (shift && lea)
1204 DBG_OPT_LEA3(irn, lea, shift, res);
1205 else if (shift && lea_o)
1206 DBG_OPT_LEA3(irn, lea_o, shift, res);
1207 else if (lea && lea_o)
1208 DBG_OPT_LEA3(irn, lea_o, lea, res);
1210 DBG_OPT_LEA2(irn, shift, res);
1212 DBG_OPT_LEA2(irn, lea, res);
1214 DBG_OPT_LEA2(irn, lea_o, res);
1216 DBG_OPT_LEA1(irn, res);
1218 /* get the result Proj of the Add/Sub */
1219 irn = get_res_proj(irn);
1221 assert(irn && "Couldn't find result proj");
1223 /* exchange the old op with the new LEA */
1232 * Merges a Load/Store node with a LEA.
1233 * @param irn The Load/Store node
1234 * @param lea The LEA
1236 static void merge_loadstore_lea(ir_node *irn, ir_node *lea) {
1237 entity *irn_ent = get_ia32_frame_ent(irn);
1238 entity *lea_ent = get_ia32_frame_ent(lea);
1240 /* If the irn and the LEA both have a different frame entity set: do not merge */
1241 if (irn_ent && lea_ent && (irn_ent != lea_ent))
1243 else if (! irn_ent && lea_ent) {
1244 set_ia32_frame_ent(irn, lea_ent);
1245 set_ia32_use_frame(irn);
1248 /* get the AM attributes from the LEA */
1249 add_ia32_am_offs(irn, get_ia32_am_offs(lea));
1250 set_ia32_am_scale(irn, get_ia32_am_scale(lea));
1251 set_ia32_am_flavour(irn, get_ia32_am_flavour(lea));
1253 set_ia32_am_sc(irn, get_ia32_am_sc(lea));
1254 if (is_ia32_am_sc_sign(lea))
1255 set_ia32_am_sc_sign(irn);
1257 set_ia32_op_type(irn, is_ia32_Ld(irn) ? ia32_AddrModeS : ia32_AddrModeD);
1259 /* set base and index */
1260 set_irn_n(irn, 0, get_irn_n(lea, 0));
1261 set_irn_n(irn, 1, get_irn_n(lea, 1));
1263 /* clear remat flag */
1264 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1266 if (is_ia32_Ld(irn))
1267 DBG_OPT_LOAD_LEA(lea, irn);
1269 DBG_OPT_STORE_LEA(lea, irn);
1274 * Sets new_right index of irn to right and new_left index to left.
1275 * Also exchange left and right
1277 static void exchange_left_right(ir_node *irn, ir_node **left, ir_node **right, int new_left, int new_right) {
1280 set_irn_n(irn, new_right, *right);
1281 set_irn_n(irn, new_left, *left);
1287 /* this is only needed for Compares, but currently ALL nodes
1288 * have this attribute :-) */
1289 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1293 * Performs address calculation optimization (create LEAs if possible)
1295 static void optimize_lea(ir_node *irn, void *env) {
1296 ia32_code_gen_t *cg = env;
1297 ir_node *block, *noreg_gp, *left, *right;
1299 if (! is_ia32_irn(irn))
1302 /* Following cases can occur: */
1303 /* - Sub (l, imm) -> LEA [base - offset] */
1304 /* - Sub (l, r == LEA with ia32_am_O) -> LEA [base - offset] */
1305 /* - Add (l, imm) -> LEA [base + offset] */
1306 /* - Add (l, r == LEA with ia32_am_O) -> LEA [base + offset] */
1307 /* - Add (l == LEA with ia32_am_O, r) -> LEA [base + offset] */
1308 /* - Add (l, r) -> LEA [base + index * scale] */
1309 /* with scale > 1 iff l/r == shl (1,2,3) */
1311 if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
1312 left = get_irn_n(irn, 2);
1313 right = get_irn_n(irn, 3);
1314 block = get_nodes_block(irn);
1315 noreg_gp = ia32_new_NoReg_gp(cg);
1317 /* Do not try to create a LEA if one of the operands is a Load. */
1318 /* check is irn is a candidate for address calculation */
1319 if (is_addr_candidate(block, irn)) {
1322 DBG((cg->mod, LEVEL_1, "\tfound address calculation candidate %+F ... ", irn));
1323 res = fold_addr(cg, irn, noreg_gp);
1326 DB((cg->mod, LEVEL_1, "transformed into %+F\n", res));
1328 DB((cg->mod, LEVEL_1, "not transformed\n"));
1331 else if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn)) {
1332 /* - Load -> LEA into Load } TODO: If the LEA is used by more than one Load/Store */
1333 /* - Store -> LEA into Store } it might be better to keep the LEA */
1334 left = get_irn_n(irn, 0);
1336 if (is_ia32_Lea(left)) {
1337 const ir_edge_t *edge, *ne;
1340 /* merge all Loads/Stores connected to this LEA with the LEA */
1341 foreach_out_edge_safe(left, edge, ne) {
1342 src = get_edge_src_irn(edge);
1344 if (src && (is_ia32_Ld(src) || is_ia32_St(src) || is_ia32_Store8Bit(src))) {
1345 DBG((cg->mod, LEVEL_1, "\nmerging %+F into %+F\n", left, irn));
1346 if (! is_ia32_got_lea(src))
1347 merge_loadstore_lea(src, left);
1348 set_ia32_got_lea(src);
1357 * Checks for address mode patterns and performs the
1358 * necessary transformations.
1359 * This function is called by a walker.
1361 static void optimize_am(ir_node *irn, void *env) {
1362 ia32_am_opt_env_t *am_opt_env = env;
1363 ia32_code_gen_t *cg = am_opt_env->cg;
1364 heights_t *h = am_opt_env->h;
1365 ir_node *block, *noreg_gp, *noreg_fp;
1366 ir_node *left, *right;
1367 ir_node *store, *load, *mem_proj;
1368 ir_node *succ, *addr_b, *addr_i;
1369 int check_am_src = 0;
1370 int need_exchange_on_fail = 0;
1371 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
1373 if (! is_ia32_irn(irn))
1376 block = get_nodes_block(irn);
1377 noreg_gp = ia32_new_NoReg_gp(cg);
1378 noreg_fp = ia32_new_NoReg_fp(cg);
1380 DBG((mod, LEVEL_1, "checking for AM\n"));
1382 /* fold following patterns: */
1383 /* - op -> Load into AMop with am_Source */
1385 /* - op is am_Source capable AND */
1386 /* - the Load is only used by this op AND */
1387 /* - the Load is in the same block */
1388 /* - Store -> op -> Load into AMop with am_Dest */
1390 /* - op is am_Dest capable AND */
1391 /* - the Store uses the same address as the Load AND */
1392 /* - the Load is only used by this op AND */
1393 /* - the Load and Store are in the same block AND */
1394 /* - nobody else uses the result of the op */
1396 if ((get_ia32_am_support(irn) != ia32_am_None) && ! is_ia32_Lea(irn)) {
1397 ia32_am_cand_t cand = is_am_candidate(h, block, irn);
1398 ia32_am_cand_t orig_cand = cand;
1400 /* cand == 1: load is left; cand == 2: load is right; */
1402 if (cand == IA32_AM_CAND_NONE)
1405 DBG((mod, LEVEL_1, "\tfound address mode candidate %+F ... ", irn));
1407 left = get_irn_n(irn, 2);
1408 if (get_irn_arity(irn) == 4) {
1409 /* it's an "unary" operation */
1413 right = get_irn_n(irn, 3);
1416 /* normalize commutative ops */
1417 if (node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_LEFT)) {
1419 /* Assure that right operand is always a Load if there is one */
1420 /* because non-commutative ops can only use Dest AM if the right */
1421 /* operand is a load, so we only need to check right operand. */
1423 exchange_left_right(irn, &left, &right, 3, 2);
1424 need_exchange_on_fail = 1;
1426 /* now: load is right */
1427 cand = IA32_AM_CAND_RIGHT;
1430 /* check for Store -> op -> Load */
1432 /* Store -> op -> Load optimization is only possible if supported by op */
1433 /* and if right operand is a Load */
1434 if ((get_ia32_am_support(irn) & ia32_am_Dest) && (cand & IA32_AM_CAND_RIGHT))
1436 /* An address mode capable op always has a result Proj. */
1437 /* If this Proj is used by more than one other node, we don't need to */
1438 /* check further, otherwise we check for Store and remember the address, */
1439 /* the Store points to. */
1441 succ = get_res_proj(irn);
1442 assert(succ && "Couldn't find result proj");
1448 /* now check for users and Store */
1449 if (ia32_get_irn_n_edges(succ) == 1) {
1450 succ = get_edge_src_irn(get_irn_out_edge_first(succ));
1452 if (is_ia32_xStore(succ) || is_ia32_Store(succ)) {
1454 addr_b = get_irn_n(store, 0);
1455 addr_i = get_irn_n(store, 1);
1460 /* we found a Store as single user: Now check for Load */
1462 /* Extra check for commutative ops with two Loads */
1463 /* -> put the interesting Load right */
1464 if (node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_BOTH)) {
1465 if ((addr_b == get_irn_n(get_Proj_pred(left), 0)) &&
1466 (addr_i == get_irn_n(get_Proj_pred(left), 1)))
1468 /* We exchange left and right, so it's easier to kill */
1469 /* the correct Load later and to handle unary operations. */
1470 exchange_left_right(irn, &left, &right, 3, 2);
1471 need_exchange_on_fail ^= 1;
1475 /* skip the Proj for easier access */
1476 load = get_Proj_pred(right);
1478 /* Compare Load and Store address */
1479 if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1480 /* Right Load is from same address, so we can */
1481 /* disconnect the Load and Store here */
1483 /* set new base, index and attributes */
1484 set_irn_n(irn, 0, addr_b);
1485 set_irn_n(irn, 1, addr_i);
1486 add_ia32_am_offs(irn, get_ia32_am_offs(load));
1487 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1488 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1489 set_ia32_op_type(irn, ia32_AddrModeD);
1490 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1491 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1493 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1494 if (is_ia32_am_sc_sign(load))
1495 set_ia32_am_sc_sign(irn);
1497 if (is_ia32_use_frame(load))
1498 set_ia32_use_frame(irn);
1500 /* connect to Load memory and disconnect Load */
1501 if (get_irn_arity(irn) == 5) {
1503 set_irn_n(irn, 4, get_irn_n(load, 2));
1504 set_irn_n(irn, 3, noreg_gp);
1508 set_irn_n(irn, 3, get_irn_n(load, 2));
1509 set_irn_n(irn, 2, noreg_gp);
1512 /* connect the memory Proj of the Store to the op */
1513 mem_proj = get_mem_proj(store);
1514 set_Proj_pred(mem_proj, irn);
1515 set_Proj_proj(mem_proj, 1);
1517 /* clear remat flag */
1518 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1520 DBG_OPT_AM_D(load, store, irn);
1522 DB((mod, LEVEL_1, "merged with %+F and %+F into dest AM\n", load, store));
1524 need_exchange_on_fail = 0;
1527 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1528 /* There was no store, check if we still can optimize for source address mode */
1531 } /* if (support AM Dest) */
1532 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1533 /* op doesn't support am AM Dest -> check for AM Source */
1537 /* was exchanged but optimize failed: exchange back */
1538 if (need_exchange_on_fail) {
1539 exchange_left_right(irn, &left, &right, 3, 2);
1543 need_exchange_on_fail = 0;
1545 /* normalize commutative ops */
1546 if (check_am_src && node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_RIGHT)) {
1548 /* Assure that left operand is always a Load if there is one */
1549 /* because non-commutative ops can only use Source AM if the */
1550 /* left operand is a Load, so we only need to check the left */
1551 /* operand afterwards. */
1553 exchange_left_right(irn, &left, &right, 3, 2);
1554 need_exchange_on_fail = 1;
1556 /* now: load is left */
1557 cand = IA32_AM_CAND_LEFT;
1560 /* optimize op -> Load iff Load is only used by this op */
1561 /* and left operand is a Load which only used by this irn */
1563 (cand & IA32_AM_CAND_LEFT) &&
1564 (ia32_get_irn_n_edges(left) == 1))
1566 left = get_Proj_pred(left);
1568 addr_b = get_irn_n(left, 0);
1569 addr_i = get_irn_n(left, 1);
1571 /* set new base, index and attributes */
1572 set_irn_n(irn, 0, addr_b);
1573 set_irn_n(irn, 1, addr_i);
1574 add_ia32_am_offs(irn, get_ia32_am_offs(left));
1575 set_ia32_am_scale(irn, get_ia32_am_scale(left));
1576 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
1577 set_ia32_op_type(irn, ia32_AddrModeS);
1578 set_ia32_frame_ent(irn, get_ia32_frame_ent(left));
1579 set_ia32_ls_mode(irn, get_ia32_ls_mode(left));
1581 set_ia32_am_sc(irn, get_ia32_am_sc(left));
1582 if (is_ia32_am_sc_sign(left))
1583 set_ia32_am_sc_sign(irn);
1585 /* clear remat flag */
1586 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1588 if (is_ia32_use_frame(left))
1589 set_ia32_use_frame(irn);
1591 /* connect to Load memory */
1592 if (get_irn_arity(irn) == 5) {
1594 set_irn_n(irn, 4, get_irn_n(left, 2));
1596 /* this is only needed for Compares, but currently ALL nodes
1597 * have this attribute :-) */
1598 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1600 /* disconnect from Load */
1601 /* (make second op -> first, set second in to noreg) */
1602 set_irn_n(irn, 2, get_irn_n(irn, 3));
1603 set_irn_n(irn, 3, noreg_gp);
1607 set_irn_n(irn, 3, get_irn_n(left, 2));
1609 /* disconnect from Load */
1610 set_irn_n(irn, 2, noreg_gp);
1613 DBG_OPT_AM_S(left, irn);
1615 /* If Load has a memory Proj, connect it to the op */
1616 mem_proj = get_mem_proj(left);
1618 set_Proj_pred(mem_proj, irn);
1619 set_Proj_proj(mem_proj, 1);
1622 DB((mod, LEVEL_1, "merged with %+F into source AM\n", left));
1625 /* was exchanged but optimize failed: exchange back */
1626 if (need_exchange_on_fail)
1627 exchange_left_right(irn, &left, &right, 3, 2);
1633 * Performs address mode optimization.
1635 void ia32_optimize_addressmode(ia32_code_gen_t *cg) {
1636 /* if we are supposed to do AM or LEA optimization: recalculate edges */
1637 if (cg->opt & (IA32_OPT_DOAM | IA32_OPT_LEA)) {
1638 edges_deactivate(cg->irg);
1639 edges_activate(cg->irg);
1642 /* no optimizations at all */
1646 /* beware: we cannot optimize LEA and AM in one run because */
1647 /* LEA optimization adds new nodes to the irg which */
1648 /* invalidates the phase data */
1650 if (cg->opt & IA32_OPT_LEA) {
1651 irg_walk_blkwise_graph(cg->irg, NULL, optimize_lea, cg);
1654 if (cg->opt & IA32_OPT_DOAM) {
1655 /* we need height information for am optimization */
1656 heights_t *h = heights_new(cg->irg);
1657 ia32_am_opt_env_t env;
1662 irg_walk_blkwise_graph(cg->irg, NULL, optimize_am, &env);