OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [tools/] [riscv-gnu-toolchain-master/] [gcc/] [gcc/] [config/] [riscv/] [riscv.c] - Blame information for rev 13

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.