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"
35 #include "ia32_util.h"
38 IA32_AM_CAND_NONE = 0,
39 IA32_AM_CAND_LEFT = 1,
40 IA32_AM_CAND_RIGHT = 2,
45 #define is_NoMem(irn) (get_irn_op(irn) == op_NoMem)
47 typedef int is_op_func_t(const ir_node *n);
48 typedef ir_node *load_func_t(dbg_info *db, ir_graph *irg, ir_node *block, ir_node *base, ir_node *index, ir_node *mem);
51 * checks if a node represents the NOREG value
53 static int be_is_NoReg(ia32_code_gen_t *cg, const ir_node *irn) {
54 be_abi_irg_t *babi = cg->birg->abi;
55 const arch_register_t *fp_noreg = USE_SSE2(cg) ?
56 &ia32_xmm_regs[REG_XMM_NOREG] : &ia32_vfp_regs[REG_VFP_NOREG];
58 return (be_abi_get_callee_save_irn(babi, &ia32_gp_regs[REG_GP_NOREG]) == irn) ||
59 (be_abi_get_callee_save_irn(babi, fp_noreg) == irn);
64 /*************************************************
67 * | | ___ _ __ ___| |_ __ _ _ __ | |_ ___
68 * | | / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
69 * | |___| (_) | | | \__ \ || (_| | | | | |_\__ \
70 * \_____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
72 *************************************************/
75 * creates a unique ident by adding a number to a tag
77 * @param tag the tag string, must contain a %d if a number
80 static ident *unique_id(const char *tag)
82 static unsigned id = 0;
85 snprintf(str, sizeof(str), tag, ++id);
86 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) {
99 dbg_info *dbg = env->dbg;
100 ir_mode *mode = env->mode;
101 ir_graph *irg = env->irg;
102 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) {
186 ir_node *cnst, *load;
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);
207 cnst = new_rd_SymConst(dbg, irg, block, sym, symconst_addr_ent);
208 load = new_r_Load(irg, block, get_irg_no_mem(irg), cnst, mode);
209 load = new_r_Proj(irg, block, load, mode, pn_Load_res);
212 cnst = gen_SymConst(env);
213 set_Load_ptr(get_Proj_pred(load), cnst);
217 cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), get_irn_mode(node));
218 set_ia32_Const_attr(cnst, node);
226 * Transforms (all) Const's into ia32_Const and places them in the
227 * block where they are used (or in the cfg-pred Block in case of Phi's).
228 * Additionally all reference nodes are changed into mode_Is nodes.
230 void ia32_place_consts_set_modes(ir_node *irn, void *env) {
231 ia32_code_gen_t *cg = env;
232 ia32_transform_env_t tenv;
234 ir_node *pred, *cnst;
241 mode = get_irn_mode(irn);
243 /* transform all reference nodes into mode_Is nodes */
244 if (mode_is_reference(mode)) {
246 set_irn_mode(irn, mode);
250 Annotate mode of stored value to link field of the Store
251 as floating point converts might be optimized and we would
254 if (get_irn_opcode(irn) == iro_Store) {
255 set_irn_link(irn, get_irn_mode(get_Store_value(irn)));
258 tenv.block = get_nodes_block(irn);
261 DEBUG_ONLY(tenv.mod = cg->mod;)
263 /* Loop over all predecessors and check for Sym/Const nodes */
264 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
265 pred = get_irn_n(irn, i);
267 opc = get_irn_opcode(pred);
269 tenv.mode = get_irn_mode(pred);
270 tenv.dbg = get_irn_dbg_info(pred);
272 /* If it's a Phi, then we need to create the */
273 /* new Const in it's predecessor block */
275 tenv.block = get_Block_cfgpred_block(get_nodes_block(irn), i);
278 /* put the const into the block where the original const was */
279 if (! (cg->opt & IA32_OPT_PLACECNST)) {
280 tenv.block = get_nodes_block(pred);
285 cnst = gen_Const(&tenv);
288 cnst = gen_SymConst(&tenv);
294 /* if we found a const, then set it */
296 set_irn_n(irn, i, cnst);
303 /********************************************************************************************************
304 * _____ _ _ ____ _ _ _ _ _
305 * | __ \ | | | | / __ \ | | (_) (_) | | (_)
306 * | |__) |__ ___ _ __ | |__ ___ | | ___ | | | |_ __ | |_ _ _ __ ___ _ ______ _| |_ _ ___ _ __
307 * | ___/ _ \/ _ \ '_ \| '_ \ / _ \| |/ _ \ | | | | '_ \| __| | '_ ` _ \| |_ / _` | __| |/ _ \| '_ \
308 * | | | __/ __/ |_) | | | | (_) | | __/ | |__| | |_) | |_| | | | | | | |/ / (_| | |_| | (_) | | | |
309 * |_| \___|\___| .__/|_| |_|\___/|_|\___| \____/| .__/ \__|_|_| |_| |_|_/___\__,_|\__|_|\___/|_| |_|
312 ********************************************************************************************************/
315 * NOTE: THESE PEEPHOLE OPTIMIZATIONS MUST BE CALLED AFTER SCHEDULING AND REGISTER ALLOCATION.
318 static int ia32_cnst_compare(ir_node *n1, ir_node *n2) {
319 return get_ia32_id_cnst(n1) == get_ia32_id_cnst(n2);
323 * Checks for potential CJmp/CJmpAM optimization candidates.
325 static ir_node *ia32_determine_cjmp_cand(ir_node *irn, is_op_func_t *is_op_func) {
326 ir_node *cand = NULL;
327 ir_node *prev = sched_prev(irn);
329 if (is_Block(prev)) {
330 if (get_Block_n_cfgpreds(prev) == 1)
331 prev = get_Block_cfgpred(prev, 0);
336 /* The predecessor must be a ProjX. */
337 if (prev && is_Proj(prev) && get_irn_mode(prev) == mode_X) {
338 prev = get_Proj_pred(prev);
340 if (is_op_func(prev))
347 static int is_TestJmp_cand(const ir_node *irn) {
348 return is_ia32_TestJmp(irn) || is_ia32_And(irn);
352 * Checks if two consecutive arguments of cand matches
353 * the two arguments of irn (TestJmp).
355 static int is_TestJmp_replacement(ir_node *cand, ir_node *irn) {
356 ir_node *in1 = get_irn_n(irn, 0);
357 ir_node *in2 = get_irn_n(irn, 1);
358 int i, n = get_irn_arity(cand);
361 for (i = 0; i < n - 1; i++) {
362 if (get_irn_n(cand, i) == in1 &&
363 get_irn_n(cand, i + 1) == in2)
371 return ia32_cnst_compare(cand, irn);
377 * Tries to replace a TestJmp by a CJmp or CJmpAM (in case of And)
379 static void ia32_optimize_TestJmp(ir_node *irn, ia32_code_gen_t *cg) {
380 ir_node *cand = ia32_determine_cjmp_cand(irn, is_TestJmp_cand);
383 /* we found a possible candidate */
384 replace = cand ? is_TestJmp_replacement(cand, irn) : 0;
387 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
389 if (is_ia32_And(cand))
390 set_irn_op(irn, op_ia32_CJmpAM);
392 set_irn_op(irn, op_ia32_CJmp);
394 DB((cg->mod, LEVEL_1, "%+F\n", irn));
398 static int is_CondJmp_cand(const ir_node *irn) {
399 return is_ia32_CondJmp(irn) || is_ia32_Sub(irn);
403 * Checks if the arguments of cand are the same of irn.
405 static int is_CondJmp_replacement(ir_node *cand, ir_node *irn) {
406 int i, n = get_irn_arity(cand);
409 for (i = 0; i < n; i++) {
410 if (get_irn_n(cand, i) != get_irn_n(irn, i)) {
417 return ia32_cnst_compare(cand, irn);
423 * Tries to replace a CondJmp by a CJmpAM
425 static void ia32_optimize_CondJmp(ir_node *irn, ia32_code_gen_t *cg) {
426 ir_node *cand = ia32_determine_cjmp_cand(irn, is_CondJmp_cand);
429 /* we found a possible candidate */
430 replace = cand ? is_CondJmp_replacement(cand, irn) : 0;
433 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
436 set_irn_op(irn, op_ia32_CJmpAM);
438 DB((cg->mod, LEVEL_1, "%+F\n", irn));
443 * Creates a Push from Store(IncSP(gp_reg_size))
445 static void ia32_create_Push(ir_node *irn, ia32_code_gen_t *cg) {
446 ir_node *sp = get_irn_n(irn, 0);
447 ir_graph *irg = cg->irg;
448 ir_node *val, *next, *push, *bl, *proj_M, *proj_res, *old_proj_M, *mem;
449 const ir_edge_t *edge;
452 /* do not create push if store has already an offset assigned or base is not a IncSP */
453 if (get_ia32_am_offs(irn) || ! be_is_IncSP(sp))
456 /* do not create push if index is not NOREG */
457 if (arch_get_irn_register(cg->arch_env, get_irn_n(irn, 1)) !=
458 &ia32_gp_regs[REG_GP_NOREG])
461 /* do not create push for floating point */
462 val = get_irn_n(irn, 2);
463 if (mode_is_float(get_irn_mode(val)))
466 /* do not create push if IncSp doesn't expand stack or expand size is different from register size */
467 if (be_get_IncSP_direction(sp) != be_stack_dir_expand ||
468 be_get_IncSP_offset(sp) != get_mode_size_bytes(ia32_reg_classes[CLASS_ia32_gp].mode))
471 /* do not create push, if there is a path (inside the block) from the push value to IncSP */
472 h = heights_new(cg->irg);
473 if (get_nodes_block(val) == get_nodes_block(sp) &&
474 heights_reachable_in_block(h, val, sp))
481 /* ok, translate into Push */
482 edge = get_irn_out_edge_first(irn);
483 old_proj_M = get_edge_src_irn(edge);
484 bl = get_nodes_block(irn);
486 next = sched_next(irn);
492 if the IncSP points to NoMem -> just use the memory input from store
493 if IncSP points to somewhere else -> sync memory of IncSP and Store
495 mem = be_get_IncSP_mem(sp);
496 if (mem == get_irg_no_mem(irg))
497 mem = get_irn_n(irn, 3);
502 in[1] = get_irn_n(irn, 3);
503 mem = new_r_Sync(irg, bl, 2, in);
506 push = new_rd_ia32_Push(NULL, irg, bl, be_get_IncSP_pred(sp), val, mem);
507 proj_res = new_r_Proj(irg, bl, push, get_irn_mode(sp), pn_ia32_Push_stack);
508 proj_M = new_r_Proj(irg, bl, push, mode_M, pn_ia32_Push_M);
510 /* copy a possible constant from the store */
511 set_ia32_id_cnst(push, get_ia32_id_cnst(irn));
512 set_ia32_immop_type(push, get_ia32_immop_type(irn));
514 /* the push must have SP out register */
515 arch_set_irn_register(cg->arch_env, push, arch_get_irn_register(cg->arch_env, sp));
517 exchange(old_proj_M, proj_M);
518 exchange(sp, proj_res);
519 sched_add_before(next, push);
520 sched_add_after(push, proj_res);
524 * Creates a Pop from IncSP(Load(sp))
526 static void ia32_create_Pop(ir_node *irn, ia32_code_gen_t *cg) {
527 ir_node *old_proj_M = be_get_IncSP_mem(irn);
528 ir_node *load = skip_Proj(old_proj_M);
529 ir_node *old_proj_res = NULL;
530 ir_node *bl, *pop, *next, *proj_res, *proj_sp, *proj_M;
531 const ir_edge_t *edge;
532 const arch_register_t *reg, *sp;
534 if (! is_ia32_Load(load) || get_ia32_am_offs(load))
537 if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 1)) !=
538 &ia32_gp_regs[REG_GP_NOREG])
540 if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 0)) != cg->isa->arch_isa.sp)
543 /* ok, translate into pop */
544 foreach_out_edge(load, edge) {
545 ir_node *succ = get_edge_src_irn(edge);
546 if (succ != old_proj_M) {
551 if (! old_proj_res) {
553 return; /* should not happen */
556 bl = get_nodes_block(load);
558 /* IncSP is typically scheduled after the load, so remove it first */
560 next = sched_next(old_proj_res);
561 sched_remove(old_proj_res);
564 reg = arch_get_irn_register(cg->arch_env, load);
565 sp = arch_get_irn_register(cg->arch_env, irn);
567 pop = new_rd_ia32_Pop(NULL, current_ir_graph, bl, get_irn_n(irn, 0), get_irn_n(load, 2));
568 proj_res = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(old_proj_res), pn_ia32_Pop_res);
569 proj_sp = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(irn), pn_ia32_Pop_stack);
570 proj_M = new_r_Proj(current_ir_graph, bl, pop, mode_M, pn_ia32_Pop_M);
572 exchange(old_proj_M, proj_M);
573 exchange(old_proj_res, proj_res);
574 exchange(irn, proj_sp);
576 arch_set_irn_register(cg->arch_env, proj_res, reg);
577 arch_set_irn_register(cg->arch_env, proj_sp, sp);
579 sched_add_before(next, proj_sp);
580 sched_add_before(proj_sp, proj_res);
581 sched_add_before(proj_res,pop);
585 * Tries to optimize two following IncSP.
587 static void ia32_optimize_IncSP(ir_node *irn, ia32_code_gen_t *cg) {
588 ir_node *prev = be_get_IncSP_pred(irn);
589 int real_uses = get_irn_n_edges(prev);
591 if (be_is_IncSP(prev) && real_uses == 1) {
592 /* first IncSP has only one IncSP user, kill the first one */
593 unsigned prev_offs = be_get_IncSP_offset(prev);
594 be_stack_dir_t prev_dir = be_get_IncSP_direction(prev);
595 unsigned curr_offs = be_get_IncSP_offset(irn);
596 be_stack_dir_t curr_dir = be_get_IncSP_direction(irn);
598 int new_ofs = prev_offs * (prev_dir == be_stack_dir_expand ? -1 : +1) +
599 curr_offs * (curr_dir == be_stack_dir_expand ? -1 : +1);
603 curr_dir = be_stack_dir_expand;
606 curr_dir = be_stack_dir_shrink;
607 be_set_IncSP_offset(prev, 0);
608 be_set_IncSP_offset(irn, (unsigned)new_ofs);
609 be_set_IncSP_direction(irn, curr_dir);
611 /* Omit the optimized IncSP */
612 be_set_IncSP_pred(irn, be_get_IncSP_pred(prev));
617 * Performs Peephole Optimizations.
619 void ia32_peephole_optimization(ir_node *irn, void *env) {
620 ia32_code_gen_t *cg = env;
622 /* AMD CPUs want explicit compare before conditional jump */
623 if (! ARCH_AMD(cg->opt_arch)) {
624 if (is_ia32_TestJmp(irn))
625 ia32_optimize_TestJmp(irn, cg);
626 else if (is_ia32_CondJmp(irn))
627 ia32_optimize_CondJmp(irn, cg);
629 /* seems to be buggy when using Pushes */
630 // else if (be_is_IncSP(irn))
631 // ia32_optimize_IncSP(irn, cg);
632 else if (is_ia32_Store(irn))
633 ia32_create_Push(irn, cg);
638 /******************************************************************
640 * /\ | | | | | \/ | | |
641 * / \ __| | __| |_ __ ___ ___ ___| \ / | ___ __| | ___
642 * / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
643 * / ____ \ (_| | (_| | | | __/\__ \__ \ | | | (_) | (_| | __/
644 * /_/ \_\__,_|\__,_|_| \___||___/___/_| |_|\___/ \__,_|\___|
646 ******************************************************************/
653 static int node_is_ia32_comm(const ir_node *irn) {
654 return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
657 static int ia32_get_irn_n_edges(const ir_node *irn) {
658 const ir_edge_t *edge;
661 foreach_out_edge(irn, edge) {
669 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
671 * @param pred The node to be checked
672 * @param is_op_func The check-function
673 * @return 1 if conditions are fulfilled, 0 otherwise
675 static int pred_is_specific_node(const ir_node *pred, is_op_func_t *is_op_func) {
676 if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
684 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
685 * and if the predecessor is in block bl.
687 * @param bl The block
688 * @param pred The node to be checked
689 * @param is_op_func The check-function
690 * @return 1 if conditions are fulfilled, 0 otherwise
692 static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
693 int (*is_op_func)(const ir_node *n))
696 pred = get_Proj_pred(pred);
697 if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
706 * Checks if irn is a candidate for address calculation.
708 * - none of the operand must be a Load within the same block OR
709 * - all Loads must have more than one user OR
710 * - the irn has a frame entity (it's a former FrameAddr)
712 * @param block The block the Loads must/mustnot be in
713 * @param irn The irn to check
714 * return 1 if irn is a candidate, 0 otherwise
716 static int is_addr_candidate(const ir_node *block, const ir_node *irn) {
717 ir_node *in, *left, *right;
720 left = get_irn_n(irn, 2);
721 right = get_irn_n(irn, 3);
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 */
732 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
733 n = ia32_get_irn_n_edges(in);
734 is_cand = (n == 1) ? 0 : is_cand; /* load with only one user: don't create LEA */
737 is_cand = get_ia32_frame_ent(irn) ? 1 : is_cand;
743 * Checks if irn is a candidate for address mode.
746 * - at least one operand has to be a Load within the same block AND
747 * - the load must not have other users than the irn AND
748 * - the irn must not have a frame entity set
750 * @param cg The ia32 code generator
751 * @param h The height information of the irg
752 * @param block The block the Loads must/mustnot be in
753 * @param irn The irn to check
754 * return 0 if irn is no candidate, 1 if left load can be used, 2 if right one, 3 for both
756 static ia32_am_cand_t is_am_candidate(ia32_code_gen_t *cg, heights_t *h, const ir_node *block, ir_node *irn) {
757 ir_node *in, *load, *other, *left, *right;
758 int n, is_cand = 0, cand;
760 if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn) || is_ia32_vfild(irn) || is_ia32_vfist(irn))
763 left = get_irn_n(irn, 2);
764 right = get_irn_n(irn, 3);
768 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
769 n = ia32_get_irn_n_edges(in);
770 is_cand = (n == 1) ? 1 : is_cand; /* load with more than one user: no AM */
772 load = get_Proj_pred(in);
775 /* If there is a data dependency of other irn from load: cannot use AM */
776 if (get_nodes_block(other) == block) {
777 other = skip_Proj(other);
778 is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
779 /* this could happen in loops */
780 is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
784 cand = is_cand ? IA32_AM_CAND_LEFT : IA32_AM_CAND_NONE;
788 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
789 n = ia32_get_irn_n_edges(in);
790 is_cand = (n == 1) ? 1 : is_cand; /* load with more than one user: no AM */
792 load = get_Proj_pred(in);
795 /* If there is a data dependency of other irn from load: cannot use load */
796 if (get_nodes_block(other) == block) {
797 other = skip_Proj(other);
798 is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
799 /* this could happen in loops */
800 is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
804 cand = is_cand ? (cand | IA32_AM_CAND_RIGHT) : cand;
806 /* check some special cases */
807 if (USE_SSE2(cg) && is_ia32_Conv_I2FP(irn)) {
808 /* SSE Conv I -> FP cvtsi2s(s|d) can only load 32 bit values */
809 if (get_mode_size_bits(get_ia32_tgt_mode(irn)) != 32)
810 cand = IA32_AM_CAND_NONE;
812 else if (is_ia32_Conv_I2I(irn)) {
813 /* we cannot load an N bit value and implicitly convert it into an M bit value if N > M */
814 if (get_mode_size_bits(get_ia32_src_mode(irn)) > get_mode_size_bits(get_ia32_tgt_mode(irn)))
815 cand = IA32_AM_CAND_NONE;
818 /* if the irn has a frame entity: we do not use address mode */
819 return get_ia32_frame_ent(irn) ? IA32_AM_CAND_NONE : cand;
823 * Compares the base and index addr and the load/store entities
824 * and returns 1 if they are equal.
826 static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
827 const ir_node *addr_b, const ir_node *addr_i)
829 int is_equal = (addr_b == get_irn_n(load, 0)) && (addr_i == get_irn_n(load, 1));
830 entity *lent = get_ia32_frame_ent(load);
831 entity *sent = get_ia32_frame_ent(store);
832 ident *lid = get_ia32_am_sc(load);
833 ident *sid = get_ia32_am_sc(store);
834 char *loffs = get_ia32_am_offs(load);
835 char *soffs = get_ia32_am_offs(store);
837 /* are both entities set and equal? */
838 if (is_equal && (lent || sent))
839 is_equal = lent && sent && (lent == sent);
841 /* are address mode idents set and equal? */
842 if (is_equal && (lid || sid))
843 is_equal = lid && sid && (lid == sid);
845 /* are offsets set and equal */
846 if (is_equal && (loffs || soffs))
847 is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;
849 /* are the load and the store of the same mode? */
850 is_equal = is_equal ? get_ia32_ls_mode(load) == get_ia32_ls_mode(store) : 0;
855 typedef enum _ia32_take_lea_attr {
856 IA32_LEA_ATTR_NONE = 0,
857 IA32_LEA_ATTR_BASE = (1 << 0),
858 IA32_LEA_ATTR_INDEX = (1 << 1),
859 IA32_LEA_ATTR_OFFS = (1 << 2),
860 IA32_LEA_ATTR_SCALE = (1 << 3),
861 IA32_LEA_ATTR_AMSC = (1 << 4),
862 IA32_LEA_ATTR_FENT = (1 << 5)
863 } ia32_take_lea_attr;
866 * Decides if we have to keep the LEA operand or if we can assimilate it.
868 static int do_new_lea(ir_node *irn, ir_node *base, ir_node *index, ir_node *lea,
869 int have_am_sc, ia32_code_gen_t *cg)
871 ir_node *lea_base = get_irn_n(lea, 0);
872 ir_node *lea_idx = get_irn_n(lea, 1);
873 entity *irn_ent = get_ia32_frame_ent(irn);
874 entity *lea_ent = get_ia32_frame_ent(lea);
876 int is_noreg_base = be_is_NoReg(cg, base);
877 int is_noreg_index = be_is_NoReg(cg, index);
878 ia32_am_flavour_t am_flav = get_ia32_am_flavour(lea);
880 /* If the Add and the LEA both have a different frame entity set: keep */
881 if (irn_ent && lea_ent && (irn_ent != lea_ent))
882 return IA32_LEA_ATTR_NONE;
883 else if (! irn_ent && lea_ent)
884 ret_val |= IA32_LEA_ATTR_FENT;
886 /* If the Add and the LEA both have already an address mode symconst: keep */
887 if (have_am_sc && get_ia32_am_sc(lea))
888 return IA32_LEA_ATTR_NONE;
889 else if (get_ia32_am_sc(lea))
890 ret_val |= IA32_LEA_ATTR_AMSC;
892 /* Check the different base-index combinations */
894 if (! is_noreg_base && ! is_noreg_index) {
895 /* Assimilate if base is the lea and the LEA is just a Base + Offset calculation */
896 if ((base == lea) && ! (am_flav & ia32_I ? 1 : 0)) {
897 if (am_flav & ia32_O)
898 ret_val |= IA32_LEA_ATTR_OFFS;
900 ret_val |= IA32_LEA_ATTR_BASE;
903 return IA32_LEA_ATTR_NONE;
905 else if (! is_noreg_base && is_noreg_index) {
906 /* Base is set but index not */
908 /* Base points to LEA: assimilate everything */
909 if (am_flav & ia32_O)
910 ret_val |= IA32_LEA_ATTR_OFFS;
911 if (am_flav & ia32_S)
912 ret_val |= IA32_LEA_ATTR_SCALE;
913 if (am_flav & ia32_I)
914 ret_val |= IA32_LEA_ATTR_INDEX;
916 ret_val |= IA32_LEA_ATTR_BASE;
918 else if (am_flav & ia32_B ? 0 : 1) {
919 /* Base is not the LEA but the LEA is an index only calculation: assimilate */
920 if (am_flav & ia32_O)
921 ret_val |= IA32_LEA_ATTR_OFFS;
922 if (am_flav & ia32_S)
923 ret_val |= IA32_LEA_ATTR_SCALE;
925 ret_val |= IA32_LEA_ATTR_INDEX;
928 return IA32_LEA_ATTR_NONE;
930 else if (is_noreg_base && ! is_noreg_index) {
931 /* Index is set but not base */
933 /* Index points to LEA: assimilate everything */
934 if (am_flav & ia32_O)
935 ret_val |= IA32_LEA_ATTR_OFFS;
936 if (am_flav & ia32_S)
937 ret_val |= IA32_LEA_ATTR_SCALE;
938 if (am_flav & ia32_B)
939 ret_val |= IA32_LEA_ATTR_BASE;
941 ret_val |= IA32_LEA_ATTR_INDEX;
943 else if (am_flav & ia32_I ? 0 : 1) {
944 /* Index is not the LEA but the LEA is a base only calculation: assimilate */
945 if (am_flav & ia32_O)
946 ret_val |= IA32_LEA_ATTR_OFFS;
947 if (am_flav & ia32_S)
948 ret_val |= IA32_LEA_ATTR_SCALE;
950 ret_val |= IA32_LEA_ATTR_BASE;
953 return IA32_LEA_ATTR_NONE;
956 assert(0 && "There must have been set base or index");
964 * Folds Add or Sub to LEA if possible
966 static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn, ir_node *noreg) {
967 ir_graph *irg = get_irn_irg(irn);
968 dbg_info *dbg = get_irn_dbg_info(irn);
969 ir_node *block = get_nodes_block(irn);
971 ir_node *shift = NULL;
972 ir_node *lea_o = NULL;
975 const char *offs_cnst = NULL;
976 char *offs_lea = NULL;
983 entity *lea_ent = NULL;
984 ir_node *left, *right, *temp;
985 ir_node *base, *index;
986 ia32_am_flavour_t am_flav;
987 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
989 if (is_ia32_Add(irn))
992 left = get_irn_n(irn, 2);
993 right = get_irn_n(irn, 3);
995 /* "normalize" arguments in case of add with two operands */
996 if (isadd && ! be_is_NoReg(cg, right)) {
997 /* put LEA == ia32_am_O as right operand */
998 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
999 set_irn_n(irn, 2, right);
1000 set_irn_n(irn, 3, left);
1006 /* put LEA != ia32_am_O as left operand */
1007 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
1008 set_irn_n(irn, 2, right);
1009 set_irn_n(irn, 3, left);
1015 /* put SHL as left operand iff left is NOT a LEA */
1016 if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
1017 set_irn_n(irn, 2, right);
1018 set_irn_n(irn, 3, left);
1031 /* check for operation with immediate */
1032 if (is_ia32_ImmConst(irn)) {
1033 DBG((mod, LEVEL_1, "\tfound op with imm const"));
1035 offs_cnst = get_ia32_cnst(irn);
1038 else if (is_ia32_ImmSymConst(irn)) {
1039 DBG((mod, LEVEL_1, "\tfound op with imm symconst"));
1043 am_sc = get_ia32_id_cnst(irn);
1044 am_sc_sign = is_ia32_am_sc_sign(irn);
1047 /* determine the operand which needs to be checked */
1048 temp = be_is_NoReg(cg, right) ? left : right;
1050 /* check if right operand is AMConst (LEA with ia32_am_O) */
1051 /* but we can only eat it up if there is no other symconst */
1052 /* because the linker won't accept two symconsts */
1053 if (! have_am_sc && is_ia32_Lea(temp) && get_ia32_am_flavour(temp) == ia32_am_O) {
1054 DBG((mod, LEVEL_1, "\tgot op with LEA am_O"));
1056 offs_lea = get_ia32_am_offs(temp);
1057 am_sc = get_ia32_am_sc(temp);
1058 am_sc_sign = is_ia32_am_sc_sign(temp);
1065 else if (temp == right)
1070 /* default for add -> make right operand to index */
1074 DBG((mod, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
1076 /* determine the operand which needs to be checked */
1078 if (is_ia32_Lea(left)) {
1082 /* check for SHL 1,2,3 */
1083 if (pred_is_specific_node(temp, is_ia32_Shl)) {
1084 temp = get_Proj_pred(temp);
1087 if (get_ia32_Immop_tarval(temp)) {
1088 scale = get_tarval_long(get_ia32_Immop_tarval(temp));
1091 index = get_irn_n(temp, 2);
1093 DBG((mod, LEVEL_1, "\tgot scaled index %+F\n", index));
1103 if (! be_is_NoReg(cg, index)) {
1104 /* if we have index, but left == right -> no base */
1105 if (left == right) {
1108 else if (! is_ia32_Lea(left) && (index != right)) {
1109 /* index != right -> we found a good Shl */
1110 /* left != LEA -> this Shl was the left operand */
1111 /* -> base is right operand */
1112 base = (right == lea_o) ? noreg : right;
1117 /* Try to assimilate a LEA as left operand */
1118 if (is_ia32_Lea(left) && (get_ia32_am_flavour(left) != ia32_am_O)) {
1119 /* check if we can assimilate the LEA */
1120 int take_attr = do_new_lea(irn, base, index, left, have_am_sc, cg);
1122 if (take_attr == IA32_LEA_ATTR_NONE) {
1123 DBG((mod, LEVEL_1, "\tleave old LEA, creating new one\n"));
1126 DBG((mod, LEVEL_1, "\tgot LEA as left operand ... assimilating\n"));
1127 lea = left; /* for statistics */
1129 if (take_attr & IA32_LEA_ATTR_OFFS)
1130 offs = get_ia32_am_offs(left);
1132 if (take_attr & IA32_LEA_ATTR_AMSC) {
1133 am_sc = get_ia32_am_sc(left);
1135 am_sc_sign = is_ia32_am_sc_sign(left);
1138 if (take_attr & IA32_LEA_ATTR_SCALE)
1139 scale = get_ia32_am_scale(left);
1141 if (take_attr & IA32_LEA_ATTR_BASE)
1142 base = get_irn_n(left, 0);
1144 if (take_attr & IA32_LEA_ATTR_INDEX)
1145 index = get_irn_n(left, 1);
1147 if (take_attr & IA32_LEA_ATTR_FENT)
1148 lea_ent = get_ia32_frame_ent(left);
1152 /* ok, we can create a new LEA */
1154 res = new_rd_ia32_Lea(dbg, irg, block, base, index, mode_Is);
1156 /* add the old offset of a previous LEA */
1158 add_ia32_am_offs(res, offs);
1161 /* add the new offset */
1164 add_ia32_am_offs(res, offs_cnst);
1167 add_ia32_am_offs(res, offs_lea);
1171 /* either lea_O-cnst, -cnst or -lea_O */
1174 add_ia32_am_offs(res, offs_lea);
1177 sub_ia32_am_offs(res, offs_cnst);
1180 sub_ia32_am_offs(res, offs_lea);
1184 /* set the address mode symconst */
1186 set_ia32_am_sc(res, am_sc);
1188 set_ia32_am_sc_sign(res);
1191 /* copy the frame entity (could be set in case of Add */
1192 /* which was a FrameAddr) */
1194 set_ia32_frame_ent(res, lea_ent);
1196 set_ia32_frame_ent(res, get_ia32_frame_ent(irn));
1198 if (get_ia32_frame_ent(res))
1199 set_ia32_use_frame(res);
1202 set_ia32_am_scale(res, scale);
1204 am_flav = ia32_am_N;
1205 /* determine new am flavour */
1206 if (offs || offs_cnst || offs_lea || have_am_sc) {
1209 if (! be_is_NoReg(cg, base)) {
1212 if (! be_is_NoReg(cg, index)) {
1218 set_ia32_am_flavour(res, am_flav);
1220 set_ia32_op_type(res, ia32_AddrModeS);
1222 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
1224 DBG((mod, LEVEL_1, "\tLEA [%+F + %+F * %d + %s]\n", base, index, scale, get_ia32_am_offs(res)));
1226 /* we will exchange it, report here before the Proj is created */
1227 if (shift && lea && lea_o)
1228 DBG_OPT_LEA4(irn, lea_o, lea, shift, res);
1229 else if (shift && lea)
1230 DBG_OPT_LEA3(irn, lea, shift, res);
1231 else if (shift && lea_o)
1232 DBG_OPT_LEA3(irn, lea_o, shift, res);
1233 else if (lea && lea_o)
1234 DBG_OPT_LEA3(irn, lea_o, lea, res);
1236 DBG_OPT_LEA2(irn, shift, res);
1238 DBG_OPT_LEA2(irn, lea, res);
1240 DBG_OPT_LEA2(irn, lea_o, res);
1242 DBG_OPT_LEA1(irn, res);
1244 /* get the result Proj of the Add/Sub */
1245 irn = ia32_get_res_proj(irn);
1247 assert(irn && "Couldn't find result proj");
1249 /* exchange the old op with the new LEA */
1258 * Merges a Load/Store node with a LEA.
1259 * @param irn The Load/Store node
1260 * @param lea The LEA
1262 static void merge_loadstore_lea(ir_node *irn, ir_node *lea) {
1263 entity *irn_ent = get_ia32_frame_ent(irn);
1264 entity *lea_ent = get_ia32_frame_ent(lea);
1266 /* If the irn and the LEA both have a different frame entity set: do not merge */
1267 if (irn_ent && lea_ent && (irn_ent != lea_ent))
1269 else if (! irn_ent && lea_ent) {
1270 set_ia32_frame_ent(irn, lea_ent);
1271 set_ia32_use_frame(irn);
1274 /* get the AM attributes from the LEA */
1275 add_ia32_am_offs(irn, get_ia32_am_offs(lea));
1276 set_ia32_am_scale(irn, get_ia32_am_scale(lea));
1277 set_ia32_am_flavour(irn, get_ia32_am_flavour(lea));
1279 set_ia32_am_sc(irn, get_ia32_am_sc(lea));
1280 if (is_ia32_am_sc_sign(lea))
1281 set_ia32_am_sc_sign(irn);
1283 set_ia32_op_type(irn, is_ia32_Ld(irn) ? ia32_AddrModeS : ia32_AddrModeD);
1285 /* set base and index */
1286 set_irn_n(irn, 0, get_irn_n(lea, 0));
1287 set_irn_n(irn, 1, get_irn_n(lea, 1));
1289 /* clear remat flag */
1290 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1292 if (is_ia32_Ld(irn))
1293 DBG_OPT_LOAD_LEA(lea, irn);
1295 DBG_OPT_STORE_LEA(lea, irn);
1300 * Sets new_right index of irn to right and new_left index to left.
1301 * Also exchange left and right
1303 static void exchange_left_right(ir_node *irn, ir_node **left, ir_node **right, int new_left, int new_right) {
1306 set_irn_n(irn, new_right, *right);
1307 set_irn_n(irn, new_left, *left);
1313 /* this is only needed for Compares, but currently ALL nodes
1314 * have this attribute :-) */
1315 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1319 * Performs address calculation optimization (create LEAs if possible)
1321 static void optimize_lea(ir_node *irn, void *env) {
1322 ia32_code_gen_t *cg = env;
1323 ir_node *block, *noreg_gp, *left, *right;
1325 if (! is_ia32_irn(irn))
1328 /* Following cases can occur: */
1329 /* - Sub (l, imm) -> LEA [base - offset] */
1330 /* - Sub (l, r == LEA with ia32_am_O) -> LEA [base - offset] */
1331 /* - Add (l, imm) -> LEA [base + offset] */
1332 /* - Add (l, r == LEA with ia32_am_O) -> LEA [base + offset] */
1333 /* - Add (l == LEA with ia32_am_O, r) -> LEA [base + offset] */
1334 /* - Add (l, r) -> LEA [base + index * scale] */
1335 /* with scale > 1 iff l/r == shl (1,2,3) */
1337 if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
1338 left = get_irn_n(irn, 2);
1339 right = get_irn_n(irn, 3);
1340 block = get_nodes_block(irn);
1341 noreg_gp = ia32_new_NoReg_gp(cg);
1343 /* Do not try to create a LEA if one of the operands is a Load. */
1344 /* check is irn is a candidate for address calculation */
1345 if (is_addr_candidate(block, irn)) {
1348 DBG((cg->mod, LEVEL_1, "\tfound address calculation candidate %+F ... ", irn));
1349 res = fold_addr(cg, irn, noreg_gp);
1352 DB((cg->mod, LEVEL_1, "transformed into %+F\n", res));
1354 DB((cg->mod, LEVEL_1, "not transformed\n"));
1357 else if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn)) {
1358 /* - Load -> LEA into Load } TODO: If the LEA is used by more than one Load/Store */
1359 /* - Store -> LEA into Store } it might be better to keep the LEA */
1360 left = get_irn_n(irn, 0);
1362 if (is_ia32_Lea(left)) {
1363 const ir_edge_t *edge, *ne;
1366 /* merge all Loads/Stores connected to this LEA with the LEA */
1367 foreach_out_edge_safe(left, edge, ne) {
1368 src = get_edge_src_irn(edge);
1370 if (src && (get_edge_src_pos(edge) == 0) && (is_ia32_Ld(src) || is_ia32_St(src) || is_ia32_Store8Bit(src))) {
1371 DBG((cg->mod, LEVEL_1, "\nmerging %+F into %+F\n", left, irn));
1372 if (! is_ia32_got_lea(src))
1373 merge_loadstore_lea(src, left);
1374 set_ia32_got_lea(src);
1383 * Checks for address mode patterns and performs the
1384 * necessary transformations.
1385 * This function is called by a walker.
1387 static void optimize_am(ir_node *irn, void *env) {
1388 ia32_am_opt_env_t *am_opt_env = env;
1389 ia32_code_gen_t *cg = am_opt_env->cg;
1390 heights_t *h = am_opt_env->h;
1391 ir_node *block, *noreg_gp, *noreg_fp;
1392 ir_node *left, *right;
1393 ir_node *store, *load, *mem_proj;
1394 ir_node *succ, *addr_b, *addr_i;
1395 int check_am_src = 0;
1396 int need_exchange_on_fail = 0;
1397 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
1399 if (! is_ia32_irn(irn) || is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn))
1402 block = get_nodes_block(irn);
1403 noreg_gp = ia32_new_NoReg_gp(cg);
1404 noreg_fp = ia32_new_NoReg_fp(cg);
1406 DBG((mod, LEVEL_1, "checking for AM\n"));
1408 /* fold following patterns: */
1409 /* - op -> Load into AMop with am_Source */
1411 /* - op is am_Source capable AND */
1412 /* - the Load is only used by this op AND */
1413 /* - the Load is in the same block */
1414 /* - Store -> op -> Load into AMop with am_Dest */
1416 /* - op is am_Dest capable AND */
1417 /* - the Store uses the same address as the Load AND */
1418 /* - the Load is only used by this op AND */
1419 /* - the Load and Store are in the same block AND */
1420 /* - nobody else uses the result of the op */
1422 if ((get_ia32_am_support(irn) != ia32_am_None) && ! is_ia32_Lea(irn)) {
1423 ia32_am_cand_t cand = is_am_candidate(cg, h, block, irn);
1424 ia32_am_cand_t orig_cand = cand;
1426 /* cand == 1: load is left; cand == 2: load is right; */
1428 if (cand == IA32_AM_CAND_NONE)
1431 DBG((mod, LEVEL_1, "\tfound address mode candidate %+F ... ", irn));
1433 left = get_irn_n(irn, 2);
1434 if (get_irn_arity(irn) == 4) {
1435 /* it's an "unary" operation */
1439 right = get_irn_n(irn, 3);
1442 /* normalize commutative ops */
1443 if (node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_RIGHT)) {
1445 /* Assure that left operand is always a Load if there is one */
1446 /* because non-commutative ops can only use Dest AM if the left */
1447 /* operand is a load, so we only need to check left operand. */
1449 exchange_left_right(irn, &left, &right, 3, 2);
1450 need_exchange_on_fail = 1;
1452 /* now: load is right */
1453 cand = IA32_AM_CAND_LEFT;
1456 /* check for Store -> op -> Load */
1458 /* Store -> op -> Load optimization is only possible if supported by op */
1459 /* and if right operand is a Load */
1460 if ((get_ia32_am_support(irn) & ia32_am_Dest) && (cand & IA32_AM_CAND_LEFT))
1462 /* An address mode capable op always has a result Proj. */
1463 /* If this Proj is used by more than one other node, we don't need to */
1464 /* check further, otherwise we check for Store and remember the address, */
1465 /* the Store points to. */
1467 succ = ia32_get_res_proj(irn);
1468 assert(succ && "Couldn't find result proj");
1474 /* now check for users and Store */
1475 if (ia32_get_irn_n_edges(succ) == 1) {
1476 succ = get_edge_src_irn(get_irn_out_edge_first(succ));
1478 if (is_ia32_xStore(succ) || is_ia32_Store(succ)) {
1480 addr_b = get_irn_n(store, 0);
1481 addr_i = get_irn_n(store, 1);
1486 /* we found a Store as single user: Now check for Load */
1488 /* skip the Proj for easier access */
1489 load = is_Proj(right) ? (is_ia32_Load(get_Proj_pred(right)) ? get_Proj_pred(right) : NULL) : NULL;
1491 /* Extra check for commutative ops with two Loads */
1492 /* -> put the interesting Load left */
1493 if (load && node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_BOTH)) {
1494 if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1495 /* We exchange left and right, so it's easier to kill */
1496 /* the correct Load later and to handle unary operations. */
1497 exchange_left_right(irn, &left, &right, 3, 2);
1498 need_exchange_on_fail ^= 1;
1502 /* skip the Proj for easier access */
1503 load = get_Proj_pred(left);
1505 /* Compare Load and Store address */
1506 if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1507 /* Left Load is from same address, so we can */
1508 /* disconnect the Load and Store here */
1510 /* set new base, index and attributes */
1511 set_irn_n(irn, 0, addr_b);
1512 set_irn_n(irn, 1, addr_i);
1513 add_ia32_am_offs(irn, get_ia32_am_offs(load));
1514 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1515 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1516 set_ia32_op_type(irn, ia32_AddrModeD);
1517 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1518 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1520 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1521 if (is_ia32_am_sc_sign(load))
1522 set_ia32_am_sc_sign(irn);
1524 if (is_ia32_use_frame(load))
1525 set_ia32_use_frame(irn);
1527 /* connect to Load memory and disconnect Load */
1528 if (get_irn_arity(irn) == 5) {
1530 set_irn_n(irn, 4, get_irn_n(load, 2));
1531 set_irn_n(irn, 2, noreg_gp);
1535 set_irn_n(irn, 3, get_irn_n(load, 2));
1536 set_irn_n(irn, 2, noreg_gp);
1539 /* connect the memory Proj of the Store to the op */
1540 mem_proj = ia32_get_proj_for_mode(store, mode_M);
1541 set_Proj_pred(mem_proj, irn);
1542 set_Proj_proj(mem_proj, 1);
1544 /* clear remat flag */
1545 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1547 DBG_OPT_AM_D(load, store, irn);
1549 DB((mod, LEVEL_1, "merged with %+F and %+F into dest AM\n", load, store));
1551 need_exchange_on_fail = 0;
1554 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1555 /* There was no store, check if we still can optimize for source address mode */
1558 } /* if (support AM Dest) */
1559 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1560 /* op doesn't support am AM Dest -> check for AM Source */
1564 /* was exchanged but optimize failed: exchange back */
1565 if (need_exchange_on_fail) {
1566 exchange_left_right(irn, &left, &right, 3, 2);
1570 need_exchange_on_fail = 0;
1572 /* normalize commutative ops */
1573 if (check_am_src && node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_LEFT)) {
1575 /* Assure that right operand is always a Load if there is one */
1576 /* because non-commutative ops can only use Source AM if the */
1577 /* right operand is a Load, so we only need to check the right */
1578 /* operand afterwards. */
1580 exchange_left_right(irn, &left, &right, 3, 2);
1581 need_exchange_on_fail = 1;
1583 /* now: load is left */
1584 cand = IA32_AM_CAND_RIGHT;
1587 /* optimize op -> Load iff Load is only used by this op */
1588 /* and right operand is a Load which only used by this irn */
1590 (cand & IA32_AM_CAND_RIGHT) &&
1591 (get_irn_arity(irn) == 5) &&
1592 (ia32_get_irn_n_edges(right) == 1))
1594 right = get_Proj_pred(right);
1596 addr_b = get_irn_n(right, 0);
1597 addr_i = get_irn_n(right, 1);
1599 /* set new base, index and attributes */
1600 set_irn_n(irn, 0, addr_b);
1601 set_irn_n(irn, 1, addr_i);
1602 add_ia32_am_offs(irn, get_ia32_am_offs(right));
1603 set_ia32_am_scale(irn, get_ia32_am_scale(right));
1604 set_ia32_am_flavour(irn, get_ia32_am_flavour(right));
1605 set_ia32_op_type(irn, ia32_AddrModeS);
1606 set_ia32_frame_ent(irn, get_ia32_frame_ent(right));
1607 set_ia32_ls_mode(irn, get_ia32_ls_mode(right));
1609 set_ia32_am_sc(irn, get_ia32_am_sc(right));
1610 if (is_ia32_am_sc_sign(right))
1611 set_ia32_am_sc_sign(irn);
1613 /* clear remat flag */
1614 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1616 if (is_ia32_use_frame(right))
1617 set_ia32_use_frame(irn);
1619 /* connect to Load memory */
1620 set_irn_n(irn, 4, get_irn_n(right, 2));
1622 /* this is only needed for Compares, but currently ALL nodes
1623 * have this attribute :-) */
1624 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1626 /* disconnect from Load */
1627 set_irn_n(irn, 3, noreg_gp);
1629 DBG_OPT_AM_S(right, irn);
1631 /* If Load has a memory Proj, connect it to the op */
1632 mem_proj = ia32_get_proj_for_mode(right, mode_M);
1634 set_Proj_pred(mem_proj, irn);
1635 set_Proj_proj(mem_proj, 1);
1638 DB((mod, LEVEL_1, "merged with %+F into source AM\n", right));
1641 /* was exchanged but optimize failed: exchange back */
1642 if (need_exchange_on_fail)
1643 exchange_left_right(irn, &left, &right, 3, 2);
1649 * Performs address mode optimization.
1651 void ia32_optimize_addressmode(ia32_code_gen_t *cg) {
1652 /* if we are supposed to do AM or LEA optimization: recalculate edges */
1653 if (cg->opt & (IA32_OPT_DOAM | IA32_OPT_LEA)) {
1654 edges_deactivate(cg->irg);
1655 edges_activate(cg->irg);
1658 /* no optimizations at all */
1662 /* beware: we cannot optimize LEA and AM in one run because */
1663 /* LEA optimization adds new nodes to the irg which */
1664 /* invalidates the phase data */
1666 if (cg->opt & IA32_OPT_LEA) {
1667 irg_walk_blkwise_graph(cg->irg, NULL, optimize_lea, cg);
1671 be_dump(cg->irg, "-lea", dump_ir_block_graph_sched);
1673 if (cg->opt & IA32_OPT_DOAM) {
1674 /* we need height information for am optimization */
1675 heights_t *h = heights_new(cg->irg);
1676 ia32_am_opt_env_t env;
1681 irg_walk_blkwise_graph(cg->irg, NULL, optimize_am, &env);