* config/riscv/riscv.c: Remove unnecessary includes. Reorder
[official-gcc.git] / gcc / config / riscv / riscv.c
blob57b2edbcb4302727e424917926845a32962c3d8d
1 /* Subroutines used for code generation for RISC-V.
2 Copyright (C) 2011-2017 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 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "insn-config.h"
29 #include "insn-attr.h"
30 #include "recog.h"
31 #include "output.h"
32 #include "alias.h"
33 #include "tree.h"
34 #include "varasm.h"
35 #include "stor-layout.h"
36 #include "calls.h"
37 #include "function.h"
38 #include "explow.h"
39 #include "memmodel.h"
40 #include "emit-rtl.h"
41 #include "reload.h"
42 #include "tm_p.h"
43 #include "target.h"
44 #include "target-def.h"
45 #include "basic-block.h"
46 #include "expr.h"
47 #include "optabs.h"
48 #include "bitmap.h"
49 #include "df.h"
50 #include "diagnostic.h"
51 #include "builtins.h"
53 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
54 #define UNSPEC_ADDRESS_P(X) \
55 (GET_CODE (X) == UNSPEC \
56 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
57 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
59 /* Extract the symbol or label from UNSPEC wrapper X. */
60 #define UNSPEC_ADDRESS(X) \
61 XVECEXP (X, 0, 0)
63 /* Extract the symbol type from UNSPEC wrapper X. */
64 #define UNSPEC_ADDRESS_TYPE(X) \
65 ((enum riscv_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
67 /* True if bit BIT is set in VALUE. */
68 #define BITSET_P(VALUE, BIT) (((VALUE) & (1ULL << (BIT))) != 0)
70 /* Classifies an address.
72 ADDRESS_REG
73 A natural register + offset address. The register satisfies
74 riscv_valid_base_register_p and the offset is a const_arith_operand.
76 ADDRESS_LO_SUM
77 A LO_SUM rtx. The first operand is a valid base register and
78 the second operand is a symbolic address.
80 ADDRESS_CONST_INT
81 A signed 16-bit constant address.
83 ADDRESS_SYMBOLIC:
84 A constant symbolic address. */
85 enum riscv_address_type {
86 ADDRESS_REG,
87 ADDRESS_LO_SUM,
88 ADDRESS_CONST_INT,
89 ADDRESS_SYMBOLIC
92 /* Information about a function's frame layout. */
93 struct GTY(()) riscv_frame_info {
94 /* The size of the frame in bytes. */
95 HOST_WIDE_INT total_size;
97 /* Bit X is set if the function saves or restores GPR X. */
98 unsigned int mask;
100 /* Likewise FPR X. */
101 unsigned int fmask;
103 /* How much the GPR save/restore routines adjust sp (or 0 if unused). */
104 unsigned save_libcall_adjustment;
106 /* Offsets of fixed-point and floating-point save areas from frame bottom */
107 HOST_WIDE_INT gp_sp_offset;
108 HOST_WIDE_INT fp_sp_offset;
110 /* Offset of virtual frame pointer from stack pointer/frame bottom */
111 HOST_WIDE_INT frame_pointer_offset;
113 /* Offset of hard frame pointer from stack pointer/frame bottom */
114 HOST_WIDE_INT hard_frame_pointer_offset;
116 /* The offset of arg_pointer_rtx from the bottom of the frame. */
117 HOST_WIDE_INT arg_pointer_offset;
120 struct GTY(()) machine_function {
121 /* The number of extra stack bytes taken up by register varargs.
122 This area is allocated by the callee at the very top of the frame. */
123 int varargs_size;
125 /* Memoized return value of leaf_function_p. <0 if false, >0 if true. */
126 int is_leaf;
128 /* The current frame information, calculated by riscv_compute_frame_info. */
129 struct riscv_frame_info frame;
132 /* Information about a single argument. */
133 struct riscv_arg_info {
134 /* True if the argument is at least partially passed on the stack. */
135 bool stack_p;
137 /* The number of integer registers allocated to this argument. */
138 unsigned int num_gprs;
140 /* The offset of the first register used, provided num_gprs is nonzero.
141 If passed entirely on the stack, the value is MAX_ARGS_IN_REGISTERS. */
142 unsigned int gpr_offset;
144 /* The number of floating-point registers allocated to this argument. */
145 unsigned int num_fprs;
147 /* The offset of the first register used, provided num_fprs is nonzero. */
148 unsigned int fpr_offset;
151 /* Information about an address described by riscv_address_type.
153 ADDRESS_CONST_INT
154 No fields are used.
156 ADDRESS_REG
157 REG is the base register and OFFSET is the constant offset.
159 ADDRESS_LO_SUM
160 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
161 is the type of symbol it references.
163 ADDRESS_SYMBOLIC
164 SYMBOL_TYPE is the type of symbol that the address references. */
165 struct riscv_address_info {
166 enum riscv_address_type type;
167 rtx reg;
168 rtx offset;
169 enum riscv_symbol_type symbol_type;
172 /* One stage in a constant building sequence. These sequences have
173 the form:
175 A = VALUE[0]
176 A = A CODE[1] VALUE[1]
177 A = A CODE[2] VALUE[2]
180 where A is an accumulator, each CODE[i] is a binary rtl operation
181 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
182 struct riscv_integer_op {
183 enum rtx_code code;
184 unsigned HOST_WIDE_INT value;
187 /* The largest number of operations needed to load an integer constant.
188 The worst case is LUI, ADDI, SLLI, ADDI, SLLI, ADDI, SLLI, ADDI. */
189 #define RISCV_MAX_INTEGER_OPS 8
191 /* Costs of various operations on the different architectures. */
193 struct riscv_tune_info
195 unsigned short fp_add[2];
196 unsigned short fp_mul[2];
197 unsigned short fp_div[2];
198 unsigned short int_mul[2];
199 unsigned short int_div[2];
200 unsigned short issue_rate;
201 unsigned short branch_cost;
202 unsigned short memory_cost;
203 bool slow_unaligned_access;
206 /* Information about one CPU we know about. */
207 struct riscv_cpu_info {
208 /* This CPU's canonical name. */
209 const char *name;
211 /* Tuning parameters for this CPU. */
212 const struct riscv_tune_info *tune_info;
215 /* Global variables for machine-dependent things. */
217 /* Whether unaligned accesses execute very slowly. */
218 bool riscv_slow_unaligned_access;
220 /* Which tuning parameters to use. */
221 static const struct riscv_tune_info *tune_info;
223 /* Index R is the smallest register class that contains register R. */
224 const enum reg_class riscv_regno_to_class[FIRST_PSEUDO_REGISTER] = {
225 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
226 GR_REGS, GR_REGS, SIBCALL_REGS, SIBCALL_REGS,
227 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
228 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
229 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
230 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
231 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
232 SIBCALL_REGS, SIBCALL_REGS, SIBCALL_REGS, SIBCALL_REGS,
233 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
234 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
235 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
236 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
237 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
238 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
239 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
240 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
241 FRAME_REGS, FRAME_REGS,
244 /* Costs to use when optimizing for rocket. */
245 static const struct riscv_tune_info rocket_tune_info = {
246 {COSTS_N_INSNS (4), COSTS_N_INSNS (5)}, /* fp_add */
247 {COSTS_N_INSNS (4), COSTS_N_INSNS (5)}, /* fp_mul */
248 {COSTS_N_INSNS (20), COSTS_N_INSNS (20)}, /* fp_div */
249 {COSTS_N_INSNS (4), COSTS_N_INSNS (4)}, /* int_mul */
250 {COSTS_N_INSNS (6), COSTS_N_INSNS (6)}, /* int_div */
251 1, /* issue_rate */
252 3, /* branch_cost */
253 5, /* memory_cost */
254 true, /* slow_unaligned_access */
257 /* Costs to use when optimizing for size. */
258 static const struct riscv_tune_info optimize_size_tune_info = {
259 {COSTS_N_INSNS (1), COSTS_N_INSNS (1)}, /* fp_add */
260 {COSTS_N_INSNS (1), COSTS_N_INSNS (1)}, /* fp_mul */
261 {COSTS_N_INSNS (1), COSTS_N_INSNS (1)}, /* fp_div */
262 {COSTS_N_INSNS (1), COSTS_N_INSNS (1)}, /* int_mul */
263 {COSTS_N_INSNS (1), COSTS_N_INSNS (1)}, /* int_div */
264 1, /* issue_rate */
265 1, /* branch_cost */
266 2, /* memory_cost */
267 false, /* slow_unaligned_access */
270 /* A table describing all the processors GCC knows about. */
271 static const struct riscv_cpu_info riscv_cpu_info_table[] = {
272 { "rocket", &rocket_tune_info },
273 { "size", &optimize_size_tune_info },
276 /* Return the riscv_cpu_info entry for the given name string. */
278 static const struct riscv_cpu_info *
279 riscv_parse_cpu (const char *cpu_string)
281 for (unsigned i = 0; i < ARRAY_SIZE (riscv_cpu_info_table); i++)
282 if (strcmp (riscv_cpu_info_table[i].name, cpu_string) == 0)
283 return riscv_cpu_info_table + i;
285 error ("unknown cpu %qs for -mtune", cpu_string);
286 return riscv_cpu_info_table;
289 /* Helper function for riscv_build_integer; arguments are as for
290 riscv_build_integer. */
292 static int
293 riscv_build_integer_1 (struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS],
294 HOST_WIDE_INT value, machine_mode mode)
296 HOST_WIDE_INT low_part = CONST_LOW_PART (value);
297 int cost = RISCV_MAX_INTEGER_OPS + 1, alt_cost;
298 struct riscv_integer_op alt_codes[RISCV_MAX_INTEGER_OPS];
300 if (SMALL_OPERAND (value) || LUI_OPERAND (value))
302 /* Simply ADDI or LUI. */
303 codes[0].code = UNKNOWN;
304 codes[0].value = value;
305 return 1;
308 /* End with ADDI. When constructing HImode constants, do not generate any
309 intermediate value that is not itself a valid HImode constant. The
310 XORI case below will handle those remaining HImode constants. */
311 if (low_part != 0
312 && (mode != HImode
313 || value - low_part <= ((1 << (GET_MODE_BITSIZE (HImode) - 1)) - 1)))
315 alt_cost = 1 + riscv_build_integer_1 (alt_codes, value - low_part, mode);
316 if (alt_cost < cost)
318 alt_codes[alt_cost-1].code = PLUS;
319 alt_codes[alt_cost-1].value = low_part;
320 memcpy (codes, alt_codes, sizeof (alt_codes));
321 cost = alt_cost;
325 /* End with XORI. */
326 if (cost > 2 && (low_part < 0 || mode == HImode))
328 alt_cost = 1 + riscv_build_integer_1 (alt_codes, value ^ low_part, mode);
329 if (alt_cost < cost)
331 alt_codes[alt_cost-1].code = XOR;
332 alt_codes[alt_cost-1].value = low_part;
333 memcpy (codes, alt_codes, sizeof (alt_codes));
334 cost = alt_cost;
338 /* Eliminate trailing zeros and end with SLLI. */
339 if (cost > 2 && (value & 1) == 0)
341 int shift = ctz_hwi (value);
342 unsigned HOST_WIDE_INT x = value;
343 x = sext_hwi (x >> shift, HOST_BITS_PER_WIDE_INT - shift);
345 /* Don't eliminate the lower 12 bits if LUI might apply. */
346 if (shift > IMM_BITS && !SMALL_OPERAND (x) && LUI_OPERAND (x << IMM_BITS))
347 shift -= IMM_BITS, x <<= IMM_BITS;
349 alt_cost = 1 + riscv_build_integer_1 (alt_codes, x, mode);
350 if (alt_cost < cost)
352 alt_codes[alt_cost-1].code = ASHIFT;
353 alt_codes[alt_cost-1].value = shift;
354 memcpy (codes, alt_codes, sizeof (alt_codes));
355 cost = alt_cost;
359 gcc_assert (cost <= RISCV_MAX_INTEGER_OPS);
360 return cost;
363 /* Fill CODES with a sequence of rtl operations to load VALUE.
364 Return the number of operations needed. */
366 static int
367 riscv_build_integer (struct riscv_integer_op *codes, HOST_WIDE_INT value,
368 machine_mode mode)
370 int cost = riscv_build_integer_1 (codes, value, mode);
372 /* Eliminate leading zeros and end with SRLI. */
373 if (value > 0 && cost > 2)
375 struct riscv_integer_op alt_codes[RISCV_MAX_INTEGER_OPS];
376 int alt_cost, shift = clz_hwi (value);
377 HOST_WIDE_INT shifted_val;
379 /* Try filling trailing bits with 1s. */
380 shifted_val = (value << shift) | ((((HOST_WIDE_INT) 1) << shift) - 1);
381 alt_cost = 1 + riscv_build_integer_1 (alt_codes, shifted_val, mode);
382 if (alt_cost < cost)
384 alt_codes[alt_cost-1].code = LSHIFTRT;
385 alt_codes[alt_cost-1].value = shift;
386 memcpy (codes, alt_codes, sizeof (alt_codes));
387 cost = alt_cost;
390 /* Try filling trailing bits with 0s. */
391 shifted_val = value << shift;
392 alt_cost = 1 + riscv_build_integer_1 (alt_codes, shifted_val, mode);
393 if (alt_cost < cost)
395 alt_codes[alt_cost-1].code = LSHIFTRT;
396 alt_codes[alt_cost-1].value = shift;
397 memcpy (codes, alt_codes, sizeof (alt_codes));
398 cost = alt_cost;
402 return cost;
405 /* Return the cost of constructing VAL in the event that a scratch
406 register is available. */
408 static int
409 riscv_split_integer_cost (HOST_WIDE_INT val)
411 int cost;
412 unsigned HOST_WIDE_INT loval = sext_hwi (val, 32);
413 unsigned HOST_WIDE_INT hival = sext_hwi ((val - loval) >> 32, 32);
414 struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
416 cost = 2 + riscv_build_integer (codes, loval, VOIDmode);
417 if (loval != hival)
418 cost += riscv_build_integer (codes, hival, VOIDmode);
420 return cost;
423 /* Return the cost of constructing the integer constant VAL. */
425 static int
426 riscv_integer_cost (HOST_WIDE_INT val)
428 struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
429 return MIN (riscv_build_integer (codes, val, VOIDmode),
430 riscv_split_integer_cost (val));
433 /* Try to split a 64b integer into 32b parts, then reassemble. */
435 static rtx
436 riscv_split_integer (HOST_WIDE_INT val, machine_mode mode)
438 unsigned HOST_WIDE_INT loval = sext_hwi (val, 32);
439 unsigned HOST_WIDE_INT hival = sext_hwi ((val - loval) >> 32, 32);
440 rtx hi = gen_reg_rtx (mode), lo = gen_reg_rtx (mode);
442 riscv_move_integer (hi, hi, hival);
443 riscv_move_integer (lo, lo, loval);
445 hi = gen_rtx_fmt_ee (ASHIFT, mode, hi, GEN_INT (32));
446 hi = force_reg (mode, hi);
448 return gen_rtx_fmt_ee (PLUS, mode, hi, lo);
451 /* Return true if X is a thread-local symbol. */
453 static bool
454 riscv_tls_symbol_p (const_rtx x)
456 return SYMBOL_REF_P (x) && SYMBOL_REF_TLS_MODEL (x) != 0;
459 /* Return true if symbol X binds locally. */
461 static bool
462 riscv_symbol_binds_local_p (const_rtx x)
464 if (SYMBOL_REF_P (x))
465 return (SYMBOL_REF_DECL (x)
466 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
467 : SYMBOL_REF_LOCAL_P (x));
468 else
469 return false;
472 /* Return the method that should be used to access SYMBOL_REF or
473 LABEL_REF X. */
475 static enum riscv_symbol_type
476 riscv_classify_symbol (const_rtx x)
478 if (riscv_tls_symbol_p (x))
479 return SYMBOL_TLS;
481 if (GET_CODE (x) == SYMBOL_REF && flag_pic && !riscv_symbol_binds_local_p (x))
482 return SYMBOL_GOT_DISP;
484 return riscv_cmodel == CM_MEDLOW ? SYMBOL_ABSOLUTE : SYMBOL_PCREL;
487 /* Classify the base of symbolic expression X. */
489 enum riscv_symbol_type
490 riscv_classify_symbolic_expression (rtx x)
492 rtx offset;
494 split_const (x, &x, &offset);
495 if (UNSPEC_ADDRESS_P (x))
496 return UNSPEC_ADDRESS_TYPE (x);
498 return riscv_classify_symbol (x);
501 /* Return true if X is a symbolic constant. If it is, store the type of
502 the symbol in *SYMBOL_TYPE. */
504 bool
505 riscv_symbolic_constant_p (rtx x, enum riscv_symbol_type *symbol_type)
507 rtx offset;
509 split_const (x, &x, &offset);
510 if (UNSPEC_ADDRESS_P (x))
512 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
513 x = UNSPEC_ADDRESS (x);
515 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
516 *symbol_type = riscv_classify_symbol (x);
517 else
518 return false;
520 if (offset == const0_rtx)
521 return true;
523 /* Nonzero offsets are only valid for references that don't use the GOT. */
524 switch (*symbol_type)
526 case SYMBOL_ABSOLUTE:
527 case SYMBOL_PCREL:
528 case SYMBOL_TLS_LE:
529 /* GAS rejects offsets outside the range [-2^31, 2^31-1]. */
530 return sext_hwi (INTVAL (offset), 32) == INTVAL (offset);
532 default:
533 return false;
537 /* Returns the number of instructions necessary to reference a symbol. */
539 static int riscv_symbol_insns (enum riscv_symbol_type type)
541 switch (type)
543 case SYMBOL_TLS: return 0; /* Depends on the TLS model. */
544 case SYMBOL_ABSOLUTE: return 2; /* LUI + the reference. */
545 case SYMBOL_PCREL: return 2; /* AUIPC + the reference. */
546 case SYMBOL_TLS_LE: return 3; /* LUI + ADD TP + the reference. */
547 case SYMBOL_GOT_DISP: return 3; /* AUIPC + LD GOT + the reference. */
548 default: gcc_unreachable ();
552 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
554 static bool
555 riscv_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
557 return riscv_const_insns (x) > 0;
560 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
562 static bool
563 riscv_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
565 enum riscv_symbol_type type;
566 rtx base, offset;
568 /* There is no assembler syntax for expressing an address-sized
569 high part. */
570 if (GET_CODE (x) == HIGH)
571 return true;
573 split_const (x, &base, &offset);
574 if (riscv_symbolic_constant_p (base, &type))
576 /* As an optimization, don't spill symbolic constants that are as
577 cheap to rematerialize as to access in the constant pool. */
578 if (SMALL_OPERAND (INTVAL (offset)) && riscv_symbol_insns (type) > 0)
579 return true;
581 /* As an optimization, avoid needlessly generate dynamic relocations. */
582 if (flag_pic)
583 return true;
586 /* TLS symbols must be computed by riscv_legitimize_move. */
587 if (tls_referenced_p (x))
588 return true;
590 return false;
593 /* Return true if register REGNO is a valid base register for mode MODE.
594 STRICT_P is true if REG_OK_STRICT is in effect. */
597 riscv_regno_mode_ok_for_base_p (int regno,
598 machine_mode mode ATTRIBUTE_UNUSED,
599 bool strict_p)
601 if (!HARD_REGISTER_NUM_P (regno))
603 if (!strict_p)
604 return true;
605 regno = reg_renumber[regno];
608 /* These fake registers will be eliminated to either the stack or
609 hard frame pointer, both of which are usually valid base registers.
610 Reload deals with the cases where the eliminated form isn't valid. */
611 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
612 return true;
614 return GP_REG_P (regno);
617 /* Return true if X is a valid base register for mode MODE.
618 STRICT_P is true if REG_OK_STRICT is in effect. */
620 static bool
621 riscv_valid_base_register_p (rtx x, machine_mode mode, bool strict_p)
623 if (!strict_p && GET_CODE (x) == SUBREG)
624 x = SUBREG_REG (x);
626 return (REG_P (x)
627 && riscv_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
630 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
631 can address a value of mode MODE. */
633 static bool
634 riscv_valid_offset_p (rtx x, machine_mode mode)
636 /* Check that X is a signed 12-bit number. */
637 if (!const_arith_operand (x, Pmode))
638 return false;
640 /* We may need to split multiword moves, so make sure that every word
641 is accessible. */
642 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
643 && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
644 return false;
646 return true;
649 /* Should a symbol of type SYMBOL_TYPE should be split in two? */
651 bool
652 riscv_split_symbol_type (enum riscv_symbol_type symbol_type)
654 if (symbol_type == SYMBOL_TLS_LE)
655 return true;
657 if (!TARGET_EXPLICIT_RELOCS)
658 return false;
660 return symbol_type == SYMBOL_ABSOLUTE || symbol_type == SYMBOL_PCREL;
663 /* Return true if a LO_SUM can address a value of mode MODE when the
664 LO_SUM symbol has type SYM_TYPE. */
666 static bool
667 riscv_valid_lo_sum_p (enum riscv_symbol_type sym_type, machine_mode mode)
669 /* Check that symbols of type SYMBOL_TYPE can be used to access values
670 of mode MODE. */
671 if (riscv_symbol_insns (sym_type) == 0)
672 return false;
674 /* Check that there is a known low-part relocation. */
675 if (!riscv_split_symbol_type (sym_type))
676 return false;
678 /* We may need to split multiword moves, so make sure that each word
679 can be accessed without inducing a carry. */
680 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
681 && (!TARGET_STRICT_ALIGN
682 || GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode)))
683 return false;
685 return true;
688 /* Return true if X is a valid address for machine mode MODE. If it is,
689 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
690 effect. */
692 static bool
693 riscv_classify_address (struct riscv_address_info *info, rtx x,
694 machine_mode mode, bool strict_p)
696 switch (GET_CODE (x))
698 case REG:
699 case SUBREG:
700 info->type = ADDRESS_REG;
701 info->reg = x;
702 info->offset = const0_rtx;
703 return riscv_valid_base_register_p (info->reg, mode, strict_p);
705 case PLUS:
706 info->type = ADDRESS_REG;
707 info->reg = XEXP (x, 0);
708 info->offset = XEXP (x, 1);
709 return (riscv_valid_base_register_p (info->reg, mode, strict_p)
710 && riscv_valid_offset_p (info->offset, mode));
712 case LO_SUM:
713 info->type = ADDRESS_LO_SUM;
714 info->reg = XEXP (x, 0);
715 info->offset = XEXP (x, 1);
716 /* We have to trust the creator of the LO_SUM to do something vaguely
717 sane. Target-independent code that creates a LO_SUM should also
718 create and verify the matching HIGH. Target-independent code that
719 adds an offset to a LO_SUM must prove that the offset will not
720 induce a carry. Failure to do either of these things would be
721 a bug, and we are not required to check for it here. The RISC-V
722 backend itself should only create LO_SUMs for valid symbolic
723 constants, with the high part being either a HIGH or a copy
724 of _gp. */
725 info->symbol_type
726 = riscv_classify_symbolic_expression (info->offset);
727 return (riscv_valid_base_register_p (info->reg, mode, strict_p)
728 && riscv_valid_lo_sum_p (info->symbol_type, mode));
730 case CONST_INT:
731 /* Small-integer addresses don't occur very often, but they
732 are legitimate if x0 is a valid base register. */
733 info->type = ADDRESS_CONST_INT;
734 return SMALL_OPERAND (INTVAL (x));
736 default:
737 return false;
741 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
743 static bool
744 riscv_legitimate_address_p (machine_mode mode, rtx x, bool strict_p)
746 struct riscv_address_info addr;
748 return riscv_classify_address (&addr, x, mode, strict_p);
751 /* Return the number of instructions needed to load or store a value
752 of mode MODE at address X. Return 0 if X isn't valid for MODE.
753 Assume that multiword moves may need to be split into word moves
754 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
755 enough. */
758 riscv_address_insns (rtx x, machine_mode mode, bool might_split_p)
760 struct riscv_address_info addr;
761 int n = 1;
763 if (!riscv_classify_address (&addr, x, mode, false))
764 return 0;
766 /* BLKmode is used for single unaligned loads and stores and should
767 not count as a multiword mode. */
768 if (mode != BLKmode && might_split_p)
769 n += (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
771 if (addr.type == ADDRESS_LO_SUM)
772 n += riscv_symbol_insns (addr.symbol_type) - 1;
774 return n;
777 /* Return the number of instructions needed to load constant X.
778 Return 0 if X isn't a valid constant. */
781 riscv_const_insns (rtx x)
783 enum riscv_symbol_type symbol_type;
784 rtx offset;
786 switch (GET_CODE (x))
788 case HIGH:
789 if (!riscv_symbolic_constant_p (XEXP (x, 0), &symbol_type)
790 || !riscv_split_symbol_type (symbol_type))
791 return 0;
793 /* This is simply an LUI. */
794 return 1;
796 case CONST_INT:
798 int cost = riscv_integer_cost (INTVAL (x));
799 /* Force complicated constants to memory. */
800 return cost < 4 ? cost : 0;
803 case CONST_DOUBLE:
804 case CONST_VECTOR:
805 /* We can use x0 to load floating-point zero. */
806 return x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
808 case CONST:
809 /* See if we can refer to X directly. */
810 if (riscv_symbolic_constant_p (x, &symbol_type))
811 return riscv_symbol_insns (symbol_type);
813 /* Otherwise try splitting the constant into a base and offset. */
814 split_const (x, &x, &offset);
815 if (offset != 0)
817 int n = riscv_const_insns (x);
818 if (n != 0)
819 return n + riscv_integer_cost (INTVAL (offset));
821 return 0;
823 case SYMBOL_REF:
824 case LABEL_REF:
825 return riscv_symbol_insns (riscv_classify_symbol (x));
827 default:
828 return 0;
832 /* X is a doubleword constant that can be handled by splitting it into
833 two words and loading each word separately. Return the number of
834 instructions required to do this. */
837 riscv_split_const_insns (rtx x)
839 unsigned int low, high;
841 low = riscv_const_insns (riscv_subword (x, false));
842 high = riscv_const_insns (riscv_subword (x, true));
843 gcc_assert (low > 0 && high > 0);
844 return low + high;
847 /* Return the number of instructions needed to implement INSN,
848 given that it loads from or stores to MEM. */
851 riscv_load_store_insns (rtx mem, rtx_insn *insn)
853 machine_mode mode;
854 bool might_split_p;
855 rtx set;
857 gcc_assert (MEM_P (mem));
858 mode = GET_MODE (mem);
860 /* Try to prove that INSN does not need to be split. */
861 might_split_p = true;
862 if (GET_MODE_BITSIZE (mode) <= 32)
863 might_split_p = false;
864 else if (GET_MODE_BITSIZE (mode) == 64)
866 set = single_set (insn);
867 if (set && !riscv_split_64bit_move_p (SET_DEST (set), SET_SRC (set)))
868 might_split_p = false;
871 return riscv_address_insns (XEXP (mem, 0), mode, might_split_p);
874 /* Emit a move from SRC to DEST. Assume that the move expanders can
875 handle all moves if !can_create_pseudo_p (). The distinction is
876 important because, unlike emit_move_insn, the move expanders know
877 how to force Pmode objects into the constant pool even when the
878 constant pool address is not itself legitimate. */
881 riscv_emit_move (rtx dest, rtx src)
883 return (can_create_pseudo_p ()
884 ? emit_move_insn (dest, src)
885 : emit_move_insn_1 (dest, src));
888 /* Emit an instruction of the form (set TARGET SRC). */
890 static rtx
891 riscv_emit_set (rtx target, rtx src)
893 emit_insn (gen_rtx_SET (target, src));
894 return target;
897 /* Emit an instruction of the form (set DEST (CODE X Y)). */
899 static rtx
900 riscv_emit_binary (enum rtx_code code, rtx dest, rtx x, rtx y)
902 return riscv_emit_set (dest, gen_rtx_fmt_ee (code, GET_MODE (dest), x, y));
905 /* Compute (CODE X Y) and store the result in a new register
906 of mode MODE. Return that new register. */
908 static rtx
909 riscv_force_binary (machine_mode mode, enum rtx_code code, rtx x, rtx y)
911 return riscv_emit_binary (code, gen_reg_rtx (mode), x, y);
914 /* Copy VALUE to a register and return that register. If new pseudos
915 are allowed, copy it into a new register, otherwise use DEST. */
917 static rtx
918 riscv_force_temporary (rtx dest, rtx value)
920 if (can_create_pseudo_p ())
921 return force_reg (Pmode, value);
922 else
924 riscv_emit_move (dest, value);
925 return dest;
929 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
930 then add CONST_INT OFFSET to the result. */
932 static rtx
933 riscv_unspec_address_offset (rtx base, rtx offset,
934 enum riscv_symbol_type symbol_type)
936 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
937 UNSPEC_ADDRESS_FIRST + symbol_type);
938 if (offset != const0_rtx)
939 base = gen_rtx_PLUS (Pmode, base, offset);
940 return gen_rtx_CONST (Pmode, base);
943 /* Return an UNSPEC address with underlying address ADDRESS and symbol
944 type SYMBOL_TYPE. */
947 riscv_unspec_address (rtx address, enum riscv_symbol_type symbol_type)
949 rtx base, offset;
951 split_const (address, &base, &offset);
952 return riscv_unspec_address_offset (base, offset, symbol_type);
955 /* If OP is an UNSPEC address, return the address to which it refers,
956 otherwise return OP itself. */
958 static rtx
959 riscv_strip_unspec_address (rtx op)
961 rtx base, offset;
963 split_const (op, &base, &offset);
964 if (UNSPEC_ADDRESS_P (base))
965 op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
966 return op;
969 /* If riscv_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
970 high part to BASE and return the result. Just return BASE otherwise.
971 TEMP is as for riscv_force_temporary.
973 The returned expression can be used as the first operand to a LO_SUM. */
975 static rtx
976 riscv_unspec_offset_high (rtx temp, rtx addr, enum riscv_symbol_type symbol_type)
978 addr = gen_rtx_HIGH (Pmode, riscv_unspec_address (addr, symbol_type));
979 return riscv_force_temporary (temp, addr);
982 /* Load an entry from the GOT for a TLS GD access. */
984 static rtx riscv_got_load_tls_gd (rtx dest, rtx sym)
986 if (Pmode == DImode)
987 return gen_got_load_tls_gddi (dest, sym);
988 else
989 return gen_got_load_tls_gdsi (dest, sym);
992 /* Load an entry from the GOT for a TLS IE access. */
994 static rtx riscv_got_load_tls_ie (rtx dest, rtx sym)
996 if (Pmode == DImode)
997 return gen_got_load_tls_iedi (dest, sym);
998 else
999 return gen_got_load_tls_iesi (dest, sym);
1002 /* Add in the thread pointer for a TLS LE access. */
1004 static rtx riscv_tls_add_tp_le (rtx dest, rtx base, rtx sym)
1006 rtx tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
1007 if (Pmode == DImode)
1008 return gen_tls_add_tp_ledi (dest, base, tp, sym);
1009 else
1010 return gen_tls_add_tp_lesi (dest, base, tp, sym);
1013 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
1014 it appears in a MEM of that mode. Return true if ADDR is a legitimate
1015 constant in that context and can be split into high and low parts.
1016 If so, and if LOW_OUT is nonnull, emit the high part and store the
1017 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
1019 TEMP is as for riscv_force_temporary and is used to load the high
1020 part into a register.
1022 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
1023 a legitimize SET_SRC for an .md pattern, otherwise the low part
1024 is guaranteed to be a legitimate address for mode MODE. */
1026 bool
1027 riscv_split_symbol (rtx temp, rtx addr, machine_mode mode, rtx *low_out)
1029 enum riscv_symbol_type symbol_type;
1031 if ((GET_CODE (addr) == HIGH && mode == MAX_MACHINE_MODE)
1032 || !riscv_symbolic_constant_p (addr, &symbol_type)
1033 || riscv_symbol_insns (symbol_type) == 0
1034 || !riscv_split_symbol_type (symbol_type))
1035 return false;
1037 if (low_out)
1038 switch (symbol_type)
1040 case SYMBOL_ABSOLUTE:
1042 rtx high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
1043 high = riscv_force_temporary (temp, high);
1044 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
1046 break;
1048 case SYMBOL_PCREL:
1050 static unsigned seqno;
1051 char buf[32];
1052 rtx label;
1054 ssize_t bytes = snprintf (buf, sizeof (buf), ".LA%u", seqno);
1055 gcc_assert ((size_t) bytes < sizeof (buf));
1057 label = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1058 SYMBOL_REF_FLAGS (label) |= SYMBOL_FLAG_LOCAL;
1060 if (temp == NULL)
1061 temp = gen_reg_rtx (Pmode);
1063 if (Pmode == DImode)
1064 emit_insn (gen_auipcdi (temp, copy_rtx (addr), GEN_INT (seqno)));
1065 else
1066 emit_insn (gen_auipcsi (temp, copy_rtx (addr), GEN_INT (seqno)));
1068 *low_out = gen_rtx_LO_SUM (Pmode, temp, label);
1070 seqno++;
1072 break;
1074 default:
1075 gcc_unreachable ();
1078 return true;
1081 /* Return a legitimate address for REG + OFFSET. TEMP is as for
1082 riscv_force_temporary; it is only needed when OFFSET is not a
1083 SMALL_OPERAND. */
1085 static rtx
1086 riscv_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
1088 if (!SMALL_OPERAND (offset))
1090 rtx high;
1092 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
1093 The addition inside the macro CONST_HIGH_PART may cause an
1094 overflow, so we need to force a sign-extension check. */
1095 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
1096 offset = CONST_LOW_PART (offset);
1097 high = riscv_force_temporary (temp, high);
1098 reg = riscv_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
1100 return plus_constant (Pmode, reg, offset);
1103 /* The __tls_get_attr symbol. */
1104 static GTY(()) rtx riscv_tls_symbol;
1106 /* Return an instruction sequence that calls __tls_get_addr. SYM is
1107 the TLS symbol we are referencing and TYPE is the symbol type to use
1108 (either global dynamic or local dynamic). RESULT is an RTX for the
1109 return value location. */
1111 static rtx_insn *
1112 riscv_call_tls_get_addr (rtx sym, rtx result)
1114 rtx a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST), func;
1115 rtx_insn *insn;
1117 if (!riscv_tls_symbol)
1118 riscv_tls_symbol = init_one_libfunc ("__tls_get_addr");
1119 func = gen_rtx_MEM (FUNCTION_MODE, riscv_tls_symbol);
1121 start_sequence ();
1123 emit_insn (riscv_got_load_tls_gd (a0, sym));
1124 insn = emit_call_insn (gen_call_value (result, func, const0_rtx, NULL));
1125 RTL_CONST_CALL_P (insn) = 1;
1126 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
1127 insn = get_insns ();
1129 end_sequence ();
1131 return insn;
1134 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
1135 its address. The return value will be both a valid address and a valid
1136 SET_SRC (either a REG or a LO_SUM). */
1138 static rtx
1139 riscv_legitimize_tls_address (rtx loc)
1141 rtx dest, tp, tmp;
1142 enum tls_model model = SYMBOL_REF_TLS_MODEL (loc);
1144 /* Since we support TLS copy relocs, non-PIC TLS accesses may all use LE. */
1145 if (!flag_pic)
1146 model = TLS_MODEL_LOCAL_EXEC;
1148 switch (model)
1150 case TLS_MODEL_LOCAL_DYNAMIC:
1151 /* Rely on section anchors for the optimization that LDM TLS
1152 provides. The anchor's address is loaded with GD TLS. */
1153 case TLS_MODEL_GLOBAL_DYNAMIC:
1154 tmp = gen_rtx_REG (Pmode, GP_RETURN);
1155 dest = gen_reg_rtx (Pmode);
1156 emit_libcall_block (riscv_call_tls_get_addr (loc, tmp), dest, tmp, loc);
1157 break;
1159 case TLS_MODEL_INITIAL_EXEC:
1160 /* la.tls.ie; tp-relative add */
1161 tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
1162 tmp = gen_reg_rtx (Pmode);
1163 emit_insn (riscv_got_load_tls_ie (tmp, loc));
1164 dest = gen_reg_rtx (Pmode);
1165 emit_insn (gen_add3_insn (dest, tmp, tp));
1166 break;
1168 case TLS_MODEL_LOCAL_EXEC:
1169 tmp = riscv_unspec_offset_high (NULL, loc, SYMBOL_TLS_LE);
1170 dest = gen_reg_rtx (Pmode);
1171 emit_insn (riscv_tls_add_tp_le (dest, tmp, loc));
1172 dest = gen_rtx_LO_SUM (Pmode, dest,
1173 riscv_unspec_address (loc, SYMBOL_TLS_LE));
1174 break;
1176 default:
1177 gcc_unreachable ();
1179 return dest;
1182 /* If X is not a valid address for mode MODE, force it into a register. */
1184 static rtx
1185 riscv_force_address (rtx x, machine_mode mode)
1187 if (!riscv_legitimate_address_p (mode, x, false))
1188 x = force_reg (Pmode, x);
1189 return x;
1192 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
1193 be legitimized in a way that the generic machinery might not expect,
1194 return a new address, otherwise return NULL. MODE is the mode of
1195 the memory being accessed. */
1197 static rtx
1198 riscv_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1199 machine_mode mode)
1201 rtx addr;
1203 if (riscv_tls_symbol_p (x))
1204 return riscv_legitimize_tls_address (x);
1206 /* See if the address can split into a high part and a LO_SUM. */
1207 if (riscv_split_symbol (NULL, x, mode, &addr))
1208 return riscv_force_address (addr, mode);
1210 /* Handle BASE + OFFSET using riscv_add_offset. */
1211 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1))
1212 && INTVAL (XEXP (x, 1)) != 0)
1214 rtx base = XEXP (x, 0);
1215 HOST_WIDE_INT offset = INTVAL (XEXP (x, 1));
1217 if (!riscv_valid_base_register_p (base, mode, false))
1218 base = copy_to_mode_reg (Pmode, base);
1219 addr = riscv_add_offset (NULL, base, offset);
1220 return riscv_force_address (addr, mode);
1223 return x;
1226 /* Load VALUE into DEST. TEMP is as for riscv_force_temporary. */
1228 void
1229 riscv_move_integer (rtx temp, rtx dest, HOST_WIDE_INT value)
1231 struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
1232 machine_mode mode;
1233 int i, num_ops;
1234 rtx x;
1236 mode = GET_MODE (dest);
1237 num_ops = riscv_build_integer (codes, value, mode);
1239 if (can_create_pseudo_p () && num_ops > 2 /* not a simple constant */
1240 && num_ops >= riscv_split_integer_cost (value))
1241 x = riscv_split_integer (value, mode);
1242 else
1244 /* Apply each binary operation to X. */
1245 x = GEN_INT (codes[0].value);
1247 for (i = 1; i < num_ops; i++)
1249 if (!can_create_pseudo_p ())
1250 x = riscv_emit_set (temp, x);
1251 else
1252 x = force_reg (mode, x);
1254 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
1258 riscv_emit_set (dest, x);
1261 /* Subroutine of riscv_legitimize_move. Move constant SRC into register
1262 DEST given that SRC satisfies immediate_operand but doesn't satisfy
1263 move_operand. */
1265 static void
1266 riscv_legitimize_const_move (machine_mode mode, rtx dest, rtx src)
1268 rtx base, offset;
1270 /* Split moves of big integers into smaller pieces. */
1271 if (splittable_const_int_operand (src, mode))
1273 riscv_move_integer (dest, dest, INTVAL (src));
1274 return;
1277 /* Split moves of symbolic constants into high/low pairs. */
1278 if (riscv_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
1280 riscv_emit_set (dest, src);
1281 return;
1284 /* Generate the appropriate access sequences for TLS symbols. */
1285 if (riscv_tls_symbol_p (src))
1287 riscv_emit_move (dest, riscv_legitimize_tls_address (src));
1288 return;
1291 /* If we have (const (plus symbol offset)), and that expression cannot
1292 be forced into memory, load the symbol first and add in the offset. Also
1293 prefer to do this even if the constant _can_ be forced into memory, as it
1294 usually produces better code. */
1295 split_const (src, &base, &offset);
1296 if (offset != const0_rtx
1297 && (targetm.cannot_force_const_mem (mode, src) || can_create_pseudo_p ()))
1299 base = riscv_force_temporary (dest, base);
1300 riscv_emit_move (dest, riscv_add_offset (NULL, base, INTVAL (offset)));
1301 return;
1304 src = force_const_mem (mode, src);
1306 /* When using explicit relocs, constant pool references are sometimes
1307 not legitimate addresses. */
1308 riscv_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
1309 riscv_emit_move (dest, src);
1312 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
1313 sequence that is valid. */
1315 bool
1316 riscv_legitimize_move (machine_mode mode, rtx dest, rtx src)
1318 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
1320 riscv_emit_move (dest, force_reg (mode, src));
1321 return true;
1324 /* We need to deal with constants that would be legitimate
1325 immediate_operands but aren't legitimate move_operands. */
1326 if (CONSTANT_P (src) && !move_operand (src, mode))
1328 riscv_legitimize_const_move (mode, dest, src);
1329 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
1330 return true;
1333 return false;
1336 /* Return true if there is an instruction that implements CODE and accepts
1337 X as an immediate operand. */
1339 static int
1340 riscv_immediate_operand_p (int code, HOST_WIDE_INT x)
1342 switch (code)
1344 case ASHIFT:
1345 case ASHIFTRT:
1346 case LSHIFTRT:
1347 /* All shift counts are truncated to a valid constant. */
1348 return true;
1350 case AND:
1351 case IOR:
1352 case XOR:
1353 case PLUS:
1354 case LT:
1355 case LTU:
1356 /* These instructions take 12-bit signed immediates. */
1357 return SMALL_OPERAND (x);
1359 case LE:
1360 /* We add 1 to the immediate and use SLT. */
1361 return SMALL_OPERAND (x + 1);
1363 case LEU:
1364 /* Likewise SLTU, but reject the always-true case. */
1365 return SMALL_OPERAND (x + 1) && x + 1 != 0;
1367 case GE:
1368 case GEU:
1369 /* We can emulate an immediate of 1 by using GT/GTU against x0. */
1370 return x == 1;
1372 default:
1373 /* By default assume that x0 can be used for 0. */
1374 return x == 0;
1378 /* Return the cost of binary operation X, given that the instruction
1379 sequence for a word-sized or smaller operation takes SIGNLE_INSNS
1380 instructions and that the sequence of a double-word operation takes
1381 DOUBLE_INSNS instructions. */
1383 static int
1384 riscv_binary_cost (rtx x, int single_insns, int double_insns)
1386 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
1387 return COSTS_N_INSNS (double_insns);
1388 return COSTS_N_INSNS (single_insns);
1391 /* Return the cost of sign- or zero-extending OP. */
1393 static int
1394 riscv_extend_cost (rtx op, bool unsigned_p)
1396 if (MEM_P (op))
1397 return 0;
1399 if (unsigned_p && GET_MODE (op) == QImode)
1400 /* We can use ANDI. */
1401 return COSTS_N_INSNS (1);
1403 if (!unsigned_p && GET_MODE (op) == SImode)
1404 /* We can use SEXT.W. */
1405 return COSTS_N_INSNS (1);
1407 /* We need to use a shift left and a shift right. */
1408 return COSTS_N_INSNS (2);
1411 /* Implement TARGET_RTX_COSTS. */
1413 static bool
1414 riscv_rtx_costs (rtx x, machine_mode mode, int outer_code, int opno ATTRIBUTE_UNUSED,
1415 int *total, bool speed)
1417 bool float_mode_p = FLOAT_MODE_P (mode);
1418 int cost;
1420 switch (GET_CODE (x))
1422 case CONST_INT:
1423 if (riscv_immediate_operand_p (outer_code, INTVAL (x)))
1425 *total = 0;
1426 return true;
1428 /* Fall through. */
1430 case SYMBOL_REF:
1431 case LABEL_REF:
1432 case CONST_DOUBLE:
1433 case CONST:
1434 if ((cost = riscv_const_insns (x)) > 0)
1436 /* If the constant is likely to be stored in a GPR, SETs of
1437 single-insn constants are as cheap as register sets; we
1438 never want to CSE them. */
1439 if (cost == 1 && outer_code == SET)
1440 *total = 0;
1441 /* When we load a constant more than once, it usually is better
1442 to duplicate the last operation in the sequence than to CSE
1443 the constant itself. */
1444 else if (outer_code == SET || GET_MODE (x) == VOIDmode)
1445 *total = COSTS_N_INSNS (1);
1447 else /* The instruction will be fetched from the constant pool. */
1448 *total = COSTS_N_INSNS (riscv_symbol_insns (SYMBOL_ABSOLUTE));
1449 return true;
1451 case MEM:
1452 /* If the address is legitimate, return the number of
1453 instructions it needs. */
1454 if ((cost = riscv_address_insns (XEXP (x, 0), mode, true)) > 0)
1456 *total = COSTS_N_INSNS (cost + tune_info->memory_cost);
1457 return true;
1459 /* Otherwise use the default handling. */
1460 return false;
1462 case NOT:
1463 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
1464 return false;
1466 case AND:
1467 case IOR:
1468 case XOR:
1469 /* Double-word operations use two single-word operations. */
1470 *total = riscv_binary_cost (x, 1, 2);
1471 return false;
1473 case ASHIFT:
1474 case ASHIFTRT:
1475 case LSHIFTRT:
1476 *total = riscv_binary_cost (x, 1, CONSTANT_P (XEXP (x, 1)) ? 4 : 9);
1477 return false;
1479 case ABS:
1480 *total = COSTS_N_INSNS (float_mode_p ? 1 : 3);
1481 return false;
1483 case LO_SUM:
1484 *total = set_src_cost (XEXP (x, 0), mode, speed);
1485 return true;
1487 case LT:
1488 case LTU:
1489 case LE:
1490 case LEU:
1491 case GT:
1492 case GTU:
1493 case GE:
1494 case GEU:
1495 case EQ:
1496 case NE:
1497 /* Branch comparisons have VOIDmode, so use the first operand's
1498 mode instead. */
1499 mode = GET_MODE (XEXP (x, 0));
1500 if (float_mode_p)
1501 *total = tune_info->fp_add[mode == DFmode];
1502 else
1503 *total = riscv_binary_cost (x, 1, 3);
1504 return false;
1506 case UNORDERED:
1507 case ORDERED:
1508 /* (FEQ(A, A) & FEQ(B, B)) compared against 0. */
1509 mode = GET_MODE (XEXP (x, 0));
1510 *total = tune_info->fp_add[mode == DFmode] + COSTS_N_INSNS (2);
1511 return false;
1513 case UNEQ:
1514 case LTGT:
1515 /* (FEQ(A, A) & FEQ(B, B)) compared against FEQ(A, B). */
1516 mode = GET_MODE (XEXP (x, 0));
1517 *total = tune_info->fp_add[mode == DFmode] + COSTS_N_INSNS (3);
1518 return false;
1520 case UNGE:
1521 case UNGT:
1522 case UNLE:
1523 case UNLT:
1524 /* FLT or FLE, but guarded by an FFLAGS read and write. */
1525 mode = GET_MODE (XEXP (x, 0));
1526 *total = tune_info->fp_add[mode == DFmode] + COSTS_N_INSNS (4);
1527 return false;
1529 case MINUS:
1530 case PLUS:
1531 if (float_mode_p)
1532 *total = tune_info->fp_add[mode == DFmode];
1533 else
1534 *total = riscv_binary_cost (x, 1, 4);
1535 return false;
1537 case NEG:
1539 rtx op = XEXP (x, 0);
1540 if (GET_CODE (op) == FMA && !HONOR_SIGNED_ZEROS (mode))
1542 *total = (tune_info->fp_mul[mode == DFmode]
1543 + set_src_cost (XEXP (op, 0), mode, speed)
1544 + set_src_cost (XEXP (op, 1), mode, speed)
1545 + set_src_cost (XEXP (op, 2), mode, speed));
1546 return true;
1550 if (float_mode_p)
1551 *total = tune_info->fp_add[mode == DFmode];
1552 else
1553 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
1554 return false;
1556 case MULT:
1557 if (float_mode_p)
1558 *total = tune_info->fp_mul[mode == DFmode];
1559 else if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
1560 *total = 3 * tune_info->int_mul[0] + COSTS_N_INSNS (2);
1561 else if (!speed)
1562 *total = COSTS_N_INSNS (1);
1563 else
1564 *total = tune_info->int_mul[mode == DImode];
1565 return false;
1567 case DIV:
1568 case SQRT:
1569 case MOD:
1570 if (float_mode_p)
1572 *total = tune_info->fp_div[mode == DFmode];
1573 return false;
1575 /* Fall through. */
1577 case UDIV:
1578 case UMOD:
1579 if (speed)
1580 *total = tune_info->int_div[mode == DImode];
1581 else
1582 *total = COSTS_N_INSNS (1);
1583 return false;
1585 case SIGN_EXTEND:
1586 case ZERO_EXTEND:
1587 *total = riscv_extend_cost (XEXP (x, 0), GET_CODE (x) == ZERO_EXTEND);
1588 return false;
1590 case FLOAT:
1591 case UNSIGNED_FLOAT:
1592 case FIX:
1593 case FLOAT_EXTEND:
1594 case FLOAT_TRUNCATE:
1595 *total = tune_info->fp_add[mode == DFmode];
1596 return false;
1598 case FMA:
1599 *total = (tune_info->fp_mul[mode == DFmode]
1600 + set_src_cost (XEXP (x, 0), mode, speed)
1601 + set_src_cost (XEXP (x, 1), mode, speed)
1602 + set_src_cost (XEXP (x, 2), mode, speed));
1603 return true;
1605 case UNSPEC:
1606 if (XINT (x, 1) == UNSPEC_AUIPC)
1608 /* Make AUIPC cheap to avoid spilling its result to the stack. */
1609 *total = 1;
1610 return true;
1612 return false;
1614 default:
1615 return false;
1619 /* Implement TARGET_ADDRESS_COST. */
1621 static int
1622 riscv_address_cost (rtx addr, machine_mode mode,
1623 addr_space_t as ATTRIBUTE_UNUSED,
1624 bool speed ATTRIBUTE_UNUSED)
1626 return riscv_address_insns (addr, mode, false);
1629 /* Return one word of double-word value OP. HIGH_P is true to select the
1630 high part or false to select the low part. */
1633 riscv_subword (rtx op, bool high_p)
1635 unsigned int byte = high_p ? UNITS_PER_WORD : 0;
1636 machine_mode mode = GET_MODE (op);
1638 if (mode == VOIDmode)
1639 mode = TARGET_64BIT ? TImode : DImode;
1641 if (MEM_P (op))
1642 return adjust_address (op, word_mode, byte);
1644 if (REG_P (op))
1645 gcc_assert (!FP_REG_RTX_P (op));
1647 return simplify_gen_subreg (word_mode, op, mode, byte);
1650 /* Return true if a 64-bit move from SRC to DEST should be split into two. */
1652 bool
1653 riscv_split_64bit_move_p (rtx dest, rtx src)
1655 if (TARGET_64BIT)
1656 return false;
1658 /* Allow FPR <-> FPR and FPR <-> MEM moves, and permit the special case
1659 of zeroing an FPR with FCVT.D.W. */
1660 if (TARGET_DOUBLE_FLOAT
1661 && ((FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
1662 || (FP_REG_RTX_P (dest) && MEM_P (src))
1663 || (FP_REG_RTX_P (src) && MEM_P (dest))
1664 || (FP_REG_RTX_P (dest) && src == CONST0_RTX (GET_MODE (src)))))
1665 return false;
1667 return true;
1670 /* Split a doubleword move from SRC to DEST. On 32-bit targets,
1671 this function handles 64-bit moves for which riscv_split_64bit_move_p
1672 holds. For 64-bit targets, this function handles 128-bit moves. */
1674 void
1675 riscv_split_doubleword_move (rtx dest, rtx src)
1677 rtx low_dest;
1679 /* The operation can be split into two normal moves. Decide in
1680 which order to do them. */
1681 low_dest = riscv_subword (dest, false);
1682 if (REG_P (low_dest) && reg_overlap_mentioned_p (low_dest, src))
1684 riscv_emit_move (riscv_subword (dest, true), riscv_subword (src, true));
1685 riscv_emit_move (low_dest, riscv_subword (src, false));
1687 else
1689 riscv_emit_move (low_dest, riscv_subword (src, false));
1690 riscv_emit_move (riscv_subword (dest, true), riscv_subword (src, true));
1694 /* Return the appropriate instructions to move SRC into DEST. Assume
1695 that SRC is operand 1 and DEST is operand 0. */
1697 const char *
1698 riscv_output_move (rtx dest, rtx src)
1700 enum rtx_code dest_code, src_code;
1701 machine_mode mode;
1702 bool dbl_p;
1704 dest_code = GET_CODE (dest);
1705 src_code = GET_CODE (src);
1706 mode = GET_MODE (dest);
1707 dbl_p = (GET_MODE_SIZE (mode) == 8);
1709 if (dbl_p && riscv_split_64bit_move_p (dest, src))
1710 return "#";
1712 if (dest_code == REG && GP_REG_P (REGNO (dest)))
1714 if (src_code == REG && FP_REG_P (REGNO (src)))
1715 return dbl_p ? "fmv.x.d\t%0,%1" : "fmv.x.s\t%0,%1";
1717 if (src_code == MEM)
1718 switch (GET_MODE_SIZE (mode))
1720 case 1: return "lbu\t%0,%1";
1721 case 2: return "lhu\t%0,%1";
1722 case 4: return "lw\t%0,%1";
1723 case 8: return "ld\t%0,%1";
1726 if (src_code == CONST_INT)
1727 return "li\t%0,%1";
1729 if (src_code == HIGH)
1730 return "lui\t%0,%h1";
1732 if (symbolic_operand (src, VOIDmode))
1733 switch (riscv_classify_symbolic_expression (src))
1735 case SYMBOL_GOT_DISP: return "la\t%0,%1";
1736 case SYMBOL_ABSOLUTE: return "lla\t%0,%1";
1737 case SYMBOL_PCREL: return "lla\t%0,%1";
1738 default: gcc_unreachable ();
1741 if ((src_code == REG && GP_REG_P (REGNO (src)))
1742 || (src == CONST0_RTX (mode)))
1744 if (dest_code == REG)
1746 if (GP_REG_P (REGNO (dest)))
1747 return "mv\t%0,%z1";
1749 if (FP_REG_P (REGNO (dest)))
1751 if (!dbl_p)
1752 return "fmv.s.x\t%0,%z1";
1753 if (TARGET_64BIT)
1754 return "fmv.d.x\t%0,%z1";
1755 /* in RV32, we can emulate fmv.d.x %0, x0 using fcvt.d.w */
1756 gcc_assert (src == CONST0_RTX (mode));
1757 return "fcvt.d.w\t%0,x0";
1760 if (dest_code == MEM)
1761 switch (GET_MODE_SIZE (mode))
1763 case 1: return "sb\t%z1,%0";
1764 case 2: return "sh\t%z1,%0";
1765 case 4: return "sw\t%z1,%0";
1766 case 8: return "sd\t%z1,%0";
1769 if (src_code == REG && FP_REG_P (REGNO (src)))
1771 if (dest_code == REG && FP_REG_P (REGNO (dest)))
1772 return dbl_p ? "fmv.d\t%0,%1" : "fmv.s\t%0,%1";
1774 if (dest_code == MEM)
1775 return dbl_p ? "fsd\t%1,%0" : "fsw\t%1,%0";
1777 if (dest_code == REG && FP_REG_P (REGNO (dest)))
1779 if (src_code == MEM)
1780 return dbl_p ? "fld\t%0,%1" : "flw\t%0,%1";
1782 gcc_unreachable ();
1785 /* Return true if CMP1 is a suitable second operand for integer ordering
1786 test CODE. See also the *sCC patterns in riscv.md. */
1788 static bool
1789 riscv_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
1791 switch (code)
1793 case GT:
1794 case GTU:
1795 return reg_or_0_operand (cmp1, VOIDmode);
1797 case GE:
1798 case GEU:
1799 return cmp1 == const1_rtx;
1801 case LT:
1802 case LTU:
1803 return arith_operand (cmp1, VOIDmode);
1805 case LE:
1806 return sle_operand (cmp1, VOIDmode);
1808 case LEU:
1809 return sleu_operand (cmp1, VOIDmode);
1811 default:
1812 gcc_unreachable ();
1816 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
1817 integer ordering test *CODE, or if an equivalent combination can
1818 be formed by adjusting *CODE and *CMP1. When returning true, update
1819 *CODE and *CMP1 with the chosen code and operand, otherwise leave
1820 them alone. */
1822 static bool
1823 riscv_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
1824 machine_mode mode)
1826 HOST_WIDE_INT plus_one;
1828 if (riscv_int_order_operand_ok_p (*code, *cmp1))
1829 return true;
1831 if (CONST_INT_P (*cmp1))
1832 switch (*code)
1834 case LE:
1835 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
1836 if (INTVAL (*cmp1) < plus_one)
1838 *code = LT;
1839 *cmp1 = force_reg (mode, GEN_INT (plus_one));
1840 return true;
1842 break;
1844 case LEU:
1845 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
1846 if (plus_one != 0)
1848 *code = LTU;
1849 *cmp1 = force_reg (mode, GEN_INT (plus_one));
1850 return true;
1852 break;
1854 default:
1855 break;
1857 return false;
1860 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
1861 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
1862 is nonnull, it's OK to set TARGET to the inverse of the result and
1863 flip *INVERT_PTR instead. */
1865 static void
1866 riscv_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
1867 rtx target, rtx cmp0, rtx cmp1)
1869 machine_mode mode;
1871 /* First see if there is a RISCV instruction that can do this operation.
1872 If not, try doing the same for the inverse operation. If that also
1873 fails, force CMP1 into a register and try again. */
1874 mode = GET_MODE (cmp0);
1875 if (riscv_canonicalize_int_order_test (&code, &cmp1, mode))
1876 riscv_emit_binary (code, target, cmp0, cmp1);
1877 else
1879 enum rtx_code inv_code = reverse_condition (code);
1880 if (!riscv_canonicalize_int_order_test (&inv_code, &cmp1, mode))
1882 cmp1 = force_reg (mode, cmp1);
1883 riscv_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
1885 else if (invert_ptr == 0)
1887 rtx inv_target = riscv_force_binary (GET_MODE (target),
1888 inv_code, cmp0, cmp1);
1889 riscv_emit_binary (XOR, target, inv_target, const1_rtx);
1891 else
1893 *invert_ptr = !*invert_ptr;
1894 riscv_emit_binary (inv_code, target, cmp0, cmp1);
1899 /* Return a register that is zero iff CMP0 and CMP1 are equal.
1900 The register will have the same mode as CMP0. */
1902 static rtx
1903 riscv_zero_if_equal (rtx cmp0, rtx cmp1)
1905 if (cmp1 == const0_rtx)
1906 return cmp0;
1908 return expand_binop (GET_MODE (cmp0), sub_optab,
1909 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
1912 /* Sign- or zero-extend OP0 and OP1 for integer comparisons. */
1914 static void
1915 riscv_extend_comparands (rtx_code code, rtx *op0, rtx *op1)
1917 /* Comparisons consider all XLEN bits, so extend sub-XLEN values. */
1918 if (GET_MODE_SIZE (word_mode) > GET_MODE_SIZE (GET_MODE (*op0)))
1920 /* It is more profitable to zero-extend QImode values. */
1921 if (unsigned_condition (code) == code && GET_MODE (*op0) == QImode)
1923 *op0 = gen_rtx_ZERO_EXTEND (word_mode, *op0);
1924 if (CONST_INT_P (*op1))
1925 *op1 = GEN_INT ((uint8_t) INTVAL (*op1));
1926 else
1927 *op1 = gen_rtx_ZERO_EXTEND (word_mode, *op1);
1929 else
1931 *op0 = gen_rtx_SIGN_EXTEND (word_mode, *op0);
1932 if (*op1 != const0_rtx)
1933 *op1 = gen_rtx_SIGN_EXTEND (word_mode, *op1);
1938 /* Convert a comparison into something that can be used in a branch. On
1939 entry, *OP0 and *OP1 are the values being compared and *CODE is the code
1940 used to compare them. Update them to describe the final comparison. */
1942 static void
1943 riscv_emit_int_compare (enum rtx_code *code, rtx *op0, rtx *op1)
1945 if (splittable_const_int_operand (*op1, VOIDmode))
1947 HOST_WIDE_INT rhs = INTVAL (*op1);
1949 if (*code == EQ || *code == NE)
1951 /* Convert e.g. OP0 == 2048 into OP0 - 2048 == 0. */
1952 if (SMALL_OPERAND (-rhs))
1954 *op0 = riscv_force_binary (GET_MODE (*op0), PLUS, *op0,
1955 GEN_INT (-rhs));
1956 *op1 = const0_rtx;
1959 else
1961 static const enum rtx_code mag_comparisons[][2] = {
1962 {LEU, LTU}, {GTU, GEU}, {LE, LT}, {GT, GE}
1965 /* Convert e.g. (OP0 <= 0xFFF) into (OP0 < 0x1000). */
1966 for (size_t i = 0; i < ARRAY_SIZE (mag_comparisons); i++)
1968 HOST_WIDE_INT new_rhs;
1969 bool increment = *code == mag_comparisons[i][0];
1970 bool decrement = *code == mag_comparisons[i][1];
1971 if (!increment && !decrement)
1972 continue;
1974 new_rhs = rhs + (increment ? 1 : -1);
1975 if (riscv_integer_cost (new_rhs) < riscv_integer_cost (rhs)
1976 && (rhs < 0) == (new_rhs < 0))
1978 *op1 = GEN_INT (new_rhs);
1979 *code = mag_comparisons[i][increment];
1981 break;
1986 riscv_extend_comparands (*code, op0, op1);
1988 *op0 = force_reg (word_mode, *op0);
1989 if (*op1 != const0_rtx)
1990 *op1 = force_reg (word_mode, *op1);
1993 /* Like riscv_emit_int_compare, but for floating-point comparisons. */
1995 static void
1996 riscv_emit_float_compare (enum rtx_code *code, rtx *op0, rtx *op1)
1998 rtx tmp0, tmp1, cmp_op0 = *op0, cmp_op1 = *op1;
1999 enum rtx_code fp_code = *code;
2000 *code = NE;
2002 switch (fp_code)
2004 case UNORDERED:
2005 *code = EQ;
2006 /* Fall through. */
2008 case ORDERED:
2009 /* a == a && b == b */
2010 tmp0 = riscv_force_binary (word_mode, EQ, cmp_op0, cmp_op0);
2011 tmp1 = riscv_force_binary (word_mode, EQ, cmp_op1, cmp_op1);
2012 *op0 = riscv_force_binary (word_mode, AND, tmp0, tmp1);
2013 *op1 = const0_rtx;
2014 break;
2016 case UNEQ:
2017 case LTGT:
2018 /* ordered(a, b) > (a == b) */
2019 *code = fp_code == LTGT ? GTU : EQ;
2020 tmp0 = riscv_force_binary (word_mode, EQ, cmp_op0, cmp_op0);
2021 tmp1 = riscv_force_binary (word_mode, EQ, cmp_op1, cmp_op1);
2022 *op0 = riscv_force_binary (word_mode, AND, tmp0, tmp1);
2023 *op1 = riscv_force_binary (word_mode, EQ, cmp_op0, cmp_op1);
2024 break;
2026 #define UNORDERED_COMPARISON(CODE, CMP) \
2027 case CODE: \
2028 *code = EQ; \
2029 *op0 = gen_reg_rtx (word_mode); \
2030 if (GET_MODE (cmp_op0) == SFmode && TARGET_64BIT) \
2031 emit_insn (gen_f##CMP##_quietsfdi4 (*op0, cmp_op0, cmp_op1)); \
2032 else if (GET_MODE (cmp_op0) == SFmode) \
2033 emit_insn (gen_f##CMP##_quietsfsi4 (*op0, cmp_op0, cmp_op1)); \
2034 else if (GET_MODE (cmp_op0) == DFmode && TARGET_64BIT) \
2035 emit_insn (gen_f##CMP##_quietdfdi4 (*op0, cmp_op0, cmp_op1)); \
2036 else if (GET_MODE (cmp_op0) == DFmode) \
2037 emit_insn (gen_f##CMP##_quietdfsi4 (*op0, cmp_op0, cmp_op1)); \
2038 else \
2039 gcc_unreachable (); \
2040 *op1 = const0_rtx; \
2041 break;
2043 case UNLT:
2044 std::swap (cmp_op0, cmp_op1);
2045 gcc_fallthrough ();
2047 UNORDERED_COMPARISON(UNGT, le)
2049 case UNLE:
2050 std::swap (cmp_op0, cmp_op1);
2051 gcc_fallthrough ();
2053 UNORDERED_COMPARISON(UNGE, lt)
2054 #undef UNORDERED_COMPARISON
2056 case NE:
2057 fp_code = EQ;
2058 *code = EQ;
2059 /* Fall through. */
2061 case EQ:
2062 case LE:
2063 case LT:
2064 case GE:
2065 case GT:
2066 /* We have instructions for these cases. */
2067 *op0 = riscv_force_binary (word_mode, fp_code, cmp_op0, cmp_op1);
2068 *op1 = const0_rtx;
2069 break;
2071 default:
2072 gcc_unreachable ();
2076 /* CODE-compare OP0 and OP1. Store the result in TARGET. */
2078 void
2079 riscv_expand_int_scc (rtx target, enum rtx_code code, rtx op0, rtx op1)
2081 riscv_extend_comparands (code, &op0, &op1);
2082 op0 = force_reg (word_mode, op0);
2084 if (code == EQ || code == NE)
2086 rtx zie = riscv_zero_if_equal (op0, op1);
2087 riscv_emit_binary (code, target, zie, const0_rtx);
2089 else
2090 riscv_emit_int_order_test (code, 0, target, op0, op1);
2093 /* Like riscv_expand_int_scc, but for floating-point comparisons. */
2095 void
2096 riscv_expand_float_scc (rtx target, enum rtx_code code, rtx op0, rtx op1)
2098 riscv_emit_float_compare (&code, &op0, &op1);
2100 rtx cmp = riscv_force_binary (word_mode, code, op0, op1);
2101 riscv_emit_set (target, lowpart_subreg (SImode, cmp, word_mode));
2104 /* Jump to LABEL if (CODE OP0 OP1) holds. */
2106 void
2107 riscv_expand_conditional_branch (rtx label, rtx_code code, rtx op0, rtx op1)
2109 if (FLOAT_MODE_P (GET_MODE (op1)))
2110 riscv_emit_float_compare (&code, &op0, &op1);
2111 else
2112 riscv_emit_int_compare (&code, &op0, &op1);
2114 rtx condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
2115 emit_jump_insn (gen_condjump (condition, label));
2118 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Every parameter gets at
2119 least PARM_BOUNDARY bits of alignment, but will be given anything up
2120 to STACK_BOUNDARY bits if the type requires it. */
2122 static unsigned int
2123 riscv_function_arg_boundary (machine_mode mode, const_tree type)
2125 unsigned int alignment;
2127 /* Use natural alignment if the type is not aggregate data. */
2128 if (type && !AGGREGATE_TYPE_P (type))
2129 alignment = TYPE_ALIGN (TYPE_MAIN_VARIANT (type));
2130 else
2131 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
2133 return MIN (STACK_BOUNDARY, MAX (PARM_BOUNDARY, alignment));
2136 /* If MODE represents an argument that can be passed or returned in
2137 floating-point registers, return the number of registers, else 0. */
2139 static unsigned
2140 riscv_pass_mode_in_fpr_p (machine_mode mode)
2142 if (GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FP_ARG)
2144 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2145 return 1;
2147 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
2148 return 2;
2151 return 0;
2154 typedef struct {
2155 const_tree type;
2156 HOST_WIDE_INT offset;
2157 } riscv_aggregate_field;
2159 /* Identify subfields of aggregates that are candidates for passing in
2160 floating-point registers. */
2162 static int
2163 riscv_flatten_aggregate_field (const_tree type,
2164 riscv_aggregate_field fields[2],
2165 int n, HOST_WIDE_INT offset)
2167 switch (TREE_CODE (type))
2169 case RECORD_TYPE:
2170 /* Can't handle incomplete types nor sizes that are not fixed. */
2171 if (!COMPLETE_TYPE_P (type)
2172 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
2173 || !tree_fits_uhwi_p (TYPE_SIZE (type)))
2174 return -1;
2176 for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
2177 if (TREE_CODE (f) == FIELD_DECL)
2179 if (!TYPE_P (TREE_TYPE (f)))
2180 return -1;
2182 HOST_WIDE_INT pos = offset + int_byte_position (f);
2183 n = riscv_flatten_aggregate_field (TREE_TYPE (f), fields, n, pos);
2184 if (n < 0)
2185 return -1;
2187 return n;
2189 case ARRAY_TYPE:
2191 HOST_WIDE_INT n_elts;
2192 riscv_aggregate_field subfields[2];
2193 tree index = TYPE_DOMAIN (type);
2194 tree elt_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
2195 int n_subfields = riscv_flatten_aggregate_field (TREE_TYPE (type),
2196 subfields, 0, offset);
2198 /* Can't handle incomplete types nor sizes that are not fixed. */
2199 if (n_subfields <= 0
2200 || !COMPLETE_TYPE_P (type)
2201 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
2202 || !index
2203 || !TYPE_MAX_VALUE (index)
2204 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
2205 || !TYPE_MIN_VALUE (index)
2206 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
2207 || !tree_fits_uhwi_p (elt_size))
2208 return -1;
2210 n_elts = 1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
2211 - tree_to_uhwi (TYPE_MIN_VALUE (index));
2212 gcc_assert (n_elts >= 0);
2214 for (HOST_WIDE_INT i = 0; i < n_elts; i++)
2215 for (int j = 0; j < n_subfields; j++)
2217 if (n >= 2)
2218 return -1;
2220 fields[n] = subfields[j];
2221 fields[n++].offset += i * tree_to_uhwi (elt_size);
2224 return n;
2227 case COMPLEX_TYPE:
2229 /* Complex type need consume 2 field, so n must be 0. */
2230 if (n != 0)
2231 return -1;
2233 HOST_WIDE_INT elt_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (type)));
2235 if (elt_size <= UNITS_PER_FP_ARG)
2237 fields[0].type = TREE_TYPE (type);
2238 fields[0].offset = offset;
2239 fields[1].type = TREE_TYPE (type);
2240 fields[1].offset = offset + elt_size;
2242 return 2;
2245 return -1;
2248 default:
2249 if (n < 2
2250 && ((SCALAR_FLOAT_TYPE_P (type)
2251 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FP_ARG)
2252 || (INTEGRAL_TYPE_P (type)
2253 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_WORD)))
2255 fields[n].type = type;
2256 fields[n].offset = offset;
2257 return n + 1;
2259 else
2260 return -1;
2264 /* Identify candidate aggregates for passing in floating-point registers.
2265 Candidates have at most two fields after flattening. */
2267 static int
2268 riscv_flatten_aggregate_argument (const_tree type,
2269 riscv_aggregate_field fields[2])
2271 if (!type || TREE_CODE (type) != RECORD_TYPE)
2272 return -1;
2274 return riscv_flatten_aggregate_field (type, fields, 0, 0);
2277 /* See whether TYPE is a record whose fields should be returned in one or
2278 two floating-point registers. If so, populate FIELDS accordingly. */
2280 static unsigned
2281 riscv_pass_aggregate_in_fpr_pair_p (const_tree type,
2282 riscv_aggregate_field fields[2])
2284 int n = riscv_flatten_aggregate_argument (type, fields);
2286 for (int i = 0; i < n; i++)
2287 if (!SCALAR_FLOAT_TYPE_P (fields[i].type))
2288 return 0;
2290 return n > 0 ? n : 0;
2293 /* See whether TYPE is a record whose fields should be returned in one or
2294 floating-point register and one integer register. If so, populate
2295 FIELDS accordingly. */
2297 static bool
2298 riscv_pass_aggregate_in_fpr_and_gpr_p (const_tree type,
2299 riscv_aggregate_field fields[2])
2301 unsigned num_int = 0, num_float = 0;
2302 int n = riscv_flatten_aggregate_argument (type, fields);
2304 for (int i = 0; i < n; i++)
2306 num_float += SCALAR_FLOAT_TYPE_P (fields[i].type);
2307 num_int += INTEGRAL_TYPE_P (fields[i].type);
2310 return num_int == 1 && num_float == 1;
2313 /* Return the representation of an argument passed or returned in an FPR
2314 when the value has mode VALUE_MODE and the type has TYPE_MODE. The
2315 two modes may be different for structures like:
2317 struct __attribute__((packed)) foo { float f; }
2319 where the SFmode value "f" is passed in REGNO but the struct itself
2320 has mode BLKmode. */
2322 static rtx
2323 riscv_pass_fpr_single (machine_mode type_mode, unsigned regno,
2324 machine_mode value_mode)
2326 rtx x = gen_rtx_REG (value_mode, regno);
2328 if (type_mode != value_mode)
2330 x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
2331 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
2333 return x;
2336 /* Pass or return a composite value in the FPR pair REGNO and REGNO + 1.
2337 MODE is the mode of the composite. MODE1 and OFFSET1 are the mode and
2338 byte offset for the first value, likewise MODE2 and OFFSET2 for the
2339 second value. */
2341 static rtx
2342 riscv_pass_fpr_pair (machine_mode mode, unsigned regno1,
2343 machine_mode mode1, HOST_WIDE_INT offset1,
2344 unsigned regno2, machine_mode mode2,
2345 HOST_WIDE_INT offset2)
2347 return gen_rtx_PARALLEL
2348 (mode,
2349 gen_rtvec (2,
2350 gen_rtx_EXPR_LIST (VOIDmode,
2351 gen_rtx_REG (mode1, regno1),
2352 GEN_INT (offset1)),
2353 gen_rtx_EXPR_LIST (VOIDmode,
2354 gen_rtx_REG (mode2, regno2),
2355 GEN_INT (offset2))));
2358 /* Fill INFO with information about a single argument, and return an
2359 RTL pattern to pass or return the argument. CUM is the cumulative
2360 state for earlier arguments. MODE is the mode of this argument and
2361 TYPE is its type (if known). NAMED is true if this is a named
2362 (fixed) argument rather than a variable one. RETURN_P is true if
2363 returning the argument, or false if passing the argument. */
2365 static rtx
2366 riscv_get_arg_info (struct riscv_arg_info *info, const CUMULATIVE_ARGS *cum,
2367 machine_mode mode, const_tree type, bool named,
2368 bool return_p)
2370 unsigned num_bytes, num_words;
2371 unsigned fpr_base = return_p ? FP_RETURN : FP_ARG_FIRST;
2372 unsigned gpr_base = return_p ? GP_RETURN : GP_ARG_FIRST;
2373 unsigned alignment = riscv_function_arg_boundary (mode, type);
2375 memset (info, 0, sizeof (*info));
2376 info->gpr_offset = cum->num_gprs;
2377 info->fpr_offset = cum->num_fprs;
2379 if (named)
2381 riscv_aggregate_field fields[2];
2382 unsigned fregno = fpr_base + info->fpr_offset;
2383 unsigned gregno = gpr_base + info->gpr_offset;
2385 /* Pass one- or two-element floating-point aggregates in FPRs. */
2386 if ((info->num_fprs = riscv_pass_aggregate_in_fpr_pair_p (type, fields))
2387 && info->fpr_offset + info->num_fprs <= MAX_ARGS_IN_REGISTERS)
2388 switch (info->num_fprs)
2390 case 1:
2391 return riscv_pass_fpr_single (mode, fregno,
2392 TYPE_MODE (fields[0].type));
2394 case 2:
2395 return riscv_pass_fpr_pair (mode, fregno,
2396 TYPE_MODE (fields[0].type),
2397 fields[0].offset,
2398 fregno + 1,
2399 TYPE_MODE (fields[1].type),
2400 fields[1].offset);
2402 default:
2403 gcc_unreachable ();
2406 /* Pass real and complex floating-point numbers in FPRs. */
2407 if ((info->num_fprs = riscv_pass_mode_in_fpr_p (mode))
2408 && info->fpr_offset + info->num_fprs <= MAX_ARGS_IN_REGISTERS)
2409 switch (GET_MODE_CLASS (mode))
2411 case MODE_FLOAT:
2412 return gen_rtx_REG (mode, fregno);
2414 case MODE_COMPLEX_FLOAT:
2415 return riscv_pass_fpr_pair (mode, fregno, GET_MODE_INNER (mode), 0,
2416 fregno + 1, GET_MODE_INNER (mode),
2417 GET_MODE_UNIT_SIZE (mode));
2419 default:
2420 gcc_unreachable ();
2423 /* Pass structs with one float and one integer in an FPR and a GPR. */
2424 if (riscv_pass_aggregate_in_fpr_and_gpr_p (type, fields)
2425 && info->gpr_offset < MAX_ARGS_IN_REGISTERS
2426 && info->fpr_offset < MAX_ARGS_IN_REGISTERS)
2428 info->num_gprs = 1;
2429 info->num_fprs = 1;
2431 if (!SCALAR_FLOAT_TYPE_P (fields[0].type))
2432 std::swap (fregno, gregno);
2434 return riscv_pass_fpr_pair (mode, fregno, TYPE_MODE (fields[0].type),
2435 fields[0].offset,
2436 gregno, TYPE_MODE (fields[1].type),
2437 fields[1].offset);
2441 /* Work out the size of the argument. */
2442 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
2443 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2445 /* Doubleword-aligned varargs start on an even register boundary. */
2446 if (!named && num_bytes != 0 && alignment > BITS_PER_WORD)
2447 info->gpr_offset += info->gpr_offset & 1;
2449 /* Partition the argument between registers and stack. */
2450 info->num_fprs = 0;
2451 info->num_gprs = MIN (num_words, MAX_ARGS_IN_REGISTERS - info->gpr_offset);
2452 info->stack_p = (num_words - info->num_gprs) != 0;
2454 if (info->num_gprs || return_p)
2455 return gen_rtx_REG (mode, gpr_base + info->gpr_offset);
2457 return NULL_RTX;
2460 /* Implement TARGET_FUNCTION_ARG. */
2462 static rtx
2463 riscv_function_arg (cumulative_args_t cum_v, machine_mode mode,
2464 const_tree type, bool named)
2466 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2467 struct riscv_arg_info info;
2469 if (mode == VOIDmode)
2470 return NULL;
2472 return riscv_get_arg_info (&info, cum, mode, type, named, false);
2475 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
2477 static void
2478 riscv_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
2479 const_tree type, bool named)
2481 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2482 struct riscv_arg_info info;
2484 riscv_get_arg_info (&info, cum, mode, type, named, false);
2486 /* Advance the register count. This has the effect of setting
2487 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
2488 argument required us to skip the final GPR and pass the whole
2489 argument on the stack. */
2490 cum->num_fprs = info.fpr_offset + info.num_fprs;
2491 cum->num_gprs = info.gpr_offset + info.num_gprs;
2494 /* Implement TARGET_ARG_PARTIAL_BYTES. */
2496 static int
2497 riscv_arg_partial_bytes (cumulative_args_t cum,
2498 machine_mode mode, tree type, bool named)
2500 struct riscv_arg_info arg;
2502 riscv_get_arg_info (&arg, get_cumulative_args (cum), mode, type, named, false);
2503 return arg.stack_p ? arg.num_gprs * UNITS_PER_WORD : 0;
2506 /* Implement FUNCTION_VALUE and LIBCALL_VALUE. For normal calls,
2507 VALTYPE is the return type and MODE is VOIDmode. For libcalls,
2508 VALTYPE is null and MODE is the mode of the return value. */
2511 riscv_function_value (const_tree type, const_tree func, machine_mode mode)
2513 struct riscv_arg_info info;
2514 CUMULATIVE_ARGS args;
2516 if (type)
2518 int unsigned_p = TYPE_UNSIGNED (type);
2520 mode = TYPE_MODE (type);
2522 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
2523 return values, promote the mode here too. */
2524 mode = promote_function_mode (type, mode, &unsigned_p, func, 1);
2527 memset (&args, 0, sizeof args);
2528 return riscv_get_arg_info (&info, &args, mode, type, true, true);
2531 /* Implement TARGET_PASS_BY_REFERENCE. */
2533 static bool
2534 riscv_pass_by_reference (cumulative_args_t cum_v, machine_mode mode,
2535 const_tree type, bool named)
2537 HOST_WIDE_INT size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
2538 struct riscv_arg_info info;
2539 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2541 /* ??? std_gimplify_va_arg_expr passes NULL for cum. Fortunately, we
2542 never pass variadic arguments in floating-point registers, so we can
2543 avoid the call to riscv_get_arg_info in this case. */
2544 if (cum != NULL)
2546 /* Don't pass by reference if we can use a floating-point register. */
2547 riscv_get_arg_info (&info, cum, mode, type, named, false);
2548 if (info.num_fprs)
2549 return false;
2552 /* Pass by reference if the data do not fit in two integer registers. */
2553 return !IN_RANGE (size, 0, 2 * UNITS_PER_WORD);
2556 /* Implement TARGET_RETURN_IN_MEMORY. */
2558 static bool
2559 riscv_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
2561 CUMULATIVE_ARGS args;
2562 cumulative_args_t cum = pack_cumulative_args (&args);
2564 /* The rules for returning in memory are the same as for passing the
2565 first named argument by reference. */
2566 memset (&args, 0, sizeof args);
2567 return riscv_pass_by_reference (cum, TYPE_MODE (type), type, true);
2570 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
2572 static void
2573 riscv_setup_incoming_varargs (cumulative_args_t cum, machine_mode mode,
2574 tree type, int *pretend_size ATTRIBUTE_UNUSED,
2575 int no_rtl)
2577 CUMULATIVE_ARGS local_cum;
2578 int gp_saved;
2580 /* The caller has advanced CUM up to, but not beyond, the last named
2581 argument. Advance a local copy of CUM past the last "real" named
2582 argument, to find out how many registers are left over. */
2583 local_cum = *get_cumulative_args (cum);
2584 riscv_function_arg_advance (pack_cumulative_args (&local_cum), mode, type, 1);
2586 /* Found out how many registers we need to save. */
2587 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
2589 if (!no_rtl && gp_saved > 0)
2591 rtx ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
2592 REG_PARM_STACK_SPACE (cfun->decl)
2593 - gp_saved * UNITS_PER_WORD);
2594 rtx mem = gen_frame_mem (BLKmode, ptr);
2595 set_mem_alias_set (mem, get_varargs_alias_set ());
2597 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
2598 mem, gp_saved);
2600 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
2601 cfun->machine->varargs_size = gp_saved * UNITS_PER_WORD;
2604 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
2606 static void
2607 riscv_va_start (tree valist, rtx nextarg)
2609 nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
2610 std_expand_builtin_va_start (valist, nextarg);
2613 /* Make ADDR suitable for use as a call or sibcall target. */
2616 riscv_legitimize_call_address (rtx addr)
2618 if (!call_insn_operand (addr, VOIDmode))
2620 rtx reg = RISCV_PROLOGUE_TEMP (Pmode);
2621 riscv_emit_move (reg, addr);
2622 return reg;
2624 return addr;
2627 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
2628 in context CONTEXT. HI_RELOC indicates a high-part reloc. */
2630 static void
2631 riscv_print_operand_reloc (FILE *file, rtx op, bool hi_reloc)
2633 const char *reloc;
2635 switch (riscv_classify_symbolic_expression (op))
2637 case SYMBOL_ABSOLUTE:
2638 reloc = hi_reloc ? "%hi" : "%lo";
2639 break;
2641 case SYMBOL_PCREL:
2642 reloc = hi_reloc ? "%pcrel_hi" : "%pcrel_lo";
2643 break;
2645 case SYMBOL_TLS_LE:
2646 reloc = hi_reloc ? "%tprel_hi" : "%tprel_lo";
2647 break;
2649 default:
2650 gcc_unreachable ();
2653 fprintf (file, "%s(", reloc);
2654 output_addr_const (file, riscv_strip_unspec_address (op));
2655 fputc (')', file);
2658 /* Return true if the .AQ suffix should be added to an AMO to implement the
2659 acquire portion of memory model MODEL. */
2661 static bool
2662 riscv_memmodel_needs_amo_acquire (enum memmodel model)
2664 switch (model)
2666 case MEMMODEL_ACQ_REL:
2667 case MEMMODEL_SEQ_CST:
2668 case MEMMODEL_SYNC_SEQ_CST:
2669 case MEMMODEL_ACQUIRE:
2670 case MEMMODEL_CONSUME:
2671 case MEMMODEL_SYNC_ACQUIRE:
2672 return true;
2674 case MEMMODEL_RELEASE:
2675 case MEMMODEL_SYNC_RELEASE:
2676 case MEMMODEL_RELAXED:
2677 return false;
2679 default:
2680 gcc_unreachable ();
2684 /* Return true if a FENCE should be emitted to before a memory access to
2685 implement the release portion of memory model MODEL. */
2687 static bool
2688 riscv_memmodel_needs_release_fence (enum memmodel model)
2690 switch (model)
2692 case MEMMODEL_ACQ_REL:
2693 case MEMMODEL_SEQ_CST:
2694 case MEMMODEL_SYNC_SEQ_CST:
2695 case MEMMODEL_RELEASE:
2696 case MEMMODEL_SYNC_RELEASE:
2697 return true;
2699 case MEMMODEL_ACQUIRE:
2700 case MEMMODEL_CONSUME:
2701 case MEMMODEL_SYNC_ACQUIRE:
2702 case MEMMODEL_RELAXED:
2703 return false;
2705 default:
2706 gcc_unreachable ();
2710 /* Implement TARGET_PRINT_OPERAND. The RISCV-specific operand codes are:
2712 'h' Print the high-part relocation associated with OP, after stripping
2713 any outermost HIGH.
2714 'R' Print the low-part relocation associated with OP.
2715 'C' Print the integer branch condition for comparison OP.
2716 'A' Print the atomic operation suffix for memory model OP.
2717 'F' Print a FENCE if the memory model requires a release.
2718 'z' Print x0 if OP is zero, otherwise print OP normally. */
2720 static void
2721 riscv_print_operand (FILE *file, rtx op, int letter)
2723 machine_mode mode = GET_MODE (op);
2724 enum rtx_code code = GET_CODE (op);
2726 switch (letter)
2728 case 'h':
2729 if (code == HIGH)
2730 op = XEXP (op, 0);
2731 riscv_print_operand_reloc (file, op, true);
2732 break;
2734 case 'R':
2735 riscv_print_operand_reloc (file, op, false);
2736 break;
2738 case 'C':
2739 /* The RTL names match the instruction names. */
2740 fputs (GET_RTX_NAME (code), file);
2741 break;
2743 case 'A':
2744 if (riscv_memmodel_needs_amo_acquire ((enum memmodel) INTVAL (op)))
2745 fputs (".aq", file);
2746 break;
2748 case 'F':
2749 if (riscv_memmodel_needs_release_fence ((enum memmodel) INTVAL (op)))
2750 fputs ("fence iorw,ow; ", file);
2751 break;
2753 default:
2754 switch (code)
2756 case REG:
2757 if (letter && letter != 'z')
2758 output_operand_lossage ("invalid use of '%%%c'", letter);
2759 fprintf (file, "%s", reg_names[REGNO (op)]);
2760 break;
2762 case MEM:
2763 if (letter && letter != 'z')
2764 output_operand_lossage ("invalid use of '%%%c'", letter);
2765 else
2766 output_address (mode, XEXP (op, 0));
2767 break;
2769 default:
2770 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
2771 fputs (reg_names[GP_REG_FIRST], file);
2772 else if (letter && letter != 'z')
2773 output_operand_lossage ("invalid use of '%%%c'", letter);
2774 else
2775 output_addr_const (file, riscv_strip_unspec_address (op));
2776 break;
2781 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
2783 static void
2784 riscv_print_operand_address (FILE *file, machine_mode mode ATTRIBUTE_UNUSED, rtx x)
2786 struct riscv_address_info addr;
2788 if (riscv_classify_address (&addr, x, word_mode, true))
2789 switch (addr.type)
2791 case ADDRESS_REG:
2792 riscv_print_operand (file, addr.offset, 0);
2793 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
2794 return;
2796 case ADDRESS_LO_SUM:
2797 riscv_print_operand_reloc (file, addr.offset, false);
2798 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
2799 return;
2801 case ADDRESS_CONST_INT:
2802 output_addr_const (file, x);
2803 fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
2804 return;
2806 case ADDRESS_SYMBOLIC:
2807 output_addr_const (file, riscv_strip_unspec_address (x));
2808 return;
2810 gcc_unreachable ();
2813 static bool
2814 riscv_size_ok_for_small_data_p (int size)
2816 return g_switch_value && IN_RANGE (size, 1, g_switch_value);
2819 /* Return true if EXP should be placed in the small data section. */
2821 static bool
2822 riscv_in_small_data_p (const_tree x)
2824 if (TREE_CODE (x) == STRING_CST || TREE_CODE (x) == FUNCTION_DECL)
2825 return false;
2827 if (TREE_CODE (x) == VAR_DECL && DECL_SECTION_NAME (x))
2829 const char *sec = DECL_SECTION_NAME (x);
2830 return strcmp (sec, ".sdata") == 0 || strcmp (sec, ".sbss") == 0;
2833 return riscv_size_ok_for_small_data_p (int_size_in_bytes (TREE_TYPE (x)));
2836 /* Return a section for X, handling small data. */
2838 static section *
2839 riscv_elf_select_rtx_section (machine_mode mode, rtx x,
2840 unsigned HOST_WIDE_INT align)
2842 section *s = default_elf_select_rtx_section (mode, x, align);
2844 if (riscv_size_ok_for_small_data_p (GET_MODE_SIZE (mode)))
2846 if (strncmp (s->named.name, ".rodata.cst", strlen (".rodata.cst")) == 0)
2848 /* Rename .rodata.cst* to .srodata.cst*. */
2849 char *name = (char *) alloca (strlen (s->named.name) + 2);
2850 sprintf (name, ".s%s", s->named.name + 1);
2851 return get_section (name, s->named.common.flags, NULL);
2854 if (s == data_section)
2855 return sdata_section;
2858 return s;
2861 /* Make the last instruction frame-related and note that it performs
2862 the operation described by FRAME_PATTERN. */
2864 static void
2865 riscv_set_frame_expr (rtx frame_pattern)
2867 rtx insn;
2869 insn = get_last_insn ();
2870 RTX_FRAME_RELATED_P (insn) = 1;
2871 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
2872 frame_pattern,
2873 REG_NOTES (insn));
2876 /* Return a frame-related rtx that stores REG at MEM.
2877 REG must be a single register. */
2879 static rtx
2880 riscv_frame_set (rtx mem, rtx reg)
2882 rtx set = gen_rtx_SET (mem, reg);
2883 RTX_FRAME_RELATED_P (set) = 1;
2884 return set;
2887 /* Return true if the current function must save register REGNO. */
2889 static bool
2890 riscv_save_reg_p (unsigned int regno)
2892 bool call_saved = !global_regs[regno] && !call_used_regs[regno];
2893 bool might_clobber = crtl->saves_all_registers
2894 || df_regs_ever_live_p (regno);
2896 if (call_saved && might_clobber)
2897 return true;
2899 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
2900 return true;
2902 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
2903 return true;
2905 return false;
2908 /* Determine whether to call GPR save/restore routines. */
2909 static bool
2910 riscv_use_save_libcall (const struct riscv_frame_info *frame)
2912 if (!TARGET_SAVE_RESTORE || crtl->calls_eh_return || frame_pointer_needed)
2913 return false;
2915 return frame->save_libcall_adjustment != 0;
2918 /* Determine which GPR save/restore routine to call. */
2920 static unsigned
2921 riscv_save_libcall_count (unsigned mask)
2923 for (unsigned n = GP_REG_LAST; n > GP_REG_FIRST; n--)
2924 if (BITSET_P (mask, n))
2925 return CALLEE_SAVED_REG_NUMBER (n) + 1;
2926 abort ();
2929 /* Populate the current function's riscv_frame_info structure.
2931 RISC-V stack frames grown downward. High addresses are at the top.
2933 +-------------------------------+
2935 | incoming stack arguments |
2937 +-------------------------------+ <-- incoming stack pointer
2939 | callee-allocated save area |
2940 | for arguments that are |
2941 | split between registers and |
2942 | the stack |
2944 +-------------------------------+ <-- arg_pointer_rtx
2946 | callee-allocated save area |
2947 | for register varargs |
2949 +-------------------------------+ <-- hard_frame_pointer_rtx;
2950 | | stack_pointer_rtx + gp_sp_offset
2951 | GPR save area | + UNITS_PER_WORD
2953 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
2954 | | + UNITS_PER_HWVALUE
2955 | FPR save area |
2957 +-------------------------------+ <-- frame_pointer_rtx (virtual)
2959 | local variables |
2961 P +-------------------------------+
2963 | outgoing stack arguments |
2965 +-------------------------------+ <-- stack_pointer_rtx
2967 Dynamic stack allocations such as alloca insert data at point P.
2968 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
2969 hard_frame_pointer_rtx unchanged. */
2971 static void
2972 riscv_compute_frame_info (void)
2974 struct riscv_frame_info *frame;
2975 HOST_WIDE_INT offset;
2976 unsigned int regno, i, num_x_saved = 0, num_f_saved = 0;
2978 frame = &cfun->machine->frame;
2979 memset (frame, 0, sizeof (*frame));
2981 /* Find out which GPRs we need to save. */
2982 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
2983 if (riscv_save_reg_p (regno))
2984 frame->mask |= 1 << (regno - GP_REG_FIRST), num_x_saved++;
2986 /* If this function calls eh_return, we must also save and restore the
2987 EH data registers. */
2988 if (crtl->calls_eh_return)
2989 for (i = 0; (regno = EH_RETURN_DATA_REGNO (i)) != INVALID_REGNUM; i++)
2990 frame->mask |= 1 << (regno - GP_REG_FIRST), num_x_saved++;
2992 /* Find out which FPRs we need to save. This loop must iterate over
2993 the same space as its companion in riscv_for_each_saved_reg. */
2994 if (TARGET_HARD_FLOAT)
2995 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
2996 if (riscv_save_reg_p (regno))
2997 frame->fmask |= 1 << (regno - FP_REG_FIRST), num_f_saved++;
2999 /* At the bottom of the frame are any outgoing stack arguments. */
3000 offset = crtl->outgoing_args_size;
3001 /* Next are local stack variables. */
3002 offset += RISCV_STACK_ALIGN (get_frame_size ());
3003 /* The virtual frame pointer points above the local variables. */
3004 frame->frame_pointer_offset = offset;
3005 /* Next are the callee-saved FPRs. */
3006 if (frame->fmask)
3007 offset += RISCV_STACK_ALIGN (num_f_saved * UNITS_PER_FP_REG);
3008 frame->fp_sp_offset = offset - UNITS_PER_FP_REG;
3009 /* Next are the callee-saved GPRs. */
3010 if (frame->mask)
3012 unsigned x_save_size = RISCV_STACK_ALIGN (num_x_saved * UNITS_PER_WORD);
3013 unsigned num_save_restore = 1 + riscv_save_libcall_count (frame->mask);
3015 /* Only use save/restore routines if they don't alter the stack size. */
3016 if (RISCV_STACK_ALIGN (num_save_restore * UNITS_PER_WORD) == x_save_size)
3017 frame->save_libcall_adjustment = x_save_size;
3019 offset += x_save_size;
3021 frame->gp_sp_offset = offset - UNITS_PER_WORD;
3022 /* The hard frame pointer points above the callee-saved GPRs. */
3023 frame->hard_frame_pointer_offset = offset;
3024 /* Above the hard frame pointer is the callee-allocated varags save area. */
3025 offset += RISCV_STACK_ALIGN (cfun->machine->varargs_size);
3026 frame->arg_pointer_offset = offset;
3027 /* Next is the callee-allocated area for pretend stack arguments. */
3028 offset += crtl->args.pretend_args_size;
3029 frame->total_size = offset;
3030 /* Next points the incoming stack pointer and any incoming arguments. */
3032 /* Only use save/restore routines when the GPRs are atop the frame. */
3033 if (frame->hard_frame_pointer_offset != frame->total_size)
3034 frame->save_libcall_adjustment = 0;
3037 /* Make sure that we're not trying to eliminate to the wrong hard frame
3038 pointer. */
3040 static bool
3041 riscv_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
3043 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
3046 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
3047 or argument pointer. TO is either the stack pointer or hard frame
3048 pointer. */
3050 HOST_WIDE_INT
3051 riscv_initial_elimination_offset (int from, int to)
3053 HOST_WIDE_INT src, dest;
3055 riscv_compute_frame_info ();
3057 if (to == HARD_FRAME_POINTER_REGNUM)
3058 dest = cfun->machine->frame.hard_frame_pointer_offset;
3059 else if (to == STACK_POINTER_REGNUM)
3060 dest = 0; /* The stack pointer is the base of all offsets, hence 0. */
3061 else
3062 gcc_unreachable ();
3064 if (from == FRAME_POINTER_REGNUM)
3065 src = cfun->machine->frame.frame_pointer_offset;
3066 else if (from == ARG_POINTER_REGNUM)
3067 src = cfun->machine->frame.arg_pointer_offset;
3068 else
3069 gcc_unreachable ();
3071 return src - dest;
3074 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
3075 previous frame. */
3078 riscv_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
3080 if (count != 0)
3081 return const0_rtx;
3083 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
3086 /* Emit code to change the current function's return address to
3087 ADDRESS. SCRATCH is available as a scratch register, if needed.
3088 ADDRESS and SCRATCH are both word-mode GPRs. */
3090 void
3091 riscv_set_return_address (rtx address, rtx scratch)
3093 rtx slot_address;
3095 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
3096 slot_address = riscv_add_offset (scratch, stack_pointer_rtx,
3097 cfun->machine->frame.gp_sp_offset);
3098 riscv_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
3101 /* A function to save or store a register. The first argument is the
3102 register and the second is the stack slot. */
3103 typedef void (*riscv_save_restore_fn) (rtx, rtx);
3105 /* Use FN to save or restore register REGNO. MODE is the register's
3106 mode and OFFSET is the offset of its save slot from the current
3107 stack pointer. */
3109 static void
3110 riscv_save_restore_reg (machine_mode mode, int regno,
3111 HOST_WIDE_INT offset, riscv_save_restore_fn fn)
3113 rtx mem;
3115 mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx, offset));
3116 fn (gen_rtx_REG (mode, regno), mem);
3119 /* Call FN for each register that is saved by the current function.
3120 SP_OFFSET is the offset of the current stack pointer from the start
3121 of the frame. */
3123 static void
3124 riscv_for_each_saved_reg (HOST_WIDE_INT sp_offset, riscv_save_restore_fn fn)
3126 HOST_WIDE_INT offset;
3128 /* Save the link register and s-registers. */
3129 offset = cfun->machine->frame.gp_sp_offset - sp_offset;
3130 for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST-1; regno++)
3131 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
3133 riscv_save_restore_reg (word_mode, regno, offset, fn);
3134 offset -= UNITS_PER_WORD;
3137 /* This loop must iterate over the same space as its companion in
3138 riscv_compute_frame_info. */
3139 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
3140 for (int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
3141 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
3143 machine_mode mode = TARGET_DOUBLE_FLOAT ? DFmode : SFmode;
3145 riscv_save_restore_reg (mode, regno, offset, fn);
3146 offset -= GET_MODE_SIZE (mode);
3150 /* Save register REG to MEM. Make the instruction frame-related. */
3152 static void
3153 riscv_save_reg (rtx reg, rtx mem)
3155 riscv_emit_move (mem, reg);
3156 riscv_set_frame_expr (riscv_frame_set (mem, reg));
3159 /* Restore register REG from MEM. */
3161 static void
3162 riscv_restore_reg (rtx reg, rtx mem)
3164 rtx insn = riscv_emit_move (reg, mem);
3165 rtx dwarf = NULL_RTX;
3166 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
3167 REG_NOTES (insn) = dwarf;
3169 RTX_FRAME_RELATED_P (insn) = 1;
3172 /* Return the code to invoke the GPR save routine. */
3174 const char *
3175 riscv_output_gpr_save (unsigned mask)
3177 static char s[32];
3178 unsigned n = riscv_save_libcall_count (mask);
3180 ssize_t bytes = snprintf (s, sizeof (s), "call\tt0,__riscv_save_%u", n);
3181 gcc_assert ((size_t) bytes < sizeof (s));
3183 return s;
3186 /* For stack frames that can't be allocated with a single ADDI instruction,
3187 compute the best value to initially allocate. It must at a minimum
3188 allocate enough space to spill the callee-saved registers. */
3190 static HOST_WIDE_INT
3191 riscv_first_stack_step (struct riscv_frame_info *frame)
3193 HOST_WIDE_INT min_first_step = frame->total_size - frame->fp_sp_offset;
3194 HOST_WIDE_INT max_first_step = IMM_REACH / 2 - STACK_BOUNDARY / 8;
3196 if (SMALL_OPERAND (frame->total_size))
3197 return frame->total_size;
3199 /* As an optimization, use the least-significant bits of the total frame
3200 size, so that the second adjustment step is just LUI + ADD. */
3201 if (!SMALL_OPERAND (frame->total_size - max_first_step)
3202 && frame->total_size % IMM_REACH < IMM_REACH / 2
3203 && frame->total_size % IMM_REACH >= min_first_step)
3204 return frame->total_size % IMM_REACH;
3206 gcc_assert (min_first_step <= max_first_step);
3207 return max_first_step;
3210 static rtx
3211 riscv_adjust_libcall_cfi_prologue ()
3213 rtx dwarf = NULL_RTX;
3214 rtx adjust_sp_rtx, reg, mem, insn;
3215 int saved_size = cfun->machine->frame.save_libcall_adjustment;
3216 int offset;
3218 for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST-1; regno++)
3219 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
3221 /* The save order is ra, s0, s1, s2 to s11. */
3222 if (regno == RETURN_ADDR_REGNUM)
3223 offset = saved_size - UNITS_PER_WORD;
3224 else if (regno == S0_REGNUM)
3225 offset = saved_size - UNITS_PER_WORD * 2;
3226 else if (regno == S1_REGNUM)
3227 offset = saved_size - UNITS_PER_WORD * 3;
3228 else
3229 offset = saved_size - ((regno - S2_REGNUM + 4) * UNITS_PER_WORD);
3231 reg = gen_rtx_REG (SImode, regno);
3232 mem = gen_frame_mem (SImode, plus_constant (Pmode,
3233 stack_pointer_rtx,
3234 offset));
3236 insn = gen_rtx_SET (mem, reg);
3237 dwarf = alloc_reg_note (REG_CFA_OFFSET, insn, dwarf);
3240 /* Debug info for adjust sp. */
3241 adjust_sp_rtx = gen_add3_insn (stack_pointer_rtx,
3242 stack_pointer_rtx, GEN_INT (-saved_size));
3243 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, adjust_sp_rtx,
3244 dwarf);
3245 return dwarf;
3248 static void
3249 riscv_emit_stack_tie (void)
3251 if (Pmode == SImode)
3252 emit_insn (gen_stack_tiesi (stack_pointer_rtx, hard_frame_pointer_rtx));
3253 else
3254 emit_insn (gen_stack_tiedi (stack_pointer_rtx, hard_frame_pointer_rtx));
3257 /* Expand the "prologue" pattern. */
3259 void
3260 riscv_expand_prologue (void)
3262 struct riscv_frame_info *frame = &cfun->machine->frame;
3263 HOST_WIDE_INT size = frame->total_size;
3264 unsigned mask = frame->mask;
3265 rtx insn;
3267 if (flag_stack_usage_info)
3268 current_function_static_stack_size = size;
3270 /* When optimizing for size, call a subroutine to save the registers. */
3271 if (riscv_use_save_libcall (frame))
3273 rtx dwarf = NULL_RTX;
3274 dwarf = riscv_adjust_libcall_cfi_prologue ();
3276 frame->mask = 0; /* Temporarily fib that we need not save GPRs. */
3277 size -= frame->save_libcall_adjustment;
3278 insn = emit_insn (gen_gpr_save (GEN_INT (mask)));
3280 RTX_FRAME_RELATED_P (insn) = 1;
3281 REG_NOTES (insn) = dwarf;
3284 /* Save the registers. */
3285 if ((frame->mask | frame->fmask) != 0)
3287 HOST_WIDE_INT step1 = MIN (size, riscv_first_stack_step (frame));
3289 insn = gen_add3_insn (stack_pointer_rtx,
3290 stack_pointer_rtx,
3291 GEN_INT (-step1));
3292 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
3293 size -= step1;
3294 riscv_for_each_saved_reg (size, riscv_save_reg);
3297 frame->mask = mask; /* Undo the above fib. */
3299 /* Set up the frame pointer, if we're using one. */
3300 if (frame_pointer_needed)
3302 insn = gen_add3_insn (hard_frame_pointer_rtx, stack_pointer_rtx,
3303 GEN_INT (frame->hard_frame_pointer_offset - size));
3304 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
3306 riscv_emit_stack_tie ();
3309 /* Allocate the rest of the frame. */
3310 if (size > 0)
3312 if (SMALL_OPERAND (-size))
3314 insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
3315 GEN_INT (-size));
3316 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
3318 else
3320 riscv_emit_move (RISCV_PROLOGUE_TEMP (Pmode), GEN_INT (-size));
3321 emit_insn (gen_add3_insn (stack_pointer_rtx,
3322 stack_pointer_rtx,
3323 RISCV_PROLOGUE_TEMP (Pmode)));
3325 /* Describe the effect of the previous instructions. */
3326 insn = plus_constant (Pmode, stack_pointer_rtx, -size);
3327 insn = gen_rtx_SET (stack_pointer_rtx, insn);
3328 riscv_set_frame_expr (insn);
3333 static rtx
3334 riscv_adjust_libcall_cfi_epilogue ()
3336 rtx dwarf = NULL_RTX;
3337 rtx adjust_sp_rtx, reg;
3338 int saved_size = cfun->machine->frame.save_libcall_adjustment;
3340 /* Debug info for adjust sp. */
3341 adjust_sp_rtx = gen_add3_insn (stack_pointer_rtx,
3342 stack_pointer_rtx, GEN_INT (saved_size));
3343 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, adjust_sp_rtx,
3344 dwarf);
3346 for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST-1; regno++)
3347 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
3349 reg = gen_rtx_REG (SImode, regno);
3350 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
3353 return dwarf;
3356 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
3357 says which. */
3359 void
3360 riscv_expand_epilogue (bool sibcall_p)
3362 /* Split the frame into two. STEP1 is the amount of stack we should
3363 deallocate before restoring the registers. STEP2 is the amount we
3364 should deallocate afterwards.
3366 Start off by assuming that no registers need to be restored. */
3367 struct riscv_frame_info *frame = &cfun->machine->frame;
3368 unsigned mask = frame->mask;
3369 HOST_WIDE_INT step1 = frame->total_size;
3370 HOST_WIDE_INT step2 = 0;
3371 bool use_restore_libcall = !sibcall_p && riscv_use_save_libcall (frame);
3372 rtx ra = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
3373 rtx insn;
3375 /* We need to add memory barrier to prevent read from deallocated stack. */
3376 bool need_barrier_p = (get_frame_size ()
3377 + cfun->machine->frame.arg_pointer_offset) != 0;
3379 if (!sibcall_p && riscv_can_use_return_insn ())
3381 emit_jump_insn (gen_return ());
3382 return;
3385 /* Move past any dynamic stack allocations. */
3386 if (cfun->calls_alloca)
3388 /* Emit a barrier to prevent loads from a deallocated stack. */
3389 riscv_emit_stack_tie ();
3390 need_barrier_p = false;
3392 rtx adjust = GEN_INT (-frame->hard_frame_pointer_offset);
3393 if (!SMALL_OPERAND (INTVAL (adjust)))
3395 riscv_emit_move (RISCV_PROLOGUE_TEMP (Pmode), adjust);
3396 adjust = RISCV_PROLOGUE_TEMP (Pmode);
3399 insn = emit_insn (
3400 gen_add3_insn (stack_pointer_rtx, hard_frame_pointer_rtx,
3401 adjust));
3403 rtx dwarf = NULL_RTX;
3404 rtx cfa_adjust_value = gen_rtx_PLUS (
3405 Pmode, hard_frame_pointer_rtx,
3406 GEN_INT (-frame->hard_frame_pointer_offset));
3407 rtx cfa_adjust_rtx = gen_rtx_SET (stack_pointer_rtx, cfa_adjust_value);
3408 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, cfa_adjust_rtx, dwarf);
3409 RTX_FRAME_RELATED_P (insn) = 1;
3411 REG_NOTES (insn) = dwarf;
3414 /* If we need to restore registers, deallocate as much stack as
3415 possible in the second step without going out of range. */
3416 if ((frame->mask | frame->fmask) != 0)
3418 step2 = riscv_first_stack_step (frame);
3419 step1 -= step2;
3422 /* Set TARGET to BASE + STEP1. */
3423 if (step1 > 0)
3425 /* Emit a barrier to prevent loads from a deallocated stack. */
3426 riscv_emit_stack_tie ();
3427 need_barrier_p = false;
3429 /* Get an rtx for STEP1 that we can add to BASE. */
3430 rtx adjust = GEN_INT (step1);
3431 if (!SMALL_OPERAND (step1))
3433 riscv_emit_move (RISCV_PROLOGUE_TEMP (Pmode), adjust);
3434 adjust = RISCV_PROLOGUE_TEMP (Pmode);
3437 insn = emit_insn (
3438 gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx, adjust));
3440 rtx dwarf = NULL_RTX;
3441 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3442 GEN_INT (step2));
3444 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
3445 RTX_FRAME_RELATED_P (insn) = 1;
3447 REG_NOTES (insn) = dwarf;
3450 if (use_restore_libcall)
3451 frame->mask = 0; /* Temporarily fib that we need not save GPRs. */
3453 /* Restore the registers. */
3454 riscv_for_each_saved_reg (frame->total_size - step2, riscv_restore_reg);
3456 if (use_restore_libcall)
3458 frame->mask = mask; /* Undo the above fib. */
3459 gcc_assert (step2 >= frame->save_libcall_adjustment);
3460 step2 -= frame->save_libcall_adjustment;
3463 if (need_barrier_p)
3464 riscv_emit_stack_tie ();
3466 /* Deallocate the final bit of the frame. */
3467 if (step2 > 0)
3469 insn = emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
3470 GEN_INT (step2)));
3472 rtx dwarf = NULL_RTX;
3473 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3474 const0_rtx);
3475 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
3476 RTX_FRAME_RELATED_P (insn) = 1;
3478 REG_NOTES (insn) = dwarf;
3481 if (use_restore_libcall)
3483 rtx dwarf = riscv_adjust_libcall_cfi_epilogue ();
3484 insn = emit_insn (gen_gpr_restore (GEN_INT (riscv_save_libcall_count (mask))));
3485 RTX_FRAME_RELATED_P (insn) = 1;
3486 REG_NOTES (insn) = dwarf;
3488 emit_jump_insn (gen_gpr_restore_return (ra));
3489 return;
3492 /* Add in the __builtin_eh_return stack adjustment. */
3493 if (crtl->calls_eh_return)
3494 emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
3495 EH_RETURN_STACKADJ_RTX));
3497 if (!sibcall_p)
3498 emit_jump_insn (gen_simple_return_internal (ra));
3501 /* Return nonzero if this function is known to have a null epilogue.
3502 This allows the optimizer to omit jumps to jumps if no stack
3503 was created. */
3505 bool
3506 riscv_can_use_return_insn (void)
3508 return reload_completed && cfun->machine->frame.total_size == 0;
3511 /* Implement TARGET_REGISTER_MOVE_COST. */
3513 static int
3514 riscv_register_move_cost (machine_mode mode,
3515 reg_class_t from, reg_class_t to)
3517 return SECONDARY_MEMORY_NEEDED (from, to, mode) ? 8 : 2;
3520 /* Return true if register REGNO can store a value of mode MODE. */
3522 bool
3523 riscv_hard_regno_mode_ok_p (unsigned int regno, machine_mode mode)
3525 unsigned int nregs = riscv_hard_regno_nregs (regno, mode);
3527 if (GP_REG_P (regno))
3529 if (!GP_REG_P (regno + nregs - 1))
3530 return false;
3532 else if (FP_REG_P (regno))
3534 if (!FP_REG_P (regno + nregs - 1))
3535 return false;
3537 if (GET_MODE_CLASS (mode) != MODE_FLOAT
3538 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
3539 return false;
3541 /* Only use callee-saved registers if a potential callee is guaranteed
3542 to spill the requisite width. */
3543 if (GET_MODE_UNIT_SIZE (mode) > UNITS_PER_FP_REG
3544 || (!call_used_regs[regno]
3545 && GET_MODE_UNIT_SIZE (mode) > UNITS_PER_FP_ARG))
3546 return false;
3548 else
3549 return false;
3551 /* Require same callee-savedness for all registers. */
3552 for (unsigned i = 1; i < nregs; i++)
3553 if (call_used_regs[regno] != call_used_regs[regno + i])
3554 return false;
3556 return true;
3559 /* Implement HARD_REGNO_NREGS. */
3561 unsigned int
3562 riscv_hard_regno_nregs (int regno, machine_mode mode)
3564 if (FP_REG_P (regno))
3565 return (GET_MODE_SIZE (mode) + UNITS_PER_FP_REG - 1) / UNITS_PER_FP_REG;
3567 /* All other registers are word-sized. */
3568 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3571 /* Implement CLASS_MAX_NREGS. */
3573 static unsigned char
3574 riscv_class_max_nregs (reg_class_t rclass, machine_mode mode)
3576 if (reg_class_subset_p (FP_REGS, rclass))
3577 return riscv_hard_regno_nregs (FP_REG_FIRST, mode);
3579 if (reg_class_subset_p (GR_REGS, rclass))
3580 return riscv_hard_regno_nregs (GP_REG_FIRST, mode);
3582 return 0;
3585 /* Implement TARGET_MEMORY_MOVE_COST. */
3587 static int
3588 riscv_memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
3590 return (tune_info->memory_cost
3591 + memory_move_secondary_cost (mode, rclass, in));
3594 /* Return the number of instructions that can be issued per cycle. */
3596 static int
3597 riscv_issue_rate (void)
3599 return tune_info->issue_rate;
3602 /* Implement TARGET_ASM_FILE_START. */
3604 static void
3605 riscv_file_start (void)
3607 default_file_start ();
3609 /* Instruct GAS to generate position-[in]dependent code. */
3610 fprintf (asm_out_file, "\t.option %spic\n", (flag_pic ? "" : "no"));
3613 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
3614 in order to avoid duplicating too much logic from elsewhere. */
3616 static void
3617 riscv_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
3618 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
3619 tree function)
3621 rtx this_rtx, temp1, temp2, fnaddr;
3622 rtx_insn *insn;
3624 /* Pretend to be a post-reload pass while generating rtl. */
3625 reload_completed = 1;
3627 /* Mark the end of the (empty) prologue. */
3628 emit_note (NOTE_INSN_PROLOGUE_END);
3630 /* Determine if we can use a sibcall to call FUNCTION directly. */
3631 fnaddr = gen_rtx_MEM (FUNCTION_MODE, XEXP (DECL_RTL (function), 0));
3633 /* We need two temporary registers in some cases. */
3634 temp1 = gen_rtx_REG (Pmode, RISCV_PROLOGUE_TEMP_REGNUM);
3635 temp2 = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
3637 /* Find out which register contains the "this" pointer. */
3638 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
3639 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
3640 else
3641 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3643 /* Add DELTA to THIS_RTX. */
3644 if (delta != 0)
3646 rtx offset = GEN_INT (delta);
3647 if (!SMALL_OPERAND (delta))
3649 riscv_emit_move (temp1, offset);
3650 offset = temp1;
3652 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
3655 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
3656 if (vcall_offset != 0)
3658 rtx addr;
3660 /* Set TEMP1 to *THIS_RTX. */
3661 riscv_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
3663 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
3664 addr = riscv_add_offset (temp2, temp1, vcall_offset);
3666 /* Load the offset and add it to THIS_RTX. */
3667 riscv_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
3668 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
3671 /* Jump to the target function. */
3672 insn = emit_call_insn (gen_sibcall (fnaddr, const0_rtx, NULL, const0_rtx));
3673 SIBLING_CALL_P (insn) = 1;
3675 /* Run just enough of rest_of_compilation. This sequence was
3676 "borrowed" from alpha.c. */
3677 insn = get_insns ();
3678 split_all_insns_noflow ();
3679 shorten_branches (insn);
3680 final_start_function (insn, file, 1);
3681 final (insn, file, 1);
3682 final_end_function ();
3684 /* Clean up the vars set above. Note that final_end_function resets
3685 the global pointer for us. */
3686 reload_completed = 0;
3689 /* Allocate a chunk of memory for per-function machine-dependent data. */
3691 static struct machine_function *
3692 riscv_init_machine_status (void)
3694 return ggc_cleared_alloc<machine_function> ();
3697 /* Implement TARGET_OPTION_OVERRIDE. */
3699 static void
3700 riscv_option_override (void)
3702 const struct riscv_cpu_info *cpu;
3704 #ifdef SUBTARGET_OVERRIDE_OPTIONS
3705 SUBTARGET_OVERRIDE_OPTIONS;
3706 #endif
3708 flag_pcc_struct_return = 0;
3710 if (flag_pic)
3711 g_switch_value = 0;
3713 /* The presence of the M extension implies that division instructions
3714 are present, so include them unless explicitly disabled. */
3715 if (TARGET_MUL && (target_flags_explicit & MASK_DIV) == 0)
3716 target_flags |= MASK_DIV;
3717 else if (!TARGET_MUL && TARGET_DIV)
3718 error ("-mdiv requires -march to subsume the %<M%> extension");
3720 /* Likewise floating-point division and square root. */
3721 if (TARGET_HARD_FLOAT && (target_flags_explicit & MASK_FDIV) == 0)
3722 target_flags |= MASK_FDIV;
3724 /* Handle -mtune. */
3725 cpu = riscv_parse_cpu (riscv_tune_string ? riscv_tune_string :
3726 RISCV_TUNE_STRING_DEFAULT);
3727 tune_info = optimize_size ? &optimize_size_tune_info : cpu->tune_info;
3729 /* Use -mtune's setting for slow_unaligned_access, even when optimizing
3730 for size. For architectures that trap and emulate unaligned accesses,
3731 the performance cost is too great, even for -Os. */
3732 riscv_slow_unaligned_access = (cpu->tune_info->slow_unaligned_access
3733 || TARGET_STRICT_ALIGN);
3735 /* If the user hasn't specified a branch cost, use the processor's
3736 default. */
3737 if (riscv_branch_cost == 0)
3738 riscv_branch_cost = tune_info->branch_cost;
3740 /* Function to allocate machine-dependent function status. */
3741 init_machine_status = &riscv_init_machine_status;
3743 if (flag_pic)
3744 riscv_cmodel = CM_PIC;
3746 /* We get better code with explicit relocs for CM_MEDLOW, but
3747 worse code for the others (for now). Pick the best default. */
3748 if ((target_flags_explicit & MASK_EXPLICIT_RELOCS) == 0)
3749 if (riscv_cmodel == CM_MEDLOW)
3750 target_flags |= MASK_EXPLICIT_RELOCS;
3752 /* Require that the ISA supports the requested floating-point ABI. */
3753 if (UNITS_PER_FP_ARG > (TARGET_HARD_FLOAT ? UNITS_PER_FP_REG : 0))
3754 error ("requested ABI requires -march to subsume the %qc extension",
3755 UNITS_PER_FP_ARG > 8 ? 'Q' : (UNITS_PER_FP_ARG > 4 ? 'D' : 'F'));
3757 /* We do not yet support ILP32 on RV64. */
3758 if (BITS_PER_WORD != POINTER_SIZE)
3759 error ("ABI requires -march=rv%d", POINTER_SIZE);
3762 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
3764 static void
3765 riscv_conditional_register_usage (void)
3767 if (!TARGET_HARD_FLOAT)
3769 for (int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
3770 fixed_regs[regno] = call_used_regs[regno] = 1;
3774 /* Return a register priority for hard reg REGNO. */
3776 static int
3777 riscv_register_priority (int regno)
3779 /* Favor x8-x15/f8-f15 to improve the odds of RVC instruction selection. */
3780 if (TARGET_RVC && (IN_RANGE (regno, GP_REG_FIRST + 8, GP_REG_FIRST + 15)
3781 || IN_RANGE (regno, FP_REG_FIRST + 8, FP_REG_FIRST + 15)))
3782 return 1;
3784 return 0;
3787 /* Implement TARGET_TRAMPOLINE_INIT. */
3789 static void
3790 riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
3792 rtx addr, end_addr, mem;
3793 uint32_t trampoline[4];
3794 unsigned int i;
3795 HOST_WIDE_INT static_chain_offset, target_function_offset;
3797 /* Work out the offsets of the pointers from the start of the
3798 trampoline code. */
3799 gcc_assert (ARRAY_SIZE (trampoline) * 4 == TRAMPOLINE_CODE_SIZE);
3801 /* Get pointers to the beginning and end of the code block. */
3802 addr = force_reg (Pmode, XEXP (m_tramp, 0));
3803 end_addr = riscv_force_binary (Pmode, PLUS, addr,
3804 GEN_INT (TRAMPOLINE_CODE_SIZE));
3807 if (Pmode == SImode)
3809 chain_value = force_reg (Pmode, chain_value);
3811 rtx target_function = force_reg (Pmode, XEXP (DECL_RTL (fndecl), 0));
3812 /* lui t2, hi(chain)
3813 lui t1, hi(func)
3814 addi t2, t2, lo(chain)
3815 jr r1, lo(func)
3817 unsigned HOST_WIDE_INT lui_hi_chain_code, lui_hi_func_code;
3818 unsigned HOST_WIDE_INT lo_chain_code, lo_func_code;
3820 rtx uimm_mask = force_reg (SImode, gen_int_mode (-IMM_REACH, SImode));
3822 /* 0xfff. */
3823 rtx imm12_mask = gen_reg_rtx (SImode);
3824 emit_insn (gen_one_cmplsi2 (imm12_mask, uimm_mask));
3826 rtx fixup_value = force_reg (SImode, gen_int_mode (IMM_REACH/2, SImode));
3828 /* Gen lui t2, hi(chain). */
3829 rtx hi_chain = riscv_force_binary (SImode, PLUS, chain_value,
3830 fixup_value);
3831 hi_chain = riscv_force_binary (SImode, AND, hi_chain,
3832 uimm_mask);
3833 lui_hi_chain_code = OPCODE_LUI | (STATIC_CHAIN_REGNUM << SHIFT_RD);
3834 rtx lui_hi_chain = riscv_force_binary (SImode, IOR, hi_chain,
3835 gen_int_mode (lui_hi_chain_code, SImode));
3837 mem = adjust_address (m_tramp, SImode, 0);
3838 riscv_emit_move (mem, lui_hi_chain);
3840 /* Gen lui t1, hi(func). */
3841 rtx hi_func = riscv_force_binary (SImode, PLUS, target_function,
3842 fixup_value);
3843 hi_func = riscv_force_binary (SImode, AND, hi_func,
3844 uimm_mask);
3845 lui_hi_func_code = OPCODE_LUI | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RD);
3846 rtx lui_hi_func = riscv_force_binary (SImode, IOR, hi_func,
3847 gen_int_mode (lui_hi_func_code, SImode));
3849 mem = adjust_address (m_tramp, SImode, 1 * GET_MODE_SIZE (SImode));
3850 riscv_emit_move (mem, lui_hi_func);
3852 /* Gen addi t2, t2, lo(chain). */
3853 rtx lo_chain = riscv_force_binary (SImode, AND, chain_value,
3854 imm12_mask);
3855 lo_chain = riscv_force_binary (SImode, ASHIFT, lo_chain, GEN_INT (20));
3857 lo_chain_code = OPCODE_ADDI
3858 | (STATIC_CHAIN_REGNUM << SHIFT_RD)
3859 | (STATIC_CHAIN_REGNUM << SHIFT_RS1);
3861 rtx addi_lo_chain = riscv_force_binary (SImode, IOR, lo_chain,
3862 force_reg (SImode, GEN_INT (lo_chain_code)));
3864 mem = adjust_address (m_tramp, SImode, 2 * GET_MODE_SIZE (SImode));
3865 riscv_emit_move (mem, addi_lo_chain);
3867 /* Gen jr r1, lo(func). */
3868 rtx lo_func = riscv_force_binary (SImode, AND, target_function,
3869 imm12_mask);
3870 lo_func = riscv_force_binary (SImode, ASHIFT, lo_func, GEN_INT (20));
3872 lo_func_code = OPCODE_JALR | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RS1);
3874 rtx jr_lo_func = riscv_force_binary (SImode, IOR, lo_func,
3875 force_reg (SImode, GEN_INT (lo_func_code)));
3877 mem = adjust_address (m_tramp, SImode, 3 * GET_MODE_SIZE (SImode));
3878 riscv_emit_move (mem, jr_lo_func);
3880 else
3882 static_chain_offset = TRAMPOLINE_CODE_SIZE;
3883 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
3885 /* auipc t2, 0
3886 l[wd] t1, target_function_offset(t2)
3887 l[wd] t2, static_chain_offset(t2)
3888 jr t1
3890 trampoline[0] = OPCODE_AUIPC | (STATIC_CHAIN_REGNUM << SHIFT_RD);
3891 trampoline[1] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW)
3892 | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RD)
3893 | (STATIC_CHAIN_REGNUM << SHIFT_RS1)
3894 | (target_function_offset << SHIFT_IMM);
3895 trampoline[2] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW)
3896 | (STATIC_CHAIN_REGNUM << SHIFT_RD)
3897 | (STATIC_CHAIN_REGNUM << SHIFT_RS1)
3898 | (static_chain_offset << SHIFT_IMM);
3899 trampoline[3] = OPCODE_JALR | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RS1);
3901 /* Copy the trampoline code. */
3902 for (i = 0; i < ARRAY_SIZE (trampoline); i++)
3904 mem = adjust_address (m_tramp, SImode, i * GET_MODE_SIZE (SImode));
3905 riscv_emit_move (mem, gen_int_mode (trampoline[i], SImode));
3908 /* Set up the static chain pointer field. */
3909 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
3910 riscv_emit_move (mem, chain_value);
3912 /* Set up the target function field. */
3913 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
3914 riscv_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
3917 /* Flush the code part of the trampoline. */
3918 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
3919 emit_insn (gen_clear_cache (addr, end_addr));
3922 /* Return leaf_function_p () and memoize the result. */
3924 static bool
3925 riscv_leaf_function_p (void)
3927 if (cfun->machine->is_leaf == 0)
3928 cfun->machine->is_leaf = leaf_function_p () ? 1 : -1;
3930 return cfun->machine->is_leaf > 0;
3933 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
3935 static bool
3936 riscv_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
3937 tree exp ATTRIBUTE_UNUSED)
3939 /* When optimzing for size, don't use sibcalls in non-leaf routines */
3940 if (TARGET_SAVE_RESTORE)
3941 return riscv_leaf_function_p ();
3943 return true;
3946 /* Implement TARGET_CANNOT_COPY_INSN_P. */
3948 static bool
3949 riscv_cannot_copy_insn_p (rtx_insn *insn)
3951 return recog_memoized (insn) >= 0 && get_attr_cannot_copy (insn);
3954 /* Initialize the GCC target structure. */
3955 #undef TARGET_ASM_ALIGNED_HI_OP
3956 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
3957 #undef TARGET_ASM_ALIGNED_SI_OP
3958 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
3959 #undef TARGET_ASM_ALIGNED_DI_OP
3960 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
3962 #undef TARGET_OPTION_OVERRIDE
3963 #define TARGET_OPTION_OVERRIDE riscv_option_override
3965 #undef TARGET_LEGITIMIZE_ADDRESS
3966 #define TARGET_LEGITIMIZE_ADDRESS riscv_legitimize_address
3968 #undef TARGET_SCHED_ISSUE_RATE
3969 #define TARGET_SCHED_ISSUE_RATE riscv_issue_rate
3971 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
3972 #define TARGET_FUNCTION_OK_FOR_SIBCALL riscv_function_ok_for_sibcall
3974 #undef TARGET_REGISTER_MOVE_COST
3975 #define TARGET_REGISTER_MOVE_COST riscv_register_move_cost
3976 #undef TARGET_MEMORY_MOVE_COST
3977 #define TARGET_MEMORY_MOVE_COST riscv_memory_move_cost
3978 #undef TARGET_RTX_COSTS
3979 #define TARGET_RTX_COSTS riscv_rtx_costs
3980 #undef TARGET_ADDRESS_COST
3981 #define TARGET_ADDRESS_COST riscv_address_cost
3983 #undef TARGET_ASM_FILE_START
3984 #define TARGET_ASM_FILE_START riscv_file_start
3985 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
3986 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
3988 #undef TARGET_EXPAND_BUILTIN_VA_START
3989 #define TARGET_EXPAND_BUILTIN_VA_START riscv_va_start
3991 #undef TARGET_PROMOTE_FUNCTION_MODE
3992 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
3994 #undef TARGET_RETURN_IN_MEMORY
3995 #define TARGET_RETURN_IN_MEMORY riscv_return_in_memory
3997 #undef TARGET_ASM_OUTPUT_MI_THUNK
3998 #define TARGET_ASM_OUTPUT_MI_THUNK riscv_output_mi_thunk
3999 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
4000 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
4002 #undef TARGET_PRINT_OPERAND
4003 #define TARGET_PRINT_OPERAND riscv_print_operand
4004 #undef TARGET_PRINT_OPERAND_ADDRESS
4005 #define TARGET_PRINT_OPERAND_ADDRESS riscv_print_operand_address
4007 #undef TARGET_SETUP_INCOMING_VARARGS
4008 #define TARGET_SETUP_INCOMING_VARARGS riscv_setup_incoming_varargs
4009 #undef TARGET_STRICT_ARGUMENT_NAMING
4010 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
4011 #undef TARGET_MUST_PASS_IN_STACK
4012 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
4013 #undef TARGET_PASS_BY_REFERENCE
4014 #define TARGET_PASS_BY_REFERENCE riscv_pass_by_reference
4015 #undef TARGET_ARG_PARTIAL_BYTES
4016 #define TARGET_ARG_PARTIAL_BYTES riscv_arg_partial_bytes
4017 #undef TARGET_FUNCTION_ARG
4018 #define TARGET_FUNCTION_ARG riscv_function_arg
4019 #undef TARGET_FUNCTION_ARG_ADVANCE
4020 #define TARGET_FUNCTION_ARG_ADVANCE riscv_function_arg_advance
4021 #undef TARGET_FUNCTION_ARG_BOUNDARY
4022 #define TARGET_FUNCTION_ARG_BOUNDARY riscv_function_arg_boundary
4024 /* The generic ELF target does not always have TLS support. */
4025 #ifdef HAVE_AS_TLS
4026 #undef TARGET_HAVE_TLS
4027 #define TARGET_HAVE_TLS true
4028 #endif
4030 #undef TARGET_CANNOT_FORCE_CONST_MEM
4031 #define TARGET_CANNOT_FORCE_CONST_MEM riscv_cannot_force_const_mem
4033 #undef TARGET_LEGITIMATE_CONSTANT_P
4034 #define TARGET_LEGITIMATE_CONSTANT_P riscv_legitimate_constant_p
4036 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
4037 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
4039 #undef TARGET_LEGITIMATE_ADDRESS_P
4040 #define TARGET_LEGITIMATE_ADDRESS_P riscv_legitimate_address_p
4042 #undef TARGET_CAN_ELIMINATE
4043 #define TARGET_CAN_ELIMINATE riscv_can_eliminate
4045 #undef TARGET_CONDITIONAL_REGISTER_USAGE
4046 #define TARGET_CONDITIONAL_REGISTER_USAGE riscv_conditional_register_usage
4048 #undef TARGET_CLASS_MAX_NREGS
4049 #define TARGET_CLASS_MAX_NREGS riscv_class_max_nregs
4051 #undef TARGET_TRAMPOLINE_INIT
4052 #define TARGET_TRAMPOLINE_INIT riscv_trampoline_init
4054 #undef TARGET_IN_SMALL_DATA_P
4055 #define TARGET_IN_SMALL_DATA_P riscv_in_small_data_p
4057 #undef TARGET_ASM_SELECT_RTX_SECTION
4058 #define TARGET_ASM_SELECT_RTX_SECTION riscv_elf_select_rtx_section
4060 #undef TARGET_MIN_ANCHOR_OFFSET
4061 #define TARGET_MIN_ANCHOR_OFFSET (-IMM_REACH/2)
4063 #undef TARGET_MAX_ANCHOR_OFFSET
4064 #define TARGET_MAX_ANCHOR_OFFSET (IMM_REACH/2-1)
4066 #undef TARGET_REGISTER_PRIORITY
4067 #define TARGET_REGISTER_PRIORITY riscv_register_priority
4069 #undef TARGET_CANNOT_COPY_INSN_P
4070 #define TARGET_CANNOT_COPY_INSN_P riscv_cannot_copy_insn_p
4072 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
4073 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV riscv_atomic_assign_expand_fenv
4075 #undef TARGET_INIT_BUILTINS
4076 #define TARGET_INIT_BUILTINS riscv_init_builtins
4078 #undef TARGET_BUILTIN_DECL
4079 #define TARGET_BUILTIN_DECL riscv_builtin_decl
4081 #undef TARGET_EXPAND_BUILTIN
4082 #define TARGET_EXPAND_BUILTIN riscv_expand_builtin
4084 struct gcc_target targetm = TARGET_INITIALIZER;
4086 #include "gt-riscv.h"