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) */
23 #define is_NoMem(irn) (get_irn_op(irn) == op_NoMem)
25 typedef int is_op_func_t(const ir_node *n);
28 * checks if a node represents the NOREG value
30 static int be_is_NoReg(ia32_code_gen_t *cg, const ir_node *irn) {
31 be_abi_irg_t *babi = cg->birg->abi;
32 const arch_register_t *fp_noreg = USE_SSE2(cg) ?
33 &ia32_xmm_regs[REG_XMM_NOREG] : &ia32_vfp_regs[REG_VFP_NOREG];
35 return (be_abi_get_callee_save_irn(babi, &ia32_gp_regs[REG_GP_NOREG]) == irn) ||
36 (be_abi_get_callee_save_irn(babi, fp_noreg) == irn);
41 /*************************************************
44 * | | ___ _ __ ___| |_ __ _ _ __ | |_ ___
45 * | | / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
46 * | |___| (_) | | | \__ \ || (_| | | | | |_\__ \
47 * \_____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
49 *************************************************/
52 * creates a unique ident by adding a number to a tag
54 * @param tag the tag string, must contain a %d if a number
57 static ident *unique_id(const char *tag)
59 static unsigned id = 0;
62 snprintf(str, sizeof(str), tag, ++id);
63 return new_id_from_str(str);
69 * Transforms a SymConst.
71 * @param mod the debug module
72 * @param block the block the new node should belong to
73 * @param node the ir SymConst node
74 * @param mode mode of the SymConst
75 * @return the created ia32 Const node
77 static ir_node *gen_SymConst(ia32_transform_env_t *env) {
79 dbg_info *dbg = env->dbg;
80 ir_mode *mode = env->mode;
81 ir_graph *irg = env->irg;
82 ir_node *block = env->block;
84 if (mode_is_float(mode)) {
85 if (USE_SSE2(env->cg))
86 cnst = new_rd_ia32_fConst(dbg, irg, block, mode);
88 cnst = new_rd_ia32_vfConst(dbg, irg, block, mode);
91 cnst = new_rd_ia32_Const(dbg, irg, block, mode);
93 set_ia32_Const_attr(cnst, env->irn);
98 * Get a primitive type for a mode.
100 static ir_type *get_prim_type(pmap *types, ir_mode *mode)
102 pmap_entry *e = pmap_find(types, mode);
107 snprintf(buf, sizeof(buf), "prim_type_%s", get_mode_name(mode));
108 res = new_type_primitive(new_id_from_str(buf), mode);
109 pmap_insert(types, mode, res);
117 * Get an entity that is initialized with a tarval
119 static entity *get_entity_for_tv(ia32_code_gen_t *cg, ir_node *cnst)
121 tarval *tv = get_Const_tarval(cnst);
122 pmap_entry *e = pmap_find(cg->tv_ent, tv);
127 ir_mode *mode = get_irn_mode(cnst);
128 ir_type *tp = get_Const_type(cnst);
129 if (tp == firm_unknown_type)
130 tp = get_prim_type(cg->types, mode);
132 res = new_entity(get_glob_type(), unique_id("ia32FloatCnst_%u"), tp);
134 set_entity_ld_ident(res, get_entity_ident(res));
135 set_entity_visibility(res, visibility_local);
136 set_entity_variability(res, variability_constant);
137 set_entity_allocation(res, allocation_static);
139 /* we create a new entity here: It's initialization must resist on the
141 rem = current_ir_graph;
142 current_ir_graph = get_const_code_irg();
143 set_atomic_ent_value(res, new_Const_type(tv, tp));
144 current_ir_graph = rem;
152 * Transforms a Const.
154 * @param mod the debug module
155 * @param block the block the new node should belong to
156 * @param node the ir Const node
157 * @param mode mode of the Const
158 * @return the created ia32 Const node
160 static ir_node *gen_Const(ia32_transform_env_t *env) {
163 ir_graph *irg = env->irg;
164 ir_node *block = env->block;
165 ir_node *node = env->irn;
166 dbg_info *dbg = env->dbg;
167 ir_mode *mode = env->mode;
169 if (mode_is_float(mode)) {
170 if (! USE_SSE2(env->cg)) {
171 cnst_classify_t clss = classify_Const(node);
173 if (clss == CNST_NULL)
174 return new_rd_ia32_vfldz(dbg, irg, block, mode);
175 else if (clss == CNST_ONE)
176 return new_rd_ia32_vfld1(dbg, irg, block, mode);
178 sym.entity_p = get_entity_for_tv(env->cg, node);
180 cnst = new_rd_SymConst(dbg, irg, block, sym, symconst_addr_ent);
182 cnst = gen_SymConst(env);
185 cnst = new_rd_ia32_Const(dbg, irg, block, get_irn_mode(node));
186 set_ia32_Const_attr(cnst, node);
194 * Transforms (all) Const's into ia32_Const and places them in the
195 * block where they are used (or in the cfg-pred Block in case of Phi's).
196 * Additionally all reference nodes are changed into mode_Is nodes.
198 void ia32_place_consts_set_modes(ir_node *irn, void *env) {
199 ia32_code_gen_t *cg = env;
200 ia32_transform_env_t tenv;
202 ir_node *pred, *cnst;
209 mode = get_irn_mode(irn);
211 /* transform all reference nodes into mode_Is nodes */
212 if (mode_is_reference(mode)) {
214 set_irn_mode(irn, mode);
217 tenv.block = get_nodes_block(irn);
220 DEBUG_ONLY(tenv.mod = cg->mod;)
222 /* Loop over all predecessors and check for Sym/Const nodes */
223 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
224 pred = get_irn_n(irn, i);
226 opc = get_irn_opcode(pred);
228 tenv.mode = get_irn_mode(pred);
229 tenv.dbg = get_irn_dbg_info(pred);
231 /* If it's a Phi, then we need to create the */
232 /* new Const in it's predecessor block */
234 tenv.block = get_Block_cfgpred_block(get_nodes_block(irn), i);
237 /* put the const into the block where the original const was */
238 if (! cg->opt.placecnst) {
239 tenv.block = get_nodes_block(pred);
244 cnst = gen_Const(&tenv);
247 cnst = gen_SymConst(&tenv);
253 /* if we found a const, then set it */
255 set_irn_n(irn, i, cnst);
262 /********************************************************************************************************
263 * _____ _ _ ____ _ _ _ _ _
264 * | __ \ | | | | / __ \ | | (_) (_) | | (_)
265 * | |__) |__ ___ _ __ | |__ ___ | | ___ | | | |_ __ | |_ _ _ __ ___ _ ______ _| |_ _ ___ _ __
266 * | ___/ _ \/ _ \ '_ \| '_ \ / _ \| |/ _ \ | | | | '_ \| __| | '_ ` _ \| |_ / _` | __| |/ _ \| '_ \
267 * | | | __/ __/ |_) | | | | (_) | | __/ | |__| | |_) | |_| | | | | | | |/ / (_| | |_| | (_) | | | |
268 * |_| \___|\___| .__/|_| |_|\___/|_|\___| \____/| .__/ \__|_|_| |_| |_|_/___\__,_|\__|_|\___/|_| |_|
271 ********************************************************************************************************/
274 * NOTE: THESE PEEPHOLE OPTIMIZATIONS MUST BE CALLED AFTER SCHEDULING AND REGISTER ALLOCATION.
277 static int ia32_cnst_compare(ir_node *n1, ir_node *n2) {
278 return get_ia32_id_cnst(n1) == get_ia32_id_cnst(n2);
282 * Checks for potential CJmp/CJmpAM optimization candidates.
284 static ir_node *ia32_determine_cjmp_cand(ir_node *irn, is_op_func_t *is_op_func) {
285 ir_node *cand = NULL;
286 ir_node *prev = sched_prev(irn);
288 if (is_Block(prev)) {
289 if (get_Block_n_cfgpreds(prev) == 1)
290 prev = get_Block_cfgpred(prev, 0);
295 /* The predecessor must be a ProjX. */
296 if (prev && is_Proj(prev) && get_irn_mode(prev) == mode_X) {
297 prev = get_Proj_pred(prev);
299 if (is_op_func(prev))
306 static int is_TestJmp_cand(const ir_node *irn) {
307 return is_ia32_TestJmp(irn) || is_ia32_And(irn);
311 * Checks if two consecutive arguments of cand matches
312 * the two arguments of irn (TestJmp).
314 static int is_TestJmp_replacement(ir_node *cand, ir_node *irn) {
315 ir_node *in1 = get_irn_n(irn, 0);
316 ir_node *in2 = get_irn_n(irn, 1);
317 int i, n = get_irn_arity(cand);
320 for (i = 0; i < n - 1; i++) {
321 if (get_irn_n(cand, i) == in1 &&
322 get_irn_n(cand, i + 1) == in2)
330 return ia32_cnst_compare(cand, irn);
336 * Tries to replace a TestJmp by a CJmp or CJmpAM (in case of And)
338 static void ia32_optimize_TestJmp(ir_node *irn, ia32_code_gen_t *cg) {
339 ir_node *cand = ia32_determine_cjmp_cand(irn, is_TestJmp_cand);
342 /* we found a possible candidate */
343 replace = cand ? is_TestJmp_replacement(cand, irn) : 0;
346 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
348 if (is_ia32_And(cand))
349 set_irn_op(irn, op_ia32_CJmpAM);
351 set_irn_op(irn, op_ia32_CJmp);
353 DB((cg->mod, LEVEL_1, "%+F\n", irn));
357 static int is_CondJmp_cand(const ir_node *irn) {
358 return is_ia32_CondJmp(irn) || is_ia32_Sub(irn);
362 * Checks if the arguments of cand are the same of irn.
364 static int is_CondJmp_replacement(ir_node *cand, ir_node *irn) {
365 int i, n = get_irn_arity(cand);
368 for (i = 0; i < n; i++) {
369 if (get_irn_n(cand, i) == get_irn_n(irn, i)) {
376 return ia32_cnst_compare(cand, irn);
382 * Tries to replace a CondJmp by a CJmpAM
384 static void ia32_optimize_CondJmp(ir_node *irn, ia32_code_gen_t *cg) {
385 ir_node *cand = ia32_determine_cjmp_cand(irn, is_CondJmp_cand);
388 /* we found a possible candidate */
389 replace = cand ? is_CondJmp_replacement(cand, irn) : 0;
392 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
394 set_irn_op(irn, op_ia32_CJmp);
396 DB((cg->mod, LEVEL_1, "%+F\n", irn));
401 * Performs Peephole Optimizations
403 void ia32_peephole_optimization(ir_node *irn, void *env) {
404 if (is_ia32_TestJmp(irn)) {
405 ia32_optimize_TestJmp(irn, env);
407 else if (is_ia32_CondJmp(irn)) {
408 ia32_optimize_CondJmp(irn, env);
411 else if (be_is_IncSP(irn)) {
412 ir_node *prev = sched_prev(irn);
414 if (be_is_IncSP(prev)) {
415 /* two scheduled IncSP one-after-one, kill the first one */
416 unsigned prev_offs = be_get_IncSP_offset(prev);
417 be_stack_dir_t prev_dir = be_get_IncSP_direction(prev);
418 unsigned curr_offs = be_get_IncSP_offset(irn);
419 be_stack_dir_t curr_dir = be_get_IncSP_direction(irn);
421 int new_ofs = prev_offs * (prev_dir == be_stack_dir_expand ? -1 : +1) +
422 curr_offs * (curr_dir == be_stack_dir_expand ? -1 : +1);
426 curr_dir = be_stack_dir_expand;
429 curr_dir = be_stack_dir_shrink;
430 be_set_IncSP_offset(prev, 0);
431 be_set_IncSP_offset(irn, (unsigned)new_ofs);
432 be_set_IncSP_direction(irn, curr_dir);
440 /******************************************************************
442 * /\ | | | | | \/ | | |
443 * / \ __| | __| |_ __ ___ ___ ___| \ / | ___ __| | ___
444 * / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
445 * / ____ \ (_| | (_| | | | __/\__ \__ \ | | | (_) | (_| | __/
446 * /_/ \_\__,_|\__,_|_| \___||___/___/_| |_|\___/ \__,_|\___|
448 ******************************************************************/
450 static int node_is_ia32_comm(const ir_node *irn) {
451 return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
454 static int ia32_get_irn_n_edges(const ir_node *irn) {
455 const ir_edge_t *edge;
458 foreach_out_edge(irn, edge) {
466 * Returns the first mode_M Proj connected to irn.
468 static ir_node *get_mem_proj(const ir_node *irn) {
469 const ir_edge_t *edge;
472 assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
474 foreach_out_edge(irn, edge) {
475 src = get_edge_src_irn(edge);
477 assert(is_Proj(src) && "Proj expected");
479 if (get_irn_mode(src) == mode_M)
487 * Returns the first Proj with mode != mode_M connected to irn.
489 static ir_node *get_res_proj(const ir_node *irn) {
490 const ir_edge_t *edge;
493 assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
495 foreach_out_edge(irn, edge) {
496 src = get_edge_src_irn(edge);
498 assert(is_Proj(src) && "Proj expected");
500 if (get_irn_mode(src) != mode_M)
508 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
510 * @param pred The node to be checked
511 * @param is_op_func The check-function
512 * @return 1 if conditions are fulfilled, 0 otherwise
514 static int pred_is_specific_node(const ir_node *pred, is_op_func_t *is_op_func) {
515 if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
523 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
524 * and if the predecessor is in block bl.
526 * @param bl The block
527 * @param pred The node to be checked
528 * @param is_op_func The check-function
529 * @return 1 if conditions are fulfilled, 0 otherwise
531 static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
532 int (*is_op_func)(const ir_node *n))
535 pred = get_Proj_pred(pred);
536 if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
547 * Checks if irn is a candidate for address calculation or address mode.
549 * address calculation (AC):
550 * - none of the operand must be a Load within the same block OR
551 * - all Loads must have more than one user OR
552 * - the irn has a frame entity (it's a former FrameAddr)
555 * - at least one operand has to be a Load within the same block AND
556 * - the load must not have other users than the irn AND
557 * - the irn must not have a frame entity set
559 * @param block The block the Loads must/not be in
560 * @param irn The irn to check
561 * @param check_addr 1 if to check for address calculation, 0 otherwise
562 * return 1 if irn is a candidate for AC or AM, 0 otherwise
564 static int is_candidate(const ir_node *block, const ir_node *irn, int check_addr) {
566 int n, is_cand = check_addr;
568 in = get_irn_n(irn, 2);
570 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
571 n = ia32_get_irn_n_edges(in);
572 is_cand = check_addr ? (n == 1 ? 0 : is_cand) : (n == 1 ? 1 : is_cand);
575 in = get_irn_n(irn, 3);
577 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
578 n = ia32_get_irn_n_edges(in);
579 is_cand = check_addr ? (n == 1 ? 0 : is_cand) : (n == 1 ? 1 : is_cand);
582 is_cand = get_ia32_frame_ent(irn) ? (check_addr ? 1 : 0) : is_cand;
588 * Compares the base and index addr and the load/store entities
589 * and returns 1 if they are equal.
591 static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
592 const ir_node *addr_b, const ir_node *addr_i)
594 int is_equal = (addr_b == get_irn_n(load, 0)) && (addr_i == get_irn_n(load, 1));
595 entity *lent = get_ia32_frame_ent(load);
596 entity *sent = get_ia32_frame_ent(store);
597 ident *lid = get_ia32_am_sc(load);
598 ident *sid = get_ia32_am_sc(store);
599 char *loffs = get_ia32_am_offs(load);
600 char *soffs = get_ia32_am_offs(store);
602 /* are both entities set and equal? */
603 if (is_equal && (lent || sent))
604 is_equal = lent && sent && (lent == sent);
606 /* are address mode idents set and equal? */
607 if (is_equal && (lid || sid))
608 is_equal = lid && sid && (lid == sid);
610 /* are offsets set and equal */
611 if (is_equal && (loffs || soffs))
612 is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;
614 /* are the load and the store of the same mode? */
615 is_equal = is_equal ? get_ia32_ls_mode(load) == get_ia32_ls_mode(store) : 0;
623 * Folds Add or Sub to LEA if possible
625 static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn, ir_node *noreg) {
626 ir_graph *irg = get_irn_irg(irn);
627 dbg_info *dbg = get_irn_dbg_info(irn);
628 ir_node *block = get_nodes_block(irn);
631 const char *offs_cnst = NULL;
632 char *offs_lea = NULL;
639 ir_node *left, *right, *temp;
640 ir_node *base, *index;
641 ia32_am_flavour_t am_flav;
642 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
644 if (is_ia32_Add(irn))
647 left = get_irn_n(irn, 2);
648 right = get_irn_n(irn, 3);
650 /* "normalize" arguments in case of add with two operands */
651 if (isadd && ! be_is_NoReg(cg, right)) {
652 /* put LEA == ia32_am_O as right operand */
653 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
654 set_irn_n(irn, 2, right);
655 set_irn_n(irn, 3, left);
661 /* put LEA != ia32_am_O as left operand */
662 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
663 set_irn_n(irn, 2, right);
664 set_irn_n(irn, 3, left);
670 /* put SHL as left operand iff left is NOT a LEA */
671 if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
672 set_irn_n(irn, 2, right);
673 set_irn_n(irn, 3, left);
686 /* check for operation with immediate */
687 if (is_ia32_ImmConst(irn)) {
688 DBG((mod, LEVEL_1, "\tfound op with imm const"));
690 offs_cnst = get_ia32_cnst(irn);
693 else if (is_ia32_ImmSymConst(irn)) {
694 DBG((mod, LEVEL_1, "\tfound op with imm symconst"));
698 am_sc = get_ia32_id_cnst(irn);
699 am_sc_sign = is_ia32_am_sc_sign(irn);
702 /* determine the operand which needs to be checked */
703 if (be_is_NoReg(cg, right)) {
710 /* check if right operand is AMConst (LEA with ia32_am_O) */
711 /* but we can only eat it up if there is no other symconst */
712 /* because the linker won't accept two symconsts */
713 if (! have_am_sc && is_ia32_Lea(temp) && get_ia32_am_flavour(temp) == ia32_am_O) {
714 DBG((mod, LEVEL_1, "\tgot op with LEA am_O"));
716 offs_lea = get_ia32_am_offs(temp);
717 am_sc = get_ia32_am_sc(temp);
718 am_sc_sign = is_ia32_am_sc_sign(temp);
724 /* default for add -> make right operand to index */
728 DBG((mod, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
730 /* determine the operand which needs to be checked */
732 if (is_ia32_Lea(left)) {
736 /* check for SHL 1,2,3 */
737 if (pred_is_specific_node(temp, is_ia32_Shl)) {
738 temp = get_Proj_pred(temp);
740 if (get_ia32_Immop_tarval(temp)) {
741 scale = get_tarval_long(get_ia32_Immop_tarval(temp));
744 index = get_irn_n(temp, 2);
746 DBG((mod, LEVEL_1, "\tgot scaled index %+F\n", index));
752 if (! be_is_NoReg(cg, index)) {
753 /* if we have index, but left == right -> no base */
757 else if (! is_ia32_Lea(left) && (index != right)) {
758 /* index != right -> we found a good Shl */
759 /* left != LEA -> this Shl was the left operand */
760 /* -> base is right operand */
766 /* Try to assimilate a LEA as left operand */
767 if (is_ia32_Lea(left) && (get_ia32_am_flavour(left) != ia32_am_O)) {
768 am_flav = get_ia32_am_flavour(left);
770 /* If we have an Add with a real right operand (not NoReg) and */
771 /* the LEA contains already an index calculation then we create */
773 /* If the LEA contains already a frame_entity then we also */
774 /* create a new one otherwise we would loose it. */
775 if ((isadd && !be_is_NoReg(cg, index) && (am_flav & ia32_am_I)) || /* no new LEA if index already set */
776 get_ia32_frame_ent(left) || /* no new LEA if stack access */
777 (have_am_sc && get_ia32_am_sc(left))) /* no new LEA if AM symconst already present */
779 DBG((mod, LEVEL_1, "\tleave old LEA, creating new one\n"));
782 DBG((mod, LEVEL_1, "\tgot LEA as left operand ... assimilating\n"));
783 offs = get_ia32_am_offs(left);
784 am_sc = have_am_sc ? am_sc : get_ia32_am_sc(left);
785 have_am_sc = am_sc ? 1 : 0;
786 am_sc_sign = is_ia32_am_sc_sign(left);
787 base = get_irn_n(left, 0);
788 index = get_irn_n(left, 1);
789 scale = get_ia32_am_scale(left);
793 /* ok, we can create a new LEA */
795 res = new_rd_ia32_Lea(dbg, irg, block, base, index, mode_Is);
797 /* add the old offset of a previous LEA */
799 add_ia32_am_offs(res, offs);
802 /* add the new offset */
805 add_ia32_am_offs(res, offs_cnst);
808 add_ia32_am_offs(res, offs_lea);
812 /* either lea_O-cnst, -cnst or -lea_O */
815 add_ia32_am_offs(res, offs_lea);
818 sub_ia32_am_offs(res, offs_cnst);
821 sub_ia32_am_offs(res, offs_lea);
825 /* set the address mode symconst */
827 set_ia32_am_sc(res, am_sc);
829 set_ia32_am_sc_sign(res);
832 /* copy the frame entity (could be set in case of Add */
833 /* which was a FrameAddr) */
834 set_ia32_frame_ent(res, get_ia32_frame_ent(irn));
836 if (is_ia32_use_frame(irn))
837 set_ia32_use_frame(res);
840 set_ia32_am_scale(res, scale);
843 /* determine new am flavour */
844 if (offs || offs_cnst || offs_lea) {
847 if (! be_is_NoReg(cg, base)) {
850 if (! be_is_NoReg(cg, index)) {
856 set_ia32_am_flavour(res, am_flav);
858 set_ia32_op_type(res, ia32_AddrModeS);
860 DBG((mod, LEVEL_1, "\tLEA [%+F + %+F * %d + %s]\n", base, index, scale, get_ia32_am_offs(res)));
862 /* get the result Proj of the Add/Sub */
863 irn = get_res_proj(irn);
865 assert(irn && "Couldn't find result proj");
867 /* exchange the old op with the new LEA */
875 * Optimizes a pattern around irn to address mode if possible.
877 void ia32_optimize_am(ir_node *irn, void *env) {
878 ia32_code_gen_t *cg = env;
882 ir_node *block, *noreg_gp, *noreg_fp;
883 ir_node *left, *right, *temp;
884 ir_node *store, *load, *mem_proj;
885 ir_node *succ, *addr_b, *addr_i;
886 int check_am_src = 0;
887 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
889 if (! is_ia32_irn(irn))
892 dbg = get_irn_dbg_info(irn);
893 mode = get_irn_mode(irn);
894 block = get_nodes_block(irn);
895 noreg_gp = ia32_new_NoReg_gp(cg);
896 noreg_fp = ia32_new_NoReg_fp(cg);
898 DBG((mod, LEVEL_1, "checking for AM\n"));
900 /* 1st part: check for address calculations and transform the into Lea */
902 /* Following cases can occur: */
903 /* - Sub (l, imm) -> LEA [base - offset] */
904 /* - Sub (l, r == LEA with ia32_am_O) -> LEA [base - offset] */
905 /* - Add (l, imm) -> LEA [base + offset] */
906 /* - Add (l, r == LEA with ia32_am_O) -> LEA [base + offset] */
907 /* - Add (l == LEA with ia32_am_O, r) -> LEA [base + offset] */
908 /* - Add (l, r) -> LEA [base + index * scale] */
909 /* with scale > 1 iff l/r == shl (1,2,3) */
911 if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
912 left = get_irn_n(irn, 2);
913 right = get_irn_n(irn, 3);
915 /* Do not try to create a LEA if one of the operands is a Load. */
916 /* check is irn is a candidate for address calculation */
917 if (is_candidate(block, irn, 1)) {
918 DBG((mod, LEVEL_1, "\tfound address calculation candidate %+F ... ", irn));
919 res = fold_addr(cg, irn, noreg_gp);
922 DB((mod, LEVEL_1, "transformed into %+F\n", res));
924 DB((mod, LEVEL_1, "not transformed\n"));
928 /* 2nd part: fold following patterns: */
929 /* - Load -> LEA into Load } TODO: If the LEA is used by more than one Load/Store */
930 /* - Store -> LEA into Store } it might be better to keep the LEA */
931 /* - op -> Load into AMop with am_Source */
933 /* - op is am_Source capable AND */
934 /* - the Load is only used by this op AND */
935 /* - the Load is in the same block */
936 /* - Store -> op -> Load into AMop with am_Dest */
938 /* - op is am_Dest capable AND */
939 /* - the Store uses the same address as the Load AND */
940 /* - the Load is only used by this op AND */
941 /* - the Load and Store are in the same block AND */
942 /* - nobody else uses the result of the op */
944 if ((res == irn) && (get_ia32_am_support(irn) != ia32_am_None) && !is_ia32_Lea(irn)) {
945 /* 1st: check for Load/Store -> LEA */
946 if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn)) {
947 left = get_irn_n(irn, 0);
949 if (is_ia32_Lea(left)) {
950 DBG((mod, LEVEL_1, "\nmerging %+F into %+F\n", left, irn));
952 /* get the AM attributes from the LEA */
953 add_ia32_am_offs(irn, get_ia32_am_offs(left));
954 set_ia32_am_scale(irn, get_ia32_am_scale(left));
955 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
957 set_ia32_am_sc(irn, get_ia32_am_sc(left));
958 if (is_ia32_am_sc_sign(left))
959 set_ia32_am_sc_sign(irn);
961 set_ia32_op_type(irn, is_ia32_Ld(irn) ? ia32_AddrModeS : ia32_AddrModeD);
963 /* set base and index */
964 set_irn_n(irn, 0, get_irn_n(left, 0));
965 set_irn_n(irn, 1, get_irn_n(left, 1));
967 /* clear remat flag */
968 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
971 /* check if the node is an address mode candidate */
972 else if (is_candidate(block, irn, 0)) {
973 DBG((mod, LEVEL_1, "\tfound address mode candidate %+F ... ", irn));
975 left = get_irn_n(irn, 2);
976 if (get_irn_arity(irn) == 4) {
977 /* it's an "unary" operation */
981 right = get_irn_n(irn, 3);
984 /* normalize commutative ops */
985 if (node_is_ia32_comm(irn)) {
986 /* Assure that right operand is always a Load if there is one */
987 /* because non-commutative ops can only use Dest AM if the right */
988 /* operand is a load, so we only need to check right operand. */
989 if (pred_is_specific_nodeblock(block, left, is_ia32_Ld))
991 set_irn_n(irn, 2, right);
992 set_irn_n(irn, 3, left);
1000 /* check for Store -> op -> Load */
1002 /* Store -> op -> Load optimization is only possible if supported by op */
1003 /* and if right operand is a Load */
1004 if ((get_ia32_am_support(irn) & ia32_am_Dest) &&
1005 pred_is_specific_nodeblock(block, right, is_ia32_Ld))
1008 /* An address mode capable op always has a result Proj. */
1009 /* If this Proj is used by more than one other node, we don't need to */
1010 /* check further, otherwise we check for Store and remember the address, */
1011 /* the Store points to. */
1013 succ = get_res_proj(irn);
1014 assert(succ && "Couldn't find result proj");
1020 /* now check for users and Store */
1021 if (ia32_get_irn_n_edges(succ) == 1) {
1022 succ = get_edge_src_irn(get_irn_out_edge_first(succ));
1024 if (is_ia32_fStore(succ) || is_ia32_Store(succ)) {
1026 addr_b = get_irn_n(store, 0);
1028 /* Could be that the Store is connected to the address */
1029 /* calculating LEA while the Load is already transformed. */
1030 if (is_ia32_Lea(addr_b)) {
1032 addr_b = get_irn_n(succ, 0);
1033 addr_i = get_irn_n(succ, 1);
1042 /* we found a Store as single user: Now check for Load */
1044 /* Extra check for commutative ops with two Loads */
1045 /* -> put the interesting Load right */
1046 if (node_is_ia32_comm(irn) &&
1047 pred_is_specific_nodeblock(block, left, is_ia32_Ld))
1049 if ((addr_b == get_irn_n(get_Proj_pred(left), 0)) &&
1050 (addr_i == get_irn_n(get_Proj_pred(left), 1)))
1052 /* We exchange left and right, so it's easier to kill */
1053 /* the correct Load later and to handle unary operations. */
1054 set_irn_n(irn, 2, right);
1055 set_irn_n(irn, 3, left);
1063 /* skip the Proj for easier access */
1064 load = get_Proj_pred(right);
1066 /* Compare Load and Store address */
1067 if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1068 /* Right Load is from same address, so we can */
1069 /* disconnect the Load and Store here */
1071 /* set new base, index and attributes */
1072 set_irn_n(irn, 0, addr_b);
1073 set_irn_n(irn, 1, addr_i);
1074 add_ia32_am_offs(irn, get_ia32_am_offs(load));
1075 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1076 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1077 set_ia32_op_type(irn, ia32_AddrModeD);
1078 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1079 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1081 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1082 if (is_ia32_am_sc_sign(load))
1083 set_ia32_am_sc_sign(irn);
1085 if (is_ia32_use_frame(load))
1086 set_ia32_use_frame(irn);
1088 /* connect to Load memory and disconnect Load */
1089 if (get_irn_arity(irn) == 5) {
1091 set_irn_n(irn, 4, get_irn_n(load, 2));
1092 set_irn_n(irn, 3, noreg_gp);
1096 set_irn_n(irn, 3, get_irn_n(load, 2));
1097 set_irn_n(irn, 2, noreg_gp);
1100 /* connect the memory Proj of the Store to the op */
1101 mem_proj = get_mem_proj(store);
1102 set_Proj_pred(mem_proj, irn);
1103 set_Proj_proj(mem_proj, 1);
1105 /* clear remat flag */
1106 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1108 DB((mod, LEVEL_1, "merged with %+F and %+F into dest AM\n", load, store));
1111 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1112 /* There was no store, check if we still can optimize for source address mode */
1115 } /* if (support AM Dest) */
1116 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1117 /* op doesn't support am AM Dest -> check for AM Source */
1121 /* normalize commutative ops */
1122 if (node_is_ia32_comm(irn)) {
1123 /* Assure that left operand is always a Load if there is one */
1124 /* because non-commutative ops can only use Source AM if the */
1125 /* left operand is a Load, so we only need to check the left */
1126 /* operand afterwards. */
1127 if (pred_is_specific_nodeblock(block, right, is_ia32_Ld)) {
1128 set_irn_n(irn, 2, right);
1129 set_irn_n(irn, 3, left);
1137 /* optimize op -> Load iff Load is only used by this op */
1138 /* and left operand is a Load which only used by this irn */
1140 pred_is_specific_nodeblock(block, left, is_ia32_Ld) &&
1141 (ia32_get_irn_n_edges(left) == 1))
1143 left = get_Proj_pred(left);
1145 addr_b = get_irn_n(left, 0);
1146 addr_i = get_irn_n(left, 1);
1148 /* set new base, index and attributes */
1149 set_irn_n(irn, 0, addr_b);
1150 set_irn_n(irn, 1, addr_i);
1151 add_ia32_am_offs(irn, get_ia32_am_offs(left));
1152 set_ia32_am_scale(irn, get_ia32_am_scale(left));
1153 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
1154 set_ia32_op_type(irn, ia32_AddrModeS);
1155 set_ia32_frame_ent(irn, get_ia32_frame_ent(left));
1156 set_ia32_ls_mode(irn, get_ia32_ls_mode(left));
1158 set_ia32_am_sc(irn, get_ia32_am_sc(left));
1159 if (is_ia32_am_sc_sign(left))
1160 set_ia32_am_sc_sign(irn);
1162 /* clear remat flag */
1163 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1165 if (is_ia32_use_frame(left))
1166 set_ia32_use_frame(irn);
1168 /* connect to Load memory */
1169 if (get_irn_arity(irn) == 5) {
1171 set_irn_n(irn, 4, get_irn_n(left, 2));
1175 set_irn_n(irn, 3, get_irn_n(left, 2));
1178 /* disconnect from Load */
1179 set_irn_n(irn, 2, noreg_gp);
1181 /* If Load has a memory Proj, connect it to the op */
1182 mem_proj = get_mem_proj(left);
1184 set_Proj_pred(mem_proj, irn);
1185 set_Proj_proj(mem_proj, 1);
1188 DB((mod, LEVEL_1, "merged with %+F into source AM\n", left));