Debian Codegraph @

gcc/config/c6x/c6x.cc

raw
1 /* Target Code for TI C6X
2 Copyright (C) 2010-2025 Free Software Foundation, Inc.
3 Contributed by Andrew Jenner <[email protected]>
4 Contributed by Bernd Schmidt <[email protected]>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published
10 by the Free Software Foundation; either version 3, or (at your
11 option) any later version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #define IN_TARGET_CODE 1
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "gimple-expr.h"
32 #include "cfghooks.h"
33 #include "df.h"
34 #include "memmodel.h"
35 #include "tm_p.h"
36 #include "stringpool.h"
37 #include "attribs.h"
38 #include "optabs.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "recog.h"
42 #include "cgraph.h"
43 #include "diagnostic-core.h"
44 #include "stor-layout.h"
45 #include "varasm.h"
46 #include "calls.h"
47 #include "output.h"
48 #include "insn-attr.h"
49 #include "explow.h"
50 #include "expr.h"
51 #include "cfgrtl.h"
52 #include "sched-int.h"
53 #include "tm-constrs.h"
54 #include "langhooks.h"
55 #include "sel-sched.h"
56 #include "debug.h"
57 #include "hw-doloop.h"
58 #include "function-abi.h"
59 #include "regrename.h"
60 #include "dumpfile.h"
61 #include "builtins.h"
62 #include "flags.h"
63 #include "opts.h"
64
65 /* This file should be included last. */
66 #include "target-def.h"
67
68 /* Table of supported architecture variants. */
69 typedef struct
70 {
71 const char *arch;
72 enum c6x_cpu_type type;
73 unsigned short features;
74 } c6x_arch_table;
75
76 /* A list of all ISAs, mapping each one to a representative device.
77 Used for -march selection. */
78 static const c6x_arch_table all_isas[] =
79 {
80 #define C6X_ISA(NAME,DEVICE,FLAGS) \
81 { NAME, DEVICE, FLAGS },
82 #include "c6x-isas.def"
83 #undef C6X_ISA
84 { NULL, C6X_CPU_C62X, 0 }
85 };
86
87 /* This is the parsed result of the "-march=" option, if given. */
88 enum c6x_cpu_type c6x_arch = C6X_DEFAULT_ARCH;
89
90 /* A mask of insn types that are allowed by the architecture selected by
91 the -march option. */
92 unsigned long c6x_insn_mask = C6X_DEFAULT_INSN_MASK;
93
94 /* The instruction that is being output (as obtained from FINAL_PRESCAN_INSN).
95 */
96 static rtx_insn *c6x_current_insn = NULL;
97
98 /* A decl we build to access __c6xabi_DSBT_base. */
99 static GTY(()) tree dsbt_decl;
100
101 /* Determines whether we run our final scheduling pass or not. We always
102 avoid the normal second scheduling pass. */
103 static int c6x_flag_schedule_insns2;
104
105 /* Determines whether we run variable tracking in machine dependent
106 reorganization. */
107 static int c6x_flag_var_tracking;
108
109 /* Determines whether we use modulo scheduling. */
110 static int c6x_flag_modulo_sched;
111
112 /* Record the state of flag_pic before we set it to 1 for DSBT. */
113 int c6x_initial_flag_pic;
114
115 typedef struct
116 {
117 /* We record the clock cycle for every insn during scheduling. */
118 int clock;
119 /* After scheduling, we run assign_reservations to choose unit
120 reservations for all insns. These are recorded here. */
121 int reservation;
122 /* Records the new condition for insns which must be made
123 conditional after scheduling. An entry of NULL_RTX means no such
124 change is necessary. */
125 rtx new_cond;
126 /* True for the first insn that was scheduled in an ebb. */
127 bool ebb_start;
128 /* The scheduler state after the insn, transformed into a mask of UNIT_QID
129 bits rather than storing the state. Meaningful only for the last
130 insn in a cycle. */
131 unsigned int unit_mask;
132 } c6x_sched_insn_info;
133
134
135 /* Record a c6x_sched_insn_info structure for every insn in the function. */
136 static vec<c6x_sched_insn_info> insn_info;
137
138 #define INSN_INFO_LENGTH (insn_info).length ()
139 #define INSN_INFO_ENTRY(N) (insn_info[(N)])
140
141 static bool done_cfi_sections;
142
143 #define RESERVATION_FLAG_D 1
144 #define RESERVATION_FLAG_L 2
145 #define RESERVATION_FLAG_S 4
146 #define RESERVATION_FLAG_M 8
147 #define RESERVATION_FLAG_DL (RESERVATION_FLAG_D | RESERVATION_FLAG_L)
148 #define RESERVATION_FLAG_DS (RESERVATION_FLAG_D | RESERVATION_FLAG_S)
149 #define RESERVATION_FLAG_LS (RESERVATION_FLAG_L | RESERVATION_FLAG_S)
150 #define RESERVATION_FLAG_DLS (RESERVATION_FLAG_D | RESERVATION_FLAG_LS)
151
152 /* The DFA names of the units. */
153 static const char *const c6x_unit_names[] =
154 {
155 "d1", "l1", "s1", "m1", "fps1", "fpl1", "adddps1", "adddpl1",
156 "d2", "l2", "s2", "m2", "fps2", "fpl2", "adddps2", "adddpl2"
157 };
158
159 /* The DFA unit number for each unit in c6x_unit_names[]. */
160 static int c6x_unit_codes[ARRAY_SIZE (c6x_unit_names)];
161
162 /* Unit query IDs. */
163 #define UNIT_QID_D1 0
164 #define UNIT_QID_L1 1
165 #define UNIT_QID_S1 2
166 #define UNIT_QID_M1 3
167 #define UNIT_QID_FPS1 4
168 #define UNIT_QID_FPL1 5
169 #define UNIT_QID_ADDDPS1 6
170 #define UNIT_QID_ADDDPL1 7
171 #define UNIT_QID_SIDE_OFFSET 8
172
173 #define RESERVATION_S1 2
174 #define RESERVATION_S2 10
175
176 /* An enum for the unit requirements we count in the UNIT_REQS table. */
177 enum unitreqs
178 {
179 UNIT_REQ_D,
180 UNIT_REQ_L,
181 UNIT_REQ_S,
182 UNIT_REQ_M,
183 UNIT_REQ_DL,
184 UNIT_REQ_DS,
185 UNIT_REQ_LS,
186 UNIT_REQ_DLS,
187 UNIT_REQ_T,
188 UNIT_REQ_X,
189 UNIT_REQ_MAX
190 };
191
192 /* A table used to count unit requirements. Used when computing minimum
193 iteration intervals. */
194 typedef int unit_req_table[2][UNIT_REQ_MAX];
195 static unit_req_table unit_reqs;
196
197 /* Register map for debugging. */
198 unsigned const debugger_register_map[FIRST_PSEUDO_REGISTER] =
199 {
200 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, /* A0 - A15. */
201 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, /* A16 - A32. */
202 50, 51, 52,
203 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, /* B0 - B15. */
204 29, 30, 31,
205 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, /* B16 - B32. */
206 66, 67, 68,
207 -1, -1, -1 /* FP, ARGP, ILC. */
208 };
209
210 /* Allocate a new, cleared machine_function structure. */
211
212 static struct machine_function *
213 c6x_init_machine_status (void)
214 {
215 return ggc_cleared_alloc<machine_function> ();
216 }
217
218 /* Implement TARGET_OPTION_OVERRIDE. */
219
220 static void
221 c6x_option_override (void)
222 {
223 unsigned i;
224
225 if (OPTION_SET_P (c6x_arch_option))
226 {
227 c6x_arch = all_isas[c6x_arch_option].type;
228 c6x_insn_mask &= ~C6X_INSNS_ALL_CPU_BITS;
229 c6x_insn_mask |= all_isas[c6x_arch_option].features;
230 }
231
232 c6x_flag_schedule_insns2 = flag_schedule_insns_after_reload;
233 flag_schedule_insns_after_reload = 0;
234
235 c6x_flag_modulo_sched = flag_modulo_sched;
236 flag_modulo_sched = 0;
237
238 init_machine_status = c6x_init_machine_status;
239
240 for (i = 0; i < ARRAY_SIZE (c6x_unit_names); i++)
241 c6x_unit_codes[i] = get_cpu_unit_code (c6x_unit_names[i]);
242
243 if (flag_pic && !TARGET_DSBT)
244 {
245 error ("%<-fpic%> and %<-fPIC%> not supported without %<-mdsbt%> "
246 "on this target");
247 flag_pic = 0;
248 }
249 c6x_initial_flag_pic = flag_pic;
250 if (TARGET_DSBT && !flag_pic)
251 flag_pic = 1;
252 }
253
254
255 /* Implement the TARGET_CONDITIONAL_REGISTER_USAGE hook. */
256
257 static void
258 c6x_conditional_register_usage (void)
259 {
260 int i;
261 if (c6x_arch == C6X_CPU_C62X || c6x_arch == C6X_CPU_C67X)
262 for (i = 16; i < 32; i++)
263 {
264 fixed_regs[i] = 1;
265 fixed_regs[32 + i] = 1;
266 }
267 if (TARGET_INSNS_64)
268 {
269 SET_HARD_REG_BIT (reg_class_contents[(int)PREDICATE_A_REGS],
270 REG_A0);
271 SET_HARD_REG_BIT (reg_class_contents[(int)PREDICATE_REGS],
272 REG_A0);
273 CLEAR_HARD_REG_BIT (reg_class_contents[(int)NONPREDICATE_A_REGS],
274 REG_A0);
275 CLEAR_HARD_REG_BIT (reg_class_contents[(int)NONPREDICATE_REGS],
276 REG_A0);
277 }
278 }
279
280 static GTY(()) rtx eqdf_libfunc;
281 static GTY(()) rtx nedf_libfunc;
282 static GTY(()) rtx ledf_libfunc;
283 static GTY(()) rtx ltdf_libfunc;
284 static GTY(()) rtx gedf_libfunc;
285 static GTY(()) rtx gtdf_libfunc;
286 static GTY(()) rtx eqsf_libfunc;
287 static GTY(()) rtx nesf_libfunc;
288 static GTY(()) rtx lesf_libfunc;
289 static GTY(()) rtx ltsf_libfunc;
290 static GTY(()) rtx gesf_libfunc;
291 static GTY(()) rtx gtsf_libfunc;
292 static GTY(()) rtx strasgi_libfunc;
293 static GTY(()) rtx strasgi64p_libfunc;
294
295 /* Implement the TARGET_INIT_LIBFUNCS macro. We use this to rename library
296 functions to match the C6x ABI. */
297
298 static void
299 c6x_init_libfuncs (void)
300 {
301 /* Double-precision floating-point arithmetic. */
302 set_optab_libfunc (add_optab, DFmode, "__c6xabi_addd");
303 set_optab_libfunc (sdiv_optab, DFmode, "__c6xabi_divd");
304 set_optab_libfunc (smul_optab, DFmode, "__c6xabi_mpyd");
305 set_optab_libfunc (neg_optab, DFmode, "__c6xabi_negd");
306 set_optab_libfunc (sub_optab, DFmode, "__c6xabi_subd");
307
308 /* Single-precision floating-point arithmetic. */
309 set_optab_libfunc (add_optab, SFmode, "__c6xabi_addf");
310 set_optab_libfunc (sdiv_optab, SFmode, "__c6xabi_divf");
311 set_optab_libfunc (smul_optab, SFmode, "__c6xabi_mpyf");
312 set_optab_libfunc (neg_optab, SFmode, "__c6xabi_negf");
313 set_optab_libfunc (sub_optab, SFmode, "__c6xabi_subf");
314
315 /* Floating-point comparisons. */
316 eqsf_libfunc = init_one_libfunc ("__c6xabi_eqf");
317 nesf_libfunc = init_one_libfunc ("__c6xabi_neqf");
318 lesf_libfunc = init_one_libfunc ("__c6xabi_lef");
319 ltsf_libfunc = init_one_libfunc ("__c6xabi_ltf");
320 gesf_libfunc = init_one_libfunc ("__c6xabi_gef");
321 gtsf_libfunc = init_one_libfunc ("__c6xabi_gtf");
322 eqdf_libfunc = init_one_libfunc ("__c6xabi_eqd");
323 nedf_libfunc = init_one_libfunc ("__c6xabi_neqd");
324 ledf_libfunc = init_one_libfunc ("__c6xabi_led");
325 ltdf_libfunc = init_one_libfunc ("__c6xabi_ltd");
326 gedf_libfunc = init_one_libfunc ("__c6xabi_ged");
327 gtdf_libfunc = init_one_libfunc ("__c6xabi_gtd");
328
329 set_optab_libfunc (eq_optab, SFmode, NULL);
330 set_optab_libfunc (ne_optab, SFmode, "__c6xabi_neqf");
331 set_optab_libfunc (gt_optab, SFmode, NULL);
332 set_optab_libfunc (ge_optab, SFmode, NULL);
333 set_optab_libfunc (lt_optab, SFmode, NULL);
334 set_optab_libfunc (le_optab, SFmode, NULL);
335 set_optab_libfunc (unord_optab, SFmode, "__c6xabi_unordf");
336 set_optab_libfunc (eq_optab, DFmode, NULL);
337 set_optab_libfunc (ne_optab, DFmode, "__c6xabi_neqd");
338 set_optab_libfunc (gt_optab, DFmode, NULL);
339 set_optab_libfunc (ge_optab, DFmode, NULL);
340 set_optab_libfunc (lt_optab, DFmode, NULL);
341 set_optab_libfunc (le_optab, DFmode, NULL);
342 set_optab_libfunc (unord_optab, DFmode, "__c6xabi_unordd");
343
344 /* Floating-point to integer conversions. */
345 set_conv_libfunc (sfix_optab, SImode, DFmode, "__c6xabi_fixdi");
346 set_conv_libfunc (ufix_optab, SImode, DFmode, "__c6xabi_fixdu");
347 set_conv_libfunc (sfix_optab, DImode, DFmode, "__c6xabi_fixdlli");
348 set_conv_libfunc (ufix_optab, DImode, DFmode, "__c6xabi_fixdull");
349 set_conv_libfunc (sfix_optab, SImode, SFmode, "__c6xabi_fixfi");
350 set_conv_libfunc (ufix_optab, SImode, SFmode, "__c6xabi_fixfu");
351 set_conv_libfunc (sfix_optab, DImode, SFmode, "__c6xabi_fixflli");
352 set_conv_libfunc (ufix_optab, DImode, SFmode, "__c6xabi_fixfull");
353
354 /* Conversions between floating types. */
355 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__c6xabi_cvtdf");
356 set_conv_libfunc (sext_optab, DFmode, SFmode, "__c6xabi_cvtfd");
357
358 /* Integer to floating-point conversions. */
359 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__c6xabi_fltid");
360 set_conv_libfunc (ufloat_optab, DFmode, SImode, "__c6xabi_fltud");
361 set_conv_libfunc (sfloat_optab, DFmode, DImode, "__c6xabi_fltllid");
362 set_conv_libfunc (ufloat_optab, DFmode, DImode, "__c6xabi_fltulld");
363 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__c6xabi_fltif");
364 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__c6xabi_fltuf");
365 set_conv_libfunc (sfloat_optab, SFmode, DImode, "__c6xabi_fltllif");
366 set_conv_libfunc (ufloat_optab, SFmode, DImode, "__c6xabi_fltullf");
367
368 /* Long long. */
369 set_optab_libfunc (smul_optab, DImode, "__c6xabi_mpyll");
370 set_optab_libfunc (ashl_optab, DImode, "__c6xabi_llshl");
371 set_optab_libfunc (lshr_optab, DImode, "__c6xabi_llshru");
372 set_optab_libfunc (ashr_optab, DImode, "__c6xabi_llshr");
373
374 set_optab_libfunc (sdiv_optab, SImode, "__c6xabi_divi");
375 set_optab_libfunc (udiv_optab, SImode, "__c6xabi_divu");
376 set_optab_libfunc (smod_optab, SImode, "__c6xabi_remi");
377 set_optab_libfunc (umod_optab, SImode, "__c6xabi_remu");
378 set_optab_libfunc (sdivmod_optab, SImode, "__c6xabi_divremi");
379 set_optab_libfunc (udivmod_optab, SImode, "__c6xabi_divremu");
380 set_optab_libfunc (sdiv_optab, DImode, "__c6xabi_divlli");
381 set_optab_libfunc (udiv_optab, DImode, "__c6xabi_divull");
382 set_optab_libfunc (smod_optab, DImode, "__c6xabi_remlli");
383 set_optab_libfunc (umod_optab, DImode, "__c6xabi_remull");
384 set_optab_libfunc (udivmod_optab, DImode, "__c6xabi_divremull");
385
386 /* Block move. */
387 strasgi_libfunc = init_one_libfunc ("__c6xabi_strasgi");
388 strasgi64p_libfunc = init_one_libfunc ("__c6xabi_strasgi_64plus");
389 }
390
391 /* Begin the assembly file. */
392
393 static void
394 c6x_file_start (void)
395 {
396 /* Variable tracking should be run after all optimizations which change order
397 of insns. It also needs a valid CFG. This can't be done in
398 c6x_override_options, because flag_var_tracking is finalized after
399 that. */
400 c6x_flag_var_tracking = flag_var_tracking;
401 flag_var_tracking = 0;
402
403 done_cfi_sections = false;
404 default_file_start ();
405
406 /* Arrays are aligned to 8-byte boundaries. */
407 asm_fprintf (asm_out_file,
408 "\t.c6xabi_attribute Tag_ABI_array_object_alignment, 0\n");
409 asm_fprintf (asm_out_file,
410 "\t.c6xabi_attribute Tag_ABI_array_object_align_expected, 0\n");
411
412 /* Stack alignment is 8 bytes. */
413 asm_fprintf (asm_out_file,
414 "\t.c6xabi_attribute Tag_ABI_stack_align_needed, 0\n");
415 asm_fprintf (asm_out_file,
416 "\t.c6xabi_attribute Tag_ABI_stack_align_preserved, 0\n");
417
418 #if 0 /* FIXME: Reenable when TI's tools are fixed. */
419 /* ??? Ideally we'd check flag_short_wchar somehow. */
420 asm_fprintf (asm_out_file, "\t.c6xabi_attribute Tag_ABI_wchar_t, %d\n", 2);
421 #endif
422
423 /* We conform to version 1.0 of the ABI. */
424 asm_fprintf (asm_out_file,
425 "\t.c6xabi_attribute Tag_ABI_conformance, \"1.0\"\n");
426
427 }
428
429 /* The LTO frontend only enables exceptions when it sees a function that
430 uses it. This changes the return value of dwarf2out_do_frame, so we
431 have to check before every function. */
432
433 void
434 c6x_output_file_unwind (FILE * f)
435 {
436 if (done_cfi_sections)
437 return;
438
439 /* Output a .cfi_sections directive. */
440 if (dwarf2out_do_frame ())
441 {
442 if (flag_unwind_tables || flag_exceptions)
443 {
444 if (dwarf_debuginfo_p ())
445 asm_fprintf (f, "\t.cfi_sections .debug_frame, .c6xabi.exidx\n");
446 else
447 asm_fprintf (f, "\t.cfi_sections .c6xabi.exidx\n");
448 }
449 else
450 asm_fprintf (f, "\t.cfi_sections .debug_frame\n");
451 done_cfi_sections = true;
452 }
453 }
454
455 /* Output unwind directives at the end of a function. */
456
457 static void
458 c6x_output_fn_unwind (FILE * f)
459 {
460 /* Return immediately if we are not generating unwinding tables. */
461 if (! (flag_unwind_tables || flag_exceptions))
462 return;
463
464 /* If this function will never be unwound, then mark it as such. */
465 if (!(flag_unwind_tables || crtl->uses_eh_lsda)
466 && (TREE_NOTHROW (current_function_decl)
467 || crtl->all_throwers_are_sibcalls))
468 fputs("\t.cantunwind\n", f);
469
470 fputs ("\t.endp\n", f);
471 }
472
473
474 /* Stack and Calling. */
475
476 int argument_registers[10] =
477 {
478 REG_A4, REG_B4,
479 REG_A6, REG_B6,
480 REG_A8, REG_B8,
481 REG_A10, REG_B10,
482 REG_A12, REG_B12
483 };
484
485 /* Implements the macro INIT_CUMULATIVE_ARGS defined in c6x.h. */
486
487 void
488 c6x_init_cumulative_args (CUMULATIVE_ARGS *cum, const_tree fntype, rtx libname,
489 int n_named_args ATTRIBUTE_UNUSED)
490 {
491 cum->count = 0;
492 cum->nregs = 10;
493 if (!libname && fntype)
494 {
495 /* We need to find out the number of named arguments. Unfortunately,
496 for incoming arguments, N_NAMED_ARGS is set to -1. */
497 if (stdarg_p (fntype))
498 cum->nregs = type_num_arguments (fntype) - 1;
499 if (cum->nregs > 10)
500 cum->nregs = 10;
501 }
502 }
503
504 /* Implement TARGET_FUNCTION_ARG. */
505
506 static rtx
507 c6x_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
508 {
509 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
510 if (cum->count >= cum->nregs)
511 return NULL_RTX;
512 if (tree type = arg.type)
513 {
514 HOST_WIDE_INT size = int_size_in_bytes (type);
515 if (TARGET_BIG_ENDIAN && AGGREGATE_TYPE_P (type))
516 {
517 if (size > 4)
518 {
519 rtx reg1 = gen_rtx_REG (SImode, argument_registers[cum->count] + 1);
520 rtx reg2 = gen_rtx_REG (SImode, argument_registers[cum->count]);
521 rtvec vec = gen_rtvec (2, gen_rtx_EXPR_LIST (VOIDmode, reg1, const0_rtx),
522 gen_rtx_EXPR_LIST (VOIDmode, reg2, GEN_INT (4)));
523 return gen_rtx_PARALLEL (arg.mode, vec);
524 }
525 }
526 }
527 return gen_rtx_REG (arg.mode, argument_registers[cum->count]);
528 }
529
530 static void
531 c6x_function_arg_advance (cumulative_args_t cum_v, const function_arg_info &)
532 {
533 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
534 cum->count++;
535 }
536
537
538 /* Return true if BLOCK_REG_PADDING (MODE, TYPE, FIRST) should return
539 upward rather than downward. */
540
541 bool
542 c6x_block_reg_pad_upward (machine_mode mode ATTRIBUTE_UNUSED,
543 const_tree type, bool first)
544 {
545 HOST_WIDE_INT size;
546
547 if (!TARGET_BIG_ENDIAN)
548 return true;
549 if (!first)
550 return true;
551 if (!type)
552 return true;
553 size = int_size_in_bytes (type);
554 return size == 3;
555 }
556
557 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. */
558
559 static unsigned int
560 c6x_function_arg_boundary (machine_mode mode, const_tree type)
561 {
562 unsigned int boundary = type ? TYPE_ALIGN (type) : GET_MODE_BITSIZE (mode);
563
564 if (boundary > BITS_PER_WORD)
565 return 2 * BITS_PER_WORD;
566
567 if (mode == BLKmode)
568 {
569 HOST_WIDE_INT size = int_size_in_bytes (type);
570 if (size > 4)
571 return 2 * BITS_PER_WORD;
572 if (boundary < BITS_PER_WORD)
573 {
574 if (size >= 3)
575 return BITS_PER_WORD;
576 if (size >= 2)
577 return 2 * BITS_PER_UNIT;
578 }
579 }
580 return boundary;
581 }
582
583 /* Implement TARGET_FUNCTION_ARG_ROUND_BOUNDARY. */
584 static unsigned int
585 c6x_function_arg_round_boundary (machine_mode mode, const_tree type)
586 {
587 return c6x_function_arg_boundary (mode, type);
588 }
589
590 /* TARGET_FUNCTION_VALUE implementation. Returns an RTX representing the place
591 where function FUNC returns or receives a value of data type TYPE. */
592
593 static rtx
594 c6x_function_value (const_tree type, const_tree func ATTRIBUTE_UNUSED,
595 bool outgoing ATTRIBUTE_UNUSED)
596 {
597 /* Functions return values in register A4. When returning aggregates, we may
598 have to adjust for endianness. */
599 if (TARGET_BIG_ENDIAN && type && AGGREGATE_TYPE_P (type))
600 {
601 HOST_WIDE_INT size = int_size_in_bytes (type);
602 if (size > 4)
603 {
604
605 rtx reg1 = gen_rtx_REG (SImode, REG_A4 + 1);
606 rtx reg2 = gen_rtx_REG (SImode, REG_A4);
607 rtvec vec = gen_rtvec (2, gen_rtx_EXPR_LIST (VOIDmode, reg1, const0_rtx),
608 gen_rtx_EXPR_LIST (VOIDmode, reg2, GEN_INT (4)));
609 return gen_rtx_PARALLEL (TYPE_MODE (type), vec);
610 }
611 }
612 return gen_rtx_REG (TYPE_MODE (type), REG_A4);
613 }
614
615 /* Implement TARGET_LIBCALL_VALUE. */
616
617 static rtx
618 c6x_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
619 {
620 return gen_rtx_REG (mode, REG_A4);
621 }
622
623 /* TARGET_STRUCT_VALUE_RTX implementation. */
624
625 static rtx
626 c6x_struct_value_rtx (tree type ATTRIBUTE_UNUSED, int incoming ATTRIBUTE_UNUSED)
627 {
628 return gen_rtx_REG (Pmode, REG_A3);
629 }
630
631 /* Implement TARGET_FUNCTION_VALUE_REGNO_P. */
632
633 static bool
634 c6x_function_value_regno_p (const unsigned int regno)
635 {
636 return regno == REG_A4;
637 }
638
639 /* Types larger than 64 bit, and variable sized types, are passed by
640 reference. The callee must copy them; see TARGET_CALLEE_COPIES. */
641
642 static bool
643 c6x_pass_by_reference (cumulative_args_t, const function_arg_info &arg)
644 {
645 int size = -1;
646 if (arg.type)
647 size = int_size_in_bytes (arg.type);
648 else if (arg.mode != VOIDmode)
649 size = GET_MODE_SIZE (arg.mode);
650 return size > 2 * UNITS_PER_WORD || size == -1;
651 }
652
653 /* Decide whether a type should be returned in memory (true)
654 or in a register (false). This is called by the macro
655 TARGET_RETURN_IN_MEMORY. */
656
657 static bool
658 c6x_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
659 {
660 int size = int_size_in_bytes (type);
661 return size > 2 * UNITS_PER_WORD || size == -1;
662 }
663
664 /* Values which must be returned in the most-significant end of the return
665 register. */
666
667 static bool
668 c6x_return_in_msb (const_tree valtype)
669 {
670 HOST_WIDE_INT size = int_size_in_bytes (valtype);
671 return TARGET_BIG_ENDIAN && AGGREGATE_TYPE_P (valtype) && size == 3;
672 }
673
674 /* Return the type to use as __builtin_va_list. */
675 static tree
676 c6x_build_builtin_va_list (void)
677 {
678 return build_pointer_type (char_type_node);
679 }
680
681 static void
682 c6x_asm_trampoline_template (FILE *f)
683 {
684 fprintf (f, "\t.long\t0x0000002b\n"); /* mvkl .s2 fnlow,B0 */
685 fprintf (f, "\t.long\t0x01000028\n"); /* || mvkl .s1 sclow,A2 */
686 fprintf (f, "\t.long\t0x0000006b\n"); /* mvkh .s2 fnhigh,B0 */
687 fprintf (f, "\t.long\t0x01000068\n"); /* || mvkh .s1 schigh,A2 */
688 fprintf (f, "\t.long\t0x00000362\n"); /* b .s2 B0 */
689 fprintf (f, "\t.long\t0x00008000\n"); /* nop 5 */
690 fprintf (f, "\t.long\t0x00000000\n"); /* nop */
691 fprintf (f, "\t.long\t0x00000000\n"); /* nop */
692 }
693
694 /* Emit RTL insns to initialize the variable parts of a trampoline at
695 TRAMP. FNADDR is an RTX for the address of the function's pure
696 code. CXT is an RTX for the static chain value for the function. */
697
698 static void
699 c6x_initialize_trampoline (rtx tramp, tree fndecl, rtx cxt)
700 {
701 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
702 rtx t1 = copy_to_reg (fnaddr);
703 rtx t2 = copy_to_reg (cxt);
704 rtx mask = gen_reg_rtx (SImode);
705 int i;
706
707 emit_block_move (tramp, assemble_trampoline_template (),
708 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
709
710 emit_move_insn (mask, GEN_INT (0xffff << 7));
711
712 for (i = 0; i < 4; i++)
713 {
714 rtx mem = adjust_address (tramp, SImode, i * 4);
715 rtx t = (i & 1) ? t2 : t1;
716 rtx v1 = gen_reg_rtx (SImode);
717 rtx v2 = gen_reg_rtx (SImode);
718 emit_move_insn (v1, mem);
719 if (i < 2)
720 emit_insn (gen_ashlsi3 (v2, t, GEN_INT (7)));
721 else
722 emit_insn (gen_lshrsi3 (v2, t, GEN_INT (9)));
723 emit_insn (gen_andsi3 (v2, v2, mask));
724 emit_insn (gen_iorsi3 (v2, v2, v1));
725 emit_move_insn (mem, v2);
726 }
727 #ifdef CLEAR_INSN_CACHE
728 tramp = XEXP (tramp, 0);
729 maybe_emit_call_builtin___clear_cache (tramp,
730 plus_constant (Pmode,
731 tramp,
732 TRAMPOLINE_SIZE));
733 #endif
734 }
735
736 /* Determine whether c6x_output_mi_thunk can succeed. */
737
738 static bool
739 c6x_can_output_mi_thunk (const_tree thunk ATTRIBUTE_UNUSED,
740 HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
741 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
742 const_tree function ATTRIBUTE_UNUSED)
743 {
744 return !TARGET_LONG_CALLS;
745 }
746
747 /* Output the assembler code for a thunk function. THUNK is the
748 declaration for the thunk function itself, FUNCTION is the decl for
749 the target function. DELTA is an immediate constant offset to be
750 added to THIS. If VCALL_OFFSET is nonzero, the word at
751 *(*this + vcall_offset) should be added to THIS. */
752
753 static void
754 c6x_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
755 tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
756 HOST_WIDE_INT vcall_offset, tree function)
757 {
758 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk));
759 rtx xops[5];
760 /* The this parameter is passed as the first argument. */
761 rtx this_rtx = gen_rtx_REG (Pmode, REG_A4);
762
763 assemble_start_function (thunk, fnname);
764 c6x_current_insn = NULL;
765
766 xops[4] = XEXP (DECL_RTL (function), 0);
767 if (!vcall_offset)
768 {
769 output_asm_insn ("b .s2 \t%4", xops);
770 if (!delta)
771 output_asm_insn ("nop 5", xops);
772 }
773
774 /* Adjust the this parameter by a fixed constant. */
775 if (delta)
776 {
777 xops[0] = GEN_INT (delta);
778 xops[1] = this_rtx;
779 if (delta >= -16 && delta <= 15)
780 {
781 output_asm_insn ("add .s1 %0, %1, %1", xops);
782 if (!vcall_offset)
783 output_asm_insn ("nop 4", xops);
784 }
785 else if (delta >= 16 && delta < 32)
786 {
787 output_asm_insn ("add .d1 %0, %1, %1", xops);
788 if (!vcall_offset)
789 output_asm_insn ("nop 4", xops);
790 }
791 else if (delta >= -32768 && delta < 32768)
792 {
793 output_asm_insn ("mvk .s1 %0, A0", xops);
794 output_asm_insn ("add .d1 %1, A0, %1", xops);
795 if (!vcall_offset)
796 output_asm_insn ("nop 3", xops);
797 }
798 else
799 {
800 output_asm_insn ("mvkl .s1 %0, A0", xops);
801 output_asm_insn ("mvkh .s1 %0, A0", xops);
802 output_asm_insn ("add .d1 %1, A0, %1", xops);
803 if (!vcall_offset)
804 output_asm_insn ("nop 3", xops);
805 }
806 }
807
808 /* Adjust the this parameter by a value stored in the vtable. */
809 if (vcall_offset)
810 {
811 rtx a0tmp = gen_rtx_REG (Pmode, REG_A0);
812 rtx a3tmp = gen_rtx_REG (Pmode, REG_A3);
813
814 xops[1] = a3tmp;
815 xops[2] = a0tmp;
816 xops[3] = gen_rtx_MEM (Pmode, a0tmp);
817 output_asm_insn ("mv .s1 a4, %2", xops);
818 output_asm_insn ("ldw .d1t1 %3, %2", xops);
819
820 /* Adjust the this parameter. */
821 xops[0] = gen_rtx_MEM (Pmode, plus_constant (Pmode, a0tmp,
822 vcall_offset));
823 if (!memory_operand (xops[0], Pmode))
824 {
825 rtx tmp2 = gen_rtx_REG (Pmode, REG_A1);
826 xops[0] = GEN_INT (vcall_offset);
827 xops[1] = tmp2;
828 output_asm_insn ("mvkl .s1 %0, %1", xops);
829 output_asm_insn ("mvkh .s1 %0, %1", xops);
830 output_asm_insn ("nop 2", xops);
831 output_asm_insn ("add .d1 %2, %1, %2", xops);
832 xops[0] = gen_rtx_MEM (Pmode, a0tmp);
833 }
834 else
835 output_asm_insn ("nop 4", xops);
836 xops[2] = this_rtx;
837 output_asm_insn ("ldw .d1t1 %0, %1", xops);
838 output_asm_insn ("|| b .s2 \t%4", xops);
839 output_asm_insn ("nop 4", xops);
840 output_asm_insn ("add .d1 %2, %1, %2", xops);
841 }
842 assemble_end_function (thunk, fnname);
843 }
844
845 /* Return true if EXP goes in small data/bss. */
846
847 static bool
848 c6x_in_small_data_p (const_tree exp)
849 {
850 /* We want to merge strings, so we never consider them small data. */
851 if (TREE_CODE (exp) == STRING_CST)
852 return false;
853
854 /* Functions are never small data. */
855 if (TREE_CODE (exp) == FUNCTION_DECL)
856 return false;
857
858 if (VAR_P (exp) && DECL_WEAK (exp))
859 return false;
860
861 if (VAR_P (exp) && DECL_SECTION_NAME (exp))
862 {
863 const char *section = DECL_SECTION_NAME (exp);
864
865 if (strcmp (section, ".neardata") == 0
866 || startswith (section, ".neardata.")
867 || startswith (section, ".gnu.linkonce.s.")
868 || strcmp (section, ".bss") == 0
869 || startswith (section, ".bss.")
870 || startswith (section, ".gnu.linkonce.sb.")
871 || strcmp (section, ".rodata") == 0
872 || startswith (section, ".rodata.")
873 || startswith (section, ".gnu.linkonce.s2."))
874 return true;
875 }
876 else
877 return PLACE_IN_SDATA_P (exp);
878
879 return false;
880 }
881
882 /* Return a section for X. The only special thing we do here is to
883 honor small data. We don't have a tree type, so we can't use the
884 PLACE_IN_SDATA_P macro we use everywhere else; we choose to place
885 everything sized 8 bytes or smaller into small data. */
886
887 static section *
888 c6x_select_rtx_section (machine_mode mode, rtx x,
889 unsigned HOST_WIDE_INT align)
890 {
891 if (c6x_sdata_mode == C6X_SDATA_ALL
892 || (c6x_sdata_mode != C6X_SDATA_NONE && GET_MODE_SIZE (mode) <= 8))
893 /* ??? Consider using mergeable sdata sections. */
894 return sdata_section;
895 else
896 return default_elf_select_rtx_section (mode, x, align);
897 }
898
899 static section *
900 c6x_elf_select_section (tree decl, int reloc,
901 unsigned HOST_WIDE_INT align)
902 {
903 const char *sname = NULL;
904 unsigned int flags = SECTION_WRITE;
905 if (c6x_in_small_data_p (decl))
906 {
907 switch (categorize_decl_for_section (decl, reloc))
908 {
909 case SECCAT_SRODATA:
910 sname = ".rodata";
911 flags = 0;
912 break;
913 case SECCAT_SDATA:
914 sname = ".neardata";
915 break;
916 case SECCAT_SBSS:
917 sname = ".bss";
918 flags |= SECTION_BSS;
919 default:
920 break;
921 }
922 }
923 else
924 {
925 switch (categorize_decl_for_section (decl, reloc))
926 {
927 case SECCAT_DATA:
928 sname = ".fardata";
929 break;
930 case SECCAT_DATA_REL:
931 sname = ".fardata.rel";
932 break;
933 case SECCAT_DATA_REL_LOCAL:
934 sname = ".fardata.rel.local";
935 break;
936 case SECCAT_DATA_REL_RO:
937 sname = ".fardata.rel.ro";
938 break;
939 case SECCAT_DATA_REL_RO_LOCAL:
940 sname = ".fardata.rel.ro.local";
941 break;
942 case SECCAT_BSS:
943 sname = ".far";
944 flags |= SECTION_BSS;
945 break;
946 case SECCAT_RODATA:
947 sname = ".const";
948 flags = 0;
949 break;
950 case SECCAT_SRODATA:
951 case SECCAT_SDATA:
952 case SECCAT_SBSS:
953 gcc_unreachable ();
954 default:
955 break;
956 }
957 }
958 if (sname)
959 {
960 /* We might get called with string constants, but get_named_section
961 doesn't like them as they are not DECLs. Also, we need to set
962 flags in that case. */
963 if (!DECL_P (decl))
964 return get_section (sname, flags, NULL);
965 return get_named_section (decl, sname, reloc);
966 }
967
968 return default_elf_select_section (decl, reloc, align);
969 }
970
971 /* Build up a unique section name, expressed as a
972 STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
973 RELOC indicates whether the initial value of EXP requires
974 link-time relocations. */
975
976 static void ATTRIBUTE_UNUSED
977 c6x_elf_unique_section (tree decl, int reloc)
978 {
979 const char *prefix = NULL;
980 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
981 bool one_only = DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP;
982
983 if (c6x_in_small_data_p (decl))
984 {
985 switch (categorize_decl_for_section (decl, reloc))
986 {
987 case SECCAT_SDATA:
988 prefix = one_only ? ".s" : ".neardata";
989 break;
990 case SECCAT_SBSS:
991 prefix = one_only ? ".sb" : ".bss";
992 break;
993 case SECCAT_SRODATA:
994 prefix = one_only ? ".s2" : ".rodata";
995 break;
996 case SECCAT_RODATA_MERGE_STR:
997 case SECCAT_RODATA_MERGE_STR_INIT:
998 case SECCAT_RODATA_MERGE_CONST:
999 case SECCAT_RODATA:
1000 case SECCAT_DATA:
1001 case SECCAT_DATA_REL:
1002 case SECCAT_DATA_REL_LOCAL:
1003 case SECCAT_DATA_REL_RO:
1004 case SECCAT_DATA_REL_RO_LOCAL:
1005 gcc_unreachable ();
1006 default:
1007 /* Everything else we place into default sections and hope for the
1008 best. */
1009 break;
1010 }
1011 }
1012 else
1013 {
1014 switch (categorize_decl_for_section (decl, reloc))
1015 {
1016 case SECCAT_DATA:
1017 case SECCAT_DATA_REL:
1018 case SECCAT_DATA_REL_LOCAL:
1019 case SECCAT_DATA_REL_RO:
1020 case SECCAT_DATA_REL_RO_LOCAL:
1021 prefix = one_only ? ".fd" : ".fardata";
1022 break;
1023 case SECCAT_BSS:
1024 prefix = one_only ? ".fb" : ".far";
1025 break;
1026 case SECCAT_RODATA:
1027 case SECCAT_RODATA_MERGE_STR:
1028 case SECCAT_RODATA_MERGE_STR_INIT:
1029 case SECCAT_RODATA_MERGE_CONST:
1030 prefix = one_only ? ".fr" : ".const";
1031 break;
1032 case SECCAT_SRODATA:
1033 case SECCAT_SDATA:
1034 case SECCAT_SBSS:
1035 gcc_unreachable ();
1036 default:
1037 break;
1038 }
1039 }
1040
1041 if (prefix)
1042 {
1043 const char *name, *linkonce;
1044 char *string;
1045
1046 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1047 name = targetm.strip_name_encoding (name);
1048
1049 /* If we're using one_only, then there needs to be a .gnu.linkonce
1050 prefix to the section name. */
1051 linkonce = one_only ? ".gnu.linkonce" : "";
1052
1053 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
1054
1055 set_decl_section_name (decl, string);
1056 return;
1057 }
1058 default_unique_section (decl, reloc);
1059 }
1060
1061 static unsigned int
1062 c6x_section_type_flags (tree decl, const char *name, int reloc)
1063 {
1064 unsigned int flags = 0;
1065
1066 if (strcmp (name, ".far") == 0
1067 || startswith (name, ".far."))
1068 flags |= SECTION_BSS;
1069
1070 flags |= default_section_type_flags (decl, name, reloc);
1071
1072 /* The ".far" section will be declared with @nobits elsewhere.
1073 But when declared via this path it will not have the @nobits
1074 flag because of SECTION_NOTYPE. This causes linker warnings
1075 due to the mismatched attribute. Clearing SECTION_NOTYPE
1076 for the ".far" section is sufficient to fix this problem. */
1077 if (strcmp (name, ".far") == 0)
1078 flags &= ~SECTION_NOTYPE;
1079
1080 return flags;
1081 }
1082
1083 /* Checks whether the given CALL_EXPR would use a caller saved
1084 register. This is used to decide whether sibling call optimization
1085 could be performed on the respective function call. */
1086
1087 static bool
1088 c6x_call_saved_register_used (tree call_expr)
1089 {
1090 CUMULATIVE_ARGS cum_v;
1091 cumulative_args_t cum;
1092 HARD_REG_SET call_saved_regset;
1093 tree parameter;
1094 rtx parm_rtx;
1095 int i;
1096
1097 INIT_CUMULATIVE_ARGS (cum_v, NULL, NULL, 0, 0);
1098 cum = pack_cumulative_args (&cum_v);
1099
1100 call_saved_regset = ~call_used_or_fixed_regs;
1101 for (i = 0; i < call_expr_nargs (call_expr); i++)
1102 {
1103 parameter = CALL_EXPR_ARG (call_expr, i);
1104 gcc_assert (parameter);
1105
1106 /* For an undeclared variable passed as parameter we will get
1107 an ERROR_MARK node here. */
1108 if (TREE_CODE (parameter) == ERROR_MARK)
1109 return true;
1110
1111 function_arg_info arg (TREE_TYPE (parameter), /*named=*/true);
1112 apply_pass_by_reference_rules (&cum_v, arg);
1113
1114 parm_rtx = c6x_function_arg (cum, arg);
1115
1116 c6x_function_arg_advance (cum, arg);
1117
1118 if (!parm_rtx)
1119 continue;
1120
1121 if (REG_P (parm_rtx)
1122 && overlaps_hard_reg_set_p (call_saved_regset, GET_MODE (parm_rtx),
1123 REGNO (parm_rtx)))
1124 return true;
1125 if (GET_CODE (parm_rtx) == PARALLEL)
1126 {
1127 int n = XVECLEN (parm_rtx, 0);
1128 while (n-- > 0)
1129 {
1130 rtx x = XEXP (XVECEXP (parm_rtx, 0, n), 0);
1131 if (REG_P (x)
1132 && overlaps_hard_reg_set_p (call_saved_regset,
1133 GET_MODE (x), REGNO (x)))
1134 return true;
1135 }
1136 }
1137 }
1138 return false;
1139 }
1140
1141 /* Decide whether we can make a sibling call to a function. DECL is the
1142 declaration of the function being targeted by the call and EXP is the
1143 CALL_EXPR representing the call. */
1144
1145 static bool
1146 c6x_function_ok_for_sibcall (tree decl, tree exp)
1147 {
1148 /* Registers A10, A12, B10 and B12 are available as arguments
1149 register but unfortunately caller saved. This makes functions
1150 needing these registers for arguments not suitable for
1151 sibcalls. */
1152 if (c6x_call_saved_register_used (exp))
1153 return false;
1154
1155 if (!flag_pic)
1156 return true;
1157
1158 if (TARGET_DSBT)
1159 {
1160 /* When compiling for DSBT, the calling function must be local,
1161 so that when we reload B14 in the sibcall epilogue, it will
1162 not change its value. */
1163
1164 if (!decl)
1165 /* Not enough information. */
1166 return false;
1167
1168 cgraph_node *this_func
1169 = cgraph_node::local_info_node (current_function_decl);
1170 return this_func->local;
1171 }
1172
1173 return true;
1174 }
1175
1176 /* Return true if DECL is known to be linked into section SECTION. */
1177
1178 static bool
1179 c6x_function_in_section_p (tree decl, section *section)
1180 {
1181 /* We can only be certain about functions defined in the same
1182 compilation unit. */
1183 if (!TREE_STATIC (decl))
1184 return false;
1185
1186 /* Make sure that SYMBOL always binds to the definition in this
1187 compilation unit. */
1188 if (!targetm.binds_local_p (decl))
1189 return false;
1190
1191 /* If DECL_SECTION_NAME is set, assume it is trustworthy. */
1192 if (!DECL_SECTION_NAME (decl))
1193 {
1194 /* Make sure that we will not create a unique section for DECL. */
1195 if (flag_function_sections || DECL_COMDAT_GROUP (decl))
1196 return false;
1197 }
1198
1199 return function_section (decl) == section;
1200 }
1201
1202 /* Return true if a call to OP, which is a SYMBOL_REF, must be expanded
1203 as a long call. */
1204 bool
1205 c6x_long_call_p (rtx op)
1206 {
1207 tree decl;
1208
1209 if (!TARGET_LONG_CALLS)
1210 return false;
1211
1212 decl = SYMBOL_REF_DECL (op);
1213
1214 /* Try to determine whether the symbol is in the same section as the current
1215 function. Be conservative, and only cater for cases in which the
1216 whole of the current function is placed in the same section. */
1217 if (decl != NULL_TREE
1218 && !flag_reorder_blocks_and_partition
1219 && TREE_CODE (decl) == FUNCTION_DECL
1220 && c6x_function_in_section_p (decl, current_function_section ()))
1221 return false;
1222
1223 return true;
1224 }
1225
1226 /* Emit the sequence for a call. */
1227 void
1228 c6x_expand_call (rtx retval, rtx address, bool sibcall)
1229 {
1230 rtx callee = XEXP (address, 0);
1231 rtx call_insn;
1232
1233 if (!c6x_call_operand (callee, Pmode))
1234 {
1235 callee = force_reg (Pmode, callee);
1236 address = change_address (address, Pmode, callee);
1237 }
1238 call_insn = gen_rtx_CALL (VOIDmode, address, const0_rtx);
1239 if (sibcall)
1240 {
1241 call_insn = emit_call_insn (call_insn);
1242 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn),
1243 gen_rtx_REG (Pmode, REG_B3));
1244 }
1245 else
1246 {
1247 if (retval == NULL_RTX)
1248 call_insn = emit_call_insn (call_insn);
1249 else
1250 call_insn = emit_call_insn (gen_rtx_SET (retval, call_insn));
1251 }
1252 if (flag_pic)
1253 use_reg (&CALL_INSN_FUNCTION_USAGE (call_insn), pic_offset_table_rtx);
1254 }
1255
1256 /* Legitimize PIC addresses. If the address is already position-independent,
1257 we return ORIG. Newly generated position-independent addresses go into a
1258 reg. This is REG if nonzero, otherwise we allocate register(s) as
1259 necessary. PICREG is the register holding the pointer to the PIC offset
1260 table. */
1261
1262 static rtx
1263 legitimize_pic_address (rtx orig, rtx reg, rtx picreg)
1264 {
1265 rtx addr = orig;
1266 rtx new_rtx = orig;
1267
1268 if (GET_CODE (addr) == SYMBOL_REF || GET_CODE (addr) == LABEL_REF)
1269 {
1270 int unspec = UNSPEC_LOAD_GOT;
1271 rtx tmp;
1272
1273 if (reg == 0)
1274 {
1275 gcc_assert (can_create_pseudo_p ());
1276 reg = gen_reg_rtx (Pmode);
1277 }
1278 if (flag_pic == 2)
1279 {
1280 if (can_create_pseudo_p ())
1281 tmp = gen_reg_rtx (Pmode);
1282 else
1283 tmp = reg;
1284 emit_insn (gen_movsi_gotoff_high (tmp, addr));
1285 emit_insn (gen_movsi_gotoff_lo_sum (tmp, tmp, addr));
1286 emit_insn (gen_load_got_gotoff (reg, picreg, tmp));
1287 }
1288 else
1289 {
1290 tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), unspec);
1291 new_rtx = gen_const_mem (Pmode, gen_rtx_PLUS (Pmode, picreg, tmp));
1292
1293 emit_move_insn (reg, new_rtx);
1294 }
1295 if (picreg == pic_offset_table_rtx)
1296 crtl->uses_pic_offset_table = 1;
1297 return reg;
1298 }
1299
1300 else if (GET_CODE (addr) == CONST || GET_CODE (addr) == PLUS)
1301 {
1302 rtx base;
1303
1304 if (GET_CODE (addr) == CONST)
1305 {
1306 addr = XEXP (addr, 0);
1307 gcc_assert (GET_CODE (addr) == PLUS);
1308 }
1309
1310 if (XEXP (addr, 0) == picreg)
1311 return orig;
1312
1313 if (reg == 0)
1314 {
1315 gcc_assert (can_create_pseudo_p ());
1316 reg = gen_reg_rtx (Pmode);
1317 }
1318
1319 base = legitimize_pic_address (XEXP (addr, 0), reg, picreg);
1320 addr = legitimize_pic_address (XEXP (addr, 1),
1321 base == reg ? NULL_RTX : reg,
1322 picreg);
1323
1324 if (GET_CODE (addr) == CONST_INT)
1325 {
1326 gcc_assert (! reload_in_progress && ! reload_completed);
1327 addr = force_reg (Pmode, addr);
1328 }
1329
1330 if (GET_CODE (addr) == PLUS && CONSTANT_P (XEXP (addr, 1)))
1331 {
1332 base = gen_rtx_PLUS (Pmode, base, XEXP (addr, 0));
1333 addr = XEXP (addr, 1);
1334 }
1335
1336 return gen_rtx_PLUS (Pmode, base, addr);
1337 }
1338
1339 return new_rtx;
1340 }
1341
1342 /* Expand a move operation in mode MODE. The operands are in OPERANDS.
1343 Returns true if no further code must be generated, false if the caller
1344 should generate an insn to move OPERANDS[1] to OPERANDS[0]. */
1345
1346 bool
1347 expand_move (rtx *operands, machine_mode mode)
1348 {
1349 rtx dest = operands[0];
1350 rtx op = operands[1];
1351
1352 if ((reload_in_progress | reload_completed) == 0
1353 && GET_CODE (dest) == MEM && GET_CODE (op) != REG)
1354 operands[1] = force_reg (mode, op);
1355 else if (mode == SImode && symbolic_operand (op, SImode))
1356 {
1357 if (flag_pic)
1358 {
1359 if (sdata_symbolic_operand (op, SImode))
1360 {
1361 emit_insn (gen_load_sdata_pic (dest, pic_offset_table_rtx, op));
1362 crtl->uses_pic_offset_table = 1;
1363 return true;
1364 }
1365 else
1366 {
1367 rtx temp = (reload_completed || reload_in_progress
1368 ? dest : gen_reg_rtx (Pmode));
1369
1370 operands[1] = legitimize_pic_address (op, temp,
1371 pic_offset_table_rtx);
1372 }
1373 }
1374 else if (reload_completed
1375 && !sdata_symbolic_operand (op, SImode))
1376 {
1377 emit_insn (gen_movsi_high (dest, op));
1378 emit_insn (gen_movsi_lo_sum (dest, dest, op));
1379 return true;
1380 }
1381 }
1382 return false;
1383 }
1384
1385 /* This function is called when we're about to expand an integer compare
1386 operation which performs COMPARISON. It examines the second operand,
1387 and if it is an integer constant that cannot be used directly on the
1388 current machine in a comparison insn, it returns true. */
1389 bool
1390 c6x_force_op_for_comparison_p (enum rtx_code code, rtx op)
1391 {
1392 if (!CONST_INT_P (op) || satisfies_constraint_Iu4 (op))
1393 return false;
1394
1395 if ((code == EQ || code == LT || code == GT)
1396 && !satisfies_constraint_Is5 (op))
1397 return true;
1398 if ((code == GTU || code == LTU)
1399 && (!TARGET_INSNS_64 || !satisfies_constraint_Iu5 (op)))
1400 return true;
1401
1402 return false;
1403 }
1404
1405 /* Emit comparison instruction if necessary, returning the expression
1406 that holds the compare result in the proper mode. Return the comparison
1407 that should be used in the jump insn. */
1408
1409 rtx
1410 c6x_expand_compare (rtx comparison, machine_mode mode)
1411 {
1412 enum rtx_code code = GET_CODE (comparison);
1413 rtx op0 = XEXP (comparison, 0);
1414 rtx op1 = XEXP (comparison, 1);
1415 rtx cmp;
1416 enum rtx_code jump_code = code;
1417 machine_mode op_mode = GET_MODE (op0);
1418
1419 if (op_mode == DImode && (code == NE || code == EQ) && op1 == const0_rtx)
1420 {
1421 rtx t = gen_reg_rtx (SImode);
1422 emit_insn (gen_iorsi3 (t, gen_lowpart (SImode, op0),
1423 gen_highpart (SImode, op0)));
1424 op_mode = SImode;
1425 cmp = t;
1426 }
1427 else if (op_mode == DImode)
1428 {
1429 rtx lo[2], high[2];
1430 rtx cmp1, cmp2;
1431
1432 if (code == NE || code == GEU || code == LEU || code == GE || code == LE)
1433 {
1434 code = reverse_condition (code);
1435 jump_code = EQ;
1436 }
1437 else
1438 jump_code = NE;
1439
1440 split_di (&op0, 1, lo, high);
1441 split_di (&op1, 1, lo + 1, high + 1);
1442
1443 if (c6x_force_op_for_comparison_p (code, high[1])
1444 || c6x_force_op_for_comparison_p (EQ, high[1]))
1445 high[1] = force_reg (SImode, high[1]);
1446
1447 cmp1 = gen_reg_rtx (SImode);
1448 cmp2 = gen_reg_rtx (SImode);
1449 emit_insn (gen_rtx_SET (cmp1, gen_rtx_fmt_ee (code, SImode,
1450 high[0], high[1])));
1451 if (code == EQ)
1452 {
1453 if (c6x_force_op_for_comparison_p (code, lo[1]))
1454 lo[1] = force_reg (SImode, lo[1]);
1455 emit_insn (gen_rtx_SET (cmp2, gen_rtx_fmt_ee (code, SImode,
1456 lo[0], lo[1])));
1457 emit_insn (gen_andsi3 (cmp1, cmp1, cmp2));
1458 }
1459 else
1460 {
1461 emit_insn (gen_rtx_SET (cmp2, gen_rtx_EQ (SImode, high[0],
1462 high[1])));
1463 if (code == GT)
1464 code = GTU;
1465 else if (code == LT)
1466 code = LTU;
1467 if (c6x_force_op_for_comparison_p (code, lo[1]))
1468 lo[1] = force_reg (SImode, lo[1]);
1469 emit_insn (gen_cmpsi_and (cmp2, gen_rtx_fmt_ee (code, SImode,
1470 lo[0], lo[1]),
1471 lo[0], lo[1], cmp2));
1472 emit_insn (gen_iorsi3 (cmp1, cmp1, cmp2));
1473 }
1474 cmp = cmp1;
1475 }
1476 else if (TARGET_FP && !flag_finite_math_only
1477 && (op_mode == DFmode || op_mode == SFmode)
1478 && code != EQ && code != NE && code != LT && code != GT
1479 && code != UNLE && code != UNGE)
1480 {
1481 enum rtx_code code1, code2, code3;
1482 rtx (*fn) (rtx, rtx, rtx, rtx, rtx);
1483
1484 jump_code = NE;
1485 code3 = UNKNOWN;
1486 switch (code)
1487 {
1488 case UNLT:
1489 case UNGT:
1490 jump_code = EQ;
1491 /* fall through */
1492 case LE:
1493 case GE:
1494 code1 = code == LE || code == UNGT ? LT : GT;
1495 code2 = EQ;
1496 break;
1497
1498 case UNORDERED:
1499 jump_code = EQ;
1500 /* fall through */
1501 case ORDERED:
1502 code3 = EQ;
1503 /* fall through */
1504 case LTGT:
1505 code1 = LT;
1506 code2 = GT;
1507 break;
1508
1509 case UNEQ:
1510 code1 = LT;
1511 code2 = GT;
1512 jump_code = EQ;
1513 break;
1514
1515 default:
1516 gcc_unreachable ();
1517 }
1518
1519 cmp = gen_reg_rtx (SImode);
1520 emit_insn (gen_rtx_SET (cmp, gen_rtx_fmt_ee (code1, SImode, op0, op1)));
1521 fn = op_mode == DFmode ? gen_cmpdf_ior : gen_cmpsf_ior;
1522 emit_insn (fn (cmp, gen_rtx_fmt_ee (code2, SImode, op0, op1),
1523 op0, op1, cmp));
1524 if (code3 != UNKNOWN)
1525 emit_insn (fn (cmp, gen_rtx_fmt_ee (code3, SImode, op0, op1),
1526 op0, op1, cmp));
1527 }
1528 else if (op_mode == SImode && (code == NE || code == EQ) && op1 == const0_rtx)
1529 cmp = op0;
1530 else
1531 {
1532 bool is_fp_libfunc;
1533 is_fp_libfunc = !TARGET_FP && (op_mode == DFmode || op_mode == SFmode);
1534
1535 if ((code == NE || code == GEU || code == LEU || code == GE || code == LE)
1536 && !is_fp_libfunc)
1537 {
1538 code = reverse_condition (code);
1539 jump_code = EQ;
1540 }
1541 else if (code == UNGE)
1542 {
1543 code = LT;
1544 jump_code = EQ;
1545 }
1546 else if (code == UNLE)
1547 {
1548 code = GT;
1549 jump_code = EQ;
1550 }
1551 else
1552 jump_code = NE;
1553
1554 if (is_fp_libfunc)
1555 {
1556 rtx_insn *insns;
1557 rtx libfunc;
1558 switch (code)
1559 {
1560 case EQ:
1561 libfunc = op_mode == DFmode ? eqdf_libfunc : eqsf_libfunc;
1562 break;
1563 case NE:
1564 libfunc = op_mode == DFmode ? nedf_libfunc : nesf_libfunc;
1565 break;
1566 case GT:
1567 libfunc = op_mode == DFmode ? gtdf_libfunc : gtsf_libfunc;
1568 break;
1569 case GE:
1570 libfunc = op_mode == DFmode ? gedf_libfunc : gesf_libfunc;
1571 break;
1572 case LT:
1573 libfunc = op_mode == DFmode ? ltdf_libfunc : ltsf_libfunc;
1574 break;
1575 case LE:
1576 libfunc = op_mode == DFmode ? ledf_libfunc : lesf_libfunc;
1577 break;
1578 default:
1579 gcc_unreachable ();
1580 }
1581 start_sequence ();
1582
1583 cmp = emit_library_call_value (libfunc, 0, LCT_CONST, SImode,
1584 op0, op_mode, op1, op_mode);
1585 insns = get_insns ();
1586 end_sequence ();
1587
1588 emit_libcall_block (insns, cmp, cmp,
1589 gen_rtx_fmt_ee (code, SImode, op0, op1));
1590 }
1591 else
1592 {
1593 cmp = gen_reg_rtx (SImode);
1594 if (c6x_force_op_for_comparison_p (code, op1))
1595 op1 = force_reg (SImode, op1);
1596 emit_insn (gen_rtx_SET (cmp, gen_rtx_fmt_ee (code, SImode,
1597 op0, op1)));
1598 }
1599 }
1600
1601 return gen_rtx_fmt_ee (jump_code, mode, cmp, const0_rtx);
1602 }
1603
1604 /* Return one word of double-word value OP. HIGH_P is true to select the
1605 high part, false to select the low part. When encountering auto-increment
1606 addressing, we make the assumption that the low part is going to be accessed
1607 first. */
1608
1609 rtx
1610 c6x_subword (rtx op, bool high_p)
1611 {
1612 unsigned int byte;
1613 machine_mode mode;
1614
1615 mode = GET_MODE (op);
1616 if (mode == VOIDmode)
1617 mode = DImode;
1618
1619 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
1620 byte = UNITS_PER_WORD;
1621 else
1622 byte = 0;
1623
1624 if (MEM_P (op))
1625 {
1626 rtx addr = XEXP (op, 0);
1627 if (GET_CODE (addr) == PLUS || REG_P (addr))
1628 return adjust_address (op, word_mode, byte);
1629 /* FIXME: should really support autoincrement addressing for
1630 multi-word modes. */
1631 gcc_unreachable ();
1632 }
1633
1634 return simplify_gen_subreg (word_mode, op, mode, byte);
1635 }
1636
1637 /* Split one or more DImode RTL references into pairs of SImode
1638 references. The RTL can be REG, offsettable MEM, integer constant, or
1639 CONST_DOUBLE. "operands" is a pointer to an array of DImode RTL to
1640 split and "num" is its length. lo_half and hi_half are output arrays
1641 that parallel "operands". */
1642
1643 void
1644 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
1645 {
1646 while (num--)
1647 {
1648 rtx op = operands[num];
1649
1650 lo_half[num] = c6x_subword (op, false);
1651 hi_half[num] = c6x_subword (op, true);
1652 }
1653 }
1654
1655 /* Return true if VAL is a mask valid for a clr instruction. */
1656 bool
1657 c6x_valid_mask_p (HOST_WIDE_INT val)
1658 {
1659 int i;
1660 for (i = 0; i < 32; i++)
1661 if (!(val & ((unsigned HOST_WIDE_INT)1 << i)))
1662 break;
1663 for (; i < 32; i++)
1664 if (val & ((unsigned HOST_WIDE_INT)1 << i))
1665 break;
1666 for (; i < 32; i++)
1667 if (!(val & ((unsigned HOST_WIDE_INT)1 << i)))
1668 return false;
1669 return true;
1670 }
1671
1672 /* Expand a block move for a cpymemM pattern. */
1673
1674 bool
1675 c6x_expand_cpymem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
1676 rtx expected_align_exp ATTRIBUTE_UNUSED,
1677 rtx expected_size_exp ATTRIBUTE_UNUSED)
1678 {
1679 unsigned HOST_WIDE_INT align = 1;
1680 unsigned HOST_WIDE_INT src_mem_align, dst_mem_align, min_mem_align;
1681 unsigned HOST_WIDE_INT count = 0, offset = 0;
1682 unsigned int biggest_move = TARGET_STDW ? 8 : 4;
1683
1684 if (CONST_INT_P (align_exp))
1685 align = INTVAL (align_exp);
1686
1687 src_mem_align = MEM_ALIGN (src) / BITS_PER_UNIT;
1688 dst_mem_align = MEM_ALIGN (dst) / BITS_PER_UNIT;
1689 min_mem_align = MIN (src_mem_align, dst_mem_align);
1690
1691 if (min_mem_align > align)
1692 align = min_mem_align / BITS_PER_UNIT;
1693 if (src_mem_align < align)
1694 src_mem_align = align;
1695 if (dst_mem_align < align)
1696 dst_mem_align = align;
1697
1698 if (CONST_INT_P (count_exp))
1699 count = INTVAL (count_exp);
1700 else
1701 return false;
1702
1703 /* Make sure we don't need to care about overflow later on. */
1704 if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
1705 return false;
1706
1707 if (count >= 28 && (count & 3) == 0 && align >= 4)
1708 {
1709 tree dst_expr = MEM_EXPR (dst);
1710 tree src_expr = MEM_EXPR (src);
1711 rtx fn = TARGET_INSNS_64PLUS ? strasgi64p_libfunc : strasgi_libfunc;
1712 rtx srcreg = force_reg (Pmode, XEXP (src, 0));
1713 rtx dstreg = force_reg (Pmode, XEXP (dst, 0));
1714
1715 if (src_expr)
1716 mark_addressable (src_expr);
1717 if (dst_expr)
1718 mark_addressable (dst_expr);
1719 emit_library_call (fn, LCT_NORMAL, VOIDmode,
1720 dstreg, Pmode, srcreg, Pmode, count_exp, SImode);
1721 return true;
1722 }
1723
1724 if (biggest_move > align && !TARGET_INSNS_64)
1725 biggest_move = align;
1726
1727 if (count / biggest_move > 7)
1728 return false;
1729
1730 while (count > 0)
1731 {
1732 rtx reg, reg_lowpart;
1733 machine_mode srcmode, dstmode;
1734 unsigned HOST_WIDE_INT src_size, dst_size, src_left;
1735 int shift;
1736 rtx srcmem, dstmem;
1737
1738 while (biggest_move > count)
1739 biggest_move /= 2;
1740
1741 src_size = dst_size = biggest_move;
1742 if (src_size > src_mem_align && src_size == 2)
1743 src_size = 1;
1744 if (dst_size > dst_mem_align && dst_size == 2)
1745 dst_size = 1;
1746
1747 if (dst_size > src_size)
1748 dst_size = src_size;
1749
1750 srcmode = int_mode_for_size (src_size * BITS_PER_UNIT, 0).require ();
1751 dstmode = int_mode_for_size (dst_size * BITS_PER_UNIT, 0).require ();
1752 if (src_size >= 4)
1753 reg_lowpart = reg = gen_reg_rtx (srcmode);
1754 else
1755 {
1756 reg = gen_reg_rtx (SImode);
1757 reg_lowpart = gen_lowpart (srcmode, reg);
1758 }
1759
1760 srcmem = adjust_address (copy_rtx (src), srcmode, offset);
1761
1762 if (src_size > src_mem_align)
1763 {
1764 enum insn_code icode = (srcmode == SImode ? CODE_FOR_movmisalignsi
1765 : CODE_FOR_movmisaligndi);
1766 emit_insn (GEN_FCN (icode) (reg_lowpart, srcmem));
1767 }
1768 else
1769 emit_move_insn (reg_lowpart, srcmem);
1770
1771 src_left = src_size;
1772 shift = TARGET_BIG_ENDIAN ? (src_size - dst_size) * BITS_PER_UNIT : 0;
1773 while (src_left > 0)
1774 {
1775 rtx dstreg = reg_lowpart;
1776
1777 if (src_size > dst_size)
1778 {
1779 rtx srcword = reg;
1780 int shift_amount = shift & (BITS_PER_WORD - 1);
1781 if (src_size > 4)
1782 srcword = operand_subword_force (srcword, src_left >= 4 ? 0 : 4,
1783 SImode);
1784 if (shift_amount > 0)
1785 {
1786 dstreg = gen_reg_rtx (SImode);
1787 emit_insn (gen_lshrsi3 (dstreg, srcword,
1788 GEN_INT (shift_amount)));
1789 }
1790 else
1791 dstreg = srcword;
1792 dstreg = gen_lowpart (dstmode, dstreg);
1793 }
1794
1795 dstmem = adjust_address (copy_rtx (dst), dstmode, offset);
1796 if (dst_size > dst_mem_align)
1797 {
1798 enum insn_code icode = (dstmode == SImode ? CODE_FOR_movmisalignsi
1799 : CODE_FOR_movmisaligndi);
1800 emit_insn (GEN_FCN (icode) (dstmem, dstreg));
1801 }
1802 else
1803 emit_move_insn (dstmem, dstreg);
1804
1805 if (TARGET_BIG_ENDIAN)
1806 shift -= dst_size * BITS_PER_UNIT;
1807 else
1808 shift += dst_size * BITS_PER_UNIT;
1809 offset += dst_size;
1810 src_left -= dst_size;
1811 }
1812 count -= src_size;
1813 }
1814 return true;
1815 }
1816
1817 /* Subroutine of print_address_operand, print a single address offset OFF for
1818 a memory access of mode MEM_MODE, choosing between normal form and scaled
1819 form depending on the type of the insn. Misaligned memory references must
1820 use the scaled form. */
1821
1822 static void
1823 print_address_offset (FILE *file, rtx off, machine_mode mem_mode)
1824 {
1825 rtx pat;
1826
1827 if (c6x_current_insn != NULL_RTX)
1828 {
1829 pat = PATTERN (c6x_current_insn);
1830 if (GET_CODE (pat) == COND_EXEC)
1831 pat = COND_EXEC_CODE (pat);
1832 if (GET_CODE (pat) == PARALLEL)
1833 pat = XVECEXP (pat, 0, 0);
1834
1835 if (GET_CODE (pat) == SET
1836 && GET_CODE (SET_SRC (pat)) == UNSPEC
1837 && XINT (SET_SRC (pat), 1) == UNSPEC_MISALIGNED_ACCESS)
1838 {
1839 gcc_assert (CONST_INT_P (off)
1840 && (INTVAL (off) & (GET_MODE_SIZE (mem_mode) - 1)) == 0);
1841 fprintf (file, "[" HOST_WIDE_INT_PRINT_DEC "]",
1842 INTVAL (off) / GET_MODE_SIZE (mem_mode));
1843 return;
1844 }
1845 }
1846 fputs ("(", file);
1847 output_address (mem_mode, off);
1848 fputs (")", file);
1849 }
1850
1851 static bool
1852 c6x_print_operand_punct_valid_p (unsigned char c)
1853 {
1854 return c == '$' || c == '.' || c == '|';
1855 }
1856
1857 static void c6x_print_operand (FILE *, rtx, int);
1858
1859 /* Subroutine of c6x_print_operand; used to print a memory reference X to FILE. */
1860
1861 static void
1862 c6x_print_address_operand (FILE *file, rtx x, machine_mode mem_mode)
1863 {
1864 rtx off;
1865 switch (GET_CODE (x))
1866 {
1867 case PRE_MODIFY:
1868 case POST_MODIFY:
1869 if (GET_CODE (x) == POST_MODIFY)
1870 output_address (mem_mode, XEXP (x, 0));
1871 off = XEXP (XEXP (x, 1), 1);
1872 if (XEXP (x, 0) == stack_pointer_rtx)
1873 {
1874 if (GET_CODE (x) == PRE_MODIFY)
1875 gcc_assert (INTVAL (off) > 0);
1876 else
1877 gcc_assert (INTVAL (off) < 0);
1878 }
1879 if (CONST_INT_P (off) && INTVAL (off) < 0)
1880 {
1881 fprintf (file, "--");
1882 off = GEN_INT (-INTVAL (off));
1883 }
1884 else
1885 fprintf (file, "++");
1886 if (GET_CODE (x) == PRE_MODIFY)
1887 output_address (mem_mode, XEXP (x, 0));
1888 print_address_offset (file, off, mem_mode);
1889 break;
1890
1891 case PLUS:
1892 off = XEXP (x, 1);
1893 if (CONST_INT_P (off) && INTVAL (off) < 0)
1894 {
1895 fprintf (file, "-");
1896 off = GEN_INT (-INTVAL (off));
1897 }
1898 else
1899 fprintf (file, "+");
1900 output_address (mem_mode, XEXP (x, 0));
1901 print_address_offset (file, off, mem_mode);
1902 break;
1903
1904 case PRE_DEC:
1905 gcc_assert (XEXP (x, 0) != stack_pointer_rtx);
1906 fprintf (file, "--");
1907 output_address (mem_mode, XEXP (x, 0));
1908 fprintf (file, "[1]");
1909 break;
1910 case PRE_INC:
1911 fprintf (file, "++");
1912 output_address (mem_mode, XEXP (x, 0));
1913 fprintf (file, "[1]");
1914 break;
1915 case POST_INC:
1916 gcc_assert (XEXP (x, 0) != stack_pointer_rtx);
1917 output_address (mem_mode, XEXP (x, 0));
1918 fprintf (file, "++[1]");
1919 break;
1920 case POST_DEC:
1921 output_address (mem_mode, XEXP (x, 0));
1922 fprintf (file, "--[1]");
1923 break;
1924
1925 case SYMBOL_REF:
1926 case CONST:
1927 case LABEL_REF:
1928 gcc_assert (sdata_symbolic_operand (x, Pmode));
1929 fprintf (file, "+B14(");
1930 output_addr_const (file, x);
1931 fprintf (file, ")");
1932 break;
1933
1934 case UNSPEC:
1935 switch (XINT (x, 1))
1936 {
1937 case UNSPEC_LOAD_GOT:
1938 fputs ("$GOT(", file);
1939 output_addr_const (file, XVECEXP (x, 0, 0));
1940 fputs (")", file);
1941 break;
1942 case UNSPEC_LOAD_SDATA:
1943 output_addr_const (file, XVECEXP (x, 0, 0));
1944 break;
1945 default:
1946 gcc_unreachable ();
1947 }
1948 break;
1949
1950 default:
1951 gcc_assert (GET_CODE (x) != MEM);
1952 c6x_print_operand (file, x, 0);
1953 break;
1954 }
1955 }
1956
1957 /* Return a single character, which is either 'l', 's', 'd' or 'm', which
1958 specifies the functional unit used by INSN. */
1959
1960 char
1961 c6x_get_unit_specifier (rtx_insn *insn)
1962 {
1963 enum attr_units units;
1964
1965 if (insn_info.exists ())
1966 {
1967 int unit = INSN_INFO_ENTRY (INSN_UID (insn)).reservation;
1968 return c6x_unit_names[unit][0];
1969 }
1970
1971 units = get_attr_units (insn);
1972 switch (units)
1973 {
1974 case UNITS_D:
1975 case UNITS_DL:
1976 case UNITS_DS:
1977 case UNITS_DLS:
1978 case UNITS_D_ADDR:
1979 return 'd';
1980 case UNITS_L:
1981 case UNITS_LS:
1982 return 'l';
1983 case UNITS_S:
1984 return 's';
1985 case UNITS_M:
1986 return 'm';
1987 default:
1988 gcc_unreachable ();
1989 }
1990 }
1991
1992 /* Prints the unit specifier field. */
1993 static void
1994 c6x_print_unit_specifier_field (FILE *file, rtx_insn *insn)
1995 {
1996 enum attr_units units = get_attr_units (insn);
1997 enum attr_cross cross = get_attr_cross (insn);
1998 enum attr_dest_regfile rf = get_attr_dest_regfile (insn);
1999 int half;
2000 char unitspec;
2001
2002 if (units == UNITS_D_ADDR)
2003 {
2004 enum attr_addr_regfile arf = get_attr_addr_regfile (insn);
2005 int t_half;
2006 gcc_assert (arf != ADDR_REGFILE_UNKNOWN);
2007 half = arf == ADDR_REGFILE_A ? 1 : 2;
2008 t_half = rf == DEST_REGFILE_A ? 1 : 2;
2009 fprintf (file, ".d%dt%d", half, t_half);
2010 return;
2011 }
2012
2013 if (insn_info.exists ())
2014 {
2015 int unit = INSN_INFO_ENTRY (INSN_UID (insn)).reservation;
2016 fputs (".", file);
2017 fputs (c6x_unit_names[unit], file);
2018 if (cross == CROSS_Y)
2019 fputs ("x", file);
2020 return;
2021 }
2022
2023 gcc_assert (rf != DEST_REGFILE_UNKNOWN);
2024 unitspec = c6x_get_unit_specifier (insn);
2025 half = rf == DEST_REGFILE_A ? 1 : 2;
2026 fprintf (file, ".%c%d%s", unitspec, half, cross == CROSS_Y ? "x" : "");
2027 }
2028
2029 /* Output assembly language output for the address ADDR to FILE. */
2030 static void
2031 c6x_print_operand_address (FILE *file, machine_mode mode, rtx addr)
2032 {
2033 c6x_print_address_operand (file, addr, mode);
2034 }
2035
2036 /* Print an operand, X, to FILE, with an optional modifier in CODE.
2037
2038 Meaning of CODE:
2039 $ -- print the unit specifier field for the instruction.
2040 . -- print the predicate for the instruction or an emptry string for an
2041 unconditional one.
2042 | -- print "||" if the insn should be issued in parallel with the previous
2043 one.
2044
2045 C -- print an opcode suffix for a reversed condition
2046 d -- H, W or D as a suffix for ADDA, based on the factor given by the
2047 operand
2048 D -- print either B, H, W or D as a suffix for ADDA, based on the size of
2049 the operand
2050 J -- print a predicate
2051 j -- like J, but use reverse predicate
2052 k -- treat a CONST_INT as a register number and print it as a register
2053 k -- like k, but print out a doubleword register
2054 n -- print an integer operand, negated
2055 p -- print the low part of a DImode register
2056 P -- print the high part of a DImode register
2057 r -- print the absolute value of an integer operand, shifted right by 1
2058 R -- print the absolute value of an integer operand, shifted right by 2
2059 f -- the first clear bit in an integer operand assumed to be a mask for
2060 a clr instruction
2061 F -- the last clear bit in such a mask
2062 s -- the first set bit in an integer operand assumed to be a mask for
2063 a set instruction
2064 S -- the last set bit in such a mask
2065 U -- print either 1 or 2, depending on the side of the machine used by
2066 the operand */
2067
2068 static void
2069 c6x_print_operand (FILE *file, rtx x, int code)
2070 {
2071 int i;
2072 HOST_WIDE_INT v;
2073 tree t;
2074 machine_mode mode;
2075
2076 if (code == '|')
2077 {
2078 if (GET_MODE (c6x_current_insn) != TImode)
2079 fputs ("||", file);
2080 return;
2081 }
2082 if (code == '$')
2083 {
2084 c6x_print_unit_specifier_field (file, c6x_current_insn);
2085 return;
2086 }
2087
2088 if (code == '.')
2089 {
2090 x = current_insn_predicate;
2091 if (x)
2092 {
2093 unsigned int regno = REGNO (XEXP (x, 0));
2094 fputs ("[", file);
2095 if (GET_CODE (x) == EQ)
2096 fputs ("!", file);
2097 fputs (reg_names [regno], file);
2098 fputs ("]", file);
2099 }
2100 return;
2101 }
2102
2103 mode = GET_MODE (x);
2104
2105 switch (code)
2106 {
2107 case 'C':
2108 case 'c':
2109 {
2110 enum rtx_code c = GET_CODE (x);
2111 if (code == 'C')
2112 c = swap_condition (c);
2113 fputs (GET_RTX_NAME (c), file);
2114 }
2115 return;
2116
2117 case 'J':
2118 case 'j':
2119 {
2120 unsigned int regno = REGNO (XEXP (x, 0));
2121 if ((GET_CODE (x) == EQ) == (code == 'J'))
2122 fputs ("!", file);
2123 fputs (reg_names [regno], file);
2124 }
2125 return;
2126
2127 case 'k':
2128 gcc_assert (GET_CODE (x) == CONST_INT);
2129 v = INTVAL (x);
2130 fprintf (file, "%s", reg_names[v]);
2131 return;
2132 case 'K':
2133 gcc_assert (GET_CODE (x) == CONST_INT);
2134 v = INTVAL (x);
2135 gcc_assert ((v & 1) == 0);
2136 fprintf (file, "%s:%s", reg_names[v + 1], reg_names[v]);
2137 return;
2138
2139 case 's':
2140 case 'S':
2141 case 'f':
2142 case 'F':
2143 gcc_assert (GET_CODE (x) == CONST_INT);
2144 v = INTVAL (x);
2145 for (i = 0; i < 32; i++)
2146 {
2147 HOST_WIDE_INT tst = v & 1;
2148 if (((code == 'f' || code == 'F') && !tst)
2149 || ((code == 's' || code == 'S') && tst))
2150 break;
2151 v >>= 1;
2152 }
2153 if (code == 'f' || code == 's')
2154 {
2155 fprintf (file, "%d", i);
2156 return;
2157 }
2158 for (;i < 32; i++)
2159 {
2160 HOST_WIDE_INT tst = v & 1;
2161 if ((code == 'F' && tst) || (code == 'S' && !tst))
2162 break;
2163 v >>= 1;
2164 }
2165 fprintf (file, "%d", i - 1);
2166 return;
2167
2168 case 'n':
2169 gcc_assert (GET_CODE (x) == CONST_INT);
2170 output_addr_const (file, GEN_INT (-INTVAL (x)));
2171 return;
2172
2173 case 'r':
2174 gcc_assert (GET_CODE (x) == CONST_INT);
2175 v = INTVAL (x);
2176 if (v < 0)
2177 v = -v;
2178 output_addr_const (file, GEN_INT (v >> 1));
2179 return;
2180
2181 case 'R':
2182 gcc_assert (GET_CODE (x) == CONST_INT);
2183 v = INTVAL (x);
2184 if (v < 0)
2185 v = -v;
2186 output_addr_const (file, GEN_INT (v >> 2));
2187 return;
2188
2189 case 'd':
2190 gcc_assert (GET_CODE (x) == CONST_INT);
2191 v = INTVAL (x);
2192 fputs (v == 2 ? "h" : v == 4 ? "w" : "d", file);
2193 return;
2194
2195 case 'p':
2196 case 'P':
2197 gcc_assert (GET_CODE (x) == REG);
2198 v = REGNO (x);
2199 if (code == 'P')
2200 v++;
2201 fputs (reg_names[v], file);
2202 return;
2203
2204 case 'D':
2205 v = 0;
2206 if (GET_CODE (x) == CONST)
2207 {
2208 x = XEXP (x, 0);
2209 gcc_assert (GET_CODE (x) == PLUS);
2210 gcc_assert (GET_CODE (XEXP (x, 1)) == CONST_INT);
2211 v = INTVAL (XEXP (x, 1));
2212 x = XEXP (x, 0);
2213
2214 }
2215 gcc_assert (GET_CODE (x) == SYMBOL_REF);
2216
2217 t = SYMBOL_REF_DECL (x);
2218 if (DECL_P (t))
2219 v |= DECL_ALIGN_UNIT (t);
2220 else
2221 v |= TYPE_ALIGN_UNIT (TREE_TYPE (t));
2222 if (v & 1)
2223 fputs ("b", file);
2224 else if (v & 2)
2225 fputs ("h", file);
2226 else
2227 fputs ("w", file);
2228 return;
2229
2230 case 'U':
2231 if (MEM_P (x))
2232 {
2233 x = XEXP (x, 0);
2234 if (GET_CODE (x) == PLUS
2235 || GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
2236 x = XEXP (x, 0);
2237 if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF)
2238 {
2239 gcc_assert (sdata_symbolic_operand (x, Pmode));
2240 fputs ("2", file);
2241 return;
2242 }
2243 }
2244 gcc_assert (REG_P (x));
2245 if (A_REGNO_P (REGNO (x)))
2246 fputs ("1", file);
2247 if (B_REGNO_P (REGNO (x)))
2248 fputs ("2", file);
2249 return;
2250
2251 default:
2252 switch (GET_CODE (x))
2253 {
2254 case REG:
2255 if (GET_MODE_SIZE (mode) == 8)
2256 fprintf (file, "%s:%s", reg_names[REGNO (x) + 1],
2257 reg_names[REGNO (x)]);
2258 else
2259 fprintf (file, "%s", reg_names[REGNO (x)]);
2260 break;
2261
2262 case MEM:
2263 fputc ('*', file);
2264 gcc_assert (XEXP (x, 0) != stack_pointer_rtx);
2265 c6x_print_address_operand (file, XEXP (x, 0), GET_MODE (x));
2266 break;
2267
2268 case SYMBOL_REF:
2269 fputc ('(', file);
2270 output_addr_const (file, x);
2271 fputc (')', file);
2272 break;
2273
2274 case CONST_INT:
2275 output_addr_const (file, x);
2276 break;
2277
2278 case CONST_DOUBLE:
2279 output_operand_lossage ("invalid const_double operand");
2280 break;
2281
2282 default:
2283 output_addr_const (file, x);
2284 }
2285 }
2286 }
2287
2288 /* Return TRUE if OP is a valid memory address with a base register of
2289 class C. If SMALL_OFFSET is true, we disallow memory references which would
2290 require a long offset with B14/B15. */
2291
2292 bool
2293 c6x_mem_operand (rtx op, enum reg_class c, bool small_offset)
2294 {
2295 machine_mode mode = GET_MODE (op);
2296 rtx base = XEXP (op, 0);
2297 switch (GET_CODE (base))
2298 {
2299 case REG:
2300 break;
2301 case PLUS:
2302 if (small_offset
2303 && (XEXP (base, 0) == stack_pointer_rtx
2304 || XEXP (base, 0) == pic_offset_table_rtx))
2305 {
2306 if (!c6x_legitimate_address_p_1 (mode, base, true, true))
2307 return false;
2308 }
2309
2310 /* fall through */
2311 case PRE_INC:
2312 case PRE_DEC:
2313 case PRE_MODIFY:
2314 case POST_INC:
2315 case POST_DEC:
2316 case POST_MODIFY:
2317 base = XEXP (base, 0);
2318 break;
2319
2320 case CONST:
2321 case LABEL_REF:
2322 case SYMBOL_REF:
2323 gcc_assert (sdata_symbolic_operand (base, Pmode));
2324 return !small_offset && c == B_REGS;
2325
2326 default:
2327 return false;
2328 }
2329 return TEST_HARD_REG_BIT (reg_class_contents[ (int) (c)], REGNO (base));
2330 }
2331
2332 /* Returns true if X is a valid address for use in a memory reference
2333 of mode MODE. If STRICT is true, we do not allow pseudo registers
2334 in the address. NO_LARGE_OFFSET is true if we are examining an
2335 address for use in a load or store misaligned instruction, or
2336 recursively examining an operand inside a PRE/POST_MODIFY. */
2337
2338 bool
2339 c6x_legitimate_address_p_1 (machine_mode mode, rtx x, bool strict,
2340 bool no_large_offset)
2341 {
2342 int size, size1;
2343 HOST_WIDE_INT off;
2344 enum rtx_code code = GET_CODE (x);
2345
2346 switch (code)
2347 {
2348 case PRE_MODIFY:
2349 case POST_MODIFY:
2350 /* We can't split these into word-sized pieces yet. */
2351 if (!TARGET_STDW && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
2352 return false;
2353 if (GET_CODE (XEXP (x, 1)) != PLUS)
2354 return false;
2355 if (!c6x_legitimate_address_p_1 (mode, XEXP (x, 1), strict, true))
2356 return false;
2357 if (!rtx_equal_p (XEXP (x, 0), XEXP (XEXP (x, 1), 0)))
2358 return false;
2359
2360 /* fall through */
2361 case PRE_INC:
2362 case PRE_DEC:
2363 case POST_INC:
2364 case POST_DEC:
2365 /* We can't split these into word-sized pieces yet. */
2366 if (!TARGET_STDW && GET_MODE_SIZE (mode) > UNITS_PER_WORD)
2367 return false;
2368 x = XEXP (x, 0);
2369 if (!REG_P (x))
2370 return false;
2371
2372 /* fall through */
2373 case REG:
2374 if (strict)
2375 return REGNO_OK_FOR_BASE_STRICT_P (REGNO (x));
2376 else
2377 return REGNO_OK_FOR_BASE_NONSTRICT_P (REGNO (x));
2378
2379 case PLUS:
2380 if (!REG_P (XEXP (x, 0))
2381 || !c6x_legitimate_address_p_1 (mode, XEXP (x, 0), strict, false))
2382 return false;
2383 /* We cannot ensure currently that both registers end up in the
2384 same register file. */
2385 if (REG_P (XEXP (x, 1)))
2386 return false;
2387
2388 if (mode == BLKmode)
2389 size = 4;
2390 else if (mode == VOIDmode)
2391 /* ??? This can happen during ivopts. */
2392 size = 1;
2393 else
2394 size = GET_MODE_SIZE (mode);
2395
2396 if (flag_pic
2397 && GET_CODE (XEXP (x, 1)) == UNSPEC
2398 && XINT (XEXP (x, 1), 1) == UNSPEC_LOAD_SDATA
2399 && XEXP (x, 0) == pic_offset_table_rtx
2400 && sdata_symbolic_operand (XVECEXP (XEXP (x, 1), 0, 0), SImode))
2401 return !no_large_offset && size <= 4;
2402 if (flag_pic == 1
2403 && mode == Pmode
2404 && GET_CODE (XEXP (x, 1)) == UNSPEC
2405 && XINT (XEXP (x, 1), 1) == UNSPEC_LOAD_GOT
2406 && XEXP (x, 0) == pic_offset_table_rtx
2407 && (GET_CODE (XVECEXP (XEXP (x, 1), 0, 0)) == SYMBOL_REF
2408 || GET_CODE (XVECEXP (XEXP (x, 1), 0, 0)) == LABEL_REF))
2409 return !no_large_offset;
2410 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
2411 return false;
2412
2413 off = INTVAL (XEXP (x, 1));
2414
2415 /* If the machine does not have doubleword load/stores, we'll use
2416 word size accesses. */
2417 size1 = size;
2418 if (size == 2 * UNITS_PER_WORD && !TARGET_STDW)
2419 size = UNITS_PER_WORD;
2420
2421 if (((HOST_WIDE_INT)size1 - 1) & off)
2422 return false;
2423 off /= size;
2424 if (off > -32 && off < (size1 == size ? 32 : 28))
2425 return true;
2426 if (no_large_offset || code != PLUS || XEXP (x, 0) != stack_pointer_rtx
2427 || size1 > UNITS_PER_WORD)
2428 return false;
2429 return off >= 0 && off < 32768;
2430
2431 case CONST:
2432 case SYMBOL_REF:
2433 case LABEL_REF:
2434 return (!no_large_offset
2435 /* With -fpic, we must wrap it in an unspec to show the B14
2436 dependency. */
2437 && !flag_pic
2438 && GET_MODE_SIZE (mode) <= UNITS_PER_WORD
2439 && sdata_symbolic_operand (x, Pmode));
2440
2441 default:
2442 return false;
2443 }
2444 }
2445
2446 static bool
2447 c6x_legitimate_address_p (machine_mode mode, rtx x, bool strict,
2448 code_helper = ERROR_MARK)
2449 {
2450 return c6x_legitimate_address_p_1 (mode, x, strict, false);
2451 }
2452
2453 static bool
2454 c6x_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED,
2455 rtx x ATTRIBUTE_UNUSED)
2456 {
2457 return true;
2458 }
2459
2460 /* Implements TARGET_PREFERRED_RENAME_CLASS. */
2461 static reg_class_t
2462 c6x_preferred_rename_class (reg_class_t cl)
2463 {
2464 if (cl == A_REGS)
2465 return NONPREDICATE_A_REGS;
2466 if (cl == B_REGS)
2467 return NONPREDICATE_B_REGS;
2468 if (cl == ALL_REGS || cl == GENERAL_REGS)
2469 return NONPREDICATE_REGS;
2470 return NO_REGS;
2471 }
2472
2473 /* Implements FINAL_PRESCAN_INSN. */
2474 void
2475 c6x_final_prescan_insn (rtx_insn *insn, rtx *opvec ATTRIBUTE_UNUSED,
2476 int noperands ATTRIBUTE_UNUSED)
2477 {
2478 c6x_current_insn = insn;
2479 }
2480
2481 /* A structure to describe the stack layout of a function. The layout is
2482 as follows:
2483
2484 [saved frame pointer (or possibly padding0)]
2485 --> incoming stack pointer, new hard frame pointer
2486 [saved call-used regs]
2487 [optional padding1]
2488 --> soft frame pointer
2489 [frame]
2490 [outgoing arguments]
2491 [optional padding2]
2492
2493 The structure members are laid out in this order. */
2494
2495 struct c6x_frame
2496 {
2497 int padding0;
2498 /* Number of registers to save. */
2499 int nregs;
2500 int padding1;
2501 HOST_WIDE_INT frame;
2502 int outgoing_arguments_size;
2503 int padding2;
2504
2505 HOST_WIDE_INT to_allocate;
2506 /* The offsets relative to the incoming stack pointer (which
2507 becomes HARD_FRAME_POINTER). */
2508 HOST_WIDE_INT frame_pointer_offset;
2509 HOST_WIDE_INT b3_offset;
2510
2511 /* True if we should call push_rts/pop_rts to save and restore
2512 registers. */
2513 bool push_rts;
2514 };
2515
2516 /* Return true if we need to save and modify the PIC register in the
2517 prologue. */
2518
2519 static bool
2520 must_reload_pic_reg_p (void)
2521 {
2522 if (!TARGET_DSBT)
2523 return false;
2524
2525 cgraph_node *local_info_node
2526 = cgraph_node::local_info_node (current_function_decl);
2527 if ((crtl->uses_pic_offset_table || !crtl->is_leaf)
2528 && !local_info_node->local)
2529 return true;
2530 return false;
2531 }
2532
2533 /* Return 1 if we need to save REGNO. */
2534 static int
2535 c6x_save_reg (unsigned int regno)
2536 {
2537 return ((df_regs_ever_live_p (regno)
2538 && !call_used_or_fixed_reg_p (regno))
2539 || (regno == RETURN_ADDR_REGNO
2540 && (df_regs_ever_live_p (regno)
2541 || !crtl->is_leaf))
2542 || (regno == PIC_OFFSET_TABLE_REGNUM && must_reload_pic_reg_p ()));
2543 }
2544
2545 /* Examine the number of regs NREGS we've determined we must save.
2546 Return true if we should use __c6xabi_push_rts/__c6xabi_pop_rts for
2547 prologue and epilogue. */
2548
2549 static bool
2550 use_push_rts_p (int nregs)
2551 {
2552 if (TARGET_INSNS_64PLUS && optimize_function_for_size_p (cfun)
2553 && !cfun->machine->contains_sibcall
2554 && !cfun->returns_struct
2555 && !TARGET_LONG_CALLS
2556 && nregs >= 6 && !frame_pointer_needed)
2557 return true;
2558 return false;
2559 }
2560
2561 /* Return number of saved general prupose registers. */
2562
2563 int
2564 c6x_nsaved_regs (void)
2565 {
2566 int nregs = 0;
2567 int regno;
2568
2569 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2570 if (c6x_save_reg (regno))
2571 nregs++;
2572 return nregs;
2573 }
2574
2575 /* The safe debug order mandated by the ABI. */
2576 static unsigned reg_save_order[] =
2577 {
2578 REG_A10, REG_A11, REG_A12, REG_A13,
2579 REG_A14, REG_B3,
2580 REG_B10, REG_B11, REG_B12, REG_B13,
2581 REG_B14, REG_A15
2582 };
2583
2584 #define N_SAVE_ORDER (ARRAY_SIZE (reg_save_order))
2585
2586 /* Compute the layout of the stack frame and store it in FRAME. */
2587
2588 static void
2589 c6x_compute_frame_layout (struct c6x_frame *frame)
2590 {
2591 HOST_WIDE_INT size = get_frame_size ();
2592 HOST_WIDE_INT offset;
2593 int nregs;
2594
2595 /* We use the four bytes which are technically inside the caller's frame,
2596 usually to save the frame pointer. */
2597 offset = -4;
2598 frame->padding0 = 0;
2599 nregs = c6x_nsaved_regs ();
2600 frame->push_rts = false;
2601 frame->b3_offset = 0;
2602 if (use_push_rts_p (nregs))
2603 {
2604 frame->push_rts = true;
2605 frame->b3_offset = (TARGET_BIG_ENDIAN ? -12 : -13) * 4;
2606 nregs = 14;
2607 }
2608 else if (c6x_save_reg (REG_B3))
2609 {
2610 int idx;
2611 for (idx = N_SAVE_ORDER - 1; reg_save_order[idx] != REG_B3; idx--)
2612 {
2613 if (c6x_save_reg (reg_save_order[idx]))
2614 frame->b3_offset -= 4;
2615 }
2616 }
2617 frame->nregs = nregs;
2618
2619 if (size == 0 && nregs == 0)
2620 {
2621 frame->padding0 = 4;
2622 frame->padding1 = frame->padding2 = 0;
2623 frame->frame_pointer_offset = frame->to_allocate = 0;
2624 frame->outgoing_arguments_size = 0;
2625 return;
2626 }
2627
2628 if (!frame->push_rts)
2629 offset += frame->nregs * 4;
2630
2631 if (offset == 0 && size == 0 && crtl->outgoing_args_size == 0
2632 && !crtl->is_leaf)
2633 /* Don't use the bottom of the caller's frame if we have no
2634 allocation of our own and call other functions. */
2635 frame->padding0 = frame->padding1 = 4;
2636 else if (offset & 4)
2637 frame->padding1 = 4;
2638 else
2639 frame->padding1 = 0;
2640
2641 offset += frame->padding0 + frame->padding1;
2642 frame->frame_pointer_offset = offset;
2643 offset += size;
2644
2645 frame->outgoing_arguments_size = crtl->outgoing_args_size;
2646 offset += frame->outgoing_arguments_size;
2647
2648 if ((offset & 4) == 0)
2649 frame->padding2 = 8;
2650 else
2651 frame->padding2 = 4;
2652 frame->to_allocate = offset + frame->padding2;
2653 }
2654
2655 /* Return the offset between two registers, one to be eliminated, and the other
2656 its replacement, at the start of a routine. */
2657
2658 HOST_WIDE_INT
2659 c6x_initial_elimination_offset (int from, int to)
2660 {
2661 struct c6x_frame frame;
2662 c6x_compute_frame_layout (&frame);
2663
2664 if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
2665 return 0;
2666 else if (from == FRAME_POINTER_REGNUM
2667 && to == HARD_FRAME_POINTER_REGNUM)
2668 return -frame.frame_pointer_offset;
2669 else
2670 {
2671 gcc_assert (to == STACK_POINTER_REGNUM);
2672
2673 if (from == ARG_POINTER_REGNUM)
2674 return frame.to_allocate + (frame.push_rts ? 56 : 0);
2675
2676 gcc_assert (from == FRAME_POINTER_REGNUM);
2677 return frame.to_allocate - frame.frame_pointer_offset;
2678 }
2679 }
2680
2681 /* Given FROM and TO register numbers, say whether this elimination is
2682 allowed. Frame pointer elimination is automatically handled. */
2683
2684 static bool
2685 c6x_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
2686 {
2687 if (to == STACK_POINTER_REGNUM)
2688 return !frame_pointer_needed;
2689 return true;
2690 }
2691
2692 /* Emit insns to increment the stack pointer by OFFSET. If
2693 FRAME_RELATED_P, set the RTX_FRAME_RELATED_P flag on the insns.
2694 Does nothing if the offset is zero. */
2695
2696 static void
2697 emit_add_sp_const (HOST_WIDE_INT offset, bool frame_related_p)
2698 {
2699 rtx to_add = GEN_INT (offset);
2700 rtx orig_to_add = to_add;
2701 rtx_insn *insn;
2702
2703 if (offset == 0)
2704 return;
2705
2706 if (offset < -32768 || offset > 32767)
2707 {
2708 rtx reg = gen_rtx_REG (SImode, REG_A0);
2709 rtx low = GEN_INT (trunc_int_for_mode (offset, HImode));
2710
2711 insn = emit_insn (gen_movsi_high (reg, low));
2712 if (frame_related_p)
2713 RTX_FRAME_RELATED_P (insn) = 1;
2714 insn = emit_insn (gen_movsi_lo_sum (reg, reg, to_add));
2715 if (frame_related_p)
2716 RTX_FRAME_RELATED_P (insn) = 1;
2717 to_add = reg;
2718 }
2719 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
2720 to_add));
2721 if (frame_related_p)
2722 {
2723 if (REG_P (to_add))
2724 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
2725 gen_rtx_SET (stack_pointer_rtx,
2726 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
2727 orig_to_add)));
2728
2729 RTX_FRAME_RELATED_P (insn) = 1;
2730 }
2731 }
2732
2733 /* Prologue and epilogue. */
2734 void
2735 c6x_expand_prologue (void)
2736 {
2737 struct c6x_frame frame;
2738 rtx_insn *insn;
2739 rtx mem;
2740 int nsaved = 0;
2741 HOST_WIDE_INT initial_offset, off, added_already;
2742
2743 c6x_compute_frame_layout (&frame);
2744
2745 if (flag_stack_usage_info)
2746 current_function_static_stack_size = frame.to_allocate;
2747
2748 initial_offset = -frame.to_allocate;
2749 if (frame.push_rts)
2750 {
2751 emit_insn (gen_push_rts ());
2752 nsaved = frame.nregs;
2753 }
2754
2755 /* If the offsets would be too large for the memory references we will
2756 create to save registers, do the stack allocation in two parts.
2757 Ensure by subtracting 8 that we don't store to the word pointed to
2758 by the stack pointer. */
2759 if (initial_offset < -32768)
2760 initial_offset = -frame.frame_pointer_offset - 8;
2761
2762 if (frame.to_allocate > 0)
2763 gcc_assert (initial_offset != 0);
2764
2765 off = -initial_offset + 4 - frame.padding0;
2766
2767 mem = gen_frame_mem (Pmode, stack_pointer_rtx);
2768
2769 added_already = 0;
2770 if (frame_pointer_needed)
2771 {
2772 rtx fp_reg = gen_rtx_REG (SImode, REG_A15);
2773 /* We go through some contortions here to both follow the ABI's
2774 recommendation that FP == incoming SP, and to avoid writing or
2775 reading the word pointed to by the stack pointer. */
2776 rtx addr = gen_rtx_POST_MODIFY (Pmode, stack_pointer_rtx,
2777 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
2778 GEN_INT (-8)));
2779 insn = emit_move_insn (gen_frame_mem (Pmode, addr), fp_reg);
2780 RTX_FRAME_RELATED_P (insn) = 1;
2781 nsaved++;
2782 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, stack_pointer_rtx,
2783 GEN_INT (8)));
2784 RTX_FRAME_RELATED_P (insn) = 1;
2785 off -= 4;
2786 added_already = -8;
2787 }
2788
2789 emit_add_sp_const (initial_offset - added_already, true);
2790
2791 if (nsaved < frame.nregs)
2792 {
2793 unsigned i;
2794
2795 for (i = 0; i < N_SAVE_ORDER; i++)
2796 {
2797 int idx = N_SAVE_ORDER - i - 1;
2798 unsigned regno = reg_save_order[idx];
2799 rtx reg;
2800 machine_mode save_mode = SImode;
2801
2802 if (regno == REG_A15 && frame_pointer_needed)
2803 /* Already saved. */
2804 continue;
2805 if (!c6x_save_reg (regno))
2806 continue;
2807
2808 if (TARGET_STDW && (off & 4) == 0 && off <= 256
2809 && (regno & 1) == 1
2810 && i + 1 < N_SAVE_ORDER
2811 && reg_save_order[idx - 1] == regno - 1
2812 && c6x_save_reg (regno - 1))
2813 {
2814 save_mode = DImode;
2815 regno--;
2816 i++;
2817 }
2818 reg = gen_rtx_REG (save_mode, regno);
2819 off -= GET_MODE_SIZE (save_mode);
2820
2821 insn = emit_move_insn (adjust_address (mem, save_mode, off),
2822 reg);
2823 RTX_FRAME_RELATED_P (insn) = 1;
2824
2825 nsaved += hard_regno_nregs (regno, save_mode);
2826 }
2827 }
2828 gcc_assert (nsaved == frame.nregs);
2829 emit_add_sp_const (-frame.to_allocate - initial_offset, true);
2830 if (must_reload_pic_reg_p ())
2831 {
2832 if (dsbt_decl == NULL)
2833 {
2834 tree t;
2835
2836 t = build_index_type (integer_one_node);
2837 t = build_array_type (integer_type_node, t);
2838 t = build_decl (BUILTINS_LOCATION, VAR_DECL,
2839 get_identifier ("__c6xabi_DSBT_BASE"), t);
2840 DECL_ARTIFICIAL (t) = 1;
2841 DECL_IGNORED_P (t) = 1;
2842 DECL_EXTERNAL (t) = 1;
2843 TREE_STATIC (t) = 1;
2844 TREE_PUBLIC (t) = 1;
2845 TREE_USED (t) = 1;
2846
2847 dsbt_decl = t;
2848 }
2849 emit_insn (gen_setup_dsbt (pic_offset_table_rtx,
2850 XEXP (DECL_RTL (dsbt_decl), 0)));
2851 }
2852 }
2853
2854 void
2855 c6x_expand_epilogue (bool sibcall)
2856 {
2857 unsigned i;
2858 struct c6x_frame frame;
2859 rtx mem;
2860 HOST_WIDE_INT off;
2861 int nsaved = 0;
2862
2863 c6x_compute_frame_layout (&frame);
2864
2865 mem = gen_frame_mem (Pmode, stack_pointer_rtx);
2866
2867 /* Insert a dummy set/use of the stack pointer. This creates a
2868 scheduler barrier between the prologue saves and epilogue restores. */
2869 emit_insn (gen_epilogue_barrier (stack_pointer_rtx, stack_pointer_rtx));
2870
2871 /* If the offsets would be too large for the memory references we will
2872 create to restore registers, do a preliminary stack adjustment here. */
2873 off = frame.to_allocate - frame.frame_pointer_offset + frame.padding1;
2874 if (frame.push_rts)
2875 {
2876 nsaved = frame.nregs;
2877 }
2878 else
2879 {
2880 if (frame.to_allocate > 32768)
2881 {
2882 /* Don't add the entire offset so that we leave an unused word
2883 above the stack pointer. */
2884 emit_add_sp_const ((off - 16) & ~7, false);
2885 off &= 7;
2886 off += 16;
2887 }
2888 for (i = 0; i < N_SAVE_ORDER; i++)
2889 {
2890 unsigned regno = reg_save_order[i];
2891 rtx reg;
2892 machine_mode save_mode = SImode;
2893
2894 if (!c6x_save_reg (regno))
2895 continue;
2896 if (regno == REG_A15 && frame_pointer_needed)
2897 continue;
2898
2899 if (TARGET_STDW && (off & 4) == 0 && off < 256
2900 && (regno & 1) == 0
2901 && i + 1 < N_SAVE_ORDER
2902 && reg_save_order[i + 1] == regno + 1
2903 && c6x_save_reg (regno + 1))
2904 {
2905 save_mode = DImode;
2906 i++;
2907 }
2908 reg = gen_rtx_REG (save_mode, regno);
2909
2910 emit_move_insn (reg, adjust_address (mem, save_mode, off));
2911
2912 off += GET_MODE_SIZE (save_mode);
2913 nsaved += hard_regno_nregs (regno, save_mode);
2914 }
2915 }
2916 if (!frame_pointer_needed)
2917 emit_add_sp_const (off + frame.padding0 - 4, false);
2918 else
2919 {
2920 rtx fp_reg = gen_rtx_REG (SImode, REG_A15);
2921 rtx addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx,
2922 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
2923 GEN_INT (8)));
2924 emit_insn (gen_addsi3 (stack_pointer_rtx, hard_frame_pointer_rtx,
2925 GEN_INT (-8)));
2926 emit_move_insn (fp_reg, gen_frame_mem (Pmode, addr));
2927 nsaved++;
2928 }
2929 gcc_assert (nsaved == frame.nregs);
2930 if (!sibcall)
2931 {
2932 if (frame.push_rts)
2933 emit_jump_insn (gen_pop_rts ());
2934 else
2935 emit_jump_insn (gen_return_internal (gen_rtx_REG (SImode,
2936 RETURN_ADDR_REGNO)));
2937 }
2938 }
2939
2940 /* Return the value of the return address for the frame COUNT steps up
2941 from the current frame, after the prologue.
2942 We punt for everything but the current frame by returning const0_rtx. */
2943
2944 rtx
2945 c6x_return_addr_rtx (int count)
2946 {
2947 if (count != 0)
2948 return const0_rtx;
2949
2950 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNO);
2951 }
2952
2953 /* Return true iff TYPE is one of the shadow types. */
2954 static bool
2955 shadow_type_p (enum attr_type type)
2956 {
2957 return (type == TYPE_SHADOW || type == TYPE_LOAD_SHADOW
2958 || type == TYPE_MULT_SHADOW);
2959 }
2960
2961 /* Return true iff INSN is a shadow pattern. */
2962 static bool
2963 shadow_p (rtx_insn *insn)
2964 {
2965 if (!NONDEBUG_INSN_P (insn) || recog_memoized (insn) < 0)
2966 return false;
2967 return shadow_type_p (get_attr_type (insn));
2968 }
2969
2970 /* Return true iff INSN is a shadow or blockage pattern. */
2971 static bool
2972 shadow_or_blockage_p (rtx_insn *insn)
2973 {
2974 enum attr_type type;
2975 if (!NONDEBUG_INSN_P (insn) || recog_memoized (insn) < 0)
2976 return false;
2977 type = get_attr_type (insn);
2978 return shadow_type_p (type) || type == TYPE_BLOCKAGE;
2979 }
2980
2981 /* Translate UNITS into a bitmask of units we can reserve for this
2982 insn. */
2983 static int
2984 get_reservation_flags (enum attr_units units)
2985 {
2986 switch (units)
2987 {
2988 case UNITS_D:
2989 case UNITS_D_ADDR:
2990 return RESERVATION_FLAG_D;
2991 case UNITS_L:
2992 return RESERVATION_FLAG_L;
2993 case UNITS_S:
2994 return RESERVATION_FLAG_S;
2995 case UNITS_M:
2996 return RESERVATION_FLAG_M;
2997 case UNITS_LS:
2998 return RESERVATION_FLAG_LS;
2999 case UNITS_DL:
3000 return RESERVATION_FLAG_DL;
3001 case UNITS_DS:
3002 return RESERVATION_FLAG_DS;
3003 case UNITS_DLS:
3004 return RESERVATION_FLAG_DLS;
3005 default:
3006 return 0;
3007 }
3008 }
3009
3010 /* Compute the side of the machine used by INSN, which reserves UNITS.
3011 This must match the reservations in the scheduling description. */
3012 static int
3013 get_insn_side (rtx_insn *insn, enum attr_units units)
3014 {
3015 if (units == UNITS_D_ADDR)
3016 return (get_attr_addr_regfile (insn) == ADDR_REGFILE_A ? 0 : 1);
3017 else
3018 {
3019 enum attr_dest_regfile rf = get_attr_dest_regfile (insn);
3020 if (rf == DEST_REGFILE_ANY)
3021 return get_attr_type (insn) == TYPE_BRANCH ? 0 : 1;
3022 else
3023 return rf == DEST_REGFILE_A ? 0 : 1;
3024 }
3025 }
3026
3027 /* After scheduling, walk the insns between HEAD and END and assign unit
3028 reservations. */
3029 static void
3030 assign_reservations (rtx_insn *head, rtx_insn *end)
3031 {
3032 rtx_insn *insn;
3033 for (insn = head; insn != NEXT_INSN (end); insn = NEXT_INSN (insn))
3034 {
3035 unsigned int sched_mask, reserved;
3036 rtx_insn *within, *last;
3037 int pass;
3038 int rsrv[2];
3039 int rsrv_count[2][4];
3040 int i;
3041
3042 if (GET_MODE (insn) != TImode)
3043 continue;
3044
3045 reserved = 0;
3046 last = NULL;
3047 /* Find the last insn in the packet. It has a state recorded for it,
3048 which we can use to determine the units we should be using. */
3049 for (within = insn;
3050 (within != NEXT_INSN (end)
3051 && (within == insn || GET_MODE (within) != TImode));
3052 within = NEXT_INSN (within))
3053 {
3054 int icode;
3055 if (!NONDEBUG_INSN_P (within))
3056 continue;
3057 icode = recog_memoized (within);
3058 if (icode < 0)
3059 continue;
3060 if (shadow_p (within))
3061 continue;
3062 if (INSN_INFO_ENTRY (INSN_UID (within)).reservation != 0)
3063 reserved |= 1 << INSN_INFO_ENTRY (INSN_UID (within)).reservation;
3064 last = within;
3065 }
3066 if (last == NULL_RTX)
3067 continue;
3068
3069 sched_mask = INSN_INFO_ENTRY (INSN_UID (last)).unit_mask;
3070 sched_mask &= ~reserved;
3071
3072 memset (rsrv_count, 0, sizeof rsrv_count);
3073 rsrv[0] = rsrv[1] = ~0;
3074 for (i = 0; i < 8; i++)
3075 {
3076 int side = i / 4;
3077 int unit = i & 3;
3078 unsigned unit_bit = 1 << (unit + side * UNIT_QID_SIDE_OFFSET);
3079 /* Clear the bits which we expect to reserve in the following loop,
3080 leaving the ones set which aren't present in the scheduler's
3081 state and shouldn't be reserved. */
3082 if (sched_mask & unit_bit)
3083 rsrv[i / 4] &= ~(1 << unit);
3084 }
3085
3086 /* Walk through the insns that occur in the same cycle. We use multiple
3087 passes to assign units, assigning for insns with the most specific
3088 requirements first. */
3089 for (pass = 0; pass < 4; pass++)
3090 for (within = insn;
3091 (within != NEXT_INSN (end)
3092 && (within == insn || GET_MODE (within) != TImode));
3093 within = NEXT_INSN (within))
3094 {
3095 int uid = INSN_UID (within);
3096 int this_rsrv, side;
3097 int icode;
3098 enum attr_units units;
3099 enum attr_type type;
3100 int j;
3101
3102 if (!NONDEBUG_INSN_P (within))
3103 continue;
3104 icode = recog_memoized (within);
3105 if (icode < 0)
3106 continue;
3107 if (INSN_INFO_ENTRY (uid).reservation != 0)
3108 continue;
3109 units = get_attr_units (within);
3110 type = get_attr_type (within);
3111 this_rsrv = get_reservation_flags (units);
3112 if (this_rsrv == 0)
3113 continue;
3114 side = get_insn_side (within, units);
3115
3116 /* Certain floating point instructions are treated specially. If
3117 an insn can choose between units it can reserve, and its
3118 reservation spans more than one cycle, the reservation contains
3119 special markers in the first cycle to help us reconstruct what
3120 the automaton chose. */
3121 if ((type == TYPE_ADDDP || type == TYPE_FP4)
3122 && units == UNITS_LS)
3123 {
3124 int test1_code = ((type == TYPE_FP4 ? UNIT_QID_FPL1 : UNIT_QID_ADDDPL1)
3125 + side * UNIT_QID_SIDE_OFFSET);
3126 int test2_code = ((type == TYPE_FP4 ? UNIT_QID_FPS1 : UNIT_QID_ADDDPS1)
3127 + side * UNIT_QID_SIDE_OFFSET);
3128 if ((sched_mask & (1 << test1_code)) != 0)
3129 {
3130 this_rsrv = RESERVATION_FLAG_L;
3131 sched_mask &= ~(1 << test1_code);
3132 }
3133 else if ((sched_mask & (1 << test2_code)) != 0)
3134 {
3135 this_rsrv = RESERVATION_FLAG_S;
3136 sched_mask &= ~(1 << test2_code);
3137 }
3138 }
3139
3140 if ((this_rsrv & (this_rsrv - 1)) == 0)
3141 {
3142 int t = exact_log2 (this_rsrv) + side * UNIT_QID_SIDE_OFFSET;
3143 rsrv[side] |= this_rsrv;
3144 INSN_INFO_ENTRY (uid).reservation = t;
3145 continue;
3146 }
3147
3148 if (pass == 1)
3149 {
3150 for (j = 0; j < 4; j++)
3151 if (this_rsrv & (1 << j))
3152 rsrv_count[side][j]++;
3153 continue;
3154 }
3155 if ((pass == 2 && this_rsrv != RESERVATION_FLAG_DLS)
3156 || (pass == 3 && this_rsrv == RESERVATION_FLAG_DLS))
3157 {
3158 int best = -1, best_cost = INT_MAX;
3159 for (j = 0; j < 4; j++)
3160 if ((this_rsrv & (1 << j))
3161 && !(rsrv[side] & (1 << j))
3162 && rsrv_count[side][j] < best_cost)
3163 {
3164 best_cost = rsrv_count[side][j];
3165 best = j;
3166 }
3167 gcc_assert (best != -1);
3168 rsrv[side] |= 1 << best;
3169 for (j = 0; j < 4; j++)
3170 if ((this_rsrv & (1 << j)) && j != best)
3171 rsrv_count[side][j]--;
3172
3173 INSN_INFO_ENTRY (uid).reservation
3174 = best + side * UNIT_QID_SIDE_OFFSET;
3175 }
3176 }
3177 }
3178 }
3179
3180 /* Return a factor by which to weight unit imbalances for a reservation
3181 R. */
3182 static int
3183 unit_req_factor (enum unitreqs r)
3184 {
3185 switch (r)
3186 {
3187 case UNIT_REQ_D:
3188 case UNIT_REQ_L:
3189 case UNIT_REQ_S:
3190 case UNIT_REQ_M:
3191 case UNIT_REQ_X:
3192 case UNIT_REQ_T:
3193 return 1;
3194 case UNIT_REQ_DL:
3195 case UNIT_REQ_LS:
3196 case UNIT_REQ_DS:
3197 return 2;
3198 case UNIT_REQ_DLS:
3199 return 3;
3200 default:
3201 gcc_unreachable ();
3202 }
3203 }
3204
3205 /* Examine INSN, and store in REQ1/SIDE1 and REQ2/SIDE2 the unit
3206 requirements. Returns zero if INSN can't be handled, otherwise
3207 either one or two to show how many of the two pairs are in use.
3208 REQ1 is always used, it holds what is normally thought of as the
3209 instructions reservation, e.g. UNIT_REQ_DL. REQ2 is used to either
3210 describe a cross path, or for loads/stores, the T unit. */
3211 static int
3212 get_unit_reqs (rtx_insn *insn, int *req1, int *side1, int *req2, int *side2)
3213 {
3214 enum attr_units units;
3215 enum attr_cross cross;
3216 int side, req;
3217
3218 if (!NONDEBUG_INSN_P (insn) || recog_memoized (insn) < 0)
3219 return 0;
3220 units = get_attr_units (insn);
3221 if (units == UNITS_UNKNOWN)
3222 return 0;
3223 side = get_insn_side (insn, units);
3224 cross = get_attr_cross (insn);
3225
3226 req = (units == UNITS_D ? UNIT_REQ_D
3227 : units == UNITS_D_ADDR ? UNIT_REQ_D
3228 : units == UNITS_DL ? UNIT_REQ_DL
3229 : units == UNITS_DS ? UNIT_REQ_DS
3230 : units == UNITS_L ? UNIT_REQ_L
3231 : units == UNITS_LS ? UNIT_REQ_LS
3232 : units == UNITS_S ? UNIT_REQ_S
3233 : units == UNITS_M ? UNIT_REQ_M
3234 : units == UNITS_DLS ? UNIT_REQ_DLS
3235 : -1);
3236 gcc_assert (req != -1);
3237 *req1 = req;
3238 *side1 = side;
3239 if (units == UNITS_D_ADDR)
3240 {
3241 *req2 = UNIT_REQ_T;
3242 *side2 = side ^ (cross == CROSS_Y ? 1 : 0);
3243 return 2;
3244 }
3245 else if (cross == CROSS_Y)
3246 {
3247 *req2 = UNIT_REQ_X;
3248 *side2 = side;
3249 return 2;
3250 }
3251 return 1;
3252 }
3253
3254 /* Walk the insns between and including HEAD and TAIL, and mark the
3255 resource requirements in the unit_reqs table. */
3256 static void
3257 count_unit_reqs (unit_req_table reqs, rtx_insn *head, rtx_insn *tail)
3258 {
3259 rtx_insn *insn;
3260
3261 memset (reqs, 0, sizeof (unit_req_table));
3262
3263 for (insn = head; insn != NEXT_INSN (tail); insn = NEXT_INSN (insn))
3264 {
3265 int side1, side2, req1, req2;
3266
3267 switch (get_unit_reqs (insn, &req1, &side1, &req2, &side2))
3268 {
3269 case 2:
3270 reqs[side2][req2]++;
3271 /* fall through */
3272 case 1:
3273 reqs[side1][req1]++;
3274 break;
3275 }
3276 }
3277 }
3278
3279 /* Update the table REQS by merging more specific unit reservations into
3280 more general ones, i.e. counting (for example) UNIT_REQ_D also in
3281 UNIT_REQ_DL, DS, and DLS. */
3282 static void
3283 merge_unit_reqs (unit_req_table reqs)
3284 {
3285 int side;
3286 for (side = 0; side < 2; side++)
3287 {
3288 int d = reqs[side][UNIT_REQ_D];
3289 int l = reqs[side][UNIT_REQ_L];
3290 int s = reqs[side][UNIT_REQ_S];
3291 int dl = reqs[side][UNIT_REQ_DL];
3292 int ls = reqs[side][UNIT_REQ_LS];
3293 int ds = reqs[side][UNIT_REQ_DS];
3294
3295 reqs[side][UNIT_REQ_DL] += d;
3296 reqs[side][UNIT_REQ_DL] += l;
3297 reqs[side][UNIT_REQ_DS] += d;
3298 reqs[side][UNIT_REQ_DS] += s;
3299 reqs[side][UNIT_REQ_LS] += l;
3300 reqs[side][UNIT_REQ_LS] += s;
3301 reqs[side][UNIT_REQ_DLS] += ds + dl + ls + d + l + s;
3302 }
3303 }
3304
3305 /* Examine the table REQS and return a measure of unit imbalance by comparing
3306 the two sides of the machine. If, for example, D1 is used twice and D2
3307 used not at all, the return value should be 1 in the absence of other
3308 imbalances. */
3309 static int
3310 unit_req_imbalance (unit_req_table reqs)
3311 {
3312 int val = 0;
3313 int i;
3314
3315 for (i = 0; i < UNIT_REQ_MAX; i++)
3316 {
3317 int factor = unit_req_factor ((enum unitreqs) i);
3318 int diff = abs (reqs[0][i] - reqs[1][i]);
3319 val += (diff + factor - 1) / factor / 2;
3320 }
3321 return val;
3322 }
3323
3324 /* Return the resource-constrained minimum iteration interval given the
3325 data in the REQS table. This must have been processed with
3326 merge_unit_reqs already. */
3327 static int
3328 res_mii (unit_req_table reqs)
3329 {
3330 int side, req;
3331 int worst = 1;
3332 for (side = 0; side < 2; side++)
3333 for (req = 0; req < UNIT_REQ_MAX; req++)
3334 {
3335 int factor = unit_req_factor ((enum unitreqs) req);
3336 worst = MAX ((reqs[side][UNIT_REQ_D] + factor - 1) / factor, worst);
3337 }
3338
3339 return worst;
3340 }
3341
3342 /* Examine INSN, and store in PMASK1 and PMASK2 bitmasks that represent
3343 the operands that are involved in the (up to) two reservations, as
3344 found by get_unit_reqs. Return true if we did this successfully, false
3345 if we couldn't identify what to do with INSN. */
3346 static bool
3347 get_unit_operand_masks (rtx_insn *insn, unsigned int *pmask1,
3348 unsigned int *pmask2)
3349 {
3350 enum attr_op_pattern op_pat;
3351
3352 if (recog_memoized (insn) < 0)
3353 return 0;
3354 if (GET_CODE (PATTERN (insn)) == COND_EXEC)
3355 return false;
3356 extract_insn (insn);
3357 op_pat = get_attr_op_pattern (insn);
3358 if (op_pat == OP_PATTERN_DT)
3359 {
3360 gcc_assert (recog_data.n_operands == 2);
3361 *pmask1 = 1 << 0;
3362 *pmask2 = 1 << 1;
3363 return true;
3364 }
3365 else if (op_pat == OP_PATTERN_TD)
3366 {
3367 gcc_assert (recog_data.n_operands == 2);
3368 *pmask1 = 1 << 1;
3369 *pmask2 = 1 << 0;
3370 return true;
3371 }
3372 else if (op_pat == OP_PATTERN_SXS)
3373 {
3374 gcc_assert (recog_data.n_operands == 3);
3375 *pmask1 = (1 << 0) | (1 << 2);
3376 *pmask2 = 1 << 1;
3377 return true;
3378 }
3379 else if (op_pat == OP_PATTERN_SX)
3380 {
3381 gcc_assert (recog_data.n_operands == 2);
3382 *pmask1 = 1 << 0;
3383 *pmask2 = 1 << 1;
3384 return true;
3385 }
3386 else if (op_pat == OP_PATTERN_SSX)
3387 {
3388 gcc_assert (recog_data.n_operands == 3);
3389 *pmask1 = (1 << 0) | (1 << 1);
3390 *pmask2 = 1 << 2;
3391 return true;
3392 }
3393 return false;
3394 }
3395
3396 /* Try to replace a register in INSN, which has corresponding rename info
3397 from regrename_analyze in INFO. OP_MASK and ORIG_SIDE provide information
3398 about the operands that must be renamed and the side they are on.
3399 REQS is the table of unit reservations in the loop between HEAD and TAIL.
3400 We recompute this information locally after our transformation, and keep
3401 it only if we managed to improve the balance. */
3402 static void
3403 try_rename_operands (rtx_insn *head, rtx_insn *tail, unit_req_table reqs,
3404 rtx insn,
3405 insn_rr_info *info, unsigned int op_mask, int orig_side)
3406 {
3407 enum reg_class super_class = orig_side == 0 ? B_REGS : A_REGS;
3408 HARD_REG_SET unavailable;
3409 du_head_p this_head;
3410 struct du_chain *chain;
3411 int i;
3412 unsigned tmp_mask;
3413 int best_reg, old_reg;
3414 vec<du_head_p> involved_chains = vNULL;
3415 unit_req_table new_reqs;
3416 bool ok;
3417
3418 for (i = 0, tmp_mask = op_mask; tmp_mask; i++)
3419 {
3420 du_head_p op_chain;
3421 if ((tmp_mask & (1 << i)) == 0)
3422 continue;
3423 if (info->op_info[i].n_chains != 1)
3424 goto out_fail;
3425 op_chain = regrename_chain_from_id (info->op_info[i].heads[0]->id);
3426 involved_chains.safe_push (op_chain);
3427 tmp_mask &= ~(1 << i);
3428 }
3429
3430 if (involved_chains.length () > 1)
3431 goto out_fail;
3432
3433 this_head = involved_chains[0];
3434 if (this_head->cannot_rename)
3435 goto out_fail;
3436
3437 for (chain = this_head->first; chain; chain = chain->next_use)
3438 {
3439 unsigned int mask1, mask2, mask_changed;
3440 int count, side1, side2, req1, req2;
3441 insn_rr_info *this_rr = &insn_rr[INSN_UID (chain->insn)];
3442
3443 count = get_unit_reqs (chain->insn, &req1, &side1, &req2, &side2);
3444
3445 if (count == 0)
3446 goto out_fail;
3447
3448 if (!get_unit_operand_masks (chain->insn, &mask1, &mask2))
3449 goto out_fail;
3450
3451 extract_insn (chain->insn);
3452
3453 mask_changed = 0;
3454 for (i = 0; i < recog_data.n_operands; i++)
3455 {
3456 int j;
3457 int n_this_op = this_rr->op_info[i].n_chains;
3458 for (j = 0; j < n_this_op; j++)
3459 {
3460 du_head_p other = this_rr->op_info[i].heads[j];
3461 if (regrename_chain_from_id (other->id) == this_head)
3462 break;
3463 }
3464 if (j == n_this_op)
3465 continue;
3466
3467 if (n_this_op != 1)
3468 goto out_fail;
3469 mask_changed |= 1 << i;
3470 }
3471 gcc_assert (mask_changed != 0);
3472 if (mask_changed != mask1 && mask_changed != mask2)
3473 goto out_fail;
3474 }
3475
3476 /* If we get here, we can do the renaming. */
3477 unavailable = ~reg_class_contents[super_class];
3478
3479 old_reg = this_head->regno;
3480 best_reg =
3481 find_rename_reg (this_head, super_class, &unavailable, old_reg, true);
3482
3483 ok = regrename_do_replace (this_head, best_reg);
3484 gcc_assert (ok);
3485
3486 count_unit_reqs (new_reqs, head, PREV_INSN (tail));
3487 merge_unit_reqs (new_reqs);
3488 if (dump_file)
3489 {
3490 fprintf (dump_file, "reshuffle for insn %d, op_mask %x, "
3491 "original side %d, new reg %d\n",
3492 INSN_UID (insn), op_mask, orig_side, best_reg);
3493 fprintf (dump_file, " imbalance %d -> %d\n",
3494 unit_req_imbalance (reqs), unit_req_imbalance (new_reqs));
3495 }
3496 if (unit_req_imbalance (new_reqs) > unit_req_imbalance (reqs))
3497 {
3498 ok = regrename_do_replace (this_head, old_reg);
3499 gcc_assert (ok);
3500 }
3501 else
3502 memcpy (reqs, new_reqs, sizeof (unit_req_table));
3503
3504 out_fail:
3505 involved_chains.release ();
3506 }
3507
3508 /* Find insns in LOOP which would, if shifted to the other side
3509 of the machine, reduce an imbalance in the unit reservations. */
3510 static void
3511 reshuffle_units (basic_block loop)
3512 {
3513 rtx_insn *head = BB_HEAD (loop);
3514 rtx_insn *tail = BB_END (loop);
3515 rtx_insn *insn;
3516 unit_req_table reqs;
3517 edge e;
3518 edge_iterator ei;
3519 bitmap_head bbs;
3520
3521 count_unit_reqs (reqs, head, PREV_INSN (tail));
3522 merge_unit_reqs (reqs);
3523
3524 regrename_init (true);
3525
3526 bitmap_initialize (&bbs, &bitmap_default_obstack);
3527
3528 FOR_EACH_EDGE (e, ei, loop->preds)
3529 bitmap_set_bit (&bbs, e->src->index);
3530
3531 bitmap_set_bit (&bbs, loop->index);
3532 regrename_analyze (&bbs);
3533
3534 for (insn = head; insn != NEXT_INSN (tail); insn = NEXT_INSN (insn))
3535 {
3536 enum attr_units units;
3537 int count, side1, side2, req1, req2;
3538 unsigned int mask1, mask2;
3539 insn_rr_info *info;
3540
3541 if (!NONDEBUG_INSN_P (insn))
3542 continue;
3543
3544 count = get_unit_reqs (insn, &req1, &side1, &req2, &side2);
3545
3546 if (count == 0)
3547 continue;
3548
3549 if (!get_unit_operand_masks (insn, &mask1, &mask2))
3550 continue;
3551
3552 info = &insn_rr[INSN_UID (insn)];
3553 if (info->op_info == NULL)
3554 continue;
3555
3556 if (reqs[side1][req1] > 1
3557 && reqs[side1][req1] > 2 * reqs[side1 ^ 1][req1])
3558 {
3559 try_rename_operands (head, tail, reqs, insn, info, mask1, side1);
3560 }
3561
3562 units = get_attr_units (insn);
3563 if (units == UNITS_D_ADDR)
3564 {
3565 gcc_assert (count == 2);
3566 if (reqs[side2][req2] > 1
3567 && reqs[side2][req2] > 2 * reqs[side2 ^ 1][req2])
3568 {
3569 try_rename_operands (head, tail, reqs, insn, info, mask2, side2);
3570 }
3571 }
3572 }
3573 regrename_finish ();
3574 }
3575
3576 /* Backend scheduling state. */
3577 typedef struct c6x_sched_context
3578 {
3579 /* The current scheduler clock, saved in the sched_reorder hook. */
3580 int curr_sched_clock;
3581
3582 /* Number of insns issued so far in this cycle. */
3583 int issued_this_cycle;
3584
3585 /* We record the time at which each jump occurs in JUMP_CYCLES. The
3586 theoretical maximum for number of jumps in flight is 12: 2 every
3587 cycle, with a latency of 6 cycles each. This is a circular
3588 buffer; JUMP_CYCLE_INDEX is the pointer to the start. Earlier
3589 jumps have a higher index. This array should be accessed through
3590 the jump_cycle function. */
3591 int jump_cycles[12];
3592 int jump_cycle_index;
3593
3594 /* In parallel with jump_cycles, this array records the opposite of
3595 the condition used in each pending jump. This is used to
3596 predicate insns that are scheduled in the jump's delay slots. If
3597 this is NULL_RTX no such predication happens. */
3598 rtx jump_cond[12];
3599
3600 /* Similar to the jump_cycles mechanism, but here we take into
3601 account all insns with delay slots, to avoid scheduling asms into
3602 the delay slots. */
3603 int delays_finished_at;
3604
3605 /* The following variable value is the last issued insn. */
3606 rtx_insn *last_scheduled_insn;
3607 /* The last issued insn that isn't a shadow of another. */
3608 rtx_insn *last_scheduled_iter0;
3609
3610 /* The following variable value is DFA state before issuing the
3611 first insn in the current clock cycle. We do not use this member
3612 of the structure directly; we copy the data in and out of
3613 prev_cycle_state. */
3614 state_t prev_cycle_state_ctx;
3615
3616 int reg_n_accesses[FIRST_PSEUDO_REGISTER];
3617 int reg_n_xaccesses[FIRST_PSEUDO_REGISTER];
3618 int reg_set_in_cycle[FIRST_PSEUDO_REGISTER];
3619
3620 int tmp_reg_n_accesses[FIRST_PSEUDO_REGISTER];
3621 int tmp_reg_n_xaccesses[FIRST_PSEUDO_REGISTER];
3622 } *c6x_sched_context_t;
3623
3624 /* The current scheduling state. */
3625 static struct c6x_sched_context ss;
3626
3627 /* The following variable value is DFA state before issuing the first insn
3628 in the current clock cycle. This is used in c6x_variable_issue for
3629 comparison with the state after issuing the last insn in a cycle. */
3630 static state_t prev_cycle_state;
3631
3632 /* Set when we discover while processing an insn that it would lead to too
3633 many accesses of the same register. */
3634 static bool reg_access_stall;
3635
3636 /* The highest insn uid after delayed insns were split, but before loop bodies
3637 were copied by the modulo scheduling code. */
3638 static int sploop_max_uid_iter0;
3639
3640 /* Look up the jump cycle with index N. For an out-of-bounds N, we return 0,
3641 so the caller does not specifically have to test for it. */
3642 static int
3643 get_jump_cycle (int n)
3644 {
3645 if (n >= 12)
3646 return 0;
3647 n += ss.jump_cycle_index;
3648 if (n >= 12)
3649 n -= 12;
3650 return ss.jump_cycles[n];
3651 }
3652
3653 /* Look up the jump condition with index N. */
3654 static rtx
3655 get_jump_cond (int n)
3656 {
3657 if (n >= 12)
3658 return NULL_RTX;
3659 n += ss.jump_cycle_index;
3660 if (n >= 12)
3661 n -= 12;
3662 return ss.jump_cond[n];
3663 }
3664
3665 /* Return the index of the first jump that occurs after CLOCK_VAR. If no jump
3666 has delay slots beyond CLOCK_VAR, return -1. */
3667 static int
3668 first_jump_index (int clock_var)
3669 {
3670 int retval = -1;
3671 int n = 0;
3672 for (;;)
3673 {
3674 int t = get_jump_cycle (n);
3675 if (t <= clock_var)
3676 break;
3677 retval = n;
3678 n++;
3679 }
3680 return retval;
3681 }
3682
3683 /* Add a new entry in our scheduling state for a jump that occurs in CYCLE
3684 and has the opposite condition of COND. */
3685 static void
3686 record_jump (int cycle, rtx cond)
3687 {
3688 if (ss.jump_cycle_index == 0)
3689 ss.jump_cycle_index = 11;
3690 else
3691 ss.jump_cycle_index--;
3692 ss.jump_cycles[ss.jump_cycle_index] = cycle;
3693 ss.jump_cond[ss.jump_cycle_index] = cond;
3694 }
3695
3696 /* Set the clock cycle of INSN to CYCLE. Also clears the insn's entry in
3697 new_conditions. */
3698 static void
3699 insn_set_clock (rtx insn, int cycle)
3700 {
3701 unsigned uid = INSN_UID (insn);
3702
3703 if (uid >= INSN_INFO_LENGTH)
3704 insn_info.safe_grow (uid * 5 / 4 + 10, true);
3705
3706 INSN_INFO_ENTRY (uid).clock = cycle;
3707 INSN_INFO_ENTRY (uid).new_cond = NULL;
3708 INSN_INFO_ENTRY (uid).reservation = 0;
3709 INSN_INFO_ENTRY (uid).ebb_start = false;
3710 }
3711
3712 /* Return the clock cycle we set for the insn with uid UID. */
3713 static int
3714 insn_uid_get_clock (int uid)
3715 {
3716 return INSN_INFO_ENTRY (uid).clock;
3717 }
3718
3719 /* Return the clock cycle we set for INSN. */
3720 static int
3721 insn_get_clock (rtx insn)
3722 {
3723 return insn_uid_get_clock (INSN_UID (insn));
3724 }
3725
3726 /* Examine INSN, and if it is a conditional jump of any kind, return
3727 the opposite of the condition in which it branches. Otherwise,
3728 return NULL_RTX. */
3729 static rtx
3730 condjump_opposite_condition (rtx insn)
3731 {
3732 rtx pat = PATTERN (insn);
3733 int icode = INSN_CODE (insn);
3734 rtx x = NULL;
3735
3736 if (icode == CODE_FOR_br_true || icode == CODE_FOR_br_false)
3737 {
3738 x = XEXP (SET_SRC (pat), 0);
3739 if (icode == CODE_FOR_br_false)
3740 return x;
3741 }
3742 if (GET_CODE (pat) == COND_EXEC)
3743 {
3744 rtx t = COND_EXEC_CODE (pat);
3745 if ((GET_CODE (t) == PARALLEL
3746 && GET_CODE (XVECEXP (t, 0, 0)) == RETURN)
3747 || (GET_CODE (t) == UNSPEC && XINT (t, 1) == UNSPEC_REAL_JUMP)
3748 || (GET_CODE (t) == SET && SET_DEST (t) == pc_rtx))
3749 x = COND_EXEC_TEST (pat);
3750 }
3751
3752 if (x != NULL_RTX)
3753 {
3754 enum rtx_code code = GET_CODE (x);
3755 x = gen_rtx_fmt_ee (code == EQ ? NE : EQ,
3756 GET_MODE (x), XEXP (x, 0),
3757 XEXP (x, 1));
3758 }
3759 return x;
3760 }
3761
3762 /* Return true iff COND1 and COND2 are exactly opposite conditions
3763 one of them NE and the other EQ. */
3764 static bool
3765 conditions_opposite_p (rtx cond1, rtx cond2)
3766 {
3767 return (rtx_equal_p (XEXP (cond1, 0), XEXP (cond2, 0))
3768 && rtx_equal_p (XEXP (cond1, 1), XEXP (cond2, 1))
3769 && GET_CODE (cond1) == reverse_condition (GET_CODE (cond2)));
3770 }
3771
3772 /* Return true if we can add a predicate COND to INSN, or if INSN
3773 already has that predicate. If DOIT is true, also perform the
3774 modification. */
3775 static bool
3776 predicate_insn (rtx_insn *insn, rtx cond, bool doit)
3777 {
3778 int icode;
3779 if (cond == NULL_RTX)
3780 {
3781 gcc_assert (!doit);
3782 return false;
3783 }
3784
3785 if (get_attr_predicable (insn) == PREDICABLE_YES
3786 && GET_CODE (PATTERN (insn)) != COND_EXEC)
3787 {
3788 if (doit)
3789 {
3790 cond = copy_rtx (cond);
3791 rtx newpat = gen_rtx_COND_EXEC (VOIDmode, cond, PATTERN (insn));
3792 PATTERN (insn) = newpat;
3793 INSN_CODE (insn) = -1;
3794 }
3795 return true;
3796 }
3797 if (GET_CODE (PATTERN (insn)) == COND_EXEC
3798 && rtx_equal_p (COND_EXEC_TEST (PATTERN (insn)), cond))
3799 return true;
3800 icode = INSN_CODE (insn);
3801 if (icode == CODE_FOR_real_jump
3802 || icode == CODE_FOR_jump
3803 || icode == CODE_FOR_indirect_jump)
3804 {
3805 rtx pat = PATTERN (insn);
3806 rtx dest = (icode == CODE_FOR_real_jump ? XVECEXP (pat, 0, 0)
3807 : icode == CODE_FOR_jump ? XEXP (SET_SRC (pat), 0)
3808 : SET_SRC (pat));
3809 if (doit)
3810 {
3811 rtx newpat;
3812 if (REG_P (dest))
3813 newpat = gen_rtx_COND_EXEC (VOIDmode, cond, PATTERN (insn));
3814 else
3815 newpat = gen_br_true (cond, XEXP (cond, 0), dest);
3816 PATTERN (insn) = newpat;
3817 INSN_CODE (insn) = -1;
3818 }
3819 return true;
3820 }
3821 if (INSN_CODE (insn) == CODE_FOR_br_true)
3822 {
3823 rtx br_cond = XEXP (SET_SRC (PATTERN (insn)), 0);
3824 return rtx_equal_p (br_cond, cond);
3825 }
3826 if (INSN_CODE (insn) == CODE_FOR_br_false)
3827 {
3828 rtx br_cond = XEXP (SET_SRC (PATTERN (insn)), 0);
3829 return conditions_opposite_p (br_cond, cond);
3830 }
3831 return false;
3832 }
3833
3834 /* Initialize SC. Used by c6x_init_sched_context and c6x_sched_init. */
3835 static void
3836 init_sched_state (c6x_sched_context_t sc)
3837 {
3838 sc->last_scheduled_insn = NULL;
3839 sc->last_scheduled_iter0 = NULL;
3840 sc->issued_this_cycle = 0;
3841 memset (sc->jump_cycles, 0, sizeof sc->jump_cycles);
3842 memset (sc->jump_cond, 0, sizeof sc->jump_cond);
3843 sc->jump_cycle_index = 0;
3844 sc->delays_finished_at = 0;
3845 sc->curr_sched_clock = 0;
3846
3847 sc->prev_cycle_state_ctx = xmalloc (dfa_state_size);
3848
3849 memset (sc->reg_n_accesses, 0, sizeof sc->reg_n_accesses);
3850 memset (sc->reg_n_xaccesses, 0, sizeof sc->reg_n_xaccesses);
3851 memset (sc->reg_set_in_cycle, 0, sizeof sc->reg_set_in_cycle);
3852
3853 state_reset (sc->prev_cycle_state_ctx);
3854 }
3855
3856 /* Allocate store for new scheduling context. */
3857 static void *
3858 c6x_alloc_sched_context (void)
3859 {
3860 return xmalloc (sizeof (struct c6x_sched_context));
3861 }
3862
3863 /* If CLEAN_P is true then initializes _SC with clean data,
3864 and from the global context otherwise. */
3865 static void
3866 c6x_init_sched_context (void *_sc, bool clean_p)
3867 {
3868 c6x_sched_context_t sc = (c6x_sched_context_t) _sc;
3869
3870 if (clean_p)
3871 {
3872 init_sched_state (sc);
3873 }
3874 else
3875 {
3876 *sc = ss;
3877 sc->prev_cycle_state_ctx = xmalloc (dfa_state_size);
3878 memcpy (sc->prev_cycle_state_ctx, prev_cycle_state, dfa_state_size);
3879 }
3880 }
3881
3882 /* Sets the global scheduling context to the one pointed to by _SC. */
3883 static void
3884 c6x_set_sched_context (void *_sc)
3885 {
3886 c6x_sched_context_t sc = (c6x_sched_context_t) _sc;
3887
3888 gcc_assert (sc != NULL);
3889 ss = *sc;
3890 memcpy (prev_cycle_state, sc->prev_cycle_state_ctx, dfa_state_size);
3891 }
3892
3893 /* Clear data in _SC. */
3894 static void
3895 c6x_clear_sched_context (void *_sc)
3896 {
3897 c6x_sched_context_t sc = (c6x_sched_context_t) _sc;
3898 gcc_assert (_sc != NULL);
3899
3900 free (sc->prev_cycle_state_ctx);
3901 }
3902
3903 /* Free _SC. */
3904 static void
3905 c6x_free_sched_context (void *_sc)
3906 {
3907 free (_sc);
3908 }
3909
3910 /* True if we are currently performing a preliminary scheduling
3911 pass before modulo scheduling; we can't allow the scheduler to
3912 modify instruction patterns using packetization assumptions,
3913 since there will be another scheduling pass later if modulo
3914 scheduling fails. */
3915 static bool in_hwloop;
3916
3917 /* Provide information about speculation capabilities, and set the
3918 DO_BACKTRACKING flag. */
3919 static void
3920 c6x_set_sched_flags (spec_info_t spec_info)
3921 {
3922 unsigned int *flags = &(current_sched_info->flags);
3923
3924 if (*flags & SCHED_EBB)
3925 {
3926 *flags |= DO_BACKTRACKING | DO_PREDICATION;
3927 }
3928 if (in_hwloop)
3929 *flags |= DONT_BREAK_DEPENDENCIES;
3930
3931 spec_info->mask = 0;
3932 }
3933
3934 /* Implement the TARGET_SCHED_ISSUE_RATE hook. */
3935
3936 static int
3937 c6x_issue_rate (void)
3938 {
3939 return 8;
3940 }
3941
3942 /* Used together with the collapse_ndfa option, this ensures that we reach a
3943 deterministic automaton state before trying to advance a cycle.
3944 With collapse_ndfa, genautomata creates advance cycle arcs only for
3945 such deterministic states. */
3946
3947 static rtx
3948 c6x_sched_dfa_pre_cycle_insn (void)
3949 {
3950 return const0_rtx;
3951 }
3952
3953 /* We're beginning a new block. Initialize data structures as necessary. */
3954
3955 static void
3956 c6x_sched_init (FILE *dump ATTRIBUTE_UNUSED,
3957 int sched_verbose ATTRIBUTE_UNUSED,
3958 int max_ready ATTRIBUTE_UNUSED)
3959 {
3960 if (prev_cycle_state == NULL)
3961 {
3962 prev_cycle_state = xmalloc (dfa_state_size);
3963 }
3964 init_sched_state (&ss);
3965 state_reset (prev_cycle_state);
3966 }
3967
3968 /* We are about to being issuing INSN. Return nonzero if we cannot
3969 issue it on given cycle CLOCK and return zero if we should not sort
3970 the ready queue on the next clock start.
3971 For C6X, we use this function just to copy the previous DFA state
3972 for comparison purposes. */
3973
3974 static int
3975 c6x_dfa_new_cycle (FILE *dump ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
3976 rtx_insn *insn ATTRIBUTE_UNUSED,
3977 int last_clock ATTRIBUTE_UNUSED,
3978 int clock ATTRIBUTE_UNUSED, int *sort_p ATTRIBUTE_UNUSED)
3979 {
3980 if (clock != last_clock)
3981 memcpy (prev_cycle_state, curr_state, dfa_state_size);
3982 return 0;
3983 }
3984
3985 static void
3986 c6x_mark_regno_read (int regno, bool cross)
3987 {
3988 int t = ++ss.tmp_reg_n_accesses[regno];
3989
3990 if (t > 4)
3991 reg_access_stall = true;
3992
3993 if (cross)
3994 {
3995 int set_cycle = ss.reg_set_in_cycle[regno];
3996 /* This must be done in this way rather than by tweaking things in
3997 adjust_cost, since the stall occurs even for insns with opposite
3998 predicates, and the scheduler may not even see a dependency. */
3999 if (set_cycle > 0 && set_cycle == ss.curr_sched_clock)
4000 reg_access_stall = true;
4001 /* This doesn't quite do anything yet as we're only modeling one
4002 x unit. */
4003 ++ss.tmp_reg_n_xaccesses[regno];
4004 }
4005 }
4006
4007 /* Note that REG is read in the insn being examined. If CROSS, it
4008 means the access is through a cross path. Update the temporary reg
4009 access arrays, and set REG_ACCESS_STALL if the insn can't be issued
4010 in the current cycle. */
4011
4012 static void
4013 c6x_mark_reg_read (rtx reg, bool cross)
4014 {
4015 unsigned regno = REGNO (reg);
4016 unsigned nregs = REG_NREGS (reg);
4017
4018 while (nregs-- > 0)
4019 c6x_mark_regno_read (regno + nregs, cross);
4020 }
4021
4022 /* Note that register REG is written in cycle CYCLES. */
4023
4024 static void
4025 c6x_mark_reg_written (rtx reg, int cycles)
4026 {
4027 unsigned regno = REGNO (reg);
4028 unsigned nregs = REG_NREGS (reg);
4029
4030 while (nregs-- > 0)
4031 ss.reg_set_in_cycle[regno + nregs] = cycles;
4032 }
4033
4034 /* Update the register state information for an instruction whose
4035 body is X. Return true if the instruction has to be delayed until the
4036 next cycle. */
4037
4038 static bool
4039 c6x_registers_update (rtx_insn *insn)
4040 {
4041 enum attr_cross cross;
4042 enum attr_dest_regfile destrf;
4043 int i, nops;
4044 rtx x;
4045
4046 if (!reload_completed || recog_memoized (insn) < 0)
4047 return false;
4048
4049 reg_access_stall = false;
4050 memcpy (ss.tmp_reg_n_accesses, ss.reg_n_accesses,
4051 sizeof ss.tmp_reg_n_accesses);
4052 memcpy (ss.tmp_reg_n_xaccesses, ss.reg_n_xaccesses,
4053 sizeof ss.tmp_reg_n_xaccesses);
4054
4055 extract_insn (insn);
4056
4057 cross = get_attr_cross (insn);
4058 destrf = get_attr_dest_regfile (insn);
4059
4060 nops = recog_data.n_operands;
4061 x = PATTERN (insn);
4062 if (GET_CODE (x) == COND_EXEC)
4063 {
4064 c6x_mark_reg_read (XEXP (XEXP (x, 0), 0), false);
4065 nops -= 2;
4066 }
4067
4068 for (i = 0; i < nops; i++)
4069 {
4070 rtx op = recog_data.operand[i];
4071 if (recog_data.operand_type[i] == OP_OUT)
4072 continue;
4073 if (REG_P (op))
4074 {
4075 bool this_cross = cross;
4076 if (destrf == DEST_REGFILE_A && A_REGNO_P (REGNO (op)))
4077 this_cross = false;
4078 if (destrf == DEST_REGFILE_B && B_REGNO_P (REGNO (op)))
4079 this_cross = false;
4080 c6x_mark_reg_read (op, this_cross);
4081 }
4082 else if (MEM_P (op))
4083 {
4084 op = XEXP (op, 0);
4085 switch (GET_CODE (op))
4086 {
4087 case POST_INC:
4088 case PRE_INC:
4089 case POST_DEC:
4090 case PRE_DEC:
4091 op = XEXP (op, 0);
4092 /* fall through */
4093 case REG:
4094 c6x_mark_reg_read (op, false);
4095 break;
4096 case POST_MODIFY:
4097 case PRE_MODIFY:
4098 op = XEXP (op, 1);
4099 gcc_assert (GET_CODE (op) == PLUS);
4100 /* fall through */
4101 case PLUS:
4102 c6x_mark_reg_read (XEXP (op, 0), false);
4103 if (REG_P (XEXP (op, 1)))
4104 c6x_mark_reg_read (XEXP (op, 1), false);
4105 break;
4106 case SYMBOL_REF:
4107 case LABEL_REF:
4108 case CONST:
4109 c6x_mark_regno_read (REG_B14, false);
4110 break;
4111 default:
4112 gcc_unreachable ();
4113 }
4114 }
4115 else if (!CONSTANT_P (op) && strlen (recog_data.constraints[i]) > 0)
4116 gcc_unreachable ();
4117 }
4118 return reg_access_stall;
4119 }
4120
4121 /* Helper function for the TARGET_SCHED_REORDER and
4122 TARGET_SCHED_REORDER2 hooks. If scheduling an insn would be unsafe
4123 in the current cycle, move it down in the ready list and return the
4124 number of non-unsafe insns. */
4125
4126 static int
4127 c6x_sched_reorder_1 (rtx_insn **ready, int *pn_ready, int clock_var)
4128 {
4129 int n_ready = *pn_ready;
4130 rtx_insn **e_ready = ready + n_ready;
4131 rtx_insn **insnp;
4132 int first_jump;
4133
4134 /* Keep track of conflicts due to a limit number of register accesses,
4135 and due to stalls incurred by too early accesses of registers using
4136 cross paths. */
4137
4138 for (insnp = ready; insnp < e_ready; insnp++)
4139 {
4140 rtx_insn *insn = *insnp;
4141 int icode = recog_memoized (insn);
4142 bool is_asm = (icode < 0
4143 && (GET_CODE (PATTERN (insn)) == ASM_INPUT
4144 || asm_noperands (PATTERN (insn)) >= 0));
4145 bool no_parallel = (is_asm || icode == CODE_FOR_sploop
4146 || (icode >= 0
4147 && get_attr_type (insn) == TYPE_ATOMIC));
4148
4149 /* We delay asm insns until all delay slots are exhausted. We can't
4150 accurately tell how many cycles an asm takes, and the main scheduling
4151 code always assumes at least 1 cycle, which may be wrong. */
4152 if ((no_parallel
4153 && (ss.issued_this_cycle > 0 || clock_var < ss.delays_finished_at))
4154 || c6x_registers_update (insn)
4155 || (ss.issued_this_cycle > 0 && icode == CODE_FOR_sploop))
4156 {
4157 memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
4158 *ready = insn;
4159 n_ready--;
4160 ready++;
4161 }
4162 else if (shadow_p (insn))
4163 {
4164 memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
4165 *ready = insn;
4166 }
4167 }
4168
4169 /* Ensure that no other jump is scheduled in jump delay slots, since
4170 it would put the machine into the wrong state. Also, we must
4171 avoid scheduling insns that have a latency longer than the
4172 remaining jump delay slots, as the code at the jump destination
4173 won't be prepared for it.
4174
4175 However, we can relax this condition somewhat. The rest of the
4176 scheduler will automatically avoid scheduling an insn on which
4177 the jump shadow depends so late that its side effect happens
4178 after the jump. This means that if we see an insn with a longer
4179 latency here, it can safely be scheduled if we can ensure that it
4180 has a predicate opposite of the previous jump: the side effect
4181 will happen in what we think of as the same basic block. In
4182 c6x_variable_issue, we will record the necessary predicate in
4183 new_conditions, and after scheduling is finished, we will modify
4184 the insn.
4185
4186 Special care must be taken whenever there is more than one jump
4187 in flight. */
4188
4189 first_jump = first_jump_index (clock_var);
4190 if (first_jump != -1)
4191 {
4192 int first_cycle = get_jump_cycle (first_jump);
4193 rtx first_cond = get_jump_cond (first_jump);
4194 int second_cycle = 0;
4195
4196 if (first_jump > 0)
4197 second_cycle = get_jump_cycle (first_jump - 1);
4198
4199 for (insnp = ready; insnp < e_ready; insnp++)
4200 {
4201 rtx_insn *insn = *insnp;
4202 int icode = recog_memoized (insn);
4203 bool is_asm = (icode < 0
4204 && (GET_CODE (PATTERN (insn)) == ASM_INPUT
4205 || asm_noperands (PATTERN (insn)) >= 0));
4206 int this_cycles, rsrv_cycles;
4207 enum attr_type type;
4208
4209 gcc_assert (!is_asm);
4210 if (icode < 0)
4211 continue;
4212 this_cycles = get_attr_cycles (insn);
4213 rsrv_cycles = get_attr_reserve_cycles (insn);
4214 type = get_attr_type (insn);
4215 /* Treat branches specially; there is also a hazard if two jumps
4216 end at the same cycle. */
4217 if (type == TYPE_BRANCH || type == TYPE_CALL)
4218 this_cycles++;
4219 if (clock_var + this_cycles <= first_cycle)
4220 continue;
4221 if ((first_jump > 0 && clock_var + this_cycles > second_cycle)
4222 || clock_var + rsrv_cycles > first_cycle
4223 || !predicate_insn (insn, first_cond, false))
4224 {
4225 memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
4226 *ready = insn;
4227 n_ready--;
4228 ready++;
4229 }
4230 }
4231 }
4232
4233 return n_ready;
4234 }
4235
4236 /* Implement the TARGET_SCHED_REORDER hook. We save the current clock
4237 for later and clear the register access information for the new
4238 cycle. We also move asm statements out of the way if they would be
4239 scheduled in a delay slot. */
4240
4241 static int
4242 c6x_sched_reorder (FILE *dump ATTRIBUTE_UNUSED,
4243 int sched_verbose ATTRIBUTE_UNUSED,
4244 rtx_insn **ready ATTRIBUTE_UNUSED,
4245 int *pn_ready ATTRIBUTE_UNUSED, int clock_var)
4246 {
4247 ss.curr_sched_clock = clock_var;
4248 ss.issued_this_cycle = 0;
4249 memset (ss.reg_n_accesses, 0, sizeof ss.reg_n_accesses);
4250 memset (ss.reg_n_xaccesses, 0, sizeof ss.reg_n_xaccesses);
4251
4252 if (ready == NULL)
4253 return 0;
4254
4255 return c6x_sched_reorder_1 (ready, pn_ready, clock_var);
4256 }
4257
4258 /* Implement the TARGET_SCHED_REORDER2 hook. We use this to record the clock
4259 cycle for every insn. */
4260
4261 static int
4262 c6x_sched_reorder2 (FILE *dump ATTRIBUTE_UNUSED,
4263 int sched_verbose ATTRIBUTE_UNUSED,
4264 rtx_insn **ready ATTRIBUTE_UNUSED,
4265 int *pn_ready ATTRIBUTE_UNUSED, int clock_var)
4266 {
4267 /* FIXME: the assembler rejects labels inside an execute packet.
4268 This can occur if prologue insns are scheduled in parallel with
4269 others, so we avoid this here. Also make sure that nothing is
4270 scheduled in parallel with a TYPE_ATOMIC insn or after a jump. */
4271 if (RTX_FRAME_RELATED_P (ss.last_scheduled_insn)
4272 || JUMP_P (ss.last_scheduled_insn)
4273 || (recog_memoized (ss.last_scheduled_insn) >= 0
4274 && get_attr_type (ss.last_scheduled_insn) == TYPE_ATOMIC))
4275 {
4276 int n_ready = *pn_ready;
4277 rtx_insn **e_ready = ready + n_ready;
4278 rtx_insn **insnp;
4279
4280 for (insnp = ready; insnp < e_ready; insnp++)
4281 {
4282 rtx_insn *insn = *insnp;
4283 if (!shadow_p (insn))
4284 {
4285 memmove (ready + 1, ready, (insnp - ready) * sizeof (rtx));
4286 *ready = insn;
4287 n_ready--;
4288 ready++;
4289 }
4290 }
4291 return n_ready;
4292 }
4293
4294 return c6x_sched_reorder_1 (ready, pn_ready, clock_var);
4295 }
4296
4297 /* Subroutine of maybe_clobber_cond, called through note_stores. */
4298
4299 static void
4300 clobber_cond_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data1)
4301 {
4302 rtx *cond = (rtx *)data1;
4303 if (*cond != NULL_RTX && reg_overlap_mentioned_p (x, *cond))
4304 *cond = NULL_RTX;
4305 }
4306
4307 /* Examine INSN, and if it destroys the conditions have recorded for
4308 any of the jumps in flight, clear that condition so that we don't
4309 predicate any more insns. CLOCK_VAR helps us limit the search to
4310 only those jumps which are still in flight. */
4311
4312 static void
4313 maybe_clobber_cond (rtx_insn *insn, int clock_var)
4314 {
4315 int n, idx;
4316 idx = ss.jump_cycle_index;
4317 for (n = 0; n < 12; n++, idx++)
4318 {
4319 rtx cond, link;
4320 int cycle;
4321
4322 if (idx >= 12)
4323 idx -= 12;
4324 cycle = ss.jump_cycles[idx];
4325 if (cycle <= clock_var)
4326 return;
4327
4328 cond = ss.jump_cond[idx];
4329 if (cond == NULL_RTX)
4330 continue;
4331
4332 if (CALL_P (insn))
4333 {
4334 ss.jump_cond[idx] = NULL_RTX;
4335 continue;
4336 }
4337
4338 note_stores (insn, clobber_cond_1, ss.jump_cond + idx);
4339 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
4340 if (REG_NOTE_KIND (link) == REG_INC)
4341 clobber_cond_1 (XEXP (link, 0), NULL_RTX, ss.jump_cond + idx);
4342 }
4343 }
4344
4345 /* Implement the TARGET_SCHED_VARIABLE_ISSUE hook. We are about to
4346 issue INSN. Return the number of insns left on the ready queue
4347 that can be issued this cycle.
4348 We use this hook to record clock cycles and reservations for every insn. */
4349
4350 static int
4351 c6x_variable_issue (FILE *dump ATTRIBUTE_UNUSED,
4352 int sched_verbose ATTRIBUTE_UNUSED,
4353 rtx_insn *insn, int can_issue_more ATTRIBUTE_UNUSED)
4354 {
4355 ss.last_scheduled_insn = insn;
4356 if (INSN_UID (insn) < sploop_max_uid_iter0 && !JUMP_P (insn))
4357 ss.last_scheduled_iter0 = insn;
4358 if (GET_CODE (PATTERN (insn)) != USE && GET_CODE (PATTERN (insn)) != CLOBBER)
4359 ss.issued_this_cycle++;
4360 if (insn_info.exists ())
4361 {
4362 state_t st_after = alloca (dfa_state_size);
4363 int curr_clock = ss.curr_sched_clock;
4364 int uid = INSN_UID (insn);
4365 int icode = recog_memoized (insn);
4366 rtx first_cond;
4367 int first, first_cycle;
4368 unsigned int mask;
4369 int i;
4370
4371 insn_set_clock (insn, curr_clock);
4372 INSN_INFO_ENTRY (uid).ebb_start
4373 = curr_clock == 0 && ss.issued_this_cycle == 1;
4374
4375 first = first_jump_index (ss.curr_sched_clock);
4376 if (first == -1)
4377 {
4378 first_cycle = 0;
4379 first_cond = NULL_RTX;
4380 }
4381 else
4382 {
4383 first_cycle = get_jump_cycle (first);
4384 first_cond = get_jump_cond (first);
4385 }
4386 if (icode >= 0
4387 && first_cycle > curr_clock
4388 && first_cond != NULL_RTX
4389 && (curr_clock + get_attr_cycles (insn) > first_cycle
4390 || get_attr_type (insn) == TYPE_BRANCH
4391 || get_attr_type (insn) == TYPE_CALL))
4392 INSN_INFO_ENTRY (uid).new_cond = first_cond;
4393
4394 memcpy (st_after, curr_state, dfa_state_size);
4395 state_transition (st_after, const0_rtx);
4396
4397 mask = 0;
4398 for (i = 0; i < 2 * UNIT_QID_SIDE_OFFSET; i++)
4399 if (cpu_unit_reservation_p (st_after, c6x_unit_codes[i])
4400 && !cpu_unit_reservation_p (prev_cycle_state, c6x_unit_codes[i]))
4401 mask |= 1 << i;
4402 INSN_INFO_ENTRY (uid).unit_mask = mask;
4403
4404 maybe_clobber_cond (insn, curr_clock);
4405
4406 if (icode >= 0)
4407 {
4408 int i, cycles;
4409
4410 c6x_registers_update (insn);
4411 memcpy (ss.reg_n_accesses, ss.tmp_reg_n_accesses,
4412 sizeof ss.reg_n_accesses);
4413 memcpy (ss.reg_n_xaccesses, ss.tmp_reg_n_accesses,
4414 sizeof ss.reg_n_xaccesses);
4415
4416 cycles = get_attr_cycles (insn);
4417 if (ss.delays_finished_at < ss.curr_sched_clock + cycles)
4418 ss.delays_finished_at = ss.curr_sched_clock + cycles;
4419 if (get_attr_type (insn) == TYPE_BRANCH
4420 || get_attr_type (insn) == TYPE_CALL)
4421 {
4422 rtx opposite = condjump_opposite_condition (insn);
4423 record_jump (ss.curr_sched_clock + cycles, opposite);
4424 }
4425
4426 /* Mark the cycles in which the destination registers are written.
4427 This is used for calculating stalls when using cross units. */
4428 extract_insn (insn);
4429 /* Cross-path stalls don't apply to results of load insns. */
4430 if (get_attr_type (insn) == TYPE_LOAD
4431 || get_attr_type (insn) == TYPE_LOADN
4432 || get_attr_type (insn) == TYPE_LOAD_SHADOW)
4433 cycles--;
4434 for (i = 0; i < recog_data.n_operands; i++)
4435 {
4436 rtx op = recog_data.operand[i];
4437 if (MEM_P (op))
4438 {
4439 rtx addr = XEXP (op, 0);
4440 if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
4441 c6x_mark_reg_written (XEXP (addr, 0),
4442 insn_uid_get_clock (uid) + 1);
4443 }
4444 if (recog_data.operand_type[i] != OP_IN
4445 && REG_P (op))
4446 {
4447 c6x_mark_reg_written (op,
4448 insn_uid_get_clock (uid) + cycles);
4449 }
4450 }
4451 }
4452 }
4453 return can_issue_more;
4454 }
4455
4456 /* Implement the TARGET_SCHED_ADJUST_COST hook. We need special handling for
4457 anti- and output dependencies. */
4458
4459 static int
4460 c6x_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn, int cost,
4461 unsigned int)
4462 {
4463 enum attr_type insn_type = TYPE_UNKNOWN, dep_insn_type = TYPE_UNKNOWN;
4464 int dep_insn_code_number, insn_code_number;
4465 int shadow_bonus = 0;
4466 enum reg_note kind;
4467 dep_insn_code_number = recog_memoized (dep_insn);
4468 insn_code_number = recog_memoized (insn);
4469
4470 if (dep_insn_code_number >= 0)
4471 dep_insn_type = get_attr_type (dep_insn);
4472
4473 if (insn_code_number >= 0)
4474 insn_type = get_attr_type (insn);
4475
4476 kind = (reg_note) dep_type;
4477 if (kind == 0)
4478 {
4479 /* If we have a dependency on a load, and it's not for the result of
4480 the load, it must be for an autoincrement. Reduce the cost in that
4481 case. */
4482 if (dep_insn_type == TYPE_LOAD)
4483 {
4484 rtx set = PATTERN (dep_insn);
4485 if (GET_CODE (set) == COND_EXEC)
4486 set = COND_EXEC_CODE (set);
4487 if (GET_CODE (set) == UNSPEC)
4488 cost = 1;
4489 else
4490 {
4491 gcc_assert (GET_CODE (set) == SET);
4492 if (!reg_overlap_mentioned_p (SET_DEST (set), PATTERN (insn)))
4493 cost = 1;
4494 }
4495 }
4496 }
4497
4498 /* A jump shadow needs to have its latency decreased by one. Conceptually,
4499 it occurs in between two cycles, but we schedule it at the end of the
4500 first cycle. */
4501 if (shadow_type_p (insn_type))
4502 shadow_bonus = 1;
4503
4504 /* Anti and output dependencies usually have zero cost, but we want
4505 to insert a stall after a jump, and after certain floating point
4506 insns that take more than one cycle to read their inputs. In the
4507 future, we should try to find a better algorithm for scheduling
4508 jumps. */
4509 if (kind != 0)
4510 {
4511 /* We can get anti-dependencies against shadow insns. Treat these
4512 like output dependencies, so that the insn is entirely finished
4513 before the branch takes place. */
4514 if (kind == REG_DEP_ANTI && insn_type == TYPE_SHADOW)
4515 kind = REG_DEP_OUTPUT;
4516 switch (dep_insn_type)
4517 {
4518 case TYPE_CALLP:
4519 return 1;
4520 case TYPE_BRANCH:
4521 case TYPE_CALL:
4522 if (get_attr_has_shadow (dep_insn) == HAS_SHADOW_Y)
4523 /* This is a real_jump/real_call insn. These don't have
4524 outputs, and ensuring the validity of scheduling things
4525 in the delay slot is the job of
4526 c6x_sched_reorder_1. */
4527 return 0;
4528 /* Unsplit calls can happen - e.g. for divide insns. */
4529 return 6;
4530 case TYPE_LOAD:
4531 case TYPE_LOADN:
4532 case TYPE_INTDP:
4533 if (kind == REG_DEP_OUTPUT)
4534 return 5 - shadow_bonus;
4535 return 0;
4536 case TYPE_MPY4:
4537 case TYPE_FP4:
4538 if (kind == REG_DEP_OUTPUT)
4539 return 4 - shadow_bonus;
4540 return 0;
4541 case TYPE_MPY2:
4542 if (kind == REG_DEP_OUTPUT)
4543 return 2 - shadow_bonus;
4544 return 0;
4545 case TYPE_CMPDP:
4546 if (kind == REG_DEP_OUTPUT)
4547 return 2 - shadow_bonus;
4548 return 2;
4549 case TYPE_ADDDP:
4550 case TYPE_MPYSPDP:
4551 if (kind == REG_DEP_OUTPUT)
4552 return 7 - shadow_bonus;
4553 return 2;
4554 case TYPE_MPYSP2DP:
4555 if (kind == REG_DEP_OUTPUT)
4556 return 5 - shadow_bonus;
4557 return 2;
4558 case TYPE_MPYI:
4559 if (kind == REG_DEP_OUTPUT)
4560 return 9 - shadow_bonus;
4561 return 4;
4562 case TYPE_MPYID:
4563 case TYPE_MPYDP:
4564 if (kind == REG_DEP_OUTPUT)
4565 return 10 - shadow_bonus;
4566 return 4;
4567
4568 default:
4569 if (insn_type == TYPE_SPKERNEL)
4570 return 0;
4571 if (kind == REG_DEP_OUTPUT)
4572 return 1 - shadow_bonus;
4573
4574 return 0;
4575 }
4576 }
4577
4578 return cost - shadow_bonus;
4579 }
4580
4581 /* Create a SEQUENCE rtx to replace the instructions in SLOT, of which there
4582 are N_FILLED. REAL_FIRST identifies the slot if the insn that appears
4583 first in the original stream. */
4584
4585 static void
4586 gen_one_bundle (rtx_insn **slot, int n_filled, int real_first)
4587 {
4588 rtx seq;
4589 rtx_insn *bundle;
4590 rtx_insn *t;
4591 int i;
4592
4593 seq = gen_rtx_SEQUENCE (VOIDmode, gen_rtvec_v (n_filled, slot));
4594 bundle = make_insn_raw (seq);
4595 BLOCK_FOR_INSN (bundle) = BLOCK_FOR_INSN (slot[0]);
4596 INSN_LOCATION (bundle) = INSN_LOCATION (slot[0]);
4597 SET_PREV_INSN (bundle) = SET_PREV_INSN (slot[real_first]);
4598
4599 t = NULL;
4600
4601 for (i = 0; i < n_filled; i++)
4602 {
4603 rtx_insn *insn = slot[i];
4604 remove_insn (insn);
4605 SET_PREV_INSN (insn) = t ? t : PREV_INSN (bundle);
4606 if (t != NULL_RTX)
4607 SET_NEXT_INSN (t) = insn;
4608 t = insn;
4609 if (i > 0)
4610 INSN_LOCATION (slot[i]) = INSN_LOCATION (bundle);
4611 }
4612
4613 SET_NEXT_INSN (bundle) = NEXT_INSN (PREV_INSN (bundle));
4614 SET_NEXT_INSN (t) = NEXT_INSN (bundle);
4615 SET_NEXT_INSN (PREV_INSN (bundle)) = bundle;
4616 SET_PREV_INSN (NEXT_INSN (bundle)) = bundle;
4617 }
4618
4619 /* Move all parallel instructions into SEQUENCEs, so that no subsequent passes
4620 try to insert labels in the middle. */
4621
4622 static void
4623 c6x_gen_bundles (void)
4624 {
4625 basic_block bb;
4626
4627 FOR_EACH_BB_FN (bb, cfun)
4628 {
4629 rtx_insn *insn, *next;
4630 /* The machine is eight insns wide. We can have up to six shadow
4631 insns, plus an extra slot for merging the jump shadow. */
4632 rtx_insn *slot[15];
4633 int n_filled = 0;
4634 int first_slot = 0;
4635
4636 for (insn = BB_HEAD (bb);; insn = next)
4637 {
4638 int at_end;
4639 rtx delete_this = NULL_RTX;
4640
4641 if (NONDEBUG_INSN_P (insn))
4642 {
4643 /* Put calls at the start of the sequence. */
4644 if (CALL_P (insn))
4645 {
4646 first_slot++;
4647 if (n_filled)
4648 {
4649 memmove (&slot[1], &slot[0],
4650 n_filled * sizeof (slot[0]));
4651 }
4652 if (!shadow_p (insn))
4653 {
4654 PUT_MODE (insn, TImode);
4655 if (n_filled)
4656 PUT_MODE (slot[1], VOIDmode);
4657 }
4658 n_filled++;
4659 slot[0] = insn;
4660 }
4661 else
4662 {
4663 slot[n_filled++] = insn;
4664 }
4665 }
4666
4667 next = NEXT_INSN (insn);
4668 while (next && insn != BB_END (bb)
4669 && !(NONDEBUG_INSN_P (next)
4670 && GET_CODE (PATTERN (next)) != USE
4671 && GET_CODE (PATTERN (next)) != CLOBBER))
4672 {
4673 insn = next;
4674 next = NEXT_INSN (insn);
4675 }
4676
4677 at_end = insn == BB_END (bb);
4678 if (delete_this == NULL_RTX
4679 && (at_end || (GET_MODE (next) == TImode
4680 && !(shadow_p (next) && CALL_P (next)))))
4681 {
4682 if (n_filled >= 2)
4683 gen_one_bundle (slot, n_filled, first_slot);
4684
4685 n_filled = 0;
4686 first_slot = 0;
4687 }
4688 if (at_end)
4689 break;
4690 }
4691 }
4692 }
4693
4694 /* Emit a NOP instruction for CYCLES cycles after insn AFTER. Return it. */
4695
4696 static rtx_insn *
4697 emit_nop_after (int cycles, rtx_insn *after)
4698 {
4699 rtx_insn *insn;
4700
4701 /* mpydp has 9 delay slots, and we may schedule a stall for a cross-path
4702 operation. We don't need the extra NOP since in this case, the hardware
4703 will automatically insert the required stall. */
4704 if (cycles == 10)
4705 cycles--;
4706
4707 gcc_assert (cycles < 10);
4708
4709 insn = emit_insn_after (gen_nop_count (GEN_INT (cycles)), after);
4710 PUT_MODE (insn, TImode);
4711
4712 return insn;
4713 }
4714
4715 /* Determine whether INSN is a call that needs to have a return label
4716 placed. */
4717
4718 static bool
4719 returning_call_p (rtx_insn *insn)
4720 {
4721 if (CALL_P (insn))
4722 return (!SIBLING_CALL_P (insn)
4723 && get_attr_type (insn) != TYPE_CALLP
4724 && get_attr_type (insn) != TYPE_SHADOW);
4725 if (recog_memoized (insn) < 0)
4726 return false;
4727 if (get_attr_type (insn) == TYPE_CALL)
4728 return true;
4729 return false;
4730 }
4731
4732 /* Determine whether INSN's pattern can be converted to use callp. */
4733 static bool
4734 can_use_callp (rtx_insn *insn)
4735 {
4736 int icode = recog_memoized (insn);
4737 if (!TARGET_INSNS_64PLUS
4738 || icode < 0
4739 || GET_CODE (PATTERN (insn)) == COND_EXEC)
4740 return false;
4741
4742 return ((icode == CODE_FOR_real_call
4743 || icode == CODE_FOR_call_internal
4744 || icode == CODE_FOR_call_value_internal)
4745 && get_attr_dest_regfile (insn) == DEST_REGFILE_ANY);
4746 }
4747
4748 /* Convert the pattern of INSN, which must be a CALL_INSN, into a callp. */
4749 static void
4750 convert_to_callp (rtx_insn *insn)
4751 {
4752 rtx lab;
4753 extract_insn (insn);
4754 if (GET_CODE (PATTERN (insn)) == SET)
4755 {
4756 rtx dest = recog_data.operand[0];
4757 lab = recog_data.operand[1];
4758 PATTERN (insn) = gen_callp_value (dest, lab);
4759 INSN_CODE (insn) = CODE_FOR_callp_value;
4760 }
4761 else
4762 {
4763 lab = recog_data.operand[0];
4764 PATTERN (insn) = gen_callp (lab);
4765 INSN_CODE (insn) = CODE_FOR_callp;
4766 }
4767 }
4768
4769 /* Scan forwards from INSN until we find the next insn that has mode TImode
4770 (indicating it starts a new cycle), and occurs in cycle CLOCK.
4771 Return it if we find such an insn, NULL_RTX otherwise. */
4772 static rtx_insn *
4773 find_next_cycle_insn (rtx_insn *insn, int clock)
4774 {
4775 rtx_insn *t = insn;
4776 if (GET_MODE (t) == TImode)
4777 t = next_real_insn (t);
4778 while (t && GET_MODE (t) != TImode)
4779 t = next_real_insn (t);
4780
4781 if (t && insn_get_clock (t) == clock)
4782 return t;
4783 return NULL;
4784 }
4785
4786 /* If COND_INSN has a COND_EXEC condition, wrap the same condition
4787 around PAT. Return PAT either unchanged or modified in this
4788 way. */
4789 static rtx
4790 duplicate_cond (rtx pat, rtx cond_insn)
4791 {
4792 rtx cond_pat = PATTERN (cond_insn);
4793 if (GET_CODE (cond_pat) == COND_EXEC)
4794 pat = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (COND_EXEC_TEST (cond_pat)),
4795 pat);
4796 return pat;
4797 }
4798
4799 /* Walk forward from INSN to find the last insn that issues in the same clock
4800 cycle. */
4801 static rtx_insn *
4802 find_last_same_clock (rtx_insn *insn)
4803 {
4804 rtx_insn *retval = insn;
4805 rtx_insn *t = next_real_insn (insn);
4806
4807 while (t && GET_MODE (t) != TImode)
4808 {
4809 if (!DEBUG_INSN_P (t) && recog_memoized (t) >= 0)
4810 retval = t;
4811 t = next_real_insn (t);
4812 }
4813 return retval;
4814 }
4815
4816 /* For every call insn in the function, emit code to load the return
4817 address. For each call we create a return label and store it in
4818 CALL_LABELS. If are not scheduling, we emit the labels here,
4819 otherwise the caller will do it later.
4820 This function is called after final insn scheduling, but before creating
4821 the SEQUENCEs that represent execute packets. */
4822
4823 static void
4824 reorg_split_calls (rtx_code_label **call_labels)
4825 {
4826 unsigned int reservation_mask = 0;
4827 rtx_insn *insn = get_insns ();
4828 gcc_assert (NOTE_P (insn));
4829 insn = next_real_insn (insn);
4830 while (insn)
4831 {
4832 int uid;
4833 rtx_insn *next = next_real_insn (insn);
4834
4835 if (DEBUG_INSN_P (insn))
4836 goto done;
4837
4838 if (GET_MODE (insn) == TImode)
4839 reservation_mask = 0;
4840 uid = INSN_UID (insn);
4841 if (c6x_flag_schedule_insns2 && recog_memoized (insn) >= 0)
4842 reservation_mask |= 1 << INSN_INFO_ENTRY (uid).reservation;
4843
4844 if (returning_call_p (insn))
4845 {
4846 rtx_code_label *label = gen_label_rtx ();
4847 rtx labelref = gen_rtx_LABEL_REF (Pmode, label);
4848 rtx reg = gen_rtx_REG (SImode, RETURN_ADDR_REGNO);
4849
4850 LABEL_NUSES (label) = 2;
4851 if (!c6x_flag_schedule_insns2)
4852 {
4853 if (can_use_callp (insn))
4854 convert_to_callp (insn);
4855 else
4856 {
4857 rtx t;
4858 rtx_insn *slot[4];
4859 emit_label_after (label, insn);
4860
4861 /* Bundle the call and its delay slots into a single
4862 SEQUENCE. While these do not issue in parallel
4863 we need to group them into a single EH region. */
4864 slot[0] = insn;
4865 PUT_MODE (insn, TImode);
4866 if (TARGET_INSNS_64)
4867 {
4868 t = gen_addkpc (reg, labelref, GEN_INT (4));
4869 slot[1] = emit_insn_after (duplicate_cond (t, insn),
4870 insn);
4871 PUT_MODE (slot[1], TImode);
4872 gen_one_bundle (slot, 2, 0);
4873 }
4874 else
4875 {
4876 slot[3] = emit_insn_after (gen_nop_count (GEN_INT (3)),
4877 insn);
4878 PUT_MODE (slot[3], TImode);
4879 t = gen_movsi_lo_sum (reg, reg, labelref);
4880 slot[2] = emit_insn_after (duplicate_cond (t, insn),
4881 insn);
4882 PUT_MODE (slot[2], TImode);
4883 t = gen_movsi_high (reg, labelref);
4884 slot[1] = emit_insn_after (duplicate_cond (t, insn),
4885 insn);
4886 PUT_MODE (slot[1], TImode);
4887 gen_one_bundle (slot, 4, 0);
4888 }
4889 }
4890 }
4891 else
4892 {
4893 /* If we scheduled, we reserved the .S2 unit for one or two
4894 cycles after the call. Emit the insns in these slots,
4895 unless it's possible to create a CALLP insn.
4896 Note that this works because the dependencies ensure that
4897 no insn setting/using B3 is scheduled in the delay slots of
4898 a call. */
4899 int this_clock = insn_get_clock (insn);
4900 rtx_insn *after1;
4901
4902 call_labels[INSN_UID (insn)] = label;
4903
4904 rtx_insn *last_same_clock = find_last_same_clock (insn);
4905
4906 if (can_use_callp (insn))
4907 {
4908 /* Find the first insn of the next execute packet. If it
4909 is the shadow insn corresponding to this call, we may
4910 use a CALLP insn. */
4911 rtx_insn *shadow =
4912 next_nonnote_nondebug_insn (last_same_clock);
4913
4914 if (CALL_P (shadow)
4915 && insn_get_clock (shadow) == this_clock + 5)
4916 {
4917 convert_to_callp (shadow);
4918 insn_set_clock (shadow, this_clock);
4919 INSN_INFO_ENTRY (INSN_UID (shadow)).reservation
4920 = RESERVATION_S2;
4921 INSN_INFO_ENTRY (INSN_UID (shadow)).unit_mask
4922 = INSN_INFO_ENTRY (INSN_UID (last_same_clock)).unit_mask;
4923 if (GET_MODE (insn) == TImode)
4924 {
4925 rtx_insn *new_cycle_first = NEXT_INSN (insn);
4926 while (!NONDEBUG_INSN_P (new_cycle_first)
4927 || GET_CODE (PATTERN (new_cycle_first)) == USE
4928 || GET_CODE (PATTERN (new_cycle_first)) == CLOBBER)
4929 new_cycle_first = NEXT_INSN (new_cycle_first);
4930 PUT_MODE (new_cycle_first, TImode);
4931 if (new_cycle_first != shadow)
4932 PUT_MODE (shadow, VOIDmode);
4933 INSN_INFO_ENTRY (INSN_UID (new_cycle_first)).ebb_start
4934 = INSN_INFO_ENTRY (INSN_UID (insn)).ebb_start;
4935 }
4936 else
4937 PUT_MODE (shadow, VOIDmode);
4938 delete_insn (insn);
4939 goto done;
4940 }
4941 }
4942 after1 = find_next_cycle_insn (last_same_clock, this_clock + 1);
4943 if (after1 == NULL_RTX)
4944 after1 = last_same_clock;
4945 else
4946 after1 = find_last_same_clock (after1);
4947 if (TARGET_INSNS_64)
4948 {
4949 rtx x1 = gen_addkpc (reg, labelref, const0_rtx);
4950 x1 = emit_insn_after (duplicate_cond (x1, insn), after1);
4951 insn_set_clock (x1, this_clock + 1);
4952 INSN_INFO_ENTRY (INSN_UID (x1)).reservation = RESERVATION_S2;
4953 if (after1 == last_same_clock)
4954 PUT_MODE (x1, TImode);
4955 else
4956 INSN_INFO_ENTRY (INSN_UID (x1)).unit_mask
4957 = INSN_INFO_ENTRY (INSN_UID (after1)).unit_mask;
4958 }
4959 else
4960 {
4961 rtx x1, x2;
4962 rtx_insn *after2 = find_next_cycle_insn (after1,
4963 this_clock + 2);
4964 if (after2 == NULL_RTX)
4965 after2 = after1;
4966 x2 = gen_movsi_lo_sum (reg, reg, labelref);
4967 x2 = emit_insn_after (duplicate_cond (x2, insn), after2);
4968 x1 = gen_movsi_high (reg, labelref);
4969 x1 = emit_insn_after (duplicate_cond (x1, insn), after1);
4970 insn_set_clock (x1, this_clock + 1);
4971 insn_set_clock (x2, this_clock + 2);
4972 INSN_INFO_ENTRY (INSN_UID (x1)).reservation = RESERVATION_S2;
4973 INSN_INFO_ENTRY (INSN_UID (x2)).reservation = RESERVATION_S2;
4974 if (after1 == last_same_clock)
4975 PUT_MODE (x1, TImode);
4976 else
4977 INSN_INFO_ENTRY (INSN_UID (x1)).unit_mask
4978 = INSN_INFO_ENTRY (INSN_UID (after1)).unit_mask;
4979 if (after1 == after2)
4980 PUT_MODE (x2, TImode);
4981 else
4982 INSN_INFO_ENTRY (INSN_UID (x2)).unit_mask
4983 = INSN_INFO_ENTRY (INSN_UID (after2)).unit_mask;
4984 }
4985 }
4986 }
4987 done:
4988 insn = next;
4989 }
4990 }
4991
4992 /* Called as part of c6x_reorg. This function emits multi-cycle NOP
4993 insns as required for correctness. CALL_LABELS is the array that
4994 holds the return labels for call insns; we emit these here if
4995 scheduling was run earlier. */
4996
4997 static void
4998 reorg_emit_nops (rtx_code_label **call_labels)
4999 {
5000 bool first;
5001 rtx last_call;
5002 rtx_insn *prev;
5003 int prev_clock, earliest_bb_end;
5004 int prev_implicit_nops;
5005 rtx_insn *insn = get_insns ();
5006
5007 /* We look at one insn (or bundle inside a sequence) in each iteration, storing
5008 its issue time in PREV_CLOCK for the next iteration. If there is a gap in
5009 clocks, we must insert a NOP.
5010 EARLIEST_BB_END tracks in which cycle all insns that have been issued in the
5011 current basic block will finish. We must not allow the next basic block to
5012 begin before this cycle.
5013 PREV_IMPLICIT_NOPS tells us whether we've seen an insn that implicitly contains
5014 a multi-cycle nop. The code is scheduled such that subsequent insns will
5015 show the cycle gap, but we needn't insert a real NOP instruction. */
5016 insn = next_real_insn (insn);
5017 last_call = prev = NULL;
5018 prev_clock = -1;
5019 earliest_bb_end = 0;
5020 prev_implicit_nops = 0;
5021 first = true;
5022 while (insn)
5023 {
5024 int this_clock = -1;
5025 rtx_insn *next;
5026 int max_cycles = 0;
5027
5028 next = next_real_insn (insn);
5029
5030 if (DEBUG_INSN_P (insn)
5031 || GET_CODE (PATTERN (insn)) == USE
5032 || GET_CODE (PATTERN (insn)) == CLOBBER
5033 || shadow_or_blockage_p (insn)
5034 || JUMP_TABLE_DATA_P (insn))
5035 goto next_insn;
5036
5037 if (!c6x_flag_schedule_insns2)
5038 /* No scheduling; ensure that no parallel issue happens. */
5039 PUT_MODE (insn, TImode);
5040 else
5041 {
5042 int cycles;
5043
5044 this_clock = insn_get_clock (insn);
5045 if (this_clock != prev_clock)
5046 {
5047 PUT_MODE (insn, TImode);
5048
5049 if (!first)
5050 {
5051 cycles = this_clock - prev_clock;
5052
5053 cycles -= prev_implicit_nops;
5054 if (cycles > 1)
5055 {
5056 rtx nop = emit_nop_after (cycles - 1, prev);
5057 insn_set_clock (nop, prev_clock + prev_implicit_nops + 1);
5058 }
5059 }
5060 prev_clock = this_clock;
5061
5062 if (last_call
5063 && insn_get_clock (last_call) + 6 <= this_clock)
5064 {
5065 emit_label_before (call_labels[INSN_UID (last_call)], insn);
5066 last_call = NULL_RTX;
5067 }
5068 prev_implicit_nops = 0;
5069 }
5070 }
5071
5072 /* Examine how many cycles the current insn takes, and adjust
5073 LAST_CALL, EARLIEST_BB_END and PREV_IMPLICIT_NOPS. */
5074 if (recog_memoized (insn) >= 0
5075 /* If not scheduling, we've emitted NOPs after calls already. */
5076 && (c6x_flag_schedule_insns2 || !returning_call_p (insn)))
5077 {
5078 max_cycles = get_attr_cycles (insn);
5079 if (get_attr_type (insn) == TYPE_CALLP)
5080 prev_implicit_nops = 5;
5081 }
5082 else
5083 max_cycles = 1;
5084 if (returning_call_p (insn))
5085 last_call = insn;
5086
5087 if (c6x_flag_schedule_insns2)
5088 {
5089 gcc_assert (this_clock >= 0);
5090 if (earliest_bb_end < this_clock + max_cycles)
5091 earliest_bb_end = this_clock + max_cycles;
5092 }
5093 else if (max_cycles > 1)
5094 emit_nop_after (max_cycles - 1, insn);
5095
5096 prev = insn;
5097 first = false;
5098
5099 next_insn:
5100 if (c6x_flag_schedule_insns2
5101 && (next == NULL_RTX
5102 || (GET_MODE (next) == TImode
5103 && INSN_INFO_ENTRY (INSN_UID (next)).ebb_start))
5104 && earliest_bb_end > 0)
5105 {
5106 int cycles = earliest_bb_end - prev_clock;
5107 if (cycles > 1)
5108 {
5109 prev = emit_nop_after (cycles - 1, prev);
5110 insn_set_clock (prev, prev_clock + prev_implicit_nops + 1);
5111 }
5112 earliest_bb_end = 0;
5113 prev_clock = -1;
5114 first = true;
5115
5116 if (last_call)
5117 emit_label_after (call_labels[INSN_UID (last_call)], prev);
5118 last_call = NULL_RTX;
5119 }
5120 insn = next;
5121 }
5122 }
5123
5124 /* If possible, split INSN, which we know is either a jump or a call, into a real
5125 insn and its shadow. */
5126 static void
5127 split_delayed_branch (rtx_insn *insn)
5128 {
5129 int code = recog_memoized (insn);
5130 rtx_insn *i1;
5131 rtx newpat;
5132 rtx pat = PATTERN (insn);
5133
5134 if (GET_CODE (pat) == COND_EXEC)
5135 pat = COND_EXEC_CODE (pat);
5136
5137 if (CALL_P (insn))
5138 {
5139 rtx src = pat, dest = NULL_RTX;
5140 rtx callee;
5141 if (GET_CODE (pat) == SET)
5142 {
5143 dest = SET_DEST (pat);
5144 src = SET_SRC (pat);
5145 }
5146 callee = XEXP (XEXP (src, 0), 0);
5147 if (SIBLING_CALL_P (insn))
5148 {
5149 if (REG_P (callee))
5150 newpat = gen_indirect_sibcall_shadow ();
5151 else
5152 newpat = gen_sibcall_shadow (callee);
5153 pat = gen_real_jump (callee);
5154 }
5155 else if (dest != NULL_RTX)
5156 {
5157 if (REG_P (callee))
5158 newpat = gen_indirect_call_value_shadow (dest);
5159 else
5160 newpat = gen_call_value_shadow (dest, callee);
5161 pat = gen_real_call (callee);
5162 }
5163 else
5164 {
5165 if (REG_P (callee))
5166 newpat = gen_indirect_call_shadow ();
5167 else
5168 newpat = gen_call_shadow (callee);
5169 pat = gen_real_call (callee);
5170 }
5171 pat = duplicate_cond (pat, insn);
5172 newpat = duplicate_cond (newpat, insn);
5173 }
5174 else
5175 {
5176 rtx src, op;
5177 if (GET_CODE (pat) == PARALLEL
5178 && GET_CODE (XVECEXP (pat, 0, 0)) == RETURN)
5179 {
5180 newpat = gen_return_shadow ();
5181 pat = gen_real_ret (XEXP (XVECEXP (pat, 0, 1), 0));
5182 newpat = duplicate_cond (newpat, insn);
5183 }
5184 else
5185 switch (code)
5186 {
5187 case CODE_FOR_br_true:
5188 case CODE_FOR_br_false:
5189 src = SET_SRC (pat);
5190 op = XEXP (src, code == CODE_FOR_br_true ? 1 : 2);
5191 newpat = gen_condjump_shadow (op);
5192 pat = gen_real_jump (op);
5193 if (code == CODE_FOR_br_true)
5194 pat = gen_rtx_COND_EXEC (VOIDmode, XEXP (src, 0), pat);
5195 else
5196 pat = gen_rtx_COND_EXEC (VOIDmode,
5197 reversed_comparison (XEXP (src, 0),
5198 VOIDmode),
5199 pat);
5200 break;
5201
5202 case CODE_FOR_jump:
5203 op = SET_SRC (pat);
5204 newpat = gen_jump_shadow (op);
5205 break;
5206
5207 case CODE_FOR_indirect_jump:
5208 newpat = gen_indirect_jump_shadow ();
5209 break;
5210
5211 case CODE_FOR_return_internal:
5212 newpat = gen_return_shadow ();
5213 pat = gen_real_ret (XEXP (XVECEXP (pat, 0, 1), 0));
5214 break;
5215
5216 default:
5217 return;
5218 }
5219 }
5220 i1 = emit_insn_before (pat, insn);
5221 PATTERN (insn) = newpat;
5222 INSN_CODE (insn) = -1;
5223 record_delay_slot_pair (i1, insn, 5, 0);
5224 }
5225
5226 /* If INSN is a multi-cycle insn that should be handled properly in
5227 modulo-scheduling, split it into a real insn and a shadow.
5228 Return true if we made a change.
5229
5230 It is valid for us to fail to split an insn; the caller has to deal
5231 with the possibility. Currently we handle loads and most mpy2 and
5232 mpy4 insns. */
5233 static bool
5234 split_delayed_nonbranch (rtx_insn *insn)
5235 {
5236 int code = recog_memoized (insn);
5237 enum attr_type type;
5238 rtx_insn *i1;
5239 rtx newpat, src, dest;
5240 rtx pat = PATTERN (insn);
5241 rtvec rtv;
5242 int delay;
5243
5244 if (GET_CODE (pat) == COND_EXEC)
5245 pat = COND_EXEC_CODE (pat);
5246
5247 if (code < 0 || GET_CODE (pat) != SET)
5248 return false;
5249 src = SET_SRC (pat);
5250 dest = SET_DEST (pat);
5251 if (!REG_P (dest))
5252 return false;
5253
5254 type = get_attr_type (insn);
5255 if (code >= 0
5256 && (type == TYPE_LOAD
5257 || type == TYPE_LOADN))
5258 {
5259 if (!MEM_P (src)
5260 && (GET_CODE (src) != ZERO_EXTEND
5261 || !MEM_P (XEXP (src, 0))))
5262 return false;
5263
5264 if (GET_MODE_SIZE (GET_MODE (dest)) > 4
5265 && (GET_MODE_SIZE (GET_MODE (dest)) != 8 || !TARGET_LDDW))
5266 return false;
5267
5268 rtv = gen_rtvec (2, GEN_INT (REGNO (SET_DEST (pat))),
5269 SET_SRC (pat));
5270 newpat = gen_load_shadow (SET_DEST (pat));
5271 pat = gen_rtx_UNSPEC (VOIDmode, rtv, UNSPEC_REAL_LOAD);
5272 delay = 4;
5273 }
5274 else if (code >= 0
5275 && (type == TYPE_MPY2
5276 || type == TYPE_MPY4))
5277 {
5278 /* We don't handle floating point multiplies yet. */
5279 if (GET_MODE (dest) == SFmode)
5280 return false;
5281
5282 rtv = gen_rtvec (2, GEN_INT (REGNO (SET_DEST (pat))),
5283 SET_SRC (pat));
5284 newpat = gen_mult_shadow (SET_DEST (pat));
5285 pat = gen_rtx_UNSPEC (VOIDmode, rtv, UNSPEC_REAL_MULT);
5286 delay = type == TYPE_MPY2 ? 1 : 3;
5287 }
5288 else
5289 return false;
5290
5291 pat = duplicate_cond (pat, insn);
5292 newpat = duplicate_cond (newpat, insn);
5293 i1 = emit_insn_before (pat, insn);
5294 PATTERN (insn) = newpat;
5295 INSN_CODE (insn) = -1;
5296 recog_memoized (insn);
5297 recog_memoized (i1);
5298 record_delay_slot_pair (i1, insn, delay, 0);
5299 return true;
5300 }
5301
5302 /* Examine if INSN is the result of splitting a load into a real load and a
5303 shadow, and if so, undo the transformation. */
5304 static void
5305 undo_split_delayed_nonbranch (rtx_insn *insn)
5306 {
5307 int icode = recog_memoized (insn);
5308 enum attr_type type;
5309 rtx prev_pat, insn_pat;
5310 rtx_insn *prev;
5311
5312 if (icode < 0)
5313 return;
5314 type = get_attr_type (insn);
5315 if (type != TYPE_LOAD_SHADOW && type != TYPE_MULT_SHADOW)
5316 return;
5317 prev = PREV_INSN (insn);
5318 prev_pat = PATTERN (prev);
5319 insn_pat = PATTERN (insn);
5320 if (GET_CODE (prev_pat) == COND_EXEC)
5321 {
5322 prev_pat = COND_EXEC_CODE (prev_pat);
5323 insn_pat = COND_EXEC_CODE (insn_pat);
5324 }
5325
5326 gcc_assert (GET_CODE (prev_pat) == UNSPEC
5327 && ((XINT (prev_pat, 1) == UNSPEC_REAL_LOAD
5328 && type == TYPE_LOAD_SHADOW)
5329 || (XINT (prev_pat, 1) == UNSPEC_REAL_MULT
5330 && type == TYPE_MULT_SHADOW)));
5331 insn_pat = gen_rtx_SET (SET_DEST (insn_pat),
5332 XVECEXP (prev_pat, 0, 1));
5333 insn_pat = duplicate_cond (insn_pat, prev);
5334 PATTERN (insn) = insn_pat;
5335 INSN_CODE (insn) = -1;
5336 delete_insn (prev);
5337 }
5338
5339 /* Split every insn (i.e. jumps and calls) which can have delay slots into
5340 two parts: the first one is scheduled normally and emits the instruction,
5341 while the second one is a shadow insn which shows the side effect taking
5342 place. The second one is placed in the right cycle by the scheduler, but
5343 not emitted as an assembly instruction. */
5344
5345 static void
5346 split_delayed_insns (void)
5347 {
5348 rtx_insn *insn;
5349 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
5350 {
5351 if (JUMP_P (insn) || CALL_P (insn))
5352 split_delayed_branch (insn);
5353 }
5354 }
5355
5356 /* For every insn that has an entry in the new_conditions vector, give it
5357 the appropriate predicate. */
5358 static void
5359 conditionalize_after_sched (void)
5360 {
5361 basic_block bb;
5362 rtx_insn *insn;
5363 FOR_EACH_BB_FN (bb, cfun)
5364 FOR_BB_INSNS (bb, insn)
5365 {
5366 unsigned uid = INSN_UID (insn);
5367 rtx cond;
5368 if (!NONDEBUG_INSN_P (insn) || uid >= INSN_INFO_LENGTH)
5369 continue;
5370 cond = INSN_INFO_ENTRY (uid).new_cond;
5371 if (cond == NULL_RTX)
5372 continue;
5373 if (dump_file)
5374 fprintf (dump_file, "Conditionalizing insn %d\n", uid);
5375 predicate_insn (insn, cond, true);
5376 }
5377 }
5378
5379 /* A callback for the hw-doloop pass. This function examines INSN; if
5380 it is a loop_end pattern we recognize, return the reg rtx for the
5381 loop counter. Otherwise, return NULL_RTX. */
5382
5383 static rtx
5384 hwloop_pattern_reg (rtx_insn *insn)
5385 {
5386 rtx pat, reg;
5387
5388 if (!JUMP_P (insn) || recog_memoized (insn) != CODE_FOR_loop_end)
5389 return NULL_RTX;
5390
5391 pat = PATTERN (insn);
5392 reg = SET_DEST (XVECEXP (pat, 0, 1));
5393 if (!REG_P (reg))
5394 return NULL_RTX;
5395 return reg;
5396 }
5397
5398 /* Return the number of cycles taken by BB, as computed by scheduling,
5399 including the latencies of all insns with delay slots. IGNORE is
5400 an insn we should ignore in the calculation, usually the final
5401 branch. */
5402 static int
5403 bb_earliest_end_cycle (basic_block bb, rtx ignore)
5404 {
5405 int earliest = 0;
5406 rtx_insn *insn;
5407
5408 FOR_BB_INSNS (bb, insn)
5409 {
5410 int cycles, this_clock;
5411
5412 if (LABEL_P (insn) || NOTE_P (insn) || DEBUG_INSN_P (insn)
5413 || GET_CODE (PATTERN (insn)) == USE
5414 || GET_CODE (PATTERN (insn)) == CLOBBER
5415 || insn == ignore)
5416 continue;
5417
5418 this_clock = insn_get_clock (insn);
5419 cycles = get_attr_cycles (insn);
5420
5421 if (earliest < this_clock + cycles)
5422 earliest = this_clock + cycles;
5423 }
5424 return earliest;
5425 }
5426
5427 /* Examine the insns in BB and remove all which have a uid greater or
5428 equal to MAX_UID. */
5429 static void
5430 filter_insns_above (basic_block bb, int max_uid)
5431 {
5432 rtx_insn *insn, *next;
5433 bool prev_ti = false;
5434 int prev_cycle = -1;
5435
5436 FOR_BB_INSNS_SAFE (bb, insn, next)
5437 {
5438 int this_cycle;
5439 if (!NONDEBUG_INSN_P (insn))
5440 continue;
5441 if (insn == BB_END (bb))
5442 return;
5443 this_cycle = insn_get_clock (insn);
5444 if (prev_ti && this_cycle == prev_cycle)
5445 {
5446 gcc_assert (GET_MODE (insn) != TImode);
5447 PUT_MODE (insn, TImode);
5448 }
5449 prev_ti = false;
5450 if (INSN_UID (insn) >= max_uid)
5451 {
5452 if (GET_MODE (insn) == TImode)
5453 {
5454 prev_ti = true;
5455 prev_cycle = this_cycle;
5456 }
5457 delete_insn (insn);
5458 }
5459 }
5460 }
5461
5462 /* Implement TARGET_ASM_EMIT_EXCEPT_PERSONALITY. */
5463
5464 static void
5465 c6x_asm_emit_except_personality (rtx personality)
5466 {
5467 fputs ("\t.personality\t", asm_out_file);
5468 output_addr_const (asm_out_file, personality);
5469 fputc ('\n', asm_out_file);
5470 }
5471
5472 /* Use a special assembly directive rather than a regular setion for
5473 unwind table data. */
5474
5475 static void
5476 c6x_asm_init_sections (void)
5477 {
5478 exception_section = get_unnamed_section (0, output_section_asm_op,
5479 "\t.handlerdata");
5480 }
5481
5482 /* A callback for the hw-doloop pass. Called to optimize LOOP in a
5483 machine-specific fashion; returns true if successful and false if
5484 the hwloop_fail function should be called. */
5485
5486 static bool
5487 hwloop_optimize (hwloop_info loop)
5488 {
5489 basic_block entry_bb, bb;
5490 rtx_insn *seq, *insn, *prev, *entry_after, *end_packet;
5491 rtx_insn *head_insn, *tail_insn, *new_insns, *last_insn;
5492 int loop_earliest;
5493 int n_execute_packets;
5494 edge entry_edge;
5495 unsigned ix;
5496 int max_uid_before, delayed_splits;
5497 int i, sp_ii, min_ii, max_ii, max_parallel, n_insns, n_real_insns, stages;
5498 rtx_insn **orig_vec;
5499 rtx_insn **copies;
5500 rtx_insn ***insn_copies;
5501
5502 if (!c6x_flag_modulo_sched || !c6x_flag_schedule_insns2
5503 || !TARGET_INSNS_64PLUS)
5504 return false;
5505
5506 if (loop->iter_reg_used || loop->depth > 1)
5507 return false;
5508 if (loop->has_call || loop->has_asm)
5509 return false;
5510
5511 if (loop->head != loop->tail)
5512 return false;
5513
5514 gcc_assert (loop->incoming_dest == loop->head);
5515
5516 entry_edge = NULL;
5517 FOR_EACH_VEC_SAFE_ELT (loop->incoming, i, entry_edge)
5518 if (entry_edge->flags & EDGE_FALLTHRU)
5519 break;
5520 if (entry_edge == NULL)
5521 return false;
5522
5523 reshuffle_units (loop->head);
5524
5525 in_hwloop = true;
5526 schedule_ebbs_init ();
5527 schedule_ebb (BB_HEAD (loop->tail), loop->loop_end, true);
5528 schedule_ebbs_finish ();
5529 in_hwloop = false;
5530
5531 bb = loop->head;
5532 loop_earliest = bb_earliest_end_cycle (bb, loop->loop_end) + 1;
5533
5534 max_uid_before = get_max_uid ();
5535
5536 /* Split all multi-cycle operations, such as loads. For normal
5537 scheduling, we only do this for branches, as the generated code
5538 would otherwise not be interrupt-safe. When using sploop, it is
5539 safe and beneficial to split them. If any multi-cycle operations
5540 remain after splitting (because we don't handle them yet), we
5541 cannot pipeline the loop. */
5542 delayed_splits = 0;
5543 FOR_BB_INSNS (bb, insn)
5544 {
5545 if (NONDEBUG_INSN_P (insn))
5546 {
5547 recog_memoized (insn);
5548 if (split_delayed_nonbranch (insn))
5549 delayed_splits++;
5550 else if (INSN_CODE (insn) >= 0
5551 && get_attr_cycles (insn) > 1)
5552 goto undo_splits;
5553 }
5554 }
5555
5556 /* Count the number of insns as well as the number real insns, and save
5557 the original sequence of insns in case we must restore it later. */
5558 n_insns = n_real_insns = 0;
5559 FOR_BB_INSNS (bb, insn)
5560 {
5561 n_insns++;
5562 if (NONDEBUG_INSN_P (insn) && insn != loop->loop_end)
5563 n_real_insns++;
5564 }
5565 orig_vec = XNEWVEC (rtx_insn *, n_insns);
5566 n_insns = 0;
5567 FOR_BB_INSNS (bb, insn)
5568 orig_vec[n_insns++] = insn;
5569
5570 /* Count the unit reservations, and compute a minimum II from that
5571 table. */
5572 count_unit_reqs (unit_reqs, loop->start_label,
5573 PREV_INSN (loop->loop_end));
5574 merge_unit_reqs (unit_reqs);
5575
5576 min_ii = res_mii (unit_reqs);
5577 max_ii = loop_earliest < 15 ? loop_earliest : 14;
5578
5579 /* Make copies of the loop body, up to a maximum number of stages we want
5580 to handle. */
5581 max_parallel = loop_earliest / min_ii + 1;
5582
5583 copies = XCNEWVEC (rtx_insn *, (max_parallel + 1) * n_real_insns);
5584 insn_copies = XNEWVEC (rtx_insn **, max_parallel + 1);
5585 for (i = 0; i < max_parallel + 1; i++)
5586 insn_copies[i] = copies + i * n_real_insns;
5587
5588 head_insn = next_nonnote_nondebug_insn (loop->start_label);
5589 tail_insn = prev_real_insn (BB_END (bb));
5590
5591 i = 0;
5592 FOR_BB_INSNS (bb, insn)
5593 if (NONDEBUG_INSN_P (insn) && insn != loop->loop_end)
5594 insn_copies[0][i++] = insn;
5595
5596 sploop_max_uid_iter0 = get_max_uid ();
5597
5598 /* Generate the copies of the loop body, and save them in the
5599 INSN_COPIES array. */
5600 start_sequence ();
5601 for (i = 0; i < max_parallel; i++)
5602 {
5603 int j;
5604 rtx_insn *this_iter;
5605
5606 copy_bb_data id;
5607 this_iter = duplicate_insn_chain (head_insn, tail_insn, NULL, &id);
5608 j = 0;
5609 while (this_iter)
5610 {
5611 rtx_insn *prev_stage_insn = insn_copies[i][j];
5612 gcc_assert (INSN_CODE (this_iter) == INSN_CODE (prev_stage_insn));
5613
5614 if (INSN_CODE (this_iter) >= 0
5615 && (get_attr_type (this_iter) == TYPE_LOAD_SHADOW
5616 || get_attr_type (this_iter) == TYPE_MULT_SHADOW))
5617 {
5618 rtx_insn *prev = PREV_INSN (this_iter);
5619 record_delay_slot_pair (prev, this_iter,
5620 get_attr_cycles (prev) - 1, 0);
5621 }
5622 else
5623 record_delay_slot_pair (prev_stage_insn, this_iter, i, 1);
5624
5625 insn_copies[i + 1][j] = this_iter;
5626 j++;
5627 this_iter = next_nonnote_nondebug_insn (this_iter);
5628 }
5629 }
5630 new_insns = get_insns ();
5631 last_insn = insn_copies[max_parallel][n_real_insns - 1];
5632 end_sequence ();
5633 emit_insn_before (new_insns, BB_END (bb));
5634
5635 /* Try to schedule the loop using varying initiation intervals,
5636 starting with the smallest possible and incrementing it
5637 on failure. */
5638 for (sp_ii = min_ii; sp_ii <= max_ii; sp_ii++)
5639 {
5640 basic_block tmp_bb;
5641 if (dump_file)
5642 fprintf (dump_file, "Trying to schedule for II %d\n", sp_ii);
5643
5644 df_clear_flags (DF_LR_RUN_DCE);
5645
5646 schedule_ebbs_init ();
5647 set_modulo_params (sp_ii, max_parallel, n_real_insns,
5648 sploop_max_uid_iter0);
5649 tmp_bb = schedule_ebb (BB_HEAD (bb), last_insn, true);
5650 schedule_ebbs_finish ();
5651
5652 if (tmp_bb)
5653 {
5654 if (dump_file)
5655 fprintf (dump_file, "Found schedule with II %d\n", sp_ii);
5656 break;
5657 }
5658 }
5659
5660 discard_delay_pairs_above (max_uid_before);
5661
5662 if (sp_ii > max_ii)
5663 goto restore_loop;
5664
5665 stages = insn_get_clock (ss.last_scheduled_iter0) / sp_ii + 1;
5666
5667 if (stages == 1 && sp_ii > 5)
5668 goto restore_loop;
5669
5670 /* At this point, we know we've been successful, unless we find later that
5671 there are too many execute packets for the loop buffer to hold. */
5672
5673 /* Assign reservations to the instructions in the loop. We must find
5674 the stage that contains the full loop kernel, and transfer the
5675 reservations of the instructions contained in it to the corresponding
5676 instructions from iteration 0, which are the only ones we'll keep. */
5677 assign_reservations (BB_HEAD (bb), ss.last_scheduled_insn);
5678 SET_PREV_INSN (BB_END (bb)) = ss.last_scheduled_iter0;
5679 SET_NEXT_INSN (ss.last_scheduled_iter0) = BB_END (bb);
5680 filter_insns_above (bb, sploop_max_uid_iter0);
5681
5682 for (i = 0; i < n_real_insns; i++)
5683 {
5684 rtx insn = insn_copies[0][i];
5685 int uid = INSN_UID (insn);
5686 int stage = insn_uid_get_clock (uid) / sp_ii;
5687
5688 if (stage + 1 < stages)
5689 {
5690 int copy_uid;
5691 stage = stages - stage - 1;
5692 copy_uid = INSN_UID (insn_copies[stage][i]);
5693 INSN_INFO_ENTRY (uid).reservation
5694 = INSN_INFO_ENTRY (copy_uid).reservation;
5695 }
5696 }
5697 if (stages == 1)
5698 stages++;
5699
5700 /* Compute the number of execute packets the pipelined form of the loop will
5701 require. */
5702 prev = NULL;
5703 n_execute_packets = 0;
5704 for (insn = loop->start_label;
5705 insn != loop->loop_end;
5706 insn = NEXT_INSN (insn))
5707 {
5708 if (NONDEBUG_INSN_P (insn) && GET_MODE (insn) == TImode
5709 && !shadow_p (insn))
5710 {
5711 n_execute_packets++;
5712 if (prev && insn_get_clock (prev) + 1 != insn_get_clock (insn))
5713 /* We need an extra NOP instruction. */
5714 n_execute_packets++;
5715
5716 prev = insn;
5717 }
5718 }
5719
5720 end_packet = ss.last_scheduled_iter0;
5721 while (!NONDEBUG_INSN_P (end_packet) || GET_MODE (end_packet) != TImode)
5722 end_packet = PREV_INSN (end_packet);
5723
5724 /* The earliest cycle in which we can emit the SPKERNEL instruction. */
5725 loop_earliest = (stages - 1) * sp_ii;
5726 if (loop_earliest > insn_get_clock (end_packet))
5727 {
5728 n_execute_packets++;
5729 end_packet = loop->loop_end;
5730 }
5731 else
5732 loop_earliest = insn_get_clock (end_packet);
5733
5734 if (n_execute_packets > 14)
5735 goto restore_loop;
5736
5737 /* Generate the spkernel instruction, and place it at the appropriate
5738 spot. */
5739 PUT_MODE (end_packet, VOIDmode);
5740
5741 insn = emit_jump_insn_before (
5742 gen_spkernel (GEN_INT (stages - 1),
5743 const0_rtx, JUMP_LABEL (loop->loop_end)),
5744 end_packet);
5745 JUMP_LABEL (insn) = JUMP_LABEL (loop->loop_end);
5746 insn_set_clock (insn, loop_earliest);
5747 PUT_MODE (insn, TImode);
5748 INSN_INFO_ENTRY (INSN_UID (insn)).ebb_start = false;
5749 delete_insn (loop->loop_end);
5750
5751 /* Place the mvc and sploop instructions before the loop. */
5752 entry_bb = entry_edge->src;
5753
5754 start_sequence ();
5755
5756 insn = emit_insn (gen_mvilc (loop->iter_reg));
5757 if (loop->iter_reg_used_outside)
5758 insn = emit_move_insn (loop->iter_reg, const0_rtx);
5759 insn = emit_insn (gen_sploop (GEN_INT (sp_ii)));
5760 seq = get_insns ();
5761
5762 if (!single_succ_p (entry_bb) || vec_safe_length (loop->incoming) > 1)
5763 {
5764 basic_block new_bb;
5765 edge e;
5766 edge_iterator ei;
5767
5768 emit_insn_before (seq, BB_HEAD (loop->head));
5769 seq = emit_label_before (gen_label_rtx (), seq);
5770
5771 new_bb = create_basic_block (seq, insn, entry_bb);
5772 FOR_EACH_EDGE (e, ei, loop->incoming)
5773 {
5774 if (!(e->flags & EDGE_FALLTHRU))
5775 redirect_edge_and_branch_force (e, new_bb);
5776 else
5777 redirect_edge_succ (e, new_bb);
5778 }
5779 make_edge (new_bb, loop->head, 0);
5780 }
5781 else
5782 {
5783 entry_after = BB_END (entry_bb);
5784 while (DEBUG_INSN_P (entry_after)
5785 || (NOTE_P (entry_after)
5786 && NOTE_KIND (entry_after) != NOTE_INSN_BASIC_BLOCK))
5787 entry_after = PREV_INSN (entry_after);
5788 emit_insn_after (seq, entry_after);
5789 }
5790
5791 end_sequence ();
5792
5793 /* Make sure we don't try to schedule this loop again. */
5794 for (ix = 0; loop->blocks.iterate (ix, &bb); ix++)
5795 bb->flags |= BB_DISABLE_SCHEDULE;
5796
5797 return true;
5798
5799 restore_loop:
5800 if (dump_file)
5801 fprintf (dump_file, "Unable to pipeline loop.\n");
5802
5803 for (i = 1; i < n_insns; i++)
5804 {
5805 SET_NEXT_INSN (orig_vec[i - 1]) = orig_vec[i];
5806 SET_PREV_INSN (orig_vec[i]) = orig_vec[i - 1];
5807 }
5808 SET_PREV_INSN (orig_vec[0]) = PREV_INSN (BB_HEAD (bb));
5809 SET_NEXT_INSN (PREV_INSN (BB_HEAD (bb))) = orig_vec[0];
5810 SET_NEXT_INSN (orig_vec[n_insns - 1]) = NEXT_INSN (BB_END (bb));
5811 SET_PREV_INSN (NEXT_INSN (BB_END (bb))) = orig_vec[n_insns - 1];
5812 BB_HEAD (bb) = orig_vec[0];
5813 BB_END (bb) = orig_vec[n_insns - 1];
5814 undo_splits:
5815 free_delay_pairs ();
5816 FOR_BB_INSNS (bb, insn)
5817 if (NONDEBUG_INSN_P (insn))
5818 undo_split_delayed_nonbranch (insn);
5819 return false;
5820 }
5821
5822 /* A callback for the hw-doloop pass. Called when a loop we have discovered
5823 turns out not to be optimizable; we have to split the doloop_end pattern
5824 into a subtract and a test. */
5825 static void
5826 hwloop_fail (hwloop_info loop)
5827 {
5828 rtx insn, test, testreg;
5829
5830 if (dump_file)
5831 fprintf (dump_file, "splitting doloop insn %d\n",
5832 INSN_UID (loop->loop_end));
5833 insn = gen_addsi3 (loop->iter_reg, loop->iter_reg, constm1_rtx);
5834 /* See if we can emit the add at the head of the loop rather than at the
5835 end. */
5836 if (loop->head == NULL
5837 || loop->iter_reg_used_outside
5838 || loop->iter_reg_used
5839 || TEST_HARD_REG_BIT (loop->regs_set_in_loop, REGNO (loop->iter_reg))
5840 || loop->incoming_dest != loop->head
5841 || EDGE_COUNT (loop->head->preds) != 2)
5842 emit_insn_before (insn, loop->loop_end);
5843 else
5844 {
5845 rtx_insn *t = loop->start_label;
5846 while (!NOTE_P (t) || NOTE_KIND (t) != NOTE_INSN_BASIC_BLOCK)
5847 t = NEXT_INSN (t);
5848 emit_insn_after (insn, t);
5849 }
5850
5851 testreg = SET_DEST (XVECEXP (PATTERN (loop->loop_end), 0, 2));
5852 if (GET_CODE (testreg) == SCRATCH)
5853 testreg = loop->iter_reg;
5854 else
5855 emit_insn_before (gen_movsi (testreg, loop->iter_reg), loop->loop_end);
5856
5857 test = gen_rtx_NE (VOIDmode, testreg, const0_rtx);
5858 insn = emit_jump_insn_before (gen_cbranchsi4 (test, testreg, const0_rtx,
5859 loop->start_label),
5860 loop->loop_end);
5861
5862 JUMP_LABEL (insn) = loop->start_label;
5863 LABEL_NUSES (loop->start_label)++;
5864 delete_insn (loop->loop_end);
5865 }
5866
5867 static struct hw_doloop_hooks c6x_doloop_hooks =
5868 {
5869 hwloop_pattern_reg,
5870 hwloop_optimize,
5871 hwloop_fail
5872 };
5873
5874 /* Run the hw-doloop pass to modulo-schedule hardware loops, or split the
5875 doloop_end patterns where such optimizations are impossible. */
5876 static void
5877 c6x_hwloops (void)
5878 {
5879 if (optimize)
5880 reorg_loops (true, &c6x_doloop_hooks);
5881 }
5882
5883 /* Implement the TARGET_MACHINE_DEPENDENT_REORG pass. We split call insns here
5884 into a sequence that loads the return register and performs the call,
5885 and emit the return label.
5886 If scheduling after reload is requested, it happens here. */
5887
5888 static void
5889 c6x_reorg (void)
5890 {
5891 basic_block bb;
5892 bool do_selsched = (c6x_flag_schedule_insns2 && flag_selective_scheduling2
5893 && !maybe_skip_selective_scheduling ());
5894
5895 /* We are freeing block_for_insn in the toplev to keep compatibility
5896 with old MDEP_REORGS that are not CFG based. Recompute it now. */
5897 compute_bb_for_insn ();
5898
5899 df_clear_flags (DF_LR_RUN_DCE);
5900 df_note_add_problem ();
5901
5902 /* If optimizing, we'll have split before scheduling. */
5903 if (optimize == 0)
5904 split_all_insns ();
5905
5906 df_analyze ();
5907
5908 if (c6x_flag_schedule_insns2)
5909 {
5910 int sz = get_max_uid () * 3 / 2 + 1;
5911
5912 insn_info.create (sz);
5913 }
5914
5915 /* Make sure the real-jump insns we create are not deleted. When modulo-
5916 scheduling, situations where a reg is only stored in a loop can also
5917 cause dead code when doing the initial unrolling. */
5918 sched_no_dce = true;
5919
5920 c6x_hwloops ();
5921
5922 if (c6x_flag_schedule_insns2)
5923 {
5924 split_delayed_insns ();
5925 timevar_push (TV_SCHED2);
5926 if (do_selsched)
5927 run_selective_scheduling ();
5928 else
5929 schedule_ebbs ();
5930 conditionalize_after_sched ();
5931 timevar_pop (TV_SCHED2);
5932
5933 free_delay_pairs ();
5934 }
5935 sched_no_dce = false;
5936
5937 rtx_code_label **call_labels = XCNEWVEC (rtx_code_label *, get_max_uid () + 1);
5938
5939 reorg_split_calls (call_labels);
5940
5941 if (c6x_flag_schedule_insns2)
5942 {
5943 FOR_EACH_BB_FN (bb, cfun)
5944 if ((bb->flags & BB_DISABLE_SCHEDULE) == 0)
5945 assign_reservations (BB_HEAD (bb), BB_END (bb));
5946 }
5947
5948 if (c6x_flag_var_tracking)
5949 {
5950 timevar_push (TV_VAR_TRACKING);
5951 variable_tracking_main ();
5952 timevar_pop (TV_VAR_TRACKING);
5953 }
5954
5955 reorg_emit_nops (call_labels);
5956
5957 /* Post-process the schedule to move parallel insns into SEQUENCEs. */
5958 if (c6x_flag_schedule_insns2)
5959 {
5960 free_delay_pairs ();
5961 c6x_gen_bundles ();
5962 }
5963
5964 df_finish_pass (false);
5965 }
5966
5967 /* Called when a function has been assembled. It should perform all the
5968 tasks of ASM_DECLARE_FUNCTION_SIZE in elfos.h, plus target-specific
5969 tasks.
5970 We free the reservation (and other scheduling) information here now that
5971 all insns have been output. */
5972 void
5973 c6x_function_end (FILE *file, const char *fname)
5974 {
5975 c6x_output_fn_unwind (file);
5976
5977 insn_info.release ();
5978
5979 if (!flag_inhibit_size_directive)
5980 ASM_OUTPUT_MEASURED_SIZE (file, fname);
5981 }
5982
5983 /* Determine whether X is a shift with code CODE and an integer amount
5984 AMOUNT. */
5985 static bool
5986 shift_p (rtx x, enum rtx_code code, int amount)
5987 {
5988 return (GET_CODE (x) == code && GET_CODE (XEXP (x, 1)) == CONST_INT
5989 && INTVAL (XEXP (x, 1)) == amount);
5990 }
5991
5992 /* Compute a (partial) cost for rtx X. Return true if the complete
5993 cost has been computed, and false if subexpressions should be
5994 scanned. In either case, *TOTAL contains the cost result. */
5995
5996 static bool
5997 c6x_rtx_costs (rtx x, machine_mode mode, int outer_code, int opno, int *total,
5998 bool speed)
5999 {
6000 int cost2 = COSTS_N_INSNS (1);
6001 rtx op0, op1;
6002 int code = GET_CODE (x);
6003
6004 switch (code)
6005 {
6006 case CONST_INT:
6007 if (outer_code == SET || outer_code == PLUS)
6008 *total = satisfies_constraint_IsB (x) ? 0 : cost2;
6009 else if (outer_code == AND || outer_code == IOR || outer_code == XOR
6010 || outer_code == MINUS)
6011 *total = satisfies_constraint_Is5 (x) ? 0 : cost2;
6012 else if (GET_RTX_CLASS (outer_code) == RTX_COMPARE
6013 || GET_RTX_CLASS (outer_code) == RTX_COMM_COMPARE)
6014 *total = satisfies_constraint_Iu4 (x) ? 0 : cost2;
6015 else if (outer_code == ASHIFT || outer_code == ASHIFTRT
6016 || outer_code == LSHIFTRT)
6017 *total = satisfies_constraint_Iu5 (x) ? 0 : cost2;
6018 else
6019 *total = cost2;
6020 return true;
6021
6022 case CONST:
6023 case LABEL_REF:
6024 case SYMBOL_REF:
6025 case CONST_DOUBLE:
6026 *total = COSTS_N_INSNS (2);
6027 return true;
6028
6029 case TRUNCATE:
6030 /* Recognize a mult_highpart operation. */
6031 if ((mode == HImode || mode == SImode)
6032 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6033 && GET_MODE (XEXP (x, 0)) == GET_MODE_2XWIDER_MODE (mode).require ()
6034 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
6035 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6036 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode))
6037 {
6038 rtx mul = XEXP (XEXP (x, 0), 0);
6039 rtx op0 = XEXP (mul, 0);
6040 rtx op1 = XEXP (mul, 1);
6041 enum rtx_code code0 = GET_CODE (op0);
6042 enum rtx_code code1 = GET_CODE (op1);
6043
6044 if ((code0 == code1
6045 && (code0 == SIGN_EXTEND || code0 == ZERO_EXTEND))
6046 || (mode == HImode
6047 && code0 == ZERO_EXTEND && code1 == SIGN_EXTEND))
6048 {
6049 if (mode == HImode)
6050 *total = COSTS_N_INSNS (2);
6051 else
6052 *total = COSTS_N_INSNS (12);
6053 mode = GET_MODE (XEXP (op0, 0));
6054 *total += rtx_cost (XEXP (op0, 0), mode, code0, 0, speed);
6055 *total += rtx_cost (XEXP (op1, 0), mode, code1, 0, speed);
6056 return true;
6057 }
6058 }
6059 return false;
6060
6061 case ASHIFT:
6062 case ASHIFTRT:
6063 case LSHIFTRT:
6064 if (mode == DImode)
6065 *total = COSTS_N_INSNS (CONSTANT_P (XEXP (x, 1)) ? 4 : 15);
6066 else
6067 *total = COSTS_N_INSNS (1);
6068 return false;
6069
6070 case PLUS:
6071 case MINUS:
6072 *total = COSTS_N_INSNS (1);
6073 op0 = code == PLUS ? XEXP (x, 0) : XEXP (x, 1);
6074 op1 = code == PLUS ? XEXP (x, 1) : XEXP (x, 0);
6075 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
6076 && INTEGRAL_MODE_P (mode)
6077 && GET_CODE (op0) == MULT
6078 && GET_CODE (XEXP (op0, 1)) == CONST_INT
6079 && (INTVAL (XEXP (op0, 1)) == 2
6080 || INTVAL (XEXP (op0, 1)) == 4
6081 || (code == PLUS && INTVAL (XEXP (op0, 1)) == 8)))
6082 {
6083 *total += rtx_cost (XEXP (op0, 0), mode, ASHIFT, 0, speed);
6084 *total += rtx_cost (op1, mode, (enum rtx_code) code, 1, speed);
6085 return true;
6086 }
6087 return false;
6088
6089 case MULT:
6090 op0 = XEXP (x, 0);
6091 op1 = XEXP (x, 1);
6092 if (mode == DFmode)
6093 {
6094 if (TARGET_FP)
6095 *total = COSTS_N_INSNS (speed ? 10 : 1);
6096 else
6097 *total = COSTS_N_INSNS (speed ? 200 : 4);
6098 }
6099 else if (mode == SFmode)
6100 {
6101 if (TARGET_FP)
6102 *total = COSTS_N_INSNS (speed ? 4 : 1);
6103 else
6104 *total = COSTS_N_INSNS (speed ? 100 : 4);
6105 }
6106 else if (mode == DImode)
6107 {
6108 if (TARGET_MPY32
6109 && GET_CODE (op0) == GET_CODE (op1)
6110 && (GET_CODE (op0) == ZERO_EXTEND
6111 || GET_CODE (op0) == SIGN_EXTEND))
6112 {
6113 *total = COSTS_N_INSNS (speed ? 2 : 1);
6114 op0 = XEXP (op0, 0);
6115 op1 = XEXP (op1, 0);
6116 }
6117 else
6118 /* Maybe improve this laster. */
6119 *total = COSTS_N_INSNS (20);
6120 }
6121 else if (mode == SImode)
6122 {
6123 if (((GET_CODE (op0) == ZERO_EXTEND
6124 || GET_CODE (op0) == SIGN_EXTEND
6125 || shift_p (op0, LSHIFTRT, 16))
6126 && (GET_CODE (op1) == SIGN_EXTEND
6127 || GET_CODE (op1) == ZERO_EXTEND
6128 || scst5_operand (op1, SImode)
6129 || shift_p (op1, ASHIFTRT, 16)
6130 || shift_p (op1, LSHIFTRT, 16)))
6131 || (shift_p (op0, ASHIFTRT, 16)
6132 && (GET_CODE (op1) == SIGN_EXTEND
6133 || shift_p (op1, ASHIFTRT, 16))))
6134 {
6135 *total = COSTS_N_INSNS (speed ? 2 : 1);
6136 op0 = XEXP (op0, 0);
6137 if (scst5_operand (op1, SImode))
6138 op1 = NULL_RTX;
6139 else
6140 op1 = XEXP (op1, 0);
6141 }
6142 else if (!speed)
6143 *total = COSTS_N_INSNS (1);
6144 else if (TARGET_MPY32)
6145 *total = COSTS_N_INSNS (4);
6146 else
6147 *total = COSTS_N_INSNS (6);
6148 }
6149 else if (mode == HImode)
6150 *total = COSTS_N_INSNS (speed ? 2 : 1);
6151
6152 if (GET_CODE (op0) != REG
6153 && (GET_CODE (op0) != SUBREG || GET_CODE (SUBREG_REG (op0)) != REG))
6154 *total += rtx_cost (op0, mode, MULT, 0, speed);
6155 if (op1 && GET_CODE (op1) != REG
6156 && (GET_CODE (op1) != SUBREG || GET_CODE (SUBREG_REG (op1)) != REG))
6157 *total += rtx_cost (op1, mode, MULT, 1, speed);
6158 return true;
6159
6160 case UDIV:
6161 case DIV:
6162 /* This is a bit random; assuming on average there'll be 16 leading
6163 zeros. FIXME: estimate better for constant dividends. */
6164 *total = COSTS_N_INSNS (6 + 3 * 16);
6165 return false;
6166
6167 case IF_THEN_ELSE:
6168 /* Recognize the cmp_and/ior patterns. */
6169 op0 = XEXP (x, 0);
6170 if ((GET_CODE (op0) == EQ || GET_CODE (op0) == NE)
6171 && REG_P (XEXP (op0, 0))
6172 && XEXP (op0, 1) == const0_rtx
6173 && rtx_equal_p (XEXP (x, 1), XEXP (op0, 0)))
6174 {
6175 *total = rtx_cost (XEXP (x, 1), VOIDmode, (enum rtx_code) outer_code,
6176 opno, speed);
6177 return false;
6178 }
6179 return false;
6180
6181 default:
6182 return false;
6183 }
6184 }
6185
6186 /* Implements target hook vector_mode_supported_p. */
6187
6188 static bool
6189 c6x_vector_mode_supported_p (machine_mode mode)
6190 {
6191 switch (mode)
6192 {
6193 case E_V2HImode:
6194 case E_V4QImode:
6195 case E_V2SImode:
6196 case E_V4HImode:
6197 case E_V8QImode:
6198 return true;
6199 default:
6200 return false;
6201 }
6202 }
6203
6204 /* Implements TARGET_VECTORIZE_PREFERRED_SIMD_MODE. */
6205 static machine_mode
6206 c6x_preferred_simd_mode (scalar_mode mode)
6207 {
6208 switch (mode)
6209 {
6210 case E_HImode:
6211 return V2HImode;
6212 case E_QImode:
6213 return V4QImode;
6214
6215 default:
6216 return word_mode;
6217 }
6218 }
6219
6220 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P. */
6221
6222 static bool
6223 c6x_scalar_mode_supported_p (scalar_mode mode)
6224 {
6225 if (ALL_FIXED_POINT_MODE_P (mode)
6226 && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
6227 return true;
6228
6229 return default_scalar_mode_supported_p (mode);
6230 }
6231
6232 /* Output a reference from a function exception table to the type_info
6233 object X. Output these via a special assembly directive. */
6234
6235 static bool
6236 c6x_output_ttype (rtx x)
6237 {
6238 /* Use special relocations for symbol references. */
6239 if (GET_CODE (x) != CONST_INT)
6240 fputs ("\t.ehtype\t", asm_out_file);
6241 else
6242 fputs ("\t.word\t", asm_out_file);
6243 output_addr_const (asm_out_file, x);
6244 fputc ('\n', asm_out_file);
6245
6246 return TRUE;
6247 }
6248
6249 /* Modify the return address of the current function. */
6250
6251 void
6252 c6x_set_return_address (rtx source, rtx scratch)
6253 {
6254 struct c6x_frame frame;
6255 rtx addr;
6256 HOST_WIDE_INT offset;
6257
6258 c6x_compute_frame_layout (&frame);
6259 if (! c6x_save_reg (RETURN_ADDR_REGNO))
6260 emit_move_insn (gen_rtx_REG (Pmode, RETURN_ADDR_REGNO), source);
6261 else
6262 {
6263
6264 if (frame_pointer_needed)
6265 {
6266 addr = hard_frame_pointer_rtx;
6267 offset = frame.b3_offset;
6268 }
6269 else
6270 {
6271 addr = stack_pointer_rtx;
6272 offset = frame.to_allocate - frame.b3_offset;
6273 }
6274
6275 /* TODO: Use base+offset loads where possible. */
6276 if (offset)
6277 {
6278 HOST_WIDE_INT low = trunc_int_for_mode (offset, HImode);
6279
6280 emit_insn (gen_movsi_high (scratch, GEN_INT (low)));
6281 if (low != offset)
6282 emit_insn (gen_movsi_lo_sum (scratch, scratch, GEN_INT(offset)));
6283 emit_insn (gen_addsi3 (scratch, addr, scratch));
6284 addr = scratch;
6285 }
6286
6287 emit_move_insn (gen_frame_mem (Pmode, addr), source);
6288 }
6289 }
6290
6291 /* We save pairs of registers using a DImode store. Describe the component
6292 registers for DWARF generation code. */
6293
6294 static rtx
6295 c6x_dwarf_register_span (rtx rtl)
6296 {
6297 unsigned regno;
6298 unsigned real_regno;
6299 int nregs;
6300 int i;
6301 rtx p;
6302
6303 regno = REGNO (rtl);
6304 nregs = REG_NREGS (rtl);
6305 if (nregs == 1)
6306 return NULL_RTX;
6307
6308 p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc(nregs));
6309 for (i = 0; i < nregs; i++)
6310 {
6311 if (TARGET_BIG_ENDIAN)
6312 real_regno = regno + nregs - (i + 1);
6313 else
6314 real_regno = regno + i;
6315
6316 XVECEXP (p, 0, i) = gen_rtx_REG (SImode, real_regno);
6317 }
6318
6319 return p;
6320 }
6321
6322 /* Codes for all the C6X builtins. */
6323 enum c6x_builtins
6324 {
6325 C6X_BUILTIN_SADD,
6326 C6X_BUILTIN_SSUB,
6327 C6X_BUILTIN_ADD2,
6328 C6X_BUILTIN_SUB2,
6329 C6X_BUILTIN_ADD4,
6330 C6X_BUILTIN_SUB4,
6331 C6X_BUILTIN_SADD2,
6332 C6X_BUILTIN_SSUB2,
6333 C6X_BUILTIN_SADDU4,
6334
6335 C6X_BUILTIN_SMPY,
6336 C6X_BUILTIN_SMPYH,
6337 C6X_BUILTIN_SMPYHL,
6338 C6X_BUILTIN_SMPYLH,
6339 C6X_BUILTIN_MPY2,
6340 C6X_BUILTIN_SMPY2,
6341
6342 C6X_BUILTIN_CLRR,
6343 C6X_BUILTIN_EXTR,
6344 C6X_BUILTIN_EXTRU,
6345
6346 C6X_BUILTIN_SSHL,
6347 C6X_BUILTIN_SUBC,
6348 C6X_BUILTIN_ABS,
6349 C6X_BUILTIN_ABS2,
6350 C6X_BUILTIN_AVG2,
6351 C6X_BUILTIN_AVGU4,
6352
6353 C6X_BUILTIN_MAX
6354 };
6355
6356
6357 static GTY(()) tree c6x_builtin_decls[C6X_BUILTIN_MAX];
6358
6359 /* Return the C6X builtin for CODE. */
6360 static tree
6361 c6x_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
6362 {
6363 if (code >= C6X_BUILTIN_MAX)
6364 return error_mark_node;
6365
6366 return c6x_builtin_decls[code];
6367 }
6368
6369 #define def_builtin(NAME, TYPE, CODE) \
6370 do { \
6371 tree bdecl; \
6372 bdecl = add_builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD, \
6373 NULL, NULL_TREE); \
6374 c6x_builtin_decls[CODE] = bdecl; \
6375 } while (0)
6376
6377 /* Set up all builtin functions for this target. */
6378 static void
6379 c6x_init_builtins (void)
6380 {
6381 tree V4QI_type_node = build_vector_type (unsigned_intQI_type_node, 4);
6382 tree V2HI_type_node = build_vector_type (intHI_type_node, 2);
6383 tree V2SI_type_node = build_vector_type (intSI_type_node, 2);
6384 tree int_ftype_int
6385 = build_function_type_list (integer_type_node, integer_type_node,
6386 NULL_TREE);
6387 tree int_ftype_int_int
6388 = build_function_type_list (integer_type_node, integer_type_node,
6389 integer_type_node, NULL_TREE);
6390 tree v2hi_ftype_v2hi
6391 = build_function_type_list (V2HI_type_node, V2HI_type_node, NULL_TREE);
6392 tree v4qi_ftype_v4qi_v4qi
6393 = build_function_type_list (V4QI_type_node, V4QI_type_node,
6394 V4QI_type_node, NULL_TREE);
6395 tree v2hi_ftype_v2hi_v2hi
6396 = build_function_type_list (V2HI_type_node, V2HI_type_node,
6397 V2HI_type_node, NULL_TREE);
6398 tree v2si_ftype_v2hi_v2hi
6399 = build_function_type_list (V2SI_type_node, V2HI_type_node,
6400 V2HI_type_node, NULL_TREE);
6401
6402 def_builtin ("__builtin_c6x_sadd", int_ftype_int_int,
6403 C6X_BUILTIN_SADD);
6404 def_builtin ("__builtin_c6x_ssub", int_ftype_int_int,
6405 C6X_BUILTIN_SSUB);
6406 def_builtin ("__builtin_c6x_add2", v2hi_ftype_v2hi_v2hi,
6407 C6X_BUILTIN_ADD2);
6408 def_builtin ("__builtin_c6x_sub2", v2hi_ftype_v2hi_v2hi,
6409 C6X_BUILTIN_SUB2);
6410 def_builtin ("__builtin_c6x_add4", v4qi_ftype_v4qi_v4qi,
6411 C6X_BUILTIN_ADD4);
6412 def_builtin ("__builtin_c6x_sub4", v4qi_ftype_v4qi_v4qi,
6413 C6X_BUILTIN_SUB4);
6414 def_builtin ("__builtin_c6x_mpy2", v2si_ftype_v2hi_v2hi,
6415 C6X_BUILTIN_MPY2);
6416 def_builtin ("__builtin_c6x_sadd2", v2hi_ftype_v2hi_v2hi,
6417 C6X_BUILTIN_SADD2);
6418 def_builtin ("__builtin_c6x_ssub2", v2hi_ftype_v2hi_v2hi,
6419 C6X_BUILTIN_SSUB2);
6420 def_builtin ("__builtin_c6x_saddu4", v4qi_ftype_v4qi_v4qi,
6421 C6X_BUILTIN_SADDU4);
6422 def_builtin ("__builtin_c6x_smpy2", v2si_ftype_v2hi_v2hi,
6423 C6X_BUILTIN_SMPY2);
6424
6425 def_builtin ("__builtin_c6x_smpy", int_ftype_int_int,
6426 C6X_BUILTIN_SMPY);
6427 def_builtin ("__builtin_c6x_smpyh", int_ftype_int_int,
6428 C6X_BUILTIN_SMPYH);
6429 def_builtin ("__builtin_c6x_smpyhl", int_ftype_int_int,
6430 C6X_BUILTIN_SMPYHL);
6431 def_builtin ("__builtin_c6x_smpylh", int_ftype_int_int,
6432 C6X_BUILTIN_SMPYLH);
6433
6434 def_builtin ("__builtin_c6x_sshl", int_ftype_int_int,
6435 C6X_BUILTIN_SSHL);
6436 def_builtin ("__builtin_c6x_subc", int_ftype_int_int,
6437 C6X_BUILTIN_SUBC);
6438
6439 def_builtin ("__builtin_c6x_avg2", v2hi_ftype_v2hi_v2hi,
6440 C6X_BUILTIN_AVG2);
6441 def_builtin ("__builtin_c6x_avgu4", v4qi_ftype_v4qi_v4qi,
6442 C6X_BUILTIN_AVGU4);
6443
6444 def_builtin ("__builtin_c6x_clrr", int_ftype_int_int,
6445 C6X_BUILTIN_CLRR);
6446 def_builtin ("__builtin_c6x_extr", int_ftype_int_int,
6447 C6X_BUILTIN_EXTR);
6448 def_builtin ("__builtin_c6x_extru", int_ftype_int_int,
6449 C6X_BUILTIN_EXTRU);
6450
6451 def_builtin ("__builtin_c6x_abs", int_ftype_int, C6X_BUILTIN_ABS);
6452 def_builtin ("__builtin_c6x_abs2", v2hi_ftype_v2hi, C6X_BUILTIN_ABS2);
6453 }
6454
6455
6456 struct builtin_description
6457 {
6458 const enum insn_code icode;
6459 const char *const name;
6460 const enum c6x_builtins code;
6461 };
6462
6463 static const struct builtin_description bdesc_2arg[] =
6464 {
6465 { CODE_FOR_saddsi3, "__builtin_c6x_sadd", C6X_BUILTIN_SADD },
6466 { CODE_FOR_ssubsi3, "__builtin_c6x_ssub", C6X_BUILTIN_SSUB },
6467 { CODE_FOR_addv2hi3, "__builtin_c6x_add2", C6X_BUILTIN_ADD2 },
6468 { CODE_FOR_subv2hi3, "__builtin_c6x_sub2", C6X_BUILTIN_SUB2 },
6469 { CODE_FOR_addv4qi3, "__builtin_c6x_add4", C6X_BUILTIN_ADD4 },
6470 { CODE_FOR_subv4qi3, "__builtin_c6x_sub4", C6X_BUILTIN_SUB4 },
6471 { CODE_FOR_ss_addv2hi3, "__builtin_c6x_sadd2", C6X_BUILTIN_SADD2 },
6472 { CODE_FOR_ss_subv2hi3, "__builtin_c6x_ssub2", C6X_BUILTIN_SSUB2 },
6473 { CODE_FOR_us_addv4qi3, "__builtin_c6x_saddu4", C6X_BUILTIN_SADDU4 },
6474
6475 { CODE_FOR_subcsi3, "__builtin_c6x_subc", C6X_BUILTIN_SUBC },
6476 { CODE_FOR_ss_ashlsi3, "__builtin_c6x_sshl", C6X_BUILTIN_SSHL },
6477
6478 { CODE_FOR_avgv2hi3, "__builtin_c6x_avg2", C6X_BUILTIN_AVG2 },
6479 { CODE_FOR_uavgv4qi3, "__builtin_c6x_avgu4", C6X_BUILTIN_AVGU4 },
6480
6481 { CODE_FOR_mulhqsq3, "__builtin_c6x_smpy", C6X_BUILTIN_SMPY },
6482 { CODE_FOR_mulhqsq3_hh, "__builtin_c6x_smpyh", C6X_BUILTIN_SMPYH },
6483 { CODE_FOR_mulhqsq3_lh, "__builtin_c6x_smpylh", C6X_BUILTIN_SMPYLH },
6484 { CODE_FOR_mulhqsq3_hl, "__builtin_c6x_smpyhl", C6X_BUILTIN_SMPYHL },
6485
6486 { CODE_FOR_mulv2hqv2sq3, "__builtin_c6x_smpy2", C6X_BUILTIN_SMPY2 },
6487
6488 { CODE_FOR_clrr, "__builtin_c6x_clrr", C6X_BUILTIN_CLRR },
6489 { CODE_FOR_extr, "__builtin_c6x_extr", C6X_BUILTIN_EXTR },
6490 { CODE_FOR_extru, "__builtin_c6x_extru", C6X_BUILTIN_EXTRU }
6491 };
6492
6493 static const struct builtin_description bdesc_1arg[] =
6494 {
6495 { CODE_FOR_ssabssi2, "__builtin_c6x_abs", C6X_BUILTIN_ABS },
6496 { CODE_FOR_ssabsv2hi2, "__builtin_c6x_abs2", C6X_BUILTIN_ABS2 }
6497 };
6498
6499 /* Errors in the source file can cause expand_expr to return const0_rtx
6500 where we expect a vector. To avoid crashing, use one of the vector
6501 clear instructions. */
6502 static rtx
6503 safe_vector_operand (rtx x, machine_mode mode)
6504 {
6505 if (x != const0_rtx)
6506 return x;
6507 x = gen_reg_rtx (SImode);
6508
6509 emit_insn (gen_movsi (x, CONST0_RTX (SImode)));
6510 return gen_lowpart (mode, x);
6511 }
6512
6513 /* Subroutine of c6x_expand_builtin to take care of binop insns. MACFLAG is -1
6514 if this is a normal binary op, or one of the MACFLAG_xxx constants. */
6515
6516 static rtx
6517 c6x_expand_binop_builtin (enum insn_code icode, tree exp, rtx target,
6518 bool match_op)
6519 {
6520 int offs = match_op ? 1 : 0;
6521 rtx pat;
6522 tree arg0 = CALL_EXPR_ARG (exp, 0);
6523 tree arg1 = CALL_EXPR_ARG (exp, 1);
6524 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
6525 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
6526 machine_mode op0mode = GET_MODE (op0);
6527 machine_mode op1mode = GET_MODE (op1);
6528 machine_mode tmode = insn_data[icode].operand[0].mode;
6529 machine_mode mode0 = insn_data[icode].operand[1 + offs].mode;
6530 machine_mode mode1 = insn_data[icode].operand[2 + offs].mode;
6531 rtx ret = target;
6532
6533 if (VECTOR_MODE_P (mode0))
6534 op0 = safe_vector_operand (op0, mode0);
6535 if (VECTOR_MODE_P (mode1))
6536 op1 = safe_vector_operand (op1, mode1);
6537
6538 if (! target
6539 || GET_MODE (target) != tmode
6540 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
6541 {
6542 if (tmode == SQmode || tmode == V2SQmode)
6543 {
6544 ret = gen_reg_rtx (tmode == SQmode ? SImode : V2SImode);
6545 target = gen_lowpart (tmode, ret);
6546 }
6547 else
6548 target = gen_reg_rtx (tmode);
6549 }
6550
6551 if ((op0mode == V2HImode || op0mode == SImode || op0mode == VOIDmode)
6552 && (mode0 == V2HQmode || mode0 == HQmode || mode0 == SQmode))
6553 {
6554 op0mode = mode0;
6555 op0 = gen_lowpart (mode0, op0);
6556 }
6557 if ((op1mode == V2HImode || op1mode == SImode || op1mode == VOIDmode)
6558 && (mode1 == V2HQmode || mode1 == HQmode || mode1 == SQmode))
6559 {
6560 op1mode = mode1;
6561 op1 = gen_lowpart (mode1, op1);
6562 }
6563 /* In case the insn wants input operands in modes different from
6564 the result, abort. */
6565 gcc_assert ((op0mode == mode0 || op0mode == VOIDmode)
6566 && (op1mode == mode1 || op1mode == VOIDmode));
6567
6568 if (! (*insn_data[icode].operand[1 + offs].predicate) (op0, mode0))
6569 op0 = copy_to_mode_reg (mode0, op0);
6570 if (! (*insn_data[icode].operand[2 + offs].predicate) (op1, mode1))
6571 op1 = copy_to_mode_reg (mode1, op1);
6572
6573 if (match_op)
6574 pat = GEN_FCN (icode) (target, target, op0, op1);
6575 else
6576 pat = GEN_FCN (icode) (target, op0, op1);
6577
6578 if (! pat)
6579 return 0;
6580
6581 emit_insn (pat);
6582
6583 return ret;
6584 }
6585
6586 /* Subroutine of c6x_expand_builtin to take care of unop insns. */
6587
6588 static rtx
6589 c6x_expand_unop_builtin (enum insn_code icode, tree exp,
6590 rtx target)
6591 {
6592 rtx pat;
6593 tree arg0 = CALL_EXPR_ARG (exp, 0);
6594 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
6595 machine_mode op0mode = GET_MODE (op0);
6596 machine_mode tmode = insn_data[icode].operand[0].mode;
6597 machine_mode mode0 = insn_data[icode].operand[1].mode;
6598
6599 if (! target
6600 || GET_MODE (target) != tmode
6601 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
6602 target = gen_reg_rtx (tmode);
6603
6604 if (VECTOR_MODE_P (mode0))
6605 op0 = safe_vector_operand (op0, mode0);
6606
6607 if (op0mode == SImode && mode0 == HImode)
6608 {
6609 op0mode = HImode;
6610 op0 = gen_lowpart (HImode, op0);
6611 }
6612 gcc_assert (op0mode == mode0 || op0mode == VOIDmode);
6613
6614 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
6615 op0 = copy_to_mode_reg (mode0, op0);
6616
6617 pat = GEN_FCN (icode) (target, op0);
6618 if (! pat)
6619 return 0;
6620 emit_insn (pat);
6621 return target;
6622 }
6623
6624 /* Expand an expression EXP that calls a built-in function,
6625 with result going to TARGET if that's convenient
6626 (and in mode MODE if that's convenient).
6627 SUBTARGET may be used as the target for computing one of EXP's operands.
6628 IGNORE is nonzero if the value is to be ignored. */
6629
6630 static rtx
6631 c6x_expand_builtin (tree exp, rtx target ATTRIBUTE_UNUSED,
6632 rtx subtarget ATTRIBUTE_UNUSED,
6633 machine_mode mode ATTRIBUTE_UNUSED,
6634 int ignore ATTRIBUTE_UNUSED)
6635 {
6636 size_t i;
6637 const struct builtin_description *d;
6638 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
6639 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
6640
6641 for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
6642 if (d->code == fcode)
6643 return c6x_expand_binop_builtin (d->icode, exp, target,
6644 fcode == C6X_BUILTIN_CLRR);
6645
6646 for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
6647 if (d->code == fcode)
6648 return c6x_expand_unop_builtin (d->icode, exp, target);
6649
6650 gcc_unreachable ();
6651 }
6652
6653 /* Target unwind frame info is generated from dwarf CFI directives, so
6654 always output dwarf2 unwind info. */
6655
6656 static enum unwind_info_type
6657 c6x_debug_unwind_info (void)
6658 {
6659 if (flag_unwind_tables || flag_exceptions)
6660 return UI_DWARF2;
6661
6662 return default_debug_unwind_info ();
6663 }
6664
6665 /* Implement TARGET_HARD_REGNO_MODE_OK. */
6666
6667 static bool
6668 c6x_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
6669 {
6670 return GET_MODE_SIZE (mode) <= UNITS_PER_WORD || (regno & 1) == 0;
6671 }
6672
6673 /* Implement TARGET_MODES_TIEABLE_P. */
6674
6675 static bool
6676 c6x_modes_tieable_p (machine_mode mode1, machine_mode mode2)
6677 {
6678 return (mode1 == mode2
6679 || (GET_MODE_SIZE (mode1) <= UNITS_PER_WORD
6680 && GET_MODE_SIZE (mode2) <= UNITS_PER_WORD));
6681 }
6682
6683 /* Implement REGNO_REG_CLASS. */
6684
6685 enum reg_class
6686 c6x_regno_reg_class (int reg)
6687 {
6688 if (reg >= REG_A1 && reg <= REG_A2)
6689 return PREDICATE_A_REGS;
6690
6691 if (reg == REG_A0 && TARGET_INSNS_64)
6692 return PREDICATE_A_REGS;
6693
6694 if (reg >= REG_B0 && reg <= REG_B2)
6695 return PREDICATE_B_REGS;
6696
6697 if (A_REGNO_P (reg))
6698 return NONPREDICATE_A_REGS;
6699
6700 if (call_used_or_fixed_reg_p (reg))
6701 return CALL_USED_B_REGS;
6702
6703 return B_REGS;
6704 }
6705
6706 /* Target Structure. */
6707
6708 /* Initialize the GCC target structure. */
6709 #undef TARGET_FUNCTION_ARG
6710 #define TARGET_FUNCTION_ARG c6x_function_arg
6711 #undef TARGET_FUNCTION_ARG_ADVANCE
6712 #define TARGET_FUNCTION_ARG_ADVANCE c6x_function_arg_advance
6713 #undef TARGET_FUNCTION_ARG_BOUNDARY
6714 #define TARGET_FUNCTION_ARG_BOUNDARY c6x_function_arg_boundary
6715 #undef TARGET_FUNCTION_ARG_ROUND_BOUNDARY
6716 #define TARGET_FUNCTION_ARG_ROUND_BOUNDARY \
6717 c6x_function_arg_round_boundary
6718 #undef TARGET_FUNCTION_VALUE_REGNO_P
6719 #define TARGET_FUNCTION_VALUE_REGNO_P c6x_function_value_regno_p
6720 #undef TARGET_FUNCTION_VALUE
6721 #define TARGET_FUNCTION_VALUE c6x_function_value
6722 #undef TARGET_LIBCALL_VALUE
6723 #define TARGET_LIBCALL_VALUE c6x_libcall_value
6724 #undef TARGET_RETURN_IN_MEMORY
6725 #define TARGET_RETURN_IN_MEMORY c6x_return_in_memory
6726 #undef TARGET_RETURN_IN_MSB
6727 #define TARGET_RETURN_IN_MSB c6x_return_in_msb
6728 #undef TARGET_PASS_BY_REFERENCE
6729 #define TARGET_PASS_BY_REFERENCE c6x_pass_by_reference
6730 #undef TARGET_CALLEE_COPIES
6731 #define TARGET_CALLEE_COPIES hook_bool_CUMULATIVE_ARGS_arg_info_true
6732 #undef TARGET_STRUCT_VALUE_RTX
6733 #define TARGET_STRUCT_VALUE_RTX c6x_struct_value_rtx
6734 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
6735 #define TARGET_FUNCTION_OK_FOR_SIBCALL c6x_function_ok_for_sibcall
6736
6737 #undef TARGET_ASM_OUTPUT_MI_THUNK
6738 #define TARGET_ASM_OUTPUT_MI_THUNK c6x_output_mi_thunk
6739 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
6740 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK c6x_can_output_mi_thunk
6741
6742 #undef TARGET_BUILD_BUILTIN_VA_LIST
6743 #define TARGET_BUILD_BUILTIN_VA_LIST c6x_build_builtin_va_list
6744
6745 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
6746 #define TARGET_ASM_TRAMPOLINE_TEMPLATE c6x_asm_trampoline_template
6747 #undef TARGET_TRAMPOLINE_INIT
6748 #define TARGET_TRAMPOLINE_INIT c6x_initialize_trampoline
6749
6750 #undef TARGET_LEGITIMATE_CONSTANT_P
6751 #define TARGET_LEGITIMATE_CONSTANT_P c6x_legitimate_constant_p
6752 #undef TARGET_LEGITIMATE_ADDRESS_P
6753 #define TARGET_LEGITIMATE_ADDRESS_P c6x_legitimate_address_p
6754
6755 #undef TARGET_LRA_P
6756 #define TARGET_LRA_P hook_bool_void_false
6757
6758 #undef TARGET_IN_SMALL_DATA_P
6759 #define TARGET_IN_SMALL_DATA_P c6x_in_small_data_p
6760 #undef TARGET_ASM_SELECT_RTX_SECTION
6761 #define TARGET_ASM_SELECT_RTX_SECTION c6x_select_rtx_section
6762 #undef TARGET_ASM_SELECT_SECTION
6763 #define TARGET_ASM_SELECT_SECTION c6x_elf_select_section
6764 #undef TARGET_ASM_UNIQUE_SECTION
6765 #define TARGET_ASM_UNIQUE_SECTION c6x_elf_unique_section
6766 #undef TARGET_SECTION_TYPE_FLAGS
6767 #define TARGET_SECTION_TYPE_FLAGS c6x_section_type_flags
6768 #undef TARGET_HAVE_SRODATA_SECTION
6769 #define TARGET_HAVE_SRODATA_SECTION true
6770 #undef TARGET_ASM_MERGEABLE_RODATA_PREFIX
6771 #define TARGET_ASM_MERGEABLE_RODATA_PREFIX ".const"
6772
6773 #undef TARGET_OPTION_OVERRIDE
6774 #define TARGET_OPTION_OVERRIDE c6x_option_override
6775 #undef TARGET_CONDITIONAL_REGISTER_USAGE
6776 #define TARGET_CONDITIONAL_REGISTER_USAGE c6x_conditional_register_usage
6777
6778 #undef TARGET_INIT_LIBFUNCS
6779 #define TARGET_INIT_LIBFUNCS c6x_init_libfuncs
6780 #undef TARGET_LIBFUNC_GNU_PREFIX
6781 #define TARGET_LIBFUNC_GNU_PREFIX true
6782
6783 #undef TARGET_SCALAR_MODE_SUPPORTED_P
6784 #define TARGET_SCALAR_MODE_SUPPORTED_P c6x_scalar_mode_supported_p
6785 #undef TARGET_VECTOR_MODE_SUPPORTED_P
6786 #define TARGET_VECTOR_MODE_SUPPORTED_P c6x_vector_mode_supported_p
6787 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
6788 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE c6x_preferred_simd_mode
6789
6790 #undef TARGET_RTX_COSTS
6791 #define TARGET_RTX_COSTS c6x_rtx_costs
6792
6793 #undef TARGET_SCHED_INIT
6794 #define TARGET_SCHED_INIT c6x_sched_init
6795 #undef TARGET_SCHED_SET_SCHED_FLAGS
6796 #define TARGET_SCHED_SET_SCHED_FLAGS c6x_set_sched_flags
6797 #undef TARGET_SCHED_ADJUST_COST
6798 #define TARGET_SCHED_ADJUST_COST c6x_adjust_cost
6799 #undef TARGET_SCHED_ISSUE_RATE
6800 #define TARGET_SCHED_ISSUE_RATE c6x_issue_rate
6801 #undef TARGET_SCHED_VARIABLE_ISSUE
6802 #define TARGET_SCHED_VARIABLE_ISSUE c6x_variable_issue
6803 #undef TARGET_SCHED_REORDER
6804 #define TARGET_SCHED_REORDER c6x_sched_reorder
6805 #undef TARGET_SCHED_REORDER2
6806 #define TARGET_SCHED_REORDER2 c6x_sched_reorder2
6807 #undef TARGET_SCHED_DFA_NEW_CYCLE
6808 #define TARGET_SCHED_DFA_NEW_CYCLE c6x_dfa_new_cycle
6809 #undef TARGET_SCHED_DFA_PRE_CYCLE_INSN
6810 #define TARGET_SCHED_DFA_PRE_CYCLE_INSN c6x_sched_dfa_pre_cycle_insn
6811 #undef TARGET_SCHED_EXPOSED_PIPELINE
6812 #define TARGET_SCHED_EXPOSED_PIPELINE true
6813
6814 #undef TARGET_SCHED_ALLOC_SCHED_CONTEXT
6815 #define TARGET_SCHED_ALLOC_SCHED_CONTEXT c6x_alloc_sched_context
6816 #undef TARGET_SCHED_INIT_SCHED_CONTEXT
6817 #define TARGET_SCHED_INIT_SCHED_CONTEXT c6x_init_sched_context
6818 #undef TARGET_SCHED_SET_SCHED_CONTEXT
6819 #define TARGET_SCHED_SET_SCHED_CONTEXT c6x_set_sched_context
6820 #undef TARGET_SCHED_CLEAR_SCHED_CONTEXT
6821 #define TARGET_SCHED_CLEAR_SCHED_CONTEXT c6x_clear_sched_context
6822 #undef TARGET_SCHED_FREE_SCHED_CONTEXT
6823 #define TARGET_SCHED_FREE_SCHED_CONTEXT c6x_free_sched_context
6824
6825 #undef TARGET_CAN_ELIMINATE
6826 #define TARGET_CAN_ELIMINATE c6x_can_eliminate
6827
6828 #undef TARGET_PREFERRED_RENAME_CLASS
6829 #define TARGET_PREFERRED_RENAME_CLASS c6x_preferred_rename_class
6830
6831 #undef TARGET_MACHINE_DEPENDENT_REORG
6832 #define TARGET_MACHINE_DEPENDENT_REORG c6x_reorg
6833
6834 #undef TARGET_ASM_FILE_START
6835 #define TARGET_ASM_FILE_START c6x_file_start
6836
6837 #undef TARGET_PRINT_OPERAND
6838 #define TARGET_PRINT_OPERAND c6x_print_operand
6839 #undef TARGET_PRINT_OPERAND_ADDRESS
6840 #define TARGET_PRINT_OPERAND_ADDRESS c6x_print_operand_address
6841 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
6842 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P c6x_print_operand_punct_valid_p
6843
6844 /* C6x unwinding tables use a different format for the typeinfo tables. */
6845 #undef TARGET_ASM_TTYPE
6846 #define TARGET_ASM_TTYPE c6x_output_ttype
6847
6848 /* The C6x ABI follows the ARM EABI exception handling rules. */
6849 #undef TARGET_ARM_EABI_UNWINDER
6850 #define TARGET_ARM_EABI_UNWINDER true
6851
6852 #undef TARGET_ASM_EMIT_EXCEPT_PERSONALITY
6853 #define TARGET_ASM_EMIT_EXCEPT_PERSONALITY c6x_asm_emit_except_personality
6854
6855 #undef TARGET_ASM_INIT_SECTIONS
6856 #define TARGET_ASM_INIT_SECTIONS c6x_asm_init_sections
6857
6858 #undef TARGET_DEBUG_UNWIND_INFO
6859 #define TARGET_DEBUG_UNWIND_INFO c6x_debug_unwind_info
6860
6861 #undef TARGET_DWARF_REGISTER_SPAN
6862 #define TARGET_DWARF_REGISTER_SPAN c6x_dwarf_register_span
6863
6864 #undef TARGET_INIT_BUILTINS
6865 #define TARGET_INIT_BUILTINS c6x_init_builtins
6866 #undef TARGET_EXPAND_BUILTIN
6867 #define TARGET_EXPAND_BUILTIN c6x_expand_builtin
6868 #undef TARGET_BUILTIN_DECL
6869 #define TARGET_BUILTIN_DECL c6x_builtin_decl
6870
6871 #undef TARGET_HARD_REGNO_MODE_OK
6872 #define TARGET_HARD_REGNO_MODE_OK c6x_hard_regno_mode_ok
6873 #undef TARGET_MODES_TIEABLE_P
6874 #define TARGET_MODES_TIEABLE_P c6x_modes_tieable_p
6875
6876 struct gcc_target targetm = TARGET_INITIALIZER;
6877
6878 #include "gt-c6x.h"