2 * This is the main ia32 firm backend driver.
17 #include "pseudo_irg.h"
21 #include "iredges_t.h"
28 #include "../beabi.h" /* the general register allocator interface */
29 #include "../benode_t.h"
30 #include "../belower.h"
31 #include "../besched_t.h"
33 #include "bearch_ia32_t.h"
35 #include "ia32_new_nodes.h" /* ia32 nodes interface */
36 #include "gen_ia32_regalloc_if.h" /* the generated interface (register type and class defenitions) */
37 #include "ia32_gen_decls.h" /* interface declaration emitter */
38 #include "ia32_transform.h"
39 #include "ia32_emitter.h"
40 #include "ia32_map_regs.h"
41 #include "ia32_optimize.h"
43 #define DEBUG_MODULE "firm.be.ia32.isa"
46 static set *cur_reg_set = NULL;
49 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
51 ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
52 return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_gp_regs[REG_XXX]);
55 ir_node *ia32_new_NoReg_fp(ia32_code_gen_t *cg) {
56 return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_fp_regs[REG_XXXX]);
59 /**************************************************
62 * _ __ ___ __ _ __ _| | | ___ ___ _| |_
63 * | '__/ _ \/ _` | / _` | | |/ _ \ / __| | | _|
64 * | | | __/ (_| | | (_| | | | (_) | (__ | | |
65 * |_| \___|\__, | \__,_|_|_|\___/ \___| |_|_|
68 **************************************************/
70 static ir_node *my_skip_proj(const ir_node *n) {
77 * Return register requirements for an ia32 node.
78 * If the node returns a tuple (mode_T) then the proj's
79 * will be asked for this information.
81 static const arch_register_req_t *ia32_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos) {
82 const ia32_register_req_t *irn_req;
83 long node_pos = pos == -1 ? 0 : pos;
84 ir_mode *mode = get_irn_mode(irn);
85 firm_dbg_module_t *mod = firm_dbg_register(DEBUG_MODULE);
87 if (mode == mode_T || mode == mode_M) {
88 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
92 DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
97 node_pos = ia32_translate_proj_pos(irn);
103 irn = my_skip_proj(irn);
105 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
108 if (is_ia32_irn(irn)) {
110 irn_req = get_ia32_in_req(irn, pos);
113 irn_req = get_ia32_out_req(irn, node_pos);
116 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
118 memcpy(req, &(irn_req->req), sizeof(*req));
120 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
121 assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
122 req->other_same = get_irn_n(irn, irn_req->same_pos);
125 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
126 assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
127 req->other_different = get_irn_n(irn, irn_req->different_pos);
131 /* treat Phi like Const with default requirements */
133 DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
134 if (mode_is_float(mode))
135 memcpy(req, &(ia32_default_req_ia32_fp.req), sizeof(*req));
136 else if (mode_is_int(mode) || mode_is_reference(mode))
137 memcpy(req, &(ia32_default_req_ia32_gp.req), sizeof(*req));
138 else if (mode == mode_T || mode == mode_M) {
139 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
143 assert(0 && "unsupported Phi-Mode");
146 DB((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
154 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
156 const ia32_irn_ops_t *ops = self;
158 DBG((ops->cg->mod, LEVEL_1, "ia32 assigned register %s to node %+F\n", reg->name, irn));
161 pos = ia32_translate_proj_pos(irn);
162 irn = my_skip_proj(irn);
165 if (is_ia32_irn(irn)) {
166 const arch_register_t **slots;
168 slots = get_ia32_slots(irn);
172 ia32_set_firm_reg(irn, reg, cur_reg_set);
176 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
178 const arch_register_t *reg = NULL;
181 pos = ia32_translate_proj_pos(irn);
182 irn = my_skip_proj(irn);
185 if (is_ia32_irn(irn)) {
186 const arch_register_t **slots;
187 slots = get_ia32_slots(irn);
191 reg = ia32_get_firm_reg(irn, cur_reg_set);
197 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
198 irn = my_skip_proj(irn);
200 return arch_irn_class_branch;
201 else if (is_ia32_irn(irn))
202 return arch_irn_class_normal;
207 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
208 irn = my_skip_proj(irn);
209 if (is_ia32_irn(irn))
210 return get_ia32_flags(irn);
216 static entity *ia32_get_frame_entity(const void *self, const ir_node *irn) {
217 return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
220 static void ia32_set_stack_bias(const void *self, ir_node *irn, int bias) {
222 const ia32_irn_ops_t *ops = self;
224 if (is_ia32_use_frame(irn) && bias != 0) {
225 ia32_am_flavour_t am_flav = get_ia32_am_flavour(irn);
227 DBG((ops->cg->mod, LEVEL_1, "stack biased %+F with %d\n", irn, bias));
228 snprintf(buf, sizeof(buf), "%d", bias);
229 add_ia32_am_offs(irn, buf);
231 set_ia32_am_flavour(irn, am_flav);
235 /* fill register allocator interface */
237 static const arch_irn_ops_if_t ia32_irn_ops_if = {
238 ia32_get_irn_reg_req,
243 ia32_get_frame_entity,
247 ia32_irn_ops_t ia32_irn_ops = {
254 /**************************************************
257 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
258 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
259 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
260 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
263 **************************************************/
266 * Transforms the standard firm graph into
269 static void ia32_prepare_graph(void *self) {
270 ia32_code_gen_t *cg = self;
271 firm_dbg_module_t *old_mod = cg->mod;
273 cg->mod = firm_dbg_register("firm.be.ia32.transform");
274 irg_walk_blkwise_graph(cg->irg, ia32_place_consts_set_modes, ia32_transform_node, cg);
275 dump_ir_block_graph_sched(cg->irg, "-transformed");
280 irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
281 dump_ir_block_graph_sched(cg->irg, "-am");
287 * Insert copies for all ia32 nodes where the should_be_same requirement
289 * Transform Sub into Neg -- Add if IN2 == OUT
291 static void ia32_finish_irg_walker(ir_node *irn, void *env) {
292 ia32_code_gen_t *cg = env;
293 const ia32_register_req_t **reqs;
294 const arch_register_t *out_reg, *in_reg;
296 ir_node *copy, *in_node, *block;
298 if (! is_ia32_irn(irn))
301 /* nodes with destination address mode don't produce values */
302 if (get_ia32_op_type(irn) == ia32_AddrModeD)
305 reqs = get_ia32_out_req_all(irn);
306 n_res = get_ia32_n_res(irn);
307 block = get_nodes_block(irn);
309 /* check all OUT requirements, if there is a should_be_same */
310 for (i = 0; i < n_res; i++) {
311 if (arch_register_req_is(&(reqs[i]->req), should_be_same)) {
312 /* get in and out register */
313 out_reg = get_ia32_out_reg(irn, i);
314 in_node = get_irn_n(irn, reqs[i]->same_pos);
315 in_reg = arch_get_irn_register(cg->arch_env, in_node);
317 /* check if in and out register are equal */
318 if (arch_register_get_index(out_reg) != arch_register_get_index(in_reg)) {
319 DBG((cg->mod, LEVEL_1, "inserting copy for %+F in_pos %d\n", irn, reqs[i]->same_pos));
321 /* create copy from in register */
322 copy = be_new_Copy(arch_register_get_class(in_reg), cg->irg, block, in_node);
324 /* destination is the out register */
325 arch_set_irn_register(cg->arch_env, copy, out_reg);
327 /* insert copy before the node into the schedule */
328 sched_add_before(irn, copy);
331 set_irn_n(irn, reqs[i]->same_pos, copy);
336 /* check if there is a sub which need to be transformed */
337 ia32_transform_sub_to_neg_add(irn, cg);
341 * Add Copy nodes for not fulfilled should_be_equal constraints
343 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
344 irg_walk_blkwise_graph(irg, NULL, ia32_finish_irg_walker, cg);
350 * Dummy functions for hooks we don't need but which must be filled.
352 static void ia32_before_sched(void *self) {
355 static void ia32_before_ra(void *self) {
361 * Transforms a be node into a Load.
363 static void transform_to_Load(ia32_transform_env_t *env) {
364 ir_node *irn = env->irn;
365 entity *ent = be_get_frame_entity(irn);
366 ir_mode *mode = env->mode;
367 ir_node *noreg = ia32_new_NoReg_gp(env->cg);
368 ir_node *nomem = new_rd_NoMem(env->irg);
369 ir_node *sched_point = NULL;
370 ir_node *ptr = get_irn_n(irn, 0);
371 ir_node *mem = be_is_Reload(irn) ? get_irn_n(irn, 1) : nomem;
372 ir_node *new_op, *proj;
373 const arch_register_t *reg;
375 if (sched_is_scheduled(irn)) {
376 sched_point = sched_prev(irn);
379 if (mode_is_float(mode)) {
380 new_op = new_rd_ia32_fLoad(env->dbg, env->irg, env->block, ptr, noreg, mem, mode_T);
383 new_op = new_rd_ia32_Load(env->dbg, env->irg, env->block, ptr, noreg, mem, mode_T);
386 set_ia32_am_support(new_op, ia32_am_Source);
387 set_ia32_op_type(new_op, ia32_AddrModeS);
388 set_ia32_am_flavour(new_op, ia32_B);
389 set_ia32_ls_mode(new_op, mode);
390 set_ia32_frame_ent(new_op, ent);
391 set_ia32_use_frame(new_op);
393 proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, pn_Load_res);
396 sched_add_after(sched_point, new_op);
397 sched_add_after(new_op, proj);
402 /* copy the register from the old node to the new Load */
403 reg = arch_get_irn_register(env->cg->arch_env, irn);
404 arch_set_irn_register(env->cg->arch_env, new_op, reg);
411 * Transforms a be node into a Store.
413 static void transform_to_Store(ia32_transform_env_t *env) {
414 ir_node *irn = env->irn;
415 entity *ent = be_get_frame_entity(irn);
416 ir_mode *mode = env->mode;
417 ir_node *noreg = ia32_new_NoReg_gp(env->cg);
418 ir_node *nomem = new_rd_NoMem(env->irg);
419 ir_node *ptr = get_irn_n(irn, 0);
420 ir_node *val = get_irn_n(irn, 1);
421 ir_node *new_op, *proj;
422 ir_node *sched_point = NULL;
424 if (sched_is_scheduled(irn)) {
425 sched_point = sched_prev(irn);
428 if (mode_is_float(mode)) {
429 new_op = new_rd_ia32_fStore(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
432 new_op = new_rd_ia32_Store(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
435 set_ia32_am_support(new_op, ia32_am_Dest);
436 set_ia32_op_type(new_op, ia32_AddrModeD);
437 set_ia32_am_flavour(new_op, ia32_B);
438 set_ia32_ls_mode(new_op, get_irn_mode(val));
439 set_ia32_frame_ent(new_op, ent);
440 set_ia32_use_frame(new_op);
442 proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, 0);
445 sched_add_after(sched_point, new_op);
446 sched_add_after(new_op, proj);
456 * Calls the transform functions for StackParam, Spill and Reload.
458 static void ia32_after_ra_walker(ir_node *node, void *env) {
459 ia32_code_gen_t *cg = env;
460 ia32_transform_env_t tenv;
465 tenv.block = get_nodes_block(node);
466 tenv.dbg = get_irn_dbg_info(node);
467 tenv.irg = current_ir_graph;
470 tenv.mode = get_irn_mode(node);
473 if (be_is_StackParam(node) || be_is_Reload(node)) {
474 transform_to_Load(&tenv);
476 else if (be_is_Spill(node)) {
477 transform_to_Store(&tenv);
482 * We transform StackParam, Spill and Reload here. This needs to be done before
483 * stack biasing otherwise we would miss the corrected offset for these nodes.
485 static void ia32_after_ra(void *self) {
486 ia32_code_gen_t *cg = self;
487 irg_walk_blkwise_graph(cg->irg, NULL, ia32_after_ra_walker, self);
492 * Emits the code, closes the output file and frees
493 * the code generator interface.
495 static void ia32_codegen(void *self) {
496 ia32_code_gen_t *cg = self;
497 ir_graph *irg = cg->irg;
500 if (cg->emit_decls) {
501 ia32_gen_decls(cg->out);
505 ia32_finish_irg(irg, cg);
506 dump_ir_block_graph_sched(irg, "-finished");
507 ia32_gen_routine(out, irg, cg);
511 pmap_destroy(cg->tv_ent);
512 pmap_destroy(cg->types);
514 /* de-allocate code generator */
515 del_set(cg->reg_set);
519 static void *ia32_cg_init(FILE *F, const be_irg_t *birg);
521 static const arch_code_generator_if_t ia32_code_gen_if = {
523 NULL, /* before abi introduce hook */
525 ia32_before_sched, /* before scheduling hook */
526 ia32_before_ra, /* before register allocation hook */
527 ia32_after_ra, /* after register allocation hook */
528 ia32_codegen /* emit && done */
532 * Initializes the code generator.
534 static void *ia32_cg_init(FILE *F, const be_irg_t *birg) {
535 ia32_isa_t *isa = (ia32_isa_t *)birg->main_env->arch_env->isa;
536 ia32_code_gen_t *cg = xcalloc(1, sizeof(*cg));
538 cg->impl = &ia32_code_gen_if;
540 cg->reg_set = new_set(ia32_cmp_irn_reg_assoc, 1024);
541 cg->mod = firm_dbg_register("firm.be.ia32.cg");
543 cg->arch_env = birg->main_env->arch_env;
544 cg->types = pmap_create();
545 cg->tv_ent = pmap_create();
548 /* set optimizations */
551 cg->opt.placecnst = 1;
555 if (isa->name_obst_size) {
556 //printf("freed %d bytes from name obst\n", isa->name_obst_size);
557 isa->name_obst_size = 0;
558 obstack_free(isa->name_obst, NULL);
559 obstack_init(isa->name_obst);
565 if (isa->num_codegens > 1)
570 cur_reg_set = cg->reg_set;
572 ia32_irn_ops.cg = cg;
574 return (arch_code_generator_t *)cg;
579 /*****************************************************************
580 * ____ _ _ _____ _____
581 * | _ \ | | | | |_ _|/ ____| /\
582 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
583 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
584 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
585 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
587 *****************************************************************/
589 static ia32_isa_t ia32_isa_template = {
590 &ia32_isa_if, /* isa interface implementation */
591 &ia32_gp_regs[REG_ESP], /* stack pointer register */
592 &ia32_gp_regs[REG_EBP], /* base pointer register */
593 -1, /* stack direction */
594 0, /* number of code generator objects so far */
595 NULL /* name obstack */
599 * Initializes the backend ISA.
601 static void *ia32_init(void) {
602 static int inited = 0;
608 isa = xcalloc(1, sizeof(*isa));
609 memcpy(isa, &ia32_isa_template, sizeof(*isa));
611 ia32_register_init(isa);
612 ia32_create_opcodes();
615 isa->name_obst = xcalloc(1, sizeof(*(isa->name_obst)));
616 obstack_init(isa->name_obst);
617 isa->name_obst_size = 0;
628 * Closes the output file and frees the ISA structure.
630 static void ia32_done(void *self) {
631 ia32_isa_t *isa = self;
634 //printf("name obst size = %d bytes\n", isa->name_obst_size);
635 obstack_free(isa->name_obst, NULL);
643 static int ia32_get_n_reg_class(const void *self) {
647 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
648 assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
649 return &ia32_reg_classes[i];
653 * Get the register class which shall be used to store a value of a given mode.
654 * @param self The this pointer.
655 * @param mode The mode in question.
656 * @return A register class which can hold values of the given mode.
658 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
659 if (mode_is_float(mode))
660 return &ia32_reg_classes[CLASS_ia32_fp];
662 return &ia32_reg_classes[CLASS_ia32_gp];
666 * Produces the type which sits between the stack args and the locals on the stack.
667 * it will contain the return address and space to store the old base pointer.
668 * @return The Firm type modeling the ABI between type.
670 static ir_type *get_between_type(void)
672 static ir_type *between_type = NULL;
673 static entity *old_bp_ent = NULL;
676 entity *ret_addr_ent;
677 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
678 ir_type *old_bp_type = new_type_primitive(new_id_from_str("bp"), mode_P);
680 between_type = new_type_class(new_id_from_str("ia32_between_type"));
681 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
682 ret_addr_ent = new_entity(between_type, new_id_from_str("ret_addr"), ret_addr_type);
684 set_entity_offset_bytes(old_bp_ent, 0);
685 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
686 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
693 * Get the ABI restrictions for procedure calls.
694 * @param self The this pointer.
695 * @param method_type The type of the method (procedure) in question.
696 * @param abi The abi object to be modified
698 void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
699 ir_type *between_type;
702 unsigned cc = get_method_calling_convention(method_type);
703 int n = get_method_n_params(method_type);
708 const arch_register_t *reg;
709 be_abi_call_flags_t call_flags;
711 /* set abi flags for calls */
712 call_flags.bits.left_to_right = 0;
713 call_flags.bits.store_args_sequential = 0;
714 call_flags.bits.try_omit_fp = 1;
715 call_flags.bits.fp_free = 0;
716 call_flags.bits.call_has_imm = 1;
718 /* get the between type and the frame pointer save entity */
719 between_type = get_between_type();
721 /* set stack parameter passing style */
722 be_abi_call_set_flags(abi, call_flags, between_type);
724 /* collect the mode for each type */
725 modes = alloca(n * sizeof(modes[0]));
727 for (i = 0; i < n; i++) {
728 tp = get_method_param_type(method_type, i);
729 modes[i] = get_type_mode(tp);
732 /* set register parameters */
733 if (cc & cc_reg_param) {
734 /* determine the number of parameters passed via registers */
735 biggest_n = ia32_get_n_regparam_class(n, modes, &ignore, &ignore);
737 /* loop over all parameters and set the register requirements */
738 for (i = 0; i <= biggest_n; i++) {
739 reg = ia32_get_RegParam_reg(n, modes, i, cc);
740 assert(reg && "kaputt");
741 be_abi_call_param_reg(abi, i, reg);
748 /* set stack parameters */
749 for (i = stack_idx; i < n; i++) {
750 be_abi_call_param_stack(abi, i);
754 /* set return registers */
755 n = get_method_n_ress(method_type);
757 assert(n <= 2 && "more than two results not supported");
759 /* In case of 64bit returns, we will have two 32bit values */
761 tp = get_method_res_type(method_type, 0);
762 mode = get_type_mode(tp);
764 assert(!mode_is_float(mode) && "two FP results not supported");
766 tp = get_method_res_type(method_type, 1);
767 mode = get_type_mode(tp);
769 assert(!mode_is_float(mode) && "two FP results not supported");
771 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
772 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
775 tp = get_method_res_type(method_type, 0);
776 assert(is_atomic_type(tp));
777 mode = get_type_mode(tp);
779 be_abi_call_res_reg(abi, 0, mode_is_float(mode) ? &ia32_fp_regs[REG_XMM0] : &ia32_gp_regs[REG_EAX]);
784 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
785 return &ia32_irn_ops;
788 const arch_irn_handler_t ia32_irn_handler = {
792 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
793 return &ia32_irn_handler;
796 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
797 return is_ia32_irn(irn);
801 * Initializes the code generator interface.
803 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
804 return &ia32_code_gen_if;
807 list_sched_selector_t ia32_sched_selector;
810 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
812 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
813 memcpy(&ia32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
814 ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
815 return &ia32_sched_selector;
819 static void ia32_register_options(lc_opt_entry_t *ent)
822 #endif /* WITH_LIBCORE */
824 const arch_isa_if_t ia32_isa_if = {
826 ia32_register_options,
830 ia32_get_n_reg_class,
832 ia32_get_reg_class_for_mode,
834 ia32_get_irn_handler,
835 ia32_get_code_generator_if,
836 ia32_get_list_sched_selector