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"
28 #include "../benode_t.h"
29 #include "../besched_t.h"
31 #include "ia32_new_nodes.h"
32 #include "bearch_ia32_t.h"
33 #include "gen_ia32_regalloc_if.h" /* the generated interface (register type and class defenitions) */
34 #include "ia32_transform.h"
35 #include "ia32_dbg_stat.h"
36 #include "ia32_util.h"
41 IA32_AM_CAND_NONE = 0,
42 IA32_AM_CAND_LEFT = 1,
43 IA32_AM_CAND_RIGHT = 2,
47 typedef int is_op_func_t(const ir_node *n);
48 typedef ir_node *load_func_t(dbg_info *db, ir_graph *irg, ir_node *block, ir_node *base, ir_node *index, ir_node *mem);
51 * checks if a node represents the NOREG value
53 static int be_is_NoReg(ia32_code_gen_t *cg, const ir_node *irn) {
54 be_abi_irg_t *babi = cg->birg->abi;
55 const arch_register_t *fp_noreg = USE_SSE2(cg) ?
56 &ia32_xmm_regs[REG_XMM_NOREG] : &ia32_vfp_regs[REG_VFP_NOREG];
58 return (be_abi_get_callee_save_irn(babi, &ia32_gp_regs[REG_GP_NOREG]) == irn) ||
59 (be_abi_get_callee_save_irn(babi, fp_noreg) == irn);
64 /*************************************************
67 * | | ___ _ __ ___| |_ __ _ _ __ | |_ ___
68 * | | / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
69 * | |___| (_) | | | \__ \ || (_| | | | | |_\__ \
70 * \_____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
72 *************************************************/
75 * creates a unique ident by adding a number to a tag
77 * @param tag the tag string, must contain a %d if a number
80 static ident *unique_id(const char *tag)
82 static unsigned id = 0;
85 snprintf(str, sizeof(str), tag, ++id);
86 return new_id_from_str(str);
90 * Transforms a SymConst.
92 * @param mod the debug module
93 * @param block the block the new node should belong to
94 * @param node the ir SymConst node
95 * @param mode mode of the SymConst
96 * @return the created ia32 Const node
98 static ir_node *gen_SymConst(ia32_transform_env_t *env) {
99 dbg_info *dbg = env->dbg;
100 ir_mode *mode = env->mode;
101 ir_graph *irg = env->irg;
102 ir_node *block = env->block;
105 if (mode_is_float(mode)) {
107 if (USE_SSE2(env->cg))
108 cnst = new_rd_ia32_xConst(dbg, irg, block, mode);
110 cnst = new_rd_ia32_vfConst(dbg, irg, block, mode);
113 cnst = new_rd_ia32_Const(dbg, irg, block, mode);
115 set_ia32_Const_attr(cnst, env->irn);
121 * Get a primitive type for a mode.
123 static ir_type *get_prim_type(pmap *types, ir_mode *mode)
125 pmap_entry *e = pmap_find(types, mode);
130 snprintf(buf, sizeof(buf), "prim_type_%s", get_mode_name(mode));
131 res = new_type_primitive(new_id_from_str(buf), mode);
132 pmap_insert(types, mode, res);
140 * Get an entity that is initialized with a tarval
142 static entity *get_entity_for_tv(ia32_code_gen_t *cg, ir_node *cnst)
144 tarval *tv = get_Const_tarval(cnst);
145 pmap_entry *e = pmap_find(cg->isa->tv_ent, tv);
150 ir_mode *mode = get_irn_mode(cnst);
151 ir_type *tp = get_Const_type(cnst);
152 if (tp == firm_unknown_type)
153 tp = get_prim_type(cg->isa->types, mode);
155 res = new_entity(get_glob_type(), unique_id(".LC%u"), tp);
157 set_entity_ld_ident(res, get_entity_ident(res));
158 set_entity_visibility(res, visibility_local);
159 set_entity_variability(res, variability_constant);
160 set_entity_allocation(res, allocation_static);
162 /* we create a new entity here: It's initialization must resist on the
164 rem = current_ir_graph;
165 current_ir_graph = get_const_code_irg();
166 set_atomic_ent_value(res, new_Const_type(tv, tp));
167 current_ir_graph = rem;
169 pmap_insert(cg->isa->tv_ent, tv, res);
177 * Transforms a Const.
179 * @param mod the debug module
180 * @param block the block the new node should belong to
181 * @param node the ir Const node
182 * @param mode mode of the Const
183 * @return the created ia32 Const node
185 static ir_node *gen_Const(ia32_transform_env_t *env) {
186 ir_node *cnst, *load;
188 ir_graph *irg = env->irg;
189 ir_node *block = env->block;
190 ir_node *node = env->irn;
191 dbg_info *dbg = env->dbg;
192 ir_mode *mode = env->mode;
193 ir_node *start_block = get_irg_start_block(irg);
195 if (mode_is_float(mode)) {
197 if (! USE_SSE2(env->cg)) {
198 cnst_classify_t clss = classify_Const(node);
200 if (clss == CNST_NULL)
201 return new_rd_ia32_vfldz(dbg, irg, block, mode);
202 else if (clss == CNST_ONE)
203 return new_rd_ia32_vfld1(dbg, irg, block, mode);
205 sym.entity_p = get_entity_for_tv(env->cg, node);
208 cnst = new_rd_SymConst(dbg, irg, block, sym, symconst_addr_ent);
209 load = new_r_Load(irg, block, get_irg_no_mem(irg), cnst, mode);
210 load = new_r_Proj(irg, block, load, mode, pn_Load_res);
213 cnst = gen_SymConst(env);
215 if (start_block == block)
216 add_irn_dep(cnst, be_abi_get_start_barrier(env->cg->birg->abi));
218 set_Load_ptr(get_Proj_pred(load), cnst);
222 cnst = new_rd_ia32_Const(dbg, irg, block, get_irn_mode(node));
224 if (start_block == block)
225 add_irn_dep(cnst, be_abi_get_start_barrier(env->cg->birg->abi));
227 set_ia32_Const_attr(cnst, node);
234 * Transforms (all) Const's into ia32_Const and places them in the
235 * block where they are used (or in the cfg-pred Block in case of Phi's).
236 * Additionally all reference nodes are changed into mode_Is nodes.
237 * NOTE: irn must be a firm constant!
239 static void ia32_transform_const(ir_node *irn, void *env) {
240 ia32_code_gen_t *cg = env;
241 ir_node *cnst = NULL;
242 ia32_transform_env_t tenv;
246 tenv.mode = get_irn_mode(irn);
247 tenv.dbg = get_irn_dbg_info(irn);
249 DEBUG_ONLY(tenv.mod = cg->mod;)
252 /* place const either in the smallest dominator of all its users or the original block */
253 if (cg->opt & IA32_OPT_PLACECNST)
254 tenv.block = node_users_smallest_common_dominator(irn, 1);
256 tenv.block = get_nodes_block(irn);
258 /* Actually, there is no real sense in placing */
259 /* the Consts in the successor of the start block. */
261 ir_node *afterstart = NULL;
262 ir_node *startblock = get_irg_start_block(tenv.irg);
263 const ir_edge_t *edge;
265 foreach_block_succ(startblock, edge) {
266 ir_node *block = get_edge_src_irn(edge);
267 if (block != startblock) {
272 assert(afterstart != NULL);
273 tenv.block = afterstart;
277 switch (get_irn_opcode(irn)) {
279 cnst = gen_Const(&tenv);
282 cnst = gen_SymConst(&tenv);
285 assert(0 && "Wrong usage of ia32_transform_const!");
288 assert(cnst && "Could not create ia32 Const");
290 /* set the new ia32 const */
295 * Transform all firm consts and assure, we visit each const only once.
297 static void ia32_place_consts_walker(ir_node *irn, void *env) {
298 ia32_code_gen_t *cg = env;
300 if (! is_Const(irn) && ! is_SymConst(irn))
303 ia32_transform_const(irn, cg);
307 * Replace reference modes with mode_Iu and preserve store value modes.
309 static void ia32_set_modes(ir_node *irn, void *env) {
313 /* transform all reference nodes into mode_Iu nodes */
314 if (mode_is_reference(get_irn_mode(irn))) {
315 set_irn_mode(irn, mode_Iu);
320 * Walks over the graph, transforms all firm consts into ia32 consts
321 * and places them into the "best" block.
322 * @param cg The ia32 codegenerator object
324 static void ia32_transform_all_firm_consts(ia32_code_gen_t *cg) {
325 irg_walk_graph(cg->irg, NULL, ia32_place_consts_walker, cg);
328 /* Place all consts and change pointer arithmetics into unsigned integer arithmetics. */
329 void ia32_pre_transform_phase(ia32_code_gen_t *cg) {
331 We need to transform the consts twice:
332 - the psi condition tree transformer needs existing constants to be ia32 constants
333 - the psi condition tree transformer inserts new firm constants which need to be transformed
335 ia32_transform_all_firm_consts(cg);
336 irg_walk_graph(cg->irg, ia32_set_modes, ia32_transform_psi_cond_tree, cg);
337 ia32_transform_all_firm_consts(cg);
340 /********************************************************************************************************
341 * _____ _ _ ____ _ _ _ _ _
342 * | __ \ | | | | / __ \ | | (_) (_) | | (_)
343 * | |__) |__ ___ _ __ | |__ ___ | | ___ | | | |_ __ | |_ _ _ __ ___ _ ______ _| |_ _ ___ _ __
344 * | ___/ _ \/ _ \ '_ \| '_ \ / _ \| |/ _ \ | | | | '_ \| __| | '_ ` _ \| |_ / _` | __| |/ _ \| '_ \
345 * | | | __/ __/ |_) | | | | (_) | | __/ | |__| | |_) | |_| | | | | | | |/ / (_| | |_| | (_) | | | |
346 * |_| \___|\___| .__/|_| |_|\___/|_|\___| \____/| .__/ \__|_|_| |_| |_|_/___\__,_|\__|_|\___/|_| |_|
349 ********************************************************************************************************/
352 * NOTE: THESE PEEPHOLE OPTIMIZATIONS MUST BE CALLED AFTER SCHEDULING AND REGISTER ALLOCATION.
355 static int ia32_cnst_compare(ir_node *n1, ir_node *n2) {
356 return get_ia32_id_cnst(n1) == get_ia32_id_cnst(n2);
360 * Checks for potential CJmp/CJmpAM optimization candidates.
362 static ir_node *ia32_determine_cjmp_cand(ir_node *irn, is_op_func_t *is_op_func) {
363 ir_node *cand = NULL;
364 ir_node *prev = sched_prev(irn);
366 if (is_Block(prev)) {
367 if (get_Block_n_cfgpreds(prev) == 1)
368 prev = get_Block_cfgpred(prev, 0);
373 /* The predecessor must be a ProjX. */
374 if (prev && is_Proj(prev) && get_irn_mode(prev) == mode_X) {
375 prev = get_Proj_pred(prev);
377 if (is_op_func(prev))
384 static int is_TestJmp_cand(const ir_node *irn) {
385 return is_ia32_TestJmp(irn) || is_ia32_And(irn);
389 * Checks if two consecutive arguments of cand matches
390 * the two arguments of irn (TestJmp).
392 static int is_TestJmp_replacement(ir_node *cand, ir_node *irn) {
393 ir_node *in1 = get_irn_n(irn, 0);
394 ir_node *in2 = get_irn_n(irn, 1);
395 int i, n = get_irn_arity(cand);
398 for (i = 0; i < n - 1; i++) {
399 if (get_irn_n(cand, i) == in1 &&
400 get_irn_n(cand, i + 1) == in2)
408 return ia32_cnst_compare(cand, irn);
414 * Tries to replace a TestJmp by a CJmp or CJmpAM (in case of And)
416 static void ia32_optimize_TestJmp(ir_node *irn, ia32_code_gen_t *cg) {
417 ir_node *cand = ia32_determine_cjmp_cand(irn, is_TestJmp_cand);
420 /* we found a possible candidate */
421 replace = cand ? is_TestJmp_replacement(cand, irn) : 0;
424 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
426 if (is_ia32_And(cand))
427 set_irn_op(irn, op_ia32_CJmpAM);
429 set_irn_op(irn, op_ia32_CJmp);
431 DB((cg->mod, LEVEL_1, "%+F\n", irn));
435 static int is_CondJmp_cand(const ir_node *irn) {
436 return is_ia32_CondJmp(irn) || is_ia32_Sub(irn);
440 * Checks if the arguments of cand are the same of irn.
442 static int is_CondJmp_replacement(ir_node *cand, ir_node *irn) {
443 int i, n = get_irn_arity(cand);
446 for (i = 0; i < n; i++) {
447 if (get_irn_n(cand, i) != get_irn_n(irn, i)) {
454 return ia32_cnst_compare(cand, irn);
460 * Tries to replace a CondJmp by a CJmpAM
462 static void ia32_optimize_CondJmp(ir_node *irn, ia32_code_gen_t *cg) {
463 ir_node *cand = ia32_determine_cjmp_cand(irn, is_CondJmp_cand);
466 /* we found a possible candidate */
467 replace = cand ? is_CondJmp_replacement(cand, irn) : 0;
470 DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
473 set_irn_op(irn, op_ia32_CJmpAM);
475 DB((cg->mod, LEVEL_1, "%+F\n", irn));
479 // only optimize up to 48 stores behind IncSPs
480 #define MAXPUSH_OPTIMIZE 48
483 * Tries to create pushs from IncSP,Store combinations
485 static void ia32_create_Pushs(ir_node *irn, ia32_code_gen_t *cg) {
489 ir_node *stores[MAXPUSH_OPTIMIZE];
490 ir_node *block = get_nodes_block(irn);
491 ir_graph *irg = cg->irg;
493 ir_mode *spmode = get_irn_mode(irn);
495 memset(stores, 0, sizeof(stores));
497 assert(be_is_IncSP(irn));
499 offset = be_get_IncSP_offset(irn);
504 * We first walk the schedule after the IncSP node as long as we find
505 * suitable stores that could be transformed to a push.
506 * We save them into the stores array which is sorted by the frame offset/4
507 * attached to the node
509 for(node = sched_next(irn); !sched_is_end(node); node = sched_next(node)) {
516 // it has to be a store
517 if(!is_ia32_Store(node))
520 // it has to use our sp value
521 if(get_irn_n(node, 0) != irn)
523 // store has to be attached to NoMem
524 mem = get_irn_n(node, 3);
529 if( (get_ia32_am_flavour(node) & ia32_am_IS) != 0)
532 am_offs = get_ia32_am_offs(node);
533 if(am_offs == NULL) {
536 // the am_offs has to be of the form "+NUMBER"
537 if(sscanf(am_offs, "+%d%n", &offset, &n) != 1 || am_offs[n] != '\0') {
538 // we shouldn't have any cases in the compiler at the moment
539 // that produce something different from esp+XX
545 storeslot = offset / 4;
546 if(storeslot >= MAXPUSH_OPTIMIZE)
549 // storing into the same slot twice is bad (and shouldn't happen...)
550 if(stores[storeslot] != NULL)
553 // storing at half-slots is bad
557 stores[storeslot] = node;
560 curr_sp = get_irn_n(irn, 0);
562 // walk the stores in inverse order and create pushs for them
563 i = (offset / 4) - 1;
564 if(i >= MAXPUSH_OPTIMIZE) {
565 i = MAXPUSH_OPTIMIZE - 1;
568 for( ; i >= 0; --i) {
569 const ir_edge_t *edge, *next;
570 const arch_register_t *spreg;
573 ir_node *store = stores[i];
574 ir_node *noreg = ia32_new_NoReg_gp(cg);
576 if(store == NULL || is_Bad(store))
579 val = get_irn_n(store, 2);
580 mem = get_irn_n(store, 3);
581 spreg = arch_get_irn_register(cg->arch_env, curr_sp);
584 push = new_rd_ia32_Push(NULL, irg, block, noreg, noreg, val, curr_sp, mem);
585 if(get_ia32_immop_type(store) != ia32_ImmNone) {
586 copy_ia32_Immop_attr(push, store);
588 sched_add_before(irn, push);
590 // create stackpointer proj
591 curr_sp = new_r_Proj(irg, block, push, spmode, pn_ia32_Push_stack);
592 arch_set_irn_register(cg->arch_env, curr_sp, spreg);
593 sched_add_before(irn, curr_sp);
595 // rewire memprojs of the store
596 foreach_out_edge_safe(store, edge, next) {
597 ir_node *succ = get_edge_src_irn(edge);
599 assert(is_Proj(succ) && get_Proj_proj(succ) == pn_ia32_Store_M);
600 set_irn_n(succ, 0, push);
603 // we can remove the store now
604 set_irn_n(store, 0, new_Bad());
605 set_irn_n(store, 1, new_Bad());
606 set_irn_n(store, 2, new_Bad());
607 set_irn_n(store, 3, new_Bad());
613 be_set_IncSP_offset(irn, offset);
615 // can we remove the IncSP now?
617 const ir_edge_t *edge, *next;
619 foreach_out_edge_safe(irn, edge, next) {
620 ir_node *arg = get_edge_src_irn(edge);
621 int pos = get_edge_src_pos(edge);
623 set_irn_n(arg, pos, curr_sp);
626 set_irn_n(irn, 0, new_Bad());
629 set_irn_n(irn, 0, curr_sp);
634 * Tries to optimize two following IncSP.
636 static void ia32_optimize_IncSP(ir_node *irn, ia32_code_gen_t *cg) {
637 ir_node *prev = be_get_IncSP_pred(irn);
638 int real_uses = get_irn_n_edges(prev);
640 if (be_is_IncSP(prev) && real_uses == 1) {
641 /* first IncSP has only one IncSP user, kill the first one */
642 int prev_offs = be_get_IncSP_offset(prev);
643 int curr_offs = be_get_IncSP_offset(irn);
645 be_set_IncSP_offset(prev, prev_offs + curr_offs);
647 /* Omit the optimized IncSP */
648 be_set_IncSP_pred(irn, be_get_IncSP_pred(prev));
650 set_irn_n(prev, 0, new_Bad());
656 * Performs Peephole Optimizations.
658 static void ia32_peephole_optimize_node(ir_node *irn, void *env) {
659 ia32_code_gen_t *cg = env;
661 /* AMD CPUs want explicit compare before conditional jump */
662 if (! ARCH_AMD(cg->opt_arch)) {
663 if (is_ia32_TestJmp(irn))
664 ia32_optimize_TestJmp(irn, cg);
665 else if (is_ia32_CondJmp(irn))
666 ia32_optimize_CondJmp(irn, cg);
669 if (be_is_IncSP(irn)) {
670 // optimize_IncSP doesn't respect dependency edges yet...
671 //ia32_optimize_IncSP(irn, cg);
672 (void) ia32_optimize_IncSP;
673 if (cg->opt & IA32_OPT_PUSHARGS)
674 ia32_create_Pushs(irn, cg);
678 void ia32_peephole_optimization(ir_graph *irg, ia32_code_gen_t *cg) {
679 irg_walk_graph(irg, ia32_peephole_optimize_node, NULL, cg);
682 /******************************************************************
684 * /\ | | | | | \/ | | |
685 * / \ __| | __| |_ __ ___ ___ ___| \ / | ___ __| | ___
686 * / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
687 * / ____ \ (_| | (_| | | | __/\__ \__ \ | | | (_) | (_| | __/
688 * /_/ \_\__,_|\__,_|_| \___||___/___/_| |_|\___/ \__,_|\___|
690 ******************************************************************/
697 static int node_is_ia32_comm(const ir_node *irn) {
698 return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
701 static int ia32_get_irn_n_edges(const ir_node *irn) {
702 const ir_edge_t *edge;
705 foreach_out_edge(irn, edge) {
713 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
715 * @param pred The node to be checked
716 * @param is_op_func The check-function
717 * @return 1 if conditions are fulfilled, 0 otherwise
719 static int pred_is_specific_node(const ir_node *pred, is_op_func_t *is_op_func) {
720 if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
728 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
729 * and if the predecessor is in block bl.
731 * @param bl The block
732 * @param pred The node to be checked
733 * @param is_op_func The check-function
734 * @return 1 if conditions are fulfilled, 0 otherwise
736 static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
737 int (*is_op_func)(const ir_node *n))
740 pred = get_Proj_pred(pred);
741 if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
750 * Checks if irn is a candidate for address calculation.
752 * - none of the operand must be a Load within the same block OR
753 * - all Loads must have more than one user OR
754 * - the irn has a frame entity (it's a former FrameAddr)
756 * @param block The block the Loads must/mustnot be in
757 * @param irn The irn to check
758 * return 1 if irn is a candidate, 0 otherwise
760 static int is_addr_candidate(const ir_node *block, const ir_node *irn) {
761 ir_node *in, *left, *right;
764 left = get_irn_n(irn, 2);
765 right = get_irn_n(irn, 3);
769 #ifndef AGGRESSIVE_AM
770 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
771 n = ia32_get_irn_n_edges(in);
772 is_cand = (n == 1) ? 0 : is_cand; /* load with only one user: don't create LEA */
777 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
778 n = ia32_get_irn_n_edges(in);
779 is_cand = (n == 1) ? 0 : is_cand; /* load with only one user: don't create LEA */
785 is_cand = get_ia32_frame_ent(irn) ? 1 : is_cand;
791 * Checks if irn is a candidate for address mode.
794 * - at least one operand has to be a Load within the same block AND
795 * - the load must not have other users than the irn AND
796 * - the irn must not have a frame entity set
798 * @param cg The ia32 code generator
799 * @param h The height information of the irg
800 * @param block The block the Loads must/mustnot be in
801 * @param irn The irn to check
802 * return 0 if irn is no candidate, 1 if left load can be used, 2 if right one, 3 for both
804 static ia32_am_cand_t is_am_candidate(ia32_code_gen_t *cg, heights_t *h, const ir_node *block, ir_node *irn) {
805 ir_node *in, *load, *other, *left, *right;
806 int is_cand = 0, cand;
808 if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn) || is_ia32_vfild(irn) || is_ia32_vfist(irn) ||
809 is_ia32_GetST0(irn) || is_ia32_SetST0(irn) || is_ia32_xStoreSimple(irn))
812 left = get_irn_n(irn, 2);
813 right = get_irn_n(irn, 3);
817 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
818 #ifndef AGGRESSIVE_AM
820 n = ia32_get_irn_n_edges(in);
821 is_cand = (n == 1) ? 1 : is_cand; /* load with more than one user: no AM */
826 load = get_Proj_pred(in);
829 /* 8bit Loads are not supported (for binary ops),
830 * they cannot be used with every register */
831 if (get_irn_arity(irn) != 4 && get_mode_size_bits(get_ia32_ls_mode(load)) < 16) {
832 assert(get_irn_arity(irn) == 5);
836 /* If there is a data dependency of other irn from load: cannot use AM */
837 if (is_cand && get_nodes_block(other) == block) {
838 other = skip_Proj(other);
839 is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
840 /* this could happen in loops */
841 is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
845 cand = is_cand ? IA32_AM_CAND_LEFT : IA32_AM_CAND_NONE;
849 if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
850 #ifndef AGGRESSIVE_AM
852 n = ia32_get_irn_n_edges(in);
853 is_cand = (n == 1) ? 1 : is_cand; /* load with more than one user: no AM */
856 load = get_Proj_pred(in);
859 /* 8bit Loads are not supported, they cannot be used with every register */
860 if (get_mode_size_bits(get_ia32_ls_mode(load)) < 16)
863 /* If there is a data dependency of other irn from load: cannot use load */
864 if (is_cand && get_nodes_block(other) == block) {
865 other = skip_Proj(other);
866 is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
867 /* this could happen in loops */
868 is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
872 cand = is_cand ? (cand | IA32_AM_CAND_RIGHT) : cand;
874 /* check some special cases */
875 if (USE_SSE2(cg) && is_ia32_Conv_I2FP(irn)) {
876 /* SSE Conv I -> FP cvtsi2s(s|d) can only load 32 bit values */
877 if (get_mode_size_bits(get_ia32_tgt_mode(irn)) != 32)
878 cand = IA32_AM_CAND_NONE;
880 else if (is_ia32_Conv_I2I(irn)) {
881 /* we cannot load an N bit value and implicitly convert it into an M bit value if N > M */
882 if (get_mode_size_bits(get_ia32_src_mode(irn)) > get_mode_size_bits(get_ia32_tgt_mode(irn)))
883 cand = IA32_AM_CAND_NONE;
886 /* if the irn has a frame entity: we do not use address mode */
887 return get_ia32_frame_ent(irn) ? IA32_AM_CAND_NONE : cand;
891 * Compares the base and index addr and the load/store entities
892 * and returns 1 if they are equal.
894 static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
895 const ir_node *addr_b, const ir_node *addr_i)
897 int is_equal = (addr_b == get_irn_n(load, 0)) && (addr_i == get_irn_n(load, 1));
898 entity *lent = get_ia32_frame_ent(load);
899 entity *sent = get_ia32_frame_ent(store);
900 ident *lid = get_ia32_am_sc(load);
901 ident *sid = get_ia32_am_sc(store);
902 char *loffs = get_ia32_am_offs(load);
903 char *soffs = get_ia32_am_offs(store);
905 /* are both entities set and equal? */
906 if (is_equal && (lent || sent))
907 is_equal = lent && sent && (lent == sent);
909 /* are address mode idents set and equal? */
910 if (is_equal && (lid || sid))
911 is_equal = lid && sid && (lid == sid);
913 /* are offsets set and equal */
914 if (is_equal && (loffs || soffs))
915 is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;
917 /* are the load and the store of the same mode? */
918 is_equal = is_equal ? get_ia32_ls_mode(load) == get_ia32_ls_mode(store) : 0;
923 typedef enum _ia32_take_lea_attr {
924 IA32_LEA_ATTR_NONE = 0,
925 IA32_LEA_ATTR_BASE = (1 << 0),
926 IA32_LEA_ATTR_INDEX = (1 << 1),
927 IA32_LEA_ATTR_OFFS = (1 << 2),
928 IA32_LEA_ATTR_SCALE = (1 << 3),
929 IA32_LEA_ATTR_AMSC = (1 << 4),
930 IA32_LEA_ATTR_FENT = (1 << 5)
931 } ia32_take_lea_attr;
934 * Decides if we have to keep the LEA operand or if we can assimilate it.
936 static int do_new_lea(ir_node *irn, ir_node *base, ir_node *index, ir_node *lea,
937 int have_am_sc, ia32_code_gen_t *cg)
939 entity *irn_ent = get_ia32_frame_ent(irn);
940 entity *lea_ent = get_ia32_frame_ent(lea);
942 int is_noreg_base = be_is_NoReg(cg, base);
943 int is_noreg_index = be_is_NoReg(cg, index);
944 ia32_am_flavour_t am_flav = get_ia32_am_flavour(lea);
946 /* If the Add and the LEA both have a different frame entity set: keep */
947 if (irn_ent && lea_ent && (irn_ent != lea_ent))
948 return IA32_LEA_ATTR_NONE;
949 else if (! irn_ent && lea_ent)
950 ret_val |= IA32_LEA_ATTR_FENT;
952 /* If the Add and the LEA both have already an address mode symconst: keep */
953 if (have_am_sc && get_ia32_am_sc(lea))
954 return IA32_LEA_ATTR_NONE;
955 else if (get_ia32_am_sc(lea))
956 ret_val |= IA32_LEA_ATTR_AMSC;
958 /* Check the different base-index combinations */
960 if (! is_noreg_base && ! is_noreg_index) {
961 /* Assimilate if base is the lea and the LEA is just a Base + Offset calculation */
962 if ((base == lea) && ! (am_flav & ia32_I ? 1 : 0)) {
963 if (am_flav & ia32_O)
964 ret_val |= IA32_LEA_ATTR_OFFS;
966 ret_val |= IA32_LEA_ATTR_BASE;
969 return IA32_LEA_ATTR_NONE;
971 else if (! is_noreg_base && is_noreg_index) {
972 /* Base is set but index not */
974 /* Base points to LEA: assimilate everything */
975 if (am_flav & ia32_O)
976 ret_val |= IA32_LEA_ATTR_OFFS;
977 if (am_flav & ia32_S)
978 ret_val |= IA32_LEA_ATTR_SCALE;
979 if (am_flav & ia32_I)
980 ret_val |= IA32_LEA_ATTR_INDEX;
982 ret_val |= IA32_LEA_ATTR_BASE;
984 else if (am_flav & ia32_B ? 0 : 1) {
985 /* Base is not the LEA but the LEA is an index only calculation: assimilate */
986 if (am_flav & ia32_O)
987 ret_val |= IA32_LEA_ATTR_OFFS;
988 if (am_flav & ia32_S)
989 ret_val |= IA32_LEA_ATTR_SCALE;
991 ret_val |= IA32_LEA_ATTR_INDEX;
994 return IA32_LEA_ATTR_NONE;
996 else if (is_noreg_base && ! is_noreg_index) {
997 /* Index is set but not base */
999 /* Index points to LEA: assimilate everything */
1000 if (am_flav & ia32_O)
1001 ret_val |= IA32_LEA_ATTR_OFFS;
1002 if (am_flav & ia32_S)
1003 ret_val |= IA32_LEA_ATTR_SCALE;
1004 if (am_flav & ia32_B)
1005 ret_val |= IA32_LEA_ATTR_BASE;
1007 ret_val |= IA32_LEA_ATTR_INDEX;
1009 else if (am_flav & ia32_I ? 0 : 1) {
1010 /* Index is not the LEA but the LEA is a base only calculation: assimilate */
1011 if (am_flav & ia32_O)
1012 ret_val |= IA32_LEA_ATTR_OFFS;
1013 if (am_flav & ia32_S)
1014 ret_val |= IA32_LEA_ATTR_SCALE;
1016 ret_val |= IA32_LEA_ATTR_BASE;
1019 return IA32_LEA_ATTR_NONE;
1022 assert(0 && "There must have been set base or index");
1029 * Adds res before irn into schedule if irn was scheduled.
1030 * @param irn The schedule point
1031 * @param res The node to be scheduled
1033 static INLINE void try_add_to_sched(ir_node *irn, ir_node *res) {
1034 if (sched_is_scheduled(irn))
1035 sched_add_before(irn, res);
1039 * Removes irn from schedule if it was scheduled. If irn is a mode_T node
1040 * all it's Projs are removed as well.
1041 * @param irn The irn to be removed from schedule
1043 static INLINE void try_remove_from_sched(ir_node *irn) {
1046 if (sched_is_scheduled(irn)) {
1047 if (get_irn_mode(irn) == mode_T) {
1048 const ir_edge_t *edge;
1049 foreach_out_edge(irn, edge) {
1050 ir_node *proj = get_edge_src_irn(edge);
1051 if (sched_is_scheduled(proj)) {
1052 set_irn_n(proj, 0, new_Bad());
1058 arity = get_irn_arity(irn);
1059 for(i = 0; i < arity; ++i) {
1060 set_irn_n(irn, i, new_Bad());
1067 * Folds Add or Sub to LEA if possible
1069 static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn, ir_node *noreg) {
1070 ir_graph *irg = get_irn_irg(irn);
1071 dbg_info *dbg = get_irn_dbg_info(irn);
1072 ir_node *block = get_nodes_block(irn);
1074 ir_node *shift = NULL;
1075 ir_node *lea_o = NULL;
1076 ir_node *lea = NULL;
1078 const char *offs_cnst = NULL;
1079 char *offs_lea = NULL;
1085 ident *am_sc = NULL;
1086 entity *lea_ent = NULL;
1087 ir_node *left, *right, *temp;
1088 ir_node *base, *index;
1089 int consumed_left_shift;
1090 ia32_am_flavour_t am_flav;
1091 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
1093 if (is_ia32_Add(irn))
1096 left = get_irn_n(irn, 2);
1097 right = get_irn_n(irn, 3);
1099 /* "normalize" arguments in case of add with two operands */
1100 if (isadd && ! be_is_NoReg(cg, right)) {
1101 /* put LEA == ia32_am_O as right operand */
1102 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
1103 set_irn_n(irn, 2, right);
1104 set_irn_n(irn, 3, left);
1110 /* put LEA != ia32_am_O as left operand */
1111 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
1112 set_irn_n(irn, 2, right);
1113 set_irn_n(irn, 3, left);
1119 /* put SHL as left operand iff left is NOT a LEA */
1120 if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
1121 set_irn_n(irn, 2, right);
1122 set_irn_n(irn, 3, left);
1135 /* check for operation with immediate */
1136 if (is_ia32_ImmConst(irn)) {
1137 DBG((mod, LEVEL_1, "\tfound op with imm const"));
1139 offs_cnst = get_ia32_cnst(irn);
1142 else if (isadd && is_ia32_ImmSymConst(irn)) {
1143 DBG((mod, LEVEL_1, "\tfound op with imm symconst"));
1147 am_sc = get_ia32_id_cnst(irn);
1148 am_sc_sign = is_ia32_am_sc_sign(irn);
1151 /* determine the operand which needs to be checked */
1152 temp = be_is_NoReg(cg, right) ? left : right;
1154 /* check if right operand is AMConst (LEA with ia32_am_O) */
1155 /* but we can only eat it up if there is no other symconst */
1156 /* because the linker won't accept two symconsts */
1157 if (! have_am_sc && is_ia32_Lea(temp) && get_ia32_am_flavour(temp) == ia32_am_O) {
1158 DBG((mod, LEVEL_1, "\tgot op with LEA am_O"));
1160 offs_lea = get_ia32_am_offs(temp);
1161 am_sc = get_ia32_am_sc(temp);
1162 am_sc_sign = is_ia32_am_sc_sign(temp);
1169 else if (temp == right)
1174 /* default for add -> make right operand to index */
1177 consumed_left_shift = -1;
1179 DBG((mod, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
1181 /* determine the operand which needs to be checked */
1183 if (is_ia32_Lea(left)) {
1185 consumed_left_shift = 0;
1188 /* check for SHL 1,2,3 */
1189 if (pred_is_specific_node(temp, is_ia32_Shl)) {
1190 temp = get_Proj_pred(temp);
1193 if (get_ia32_Immop_tarval(temp)) {
1194 scale = get_tarval_long(get_ia32_Immop_tarval(temp));
1197 index = get_irn_n(temp, 2);
1198 consumed_left_shift = consumed_left_shift < 0 ? 1 : 0;
1200 DBG((mod, LEVEL_1, "\tgot scaled index %+F\n", index));
1210 if (! be_is_NoReg(cg, index)) {
1211 /* if we have index, but left == right -> no base */
1212 if (left == right) {
1215 else if (consumed_left_shift == 1) {
1216 /* -> base is right operand */
1217 base = (right == lea_o) ? noreg : right;
1222 /* Try to assimilate a LEA as left operand */
1223 if (is_ia32_Lea(left) && (get_ia32_am_flavour(left) != ia32_am_O)) {
1224 /* check if we can assimilate the LEA */
1225 int take_attr = do_new_lea(irn, base, index, left, have_am_sc, cg);
1227 if (take_attr == IA32_LEA_ATTR_NONE) {
1228 DBG((mod, LEVEL_1, "\tleave old LEA, creating new one\n"));
1231 DBG((mod, LEVEL_1, "\tgot LEA as left operand ... assimilating\n"));
1232 lea = left; /* for statistics */
1234 if (take_attr & IA32_LEA_ATTR_OFFS)
1235 offs = get_ia32_am_offs(left);
1237 if (take_attr & IA32_LEA_ATTR_AMSC) {
1238 am_sc = get_ia32_am_sc(left);
1240 am_sc_sign = is_ia32_am_sc_sign(left);
1243 if (take_attr & IA32_LEA_ATTR_SCALE)
1244 scale = get_ia32_am_scale(left);
1246 if (take_attr & IA32_LEA_ATTR_BASE)
1247 base = get_irn_n(left, 0);
1249 if (take_attr & IA32_LEA_ATTR_INDEX)
1250 index = get_irn_n(left, 1);
1252 if (take_attr & IA32_LEA_ATTR_FENT)
1253 lea_ent = get_ia32_frame_ent(left);
1257 /* ok, we can create a new LEA */
1259 res = new_rd_ia32_Lea(dbg, irg, block, base, index, mode_Is);
1261 /* add the old offset of a previous LEA */
1263 add_ia32_am_offs(res, offs);
1266 /* add the new offset */
1269 add_ia32_am_offs(res, offs_cnst);
1272 add_ia32_am_offs(res, offs_lea);
1276 /* either lea_O-cnst, -cnst or -lea_O */
1279 add_ia32_am_offs(res, offs_lea);
1282 sub_ia32_am_offs(res, offs_cnst);
1285 sub_ia32_am_offs(res, offs_lea);
1289 /* set the address mode symconst */
1291 set_ia32_am_sc(res, am_sc);
1293 set_ia32_am_sc_sign(res);
1296 /* copy the frame entity (could be set in case of Add */
1297 /* which was a FrameAddr) */
1299 set_ia32_frame_ent(res, lea_ent);
1301 set_ia32_frame_ent(res, get_ia32_frame_ent(irn));
1303 if (get_ia32_frame_ent(res))
1304 set_ia32_use_frame(res);
1307 set_ia32_am_scale(res, scale);
1309 am_flav = ia32_am_N;
1310 /* determine new am flavour */
1311 if (offs || offs_cnst || offs_lea || have_am_sc) {
1314 if (! be_is_NoReg(cg, base)) {
1317 if (! be_is_NoReg(cg, index)) {
1323 set_ia32_am_flavour(res, am_flav);
1325 set_ia32_op_type(res, ia32_AddrModeS);
1327 SET_IA32_ORIG_NODE(res, ia32_get_old_node_name(cg, irn));
1329 DBG((mod, LEVEL_1, "\tLEA [%+F + %+F * %d + %s]\n", base, index, scale, get_ia32_am_offs(res)));
1331 /* we will exchange it, report here before the Proj is created */
1332 if (shift && lea && lea_o) {
1333 try_remove_from_sched(shift);
1334 try_remove_from_sched(lea);
1335 try_remove_from_sched(lea_o);
1336 DBG_OPT_LEA4(irn, lea_o, lea, shift, res);
1338 else if (shift && lea) {
1339 try_remove_from_sched(shift);
1340 try_remove_from_sched(lea);
1341 DBG_OPT_LEA3(irn, lea, shift, res);
1343 else if (shift && lea_o) {
1344 try_remove_from_sched(shift);
1345 try_remove_from_sched(lea_o);
1346 DBG_OPT_LEA3(irn, lea_o, shift, res);
1348 else if (lea && lea_o) {
1349 try_remove_from_sched(lea);
1350 try_remove_from_sched(lea_o);
1351 DBG_OPT_LEA3(irn, lea_o, lea, res);
1354 try_remove_from_sched(shift);
1355 DBG_OPT_LEA2(irn, shift, res);
1358 try_remove_from_sched(lea);
1359 DBG_OPT_LEA2(irn, lea, res);
1362 try_remove_from_sched(lea_o);
1363 DBG_OPT_LEA2(irn, lea_o, res);
1366 DBG_OPT_LEA1(irn, res);
1368 /* get the result Proj of the Add/Sub */
1369 try_add_to_sched(irn, res);
1370 try_remove_from_sched(irn);
1371 irn = ia32_get_res_proj(irn);
1373 assert(irn && "Couldn't find result proj");
1375 /* exchange the old op with the new LEA */
1384 * Merges a Load/Store node with a LEA.
1385 * @param irn The Load/Store node
1386 * @param lea The LEA
1388 static void merge_loadstore_lea(ir_node *irn, ir_node *lea) {
1389 entity *irn_ent = get_ia32_frame_ent(irn);
1390 entity *lea_ent = get_ia32_frame_ent(lea);
1392 /* If the irn and the LEA both have a different frame entity set: do not merge */
1393 if (irn_ent && lea_ent && (irn_ent != lea_ent))
1395 else if (! irn_ent && lea_ent) {
1396 set_ia32_frame_ent(irn, lea_ent);
1397 set_ia32_use_frame(irn);
1400 /* get the AM attributes from the LEA */
1401 add_ia32_am_offs(irn, get_ia32_am_offs(lea));
1402 set_ia32_am_scale(irn, get_ia32_am_scale(lea));
1403 set_ia32_am_flavour(irn, get_ia32_am_flavour(lea));
1405 set_ia32_am_sc(irn, get_ia32_am_sc(lea));
1406 if (is_ia32_am_sc_sign(lea))
1407 set_ia32_am_sc_sign(irn);
1409 set_ia32_op_type(irn, is_ia32_Ld(irn) ? ia32_AddrModeS : ia32_AddrModeD);
1411 /* set base and index */
1412 set_irn_n(irn, 0, get_irn_n(lea, 0));
1413 set_irn_n(irn, 1, get_irn_n(lea, 1));
1415 try_remove_from_sched(lea);
1417 /* clear remat flag */
1418 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1420 if (is_ia32_Ld(irn))
1421 DBG_OPT_LOAD_LEA(lea, irn);
1423 DBG_OPT_STORE_LEA(lea, irn);
1428 * Sets new_right index of irn to right and new_left index to left.
1429 * Also exchange left and right
1431 static void exchange_left_right(ir_node *irn, ir_node **left, ir_node **right, int new_left, int new_right) {
1434 set_irn_n(irn, new_right, *right);
1435 set_irn_n(irn, new_left, *left);
1441 /* this is only needed for Compares, but currently ALL nodes
1442 * have this attribute :-) */
1443 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1447 * Performs address calculation optimization (create LEAs if possible)
1449 static void optimize_lea(ir_node *irn, void *env) {
1450 ia32_code_gen_t *cg = env;
1451 ir_node *block, *noreg_gp, *left, *right;
1453 if (! is_ia32_irn(irn))
1456 /* Following cases can occur: */
1457 /* - Sub (l, imm) -> LEA [base - offset] */
1458 /* - Sub (l, r == LEA with ia32_am_O) -> LEA [base - offset] */
1459 /* - Add (l, imm) -> LEA [base + offset] */
1460 /* - Add (l, r == LEA with ia32_am_O) -> LEA [base + offset] */
1461 /* - Add (l == LEA with ia32_am_O, r) -> LEA [base + offset] */
1462 /* - Add (l, r) -> LEA [base + index * scale] */
1463 /* with scale > 1 iff l/r == shl (1,2,3) */
1465 if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
1466 left = get_irn_n(irn, 2);
1467 right = get_irn_n(irn, 3);
1468 block = get_nodes_block(irn);
1469 noreg_gp = ia32_new_NoReg_gp(cg);
1471 /* Do not try to create a LEA if one of the operands is a Load. */
1472 /* check is irn is a candidate for address calculation */
1473 if (is_addr_candidate(block, irn)) {
1476 DBG((cg->mod, LEVEL_1, "\tfound address calculation candidate %+F ... ", irn));
1477 res = fold_addr(cg, irn, noreg_gp);
1480 DB((cg->mod, LEVEL_1, "transformed into %+F\n", res));
1482 DB((cg->mod, LEVEL_1, "not transformed\n"));
1485 else if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn)) {
1486 /* - Load -> LEA into Load } TODO: If the LEA is used by more than one Load/Store */
1487 /* - Store -> LEA into Store } it might be better to keep the LEA */
1488 left = get_irn_n(irn, 0);
1490 if (is_ia32_Lea(left)) {
1491 const ir_edge_t *edge, *ne;
1494 /* merge all Loads/Stores connected to this LEA with the LEA */
1495 foreach_out_edge_safe(left, edge, ne) {
1496 src = get_edge_src_irn(edge);
1498 if (src && (get_edge_src_pos(edge) == 0) && (is_ia32_Ld(src) || is_ia32_St(src) || is_ia32_Store8Bit(src))) {
1499 DBG((cg->mod, LEVEL_1, "\nmerging %+F into %+F\n", left, irn));
1500 if (! is_ia32_got_lea(src))
1501 merge_loadstore_lea(src, left);
1502 set_ia32_got_lea(src);
1510 * Checks for address mode patterns and performs the
1511 * necessary transformations.
1512 * This function is called by a walker.
1514 static void optimize_am(ir_node *irn, void *env) {
1515 ia32_am_opt_env_t *am_opt_env = env;
1516 ia32_code_gen_t *cg = am_opt_env->cg;
1517 heights_t *h = am_opt_env->h;
1518 ir_node *block, *left, *right;
1519 ir_node *store, *load, *mem_proj;
1520 ir_node *succ, *addr_b, *addr_i;
1521 int check_am_src = 0;
1522 int need_exchange_on_fail = 0;
1523 DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
1525 if (! is_ia32_irn(irn) || is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn))
1528 block = get_nodes_block(irn);
1530 DBG((mod, LEVEL_1, "checking for AM\n"));
1532 /* fold following patterns: */
1533 /* - op -> Load into AMop with am_Source */
1535 /* - op is am_Source capable AND */
1536 /* - the Load is only used by this op AND */
1537 /* - the Load is in the same block */
1538 /* - Store -> op -> Load into AMop with am_Dest */
1540 /* - op is am_Dest capable AND */
1541 /* - the Store uses the same address as the Load AND */
1542 /* - the Load is only used by this op AND */
1543 /* - the Load and Store are in the same block AND */
1544 /* - nobody else uses the result of the op */
1546 if ((get_ia32_am_support(irn) != ia32_am_None) && ! is_ia32_Lea(irn)) {
1547 ia32_am_cand_t cand = is_am_candidate(cg, h, block, irn);
1548 ia32_am_cand_t orig_cand = cand;
1550 /* cand == 1: load is left; cand == 2: load is right; */
1552 if (cand == IA32_AM_CAND_NONE)
1555 DBG((mod, LEVEL_1, "\tfound address mode candidate %+F ... ", irn));
1557 left = get_irn_n(irn, 2);
1558 if (get_irn_arity(irn) == 4) {
1559 /* it's an "unary" operation */
1561 cand = IA32_AM_CAND_BOTH;
1564 right = get_irn_n(irn, 3);
1567 /* normalize commutative ops */
1568 if (node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_RIGHT)) {
1570 /* Assure that left operand is always a Load if there is one */
1571 /* because non-commutative ops can only use Dest AM if the left */
1572 /* operand is a load, so we only need to check left operand. */
1574 exchange_left_right(irn, &left, &right, 3, 2);
1575 need_exchange_on_fail = 1;
1577 /* now: load is right */
1578 cand = IA32_AM_CAND_LEFT;
1581 /* check for Store -> op -> Load */
1583 /* Store -> op -> Load optimization is only possible if supported by op */
1584 /* and if right operand is a Load */
1585 if ((get_ia32_am_support(irn) & ia32_am_Dest) && (cand & IA32_AM_CAND_LEFT))
1587 /* An address mode capable op always has a result Proj. */
1588 /* If this Proj is used by more than one other node, we don't need to */
1589 /* check further, otherwise we check for Store and remember the address, */
1590 /* the Store points to. */
1592 succ = ia32_get_res_proj(irn);
1593 assert(succ && "Couldn't find result proj");
1599 /* now check for users and Store */
1600 if (ia32_get_irn_n_edges(succ) == 1) {
1601 succ = get_edge_src_irn(get_irn_out_edge_first(succ));
1603 if (is_ia32_xStore(succ) || is_ia32_Store(succ)) {
1605 addr_b = get_irn_n(store, 0);
1606 addr_i = get_irn_n(store, 1);
1611 /* we found a Store as single user: Now check for Load */
1613 /* skip the Proj for easier access */
1614 load = is_Proj(right) ? (is_ia32_Load(get_Proj_pred(right)) ? get_Proj_pred(right) : NULL) : NULL;
1616 /* Extra check for commutative ops with two Loads */
1617 /* -> put the interesting Load left */
1618 if (load && node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_BOTH)) {
1619 if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1620 /* We exchange left and right, so it's easier to kill */
1621 /* the correct Load later and to handle unary operations. */
1622 exchange_left_right(irn, &left, &right, 3, 2);
1623 need_exchange_on_fail ^= 1;
1627 /* we have to be the only user of the load */
1628 if(get_irn_n_edges(left) > 1) {
1633 /* skip the Proj for easier access */
1634 load = get_Proj_pred(left);
1636 /* Compare Load and Store address */
1637 if (load_store_addr_is_equal(load, store, addr_b, addr_i)) {
1638 /* Left Load is from same address, so we can */
1639 /* disconnect the Load and Store here */
1641 /* set new base, index and attributes */
1642 set_irn_n(irn, 0, addr_b);
1643 set_irn_n(irn, 1, addr_i);
1644 add_ia32_am_offs(irn, get_ia32_am_offs(load));
1645 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1646 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1647 set_ia32_op_type(irn, ia32_AddrModeD);
1648 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1649 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1651 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1652 if (is_ia32_am_sc_sign(load))
1653 set_ia32_am_sc_sign(irn);
1655 if (is_ia32_use_frame(load))
1656 set_ia32_use_frame(irn);
1658 /* connect to Load memory and disconnect Load */
1659 if (get_irn_arity(irn) == 5) {
1661 set_irn_n(irn, 4, get_irn_n(load, 2));
1662 set_irn_n(irn, 2, ia32_get_admissible_noreg(cg, irn, 2));
1666 set_irn_n(irn, 3, get_irn_n(load, 2));
1667 set_irn_n(irn, 2, ia32_get_admissible_noreg(cg, irn, 2));
1670 /* connect the memory Proj of the Store to the op */
1671 mem_proj = ia32_get_proj_for_mode(store, mode_M);
1672 set_Proj_pred(mem_proj, irn);
1673 set_Proj_proj(mem_proj, 1);
1675 /* clear remat flag */
1676 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1678 try_remove_from_sched(load);
1679 try_remove_from_sched(store);
1680 DBG_OPT_AM_D(load, store, irn);
1682 DB((mod, LEVEL_1, "merged with %+F and %+F into dest AM\n", load, store));
1684 need_exchange_on_fail = 0;
1687 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1688 /* There was no store, check if we still can optimize for source address mode */
1691 } /* if (support AM Dest) */
1692 else if (get_ia32_am_support(irn) & ia32_am_Source) {
1693 /* op doesn't support am AM Dest -> check for AM Source */
1697 /* was exchanged but optimize failed: exchange back */
1698 if (need_exchange_on_fail) {
1699 exchange_left_right(irn, &left, &right, 3, 2);
1703 need_exchange_on_fail = 0;
1705 /* normalize commutative ops */
1706 if (check_am_src && node_is_ia32_comm(irn) && (cand == IA32_AM_CAND_LEFT)) {
1708 /* Assure that right operand is always a Load if there is one */
1709 /* because non-commutative ops can only use Source AM if the */
1710 /* right operand is a Load, so we only need to check the right */
1711 /* operand afterwards. */
1713 exchange_left_right(irn, &left, &right, 3, 2);
1714 need_exchange_on_fail = 1;
1716 /* now: load is left */
1717 cand = IA32_AM_CAND_RIGHT;
1720 /* optimize op -> Load iff Load is only used by this op */
1721 /* and right operand is a Load which only used by this irn */
1723 (cand & IA32_AM_CAND_RIGHT) &&
1724 (ia32_get_irn_n_edges(right) == 1))
1726 ir_node *load = get_Proj_pred(right);
1728 addr_b = get_irn_n(load, 0);
1729 addr_i = get_irn_n(load, 1);
1731 /* set new base, index and attributes */
1732 set_irn_n(irn, 0, addr_b);
1733 set_irn_n(irn, 1, addr_i);
1734 add_ia32_am_offs(irn, get_ia32_am_offs(load));
1735 set_ia32_am_scale(irn, get_ia32_am_scale(load));
1736 set_ia32_am_flavour(irn, get_ia32_am_flavour(load));
1737 set_ia32_op_type(irn, ia32_AddrModeS);
1738 set_ia32_frame_ent(irn, get_ia32_frame_ent(load));
1739 set_ia32_ls_mode(irn, get_ia32_ls_mode(load));
1741 set_ia32_am_sc(irn, get_ia32_am_sc(load));
1742 if (is_ia32_am_sc_sign(load))
1743 set_ia32_am_sc_sign(irn);
1745 /* clear remat flag */
1746 set_ia32_flags(irn, get_ia32_flags(irn) & ~arch_irn_flags_rematerializable);
1748 if (is_ia32_use_frame(load))
1749 set_ia32_use_frame(irn);
1751 /* connect to Load memory and disconnect Load */
1752 if (get_irn_arity(irn) == 5) {
1754 set_irn_n(irn, 4, get_irn_n(load, 2));
1755 set_irn_n(irn, 3, ia32_get_admissible_noreg(cg, irn, 3));
1757 assert(get_irn_arity(irn) == 4);
1759 set_irn_n(irn, 3, get_irn_n(load, 2));
1760 set_irn_n(irn, 2, ia32_get_admissible_noreg(cg, irn, 2));
1763 /* this is only needed for Compares, but currently ALL nodes
1764 * have this attribute :-) */
1765 set_ia32_pncode(irn, get_inversed_pnc(get_ia32_pncode(irn)));
1767 DBG_OPT_AM_S(load, irn);
1769 /* If Load has a memory Proj, connect it to the op */
1770 mem_proj = ia32_get_proj_for_mode(load, mode_M);
1772 set_Proj_pred(mem_proj, irn);
1773 set_Proj_proj(mem_proj, 1);
1776 try_remove_from_sched(load);
1778 DB((mod, LEVEL_1, "merged with %+F into source AM\n", load));
1781 /* was exchanged but optimize failed: exchange back */
1782 if (need_exchange_on_fail)
1783 exchange_left_right(irn, &left, &right, 3, 2);
1789 * Performs address mode optimization.
1791 void ia32_optimize_addressmode(ia32_code_gen_t *cg) {
1792 /* if we are supposed to do AM or LEA optimization: recalculate edges */
1793 if (cg->opt & (IA32_OPT_DOAM | IA32_OPT_LEA)) {
1794 edges_deactivate(cg->irg);
1795 edges_activate(cg->irg);
1798 /* no optimizations at all */
1802 /* beware: we cannot optimize LEA and AM in one run because */
1803 /* LEA optimization adds new nodes to the irg which */
1804 /* invalidates the phase data */
1806 if (cg->opt & IA32_OPT_LEA) {
1807 irg_walk_blkwise_graph(cg->irg, NULL, optimize_lea, cg);
1811 be_dump(cg->irg, "-lea", dump_ir_block_graph_sched);
1813 if (cg->opt & IA32_OPT_DOAM) {
1814 /* we need height information for am optimization */
1815 heights_t *h = heights_new(cg->irg);
1816 ia32_am_opt_env_t env;
1821 irg_walk_blkwise_graph(cg->irg, NULL, optimize_am, &env);