8 #include "firm_types.h"
15 #include "../benode_t.h"
16 #include "../besched_t.h"
18 #include "ia32_new_nodes.h"
19 #include "bearch_ia32_t.h"
20 #include "gen_ia32_regalloc_if.h" /* the generated interface (register type and class defenitions) */
21 #include "ia32_transform.h"
22 #include "ia32_dbg_stat.h"
25 #define is_NoMem(irn) (get_irn_op(irn) == op_NoMem)
27 typedef int is_op_func_t(const ir_node *n);
30 * checks if a node represents the NOREG value
32 static int be_is_NoReg(ia32_code_gen_t *cg, const ir_node *irn) {
33 be_abi_irg_t *babi = cg->birg->abi;
34 const arch_register_t *fp_noreg = USE_SSE2(cg) ?
35 &ia32_xmm_regs[REG_XMM_NOREG] : &ia32_vfp_regs[REG_VFP_NOREG];
37 return (be_abi_get_callee_save_irn(babi, &ia32_gp_regs[REG_GP_NOREG]) == irn) ||
38 (be_abi_get_callee_save_irn(babi, fp_noreg) == irn);
43 /*************************************************
46 * | | ___ _ __ ___| |_ __ _ _ __ | |_ ___
47 * | | / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
48 * | |___| (_) | | | \__ \ || (_| | | | | |_\__ \
49 * \_____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
51 *************************************************/
54 * creates a unique ident by adding a number to a tag
56 * @param tag the tag string, must contain a %d if a number
59 static ident *unique_id(const char *tag)
61 static unsigned id = 0;
64 snprintf(str, sizeof(str), tag, ++id);
65 return new_id_from_str(str);
71 * Transforms a SymConst.
73 * @param mod the debug module
74 * @param block the block the new node should belong to
75 * @param node the ir SymConst node
76 * @param mode mode of the SymConst
77 * @return the created ia32 Const node
79 static ir_node *gen_SymConst(ia32_transform_env_t *env) {
81 dbg_info *dbg = env->dbg;
82 ir_mode *mode = env->mode;
83 ir_graph *irg = env->irg;
84 ir_node *block = env->block;
86 if (mode_is_float(mode)) {
88 if (USE_SSE2(env->cg))
89 cnst = new_rd_ia32_xConst(dbg, irg, block, get_irg_no_mem(irg), mode);
91 cnst = new_rd_ia32_vfConst(dbg, irg, block, get_irg_no_mem(irg), mode);
94 cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), mode);
96 set_ia32_Const_attr(cnst, env->irn);
102 * Get a primitive type for a mode.
104 static ir_type *get_prim_type(pmap *types, ir_mode *mode)
106 pmap_entry *e = pmap_find(types, mode);
111 snprintf(buf, sizeof(buf), "prim_type_%s", get_mode_name(mode));
112 res = new_type_primitive(new_id_from_str(buf), mode);
113 pmap_insert(types, mode, res);
121 * Get an entity that is initialized with a tarval
123 static entity *get_entity_for_tv(ia32_code_gen_t *cg, ir_node *cnst)
125 tarval *tv = get_Const_tarval(cnst);
126 pmap_entry *e = pmap_find(cg->isa->tv_ent, tv);
131 ir_mode *mode = get_irn_mode(cnst);
132 ir_type *tp = get_Const_type(cnst);
133 if (tp == firm_unknown_type)
134 tp = get_prim_type(cg->isa->types, mode);
136 res = new_entity(get_glob_type(), unique_id("ia32FloatCnst_%u"), tp);
138 set_entity_ld_ident(res, get_entity_ident(res));
139 set_entity_visibility(res, visibility_local);
140 set_entity_variability(res, variability_constant);
141 set_entity_allocation(res, allocation_static);
143 /* we create a new entity here: It's initialization must resist on the
145 rem = current_ir_graph;
146 current_ir_graph = get_const_code_irg();
147 set_atomic_ent_value(res, new_Const_type(tv, tp));
148 current_ir_graph = rem;
150 pmap_insert(cg->isa->tv_ent, tv, res);
158 * Transforms a Const.
160 * @param mod the debug module
161 * @param block the block the new node should belong to
162 * @param node the ir Const node
163 * @param mode mode of the Const
164 * @return the created ia32 Const node
166 static ir_node *gen_Const(ia32_transform_env_t *env) {
169 ir_graph *irg = env->irg;
170 ir_node *block = env->block;
171 ir_node *node = env->irn;
172 dbg_info *dbg = env->dbg;
173 ir_mode *mode = env->mode;
175 if (mode_is_float(mode)) {
177 if (! USE_SSE2(env->cg)) {
178 cnst_classify_t clss = classify_Const(node);
180 if (clss == CNST_NULL)
181 return new_rd_ia32_vfldz(dbg, irg, block, mode);
182 else if (clss == CNST_ONE)
183 return new_rd_ia32_vfld1(dbg, irg, block, mode);
185 sym.entity_p = get_entity_for_tv(env->cg, node);
187 cnst = new_rd_SymConst(dbg, irg, block, sym, symconst_addr_ent);
189 cnst = gen_SymConst(env);
192 cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), get_irn_mode(node));
193 set_ia32_Const_attr(cnst, node);
201 * Transforms (all) Const's into ia32_Const and places them in the
202 * block where they are used (or in the cfg-pred Block in case of Phi's).
203 * Additionally all reference nodes are changed into mode_Is nodes.
205 void ia32_place_consts_set_modes(ir_node *irn, void *env) {
206 ia32_code_gen_t *cg = env;
207 ia32_transform_env_t tenv;
209 ir_node *pred, *cnst;
216 mode = get_irn_mode(irn);
218 /* transform all reference nodes into mode_Is nodes */
219 if (mode_is_reference(mode)) {
221 set_irn_mode(irn, mode);
224 tenv.block = get_nodes_block(irn);
227 DEBUG_ONLY(tenv.mod = cg->mod;)
229 /* Loop over all predecessors and check for Sym/Const nodes */
230 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
231 pred = get_irn_n(irn, i);
233 opc = get_irn_opcode(pred);
235 tenv.mode = get_irn_mode(pred);
236 tenv.dbg = get_irn_dbg_info(pred);
238 /* If it's a Phi, then we need to create the */
239 /* new Const in it's predecessor block */
241 tenv.block = get_Block_cfgpred_block(get_nodes_block(irn), i);
244 /* put the const into the block where the original const was */
245 if (! (cg->opt & IA32_OPT_PLACECNST)) {
246 tenv.block = get_nodes_block(pred);
251 cnst = gen_Const(&tenv);
254 cnst = gen_SymConst(&tenv);
260 /* if we found a const, then set it */
262 set_irn_n(irn, i, cnst);
269 /********************************************************************************************************
270 * _____ _ _ ____ _ _ _ _ _
271 * | __ \ | | | | / __ \ | | (_) (_) | | (_)
272 * | |__) |__ ___ _ __ | |__ ___ | | ___ | | | |_ __ | |_ _ _ __ ___ _ ______ _| |_ _ ___ _ __
273 * | ___/ _ \/ _ \ '_ \| '_ \ / _ \| |/ _ \ | | | | '_ \| __| | '_ ` _ \| |_ / _` | __| |/ _ \| '_ \
274 * | | | __/ __/ |_) | | | | (_) | | __/ | |__| | |_) | |_| | | | | | | |/ / (_| | |_| | (_) | | | |
275 * |_| \___|\___| .__/|_| |_|\___/|_|\___| \____/| .__/ \__|_|_| |_| |_|_/___\__,_|\__|_|\___/|_| |_|
278 ********************************************************************************************************/
281 * NOTE: THESE PEEPHOLE OPTIMIZATIONS MUST BE CALLED AFTER SCHEDULING AND REGISTER ALLOCATION.
284 static int ia32_cnst_compare(ir_node *n1, ir_node *n2) {
285 return get_ia32_id_cnst(n1) == get_ia32_id_cnst(n2);
289 * Checks for potential CJmp/CJmpAM optimization candidates.
291 static ir_node *ia32_determine_cjmp_cand(ir_node *irn, is_op_func_t *is_op_func) {
292 ir_node *cand = NULL;
293 ir_node *prev = sched_prev(irn);
295 if (is_Block(prev)) {
296 if (get_Block_n_cfgpreds(prev) == 1)
297 prev = get_Block_cfgpred(prev, 0);
302 /* The predecessor must be a ProjX. */
303 if (prev && is_Proj(prev) && get_irn_mode(prev) == mode_X) {
304 prev = get_Proj_pred(prev);
306 if (is_op_func(prev))
313 static int is_TestJmp_cand(const ir_node *irn) {
314 return is_ia32_TestJmp(irn) || is_ia32_And(irn);
318 * Checks if two consecutive arguments of cand matches
319 * the two arguments of irn (TestJmp).
321 static int is_TestJmp_replacement(ir_node *cand, ir_node *irn) {
322 ir_node *in1 = get_irn_n(irn, 0);
323 ir_node *in2 = get_irn_n(irn, 1);
324 int i, n = get_irn_arity(cand);
327 for (i = 0; i < n - 1; i++) {
328 if (get_irn_n(cand, i) == in1 &&
329 get_irn_n(cand, i + 1) == in2)
337 return ia32_cnst_compare(cand, irn);
343 * Tries to replace a TestJmp by a CJmp or CJmpAM (in case of And)
345 static void ia32_optimize_TestJmp(ir_node *irn, ia32_code_gen_t *cg) {
346 ir_node *cand = ia32_determine_cjmp_cand(irn, is_TestJmp_cand);
349 /* we found a possible candidate */
350 replace = cand ? is_TestJmp_replacement(cand, irn) : 0;
353 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
355 if (is_ia32_And(cand))
356 set_irn_op(irn, op_ia32_CJmpAM);
358 set_irn_op(irn, op_ia32_CJmp);
360 DB((cg->mod, LEVEL_1, "%+F\n", irn));
364 static int is_CondJmp_cand(const ir_node *irn) {
365 return is_ia32_CondJmp(irn) || is_ia32_Sub(irn);
369 * Checks if the arguments of cand are the same of irn.
371 static int is_CondJmp_replacement(ir_node *cand, ir_node *irn) {
372 int i, n = get_irn_arity(cand);
375 for (i = 0; i < n; i++) {
376 if (get_irn_n(cand, i) != get_irn_n(irn, i)) {
383 return ia32_cnst_compare(cand, irn);
389 * Tries to replace a CondJmp by a CJmpAM
391 static void ia32_optimize_CondJmp(ir_node *irn, ia32_code_gen_t *cg) {
392 ir_node *cand = ia32_determine_cjmp_cand(irn, is_CondJmp_cand);
395 /* we found a possible candidate */
396 replace = cand ? is_CondJmp_replacement(cand, irn) : 0;
399 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
402 set_irn_op(irn, op_ia32_CJmpAM);
404 DB((cg->mod, LEVEL_1, "%+F\n", irn));
409 * Creates a Push from Store(IncSP(gp_reg_size))
411 static void ia32_create_Push(ir_node *irn, ia32_code_gen_t *cg) {
412 ir_node *sp = get_irn_n(irn, 0);
413 ir_node *val, *next, *push, *bl, *proj_M, *proj_res, *old_proj_M;
414 const ir_edge_t *edge;
416 if (get_ia32_am_offs(irn) || !be_is_IncSP(sp))
419 if (arch_get_irn_register(cg->arch_env, get_irn_n(irn, 1)) !=
420 &ia32_gp_regs[REG_GP_NOREG])
423 val = get_irn_n(irn, 2);
424 if (mode_is_float(get_irn_mode(val)))
427 if (be_get_IncSP_direction(sp) != be_stack_dir_expand ||
428 be_get_IncSP_offset(sp) != get_mode_size_bytes(ia32_reg_classes[CLASS_ia32_gp].mode))
431 /* ok, translate into Push */
432 edge = get_irn_out_edge_first(irn);
433 old_proj_M = get_edge_src_irn(edge);
435 next = sched_next(irn);
439 bl = get_nodes_block(irn);
440 push = new_rd_ia32_Push(NULL, current_ir_graph, bl,
441 be_get_IncSP_pred(sp), val, be_get_IncSP_mem(sp));
442 proj_res = new_r_Proj(current_ir_graph, bl, push, get_irn_mode(sp), pn_ia32_Push_stack);
443 proj_M = new_r_Proj(current_ir_graph, bl, push, mode_M, pn_ia32_Push_M);
445 /* copy a possible constant from the store */
446 set_ia32_id_cnst(push, get_ia32_id_cnst(irn));
447 set_ia32_immop_type(push, get_ia32_immop_type(irn));
449 /* the push must have SP out register */
450 arch_set_irn_register(cg->arch_env, push, arch_get_irn_register(cg->arch_env, sp));
452 exchange(old_proj_M, proj_M);
453 exchange(sp, proj_res);
454 sched_add_before(next, push);
455 sched_add_after(push, proj_res);
459 * Creates a Pop from IncSP(Load(sp))
461 static void ia32_create_Pop(ir_node *irn, ia32_code_gen_t *cg) {
462 ir_node *old_proj_M = be_get_IncSP_mem(irn);
463 ir_node *load = skip_Proj(old_proj_M);
464 ir_node *old_proj_res = NULL;
465 ir_node *bl, *pop, *next, *proj_res, *proj_sp, *proj_M;
466 const ir_edge_t *edge;
467 const arch_register_t *reg, *sp;
469 if (! is_ia32_Load(load) || get_ia32_am_offs(load))
472 if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 1)) !=
473 &ia32_gp_regs[REG_GP_NOREG])
475 if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 0)) != cg->isa->arch_isa.sp)
478 /* ok, translate into pop */
479 foreach_out_edge(load, edge) {
480 ir_node *succ = get_edge_src_irn(edge);
481 if (succ != old_proj_M) {
486 if (! old_proj_res) {
488 return; /* should not happen */
491 bl = get_nodes_block(load);
493 /* IncSP is typically scheduled after the load, so remove it first */
495 next = sched_next(old_proj_res);
496 sched_remove(old_proj_res);
499 reg = arch_get_irn_register(cg->arch_env, load);
500 sp = arch_get_irn_register(cg->arch_env, irn);
502 pop = new_rd_ia32_Pop(NULL, current_ir_graph, bl, get_irn_n(irn, 0), get_irn_n(load, 2));
503 proj_res = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(old_proj_res), pn_ia32_Pop_res);
504 proj_sp = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(irn), pn_ia32_Pop_stack);
505 proj_M = new_r_Proj(current_ir_graph, bl, pop, mode_M, pn_ia32_Pop_M);
507 exchange(old_proj_M, proj_M);
508 exchange(old_proj_res, proj_res);
509 exchange(irn, proj_sp);
511 arch_set_irn_register(cg->arch_env, proj_res, reg);
512 arch_set_irn_register(cg->arch_env, proj_sp, sp);
514 sched_add_before(next, proj_sp);
515 sched_add_before(proj_sp, proj_res);
516 sched_add_before(proj_res,pop);
520 * Tries to optimize two following IncSP.
522 static void ia32_optimize_IncSP(ir_node *irn, ia32_code_gen_t *cg) {
523 ir_node *prev = be_get_IncSP_pred(irn);
524 int real_uses = get_irn_n_edges(prev);
526 if (be_is_IncSP(prev) && real_uses == 1) {
527 /* first IncSP has only one IncSP user, kill the first one */
528 unsigned prev_offs = be_get_IncSP_offset(prev);
529 be_stack_dir_t prev_dir = be_get_IncSP_direction(prev);
530 unsigned curr_offs = be_get_IncSP_offset(irn);
531 be_stack_dir_t curr_dir = be_get_IncSP_direction(irn);
533 int new_ofs = prev_offs * (prev_dir == be_stack_dir_expand ? -1 : +1) +
534 curr_offs * (curr_dir == be_stack_dir_expand ? -1 : +1);
538 curr_dir = be_stack_dir_expand;
541 curr_dir = be_stack_dir_shrink;
542 be_set_IncSP_offset(prev, 0);
543 be_set_IncSP_offset(irn, (unsigned)new_ofs);
544 be_set_IncSP_direction(irn, curr_dir);
546 /* Omit the optimized IncSP */
547 be_set_IncSP_pred(irn, be_get_IncSP_pred(prev));
552 * Performs Peephole Optimizations.
554 void ia32_peephole_optimization(ir_node *irn, void *env) {
555 ia32_code_gen_t *cg = env;
557 if (is_ia32_TestJmp(irn))
558 ia32_optimize_TestJmp(irn, cg);
559 else if (is_ia32_CondJmp(irn))
560 ia32_optimize_CondJmp(irn, cg);
561 /* seems to be buggy when using Pushes */
562 // else if (be_is_IncSP(irn))
563 // ia32_optimize_IncSP(irn, cg);
564 else if (is_ia32_Store(irn))
565 ia32_create_Push(irn, cg);
570 /******************************************************************
572 * /\ | | | | | \/ | | |
573 * / \ __| | __| |_ __ ___ ___ ___| \ / | ___ __| | ___
574 * / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
575 * / ____ \ (_| | (_| | | | __/\__ \__ \ | | | (_) | (_| | __/
576 * /_/ \_\__,_|\__,_|_| \___||___/___/_| |_|\___/ \__,_|\___|
578 ******************************************************************/
580 static int node_is_ia32_comm(const ir_node *irn) {
581 return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
584 static int ia32_get_irn_n_edges(const ir_node *irn) {
585 const ir_edge_t *edge;
588 foreach_out_edge(irn, edge) {
596 * Returns the first mode_M Proj connected to irn.
598 static ir_node *get_mem_proj(const ir_node *irn) {
599 const ir_edge_t *edge;
602 assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
604 foreach_out_edge(irn, edge) {
605 src = get_edge_src_irn(edge);
607 assert(is_Proj(src) && "Proj expected");
609 if (get_irn_mode(src) == mode_M)
617 * Returns the first Proj with mode != mode_M connected to irn.
619 static ir_node *get_res_proj(const ir_node *irn) {
620 const ir_edge_t *edge;
623 assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
625 foreach_out_edge(irn, edge) {
626 src = get_edge_src_irn(edge);
628 assert(is_Proj(src) && "Proj expected");
630 if (get_irn_mode(src) != mode_M)
638 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
640 * @param pred The node to be checked
641 * @param is_op_func The check-function
642 * @return 1 if conditions are fulfilled, 0 otherwise
644 static int pred_is_specific_node(const ir_node *pred, is_op_func_t *is_op_func) {
645 if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
653 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
654 * and if the predecessor is in block bl.
656 * @param bl The block
657 * @param pred The node to be checked
658 * @param is_op_func The check-function
659 * @return 1 if conditions are fulfilled, 0 otherwise
661 static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
662 int (*is_op_func)(const ir_node *n))
665 pred = get_Proj_pred(pred);
666 if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
677 * Checks if irn is a candidate for address calculation or address mode.
679 * address calculation (AC):
680 * - none of the operand must be a Load within the same block OR
681 * - all Loads must have more than one user OR
682 * - the irn has a frame entity (it's a former FrameAddr)
685 * - at least one operand has to be a Load within the same block AND
686 * - the load must not have other users than the irn AND
687 * - the irn must not have a frame entity set
689 * @param block The block the Loads must/not be in
690 * @param irn The irn to check
691 * @param check_addr 1 if to check for address calculation, 0 otherwise
692 * return 1 if irn is a candidate for AC or AM, 0 otherwise
694 static int is_candidate(const ir_node *block, const ir_node *irn, int check_addr) {
696 int n, is_cand = check_addr;
698 in = get_irn_n(irn, 2);
700 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
701 n = ia32_get_irn_n_edges(in);
702 is_cand = check_addr ? (n == 1 ? 0 : is_cand) : (n == 1 ? 1 : is_cand);
705 in = get_irn_n(irn, 3);
707 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
708 n = ia32_get_irn_n_edges(in);
709 is_cand = check_addr ? (n == 1 ? 0 : is_cand) : (n == 1 ? 1 : is_cand);
712 is_cand = get_ia32_frame_ent(irn) ? (check_addr ? 1 : 0) : is_cand;
718 * Compares the base and index addr and the load/store entities
719 * and returns 1 if they are equal.
721 static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
722 const ir_node *addr_b, const ir_node *addr_i)
724 int is_equal = (addr_b == get_irn_n(load, 0)) && (addr_i == get_irn_n(load, 1));
725 entity *lent = get_ia32_frame_ent(load);
726 entity *sent = get_ia32_frame_ent(store);
727 ident *lid = get_ia32_am_sc(load);
728 ident *sid = get_ia32_am_sc(store);
729 char *loffs = get_ia32_am_offs(load);
730 char *soffs = get_ia32_am_offs(store);
732 /* are both entities set and equal? */
733 if (is_equal && (lent || sent))
734 is_equal = lent && sent && (lent == sent);
736 /* are address mode idents set and equal? */
737 if (is_equal && (lid || sid))
738 is_equal = lid && sid && (lid == sid);
740 /* are offsets set and equal */
741 if (is_equal && (loffs || soffs))
742 is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;
744 /* are the load and the store of the same mode? */
745 is_equal = is_equal ? get_ia32_ls_mode(load) == get_ia32_ls_mode(store) : 0;
750 typedef enum _ia32_take_lea_attr {
751 IA32_LEA_ATTR_NONE = 0,
752 IA32_LEA_ATTR_BASE = (1 << 0),
753 IA32_LEA_ATTR_INDEX = (1 << 1),
754 IA32_LEA_ATTR_OFFS = (1 << 2),
755 IA32_LEA_ATTR_SCALE = (1 << 3),
756 IA32_LEA_ATTR_AMSC = (1 << 4),
757 IA32_LEA_ATTR_FENT = (1 << 5)
758 } ia32_take_lea_attr;
761 * Decides if we have to keep the LEA operand or if we can assimilate it.
763 static int do_new_lea(ir_node *irn, ir_node *base, ir_node *index, ir_node *lea,
764 int have_am_sc, ia32_code_gen_t *cg)
766 ir_node *lea_base = get_irn_n(lea, 0);
767 ir_node *lea_idx = get_irn_n(lea, 1);
768 entity *irn_ent = get_ia32_frame_ent(irn);
769 entity *lea_ent = get_ia32_frame_ent(lea);
771 int is_noreg_base = be_is_NoReg(cg, base);
772 int is_noreg_index = be_is_NoReg(cg, index);
773 ia32_am_flavour_t am_flav = get_ia32_am_flavour(lea);
775 /* If the Add and the LEA both have a different frame entity set: keep */
776 if (irn_ent && lea_ent && (irn_ent != lea_ent))
777 return IA32_LEA_ATTR_NONE;
778 else if (! irn_ent && lea_ent)
779 ret_val |= IA32_LEA_ATTR_FENT;
781 /* If the Add and the LEA both have already an address mode symconst: keep */
782 if (have_am_sc && get_ia32_am_sc(lea))
783 return IA32_LEA_ATTR_NONE;
784 else if (get_ia32_am_sc(lea))
785 ret_val |= IA32_LEA_ATTR_AMSC;
787 /* Check the different base-index combinations */
789 if (! is_noreg_base && ! is_noreg_index) {
790 /* Assimilate if base is the lea and the LEA is just a Base + Offset calculation */
791 if ((base == lea) && ! (am_flav & ia32_I ? 1 : 0)) {
792 if (am_flav & ia32_O)
793 ret_val |= IA32_LEA_ATTR_OFFS;
795 ret_val |= IA32_LEA_ATTR_BASE;
798 return IA32_LEA_ATTR_NONE;
800 else if (! is_noreg_base && is_noreg_index) {
801 /* Base is set but index not */
803 /* Base points to LEA: assimilate everything */
804 if (am_flav & ia32_O)
805 ret_val |= IA32_LEA_ATTR_OFFS;
806 if (am_flav & ia32_S)
807 ret_val |= IA32_LEA_ATTR_SCALE;
808 if (am_flav & ia32_I)
809 ret_val |= IA32_LEA_ATTR_INDEX;
811 ret_val |= IA32_LEA_ATTR_BASE;
813 else if (am_flav & ia32_B ? 0 : 1) {
814 /* Base is not the LEA but the LEA is an index only calculation: assimilate */
815 if (am_flav & ia32_O)
816 ret_val |= IA32_LEA_ATTR_OFFS;
817 if (am_flav & ia32_S)
818 ret_val |= IA32_LEA_ATTR_SCALE;
820 ret_val |= IA32_LEA_ATTR_INDEX;
823 return IA32_LEA_ATTR_NONE;
825 else if (is_noreg_base && ! is_noreg_index) {
826 /* Index is set but not base */
828 /* Index points to LEA: assimilate everything */
829 if (am_flav & ia32_O)
830 ret_val |= IA32_LEA_ATTR_OFFS;
831 if (am_flav & ia32_S)
832 ret_val |= IA32_LEA_ATTR_SCALE;
833 if (am_flav & ia32_B)
834 ret_val |= IA32_LEA_ATTR_BASE;
836 ret_val |= IA32_LEA_ATTR_INDEX;
838 else if (am_flav & ia32_I ? 0 : 1) {
839 /* Index is not the LEA but the LEA is a base only calculation: assimilate */
840 if (am_flav & ia32_O)
841 ret_val |= IA32_LEA_ATTR_OFFS;
842 if (am_flav & ia32_S)
843 ret_val |= IA32_LEA_ATTR_SCALE;
845 ret_val |= IA32_LEA_ATTR_BASE;
848 return IA32_LEA_ATTR_NONE;
851 assert(0 && "There must have been set base or index");
859 * Folds Add or Sub to LEA if possible
861 static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn, ir_node *noreg) {
862 ir_graph *irg = get_irn_irg(irn);
863 dbg_info *dbg = get_irn_dbg_info(irn);
864 ir_node *block = get_nodes_block(irn);
866 ir_node *shift = NULL;
867 ir_node *lea_o = NULL;
870 const char *offs_cnst = NULL;
871 char *offs_lea = NULL;
878 entity *lea_ent = NULL;
879 ir_node *left, *right, *temp;
880 ir_node *base, *index;
881 ia32_am_flavour_t am_flav;
882 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
884 if (is_ia32_Add(irn))
887 left = get_irn_n(irn, 2);
888 right = get_irn_n(irn, 3);
890 /* "normalize" arguments in case of add with two operands */
891 if (isadd && ! be_is_NoReg(cg, right)) {
892 /* put LEA == ia32_am_O as right operand */
893 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
894 set_irn_n(irn, 2, right);
895 set_irn_n(irn, 3, left);
901 /* put LEA != ia32_am_O as left operand */
902 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
903 set_irn_n(irn, 2, right);
904 set_irn_n(irn, 3, left);
910 /* put SHL as left operand iff left is NOT a LEA */
911 if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
912 set_irn_n(irn, 2, right);
913 set_irn_n(irn, 3, left);
926 /* check for operation with immediate */
927 if (is_ia32_ImmConst(irn)) {
928 DBG((mod, LEVEL_1, "\tfound op with imm const"));
930 offs_cnst = get_ia32_cnst(irn);
933 else if (is_ia32_ImmSymConst(irn)) {
934 DBG((mod, LEVEL_1, "\tfound op with imm symconst"));
938 am_sc = get_ia32_id_cnst(irn);
939 am_sc_sign = is_ia32_am_sc_sign(irn);
942 /* determine the operand which needs to be checked */
943 if (be_is_NoReg(cg, right)) {
950 /* check if right operand is AMConst (LEA with ia32_am_O) */
951 /* but we can only eat it up if there is no other symconst */
952 /* because the linker won't accept two symconsts */
953 if (! have_am_sc && is_ia32_Lea(temp) && get_ia32_am_flavour(temp) == ia32_am_O) {
954 DBG((mod, LEVEL_1, "\tgot op with LEA am_O"));
956 offs_lea = get_ia32_am_offs(temp);
957 am_sc = get_ia32_am_sc(temp);
958 am_sc_sign = is_ia32_am_sc_sign(temp);
965 /* default for add -> make right operand to index */
969 DBG((mod, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
971 /* determine the operand which needs to be checked */
973 if (is_ia32_Lea(left)) {
977 /* check for SHL 1,2,3 */
978 if (pred_is_specific_node(temp, is_ia32_Shl)) {
979 temp = get_Proj_pred(temp);
982 if (get_ia32_Immop_tarval(temp)) {
983 scale = get_tarval_long(get_ia32_Immop_tarval(temp));
986 index = get_irn_n(temp, 2);
988 DBG((mod, LEVEL_1, "\tgot scaled index %+F\n", index));
998 if (! be_is_NoReg(cg, index)) {
999 /* if we have index, but left == right -> no base */
1000 if (left == right) {
1003 else if (! is_ia32_Lea(left) && (index != right)) {
1004 /* index != right -> we found a good Shl */
1005 /* left != LEA -> this Shl was the left operand */
1006 /* -> base is right operand */
1012 /* Try to assimilate a LEA as left operand */
1013 if (is_ia32_Lea(left) && (get_ia32_am_flavour(left) != ia32_am_O)) {
1014 /* check if we can assimilate the LEA */
1015 int take_attr = do_new_lea(irn, base, index, left, have_am_sc, cg);
1017 if (take_attr == IA32_LEA_ATTR_NONE) {
1018 DBG((mod, LEVEL_1, "\tleave old LEA, creating new one\n"));
1021 DBG((mod, LEVEL_1, "\tgot LEA as left operand ... assimilating\n"));
1022 lea = left; /* for statistics */
1024 if (take_attr & IA32_LEA_ATTR_OFFS)
1025 offs = get_ia32_am_offs(left);
1027 if (take_attr & IA32_LEA_ATTR_AMSC) {
1028 am_sc = get_ia32_am_sc(left);
1030 am_sc_sign = is_ia32_am_sc_sign(left);
1033 if (take_attr & IA32_LEA_ATTR_SCALE)
1034 scale = get_ia32_am_scale(left);
1036 if (take_attr & IA32_LEA_ATTR_BASE)
1037 base = get_irn_n(left, 0);
1039 if (take_attr & IA32_LEA_ATTR_INDEX)
1040 index = get_irn_n(left, 1);
1042 if (take_attr & IA32_LEA_ATTR_FENT)
1043 lea_ent = get_ia32_frame_ent(left);
1047 /* ok, we can create a new LEA */
1049 res = new_rd_ia32_Lea(dbg, irg, block, base, index, mode_Is);
1051 /* add the old offset of a previous LEA */
1053 add_ia32_am_offs(res, offs);
1056 /* add the new offset */
1059 add_ia32_am_offs(res, offs_cnst);
1062 add_ia32_am_offs(res, offs_lea);
1066 /* either lea_O-cnst, -cnst or -lea_O */
1069 add_ia32_am_offs(res, offs_lea);
1072 sub_ia32_am_offs(res, offs_cnst);
1075 sub_ia32_am_offs(res, offs_lea);
1079 /* set the address mode symconst */
1081 set_ia32_am_sc(res, am_sc);
1083 set_ia32_am_sc_sign(res);
1086 /* copy the frame entity (could be set in case of Add */
1087 /* which was a FrameAddr) */
1089 set_ia32_frame_ent(res, lea_ent);
1091 set_ia32_frame_ent(res, get_ia32_frame_ent(irn));
1093 if (get_ia32_frame_ent(res))
1094 set_ia32_use_frame(res);
1097 set_ia32_am_scale(res, scale);
1099 am_flav = ia32_am_N;
1100 /* determine new am flavour */
1101 if (offs || offs_cnst || offs_lea) {
1104 if (! be_is_NoReg(cg, base)) {
1107 if (! be_is_NoReg(cg, index)) {
1113 set_ia32_am_flavour(res, am_flav);
1115 set_ia32_op_type(res, ia32_AddrModeS);
1117 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
1119 DBG((mod, LEVEL_1, "\tLEA [%+F + %+F * %d + %s]\n", base, index, scale, get_ia32_am_offs(res)));
1121 /* we will exchange it, report here before the Proj is created */
1122 if (shift && lea && lea_o)
1123 DBG_OPT_LEA4(irn, lea_o, lea, shift, res);
1124 else if (shift && lea)
1125 DBG_OPT_LEA3(irn, lea, shift, res);
1126 else if (shift && lea_o)
1127 DBG_OPT_LEA3(irn, lea_o, shift, res);
1128 else if (lea && lea_o)
1129 DBG_OPT_LEA3(irn, lea_o, lea, res);
1131 DBG_OPT_LEA2(irn, shift, res);
1133 DBG_OPT_LEA2(irn, lea, res);
1135 DBG_OPT_LEA2(irn, lea_o, res);
1137 DBG_OPT_LEA1(irn, res);
1139 /* get the result Proj of the Add/Sub */
1140 irn = get_res_proj(irn);
1142 assert(irn && "Couldn't find result proj");
1144 /* exchange the old op with the new LEA */
1153 * Merges a Load/Store node with a LEA.
1154 * @param irn The Load/Store node
1155 * @param lea The LEA
1157 static void merge_loadstore_lea(ir_node *irn, ir_node *lea) {
1158 entity *irn_ent = get_ia32_frame_ent(irn);
1159 entity *lea_ent = get_ia32_frame_ent(lea);
1161 /* If the irn and the LEA both have a different frame entity set: do not merge */
1162 if (irn_ent && lea_ent && (irn_ent != lea_ent))
1164 else if (! irn_ent && lea_ent) {
1165 set_ia32_frame_ent(irn, lea_ent);
1166 set_ia32_use_frame(irn);
1169 /* get the AM attributes from the LEA */
1170 add_ia32_am_offs(irn, get_ia32_am_offs(lea));
1171 set_ia32_am_scale(irn, get_ia32_am_scale(lea));
1172 set_ia32_am_flavour(irn, get_ia32_am_flavour(lea));
1174 set_ia32_am_sc(irn, get_ia32_am_sc(lea));
1175 if (is_ia32_am_sc_sign(lea))
1176 set_ia32_am_sc_sign(irn);
1178 set_ia32_op_type(irn, is_ia32_Ld(irn) ? ia32_AddrModeS : ia32_AddrModeD);
1180 /* set base and index */
1181 set_irn_n(irn, 0, get_irn_n(lea, 0));
1182 set_irn_n(irn, 1, get_irn_n(lea, 1));
1184 /* clear remat flag */
1185 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1187 if (is_ia32_Ld(irn))
1188 DBG_OPT_LOAD_LEA(lea, irn);
1190 DBG_OPT_STORE_LEA(lea, irn);
1195 * Sets new_right index of irn to right and new_left index to left.
1196 * Also exchange left and right
1198 static void exchange_left_right(ir_node *irn, ir_node **left, ir_node **right, int new_left, int new_right) {
1201 set_irn_n(irn, new_right, *right);
1202 set_irn_n(irn, new_left, *left);
1208 /* this is only needed for Compares, but currently ALL nodes
1209 * have this attribute :-) */
1210 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1214 * Optimizes a pattern around irn to address mode if possible.
1216 void ia32_optimize_am(ir_node *irn, void *env) {
1217 ia32_code_gen_t *cg = env;
1221 ir_node *block, *noreg_gp, *noreg_fp;
1222 ir_node *left, *right, *temp;
1223 ir_node *store, *load, *mem_proj;
1224 ir_node *succ, *addr_b, *addr_i;
1225 int check_am_src = 0;
1226 int need_exchange_on_fail = 0;
1227 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
1229 if (! is_ia32_irn(irn))
1232 dbg = get_irn_dbg_info(irn);
1233 mode = get_irn_mode(irn);
1234 block = get_nodes_block(irn);
1235 noreg_gp = ia32_new_NoReg_gp(cg);
1236 noreg_fp = ia32_new_NoReg_fp(cg);
1238 DBG((mod, LEVEL_1, "checking for AM\n"));
1240 /* 1st part: check for address calculations and transform the into Lea */
1242 /* Following cases can occur: */
1243 /* - Sub (l, imm) -> LEA [base - offset] */
1244 /* - Sub (l, r == LEA with ia32_am_O) -> LEA [base - offset] */
1245 /* - Add (l, imm) -> LEA [base + offset] */
1246 /* - Add (l, r == LEA with ia32_am_O) -> LEA [base + offset] */
1247 /* - Add (l == LEA with ia32_am_O, r) -> LEA [base + offset] */
1248 /* - Add (l, r) -> LEA [base + index * scale] */
1249 /* with scale > 1 iff l/r == shl (1,2,3) */
1251 if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
1252 left = get_irn_n(irn, 2);
1253 right = get_irn_n(irn, 3);
1255 /* Do not try to create a LEA if one of the operands is a Load. */
1256 /* check is irn is a candidate for address calculation */
1257 if (is_candidate(block, irn, 1)) {
1258 DBG((mod, LEVEL_1, "\tfound address calculation candidate %+F ... ", irn));
1259 res = fold_addr(cg, irn, noreg_gp);
1262 DB((mod, LEVEL_1, "transformed into %+F\n", res));
1264 DB((mod, LEVEL_1, "not transformed\n"));
1268 /* 2nd part: fold following patterns: */
1269 /* - Load -> LEA into Load } TODO: If the LEA is used by more than one Load/Store */
1270 /* - Store -> LEA into Store } it might be better to keep the LEA */
1271 /* - op -> Load into AMop with am_Source */
1273 /* - op is am_Source capable AND */
1274 /* - the Load is only used by this op AND */
1275 /* - the Load is in the same block */
1276 /* - Store -> op -> Load into AMop with am_Dest */
1278 /* - op is am_Dest capable AND */
1279 /* - the Store uses the same address as the Load AND */
1280 /* - the Load is only used by this op AND */
1281 /* - the Load and Store are in the same block AND */
1282 /* - nobody else uses the result of the op */
1284 if ((res == irn) && (get_ia32_am_support(irn) != ia32_am_None) && !is_ia32_Lea(irn)) {
1285 /* 1st: check for Load/Store -> LEA */
1286 if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn)) {
1287 left = get_irn_n(irn, 0);
1289 if (is_ia32_Lea(left)) {
1290 const ir_edge_t *edge, *ne;
1293 /* merge all Loads/Stores connected to this LEA with the LEA */
1294 foreach_out_edge_safe(left, edge, ne) {
1295 src = get_edge_src_irn(edge);
1297 if (src && (is_ia32_Ld(src) || is_ia32_St(src) || is_ia32_Store8Bit(src))) {
1298 DBG((mod, LEVEL_1, "\nmerging %+F into %+F\n", left, irn));
1299 merge_loadstore_lea(src, left);
1304 /* check if the node is an address mode candidate */
1305 else if (is_candidate(block, irn, 0)) {
1306 DBG((mod, LEVEL_1, "\tfound address mode candidate %+F ... ", irn));
1308 left = get_irn_n(irn, 2);
1309 if (get_irn_arity(irn) == 4) {
1310 /* it's an "unary" operation */
1314 right = get_irn_n(irn, 3);
1317 /* normalize commutative ops */
1318 if (node_is_ia32_comm(irn)) {
1319 /* Assure that right operand is always a Load if there is one */
1320 /* because non-commutative ops can only use Dest AM if the right */
1321 /* operand is a load, so we only need to check right operand. */
1322 if (pred_is_specific_nodeblock(block, left, is_ia32_Ld))
1324 exchange_left_right(irn, &left, &right, 3, 2);
1325 need_exchange_on_fail = 1;
1329 /* check for Store -> op -> Load */
1331 /* Store -> op -> Load optimization is only possible if supported by op */
1332 /* and if right operand is a Load */
1333 if ((get_ia32_am_support(irn) & ia32_am_Dest) &&
1334 pred_is_specific_nodeblock(block, right, is_ia32_Ld))
1337 /* An address mode capable op always has a result Proj. */
1338 /* If this Proj is used by more than one other node, we don't need to */
1339 /* check further, otherwise we check for Store and remember the address, */
1340 /* the Store points to. */
1342 succ = get_res_proj(irn);
1343 assert(succ && "Couldn't find result proj");
1349 /* now check for users and Store */
1350 if (ia32_get_irn_n_edges(succ) == 1) {
1351 succ = get_edge_src_irn(get_irn_out_edge_first(succ));
1353 if (is_ia32_xStore(succ) || is_ia32_Store(succ)) {
1355 addr_b = get_irn_n(store, 0);
1356 addr_i = get_irn_n(store, 1);
1361 /* we found a Store as single user: Now check for Load */
1363 /* Extra check for commutative ops with two Loads */
1364 /* -> put the interesting Load right */
1365 if (node_is_ia32_comm(irn) &&
1366 pred_is_specific_nodeblock(block, left, is_ia32_Ld))
1368 if ((addr_b == get_irn_n(get_Proj_pred(left), 0)) &&
1369 (addr_i == get_irn_n(get_Proj_pred(left), 1)))
1371 /* We exchange left and right, so it's easier to kill */
1372 /* the correct Load later and to handle unary operations. */
1373 set_irn_n(irn, 2, right);
1374 set_irn_n(irn, 3, left);
1380 /* this is only needed for Compares, but currently ALL nodes
1381 * have this attribute :-) */
1382 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1386 /* skip the Proj for easier access */
1387 load = get_Proj_pred(right);
1389 /* Compare Load and Store address */
1390 if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1391 /* Right Load is from same address, so we can */
1392 /* disconnect the Load and Store here */
1394 /* set new base, index and attributes */
1395 set_irn_n(irn, 0, addr_b);
1396 set_irn_n(irn, 1, addr_i);
1397 add_ia32_am_offs(irn, get_ia32_am_offs(load));
1398 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1399 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1400 set_ia32_op_type(irn, ia32_AddrModeD);
1401 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1402 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1404 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1405 if (is_ia32_am_sc_sign(load))
1406 set_ia32_am_sc_sign(irn);
1408 if (is_ia32_use_frame(load))
1409 set_ia32_use_frame(irn);
1411 /* connect to Load memory and disconnect Load */
1412 if (get_irn_arity(irn) == 5) {
1414 set_irn_n(irn, 4, get_irn_n(load, 2));
1415 set_irn_n(irn, 3, noreg_gp);
1419 set_irn_n(irn, 3, get_irn_n(load, 2));
1420 set_irn_n(irn, 2, noreg_gp);
1423 /* connect the memory Proj of the Store to the op */
1424 mem_proj = get_mem_proj(store);
1425 set_Proj_pred(mem_proj, irn);
1426 set_Proj_proj(mem_proj, 1);
1428 /* clear remat flag */
1429 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1431 DBG_OPT_AM_D(load, store, irn);
1433 DB((mod, LEVEL_1, "merged with %+F and %+F into dest AM\n", load, store));
1436 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1437 /* There was no store, check if we still can optimize for source address mode */
1440 } /* if (support AM Dest) */
1441 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1442 /* op doesn't support am AM Dest -> check for AM Source */
1446 /* was exchanged but optimize failed: exchange back */
1447 if (check_am_src && need_exchange_on_fail)
1448 exchange_left_right(irn, &left, &right, 3, 2);
1450 need_exchange_on_fail = 0;
1452 /* normalize commutative ops */
1453 if (check_am_src && node_is_ia32_comm(irn)) {
1454 /* Assure that left operand is always a Load if there is one */
1455 /* because non-commutative ops can only use Source AM if the */
1456 /* left operand is a Load, so we only need to check the left */
1457 /* operand afterwards. */
1458 if (pred_is_specific_nodeblock(block, right, is_ia32_Ld)) {
1459 exchange_left_right(irn, &left, &right, 3, 2);
1460 need_exchange_on_fail = 1;
1464 /* optimize op -> Load iff Load is only used by this op */
1465 /* and left operand is a Load which only used by this irn */
1467 pred_is_specific_nodeblock(block, left, is_ia32_Ld) &&
1468 (ia32_get_irn_n_edges(left) == 1))
1470 left = get_Proj_pred(left);
1472 addr_b = get_irn_n(left, 0);
1473 addr_i = get_irn_n(left, 1);
1475 /* set new base, index and attributes */
1476 set_irn_n(irn, 0, addr_b);
1477 set_irn_n(irn, 1, addr_i);
1478 add_ia32_am_offs(irn, get_ia32_am_offs(left));
1479 set_ia32_am_scale(irn, get_ia32_am_scale(left));
1480 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
1481 set_ia32_op_type(irn, ia32_AddrModeS);
1482 set_ia32_frame_ent(irn, get_ia32_frame_ent(left));
1483 set_ia32_ls_mode(irn, get_ia32_ls_mode(left));
1485 set_ia32_am_sc(irn, get_ia32_am_sc(left));
1486 if (is_ia32_am_sc_sign(left))
1487 set_ia32_am_sc_sign(irn);
1489 /* clear remat flag */
1490 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1492 if (is_ia32_use_frame(left))
1493 set_ia32_use_frame(irn);
1495 /* connect to Load memory */
1496 if (get_irn_arity(irn) == 5) {
1498 set_irn_n(irn, 4, get_irn_n(left, 2));
1500 /* this is only needed for Compares, but currently ALL nodes
1501 * have this attribute :-) */
1502 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1504 /* disconnect from Load */
1505 /* (make second op -> first, set second in to noreg) */
1506 set_irn_n(irn, 2, get_irn_n(irn, 3));
1507 set_irn_n(irn, 3, noreg_gp);
1511 set_irn_n(irn, 3, get_irn_n(left, 2));
1513 /* disconnect from Load */
1514 set_irn_n(irn, 2, noreg_gp);
1517 DBG_OPT_AM_S(left, irn);
1519 /* If Load has a memory Proj, connect it to the op */
1520 mem_proj = get_mem_proj(left);
1522 set_Proj_pred(mem_proj, irn);
1523 set_Proj_proj(mem_proj, 1);
1526 DB((mod, LEVEL_1, "merged with %+F into source AM\n", left));
1529 /* was exchanged but optimize failed: exchange back */
1530 if (need_exchange_on_fail)
1531 exchange_left_right(irn, &left, &right, 3, 2);