Debian Codegraph @

gcc/config/visium/visium.cc

raw
1 /* Output routines for Visium.
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 Contributed by C.Nettleton, J.P.Parkes and P.Garbett.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #define IN_TARGET_CODE 1
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "backend.h"
27 #include "target.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "gimple-expr.h"
31 #include "df.h"
32 #include "memmodel.h"
33 #include "tm_p.h"
34 #include "stringpool.h"
35 #include "attribs.h"
36 #include "expmed.h"
37 #include "optabs.h"
38 #include "regs.h"
39 #include "emit-rtl.h"
40 #include "recog.h"
41 #include "diagnostic-core.h"
42 #include "alias.h"
43 #include "flags.h"
44 #include "fold-const.h"
45 #include "stor-layout.h"
46 #include "calls.h"
47 #include "varasm.h"
48 #include "output.h"
49 #include "insn-attr.h"
50 #include "explow.h"
51 #include "expr.h"
52 #include "gimplify.h"
53 #include "langhooks.h"
54 #include "reload.h"
55 #include "tm-constrs.h"
56 #include "tree-pass.h"
57 #include "context.h"
58 #include "builtins.h"
59 #include "opts.h"
60
61 /* This file should be included last. */
62 #include "target-def.h"
63
64 /* Enumeration of indexes into machine_libfunc_table. */
65 enum machine_libfunc_index
66 {
67 MLTI_long_int_memcpy,
68 MLTI_wrd_memcpy,
69 MLTI_byt_memcpy,
70
71 MLTI_long_int_memset,
72 MLTI_wrd_memset,
73 MLTI_byt_memset,
74
75 MLTI_set_trampoline_parity,
76
77 MLTI_MAX
78 };
79
80 struct GTY(()) machine_libfuncs
81 {
82 rtx table[MLTI_MAX];
83 };
84
85 /* The table of Visium-specific libfuncs. */
86 static GTY(()) struct machine_libfuncs visium_libfuncs;
87
88 #define vlt visium_libfuncs.table
89
90 /* Accessor macros for visium_libfuncs. */
91 #define long_int_memcpy_libfunc (vlt[MLTI_long_int_memcpy])
92 #define wrd_memcpy_libfunc (vlt[MLTI_wrd_memcpy])
93 #define byt_memcpy_libfunc (vlt[MLTI_byt_memcpy])
94 #define long_int_memset_libfunc (vlt[MLTI_long_int_memset])
95 #define wrd_memset_libfunc (vlt[MLTI_wrd_memset])
96 #define byt_memset_libfunc (vlt[MLTI_byt_memset])
97 #define set_trampoline_parity_libfunc (vlt[MLTI_set_trampoline_parity])
98
99 /* Machine specific function data. */
100 struct GTY (()) machine_function
101 {
102 /* Size of the frame of the function. */
103 int frame_size;
104
105 /* Size of the reg parm save area, non-zero only for functions with variable
106 argument list. We cannot use the crtl->args.pretend_args_size machinery
107 for this purpose because this size is added to virtual_incoming_args_rtx
108 to give the location of the first parameter passed by the caller on the
109 stack and virtual_incoming_args_rtx is also the location of the first
110 parameter on the stack. So crtl->args.pretend_args_size can be non-zero
111 only if the first non-register named parameter is not passed entirely on
112 the stack and this runs afoul of the need to have a reg parm save area
113 even with a variable argument list starting on the stack because of the
114 separate handling of general and floating-point registers. */
115 int reg_parm_save_area_size;
116
117 /* True if we have created an rtx which relies on the frame pointer. */
118 bool frame_needed;
119
120 /* True if we have exposed the flags register. From this moment on, we
121 cannot generate simple operations for integer registers. We could
122 use reload_completed for this purpose, but this would cripple the
123 postreload CSE and GCSE passes which run before postreload split. */
124 bool flags_exposed;
125 };
126
127 #define visium_frame_size cfun->machine->frame_size
128 #define visium_reg_parm_save_area_size cfun->machine->reg_parm_save_area_size
129 #define visium_frame_needed cfun->machine->frame_needed
130 #define visium_flags_exposed cfun->machine->flags_exposed
131
132 /* 1 if the next opcode is to be specially indented. */
133 int visium_indent_opcode = 0;
134
135 /* Register number used for long branches when LR isn't available. It
136 must be a call-used register since it isn't saved on function entry.
137 We do not care whether the branch is predicted or not on the GR6,
138 given how unlikely it is to have a long branch in a leaf function. */
139 static unsigned int long_branch_regnum = 31;
140
141 static tree visium_handle_interrupt_attr (tree *, tree, tree, int, bool *);
142 static inline bool current_function_saves_fp (void);
143 static inline bool current_function_saves_lr (void);
144 static inline bool current_function_has_lr_slot (void);
145
146 /* Supported attributes:
147 interrupt -- specifies this function is an interrupt handler. */
148 TARGET_GNU_ATTRIBUTES (visium_attribute_table,
149 {
150 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
151 affects_type_identity, handler, exclude } */
152 { "interrupt", 0, 0, true, false, false, false, visium_handle_interrupt_attr,
153 NULL}
154 });
155
156 static struct machine_function *visium_init_machine_status (void);
157
158 /* Target hooks and TARGET_INITIALIZER */
159
160 static bool visium_pass_by_reference (cumulative_args_t,
161 const function_arg_info &);
162
163 static rtx visium_function_arg (cumulative_args_t, const function_arg_info &);
164
165 static void visium_function_arg_advance (cumulative_args_t,
166 const function_arg_info &);
167
168 static bool visium_return_in_memory (const_tree, const_tree fntype);
169
170 static rtx visium_function_value (const_tree, const_tree fn_decl_or_type,
171 bool);
172
173 static rtx visium_libcall_value (machine_mode, const_rtx);
174
175 static void visium_setup_incoming_varargs (cumulative_args_t,
176 const function_arg_info &,
177 int *, int);
178
179 static void visium_va_start (tree valist, rtx nextarg);
180
181 static tree visium_gimplify_va_arg (tree, tree, gimple_seq *, gimple_seq *);
182
183 static bool visium_function_ok_for_sibcall (tree, tree);
184
185 static bool visium_frame_pointer_required (void);
186
187 static tree visium_build_builtin_va_list (void);
188
189 static rtx_insn *visium_md_asm_adjust (vec<rtx> &, vec<rtx> &,
190 vec<machine_mode> &,
191 vec<const char *> &, vec<rtx> &,
192 vec<rtx> &, HARD_REG_SET &, location_t);
193
194 static bool visium_legitimate_constant_p (machine_mode, rtx);
195
196 static bool visium_legitimate_address_p (machine_mode, rtx, bool,
197 code_helper = ERROR_MARK);
198
199 static bool visium_print_operand_punct_valid_p (unsigned char);
200 static void visium_print_operand (FILE *, rtx, int);
201 static void visium_print_operand_address (FILE *, machine_mode, rtx);
202
203 static void visium_conditional_register_usage (void);
204
205 static rtx visium_legitimize_address (rtx, rtx, machine_mode);
206
207 static reg_class_t visium_secondary_reload (bool, rtx, reg_class_t,
208 machine_mode,
209 secondary_reload_info *);
210
211 static bool visium_class_likely_spilled_p (reg_class_t);
212
213 static void visium_trampoline_init (rtx, tree, rtx);
214
215 static int visium_issue_rate (void);
216
217 static int visium_adjust_priority (rtx_insn *, int);
218
219 static int visium_adjust_cost (rtx_insn *, int, rtx_insn *, int, unsigned int);
220
221 static int visium_register_move_cost (machine_mode, reg_class_t,
222 reg_class_t);
223
224 static int visium_memory_move_cost (machine_mode, reg_class_t, bool);
225
226 static bool visium_rtx_costs (rtx, machine_mode, int, int, int *, bool);
227
228 static void visium_option_override (void);
229
230 static void visium_init_libfuncs (void);
231
232 static unsigned int visium_reorg (void);
233
234 static unsigned int visium_hard_regno_nregs (unsigned int, machine_mode);
235
236 static bool visium_hard_regno_mode_ok (unsigned int, machine_mode);
237
238 static bool visium_modes_tieable_p (machine_mode, machine_mode);
239
240 static bool visium_can_change_mode_class (machine_mode, machine_mode,
241 reg_class_t);
242
243 static HOST_WIDE_INT visium_constant_alignment (const_tree, HOST_WIDE_INT);
244
245 /* Setup the global target hooks structure. */
246
247 #undef TARGET_MAX_ANCHOR_OFFSET
248 #define TARGET_MAX_ANCHOR_OFFSET 31
249
250 #undef TARGET_PASS_BY_REFERENCE
251 #define TARGET_PASS_BY_REFERENCE visium_pass_by_reference
252
253 #undef TARGET_FUNCTION_ARG
254 #define TARGET_FUNCTION_ARG visium_function_arg
255
256 #undef TARGET_FUNCTION_ARG_ADVANCE
257 #define TARGET_FUNCTION_ARG_ADVANCE visium_function_arg_advance
258
259 #undef TARGET_RETURN_IN_MEMORY
260 #define TARGET_RETURN_IN_MEMORY visium_return_in_memory
261
262 #undef TARGET_FUNCTION_VALUE
263 #define TARGET_FUNCTION_VALUE visium_function_value
264
265 #undef TARGET_LIBCALL_VALUE
266 #define TARGET_LIBCALL_VALUE visium_libcall_value
267
268 #undef TARGET_SETUP_INCOMING_VARARGS
269 #define TARGET_SETUP_INCOMING_VARARGS visium_setup_incoming_varargs
270
271 #undef TARGET_EXPAND_BUILTIN_VA_START
272 #define TARGET_EXPAND_BUILTIN_VA_START visium_va_start
273
274 #undef TARGET_BUILD_BUILTIN_VA_LIST
275 #define TARGET_BUILD_BUILTIN_VA_LIST visium_build_builtin_va_list
276
277 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
278 #define TARGET_GIMPLIFY_VA_ARG_EXPR visium_gimplify_va_arg
279
280 #undef TARGET_LEGITIMATE_CONSTANT_P
281 #define TARGET_LEGITIMATE_CONSTANT_P visium_legitimate_constant_p
282
283 #undef TARGET_LRA_P
284 #define TARGET_LRA_P hook_bool_void_false
285
286 #undef TARGET_LEGITIMATE_ADDRESS_P
287 #define TARGET_LEGITIMATE_ADDRESS_P visium_legitimate_address_p
288
289 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
290 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P visium_print_operand_punct_valid_p
291
292 #undef TARGET_PRINT_OPERAND
293 #define TARGET_PRINT_OPERAND visium_print_operand
294
295 #undef TARGET_PRINT_OPERAND_ADDRESS
296 #define TARGET_PRINT_OPERAND_ADDRESS visium_print_operand_address
297
298 #undef TARGET_ATTRIBUTE_TABLE
299 #define TARGET_ATTRIBUTE_TABLE visium_attribute_table
300
301 #undef TARGET_ADDRESS_COST
302 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
303
304 #undef TARGET_STRICT_ARGUMENT_NAMING
305 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
306
307 #undef TARGET_SCHED_ISSUE_RATE
308 #define TARGET_SCHED_ISSUE_RATE visium_issue_rate
309
310 #undef TARGET_SCHED_ADJUST_PRIORITY
311 #define TARGET_SCHED_ADJUST_PRIORITY visium_adjust_priority
312
313 #undef TARGET_SCHED_ADJUST_COST
314 #define TARGET_SCHED_ADJUST_COST visium_adjust_cost
315
316 #undef TARGET_MEMORY_MOVE_COST
317 #define TARGET_MEMORY_MOVE_COST visium_memory_move_cost
318
319 #undef TARGET_REGISTER_MOVE_COST
320 #define TARGET_REGISTER_MOVE_COST visium_register_move_cost
321
322 #undef TARGET_RTX_COSTS
323 #define TARGET_RTX_COSTS visium_rtx_costs
324
325 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
326 #define TARGET_FUNCTION_OK_FOR_SIBCALL visium_function_ok_for_sibcall
327
328 #undef TARGET_FRAME_POINTER_REQUIRED
329 #define TARGET_FRAME_POINTER_REQUIRED visium_frame_pointer_required
330
331 #undef TARGET_SECONDARY_RELOAD
332 #define TARGET_SECONDARY_RELOAD visium_secondary_reload
333
334 #undef TARGET_CLASS_LIKELY_SPILLED_P
335 #define TARGET_CLASS_LIKELY_SPILLED_P visium_class_likely_spilled_p
336
337 #undef TARGET_LEGITIMIZE_ADDRESS
338 #define TARGET_LEGITIMIZE_ADDRESS visium_legitimize_address
339
340 #undef TARGET_OPTION_OVERRIDE
341 #define TARGET_OPTION_OVERRIDE visium_option_override
342
343 #undef TARGET_INIT_LIBFUNCS
344 #define TARGET_INIT_LIBFUNCS visium_init_libfuncs
345
346 #undef TARGET_CONDITIONAL_REGISTER_USAGE
347 #define TARGET_CONDITIONAL_REGISTER_USAGE visium_conditional_register_usage
348
349 #undef TARGET_TRAMPOLINE_INIT
350 #define TARGET_TRAMPOLINE_INIT visium_trampoline_init
351
352 #undef TARGET_MD_ASM_ADJUST
353 #define TARGET_MD_ASM_ADJUST visium_md_asm_adjust
354
355 #undef TARGET_FLAGS_REGNUM
356 #define TARGET_FLAGS_REGNUM FLAGS_REGNUM
357
358 #undef TARGET_HARD_REGNO_NREGS
359 #define TARGET_HARD_REGNO_NREGS visium_hard_regno_nregs
360
361 #undef TARGET_HARD_REGNO_MODE_OK
362 #define TARGET_HARD_REGNO_MODE_OK visium_hard_regno_mode_ok
363
364 #undef TARGET_MODES_TIEABLE_P
365 #define TARGET_MODES_TIEABLE_P visium_modes_tieable_p
366
367 #undef TARGET_CAN_CHANGE_MODE_CLASS
368 #define TARGET_CAN_CHANGE_MODE_CLASS visium_can_change_mode_class
369
370 #undef TARGET_CONSTANT_ALIGNMENT
371 #define TARGET_CONSTANT_ALIGNMENT visium_constant_alignment
372
373 #undef TARGET_HAVE_SPECULATION_SAFE_VALUE
374 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
375
376 #undef TARGET_DOCUMENTATION_NAME
377 #define TARGET_DOCUMENTATION_NAME "Visium"
378
379 struct gcc_target targetm = TARGET_INITIALIZER;
380
381 namespace {
382
383 const pass_data pass_data_visium_reorg =
384 {
385 RTL_PASS, /* type */
386 "mach2", /* name */
387 OPTGROUP_NONE, /* optinfo_flags */
388 TV_MACH_DEP, /* tv_id */
389 0, /* properties_required */
390 0, /* properties_provided */
391 0, /* properties_destroyed */
392 0, /* todo_flags_start */
393 0, /* todo_flags_finish */
394 };
395
396 class pass_visium_reorg : public rtl_opt_pass
397 {
398 public:
399 pass_visium_reorg(gcc::context *ctxt)
400 : rtl_opt_pass(pass_data_visium_reorg, ctxt)
401 {}
402
403 /* opt_pass methods: */
404 virtual unsigned int execute (function *)
405 {
406 return visium_reorg ();
407 }
408
409 }; // class pass_work_around_errata
410
411 } // anon namespace
412
413 rtl_opt_pass *
414 make_pass_visium_reorg (gcc::context *ctxt)
415 {
416 return new pass_visium_reorg (ctxt);
417 }
418
419 /* Options override for Visium. */
420
421 static void
422 visium_option_override (void)
423 {
424 if (flag_pic == 1)
425 warning (OPT_fpic, "%<-fpic%> is not supported");
426 if (flag_pic == 2)
427 warning (OPT_fPIC, "%<-fPIC%> is not supported");
428
429 /* MCM is the default in the GR5/GR6 era. */
430 target_flags |= MASK_MCM;
431
432 /* FPU is the default with MCM, but don't override an explicit option. */
433 if ((target_flags_explicit & MASK_FPU) == 0)
434 target_flags |= MASK_FPU;
435
436 /* The supervisor mode is the default. */
437 if ((target_flags_explicit & MASK_SV_MODE) == 0)
438 target_flags |= MASK_SV_MODE;
439
440 /* The GR6 has the Block Move Instructions and an IEEE-compliant FPU. */
441 if (visium_cpu_and_features == PROCESSOR_GR6)
442 {
443 target_flags |= MASK_BMI;
444 if (target_flags & MASK_FPU)
445 target_flags |= MASK_FPU_IEEE;
446 }
447
448 /* Set -mtune from -mcpu if not specified. */
449 if (!OPTION_SET_P (visium_cpu))
450 visium_cpu = visium_cpu_and_features;
451
452 /* Align functions on 256-byte (32-quadword) for GR5 and 64-byte (8-quadword)
453 boundaries for GR6 so they start a new burst mode window. */
454 if (flag_align_functions && !str_align_functions)
455 {
456 if (visium_cpu == PROCESSOR_GR6)
457 str_align_functions = "64";
458 else
459 str_align_functions = "256";
460
461 /* Allow the size of compilation units to double because of inlining.
462 In practice the global size of the object code is hardly affected
463 because the additional instructions will take up the padding. */
464 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
465 param_inline_unit_growth, 100);
466 }
467
468 /* Likewise for loops. */
469 if (flag_align_loops && !str_align_loops)
470 {
471 if (visium_cpu == PROCESSOR_GR6)
472 str_align_loops = "64";
473 else
474 {
475 /* But not if they are too far away from a 256-byte boundary. */
476 str_align_loops = "256:32:8";
477 }
478 }
479
480 /* Align all jumps on quadword boundaries for the burst mode, and even
481 on 8-quadword boundaries for GR6 so they start a new window. */
482 if (flag_align_jumps && !str_align_jumps)
483 {
484 if (visium_cpu == PROCESSOR_GR6)
485 str_align_jumps = "64";
486 else
487 str_align_jumps = "8";
488 }
489 }
490
491 /* Register the Visium-specific libfuncs with the middle-end. */
492
493 static void
494 visium_init_libfuncs (void)
495 {
496 if (!TARGET_BMI)
497 long_int_memcpy_libfunc = init_one_libfunc ("__long_int_memcpy");
498 wrd_memcpy_libfunc = init_one_libfunc ("__wrd_memcpy");
499 byt_memcpy_libfunc = init_one_libfunc ("__byt_memcpy");
500
501 long_int_memset_libfunc = init_one_libfunc ("__long_int_memset");
502 wrd_memset_libfunc = init_one_libfunc ("__wrd_memset");
503 byt_memset_libfunc = init_one_libfunc ("__byt_memset");
504
505 set_trampoline_parity_libfunc = init_one_libfunc ("__set_trampoline_parity");
506 }
507
508 /* Return the number of instructions that can issue on the same cycle. */
509
510 static int
511 visium_issue_rate (void)
512 {
513 switch (visium_cpu)
514 {
515 case PROCESSOR_GR5:
516 return 1;
517
518 case PROCESSOR_GR6:
519 return 2;
520
521 default:
522 gcc_unreachable ();
523 }
524 }
525
526 /* Return the adjusted PRIORITY of INSN. */
527
528 static int
529 visium_adjust_priority (rtx_insn *insn, int priority)
530 {
531 /* On the GR5, we slightly increase the priority of writes in order to avoid
532 scheduling a read on the next cycle. This is necessary in addition to the
533 associated insn reservation because there are no data dependencies.
534 We also slightly increase the priority of reads from ROM in order to group
535 them as much as possible. These reads are a bit problematic because they
536 conflict with the instruction fetches, i.e. the data and instruction buses
537 tread on each other's toes when they are executed. */
538 if (visium_cpu == PROCESSOR_GR5
539 && reload_completed
540 && INSN_P (insn)
541 && recog_memoized (insn) >= 0)
542 {
543 enum attr_type attr_type = get_attr_type (insn);
544 if (attr_type == TYPE_REG_MEM
545 || (attr_type == TYPE_MEM_REG
546 && MEM_READONLY_P (SET_SRC (PATTERN (insn)))))
547 return priority + 1;
548 }
549
550 return priority;
551 }
552
553 /* Adjust the cost of a scheduling dependency. Return the new cost of
554 a dependency LINK of INSN on DEP_INSN. COST is the current cost. */
555
556 static int
557 visium_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn, int cost,
558 unsigned int)
559 {
560 enum attr_type attr_type;
561
562 /* Don't adjust costs for true dependencies as they are described with
563 bypasses. But we make an exception for the first scheduling pass to
564 help the subsequent postreload compare elimination pass. */
565 if (dep_type == REG_DEP_TRUE)
566 {
567 if (!reload_completed
568 && recog_memoized (insn) >= 0
569 && get_attr_type (insn) == TYPE_CMP)
570 {
571 rtx pat = PATTERN (insn);
572 gcc_assert (GET_CODE (pat) == SET);
573 rtx src = SET_SRC (pat);
574
575 /* Only the branches can be modified by the postreload compare
576 elimination pass, not the cstores because they accept only
577 unsigned comparison operators and they are eliminated if
578 one of the operands is zero. */
579 if (GET_CODE (src) == IF_THEN_ELSE
580 && XEXP (XEXP (src, 0), 1) == const0_rtx
581 && recog_memoized (dep_insn) >= 0)
582 {
583 enum attr_type dep_attr_type = get_attr_type (dep_insn);
584
585 /* The logical instructions use CCmode and thus work with any
586 comparison operator, whereas the arithmetic instructions use
587 CCNZmode and thus work with only a small subset. */
588 if (dep_attr_type == TYPE_LOGIC
589 || (dep_attr_type == TYPE_ARITH
590 && visium_nz_comparison_operator (XEXP (src, 0),
591 GET_MODE
592 (XEXP (src, 0)))))
593 return 0;
594 }
595 }
596
597 return cost;
598 }
599
600 if (recog_memoized (insn) < 0)
601 return 0;
602
603 attr_type = get_attr_type (insn);
604
605 /* Anti dependency: DEP_INSN reads a register that INSN writes some
606 cycles later. */
607 if (dep_type == REG_DEP_ANTI)
608 {
609 /* On the GR5, the latency of FP instructions needs to be taken into
610 account for every dependency involving a write. */
611 if (attr_type == TYPE_REG_FP && visium_cpu == PROCESSOR_GR5)
612 {
613 /* INSN is FLOAD. */
614 rtx pat = PATTERN (insn);
615 rtx dep_pat = PATTERN (dep_insn);
616
617 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
618 /* If this happens, we have to extend this to schedule
619 optimally. Return 0 for now. */
620 return 0;
621
622 if (reg_mentioned_p (SET_DEST (pat), SET_SRC (dep_pat)))
623 {
624 if (recog_memoized (dep_insn) < 0)
625 return 0;
626
627 switch (get_attr_type (dep_insn))
628 {
629 case TYPE_FDIV:
630 case TYPE_FSQRT:
631 case TYPE_FTOI:
632 case TYPE_ITOF:
633 case TYPE_FP:
634 case TYPE_FMOVE:
635 /* A fload can't be issued until a preceding arithmetic
636 operation has finished if the target of the fload is
637 any of the sources (or destination) of the arithmetic
638 operation. Note that the latency may be (much)
639 greater than this if the preceding instruction
640 concerned is in a queue. */
641 return insn_default_latency (dep_insn);
642
643 default:
644 return 0;
645 }
646 }
647 }
648
649 /* On the GR6, we try to make sure that the link register is restored
650 sufficiently ahead of the return as to yield a correct prediction
651 from the branch predictor. By default there is no true dependency
652 but an anti dependency between them, so we simply reuse it. */
653 else if (attr_type == TYPE_RET && visium_cpu == PROCESSOR_GR6)
654 {
655 rtx dep_pat = PATTERN (dep_insn);
656 if (GET_CODE (dep_pat) == SET
657 && REG_P (SET_DEST (dep_pat))
658 && REGNO (SET_DEST (dep_pat)) == LINK_REGNUM)
659 return 8;
660 }
661
662 /* For other anti dependencies, the cost is 0. */
663 return 0;
664 }
665
666 /* Output dependency: DEP_INSN writes a register that INSN writes some
667 cycles later. */
668 else if (dep_type == REG_DEP_OUTPUT)
669 {
670 /* On the GR5, the latency of FP instructions needs to be taken into
671 account for every dependency involving a write. */
672 if (attr_type == TYPE_REG_FP && visium_cpu == PROCESSOR_GR5)
673 {
674 /* INSN is FLOAD. */
675 rtx pat = PATTERN (insn);
676 rtx dep_pat = PATTERN (dep_insn);
677
678 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
679 /* If this happens, we have to extend this to schedule
680 optimally. Return 0 for now. */
681 return 0;
682
683 if (reg_mentioned_p (SET_DEST (pat), SET_DEST (dep_pat)))
684 {
685 if (recog_memoized (dep_insn) < 0)
686 return 0;
687
688 switch (get_attr_type (dep_insn))
689 {
690 case TYPE_FDIV:
691 case TYPE_FSQRT:
692 case TYPE_FTOI:
693 case TYPE_ITOF:
694 case TYPE_FP:
695 case TYPE_FMOVE:
696 /* A fload can't be issued until a preceding arithmetic
697 operation has finished if the target of the fload is
698 the destination of the arithmetic operation. Note that
699 the latency may be (much) greater than this if the
700 preceding instruction concerned is in a queue. */
701 return insn_default_latency (dep_insn);
702
703 default:
704 return 0;
705 }
706 }
707 }
708
709 /* For other output dependencies, the cost is 0. */
710 return 0;
711 }
712
713 return 0;
714 }
715
716 /* Handle an "interrupt_handler" attribute; arguments as in
717 struct attribute_spec.handler. */
718
719 static tree
720 visium_handle_interrupt_attr (tree *node, tree name,
721 tree args ATTRIBUTE_UNUSED,
722 int flags ATTRIBUTE_UNUSED,
723 bool *no_add_attrs)
724 {
725 if (TREE_CODE (*node) != FUNCTION_DECL)
726 {
727 warning (OPT_Wattributes, "%qE attribute only applies to functions",
728 name);
729 *no_add_attrs = true;
730 }
731 else if (!TARGET_SV_MODE)
732 {
733 error ("an interrupt handler cannot be compiled with %<-muser-mode%>");
734 *no_add_attrs = true;
735 }
736
737 return NULL_TREE;
738 }
739
740 /* Return non-zero if the current function is an interrupt function. */
741
742 int
743 visium_interrupt_function_p (void)
744 {
745 return
746 lookup_attribute ("interrupt",
747 DECL_ATTRIBUTES (current_function_decl)) != NULL_TREE;
748 }
749
750 /* Conditionally modify the settings of the register file. */
751
752 static void
753 visium_conditional_register_usage (void)
754 {
755 /* If the supervisor mode is disabled, mask some general registers. */
756 if (!TARGET_SV_MODE)
757 {
758 if (visium_cpu_and_features == PROCESSOR_GR5)
759 {
760 fixed_regs[24] = 1;
761 fixed_regs[25] = 1;
762 fixed_regs[26] = 1;
763 fixed_regs[27] = 1;
764 fixed_regs[28] = 1;
765 call_used_regs[24] = 0;
766 call_used_regs[25] = 0;
767 call_used_regs[26] = 0;
768 call_used_regs[27] = 0;
769 call_used_regs[28] = 0;
770 }
771
772 fixed_regs[31] = 1;
773 call_used_regs[31] = 0;
774
775 /* We also need to change the long-branch register. */
776 if (visium_cpu_and_features == PROCESSOR_GR5)
777 long_branch_regnum = 20;
778 else
779 long_branch_regnum = 28;
780 }
781
782 /* If the FPU is disabled, mask the FP registers. */
783 if (!TARGET_FPU)
784 {
785 for (int i = FP_FIRST_REGNUM; i <= FP_LAST_REGNUM; i++)
786 {
787 fixed_regs[i] = 1;
788 call_used_regs[i] = 0;
789 }
790 }
791 }
792
793 /* Prepend to CLOBBERS hard registers that are automatically clobbered for
794 an asm We do this for the FLAGS to maintain source compatibility with
795 the original cc0-based compiler. */
796
797 static rtx_insn *
798 visium_md_asm_adjust (vec<rtx> & /*outputs*/, vec<rtx> & /*inputs*/,
799 vec<machine_mode> & /*input_modes*/,
800 vec<const char *> & /*constraints*/,
801 vec<rtx> &/*uses*/, vec<rtx> &clobbers,
802 HARD_REG_SET &clobbered_regs, location_t /*loc*/)
803 {
804 clobbers.safe_push (gen_rtx_REG (CCmode, FLAGS_REGNUM));
805 SET_HARD_REG_BIT (clobbered_regs, FLAGS_REGNUM);
806 return NULL;
807 }
808
809 /* Return true if X is a legitimate constant for a MODE immediate operand.
810 X is guaranteed to satisfy the CONSTANT_P predicate. */
811
812 static bool
813 visium_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED,
814 rtx x ATTRIBUTE_UNUSED)
815 {
816 return true;
817 }
818
819 /* Compute the alignment for a variable. The alignment of an aggregate is
820 set to be at least that of a scalar less than or equal to it in size. */
821
822 unsigned int
823 visium_data_alignment (tree type, unsigned int align)
824 {
825 if (AGGREGATE_TYPE_P (type)
826 && TYPE_SIZE (type)
827 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST && align < 32)
828 {
829 if (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 32)
830 return 32;
831
832 if (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16 && align < 16)
833 return 16;
834 }
835
836 return align;
837 }
838
839 /* Implement TARGET_CONSTANT_ALIGNMENT. */
840
841 static HOST_WIDE_INT
842 visium_constant_alignment (const_tree exp, HOST_WIDE_INT align)
843 {
844 return visium_data_alignment (TREE_TYPE (exp), align);
845 }
846
847 /* Helper function for HARD_REGNO_RENAME_OK (FROM, TO). Return non-zero if
848 it is OK to rename a hard register FROM to another hard register TO. */
849
850 int
851 visium_hard_regno_rename_ok (unsigned int from ATTRIBUTE_UNUSED,
852 unsigned int to)
853 {
854 /* If the function doesn't save LR, then the long-branch register will be
855 used for long branches so we need to know whether it is live before the
856 frame layout is computed. */
857 if (!current_function_saves_lr () && to == long_branch_regnum)
858 return 0;
859
860 /* Interrupt functions can only use registers that have already been
861 saved by the prologue, even if they would normally be call-clobbered. */
862 if (crtl->is_leaf
863 && !df_regs_ever_live_p (to)
864 && visium_interrupt_function_p ())
865 return 0;
866
867 return 1;
868 }
869
870 /* Implement TARGET_HARD_REGNO_NREGS. */
871
872 static unsigned int
873 visium_hard_regno_nregs (unsigned int regno, machine_mode mode)
874 {
875 if (regno == MDB_REGNUM)
876 return CEIL (GET_MODE_SIZE (mode), 2 * UNITS_PER_WORD);
877 return CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD);
878 }
879
880 /* Implement TARGET_HARD_REGNO_MODE_OK.
881
882 Modes with sizes which cross from the one register class to the
883 other cannot be allowed. Only single floats are allowed in the
884 floating point registers, and only fixed point values in the EAM
885 registers. */
886
887 static bool
888 visium_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
889 {
890 if (GP_REGISTER_P (regno))
891 return GP_REGISTER_P (end_hard_regno (mode, regno) - 1);
892
893 if (FP_REGISTER_P (regno))
894 return mode == SFmode || (mode == SImode && TARGET_FPU_IEEE);
895
896 return (GET_MODE_CLASS (mode) == MODE_INT
897 && visium_hard_regno_nregs (regno, mode) == 1);
898 }
899
900 /* Implement TARGET_MODES_TIEABLE_P. */
901
902 static bool
903 visium_modes_tieable_p (machine_mode mode1, machine_mode mode2)
904 {
905 return (GET_MODE_CLASS (mode1) == MODE_INT
906 && GET_MODE_CLASS (mode2) == MODE_INT);
907 }
908
909 /* Return true if it is ok to do sibling call optimization for the specified
910 call expression EXP. DECL will be the called function, or NULL if this
911 is an indirect call. */
912
913 static bool
914 visium_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
915 tree exp ATTRIBUTE_UNUSED)
916 {
917 return !visium_interrupt_function_p ();
918 }
919
920 /* Prepare operands for a move define_expand in MODE. */
921
922 void
923 prepare_move_operands (rtx *operands, machine_mode mode)
924 {
925 /* If the output is not a register, the input must be. */
926 if (GET_CODE (operands[0]) == MEM && !reg_or_0_operand (operands[1], mode))
927 operands[1] = force_reg (mode, operands[1]);
928 }
929
930 /* Return true if the operands are valid for a simple move insn. */
931
932 bool
933 ok_for_simple_move_operands (rtx *operands, machine_mode mode)
934 {
935 /* One of the operands must be a register. */
936 if (!register_operand (operands[0], mode)
937 && !reg_or_0_operand (operands[1], mode))
938 return false;
939
940 /* Once the flags are exposed, no simple moves between integer registers. */
941 if (visium_flags_exposed
942 && gpc_reg_operand (operands[0], mode)
943 && gpc_reg_operand (operands[1], mode))
944 return false;
945
946 return true;
947 }
948
949 /* Return true if the operands are valid for a simple move strict insn. */
950
951 bool
952 ok_for_simple_move_strict_operands (rtx *operands, machine_mode mode)
953 {
954 /* Once the flags are exposed, no simple moves between integer registers.
955 Note that, in QImode only, a zero source counts as an integer register
956 since it will be emitted as r0. */
957 if (visium_flags_exposed
958 && gpc_reg_operand (operands[0], mode)
959 && (gpc_reg_operand (operands[1], mode)
960 || (mode == QImode && operands[1] == const0_rtx)))
961 return false;
962
963 return true;
964 }
965
966 /* Return true if the operands are valid for a simple arithmetic or logical
967 insn. */
968
969 bool
970 ok_for_simple_arith_logic_operands (rtx *, machine_mode)
971 {
972 /* Once the flags are exposed, no simple arithmetic or logical operations
973 between integer registers. */
974 return !visium_flags_exposed;
975 }
976
977 /* Return non-zero if a branch or call instruction will be emitting a nop
978 into its delay slot. */
979
980 int
981 empty_delay_slot (rtx_insn *insn)
982 {
983 rtx seq;
984
985 /* If no previous instruction (should not happen), return true. */
986 if (PREV_INSN (insn) == NULL)
987 return 1;
988
989 seq = NEXT_INSN (PREV_INSN (insn));
990 if (GET_CODE (PATTERN (seq)) == SEQUENCE)
991 return 0;
992
993 return 1;
994 }
995
996 /* Wrapper around single_set which returns the second SET of a pair if the
997 first SET is to the flags register. */
998
999 static rtx
1000 single_set_and_flags (rtx_insn *insn)
1001 {
1002 if (multiple_sets (insn))
1003 {
1004 rtx pat = PATTERN (insn);
1005 if (XVECLEN (pat, 0) == 2
1006 && GET_CODE (XVECEXP (pat, 0, 0)) == SET
1007 && REG_P (SET_DEST (XVECEXP (pat, 0, 0)))
1008 && REGNO (SET_DEST (XVECEXP (pat, 0, 0))) == FLAGS_REGNUM)
1009 return XVECEXP (pat, 0, 1);
1010 }
1011
1012 return single_set (insn);
1013 }
1014
1015 /* This is called with OUT_INSN an instruction setting a (base) register
1016 and IN_INSN a read or a write. Return 1 if these instructions together
1017 constitute a pipeline hazard.
1018
1019 On the original architecture, a pipeline data hazard occurs when the Dest
1020 of one instruction becomes the SrcA for an immediately following READ or
1021 WRITE instruction with a non-zero index (indexing occurs at the decode
1022 stage and so a NOP must be inserted in-between for this to work).
1023
1024 An example is:
1025
1026 move.l r2,r1
1027 read.l r4,10(r2)
1028
1029 On the MCM, the non-zero index condition is lifted but the hazard is
1030 patched up by the hardware through the injection of wait states:
1031
1032 move.l r2,r1
1033 read.l r4,(r2)
1034
1035 We nevertheless try to schedule instructions around this. */
1036
1037 int
1038 gr5_hazard_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
1039 {
1040 rtx out_set, in_set, dest, memexpr;
1041 unsigned int out_reg, in_reg;
1042
1043 /* A CALL is storage register class, but the link register is of no
1044 interest here. */
1045 if (GET_CODE (out_insn) == CALL_INSN)
1046 return 0;
1047
1048 out_set = single_set_and_flags (out_insn);
1049 dest = SET_DEST (out_set);
1050
1051 /* Should be no stall/hazard if OUT_INSN is MEM := ???. This only
1052 occurs prior to reload. */
1053 if (GET_CODE (dest) == MEM)
1054 return 0;
1055
1056 if (GET_CODE (dest) == STRICT_LOW_PART)
1057 dest = XEXP (dest, 0);
1058 if (GET_CODE (dest) == SUBREG)
1059 dest = SUBREG_REG (dest);
1060 out_reg = REGNO (dest);
1061
1062 in_set = single_set_and_flags (in_insn);
1063
1064 /* If IN_INSN is MEM := MEM, it's the source that counts. */
1065 if (GET_CODE (SET_SRC (in_set)) == MEM)
1066 memexpr = XEXP (SET_SRC (in_set), 0);
1067 else
1068 memexpr = XEXP (SET_DEST (in_set), 0);
1069
1070 if (GET_CODE (memexpr) == PLUS)
1071 {
1072 memexpr = XEXP (memexpr, 0);
1073 if (GET_CODE (memexpr) == SUBREG)
1074 in_reg = REGNO (SUBREG_REG (memexpr));
1075 else
1076 in_reg = REGNO (memexpr);
1077
1078 if (in_reg == out_reg)
1079 return 1;
1080 }
1081 else if (TARGET_MCM)
1082 {
1083 if (GET_CODE (memexpr) == STRICT_LOW_PART)
1084 memexpr = XEXP (memexpr, 0);
1085 if (GET_CODE (memexpr) == SUBREG)
1086 memexpr = SUBREG_REG (memexpr);
1087 in_reg = REGNO (memexpr);
1088
1089 if (in_reg == out_reg)
1090 return 1;
1091 }
1092
1093 return 0;
1094 }
1095
1096 /* Return true if INSN is an empty asm instruction. */
1097
1098 static bool
1099 empty_asm_p (rtx insn)
1100 {
1101 rtx body = PATTERN (insn);
1102 const char *templ;
1103
1104 if (GET_CODE (body) == ASM_INPUT)
1105 templ = XSTR (body, 0);
1106 else if (asm_noperands (body) >= 0)
1107 templ = decode_asm_operands (body, NULL, NULL, NULL, NULL, NULL);
1108 else
1109 templ = NULL;
1110
1111 return (templ && templ[0] == '\0');
1112 }
1113
1114 /* Insert a NOP immediately before INSN wherever there is a pipeline hazard.
1115 LAST_REG records the register set in the last insn and LAST_INSN_CALL
1116 records whether the last insn was a call insn. */
1117
1118 static void
1119 gr5_avoid_hazard (rtx_insn *insn, unsigned int *last_reg, bool *last_insn_call)
1120 {
1121 unsigned int dest_reg = 0;
1122 rtx set;
1123
1124 switch (GET_CODE (insn))
1125 {
1126 case CALL_INSN:
1127 *last_reg = 0;
1128 *last_insn_call = true;
1129 return;
1130
1131 case JUMP_INSN:
1132 /* If this is an empty asm, just skip it. */
1133 if (!empty_asm_p (insn))
1134 {
1135 *last_reg = 0;
1136 *last_insn_call = false;
1137 }
1138 return;
1139
1140 case INSN:
1141 /* If this is an empty asm, just skip it. */
1142 if (empty_asm_p (insn))
1143 return;
1144 break;
1145
1146 default:
1147 return;
1148 }
1149
1150 set = single_set_and_flags (insn);
1151 if (set != NULL_RTX)
1152 {
1153 rtx dest = SET_DEST (set);
1154 const bool double_p = GET_MODE_SIZE (GET_MODE (dest)) > UNITS_PER_WORD;
1155 rtx memrtx = NULL;
1156
1157 if (GET_CODE (SET_SRC (set)) == MEM)
1158 {
1159 memrtx = XEXP (SET_SRC (set), 0);
1160 if (GET_CODE (dest) == STRICT_LOW_PART)
1161 dest = XEXP (dest, 0);
1162 if (REG_P (dest))
1163 dest_reg = REGNO (dest);
1164
1165 /* If this is a DI or DF mode memory to register
1166 copy, then if rd = rs we get
1167
1168 rs + 1 := 1[rs]
1169 rs := [rs]
1170
1171 otherwise the order is
1172
1173 rd := [rs]
1174 rd + 1 := 1[rs] */
1175
1176 if (double_p)
1177 {
1178 unsigned int base_reg;
1179
1180 if (GET_CODE (memrtx) == PLUS)
1181 base_reg = REGNO (XEXP (memrtx, 0));
1182 else
1183 base_reg = REGNO (memrtx);
1184
1185 if (dest_reg != base_reg)
1186 dest_reg++;
1187 }
1188 }
1189
1190 else if (GET_CODE (dest) == MEM)
1191 memrtx = XEXP (dest, 0);
1192
1193 else if (GET_MODE_CLASS (GET_MODE (dest)) != MODE_CC)
1194 {
1195 if (GET_CODE (dest) == STRICT_LOW_PART
1196 ||GET_CODE (dest) == ZERO_EXTRACT)
1197 dest = XEXP (dest, 0);
1198 dest_reg = REGNO (dest);
1199
1200 if (GET_CODE (SET_SRC (set)) == REG)
1201 {
1202 unsigned int srcreg = REGNO (SET_SRC (set));
1203
1204 /* Check for rs := rs, which will be deleted. */
1205 if (srcreg == dest_reg)
1206 return;
1207
1208 /* In the case of a DI or DF mode move from register to
1209 register there is overlap if rd = rs + 1 in which case
1210 the order of the copies is reversed :
1211
1212 rd + 1 := rs + 1;
1213 rd := rs */
1214
1215 if (double_p && dest_reg != srcreg + 1)
1216 dest_reg++;
1217 }
1218 }
1219
1220 /* If this is the delay slot of a call insn, any register it sets
1221 is not relevant. */
1222 if (*last_insn_call)
1223 dest_reg = 0;
1224
1225 /* If the previous insn sets the value of a register, and this insn
1226 uses a base register, check for the pipeline hazard where it is
1227 the same register in each case. */
1228 if (*last_reg != 0 && memrtx != NULL_RTX)
1229 {
1230 unsigned int reg = 0;
1231
1232 /* Check for an index (original architecture). */
1233 if (GET_CODE (memrtx) == PLUS)
1234 reg = REGNO (XEXP (memrtx, 0));
1235
1236 /* Check for an MCM target or rs := [rs], in DI or DF mode. */
1237 else if (TARGET_MCM || (double_p && REGNO (memrtx) == dest_reg))
1238 reg = REGNO (memrtx);
1239
1240 /* Remove any pipeline hazard by inserting a NOP. */
1241 if (reg == *last_reg)
1242 {
1243 if (dump_file)
1244 fprintf (dump_file,
1245 "inserting nop before insn %d\n", INSN_UID (insn));
1246 emit_insn_after (gen_hazard_nop (), prev_active_insn (insn));
1247 emit_insn_after (gen_blockage (), insn);
1248 }
1249 }
1250
1251 *last_reg = dest_reg;
1252 }
1253
1254 *last_insn_call = false;
1255 }
1256
1257 /* Go through the instruction stream and insert nops where necessary to avoid
1258 pipeline hazards. There are two cases:
1259
1260 1. On the original architecture, it is invalid to set the value of a
1261 (base) register and then use it in an address with a non-zero index
1262 in the next instruction.
1263
1264 2. On the MCM, setting the value of a (base) register and then using
1265 it in address (including with zero index) in the next instruction
1266 will result in a pipeline stall of 3 cycles. */
1267
1268 static void
1269 gr5_hazard_avoidance (void)
1270 {
1271 unsigned int last_reg = 0;
1272 bool last_insn_call = false;
1273 rtx_insn *insn;
1274
1275 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1276 if (INSN_P (insn))
1277 {
1278 rtx pat = PATTERN (insn);
1279
1280 if (GET_CODE (pat) == SEQUENCE)
1281 {
1282 for (int i = 0; i < XVECLEN (pat, 0); i++)
1283 gr5_avoid_hazard (as_a <rtx_insn *> (XVECEXP (pat, 0, i)),
1284 &last_reg, &last_insn_call);
1285 }
1286
1287 else if (GET_CODE (insn) == CALL_INSN)
1288 {
1289 /* This call is going to get a nop in its delay slot. */
1290 last_reg = 0;
1291 last_insn_call = false;
1292 }
1293
1294 else
1295 gr5_avoid_hazard (insn, &last_reg, &last_insn_call);
1296 }
1297
1298 else if (GET_CODE (insn) == BARRIER)
1299 last_reg = 0;
1300 }
1301
1302 /* Perform a target-specific pass over the instruction stream. The compiler
1303 will run it at all optimization levels, just after the point at which it
1304 normally does delayed-branch scheduling. */
1305
1306 static unsigned int
1307 visium_reorg (void)
1308 {
1309 if (visium_cpu == PROCESSOR_GR5)
1310 gr5_hazard_avoidance ();
1311
1312 return 0;
1313 }
1314 /* Return true if an argument must be passed by indirect reference. */
1315
1316 static bool
1317 visium_pass_by_reference (cumulative_args_t, const function_arg_info &arg)
1318 {
1319 tree type = arg.type;
1320 return type && (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type));
1321 }
1322
1323 /* Define how arguments are passed.
1324
1325 A range of general registers and floating registers is available
1326 for passing arguments. When the class of registers which an
1327 argument would normally use is exhausted, that argument, is passed
1328 in the overflow region of the stack. No argument is split between
1329 registers and stack.
1330
1331 Arguments of type float or _Complex float go in FP registers if FP
1332 hardware is available. If there is no FP hardware, arguments of
1333 type float go in general registers. All other arguments are passed
1334 in general registers. */
1335
1336 static rtx
1337 visium_function_arg (cumulative_args_t pcum_v, const function_arg_info &arg)
1338 {
1339 int size;
1340 CUMULATIVE_ARGS *ca = get_cumulative_args (pcum_v);
1341
1342 size = (GET_MODE_SIZE (arg.mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1343 if (arg.end_marker_p ())
1344 return GEN_INT (0);
1345
1346 /* Scalar or complex single precision floating point arguments are returned
1347 in floating registers. */
1348 if (TARGET_FPU
1349 && ((GET_MODE_CLASS (arg.mode) == MODE_FLOAT
1350 && GET_MODE_SIZE (arg.mode) <= UNITS_PER_HWFPVALUE)
1351 || (GET_MODE_CLASS (arg.mode) == MODE_COMPLEX_FLOAT
1352 && GET_MODE_SIZE (arg.mode) <= UNITS_PER_HWFPVALUE * 2)))
1353 {
1354 if (ca->frcount + size <= MAX_ARGS_IN_FP_REGISTERS)
1355 return gen_rtx_REG (arg.mode, FP_ARG_FIRST + ca->frcount);
1356 else
1357 return NULL_RTX;
1358 }
1359
1360 if (ca->grcount + size <= MAX_ARGS_IN_GP_REGISTERS)
1361 return gen_rtx_REG (arg.mode, ca->grcount + GP_ARG_FIRST);
1362
1363 return NULL_RTX;
1364 }
1365
1366 /* Update the summarizer variable pointed to by PCUM_V to advance past
1367 argument ARG. Once this is done, the variable CUM is suitable for
1368 analyzing the _following_ argument with visium_function_arg. */
1369
1370 static void
1371 visium_function_arg_advance (cumulative_args_t pcum_v,
1372 const function_arg_info &arg)
1373 {
1374 int size = (GET_MODE_SIZE (arg.mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1375 int stack_size = 0;
1376 CUMULATIVE_ARGS *ca = get_cumulative_args (pcum_v);
1377
1378 /* Scalar or complex single precision floating point arguments are returned
1379 in floating registers. */
1380 if (TARGET_FPU
1381 && ((GET_MODE_CLASS (arg.mode) == MODE_FLOAT
1382 && GET_MODE_SIZE (arg.mode) <= UNITS_PER_HWFPVALUE)
1383 || (GET_MODE_CLASS (arg.mode) == MODE_COMPLEX_FLOAT
1384 && GET_MODE_SIZE (arg.mode) <= UNITS_PER_HWFPVALUE * 2)))
1385 {
1386 if (ca->frcount + size <= MAX_ARGS_IN_FP_REGISTERS)
1387 ca->frcount += size;
1388 else
1389 {
1390 stack_size = size;
1391 ca->frcount = MAX_ARGS_IN_FP_REGISTERS;
1392 }
1393 }
1394 else
1395 {
1396 /* Everything else goes in a general register, if enough are
1397 available. */
1398 if (ca->grcount + size <= MAX_ARGS_IN_GP_REGISTERS)
1399 ca->grcount += size;
1400 else
1401 {
1402 stack_size = size;
1403 ca->grcount = MAX_ARGS_IN_GP_REGISTERS;
1404 }
1405 }
1406
1407 if (arg.named)
1408 ca->stack_words += stack_size;
1409 }
1410
1411 /* Specify whether to return the return value in memory. */
1412
1413 static bool
1414 visium_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
1415 {
1416 return (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type));
1417 }
1418
1419 /* Define how scalar values are returned. */
1420
1421 static rtx
1422 visium_function_value_1 (machine_mode mode)
1423 {
1424 /* Scalar or complex single precision floating point values
1425 are returned in floating register f1. */
1426 if (TARGET_FPU
1427 && ((GET_MODE_CLASS (mode) == MODE_FLOAT
1428 && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE)
1429 || (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
1430 && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE * 2)))
1431 return gen_rtx_REG (mode, FP_RETURN_REGNUM);
1432
1433 /* All others are returned in r1. */
1434 return gen_rtx_REG (mode, RETURN_REGNUM);
1435 }
1436
1437 /* Return an RTX representing the place where a function returns or receives
1438 a value of data type RET_TYPE. */
1439
1440 static rtx
1441 visium_function_value (const_tree ret_type,
1442 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
1443 bool outgoing ATTRIBUTE_UNUSED)
1444 {
1445 return visium_function_value_1 (TYPE_MODE (ret_type));
1446 }
1447
1448 /* Return an RTX representing the place where the library function result will
1449 be returned. */
1450
1451 static rtx
1452 visium_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
1453 {
1454 return visium_function_value_1 (mode);
1455 }
1456
1457 /* Store the anonymous register arguments into the stack so that all the
1458 arguments appear to have been passed consecutively on the stack. */
1459
1460 static void
1461 visium_setup_incoming_varargs (cumulative_args_t pcum_v,
1462 const function_arg_info &arg,
1463 int *pretend_size ATTRIBUTE_UNUSED,
1464 int no_rtl)
1465 {
1466 cumulative_args_t local_args_so_far;
1467 CUMULATIVE_ARGS local_copy;
1468 CUMULATIVE_ARGS *locargs;
1469 int gp_saved, fp_saved, size;
1470
1471 /* Create an internal cumulative_args_t pointer to internally define
1472 storage to ensure calling TARGET_FUNCTION_ARG_ADVANCE does not
1473 make global changes. */
1474 local_args_so_far.p = &local_copy;
1475 locargs = get_cumulative_args (pcum_v);
1476
1477 #if CHECKING_P
1478 local_args_so_far.magic = CUMULATIVE_ARGS_MAGIC;
1479 #endif
1480
1481 local_copy.grcount = locargs->grcount;
1482 local_copy.frcount = locargs->frcount;
1483 local_copy.stack_words = locargs->stack_words;
1484
1485 /* The caller has advanced ARGS_SO_FAR up to, but not beyond, the last named
1486 argument. Advance a local copy of ARGS_SO_FAR past the last "real" named
1487 argument, to find out how many registers are left over. */
1488 if (!TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (current_function_decl))
1489 || arg.type != NULL_TREE)
1490 TARGET_FUNCTION_ARG_ADVANCE (local_args_so_far, arg);
1491
1492 /* Find how many registers we need to save. */
1493 locargs = get_cumulative_args (local_args_so_far);
1494 gp_saved = MAX_ARGS_IN_GP_REGISTERS - locargs->grcount;
1495 fp_saved = (TARGET_FPU ? MAX_ARGS_IN_FP_REGISTERS - locargs->frcount : 0);
1496 size = (gp_saved * UNITS_PER_WORD) + (fp_saved * UNITS_PER_HWFPVALUE);
1497
1498 if (!no_rtl && size > 0)
1499 {
1500 /* To avoid negative offsets, which are not valid addressing modes on
1501 the Visium, we create a base register for the pretend args. */
1502 rtx ptr
1503 = force_reg (Pmode,
1504 plus_constant (Pmode, virtual_incoming_args_rtx, -size));
1505
1506 if (gp_saved > 0)
1507 {
1508 rtx mem
1509 = gen_rtx_MEM (BLKmode,
1510 plus_constant (Pmode,
1511 ptr,
1512 fp_saved * UNITS_PER_HWFPVALUE));
1513 MEM_NOTRAP_P (mem) = 1;
1514 set_mem_alias_set (mem, get_varargs_alias_set ());
1515 move_block_from_reg (locargs->grcount + GP_ARG_FIRST, mem, gp_saved);
1516 }
1517
1518 if (fp_saved > 0)
1519 {
1520 rtx mem = gen_rtx_MEM (BLKmode, ptr);
1521 MEM_NOTRAP_P (mem) = 1;
1522 set_mem_alias_set (mem, get_varargs_alias_set ());
1523 gcc_assert (UNITS_PER_WORD == UNITS_PER_HWFPVALUE);
1524 move_block_from_reg (locargs->frcount + FP_ARG_FIRST, mem, fp_saved);
1525 }
1526 }
1527
1528 visium_reg_parm_save_area_size = size;
1529 }
1530
1531 /* Define the `__builtin_va_list' type for the ABI. */
1532
1533 static tree
1534 visium_build_builtin_va_list (void)
1535 {
1536 tree f_ovfl, f_gbase, f_fbase, f_gbytes, f_fbytes, record;
1537
1538 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
1539 f_ovfl = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1540 get_identifier ("__overflow_argptr"), ptr_type_node);
1541 f_gbase = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1542 get_identifier ("__gpr_base"), ptr_type_node);
1543 f_fbase = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1544 get_identifier ("__fpr_base"), ptr_type_node);
1545 f_gbytes = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1546 get_identifier ("__gpr_bytes"),
1547 short_unsigned_type_node);
1548 f_fbytes = build_decl (BUILTINS_LOCATION, FIELD_DECL,
1549 get_identifier ("__fpr_bytes"),
1550 short_unsigned_type_node);
1551
1552 DECL_FIELD_CONTEXT (f_ovfl) = record;
1553 DECL_FIELD_CONTEXT (f_gbase) = record;
1554 DECL_FIELD_CONTEXT (f_fbase) = record;
1555 DECL_FIELD_CONTEXT (f_gbytes) = record;
1556 DECL_FIELD_CONTEXT (f_fbytes) = record;
1557 TYPE_FIELDS (record) = f_ovfl;
1558 TREE_CHAIN (f_ovfl) = f_gbase;
1559 TREE_CHAIN (f_gbase) = f_fbase;
1560 TREE_CHAIN (f_fbase) = f_gbytes;
1561 TREE_CHAIN (f_gbytes) = f_fbytes;
1562 layout_type (record);
1563
1564 return record;
1565 }
1566
1567 /* Implement `va_start' for varargs and stdarg. */
1568
1569 static void
1570 visium_va_start (tree valist, rtx nextarg ATTRIBUTE_UNUSED)
1571 {
1572 const CUMULATIVE_ARGS *ca = &crtl->args.info;
1573 int gp_saved = MAX_ARGS_IN_GP_REGISTERS - ca->grcount;
1574 int fp_saved = (TARGET_FPU ? MAX_ARGS_IN_FP_REGISTERS - ca->frcount : 0);
1575 int named_stack_size = ca->stack_words * UNITS_PER_WORD, offset;
1576 tree f_ovfl, f_gbase, f_fbase, f_gbytes, f_fbytes;
1577 tree ovfl, gbase, gbytes, fbase, fbytes, t;
1578
1579 f_ovfl = TYPE_FIELDS (va_list_type_node);
1580 f_gbase = TREE_CHAIN (f_ovfl);
1581 f_fbase = TREE_CHAIN (f_gbase);
1582 f_gbytes = TREE_CHAIN (f_fbase);
1583 f_fbytes = TREE_CHAIN (f_gbytes);
1584 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl, NULL_TREE);
1585 gbase = build3 (COMPONENT_REF, TREE_TYPE (f_gbase), valist, f_gbase,
1586 NULL_TREE);
1587 fbase = build3 (COMPONENT_REF, TREE_TYPE (f_fbase), valist, f_fbase,
1588 NULL_TREE);
1589 gbytes = build3 (COMPONENT_REF, TREE_TYPE (f_gbytes), valist, f_gbytes,
1590 NULL_TREE);
1591 fbytes = build3 (COMPONENT_REF, TREE_TYPE (f_fbytes), valist, f_fbytes,
1592 NULL_TREE);
1593
1594 /* Store the stacked vararg pointer in the OVFL member. */
1595 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
1596 t = fold_build_pointer_plus_hwi (t, named_stack_size);
1597 t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
1598 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1599
1600 /* Store the base address of the GPR save area into GBASE. */
1601 t = make_tree (TREE_TYPE (gbase), virtual_incoming_args_rtx);
1602 offset = MAX_ARGS_IN_GP_REGISTERS * UNITS_PER_WORD;
1603 t = fold_build_pointer_plus_hwi (t, -offset);
1604 t = build2 (MODIFY_EXPR, TREE_TYPE (gbase), gbase, t);
1605 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1606
1607 /* Store the base address of the FPR save area into FBASE. */
1608 if (fp_saved)
1609 {
1610 t = make_tree (TREE_TYPE (fbase), virtual_incoming_args_rtx);
1611 offset = gp_saved * UNITS_PER_WORD
1612 + MAX_ARGS_IN_FP_REGISTERS * UNITS_PER_HWFPVALUE;
1613 t = fold_build_pointer_plus_hwi (t, -offset);
1614 t = build2 (MODIFY_EXPR, TREE_TYPE (fbase), fbase, t);
1615 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1616 }
1617
1618 /* Fill in the GBYTES member. */
1619 t = build2 (MODIFY_EXPR, TREE_TYPE (gbytes), gbytes,
1620 size_int (gp_saved * UNITS_PER_WORD));
1621 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1622
1623 /* Fill in the FBYTES member. */
1624 t = build2 (MODIFY_EXPR, TREE_TYPE (fbytes),
1625 fbytes, size_int (fp_saved * UNITS_PER_HWFPVALUE));
1626 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
1627 }
1628
1629 /* Implement `va_arg'. */
1630
1631 static tree
1632 visium_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
1633 gimple_seq *post_p)
1634 {
1635 tree f_ovfl, f_gbase, f_fbase, f_gbytes, f_fbytes;
1636 tree ovfl, base, bytes;
1637 HOST_WIDE_INT size, rsize;
1638 const bool by_reference_p = pass_va_arg_by_reference (type);
1639 const bool float_reg_arg_p
1640 = (TARGET_FPU && !by_reference_p
1641 && ((GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
1642 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_HWFPVALUE)
1643 || (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_COMPLEX_FLOAT
1644 && (GET_MODE_SIZE (TYPE_MODE (type))
1645 <= UNITS_PER_HWFPVALUE * 2))));
1646 const int max_save_area_size
1647 = (float_reg_arg_p ? MAX_ARGS_IN_FP_REGISTERS * UNITS_PER_HWFPVALUE
1648 : MAX_ARGS_IN_GP_REGISTERS * UNITS_PER_WORD);
1649 tree t, u, offs;
1650 tree lab_false, lab_over, addr;
1651 tree ptrtype = build_pointer_type (type);
1652
1653 if (by_reference_p)
1654 {
1655 t = visium_gimplify_va_arg (valist, ptrtype, pre_p, post_p);
1656 return build_va_arg_indirect_ref (t);
1657 }
1658
1659 size = int_size_in_bytes (type);
1660 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
1661 f_ovfl = TYPE_FIELDS (va_list_type_node);
1662 f_gbase = TREE_CHAIN (f_ovfl);
1663 f_fbase = TREE_CHAIN (f_gbase);
1664 f_gbytes = TREE_CHAIN (f_fbase);
1665 f_fbytes = TREE_CHAIN (f_gbytes);
1666
1667 /* We maintain separate pointers and offsets for floating-point and
1668 general registers, but we need similar code in both cases.
1669
1670 Let:
1671
1672 BYTES be the number of unused bytes in the register save area.
1673 BASE be the base address of the register save area.
1674 OFFS be the current offset into the register save area. Either
1675 MAX_ARGS_IN_GP_REGISTERS * UNITS_PER_WORD - bytes or
1676 MAX_ARGS_IN_FP_REGISTERS * UNITS_PER_HWFPVALUE - bytes
1677 depending upon whether the argument is in general or floating
1678 registers.
1679 ADDR_RTX be the address of the argument.
1680 RSIZE be the size in bytes of the argument.
1681 OVFL be the pointer to the stack overflow area.
1682
1683 The code we want is:
1684
1685 1: if (bytes >= rsize)
1686 2: {
1687 3: addr_rtx = base + offs;
1688 4: bytes -= rsize;
1689 5: }
1690 6: else
1691 7: {
1692 8: bytes = 0;
1693 9: addr_rtx = ovfl;
1694 10: ovfl += rsize;
1695 11: }
1696
1697 */
1698
1699 addr = create_tmp_var (ptr_type_node, "addr");
1700 lab_false = create_artificial_label (UNKNOWN_LOCATION);
1701 lab_over = create_artificial_label (UNKNOWN_LOCATION);
1702 if (float_reg_arg_p)
1703 bytes = build3 (COMPONENT_REF, TREE_TYPE (f_fbytes), unshare_expr (valist),
1704 f_fbytes, NULL_TREE);
1705 else
1706 bytes = build3 (COMPONENT_REF, TREE_TYPE (f_gbytes), unshare_expr (valist),
1707 f_gbytes, NULL_TREE);
1708
1709 /* [1] Emit code to branch if bytes < rsize. */
1710 t = fold_convert (TREE_TYPE (bytes), size_int (rsize));
1711 t = build2 (LT_EXPR, boolean_type_node, bytes, t);
1712 u = build1 (GOTO_EXPR, void_type_node, lab_false);
1713 t = build3 (COND_EXPR, void_type_node, t, u, NULL_TREE);
1714 gimplify_and_add (t, pre_p);
1715
1716 /* [3] Emit code for: addr_rtx = base + offs, where
1717 offs = max_save_area_size - bytes. */
1718 t = fold_convert (sizetype, bytes);
1719 offs = build2 (MINUS_EXPR, sizetype, size_int (max_save_area_size), t);
1720 if (float_reg_arg_p)
1721 base = build3 (COMPONENT_REF, TREE_TYPE (f_fbase), valist, f_fbase,
1722 NULL_TREE);
1723 else
1724 base = build3 (COMPONENT_REF, TREE_TYPE (f_gbase), valist, f_gbase,
1725 NULL_TREE);
1726
1727 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (base), base, offs);
1728 t = build2 (MODIFY_EXPR, void_type_node, addr, t);
1729 gimplify_and_add (t, pre_p);
1730
1731 /* [4] Emit code for: bytes -= rsize. */
1732 t = fold_convert (TREE_TYPE (bytes), size_int (rsize));
1733 t = build2 (MINUS_EXPR, TREE_TYPE (bytes), bytes, t);
1734 t = build2 (MODIFY_EXPR, TREE_TYPE (bytes), bytes, t);
1735 gimplify_and_add (t, pre_p);
1736
1737 /* [6] Emit code to branch over the else clause, then the label. */
1738 t = build1 (GOTO_EXPR, void_type_node, lab_over);
1739 gimplify_and_add (t, pre_p);
1740 t = build1 (LABEL_EXPR, void_type_node, lab_false);
1741 gimplify_and_add (t, pre_p);
1742
1743 /* [8] Emit code for: bytes = 0. */
1744 t = fold_convert (TREE_TYPE (bytes), size_int (0));
1745 t = build2 (MODIFY_EXPR, TREE_TYPE (bytes), unshare_expr (bytes), t);
1746 gimplify_and_add (t, pre_p);
1747
1748 /* [9] Emit code for: addr_rtx = ovfl. */
1749 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl, NULL_TREE);
1750 t = build2 (MODIFY_EXPR, void_type_node, addr, ovfl);
1751 gimplify_and_add (t, pre_p);
1752
1753 /* [10] Emit code for: ovfl += rsize. */
1754 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ovfl), ovfl, size_int (rsize));
1755 t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), unshare_expr (ovfl), t);
1756 gimplify_and_add (t, pre_p);
1757 t = build1 (LABEL_EXPR, void_type_node, lab_over);
1758 gimplify_and_add (t, pre_p);
1759
1760 /* Emit a big-endian correction if size < UNITS_PER_WORD. */
1761 if (size < UNITS_PER_WORD)
1762 {
1763 t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (addr), addr,
1764 size_int (UNITS_PER_WORD - size));
1765 t = build2 (MODIFY_EXPR, void_type_node, addr, t);
1766 gimplify_and_add (t, pre_p);
1767 }
1768
1769 addr = fold_convert (ptrtype, addr);
1770
1771 return build_va_arg_indirect_ref (addr);
1772 }
1773
1774 /* Return true if OP is an offset suitable for use as a displacement in the
1775 address of a memory access in mode MODE. */
1776
1777 static bool
1778 rtx_ok_for_offset_p (machine_mode mode, rtx op)
1779 {
1780 if (!CONST_INT_P (op) || INTVAL (op) < 0)
1781 return false;
1782
1783 switch (mode)
1784 {
1785 case E_QImode:
1786 return INTVAL (op) <= 31;
1787
1788 case E_HImode:
1789 return (INTVAL (op) % 2) == 0 && INTVAL (op) < 63;
1790
1791 case E_SImode:
1792 case E_SFmode:
1793 return (INTVAL (op) % 4) == 0 && INTVAL (op) < 127;
1794
1795 case E_DImode:
1796 case E_DFmode:
1797 return (INTVAL (op) % 4) == 0 && INTVAL (op) < 123;
1798
1799 default:
1800 return false;
1801 }
1802 }
1803
1804 /* Return whether X is a legitimate memory address for a memory operand
1805 of mode MODE.
1806
1807 Legitimate addresses are defined in two variants: a strict variant
1808 and a non-strict one. The STRICT parameter chooses which variant
1809 is desired by the caller.
1810
1811 The strict variant is used in the reload pass. It must be defined
1812 so that any pseudo-register that has not been allocated a hard
1813 register is considered a memory reference. This is because in
1814 contexts where some kind of register is required, a
1815 pseudo-register with no hard register must be rejected. For
1816 non-hard registers, the strict variant should look up the
1817 `reg_renumber' array; it should then proceed using the hard
1818 register number in the array, or treat the pseudo as a memory
1819 reference if the array holds `-1'.
1820
1821 The non-strict variant is used in other passes. It must be
1822 defined to accept all pseudo-registers in every context where some
1823 kind of register is required. */
1824
1825 static bool
1826 visium_legitimate_address_p (machine_mode mode, rtx x, bool strict, code_helper)
1827 {
1828 rtx base;
1829 unsigned int regno;
1830
1831 /* If X is base+disp, check that we have an appropriate offset. */
1832 if (GET_CODE (x) == PLUS)
1833 {
1834 if (!rtx_ok_for_offset_p (mode, XEXP (x, 1)))
1835 return false;
1836 base = XEXP (x, 0);
1837 }
1838 else
1839 base = x;
1840
1841 /* Now check the base: it must be either a register or a subreg thereof. */
1842 if (GET_CODE (base) == SUBREG)
1843 base = SUBREG_REG (base);
1844 if (!REG_P (base))
1845 return false;
1846
1847 regno = REGNO (base);
1848
1849 /* For the strict variant, the register must be REGNO_OK_FOR_BASE_P. */
1850 if (strict)
1851 return REGNO_OK_FOR_BASE_P (regno);
1852
1853 /* For the non-strict variant, the register may also be a pseudo. */
1854 return BASE_REGISTER_P (regno) || regno >= FIRST_PSEUDO_REGISTER;
1855 }
1856
1857 /* Try machine-dependent ways of modifying an illegitimate address
1858 to be legitimate. If we find one, return the new, valid address.
1859 This macro is used in only one place: `memory_address' in explow.cc.
1860
1861 OLDX is the address as it was before break_out_memory_refs was called.
1862 In some cases it is useful to look at this to decide what needs to be done.
1863
1864 MODE and WIN are passed so that this macro can use
1865 GO_IF_LEGITIMATE_ADDRESS.
1866
1867 It is always safe for this macro to do nothing. It exists to recognize
1868 opportunities to optimize the output.
1869
1870 For Visium
1871
1872 memory (reg + <out of range int>)
1873
1874 is transformed to
1875
1876 base_int = <out of range int> & ~mask
1877 ptr_reg = reg + base_int
1878 memory (ptr_reg + <out of range int> - base_int)
1879
1880 Thus ptr_reg is a base register for a range of addresses,
1881 which should help CSE.
1882
1883 For a 1 byte reference mask is 0x1f
1884 for a 2 byte reference mask is 0x3f
1885 For a 4 byte reference mask is 0x7f
1886
1887 This reflects the indexing range of the processor.
1888
1889 For a > 4 byte reference the mask is 0x7f provided all of the words
1890 can be accessed with the base address obtained. Otherwise a mask
1891 of 0x3f is used.
1892
1893 On rare occasions an unaligned base register value with an
1894 unaligned offset is generated. Unaligned offsets are left alone for
1895 this reason. */
1896
1897 static rtx
1898 visium_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1899 machine_mode mode)
1900 {
1901 if (GET_CODE (x) == PLUS
1902 && GET_CODE (XEXP (x, 1)) == CONST_INT
1903 && GET_CODE (XEXP (x, 0)) == REG && mode != BLKmode)
1904 {
1905 int offset = INTVAL (XEXP (x, 1));
1906 int size = GET_MODE_SIZE (mode);
1907 int mask = (size == 1 ? 0x1f : (size == 2 ? 0x3f : 0x7f));
1908 int mask1 = (size == 1 ? 0 : (size == 2 ? 1 : 3));
1909 int offset_base = offset & ~mask;
1910
1911 /* Check that all of the words can be accessed. */
1912 if (size > 4 && size + offset - offset_base > 0x80)
1913 offset_base = offset & ~0x3f;
1914 if (offset_base != 0 && offset_base != offset && (offset & mask1) == 0)
1915 {
1916 rtx ptr_reg = force_reg (Pmode,
1917 gen_rtx_PLUS (Pmode,
1918 XEXP (x, 0),
1919 GEN_INT (offset_base)));
1920
1921 return plus_constant (Pmode, ptr_reg, offset - offset_base);
1922 }
1923 }
1924
1925 return x;
1926 }
1927
1928 /* Perform a similar function to visium_legitimize_address, but this time
1929 for reload. Generating new registers is not an option here. Parts
1930 that need reloading are indicated by calling push_reload. */
1931
1932 rtx
1933 visium_legitimize_reload_address (rtx x, machine_mode mode, int opnum,
1934 int type, int ind ATTRIBUTE_UNUSED)
1935 {
1936 rtx newrtx, tem = NULL_RTX;
1937
1938 if (mode == BLKmode)
1939 return NULL_RTX;
1940
1941 if (optimize && GET_CODE (x) == PLUS)
1942 tem = simplify_binary_operation (PLUS, GET_MODE (x), XEXP (x, 0),
1943 XEXP (x, 1));
1944
1945 newrtx = tem ? tem : x;
1946 if (GET_CODE (newrtx) == PLUS
1947 && GET_CODE (XEXP (newrtx, 1)) == CONST_INT
1948 && GET_CODE (XEXP (newrtx, 0)) == REG
1949 && BASE_REGISTER_P (REGNO (XEXP (newrtx, 0))))
1950 {
1951 int offset = INTVAL (XEXP (newrtx, 1));
1952 int size = GET_MODE_SIZE (mode);
1953 int mask = (size == 1 ? 0x1f : (size == 2 ? 0x3f : 0x7f));
1954 int mask1 = (size == 1 ? 0 : (size == 2 ? 1 : 3));
1955 int offset_base = offset & ~mask;
1956
1957 /* Check that all of the words can be accessed. */
1958 if (size > 4 && size + offset - offset_base > 0x80)
1959 offset_base = offset & ~0x3f;
1960
1961 if (offset_base && (offset & mask1) == 0)
1962 {
1963 rtx temp = gen_rtx_PLUS (Pmode,
1964 XEXP (newrtx, 0), GEN_INT (offset_base));
1965
1966 x = gen_rtx_PLUS (Pmode, temp, GEN_INT (offset - offset_base));
1967 push_reload (XEXP (x, 0), 0, &XEXP (x, 0), 0,
1968 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum,
1969 (enum reload_type) type);
1970 return x;
1971 }
1972 }
1973
1974 return NULL_RTX;
1975 }
1976
1977 /* Return the cost of moving data of mode MODE from a register in class FROM to
1978 one in class TO. A value of 2 is the default; other values are interpreted
1979 relative to that. */
1980
1981 static int
1982 visium_register_move_cost (machine_mode mode, reg_class_t from,
1983 reg_class_t to)
1984 {
1985 const int numwords = (GET_MODE_SIZE (mode) <= UNITS_PER_WORD) ? 1 : 2;
1986
1987 if (from == MDB || to == MDB)
1988 return 4;
1989 else if (from == MDC || to == MDC || (from == FP_REGS) != (to == FP_REGS))
1990 return 4 * numwords;
1991 else
1992 return 2 * numwords;
1993 }
1994
1995 /* Return the cost of moving data of mode MODE between a register of class
1996 CLASS and memory. IN is zero if the value is to be written to memory,
1997 non-zero if it is to be read in. This cost is relative to those in
1998 visium_register_move_cost. */
1999
2000 static int
2001 visium_memory_move_cost (machine_mode mode,
2002 reg_class_t to ATTRIBUTE_UNUSED,
2003 bool in)
2004 {
2005 /* Moving data in can be from PROM and this is expensive. */
2006 if (in)
2007 {
2008 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2009 return 7;
2010 else
2011 return 13;
2012 }
2013
2014 /* Moving data out is mostly to RAM and should be cheaper. */
2015 else
2016 {
2017 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2018 return 6;
2019 else
2020 return 12;
2021 }
2022 }
2023
2024 /* Return the relative costs of expression X. */
2025
2026 static bool
2027 visium_rtx_costs (rtx x, machine_mode mode, int outer_code ATTRIBUTE_UNUSED,
2028 int opno ATTRIBUTE_UNUSED, int *total,
2029 bool speed ATTRIBUTE_UNUSED)
2030 {
2031 int code = GET_CODE (x);
2032
2033 switch (code)
2034 {
2035 case CONST_INT:
2036 /* Small integers are as cheap as registers. 4-byte values can
2037 be fetched as immediate constants - let's give that the cost
2038 of an extra insn. */
2039 *total = COSTS_N_INSNS (!satisfies_constraint_J (x));
2040 return true;
2041
2042 case CONST:
2043 case LABEL_REF:
2044 case SYMBOL_REF:
2045 *total = COSTS_N_INSNS (2);
2046 return true;
2047
2048 case CONST_DOUBLE:
2049 {
2050 rtx high, low;
2051 split_double (x, &high, &low);
2052 *total =
2053 COSTS_N_INSNS
2054 (!satisfies_constraint_J (high) + !satisfies_constraint_J (low));
2055 return true;
2056 }
2057
2058 case MULT:
2059 *total = COSTS_N_INSNS (3);
2060 return false;
2061
2062 case DIV:
2063 case UDIV:
2064 case MOD:
2065 case UMOD:
2066 if (mode == DImode)
2067 *total = COSTS_N_INSNS (64);
2068 else
2069 *total = COSTS_N_INSNS (32);
2070 return false;
2071
2072 case PLUS:
2073 case MINUS:
2074 case NEG:
2075 /* DImode operations are performed directly on the ALU. */
2076 if (mode == DImode)
2077 *total = COSTS_N_INSNS (2);
2078 else
2079 *total = COSTS_N_INSNS (1);
2080 return false;
2081
2082 case ASHIFT:
2083 case ASHIFTRT:
2084 case LSHIFTRT:
2085 /* DImode operations are performed on the EAM instead. */
2086 if (mode == DImode)
2087 *total = COSTS_N_INSNS (3);
2088 else
2089 *total = COSTS_N_INSNS (1);
2090 return false;
2091
2092 case COMPARE:
2093 /* This matches the btst pattern. */
2094 if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
2095 && XEXP (x, 1) == const0_rtx
2096 && XEXP (XEXP (x, 0), 1) == const1_rtx
2097 && satisfies_constraint_K (XEXP (XEXP (x, 0), 2)))
2098 *total = COSTS_N_INSNS (1);
2099 return false;
2100
2101 default:
2102 return false;
2103 }
2104 }
2105
2106 /* Split a double move of OPERANDS in MODE. */
2107
2108 void
2109 visium_split_double_move (rtx *operands, machine_mode mode)
2110 {
2111 bool swap = false;
2112
2113 /* Check register to register with overlap. */
2114 if (GET_CODE (operands[0]) == REG
2115 && GET_CODE (operands[1]) == REG
2116 && REGNO (operands[0]) == REGNO (operands[1]) + 1)
2117 swap = true;
2118
2119 /* Check memory to register where the base reg overlaps the destination. */
2120 if (GET_CODE (operands[0]) == REG && GET_CODE (operands[1]) == MEM)
2121 {
2122 rtx op = XEXP (operands[1], 0);
2123
2124 if (GET_CODE (op) == SUBREG)
2125 op = SUBREG_REG (op);
2126
2127 if (GET_CODE (op) == REG && REGNO (op) == REGNO (operands[0]))
2128 swap = true;
2129
2130 if (GET_CODE (op) == PLUS)
2131 {
2132 rtx x = XEXP (op, 0);
2133 rtx y = XEXP (op, 1);
2134
2135 if (GET_CODE (x) == REG && REGNO (x) == REGNO (operands[0]))
2136 swap = true;
2137
2138 if (GET_CODE (y) == REG && REGNO (y) == REGNO (operands[0]))
2139 swap = true;
2140 }
2141 }
2142
2143 if (swap)
2144 {
2145 operands[2] = operand_subword (operands[0], 1, 1, mode);
2146 operands[3] = operand_subword (operands[1], 1, 1, mode);
2147 operands[4] = operand_subword (operands[0], 0, 1, mode);
2148 operands[5] = operand_subword (operands[1], 0, 1, mode);
2149 }
2150 else
2151 {
2152 operands[2] = operand_subword (operands[0], 0, 1, mode);
2153 operands[3] = operand_subword (operands[1], 0, 1, mode);
2154 operands[4] = operand_subword (operands[0], 1, 1, mode);
2155 operands[5] = operand_subword (operands[1], 1, 1, mode);
2156 }
2157 }
2158
2159 /* Split a double addition or subtraction of operands. */
2160
2161 void
2162 visium_split_double_add (enum rtx_code code, rtx op0, rtx op1, rtx op2)
2163 {
2164 rtx op3 = gen_lowpart (SImode, op0);
2165 rtx op4 = gen_lowpart (SImode, op1);
2166 rtx op5;
2167 rtx op6 = gen_highpart (SImode, op0);
2168 rtx op7 = (op1 == const0_rtx ? op1 : gen_highpart (SImode, op1));
2169 rtx op8;
2170 rtx x, pat, flags;
2171
2172 /* If operand #2 is a small constant, then its high part is null. */
2173 if (CONST_INT_P (op2))
2174 {
2175 HOST_WIDE_INT val = INTVAL (op2);
2176
2177 if (val < 0)
2178 {
2179 code = (code == MINUS ? PLUS : MINUS);
2180 val = -val;
2181 }
2182
2183 op5 = gen_int_mode (val, SImode);
2184 op8 = const0_rtx;
2185 }
2186 else
2187 {
2188 op5 = gen_lowpart (SImode, op2);
2189 op8 = gen_highpart (SImode, op2);
2190 }
2191
2192 if (op4 == const0_rtx)
2193 pat = gen_negsi2_insn_set_carry (op3, op5);
2194 else if (code == MINUS)
2195 pat = gen_subsi3_insn_set_carry (op3, op4, op5);
2196 else
2197 pat = gen_addsi3_insn_set_carry (op3, op4, op5);
2198 emit_insn (pat);
2199
2200 /* This is the plus_[plus_]sltu_flags or minus_[minus_]sltu_flags pattern. */
2201 if (op8 == const0_rtx)
2202 x = op7;
2203 else
2204 x = gen_rtx_fmt_ee (code, SImode, op7, op8);
2205 flags = gen_rtx_REG (CCCmode, FLAGS_REGNUM);
2206 x = gen_rtx_fmt_ee (code, SImode, x, gen_rtx_LTU (SImode, flags, const0_rtx));
2207 pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (2));
2208 XVECEXP (pat, 0, 0) = gen_rtx_SET (op6, x);
2209 flags = gen_rtx_REG (CCmode, FLAGS_REGNUM);
2210 XVECEXP (pat, 0, 1) = gen_rtx_CLOBBER (VOIDmode, flags);
2211 emit_insn (pat);
2212
2213 visium_flags_exposed = true;
2214 }
2215
2216 /* Expand a copysign of OPERANDS in MODE. */
2217
2218 void
2219 visium_expand_copysign (rtx *operands, machine_mode mode)
2220 {
2221 rtx op0 = operands[0];
2222 rtx op1 = operands[1];
2223 rtx op2 = operands[2];
2224 rtx mask = force_reg (SImode, GEN_INT (0x7fffffff));
2225 rtx x;
2226
2227 /* We manually handle SFmode because the abs and neg instructions of
2228 the FPU on the MCM have a non-standard behavior wrt NaNs. */
2229 gcc_assert (mode == SFmode);
2230
2231 /* First get all the non-sign bits of op1. */
2232 if (GET_CODE (op1) == CONST_DOUBLE)
2233 {
2234 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op1)))
2235 op1 = simplify_unary_operation (ABS, mode, op1, mode);
2236 if (op1 != CONST0_RTX (mode))
2237 {
2238 long l;
2239 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (op1), l);
2240 op1 = force_reg (SImode, gen_int_mode (l, SImode));
2241 }
2242 }
2243 else
2244 {
2245 op1 = copy_to_mode_reg (SImode, gen_lowpart (SImode, op1));
2246 op1 = force_reg (SImode, gen_rtx_AND (SImode, op1, mask));
2247 }
2248
2249 /* Then get the sign bit of op2. */
2250 mask = force_reg (SImode, gen_rtx_NOT (SImode, mask));
2251 op2 = copy_to_mode_reg (SImode, gen_lowpart (SImode, op2));
2252 op2 = force_reg (SImode, gen_rtx_AND (SImode, op2, mask));
2253
2254 /* Finally OR the two values. */
2255 if (op1 == CONST0_RTX (SFmode))
2256 x = op2;
2257 else
2258 x = force_reg (SImode, gen_rtx_IOR (SImode, op1, op2));
2259
2260 /* And move the result to the destination. */
2261 emit_insn (gen_rtx_SET (op0, gen_lowpart (SFmode, x)));
2262 }
2263
2264 /* Expand a cstore of OPERANDS in MODE for EQ/NE/LTU/GTU/GEU/LEU. We generate
2265 the result in the C flag and use the ADC/SUBC instructions to write it into
2266 the destination register.
2267
2268 It would also be possible to implement support for LT/GT/LE/GE by means of
2269 the RFLAG instruction followed by some shifts, but this can pessimize the
2270 generated code. */
2271
2272 void
2273 visium_expand_int_cstore (rtx *operands, machine_mode mode)
2274 {
2275 enum rtx_code code = GET_CODE (operands[1]);
2276 rtx op0 = operands[0], op1 = operands[2], op2 = operands[3], sltu;
2277 bool reverse = false;
2278
2279 switch (code)
2280 {
2281 case EQ:
2282 case NE:
2283 /* We use a special comparison to get the result in the C flag. */
2284 if (op2 != const0_rtx)
2285 op1 = force_reg (mode, gen_rtx_XOR (mode, op1, op2));
2286 op1 = gen_rtx_NOT (mode, op1);
2287 op2 = constm1_rtx;
2288 if (code == EQ)
2289 reverse = true;
2290 break;
2291
2292 case LEU:
2293 case GEU:
2294 /* The result is naturally in the C flag modulo a couple of tricks. */
2295 code = reverse_condition (code);
2296 reverse = true;
2297
2298 /* ... fall through ... */
2299
2300 case LTU:
2301 case GTU:
2302 if (code == GTU)
2303 {
2304 rtx tmp = op1;
2305 op1 = op2;
2306 op2 = tmp;
2307 }
2308 break;
2309
2310 default:
2311 gcc_unreachable ();
2312 }
2313
2314 /* We need either a single ADC or a SUBC and a PLUS. */
2315 sltu = gen_rtx_LTU (SImode, op1, op2);
2316
2317 if (reverse)
2318 {
2319 rtx tmp = copy_to_mode_reg (SImode, gen_rtx_NEG (SImode, sltu));
2320 emit_insn (gen_add3_insn (op0, tmp, const1_rtx));
2321 }
2322 else
2323 emit_insn (gen_rtx_SET (op0, sltu));
2324 }
2325
2326 /* Expand a cstore of OPERANDS in MODE for LT/GT/UNGE/UNLE. We generate the
2327 result in the C flag and use the ADC/SUBC instructions to write it into
2328 the destination register. */
2329
2330 void
2331 visium_expand_fp_cstore (rtx *operands,
2332 machine_mode mode ATTRIBUTE_UNUSED)
2333 {
2334 enum rtx_code code = GET_CODE (operands[1]);
2335 rtx op0 = operands[0], op1 = operands[2], op2 = operands[3], slt;
2336 bool reverse = false;
2337
2338 switch (code)
2339 {
2340 case UNLE:
2341 case UNGE:
2342 /* The result is naturally in the C flag modulo a couple of tricks. */
2343 code = reverse_condition_maybe_unordered (code);
2344 reverse = true;
2345
2346 /* ... fall through ... */
2347
2348 case LT:
2349 case GT:
2350 if (code == GT)
2351 {
2352 rtx tmp = op1;
2353 op1 = op2;
2354 op2 = tmp;
2355 }
2356 break;
2357
2358 default:
2359 gcc_unreachable ();
2360 }
2361
2362 /* We need either a single ADC or a SUBC and a PLUS. */
2363 slt = gen_rtx_LT (SImode, op1, op2);
2364
2365 if (reverse)
2366 {
2367 rtx tmp = copy_to_mode_reg (SImode, gen_rtx_NEG (SImode, slt));
2368 emit_insn (gen_add3_insn (op0, tmp, const1_rtx));
2369 }
2370 else
2371 emit_insn (gen_rtx_SET (op0, slt));
2372 }
2373
2374 /* Split a compare-and-store with CODE, operands OP2 and OP3, combined with
2375 operation with OP_CODE, operands OP0 and OP1. */
2376
2377 void
2378 visium_split_cstore (enum rtx_code op_code, rtx op0, rtx op1,
2379 enum rtx_code code, rtx op2, rtx op3)
2380 {
2381 machine_mode cc_mode = visium_select_cc_mode (code, op2, op3);
2382
2383 /* If a FP cstore was reversed, then it was originally UNGE/UNLE. */
2384 if (cc_mode == CCFPEmode && (op_code == NEG || op_code == MINUS))
2385 cc_mode = CCFPmode;
2386
2387 rtx flags = gen_rtx_REG (cc_mode, FLAGS_REGNUM);
2388 rtx x = gen_rtx_COMPARE (cc_mode, op2, op3);
2389 x = gen_rtx_SET (flags, x);
2390 emit_insn (x);
2391
2392 x = gen_rtx_fmt_ee (code, SImode, flags, const0_rtx);
2393 switch (op_code)
2394 {
2395 case SET:
2396 break;
2397 case NEG:
2398 x = gen_rtx_NEG (SImode, x);
2399 break;
2400 case PLUS:
2401 case MINUS:
2402 x = gen_rtx_fmt_ee (op_code, SImode, op1, x);
2403 break;
2404 default:
2405 gcc_unreachable ();
2406 }
2407
2408 rtx pat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (2));
2409 XVECEXP (pat, 0, 0) = gen_rtx_SET (op0, x);
2410 flags = gen_rtx_REG (CCmode, FLAGS_REGNUM);
2411 XVECEXP (pat, 0, 1) = gen_rtx_CLOBBER (VOIDmode, flags);
2412 emit_insn (pat);
2413
2414 visium_flags_exposed = true;
2415 }
2416
2417 /* Generate a call to a library function to move BYTES_RTX bytes from SRC with
2418 address SRC_REG to DST with address DST_REG in 4-byte chunks. */
2419
2420 static void
2421 expand_block_move_4 (rtx dst, rtx dst_reg, rtx src, rtx src_reg, rtx bytes_rtx)
2422 {
2423 unsigned HOST_WIDE_INT bytes = UINTVAL (bytes_rtx);
2424 unsigned int rem = bytes % 4;
2425
2426 if (TARGET_BMI)
2427 {
2428 unsigned int i;
2429 rtx insn;
2430
2431 emit_move_insn (regno_reg_rtx[1], dst_reg);
2432 emit_move_insn (regno_reg_rtx[2], src_reg);
2433 emit_move_insn (regno_reg_rtx[3], bytes_rtx);
2434
2435 insn = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (8));
2436 XVECEXP (insn, 0, 0)
2437 = gen_rtx_SET (replace_equiv_address_nv (dst, regno_reg_rtx[1]),
2438 replace_equiv_address_nv (src, regno_reg_rtx[2]));
2439 XVECEXP (insn, 0, 1) = gen_rtx_USE (VOIDmode, regno_reg_rtx[3]);
2440 for (i = 1; i <= 6; i++)
2441 XVECEXP (insn, 0, 1 + i)
2442 = gen_rtx_CLOBBER (VOIDmode, regno_reg_rtx[i]);
2443 emit_insn (insn);
2444 }
2445 else
2446 emit_library_call (long_int_memcpy_libfunc, LCT_NORMAL, VOIDmode,
2447 dst_reg, Pmode,
2448 src_reg, Pmode,
2449 convert_to_mode (TYPE_MODE (sizetype),
2450 GEN_INT (bytes >> 2),
2451 TYPE_UNSIGNED (sizetype)),
2452 TYPE_MODE (sizetype));
2453 if (rem == 0)
2454 return;
2455
2456 dst = replace_equiv_address_nv (dst, dst_reg);
2457 src = replace_equiv_address_nv (src, src_reg);
2458 bytes -= rem;
2459
2460 if (rem > 1)
2461 {
2462 emit_move_insn (adjust_address_nv (dst, HImode, bytes),
2463 adjust_address_nv (src, HImode, bytes));
2464 bytes += 2;
2465 rem -= 2;
2466 }
2467
2468 if (rem > 0)
2469 emit_move_insn (adjust_address_nv (dst, QImode, bytes),
2470 adjust_address_nv (src, QImode, bytes));
2471 }
2472
2473 /* Generate a call to a library function to move BYTES_RTX bytes from SRC with
2474 address SRC_REG to DST with address DST_REG in 2-bytes chunks. */
2475
2476 static void
2477 expand_block_move_2 (rtx dst, rtx dst_reg, rtx src, rtx src_reg, rtx bytes_rtx)
2478 {
2479 unsigned HOST_WIDE_INT bytes = UINTVAL (bytes_rtx);
2480 unsigned int rem = bytes % 2;
2481
2482 emit_library_call (wrd_memcpy_libfunc, LCT_NORMAL, VOIDmode,
2483 dst_reg, Pmode,
2484 src_reg, Pmode,
2485 convert_to_mode (TYPE_MODE (sizetype),
2486 GEN_INT (bytes >> 1),
2487 TYPE_UNSIGNED (sizetype)),
2488 TYPE_MODE (sizetype));
2489 if (rem == 0)
2490 return;
2491
2492 dst = replace_equiv_address_nv (dst, dst_reg);
2493 src = replace_equiv_address_nv (src, src_reg);
2494 bytes -= rem;
2495
2496 emit_move_insn (adjust_address_nv (dst, QImode, bytes),
2497 adjust_address_nv (src, QImode, bytes));
2498 }
2499
2500 /* Generate a call to a library function to move BYTES_RTX bytes from address
2501 SRC_REG to address DST_REG in 1-byte chunks. */
2502
2503 static void
2504 expand_block_move_1 (rtx dst_reg, rtx src_reg, rtx bytes_rtx)
2505 {
2506 emit_library_call (byt_memcpy_libfunc, LCT_NORMAL, VOIDmode,
2507 dst_reg, Pmode,
2508 src_reg, Pmode,
2509 convert_to_mode (TYPE_MODE (sizetype),
2510 bytes_rtx,
2511 TYPE_UNSIGNED (sizetype)),
2512 TYPE_MODE (sizetype));
2513 }
2514
2515 /* Generate a call to a library function to set BYTES_RTX bytes of DST with
2516 address DST_REG to VALUE_RTX in 4-byte chunks. */
2517
2518 static void
2519 expand_block_set_4 (rtx dst, rtx dst_reg, rtx value_rtx, rtx bytes_rtx)
2520 {
2521 unsigned HOST_WIDE_INT bytes = UINTVAL (bytes_rtx);
2522 unsigned int rem = bytes % 4;
2523
2524 value_rtx = convert_to_mode (Pmode, value_rtx, 1);
2525 emit_library_call (long_int_memset_libfunc, LCT_NORMAL, VOIDmode,
2526 dst_reg, Pmode,
2527 value_rtx, Pmode,
2528 convert_to_mode (TYPE_MODE (sizetype),
2529 GEN_INT (bytes >> 2),
2530 TYPE_UNSIGNED (sizetype)),
2531 TYPE_MODE (sizetype));
2532 if (rem == 0)
2533 return;
2534
2535 dst = replace_equiv_address_nv (dst, dst_reg);
2536 bytes -= rem;
2537
2538 if (rem > 1)
2539 {
2540 if (CONST_INT_P (value_rtx))
2541 {
2542 const unsigned HOST_WIDE_INT value = UINTVAL (value_rtx) & 0xff;
2543 emit_move_insn (adjust_address_nv (dst, HImode, bytes),
2544 gen_int_mode ((value << 8) | value, HImode));
2545 }
2546 else
2547 {
2548 rtx temp = convert_to_mode (QImode, value_rtx, 1);
2549 emit_move_insn (adjust_address_nv (dst, QImode, bytes), temp);
2550 emit_move_insn (adjust_address_nv (dst, QImode, bytes + 1), temp);
2551 }
2552 bytes += 2;
2553 rem -= 2;
2554 }
2555
2556 if (rem > 0)
2557 emit_move_insn (adjust_address_nv (dst, QImode, bytes),
2558 convert_to_mode (QImode, value_rtx, 1));
2559 }
2560
2561 /* Generate a call to a library function to set BYTES_RTX bytes of DST with
2562 address DST_REG to VALUE_RTX in 2-byte chunks. */
2563
2564 static void
2565 expand_block_set_2 (rtx dst, rtx dst_reg, rtx value_rtx, rtx bytes_rtx)
2566 {
2567 unsigned HOST_WIDE_INT bytes = UINTVAL (bytes_rtx);
2568 unsigned int rem = bytes % 2;
2569
2570 value_rtx = convert_to_mode (Pmode, value_rtx, 1);
2571 emit_library_call (wrd_memset_libfunc, LCT_NORMAL, VOIDmode,
2572 dst_reg, Pmode,
2573 value_rtx, Pmode,
2574 convert_to_mode (TYPE_MODE (sizetype),
2575 GEN_INT (bytes >> 1),
2576 TYPE_UNSIGNED (sizetype)),
2577 TYPE_MODE (sizetype));
2578 if (rem == 0)
2579 return;
2580
2581 dst = replace_equiv_address_nv (dst, dst_reg);
2582 bytes -= rem;
2583
2584 emit_move_insn (adjust_address_nv (dst, QImode, bytes),
2585 convert_to_mode (QImode, value_rtx, 1));
2586 }
2587
2588 /* Generate a call to a library function to set BYTES_RTX bytes at address
2589 DST_REG to VALUE_RTX in 1-byte chunks. */
2590
2591 static void
2592 expand_block_set_1 (rtx dst_reg, rtx value_rtx, rtx bytes_rtx)
2593 {
2594 value_rtx = convert_to_mode (Pmode, value_rtx, 1);
2595 emit_library_call (byt_memset_libfunc, LCT_NORMAL, VOIDmode,
2596 dst_reg, Pmode,
2597 value_rtx, Pmode,
2598 convert_to_mode (TYPE_MODE (sizetype),
2599 bytes_rtx,
2600 TYPE_UNSIGNED (sizetype)),
2601 TYPE_MODE (sizetype));
2602 }
2603
2604 /* Expand string/block move operations.
2605
2606 operands[0] is the pointer to the destination.
2607 operands[1] is the pointer to the source.
2608 operands[2] is the number of bytes to move.
2609 operands[3] is the alignment.
2610
2611 Return 1 upon success, 0 otherwise. */
2612
2613 int
2614 visium_expand_block_move (rtx *operands)
2615 {
2616 rtx dst = operands[0];
2617 rtx src = operands[1];
2618 rtx bytes_rtx = operands[2];
2619 rtx align_rtx = operands[3];
2620 const int align = INTVAL (align_rtx);
2621 rtx dst_reg, src_reg;
2622 tree dst_expr, src_expr;
2623
2624 /* We only handle a fixed number of bytes for now. */
2625 if (!CONST_INT_P (bytes_rtx) || INTVAL (bytes_rtx) <= 0)
2626 return 0;
2627
2628 /* Copy the addresses into scratch registers. */
2629 dst_reg = copy_addr_to_reg (XEXP (dst, 0));
2630 src_reg = copy_addr_to_reg (XEXP (src, 0));
2631
2632 /* Move the data with the appropriate granularity. */
2633 if (align >= 4)
2634 expand_block_move_4 (dst, dst_reg, src, src_reg, bytes_rtx);
2635 else if (align >= 2)
2636 expand_block_move_2 (dst, dst_reg, src, src_reg, bytes_rtx);
2637 else
2638 expand_block_move_1 (dst_reg, src_reg, bytes_rtx);
2639
2640 /* Since DST and SRC are passed to a libcall, mark the corresponding
2641 tree EXPR as addressable. */
2642 dst_expr = MEM_EXPR (dst);
2643 src_expr = MEM_EXPR (src);
2644 if (dst_expr)
2645 mark_addressable (dst_expr);
2646 if (src_expr)
2647 mark_addressable (src_expr);
2648
2649 return 1;
2650 }
2651
2652 /* Expand string/block set operations.
2653
2654 operands[0] is the pointer to the destination.
2655 operands[1] is the number of bytes to set.
2656 operands[2] is the source value.
2657 operands[3] is the alignment.
2658
2659 Return 1 upon success, 0 otherwise. */
2660
2661 int
2662 visium_expand_block_set (rtx *operands)
2663 {
2664 rtx dst = operands[0];
2665 rtx bytes_rtx = operands[1];
2666 rtx value_rtx = operands[2];
2667 rtx align_rtx = operands[3];
2668 const int align = INTVAL (align_rtx);
2669 rtx dst_reg;
2670 tree dst_expr;
2671
2672 /* We only handle a fixed number of bytes for now. */
2673 if (!CONST_INT_P (bytes_rtx) || INTVAL (bytes_rtx) <= 0)
2674 return 0;
2675
2676 /* Copy the address into a scratch register. */
2677 dst_reg = copy_addr_to_reg (XEXP (dst, 0));
2678
2679 /* Set the data with the appropriate granularity. */
2680 if (align >= 4)
2681 expand_block_set_4 (dst, dst_reg, value_rtx, bytes_rtx);
2682 else if (align >= 2)
2683 expand_block_set_2 (dst, dst_reg, value_rtx, bytes_rtx);
2684 else
2685 expand_block_set_1 (dst_reg, value_rtx, bytes_rtx);
2686
2687 /* Since DST is passed to a libcall, mark the corresponding
2688 tree EXPR as addressable. */
2689 dst_expr = MEM_EXPR (dst);
2690 if (dst_expr)
2691 mark_addressable (dst_expr);
2692
2693 return 1;
2694 }
2695
2696 /* Initialize a trampoline. M_TRAMP is an RTX for the memory block for the
2697 trampoline, FNDECL is the FUNCTION_DECL for the nested function and
2698 STATIC_CHAIN is an RTX for the static chain value that should be passed
2699 to the function when it is called. */
2700
2701 static void
2702 visium_trampoline_init (rtx m_tramp, tree fndecl, rtx static_chain)
2703 {
2704 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
2705 rtx addr = XEXP (m_tramp, 0);
2706
2707 /* The trampoline initialization sequence is:
2708
2709 moviu r9,%u FUNCTION
2710 movil r9,%l FUNCTION
2711 [nop]
2712 moviu r20,%u STATIC
2713 bra tr,r9,r9
2714 movil r20,%l STATIC
2715
2716 We don't use r0 as the destination register of the branch because we want
2717 the Branch Pre-decode Logic of the GR6 to use the Address Load Array to
2718 predict the branch target. */
2719
2720 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (Pmode, addr, 0)),
2721 plus_constant (SImode,
2722 expand_shift (RSHIFT_EXPR, SImode, fnaddr,
2723 16, NULL_RTX, 1),
2724 0x04a90000));
2725
2726 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (Pmode, addr, 4)),
2727 plus_constant (SImode,
2728 expand_and (SImode, fnaddr, GEN_INT (0xffff),
2729 NULL_RTX),
2730 0x04890000));
2731
2732 if (visium_cpu == PROCESSOR_GR6)
2733 {
2734 /* For the GR6, the BRA insn must be aligned on a 64-bit boundary. */
2735 gcc_assert (TRAMPOLINE_ALIGNMENT >= 64);
2736 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (Pmode, addr, 12)),
2737 gen_int_mode (0, SImode));
2738 }
2739
2740 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (Pmode, addr, 8)),
2741 plus_constant (SImode,
2742 expand_shift (RSHIFT_EXPR, SImode,
2743 static_chain,
2744 16, NULL_RTX, 1),
2745 0x04b40000));
2746
2747 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (Pmode, addr, 12)),
2748 gen_int_mode (0xff892404, SImode));
2749
2750 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (Pmode, addr, 16)),
2751 plus_constant (SImode,
2752 expand_and (SImode, static_chain,
2753 GEN_INT (0xffff), NULL_RTX),
2754 0x04940000));
2755
2756 emit_library_call (set_trampoline_parity_libfunc, LCT_NORMAL, VOIDmode,
2757 addr, SImode);
2758 }
2759
2760 /* Return true if the current function must have and use a frame pointer. */
2761
2762 static bool
2763 visium_frame_pointer_required (void)
2764 {
2765 /* The frame pointer is required if the function isn't leaf to be able to
2766 do manual stack unwinding. */
2767 if (!crtl->is_leaf)
2768 return true;
2769
2770 /* If the stack pointer is dynamically modified in the function, it cannot
2771 serve as the frame pointer. */
2772 if (!crtl->sp_is_unchanging)
2773 return true;
2774
2775 /* If the function receives nonlocal gotos, it needs to save the frame
2776 pointer in the nonlocal_goto_save_area object. */
2777 if (cfun->has_nonlocal_label)
2778 return true;
2779
2780 /* The frame also needs to be established in some special cases. */
2781 if (visium_frame_needed)
2782 return true;
2783
2784 return false;
2785 }
2786
2787 /* Profiling support. Just a call to MCOUNT is needed. No labelled counter
2788 location is involved. Proper support for __builtin_return_address is also
2789 required, which is fairly straightforward provided a frame gets created. */
2790
2791 void
2792 visium_profile_hook (void)
2793 {
2794 visium_frame_needed = true;
2795 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "mcount"), LCT_NORMAL,
2796 VOIDmode);
2797 }
2798
2799 /* A C expression whose value is RTL representing the address in a stack frame
2800 where the pointer to the caller's frame is stored. Assume that FRAMEADDR is
2801 an RTL expression for the address of the stack frame itself.
2802
2803 If you don't define this macro, the default is to return the value of
2804 FRAMEADDR--that is, the stack frame address is also the address of the stack
2805 word that points to the previous frame. */
2806
2807 rtx
2808 visium_dynamic_chain_address (rtx frame)
2809 {
2810 /* This is the default, but we need to make sure the frame gets created. */
2811 visium_frame_needed = true;
2812 return frame;
2813 }
2814
2815 /* A C expression whose value is RTL representing the value of the return
2816 address for the frame COUNT steps up from the current frame, after the
2817 prologue. FRAMEADDR is the frame pointer of the COUNT frame, or the frame
2818 pointer of the COUNT - 1 frame if `RETURN_ADDR_IN_PREVIOUS_FRAME' is
2819 defined.
2820
2821 The value of the expression must always be the correct address when COUNT is
2822 zero, but may be `NULL_RTX' if there is not way to determine the return
2823 address of other frames. */
2824
2825 rtx
2826 visium_return_addr_rtx (int count, rtx frame ATTRIBUTE_UNUSED)
2827 {
2828 /* Dont try to compute anything other than frame zero. */
2829 if (count != 0)
2830 return NULL_RTX;
2831
2832 visium_frame_needed = true;
2833 return
2834 gen_frame_mem (Pmode, plus_constant (Pmode, hard_frame_pointer_rtx, 4));
2835 }
2836
2837 /* Helper function for EH_RETURN_HANDLER_RTX. Return the RTX representing a
2838 location in which to store the address of an exception handler to which we
2839 should return. */
2840
2841 rtx
2842 visium_eh_return_handler_rtx (void)
2843 {
2844 rtx mem
2845 = gen_frame_mem (SImode, plus_constant (Pmode, hard_frame_pointer_rtx, 4));
2846 MEM_VOLATILE_P (mem) = 1;
2847 return mem;
2848 }
2849
2850 static struct machine_function *
2851 visium_init_machine_status (void)
2852 {
2853 return ggc_cleared_alloc<machine_function> ();
2854 }
2855
2856 /* The per-function data machinery is needed to indicate when a frame
2857 is required. */
2858
2859 void
2860 visium_init_expanders (void)
2861 {
2862 init_machine_status = visium_init_machine_status;
2863 }
2864
2865 /* Given a comparison code (EQ, NE, etc.) and the operands of a COMPARE,
2866 return the mode to be used for the comparison. */
2867
2868 machine_mode
2869 visium_select_cc_mode (enum rtx_code code, rtx op0, rtx op1)
2870 {
2871 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
2872 {
2873 switch (code)
2874 {
2875 case EQ:
2876 case NE:
2877 case ORDERED:
2878 case UNORDERED:
2879 case UNLT:
2880 case UNLE:
2881 case UNGT:
2882 case UNGE:
2883 return CCFPmode;
2884
2885 case LT:
2886 case LE:
2887 case GT:
2888 case GE:
2889 return CCFPEmode;
2890
2891 /* These 2 comparison codes are not supported. */
2892 case UNEQ:
2893 case LTGT:
2894 default:
2895 gcc_unreachable ();
2896 }
2897 }
2898
2899 /* This is for the cmp<mode>_sne pattern. */
2900 if (op1 == constm1_rtx)
2901 return CCCmode;
2902
2903 /* This is for the add<mode>3_insn_set_carry pattern. */
2904 if ((code == LTU || code == GEU)
2905 && GET_CODE (op0) == PLUS
2906 && rtx_equal_p (XEXP (op0, 0), op1))
2907 return CCCmode;
2908
2909 /* This is for the {add,sub,neg}<mode>3_insn_set_overflow pattern. */
2910 if ((code == EQ || code == NE)
2911 && GET_CODE (op1) == UNSPEC
2912 && (XINT (op1, 1) == UNSPEC_ADDV
2913 || XINT (op1, 1) == UNSPEC_SUBV
2914 || XINT (op1, 1) == UNSPEC_NEGV))
2915 return CCVmode;
2916
2917 if (op1 != const0_rtx)
2918 return CCmode;
2919
2920 switch (GET_CODE (op0))
2921 {
2922 case PLUS:
2923 case MINUS:
2924 case NEG:
2925 case ASHIFT:
2926 case LTU:
2927 case LT:
2928 /* The C and V flags may be set differently from a COMPARE with zero.
2929 The consequence is that a comparison operator testing C or V must
2930 be turned into another operator not testing C or V and yielding
2931 the same result for a comparison with zero. That's possible for
2932 GE/LT which become NC/NS respectively, but not for GT/LE for which
2933 the altered operator doesn't exist on the Visium. */
2934 return CCNZmode;
2935
2936 case ZERO_EXTRACT:
2937 /* This is a btst, the result is in C instead of Z. */
2938 return CCCmode;
2939
2940 case REG:
2941 case AND:
2942 case IOR:
2943 case XOR:
2944 case NOT:
2945 case ASHIFTRT:
2946 case LSHIFTRT:
2947 case TRUNCATE:
2948 case SIGN_EXTEND:
2949 /* Pretend that the flags are set as for a COMPARE with zero.
2950 That's mostly true, except for the 2 right shift insns that
2951 will set the C flag. But the C flag is relevant only for
2952 the unsigned comparison operators and they are eliminated
2953 when applied to a comparison with zero. */
2954 return CCmode;
2955
2956 /* ??? Cater to the junk RTXes sent by try_merge_compare. */
2957 case ASM_OPERANDS:
2958 case CALL:
2959 case CONST_INT:
2960 case LO_SUM:
2961 case HIGH:
2962 case MEM:
2963 case UNSPEC:
2964 case ZERO_EXTEND:
2965 return CCmode;
2966
2967 default:
2968 gcc_unreachable ();
2969 }
2970 }
2971
2972 /* Split a compare-and-branch with CODE, operands OP0 and OP1, and LABEL. */
2973
2974 void
2975 visium_split_cbranch (enum rtx_code code, rtx op0, rtx op1, rtx label)
2976 {
2977 machine_mode cc_mode = visium_select_cc_mode (code, op0, op1);
2978 rtx flags = gen_rtx_REG (cc_mode, FLAGS_REGNUM);
2979
2980 rtx x = gen_rtx_COMPARE (cc_mode, op0, op1);
2981 x = gen_rtx_SET (flags, x);
2982 emit_insn (x);
2983
2984 x = gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
2985 x = gen_rtx_IF_THEN_ELSE (VOIDmode, x, gen_rtx_LABEL_REF (Pmode, label),
2986 pc_rtx);
2987 x = gen_rtx_SET (pc_rtx, x);
2988 emit_jump_insn (x);
2989
2990 visium_flags_exposed = true;
2991 }
2992
2993 /* Branch instructions on the Visium.
2994
2995 Setting aside the interrupt-handling specific instructions, the ISA has
2996 two branch instructions: BRR and BRA. The former is used to implement
2997 short branches (+/- 2^17) within functions and its target is encoded in
2998 the instruction. The latter is used to implement all the other types
2999 of control flow changes and its target might not be statically known
3000 or even easily predictable at run time. Here's a complete summary of
3001 the patterns that generate a BRA instruction:
3002
3003 1. Indirect jump
3004 2. Table jump
3005 3. Call
3006 4. Sibling call
3007 5. Return
3008 6. Long branch
3009 7. Trampoline
3010
3011 Among these patterns, only the return (5) and the long branch (6) can be
3012 conditional; all the other patterns are always unconditional.
3013
3014 The following algorithm can be used to identify the pattern for which
3015 the BRA instruction was generated and work out its target:
3016
3017 A. If the source is r21 and the destination is r0, this is a return (5)
3018 and the target is the caller (i.e. the value of r21 on function's
3019 entry).
3020
3021 B. If the source is rN, N != 21 and the destination is r0, this is either
3022 an indirect jump or a table jump (1, 2) and the target is not easily
3023 predictable.
3024
3025 C. If the source is rN, N != 21 and the destination is r21, this is a call
3026 (3) and the target is given by the preceding MOVIL/MOVIU pair for rN,
3027 unless this is an indirect call in which case the target is not easily
3028 predictable.
3029
3030 D. If the source is rN, N != 21 and the destination is also rN, this is
3031 either a sibling call or a trampoline (4, 7) and the target is given
3032 by the preceding MOVIL/MOVIU pair for rN.
3033
3034 E. If the source is r21 and the destination is also r21, this is a long
3035 branch (6) and the target is given by the preceding MOVIL/MOVIU pair
3036 for r21.
3037
3038 The other combinations are not used. This implementation has been devised
3039 to accommodate the branch predictor of the GR6 but is used unconditionally
3040 by the compiler, i.e. including for earlier processors. */
3041
3042 /* Output a conditional/unconditional branch to LABEL. COND is the string
3043 condition. INSN is the instruction. */
3044
3045 static const char *
3046 output_branch (rtx label, const char *cond, rtx_insn *insn)
3047 {
3048 char str[64];
3049 rtx operands[2];
3050
3051 gcc_assert (cond);
3052 operands[0] = label;
3053
3054 /* If the length of the instruction is greater than 12, then this is a
3055 long branch and we need to work harder to emit it properly. */
3056 if (get_attr_length (insn) > 12)
3057 {
3058 bool spilled;
3059
3060 /* If the link register has been saved, then we use it. */
3061 if (current_function_saves_lr ())
3062 {
3063 operands[1] = regno_reg_rtx [LINK_REGNUM];
3064 spilled = false;
3065 }
3066
3067 /* Or else, if the long-branch register isn't live, we use it. */
3068 else if (!df_regs_ever_live_p (long_branch_regnum))
3069 {
3070 operands[1] = regno_reg_rtx [long_branch_regnum];
3071 spilled = false;
3072 }
3073
3074 /* Otherwise, we will use the long-branch register but we need to
3075 spill it to the stack and reload it at the end. We should have
3076 reserved the LR slot for this purpose. */
3077 else
3078 {
3079 operands[1] = regno_reg_rtx [long_branch_regnum];
3080 spilled = true;
3081 gcc_assert (current_function_has_lr_slot ());
3082 }
3083
3084 /* First emit the spill to the stack:
3085
3086 insn_in_delay_slot
3087 write.l [1](sp),reg */
3088 if (spilled)
3089 {
3090 if (final_sequence)
3091 {
3092 rtx_insn *delay = NEXT_INSN (insn);
3093 gcc_assert (delay);
3094
3095 final_scan_insn (delay, asm_out_file, optimize, 0, NULL);
3096 PATTERN (delay) = gen_blockage ();
3097 INSN_CODE (delay) = -1;
3098 }
3099
3100 if (current_function_saves_fp ())
3101 output_asm_insn ("write.l 1(sp),%1", operands);
3102 else
3103 output_asm_insn ("write.l (sp),%1", operands);
3104 }
3105
3106 /* Then emit the core sequence:
3107
3108 moviu reg,%u label
3109 movil reg,%l label
3110 bra tr,reg,reg
3111
3112 We don't use r0 as the destination register of the branch because we
3113 want the Branch Pre-decode Logic of the GR6 to use the Address Load
3114 Array to predict the branch target. */
3115 output_asm_insn ("moviu %1,%%u %0", operands);
3116 output_asm_insn ("movil %1,%%l %0", operands);
3117 strcpy (str, "bra ");
3118 strcat (str, cond);
3119 strcat (str, ",%1,%1");
3120 if (!spilled)
3121 strcat (str, "%#");
3122 strcat (str, "\t\t;long branch");
3123 output_asm_insn (str, operands);
3124
3125 /* Finally emit the reload:
3126
3127 read.l reg,[1](sp) */
3128 if (spilled)
3129 {
3130 if (current_function_saves_fp ())
3131 output_asm_insn (" read.l %1,1(sp)", operands);
3132 else
3133 output_asm_insn (" read.l %1,(sp)", operands);
3134 }
3135 }
3136
3137 /* Or else, if the label is PC, then this is a return. */
3138 else if (label == pc_rtx)
3139 {
3140 strcpy (str, "bra ");
3141 strcat (str, cond);
3142 strcat (str, ",r21,r0%#\t\t;return");
3143 output_asm_insn (str, operands);
3144 }
3145
3146 /* Otherwise, this is a short branch. */
3147 else
3148 {
3149 strcpy (str, "brr ");
3150 strcat (str, cond);
3151 strcat (str, ",%0%#");
3152 output_asm_insn (str, operands);
3153 }
3154
3155 return "";
3156 }
3157
3158 /* Output an unconditional branch to LABEL. INSN is the instruction. */
3159
3160 const char *
3161 output_ubranch (rtx label, rtx_insn *insn)
3162 {
3163 return output_branch (label, "tr", insn);
3164 }
3165
3166 /* Output a conditional branch to LABEL. CODE is the comparison code.
3167 CC_MODE is the mode of the CC register. REVERSED is non-zero if we
3168 should reverse the sense of the comparison. INSN is the instruction. */
3169
3170 const char *
3171 output_cbranch (rtx label, enum rtx_code code, machine_mode cc_mode,
3172 int reversed, rtx_insn *insn)
3173 {
3174 const char *cond;
3175
3176 if (reversed)
3177 {
3178 if (cc_mode == CCFPmode || cc_mode == CCFPEmode)
3179 code = reverse_condition_maybe_unordered (code);
3180 else
3181 code = reverse_condition (code);
3182 }
3183
3184 switch (code)
3185 {
3186 case NE:
3187 if (cc_mode == CCCmode)
3188 cond = "cs";
3189 else if (cc_mode == CCVmode)
3190 cond = "os";
3191 else
3192 cond = "ne";
3193 break;
3194
3195 case EQ:
3196 if (cc_mode == CCCmode)
3197 cond = "cc";
3198 else if (cc_mode == CCVmode)
3199 cond = "oc";
3200 else
3201 cond = "eq";
3202 break;
3203
3204 case GE:
3205 if (cc_mode == CCNZmode)
3206 cond = "nc";
3207 else
3208 cond = "ge";
3209 break;
3210
3211 case GT:
3212 cond = "gt";
3213 break;
3214
3215 case LE:
3216 if (cc_mode == CCFPmode || cc_mode == CCFPEmode)
3217 cond = "ls";
3218 else
3219 cond = "le";
3220 break;
3221
3222 case LT:
3223 if (cc_mode == CCFPmode || cc_mode == CCFPEmode)
3224 cond = "cs"; /* or "ns" */
3225 else if (cc_mode == CCNZmode)
3226 cond = "ns";
3227 else
3228 cond = "lt";
3229 break;
3230
3231 case GEU:
3232 cond = "cc";
3233 break;
3234
3235 case GTU:
3236 cond = "hi";
3237 break;
3238
3239 case LEU:
3240 cond = "ls";
3241 break;
3242
3243 case LTU:
3244 cond = "cs";
3245 break;
3246
3247 case UNORDERED:
3248 cond = "os";
3249 break;
3250
3251 case ORDERED:
3252 cond = "oc";
3253 break;
3254
3255 case UNGE:
3256 cond = "cc"; /* or "nc" */
3257 break;
3258
3259 case UNGT:
3260 cond = "hi";
3261 break;
3262
3263 case UNLE:
3264 cond = "le";
3265 break;
3266
3267 case UNLT:
3268 cond = "lt";
3269 break;
3270
3271 /* These 2 comparison codes are not supported. */
3272 case UNEQ:
3273 case LTGT:
3274 default:
3275 gcc_unreachable ();
3276 }
3277
3278 return output_branch (label, cond, insn);
3279 }
3280
3281 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
3282
3283 static bool
3284 visium_print_operand_punct_valid_p (unsigned char code)
3285 {
3286 return code == '#';
3287 }
3288
3289 /* Implement TARGET_PRINT_OPERAND. Output to stdio stream FILE the assembler
3290 syntax for an instruction operand OP subject to the modifier LETTER. */
3291
3292 static void
3293 visium_print_operand (FILE *file, rtx op, int letter)
3294 {
3295 switch (letter)
3296 {
3297 case '#':
3298 /* Output an insn in a delay slot. */
3299 if (final_sequence)
3300 visium_indent_opcode = 1;
3301 else
3302 fputs ("\n\t nop", file);
3303 return;
3304
3305 case 'b':
3306 /* Print LS 8 bits of operand. */
3307 fprintf (file, HOST_WIDE_INT_PRINT_UNSIGNED, UINTVAL (op) & 0xff);
3308 return;
3309
3310 case 'w':
3311 /* Print LS 16 bits of operand. */
3312 fprintf (file, HOST_WIDE_INT_PRINT_UNSIGNED, UINTVAL (op) & 0xffff);
3313 return;
3314
3315 case 'u':
3316 /* Print MS 16 bits of operand. */
3317 fprintf (file,
3318 HOST_WIDE_INT_PRINT_UNSIGNED, (UINTVAL (op) >> 16) & 0xffff);
3319 return;
3320
3321 case 'r':
3322 /* It's either a register or zero. */
3323 if (GET_CODE (op) == REG)
3324 fputs (reg_names[REGNO (op)], file);
3325 else
3326 fputs (reg_names[0], file);
3327 return;
3328
3329 case 'f':
3330 /* It's either a FP register or zero. */
3331 if (GET_CODE (op) == REG)
3332 fputs (reg_names[REGNO (op)], file);
3333 else
3334 fputs (reg_names[FP_FIRST_REGNUM], file);
3335 return;
3336 }
3337
3338 switch (GET_CODE (op))
3339 {
3340 case REG:
3341 if (letter == 'd')
3342 fputs (reg_names[REGNO (op) + 1], file);
3343 else
3344 fputs (reg_names[REGNO (op)], file);
3345 break;
3346
3347 case SYMBOL_REF:
3348 case LABEL_REF:
3349 case CONST:
3350 output_addr_const (file, op);
3351 break;
3352
3353 case MEM:
3354 visium_print_operand_address (file, GET_MODE (op), XEXP (op, 0));
3355 break;
3356
3357 case CONST_INT:
3358 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
3359 break;
3360
3361 case CODE_LABEL:
3362 asm_fprintf (file, "%LL%d", CODE_LABEL_NUMBER (op));
3363 break;
3364
3365 case HIGH:
3366 visium_print_operand (file, XEXP (op, 1), letter);
3367 break;
3368
3369 default:
3370 fatal_insn ("illegal operand ", op);
3371 }
3372 }
3373
3374 /* Implement TARGET_PRINT_OPERAND_ADDRESS. Output to stdio stream FILE the
3375 assembler syntax for an instruction operand that is a memory reference
3376 whose address is ADDR. */
3377
3378 static void
3379 visium_print_operand_address (FILE *file, machine_mode mode, rtx addr)
3380 {
3381 switch (GET_CODE (addr))
3382 {
3383 case REG:
3384 case SUBREG:
3385 fprintf (file, "(%s)", reg_names[true_regnum (addr)]);
3386 break;
3387
3388 case PLUS:
3389 {
3390 rtx x = XEXP (addr, 0), y = XEXP (addr, 1);
3391
3392 switch (GET_CODE (x))
3393 {
3394 case REG:
3395 case SUBREG:
3396 if (CONST_INT_P (y))
3397 {
3398 unsigned int regno = true_regnum (x);
3399 HOST_WIDE_INT val = INTVAL (y);
3400 switch (mode)
3401 {
3402 case E_SImode:
3403 case E_DImode:
3404 case E_SFmode:
3405 case E_DFmode:
3406 val >>= 2;
3407 break;
3408
3409 case E_HImode:
3410 val >>= 1;
3411 break;
3412
3413 case E_QImode:
3414 default:
3415 break;
3416 }
3417 fprintf (file, HOST_WIDE_INT_PRINT_DEC"(%s)", val,
3418 reg_names[regno]);
3419 }
3420 else
3421 fatal_insn ("illegal operand address (1)", addr);
3422 break;
3423
3424 default:
3425 if (CONSTANT_P (x) && CONSTANT_P (y))
3426 output_addr_const (file, addr);
3427 else
3428 fatal_insn ("illegal operand address (2)", addr);
3429 break;
3430 }
3431 }
3432 break;
3433
3434 case LABEL_REF:
3435 case SYMBOL_REF:
3436 case CONST_INT:
3437 case CONST:
3438 output_addr_const (file, addr);
3439 break;
3440
3441 case NOTE:
3442 if (NOTE_KIND (addr) != NOTE_INSN_DELETED_LABEL)
3443 fatal_insn ("illegal operand address (3)", addr);
3444 break;
3445
3446 case CODE_LABEL:
3447 asm_fprintf (file, "%LL%d", CODE_LABEL_NUMBER (addr));
3448 break;
3449
3450 default:
3451 fatal_insn ("illegal operand address (4)", addr);
3452 break;
3453 }
3454 }
3455
3456 /* The Visium stack frames look like:
3457
3458 Before call After call
3459 +-----------------------+ +-----------------------+
3460 | | | |
3461 high | previous | | previous |
3462 mem | frame | | frame |
3463 | | | |
3464 +-----------------------+ +-----------------------+
3465 | | | |
3466 | arguments on stack | | arguments on stack |
3467 | | | |
3468 SP+0->+-----------------------+ +-----------------------+
3469 | reg parm save area, |
3470 | only created for |
3471 | variable argument |
3472 | functions |
3473 +-----------------------+
3474 | |
3475 | register save area |
3476 | |
3477 +-----------------------+
3478 | |
3479 | local variables |
3480 | |
3481 FP+8->+-----------------------+
3482 | return address |
3483 FP+4->+-----------------------+
3484 | previous FP |
3485 FP+0->+-----------------------+
3486 | |
3487 | alloca allocations |
3488 | |
3489 +-----------------------+
3490 | |
3491 low | arguments on stack |
3492 mem | |
3493 SP+0->+-----------------------+
3494
3495 Notes:
3496 1) The "reg parm save area" does not exist for non variable argument fns.
3497 2) The FP register is not saved if `frame_pointer_needed' is zero and it
3498 is not altered in the current function.
3499 3) The return address is not saved if there is no frame pointer and the
3500 current function is leaf.
3501 4) If the return address is not saved and the static chain register is
3502 live in the function, we allocate the return address slot to be able
3503 to spill the register for a long branch. */
3504
3505 /* Define the register classes for local purposes. */
3506 enum reg_type { general, mdb, mdc, floating, last_type};
3507
3508 #define GET_REG_TYPE(regno) \
3509 (GP_REGISTER_P (regno) ? general : \
3510 (regno) == MDB_REGNUM ? mdb : \
3511 (regno) == MDC_REGNUM ? mdc : \
3512 floating)
3513
3514 /* First regno of each register type. */
3515 const int first_regno[last_type] = {0, MDB_REGNUM, MDC_REGNUM, FP_FIRST_REGNUM};
3516
3517 /* Size in bytes of each register type. */
3518 const int reg_type_size[last_type] = {4, 8, 4, 4};
3519
3520 /* Structure to be filled in by visium_compute_frame_size. */
3521 struct visium_frame_info
3522 {
3523 unsigned int save_area_size; /* # bytes in the reg parm save area. */
3524 unsigned int reg_size1; /* # bytes to store first block of regs. */
3525 unsigned int reg_size2; /* # bytes to store second block of regs. */
3526 unsigned int max_reg1; /* max. regno in first block */
3527 unsigned int var_size; /* # bytes that variables take up. */
3528 unsigned int save_fp; /* Nonzero if fp must be saved. */
3529 unsigned int save_lr; /* Nonzero if lr must be saved. */
3530 unsigned int lr_slot; /* Nonzero if the lr slot is needed. */
3531 unsigned int combine; /* Nonzero if we can combine the allocation of
3532 variables and regs. */
3533 unsigned int interrupt; /* Nonzero if the function is an interrupt
3534 handler. */
3535 unsigned int mask[last_type]; /* Masks of saved regs: gp, mdb, mdc, fp */
3536 };
3537
3538 /* Current frame information calculated by visium_compute_frame_size. */
3539 static struct visium_frame_info current_frame_info;
3540
3541 /* Accessor for current_frame_info.save_fp. */
3542
3543 static inline bool
3544 current_function_saves_fp (void)
3545 {
3546 return current_frame_info.save_fp != 0;
3547 }
3548
3549 /* Accessor for current_frame_info.save_lr. */
3550
3551 static inline bool
3552 current_function_saves_lr (void)
3553 {
3554 return current_frame_info.save_lr != 0;
3555 }
3556
3557 /* Accessor for current_frame_info.lr_slot. */
3558
3559 static inline bool
3560 current_function_has_lr_slot (void)
3561 {
3562 return current_frame_info.lr_slot != 0;
3563 }
3564
3565 /* Return non-zero if register REGNO needs to be saved in the frame. */
3566
3567 static int
3568 visium_save_reg_p (int interrupt, int regno)
3569 {
3570 switch (regno)
3571 {
3572 case HARD_FRAME_POINTER_REGNUM:
3573 /* This register is call-saved but handled specially. */
3574 return 0;
3575
3576 case MDC_REGNUM:
3577 /* This register is fixed but can be modified. */
3578 break;
3579
3580 case 29:
3581 case 30:
3582 /* These registers are fixed and hold the interrupt context. */
3583 return (interrupt != 0);
3584
3585 default:
3586 /* The other fixed registers are either immutable or special. */
3587 if (fixed_regs[regno])
3588 return 0;
3589 break;
3590 }
3591
3592 if (interrupt)
3593 {
3594 if (crtl->is_leaf)
3595 {
3596 if (df_regs_ever_live_p (regno))
3597 return 1;
3598 }
3599 else if (call_used_or_fixed_reg_p (regno))
3600 return 1;
3601
3602 /* To save mdb requires two temporary registers. To save mdc or
3603 any of the floating registers requires one temporary
3604 register. If this is an interrupt routine, the temporary
3605 registers need to be saved as well. These temporary registers
3606 are call used, so we only need deal with the case of leaf
3607 functions here. */
3608 if (regno == PROLOGUE_TMP_REGNUM)
3609 {
3610 if (df_regs_ever_live_p (MDB_REGNUM)
3611 || df_regs_ever_live_p (MDC_REGNUM))
3612 return 1;
3613
3614 for (int i = FP_FIRST_REGNUM; i <= FP_LAST_REGNUM; i++)
3615 if (df_regs_ever_live_p (i))
3616 return 1;
3617 }
3618
3619 else if (regno == PROLOGUE_TMP_REGNUM + 1)
3620 {
3621 if (df_regs_ever_live_p (MDB_REGNUM))
3622 return 1;
3623 }
3624 }
3625
3626 return df_regs_ever_live_p (regno) && !call_used_or_fixed_reg_p (regno);
3627 }
3628
3629 /* Compute the frame size required by the function. This function is called
3630 during the reload pass and also by visium_expand_prologue. */
3631
3632 static int
3633 visium_compute_frame_size (int size)
3634 {
3635 const int save_area_size = visium_reg_parm_save_area_size;
3636 const int var_size = VISIUM_STACK_ALIGN (size);
3637 const int save_fp
3638 = frame_pointer_needed || df_regs_ever_live_p (HARD_FRAME_POINTER_REGNUM);
3639 const int save_lr = frame_pointer_needed || !crtl->is_leaf;
3640 const int lr_slot = !save_lr && df_regs_ever_live_p (long_branch_regnum);
3641 const int local_frame_offset
3642 = (save_fp + save_lr + lr_slot) * UNITS_PER_WORD;
3643 const int interrupt = visium_interrupt_function_p ();
3644 unsigned int mask[last_type];
3645 int reg_size1 = 0;
3646 int max_reg1 = 0;
3647 int reg_size2 = 0;
3648 int reg_size;
3649 int combine;
3650 int frame_size;
3651 int regno;
3652
3653 memset (mask, 0, last_type * sizeof (unsigned int));
3654
3655 /* The registers may need stacking in 2 blocks since only 32 32-bit words
3656 can be indexed from a given base address. */
3657 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
3658 {
3659 if (visium_save_reg_p (interrupt, regno))
3660 {
3661 enum reg_type reg_type = GET_REG_TYPE (regno);
3662 int mask_bit = 1 << (regno - first_regno[reg_type]);
3663 int nbytes = reg_type_size[reg_type];
3664
3665 if (reg_size1 + nbytes > 32 * UNITS_PER_WORD)
3666 break;
3667
3668 reg_size1 += nbytes;
3669 max_reg1 = regno;
3670 mask[reg_type] |= mask_bit;
3671 }
3672 }
3673
3674 for (regno = max_reg1 + 1; regno < FIRST_PSEUDO_REGISTER; regno++)
3675 {
3676 if (visium_save_reg_p (interrupt, regno))
3677 {
3678 enum reg_type reg_type = GET_REG_TYPE (regno);
3679 int mask_bit = 1 << (regno - first_regno[reg_type]);
3680 int nbytes = reg_type_size[reg_type];
3681
3682 reg_size2 += nbytes;
3683 mask[reg_type] |= mask_bit;
3684 }
3685 }
3686
3687 reg_size = reg_size2 ? reg_size2 : reg_size1;
3688 combine = (local_frame_offset + var_size + reg_size) <= 32 * UNITS_PER_WORD;
3689 frame_size
3690 = local_frame_offset + var_size + reg_size2 + reg_size1 + save_area_size;
3691
3692 current_frame_info.save_area_size = save_area_size;
3693 current_frame_info.reg_size1 = reg_size1;
3694 current_frame_info.max_reg1 = max_reg1;
3695 current_frame_info.reg_size2 = reg_size2;
3696 current_frame_info.var_size = var_size;
3697 current_frame_info.save_fp = save_fp;
3698 current_frame_info.save_lr = save_lr;
3699 current_frame_info.lr_slot = lr_slot;
3700 current_frame_info.combine = combine;
3701 current_frame_info.interrupt = interrupt;
3702
3703 memcpy (current_frame_info.mask, mask, last_type * sizeof (unsigned int));
3704
3705 return frame_size;
3706 }
3707
3708 /* Helper function for INITIAL_ELIMINATION_OFFSET(FROM, TO, OFFSET). Define
3709 the offset between two registers, one to be eliminated, and the other its
3710 replacement, at the start of a routine. */
3711
3712 int
3713 visium_initial_elimination_offset (int from, int to ATTRIBUTE_UNUSED)
3714 {
3715 const int save_fp = current_frame_info.save_fp;
3716 const int save_lr = current_frame_info.save_lr;
3717 const int lr_slot = current_frame_info.lr_slot;
3718 int offset;
3719
3720 if (from == FRAME_POINTER_REGNUM)
3721 offset = (save_fp + save_lr + lr_slot) * UNITS_PER_WORD;
3722 else if (from == ARG_POINTER_REGNUM)
3723 offset = visium_compute_frame_size (get_frame_size ());
3724 else
3725 gcc_unreachable ();
3726
3727 return offset;
3728 }
3729
3730 /* For an interrupt handler, we may be saving call-clobbered registers.
3731 Say the epilogue uses these in addition to the link register. */
3732
3733 int
3734 visium_epilogue_uses (int regno)
3735 {
3736 if (regno == LINK_REGNUM)
3737 return 1;
3738
3739 if (reload_completed)
3740 {
3741 enum reg_type reg_type = GET_REG_TYPE (regno);
3742 int mask_bit = 1 << (regno - first_regno[reg_type]);
3743
3744 return (current_frame_info.mask[reg_type] & mask_bit) != 0;
3745 }
3746
3747 return 0;
3748 }
3749
3750 /* Wrapper around emit_insn that sets RTX_FRAME_RELATED_P on the insn. */
3751
3752 static rtx
3753 emit_frame_insn (rtx x)
3754 {
3755 x = emit_insn (x);
3756 RTX_FRAME_RELATED_P (x) = 1;
3757 return x;
3758 }
3759
3760 /* Allocate ALLOC bytes on the stack and save the registers LOW_REGNO to
3761 HIGH_REGNO at OFFSET from the stack pointer. */
3762
3763 static void
3764 visium_save_regs (int alloc, int offset, int low_regno, int high_regno)
3765 {
3766 /* If this is an interrupt handler function, then mark the register
3767 stores as volatile. This will prevent the instruction scheduler
3768 from scrambling the order of register saves. */
3769 const int volatile_p = current_frame_info.interrupt;
3770 int regno;
3771
3772 /* Allocate the stack space. */
3773 emit_frame_insn (gen_addsi3_flags (stack_pointer_rtx, stack_pointer_rtx,
3774 GEN_INT (-alloc)));
3775
3776 for (regno = low_regno; regno <= high_regno; regno++)
3777 {
3778 enum reg_type reg_type = GET_REG_TYPE (regno);
3779 int mask_bit = 1 << (regno - first_regno[reg_type]);
3780 rtx insn;
3781
3782 if (current_frame_info.mask[reg_type] & mask_bit)
3783 {
3784 offset -= reg_type_size[reg_type];
3785 switch (reg_type)
3786 {
3787 case general:
3788 {
3789 rtx mem
3790 = gen_frame_mem (SImode,
3791 plus_constant (Pmode,
3792 stack_pointer_rtx, offset));
3793 MEM_VOLATILE_P (mem) = volatile_p;
3794 emit_frame_insn (gen_movsi (mem, gen_rtx_REG (SImode, regno)));
3795 }
3796 break;
3797
3798 case mdb:
3799 {
3800 rtx tmp = gen_rtx_REG (DImode, PROLOGUE_TMP_REGNUM);
3801 rtx mem
3802 = gen_frame_mem (DImode,
3803 plus_constant (Pmode,
3804 stack_pointer_rtx, offset));
3805 rtx reg = gen_rtx_REG (DImode, regno);
3806 MEM_VOLATILE_P (mem) = volatile_p;
3807 emit_insn (gen_movdi (tmp, reg));
3808 /* Do not generate CFI if in interrupt handler. */
3809 if (volatile_p)
3810 emit_insn (gen_movdi (mem, tmp));
3811 else
3812 {
3813 insn = emit_frame_insn (gen_movdi (mem, tmp));
3814 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3815 gen_rtx_SET (mem, reg));
3816 }
3817 }
3818 break;
3819
3820 case mdc:
3821 {
3822 rtx tmp = gen_rtx_REG (SImode, PROLOGUE_TMP_REGNUM);
3823 rtx mem
3824 = gen_frame_mem (SImode,
3825 plus_constant (Pmode,
3826 stack_pointer_rtx, offset));
3827 rtx reg = gen_rtx_REG (SImode, regno);
3828 MEM_VOLATILE_P (mem) = volatile_p;
3829 emit_insn (gen_movsi (tmp, reg));
3830 insn = emit_frame_insn (gen_movsi (mem, tmp));
3831 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3832 gen_rtx_SET (mem, reg));
3833 }
3834 break;
3835
3836 case floating:
3837 {
3838 rtx tmp = gen_rtx_REG (SFmode, PROLOGUE_TMP_REGNUM);
3839 rtx mem
3840 = gen_frame_mem (SFmode,
3841 plus_constant (Pmode,
3842 stack_pointer_rtx, offset));
3843 rtx reg = gen_rtx_REG (SFmode, regno);
3844 MEM_VOLATILE_P (mem) = volatile_p;
3845 emit_insn (gen_movsf (tmp, reg));
3846 insn = emit_frame_insn (gen_movsf (mem, tmp));
3847 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3848 gen_rtx_SET (mem, reg));
3849 }
3850 break;
3851
3852 default:
3853 break;
3854 }
3855 }
3856 }
3857 }
3858
3859 /* This function generates the code for function entry. */
3860
3861 void
3862 visium_expand_prologue (void)
3863 {
3864 const int frame_size = visium_compute_frame_size (get_frame_size ());
3865 const int save_area_size = current_frame_info.save_area_size;
3866 const int reg_size1 = current_frame_info.reg_size1;
3867 const int max_reg1 = current_frame_info.max_reg1;
3868 const int reg_size2 = current_frame_info.reg_size2;
3869 const int var_size = current_frame_info.var_size;
3870 const int save_fp = current_frame_info.save_fp;
3871 const int save_lr = current_frame_info.save_lr;
3872 const int lr_slot = current_frame_info.lr_slot;
3873 const int local_frame_offset
3874 = (save_fp + save_lr + lr_slot) * UNITS_PER_WORD;
3875 const int combine = current_frame_info.combine;
3876 int reg_size;
3877 int first_reg;
3878 int fsize;
3879
3880 /* Save the frame size for future references. */
3881 visium_frame_size = frame_size;
3882
3883 if (flag_stack_usage_info)
3884 current_function_static_stack_size = frame_size;
3885
3886 /* If the registers have to be stacked in 2 blocks, stack the first one. */
3887 if (reg_size2)
3888 {
3889 visium_save_regs (reg_size1 + save_area_size, reg_size1, 0, max_reg1);
3890 reg_size = reg_size2;
3891 first_reg = max_reg1 + 1;
3892 fsize = local_frame_offset + var_size + reg_size2;
3893 }
3894 else
3895 {
3896 reg_size = reg_size1;
3897 first_reg = 0;
3898 fsize = local_frame_offset + var_size + reg_size1 + save_area_size;
3899 }
3900
3901 /* If we can't combine register stacking with variable allocation, partially
3902 allocate and stack the (remaining) registers now. */
3903 if (reg_size && !combine)
3904 visium_save_regs (fsize - local_frame_offset - var_size, reg_size,
3905 first_reg, FIRST_PSEUDO_REGISTER - 1);
3906
3907 /* If we can combine register stacking with variable allocation, fully
3908 allocate and stack the (remaining) registers now. */
3909 if (reg_size && combine)
3910 visium_save_regs (fsize, local_frame_offset + var_size + reg_size,
3911 first_reg, FIRST_PSEUDO_REGISTER - 1);
3912
3913 /* Otherwise space may still need to be allocated for the variables. */
3914 else if (fsize)
3915 {
3916 const int alloc_size = reg_size ? local_frame_offset + var_size : fsize;
3917
3918 if (alloc_size > 65535)
3919 {
3920 rtx tmp = gen_rtx_REG (SImode, PROLOGUE_TMP_REGNUM), insn;
3921 emit_insn (gen_movsi (tmp, GEN_INT (alloc_size)));
3922 insn = emit_frame_insn (gen_subsi3_flags (stack_pointer_rtx,
3923 stack_pointer_rtx,
3924 tmp));
3925 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3926 gen_rtx_SET (stack_pointer_rtx,
3927 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3928 GEN_INT (-alloc_size))));
3929 }
3930 else
3931 emit_frame_insn (gen_addsi3_flags (stack_pointer_rtx,
3932 stack_pointer_rtx,
3933 GEN_INT (-alloc_size)));
3934 }
3935
3936 if (save_fp)
3937 emit_frame_insn (gen_movsi (gen_frame_mem (SImode, stack_pointer_rtx),
3938 hard_frame_pointer_rtx));
3939
3940 if (frame_pointer_needed)
3941 emit_frame_insn (gen_stack_save ());
3942
3943 if (save_lr)
3944 {
3945 rtx base_rtx, mem;
3946
3947 /* Normally the frame pointer and link register get saved via
3948 write.l (sp),fp
3949 move.l fp,sp
3950 write.l 1(sp),r21
3951
3952 Indexing off sp rather than fp to store the link register
3953 avoids presenting the instruction scheduler with an initial
3954 pipeline hazard. If however the frame is needed for eg.
3955 __builtin_return_address which needs to retrieve the saved
3956 value of the link register from the stack at fp + 4 then
3957 indexing from sp can confuse the dataflow, causing the link
3958 register to be retrieved before it has been saved. */
3959 if (cfun->machine->frame_needed)
3960 base_rtx = hard_frame_pointer_rtx;
3961 else
3962 base_rtx = stack_pointer_rtx;
3963
3964 mem = gen_frame_mem (SImode,
3965 plus_constant (Pmode,
3966 base_rtx, save_fp * UNITS_PER_WORD));
3967 emit_frame_insn (gen_movsi (mem, gen_rtx_REG (SImode, LINK_REGNUM)));
3968 }
3969 }
3970
3971 static GTY(()) rtx cfa_restores;
3972
3973 /* Queue a REG_CFA_RESTORE note until next stack manipulation insn. */
3974
3975 static void
3976 visium_add_cfa_restore_note (rtx reg)
3977 {
3978 cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg, cfa_restores);
3979 }
3980
3981 /* Add queued REG_CFA_RESTORE notes to INSN, if any. */
3982
3983 static void
3984 visium_add_queued_cfa_restore_notes (rtx insn)
3985 {
3986 rtx last;
3987 if (!cfa_restores)
3988 return;
3989 for (last = cfa_restores; XEXP (last, 1); last = XEXP (last, 1))
3990 ;
3991 XEXP (last, 1) = REG_NOTES (insn);
3992 REG_NOTES (insn) = cfa_restores;
3993 cfa_restores = NULL_RTX;
3994 }
3995
3996 /* Restore the registers LOW_REGNO to HIGH_REGNO from the save area at OFFSET
3997 from the stack pointer and pop DEALLOC bytes off the stack. */
3998
3999 static void
4000 visium_restore_regs (int dealloc, int offset, int high_regno, int low_regno)
4001 {
4002 /* If this is an interrupt handler function, then mark the register
4003 restores as volatile. This will prevent the instruction scheduler
4004 from scrambling the order of register restores. */
4005 const int volatile_p = current_frame_info.interrupt;
4006 int r30_offset = -1;
4007 int regno;
4008
4009 for (regno = high_regno; regno >= low_regno; --regno)
4010 {
4011 enum reg_type reg_type = GET_REG_TYPE (regno);
4012 int mask_bit = 1 << (regno - first_regno[reg_type]);
4013
4014 if (current_frame_info.mask[reg_type] & mask_bit)
4015 {
4016 switch (reg_type)
4017 {
4018 case general:
4019 /* Postpone restoring the interrupted context registers
4020 until last, since they need to be preceded by a dsi. */
4021 if (regno == 29)
4022 ;
4023 else if (regno == 30)
4024 r30_offset = offset;
4025 else
4026 {
4027 rtx mem
4028 = gen_frame_mem (SImode,
4029 plus_constant (Pmode,
4030 stack_pointer_rtx,
4031 offset));
4032 rtx reg = gen_rtx_REG (SImode, regno);
4033 MEM_VOLATILE_P (mem) = volatile_p;
4034 emit_insn (gen_movsi (reg, mem));
4035 visium_add_cfa_restore_note (reg);
4036 }
4037 break;
4038
4039 case mdb:
4040 {
4041 rtx tmp = gen_rtx_REG (DImode, PROLOGUE_TMP_REGNUM);
4042 rtx mem
4043 = gen_frame_mem (DImode,
4044 plus_constant (Pmode,
4045 stack_pointer_rtx, offset));
4046 rtx reg = gen_rtx_REG (DImode, regno);
4047 MEM_VOLATILE_P (mem) = volatile_p;
4048 emit_insn (gen_movdi (tmp, mem));
4049 emit_insn (gen_movdi (reg, tmp));
4050 /* Do not generate CFI if in interrupt handler. */
4051 if (!volatile_p)
4052 visium_add_cfa_restore_note (reg);
4053 }
4054 break;
4055
4056 case mdc:
4057 {
4058 rtx tmp = gen_rtx_REG (SImode, PROLOGUE_TMP_REGNUM);
4059 rtx mem
4060 = gen_frame_mem (SImode,
4061 plus_constant (Pmode,
4062 stack_pointer_rtx, offset));
4063 rtx reg = gen_rtx_REG (SImode, regno);
4064 MEM_VOLATILE_P (mem) = volatile_p;
4065 emit_insn (gen_movsi (tmp, mem));
4066 emit_insn (gen_movsi (reg, tmp));
4067 visium_add_cfa_restore_note (reg);
4068 }
4069 break;
4070
4071 case floating:
4072 {
4073 rtx tmp = gen_rtx_REG (SFmode, PROLOGUE_TMP_REGNUM);
4074 rtx mem
4075 = gen_frame_mem (SFmode,
4076 plus_constant (Pmode,
4077 stack_pointer_rtx, offset));
4078 rtx reg = gen_rtx_REG (SFmode, regno);
4079 MEM_VOLATILE_P (mem) = volatile_p;
4080 emit_insn (gen_movsf (tmp, mem));
4081 emit_insn (gen_movsf (reg, tmp));
4082 visium_add_cfa_restore_note (reg);
4083 }
4084 break;
4085
4086 default:
4087 break;
4088 }
4089
4090 offset += reg_type_size[reg_type];
4091 }
4092 }
4093
4094 /* If the interrupted context needs to be restored, precede the
4095 restores of r29 and r30 by a dsi. */
4096 if (r30_offset >= 0)
4097 {
4098 emit_insn (gen_dsi ());
4099 emit_move_insn (gen_rtx_REG (SImode, 30),
4100 gen_frame_mem (SImode,
4101 plus_constant (Pmode,
4102 stack_pointer_rtx,
4103 r30_offset)));
4104 emit_move_insn (gen_rtx_REG (SImode, 29),
4105 gen_frame_mem (SImode,
4106 plus_constant (Pmode,
4107 stack_pointer_rtx,
4108 r30_offset + 4)));
4109 }
4110
4111 /* Deallocate the stack space. */
4112 rtx insn = emit_frame_insn (gen_stack_pop (GEN_INT (dealloc)));
4113 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
4114 gen_rtx_SET (stack_pointer_rtx,
4115 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4116 GEN_INT (dealloc))));
4117 visium_add_queued_cfa_restore_notes (insn);
4118 }
4119
4120 /* This function generates the code for function exit. */
4121
4122 void
4123 visium_expand_epilogue (void)
4124 {
4125 const int save_area_size = current_frame_info.save_area_size;
4126 const int reg_size1 = current_frame_info.reg_size1;
4127 const int max_reg1 = current_frame_info.max_reg1;
4128 const int reg_size2 = current_frame_info.reg_size2;
4129 const int var_size = current_frame_info.var_size;
4130 const int restore_fp = current_frame_info.save_fp;
4131 const int restore_lr = current_frame_info.save_lr;
4132 const int lr_slot = current_frame_info.lr_slot;
4133 const int local_frame_offset
4134 = (restore_fp + restore_lr + lr_slot) * UNITS_PER_WORD;
4135 const int combine = current_frame_info.combine;
4136 int reg_size;
4137 int last_reg;
4138 int fsize;
4139
4140 /* Do not bother restoring the stack pointer if it hasn't been changed in
4141 the function since it was saved _after_ the allocation of the frame. */
4142 if (!crtl->sp_is_unchanging)
4143 emit_insn (gen_stack_restore ());
4144
4145 /* Restore the frame pointer if necessary. The usual code would be:
4146
4147 move.l sp,fp
4148 read.l fp,(sp)
4149
4150 but for the MCM this constitutes a stall/hazard so it is changed to:
4151
4152 move.l sp,fp
4153 read.l fp,(fp)
4154
4155 if the stack pointer has actually been restored. */
4156 if (restore_fp)
4157 {
4158 rtx src;
4159
4160 if (TARGET_MCM && !crtl->sp_is_unchanging)
4161 src = gen_frame_mem (SImode, hard_frame_pointer_rtx);
4162 else
4163 src = gen_frame_mem (SImode, stack_pointer_rtx);
4164
4165 rtx insn = emit_frame_insn (gen_movsi (hard_frame_pointer_rtx, src));
4166 add_reg_note (insn, REG_CFA_ADJUST_CFA,
4167 gen_rtx_SET (stack_pointer_rtx,
4168 hard_frame_pointer_rtx));
4169 visium_add_cfa_restore_note (hard_frame_pointer_rtx);
4170 }
4171
4172 /* Restore the link register if necessary. */
4173 if (restore_lr)
4174 {
4175 rtx mem = gen_frame_mem (SImode,
4176 plus_constant (Pmode,
4177 stack_pointer_rtx,
4178 restore_fp * UNITS_PER_WORD));
4179 rtx reg = gen_rtx_REG (SImode, LINK_REGNUM);
4180 emit_insn (gen_movsi (reg, mem));
4181 visium_add_cfa_restore_note (reg);
4182 }
4183
4184 /* If we have two blocks of registers, deal with the second one first. */
4185 if (reg_size2)
4186 {
4187 reg_size = reg_size2;
4188 last_reg = max_reg1 + 1;
4189 fsize = local_frame_offset + var_size + reg_size2;
4190 }
4191 else
4192 {
4193 reg_size = reg_size1;
4194 last_reg = 0;
4195 fsize = local_frame_offset + var_size + reg_size1 + save_area_size;
4196 }
4197
4198 /* If the variable allocation could be combined with register stacking,
4199 restore the (remaining) registers and fully deallocate now. */
4200 if (reg_size && combine)
4201 visium_restore_regs (fsize, local_frame_offset + var_size,
4202 FIRST_PSEUDO_REGISTER - 1, last_reg);
4203
4204 /* Otherwise deallocate the variables first. */
4205 else if (fsize)
4206 {
4207 const int pop_size = reg_size ? local_frame_offset + var_size : fsize;
4208 rtx insn;
4209
4210 if (pop_size > 65535)
4211 {
4212 rtx tmp = gen_rtx_REG (SImode, PROLOGUE_TMP_REGNUM);
4213 emit_move_insn (tmp, GEN_INT (pop_size));
4214 insn = emit_frame_insn (gen_stack_pop (tmp));
4215 }
4216 else
4217 insn = emit_frame_insn (gen_stack_pop (GEN_INT (pop_size)));
4218 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
4219 gen_rtx_SET (stack_pointer_rtx,
4220 gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4221 GEN_INT (pop_size))));
4222 visium_add_queued_cfa_restore_notes (insn);
4223 }
4224
4225 /* If the variable allocation couldn't be combined with register stacking,
4226 restore the (remaining) registers now and partially deallocate. */
4227 if (reg_size && !combine)
4228 visium_restore_regs (fsize - local_frame_offset - var_size, 0,
4229 FIRST_PSEUDO_REGISTER - 1, last_reg);
4230
4231 /* If the first block of registers has yet to be restored, do it now. */
4232 if (reg_size2)
4233 visium_restore_regs (reg_size1 + save_area_size, 0, max_reg1, 0);
4234
4235 /* If this is an exception return, make the necessary stack adjustment. */
4236 if (crtl->calls_eh_return)
4237 emit_insn (gen_stack_pop (EH_RETURN_STACKADJ_RTX));
4238 }
4239
4240 /* Return true if it is appropriate to emit `return' instructions in the
4241 body of a function. */
4242
4243 bool
4244 visium_can_use_return_insn_p (void)
4245 {
4246 return reload_completed
4247 && visium_frame_size == 0
4248 && !visium_interrupt_function_p ();
4249 }
4250
4251 /* Return the register class required for an intermediate register used to
4252 copy a register of RCLASS from/to X. If no such intermediate register is
4253 required, return NO_REGS. If more than one such intermediate register is
4254 required, describe the one that is closest in the copy chain to the reload
4255 register. */
4256
4257 static reg_class_t
4258 visium_secondary_reload (bool in_p ATTRIBUTE_UNUSED, rtx x,
4259 reg_class_t rclass,
4260 machine_mode mode ATTRIBUTE_UNUSED,
4261 secondary_reload_info *sri ATTRIBUTE_UNUSED)
4262 {
4263 int regno = true_regnum (x);
4264
4265 /* For MDB, MDC and FP_REGS, a general register is needed for a move to
4266 or from memory. */
4267 if (regno == -1 && (rclass == MDB || rclass == MDC || rclass == FP_REGS))
4268 return GENERAL_REGS;
4269
4270 /* Moves between MDB, MDC and FP_REGS also require a general register. */
4271 else if (((regno == R_MDB || regno == R_MDC) && rclass == FP_REGS)
4272 || (FP_REGISTER_P (regno) && (rclass == MDB || rclass == MDC)))
4273 return GENERAL_REGS;
4274
4275 /* Finally an (unlikely ?) move between MDB and MDC needs a general reg. */
4276 else if ((regno == R_MDB && rclass == MDC)
4277 || (rclass == MDB && regno == R_MDC))
4278 return GENERAL_REGS;
4279
4280 return NO_REGS;
4281 }
4282
4283 /* Return true if pseudos that have been assigned to registers of RCLASS
4284 would likely be spilled because registers of RCLASS are needed for
4285 spill registers. */
4286
4287 static bool
4288 visium_class_likely_spilled_p (reg_class_t rclass ATTRIBUTE_UNUSED)
4289 {
4290 /* Return false for classes R1, R2 and R3, which are intended to be used
4291 only in the source code in conjunction with block move instructions. */
4292 return false;
4293 }
4294
4295 /* Return the register number if OP is a REG or a SUBREG of a REG, and
4296 INVALID_REGNUM in all the other cases. */
4297
4298 unsigned int
4299 reg_or_subreg_regno (rtx op)
4300 {
4301 unsigned int regno;
4302
4303 if (GET_CODE (op) == REG)
4304 regno = REGNO (op);
4305 else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
4306 {
4307 if (REGNO (SUBREG_REG (op)) < FIRST_PSEUDO_REGISTER)
4308 regno = subreg_regno (op);
4309 else
4310 regno = REGNO (SUBREG_REG (op));
4311 }
4312 else
4313 regno = INVALID_REGNUM;
4314
4315 return regno;
4316 }
4317
4318 /* Implement TARGET_CAN_CHANGE_MODE_CLASS.
4319
4320 It's not obvious from the documentation of the hook that MDB cannot
4321 change mode. However difficulties arise from expressions of the form
4322
4323 (subreg:SI (reg:DI R_MDB) 0)
4324
4325 There is no way to convert that reference to a single machine
4326 register and, without the following definition, reload will quietly
4327 convert it to
4328
4329 (reg:SI R_MDB). */
4330
4331 static bool
4332 visium_can_change_mode_class (machine_mode from, machine_mode to,
4333 reg_class_t rclass)
4334 {
4335 return (rclass != MDB || GET_MODE_SIZE (from) == GET_MODE_SIZE (to));
4336 }
4337
4338 #include "gt-visium.h"