fdc211944638e9c137c84840823bb9e058fb6311
[libfirm] / ir / be / sparc / sparc_emitter.c
1 /*
2  * Copyright (C) 1995-2010 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   emit assembler for a backend graph
23  * @author  Hannes Rapp, Matthias Braun
24  */
25 #include "config.h"
26
27 #include <limits.h>
28
29 #include "bitfiddle.h"
30 #include "xmalloc.h"
31 #include "tv.h"
32 #include "iredges.h"
33 #include "debug.h"
34 #include "irgwalk.h"
35 #include "irprintf.h"
36 #include "irop_t.h"
37 #include "irargs_t.h"
38 #include "irprog.h"
39 #include "irargs_t.h"
40 #include "error.h"
41 #include "raw_bitset.h"
42 #include "dbginfo.h"
43 #include "heights.h"
44
45 #include "besched.h"
46 #include "beblocksched.h"
47 #include "beirg.h"
48 #include "begnuas.h"
49 #include "bedwarf.h"
50 #include "benode.h"
51 #include "bestack.h"
52 #include "bepeephole.h"
53
54 #include "sparc_emitter.h"
55 #include "gen_sparc_emitter.h"
56 #include "sparc_nodes_attr.h"
57 #include "sparc_new_nodes.h"
58 #include "gen_sparc_regalloc_if.h"
59
60 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
61
62 static ir_heights_t  *heights;
63 static const ir_node *delay_slot_filler; /**< this node has been choosen to fill
64                                               the next delay slot */
65
66 static void sparc_emit_node(const ir_node *node);
67 static bool emitting_delay_slot;
68
69 /**
70  * indent before instruction. (Adds additional indentation when emitting
71  * delay slots)
72  */
73 static void sparc_emit_indent(void)
74 {
75         be_emit_char('\t');
76         if (emitting_delay_slot)
77                 be_emit_char(' ');
78 }
79
80 static void sparc_emit_immediate(ir_node const *const node)
81 {
82         const sparc_attr_t *attr   = get_sparc_attr_const(node);
83         ir_entity          *entity = attr->immediate_value_entity;
84
85         if (entity == NULL) {
86                 int32_t value = attr->immediate_value;
87                 assert(sparc_is_value_imm_encodeable(value));
88                 be_emit_irprintf("%d", value);
89         } else {
90                 if (get_entity_owner(entity) == get_tls_type()) {
91                         be_emit_cstring("%tle_lox10(");
92                 } else {
93                         be_emit_cstring("%lo(");
94                 }
95                 be_gas_emit_entity(entity);
96                 if (attr->immediate_value != 0) {
97                         be_emit_irprintf("%+d", attr->immediate_value);
98                 }
99                 be_emit_char(')');
100         }
101 }
102
103 static void sparc_emit_high_immediate(ir_node const *node)
104 {
105         const sparc_attr_t *attr   = get_sparc_attr_const(node);
106         ir_entity          *entity = attr->immediate_value_entity;
107
108         if (entity == NULL) {
109                 uint32_t value = (uint32_t) attr->immediate_value;
110                 be_emit_irprintf("%%hi(0x%X)", value);
111         } else {
112                 if (get_entity_owner(entity) == get_tls_type()) {
113                         be_emit_cstring("%tle_hix22(");
114                 } else {
115                         be_emit_cstring("%hi(");
116                 }
117                 be_gas_emit_entity(entity);
118                 if (attr->immediate_value != 0) {
119                         be_emit_irprintf("%+d", attr->immediate_value);
120                 }
121                 be_emit_char(')');
122         }
123 }
124
125 static void sparc_emit_source_register(ir_node const *node, int const pos)
126 {
127         const arch_register_t *reg = arch_get_irn_register_in(node, pos);
128         be_emit_char('%');
129         be_emit_string(arch_register_get_name(reg));
130 }
131
132 static void sparc_emit_dest_register(ir_node const *const node, int const pos)
133 {
134         const arch_register_t *reg = arch_get_irn_register_out(node, pos);
135         be_emit_char('%');
136         be_emit_string(arch_register_get_name(reg));
137 }
138
139 /**
140  * emit SP offset
141  */
142 static void sparc_emit_offset(const ir_node *node, int offset_node_pos)
143 {
144         const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
145
146         if (attr->is_reg_reg) {
147                 assert(!attr->is_frame_entity);
148                 assert(attr->base.immediate_value == 0);
149                 assert(attr->base.immediate_value_entity == NULL);
150                 be_emit_char('+');
151                 sparc_emit_source_register(node, offset_node_pos);
152         } else if (attr->is_frame_entity) {
153                 int32_t offset = attr->base.immediate_value;
154                 if (offset != 0) {
155                         assert(sparc_is_value_imm_encodeable(offset));
156                         be_emit_irprintf("%+ld", offset);
157                 }
158         } else if (attr->base.immediate_value != 0
159                         || attr->base.immediate_value_entity != NULL) {
160                 be_emit_char('+');
161                 sparc_emit_immediate(node);
162         }
163 }
164
165 /**
166  *  Emit load mode
167  */
168 static void sparc_emit_load_mode(ir_node const *const node)
169 {
170         const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
171         ir_mode *mode      = attr->load_store_mode;
172         int      bits      = get_mode_size_bits(mode);
173         bool     is_signed = mode_is_signed(mode);
174
175         switch (bits) {
176         case   8: be_emit_string(is_signed ? "sb" : "ub"); break;
177         case  16: be_emit_string(is_signed ? "sh" : "uh"); break;
178         case  32: break;
179         case  64: be_emit_char('d'); break;
180         case 128: be_emit_char('q'); break;
181         default:  panic("invalid load/store mode %+F", mode);
182         }
183 }
184
185 /**
186  * Emit store mode char
187  */
188 static void sparc_emit_store_mode(ir_node const *const node)
189 {
190         const sparc_load_store_attr_t *attr = get_sparc_load_store_attr_const(node);
191         ir_mode *mode      = attr->load_store_mode;
192         int      bits      = get_mode_size_bits(mode);
193
194         switch (bits) {
195         case   8: be_emit_char('b'); break;
196         case  16: be_emit_char('h'); break;
197         case  32: break;
198         case  64: be_emit_char('d'); break;
199         case 128: be_emit_char('q'); break;
200         default:  panic("invalid load/store mode %+F", mode);
201         }
202 }
203
204 static void emit_fp_suffix(const ir_mode *mode)
205 {
206         assert(mode_is_float(mode));
207         switch (get_mode_size_bits(mode)) {
208         case  32: be_emit_char('s'); break;
209         case  64: be_emit_char('d'); break;
210         case 128: be_emit_char('q'); break;
211         default:  panic("invalid FP mode");
212         }
213 }
214
215 static ir_node *get_jump_target(const ir_node *jump)
216 {
217         return (ir_node*)get_irn_link(jump);
218 }
219
220 /**
221  * Returns the target label for a control flow node.
222  */
223 static void sparc_emit_cfop_target(const ir_node *node)
224 {
225         ir_node *block = get_jump_target(node);
226         be_gas_emit_block_name(block);
227 }
228
229 /**
230  * returns true if a sparc_call calls a register and not an immediate
231  */
232 static bool is_sparc_reg_call(const ir_node *node)
233 {
234         const sparc_attr_t *attr = get_sparc_attr_const(node);
235         return attr->immediate_value_entity == NULL;
236 }
237
238 static int get_sparc_Call_dest_addr_pos(const ir_node *node)
239 {
240         assert(is_sparc_reg_call(node));
241         return get_irn_arity(node)-1;
242 }
243
244 static bool ba_is_fallthrough(const ir_node *node)
245 {
246         ir_node *block      = get_nodes_block(node);
247         ir_node *next_block = (ir_node*)get_irn_link(block);
248         return get_irn_link(node) == next_block;
249 }
250
251 static bool is_no_instruction(const ir_node *node)
252 {
253         /* copies are nops if src_reg == dest_reg */
254         if (be_is_Copy(node) || be_is_CopyKeep(node)) {
255                 const arch_register_t *src_reg  = arch_get_irn_register_in(node, 0);
256                 const arch_register_t *dest_reg = arch_get_irn_register_out(node, 0);
257
258                 if (src_reg == dest_reg)
259                         return true;
260         }
261         if (be_is_IncSP(node) && be_get_IncSP_offset(node) == 0)
262                 return true;
263         /* Ba is not emitted if it is a simple fallthrough */
264         if (is_sparc_Ba(node) && ba_is_fallthrough(node))
265                 return true;
266
267         return be_is_Keep(node) || be_is_Start(node) || is_Phi(node);
268 }
269
270 static bool has_delay_slot(const ir_node *node)
271 {
272         if (is_sparc_Ba(node)) {
273                 return !ba_is_fallthrough(node);
274         }
275
276         return arch_get_irn_flags(node) & sparc_arch_irn_flag_has_delay_slot;
277 }
278
279 /** returns true if the emitter for this sparc node can produce more than one
280  * actual sparc instruction.
281  * Usually it is a bad sign if we have to add instructions here. We should
282  * rather try to get them lowered down. So we can actually put them into
283  * delay slots and make them more accessible to the scheduler.
284  */
285 static bool emits_multiple_instructions(const ir_node *node)
286 {
287         if (has_delay_slot(node))
288                 return true;
289
290         if (is_sparc_Call(node)) {
291                 return arch_get_irn_flags(node) & sparc_arch_irn_flag_aggregate_return;
292         }
293
294         return is_sparc_SMulh(node) || is_sparc_UMulh(node)
295                 || is_sparc_SDiv(node) || is_sparc_UDiv(node)
296                 || be_is_MemPerm(node) || be_is_Perm(node)
297                 || is_sparc_SubSP(node);
298 }
299
300 static bool uses_reg(const ir_node *node, const arch_register_t *reg)
301 {
302         int arity = get_irn_arity(node);
303         int i;
304
305         for (i = 0; i < arity; ++i) {
306                 const arch_register_t *in_reg = arch_get_irn_register_in(node, i);
307                 if (reg == in_reg)
308                         return true;
309         }
310         return false;
311 }
312
313 static bool writes_reg(const ir_node *node, const arch_register_t *reg)
314 {
315         unsigned n_outs = arch_get_irn_n_outs(node);
316         unsigned o;
317         for (o = 0; o < n_outs; ++o) {
318                 const arch_register_t *out_reg = arch_get_irn_register_out(node, o);
319                 if (out_reg == reg)
320                         return true;
321         }
322         return false;
323 }
324
325 static bool can_move_into_delayslot(const ir_node *node, const ir_node *to)
326 {
327         if (!be_can_move_before(heights, node, to))
328                 return false;
329
330         if (is_sparc_Call(to)) {
331                 ir_node *check;
332                 /** all deps are used after the delay slot so, we're fine */
333                 if (!is_sparc_reg_call(to))
334                         return true;
335
336                 check = get_irn_n(to, get_sparc_Call_dest_addr_pos(to));
337                 if (skip_Proj(check) == node)
338                         return false;
339
340                 /* the Call also destroys the value of %o7, but since this is
341                  * currently marked as ignore register in the backend, it
342                  * should never be used by the instruction in the delay slot. */
343                 if (uses_reg(node, &sparc_registers[REG_O7]))
344                         return false;
345                 return true;
346         } else if (is_sparc_Return(to)) {
347                 /* return uses the value of %o7, all other values are not
348                  * immediately used */
349                 if (writes_reg(node, &sparc_registers[REG_O7]))
350                         return false;
351                 return true;
352         } else {
353                 /* the node must not use our computed values */
354                 int arity = get_irn_arity(to);
355                 int i;
356                 for (i = 0; i < arity; ++i) {
357                         ir_node *in = get_irn_n(to, i);
358                         if (skip_Proj(in) == node)
359                                 return false;
360                 }
361                 return true;
362         }
363 }
364
365 /**
366  * search for an instruction that can fill the delay slot of @p node
367  */
368 static const ir_node *pick_delay_slot_for(const ir_node *node)
369 {
370         const ir_node *schedpoint = node;
371         unsigned       tries      = 0;
372         /* currently we don't track which registers are still alive, so we can't
373          * pick any other instructions other than the one directly preceding */
374         static const unsigned PICK_DELAY_SLOT_MAX_DISTANCE = 10;
375
376         assert(has_delay_slot(node));
377
378         while (sched_has_prev(schedpoint)) {
379                 schedpoint = sched_prev(schedpoint);
380
381                 if (has_delay_slot(schedpoint))
382                         break;
383
384                 /* skip things which don't really result in instructions */
385                 if (is_no_instruction(schedpoint))
386                         continue;
387
388                 if (tries++ >= PICK_DELAY_SLOT_MAX_DISTANCE)
389                         break;
390
391                 if (emits_multiple_instructions(schedpoint))
392                         continue;
393
394                 if (!can_move_into_delayslot(schedpoint, node))
395                         continue;
396
397                 /* found something */
398                 return schedpoint;
399         }
400
401         return NULL;
402 }
403
404 void sparc_emitf(ir_node const *const node, char const *fmt, ...)
405 {
406         va_list ap;
407         va_start(ap, fmt);
408         sparc_emit_indent();
409         for (;;) {
410                 char const *start = fmt;
411
412                 while (*fmt != '%' && *fmt != '\0')
413                         ++fmt;
414                 be_emit_string_len(start, fmt - start);
415                 if (*fmt == '\0')
416                         break;
417                 ++fmt;
418
419                 bool plus = false;
420                 if (*fmt == '+') {
421                         plus = true;
422                         ++fmt;
423                 }
424
425                 switch (*fmt++) {
426                 case '%':
427                         be_emit_char('%');
428                         break;
429
430                 case 'D':
431                         if (*fmt < '0' || '9' <= *fmt)
432                                 goto unknown;
433                         sparc_emit_dest_register(node, *fmt++ - '0');
434                         break;
435
436                 case 'E': {
437                         sparc_attr_t const *const attr = get_sparc_attr_const(node);
438                         be_gas_emit_entity(attr->immediate_value_entity);
439                         if (attr->immediate_value != 0) {
440                                 be_emit_irprintf(plus ? "%+d" : "%d", attr->immediate_value);
441                         }
442                         break;
443                 }
444
445                 case 'F': {
446                         ir_mode *mode;
447                         switch (*fmt++) {
448                         case 'D': mode = get_sparc_fp_conv_attr_const(node)->dest_mode; break;
449                         case 'M': mode = get_sparc_fp_attr_const(node)->fp_mode;        break;
450                         case 'S': mode = get_sparc_fp_conv_attr_const(node)->src_mode;  break;
451                         default:  goto unknown;
452                         }
453                         emit_fp_suffix(mode);
454                         break;
455                 }
456
457                 case 'H':
458                         sparc_emit_high_immediate(node);
459                         break;
460
461                 case 'L':
462                         sparc_emit_cfop_target(node);
463                         break;
464
465                 case 'M':
466                         switch (*fmt++) {
467                         case 'L': sparc_emit_load_mode(node);  break;
468                         case 'S': sparc_emit_store_mode(node); break;
469                         default:  goto unknown;
470                         }
471                         break;
472
473                 case 'O':
474                         if (*fmt < '0' || '9' <= *fmt)
475                                 goto unknown;
476                         sparc_emit_offset(node, *fmt++ - '0');
477                         break;
478
479                 case 'R': {
480                         arch_register_t const *const reg = va_arg(ap, const arch_register_t*);
481                         be_emit_char('%');
482                         be_emit_string(arch_register_get_name(reg));
483                         break;
484                 }
485
486                 case 'S': {
487                         bool imm = false;
488                         if (*fmt == 'I') {
489                                 imm = true;
490                                 ++fmt;
491                         }
492                         if (*fmt < '0' || '9' <= *fmt)
493                                 goto unknown;
494                         unsigned const pos = *fmt++ - '0';
495                         if (imm && arch_get_irn_flags(node) & (arch_irn_flags_t)sparc_arch_irn_flag_immediate_form) {
496                                 sparc_emit_immediate(node);
497                         } else {
498                                 sparc_emit_source_register(node, pos);
499                         }
500                         break;
501                 }
502
503                 case 'd': {
504                         int const num = va_arg(ap, int);
505                         be_emit_irprintf(plus ? "%+d" : "%d", num);
506                         break;
507                 }
508
509                 case 's': {
510                         char const *const str = va_arg(ap, char const*);
511                         be_emit_string(str);
512                         break;
513                 }
514
515                 case 'u': {
516                         unsigned const num = va_arg(ap, unsigned);
517                         be_emit_irprintf(plus ? "%+u" : "%u", num);
518                         break;
519                 }
520
521                 default:
522 unknown:
523                         panic("unknown format conversion in sparc_emitf()");
524                 }
525         }
526         be_emit_finish_line_gas(node);
527         va_end(ap);
528 }
529
530 /**
531  * Emits code for stack space management
532  */
533 static void emit_be_IncSP(const ir_node *irn)
534 {
535         int offset = be_get_IncSP_offset(irn);
536
537         if (offset == 0)
538                 return;
539
540         /* SPARC stack grows downwards */
541         char const *const insn = offset > 0 ? offset = -offset, "add" : "sub";
542         sparc_emitf(irn, "%s %S0, %d, %D0", insn, offset);
543 }
544
545 /**
546  * Emits code for stack space management.
547  */
548 static void emit_sparc_SubSP(const ir_node *irn)
549 {
550         sparc_emitf(irn, "sub %S0, %SI1, %D0");
551         sparc_emitf(irn, "add %S0, %u, %D1", SPARC_MIN_STACKSIZE);
552 }
553
554 static void fill_delay_slot(void)
555 {
556         emitting_delay_slot = true;
557         if (delay_slot_filler != NULL) {
558                 sparc_emit_node(delay_slot_filler);
559                 delay_slot_filler = NULL;
560         } else {
561                 sparc_emitf(NULL, "nop");
562         }
563         emitting_delay_slot = false;
564 }
565
566 static void emit_sparc_Div(const ir_node *node, char const *const insn)
567 {
568         /* can we get the delay count of the wr instruction somewhere? */
569         unsigned wry_delay_count = 3;
570         unsigned i;
571
572         sparc_emitf(node, "wr %S0, 0, %%y");
573
574         for (i = 0; i < wry_delay_count; ++i) {
575                 fill_delay_slot();
576         }
577
578         sparc_emitf(node, "%s %S1, %SI2, %D0", insn);
579 }
580
581 static void emit_sparc_SDiv(const ir_node *node)
582 {
583         emit_sparc_Div(node, "sdiv");
584 }
585
586 static void emit_sparc_UDiv(const ir_node *node)
587 {
588         emit_sparc_Div(node, "udiv");
589 }
590
591 static void emit_sparc_Call(const ir_node *node)
592 {
593         if (is_sparc_reg_call(node)) {
594                 int dest_addr = get_sparc_Call_dest_addr_pos(node);
595                 sparc_emitf(node, "call %R", arch_get_irn_register_in(node, dest_addr));
596         } else {
597                 sparc_emitf(node, "call %E, 0");
598         }
599
600         fill_delay_slot();
601
602         if (arch_get_irn_flags(node) & sparc_arch_irn_flag_aggregate_return) {
603                 sparc_emitf(NULL, "unimp 8");
604         }
605 }
606
607 static void emit_be_Perm(const ir_node *irn)
608 {
609         sparc_emitf(irn, "xor %S1, %S0, %S0");
610         sparc_emitf(irn, "xor %S1, %S0, %S1");
611         sparc_emitf(irn, "xor %S1, %S0, %S0");
612 }
613
614 /* The stack pointer must always be SPARC_STACK_ALIGNMENT bytes aligned, so get
615  * the next bigger integer that's evenly divisible by it. */
616 static unsigned get_aligned_sp_change(const unsigned num_regs)
617 {
618         const unsigned bytes = num_regs * SPARC_REGISTER_SIZE;
619         return round_up2(bytes, SPARC_STACK_ALIGNMENT);
620 }
621
622 /* Spill register l0 or both l0 and l1, depending on n_spilled and n_to_spill.*/
623 static void memperm_emit_spill_registers(const ir_node *node, int n_spilled,
624                                          int n_to_spill)
625 {
626         assert(n_spilled < n_to_spill);
627
628         if (n_spilled == 0) {
629                 /* We always reserve stack space for two registers because during copy
630                  * processing we don't know yet if we also need to handle a cycle which
631                  * needs two registers.  More complicated code in emit_MemPerm would
632                  * prevent wasting SPARC_REGISTER_SIZE bytes of stack space but
633                  * it is not worth the worse readability of emit_MemPerm. */
634
635                 /* Keep stack pointer aligned. */
636                 unsigned sp_change = get_aligned_sp_change(2);
637                 sparc_emitf(node, "sub %%sp, %u, %%sp", sp_change);
638
639                 /* Spill register l0. */
640                 sparc_emitf(node, "st %%l0, [%%sp%+d]", SPARC_MIN_STACKSIZE);
641         }
642
643         if (n_to_spill == 2) {
644                 /* Spill register l1. */
645                 sparc_emitf(node, "st %%l1, [%%sp%+d]", SPARC_MIN_STACKSIZE + SPARC_REGISTER_SIZE);
646         }
647 }
648
649 /* Restore register l0 or both l0 and l1, depending on n_spilled. */
650 static void memperm_emit_restore_registers(const ir_node *node, int n_spilled)
651 {
652         unsigned sp_change;
653
654         if (n_spilled == 2) {
655                 /* Restore register l1. */
656                 sparc_emitf(node, "ld [%%sp%+d], %%l1", SPARC_MIN_STACKSIZE + SPARC_REGISTER_SIZE);
657         }
658
659         /* Restore register l0. */
660         sparc_emitf(node, "ld [%%sp%+d], %%l0", SPARC_MIN_STACKSIZE);
661
662         /* Restore stack pointer. */
663         sp_change = get_aligned_sp_change(2);
664         sparc_emitf(node, "add %%sp, %u, %%sp", sp_change);
665 }
666
667 /* Emit code to copy in_ent to out_ent.  Only uses l0. */
668 static void memperm_emit_copy(const ir_node *node, ir_entity *in_ent,
669                               ir_entity *out_ent)
670 {
671         ir_graph          *irg     = get_irn_irg(node);
672         be_stack_layout_t *layout  = be_get_irg_stack_layout(irg);
673         int                off_in  = be_get_stack_entity_offset(layout, in_ent, 0);
674         int                off_out = be_get_stack_entity_offset(layout, out_ent, 0);
675
676         /* Load from input entity. */
677         sparc_emitf(node, "ld [%%fp%+d], %%l0", off_in);
678         /* Store to output entity. */
679         sparc_emitf(node, "st %%l0, [%%fp%+d]", off_out);
680 }
681
682 /* Emit code to swap ent1 and ent2.  Uses l0 and l1. */
683 static void memperm_emit_swap(const ir_node *node, ir_entity *ent1,
684                               ir_entity *ent2)
685 {
686         ir_graph          *irg     = get_irn_irg(node);
687         be_stack_layout_t *layout  = be_get_irg_stack_layout(irg);
688         int                off1    = be_get_stack_entity_offset(layout, ent1, 0);
689         int                off2    = be_get_stack_entity_offset(layout, ent2, 0);
690
691         /* Load from first input entity. */
692         sparc_emitf(node, "ld [%%fp%+d], %%l0", off1);
693         /* Load from second input entity. */
694         sparc_emitf(node, "ld [%%fp%+d], %%l1", off2);
695         /* Store first value to second output entity. */
696         sparc_emitf(node, "st %%l0, [%%fp%+d]", off2);
697         /* Store second value to first output entity. */
698         sparc_emitf(node, "st %%l1, [%%fp%+d]", off1);
699 }
700
701 /* Find the index of ent in ents or return -1 if not found. */
702 static int get_index(ir_entity **ents, int n, ir_entity *ent)
703 {
704         int i;
705
706         for (i = 0; i < n; ++i)
707                 if (ents[i] == ent)
708                         return i;
709
710         return -1;
711 }
712
713 /*
714  * Emit code for a MemPerm node.
715  *
716  * Analyze MemPerm for copy chains and cyclic swaps and resolve them using
717  * loads and stores.
718  * This function is conceptually very similar to permute_values in
719  * beprefalloc.c.
720  */
721 static void emit_be_MemPerm(const ir_node *node)
722 {
723         int         memperm_arity = be_get_MemPerm_entity_arity(node);
724         /* Upper limit for the number of participating entities is twice the
725          * arity, e.g., for a simple copying MemPerm node with one input/output. */
726         int         max_size      = 2 * memperm_arity;
727         ir_entity **entities      = ALLOCANZ(ir_entity *, max_size);
728         /* sourceof contains the input entity for each entity.  If an entity is
729          * never used as an output, its entry in sourceof is a fix point. */
730         int        *sourceof      = ALLOCANZ(int,         max_size);
731         /* n_users counts how many output entities use this entity as their input.*/
732         int        *n_users       = ALLOCANZ(int,         max_size);
733         /* n_spilled records the number of spilled registers, either 1 or 2. */
734         int         n_spilled     = 0;
735         int         i, n, oidx;
736
737         /* This implementation currently only works with frame pointers. */
738         ir_graph          *irg    = get_irn_irg(node);
739         be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
740         assert(!layout->sp_relative && "MemPerms currently do not work without frame pointers");
741
742         for (i = 0; i < max_size; ++i) {
743                 sourceof[i] = i;
744         }
745
746         for (i = n = 0; i < memperm_arity; ++i) {
747                 ir_entity *out  = be_get_MemPerm_out_entity(node, i);
748                 ir_entity *in   = be_get_MemPerm_in_entity(node, i);
749                 int              oidx; /* Out index */
750                 int              iidx; /* In index */
751
752                 /* Insert into entities to be able to operate on unique indices. */
753                 if (get_index(entities, n, out) == -1)
754                         entities[n++] = out;
755                 if (get_index(entities, n, in) == -1)
756                         entities[n++] = in;
757
758                 oidx = get_index(entities, n, out);
759                 iidx = get_index(entities, n, in);
760
761                 sourceof[oidx] = iidx; /* Remember the source. */
762                 ++n_users[iidx]; /* Increment number of users of this entity. */
763         }
764
765         /* First do all the copies. */
766         for (oidx = 0; oidx < n; /* empty */) {
767                 int iidx = sourceof[oidx];
768
769                 /* Nothing to do for fix points.
770                  * Also, if entities[oidx] is used as an input by another copy, we
771                  * can't overwrite entities[oidx] yet.*/
772                 if (iidx == oidx || n_users[oidx] > 0) {
773                         ++oidx;
774                         continue;
775                 }
776
777                 /* We found the end of a 'chain', so do the copy. */
778                 if (n_spilled == 0) {
779                         memperm_emit_spill_registers(node, n_spilled, /*n_to_spill=*/1);
780                         n_spilled = 1;
781                 }
782                 memperm_emit_copy(node, entities[iidx], entities[oidx]);
783
784                 /* Mark as done. */
785                 sourceof[oidx] = oidx;
786
787                 assert(n_users[iidx] > 0);
788                 /* Decrementing the number of users might enable us to do another
789                  * copy. */
790                 --n_users[iidx];
791
792                 if (iidx < oidx && n_users[iidx] == 0) {
793                         oidx = iidx;
794                 } else {
795                         ++oidx;
796                 }
797         }
798
799         /* The rest are cycles. */
800         for (oidx = 0; oidx < n; /* empty */) {
801                 int iidx = sourceof[oidx];
802                 int tidx;
803
804                 /* Nothing to do for fix points. */
805                 if (iidx == oidx) {
806                         ++oidx;
807                         continue;
808                 }
809
810                 assert(n_users[iidx] == 1);
811
812                 /* Swap the two values to resolve the cycle. */
813                 if (n_spilled < 2) {
814                         memperm_emit_spill_registers(node, n_spilled, /*n_to_spill=*/2);
815                         n_spilled = 2;
816                 }
817                 memperm_emit_swap(node, entities[iidx], entities[oidx]);
818
819                 tidx = sourceof[iidx];
820                 /* Mark as done. */
821                 sourceof[iidx] = iidx;
822
823                 /* The source of oidx is now the old source of iidx, because we swapped
824                  * the two entities. */
825                 sourceof[oidx] = tidx;
826         }
827
828 #ifdef DEBUG_libfirm
829         /* Only fix points should remain. */
830         for (i = 0; i < max_size; ++i) {
831                 assert(sourceof[i] == i);
832         }
833 #endif
834
835         assert(n_spilled > 0 && "Useless MemPerm node");
836
837         memperm_emit_restore_registers(node, n_spilled);
838 }
839
840 static void emit_sparc_Return(const ir_node *node)
841 {
842         ir_graph  *irg    = get_irn_irg(node);
843         ir_entity *entity = get_irg_entity(irg);
844         ir_type   *type   = get_entity_type(entity);
845
846         const char *destreg = "%o7";
847
848         /* hack: we don't explicitely model register changes because of the
849          * restore node. So we have to do it manually here */
850         if (delay_slot_filler != NULL &&
851                         (is_sparc_Restore(delay_slot_filler)
852                          || is_sparc_RestoreZero(delay_slot_filler))) {
853                 destreg = "%i7";
854         }
855         char const *const offset = get_method_calling_convention(type) & cc_compound_ret ? "12" : "8";
856         sparc_emitf(node, "jmp %s+%s", destreg, offset);
857         fill_delay_slot();
858 }
859
860 static const arch_register_t *map_i_to_o_reg(const arch_register_t *reg)
861 {
862         unsigned idx = reg->global_index;
863         if (idx < REG_I0 || idx > REG_I7)
864                 return reg;
865         idx += REG_O0 - REG_I0;
866         assert(REG_O0 <= idx && idx <= REG_O7);
867         return &sparc_registers[idx];
868 }
869
870 static void emit_sparc_Restore(const ir_node *node)
871 {
872         const arch_register_t *destreg
873                 = arch_get_irn_register_out(node, pn_sparc_Restore_res);
874         sparc_emitf(node, "restore %S2, %SI3, %R", map_i_to_o_reg(destreg));
875 }
876
877 static void emit_sparc_FrameAddr(const ir_node *node)
878 {
879         const sparc_attr_t *attr   = get_sparc_attr_const(node);
880         int32_t             offset = attr->immediate_value;
881
882         char const *const insn = offset > 0 ? offset = -offset, "sub" : "add";
883         assert(sparc_is_value_imm_encodeable(offset));
884         sparc_emitf(node, "%s %S0, %d, %D0", insn, offset);
885 }
886
887 static const char *get_icc_unsigned(ir_relation relation)
888 {
889         switch (relation & (ir_relation_less_equal_greater)) {
890         case ir_relation_false:              return "bn";
891         case ir_relation_equal:              return "be";
892         case ir_relation_less:               return "blu";
893         case ir_relation_less_equal:         return "bleu";
894         case ir_relation_greater:            return "bgu";
895         case ir_relation_greater_equal:      return "bgeu";
896         case ir_relation_less_greater:       return "bne";
897         case ir_relation_less_equal_greater: return "ba";
898         default: panic("Cmp has unsupported relation");
899         }
900 }
901
902 static const char *get_icc_signed(ir_relation relation)
903 {
904         switch (relation & (ir_relation_less_equal_greater)) {
905         case ir_relation_false:              return "bn";
906         case ir_relation_equal:              return "be";
907         case ir_relation_less:               return "bl";
908         case ir_relation_less_equal:         return "ble";
909         case ir_relation_greater:            return "bg";
910         case ir_relation_greater_equal:      return "bge";
911         case ir_relation_less_greater:       return "bne";
912         case ir_relation_less_equal_greater: return "ba";
913         default: panic("Cmp has unsupported relation");
914         }
915 }
916
917 static const char *get_fcc(ir_relation relation)
918 {
919         switch (relation) {
920         case ir_relation_false:                   return "fbn";
921         case ir_relation_equal:                   return "fbe";
922         case ir_relation_less:                    return "fbl";
923         case ir_relation_less_equal:              return "fble";
924         case ir_relation_greater:                 return "fbg";
925         case ir_relation_greater_equal:           return "fbge";
926         case ir_relation_less_greater:            return "fblg";
927         case ir_relation_less_equal_greater:      return "fbo";
928         case ir_relation_unordered:               return "fbu";
929         case ir_relation_unordered_equal:         return "fbue";
930         case ir_relation_unordered_less:          return "fbul";
931         case ir_relation_unordered_less_equal:    return "fbule";
932         case ir_relation_unordered_greater:       return "fbug";
933         case ir_relation_unordered_greater_equal: return "fbuge";
934         case ir_relation_unordered_less_greater:  return "fbne";
935         case ir_relation_true:                    return "fba";
936         }
937         panic("invalid relation");
938 }
939
940 typedef const char* (*get_cc_func)(ir_relation relation);
941
942 static void emit_sparc_branch(const ir_node *node, get_cc_func get_cc)
943 {
944         const sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr_const(node);
945         ir_relation    relation    = attr->relation;
946         const ir_node *proj_true   = NULL;
947         const ir_node *proj_false  = NULL;
948         const ir_node *block;
949         const ir_node *next_block;
950
951         foreach_out_edge(node, edge) {
952                 ir_node *proj = get_edge_src_irn(edge);
953                 long nr = get_Proj_proj(proj);
954                 if (nr == pn_Cond_true) {
955                         proj_true = proj;
956                 } else {
957                         proj_false = proj;
958                 }
959         }
960
961         /* for now, the code works for scheduled and non-schedules blocks */
962         block = get_nodes_block(node);
963
964         /* we have a block schedule */
965         next_block = (ir_node*)get_irn_link(block);
966
967         if (get_irn_link(proj_true) == next_block) {
968                 /* exchange both proj's so the second one can be omitted */
969                 const ir_node *t = proj_true;
970
971                 proj_true  = proj_false;
972                 proj_false = t;
973                 relation   = get_negated_relation(relation);
974         }
975
976         /* emit the true proj */
977         sparc_emitf(proj_true, "%s %L", get_cc(relation));
978         fill_delay_slot();
979
980         if (get_irn_link(proj_false) == next_block) {
981                 if (be_options.verbose_asm) {
982                         sparc_emitf(proj_false, "/* fallthrough to %L */");
983                 }
984         } else {
985                 sparc_emitf(proj_false, "ba %L");
986                 fill_delay_slot();
987         }
988 }
989
990 static void emit_sparc_Bicc(const ir_node *node)
991 {
992         const sparc_jmp_cond_attr_t *attr = get_sparc_jmp_cond_attr_const(node);
993         bool             is_unsigned = attr->is_unsigned;
994         emit_sparc_branch(node, is_unsigned ? get_icc_unsigned : get_icc_signed);
995 }
996
997 static void emit_sparc_fbfcc(const ir_node *node)
998 {
999         /* if the flags producing node was immediately in front of us, emit
1000          * a nop */
1001         ir_node *flags = get_irn_n(node, n_sparc_fbfcc_flags);
1002         ir_node *prev  = sched_prev(node);
1003         if (is_Block(prev)) {
1004                 /* TODO: when the flags come from another block, then we have to do
1005                  * more complicated tests to see wether the flag producing node is
1006                  * potentially in front of us (could happen for fallthroughs) */
1007                 panic("TODO: fbfcc flags come from other block");
1008         }
1009         if (skip_Proj(flags) == prev) {
1010                 sparc_emitf(NULL, "nop");
1011         }
1012         emit_sparc_branch(node, get_fcc);
1013 }
1014
1015 static void emit_sparc_Ba(const ir_node *node)
1016 {
1017         if (ba_is_fallthrough(node)) {
1018                 if (be_options.verbose_asm) {
1019                         sparc_emitf(node, "/* fallthrough to %L */");
1020                 }
1021         } else {
1022                 sparc_emitf(node, "ba %L");
1023                 fill_delay_slot();
1024         }
1025 }
1026
1027 static void emit_sparc_SwitchJmp(const ir_node *node)
1028 {
1029         const sparc_switch_jmp_attr_t *attr = get_sparc_switch_jmp_attr_const(node);
1030
1031         sparc_emitf(node, "jmp %S0");
1032         fill_delay_slot();
1033
1034         be_emit_jump_table(node, attr->table, attr->table_entity, get_jump_target);
1035 }
1036
1037 static void emit_fmov(const ir_node *node, const arch_register_t *src_reg,
1038                       const arch_register_t *dst_reg)
1039 {
1040         sparc_emitf(node, "fmovs %R, %R", src_reg, dst_reg);
1041 }
1042
1043 static const arch_register_t *get_next_fp_reg(const arch_register_t *reg)
1044 {
1045         unsigned idx = reg->global_index;
1046         assert(reg == &sparc_registers[idx]);
1047         idx++;
1048         assert(idx - REG_F0 < N_sparc_fp_REGS);
1049         return &sparc_registers[idx];
1050 }
1051
1052 static void emit_be_Copy(const ir_node *node)
1053 {
1054         ir_mode               *mode    = get_irn_mode(node);
1055         const arch_register_t *src_reg = arch_get_irn_register_in(node, 0);
1056         const arch_register_t *dst_reg = arch_get_irn_register_out(node, 0);
1057
1058         if (src_reg == dst_reg)
1059                 return;
1060
1061         if (mode_is_float(mode)) {
1062                 unsigned bits = get_mode_size_bits(mode);
1063                 int      n    = bits > 32 ? bits > 64 ? 3 : 1 : 0;
1064                 int      i;
1065                 emit_fmov(node, src_reg, dst_reg);
1066                 for (i = 0; i < n; ++i) {
1067                         src_reg = get_next_fp_reg(src_reg);
1068                         dst_reg = get_next_fp_reg(dst_reg);
1069                         emit_fmov(node, src_reg, dst_reg);
1070                 }
1071         } else if (mode_is_data(mode)) {
1072                 sparc_emitf(node, "mov %S0, %D0");
1073         } else {
1074                 panic("invalid mode");
1075         }
1076 }
1077
1078 static void emit_nothing(const ir_node *irn)
1079 {
1080         (void) irn;
1081 }
1082
1083 typedef void (*emit_func) (const ir_node *);
1084
1085 static inline void set_emitter(ir_op *op, emit_func sparc_emit_node)
1086 {
1087         op->ops.generic = (op_func)sparc_emit_node;
1088 }
1089
1090 /**
1091  * Enters the emitter functions for handled nodes into the generic
1092  * pointer of an opcode.
1093  */
1094 static void sparc_register_emitters(void)
1095 {
1096         /* first clear the generic function pointer for all ops */
1097         ir_clear_opcodes_generic_func();
1098         /* register all emitter functions defined in spec */
1099         sparc_register_spec_emitters();
1100
1101         /* custom emitter */
1102         set_emitter(op_be_Copy,         emit_be_Copy);
1103         set_emitter(op_be_CopyKeep,     emit_be_Copy);
1104         set_emitter(op_be_IncSP,        emit_be_IncSP);
1105         set_emitter(op_be_MemPerm,      emit_be_MemPerm);
1106         set_emitter(op_be_Perm,         emit_be_Perm);
1107         set_emitter(op_sparc_Ba,        emit_sparc_Ba);
1108         set_emitter(op_sparc_Bicc,      emit_sparc_Bicc);
1109         set_emitter(op_sparc_Call,      emit_sparc_Call);
1110         set_emitter(op_sparc_fbfcc,     emit_sparc_fbfcc);
1111         set_emitter(op_sparc_FrameAddr, emit_sparc_FrameAddr);
1112         set_emitter(op_sparc_SubSP,     emit_sparc_SubSP);
1113         set_emitter(op_sparc_Restore,   emit_sparc_Restore);
1114         set_emitter(op_sparc_Return,    emit_sparc_Return);
1115         set_emitter(op_sparc_SDiv,      emit_sparc_SDiv);
1116         set_emitter(op_sparc_SwitchJmp, emit_sparc_SwitchJmp);
1117         set_emitter(op_sparc_UDiv,      emit_sparc_UDiv);
1118
1119         /* no need to emit anything for the following nodes */
1120         set_emitter(op_be_Keep,     emit_nothing);
1121         set_emitter(op_sparc_Start, emit_nothing);
1122         set_emitter(op_Phi,         emit_nothing);
1123 }
1124
1125 /**
1126  * Emits code for a node.
1127  */
1128 static void sparc_emit_node(const ir_node *node)
1129 {
1130         ir_op *op = get_irn_op(node);
1131
1132         if (op->ops.generic) {
1133                 emit_func func = (emit_func) op->ops.generic;
1134                 be_dwarf_location(get_irn_dbg_info(node));
1135                 (*func) (node);
1136         } else {
1137                 panic("No emit handler for node %+F (graph %+F)\n", node,
1138                       get_irn_irg(node));
1139         }
1140 }
1141
1142 static ir_node *find_next_delay_slot(ir_node *from)
1143 {
1144         ir_node *schedpoint = from;
1145         while (!has_delay_slot(schedpoint)) {
1146                 if (!sched_has_next(schedpoint))
1147                         return NULL;
1148                 schedpoint = sched_next(schedpoint);
1149         }
1150         return schedpoint;
1151 }
1152
1153 static bool block_needs_label(const ir_node *block, const ir_node *sched_prev)
1154 {
1155         int n_cfgpreds;
1156
1157         if (get_Block_entity(block) != NULL)
1158                 return true;
1159
1160         n_cfgpreds = get_Block_n_cfgpreds(block);
1161         if (n_cfgpreds == 0) {
1162                 return false;
1163         } else if (n_cfgpreds > 1) {
1164                 return true;
1165         } else {
1166                 ir_node *cfgpred       = get_Block_cfgpred(block, 0);
1167                 ir_node *cfgpred_block = get_nodes_block(cfgpred);
1168                 if (is_Proj(cfgpred) && is_sparc_SwitchJmp(get_Proj_pred(cfgpred)))
1169                         return true;
1170                 return sched_prev != cfgpred_block || get_irn_link(cfgpred) != block;
1171         }
1172 }
1173
1174 /**
1175  * Walks over the nodes in a block connected by scheduling edges
1176  * and emits code for each node.
1177  */
1178 static void sparc_emit_block(ir_node *block, ir_node *prev)
1179 {
1180         ir_node *next_delay_slot;
1181         bool     needs_label = block_needs_label(block, prev);
1182
1183         be_gas_begin_block(block, needs_label);
1184
1185         next_delay_slot = find_next_delay_slot(sched_first(block));
1186         if (next_delay_slot != NULL)
1187                 delay_slot_filler = pick_delay_slot_for(next_delay_slot);
1188
1189         sched_foreach(block, node) {
1190                 if (node == delay_slot_filler) {
1191                         continue;
1192                 }
1193
1194                 sparc_emit_node(node);
1195
1196                 if (node == next_delay_slot) {
1197                         assert(delay_slot_filler == NULL);
1198                         next_delay_slot = find_next_delay_slot(sched_next(node));
1199                         if (next_delay_slot != NULL)
1200                                 delay_slot_filler = pick_delay_slot_for(next_delay_slot);
1201                 }
1202         }
1203 }
1204
1205 /**
1206  * Emits code for function start.
1207  */
1208 static void sparc_emit_func_prolog(ir_graph *irg)
1209 {
1210         ir_entity *entity = get_irg_entity(irg);
1211         be_gas_emit_function_prolog(entity, 4, NULL);
1212 }
1213
1214 /**
1215  * Emits code for function end
1216  */
1217 static void sparc_emit_func_epilog(ir_graph *irg)
1218 {
1219         ir_entity *entity = get_irg_entity(irg);
1220         be_gas_emit_function_epilog(entity);
1221 }
1222
1223 static void sparc_gen_labels(ir_node *block, void *env)
1224 {
1225         ir_node *pred;
1226         int n = get_Block_n_cfgpreds(block);
1227         (void) env;
1228
1229         for (n--; n >= 0; n--) {
1230                 pred = get_Block_cfgpred(block, n);
1231                 set_irn_link(pred, block); // link the pred of a block (which is a jmp)
1232         }
1233 }
1234
1235 void sparc_emit_routine(ir_graph *irg)
1236 {
1237         ir_node **block_schedule;
1238         size_t    i;
1239         size_t    n;
1240
1241         heights = heights_new(irg);
1242
1243         /* register all emitter functions */
1244         sparc_register_emitters();
1245
1246         /* create the block schedule. For now, we don't need it earlier. */
1247         block_schedule = be_create_block_schedule(irg);
1248
1249         sparc_emit_func_prolog(irg);
1250         irg_block_walk_graph(irg, sparc_gen_labels, NULL, NULL);
1251
1252         /* inject block scheduling links & emit code of each block */
1253         n = ARR_LEN(block_schedule);
1254         for (i = 0; i < n; ++i) {
1255                 ir_node *block      = block_schedule[i];
1256                 ir_node *next_block = i+1 < n ? block_schedule[i+1] : NULL;
1257                 set_irn_link(block, next_block);
1258         }
1259
1260         for (i = 0; i < n; ++i) {
1261                 ir_node *block = block_schedule[i];
1262                 ir_node *prev  = i>=1 ? block_schedule[i-1] : NULL;
1263                 if (block == get_irg_end_block(irg))
1264                         continue;
1265                 sparc_emit_block(block, prev);
1266         }
1267
1268         /* emit function epilog */
1269         sparc_emit_func_epilog(irg);
1270
1271         heights_free(heights);
1272 }
1273
1274 void sparc_init_emitter(void)
1275 {
1276         FIRM_DBG_REGISTER(dbg, "firm.be.sparc.emit");
1277 }