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);
244 Annotate mode of stored value to link field of the Store
245 as floating point converts might be optimized and we would
248 if (get_irn_opcode(irn) == iro_Store) {
249 set_irn_link(irn, get_irn_mode(get_Store_value(irn)));
252 tenv.block = get_nodes_block(irn);
255 DEBUG_ONLY(tenv.mod = cg->mod;)
257 /* Loop over all predecessors and check for Sym/Const nodes */
258 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
259 pred = get_irn_n(irn, i);
261 opc = get_irn_opcode(pred);
263 tenv.mode = get_irn_mode(pred);
264 tenv.dbg = get_irn_dbg_info(pred);
266 /* If it's a Phi, then we need to create the */
267 /* new Const in it's predecessor block */
269 tenv.block = get_Block_cfgpred_block(get_nodes_block(irn), i);
272 /* put the const into the block where the original const was */
273 if (! (cg->opt & IA32_OPT_PLACECNST)) {
274 tenv.block = get_nodes_block(pred);
279 cnst = gen_Const(&tenv);
282 cnst = gen_SymConst(&tenv);
288 /* if we found a const, then set it */
290 set_irn_n(irn, i, cnst);
297 /********************************************************************************************************
298 * _____ _ _ ____ _ _ _ _ _
299 * | __ \ | | | | / __ \ | | (_) (_) | | (_)
300 * | |__) |__ ___ _ __ | |__ ___ | | ___ | | | |_ __ | |_ _ _ __ ___ _ ______ _| |_ _ ___ _ __
301 * | ___/ _ \/ _ \ '_ \| '_ \ / _ \| |/ _ \ | | | | '_ \| __| | '_ ` _ \| |_ / _` | __| |/ _ \| '_ \
302 * | | | __/ __/ |_) | | | | (_) | | __/ | |__| | |_) | |_| | | | | | | |/ / (_| | |_| | (_) | | | |
303 * |_| \___|\___| .__/|_| |_|\___/|_|\___| \____/| .__/ \__|_|_| |_| |_|_/___\__,_|\__|_|\___/|_| |_|
306 ********************************************************************************************************/
309 * NOTE: THESE PEEPHOLE OPTIMIZATIONS MUST BE CALLED AFTER SCHEDULING AND REGISTER ALLOCATION.
312 static int ia32_cnst_compare(ir_node *n1, ir_node *n2) {
313 return get_ia32_id_cnst(n1) == get_ia32_id_cnst(n2);
317 * Checks for potential CJmp/CJmpAM optimization candidates.
319 static ir_node *ia32_determine_cjmp_cand(ir_node *irn, is_op_func_t *is_op_func) {
320 ir_node *cand = NULL;
321 ir_node *prev = sched_prev(irn);
323 if (is_Block(prev)) {
324 if (get_Block_n_cfgpreds(prev) == 1)
325 prev = get_Block_cfgpred(prev, 0);
330 /* The predecessor must be a ProjX. */
331 if (prev && is_Proj(prev) && get_irn_mode(prev) == mode_X) {
332 prev = get_Proj_pred(prev);
334 if (is_op_func(prev))
341 static int is_TestJmp_cand(const ir_node *irn) {
342 return is_ia32_TestJmp(irn) || is_ia32_And(irn);
346 * Checks if two consecutive arguments of cand matches
347 * the two arguments of irn (TestJmp).
349 static int is_TestJmp_replacement(ir_node *cand, ir_node *irn) {
350 ir_node *in1 = get_irn_n(irn, 0);
351 ir_node *in2 = get_irn_n(irn, 1);
352 int i, n = get_irn_arity(cand);
355 for (i = 0; i < n - 1; i++) {
356 if (get_irn_n(cand, i) == in1 &&
357 get_irn_n(cand, i + 1) == in2)
365 return ia32_cnst_compare(cand, irn);
371 * Tries to replace a TestJmp by a CJmp or CJmpAM (in case of And)
373 static void ia32_optimize_TestJmp(ir_node *irn, ia32_code_gen_t *cg) {
374 ir_node *cand = ia32_determine_cjmp_cand(irn, is_TestJmp_cand);
377 /* we found a possible candidate */
378 replace = cand ? is_TestJmp_replacement(cand, irn) : 0;
381 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
383 if (is_ia32_And(cand))
384 set_irn_op(irn, op_ia32_CJmpAM);
386 set_irn_op(irn, op_ia32_CJmp);
388 DB((cg->mod, LEVEL_1, "%+F\n", irn));
392 static int is_CondJmp_cand(const ir_node *irn) {
393 return is_ia32_CondJmp(irn) || is_ia32_Sub(irn);
397 * Checks if the arguments of cand are the same of irn.
399 static int is_CondJmp_replacement(ir_node *cand, ir_node *irn) {
400 int i, n = get_irn_arity(cand);
403 for (i = 0; i < n; i++) {
404 if (get_irn_n(cand, i) != get_irn_n(irn, i)) {
411 return ia32_cnst_compare(cand, irn);
417 * Tries to replace a CondJmp by a CJmpAM
419 static void ia32_optimize_CondJmp(ir_node *irn, ia32_code_gen_t *cg) {
420 ir_node *cand = ia32_determine_cjmp_cand(irn, is_CondJmp_cand);
423 /* we found a possible candidate */
424 replace = cand ? is_CondJmp_replacement(cand, irn) : 0;
427 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
430 set_irn_op(irn, op_ia32_CJmpAM);
432 DB((cg->mod, LEVEL_1, "%+F\n", irn));
437 * Creates a Push from Store(IncSP(gp_reg_size))
439 static void ia32_create_Push(ir_node *irn, ia32_code_gen_t *cg) {
440 ir_node *sp = get_irn_n(irn, 0);
441 ir_graph *irg = cg->irg;
442 ir_node *val, *next, *push, *bl, *proj_M, *proj_res, *old_proj_M, *mem;
443 const ir_edge_t *edge;
446 /* do not create push if store has already an offset assigned or base is not a IncSP */
447 if (get_ia32_am_offs(irn) || ! be_is_IncSP(sp))
450 /* do not create push if index is not NOREG */
451 if (arch_get_irn_register(cg->arch_env, get_irn_n(irn, 1)) !=
452 &ia32_gp_regs[REG_GP_NOREG])
455 /* do not create push for floating point */
456 val = get_irn_n(irn, 2);
457 if (mode_is_float(get_irn_mode(val)))
460 /* do not create push if IncSp doesn't expand stack or expand size is different from register size */
461 if (be_get_IncSP_direction(sp) != be_stack_dir_expand ||
462 be_get_IncSP_offset(sp) != get_mode_size_bytes(ia32_reg_classes[CLASS_ia32_gp].mode))
465 /* do not create push, if there is a path (inside the block) from the push value to IncSP */
466 h = heights_new(cg->irg);
467 if (get_nodes_block(val) == get_nodes_block(sp) &&
468 heights_reachable_in_block(h, val, sp))
475 /* ok, translate into Push */
476 edge = get_irn_out_edge_first(irn);
477 old_proj_M = get_edge_src_irn(edge);
479 next = sched_next(irn);
485 if the IncSP points to NoMem -> just use the memory input from store
486 if IncSP points to somewhere else -> sync memory of IncSP and Store
488 mem = be_get_IncSP_mem(sp);
489 if (mem == get_irg_no_mem(irg))
490 mem = get_irn_n(irn, 3);
495 in[1] = get_irn_n(irn, 3);
496 mem = new_r_Sync(irg, bl, 2, in);
499 bl = get_nodes_block(irn);
500 push = new_rd_ia32_Push(NULL, irg, bl, be_get_IncSP_pred(sp), val, mem);
501 proj_res = new_r_Proj(irg, bl, push, get_irn_mode(sp), pn_ia32_Push_stack);
502 proj_M = new_r_Proj(irg, bl, push, mode_M, pn_ia32_Push_M);
504 /* copy a possible constant from the store */
505 set_ia32_id_cnst(push, get_ia32_id_cnst(irn));
506 set_ia32_immop_type(push, get_ia32_immop_type(irn));
508 /* the push must have SP out register */
509 arch_set_irn_register(cg->arch_env, push, arch_get_irn_register(cg->arch_env, sp));
511 exchange(old_proj_M, proj_M);
512 exchange(sp, proj_res);
513 sched_add_before(next, push);
514 sched_add_after(push, proj_res);
518 * Creates a Pop from IncSP(Load(sp))
520 static void ia32_create_Pop(ir_node *irn, ia32_code_gen_t *cg) {
521 ir_node *old_proj_M = be_get_IncSP_mem(irn);
522 ir_node *load = skip_Proj(old_proj_M);
523 ir_node *old_proj_res = NULL;
524 ir_node *bl, *pop, *next, *proj_res, *proj_sp, *proj_M;
525 const ir_edge_t *edge;
526 const arch_register_t *reg, *sp;
528 if (! is_ia32_Load(load) || get_ia32_am_offs(load))
531 if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 1)) !=
532 &ia32_gp_regs[REG_GP_NOREG])
534 if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 0)) != cg->isa->arch_isa.sp)
537 /* ok, translate into pop */
538 foreach_out_edge(load, edge) {
539 ir_node *succ = get_edge_src_irn(edge);
540 if (succ != old_proj_M) {
545 if (! old_proj_res) {
547 return; /* should not happen */
550 bl = get_nodes_block(load);
552 /* IncSP is typically scheduled after the load, so remove it first */
554 next = sched_next(old_proj_res);
555 sched_remove(old_proj_res);
558 reg = arch_get_irn_register(cg->arch_env, load);
559 sp = arch_get_irn_register(cg->arch_env, irn);
561 pop = new_rd_ia32_Pop(NULL, current_ir_graph, bl, get_irn_n(irn, 0), get_irn_n(load, 2));
562 proj_res = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(old_proj_res), pn_ia32_Pop_res);
563 proj_sp = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(irn), pn_ia32_Pop_stack);
564 proj_M = new_r_Proj(current_ir_graph, bl, pop, mode_M, pn_ia32_Pop_M);
566 exchange(old_proj_M, proj_M);
567 exchange(old_proj_res, proj_res);
568 exchange(irn, proj_sp);
570 arch_set_irn_register(cg->arch_env, proj_res, reg);
571 arch_set_irn_register(cg->arch_env, proj_sp, sp);
573 sched_add_before(next, proj_sp);
574 sched_add_before(proj_sp, proj_res);
575 sched_add_before(proj_res,pop);
579 * Tries to optimize two following IncSP.
581 static void ia32_optimize_IncSP(ir_node *irn, ia32_code_gen_t *cg) {
582 ir_node *prev = be_get_IncSP_pred(irn);
583 int real_uses = get_irn_n_edges(prev);
585 if (be_is_IncSP(prev) && real_uses == 1) {
586 /* first IncSP has only one IncSP user, kill the first one */
587 unsigned prev_offs = be_get_IncSP_offset(prev);
588 be_stack_dir_t prev_dir = be_get_IncSP_direction(prev);
589 unsigned curr_offs = be_get_IncSP_offset(irn);
590 be_stack_dir_t curr_dir = be_get_IncSP_direction(irn);
592 int new_ofs = prev_offs * (prev_dir == be_stack_dir_expand ? -1 : +1) +
593 curr_offs * (curr_dir == be_stack_dir_expand ? -1 : +1);
597 curr_dir = be_stack_dir_expand;
600 curr_dir = be_stack_dir_shrink;
601 be_set_IncSP_offset(prev, 0);
602 be_set_IncSP_offset(irn, (unsigned)new_ofs);
603 be_set_IncSP_direction(irn, curr_dir);
605 /* Omit the optimized IncSP */
606 be_set_IncSP_pred(irn, be_get_IncSP_pred(prev));
611 * Performs Peephole Optimizations.
613 void ia32_peephole_optimization(ir_node *irn, void *env) {
614 ia32_code_gen_t *cg = env;
616 if (is_ia32_TestJmp(irn))
617 ia32_optimize_TestJmp(irn, cg);
618 else if (is_ia32_CondJmp(irn))
619 ia32_optimize_CondJmp(irn, cg);
620 /* seems to be buggy when using Pushes */
621 // else if (be_is_IncSP(irn))
622 // ia32_optimize_IncSP(irn, cg);
623 else if (is_ia32_Store(irn))
624 ia32_create_Push(irn, cg);
629 /******************************************************************
631 * /\ | | | | | \/ | | |
632 * / \ __| | __| |_ __ ___ ___ ___| \ / | ___ __| | ___
633 * / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
634 * / ____ \ (_| | (_| | | | __/\__ \__ \ | | | (_) | (_| | __/
635 * /_/ \_\__,_|\__,_|_| \___||___/___/_| |_|\___/ \__,_|\___|
637 ******************************************************************/
644 static int node_is_ia32_comm(const ir_node *irn) {
645 return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
648 static int ia32_get_irn_n_edges(const ir_node *irn) {
649 const ir_edge_t *edge;
652 foreach_out_edge(irn, edge) {
660 * Returns the first mode_M Proj connected to irn.
662 static ir_node *get_mem_proj(const ir_node *irn) {
663 const ir_edge_t *edge;
666 assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
668 foreach_out_edge(irn, edge) {
669 src = get_edge_src_irn(edge);
671 assert(is_Proj(src) && "Proj expected");
673 if (get_irn_mode(src) == mode_M)
681 * Returns the first Proj with mode != mode_M connected to irn.
683 static ir_node *get_res_proj(const ir_node *irn) {
684 const ir_edge_t *edge;
687 assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
689 foreach_out_edge(irn, edge) {
690 src = get_edge_src_irn(edge);
692 assert(is_Proj(src) && "Proj expected");
694 if (get_irn_mode(src) != mode_M)
702 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
704 * @param pred The node to be checked
705 * @param is_op_func The check-function
706 * @return 1 if conditions are fulfilled, 0 otherwise
708 static int pred_is_specific_node(const ir_node *pred, is_op_func_t *is_op_func) {
709 if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
717 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
718 * and if the predecessor is in block bl.
720 * @param bl The block
721 * @param pred The node to be checked
722 * @param is_op_func The check-function
723 * @return 1 if conditions are fulfilled, 0 otherwise
725 static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
726 int (*is_op_func)(const ir_node *n))
729 pred = get_Proj_pred(pred);
730 if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
739 * Checks if irn is a candidate for address calculation.
741 * - none of the operand must be a Load within the same block OR
742 * - all Loads must have more than one user OR
743 * - the irn has a frame entity (it's a former FrameAddr)
745 * @param block The block the Loads must/mustnot be in
746 * @param irn The irn to check
747 * return 1 if irn is a candidate, 0 otherwise
749 static int is_addr_candidate(const ir_node *block, const ir_node *irn) {
750 ir_node *in, *left, *right;
753 left = get_irn_n(irn, 2);
754 right = get_irn_n(irn, 3);
758 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
759 n = ia32_get_irn_n_edges(in);
760 is_cand = (n == 1) ? 0 : is_cand; /* load with only one user: don't create LEA */
765 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
766 n = ia32_get_irn_n_edges(in);
767 is_cand = (n == 1) ? 0 : is_cand; /* load with only one user: don't create LEA */
770 is_cand = get_ia32_frame_ent(irn) ? 1 : is_cand;
776 * Checks if irn is a candidate for address mode.
779 * - at least one operand has to be a Load within the same block AND
780 * - the load must not have other users than the irn AND
781 * - the irn must not have a frame entity set
783 * @param cg The ia32 code generator
784 * @param h The height information of the irg
785 * @param block The block the Loads must/mustnot be in
786 * @param irn The irn to check
787 * return 0 if irn is no candidate, 1 if left load can be used, 2 if right one, 3 for both
789 static ia32_am_cand_t is_am_candidate(ia32_code_gen_t *cg, heights_t *h, const ir_node *block, ir_node *irn) {
790 ir_node *in, *load, *other, *left, *right;
791 int n, is_cand = 0, cand;
793 if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn) || is_ia32_vfild(irn) || is_ia32_vfist(irn))
796 left = get_irn_n(irn, 2);
797 right = get_irn_n(irn, 3);
801 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
802 n = ia32_get_irn_n_edges(in);
803 is_cand = (n == 1) ? 1 : is_cand; /* load with more than one user: no AM */
805 load = get_Proj_pred(in);
808 /* If there is a data dependency of other irn from load: cannot use AM */
809 if (get_nodes_block(other) == block) {
810 other = skip_Proj(other);
811 is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
815 cand = is_cand ? IA32_AM_CAND_LEFT : IA32_AM_CAND_NONE;
819 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
820 n = ia32_get_irn_n_edges(in);
821 is_cand = (n == 1) ? 1 : is_cand; /* load with more than one user: no AM */
823 load = get_Proj_pred(in);
826 /* If there is a data dependency of other irn from load: cannot use load */
827 if (get_nodes_block(other) == block) {
828 other = skip_Proj(other);
829 is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
833 cand = is_cand ? (cand | IA32_AM_CAND_RIGHT) : cand;
835 /* check some special cases */
836 if (USE_SSE2(cg) && is_ia32_Conv_I2FP(irn)) {
837 /* SSE Conv I -> FP cvtsi2s(s|d) can only load 32 bit values */
838 if (get_mode_size_bits(get_ia32_tgt_mode(irn)) != 32)
839 cand = IA32_AM_CAND_NONE;
841 else if (is_ia32_Conv_I2I(irn)) {
842 /* we cannot load an N bit value and implicitly convert it into an M bit value if N > M */
843 if (get_mode_size_bits(get_ia32_src_mode(irn)) > get_mode_size_bits(get_ia32_tgt_mode(irn)))
844 cand = IA32_AM_CAND_NONE;
847 /* if the irn has a frame entity: we do not use address mode */
848 return get_ia32_frame_ent(irn) ? IA32_AM_CAND_NONE : cand;
852 * Compares the base and index addr and the load/store entities
853 * and returns 1 if they are equal.
855 static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
856 const ir_node *addr_b, const ir_node *addr_i)
858 int is_equal = (addr_b == get_irn_n(load, 0)) && (addr_i == get_irn_n(load, 1));
859 entity *lent = get_ia32_frame_ent(load);
860 entity *sent = get_ia32_frame_ent(store);
861 ident *lid = get_ia32_am_sc(load);
862 ident *sid = get_ia32_am_sc(store);
863 char *loffs = get_ia32_am_offs(load);
864 char *soffs = get_ia32_am_offs(store);
866 /* are both entities set and equal? */
867 if (is_equal && (lent || sent))
868 is_equal = lent && sent && (lent == sent);
870 /* are address mode idents set and equal? */
871 if (is_equal && (lid || sid))
872 is_equal = lid && sid && (lid == sid);
874 /* are offsets set and equal */
875 if (is_equal && (loffs || soffs))
876 is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;
878 /* are the load and the store of the same mode? */
879 is_equal = is_equal ? get_ia32_ls_mode(load) == get_ia32_ls_mode(store) : 0;
884 typedef enum _ia32_take_lea_attr {
885 IA32_LEA_ATTR_NONE = 0,
886 IA32_LEA_ATTR_BASE = (1 << 0),
887 IA32_LEA_ATTR_INDEX = (1 << 1),
888 IA32_LEA_ATTR_OFFS = (1 << 2),
889 IA32_LEA_ATTR_SCALE = (1 << 3),
890 IA32_LEA_ATTR_AMSC = (1 << 4),
891 IA32_LEA_ATTR_FENT = (1 << 5)
892 } ia32_take_lea_attr;
895 * Decides if we have to keep the LEA operand or if we can assimilate it.
897 static int do_new_lea(ir_node *irn, ir_node *base, ir_node *index, ir_node *lea,
898 int have_am_sc, ia32_code_gen_t *cg)
900 ir_node *lea_base = get_irn_n(lea, 0);
901 ir_node *lea_idx = get_irn_n(lea, 1);
902 entity *irn_ent = get_ia32_frame_ent(irn);
903 entity *lea_ent = get_ia32_frame_ent(lea);
905 int is_noreg_base = be_is_NoReg(cg, base);
906 int is_noreg_index = be_is_NoReg(cg, index);
907 ia32_am_flavour_t am_flav = get_ia32_am_flavour(lea);
909 /* If the Add and the LEA both have a different frame entity set: keep */
910 if (irn_ent && lea_ent && (irn_ent != lea_ent))
911 return IA32_LEA_ATTR_NONE;
912 else if (! irn_ent && lea_ent)
913 ret_val |= IA32_LEA_ATTR_FENT;
915 /* If the Add and the LEA both have already an address mode symconst: keep */
916 if (have_am_sc && get_ia32_am_sc(lea))
917 return IA32_LEA_ATTR_NONE;
918 else if (get_ia32_am_sc(lea))
919 ret_val |= IA32_LEA_ATTR_AMSC;
921 /* Check the different base-index combinations */
923 if (! is_noreg_base && ! is_noreg_index) {
924 /* Assimilate if base is the lea and the LEA is just a Base + Offset calculation */
925 if ((base == lea) && ! (am_flav & ia32_I ? 1 : 0)) {
926 if (am_flav & ia32_O)
927 ret_val |= IA32_LEA_ATTR_OFFS;
929 ret_val |= IA32_LEA_ATTR_BASE;
932 return IA32_LEA_ATTR_NONE;
934 else if (! is_noreg_base && is_noreg_index) {
935 /* Base is set but index not */
937 /* Base points to LEA: assimilate everything */
938 if (am_flav & ia32_O)
939 ret_val |= IA32_LEA_ATTR_OFFS;
940 if (am_flav & ia32_S)
941 ret_val |= IA32_LEA_ATTR_SCALE;
942 if (am_flav & ia32_I)
943 ret_val |= IA32_LEA_ATTR_INDEX;
945 ret_val |= IA32_LEA_ATTR_BASE;
947 else if (am_flav & ia32_B ? 0 : 1) {
948 /* Base is not the LEA but the LEA is an index only calculation: assimilate */
949 if (am_flav & ia32_O)
950 ret_val |= IA32_LEA_ATTR_OFFS;
951 if (am_flav & ia32_S)
952 ret_val |= IA32_LEA_ATTR_SCALE;
954 ret_val |= IA32_LEA_ATTR_INDEX;
957 return IA32_LEA_ATTR_NONE;
959 else if (is_noreg_base && ! is_noreg_index) {
960 /* Index is set but not base */
962 /* Index points to LEA: assimilate everything */
963 if (am_flav & ia32_O)
964 ret_val |= IA32_LEA_ATTR_OFFS;
965 if (am_flav & ia32_S)
966 ret_val |= IA32_LEA_ATTR_SCALE;
967 if (am_flav & ia32_B)
968 ret_val |= IA32_LEA_ATTR_BASE;
970 ret_val |= IA32_LEA_ATTR_INDEX;
972 else if (am_flav & ia32_I ? 0 : 1) {
973 /* Index is not the LEA but the LEA is a base only calculation: assimilate */
974 if (am_flav & ia32_O)
975 ret_val |= IA32_LEA_ATTR_OFFS;
976 if (am_flav & ia32_S)
977 ret_val |= IA32_LEA_ATTR_SCALE;
979 ret_val |= IA32_LEA_ATTR_BASE;
982 return IA32_LEA_ATTR_NONE;
985 assert(0 && "There must have been set base or index");
993 * Folds Add or Sub to LEA if possible
995 static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn, ir_node *noreg) {
996 ir_graph *irg = get_irn_irg(irn);
997 dbg_info *dbg = get_irn_dbg_info(irn);
998 ir_node *block = get_nodes_block(irn);
1000 ir_node *shift = NULL;
1001 ir_node *lea_o = NULL;
1002 ir_node *lea = NULL;
1004 const char *offs_cnst = NULL;
1005 char *offs_lea = NULL;
1011 ident *am_sc = NULL;
1012 entity *lea_ent = NULL;
1013 ir_node *left, *right, *temp;
1014 ir_node *base, *index;
1015 ia32_am_flavour_t am_flav;
1016 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
1018 if (is_ia32_Add(irn))
1021 left = get_irn_n(irn, 2);
1022 right = get_irn_n(irn, 3);
1024 /* "normalize" arguments in case of add with two operands */
1025 if (isadd && ! be_is_NoReg(cg, right)) {
1026 /* put LEA == ia32_am_O as right operand */
1027 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
1028 set_irn_n(irn, 2, right);
1029 set_irn_n(irn, 3, left);
1035 /* put LEA != ia32_am_O as left operand */
1036 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
1037 set_irn_n(irn, 2, right);
1038 set_irn_n(irn, 3, left);
1044 /* put SHL as left operand iff left is NOT a LEA */
1045 if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
1046 set_irn_n(irn, 2, right);
1047 set_irn_n(irn, 3, left);
1060 /* check for operation with immediate */
1061 if (is_ia32_ImmConst(irn)) {
1062 DBG((mod, LEVEL_1, "\tfound op with imm const"));
1064 offs_cnst = get_ia32_cnst(irn);
1067 else if (is_ia32_ImmSymConst(irn)) {
1068 DBG((mod, LEVEL_1, "\tfound op with imm symconst"));
1072 am_sc = get_ia32_id_cnst(irn);
1073 am_sc_sign = is_ia32_am_sc_sign(irn);
1076 /* determine the operand which needs to be checked */
1077 temp = be_is_NoReg(cg, right) ? left : right;
1079 /* check if right operand is AMConst (LEA with ia32_am_O) */
1080 /* but we can only eat it up if there is no other symconst */
1081 /* because the linker won't accept two symconsts */
1082 if (! have_am_sc && is_ia32_Lea(temp) && get_ia32_am_flavour(temp) == ia32_am_O) {
1083 DBG((mod, LEVEL_1, "\tgot op with LEA am_O"));
1085 offs_lea = get_ia32_am_offs(temp);
1086 am_sc = get_ia32_am_sc(temp);
1087 am_sc_sign = is_ia32_am_sc_sign(temp);
1097 /* default for add -> make right operand to index */
1101 DBG((mod, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
1103 /* determine the operand which needs to be checked */
1105 if (is_ia32_Lea(left)) {
1109 /* check for SHL 1,2,3 */
1110 if (pred_is_specific_node(temp, is_ia32_Shl)) {
1111 temp = get_Proj_pred(temp);
1114 if (get_ia32_Immop_tarval(temp)) {
1115 scale = get_tarval_long(get_ia32_Immop_tarval(temp));
1118 index = get_irn_n(temp, 2);
1120 DBG((mod, LEVEL_1, "\tgot scaled index %+F\n", index));
1130 if (! be_is_NoReg(cg, index)) {
1131 /* if we have index, but left == right -> no base */
1132 if (left == right) {
1135 else if (! is_ia32_Lea(left) && (index != right)) {
1136 /* index != right -> we found a good Shl */
1137 /* left != LEA -> this Shl was the left operand */
1138 /* -> base is right operand */
1139 base = (right == lea_o) ? noreg : right;
1144 /* Try to assimilate a LEA as left operand */
1145 if (is_ia32_Lea(left) && (get_ia32_am_flavour(left) != ia32_am_O)) {
1146 /* check if we can assimilate the LEA */
1147 int take_attr = do_new_lea(irn, base, index, left, have_am_sc, cg);
1149 if (take_attr == IA32_LEA_ATTR_NONE) {
1150 DBG((mod, LEVEL_1, "\tleave old LEA, creating new one\n"));
1153 DBG((mod, LEVEL_1, "\tgot LEA as left operand ... assimilating\n"));
1154 lea = left; /* for statistics */
1156 if (take_attr & IA32_LEA_ATTR_OFFS)
1157 offs = get_ia32_am_offs(left);
1159 if (take_attr & IA32_LEA_ATTR_AMSC) {
1160 am_sc = get_ia32_am_sc(left);
1162 am_sc_sign = is_ia32_am_sc_sign(left);
1165 if (take_attr & IA32_LEA_ATTR_SCALE)
1166 scale = get_ia32_am_scale(left);
1168 if (take_attr & IA32_LEA_ATTR_BASE)
1169 base = get_irn_n(left, 0);
1171 if (take_attr & IA32_LEA_ATTR_INDEX)
1172 index = get_irn_n(left, 1);
1174 if (take_attr & IA32_LEA_ATTR_FENT)
1175 lea_ent = get_ia32_frame_ent(left);
1179 /* ok, we can create a new LEA */
1181 res = new_rd_ia32_Lea(dbg, irg, block, base, index, mode_Is);
1183 /* add the old offset of a previous LEA */
1185 add_ia32_am_offs(res, offs);
1188 /* add the new offset */
1191 add_ia32_am_offs(res, offs_cnst);
1194 add_ia32_am_offs(res, offs_lea);
1198 /* either lea_O-cnst, -cnst or -lea_O */
1201 add_ia32_am_offs(res, offs_lea);
1204 sub_ia32_am_offs(res, offs_cnst);
1207 sub_ia32_am_offs(res, offs_lea);
1211 /* set the address mode symconst */
1213 set_ia32_am_sc(res, am_sc);
1215 set_ia32_am_sc_sign(res);
1218 /* copy the frame entity (could be set in case of Add */
1219 /* which was a FrameAddr) */
1221 set_ia32_frame_ent(res, lea_ent);
1223 set_ia32_frame_ent(res, get_ia32_frame_ent(irn));
1225 if (get_ia32_frame_ent(res))
1226 set_ia32_use_frame(res);
1229 set_ia32_am_scale(res, scale);
1231 am_flav = ia32_am_N;
1232 /* determine new am flavour */
1233 if (offs || offs_cnst || offs_lea || have_am_sc) {
1236 if (! be_is_NoReg(cg, base)) {
1239 if (! be_is_NoReg(cg, index)) {
1245 set_ia32_am_flavour(res, am_flav);
1247 set_ia32_op_type(res, ia32_AddrModeS);
1249 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
1251 DBG((mod, LEVEL_1, "\tLEA [%+F + %+F * %d + %s]\n", base, index, scale, get_ia32_am_offs(res)));
1253 /* we will exchange it, report here before the Proj is created */
1254 if (shift && lea && lea_o)
1255 DBG_OPT_LEA4(irn, lea_o, lea, shift, res);
1256 else if (shift && lea)
1257 DBG_OPT_LEA3(irn, lea, shift, res);
1258 else if (shift && lea_o)
1259 DBG_OPT_LEA3(irn, lea_o, shift, res);
1260 else if (lea && lea_o)
1261 DBG_OPT_LEA3(irn, lea_o, lea, res);
1263 DBG_OPT_LEA2(irn, shift, res);
1265 DBG_OPT_LEA2(irn, lea, res);
1267 DBG_OPT_LEA2(irn, lea_o, res);
1269 DBG_OPT_LEA1(irn, res);
1271 /* get the result Proj of the Add/Sub */
1272 irn = get_res_proj(irn);
1274 assert(irn && "Couldn't find result proj");
1276 /* exchange the old op with the new LEA */
1285 * Merges a Load/Store node with a LEA.
1286 * @param irn The Load/Store node
1287 * @param lea The LEA
1289 static void merge_loadstore_lea(ir_node *irn, ir_node *lea) {
1290 entity *irn_ent = get_ia32_frame_ent(irn);
1291 entity *lea_ent = get_ia32_frame_ent(lea);
1293 /* If the irn and the LEA both have a different frame entity set: do not merge */
1294 if (irn_ent && lea_ent && (irn_ent != lea_ent))
1296 else if (! irn_ent && lea_ent) {
1297 set_ia32_frame_ent(irn, lea_ent);
1298 set_ia32_use_frame(irn);
1301 /* get the AM attributes from the LEA */
1302 add_ia32_am_offs(irn, get_ia32_am_offs(lea));
1303 set_ia32_am_scale(irn, get_ia32_am_scale(lea));
1304 set_ia32_am_flavour(irn, get_ia32_am_flavour(lea));
1306 set_ia32_am_sc(irn, get_ia32_am_sc(lea));
1307 if (is_ia32_am_sc_sign(lea))
1308 set_ia32_am_sc_sign(irn);
1310 set_ia32_op_type(irn, is_ia32_Ld(irn) ? ia32_AddrModeS : ia32_AddrModeD);
1312 /* set base and index */
1313 set_irn_n(irn, 0, get_irn_n(lea, 0));
1314 set_irn_n(irn, 1, get_irn_n(lea, 1));
1316 /* clear remat flag */
1317 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1319 if (is_ia32_Ld(irn))
1320 DBG_OPT_LOAD_LEA(lea, irn);
1322 DBG_OPT_STORE_LEA(lea, irn);
1327 * Sets new_right index of irn to right and new_left index to left.
1328 * Also exchange left and right
1330 static void exchange_left_right(ir_node *irn, ir_node **left, ir_node **right, int new_left, int new_right) {
1333 set_irn_n(irn, new_right, *right);
1334 set_irn_n(irn, new_left, *left);
1340 /* this is only needed for Compares, but currently ALL nodes
1341 * have this attribute :-) */
1342 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1346 * Performs address calculation optimization (create LEAs if possible)
1348 static void optimize_lea(ir_node *irn, void *env) {
1349 ia32_code_gen_t *cg = env;
1350 ir_node *block, *noreg_gp, *left, *right;
1352 if (! is_ia32_irn(irn))
1355 /* Following cases can occur: */
1356 /* - Sub (l, imm) -> LEA [base - offset] */
1357 /* - Sub (l, r == LEA with ia32_am_O) -> LEA [base - offset] */
1358 /* - Add (l, imm) -> LEA [base + offset] */
1359 /* - Add (l, r == LEA with ia32_am_O) -> LEA [base + offset] */
1360 /* - Add (l == LEA with ia32_am_O, r) -> LEA [base + offset] */
1361 /* - Add (l, r) -> LEA [base + index * scale] */
1362 /* with scale > 1 iff l/r == shl (1,2,3) */
1364 if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
1365 left = get_irn_n(irn, 2);
1366 right = get_irn_n(irn, 3);
1367 block = get_nodes_block(irn);
1368 noreg_gp = ia32_new_NoReg_gp(cg);
1370 /* Do not try to create a LEA if one of the operands is a Load. */
1371 /* check is irn is a candidate for address calculation */
1372 if (is_addr_candidate(block, irn)) {
1375 DBG((cg->mod, LEVEL_1, "\tfound address calculation candidate %+F ... ", irn));
1376 res = fold_addr(cg, irn, noreg_gp);
1379 DB((cg->mod, LEVEL_1, "transformed into %+F\n", res));
1381 DB((cg->mod, LEVEL_1, "not transformed\n"));
1384 else if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn)) {
1385 /* - Load -> LEA into Load } TODO: If the LEA is used by more than one Load/Store */
1386 /* - Store -> LEA into Store } it might be better to keep the LEA */
1387 left = get_irn_n(irn, 0);
1389 if (is_ia32_Lea(left)) {
1390 const ir_edge_t *edge, *ne;
1393 /* merge all Loads/Stores connected to this LEA with the LEA */
1394 foreach_out_edge_safe(left, edge, ne) {
1395 src = get_edge_src_irn(edge);
1397 if (src && (is_ia32_Ld(src) || is_ia32_St(src) || is_ia32_Store8Bit(src))) {
1398 DBG((cg->mod, LEVEL_1, "\nmerging %+F into %+F\n", left, irn));
1399 if (! is_ia32_got_lea(src))
1400 merge_loadstore_lea(src, left);
1401 set_ia32_got_lea(src);
1410 * Checks for address mode patterns and performs the
1411 * necessary transformations.
1412 * This function is called by a walker.
1414 static void optimize_am(ir_node *irn, void *env) {
1415 ia32_am_opt_env_t *am_opt_env = env;
1416 ia32_code_gen_t *cg = am_opt_env->cg;
1417 heights_t *h = am_opt_env->h;
1418 ir_node *block, *noreg_gp, *noreg_fp;
1419 ir_node *left, *right;
1420 ir_node *store, *load, *mem_proj;
1421 ir_node *succ, *addr_b, *addr_i;
1422 int check_am_src = 0;
1423 int need_exchange_on_fail = 0;
1424 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
1426 if (! is_ia32_irn(irn))
1429 block = get_nodes_block(irn);
1430 noreg_gp = ia32_new_NoReg_gp(cg);
1431 noreg_fp = ia32_new_NoReg_fp(cg);
1433 DBG((mod, LEVEL_1, "checking for AM\n"));
1435 /* fold following patterns: */
1436 /* - op -> Load into AMop with am_Source */
1438 /* - op is am_Source capable AND */
1439 /* - the Load is only used by this op AND */
1440 /* - the Load is in the same block */
1441 /* - Store -> op -> Load into AMop with am_Dest */
1443 /* - op is am_Dest capable AND */
1444 /* - the Store uses the same address as the Load AND */
1445 /* - the Load is only used by this op AND */
1446 /* - the Load and Store are in the same block AND */
1447 /* - nobody else uses the result of the op */
1449 if ((get_ia32_am_support(irn) != ia32_am_None) && ! is_ia32_Lea(irn)) {
1450 ia32_am_cand_t cand = is_am_candidate(cg, h, block, irn);
1451 ia32_am_cand_t orig_cand = cand;
1453 /* cand == 1: load is left; cand == 2: load is right; */
1455 if (cand == IA32_AM_CAND_NONE)
1458 DBG((mod, LEVEL_1, "\tfound address mode candidate %+F ... ", irn));
1460 left = get_irn_n(irn, 2);
1461 if (get_irn_arity(irn) == 4) {
1462 /* it's an "unary" operation */
1466 right = get_irn_n(irn, 3);
1469 /* normalize commutative ops */
1470 if (node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_LEFT)) {
1472 /* Assure that right operand is always a Load if there is one */
1473 /* because non-commutative ops can only use Dest AM if the right */
1474 /* operand is a load, so we only need to check right operand. */
1476 exchange_left_right(irn, &left, &right, 3, 2);
1477 need_exchange_on_fail = 1;
1479 /* now: load is right */
1480 cand = IA32_AM_CAND_RIGHT;
1483 /* check for Store -> op -> Load */
1485 /* Store -> op -> Load optimization is only possible if supported by op */
1486 /* and if right operand is a Load */
1487 if ((get_ia32_am_support(irn) & ia32_am_Dest) && (cand & IA32_AM_CAND_RIGHT))
1489 /* An address mode capable op always has a result Proj. */
1490 /* If this Proj is used by more than one other node, we don't need to */
1491 /* check further, otherwise we check for Store and remember the address, */
1492 /* the Store points to. */
1494 succ = get_res_proj(irn);
1495 assert(succ && "Couldn't find result proj");
1501 /* now check for users and Store */
1502 if (ia32_get_irn_n_edges(succ) == 1) {
1503 succ = get_edge_src_irn(get_irn_out_edge_first(succ));
1505 if (is_ia32_xStore(succ) || is_ia32_Store(succ)) {
1507 addr_b = get_irn_n(store, 0);
1508 addr_i = get_irn_n(store, 1);
1513 /* we found a Store as single user: Now check for Load */
1515 /* Extra check for commutative ops with two Loads */
1516 /* -> put the interesting Load right */
1517 if (node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_BOTH)) {
1518 if ((addr_b == get_irn_n(get_Proj_pred(left), 0)) &&
1519 (addr_i == get_irn_n(get_Proj_pred(left), 1)))
1521 /* We exchange left and right, so it's easier to kill */
1522 /* the correct Load later and to handle unary operations. */
1523 exchange_left_right(irn, &left, &right, 3, 2);
1524 need_exchange_on_fail ^= 1;
1528 /* skip the Proj for easier access */
1529 load = get_Proj_pred(right);
1531 /* Compare Load and Store address */
1532 if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1533 /* Right Load is from same address, so we can */
1534 /* disconnect the Load and Store here */
1536 /* set new base, index and attributes */
1537 set_irn_n(irn, 0, addr_b);
1538 set_irn_n(irn, 1, addr_i);
1539 add_ia32_am_offs(irn, get_ia32_am_offs(load));
1540 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1541 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1542 set_ia32_op_type(irn, ia32_AddrModeD);
1543 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1544 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1546 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1547 if (is_ia32_am_sc_sign(load))
1548 set_ia32_am_sc_sign(irn);
1550 if (is_ia32_use_frame(load))
1551 set_ia32_use_frame(irn);
1553 /* connect to Load memory and disconnect Load */
1554 if (get_irn_arity(irn) == 5) {
1556 set_irn_n(irn, 4, get_irn_n(load, 2));
1557 set_irn_n(irn, 3, noreg_gp);
1561 set_irn_n(irn, 3, get_irn_n(load, 2));
1562 set_irn_n(irn, 2, noreg_gp);
1565 /* connect the memory Proj of the Store to the op */
1566 mem_proj = get_mem_proj(store);
1567 set_Proj_pred(mem_proj, irn);
1568 set_Proj_proj(mem_proj, 1);
1570 /* clear remat flag */
1571 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1573 DBG_OPT_AM_D(load, store, irn);
1575 DB((mod, LEVEL_1, "merged with %+F and %+F into dest AM\n", load, store));
1577 need_exchange_on_fail = 0;
1580 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1581 /* There was no store, check if we still can optimize for source address mode */
1584 } /* if (support AM Dest) */
1585 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1586 /* op doesn't support am AM Dest -> check for AM Source */
1590 /* was exchanged but optimize failed: exchange back */
1591 if (need_exchange_on_fail) {
1592 exchange_left_right(irn, &left, &right, 3, 2);
1596 need_exchange_on_fail = 0;
1598 /* normalize commutative ops */
1599 if (check_am_src && node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_RIGHT)) {
1601 /* Assure that left operand is always a Load if there is one */
1602 /* because non-commutative ops can only use Source AM if the */
1603 /* left operand is a Load, so we only need to check the left */
1604 /* operand afterwards. */
1606 exchange_left_right(irn, &left, &right, 3, 2);
1607 need_exchange_on_fail = 1;
1609 /* now: load is left */
1610 cand = IA32_AM_CAND_LEFT;
1613 /* optimize op -> Load iff Load is only used by this op */
1614 /* and left operand is a Load which only used by this irn */
1616 (cand & IA32_AM_CAND_LEFT) &&
1617 (ia32_get_irn_n_edges(left) == 1))
1619 left = get_Proj_pred(left);
1621 addr_b = get_irn_n(left, 0);
1622 addr_i = get_irn_n(left, 1);
1624 /* set new base, index and attributes */
1625 set_irn_n(irn, 0, addr_b);
1626 set_irn_n(irn, 1, addr_i);
1627 add_ia32_am_offs(irn, get_ia32_am_offs(left));
1628 set_ia32_am_scale(irn, get_ia32_am_scale(left));
1629 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
1630 set_ia32_op_type(irn, ia32_AddrModeS);
1631 set_ia32_frame_ent(irn, get_ia32_frame_ent(left));
1632 set_ia32_ls_mode(irn, get_ia32_ls_mode(left));
1634 set_ia32_am_sc(irn, get_ia32_am_sc(left));
1635 if (is_ia32_am_sc_sign(left))
1636 set_ia32_am_sc_sign(irn);
1638 /* clear remat flag */
1639 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1641 if (is_ia32_use_frame(left))
1642 set_ia32_use_frame(irn);
1644 /* connect to Load memory */
1645 if (get_irn_arity(irn) == 5) {
1647 set_irn_n(irn, 4, get_irn_n(left, 2));
1649 /* this is only needed for Compares, but currently ALL nodes
1650 * have this attribute :-) */
1651 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1653 /* disconnect from Load */
1654 /* (make second op -> first, set second in to noreg) */
1655 set_irn_n(irn, 2, get_irn_n(irn, 3));
1656 set_irn_n(irn, 3, noreg_gp);
1660 set_irn_n(irn, 3, get_irn_n(left, 2));
1662 /* disconnect from Load */
1663 set_irn_n(irn, 2, noreg_gp);
1666 DBG_OPT_AM_S(left, irn);
1668 /* If Load has a memory Proj, connect it to the op */
1669 mem_proj = get_mem_proj(left);
1671 set_Proj_pred(mem_proj, irn);
1672 set_Proj_proj(mem_proj, 1);
1675 DB((mod, LEVEL_1, "merged with %+F into source AM\n", left));
1678 /* was exchanged but optimize failed: exchange back */
1679 if (need_exchange_on_fail)
1680 exchange_left_right(irn, &left, &right, 3, 2);
1686 * Performs address mode optimization.
1688 void ia32_optimize_addressmode(ia32_code_gen_t *cg) {
1689 /* if we are supposed to do AM or LEA optimization: recalculate edges */
1690 if (cg->opt & (IA32_OPT_DOAM | IA32_OPT_LEA)) {
1691 edges_deactivate(cg->irg);
1692 edges_activate(cg->irg);
1695 /* no optimizations at all */
1699 /* beware: we cannot optimize LEA and AM in one run because */
1700 /* LEA optimization adds new nodes to the irg which */
1701 /* invalidates the phase data */
1703 if (cg->opt & IA32_OPT_LEA) {
1704 irg_walk_blkwise_graph(cg->irg, NULL, optimize_lea, cg);
1707 if (cg->opt & IA32_OPT_DOAM) {
1708 /* we need height information for am optimization */
1709 heights_t *h = heights_new(cg->irg);
1710 ia32_am_opt_env_t env;
1715 irg_walk_blkwise_graph(cg->irg, NULL, optimize_am, &env);