8 #include "firm_types.h"
13 #include "../benode_t.h"
15 #include "ia32_new_nodes.h"
16 #include "bearch_ia32_t.h"
19 #define is_NoMem(irn) (get_irn_op(irn) == op_NoMem)
22 * creates a unique ident by adding a number to a tag
24 * @param tag the tag string, must contain a %d if a number
27 static ident *unique_id(const char *tag)
29 static unsigned id = 0;
32 snprintf(str, sizeof(str), tag, ++id);
33 return new_id_from_str(str);
39 * Transforms a SymConst.
41 * @param mod the debug module
42 * @param block the block the new node should belong to
43 * @param node the ir SymConst node
44 * @param mode mode of the SymConst
45 * @return the created ia32 Const node
47 static ir_node *gen_SymConst(ia32_transform_env_t *env) {
49 dbg_info *dbg = env->dbg;
50 ir_mode *mode = env->mode;
51 ir_graph *irg = env->irg;
52 ir_node *block = env->block;
54 cnst = new_rd_ia32_Const(dbg, irg, block, mode);
55 set_ia32_Const_attr(cnst, env->irn);
60 * Get a primitive type for a mode.
62 static ir_type *get_prim_type(pmap *types, ir_mode *mode)
64 pmap_entry *e = pmap_find(types, mode);
69 snprintf(buf, sizeof(buf), "prim_type_%s", get_mode_name(mode));
70 res = new_type_primitive(new_id_from_str(buf), mode);
71 pmap_insert(types, mode, res);
79 * Get an entity that is initialized with a tarval
81 static entity *get_entity_for_tv(ia32_code_gen_t *cg, ir_node *cnst)
83 tarval *tv = get_Const_tarval(cnst);
84 pmap_entry *e = pmap_find(cg->tv_ent, tv);
89 ir_mode *mode = get_irn_mode(cnst);
90 ir_type *tp = get_Const_type(cnst);
91 if (tp == firm_unknown_type)
92 tp = get_prim_type(cg->types, mode);
94 res = new_entity(get_glob_type(), unique_id("ia32FloatCnst_%u"), tp);
96 set_entity_ld_ident(res, get_entity_ident(res));
97 set_entity_visibility(res, visibility_local);
98 set_entity_variability(res, variability_constant);
99 set_entity_allocation(res, allocation_static);
101 /* we create a new entity here: It's initialization must resist on the
103 rem = current_ir_graph;
104 current_ir_graph = get_const_code_irg();
105 set_atomic_ent_value(res, new_Const_type(tv, tp));
106 current_ir_graph = rem;
114 * Transforms a Const.
116 * @param mod the debug module
117 * @param block the block the new node should belong to
118 * @param node the ir Const node
119 * @param mode mode of the Const
120 * @return the created ia32 Const node
122 static ir_node *gen_Const(ia32_transform_env_t *env) {
125 ir_graph *irg = env->irg;
126 ir_node *block = env->block;
127 ir_node *node = env->irn;
128 dbg_info *dbg = env->dbg;
129 ir_mode *mode = env->mode;
131 if (mode_is_float(mode)) {
132 sym.entity_p = get_entity_for_tv(env->cg, node);
134 cnst = new_rd_SymConst(dbg, irg, block, sym, symconst_addr_ent);
136 cnst = gen_SymConst(env);
139 cnst = new_rd_ia32_Const(dbg, irg, block, get_irn_mode(node));
140 set_ia32_Const_attr(cnst, node);
148 * Transforms (all) Const's into ia32_Const and places them in the
149 * block where they are used (or in the cfg-pred Block in case of Phi's)
151 void ia32_place_consts(ir_node *irn, void *env) {
152 ia32_code_gen_t *cg = env;
153 ia32_transform_env_t tenv;
155 ir_node *pred, *cnst;
162 mode = get_irn_mode(irn);
164 tenv.arch_env = cg->arch_env;
165 tenv.block = get_nodes_block(irn);
170 /* Loop over all predecessors and check for Sym/Const nodes */
171 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
172 pred = get_irn_n(irn, i);
174 opc = get_irn_opcode(pred);
176 tenv.mode = get_irn_mode(pred);
177 tenv.dbg = get_irn_dbg_info(pred);
179 /* If it's a Phi, then we need to create the */
180 /* new Const in it's predecessor block */
182 tenv.block = get_Block_cfgpred_block(get_nodes_block(irn), i);
187 cnst = gen_Const(&tenv);
190 cnst = gen_SymConst(&tenv);
196 /* if we found a const, then set it */
198 set_irn_n(irn, i, cnst);
204 /******************************************************************
206 * /\ | | | | | \/ | | |
207 * / \ __| | __| |_ __ ___ ___ ___| \ / | ___ __| | ___
208 * / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
209 * / ____ \ (_| | (_| | | | __/\__ \__ \ | | | (_) | (_| | __/
210 * /_/ \_\__,_|\__,_|_| \___||___/___/_| |_|\___/ \__,_|\___|
212 ******************************************************************/
214 static int node_is_comm(const ir_node *irn) {
215 if (is_ia32_Add(irn) ||
238 * Returns the first mode_M Proj connected to irn.
240 static ir_node *get_mem_proj(const ir_node *irn) {
241 const ir_edge_t *edge;
244 assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
246 foreach_out_edge(irn, edge) {
247 src = get_edge_src_irn(edge);
249 assert(is_Proj(src) && "Proj expected");
251 if (get_irn_mode(src) == mode_M)
259 * Returns the Proj with number 0 connected to irn.
261 static ir_node *get_res_proj(const ir_node *irn) {
262 const ir_edge_t *edge;
265 assert(get_irn_mode(irn) == mode_T && "expected mode_T node");
267 foreach_out_edge(irn, edge) {
268 src = get_edge_src_irn(edge);
270 assert(is_Proj(src) && "Proj expected");
272 if (get_Proj_proj(src) == 0)
281 * Determines if irn is a Proj and if is_op_func returns true for it's predecessor.
283 static int pred_is_specific_node(const ir_node *irn, int (*is_op_func)(const ir_node *n)) {
284 if (is_Proj(irn) && is_op_func(get_Proj_pred(irn))) {
292 * Folds Add or Sub to LEA if possible
294 static ir_node *fold_addr(ir_node *irn, firm_dbg_module_t *mod, ir_node *noreg) {
295 ir_graph *irg = get_irn_irg(irn);
296 ir_mode *mode = get_irn_mode(irn);
297 dbg_info *dbg = get_irn_dbg_info(irn);
298 ir_node *block = get_nodes_block(irn);
301 char *new_offs = NULL;
305 ir_node *left, *right, *temp;
306 ir_node *base, *index;
307 ia32_am_flavour_t am_flav;
309 if (is_ia32_Add(irn))
312 left = get_irn_n(irn, 2);
313 right = get_irn_n(irn, 3);
315 /* "normalize" arguments in case of add */
317 /* put LEA == ia32_am_O as right operand */
318 if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
319 set_irn_n(irn, 2, right);
320 set_irn_n(irn, 3, left);
326 /* put LEA != ia32_am_O as left operand */
327 if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
328 set_irn_n(irn, 2, right);
329 set_irn_n(irn, 3, left);
335 /* put SHL as right operand */
336 if (pred_is_specific_node(left, is_ia32_Shl)) {
337 set_irn_n(irn, 2, right);
338 set_irn_n(irn, 3, left);
345 /* Left operand could already be a LEA */
346 if (is_ia32_Lea(left)) {
347 DBG((mod, LEVEL_1, "\tgot LEA as left operand\n"));
349 base = get_irn_n(left, 0);
350 index = get_irn_n(left, 1);
351 offs = get_ia32_am_offs(left);
352 scale = get_ia32_am_scale(left);
362 /* check if operand is either const or right operand is AMConst (LEA with ia32_am_O) */
363 if (get_ia32_cnst(irn)) {
364 DBG((mod, LEVEL_1, "\tfound op with imm"));
366 new_offs = get_ia32_cnst(irn);
369 else if (is_ia32_Lea(right) && get_ia32_am_flavour(right) == ia32_am_O) {
370 DBG((mod, LEVEL_1, "\tgot op with LEA am_O"));
372 new_offs = get_ia32_am_offs(right);
375 /* we can only get an additional index if there isn't already one */
376 else if (isadd && be_is_NoReg(index)) {
377 /* default for add -> make right operand to index */
381 DBG((mod, LEVEL_1, "\tgot LEA candidate with index %+F\n", index));
382 /* check for SHL 1,2,3 */
383 if (pred_is_specific_node(right, is_ia32_Shl)) {
384 temp = get_Proj_pred(right);
386 if (get_ia32_Immop_tarval(temp)) {
387 scale = get_tarval_long(get_ia32_Immop_tarval(temp));
391 index = get_irn_n(temp, 2);
393 DBG((mod, LEVEL_1, "\tgot scaled index %+F\n", index));
399 /* ok, we can create a new LEA */
401 res = new_rd_ia32_Lea(dbg, irg, block, base, index, mode_Is);
403 /* add the old offset of a previous LEA */
405 add_ia32_am_offs(res, offs);
408 /* add the new offset */
411 add_ia32_am_offs(res, new_offs);
415 sub_ia32_am_offs(res, new_offs);
419 set_ia32_am_scale(res, scale);
422 /* determine new am flavour */
423 if (offs || new_offs) {
426 if (! be_is_NoReg(base)) {
429 if (! be_is_NoReg(index)) {
435 set_ia32_am_flavour(res, am_flav);
437 set_ia32_op_type(res, ia32_AddrModeS);
439 DBG((mod, LEVEL_1, "\tLEA [%+F + %+F * %d + %s]\n", base, index, scale, get_ia32_am_offs(res)));
441 /* get the result Proj of the Add/Sub */
442 irn = get_res_proj(irn);
444 assert(irn && "Couldn't find result proj");
446 /* exchange the old op with the new LEA */
454 * Optimizes a pattern around irn to address mode if possible.
456 void ia32_optimize_am(ir_node *irn, void *env) {
457 ia32_code_gen_t *cg = env;
458 ir_graph *irg = cg->irg;
459 firm_dbg_module_t *mod = cg->mod;
463 ir_node *block, *noreg_gp, *noreg_fp;
464 ir_node *left, *right, *temp;
465 ir_node *store, *mem_proj;
466 ir_node *succ, *addr_b, *addr_i;
467 int check_am_src = 0;
469 if (! is_ia32_irn(irn))
472 dbg = get_irn_dbg_info(irn);
473 mode = get_irn_mode(irn);
474 block = get_nodes_block(irn);
475 noreg_gp = ia32_new_NoReg_gp(cg);
476 noreg_fp = ia32_new_NoReg_fp(cg);
478 DBG((mod, LEVEL_1, "checking for AM\n"));
480 /* 1st part: check for address calculations and transform the into Lea */
482 /* Following cases can occur: */
483 /* - Sub (l, imm) -> LEA [base - offset] */
484 /* - Sub (l, r == LEA with ia32_am_O) -> LEA [base - offset] */
485 /* - Add (l, imm) -> LEA [base + offset] */
486 /* - Add (l, r == LEA with ia32_am_O) -> LEA [base + offset] */
487 /* - Add (l == LEA with ia32_am_O, r) -> LEA [base + offset] */
488 /* - Add (l, r) -> LEA [base + index * scale] */
489 /* with scale > 1 iff l/r == shl (1,2,3) */
491 if (is_ia32_Sub(irn) || is_ia32_Add(irn)) {
492 left = get_irn_n(irn, 2);
493 right = get_irn_n(irn, 3);
495 /* Do not try to create a LEA if one of the operands is a Load. */
496 if (! pred_is_specific_node(left, is_ia32_Load) &&
497 ! pred_is_specific_node(right, is_ia32_Load))
499 res = fold_addr(irn, mod, noreg_gp);
503 /* 2nd part: fold following patterns:
504 /* - Load -> LEA into Load } TODO: If the LEA is used by more than one Load/Store */
505 /* - Store -> LEA into Store } it might be better to keep the LEA */
506 /* - op -> Load into AMop with am_Source
508 /* - op is am_Source capable AND */
509 /* - the Load is only used by this op AND */
510 /* - the Load is in the same block */
511 /* - Store -> op -> Load into AMop with am_Dest */
513 /* - op is am_Dest capable AND */
514 /* - the Store uses the same address as the Load AND */
515 /* - the Load is only used by this op AND */
516 /* - the Load and Store are in the same block AND */
517 /* - nobody else uses the result of the op */
519 if ((res == irn) && (get_ia32_am_support(irn) != ia32_am_None) && !is_ia32_Lea(irn)) {
520 /* 1st: check for Load/Store -> LEA */
521 if (is_ia32_Load(irn) || is_ia32_fLoad(irn) ||
522 is_ia32_Store(irn) || is_ia32_fStore(irn))
524 left = get_irn_n(irn, 0);
526 if (is_ia32_Lea(left)) {
527 /* get the AM attributes from the LEA */
528 add_ia32_am_offs(irn, get_ia32_am_offs(left));
529 set_ia32_am_scale(irn, get_ia32_am_scale(left));
530 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
531 set_ia32_op_type(irn, get_ia32_op_type(left));
533 /* set base and index */
534 set_irn_n(irn, 0, get_irn_n(left, 0));
535 set_irn_n(irn, 1, get_irn_n(left, 1));
538 /* check if at least one operand is a Load */
539 else if (pred_is_specific_node(get_irn_n(irn, 2), is_ia32_Load) ||
540 pred_is_specific_node(get_irn_n(irn, 2), is_ia32_fLoad) ||
541 pred_is_specific_node(get_irn_n(irn, 3), is_ia32_Load) ||
542 pred_is_specific_node(get_irn_n(irn, 3), is_ia32_fLoad))
545 /* normalize commutative ops */
546 if (node_is_comm(irn)) {
547 left = get_irn_n(irn, 2);
548 right = get_irn_n(irn, 3);
550 /* assure that Left operand is always a Load if there is one */
551 if (pred_is_specific_node(right, is_ia32_Load) ||
552 pred_is_specific_node(right, is_ia32_fLoad))
554 set_irn_n(irn, 2, right);
555 set_irn_n(irn, 3, left);
563 /* check for Store -> op -> Load */
565 /* Store -> op -> Load optimization is only possible if supported by op */
566 if (get_ia32_am_support(irn) & ia32_am_Dest) {
568 /* An address mode capable op always has a result Proj. */
569 /* If this Proj is used by more than one other node, we don't need to */
570 /* check further, otherwise we check for Store and remember the address, */
571 /* the Store points to. */
573 succ = get_res_proj(irn);
574 assert(succ && "Couldn't find result proj");
580 /* now check for users and Store */
581 if (get_irn_n_edges(succ) == 1) {
582 succ = get_edge_src_irn(get_irn_out_edge_first(succ));
584 if (is_ia32_fStore(succ) || is_ia32_Store(succ)) {
586 addr_b = get_irn_n(store, 0);
588 /* Could be that the Store is connected to the address */
589 /* calculating LEA while the Load is already transformed. */
590 if (is_ia32_Lea(addr_b)) {
592 addr_b = get_irn_n(succ, 0);
593 addr_i = get_irn_n(succ, 1);
602 /* we found a Store as single user: Now check for Load */
603 left = get_irn_n(irn, 2);
604 right = get_irn_n(irn, 3);
606 /* Could be that the right operand is also a Load, so we make */
607 /* sure that the "interesting" Load is always the left one */
609 /* right != NoMem means, we have a "binary" operation */
610 if (! is_NoMem(right) &&
611 (pred_is_specific_node(right, is_ia32_Load) ||
612 pred_is_specific_node(right, is_ia32_fLoad)))
614 if ((addr_b == get_irn_n(get_Proj_pred(right), 0)) &&
615 (addr_i == get_irn_n(get_Proj_pred(right), 1)))
617 /* We exchange left and right, so it's easier to kill */
618 /* the correct Load later and to handle unary operations. */
619 set_irn_n(irn, 2, right);
620 set_irn_n(irn, 3, left);
628 /* skip the Proj for easier access */
629 left = get_Proj_pred(left);
631 /* Compare Load and Store address */
632 if ((addr_b == get_irn_n(left, 0)) && (addr_i == get_irn_n(left, 1)))
634 /* Left Load is from same address, so we can */
635 /* disconnect the Load and Store here */
637 /* set new base, index and attributes */
638 set_irn_n(irn, 0, addr_b);
639 set_irn_n(irn, 1, addr_i);
640 add_ia32_am_offs(irn, get_ia32_am_offs(left));
641 set_ia32_am_scale(irn, get_ia32_am_scale(left));
642 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
643 set_ia32_op_type(irn, ia32_AddrModeD);
645 /* connect to Load memory */
646 if (get_irn_arity(irn) == 5) {
648 set_irn_n(irn, 4, get_irn_n(left, 2));
652 set_irn_n(irn, 3, get_irn_n(left, 2));
655 /* disconnect from Load */
656 set_irn_n(irn, 2, noreg_gp);
658 /* connect the memory Proj of the Store to the op */
659 mem_proj = get_mem_proj(store);
660 set_Proj_pred(mem_proj, irn);
661 set_Proj_proj(mem_proj, 1);
664 else if (get_ia32_am_support(irn) & ia32_am_Source) {
665 /* There was no store, check if we still can optimize for source address mode */
668 } /* if (support AM Dest) */
670 /* op doesn't support am AM Dest -> check for AM Source */
674 /* optimize op -> Load iff Load is only used by this op */
676 left = get_irn_n(irn, 2);
678 if (get_irn_n_edges(left) == 1) {
679 left = get_Proj_pred(left);
681 addr_b = get_irn_n(left, 0);
682 addr_i = get_irn_n(left, 1);
684 /* set new base, index and attributes */
685 set_irn_n(irn, 0, addr_b);
686 set_irn_n(irn, 1, addr_i);
687 add_ia32_am_offs(irn, get_ia32_am_offs(left));
688 set_ia32_am_scale(irn, get_ia32_am_scale(left));
689 set_ia32_am_flavour(irn, get_ia32_am_flavour(left));
690 set_ia32_op_type(irn, ia32_AddrModeS);
692 /* connect to Load memory */
693 if (get_irn_arity(irn) == 5) {
695 set_irn_n(irn, 4, get_irn_n(left, 2));
699 set_irn_n(irn, 3, get_irn_n(left, 2));
702 /* disconnect from Load */
703 set_irn_n(irn, 2, noreg_gp);
705 /* If Load has a memory Proj, connect it to the op */
706 mem_proj = get_mem_proj(left);
708 set_Proj_pred(mem_proj, irn);
709 set_Proj_proj(mem_proj, 1);