RISC-V: Fix %lo overflow with BLKmode references.
[official-gcc.git] / gcc / config / riscv / riscv.c
blobd8446f82b9622d054e2fcffe0fb118a1ea703758
1 /* Subroutines used for code generation for RISC-V.
2 Copyright (C) 2011-2019 Free Software Foundation, Inc.
3 Contributed by Andrew Waterman (andrew@sifive.com).
4 Based on MIPS target for GNU compiler.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #define IN_TARGET_CODE 1
24 #define INCLUDE_STRING
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "regs.h"
31 #include "insn-config.h"
32 #include "insn-attr.h"
33 #include "recog.h"
34 #include "output.h"
35 #include "alias.h"
36 #include "tree.h"
37 #include "stringpool.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "stor-layout.h"
41 #include "calls.h"
42 #include "function.h"
43 #include "explow.h"
44 #include "memmodel.h"
45 #include "emit-rtl.h"
46 #include "reload.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "basic-block.h"
51 #include "expr.h"
52 #include "optabs.h"
53 #include "bitmap.h"
54 #include "df.h"
55 #include "diagnostic.h"
56 #include "builtins.h"
57 #include "predict.h"
59 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
60 #define UNSPEC_ADDRESS_P(X) \
61 (GET_CODE (X) == UNSPEC \
62 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
63 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
65 /* Extract the symbol or label from UNSPEC wrapper X. */
66 #define UNSPEC_ADDRESS(X) \
67 XVECEXP (X, 0, 0)
69 /* Extract the symbol type from UNSPEC wrapper X. */
70 #define UNSPEC_ADDRESS_TYPE(X) \
71 ((enum riscv_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
73 /* True if bit BIT is set in VALUE. */
74 #define BITSET_P(VALUE, BIT) (((VALUE) & (1ULL << (BIT))) != 0)
76 /* Classifies an address.
78 ADDRESS_REG
79 A natural register + offset address. The register satisfies
80 riscv_valid_base_register_p and the offset is a const_arith_operand.
82 ADDRESS_LO_SUM
83 A LO_SUM rtx. The first operand is a valid base register and
84 the second operand is a symbolic address.
86 ADDRESS_CONST_INT
87 A signed 16-bit constant address.
89 ADDRESS_SYMBOLIC:
90 A constant symbolic address. */
91 enum riscv_address_type {
92 ADDRESS_REG,
93 ADDRESS_LO_SUM,
94 ADDRESS_CONST_INT,
95 ADDRESS_SYMBOLIC
98 /* Information about a function's frame layout. */
99 struct GTY(()) riscv_frame_info {
100 /* The size of the frame in bytes. */
101 HOST_WIDE_INT total_size;
103 /* Bit X is set if the function saves or restores GPR X. */
104 unsigned int mask;
106 /* Likewise FPR X. */
107 unsigned int fmask;
109 /* How much the GPR save/restore routines adjust sp (or 0 if unused). */
110 unsigned save_libcall_adjustment;
112 /* Offsets of fixed-point and floating-point save areas from frame bottom */
113 HOST_WIDE_INT gp_sp_offset;
114 HOST_WIDE_INT fp_sp_offset;
116 /* Offset of virtual frame pointer from stack pointer/frame bottom */
117 HOST_WIDE_INT frame_pointer_offset;
119 /* Offset of hard frame pointer from stack pointer/frame bottom */
120 HOST_WIDE_INT hard_frame_pointer_offset;
122 /* The offset of arg_pointer_rtx from the bottom of the frame. */
123 HOST_WIDE_INT arg_pointer_offset;
126 enum riscv_privilege_levels {
127 UNKNOWN_MODE, USER_MODE, SUPERVISOR_MODE, MACHINE_MODE
130 struct GTY(()) machine_function {
131 /* The number of extra stack bytes taken up by register varargs.
132 This area is allocated by the callee at the very top of the frame. */
133 int varargs_size;
135 /* True if current function is a naked function. */
136 bool naked_p;
138 /* True if current function is an interrupt function. */
139 bool interrupt_handler_p;
140 /* For an interrupt handler, indicates the privilege level. */
141 enum riscv_privilege_levels interrupt_mode;
143 /* True if attributes on current function have been checked. */
144 bool attributes_checked_p;
146 /* The current frame information, calculated by riscv_compute_frame_info. */
147 struct riscv_frame_info frame;
150 /* Information about a single argument. */
151 struct riscv_arg_info {
152 /* True if the argument is at least partially passed on the stack. */
153 bool stack_p;
155 /* The number of integer registers allocated to this argument. */
156 unsigned int num_gprs;
158 /* The offset of the first register used, provided num_gprs is nonzero.
159 If passed entirely on the stack, the value is MAX_ARGS_IN_REGISTERS. */
160 unsigned int gpr_offset;
162 /* The number of floating-point registers allocated to this argument. */
163 unsigned int num_fprs;
165 /* The offset of the first register used, provided num_fprs is nonzero. */
166 unsigned int fpr_offset;
169 /* Information about an address described by riscv_address_type.
171 ADDRESS_CONST_INT
172 No fields are used.
174 ADDRESS_REG
175 REG is the base register and OFFSET is the constant offset.
177 ADDRESS_LO_SUM
178 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
179 is the type of symbol it references.
181 ADDRESS_SYMBOLIC
182 SYMBOL_TYPE is the type of symbol that the address references. */
183 struct riscv_address_info {
184 enum riscv_address_type type;
185 rtx reg;
186 rtx offset;
187 enum riscv_symbol_type symbol_type;
190 /* One stage in a constant building sequence. These sequences have
191 the form:
193 A = VALUE[0]
194 A = A CODE[1] VALUE[1]
195 A = A CODE[2] VALUE[2]
198 where A is an accumulator, each CODE[i] is a binary rtl operation
199 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
200 struct riscv_integer_op {
201 enum rtx_code code;
202 unsigned HOST_WIDE_INT value;
205 /* The largest number of operations needed to load an integer constant.
206 The worst case is LUI, ADDI, SLLI, ADDI, SLLI, ADDI, SLLI, ADDI. */
207 #define RISCV_MAX_INTEGER_OPS 8
209 /* Costs of various operations on the different architectures. */
211 struct riscv_tune_info
213 unsigned short fp_add[2];
214 unsigned short fp_mul[2];
215 unsigned short fp_div[2];
216 unsigned short int_mul[2];
217 unsigned short int_div[2];
218 unsigned short issue_rate;
219 unsigned short branch_cost;
220 unsigned short memory_cost;
221 bool slow_unaligned_access;
224 /* Information about one CPU we know about. */
225 struct riscv_cpu_info {
226 /* This CPU's canonical name. */
227 const char *name;
229 /* Tuning parameters for this CPU. */
230 const struct riscv_tune_info *tune_info;
233 /* Global variables for machine-dependent things. */
235 /* Whether unaligned accesses execute very slowly. */
236 bool riscv_slow_unaligned_access_p;
238 /* Stack alignment to assume/maintain. */
239 unsigned riscv_stack_boundary;
241 /* If non-zero, this is an offset to be added to SP to redefine the CFA
242 when restoring the FP register from the stack. Only valid when generating
243 the epilogue. */
244 static int epilogue_cfa_sp_offset;
246 /* Which tuning parameters to use. */
247 static const struct riscv_tune_info *tune_info;
249 /* Index R is the smallest register class that contains register R. */
250 const enum reg_class riscv_regno_to_class[FIRST_PSEUDO_REGISTER] = {
251 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
252 GR_REGS, GR_REGS, SIBCALL_REGS, SIBCALL_REGS,
253 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
254 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
255 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
256 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
257 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
258 SIBCALL_REGS, SIBCALL_REGS, SIBCALL_REGS, SIBCALL_REGS,
259 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
260 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
261 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
262 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
263 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
264 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
265 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
266 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
267 FRAME_REGS, FRAME_REGS,
270 /* Costs to use when optimizing for rocket. */
271 static const struct riscv_tune_info rocket_tune_info = {
272 {COSTS_N_INSNS (4), COSTS_N_INSNS (5)}, /* fp_add */
273 {COSTS_N_INSNS (4), COSTS_N_INSNS (5)}, /* fp_mul */
274 {COSTS_N_INSNS (20), COSTS_N_INSNS (20)}, /* fp_div */
275 {COSTS_N_INSNS (4), COSTS_N_INSNS (4)}, /* int_mul */
276 {COSTS_N_INSNS (6), COSTS_N_INSNS (6)}, /* int_div */
277 1, /* issue_rate */
278 3, /* branch_cost */
279 5, /* memory_cost */
280 true, /* slow_unaligned_access */
283 /* Costs to use when optimizing for size. */
284 static const struct riscv_tune_info optimize_size_tune_info = {
285 {COSTS_N_INSNS (1), COSTS_N_INSNS (1)}, /* fp_add */
286 {COSTS_N_INSNS (1), COSTS_N_INSNS (1)}, /* fp_mul */
287 {COSTS_N_INSNS (1), COSTS_N_INSNS (1)}, /* fp_div */
288 {COSTS_N_INSNS (1), COSTS_N_INSNS (1)}, /* int_mul */
289 {COSTS_N_INSNS (1), COSTS_N_INSNS (1)}, /* int_div */
290 1, /* issue_rate */
291 1, /* branch_cost */
292 2, /* memory_cost */
293 false, /* slow_unaligned_access */
296 static tree riscv_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
297 static tree riscv_handle_type_attribute (tree *, tree, tree, int, bool *);
299 /* Defining target-specific uses of __attribute__. */
300 static const struct attribute_spec riscv_attribute_table[] =
302 /* Syntax: { name, min_len, max_len, decl_required, type_required,
303 function_type_required, affects_type_identity, handler,
304 exclude } */
306 /* The attribute telling no prologue/epilogue. */
307 { "naked", 0, 0, true, false, false, false,
308 riscv_handle_fndecl_attribute, NULL },
309 /* This attribute generates prologue/epilogue for interrupt handlers. */
310 { "interrupt", 0, 1, false, true, true, false,
311 riscv_handle_type_attribute, NULL },
313 /* The last attribute spec is set to be NULL. */
314 { NULL, 0, 0, false, false, false, false, NULL, NULL }
317 /* A table describing all the processors GCC knows about. */
318 static const struct riscv_cpu_info riscv_cpu_info_table[] = {
319 { "rocket", &rocket_tune_info },
320 { "size", &optimize_size_tune_info },
323 /* Return the riscv_cpu_info entry for the given name string. */
325 static const struct riscv_cpu_info *
326 riscv_parse_cpu (const char *cpu_string)
328 for (unsigned i = 0; i < ARRAY_SIZE (riscv_cpu_info_table); i++)
329 if (strcmp (riscv_cpu_info_table[i].name, cpu_string) == 0)
330 return riscv_cpu_info_table + i;
332 error ("unknown cpu %qs for %<-mtune%>", cpu_string);
333 return riscv_cpu_info_table;
336 /* Helper function for riscv_build_integer; arguments are as for
337 riscv_build_integer. */
339 static int
340 riscv_build_integer_1 (struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS],
341 HOST_WIDE_INT value, machine_mode mode)
343 HOST_WIDE_INT low_part = CONST_LOW_PART (value);
344 int cost = RISCV_MAX_INTEGER_OPS + 1, alt_cost;
345 struct riscv_integer_op alt_codes[RISCV_MAX_INTEGER_OPS];
347 if (SMALL_OPERAND (value) || LUI_OPERAND (value))
349 /* Simply ADDI or LUI. */
350 codes[0].code = UNKNOWN;
351 codes[0].value = value;
352 return 1;
355 /* End with ADDI. When constructing HImode constants, do not generate any
356 intermediate value that is not itself a valid HImode constant. The
357 XORI case below will handle those remaining HImode constants. */
358 if (low_part != 0
359 && (mode != HImode
360 || value - low_part <= ((1 << (GET_MODE_BITSIZE (HImode) - 1)) - 1)))
362 alt_cost = 1 + riscv_build_integer_1 (alt_codes, value - low_part, mode);
363 if (alt_cost < cost)
365 alt_codes[alt_cost-1].code = PLUS;
366 alt_codes[alt_cost-1].value = low_part;
367 memcpy (codes, alt_codes, sizeof (alt_codes));
368 cost = alt_cost;
372 /* End with XORI. */
373 if (cost > 2 && (low_part < 0 || mode == HImode))
375 alt_cost = 1 + riscv_build_integer_1 (alt_codes, value ^ low_part, mode);
376 if (alt_cost < cost)
378 alt_codes[alt_cost-1].code = XOR;
379 alt_codes[alt_cost-1].value = low_part;
380 memcpy (codes, alt_codes, sizeof (alt_codes));
381 cost = alt_cost;
385 /* Eliminate trailing zeros and end with SLLI. */
386 if (cost > 2 && (value & 1) == 0)
388 int shift = ctz_hwi (value);
389 unsigned HOST_WIDE_INT x = value;
390 x = sext_hwi (x >> shift, HOST_BITS_PER_WIDE_INT - shift);
392 /* Don't eliminate the lower 12 bits if LUI might apply. */
393 if (shift > IMM_BITS && !SMALL_OPERAND (x) && LUI_OPERAND (x << IMM_BITS))
394 shift -= IMM_BITS, x <<= IMM_BITS;
396 alt_cost = 1 + riscv_build_integer_1 (alt_codes, x, mode);
397 if (alt_cost < cost)
399 alt_codes[alt_cost-1].code = ASHIFT;
400 alt_codes[alt_cost-1].value = shift;
401 memcpy (codes, alt_codes, sizeof (alt_codes));
402 cost = alt_cost;
406 gcc_assert (cost <= RISCV_MAX_INTEGER_OPS);
407 return cost;
410 /* Fill CODES with a sequence of rtl operations to load VALUE.
411 Return the number of operations needed. */
413 static int
414 riscv_build_integer (struct riscv_integer_op *codes, HOST_WIDE_INT value,
415 machine_mode mode)
417 int cost = riscv_build_integer_1 (codes, value, mode);
419 /* Eliminate leading zeros and end with SRLI. */
420 if (value > 0 && cost > 2)
422 struct riscv_integer_op alt_codes[RISCV_MAX_INTEGER_OPS];
423 int alt_cost, shift = clz_hwi (value);
424 HOST_WIDE_INT shifted_val;
426 /* Try filling trailing bits with 1s. */
427 shifted_val = (value << shift) | ((((HOST_WIDE_INT) 1) << shift) - 1);
428 alt_cost = 1 + riscv_build_integer_1 (alt_codes, shifted_val, mode);
429 if (alt_cost < cost)
431 alt_codes[alt_cost-1].code = LSHIFTRT;
432 alt_codes[alt_cost-1].value = shift;
433 memcpy (codes, alt_codes, sizeof (alt_codes));
434 cost = alt_cost;
437 /* Try filling trailing bits with 0s. */
438 shifted_val = value << shift;
439 alt_cost = 1 + riscv_build_integer_1 (alt_codes, shifted_val, mode);
440 if (alt_cost < cost)
442 alt_codes[alt_cost-1].code = LSHIFTRT;
443 alt_codes[alt_cost-1].value = shift;
444 memcpy (codes, alt_codes, sizeof (alt_codes));
445 cost = alt_cost;
449 return cost;
452 /* Return the cost of constructing VAL in the event that a scratch
453 register is available. */
455 static int
456 riscv_split_integer_cost (HOST_WIDE_INT val)
458 int cost;
459 unsigned HOST_WIDE_INT loval = sext_hwi (val, 32);
460 unsigned HOST_WIDE_INT hival = sext_hwi ((val - loval) >> 32, 32);
461 struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
463 cost = 2 + riscv_build_integer (codes, loval, VOIDmode);
464 if (loval != hival)
465 cost += riscv_build_integer (codes, hival, VOIDmode);
467 return cost;
470 /* Return the cost of constructing the integer constant VAL. */
472 static int
473 riscv_integer_cost (HOST_WIDE_INT val)
475 struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
476 return MIN (riscv_build_integer (codes, val, VOIDmode),
477 riscv_split_integer_cost (val));
480 /* Try to split a 64b integer into 32b parts, then reassemble. */
482 static rtx
483 riscv_split_integer (HOST_WIDE_INT val, machine_mode mode)
485 unsigned HOST_WIDE_INT loval = sext_hwi (val, 32);
486 unsigned HOST_WIDE_INT hival = sext_hwi ((val - loval) >> 32, 32);
487 rtx hi = gen_reg_rtx (mode), lo = gen_reg_rtx (mode);
489 riscv_move_integer (hi, hi, hival);
490 riscv_move_integer (lo, lo, loval);
492 hi = gen_rtx_fmt_ee (ASHIFT, mode, hi, GEN_INT (32));
493 hi = force_reg (mode, hi);
495 return gen_rtx_fmt_ee (PLUS, mode, hi, lo);
498 /* Return true if X is a thread-local symbol. */
500 static bool
501 riscv_tls_symbol_p (const_rtx x)
503 return SYMBOL_REF_P (x) && SYMBOL_REF_TLS_MODEL (x) != 0;
506 /* Return true if symbol X binds locally. */
508 static bool
509 riscv_symbol_binds_local_p (const_rtx x)
511 if (SYMBOL_REF_P (x))
512 return (SYMBOL_REF_DECL (x)
513 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
514 : SYMBOL_REF_LOCAL_P (x));
515 else
516 return false;
519 /* Return the method that should be used to access SYMBOL_REF or
520 LABEL_REF X. */
522 static enum riscv_symbol_type
523 riscv_classify_symbol (const_rtx x)
525 if (riscv_tls_symbol_p (x))
526 return SYMBOL_TLS;
528 if (GET_CODE (x) == SYMBOL_REF && flag_pic && !riscv_symbol_binds_local_p (x))
529 return SYMBOL_GOT_DISP;
531 return riscv_cmodel == CM_MEDLOW ? SYMBOL_ABSOLUTE : SYMBOL_PCREL;
534 /* Classify the base of symbolic expression X. */
536 enum riscv_symbol_type
537 riscv_classify_symbolic_expression (rtx x)
539 rtx offset;
541 split_const (x, &x, &offset);
542 if (UNSPEC_ADDRESS_P (x))
543 return UNSPEC_ADDRESS_TYPE (x);
545 return riscv_classify_symbol (x);
548 /* Return true if X is a symbolic constant. If it is, store the type of
549 the symbol in *SYMBOL_TYPE. */
551 bool
552 riscv_symbolic_constant_p (rtx x, enum riscv_symbol_type *symbol_type)
554 rtx offset;
556 split_const (x, &x, &offset);
557 if (UNSPEC_ADDRESS_P (x))
559 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
560 x = UNSPEC_ADDRESS (x);
562 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
563 *symbol_type = riscv_classify_symbol (x);
564 else
565 return false;
567 if (offset == const0_rtx)
568 return true;
570 /* Nonzero offsets are only valid for references that don't use the GOT. */
571 switch (*symbol_type)
573 case SYMBOL_ABSOLUTE:
574 case SYMBOL_PCREL:
575 case SYMBOL_TLS_LE:
576 /* GAS rejects offsets outside the range [-2^31, 2^31-1]. */
577 return sext_hwi (INTVAL (offset), 32) == INTVAL (offset);
579 default:
580 return false;
584 /* Returns the number of instructions necessary to reference a symbol. */
586 static int riscv_symbol_insns (enum riscv_symbol_type type)
588 switch (type)
590 case SYMBOL_TLS: return 0; /* Depends on the TLS model. */
591 case SYMBOL_ABSOLUTE: return 2; /* LUI + the reference. */
592 case SYMBOL_PCREL: return 2; /* AUIPC + the reference. */
593 case SYMBOL_TLS_LE: return 3; /* LUI + ADD TP + the reference. */
594 case SYMBOL_GOT_DISP: return 3; /* AUIPC + LD GOT + the reference. */
595 default: gcc_unreachable ();
599 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
601 static bool
602 riscv_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
604 return riscv_const_insns (x) > 0;
607 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
609 static bool
610 riscv_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
612 enum riscv_symbol_type type;
613 rtx base, offset;
615 /* There is no assembler syntax for expressing an address-sized
616 high part. */
617 if (GET_CODE (x) == HIGH)
618 return true;
620 split_const (x, &base, &offset);
621 if (riscv_symbolic_constant_p (base, &type))
623 /* As an optimization, don't spill symbolic constants that are as
624 cheap to rematerialize as to access in the constant pool. */
625 if (SMALL_OPERAND (INTVAL (offset)) && riscv_symbol_insns (type) > 0)
626 return true;
628 /* As an optimization, avoid needlessly generate dynamic relocations. */
629 if (flag_pic)
630 return true;
633 /* TLS symbols must be computed by riscv_legitimize_move. */
634 if (tls_referenced_p (x))
635 return true;
637 return false;
640 /* Return true if register REGNO is a valid base register for mode MODE.
641 STRICT_P is true if REG_OK_STRICT is in effect. */
644 riscv_regno_mode_ok_for_base_p (int regno,
645 machine_mode mode ATTRIBUTE_UNUSED,
646 bool strict_p)
648 if (!HARD_REGISTER_NUM_P (regno))
650 if (!strict_p)
651 return true;
652 regno = reg_renumber[regno];
655 /* These fake registers will be eliminated to either the stack or
656 hard frame pointer, both of which are usually valid base registers.
657 Reload deals with the cases where the eliminated form isn't valid. */
658 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
659 return true;
661 return GP_REG_P (regno);
664 /* Return true if X is a valid base register for mode MODE.
665 STRICT_P is true if REG_OK_STRICT is in effect. */
667 static bool
668 riscv_valid_base_register_p (rtx x, machine_mode mode, bool strict_p)
670 if (!strict_p && GET_CODE (x) == SUBREG)
671 x = SUBREG_REG (x);
673 return (REG_P (x)
674 && riscv_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
677 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
678 can address a value of mode MODE. */
680 static bool
681 riscv_valid_offset_p (rtx x, machine_mode mode)
683 /* Check that X is a signed 12-bit number. */
684 if (!const_arith_operand (x, Pmode))
685 return false;
687 /* We may need to split multiword moves, so make sure that every word
688 is accessible. */
689 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
690 && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
691 return false;
693 return true;
696 /* Should a symbol of type SYMBOL_TYPE should be split in two? */
698 bool
699 riscv_split_symbol_type (enum riscv_symbol_type symbol_type)
701 if (symbol_type == SYMBOL_TLS_LE)
702 return true;
704 if (!TARGET_EXPLICIT_RELOCS)
705 return false;
707 return symbol_type == SYMBOL_ABSOLUTE || symbol_type == SYMBOL_PCREL;
710 /* Return true if a LO_SUM can address a value of mode MODE when the
711 LO_SUM symbol has type SYM_TYPE. X is the LO_SUM second operand, which
712 is used when the mode is BLKmode. */
714 static bool
715 riscv_valid_lo_sum_p (enum riscv_symbol_type sym_type, machine_mode mode,
716 rtx x)
718 int align, size;
720 /* Check that symbols of type SYMBOL_TYPE can be used to access values
721 of mode MODE. */
722 if (riscv_symbol_insns (sym_type) == 0)
723 return false;
725 /* Check that there is a known low-part relocation. */
726 if (!riscv_split_symbol_type (sym_type))
727 return false;
729 /* We can't tell size or alignment when we have BLKmode, so try extracing a
730 decl from the symbol if possible. */
731 if (mode == BLKmode)
733 rtx offset;
735 /* Extract the symbol from the LO_SUM operand, if any. */
736 split_const (x, &x, &offset);
738 /* Might be a CODE_LABEL. We can compute align but not size for that,
739 so don't bother trying to handle it. */
740 if (!SYMBOL_REF_P (x))
741 return false;
743 /* Use worst case assumptions if we don't have a SYMBOL_REF_DECL. */
744 align = (SYMBOL_REF_DECL (x)
745 ? DECL_ALIGN (SYMBOL_REF_DECL (x))
746 : 1);
747 size = (SYMBOL_REF_DECL (x) && DECL_SIZE (SYMBOL_REF_DECL (x))
748 ? tree_to_uhwi (DECL_SIZE (SYMBOL_REF_DECL (x)))
749 : 2*BITS_PER_WORD);
751 else
753 align = GET_MODE_ALIGNMENT (mode);
754 size = GET_MODE_BITSIZE (mode);
757 /* We may need to split multiword moves, so make sure that each word
758 can be accessed without inducing a carry. */
759 if (size > BITS_PER_WORD
760 && (!TARGET_STRICT_ALIGN || size > align))
761 return false;
763 return true;
766 /* Return true if X is a valid address for machine mode MODE. If it is,
767 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
768 effect. */
770 static bool
771 riscv_classify_address (struct riscv_address_info *info, rtx x,
772 machine_mode mode, bool strict_p)
774 switch (GET_CODE (x))
776 case REG:
777 case SUBREG:
778 info->type = ADDRESS_REG;
779 info->reg = x;
780 info->offset = const0_rtx;
781 return riscv_valid_base_register_p (info->reg, mode, strict_p);
783 case PLUS:
784 info->type = ADDRESS_REG;
785 info->reg = XEXP (x, 0);
786 info->offset = XEXP (x, 1);
787 return (riscv_valid_base_register_p (info->reg, mode, strict_p)
788 && riscv_valid_offset_p (info->offset, mode));
790 case LO_SUM:
791 info->type = ADDRESS_LO_SUM;
792 info->reg = XEXP (x, 0);
793 info->offset = XEXP (x, 1);
794 /* We have to trust the creator of the LO_SUM to do something vaguely
795 sane. Target-independent code that creates a LO_SUM should also
796 create and verify the matching HIGH. Target-independent code that
797 adds an offset to a LO_SUM must prove that the offset will not
798 induce a carry. Failure to do either of these things would be
799 a bug, and we are not required to check for it here. The RISC-V
800 backend itself should only create LO_SUMs for valid symbolic
801 constants, with the high part being either a HIGH or a copy
802 of _gp. */
803 info->symbol_type
804 = riscv_classify_symbolic_expression (info->offset);
805 return (riscv_valid_base_register_p (info->reg, mode, strict_p)
806 && riscv_valid_lo_sum_p (info->symbol_type, mode, info->offset));
808 case CONST_INT:
809 /* Small-integer addresses don't occur very often, but they
810 are legitimate if x0 is a valid base register. */
811 info->type = ADDRESS_CONST_INT;
812 return SMALL_OPERAND (INTVAL (x));
814 default:
815 return false;
819 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
821 static bool
822 riscv_legitimate_address_p (machine_mode mode, rtx x, bool strict_p)
824 struct riscv_address_info addr;
826 return riscv_classify_address (&addr, x, mode, strict_p);
829 /* Return the number of instructions needed to load or store a value
830 of mode MODE at address X. Return 0 if X isn't valid for MODE.
831 Assume that multiword moves may need to be split into word moves
832 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
833 enough. */
836 riscv_address_insns (rtx x, machine_mode mode, bool might_split_p)
838 struct riscv_address_info addr;
839 int n = 1;
841 if (!riscv_classify_address (&addr, x, mode, false))
843 /* This could be a pattern from the pic.md file. In which case we want
844 this address to always have a cost of 3 to make it as expensive as the
845 most expensive symbol. This prevents constant propagation from
846 preferring symbols over register plus offset. */
847 return 3;
850 /* BLKmode is used for single unaligned loads and stores and should
851 not count as a multiword mode. */
852 if (mode != BLKmode && might_split_p)
853 n += (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
855 if (addr.type == ADDRESS_LO_SUM)
856 n += riscv_symbol_insns (addr.symbol_type) - 1;
858 return n;
861 /* Return the number of instructions needed to load constant X.
862 Return 0 if X isn't a valid constant. */
865 riscv_const_insns (rtx x)
867 enum riscv_symbol_type symbol_type;
868 rtx offset;
870 switch (GET_CODE (x))
872 case HIGH:
873 if (!riscv_symbolic_constant_p (XEXP (x, 0), &symbol_type)
874 || !riscv_split_symbol_type (symbol_type))
875 return 0;
877 /* This is simply an LUI. */
878 return 1;
880 case CONST_INT:
882 int cost = riscv_integer_cost (INTVAL (x));
883 /* Force complicated constants to memory. */
884 return cost < 4 ? cost : 0;
887 case CONST_DOUBLE:
888 case CONST_VECTOR:
889 /* We can use x0 to load floating-point zero. */
890 return x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
892 case CONST:
893 /* See if we can refer to X directly. */
894 if (riscv_symbolic_constant_p (x, &symbol_type))
895 return riscv_symbol_insns (symbol_type);
897 /* Otherwise try splitting the constant into a base and offset. */
898 split_const (x, &x, &offset);
899 if (offset != 0)
901 int n = riscv_const_insns (x);
902 if (n != 0)
903 return n + riscv_integer_cost (INTVAL (offset));
905 return 0;
907 case SYMBOL_REF:
908 case LABEL_REF:
909 return riscv_symbol_insns (riscv_classify_symbol (x));
911 default:
912 return 0;
916 /* X is a doubleword constant that can be handled by splitting it into
917 two words and loading each word separately. Return the number of
918 instructions required to do this. */
921 riscv_split_const_insns (rtx x)
923 unsigned int low, high;
925 low = riscv_const_insns (riscv_subword (x, false));
926 high = riscv_const_insns (riscv_subword (x, true));
927 gcc_assert (low > 0 && high > 0);
928 return low + high;
931 /* Return the number of instructions needed to implement INSN,
932 given that it loads from or stores to MEM. */
935 riscv_load_store_insns (rtx mem, rtx_insn *insn)
937 machine_mode mode;
938 bool might_split_p;
939 rtx set;
941 gcc_assert (MEM_P (mem));
942 mode = GET_MODE (mem);
944 /* Try to prove that INSN does not need to be split. */
945 might_split_p = true;
946 if (GET_MODE_BITSIZE (mode) <= 32)
947 might_split_p = false;
948 else if (GET_MODE_BITSIZE (mode) == 64)
950 set = single_set (insn);
951 if (set && !riscv_split_64bit_move_p (SET_DEST (set), SET_SRC (set)))
952 might_split_p = false;
955 return riscv_address_insns (XEXP (mem, 0), mode, might_split_p);
958 /* Emit a move from SRC to DEST. Assume that the move expanders can
959 handle all moves if !can_create_pseudo_p (). The distinction is
960 important because, unlike emit_move_insn, the move expanders know
961 how to force Pmode objects into the constant pool even when the
962 constant pool address is not itself legitimate. */
965 riscv_emit_move (rtx dest, rtx src)
967 return (can_create_pseudo_p ()
968 ? emit_move_insn (dest, src)
969 : emit_move_insn_1 (dest, src));
972 /* Emit an instruction of the form (set TARGET SRC). */
974 static rtx
975 riscv_emit_set (rtx target, rtx src)
977 emit_insn (gen_rtx_SET (target, src));
978 return target;
981 /* Emit an instruction of the form (set DEST (CODE X Y)). */
983 static rtx
984 riscv_emit_binary (enum rtx_code code, rtx dest, rtx x, rtx y)
986 return riscv_emit_set (dest, gen_rtx_fmt_ee (code, GET_MODE (dest), x, y));
989 /* Compute (CODE X Y) and store the result in a new register
990 of mode MODE. Return that new register. */
992 static rtx
993 riscv_force_binary (machine_mode mode, enum rtx_code code, rtx x, rtx y)
995 return riscv_emit_binary (code, gen_reg_rtx (mode), x, y);
998 /* Copy VALUE to a register and return that register. If new pseudos
999 are allowed, copy it into a new register, otherwise use DEST. */
1001 static rtx
1002 riscv_force_temporary (rtx dest, rtx value)
1004 if (can_create_pseudo_p ())
1005 return force_reg (Pmode, value);
1006 else
1008 riscv_emit_move (dest, value);
1009 return dest;
1013 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
1014 then add CONST_INT OFFSET to the result. */
1016 static rtx
1017 riscv_unspec_address_offset (rtx base, rtx offset,
1018 enum riscv_symbol_type symbol_type)
1020 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
1021 UNSPEC_ADDRESS_FIRST + symbol_type);
1022 if (offset != const0_rtx)
1023 base = gen_rtx_PLUS (Pmode, base, offset);
1024 return gen_rtx_CONST (Pmode, base);
1027 /* Return an UNSPEC address with underlying address ADDRESS and symbol
1028 type SYMBOL_TYPE. */
1031 riscv_unspec_address (rtx address, enum riscv_symbol_type symbol_type)
1033 rtx base, offset;
1035 split_const (address, &base, &offset);
1036 return riscv_unspec_address_offset (base, offset, symbol_type);
1039 /* If OP is an UNSPEC address, return the address to which it refers,
1040 otherwise return OP itself. */
1042 static rtx
1043 riscv_strip_unspec_address (rtx op)
1045 rtx base, offset;
1047 split_const (op, &base, &offset);
1048 if (UNSPEC_ADDRESS_P (base))
1049 op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
1050 return op;
1053 /* If riscv_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
1054 high part to BASE and return the result. Just return BASE otherwise.
1055 TEMP is as for riscv_force_temporary.
1057 The returned expression can be used as the first operand to a LO_SUM. */
1059 static rtx
1060 riscv_unspec_offset_high (rtx temp, rtx addr, enum riscv_symbol_type symbol_type)
1062 addr = gen_rtx_HIGH (Pmode, riscv_unspec_address (addr, symbol_type));
1063 return riscv_force_temporary (temp, addr);
1066 /* Load an entry from the GOT for a TLS GD access. */
1068 static rtx riscv_got_load_tls_gd (rtx dest, rtx sym)
1070 if (Pmode == DImode)
1071 return gen_got_load_tls_gddi (dest, sym);
1072 else
1073 return gen_got_load_tls_gdsi (dest, sym);
1076 /* Load an entry from the GOT for a TLS IE access. */
1078 static rtx riscv_got_load_tls_ie (rtx dest, rtx sym)
1080 if (Pmode == DImode)
1081 return gen_got_load_tls_iedi (dest, sym);
1082 else
1083 return gen_got_load_tls_iesi (dest, sym);
1086 /* Add in the thread pointer for a TLS LE access. */
1088 static rtx riscv_tls_add_tp_le (rtx dest, rtx base, rtx sym)
1090 rtx tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
1091 if (Pmode == DImode)
1092 return gen_tls_add_tp_ledi (dest, base, tp, sym);
1093 else
1094 return gen_tls_add_tp_lesi (dest, base, tp, sym);
1097 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
1098 it appears in a MEM of that mode. Return true if ADDR is a legitimate
1099 constant in that context and can be split into high and low parts.
1100 If so, and if LOW_OUT is nonnull, emit the high part and store the
1101 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
1103 TEMP is as for riscv_force_temporary and is used to load the high
1104 part into a register.
1106 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
1107 a legitimize SET_SRC for an .md pattern, otherwise the low part
1108 is guaranteed to be a legitimate address for mode MODE. */
1110 bool
1111 riscv_split_symbol (rtx temp, rtx addr, machine_mode mode, rtx *low_out)
1113 enum riscv_symbol_type symbol_type;
1115 if ((GET_CODE (addr) == HIGH && mode == MAX_MACHINE_MODE)
1116 || !riscv_symbolic_constant_p (addr, &symbol_type)
1117 || riscv_symbol_insns (symbol_type) == 0
1118 || !riscv_split_symbol_type (symbol_type))
1119 return false;
1121 if (low_out)
1122 switch (symbol_type)
1124 case SYMBOL_ABSOLUTE:
1126 rtx high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
1127 high = riscv_force_temporary (temp, high);
1128 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
1130 break;
1132 case SYMBOL_PCREL:
1134 static unsigned seqno;
1135 char buf[32];
1136 rtx label;
1138 ssize_t bytes = snprintf (buf, sizeof (buf), ".LA%u", seqno);
1139 gcc_assert ((size_t) bytes < sizeof (buf));
1141 label = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1142 SYMBOL_REF_FLAGS (label) |= SYMBOL_FLAG_LOCAL;
1143 /* ??? Ugly hack to make weak symbols work. May need to change the
1144 RTL for the auipc and/or low patterns to get a better fix for
1145 this. */
1146 if (! nonzero_address_p (addr))
1147 SYMBOL_REF_WEAK (label) = 1;
1149 if (temp == NULL)
1150 temp = gen_reg_rtx (Pmode);
1152 if (Pmode == DImode)
1153 emit_insn (gen_auipcdi (temp, copy_rtx (addr), GEN_INT (seqno)));
1154 else
1155 emit_insn (gen_auipcsi (temp, copy_rtx (addr), GEN_INT (seqno)));
1157 *low_out = gen_rtx_LO_SUM (Pmode, temp, label);
1159 seqno++;
1161 break;
1163 default:
1164 gcc_unreachable ();
1167 return true;
1170 /* Return a legitimate address for REG + OFFSET. TEMP is as for
1171 riscv_force_temporary; it is only needed when OFFSET is not a
1172 SMALL_OPERAND. */
1174 static rtx
1175 riscv_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
1177 if (!SMALL_OPERAND (offset))
1179 rtx high;
1181 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
1182 The addition inside the macro CONST_HIGH_PART may cause an
1183 overflow, so we need to force a sign-extension check. */
1184 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
1185 offset = CONST_LOW_PART (offset);
1186 high = riscv_force_temporary (temp, high);
1187 reg = riscv_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
1189 return plus_constant (Pmode, reg, offset);
1192 /* The __tls_get_attr symbol. */
1193 static GTY(()) rtx riscv_tls_symbol;
1195 /* Return an instruction sequence that calls __tls_get_addr. SYM is
1196 the TLS symbol we are referencing and TYPE is the symbol type to use
1197 (either global dynamic or local dynamic). RESULT is an RTX for the
1198 return value location. */
1200 static rtx_insn *
1201 riscv_call_tls_get_addr (rtx sym, rtx result)
1203 rtx a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST), func;
1204 rtx_insn *insn;
1206 if (!riscv_tls_symbol)
1207 riscv_tls_symbol = init_one_libfunc ("__tls_get_addr");
1208 func = gen_rtx_MEM (FUNCTION_MODE, riscv_tls_symbol);
1210 start_sequence ();
1212 emit_insn (riscv_got_load_tls_gd (a0, sym));
1213 insn = emit_call_insn (gen_call_value (result, func, const0_rtx, NULL));
1214 RTL_CONST_CALL_P (insn) = 1;
1215 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
1216 insn = get_insns ();
1218 end_sequence ();
1220 return insn;
1223 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
1224 its address. The return value will be both a valid address and a valid
1225 SET_SRC (either a REG or a LO_SUM). */
1227 static rtx
1228 riscv_legitimize_tls_address (rtx loc)
1230 rtx dest, tp, tmp;
1231 enum tls_model model = SYMBOL_REF_TLS_MODEL (loc);
1233 /* Since we support TLS copy relocs, non-PIC TLS accesses may all use LE. */
1234 if (!flag_pic)
1235 model = TLS_MODEL_LOCAL_EXEC;
1237 switch (model)
1239 case TLS_MODEL_LOCAL_DYNAMIC:
1240 /* Rely on section anchors for the optimization that LDM TLS
1241 provides. The anchor's address is loaded with GD TLS. */
1242 case TLS_MODEL_GLOBAL_DYNAMIC:
1243 tmp = gen_rtx_REG (Pmode, GP_RETURN);
1244 dest = gen_reg_rtx (Pmode);
1245 emit_libcall_block (riscv_call_tls_get_addr (loc, tmp), dest, tmp, loc);
1246 break;
1248 case TLS_MODEL_INITIAL_EXEC:
1249 /* la.tls.ie; tp-relative add */
1250 tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
1251 tmp = gen_reg_rtx (Pmode);
1252 emit_insn (riscv_got_load_tls_ie (tmp, loc));
1253 dest = gen_reg_rtx (Pmode);
1254 emit_insn (gen_add3_insn (dest, tmp, tp));
1255 break;
1257 case TLS_MODEL_LOCAL_EXEC:
1258 tmp = riscv_unspec_offset_high (NULL, loc, SYMBOL_TLS_LE);
1259 dest = gen_reg_rtx (Pmode);
1260 emit_insn (riscv_tls_add_tp_le (dest, tmp, loc));
1261 dest = gen_rtx_LO_SUM (Pmode, dest,
1262 riscv_unspec_address (loc, SYMBOL_TLS_LE));
1263 break;
1265 default:
1266 gcc_unreachable ();
1268 return dest;
1271 /* If X is not a valid address for mode MODE, force it into a register. */
1273 static rtx
1274 riscv_force_address (rtx x, machine_mode mode)
1276 if (!riscv_legitimate_address_p (mode, x, false))
1277 x = force_reg (Pmode, x);
1278 return x;
1281 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
1282 be legitimized in a way that the generic machinery might not expect,
1283 return a new address, otherwise return NULL. MODE is the mode of
1284 the memory being accessed. */
1286 static rtx
1287 riscv_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1288 machine_mode mode)
1290 rtx addr;
1292 if (riscv_tls_symbol_p (x))
1293 return riscv_legitimize_tls_address (x);
1295 /* See if the address can split into a high part and a LO_SUM. */
1296 if (riscv_split_symbol (NULL, x, mode, &addr))
1297 return riscv_force_address (addr, mode);
1299 /* Handle BASE + OFFSET using riscv_add_offset. */
1300 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1))
1301 && INTVAL (XEXP (x, 1)) != 0)
1303 rtx base = XEXP (x, 0);
1304 HOST_WIDE_INT offset = INTVAL (XEXP (x, 1));
1306 if (!riscv_valid_base_register_p (base, mode, false))
1307 base = copy_to_mode_reg (Pmode, base);
1308 addr = riscv_add_offset (NULL, base, offset);
1309 return riscv_force_address (addr, mode);
1312 return x;
1315 /* Load VALUE into DEST. TEMP is as for riscv_force_temporary. */
1317 void
1318 riscv_move_integer (rtx temp, rtx dest, HOST_WIDE_INT value)
1320 struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
1321 machine_mode mode;
1322 int i, num_ops;
1323 rtx x;
1325 mode = GET_MODE (dest);
1326 num_ops = riscv_build_integer (codes, value, mode);
1328 if (can_create_pseudo_p () && num_ops > 2 /* not a simple constant */
1329 && num_ops >= riscv_split_integer_cost (value))
1330 x = riscv_split_integer (value, mode);
1331 else
1333 /* Apply each binary operation to X. */
1334 x = GEN_INT (codes[0].value);
1336 for (i = 1; i < num_ops; i++)
1338 if (!can_create_pseudo_p ())
1339 x = riscv_emit_set (temp, x);
1340 else
1341 x = force_reg (mode, x);
1343 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
1347 riscv_emit_set (dest, x);
1350 /* Subroutine of riscv_legitimize_move. Move constant SRC into register
1351 DEST given that SRC satisfies immediate_operand but doesn't satisfy
1352 move_operand. */
1354 static void
1355 riscv_legitimize_const_move (machine_mode mode, rtx dest, rtx src)
1357 rtx base, offset;
1359 /* Split moves of big integers into smaller pieces. */
1360 if (splittable_const_int_operand (src, mode))
1362 riscv_move_integer (dest, dest, INTVAL (src));
1363 return;
1366 /* Split moves of symbolic constants into high/low pairs. */
1367 if (riscv_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
1369 riscv_emit_set (dest, src);
1370 return;
1373 /* Generate the appropriate access sequences for TLS symbols. */
1374 if (riscv_tls_symbol_p (src))
1376 riscv_emit_move (dest, riscv_legitimize_tls_address (src));
1377 return;
1380 /* If we have (const (plus symbol offset)), and that expression cannot
1381 be forced into memory, load the symbol first and add in the offset. Also
1382 prefer to do this even if the constant _can_ be forced into memory, as it
1383 usually produces better code. */
1384 split_const (src, &base, &offset);
1385 if (offset != const0_rtx
1386 && (targetm.cannot_force_const_mem (mode, src) || can_create_pseudo_p ()))
1388 base = riscv_force_temporary (dest, base);
1389 riscv_emit_move (dest, riscv_add_offset (NULL, base, INTVAL (offset)));
1390 return;
1393 src = force_const_mem (mode, src);
1395 /* When using explicit relocs, constant pool references are sometimes
1396 not legitimate addresses. */
1397 riscv_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
1398 riscv_emit_move (dest, src);
1401 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
1402 sequence that is valid. */
1404 bool
1405 riscv_legitimize_move (machine_mode mode, rtx dest, rtx src)
1407 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
1409 riscv_emit_move (dest, force_reg (mode, src));
1410 return true;
1413 /* We need to deal with constants that would be legitimate
1414 immediate_operands but aren't legitimate move_operands. */
1415 if (CONSTANT_P (src) && !move_operand (src, mode))
1417 riscv_legitimize_const_move (mode, dest, src);
1418 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
1419 return true;
1422 /* RISC-V GCC may generate non-legitimate address due to we provide some
1423 pattern for optimize access PIC local symbol and it's make GCC generate
1424 unrecognizable instruction during optmizing. */
1426 if (MEM_P (dest) && !riscv_legitimate_address_p (mode, XEXP (dest, 0),
1427 reload_completed))
1429 XEXP (dest, 0) = riscv_force_address (XEXP (dest, 0), mode);
1432 if (MEM_P (src) && !riscv_legitimate_address_p (mode, XEXP (src, 0),
1433 reload_completed))
1435 XEXP (src, 0) = riscv_force_address (XEXP (src, 0), mode);
1438 return false;
1441 /* Return true if there is an instruction that implements CODE and accepts
1442 X as an immediate operand. */
1444 static int
1445 riscv_immediate_operand_p (int code, HOST_WIDE_INT x)
1447 switch (code)
1449 case ASHIFT:
1450 case ASHIFTRT:
1451 case LSHIFTRT:
1452 /* All shift counts are truncated to a valid constant. */
1453 return true;
1455 case AND:
1456 case IOR:
1457 case XOR:
1458 case PLUS:
1459 case LT:
1460 case LTU:
1461 /* These instructions take 12-bit signed immediates. */
1462 return SMALL_OPERAND (x);
1464 case LE:
1465 /* We add 1 to the immediate and use SLT. */
1466 return SMALL_OPERAND (x + 1);
1468 case LEU:
1469 /* Likewise SLTU, but reject the always-true case. */
1470 return SMALL_OPERAND (x + 1) && x + 1 != 0;
1472 case GE:
1473 case GEU:
1474 /* We can emulate an immediate of 1 by using GT/GTU against x0. */
1475 return x == 1;
1477 default:
1478 /* By default assume that x0 can be used for 0. */
1479 return x == 0;
1483 /* Return the cost of binary operation X, given that the instruction
1484 sequence for a word-sized or smaller operation takes SIGNLE_INSNS
1485 instructions and that the sequence of a double-word operation takes
1486 DOUBLE_INSNS instructions. */
1488 static int
1489 riscv_binary_cost (rtx x, int single_insns, int double_insns)
1491 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
1492 return COSTS_N_INSNS (double_insns);
1493 return COSTS_N_INSNS (single_insns);
1496 /* Return the cost of sign- or zero-extending OP. */
1498 static int
1499 riscv_extend_cost (rtx op, bool unsigned_p)
1501 if (MEM_P (op))
1502 return 0;
1504 if (unsigned_p && GET_MODE (op) == QImode)
1505 /* We can use ANDI. */
1506 return COSTS_N_INSNS (1);
1508 if (!unsigned_p && GET_MODE (op) == SImode)
1509 /* We can use SEXT.W. */
1510 return COSTS_N_INSNS (1);
1512 /* We need to use a shift left and a shift right. */
1513 return COSTS_N_INSNS (2);
1516 /* Implement TARGET_RTX_COSTS. */
1518 #define SINGLE_SHIFT_COST 1
1520 static bool
1521 riscv_rtx_costs (rtx x, machine_mode mode, int outer_code, int opno ATTRIBUTE_UNUSED,
1522 int *total, bool speed)
1524 bool float_mode_p = FLOAT_MODE_P (mode);
1525 int cost;
1527 switch (GET_CODE (x))
1529 case CONST_INT:
1530 if (riscv_immediate_operand_p (outer_code, INTVAL (x)))
1532 *total = 0;
1533 return true;
1535 /* Fall through. */
1537 case SYMBOL_REF:
1538 case LABEL_REF:
1539 case CONST_DOUBLE:
1540 case CONST:
1541 if ((cost = riscv_const_insns (x)) > 0)
1543 /* If the constant is likely to be stored in a GPR, SETs of
1544 single-insn constants are as cheap as register sets; we
1545 never want to CSE them. */
1546 if (cost == 1 && outer_code == SET)
1547 *total = 0;
1548 /* When we load a constant more than once, it usually is better
1549 to duplicate the last operation in the sequence than to CSE
1550 the constant itself. */
1551 else if (outer_code == SET || GET_MODE (x) == VOIDmode)
1552 *total = COSTS_N_INSNS (1);
1554 else /* The instruction will be fetched from the constant pool. */
1555 *total = COSTS_N_INSNS (riscv_symbol_insns (SYMBOL_ABSOLUTE));
1556 return true;
1558 case MEM:
1559 /* If the address is legitimate, return the number of
1560 instructions it needs. */
1561 if ((cost = riscv_address_insns (XEXP (x, 0), mode, true)) > 0)
1563 *total = COSTS_N_INSNS (cost + tune_info->memory_cost);
1564 return true;
1566 /* Otherwise use the default handling. */
1567 return false;
1569 case NOT:
1570 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
1571 return false;
1573 case AND:
1574 case IOR:
1575 case XOR:
1576 /* Double-word operations use two single-word operations. */
1577 *total = riscv_binary_cost (x, 1, 2);
1578 return false;
1580 case ZERO_EXTRACT:
1581 /* This is an SImode shift. */
1582 if (outer_code == SET && (INTVAL (XEXP (x, 2)) > 0)
1583 && (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (x, 2)) == 32))
1585 *total = COSTS_N_INSNS (SINGLE_SHIFT_COST);
1586 return true;
1588 return false;
1590 case ASHIFT:
1591 case ASHIFTRT:
1592 case LSHIFTRT:
1593 *total = riscv_binary_cost (x, SINGLE_SHIFT_COST,
1594 CONSTANT_P (XEXP (x, 1)) ? 4 : 9);
1595 return false;
1597 case ABS:
1598 *total = COSTS_N_INSNS (float_mode_p ? 1 : 3);
1599 return false;
1601 case LO_SUM:
1602 *total = set_src_cost (XEXP (x, 0), mode, speed);
1603 return true;
1605 case LT:
1606 /* This is an SImode shift. */
1607 if (outer_code == SET && GET_MODE (x) == DImode
1608 && GET_MODE (XEXP (x, 0)) == SImode)
1610 *total = COSTS_N_INSNS (SINGLE_SHIFT_COST);
1611 return true;
1613 /* Fall through. */
1614 case LTU:
1615 case LE:
1616 case LEU:
1617 case GT:
1618 case GTU:
1619 case GE:
1620 case GEU:
1621 case EQ:
1622 case NE:
1623 /* Branch comparisons have VOIDmode, so use the first operand's
1624 mode instead. */
1625 mode = GET_MODE (XEXP (x, 0));
1626 if (float_mode_p)
1627 *total = tune_info->fp_add[mode == DFmode];
1628 else
1629 *total = riscv_binary_cost (x, 1, 3);
1630 return false;
1632 case UNORDERED:
1633 case ORDERED:
1634 /* (FEQ(A, A) & FEQ(B, B)) compared against 0. */
1635 mode = GET_MODE (XEXP (x, 0));
1636 *total = tune_info->fp_add[mode == DFmode] + COSTS_N_INSNS (2);
1637 return false;
1639 case UNEQ:
1640 case LTGT:
1641 /* (FEQ(A, A) & FEQ(B, B)) compared against FEQ(A, B). */
1642 mode = GET_MODE (XEXP (x, 0));
1643 *total = tune_info->fp_add[mode == DFmode] + COSTS_N_INSNS (3);
1644 return false;
1646 case UNGE:
1647 case UNGT:
1648 case UNLE:
1649 case UNLT:
1650 /* FLT or FLE, but guarded by an FFLAGS read and write. */
1651 mode = GET_MODE (XEXP (x, 0));
1652 *total = tune_info->fp_add[mode == DFmode] + COSTS_N_INSNS (4);
1653 return false;
1655 case MINUS:
1656 case PLUS:
1657 if (float_mode_p)
1658 *total = tune_info->fp_add[mode == DFmode];
1659 else
1660 *total = riscv_binary_cost (x, 1, 4);
1661 return false;
1663 case NEG:
1665 rtx op = XEXP (x, 0);
1666 if (GET_CODE (op) == FMA && !HONOR_SIGNED_ZEROS (mode))
1668 *total = (tune_info->fp_mul[mode == DFmode]
1669 + set_src_cost (XEXP (op, 0), mode, speed)
1670 + set_src_cost (XEXP (op, 1), mode, speed)
1671 + set_src_cost (XEXP (op, 2), mode, speed));
1672 return true;
1676 if (float_mode_p)
1677 *total = tune_info->fp_add[mode == DFmode];
1678 else
1679 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
1680 return false;
1682 case MULT:
1683 if (float_mode_p)
1684 *total = tune_info->fp_mul[mode == DFmode];
1685 else if (!TARGET_MUL)
1686 /* Estimate the cost of a library call. */
1687 *total = COSTS_N_INSNS (speed ? 32 : 6);
1688 else if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
1689 *total = 3 * tune_info->int_mul[0] + COSTS_N_INSNS (2);
1690 else if (!speed)
1691 *total = COSTS_N_INSNS (1);
1692 else
1693 *total = tune_info->int_mul[mode == DImode];
1694 return false;
1696 case DIV:
1697 case SQRT:
1698 case MOD:
1699 if (float_mode_p)
1701 *total = tune_info->fp_div[mode == DFmode];
1702 return false;
1704 /* Fall through. */
1706 case UDIV:
1707 case UMOD:
1708 if (!TARGET_DIV)
1709 /* Estimate the cost of a library call. */
1710 *total = COSTS_N_INSNS (speed ? 32 : 6);
1711 else if (speed)
1712 *total = tune_info->int_div[mode == DImode];
1713 else
1714 *total = COSTS_N_INSNS (1);
1715 return false;
1717 case ZERO_EXTEND:
1718 /* This is an SImode shift. */
1719 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT)
1721 *total = COSTS_N_INSNS (SINGLE_SHIFT_COST);
1722 return true;
1724 /* Fall through. */
1725 case SIGN_EXTEND:
1726 *total = riscv_extend_cost (XEXP (x, 0), GET_CODE (x) == ZERO_EXTEND);
1727 return false;
1729 case FLOAT:
1730 case UNSIGNED_FLOAT:
1731 case FIX:
1732 case FLOAT_EXTEND:
1733 case FLOAT_TRUNCATE:
1734 *total = tune_info->fp_add[mode == DFmode];
1735 return false;
1737 case FMA:
1738 *total = (tune_info->fp_mul[mode == DFmode]
1739 + set_src_cost (XEXP (x, 0), mode, speed)
1740 + set_src_cost (XEXP (x, 1), mode, speed)
1741 + set_src_cost (XEXP (x, 2), mode, speed));
1742 return true;
1744 case UNSPEC:
1745 if (XINT (x, 1) == UNSPEC_AUIPC)
1747 /* Make AUIPC cheap to avoid spilling its result to the stack. */
1748 *total = 1;
1749 return true;
1751 return false;
1753 default:
1754 return false;
1758 /* Implement TARGET_ADDRESS_COST. */
1760 static int
1761 riscv_address_cost (rtx addr, machine_mode mode,
1762 addr_space_t as ATTRIBUTE_UNUSED,
1763 bool speed ATTRIBUTE_UNUSED)
1765 return riscv_address_insns (addr, mode, false);
1768 /* Return one word of double-word value OP. HIGH_P is true to select the
1769 high part or false to select the low part. */
1772 riscv_subword (rtx op, bool high_p)
1774 unsigned int byte = high_p ? UNITS_PER_WORD : 0;
1775 machine_mode mode = GET_MODE (op);
1777 if (mode == VOIDmode)
1778 mode = TARGET_64BIT ? TImode : DImode;
1780 if (MEM_P (op))
1781 return adjust_address (op, word_mode, byte);
1783 if (REG_P (op))
1784 gcc_assert (!FP_REG_RTX_P (op));
1786 return simplify_gen_subreg (word_mode, op, mode, byte);
1789 /* Return true if a 64-bit move from SRC to DEST should be split into two. */
1791 bool
1792 riscv_split_64bit_move_p (rtx dest, rtx src)
1794 if (TARGET_64BIT)
1795 return false;
1797 /* Allow FPR <-> FPR and FPR <-> MEM moves, and permit the special case
1798 of zeroing an FPR with FCVT.D.W. */
1799 if (TARGET_DOUBLE_FLOAT
1800 && ((FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
1801 || (FP_REG_RTX_P (dest) && MEM_P (src))
1802 || (FP_REG_RTX_P (src) && MEM_P (dest))
1803 || (FP_REG_RTX_P (dest) && src == CONST0_RTX (GET_MODE (src)))))
1804 return false;
1806 return true;
1809 /* Split a doubleword move from SRC to DEST. On 32-bit targets,
1810 this function handles 64-bit moves for which riscv_split_64bit_move_p
1811 holds. For 64-bit targets, this function handles 128-bit moves. */
1813 void
1814 riscv_split_doubleword_move (rtx dest, rtx src)
1816 rtx low_dest;
1818 /* The operation can be split into two normal moves. Decide in
1819 which order to do them. */
1820 low_dest = riscv_subword (dest, false);
1821 if (REG_P (low_dest) && reg_overlap_mentioned_p (low_dest, src))
1823 riscv_emit_move (riscv_subword (dest, true), riscv_subword (src, true));
1824 riscv_emit_move (low_dest, riscv_subword (src, false));
1826 else
1828 riscv_emit_move (low_dest, riscv_subword (src, false));
1829 riscv_emit_move (riscv_subword (dest, true), riscv_subword (src, true));
1833 /* Return the appropriate instructions to move SRC into DEST. Assume
1834 that SRC is operand 1 and DEST is operand 0. */
1836 const char *
1837 riscv_output_move (rtx dest, rtx src)
1839 enum rtx_code dest_code, src_code;
1840 machine_mode mode;
1841 bool dbl_p;
1843 dest_code = GET_CODE (dest);
1844 src_code = GET_CODE (src);
1845 mode = GET_MODE (dest);
1846 dbl_p = (GET_MODE_SIZE (mode) == 8);
1848 if (dbl_p && riscv_split_64bit_move_p (dest, src))
1849 return "#";
1851 if (dest_code == REG && GP_REG_P (REGNO (dest)))
1853 if (src_code == REG && FP_REG_P (REGNO (src)))
1854 return dbl_p ? "fmv.x.d\t%0,%1" : "fmv.x.s\t%0,%1";
1856 if (src_code == MEM)
1857 switch (GET_MODE_SIZE (mode))
1859 case 1: return "lbu\t%0,%1";
1860 case 2: return "lhu\t%0,%1";
1861 case 4: return "lw\t%0,%1";
1862 case 8: return "ld\t%0,%1";
1865 if (src_code == CONST_INT)
1866 return "li\t%0,%1";
1868 if (src_code == HIGH)
1869 return "lui\t%0,%h1";
1871 if (symbolic_operand (src, VOIDmode))
1872 switch (riscv_classify_symbolic_expression (src))
1874 case SYMBOL_GOT_DISP: return "la\t%0,%1";
1875 case SYMBOL_ABSOLUTE: return "lla\t%0,%1";
1876 case SYMBOL_PCREL: return "lla\t%0,%1";
1877 default: gcc_unreachable ();
1880 if ((src_code == REG && GP_REG_P (REGNO (src)))
1881 || (src == CONST0_RTX (mode)))
1883 if (dest_code == REG)
1885 if (GP_REG_P (REGNO (dest)))
1886 return "mv\t%0,%z1";
1888 if (FP_REG_P (REGNO (dest)))
1890 if (!dbl_p)
1891 return "fmv.s.x\t%0,%z1";
1892 if (TARGET_64BIT)
1893 return "fmv.d.x\t%0,%z1";
1894 /* in RV32, we can emulate fmv.d.x %0, x0 using fcvt.d.w */
1895 gcc_assert (src == CONST0_RTX (mode));
1896 return "fcvt.d.w\t%0,x0";
1899 if (dest_code == MEM)
1900 switch (GET_MODE_SIZE (mode))
1902 case 1: return "sb\t%z1,%0";
1903 case 2: return "sh\t%z1,%0";
1904 case 4: return "sw\t%z1,%0";
1905 case 8: return "sd\t%z1,%0";
1908 if (src_code == REG && FP_REG_P (REGNO (src)))
1910 if (dest_code == REG && FP_REG_P (REGNO (dest)))
1911 return dbl_p ? "fmv.d\t%0,%1" : "fmv.s\t%0,%1";
1913 if (dest_code == MEM)
1914 return dbl_p ? "fsd\t%1,%0" : "fsw\t%1,%0";
1916 if (dest_code == REG && FP_REG_P (REGNO (dest)))
1918 if (src_code == MEM)
1919 return dbl_p ? "fld\t%0,%1" : "flw\t%0,%1";
1921 gcc_unreachable ();
1924 const char *
1925 riscv_output_return ()
1927 if (cfun->machine->naked_p)
1928 return "";
1930 return "ret";
1934 /* Return true if CMP1 is a suitable second operand for integer ordering
1935 test CODE. See also the *sCC patterns in riscv.md. */
1937 static bool
1938 riscv_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
1940 switch (code)
1942 case GT:
1943 case GTU:
1944 return reg_or_0_operand (cmp1, VOIDmode);
1946 case GE:
1947 case GEU:
1948 return cmp1 == const1_rtx;
1950 case LT:
1951 case LTU:
1952 return arith_operand (cmp1, VOIDmode);
1954 case LE:
1955 return sle_operand (cmp1, VOIDmode);
1957 case LEU:
1958 return sleu_operand (cmp1, VOIDmode);
1960 default:
1961 gcc_unreachable ();
1965 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
1966 integer ordering test *CODE, or if an equivalent combination can
1967 be formed by adjusting *CODE and *CMP1. When returning true, update
1968 *CODE and *CMP1 with the chosen code and operand, otherwise leave
1969 them alone. */
1971 static bool
1972 riscv_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
1973 machine_mode mode)
1975 HOST_WIDE_INT plus_one;
1977 if (riscv_int_order_operand_ok_p (*code, *cmp1))
1978 return true;
1980 if (CONST_INT_P (*cmp1))
1981 switch (*code)
1983 case LE:
1984 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
1985 if (INTVAL (*cmp1) < plus_one)
1987 *code = LT;
1988 *cmp1 = force_reg (mode, GEN_INT (plus_one));
1989 return true;
1991 break;
1993 case LEU:
1994 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
1995 if (plus_one != 0)
1997 *code = LTU;
1998 *cmp1 = force_reg (mode, GEN_INT (plus_one));
1999 return true;
2001 break;
2003 default:
2004 break;
2006 return false;
2009 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
2010 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
2011 is nonnull, it's OK to set TARGET to the inverse of the result and
2012 flip *INVERT_PTR instead. */
2014 static void
2015 riscv_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
2016 rtx target, rtx cmp0, rtx cmp1)
2018 machine_mode mode;
2020 /* First see if there is a RISCV instruction that can do this operation.
2021 If not, try doing the same for the inverse operation. If that also
2022 fails, force CMP1 into a register and try again. */
2023 mode = GET_MODE (cmp0);
2024 if (riscv_canonicalize_int_order_test (&code, &cmp1, mode))
2025 riscv_emit_binary (code, target, cmp0, cmp1);
2026 else
2028 enum rtx_code inv_code = reverse_condition (code);
2029 if (!riscv_canonicalize_int_order_test (&inv_code, &cmp1, mode))
2031 cmp1 = force_reg (mode, cmp1);
2032 riscv_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
2034 else if (invert_ptr == 0)
2036 rtx inv_target = riscv_force_binary (GET_MODE (target),
2037 inv_code, cmp0, cmp1);
2038 riscv_emit_binary (XOR, target, inv_target, const1_rtx);
2040 else
2042 *invert_ptr = !*invert_ptr;
2043 riscv_emit_binary (inv_code, target, cmp0, cmp1);
2048 /* Return a register that is zero iff CMP0 and CMP1 are equal.
2049 The register will have the same mode as CMP0. */
2051 static rtx
2052 riscv_zero_if_equal (rtx cmp0, rtx cmp1)
2054 if (cmp1 == const0_rtx)
2055 return cmp0;
2057 return expand_binop (GET_MODE (cmp0), sub_optab,
2058 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
2061 /* Sign- or zero-extend OP0 and OP1 for integer comparisons. */
2063 static void
2064 riscv_extend_comparands (rtx_code code, rtx *op0, rtx *op1)
2066 /* Comparisons consider all XLEN bits, so extend sub-XLEN values. */
2067 if (GET_MODE_SIZE (word_mode) > GET_MODE_SIZE (GET_MODE (*op0)))
2069 /* It is more profitable to zero-extend QImode values. But not if the
2070 first operand has already been sign-extended, and the second one is
2071 is a constant or has already been sign-extended also. */
2072 if (unsigned_condition (code) == code
2073 && (GET_MODE (*op0) == QImode
2074 && ! (GET_CODE (*op0) == SUBREG
2075 && SUBREG_PROMOTED_VAR_P (*op0)
2076 && SUBREG_PROMOTED_SIGNED_P (*op0)
2077 && (CONST_INT_P (*op1)
2078 || (GET_CODE (*op1) == SUBREG
2079 && SUBREG_PROMOTED_VAR_P (*op1)
2080 && SUBREG_PROMOTED_SIGNED_P (*op1))))))
2082 *op0 = gen_rtx_ZERO_EXTEND (word_mode, *op0);
2083 if (CONST_INT_P (*op1))
2084 *op1 = GEN_INT ((uint8_t) INTVAL (*op1));
2085 else
2086 *op1 = gen_rtx_ZERO_EXTEND (word_mode, *op1);
2088 else
2090 *op0 = gen_rtx_SIGN_EXTEND (word_mode, *op0);
2091 if (*op1 != const0_rtx)
2092 *op1 = gen_rtx_SIGN_EXTEND (word_mode, *op1);
2097 /* Convert a comparison into something that can be used in a branch. On
2098 entry, *OP0 and *OP1 are the values being compared and *CODE is the code
2099 used to compare them. Update them to describe the final comparison. */
2101 static void
2102 riscv_emit_int_compare (enum rtx_code *code, rtx *op0, rtx *op1)
2104 if (splittable_const_int_operand (*op1, VOIDmode))
2106 HOST_WIDE_INT rhs = INTVAL (*op1);
2108 if (*code == EQ || *code == NE)
2110 /* Convert e.g. OP0 == 2048 into OP0 - 2048 == 0. */
2111 if (SMALL_OPERAND (-rhs))
2113 *op0 = riscv_force_binary (GET_MODE (*op0), PLUS, *op0,
2114 GEN_INT (-rhs));
2115 *op1 = const0_rtx;
2118 else
2120 static const enum rtx_code mag_comparisons[][2] = {
2121 {LEU, LTU}, {GTU, GEU}, {LE, LT}, {GT, GE}
2124 /* Convert e.g. (OP0 <= 0xFFF) into (OP0 < 0x1000). */
2125 for (size_t i = 0; i < ARRAY_SIZE (mag_comparisons); i++)
2127 HOST_WIDE_INT new_rhs;
2128 bool increment = *code == mag_comparisons[i][0];
2129 bool decrement = *code == mag_comparisons[i][1];
2130 if (!increment && !decrement)
2131 continue;
2133 new_rhs = rhs + (increment ? 1 : -1);
2134 if (riscv_integer_cost (new_rhs) < riscv_integer_cost (rhs)
2135 && (rhs < 0) == (new_rhs < 0))
2137 *op1 = GEN_INT (new_rhs);
2138 *code = mag_comparisons[i][increment];
2140 break;
2145 riscv_extend_comparands (*code, op0, op1);
2147 *op0 = force_reg (word_mode, *op0);
2148 if (*op1 != const0_rtx)
2149 *op1 = force_reg (word_mode, *op1);
2152 /* Like riscv_emit_int_compare, but for floating-point comparisons. */
2154 static void
2155 riscv_emit_float_compare (enum rtx_code *code, rtx *op0, rtx *op1)
2157 rtx tmp0, tmp1, cmp_op0 = *op0, cmp_op1 = *op1;
2158 enum rtx_code fp_code = *code;
2159 *code = NE;
2161 switch (fp_code)
2163 case UNORDERED:
2164 *code = EQ;
2165 /* Fall through. */
2167 case ORDERED:
2168 /* a == a && b == b */
2169 tmp0 = riscv_force_binary (word_mode, EQ, cmp_op0, cmp_op0);
2170 tmp1 = riscv_force_binary (word_mode, EQ, cmp_op1, cmp_op1);
2171 *op0 = riscv_force_binary (word_mode, AND, tmp0, tmp1);
2172 *op1 = const0_rtx;
2173 break;
2175 case UNEQ:
2176 case LTGT:
2177 /* ordered(a, b) > (a == b) */
2178 *code = fp_code == LTGT ? GTU : EQ;
2179 tmp0 = riscv_force_binary (word_mode, EQ, cmp_op0, cmp_op0);
2180 tmp1 = riscv_force_binary (word_mode, EQ, cmp_op1, cmp_op1);
2181 *op0 = riscv_force_binary (word_mode, AND, tmp0, tmp1);
2182 *op1 = riscv_force_binary (word_mode, EQ, cmp_op0, cmp_op1);
2183 break;
2185 #define UNORDERED_COMPARISON(CODE, CMP) \
2186 case CODE: \
2187 *code = EQ; \
2188 *op0 = gen_reg_rtx (word_mode); \
2189 if (GET_MODE (cmp_op0) == SFmode && TARGET_64BIT) \
2190 emit_insn (gen_f##CMP##_quietsfdi4 (*op0, cmp_op0, cmp_op1)); \
2191 else if (GET_MODE (cmp_op0) == SFmode) \
2192 emit_insn (gen_f##CMP##_quietsfsi4 (*op0, cmp_op0, cmp_op1)); \
2193 else if (GET_MODE (cmp_op0) == DFmode && TARGET_64BIT) \
2194 emit_insn (gen_f##CMP##_quietdfdi4 (*op0, cmp_op0, cmp_op1)); \
2195 else if (GET_MODE (cmp_op0) == DFmode) \
2196 emit_insn (gen_f##CMP##_quietdfsi4 (*op0, cmp_op0, cmp_op1)); \
2197 else \
2198 gcc_unreachable (); \
2199 *op1 = const0_rtx; \
2200 break;
2202 case UNLT:
2203 std::swap (cmp_op0, cmp_op1);
2204 gcc_fallthrough ();
2206 UNORDERED_COMPARISON(UNGT, le)
2208 case UNLE:
2209 std::swap (cmp_op0, cmp_op1);
2210 gcc_fallthrough ();
2212 UNORDERED_COMPARISON(UNGE, lt)
2213 #undef UNORDERED_COMPARISON
2215 case NE:
2216 fp_code = EQ;
2217 *code = EQ;
2218 /* Fall through. */
2220 case EQ:
2221 case LE:
2222 case LT:
2223 case GE:
2224 case GT:
2225 /* We have instructions for these cases. */
2226 *op0 = riscv_force_binary (word_mode, fp_code, cmp_op0, cmp_op1);
2227 *op1 = const0_rtx;
2228 break;
2230 default:
2231 gcc_unreachable ();
2235 /* CODE-compare OP0 and OP1. Store the result in TARGET. */
2237 void
2238 riscv_expand_int_scc (rtx target, enum rtx_code code, rtx op0, rtx op1)
2240 riscv_extend_comparands (code, &op0, &op1);
2241 op0 = force_reg (word_mode, op0);
2243 if (code == EQ || code == NE)
2245 rtx zie = riscv_zero_if_equal (op0, op1);
2246 riscv_emit_binary (code, target, zie, const0_rtx);
2248 else
2249 riscv_emit_int_order_test (code, 0, target, op0, op1);
2252 /* Like riscv_expand_int_scc, but for floating-point comparisons. */
2254 void
2255 riscv_expand_float_scc (rtx target, enum rtx_code code, rtx op0, rtx op1)
2257 riscv_emit_float_compare (&code, &op0, &op1);
2259 rtx cmp = riscv_force_binary (word_mode, code, op0, op1);
2260 riscv_emit_set (target, lowpart_subreg (SImode, cmp, word_mode));
2263 /* Jump to LABEL if (CODE OP0 OP1) holds. */
2265 void
2266 riscv_expand_conditional_branch (rtx label, rtx_code code, rtx op0, rtx op1)
2268 if (FLOAT_MODE_P (GET_MODE (op1)))
2269 riscv_emit_float_compare (&code, &op0, &op1);
2270 else
2271 riscv_emit_int_compare (&code, &op0, &op1);
2273 rtx condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
2274 emit_jump_insn (gen_condjump (condition, label));
2277 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Every parameter gets at
2278 least PARM_BOUNDARY bits of alignment, but will be given anything up
2279 to PREFERRED_STACK_BOUNDARY bits if the type requires it. */
2281 static unsigned int
2282 riscv_function_arg_boundary (machine_mode mode, const_tree type)
2284 unsigned int alignment;
2286 /* Use natural alignment if the type is not aggregate data. */
2287 if (type && !AGGREGATE_TYPE_P (type))
2288 alignment = TYPE_ALIGN (TYPE_MAIN_VARIANT (type));
2289 else
2290 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
2292 return MIN (PREFERRED_STACK_BOUNDARY, MAX (PARM_BOUNDARY, alignment));
2295 /* If MODE represents an argument that can be passed or returned in
2296 floating-point registers, return the number of registers, else 0. */
2298 static unsigned
2299 riscv_pass_mode_in_fpr_p (machine_mode mode)
2301 if (GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FP_ARG)
2303 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2304 return 1;
2306 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
2307 return 2;
2310 return 0;
2313 typedef struct {
2314 const_tree type;
2315 HOST_WIDE_INT offset;
2316 } riscv_aggregate_field;
2318 /* Identify subfields of aggregates that are candidates for passing in
2319 floating-point registers. */
2321 static int
2322 riscv_flatten_aggregate_field (const_tree type,
2323 riscv_aggregate_field fields[2],
2324 int n, HOST_WIDE_INT offset)
2326 switch (TREE_CODE (type))
2328 case RECORD_TYPE:
2329 /* Can't handle incomplete types nor sizes that are not fixed. */
2330 if (!COMPLETE_TYPE_P (type)
2331 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
2332 || !tree_fits_uhwi_p (TYPE_SIZE (type)))
2333 return -1;
2335 for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
2336 if (TREE_CODE (f) == FIELD_DECL)
2338 if (!TYPE_P (TREE_TYPE (f)))
2339 return -1;
2341 HOST_WIDE_INT pos = offset + int_byte_position (f);
2342 n = riscv_flatten_aggregate_field (TREE_TYPE (f), fields, n, pos);
2343 if (n < 0)
2344 return -1;
2346 return n;
2348 case ARRAY_TYPE:
2350 HOST_WIDE_INT n_elts;
2351 riscv_aggregate_field subfields[2];
2352 tree index = TYPE_DOMAIN (type);
2353 tree elt_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
2354 int n_subfields = riscv_flatten_aggregate_field (TREE_TYPE (type),
2355 subfields, 0, offset);
2357 /* Can't handle incomplete types nor sizes that are not fixed. */
2358 if (n_subfields <= 0
2359 || !COMPLETE_TYPE_P (type)
2360 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
2361 || !index
2362 || !TYPE_MAX_VALUE (index)
2363 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
2364 || !TYPE_MIN_VALUE (index)
2365 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
2366 || !tree_fits_uhwi_p (elt_size))
2367 return -1;
2369 n_elts = 1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
2370 - tree_to_uhwi (TYPE_MIN_VALUE (index));
2371 gcc_assert (n_elts >= 0);
2373 for (HOST_WIDE_INT i = 0; i < n_elts; i++)
2374 for (int j = 0; j < n_subfields; j++)
2376 if (n >= 2)
2377 return -1;
2379 fields[n] = subfields[j];
2380 fields[n++].offset += i * tree_to_uhwi (elt_size);
2383 return n;
2386 case COMPLEX_TYPE:
2388 /* Complex type need consume 2 field, so n must be 0. */
2389 if (n != 0)
2390 return -1;
2392 HOST_WIDE_INT elt_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (type)));
2394 if (elt_size <= UNITS_PER_FP_ARG)
2396 fields[0].type = TREE_TYPE (type);
2397 fields[0].offset = offset;
2398 fields[1].type = TREE_TYPE (type);
2399 fields[1].offset = offset + elt_size;
2401 return 2;
2404 return -1;
2407 default:
2408 if (n < 2
2409 && ((SCALAR_FLOAT_TYPE_P (type)
2410 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FP_ARG)
2411 || (INTEGRAL_TYPE_P (type)
2412 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_WORD)))
2414 fields[n].type = type;
2415 fields[n].offset = offset;
2416 return n + 1;
2418 else
2419 return -1;
2423 /* Identify candidate aggregates for passing in floating-point registers.
2424 Candidates have at most two fields after flattening. */
2426 static int
2427 riscv_flatten_aggregate_argument (const_tree type,
2428 riscv_aggregate_field fields[2])
2430 if (!type || TREE_CODE (type) != RECORD_TYPE)
2431 return -1;
2433 return riscv_flatten_aggregate_field (type, fields, 0, 0);
2436 /* See whether TYPE is a record whose fields should be returned in one or
2437 two floating-point registers. If so, populate FIELDS accordingly. */
2439 static unsigned
2440 riscv_pass_aggregate_in_fpr_pair_p (const_tree type,
2441 riscv_aggregate_field fields[2])
2443 int n = riscv_flatten_aggregate_argument (type, fields);
2445 for (int i = 0; i < n; i++)
2446 if (!SCALAR_FLOAT_TYPE_P (fields[i].type))
2447 return 0;
2449 return n > 0 ? n : 0;
2452 /* See whether TYPE is a record whose fields should be returned in one or
2453 floating-point register and one integer register. If so, populate
2454 FIELDS accordingly. */
2456 static bool
2457 riscv_pass_aggregate_in_fpr_and_gpr_p (const_tree type,
2458 riscv_aggregate_field fields[2])
2460 unsigned num_int = 0, num_float = 0;
2461 int n = riscv_flatten_aggregate_argument (type, fields);
2463 for (int i = 0; i < n; i++)
2465 num_float += SCALAR_FLOAT_TYPE_P (fields[i].type);
2466 num_int += INTEGRAL_TYPE_P (fields[i].type);
2469 return num_int == 1 && num_float == 1;
2472 /* Return the representation of an argument passed or returned in an FPR
2473 when the value has mode VALUE_MODE and the type has TYPE_MODE. The
2474 two modes may be different for structures like:
2476 struct __attribute__((packed)) foo { float f; }
2478 where the SFmode value "f" is passed in REGNO but the struct itself
2479 has mode BLKmode. */
2481 static rtx
2482 riscv_pass_fpr_single (machine_mode type_mode, unsigned regno,
2483 machine_mode value_mode,
2484 HOST_WIDE_INT offset)
2486 rtx x = gen_rtx_REG (value_mode, regno);
2488 if (type_mode != value_mode)
2490 x = gen_rtx_EXPR_LIST (VOIDmode, x, GEN_INT (offset));
2491 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
2493 return x;
2496 /* Pass or return a composite value in the FPR pair REGNO and REGNO + 1.
2497 MODE is the mode of the composite. MODE1 and OFFSET1 are the mode and
2498 byte offset for the first value, likewise MODE2 and OFFSET2 for the
2499 second value. */
2501 static rtx
2502 riscv_pass_fpr_pair (machine_mode mode, unsigned regno1,
2503 machine_mode mode1, HOST_WIDE_INT offset1,
2504 unsigned regno2, machine_mode mode2,
2505 HOST_WIDE_INT offset2)
2507 return gen_rtx_PARALLEL
2508 (mode,
2509 gen_rtvec (2,
2510 gen_rtx_EXPR_LIST (VOIDmode,
2511 gen_rtx_REG (mode1, regno1),
2512 GEN_INT (offset1)),
2513 gen_rtx_EXPR_LIST (VOIDmode,
2514 gen_rtx_REG (mode2, regno2),
2515 GEN_INT (offset2))));
2518 /* Fill INFO with information about a single argument, and return an
2519 RTL pattern to pass or return the argument. CUM is the cumulative
2520 state for earlier arguments. MODE is the mode of this argument and
2521 TYPE is its type (if known). NAMED is true if this is a named
2522 (fixed) argument rather than a variable one. RETURN_P is true if
2523 returning the argument, or false if passing the argument. */
2525 static rtx
2526 riscv_get_arg_info (struct riscv_arg_info *info, const CUMULATIVE_ARGS *cum,
2527 machine_mode mode, const_tree type, bool named,
2528 bool return_p)
2530 unsigned num_bytes, num_words;
2531 unsigned fpr_base = return_p ? FP_RETURN : FP_ARG_FIRST;
2532 unsigned gpr_base = return_p ? GP_RETURN : GP_ARG_FIRST;
2533 unsigned alignment = riscv_function_arg_boundary (mode, type);
2535 memset (info, 0, sizeof (*info));
2536 info->gpr_offset = cum->num_gprs;
2537 info->fpr_offset = cum->num_fprs;
2539 if (named)
2541 riscv_aggregate_field fields[2];
2542 unsigned fregno = fpr_base + info->fpr_offset;
2543 unsigned gregno = gpr_base + info->gpr_offset;
2545 /* Pass one- or two-element floating-point aggregates in FPRs. */
2546 if ((info->num_fprs = riscv_pass_aggregate_in_fpr_pair_p (type, fields))
2547 && info->fpr_offset + info->num_fprs <= MAX_ARGS_IN_REGISTERS)
2548 switch (info->num_fprs)
2550 case 1:
2551 return riscv_pass_fpr_single (mode, fregno,
2552 TYPE_MODE (fields[0].type),
2553 fields[0].offset);
2555 case 2:
2556 return riscv_pass_fpr_pair (mode, fregno,
2557 TYPE_MODE (fields[0].type),
2558 fields[0].offset,
2559 fregno + 1,
2560 TYPE_MODE (fields[1].type),
2561 fields[1].offset);
2563 default:
2564 gcc_unreachable ();
2567 /* Pass real and complex floating-point numbers in FPRs. */
2568 if ((info->num_fprs = riscv_pass_mode_in_fpr_p (mode))
2569 && info->fpr_offset + info->num_fprs <= MAX_ARGS_IN_REGISTERS)
2570 switch (GET_MODE_CLASS (mode))
2572 case MODE_FLOAT:
2573 return gen_rtx_REG (mode, fregno);
2575 case MODE_COMPLEX_FLOAT:
2576 return riscv_pass_fpr_pair (mode, fregno, GET_MODE_INNER (mode), 0,
2577 fregno + 1, GET_MODE_INNER (mode),
2578 GET_MODE_UNIT_SIZE (mode));
2580 default:
2581 gcc_unreachable ();
2584 /* Pass structs with one float and one integer in an FPR and a GPR. */
2585 if (riscv_pass_aggregate_in_fpr_and_gpr_p (type, fields)
2586 && info->gpr_offset < MAX_ARGS_IN_REGISTERS
2587 && info->fpr_offset < MAX_ARGS_IN_REGISTERS)
2589 info->num_gprs = 1;
2590 info->num_fprs = 1;
2592 if (!SCALAR_FLOAT_TYPE_P (fields[0].type))
2593 std::swap (fregno, gregno);
2595 return riscv_pass_fpr_pair (mode, fregno, TYPE_MODE (fields[0].type),
2596 fields[0].offset,
2597 gregno, TYPE_MODE (fields[1].type),
2598 fields[1].offset);
2602 /* Work out the size of the argument. */
2603 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
2604 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2606 /* Doubleword-aligned varargs start on an even register boundary. */
2607 if (!named && num_bytes != 0 && alignment > BITS_PER_WORD)
2608 info->gpr_offset += info->gpr_offset & 1;
2610 /* Partition the argument between registers and stack. */
2611 info->num_fprs = 0;
2612 info->num_gprs = MIN (num_words, MAX_ARGS_IN_REGISTERS - info->gpr_offset);
2613 info->stack_p = (num_words - info->num_gprs) != 0;
2615 if (info->num_gprs || return_p)
2616 return gen_rtx_REG (mode, gpr_base + info->gpr_offset);
2618 return NULL_RTX;
2621 /* Implement TARGET_FUNCTION_ARG. */
2623 static rtx
2624 riscv_function_arg (cumulative_args_t cum_v, machine_mode mode,
2625 const_tree type, bool named)
2627 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2628 struct riscv_arg_info info;
2630 if (mode == VOIDmode)
2631 return NULL;
2633 return riscv_get_arg_info (&info, cum, mode, type, named, false);
2636 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
2638 static void
2639 riscv_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
2640 const_tree type, bool named)
2642 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2643 struct riscv_arg_info info;
2645 riscv_get_arg_info (&info, cum, mode, type, named, false);
2647 /* Advance the register count. This has the effect of setting
2648 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
2649 argument required us to skip the final GPR and pass the whole
2650 argument on the stack. */
2651 cum->num_fprs = info.fpr_offset + info.num_fprs;
2652 cum->num_gprs = info.gpr_offset + info.num_gprs;
2655 /* Implement TARGET_ARG_PARTIAL_BYTES. */
2657 static int
2658 riscv_arg_partial_bytes (cumulative_args_t cum,
2659 machine_mode mode, tree type, bool named)
2661 struct riscv_arg_info arg;
2663 riscv_get_arg_info (&arg, get_cumulative_args (cum), mode, type, named, false);
2664 return arg.stack_p ? arg.num_gprs * UNITS_PER_WORD : 0;
2667 /* Implement FUNCTION_VALUE and LIBCALL_VALUE. For normal calls,
2668 VALTYPE is the return type and MODE is VOIDmode. For libcalls,
2669 VALTYPE is null and MODE is the mode of the return value. */
2672 riscv_function_value (const_tree type, const_tree func, machine_mode mode)
2674 struct riscv_arg_info info;
2675 CUMULATIVE_ARGS args;
2677 if (type)
2679 int unsigned_p = TYPE_UNSIGNED (type);
2681 mode = TYPE_MODE (type);
2683 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
2684 return values, promote the mode here too. */
2685 mode = promote_function_mode (type, mode, &unsigned_p, func, 1);
2688 memset (&args, 0, sizeof args);
2689 return riscv_get_arg_info (&info, &args, mode, type, true, true);
2692 /* Implement TARGET_PASS_BY_REFERENCE. */
2694 static bool
2695 riscv_pass_by_reference (cumulative_args_t cum_v, machine_mode mode,
2696 const_tree type, bool named)
2698 HOST_WIDE_INT size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
2699 struct riscv_arg_info info;
2700 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2702 /* ??? std_gimplify_va_arg_expr passes NULL for cum. Fortunately, we
2703 never pass variadic arguments in floating-point registers, so we can
2704 avoid the call to riscv_get_arg_info in this case. */
2705 if (cum != NULL)
2707 /* Don't pass by reference if we can use a floating-point register. */
2708 riscv_get_arg_info (&info, cum, mode, type, named, false);
2709 if (info.num_fprs)
2710 return false;
2713 /* Pass by reference if the data do not fit in two integer registers. */
2714 return !IN_RANGE (size, 0, 2 * UNITS_PER_WORD);
2717 /* Implement TARGET_RETURN_IN_MEMORY. */
2719 static bool
2720 riscv_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
2722 CUMULATIVE_ARGS args;
2723 cumulative_args_t cum = pack_cumulative_args (&args);
2725 /* The rules for returning in memory are the same as for passing the
2726 first named argument by reference. */
2727 memset (&args, 0, sizeof args);
2728 return riscv_pass_by_reference (cum, TYPE_MODE (type), type, true);
2731 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
2733 static void
2734 riscv_setup_incoming_varargs (cumulative_args_t cum, machine_mode mode,
2735 tree type, int *pretend_size ATTRIBUTE_UNUSED,
2736 int no_rtl)
2738 CUMULATIVE_ARGS local_cum;
2739 int gp_saved;
2741 /* The caller has advanced CUM up to, but not beyond, the last named
2742 argument. Advance a local copy of CUM past the last "real" named
2743 argument, to find out how many registers are left over. */
2744 local_cum = *get_cumulative_args (cum);
2745 riscv_function_arg_advance (pack_cumulative_args (&local_cum), mode, type, 1);
2747 /* Found out how many registers we need to save. */
2748 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
2750 if (!no_rtl && gp_saved > 0)
2752 rtx ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
2753 REG_PARM_STACK_SPACE (cfun->decl)
2754 - gp_saved * UNITS_PER_WORD);
2755 rtx mem = gen_frame_mem (BLKmode, ptr);
2756 set_mem_alias_set (mem, get_varargs_alias_set ());
2758 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
2759 mem, gp_saved);
2761 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
2762 cfun->machine->varargs_size = gp_saved * UNITS_PER_WORD;
2765 /* Handle an attribute requiring a FUNCTION_DECL;
2766 arguments as in struct attribute_spec.handler. */
2767 static tree
2768 riscv_handle_fndecl_attribute (tree *node, tree name,
2769 tree args ATTRIBUTE_UNUSED,
2770 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2772 if (TREE_CODE (*node) != FUNCTION_DECL)
2774 warning (OPT_Wattributes, "%qE attribute only applies to functions",
2775 name);
2776 *no_add_attrs = true;
2779 return NULL_TREE;
2782 /* Verify type based attributes. NODE is the what the attribute is being
2783 applied to. NAME is the attribute name. ARGS are the attribute args.
2784 FLAGS gives info about the context. NO_ADD_ATTRS should be set to true if
2785 the attribute should be ignored. */
2787 static tree
2788 riscv_handle_type_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
2789 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2791 /* Check for an argument. */
2792 if (is_attribute_p ("interrupt", name))
2794 if (args)
2796 tree cst = TREE_VALUE (args);
2797 const char *string;
2799 if (TREE_CODE (cst) != STRING_CST)
2801 warning (OPT_Wattributes,
2802 "%qE attribute requires a string argument",
2803 name);
2804 *no_add_attrs = true;
2805 return NULL_TREE;
2808 string = TREE_STRING_POINTER (cst);
2809 if (strcmp (string, "user") && strcmp (string, "supervisor")
2810 && strcmp (string, "machine"))
2812 warning (OPT_Wattributes,
2813 "argument to %qE attribute is not \"user\", \"supervisor\", or \"machine\"",
2814 name);
2815 *no_add_attrs = true;
2820 return NULL_TREE;
2823 /* Return true if function TYPE is an interrupt function. */
2824 static bool
2825 riscv_interrupt_type_p (tree type)
2827 return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
2830 /* Return true if FUNC is a naked function. */
2831 static bool
2832 riscv_naked_function_p (tree func)
2834 tree func_decl = func;
2835 if (func == NULL_TREE)
2836 func_decl = current_function_decl;
2837 return NULL_TREE != lookup_attribute ("naked", DECL_ATTRIBUTES (func_decl));
2840 /* Implement TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS. */
2841 static bool
2842 riscv_allocate_stack_slots_for_args ()
2844 /* Naked functions should not allocate stack slots for arguments. */
2845 return !riscv_naked_function_p (current_function_decl);
2848 /* Implement TARGET_WARN_FUNC_RETURN. */
2849 static bool
2850 riscv_warn_func_return (tree decl)
2852 /* Naked functions are implemented entirely in assembly, including the
2853 return sequence, so suppress warnings about this. */
2854 return !riscv_naked_function_p (decl);
2857 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
2859 static void
2860 riscv_va_start (tree valist, rtx nextarg)
2862 nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
2863 std_expand_builtin_va_start (valist, nextarg);
2866 /* Make ADDR suitable for use as a call or sibcall target. */
2869 riscv_legitimize_call_address (rtx addr)
2871 if (!call_insn_operand (addr, VOIDmode))
2873 rtx reg = RISCV_PROLOGUE_TEMP (Pmode);
2874 riscv_emit_move (reg, addr);
2875 return reg;
2877 return addr;
2880 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
2881 Assume that the areas do not overlap. */
2883 static void
2884 riscv_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
2886 HOST_WIDE_INT offset, delta;
2887 unsigned HOST_WIDE_INT bits;
2888 int i;
2889 enum machine_mode mode;
2890 rtx *regs;
2892 bits = MAX (BITS_PER_UNIT,
2893 MIN (BITS_PER_WORD, MIN (MEM_ALIGN (src), MEM_ALIGN (dest))));
2895 mode = mode_for_size (bits, MODE_INT, 0).require ();
2896 delta = bits / BITS_PER_UNIT;
2898 /* Allocate a buffer for the temporary registers. */
2899 regs = XALLOCAVEC (rtx, length / delta);
2901 /* Load as many BITS-sized chunks as possible. Use a normal load if
2902 the source has enough alignment, otherwise use left/right pairs. */
2903 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
2905 regs[i] = gen_reg_rtx (mode);
2906 riscv_emit_move (regs[i], adjust_address (src, mode, offset));
2909 /* Copy the chunks to the destination. */
2910 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
2911 riscv_emit_move (adjust_address (dest, mode, offset), regs[i]);
2913 /* Mop up any left-over bytes. */
2914 if (offset < length)
2916 src = adjust_address (src, BLKmode, offset);
2917 dest = adjust_address (dest, BLKmode, offset);
2918 move_by_pieces (dest, src, length - offset,
2919 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), RETURN_BEGIN);
2923 /* Helper function for doing a loop-based block operation on memory
2924 reference MEM. Each iteration of the loop will operate on LENGTH
2925 bytes of MEM.
2927 Create a new base register for use within the loop and point it to
2928 the start of MEM. Create a new memory reference that uses this
2929 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
2931 static void
2932 riscv_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
2933 rtx *loop_reg, rtx *loop_mem)
2935 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
2937 /* Although the new mem does not refer to a known location,
2938 it does keep up to LENGTH bytes of alignment. */
2939 *loop_mem = change_address (mem, BLKmode, *loop_reg);
2940 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
2943 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
2944 bytes at a time. LENGTH must be at least BYTES_PER_ITER. Assume that
2945 the memory regions do not overlap. */
2947 static void
2948 riscv_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
2949 HOST_WIDE_INT bytes_per_iter)
2951 rtx label, src_reg, dest_reg, final_src, test;
2952 HOST_WIDE_INT leftover;
2954 leftover = length % bytes_per_iter;
2955 length -= leftover;
2957 /* Create registers and memory references for use within the loop. */
2958 riscv_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
2959 riscv_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
2961 /* Calculate the value that SRC_REG should have after the last iteration
2962 of the loop. */
2963 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
2964 0, 0, OPTAB_WIDEN);
2966 /* Emit the start of the loop. */
2967 label = gen_label_rtx ();
2968 emit_label (label);
2970 /* Emit the loop body. */
2971 riscv_block_move_straight (dest, src, bytes_per_iter);
2973 /* Move on to the next block. */
2974 riscv_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
2975 riscv_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
2977 /* Emit the loop condition. */
2978 test = gen_rtx_NE (VOIDmode, src_reg, final_src);
2979 if (Pmode == DImode)
2980 emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
2981 else
2982 emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
2984 /* Mop up any left-over bytes. */
2985 if (leftover)
2986 riscv_block_move_straight (dest, src, leftover);
2987 else
2988 emit_insn(gen_nop ());
2991 /* Expand a movmemsi instruction, which copies LENGTH bytes from
2992 memory reference SRC to memory reference DEST. */
2994 bool
2995 riscv_expand_block_move (rtx dest, rtx src, rtx length)
2997 if (CONST_INT_P (length))
2999 HOST_WIDE_INT factor, align;
3001 align = MIN (MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), BITS_PER_WORD);
3002 factor = BITS_PER_WORD / align;
3004 if (optimize_function_for_size_p (cfun)
3005 && INTVAL (length) * factor * UNITS_PER_WORD > MOVE_RATIO (false))
3006 return false;
3008 if (INTVAL (length) <= RISCV_MAX_MOVE_BYTES_STRAIGHT / factor)
3010 riscv_block_move_straight (dest, src, INTVAL (length));
3011 return true;
3013 else if (optimize && align >= BITS_PER_WORD)
3015 unsigned min_iter_words
3016 = RISCV_MAX_MOVE_BYTES_PER_LOOP_ITER / UNITS_PER_WORD;
3017 unsigned iter_words = min_iter_words;
3018 HOST_WIDE_INT bytes = INTVAL (length), words = bytes / UNITS_PER_WORD;
3020 /* Lengthen the loop body if it shortens the tail. */
3021 for (unsigned i = min_iter_words; i < min_iter_words * 2 - 1; i++)
3023 unsigned cur_cost = iter_words + words % iter_words;
3024 unsigned new_cost = i + words % i;
3025 if (new_cost <= cur_cost)
3026 iter_words = i;
3029 riscv_block_move_loop (dest, src, bytes, iter_words * UNITS_PER_WORD);
3030 return true;
3033 return false;
3036 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
3037 in context CONTEXT. HI_RELOC indicates a high-part reloc. */
3039 static void
3040 riscv_print_operand_reloc (FILE *file, rtx op, bool hi_reloc)
3042 const char *reloc;
3044 switch (riscv_classify_symbolic_expression (op))
3046 case SYMBOL_ABSOLUTE:
3047 reloc = hi_reloc ? "%hi" : "%lo";
3048 break;
3050 case SYMBOL_PCREL:
3051 reloc = hi_reloc ? "%pcrel_hi" : "%pcrel_lo";
3052 break;
3054 case SYMBOL_TLS_LE:
3055 reloc = hi_reloc ? "%tprel_hi" : "%tprel_lo";
3056 break;
3058 default:
3059 gcc_unreachable ();
3062 fprintf (file, "%s(", reloc);
3063 output_addr_const (file, riscv_strip_unspec_address (op));
3064 fputc (')', file);
3067 /* Return true if the .AQ suffix should be added to an AMO to implement the
3068 acquire portion of memory model MODEL. */
3070 static bool
3071 riscv_memmodel_needs_amo_acquire (enum memmodel model)
3073 switch (model)
3075 case MEMMODEL_ACQ_REL:
3076 case MEMMODEL_SEQ_CST:
3077 case MEMMODEL_SYNC_SEQ_CST:
3078 case MEMMODEL_ACQUIRE:
3079 case MEMMODEL_CONSUME:
3080 case MEMMODEL_SYNC_ACQUIRE:
3081 return true;
3083 case MEMMODEL_RELEASE:
3084 case MEMMODEL_SYNC_RELEASE:
3085 case MEMMODEL_RELAXED:
3086 return false;
3088 default:
3089 gcc_unreachable ();
3093 /* Return true if a FENCE should be emitted to before a memory access to
3094 implement the release portion of memory model MODEL. */
3096 static bool
3097 riscv_memmodel_needs_release_fence (enum memmodel model)
3099 switch (model)
3101 case MEMMODEL_ACQ_REL:
3102 case MEMMODEL_SEQ_CST:
3103 case MEMMODEL_SYNC_SEQ_CST:
3104 case MEMMODEL_RELEASE:
3105 case MEMMODEL_SYNC_RELEASE:
3106 return true;
3108 case MEMMODEL_ACQUIRE:
3109 case MEMMODEL_CONSUME:
3110 case MEMMODEL_SYNC_ACQUIRE:
3111 case MEMMODEL_RELAXED:
3112 return false;
3114 default:
3115 gcc_unreachable ();
3119 /* Implement TARGET_PRINT_OPERAND. The RISCV-specific operand codes are:
3121 'h' Print the high-part relocation associated with OP, after stripping
3122 any outermost HIGH.
3123 'R' Print the low-part relocation associated with OP.
3124 'C' Print the integer branch condition for comparison OP.
3125 'A' Print the atomic operation suffix for memory model OP.
3126 'F' Print a FENCE if the memory model requires a release.
3127 'z' Print x0 if OP is zero, otherwise print OP normally.
3128 'i' Print i if the operand is not a register. */
3130 static void
3131 riscv_print_operand (FILE *file, rtx op, int letter)
3133 machine_mode mode = GET_MODE (op);
3134 enum rtx_code code = GET_CODE (op);
3136 switch (letter)
3138 case 'h':
3139 if (code == HIGH)
3140 op = XEXP (op, 0);
3141 riscv_print_operand_reloc (file, op, true);
3142 break;
3144 case 'R':
3145 riscv_print_operand_reloc (file, op, false);
3146 break;
3148 case 'C':
3149 /* The RTL names match the instruction names. */
3150 fputs (GET_RTX_NAME (code), file);
3151 break;
3153 case 'A':
3154 if (riscv_memmodel_needs_amo_acquire ((enum memmodel) INTVAL (op)))
3155 fputs (".aq", file);
3156 break;
3158 case 'F':
3159 if (riscv_memmodel_needs_release_fence ((enum memmodel) INTVAL (op)))
3160 fputs ("fence iorw,ow; ", file);
3161 break;
3163 case 'i':
3164 if (code != REG)
3165 fputs ("i", file);
3166 break;
3168 default:
3169 switch (code)
3171 case REG:
3172 if (letter && letter != 'z')
3173 output_operand_lossage ("invalid use of '%%%c'", letter);
3174 fprintf (file, "%s", reg_names[REGNO (op)]);
3175 break;
3177 case MEM:
3178 if (letter && letter != 'z')
3179 output_operand_lossage ("invalid use of '%%%c'", letter);
3180 else
3181 output_address (mode, XEXP (op, 0));
3182 break;
3184 default:
3185 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
3186 fputs (reg_names[GP_REG_FIRST], file);
3187 else if (letter && letter != 'z')
3188 output_operand_lossage ("invalid use of '%%%c'", letter);
3189 else
3190 output_addr_const (file, riscv_strip_unspec_address (op));
3191 break;
3196 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
3198 static void
3199 riscv_print_operand_address (FILE *file, machine_mode mode ATTRIBUTE_UNUSED, rtx x)
3201 struct riscv_address_info addr;
3203 if (riscv_classify_address (&addr, x, word_mode, true))
3204 switch (addr.type)
3206 case ADDRESS_REG:
3207 riscv_print_operand (file, addr.offset, 0);
3208 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
3209 return;
3211 case ADDRESS_LO_SUM:
3212 riscv_print_operand_reloc (file, addr.offset, false);
3213 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
3214 return;
3216 case ADDRESS_CONST_INT:
3217 output_addr_const (file, x);
3218 fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
3219 return;
3221 case ADDRESS_SYMBOLIC:
3222 output_addr_const (file, riscv_strip_unspec_address (x));
3223 return;
3225 gcc_unreachable ();
3228 static bool
3229 riscv_size_ok_for_small_data_p (int size)
3231 return g_switch_value && IN_RANGE (size, 1, g_switch_value);
3234 /* Return true if EXP should be placed in the small data section. */
3236 static bool
3237 riscv_in_small_data_p (const_tree x)
3239 if (TREE_CODE (x) == STRING_CST || TREE_CODE (x) == FUNCTION_DECL)
3240 return false;
3242 if (TREE_CODE (x) == VAR_DECL && DECL_SECTION_NAME (x))
3244 const char *sec = DECL_SECTION_NAME (x);
3245 return strcmp (sec, ".sdata") == 0 || strcmp (sec, ".sbss") == 0;
3248 return riscv_size_ok_for_small_data_p (int_size_in_bytes (TREE_TYPE (x)));
3251 /* Switch to the appropriate section for output of DECL. */
3253 static section *
3254 riscv_select_section (tree decl, int reloc,
3255 unsigned HOST_WIDE_INT align)
3257 switch (categorize_decl_for_section (decl, reloc))
3259 case SECCAT_SRODATA:
3260 return get_named_section (decl, ".srodata", reloc);
3262 default:
3263 return default_elf_select_section (decl, reloc, align);
3267 /* Return a section for X, handling small data. */
3269 static section *
3270 riscv_elf_select_rtx_section (machine_mode mode, rtx x,
3271 unsigned HOST_WIDE_INT align)
3273 section *s = default_elf_select_rtx_section (mode, x, align);
3275 if (riscv_size_ok_for_small_data_p (GET_MODE_SIZE (mode)))
3277 if (strncmp (s->named.name, ".rodata.cst", strlen (".rodata.cst")) == 0)
3279 /* Rename .rodata.cst* to .srodata.cst*. */
3280 char *name = (char *) alloca (strlen (s->named.name) + 2);
3281 sprintf (name, ".s%s", s->named.name + 1);
3282 return get_section (name, s->named.common.flags, NULL);
3285 if (s == data_section)
3286 return sdata_section;
3289 return s;
3292 /* Make the last instruction frame-related and note that it performs
3293 the operation described by FRAME_PATTERN. */
3295 static void
3296 riscv_set_frame_expr (rtx frame_pattern)
3298 rtx insn;
3300 insn = get_last_insn ();
3301 RTX_FRAME_RELATED_P (insn) = 1;
3302 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
3303 frame_pattern,
3304 REG_NOTES (insn));
3307 /* Return a frame-related rtx that stores REG at MEM.
3308 REG must be a single register. */
3310 static rtx
3311 riscv_frame_set (rtx mem, rtx reg)
3313 rtx set = gen_rtx_SET (mem, reg);
3314 RTX_FRAME_RELATED_P (set) = 1;
3315 return set;
3318 /* Return true if the current function must save register REGNO. */
3320 static bool
3321 riscv_save_reg_p (unsigned int regno)
3323 bool call_saved = !global_regs[regno] && !call_used_regs[regno];
3324 bool might_clobber = crtl->saves_all_registers
3325 || df_regs_ever_live_p (regno);
3327 if (call_saved && might_clobber)
3328 return true;
3330 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
3331 return true;
3333 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
3334 return true;
3336 /* If this is an interrupt handler, then must save extra registers. */
3337 if (cfun->machine->interrupt_handler_p)
3339 /* zero register is always zero. */
3340 if (regno == GP_REG_FIRST)
3341 return false;
3343 /* The function will return the stack pointer to its original value. */
3344 if (regno == STACK_POINTER_REGNUM)
3345 return false;
3347 /* By convention, we assume that gp and tp are safe. */
3348 if (regno == GP_REGNUM || regno == THREAD_POINTER_REGNUM)
3349 return false;
3351 /* We must save every register used in this function. If this is not a
3352 leaf function, then we must save all temporary registers. */
3353 if (df_regs_ever_live_p (regno)
3354 || (!crtl->is_leaf && call_used_regs[regno]))
3355 return true;
3358 return false;
3361 /* Determine whether to call GPR save/restore routines. */
3362 static bool
3363 riscv_use_save_libcall (const struct riscv_frame_info *frame)
3365 if (!TARGET_SAVE_RESTORE || crtl->calls_eh_return || frame_pointer_needed
3366 || cfun->machine->interrupt_handler_p)
3367 return false;
3369 return frame->save_libcall_adjustment != 0;
3372 /* Determine which GPR save/restore routine to call. */
3374 static unsigned
3375 riscv_save_libcall_count (unsigned mask)
3377 for (unsigned n = GP_REG_LAST; n > GP_REG_FIRST; n--)
3378 if (BITSET_P (mask, n))
3379 return CALLEE_SAVED_REG_NUMBER (n) + 1;
3380 abort ();
3383 /* Populate the current function's riscv_frame_info structure.
3385 RISC-V stack frames grown downward. High addresses are at the top.
3387 +-------------------------------+
3389 | incoming stack arguments |
3391 +-------------------------------+ <-- incoming stack pointer
3393 | callee-allocated save area |
3394 | for arguments that are |
3395 | split between registers and |
3396 | the stack |
3398 +-------------------------------+ <-- arg_pointer_rtx
3400 | callee-allocated save area |
3401 | for register varargs |
3403 +-------------------------------+ <-- hard_frame_pointer_rtx;
3404 | | stack_pointer_rtx + gp_sp_offset
3405 | GPR save area | + UNITS_PER_WORD
3407 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
3408 | | + UNITS_PER_HWVALUE
3409 | FPR save area |
3411 +-------------------------------+ <-- frame_pointer_rtx (virtual)
3413 | local variables |
3415 P +-------------------------------+
3417 | outgoing stack arguments |
3419 +-------------------------------+ <-- stack_pointer_rtx
3421 Dynamic stack allocations such as alloca insert data at point P.
3422 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
3423 hard_frame_pointer_rtx unchanged. */
3425 static HOST_WIDE_INT riscv_first_stack_step (struct riscv_frame_info *frame);
3427 static void
3428 riscv_compute_frame_info (void)
3430 struct riscv_frame_info *frame;
3431 HOST_WIDE_INT offset;
3432 bool interrupt_save_t1 = false;
3433 unsigned int regno, i, num_x_saved = 0, num_f_saved = 0;
3435 frame = &cfun->machine->frame;
3437 /* In an interrupt function, if we have a large frame, then we need to
3438 save/restore t1. We check for this before clearing the frame struct. */
3439 if (cfun->machine->interrupt_handler_p)
3441 HOST_WIDE_INT step1 = riscv_first_stack_step (frame);
3442 if (! SMALL_OPERAND (frame->total_size - step1))
3443 interrupt_save_t1 = true;
3446 memset (frame, 0, sizeof (*frame));
3448 if (!cfun->machine->naked_p)
3450 /* Find out which GPRs we need to save. */
3451 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
3452 if (riscv_save_reg_p (regno)
3453 || (interrupt_save_t1 && (regno == T1_REGNUM)))
3454 frame->mask |= 1 << (regno - GP_REG_FIRST), num_x_saved++;
3456 /* If this function calls eh_return, we must also save and restore the
3457 EH data registers. */
3458 if (crtl->calls_eh_return)
3459 for (i = 0; (regno = EH_RETURN_DATA_REGNO (i)) != INVALID_REGNUM; i++)
3460 frame->mask |= 1 << (regno - GP_REG_FIRST), num_x_saved++;
3462 /* Find out which FPRs we need to save. This loop must iterate over
3463 the same space as its companion in riscv_for_each_saved_reg. */
3464 if (TARGET_HARD_FLOAT)
3465 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
3466 if (riscv_save_reg_p (regno))
3467 frame->fmask |= 1 << (regno - FP_REG_FIRST), num_f_saved++;
3470 /* At the bottom of the frame are any outgoing stack arguments. */
3471 offset = RISCV_STACK_ALIGN (crtl->outgoing_args_size);
3472 /* Next are local stack variables. */
3473 offset += RISCV_STACK_ALIGN (get_frame_size ());
3474 /* The virtual frame pointer points above the local variables. */
3475 frame->frame_pointer_offset = offset;
3476 /* Next are the callee-saved FPRs. */
3477 if (frame->fmask)
3478 offset += RISCV_STACK_ALIGN (num_f_saved * UNITS_PER_FP_REG);
3479 frame->fp_sp_offset = offset - UNITS_PER_FP_REG;
3480 /* Next are the callee-saved GPRs. */
3481 if (frame->mask)
3483 unsigned x_save_size = RISCV_STACK_ALIGN (num_x_saved * UNITS_PER_WORD);
3484 unsigned num_save_restore = 1 + riscv_save_libcall_count (frame->mask);
3486 /* Only use save/restore routines if they don't alter the stack size. */
3487 if (RISCV_STACK_ALIGN (num_save_restore * UNITS_PER_WORD) == x_save_size)
3489 /* Libcall saves/restores 3 registers at once, so we need to
3490 allocate 12 bytes for callee-saved register. */
3491 if (TARGET_RVE)
3492 x_save_size = 3 * UNITS_PER_WORD;
3494 frame->save_libcall_adjustment = x_save_size;
3497 offset += x_save_size;
3499 frame->gp_sp_offset = offset - UNITS_PER_WORD;
3500 /* The hard frame pointer points above the callee-saved GPRs. */
3501 frame->hard_frame_pointer_offset = offset;
3502 /* Above the hard frame pointer is the callee-allocated varags save area. */
3503 offset += RISCV_STACK_ALIGN (cfun->machine->varargs_size);
3504 /* Next is the callee-allocated area for pretend stack arguments. */
3505 offset += RISCV_STACK_ALIGN (crtl->args.pretend_args_size);
3506 /* Arg pointer must be below pretend args, but must be above alignment
3507 padding. */
3508 frame->arg_pointer_offset = offset - crtl->args.pretend_args_size;
3509 frame->total_size = offset;
3510 /* Next points the incoming stack pointer and any incoming arguments. */
3512 /* Only use save/restore routines when the GPRs are atop the frame. */
3513 if (frame->hard_frame_pointer_offset != frame->total_size)
3514 frame->save_libcall_adjustment = 0;
3517 /* Make sure that we're not trying to eliminate to the wrong hard frame
3518 pointer. */
3520 static bool
3521 riscv_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
3523 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
3526 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
3527 or argument pointer. TO is either the stack pointer or hard frame
3528 pointer. */
3530 HOST_WIDE_INT
3531 riscv_initial_elimination_offset (int from, int to)
3533 HOST_WIDE_INT src, dest;
3535 riscv_compute_frame_info ();
3537 if (to == HARD_FRAME_POINTER_REGNUM)
3538 dest = cfun->machine->frame.hard_frame_pointer_offset;
3539 else if (to == STACK_POINTER_REGNUM)
3540 dest = 0; /* The stack pointer is the base of all offsets, hence 0. */
3541 else
3542 gcc_unreachable ();
3544 if (from == FRAME_POINTER_REGNUM)
3545 src = cfun->machine->frame.frame_pointer_offset;
3546 else if (from == ARG_POINTER_REGNUM)
3547 src = cfun->machine->frame.arg_pointer_offset;
3548 else
3549 gcc_unreachable ();
3551 return src - dest;
3554 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
3555 previous frame. */
3558 riscv_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
3560 if (count != 0)
3561 return const0_rtx;
3563 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
3566 /* Emit code to change the current function's return address to
3567 ADDRESS. SCRATCH is available as a scratch register, if needed.
3568 ADDRESS and SCRATCH are both word-mode GPRs. */
3570 void
3571 riscv_set_return_address (rtx address, rtx scratch)
3573 rtx slot_address;
3575 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
3576 slot_address = riscv_add_offset (scratch, stack_pointer_rtx,
3577 cfun->machine->frame.gp_sp_offset);
3578 riscv_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
3581 /* A function to save or store a register. The first argument is the
3582 register and the second is the stack slot. */
3583 typedef void (*riscv_save_restore_fn) (rtx, rtx);
3585 /* Use FN to save or restore register REGNO. MODE is the register's
3586 mode and OFFSET is the offset of its save slot from the current
3587 stack pointer. */
3589 static void
3590 riscv_save_restore_reg (machine_mode mode, int regno,
3591 HOST_WIDE_INT offset, riscv_save_restore_fn fn)
3593 rtx mem;
3595 mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx, offset));
3596 fn (gen_rtx_REG (mode, regno), mem);
3599 /* Call FN for each register that is saved by the current function.
3600 SP_OFFSET is the offset of the current stack pointer from the start
3601 of the frame. */
3603 static void
3604 riscv_for_each_saved_reg (HOST_WIDE_INT sp_offset, riscv_save_restore_fn fn,
3605 bool epilogue, bool maybe_eh_return)
3607 HOST_WIDE_INT offset;
3609 /* Save the link register and s-registers. */
3610 offset = cfun->machine->frame.gp_sp_offset - sp_offset;
3611 for (unsigned int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
3612 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
3614 bool handle_reg = TRUE;
3616 /* If this is a normal return in a function that calls the eh_return
3617 builtin, then do not restore the eh return data registers as that
3618 would clobber the return value. But we do still need to save them
3619 in the prologue, and restore them for an exception return, so we
3620 need special handling here. */
3621 if (epilogue && !maybe_eh_return && crtl->calls_eh_return)
3623 unsigned int i, regnum;
3625 for (i = 0; (regnum = EH_RETURN_DATA_REGNO (i)) != INVALID_REGNUM;
3626 i++)
3627 if (regno == regnum)
3629 handle_reg = FALSE;
3630 break;
3634 if (handle_reg)
3635 riscv_save_restore_reg (word_mode, regno, offset, fn);
3636 offset -= UNITS_PER_WORD;
3639 /* This loop must iterate over the same space as its companion in
3640 riscv_compute_frame_info. */
3641 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
3642 for (unsigned int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
3643 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
3645 machine_mode mode = TARGET_DOUBLE_FLOAT ? DFmode : SFmode;
3647 riscv_save_restore_reg (mode, regno, offset, fn);
3648 offset -= GET_MODE_SIZE (mode);
3652 /* Save register REG to MEM. Make the instruction frame-related. */
3654 static void
3655 riscv_save_reg (rtx reg, rtx mem)
3657 riscv_emit_move (mem, reg);
3658 riscv_set_frame_expr (riscv_frame_set (mem, reg));
3661 /* Restore register REG from MEM. */
3663 static void
3664 riscv_restore_reg (rtx reg, rtx mem)
3666 rtx insn = riscv_emit_move (reg, mem);
3667 rtx dwarf = NULL_RTX;
3668 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
3670 if (epilogue_cfa_sp_offset && REGNO (reg) == HARD_FRAME_POINTER_REGNUM)
3672 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3673 GEN_INT (epilogue_cfa_sp_offset));
3674 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
3677 REG_NOTES (insn) = dwarf;
3678 RTX_FRAME_RELATED_P (insn) = 1;
3681 /* Return the code to invoke the GPR save routine. */
3683 const char *
3684 riscv_output_gpr_save (unsigned mask)
3686 static char s[32];
3687 unsigned n = riscv_save_libcall_count (mask);
3689 ssize_t bytes = snprintf (s, sizeof (s), "call\tt0,__riscv_save_%u", n);
3690 gcc_assert ((size_t) bytes < sizeof (s));
3692 return s;
3695 /* For stack frames that can't be allocated with a single ADDI instruction,
3696 compute the best value to initially allocate. It must at a minimum
3697 allocate enough space to spill the callee-saved registers. If TARGET_RVC,
3698 try to pick a value that will allow compression of the register saves
3699 without adding extra instructions. */
3701 static HOST_WIDE_INT
3702 riscv_first_stack_step (struct riscv_frame_info *frame)
3704 if (SMALL_OPERAND (frame->total_size))
3705 return frame->total_size;
3707 HOST_WIDE_INT min_first_step =
3708 RISCV_STACK_ALIGN (frame->total_size - frame->fp_sp_offset);
3709 HOST_WIDE_INT max_first_step = IMM_REACH / 2 - PREFERRED_STACK_BOUNDARY / 8;
3710 HOST_WIDE_INT min_second_step = frame->total_size - max_first_step;
3711 gcc_assert (min_first_step <= max_first_step);
3713 /* As an optimization, use the least-significant bits of the total frame
3714 size, so that the second adjustment step is just LUI + ADD. */
3715 if (!SMALL_OPERAND (min_second_step)
3716 && frame->total_size % IMM_REACH < IMM_REACH / 2
3717 && frame->total_size % IMM_REACH >= min_first_step)
3718 return frame->total_size % IMM_REACH;
3720 if (TARGET_RVC)
3722 /* If we need two subtracts, and one is small enough to allow compressed
3723 loads and stores, then put that one first. */
3724 if (IN_RANGE (min_second_step, 0,
3725 (TARGET_64BIT ? SDSP_REACH : SWSP_REACH)))
3726 return MAX (min_second_step, min_first_step);
3728 /* If we need LUI + ADDI + ADD for the second adjustment step, then start
3729 with the minimum first step, so that we can get compressed loads and
3730 stores. */
3731 else if (!SMALL_OPERAND (min_second_step))
3732 return min_first_step;
3735 return max_first_step;
3738 static rtx
3739 riscv_adjust_libcall_cfi_prologue ()
3741 rtx dwarf = NULL_RTX;
3742 rtx adjust_sp_rtx, reg, mem, insn;
3743 int saved_size = cfun->machine->frame.save_libcall_adjustment;
3744 int offset;
3746 for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
3747 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
3749 /* The save order is ra, s0, s1, s2 to s11. */
3750 if (regno == RETURN_ADDR_REGNUM)
3751 offset = saved_size - UNITS_PER_WORD;
3752 else if (regno == S0_REGNUM)
3753 offset = saved_size - UNITS_PER_WORD * 2;
3754 else if (regno == S1_REGNUM)
3755 offset = saved_size - UNITS_PER_WORD * 3;
3756 else
3757 offset = saved_size - ((regno - S2_REGNUM + 4) * UNITS_PER_WORD);
3759 reg = gen_rtx_REG (SImode, regno);
3760 mem = gen_frame_mem (SImode, plus_constant (Pmode,
3761 stack_pointer_rtx,
3762 offset));
3764 insn = gen_rtx_SET (mem, reg);
3765 dwarf = alloc_reg_note (REG_CFA_OFFSET, insn, dwarf);
3768 /* Debug info for adjust sp. */
3769 adjust_sp_rtx = gen_add3_insn (stack_pointer_rtx,
3770 stack_pointer_rtx, GEN_INT (-saved_size));
3771 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, adjust_sp_rtx,
3772 dwarf);
3773 return dwarf;
3776 static void
3777 riscv_emit_stack_tie (void)
3779 if (Pmode == SImode)
3780 emit_insn (gen_stack_tiesi (stack_pointer_rtx, hard_frame_pointer_rtx));
3781 else
3782 emit_insn (gen_stack_tiedi (stack_pointer_rtx, hard_frame_pointer_rtx));
3785 /* Expand the "prologue" pattern. */
3787 void
3788 riscv_expand_prologue (void)
3790 struct riscv_frame_info *frame = &cfun->machine->frame;
3791 HOST_WIDE_INT size = frame->total_size;
3792 unsigned mask = frame->mask;
3793 rtx insn;
3795 if (flag_stack_usage_info)
3796 current_function_static_stack_size = size;
3798 if (cfun->machine->naked_p)
3799 return;
3801 /* When optimizing for size, call a subroutine to save the registers. */
3802 if (riscv_use_save_libcall (frame))
3804 rtx dwarf = NULL_RTX;
3805 dwarf = riscv_adjust_libcall_cfi_prologue ();
3807 frame->mask = 0; /* Temporarily fib that we need not save GPRs. */
3808 size -= frame->save_libcall_adjustment;
3809 insn = emit_insn (gen_gpr_save (GEN_INT (mask)));
3811 RTX_FRAME_RELATED_P (insn) = 1;
3812 REG_NOTES (insn) = dwarf;
3815 /* Save the registers. */
3816 if ((frame->mask | frame->fmask) != 0)
3818 HOST_WIDE_INT step1 = MIN (size, riscv_first_stack_step (frame));
3820 insn = gen_add3_insn (stack_pointer_rtx,
3821 stack_pointer_rtx,
3822 GEN_INT (-step1));
3823 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
3824 size -= step1;
3825 riscv_for_each_saved_reg (size, riscv_save_reg, false, false);
3828 frame->mask = mask; /* Undo the above fib. */
3830 /* Set up the frame pointer, if we're using one. */
3831 if (frame_pointer_needed)
3833 insn = gen_add3_insn (hard_frame_pointer_rtx, stack_pointer_rtx,
3834 GEN_INT (frame->hard_frame_pointer_offset - size));
3835 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
3837 riscv_emit_stack_tie ();
3840 /* Allocate the rest of the frame. */
3841 if (size > 0)
3843 if (SMALL_OPERAND (-size))
3845 insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
3846 GEN_INT (-size));
3847 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
3849 else
3851 riscv_emit_move (RISCV_PROLOGUE_TEMP (Pmode), GEN_INT (-size));
3852 emit_insn (gen_add3_insn (stack_pointer_rtx,
3853 stack_pointer_rtx,
3854 RISCV_PROLOGUE_TEMP (Pmode)));
3856 /* Describe the effect of the previous instructions. */
3857 insn = plus_constant (Pmode, stack_pointer_rtx, -size);
3858 insn = gen_rtx_SET (stack_pointer_rtx, insn);
3859 riscv_set_frame_expr (insn);
3864 static rtx
3865 riscv_adjust_libcall_cfi_epilogue ()
3867 rtx dwarf = NULL_RTX;
3868 rtx adjust_sp_rtx, reg;
3869 int saved_size = cfun->machine->frame.save_libcall_adjustment;
3871 /* Debug info for adjust sp. */
3872 adjust_sp_rtx = gen_add3_insn (stack_pointer_rtx,
3873 stack_pointer_rtx, GEN_INT (saved_size));
3874 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, adjust_sp_rtx,
3875 dwarf);
3877 for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
3878 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
3880 reg = gen_rtx_REG (SImode, regno);
3881 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
3884 return dwarf;
3887 /* Expand an "epilogue", "sibcall_epilogue", or "eh_return_internal" pattern;
3888 style says which. */
3890 void
3891 riscv_expand_epilogue (int style)
3893 /* Split the frame into two. STEP1 is the amount of stack we should
3894 deallocate before restoring the registers. STEP2 is the amount we
3895 should deallocate afterwards.
3897 Start off by assuming that no registers need to be restored. */
3898 struct riscv_frame_info *frame = &cfun->machine->frame;
3899 unsigned mask = frame->mask;
3900 HOST_WIDE_INT step1 = frame->total_size;
3901 HOST_WIDE_INT step2 = 0;
3902 bool use_restore_libcall = ((style == NORMAL_RETURN)
3903 && riscv_use_save_libcall (frame));
3904 rtx ra = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
3905 rtx insn;
3907 /* We need to add memory barrier to prevent read from deallocated stack. */
3908 bool need_barrier_p = (get_frame_size ()
3909 + cfun->machine->frame.arg_pointer_offset) != 0;
3911 if (cfun->machine->naked_p)
3913 gcc_assert (style == NORMAL_RETURN);
3915 emit_jump_insn (gen_return ());
3917 return;
3920 if ((style == NORMAL_RETURN) && riscv_can_use_return_insn ())
3922 emit_jump_insn (gen_return ());
3923 return;
3926 /* Reset the epilogue cfa info before starting to emit the epilogue. */
3927 epilogue_cfa_sp_offset = 0;
3929 /* Move past any dynamic stack allocations. */
3930 if (cfun->calls_alloca)
3932 /* Emit a barrier to prevent loads from a deallocated stack. */
3933 riscv_emit_stack_tie ();
3934 need_barrier_p = false;
3936 rtx adjust = GEN_INT (-frame->hard_frame_pointer_offset);
3937 if (!SMALL_OPERAND (INTVAL (adjust)))
3939 riscv_emit_move (RISCV_PROLOGUE_TEMP (Pmode), adjust);
3940 adjust = RISCV_PROLOGUE_TEMP (Pmode);
3943 insn = emit_insn (
3944 gen_add3_insn (stack_pointer_rtx, hard_frame_pointer_rtx,
3945 adjust));
3947 rtx dwarf = NULL_RTX;
3948 rtx cfa_adjust_value = gen_rtx_PLUS (
3949 Pmode, hard_frame_pointer_rtx,
3950 GEN_INT (-frame->hard_frame_pointer_offset));
3951 rtx cfa_adjust_rtx = gen_rtx_SET (stack_pointer_rtx, cfa_adjust_value);
3952 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, cfa_adjust_rtx, dwarf);
3953 RTX_FRAME_RELATED_P (insn) = 1;
3955 REG_NOTES (insn) = dwarf;
3958 /* If we need to restore registers, deallocate as much stack as
3959 possible in the second step without going out of range. */
3960 if ((frame->mask | frame->fmask) != 0)
3962 step2 = riscv_first_stack_step (frame);
3963 step1 -= step2;
3966 /* Set TARGET to BASE + STEP1. */
3967 if (step1 > 0)
3969 /* Emit a barrier to prevent loads from a deallocated stack. */
3970 riscv_emit_stack_tie ();
3971 need_barrier_p = false;
3973 /* Get an rtx for STEP1 that we can add to BASE. */
3974 rtx adjust = GEN_INT (step1);
3975 if (!SMALL_OPERAND (step1))
3977 riscv_emit_move (RISCV_PROLOGUE_TEMP (Pmode), adjust);
3978 adjust = RISCV_PROLOGUE_TEMP (Pmode);
3981 insn = emit_insn (
3982 gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx, adjust));
3984 rtx dwarf = NULL_RTX;
3985 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3986 GEN_INT (step2));
3988 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
3989 RTX_FRAME_RELATED_P (insn) = 1;
3991 REG_NOTES (insn) = dwarf;
3993 else if (frame_pointer_needed)
3995 /* Tell riscv_restore_reg to emit dwarf to redefine CFA when restoring
3996 old value of FP. */
3997 epilogue_cfa_sp_offset = step2;
4000 if (use_restore_libcall)
4001 frame->mask = 0; /* Temporarily fib that we need not save GPRs. */
4003 /* Restore the registers. */
4004 riscv_for_each_saved_reg (frame->total_size - step2, riscv_restore_reg,
4005 true, style == EXCEPTION_RETURN);
4007 if (use_restore_libcall)
4009 frame->mask = mask; /* Undo the above fib. */
4010 gcc_assert (step2 >= frame->save_libcall_adjustment);
4011 step2 -= frame->save_libcall_adjustment;
4014 if (need_barrier_p)
4015 riscv_emit_stack_tie ();
4017 /* Deallocate the final bit of the frame. */
4018 if (step2 > 0)
4020 insn = emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
4021 GEN_INT (step2)));
4023 rtx dwarf = NULL_RTX;
4024 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4025 const0_rtx);
4026 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
4027 RTX_FRAME_RELATED_P (insn) = 1;
4029 REG_NOTES (insn) = dwarf;
4032 if (use_restore_libcall)
4034 rtx dwarf = riscv_adjust_libcall_cfi_epilogue ();
4035 insn = emit_insn (gen_gpr_restore (GEN_INT (riscv_save_libcall_count (mask))));
4036 RTX_FRAME_RELATED_P (insn) = 1;
4037 REG_NOTES (insn) = dwarf;
4039 emit_jump_insn (gen_gpr_restore_return (ra));
4040 return;
4043 /* Add in the __builtin_eh_return stack adjustment. */
4044 if ((style == EXCEPTION_RETURN) && crtl->calls_eh_return)
4045 emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
4046 EH_RETURN_STACKADJ_RTX));
4048 /* Return from interrupt. */
4049 if (cfun->machine->interrupt_handler_p)
4051 enum riscv_privilege_levels mode = cfun->machine->interrupt_mode;
4053 gcc_assert (mode != UNKNOWN_MODE);
4055 if (mode == MACHINE_MODE)
4056 emit_jump_insn (gen_riscv_mret ());
4057 else if (mode == SUPERVISOR_MODE)
4058 emit_jump_insn (gen_riscv_sret ());
4059 else
4060 emit_jump_insn (gen_riscv_uret ());
4062 else if (style != SIBCALL_RETURN)
4063 emit_jump_insn (gen_simple_return_internal (ra));
4066 /* Implement EPILOGUE_USES. */
4068 bool
4069 riscv_epilogue_uses (unsigned int regno)
4071 if (regno == RETURN_ADDR_REGNUM)
4072 return true;
4074 if (epilogue_completed && cfun->machine->interrupt_handler_p)
4076 /* An interrupt function restores temp regs, so we must indicate that
4077 they are live at function end. */
4078 if (df_regs_ever_live_p (regno)
4079 || (!crtl->is_leaf && call_used_regs[regno]))
4080 return true;
4083 return false;
4086 /* Return nonzero if this function is known to have a null epilogue.
4087 This allows the optimizer to omit jumps to jumps if no stack
4088 was created. */
4090 bool
4091 riscv_can_use_return_insn (void)
4093 return (reload_completed && cfun->machine->frame.total_size == 0
4094 && ! cfun->machine->interrupt_handler_p);
4097 /* Implement TARGET_SECONDARY_MEMORY_NEEDED.
4099 When floating-point registers are wider than integer ones, moves between
4100 them must go through memory. */
4102 static bool
4103 riscv_secondary_memory_needed (machine_mode mode, reg_class_t class1,
4104 reg_class_t class2)
4106 return (GET_MODE_SIZE (mode) > UNITS_PER_WORD
4107 && (class1 == FP_REGS) != (class2 == FP_REGS));
4110 /* Implement TARGET_REGISTER_MOVE_COST. */
4112 static int
4113 riscv_register_move_cost (machine_mode mode,
4114 reg_class_t from, reg_class_t to)
4116 return riscv_secondary_memory_needed (mode, from, to) ? 8 : 2;
4119 /* Implement TARGET_HARD_REGNO_NREGS. */
4121 static unsigned int
4122 riscv_hard_regno_nregs (unsigned int regno, machine_mode mode)
4124 if (FP_REG_P (regno))
4125 return (GET_MODE_SIZE (mode) + UNITS_PER_FP_REG - 1) / UNITS_PER_FP_REG;
4127 /* All other registers are word-sized. */
4128 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4131 /* Implement TARGET_HARD_REGNO_MODE_OK. */
4133 static bool
4134 riscv_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
4136 unsigned int nregs = riscv_hard_regno_nregs (regno, mode);
4138 if (GP_REG_P (regno))
4140 if (!GP_REG_P (regno + nregs - 1))
4141 return false;
4143 else if (FP_REG_P (regno))
4145 if (!FP_REG_P (regno + nregs - 1))
4146 return false;
4148 if (GET_MODE_CLASS (mode) != MODE_FLOAT
4149 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
4150 return false;
4152 /* Only use callee-saved registers if a potential callee is guaranteed
4153 to spill the requisite width. */
4154 if (GET_MODE_UNIT_SIZE (mode) > UNITS_PER_FP_REG
4155 || (!call_used_regs[regno]
4156 && GET_MODE_UNIT_SIZE (mode) > UNITS_PER_FP_ARG))
4157 return false;
4159 else
4160 return false;
4162 /* Require same callee-savedness for all registers. */
4163 for (unsigned i = 1; i < nregs; i++)
4164 if (call_used_regs[regno] != call_used_regs[regno + i])
4165 return false;
4167 return true;
4170 /* Implement TARGET_MODES_TIEABLE_P.
4172 Don't allow floating-point modes to be tied, since type punning of
4173 single-precision and double-precision is implementation defined. */
4175 static bool
4176 riscv_modes_tieable_p (machine_mode mode1, machine_mode mode2)
4178 return (mode1 == mode2
4179 || !(GET_MODE_CLASS (mode1) == MODE_FLOAT
4180 && GET_MODE_CLASS (mode2) == MODE_FLOAT));
4183 /* Implement CLASS_MAX_NREGS. */
4185 static unsigned char
4186 riscv_class_max_nregs (reg_class_t rclass, machine_mode mode)
4188 if (reg_class_subset_p (FP_REGS, rclass))
4189 return riscv_hard_regno_nregs (FP_REG_FIRST, mode);
4191 if (reg_class_subset_p (GR_REGS, rclass))
4192 return riscv_hard_regno_nregs (GP_REG_FIRST, mode);
4194 return 0;
4197 /* Implement TARGET_MEMORY_MOVE_COST. */
4199 static int
4200 riscv_memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
4202 return (tune_info->memory_cost
4203 + memory_move_secondary_cost (mode, rclass, in));
4206 /* Return the number of instructions that can be issued per cycle. */
4208 static int
4209 riscv_issue_rate (void)
4211 return tune_info->issue_rate;
4214 /* Auxiliary function to emit RISC-V ELF attribute. */
4215 static void
4216 riscv_emit_attribute ()
4218 fprintf (asm_out_file, "\t.attribute arch, \"%s\"\n",
4219 riscv_arch_str ().c_str ());
4221 fprintf (asm_out_file, "\t.attribute unaligned_access, %d\n",
4222 TARGET_STRICT_ALIGN ? 0 : 1);
4224 fprintf (asm_out_file, "\t.attribute stack_align, %d\n",
4225 riscv_stack_boundary / 8);
4228 /* Implement TARGET_ASM_FILE_START. */
4230 static void
4231 riscv_file_start (void)
4233 default_file_start ();
4235 /* Instruct GAS to generate position-[in]dependent code. */
4236 fprintf (asm_out_file, "\t.option %spic\n", (flag_pic ? "" : "no"));
4238 /* If the user specifies "-mno-relax" on the command line then disable linker
4239 relaxation in the assembler. */
4240 if (! riscv_mrelax)
4241 fprintf (asm_out_file, "\t.option norelax\n");
4243 if (riscv_emit_attribute_p)
4244 riscv_emit_attribute ();
4247 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
4248 in order to avoid duplicating too much logic from elsewhere. */
4250 static void
4251 riscv_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
4252 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
4253 tree function)
4255 rtx this_rtx, temp1, temp2, fnaddr;
4256 rtx_insn *insn;
4258 /* Pretend to be a post-reload pass while generating rtl. */
4259 reload_completed = 1;
4261 /* Mark the end of the (empty) prologue. */
4262 emit_note (NOTE_INSN_PROLOGUE_END);
4264 /* Determine if we can use a sibcall to call FUNCTION directly. */
4265 fnaddr = gen_rtx_MEM (FUNCTION_MODE, XEXP (DECL_RTL (function), 0));
4267 /* We need two temporary registers in some cases. */
4268 temp1 = gen_rtx_REG (Pmode, RISCV_PROLOGUE_TEMP_REGNUM);
4269 temp2 = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
4271 /* Find out which register contains the "this" pointer. */
4272 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
4273 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
4274 else
4275 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
4277 /* Add DELTA to THIS_RTX. */
4278 if (delta != 0)
4280 rtx offset = GEN_INT (delta);
4281 if (!SMALL_OPERAND (delta))
4283 riscv_emit_move (temp1, offset);
4284 offset = temp1;
4286 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
4289 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
4290 if (vcall_offset != 0)
4292 rtx addr;
4294 /* Set TEMP1 to *THIS_RTX. */
4295 riscv_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
4297 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
4298 addr = riscv_add_offset (temp2, temp1, vcall_offset);
4300 /* Load the offset and add it to THIS_RTX. */
4301 riscv_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
4302 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
4305 /* Jump to the target function. */
4306 insn = emit_call_insn (gen_sibcall (fnaddr, const0_rtx, NULL, const0_rtx));
4307 SIBLING_CALL_P (insn) = 1;
4309 /* Run just enough of rest_of_compilation. This sequence was
4310 "borrowed" from alpha.c. */
4311 insn = get_insns ();
4312 split_all_insns_noflow ();
4313 shorten_branches (insn);
4314 final_start_function (insn, file, 1);
4315 final (insn, file, 1);
4316 final_end_function ();
4318 /* Clean up the vars set above. Note that final_end_function resets
4319 the global pointer for us. */
4320 reload_completed = 0;
4323 /* Allocate a chunk of memory for per-function machine-dependent data. */
4325 static struct machine_function *
4326 riscv_init_machine_status (void)
4328 return ggc_cleared_alloc<machine_function> ();
4331 /* Implement TARGET_OPTION_OVERRIDE. */
4333 static void
4334 riscv_option_override (void)
4336 const struct riscv_cpu_info *cpu;
4338 #ifdef SUBTARGET_OVERRIDE_OPTIONS
4339 SUBTARGET_OVERRIDE_OPTIONS;
4340 #endif
4342 flag_pcc_struct_return = 0;
4344 if (flag_pic)
4345 g_switch_value = 0;
4347 /* The presence of the M extension implies that division instructions
4348 are present, so include them unless explicitly disabled. */
4349 if (TARGET_MUL && (target_flags_explicit & MASK_DIV) == 0)
4350 target_flags |= MASK_DIV;
4351 else if (!TARGET_MUL && TARGET_DIV)
4352 error ("%<-mdiv%> requires %<-march%> to subsume the %<M%> extension");
4354 /* Likewise floating-point division and square root. */
4355 if (TARGET_HARD_FLOAT && (target_flags_explicit & MASK_FDIV) == 0)
4356 target_flags |= MASK_FDIV;
4358 /* Handle -mtune. */
4359 cpu = riscv_parse_cpu (riscv_tune_string ? riscv_tune_string :
4360 RISCV_TUNE_STRING_DEFAULT);
4361 tune_info = optimize_size ? &optimize_size_tune_info : cpu->tune_info;
4363 /* Use -mtune's setting for slow_unaligned_access, even when optimizing
4364 for size. For architectures that trap and emulate unaligned accesses,
4365 the performance cost is too great, even for -Os. Similarly, if
4366 -m[no-]strict-align is left unspecified, heed -mtune's advice. */
4367 riscv_slow_unaligned_access_p = (cpu->tune_info->slow_unaligned_access
4368 || TARGET_STRICT_ALIGN);
4369 if ((target_flags_explicit & MASK_STRICT_ALIGN) == 0
4370 && cpu->tune_info->slow_unaligned_access)
4371 target_flags |= MASK_STRICT_ALIGN;
4373 /* If the user hasn't specified a branch cost, use the processor's
4374 default. */
4375 if (riscv_branch_cost == 0)
4376 riscv_branch_cost = tune_info->branch_cost;
4378 /* Function to allocate machine-dependent function status. */
4379 init_machine_status = &riscv_init_machine_status;
4381 if (flag_pic)
4382 riscv_cmodel = CM_PIC;
4384 /* We get better code with explicit relocs for CM_MEDLOW, but
4385 worse code for the others (for now). Pick the best default. */
4386 if ((target_flags_explicit & MASK_EXPLICIT_RELOCS) == 0)
4387 if (riscv_cmodel == CM_MEDLOW)
4388 target_flags |= MASK_EXPLICIT_RELOCS;
4390 /* Require that the ISA supports the requested floating-point ABI. */
4391 if (UNITS_PER_FP_ARG > (TARGET_HARD_FLOAT ? UNITS_PER_FP_REG : 0))
4392 error ("requested ABI requires %<-march%> to subsume the %qc extension",
4393 UNITS_PER_FP_ARG > 8 ? 'Q' : (UNITS_PER_FP_ARG > 4 ? 'D' : 'F'));
4395 if (TARGET_RVE && riscv_abi != ABI_ILP32E)
4396 error ("rv32e requires ilp32e ABI");
4398 /* We do not yet support ILP32 on RV64. */
4399 if (BITS_PER_WORD != POINTER_SIZE)
4400 error ("ABI requires %<-march=rv%d%>", POINTER_SIZE);
4402 /* Validate -mpreferred-stack-boundary= value. */
4403 riscv_stack_boundary = ABI_STACK_BOUNDARY;
4404 if (riscv_preferred_stack_boundary_arg)
4406 int min = ctz_hwi (STACK_BOUNDARY / 8);
4407 int max = 8;
4409 if (!IN_RANGE (riscv_preferred_stack_boundary_arg, min, max))
4410 error ("%<-mpreferred-stack-boundary=%d%> must be between %d and %d",
4411 riscv_preferred_stack_boundary_arg, min, max);
4413 riscv_stack_boundary = 8 << riscv_preferred_stack_boundary_arg;
4416 if (riscv_emit_attribute_p < 0)
4417 #ifdef HAVE_AS_RISCV_ATTRIBUTE
4418 riscv_emit_attribute_p = TARGET_RISCV_ATTRIBUTE;
4419 #else
4420 riscv_emit_attribute_p = 0;
4422 if (riscv_emit_attribute_p)
4423 error ("%<-mriscv-attribute%> RISC-V ELF attribute requires GNU as 2.32"
4424 " [%<-mriscv-attribute%>]");
4425 #endif
4428 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
4430 static void
4431 riscv_conditional_register_usage (void)
4433 /* We have only x0~x15 on RV32E. */
4434 if (TARGET_RVE)
4436 for (int r = 16; r <= 31; r++)
4437 fixed_regs[r] = 1;
4440 if (riscv_abi == ABI_ILP32E)
4442 for (int r = 16; r <= 31; r++)
4443 call_used_regs[r] = 1;
4446 if (!TARGET_HARD_FLOAT)
4448 for (int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
4449 fixed_regs[regno] = call_used_regs[regno] = 1;
4452 /* In the soft-float ABI, there are no callee-saved FP registers. */
4453 if (UNITS_PER_FP_ARG == 0)
4455 for (int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
4456 call_used_regs[regno] = 1;
4460 /* Return a register priority for hard reg REGNO. */
4462 static int
4463 riscv_register_priority (int regno)
4465 /* Favor x8-x15/f8-f15 to improve the odds of RVC instruction selection. */
4466 if (TARGET_RVC && (IN_RANGE (regno, GP_REG_FIRST + 8, GP_REG_FIRST + 15)
4467 || IN_RANGE (regno, FP_REG_FIRST + 8, FP_REG_FIRST + 15)))
4468 return 1;
4470 return 0;
4473 /* Implement TARGET_TRAMPOLINE_INIT. */
4475 static void
4476 riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
4478 rtx addr, end_addr, mem;
4479 uint32_t trampoline[4];
4480 unsigned int i;
4481 HOST_WIDE_INT static_chain_offset, target_function_offset;
4483 /* Work out the offsets of the pointers from the start of the
4484 trampoline code. */
4485 gcc_assert (ARRAY_SIZE (trampoline) * 4 == TRAMPOLINE_CODE_SIZE);
4487 /* Get pointers to the beginning and end of the code block. */
4488 addr = force_reg (Pmode, XEXP (m_tramp, 0));
4489 end_addr = riscv_force_binary (Pmode, PLUS, addr,
4490 GEN_INT (TRAMPOLINE_CODE_SIZE));
4493 if (Pmode == SImode)
4495 chain_value = force_reg (Pmode, chain_value);
4497 rtx target_function = force_reg (Pmode, XEXP (DECL_RTL (fndecl), 0));
4498 /* lui t2, hi(chain)
4499 lui t1, hi(func)
4500 addi t2, t2, lo(chain)
4501 jr r1, lo(func)
4503 unsigned HOST_WIDE_INT lui_hi_chain_code, lui_hi_func_code;
4504 unsigned HOST_WIDE_INT lo_chain_code, lo_func_code;
4506 rtx uimm_mask = force_reg (SImode, gen_int_mode (-IMM_REACH, SImode));
4508 /* 0xfff. */
4509 rtx imm12_mask = gen_reg_rtx (SImode);
4510 emit_insn (gen_one_cmplsi2 (imm12_mask, uimm_mask));
4512 rtx fixup_value = force_reg (SImode, gen_int_mode (IMM_REACH/2, SImode));
4514 /* Gen lui t2, hi(chain). */
4515 rtx hi_chain = riscv_force_binary (SImode, PLUS, chain_value,
4516 fixup_value);
4517 hi_chain = riscv_force_binary (SImode, AND, hi_chain,
4518 uimm_mask);
4519 lui_hi_chain_code = OPCODE_LUI | (STATIC_CHAIN_REGNUM << SHIFT_RD);
4520 rtx lui_hi_chain = riscv_force_binary (SImode, IOR, hi_chain,
4521 gen_int_mode (lui_hi_chain_code, SImode));
4523 mem = adjust_address (m_tramp, SImode, 0);
4524 riscv_emit_move (mem, lui_hi_chain);
4526 /* Gen lui t1, hi(func). */
4527 rtx hi_func = riscv_force_binary (SImode, PLUS, target_function,
4528 fixup_value);
4529 hi_func = riscv_force_binary (SImode, AND, hi_func,
4530 uimm_mask);
4531 lui_hi_func_code = OPCODE_LUI | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RD);
4532 rtx lui_hi_func = riscv_force_binary (SImode, IOR, hi_func,
4533 gen_int_mode (lui_hi_func_code, SImode));
4535 mem = adjust_address (m_tramp, SImode, 1 * GET_MODE_SIZE (SImode));
4536 riscv_emit_move (mem, lui_hi_func);
4538 /* Gen addi t2, t2, lo(chain). */
4539 rtx lo_chain = riscv_force_binary (SImode, AND, chain_value,
4540 imm12_mask);
4541 lo_chain = riscv_force_binary (SImode, ASHIFT, lo_chain, GEN_INT (20));
4543 lo_chain_code = OPCODE_ADDI
4544 | (STATIC_CHAIN_REGNUM << SHIFT_RD)
4545 | (STATIC_CHAIN_REGNUM << SHIFT_RS1);
4547 rtx addi_lo_chain = riscv_force_binary (SImode, IOR, lo_chain,
4548 force_reg (SImode, GEN_INT (lo_chain_code)));
4550 mem = adjust_address (m_tramp, SImode, 2 * GET_MODE_SIZE (SImode));
4551 riscv_emit_move (mem, addi_lo_chain);
4553 /* Gen jr r1, lo(func). */
4554 rtx lo_func = riscv_force_binary (SImode, AND, target_function,
4555 imm12_mask);
4556 lo_func = riscv_force_binary (SImode, ASHIFT, lo_func, GEN_INT (20));
4558 lo_func_code = OPCODE_JALR | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RS1);
4560 rtx jr_lo_func = riscv_force_binary (SImode, IOR, lo_func,
4561 force_reg (SImode, GEN_INT (lo_func_code)));
4563 mem = adjust_address (m_tramp, SImode, 3 * GET_MODE_SIZE (SImode));
4564 riscv_emit_move (mem, jr_lo_func);
4566 else
4568 static_chain_offset = TRAMPOLINE_CODE_SIZE;
4569 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
4571 /* auipc t2, 0
4572 l[wd] t1, target_function_offset(t2)
4573 l[wd] t2, static_chain_offset(t2)
4574 jr t1
4576 trampoline[0] = OPCODE_AUIPC | (STATIC_CHAIN_REGNUM << SHIFT_RD);
4577 trampoline[1] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW)
4578 | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RD)
4579 | (STATIC_CHAIN_REGNUM << SHIFT_RS1)
4580 | (target_function_offset << SHIFT_IMM);
4581 trampoline[2] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW)
4582 | (STATIC_CHAIN_REGNUM << SHIFT_RD)
4583 | (STATIC_CHAIN_REGNUM << SHIFT_RS1)
4584 | (static_chain_offset << SHIFT_IMM);
4585 trampoline[3] = OPCODE_JALR | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RS1);
4587 /* Copy the trampoline code. */
4588 for (i = 0; i < ARRAY_SIZE (trampoline); i++)
4590 mem = adjust_address (m_tramp, SImode, i * GET_MODE_SIZE (SImode));
4591 riscv_emit_move (mem, gen_int_mode (trampoline[i], SImode));
4594 /* Set up the static chain pointer field. */
4595 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
4596 riscv_emit_move (mem, chain_value);
4598 /* Set up the target function field. */
4599 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
4600 riscv_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
4603 /* Flush the code part of the trampoline. */
4604 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
4605 emit_insn (gen_clear_cache (addr, end_addr));
4608 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
4610 static bool
4611 riscv_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
4612 tree exp ATTRIBUTE_UNUSED)
4614 /* Don't use sibcalls when use save-restore routine. */
4615 if (TARGET_SAVE_RESTORE)
4616 return false;
4618 /* Don't use sibcall for naked functions. */
4619 if (cfun->machine->naked_p)
4620 return false;
4622 /* Don't use sibcall for interrupt functions. */
4623 if (cfun->machine->interrupt_handler_p)
4624 return false;
4626 return true;
4629 /* Get the intterupt type, return UNKNOWN_MODE if it's not
4630 interrupt function. */
4631 static enum riscv_privilege_levels
4632 riscv_get_interrupt_type (tree decl)
4634 gcc_assert (decl != NULL_TREE);
4636 if ((TREE_CODE(decl) != FUNCTION_DECL)
4637 || (!riscv_interrupt_type_p (TREE_TYPE (decl))))
4638 return UNKNOWN_MODE;
4640 tree attr_args
4641 = TREE_VALUE (lookup_attribute ("interrupt",
4642 TYPE_ATTRIBUTES (TREE_TYPE (decl))));
4644 if (attr_args && TREE_CODE (TREE_VALUE (attr_args)) != VOID_TYPE)
4646 const char *string = TREE_STRING_POINTER (TREE_VALUE (attr_args));
4648 if (!strcmp (string, "user"))
4649 return USER_MODE;
4650 else if (!strcmp (string, "supervisor"))
4651 return SUPERVISOR_MODE;
4652 else /* Must be "machine". */
4653 return MACHINE_MODE;
4655 else
4656 /* Interrupt attributes are machine mode by default. */
4657 return MACHINE_MODE;
4660 /* Implement `TARGET_SET_CURRENT_FUNCTION'. */
4661 /* Sanity cheching for above function attributes. */
4662 static void
4663 riscv_set_current_function (tree decl)
4665 if (decl == NULL_TREE
4666 || current_function_decl == NULL_TREE
4667 || current_function_decl == error_mark_node
4668 || ! cfun->machine
4669 || cfun->machine->attributes_checked_p)
4670 return;
4672 cfun->machine->naked_p = riscv_naked_function_p (decl);
4673 cfun->machine->interrupt_handler_p
4674 = riscv_interrupt_type_p (TREE_TYPE (decl));
4676 if (cfun->machine->naked_p && cfun->machine->interrupt_handler_p)
4677 error ("function attributes %qs and %qs are mutually exclusive",
4678 "interrupt", "naked");
4680 if (cfun->machine->interrupt_handler_p)
4682 tree ret = TREE_TYPE (TREE_TYPE (decl));
4683 tree args = TYPE_ARG_TYPES (TREE_TYPE (decl));
4685 if (TREE_CODE (ret) != VOID_TYPE)
4686 error ("%qs function cannot return a value", "interrupt");
4688 if (args && TREE_CODE (TREE_VALUE (args)) != VOID_TYPE)
4689 error ("%qs function cannot have arguments", "interrupt");
4691 cfun->machine->interrupt_mode = riscv_get_interrupt_type (decl);
4693 gcc_assert (cfun->machine->interrupt_mode != UNKNOWN_MODE);
4696 /* Don't print the above diagnostics more than once. */
4697 cfun->machine->attributes_checked_p = 1;
4700 /* Implement TARGET_MERGE_DECL_ATTRIBUTES. */
4701 static tree
4702 riscv_merge_decl_attributes (tree olddecl, tree newdecl)
4704 tree combined_attrs;
4706 enum riscv_privilege_levels old_interrupt_type
4707 = riscv_get_interrupt_type (olddecl);
4708 enum riscv_privilege_levels new_interrupt_type
4709 = riscv_get_interrupt_type (newdecl);
4711 /* Check old and new has same interrupt type. */
4712 if ((old_interrupt_type != UNKNOWN_MODE)
4713 && (new_interrupt_type != UNKNOWN_MODE)
4714 && (old_interrupt_type != new_interrupt_type))
4715 error ("%qs function cannot have different intterupt type.", "interrupt");
4717 /* Create combined attributes. */
4718 combined_attrs = merge_attributes (DECL_ATTRIBUTES (olddecl),
4719 DECL_ATTRIBUTES (newdecl));
4721 return combined_attrs;
4724 /* Implement TARGET_CANNOT_COPY_INSN_P. */
4726 static bool
4727 riscv_cannot_copy_insn_p (rtx_insn *insn)
4729 return recog_memoized (insn) >= 0 && get_attr_cannot_copy (insn);
4732 /* Implement TARGET_SLOW_UNALIGNED_ACCESS. */
4734 static bool
4735 riscv_slow_unaligned_access (machine_mode, unsigned int)
4737 return riscv_slow_unaligned_access_p;
4740 /* Implement TARGET_CAN_CHANGE_MODE_CLASS. */
4742 static bool
4743 riscv_can_change_mode_class (machine_mode, machine_mode, reg_class_t rclass)
4745 return !reg_classes_intersect_p (FP_REGS, rclass);
4749 /* Implement TARGET_CONSTANT_ALIGNMENT. */
4751 static HOST_WIDE_INT
4752 riscv_constant_alignment (const_tree exp, HOST_WIDE_INT align)
4754 if (TREE_CODE (exp) == STRING_CST || TREE_CODE (exp) == CONSTRUCTOR)
4755 return MAX (align, BITS_PER_WORD);
4756 return align;
4759 /* Initialize the GCC target structure. */
4760 #undef TARGET_ASM_ALIGNED_HI_OP
4761 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
4762 #undef TARGET_ASM_ALIGNED_SI_OP
4763 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
4764 #undef TARGET_ASM_ALIGNED_DI_OP
4765 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
4767 #undef TARGET_OPTION_OVERRIDE
4768 #define TARGET_OPTION_OVERRIDE riscv_option_override
4770 #undef TARGET_LEGITIMIZE_ADDRESS
4771 #define TARGET_LEGITIMIZE_ADDRESS riscv_legitimize_address
4773 #undef TARGET_SCHED_ISSUE_RATE
4774 #define TARGET_SCHED_ISSUE_RATE riscv_issue_rate
4776 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
4777 #define TARGET_FUNCTION_OK_FOR_SIBCALL riscv_function_ok_for_sibcall
4779 #undef TARGET_SET_CURRENT_FUNCTION
4780 #define TARGET_SET_CURRENT_FUNCTION riscv_set_current_function
4782 #undef TARGET_REGISTER_MOVE_COST
4783 #define TARGET_REGISTER_MOVE_COST riscv_register_move_cost
4784 #undef TARGET_MEMORY_MOVE_COST
4785 #define TARGET_MEMORY_MOVE_COST riscv_memory_move_cost
4786 #undef TARGET_RTX_COSTS
4787 #define TARGET_RTX_COSTS riscv_rtx_costs
4788 #undef TARGET_ADDRESS_COST
4789 #define TARGET_ADDRESS_COST riscv_address_cost
4791 #undef TARGET_ASM_FILE_START
4792 #define TARGET_ASM_FILE_START riscv_file_start
4793 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
4794 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
4796 #undef TARGET_EXPAND_BUILTIN_VA_START
4797 #define TARGET_EXPAND_BUILTIN_VA_START riscv_va_start
4799 #undef TARGET_PROMOTE_FUNCTION_MODE
4800 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
4802 #undef TARGET_RETURN_IN_MEMORY
4803 #define TARGET_RETURN_IN_MEMORY riscv_return_in_memory
4805 #undef TARGET_ASM_OUTPUT_MI_THUNK
4806 #define TARGET_ASM_OUTPUT_MI_THUNK riscv_output_mi_thunk
4807 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
4808 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
4810 #undef TARGET_PRINT_OPERAND
4811 #define TARGET_PRINT_OPERAND riscv_print_operand
4812 #undef TARGET_PRINT_OPERAND_ADDRESS
4813 #define TARGET_PRINT_OPERAND_ADDRESS riscv_print_operand_address
4815 #undef TARGET_SETUP_INCOMING_VARARGS
4816 #define TARGET_SETUP_INCOMING_VARARGS riscv_setup_incoming_varargs
4817 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
4818 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS riscv_allocate_stack_slots_for_args
4819 #undef TARGET_STRICT_ARGUMENT_NAMING
4820 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
4821 #undef TARGET_MUST_PASS_IN_STACK
4822 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
4823 #undef TARGET_PASS_BY_REFERENCE
4824 #define TARGET_PASS_BY_REFERENCE riscv_pass_by_reference
4825 #undef TARGET_ARG_PARTIAL_BYTES
4826 #define TARGET_ARG_PARTIAL_BYTES riscv_arg_partial_bytes
4827 #undef TARGET_FUNCTION_ARG
4828 #define TARGET_FUNCTION_ARG riscv_function_arg
4829 #undef TARGET_FUNCTION_ARG_ADVANCE
4830 #define TARGET_FUNCTION_ARG_ADVANCE riscv_function_arg_advance
4831 #undef TARGET_FUNCTION_ARG_BOUNDARY
4832 #define TARGET_FUNCTION_ARG_BOUNDARY riscv_function_arg_boundary
4834 /* The generic ELF target does not always have TLS support. */
4835 #ifdef HAVE_AS_TLS
4836 #undef TARGET_HAVE_TLS
4837 #define TARGET_HAVE_TLS true
4838 #endif
4840 #undef TARGET_CANNOT_FORCE_CONST_MEM
4841 #define TARGET_CANNOT_FORCE_CONST_MEM riscv_cannot_force_const_mem
4843 #undef TARGET_LEGITIMATE_CONSTANT_P
4844 #define TARGET_LEGITIMATE_CONSTANT_P riscv_legitimate_constant_p
4846 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
4847 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
4849 #undef TARGET_LEGITIMATE_ADDRESS_P
4850 #define TARGET_LEGITIMATE_ADDRESS_P riscv_legitimate_address_p
4852 #undef TARGET_CAN_ELIMINATE
4853 #define TARGET_CAN_ELIMINATE riscv_can_eliminate
4855 #undef TARGET_CONDITIONAL_REGISTER_USAGE
4856 #define TARGET_CONDITIONAL_REGISTER_USAGE riscv_conditional_register_usage
4858 #undef TARGET_CLASS_MAX_NREGS
4859 #define TARGET_CLASS_MAX_NREGS riscv_class_max_nregs
4861 #undef TARGET_TRAMPOLINE_INIT
4862 #define TARGET_TRAMPOLINE_INIT riscv_trampoline_init
4864 #undef TARGET_IN_SMALL_DATA_P
4865 #define TARGET_IN_SMALL_DATA_P riscv_in_small_data_p
4867 #undef TARGET_HAVE_SRODATA_SECTION
4868 #define TARGET_HAVE_SRODATA_SECTION true
4870 #undef TARGET_ASM_SELECT_SECTION
4871 #define TARGET_ASM_SELECT_SECTION riscv_select_section
4873 #undef TARGET_ASM_SELECT_RTX_SECTION
4874 #define TARGET_ASM_SELECT_RTX_SECTION riscv_elf_select_rtx_section
4876 #undef TARGET_MIN_ANCHOR_OFFSET
4877 #define TARGET_MIN_ANCHOR_OFFSET (-IMM_REACH/2)
4879 #undef TARGET_MAX_ANCHOR_OFFSET
4880 #define TARGET_MAX_ANCHOR_OFFSET (IMM_REACH/2-1)
4882 #undef TARGET_REGISTER_PRIORITY
4883 #define TARGET_REGISTER_PRIORITY riscv_register_priority
4885 #undef TARGET_CANNOT_COPY_INSN_P
4886 #define TARGET_CANNOT_COPY_INSN_P riscv_cannot_copy_insn_p
4888 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
4889 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV riscv_atomic_assign_expand_fenv
4891 #undef TARGET_INIT_BUILTINS
4892 #define TARGET_INIT_BUILTINS riscv_init_builtins
4894 #undef TARGET_BUILTIN_DECL
4895 #define TARGET_BUILTIN_DECL riscv_builtin_decl
4897 #undef TARGET_EXPAND_BUILTIN
4898 #define TARGET_EXPAND_BUILTIN riscv_expand_builtin
4900 #undef TARGET_HARD_REGNO_NREGS
4901 #define TARGET_HARD_REGNO_NREGS riscv_hard_regno_nregs
4902 #undef TARGET_HARD_REGNO_MODE_OK
4903 #define TARGET_HARD_REGNO_MODE_OK riscv_hard_regno_mode_ok
4905 #undef TARGET_MODES_TIEABLE_P
4906 #define TARGET_MODES_TIEABLE_P riscv_modes_tieable_p
4908 #undef TARGET_SLOW_UNALIGNED_ACCESS
4909 #define TARGET_SLOW_UNALIGNED_ACCESS riscv_slow_unaligned_access
4911 #undef TARGET_SECONDARY_MEMORY_NEEDED
4912 #define TARGET_SECONDARY_MEMORY_NEEDED riscv_secondary_memory_needed
4914 #undef TARGET_CAN_CHANGE_MODE_CLASS
4915 #define TARGET_CAN_CHANGE_MODE_CLASS riscv_can_change_mode_class
4917 #undef TARGET_CONSTANT_ALIGNMENT
4918 #define TARGET_CONSTANT_ALIGNMENT riscv_constant_alignment
4920 #undef TARGET_MERGE_DECL_ATTRIBUTES
4921 #define TARGET_MERGE_DECL_ATTRIBUTES riscv_merge_decl_attributes
4923 #undef TARGET_ATTRIBUTE_TABLE
4924 #define TARGET_ATTRIBUTE_TABLE riscv_attribute_table
4926 #undef TARGET_WARN_FUNC_RETURN
4927 #define TARGET_WARN_FUNC_RETURN riscv_warn_func_return
4929 /* The low bit is ignored by jump instructions so is safe to use. */
4930 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
4931 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
4933 struct gcc_target targetm = TARGET_INITIALIZER;
4935 #include "gt-riscv.h"