]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/riscv/riscv.c
RISC-V: Cost model for zbb extension.
[thirdparty/gcc.git] / gcc / config / riscv / riscv.c
1 /* Subroutines used for code generation for RISC-V.
2 Copyright (C) 2011-2021 Free Software Foundation, Inc.
3 Contributed by Andrew Waterman (andrew@sifive.com).
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 #define IN_TARGET_CODE 1
23
24 #define INCLUDE_STRING
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "regs.h"
31 #include "insn-config.h"
32 #include "insn-attr.h"
33 #include "recog.h"
34 #include "output.h"
35 #include "alias.h"
36 #include "tree.h"
37 #include "stringpool.h"
38 #include "attribs.h"
39 #include "varasm.h"
40 #include "stor-layout.h"
41 #include "calls.h"
42 #include "function.h"
43 #include "explow.h"
44 #include "memmodel.h"
45 #include "emit-rtl.h"
46 #include "reload.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "basic-block.h"
51 #include "expr.h"
52 #include "optabs.h"
53 #include "bitmap.h"
54 #include "df.h"
55 #include "diagnostic.h"
56 #include "builtins.h"
57 #include "predict.h"
58 #include "tree-pass.h"
59 #include "opts.h"
60
61 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
62 #define UNSPEC_ADDRESS_P(X) \
63 (GET_CODE (X) == UNSPEC \
64 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
65 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
66
67 /* Extract the symbol or label from UNSPEC wrapper X. */
68 #define UNSPEC_ADDRESS(X) \
69 XVECEXP (X, 0, 0)
70
71 /* Extract the symbol type from UNSPEC wrapper X. */
72 #define UNSPEC_ADDRESS_TYPE(X) \
73 ((enum riscv_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
74
75 /* True if bit BIT is set in VALUE. */
76 #define BITSET_P(VALUE, BIT) (((VALUE) & (1ULL << (BIT))) != 0)
77
78 /* Classifies an address.
79
80 ADDRESS_REG
81 A natural register + offset address. The register satisfies
82 riscv_valid_base_register_p and the offset is a const_arith_operand.
83
84 ADDRESS_LO_SUM
85 A LO_SUM rtx. The first operand is a valid base register and
86 the second operand is a symbolic address.
87
88 ADDRESS_CONST_INT
89 A signed 16-bit constant address.
90
91 ADDRESS_SYMBOLIC:
92 A constant symbolic address. */
93 enum riscv_address_type {
94 ADDRESS_REG,
95 ADDRESS_LO_SUM,
96 ADDRESS_CONST_INT,
97 ADDRESS_SYMBOLIC
98 };
99
100 /* Information about a function's frame layout. */
101 struct GTY(()) riscv_frame_info {
102 /* The size of the frame in bytes. */
103 HOST_WIDE_INT total_size;
104
105 /* Bit X is set if the function saves or restores GPR X. */
106 unsigned int mask;
107
108 /* Likewise FPR X. */
109 unsigned int fmask;
110
111 /* How much the GPR save/restore routines adjust sp (or 0 if unused). */
112 unsigned save_libcall_adjustment;
113
114 /* Offsets of fixed-point and floating-point save areas from frame bottom */
115 HOST_WIDE_INT gp_sp_offset;
116 HOST_WIDE_INT fp_sp_offset;
117
118 /* Offset of virtual frame pointer from stack pointer/frame bottom */
119 HOST_WIDE_INT frame_pointer_offset;
120
121 /* Offset of hard frame pointer from stack pointer/frame bottom */
122 HOST_WIDE_INT hard_frame_pointer_offset;
123
124 /* The offset of arg_pointer_rtx from the bottom of the frame. */
125 HOST_WIDE_INT arg_pointer_offset;
126 };
127
128 enum riscv_privilege_levels {
129 UNKNOWN_MODE, USER_MODE, SUPERVISOR_MODE, MACHINE_MODE
130 };
131
132 struct GTY(()) machine_function {
133 /* The number of extra stack bytes taken up by register varargs.
134 This area is allocated by the callee at the very top of the frame. */
135 int varargs_size;
136
137 /* True if current function is a naked function. */
138 bool naked_p;
139
140 /* True if current function is an interrupt function. */
141 bool interrupt_handler_p;
142 /* For an interrupt handler, indicates the privilege level. */
143 enum riscv_privilege_levels interrupt_mode;
144
145 /* True if attributes on current function have been checked. */
146 bool attributes_checked_p;
147
148 /* The current frame information, calculated by riscv_compute_frame_info. */
149 struct riscv_frame_info frame;
150 };
151
152 /* Information about a single argument. */
153 struct riscv_arg_info {
154 /* True if the argument is at least partially passed on the stack. */
155 bool stack_p;
156
157 /* The number of integer registers allocated to this argument. */
158 unsigned int num_gprs;
159
160 /* The offset of the first register used, provided num_gprs is nonzero.
161 If passed entirely on the stack, the value is MAX_ARGS_IN_REGISTERS. */
162 unsigned int gpr_offset;
163
164 /* The number of floating-point registers allocated to this argument. */
165 unsigned int num_fprs;
166
167 /* The offset of the first register used, provided num_fprs is nonzero. */
168 unsigned int fpr_offset;
169 };
170
171 /* Information about an address described by riscv_address_type.
172
173 ADDRESS_CONST_INT
174 No fields are used.
175
176 ADDRESS_REG
177 REG is the base register and OFFSET is the constant offset.
178
179 ADDRESS_LO_SUM
180 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
181 is the type of symbol it references.
182
183 ADDRESS_SYMBOLIC
184 SYMBOL_TYPE is the type of symbol that the address references. */
185 struct riscv_address_info {
186 enum riscv_address_type type;
187 rtx reg;
188 rtx offset;
189 enum riscv_symbol_type symbol_type;
190 };
191
192 /* One stage in a constant building sequence. These sequences have
193 the form:
194
195 A = VALUE[0]
196 A = A CODE[1] VALUE[1]
197 A = A CODE[2] VALUE[2]
198 ...
199
200 where A is an accumulator, each CODE[i] is a binary rtl operation
201 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
202 struct riscv_integer_op {
203 enum rtx_code code;
204 unsigned HOST_WIDE_INT value;
205 };
206
207 /* The largest number of operations needed to load an integer constant.
208 The worst case is LUI, ADDI, SLLI, ADDI, SLLI, ADDI, SLLI, ADDI. */
209 #define RISCV_MAX_INTEGER_OPS 8
210
211 /* Costs of various operations on the different architectures. */
212
213 struct riscv_tune_param
214 {
215 unsigned short fp_add[2];
216 unsigned short fp_mul[2];
217 unsigned short fp_div[2];
218 unsigned short int_mul[2];
219 unsigned short int_div[2];
220 unsigned short issue_rate;
221 unsigned short branch_cost;
222 unsigned short memory_cost;
223 bool slow_unaligned_access;
224 };
225
226 /* Information about one micro-arch we know about. */
227 struct riscv_tune_info {
228 /* This micro-arch canonical name. */
229 const char *name;
230
231 /* Which automaton to use for tuning. */
232 enum riscv_microarchitecture_type microarchitecture;
233
234 /* Tuning parameters for this micro-arch. */
235 const struct riscv_tune_param *tune_param;
236 };
237
238 /* Global variables for machine-dependent things. */
239
240 /* Whether unaligned accesses execute very slowly. */
241 bool riscv_slow_unaligned_access_p;
242
243 /* Stack alignment to assume/maintain. */
244 unsigned riscv_stack_boundary;
245
246 /* If non-zero, this is an offset to be added to SP to redefine the CFA
247 when restoring the FP register from the stack. Only valid when generating
248 the epilogue. */
249 static int epilogue_cfa_sp_offset;
250
251 /* Which tuning parameters to use. */
252 static const struct riscv_tune_param *tune_param;
253
254 /* Which automaton to use for tuning. */
255 enum riscv_microarchitecture_type riscv_microarchitecture;
256
257 /* Index R is the smallest register class that contains register R. */
258 const enum reg_class riscv_regno_to_class[FIRST_PSEUDO_REGISTER] = {
259 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
260 GR_REGS, GR_REGS, SIBCALL_REGS, SIBCALL_REGS,
261 JALR_REGS, JALR_REGS, SIBCALL_REGS, SIBCALL_REGS,
262 SIBCALL_REGS, SIBCALL_REGS, SIBCALL_REGS, SIBCALL_REGS,
263 SIBCALL_REGS, SIBCALL_REGS, JALR_REGS, JALR_REGS,
264 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
265 JALR_REGS, JALR_REGS, JALR_REGS, JALR_REGS,
266 SIBCALL_REGS, SIBCALL_REGS, SIBCALL_REGS, SIBCALL_REGS,
267 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
268 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
269 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
270 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
271 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
272 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
273 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
274 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
275 FRAME_REGS, FRAME_REGS,
276 };
277
278 /* Costs to use when optimizing for rocket. */
279 static const struct riscv_tune_param rocket_tune_info = {
280 {COSTS_N_INSNS (4), COSTS_N_INSNS (5)}, /* fp_add */
281 {COSTS_N_INSNS (4), COSTS_N_INSNS (5)}, /* fp_mul */
282 {COSTS_N_INSNS (20), COSTS_N_INSNS (20)}, /* fp_div */
283 {COSTS_N_INSNS (4), COSTS_N_INSNS (4)}, /* int_mul */
284 {COSTS_N_INSNS (6), COSTS_N_INSNS (6)}, /* int_div */
285 1, /* issue_rate */
286 3, /* branch_cost */
287 5, /* memory_cost */
288 true, /* slow_unaligned_access */
289 };
290
291 /* Costs to use when optimizing for Sifive 7 Series. */
292 static const struct riscv_tune_param sifive_7_tune_info = {
293 {COSTS_N_INSNS (4), COSTS_N_INSNS (5)}, /* fp_add */
294 {COSTS_N_INSNS (4), COSTS_N_INSNS (5)}, /* fp_mul */
295 {COSTS_N_INSNS (20), COSTS_N_INSNS (20)}, /* fp_div */
296 {COSTS_N_INSNS (4), COSTS_N_INSNS (4)}, /* int_mul */
297 {COSTS_N_INSNS (6), COSTS_N_INSNS (6)}, /* int_div */
298 2, /* issue_rate */
299 4, /* branch_cost */
300 3, /* memory_cost */
301 true, /* slow_unaligned_access */
302 };
303
304 /* Costs to use when optimizing for T-HEAD c906. */
305 static const struct riscv_tune_param thead_c906_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 5, /* memory_cost */
314 false, /* slow_unaligned_access */
315 };
316
317 /* Costs to use when optimizing for size. */
318 static const struct riscv_tune_param 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 2, /* memory_cost */
327 false, /* slow_unaligned_access */
328 };
329
330 static tree riscv_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
331 static tree riscv_handle_type_attribute (tree *, tree, tree, int, bool *);
332
333 /* Defining target-specific uses of __attribute__. */
334 static const struct attribute_spec riscv_attribute_table[] =
335 {
336 /* Syntax: { name, min_len, max_len, decl_required, type_required,
337 function_type_required, affects_type_identity, handler,
338 exclude } */
339
340 /* The attribute telling no prologue/epilogue. */
341 { "naked", 0, 0, true, false, false, false,
342 riscv_handle_fndecl_attribute, NULL },
343 /* This attribute generates prologue/epilogue for interrupt handlers. */
344 { "interrupt", 0, 1, false, true, true, false,
345 riscv_handle_type_attribute, NULL },
346
347 /* The last attribute spec is set to be NULL. */
348 { NULL, 0, 0, false, false, false, false, NULL, NULL }
349 };
350
351 /* Order for the CLOBBERs/USEs of gpr_save. */
352 static const unsigned gpr_save_reg_order[] = {
353 INVALID_REGNUM, T0_REGNUM, T1_REGNUM, RETURN_ADDR_REGNUM,
354 S0_REGNUM, S1_REGNUM, S2_REGNUM, S3_REGNUM, S4_REGNUM,
355 S5_REGNUM, S6_REGNUM, S7_REGNUM, S8_REGNUM, S9_REGNUM,
356 S10_REGNUM, S11_REGNUM
357 };
358
359 /* A table describing all the processors GCC knows about. */
360 static const struct riscv_tune_info riscv_tune_info_table[] = {
361 { "rocket", generic, &rocket_tune_info },
362 { "sifive-3-series", generic, &rocket_tune_info },
363 { "sifive-5-series", generic, &rocket_tune_info },
364 { "sifive-7-series", sifive_7, &sifive_7_tune_info },
365 { "thead-c906", generic, &thead_c906_tune_info },
366 { "size", generic, &optimize_size_tune_info },
367 };
368
369 /* Implement TARGET_MIN_ARITHMETIC_PRECISION. */
370
371 static unsigned int
372 riscv_min_arithmetic_precision (void)
373 {
374 return 32;
375 }
376
377 /* Return the riscv_tune_info entry for the given name string. */
378
379 static const struct riscv_tune_info *
380 riscv_parse_tune (const char *tune_string)
381 {
382 const riscv_cpu_info *cpu = riscv_find_cpu (tune_string);
383
384 if (cpu)
385 tune_string = cpu->tune;
386
387 for (unsigned i = 0; i < ARRAY_SIZE (riscv_tune_info_table); i++)
388 if (strcmp (riscv_tune_info_table[i].name, tune_string) == 0)
389 return riscv_tune_info_table + i;
390
391 error ("unknown cpu %qs for %<-mtune%>", tune_string);
392 return riscv_tune_info_table;
393 }
394
395 /* Helper function for riscv_build_integer; arguments are as for
396 riscv_build_integer. */
397
398 static int
399 riscv_build_integer_1 (struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS],
400 HOST_WIDE_INT value, machine_mode mode)
401 {
402 HOST_WIDE_INT low_part = CONST_LOW_PART (value);
403 int cost = RISCV_MAX_INTEGER_OPS + 1, alt_cost;
404 struct riscv_integer_op alt_codes[RISCV_MAX_INTEGER_OPS];
405
406 if (SMALL_OPERAND (value) || LUI_OPERAND (value))
407 {
408 /* Simply ADDI or LUI. */
409 codes[0].code = UNKNOWN;
410 codes[0].value = value;
411 return 1;
412 }
413
414 /* End with ADDI. When constructing HImode constants, do not generate any
415 intermediate value that is not itself a valid HImode constant. The
416 XORI case below will handle those remaining HImode constants. */
417 if (low_part != 0
418 && (mode != HImode
419 || value - low_part <= ((1 << (GET_MODE_BITSIZE (HImode) - 1)) - 1)))
420 {
421 alt_cost = 1 + riscv_build_integer_1 (alt_codes, value - low_part, mode);
422 if (alt_cost < cost)
423 {
424 alt_codes[alt_cost-1].code = PLUS;
425 alt_codes[alt_cost-1].value = low_part;
426 memcpy (codes, alt_codes, sizeof (alt_codes));
427 cost = alt_cost;
428 }
429 }
430
431 /* End with XORI. */
432 if (cost > 2 && (low_part < 0 || mode == HImode))
433 {
434 alt_cost = 1 + riscv_build_integer_1 (alt_codes, value ^ low_part, mode);
435 if (alt_cost < cost)
436 {
437 alt_codes[alt_cost-1].code = XOR;
438 alt_codes[alt_cost-1].value = low_part;
439 memcpy (codes, alt_codes, sizeof (alt_codes));
440 cost = alt_cost;
441 }
442 }
443
444 /* Eliminate trailing zeros and end with SLLI. */
445 if (cost > 2 && (value & 1) == 0)
446 {
447 int shift = ctz_hwi (value);
448 unsigned HOST_WIDE_INT x = value;
449 x = sext_hwi (x >> shift, HOST_BITS_PER_WIDE_INT - shift);
450
451 /* Don't eliminate the lower 12 bits if LUI might apply. */
452 if (shift > IMM_BITS && !SMALL_OPERAND (x) && LUI_OPERAND (x << IMM_BITS))
453 shift -= IMM_BITS, x <<= IMM_BITS;
454
455 alt_cost = 1 + riscv_build_integer_1 (alt_codes, x, mode);
456 if (alt_cost < cost)
457 {
458 alt_codes[alt_cost-1].code = ASHIFT;
459 alt_codes[alt_cost-1].value = shift;
460 memcpy (codes, alt_codes, sizeof (alt_codes));
461 cost = alt_cost;
462 }
463 }
464
465 gcc_assert (cost <= RISCV_MAX_INTEGER_OPS);
466 return cost;
467 }
468
469 /* Fill CODES with a sequence of rtl operations to load VALUE.
470 Return the number of operations needed. */
471
472 static int
473 riscv_build_integer (struct riscv_integer_op *codes, HOST_WIDE_INT value,
474 machine_mode mode)
475 {
476 int cost = riscv_build_integer_1 (codes, value, mode);
477
478 /* Eliminate leading zeros and end with SRLI. */
479 if (value > 0 && cost > 2)
480 {
481 struct riscv_integer_op alt_codes[RISCV_MAX_INTEGER_OPS];
482 int alt_cost, shift = clz_hwi (value);
483 HOST_WIDE_INT shifted_val;
484
485 /* Try filling trailing bits with 1s. */
486 shifted_val = (value << shift) | ((((HOST_WIDE_INT) 1) << shift) - 1);
487 alt_cost = 1 + riscv_build_integer_1 (alt_codes, shifted_val, mode);
488 if (alt_cost < cost)
489 {
490 alt_codes[alt_cost-1].code = LSHIFTRT;
491 alt_codes[alt_cost-1].value = shift;
492 memcpy (codes, alt_codes, sizeof (alt_codes));
493 cost = alt_cost;
494 }
495
496 /* Try filling trailing bits with 0s. */
497 shifted_val = value << shift;
498 alt_cost = 1 + riscv_build_integer_1 (alt_codes, shifted_val, mode);
499 if (alt_cost < cost)
500 {
501 alt_codes[alt_cost-1].code = LSHIFTRT;
502 alt_codes[alt_cost-1].value = shift;
503 memcpy (codes, alt_codes, sizeof (alt_codes));
504 cost = alt_cost;
505 }
506 }
507
508 return cost;
509 }
510
511 /* Return the cost of constructing VAL in the event that a scratch
512 register is available. */
513
514 static int
515 riscv_split_integer_cost (HOST_WIDE_INT val)
516 {
517 int cost;
518 unsigned HOST_WIDE_INT loval = sext_hwi (val, 32);
519 unsigned HOST_WIDE_INT hival = sext_hwi ((val - loval) >> 32, 32);
520 struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
521
522 cost = 2 + riscv_build_integer (codes, loval, VOIDmode);
523 if (loval != hival)
524 cost += riscv_build_integer (codes, hival, VOIDmode);
525
526 return cost;
527 }
528
529 /* Return the cost of constructing the integer constant VAL. */
530
531 static int
532 riscv_integer_cost (HOST_WIDE_INT val)
533 {
534 struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
535 return MIN (riscv_build_integer (codes, val, VOIDmode),
536 riscv_split_integer_cost (val));
537 }
538
539 /* Try to split a 64b integer into 32b parts, then reassemble. */
540
541 static rtx
542 riscv_split_integer (HOST_WIDE_INT val, machine_mode mode)
543 {
544 unsigned HOST_WIDE_INT loval = sext_hwi (val, 32);
545 unsigned HOST_WIDE_INT hival = sext_hwi ((val - loval) >> 32, 32);
546 rtx hi = gen_reg_rtx (mode), lo = gen_reg_rtx (mode);
547
548 riscv_move_integer (hi, hi, hival, mode, FALSE);
549 riscv_move_integer (lo, lo, loval, mode, FALSE);
550
551 hi = gen_rtx_fmt_ee (ASHIFT, mode, hi, GEN_INT (32));
552 hi = force_reg (mode, hi);
553
554 return gen_rtx_fmt_ee (PLUS, mode, hi, lo);
555 }
556
557 /* Return true if X is a thread-local symbol. */
558
559 static bool
560 riscv_tls_symbol_p (const_rtx x)
561 {
562 return SYMBOL_REF_P (x) && SYMBOL_REF_TLS_MODEL (x) != 0;
563 }
564
565 /* Return true if symbol X binds locally. */
566
567 static bool
568 riscv_symbol_binds_local_p (const_rtx x)
569 {
570 if (SYMBOL_REF_P (x))
571 return (SYMBOL_REF_DECL (x)
572 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
573 : SYMBOL_REF_LOCAL_P (x));
574 else
575 return false;
576 }
577
578 /* Return the method that should be used to access SYMBOL_REF or
579 LABEL_REF X. */
580
581 static enum riscv_symbol_type
582 riscv_classify_symbol (const_rtx x)
583 {
584 if (riscv_tls_symbol_p (x))
585 return SYMBOL_TLS;
586
587 if (GET_CODE (x) == SYMBOL_REF && flag_pic && !riscv_symbol_binds_local_p (x))
588 return SYMBOL_GOT_DISP;
589
590 return riscv_cmodel == CM_MEDLOW ? SYMBOL_ABSOLUTE : SYMBOL_PCREL;
591 }
592
593 /* Classify the base of symbolic expression X. */
594
595 enum riscv_symbol_type
596 riscv_classify_symbolic_expression (rtx x)
597 {
598 rtx offset;
599
600 split_const (x, &x, &offset);
601 if (UNSPEC_ADDRESS_P (x))
602 return UNSPEC_ADDRESS_TYPE (x);
603
604 return riscv_classify_symbol (x);
605 }
606
607 /* Return true if X is a symbolic constant. If it is, store the type of
608 the symbol in *SYMBOL_TYPE. */
609
610 bool
611 riscv_symbolic_constant_p (rtx x, enum riscv_symbol_type *symbol_type)
612 {
613 rtx offset;
614
615 split_const (x, &x, &offset);
616 if (UNSPEC_ADDRESS_P (x))
617 {
618 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
619 x = UNSPEC_ADDRESS (x);
620 }
621 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
622 *symbol_type = riscv_classify_symbol (x);
623 else
624 return false;
625
626 if (offset == const0_rtx)
627 return true;
628
629 /* Nonzero offsets are only valid for references that don't use the GOT. */
630 switch (*symbol_type)
631 {
632 case SYMBOL_ABSOLUTE:
633 case SYMBOL_PCREL:
634 case SYMBOL_TLS_LE:
635 /* GAS rejects offsets outside the range [-2^31, 2^31-1]. */
636 return sext_hwi (INTVAL (offset), 32) == INTVAL (offset);
637
638 default:
639 return false;
640 }
641 }
642
643 /* Returns the number of instructions necessary to reference a symbol. */
644
645 static int riscv_symbol_insns (enum riscv_symbol_type type)
646 {
647 switch (type)
648 {
649 case SYMBOL_TLS: return 0; /* Depends on the TLS model. */
650 case SYMBOL_ABSOLUTE: return 2; /* LUI + the reference. */
651 case SYMBOL_PCREL: return 2; /* AUIPC + the reference. */
652 case SYMBOL_TLS_LE: return 3; /* LUI + ADD TP + the reference. */
653 case SYMBOL_GOT_DISP: return 3; /* AUIPC + LD GOT + the reference. */
654 default: gcc_unreachable ();
655 }
656 }
657
658 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
659
660 static bool
661 riscv_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
662 {
663 return riscv_const_insns (x) > 0;
664 }
665
666 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
667
668 static bool
669 riscv_cannot_force_const_mem (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
670 {
671 enum riscv_symbol_type type;
672 rtx base, offset;
673
674 /* There is no assembler syntax for expressing an address-sized
675 high part. */
676 if (GET_CODE (x) == HIGH)
677 return true;
678
679 split_const (x, &base, &offset);
680 if (riscv_symbolic_constant_p (base, &type))
681 {
682 /* As an optimization, don't spill symbolic constants that are as
683 cheap to rematerialize as to access in the constant pool. */
684 if (SMALL_OPERAND (INTVAL (offset)) && riscv_symbol_insns (type) > 0)
685 return true;
686
687 /* As an optimization, avoid needlessly generate dynamic relocations. */
688 if (flag_pic)
689 return true;
690 }
691
692 /* TLS symbols must be computed by riscv_legitimize_move. */
693 if (tls_referenced_p (x))
694 return true;
695
696 return false;
697 }
698
699 /* Return true if register REGNO is a valid base register for mode MODE.
700 STRICT_P is true if REG_OK_STRICT is in effect. */
701
702 int
703 riscv_regno_mode_ok_for_base_p (int regno,
704 machine_mode mode ATTRIBUTE_UNUSED,
705 bool strict_p)
706 {
707 if (!HARD_REGISTER_NUM_P (regno))
708 {
709 if (!strict_p)
710 return true;
711 regno = reg_renumber[regno];
712 }
713
714 /* These fake registers will be eliminated to either the stack or
715 hard frame pointer, both of which are usually valid base registers.
716 Reload deals with the cases where the eliminated form isn't valid. */
717 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
718 return true;
719
720 return GP_REG_P (regno);
721 }
722
723 /* Return true if X is a valid base register for mode MODE.
724 STRICT_P is true if REG_OK_STRICT is in effect. */
725
726 static bool
727 riscv_valid_base_register_p (rtx x, machine_mode mode, bool strict_p)
728 {
729 if (!strict_p && GET_CODE (x) == SUBREG)
730 x = SUBREG_REG (x);
731
732 return (REG_P (x)
733 && riscv_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
734 }
735
736 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
737 can address a value of mode MODE. */
738
739 static bool
740 riscv_valid_offset_p (rtx x, machine_mode mode)
741 {
742 /* Check that X is a signed 12-bit number. */
743 if (!const_arith_operand (x, Pmode))
744 return false;
745
746 /* We may need to split multiword moves, so make sure that every word
747 is accessible. */
748 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
749 && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
750 return false;
751
752 return true;
753 }
754
755 /* Should a symbol of type SYMBOL_TYPE should be split in two? */
756
757 bool
758 riscv_split_symbol_type (enum riscv_symbol_type symbol_type)
759 {
760 if (symbol_type == SYMBOL_TLS_LE)
761 return true;
762
763 if (!TARGET_EXPLICIT_RELOCS)
764 return false;
765
766 return symbol_type == SYMBOL_ABSOLUTE || symbol_type == SYMBOL_PCREL;
767 }
768
769 /* Return true if a LO_SUM can address a value of mode MODE when the
770 LO_SUM symbol has type SYM_TYPE. X is the LO_SUM second operand, which
771 is used when the mode is BLKmode. */
772
773 static bool
774 riscv_valid_lo_sum_p (enum riscv_symbol_type sym_type, machine_mode mode,
775 rtx x)
776 {
777 int align, size;
778
779 /* Check that symbols of type SYMBOL_TYPE can be used to access values
780 of mode MODE. */
781 if (riscv_symbol_insns (sym_type) == 0)
782 return false;
783
784 /* Check that there is a known low-part relocation. */
785 if (!riscv_split_symbol_type (sym_type))
786 return false;
787
788 /* We can't tell size or alignment when we have BLKmode, so try extracing a
789 decl from the symbol if possible. */
790 if (mode == BLKmode)
791 {
792 rtx offset;
793
794 /* Extract the symbol from the LO_SUM operand, if any. */
795 split_const (x, &x, &offset);
796
797 /* Might be a CODE_LABEL. We can compute align but not size for that,
798 so don't bother trying to handle it. */
799 if (!SYMBOL_REF_P (x))
800 return false;
801
802 /* Use worst case assumptions if we don't have a SYMBOL_REF_DECL. */
803 align = (SYMBOL_REF_DECL (x)
804 ? DECL_ALIGN (SYMBOL_REF_DECL (x))
805 : 1);
806 size = (SYMBOL_REF_DECL (x) && DECL_SIZE (SYMBOL_REF_DECL (x))
807 ? tree_to_uhwi (DECL_SIZE (SYMBOL_REF_DECL (x)))
808 : 2*BITS_PER_WORD);
809 }
810 else
811 {
812 align = GET_MODE_ALIGNMENT (mode);
813 size = GET_MODE_BITSIZE (mode);
814 }
815
816 /* We may need to split multiword moves, so make sure that each word
817 can be accessed without inducing a carry. */
818 if (size > BITS_PER_WORD
819 && (!TARGET_STRICT_ALIGN || size > align))
820 return false;
821
822 return true;
823 }
824
825 /* Return true if X is a valid address for machine mode MODE. If it is,
826 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
827 effect. */
828
829 static bool
830 riscv_classify_address (struct riscv_address_info *info, rtx x,
831 machine_mode mode, bool strict_p)
832 {
833 switch (GET_CODE (x))
834 {
835 case REG:
836 case SUBREG:
837 info->type = ADDRESS_REG;
838 info->reg = x;
839 info->offset = const0_rtx;
840 return riscv_valid_base_register_p (info->reg, mode, strict_p);
841
842 case PLUS:
843 info->type = ADDRESS_REG;
844 info->reg = XEXP (x, 0);
845 info->offset = XEXP (x, 1);
846 return (riscv_valid_base_register_p (info->reg, mode, strict_p)
847 && riscv_valid_offset_p (info->offset, mode));
848
849 case LO_SUM:
850 info->type = ADDRESS_LO_SUM;
851 info->reg = XEXP (x, 0);
852 info->offset = XEXP (x, 1);
853 /* We have to trust the creator of the LO_SUM to do something vaguely
854 sane. Target-independent code that creates a LO_SUM should also
855 create and verify the matching HIGH. Target-independent code that
856 adds an offset to a LO_SUM must prove that the offset will not
857 induce a carry. Failure to do either of these things would be
858 a bug, and we are not required to check for it here. The RISC-V
859 backend itself should only create LO_SUMs for valid symbolic
860 constants, with the high part being either a HIGH or a copy
861 of _gp. */
862 info->symbol_type
863 = riscv_classify_symbolic_expression (info->offset);
864 return (riscv_valid_base_register_p (info->reg, mode, strict_p)
865 && riscv_valid_lo_sum_p (info->symbol_type, mode, info->offset));
866
867 case CONST_INT:
868 /* Small-integer addresses don't occur very often, but they
869 are legitimate if x0 is a valid base register. */
870 info->type = ADDRESS_CONST_INT;
871 return SMALL_OPERAND (INTVAL (x));
872
873 default:
874 return false;
875 }
876 }
877
878 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
879
880 static bool
881 riscv_legitimate_address_p (machine_mode mode, rtx x, bool strict_p)
882 {
883 struct riscv_address_info addr;
884
885 return riscv_classify_address (&addr, x, mode, strict_p);
886 }
887
888 /* Return true if hard reg REGNO can be used in compressed instructions. */
889
890 static bool
891 riscv_compressed_reg_p (int regno)
892 {
893 /* x8-x15/f8-f15 are compressible registers. */
894 return (TARGET_RVC && (IN_RANGE (regno, GP_REG_FIRST + 8, GP_REG_FIRST + 15)
895 || IN_RANGE (regno, FP_REG_FIRST + 8, FP_REG_FIRST + 15)));
896 }
897
898 /* Return true if x is an unsigned 5-bit immediate scaled by 4. */
899
900 static bool
901 riscv_compressed_lw_offset_p (rtx x)
902 {
903 return (CONST_INT_P (x)
904 && (INTVAL (x) & 3) == 0
905 && IN_RANGE (INTVAL (x), 0, CSW_MAX_OFFSET));
906 }
907
908 /* Return true if load/store from/to address x can be compressed. */
909
910 static bool
911 riscv_compressed_lw_address_p (rtx x)
912 {
913 struct riscv_address_info addr;
914 bool result = riscv_classify_address (&addr, x, GET_MODE (x),
915 reload_completed);
916
917 /* Return false if address is not compressed_reg + small_offset. */
918 if (!result
919 || addr.type != ADDRESS_REG
920 /* Before reload, assume all registers are OK. */
921 || (reload_completed
922 && !riscv_compressed_reg_p (REGNO (addr.reg))
923 && addr.reg != stack_pointer_rtx)
924 || !riscv_compressed_lw_offset_p (addr.offset))
925 return false;
926
927 return result;
928 }
929
930 /* Return the number of instructions needed to load or store a value
931 of mode MODE at address X. Return 0 if X isn't valid for MODE.
932 Assume that multiword moves may need to be split into word moves
933 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
934 enough. */
935
936 int
937 riscv_address_insns (rtx x, machine_mode mode, bool might_split_p)
938 {
939 struct riscv_address_info addr = {};
940 int n = 1;
941
942 if (!riscv_classify_address (&addr, x, mode, false))
943 {
944 /* This could be a pattern from the pic.md file. In which case we want
945 this address to always have a cost of 3 to make it as expensive as the
946 most expensive symbol. This prevents constant propagation from
947 preferring symbols over register plus offset. */
948 return 3;
949 }
950
951 /* BLKmode is used for single unaligned loads and stores and should
952 not count as a multiword mode. */
953 if (mode != BLKmode && might_split_p)
954 n += (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
955
956 if (addr.type == ADDRESS_LO_SUM)
957 n += riscv_symbol_insns (addr.symbol_type) - 1;
958
959 return n;
960 }
961
962 /* Return the number of instructions needed to load constant X.
963 Return 0 if X isn't a valid constant. */
964
965 int
966 riscv_const_insns (rtx x)
967 {
968 enum riscv_symbol_type symbol_type;
969 rtx offset;
970
971 switch (GET_CODE (x))
972 {
973 case HIGH:
974 if (!riscv_symbolic_constant_p (XEXP (x, 0), &symbol_type)
975 || !riscv_split_symbol_type (symbol_type))
976 return 0;
977
978 /* This is simply an LUI. */
979 return 1;
980
981 case CONST_INT:
982 {
983 int cost = riscv_integer_cost (INTVAL (x));
984 /* Force complicated constants to memory. */
985 return cost < 4 ? cost : 0;
986 }
987
988 case CONST_DOUBLE:
989 case CONST_VECTOR:
990 /* We can use x0 to load floating-point zero. */
991 return x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
992
993 case CONST:
994 /* See if we can refer to X directly. */
995 if (riscv_symbolic_constant_p (x, &symbol_type))
996 return riscv_symbol_insns (symbol_type);
997
998 /* Otherwise try splitting the constant into a base and offset. */
999 split_const (x, &x, &offset);
1000 if (offset != 0)
1001 {
1002 int n = riscv_const_insns (x);
1003 if (n != 0)
1004 return n + riscv_integer_cost (INTVAL (offset));
1005 }
1006 return 0;
1007
1008 case SYMBOL_REF:
1009 case LABEL_REF:
1010 return riscv_symbol_insns (riscv_classify_symbol (x));
1011
1012 default:
1013 return 0;
1014 }
1015 }
1016
1017 /* X is a doubleword constant that can be handled by splitting it into
1018 two words and loading each word separately. Return the number of
1019 instructions required to do this. */
1020
1021 int
1022 riscv_split_const_insns (rtx x)
1023 {
1024 unsigned int low, high;
1025
1026 low = riscv_const_insns (riscv_subword (x, false));
1027 high = riscv_const_insns (riscv_subword (x, true));
1028 gcc_assert (low > 0 && high > 0);
1029 return low + high;
1030 }
1031
1032 /* Return the number of instructions needed to implement INSN,
1033 given that it loads from or stores to MEM. */
1034
1035 int
1036 riscv_load_store_insns (rtx mem, rtx_insn *insn)
1037 {
1038 machine_mode mode;
1039 bool might_split_p;
1040 rtx set;
1041
1042 gcc_assert (MEM_P (mem));
1043 mode = GET_MODE (mem);
1044
1045 /* Try to prove that INSN does not need to be split. */
1046 might_split_p = true;
1047 if (GET_MODE_BITSIZE (mode) <= 32)
1048 might_split_p = false;
1049 else if (GET_MODE_BITSIZE (mode) == 64)
1050 {
1051 set = single_set (insn);
1052 if (set && !riscv_split_64bit_move_p (SET_DEST (set), SET_SRC (set)))
1053 might_split_p = false;
1054 }
1055
1056 return riscv_address_insns (XEXP (mem, 0), mode, might_split_p);
1057 }
1058
1059 /* Emit a move from SRC to DEST. Assume that the move expanders can
1060 handle all moves if !can_create_pseudo_p (). The distinction is
1061 important because, unlike emit_move_insn, the move expanders know
1062 how to force Pmode objects into the constant pool even when the
1063 constant pool address is not itself legitimate. */
1064
1065 rtx
1066 riscv_emit_move (rtx dest, rtx src)
1067 {
1068 return (can_create_pseudo_p ()
1069 ? emit_move_insn (dest, src)
1070 : emit_move_insn_1 (dest, src));
1071 }
1072
1073 /* Emit an instruction of the form (set TARGET SRC). */
1074
1075 static rtx
1076 riscv_emit_set (rtx target, rtx src)
1077 {
1078 emit_insn (gen_rtx_SET (target, src));
1079 return target;
1080 }
1081
1082 /* Emit an instruction of the form (set DEST (CODE X Y)). */
1083
1084 static rtx
1085 riscv_emit_binary (enum rtx_code code, rtx dest, rtx x, rtx y)
1086 {
1087 return riscv_emit_set (dest, gen_rtx_fmt_ee (code, GET_MODE (dest), x, y));
1088 }
1089
1090 /* Compute (CODE X Y) and store the result in a new register
1091 of mode MODE. Return that new register. */
1092
1093 static rtx
1094 riscv_force_binary (machine_mode mode, enum rtx_code code, rtx x, rtx y)
1095 {
1096 return riscv_emit_binary (code, gen_reg_rtx (mode), x, y);
1097 }
1098
1099 static rtx
1100 riscv_swap_instruction (rtx inst)
1101 {
1102 gcc_assert (GET_MODE (inst) == SImode);
1103 if (BYTES_BIG_ENDIAN)
1104 inst = expand_unop (SImode, bswap_optab, inst, gen_reg_rtx (SImode), 1);
1105 return inst;
1106 }
1107
1108 /* Copy VALUE to a register and return that register. If new pseudos
1109 are allowed, copy it into a new register, otherwise use DEST. */
1110
1111 static rtx
1112 riscv_force_temporary (rtx dest, rtx value, bool in_splitter)
1113 {
1114 /* We can't call gen_reg_rtx from a splitter, because this might realloc
1115 the regno_reg_rtx array, which would invalidate reg rtx pointers in the
1116 combine undo buffer. */
1117 if (can_create_pseudo_p () && !in_splitter)
1118 return force_reg (Pmode, value);
1119 else
1120 {
1121 riscv_emit_move (dest, value);
1122 return dest;
1123 }
1124 }
1125
1126 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
1127 then add CONST_INT OFFSET to the result. */
1128
1129 static rtx
1130 riscv_unspec_address_offset (rtx base, rtx offset,
1131 enum riscv_symbol_type symbol_type)
1132 {
1133 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
1134 UNSPEC_ADDRESS_FIRST + symbol_type);
1135 if (offset != const0_rtx)
1136 base = gen_rtx_PLUS (Pmode, base, offset);
1137 return gen_rtx_CONST (Pmode, base);
1138 }
1139
1140 /* Return an UNSPEC address with underlying address ADDRESS and symbol
1141 type SYMBOL_TYPE. */
1142
1143 rtx
1144 riscv_unspec_address (rtx address, enum riscv_symbol_type symbol_type)
1145 {
1146 rtx base, offset;
1147
1148 split_const (address, &base, &offset);
1149 return riscv_unspec_address_offset (base, offset, symbol_type);
1150 }
1151
1152 /* If OP is an UNSPEC address, return the address to which it refers,
1153 otherwise return OP itself. */
1154
1155 static rtx
1156 riscv_strip_unspec_address (rtx op)
1157 {
1158 rtx base, offset;
1159
1160 split_const (op, &base, &offset);
1161 if (UNSPEC_ADDRESS_P (base))
1162 op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
1163 return op;
1164 }
1165
1166 /* If riscv_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
1167 high part to BASE and return the result. Just return BASE otherwise.
1168 TEMP is as for riscv_force_temporary.
1169
1170 The returned expression can be used as the first operand to a LO_SUM. */
1171
1172 static rtx
1173 riscv_unspec_offset_high (rtx temp, rtx addr, enum riscv_symbol_type symbol_type)
1174 {
1175 addr = gen_rtx_HIGH (Pmode, riscv_unspec_address (addr, symbol_type));
1176 return riscv_force_temporary (temp, addr, FALSE);
1177 }
1178
1179 /* Load an entry from the GOT for a TLS GD access. */
1180
1181 static rtx riscv_got_load_tls_gd (rtx dest, rtx sym)
1182 {
1183 if (Pmode == DImode)
1184 return gen_got_load_tls_gddi (dest, sym);
1185 else
1186 return gen_got_load_tls_gdsi (dest, sym);
1187 }
1188
1189 /* Load an entry from the GOT for a TLS IE access. */
1190
1191 static rtx riscv_got_load_tls_ie (rtx dest, rtx sym)
1192 {
1193 if (Pmode == DImode)
1194 return gen_got_load_tls_iedi (dest, sym);
1195 else
1196 return gen_got_load_tls_iesi (dest, sym);
1197 }
1198
1199 /* Add in the thread pointer for a TLS LE access. */
1200
1201 static rtx riscv_tls_add_tp_le (rtx dest, rtx base, rtx sym)
1202 {
1203 rtx tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
1204 if (Pmode == DImode)
1205 return gen_tls_add_tp_ledi (dest, base, tp, sym);
1206 else
1207 return gen_tls_add_tp_lesi (dest, base, tp, sym);
1208 }
1209
1210 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
1211 it appears in a MEM of that mode. Return true if ADDR is a legitimate
1212 constant in that context and can be split into high and low parts.
1213 If so, and if LOW_OUT is nonnull, emit the high part and store the
1214 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
1215
1216 TEMP is as for riscv_force_temporary and is used to load the high
1217 part into a register.
1218
1219 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
1220 a legitimize SET_SRC for an .md pattern, otherwise the low part
1221 is guaranteed to be a legitimate address for mode MODE. */
1222
1223 bool
1224 riscv_split_symbol (rtx temp, rtx addr, machine_mode mode, rtx *low_out,
1225 bool in_splitter)
1226 {
1227 enum riscv_symbol_type symbol_type;
1228
1229 if ((GET_CODE (addr) == HIGH && mode == MAX_MACHINE_MODE)
1230 || !riscv_symbolic_constant_p (addr, &symbol_type)
1231 || riscv_symbol_insns (symbol_type) == 0
1232 || !riscv_split_symbol_type (symbol_type))
1233 return false;
1234
1235 if (low_out)
1236 switch (symbol_type)
1237 {
1238 case SYMBOL_ABSOLUTE:
1239 {
1240 rtx high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
1241 high = riscv_force_temporary (temp, high, in_splitter);
1242 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
1243 }
1244 break;
1245
1246 case SYMBOL_PCREL:
1247 {
1248 static unsigned seqno;
1249 char buf[32];
1250 rtx label;
1251
1252 ssize_t bytes = snprintf (buf, sizeof (buf), ".LA%u", seqno);
1253 gcc_assert ((size_t) bytes < sizeof (buf));
1254
1255 label = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
1256 SYMBOL_REF_FLAGS (label) |= SYMBOL_FLAG_LOCAL;
1257 /* ??? Ugly hack to make weak symbols work. May need to change the
1258 RTL for the auipc and/or low patterns to get a better fix for
1259 this. */
1260 if (! nonzero_address_p (addr))
1261 SYMBOL_REF_WEAK (label) = 1;
1262
1263 if (temp == NULL)
1264 temp = gen_reg_rtx (Pmode);
1265
1266 if (Pmode == DImode)
1267 emit_insn (gen_auipcdi (temp, copy_rtx (addr), GEN_INT (seqno)));
1268 else
1269 emit_insn (gen_auipcsi (temp, copy_rtx (addr), GEN_INT (seqno)));
1270
1271 *low_out = gen_rtx_LO_SUM (Pmode, temp, label);
1272
1273 seqno++;
1274 }
1275 break;
1276
1277 default:
1278 gcc_unreachable ();
1279 }
1280
1281 return true;
1282 }
1283
1284 /* Return a legitimate address for REG + OFFSET. TEMP is as for
1285 riscv_force_temporary; it is only needed when OFFSET is not a
1286 SMALL_OPERAND. */
1287
1288 static rtx
1289 riscv_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
1290 {
1291 if (!SMALL_OPERAND (offset))
1292 {
1293 rtx high;
1294
1295 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
1296 The addition inside the macro CONST_HIGH_PART may cause an
1297 overflow, so we need to force a sign-extension check. */
1298 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
1299 offset = CONST_LOW_PART (offset);
1300 high = riscv_force_temporary (temp, high, FALSE);
1301 reg = riscv_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg),
1302 FALSE);
1303 }
1304 return plus_constant (Pmode, reg, offset);
1305 }
1306
1307 /* The __tls_get_attr symbol. */
1308 static GTY(()) rtx riscv_tls_symbol;
1309
1310 /* Return an instruction sequence that calls __tls_get_addr. SYM is
1311 the TLS symbol we are referencing and TYPE is the symbol type to use
1312 (either global dynamic or local dynamic). RESULT is an RTX for the
1313 return value location. */
1314
1315 static rtx_insn *
1316 riscv_call_tls_get_addr (rtx sym, rtx result)
1317 {
1318 rtx a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST), func;
1319 rtx_insn *insn;
1320
1321 if (!riscv_tls_symbol)
1322 riscv_tls_symbol = init_one_libfunc ("__tls_get_addr");
1323 func = gen_rtx_MEM (FUNCTION_MODE, riscv_tls_symbol);
1324
1325 start_sequence ();
1326
1327 emit_insn (riscv_got_load_tls_gd (a0, sym));
1328 insn = emit_call_insn (gen_call_value (result, func, const0_rtx, NULL));
1329 RTL_CONST_CALL_P (insn) = 1;
1330 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
1331 insn = get_insns ();
1332
1333 end_sequence ();
1334
1335 return insn;
1336 }
1337
1338 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
1339 its address. The return value will be both a valid address and a valid
1340 SET_SRC (either a REG or a LO_SUM). */
1341
1342 static rtx
1343 riscv_legitimize_tls_address (rtx loc)
1344 {
1345 rtx dest, tp, tmp;
1346 enum tls_model model = SYMBOL_REF_TLS_MODEL (loc);
1347
1348 #if 0
1349 /* TLS copy relocs are now deprecated and should not be used. */
1350 /* Since we support TLS copy relocs, non-PIC TLS accesses may all use LE. */
1351 if (!flag_pic)
1352 model = TLS_MODEL_LOCAL_EXEC;
1353 #endif
1354
1355 switch (model)
1356 {
1357 case TLS_MODEL_LOCAL_DYNAMIC:
1358 /* Rely on section anchors for the optimization that LDM TLS
1359 provides. The anchor's address is loaded with GD TLS. */
1360 case TLS_MODEL_GLOBAL_DYNAMIC:
1361 tmp = gen_rtx_REG (Pmode, GP_RETURN);
1362 dest = gen_reg_rtx (Pmode);
1363 emit_libcall_block (riscv_call_tls_get_addr (loc, tmp), dest, tmp, loc);
1364 break;
1365
1366 case TLS_MODEL_INITIAL_EXEC:
1367 /* la.tls.ie; tp-relative add */
1368 tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
1369 tmp = gen_reg_rtx (Pmode);
1370 emit_insn (riscv_got_load_tls_ie (tmp, loc));
1371 dest = gen_reg_rtx (Pmode);
1372 emit_insn (gen_add3_insn (dest, tmp, tp));
1373 break;
1374
1375 case TLS_MODEL_LOCAL_EXEC:
1376 tmp = riscv_unspec_offset_high (NULL, loc, SYMBOL_TLS_LE);
1377 dest = gen_reg_rtx (Pmode);
1378 emit_insn (riscv_tls_add_tp_le (dest, tmp, loc));
1379 dest = gen_rtx_LO_SUM (Pmode, dest,
1380 riscv_unspec_address (loc, SYMBOL_TLS_LE));
1381 break;
1382
1383 default:
1384 gcc_unreachable ();
1385 }
1386 return dest;
1387 }
1388 \f
1389 /* If X is not a valid address for mode MODE, force it into a register. */
1390
1391 static rtx
1392 riscv_force_address (rtx x, machine_mode mode)
1393 {
1394 if (!riscv_legitimate_address_p (mode, x, false))
1395 x = force_reg (Pmode, x);
1396 return x;
1397 }
1398
1399 /* Modify base + offset so that offset fits within a compressed load/store insn
1400 and the excess is added to base. */
1401
1402 static rtx
1403 riscv_shorten_lw_offset (rtx base, HOST_WIDE_INT offset)
1404 {
1405 rtx addr, high;
1406 /* Leave OFFSET as an unsigned 5-bit offset scaled by 4 and put the excess
1407 into HIGH. */
1408 high = GEN_INT (offset & ~CSW_MAX_OFFSET);
1409 offset &= CSW_MAX_OFFSET;
1410 if (!SMALL_OPERAND (INTVAL (high)))
1411 high = force_reg (Pmode, high);
1412 base = force_reg (Pmode, gen_rtx_PLUS (Pmode, high, base));
1413 addr = plus_constant (Pmode, base, offset);
1414 return addr;
1415 }
1416
1417 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
1418 be legitimized in a way that the generic machinery might not expect,
1419 return a new address, otherwise return NULL. MODE is the mode of
1420 the memory being accessed. */
1421
1422 static rtx
1423 riscv_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
1424 machine_mode mode)
1425 {
1426 rtx addr;
1427
1428 if (riscv_tls_symbol_p (x))
1429 return riscv_legitimize_tls_address (x);
1430
1431 /* See if the address can split into a high part and a LO_SUM. */
1432 if (riscv_split_symbol (NULL, x, mode, &addr, FALSE))
1433 return riscv_force_address (addr, mode);
1434
1435 /* Handle BASE + OFFSET. */
1436 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1))
1437 && INTVAL (XEXP (x, 1)) != 0)
1438 {
1439 rtx base = XEXP (x, 0);
1440 HOST_WIDE_INT offset = INTVAL (XEXP (x, 1));
1441
1442 if (!riscv_valid_base_register_p (base, mode, false))
1443 base = copy_to_mode_reg (Pmode, base);
1444 if (optimize_function_for_size_p (cfun)
1445 && (strcmp (current_pass->name, "shorten_memrefs") == 0)
1446 && mode == SImode)
1447 /* Convert BASE + LARGE_OFFSET into NEW_BASE + SMALL_OFFSET to allow
1448 possible compressed load/store. */
1449 addr = riscv_shorten_lw_offset (base, offset);
1450 else
1451 addr = riscv_add_offset (NULL, base, offset);
1452 return riscv_force_address (addr, mode);
1453 }
1454
1455 return x;
1456 }
1457
1458 /* Load VALUE into DEST. TEMP is as for riscv_force_temporary. ORIG_MODE
1459 is the original src mode before promotion. */
1460
1461 void
1462 riscv_move_integer (rtx temp, rtx dest, HOST_WIDE_INT value,
1463 machine_mode orig_mode, bool in_splitter)
1464 {
1465 struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS];
1466 machine_mode mode;
1467 int i, num_ops;
1468 rtx x;
1469
1470 /* We can't call gen_reg_rtx from a splitter, because this might realloc
1471 the regno_reg_rtx array, which would invalidate reg rtx pointers in the
1472 combine undo buffer. */
1473 bool can_create_pseudo = can_create_pseudo_p () && ! in_splitter;
1474
1475 mode = GET_MODE (dest);
1476 /* We use the original mode for the riscv_build_integer call, because HImode
1477 values are given special treatment. */
1478 num_ops = riscv_build_integer (codes, value, orig_mode);
1479
1480 if (can_create_pseudo && num_ops > 2 /* not a simple constant */
1481 && num_ops >= riscv_split_integer_cost (value))
1482 x = riscv_split_integer (value, mode);
1483 else
1484 {
1485 /* Apply each binary operation to X. */
1486 x = GEN_INT (codes[0].value);
1487
1488 for (i = 1; i < num_ops; i++)
1489 {
1490 if (!can_create_pseudo)
1491 x = riscv_emit_set (temp, x);
1492 else
1493 x = force_reg (mode, x);
1494
1495 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
1496 }
1497 }
1498
1499 riscv_emit_set (dest, x);
1500 }
1501
1502 /* Subroutine of riscv_legitimize_move. Move constant SRC into register
1503 DEST given that SRC satisfies immediate_operand but doesn't satisfy
1504 move_operand. */
1505
1506 static void
1507 riscv_legitimize_const_move (machine_mode mode, rtx dest, rtx src)
1508 {
1509 rtx base, offset;
1510
1511 /* Split moves of big integers into smaller pieces. */
1512 if (splittable_const_int_operand (src, mode))
1513 {
1514 riscv_move_integer (dest, dest, INTVAL (src), mode, FALSE);
1515 return;
1516 }
1517
1518 /* Split moves of symbolic constants into high/low pairs. */
1519 if (riscv_split_symbol (dest, src, MAX_MACHINE_MODE, &src, FALSE))
1520 {
1521 riscv_emit_set (dest, src);
1522 return;
1523 }
1524
1525 /* Generate the appropriate access sequences for TLS symbols. */
1526 if (riscv_tls_symbol_p (src))
1527 {
1528 riscv_emit_move (dest, riscv_legitimize_tls_address (src));
1529 return;
1530 }
1531
1532 /* If we have (const (plus symbol offset)), and that expression cannot
1533 be forced into memory, load the symbol first and add in the offset. Also
1534 prefer to do this even if the constant _can_ be forced into memory, as it
1535 usually produces better code. */
1536 split_const (src, &base, &offset);
1537 if (offset != const0_rtx
1538 && (targetm.cannot_force_const_mem (mode, src) || can_create_pseudo_p ()))
1539 {
1540 base = riscv_force_temporary (dest, base, FALSE);
1541 riscv_emit_move (dest, riscv_add_offset (NULL, base, INTVAL (offset)));
1542 return;
1543 }
1544
1545 src = force_const_mem (mode, src);
1546
1547 /* When using explicit relocs, constant pool references are sometimes
1548 not legitimate addresses. */
1549 riscv_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0), FALSE);
1550 riscv_emit_move (dest, src);
1551 }
1552
1553 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
1554 sequence that is valid. */
1555
1556 bool
1557 riscv_legitimize_move (machine_mode mode, rtx dest, rtx src)
1558 {
1559 /* Expand
1560 (set (reg:QI target) (mem:QI (address)))
1561 to
1562 (set (reg:DI temp) (zero_extend:DI (mem:QI (address))))
1563 (set (reg:QI target) (subreg:QI (reg:DI temp) 0))
1564 with auto-sign/zero extend. */
1565 if (GET_MODE_CLASS (mode) == MODE_INT
1566 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
1567 && can_create_pseudo_p ()
1568 && MEM_P (src))
1569 {
1570 rtx temp_reg;
1571 int zero_extend_p;
1572
1573 temp_reg = gen_reg_rtx (word_mode);
1574 zero_extend_p = (LOAD_EXTEND_OP (mode) == ZERO_EXTEND);
1575 emit_insn (gen_extend_insn (temp_reg, src, word_mode, mode,
1576 zero_extend_p));
1577 riscv_emit_move (dest, gen_lowpart (mode, temp_reg));
1578 return true;
1579 }
1580
1581 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
1582 {
1583 rtx reg;
1584
1585 if (GET_CODE (src) == CONST_INT)
1586 {
1587 /* Apply the equivalent of PROMOTE_MODE here for constants to
1588 improve cse. */
1589 machine_mode promoted_mode = mode;
1590 if (GET_MODE_CLASS (mode) == MODE_INT
1591 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
1592 promoted_mode = word_mode;
1593
1594 if (splittable_const_int_operand (src, mode))
1595 {
1596 reg = gen_reg_rtx (promoted_mode);
1597 riscv_move_integer (reg, reg, INTVAL (src), mode, FALSE);
1598 }
1599 else
1600 reg = force_reg (promoted_mode, src);
1601
1602 if (promoted_mode != mode)
1603 reg = gen_lowpart (mode, reg);
1604 }
1605 else
1606 reg = force_reg (mode, src);
1607 riscv_emit_move (dest, reg);
1608 return true;
1609 }
1610
1611 /* We need to deal with constants that would be legitimate
1612 immediate_operands but aren't legitimate move_operands. */
1613 if (CONSTANT_P (src) && !move_operand (src, mode))
1614 {
1615 riscv_legitimize_const_move (mode, dest, src);
1616 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
1617 return true;
1618 }
1619
1620 /* RISC-V GCC may generate non-legitimate address due to we provide some
1621 pattern for optimize access PIC local symbol and it's make GCC generate
1622 unrecognizable instruction during optmizing. */
1623
1624 if (MEM_P (dest) && !riscv_legitimate_address_p (mode, XEXP (dest, 0),
1625 reload_completed))
1626 {
1627 XEXP (dest, 0) = riscv_force_address (XEXP (dest, 0), mode);
1628 }
1629
1630 if (MEM_P (src) && !riscv_legitimate_address_p (mode, XEXP (src, 0),
1631 reload_completed))
1632 {
1633 XEXP (src, 0) = riscv_force_address (XEXP (src, 0), mode);
1634 }
1635
1636 return false;
1637 }
1638
1639 /* Return true if there is an instruction that implements CODE and accepts
1640 X as an immediate operand. */
1641
1642 static int
1643 riscv_immediate_operand_p (int code, HOST_WIDE_INT x)
1644 {
1645 switch (code)
1646 {
1647 case ASHIFT:
1648 case ASHIFTRT:
1649 case LSHIFTRT:
1650 /* All shift counts are truncated to a valid constant. */
1651 return true;
1652
1653 case AND:
1654 case IOR:
1655 case XOR:
1656 case PLUS:
1657 case LT:
1658 case LTU:
1659 /* These instructions take 12-bit signed immediates. */
1660 return SMALL_OPERAND (x);
1661
1662 case LE:
1663 /* We add 1 to the immediate and use SLT. */
1664 return SMALL_OPERAND (x + 1);
1665
1666 case LEU:
1667 /* Likewise SLTU, but reject the always-true case. */
1668 return SMALL_OPERAND (x + 1) && x + 1 != 0;
1669
1670 case GE:
1671 case GEU:
1672 /* We can emulate an immediate of 1 by using GT/GTU against x0. */
1673 return x == 1;
1674
1675 default:
1676 /* By default assume that x0 can be used for 0. */
1677 return x == 0;
1678 }
1679 }
1680
1681 /* Return the cost of binary operation X, given that the instruction
1682 sequence for a word-sized or smaller operation takes SIGNLE_INSNS
1683 instructions and that the sequence of a double-word operation takes
1684 DOUBLE_INSNS instructions. */
1685
1686 static int
1687 riscv_binary_cost (rtx x, int single_insns, int double_insns)
1688 {
1689 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
1690 return COSTS_N_INSNS (double_insns);
1691 return COSTS_N_INSNS (single_insns);
1692 }
1693
1694 /* Return the cost of sign- or zero-extending OP. */
1695
1696 static int
1697 riscv_extend_cost (rtx op, bool unsigned_p)
1698 {
1699 if (MEM_P (op))
1700 return 0;
1701
1702 if (unsigned_p && GET_MODE (op) == QImode)
1703 /* We can use ANDI. */
1704 return COSTS_N_INSNS (1);
1705
1706 /* ZBA provide zext.w. */
1707 if (TARGET_ZBA && TARGET_64BIT && unsigned_p && GET_MODE (op) == SImode)
1708 return COSTS_N_INSNS (1);
1709
1710 /* ZBB provide zext.h, sext.b and sext.h. */
1711 if (TARGET_ZBB)
1712 {
1713 if (!unsigned_p && GET_MODE (op) == QImode)
1714 return COSTS_N_INSNS (1);
1715
1716 if (GET_MODE (op) == HImode)
1717 return COSTS_N_INSNS (1);
1718 }
1719
1720 if (!unsigned_p && GET_MODE (op) == SImode)
1721 /* We can use SEXT.W. */
1722 return COSTS_N_INSNS (1);
1723
1724 /* We need to use a shift left and a shift right. */
1725 return COSTS_N_INSNS (2);
1726 }
1727
1728 /* Implement TARGET_RTX_COSTS. */
1729
1730 #define SINGLE_SHIFT_COST 1
1731
1732 static bool
1733 riscv_rtx_costs (rtx x, machine_mode mode, int outer_code, int opno ATTRIBUTE_UNUSED,
1734 int *total, bool speed)
1735 {
1736 bool float_mode_p = FLOAT_MODE_P (mode);
1737 int cost;
1738
1739 switch (GET_CODE (x))
1740 {
1741 case CONST_INT:
1742 if (riscv_immediate_operand_p (outer_code, INTVAL (x)))
1743 {
1744 *total = 0;
1745 return true;
1746 }
1747 /* Fall through. */
1748
1749 case SYMBOL_REF:
1750 case LABEL_REF:
1751 case CONST_DOUBLE:
1752 case CONST:
1753 if ((cost = riscv_const_insns (x)) > 0)
1754 {
1755 /* If the constant is likely to be stored in a GPR, SETs of
1756 single-insn constants are as cheap as register sets; we
1757 never want to CSE them. */
1758 if (cost == 1 && outer_code == SET)
1759 *total = 0;
1760 /* When we load a constant more than once, it usually is better
1761 to duplicate the last operation in the sequence than to CSE
1762 the constant itself. */
1763 else if (outer_code == SET || GET_MODE (x) == VOIDmode)
1764 *total = COSTS_N_INSNS (1);
1765 }
1766 else /* The instruction will be fetched from the constant pool. */
1767 *total = COSTS_N_INSNS (riscv_symbol_insns (SYMBOL_ABSOLUTE));
1768 return true;
1769
1770 case MEM:
1771 /* If the address is legitimate, return the number of
1772 instructions it needs. */
1773 if ((cost = riscv_address_insns (XEXP (x, 0), mode, true)) > 0)
1774 {
1775 /* When optimizing for size, make uncompressible 32-bit addresses
1776 more expensive so that compressible 32-bit addresses are
1777 preferred. */
1778 if (TARGET_RVC && !speed && riscv_mshorten_memrefs && mode == SImode
1779 && !riscv_compressed_lw_address_p (XEXP (x, 0)))
1780 cost++;
1781
1782 *total = COSTS_N_INSNS (cost + tune_param->memory_cost);
1783 return true;
1784 }
1785 /* Otherwise use the default handling. */
1786 return false;
1787
1788 case NOT:
1789 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
1790 return false;
1791
1792 case AND:
1793 /* slli.uw pattern for zba. */
1794 if (TARGET_ZBA && TARGET_64BIT && mode == DImode
1795 && GET_CODE (XEXP (x, 0)) == ASHIFT)
1796 {
1797 rtx and_rhs = XEXP (x, 1);
1798 rtx ashift_lhs = XEXP (XEXP (x, 0), 0);
1799 rtx ashift_rhs = XEXP (XEXP (x, 0), 1);
1800 if (REG_P (ashift_lhs)
1801 && CONST_INT_P (ashift_rhs)
1802 && CONST_INT_P (and_rhs)
1803 && ((INTVAL (and_rhs) >> INTVAL (ashift_rhs)) == 0xffffffff))
1804 *total = COSTS_N_INSNS (1);
1805 return true;
1806 }
1807 gcc_fallthrough ();
1808 case IOR:
1809 case XOR:
1810 /* orn, andn and xorn pattern for zbb. */
1811 if (TARGET_ZBB
1812 && GET_CODE (XEXP (x, 0)) == NOT)
1813 {
1814 *total = riscv_binary_cost (x, 1, 2);
1815 return true;
1816 }
1817 /* Double-word operations use two single-word operations. */
1818 *total = riscv_binary_cost (x, 1, 2);
1819 return false;
1820
1821 case ZERO_EXTRACT:
1822 /* This is an SImode shift. */
1823 if (outer_code == SET
1824 && CONST_INT_P (XEXP (x, 1))
1825 && CONST_INT_P (XEXP (x, 2))
1826 && (INTVAL (XEXP (x, 2)) > 0)
1827 && (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (x, 2)) == 32))
1828 {
1829 *total = COSTS_N_INSNS (SINGLE_SHIFT_COST);
1830 return true;
1831 }
1832 return false;
1833
1834 case ASHIFT:
1835 case ASHIFTRT:
1836 case LSHIFTRT:
1837 *total = riscv_binary_cost (x, SINGLE_SHIFT_COST,
1838 CONSTANT_P (XEXP (x, 1)) ? 4 : 9);
1839 return false;
1840
1841 case ABS:
1842 *total = COSTS_N_INSNS (float_mode_p ? 1 : 3);
1843 return false;
1844
1845 case LO_SUM:
1846 *total = set_src_cost (XEXP (x, 0), mode, speed);
1847 return true;
1848
1849 case LT:
1850 /* This is an SImode shift. */
1851 if (outer_code == SET && GET_MODE (x) == DImode
1852 && GET_MODE (XEXP (x, 0)) == SImode)
1853 {
1854 *total = COSTS_N_INSNS (SINGLE_SHIFT_COST);
1855 return true;
1856 }
1857 /* Fall through. */
1858 case LTU:
1859 case LE:
1860 case LEU:
1861 case GT:
1862 case GTU:
1863 case GE:
1864 case GEU:
1865 case EQ:
1866 case NE:
1867 /* Branch comparisons have VOIDmode, so use the first operand's
1868 mode instead. */
1869 mode = GET_MODE (XEXP (x, 0));
1870 if (float_mode_p)
1871 *total = tune_param->fp_add[mode == DFmode];
1872 else
1873 *total = riscv_binary_cost (x, 1, 3);
1874 return false;
1875
1876 case UNORDERED:
1877 case ORDERED:
1878 /* (FEQ(A, A) & FEQ(B, B)) compared against 0. */
1879 mode = GET_MODE (XEXP (x, 0));
1880 *total = tune_param->fp_add[mode == DFmode] + COSTS_N_INSNS (2);
1881 return false;
1882
1883 case UNEQ:
1884 /* (FEQ(A, A) & FEQ(B, B)) compared against FEQ(A, B). */
1885 mode = GET_MODE (XEXP (x, 0));
1886 *total = tune_param->fp_add[mode == DFmode] + COSTS_N_INSNS (3);
1887 return false;
1888
1889 case LTGT:
1890 /* (FLT(A, A) || FGT(B, B)). */
1891 mode = GET_MODE (XEXP (x, 0));
1892 *total = tune_param->fp_add[mode == DFmode] + COSTS_N_INSNS (2);
1893 return false;
1894
1895 case UNGE:
1896 case UNGT:
1897 case UNLE:
1898 case UNLT:
1899 /* FLT or FLE, but guarded by an FFLAGS read and write. */
1900 mode = GET_MODE (XEXP (x, 0));
1901 *total = tune_param->fp_add[mode == DFmode] + COSTS_N_INSNS (4);
1902 return false;
1903
1904 case MINUS:
1905 case PLUS:
1906 /* add.uw pattern for zba. */
1907 if (TARGET_ZBA
1908 && (TARGET_64BIT && (mode == DImode))
1909 && GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
1910 && REG_P (XEXP (XEXP (x, 0), 0))
1911 && GET_MODE (XEXP (XEXP (x, 0), 0)) == SImode)
1912 {
1913 *total = COSTS_N_INSNS (1);
1914 return true;
1915 }
1916 /* shNadd pattern for zba. */
1917 if (TARGET_ZBA
1918 && ((!TARGET_64BIT && (mode == SImode)) ||
1919 (TARGET_64BIT && (mode == DImode)))
1920 && (GET_CODE (XEXP (x, 0)) == ASHIFT)
1921 && REG_P (XEXP (XEXP (x, 0), 0))
1922 && CONST_INT_P (XEXP (XEXP (x, 0), 0))
1923 && IN_RANGE (INTVAL (XEXP (XEXP (x, 0), 0)), 1, 3))
1924 {
1925 *total = COSTS_N_INSNS (1);
1926 return true;
1927 }
1928 /* shNadd.uw pattern for zba.
1929 [(set (match_operand:DI 0 "register_operand" "=r")
1930 (plus:DI
1931 (and:DI (ashift:DI (match_operand:DI 1 "register_operand" "r")
1932 (match_operand:QI 2 "immediate_operand" "I"))
1933 (match_operand 3 "immediate_operand" ""))
1934 (match_operand:DI 4 "register_operand" "r")))]
1935 "TARGET_64BIT && TARGET_ZBA
1936 && (INTVAL (operands[2]) >= 1) && (INTVAL (operands[2]) <= 3)
1937 && (INTVAL (operands[3]) >> INTVAL (operands[2])) == 0xffffffff"
1938 */
1939 if (TARGET_ZBA
1940 && (TARGET_64BIT && (mode == DImode))
1941 && (GET_CODE (XEXP (x, 0)) == AND)
1942 && (REG_P (XEXP (x, 1))))
1943 {
1944 do {
1945 rtx and_lhs = XEXP (XEXP (x, 0), 0);
1946 rtx and_rhs = XEXP (XEXP (x, 0), 1);
1947 if (GET_CODE (and_lhs) != ASHIFT)
1948 break;
1949 if (!CONST_INT_P (and_rhs))
1950 break;
1951
1952 rtx ashift_lhs = XEXP (and_lhs, 0);
1953 rtx ashift_rhs = XEXP (and_lhs, 1);
1954
1955 if (!CONST_INT_P (ashift_rhs)
1956 || !IN_RANGE (INTVAL (ashift_rhs), 1, 3))
1957 break;
1958
1959 if (CONST_INT_P (and_rhs)
1960 && ((INTVAL (and_rhs) >> INTVAL (ashift_rhs)) == 0xffffffff))
1961 {
1962 *total = COSTS_N_INSNS (1);
1963 return true;
1964 }
1965 } while (false);
1966 }
1967
1968 if (float_mode_p)
1969 *total = tune_param->fp_add[mode == DFmode];
1970 else
1971 *total = riscv_binary_cost (x, 1, 4);
1972 return false;
1973
1974 case NEG:
1975 {
1976 rtx op = XEXP (x, 0);
1977 if (GET_CODE (op) == FMA && !HONOR_SIGNED_ZEROS (mode))
1978 {
1979 *total = (tune_param->fp_mul[mode == DFmode]
1980 + set_src_cost (XEXP (op, 0), mode, speed)
1981 + set_src_cost (XEXP (op, 1), mode, speed)
1982 + set_src_cost (XEXP (op, 2), mode, speed));
1983 return true;
1984 }
1985 }
1986
1987 if (float_mode_p)
1988 *total = tune_param->fp_add[mode == DFmode];
1989 else
1990 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
1991 return false;
1992
1993 case MULT:
1994 if (float_mode_p)
1995 *total = tune_param->fp_mul[mode == DFmode];
1996 else if (!TARGET_MUL)
1997 /* Estimate the cost of a library call. */
1998 *total = COSTS_N_INSNS (speed ? 32 : 6);
1999 else if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
2000 *total = 3 * tune_param->int_mul[0] + COSTS_N_INSNS (2);
2001 else if (!speed)
2002 *total = COSTS_N_INSNS (1);
2003 else
2004 *total = tune_param->int_mul[mode == DImode];
2005 return false;
2006
2007 case DIV:
2008 case SQRT:
2009 case MOD:
2010 if (float_mode_p)
2011 {
2012 *total = tune_param->fp_div[mode == DFmode];
2013 return false;
2014 }
2015 /* Fall through. */
2016
2017 case UDIV:
2018 case UMOD:
2019 if (!TARGET_DIV)
2020 /* Estimate the cost of a library call. */
2021 *total = COSTS_N_INSNS (speed ? 32 : 6);
2022 else if (speed)
2023 *total = tune_param->int_div[mode == DImode];
2024 else
2025 *total = COSTS_N_INSNS (1);
2026 return false;
2027
2028 case ZERO_EXTEND:
2029 /* This is an SImode shift. */
2030 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT)
2031 {
2032 *total = COSTS_N_INSNS (SINGLE_SHIFT_COST);
2033 return true;
2034 }
2035 /* Fall through. */
2036 case SIGN_EXTEND:
2037 *total = riscv_extend_cost (XEXP (x, 0), GET_CODE (x) == ZERO_EXTEND);
2038 return false;
2039
2040 case FLOAT:
2041 case UNSIGNED_FLOAT:
2042 case FIX:
2043 case FLOAT_EXTEND:
2044 case FLOAT_TRUNCATE:
2045 *total = tune_param->fp_add[mode == DFmode];
2046 return false;
2047
2048 case FMA:
2049 *total = (tune_param->fp_mul[mode == DFmode]
2050 + set_src_cost (XEXP (x, 0), mode, speed)
2051 + set_src_cost (XEXP (x, 1), mode, speed)
2052 + set_src_cost (XEXP (x, 2), mode, speed));
2053 return true;
2054
2055 case UNSPEC:
2056 if (XINT (x, 1) == UNSPEC_AUIPC)
2057 {
2058 /* Make AUIPC cheap to avoid spilling its result to the stack. */
2059 *total = 1;
2060 return true;
2061 }
2062 return false;
2063
2064 default:
2065 return false;
2066 }
2067 }
2068
2069 /* Implement TARGET_ADDRESS_COST. */
2070
2071 static int
2072 riscv_address_cost (rtx addr, machine_mode mode,
2073 addr_space_t as ATTRIBUTE_UNUSED,
2074 bool speed ATTRIBUTE_UNUSED)
2075 {
2076 /* When optimizing for size, make uncompressible 32-bit addresses more
2077 * expensive so that compressible 32-bit addresses are preferred. */
2078 if (TARGET_RVC && !speed && riscv_mshorten_memrefs && mode == SImode
2079 && !riscv_compressed_lw_address_p (addr))
2080 return riscv_address_insns (addr, mode, false) + 1;
2081 return riscv_address_insns (addr, mode, false);
2082 }
2083
2084 /* Return one word of double-word value OP. HIGH_P is true to select the
2085 high part or false to select the low part. */
2086
2087 rtx
2088 riscv_subword (rtx op, bool high_p)
2089 {
2090 unsigned int byte = (high_p != BYTES_BIG_ENDIAN) ? UNITS_PER_WORD : 0;
2091 machine_mode mode = GET_MODE (op);
2092
2093 if (mode == VOIDmode)
2094 mode = TARGET_64BIT ? TImode : DImode;
2095
2096 if (MEM_P (op))
2097 return adjust_address (op, word_mode, byte);
2098
2099 if (REG_P (op))
2100 gcc_assert (!FP_REG_RTX_P (op));
2101
2102 return simplify_gen_subreg (word_mode, op, mode, byte);
2103 }
2104
2105 /* Return true if a 64-bit move from SRC to DEST should be split into two. */
2106
2107 bool
2108 riscv_split_64bit_move_p (rtx dest, rtx src)
2109 {
2110 if (TARGET_64BIT)
2111 return false;
2112
2113 /* Allow FPR <-> FPR and FPR <-> MEM moves, and permit the special case
2114 of zeroing an FPR with FCVT.D.W. */
2115 if (TARGET_DOUBLE_FLOAT
2116 && ((FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
2117 || (FP_REG_RTX_P (dest) && MEM_P (src))
2118 || (FP_REG_RTX_P (src) && MEM_P (dest))
2119 || (FP_REG_RTX_P (dest) && src == CONST0_RTX (GET_MODE (src)))))
2120 return false;
2121
2122 return true;
2123 }
2124
2125 /* Split a doubleword move from SRC to DEST. On 32-bit targets,
2126 this function handles 64-bit moves for which riscv_split_64bit_move_p
2127 holds. For 64-bit targets, this function handles 128-bit moves. */
2128
2129 void
2130 riscv_split_doubleword_move (rtx dest, rtx src)
2131 {
2132 rtx low_dest;
2133
2134 /* The operation can be split into two normal moves. Decide in
2135 which order to do them. */
2136 low_dest = riscv_subword (dest, false);
2137 if (REG_P (low_dest) && reg_overlap_mentioned_p (low_dest, src))
2138 {
2139 riscv_emit_move (riscv_subword (dest, true), riscv_subword (src, true));
2140 riscv_emit_move (low_dest, riscv_subword (src, false));
2141 }
2142 else
2143 {
2144 riscv_emit_move (low_dest, riscv_subword (src, false));
2145 riscv_emit_move (riscv_subword (dest, true), riscv_subword (src, true));
2146 }
2147 }
2148 \f
2149 /* Return the appropriate instructions to move SRC into DEST. Assume
2150 that SRC is operand 1 and DEST is operand 0. */
2151
2152 const char *
2153 riscv_output_move (rtx dest, rtx src)
2154 {
2155 enum rtx_code dest_code, src_code;
2156 machine_mode mode;
2157 bool dbl_p;
2158
2159 dest_code = GET_CODE (dest);
2160 src_code = GET_CODE (src);
2161 mode = GET_MODE (dest);
2162 dbl_p = (GET_MODE_SIZE (mode) == 8);
2163
2164 if (dbl_p && riscv_split_64bit_move_p (dest, src))
2165 return "#";
2166
2167 if (dest_code == REG && GP_REG_P (REGNO (dest)))
2168 {
2169 if (src_code == REG && FP_REG_P (REGNO (src)))
2170 return dbl_p ? "fmv.x.d\t%0,%1" : "fmv.x.w\t%0,%1";
2171
2172 if (src_code == MEM)
2173 switch (GET_MODE_SIZE (mode))
2174 {
2175 case 1: return "lbu\t%0,%1";
2176 case 2: return "lhu\t%0,%1";
2177 case 4: return "lw\t%0,%1";
2178 case 8: return "ld\t%0,%1";
2179 }
2180
2181 if (src_code == CONST_INT)
2182 return "li\t%0,%1";
2183
2184 if (src_code == HIGH)
2185 return "lui\t%0,%h1";
2186
2187 if (symbolic_operand (src, VOIDmode))
2188 switch (riscv_classify_symbolic_expression (src))
2189 {
2190 case SYMBOL_GOT_DISP: return "la\t%0,%1";
2191 case SYMBOL_ABSOLUTE: return "lla\t%0,%1";
2192 case SYMBOL_PCREL: return "lla\t%0,%1";
2193 default: gcc_unreachable ();
2194 }
2195 }
2196 if ((src_code == REG && GP_REG_P (REGNO (src)))
2197 || (src == CONST0_RTX (mode)))
2198 {
2199 if (dest_code == REG)
2200 {
2201 if (GP_REG_P (REGNO (dest)))
2202 return "mv\t%0,%z1";
2203
2204 if (FP_REG_P (REGNO (dest)))
2205 {
2206 if (!dbl_p)
2207 return "fmv.w.x\t%0,%z1";
2208 if (TARGET_64BIT)
2209 return "fmv.d.x\t%0,%z1";
2210 /* in RV32, we can emulate fmv.d.x %0, x0 using fcvt.d.w */
2211 gcc_assert (src == CONST0_RTX (mode));
2212 return "fcvt.d.w\t%0,x0";
2213 }
2214 }
2215 if (dest_code == MEM)
2216 switch (GET_MODE_SIZE (mode))
2217 {
2218 case 1: return "sb\t%z1,%0";
2219 case 2: return "sh\t%z1,%0";
2220 case 4: return "sw\t%z1,%0";
2221 case 8: return "sd\t%z1,%0";
2222 }
2223 }
2224 if (src_code == REG && FP_REG_P (REGNO (src)))
2225 {
2226 if (dest_code == REG && FP_REG_P (REGNO (dest)))
2227 return dbl_p ? "fmv.d\t%0,%1" : "fmv.s\t%0,%1";
2228
2229 if (dest_code == MEM)
2230 return dbl_p ? "fsd\t%1,%0" : "fsw\t%1,%0";
2231 }
2232 if (dest_code == REG && FP_REG_P (REGNO (dest)))
2233 {
2234 if (src_code == MEM)
2235 return dbl_p ? "fld\t%0,%1" : "flw\t%0,%1";
2236 }
2237 gcc_unreachable ();
2238 }
2239
2240 const char *
2241 riscv_output_return ()
2242 {
2243 if (cfun->machine->naked_p)
2244 return "";
2245
2246 return "ret";
2247 }
2248
2249 \f
2250 /* Return true if CMP1 is a suitable second operand for integer ordering
2251 test CODE. See also the *sCC patterns in riscv.md. */
2252
2253 static bool
2254 riscv_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
2255 {
2256 switch (code)
2257 {
2258 case GT:
2259 case GTU:
2260 return reg_or_0_operand (cmp1, VOIDmode);
2261
2262 case GE:
2263 case GEU:
2264 return cmp1 == const1_rtx;
2265
2266 case LT:
2267 case LTU:
2268 return arith_operand (cmp1, VOIDmode);
2269
2270 case LE:
2271 return sle_operand (cmp1, VOIDmode);
2272
2273 case LEU:
2274 return sleu_operand (cmp1, VOIDmode);
2275
2276 default:
2277 gcc_unreachable ();
2278 }
2279 }
2280
2281 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
2282 integer ordering test *CODE, or if an equivalent combination can
2283 be formed by adjusting *CODE and *CMP1. When returning true, update
2284 *CODE and *CMP1 with the chosen code and operand, otherwise leave
2285 them alone. */
2286
2287 static bool
2288 riscv_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
2289 machine_mode mode)
2290 {
2291 HOST_WIDE_INT plus_one;
2292
2293 if (riscv_int_order_operand_ok_p (*code, *cmp1))
2294 return true;
2295
2296 if (CONST_INT_P (*cmp1))
2297 switch (*code)
2298 {
2299 case LE:
2300 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
2301 if (INTVAL (*cmp1) < plus_one)
2302 {
2303 *code = LT;
2304 *cmp1 = force_reg (mode, GEN_INT (plus_one));
2305 return true;
2306 }
2307 break;
2308
2309 case LEU:
2310 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
2311 if (plus_one != 0)
2312 {
2313 *code = LTU;
2314 *cmp1 = force_reg (mode, GEN_INT (plus_one));
2315 return true;
2316 }
2317 break;
2318
2319 default:
2320 break;
2321 }
2322 return false;
2323 }
2324
2325 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
2326 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
2327 is nonnull, it's OK to set TARGET to the inverse of the result and
2328 flip *INVERT_PTR instead. */
2329
2330 static void
2331 riscv_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
2332 rtx target, rtx cmp0, rtx cmp1)
2333 {
2334 machine_mode mode;
2335
2336 /* First see if there is a RISCV instruction that can do this operation.
2337 If not, try doing the same for the inverse operation. If that also
2338 fails, force CMP1 into a register and try again. */
2339 mode = GET_MODE (cmp0);
2340 if (riscv_canonicalize_int_order_test (&code, &cmp1, mode))
2341 riscv_emit_binary (code, target, cmp0, cmp1);
2342 else
2343 {
2344 enum rtx_code inv_code = reverse_condition (code);
2345 if (!riscv_canonicalize_int_order_test (&inv_code, &cmp1, mode))
2346 {
2347 cmp1 = force_reg (mode, cmp1);
2348 riscv_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
2349 }
2350 else if (invert_ptr == 0)
2351 {
2352 rtx inv_target = riscv_force_binary (GET_MODE (target),
2353 inv_code, cmp0, cmp1);
2354 riscv_emit_binary (XOR, target, inv_target, const1_rtx);
2355 }
2356 else
2357 {
2358 *invert_ptr = !*invert_ptr;
2359 riscv_emit_binary (inv_code, target, cmp0, cmp1);
2360 }
2361 }
2362 }
2363
2364 /* Return a register that is zero iff CMP0 and CMP1 are equal.
2365 The register will have the same mode as CMP0. */
2366
2367 static rtx
2368 riscv_zero_if_equal (rtx cmp0, rtx cmp1)
2369 {
2370 if (cmp1 == const0_rtx)
2371 return cmp0;
2372
2373 return expand_binop (GET_MODE (cmp0), sub_optab,
2374 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
2375 }
2376
2377 /* Sign- or zero-extend OP0 and OP1 for integer comparisons. */
2378
2379 static void
2380 riscv_extend_comparands (rtx_code code, rtx *op0, rtx *op1)
2381 {
2382 /* Comparisons consider all XLEN bits, so extend sub-XLEN values. */
2383 if (GET_MODE_SIZE (word_mode) > GET_MODE_SIZE (GET_MODE (*op0)))
2384 {
2385 /* It is more profitable to zero-extend QImode values. But not if the
2386 first operand has already been sign-extended, and the second one is
2387 is a constant or has already been sign-extended also. */
2388 if (unsigned_condition (code) == code
2389 && (GET_MODE (*op0) == QImode
2390 && ! (GET_CODE (*op0) == SUBREG
2391 && SUBREG_PROMOTED_VAR_P (*op0)
2392 && SUBREG_PROMOTED_SIGNED_P (*op0)
2393 && (CONST_INT_P (*op1)
2394 || (GET_CODE (*op1) == SUBREG
2395 && SUBREG_PROMOTED_VAR_P (*op1)
2396 && SUBREG_PROMOTED_SIGNED_P (*op1))))))
2397 {
2398 *op0 = gen_rtx_ZERO_EXTEND (word_mode, *op0);
2399 if (CONST_INT_P (*op1))
2400 *op1 = GEN_INT ((uint8_t) INTVAL (*op1));
2401 else
2402 *op1 = gen_rtx_ZERO_EXTEND (word_mode, *op1);
2403 }
2404 else
2405 {
2406 *op0 = gen_rtx_SIGN_EXTEND (word_mode, *op0);
2407 if (*op1 != const0_rtx)
2408 *op1 = gen_rtx_SIGN_EXTEND (word_mode, *op1);
2409 }
2410 }
2411 }
2412
2413 /* Convert a comparison into something that can be used in a branch. On
2414 entry, *OP0 and *OP1 are the values being compared and *CODE is the code
2415 used to compare them. Update them to describe the final comparison. */
2416
2417 static void
2418 riscv_emit_int_compare (enum rtx_code *code, rtx *op0, rtx *op1)
2419 {
2420 if (splittable_const_int_operand (*op1, VOIDmode))
2421 {
2422 HOST_WIDE_INT rhs = INTVAL (*op1);
2423
2424 if (*code == EQ || *code == NE)
2425 {
2426 /* Convert e.g. OP0 == 2048 into OP0 - 2048 == 0. */
2427 if (SMALL_OPERAND (-rhs))
2428 {
2429 *op0 = riscv_force_binary (GET_MODE (*op0), PLUS, *op0,
2430 GEN_INT (-rhs));
2431 *op1 = const0_rtx;
2432 }
2433 }
2434 else
2435 {
2436 static const enum rtx_code mag_comparisons[][2] = {
2437 {LEU, LTU}, {GTU, GEU}, {LE, LT}, {GT, GE}
2438 };
2439
2440 /* Convert e.g. (OP0 <= 0xFFF) into (OP0 < 0x1000). */
2441 for (size_t i = 0; i < ARRAY_SIZE (mag_comparisons); i++)
2442 {
2443 HOST_WIDE_INT new_rhs;
2444 bool increment = *code == mag_comparisons[i][0];
2445 bool decrement = *code == mag_comparisons[i][1];
2446 if (!increment && !decrement)
2447 continue;
2448
2449 new_rhs = rhs + (increment ? 1 : -1);
2450 if (riscv_integer_cost (new_rhs) < riscv_integer_cost (rhs)
2451 && (rhs < 0) == (new_rhs < 0))
2452 {
2453 *op1 = GEN_INT (new_rhs);
2454 *code = mag_comparisons[i][increment];
2455 }
2456 break;
2457 }
2458 }
2459 }
2460
2461 riscv_extend_comparands (*code, op0, op1);
2462
2463 *op0 = force_reg (word_mode, *op0);
2464 if (*op1 != const0_rtx)
2465 *op1 = force_reg (word_mode, *op1);
2466 }
2467
2468 /* Like riscv_emit_int_compare, but for floating-point comparisons. */
2469
2470 static void
2471 riscv_emit_float_compare (enum rtx_code *code, rtx *op0, rtx *op1)
2472 {
2473 rtx tmp0, tmp1, cmp_op0 = *op0, cmp_op1 = *op1;
2474 enum rtx_code fp_code = *code;
2475 *code = NE;
2476
2477 switch (fp_code)
2478 {
2479 case UNORDERED:
2480 *code = EQ;
2481 /* Fall through. */
2482
2483 case ORDERED:
2484 /* a == a && b == b */
2485 tmp0 = riscv_force_binary (word_mode, EQ, cmp_op0, cmp_op0);
2486 tmp1 = riscv_force_binary (word_mode, EQ, cmp_op1, cmp_op1);
2487 *op0 = riscv_force_binary (word_mode, AND, tmp0, tmp1);
2488 *op1 = const0_rtx;
2489 break;
2490
2491 case UNEQ:
2492 /* ordered(a, b) > (a == b) */
2493 *code = EQ;
2494 tmp0 = riscv_force_binary (word_mode, EQ, cmp_op0, cmp_op0);
2495 tmp1 = riscv_force_binary (word_mode, EQ, cmp_op1, cmp_op1);
2496 *op0 = riscv_force_binary (word_mode, AND, tmp0, tmp1);
2497 *op1 = riscv_force_binary (word_mode, EQ, cmp_op0, cmp_op1);
2498 break;
2499
2500 #define UNORDERED_COMPARISON(CODE, CMP) \
2501 case CODE: \
2502 *code = EQ; \
2503 *op0 = gen_reg_rtx (word_mode); \
2504 if (GET_MODE (cmp_op0) == SFmode && TARGET_64BIT) \
2505 emit_insn (gen_f##CMP##_quietsfdi4 (*op0, cmp_op0, cmp_op1)); \
2506 else if (GET_MODE (cmp_op0) == SFmode) \
2507 emit_insn (gen_f##CMP##_quietsfsi4 (*op0, cmp_op0, cmp_op1)); \
2508 else if (GET_MODE (cmp_op0) == DFmode && TARGET_64BIT) \
2509 emit_insn (gen_f##CMP##_quietdfdi4 (*op0, cmp_op0, cmp_op1)); \
2510 else if (GET_MODE (cmp_op0) == DFmode) \
2511 emit_insn (gen_f##CMP##_quietdfsi4 (*op0, cmp_op0, cmp_op1)); \
2512 else \
2513 gcc_unreachable (); \
2514 *op1 = const0_rtx; \
2515 break;
2516
2517 case UNLT:
2518 std::swap (cmp_op0, cmp_op1);
2519 gcc_fallthrough ();
2520
2521 UNORDERED_COMPARISON(UNGT, le)
2522
2523 case UNLE:
2524 std::swap (cmp_op0, cmp_op1);
2525 gcc_fallthrough ();
2526
2527 UNORDERED_COMPARISON(UNGE, lt)
2528 #undef UNORDERED_COMPARISON
2529
2530 case NE:
2531 fp_code = EQ;
2532 *code = EQ;
2533 /* Fall through. */
2534
2535 case EQ:
2536 case LE:
2537 case LT:
2538 case GE:
2539 case GT:
2540 /* We have instructions for these cases. */
2541 *op0 = riscv_force_binary (word_mode, fp_code, cmp_op0, cmp_op1);
2542 *op1 = const0_rtx;
2543 break;
2544
2545 case LTGT:
2546 /* (a < b) | (a > b) */
2547 tmp0 = riscv_force_binary (word_mode, LT, cmp_op0, cmp_op1);
2548 tmp1 = riscv_force_binary (word_mode, GT, cmp_op0, cmp_op1);
2549 *op0 = riscv_force_binary (word_mode, IOR, tmp0, tmp1);
2550 *op1 = const0_rtx;
2551 break;
2552
2553 default:
2554 gcc_unreachable ();
2555 }
2556 }
2557
2558 /* CODE-compare OP0 and OP1. Store the result in TARGET. */
2559
2560 void
2561 riscv_expand_int_scc (rtx target, enum rtx_code code, rtx op0, rtx op1)
2562 {
2563 riscv_extend_comparands (code, &op0, &op1);
2564 op0 = force_reg (word_mode, op0);
2565
2566 if (code == EQ || code == NE)
2567 {
2568 rtx zie = riscv_zero_if_equal (op0, op1);
2569 riscv_emit_binary (code, target, zie, const0_rtx);
2570 }
2571 else
2572 riscv_emit_int_order_test (code, 0, target, op0, op1);
2573 }
2574
2575 /* Like riscv_expand_int_scc, but for floating-point comparisons. */
2576
2577 void
2578 riscv_expand_float_scc (rtx target, enum rtx_code code, rtx op0, rtx op1)
2579 {
2580 riscv_emit_float_compare (&code, &op0, &op1);
2581
2582 rtx cmp = riscv_force_binary (word_mode, code, op0, op1);
2583 riscv_emit_set (target, lowpart_subreg (SImode, cmp, word_mode));
2584 }
2585
2586 /* Jump to LABEL if (CODE OP0 OP1) holds. */
2587
2588 void
2589 riscv_expand_conditional_branch (rtx label, rtx_code code, rtx op0, rtx op1)
2590 {
2591 if (FLOAT_MODE_P (GET_MODE (op1)))
2592 riscv_emit_float_compare (&code, &op0, &op1);
2593 else
2594 riscv_emit_int_compare (&code, &op0, &op1);
2595
2596 rtx condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
2597 emit_jump_insn (gen_condjump (condition, label));
2598 }
2599
2600 /* If (CODE OP0 OP1) holds, move CONS to DEST; else move ALT to DEST. */
2601
2602 void
2603 riscv_expand_conditional_move (rtx dest, rtx cons, rtx alt, rtx_code code,
2604 rtx op0, rtx op1)
2605 {
2606 riscv_emit_int_compare (&code, &op0, &op1);
2607 rtx cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
2608 emit_insn (gen_rtx_SET (dest, gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond,
2609 cons, alt)));
2610 }
2611
2612 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Every parameter gets at
2613 least PARM_BOUNDARY bits of alignment, but will be given anything up
2614 to PREFERRED_STACK_BOUNDARY bits if the type requires it. */
2615
2616 static unsigned int
2617 riscv_function_arg_boundary (machine_mode mode, const_tree type)
2618 {
2619 unsigned int alignment;
2620
2621 /* Use natural alignment if the type is not aggregate data. */
2622 if (type && !AGGREGATE_TYPE_P (type))
2623 alignment = TYPE_ALIGN (TYPE_MAIN_VARIANT (type));
2624 else
2625 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
2626
2627 return MIN (PREFERRED_STACK_BOUNDARY, MAX (PARM_BOUNDARY, alignment));
2628 }
2629
2630 /* If MODE represents an argument that can be passed or returned in
2631 floating-point registers, return the number of registers, else 0. */
2632
2633 static unsigned
2634 riscv_pass_mode_in_fpr_p (machine_mode mode)
2635 {
2636 if (GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FP_ARG)
2637 {
2638 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2639 return 1;
2640
2641 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
2642 return 2;
2643 }
2644
2645 return 0;
2646 }
2647
2648 typedef struct {
2649 const_tree type;
2650 HOST_WIDE_INT offset;
2651 } riscv_aggregate_field;
2652
2653 /* Identify subfields of aggregates that are candidates for passing in
2654 floating-point registers. */
2655
2656 static int
2657 riscv_flatten_aggregate_field (const_tree type,
2658 riscv_aggregate_field fields[2],
2659 int n, HOST_WIDE_INT offset,
2660 bool ignore_zero_width_bit_field_p)
2661 {
2662 switch (TREE_CODE (type))
2663 {
2664 case RECORD_TYPE:
2665 /* Can't handle incomplete types nor sizes that are not fixed. */
2666 if (!COMPLETE_TYPE_P (type)
2667 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
2668 || !tree_fits_uhwi_p (TYPE_SIZE (type)))
2669 return -1;
2670
2671 for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
2672 if (TREE_CODE (f) == FIELD_DECL)
2673 {
2674 if (!TYPE_P (TREE_TYPE (f)))
2675 return -1;
2676
2677 /* The C++ front end strips zero-length bit-fields from structs.
2678 So we need to ignore them in the C front end to make C code
2679 compatible with C++ code. */
2680 if (ignore_zero_width_bit_field_p
2681 && DECL_BIT_FIELD (f)
2682 && (DECL_SIZE (f) == NULL_TREE
2683 || integer_zerop (DECL_SIZE (f))))
2684 ;
2685 else
2686 {
2687 HOST_WIDE_INT pos = offset + int_byte_position (f);
2688 n = riscv_flatten_aggregate_field (TREE_TYPE (f),
2689 fields, n, pos,
2690 ignore_zero_width_bit_field_p);
2691 }
2692 if (n < 0)
2693 return -1;
2694 }
2695 return n;
2696
2697 case ARRAY_TYPE:
2698 {
2699 HOST_WIDE_INT n_elts;
2700 riscv_aggregate_field subfields[2];
2701 tree index = TYPE_DOMAIN (type);
2702 tree elt_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
2703 int n_subfields = riscv_flatten_aggregate_field (TREE_TYPE (type),
2704 subfields, 0, offset,
2705 ignore_zero_width_bit_field_p);
2706
2707 /* Can't handle incomplete types nor sizes that are not fixed. */
2708 if (n_subfields <= 0
2709 || !COMPLETE_TYPE_P (type)
2710 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
2711 || !index
2712 || !TYPE_MAX_VALUE (index)
2713 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
2714 || !TYPE_MIN_VALUE (index)
2715 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
2716 || !tree_fits_uhwi_p (elt_size))
2717 return -1;
2718
2719 n_elts = 1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
2720 - tree_to_uhwi (TYPE_MIN_VALUE (index));
2721 gcc_assert (n_elts >= 0);
2722
2723 for (HOST_WIDE_INT i = 0; i < n_elts; i++)
2724 for (int j = 0; j < n_subfields; j++)
2725 {
2726 if (n >= 2)
2727 return -1;
2728
2729 fields[n] = subfields[j];
2730 fields[n++].offset += i * tree_to_uhwi (elt_size);
2731 }
2732
2733 return n;
2734 }
2735
2736 case COMPLEX_TYPE:
2737 {
2738 /* Complex type need consume 2 field, so n must be 0. */
2739 if (n != 0)
2740 return -1;
2741
2742 HOST_WIDE_INT elt_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (type)));
2743
2744 if (elt_size <= UNITS_PER_FP_ARG)
2745 {
2746 fields[0].type = TREE_TYPE (type);
2747 fields[0].offset = offset;
2748 fields[1].type = TREE_TYPE (type);
2749 fields[1].offset = offset + elt_size;
2750
2751 return 2;
2752 }
2753
2754 return -1;
2755 }
2756
2757 default:
2758 if (n < 2
2759 && ((SCALAR_FLOAT_TYPE_P (type)
2760 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FP_ARG)
2761 || (INTEGRAL_TYPE_P (type)
2762 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_WORD)))
2763 {
2764 fields[n].type = type;
2765 fields[n].offset = offset;
2766 return n + 1;
2767 }
2768 else
2769 return -1;
2770 }
2771 }
2772
2773 /* Identify candidate aggregates for passing in floating-point registers.
2774 Candidates have at most two fields after flattening. */
2775
2776 static int
2777 riscv_flatten_aggregate_argument (const_tree type,
2778 riscv_aggregate_field fields[2],
2779 bool ignore_zero_width_bit_field_p)
2780 {
2781 if (!type || TREE_CODE (type) != RECORD_TYPE)
2782 return -1;
2783
2784 return riscv_flatten_aggregate_field (type, fields, 0, 0,
2785 ignore_zero_width_bit_field_p);
2786 }
2787
2788 /* See whether TYPE is a record whose fields should be returned in one or
2789 two floating-point registers. If so, populate FIELDS accordingly. */
2790
2791 static unsigned
2792 riscv_pass_aggregate_in_fpr_pair_p (const_tree type,
2793 riscv_aggregate_field fields[2])
2794 {
2795 static int warned = 0;
2796
2797 /* This is the old ABI, which differs for C++ and C. */
2798 int n_old = riscv_flatten_aggregate_argument (type, fields, false);
2799 for (int i = 0; i < n_old; i++)
2800 if (!SCALAR_FLOAT_TYPE_P (fields[i].type))
2801 {
2802 n_old = -1;
2803 break;
2804 }
2805
2806 /* This is the new ABI, which is the same for C++ and C. */
2807 int n_new = riscv_flatten_aggregate_argument (type, fields, true);
2808 for (int i = 0; i < n_new; i++)
2809 if (!SCALAR_FLOAT_TYPE_P (fields[i].type))
2810 {
2811 n_new = -1;
2812 break;
2813 }
2814
2815 if ((n_old != n_new) && (warned == 0))
2816 {
2817 warning (0, "ABI for flattened struct with zero-length bit-fields "
2818 "changed in GCC 10");
2819 warned = 1;
2820 }
2821
2822 return n_new > 0 ? n_new : 0;
2823 }
2824
2825 /* See whether TYPE is a record whose fields should be returned in one or
2826 floating-point register and one integer register. If so, populate
2827 FIELDS accordingly. */
2828
2829 static bool
2830 riscv_pass_aggregate_in_fpr_and_gpr_p (const_tree type,
2831 riscv_aggregate_field fields[2])
2832 {
2833 static int warned = 0;
2834
2835 /* This is the old ABI, which differs for C++ and C. */
2836 unsigned num_int_old = 0, num_float_old = 0;
2837 int n_old = riscv_flatten_aggregate_argument (type, fields, false);
2838 for (int i = 0; i < n_old; i++)
2839 {
2840 num_float_old += SCALAR_FLOAT_TYPE_P (fields[i].type);
2841 num_int_old += INTEGRAL_TYPE_P (fields[i].type);
2842 }
2843
2844 /* This is the new ABI, which is the same for C++ and C. */
2845 unsigned num_int_new = 0, num_float_new = 0;
2846 int n_new = riscv_flatten_aggregate_argument (type, fields, true);
2847 for (int i = 0; i < n_new; i++)
2848 {
2849 num_float_new += SCALAR_FLOAT_TYPE_P (fields[i].type);
2850 num_int_new += INTEGRAL_TYPE_P (fields[i].type);
2851 }
2852
2853 if (((num_int_old == 1 && num_float_old == 1
2854 && (num_int_old != num_int_new || num_float_old != num_float_new))
2855 || (num_int_new == 1 && num_float_new == 1
2856 && (num_int_old != num_int_new || num_float_old != num_float_new)))
2857 && (warned == 0))
2858 {
2859 warning (0, "ABI for flattened struct with zero-length bit-fields "
2860 "changed in GCC 10");
2861 warned = 1;
2862 }
2863
2864 return num_int_new == 1 && num_float_new == 1;
2865 }
2866
2867 /* Return the representation of an argument passed or returned in an FPR
2868 when the value has mode VALUE_MODE and the type has TYPE_MODE. The
2869 two modes may be different for structures like:
2870
2871 struct __attribute__((packed)) foo { float f; }
2872
2873 where the SFmode value "f" is passed in REGNO but the struct itself
2874 has mode BLKmode. */
2875
2876 static rtx
2877 riscv_pass_fpr_single (machine_mode type_mode, unsigned regno,
2878 machine_mode value_mode,
2879 HOST_WIDE_INT offset)
2880 {
2881 rtx x = gen_rtx_REG (value_mode, regno);
2882
2883 if (type_mode != value_mode)
2884 {
2885 x = gen_rtx_EXPR_LIST (VOIDmode, x, GEN_INT (offset));
2886 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
2887 }
2888 return x;
2889 }
2890
2891 /* Pass or return a composite value in the FPR pair REGNO and REGNO + 1.
2892 MODE is the mode of the composite. MODE1 and OFFSET1 are the mode and
2893 byte offset for the first value, likewise MODE2 and OFFSET2 for the
2894 second value. */
2895
2896 static rtx
2897 riscv_pass_fpr_pair (machine_mode mode, unsigned regno1,
2898 machine_mode mode1, HOST_WIDE_INT offset1,
2899 unsigned regno2, machine_mode mode2,
2900 HOST_WIDE_INT offset2)
2901 {
2902 return gen_rtx_PARALLEL
2903 (mode,
2904 gen_rtvec (2,
2905 gen_rtx_EXPR_LIST (VOIDmode,
2906 gen_rtx_REG (mode1, regno1),
2907 GEN_INT (offset1)),
2908 gen_rtx_EXPR_LIST (VOIDmode,
2909 gen_rtx_REG (mode2, regno2),
2910 GEN_INT (offset2))));
2911 }
2912
2913 /* Fill INFO with information about a single argument, and return an
2914 RTL pattern to pass or return the argument. CUM is the cumulative
2915 state for earlier arguments. MODE is the mode of this argument and
2916 TYPE is its type (if known). NAMED is true if this is a named
2917 (fixed) argument rather than a variable one. RETURN_P is true if
2918 returning the argument, or false if passing the argument. */
2919
2920 static rtx
2921 riscv_get_arg_info (struct riscv_arg_info *info, const CUMULATIVE_ARGS *cum,
2922 machine_mode mode, const_tree type, bool named,
2923 bool return_p)
2924 {
2925 unsigned num_bytes, num_words;
2926 unsigned fpr_base = return_p ? FP_RETURN : FP_ARG_FIRST;
2927 unsigned gpr_base = return_p ? GP_RETURN : GP_ARG_FIRST;
2928 unsigned alignment = riscv_function_arg_boundary (mode, type);
2929
2930 memset (info, 0, sizeof (*info));
2931 info->gpr_offset = cum->num_gprs;
2932 info->fpr_offset = cum->num_fprs;
2933
2934 if (named)
2935 {
2936 riscv_aggregate_field fields[2];
2937 unsigned fregno = fpr_base + info->fpr_offset;
2938 unsigned gregno = gpr_base + info->gpr_offset;
2939
2940 /* Pass one- or two-element floating-point aggregates in FPRs. */
2941 if ((info->num_fprs = riscv_pass_aggregate_in_fpr_pair_p (type, fields))
2942 && info->fpr_offset + info->num_fprs <= MAX_ARGS_IN_REGISTERS)
2943 switch (info->num_fprs)
2944 {
2945 case 1:
2946 return riscv_pass_fpr_single (mode, fregno,
2947 TYPE_MODE (fields[0].type),
2948 fields[0].offset);
2949
2950 case 2:
2951 return riscv_pass_fpr_pair (mode, fregno,
2952 TYPE_MODE (fields[0].type),
2953 fields[0].offset,
2954 fregno + 1,
2955 TYPE_MODE (fields[1].type),
2956 fields[1].offset);
2957
2958 default:
2959 gcc_unreachable ();
2960 }
2961
2962 /* Pass real and complex floating-point numbers in FPRs. */
2963 if ((info->num_fprs = riscv_pass_mode_in_fpr_p (mode))
2964 && info->fpr_offset + info->num_fprs <= MAX_ARGS_IN_REGISTERS)
2965 switch (GET_MODE_CLASS (mode))
2966 {
2967 case MODE_FLOAT:
2968 return gen_rtx_REG (mode, fregno);
2969
2970 case MODE_COMPLEX_FLOAT:
2971 return riscv_pass_fpr_pair (mode, fregno, GET_MODE_INNER (mode), 0,
2972 fregno + 1, GET_MODE_INNER (mode),
2973 GET_MODE_UNIT_SIZE (mode));
2974
2975 default:
2976 gcc_unreachable ();
2977 }
2978
2979 /* Pass structs with one float and one integer in an FPR and a GPR. */
2980 if (riscv_pass_aggregate_in_fpr_and_gpr_p (type, fields)
2981 && info->gpr_offset < MAX_ARGS_IN_REGISTERS
2982 && info->fpr_offset < MAX_ARGS_IN_REGISTERS)
2983 {
2984 info->num_gprs = 1;
2985 info->num_fprs = 1;
2986
2987 if (!SCALAR_FLOAT_TYPE_P (fields[0].type))
2988 std::swap (fregno, gregno);
2989
2990 return riscv_pass_fpr_pair (mode, fregno, TYPE_MODE (fields[0].type),
2991 fields[0].offset,
2992 gregno, TYPE_MODE (fields[1].type),
2993 fields[1].offset);
2994 }
2995 }
2996
2997 /* Work out the size of the argument. */
2998 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
2999 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3000
3001 /* Doubleword-aligned varargs start on an even register boundary. */
3002 if (!named && num_bytes != 0 && alignment > BITS_PER_WORD)
3003 info->gpr_offset += info->gpr_offset & 1;
3004
3005 /* Partition the argument between registers and stack. */
3006 info->num_fprs = 0;
3007 info->num_gprs = MIN (num_words, MAX_ARGS_IN_REGISTERS - info->gpr_offset);
3008 info->stack_p = (num_words - info->num_gprs) != 0;
3009
3010 if (info->num_gprs || return_p)
3011 return gen_rtx_REG (mode, gpr_base + info->gpr_offset);
3012
3013 return NULL_RTX;
3014 }
3015
3016 /* Implement TARGET_FUNCTION_ARG. */
3017
3018 static rtx
3019 riscv_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
3020 {
3021 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
3022 struct riscv_arg_info info;
3023
3024 if (arg.end_marker_p ())
3025 return NULL;
3026
3027 return riscv_get_arg_info (&info, cum, arg.mode, arg.type, arg.named, false);
3028 }
3029
3030 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
3031
3032 static void
3033 riscv_function_arg_advance (cumulative_args_t cum_v,
3034 const function_arg_info &arg)
3035 {
3036 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
3037 struct riscv_arg_info info;
3038
3039 riscv_get_arg_info (&info, cum, arg.mode, arg.type, arg.named, false);
3040
3041 /* Advance the register count. This has the effect of setting
3042 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
3043 argument required us to skip the final GPR and pass the whole
3044 argument on the stack. */
3045 cum->num_fprs = info.fpr_offset + info.num_fprs;
3046 cum->num_gprs = info.gpr_offset + info.num_gprs;
3047 }
3048
3049 /* Implement TARGET_ARG_PARTIAL_BYTES. */
3050
3051 static int
3052 riscv_arg_partial_bytes (cumulative_args_t cum,
3053 const function_arg_info &generic_arg)
3054 {
3055 struct riscv_arg_info arg;
3056
3057 riscv_get_arg_info (&arg, get_cumulative_args (cum), generic_arg.mode,
3058 generic_arg.type, generic_arg.named, false);
3059 return arg.stack_p ? arg.num_gprs * UNITS_PER_WORD : 0;
3060 }
3061
3062 /* Implement FUNCTION_VALUE and LIBCALL_VALUE. For normal calls,
3063 VALTYPE is the return type and MODE is VOIDmode. For libcalls,
3064 VALTYPE is null and MODE is the mode of the return value. */
3065
3066 rtx
3067 riscv_function_value (const_tree type, const_tree func, machine_mode mode)
3068 {
3069 struct riscv_arg_info info;
3070 CUMULATIVE_ARGS args;
3071
3072 if (type)
3073 {
3074 int unsigned_p = TYPE_UNSIGNED (type);
3075
3076 mode = TYPE_MODE (type);
3077
3078 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
3079 return values, promote the mode here too. */
3080 mode = promote_function_mode (type, mode, &unsigned_p, func, 1);
3081 }
3082
3083 memset (&args, 0, sizeof args);
3084 return riscv_get_arg_info (&info, &args, mode, type, true, true);
3085 }
3086
3087 /* Implement TARGET_PASS_BY_REFERENCE. */
3088
3089 static bool
3090 riscv_pass_by_reference (cumulative_args_t cum_v, const function_arg_info &arg)
3091 {
3092 HOST_WIDE_INT size = arg.type_size_in_bytes ();
3093 struct riscv_arg_info info;
3094 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
3095
3096 /* ??? std_gimplify_va_arg_expr passes NULL for cum. Fortunately, we
3097 never pass variadic arguments in floating-point registers, so we can
3098 avoid the call to riscv_get_arg_info in this case. */
3099 if (cum != NULL)
3100 {
3101 /* Don't pass by reference if we can use a floating-point register. */
3102 riscv_get_arg_info (&info, cum, arg.mode, arg.type, arg.named, false);
3103 if (info.num_fprs)
3104 return false;
3105 }
3106
3107 /* Pass by reference if the data do not fit in two integer registers. */
3108 return !IN_RANGE (size, 0, 2 * UNITS_PER_WORD);
3109 }
3110
3111 /* Implement TARGET_RETURN_IN_MEMORY. */
3112
3113 static bool
3114 riscv_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
3115 {
3116 CUMULATIVE_ARGS args;
3117 cumulative_args_t cum = pack_cumulative_args (&args);
3118
3119 /* The rules for returning in memory are the same as for passing the
3120 first named argument by reference. */
3121 memset (&args, 0, sizeof args);
3122 function_arg_info arg (const_cast<tree> (type), /*named=*/true);
3123 return riscv_pass_by_reference (cum, arg);
3124 }
3125
3126 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
3127
3128 static void
3129 riscv_setup_incoming_varargs (cumulative_args_t cum,
3130 const function_arg_info &arg,
3131 int *pretend_size ATTRIBUTE_UNUSED, int no_rtl)
3132 {
3133 CUMULATIVE_ARGS local_cum;
3134 int gp_saved;
3135
3136 /* The caller has advanced CUM up to, but not beyond, the last named
3137 argument. Advance a local copy of CUM past the last "real" named
3138 argument, to find out how many registers are left over. */
3139 local_cum = *get_cumulative_args (cum);
3140 riscv_function_arg_advance (pack_cumulative_args (&local_cum), arg);
3141
3142 /* Found out how many registers we need to save. */
3143 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
3144
3145 if (!no_rtl && gp_saved > 0)
3146 {
3147 rtx ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
3148 REG_PARM_STACK_SPACE (cfun->decl)
3149 - gp_saved * UNITS_PER_WORD);
3150 rtx mem = gen_frame_mem (BLKmode, ptr);
3151 set_mem_alias_set (mem, get_varargs_alias_set ());
3152
3153 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
3154 mem, gp_saved);
3155 }
3156 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
3157 cfun->machine->varargs_size = gp_saved * UNITS_PER_WORD;
3158 }
3159
3160 /* Handle an attribute requiring a FUNCTION_DECL;
3161 arguments as in struct attribute_spec.handler. */
3162 static tree
3163 riscv_handle_fndecl_attribute (tree *node, tree name,
3164 tree args ATTRIBUTE_UNUSED,
3165 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
3166 {
3167 if (TREE_CODE (*node) != FUNCTION_DECL)
3168 {
3169 warning (OPT_Wattributes, "%qE attribute only applies to functions",
3170 name);
3171 *no_add_attrs = true;
3172 }
3173
3174 return NULL_TREE;
3175 }
3176
3177 /* Verify type based attributes. NODE is the what the attribute is being
3178 applied to. NAME is the attribute name. ARGS are the attribute args.
3179 FLAGS gives info about the context. NO_ADD_ATTRS should be set to true if
3180 the attribute should be ignored. */
3181
3182 static tree
3183 riscv_handle_type_attribute (tree *node ATTRIBUTE_UNUSED, tree name, tree args,
3184 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
3185 {
3186 /* Check for an argument. */
3187 if (is_attribute_p ("interrupt", name))
3188 {
3189 if (args)
3190 {
3191 tree cst = TREE_VALUE (args);
3192 const char *string;
3193
3194 if (TREE_CODE (cst) != STRING_CST)
3195 {
3196 warning (OPT_Wattributes,
3197 "%qE attribute requires a string argument",
3198 name);
3199 *no_add_attrs = true;
3200 return NULL_TREE;
3201 }
3202
3203 string = TREE_STRING_POINTER (cst);
3204 if (strcmp (string, "user") && strcmp (string, "supervisor")
3205 && strcmp (string, "machine"))
3206 {
3207 warning (OPT_Wattributes,
3208 "argument to %qE attribute is not \"user\", \"supervisor\", or \"machine\"",
3209 name);
3210 *no_add_attrs = true;
3211 }
3212 }
3213 }
3214
3215 return NULL_TREE;
3216 }
3217
3218 /* Return true if function TYPE is an interrupt function. */
3219 static bool
3220 riscv_interrupt_type_p (tree type)
3221 {
3222 return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
3223 }
3224
3225 /* Return true if FUNC is a naked function. */
3226 static bool
3227 riscv_naked_function_p (tree func)
3228 {
3229 tree func_decl = func;
3230 if (func == NULL_TREE)
3231 func_decl = current_function_decl;
3232 return NULL_TREE != lookup_attribute ("naked", DECL_ATTRIBUTES (func_decl));
3233 }
3234
3235 /* Implement TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS. */
3236 static bool
3237 riscv_allocate_stack_slots_for_args ()
3238 {
3239 /* Naked functions should not allocate stack slots for arguments. */
3240 return !riscv_naked_function_p (current_function_decl);
3241 }
3242
3243 /* Implement TARGET_WARN_FUNC_RETURN. */
3244 static bool
3245 riscv_warn_func_return (tree decl)
3246 {
3247 /* Naked functions are implemented entirely in assembly, including the
3248 return sequence, so suppress warnings about this. */
3249 return !riscv_naked_function_p (decl);
3250 }
3251
3252 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
3253
3254 static void
3255 riscv_va_start (tree valist, rtx nextarg)
3256 {
3257 nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
3258 std_expand_builtin_va_start (valist, nextarg);
3259 }
3260
3261 /* Make ADDR suitable for use as a call or sibcall target. */
3262
3263 rtx
3264 riscv_legitimize_call_address (rtx addr)
3265 {
3266 if (!call_insn_operand (addr, VOIDmode))
3267 {
3268 rtx reg = RISCV_CALL_ADDRESS_TEMP (Pmode);
3269 riscv_emit_move (reg, addr);
3270 return reg;
3271 }
3272 return addr;
3273 }
3274
3275 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
3276 Assume that the areas do not overlap. */
3277
3278 static void
3279 riscv_block_move_straight (rtx dest, rtx src, unsigned HOST_WIDE_INT length)
3280 {
3281 unsigned HOST_WIDE_INT offset, delta;
3282 unsigned HOST_WIDE_INT bits;
3283 int i;
3284 enum machine_mode mode;
3285 rtx *regs;
3286
3287 bits = MAX (BITS_PER_UNIT,
3288 MIN (BITS_PER_WORD, MIN (MEM_ALIGN (src), MEM_ALIGN (dest))));
3289
3290 mode = mode_for_size (bits, MODE_INT, 0).require ();
3291 delta = bits / BITS_PER_UNIT;
3292
3293 /* Allocate a buffer for the temporary registers. */
3294 regs = XALLOCAVEC (rtx, length / delta);
3295
3296 /* Load as many BITS-sized chunks as possible. Use a normal load if
3297 the source has enough alignment, otherwise use left/right pairs. */
3298 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
3299 {
3300 regs[i] = gen_reg_rtx (mode);
3301 riscv_emit_move (regs[i], adjust_address (src, mode, offset));
3302 }
3303
3304 /* Copy the chunks to the destination. */
3305 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
3306 riscv_emit_move (adjust_address (dest, mode, offset), regs[i]);
3307
3308 /* Mop up any left-over bytes. */
3309 if (offset < length)
3310 {
3311 src = adjust_address (src, BLKmode, offset);
3312 dest = adjust_address (dest, BLKmode, offset);
3313 move_by_pieces (dest, src, length - offset,
3314 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), RETURN_BEGIN);
3315 }
3316 }
3317
3318 /* Helper function for doing a loop-based block operation on memory
3319 reference MEM. Each iteration of the loop will operate on LENGTH
3320 bytes of MEM.
3321
3322 Create a new base register for use within the loop and point it to
3323 the start of MEM. Create a new memory reference that uses this
3324 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
3325
3326 static void
3327 riscv_adjust_block_mem (rtx mem, unsigned HOST_WIDE_INT length,
3328 rtx *loop_reg, rtx *loop_mem)
3329 {
3330 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
3331
3332 /* Although the new mem does not refer to a known location,
3333 it does keep up to LENGTH bytes of alignment. */
3334 *loop_mem = change_address (mem, BLKmode, *loop_reg);
3335 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
3336 }
3337
3338 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
3339 bytes at a time. LENGTH must be at least BYTES_PER_ITER. Assume that
3340 the memory regions do not overlap. */
3341
3342 static void
3343 riscv_block_move_loop (rtx dest, rtx src, unsigned HOST_WIDE_INT length,
3344 unsigned HOST_WIDE_INT bytes_per_iter)
3345 {
3346 rtx label, src_reg, dest_reg, final_src, test;
3347 unsigned HOST_WIDE_INT leftover;
3348
3349 leftover = length % bytes_per_iter;
3350 length -= leftover;
3351
3352 /* Create registers and memory references for use within the loop. */
3353 riscv_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
3354 riscv_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
3355
3356 /* Calculate the value that SRC_REG should have after the last iteration
3357 of the loop. */
3358 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
3359 0, 0, OPTAB_WIDEN);
3360
3361 /* Emit the start of the loop. */
3362 label = gen_label_rtx ();
3363 emit_label (label);
3364
3365 /* Emit the loop body. */
3366 riscv_block_move_straight (dest, src, bytes_per_iter);
3367
3368 /* Move on to the next block. */
3369 riscv_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
3370 riscv_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
3371
3372 /* Emit the loop condition. */
3373 test = gen_rtx_NE (VOIDmode, src_reg, final_src);
3374 emit_jump_insn (gen_cbranch4 (Pmode, test, src_reg, final_src, label));
3375
3376 /* Mop up any left-over bytes. */
3377 if (leftover)
3378 riscv_block_move_straight (dest, src, leftover);
3379 else
3380 emit_insn(gen_nop ());
3381 }
3382
3383 /* Expand a cpymemsi instruction, which copies LENGTH bytes from
3384 memory reference SRC to memory reference DEST. */
3385
3386 bool
3387 riscv_expand_block_move (rtx dest, rtx src, rtx length)
3388 {
3389 if (CONST_INT_P (length))
3390 {
3391 unsigned HOST_WIDE_INT hwi_length = UINTVAL (length);
3392 unsigned HOST_WIDE_INT factor, align;
3393
3394 align = MIN (MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), BITS_PER_WORD);
3395 factor = BITS_PER_WORD / align;
3396
3397 if (optimize_function_for_size_p (cfun)
3398 && hwi_length * factor * UNITS_PER_WORD > MOVE_RATIO (false))
3399 return false;
3400
3401 if (hwi_length <= (RISCV_MAX_MOVE_BYTES_STRAIGHT / factor))
3402 {
3403 riscv_block_move_straight (dest, src, INTVAL (length));
3404 return true;
3405 }
3406 else if (optimize && align >= BITS_PER_WORD)
3407 {
3408 unsigned min_iter_words
3409 = RISCV_MAX_MOVE_BYTES_PER_LOOP_ITER / UNITS_PER_WORD;
3410 unsigned iter_words = min_iter_words;
3411 unsigned HOST_WIDE_INT bytes = hwi_length;
3412 unsigned HOST_WIDE_INT words = bytes / UNITS_PER_WORD;
3413
3414 /* Lengthen the loop body if it shortens the tail. */
3415 for (unsigned i = min_iter_words; i < min_iter_words * 2 - 1; i++)
3416 {
3417 unsigned cur_cost = iter_words + words % iter_words;
3418 unsigned new_cost = i + words % i;
3419 if (new_cost <= cur_cost)
3420 iter_words = i;
3421 }
3422
3423 riscv_block_move_loop (dest, src, bytes, iter_words * UNITS_PER_WORD);
3424 return true;
3425 }
3426 }
3427 return false;
3428 }
3429
3430 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
3431 in context CONTEXT. HI_RELOC indicates a high-part reloc. */
3432
3433 static void
3434 riscv_print_operand_reloc (FILE *file, rtx op, bool hi_reloc)
3435 {
3436 const char *reloc;
3437
3438 switch (riscv_classify_symbolic_expression (op))
3439 {
3440 case SYMBOL_ABSOLUTE:
3441 reloc = hi_reloc ? "%hi" : "%lo";
3442 break;
3443
3444 case SYMBOL_PCREL:
3445 reloc = hi_reloc ? "%pcrel_hi" : "%pcrel_lo";
3446 break;
3447
3448 case SYMBOL_TLS_LE:
3449 reloc = hi_reloc ? "%tprel_hi" : "%tprel_lo";
3450 break;
3451
3452 default:
3453 output_operand_lossage ("invalid use of '%%%c'", hi_reloc ? 'h' : 'R');
3454 return;
3455 }
3456
3457 fprintf (file, "%s(", reloc);
3458 output_addr_const (file, riscv_strip_unspec_address (op));
3459 fputc (')', file);
3460 }
3461
3462 /* Return true if the .AQ suffix should be added to an AMO to implement the
3463 acquire portion of memory model MODEL. */
3464
3465 static bool
3466 riscv_memmodel_needs_amo_acquire (enum memmodel model)
3467 {
3468 switch (model)
3469 {
3470 case MEMMODEL_ACQ_REL:
3471 case MEMMODEL_SEQ_CST:
3472 case MEMMODEL_SYNC_SEQ_CST:
3473 case MEMMODEL_ACQUIRE:
3474 case MEMMODEL_CONSUME:
3475 case MEMMODEL_SYNC_ACQUIRE:
3476 return true;
3477
3478 case MEMMODEL_RELEASE:
3479 case MEMMODEL_SYNC_RELEASE:
3480 case MEMMODEL_RELAXED:
3481 return false;
3482
3483 default:
3484 gcc_unreachable ();
3485 }
3486 }
3487
3488 /* Return true if a FENCE should be emitted to before a memory access to
3489 implement the release portion of memory model MODEL. */
3490
3491 static bool
3492 riscv_memmodel_needs_release_fence (enum memmodel model)
3493 {
3494 switch (model)
3495 {
3496 case MEMMODEL_ACQ_REL:
3497 case MEMMODEL_SEQ_CST:
3498 case MEMMODEL_SYNC_SEQ_CST:
3499 case MEMMODEL_RELEASE:
3500 case MEMMODEL_SYNC_RELEASE:
3501 return true;
3502
3503 case MEMMODEL_ACQUIRE:
3504 case MEMMODEL_CONSUME:
3505 case MEMMODEL_SYNC_ACQUIRE:
3506 case MEMMODEL_RELAXED:
3507 return false;
3508
3509 default:
3510 gcc_unreachable ();
3511 }
3512 }
3513
3514 /* Implement TARGET_PRINT_OPERAND. The RISCV-specific operand codes are:
3515
3516 'h' Print the high-part relocation associated with OP, after stripping
3517 any outermost HIGH.
3518 'R' Print the low-part relocation associated with OP.
3519 'C' Print the integer branch condition for comparison OP.
3520 'A' Print the atomic operation suffix for memory model OP.
3521 'F' Print a FENCE if the memory model requires a release.
3522 'z' Print x0 if OP is zero, otherwise print OP normally.
3523 'i' Print i if the operand is not a register. */
3524
3525 static void
3526 riscv_print_operand (FILE *file, rtx op, int letter)
3527 {
3528 machine_mode mode = GET_MODE (op);
3529 enum rtx_code code = GET_CODE (op);
3530
3531 switch (letter)
3532 {
3533 case 'h':
3534 if (code == HIGH)
3535 op = XEXP (op, 0);
3536 riscv_print_operand_reloc (file, op, true);
3537 break;
3538
3539 case 'R':
3540 riscv_print_operand_reloc (file, op, false);
3541 break;
3542
3543 case 'C':
3544 /* The RTL names match the instruction names. */
3545 fputs (GET_RTX_NAME (code), file);
3546 break;
3547
3548 case 'A':
3549 if (riscv_memmodel_needs_amo_acquire ((enum memmodel) INTVAL (op)))
3550 fputs (".aq", file);
3551 break;
3552
3553 case 'F':
3554 if (riscv_memmodel_needs_release_fence ((enum memmodel) INTVAL (op)))
3555 fputs ("fence iorw,ow; ", file);
3556 break;
3557
3558 case 'i':
3559 if (code != REG)
3560 fputs ("i", file);
3561 break;
3562
3563 default:
3564 switch (code)
3565 {
3566 case REG:
3567 if (letter && letter != 'z')
3568 output_operand_lossage ("invalid use of '%%%c'", letter);
3569 fprintf (file, "%s", reg_names[REGNO (op)]);
3570 break;
3571
3572 case MEM:
3573 if (letter && letter != 'z')
3574 output_operand_lossage ("invalid use of '%%%c'", letter);
3575 else
3576 output_address (mode, XEXP (op, 0));
3577 break;
3578
3579 default:
3580 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
3581 fputs (reg_names[GP_REG_FIRST], file);
3582 else if (letter && letter != 'z')
3583 output_operand_lossage ("invalid use of '%%%c'", letter);
3584 else
3585 output_addr_const (file, riscv_strip_unspec_address (op));
3586 break;
3587 }
3588 }
3589 }
3590
3591 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
3592
3593 static void
3594 riscv_print_operand_address (FILE *file, machine_mode mode ATTRIBUTE_UNUSED, rtx x)
3595 {
3596 struct riscv_address_info addr;
3597
3598 if (riscv_classify_address (&addr, x, word_mode, true))
3599 switch (addr.type)
3600 {
3601 case ADDRESS_REG:
3602 riscv_print_operand (file, addr.offset, 0);
3603 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
3604 return;
3605
3606 case ADDRESS_LO_SUM:
3607 riscv_print_operand_reloc (file, addr.offset, false);
3608 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
3609 return;
3610
3611 case ADDRESS_CONST_INT:
3612 output_addr_const (file, x);
3613 fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
3614 return;
3615
3616 case ADDRESS_SYMBOLIC:
3617 output_addr_const (file, riscv_strip_unspec_address (x));
3618 return;
3619 }
3620 gcc_unreachable ();
3621 }
3622
3623 static bool
3624 riscv_size_ok_for_small_data_p (int size)
3625 {
3626 return g_switch_value && IN_RANGE (size, 1, g_switch_value);
3627 }
3628
3629 /* Return true if EXP should be placed in the small data section. */
3630
3631 static bool
3632 riscv_in_small_data_p (const_tree x)
3633 {
3634 if (TREE_CODE (x) == STRING_CST || TREE_CODE (x) == FUNCTION_DECL)
3635 return false;
3636
3637 if (TREE_CODE (x) == VAR_DECL && DECL_SECTION_NAME (x))
3638 {
3639 const char *sec = DECL_SECTION_NAME (x);
3640 return strcmp (sec, ".sdata") == 0 || strcmp (sec, ".sbss") == 0;
3641 }
3642
3643 return riscv_size_ok_for_small_data_p (int_size_in_bytes (TREE_TYPE (x)));
3644 }
3645
3646 /* Switch to the appropriate section for output of DECL. */
3647
3648 static section *
3649 riscv_select_section (tree decl, int reloc,
3650 unsigned HOST_WIDE_INT align)
3651 {
3652 switch (categorize_decl_for_section (decl, reloc))
3653 {
3654 case SECCAT_SRODATA:
3655 return get_named_section (decl, ".srodata", reloc);
3656
3657 default:
3658 return default_elf_select_section (decl, reloc, align);
3659 }
3660 }
3661
3662 /* Switch to the appropriate section for output of DECL. */
3663
3664 static void
3665 riscv_unique_section (tree decl, int reloc)
3666 {
3667 const char *prefix = NULL;
3668 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
3669
3670 switch (categorize_decl_for_section (decl, reloc))
3671 {
3672 case SECCAT_SRODATA:
3673 prefix = one_only ? ".sr" : ".srodata";
3674 break;
3675
3676 default:
3677 break;
3678 }
3679 if (prefix)
3680 {
3681 const char *name, *linkonce;
3682 char *string;
3683
3684 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
3685 name = targetm.strip_name_encoding (name);
3686
3687 /* If we're using one_only, then there needs to be a .gnu.linkonce
3688 prefix to the section name. */
3689 linkonce = one_only ? ".gnu.linkonce" : "";
3690
3691 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
3692
3693 set_decl_section_name (decl, string);
3694 return;
3695 }
3696 default_unique_section (decl, reloc);
3697 }
3698
3699 /* Return a section for X, handling small data. */
3700
3701 static section *
3702 riscv_elf_select_rtx_section (machine_mode mode, rtx x,
3703 unsigned HOST_WIDE_INT align)
3704 {
3705 section *s = default_elf_select_rtx_section (mode, x, align);
3706
3707 if (riscv_size_ok_for_small_data_p (GET_MODE_SIZE (mode)))
3708 {
3709 if (startswith (s->named.name, ".rodata.cst"))
3710 {
3711 /* Rename .rodata.cst* to .srodata.cst*. */
3712 char *name = (char *) alloca (strlen (s->named.name) + 2);
3713 sprintf (name, ".s%s", s->named.name + 1);
3714 return get_section (name, s->named.common.flags, NULL);
3715 }
3716
3717 if (s == data_section)
3718 return sdata_section;
3719 }
3720
3721 return s;
3722 }
3723
3724 /* Make the last instruction frame-related and note that it performs
3725 the operation described by FRAME_PATTERN. */
3726
3727 static void
3728 riscv_set_frame_expr (rtx frame_pattern)
3729 {
3730 rtx insn;
3731
3732 insn = get_last_insn ();
3733 RTX_FRAME_RELATED_P (insn) = 1;
3734 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
3735 frame_pattern,
3736 REG_NOTES (insn));
3737 }
3738
3739 /* Return a frame-related rtx that stores REG at MEM.
3740 REG must be a single register. */
3741
3742 static rtx
3743 riscv_frame_set (rtx mem, rtx reg)
3744 {
3745 rtx set = gen_rtx_SET (mem, reg);
3746 RTX_FRAME_RELATED_P (set) = 1;
3747 return set;
3748 }
3749
3750 /* Return true if the current function must save register REGNO. */
3751
3752 static bool
3753 riscv_save_reg_p (unsigned int regno)
3754 {
3755 bool call_saved = !global_regs[regno] && !call_used_or_fixed_reg_p (regno);
3756 bool might_clobber = crtl->saves_all_registers
3757 || df_regs_ever_live_p (regno);
3758
3759 if (call_saved && might_clobber)
3760 return true;
3761
3762 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
3763 return true;
3764
3765 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
3766 return true;
3767
3768 /* If this is an interrupt handler, then must save extra registers. */
3769 if (cfun->machine->interrupt_handler_p)
3770 {
3771 /* zero register is always zero. */
3772 if (regno == GP_REG_FIRST)
3773 return false;
3774
3775 /* The function will return the stack pointer to its original value. */
3776 if (regno == STACK_POINTER_REGNUM)
3777 return false;
3778
3779 /* By convention, we assume that gp and tp are safe. */
3780 if (regno == GP_REGNUM || regno == THREAD_POINTER_REGNUM)
3781 return false;
3782
3783 /* We must save every register used in this function. If this is not a
3784 leaf function, then we must save all temporary registers. */
3785 if (df_regs_ever_live_p (regno)
3786 || (!crtl->is_leaf && call_used_or_fixed_reg_p (regno)))
3787 return true;
3788 }
3789
3790 return false;
3791 }
3792
3793 /* Determine whether to call GPR save/restore routines. */
3794 static bool
3795 riscv_use_save_libcall (const struct riscv_frame_info *frame)
3796 {
3797 if (!TARGET_SAVE_RESTORE || crtl->calls_eh_return || frame_pointer_needed
3798 || cfun->machine->interrupt_handler_p)
3799 return false;
3800
3801 return frame->save_libcall_adjustment != 0;
3802 }
3803
3804 /* Determine which GPR save/restore routine to call. */
3805
3806 static unsigned
3807 riscv_save_libcall_count (unsigned mask)
3808 {
3809 for (unsigned n = GP_REG_LAST; n > GP_REG_FIRST; n--)
3810 if (BITSET_P (mask, n))
3811 return CALLEE_SAVED_REG_NUMBER (n) + 1;
3812 abort ();
3813 }
3814
3815 /* Populate the current function's riscv_frame_info structure.
3816
3817 RISC-V stack frames grown downward. High addresses are at the top.
3818
3819 +-------------------------------+
3820 | |
3821 | incoming stack arguments |
3822 | |
3823 +-------------------------------+ <-- incoming stack pointer
3824 | |
3825 | callee-allocated save area |
3826 | for arguments that are |
3827 | split between registers and |
3828 | the stack |
3829 | |
3830 +-------------------------------+ <-- arg_pointer_rtx
3831 | |
3832 | callee-allocated save area |
3833 | for register varargs |
3834 | |
3835 +-------------------------------+ <-- hard_frame_pointer_rtx;
3836 | | stack_pointer_rtx + gp_sp_offset
3837 | GPR save area | + UNITS_PER_WORD
3838 | |
3839 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
3840 | | + UNITS_PER_HWVALUE
3841 | FPR save area |
3842 | |
3843 +-------------------------------+ <-- frame_pointer_rtx (virtual)
3844 | |
3845 | local variables |
3846 | |
3847 P +-------------------------------+
3848 | |
3849 | outgoing stack arguments |
3850 | |
3851 +-------------------------------+ <-- stack_pointer_rtx
3852
3853 Dynamic stack allocations such as alloca insert data at point P.
3854 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
3855 hard_frame_pointer_rtx unchanged. */
3856
3857 static HOST_WIDE_INT riscv_first_stack_step (struct riscv_frame_info *frame);
3858
3859 static void
3860 riscv_compute_frame_info (void)
3861 {
3862 struct riscv_frame_info *frame;
3863 HOST_WIDE_INT offset;
3864 bool interrupt_save_prologue_temp = false;
3865 unsigned int regno, i, num_x_saved = 0, num_f_saved = 0;
3866
3867 frame = &cfun->machine->frame;
3868
3869 /* In an interrupt function, if we have a large frame, then we need to
3870 save/restore t0. We check for this before clearing the frame struct. */
3871 if (cfun->machine->interrupt_handler_p)
3872 {
3873 HOST_WIDE_INT step1 = riscv_first_stack_step (frame);
3874 if (! SMALL_OPERAND (frame->total_size - step1))
3875 interrupt_save_prologue_temp = true;
3876 }
3877
3878 memset (frame, 0, sizeof (*frame));
3879
3880 if (!cfun->machine->naked_p)
3881 {
3882 /* Find out which GPRs we need to save. */
3883 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
3884 if (riscv_save_reg_p (regno)
3885 || (interrupt_save_prologue_temp
3886 && (regno == RISCV_PROLOGUE_TEMP_REGNUM)))
3887 frame->mask |= 1 << (regno - GP_REG_FIRST), num_x_saved++;
3888
3889 /* If this function calls eh_return, we must also save and restore the
3890 EH data registers. */
3891 if (crtl->calls_eh_return)
3892 for (i = 0; (regno = EH_RETURN_DATA_REGNO (i)) != INVALID_REGNUM; i++)
3893 frame->mask |= 1 << (regno - GP_REG_FIRST), num_x_saved++;
3894
3895 /* Find out which FPRs we need to save. This loop must iterate over
3896 the same space as its companion in riscv_for_each_saved_reg. */
3897 if (TARGET_HARD_FLOAT)
3898 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
3899 if (riscv_save_reg_p (regno))
3900 frame->fmask |= 1 << (regno - FP_REG_FIRST), num_f_saved++;
3901 }
3902
3903 /* At the bottom of the frame are any outgoing stack arguments. */
3904 offset = RISCV_STACK_ALIGN (crtl->outgoing_args_size);
3905 /* Next are local stack variables. */
3906 offset += RISCV_STACK_ALIGN (get_frame_size ());
3907 /* The virtual frame pointer points above the local variables. */
3908 frame->frame_pointer_offset = offset;
3909 /* Next are the callee-saved FPRs. */
3910 if (frame->fmask)
3911 offset += RISCV_STACK_ALIGN (num_f_saved * UNITS_PER_FP_REG);
3912 frame->fp_sp_offset = offset - UNITS_PER_FP_REG;
3913 /* Next are the callee-saved GPRs. */
3914 if (frame->mask)
3915 {
3916 unsigned x_save_size = RISCV_STACK_ALIGN (num_x_saved * UNITS_PER_WORD);
3917 unsigned num_save_restore = 1 + riscv_save_libcall_count (frame->mask);
3918
3919 /* Only use save/restore routines if they don't alter the stack size. */
3920 if (RISCV_STACK_ALIGN (num_save_restore * UNITS_PER_WORD) == x_save_size)
3921 {
3922 /* Libcall saves/restores 3 registers at once, so we need to
3923 allocate 12 bytes for callee-saved register. */
3924 if (TARGET_RVE)
3925 x_save_size = 3 * UNITS_PER_WORD;
3926
3927 frame->save_libcall_adjustment = x_save_size;
3928 }
3929
3930 offset += x_save_size;
3931 }
3932 frame->gp_sp_offset = offset - UNITS_PER_WORD;
3933 /* The hard frame pointer points above the callee-saved GPRs. */
3934 frame->hard_frame_pointer_offset = offset;
3935 /* Above the hard frame pointer is the callee-allocated varags save area. */
3936 offset += RISCV_STACK_ALIGN (cfun->machine->varargs_size);
3937 /* Next is the callee-allocated area for pretend stack arguments. */
3938 offset += RISCV_STACK_ALIGN (crtl->args.pretend_args_size);
3939 /* Arg pointer must be below pretend args, but must be above alignment
3940 padding. */
3941 frame->arg_pointer_offset = offset - crtl->args.pretend_args_size;
3942 frame->total_size = offset;
3943 /* Next points the incoming stack pointer and any incoming arguments. */
3944
3945 /* Only use save/restore routines when the GPRs are atop the frame. */
3946 if (frame->hard_frame_pointer_offset != frame->total_size)
3947 frame->save_libcall_adjustment = 0;
3948 }
3949
3950 /* Make sure that we're not trying to eliminate to the wrong hard frame
3951 pointer. */
3952
3953 static bool
3954 riscv_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
3955 {
3956 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
3957 }
3958
3959 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
3960 or argument pointer. TO is either the stack pointer or hard frame
3961 pointer. */
3962
3963 HOST_WIDE_INT
3964 riscv_initial_elimination_offset (int from, int to)
3965 {
3966 HOST_WIDE_INT src, dest;
3967
3968 riscv_compute_frame_info ();
3969
3970 if (to == HARD_FRAME_POINTER_REGNUM)
3971 dest = cfun->machine->frame.hard_frame_pointer_offset;
3972 else if (to == STACK_POINTER_REGNUM)
3973 dest = 0; /* The stack pointer is the base of all offsets, hence 0. */
3974 else
3975 gcc_unreachable ();
3976
3977 if (from == FRAME_POINTER_REGNUM)
3978 src = cfun->machine->frame.frame_pointer_offset;
3979 else if (from == ARG_POINTER_REGNUM)
3980 src = cfun->machine->frame.arg_pointer_offset;
3981 else
3982 gcc_unreachable ();
3983
3984 return src - dest;
3985 }
3986
3987 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
3988 previous frame. */
3989
3990 rtx
3991 riscv_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
3992 {
3993 if (count != 0)
3994 return const0_rtx;
3995
3996 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
3997 }
3998
3999 /* Emit code to change the current function's return address to
4000 ADDRESS. SCRATCH is available as a scratch register, if needed.
4001 ADDRESS and SCRATCH are both word-mode GPRs. */
4002
4003 void
4004 riscv_set_return_address (rtx address, rtx scratch)
4005 {
4006 rtx slot_address;
4007
4008 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
4009 slot_address = riscv_add_offset (scratch, stack_pointer_rtx,
4010 cfun->machine->frame.gp_sp_offset);
4011 riscv_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
4012 }
4013
4014 /* A function to save or store a register. The first argument is the
4015 register and the second is the stack slot. */
4016 typedef void (*riscv_save_restore_fn) (rtx, rtx);
4017
4018 /* Use FN to save or restore register REGNO. MODE is the register's
4019 mode and OFFSET is the offset of its save slot from the current
4020 stack pointer. */
4021
4022 static void
4023 riscv_save_restore_reg (machine_mode mode, int regno,
4024 HOST_WIDE_INT offset, riscv_save_restore_fn fn)
4025 {
4026 rtx mem;
4027
4028 mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx, offset));
4029 fn (gen_rtx_REG (mode, regno), mem);
4030 }
4031
4032 /* Call FN for each register that is saved by the current function.
4033 SP_OFFSET is the offset of the current stack pointer from the start
4034 of the frame. */
4035
4036 static void
4037 riscv_for_each_saved_reg (HOST_WIDE_INT sp_offset, riscv_save_restore_fn fn,
4038 bool epilogue, bool maybe_eh_return)
4039 {
4040 HOST_WIDE_INT offset;
4041
4042 /* Save the link register and s-registers. */
4043 offset = cfun->machine->frame.gp_sp_offset - sp_offset;
4044 for (unsigned int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
4045 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
4046 {
4047 bool handle_reg = TRUE;
4048
4049 /* If this is a normal return in a function that calls the eh_return
4050 builtin, then do not restore the eh return data registers as that
4051 would clobber the return value. But we do still need to save them
4052 in the prologue, and restore them for an exception return, so we
4053 need special handling here. */
4054 if (epilogue && !maybe_eh_return && crtl->calls_eh_return)
4055 {
4056 unsigned int i, regnum;
4057
4058 for (i = 0; (regnum = EH_RETURN_DATA_REGNO (i)) != INVALID_REGNUM;
4059 i++)
4060 if (regno == regnum)
4061 {
4062 handle_reg = FALSE;
4063 break;
4064 }
4065 }
4066
4067 if (handle_reg)
4068 riscv_save_restore_reg (word_mode, regno, offset, fn);
4069 offset -= UNITS_PER_WORD;
4070 }
4071
4072 /* This loop must iterate over the same space as its companion in
4073 riscv_compute_frame_info. */
4074 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
4075 for (unsigned int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
4076 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
4077 {
4078 machine_mode mode = TARGET_DOUBLE_FLOAT ? DFmode : SFmode;
4079
4080 riscv_save_restore_reg (mode, regno, offset, fn);
4081 offset -= GET_MODE_SIZE (mode);
4082 }
4083 }
4084
4085 /* Save register REG to MEM. Make the instruction frame-related. */
4086
4087 static void
4088 riscv_save_reg (rtx reg, rtx mem)
4089 {
4090 riscv_emit_move (mem, reg);
4091 riscv_set_frame_expr (riscv_frame_set (mem, reg));
4092 }
4093
4094 /* Restore register REG from MEM. */
4095
4096 static void
4097 riscv_restore_reg (rtx reg, rtx mem)
4098 {
4099 rtx insn = riscv_emit_move (reg, mem);
4100 rtx dwarf = NULL_RTX;
4101 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
4102
4103 if (epilogue_cfa_sp_offset && REGNO (reg) == HARD_FRAME_POINTER_REGNUM)
4104 {
4105 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4106 GEN_INT (epilogue_cfa_sp_offset));
4107 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
4108 }
4109
4110 REG_NOTES (insn) = dwarf;
4111 RTX_FRAME_RELATED_P (insn) = 1;
4112 }
4113
4114 /* For stack frames that can't be allocated with a single ADDI instruction,
4115 compute the best value to initially allocate. It must at a minimum
4116 allocate enough space to spill the callee-saved registers. If TARGET_RVC,
4117 try to pick a value that will allow compression of the register saves
4118 without adding extra instructions. */
4119
4120 static HOST_WIDE_INT
4121 riscv_first_stack_step (struct riscv_frame_info *frame)
4122 {
4123 if (SMALL_OPERAND (frame->total_size))
4124 return frame->total_size;
4125
4126 HOST_WIDE_INT min_first_step =
4127 RISCV_STACK_ALIGN (frame->total_size - frame->fp_sp_offset);
4128 HOST_WIDE_INT max_first_step = IMM_REACH / 2 - PREFERRED_STACK_BOUNDARY / 8;
4129 HOST_WIDE_INT min_second_step = frame->total_size - max_first_step;
4130 gcc_assert (min_first_step <= max_first_step);
4131
4132 /* As an optimization, use the least-significant bits of the total frame
4133 size, so that the second adjustment step is just LUI + ADD. */
4134 if (!SMALL_OPERAND (min_second_step)
4135 && frame->total_size % IMM_REACH < IMM_REACH / 2
4136 && frame->total_size % IMM_REACH >= min_first_step)
4137 return frame->total_size % IMM_REACH;
4138
4139 if (TARGET_RVC)
4140 {
4141 /* If we need two subtracts, and one is small enough to allow compressed
4142 loads and stores, then put that one first. */
4143 if (IN_RANGE (min_second_step, 0,
4144 (TARGET_64BIT ? SDSP_REACH : SWSP_REACH)))
4145 return MAX (min_second_step, min_first_step);
4146
4147 /* If we need LUI + ADDI + ADD for the second adjustment step, then start
4148 with the minimum first step, so that we can get compressed loads and
4149 stores. */
4150 else if (!SMALL_OPERAND (min_second_step))
4151 return min_first_step;
4152 }
4153
4154 return max_first_step;
4155 }
4156
4157 static rtx
4158 riscv_adjust_libcall_cfi_prologue ()
4159 {
4160 rtx dwarf = NULL_RTX;
4161 rtx adjust_sp_rtx, reg, mem, insn;
4162 int saved_size = cfun->machine->frame.save_libcall_adjustment;
4163 int offset;
4164
4165 for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
4166 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
4167 {
4168 /* The save order is ra, s0, s1, s2 to s11. */
4169 if (regno == RETURN_ADDR_REGNUM)
4170 offset = saved_size - UNITS_PER_WORD;
4171 else if (regno == S0_REGNUM)
4172 offset = saved_size - UNITS_PER_WORD * 2;
4173 else if (regno == S1_REGNUM)
4174 offset = saved_size - UNITS_PER_WORD * 3;
4175 else
4176 offset = saved_size - ((regno - S2_REGNUM + 4) * UNITS_PER_WORD);
4177
4178 reg = gen_rtx_REG (SImode, regno);
4179 mem = gen_frame_mem (SImode, plus_constant (Pmode,
4180 stack_pointer_rtx,
4181 offset));
4182
4183 insn = gen_rtx_SET (mem, reg);
4184 dwarf = alloc_reg_note (REG_CFA_OFFSET, insn, dwarf);
4185 }
4186
4187 /* Debug info for adjust sp. */
4188 adjust_sp_rtx = gen_add3_insn (stack_pointer_rtx,
4189 stack_pointer_rtx, GEN_INT (-saved_size));
4190 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, adjust_sp_rtx,
4191 dwarf);
4192 return dwarf;
4193 }
4194
4195 static void
4196 riscv_emit_stack_tie (void)
4197 {
4198 if (Pmode == SImode)
4199 emit_insn (gen_stack_tiesi (stack_pointer_rtx, hard_frame_pointer_rtx));
4200 else
4201 emit_insn (gen_stack_tiedi (stack_pointer_rtx, hard_frame_pointer_rtx));
4202 }
4203
4204 /* Expand the "prologue" pattern. */
4205
4206 void
4207 riscv_expand_prologue (void)
4208 {
4209 struct riscv_frame_info *frame = &cfun->machine->frame;
4210 HOST_WIDE_INT size = frame->total_size;
4211 unsigned mask = frame->mask;
4212 rtx insn;
4213
4214 if (flag_stack_usage_info)
4215 current_function_static_stack_size = size;
4216
4217 if (cfun->machine->naked_p)
4218 return;
4219
4220 /* When optimizing for size, call a subroutine to save the registers. */
4221 if (riscv_use_save_libcall (frame))
4222 {
4223 rtx dwarf = NULL_RTX;
4224 dwarf = riscv_adjust_libcall_cfi_prologue ();
4225
4226 size -= frame->save_libcall_adjustment;
4227 insn = emit_insn (riscv_gen_gpr_save_insn (frame));
4228 frame->mask = 0; /* Temporarily fib that we need not save GPRs. */
4229
4230 RTX_FRAME_RELATED_P (insn) = 1;
4231 REG_NOTES (insn) = dwarf;
4232 }
4233
4234 /* Save the registers. */
4235 if ((frame->mask | frame->fmask) != 0)
4236 {
4237 HOST_WIDE_INT step1 = MIN (size, riscv_first_stack_step (frame));
4238
4239 insn = gen_add3_insn (stack_pointer_rtx,
4240 stack_pointer_rtx,
4241 GEN_INT (-step1));
4242 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
4243 size -= step1;
4244 riscv_for_each_saved_reg (size, riscv_save_reg, false, false);
4245 }
4246
4247 frame->mask = mask; /* Undo the above fib. */
4248
4249 /* Set up the frame pointer, if we're using one. */
4250 if (frame_pointer_needed)
4251 {
4252 insn = gen_add3_insn (hard_frame_pointer_rtx, stack_pointer_rtx,
4253 GEN_INT (frame->hard_frame_pointer_offset - size));
4254 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
4255
4256 riscv_emit_stack_tie ();
4257 }
4258
4259 /* Allocate the rest of the frame. */
4260 if (size > 0)
4261 {
4262 if (SMALL_OPERAND (-size))
4263 {
4264 insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
4265 GEN_INT (-size));
4266 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
4267 }
4268 else
4269 {
4270 riscv_emit_move (RISCV_PROLOGUE_TEMP (Pmode), GEN_INT (-size));
4271 emit_insn (gen_add3_insn (stack_pointer_rtx,
4272 stack_pointer_rtx,
4273 RISCV_PROLOGUE_TEMP (Pmode)));
4274
4275 /* Describe the effect of the previous instructions. */
4276 insn = plus_constant (Pmode, stack_pointer_rtx, -size);
4277 insn = gen_rtx_SET (stack_pointer_rtx, insn);
4278 riscv_set_frame_expr (insn);
4279 }
4280 }
4281 }
4282
4283 static rtx
4284 riscv_adjust_libcall_cfi_epilogue ()
4285 {
4286 rtx dwarf = NULL_RTX;
4287 rtx adjust_sp_rtx, reg;
4288 int saved_size = cfun->machine->frame.save_libcall_adjustment;
4289
4290 /* Debug info for adjust sp. */
4291 adjust_sp_rtx = gen_add3_insn (stack_pointer_rtx,
4292 stack_pointer_rtx, GEN_INT (saved_size));
4293 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, adjust_sp_rtx,
4294 dwarf);
4295
4296 for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
4297 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
4298 {
4299 reg = gen_rtx_REG (SImode, regno);
4300 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
4301 }
4302
4303 return dwarf;
4304 }
4305
4306 /* Expand an "epilogue", "sibcall_epilogue", or "eh_return_internal" pattern;
4307 style says which. */
4308
4309 void
4310 riscv_expand_epilogue (int style)
4311 {
4312 /* Split the frame into two. STEP1 is the amount of stack we should
4313 deallocate before restoring the registers. STEP2 is the amount we
4314 should deallocate afterwards.
4315
4316 Start off by assuming that no registers need to be restored. */
4317 struct riscv_frame_info *frame = &cfun->machine->frame;
4318 unsigned mask = frame->mask;
4319 HOST_WIDE_INT step1 = frame->total_size;
4320 HOST_WIDE_INT step2 = 0;
4321 bool use_restore_libcall = ((style == NORMAL_RETURN)
4322 && riscv_use_save_libcall (frame));
4323 rtx ra = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
4324 rtx insn;
4325
4326 /* We need to add memory barrier to prevent read from deallocated stack. */
4327 bool need_barrier_p = (get_frame_size ()
4328 + cfun->machine->frame.arg_pointer_offset) != 0;
4329
4330 if (cfun->machine->naked_p)
4331 {
4332 gcc_assert (style == NORMAL_RETURN);
4333
4334 emit_jump_insn (gen_return ());
4335
4336 return;
4337 }
4338
4339 if ((style == NORMAL_RETURN) && riscv_can_use_return_insn ())
4340 {
4341 emit_jump_insn (gen_return ());
4342 return;
4343 }
4344
4345 /* Reset the epilogue cfa info before starting to emit the epilogue. */
4346 epilogue_cfa_sp_offset = 0;
4347
4348 /* Move past any dynamic stack allocations. */
4349 if (cfun->calls_alloca)
4350 {
4351 /* Emit a barrier to prevent loads from a deallocated stack. */
4352 riscv_emit_stack_tie ();
4353 need_barrier_p = false;
4354
4355 rtx adjust = GEN_INT (-frame->hard_frame_pointer_offset);
4356 if (!SMALL_OPERAND (INTVAL (adjust)))
4357 {
4358 riscv_emit_move (RISCV_PROLOGUE_TEMP (Pmode), adjust);
4359 adjust = RISCV_PROLOGUE_TEMP (Pmode);
4360 }
4361
4362 insn = emit_insn (
4363 gen_add3_insn (stack_pointer_rtx, hard_frame_pointer_rtx,
4364 adjust));
4365
4366 rtx dwarf = NULL_RTX;
4367 rtx cfa_adjust_value = gen_rtx_PLUS (
4368 Pmode, hard_frame_pointer_rtx,
4369 GEN_INT (-frame->hard_frame_pointer_offset));
4370 rtx cfa_adjust_rtx = gen_rtx_SET (stack_pointer_rtx, cfa_adjust_value);
4371 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, cfa_adjust_rtx, dwarf);
4372 RTX_FRAME_RELATED_P (insn) = 1;
4373
4374 REG_NOTES (insn) = dwarf;
4375 }
4376
4377 /* If we need to restore registers, deallocate as much stack as
4378 possible in the second step without going out of range. */
4379 if ((frame->mask | frame->fmask) != 0)
4380 {
4381 step2 = riscv_first_stack_step (frame);
4382 step1 -= step2;
4383 }
4384
4385 /* Set TARGET to BASE + STEP1. */
4386 if (step1 > 0)
4387 {
4388 /* Emit a barrier to prevent loads from a deallocated stack. */
4389 riscv_emit_stack_tie ();
4390 need_barrier_p = false;
4391
4392 /* Get an rtx for STEP1 that we can add to BASE. */
4393 rtx adjust = GEN_INT (step1);
4394 if (!SMALL_OPERAND (step1))
4395 {
4396 riscv_emit_move (RISCV_PROLOGUE_TEMP (Pmode), adjust);
4397 adjust = RISCV_PROLOGUE_TEMP (Pmode);
4398 }
4399
4400 insn = emit_insn (
4401 gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx, adjust));
4402
4403 rtx dwarf = NULL_RTX;
4404 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4405 GEN_INT (step2));
4406
4407 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
4408 RTX_FRAME_RELATED_P (insn) = 1;
4409
4410 REG_NOTES (insn) = dwarf;
4411 }
4412 else if (frame_pointer_needed)
4413 {
4414 /* Tell riscv_restore_reg to emit dwarf to redefine CFA when restoring
4415 old value of FP. */
4416 epilogue_cfa_sp_offset = step2;
4417 }
4418
4419 if (use_restore_libcall)
4420 frame->mask = 0; /* Temporarily fib that we need not save GPRs. */
4421
4422 /* Restore the registers. */
4423 riscv_for_each_saved_reg (frame->total_size - step2, riscv_restore_reg,
4424 true, style == EXCEPTION_RETURN);
4425
4426 if (use_restore_libcall)
4427 {
4428 frame->mask = mask; /* Undo the above fib. */
4429 gcc_assert (step2 >= frame->save_libcall_adjustment);
4430 step2 -= frame->save_libcall_adjustment;
4431 }
4432
4433 if (need_barrier_p)
4434 riscv_emit_stack_tie ();
4435
4436 /* Deallocate the final bit of the frame. */
4437 if (step2 > 0)
4438 {
4439 insn = emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
4440 GEN_INT (step2)));
4441
4442 rtx dwarf = NULL_RTX;
4443 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
4444 const0_rtx);
4445 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
4446 RTX_FRAME_RELATED_P (insn) = 1;
4447
4448 REG_NOTES (insn) = dwarf;
4449 }
4450
4451 if (use_restore_libcall)
4452 {
4453 rtx dwarf = riscv_adjust_libcall_cfi_epilogue ();
4454 insn = emit_insn (gen_gpr_restore (GEN_INT (riscv_save_libcall_count (mask))));
4455 RTX_FRAME_RELATED_P (insn) = 1;
4456 REG_NOTES (insn) = dwarf;
4457
4458 emit_jump_insn (gen_gpr_restore_return (ra));
4459 return;
4460 }
4461
4462 /* Add in the __builtin_eh_return stack adjustment. */
4463 if ((style == EXCEPTION_RETURN) && crtl->calls_eh_return)
4464 emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
4465 EH_RETURN_STACKADJ_RTX));
4466
4467 /* Return from interrupt. */
4468 if (cfun->machine->interrupt_handler_p)
4469 {
4470 enum riscv_privilege_levels mode = cfun->machine->interrupt_mode;
4471
4472 gcc_assert (mode != UNKNOWN_MODE);
4473
4474 if (mode == MACHINE_MODE)
4475 emit_jump_insn (gen_riscv_mret ());
4476 else if (mode == SUPERVISOR_MODE)
4477 emit_jump_insn (gen_riscv_sret ());
4478 else
4479 emit_jump_insn (gen_riscv_uret ());
4480 }
4481 else if (style != SIBCALL_RETURN)
4482 emit_jump_insn (gen_simple_return_internal (ra));
4483 }
4484
4485 /* Implement EPILOGUE_USES. */
4486
4487 bool
4488 riscv_epilogue_uses (unsigned int regno)
4489 {
4490 if (regno == RETURN_ADDR_REGNUM)
4491 return true;
4492
4493 if (epilogue_completed && cfun->machine->interrupt_handler_p)
4494 {
4495 /* An interrupt function restores temp regs, so we must indicate that
4496 they are live at function end. */
4497 if (df_regs_ever_live_p (regno)
4498 || (!crtl->is_leaf && call_used_or_fixed_reg_p (regno)))
4499 return true;
4500 }
4501
4502 return false;
4503 }
4504
4505 /* Return nonzero if this function is known to have a null epilogue.
4506 This allows the optimizer to omit jumps to jumps if no stack
4507 was created. */
4508
4509 bool
4510 riscv_can_use_return_insn (void)
4511 {
4512 return (reload_completed && cfun->machine->frame.total_size == 0
4513 && ! cfun->machine->interrupt_handler_p);
4514 }
4515
4516 /* Given that there exists at least one variable that is set (produced)
4517 by OUT_INSN and read (consumed) by IN_INSN, return true iff
4518 IN_INSN represents one or more memory store operations and none of
4519 the variables set by OUT_INSN is used by IN_INSN as the address of a
4520 store operation. If either IN_INSN or OUT_INSN does not represent
4521 a "single" RTL SET expression (as loosely defined by the
4522 implementation of the single_set function) or a PARALLEL with only
4523 SETs, CLOBBERs, and USEs inside, this function returns false.
4524
4525 Borrowed from rs6000, riscv_store_data_bypass_p checks for certain
4526 conditions that result in assertion failures in the generic
4527 store_data_bypass_p function and returns FALSE in such cases.
4528
4529 This is required to make -msave-restore work with the sifive-7
4530 pipeline description. */
4531
4532 bool
4533 riscv_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
4534 {
4535 rtx out_set, in_set;
4536 rtx out_pat, in_pat;
4537 rtx out_exp, in_exp;
4538 int i, j;
4539
4540 in_set = single_set (in_insn);
4541 if (in_set)
4542 {
4543 if (MEM_P (SET_DEST (in_set)))
4544 {
4545 out_set = single_set (out_insn);
4546 if (!out_set)
4547 {
4548 out_pat = PATTERN (out_insn);
4549 if (GET_CODE (out_pat) == PARALLEL)
4550 {
4551 for (i = 0; i < XVECLEN (out_pat, 0); i++)
4552 {
4553 out_exp = XVECEXP (out_pat, 0, i);
4554 if ((GET_CODE (out_exp) == CLOBBER)
4555 || (GET_CODE (out_exp) == USE))
4556 continue;
4557 else if (GET_CODE (out_exp) != SET)
4558 return false;
4559 }
4560 }
4561 }
4562 }
4563 }
4564 else
4565 {
4566 in_pat = PATTERN (in_insn);
4567 if (GET_CODE (in_pat) != PARALLEL)
4568 return false;
4569
4570 for (i = 0; i < XVECLEN (in_pat, 0); i++)
4571 {
4572 in_exp = XVECEXP (in_pat, 0, i);
4573 if ((GET_CODE (in_exp) == CLOBBER) || (GET_CODE (in_exp) == USE))
4574 continue;
4575 else if (GET_CODE (in_exp) != SET)
4576 return false;
4577
4578 if (MEM_P (SET_DEST (in_exp)))
4579 {
4580 out_set = single_set (out_insn);
4581 if (!out_set)
4582 {
4583 out_pat = PATTERN (out_insn);
4584 if (GET_CODE (out_pat) != PARALLEL)
4585 return false;
4586 for (j = 0; j < XVECLEN (out_pat, 0); j++)
4587 {
4588 out_exp = XVECEXP (out_pat, 0, j);
4589 if ((GET_CODE (out_exp) == CLOBBER)
4590 || (GET_CODE (out_exp) == USE))
4591 continue;
4592 else if (GET_CODE (out_exp) != SET)
4593 return false;
4594 }
4595 }
4596 }
4597 }
4598 }
4599
4600 return store_data_bypass_p (out_insn, in_insn);
4601 }
4602
4603 /* Implement TARGET_SECONDARY_MEMORY_NEEDED.
4604
4605 When floating-point registers are wider than integer ones, moves between
4606 them must go through memory. */
4607
4608 static bool
4609 riscv_secondary_memory_needed (machine_mode mode, reg_class_t class1,
4610 reg_class_t class2)
4611 {
4612 return (GET_MODE_SIZE (mode) > UNITS_PER_WORD
4613 && (class1 == FP_REGS) != (class2 == FP_REGS));
4614 }
4615
4616 /* Implement TARGET_REGISTER_MOVE_COST. */
4617
4618 static int
4619 riscv_register_move_cost (machine_mode mode,
4620 reg_class_t from, reg_class_t to)
4621 {
4622 return riscv_secondary_memory_needed (mode, from, to) ? 8 : 2;
4623 }
4624
4625 /* Implement TARGET_HARD_REGNO_NREGS. */
4626
4627 static unsigned int
4628 riscv_hard_regno_nregs (unsigned int regno, machine_mode mode)
4629 {
4630 if (FP_REG_P (regno))
4631 return (GET_MODE_SIZE (mode) + UNITS_PER_FP_REG - 1) / UNITS_PER_FP_REG;
4632
4633 /* All other registers are word-sized. */
4634 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4635 }
4636
4637 /* Implement TARGET_HARD_REGNO_MODE_OK. */
4638
4639 static bool
4640 riscv_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
4641 {
4642 unsigned int nregs = riscv_hard_regno_nregs (regno, mode);
4643
4644 if (GP_REG_P (regno))
4645 {
4646 if (!GP_REG_P (regno + nregs - 1))
4647 return false;
4648 }
4649 else if (FP_REG_P (regno))
4650 {
4651 if (!FP_REG_P (regno + nregs - 1))
4652 return false;
4653
4654 if (GET_MODE_CLASS (mode) != MODE_FLOAT
4655 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
4656 return false;
4657
4658 /* Only use callee-saved registers if a potential callee is guaranteed
4659 to spill the requisite width. */
4660 if (GET_MODE_UNIT_SIZE (mode) > UNITS_PER_FP_REG
4661 || (!call_used_or_fixed_reg_p (regno)
4662 && GET_MODE_UNIT_SIZE (mode) > UNITS_PER_FP_ARG))
4663 return false;
4664 }
4665 else
4666 return false;
4667
4668 /* Require same callee-savedness for all registers. */
4669 for (unsigned i = 1; i < nregs; i++)
4670 if (call_used_or_fixed_reg_p (regno)
4671 != call_used_or_fixed_reg_p (regno + i))
4672 return false;
4673
4674 return true;
4675 }
4676
4677 /* Implement TARGET_MODES_TIEABLE_P.
4678
4679 Don't allow floating-point modes to be tied, since type punning of
4680 single-precision and double-precision is implementation defined. */
4681
4682 static bool
4683 riscv_modes_tieable_p (machine_mode mode1, machine_mode mode2)
4684 {
4685 return (mode1 == mode2
4686 || !(GET_MODE_CLASS (mode1) == MODE_FLOAT
4687 && GET_MODE_CLASS (mode2) == MODE_FLOAT));
4688 }
4689
4690 /* Implement CLASS_MAX_NREGS. */
4691
4692 static unsigned char
4693 riscv_class_max_nregs (reg_class_t rclass, machine_mode mode)
4694 {
4695 if (reg_class_subset_p (FP_REGS, rclass))
4696 return riscv_hard_regno_nregs (FP_REG_FIRST, mode);
4697
4698 if (reg_class_subset_p (GR_REGS, rclass))
4699 return riscv_hard_regno_nregs (GP_REG_FIRST, mode);
4700
4701 return 0;
4702 }
4703
4704 /* Implement TARGET_MEMORY_MOVE_COST. */
4705
4706 static int
4707 riscv_memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
4708 {
4709 return (tune_param->memory_cost
4710 + memory_move_secondary_cost (mode, rclass, in));
4711 }
4712
4713 /* Return the number of instructions that can be issued per cycle. */
4714
4715 static int
4716 riscv_issue_rate (void)
4717 {
4718 return tune_param->issue_rate;
4719 }
4720
4721 /* Auxiliary function to emit RISC-V ELF attribute. */
4722 static void
4723 riscv_emit_attribute ()
4724 {
4725 fprintf (asm_out_file, "\t.attribute arch, \"%s\"\n",
4726 riscv_arch_str ().c_str ());
4727
4728 fprintf (asm_out_file, "\t.attribute unaligned_access, %d\n",
4729 TARGET_STRICT_ALIGN ? 0 : 1);
4730
4731 fprintf (asm_out_file, "\t.attribute stack_align, %d\n",
4732 riscv_stack_boundary / 8);
4733 }
4734
4735 /* Implement TARGET_ASM_FILE_START. */
4736
4737 static void
4738 riscv_file_start (void)
4739 {
4740 default_file_start ();
4741
4742 /* Instruct GAS to generate position-[in]dependent code. */
4743 fprintf (asm_out_file, "\t.option %spic\n", (flag_pic ? "" : "no"));
4744
4745 /* If the user specifies "-mno-relax" on the command line then disable linker
4746 relaxation in the assembler. */
4747 if (! riscv_mrelax)
4748 fprintf (asm_out_file, "\t.option norelax\n");
4749
4750 if (riscv_emit_attribute_p)
4751 riscv_emit_attribute ();
4752 }
4753
4754 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
4755 in order to avoid duplicating too much logic from elsewhere. */
4756
4757 static void
4758 riscv_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
4759 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
4760 tree function)
4761 {
4762 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
4763 rtx this_rtx, temp1, temp2, fnaddr;
4764 rtx_insn *insn;
4765
4766 /* Pretend to be a post-reload pass while generating rtl. */
4767 reload_completed = 1;
4768
4769 /* Mark the end of the (empty) prologue. */
4770 emit_note (NOTE_INSN_PROLOGUE_END);
4771
4772 /* Determine if we can use a sibcall to call FUNCTION directly. */
4773 fnaddr = gen_rtx_MEM (FUNCTION_MODE, XEXP (DECL_RTL (function), 0));
4774
4775 /* We need two temporary registers in some cases. */
4776 temp1 = gen_rtx_REG (Pmode, RISCV_PROLOGUE_TEMP_REGNUM);
4777 temp2 = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
4778
4779 /* Find out which register contains the "this" pointer. */
4780 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
4781 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
4782 else
4783 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
4784
4785 /* Add DELTA to THIS_RTX. */
4786 if (delta != 0)
4787 {
4788 rtx offset = GEN_INT (delta);
4789 if (!SMALL_OPERAND (delta))
4790 {
4791 riscv_emit_move (temp1, offset);
4792 offset = temp1;
4793 }
4794 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
4795 }
4796
4797 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
4798 if (vcall_offset != 0)
4799 {
4800 rtx addr;
4801
4802 /* Set TEMP1 to *THIS_RTX. */
4803 riscv_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
4804
4805 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
4806 addr = riscv_add_offset (temp2, temp1, vcall_offset);
4807
4808 /* Load the offset and add it to THIS_RTX. */
4809 riscv_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
4810 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
4811 }
4812
4813 /* Jump to the target function. */
4814 insn = emit_call_insn (gen_sibcall (fnaddr, const0_rtx, NULL, const0_rtx));
4815 SIBLING_CALL_P (insn) = 1;
4816
4817 /* Run just enough of rest_of_compilation. This sequence was
4818 "borrowed" from alpha.c. */
4819 insn = get_insns ();
4820 split_all_insns_noflow ();
4821 shorten_branches (insn);
4822 assemble_start_function (thunk_fndecl, fnname);
4823 final_start_function (insn, file, 1);
4824 final (insn, file, 1);
4825 final_end_function ();
4826 assemble_end_function (thunk_fndecl, fnname);
4827
4828 /* Clean up the vars set above. Note that final_end_function resets
4829 the global pointer for us. */
4830 reload_completed = 0;
4831 }
4832
4833 /* Allocate a chunk of memory for per-function machine-dependent data. */
4834
4835 static struct machine_function *
4836 riscv_init_machine_status (void)
4837 {
4838 return ggc_cleared_alloc<machine_function> ();
4839 }
4840
4841 /* Implement TARGET_OPTION_OVERRIDE. */
4842
4843 static void
4844 riscv_option_override (void)
4845 {
4846 const struct riscv_tune_info *cpu;
4847
4848 #ifdef SUBTARGET_OVERRIDE_OPTIONS
4849 SUBTARGET_OVERRIDE_OPTIONS;
4850 #endif
4851
4852 flag_pcc_struct_return = 0;
4853
4854 if (flag_pic)
4855 g_switch_value = 0;
4856
4857 /* The presence of the M extension implies that division instructions
4858 are present, so include them unless explicitly disabled. */
4859 if (TARGET_MUL && (target_flags_explicit & MASK_DIV) == 0)
4860 target_flags |= MASK_DIV;
4861 else if (!TARGET_MUL && TARGET_DIV)
4862 error ("%<-mdiv%> requires %<-march%> to subsume the %<M%> extension");
4863
4864 /* Likewise floating-point division and square root. */
4865 if (TARGET_HARD_FLOAT && (target_flags_explicit & MASK_FDIV) == 0)
4866 target_flags |= MASK_FDIV;
4867
4868 /* Handle -mtune, use -mcpu if -mtune is not given, and use default -mtune
4869 if -mtune and -mcpu both not not given. */
4870 cpu = riscv_parse_tune (riscv_tune_string ? riscv_tune_string :
4871 (riscv_cpu_string ? riscv_cpu_string :
4872 RISCV_TUNE_STRING_DEFAULT));
4873 riscv_microarchitecture = cpu->microarchitecture;
4874 tune_param = optimize_size ? &optimize_size_tune_info : cpu->tune_param;
4875
4876 /* Use -mtune's setting for slow_unaligned_access, even when optimizing
4877 for size. For architectures that trap and emulate unaligned accesses,
4878 the performance cost is too great, even for -Os. Similarly, if
4879 -m[no-]strict-align is left unspecified, heed -mtune's advice. */
4880 riscv_slow_unaligned_access_p = (cpu->tune_param->slow_unaligned_access
4881 || TARGET_STRICT_ALIGN);
4882 if ((target_flags_explicit & MASK_STRICT_ALIGN) == 0
4883 && cpu->tune_param->slow_unaligned_access)
4884 target_flags |= MASK_STRICT_ALIGN;
4885
4886 /* If the user hasn't specified a branch cost, use the processor's
4887 default. */
4888 if (riscv_branch_cost == 0)
4889 riscv_branch_cost = tune_param->branch_cost;
4890
4891 /* Function to allocate machine-dependent function status. */
4892 init_machine_status = &riscv_init_machine_status;
4893
4894 if (flag_pic)
4895 riscv_cmodel = CM_PIC;
4896
4897 /* We get better code with explicit relocs for CM_MEDLOW, but
4898 worse code for the others (for now). Pick the best default. */
4899 if ((target_flags_explicit & MASK_EXPLICIT_RELOCS) == 0)
4900 if (riscv_cmodel == CM_MEDLOW)
4901 target_flags |= MASK_EXPLICIT_RELOCS;
4902
4903 /* Require that the ISA supports the requested floating-point ABI. */
4904 if (UNITS_PER_FP_ARG > (TARGET_HARD_FLOAT ? UNITS_PER_FP_REG : 0))
4905 error ("requested ABI requires %<-march%> to subsume the %qc extension",
4906 UNITS_PER_FP_ARG > 8 ? 'Q' : (UNITS_PER_FP_ARG > 4 ? 'D' : 'F'));
4907
4908 if (TARGET_RVE && riscv_abi != ABI_ILP32E)
4909 error ("rv32e requires ilp32e ABI");
4910
4911 /* We do not yet support ILP32 on RV64. */
4912 if (BITS_PER_WORD != POINTER_SIZE)
4913 error ("ABI requires %<-march=rv%d%>", POINTER_SIZE);
4914
4915 /* Validate -mpreferred-stack-boundary= value. */
4916 riscv_stack_boundary = ABI_STACK_BOUNDARY;
4917 if (riscv_preferred_stack_boundary_arg)
4918 {
4919 int min = ctz_hwi (STACK_BOUNDARY / 8);
4920 int max = 8;
4921
4922 if (!IN_RANGE (riscv_preferred_stack_boundary_arg, min, max))
4923 error ("%<-mpreferred-stack-boundary=%d%> must be between %d and %d",
4924 riscv_preferred_stack_boundary_arg, min, max);
4925
4926 riscv_stack_boundary = 8 << riscv_preferred_stack_boundary_arg;
4927 }
4928
4929 if (riscv_emit_attribute_p < 0)
4930 #ifdef HAVE_AS_RISCV_ATTRIBUTE
4931 riscv_emit_attribute_p = TARGET_RISCV_ATTRIBUTE;
4932 #else
4933 riscv_emit_attribute_p = 0;
4934
4935 if (riscv_emit_attribute_p)
4936 error ("%<-mriscv-attribute%> RISC-V ELF attribute requires GNU as 2.32"
4937 " [%<-mriscv-attribute%>]");
4938 #endif
4939
4940 if (riscv_stack_protector_guard == SSP_GLOBAL
4941 && OPTION_SET_P (riscv_stack_protector_guard_offset_str))
4942 {
4943 error ("incompatible options %<-mstack-protector-guard=global%> and "
4944 "%<-mstack-protector-guard-offset=%s%>",
4945 riscv_stack_protector_guard_offset_str);
4946 }
4947
4948 if (riscv_stack_protector_guard == SSP_TLS
4949 && !(OPTION_SET_P (riscv_stack_protector_guard_offset_str)
4950 && OPTION_SET_P (riscv_stack_protector_guard_reg_str)))
4951 {
4952 error ("both %<-mstack-protector-guard-offset%> and "
4953 "%<-mstack-protector-guard-reg%> must be used "
4954 "with %<-mstack-protector-guard=sysreg%>");
4955 }
4956
4957 if (OPTION_SET_P (riscv_stack_protector_guard_reg_str))
4958 {
4959 const char *str = riscv_stack_protector_guard_reg_str;
4960 int reg = decode_reg_name (str);
4961
4962 if (!IN_RANGE (reg, GP_REG_FIRST + 1, GP_REG_LAST))
4963 error ("%qs is not a valid base register in %qs", str,
4964 "-mstack-protector-guard-reg=");
4965
4966 riscv_stack_protector_guard_reg = reg;
4967 }
4968
4969 if (OPTION_SET_P (riscv_stack_protector_guard_offset_str))
4970 {
4971 char *end;
4972 const char *str = riscv_stack_protector_guard_offset_str;
4973 errno = 0;
4974 long offs = strtol (riscv_stack_protector_guard_offset_str, &end, 0);
4975
4976 if (!*str || *end || errno)
4977 error ("%qs is not a valid number in %qs", str,
4978 "-mstack-protector-guard-offset=");
4979
4980 if (!SMALL_OPERAND (offs))
4981 error ("%qs is not a valid offset in %qs", str,
4982 "-mstack-protector-guard-offset=");
4983
4984 riscv_stack_protector_guard_offset = offs;
4985 }
4986
4987 }
4988
4989 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
4990
4991 static void
4992 riscv_conditional_register_usage (void)
4993 {
4994 /* We have only x0~x15 on RV32E. */
4995 if (TARGET_RVE)
4996 {
4997 for (int r = 16; r <= 31; r++)
4998 fixed_regs[r] = 1;
4999 }
5000
5001 if (riscv_abi == ABI_ILP32E)
5002 {
5003 for (int r = 16; r <= 31; r++)
5004 call_used_regs[r] = 1;
5005 }
5006
5007 if (!TARGET_HARD_FLOAT)
5008 {
5009 for (int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
5010 fixed_regs[regno] = call_used_regs[regno] = 1;
5011 }
5012
5013 /* In the soft-float ABI, there are no callee-saved FP registers. */
5014 if (UNITS_PER_FP_ARG == 0)
5015 {
5016 for (int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
5017 call_used_regs[regno] = 1;
5018 }
5019 }
5020
5021 /* Return a register priority for hard reg REGNO. */
5022
5023 static int
5024 riscv_register_priority (int regno)
5025 {
5026 /* Favor compressed registers to improve the odds of RVC instruction
5027 selection. */
5028 if (riscv_compressed_reg_p (regno))
5029 return 1;
5030
5031 return 0;
5032 }
5033
5034 /* Implement TARGET_TRAMPOLINE_INIT. */
5035
5036 static void
5037 riscv_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
5038 {
5039 rtx addr, end_addr, mem;
5040 uint32_t trampoline[4];
5041 unsigned int i;
5042 HOST_WIDE_INT static_chain_offset, target_function_offset;
5043
5044 /* Work out the offsets of the pointers from the start of the
5045 trampoline code. */
5046 gcc_assert (ARRAY_SIZE (trampoline) * 4 == TRAMPOLINE_CODE_SIZE);
5047
5048 /* Get pointers to the beginning and end of the code block. */
5049 addr = force_reg (Pmode, XEXP (m_tramp, 0));
5050 end_addr = riscv_force_binary (Pmode, PLUS, addr,
5051 GEN_INT (TRAMPOLINE_CODE_SIZE));
5052
5053
5054 if (Pmode == SImode)
5055 {
5056 chain_value = force_reg (Pmode, chain_value);
5057
5058 rtx target_function = force_reg (Pmode, XEXP (DECL_RTL (fndecl), 0));
5059 /* lui t2, hi(chain)
5060 lui t0, hi(func)
5061 addi t2, t2, lo(chain)
5062 jr t0, lo(func)
5063 */
5064 unsigned HOST_WIDE_INT lui_hi_chain_code, lui_hi_func_code;
5065 unsigned HOST_WIDE_INT lo_chain_code, lo_func_code;
5066
5067 rtx uimm_mask = force_reg (SImode, gen_int_mode (-IMM_REACH, SImode));
5068
5069 /* 0xfff. */
5070 rtx imm12_mask = gen_reg_rtx (SImode);
5071 emit_insn (gen_one_cmplsi2 (imm12_mask, uimm_mask));
5072
5073 rtx fixup_value = force_reg (SImode, gen_int_mode (IMM_REACH/2, SImode));
5074
5075 /* Gen lui t2, hi(chain). */
5076 rtx hi_chain = riscv_force_binary (SImode, PLUS, chain_value,
5077 fixup_value);
5078 hi_chain = riscv_force_binary (SImode, AND, hi_chain,
5079 uimm_mask);
5080 lui_hi_chain_code = OPCODE_LUI | (STATIC_CHAIN_REGNUM << SHIFT_RD);
5081 rtx lui_hi_chain = riscv_force_binary (SImode, IOR, hi_chain,
5082 gen_int_mode (lui_hi_chain_code, SImode));
5083
5084 mem = adjust_address (m_tramp, SImode, 0);
5085 riscv_emit_move (mem, riscv_swap_instruction (lui_hi_chain));
5086
5087 /* Gen lui t0, hi(func). */
5088 rtx hi_func = riscv_force_binary (SImode, PLUS, target_function,
5089 fixup_value);
5090 hi_func = riscv_force_binary (SImode, AND, hi_func,
5091 uimm_mask);
5092 lui_hi_func_code = OPCODE_LUI | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RD);
5093 rtx lui_hi_func = riscv_force_binary (SImode, IOR, hi_func,
5094 gen_int_mode (lui_hi_func_code, SImode));
5095
5096 mem = adjust_address (m_tramp, SImode, 1 * GET_MODE_SIZE (SImode));
5097 riscv_emit_move (mem, riscv_swap_instruction (lui_hi_func));
5098
5099 /* Gen addi t2, t2, lo(chain). */
5100 rtx lo_chain = riscv_force_binary (SImode, AND, chain_value,
5101 imm12_mask);
5102 lo_chain = riscv_force_binary (SImode, ASHIFT, lo_chain, GEN_INT (20));
5103
5104 lo_chain_code = OPCODE_ADDI
5105 | (STATIC_CHAIN_REGNUM << SHIFT_RD)
5106 | (STATIC_CHAIN_REGNUM << SHIFT_RS1);
5107
5108 rtx addi_lo_chain = riscv_force_binary (SImode, IOR, lo_chain,
5109 force_reg (SImode, GEN_INT (lo_chain_code)));
5110
5111 mem = adjust_address (m_tramp, SImode, 2 * GET_MODE_SIZE (SImode));
5112 riscv_emit_move (mem, riscv_swap_instruction (addi_lo_chain));
5113
5114 /* Gen jr t0, lo(func). */
5115 rtx lo_func = riscv_force_binary (SImode, AND, target_function,
5116 imm12_mask);
5117 lo_func = riscv_force_binary (SImode, ASHIFT, lo_func, GEN_INT (20));
5118
5119 lo_func_code = OPCODE_JALR | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RS1);
5120
5121 rtx jr_lo_func = riscv_force_binary (SImode, IOR, lo_func,
5122 force_reg (SImode, GEN_INT (lo_func_code)));
5123
5124 mem = adjust_address (m_tramp, SImode, 3 * GET_MODE_SIZE (SImode));
5125 riscv_emit_move (mem, riscv_swap_instruction (jr_lo_func));
5126 }
5127 else
5128 {
5129 static_chain_offset = TRAMPOLINE_CODE_SIZE;
5130 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
5131
5132 /* auipc t2, 0
5133 l[wd] t0, target_function_offset(t2)
5134 l[wd] t2, static_chain_offset(t2)
5135 jr t0
5136 */
5137 trampoline[0] = OPCODE_AUIPC | (STATIC_CHAIN_REGNUM << SHIFT_RD);
5138 trampoline[1] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW)
5139 | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RD)
5140 | (STATIC_CHAIN_REGNUM << SHIFT_RS1)
5141 | (target_function_offset << SHIFT_IMM);
5142 trampoline[2] = (Pmode == DImode ? OPCODE_LD : OPCODE_LW)
5143 | (STATIC_CHAIN_REGNUM << SHIFT_RD)
5144 | (STATIC_CHAIN_REGNUM << SHIFT_RS1)
5145 | (static_chain_offset << SHIFT_IMM);
5146 trampoline[3] = OPCODE_JALR | (RISCV_PROLOGUE_TEMP_REGNUM << SHIFT_RS1);
5147
5148 /* Copy the trampoline code. */
5149 for (i = 0; i < ARRAY_SIZE (trampoline); i++)
5150 {
5151 if (BYTES_BIG_ENDIAN)
5152 trampoline[i] = __builtin_bswap32(trampoline[i]);
5153 mem = adjust_address (m_tramp, SImode, i * GET_MODE_SIZE (SImode));
5154 riscv_emit_move (mem, gen_int_mode (trampoline[i], SImode));
5155 }
5156
5157 /* Set up the static chain pointer field. */
5158 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
5159 riscv_emit_move (mem, chain_value);
5160
5161 /* Set up the target function field. */
5162 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
5163 riscv_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
5164 }
5165
5166 /* Flush the code part of the trampoline. */
5167 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
5168 emit_insn (gen_clear_cache (addr, end_addr));
5169 }
5170
5171 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
5172
5173 static bool
5174 riscv_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
5175 tree exp ATTRIBUTE_UNUSED)
5176 {
5177 /* Don't use sibcalls when use save-restore routine. */
5178 if (TARGET_SAVE_RESTORE)
5179 return false;
5180
5181 /* Don't use sibcall for naked functions. */
5182 if (cfun->machine->naked_p)
5183 return false;
5184
5185 /* Don't use sibcall for interrupt functions. */
5186 if (cfun->machine->interrupt_handler_p)
5187 return false;
5188
5189 return true;
5190 }
5191
5192 /* Get the interrupt type, return UNKNOWN_MODE if it's not
5193 interrupt function. */
5194 static enum riscv_privilege_levels
5195 riscv_get_interrupt_type (tree decl)
5196 {
5197 gcc_assert (decl != NULL_TREE);
5198
5199 if ((TREE_CODE(decl) != FUNCTION_DECL)
5200 || (!riscv_interrupt_type_p (TREE_TYPE (decl))))
5201 return UNKNOWN_MODE;
5202
5203 tree attr_args
5204 = TREE_VALUE (lookup_attribute ("interrupt",
5205 TYPE_ATTRIBUTES (TREE_TYPE (decl))));
5206
5207 if (attr_args && TREE_CODE (TREE_VALUE (attr_args)) != VOID_TYPE)
5208 {
5209 const char *string = TREE_STRING_POINTER (TREE_VALUE (attr_args));
5210
5211 if (!strcmp (string, "user"))
5212 return USER_MODE;
5213 else if (!strcmp (string, "supervisor"))
5214 return SUPERVISOR_MODE;
5215 else /* Must be "machine". */
5216 return MACHINE_MODE;
5217 }
5218 else
5219 /* Interrupt attributes are machine mode by default. */
5220 return MACHINE_MODE;
5221 }
5222
5223 /* Implement `TARGET_SET_CURRENT_FUNCTION'. */
5224 /* Sanity cheching for above function attributes. */
5225 static void
5226 riscv_set_current_function (tree decl)
5227 {
5228 if (decl == NULL_TREE
5229 || current_function_decl == NULL_TREE
5230 || current_function_decl == error_mark_node
5231 || ! cfun->machine
5232 || cfun->machine->attributes_checked_p)
5233 return;
5234
5235 cfun->machine->naked_p = riscv_naked_function_p (decl);
5236 cfun->machine->interrupt_handler_p
5237 = riscv_interrupt_type_p (TREE_TYPE (decl));
5238
5239 if (cfun->machine->naked_p && cfun->machine->interrupt_handler_p)
5240 error ("function attributes %qs and %qs are mutually exclusive",
5241 "interrupt", "naked");
5242
5243 if (cfun->machine->interrupt_handler_p)
5244 {
5245 tree ret = TREE_TYPE (TREE_TYPE (decl));
5246 tree args = TYPE_ARG_TYPES (TREE_TYPE (decl));
5247
5248 if (TREE_CODE (ret) != VOID_TYPE)
5249 error ("%qs function cannot return a value", "interrupt");
5250
5251 if (args && TREE_CODE (TREE_VALUE (args)) != VOID_TYPE)
5252 error ("%qs function cannot have arguments", "interrupt");
5253
5254 cfun->machine->interrupt_mode = riscv_get_interrupt_type (decl);
5255
5256 gcc_assert (cfun->machine->interrupt_mode != UNKNOWN_MODE);
5257 }
5258
5259 /* Don't print the above diagnostics more than once. */
5260 cfun->machine->attributes_checked_p = 1;
5261 }
5262
5263 /* Implement TARGET_MERGE_DECL_ATTRIBUTES. */
5264 static tree
5265 riscv_merge_decl_attributes (tree olddecl, tree newdecl)
5266 {
5267 tree combined_attrs;
5268
5269 enum riscv_privilege_levels old_interrupt_type
5270 = riscv_get_interrupt_type (olddecl);
5271 enum riscv_privilege_levels new_interrupt_type
5272 = riscv_get_interrupt_type (newdecl);
5273
5274 /* Check old and new has same interrupt type. */
5275 if ((old_interrupt_type != UNKNOWN_MODE)
5276 && (new_interrupt_type != UNKNOWN_MODE)
5277 && (old_interrupt_type != new_interrupt_type))
5278 error ("%qs function cannot have different interrupt type", "interrupt");
5279
5280 /* Create combined attributes. */
5281 combined_attrs = merge_attributes (DECL_ATTRIBUTES (olddecl),
5282 DECL_ATTRIBUTES (newdecl));
5283
5284 return combined_attrs;
5285 }
5286
5287 /* Implement TARGET_CANNOT_COPY_INSN_P. */
5288
5289 static bool
5290 riscv_cannot_copy_insn_p (rtx_insn *insn)
5291 {
5292 return recog_memoized (insn) >= 0 && get_attr_cannot_copy (insn);
5293 }
5294
5295 /* Implement TARGET_SLOW_UNALIGNED_ACCESS. */
5296
5297 static bool
5298 riscv_slow_unaligned_access (machine_mode, unsigned int)
5299 {
5300 return riscv_slow_unaligned_access_p;
5301 }
5302
5303 /* Implement TARGET_CAN_CHANGE_MODE_CLASS. */
5304
5305 static bool
5306 riscv_can_change_mode_class (machine_mode, machine_mode, reg_class_t rclass)
5307 {
5308 return !reg_classes_intersect_p (FP_REGS, rclass);
5309 }
5310
5311
5312 /* Implement TARGET_CONSTANT_ALIGNMENT. */
5313
5314 static HOST_WIDE_INT
5315 riscv_constant_alignment (const_tree exp, HOST_WIDE_INT align)
5316 {
5317 if ((TREE_CODE (exp) == STRING_CST || TREE_CODE (exp) == CONSTRUCTOR)
5318 && (riscv_align_data_type == riscv_align_data_type_xlen))
5319 return MAX (align, BITS_PER_WORD);
5320 return align;
5321 }
5322
5323 /* Implement TARGET_PROMOTE_FUNCTION_MODE. */
5324
5325 /* This function is equivalent to default_promote_function_mode_always_promote
5326 except that it returns a promoted mode even if type is NULL_TREE. This is
5327 needed by libcalls which have no type (only a mode) such as fixed conversion
5328 routines that take a signed or unsigned char/short/int argument and convert
5329 it to a fixed type. */
5330
5331 static machine_mode
5332 riscv_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
5333 machine_mode mode,
5334 int *punsignedp ATTRIBUTE_UNUSED,
5335 const_tree fntype ATTRIBUTE_UNUSED,
5336 int for_return ATTRIBUTE_UNUSED)
5337 {
5338 int unsignedp;
5339
5340 if (type != NULL_TREE)
5341 return promote_mode (type, mode, punsignedp);
5342
5343 unsignedp = *punsignedp;
5344 PROMOTE_MODE (mode, unsignedp, type);
5345 *punsignedp = unsignedp;
5346 return mode;
5347 }
5348
5349 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
5350
5351 static void
5352 riscv_reorg (void)
5353 {
5354 /* Do nothing unless we have -msave-restore */
5355 if (TARGET_SAVE_RESTORE)
5356 riscv_remove_unneeded_save_restore_calls ();
5357 }
5358
5359 /* Return nonzero if register FROM_REGNO can be renamed to register
5360 TO_REGNO. */
5361
5362 bool
5363 riscv_hard_regno_rename_ok (unsigned from_regno ATTRIBUTE_UNUSED,
5364 unsigned to_regno)
5365 {
5366 /* Interrupt functions can only use registers that have already been
5367 saved by the prologue, even if they would normally be
5368 call-clobbered. */
5369 return !cfun->machine->interrupt_handler_p || df_regs_ever_live_p (to_regno);
5370 }
5371
5372 /* Implement TARGET_NEW_ADDRESS_PROFITABLE_P. */
5373
5374 bool
5375 riscv_new_address_profitable_p (rtx memref, rtx_insn *insn, rtx new_addr)
5376 {
5377 /* Prefer old address if it is less expensive. */
5378 addr_space_t as = MEM_ADDR_SPACE (memref);
5379 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
5380 int old_cost = address_cost (XEXP (memref, 0), GET_MODE (memref), as, speed);
5381 int new_cost = address_cost (new_addr, GET_MODE (memref), as, speed);
5382 return new_cost <= old_cost;
5383 }
5384
5385 /* Helper function for generating gpr_save pattern. */
5386
5387 rtx
5388 riscv_gen_gpr_save_insn (struct riscv_frame_info *frame)
5389 {
5390 unsigned count = riscv_save_libcall_count (frame->mask);
5391 /* 1 for unspec 2 for clobber t0/t1 and 1 for ra. */
5392 unsigned veclen = 1 + 2 + 1 + count;
5393 rtvec vec = rtvec_alloc (veclen);
5394
5395 gcc_assert (veclen <= ARRAY_SIZE (gpr_save_reg_order));
5396
5397 RTVEC_ELT (vec, 0) =
5398 gen_rtx_UNSPEC_VOLATILE (VOIDmode,
5399 gen_rtvec (1, GEN_INT (count)), UNSPECV_GPR_SAVE);
5400
5401 for (unsigned i = 1; i < veclen; ++i)
5402 {
5403 unsigned regno = gpr_save_reg_order[i];
5404 rtx reg = gen_rtx_REG (Pmode, regno);
5405 rtx elt;
5406
5407 /* t0 and t1 are CLOBBERs, others are USEs. */
5408 if (i < 3)
5409 elt = gen_rtx_CLOBBER (Pmode, reg);
5410 else
5411 elt = gen_rtx_USE (Pmode, reg);
5412
5413 RTVEC_ELT (vec, i) = elt;
5414 }
5415
5416 /* Largest number of caller-save register must set in mask if we are
5417 not using __riscv_save_0. */
5418 gcc_assert ((count == 0) ||
5419 BITSET_P (frame->mask, gpr_save_reg_order[veclen - 1]));
5420
5421 return gen_rtx_PARALLEL (VOIDmode, vec);
5422 }
5423
5424 /* Return true if it's valid gpr_save pattern. */
5425
5426 bool
5427 riscv_gpr_save_operation_p (rtx op)
5428 {
5429 unsigned len = XVECLEN (op, 0);
5430
5431 if (len > ARRAY_SIZE (gpr_save_reg_order))
5432 return false;
5433
5434 for (unsigned i = 0; i < len; i++)
5435 {
5436 rtx elt = XVECEXP (op, 0, i);
5437 if (i == 0)
5438 {
5439 /* First element in parallel is unspec. */
5440 if (GET_CODE (elt) != UNSPEC_VOLATILE
5441 || GET_CODE (XVECEXP (elt, 0, 0)) != CONST_INT
5442 || XINT (elt, 1) != UNSPECV_GPR_SAVE)
5443 return false;
5444 }
5445 else
5446 {
5447 /* Two CLOBBER and USEs, must check the order. */
5448 unsigned expect_code = i < 3 ? CLOBBER : USE;
5449 if (GET_CODE (elt) != expect_code
5450 || !REG_P (XEXP (elt, 1))
5451 || (REGNO (XEXP (elt, 1)) != gpr_save_reg_order[i]))
5452 return false;
5453 }
5454 break;
5455 }
5456 return true;
5457 }
5458
5459 /* Implement TARGET_ASAN_SHADOW_OFFSET. */
5460
5461 static unsigned HOST_WIDE_INT
5462 riscv_asan_shadow_offset (void)
5463 {
5464 /* We only have libsanitizer support for RV64 at present.
5465
5466 This number must match kRiscv*_ShadowOffset* in the file
5467 libsanitizer/asan/asan_mapping.h which is currently 1<<29 for rv64,
5468 even though 1<<36 makes more sense. */
5469 return TARGET_64BIT ? (HOST_WIDE_INT_1 << 29) : 0;
5470 }
5471
5472 /* Initialize the GCC target structure. */
5473 #undef TARGET_ASM_ALIGNED_HI_OP
5474 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
5475 #undef TARGET_ASM_ALIGNED_SI_OP
5476 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
5477 #undef TARGET_ASM_ALIGNED_DI_OP
5478 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
5479
5480 #undef TARGET_OPTION_OVERRIDE
5481 #define TARGET_OPTION_OVERRIDE riscv_option_override
5482
5483 #undef TARGET_LEGITIMIZE_ADDRESS
5484 #define TARGET_LEGITIMIZE_ADDRESS riscv_legitimize_address
5485
5486 #undef TARGET_SCHED_ISSUE_RATE
5487 #define TARGET_SCHED_ISSUE_RATE riscv_issue_rate
5488
5489 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
5490 #define TARGET_FUNCTION_OK_FOR_SIBCALL riscv_function_ok_for_sibcall
5491
5492 #undef TARGET_SET_CURRENT_FUNCTION
5493 #define TARGET_SET_CURRENT_FUNCTION riscv_set_current_function
5494
5495 #undef TARGET_REGISTER_MOVE_COST
5496 #define TARGET_REGISTER_MOVE_COST riscv_register_move_cost
5497 #undef TARGET_MEMORY_MOVE_COST
5498 #define TARGET_MEMORY_MOVE_COST riscv_memory_move_cost
5499 #undef TARGET_RTX_COSTS
5500 #define TARGET_RTX_COSTS riscv_rtx_costs
5501 #undef TARGET_ADDRESS_COST
5502 #define TARGET_ADDRESS_COST riscv_address_cost
5503
5504 #undef TARGET_ASM_FILE_START
5505 #define TARGET_ASM_FILE_START riscv_file_start
5506 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
5507 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
5508
5509 #undef TARGET_EXPAND_BUILTIN_VA_START
5510 #define TARGET_EXPAND_BUILTIN_VA_START riscv_va_start
5511
5512 #undef TARGET_PROMOTE_FUNCTION_MODE
5513 #define TARGET_PROMOTE_FUNCTION_MODE riscv_promote_function_mode
5514
5515 #undef TARGET_RETURN_IN_MEMORY
5516 #define TARGET_RETURN_IN_MEMORY riscv_return_in_memory
5517
5518 #undef TARGET_ASM_OUTPUT_MI_THUNK
5519 #define TARGET_ASM_OUTPUT_MI_THUNK riscv_output_mi_thunk
5520 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
5521 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
5522
5523 #undef TARGET_PRINT_OPERAND
5524 #define TARGET_PRINT_OPERAND riscv_print_operand
5525 #undef TARGET_PRINT_OPERAND_ADDRESS
5526 #define TARGET_PRINT_OPERAND_ADDRESS riscv_print_operand_address
5527
5528 #undef TARGET_SETUP_INCOMING_VARARGS
5529 #define TARGET_SETUP_INCOMING_VARARGS riscv_setup_incoming_varargs
5530 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
5531 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS riscv_allocate_stack_slots_for_args
5532 #undef TARGET_STRICT_ARGUMENT_NAMING
5533 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
5534 #undef TARGET_MUST_PASS_IN_STACK
5535 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
5536 #undef TARGET_PASS_BY_REFERENCE
5537 #define TARGET_PASS_BY_REFERENCE riscv_pass_by_reference
5538 #undef TARGET_ARG_PARTIAL_BYTES
5539 #define TARGET_ARG_PARTIAL_BYTES riscv_arg_partial_bytes
5540 #undef TARGET_FUNCTION_ARG
5541 #define TARGET_FUNCTION_ARG riscv_function_arg
5542 #undef TARGET_FUNCTION_ARG_ADVANCE
5543 #define TARGET_FUNCTION_ARG_ADVANCE riscv_function_arg_advance
5544 #undef TARGET_FUNCTION_ARG_BOUNDARY
5545 #define TARGET_FUNCTION_ARG_BOUNDARY riscv_function_arg_boundary
5546
5547 /* The generic ELF target does not always have TLS support. */
5548 #ifdef HAVE_AS_TLS
5549 #undef TARGET_HAVE_TLS
5550 #define TARGET_HAVE_TLS true
5551 #endif
5552
5553 #undef TARGET_CANNOT_FORCE_CONST_MEM
5554 #define TARGET_CANNOT_FORCE_CONST_MEM riscv_cannot_force_const_mem
5555
5556 #undef TARGET_LEGITIMATE_CONSTANT_P
5557 #define TARGET_LEGITIMATE_CONSTANT_P riscv_legitimate_constant_p
5558
5559 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
5560 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
5561
5562 #undef TARGET_LEGITIMATE_ADDRESS_P
5563 #define TARGET_LEGITIMATE_ADDRESS_P riscv_legitimate_address_p
5564
5565 #undef TARGET_CAN_ELIMINATE
5566 #define TARGET_CAN_ELIMINATE riscv_can_eliminate
5567
5568 #undef TARGET_CONDITIONAL_REGISTER_USAGE
5569 #define TARGET_CONDITIONAL_REGISTER_USAGE riscv_conditional_register_usage
5570
5571 #undef TARGET_CLASS_MAX_NREGS
5572 #define TARGET_CLASS_MAX_NREGS riscv_class_max_nregs
5573
5574 #undef TARGET_TRAMPOLINE_INIT
5575 #define TARGET_TRAMPOLINE_INIT riscv_trampoline_init
5576
5577 #undef TARGET_IN_SMALL_DATA_P
5578 #define TARGET_IN_SMALL_DATA_P riscv_in_small_data_p
5579
5580 #undef TARGET_HAVE_SRODATA_SECTION
5581 #define TARGET_HAVE_SRODATA_SECTION true
5582
5583 #undef TARGET_ASM_SELECT_SECTION
5584 #define TARGET_ASM_SELECT_SECTION riscv_select_section
5585
5586 #undef TARGET_ASM_UNIQUE_SECTION
5587 #define TARGET_ASM_UNIQUE_SECTION riscv_unique_section
5588
5589 #undef TARGET_ASM_SELECT_RTX_SECTION
5590 #define TARGET_ASM_SELECT_RTX_SECTION riscv_elf_select_rtx_section
5591
5592 #undef TARGET_MIN_ANCHOR_OFFSET
5593 #define TARGET_MIN_ANCHOR_OFFSET (-IMM_REACH/2)
5594
5595 #undef TARGET_MAX_ANCHOR_OFFSET
5596 #define TARGET_MAX_ANCHOR_OFFSET (IMM_REACH/2-1)
5597
5598 #undef TARGET_REGISTER_PRIORITY
5599 #define TARGET_REGISTER_PRIORITY riscv_register_priority
5600
5601 #undef TARGET_CANNOT_COPY_INSN_P
5602 #define TARGET_CANNOT_COPY_INSN_P riscv_cannot_copy_insn_p
5603
5604 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
5605 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV riscv_atomic_assign_expand_fenv
5606
5607 #undef TARGET_INIT_BUILTINS
5608 #define TARGET_INIT_BUILTINS riscv_init_builtins
5609
5610 #undef TARGET_BUILTIN_DECL
5611 #define TARGET_BUILTIN_DECL riscv_builtin_decl
5612
5613 #undef TARGET_EXPAND_BUILTIN
5614 #define TARGET_EXPAND_BUILTIN riscv_expand_builtin
5615
5616 #undef TARGET_HARD_REGNO_NREGS
5617 #define TARGET_HARD_REGNO_NREGS riscv_hard_regno_nregs
5618 #undef TARGET_HARD_REGNO_MODE_OK
5619 #define TARGET_HARD_REGNO_MODE_OK riscv_hard_regno_mode_ok
5620
5621 #undef TARGET_MODES_TIEABLE_P
5622 #define TARGET_MODES_TIEABLE_P riscv_modes_tieable_p
5623
5624 #undef TARGET_SLOW_UNALIGNED_ACCESS
5625 #define TARGET_SLOW_UNALIGNED_ACCESS riscv_slow_unaligned_access
5626
5627 #undef TARGET_SECONDARY_MEMORY_NEEDED
5628 #define TARGET_SECONDARY_MEMORY_NEEDED riscv_secondary_memory_needed
5629
5630 #undef TARGET_CAN_CHANGE_MODE_CLASS
5631 #define TARGET_CAN_CHANGE_MODE_CLASS riscv_can_change_mode_class
5632
5633 #undef TARGET_CONSTANT_ALIGNMENT
5634 #define TARGET_CONSTANT_ALIGNMENT riscv_constant_alignment
5635
5636 #undef TARGET_MERGE_DECL_ATTRIBUTES
5637 #define TARGET_MERGE_DECL_ATTRIBUTES riscv_merge_decl_attributes
5638
5639 #undef TARGET_ATTRIBUTE_TABLE
5640 #define TARGET_ATTRIBUTE_TABLE riscv_attribute_table
5641
5642 #undef TARGET_WARN_FUNC_RETURN
5643 #define TARGET_WARN_FUNC_RETURN riscv_warn_func_return
5644
5645 /* The low bit is ignored by jump instructions so is safe to use. */
5646 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
5647 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
5648
5649 #undef TARGET_MACHINE_DEPENDENT_REORG
5650 #define TARGET_MACHINE_DEPENDENT_REORG riscv_reorg
5651
5652 #undef TARGET_NEW_ADDRESS_PROFITABLE_P
5653 #define TARGET_NEW_ADDRESS_PROFITABLE_P riscv_new_address_profitable_p
5654
5655 #undef TARGET_ASAN_SHADOW_OFFSET
5656 #define TARGET_ASAN_SHADOW_OFFSET riscv_asan_shadow_offset
5657
5658 #ifdef TARGET_BIG_ENDIAN_DEFAULT
5659 #undef TARGET_DEFAULT_TARGET_FLAGS
5660 #define TARGET_DEFAULT_TARGET_FLAGS (MASK_BIG_ENDIAN)
5661 #endif
5662
5663 struct gcc_target targetm = TARGET_INITIALIZER;
5664
5665 #include "gt-riscv.h"