11 #include "pseudo_irg.h"
15 #include "iredges_t.h"
22 #include "../beabi.h" /* the general register allocator interface */
23 #include "../benode_t.h"
24 #include "../belower.h"
25 #include "../besched_t.h"
27 #include "bearch_ia32_t.h"
29 #include "ia32_new_nodes.h" /* ia32 nodes interface */
30 #include "gen_ia32_regalloc_if.h" /* the generated interface (register type and class defenitions) */
31 #include "ia32_gen_decls.h" /* interface declaration emitter */
32 #include "ia32_transform.h"
33 #include "ia32_emitter.h"
34 #include "ia32_map_regs.h"
35 #include "ia32_optimize.h"
37 #define DEBUG_MODULE "firm.be.ia32.isa"
40 static set *cur_reg_set = NULL;
43 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
45 ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
46 return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_gp_regs[REG_XXX]);
49 ir_node *ia32_new_NoReg_fp(ia32_code_gen_t *cg) {
50 return be_abi_get_callee_save_irn(cg->birg->abi, &ia32_fp_regs[REG_XXXX]);
53 /**************************************************
56 * _ __ ___ __ _ __ _| | | ___ ___ _| |_
57 * | '__/ _ \/ _` | / _` | | |/ _ \ / __| | | _|
58 * | | | __/ (_| | | (_| | | | (_) | (__ | | |
59 * |_| \___|\__, | \__,_|_|_|\___/ \___| |_|_|
62 **************************************************/
64 static ir_node *my_skip_proj(const ir_node *n) {
71 * Return register requirements for an ia32 node.
72 * If the node returns a tuple (mode_T) then the proj's
73 * will be asked for this information.
75 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) {
76 const ia32_register_req_t *irn_req;
77 long node_pos = pos == -1 ? 0 : pos;
78 ir_mode *mode = get_irn_mode(irn);
79 firm_dbg_module_t *mod = firm_dbg_register(DEBUG_MODULE);
80 const ia32_irn_ops_t *ops = self;
82 if (mode == mode_T || mode == mode_M) {
83 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
87 DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
92 node_pos = ia32_translate_proj_pos(irn);
98 irn = my_skip_proj(irn);
100 DB((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
103 if (is_ia32_irn(irn)) {
105 irn_req = get_ia32_in_req(irn, pos);
108 irn_req = get_ia32_out_req(irn, node_pos);
111 DB((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
113 memcpy(req, &(irn_req->req), sizeof(*req));
115 if (arch_register_req_is(&(irn_req->req), should_be_same)) {
116 assert(irn_req->same_pos >= 0 && "should be same constraint for in -> out NYI");
117 req->other_same = get_irn_n(irn, irn_req->same_pos);
120 if (arch_register_req_is(&(irn_req->req), should_be_different)) {
121 assert(irn_req->different_pos >= 0 && "should be different constraint for in -> out NYI");
122 req->other_different = get_irn_n(irn, irn_req->different_pos);
126 /* treat Phi like Const with default requirements */
128 DB((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
129 if (mode_is_float(mode))
130 memcpy(req, &(ia32_default_req_ia32_fp.req), sizeof(*req));
131 else if (mode_is_int(mode) || mode_is_reference(mode))
132 memcpy(req, &(ia32_default_req_ia32_gp.req), sizeof(*req));
133 else if (mode == mode_T || mode == mode_M) {
134 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
138 assert(0 && "unsupported Phi-Mode");
141 DB((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
149 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
153 pos = ia32_translate_proj_pos(irn);
154 irn = my_skip_proj(irn);
157 if (is_ia32_irn(irn)) {
158 const arch_register_t **slots;
160 slots = get_ia32_slots(irn);
164 ia32_set_firm_reg(irn, reg, cur_reg_set);
168 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
170 const arch_register_t *reg = NULL;
173 pos = ia32_translate_proj_pos(irn);
174 irn = my_skip_proj(irn);
177 if (is_ia32_irn(irn)) {
178 const arch_register_t **slots;
179 slots = get_ia32_slots(irn);
183 reg = ia32_get_firm_reg(irn, cur_reg_set);
189 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
190 irn = my_skip_proj(irn);
192 return arch_irn_class_branch;
193 else if (is_ia32_irn(irn))
194 return arch_irn_class_normal;
199 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
200 irn = my_skip_proj(irn);
201 if (is_ia32_irn(irn))
202 return get_ia32_flags(irn);
208 static entity *ia32_get_frame_entity(const void *self, const ir_node *irn) {
209 return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
212 static void ia32_set_stack_bias(const void *self, ir_node *irn, int bias) {
214 const ia32_irn_ops_t *ops = self;
216 if (is_ia32_use_frame(irn) && bias != 0) {
217 ia32_am_flavour_t am_flav = get_ia32_am_flavour(irn);
219 DBG((ops->cg->mod, LEVEL_1, "stack biased %+F with %d\n", irn, bias));
220 snprintf(buf, sizeof(buf), "%d", bias);
221 add_ia32_am_offs(irn, buf);
223 set_ia32_am_flavour(irn, am_flav);
227 /* fill register allocator interface */
229 static const arch_irn_ops_if_t ia32_irn_ops_if = {
230 ia32_get_irn_reg_req,
235 ia32_get_frame_entity,
239 ia32_irn_ops_t ia32_irn_ops = {
246 /**************************************************
249 * ___ ___ __| | ___ __ _ ___ _ __ _| |_
250 * / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \ | | _|
251 * | (_| (_) | (_| | __/ (_| | __/ | | | | | |
252 * \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
255 **************************************************/
258 * Transforms the standard firm graph into
261 static void ia32_prepare_graph(void *self) {
262 ia32_code_gen_t *cg = self;
264 irg_walk_blkwise_graph(cg->irg, ia32_place_consts, ia32_transform_node, cg);
265 dump_ir_block_graph_sched(cg->irg, "-transformed");
266 edges_deactivate(cg->irg);
267 edges_activate(cg->irg);
270 irg_walk_blkwise_graph(cg->irg, NULL, ia32_optimize_am, cg);
271 dump_ir_block_graph_sched(cg->irg, "-am");
277 * Insert copies for all ia32 nodes where the should_be_same requirement
280 static void ia32_finish_irg_walker(ir_node *irn, void *env) {
281 ia32_code_gen_t *cg = env;
282 const ia32_register_req_t **reqs;
283 const arch_register_t *out_reg, *in_reg;
285 ir_node *copy, *in_node, *block;
287 if (! is_ia32_irn(irn))
290 /* nodes with destination address mode don't produce values */
291 if (get_ia32_op_type(irn) == ia32_AddrModeD)
294 reqs = get_ia32_out_req_all(irn);
295 n_res = get_ia32_n_res(irn);
296 block = get_nodes_block(irn);
298 /* check all OUT requirements, if there is a should_be_same */
299 for (i = 0; i < n_res; i++) {
300 if (arch_register_req_is(&(reqs[i]->req), should_be_same)) {
301 /* get in and out register */
302 out_reg = get_ia32_out_reg(irn, i);
303 in_node = get_irn_n(irn, reqs[i]->same_pos);
304 in_reg = arch_get_irn_register(cg->arch_env, in_node);
306 /* check if in and out register are equal */
307 if (arch_register_get_index(out_reg) != arch_register_get_index(in_reg)) {
308 DBG((cg->mod, LEVEL_1, "inserting copy for %+F in_pos %d\n", irn, reqs[i]->same_pos));
310 /* create copy from in register */
311 copy = be_new_Copy(arch_register_get_class(in_reg), cg->irg, block, in_node);
313 /* destination is the out register */
314 arch_set_irn_register(cg->arch_env, copy, out_reg);
316 /* insert copy before the node into the schedule */
317 sched_add_before(irn, copy);
320 set_irn_n(irn, reqs[i]->same_pos, copy);
327 * Add Copy nodes for not fulfilled should_be_equal constraints
329 static void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg) {
330 irg_walk_blkwise_graph(irg, NULL, ia32_finish_irg_walker, cg);
336 * Dummy functions for hooks we don't need but which must be filled.
338 static void ia32_before_sched(void *self) {
341 static void ia32_before_ra(void *self) {
347 * Transforms a be node into a Load.
349 static void transform_to_Load(ia32_transform_env_t *env) {
350 ir_node *irn = env->irn;
351 entity *ent = be_get_frame_entity(irn);
352 ir_mode *mode = env->mode;
353 ir_node *noreg = ia32_new_NoReg_gp(env->cg);
354 ir_node *nomem = new_rd_NoMem(env->irg);
355 ir_node *sched_point = NULL;
356 ir_node *ptr = get_irn_n(irn, 0);
357 ir_node *mem = be_is_Reload(irn) ? get_irn_n(irn, 1) : nomem;
358 ir_node *new_op, *proj;
359 const arch_register_t *reg;
361 if (sched_is_scheduled(irn)) {
362 sched_point = sched_prev(irn);
365 if (mode_is_float(mode)) {
366 new_op = new_rd_ia32_fLoad(env->dbg, env->irg, env->block, ptr, noreg, mem, mode_T);
369 new_op = new_rd_ia32_Load(env->dbg, env->irg, env->block, ptr, noreg, mem, mode_T);
372 set_ia32_am_support(new_op, ia32_am_Source);
373 set_ia32_op_type(new_op, ia32_AddrModeS);
374 set_ia32_am_flavour(new_op, ia32_B);
375 set_ia32_ls_mode(new_op, mode);
376 set_ia32_frame_ent(new_op, ent);
377 set_ia32_use_frame(new_op);
379 proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, pn_Load_res);
382 sched_add_after(sched_point, new_op);
383 sched_add_after(new_op, proj);
388 /* copy the register from the old node to the new Load */
389 reg = arch_get_irn_register(env->cg->arch_env, irn);
390 arch_set_irn_register(env->cg->arch_env, new_op, reg);
397 * Transforms a be node into a Store.
399 static void transform_to_Store(ia32_transform_env_t *env) {
400 ir_node *irn = env->irn;
401 entity *ent = be_get_frame_entity(irn);
402 ir_mode *mode = env->mode;
403 ir_node *noreg = ia32_new_NoReg_gp(env->cg);
404 ir_node *nomem = new_rd_NoMem(env->irg);
405 ir_node *ptr = get_irn_n(irn, 0);
406 ir_node *val = get_irn_n(irn, 1);
407 ir_node *new_op, *proj;
408 ir_node *sched_point = NULL;
410 if (sched_is_scheduled(irn)) {
411 sched_point = sched_prev(irn);
414 if (mode_is_float(mode)) {
415 new_op = new_rd_ia32_fStore(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
418 new_op = new_rd_ia32_Store(env->dbg, env->irg, env->block, ptr, noreg, val, nomem, mode_T);
421 set_ia32_am_support(new_op, ia32_am_Dest);
422 set_ia32_op_type(new_op, ia32_AddrModeD);
423 set_ia32_am_flavour(new_op, ia32_B);
424 set_ia32_ls_mode(new_op, get_irn_mode(val));
425 set_ia32_frame_ent(new_op, ent);
426 set_ia32_use_frame(new_op);
428 proj = new_rd_Proj(env->dbg, env->irg, env->block, new_op, mode, 0);
431 sched_add_after(sched_point, new_op);
432 sched_add_after(new_op, proj);
442 * Calls the transform functions for StackParam, Spill and Reload.
444 static void ia32_after_ra_walker(ir_node *node, void *env) {
445 ia32_code_gen_t *cg = env;
446 ir_node *new_node = NULL;
447 ia32_transform_env_t tenv;
452 tenv.block = get_nodes_block(node);
453 tenv.dbg = get_irn_dbg_info(node);
454 tenv.irg = current_ir_graph;
457 tenv.mode = get_irn_mode(node);
460 if (be_is_StackParam(node) || be_is_Reload(node)) {
461 transform_to_Load(&tenv);
463 else if (be_is_Spill(node)) {
464 transform_to_Store(&tenv);
469 * We transform StackParam, Spill and Reload here. This needs to be done before
470 * stack biasing otherwise we would miss the corrected offset for these nodes.
472 static void ia32_after_ra(void *self) {
473 ia32_code_gen_t *cg = self;
474 irg_walk_blkwise_graph(cg->irg, NULL, ia32_after_ra_walker, self);
479 * Emits the code, closes the output file and frees
480 * the code generator interface.
482 static void ia32_codegen(void *self) {
483 ia32_code_gen_t *cg = self;
484 ir_graph *irg = cg->irg;
487 if (cg->emit_decls) {
488 ia32_gen_decls(cg->out);
492 ia32_finish_irg(irg, cg);
493 dump_ir_block_graph_sched(irg, "-finished");
494 ia32_gen_routine(out, irg, cg);
498 pmap_destroy(cg->tv_ent);
499 pmap_destroy(cg->types);
501 /* de-allocate code generator */
502 del_set(cg->reg_set);
506 static void *ia32_cg_init(FILE *F, const be_irg_t *birg);
508 static const arch_code_generator_if_t ia32_code_gen_if = {
511 ia32_before_sched, /* before scheduling hook */
512 ia32_before_ra, /* before register allocation hook */
513 ia32_after_ra, /* after register allocation hook */
514 ia32_codegen /* emit && done */
518 * Initializes the code generator.
520 static void *ia32_cg_init(FILE *F, const be_irg_t *birg) {
521 ia32_isa_t *isa = (ia32_isa_t *)birg->main_env->arch_env->isa;
522 ia32_code_gen_t *cg = xcalloc(1, sizeof(*cg));
524 cg->impl = &ia32_code_gen_if;
526 cg->reg_set = new_set(ia32_cmp_irn_reg_assoc, 1024);
527 cg->mod = firm_dbg_register("firm.be.ia32.cg");
529 cg->arch_env = birg->main_env->arch_env;
530 cg->types = pmap_create();
531 cg->tv_ent = pmap_create();
534 /* set optimizations */
537 cg->opt.placecnst = 1;
542 if (isa->num_codegens > 1)
547 cur_reg_set = cg->reg_set;
549 ia32_irn_ops.cg = cg;
551 return (arch_code_generator_t *)cg;
556 /*****************************************************************
557 * ____ _ _ _____ _____
558 * | _ \ | | | | |_ _|/ ____| /\
559 * | |_) | __ _ ___| | _____ _ __ __| | | | | (___ / \
560 * | _ < / _` |/ __| |/ / _ \ '_ \ / _` | | | \___ \ / /\ \
561 * | |_) | (_| | (__| < __/ | | | (_| | _| |_ ____) / ____ \
562 * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/ \_\
564 *****************************************************************/
566 static ia32_isa_t ia32_isa_template = {
568 &ia32_gp_regs[REG_ESP],
569 &ia32_gp_regs[REG_EBP],
575 * Initializes the backend ISA.
577 static void *ia32_init(void) {
578 static int inited = 0;
584 isa = xcalloc(1, sizeof(*isa));
585 memcpy(isa, &ia32_isa_template, sizeof(*isa));
587 ia32_register_init(isa);
588 ia32_create_opcodes();
598 * Closes the output file and frees the ISA structure.
600 static void ia32_done(void *self) {
606 static int ia32_get_n_reg_class(const void *self) {
610 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
611 assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
612 return &ia32_reg_classes[i];
616 * Get the register class which shall be used to store a value of a given mode.
617 * @param self The this pointer.
618 * @param mode The mode in question.
619 * @return A register class which can hold values of the given mode.
621 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
622 if (mode_is_float(mode))
623 return &ia32_reg_classes[CLASS_ia32_fp];
625 return &ia32_reg_classes[CLASS_ia32_gp];
629 * Produces the type which sits between the stack args and the locals on the stack.
630 * it will contain the return address and space to store the old base pointer.
631 * @return The Firm type modelling the ABI between type.
633 static ir_type *get_between_type(void)
635 static ir_type *between_type = NULL;
636 static entity *old_bp_ent = NULL;
639 entity *ret_addr_ent;
640 ir_type *ret_addr_type = new_type_primitive(new_id_from_str("return_addr"), mode_P);
641 ir_type *old_bp_type = new_type_primitive(new_id_from_str("bp"), mode_P);
643 between_type = new_type_class(new_id_from_str("ia32_between_type"));
644 old_bp_ent = new_entity(between_type, new_id_from_str("old_bp"), old_bp_type);
645 ret_addr_ent = new_entity(between_type, new_id_from_str("ret_addr"), ret_addr_type);
647 set_entity_offset_bytes(old_bp_ent, 0);
648 set_entity_offset_bytes(ret_addr_ent, get_type_size_bytes(old_bp_type));
649 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
656 * Get the ABI restrictions for procedure calls.
657 * @param self The this pointer.
658 * @param method_type The type of the method (procedure) in question.
659 * @param abi The abi object to be modified
661 void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
662 ir_type *between_type;
665 unsigned cc = get_method_calling_convention(method_type);
666 int n = get_method_n_params(method_type);
671 const arch_register_t *reg;
672 be_abi_call_flags_t call_flags = { 0, 0, 1, 0, 1 };
674 /* get the between type and the frame pointer save entity */
675 between_type = get_between_type();
677 /* set stack parameter passing style */
678 be_abi_call_set_flags(abi, call_flags, between_type);
680 /* collect the mode for each type */
681 modes = alloca(n * sizeof(modes[0]));
683 for (i = 0; i < n; i++) {
684 tp = get_method_param_type(method_type, i);
685 modes[i] = get_type_mode(tp);
688 /* set register parameters */
689 if (cc & cc_reg_param) {
690 /* determine the number of parameters passed via registers */
691 biggest_n = ia32_get_n_regparam_class(n, modes, &ignore, &ignore);
693 /* loop over all parameters and set the register requirements */
694 for (i = 0; i <= biggest_n; i++) {
695 reg = ia32_get_RegParam_reg(n, modes, i, cc);
696 assert(reg && "kaputt");
697 be_abi_call_param_reg(abi, i, reg);
704 /* set stack parameters */
705 for (i = stack_idx; i < n; i++) {
706 be_abi_call_param_stack(abi, i);
710 /* set return registers */
711 n = get_method_n_ress(method_type);
713 assert(n <= 2 && "more than two results not supported");
715 /* In case of 64bit returns, we will have two 32bit values */
717 tp = get_method_res_type(method_type, 0);
718 mode = get_type_mode(tp);
720 assert(!mode_is_float(mode) && "two FP results not supported");
722 tp = get_method_res_type(method_type, 1);
723 mode = get_type_mode(tp);
725 assert(!mode_is_float(mode) && "two FP results not supported");
727 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
728 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
731 tp = get_method_res_type(method_type, 0);
732 assert(is_atomic_type(tp));
733 mode = get_type_mode(tp);
735 be_abi_call_res_reg(abi, 0, mode_is_float(mode) ? &ia32_fp_regs[REG_XMM0] : &ia32_gp_regs[REG_EAX]);
740 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
741 return &ia32_irn_ops;
744 const arch_irn_handler_t ia32_irn_handler = {
748 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
749 return &ia32_irn_handler;
752 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
753 return is_ia32_irn(irn);
757 * Initializes the code generator interface.
759 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
760 return &ia32_code_gen_if;
763 list_sched_selector_t ia32_sched_selector;
766 * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
768 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
769 memcpy(&ia32_sched_selector, trivial_selector, sizeof(list_sched_selector_t));
770 ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
771 return &ia32_sched_selector;
775 static void ia32_register_options(lc_opt_entry_t *ent)
778 #endif /* WITH_LIBCORE */
780 const arch_isa_if_t ia32_isa_if = {
782 ia32_register_options,
786 ia32_get_n_reg_class,
788 ia32_get_reg_class_for_mode,
790 ia32_get_irn_handler,
791 ia32_get_code_generator_if,
792 ia32_get_list_sched_selector