xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/config/riscv/riscv.c (revision 75f6d617e282811cb173c2ccfbf5df0dd71f7045)
1 /* Subroutines used for code generation for RISC-V.
2    Copyright (C) 2011-2014 Free Software Foundation, Inc.
3    Contributed by Andrew Waterman (waterman@cs.berkeley.edu) at UC Berkeley.
4    Based on MIPS target for GNU compiler.
5 
6 This file is part of GCC.
7 
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.
12 
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.
17 
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21 
22 #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 "hard-reg-set.h"
29 #include "insn-config.h"
30 #include "conditions.h"
31 #include "insn-attr.h"
32 #include "recog.h"
33 #include "output.h"
34 #include "tree.h"
35 //#include "varasm.h"
36 //#include "stor-layout.h"
37 //#include "calls.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "optabs.h"
41 #include "libfuncs.h"
42 #include "flags.h"
43 #include "reload.h"
44 #include "tm_p.h"
45 #include "ggc.h"
46 #include "gstab.h"
47 #include "hashtab.h"
48 #include "debug.h"
49 #include "target.h"
50 #include "target-def.h"
51 #include "langhooks.h"
52 #include "sched-int.h"
53 #include "bitmap.h"
54 #include "diagnostic.h"
55 #include "target-globals.h"
56 #include "symcat.h"
57 #include <stdint.h>
58 
59 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF.  */
60 #define UNSPEC_ADDRESS_P(X)					\
61   (GET_CODE (X) == UNSPEC					\
62    && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST			\
63    && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
64 
65 /* Extract the symbol or label from UNSPEC wrapper X.  */
66 #define UNSPEC_ADDRESS(X) \
67   XVECEXP (X, 0, 0)
68 
69 /* Extract the symbol type from UNSPEC wrapper X.  */
70 #define UNSPEC_ADDRESS_TYPE(X) \
71   ((enum riscv_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
72 
73 /* The maximum distance between the top of the stack frame and the
74    value sp has when we save and restore registers.  This is set by the
75    range  of load/store offsets and must also preserve stack alignment. */
76 #define RISCV_MAX_FIRST_STACK_STEP (RISCV_IMM_REACH/2 - 16)
77 
78 /* True if INSN is a riscv.md pattern or asm statement.  */
79 #define USEFUL_INSN_P(INSN)						\
80   (NONDEBUG_INSN_P (INSN)						\
81    && GET_CODE (PATTERN (INSN)) != USE					\
82    && GET_CODE (PATTERN (INSN)) != CLOBBER				\
83    && GET_CODE (PATTERN (INSN)) != ADDR_VEC				\
84    && GET_CODE (PATTERN (INSN)) != ADDR_DIFF_VEC)
85 
86 /* True if bit BIT is set in VALUE.  */
87 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
88 
89 /* Classifies an address.
90 
91    ADDRESS_REG
92        A natural register + offset address.  The register satisfies
93        riscv_valid_base_register_p and the offset is a const_arith_operand.
94 
95    ADDRESS_LO_SUM
96        A LO_SUM rtx.  The first operand is a valid base register and
97        the second operand is a symbolic address.
98 
99    ADDRESS_CONST_INT
100        A signed 16-bit constant address.
101 
102    ADDRESS_SYMBOLIC:
103        A constant symbolic address.  */
104 enum riscv_address_type {
105   ADDRESS_REG,
106   ADDRESS_LO_SUM,
107   ADDRESS_CONST_INT,
108   ADDRESS_SYMBOLIC
109 };
110 
111 enum riscv_code_model riscv_cmodel = TARGET_DEFAULT_CMODEL;
112 
113 /* Macros to create an enumeration identifier for a function prototype.  */
114 #define RISCV_FTYPE_NAME1(A, B) RISCV_##A##_FTYPE_##B
115 #define RISCV_FTYPE_NAME2(A, B, C) RISCV_##A##_FTYPE_##B##_##C
116 #define RISCV_FTYPE_NAME3(A, B, C, D) RISCV_##A##_FTYPE_##B##_##C##_##D
117 #define RISCV_FTYPE_NAME4(A, B, C, D, E) RISCV_##A##_FTYPE_##B##_##C##_##D##_##E
118 
119 /* Classifies the prototype of a built-in function.  */
120 enum riscv_function_type {
121 #define DEF_RISCV_FTYPE(NARGS, LIST) RISCV_FTYPE_NAME##NARGS LIST,
122 #include "config/riscv/riscv-ftypes.def"
123 #undef DEF_RISCV_FTYPE
124   RISCV_MAX_FTYPE_MAX
125 };
126 
127 /* Specifies how a built-in function should be converted into rtl.  */
128 enum riscv_builtin_type {
129   /* The function corresponds directly to an .md pattern.  The return
130      value is mapped to operand 0 and the arguments are mapped to
131      operands 1 and above.  */
132   RISCV_BUILTIN_DIRECT,
133 
134   /* The function corresponds directly to an .md pattern.  There is no return
135      value and the arguments are mapped to operands 0 and above.  */
136   RISCV_BUILTIN_DIRECT_NO_TARGET
137 };
138 
139 /* Information about a function's frame layout.  */
140 struct GTY(())  riscv_frame_info {
141   /* The size of the frame in bytes.  */
142   HOST_WIDE_INT total_size;
143 
144   /* Bit X is set if the function saves or restores GPR X.  */
145   unsigned int mask;
146 
147   /* Likewise FPR X.  */
148   unsigned int fmask;
149 
150   /* Offsets of fixed-point and floating-point save areas from frame bottom */
151   HOST_WIDE_INT gp_sp_offset;
152   HOST_WIDE_INT fp_sp_offset;
153 
154   /* Offset of virtual frame pointer from stack pointer/frame bottom */
155   HOST_WIDE_INT frame_pointer_offset;
156 
157   /* Offset of hard frame pointer from stack pointer/frame bottom */
158   HOST_WIDE_INT hard_frame_pointer_offset;
159 
160   /* The offset of arg_pointer_rtx from the bottom of the frame.  */
161   HOST_WIDE_INT arg_pointer_offset;
162 };
163 
164 struct GTY(())  machine_function {
165   /* The number of extra stack bytes taken up by register varargs.
166      This area is allocated by the callee at the very top of the frame.  */
167   int varargs_size;
168 
169   /* The current frame information, calculated by riscv_compute_frame_info.  */
170   struct riscv_frame_info frame;
171 };
172 
173 /* Information about a single argument.  */
174 struct riscv_arg_info {
175   /* True if the argument is passed in a floating-point register, or
176      would have been if we hadn't run out of registers.  */
177   bool fpr_p;
178 
179   /* The number of words passed in registers, rounded up.  */
180   unsigned int reg_words;
181 
182   /* For EABI, the offset of the first register from GP_ARG_FIRST or
183      FP_ARG_FIRST.  For other ABIs, the offset of the first register from
184      the start of the ABI's argument structure (see the CUMULATIVE_ARGS
185      comment for details).
186 
187      The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
188      on the stack.  */
189   unsigned int reg_offset;
190 
191   /* The number of words that must be passed on the stack, rounded up.  */
192   unsigned int stack_words;
193 
194   /* The offset from the start of the stack overflow area of the argument's
195      first stack word.  Only meaningful when STACK_WORDS is nonzero.  */
196   unsigned int stack_offset;
197 };
198 
199 /* Information about an address described by riscv_address_type.
200 
201    ADDRESS_CONST_INT
202        No fields are used.
203 
204    ADDRESS_REG
205        REG is the base register and OFFSET is the constant offset.
206 
207    ADDRESS_LO_SUM
208        REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
209        is the type of symbol it references.
210 
211    ADDRESS_SYMBOLIC
212        SYMBOL_TYPE is the type of symbol that the address references.  */
213 struct riscv_address_info {
214   enum riscv_address_type type;
215   rtx reg;
216   rtx offset;
217   enum riscv_symbol_type symbol_type;
218 };
219 
220 /* One stage in a constant building sequence.  These sequences have
221    the form:
222 
223 	A = VALUE[0]
224 	A = A CODE[1] VALUE[1]
225 	A = A CODE[2] VALUE[2]
226 	...
227 
228    where A is an accumulator, each CODE[i] is a binary rtl operation
229    and each VALUE[i] is a constant integer.  CODE[0] is undefined.  */
230 struct riscv_integer_op {
231   enum rtx_code code;
232   unsigned HOST_WIDE_INT value;
233 };
234 
235 /* The largest number of operations needed to load an integer constant.
236    The worst case is LUI, ADDI, SLLI, ADDI, SLLI, ADDI, SLLI, ADDI,
237    but we may attempt and reject even worse sequences.  */
238 #define RISCV_MAX_INTEGER_OPS 32
239 
240 /* Costs of various operations on the different architectures.  */
241 
242 struct riscv_tune_info
243 {
244   unsigned short fp_add[2];
245   unsigned short fp_mul[2];
246   unsigned short fp_div[2];
247   unsigned short int_mul[2];
248   unsigned short int_div[2];
249   unsigned short issue_rate;
250   unsigned short branch_cost;
251   unsigned short fp_to_int_cost;
252   unsigned short memory_cost;
253 };
254 
255 /* Information about one CPU we know about.  */
256 struct riscv_cpu_info {
257   /* This CPU's canonical name.  */
258   const char *name;
259 
260   /* The RISC-V ISA and extensions supported by this CPU.  */
261   const char *isa;
262 
263   /* Tuning parameters for this CPU.  */
264   const struct riscv_tune_info *tune_info;
265 };
266 
267 /* Global variables for machine-dependent things.  */
268 
269 /* Which tuning parameters to use.  */
270 static const struct riscv_tune_info *tune_info;
271 
272 /* Index [M][R] is true if register R is allowed to hold a value of mode M.  */
273 bool riscv_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
274 
275 /* riscv_lo_relocs[X] is the relocation to use when a symbol of type X
276    appears in a LO_SUM.  It can be null if such LO_SUMs aren't valid or
277    if they are matched by a special .md file pattern.  */
278 const char *riscv_lo_relocs[NUM_SYMBOL_TYPES];
279 
280 /* Likewise for HIGHs.  */
281 const char *riscv_hi_relocs[NUM_SYMBOL_TYPES];
282 
283 /* Index R is the smallest register class that contains register R.  */
284 const enum reg_class riscv_regno_to_class[FIRST_PSEUDO_REGISTER] = {
285   GR_REGS,	GR_REGS,	GR_REGS,	GR_REGS,
286   GR_REGS,	T_REGS,		T_REGS,		T_REGS,
287   GR_REGS,	GR_REGS,	GR_REGS,	GR_REGS,
288   GR_REGS,	GR_REGS,	GR_REGS,	GR_REGS,
289   GR_REGS,	GR_REGS, 	GR_REGS,	GR_REGS,
290   GR_REGS,	GR_REGS,	GR_REGS,	GR_REGS,
291   GR_REGS,	GR_REGS,	GR_REGS,	GR_REGS,
292   T_REGS,	T_REGS,		T_REGS,		T_REGS,
293   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
294   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
295   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
296   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
297   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
298   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
299   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
300   FP_REGS,	FP_REGS,	FP_REGS,	FP_REGS,
301   FRAME_REGS,	FRAME_REGS,
302 };
303 
304 /* Costs to use when optimizing for size.  */
305 static const struct riscv_tune_info rocket_tune_info = {
306   {COSTS_N_INSNS (4), COSTS_N_INSNS (5)},	/* fp_add */
307   {COSTS_N_INSNS (4), COSTS_N_INSNS (5)},	/* fp_mul */
308   {COSTS_N_INSNS (20), COSTS_N_INSNS (20)},	/* fp_div */
309   {COSTS_N_INSNS (4), COSTS_N_INSNS (4)},	/* int_mul */
310   {COSTS_N_INSNS (6), COSTS_N_INSNS (6)},	/* int_div */
311   1,						/* issue_rate */
312   3,						/* branch_cost */
313   COSTS_N_INSNS (2),				/* fp_to_int_cost */
314   5						/* memory_cost */
315 };
316 
317 /* Costs to use when optimizing for size.  */
318 static const struct riscv_tune_info optimize_size_tune_info = {
319   {COSTS_N_INSNS (1), COSTS_N_INSNS (1)},	/* fp_add */
320   {COSTS_N_INSNS (1), COSTS_N_INSNS (1)},	/* fp_mul */
321   {COSTS_N_INSNS (1), COSTS_N_INSNS (1)},	/* fp_div */
322   {COSTS_N_INSNS (1), COSTS_N_INSNS (1)},	/* int_mul */
323   {COSTS_N_INSNS (1), COSTS_N_INSNS (1)},	/* int_div */
324   1,						/* issue_rate */
325   1,						/* branch_cost */
326   COSTS_N_INSNS (1),				/* fp_to_int_cost */
327   1						/* memory_cost */
328 };
329 
330 /* A table describing all the processors GCC knows about.  */
331 static const struct riscv_cpu_info riscv_cpu_info_table[] = {
332   /* Entries for generic ISAs.  */
333   { "rocket", "IMAFD", &rocket_tune_info },
334 };
335 
336 /* Return the riscv_cpu_info entry for the given name string.  */
337 
338 static const struct riscv_cpu_info *
339 riscv_parse_cpu (const char *cpu_string)
340 {
341   unsigned int i;
342 
343   for (i = 0; i < ARRAY_SIZE (riscv_cpu_info_table); i++)
344     if (strcmp (riscv_cpu_info_table[i].name, cpu_string) == 0)
345       return riscv_cpu_info_table + i;
346 
347   error ("unknown cpu `%s'", cpu_string);
348   return riscv_cpu_info_table;
349 }
350 
351 /* Fill CODES with a sequence of rtl operations to load VALUE.
352    Return the number of operations needed.  */
353 
354 static int
355 riscv_build_integer_1 (struct riscv_integer_op *codes, HOST_WIDE_INT value,
356 		       enum machine_mode mode)
357 {
358   HOST_WIDE_INT low_part = RISCV_CONST_LOW_PART (value);
359   int cost = INT_MAX, alt_cost;
360   struct riscv_integer_op alt_codes[RISCV_MAX_INTEGER_OPS];
361 
362   if (SMALL_OPERAND (value) || LUI_OPERAND (value))
363     {
364       /* Simply ADDI or LUI */
365       codes[0].code = UNKNOWN;
366       codes[0].value = value;
367       return 1;
368     }
369 
370   /* End with ADDI */
371   if (low_part != 0
372       && !(mode == HImode && (int16_t)(value - low_part) != (value - low_part)))
373     {
374       cost = 1 + riscv_build_integer_1 (codes, value - low_part, mode);
375       codes[cost-1].code = PLUS;
376       codes[cost-1].value = low_part;
377     }
378 
379   /* End with XORI */
380   if (cost > 2 && (low_part < 0 || mode == HImode))
381     {
382       alt_cost = 1 + riscv_build_integer_1 (alt_codes, value ^ low_part, mode);
383       alt_codes[alt_cost-1].code = XOR;
384       alt_codes[alt_cost-1].value = low_part;
385       if (alt_cost < cost)
386 	cost = alt_cost, memcpy (codes, alt_codes, sizeof(alt_codes));
387     }
388 
389   /* Eliminate trailing zeros and end with SLLI */
390   if (cost > 2 && (value & 1) == 0)
391     {
392       int shift = 0;
393       while ((value & 1) == 0)
394 	shift++, value >>= 1;
395       alt_cost = 1 + riscv_build_integer_1 (alt_codes, value, mode);
396       alt_codes[alt_cost-1].code = ASHIFT;
397       alt_codes[alt_cost-1].value = shift;
398       if (alt_cost < cost)
399 	cost = alt_cost, memcpy (codes, alt_codes, sizeof(alt_codes));
400     }
401 
402   gcc_assert (cost <= RISCV_MAX_INTEGER_OPS);
403   return cost;
404 }
405 
406 static int
407 riscv_build_integer (struct riscv_integer_op *codes, HOST_WIDE_INT value,
408 		     enum machine_mode mode)
409 {
410   int cost = riscv_build_integer_1 (codes, value, mode);
411 
412   /* Eliminate leading zeros and end with SRLI */
413   if (value > 0 && cost > 2)
414     {
415       struct riscv_integer_op alt_codes[RISCV_MAX_INTEGER_OPS];
416       int alt_cost, shift = 0;
417       HOST_WIDE_INT shifted_val;
418 
419       /* Try filling trailing bits with 1s */
420       while ((value << shift) >= 0)
421 	shift++;
422       shifted_val = (value << shift) | ((((HOST_WIDE_INT) 1) << shift) - 1);
423       alt_cost = 1 + riscv_build_integer_1 (alt_codes, shifted_val, mode);
424       alt_codes[alt_cost-1].code = LSHIFTRT;
425       alt_codes[alt_cost-1].value = shift;
426       if (alt_cost < cost)
427 	cost = alt_cost, memcpy (codes, alt_codes, sizeof (alt_codes));
428 
429       /* Try filling trailing bits with 0s */
430       shifted_val = value << shift;
431       alt_cost = 1 + riscv_build_integer_1 (alt_codes, shifted_val, mode);
432       alt_codes[alt_cost-1].code = LSHIFTRT;
433       alt_codes[alt_cost-1].value = shift;
434       if (alt_cost < cost)
435 	cost = alt_cost, memcpy (codes, alt_codes, sizeof (alt_codes));
436     }
437 
438   return cost;
439 }
440 
441 static int
442 riscv_split_integer_cost (HOST_WIDE_INT val)
443 {
444   int cost;
445   int32_t loval = val, hival = (val - (int32_t)val) >> 32;
446   struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
447 
448   cost = 2 + riscv_build_integer (codes, loval, VOIDmode);
449   if (loval != hival)
450     cost += riscv_build_integer (codes, hival, VOIDmode);
451 
452   return cost;
453 }
454 
455 static int
456 riscv_integer_cost (HOST_WIDE_INT val)
457 {
458   struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
459   return MIN (riscv_build_integer (codes, val, VOIDmode),
460 	      riscv_split_integer_cost (val));
461 }
462 
463 /* Try to split a 64b integer into 32b parts, then reassemble. */
464 
465 static rtx
466 riscv_split_integer (HOST_WIDE_INT val, enum machine_mode mode)
467 {
468   int32_t loval = val, hival = (val - (int32_t)val) >> 32;
469   rtx hi = gen_reg_rtx (mode), lo = gen_reg_rtx (mode);
470 
471   riscv_move_integer (hi, hi, hival);
472   riscv_move_integer (lo, lo, loval);
473 
474   hi = gen_rtx_fmt_ee (ASHIFT, mode, hi, GEN_INT (32));
475   hi = force_reg (mode, hi);
476 
477   return gen_rtx_fmt_ee (PLUS, mode, hi, lo);
478 }
479 
480 /* Return true if X is a thread-local symbol.  */
481 
482 static bool
483 riscv_tls_symbol_p (const_rtx x)
484 {
485   return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
486 }
487 
488 static bool
489 riscv_symbol_binds_local_p (const_rtx x)
490 {
491   return (SYMBOL_REF_DECL (x)
492 	  ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
493 	  : SYMBOL_REF_LOCAL_P (x));
494 }
495 
496 /* Return the method that should be used to access SYMBOL_REF or
497    LABEL_REF X in context CONTEXT.  */
498 
499 static enum riscv_symbol_type
500 riscv_classify_symbol (const_rtx x)
501 {
502   if (riscv_tls_symbol_p (x))
503     return SYMBOL_TLS;
504 
505   if (GET_CODE (x) == LABEL_REF)
506     {
507       if (LABEL_REF_NONLOCAL_P (x))
508 	return SYMBOL_GOT_DISP;
509       return SYMBOL_ABSOLUTE;
510     }
511 
512   gcc_assert (GET_CODE (x) == SYMBOL_REF);
513 
514   if (flag_pic && !riscv_symbol_binds_local_p (x))
515     return SYMBOL_GOT_DISP;
516 
517   return SYMBOL_ABSOLUTE;
518 }
519 
520 /* Classify the base of symbolic expression X, given that X appears in
521    context CONTEXT.  */
522 
523 static enum riscv_symbol_type
524 riscv_classify_symbolic_expression (rtx x)
525 {
526   rtx offset;
527 
528   split_const (x, &x, &offset);
529   if (UNSPEC_ADDRESS_P (x))
530     return UNSPEC_ADDRESS_TYPE (x);
531 
532   return riscv_classify_symbol (x);
533 }
534 
535 /* Return true if X is a symbolic constant that can be used in context
536    CONTEXT.  If it is, store the type of the symbol in *SYMBOL_TYPE.  */
537 
538 bool
539 riscv_symbolic_constant_p (rtx x, enum riscv_symbol_type *symbol_type)
540 {
541   rtx offset;
542 
543   split_const (x, &x, &offset);
544   if (UNSPEC_ADDRESS_P (x))
545     {
546       *symbol_type = UNSPEC_ADDRESS_TYPE (x);
547       x = UNSPEC_ADDRESS (x);
548     }
549   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
550     *symbol_type = riscv_classify_symbol (x);
551   else
552     return false;
553 
554   if (offset == const0_rtx)
555     return true;
556 
557   /* Check whether a nonzero offset is valid for the underlying
558      relocations.  */
559   switch (*symbol_type)
560     {
561     case SYMBOL_ABSOLUTE:
562     case SYMBOL_TLS_LE:
563       return (int32_t) INTVAL (offset) == INTVAL (offset);
564 
565     default:
566       return false;
567     }
568   gcc_unreachable ();
569 }
570 
571 /* Returns the number of instructions necessary to reference a symbol. */
572 
573 static int riscv_symbol_insns (enum riscv_symbol_type type)
574 {
575   switch (type)
576   {
577     case SYMBOL_TLS: return 0; /* Depends on the TLS model. */
578     case SYMBOL_ABSOLUTE: return 2; /* LUI + the reference itself */
579     case SYMBOL_TLS_LE: return 3; /* LUI + ADD TP + the reference itself */
580     case SYMBOL_GOT_DISP: return 3; /* AUIPC + LD GOT + the reference itself */
581     default: gcc_unreachable();
582   }
583 }
584 
585 /* A for_each_rtx callback.  Stop the search if *X references a
586    thread-local symbol.  */
587 
588 static int
589 riscv_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
590 {
591   return riscv_tls_symbol_p (*x);
592 }
593 
594 /* Implement TARGET_LEGITIMATE_CONSTANT_P.  */
595 
596 static bool
597 riscv_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
598 {
599   return riscv_const_insns (x) > 0;
600 }
601 
602 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
603 
604 static bool
605 riscv_cannot_force_const_mem (enum machine_mode mode, rtx x)
606 {
607   enum riscv_symbol_type type;
608   rtx base, offset;
609 
610   /* There is no assembler syntax for expressing an address-sized
611      high part.  */
612   if (GET_CODE (x) == HIGH)
613     return true;
614 
615   /* As an optimization, reject constants that riscv_legitimize_move
616      can expand inline.
617 
618      Suppose we have a multi-instruction sequence that loads constant C
619      into register R.  If R does not get allocated a hard register, and
620      R is used in an operand that allows both registers and memory
621      references, reload will consider forcing C into memory and using
622      one of the instruction's memory alternatives.  Returning false
623      here will force it to use an input reload instead.  */
624   if (CONST_INT_P (x) && riscv_legitimate_constant_p (mode, x))
625     return true;
626 
627   split_const (x, &base, &offset);
628   if (riscv_symbolic_constant_p (base, &type))
629     {
630       /* The same optimization as for CONST_INT.  */
631       if (SMALL_INT (offset) && riscv_symbol_insns (type) > 0)
632 	return true;
633 
634       /* It's not worth creating additional dynamic relocations.  */
635       if (flag_pic)
636 	return true;
637     }
638 
639   /* TLS symbols must be computed by riscv_legitimize_move.  */
640   if (for_each_rtx (&x, &riscv_tls_symbol_ref_1, NULL))
641     return true;
642 
643   return false;
644 }
645 
646 /* Return true if register REGNO is a valid base register for mode MODE.
647    STRICT_P is true if REG_OK_STRICT is in effect.  */
648 
649 int
650 riscv_regno_mode_ok_for_base_p (int regno, enum machine_mode mode ATTRIBUTE_UNUSED,
651 			       bool strict_p)
652 {
653   if (!HARD_REGISTER_NUM_P (regno))
654     {
655       if (!strict_p)
656 	return true;
657       regno = reg_renumber[regno];
658     }
659 
660   /* These fake registers will be eliminated to either the stack or
661      hard frame pointer, both of which are usually valid base registers.
662      Reload deals with the cases where the eliminated form isn't valid.  */
663   if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
664     return true;
665 
666   return GP_REG_P (regno);
667 }
668 
669 /* Return true if X is a valid base register for mode MODE.
670    STRICT_P is true if REG_OK_STRICT is in effect.  */
671 
672 static bool
673 riscv_valid_base_register_p (rtx x, enum machine_mode mode, bool strict_p)
674 {
675   if (!strict_p && GET_CODE (x) == SUBREG)
676     x = SUBREG_REG (x);
677 
678   return (REG_P (x)
679 	  && riscv_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
680 }
681 
682 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
683    can address a value of mode MODE.  */
684 
685 static bool
686 riscv_valid_offset_p (rtx x, enum machine_mode mode)
687 {
688   /* Check that X is a signed 12-bit number.  */
689   if (!const_arith_operand (x, Pmode))
690     return false;
691 
692   /* We may need to split multiword moves, so make sure that every word
693      is accessible.  */
694   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
695       && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
696     return false;
697 
698   return true;
699 }
700 
701 /* Return true if a LO_SUM can address a value of mode MODE when the
702    LO_SUM symbol has type SYMBOL_TYPE.  */
703 
704 static bool
705 riscv_valid_lo_sum_p (enum riscv_symbol_type symbol_type, enum machine_mode mode)
706 {
707   /* Check that symbols of type SYMBOL_TYPE can be used to access values
708      of mode MODE.  */
709   if (riscv_symbol_insns (symbol_type) == 0)
710     return false;
711 
712   /* Check that there is a known low-part relocation.  */
713   if (riscv_lo_relocs[symbol_type] == NULL)
714     return false;
715 
716   /* We may need to split multiword moves, so make sure that each word
717      can be accessed without inducing a carry.  This is mainly needed
718      for o64, which has historically only guaranteed 64-bit alignment
719      for 128-bit types.  */
720   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
721       && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
722     return false;
723 
724   return true;
725 }
726 
727 /* Return true if X is a valid address for machine mode MODE.  If it is,
728    fill in INFO appropriately.  STRICT_P is true if REG_OK_STRICT is in
729    effect.  */
730 
731 static bool
732 riscv_classify_address (struct riscv_address_info *info, rtx x,
733 		       enum machine_mode mode, bool strict_p)
734 {
735   switch (GET_CODE (x))
736     {
737     case REG:
738     case SUBREG:
739       info->type = ADDRESS_REG;
740       info->reg = x;
741       info->offset = const0_rtx;
742       return riscv_valid_base_register_p (info->reg, mode, strict_p);
743 
744     case PLUS:
745       info->type = ADDRESS_REG;
746       info->reg = XEXP (x, 0);
747       info->offset = XEXP (x, 1);
748       return (riscv_valid_base_register_p (info->reg, mode, strict_p)
749 	      && riscv_valid_offset_p (info->offset, mode));
750 
751     case LO_SUM:
752       info->type = ADDRESS_LO_SUM;
753       info->reg = XEXP (x, 0);
754       info->offset = XEXP (x, 1);
755       /* We have to trust the creator of the LO_SUM to do something vaguely
756 	 sane.  Target-independent code that creates a LO_SUM should also
757 	 create and verify the matching HIGH.  Target-independent code that
758 	 adds an offset to a LO_SUM must prove that the offset will not
759 	 induce a carry.  Failure to do either of these things would be
760 	 a bug, and we are not required to check for it here.  The RISCV
761 	 backend itself should only create LO_SUMs for valid symbolic
762 	 constants, with the high part being either a HIGH or a copy
763 	 of _gp. */
764       info->symbol_type
765 	= riscv_classify_symbolic_expression (info->offset);
766       return (riscv_valid_base_register_p (info->reg, mode, strict_p)
767 	      && riscv_valid_lo_sum_p (info->symbol_type, mode));
768 
769     case CONST_INT:
770       /* Small-integer addresses don't occur very often, but they
771 	 are legitimate if $0 is a valid base register.  */
772       info->type = ADDRESS_CONST_INT;
773       return SMALL_INT (x);
774 
775     default:
776       return false;
777     }
778 }
779 
780 /* Implement TARGET_LEGITIMATE_ADDRESS_P.  */
781 
782 static bool
783 riscv_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
784 {
785   struct riscv_address_info addr;
786 
787   return riscv_classify_address (&addr, x, mode, strict_p);
788 }
789 
790 /* Return the number of instructions needed to load or store a value
791    of mode MODE at address X.  Return 0 if X isn't valid for MODE.
792    Assume that multiword moves may need to be split into word moves
793    if MIGHT_SPLIT_P, otherwise assume that a single load or store is
794    enough. */
795 
796 int
797 riscv_address_insns (rtx x, enum machine_mode mode, bool might_split_p)
798 {
799   struct riscv_address_info addr;
800   int n = 1;
801 
802   if (!riscv_classify_address (&addr, x, mode, false))
803     return 0;
804 
805   /* BLKmode is used for single unaligned loads and stores and should
806      not count as a multiword mode. */
807   if (mode != BLKmode && might_split_p)
808     n += (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
809 
810   if (addr.type == ADDRESS_LO_SUM)
811     n += riscv_symbol_insns (addr.symbol_type) - 1;
812 
813   return n;
814 }
815 
816 /* Return the number of instructions needed to load constant X.
817    Return 0 if X isn't a valid constant.  */
818 
819 int
820 riscv_const_insns (rtx x)
821 {
822   enum riscv_symbol_type symbol_type;
823   rtx offset;
824 
825   switch (GET_CODE (x))
826     {
827     case HIGH:
828       if (!riscv_symbolic_constant_p (XEXP (x, 0), &symbol_type)
829 	  || !riscv_hi_relocs[symbol_type])
830 	return 0;
831 
832       /* This is simply an LUI. */
833       return 1;
834 
835     case CONST_INT:
836       {
837 	int cost = riscv_integer_cost (INTVAL (x));
838 	/* Force complicated constants to memory. */
839 	return cost < 4 ? cost : 0;
840       }
841 
842     case CONST_DOUBLE:
843     case CONST_VECTOR:
844       /* Allow zeros for normal mode, where we can use x0.  */
845       return x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
846 
847     case CONST:
848       /* See if we can refer to X directly.  */
849       if (riscv_symbolic_constant_p (x, &symbol_type))
850 	return riscv_symbol_insns (symbol_type);
851 
852       /* Otherwise try splitting the constant into a base and offset.
853 	 If the offset is a 16-bit value, we can load the base address
854 	 into a register and then use (D)ADDIU to add in the offset.
855 	 If the offset is larger, we can load the base and offset
856 	 into separate registers and add them together with (D)ADDU.
857 	 However, the latter is only possible before reload; during
858 	 and after reload, we must have the option of forcing the
859 	 constant into the pool instead.  */
860       split_const (x, &x, &offset);
861       if (offset != 0)
862 	{
863 	  int n = riscv_const_insns (x);
864 	  if (n != 0)
865 	    {
866 	      if (SMALL_INT (offset))
867 		return n + 1;
868 	      else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
869 		return n + 1 + riscv_integer_cost (INTVAL (offset));
870 	    }
871 	}
872       return 0;
873 
874     case SYMBOL_REF:
875     case LABEL_REF:
876       return riscv_symbol_insns (riscv_classify_symbol (x));
877 
878     default:
879       return 0;
880     }
881 }
882 
883 /* X is a doubleword constant that can be handled by splitting it into
884    two words and loading each word separately.  Return the number of
885    instructions required to do this.  */
886 
887 int
888 riscv_split_const_insns (rtx x)
889 {
890   unsigned int low, high;
891 
892   low = riscv_const_insns (riscv_subword (x, false));
893   high = riscv_const_insns (riscv_subword (x, true));
894   gcc_assert (low > 0 && high > 0);
895   return low + high;
896 }
897 
898 /* Return the number of instructions needed to implement INSN,
899    given that it loads from or stores to MEM. */
900 
901 int
902 riscv_load_store_insns (rtx mem, rtx insn)
903 {
904   enum machine_mode mode;
905   bool might_split_p;
906   rtx set;
907 
908   gcc_assert (MEM_P (mem));
909   mode = GET_MODE (mem);
910 
911   /* Try to prove that INSN does not need to be split.  */
912   might_split_p = true;
913   if (GET_MODE_BITSIZE (mode) == 64)
914     {
915       set = single_set (insn);
916       if (set && !riscv_split_64bit_move_p (SET_DEST (set), SET_SRC (set)))
917 	might_split_p = false;
918     }
919 
920   return riscv_address_insns (XEXP (mem, 0), mode, might_split_p);
921 }
922 
923 /* Emit a move from SRC to DEST.  Assume that the move expanders can
924    handle all moves if !can_create_pseudo_p ().  The distinction is
925    important because, unlike emit_move_insn, the move expanders know
926    how to force Pmode objects into the constant pool even when the
927    constant pool address is not itself legitimate.  */
928 
929 rtx
930 riscv_emit_move (rtx dest, rtx src)
931 {
932   return (can_create_pseudo_p ()
933 	  ? emit_move_insn (dest, src)
934 	  : emit_move_insn_1 (dest, src));
935 }
936 
937 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)).  */
938 
939 static void
940 riscv_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
941 {
942   emit_insn (gen_rtx_SET (VOIDmode, target,
943 			  gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
944 }
945 
946 /* Compute (CODE OP0 OP1) and store the result in a new register
947    of mode MODE.  Return that new register.  */
948 
949 static rtx
950 riscv_force_binary (enum machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
951 {
952   rtx reg;
953 
954   reg = gen_reg_rtx (mode);
955   riscv_emit_binary (code, reg, op0, op1);
956   return reg;
957 }
958 
959 /* Copy VALUE to a register and return that register.  If new pseudos
960    are allowed, copy it into a new register, otherwise use DEST.  */
961 
962 static rtx
963 riscv_force_temporary (rtx dest, rtx value)
964 {
965   if (can_create_pseudo_p ())
966     return force_reg (Pmode, value);
967   else
968     {
969       riscv_emit_move (dest, value);
970       return dest;
971     }
972 }
973 
974 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
975    then add CONST_INT OFFSET to the result.  */
976 
977 static rtx
978 riscv_unspec_address_offset (rtx base, rtx offset,
979 			    enum riscv_symbol_type symbol_type)
980 {
981   base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
982 			 UNSPEC_ADDRESS_FIRST + symbol_type);
983   if (offset != const0_rtx)
984     base = gen_rtx_PLUS (Pmode, base, offset);
985   return gen_rtx_CONST (Pmode, base);
986 }
987 
988 /* Return an UNSPEC address with underlying address ADDRESS and symbol
989    type SYMBOL_TYPE.  */
990 
991 rtx
992 riscv_unspec_address (rtx address, enum riscv_symbol_type symbol_type)
993 {
994   rtx base, offset;
995 
996   split_const (address, &base, &offset);
997   return riscv_unspec_address_offset (base, offset, symbol_type);
998 }
999 
1000 /* If OP is an UNSPEC address, return the address to which it refers,
1001    otherwise return OP itself.  */
1002 
1003 static rtx
1004 riscv_strip_unspec_address (rtx op)
1005 {
1006   rtx base, offset;
1007 
1008   split_const (op, &base, &offset);
1009   if (UNSPEC_ADDRESS_P (base))
1010     op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
1011   return op;
1012 }
1013 
1014 /* If riscv_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
1015    high part to BASE and return the result.  Just return BASE otherwise.
1016    TEMP is as for riscv_force_temporary.
1017 
1018    The returned expression can be used as the first operand to a LO_SUM.  */
1019 
1020 static rtx
1021 riscv_unspec_offset_high (rtx temp, rtx addr, enum riscv_symbol_type symbol_type)
1022 {
1023   addr = gen_rtx_HIGH (Pmode, riscv_unspec_address (addr, symbol_type));
1024   return riscv_force_temporary (temp, addr);
1025 }
1026 
1027 /* Load an entry from the GOT. */
1028 static rtx riscv_got_load_tls_gd(rtx dest, rtx sym)
1029 {
1030   return (Pmode == DImode ? gen_got_load_tls_gddi(dest, sym) : gen_got_load_tls_gdsi(dest, sym));
1031 }
1032 
1033 static rtx riscv_got_load_tls_ie(rtx dest, rtx sym)
1034 {
1035   return (Pmode == DImode ? gen_got_load_tls_iedi(dest, sym) : gen_got_load_tls_iesi(dest, sym));
1036 }
1037 
1038 static rtx riscv_tls_add_tp_le(rtx dest, rtx base, rtx sym)
1039 {
1040   rtx tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
1041   return (Pmode == DImode ? gen_tls_add_tp_ledi(dest, base, tp, sym) : gen_tls_add_tp_lesi(dest, base, tp, sym));
1042 }
1043 
1044 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
1045    it appears in a MEM of that mode.  Return true if ADDR is a legitimate
1046    constant in that context and can be split into high and low parts.
1047    If so, and if LOW_OUT is nonnull, emit the high part and store the
1048    low part in *LOW_OUT.  Leave *LOW_OUT unchanged otherwise.
1049 
1050    TEMP is as for riscv_force_temporary and is used to load the high
1051    part into a register.
1052 
1053    When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
1054    a legitimize SET_SRC for an .md pattern, otherwise the low part
1055    is guaranteed to be a legitimate address for mode MODE.  */
1056 
1057 bool
1058 riscv_split_symbol (rtx temp, rtx addr, enum machine_mode mode, rtx *low_out)
1059 {
1060   enum riscv_symbol_type symbol_type;
1061   rtx high;
1062 
1063   if ((GET_CODE (addr) == HIGH && mode == MAX_MACHINE_MODE)
1064       || !riscv_symbolic_constant_p (addr, &symbol_type)
1065       || riscv_symbol_insns (symbol_type) == 0
1066       || !riscv_hi_relocs[symbol_type])
1067     return false;
1068 
1069   if (low_out)
1070     {
1071       switch (symbol_type)
1072 	{
1073 	case SYMBOL_ABSOLUTE:
1074 	  high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
1075       	  high = riscv_force_temporary (temp, high);
1076       	  *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
1077 	  break;
1078 
1079 	default:
1080 	  gcc_unreachable ();
1081 	}
1082     }
1083 
1084   return true;
1085 }
1086 
1087 /* Return a legitimate address for REG + OFFSET.  TEMP is as for
1088    riscv_force_temporary; it is only needed when OFFSET is not a
1089    SMALL_OPERAND.  */
1090 
1091 static rtx
1092 riscv_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
1093 {
1094   if (!SMALL_OPERAND (offset))
1095     {
1096       rtx high;
1097 
1098       /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
1099          The addition inside the macro CONST_HIGH_PART may cause an
1100          overflow, so we need to force a sign-extension check.  */
1101       high = gen_int_mode (RISCV_CONST_HIGH_PART (offset), Pmode);
1102       offset = RISCV_CONST_LOW_PART (offset);
1103       high = riscv_force_temporary (temp, high);
1104       reg = riscv_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
1105     }
1106   return plus_constant (Pmode, reg, offset);
1107 }
1108 
1109 /* The __tls_get_attr symbol.  */
1110 static GTY(()) rtx riscv_tls_symbol;
1111 
1112 /* Return an instruction sequence that calls __tls_get_addr.  SYM is
1113    the TLS symbol we are referencing and TYPE is the symbol type to use
1114    (either global dynamic or local dynamic).  RESULT is an RTX for the
1115    return value location.  */
1116 
1117 static rtx
1118 riscv_call_tls_get_addr (rtx sym, rtx result)
1119 {
1120   rtx insn, a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
1121 
1122   if (!riscv_tls_symbol)
1123     riscv_tls_symbol = init_one_libfunc ("__tls_get_addr");
1124 
1125   start_sequence ();
1126 
1127   emit_insn (riscv_got_load_tls_gd (a0, sym));
1128   insn = riscv_expand_call (false, result, riscv_tls_symbol, const0_rtx);
1129   RTL_CONST_CALL_P (insn) = 1;
1130   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
1131   insn = get_insns ();
1132 
1133   end_sequence ();
1134 
1135   return insn;
1136 }
1137 
1138 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
1139    its address.  The return value will be both a valid address and a valid
1140    SET_SRC (either a REG or a LO_SUM).  */
1141 
1142 static rtx
1143 riscv_legitimize_tls_address (rtx loc)
1144 {
1145   rtx dest, insn, tp, tmp1;
1146   enum tls_model model = SYMBOL_REF_TLS_MODEL (loc);
1147 
1148   /* Since we support TLS copy relocs, non-PIC TLS accesses may all use LE.  */
1149   if (!flag_pic)
1150     model = TLS_MODEL_LOCAL_EXEC;
1151 
1152   switch (model)
1153     {
1154     case TLS_MODEL_LOCAL_DYNAMIC:
1155       /* Rely on section anchors for the optimization that LDM TLS
1156 	 provides.  The anchor's address is loaded with GD TLS. */
1157     case TLS_MODEL_GLOBAL_DYNAMIC:
1158       tmp1 = gen_rtx_REG (Pmode, GP_RETURN);
1159       insn = riscv_call_tls_get_addr (loc, tmp1);
1160       dest = gen_reg_rtx (Pmode);
1161       emit_libcall_block (insn, dest, tmp1, loc);
1162       break;
1163 
1164     case TLS_MODEL_INITIAL_EXEC:
1165       /* la.tls.ie; tp-relative add */
1166       tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
1167       tmp1 = gen_reg_rtx (Pmode);
1168       emit_insn (riscv_got_load_tls_ie (tmp1, loc));
1169       dest = gen_reg_rtx (Pmode);
1170       emit_insn (gen_add3_insn (dest, tmp1, tp));
1171       break;
1172 
1173     case TLS_MODEL_LOCAL_EXEC:
1174       tmp1 = riscv_unspec_offset_high (NULL, loc, SYMBOL_TLS_LE);
1175       dest = gen_reg_rtx (Pmode);
1176       emit_insn (riscv_tls_add_tp_le (dest, tmp1, loc));
1177       dest = gen_rtx_LO_SUM (Pmode, dest,
1178 			     riscv_unspec_address (loc, SYMBOL_TLS_LE));
1179       break;
1180 
1181     default:
1182       gcc_unreachable ();
1183     }
1184   return dest;
1185 }
1186 
1187 /* If X is not a valid address for mode MODE, force it into a register.  */
1188 
1189 static rtx
1190 riscv_force_address (rtx x, enum machine_mode mode)
1191 {
1192   if (!riscv_legitimate_address_p (mode, x, false))
1193     x = force_reg (Pmode, x);
1194   return x;
1195 }
1196 
1197 /* This function is used to implement LEGITIMIZE_ADDRESS.  If X can
1198    be legitimized in a way that the generic machinery might not expect,
1199    return a new address, otherwise return NULL.  MODE is the mode of
1200    the memory being accessed.  */
1201 
1202 static rtx
1203 riscv_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1204 			 enum machine_mode mode)
1205 {
1206   rtx addr;
1207 
1208   if (riscv_tls_symbol_p (x))
1209     return riscv_legitimize_tls_address (x);
1210 
1211   /* See if the address can split into a high part and a LO_SUM.  */
1212   if (riscv_split_symbol (NULL, x, mode, &addr))
1213     return riscv_force_address (addr, mode);
1214 
1215   /* Handle BASE + OFFSET using riscv_add_offset.  */
1216   if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1))
1217       && INTVAL (XEXP (x, 1)) != 0)
1218     {
1219       rtx base = XEXP (x, 0);
1220       HOST_WIDE_INT offset = INTVAL (XEXP (x, 1));
1221 
1222       if (!riscv_valid_base_register_p (base, mode, false))
1223 	base = copy_to_mode_reg (Pmode, base);
1224       addr = riscv_add_offset (NULL, base, offset);
1225       return riscv_force_address (addr, mode);
1226     }
1227 
1228   return x;
1229 }
1230 
1231 /* Load VALUE into DEST.  TEMP is as for riscv_force_temporary.  */
1232 
1233 void
1234 riscv_move_integer (rtx temp, rtx dest, HOST_WIDE_INT value)
1235 {
1236   struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
1237   enum machine_mode mode;
1238   int i, num_ops;
1239   rtx x;
1240 
1241   mode = GET_MODE (dest);
1242   num_ops = riscv_build_integer (codes, value, mode);
1243 
1244   if (can_create_pseudo_p () && num_ops > 2 /* not a simple constant */
1245       && num_ops >= riscv_split_integer_cost (value))
1246     x = riscv_split_integer (value, mode);
1247   else
1248     {
1249       /* Apply each binary operation to X. */
1250       x = GEN_INT (codes[0].value);
1251 
1252       for (i = 1; i < num_ops; i++)
1253         {
1254           if (!can_create_pseudo_p ())
1255             {
1256               emit_insn (gen_rtx_SET (VOIDmode, temp, x));
1257               x = temp;
1258             }
1259           else
1260             x = force_reg (mode, x);
1261 
1262           x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
1263         }
1264     }
1265 
1266   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
1267 }
1268 
1269 /* Subroutine of riscv_legitimize_move.  Move constant SRC into register
1270    DEST given that SRC satisfies immediate_operand but doesn't satisfy
1271    move_operand.  */
1272 
1273 static void
1274 riscv_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
1275 {
1276   rtx base, offset;
1277 
1278   /* Split moves of big integers into smaller pieces.  */
1279   if (splittable_const_int_operand (src, mode))
1280     {
1281       riscv_move_integer (dest, dest, INTVAL (src));
1282       return;
1283     }
1284 
1285   /* Split moves of symbolic constants into high/low pairs.  */
1286   if (riscv_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
1287     {
1288       emit_insn (gen_rtx_SET (VOIDmode, dest, src));
1289       return;
1290     }
1291 
1292   /* Generate the appropriate access sequences for TLS symbols.  */
1293   if (riscv_tls_symbol_p (src))
1294     {
1295       riscv_emit_move (dest, riscv_legitimize_tls_address (src));
1296       return;
1297     }
1298 
1299   /* If we have (const (plus symbol offset)), and that expression cannot
1300      be forced into memory, load the symbol first and add in the offset.  Also
1301      prefer to do this even if the constant _can_ be forced into memory, as it
1302      usually produces better code.  */
1303   split_const (src, &base, &offset);
1304   if (offset != const0_rtx
1305       && (targetm.cannot_force_const_mem (mode, src) || can_create_pseudo_p ()))
1306     {
1307       base = riscv_force_temporary (dest, base);
1308       riscv_emit_move (dest, riscv_add_offset (NULL, base, INTVAL (offset)));
1309       return;
1310     }
1311 
1312   src = force_const_mem (mode, src);
1313 
1314   /* When using explicit relocs, constant pool references are sometimes
1315      not legitimate addresses.  */
1316   riscv_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
1317   riscv_emit_move (dest, src);
1318 }
1319 
1320 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
1321    sequence that is valid.  */
1322 
1323 bool
1324 riscv_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
1325 {
1326   if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
1327     {
1328       riscv_emit_move (dest, force_reg (mode, src));
1329       return true;
1330     }
1331 
1332   /* We need to deal with constants that would be legitimate
1333      immediate_operands but aren't legitimate move_operands.  */
1334   if (CONSTANT_P (src) && !move_operand (src, mode))
1335     {
1336       riscv_legitimize_const_move (mode, dest, src);
1337       set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
1338       return true;
1339     }
1340   return false;
1341 }
1342 
1343 /* Return true if there is an instruction that implements CODE and accepts
1344    X as an immediate operand. */
1345 
1346 static int
1347 riscv_immediate_operand_p (int code, HOST_WIDE_INT x)
1348 {
1349   switch (code)
1350     {
1351     case ASHIFT:
1352     case ASHIFTRT:
1353     case LSHIFTRT:
1354       /* All shift counts are truncated to a valid constant.  */
1355       return true;
1356 
1357     case AND:
1358     case IOR:
1359     case XOR:
1360     case PLUS:
1361     case LT:
1362     case LTU:
1363       /* These instructions take 12-bit signed immediates.  */
1364       return SMALL_OPERAND (x);
1365 
1366     case LE:
1367       /* We add 1 to the immediate and use SLT.  */
1368       return SMALL_OPERAND (x + 1);
1369 
1370     case LEU:
1371       /* Likewise SLTU, but reject the always-true case.  */
1372       return SMALL_OPERAND (x + 1) && x + 1 != 0;
1373 
1374     case GE:
1375     case GEU:
1376       /* We can emulate an immediate of 1 by using GT/GTU against x0. */
1377       return x == 1;
1378 
1379     default:
1380       /* By default assume that x0 can be used for 0.  */
1381       return x == 0;
1382     }
1383 }
1384 
1385 /* Return the cost of binary operation X, given that the instruction
1386    sequence for a word-sized or smaller operation takes SIGNLE_INSNS
1387    instructions and that the sequence of a double-word operation takes
1388    DOUBLE_INSNS instructions.  */
1389 
1390 static int
1391 riscv_binary_cost (rtx x, int single_insns, int double_insns)
1392 {
1393   if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
1394     return COSTS_N_INSNS (double_insns);
1395   return COSTS_N_INSNS (single_insns);
1396 }
1397 
1398 /* Return the cost of sign-extending OP to mode MODE, not including the
1399    cost of OP itself.  */
1400 
1401 static int
1402 riscv_sign_extend_cost (enum machine_mode mode, rtx op)
1403 {
1404   if (MEM_P (op))
1405     /* Extended loads are as cheap as unextended ones.  */
1406     return 0;
1407 
1408   if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
1409     /* A sign extension from SImode to DImode in 64-bit mode is free.  */
1410     return 0;
1411 
1412   /* We need to use a shift left and a shift right.  */
1413   return COSTS_N_INSNS (2);
1414 }
1415 
1416 /* Return the cost of zero-extending OP to mode MODE, not including the
1417    cost of OP itself.  */
1418 
1419 static int
1420 riscv_zero_extend_cost (enum machine_mode mode, rtx op)
1421 {
1422   if (MEM_P (op))
1423     /* Extended loads are as cheap as unextended ones.  */
1424     return 0;
1425 
1426   if ((TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode) ||
1427       ((mode == DImode || mode == SImode) && GET_MODE (op) == HImode))
1428     /* We need a shift left by 32 bits and a shift right by 32 bits.  */
1429     return COSTS_N_INSNS (2);
1430 
1431   /* We can use ANDI.  */
1432   return COSTS_N_INSNS (1);
1433 }
1434 
1435 /* Implement TARGET_RTX_COSTS.  */
1436 
1437 static bool
1438 riscv_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
1439 		 int *total, bool speed)
1440 {
1441   enum machine_mode mode = GET_MODE (x);
1442   bool float_mode_p = FLOAT_MODE_P (mode);
1443   int cost;
1444 
1445   switch (code)
1446     {
1447     case CONST_INT:
1448       if (riscv_immediate_operand_p (outer_code, INTVAL (x)))
1449 	{
1450 	  *total = 0;
1451 	  return true;
1452 	}
1453       /* Fall through.  */
1454 
1455     case SYMBOL_REF:
1456     case LABEL_REF:
1457     case CONST_DOUBLE:
1458     case CONST:
1459       if (speed)
1460 	*total = 1;
1461       else if ((cost = riscv_const_insns (x)) > 0)
1462 	*total = COSTS_N_INSNS (cost);
1463       else /* The instruction will be fetched from the constant pool.  */
1464 	*total = COSTS_N_INSNS (riscv_symbol_insns (SYMBOL_ABSOLUTE));
1465       return true;
1466 
1467     case MEM:
1468       /* If the address is legitimate, return the number of
1469 	 instructions it needs.  */
1470       if ((cost = riscv_address_insns (XEXP (x, 0), mode, true)) > 0)
1471 	{
1472 	  *total = COSTS_N_INSNS (cost + tune_info->memory_cost);
1473 	  return true;
1474 	}
1475       /* Otherwise use the default handling.  */
1476       return false;
1477 
1478     case NOT:
1479       *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
1480       return false;
1481 
1482     case AND:
1483     case IOR:
1484     case XOR:
1485       /* Double-word operations use two single-word operations.  */
1486       *total = riscv_binary_cost (x, 1, 2);
1487       return false;
1488 
1489     case ASHIFT:
1490     case ASHIFTRT:
1491     case LSHIFTRT:
1492       *total = riscv_binary_cost (x, 1, CONSTANT_P (XEXP (x, 1)) ? 4 : 9);
1493       return false;
1494 
1495     case ABS:
1496       *total = COSTS_N_INSNS (float_mode_p ? 1 : 3);
1497       return false;
1498 
1499     case LO_SUM:
1500       *total = set_src_cost (XEXP (x, 0), speed);
1501       return true;
1502 
1503     case LT:
1504     case LTU:
1505     case LE:
1506     case LEU:
1507     case GT:
1508     case GTU:
1509     case GE:
1510     case GEU:
1511     case EQ:
1512     case NE:
1513     case UNORDERED:
1514     case LTGT:
1515       /* Branch comparisons have VOIDmode, so use the first operand's
1516 	 mode instead.  */
1517       mode = GET_MODE (XEXP (x, 0));
1518       if (float_mode_p)
1519 	*total = tune_info->fp_add[mode == DFmode];
1520       else
1521 	*total = riscv_binary_cost (x, 1, 3);
1522       return false;
1523 
1524     case MINUS:
1525       if (float_mode_p
1526 	  && !HONOR_NANS (mode)
1527 	  && !HONOR_SIGNED_ZEROS (mode))
1528 	{
1529 	  /* See if we can use NMADD or NMSUB.  See riscv.md for the
1530 	     associated patterns.  */
1531 	  rtx op0 = XEXP (x, 0);
1532 	  rtx op1 = XEXP (x, 1);
1533 	  if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
1534 	    {
1535 	      *total = (tune_info->fp_mul[mode == DFmode]
1536 			+ set_src_cost (XEXP (XEXP (op0, 0), 0), speed)
1537 			+ set_src_cost (XEXP (op0, 1), speed)
1538 			+ set_src_cost (op1, speed));
1539 	      return true;
1540 	    }
1541 	  if (GET_CODE (op1) == MULT)
1542 	    {
1543 	      *total = (tune_info->fp_mul[mode == DFmode]
1544 			+ set_src_cost (op0, speed)
1545 			+ set_src_cost (XEXP (op1, 0), speed)
1546 			+ set_src_cost (XEXP (op1, 1), speed));
1547 	      return true;
1548 	    }
1549 	}
1550       /* Fall through.  */
1551 
1552     case PLUS:
1553       if (float_mode_p)
1554 	*total = tune_info->fp_add[mode == DFmode];
1555       else
1556 	*total = riscv_binary_cost (x, 1, 4);
1557       return false;
1558 
1559     case NEG:
1560       if (float_mode_p
1561 	  && !HONOR_NANS (mode)
1562 	  && HONOR_SIGNED_ZEROS (mode))
1563 	{
1564 	  /* See if we can use NMADD or NMSUB.  See riscv.md for the
1565 	     associated patterns.  */
1566 	  rtx op = XEXP (x, 0);
1567 	  if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
1568 	      && GET_CODE (XEXP (op, 0)) == MULT)
1569 	    {
1570 	      *total = (tune_info->fp_mul[mode == DFmode]
1571 			+ set_src_cost (XEXP (XEXP (op, 0), 0), speed)
1572 			+ set_src_cost (XEXP (XEXP (op, 0), 1), speed)
1573 			+ set_src_cost (XEXP (op, 1), speed));
1574 	      return true;
1575 	    }
1576 	}
1577 
1578       if (float_mode_p)
1579 	*total = tune_info->fp_add[mode == DFmode];
1580       else
1581 	*total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
1582       return false;
1583 
1584     case MULT:
1585       if (float_mode_p)
1586 	*total = tune_info->fp_mul[mode == DFmode];
1587       else if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
1588 	*total = 3 * tune_info->int_mul[0] + COSTS_N_INSNS (2);
1589       else if (!speed)
1590 	*total = COSTS_N_INSNS (1);
1591       else
1592 	*total = tune_info->int_mul[mode == DImode];
1593       return false;
1594 
1595     case DIV:
1596     case SQRT:
1597     case MOD:
1598       if (float_mode_p)
1599 	{
1600 	  *total = tune_info->fp_div[mode == DFmode];
1601 	  return false;
1602 	}
1603       /* Fall through.  */
1604 
1605     case UDIV:
1606     case UMOD:
1607       if (speed)
1608 	*total = tune_info->int_div[mode == DImode];
1609       else
1610 	*total = COSTS_N_INSNS (1);
1611       return false;
1612 
1613     case SIGN_EXTEND:
1614       *total = riscv_sign_extend_cost (mode, XEXP (x, 0));
1615       return false;
1616 
1617     case ZERO_EXTEND:
1618       *total = riscv_zero_extend_cost (mode, XEXP (x, 0));
1619       return false;
1620 
1621     case FLOAT:
1622     case UNSIGNED_FLOAT:
1623     case FIX:
1624     case FLOAT_EXTEND:
1625     case FLOAT_TRUNCATE:
1626       *total = tune_info->fp_add[mode == DFmode];
1627       return false;
1628 
1629     default:
1630       return false;
1631     }
1632 }
1633 
1634 /* Implement TARGET_ADDRESS_COST.  */
1635 
1636 static int
1637 riscv_address_cost (rtx addr, enum machine_mode mode,
1638 		    addr_space_t as ATTRIBUTE_UNUSED,
1639 		    bool speed ATTRIBUTE_UNUSED)
1640 {
1641   return riscv_address_insns (addr, mode, false);
1642 }
1643 
1644 /* Return one word of double-word value OP.  HIGH_P is true to select the
1645    high part or false to select the low part. */
1646 
1647 rtx
1648 riscv_subword (rtx op, bool high_p)
1649 {
1650   unsigned int byte;
1651   enum machine_mode mode;
1652 
1653   mode = GET_MODE (op);
1654   if (mode == VOIDmode)
1655     mode = TARGET_64BIT ? TImode : DImode;
1656 
1657   byte = high_p ? UNITS_PER_WORD : 0;
1658 
1659   if (FP_REG_RTX_P (op))
1660     return gen_rtx_REG (word_mode, REGNO (op) + high_p);
1661 
1662   if (MEM_P (op))
1663     return adjust_address (op, word_mode, byte);
1664 
1665   return simplify_gen_subreg (word_mode, op, mode, byte);
1666 }
1667 
1668 /* Return true if a 64-bit move from SRC to DEST should be split into two.  */
1669 
1670 bool
1671 riscv_split_64bit_move_p (rtx dest, rtx src)
1672 {
1673   /* All 64b moves are legal in 64b mode.  All 64b FPR <-> FPR and
1674      FPR <-> MEM moves are legal in 32b mode, too.  Although
1675      FPR <-> GPR moves are not available in general in 32b mode,
1676      we can at least load 0 into an FPR with fcvt.d.w fpr, x0. */
1677   return !(TARGET_64BIT
1678 	   || (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
1679 	   || (FP_REG_RTX_P (dest) && MEM_P (src))
1680 	   || (FP_REG_RTX_P (src) && MEM_P (dest))
1681 	   || (FP_REG_RTX_P(dest) && src == CONST0_RTX(GET_MODE(src))));
1682 }
1683 
1684 /* Split a doubleword move from SRC to DEST.  On 32-bit targets,
1685    this function handles 64-bit moves for which riscv_split_64bit_move_p
1686    holds.  For 64-bit targets, this function handles 128-bit moves.  */
1687 
1688 void
1689 riscv_split_doubleword_move (rtx dest, rtx src)
1690 {
1691   rtx low_dest;
1692 
1693    /* The operation can be split into two normal moves.  Decide in
1694       which order to do them.  */
1695    low_dest = riscv_subword (dest, false);
1696    if (REG_P (low_dest) && reg_overlap_mentioned_p (low_dest, src))
1697      {
1698        riscv_emit_move (riscv_subword (dest, true), riscv_subword (src, true));
1699        riscv_emit_move (low_dest, riscv_subword (src, false));
1700      }
1701    else
1702      {
1703        riscv_emit_move (low_dest, riscv_subword (src, false));
1704        riscv_emit_move (riscv_subword (dest, true), riscv_subword (src, true));
1705      }
1706 }
1707 
1708 /* Return the appropriate instructions to move SRC into DEST.  Assume
1709    that SRC is operand 1 and DEST is operand 0.  */
1710 
1711 const char *
1712 riscv_output_move (rtx dest, rtx src)
1713 {
1714   enum rtx_code dest_code, src_code;
1715   enum machine_mode mode;
1716   bool dbl_p;
1717 
1718   dest_code = GET_CODE (dest);
1719   src_code = GET_CODE (src);
1720   mode = GET_MODE (dest);
1721   dbl_p = (GET_MODE_SIZE (mode) == 8);
1722 
1723   if (dbl_p && riscv_split_64bit_move_p (dest, src))
1724     return "#";
1725 
1726   if ((src_code == REG && GP_REG_P (REGNO (src)))
1727       || (src == CONST0_RTX (mode)))
1728     {
1729       if (dest_code == REG)
1730 	{
1731 	  if (GP_REG_P (REGNO (dest)))
1732 	    return "mv\t%0,%z1";
1733 
1734 	  if (FP_REG_P (REGNO (dest)))
1735 	    {
1736 	      if (!dbl_p)
1737 		return "fmv.s.x\t%0,%z1";
1738 	      if (TARGET_64BIT)
1739 		return "fmv.d.x\t%0,%z1";
1740 	      /* in RV32, we can emulate fmv.d.x %0, x0 using fcvt.d.w */
1741 	      gcc_assert (src == CONST0_RTX (mode));
1742 	      return "fcvt.d.w\t%0,x0";
1743 	    }
1744 	}
1745       if (dest_code == MEM)
1746 	switch (GET_MODE_SIZE (mode))
1747 	  {
1748 	  case 1: return "sb\t%z1,%0";
1749 	  case 2: return "sh\t%z1,%0";
1750 	  case 4: return "sw\t%z1,%0";
1751 	  case 8: return "sd\t%z1,%0";
1752 	  }
1753     }
1754   if (dest_code == REG && GP_REG_P (REGNO (dest)))
1755     {
1756       if (src_code == REG)
1757 	{
1758 	  if (FP_REG_P (REGNO (src)))
1759 	    return dbl_p ? "fmv.x.d\t%0,%1" : "fmv.x.s\t%0,%1";
1760 	}
1761 
1762       if (src_code == MEM)
1763 	switch (GET_MODE_SIZE (mode))
1764 	  {
1765 	  case 1: return "lbu\t%0,%1";
1766 	  case 2: return "lhu\t%0,%1";
1767 	  case 4: return "lw\t%0,%1";
1768 	  case 8: return "ld\t%0,%1";
1769 	  }
1770 
1771       if (src_code == CONST_INT)
1772 	return "li\t%0,%1";
1773 
1774       if (src_code == HIGH)
1775 	return "lui\t%0,%h1";
1776 
1777       if (symbolic_operand (src, VOIDmode))
1778 	switch (riscv_classify_symbolic_expression (src))
1779 	  {
1780 	  case SYMBOL_GOT_DISP: return "la\t%0,%1";
1781 	  case SYMBOL_ABSOLUTE: return "lla\t%0,%1";
1782 	  default: gcc_unreachable();
1783 	  }
1784     }
1785   if (src_code == REG && FP_REG_P (REGNO (src)))
1786     {
1787       if (dest_code == REG && FP_REG_P (REGNO (dest)))
1788 	return dbl_p ? "fmv.d\t%0,%1" : "fmv.s\t%0,%1";
1789 
1790       if (dest_code == MEM)
1791 	return dbl_p ? "fsd\t%1,%0" : "fsw\t%1,%0";
1792     }
1793   if (dest_code == REG && FP_REG_P (REGNO (dest)))
1794     {
1795       if (src_code == MEM)
1796 	return dbl_p ? "fld\t%0,%1" : "flw\t%0,%1";
1797     }
1798   gcc_unreachable ();
1799 }
1800 
1801 /* Return true if CMP1 is a suitable second operand for integer ordering
1802    test CODE.  See also the *sCC patterns in riscv.md.  */
1803 
1804 static bool
1805 riscv_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
1806 {
1807   switch (code)
1808     {
1809     case GT:
1810     case GTU:
1811       return reg_or_0_operand (cmp1, VOIDmode);
1812 
1813     case GE:
1814     case GEU:
1815       return cmp1 == const1_rtx;
1816 
1817     case LT:
1818     case LTU:
1819       return arith_operand (cmp1, VOIDmode);
1820 
1821     case LE:
1822       return sle_operand (cmp1, VOIDmode);
1823 
1824     case LEU:
1825       return sleu_operand (cmp1, VOIDmode);
1826 
1827     default:
1828       gcc_unreachable ();
1829     }
1830 }
1831 
1832 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
1833    integer ordering test *CODE, or if an equivalent combination can
1834    be formed by adjusting *CODE and *CMP1.  When returning true, update
1835    *CODE and *CMP1 with the chosen code and operand, otherwise leave
1836    them alone.  */
1837 
1838 static bool
1839 riscv_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
1840 				  enum machine_mode mode)
1841 {
1842   HOST_WIDE_INT plus_one;
1843 
1844   if (riscv_int_order_operand_ok_p (*code, *cmp1))
1845     return true;
1846 
1847   if (CONST_INT_P (*cmp1))
1848     switch (*code)
1849       {
1850       case LE:
1851 	plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
1852 	if (INTVAL (*cmp1) < plus_one)
1853 	  {
1854 	    *code = LT;
1855 	    *cmp1 = force_reg (mode, GEN_INT (plus_one));
1856 	    return true;
1857 	  }
1858 	break;
1859 
1860       case LEU:
1861 	plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
1862 	if (plus_one != 0)
1863 	  {
1864 	    *code = LTU;
1865 	    *cmp1 = force_reg (mode, GEN_INT (plus_one));
1866 	    return true;
1867 	  }
1868 	break;
1869 
1870       default:
1871 	break;
1872       }
1873   return false;
1874 }
1875 
1876 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
1877    in TARGET.  CMP0 and TARGET are register_operands.  If INVERT_PTR
1878    is nonnull, it's OK to set TARGET to the inverse of the result and
1879    flip *INVERT_PTR instead.  */
1880 
1881 static void
1882 riscv_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
1883 			  rtx target, rtx cmp0, rtx cmp1)
1884 {
1885   enum machine_mode mode;
1886 
1887   /* First see if there is a RISCV instruction that can do this operation.
1888      If not, try doing the same for the inverse operation.  If that also
1889      fails, force CMP1 into a register and try again.  */
1890   mode = GET_MODE (cmp0);
1891   if (riscv_canonicalize_int_order_test (&code, &cmp1, mode))
1892     riscv_emit_binary (code, target, cmp0, cmp1);
1893   else
1894     {
1895       enum rtx_code inv_code = reverse_condition (code);
1896       if (!riscv_canonicalize_int_order_test (&inv_code, &cmp1, mode))
1897 	{
1898 	  cmp1 = force_reg (mode, cmp1);
1899 	  riscv_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
1900 	}
1901       else if (invert_ptr == 0)
1902 	{
1903 	  rtx inv_target;
1904 
1905 	  inv_target = riscv_force_binary (GET_MODE (target),
1906 					  inv_code, cmp0, cmp1);
1907 	  riscv_emit_binary (XOR, target, inv_target, const1_rtx);
1908 	}
1909       else
1910 	{
1911 	  *invert_ptr = !*invert_ptr;
1912 	  riscv_emit_binary (inv_code, target, cmp0, cmp1);
1913 	}
1914     }
1915 }
1916 
1917 /* Return a register that is zero iff CMP0 and CMP1 are equal.
1918    The register will have the same mode as CMP0.  */
1919 
1920 static rtx
1921 riscv_zero_if_equal (rtx cmp0, rtx cmp1)
1922 {
1923   if (cmp1 == const0_rtx)
1924     return cmp0;
1925 
1926   return expand_binop (GET_MODE (cmp0), sub_optab,
1927 		       cmp0, cmp1, 0, 0, OPTAB_DIRECT);
1928 }
1929 
1930 /* Return false if we can easily emit code for the FP comparison specified
1931    by *CODE.  If not, set *CODE to its inverse and return true. */
1932 
1933 static bool
1934 riscv_reversed_fp_cond (enum rtx_code *code)
1935 {
1936   switch (*code)
1937     {
1938     case EQ:
1939     case LT:
1940     case LE:
1941     case GT:
1942     case GE:
1943     case LTGT:
1944     case ORDERED:
1945       /* We know how to emit code for these cases... */
1946       return false;
1947 
1948     default:
1949       /* ...but we must invert these and rely on the others. */
1950       *code = reverse_condition_maybe_unordered (*code);
1951       return true;
1952     }
1953 }
1954 
1955 /* Convert a comparison into something that can be used in a branch or
1956    conditional move.  On entry, *OP0 and *OP1 are the values being
1957    compared and *CODE is the code used to compare them.
1958 
1959    Update *CODE, *OP0 and *OP1 so that they describe the final comparison. */
1960 
1961 static void
1962 riscv_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1)
1963 {
1964   rtx cmp_op0 = *op0;
1965   rtx cmp_op1 = *op1;
1966 
1967   if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
1968     {
1969       if (splittable_const_int_operand (cmp_op1, VOIDmode))
1970 	{
1971 	  HOST_WIDE_INT rhs = INTVAL (cmp_op1), new_rhs;
1972 	  enum rtx_code new_code;
1973 
1974 	  switch (*code)
1975 	    {
1976 	    case LTU: new_rhs = rhs - 1; new_code = LEU; goto try_new_rhs;
1977 	    case LEU: new_rhs = rhs + 1; new_code = LTU; goto try_new_rhs;
1978 	    case GTU: new_rhs = rhs + 1; new_code = GEU; goto try_new_rhs;
1979 	    case GEU: new_rhs = rhs - 1; new_code = GTU; goto try_new_rhs;
1980 	    case LT: new_rhs = rhs - 1; new_code = LE; goto try_new_rhs;
1981 	    case LE: new_rhs = rhs + 1; new_code = LT; goto try_new_rhs;
1982 	    case GT: new_rhs = rhs + 1; new_code = GE; goto try_new_rhs;
1983 	    case GE: new_rhs = rhs - 1; new_code = GT;
1984 	    try_new_rhs:
1985 	      /* Convert e.g. OP0 > 4095 into OP0 >= 4096.  */
1986 	      if ((rhs < 0) == (new_rhs < 0)
1987 		  && riscv_integer_cost (new_rhs) < riscv_integer_cost (rhs))
1988 		{
1989 		  *op1 = GEN_INT (new_rhs);
1990 		  *code = new_code;
1991 		}
1992 	      break;
1993 
1994 	    case EQ:
1995 	    case NE:
1996 	      /* Convert e.g. OP0 == 2048 into OP0 - 2048 == 0.  */
1997 	      if (SMALL_OPERAND (-rhs))
1998 		{
1999 		  *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
2000 		  riscv_emit_binary (PLUS, *op0, cmp_op0, GEN_INT (-rhs));
2001 		  *op1 = const0_rtx;
2002 		}
2003 	    default:
2004 	      break;
2005 	    }
2006 	}
2007 
2008       if (*op1 != const0_rtx)
2009 	*op1 = force_reg (GET_MODE (cmp_op0), *op1);
2010     }
2011   else
2012     {
2013       /* For FP comparisons, set an integer register with the result of the
2014 	 comparison, then branch on it. */
2015       rtx tmp0, tmp1, final_op;
2016       enum rtx_code fp_code = *code;
2017       *code = riscv_reversed_fp_cond (&fp_code) ? EQ : NE;
2018 
2019       switch (fp_code)
2020 	{
2021 	case ORDERED:
2022 	  /* a == a && b == b */
2023 	  tmp0 = gen_reg_rtx (SImode);
2024 	  riscv_emit_binary (EQ, tmp0, cmp_op0, cmp_op0);
2025 	  tmp1 = gen_reg_rtx (SImode);
2026 	  riscv_emit_binary (EQ, tmp1, cmp_op1, cmp_op1);
2027 	  final_op = gen_reg_rtx (SImode);
2028 	  riscv_emit_binary (AND, final_op, tmp0, tmp1);
2029 	  break;
2030 
2031 	case LTGT:
2032 	  /* a < b || a > b */
2033 	  tmp0 = gen_reg_rtx (SImode);
2034 	  riscv_emit_binary (LT, tmp0, cmp_op0, cmp_op1);
2035 	  tmp1 = gen_reg_rtx (SImode);
2036 	  riscv_emit_binary (GT, tmp1, cmp_op0, cmp_op1);
2037 	  final_op = gen_reg_rtx (SImode);
2038 	  riscv_emit_binary (IOR, final_op, tmp0, tmp1);
2039 	  break;
2040 
2041 	case EQ:
2042 	case LE:
2043 	case LT:
2044 	case GE:
2045 	case GT:
2046 	  /* We have instructions for these cases. */
2047 	  final_op = gen_reg_rtx (SImode);
2048 	  riscv_emit_binary (fp_code, final_op, cmp_op0, cmp_op1);
2049 	  break;
2050 
2051 	default:
2052 	  gcc_unreachable ();
2053 	}
2054 
2055       /* Compare the binary result against 0. */
2056       *op0 = final_op;
2057       *op1 = const0_rtx;
2058     }
2059 }
2060 
2061 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
2062    and OPERAND[3].  Store the result in OPERANDS[0].
2063 
2064    On 64-bit targets, the mode of the comparison and target will always be
2065    SImode, thus possibly narrower than that of the comparison's operands.  */
2066 
2067 void
2068 riscv_expand_scc (rtx operands[])
2069 {
2070   rtx target = operands[0];
2071   enum rtx_code code = GET_CODE (operands[1]);
2072   rtx op0 = operands[2];
2073   rtx op1 = operands[3];
2074 
2075   gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
2076 
2077   if (code == EQ || code == NE)
2078     {
2079       rtx zie = riscv_zero_if_equal (op0, op1);
2080       riscv_emit_binary (code, target, zie, const0_rtx);
2081     }
2082   else
2083     riscv_emit_int_order_test (code, 0, target, op0, op1);
2084 }
2085 
2086 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
2087    CODE and jump to OPERANDS[3] if the condition holds.  */
2088 
2089 void
2090 riscv_expand_conditional_branch (rtx *operands)
2091 {
2092   enum rtx_code code = GET_CODE (operands[0]);
2093   rtx op0 = operands[1];
2094   rtx op1 = operands[2];
2095   rtx condition;
2096 
2097   riscv_emit_compare (&code, &op0, &op1);
2098   condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
2099   emit_jump_insn (gen_condjump (condition, operands[3]));
2100 }
2101 
2102 /* Implement TARGET_FUNCTION_ARG_BOUNDARY.  Every parameter gets at
2103    least PARM_BOUNDARY bits of alignment, but will be given anything up
2104    to STACK_BOUNDARY bits if the type requires it.  */
2105 
2106 static unsigned int
2107 riscv_function_arg_boundary (enum machine_mode mode, const_tree type)
2108 {
2109   unsigned int alignment;
2110 
2111   alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
2112   if (alignment < PARM_BOUNDARY)
2113     alignment = PARM_BOUNDARY;
2114   if (alignment > STACK_BOUNDARY)
2115     alignment = STACK_BOUNDARY;
2116   return alignment;
2117 }
2118 
2119 /* Fill INFO with information about a single argument.  CUM is the
2120    cumulative state for earlier arguments.  MODE is the mode of this
2121    argument and TYPE is its type (if known).  NAMED is true if this
2122    is a named (fixed) argument rather than a variable one.  */
2123 
2124 static void
2125 riscv_get_arg_info (struct riscv_arg_info *info, const CUMULATIVE_ARGS *cum,
2126 		   enum machine_mode mode, const_tree type, bool named)
2127 {
2128   bool doubleword_aligned_p;
2129   unsigned int num_bytes, num_words, max_regs;
2130 
2131   /* Work out the size of the argument.  */
2132   num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
2133   num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2134 
2135   /* Scalar, complex and vector floating-point types are passed in
2136      floating-point registers, as long as this is a named rather
2137      than a variable argument.  */
2138   info->fpr_p = (named
2139 		 && (type == 0 || FLOAT_TYPE_P (type))
2140 		 && (GET_MODE_CLASS (mode) == MODE_FLOAT
2141 		     || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
2142 		     || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
2143 		 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
2144 
2145   /* Complex floats should only go into FPRs if there are two FPRs free,
2146      otherwise they should be passed in the same way as a struct
2147      containing two floats.  */
2148   if (info->fpr_p
2149       && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
2150       && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
2151     {
2152       if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
2153         info->fpr_p = false;
2154       else
2155         num_words = 2;
2156     }
2157 
2158   /* See whether the argument has doubleword alignment.  */
2159   doubleword_aligned_p = (riscv_function_arg_boundary (mode, type)
2160 			  > BITS_PER_WORD);
2161 
2162   /* Set REG_OFFSET to the register count we're interested in.
2163      The EABI allocates the floating-point registers separately,
2164      but the other ABIs allocate them like integer registers.  */
2165   info->reg_offset = cum->num_gprs;
2166 
2167   /* Advance to an even register if the argument is doubleword-aligned.  */
2168   if (doubleword_aligned_p)
2169     info->reg_offset += info->reg_offset & 1;
2170 
2171   /* Work out the offset of a stack argument.  */
2172   info->stack_offset = cum->stack_words;
2173   if (doubleword_aligned_p)
2174     info->stack_offset += info->stack_offset & 1;
2175 
2176   max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
2177 
2178   /* Partition the argument between registers and stack.  */
2179   info->reg_words = MIN (num_words, max_regs);
2180   info->stack_words = num_words - info->reg_words;
2181 }
2182 
2183 /* INFO describes a register argument that has the normal format for the
2184    argument's mode.  Return the register it uses, assuming that FPRs are
2185    available if HARD_FLOAT_P.  */
2186 
2187 static unsigned int
2188 riscv_arg_regno (const struct riscv_arg_info *info, bool hard_float_p)
2189 {
2190   if (!info->fpr_p || !hard_float_p)
2191     return GP_ARG_FIRST + info->reg_offset;
2192   else
2193     return FP_ARG_FIRST + info->reg_offset;
2194 }
2195 
2196 /* Implement TARGET_FUNCTION_ARG.  */
2197 
2198 static rtx
2199 riscv_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
2200 		    const_tree type, bool named)
2201 {
2202   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2203   struct riscv_arg_info info;
2204 
2205   if (mode == VOIDmode)
2206     return NULL;
2207 
2208   riscv_get_arg_info (&info, cum, mode, type, named);
2209 
2210   /* Return straight away if the whole argument is passed on the stack.  */
2211   if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
2212     return NULL;
2213 
2214   /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
2215      contains a double in its entirety, then that 64-bit chunk is passed
2216      in a floating-point register.  */
2217   if (TARGET_HARD_FLOAT
2218       && named
2219       && type != 0
2220       && TREE_CODE (type) == RECORD_TYPE
2221       && TYPE_SIZE_UNIT (type)
2222       && host_integerp (TYPE_SIZE_UNIT (type), 1))
2223       //&& tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
2224     {
2225       tree field;
2226 
2227       /* First check to see if there is any such field.  */
2228       for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2229 	if (TREE_CODE (field) == FIELD_DECL
2230 	    && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
2231 	    && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
2232 	    && host_integerp (bit_position (field), 0)
2233 	    //&& tree_fits_shwi_p (bit_position (field))
2234 	    && int_bit_position (field) % BITS_PER_WORD == 0)
2235 	  break;
2236 
2237       if (field != 0)
2238 	{
2239 	  /* Now handle the special case by returning a PARALLEL
2240 	     indicating where each 64-bit chunk goes.  INFO.REG_WORDS
2241 	     chunks are passed in registers.  */
2242 	  unsigned int i;
2243 	  HOST_WIDE_INT bitpos;
2244 	  rtx ret;
2245 
2246 	  /* assign_parms checks the mode of ENTRY_PARM, so we must
2247 	     use the actual mode here.  */
2248 	  ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
2249 
2250 	  bitpos = 0;
2251 	  field = TYPE_FIELDS (type);
2252 	  for (i = 0; i < info.reg_words; i++)
2253 	    {
2254 	      rtx reg;
2255 
2256 	      for (; field; field = DECL_CHAIN (field))
2257 		if (TREE_CODE (field) == FIELD_DECL
2258 		    && int_bit_position (field) >= bitpos)
2259 		  break;
2260 
2261 	      if (field
2262 		  && int_bit_position (field) == bitpos
2263 		  && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
2264 		  && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
2265 		reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
2266 	      else
2267 		reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
2268 
2269 	      XVECEXP (ret, 0, i)
2270 		= gen_rtx_EXPR_LIST (VOIDmode, reg,
2271 				     GEN_INT (bitpos / BITS_PER_UNIT));
2272 
2273 	      bitpos += BITS_PER_WORD;
2274 	    }
2275 	  return ret;
2276 	}
2277     }
2278 
2279   /* Handle the n32/n64 conventions for passing complex floating-point
2280      arguments in FPR pairs.  The real part goes in the lower register
2281      and the imaginary part goes in the upper register.  */
2282   if (info.fpr_p
2283       && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
2284     {
2285       rtx real, imag;
2286       enum machine_mode inner;
2287       unsigned int regno;
2288 
2289       inner = GET_MODE_INNER (mode);
2290       regno = FP_ARG_FIRST + info.reg_offset;
2291       if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
2292 	{
2293 	  /* Real part in registers, imaginary part on stack.  */
2294 	  gcc_assert (info.stack_words == info.reg_words);
2295 	  return gen_rtx_REG (inner, regno);
2296 	}
2297       else
2298 	{
2299 	  gcc_assert (info.stack_words == 0);
2300 	  real = gen_rtx_EXPR_LIST (VOIDmode,
2301 				    gen_rtx_REG (inner, regno),
2302 				    const0_rtx);
2303 	  imag = gen_rtx_EXPR_LIST (VOIDmode,
2304 				    gen_rtx_REG (inner,
2305 						 regno + info.reg_words / 2),
2306 				    GEN_INT (GET_MODE_SIZE (inner)));
2307 	  return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
2308 	}
2309     }
2310 
2311   return gen_rtx_REG (mode, riscv_arg_regno (&info, TARGET_HARD_FLOAT));
2312 }
2313 
2314 /* Implement TARGET_FUNCTION_ARG_ADVANCE.  */
2315 
2316 static void
2317 riscv_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
2318 			    const_tree type, bool named)
2319 {
2320   CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2321   struct riscv_arg_info info;
2322 
2323   riscv_get_arg_info (&info, cum, mode, type, named);
2324 
2325   /* Advance the register count.  This has the effect of setting
2326      num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
2327      argument required us to skip the final GPR and pass the whole
2328      argument on the stack.  */
2329   cum->num_gprs = info.reg_offset + info.reg_words;
2330 
2331   /* Advance the stack word count.  */
2332   if (info.stack_words > 0)
2333     cum->stack_words = info.stack_offset + info.stack_words;
2334 }
2335 
2336 /* Implement TARGET_ARG_PARTIAL_BYTES.  */
2337 
2338 static int
2339 riscv_arg_partial_bytes (cumulative_args_t cum,
2340 			 enum machine_mode mode, tree type, bool named)
2341 {
2342   struct riscv_arg_info info;
2343 
2344   riscv_get_arg_info (&info, get_cumulative_args (cum), mode, type, named);
2345   return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
2346 }
2347 
2348 /* See whether VALTYPE is a record whose fields should be returned in
2349    floating-point registers.  If so, return the number of fields and
2350    list them in FIELDS (which should have two elements).  Return 0
2351    otherwise.
2352 
2353    For n32 & n64, a structure with one or two fields is returned in
2354    floating-point registers as long as every field has a floating-point
2355    type.  */
2356 
2357 static int
2358 riscv_fpr_return_fields (const_tree valtype, tree *fields)
2359 {
2360   tree field;
2361   int i;
2362 
2363   if (TREE_CODE (valtype) != RECORD_TYPE)
2364     return 0;
2365 
2366   i = 0;
2367   for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
2368     {
2369       if (TREE_CODE (field) != FIELD_DECL)
2370 	continue;
2371 
2372       if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
2373 	return 0;
2374 
2375       if (i == 2)
2376 	return 0;
2377 
2378       fields[i++] = field;
2379     }
2380   return i;
2381 }
2382 
2383 /* Return true if the function return value MODE will get returned in a
2384    floating-point register.  */
2385 
2386 static bool
2387 riscv_return_mode_in_fpr_p (enum machine_mode mode)
2388 {
2389   return ((GET_MODE_CLASS (mode) == MODE_FLOAT
2390 	   || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
2391 	   || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
2392 	  && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
2393 }
2394 
2395 /* Return the representation of an FPR return register when the
2396    value being returned in FP_RETURN has mode VALUE_MODE and the
2397    return type itself has mode TYPE_MODE.  On NewABI targets,
2398    the two modes may be different for structures like:
2399 
2400        struct __attribute__((packed)) foo { float f; }
2401 
2402    where we return the SFmode value of "f" in FP_RETURN, but where
2403    the structure itself has mode BLKmode.  */
2404 
2405 static rtx
2406 riscv_return_fpr_single (enum machine_mode type_mode,
2407 			enum machine_mode value_mode)
2408 {
2409   rtx x;
2410 
2411   x = gen_rtx_REG (value_mode, FP_RETURN);
2412   if (type_mode != value_mode)
2413     {
2414       x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
2415       x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
2416     }
2417   return x;
2418 }
2419 
2420 /* Return a composite value in a pair of floating-point registers.
2421    MODE1 and OFFSET1 are the mode and byte offset for the first value,
2422    likewise MODE2 and OFFSET2 for the second.  MODE is the mode of the
2423    complete value.
2424 
2425    For n32 & n64, $f0 always holds the first value and $f2 the second.
2426    Otherwise the values are packed together as closely as possible.  */
2427 
2428 static rtx
2429 riscv_return_fpr_pair (enum machine_mode mode,
2430 		      enum machine_mode mode1, HOST_WIDE_INT offset1,
2431 		      enum machine_mode mode2, HOST_WIDE_INT offset2)
2432 {
2433   return gen_rtx_PARALLEL
2434     (mode,
2435      gen_rtvec (2,
2436 		gen_rtx_EXPR_LIST (VOIDmode,
2437 				   gen_rtx_REG (mode1, FP_RETURN),
2438 				   GEN_INT (offset1)),
2439 		gen_rtx_EXPR_LIST (VOIDmode,
2440 				   gen_rtx_REG (mode2, FP_RETURN + 1),
2441 				   GEN_INT (offset2))));
2442 
2443 }
2444 
2445 /* Implement FUNCTION_VALUE and LIBCALL_VALUE.  For normal calls,
2446    VALTYPE is the return type and MODE is VOIDmode.  For libcalls,
2447    VALTYPE is null and MODE is the mode of the return value.  */
2448 
2449 rtx
2450 riscv_function_value (const_tree valtype, const_tree func, enum machine_mode mode)
2451 {
2452   if (valtype)
2453     {
2454       tree fields[2];
2455       int unsigned_p;
2456 
2457       mode = TYPE_MODE (valtype);
2458       unsigned_p = TYPE_UNSIGNED (valtype);
2459 
2460       /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
2461 	 return values, promote the mode here too.  */
2462       mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
2463 
2464       /* Handle structures whose fields are returned in $f0/$f2.  */
2465       switch (riscv_fpr_return_fields (valtype, fields))
2466 	{
2467 	case 1:
2468 	  return riscv_return_fpr_single (mode,
2469 					 TYPE_MODE (TREE_TYPE (fields[0])));
2470 
2471 	case 2:
2472 	  return riscv_return_fpr_pair (mode,
2473 				       TYPE_MODE (TREE_TYPE (fields[0])),
2474 				       int_byte_position (fields[0]),
2475 				       TYPE_MODE (TREE_TYPE (fields[1])),
2476 				       int_byte_position (fields[1]));
2477 	}
2478 
2479       /* Only use FPRs for scalar, complex or vector types.  */
2480       if (!FLOAT_TYPE_P (valtype))
2481 	return gen_rtx_REG (mode, GP_RETURN);
2482     }
2483 
2484   /* Handle long doubles for n32 & n64.  */
2485   if (mode == TFmode)
2486     return riscv_return_fpr_pair (mode,
2487     			     DImode, 0,
2488     			     DImode, GET_MODE_SIZE (mode) / 2);
2489 
2490   if (riscv_return_mode_in_fpr_p (mode))
2491     {
2492       if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
2493         return riscv_return_fpr_pair (mode,
2494     				 GET_MODE_INNER (mode), 0,
2495     				 GET_MODE_INNER (mode),
2496     				 GET_MODE_SIZE (mode) / 2);
2497       else
2498         return gen_rtx_REG (mode, FP_RETURN);
2499     }
2500 
2501   return gen_rtx_REG (mode, GP_RETURN);
2502 }
2503 
2504 /* Implement TARGET_RETURN_IN_MEMORY.  Scalars and small structures
2505    that fit in two registers are returned in a0/a1. */
2506 
2507 static bool
2508 riscv_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
2509 {
2510   return !IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD);
2511 }
2512 
2513 /* Implement TARGET_PASS_BY_REFERENCE. */
2514 
2515 static bool
2516 riscv_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
2517 			 enum machine_mode mode, const_tree type,
2518 			 bool named ATTRIBUTE_UNUSED)
2519 {
2520   if (type && riscv_return_in_memory (type, NULL_TREE))
2521     return true;
2522   return targetm.calls.must_pass_in_stack (mode, type);
2523 }
2524 
2525 /* Implement TARGET_SETUP_INCOMING_VARARGS.  */
2526 
2527 static void
2528 riscv_setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
2529 			     tree type, int *pretend_size ATTRIBUTE_UNUSED,
2530 			     int no_rtl)
2531 {
2532   CUMULATIVE_ARGS local_cum;
2533   int gp_saved;
2534 
2535   /* The caller has advanced CUM up to, but not beyond, the last named
2536      argument.  Advance a local copy of CUM past the last "real" named
2537      argument, to find out how many registers are left over.  */
2538   local_cum = *get_cumulative_args (cum);
2539   riscv_function_arg_advance (pack_cumulative_args (&local_cum), mode, type, 1);
2540 
2541   /* Found out how many registers we need to save.  */
2542   gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
2543 
2544   if (!no_rtl && gp_saved > 0)
2545     {
2546       rtx ptr, mem;
2547 
2548       ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
2549 			   REG_PARM_STACK_SPACE (cfun->decl)
2550 			   - gp_saved * UNITS_PER_WORD);
2551       mem = gen_frame_mem (BLKmode, ptr);
2552       set_mem_alias_set (mem, get_varargs_alias_set ());
2553 
2554       move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
2555 			   mem, gp_saved);
2556     }
2557   if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
2558     cfun->machine->varargs_size = gp_saved * UNITS_PER_WORD;
2559 }
2560 
2561 /* Implement TARGET_EXPAND_BUILTIN_VA_START.  */
2562 
2563 static void
2564 riscv_va_start (tree valist, rtx nextarg)
2565 {
2566   nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
2567   std_expand_builtin_va_start (valist, nextarg);
2568 }
2569 
2570 /* Expand a call of type TYPE.  RESULT is where the result will go (null
2571    for "call"s and "sibcall"s), ADDR is the address of the function,
2572    ARGS_SIZE is the size of the arguments and AUX is the value passed
2573    to us by riscv_function_arg.  Return the call itself.  */
2574 
2575 rtx
2576 riscv_expand_call (bool sibcall_p, rtx result, rtx addr, rtx args_size)
2577 {
2578   rtx pattern;
2579 
2580   if (!call_insn_operand (addr, VOIDmode))
2581     {
2582       rtx reg = RISCV_EPILOGUE_TEMP (Pmode);
2583       riscv_emit_move (reg, addr);
2584       addr = reg;
2585     }
2586 
2587   if (result == 0)
2588     {
2589       rtx (*fn) (rtx, rtx);
2590 
2591       if (sibcall_p)
2592 	fn = gen_sibcall_internal;
2593       else
2594 	fn = gen_call_internal;
2595 
2596       pattern = fn (addr, args_size);
2597     }
2598   else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
2599     {
2600       /* Handle return values created by riscv_return_fpr_pair.  */
2601       rtx (*fn) (rtx, rtx, rtx, rtx);
2602       rtx reg1, reg2;
2603 
2604       if (sibcall_p)
2605 	fn = gen_sibcall_value_multiple_internal;
2606       else
2607 	fn = gen_call_value_multiple_internal;
2608 
2609       reg1 = XEXP (XVECEXP (result, 0, 0), 0);
2610       reg2 = XEXP (XVECEXP (result, 0, 1), 0);
2611       pattern = fn (reg1, addr, args_size, reg2);
2612     }
2613   else
2614     {
2615       rtx (*fn) (rtx, rtx, rtx);
2616 
2617       if (sibcall_p)
2618 	fn = gen_sibcall_value_internal;
2619       else
2620 	fn = gen_call_value_internal;
2621 
2622       /* Handle return values created by riscv_return_fpr_single.  */
2623       if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
2624 	result = XEXP (XVECEXP (result, 0, 0), 0);
2625       pattern = fn (result, addr, args_size);
2626     }
2627 
2628   return emit_call_insn (pattern);
2629 }
2630 
2631 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
2632    Assume that the areas do not overlap.  */
2633 
2634 static void
2635 riscv_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
2636 {
2637   HOST_WIDE_INT offset, delta;
2638   unsigned HOST_WIDE_INT bits;
2639   int i;
2640   enum machine_mode mode;
2641   rtx *regs;
2642 
2643   bits = MAX( BITS_PER_UNIT,
2644              MIN( BITS_PER_WORD, MIN( MEM_ALIGN(src),MEM_ALIGN(dest) ) ) );
2645 
2646   mode = mode_for_size (bits, MODE_INT, 0);
2647   delta = bits / BITS_PER_UNIT;
2648 
2649   /* Allocate a buffer for the temporary registers.  */
2650   regs = XALLOCAVEC (rtx, length / delta);
2651 
2652   /* Load as many BITS-sized chunks as possible.  Use a normal load if
2653      the source has enough alignment, otherwise use left/right pairs.  */
2654   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
2655     {
2656       regs[i] = gen_reg_rtx (mode);
2657 	riscv_emit_move (regs[i], adjust_address (src, mode, offset));
2658     }
2659 
2660   /* Copy the chunks to the destination.  */
2661   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
2662       riscv_emit_move (adjust_address (dest, mode, offset), regs[i]);
2663 
2664   /* Mop up any left-over bytes.  */
2665   if (offset < length)
2666     {
2667       src = adjust_address (src, BLKmode, offset);
2668       dest = adjust_address (dest, BLKmode, offset);
2669       move_by_pieces (dest, src, length - offset,
2670 		      MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
2671     }
2672 }
2673 
2674 /* Helper function for doing a loop-based block operation on memory
2675    reference MEM.  Each iteration of the loop will operate on LENGTH
2676    bytes of MEM.
2677 
2678    Create a new base register for use within the loop and point it to
2679    the start of MEM.  Create a new memory reference that uses this
2680    register.  Store them in *LOOP_REG and *LOOP_MEM respectively.  */
2681 
2682 static void
2683 riscv_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
2684 		       rtx *loop_reg, rtx *loop_mem)
2685 {
2686   *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
2687 
2688   /* Although the new mem does not refer to a known location,
2689      it does keep up to LENGTH bytes of alignment.  */
2690   *loop_mem = change_address (mem, BLKmode, *loop_reg);
2691   set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
2692 }
2693 
2694 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
2695    bytes at a time.  LENGTH must be at least BYTES_PER_ITER.  Assume that
2696    the memory regions do not overlap.  */
2697 
2698 static void
2699 riscv_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
2700 		      HOST_WIDE_INT bytes_per_iter)
2701 {
2702   rtx label, src_reg, dest_reg, final_src, test;
2703   HOST_WIDE_INT leftover;
2704 
2705   leftover = length % bytes_per_iter;
2706   length -= leftover;
2707 
2708   /* Create registers and memory references for use within the loop.  */
2709   riscv_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
2710   riscv_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
2711 
2712   /* Calculate the value that SRC_REG should have after the last iteration
2713      of the loop.  */
2714   final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
2715 				   0, 0, OPTAB_WIDEN);
2716 
2717   /* Emit the start of the loop.  */
2718   label = gen_label_rtx ();
2719   emit_label (label);
2720 
2721   /* Emit the loop body.  */
2722   riscv_block_move_straight (dest, src, bytes_per_iter);
2723 
2724   /* Move on to the next block.  */
2725   riscv_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
2726   riscv_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
2727 
2728   /* Emit the loop condition.  */
2729   test = gen_rtx_NE (VOIDmode, src_reg, final_src);
2730   if (Pmode == DImode)
2731     emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
2732   else
2733     emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
2734 
2735   /* Mop up any left-over bytes.  */
2736   if (leftover)
2737     riscv_block_move_straight (dest, src, leftover);
2738 }
2739 
2740 /* Expand a movmemsi instruction, which copies LENGTH bytes from
2741    memory reference SRC to memory reference DEST.  */
2742 
2743 bool
2744 riscv_expand_block_move (rtx dest, rtx src, rtx length)
2745 {
2746   if (CONST_INT_P (length))
2747     {
2748       HOST_WIDE_INT factor, align;
2749 
2750       align = MIN (MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), BITS_PER_WORD);
2751       factor = BITS_PER_WORD / align;
2752 
2753       if (INTVAL (length) <= RISCV_MAX_MOVE_BYTES_STRAIGHT / factor)
2754 	{
2755 	  riscv_block_move_straight (dest, src, INTVAL (length));
2756 	  return true;
2757 	}
2758       else if (optimize && align >= BITS_PER_WORD)
2759 	{
2760 	  riscv_block_move_loop (dest, src, INTVAL (length),
2761 				RISCV_MAX_MOVE_BYTES_PER_LOOP_ITER / factor);
2762 	  return true;
2763 	}
2764     }
2765   return false;
2766 }
2767 
2768 /* (Re-)Initialize riscv_lo_relocs and riscv_hi_relocs.  */
2769 
2770 static void
2771 riscv_init_relocs (void)
2772 {
2773   memset (riscv_hi_relocs, '\0', sizeof (riscv_hi_relocs));
2774   memset (riscv_lo_relocs, '\0', sizeof (riscv_lo_relocs));
2775 
2776   if (!flag_pic && riscv_cmodel == CM_MEDLOW)
2777     {
2778       riscv_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
2779       riscv_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
2780     }
2781 
2782   if (!flag_pic || flag_pie)
2783     {
2784       riscv_hi_relocs[SYMBOL_TLS_LE] = "%tprel_hi(";
2785       riscv_lo_relocs[SYMBOL_TLS_LE] = "%tprel_lo(";
2786     }
2787 }
2788 
2789 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
2790    in context CONTEXT.  RELOCS is the array of relocations to use.  */
2791 
2792 static void
2793 riscv_print_operand_reloc (FILE *file, rtx op, const char **relocs)
2794 {
2795   enum riscv_symbol_type symbol_type;
2796   const char *p;
2797 
2798   symbol_type = riscv_classify_symbolic_expression (op);
2799   gcc_assert (relocs[symbol_type]);
2800 
2801   fputs (relocs[symbol_type], file);
2802   output_addr_const (file, riscv_strip_unspec_address (op));
2803   for (p = relocs[symbol_type]; *p != 0; p++)
2804     if (*p == '(')
2805       fputc (')', file);
2806 }
2807 
2808 static const char *
2809 riscv_memory_model_suffix (enum memmodel model)
2810 {
2811   switch (model)
2812     {
2813       case MEMMODEL_ACQ_REL:
2814       case MEMMODEL_SEQ_CST:
2815 	return ".sc";
2816       case MEMMODEL_ACQUIRE:
2817       case MEMMODEL_CONSUME:
2818 	return ".aq";
2819       case MEMMODEL_RELEASE:
2820 	return ".rl";
2821       case MEMMODEL_RELAXED:
2822 	return "";
2823       default: gcc_unreachable();
2824     }
2825 }
2826 
2827 /* Implement TARGET_PRINT_OPERAND.  The RISCV-specific operand codes are:
2828 
2829    'h'	Print the high-part relocation associated with OP, after stripping
2830 	  any outermost HIGH.
2831    'R'	Print the low-part relocation associated with OP.
2832    'C'	Print the integer branch condition for comparison OP.
2833    'A'	Print the atomic operation suffix for memory model OP.
2834    'z'	Print $0 if OP is zero, otherwise print OP normally.  */
2835 
2836 static void
2837 riscv_print_operand (FILE *file, rtx op, int letter)
2838 {
2839   enum rtx_code code;
2840 
2841   gcc_assert (op);
2842   code = GET_CODE (op);
2843 
2844   switch (letter)
2845     {
2846     case 'h':
2847       if (code == HIGH)
2848 	op = XEXP (op, 0);
2849       riscv_print_operand_reloc (file, op, riscv_hi_relocs);
2850       break;
2851 
2852     case 'R':
2853       riscv_print_operand_reloc (file, op, riscv_lo_relocs);
2854       break;
2855 
2856     case 'C':
2857       /* The RTL names match the instruction names. */
2858       fputs (GET_RTX_NAME (code), file);
2859       break;
2860 
2861     case 'A':
2862       fputs (riscv_memory_model_suffix ((enum memmodel)INTVAL (op)), file);
2863       break;
2864 
2865     default:
2866       switch (code)
2867 	{
2868 	case REG:
2869 	  if (letter && letter != 'z')
2870 	    output_operand_lossage ("invalid use of '%%%c'", letter);
2871 	  fprintf (file, "%s", reg_names[REGNO (op)]);
2872 	  break;
2873 
2874 	case MEM:
2875 	  if (letter == 'y')
2876 	    fprintf (file, "%s", reg_names[REGNO(XEXP(op, 0))]);
2877 	  else if (letter && letter != 'z')
2878 	    output_operand_lossage ("invalid use of '%%%c'", letter);
2879 	  else
2880 	    output_address (XEXP (op, 0));
2881 	  break;
2882 
2883 	default:
2884 	  if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
2885 	    fputs (reg_names[GP_REG_FIRST], file);
2886 	  else if (letter && letter != 'z')
2887 	    output_operand_lossage ("invalid use of '%%%c'", letter);
2888 	  else
2889 	    output_addr_const (file, riscv_strip_unspec_address (op));
2890 	  break;
2891 	}
2892     }
2893 }
2894 
2895 /* Implement TARGET_PRINT_OPERAND_ADDRESS.  */
2896 
2897 static void
2898 riscv_print_operand_address (FILE *file, rtx x)
2899 {
2900   struct riscv_address_info addr;
2901 
2902   if (riscv_classify_address (&addr, x, word_mode, true))
2903     switch (addr.type)
2904       {
2905       case ADDRESS_REG:
2906 	riscv_print_operand (file, addr.offset, 0);
2907 	fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
2908 	return;
2909 
2910       case ADDRESS_LO_SUM:
2911 	riscv_print_operand_reloc (file, addr.offset, riscv_lo_relocs);
2912 	fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
2913 	return;
2914 
2915       case ADDRESS_CONST_INT:
2916 	output_addr_const (file, x);
2917 	fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
2918 	return;
2919 
2920       case ADDRESS_SYMBOLIC:
2921 	output_addr_const (file, riscv_strip_unspec_address (x));
2922 	return;
2923       }
2924   gcc_unreachable ();
2925 }
2926 
2927 static bool
2928 riscv_size_ok_for_small_data_p (int size)
2929 {
2930   return g_switch_value && IN_RANGE (size, 1, g_switch_value);
2931 }
2932 
2933 /* Return true if EXP should be placed in the small data section. */
2934 
2935 static bool
2936 riscv_in_small_data_p (const_tree x)
2937 {
2938   if (TREE_CODE (x) == STRING_CST || TREE_CODE (x) == FUNCTION_DECL)
2939     return false;
2940 
2941   if (TREE_CODE (x) == VAR_DECL && DECL_SECTION_NAME (x))
2942     {
2943       const char *sec = TREE_STRING_POINTER (DECL_SECTION_NAME (x));
2944       return strcmp (sec, ".sdata") == 0 || strcmp (sec, ".sbss") == 0;
2945     }
2946 
2947   return riscv_size_ok_for_small_data_p (int_size_in_bytes (TREE_TYPE (x)));
2948 }
2949 
2950 /* Return a section for X, handling small data. */
2951 
2952 static section *
2953 riscv_elf_select_rtx_section (enum machine_mode mode, rtx x,
2954 			      unsigned HOST_WIDE_INT align)
2955 {
2956   section *s = default_elf_select_rtx_section (mode, x, align);
2957 
2958   if (riscv_size_ok_for_small_data_p (GET_MODE_SIZE (mode)))
2959     {
2960       if (strncmp (s->named.name, ".rodata.cst", strlen (".rodata.cst")) == 0)
2961 	{
2962 	  /* Rename .rodata.cst* to .srodata.cst*. */
2963 	  char name[32];
2964 	  sprintf (name, ".s%s", s->named.name + 1);
2965 	  return get_section (name, s->named.common.flags, NULL);
2966 	}
2967 
2968       if (s == data_section)
2969 	return sdata_section;
2970     }
2971 
2972   return s;
2973 }
2974 
2975 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
2976 
2977 static void ATTRIBUTE_UNUSED
2978 riscv_output_dwarf_dtprel (FILE *file, int size, rtx x)
2979 {
2980   switch (size)
2981     {
2982     case 4:
2983       fputs ("\t.dtprelword\t", file);
2984       break;
2985 
2986     case 8:
2987       fputs ("\t.dtpreldword\t", file);
2988       break;
2989 
2990     default:
2991       gcc_unreachable ();
2992     }
2993   output_addr_const (file, x);
2994   fputs ("+0x800", file);
2995 }
2996 
2997 /* Make the last instruction frame-related and note that it performs
2998    the operation described by FRAME_PATTERN.  */
2999 
3000 static void
3001 riscv_set_frame_expr (rtx frame_pattern)
3002 {
3003   rtx insn;
3004 
3005   insn = get_last_insn ();
3006   RTX_FRAME_RELATED_P (insn) = 1;
3007   REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
3008 				      frame_pattern,
3009 				      REG_NOTES (insn));
3010 }
3011 
3012 /* Return a frame-related rtx that stores REG at MEM.
3013    REG must be a single register.  */
3014 
3015 static rtx
3016 riscv_frame_set (rtx mem, rtx reg)
3017 {
3018   rtx set;
3019 
3020   set = gen_rtx_SET (VOIDmode, mem, reg);
3021   RTX_FRAME_RELATED_P (set) = 1;
3022 
3023   return set;
3024 }
3025 
3026 /* Return true if the current function must save register REGNO.  */
3027 
3028 static bool
3029 riscv_save_reg_p (unsigned int regno)
3030 {
3031   bool call_saved = !global_regs[regno] && !call_really_used_regs[regno];
3032   bool might_clobber = crtl->saves_all_registers
3033 		       || df_regs_ever_live_p (regno)
3034 		       || (regno == HARD_FRAME_POINTER_REGNUM
3035 			   && frame_pointer_needed);
3036 
3037   return (call_saved && might_clobber)
3038 	 || (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return);
3039 }
3040 
3041 /* Populate the current function's riscv_frame_info structure.
3042 
3043    RISC-V stack frames grown downward.  High addresses are at the top.
3044 
3045 	+-------------------------------+
3046 	|                               |
3047 	|  incoming stack arguments     |
3048 	|                               |
3049 	+-------------------------------+ <-- incoming stack pointer
3050 	|                               |
3051 	|  callee-allocated save area   |
3052 	|  for arguments that are       |
3053 	|  split between registers and  |
3054 	|  the stack                    |
3055 	|                               |
3056 	+-------------------------------+ <-- arg_pointer_rtx
3057 	|                               |
3058 	|  callee-allocated save area   |
3059 	|  for register varargs         |
3060 	|                               |
3061 	+-------------------------------+ <-- hard_frame_pointer_rtx;
3062 	|                               |     stack_pointer_rtx + gp_sp_offset
3063 	|  GPR save area                |       + UNITS_PER_WORD
3064 	|                               |
3065 	+-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
3066 	|                               |       + UNITS_PER_HWVALUE
3067 	|  FPR save area                |
3068 	|                               |
3069 	+-------------------------------+ <-- frame_pointer_rtx (virtual)
3070 	|                               |
3071 	|  local variables              |
3072 	|                               |
3073       P +-------------------------------+
3074 	|                               |
3075 	|  outgoing stack arguments     |
3076 	|                               |
3077 	+-------------------------------+ <-- stack_pointer_rtx
3078 
3079    Dynamic stack allocations such as alloca insert data at point P.
3080    They decrease stack_pointer_rtx but leave frame_pointer_rtx and
3081    hard_frame_pointer_rtx unchanged.  */
3082 
3083 static void
3084 riscv_compute_frame_info (void)
3085 {
3086   struct riscv_frame_info *frame;
3087   HOST_WIDE_INT offset;
3088   unsigned int regno, i;
3089 
3090   frame = &cfun->machine->frame;
3091   memset (frame, 0, sizeof (*frame));
3092 
3093   /* Find out which GPRs we need to save.  */
3094   for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
3095     if (riscv_save_reg_p (regno))
3096       frame->mask |= 1 << (regno - GP_REG_FIRST);
3097 
3098   /* If this function calls eh_return, we must also save and restore the
3099      EH data registers.  */
3100   if (crtl->calls_eh_return)
3101     for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
3102       frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
3103 
3104   /* Find out which FPRs we need to save.  This loop must iterate over
3105      the same space as its companion in riscv_for_each_saved_gpr_and_fpr.  */
3106   if (TARGET_HARD_FLOAT)
3107     for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
3108       if (riscv_save_reg_p (regno))
3109         frame->fmask |= 1 << (regno - FP_REG_FIRST);
3110 
3111   /* At the bottom of the frame are any outgoing stack arguments. */
3112   offset = crtl->outgoing_args_size;
3113   /* Next are local stack variables. */
3114   offset += RISCV_STACK_ALIGN (get_frame_size ());
3115   /* The virtual frame pointer points above the local variables. */
3116   frame->frame_pointer_offset = offset;
3117   /* Next are the callee-saved FPRs. */
3118   if (frame->fmask)
3119     {
3120       unsigned num_saved = __builtin_popcount(frame->fmask);
3121       offset += RISCV_STACK_ALIGN (num_saved * UNITS_PER_FPREG);
3122       frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
3123     }
3124   /* Next are the callee-saved GPRs. */
3125   if (frame->mask)
3126     {
3127       unsigned num_saved = __builtin_popcount(frame->mask);
3128       offset += RISCV_STACK_ALIGN (num_saved * UNITS_PER_WORD);
3129       frame->gp_sp_offset = offset - UNITS_PER_WORD;
3130     }
3131   /* The hard frame pointer points above the callee-saved GPRs. */
3132   frame->hard_frame_pointer_offset = offset;
3133   /* Above the hard frame pointer is the callee-allocated varags save area. */
3134   offset += RISCV_STACK_ALIGN (cfun->machine->varargs_size);
3135   frame->arg_pointer_offset = offset;
3136   /* Next is the callee-allocated area for pretend stack arguments.  */
3137   offset += crtl->args.pretend_args_size;
3138   frame->total_size = offset;
3139   /* Next points the incoming stack pointer and any incoming arguments. */
3140 }
3141 
3142 /* Make sure that we're not trying to eliminate to the wrong hard frame
3143    pointer.  */
3144 
3145 static bool
3146 riscv_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
3147 {
3148   return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
3149 }
3150 
3151 /* Implement INITIAL_ELIMINATION_OFFSET.  FROM is either the frame pointer
3152    or argument pointer.  TO is either the stack pointer or hard frame
3153    pointer.  */
3154 
3155 HOST_WIDE_INT
3156 riscv_initial_elimination_offset (int from, int to)
3157 {
3158   HOST_WIDE_INT src, dest;
3159 
3160   riscv_compute_frame_info ();
3161 
3162   if (to == HARD_FRAME_POINTER_REGNUM)
3163     dest = cfun->machine->frame.hard_frame_pointer_offset;
3164   else if (to == STACK_POINTER_REGNUM)
3165     dest = 0; /* this is the base of all offsets */
3166   else
3167     gcc_unreachable ();
3168 
3169   if (from == FRAME_POINTER_REGNUM)
3170     src = cfun->machine->frame.frame_pointer_offset;
3171   else if (from == ARG_POINTER_REGNUM)
3172     src = cfun->machine->frame.arg_pointer_offset;
3173   else
3174     gcc_unreachable ();
3175 
3176   return src - dest;
3177 }
3178 
3179 /* Implement RETURN_ADDR_RTX.  We do not support moving back to a
3180    previous frame.  */
3181 
3182 rtx
3183 riscv_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
3184 {
3185   if (count != 0)
3186     return const0_rtx;
3187 
3188   return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
3189 }
3190 
3191 /* Emit code to change the current function's return address to
3192    ADDRESS.  SCRATCH is available as a scratch register, if needed.
3193    ADDRESS and SCRATCH are both word-mode GPRs.  */
3194 
3195 void
3196 riscv_set_return_address (rtx address, rtx scratch)
3197 {
3198   rtx slot_address;
3199 
3200   gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
3201   slot_address = riscv_add_offset (scratch, stack_pointer_rtx,
3202 				  cfun->machine->frame.gp_sp_offset);
3203   riscv_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
3204 }
3205 
3206 /* A function to save or store a register.  The first argument is the
3207    register and the second is the stack slot.  */
3208 typedef void (*riscv_save_restore_fn) (rtx, rtx);
3209 
3210 /* Use FN to save or restore register REGNO.  MODE is the register's
3211    mode and OFFSET is the offset of its save slot from the current
3212    stack pointer.  */
3213 
3214 static void
3215 riscv_save_restore_reg (enum machine_mode mode, int regno,
3216 		       HOST_WIDE_INT offset, riscv_save_restore_fn fn)
3217 {
3218   rtx mem;
3219 
3220   mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx, offset));
3221   fn (gen_rtx_REG (mode, regno), mem);
3222 }
3223 
3224 /* Call FN for each register that is saved by the current function.
3225    SP_OFFSET is the offset of the current stack pointer from the start
3226    of the frame.  */
3227 
3228 static void
3229 riscv_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
3230 				 riscv_save_restore_fn fn)
3231 {
3232   HOST_WIDE_INT offset;
3233   int regno;
3234 
3235   /* Save the link register and s-registers. */
3236   offset = cfun->machine->frame.gp_sp_offset - sp_offset;
3237   for (regno = GP_REG_FIRST; regno <= GP_REG_LAST-1; regno++)
3238     if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
3239       {
3240         riscv_save_restore_reg (word_mode, regno, offset, fn);
3241         offset -= UNITS_PER_WORD;
3242       }
3243 
3244   /* This loop must iterate over the same space as its companion in
3245      riscv_compute_frame_info.  */
3246   offset = cfun->machine->frame.fp_sp_offset - sp_offset;
3247   for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
3248     if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
3249       {
3250 	riscv_save_restore_reg (DFmode, regno, offset, fn);
3251 	offset -= GET_MODE_SIZE (DFmode);
3252       }
3253 }
3254 
3255 /* Emit a move from SRC to DEST, given that one of them is a register
3256    save slot and that the other is a register.  TEMP is a temporary
3257    GPR of the same mode that is available if need be.  */
3258 
3259 static void
3260 riscv_emit_save_slot_move (rtx dest, rtx src, rtx temp)
3261 {
3262   unsigned int regno;
3263   rtx mem;
3264   enum reg_class rclass;
3265 
3266   if (REG_P (src))
3267     {
3268       regno = REGNO (src);
3269       mem = dest;
3270     }
3271   else
3272     {
3273       regno = REGNO (dest);
3274       mem = src;
3275     }
3276 
3277   rclass = riscv_secondary_reload_class (REGNO_REG_CLASS (regno),
3278 					 GET_MODE (mem), mem, mem == src);
3279 
3280   if (rclass == NO_REGS)
3281     riscv_emit_move (dest, src);
3282   else
3283     {
3284       gcc_assert (!reg_overlap_mentioned_p (dest, temp));
3285       riscv_emit_move (temp, src);
3286       riscv_emit_move (dest, temp);
3287     }
3288   if (MEM_P (dest))
3289     riscv_set_frame_expr (riscv_frame_set (dest, src));
3290 }
3291 
3292 /* Save register REG to MEM.  Make the instruction frame-related.  */
3293 
3294 static void
3295 riscv_save_reg (rtx reg, rtx mem)
3296 {
3297   riscv_emit_save_slot_move (mem, reg, RISCV_PROLOGUE_TEMP (GET_MODE (reg)));
3298 }
3299 
3300 
3301 /* Expand the "prologue" pattern.  */
3302 
3303 void
3304 riscv_expand_prologue (void)
3305 {
3306   const struct riscv_frame_info *frame;
3307   HOST_WIDE_INT size;
3308   rtx insn;
3309 
3310   frame = &cfun->machine->frame;
3311   size = frame->total_size;
3312 
3313   if (flag_stack_usage_info)
3314     current_function_static_stack_size = size;
3315 
3316   /* Save the registers.  Allocate up to RISCV_MAX_FIRST_STACK_STEP
3317      bytes beforehand; this is enough to cover the register save area
3318      without going out of range.  */
3319   if ((frame->mask | frame->fmask) != 0)
3320     {
3321       HOST_WIDE_INT step1;
3322 
3323       step1 = MIN (size, RISCV_MAX_FIRST_STACK_STEP);
3324       insn = gen_add3_insn (stack_pointer_rtx,
3325 			    stack_pointer_rtx,
3326 			    GEN_INT (-step1));
3327       RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
3328       size -= step1;
3329       riscv_for_each_saved_gpr_and_fpr (size, riscv_save_reg);
3330     }
3331 
3332   /* Set up the frame pointer, if we're using one.  */
3333   if (frame_pointer_needed)
3334     {
3335       insn = gen_add3_insn (hard_frame_pointer_rtx, stack_pointer_rtx,
3336                             GEN_INT (frame->hard_frame_pointer_offset - size));
3337       RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
3338     }
3339 
3340   /* Allocate the rest of the frame.  */
3341   if (size > 0)
3342     {
3343       if (SMALL_OPERAND (-size))
3344 	RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
3345 						       stack_pointer_rtx,
3346 						       GEN_INT (-size)))) = 1;
3347       else
3348 	{
3349 	  riscv_emit_move (RISCV_PROLOGUE_TEMP (Pmode), GEN_INT (size));
3350 	  emit_insn (gen_sub3_insn (stack_pointer_rtx,
3351 				    stack_pointer_rtx,
3352 				    RISCV_PROLOGUE_TEMP (Pmode)));
3353 
3354 	  /* Describe the combined effect of the previous instructions.  */
3355 	  riscv_set_frame_expr
3356 	    (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
3357 			  plus_constant (Pmode, stack_pointer_rtx, -size)));
3358 	}
3359     }
3360 }
3361 
3362 /* Emit instructions to restore register REG from slot MEM.  */
3363 
3364 static void
3365 riscv_restore_reg (rtx reg, rtx mem)
3366 {
3367   riscv_emit_save_slot_move (reg, mem, RISCV_EPILOGUE_TEMP (GET_MODE (reg)));
3368 }
3369 
3370 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
3371    says which.  */
3372 
3373 void
3374 riscv_expand_epilogue (bool sibcall_p)
3375 {
3376   const struct riscv_frame_info *frame;
3377   HOST_WIDE_INT step1, step2;
3378 
3379   if (!sibcall_p && riscv_can_use_return_insn ())
3380     {
3381       emit_jump_insn (gen_return ());
3382       return;
3383     }
3384 
3385   /* Split the frame into two.  STEP1 is the amount of stack we should
3386      deallocate before restoring the registers.  STEP2 is the amount we
3387      should deallocate afterwards.
3388 
3389      Start off by assuming that no registers need to be restored.  */
3390   frame = &cfun->machine->frame;
3391   step1 = frame->total_size;
3392   step2 = 0;
3393 
3394   /* Move past any dynamic stack allocations. */
3395   if (cfun->calls_alloca)
3396     {
3397       rtx adjust = GEN_INT (-frame->hard_frame_pointer_offset);
3398       if (!SMALL_INT (adjust))
3399 	{
3400 	  riscv_emit_move (RISCV_EPILOGUE_TEMP (Pmode), adjust);
3401 	  adjust = RISCV_EPILOGUE_TEMP (Pmode);
3402 	}
3403 
3404       emit_insn (gen_add3_insn (stack_pointer_rtx, hard_frame_pointer_rtx, adjust));
3405     }
3406 
3407   /* If we need to restore registers, deallocate as much stack as
3408      possible in the second step without going out of range.  */
3409   if ((frame->mask | frame->fmask) != 0)
3410     {
3411       step2 = MIN (step1, RISCV_MAX_FIRST_STACK_STEP);
3412       step1 -= step2;
3413     }
3414 
3415   /* Set TARGET to BASE + STEP1.  */
3416   if (step1 > 0)
3417     {
3418       /* Get an rtx for STEP1 that we can add to BASE.  */
3419       rtx adjust = GEN_INT (step1);
3420       if (!SMALL_OPERAND (step1))
3421 	{
3422 	  riscv_emit_move (RISCV_EPILOGUE_TEMP (Pmode), adjust);
3423 	  adjust = RISCV_EPILOGUE_TEMP (Pmode);
3424 	}
3425 
3426       emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx, adjust));
3427     }
3428 
3429   /* Restore the registers.  */
3430   riscv_for_each_saved_gpr_and_fpr (frame->total_size - step2,
3431 				    riscv_restore_reg);
3432 
3433   /* Deallocate the final bit of the frame.  */
3434   if (step2 > 0)
3435     emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
3436 			      GEN_INT (step2)));
3437 
3438   /* Add in the __builtin_eh_return stack adjustment. */
3439   if (crtl->calls_eh_return)
3440     emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
3441 			      EH_RETURN_STACKADJ_RTX));
3442 
3443   if (!sibcall_p)
3444     {
3445       rtx ra = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
3446       emit_jump_insn (gen_simple_return_internal (ra));
3447     }
3448 }
3449 
3450 /* Return nonzero if this function is known to have a null epilogue.
3451    This allows the optimizer to omit jumps to jumps if no stack
3452    was created.  */
3453 
3454 bool
3455 riscv_can_use_return_insn (void)
3456 {
3457   return reload_completed && cfun->machine->frame.total_size == 0;
3458 }
3459 
3460 /* Return true if register REGNO can store a value of mode MODE.
3461    The result of this function is cached in riscv_hard_regno_mode_ok.  */
3462 
3463 static bool
3464 riscv_hard_regno_mode_ok_p (unsigned int regno, enum machine_mode mode)
3465 {
3466   unsigned int size = GET_MODE_SIZE (mode);
3467   enum mode_class mclass = GET_MODE_CLASS (mode);
3468 
3469   /* This is hella bogus but ira_build segfaults on RV32 without it. */
3470   if (VECTOR_MODE_P (mode))
3471     return true;
3472 
3473   if (GP_REG_P (regno))
3474     {
3475       if (size <= UNITS_PER_WORD)
3476 	return true;
3477 
3478       /* Double-word values must be even-register-aligned.  */
3479       if (size <= 2 * UNITS_PER_WORD)
3480 	return regno % 2 == 0;
3481     }
3482 
3483   if (FP_REG_P (regno))
3484     {
3485       if (mclass == MODE_FLOAT
3486 	  || mclass == MODE_COMPLEX_FLOAT
3487 	  || mclass == MODE_VECTOR_FLOAT)
3488 	return size <= UNITS_PER_FPVALUE;
3489     }
3490 
3491   return false;
3492 }
3493 
3494 /* Implement HARD_REGNO_NREGS.  */
3495 
3496 unsigned int
3497 riscv_hard_regno_nregs (int regno, enum machine_mode mode)
3498 {
3499   if (FP_REG_P (regno))
3500     return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
3501 
3502   /* All other registers are word-sized.  */
3503   return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3504 }
3505 
3506 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
3507    in riscv_hard_regno_nregs.  */
3508 
3509 int
3510 riscv_class_max_nregs (enum reg_class rclass, enum machine_mode mode)
3511 {
3512   int size;
3513   HARD_REG_SET left;
3514 
3515   size = 0x8000;
3516   COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
3517   if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
3518     {
3519       size = MIN (size, UNITS_PER_FPREG);
3520       AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
3521     }
3522   if (!hard_reg_set_empty_p (left))
3523     size = MIN (size, UNITS_PER_WORD);
3524   return (GET_MODE_SIZE (mode) + size - 1) / size;
3525 }
3526 
3527 /* Implement TARGET_PREFERRED_RELOAD_CLASS.  */
3528 
3529 static reg_class_t
3530 riscv_preferred_reload_class (rtx x ATTRIBUTE_UNUSED, reg_class_t rclass)
3531 {
3532   return reg_class_subset_p (FP_REGS, rclass) ? FP_REGS :
3533          reg_class_subset_p (GR_REGS, rclass) ? GR_REGS :
3534 	 rclass;
3535 }
3536 
3537 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
3538    Return a "canonical" class to represent it in later calculations.  */
3539 
3540 static reg_class_t
3541 riscv_canonicalize_move_class (reg_class_t rclass)
3542 {
3543   if (reg_class_subset_p (rclass, GENERAL_REGS))
3544     rclass = GENERAL_REGS;
3545 
3546   return rclass;
3547 }
3548 
3549 /* Implement TARGET_REGISTER_MOVE_COST.  Return 0 for classes that are the
3550    maximum of the move costs for subclasses; regclass will work out
3551    the maximum for us.  */
3552 
3553 static int
3554 riscv_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
3555 			 reg_class_t from, reg_class_t to)
3556 {
3557   from = riscv_canonicalize_move_class (from);
3558   to = riscv_canonicalize_move_class (to);
3559 
3560   if ((from == GENERAL_REGS && to == GENERAL_REGS)
3561       || (from == GENERAL_REGS && to == FP_REGS)
3562       || (from == FP_REGS && to == FP_REGS))
3563     return COSTS_N_INSNS (1);
3564 
3565   if (from == FP_REGS && to == GENERAL_REGS)
3566     return tune_info->fp_to_int_cost;
3567 
3568   return 0;
3569 }
3570 
3571 /* Implement TARGET_MEMORY_MOVE_COST.  */
3572 
3573 static int
3574 riscv_memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
3575 {
3576   return (tune_info->memory_cost
3577 	  + memory_move_secondary_cost (mode, rclass, in));
3578 }
3579 
3580 /* Return the register class required for a secondary register when
3581    copying between one of the registers in RCLASS and value X, which
3582    has mode MODE.  X is the source of the move if IN_P, otherwise it
3583    is the destination.  Return NO_REGS if no secondary register is
3584    needed.  */
3585 
3586 enum reg_class
3587 riscv_secondary_reload_class (enum reg_class rclass,
3588 			     enum machine_mode mode, rtx x,
3589 			     bool in_p ATTRIBUTE_UNUSED)
3590 {
3591   int regno;
3592 
3593   regno = true_regnum (x);
3594 
3595   if (reg_class_subset_p (rclass, FP_REGS))
3596     {
3597       if (MEM_P (x) && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8))
3598 	/* We can use flw/fld/fsw/fsd. */
3599 	return NO_REGS;
3600 
3601       if (GP_REG_P (regno) || x == CONST0_RTX (mode))
3602 	/* We can use fmv or go through memory when mode > Pmode. */
3603 	return NO_REGS;
3604 
3605       if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
3606 	/* We can force the constant to memory and use flw/fld. */
3607 	return NO_REGS;
3608 
3609       if (FP_REG_P (regno))
3610 	/* We can use fmv.fmt. */
3611 	return NO_REGS;
3612 
3613       /* Otherwise, we need to reload through an integer register.  */
3614       return GR_REGS;
3615     }
3616   if (FP_REG_P (regno))
3617     return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
3618 
3619   return NO_REGS;
3620 }
3621 
3622 /* Implement TARGET_MODE_REP_EXTENDED.  */
3623 
3624 static int
3625 riscv_mode_rep_extended (enum machine_mode mode, enum machine_mode mode_rep)
3626 {
3627   /* On 64-bit targets, SImode register values are sign-extended to DImode.  */
3628   if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
3629     return SIGN_EXTEND;
3630 
3631   return UNKNOWN;
3632 }
3633 
3634 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.  */
3635 
3636 static bool
3637 riscv_scalar_mode_supported_p (enum machine_mode mode)
3638 {
3639   if (ALL_FIXED_POINT_MODE_P (mode)
3640       && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
3641     return true;
3642 
3643   return default_scalar_mode_supported_p (mode);
3644 }
3645 
3646 /* Implement TARGET_SCHED_ADJUST_COST.  We assume that anti and output
3647    dependencies have no cost. */
3648 
3649 static int
3650 riscv_adjust_cost (rtx insn ATTRIBUTE_UNUSED, rtx link,
3651 		  rtx dep ATTRIBUTE_UNUSED, int cost)
3652 {
3653   if (REG_NOTE_KIND (link) != 0)
3654     return 0;
3655   return cost;
3656 }
3657 
3658 /* Return the number of instructions that can be issued per cycle.  */
3659 
3660 static int
3661 riscv_issue_rate (void)
3662 {
3663   return tune_info->issue_rate;
3664 }
3665 
3666 /* This structure describes a single built-in function.  */
3667 struct riscv_builtin_description {
3668   /* The code of the main .md file instruction.  See riscv_builtin_type
3669      for more information.  */
3670   enum insn_code icode;
3671 
3672   /* The name of the built-in function.  */
3673   const char *name;
3674 
3675   /* Specifies how the function should be expanded.  */
3676   enum riscv_builtin_type builtin_type;
3677 
3678   /* The function's prototype.  */
3679   enum riscv_function_type function_type;
3680 
3681   /* Whether the function is available.  */
3682   unsigned int (*avail) (void);
3683 };
3684 
3685 static unsigned int
3686 riscv_builtin_avail_riscv (void)
3687 {
3688   return 1;
3689 }
3690 
3691 /* Construct a riscv_builtin_description from the given arguments.
3692 
3693    INSN is the name of the associated instruction pattern, without the
3694    leading CODE_FOR_riscv_.
3695 
3696    CODE is the floating-point condition code associated with the
3697    function.  It can be 'f' if the field is not applicable.
3698 
3699    NAME is the name of the function itself, without the leading
3700    "__builtin_riscv_".
3701 
3702    BUILTIN_TYPE and FUNCTION_TYPE are riscv_builtin_description fields.
3703 
3704    AVAIL is the name of the availability predicate, without the leading
3705    riscv_builtin_avail_.  */
3706 #define RISCV_BUILTIN(INSN, NAME, BUILTIN_TYPE, FUNCTION_TYPE, AVAIL)	\
3707   { CODE_FOR_ ## INSN, "__builtin_riscv_" NAME,				\
3708     BUILTIN_TYPE, FUNCTION_TYPE, riscv_builtin_avail_ ## AVAIL }
3709 
3710 /* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT function
3711    mapped to instruction CODE_FOR_<INSN>,  FUNCTION_TYPE and AVAIL
3712    are as for RISCV_BUILTIN.  */
3713 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL)			\
3714   RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
3715 
3716 /* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT_NO_TARGET
3717    function mapped to instruction CODE_FOR_<INSN>,  FUNCTION_TYPE
3718    and AVAIL are as for RISCV_BUILTIN.  */
3719 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL)		\
3720   RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT_NO_TARGET,		\
3721 		FUNCTION_TYPE, AVAIL)
3722 
3723 static const struct riscv_builtin_description riscv_builtins[] = {
3724   DIRECT_NO_TARGET_BUILTIN (nop, RISCV_VOID_FTYPE_VOID, riscv),
3725 };
3726 
3727 /* Index I is the function declaration for riscv_builtins[I], or null if the
3728    function isn't defined on this target.  */
3729 static GTY(()) tree riscv_builtin_decls[ARRAY_SIZE (riscv_builtins)];
3730 
3731 
3732 /* Source-level argument types.  */
3733 #define RISCV_ATYPE_VOID void_type_node
3734 #define RISCV_ATYPE_INT integer_type_node
3735 #define RISCV_ATYPE_POINTER ptr_type_node
3736 #define RISCV_ATYPE_CPOINTER const_ptr_type_node
3737 
3738 /* Standard mode-based argument types.  */
3739 #define RISCV_ATYPE_UQI unsigned_intQI_type_node
3740 #define RISCV_ATYPE_SI intSI_type_node
3741 #define RISCV_ATYPE_USI unsigned_intSI_type_node
3742 #define RISCV_ATYPE_DI intDI_type_node
3743 #define RISCV_ATYPE_UDI unsigned_intDI_type_node
3744 #define RISCV_ATYPE_SF float_type_node
3745 #define RISCV_ATYPE_DF double_type_node
3746 
3747 /* RISCV_FTYPE_ATYPESN takes N RISCV_FTYPES-like type codes and lists
3748    their associated RISCV_ATYPEs.  */
3749 #define RISCV_FTYPE_ATYPES1(A, B) \
3750   RISCV_ATYPE_##A, RISCV_ATYPE_##B
3751 
3752 #define RISCV_FTYPE_ATYPES2(A, B, C) \
3753   RISCV_ATYPE_##A, RISCV_ATYPE_##B, RISCV_ATYPE_##C
3754 
3755 #define RISCV_FTYPE_ATYPES3(A, B, C, D) \
3756   RISCV_ATYPE_##A, RISCV_ATYPE_##B, RISCV_ATYPE_##C, RISCV_ATYPE_##D
3757 
3758 #define RISCV_FTYPE_ATYPES4(A, B, C, D, E) \
3759   RISCV_ATYPE_##A, RISCV_ATYPE_##B, RISCV_ATYPE_##C, RISCV_ATYPE_##D, \
3760   RISCV_ATYPE_##E
3761 
3762 /* Return the function type associated with function prototype TYPE.  */
3763 
3764 static tree
3765 riscv_build_function_type (enum riscv_function_type type)
3766 {
3767   static tree types[(int) RISCV_MAX_FTYPE_MAX];
3768 
3769   if (types[(int) type] == NULL_TREE)
3770     switch (type)
3771       {
3772 #define DEF_RISCV_FTYPE(NUM, ARGS)					\
3773   case RISCV_FTYPE_NAME##NUM ARGS:					\
3774     types[(int) type]							\
3775       = build_function_type_list (RISCV_FTYPE_ATYPES##NUM ARGS,		\
3776 				  NULL_TREE);				\
3777     break;
3778 #include "config/riscv/riscv-ftypes.def"
3779 #undef DEF_RISCV_FTYPE
3780       default:
3781 	gcc_unreachable ();
3782       }
3783 
3784   return types[(int) type];
3785 }
3786 
3787 /* Implement TARGET_INIT_BUILTINS.  */
3788 
3789 static void
3790 riscv_init_builtins (void)
3791 {
3792   const struct riscv_builtin_description *d;
3793   unsigned int i;
3794 
3795   /* Iterate through all of the bdesc arrays, initializing all of the
3796      builtin functions.  */
3797   for (i = 0; i < ARRAY_SIZE (riscv_builtins); i++)
3798     {
3799       d = &riscv_builtins[i];
3800       if (d->avail ())
3801 	riscv_builtin_decls[i]
3802 	  = add_builtin_function (d->name,
3803 				  riscv_build_function_type (d->function_type),
3804 				  i, BUILT_IN_MD, NULL, NULL);
3805     }
3806 }
3807 
3808 /* Implement TARGET_BUILTIN_DECL.  */
3809 
3810 static tree
3811 riscv_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
3812 {
3813   if (code >= ARRAY_SIZE (riscv_builtins))
3814     return error_mark_node;
3815   return riscv_builtin_decls[code];
3816 }
3817 
3818 /* Take argument ARGNO from EXP's argument list and convert it into a
3819    form suitable for input operand OPNO of instruction ICODE.  Return the
3820    value.  */
3821 
3822 static rtx
3823 riscv_prepare_builtin_arg (enum insn_code icode,
3824 			  unsigned int opno, tree exp, unsigned int argno)
3825 {
3826   tree arg;
3827   rtx value;
3828   enum machine_mode mode;
3829 
3830   arg = CALL_EXPR_ARG (exp, argno);
3831   value = expand_normal (arg);
3832   mode = insn_data[icode].operand[opno].mode;
3833   if (!insn_data[icode].operand[opno].predicate (value, mode))
3834     {
3835       /* We need to get the mode from ARG for two reasons:
3836 
3837 	   - to cope with address operands, where MODE is the mode of the
3838 	     memory, rather than of VALUE itself.
3839 
3840 	   - to cope with special predicates like pmode_register_operand,
3841 	     where MODE is VOIDmode.  */
3842       value = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (arg)), value);
3843 
3844       /* Check the predicate again.  */
3845       if (!insn_data[icode].operand[opno].predicate (value, mode))
3846 	{
3847 	  error ("invalid argument to built-in function");
3848 	  return const0_rtx;
3849 	}
3850     }
3851 
3852   return value;
3853 }
3854 
3855 /* Return an rtx suitable for output operand OP of instruction ICODE.
3856    If TARGET is non-null, try to use it where possible.  */
3857 
3858 static rtx
3859 riscv_prepare_builtin_target (enum insn_code icode, unsigned int op, rtx target)
3860 {
3861   enum machine_mode mode;
3862 
3863   mode = insn_data[icode].operand[op].mode;
3864   if (target == 0 || !insn_data[icode].operand[op].predicate (target, mode))
3865     target = gen_reg_rtx (mode);
3866 
3867   return target;
3868 }
3869 
3870 /* Expand a RISCV_BUILTIN_DIRECT or RISCV_BUILTIN_DIRECT_NO_TARGET function;
3871    HAS_TARGET_P says which.  EXP is the CALL_EXPR that calls the function
3872    and ICODE is the code of the associated .md pattern.  TARGET, if nonnull,
3873    suggests a good place to put the result.  */
3874 
3875 static rtx
3876 riscv_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
3877 			    bool has_target_p)
3878 {
3879   rtx ops[MAX_RECOG_OPERANDS];
3880   int opno, argno;
3881 
3882   /* Map any target to operand 0.  */
3883   opno = 0;
3884   if (has_target_p)
3885     {
3886       target = riscv_prepare_builtin_target (icode, opno, target);
3887       ops[opno] = target;
3888       opno++;
3889     }
3890 
3891   /* Map the arguments to the other operands.  The n_operands value
3892      for an expander includes match_dups and match_scratches as well as
3893      match_operands, so n_operands is only an upper bound on the number
3894      of arguments to the expander function.  */
3895   gcc_assert (opno + call_expr_nargs (exp) <= insn_data[icode].n_operands);
3896   for (argno = 0; argno < call_expr_nargs (exp); argno++, opno++)
3897     ops[opno] = riscv_prepare_builtin_arg (icode, opno, exp, argno);
3898 
3899   switch (opno)
3900     {
3901     case 2:
3902       emit_insn (GEN_FCN (icode) (ops[0], ops[1]));
3903       break;
3904 
3905     case 3:
3906       emit_insn (GEN_FCN (icode) (ops[0], ops[1], ops[2]));
3907       break;
3908 
3909     case 4:
3910       emit_insn (GEN_FCN (icode) (ops[0], ops[1], ops[2], ops[3]));
3911       break;
3912 
3913     default:
3914       gcc_unreachable ();
3915     }
3916   return target;
3917 }
3918 
3919 /* Implement TARGET_EXPAND_BUILTIN.  */
3920 
3921 static rtx
3922 riscv_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
3923 		     enum machine_mode mode ATTRIBUTE_UNUSED,
3924 		     int ignore ATTRIBUTE_UNUSED)
3925 {
3926   tree fndecl;
3927   unsigned int fcode, avail;
3928   const struct riscv_builtin_description *d;
3929 
3930   fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
3931   fcode = DECL_FUNCTION_CODE (fndecl);
3932   gcc_assert (fcode < ARRAY_SIZE (riscv_builtins));
3933   d = &riscv_builtins[fcode];
3934   avail = d->avail ();
3935   gcc_assert (avail != 0);
3936   switch (d->builtin_type)
3937     {
3938     case RISCV_BUILTIN_DIRECT:
3939       return riscv_expand_builtin_direct (d->icode, target, exp, true);
3940 
3941     case RISCV_BUILTIN_DIRECT_NO_TARGET:
3942       return riscv_expand_builtin_direct (d->icode, target, exp, false);
3943     }
3944   gcc_unreachable ();
3945 }
3946 
3947 /* Implement TARGET_ASM_OUTPUT_MI_THUNK.  Generate rtl rather than asm text
3948    in order to avoid duplicating too much logic from elsewhere.  */
3949 
3950 static void
3951 riscv_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
3952 		      HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
3953 		      tree function)
3954 {
3955   rtx this_rtx, temp1, temp2, insn, fnaddr;
3956   bool use_sibcall_p;
3957 
3958   /* Pretend to be a post-reload pass while generating rtl.  */
3959   reload_completed = 1;
3960 
3961   /* Mark the end of the (empty) prologue.  */
3962   emit_note (NOTE_INSN_PROLOGUE_END);
3963 
3964   /* Determine if we can use a sibcall to call FUNCTION directly.  */
3965   fnaddr = XEXP (DECL_RTL (function), 0);
3966   use_sibcall_p = absolute_symbolic_operand (fnaddr, Pmode);
3967 
3968   /* We need two temporary registers in some cases.  */
3969   temp1 = gen_rtx_REG (Pmode, GP_TEMP_FIRST);
3970   temp2 = gen_rtx_REG (Pmode, GP_TEMP_FIRST + 1);
3971 
3972   /* Find out which register contains the "this" pointer.  */
3973   if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
3974     this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
3975   else
3976     this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3977 
3978   /* Add DELTA to THIS_RTX.  */
3979   if (delta != 0)
3980     {
3981       rtx offset = GEN_INT (delta);
3982       if (!SMALL_OPERAND (delta))
3983 	{
3984 	  riscv_emit_move (temp1, offset);
3985 	  offset = temp1;
3986 	}
3987       emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
3988     }
3989 
3990   /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX.  */
3991   if (vcall_offset != 0)
3992     {
3993       rtx addr;
3994 
3995       /* Set TEMP1 to *THIS_RTX.  */
3996       riscv_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
3997 
3998       /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET.  */
3999       addr = riscv_add_offset (temp2, temp1, vcall_offset);
4000 
4001       /* Load the offset and add it to THIS_RTX.  */
4002       riscv_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
4003       emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
4004     }
4005 
4006   /* Jump to the target function.  Use a sibcall if direct jumps are
4007      allowed, otherwise load the address into a register first.  */
4008   if (use_sibcall_p)
4009     {
4010       insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
4011       SIBLING_CALL_P (insn) = 1;
4012     }
4013   else
4014     {
4015       riscv_emit_move(temp1, fnaddr);
4016       emit_jump_insn (gen_indirect_jump (temp1));
4017     }
4018 
4019   /* Run just enough of rest_of_compilation.  This sequence was
4020      "borrowed" from alpha.c.  */
4021   insn = get_insns ();
4022   split_all_insns_noflow ();
4023   shorten_branches (insn);
4024   final_start_function (insn, file, 1);
4025   final (insn, file, 1);
4026   final_end_function ();
4027 
4028   /* Clean up the vars set above.  Note that final_end_function resets
4029      the global pointer for us.  */
4030   reload_completed = 0;
4031 }
4032 
4033 /* Allocate a chunk of memory for per-function machine-dependent data.  */
4034 
4035 static struct machine_function *
4036 riscv_init_machine_status (void)
4037 {
4038   return ggc_alloc_cleared_machine_function ();
4039 }
4040 
4041 /* Implement TARGET_OPTION_OVERRIDE.  */
4042 
4043 static void
4044 riscv_option_override (void)
4045 {
4046   int regno, mode;
4047   const struct riscv_cpu_info *cpu;
4048 
4049 #ifdef SUBTARGET_OVERRIDE_OPTIONS
4050   SUBTARGET_OVERRIDE_OPTIONS;
4051 #endif
4052 
4053   flag_pcc_struct_return = 0;
4054 
4055   if (flag_pic)
4056     g_switch_value = 0;
4057 
4058   /* Prefer a call to memcpy over inline code when optimizing for size,
4059      though see MOVE_RATIO in riscv.h.  */
4060   if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
4061     target_flags |= MASK_MEMCPY;
4062 
4063   /* Handle -mtune.  */
4064   cpu = riscv_parse_cpu (riscv_tune_string ? riscv_tune_string :
4065 			 RISCV_TUNE_STRING_DEFAULT);
4066   tune_info = optimize_size ? &optimize_size_tune_info : cpu->tune_info;
4067 
4068   /* If the user hasn't specified a branch cost, use the processor's
4069      default.  */
4070   if (riscv_branch_cost == 0)
4071     riscv_branch_cost = tune_info->branch_cost;
4072 
4073   /* Set up riscv_hard_regno_mode_ok.  */
4074   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
4075     for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4076       riscv_hard_regno_mode_ok[mode][regno]
4077 	= riscv_hard_regno_mode_ok_p (regno, (enum machine_mode) mode);
4078 
4079   /* Function to allocate machine-dependent function status.  */
4080   init_machine_status = &riscv_init_machine_status;
4081 
4082   if (riscv_cmodel_string)
4083     {
4084       if (strcmp (riscv_cmodel_string, "medlow") == 0)
4085 	riscv_cmodel = CM_MEDLOW;
4086       else if (strcmp (riscv_cmodel_string, "medany") == 0)
4087 	riscv_cmodel = CM_MEDANY;
4088       else
4089 	error ("unsupported code model: %s", riscv_cmodel_string);
4090     }
4091 
4092   if (flag_pic)
4093     riscv_cmodel = CM_PIC;
4094 
4095   riscv_init_relocs ();
4096 }
4097 
4098 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE.  */
4099 
4100 static void
4101 riscv_conditional_register_usage (void)
4102 {
4103   int regno;
4104 
4105   if (!TARGET_HARD_FLOAT)
4106     {
4107       for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
4108 	fixed_regs[regno] = call_used_regs[regno] = 1;
4109     }
4110 }
4111 
4112 /* Implement TARGET_TRAMPOLINE_INIT.  */
4113 
4114 static void
4115 riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
4116 {
4117   rtx addr, end_addr, mem;
4118   rtx trampoline[4];
4119   unsigned int i;
4120   HOST_WIDE_INT static_chain_offset, target_function_offset;
4121 
4122   /* Work out the offsets of the pointers from the start of the
4123      trampoline code.  */
4124   gcc_assert (ARRAY_SIZE (trampoline) * 4 == TRAMPOLINE_CODE_SIZE);
4125   static_chain_offset = TRAMPOLINE_CODE_SIZE;
4126   target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
4127 
4128   /* Get pointers to the beginning and end of the code block.  */
4129   addr = force_reg (Pmode, XEXP (m_tramp, 0));
4130   end_addr = riscv_force_binary (Pmode, PLUS, addr, GEN_INT (TRAMPOLINE_CODE_SIZE));
4131 
4132 #define OP(X) gen_int_mode (X, SImode)
4133 #define MATCH_LREG ((Pmode) == DImode ? MATCH_LD : MATCH_LW)
4134 
4135   /* auipc   t0, 0
4136      l[wd]   t1, target_function_offset(t0)
4137      l[wd]   $static_chain, static_chain_offset(t0)
4138      jr      t1
4139   */
4140 
4141   trampoline[0] = OP (RISCV_UTYPE (AUIPC, STATIC_CHAIN_REGNUM, 0));
4142   trampoline[1] = OP (RISCV_ITYPE (LREG, RISCV_PROLOGUE_TEMP_REGNUM,
4143 		    STATIC_CHAIN_REGNUM, target_function_offset));
4144   trampoline[2] = OP (RISCV_ITYPE (LREG, STATIC_CHAIN_REGNUM,
4145 		    STATIC_CHAIN_REGNUM, static_chain_offset));
4146   trampoline[3] = OP (RISCV_ITYPE (JALR, 0, RISCV_PROLOGUE_TEMP_REGNUM, 0));
4147 
4148 #undef MATCH_LREG
4149 #undef OP
4150 
4151   /* Copy the trampoline code.  Leave any padding uninitialized.  */
4152   for (i = 0; i < ARRAY_SIZE (trampoline); i++)
4153     {
4154       mem = adjust_address (m_tramp, SImode, i * GET_MODE_SIZE (SImode));
4155       riscv_emit_move (mem, trampoline[i]);
4156     }
4157 
4158   /* Set up the static chain pointer field.  */
4159   mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
4160   riscv_emit_move (mem, chain_value);
4161 
4162   /* Set up the target function field.  */
4163   mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
4164   riscv_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
4165 
4166   /* Flush the code part of the trampoline.  */
4167   emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
4168   emit_insn (gen_clear_cache (addr, end_addr));
4169 }
4170 
4171 static bool
4172 riscv_lra_p (void)
4173 {
4174   return riscv_lra_flag;
4175 }
4176 
4177 /* Initialize the GCC target structure.  */
4178 #undef TARGET_ASM_ALIGNED_HI_OP
4179 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
4180 #undef TARGET_ASM_ALIGNED_SI_OP
4181 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
4182 #undef TARGET_ASM_ALIGNED_DI_OP
4183 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
4184 
4185 #undef TARGET_OPTION_OVERRIDE
4186 #define TARGET_OPTION_OVERRIDE riscv_option_override
4187 
4188 #undef TARGET_LEGITIMIZE_ADDRESS
4189 #define TARGET_LEGITIMIZE_ADDRESS riscv_legitimize_address
4190 
4191 #undef TARGET_SCHED_ADJUST_COST
4192 #define TARGET_SCHED_ADJUST_COST riscv_adjust_cost
4193 #undef TARGET_SCHED_ISSUE_RATE
4194 #define TARGET_SCHED_ISSUE_RATE riscv_issue_rate
4195 
4196 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
4197 #define TARGET_FUNCTION_OK_FOR_SIBCALL hook_bool_tree_tree_true
4198 
4199 #undef TARGET_REGISTER_MOVE_COST
4200 #define TARGET_REGISTER_MOVE_COST riscv_register_move_cost
4201 #undef TARGET_MEMORY_MOVE_COST
4202 #define TARGET_MEMORY_MOVE_COST riscv_memory_move_cost
4203 #undef TARGET_RTX_COSTS
4204 #define TARGET_RTX_COSTS riscv_rtx_costs
4205 #undef TARGET_ADDRESS_COST
4206 #define TARGET_ADDRESS_COST riscv_address_cost
4207 
4208 #undef  TARGET_PREFERRED_RELOAD_CLASS
4209 #define TARGET_PREFERRED_RELOAD_CLASS riscv_preferred_reload_class
4210 
4211 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
4212 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
4213 
4214 #undef TARGET_EXPAND_BUILTIN_VA_START
4215 #define TARGET_EXPAND_BUILTIN_VA_START riscv_va_start
4216 
4217 #undef  TARGET_PROMOTE_FUNCTION_MODE
4218 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
4219 
4220 #undef TARGET_RETURN_IN_MEMORY
4221 #define TARGET_RETURN_IN_MEMORY riscv_return_in_memory
4222 
4223 #undef TARGET_ASM_OUTPUT_MI_THUNK
4224 #define TARGET_ASM_OUTPUT_MI_THUNK riscv_output_mi_thunk
4225 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
4226 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
4227 
4228 #undef TARGET_PRINT_OPERAND
4229 #define TARGET_PRINT_OPERAND riscv_print_operand
4230 #undef TARGET_PRINT_OPERAND_ADDRESS
4231 #define TARGET_PRINT_OPERAND_ADDRESS riscv_print_operand_address
4232 
4233 #undef TARGET_SETUP_INCOMING_VARARGS
4234 #define TARGET_SETUP_INCOMING_VARARGS riscv_setup_incoming_varargs
4235 #undef TARGET_STRICT_ARGUMENT_NAMING
4236 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
4237 #undef TARGET_MUST_PASS_IN_STACK
4238 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
4239 #undef TARGET_PASS_BY_REFERENCE
4240 #define TARGET_PASS_BY_REFERENCE riscv_pass_by_reference
4241 #undef TARGET_ARG_PARTIAL_BYTES
4242 #define TARGET_ARG_PARTIAL_BYTES riscv_arg_partial_bytes
4243 #undef TARGET_FUNCTION_ARG
4244 #define TARGET_FUNCTION_ARG riscv_function_arg
4245 #undef TARGET_FUNCTION_ARG_ADVANCE
4246 #define TARGET_FUNCTION_ARG_ADVANCE riscv_function_arg_advance
4247 #undef TARGET_FUNCTION_ARG_BOUNDARY
4248 #define TARGET_FUNCTION_ARG_BOUNDARY riscv_function_arg_boundary
4249 
4250 #undef TARGET_MODE_REP_EXTENDED
4251 #define TARGET_MODE_REP_EXTENDED riscv_mode_rep_extended
4252 
4253 #undef TARGET_SCALAR_MODE_SUPPORTED_P
4254 #define TARGET_SCALAR_MODE_SUPPORTED_P riscv_scalar_mode_supported_p
4255 
4256 #undef TARGET_INIT_BUILTINS
4257 #define TARGET_INIT_BUILTINS riscv_init_builtins
4258 #undef TARGET_BUILTIN_DECL
4259 #define TARGET_BUILTIN_DECL riscv_builtin_decl
4260 #undef TARGET_EXPAND_BUILTIN
4261 #define TARGET_EXPAND_BUILTIN riscv_expand_builtin
4262 
4263 #undef TARGET_HAVE_TLS
4264 #define TARGET_HAVE_TLS HAVE_AS_TLS
4265 
4266 #undef TARGET_CANNOT_FORCE_CONST_MEM
4267 #define TARGET_CANNOT_FORCE_CONST_MEM riscv_cannot_force_const_mem
4268 
4269 #undef TARGET_LEGITIMATE_CONSTANT_P
4270 #define TARGET_LEGITIMATE_CONSTANT_P riscv_legitimate_constant_p
4271 
4272 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
4273 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
4274 
4275 #ifdef HAVE_AS_DTPRELWORD
4276 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
4277 #define TARGET_ASM_OUTPUT_DWARF_DTPREL riscv_output_dwarf_dtprel
4278 #endif
4279 
4280 #undef TARGET_LEGITIMATE_ADDRESS_P
4281 #define TARGET_LEGITIMATE_ADDRESS_P	riscv_legitimate_address_p
4282 
4283 #undef TARGET_CAN_ELIMINATE
4284 #define TARGET_CAN_ELIMINATE riscv_can_eliminate
4285 
4286 #undef TARGET_CONDITIONAL_REGISTER_USAGE
4287 #define TARGET_CONDITIONAL_REGISTER_USAGE riscv_conditional_register_usage
4288 
4289 #undef TARGET_TRAMPOLINE_INIT
4290 #define TARGET_TRAMPOLINE_INIT riscv_trampoline_init
4291 
4292 #undef TARGET_IN_SMALL_DATA_P
4293 #define TARGET_IN_SMALL_DATA_P riscv_in_small_data_p
4294 
4295 #undef TARGET_ASM_SELECT_RTX_SECTION
4296 #define TARGET_ASM_SELECT_RTX_SECTION  riscv_elf_select_rtx_section
4297 
4298 #undef TARGET_MIN_ANCHOR_OFFSET
4299 #define TARGET_MIN_ANCHOR_OFFSET (-RISCV_IMM_REACH/2)
4300 
4301 #undef TARGET_MAX_ANCHOR_OFFSET
4302 #define TARGET_MAX_ANCHOR_OFFSET (RISCV_IMM_REACH/2-1)
4303 
4304 #undef TARGET_LRA_P
4305 #define TARGET_LRA_P riscv_lra_p
4306 
4307 struct gcc_target targetm = TARGET_INITIALIZER;
4308 
4309 #include "gt-riscv.h"
4310