704e6d0b530568d7be22fe7209a9c673186b9cd2
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "pseudo_irg.h"
6 #include "irgwalk.h"
7 #include "irprog.h"
8 #include "irprintf.h"
9 #include "iredges_t.h"
10 #include "ircons.h"
11
12 #include "bitset.h"
13 #include "debug.h"
14
15 #include "../bearch.h"                /* the general register allocator interface */
16 #include "../benode_t.h"
17 #include "../belower.h"
18 #include "bearch_ia32_t.h"
19
20 #include "ia32_new_nodes.h"           /* ia32 nodes interface */
21 #include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
22 #include "ia32_gen_decls.h"           /* interface declaration emitter */
23 #include "ia32_transform.h"
24 #include "ia32_emitter.h"
25 #include "ia32_map_regs.h"
26 #include "ia32_optimize.h"
27
28 #define DEBUG_MODULE "ir.be.isa.ia32"
29
30 /* TODO: ugly */
31 static set *cur_reg_set = NULL;
32
33 #undef is_Start
34 #define is_Start(irn) (get_irn_opcode(irn) == iro_Start)
35
36 /**************************************************
37  *                         _ _              _  __
38  *                        | | |            (_)/ _|
39  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
40  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
41  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
42  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
43  *            __/ |
44  *           |___/
45  **************************************************/
46
47 static ir_node *my_skip_proj(const ir_node *n) {
48         while (is_Proj(n))
49                 n = get_Proj_pred(n);
50         return (ir_node *)n;
51 }
52
53 static int is_Call_Proj(const ir_node *n) {
54         if (is_Proj(n)                               &&
55                 is_Proj(get_Proj_pred(n))                &&
56                 get_irn_mode(get_Proj_pred(n)) == mode_T &&
57                 is_ia32_Call(get_Proj_pred(get_Proj_pred(n))))
58         {
59                 return 1;
60         }
61
62         return 0;
63 }
64
65 static int is_Start_Proj(const ir_node *n) {
66         if (is_Proj(n)                               &&
67                 is_Proj(get_Proj_pred(n))                &&
68                 get_irn_mode(get_Proj_pred(n)) == mode_T &&
69                 is_Start(get_Proj_pred(get_Proj_pred(n))))
70         {
71                 return 1;
72         }
73
74         return 0;
75 }
76
77 static int is_P_frame_base_Proj(const ir_node *n) {
78         if (is_Proj(n)                                    &&
79                 is_Start(get_Proj_pred(n)) &&
80                 get_Proj_proj(n) == pn_Start_P_frame_base)
81         {
82                 return 1;
83         }
84
85         return 0;
86 }
87
88 static int is_used_by_Keep(const ir_node *n) {
89         return be_is_Keep(get_edge_src_irn(get_irn_out_edge_first(n)));
90 }
91
92 /**
93  * Return register requirements for an ia32 node.
94  * If the node returns a tuple (mode_T) then the proj's
95  * will be asked for this information.
96  */
97 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) {
98         const ia32_register_req_t *irn_req;
99         long                       node_pos = pos == -1 ? 0 : pos;
100         ir_mode                   *mode     = get_irn_mode(irn);
101         firm_dbg_module_t         *mod      = firm_dbg_register(DEBUG_MODULE);
102         const ia32_irn_ops_t      *ops      = self;
103
104         if (mode == mode_T || mode == mode_M) {
105                 DBG((mod, LEVEL_1, "ignoring mode_T, mode_M node %+F\n", irn));
106                 return NULL;
107         }
108
109         DBG((mod, LEVEL_1, "get requirements at pos %d for %+F ... ", pos, irn));
110
111
112         if (is_Call_Proj(irn) && is_used_by_Keep(irn)) {
113                 if (pos >= 0) {
114                         req = NULL;
115                 }
116                 else {
117                         irn_req = ia32_projnum_reg_req_map[get_Proj_proj(irn)];
118                         memcpy(req, &(irn_req->req), sizeof(*req));
119                 }
120
121                 return req;
122         }
123         else if (is_Start_Proj(irn)) {
124                 irn_req = ops->cg->reg_param_req[get_Proj_proj(irn)];
125                 assert(irn_req && "missing requirement for regparam");
126                 memcpy(req, &(irn_req->req), sizeof(*req));
127                 return req;
128         }
129         else if (is_Proj(irn)) {
130                 if (pos == -1) {
131                         node_pos = ia32_translate_proj_pos(irn);
132                 }
133                 else {
134                         node_pos = pos;
135                 }
136
137                 irn = my_skip_proj(irn);
138
139                 DBG((mod, LEVEL_1, "skipping Proj, going to %+F at pos %d ... ", irn, node_pos));
140         }
141
142         if (is_ia32_irn(irn)) {
143                 if (pos >= 0) {
144                         irn_req = get_ia32_in_req(irn, pos);
145                 }
146                 else {
147                         irn_req = get_ia32_out_req(irn, node_pos);
148                 }
149
150                 DBG((mod, LEVEL_1, "returning reqs for %+F at pos %d\n", irn, pos));
151
152                 memcpy(req, &(irn_req->req), sizeof(*req));
153
154                 if (arch_register_req_is(&(irn_req->req), should_be_same) ||
155                         arch_register_req_is(&(irn_req->req), should_be_different)) {
156                         assert(irn_req->pos >= 0 && "should be same/different constraint for in -> out NYI");
157                         req->other = get_irn_n(irn, irn_req->pos);
158                 }
159         }
160         else {
161                 /* treat Phi like Const with default requirements */
162                 if (is_Phi(irn)) {
163                         DBG((mod, LEVEL_1, "returning standard reqs for %+F\n", irn));
164                         if (mode_is_float(mode))
165                                 memcpy(req, &(ia32_default_req_ia32_floating_point.req), sizeof(*req));
166                         else if (mode_is_int(mode) || mode_is_reference(mode))
167                                 memcpy(req, &(ia32_default_req_ia32_general_purpose.req), sizeof(*req));
168                         else if (mode == mode_T || mode == mode_M) {
169                                 DBG((mod, LEVEL_1, "ignoring Phi node %+F\n", irn));
170                                 return NULL;
171                         }
172                         else
173                                 assert(0 && "unsupported Phi-Mode");
174                 }
175                 else if (is_Start(irn)) {
176                         DBG((mod, LEVEL_1, "returning reqs none for ProjX -> Start (%+F )\n", irn));
177                         switch (node_pos) {
178                                 case pn_Start_X_initial_exec:
179                                 case pn_Start_P_value_arg_base:
180                                 case pn_Start_P_globals:
181                                 case pn_Start_P_frame_base:
182                                         memcpy(req, &(ia32_default_req_none.req), sizeof(*req));
183                                         break;
184                                 case pn_Start_T_args:
185                                         assert(0 && "ProjT(pn_Start_T_args) should not be asked");
186                         }
187                 }
188                 else if (get_irn_op(irn) == op_Return && pos > 0) {
189                         DBG((mod, LEVEL_1, "returning reqs EAX for %+F\n", irn));
190                         memcpy(req, &(ia32_default_req_ia32_general_purpose_eax.req), sizeof(*req));
191                 }
192                 else {
193                         DBG((mod, LEVEL_1, "returning NULL for %+F (not ia32)\n", irn));
194                         req = NULL;
195                 }
196         }
197
198         return req;
199 }
200
201 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
202         int pos = 0;
203
204         if ((is_Call_Proj(irn) && is_used_by_Keep(irn)) ||
205                 is_P_frame_base_Proj(irn)                   ||
206                 is_Start_Proj(irn))
207         {
208                 /* don't skip the proj, we want to take the else below */
209         }
210         else if (is_Proj(irn)) {
211                 pos = ia32_translate_proj_pos(irn);
212                 irn = my_skip_proj(irn);
213         }
214
215         if (is_ia32_irn(irn)) {
216                 const arch_register_t **slots;
217
218                 slots      = get_ia32_slots(irn);
219                 slots[pos] = reg;
220         }
221         else {
222                 ia32_set_firm_reg(irn, reg, cur_reg_set);
223         }
224 }
225
226 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
227         int pos = 0;
228         const arch_register_t *reg = NULL;
229
230         if ((is_Call_Proj(irn) && is_used_by_Keep(irn)) ||
231                 is_P_frame_base_Proj(irn)                   ||
232                 is_Start_Proj(irn))
233         {
234                 /* don't skip the proj, we want to take the else below */
235         }
236         else if (is_Proj(irn)) {
237                 pos = ia32_translate_proj_pos(irn);
238                 irn = my_skip_proj(irn);
239         }
240
241         if (is_ia32_irn(irn)) {
242                 const arch_register_t **slots;
243                 slots = get_ia32_slots(irn);
244                 reg   = slots[pos];
245         }
246         else {
247                 reg = ia32_get_firm_reg(irn, cur_reg_set);
248         }
249
250         return reg;
251 }
252
253 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
254         irn = my_skip_proj(irn);
255         if (is_cfop(irn))
256                 return arch_irn_class_branch;
257         else if (is_ia32_Call(irn))
258                 return arch_irn_class_call;
259         else if (is_ia32_irn(irn))
260                 return arch_irn_class_normal;
261         else
262                 return 0;
263 }
264
265 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
266         irn = my_skip_proj(irn);
267         if (is_ia32_irn(irn))
268                 return get_ia32_flags(irn);
269         else {
270                 ir_printf("don't know flags of %+F\n", irn);
271                 return 0;
272         }
273 }
274
275 /* fill register allocator interface */
276
277 static const arch_irn_ops_if_t ia32_irn_ops_if = {
278         ia32_get_irn_reg_req,
279         ia32_set_irn_reg,
280         ia32_get_irn_reg,
281         ia32_classify,
282         ia32_get_flags
283 };
284
285 ia32_irn_ops_t ia32_irn_ops = {
286         &ia32_irn_ops_if,
287         NULL
288 };
289
290
291
292 /**************************************************
293  *                _                         _  __
294  *               | |                       (_)/ _|
295  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
296  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
297  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
298  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
299  *                        __/ |
300  *                       |___/
301  **************************************************/
302
303 static void check_for_alloca(ir_node *irn, void *env) {
304         int *has_alloca = env;
305
306         if (get_irn_opcode(irn) == iro_Alloc) {
307                 if (get_Alloc_where(irn) == stack_alloc) {
308                         *has_alloca = 1;
309                 }
310         }
311 }
312
313 /**
314  * Transforms the standard firm graph into
315  * an ia32 firm graph
316  */
317 static void ia32_prepare_graph(void *self) {
318         ia32_code_gen_t *cg = self;
319
320         if (! is_pseudo_ir_graph(cg->irg)) {
321                 /* If there is a alloca in the irg, we use %ebp for stack addressing */
322                 /* instead of %esp, as alloca destroys %esp.                         */
323
324                 cg->has_alloca = 0;
325
326                 /* check for alloca node */
327                 irg_walk_blkwise_graph(cg->irg, check_for_alloca, NULL, &(cg->has_alloca));
328
329                 if (cg->has_alloca) {
330                         ia32_general_purpose_regs[REG_EBP].type = arch_register_type_ignore;
331                 }
332
333                 irg_walk_blkwise_graph(cg->irg, ia32_place_consts, ia32_transform_node, cg);
334         }
335 }
336
337
338
339 /**
340  * Set the register for P_frame_base Proj to %esp.
341  */
342 static void ia32_set_P_frame_base_Proj_reg(ir_node *irn, void *env) {
343         ia32_code_gen_t *cg = env;
344
345         if (is_P_frame_base_Proj(irn)) {
346                 if (cg->has_alloca) {
347                         arch_set_irn_register(cg->arch_env, irn, &ia32_general_purpose_regs[REG_EBP]);
348                 }
349                 else {
350                         arch_set_irn_register(cg->arch_env, irn, &ia32_general_purpose_regs[REG_ESP]);
351                 }
352         }
353 }
354
355
356
357 /**
358  * Dummy functions for hooks we don't need but which must be filled.
359  */
360 static void ia32_before_sched(void *self) {
361         ia32_code_gen_t *cg = self;
362
363         lower_nodes_before_sched(cg->irg, cg->arch_env);
364 }
365
366 static void ia32_before_ra(void *self) {
367 }
368
369
370 /**
371  * Creates a Store for a Spill
372  */
373 static ir_node *ia32_lower_spill(void *self, ir_node *spill) {
374         ia32_code_gen_t *cg    = self;
375         unsigned         offs  = be_get_spill_offset(spill);
376         dbg_info        *dbg   = get_irn_dbg_info(spill);
377         ir_node         *block = get_nodes_block(spill);
378         ir_node         *ptr   = get_irg_frame(cg->irg);
379         ir_node         *val   = be_get_Spill_context(spill);
380         ir_node         *mem   = new_rd_NoMem(cg->irg);
381         ir_mode         *mode  = get_irn_mode(spill);
382         ir_node         *res;
383
384         res = new_rd_ia32_Store(dbg, cg->irg, block, ptr, val, mem, mode);
385         set_ia32_am_offs(res, new_tarval_from_long(offs, mode_Iu));
386
387         return res;
388 }
389
390 /**
391  * Create a Load for a Spill
392  */
393 static ir_node *ia32_lower_reload(void *self, ir_node *reload) {
394         ia32_code_gen_t *cg    = self;
395         dbg_info        *dbg   = get_irn_dbg_info(reload);
396         ir_node         *block = get_nodes_block(reload);
397         ir_node         *ptr   = get_irg_frame(cg->irg);
398         ir_mode         *mode  = get_irn_mode(reload);
399         ir_node         *pred  = get_irn_n(reload, 0);
400         tarval          *tv;
401         ir_node         *res;
402
403         if (be_is_Spill(pred)) {
404                 tv = new_tarval_from_long(be_get_spill_offset(pred), mode_Iu);
405         }
406         else if (is_ia32_Store(pred)) {
407                 tv = get_ia32_am_offs(pred);
408         }
409         else {
410                 assert(0 && "unsupported Reload predecessor");
411         }
412
413         res = new_rd_ia32_Load(dbg, cg->irg, block, ptr, pred, mode);
414         set_ia32_am_offs(res, tv);
415
416         return res;
417 }
418
419 /**
420  * Emits the code, closes the output file and frees
421  * the code generator interface.
422  */
423 static void ia32_codegen(void *self) {
424         ia32_code_gen_t *cg = self;
425         ir_graph       *irg = cg->irg;
426         FILE           *out = cg->out;
427
428         if (cg->emit_decls) {
429                 ia32_gen_decls(cg->out);
430                 cg->emit_decls = 0;
431         }
432
433         /* set the stack register */
434         if (! is_pseudo_ir_graph(irg))
435                 irg_walk_blkwise_graph(irg, NULL, ia32_set_P_frame_base_Proj_reg, cg);
436
437 //      ia32_finish_irg(irg);
438         ia32_gen_routine(out, irg, cg->arch_env);
439
440         cur_reg_set = NULL;
441
442         /* de-allocate code generator */
443         del_set(cg->reg_set);
444         free(self);
445 }
446
447 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env);
448
449 static const arch_code_generator_if_t ia32_code_gen_if = {
450         ia32_cg_init,
451         ia32_prepare_graph,
452         ia32_before_sched,   /* before scheduling hook */
453         ia32_before_ra,      /* before register allocation hook */
454         ia32_lower_spill,
455         ia32_lower_reload,
456         ia32_codegen         /* emit && done */
457 };
458
459 /**
460  * Initializes the code generator.
461  */
462 static void *ia32_cg_init(FILE *F, ir_graph *irg, const arch_env_t *arch_env) {
463         ia32_isa_t      *isa = (ia32_isa_t *)arch_env->isa;
464         ia32_code_gen_t *cg  = malloc(sizeof(*cg));
465
466         cg->impl       = &ia32_code_gen_if;
467         cg->irg        = irg;
468         cg->reg_set    = new_set(ia32_cmp_irn_reg_assoc, 1024);
469         cg->mod        = firm_dbg_register("be.transform.ia32");
470         cg->out        = F;
471         cg->arch_env   = arch_env;
472
473         isa->num_codegens++;
474
475         if (isa->num_codegens > 1)
476                 cg->emit_decls = 0;
477         else
478                 cg->emit_decls = 1;
479
480         cur_reg_set = cg->reg_set;
481
482         ia32_irn_ops.cg = cg;
483
484         return (arch_code_generator_t *)cg;
485 }
486
487
488
489 /*****************************************************************
490  *  ____             _                  _   _____  _____
491  * |  _ \           | |                | | |_   _|/ ____|  /\
492  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
493  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
494  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
495  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
496  *
497  *****************************************************************/
498
499 /**
500  * Initializes the backend ISA and opens the output file.
501  */
502 static void *ia32_init(void) {
503         static int inited = 0;
504         ia32_isa_t *isa   = malloc(sizeof(*isa));
505
506         isa->impl = &ia32_isa_if;
507
508         if(inited)
509                 return NULL;
510
511         inited = 1;
512
513         isa->num_codegens    = 0;
514         isa->reg_projnum_map = new_set(ia32_cmp_reg_projnum_assoc, 1024);
515
516         ia32_register_init(isa);
517         ia32_create_opcodes();
518
519         return isa;
520 }
521
522
523
524 /**
525  * Closes the output file and frees the ISA structure.
526  */
527 static void ia32_done(void *self) {
528         free(self);
529 }
530
531
532
533 static int ia32_get_n_reg_class(const void *self) {
534         return N_CLASSES;
535 }
536
537 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i) {
538         assert(i >= 0 && i < N_CLASSES && "Invalid ia32 register class requested.");
539         return &ia32_reg_classes[i];
540 }
541
542 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
543         return &ia32_irn_ops;
544 }
545
546 const arch_irn_handler_t ia32_irn_handler = {
547         ia32_get_irn_ops
548 };
549
550 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
551         return &ia32_irn_handler;
552 }
553
554 long ia32_get_call_projnum_for_reg(const void *self, const arch_register_t *reg) {
555         ia32_isa_t *isa = (ia32_isa_t *)self;
556         return ia32_get_reg_projnum(reg, isa->reg_projnum_map);
557 }
558
559 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
560         return is_ia32_irn(irn);
561 }
562
563 /**
564  * Initializes the code generator interface.
565  */
566 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
567         return &ia32_code_gen_if;
568 }
569
570 list_sched_selector_t ia32_sched_selector;
571
572 /**
573  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
574  */
575 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self) {
576         memcpy(&ia32_sched_selector, reg_pressure_selector, sizeof(list_sched_selector_t));
577         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
578         return &ia32_sched_selector;
579 }
580
581 #ifdef WITH_LIBCORE
582 static void ia32_register_options(lc_opt_entry_t *ent)
583 {
584 }
585 #endif /* WITH_LIBCORE */
586
587 const arch_isa_if_t ia32_isa_if = {
588 #ifdef WITH_LIBCORE
589         ia32_register_options,
590 #endif
591         ia32_init,
592         ia32_done,
593         ia32_get_n_reg_class,
594         ia32_get_reg_class,
595         ia32_get_irn_handler,
596         ia32_get_code_generator_if,
597         ia32_get_list_sched_selector,
598         ia32_get_call_projnum_for_reg
599 };