Debian Codegraph @

gcc/config/loongarch/loongarch.cc

raw
1 /* Subroutines used for LoongArch code generation.
2 Copyright (C) 2021-2025 Free Software Foundation, Inc.
3 Contributed by Loongson Ltd.
4 Based on MIPS and RISC-V target for GNU compiler.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #define IN_TARGET_CODE 1
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "memmodel.h"
32 #include "gimple.h"
33 #include "cfghooks.h"
34 #include "df.h"
35 #include "tm_p.h"
36 #include "stringpool.h"
37 #include "attribs.h"
38 #include "optabs.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "recog.h"
42 #include "cgraph.h"
43 #include "diagnostic.h"
44 #include "insn-attr.h"
45 #include "output.h"
46 #include "alias.h"
47 #include "fold-const.h"
48 #include "varasm.h"
49 #include "stor-layout.h"
50 #include "calls.h"
51 #include "explow.h"
52 #include "expr.h"
53 #include "libfuncs.h"
54 #include "reload.h"
55 #include "common/common-target.h"
56 #include "langhooks.h"
57 #include "cfgrtl.h"
58 #include "cfganal.h"
59 #include "sched-int.h"
60 #include "gimplify.h"
61 #include "target-globals.h"
62 #include "tree-pass.h"
63 #include "context.h"
64 #include "builtins.h"
65 #include "rtl-iter.h"
66 #include "opts.h"
67 #include "function-abi.h"
68 #include "cfgloop.h"
69 #include "tree-vectorizer.h"
70
71 /* This file should be included last. */
72 #include "target-def.h"
73
74 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
75 #define UNSPEC_ADDRESS_P(X) \
76 (GET_CODE (X) == UNSPEC \
77 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
78 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
79
80 /* Extract the symbol or label from UNSPEC wrapper X. */
81 #define UNSPEC_ADDRESS(X) XVECEXP (X, 0, 0)
82
83 /* Extract the symbol type from UNSPEC wrapper X. */
84 #define UNSPEC_ADDRESS_TYPE(X) \
85 ((enum loongarch_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
86
87 /* True if INSN is a loongarch.md pattern or asm statement. */
88 /* ??? This test exists through the compiler, perhaps it should be
89 moved to rtl.h. */
90 #define USEFUL_INSN_P(INSN) \
91 (NONDEBUG_INSN_P (INSN) \
92 && GET_CODE (PATTERN (INSN)) != USE \
93 && GET_CODE (PATTERN (INSN)) != CLOBBER)
94
95 /* True if bit BIT is set in VALUE. */
96 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
97
98 /* Classifies an address.
99
100 ADDRESS_REG
101 A natural register + offset address. The register satisfies
102 loongarch_valid_base_register_p and the offset is a const_arith_operand.
103
104 ADDRESS_REG_REG
105 A base register indexed by (optionally scaled) register.
106
107 ADDRESS_LO_SUM
108 A LO_SUM rtx. The first operand is a valid base register and the second
109 operand is a symbolic address.
110
111 ADDRESS_CONST_INT
112 A signed 16-bit constant address.
113
114 ADDRESS_SYMBOLIC:
115 A constant symbolic address. */
116 enum loongarch_address_type
117 {
118 ADDRESS_REG,
119 ADDRESS_REG_REG,
120 ADDRESS_LO_SUM,
121 ADDRESS_CONST_INT,
122 ADDRESS_SYMBOLIC
123 };
124
125
126 /* Information about an address described by loongarch_address_type. */
127 struct loongarch_address_info
128 {
129 enum loongarch_address_type type;
130 rtx reg;
131 rtx offset;
132 enum loongarch_symbol_type symbol_type;
133 };
134
135 /* Method of loading instant numbers:
136
137 METHOD_NORMAL:
138 Load 0-31 bit of the immediate number.
139
140 METHOD_LU32I:
141 Load 32-51 bit of the immediate number.
142
143 METHOD_LU52I:
144 Load 52-63 bit of the immediate number.
145
146 METHOD_MIRROR:
147 Copy 0-31 bit of the immediate number to 32-63bit.
148 */
149 enum loongarch_load_imm_method
150 {
151 METHOD_NORMAL,
152 METHOD_LU32I,
153 METHOD_LU52I,
154 METHOD_MIRROR
155 };
156
157 struct loongarch_integer_op
158 {
159 enum rtx_code code;
160 HOST_WIDE_INT value;
161 /* Represent the result of the immediate count of the load instruction at
162 each step. */
163 HOST_WIDE_INT curr_value;
164 enum loongarch_load_imm_method method;
165 };
166
167 /* The largest number of operations needed to load an integer constant.
168 The worst accepted case for 64-bit constants is LU12I.W,LU32I.D,LU52I.D,ORI
169 or LU12I.W,LU32I.D,LU52I.D,ADDI.D DECL_ASSEMBLER_NAME. */
170 #define LARCH_MAX_INTEGER_OPS 4
171
172 /* Arrays that map GCC register numbers to debugger register numbers. */
173 int loongarch_dwarf_regno[FIRST_PSEUDO_REGISTER];
174
175 /* Index [M][R] is true if register R is allowed to hold a value of mode M. */
176 static bool loongarch_hard_regno_mode_ok_p[MAX_MACHINE_MODE]
177 [FIRST_PSEUDO_REGISTER];
178
179 /* Index C is true if character C is a valid PRINT_OPERAND punctation
180 character. */
181 static bool loongarch_print_operand_punct[256];
182
183 /* Cached value of can_issue_more. This is cached in loongarch_variable_issue
184 hook and returned from loongarch_sched_reorder2. */
185 static int cached_can_issue_more;
186
187 /* Index R is the smallest register class that contains register R. */
188 const enum reg_class loongarch_regno_to_class[FIRST_PSEUDO_REGISTER] = {
189 GR_REGS, GR_REGS, GR_REGS, GR_REGS,
190 JIRL_REGS, JIRL_REGS, JIRL_REGS, JIRL_REGS,
191 JIRL_REGS, JIRL_REGS, JIRL_REGS, JIRL_REGS,
192 SIBCALL_REGS, JIRL_REGS, SIBCALL_REGS, SIBCALL_REGS,
193 SIBCALL_REGS, SIBCALL_REGS, SIBCALL_REGS, SIBCALL_REGS,
194 SIBCALL_REGS, GR_REGS, GR_REGS, JIRL_REGS,
195 JIRL_REGS, JIRL_REGS, JIRL_REGS, JIRL_REGS,
196 JIRL_REGS, JIRL_REGS, JIRL_REGS, JIRL_REGS,
197
198 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
199 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
200 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
201 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
202 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
203 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
204 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
205 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
206 FCC_REGS, FCC_REGS, FCC_REGS, FCC_REGS,
207 FCC_REGS, FCC_REGS, FCC_REGS, FCC_REGS,
208 FRAME_REGS, FRAME_REGS
209 };
210
211 /* Information about a single argument. */
212 struct loongarch_arg_info
213 {
214 /* True if the argument is at least partially passed on the stack. */
215 bool stack_p;
216
217 /* The number of integer registers allocated to this argument. */
218 unsigned int num_gprs;
219
220 /* The offset of the first register used, provided num_gprs is nonzero.
221 If passed entirely on the stack, the value is MAX_ARGS_IN_REGISTERS. */
222 unsigned int gpr_offset;
223
224 /* The number of floating-point registers allocated to this argument. */
225 unsigned int num_fprs;
226
227 /* The offset of the first register used, provided num_fprs is nonzero. */
228 unsigned int fpr_offset;
229 };
230
231 /* Invoke MACRO (COND) for each fcmp.cond.{s/d} condition. */
232 #define LARCH_FP_CONDITIONS(MACRO) \
233 MACRO (f), \
234 MACRO (un), \
235 MACRO (eq), \
236 MACRO (ueq), \
237 MACRO (olt), \
238 MACRO (ult), \
239 MACRO (ole), \
240 MACRO (ule), \
241 MACRO (sf), \
242 MACRO (ngle), \
243 MACRO (seq), \
244 MACRO (ngl), \
245 MACRO (lt), \
246 MACRO (nge), \
247 MACRO (le), \
248 MACRO (ngt)
249
250 /* Enumerates the codes above as LARCH_FP_COND_<X>. */
251 #define DECLARE_LARCH_COND(X) LARCH_FP_COND_##X
252 enum loongarch_fp_condition
253 {
254 LARCH_FP_CONDITIONS (DECLARE_LARCH_COND)
255 };
256 #undef DECLARE_LARCH_COND
257
258 /* Index X provides the string representation of LARCH_FP_COND_<X>. */
259 #define STRINGIFY(X) #X
260 const char *const
261 loongarch_fp_conditions[16]= {LARCH_FP_CONDITIONS (STRINGIFY)};
262 #undef STRINGIFY
263
264 /* Size of guard page. */
265 #define STACK_CLASH_PROTECTION_GUARD_SIZE \
266 (1 << param_stack_clash_protection_guard_size)
267
268 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Every parameter gets at
269 least PARM_BOUNDARY bits of alignment, but will be given anything up
270 to PREFERRED_STACK_BOUNDARY bits if the type requires it. */
271
272 static unsigned int
273 loongarch_function_arg_boundary (machine_mode mode, const_tree type)
274 {
275 unsigned int alignment;
276
277 /* Use natural alignment if the type is not aggregate data. */
278 if (type && !AGGREGATE_TYPE_P (type))
279 alignment = TYPE_ALIGN (TYPE_MAIN_VARIANT (type));
280 else
281 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
282
283 return MIN (PREFERRED_STACK_BOUNDARY, MAX (PARM_BOUNDARY, alignment));
284 }
285
286 /* If MODE represents an argument that can be passed or returned in
287 floating-point registers, return the number of registers, else 0. */
288
289 static unsigned
290 loongarch_pass_mode_in_fpr_p (machine_mode mode)
291 {
292 if (GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FP_ARG)
293 {
294 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
295 return 1;
296
297 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
298 return 2;
299 }
300
301 return 0;
302 }
303
304 typedef struct
305 {
306 const_tree type;
307 HOST_WIDE_INT offset;
308 } loongarch_aggregate_field;
309
310 /* Identify subfields of aggregates that are candidates for passing in
311 floating-point registers. */
312
313 static int
314 loongarch_flatten_aggregate_field (const_tree type,
315 loongarch_aggregate_field fields[2], int n,
316 HOST_WIDE_INT offset)
317 {
318 switch (TREE_CODE (type))
319 {
320 case RECORD_TYPE:
321 /* Can't handle incomplete types nor sizes that are not fixed. */
322 if (!COMPLETE_TYPE_P (type)
323 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
324 || !tree_fits_uhwi_p (TYPE_SIZE (type)))
325 return -1;
326
327 for (tree f = TYPE_FIELDS (type); f; f = DECL_CHAIN (f))
328 if (TREE_CODE (f) == FIELD_DECL)
329 {
330 if (!TYPE_P (TREE_TYPE (f)))
331 return -1;
332
333 if (DECL_SIZE (f) && integer_zerop (DECL_SIZE (f)))
334 continue;
335
336 HOST_WIDE_INT pos = offset + int_byte_position (f);
337 n = loongarch_flatten_aggregate_field (TREE_TYPE (f), fields, n,
338 pos);
339 if (n < 0)
340 return -1;
341 }
342 return n;
343
344 case ARRAY_TYPE:
345 {
346 HOST_WIDE_INT n_elts;
347 loongarch_aggregate_field subfields[2];
348 tree index = TYPE_DOMAIN (type);
349 tree elt_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
350 int n_subfields = loongarch_flatten_aggregate_field (TREE_TYPE (type),
351 subfields, 0,
352 offset);
353
354 /* Can't handle incomplete types nor sizes that are not fixed. */
355 if (n_subfields <= 0
356 || !COMPLETE_TYPE_P (type)
357 || TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
358 || !index
359 || !TYPE_MAX_VALUE (index)
360 || !tree_fits_uhwi_p (TYPE_MAX_VALUE (index))
361 || !TYPE_MIN_VALUE (index)
362 || !tree_fits_uhwi_p (TYPE_MIN_VALUE (index))
363 || !tree_fits_uhwi_p (elt_size))
364 return -1;
365
366 n_elts = 1 + tree_to_uhwi (TYPE_MAX_VALUE (index))
367 - tree_to_uhwi (TYPE_MIN_VALUE (index));
368 gcc_assert (n_elts >= 0);
369
370 for (HOST_WIDE_INT i = 0; i < n_elts; i++)
371 for (int j = 0; j < n_subfields; j++)
372 {
373 if (n >= 2)
374 return -1;
375
376 fields[n] = subfields[j];
377 fields[n++].offset += i * tree_to_uhwi (elt_size);
378 }
379
380 return n;
381 }
382
383 case COMPLEX_TYPE:
384 {
385 /* Complex type need consume 2 field, so n must be 0. */
386 if (n != 0)
387 return -1;
388
389 HOST_WIDE_INT elt_size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (type)));
390
391 if (elt_size <= UNITS_PER_FP_ARG)
392 {
393 fields[0].type = TREE_TYPE (type);
394 fields[0].offset = offset;
395 fields[1].type = TREE_TYPE (type);
396 fields[1].offset = offset + elt_size;
397
398 return 2;
399 }
400
401 return -1;
402 }
403
404 default:
405 if (n < 2
406 && ((SCALAR_FLOAT_TYPE_P (type)
407 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FP_ARG)
408 || (INTEGRAL_TYPE_P (type)
409 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_WORD)))
410 {
411 fields[n].type = type;
412 fields[n].offset = offset;
413 return n + 1;
414 }
415 else
416 return -1;
417 }
418 }
419
420 /* Identify candidate aggregates for passing in floating-point registers.
421 Candidates have at most two fields after flattening. */
422
423 static int
424 loongarch_flatten_aggregate_argument (const_tree type,
425 loongarch_aggregate_field fields[2])
426 {
427 if (!type || TREE_CODE (type) != RECORD_TYPE)
428 return -1;
429
430 return loongarch_flatten_aggregate_field (type, fields, 0, 0);
431 }
432
433 /* See whether TYPE is a record whose fields should be returned in one or
434 two floating-point registers. If so, populate FIELDS accordingly. */
435
436 static unsigned
437 loongarch_pass_aggregate_num_fpr (const_tree type,
438 loongarch_aggregate_field fields[2])
439 {
440 int n = loongarch_flatten_aggregate_argument (type, fields);
441
442 for (int i = 0; i < n; i++)
443 if (!SCALAR_FLOAT_TYPE_P (fields[i].type))
444 return 0;
445
446 return n > 0 ? n : 0;
447 }
448
449 /* See whether TYPE is a record whose fields should be returned in one
450 floating-point register and one integer register. If so, populate
451 FIELDS accordingly. */
452
453 static bool
454 loongarch_pass_aggregate_in_fpr_and_gpr_p (const_tree type,
455 loongarch_aggregate_field fields[2])
456 {
457 unsigned num_int = 0, num_float = 0;
458 int n = loongarch_flatten_aggregate_argument (type, fields);
459
460 for (int i = 0; i < n; i++)
461 {
462 num_float += SCALAR_FLOAT_TYPE_P (fields[i].type);
463 num_int += INTEGRAL_TYPE_P (fields[i].type);
464 }
465
466 return num_int == 1 && num_float == 1;
467 }
468
469 /* Return the representation of an argument passed or returned in an FPR
470 when the value has mode VALUE_MODE and the type has TYPE_MODE. The
471 two modes may be different for structures like:
472
473 struct __attribute__((packed)) foo { float f; }
474
475 where the SFmode value "f" is passed in REGNO but the struct itself
476 has mode BLKmode. */
477
478 static rtx
479 loongarch_pass_fpr_single (machine_mode type_mode, unsigned regno,
480 machine_mode value_mode,
481 HOST_WIDE_INT offset)
482 {
483 rtx x = gen_rtx_REG (value_mode, regno);
484
485 if (type_mode != value_mode)
486 {
487 x = gen_rtx_EXPR_LIST (VOIDmode, x, GEN_INT (offset));
488 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
489 }
490 return x;
491 }
492
493 /* Pass or return a composite value in the FPR pair REGNO and REGNO + 1.
494 MODE is the mode of the composite. MODE1 and OFFSET1 are the mode and
495 byte offset for the first value, likewise MODE2 and OFFSET2 for the
496 second value. */
497
498 static rtx
499 loongarch_pass_fpr_pair (machine_mode mode, unsigned regno1,
500 machine_mode mode1, HOST_WIDE_INT offset1,
501 unsigned regno2, machine_mode mode2,
502 HOST_WIDE_INT offset2)
503 {
504 return gen_rtx_PARALLEL (
505 mode, gen_rtvec (2,
506 gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_REG (mode1, regno1),
507 GEN_INT (offset1)),
508 gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_REG (mode2, regno2),
509 GEN_INT (offset2))));
510 }
511
512 /* Fill INFO with information about a single argument, and return an
513 RTL pattern to pass or return the argument. CUM is the cumulative
514 state for earlier arguments. MODE is the mode of this argument and
515 TYPE is its type (if known). NAMED is true if this is a named
516 (fixed) argument rather than a variable one. RETURN_P is true if
517 returning the argument, or false if passing the argument. */
518
519 static rtx
520 loongarch_get_arg_info (struct loongarch_arg_info *info,
521 const CUMULATIVE_ARGS *cum, machine_mode mode,
522 const_tree type, bool named, bool return_p)
523 {
524 unsigned num_bytes, num_words;
525 unsigned fpr_base = return_p ? FP_RETURN : FP_ARG_FIRST;
526 unsigned gpr_base = return_p ? GP_RETURN : GP_ARG_FIRST;
527 unsigned alignment = loongarch_function_arg_boundary (mode, type);
528
529 memset (info, 0, sizeof (*info));
530 info->gpr_offset = cum->num_gprs;
531 info->fpr_offset = cum->num_fprs;
532
533 if (named)
534 {
535 loongarch_aggregate_field fields[2];
536 unsigned fregno = fpr_base + info->fpr_offset;
537 unsigned gregno = gpr_base + info->gpr_offset;
538
539 /* Pass one- or two-element floating-point aggregates in FPRs. */
540 if ((info->num_fprs
541 = loongarch_pass_aggregate_num_fpr (type, fields))
542 && info->fpr_offset + info->num_fprs <= MAX_ARGS_IN_REGISTERS)
543 switch (info->num_fprs)
544 {
545 case 1:
546 return loongarch_pass_fpr_single (mode, fregno,
547 TYPE_MODE (fields[0].type),
548 fields[0].offset);
549
550 case 2:
551 return loongarch_pass_fpr_pair (mode, fregno,
552 TYPE_MODE (fields[0].type),
553 fields[0].offset,
554 fregno + 1,
555 TYPE_MODE (fields[1].type),
556 fields[1].offset);
557
558 default:
559 gcc_unreachable ();
560 }
561
562 /* Pass real and complex floating-point numbers in FPRs. */
563 if ((info->num_fprs = loongarch_pass_mode_in_fpr_p (mode))
564 && info->fpr_offset + info->num_fprs <= MAX_ARGS_IN_REGISTERS)
565 switch (GET_MODE_CLASS (mode))
566 {
567 case MODE_FLOAT:
568 return gen_rtx_REG (mode, fregno);
569
570 case MODE_COMPLEX_FLOAT:
571 return loongarch_pass_fpr_pair (mode, fregno,
572 GET_MODE_INNER (mode), 0,
573 fregno + 1, GET_MODE_INNER (mode),
574 GET_MODE_UNIT_SIZE (mode));
575
576 default:
577 gcc_unreachable ();
578 }
579
580 /* Pass structs with one float and one integer in an FPR and a GPR. */
581 if (loongarch_pass_aggregate_in_fpr_and_gpr_p (type, fields)
582 && info->gpr_offset < MAX_ARGS_IN_REGISTERS
583 && info->fpr_offset < MAX_ARGS_IN_REGISTERS)
584 {
585 info->num_gprs = 1;
586 info->num_fprs = 1;
587
588 if (!SCALAR_FLOAT_TYPE_P (fields[0].type))
589 std::swap (fregno, gregno);
590
591 return loongarch_pass_fpr_pair (mode, fregno,
592 TYPE_MODE (fields[0].type),
593 fields[0].offset, gregno,
594 TYPE_MODE (fields[1].type),
595 fields[1].offset);
596 }
597 }
598
599 /* Work out the size of the argument. */
600 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
601 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
602
603 /* Doubleword-aligned varargs start on an even register boundary. */
604 if (!named && num_bytes != 0 && alignment > BITS_PER_WORD)
605 info->gpr_offset += info->gpr_offset & 1;
606
607 /* Partition the argument between registers and stack. */
608 info->num_fprs = 0;
609 info->num_gprs = MIN (num_words, MAX_ARGS_IN_REGISTERS - info->gpr_offset);
610 info->stack_p = (num_words - info->num_gprs) != 0;
611
612 if (info->num_gprs || return_p)
613 return gen_rtx_REG (mode, gpr_base + info->gpr_offset);
614
615 return NULL_RTX;
616 }
617
618 /* Implement TARGET_FUNCTION_ARG. */
619
620 static rtx
621 loongarch_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
622 {
623 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
624 struct loongarch_arg_info info;
625
626 if (arg.end_marker_p ())
627 return NULL;
628
629 return loongarch_get_arg_info (&info, cum, arg.mode, arg.type, arg.named,
630 false);
631 }
632
633 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
634
635 static void
636 loongarch_function_arg_advance (cumulative_args_t cum_v,
637 const function_arg_info &arg)
638 {
639 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
640 struct loongarch_arg_info info;
641
642 loongarch_get_arg_info (&info, cum, arg.mode, arg.type, arg.named, false);
643
644 /* Advance the register count. This has the effect of setting
645 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
646 argument required us to skip the final GPR and pass the whole
647 argument on the stack. */
648 cum->num_fprs = info.fpr_offset + info.num_fprs;
649 cum->num_gprs = info.gpr_offset + info.num_gprs;
650 }
651
652 /* Implement TARGET_ARG_PARTIAL_BYTES. */
653
654 static int
655 loongarch_arg_partial_bytes (cumulative_args_t cum,
656 const function_arg_info &generic_arg)
657 {
658 struct loongarch_arg_info arg;
659
660 loongarch_get_arg_info (&arg, get_cumulative_args (cum), generic_arg.mode,
661 generic_arg.type, generic_arg.named, false);
662 return arg.stack_p ? arg.num_gprs * UNITS_PER_WORD : 0;
663 }
664
665 /* Implement FUNCTION_VALUE and LIBCALL_VALUE. For normal calls,
666 VALTYPE is the return type and MODE is VOIDmode. For libcalls,
667 VALTYPE is null and MODE is the mode of the return value. */
668
669 static rtx
670 loongarch_function_value_1 (const_tree type, const_tree func,
671 machine_mode mode)
672 {
673 struct loongarch_arg_info info;
674 CUMULATIVE_ARGS args;
675
676 if (type)
677 {
678 int unsigned_p = TYPE_UNSIGNED (type);
679
680 mode = TYPE_MODE (type);
681
682 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
683 return values, promote the mode here too. */
684 mode = promote_function_mode (type, mode, &unsigned_p, func, 1);
685 }
686
687 memset (&args, 0, sizeof (args));
688 return loongarch_get_arg_info (&info, &args, mode, type, true, true);
689 }
690
691
692 /* Implement TARGET_FUNCTION_VALUE. */
693
694 static rtx
695 loongarch_function_value (const_tree valtype, const_tree fn_decl_or_type,
696 bool outgoing ATTRIBUTE_UNUSED)
697 {
698 return loongarch_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
699 }
700
701 /* Implement TARGET_LIBCALL_VALUE. */
702
703 static rtx
704 loongarch_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
705 {
706 return loongarch_function_value_1 (NULL_TREE, NULL_TREE, mode);
707 }
708
709
710 /* Implement TARGET_PASS_BY_REFERENCE. */
711
712 static bool
713 loongarch_pass_by_reference (cumulative_args_t cum_v,
714 const function_arg_info &arg)
715 {
716 HOST_WIDE_INT size = arg.type_size_in_bytes ();
717 struct loongarch_arg_info info;
718 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
719
720 /* ??? std_gimplify_va_arg_expr passes NULL for cum. Fortunately, we
721 never pass variadic arguments in floating-point registers, so we can
722 avoid the call to loongarch_get_arg_info in this case. */
723 if (cum != NULL)
724 {
725 /* Don't pass by reference if we can use a floating-point register. */
726 loongarch_get_arg_info (&info, cum, arg.mode, arg.type, arg.named,
727 false);
728 if (info.num_fprs)
729 return false;
730 }
731
732 /* Pass by reference if the data do not fit in two integer registers. */
733 return !IN_RANGE (size, 0, 2 * UNITS_PER_WORD);
734 }
735
736 /* Implement TARGET_RETURN_IN_MEMORY. */
737
738 static bool
739 loongarch_return_in_memory (const_tree type,
740 const_tree fndecl ATTRIBUTE_UNUSED)
741 {
742 CUMULATIVE_ARGS args;
743 cumulative_args_t cum = pack_cumulative_args (&args);
744
745 /* The rules for returning in memory are the same as for passing the
746 first named argument by reference. */
747 memset (&args, 0, sizeof (args));
748 function_arg_info arg (const_cast<tree> (type), /*named=*/true);
749 return loongarch_pass_by_reference (cum, arg);
750 }
751
752 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
753
754 static void
755 loongarch_setup_incoming_varargs (cumulative_args_t cum,
756 const function_arg_info &arg,
757 int *pretend_size ATTRIBUTE_UNUSED,
758 int no_rtl)
759 {
760 CUMULATIVE_ARGS local_cum;
761 int gp_saved;
762
763 /* The caller has advanced CUM up to, but not beyond, the last named
764 argument. Advance a local copy of CUM past the last "real" named
765 argument, to find out how many registers are left over. */
766 local_cum = *get_cumulative_args (cum);
767
768 /* For a C23 variadic function w/o any named argument, and w/o an
769 artifical argument for large return value, skip advancing args.
770 There is such an artifical argument iff. arg.type is non-NULL
771 (PR 114175). */
772 if (!TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (current_function_decl))
773 || arg.type != NULL_TREE)
774 loongarch_function_arg_advance (pack_cumulative_args (&local_cum), arg);
775
776 /* Found out how many registers we need to save. */
777 gp_saved = cfun->va_list_gpr_size / UNITS_PER_WORD;
778 if (gp_saved > (int) (MAX_ARGS_IN_REGISTERS - local_cum.num_gprs))
779 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
780
781 if (!no_rtl && gp_saved > 0)
782 {
783 rtx ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
784 REG_PARM_STACK_SPACE (cfun->decl)
785 - gp_saved * UNITS_PER_WORD);
786 rtx mem = gen_frame_mem (BLKmode, ptr);
787 set_mem_alias_set (mem, get_varargs_alias_set ());
788
789 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST, mem, gp_saved);
790 }
791 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
792 cfun->machine->varargs_size = gp_saved * UNITS_PER_WORD;
793 }
794
795 /* Make the last instruction frame-related and note that it performs
796 the operation described by FRAME_PATTERN. */
797
798 static void
799 loongarch_set_frame_expr (rtx frame_pattern)
800 {
801 rtx insn;
802
803 insn = get_last_insn ();
804 RTX_FRAME_RELATED_P (insn) = 1;
805 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR, frame_pattern,
806 REG_NOTES (insn));
807 }
808
809 /* Return a frame-related rtx that stores REG at MEM.
810 REG must be a single register. */
811
812 static rtx
813 loongarch_frame_set (rtx mem, rtx reg)
814 {
815 rtx set = gen_rtx_SET (mem, reg);
816 RTX_FRAME_RELATED_P (set) = 1;
817 return set;
818 }
819
820 /* Return true if the current function must save register REGNO. */
821
822 static bool
823 loongarch_save_reg_p (unsigned int regno)
824 {
825 bool call_saved = !global_regs[regno] && !call_used_regs[regno];
826 bool might_clobber
827 = crtl->saves_all_registers || df_regs_ever_live_p (regno);
828
829 if (call_saved && might_clobber)
830 return true;
831
832 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
833 return true;
834
835 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
836 return true;
837
838 return false;
839 }
840
841 /* Determine which GPR save/restore routine to call. */
842
843 static unsigned
844 loongarch_save_libcall_count (unsigned mask)
845 {
846 for (unsigned n = GP_REG_LAST; n > GP_REG_FIRST; n--)
847 if (BITSET_P (mask, n))
848 return CALLEE_SAVED_REG_NUMBER (n) + 1;
849 abort ();
850 }
851
852 /* Populate the current function's loongarch_frame_info structure.
853
854 LoongArch stack frames grown downward. High addresses are at the top.
855
856 +-------------------------------+
857 | |
858 | incoming stack arguments |
859 | |
860 +-------------------------------+ <-- incoming stack pointer
861 | |
862 | callee-allocated save area |
863 | for arguments that are |
864 | split between registers and |
865 | the stack |
866 | |
867 +-------------------------------+ <-- arg_pointer_rtx (virtual)
868 | |
869 | callee-allocated save area |
870 | for register varargs |
871 | |
872 +-------------------------------+ <-- hard_frame_pointer_rtx;
873 | | stack_pointer_rtx + gp_sp_offset
874 | GPR save area | + UNITS_PER_WORD
875 | |
876 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
877 | | + UNITS_PER_HWVALUE
878 | FPR save area |
879 | |
880 +-------------------------------+ <-- frame_pointer_rtx (virtual)
881 | |
882 | local variables |
883 | |
884 P +-------------------------------+
885 | |
886 | outgoing stack arguments |
887 | |
888 +-------------------------------+ <-- stack_pointer_rtx
889
890 Dynamic stack allocations such as alloca insert data at point P.
891 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
892 hard_frame_pointer_rtx unchanged. */
893
894 static void
895 loongarch_compute_frame_info (void)
896 {
897 struct loongarch_frame_info *frame;
898 HOST_WIDE_INT offset;
899 unsigned int regno, i, num_x_saved = 0, num_f_saved = 0;
900
901 frame = &cfun->machine->frame;
902 memset (frame, 0, sizeof (*frame));
903
904 /* Find out which GPRs we need to save. */
905 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
906 if (loongarch_save_reg_p (regno))
907 frame->mask |= 1 << (regno - GP_REG_FIRST), num_x_saved++;
908
909 /* If this function calls eh_return, we must also save and restore the
910 EH data registers. */
911 if (crtl->calls_eh_return)
912 for (i = 0; (regno = EH_RETURN_DATA_REGNO (i)) != INVALID_REGNUM; i++)
913 frame->mask |= 1 << (regno - GP_REG_FIRST), num_x_saved++;
914
915 /* Find out which FPRs we need to save. This loop must iterate over
916 the same space as its companion in loongarch_for_each_saved_reg. */
917 if (TARGET_HARD_FLOAT)
918 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
919 if (loongarch_save_reg_p (regno))
920 frame->fmask |= 1 << (regno - FP_REG_FIRST), num_f_saved++;
921
922 /* At the bottom of the frame are any outgoing stack arguments. */
923 offset = LARCH_STACK_ALIGN (crtl->outgoing_args_size);
924 /* Next are local stack variables. */
925 offset += LARCH_STACK_ALIGN (get_frame_size ());
926 /* The virtual frame pointer points above the local variables. */
927 frame->frame_pointer_offset = offset;
928 /* Next are the callee-saved FPRs. */
929 if (frame->fmask)
930 {
931 offset += LARCH_STACK_ALIGN (num_f_saved * UNITS_PER_FP_REG);
932 frame->fp_sp_offset = offset - UNITS_PER_FP_REG;
933 }
934 else
935 frame->fp_sp_offset = offset;
936 /* Next are the callee-saved GPRs. */
937 if (frame->mask)
938 {
939 unsigned x_save_size = LARCH_STACK_ALIGN (num_x_saved * UNITS_PER_WORD);
940 unsigned num_save_restore
941 = 1 + loongarch_save_libcall_count (frame->mask);
942
943 /* Only use save/restore routines if they don't alter the stack size. */
944 if (LARCH_STACK_ALIGN (num_save_restore * UNITS_PER_WORD) == x_save_size)
945 frame->save_libcall_adjustment = x_save_size;
946
947 offset += x_save_size;
948 frame->gp_sp_offset = offset - UNITS_PER_WORD;
949 }
950 else
951 frame->gp_sp_offset = offset;
952 /* The hard frame pointer points above the callee-saved GPRs. */
953 frame->hard_frame_pointer_offset = offset;
954 /* Above the hard frame pointer is the callee-allocated varags save area. */
955 offset += LARCH_STACK_ALIGN (cfun->machine->varargs_size);
956 /* Next is the callee-allocated area for pretend stack arguments. */
957 offset += LARCH_STACK_ALIGN (crtl->args.pretend_args_size);
958 /* Arg pointer must be below pretend args, but must be above alignment
959 padding. */
960 frame->arg_pointer_offset = offset - crtl->args.pretend_args_size;
961 frame->total_size = offset;
962 /* Next points the incoming stack pointer and any incoming arguments. */
963
964 /* Only use save/restore routines when the GPRs are atop the frame. */
965 if (frame->hard_frame_pointer_offset != frame->total_size)
966 frame->save_libcall_adjustment = 0;
967 }
968
969 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
970 or argument pointer. TO is either the stack pointer or hard frame
971 pointer. */
972
973 HOST_WIDE_INT
974 loongarch_initial_elimination_offset (int from, int to)
975 {
976 HOST_WIDE_INT src, dest;
977
978 loongarch_compute_frame_info ();
979
980 if (to == HARD_FRAME_POINTER_REGNUM)
981 dest = cfun->machine->frame.hard_frame_pointer_offset;
982 else if (to == STACK_POINTER_REGNUM)
983 dest = 0; /* The stack pointer is the base of all offsets, hence 0. */
984 else
985 gcc_unreachable ();
986
987 if (from == FRAME_POINTER_REGNUM)
988 src = cfun->machine->frame.frame_pointer_offset;
989 else if (from == ARG_POINTER_REGNUM)
990 src = cfun->machine->frame.arg_pointer_offset;
991 else
992 gcc_unreachable ();
993
994 return src - dest;
995 }
996
997 /* A function to save or store a register. The first argument is the
998 register and the second is the stack slot. */
999 typedef void (*loongarch_save_restore_fn) (rtx, rtx);
1000
1001 /* Use FN to save or restore register REGNO. MODE is the register's
1002 mode and OFFSET is the offset of its save slot from the current
1003 stack pointer. */
1004
1005 static void
1006 loongarch_save_restore_reg (machine_mode mode, int regno, HOST_WIDE_INT offset,
1007 loongarch_save_restore_fn fn)
1008 {
1009 rtx mem;
1010
1011 mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx, offset));
1012 fn (gen_rtx_REG (mode, regno), mem);
1013 }
1014
1015 /* Call FN for each register that is saved by the current function.
1016 SP_OFFSET is the offset of the current stack pointer from the start
1017 of the frame. */
1018
1019 static void
1020 loongarch_for_each_saved_reg (HOST_WIDE_INT sp_offset,
1021 loongarch_save_restore_fn fn,
1022 bool skip_eh_data_regs_p)
1023 {
1024 HOST_WIDE_INT offset;
1025
1026 /* Save the link register and s-registers. */
1027 offset = cfun->machine->frame.gp_sp_offset - sp_offset;
1028 for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
1029 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
1030 {
1031 /* Special care needs to be taken for $r4-$r7 (EH_RETURN_DATA_REGNO)
1032 when returning normally from a function that calls
1033 __builtin_eh_return. In this case, these registers are saved but
1034 should not be restored, or the return value may be clobbered. */
1035
1036 if (!(cfun->machine->reg_is_wrapped_separately[regno]
1037 || (skip_eh_data_regs_p
1038 && GP_ARG_FIRST <= regno && regno < GP_ARG_FIRST + 4)))
1039 loongarch_save_restore_reg (word_mode, regno, offset, fn);
1040
1041 offset -= UNITS_PER_WORD;
1042 }
1043
1044 /* This loop must iterate over the same space as its companion in
1045 loongarch_compute_frame_info. */
1046 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
1047 machine_mode mode = TARGET_DOUBLE_FLOAT ? DFmode : SFmode;
1048
1049 for (int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
1050 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
1051 {
1052 if (!cfun->machine->reg_is_wrapped_separately[regno])
1053 loongarch_save_restore_reg (word_mode, regno, offset, fn);
1054
1055 offset -= GET_MODE_SIZE (mode);
1056 }
1057 }
1058
1059 /* Emit a move from SRC to DEST. Assume that the move expanders can
1060 handle all moves if !can_create_pseudo_p (). The distinction is
1061 important because, unlike emit_move_insn, the move expanders know
1062 how to force Pmode objects into the constant pool even when the
1063 constant pool address is not itself legitimate. */
1064
1065 rtx
1066 loongarch_emit_move (rtx dest, rtx src)
1067 {
1068 return (can_create_pseudo_p () ? emit_move_insn (dest, src)
1069 : emit_move_insn_1 (dest, src));
1070 }
1071
1072 /* Save register REG to MEM. Make the instruction frame-related. */
1073
1074 static void
1075 loongarch_save_reg (rtx reg, rtx mem)
1076 {
1077 loongarch_emit_move (mem, reg);
1078 loongarch_set_frame_expr (loongarch_frame_set (mem, reg));
1079 }
1080
1081 /* Restore register REG from MEM. */
1082
1083 static void
1084 loongarch_restore_reg (rtx reg, rtx mem)
1085 {
1086 rtx insn = loongarch_emit_move (reg, mem);
1087 rtx dwarf = NULL_RTX;
1088 dwarf = alloc_reg_note (REG_CFA_RESTORE, reg, dwarf);
1089 REG_NOTES (insn) = dwarf;
1090
1091 RTX_FRAME_RELATED_P (insn) = 1;
1092 }
1093
1094 /* For stack frames that can't be allocated with a single ADDI instruction,
1095 compute the best value to initially allocate. It must at a minimum
1096 allocate enough space to spill the callee-saved registers. */
1097
1098 static HOST_WIDE_INT
1099 loongarch_first_stack_step (struct loongarch_frame_info *frame)
1100 {
1101 HOST_WIDE_INT min_first_step
1102 = LARCH_STACK_ALIGN (frame->total_size - frame->fp_sp_offset);
1103
1104 /* When stack checking is required, if the sum of frame->total_size
1105 and stack_check_protect is greater than stack clash protection guard
1106 size, then return min_first_step. */
1107 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK
1108 || (flag_stack_clash_protection
1109 && frame->total_size > STACK_CLASH_PROTECTION_GUARD_SIZE))
1110 return min_first_step;
1111
1112 if (IMM12_OPERAND (frame->total_size))
1113 return frame->total_size;
1114
1115 HOST_WIDE_INT max_first_step = IMM_REACH / 2 - PREFERRED_STACK_BOUNDARY / 8;
1116 HOST_WIDE_INT min_second_step = frame->total_size - max_first_step;
1117 gcc_assert (min_first_step <= max_first_step);
1118
1119 /* As an optimization, use the least-significant bits of the total frame
1120 size, so that the second adjustment step is just LU12I + ADD. */
1121 if (!IMM12_OPERAND (min_second_step)
1122 && frame->total_size % IMM_REACH < IMM_REACH / 2
1123 && frame->total_size % IMM_REACH >= min_first_step)
1124 return frame->total_size % IMM_REACH;
1125
1126 return max_first_step;
1127 }
1128
1129 static void
1130 loongarch_emit_stack_tie (void)
1131 {
1132 emit_insn (gen_stack_tie (Pmode, stack_pointer_rtx,
1133 frame_pointer_needed ? hard_frame_pointer_rtx
1134 : stack_pointer_rtx));
1135 }
1136
1137 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1138
1139 #if PROBE_INTERVAL > 16384
1140 #error Cannot use indexed addressing mode for stack probing
1141 #endif
1142
1143 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
1144 inclusive. These are offsets from the current stack pointer. */
1145
1146 static void
1147 loongarch_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
1148 {
1149 HOST_WIDE_INT rounded_size;
1150 HOST_WIDE_INT interval;
1151
1152 if (flag_stack_clash_protection)
1153 interval = STACK_CLASH_PROTECTION_GUARD_SIZE;
1154 else
1155 interval = PROBE_INTERVAL;
1156
1157 rtx r12 = LARCH_PROLOGUE_TEMP2 (Pmode);
1158 rtx r14 = LARCH_PROLOGUE_TEMP3 (Pmode);
1159
1160 size = size + first;
1161
1162 /* Sanity check for the addressing mode we're going to use. */
1163 gcc_assert (first <= 16384);
1164
1165 /* Step 1: round SIZE to the previous multiple of the interval. */
1166
1167 rounded_size = ROUND_DOWN (size, interval);
1168
1169 /* Step 2: compute initial and final value of the loop counter. */
1170
1171 emit_move_insn (r14, GEN_INT (interval));
1172
1173 /* If rounded_size is zero, it means that the space requested by
1174 the local variable is less than the interval, and there is no
1175 need to display and detect the allocated space. */
1176 if (rounded_size != 0)
1177 {
1178 /* Step 3: the loop
1179
1180 do
1181 {
1182 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1183 probe at TEST_ADDR
1184 }
1185 while (TEST_ADDR != LAST_ADDR)
1186
1187 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1188 until it is equal to ROUNDED_SIZE. */
1189
1190 if (rounded_size <= STACK_CLASH_MAX_UNROLL_PAGES * interval)
1191 {
1192 for (HOST_WIDE_INT i = 0; i < rounded_size; i += interval)
1193 {
1194 emit_insn (gen_rtx_SET (stack_pointer_rtx,
1195 gen_rtx_MINUS (Pmode,
1196 stack_pointer_rtx,
1197 r14)));
1198 emit_move_insn (gen_rtx_MEM (Pmode,
1199 gen_rtx_PLUS (Pmode,
1200 stack_pointer_rtx,
1201 const0_rtx)),
1202 const0_rtx);
1203 emit_insn (gen_blockage ());
1204 }
1205 dump_stack_clash_frame_info (PROBE_INLINE, size != rounded_size);
1206 }
1207 else
1208 {
1209 emit_move_insn (r12, GEN_INT (rounded_size));
1210 emit_insn (gen_rtx_SET (r12,
1211 gen_rtx_MINUS (Pmode,
1212 stack_pointer_rtx,
1213 r12)));
1214
1215 emit_insn (gen_probe_stack_range (Pmode, stack_pointer_rtx,
1216 stack_pointer_rtx, r12, r14));
1217 emit_insn (gen_blockage ());
1218 dump_stack_clash_frame_info (PROBE_LOOP, size != rounded_size);
1219 }
1220 }
1221 else
1222 dump_stack_clash_frame_info (NO_PROBE_SMALL_FRAME, true);
1223
1224
1225 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1226 that SIZE is equal to ROUNDED_SIZE. */
1227
1228 if (size != rounded_size)
1229 {
1230 if (size - rounded_size >= 2048)
1231 {
1232 emit_move_insn (r14, GEN_INT (size - rounded_size));
1233 emit_insn (gen_rtx_SET (stack_pointer_rtx,
1234 gen_rtx_MINUS (Pmode,
1235 stack_pointer_rtx,
1236 r14)));
1237 }
1238 else
1239 emit_insn (gen_rtx_SET (stack_pointer_rtx,
1240 gen_rtx_PLUS (Pmode,
1241 stack_pointer_rtx,
1242 GEN_INT (rounded_size - size))));
1243 }
1244
1245 if (first)
1246 {
1247 emit_move_insn (r12, GEN_INT (first));
1248 emit_insn (gen_rtx_SET (stack_pointer_rtx,
1249 gen_rtx_PLUS (Pmode,
1250 stack_pointer_rtx, r12)));
1251 }
1252 /* Make sure nothing is scheduled before we are done. */
1253 emit_insn (gen_blockage ());
1254 }
1255
1256 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
1257 absolute addresses. */
1258 const char *
1259 loongarch_output_probe_stack_range (rtx reg1, rtx reg2, rtx reg3)
1260 {
1261 static int labelno = 0;
1262 char loop_lab[32], tmp[64];
1263 rtx xops[3];
1264
1265 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
1266
1267 /* Loop. */
1268 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
1269
1270 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1271 xops[0] = reg1;
1272 xops[2] = reg3;
1273 if (TARGET_64BIT)
1274 output_asm_insn ("sub.d\t%0,%0,%2", xops);
1275 else
1276 output_asm_insn ("sub.w\t%0,%0,%2", xops);
1277
1278 /* Probe at TEST_ADDR, test if TEST_ADDR == LAST_ADDR and branch. */
1279 xops[1] = reg2;
1280 strcpy (tmp, "bne\t%0,%1,");
1281 if (TARGET_64BIT)
1282 output_asm_insn ("st.d\t$r0,%0,0", xops);
1283 else
1284 output_asm_insn ("st.w\t$r0,%0,0", xops);
1285 output_asm_insn (strcat (tmp, &loop_lab[1]), xops);
1286
1287 return "";
1288 }
1289
1290 /* Expand the "prologue" pattern. */
1291
1292 void
1293 loongarch_expand_prologue (void)
1294 {
1295 struct loongarch_frame_info *frame = &cfun->machine->frame;
1296 HOST_WIDE_INT size = frame->total_size;
1297 rtx insn;
1298
1299 if (flag_stack_usage_info)
1300 current_function_static_stack_size = size;
1301
1302 /* Save the registers. */
1303 if ((frame->mask | frame->fmask) != 0)
1304 {
1305 HOST_WIDE_INT step1 = MIN (size, loongarch_first_stack_step (frame));
1306
1307 insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
1308 GEN_INT (-step1));
1309 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
1310 size -= step1;
1311 loongarch_for_each_saved_reg (size, loongarch_save_reg, false);
1312 }
1313
1314 /* Set up the frame pointer, if we're using one. */
1315 if (frame_pointer_needed)
1316 {
1317 insn = gen_add3_insn (hard_frame_pointer_rtx, stack_pointer_rtx,
1318 GEN_INT (frame->hard_frame_pointer_offset - size));
1319 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
1320
1321 loongarch_emit_stack_tie ();
1322 }
1323
1324 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK
1325 || flag_stack_clash_protection)
1326 {
1327 HOST_WIDE_INT first = get_stack_check_protect ();
1328
1329 if (frame->total_size == 0)
1330 {
1331 /* do nothing. */
1332 dump_stack_clash_frame_info (NO_PROBE_NO_FRAME, false);
1333 return;
1334 }
1335
1336 if (crtl->is_leaf && !cfun->calls_alloca)
1337 {
1338 HOST_WIDE_INT interval;
1339
1340 if (flag_stack_clash_protection)
1341 interval = STACK_CLASH_PROTECTION_GUARD_SIZE;
1342 else
1343 interval = PROBE_INTERVAL;
1344
1345 if (size > interval && size > first)
1346 loongarch_emit_probe_stack_range (first, size - first);
1347 else
1348 loongarch_emit_probe_stack_range (first, size);
1349 }
1350 else
1351 loongarch_emit_probe_stack_range (first, size);
1352
1353 if (size > 0)
1354 {
1355 /* Describe the effect of the previous instructions. */
1356 insn = plus_constant (Pmode, stack_pointer_rtx, -size);
1357 insn = gen_rtx_SET (stack_pointer_rtx, insn);
1358 loongarch_set_frame_expr (insn);
1359 }
1360 return;
1361 }
1362
1363 if (size > 0)
1364 {
1365 if (IMM12_OPERAND (-size))
1366 {
1367 insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
1368 GEN_INT (-size));
1369 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
1370 }
1371 else
1372 {
1373 loongarch_emit_move (LARCH_PROLOGUE_TEMP (Pmode),
1374 GEN_INT (-size));
1375 emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
1376 LARCH_PROLOGUE_TEMP (Pmode)));
1377
1378 /* Describe the effect of the previous instructions. */
1379 insn = plus_constant (Pmode, stack_pointer_rtx, -size);
1380 insn = gen_rtx_SET (stack_pointer_rtx, insn);
1381 loongarch_set_frame_expr (insn);
1382 }
1383 }
1384 }
1385
1386 /* Return nonzero if this function is known to have a null epilogue.
1387 This allows the optimizer to omit jumps to jumps if no stack
1388 was created. */
1389
1390 bool
1391 loongarch_can_use_return_insn (void)
1392 {
1393 return reload_completed && cfun->machine->frame.total_size == 0;
1394 }
1395
1396 /* Expand function epilogue using the following insn patterns:
1397 "epilogue" (style == NORMAL_RETURN)
1398 "sibcall_epilogue" (style == SIBCALL_RETURN)
1399 "eh_return" (style == EXCEPTION_RETURN) */
1400
1401 void
1402 loongarch_expand_epilogue (int style)
1403 {
1404 /* Split the frame into two. STEP1 is the amount of stack we should
1405 deallocate before restoring the registers. STEP2 is the amount we
1406 should deallocate afterwards.
1407
1408 Start off by assuming that no registers need to be restored. */
1409 struct loongarch_frame_info *frame = &cfun->machine->frame;
1410 HOST_WIDE_INT step1 = frame->total_size;
1411 HOST_WIDE_INT step2 = 0;
1412 rtx ra = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
1413 rtx insn;
1414
1415 /* We need to add memory barrier to prevent read from deallocated stack. */
1416 bool need_barrier_p
1417 = (get_frame_size () + cfun->machine->frame.arg_pointer_offset) != 0;
1418
1419 /* Handle simple returns. */
1420 if (style == NORMAL_RETURN && loongarch_can_use_return_insn ())
1421 {
1422 emit_jump_insn (gen_return ());
1423 return;
1424 }
1425
1426 /* Move past any dynamic stack allocations. */
1427 if (cfun->calls_alloca)
1428 {
1429 /* Emit a barrier to prevent loads from a deallocated stack. */
1430 loongarch_emit_stack_tie ();
1431 need_barrier_p = false;
1432
1433 rtx adjust = GEN_INT (-frame->hard_frame_pointer_offset);
1434 if (!IMM12_OPERAND (INTVAL (adjust)))
1435 {
1436 loongarch_emit_move (LARCH_PROLOGUE_TEMP (Pmode), adjust);
1437 adjust = LARCH_PROLOGUE_TEMP (Pmode);
1438 }
1439
1440 insn = emit_insn (gen_add3_insn (stack_pointer_rtx,
1441 hard_frame_pointer_rtx,
1442 adjust));
1443
1444 rtx dwarf = NULL_RTX;
1445 rtx minus_offset = GEN_INT (-frame->hard_frame_pointer_offset);
1446 rtx cfa_adjust_value = gen_rtx_PLUS (Pmode,
1447 hard_frame_pointer_rtx,
1448 minus_offset);
1449
1450 rtx cfa_adjust_rtx = gen_rtx_SET (stack_pointer_rtx, cfa_adjust_value);
1451 dwarf = alloc_reg_note (REG_CFA_ADJUST_CFA, cfa_adjust_rtx, dwarf);
1452 RTX_FRAME_RELATED_P (insn) = 1;
1453
1454 REG_NOTES (insn) = dwarf;
1455 }
1456
1457 /* If we need to restore registers, deallocate as much stack as
1458 possible in the second step without going out of range. */
1459 if ((frame->mask | frame->fmask) != 0)
1460 {
1461 step2 = loongarch_first_stack_step (frame);
1462 step1 -= step2;
1463 }
1464
1465 /* Set TARGET to BASE + STEP1. */
1466 if (step1 > 0)
1467 {
1468 /* Emit a barrier to prevent loads from a deallocated stack. */
1469 loongarch_emit_stack_tie ();
1470 need_barrier_p = false;
1471
1472 /* Get an rtx for STEP1 that we can add to BASE. */
1473 rtx adjust = GEN_INT (step1);
1474 if (!IMM12_OPERAND (step1))
1475 {
1476 loongarch_emit_move (LARCH_PROLOGUE_TEMP (Pmode), adjust);
1477 adjust = LARCH_PROLOGUE_TEMP (Pmode);
1478 }
1479
1480 insn = emit_insn (gen_add3_insn (stack_pointer_rtx,
1481 stack_pointer_rtx,
1482 adjust));
1483
1484 rtx dwarf = NULL_RTX;
1485 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
1486 GEN_INT (step2));
1487
1488 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
1489 RTX_FRAME_RELATED_P (insn) = 1;
1490
1491 REG_NOTES (insn) = dwarf;
1492 }
1493
1494 /* Restore the registers. */
1495 loongarch_for_each_saved_reg (frame->total_size - step2,
1496 loongarch_restore_reg,
1497 crtl->calls_eh_return
1498 && style != EXCEPTION_RETURN);
1499
1500 if (need_barrier_p)
1501 loongarch_emit_stack_tie ();
1502
1503 /* Deallocate the final bit of the frame. */
1504 if (step2 > 0)
1505 {
1506 insn = emit_insn (gen_add3_insn (stack_pointer_rtx,
1507 stack_pointer_rtx,
1508 GEN_INT (step2)));
1509
1510 rtx dwarf = NULL_RTX;
1511 rtx cfa_adjust_rtx = gen_rtx_PLUS (Pmode, stack_pointer_rtx, const0_rtx);
1512 dwarf = alloc_reg_note (REG_CFA_DEF_CFA, cfa_adjust_rtx, dwarf);
1513 RTX_FRAME_RELATED_P (insn) = 1;
1514
1515 REG_NOTES (insn) = dwarf;
1516 }
1517
1518 /* Add in the __builtin_eh_return stack adjustment. */
1519 if (crtl->calls_eh_return && style == EXCEPTION_RETURN)
1520 emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
1521 EH_RETURN_STACKADJ_RTX));
1522
1523 /* Emit return unless doing sibcall. */
1524 if (style != SIBCALL_RETURN)
1525 emit_jump_insn (gen_simple_return_internal (ra));
1526 }
1527
1528 #define LU32I_B (0xfffffULL << 32)
1529 #define LU52I_B (0xfffULL << 52)
1530
1531 /* Fill CODES with a sequence of rtl operations to load VALUE.
1532 Return the number of operations needed. */
1533
1534 static unsigned int
1535 loongarch_build_integer (struct loongarch_integer_op *codes,
1536 HOST_WIDE_INT value)
1537
1538 {
1539 unsigned int cost = 0;
1540
1541 /* Get the lower 32 bits of the value. */
1542 HOST_WIDE_INT low_part = (int32_t)value;
1543
1544 if (IMM12_OPERAND (low_part) || IMM12_OPERAND_UNSIGNED (low_part))
1545 {
1546 /* The value of the lower 32 bit be loaded with one instruction.
1547 lu12i.w. */
1548 codes[cost].code = UNKNOWN;
1549 codes[cost].method = METHOD_NORMAL;
1550 codes[cost].value = low_part;
1551 codes[cost].curr_value = low_part;
1552 cost++;
1553 }
1554 else
1555 {
1556 /* lu12i.w + ior. */
1557 codes[cost].code = UNKNOWN;
1558 codes[cost].method = METHOD_NORMAL;
1559 codes[cost].value = low_part & ~(IMM_REACH - 1);
1560 codes[cost].curr_value = codes[cost].value;
1561 cost++;
1562 HOST_WIDE_INT iorv = low_part & (IMM_REACH - 1);
1563 if (iorv != 0)
1564 {
1565 codes[cost].code = IOR;
1566 codes[cost].method = METHOD_NORMAL;
1567 codes[cost].value = iorv;
1568 codes[cost].curr_value = low_part;
1569 cost++;
1570 }
1571 }
1572
1573 if (TARGET_64BIT)
1574 {
1575 bool lu32i[2] = {(value & LU32I_B) == 0, (value & LU32I_B) == LU32I_B};
1576 bool lu52i[2] = {(value & LU52I_B) == 0, (value & LU52I_B) == LU52I_B};
1577
1578 int sign31 = (value & (HOST_WIDE_INT_1U << 31)) >> 31;
1579 int sign51 = (value & (HOST_WIDE_INT_1U << 51)) >> 51;
1580
1581 uint32_t hival = (uint32_t) (value >> 32);
1582 uint32_t loval = (uint32_t) value;
1583
1584 /* Determine whether the upper 32 bits are sign-extended from the lower
1585 32 bits. If it is, the instructions to load the high order can be
1586 ommitted. */
1587 if (lu32i[sign31] && lu52i[sign31])
1588 return cost;
1589 /* If the lower 32 bits are the same as the upper 32 bits, just copy
1590 the lower 32 bits to the upper 32 bits. */
1591 else if (loval == hival)
1592 {
1593 codes[cost].method = METHOD_MIRROR;
1594 codes[cost].curr_value = value;
1595 return cost + 1;
1596 }
1597 /* Determine whether bits 32-51 are sign-extended from the lower 32
1598 bits. If so, directly load 52-63 bits. */
1599 else if (lu32i[sign31])
1600 {
1601 codes[cost].method = METHOD_LU52I;
1602 codes[cost].value = value & LU52I_B;
1603 codes[cost].curr_value = value;
1604 return cost + 1;
1605 }
1606
1607 codes[cost].method = METHOD_LU32I;
1608 codes[cost].value = (value & LU32I_B) | (sign51 ? LU52I_B : 0);
1609 codes[cost].curr_value = (value & 0xfffffffffffff)
1610 | (sign51 ? LU52I_B : 0);
1611 cost++;
1612
1613 /* Determine whether the 52-61 bits are sign-extended from the low order,
1614 and if not, load the 52-61 bits. */
1615 if (!lu52i[(value & (HOST_WIDE_INT_1U << 51)) >> 51])
1616 {
1617 codes[cost].method = METHOD_LU52I;
1618 codes[cost].value = value & LU52I_B;
1619 codes[cost].curr_value = value;
1620 cost++;
1621 }
1622 }
1623
1624 gcc_assert (cost <= LARCH_MAX_INTEGER_OPS);
1625
1626 return cost;
1627 }
1628
1629 /* Fill CODES with a sequence of rtl operations to load VALUE.
1630 Return the number of operations needed.
1631 Split interger in loongarch_output_move. */
1632
1633 static unsigned int
1634 loongarch_integer_cost (HOST_WIDE_INT value)
1635 {
1636 struct loongarch_integer_op codes[LARCH_MAX_INTEGER_OPS];
1637 return loongarch_build_integer (codes, value);
1638 }
1639
1640 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
1641
1642 static bool
1643 loongarch_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1644 {
1645 return loongarch_const_insns (x) > 0;
1646 }
1647
1648 /* Return true if X is a thread-local symbol. */
1649
1650 static bool
1651 loongarch_tls_symbol_p (rtx x)
1652 {
1653 return SYMBOL_REF_P (x) && SYMBOL_REF_TLS_MODEL (x) != 0;
1654 }
1655
1656 /* Return true if SYMBOL_REF X is associated with a global symbol
1657 (in the STB_GLOBAL sense). */
1658
1659 bool
1660 loongarch_global_symbol_p (const_rtx x)
1661 {
1662 if (LABEL_REF_P (x))
1663 return false;
1664
1665 const_tree decl = SYMBOL_REF_DECL (x);
1666
1667 if (!decl)
1668 return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1669
1670 /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1671 or weak symbols. Relocations in the object file will be against
1672 the target symbol, so it's that symbol's binding that matters here. */
1673 return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1674 }
1675
1676 bool
1677 loongarch_global_symbol_noweak_p (const_rtx x)
1678 {
1679 if (LABEL_REF_P (x))
1680 return false;
1681
1682 const_tree decl = SYMBOL_REF_DECL (x);
1683
1684 if (!decl)
1685 return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1686
1687 return DECL_P (decl) && TREE_PUBLIC (decl);
1688 }
1689
1690 bool
1691 loongarch_weak_symbol_p (const_rtx x)
1692 {
1693 const_tree decl;
1694 if (LABEL_REF_P (x) || !(decl = SYMBOL_REF_DECL (x)))
1695 return false;
1696 return DECL_P (decl) && DECL_WEAK (decl);
1697 }
1698
1699 /* Return true if SYMBOL_REF X binds locally. */
1700
1701 bool
1702 loongarch_symbol_binds_local_p (const_rtx x)
1703 {
1704 if (TARGET_DIRECT_EXTERN_ACCESS)
1705 return true;
1706
1707 if (SYMBOL_REF_P (x))
1708 return (SYMBOL_REF_DECL (x)
1709 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1710 : SYMBOL_REF_LOCAL_P (x));
1711 else
1712 return false;
1713 }
1714
1715 /* Return true if OP is a constant vector with the number of units in MODE,
1716 and each unit has the same bit set. */
1717
1718 bool
1719 loongarch_const_vector_bitimm_set_p (rtx op, machine_mode mode)
1720 {
1721 if (GET_CODE (op) == CONST_VECTOR
1722 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
1723 || GET_MODE_CLASS (mode) == MODE_VECTOR_INT))
1724 {
1725 unsigned HOST_WIDE_INT val = UINTVAL (CONST_VECTOR_ELT (op, 0));
1726 int vlog2 = exact_log2 (val & GET_MODE_MASK (GET_MODE_INNER (mode)));
1727
1728 if (vlog2 != -1)
1729 {
1730 gcc_assert (vlog2 >= 0 && vlog2 <= GET_MODE_UNIT_BITSIZE (mode) - 1);
1731 return loongarch_const_vector_same_val_p (op, mode);
1732 }
1733 }
1734
1735 return false;
1736 }
1737
1738 /* Return true if OP is a constant vector with the number of units in MODE,
1739 and each unit has the same bit clear. */
1740
1741 bool
1742 loongarch_const_vector_bitimm_clr_p (rtx op, machine_mode mode)
1743 {
1744 if (GET_CODE (op) == CONST_VECTOR
1745 && (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
1746 || GET_MODE_CLASS (mode) == MODE_VECTOR_INT))
1747 {
1748 unsigned HOST_WIDE_INT val;
1749 if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
1750 {
1751 rtx val_s = CONST_VECTOR_ELT (op, 0);
1752 const REAL_VALUE_TYPE *x = CONST_DOUBLE_REAL_VALUE (val_s);
1753 if (GET_MODE (val_s) == DFmode)
1754 {
1755 long tmp[2];
1756 REAL_VALUE_TO_TARGET_DOUBLE (*x, tmp);
1757 val = ~((unsigned HOST_WIDE_INT) tmp[1] << 32 | tmp[0]);
1758 }
1759 else
1760 {
1761 long tmp;
1762 REAL_VALUE_TO_TARGET_SINGLE (*x, tmp);
1763 val = ~((unsigned HOST_WIDE_INT) tmp);
1764 }
1765 }
1766 else
1767 val = ~UINTVAL (CONST_VECTOR_ELT (op, 0));
1768
1769 int vlog2 = exact_log2 (val & GET_MODE_MASK (GET_MODE_INNER (mode)));
1770
1771 if (vlog2 != -1)
1772 {
1773 gcc_assert (vlog2 >= 0 && vlog2 <= GET_MODE_UNIT_BITSIZE (mode) - 1);
1774 return loongarch_const_vector_same_val_p (op, mode);
1775 }
1776 }
1777
1778 return false;
1779 }
1780
1781 /* Return true if OP is a constant vector with the number of units in MODE,
1782 and each unit has the same value. */
1783
1784 bool
1785 loongarch_const_vector_same_val_p (rtx op, machine_mode mode)
1786 {
1787 int i, nunits = GET_MODE_NUNITS (mode);
1788 rtx first;
1789
1790 if (GET_CODE (op) != CONST_VECTOR || GET_MODE (op) != mode)
1791 return false;
1792
1793 first = CONST_VECTOR_ELT (op, 0);
1794 for (i = 1; i < nunits; i++)
1795 if (!rtx_equal_p (first, CONST_VECTOR_ELT (op, i)))
1796 return false;
1797
1798 return true;
1799 }
1800
1801 /* Return true if OP is a constant vector with the number of units in MODE,
1802 and each unit has the same value as well as replicated bytes in the value.
1803 */
1804
1805 bool
1806 loongarch_const_vector_same_bytes_p (rtx op, machine_mode mode)
1807 {
1808 int i, bytes;
1809 HOST_WIDE_INT val, first_byte;
1810 rtx first;
1811
1812 if (!loongarch_const_vector_same_val_p (op, mode))
1813 return false;
1814
1815 first = CONST_VECTOR_ELT (op, 0);
1816 bytes = GET_MODE_UNIT_SIZE (mode);
1817 val = INTVAL (first);
1818 first_byte = val & 0xff;
1819 for (i = 1; i < bytes; i++)
1820 {
1821 val >>= 8;
1822 if ((val & 0xff) != first_byte)
1823 return false;
1824 }
1825
1826 return true;
1827 }
1828
1829 /* Return true if OP is a constant vector with the number of units in MODE,
1830 and each unit has the same integer value in the range [LOW, HIGH]. */
1831
1832 bool
1833 loongarch_const_vector_same_int_p (rtx op, machine_mode mode, HOST_WIDE_INT low,
1834 HOST_WIDE_INT high)
1835 {
1836 HOST_WIDE_INT value;
1837 rtx elem0;
1838
1839 if (!loongarch_const_vector_same_val_p (op, mode))
1840 return false;
1841
1842 elem0 = CONST_VECTOR_ELT (op, 0);
1843 if (!CONST_INT_P (elem0))
1844 return false;
1845
1846 value = INTVAL (elem0);
1847 return (value >= low && value <= high);
1848 }
1849
1850 /* Return true if OP is a constant vector with repeated 4-element sets
1851 in mode MODE. */
1852
1853 bool
1854 loongarch_const_vector_shuffle_set_p (rtx op, machine_mode mode)
1855 {
1856 int nunits = GET_MODE_NUNITS (mode);
1857 int nsets = nunits / 4;
1858 int set = 0;
1859 int i, j;
1860
1861 /* Check if we have the same 4-element sets. */
1862 for (j = 0; j < nsets; j++, set = 4 * j)
1863 for (i = 0; i < 4; i++)
1864 if ((INTVAL (XVECEXP (op, 0, i))
1865 != (INTVAL (XVECEXP (op, 0, set + i)) - set))
1866 || !IN_RANGE (INTVAL (XVECEXP (op, 0, set + i)), 0, set + 3))
1867 return false;
1868 return true;
1869 }
1870
1871 rtx
1872 loongarch_const_vector_vrepli (rtx x, machine_mode mode)
1873 {
1874 int size = GET_MODE_SIZE (mode);
1875
1876 if (GET_CODE (x) != CONST_VECTOR
1877 || GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
1878 return NULL_RTX;
1879
1880 for (scalar_int_mode elem_mode: {QImode, HImode, SImode, DImode})
1881 {
1882 machine_mode new_mode =
1883 mode_for_vector (elem_mode, size / GET_MODE_SIZE (elem_mode))
1884 .require ();
1885 rtx op = lowpart_subreg (new_mode, x, mode);
1886 if (loongarch_const_vector_same_int_p (op, new_mode, -512, 511))
1887 return op;
1888 }
1889
1890 return NULL_RTX;
1891 }
1892
1893 /* Return true if rtx constants of mode MODE should be put into a small
1894 data section. */
1895
1896 static bool
1897 loongarch_rtx_constant_in_small_data_p (machine_mode mode)
1898 {
1899 return (GET_MODE_SIZE (mode) <= g_switch_value);
1900 }
1901
1902 /* Return the method that should be used to access SYMBOL_REF or
1903 LABEL_REF X. */
1904
1905 static enum loongarch_symbol_type
1906 loongarch_classify_symbol (const_rtx x)
1907 {
1908 enum loongarch_symbol_type pcrel =
1909 TARGET_CMODEL_EXTREME ? SYMBOL_PCREL64 : SYMBOL_PCREL;
1910
1911 if (!SYMBOL_REF_P (x))
1912 return pcrel;
1913
1914 if (SYMBOL_REF_TLS_MODEL (x))
1915 return SYMBOL_TLS;
1916
1917 if (!loongarch_symbol_binds_local_p (x))
1918 return SYMBOL_GOT_DISP;
1919
1920 tree t = SYMBOL_REF_DECL (x);
1921 if (!t)
1922 return pcrel;
1923
1924 t = lookup_attribute ("model", DECL_ATTRIBUTES (t));
1925 if (!t)
1926 return pcrel;
1927
1928 t = TREE_VALUE (TREE_VALUE (t));
1929
1930 /* loongarch_handle_model_attribute should reject other values. */
1931 gcc_assert (TREE_CODE (t) == STRING_CST);
1932
1933 const char *model = TREE_STRING_POINTER (t);
1934 if (strcmp (model, "normal") == 0)
1935 return SYMBOL_PCREL;
1936 if (strcmp (model, "extreme") == 0)
1937 return SYMBOL_PCREL64;
1938
1939 /* loongarch_handle_model_attribute should reject unknown model
1940 name. */
1941 gcc_unreachable ();
1942 }
1943
1944 /* Classify the base of symbolic expression X, given that X appears in
1945 context CONTEXT. */
1946
1947 static enum loongarch_symbol_type
1948 loongarch_classify_symbolic_expression (rtx x)
1949 {
1950 rtx offset;
1951
1952 split_const (x, &x, &offset);
1953 if (UNSPEC_ADDRESS_P (x))
1954 return UNSPEC_ADDRESS_TYPE (x);
1955
1956 return loongarch_classify_symbol (x);
1957 }
1958
1959 /* Return true if X is a symbolic constant. If it is,
1960 store the type of the symbol in *SYMBOL_TYPE. */
1961
1962 bool
1963 loongarch_symbolic_constant_p (rtx x, enum loongarch_symbol_type *symbol_type)
1964 {
1965 rtx offset;
1966
1967 split_const (x, &x, &offset);
1968 if (UNSPEC_ADDRESS_P (x))
1969 {
1970 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1971 x = UNSPEC_ADDRESS (x);
1972 }
1973 else if (SYMBOL_REF_P (x) || LABEL_REF_P (x))
1974 *symbol_type = loongarch_classify_symbol (x);
1975 else
1976 return false;
1977
1978 if (offset == const0_rtx)
1979 return true;
1980
1981 /* Check whether a nonzero offset is valid for the underlying
1982 relocations. */
1983 switch (*symbol_type)
1984 {
1985 case SYMBOL_PCREL64:
1986 /* When the code model is extreme, the non-zero offset situation
1987 has not been handled well, so it is disabled here now. */
1988 if (!loongarch_explicit_relocs_p (SYMBOL_PCREL64))
1989 return false;
1990 /* fall through */
1991 case SYMBOL_PCREL:
1992 /* GAS rejects offsets outside the range [-2^31, 2^31-1]. */
1993 return sext_hwi (INTVAL (offset), 32) == INTVAL (offset);
1994
1995 /* The following symbol types do not allow non-zero offsets. */
1996 case SYMBOL_GOT_DISP:
1997 case SYMBOL_TLS_IE:
1998 case SYMBOL_TLSGD:
1999 case SYMBOL_TLSLDM:
2000 case SYMBOL_TLS:
2001 /* From an implementation perspective, tls_le symbols are allowed to
2002 have non-zero offsets, but currently binutils has not added support,
2003 so the generation of non-zero offsets is prohibited here. */
2004 case SYMBOL_TLS_LE:
2005 return false;
2006 }
2007 gcc_unreachable ();
2008 }
2009
2010 /* If -mexplicit-relocs=auto, we use machine operations with reloc hints
2011 for cases where the linker is unable to relax so we can schedule the
2012 machine operations, otherwise use an assembler pseudo-op so the
2013 assembler will generate R_LARCH_RELAX. */
2014
2015 bool
2016 loongarch_explicit_relocs_p (enum loongarch_symbol_type type)
2017 {
2018 if (la_opt_explicit_relocs != EXPLICIT_RELOCS_AUTO)
2019 return la_opt_explicit_relocs == EXPLICIT_RELOCS_ALWAYS;
2020
2021 /* The linker don't know how to relax accesses in extreme code model. */
2022 if (loongarch_symbol_extreme_p (type))
2023 return true;
2024
2025 switch (type)
2026 {
2027 case SYMBOL_TLS_IE:
2028 case SYMBOL_TLS_LE:
2029 case SYMBOL_PCREL64:
2030 /* TLS IE cannot be relaxed. TLS LE relaxation is different from
2031 the normal R_LARCH_RELAX-based relaxation and it **requires**
2032 using the explicit %le_{lo12,hi20,add}_r relocs. The linker
2033 does not relax 64-bit pc-relative accesses as at now. */
2034 return true;
2035 case SYMBOL_GOT_DISP:
2036 /* If we are performing LTO for a final link, and we have the
2037 linker plugin so we know the resolution of the symbols, then
2038 all GOT references are binding to external symbols or
2039 preemptable symbols. So the linker cannot relax them. */
2040 return (in_lto_p
2041 && !flag_incremental_link
2042 && HAVE_LTO_PLUGIN == 2
2043 && (!global_options_set.x_flag_use_linker_plugin
2044 || global_options.x_flag_use_linker_plugin));
2045 default:
2046 return false;
2047 }
2048 }
2049
2050 /* Returns the number of instructions necessary to reference a symbol. */
2051
2052 static int
2053 loongarch_symbol_insns (enum loongarch_symbol_type type, machine_mode mode)
2054 {
2055 /* LSX LD.* and ST.* cannot support loading symbols via an immediate
2056 operand. */
2057 if (mode != MAX_MACHINE_MODE
2058 && (LSX_SUPPORTED_MODE_P (mode) || LASX_SUPPORTED_MODE_P (mode)))
2059 return 0;
2060
2061 switch (type)
2062 {
2063 case SYMBOL_GOT_DISP:
2064 /* The constant will have to be loaded from the GOT before it
2065 is used in an address. */
2066 if (!loongarch_explicit_relocs_p (type) && mode != MAX_MACHINE_MODE)
2067 return 0;
2068
2069 return 3;
2070
2071 case SYMBOL_PCREL:
2072 case SYMBOL_TLS_IE:
2073 case SYMBOL_TLS_LE:
2074 return 2;
2075
2076 case SYMBOL_TLSGD:
2077 case SYMBOL_TLSLDM:
2078 return TARGET_TLS_DESC ? 4 : 3;
2079
2080 case SYMBOL_PCREL64:
2081 return 5;
2082
2083 case SYMBOL_TLS:
2084 /* We don't treat a bare TLS symbol as a constant. */
2085 return 0;
2086 }
2087 gcc_unreachable ();
2088 }
2089
2090 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
2091
2092 static bool
2093 loongarch_cannot_force_const_mem (machine_mode mode, rtx x)
2094 {
2095 enum loongarch_symbol_type type;
2096 rtx base, offset;
2097
2098 /* As an optimization, reject constants that loongarch_legitimize_move
2099 can expand inline.
2100
2101 Suppose we have a multi-instruction sequence that loads constant C
2102 into register R. If R does not get allocated a hard register, and
2103 R is used in an operand that allows both registers and memory
2104 references, reload will consider forcing C into memory and using
2105 one of the instruction's memory alternatives. Returning false
2106 here will force it to use an input reload instead. */
2107 if ((CONST_INT_P (x) || GET_CODE (x) == CONST_VECTOR)
2108 && loongarch_legitimate_constant_p (mode, x))
2109 return true;
2110
2111 split_const (x, &base, &offset);
2112 if (loongarch_symbolic_constant_p (base, &type))
2113 {
2114 /* The same optimization as for CONST_INT. */
2115 if (IMM12_INT (offset)
2116 && loongarch_symbol_insns (type, MAX_MACHINE_MODE) > 0)
2117 return true;
2118 }
2119
2120 /* TLS symbols must be computed by loongarch_legitimize_move. */
2121 if (tls_referenced_p (x))
2122 return true;
2123
2124 return false;
2125 }
2126
2127 /* Return true if register REGNO is a valid base register for mode MODE.
2128 STRICT_P is true if REG_OK_STRICT is in effect. */
2129
2130 int
2131 loongarch_regno_mode_ok_for_base_p (int regno,
2132 machine_mode mode ATTRIBUTE_UNUSED,
2133 bool strict_p)
2134 {
2135 if (!HARD_REGISTER_NUM_P (regno))
2136 {
2137 if (!strict_p)
2138 return true;
2139 regno = reg_renumber[regno];
2140 }
2141
2142 /* These fake registers will be eliminated to either the stack or
2143 hard frame pointer, both of which are usually valid base registers.
2144 Reload deals with the cases where the eliminated form isn't valid. */
2145 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2146 return true;
2147
2148 return GP_REG_P (regno);
2149 }
2150
2151 /* Return true if X is a valid base register for mode MODE.
2152 STRICT_P is true if REG_OK_STRICT is in effect. */
2153
2154 static bool
2155 loongarch_valid_base_register_p (rtx x, machine_mode mode, bool strict_p)
2156 {
2157 if (!strict_p && SUBREG_P (x))
2158 x = SUBREG_REG (x);
2159
2160 return (REG_P (x)
2161 && loongarch_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2162 }
2163
2164 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2165 can address a value of mode MODE. */
2166
2167 static bool
2168 loongarch_valid_offset_p (rtx x, machine_mode mode)
2169 {
2170 /* Check that X is a signed 12-bit number,
2171 or check that X is a signed 16-bit number
2172 and offset 4 byte aligned. */
2173 if (!(const_arith_operand (x, Pmode)
2174 || ((mode == E_SImode || mode == E_DImode)
2175 && const_imm16_operand (x, Pmode)
2176 && (loongarch_signed_immediate_p (INTVAL (x), 14, 2)))))
2177 return false;
2178
2179 /* We may need to split multiword moves, so make sure that every word
2180 is accessible. */
2181 if (!(LSX_SUPPORTED_MODE_P (mode) || LASX_SUPPORTED_MODE_P (mode))
2182 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
2183 && !IMM12_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2184 return false;
2185
2186 return true;
2187 }
2188
2189 /* Should a symbol of type SYMBOL_TYPE should be split in two or more? */
2190
2191 bool
2192 loongarch_split_symbol_type (enum loongarch_symbol_type symbol_type)
2193 {
2194 switch (symbol_type)
2195 {
2196 case SYMBOL_PCREL:
2197 case SYMBOL_PCREL64:
2198 case SYMBOL_GOT_DISP:
2199 case SYMBOL_TLS_IE:
2200 case SYMBOL_TLS_LE:
2201 case SYMBOL_TLSGD:
2202 case SYMBOL_TLSLDM:
2203 return true;
2204
2205 case SYMBOL_TLS:
2206 return false;
2207
2208 default:
2209 gcc_unreachable ();
2210 }
2211 }
2212
2213 /* Return true if a LO_SUM can address a value of mode MODE when the
2214 LO_SUM symbol has type SYMBOL_TYPE. */
2215
2216 static bool
2217 loongarch_valid_lo_sum_p (enum loongarch_symbol_type symbol_type,
2218 machine_mode mode, rtx x)
2219 {
2220 int align, size;
2221
2222 /* Check that symbols of type SYMBOL_TYPE can be used to access values
2223 of mode MODE. */
2224 if (loongarch_symbol_insns (symbol_type, mode) == 0)
2225 return false;
2226
2227 /* Check that there is a known low-part relocation. */
2228 if (!loongarch_split_symbol_type (symbol_type))
2229 return false;
2230
2231 /* We can't tell size or alignment when we have BLKmode, so try extracing a
2232 decl from the symbol if possible. */
2233 if (mode == BLKmode)
2234 {
2235 rtx offset;
2236
2237 /* Extract the symbol from the LO_SUM operand, if any. */
2238 split_const (x, &x, &offset);
2239
2240 /* Might be a CODE_LABEL. We can compute align but not size for that,
2241 so don't bother trying to handle it. */
2242 if (!SYMBOL_REF_P (x))
2243 return false;
2244
2245 /* Use worst case assumptions if we don't have a SYMBOL_REF_DECL. */
2246 align = (SYMBOL_REF_DECL (x)
2247 ? DECL_ALIGN (SYMBOL_REF_DECL (x))
2248 : 1);
2249 size = (SYMBOL_REF_DECL (x) && DECL_SIZE (SYMBOL_REF_DECL (x))
2250 ? tree_to_uhwi (DECL_SIZE (SYMBOL_REF_DECL (x)))
2251 : 2*BITS_PER_WORD);
2252 }
2253 else
2254 {
2255 align = GET_MODE_ALIGNMENT (mode);
2256 size = GET_MODE_BITSIZE (mode);
2257 }
2258
2259 /* We may need to split multiword moves, so make sure that each word
2260 can be accessed without inducing a carry. */
2261 if (size > BITS_PER_WORD
2262 && (!TARGET_STRICT_ALIGN || size > align))
2263 return false;
2264
2265 return true;
2266 }
2267
2268 static bool
2269 loongarch_valid_index_p (struct loongarch_address_info *info, rtx x,
2270 machine_mode mode, bool strict_p)
2271 {
2272 rtx index;
2273
2274 if ((REG_P (x) || SUBREG_P (x))
2275 && GET_MODE (x) == Pmode)
2276 {
2277 index = x;
2278 }
2279 else
2280 return false;
2281
2282 if (!strict_p
2283 && SUBREG_P (index)
2284 && contains_reg_of_mode[GENERAL_REGS][GET_MODE (SUBREG_REG (index))])
2285 index = SUBREG_REG (index);
2286
2287 if (loongarch_valid_base_register_p (index, mode, strict_p))
2288 {
2289 info->type = ADDRESS_REG_REG;
2290 info->offset = index;
2291 return true;
2292 }
2293
2294 return false;
2295 }
2296
2297 /* Return true if X is a valid address for machine mode MODE. If it is,
2298 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
2299 effect. */
2300
2301 static bool
2302 loongarch_classify_address (struct loongarch_address_info *info, rtx x,
2303 machine_mode mode, bool strict_p)
2304 {
2305 switch (GET_CODE (x))
2306 {
2307 case REG:
2308 case SUBREG:
2309 info->type = ADDRESS_REG;
2310 info->reg = x;
2311 info->offset = const0_rtx;
2312 return loongarch_valid_base_register_p (info->reg, mode, strict_p);
2313
2314 case PLUS:
2315 if (loongarch_valid_base_register_p (XEXP (x, 0), mode, strict_p)
2316 && loongarch_valid_index_p (info, XEXP (x, 1), mode, strict_p))
2317 {
2318 info->reg = XEXP (x, 0);
2319 return true;
2320 }
2321
2322 if (loongarch_valid_base_register_p (XEXP (x, 1), mode, strict_p)
2323 && loongarch_valid_index_p (info, XEXP (x, 0), mode, strict_p))
2324 {
2325 info->reg = XEXP (x, 1);
2326 return true;
2327 }
2328
2329 info->type = ADDRESS_REG;
2330 info->reg = XEXP (x, 0);
2331 info->offset = XEXP (x, 1);
2332 return (loongarch_valid_base_register_p (info->reg, mode, strict_p)
2333 && loongarch_valid_offset_p (info->offset, mode));
2334
2335 case LO_SUM:
2336 info->type = ADDRESS_LO_SUM;
2337 info->reg = XEXP (x, 0);
2338 info->offset = XEXP (x, 1);
2339 /* We have to trust the creator of the LO_SUM to do something vaguely
2340 sane. Target-independent code that creates a LO_SUM should also
2341 create and verify the matching HIGH. Target-independent code that
2342 adds an offset to a LO_SUM must prove that the offset will not
2343 induce a carry. Failure to do either of these things would be
2344 a bug, and we are not required to check for it here. The MIPS
2345 backend itself should only create LO_SUMs for valid symbolic
2346 constants, with the high part being either a HIGH or a copy
2347 of _gp. */
2348 info->symbol_type
2349 = loongarch_classify_symbolic_expression (info->offset);
2350 return (loongarch_valid_base_register_p (info->reg, mode, strict_p)
2351 && loongarch_valid_lo_sum_p (info->symbol_type, mode,
2352 info->offset));
2353 case CONST_INT:
2354 /* Small-integer addresses don't occur very often, but they
2355 are legitimate if $r0 is a valid base register. */
2356 info->type = ADDRESS_CONST_INT;
2357 return IMM12_OPERAND (INTVAL (x));
2358
2359 default:
2360 return false;
2361 }
2362 }
2363
2364 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
2365
2366 static bool
2367 loongarch_legitimate_address_p (machine_mode mode, rtx x, bool strict_p,
2368 code_helper = ERROR_MARK)
2369 {
2370 struct loongarch_address_info addr;
2371
2372 return loongarch_classify_address (&addr, x, mode, strict_p);
2373 }
2374
2375 /* Return true if ADDR matches the pattern for the indexed address
2376 instruction. */
2377
2378 static bool
2379 loongarch_index_address_p (rtx addr, machine_mode mode ATTRIBUTE_UNUSED)
2380 {
2381 if (GET_CODE (addr) != PLUS
2382 || !REG_P (XEXP (addr, 0))
2383 || !REG_P (XEXP (addr, 1)))
2384 return false;
2385 return true;
2386 }
2387
2388 static int
2389 loongarch_address_insns_1 (rtx x, machine_mode mode, bool might_split_p,
2390 int reg_reg_cost)
2391 {
2392 struct loongarch_address_info addr;
2393 int factor;
2394 bool lsx_p = (!might_split_p
2395 && (LSX_SUPPORTED_MODE_P (mode)
2396 || LASX_SUPPORTED_MODE_P (mode)));
2397
2398 if (!loongarch_classify_address (&addr, x, mode, false))
2399 return 0;
2400
2401 /* BLKmode is used for single unaligned loads and stores and should
2402 not count as a multiword mode. (GET_MODE_SIZE (BLKmode) is pretty
2403 meaningless, so we have to single it out as a special case one way
2404 or the other.) */
2405 if (mode != BLKmode && might_split_p)
2406 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2407 else
2408 factor = 1;
2409
2410 if (loongarch_classify_address (&addr, x, mode, false))
2411 switch (addr.type)
2412 {
2413 case ADDRESS_REG:
2414 if (lsx_p)
2415 {
2416 /* LSX LD.* and ST.* supports 12-bit signed offsets. */
2417 if (IMM12_OPERAND (INTVAL (addr.offset)))
2418 return 1;
2419 else
2420 return 0;
2421 }
2422 return factor;
2423
2424 case ADDRESS_REG_REG:
2425 return factor * reg_reg_cost;
2426
2427 case ADDRESS_CONST_INT:
2428 return lsx_p ? 0 : factor;
2429
2430 case ADDRESS_LO_SUM:
2431 return factor + 1;
2432
2433 case ADDRESS_SYMBOLIC:
2434 return lsx_p ? 0
2435 : factor * loongarch_symbol_insns (addr.symbol_type, mode);
2436 }
2437 return 0;
2438 }
2439
2440 /* Return the number of instructions needed to load or store a value
2441 of mode MODE at address X. Return 0 if X isn't valid for MODE.
2442 Assume that multiword moves may need to be split into word moves
2443 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2444 enough. */
2445
2446 int
2447 loongarch_address_insns (rtx x, machine_mode mode, bool might_split_p)
2448 {
2449 return loongarch_address_insns_1 (x, mode, might_split_p, 1);
2450 }
2451
2452 /* Return true if X fits within an unsigned field of BITS bits that is
2453 shifted left SHIFT bits before being used. */
2454
2455 bool
2456 loongarch_unsigned_immediate_p (unsigned HOST_WIDE_INT x, int bits,
2457 int shift = 0)
2458 {
2459 return (x & ((1 << shift) - 1)) == 0 && x < ((unsigned) 1 << (shift + bits));
2460 }
2461
2462 /* Return true if X fits within a signed field of BITS bits that is
2463 shifted left SHIFT bits before being used. */
2464
2465 bool
2466 loongarch_signed_immediate_p (unsigned HOST_WIDE_INT x, int bits,
2467 int shift = 0)
2468 {
2469 x += 1 << (bits + shift - 1);
2470 return loongarch_unsigned_immediate_p (x, bits, shift);
2471 }
2472
2473 /* Return the scale shift that applied to LSX LD/ST address offset. */
2474
2475 int
2476 loongarch_ldst_scaled_shift (machine_mode mode)
2477 {
2478 int shift = exact_log2 (GET_MODE_UNIT_SIZE (mode));
2479
2480 if (shift < 0 || shift > 8)
2481 gcc_unreachable ();
2482
2483 return shift;
2484 }
2485
2486 /* Return true if X is a legitimate address with a 12-bit offset
2487 or addr.type is ADDRESS_LO_SUM.
2488 MODE is the mode of the value being accessed. */
2489
2490 bool
2491 loongarch_12bit_offset_address_p (rtx x, machine_mode mode)
2492 {
2493 struct loongarch_address_info addr;
2494
2495 return (loongarch_classify_address (&addr, x, mode, false)
2496 && ((addr.type == ADDRESS_REG
2497 && CONST_INT_P (addr.offset)
2498 && LARCH_12BIT_OFFSET_P (INTVAL (addr.offset)))
2499 || addr.type == ADDRESS_LO_SUM));
2500 }
2501
2502 /* Return true if X is a legitimate address with a 14-bit offset shifted 2.
2503 MODE is the mode of the value being accessed. */
2504
2505 bool
2506 loongarch_14bit_shifted_offset_address_p (rtx x, machine_mode mode)
2507 {
2508 struct loongarch_address_info addr;
2509
2510 return (loongarch_classify_address (&addr, x, mode, false)
2511 && addr.type == ADDRESS_REG
2512 && CONST_INT_P (addr.offset)
2513 && LARCH_16BIT_OFFSET_P (INTVAL (addr.offset))
2514 && LARCH_SHIFT_2_OFFSET_P (INTVAL (addr.offset)));
2515 }
2516
2517 /* Return true if X is a legitimate address with base and index.
2518 MODE is the mode of the value being accessed. */
2519
2520 bool
2521 loongarch_base_index_address_p (rtx x, machine_mode mode)
2522 {
2523 struct loongarch_address_info addr;
2524
2525 return (loongarch_classify_address (&addr, x, mode, false)
2526 && addr.type == ADDRESS_REG_REG
2527 && REG_P (addr.offset));
2528 }
2529
2530 /* Return the number of instructions needed to load constant X,
2531 Return 0 if X isn't a valid constant. */
2532
2533 int
2534 loongarch_const_insns (rtx x)
2535 {
2536 enum loongarch_symbol_type symbol_type;
2537 rtx offset;
2538
2539 switch (GET_CODE (x))
2540 {
2541 case HIGH:
2542 if (!loongarch_symbolic_constant_p (XEXP (x, 0), &symbol_type)
2543 || !loongarch_split_symbol_type (symbol_type))
2544 return 0;
2545
2546 /* This is simply a PCALAU12I. */
2547 return 1;
2548
2549 case CONST_INT:
2550 return loongarch_integer_cost (INTVAL (x));
2551
2552 case CONST_VECTOR:
2553 if ((LSX_SUPPORTED_MODE_P (GET_MODE (x))
2554 || LASX_SUPPORTED_MODE_P (GET_MODE (x)))
2555 && loongarch_const_vector_vrepli (x, GET_MODE (x)))
2556 return 1;
2557 /* Fall through. */
2558 case CONST_DOUBLE:
2559 return x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2560
2561 case CONST:
2562 /* See if we can refer to X directly. */
2563 if (loongarch_symbolic_constant_p (x, &symbol_type))
2564 return loongarch_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2565
2566 /* Otherwise try splitting the constant into a base and offset.
2567 If the offset is a 12-bit value, we can load the base address
2568 into a register and then use ADDI.{W/D} to add in the offset.
2569 If the offset is larger, we can load the base and offset
2570 into separate registers and add them together with ADD.{W/D}.
2571 However, the latter is only possible before reload; during
2572 and after reload, we must have the option of forcing the
2573 constant into the pool instead. */
2574 split_const (x, &x, &offset);
2575 if (offset != 0)
2576 {
2577 int n = loongarch_const_insns (x);
2578 if (n != 0)
2579 {
2580 if (IMM12_INT (offset))
2581 return n + 1;
2582 else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
2583 return n + 1 + loongarch_integer_cost (INTVAL (offset));
2584 }
2585 }
2586 return 0;
2587
2588 case SYMBOL_REF:
2589 case LABEL_REF:
2590 return loongarch_symbol_insns (
2591 loongarch_classify_symbol (x), MAX_MACHINE_MODE);
2592
2593 default:
2594 return 0;
2595 }
2596 }
2597
2598 /* X is a doubleword constant that can be handled by splitting it into
2599 two words and loading each word separately. Return the number of
2600 instructions required to do this. */
2601
2602 int
2603 loongarch_split_const_insns (rtx x)
2604 {
2605 unsigned int low, high;
2606
2607 low = loongarch_const_insns (loongarch_subword (x, false));
2608 high = loongarch_const_insns (loongarch_subword (x, true));
2609 gcc_assert (low > 0 && high > 0);
2610 return low + high;
2611 }
2612
2613 /* Return one word of 128-bit value OP, taking into account the fixed
2614 endianness of certain registers. BYTE selects from the byte address. */
2615
2616 rtx
2617 loongarch_subword_at_byte (rtx op, unsigned int byte)
2618 {
2619 machine_mode mode;
2620
2621 mode = GET_MODE (op);
2622 if (mode == VOIDmode)
2623 mode = TImode;
2624
2625 gcc_assert (!FP_REG_RTX_P (op));
2626
2627 if (MEM_P (op))
2628 return loongarch_rewrite_small_data (adjust_address (op, word_mode, byte));
2629
2630 return simplify_gen_subreg (word_mode, op, mode, byte);
2631 }
2632
2633 /* Return the number of instructions needed to implement INSN,
2634 given that it loads from or stores to MEM. */
2635
2636 int
2637 loongarch_load_store_insns (rtx mem, rtx_insn *insn)
2638 {
2639 machine_mode mode;
2640 bool might_split_p;
2641 rtx set;
2642
2643 gcc_assert (MEM_P (mem));
2644 mode = GET_MODE (mem);
2645
2646 /* Try to prove that INSN does not need to be split. */
2647 might_split_p = GET_MODE_SIZE (mode) > UNITS_PER_WORD;
2648 if (might_split_p)
2649 {
2650 set = single_set (insn);
2651 if (set
2652 && !loongarch_split_move_p (SET_DEST (set), SET_SRC (set)))
2653 might_split_p = false;
2654 }
2655
2656 return loongarch_address_insns (XEXP (mem, 0), mode, might_split_p);
2657 }
2658
2659 /* Return true if we need to trap on division by zero. */
2660
2661 bool
2662 loongarch_check_zero_div_p (void)
2663 {
2664 /* if -m[no-]check-zero-division is given explicitly. */
2665 if (target_flags_explicit & MASK_CHECK_ZERO_DIV)
2666 return TARGET_CHECK_ZERO_DIV;
2667
2668 /* if not, don't trap for optimized code except -Og. */
2669 return !optimize || optimize_debug;
2670 }
2671
2672 /* Return the number of instructions needed for an integer division. */
2673
2674 int
2675 loongarch_idiv_insns (machine_mode mode ATTRIBUTE_UNUSED)
2676 {
2677 int count;
2678
2679 count = 1;
2680 if (loongarch_check_zero_div_p ())
2681 count += 2;
2682
2683 return count;
2684 }
2685
2686 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)). */
2687
2688 void
2689 loongarch_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2690 {
2691 emit_insn (gen_rtx_SET (target, gen_rtx_fmt_ee (code, GET_MODE (target),
2692 op0, op1)));
2693 }
2694
2695 /* Compute (CODE OP0 OP1) and store the result in a new register
2696 of mode MODE. Return that new register. */
2697
2698 static rtx
2699 loongarch_force_binary (machine_mode mode, enum rtx_code code, rtx op0,
2700 rtx op1)
2701 {
2702 rtx reg;
2703
2704 reg = gen_reg_rtx (mode);
2705 loongarch_emit_binary (code, reg, op0, op1);
2706 return reg;
2707 }
2708
2709 /* Copy VALUE to a register and return that register. If new pseudos
2710 are allowed, copy it into a new register, otherwise use DEST. */
2711
2712 static rtx
2713 loongarch_force_temporary (rtx dest, rtx value)
2714 {
2715 if (can_create_pseudo_p ())
2716 return force_reg (Pmode, value);
2717 else
2718 {
2719 loongarch_emit_move (dest, value);
2720 return dest;
2721 }
2722 }
2723
2724 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2725 then add CONST_INT OFFSET to the result. */
2726
2727 static rtx
2728 loongarch_unspec_address_offset (rtx base, rtx offset,
2729 enum loongarch_symbol_type symbol_type)
2730 {
2731 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2732 UNSPEC_ADDRESS_FIRST + symbol_type);
2733 if (offset != const0_rtx)
2734 base = gen_rtx_PLUS (Pmode, base, offset);
2735 return gen_rtx_CONST (Pmode, base);
2736 }
2737
2738 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2739 type SYMBOL_TYPE. */
2740
2741 rtx
2742 loongarch_unspec_address (rtx address, enum loongarch_symbol_type symbol_type)
2743 {
2744 rtx base, offset;
2745
2746 split_const (address, &base, &offset);
2747 return loongarch_unspec_address_offset (base, offset, symbol_type);
2748 }
2749
2750 /* Emit an instruction of the form (set TARGET SRC). */
2751
2752 static rtx
2753 loongarch_emit_set (rtx target, rtx src)
2754 {
2755 emit_insn (gen_rtx_SET (target, src));
2756 return target;
2757 }
2758
2759 /* If OP is an UNSPEC address, return the address to which it refers,
2760 otherwise return OP itself. */
2761
2762 rtx
2763 loongarch_strip_unspec_address (rtx op)
2764 {
2765 rtx base, offset;
2766
2767 split_const (op, &base, &offset);
2768 if (UNSPEC_ADDRESS_P (base))
2769 op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
2770 return op;
2771 }
2772
2773 /* Return a legitimate address for REG + OFFSET. TEMP is as for
2774 loongarch_force_temporary; it is only needed when OFFSET is not a
2775 IMM12_OPERAND. */
2776
2777 static rtx
2778 loongarch_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
2779 {
2780 if (!IMM12_OPERAND (offset))
2781 {
2782 rtx high;
2783
2784 /* Leave OFFSET as a 12-bit offset and put the excess in HIGH.
2785 The addition inside the macro CONST_HIGH_PART may cause an
2786 overflow, so we need to force a sign-extension check. */
2787 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
2788 offset = CONST_LOW_PART (offset);
2789 high = loongarch_force_temporary (temp, high);
2790 reg = loongarch_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
2791 }
2792 return plus_constant (Pmode, reg, offset);
2793 }
2794
2795 /* The __tls_get_addr symbol. */
2796 static GTY (()) rtx loongarch_tls_symbol;
2797
2798 /* Load an entry for a TLS access. */
2799
2800 static rtx
2801 loongarch_load_tls (rtx dest, rtx sym, enum loongarch_symbol_type type)
2802 {
2803 /* TLS LE gets a 32 or 64 bit offset here, so one register can do it. */
2804 if (type == SYMBOL_TLS_LE)
2805 return gen_load_tls (Pmode, dest, sym);
2806
2807 return loongarch_symbol_extreme_p (type)
2808 ? gen_movdi_symbolic_off64 (dest, sym, gen_reg_rtx (DImode))
2809 : gen_load_tls (Pmode, dest, sym);
2810 }
2811
2812 /* Return an instruction sequence that calls __tls_get_addr. SYM is
2813 the TLS symbol we are referencing and TYPE is the symbol type to use
2814 (either global dynamic or local dynamic). V0 is an RTX for the
2815 return value location. */
2816
2817 static rtx_insn *
2818 loongarch_call_tls_get_addr (rtx sym, enum loongarch_symbol_type type, rtx v0)
2819 {
2820 rtx loc, a0;
2821 rtx_insn *insn;
2822 rtx tmp = gen_reg_rtx (Pmode);
2823
2824 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
2825
2826 if (!loongarch_tls_symbol)
2827 loongarch_tls_symbol = init_one_libfunc ("__tls_get_addr");
2828
2829 loc = loongarch_unspec_address (sym, type);
2830
2831 start_sequence ();
2832
2833 if (loongarch_explicit_relocs_p (type))
2834 {
2835 if (TARGET_CMODEL_EXTREME)
2836 {
2837 rtx part1 = gen_reg_rtx (Pmode);
2838 rtx part2 = gen_reg_rtx (Pmode);
2839
2840 emit_insn (gen_la_pcrel64_two_parts (part1, part2, loc));
2841 emit_move_insn (a0, gen_rtx_PLUS (Pmode, part1, part2));
2842 }
2843 else
2844 {
2845 /* Split tls symbol to high and low. */
2846 rtx high = gen_rtx_HIGH (Pmode, copy_rtx (loc));
2847
2848 high = loongarch_force_temporary (tmp, high);
2849 emit_insn (gen_tls_low (Pmode, a0, high, loc));
2850 }
2851 }
2852 else
2853 emit_insn (loongarch_load_tls (a0, loc, type));
2854
2855 if (flag_plt)
2856 {
2857 switch (la_target.cmodel)
2858 {
2859 case CMODEL_NORMAL:
2860 insn = emit_call_insn (gen_call_value_internal (v0,
2861 loongarch_tls_symbol,
2862 const0_rtx));
2863 break;
2864
2865 case CMODEL_MEDIUM:
2866 {
2867 if (la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE)
2868 {
2869 rtx call;
2870
2871 if (HAVE_AS_SUPPORT_CALL36)
2872 call = gen_call_value_internal (v0, loongarch_tls_symbol,
2873 const0_rtx);
2874 else
2875 {
2876 rtx reg = gen_reg_rtx (Pmode);
2877 emit_insn (gen_pcalau12i (Pmode, reg,
2878 loongarch_tls_symbol));
2879 call = gen_call_value_internal_1 (Pmode, v0, reg,
2880 loongarch_tls_symbol,
2881 const0_rtx);
2882 }
2883 insn = emit_call_insn (call);
2884 }
2885 else
2886 {
2887 rtx reg = gen_reg_rtx (Pmode);
2888 emit_move_insn (reg, loongarch_tls_symbol);
2889 insn = emit_call_insn (gen_call_value_internal (v0,
2890 reg,
2891 const0_rtx));
2892 }
2893 break;
2894 }
2895
2896 /* code model extreme not support plt. */
2897 case CMODEL_EXTREME:
2898 case CMODEL_LARGE:
2899 case CMODEL_TINY:
2900 case CMODEL_TINY_STATIC:
2901 default:
2902 gcc_unreachable ();
2903 }
2904 }
2905 else
2906 {
2907 rtx dest = gen_reg_rtx (Pmode);
2908
2909 switch (la_target.cmodel)
2910 {
2911 case CMODEL_NORMAL:
2912 case CMODEL_MEDIUM:
2913 {
2914 if (loongarch_explicit_relocs_p (SYMBOL_GOT_DISP))
2915 {
2916 rtx high = gen_reg_rtx (Pmode);
2917 loongarch_emit_move (high,
2918 gen_rtx_HIGH (Pmode,
2919 loongarch_tls_symbol));
2920 emit_insn (gen_ld_from_got (Pmode, dest, high,
2921 loongarch_tls_symbol));
2922 }
2923 else
2924 loongarch_emit_move (dest, loongarch_tls_symbol);
2925 break;
2926 }
2927
2928 case CMODEL_EXTREME:
2929 {
2930 if (loongarch_explicit_relocs_p (SYMBOL_GOT_DISP))
2931 {
2932 gcc_assert (la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE);
2933
2934 rtx part1 = gen_reg_rtx (Pmode);
2935 rtx part2 = gen_reg_rtx (Pmode);
2936
2937 emit_insn (gen_la_pcrel64_two_parts (part1, part2,
2938 loongarch_tls_symbol));
2939 loongarch_emit_move (
2940 dest,
2941 gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode,
2942 part1,
2943 part2)));
2944
2945 /* Put an REG_EQUAL note here to allow CSE (storing
2946 part1 + part2, i.e. the address of tls_get_addr into
2947 a saved register and use it for multiple TLS
2948 accesses). */
2949 rtx sum = gen_rtx_UNSPEC (
2950 Pmode, gen_rtvec (1, loongarch_tls_symbol),
2951 UNSPEC_ADDRESS_FIRST
2952 + loongarch_classify_symbol (loongarch_tls_symbol));
2953 set_unique_reg_note (get_last_insn (), REG_EQUAL, sum);
2954 }
2955 else
2956 emit_insn (gen_movdi_symbolic_off64 (dest, loongarch_tls_symbol,
2957 gen_reg_rtx (DImode)));
2958 }
2959 break;
2960
2961 case CMODEL_LARGE:
2962 case CMODEL_TINY:
2963 case CMODEL_TINY_STATIC:
2964 default:
2965 gcc_unreachable ();
2966 }
2967
2968 insn = emit_call_insn (gen_call_value_internal (v0, dest, const0_rtx));
2969 }
2970
2971 RTL_CONST_CALL_P (insn) = 1;
2972 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
2973 insn = get_insns ();
2974
2975 end_sequence ();
2976
2977 return insn;
2978 }
2979
2980 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
2981 its address. The return value will be both a valid address and a valid
2982 SET_SRC (either a REG or a LO_SUM). */
2983
2984 static rtx
2985 loongarch_legitimize_tls_address (rtx loc)
2986 {
2987 rtx dest, tp, tmp, tmp1, tmp2, tmp3, a0;
2988 enum tls_model model = SYMBOL_REF_TLS_MODEL (loc);
2989 rtx_insn *insn;
2990
2991 switch (model)
2992 {
2993 case TLS_MODEL_LOCAL_DYNAMIC:
2994 if (!TARGET_TLS_DESC)
2995 {
2996 tmp = gen_rtx_REG (Pmode, GP_RETURN);
2997 dest = gen_reg_rtx (Pmode);
2998 insn = loongarch_call_tls_get_addr (loc, SYMBOL_TLSLDM, tmp);
2999 emit_libcall_block (insn, dest, tmp, loc);
3000 break;
3001 }
3002 /* Fall through. */
3003 case TLS_MODEL_GLOBAL_DYNAMIC:
3004 if (TARGET_TLS_DESC)
3005 {
3006 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3007 dest = gen_reg_rtx (Pmode);
3008 tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
3009
3010 if (TARGET_CMODEL_EXTREME)
3011 emit_insn (gen_got_load_tls_desc_off64 (loc, gen_reg_rtx (DImode)));
3012 else
3013 emit_insn (gen_got_load_tls_desc (Pmode, loc));
3014
3015 emit_insn (gen_add3_insn (dest, a0, tp));
3016 }
3017 else
3018 {
3019 tmp = gen_rtx_REG (Pmode, GP_RETURN);
3020 dest = gen_reg_rtx (Pmode);
3021 insn = loongarch_call_tls_get_addr (loc, SYMBOL_TLSGD, tmp);
3022 emit_libcall_block (insn, dest, tmp, loc);
3023 }
3024 break;
3025
3026 case TLS_MODEL_INITIAL_EXEC:
3027 {
3028 /* la.tls.ie; tp-relative add. */
3029 tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
3030 tmp1 = gen_reg_rtx (Pmode);
3031 tmp2 = loongarch_unspec_address (loc, SYMBOL_TLS_IE);
3032 dest = gen_reg_rtx (Pmode);
3033 if (loongarch_explicit_relocs_p (SYMBOL_TLS_IE))
3034 {
3035 if (TARGET_CMODEL_EXTREME)
3036 {
3037 gcc_assert (la_opt_explicit_relocs
3038 != EXPLICIT_RELOCS_NONE);
3039
3040 rtx part1 = gen_reg_rtx (Pmode);
3041 rtx part2 = gen_reg_rtx (Pmode);
3042
3043 emit_insn (gen_la_pcrel64_two_parts (part1, part2,
3044 tmp2));
3045 emit_move_insn (tmp1,
3046 gen_rtx_MEM (Pmode,
3047 gen_rtx_PLUS (Pmode,
3048 part1,
3049 part2)));
3050 }
3051 else
3052 {
3053 tmp3 = gen_reg_rtx (Pmode);
3054 rtx high = gen_rtx_HIGH (Pmode, copy_rtx (tmp2));
3055
3056 high = loongarch_force_temporary (tmp3, high);
3057 emit_insn (gen_ld_from_got (Pmode, tmp1, high, tmp2));
3058 }
3059 }
3060 else
3061 emit_insn (loongarch_load_tls (tmp1, tmp2, SYMBOL_TLS_IE));
3062 emit_insn (gen_add3_insn (dest, tmp1, tp));
3063 }
3064 break;
3065
3066 case TLS_MODEL_LOCAL_EXEC:
3067 {
3068 /* la.tls.le; tp-relative add.
3069
3070 normal:
3071 lu12i.w $rd, %le_hi20(sym)
3072 ori $rd, $rd, %le_lo12(sym)
3073 add.{w/d} $rd, $rd, $tp
3074 (st.{w/d}/ld.{w/d} $rs, $rd, 0)
3075
3076 tls le relax:
3077 lu12i.w $rd, %le_hi20_r(sym)
3078 add.{w/d} $rd,$rd,$tp
3079 addi.{w/d} $rd,$rd,%le_lo12_r(sym)
3080 (st.{w/d}/ld.{w/d} $rs, $rd, 0)
3081
3082 extreme (When the code model is set to extreme, the TLS le Relax
3083 instruction sequence is not generated):
3084 lu12i.w $rd, %le_hi20(sym)
3085 ori $rd, $rd, %le_lo12(sym)
3086 lu32i.d $rd, %le64_lo20(sym)
3087 lu52i.d $rd, $rd, %le64_hi12(sym)
3088 add.d $rd, $rd, $tp
3089 (st.{w/d}/ld.{w/d} $rs, $rd, 0) */
3090
3091 tp = gen_rtx_REG (Pmode, THREAD_POINTER_REGNUM);
3092 tmp1 = gen_reg_rtx (Pmode);
3093 tmp2 = loongarch_unspec_address (loc, SYMBOL_TLS_LE);
3094 dest = gen_reg_rtx (Pmode);
3095
3096 if (loongarch_explicit_relocs_p (SYMBOL_TLS_LE))
3097 {
3098 tmp3 = gen_reg_rtx (Pmode);
3099 rtx high = gen_rtx_HIGH (Pmode, copy_rtx (tmp2));
3100 high = loongarch_force_temporary (tmp3, high);
3101
3102 /* The assembler does not implement tls le relax support when the
3103 code model is extreme, so when the code model is extreme, the
3104 old symbol address acquisition method is still used. */
3105 if (HAVE_AS_TLS_LE_RELAXATION && !TARGET_CMODEL_EXTREME)
3106 {
3107 emit_insn (gen_add_tls_le_relax (Pmode, dest, high,
3108 tp, loc));
3109 loongarch_emit_move (dest,
3110 gen_rtx_LO_SUM (Pmode, dest, tmp2));
3111 return dest;
3112 }
3113 else
3114 emit_insn (gen_ori_l_lo12 (Pmode, tmp1, high, tmp2));
3115
3116 if (TARGET_CMODEL_EXTREME)
3117 {
3118 emit_insn (gen_lui_h_lo20 (tmp1, tmp1, tmp2));
3119 emit_insn (gen_lui_h_hi12 (tmp1, tmp1, tmp2));
3120 }
3121 }
3122 else
3123 emit_insn (loongarch_load_tls (tmp1, tmp2, SYMBOL_TLS_LE));
3124 emit_insn (gen_add3_insn (dest, tmp1, tp));
3125 }
3126 break;
3127
3128 default:
3129 gcc_unreachable ();
3130 }
3131 return dest;
3132 }
3133
3134 rtx
3135 loongarch_legitimize_call_address (rtx addr)
3136 {
3137 if (!call_insn_operand (addr, VOIDmode))
3138 {
3139 rtx reg = gen_reg_rtx (Pmode);
3140 loongarch_emit_move (reg, addr);
3141 return reg;
3142 }
3143
3144 enum loongarch_symbol_type symbol_type = loongarch_classify_symbol (addr);
3145
3146 /* If add the compilation option '-cmodel=medium', and the assembler does
3147 not support call36. The following sequence of instructions will be
3148 used for the function call:
3149 pcalau12i $rd, %pc_hi20(sym)
3150 jr $rd, %pc_lo12(sym)
3151 */
3152
3153 if (TARGET_CMODEL_MEDIUM
3154 && !HAVE_AS_SUPPORT_CALL36
3155 && (la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE)
3156 && (SYMBOL_REF_P (addr) || LABEL_REF_P (addr))
3157 && (symbol_type == SYMBOL_PCREL
3158 || (symbol_type == SYMBOL_GOT_DISP && flag_plt)))
3159 {
3160 rtx reg = gen_reg_rtx (Pmode);
3161 emit_insn (gen_pcalau12i (Pmode, reg, addr));
3162 return gen_rtx_LO_SUM (Pmode, reg, addr);
3163 }
3164
3165 return addr;
3166 }
3167
3168 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
3169 and *OFFSET_PTR. Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise. */
3170
3171 static void
3172 loongarch_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
3173 {
3174 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
3175 {
3176 *base_ptr = XEXP (x, 0);
3177 *offset_ptr = INTVAL (XEXP (x, 1));
3178 }
3179 else
3180 {
3181 *base_ptr = x;
3182 *offset_ptr = 0;
3183 }
3184 }
3185
3186 /* If X is not a valid address for mode MODE, force it into a register. */
3187
3188 static rtx
3189 loongarch_force_address (rtx x, machine_mode mode)
3190 {
3191 if (!loongarch_legitimate_address_p (mode, x, false))
3192 x = force_reg (Pmode, x);
3193 return x;
3194 }
3195
3196 bool
3197 loongarch_symbol_extreme_p (enum loongarch_symbol_type type)
3198 {
3199 switch (type)
3200 {
3201 case SYMBOL_PCREL:
3202 return false;
3203 case SYMBOL_PCREL64:
3204 return true;
3205 default:
3206 return TARGET_CMODEL_EXTREME;
3207 }
3208 }
3209
3210 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
3211 it appears in a MEM of that mode. Return true if ADDR is a legitimate
3212 constant in that context and can be split into high and low parts.
3213 If so, and if LOW_OUT is nonnull, emit the high part and store the
3214 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
3215
3216 Return false if build with '-mexplicit-relocs=none'.
3217
3218 TEMP is as for loongarch_force_temporary and is used to load the high
3219 part into a register.
3220
3221 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
3222 a legitimize SET_SRC for an .md pattern, otherwise the low part
3223 is guaranteed to be a legitimate address for mode MODE. */
3224
3225 bool
3226 loongarch_split_symbol (rtx temp, rtx addr, machine_mode mode, rtx *low_out)
3227 {
3228 enum loongarch_symbol_type symbol_type;
3229
3230 if ((GET_CODE (addr) == HIGH && mode == MAX_MACHINE_MODE)
3231 || !loongarch_symbolic_constant_p (addr, &symbol_type)
3232 || !loongarch_explicit_relocs_p (symbol_type)
3233 || loongarch_symbol_insns (symbol_type, mode) == 0
3234 || !loongarch_split_symbol_type (symbol_type))
3235 return false;
3236
3237 rtx high;
3238
3239 if (temp == NULL)
3240 temp = gen_reg_rtx (Pmode);
3241
3242 if (loongarch_symbol_extreme_p (symbol_type) && can_create_pseudo_p ())
3243 {
3244 gcc_assert (la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE);
3245
3246 high = gen_reg_rtx (Pmode);
3247 emit_insn (gen_la_pcrel64_two_parts (high, temp, addr));
3248 }
3249 else
3250 {
3251 /* Get the 12-31 bits of the address. */
3252 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
3253 high = loongarch_force_temporary (temp, high);
3254 }
3255
3256 if (low_out)
3257 switch (symbol_type)
3258 {
3259 case SYMBOL_PCREL64:
3260 if (can_create_pseudo_p ())
3261 {
3262 *low_out = gen_rtx_PLUS (Pmode, high, temp);
3263 break;
3264 }
3265 /* fall through */
3266 case SYMBOL_PCREL:
3267 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3268 break;
3269
3270 case SYMBOL_GOT_DISP:
3271 /* SYMBOL_GOT_DISP symbols are loaded from the GOT. */
3272 {
3273 if (TARGET_CMODEL_EXTREME && can_create_pseudo_p ())
3274 *low_out = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, high,
3275 temp));
3276 else
3277 {
3278 rtx low = gen_rtx_LO_SUM (Pmode, high, addr);
3279 rtx mem = gen_rtx_MEM (Pmode, low);
3280 *low_out = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, mem),
3281 UNSPEC_LOAD_FROM_GOT);
3282
3283 /* Nonzero in a mem, if the memory is statically allocated and
3284 read-only. A common example of the later is a shared library’s
3285 global offset table. */
3286 MEM_READONLY_P (mem) = 1;
3287 }
3288
3289 break;
3290 }
3291
3292 default:
3293 gcc_unreachable ();
3294 }
3295
3296 return true;
3297 }
3298
3299 /* Helper loongarch_legitimize_address. Given X, return true if it
3300 is a left shift by 1, 2 or 3 positions or a multiply by 2, 4 or 8.
3301
3302 This respectively represent canonical shift-add rtxs or scaled
3303 memory addresses. */
3304 static bool
3305 mem_shadd_or_shadd_rtx_p (rtx x)
3306 {
3307 return ((GET_CODE (x) == ASHIFT
3308 || GET_CODE (x) == MULT)
3309 && CONST_INT_P (XEXP (x, 1))
3310 && ((GET_CODE (x) == ASHIFT && IN_RANGE (INTVAL (XEXP (x, 1)), 1, 3))
3311 || (GET_CODE (x) == MULT
3312 && IN_RANGE (exact_log2 (INTVAL (XEXP (x, 1))), 1, 3))));
3313 }
3314
3315 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
3316 be legitimized in a way that the generic machinery might not expect,
3317 return a new address, otherwise return NULL. MODE is the mode of
3318 the memory being accessed. */
3319
3320 static rtx
3321 loongarch_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3322 machine_mode mode)
3323 {
3324 rtx base, addr;
3325 HOST_WIDE_INT offset;
3326
3327 if (loongarch_tls_symbol_p (x))
3328 return loongarch_legitimize_tls_address (x);
3329
3330 /* See if the address can split into a high part and a LO_SUM. */
3331 if (loongarch_split_symbol (NULL, x, mode, &addr))
3332 return loongarch_force_address (addr, mode);
3333
3334 /* Handle BASE + OFFSET using loongarch_add_offset. */
3335 loongarch_split_plus (x, &base, &offset);
3336 if (offset != 0)
3337 {
3338 /* Handle (plus (plus (mult (a) (mem_shadd_constant)) (fp)) (C)) case. */
3339 if (GET_CODE (base) == PLUS && mem_shadd_or_shadd_rtx_p (XEXP (base, 0))
3340 && IMM12_OPERAND (offset))
3341 {
3342 rtx index = XEXP (base, 0);
3343 rtx fp = XEXP (base, 1);
3344
3345 if (REG_P (fp) && REGNO (fp) == VIRTUAL_STACK_VARS_REGNUM)
3346 {
3347 /* If we were given a MULT, we must fix the constant
3348 as we're going to create the ASHIFT form. */
3349 int shift_val = INTVAL (XEXP (index, 1));
3350 if (GET_CODE (index) == MULT)
3351 shift_val = exact_log2 (shift_val);
3352
3353 rtx reg1 = gen_reg_rtx (Pmode);
3354 rtx reg3 = gen_reg_rtx (Pmode);
3355 loongarch_emit_binary (PLUS, reg1, fp, GEN_INT (offset));
3356 loongarch_emit_binary (PLUS, reg3,
3357 gen_rtx_ASHIFT (Pmode, XEXP (index, 0),
3358 GEN_INT (shift_val)),
3359 reg1);
3360
3361 return reg3;
3362 }
3363 }
3364
3365 if (!loongarch_valid_base_register_p (base, mode, false))
3366 base = copy_to_mode_reg (Pmode, base);
3367 addr = loongarch_add_offset (NULL, base, offset);
3368 return loongarch_force_address (addr, mode);
3369 }
3370
3371 return x;
3372 }
3373
3374 /* Load VALUE into DEST. TEMP is as for loongarch_force_temporary. */
3375
3376 void
3377 loongarch_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3378 {
3379 struct loongarch_integer_op codes[LARCH_MAX_INTEGER_OPS];
3380 machine_mode mode;
3381 unsigned int i, num_ops;
3382 rtx x;
3383
3384 mode = GET_MODE (dest);
3385 num_ops = loongarch_build_integer (codes, value);
3386
3387 /* Apply each binary operation to X. Invariant: X is a legitimate
3388 source operand for a SET pattern. */
3389 x = GEN_INT (codes[0].value);
3390 for (i = 1; i < num_ops; i++)
3391 {
3392 if (!can_create_pseudo_p ())
3393 {
3394 emit_insn (gen_rtx_SET (temp, x));
3395 x = temp;
3396 }
3397 else
3398 x = force_reg (mode, x);
3399
3400 set_unique_reg_note (get_last_insn (), REG_EQUAL,
3401 GEN_INT (codes[i-1].curr_value));
3402
3403 switch (codes[i].method)
3404 {
3405 case METHOD_NORMAL:
3406 x = gen_rtx_fmt_ee (codes[i].code, mode, x,
3407 GEN_INT (codes[i].value));
3408 break;
3409 case METHOD_LU32I:
3410 gcc_assert (mode == DImode);
3411 x = gen_rtx_IOR (DImode,
3412 gen_rtx_ZERO_EXTEND (DImode,
3413 gen_rtx_SUBREG (SImode, x, 0)),
3414 GEN_INT (codes[i].value));
3415 break;
3416 case METHOD_LU52I:
3417 gcc_assert (mode == DImode);
3418 x = gen_rtx_IOR (DImode,
3419 gen_rtx_AND (DImode, x, GEN_INT (0xfffffffffffff)),
3420 GEN_INT (codes[i].value));
3421 break;
3422 case METHOD_MIRROR:
3423 gcc_assert (mode == DImode);
3424 emit_insn (gen_insvdi (x, GEN_INT (32), GEN_INT (32), x));
3425 break;
3426 default:
3427 gcc_unreachable ();
3428 }
3429 }
3430
3431 emit_insn (gen_rtx_SET (dest, x));
3432 }
3433
3434 /* Subroutine of loongarch_legitimize_move. Move constant SRC into register
3435 DEST given that SRC satisfies immediate_operand but doesn't satisfy
3436 move_operand. */
3437
3438 static void
3439 loongarch_legitimize_const_move (machine_mode mode, rtx dest, rtx src)
3440 {
3441 rtx base, offset;
3442
3443 /* Split moves of big integers into smaller pieces. */
3444 if (splittable_const_int_operand (src, mode))
3445 {
3446 loongarch_move_integer (dest, dest, INTVAL (src));
3447 return;
3448 }
3449
3450 /* Split moves of symbolic constants into high and low. */
3451 if (loongarch_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3452 {
3453 loongarch_emit_set (dest, src);
3454 return;
3455 }
3456
3457 /* Generate the appropriate access sequences for TLS symbols. */
3458 if (loongarch_tls_symbol_p (src))
3459 {
3460 loongarch_emit_move (dest, loongarch_legitimize_tls_address (src));
3461 return;
3462 }
3463
3464 /* If we have (const (plus symbol offset)), and that expression cannot
3465 be forced into memory, load the symbol first and add in the offset.
3466 prefer to do this even if the constant _can_ be forced into memory,
3467 as it usually produces better code. */
3468 split_const (src, &base, &offset);
3469 if (offset != const0_rtx
3470 && (targetm.cannot_force_const_mem (mode, src)
3471 || (can_create_pseudo_p ())))
3472 {
3473 base = loongarch_force_temporary (dest, base);
3474 loongarch_emit_move (dest,
3475 loongarch_add_offset (NULL, base, INTVAL (offset)));
3476 return;
3477 }
3478
3479 src = force_const_mem (mode, src);
3480
3481 loongarch_emit_move (dest, src);
3482 }
3483
3484 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3485 sequence that is valid. */
3486
3487 bool
3488 loongarch_legitimize_move (machine_mode mode, rtx dest, rtx src)
3489 {
3490 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3491 {
3492 loongarch_emit_move (dest, force_reg (mode, src));
3493 return true;
3494 }
3495
3496 /* Both src and dest are non-registers; one special case is supported where
3497 the source is (const_int 0) and the store can source the zero register.
3498 LSX and LASX are never able to source the zero register directly in
3499 memory operations. */
3500 if (!register_operand (dest, mode) && !register_operand (src, mode)
3501 && (!const_0_operand (src, mode)
3502 || LSX_SUPPORTED_MODE_P (mode) || LASX_SUPPORTED_MODE_P (mode)))
3503 {
3504 loongarch_emit_move (dest, force_reg (mode, src));
3505 return true;
3506 }
3507
3508 /* We need to deal with constants that would be legitimate
3509 immediate_operands but aren't legitimate move_operands. */
3510 if (CONSTANT_P (src) && !move_operand (src, mode))
3511 {
3512 loongarch_legitimize_const_move (mode, dest, src);
3513 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3514 return true;
3515 }
3516
3517 /* Obtain the address of the symbol through the macro instruction
3518 of two registers. */
3519 enum loongarch_symbol_type symbol_type;
3520 if (TARGET_64BIT && register_operand (dest, mode)
3521 && loongarch_symbolic_constant_p (src, &symbol_type)
3522 && loongarch_symbol_extreme_p (symbol_type))
3523 {
3524 gcc_assert (can_create_pseudo_p ());
3525 rtx tmp_reg = gen_reg_rtx (DImode);
3526 emit_insn (gen_movdi_symbolic_off64 (dest, src, tmp_reg));
3527 set_unique_reg_note (get_last_insn (), REG_UNUSED, tmp_reg);
3528 set_unique_reg_note (get_last_insn (), REG_EQUAL, src);
3529 return true;
3530 }
3531
3532 return false;
3533 }
3534
3535 /* Return true if OP refers to small data symbols directly. */
3536
3537 static int
3538 loongarch_small_data_pattern_1 (rtx x)
3539 {
3540 subrtx_var_iterator::array_type array;
3541 FOR_EACH_SUBRTX_VAR (iter, array, x, ALL)
3542 {
3543 rtx x = *iter;
3544
3545 /* We make no particular guarantee about which symbolic constants are
3546 acceptable as asm operands versus which must be forced into a GPR. */
3547 if (GET_CODE (x) == ASM_OPERANDS)
3548 iter.skip_subrtxes ();
3549 else if (MEM_P (x))
3550 {
3551 if (loongarch_small_data_pattern_1 (XEXP (x, 0)))
3552 return true;
3553 iter.skip_subrtxes ();
3554 }
3555 }
3556 return false;
3557 }
3558
3559 /* Return true if OP refers to small data symbols directly. */
3560
3561 bool
3562 loongarch_small_data_pattern_p (rtx op)
3563 {
3564 return loongarch_small_data_pattern_1 (op);
3565 }
3566
3567 /* Rewrite *LOC so that it refers to small data using explicit
3568 relocations. */
3569
3570 static void
3571 loongarch_rewrite_small_data_1 (rtx *loc)
3572 {
3573 subrtx_ptr_iterator::array_type array;
3574 FOR_EACH_SUBRTX_PTR (iter, array, loc, ALL)
3575 {
3576 rtx *loc = *iter;
3577 if (MEM_P (*loc))
3578 {
3579 loongarch_rewrite_small_data_1 (&XEXP (*loc, 0));
3580 iter.skip_subrtxes ();
3581 }
3582 }
3583 }
3584
3585 /* Rewrite instruction pattern PATTERN so that it refers to small data
3586 using explicit relocations. */
3587
3588 rtx
3589 loongarch_rewrite_small_data (rtx pattern)
3590 {
3591 pattern = copy_insn (pattern);
3592 loongarch_rewrite_small_data_1 (&pattern);
3593 return pattern;
3594 }
3595
3596 /* The cost of loading values from the constant pool. It should be
3597 larger than the cost of any constant we want to synthesize inline. */
3598 #define CONSTANT_POOL_COST COSTS_N_INSNS (8)
3599
3600 /* Return true if there is a instruction that implements CODE
3601 and if that instruction accepts X as an immediate operand. */
3602
3603 static int
3604 loongarch_immediate_operand_p (int code, HOST_WIDE_INT x)
3605 {
3606 switch (code)
3607 {
3608 case ASHIFT:
3609 case ASHIFTRT:
3610 case LSHIFTRT:
3611 /* All shift counts are truncated to a valid constant. */
3612 return true;
3613
3614 case ROTATE:
3615 case ROTATERT:
3616 return true;
3617
3618 case AND:
3619 case IOR:
3620 case XOR:
3621 /* These instructions take 12-bit unsigned immediates. */
3622 return IMM12_OPERAND_UNSIGNED (x);
3623
3624 case PLUS:
3625 case LT:
3626 case LTU:
3627 /* These instructions take 12-bit signed immediates. */
3628 return IMM12_OPERAND (x);
3629
3630 case EQ:
3631 case NE:
3632 case GT:
3633 case GTU:
3634 /* The "immediate" forms of these instructions are really
3635 implemented as comparisons with register 0. */
3636 return x == 0;
3637
3638 case GE:
3639 case GEU:
3640 /* Likewise, meaning that the only valid immediate operand is 1. */
3641 return x == 1;
3642
3643 case LE:
3644 /* We add 1 to the immediate and use SLT. */
3645 return IMM12_OPERAND (x + 1);
3646
3647 case LEU:
3648 /* Likewise SLTU, but reject the always-true case. */
3649 return IMM12_OPERAND (x + 1) && x + 1 != 0;
3650
3651 case SIGN_EXTRACT:
3652 case ZERO_EXTRACT:
3653 /* The bit position and size are immediate operands. */
3654 return 1;
3655
3656 default:
3657 /* By default assume that $0 can be used for 0. */
3658 return x == 0;
3659 }
3660 }
3661
3662 /* Return the cost of binary operation X, given that the instruction
3663 sequence for a word-sized or smaller operation has cost SINGLE_COST
3664 and that the sequence of a double-word operation has cost DOUBLE_COST.
3665 If SPEED is true, optimize for speed otherwise optimize for size. */
3666
3667 static int
3668 loongarch_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3669 {
3670 int cost;
3671
3672 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3673 cost = double_cost;
3674 else
3675 cost = single_cost;
3676 return (cost
3677 + set_src_cost (XEXP (x, 0), GET_MODE (x), speed)
3678 + rtx_cost (XEXP (x, 1), GET_MODE (x), GET_CODE (x), 1, speed));
3679 }
3680
3681 /* Return the cost of floating-point multiplications of mode MODE. */
3682
3683 static int
3684 loongarch_fp_mult_cost (machine_mode mode)
3685 {
3686 return mode == DFmode ? loongarch_cost->fp_mult_df
3687 : loongarch_cost->fp_mult_sf;
3688 }
3689
3690 /* Return the cost of floating-point divisions of mode MODE. */
3691
3692 static int
3693 loongarch_fp_div_cost (machine_mode mode)
3694 {
3695 return mode == DFmode ? loongarch_cost->fp_div_df
3696 : loongarch_cost->fp_div_sf;
3697 }
3698
3699 /* Return the cost of sign-extending OP to mode MODE, not including the
3700 cost of OP itself. */
3701
3702 static int
3703 loongarch_sign_extend_cost (rtx op)
3704 {
3705 if (MEM_P (op))
3706 /* Extended loads are as cheap as unextended ones. */
3707 return 0;
3708
3709 return COSTS_N_INSNS (1);
3710 }
3711
3712 /* Return the cost of zero-extending OP to mode MODE, not including the
3713 cost of OP itself. */
3714
3715 static int
3716 loongarch_zero_extend_cost (rtx op)
3717 {
3718 if (MEM_P (op))
3719 /* Extended loads are as cheap as unextended ones. */
3720 return 0;
3721
3722 /* We can use ANDI. */
3723 return COSTS_N_INSNS (1);
3724 }
3725
3726 /* Return the cost of moving between two registers of mode MODE,
3727 assuming that the move will be in pieces of at most UNITS bytes. */
3728
3729 static int
3730 loongarch_set_reg_reg_piece_cost (machine_mode mode, unsigned int units)
3731 {
3732 return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
3733 }
3734
3735 static int
3736 loongarch_use_bstrins_for_ior_with_mask_1 (machine_mode mode,
3737 unsigned HOST_WIDE_INT mask1,
3738 unsigned HOST_WIDE_INT mask2)
3739 {
3740 if (mask1 != ~mask2 || !mask1 || !mask2)
3741 return 0;
3742
3743 /* Try to avoid a right-shift. */
3744 if (low_bitmask_len (mode, mask1) != -1)
3745 return -1;
3746
3747 if (low_bitmask_len (mode, mask2 >> (ffs_hwi (mask2) - 1)) != -1)
3748 return 1;
3749
3750 if (low_bitmask_len (mode, mask1 >> (ffs_hwi (mask1) - 1)) != -1)
3751 return -1;
3752
3753 return 0;
3754 }
3755
3756 /* Return the cost of moving between two registers of mode MODE. */
3757
3758 static int
3759 loongarch_set_reg_reg_cost (machine_mode mode)
3760 {
3761 switch (GET_MODE_CLASS (mode))
3762 {
3763 case MODE_CC:
3764 return loongarch_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
3765
3766 case MODE_FLOAT:
3767 case MODE_COMPLEX_FLOAT:
3768 case MODE_VECTOR_FLOAT:
3769 if (TARGET_HARD_FLOAT)
3770 return loongarch_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
3771 /* Fall through. */
3772
3773 default:
3774 return loongarch_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
3775 }
3776 }
3777
3778 /* Implement TARGET_ADDRESS_COST. */
3779
3780 static int
3781 loongarch_address_cost (rtx addr, machine_mode mode,
3782 addr_space_t as ATTRIBUTE_UNUSED,
3783 bool speed ATTRIBUTE_UNUSED)
3784 {
3785 return loongarch_address_insns_1 (addr, mode, false,
3786 la_addr_reg_reg_cost);
3787 }
3788
3789 /* Implement TARGET_RTX_COSTS. */
3790
3791 static bool
3792 loongarch_rtx_costs (rtx x, machine_mode mode, int outer_code,
3793 int opno ATTRIBUTE_UNUSED, int *total, bool speed)
3794 {
3795 int code = GET_CODE (x);
3796 bool float_mode_p = FLOAT_MODE_P (mode);
3797 int cost;
3798 rtx addr;
3799
3800 if (outer_code == COMPARE)
3801 {
3802 gcc_assert (CONSTANT_P (x));
3803 *total = 0;
3804 return true;
3805 }
3806
3807 switch (code)
3808 {
3809 case CONST_INT:
3810 if (TARGET_64BIT && outer_code == AND && UINTVAL (x) == 0xffffffff)
3811 {
3812 *total = 0;
3813 return true;
3814 }
3815
3816 /* When not optimizing for size, we care more about the cost
3817 of hot code, and hot code is often in a loop. If a constant
3818 operand needs to be forced into a register, we will often be
3819 able to hoist the constant load out of the loop, so the load
3820 should not contribute to the cost. */
3821 if (speed || loongarch_immediate_operand_p (outer_code, INTVAL (x)))
3822 {
3823 *total = 0;
3824 return true;
3825 }
3826 /* Fall through. */
3827
3828 case CONST:
3829 case SYMBOL_REF:
3830 case LABEL_REF:
3831 case CONST_DOUBLE:
3832 cost = loongarch_const_insns (x);
3833 if (cost > 0)
3834 {
3835 if (cost == 1 && outer_code == SET
3836 && !(float_mode_p && TARGET_HARD_FLOAT))
3837 cost = 0;
3838 else if ((outer_code == SET || GET_MODE (x) == VOIDmode))
3839 cost = 1;
3840 *total = COSTS_N_INSNS (cost);
3841 return true;
3842 }
3843 /* The value will need to be fetched from the constant pool. */
3844 *total = CONSTANT_POOL_COST;
3845 return true;
3846
3847 case MEM:
3848 /* If the address is legitimate, return the number of
3849 instructions it needs. */
3850 addr = XEXP (x, 0);
3851 /* Check for a scaled indexed address. */
3852 if (loongarch_index_address_p (addr, mode))
3853 {
3854 *total = COSTS_N_INSNS (2);
3855 return true;
3856 }
3857 cost = loongarch_address_cost (addr, mode, true, speed);
3858 if (cost > 0)
3859 {
3860 *total = COSTS_N_INSNS (cost + 1);
3861 return true;
3862 }
3863 /* Otherwise use the default handling. */
3864 return false;
3865
3866 case FFS:
3867 *total = COSTS_N_INSNS (6);
3868 return false;
3869
3870 case NOT:
3871 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3872 return false;
3873
3874 case AND:
3875 /* Check for a *clear_upper32 pattern and treat it like a zero
3876 extension. See the pattern's comment for details. */
3877 if (TARGET_64BIT && mode == DImode && CONST_INT_P (XEXP (x, 1))
3878 && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3879 {
3880 *total = (loongarch_zero_extend_cost (XEXP (x, 0))
3881 + set_src_cost (XEXP (x, 0), mode, speed));
3882 return true;
3883 }
3884 /* (AND (NOT op0) (NOT op1) is a nor operation that can be done in
3885 a single instruction. */
3886 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3887 {
3888 cost = GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1;
3889 *total = (COSTS_N_INSNS (cost)
3890 + set_src_cost (XEXP (XEXP (x, 0), 0), mode, speed)
3891 + set_src_cost (XEXP (XEXP (x, 1), 0), mode, speed));
3892 return true;
3893 }
3894
3895 /* Fall through. */
3896
3897 case IOR:
3898 {
3899 rtx op[2] = {XEXP (x, 0), XEXP (x, 1)};
3900 if (GET_CODE (op[0]) == AND && GET_CODE (op[1]) == AND
3901 && (mode == SImode || (TARGET_64BIT && mode == DImode)))
3902 {
3903 rtx rtx_mask0 = XEXP (op[0], 1), rtx_mask1 = XEXP (op[1], 1);
3904 if (CONST_INT_P (rtx_mask0) && CONST_INT_P (rtx_mask1))
3905 {
3906 unsigned HOST_WIDE_INT mask0 = UINTVAL (rtx_mask0);
3907 unsigned HOST_WIDE_INT mask1 = UINTVAL (rtx_mask1);
3908 if (loongarch_use_bstrins_for_ior_with_mask_1 (mode,
3909 mask0,
3910 mask1))
3911 {
3912 /* A bstrins instruction */
3913 *total = COSTS_N_INSNS (1);
3914
3915 /* A srai instruction */
3916 if (low_bitmask_len (mode, mask0) == -1
3917 && low_bitmask_len (mode, mask1) == -1)
3918 *total += COSTS_N_INSNS (1);
3919
3920 for (int i = 0; i < 2; i++)
3921 *total += set_src_cost (XEXP (op[i], 0), mode, speed);
3922
3923 return true;
3924 }
3925 }
3926 }
3927 }
3928
3929 /* Fall through. */
3930 case XOR:
3931 /* Double-word operations use two single-word operations. */
3932 *total = loongarch_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
3933 speed);
3934 return true;
3935
3936 case ASHIFT:
3937 case ASHIFTRT:
3938 case LSHIFTRT:
3939 case ROTATE:
3940 case ROTATERT:
3941 if (CONSTANT_P (XEXP (x, 1)))
3942 *total = loongarch_binary_cost (x, COSTS_N_INSNS (1),
3943 COSTS_N_INSNS (4), speed);
3944 else
3945 *total = loongarch_binary_cost (x, COSTS_N_INSNS (1),
3946 COSTS_N_INSNS (12), speed);
3947 return true;
3948
3949 case ABS:
3950 if (float_mode_p)
3951 *total = loongarch_cost->fp_add;
3952 else
3953 *total = COSTS_N_INSNS (4);
3954 return false;
3955
3956 case LT:
3957 case LTU:
3958 case LE:
3959 case LEU:
3960 case GT:
3961 case GTU:
3962 case GE:
3963 case GEU:
3964 case EQ:
3965 case NE:
3966 case UNORDERED:
3967 case LTGT:
3968 case UNGE:
3969 case UNGT:
3970 case UNLE:
3971 case UNLT:
3972 /* Branch comparisons have VOIDmode, so use the first operand's
3973 mode instead. */
3974 mode = GET_MODE (XEXP (x, 0));
3975 if (FLOAT_MODE_P (mode))
3976 {
3977 *total = loongarch_cost->fp_add;
3978 return false;
3979 }
3980 *total = loongarch_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3981 speed);
3982 return true;
3983
3984 case MINUS:
3985 case PLUS:
3986 if (float_mode_p)
3987 {
3988 *total = loongarch_cost->fp_add;
3989 return false;
3990 }
3991
3992 /* If it's an add + mult (which is equivalent to shift left) and
3993 it's immediate operand satisfies const_immalsl_operand predicate. */
3994 if (code == PLUS
3995 && (mode == SImode || (TARGET_64BIT && mode == DImode)))
3996 {
3997 HOST_WIDE_INT shamt = -1;
3998 rtx lhs = XEXP (x, 0);
3999 rtx_code code_lhs = GET_CODE (lhs);
4000
4001 switch (code_lhs)
4002 {
4003 case ASHIFT:
4004 if (CONST_INT_P (XEXP (lhs, 1)))
4005 shamt = INTVAL (XEXP (lhs, 1));
4006 break;
4007 case MULT:
4008 if (CONST_INT_P (XEXP (lhs, 1)))
4009 shamt = exact_log2 (INTVAL (XEXP (lhs, 1)));
4010 break;
4011 default:
4012 break;
4013 }
4014
4015 if (IN_RANGE (shamt, 1, 4))
4016 {
4017 *total = (COSTS_N_INSNS (1)
4018 + set_src_cost (XEXP (lhs, 0), mode, speed)
4019 + set_src_cost (XEXP (x, 1), mode, speed));
4020 return true;
4021 }
4022 }
4023
4024 /* Double-word operations require three single-word operations and
4025 an SLTU. */
4026 *total = loongarch_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
4027 speed);
4028 return true;
4029
4030 case NEG:
4031 if (float_mode_p)
4032 *total = loongarch_cost->fp_add;
4033 else
4034 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
4035 return false;
4036
4037 case FMA:
4038 *total = loongarch_fp_mult_cost (mode);
4039 return false;
4040
4041 case MULT:
4042 if (float_mode_p)
4043 *total = loongarch_fp_mult_cost (mode);
4044 else if (mode == DImode && !TARGET_64BIT)
4045 *total = (speed
4046 ? loongarch_cost->int_mult_si * 3 + 6
4047 : COSTS_N_INSNS (7));
4048 else if (mode == DImode)
4049 *total = loongarch_cost->int_mult_di;
4050 else
4051 *total = loongarch_cost->int_mult_si;
4052 return false;
4053
4054 case DIV:
4055 /* Check for a reciprocal. */
4056 if (float_mode_p
4057 && flag_unsafe_math_optimizations
4058 && XEXP (x, 0) == CONST1_RTX (mode))
4059 {
4060 if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
4061 /* An rsqrt<mode>a or rsqrt<mode>b pattern. Count the
4062 division as being free. */
4063 *total = set_src_cost (XEXP (x, 1), mode, speed);
4064 else
4065 *total = (loongarch_fp_div_cost (mode)
4066 + set_src_cost (XEXP (x, 1), mode, speed));
4067 return true;
4068 }
4069 /* Fall through. */
4070
4071 case SQRT:
4072 case MOD:
4073 if (float_mode_p)
4074 {
4075 *total = loongarch_fp_div_cost (mode);
4076 return false;
4077 }
4078 /* Fall through. */
4079
4080 case UDIV:
4081 case UMOD:
4082 if (mode == DImode)
4083 *total = loongarch_cost->int_div_di;
4084 else
4085 {
4086 *total = loongarch_cost->int_div_si;
4087 if (TARGET_64BIT && !ISA_HAS_DIV32)
4088 *total += COSTS_N_INSNS (2);
4089 }
4090
4091 if (TARGET_CHECK_ZERO_DIV)
4092 *total += COSTS_N_INSNS (2);
4093
4094 return false;
4095
4096 case SIGN_EXTEND:
4097 *total = loongarch_sign_extend_cost (XEXP (x, 0));
4098 return false;
4099
4100 case ZERO_EXTEND:
4101 *total = loongarch_zero_extend_cost (XEXP (x, 0));
4102 return false;
4103 case TRUNCATE:
4104 /* Costings for highpart multiplies. Matching patterns of the form:
4105
4106 (lshiftrt:DI (mult:DI (sign_extend:DI (...)
4107 (sign_extend:DI (...))
4108 (const_int 32)
4109 */
4110 if ((GET_CODE (XEXP (x, 0)) == ASHIFTRT
4111 || GET_CODE (XEXP (x, 0)) == LSHIFTRT)
4112 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4113 && ((INTVAL (XEXP (XEXP (x, 0), 1)) == 32
4114 && GET_MODE (XEXP (x, 0)) == DImode)
4115 || (TARGET_64BIT
4116 && INTVAL (XEXP (XEXP (x, 0), 1)) == 64
4117 && GET_MODE (XEXP (x, 0)) == TImode))
4118 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4119 && ((GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND
4120 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == SIGN_EXTEND)
4121 || (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4122 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1))
4123 == ZERO_EXTEND))))
4124 {
4125 if (mode == DImode)
4126 *total = loongarch_cost->int_mult_di;
4127 else
4128 *total = loongarch_cost->int_mult_si;
4129
4130 /* Sign extension is free, zero extension costs for DImode when
4131 on a 64bit core / when DMUL is present. */
4132 for (int i = 0; i < 2; ++i)
4133 {
4134 rtx op = XEXP (XEXP (XEXP (x, 0), 0), i);
4135 if (TARGET_64BIT
4136 && GET_CODE (op) == ZERO_EXTEND
4137 && GET_MODE (op) == DImode)
4138 *total += rtx_cost (op, DImode, MULT, i, speed);
4139 else
4140 *total += rtx_cost (XEXP (op, 0), VOIDmode, GET_CODE (op), 0,
4141 speed);
4142 }
4143
4144 return true;
4145 }
4146 return false;
4147
4148 case FLOAT:
4149 case UNSIGNED_FLOAT:
4150 case FIX:
4151 case FLOAT_EXTEND:
4152 case FLOAT_TRUNCATE:
4153 *total = loongarch_cost->fp_add;
4154 return false;
4155
4156 case SET:
4157 if (register_operand (SET_DEST (x), VOIDmode)
4158 && reg_or_0_operand (SET_SRC (x), VOIDmode))
4159 {
4160 *total = loongarch_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
4161 return true;
4162 }
4163 return false;
4164
4165 default:
4166 return false;
4167 }
4168 }
4169
4170 /* Implement targetm.vectorize.builtin_vectorization_cost. */
4171
4172 static int
4173 loongarch_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
4174 tree vectype,
4175 int misalign ATTRIBUTE_UNUSED)
4176 {
4177 unsigned elements;
4178 machine_mode mode = vectype != NULL ? TYPE_MODE (vectype) : DImode;
4179
4180 switch (type_of_cost)
4181 {
4182 case scalar_stmt:
4183 case scalar_load:
4184 case vector_stmt:
4185 case vec_to_scalar:
4186 case scalar_to_vec:
4187 case scalar_store:
4188 return 1;
4189
4190 case vec_promote_demote:
4191 case vec_perm:
4192 return LASX_SUPPORTED_MODE_P (mode)
4193 && !LSX_SUPPORTED_MODE_P (mode) ? 2 : 1;
4194
4195 case vector_load:
4196 case vector_store:
4197 case unaligned_load:
4198 case unaligned_store:
4199 return 2;
4200
4201 case cond_branch_taken:
4202 return 4;
4203
4204 case cond_branch_not_taken:
4205 return 2;
4206
4207 case vec_construct:
4208 elements = TYPE_VECTOR_SUBPARTS (vectype);
4209 if (LASX_SUPPORTED_MODE_P (mode) && !LSX_SUPPORTED_MODE_P (mode))
4210 return elements / 2 + 3;
4211 else
4212 return elements / 2 + 1;
4213
4214 default:
4215 gcc_unreachable ();
4216 }
4217 }
4218
4219 class loongarch_vector_costs : public vector_costs
4220 {
4221 public:
4222 using vector_costs::vector_costs;
4223
4224 unsigned int add_stmt_cost (int count, vect_cost_for_stmt kind,
4225 stmt_vec_info stmt_info, slp_tree, tree vectype,
4226 int misalign,
4227 vect_cost_model_location where) override;
4228 void finish_cost (const vector_costs *) override;
4229
4230 protected:
4231 void count_operations (vect_cost_for_stmt, stmt_vec_info,
4232 vect_cost_model_location, unsigned int);
4233 unsigned int determine_suggested_unroll_factor (loop_vec_info);
4234 /* The number of vectorized stmts in loop. */
4235 unsigned m_stmts = 0;
4236 /* The number of load and store operations in loop. */
4237 unsigned m_loads = 0;
4238 unsigned m_stores = 0;
4239 /* Reduction factor for suggesting unroll factor. */
4240 unsigned m_reduc_factor = 0;
4241 /* True if the loop contains an average operation. */
4242 bool m_has_avg = false;
4243 /* True if the loop uses approximation instruction sequence. */
4244 bool m_has_recip = false;
4245 };
4246
4247 /* Implement TARGET_VECTORIZE_CREATE_COSTS. */
4248 static vector_costs *
4249 loongarch_vectorize_create_costs (vec_info *vinfo, bool costing_for_scalar)
4250 {
4251 return new loongarch_vector_costs (vinfo, costing_for_scalar);
4252 }
4253
4254 void
4255 loongarch_vector_costs::count_operations (vect_cost_for_stmt kind,
4256 stmt_vec_info stmt_info,
4257 vect_cost_model_location where,
4258 unsigned int count)
4259 {
4260 if (!m_costing_for_scalar
4261 && is_a<loop_vec_info> (m_vinfo)
4262 && where == vect_body)
4263 {
4264 m_stmts += count;
4265
4266 if (kind == scalar_load
4267 || kind == vector_load
4268 || kind == unaligned_load)
4269 m_loads += count;
4270 else if (kind == scalar_store
4271 || kind == vector_store
4272 || kind == unaligned_store)
4273 m_stores += count;
4274 else if ((kind == scalar_stmt
4275 || kind == vector_stmt
4276 || kind == vec_to_scalar)
4277 && stmt_info && vect_is_reduction (stmt_info))
4278 {
4279 tree lhs = gimple_get_lhs (stmt_info->stmt);
4280 unsigned int base = FLOAT_TYPE_P (TREE_TYPE (lhs)) ? 2 : 1;
4281 m_reduc_factor = MAX (base * count, m_reduc_factor);
4282 }
4283 }
4284 }
4285
4286 unsigned int
4287 loongarch_vector_costs::determine_suggested_unroll_factor (loop_vec_info loop_vinfo)
4288 {
4289 class loop *loop = LOOP_VINFO_LOOP (loop_vinfo);
4290
4291 if (m_has_avg || m_has_recip)
4292 return 1;
4293
4294 /* Don't unroll if it's specified explicitly not to be unrolled. */
4295 if (loop->unroll == 1
4296 || (OPTION_SET_P (flag_unroll_loops) && !flag_unroll_loops)
4297 || (OPTION_SET_P (flag_unroll_all_loops) && !flag_unroll_all_loops))
4298 return 1;
4299
4300 unsigned int nstmts_nonldst = m_stmts - m_loads - m_stores;
4301 /* Don't unroll if no vector instructions excepting for memory access. */
4302 if (nstmts_nonldst == 0)
4303 return 1;
4304
4305 /* Use this simple hardware resource model that how many non vld/vst
4306 vector instructions can be issued per cycle. */
4307 unsigned int issue_info = la_vect_issue_info;
4308 unsigned int reduc_factor = m_reduc_factor > 1 ? m_reduc_factor : 1;
4309 unsigned int uf = CEIL (reduc_factor * issue_info, nstmts_nonldst);
4310 uf = MIN ((unsigned int) la_vect_unroll_limit, uf);
4311
4312 return 1 << ceil_log2 (uf);
4313 }
4314
4315 /* Check if assign stmt rhs op comes from a multiply-add operation. */
4316 static bool
4317 loongarch_multiply_add_p (vec_info *vinfo, stmt_vec_info stmt_info)
4318 {
4319 gassign *assign = dyn_cast<gassign *> (stmt_info->stmt);
4320 if (!assign)
4321 return false;
4322 tree_code code = gimple_assign_rhs_code (assign);
4323 if (code != PLUS_EXPR && code != MINUS_EXPR)
4324 return false;
4325
4326 auto is_mul_result = [&](int i)
4327 {
4328 tree rhs = gimple_op (assign, i);
4329 if (TREE_CODE (rhs) != SSA_NAME)
4330 return false;
4331
4332 stmt_vec_info def_stmt_info = vinfo->lookup_def (rhs);
4333 if (!def_stmt_info
4334 || STMT_VINFO_DEF_TYPE (def_stmt_info) != vect_internal_def)
4335 return false;
4336 gassign *rhs_assign = dyn_cast<gassign *> (def_stmt_info->stmt);
4337 if (!rhs_assign || gimple_assign_rhs_code (rhs_assign) != MULT_EXPR)
4338 return false;
4339
4340 return true;
4341 };
4342
4343 return is_mul_result (1) || is_mul_result (2);
4344 }
4345
4346 unsigned
4347 loongarch_vector_costs::add_stmt_cost (int count, vect_cost_for_stmt kind,
4348 stmt_vec_info stmt_info, slp_tree,
4349 tree vectype, int misalign,
4350 vect_cost_model_location where)
4351 {
4352 unsigned retval = 0;
4353
4354 if (flag_vect_cost_model)
4355 {
4356 int stmt_cost = loongarch_builtin_vectorization_cost (kind, vectype,
4357 misalign);
4358 if (vectype && stmt_info)
4359 {
4360 gassign *assign = dyn_cast<gassign *> (STMT_VINFO_STMT (stmt_info));
4361 machine_mode mode = TYPE_MODE (vectype);
4362
4363 /* We found through testing that this strategy (the stmt that
4364 matches the multiply-add pattern) has positive returns only
4365 when applied to the 128-bit vector stmt, so this restriction
4366 is currently made. */
4367 if (kind == vector_stmt && GET_MODE_SIZE (mode) == 16 && assign)
4368 {
4369 if (!vect_is_reduction (stmt_info)
4370 && loongarch_multiply_add_p (m_vinfo, stmt_info))
4371 stmt_cost = 0;
4372 }
4373 }
4374
4375 retval = adjust_cost_for_freq (stmt_info, where, count * stmt_cost);
4376 m_costs[where] += retval;
4377
4378 count_operations (kind, stmt_info, where, count);
4379 }
4380
4381 if (stmt_info)
4382 {
4383 /* Detect the use of an averaging operation. */
4384 gimple *stmt = stmt_info->stmt;
4385 if (is_gimple_call (stmt)
4386 && gimple_call_internal_p (stmt))
4387 {
4388 switch (gimple_call_internal_fn (stmt))
4389 {
4390 case IFN_AVG_FLOOR:
4391 case IFN_AVG_CEIL:
4392 m_has_avg = true;
4393 default:
4394 break;
4395 }
4396 }
4397 }
4398
4399 combined_fn cfn;
4400 if (kind == vector_stmt
4401 && stmt_info
4402 && stmt_info->stmt)
4403 {
4404 /* Detect the use of approximate instruction sequence. */
4405 if ((TARGET_RECIP_VEC_SQRT || TARGET_RECIP_VEC_RSQRT)
4406 && (cfn = gimple_call_combined_fn (stmt_info->stmt)) != CFN_LAST)
4407 switch (cfn)
4408 {
4409 case CFN_BUILT_IN_SQRTF:
4410 m_has_recip = true;
4411 default:
4412 break;
4413 }
4414 else if (TARGET_RECIP_VEC_DIV
4415 && vectype
4416 && gimple_code (stmt_info->stmt) == GIMPLE_ASSIGN)
4417 {
4418 machine_mode mode = TYPE_MODE (vectype);
4419 switch (gimple_assign_rhs_code (stmt_info->stmt))
4420 {
4421 case RDIV_EXPR:
4422 if (GET_MODE_INNER (mode) == SFmode)
4423 m_has_recip = true;
4424 default:
4425 break;
4426 }
4427 }
4428 }
4429
4430 return retval;
4431 }
4432
4433 void
4434 loongarch_vector_costs::finish_cost (const vector_costs *scalar_costs)
4435 {
4436 loop_vec_info loop_vinfo = dyn_cast<loop_vec_info> (m_vinfo);
4437
4438 if (loop_vinfo)
4439 m_suggested_unroll_factor
4440 = determine_suggested_unroll_factor (loop_vinfo);
4441
4442 vector_costs::finish_cost (scalar_costs);
4443 }
4444
4445 /* Implement TARGET_INSN_COST. */
4446
4447 static int
4448 loongarch_insn_cost (rtx_insn *insn, bool speed)
4449 {
4450 rtx x = PATTERN (insn);
4451 int cost = pattern_cost (x, speed);
4452
4453 /* On LA464, prevent movcf2fr and movfr2gr from merging into movcf2gr. */
4454 if (GET_CODE (x) == SET
4455 && GET_MODE (XEXP (x, 0)) == FCCmode)
4456 {
4457 rtx dest, src;
4458 dest = XEXP (x, 0);
4459 src = XEXP (x, 1);
4460
4461 if (REG_P (dest) && REG_P (src))
4462 {
4463 if (GP_REG_P (REGNO (dest)) && FCC_REG_P (REGNO (src)))
4464 cost = loongarch_cost->movcf2gr;
4465 else if (FCC_REG_P (REGNO (dest)) && GP_REG_P (REGNO (src)))
4466 cost = loongarch_cost->movgr2cf;
4467 }
4468 }
4469 return cost;
4470 }
4471
4472 /* Return one word of double-word value OP, taking into account the fixed
4473 endianness of certain registers. HIGH_P is true to select the high part,
4474 false to select the low part. */
4475
4476 rtx
4477 loongarch_subword (rtx op, bool high_p)
4478 {
4479 unsigned int byte;
4480 machine_mode mode;
4481
4482 byte = high_p ? UNITS_PER_WORD : 0;
4483 mode = GET_MODE (op);
4484 if (mode == VOIDmode)
4485 mode = TARGET_64BIT ? TImode : DImode;
4486
4487 if (FP_REG_RTX_P (op))
4488 return gen_rtx_REG (word_mode, REGNO (op) + high_p);
4489
4490 if (MEM_P (op))
4491 return loongarch_rewrite_small_data (adjust_address (op, word_mode, byte));
4492
4493 return simplify_gen_subreg (word_mode, op, mode, byte);
4494 }
4495
4496 static bool loongarch_split_vector_move_p (rtx dest, rtx src);
4497 /* Return true if a move from SRC to DEST should be split into two.
4498 SPLIT_TYPE describes the split condition. */
4499
4500 bool
4501 loongarch_split_move_p (rtx dest, rtx src)
4502 {
4503 /* FPR-to-FPR moves can be done in a single instruction, if they're
4504 allowed at all. */
4505 unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
4506 if (size == 8 && FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4507 return false;
4508
4509 /* Check for floating-point loads and stores. */
4510 if (size == 8)
4511 {
4512 if (FP_REG_RTX_P (dest) && MEM_P (src))
4513 return false;
4514 if (FP_REG_RTX_P (src) && MEM_P (dest))
4515 return false;
4516 }
4517
4518
4519 /* Check if vector moves need splitting. */
4520 if (LSX_SUPPORTED_MODE_P (GET_MODE (dest))
4521 || LASX_SUPPORTED_MODE_P (GET_MODE (dest)))
4522 return loongarch_split_vector_move_p (dest, src);
4523
4524 /* Otherwise split all multiword moves. */
4525 return size > UNITS_PER_WORD;
4526 }
4527
4528 /* Split a move from SRC to DEST, given that loongarch_split_move_p holds.
4529 SPLIT_TYPE describes the split condition. */
4530
4531 void
4532 loongarch_split_move (rtx dest, rtx src)
4533 {
4534 gcc_checking_assert (loongarch_split_move_p (dest, src));
4535 if (LSX_SUPPORTED_MODE_P (GET_MODE (dest))
4536 || LASX_SUPPORTED_MODE_P (GET_MODE (dest)))
4537 loongarch_split_vector_move (dest, src);
4538 else
4539 gcc_unreachable ();
4540 }
4541
4542 /* Check if adding an integer constant value for a specific mode can be
4543 performed with an addu16i.d instruction and an addi.{w/d}
4544 instruction. */
4545
4546 bool
4547 loongarch_addu16i_imm12_operand_p (HOST_WIDE_INT value, machine_mode mode)
4548 {
4549 /* Not necessary, but avoid unnecessary calculation if !TARGET_64BIT. */
4550 if (!TARGET_64BIT)
4551 return false;
4552
4553 if ((value & 0xffff) == 0)
4554 return false;
4555
4556 if (IMM12_OPERAND (value))
4557 return false;
4558
4559 value = (value & ~HWIT_UC_0xFFF) + ((value & 0x800) << 1);
4560 return ADDU16I_OPERAND (trunc_int_for_mode (value, mode));
4561 }
4562
4563 /* Split one integer constant op[0] into two (op[1] and op[2]) for constant
4564 plus operation in a specific mode. The splitted constants can be added
4565 onto a register with a single instruction (addi.{d/w} or addu16i.d). */
4566
4567 void
4568 loongarch_split_plus_constant (rtx *op, machine_mode mode)
4569 {
4570 HOST_WIDE_INT v = INTVAL (op[0]), a;
4571
4572 if (DUAL_IMM12_OPERAND (v))
4573 a = (v > 0 ? 2047 : -2048);
4574 else if (loongarch_addu16i_imm12_operand_p (v, mode))
4575 a = (v & ~HWIT_UC_0xFFF) + ((v & 0x800) << 1);
4576 else if (mode == DImode && DUAL_ADDU16I_OPERAND (v))
4577 a = (v > 0 ? 0x7fff0000 : ~0x7fffffff);
4578 else
4579 gcc_unreachable ();
4580
4581 op[1] = gen_int_mode (a, mode);
4582 v = v - (unsigned HOST_WIDE_INT) a;
4583 op[2] = gen_int_mode (v, mode);
4584 }
4585
4586 /* Test if reassociate (a << shamt) [&|^] mask to
4587 (a [&|^] (mask >> shamt)) << shamt is possible and beneficial.
4588 If true, return (mask >> shamt). Return NULL_RTX otherwise. */
4589
4590 rtx
4591 loongarch_reassoc_shift_bitwise (bool is_and, rtx shamt, rtx mask,
4592 machine_mode mode)
4593 {
4594 gcc_checking_assert (CONST_INT_P (shamt));
4595 gcc_checking_assert (CONST_INT_P (mask));
4596 gcc_checking_assert (mode == SImode || mode == DImode);
4597
4598 if (ctz_hwi (INTVAL (mask)) < INTVAL (shamt))
4599 return NULL_RTX;
4600
4601 /* When trying alsl.w, deliberately ignore the high bits. */
4602 mask = gen_int_mode (UINTVAL (mask), mode);
4603
4604 rtx new_mask = simplify_const_binary_operation (LSHIFTRT, mode, mask,
4605 shamt);
4606
4607 /* Do an arithmetic shift for checking ins_zero_bitmask_operand or -1:
4608 ashiftrt (0xffffffff00000000, 2) is 0xffffffff60000000 which is an
4609 ins_zero_bitmask_operand, but lshiftrt will produce
4610 0x3fffffff60000000. */
4611 rtx new_mask_1 = simplify_const_binary_operation (ASHIFTRT, mode, mask,
4612 shamt);
4613
4614 if (is_and && const_m1_operand (new_mask_1, mode))
4615 return new_mask_1;
4616
4617 if (const_uns_arith_operand (new_mask, mode))
4618 return new_mask;
4619
4620 if (!is_and)
4621 return NULL_RTX;
4622
4623 if (low_bitmask_operand (new_mask, mode))
4624 return new_mask;
4625
4626 return ins_zero_bitmask_operand (new_mask_1, mode) ? new_mask_1 : NULL_RTX;
4627 }
4628
4629 /* Implement TARGET_CONSTANT_ALIGNMENT. */
4630
4631 static HOST_WIDE_INT
4632 loongarch_constant_alignment (const_tree exp, HOST_WIDE_INT align)
4633 {
4634 if (TREE_CODE (exp) == STRING_CST || TREE_CODE (exp) == CONSTRUCTOR)
4635 return MAX (align, BITS_PER_WORD);
4636 return align;
4637 }
4638
4639 const char *
4640 loongarch_output_move_index (rtx x, machine_mode mode, bool ldr)
4641 {
4642 int index = exact_log2 (GET_MODE_SIZE (mode));
4643 if (!IN_RANGE (index, 0, 3))
4644 return NULL;
4645
4646 struct loongarch_address_info info;
4647 if ((loongarch_classify_address (&info, x, mode, false)
4648 && !(info.type == ADDRESS_REG_REG))
4649 || !loongarch_legitimate_address_p (mode, x, false))
4650 return NULL;
4651
4652 const char *const insn[][4] =
4653 {
4654 {
4655 "stx.b\t%z1,%0",
4656 "stx.h\t%z1,%0",
4657 "stx.w\t%z1,%0",
4658 "stx.d\t%z1,%0",
4659 },
4660 {
4661 "ldx.bu\t%0,%1",
4662 "ldx.hu\t%0,%1",
4663 "ldx.w\t%0,%1",
4664 "ldx.d\t%0,%1",
4665 }
4666 };
4667
4668 return insn[ldr][index];
4669 }
4670
4671 const char *
4672 loongarch_output_move_index_float (rtx x, machine_mode mode, bool ldr)
4673 {
4674 int index = exact_log2 (GET_MODE_SIZE (mode));
4675 if (!IN_RANGE (index, 2, 5))
4676 return NULL;
4677
4678 struct loongarch_address_info info;
4679 if ((loongarch_classify_address (&info, x, mode, false)
4680 && !(info.type == ADDRESS_REG_REG))
4681 || !loongarch_legitimate_address_p (mode, x, false))
4682 return NULL;
4683
4684 const char *const insn[][4] =
4685 {
4686 {
4687 "fstx.s\t%1,%0",
4688 "fstx.d\t%1,%0",
4689 "vstx\t%w1,%0",
4690 "xvstx\t%u1,%0"
4691 },
4692 {
4693 "fldx.s\t%0,%1",
4694 "fldx.d\t%0,%1",
4695 "vldx\t%w0,%1",
4696 "xvldx\t%u0,%1"
4697 }
4698 };
4699
4700 return insn[ldr][index-2];
4701 }
4702 /* Return true if a vector move from SRC to DEST should be split. */
4703
4704 static bool
4705 loongarch_split_vector_move_p (rtx dest, rtx src)
4706 {
4707 /* Vector moves can be done in a single instruction. */
4708 if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4709 return false;
4710
4711 /* Check for vector loads and stores. */
4712 if (FP_REG_RTX_P (dest) && MEM_P (src))
4713 return false;
4714 if (FP_REG_RTX_P (src) && MEM_P (dest))
4715 return false;
4716
4717 /* Check for vector set to an immediate const vector with valid replicated
4718 element. */
4719 if (FP_REG_RTX_P (dest)
4720 && loongarch_const_vector_vrepli (src, GET_MODE (src)))
4721 return false;
4722
4723 /* Check for vector load zero immediate. */
4724 if (FP_REG_RTX_P (dest) && src == CONST0_RTX (GET_MODE (src)))
4725 return false;
4726
4727 return true;
4728 }
4729
4730 /* Split a vector move from SRC to DEST. */
4731
4732 void
4733 loongarch_split_vector_move (rtx dest, rtx src)
4734 {
4735 int byte, index;
4736 rtx s, d;
4737 machine_mode mode = GET_MODE (dest);
4738 bool lsx_p = LSX_SUPPORTED_MODE_P (mode);
4739
4740 if (FP_REG_RTX_P (dest))
4741 {
4742 gcc_assert (!MEM_P (src));
4743
4744 rtx (*gen_vinsgr2vr_d) (rtx, rtx, rtx, rtx);
4745
4746 if (lsx_p)
4747 {
4748 mode = V2DImode;
4749 gen_vinsgr2vr_d = gen_lsx_vinsgr2vr_d;
4750 }
4751 else
4752 {
4753 mode = V4DImode;
4754 gen_vinsgr2vr_d = gen_lasx_xvinsgr2vr_d;
4755 }
4756
4757 rtx new_dest = dest;
4758
4759 if (GET_MODE (dest) != mode)
4760 new_dest = simplify_gen_subreg (mode, dest, GET_MODE (dest), 0);
4761
4762 for (byte = 0, index = 0; byte < GET_MODE_SIZE (GET_MODE (dest));
4763 byte += UNITS_PER_WORD, index++)
4764 {
4765 s = loongarch_subword_at_byte (src, byte);
4766 emit_insn (gen_vinsgr2vr_d (new_dest, s, new_dest,
4767 GEN_INT (1 << index)));
4768 }
4769 }
4770 else if (FP_REG_RTX_P (src))
4771 {
4772 gcc_assert (!MEM_P (dest));
4773
4774 rtx (*gen_vpickve2gr_d) (rtx, rtx, rtx);
4775
4776 if (lsx_p)
4777 {
4778 mode = V2DImode;
4779 gen_vpickve2gr_d = gen_lsx_vpickve2gr_d;
4780 }
4781 else
4782 {
4783 mode = V4DImode;
4784 gen_vpickve2gr_d = gen_lasx_xvpickve2gr_d;
4785 }
4786
4787 rtx new_src = src;
4788 if (GET_MODE (src) != mode)
4789 new_src = simplify_gen_subreg (mode, src, GET_MODE (src), 0);
4790
4791 for (byte = 0, index = 0; byte < GET_MODE_SIZE (GET_MODE (src));
4792 byte += UNITS_PER_WORD, index++)
4793 {
4794 d = loongarch_subword_at_byte (dest, byte);
4795 emit_insn (gen_vpickve2gr_d (d, new_src, GEN_INT (index)));
4796 }
4797 }
4798 else
4799 {
4800 /* This part of the code is designed to handle the following situations:
4801 (set (reg:V2DI 4 $r4)
4802 (reg:V2DI 6 $r6))
4803 The trigger test case is lsx-mov-1.c. */
4804 rtx low_dest, low_src;
4805
4806 low_dest = loongarch_subword_at_byte (dest, 0);
4807 low_src = loongarch_subword_at_byte (src, 0);
4808 gcc_assert (REG_P (low_dest) && REG_P (low_src));
4809 /* Make sure the source register is not written before reading. */
4810 if (REGNO (low_dest) <= REGNO (low_src))
4811 {
4812 for (byte = 0; byte < GET_MODE_SIZE (GET_MODE (dest));
4813 byte += UNITS_PER_WORD)
4814 {
4815 d = loongarch_subword_at_byte (dest, byte);
4816 s = loongarch_subword_at_byte (src, byte);
4817 loongarch_emit_move (d, s);
4818 }
4819 }
4820 else
4821 {
4822 for (byte = GET_MODE_SIZE (GET_MODE (dest)) - UNITS_PER_WORD;
4823 byte >= 0; byte -= UNITS_PER_WORD)
4824 {
4825 d = loongarch_subword_at_byte (dest, byte);
4826 s = loongarch_subword_at_byte (src, byte);
4827 loongarch_emit_move (d, s);
4828 }
4829 }
4830 }
4831 }
4832
4833 /* Return the appropriate instructions to move SRC into DEST. Assume
4834 that SRC is operand 1 and DEST is operand 0. */
4835
4836 const char *
4837 loongarch_output_move (rtx *operands)
4838 {
4839 rtx src = operands[1];
4840 rtx dest = operands[0];
4841 enum rtx_code dest_code = GET_CODE (dest);
4842 enum rtx_code src_code = GET_CODE (src);
4843 machine_mode mode = GET_MODE (dest);
4844 bool dbl_p = (GET_MODE_SIZE (mode) == 8);
4845 bool lsx_p = LSX_SUPPORTED_MODE_P (mode);
4846 bool lasx_p = LASX_SUPPORTED_MODE_P (mode);
4847
4848 if (loongarch_split_move_p (dest, src))
4849 return "#";
4850
4851 if ((lsx_p || lasx_p)
4852 && dest_code == REG && FP_REG_P (REGNO (dest))
4853 && src_code == CONST_VECTOR
4854 && CONST_INT_P (CONST_VECTOR_ELT (src, 0)))
4855 {
4856 operands[1] = loongarch_const_vector_vrepli (src, mode);
4857 gcc_assert (operands[1]);
4858
4859 switch (GET_MODE_SIZE (mode))
4860 {
4861 case 16:
4862 return "vrepli.%v1\t%w0,%E1";
4863 case 32:
4864 return "xvrepli.%v1\t%u0,%E1";
4865 default: gcc_unreachable ();
4866 }
4867 }
4868
4869 if ((src_code == REG && GP_REG_P (REGNO (src)))
4870 || (src == CONST0_RTX (mode)))
4871 {
4872 if (dest_code == REG)
4873 {
4874 if (GP_REG_P (REGNO (dest)))
4875 return "or\t%0,%z1,$r0";
4876
4877 if (FP_REG_P (REGNO (dest)))
4878 {
4879 if (lsx_p || lasx_p)
4880 {
4881 gcc_assert (src == CONST0_RTX (GET_MODE (src)));
4882 switch (GET_MODE_SIZE (mode))
4883 {
4884 case 16:
4885 return "vrepli.b\t%w0,0";
4886 case 32:
4887 return "xvrepli.b\t%u0,0";
4888 default:
4889 gcc_unreachable ();
4890 }
4891 }
4892 if (ISA_HAS_LSX && src == CONST0_RTX (GET_MODE (src)))
4893 return "vxor.v\t%w0,%w0,%w0";
4894
4895 return dbl_p ? "movgr2fr.d\t%0,%z1" : "movgr2fr.w\t%0,%z1";
4896 }
4897 }
4898 if (dest_code == MEM)
4899 {
4900 const char *insn = NULL;
4901 insn = loongarch_output_move_index (XEXP (dest, 0), GET_MODE (dest),
4902 false);
4903 if (insn)
4904 return insn;
4905
4906 rtx offset = XEXP (dest, 0);
4907 if (GET_CODE (offset) == PLUS)
4908 offset = XEXP (offset, 1);
4909 switch (GET_MODE_SIZE (mode))
4910 {
4911 case 1:
4912 return "st.b\t%z1,%0";
4913 case 2:
4914 return "st.h\t%z1,%0";
4915 case 4:
4916 /* Matching address type with a 12bit offset and
4917 ADDRESS_LO_SUM. */
4918 if (const_arith_operand (offset, Pmode)
4919 || GET_CODE (offset) == LO_SUM)
4920 return "st.w\t%z1,%0";
4921 else
4922 return "stptr.w\t%z1,%0";
4923 case 8:
4924 if (const_arith_operand (offset, Pmode)
4925 || GET_CODE (offset) == LO_SUM)
4926 return "st.d\t%z1,%0";
4927 else
4928 return "stptr.d\t%z1,%0";
4929 default:
4930 gcc_unreachable ();
4931 }
4932 }
4933 }
4934 if (dest_code == REG && GP_REG_P (REGNO (dest)))
4935 {
4936 if (src_code == REG)
4937 if (FP_REG_P (REGNO (src)))
4938 {
4939 gcc_assert (!lsx_p);
4940 return dbl_p ? "movfr2gr.d\t%0,%1" : "movfr2gr.s\t%0,%1";
4941 }
4942
4943 if (src_code == MEM)
4944 {
4945 const char *insn = NULL;
4946 insn = loongarch_output_move_index (XEXP (src, 0), GET_MODE (src),
4947 true);
4948 if (insn)
4949 return insn;
4950
4951 rtx offset = XEXP (src, 0);
4952 if (GET_CODE (offset) == PLUS)
4953 offset = XEXP (offset, 1);
4954 switch (GET_MODE_SIZE (mode))
4955 {
4956 case 1:
4957 return "ld.bu\t%0,%1";
4958 case 2:
4959 return "ld.hu\t%0,%1";
4960 case 4:
4961 /* Matching address type with a 12bit offset and
4962 ADDRESS_LO_SUM. */
4963 if (const_arith_operand (offset, Pmode)
4964 || GET_CODE (offset) == LO_SUM)
4965 return "ld.w\t%0,%1";
4966 else
4967 return "ldptr.w\t%0,%1";
4968 case 8:
4969 if (const_arith_operand (offset, Pmode)
4970 || GET_CODE (offset) == LO_SUM)
4971 return "ld.d\t%0,%1";
4972 else
4973 return "ldptr.d\t%0,%1";
4974 default:
4975 gcc_unreachable ();
4976 }
4977 }
4978
4979 if (src_code == HIGH)
4980 {
4981 rtx offset, x;
4982 split_const (XEXP (src, 0), &x, &offset);
4983 enum loongarch_symbol_type type = SYMBOL_PCREL;
4984
4985 if (UNSPEC_ADDRESS_P (x))
4986 type = UNSPEC_ADDRESS_TYPE (x);
4987
4988 if (type == SYMBOL_TLS_LE)
4989 return "lu12i.w\t%0,%h1";
4990 else
4991 return "%Q1pcalau12i\t%0,%h1";
4992 }
4993
4994 if (src_code == CONST_INT)
4995 {
4996 if (LU12I_INT (src))
4997 {
4998 operands[1] = GEN_INT (INTVAL (operands[1]) >> 12);
4999 return "lu12i.w\t%0,%1\t\t\t# %X1";
5000 }
5001 else if (IMM12_INT (src))
5002 return "addi.w\t%0,$r0,%1\t\t\t# %X1";
5003 else if (IMM12_INT_UNSIGNED (src))
5004 return "ori\t%0,$r0,%1\t\t\t# %X1";
5005 else if (LU52I_INT (src))
5006 {
5007 operands[1] = GEN_INT (INTVAL (operands[1]) >> 52);
5008 return "lu52i.d\t%0,$r0,%X1\t\t\t# %1";
5009 }
5010 else
5011 gcc_unreachable ();
5012 }
5013 }
5014
5015 if (!loongarch_explicit_relocs_p (loongarch_classify_symbol (src))
5016 && dest_code == REG && symbolic_operand (src, VOIDmode))
5017 {
5018 if (loongarch_classify_symbol (src) == SYMBOL_PCREL)
5019 return "la.local\t%0,%1";
5020 else
5021 return "la.global\t%0,%1";
5022 }
5023
5024 if (src_code == REG && FP_REG_P (REGNO (src)))
5025 {
5026 if (dest_code == REG && FP_REG_P (REGNO (dest)))
5027 {
5028 if (lsx_p || lasx_p)
5029 {
5030 switch (GET_MODE_SIZE (mode))
5031 {
5032 case 16:
5033 return "vori.b\t%w0,%w1,0";
5034 case 32:
5035 return "xvori.b\t%u0,%u1,0";
5036 default:
5037 gcc_unreachable ();
5038 }
5039 }
5040
5041 return dbl_p ? "fmov.d\t%0,%1" : "fmov.s\t%0,%1";
5042 }
5043
5044 if (dest_code == MEM)
5045 {
5046 const char *insn = NULL;
5047 insn = loongarch_output_move_index_float (XEXP (dest, 0),
5048 GET_MODE (dest),
5049 false);
5050 if (insn)
5051 return insn;
5052
5053 if (lsx_p || lasx_p)
5054 {
5055 switch (GET_MODE_SIZE (mode))
5056 {
5057 case 16:
5058 return "vst\t%w1,%0";
5059 case 32:
5060 return "xvst\t%u1,%0";
5061 default:
5062 gcc_unreachable ();
5063 }
5064 }
5065
5066 return dbl_p ? "fst.d\t%1,%0" : "fst.s\t%1,%0";
5067 }
5068 }
5069
5070 if (dest_code == REG && FP_REG_P (REGNO (dest)))
5071 {
5072 if (src_code == MEM)
5073 {
5074 const char *insn = NULL;
5075 insn = loongarch_output_move_index_float (XEXP (src, 0),
5076 GET_MODE (src),
5077 true);
5078 if (insn)
5079 return insn;
5080
5081 if (lsx_p || lasx_p)
5082 {
5083 switch (GET_MODE_SIZE (mode))
5084 {
5085 case 16:
5086 return "vld\t%w0,%1";
5087 case 32:
5088 return "xvld\t%u0,%1";
5089 default:
5090 gcc_unreachable ();
5091 }
5092 }
5093 return dbl_p ? "fld.d\t%0,%1" : "fld.s\t%0,%1";
5094 }
5095 }
5096
5097 gcc_unreachable ();
5098 }
5099
5100 /* Return true if CMP1 is a suitable second operand for integer ordering
5101 test CODE. */
5102
5103 static bool
5104 loongarch_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
5105 {
5106 switch (code)
5107 {
5108 case GT:
5109 case GTU:
5110 return reg_or_0_operand (cmp1, VOIDmode);
5111
5112 case GE:
5113 case GEU:
5114 return cmp1 == const1_rtx;
5115
5116 case LT:
5117 case LTU:
5118 return arith_operand (cmp1, VOIDmode);
5119
5120 case LE:
5121 return sle_operand (cmp1, VOIDmode);
5122
5123 case LEU:
5124 return sleu_operand (cmp1, VOIDmode);
5125
5126 default:
5127 gcc_unreachable ();
5128 }
5129 }
5130
5131 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
5132 integer ordering test *CODE, or if an equivalent combination can
5133 be formed by adjusting *CODE and *CMP1. When returning true, update
5134 *CODE and *CMP1 with the chosen code and operand, otherwise leave
5135 them alone. */
5136
5137 static bool
5138 loongarch_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
5139 machine_mode mode)
5140 {
5141 HOST_WIDE_INT plus_one;
5142
5143 if (loongarch_int_order_operand_ok_p (*code, *cmp1))
5144 return true;
5145
5146 if (CONST_INT_P (*cmp1))
5147 switch (*code)
5148 {
5149 case LE:
5150 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
5151 if (INTVAL (*cmp1) < plus_one)
5152 {
5153 *code = LT;
5154 *cmp1 = force_reg (mode, GEN_INT (plus_one));
5155 return true;
5156 }
5157 break;
5158
5159 case LEU:
5160 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
5161 if (plus_one != 0)
5162 {
5163 *code = LTU;
5164 *cmp1 = force_reg (mode, GEN_INT (plus_one));
5165 return true;
5166 }
5167 break;
5168
5169 default:
5170 break;
5171 }
5172 return false;
5173 }
5174
5175 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
5176 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
5177 is nonnull, it's OK to set TARGET to the inverse of the result and
5178 flip *INVERT_PTR instead. */
5179
5180 static void
5181 loongarch_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
5182 rtx target, rtx cmp0, rtx cmp1)
5183 {
5184 machine_mode mode;
5185
5186 /* First see if there is a LoongArch instruction that can do this operation.
5187 If not, try doing the same for the inverse operation. If that also
5188 fails, force CMP1 into a register and try again. */
5189 mode = GET_MODE (cmp0);
5190 if (loongarch_canonicalize_int_order_test (&code, &cmp1, mode))
5191 loongarch_emit_binary (code, target, cmp0, cmp1);
5192 else
5193 {
5194 enum rtx_code inv_code = reverse_condition (code);
5195 if (!loongarch_canonicalize_int_order_test (&inv_code, &cmp1, mode))
5196 {
5197 cmp1 = force_reg (mode, cmp1);
5198 loongarch_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
5199 }
5200 else if (invert_ptr == 0)
5201 {
5202 rtx inv_target;
5203
5204 inv_target = loongarch_force_binary (GET_MODE (target),
5205 inv_code, cmp0, cmp1);
5206 loongarch_emit_binary (XOR, target, inv_target, const1_rtx);
5207 }
5208 else
5209 {
5210 *invert_ptr = !*invert_ptr;
5211 loongarch_emit_binary (inv_code, target, cmp0, cmp1);
5212 }
5213 }
5214 }
5215
5216 /* Return a register that is zero if CMP0 and CMP1 are equal.
5217 The register will have the same mode as CMP0. */
5218
5219 static rtx
5220 loongarch_zero_if_equal (rtx cmp0, rtx cmp1)
5221 {
5222 if (cmp1 == const0_rtx)
5223 return cmp0;
5224
5225 if (uns_arith_operand (cmp1, VOIDmode))
5226 return expand_binop (GET_MODE (cmp0), xor_optab, cmp0, cmp1, 0, 0,
5227 OPTAB_DIRECT);
5228
5229 return expand_binop (GET_MODE (cmp0), sub_optab, cmp0, cmp1, 0, 0,
5230 OPTAB_DIRECT);
5231 }
5232
5233 /* Sign- or zero-extend OP0 and OP1 for integer comparisons. */
5234
5235 static void
5236 loongarch_extend_comparands (rtx_code code, rtx *op0, rtx *op1)
5237 {
5238 /* Comparisons consider all GRLEN bits, so extend sub-GRLEN values. */
5239 if (GET_MODE_SIZE (word_mode) > GET_MODE_SIZE (GET_MODE (*op0)))
5240 {
5241 /* It is more profitable to zero-extend QImode values. But not if the
5242 first operand has already been sign-extended, and the second one is
5243 is a constant or has already been sign-extended also. */
5244 if (unsigned_condition (code) == code
5245 && (GET_MODE (*op0) == QImode
5246 && ! (GET_CODE (*op0) == SUBREG
5247 && SUBREG_PROMOTED_VAR_P (*op0)
5248 && SUBREG_PROMOTED_SIGNED_P (*op0)
5249 && (CONST_INT_P (*op1)
5250 || (GET_CODE (*op1) == SUBREG
5251 && SUBREG_PROMOTED_VAR_P (*op1)
5252 && SUBREG_PROMOTED_SIGNED_P (*op1))))))
5253 {
5254 *op0 = gen_rtx_ZERO_EXTEND (word_mode, *op0);
5255 if (CONST_INT_P (*op1))
5256 *op1 = GEN_INT ((uint8_t) INTVAL (*op1));
5257 else
5258 *op1 = gen_rtx_ZERO_EXTEND (word_mode, *op1);
5259 }
5260 else
5261 {
5262 *op0 = gen_rtx_SIGN_EXTEND (word_mode, *op0);
5263 if (*op1 != const0_rtx)
5264 *op1 = gen_rtx_SIGN_EXTEND (word_mode, *op1);
5265 }
5266 }
5267 }
5268
5269
5270 /* Convert a comparison into something that can be used in a branch. On
5271 entry, *OP0 and *OP1 are the values being compared and *CODE is the code
5272 used to compare them. Update them to describe the final comparison. */
5273
5274 static void
5275 loongarch_emit_int_compare (enum rtx_code *code, rtx *op0, rtx *op1)
5276 {
5277 static const enum rtx_code
5278 mag_comparisons[][2] = {{LEU, LTU}, {GTU, GEU}, {LE, LT}, {GT, GE}};
5279
5280 if (splittable_const_int_operand (*op1, VOIDmode))
5281 {
5282 HOST_WIDE_INT rhs = INTVAL (*op1);
5283
5284 if (*code == EQ || *code == NE)
5285 {
5286 /* Convert e.g. OP0 == 2048 into OP0 - 2048 == 0. */
5287 if (IMM12_OPERAND (-rhs))
5288 {
5289 *op0 = loongarch_force_binary (GET_MODE (*op0), PLUS, *op0,
5290 GEN_INT (-rhs));
5291 *op1 = const0_rtx;
5292 }
5293 }
5294 else
5295 {
5296 /* Convert e.g. (OP0 <= 0xFFF) into (OP0 < 0x1000). */
5297 for (size_t i = 0; i < ARRAY_SIZE (mag_comparisons); i++)
5298 {
5299 HOST_WIDE_INT new_rhs;
5300 bool increment = *code == mag_comparisons[i][0];
5301 bool decrement = *code == mag_comparisons[i][1];
5302 if (!increment && !decrement)
5303 continue;
5304
5305 if ((increment && rhs == HOST_WIDE_INT_MAX)
5306 || (decrement && rhs == HOST_WIDE_INT_MIN))
5307 break;
5308
5309 new_rhs = rhs + (increment ? 1 : -1);
5310 if (loongarch_integer_cost (new_rhs)
5311 < loongarch_integer_cost (rhs))
5312 {
5313 *op1 = GEN_INT (new_rhs);
5314 *code = mag_comparisons[i][increment];
5315 }
5316 break;
5317 }
5318 }
5319 }
5320
5321 loongarch_extend_comparands (*code, op0, op1);
5322
5323 *op0 = force_reg (word_mode, *op0);
5324 if (*op1 != const0_rtx)
5325 *op1 = force_reg (word_mode, *op1);
5326 }
5327
5328 /* Like loongarch_emit_int_compare, but for floating-point comparisons. */
5329
5330 static void
5331 loongarch_emit_float_compare (enum rtx_code *code, rtx *op0, rtx *op1)
5332 {
5333 rtx cmp_op0 = *op0;
5334 rtx cmp_op1 = *op1;
5335
5336 /* Floating-point tests use a separate FCMP.cond.fmt
5337 comparison to set a register. The branch or conditional move will
5338 then compare that register against zero.
5339
5340 Set CMP_CODE to the code of the comparison instruction and
5341 *CODE to the code that the branch or move should use. */
5342 enum rtx_code cmp_code = *code;
5343 /* Three FP conditions cannot be implemented by reversing the
5344 operands for FCMP.cond.fmt, instead a reversed condition code is
5345 required and a test for false. */
5346 *code = NE;
5347 *op0 = gen_reg_rtx (FCCmode);
5348
5349 *op1 = const0_rtx;
5350 loongarch_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
5351 }
5352
5353 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
5354 and OPERAND[3]. Store the result in OPERANDS[0].
5355
5356 On 64-bit targets, the mode of the comparison and target will always be
5357 SImode, thus possibly narrower than that of the comparison's operands. */
5358
5359 void
5360 loongarch_expand_scc (rtx operands[])
5361 {
5362 rtx target = operands[0];
5363 enum rtx_code code = GET_CODE (operands[1]);
5364 rtx op0 = operands[2];
5365 rtx op1 = operands[3];
5366
5367 loongarch_extend_comparands (code, &op0, &op1);
5368 op0 = force_reg (word_mode, op0);
5369
5370 gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
5371
5372 if (code == EQ || code == NE)
5373 {
5374 rtx zie = loongarch_zero_if_equal (op0, op1);
5375 loongarch_emit_binary (code, target, zie, const0_rtx);
5376 }
5377 else
5378 loongarch_emit_int_order_test (code, 0, target, op0, op1);
5379 }
5380
5381 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
5382 CODE and jump to OPERANDS[3] if the condition holds. */
5383
5384 void
5385 loongarch_expand_conditional_branch (rtx *operands)
5386 {
5387 enum rtx_code code = GET_CODE (operands[0]);
5388 rtx op0 = operands[1];
5389 rtx op1 = operands[2];
5390 rtx condition;
5391
5392 if (FLOAT_MODE_P (GET_MODE (op1)))
5393 loongarch_emit_float_compare (&code, &op0, &op1);
5394 else
5395 loongarch_emit_int_compare (&code, &op0, &op1);
5396
5397 condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
5398 emit_jump_insn (gen_condjump (condition, operands[3]));
5399 }
5400
5401 /* Perform the comparison in OPERANDS[1]. Move OPERANDS[2] into OPERANDS[0]
5402 if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0]. */
5403
5404 void
5405 loongarch_expand_conditional_move (rtx *operands)
5406 {
5407 enum rtx_code code = GET_CODE (operands[1]);
5408 rtx op0 = XEXP (operands[1], 0);
5409 rtx op1 = XEXP (operands[1], 1);
5410 rtx op0_extend = op0;
5411 rtx op1_extend = op1;
5412
5413 /* Record whether operands[2] and operands[3] modes are promoted to word_mode. */
5414 bool promote_op[2] = {false, false};
5415 bool promote_p = false;
5416 machine_mode mode = GET_MODE (operands[0]);
5417
5418 if (FLOAT_MODE_P (GET_MODE (op1)))
5419 loongarch_emit_float_compare (&code, &op0, &op1);
5420 else
5421 {
5422 /* Optimize to reduce the number of instructions for ternary operations.
5423 Mainly implemented based on noce_try_cmove_arith.
5424 For dest = (condition) ? value_if_true : value_if_false;
5425 the optimization requires:
5426 a. value_if_false = var;
5427 b. value_if_true = var OP C (a positive integer power of 2).
5428
5429 Situations similar to the following:
5430 if (condition)
5431 dest += 1 << imm;
5432 to:
5433 dest += (condition ? 1 : 0) << imm; */
5434
5435 rtx_insn *insn;
5436 HOST_WIDE_INT val = 0; /* The value of rtx C. */
5437 /* INSN with operands[2] as the output. */
5438 rtx_insn *value_if_true_insn = NULL;
5439 /* INSN with operands[3] as the output. */
5440 rtx_insn *value_if_false_insn = NULL;
5441 rtx value_if_true_insn_src = NULL_RTX;
5442 /* Common operand var in value_if_true and value_if_false. */
5443 rtx comm_var = NULL_RTX;
5444 bool can_be_optimized = false;
5445
5446 /* Search value_if_true_insn and value_if_false_insn. */
5447 struct sequence_stack *seq = get_current_sequence ()->next;
5448 for (insn = seq->last; insn; insn = PREV_INSN (insn))
5449 {
5450 if (single_set (insn))
5451 {
5452 rtx set_dest = SET_DEST (single_set (insn));
5453 if (rtx_equal_p (set_dest, operands[2]))
5454 value_if_true_insn = insn;
5455 else if (rtx_equal_p (set_dest, operands[3]))
5456 value_if_false_insn = insn;
5457 if (value_if_true_insn && value_if_false_insn)
5458 break;
5459 }
5460 }
5461
5462 auto is_binary_op_0_keep_orig = [](enum rtx_code code)
5463 {
5464 switch (code)
5465 {
5466 case PLUS:
5467 case MINUS:
5468 case IOR:
5469 case XOR:
5470 case ROTATE:
5471 case ROTATERT:
5472 case ASHIFT:
5473 case ASHIFTRT:
5474 case LSHIFTRT:
5475 return true;
5476 default:
5477 return false;
5478 }
5479 };
5480
5481 /* Check if the optimization conditions are met. */
5482 if (value_if_true_insn
5483 && value_if_false_insn
5484 /* Make sure that the orig value OP 0 keep orig. */
5485 && (value_if_true_insn_src
5486 = SET_SRC (single_set (value_if_true_insn)))
5487 && is_binary_op_0_keep_orig ( GET_CODE (value_if_true_insn_src))
5488 /* Make sure that both value_if_true and value_if_false
5489 has the same var. */
5490 && rtx_equal_p (XEXP (value_if_true_insn_src, 0),
5491 SET_SRC (single_set (value_if_false_insn))))
5492 {
5493 comm_var = SET_SRC (single_set (value_if_false_insn));
5494 rtx src = XEXP (value_if_true_insn_src, 1);
5495 rtx imm = NULL_RTX;
5496 if (CONST_INT_P (src))
5497 imm = src;
5498 else
5499 for (insn = seq->last; insn; insn = PREV_INSN (insn))
5500 {
5501 rtx set = single_set (insn);
5502 if (set && rtx_equal_p (SET_DEST (set), src))
5503 {
5504 imm = SET_SRC (set);
5505 break;
5506 }
5507 }
5508 if (imm && CONST_INT_P (imm))
5509 {
5510 val = INTVAL (imm);
5511 /* Make sure that imm is a positive integer power of 2. */
5512 if (val > 0 && !(val & (val - 1)))
5513 can_be_optimized = true;
5514 }
5515 }
5516
5517 if (GET_MODE_SIZE (GET_MODE (op0)) < UNITS_PER_WORD)
5518 {
5519 promote_op[0] = (REG_P (op0) && REG_P (operands[2]) &&
5520 REGNO (op0) == REGNO (operands[2]));
5521 promote_op[1] = (REG_P (op1) && REG_P (operands[3]) &&
5522 REGNO (op1) == REGNO (operands[3]));
5523 }
5524
5525 if (promote_op[0] || promote_op[1])
5526 {
5527 mode = word_mode;
5528 promote_p = true;
5529 }
5530
5531 loongarch_extend_comparands (code, &op0, &op1);
5532
5533 op0 = force_reg (word_mode, op0);
5534 op0_extend = op0;
5535 op1_extend = force_reg (word_mode, op1);
5536
5537 rtx target = gen_reg_rtx (GET_MODE (op0));
5538
5539 if (code == EQ || code == NE)
5540 {
5541 op0 = loongarch_zero_if_equal (op0, op1);
5542 op1 = const0_rtx;
5543 /* For EQ, set target to 1 if op0 and op1 are the same,
5544 otherwise set to 0.
5545 For NE, set target to 0 if op0 and op1 are the same,
5546 otherwise set to 1. */
5547 if (can_be_optimized)
5548 loongarch_emit_binary (code, target, op0, const0_rtx);
5549 }
5550 else
5551 {
5552 /* The comparison needs a separate scc instruction. Store the
5553 result of the scc in *OP0 and compare it against zero. */
5554 bool invert = false;
5555 loongarch_emit_int_order_test (code, &invert, target, op0, op1);
5556 if (can_be_optimized && invert)
5557 loongarch_emit_binary (EQ, target, target, const0_rtx);
5558 code = invert ? EQ : NE;
5559 op0 = target;
5560 op1 = const0_rtx;
5561 }
5562
5563 if (can_be_optimized)
5564 {
5565 /* Perform (condition ? 1 : 0) << log2 (C). */
5566 loongarch_emit_binary (ASHIFT, target, target,
5567 GEN_INT (exact_log2 (val)));
5568 /* Shift-related insn patterns only support SImode operands[2]. */
5569 enum rtx_code opcode = GET_CODE (value_if_true_insn_src);
5570 if (opcode == ASHIFT || opcode == ASHIFTRT || opcode == LSHIFTRT
5571 || opcode == ROTATE || opcode == ROTATERT)
5572 target = gen_lowpart (SImode, target);
5573 /* Perform target = target OP ((condition ? 1 : 0) << log2 (C)). */
5574 loongarch_emit_binary (opcode, operands[0],
5575 force_reg (GET_MODE (operands[3]), comm_var),
5576 target);
5577 return;
5578 }
5579 }
5580
5581 rtx cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
5582 /* There is no direct support for general conditional GP move involving
5583 two registers using SEL. */
5584 if (INTEGRAL_MODE_P (GET_MODE (operands[2]))
5585 && register_operand (operands[2], VOIDmode)
5586 && register_operand (operands[3], VOIDmode))
5587 {
5588 rtx op2 = operands[2];
5589 rtx op3 = operands[3];
5590
5591 if (promote_p)
5592 {
5593 if (promote_op[0])
5594 op2 = op0_extend;
5595 else
5596 {
5597 loongarch_extend_comparands (code, &op2, &const0_rtx);
5598 op2 = force_reg (mode, op2);
5599 }
5600
5601 if (promote_op[1])
5602 op3 = op1_extend;
5603 else
5604 {
5605 loongarch_extend_comparands (code, &op3, &const0_rtx);
5606 op3 = force_reg (mode, op3);
5607 }
5608 }
5609
5610 rtx temp = gen_reg_rtx (mode);
5611 rtx temp2 = gen_reg_rtx (mode);
5612
5613 emit_insn (gen_rtx_SET (temp,
5614 gen_rtx_IF_THEN_ELSE (mode, cond,
5615 op2, const0_rtx)));
5616
5617 /* Flip the test for the second operand. */
5618 cond = gen_rtx_fmt_ee ((code == EQ) ? NE : EQ, GET_MODE (op0), op0, op1);
5619
5620 emit_insn (gen_rtx_SET (temp2,
5621 gen_rtx_IF_THEN_ELSE (mode, cond,
5622 op3, const0_rtx)));
5623
5624 /* Merge the two results, at least one is guaranteed to be zero. */
5625 if (promote_p)
5626 {
5627 rtx temp3 = gen_reg_rtx (mode);
5628 emit_insn (gen_rtx_SET (temp3, gen_rtx_IOR (mode, temp, temp2)));
5629 temp3 = gen_lowpart (GET_MODE (operands[0]), temp3);
5630 /* Nonzero in a subreg if it was made when accessing an object that
5631 was promoted to a wider mode in accord with the PROMOTED_MODE
5632 machine description macro. */
5633 SUBREG_PROMOTED_VAR_P (temp3) = 1;
5634 /* Sets promoted mode for SUBREG_PROMOTED_VAR_P. */
5635 SUBREG_PROMOTED_SET (temp3, SRP_SIGNED);
5636 loongarch_emit_move (operands[0], temp3);
5637 }
5638 else
5639 emit_insn (gen_rtx_SET (operands[0], gen_rtx_IOR (mode, temp, temp2)));
5640 }
5641 else
5642 emit_insn (gen_rtx_SET (operands[0],
5643 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
5644 operands[2], operands[3])));
5645 }
5646
5647 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
5648
5649 static void
5650 loongarch_va_start (tree valist, rtx nextarg)
5651 {
5652 nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
5653 std_expand_builtin_va_start (valist, nextarg);
5654 }
5655
5656 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
5657
5658 static bool
5659 loongarch_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
5660 tree exp ATTRIBUTE_UNUSED)
5661 {
5662 /* Always OK. */
5663 return true;
5664 }
5665
5666 static machine_mode
5667 loongarch_mode_for_move_size (HOST_WIDE_INT size)
5668 {
5669 switch (size)
5670 {
5671 case 32:
5672 return V32QImode;
5673 case 16:
5674 return V16QImode;
5675 }
5676
5677 return int_mode_for_size (size * BITS_PER_UNIT, 0).require ();
5678 }
5679
5680 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
5681 Assume that the areas do not overlap. */
5682
5683 static void
5684 loongarch_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length,
5685 HOST_WIDE_INT delta)
5686 {
5687 HOST_WIDE_INT offs, delta_cur;
5688 int i;
5689 machine_mode mode;
5690 rtx *regs;
5691
5692 /* Calculate how many registers we'll need for the block move.
5693 We'll emit length / delta move operations with delta as the size
5694 first. Then we may still have length % delta bytes not copied.
5695 We handle these remaining bytes by move operations with smaller
5696 (halfed) sizes. For example, if length = 21 and delta = 8, we'll
5697 emit two ld.d/st.d pairs, one ld.w/st.w pair, and one ld.b/st.b
5698 pair. For each load/store pair we use a dedicated register to keep
5699 the pipeline as populated as possible. */
5700 gcc_assert (pow2p_hwi (delta));
5701 HOST_WIDE_INT num_reg = length / delta + popcount_hwi (length % delta);
5702
5703 /* Allocate a buffer for the temporary registers. */
5704 regs = XALLOCAVEC (rtx, num_reg);
5705
5706 for (delta_cur = delta, i = 0, offs = 0; offs < length; delta_cur /= 2)
5707 {
5708 mode = loongarch_mode_for_move_size (delta_cur);
5709
5710 for (; offs + delta_cur <= length; offs += delta_cur, i++)
5711 {
5712 regs[i] = gen_reg_rtx (mode);
5713 loongarch_emit_move (regs[i], adjust_address (src, mode, offs));
5714 }
5715 }
5716
5717 for (delta_cur = delta, i = 0, offs = 0; offs < length; delta_cur /= 2)
5718 {
5719 mode = loongarch_mode_for_move_size (delta_cur);
5720
5721 for (; offs + delta_cur <= length; offs += delta_cur, i++)
5722 loongarch_emit_move (adjust_address (dest, mode, offs), regs[i]);
5723 }
5724 }
5725
5726 /* Helper function for doing a loop-based block operation on memory
5727 reference MEM. Each iteration of the loop will operate on LENGTH
5728 bytes of MEM.
5729
5730 Create a new base register for use within the loop and point it to
5731 the start of MEM. Create a new memory reference that uses this
5732 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
5733
5734 static void
5735 loongarch_adjust_block_mem (rtx mem, HOST_WIDE_INT length, rtx *loop_reg,
5736 rtx *loop_mem)
5737 {
5738 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
5739
5740 /* Although the new mem does not refer to a known location,
5741 it does keep up to LENGTH bytes of alignment. */
5742 *loop_mem = change_address (mem, BLKmode, *loop_reg);
5743 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
5744 }
5745
5746 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
5747 bytes at a time. LENGTH must be at least BYTES_PER_ITER. Assume that
5748 the memory regions do not overlap. */
5749
5750 static void
5751 loongarch_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
5752 HOST_WIDE_INT align)
5753 {
5754 rtx_code_label *label;
5755 rtx src_reg, dest_reg, final_src, test;
5756 HOST_WIDE_INT bytes_per_iter = align * LARCH_MAX_MOVE_OPS_PER_LOOP_ITER;
5757 HOST_WIDE_INT leftover;
5758
5759 leftover = length % bytes_per_iter;
5760 length -= leftover;
5761
5762 /* Create registers and memory references for use within the loop. */
5763 loongarch_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
5764 loongarch_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
5765
5766 /* Calculate the value that SRC_REG should have after the last iteration
5767 of the loop. */
5768 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length), 0,
5769 0, OPTAB_WIDEN);
5770
5771 /* Emit the start of the loop. */
5772 label = gen_label_rtx ();
5773 emit_label (label);
5774
5775 /* Emit the loop body. */
5776 loongarch_block_move_straight (dest, src, bytes_per_iter, align);
5777
5778 /* Move on to the next block. */
5779 loongarch_emit_move (src_reg,
5780 plus_constant (Pmode, src_reg, bytes_per_iter));
5781 loongarch_emit_move (dest_reg,
5782 plus_constant (Pmode, dest_reg, bytes_per_iter));
5783
5784 /* Emit the loop condition. */
5785 test = gen_rtx_NE (VOIDmode, src_reg, final_src);
5786 if (Pmode == DImode)
5787 emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
5788 else
5789 emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
5790
5791 /* Mop up any left-over bytes. */
5792 if (leftover)
5793 loongarch_block_move_straight (dest, src, leftover, align);
5794 else
5795 /* Temporary fix for PR79150. */
5796 emit_insn (gen_nop ());
5797 }
5798
5799 /* Expand a cpymemsi instruction, which copies LENGTH bytes from
5800 memory reference SRC to memory reference DEST. */
5801
5802 bool
5803 loongarch_expand_block_move (rtx dest, rtx src, rtx r_length, rtx r_align)
5804 {
5805 if (!CONST_INT_P (r_length))
5806 return false;
5807
5808 HOST_WIDE_INT length = INTVAL (r_length);
5809 if (length > la_max_inline_memcpy_size)
5810 return false;
5811
5812 HOST_WIDE_INT align = INTVAL (r_align);
5813
5814 if (!TARGET_STRICT_ALIGN || align > LARCH_MAX_MOVE_PER_INSN)
5815 align = LARCH_MAX_MOVE_PER_INSN;
5816
5817 if (length <= align * LARCH_MAX_MOVE_OPS_STRAIGHT)
5818 {
5819 loongarch_block_move_straight (dest, src, length, align);
5820 return true;
5821 }
5822
5823 if (optimize)
5824 {
5825 loongarch_block_move_loop (dest, src, length, align);
5826 return true;
5827 }
5828
5829 return false;
5830 }
5831
5832 /* Return true if loongarch_expand_block_move is the preferred
5833 implementation of the 'cpymemsi' template. */
5834
5835 bool
5836 loongarch_do_optimize_block_move_p (void)
5837 {
5838 /* if -m[no-]memcpy is given explicitly. */
5839 if (target_flags_explicit & MASK_MEMCPY)
5840 return !TARGET_MEMCPY;
5841
5842 /* if not, don't optimize under -Os. */
5843 return !optimize_size;
5844 }
5845
5846 /* Expand a QI or HI mode atomic memory operation.
5847
5848 GENERATOR contains a pointer to the gen_* function that generates
5849 the SI mode underlying atomic operation using masks that we
5850 calculate.
5851
5852 RESULT is the return register for the operation. Its value is NULL
5853 if unused.
5854
5855 MEM is the location of the atomic access.
5856
5857 OLDVAL is the first operand for the operation.
5858
5859 NEWVAL is the optional second operand for the operation. Its value
5860 is NULL if unused. */
5861
5862 void
5863 loongarch_expand_atomic_qihi (union loongarch_gen_fn_ptrs generator,
5864 rtx result, rtx mem, rtx oldval, rtx newval,
5865 rtx model)
5866 {
5867 rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
5868 rtx unshifted_mask_reg, mask, inverted_mask, si_op;
5869 rtx res = NULL;
5870 machine_mode mode;
5871
5872 mode = GET_MODE (mem);
5873
5874 /* Compute the address of the containing SImode value. */
5875 orig_addr = force_reg (Pmode, XEXP (mem, 0));
5876 memsi_addr = loongarch_force_binary (Pmode, AND, orig_addr,
5877 force_reg (Pmode, GEN_INT (-4)));
5878
5879 /* Create a memory reference for it. */
5880 memsi = gen_rtx_MEM (SImode, memsi_addr);
5881 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
5882 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
5883
5884 /* Work out the byte offset of the QImode or HImode value,
5885 counting from the least significant byte. */
5886 shift = loongarch_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
5887 /* Multiply by eight to convert the shift value from bytes to bits. */
5888 loongarch_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
5889
5890 /* Make the final shift an SImode value, so that it can be used in
5891 SImode operations. */
5892 shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
5893
5894 /* Set MASK to an inclusive mask of the QImode or HImode value. */
5895 unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
5896 unshifted_mask_reg = force_reg (SImode, unshifted_mask);
5897 mask = loongarch_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
5898
5899 /* Compute the equivalent exclusive mask. */
5900 inverted_mask = gen_reg_rtx (SImode);
5901 emit_insn (gen_rtx_SET (inverted_mask, gen_rtx_NOT (SImode, mask)));
5902
5903 /* Shift the old value into place. */
5904 if (oldval != const0_rtx)
5905 {
5906 oldval = convert_modes (SImode, mode, oldval, true);
5907 oldval = force_reg (SImode, oldval);
5908 oldval = loongarch_force_binary (SImode, ASHIFT, oldval, shiftsi);
5909 }
5910
5911 /* Do the same for the new value. */
5912 if (newval && newval != const0_rtx)
5913 {
5914 newval = convert_modes (SImode, mode, newval, true);
5915 newval = force_reg (SImode, newval);
5916 newval = loongarch_force_binary (SImode, ASHIFT, newval, shiftsi);
5917 }
5918
5919 /* Do the SImode atomic access. */
5920 if (result)
5921 res = gen_reg_rtx (SImode);
5922
5923 if (newval)
5924 si_op = generator.fn_7 (res, memsi, mask, inverted_mask, oldval, newval,
5925 model);
5926 else if (result)
5927 si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, model);
5928 else
5929 si_op = generator.fn_5 (memsi, mask, inverted_mask, oldval, model);
5930
5931 emit_insn (si_op);
5932
5933 if (result)
5934 {
5935 /* Shift and convert the result. */
5936 loongarch_emit_binary (AND, res, res, mask);
5937 loongarch_emit_binary (LSHIFTRT, res, res, shiftsi);
5938 loongarch_emit_move (result, gen_lowpart (GET_MODE (result), res));
5939 }
5940 }
5941
5942 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
5943 source of an "ext" instruction or the destination of an "ins"
5944 instruction. OP must be a register operand and the following
5945 conditions must hold:
5946
5947 0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
5948 0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
5949 0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
5950
5951 Also reject lengths equal to a word as they are better handled
5952 by the move patterns. */
5953
5954 bool
5955 loongarch_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
5956 {
5957 if (!register_operand (op, VOIDmode)
5958 || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
5959 return false;
5960
5961 if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
5962 return false;
5963
5964 if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
5965 return false;
5966
5967 return true;
5968 }
5969
5970 /* Predicate for pre-reload splitters with associated instructions,
5971 which can match any time before the split1 pass (usually combine),
5972 then are unconditionally split in that pass and should not be
5973 matched again afterwards. */
5974
5975 bool loongarch_pre_reload_split (void)
5976 {
5977 return (can_create_pseudo_p ()
5978 && !(cfun->curr_properties & PROP_rtl_split_insns));
5979 }
5980
5981 /* Check if we can use bstrins.<d> for
5982 op0 = (op1 & op2) | (op3 & op4)
5983 where op0, op1, op3 are regs, and op2, op4 are integer constants. */
5984 int
5985 loongarch_use_bstrins_for_ior_with_mask (machine_mode mode, rtx *op)
5986 {
5987 return loongarch_use_bstrins_for_ior_with_mask_1 (mode,
5988 UINTVAL (op[2]),
5989 UINTVAL (op[4]));
5990 }
5991
5992 /* Rewrite a MEM for simple load/store under -mexplicit-relocs=auto
5993 -mcmodel={normal/medium}. */
5994 rtx
5995 loongarch_rewrite_mem_for_simple_ldst (rtx mem)
5996 {
5997 rtx addr = XEXP (mem, 0);
5998 rtx hi = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr),
5999 UNSPEC_PCALAU12I_GR);
6000 rtx new_mem;
6001
6002 addr = gen_rtx_LO_SUM (Pmode, force_reg (Pmode, hi), addr);
6003 new_mem = gen_rtx_MEM (GET_MODE (mem), addr);
6004 MEM_COPY_ATTRIBUTES (new_mem, mem);
6005 return new_mem;
6006 }
6007
6008 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
6009 The punctuation characters are:
6010
6011 '.' Print the name of the register with a hard-wired zero (zero or $r0).
6012 '$' Print the name of the stack pointer register (sp or $r3).
6013
6014 See also loongarch_init_print_operand_punct. */
6015
6016 static void
6017 loongarch_print_operand_punctuation (FILE *file, int ch)
6018 {
6019 switch (ch)
6020 {
6021 case '.':
6022 fputs (reg_names[GP_REG_FIRST + 0], file);
6023 break;
6024
6025 case '$':
6026 fputs (reg_names[STACK_POINTER_REGNUM], file);
6027 break;
6028
6029 default:
6030 gcc_unreachable ();
6031 break;
6032 }
6033 }
6034
6035 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
6036 associated with condition CODE. Print the condition part of the
6037 opcode to FILE. */
6038
6039 static void
6040 loongarch_print_int_branch_condition (FILE *file, enum rtx_code code,
6041 int letter)
6042 {
6043 switch (code)
6044 {
6045 case EQ:
6046 case NE:
6047 case GT:
6048 case GE:
6049 case LT:
6050 case LE:
6051 case GTU:
6052 case GEU:
6053 case LTU:
6054 case LEU:
6055 /* Conveniently, the LoongArch names for these conditions are the same
6056 as their RTL equivalents. */
6057 fputs (GET_RTX_NAME (code), file);
6058 break;
6059
6060 default:
6061 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
6062 break;
6063 }
6064 }
6065
6066 /* Likewise floating-point branches. */
6067
6068 static void
6069 loongarch_print_float_branch_condition (FILE *file, enum rtx_code code,
6070 int letter)
6071 {
6072 switch (code)
6073 {
6074 case EQ:
6075 fputs ("ceqz", file);
6076 break;
6077
6078 case NE:
6079 fputs ("cnez", file);
6080 break;
6081
6082 default:
6083 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
6084 break;
6085 }
6086 }
6087
6088 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
6089
6090 static bool
6091 loongarch_print_operand_punct_valid_p (unsigned char code)
6092 {
6093 return loongarch_print_operand_punct[code];
6094 }
6095
6096 /* Return true if a FENCE should be emitted to before a memory access to
6097 implement the release portion of memory model MODEL. */
6098
6099 static bool
6100 loongarch_memmodel_needs_rel_acq_fence (enum memmodel model)
6101 {
6102 switch (memmodel_base (model))
6103 {
6104 case MEMMODEL_ACQ_REL:
6105 case MEMMODEL_SEQ_CST:
6106 case MEMMODEL_RELEASE:
6107 case MEMMODEL_ACQUIRE:
6108 return true;
6109
6110 case MEMMODEL_RELAXED:
6111 return false;
6112
6113 default:
6114 gcc_unreachable ();
6115 }
6116 }
6117
6118 /* Return true if a FENCE should be emitted after a failed CAS to
6119 implement the acquire semantic of failure_memorder. */
6120
6121 static bool
6122 loongarch_cas_failure_memorder_needs_acquire (enum memmodel model)
6123 {
6124 switch (memmodel_base (model))
6125 {
6126 case MEMMODEL_ACQUIRE:
6127 case MEMMODEL_ACQ_REL:
6128 case MEMMODEL_SEQ_CST:
6129 return true;
6130
6131 case MEMMODEL_RELAXED:
6132 case MEMMODEL_RELEASE:
6133 return false;
6134
6135 /* MEMMODEL_CONSUME is deliberately not handled because it's always
6136 replaced by MEMMODEL_ACQUIRE as at now. If you see an ICE caused by
6137 MEMMODEL_CONSUME, read the change (re)introducing it carefully and
6138 decide what to do. See PR 59448 and get_memmodel in builtins.cc. */
6139 default:
6140 gcc_unreachable ();
6141 }
6142 }
6143
6144 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
6145 in context CONTEXT. HI_RELOC indicates a high-part reloc. */
6146
6147 static void
6148 loongarch_print_operand_reloc (FILE *file, rtx op, bool hi64_part,
6149 bool hi_reloc)
6150 {
6151 const char *reloc;
6152 enum loongarch_symbol_type symbol_type =
6153 loongarch_classify_symbolic_expression (op);
6154
6155 if (loongarch_symbol_extreme_p (symbol_type))
6156 gcc_assert (la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE);
6157
6158 switch (symbol_type)
6159 {
6160 case SYMBOL_PCREL64:
6161 if (hi64_part)
6162 {
6163 reloc = hi_reloc ? "%pc64_hi12" : "%pc64_lo20";
6164 break;
6165 }
6166 /* fall through */
6167 case SYMBOL_PCREL:
6168 reloc = hi_reloc ? "%pc_hi20" : "%pc_lo12";
6169 break;
6170
6171 case SYMBOL_GOT_DISP:
6172 if (hi64_part)
6173 {
6174 if (TARGET_CMODEL_EXTREME)
6175 reloc = hi_reloc ? "%got64_pc_hi12" : "%got64_pc_lo20";
6176 else
6177 gcc_unreachable ();
6178 }
6179 else
6180 reloc = hi_reloc ? "%got_pc_hi20" : "%got_pc_lo12";
6181 break;
6182
6183 case SYMBOL_TLS_IE:
6184 if (hi64_part)
6185 {
6186 if (TARGET_CMODEL_EXTREME)
6187 reloc = hi_reloc ? "%ie64_pc_hi12" : "%ie64_pc_lo20";
6188 else
6189 gcc_unreachable ();
6190 }
6191 else
6192 reloc = hi_reloc ? "%ie_pc_hi20" : "%ie_pc_lo12";
6193 break;
6194
6195 case SYMBOL_TLS_LE:
6196 if (hi64_part)
6197 {
6198 if (TARGET_CMODEL_EXTREME)
6199 reloc = hi_reloc ? "%le64_hi12" : "%le64_lo20";
6200 else
6201 gcc_unreachable ();
6202 }
6203 else
6204 {
6205 if (HAVE_AS_TLS_LE_RELAXATION && !TARGET_CMODEL_EXTREME)
6206 reloc = hi_reloc ? "%le_hi20_r" : "%le_lo12_r";
6207 else
6208 reloc = hi_reloc ? "%le_hi20" : "%le_lo12";
6209 }
6210 break;
6211
6212 case SYMBOL_TLSGD:
6213 if (hi64_part)
6214 {
6215 if (TARGET_CMODEL_EXTREME)
6216 reloc = hi_reloc ? "%got64_pc_hi12" : "%got64_pc_lo20";
6217 else
6218 gcc_unreachable ();
6219 }
6220 else
6221 reloc = hi_reloc ? "%gd_pc_hi20" : "%got_pc_lo12";
6222 break;
6223
6224 case SYMBOL_TLSLDM:
6225 if (hi64_part)
6226 {
6227 if (TARGET_CMODEL_EXTREME)
6228 reloc = hi_reloc ? "%got64_pc_hi12" : "%got64_pc_lo20";
6229 else
6230 gcc_unreachable ();
6231 }
6232 else
6233 reloc = hi_reloc ? "%ld_pc_hi20" : "%got_pc_lo12";
6234 break;
6235
6236 default:
6237 gcc_unreachable ();
6238 }
6239
6240 fprintf (file, "%s(", reloc);
6241 output_addr_const (file, loongarch_strip_unspec_address (op));
6242 fputc (')', file);
6243 }
6244
6245 /* Implement TARGET_PRINT_OPERAND. The LoongArch-specific operand codes are:
6246
6247 'A' Print a _DB suffix if the memory model requires a release.
6248 'b' Print the address of a memory operand, without offset.
6249 'B' Print CONST_INT OP element 0 of a replicated CONST_VECTOR
6250 as an unsigned byte [0..255].
6251 'c' Print an integer.
6252 'C' Print the integer branch condition for comparison OP.
6253 'd' Print CONST_INT OP in decimal.
6254 'E' Print CONST_INT OP element 0 of a replicated CONST_VECTOR in decimal.
6255 'F' Print the FPU branch condition for comparison OP.
6256 'G' Print a DBAR insn for CAS failure (with an acquire semantic if
6257 needed, otherwise a simple load-load barrier).
6258 'H' Print address 52-61bit relocation associated with OP.
6259 'h' Print the high-part relocation associated with OP.
6260 'i' Print i if the operand is not a register.
6261 'L' Print the low-part relocation associated with OP.
6262 'm' Print one less than CONST_INT OP in decimal.
6263 'M' Print the indices of the lowest enabled bit and the highest
6264 enabled bit in a mask (for bstr* instructions).
6265 'N' Print the inverse of the integer branch condition for comparison OP.
6266 'Q' Print R_LARCH_RELAX for TLS IE.
6267 'r' Print address 12-31bit relocation associated with OP.
6268 'R' Print address 32-51bit relocation associated with OP.
6269 'T' Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
6270 'z' for (eq:?I ...), 'n' for (ne:?I ...).
6271 't' Like 'T', but with the EQ/NE cases reversed
6272 'u' Print a LASX register.
6273 'v' Print the insn size suffix b, h, w or d for vector modes V16QI, V8HI,
6274 V4SI, V2SI, and w, d for vector modes V4SF, V2DF respectively.
6275 'V' Print exact log2 of CONST_INT OP element 0 of a replicated
6276 CONST_VECTOR in decimal.
6277 'W' Print the inverse of the FPU branch condition for comparison OP.
6278 'w' Print a LSX register.
6279 'X' Print CONST_INT OP in hexadecimal format.
6280 'x' Print the low 16 bits of CONST_INT OP in hexadecimal format.
6281 'Y' Print loongarch_fp_conditions[INTVAL (OP)]
6282 'y' Print exact log2 of CONST_INT OP in decimal.
6283 'Z' Print OP and a comma for 8CC, otherwise print nothing.
6284 'z' Print $0 if OP is zero, otherwise print OP normally. */
6285
6286 static void
6287 loongarch_print_operand (FILE *file, rtx op, int letter)
6288 {
6289 enum rtx_code code;
6290
6291 if (loongarch_print_operand_punct_valid_p (letter))
6292 {
6293 loongarch_print_operand_punctuation (file, letter);
6294 return;
6295 }
6296
6297 gcc_assert (op);
6298 code = GET_CODE (op);
6299
6300 switch (letter)
6301 {
6302 case 'A':
6303 if (loongarch_memmodel_needs_rel_acq_fence ((enum memmodel) INTVAL (op)))
6304 fputs ("_db", file);
6305 break;
6306 case 'E':
6307 if (GET_CODE (op) == CONST_VECTOR)
6308 {
6309 gcc_assert (loongarch_const_vector_same_val_p (op, GET_MODE (op)));
6310 op = CONST_VECTOR_ELT (op, 0);
6311 gcc_assert (CONST_INT_P (op));
6312 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
6313 }
6314 else
6315 output_operand_lossage ("invalid use of '%%%c'", letter);
6316 break;
6317
6318
6319 case 'c':
6320 if (CONST_INT_P (op))
6321 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
6322 else
6323 output_operand_lossage ("unsupported operand for code '%c'", letter);
6324
6325 break;
6326
6327 case 'C':
6328 loongarch_print_int_branch_condition (file, code, letter);
6329 break;
6330
6331 case 'd':
6332 if (CONST_INT_P (op))
6333 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
6334 else
6335 output_operand_lossage ("invalid use of '%%%c'", letter);
6336 break;
6337
6338 case 'O':
6339 fprintf (file, "%s", INTVAL (XVECEXP (op, 0, 0)) ? "od" : "ev");
6340 break;
6341
6342 case 'F':
6343 loongarch_print_float_branch_condition (file, code, letter);
6344 break;
6345
6346 case 'G':
6347 if (loongarch_cas_failure_memorder_needs_acquire (
6348 memmodel_from_int (INTVAL (op))))
6349 fputs ("dbar\t0b10100", file);
6350 else if (!ISA_HAS_LD_SEQ_SA)
6351 fputs ("dbar\t0x700", file);
6352 break;
6353
6354 case 'h':
6355 if (code == HIGH)
6356 op = XEXP (op, 0);
6357 loongarch_print_operand_reloc (file, op, false /* hi64_part */,
6358 true /* hi_reloc */);
6359 break;
6360
6361 case 'H':
6362 loongarch_print_operand_reloc (file, op, true /* hi64_part */,
6363 true /* hi_reloc */);
6364 break;
6365
6366 case 'i':
6367 if (code != REG)
6368 fputs ("i", file);
6369 break;
6370
6371 case 'L':
6372 loongarch_print_operand_reloc (file, op, false /* hi64_part*/,
6373 false /* lo_reloc */);
6374 break;
6375 case 'B':
6376 if (GET_CODE (op) == CONST_VECTOR)
6377 {
6378 gcc_assert (loongarch_const_vector_same_val_p (op, GET_MODE (op)));
6379 op = CONST_VECTOR_ELT (op, 0);
6380 gcc_assert (CONST_INT_P (op));
6381 unsigned HOST_WIDE_INT val8 = UINTVAL (op) & GET_MODE_MASK (QImode);
6382 fprintf (file, HOST_WIDE_INT_PRINT_UNSIGNED, val8);
6383 }
6384 else
6385 output_operand_lossage ("invalid use of '%%%c'", letter);
6386 break;
6387
6388 case 'm':
6389 if (CONST_INT_P (op))
6390 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
6391 else
6392 output_operand_lossage ("invalid use of '%%%c'", letter);
6393 break;
6394
6395 case 'M':
6396 if (CONST_INT_P (op))
6397 {
6398 HOST_WIDE_INT mask = INTVAL (op);
6399 fprintf (file, "%d,%d", floor_log2 (mask), ctz_hwi (mask));
6400 }
6401 else
6402 output_operand_lossage ("invalid use of '%%%c'", letter);
6403 break;
6404
6405 case 'N':
6406 loongarch_print_int_branch_condition (file, reverse_condition (code),
6407 letter);
6408 break;
6409
6410 case 'Q':
6411 if (!TARGET_LINKER_RELAXATION)
6412 break;
6413
6414 if (code == HIGH)
6415 op = XEXP (op, 0);
6416
6417 if (loongarch_classify_symbolic_expression (op) == SYMBOL_TLS_IE)
6418 fprintf (file, ".reloc\t.,R_LARCH_RELAX\n\t");
6419
6420 break;
6421
6422 case 'r':
6423 loongarch_print_operand_reloc (file, op, false /* hi64_part */,
6424 true /* lo_reloc */);
6425 break;
6426
6427 case 'R':
6428 loongarch_print_operand_reloc (file, op, true /* hi64_part */,
6429 false /* lo_reloc */);
6430 break;
6431
6432 case 't':
6433 case 'T':
6434 {
6435 int truth = (code == NE) == (letter == 'T');
6436 fputc ("zfnt"[truth * 2 + FCC_REG_P (REGNO (XEXP (op, 0)))], file);
6437 }
6438 break;
6439
6440 case 'V':
6441 if (CONST_VECTOR_P (op))
6442 {
6443 machine_mode mode = GET_MODE_INNER (GET_MODE (op));
6444 rtx val_s = CONST_VECTOR_ELT (op, 0);
6445 if (CONST_INT_P (val_s))
6446 {
6447 unsigned HOST_WIDE_INT val = UINTVAL (val_s);
6448 int vlog2 = exact_log2 (val & GET_MODE_MASK (mode));
6449 if (vlog2 != -1)
6450 {
6451 fprintf (file, "%d", vlog2);
6452 break;
6453 }
6454 }
6455 }
6456 output_operand_lossage ("invalid use of '%%%c'", letter);
6457 break;
6458
6459 case 'W':
6460 loongarch_print_float_branch_condition (file, reverse_condition (code),
6461 letter);
6462 break;
6463
6464 case 'x':
6465 if (CONST_INT_P (op))
6466 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
6467 else
6468 output_operand_lossage ("invalid use of '%%%c'", letter);
6469 break;
6470
6471 case 'X':
6472 if (CONST_INT_P (op))
6473 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
6474 else
6475 output_operand_lossage ("invalid use of '%%%c'", letter);
6476 break;
6477
6478 case 'y':
6479 if (CONST_INT_P (op))
6480 {
6481 int val = exact_log2 (INTVAL (op));
6482 if (val != -1)
6483 fprintf (file, "%d", val);
6484 else
6485 output_operand_lossage ("invalid use of '%%%c'", letter);
6486 }
6487 else
6488 output_operand_lossage ("invalid use of '%%%c'", letter);
6489 break;
6490
6491 case 'Y':
6492 if (code == CONST_INT
6493 && UINTVAL (op) < ARRAY_SIZE (loongarch_fp_conditions))
6494 fputs (loongarch_fp_conditions[UINTVAL (op)], file);
6495 else
6496 output_operand_lossage ("'%%%c' is not a valid operand prefix",
6497 letter);
6498 break;
6499
6500 case 'Z':
6501 loongarch_print_operand (file, op, 0);
6502 fputc (',', file);
6503 break;
6504
6505 case 'w':
6506 if (code == REG && LSX_REG_P (REGNO (op)))
6507 fprintf (file, "$vr%s", &reg_names[REGNO (op)][2]);
6508 else
6509 output_operand_lossage ("invalid use of '%%%c'", letter);
6510 break;
6511
6512 case 'u':
6513 if (code == REG && LASX_REG_P (REGNO (op)))
6514 fprintf (file, "$xr%s", &reg_names[REGNO (op)][2]);
6515 else
6516 output_operand_lossage ("invalid use of '%%%c'", letter);
6517 break;
6518
6519 case 'v':
6520 switch (GET_MODE (op))
6521 {
6522 case E_V16QImode:
6523 case E_V32QImode:
6524 fprintf (file, "b");
6525 break;
6526 case E_V8HImode:
6527 case E_V16HImode:
6528 fprintf (file, "h");
6529 break;
6530 case E_V4SImode:
6531 case E_V4SFmode:
6532 case E_V8SImode:
6533 case E_V8SFmode:
6534 case E_SImode:
6535 fprintf (file, "w");
6536 break;
6537 case E_V2DImode:
6538 case E_V2DFmode:
6539 case E_V4DImode:
6540 case E_V4DFmode:
6541 case E_DImode:
6542 fprintf (file, "d");
6543 break;
6544 default:
6545 output_operand_lossage ("invalid use of '%%%c'", letter);
6546 }
6547 break;
6548
6549 default:
6550 switch (code)
6551 {
6552 case REG:
6553 {
6554 unsigned int regno = REGNO (op);
6555 if (letter && letter != 'z')
6556 output_operand_lossage ("invalid use of '%%%c'", letter);
6557 fprintf (file, "%s", reg_names[regno]);
6558 }
6559 break;
6560
6561 case MEM:
6562 if (letter == 'D')
6563 output_address (GET_MODE (op),
6564 plus_constant (Pmode, XEXP (op, 0), 4));
6565 else if (letter == 'b')
6566 {
6567 gcc_assert (REG_P (XEXP (op, 0)));
6568 loongarch_print_operand (file, XEXP (op, 0), 0);
6569 }
6570 else if (letter && letter != 'z')
6571 output_operand_lossage ("invalid use of '%%%c'", letter);
6572 else
6573 output_address (GET_MODE (op), XEXP (op, 0));
6574 break;
6575
6576 default:
6577 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
6578 fputs (reg_names[GP_REG_FIRST], file);
6579 else if (letter && letter != 'z')
6580 output_operand_lossage ("invalid use of '%%%c'", letter);
6581 else
6582 output_addr_const (file, loongarch_strip_unspec_address (op));
6583 break;
6584 }
6585 }
6586 }
6587
6588 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
6589
6590 static void
6591 loongarch_print_operand_address (FILE *file, machine_mode /* mode */, rtx x)
6592 {
6593 struct loongarch_address_info addr;
6594
6595 if (loongarch_classify_address (&addr, x, word_mode, true))
6596 switch (addr.type)
6597 {
6598 case ADDRESS_REG:
6599 fprintf (file, "%s,", reg_names[REGNO (addr.reg)]);
6600 loongarch_print_operand (file, addr.offset, 0);
6601 return;
6602
6603 case ADDRESS_REG_REG:
6604 fprintf (file, "%s,%s", reg_names[REGNO (addr.reg)],
6605 reg_names[REGNO (addr.offset)]);
6606 return;
6607
6608 case ADDRESS_LO_SUM:
6609 fprintf (file, "%s,", reg_names[REGNO (addr.reg)]);
6610 loongarch_print_operand_reloc (file, addr.offset, false /* hi64_part */,
6611 false /* hi_reloc */);
6612 return;
6613
6614 case ADDRESS_CONST_INT:
6615 fprintf (file, "%s,", reg_names[GP_REG_FIRST]);
6616 output_addr_const (file, x);
6617 return;
6618
6619 case ADDRESS_SYMBOLIC:
6620 output_addr_const (file, loongarch_strip_unspec_address (x));
6621 return;
6622 }
6623 if (CONST_INT_P (x))
6624 output_addr_const (file, x);
6625 else
6626 gcc_unreachable ();
6627 }
6628
6629 /* Implement TARGET_ASM_SELECT_RTX_SECTION. */
6630
6631 static section *
6632 loongarch_select_rtx_section (machine_mode mode, rtx x,
6633 unsigned HOST_WIDE_INT align)
6634 {
6635 /* ??? Consider using mergeable small data sections. */
6636 if (loongarch_rtx_constant_in_small_data_p (mode))
6637 return get_named_section (NULL, ".sdata", 0);
6638
6639 return default_elf_select_rtx_section (mode, x, align);
6640 }
6641
6642 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
6643
6644 The complication here is that jump tables will use absolute addresses,
6645 and should therefore not be included in the read-only part of a DSO.
6646 Handle such cases by selecting a normal data section instead of a
6647 read-only one. The logic apes that in default_function_rodata_section. */
6648
6649 static section *
6650 loongarch_function_rodata_section (tree decl, bool)
6651 {
6652 return default_function_rodata_section (decl, false);
6653 }
6654
6655 /* Implement TARGET_IN_SMALL_DATA_P. */
6656
6657 static bool
6658 loongarch_in_small_data_p (const_tree decl)
6659 {
6660 int size;
6661
6662 if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
6663 return false;
6664
6665 if (VAR_P (decl) && DECL_SECTION_NAME (decl) != 0)
6666 {
6667 const char *name;
6668
6669 /* Reject anything that isn't in a known small-data section. */
6670 name = DECL_SECTION_NAME (decl);
6671 if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
6672 return false;
6673
6674 /* If a symbol is defined externally, the assembler will use the
6675 usual -G rules when deciding how to implement macros. */
6676 if (!DECL_EXTERNAL (decl))
6677 return true;
6678 }
6679
6680 /* We have traditionally not treated zero-sized objects as small data,
6681 so this is now effectively part of the ABI. */
6682 size = int_size_in_bytes (TREE_TYPE (decl));
6683 return size > 0 && size <= g_switch_value;
6684 }
6685
6686 /* The LoongArch debug format wants all automatic variables and arguments
6687 to be in terms of the virtual frame pointer (stack pointer before
6688 any adjustment in the function), while the LoongArch linker wants
6689 the frame pointer to be the stack pointer after the initial
6690 adjustment. So, we do the adjustment here. The arg pointer (which
6691 is eliminated) points to the virtual frame pointer, while the frame
6692 pointer (which may be eliminated) points to the stack pointer after
6693 the initial adjustments. */
6694
6695 HOST_WIDE_INT
6696 loongarch_debugger_offset (rtx addr, HOST_WIDE_INT offset)
6697 {
6698 rtx offset2 = const0_rtx;
6699 rtx reg = eliminate_constant_term (addr, &offset2);
6700
6701 if (offset == 0)
6702 offset = INTVAL (offset2);
6703
6704 if (reg == stack_pointer_rtx
6705 || reg == frame_pointer_rtx
6706 || reg == hard_frame_pointer_rtx)
6707 {
6708 offset -= cfun->machine->frame.total_size;
6709 if (reg == hard_frame_pointer_rtx)
6710 offset += cfun->machine->frame.hard_frame_pointer_offset;
6711 }
6712
6713 return offset;
6714 }
6715
6716 /* Implement ASM_OUTPUT_EXTERNAL. */
6717
6718 void
6719 loongarch_output_external (FILE *file, tree decl, const char *name)
6720 {
6721 default_elf_asm_output_external (file, decl, name);
6722
6723 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
6724 set in order to avoid putting out names that are never really
6725 used. */
6726 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
6727 {
6728 if (loongarch_in_small_data_p (decl))
6729 {
6730 /* When using assembler macros, emit .extern directives for
6731 all small-data externs so that the assembler knows how
6732 big they are.
6733
6734 In most cases it would be safe (though pointless) to emit
6735 .externs for other symbols too. One exception is when an
6736 object is within the -G limit but declared by the user to
6737 be in a section other than .sbss or .sdata. */
6738 fputs ("\t.extern\t", file);
6739 assemble_name (file, name);
6740 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
6741 int_size_in_bytes (TREE_TYPE (decl)));
6742 }
6743 }
6744 }
6745
6746 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL. */
6747
6748 static void ATTRIBUTE_UNUSED
6749 loongarch_output_dwarf_dtprel (FILE *file, int size, rtx x)
6750 {
6751 switch (size)
6752 {
6753 case 4:
6754 fputs ("\t.dtprelword\t", file);
6755 break;
6756
6757 case 8:
6758 fputs ("\t.dtpreldword\t", file);
6759 break;
6760
6761 default:
6762 gcc_unreachable ();
6763 }
6764 output_addr_const (file, x);
6765 fputs ("+0x8000", file);
6766 }
6767
6768 /* Implement ASM_OUTPUT_ASCII. */
6769
6770 void
6771 loongarch_output_ascii (FILE *stream, const char *string, size_t len)
6772 {
6773 size_t i;
6774 int cur_pos;
6775
6776 cur_pos = 17;
6777 fprintf (stream, "\t.ascii\t\"");
6778 for (i = 0; i < len; i++)
6779 {
6780 int c;
6781
6782 c = (unsigned char) string[i];
6783 if (ISPRINT (c))
6784 {
6785 if (c == '\\' || c == '\"')
6786 {
6787 putc ('\\', stream);
6788 cur_pos++;
6789 }
6790 putc (c, stream);
6791 cur_pos++;
6792 }
6793 else
6794 {
6795 fprintf (stream, "\\%03o", c);
6796 cur_pos += 4;
6797 }
6798
6799 if (cur_pos > 72 && i + 1 < len)
6800 {
6801 cur_pos = 17;
6802 fprintf (stream, "\"\n\t.ascii\t\"");
6803 }
6804 }
6805 fprintf (stream, "\"\n");
6806 }
6807
6808 /* Implement TARGET_FRAME_POINTER_REQUIRED. */
6809
6810 static bool
6811 loongarch_frame_pointer_required (void)
6812 {
6813 /* If the function contains dynamic stack allocations, we need to
6814 use the frame pointer to access the static parts of the frame. */
6815 if (cfun->calls_alloca)
6816 return true;
6817
6818 return false;
6819 }
6820
6821 /* Implement TARGET_CAN_ELIMINATE. Make sure that we're not trying
6822 to eliminate to the wrong hard frame pointer. */
6823
6824 static bool
6825 loongarch_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
6826 {
6827 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
6828 }
6829
6830 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
6831 previous frame. */
6832
6833 rtx
6834 loongarch_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
6835 {
6836 if (count != 0)
6837 return const0_rtx;
6838
6839 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
6840 }
6841
6842 /* Emit code to change the current function's return address to
6843 ADDRESS. SCRATCH is available as a scratch register, if needed.
6844 ADDRESS and SCRATCH are both word-mode GPRs. */
6845
6846 void
6847 loongarch_set_return_address (rtx address, rtx scratch)
6848 {
6849 rtx slot_address;
6850
6851 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
6852
6853 if (frame_pointer_needed)
6854 slot_address = loongarch_add_offset (scratch, hard_frame_pointer_rtx,
6855 -UNITS_PER_WORD);
6856 else
6857 slot_address = loongarch_add_offset (scratch, stack_pointer_rtx,
6858 cfun->machine->frame.gp_sp_offset);
6859
6860 loongarch_emit_move (gen_frame_mem (GET_MODE (address), slot_address),
6861 address);
6862 }
6863
6864 /* Return true if register REGNO can store a value of mode MODE.
6865 The result of this function is cached in loongarch_hard_regno_mode_ok. */
6866
6867 static bool
6868 loongarch_hard_regno_mode_ok_uncached (unsigned int regno, machine_mode mode)
6869 {
6870 unsigned int size;
6871 enum mode_class mclass;
6872
6873 if (mode == FCCmode)
6874 return FCC_REG_P (regno) || GP_REG_P (regno) || FP_REG_P (regno);
6875
6876 size = GET_MODE_SIZE (mode);
6877 mclass = GET_MODE_CLASS (mode);
6878
6879 if (GP_REG_P (regno)
6880 && !LSX_SUPPORTED_MODE_P (mode)
6881 && !LASX_SUPPORTED_MODE_P (mode))
6882 return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
6883
6884 if (FP_REG_P (regno))
6885 {
6886 /* Allow 128-bit or 256-bit vector modes in all FPR. */
6887 if (LSX_SUPPORTED_MODE_P (mode)
6888 || LASX_SUPPORTED_MODE_P (mode))
6889 return true;
6890
6891 if (mclass == MODE_FLOAT
6892 || mclass == MODE_COMPLEX_FLOAT
6893 || mclass == MODE_VECTOR_FLOAT)
6894 return size <= UNITS_PER_HWFPVALUE;
6895
6896 /* Allow integer modes that fit into a single register. We need
6897 to put integers into FPRs when using instructions like CVT
6898 and TRUNC. There's no point allowing sizes smaller than a word,
6899 because the FPU has no appropriate load/store instructions. */
6900 if (mclass == MODE_INT)
6901 return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FP_REG;
6902 }
6903
6904 return false;
6905 }
6906
6907 /* Implement TARGET_HARD_REGNO_MODE_OK. */
6908
6909 static bool
6910 loongarch_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
6911 {
6912 return loongarch_hard_regno_mode_ok_p[mode][regno];
6913 }
6914
6915
6916 static bool
6917 loongarch_hard_regno_call_part_clobbered (unsigned int,
6918 unsigned int regno, machine_mode mode)
6919 {
6920 if (ISA_HAS_LSX && FP_REG_P (regno) && GET_MODE_SIZE (mode) > 8)
6921 return true;
6922
6923 return false;
6924 }
6925
6926 /* Implement TARGET_HARD_REGNO_NREGS. */
6927
6928 static unsigned int
6929 loongarch_hard_regno_nregs (unsigned int regno, machine_mode mode)
6930 {
6931 if (FCC_REG_P (regno))
6932 /* The size of FP status registers is always 4, because they only hold
6933 FCCmode values, and FCCmode is always considered to be 4 bytes wide. */
6934 return (GET_MODE_SIZE (mode) + 3) / 4;
6935
6936 if (FP_REG_P (regno))
6937 {
6938 if (LSX_SUPPORTED_MODE_P (mode))
6939 return 1;
6940
6941 if (LASX_SUPPORTED_MODE_P (mode))
6942 return 1;
6943
6944 return (GET_MODE_SIZE (mode) + UNITS_PER_FP_REG - 1) / UNITS_PER_FP_REG;
6945 }
6946
6947 /* All other registers are word-sized. */
6948 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6949 }
6950
6951 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
6952 in loongarch_hard_regno_nregs. */
6953
6954 int
6955 loongarch_class_max_nregs (enum reg_class rclass, machine_mode mode)
6956 {
6957 int size;
6958 HARD_REG_SET left;
6959
6960 size = 0x8000;
6961 left = reg_class_contents[rclass];
6962 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FCC_REGS]))
6963 {
6964 if (loongarch_hard_regno_mode_ok (FCC_REG_FIRST, mode))
6965 size = MIN (size, 4);
6966
6967 left &= ~reg_class_contents[FCC_REGS];
6968 }
6969 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
6970 {
6971 if (loongarch_hard_regno_mode_ok (FP_REG_FIRST, mode))
6972 {
6973 /* Fixed me. */
6974 if (LASX_SUPPORTED_MODE_P (mode))
6975 size = MIN (size, UNITS_PER_LASX_REG);
6976 else if (LSX_SUPPORTED_MODE_P (mode))
6977 size = MIN (size, UNITS_PER_LSX_REG);
6978 else
6979 size = MIN (size, UNITS_PER_FP_REG);
6980 }
6981 left &= ~reg_class_contents[FP_REGS];
6982 }
6983 if (!hard_reg_set_empty_p (left))
6984 size = MIN (size, UNITS_PER_WORD);
6985 return (GET_MODE_SIZE (mode) + size - 1) / size;
6986 }
6987
6988 /* Implement TARGET_CAN_CHANGE_MODE_CLASS. */
6989
6990 static bool
6991 loongarch_can_change_mode_class (machine_mode from, machine_mode to,
6992 reg_class_t rclass)
6993 {
6994 /* Allow conversions between different LSX/LASX vector modes. */
6995 if (LASX_SUPPORTED_MODE_P (from) && LASX_SUPPORTED_MODE_P (to))
6996 return true;
6997
6998 /* Allow conversions between different LSX vector modes. */
6999 if (LSX_SUPPORTED_MODE_P (from) && LSX_SUPPORTED_MODE_P (to))
7000 return true;
7001
7002 /* Allow conversion between LSX vector mode and scalar fp mode. */
7003 if ((LSX_SUPPORTED_MODE_P (from) && SCALAR_FLOAT_MODE_P (to))
7004 || ((SCALAR_FLOAT_MODE_P (from) && LSX_SUPPORTED_MODE_P (to))))
7005 return true;
7006
7007 return !reg_classes_intersect_p (FP_REGS, rclass);
7008 }
7009
7010 /* Return true if moves in mode MODE can use the FPU's fmov.fmt instruction,
7011 */
7012
7013 static bool
7014 loongarch_mode_ok_for_mov_fmt_p (machine_mode mode)
7015 {
7016 switch (mode)
7017 {
7018 case E_FCCmode:
7019 case E_SFmode:
7020 return TARGET_HARD_FLOAT;
7021
7022 case E_DFmode:
7023 return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
7024
7025 default:
7026 return ISA_HAS_LASX ? LASX_SUPPORTED_MODE_P (mode)
7027 : LSX_SUPPORTED_MODE_P (mode);
7028 }
7029 }
7030
7031 /* Implement TARGET_MODES_TIEABLE_P. */
7032
7033 static bool
7034 loongarch_modes_tieable_p (machine_mode mode1, machine_mode mode2)
7035 {
7036 /* FPRs allow no mode punning, so it's not worth tying modes if we'd
7037 prefer to put one of them in FPRs. */
7038 return (mode1 == mode2
7039 || (!loongarch_mode_ok_for_mov_fmt_p (mode1)
7040 && !loongarch_mode_ok_for_mov_fmt_p (mode2)));
7041 }
7042
7043 /* Implement TARGET_PREFERRED_RELOAD_CLASS. */
7044
7045 static reg_class_t
7046 loongarch_preferred_reload_class (rtx x, reg_class_t rclass)
7047 {
7048 if (reg_class_subset_p (FP_REGS, rclass)
7049 && loongarch_mode_ok_for_mov_fmt_p (GET_MODE (x)))
7050 return FP_REGS;
7051
7052 if (reg_class_subset_p (GR_REGS, rclass))
7053 rclass = GR_REGS;
7054
7055 return rclass;
7056 }
7057
7058 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
7059 Return a "canonical" class to represent it in later calculations. */
7060
7061 static reg_class_t
7062 loongarch_canonicalize_move_class (reg_class_t rclass)
7063 {
7064 if (reg_class_subset_p (rclass, GENERAL_REGS))
7065 rclass = GENERAL_REGS;
7066
7067 return rclass;
7068 }
7069
7070 /* Return the cost of moving a value from a register of class FROM to a GPR.
7071 Return 0 for classes that are unions of other classes handled by this
7072 function. */
7073
7074 static int
7075 loongarch_move_to_gpr_cost (reg_class_t from)
7076 {
7077 switch (from)
7078 {
7079 case GENERAL_REGS:
7080 /* MOVE macro. */
7081 return 2;
7082
7083 case FP_REGS:
7084 /* MOVFR2GR, etc. */
7085 return 4;
7086
7087 case FCC_REGS:
7088 return loongarch_cost->movcf2gr;
7089
7090 default:
7091 return 0;
7092 }
7093 }
7094
7095 /* Return the cost of moving a value from a GPR to a register of class TO.
7096 Return 0 for classes that are unions of other classes handled by this
7097 function. */
7098
7099 static int
7100 loongarch_move_from_gpr_cost (reg_class_t to)
7101 {
7102 switch (to)
7103 {
7104 case GENERAL_REGS:
7105 /*MOVE macro. */
7106 return 2;
7107
7108 case FP_REGS:
7109 /* MOVGR2FR, etc. */
7110 return 4;
7111
7112 case FCC_REGS:
7113 return loongarch_cost->movgr2cf;
7114
7115 default:
7116 return 0;
7117 }
7118 }
7119
7120 /* Implement TARGET_REGISTER_MOVE_COST. Return 0 for classes that are the
7121 maximum of the move costs for subclasses; regclass will work out
7122 the maximum for us. */
7123
7124 static int
7125 loongarch_register_move_cost (machine_mode mode, reg_class_t from,
7126 reg_class_t to)
7127 {
7128 reg_class_t dregs;
7129 int cost1, cost2;
7130
7131 from = loongarch_canonicalize_move_class (from);
7132 to = loongarch_canonicalize_move_class (to);
7133
7134 /* Handle moves that can be done without using general-purpose registers. */
7135 if (from == FP_REGS)
7136 {
7137 if (to == FP_REGS && loongarch_mode_ok_for_mov_fmt_p (mode))
7138 /* FMOV.FMT. */
7139 return 4;
7140 }
7141
7142 /* Handle cases in which only one class deviates from the ideal. */
7143 dregs = GENERAL_REGS;
7144 if (from == dregs)
7145 return loongarch_move_from_gpr_cost (to);
7146 if (to == dregs)
7147 return loongarch_move_to_gpr_cost (from);
7148
7149 /* fcc -> fcc, fcc -> fpr, or fpr -> fcc. */
7150 if (from == FCC_REGS || to == FCC_REGS)
7151 return COSTS_N_INSNS (from == to ? 2 : 1);
7152
7153 /* Handles cases that require a GPR temporary. */
7154 cost1 = loongarch_move_to_gpr_cost (from);
7155 if (cost1 != 0)
7156 {
7157 cost2 = loongarch_move_from_gpr_cost (to);
7158 if (cost2 != 0)
7159 return cost1 + cost2;
7160 }
7161
7162 return 0;
7163 }
7164
7165 /* Implement TARGET_MEMORY_MOVE_COST. */
7166
7167 static int
7168 loongarch_memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
7169 {
7170 return (loongarch_cost->memory_latency
7171 + memory_move_secondary_cost (mode, rclass, in));
7172 }
7173
7174 /* Return the register class required for a secondary register when
7175 copying between one of the registers in RCLASS and value X, which
7176 has mode MODE. X is the source of the move if IN_P, otherwise it
7177 is the destination. Return NO_REGS if no secondary register is
7178 needed. */
7179
7180 static reg_class_t
7181 loongarch_secondary_reload (bool in_p ATTRIBUTE_UNUSED, rtx x,
7182 reg_class_t rclass, machine_mode mode,
7183 secondary_reload_info *sri ATTRIBUTE_UNUSED)
7184 {
7185 int regno;
7186
7187 regno = true_regnum (x);
7188
7189 if (mode == FCCmode)
7190 {
7191 if (reg_class_subset_p (rclass, FCC_REGS) && !FP_REG_P (regno))
7192 {
7193 if (FCC_REG_P (regno))
7194 return FP_REGS;
7195
7196 auto fn = in_p ? loongarch_move_from_gpr_cost
7197 : loongarch_move_to_gpr_cost;
7198
7199 if (fn (FCC_REGS) > fn (FP_REGS) + COSTS_N_INSNS (1))
7200 return FP_REGS;
7201
7202 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
7203 }
7204
7205 if (reg_class_subset_p (rclass, GR_REGS) && FCC_REG_P (regno))
7206 {
7207 auto fn = in_p ? loongarch_move_to_gpr_cost
7208 : loongarch_move_from_gpr_cost;
7209
7210 if (fn (FCC_REGS) > fn (FP_REGS) + COSTS_N_INSNS (1))
7211 return FP_REGS;
7212
7213 return NO_REGS;
7214 }
7215
7216 if (reg_class_subset_p (rclass, FP_REGS)
7217 && (regno == -1 || MEM_P (x)))
7218 return GR_REGS;
7219
7220 return NO_REGS;
7221 }
7222
7223 if (reg_class_subset_p (rclass, FP_REGS))
7224 {
7225 if (regno < 0
7226 || (MEM_P (x)
7227 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)))
7228 /* In this case we can use lwc1, swc1, ldc1 or sdc1. We'll use
7229 pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported. */
7230 return NO_REGS;
7231
7232 if (MEM_P (x) && LSX_SUPPORTED_MODE_P (mode))
7233 /* In this case we can use LSX LD.* and ST.*. */
7234 return NO_REGS;
7235
7236 if (GP_REG_P (regno) || x == CONST0_RTX (mode))
7237 /* In this case we can use movgr2fr.s, movfr2gr.s, movgr2fr.d or
7238 * movfr2gr.d. */
7239 return NO_REGS;
7240
7241 if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
7242 /* We can force the constant to memory and use fld.s
7243 and fld.d. As above, we will use pairs of lwc1s if
7244 ldc1 is not supported. */
7245 return NO_REGS;
7246
7247 if (FP_REG_P (regno) && loongarch_mode_ok_for_mov_fmt_p (mode))
7248 /* In this case we can use fmov.{s/d}. */
7249 return NO_REGS;
7250
7251 /* Otherwise, we need to reload through an integer register. */
7252 return GR_REGS;
7253 }
7254 if (FP_REG_P (regno))
7255 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
7256
7257 return NO_REGS;
7258 }
7259
7260 /* Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS.
7261
7262 The register allocator chooses ALL_REGS if FP_REGS and GR_REGS have the
7263 same cost - even if ALL_REGS has a much higher cost. ALL_REGS is also used
7264 if the cost of both FP_REGS and GR_REGS is lower than the memory cost (in
7265 this case the best class is the lowest cost one). Using ALL_REGS
7266 irrespectively of itself cost results in bad allocations with many redundant
7267 int<->FP moves which are expensive on various cores.
7268
7269 To avoid this we don't allow ALL_REGS as the allocno class, but force a
7270 decision between FP_REGS and GR_REGS. We use the allocno class if it isn't
7271 ALL_REGS. Similarly, use the best class if it isn't ALL_REGS. Otherwise Set
7272 the allocno class depending on the mode.
7273
7274 This change has a similar effect to increasing the cost of FPR->GPR register
7275 moves for integer modes so that they are higher than the cost of memory but
7276 changing the allocno class is more reliable. */
7277
7278 static reg_class_t
7279 loongarch_ira_change_pseudo_allocno_class (int regno, reg_class_t allocno_class,
7280 reg_class_t best_class)
7281 {
7282 enum machine_mode mode;
7283
7284 if (allocno_class != ALL_REGS)
7285 return allocno_class;
7286
7287 if (best_class != ALL_REGS)
7288 return best_class;
7289
7290 mode = PSEUDO_REGNO_MODE (regno);
7291 return FLOAT_MODE_P (mode) || VECTOR_MODE_P (mode) ? FP_REGS : GR_REGS;
7292 }
7293
7294 /* Implement TARGET_VALID_POINTER_MODE. */
7295
7296 static bool
7297 loongarch_valid_pointer_mode (scalar_int_mode mode)
7298 {
7299 return mode == SImode || (TARGET_64BIT && mode == DImode);
7300 }
7301
7302 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P. */
7303
7304 static bool
7305 loongarch_vector_mode_supported_p (machine_mode mode)
7306 {
7307 return ISA_HAS_LASX ? LASX_SUPPORTED_MODE_P (mode)
7308 : LSX_SUPPORTED_MODE_P (mode);
7309 }
7310
7311 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P. */
7312
7313 static bool
7314 loongarch_scalar_mode_supported_p (scalar_mode mode)
7315 {
7316 if (ALL_FIXED_POINT_MODE_P (mode)
7317 && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
7318 return true;
7319
7320 return default_scalar_mode_supported_p (mode);
7321 }
7322
7323 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE. */
7324
7325 static machine_mode
7326 loongarch_preferred_simd_mode (scalar_mode mode)
7327 {
7328 if (!ISA_HAS_LSX)
7329 return word_mode;
7330
7331 switch (mode)
7332 {
7333 case E_QImode:
7334 return ISA_HAS_LASX ? E_V32QImode : E_V16QImode;
7335 case E_HImode:
7336 return ISA_HAS_LASX ? E_V16HImode : E_V8HImode;
7337 case E_SImode:
7338 return ISA_HAS_LASX ? E_V8SImode : E_V4SImode;
7339 case E_DImode:
7340 return ISA_HAS_LASX ? E_V4DImode : E_V2DImode;
7341
7342 case E_SFmode:
7343 return ISA_HAS_LASX ? E_V8SFmode : E_V4SFmode;
7344
7345 case E_DFmode:
7346 return ISA_HAS_LASX ? E_V4DFmode : E_V2DFmode;
7347
7348 default:
7349 break;
7350 }
7351 return word_mode;
7352 }
7353
7354 static unsigned int
7355 loongarch_autovectorize_vector_modes (vector_modes *modes, bool)
7356 {
7357 if (ISA_HAS_LASX)
7358 {
7359 modes->safe_push (V32QImode);
7360 modes->safe_push (V16QImode);
7361 }
7362 else if (ISA_HAS_LSX)
7363 {
7364 modes->safe_push (V16QImode);
7365 }
7366
7367 return 0;
7368 }
7369
7370 /* Return the assembly code for INSN, which has the operands given by
7371 OPERANDS, and which branches to OPERANDS[0] if some condition is true.
7372 BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
7373 is in range of a direct branch. BRANCH_IF_FALSE is an inverted
7374 version of BRANCH_IF_TRUE. */
7375
7376 const char *
7377 loongarch_output_conditional_branch (rtx_insn *insn, rtx *operands,
7378 const char *branch_if_true,
7379 const char *branch_if_false)
7380 {
7381 unsigned int length;
7382 rtx taken;
7383
7384 gcc_assert (LABEL_P (operands[0]));
7385
7386 length = get_attr_length (insn);
7387 if (length <= 4)
7388 {
7389 return branch_if_true;
7390 }
7391
7392 /* Generate a reversed branch around a direct jump. */
7393 rtx_code_label *not_taken = gen_label_rtx ();
7394 taken = operands[0];
7395
7396 /* Generate the reversed branch to NOT_TAKEN. */
7397 operands[0] = not_taken;
7398 output_asm_insn (branch_if_false, operands);
7399
7400 output_asm_insn ("b\t%0", &taken);
7401
7402 /* Output NOT_TAKEN. */
7403 targetm.asm_out.internal_label (asm_out_file, "L",
7404 CODE_LABEL_NUMBER (not_taken));
7405 return "";
7406 }
7407
7408 /* Return the assembly code for INSN, which branches to OPERANDS[0]
7409 if some equality condition is true. The condition is given by
7410 OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
7411 OPERANDS[1]. OPERANDS[2] is the comparison's first operand;
7412 OPERANDS[3] is the second operand and may be zero or a register. */
7413
7414 const char *
7415 loongarch_output_equal_conditional_branch (rtx_insn *insn, rtx *operands,
7416 bool inverted_p)
7417 {
7418 const char *branch[2];
7419 if (operands[3] == const0_rtx)
7420 {
7421 branch[!inverted_p] = LARCH_BRANCH ("b%C1z", "%2,%0");
7422 branch[inverted_p] = LARCH_BRANCH ("b%N1z", "%2,%0");
7423 }
7424 else
7425 {
7426 branch[!inverted_p] = LARCH_BRANCH ("b%C1", "%2,%z3,%0");
7427 branch[inverted_p] = LARCH_BRANCH ("b%N1", "%2,%z3,%0");
7428 }
7429
7430 return loongarch_output_conditional_branch (insn, operands, branch[1],
7431 branch[0]);
7432 }
7433
7434 /* Return the assembly code for INSN, which branches to OPERANDS[0]
7435 if some ordering condition is true. The condition is given by
7436 OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
7437 OPERANDS[1]. OPERANDS[2] is the comparison's first operand;
7438 OPERANDS[3] is the second operand and may be zero or a register. */
7439
7440 const char *
7441 loongarch_output_order_conditional_branch (rtx_insn *insn, rtx *operands,
7442 bool inverted_p)
7443 {
7444 const char *branch[2];
7445
7446 /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
7447 Make BRANCH[0] branch on the inverse condition. */
7448 if (operands[3] != const0_rtx)
7449 {
7450 /* Handle degenerate cases that should not, but do, occur. */
7451 if (REGNO (operands[2]) == REGNO (operands[3]))
7452 {
7453 switch (GET_CODE (operands[1]))
7454 {
7455 case LT:
7456 case LTU:
7457 case GT:
7458 case GTU:
7459 inverted_p = !inverted_p;
7460 /* Fall through. */
7461 case LE:
7462 case LEU:
7463 case GE:
7464 case GEU:
7465 branch[!inverted_p] = LARCH_BRANCH ("b", "%0");
7466 branch[inverted_p] = "\t# branch never";
7467 break;
7468 default:
7469 gcc_unreachable ();
7470 }
7471 }
7472 else
7473 {
7474 switch (GET_CODE (operands[1]))
7475 {
7476 case LE:
7477 case LEU:
7478 case GT:
7479 case GTU:
7480 case LT:
7481 case LTU:
7482 case GE:
7483 case GEU:
7484 branch[!inverted_p] = LARCH_BRANCH ("b%C1", "%2,%3,%0");
7485 branch[inverted_p] = LARCH_BRANCH ("b%N1", "%2,%3,%0");
7486 break;
7487 default:
7488 gcc_unreachable ();
7489 }
7490 }
7491 }
7492 else
7493 {
7494 switch (GET_CODE (operands[1]))
7495 {
7496 /* These cases are equivalent to comparisons against zero. */
7497 case LEU:
7498 case GTU:
7499 case LTU:
7500 case GEU:
7501 case LE:
7502 case GT:
7503 case LT:
7504 case GE:
7505 branch[!inverted_p] = LARCH_BRANCH ("b%C1", "%2,$r0,%0");
7506 branch[inverted_p] = LARCH_BRANCH ("b%N1", "%2,$r0,%0");
7507 break;
7508 default:
7509 gcc_unreachable ();
7510 }
7511 }
7512 return loongarch_output_conditional_branch (insn, operands, branch[1],
7513 branch[0]);
7514 }
7515
7516 /* Return the assembly code for DIV.{W/D} instruction DIVISION, which has
7517 the operands given by OPERANDS. Add in a divide-by-zero check if needed.
7518 */
7519
7520 const char *
7521 loongarch_output_division (const char *division, rtx *operands)
7522 {
7523 const char *s;
7524
7525 s = division;
7526 if (loongarch_check_zero_div_p ())
7527 {
7528 output_asm_insn (s, operands);
7529 s = "bne\t%2,%.,1f\n\tbreak\t7\n1:";
7530 }
7531 return s;
7532 }
7533
7534 /* Return the assembly code for LSX DIV_{S,U}.DF or MOD_{S,U}.DF instructions,
7535 which has the operands given by OPERANDS. Add in a divide-by-zero check
7536 if needed. */
7537
7538 const char *
7539 loongarch_lsx_output_division (const char *division, rtx *operands)
7540 {
7541 const char *s;
7542 machine_mode mode = GET_MODE (*operands);
7543
7544 s = division;
7545 if (TARGET_CHECK_ZERO_DIV)
7546 {
7547 if (ISA_HAS_LASX && GET_MODE_SIZE (mode) == 32)
7548 {
7549 output_asm_insn ("xvsetallnez.%v0\t$fcc7,%u2",operands);
7550 output_asm_insn (s, operands);
7551 output_asm_insn ("bcnez\t$fcc7,1f", operands);
7552 }
7553 else if (ISA_HAS_LSX)
7554 {
7555 output_asm_insn ("vsetallnez.%v0\t$fcc7,%w2",operands);
7556 output_asm_insn (s, operands);
7557 output_asm_insn ("bcnez\t$fcc7,1f", operands);
7558 }
7559 s = "break\t7\n1:";
7560 }
7561 return s;
7562 }
7563
7564 /* Implement TARGET_SCHED_ADJUST_COST. We assume that anti and output
7565 dependencies have no cost. */
7566
7567 static int
7568 loongarch_adjust_cost (rtx_insn *, int dep_type, rtx_insn *, int cost,
7569 unsigned int)
7570 {
7571 if (dep_type != 0 && (dep_type != REG_DEP_OUTPUT))
7572 return 0;
7573 return cost;
7574 }
7575
7576 /* Return the number of instructions that can be issued per cycle. */
7577
7578 static int
7579 loongarch_issue_rate (void)
7580 {
7581 if ((unsigned long) la_target.cpu_tune < N_TUNE_TYPES)
7582 return loongarch_cpu_issue_rate[la_target.cpu_tune];
7583 else
7584 return 1;
7585 }
7586
7587 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD. This should
7588 be as wide as the scheduling freedom in the DFA. */
7589
7590 static int
7591 loongarch_multipass_dfa_lookahead (void)
7592 {
7593 if ((unsigned long) la_target.cpu_tune < N_ARCH_TYPES)
7594 return loongarch_cpu_multipass_dfa_lookahead[la_target.cpu_tune];
7595 else
7596 return 0;
7597 }
7598
7599 /* Implement TARGET_SCHED_REORDER. */
7600
7601 static int
7602 loongarch_sched_reorder (FILE *file ATTRIBUTE_UNUSED,
7603 int verbose ATTRIBUTE_UNUSED,
7604 rtx_insn **ready ATTRIBUTE_UNUSED,
7605 int *nreadyp ATTRIBUTE_UNUSED,
7606 int cycle ATTRIBUTE_UNUSED)
7607 {
7608 return loongarch_issue_rate ();
7609 }
7610
7611 /* Implement TARGET_SCHED_REORDER2. */
7612
7613 static int
7614 loongarch_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED,
7615 int verbose ATTRIBUTE_UNUSED,
7616 rtx_insn **ready ATTRIBUTE_UNUSED,
7617 int *nreadyp ATTRIBUTE_UNUSED,
7618 int cycle ATTRIBUTE_UNUSED)
7619 {
7620 return cached_can_issue_more;
7621 }
7622
7623 /* Implement TARGET_SCHED_INIT. */
7624
7625 static void
7626 loongarch_sched_init (FILE *file ATTRIBUTE_UNUSED,
7627 int verbose ATTRIBUTE_UNUSED,
7628 int max_ready ATTRIBUTE_UNUSED)
7629 {}
7630
7631 /* Implement TARGET_SCHED_VARIABLE_ISSUE. */
7632
7633 static int
7634 loongarch_variable_issue (FILE *file ATTRIBUTE_UNUSED,
7635 int verbose ATTRIBUTE_UNUSED, rtx_insn *insn,
7636 int more)
7637 {
7638 /* Ignore USEs and CLOBBERs; don't count them against the issue rate. */
7639 if (USEFUL_INSN_P (insn))
7640 {
7641 if (get_attr_type (insn) != TYPE_GHOST)
7642 more--;
7643 }
7644
7645 /* Instructions of type 'multi' should all be split before
7646 the second scheduling pass. */
7647 gcc_assert (!reload_completed
7648 || recog_memoized (insn) < 0
7649 || get_attr_type (insn) != TYPE_MULTI);
7650
7651 cached_can_issue_more = more;
7652 return more;
7653 }
7654
7655 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
7656 return the first operand of the associated PREF or PREFX insn. */
7657
7658 rtx
7659 loongarch_prefetch_cookie (rtx write, rtx locality)
7660 {
7661 /* store_streamed / load_streamed. */
7662 if (INTVAL (locality) <= 0)
7663 return GEN_INT (INTVAL (write) + 4);
7664
7665 /* store / load. */
7666 if (INTVAL (locality) <= 2)
7667 return write;
7668
7669 /* store_retained / load_retained. */
7670 return GEN_INT (INTVAL (write) + 6);
7671 }
7672
7673 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
7674 in order to avoid duplicating too much logic from elsewhere. */
7675
7676 static void
7677 loongarch_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
7678 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
7679 tree function)
7680 {
7681 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
7682 rtx this_rtx, temp1, temp2, fnaddr;
7683 rtx_insn *insn;
7684 bool use_sibcall_p;
7685
7686 /* Pretend to be a post-reload pass while generating rtl. */
7687 reload_completed = 1;
7688
7689 /* Mark the end of the (empty) prologue. */
7690 emit_note (NOTE_INSN_PROLOGUE_END);
7691
7692 /* Determine if we can use a sibcall to call FUNCTION directly. */
7693 fnaddr = XEXP (DECL_RTL (function), 0);
7694 use_sibcall_p = const_call_insn_operand (fnaddr, Pmode);
7695
7696 /* We need two temporary registers in some cases. */
7697 temp1 = gen_rtx_REG (Pmode, 12);
7698 temp2 = gen_rtx_REG (Pmode, 13);
7699
7700 /* Find out which register contains the "this" pointer. */
7701 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
7702 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
7703 else
7704 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
7705
7706 /* Add DELTA to THIS_RTX. */
7707 if (delta != 0)
7708 {
7709 rtx offset = GEN_INT (delta);
7710 if (!IMM12_OPERAND (delta))
7711 {
7712 loongarch_emit_move (temp1, offset);
7713 offset = temp1;
7714 }
7715 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
7716 }
7717
7718 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
7719 if (vcall_offset != 0)
7720 {
7721 rtx addr;
7722
7723 /* Set TEMP1 to *THIS_RTX. */
7724 loongarch_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
7725
7726 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
7727 addr = loongarch_add_offset (temp2, temp1, vcall_offset);
7728
7729 /* Load the offset and add it to THIS_RTX. */
7730 loongarch_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
7731 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
7732 }
7733
7734 /* Jump to the target function. Use a sibcall if direct jumps are
7735 allowed, otherwise load the address into a register first. */
7736 if (use_sibcall_p)
7737 {
7738 /* If TARGET_CMODEL_EXTREME, we cannot do a direct jump at all
7739 and const_call_insn_operand should have returned false. */
7740 gcc_assert (!TARGET_CMODEL_EXTREME);
7741
7742 insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
7743 SIBLING_CALL_P (insn) = 1;
7744 }
7745 else
7746 {
7747 if (!TARGET_CMODEL_EXTREME)
7748 loongarch_emit_move (temp1, fnaddr);
7749 else if (la_opt_explicit_relocs == EXPLICIT_RELOCS_NONE)
7750 emit_insn (gen_movdi_symbolic_off64 (temp1, fnaddr, temp2));
7751 else
7752 {
7753 emit_insn (gen_la_pcrel64_two_parts (temp1, temp2, fnaddr));
7754 emit_move_insn (temp1, gen_rtx_PLUS (Pmode, temp1, temp2));
7755 }
7756
7757 emit_jump_insn (gen_indirect_jump (temp1));
7758 }
7759
7760 /* Run just enough of rest_of_compilation. This sequence was
7761 "borrowed" from alpha.c. */
7762 insn = get_insns ();
7763 split_all_insns_noflow ();
7764 shorten_branches (insn);
7765 assemble_start_function (thunk_fndecl, fnname);
7766 final_start_function (insn, file, 1);
7767 final (insn, file, 1);
7768 final_end_function ();
7769 assemble_end_function (thunk_fndecl, fnname);
7770
7771 /* Stop pretending to be a post-reload pass. */
7772 reload_completed = 0;
7773 }
7774
7775 /* Allocate a chunk of memory for per-function machine-dependent data. */
7776
7777 static struct machine_function *
7778 loongarch_init_machine_status (void)
7779 {
7780 return ggc_cleared_alloc<machine_function> ();
7781 }
7782
7783 static void
7784 loongarch_global_init (void)
7785 {
7786 /* Initialize loongarch_print_operand_punct. */
7787 for (const char *p = ".$"; *p; p++)
7788 loongarch_print_operand_punct[(unsigned char) *p] = true;
7789
7790 /* Set up array to map GCC register number to debug register number.
7791 Ignore the special purpose register numbers. */
7792 for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7793 {
7794 if (GP_REG_P (i) || FP_REG_P (i))
7795 loongarch_dwarf_regno[i] = i;
7796 else
7797 loongarch_dwarf_regno[i] = INVALID_REGNUM;
7798 }
7799
7800 /* Function to allocate machine-dependent function status. */
7801 init_machine_status = &loongarch_init_machine_status;
7802 }
7803
7804 static void
7805 loongarch_reg_init (void)
7806 {
7807 /* Set up loongarch_hard_regno_mode_ok. */
7808 for (int mode = 0; mode < MAX_MACHINE_MODE; mode++)
7809 for (int regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7810 loongarch_hard_regno_mode_ok_p[mode][regno]
7811 = loongarch_hard_regno_mode_ok_uncached (regno, (machine_mode) mode);
7812 }
7813
7814 void
7815 loongarch_option_override_internal (struct loongarch_target *target,
7816 struct gcc_options *opts,
7817 struct gcc_options *opts_set)
7818 {
7819 /* Handle options not covered by struct loongarch_target. */
7820 loongarch_init_misc_options (opts, opts_set);
7821
7822 /* Resolve the target struct. */
7823 loongarch_init_target (target,
7824 opts->x_la_opt_cpu_arch,
7825 opts->x_la_opt_cpu_tune,
7826 opts->x_la_opt_fpu,
7827 opts->x_la_opt_simd,
7828 opts->x_la_opt_abi_base,
7829 opts->x_la_opt_abi_ext,
7830 opts->x_la_opt_cmodel,
7831 opts->x_la_opt_tls_dialect,
7832 opts->x_la_isa_evolution,
7833 opts_set->x_la_isa_evolution);
7834
7835 loongarch_config_target (target, NULL, 0);
7836
7837 /* Override some options according to the resolved target. */
7838 loongarch_target_option_override (target, opts, opts_set);
7839
7840 loongarch_reg_init ();
7841 }
7842
7843 /* Remember the last target of loongarch_set_current_function. */
7844
7845 static GTY(()) tree loongarch_previous_fndecl;
7846
7847 void
7848 loongarch_reset_previous_fndecl (void)
7849 {
7850 loongarch_previous_fndecl = NULL;
7851 }
7852
7853 /* Restore or save the TREE_TARGET_GLOBALS from or to new_tree.
7854 Used by loongarch_set_current_function to
7855 make sure optab availability predicates are recomputed when necessary. */
7856
7857 void
7858 loongarch_save_restore_target_globals (tree new_tree)
7859 {
7860 if (TREE_TARGET_GLOBALS (new_tree))
7861 restore_target_globals (TREE_TARGET_GLOBALS (new_tree));
7862 else if (new_tree == target_option_default_node)
7863 restore_target_globals (&default_target_globals);
7864 else
7865 TREE_TARGET_GLOBALS (new_tree) = save_target_globals_default_opts ();
7866 }
7867
7868 /* Implement TARGET_SET_CURRENT_FUNCTION. */
7869
7870 static void
7871 loongarch_set_current_function (tree fndecl)
7872 {
7873 if (fndecl == loongarch_previous_fndecl)
7874 return;
7875
7876 tree old_tree;
7877 if (loongarch_previous_fndecl == NULL_TREE)
7878 old_tree = target_option_current_node;
7879 else if (DECL_FUNCTION_SPECIFIC_TARGET (loongarch_previous_fndecl))
7880 old_tree = DECL_FUNCTION_SPECIFIC_TARGET (loongarch_previous_fndecl);
7881 else
7882 old_tree = target_option_default_node;
7883
7884 /* When the function is optimized, the pop_cfun will be called, and
7885 the fndecl will be NULL. */
7886 if (fndecl == NULL_TREE)
7887 {
7888 if (old_tree != target_option_current_node)
7889 {
7890 /* When this function is set with special options, we need to
7891 restore the original global optimization options at the end
7892 of function optimization. */
7893 loongarch_previous_fndecl = NULL_TREE;
7894 cl_target_option_restore (&global_options, &global_options_set,
7895 TREE_TARGET_OPTION
7896 (target_option_current_node));
7897 }
7898 return;
7899 }
7900
7901 tree new_tree = DECL_FUNCTION_SPECIFIC_TARGET (fndecl);
7902
7903 /* When no separate compilation parameters are set for the function,
7904 new_tree is NULL. */
7905 if (new_tree == NULL_TREE)
7906 new_tree = target_option_default_node;
7907
7908 loongarch_previous_fndecl = fndecl;
7909
7910 if (new_tree != old_tree)
7911 /* According to the settings of the functions attribute and pragma,
7912 the options is corrected. */
7913 cl_target_option_restore (&global_options, &global_options_set,
7914 TREE_TARGET_OPTION (new_tree));
7915
7916
7917 /* After correcting the value of options, we need to update the
7918 rules for using the hardware registers to ensure that the
7919 rules correspond to the options. */
7920 loongarch_reg_init ();
7921
7922 loongarch_save_restore_target_globals (new_tree);
7923 }
7924
7925
7926
7927 /* Implement TARGET_OPTION_OVERRIDE. */
7928
7929 static void
7930 loongarch_option_override (void)
7931 {
7932 /* Global initializations. */
7933 loongarch_global_init ();
7934
7935 /* Setting up the target configuration. */
7936 loongarch_option_override_internal (&la_target,
7937 &global_options,
7938 &global_options_set);
7939
7940 /* Save the initial options so that we can restore the initial option
7941 settings later when processing attributes and pragmas. */
7942 target_option_default_node = target_option_current_node
7943 = build_target_option_node (&global_options, &global_options_set);
7944
7945 }
7946
7947 /* Implement TARGET_OPTION_SAVE. */
7948 static void
7949 loongarch_option_save (struct cl_target_option *,
7950 struct gcc_options *opts,
7951 struct gcc_options *opts_set)
7952 {
7953 loongarch_update_gcc_opt_status (&la_target, opts, opts_set);
7954 }
7955
7956 /* Implement TARGET_OPTION_RESTORE. */
7957 static void
7958 loongarch_option_restore (struct gcc_options *,
7959 struct gcc_options *,
7960 struct cl_target_option *ptr)
7961 {
7962 la_target.cpu_arch = ptr->x_la_opt_cpu_arch;
7963 la_target.cpu_tune = ptr->x_la_opt_cpu_tune;
7964
7965 la_target.isa.fpu = ptr->x_la_opt_fpu;
7966 la_target.isa.simd = ptr->x_la_opt_simd;
7967 la_target.isa.evolution = ptr->x_la_isa_evolution;
7968
7969 la_target.cmodel = ptr->x_la_opt_cmodel;
7970 la_target.tls_dialect = ptr->x_la_opt_tls_dialect;
7971 }
7972
7973 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
7974
7975 static void
7976 loongarch_conditional_register_usage (void)
7977 {
7978 if (!TARGET_HARD_FLOAT)
7979 accessible_reg_set &= ~(reg_class_contents[FP_REGS]
7980 | reg_class_contents[FCC_REGS]);
7981 }
7982
7983 /* Implement EH_USES. */
7984
7985 bool
7986 loongarch_eh_uses (unsigned int regno ATTRIBUTE_UNUSED)
7987 {
7988 return false;
7989 }
7990
7991 /* Implement EPILOGUE_USES. */
7992
7993 bool
7994 loongarch_epilogue_uses (unsigned int regno)
7995 {
7996 /* Say that the epilogue uses the return address register. Note that
7997 in the case of sibcalls, the values "used by the epilogue" are
7998 considered live at the start of the called function. */
7999 if (regno == RETURN_ADDR_REGNUM)
8000 return true;
8001
8002 return false;
8003 }
8004
8005 bool
8006 loongarch_load_store_bonding_p (rtx *operands, machine_mode mode, bool load_p)
8007 {
8008 rtx reg1, reg2, mem1, mem2, base1, base2;
8009 enum reg_class rc1, rc2;
8010 HOST_WIDE_INT offset1, offset2;
8011
8012 if (load_p)
8013 {
8014 reg1 = operands[0];
8015 reg2 = operands[2];
8016 mem1 = operands[1];
8017 mem2 = operands[3];
8018 }
8019 else
8020 {
8021 reg1 = operands[1];
8022 reg2 = operands[3];
8023 mem1 = operands[0];
8024 mem2 = operands[2];
8025 }
8026
8027 if (loongarch_address_insns (XEXP (mem1, 0), mode, false) == 0
8028 || loongarch_address_insns (XEXP (mem2, 0), mode, false) == 0)
8029 return false;
8030
8031 loongarch_split_plus (XEXP (mem1, 0), &base1, &offset1);
8032 loongarch_split_plus (XEXP (mem2, 0), &base2, &offset2);
8033
8034 /* Base regs do not match. */
8035 if (!REG_P (base1) || !rtx_equal_p (base1, base2))
8036 return false;
8037
8038 /* Either of the loads is clobbering base register. It is legitimate to bond
8039 loads if second load clobbers base register. However, hardware does not
8040 support such bonding. */
8041 if (load_p
8042 && (REGNO (reg1) == REGNO (base1) || (REGNO (reg2) == REGNO (base1))))
8043 return false;
8044
8045 /* Loading in same registers. */
8046 if (load_p && REGNO (reg1) == REGNO (reg2))
8047 return false;
8048
8049 /* The loads/stores are not of same type. */
8050 rc1 = REGNO_REG_CLASS (REGNO (reg1));
8051 rc2 = REGNO_REG_CLASS (REGNO (reg2));
8052 if (rc1 != rc2 && !reg_class_subset_p (rc1, rc2)
8053 && !reg_class_subset_p (rc2, rc1))
8054 return false;
8055
8056 if (abs (offset1 - offset2) != GET_MODE_SIZE (mode))
8057 return false;
8058
8059 return true;
8060 }
8061
8062 /* Implement TARGET_TRAMPOLINE_INIT. */
8063
8064 static void
8065 loongarch_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
8066 {
8067 rtx addr, end_addr, mem;
8068 rtx trampoline[8];
8069 unsigned int i, j;
8070 HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
8071
8072 /* Work out the offsets of the pointers from the start of the
8073 trampoline code. */
8074 end_addr_offset = TRAMPOLINE_CODE_SIZE;
8075 static_chain_offset = end_addr_offset;
8076 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
8077
8078 /* Get pointers to the beginning and end of the code block. */
8079 addr = force_reg (Pmode, XEXP (m_tramp, 0));
8080 end_addr
8081 = loongarch_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
8082
8083 #define OP(X) gen_int_mode (X, SImode)
8084
8085 /* Build up the code in TRAMPOLINE. */
8086 i = 0;
8087 /*pcaddi $static_chain,0
8088 ld.[dw] $tmp,$static_chain,target_function_offset
8089 ld.[dw] $static_chain,$static_chain,static_chain_offset
8090 jirl $r0,$tmp,0 */
8091 trampoline[i++] = OP (0x18000000 | (STATIC_CHAIN_REGNUM - GP_REG_FIRST));
8092 trampoline[i++] = OP ((ptr_mode == DImode ? 0x28c00000 : 0x28800000)
8093 | 19 /* $t7 */
8094 | ((STATIC_CHAIN_REGNUM - GP_REG_FIRST) << 5)
8095 | ((target_function_offset & 0xfff) << 10));
8096 trampoline[i++] = OP ((ptr_mode == DImode ? 0x28c00000 : 0x28800000)
8097 | (STATIC_CHAIN_REGNUM - GP_REG_FIRST)
8098 | ((STATIC_CHAIN_REGNUM - GP_REG_FIRST) << 5)
8099 | ((static_chain_offset & 0xfff) << 10));
8100 trampoline[i++] = OP (0x4c000000 | (19 << 5));
8101 #undef OP
8102
8103 for (j = 0; j < i; j++)
8104 {
8105 mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
8106 loongarch_emit_move (mem, trampoline[j]);
8107 }
8108
8109 /* Set up the static chain pointer field. */
8110 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
8111 loongarch_emit_move (mem, chain_value);
8112
8113 /* Set up the target function field. */
8114 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
8115 loongarch_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
8116
8117 /* Flush the code part of the trampoline. */
8118 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
8119 emit_insn (gen_clear_cache (addr, end_addr));
8120 }
8121
8122 /* Generate or test for an insn that supports a constant permutation. */
8123
8124 #define MAX_VECT_LEN 32
8125
8126 struct expand_vec_perm_d
8127 {
8128 rtx target, op0, op1;
8129 unsigned char perm[MAX_VECT_LEN];
8130 machine_mode vmode;
8131 unsigned char nelt;
8132 bool one_vector_p;
8133 bool testing_p;
8134 };
8135
8136 /* Construct (set target (vec_select op0 (parallel perm))) and
8137 return true if that's a valid instruction in the active ISA. */
8138
8139 static bool
8140 loongarch_expand_vselect (rtx target, rtx op0,
8141 const unsigned char *perm, unsigned nelt,
8142 bool testing_p)
8143 {
8144 rtx rperm[MAX_VECT_LEN], x;
8145 rtx_insn *insn;
8146 unsigned i;
8147
8148 for (i = 0; i < nelt; ++i)
8149 rperm[i] = GEN_INT (perm[i]);
8150
8151 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
8152 x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
8153 x = gen_rtx_SET (target, x);
8154
8155 insn = emit_insn (x);
8156 if (recog_memoized (insn) < 0)
8157 {
8158 remove_insn (insn);
8159 return false;
8160 }
8161
8162 if (testing_p)
8163 remove_insn (insn);
8164 return true;
8165 }
8166
8167 /* Similar, but generate a vec_concat from op0 and op1 as well. */
8168
8169 static bool
8170 loongarch_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
8171 const unsigned char *perm, unsigned nelt,
8172 bool testing_p)
8173 {
8174 machine_mode v2mode;
8175 rtx x;
8176
8177 if (!GET_MODE_2XWIDER_MODE (GET_MODE (op0)).exists (&v2mode))
8178 return false;
8179 x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
8180 return loongarch_expand_vselect (target, x, perm, nelt, testing_p);
8181 }
8182
8183 static tree
8184 loongarch_handle_model_attribute (tree *node, tree name, tree arg, int,
8185 bool *no_add_attrs)
8186 {
8187 tree decl = *node;
8188 if (VAR_P (decl))
8189 {
8190 if (DECL_THREAD_LOCAL_P (decl))
8191 {
8192 error_at (DECL_SOURCE_LOCATION (decl),
8193 "%qE attribute cannot be specified for thread-local "
8194 "variables", name);
8195 *no_add_attrs = true;
8196 return NULL_TREE;
8197 }
8198 if (DECL_CONTEXT (decl)
8199 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8200 && !TREE_STATIC (decl))
8201 {
8202 error_at (DECL_SOURCE_LOCATION (decl),
8203 "%qE attribute cannot be specified for local "
8204 "variables", name);
8205 *no_add_attrs = true;
8206 return NULL_TREE;
8207 }
8208 if (DECL_REGISTER (decl))
8209 {
8210 error_at (DECL_SOURCE_LOCATION (decl),
8211 "%qE attribute cannot be specified for register "
8212 "variables", name);
8213 *no_add_attrs = true;
8214 return NULL_TREE;
8215 }
8216
8217 arg = TREE_VALUE (arg);
8218 if (TREE_CODE (arg) != STRING_CST)
8219 {
8220 error_at (DECL_SOURCE_LOCATION (decl),
8221 "invalid argument of %qE attribute", name);
8222 *no_add_attrs = true;
8223 return NULL_TREE;
8224 }
8225
8226 const char *model = TREE_STRING_POINTER (arg);
8227 if (strcmp (model, "normal") != 0
8228 && strcmp (model, "extreme") != 0)
8229 {
8230 error_at (DECL_SOURCE_LOCATION (decl),
8231 "invalid argument of %qE attribute", name);
8232 *no_add_attrs = true;
8233 return NULL_TREE;
8234 }
8235
8236 if (lookup_attribute ("model", DECL_ATTRIBUTES (decl)))
8237 {
8238 error_at (DECL_SOURCE_LOCATION (decl),
8239 "multiple %qE attribute", name);
8240 *no_add_attrs = true;
8241 return NULL_TREE;
8242 }
8243 }
8244 else
8245 {
8246 warning (OPT_Wattributes, "%qE attribute ignored", name);
8247 *no_add_attrs = true;
8248 }
8249 return NULL_TREE;
8250 }
8251
8252 TARGET_GNU_ATTRIBUTES (loongarch_attribute_table,
8253 {
8254 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
8255 affects_type_identity, handler, exclude } */
8256 { "model", 1, 1, true, false, false, false,
8257 loongarch_handle_model_attribute, NULL }
8258 });
8259
8260 bool
8261 loongarch_use_anchors_for_symbol_p (const_rtx symbol)
8262 {
8263 tree decl = SYMBOL_REF_DECL (symbol);
8264
8265 /* The section anchor optimization may break custom address model. */
8266 if (decl && lookup_attribute ("model", DECL_ATTRIBUTES (decl)))
8267 return false;
8268
8269 return default_use_anchors_for_symbol_p (symbol);
8270 }
8271
8272 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
8273
8274 static unsigned HOST_WIDE_INT
8275 loongarch_asan_shadow_offset (void)
8276 {
8277 /* We only have libsanitizer support for LOONGARCH64 at present.
8278 This value is taken from the file libsanitizer/asan/asan_mapping.h. */
8279 return TARGET_64BIT ? (HOST_WIDE_INT_1 << 46) : 0;
8280 }
8281
8282 static sbitmap
8283 loongarch_get_separate_components (void)
8284 {
8285 HOST_WIDE_INT offset;
8286 sbitmap components = sbitmap_alloc (FIRST_PSEUDO_REGISTER);
8287 bitmap_clear (components);
8288 offset = cfun->machine->frame.gp_sp_offset;
8289
8290 /* The stack should be aligned to 16-bytes boundary, so we can make the use
8291 of ldptr instructions. */
8292 gcc_assert (offset % UNITS_PER_WORD == 0);
8293
8294 for (unsigned int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
8295 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
8296 {
8297 /* We can wrap general registers saved at [sp, sp + 32768) using the
8298 ldptr/stptr instructions. For large offsets a pseudo register
8299 might be needed which cannot be created during the shrink
8300 wrapping pass.
8301
8302 TODO: This may need a revise when we add LA32 as ldptr.w is not
8303 guaranteed available by the manual. */
8304 if (offset < 32768)
8305 bitmap_set_bit (components, regno);
8306
8307 offset -= UNITS_PER_WORD;
8308 }
8309
8310 offset = cfun->machine->frame.fp_sp_offset;
8311 for (unsigned int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
8312 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
8313 {
8314 /* We can only wrap FP registers with imm12 offsets. For large
8315 offsets a pseudo register might be needed which cannot be
8316 created during the shrink wrapping pass. */
8317 if (IMM12_OPERAND (offset))
8318 bitmap_set_bit (components, regno);
8319
8320 offset -= UNITS_PER_FP_REG;
8321 }
8322
8323 /* Don't mess with the hard frame pointer. */
8324 if (frame_pointer_needed)
8325 bitmap_clear_bit (components, HARD_FRAME_POINTER_REGNUM);
8326
8327 bitmap_clear_bit (components, RETURN_ADDR_REGNUM);
8328
8329 return components;
8330 }
8331
8332 static sbitmap
8333 loongarch_components_for_bb (basic_block bb)
8334 {
8335 /* Registers are used in a bb if they are in the IN, GEN, or KILL sets. */
8336 auto_bitmap used;
8337 bitmap_copy (used, DF_LIVE_IN (bb));
8338 bitmap_ior_into (used, &DF_LIVE_BB_INFO (bb)->gen);
8339 bitmap_ior_into (used, &DF_LIVE_BB_INFO (bb)->kill);
8340
8341 sbitmap components = sbitmap_alloc (FIRST_PSEUDO_REGISTER);
8342 bitmap_clear (components);
8343
8344 function_abi_aggregator callee_abis;
8345 rtx_insn *insn;
8346 FOR_BB_INSNS (bb, insn)
8347 if (CALL_P (insn))
8348 callee_abis.note_callee_abi (insn_callee_abi (insn));
8349
8350 HARD_REG_SET extra_caller_saves =
8351 callee_abis.caller_save_regs (*crtl->abi);
8352
8353 for (unsigned int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
8354 if (!fixed_regs[regno]
8355 && !crtl->abi->clobbers_full_reg_p (regno)
8356 && (TEST_HARD_REG_BIT (extra_caller_saves, regno) ||
8357 bitmap_bit_p (used, regno)))
8358 bitmap_set_bit (components, regno);
8359
8360 for (unsigned int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
8361 if (!fixed_regs[regno]
8362 && !crtl->abi->clobbers_full_reg_p (regno)
8363 && (TEST_HARD_REG_BIT (extra_caller_saves, regno) ||
8364 bitmap_bit_p (used, regno)))
8365 bitmap_set_bit (components, regno);
8366
8367 return components;
8368 }
8369
8370 static void
8371 loongarch_disqualify_components (sbitmap, edge, sbitmap, bool)
8372 {
8373 /* Do nothing. */
8374 }
8375
8376 static void
8377 loongarch_process_components (sbitmap components, loongarch_save_restore_fn fn)
8378 {
8379 HOST_WIDE_INT offset = cfun->machine->frame.gp_sp_offset;
8380
8381 for (unsigned int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
8382 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
8383 {
8384 if (bitmap_bit_p (components, regno))
8385 loongarch_save_restore_reg (word_mode, regno, offset, fn);
8386
8387 offset -= UNITS_PER_WORD;
8388 }
8389
8390 offset = cfun->machine->frame.fp_sp_offset;
8391 machine_mode mode = TARGET_DOUBLE_FLOAT ? DFmode : SFmode;
8392
8393 for (unsigned int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
8394 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
8395 {
8396 if (bitmap_bit_p (components, regno))
8397 loongarch_save_restore_reg (mode, regno, offset, fn);
8398
8399 offset -= UNITS_PER_FP_REG;
8400 }
8401 }
8402
8403 static void
8404 loongarch_emit_prologue_components (sbitmap components)
8405 {
8406 loongarch_process_components (components, loongarch_save_reg);
8407 }
8408
8409 static void
8410 loongarch_emit_epilogue_components (sbitmap components)
8411 {
8412 loongarch_process_components (components, loongarch_restore_reg);
8413 }
8414
8415 static void
8416 loongarch_set_handled_components (sbitmap components)
8417 {
8418 for (unsigned int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
8419 if (bitmap_bit_p (components, regno))
8420 cfun->machine->reg_is_wrapped_separately[regno] = true;
8421
8422 for (unsigned int regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
8423 if (bitmap_bit_p (components, regno))
8424 cfun->machine->reg_is_wrapped_separately[regno] = true;
8425 }
8426
8427 /* Use the vshuf instruction to implement all 128-bit constant vector
8428 permuatation. */
8429
8430 static bool
8431 loongarch_try_expand_lsx_vshuf_const (struct expand_vec_perm_d *d)
8432 {
8433 int i;
8434 rtx target, op0, op1;
8435 rtx rperm[MAX_VECT_LEN];
8436
8437 if (GET_MODE_SIZE (d->vmode) == 16)
8438 {
8439 target = d->target;
8440 op0 = d->op0;
8441 op1 = d->one_vector_p ? d->op0 : d->op1;
8442
8443 if (GET_MODE (op0) != GET_MODE (op1)
8444 || GET_MODE (op0) != GET_MODE (target))
8445 return false;
8446
8447 if (d->testing_p)
8448 return true;
8449
8450 for (i = 0; i < d->nelt; i += 1)
8451 rperm[i] = GEN_INT (d->perm[i]);
8452
8453 machine_mode sel_mode = related_int_vector_mode (d->vmode)
8454 .require ();
8455 rtvec sel_v = gen_rtvec_v (d->nelt, rperm);
8456
8457 /* Despite vshuf.* (except vshuf.b) needs sel == target, we cannot
8458 load sel into target right now: here we are dealing with
8459 pseudo regs, and target may be the same pseudo as one of op0
8460 or op1. Then we'd clobber the input. Instead, we use a new
8461 pseudo reg here. The reload pass will look at the constraint
8462 of vshuf.* and move sel into target first if needed. */
8463 rtx sel = force_reg (sel_mode,
8464 gen_rtx_CONST_VECTOR (sel_mode, sel_v));
8465
8466 if (d->vmode == E_V16QImode)
8467 emit_insn (gen_lsx_vshuf_b (target, op1, op0, sel));
8468 else
8469 emit_insn (gen_lsx_vshuf (d->vmode, target, sel, op1, op0));
8470
8471 return true;
8472 }
8473 return false;
8474 }
8475
8476 /* Construct (set target (vec_select op0 (parallel selector))) and
8477 return true if that's a valid instruction in the active ISA.
8478 In fact, it matches the special constant vector with repeated
8479 4-element sets. */
8480
8481 static bool
8482 loongarch_is_imm_set_shuffle (struct expand_vec_perm_d *d)
8483 {
8484 rtx x, elts[MAX_VECT_LEN];
8485 rtvec v;
8486 rtx_insn *insn;
8487 unsigned i;
8488
8489 if (!ISA_HAS_LSX && !ISA_HAS_LASX)
8490 return false;
8491
8492 for (i = 0; i < d->nelt; i++)
8493 elts[i] = GEN_INT (d->perm[i]);
8494
8495 v = gen_rtvec_v (d->nelt, elts);
8496 x = gen_rtx_PARALLEL (VOIDmode, v);
8497
8498 if (!loongarch_const_vector_shuffle_set_p (x, d->vmode))
8499 return false;
8500
8501 if (d->testing_p)
8502 return true;
8503
8504 x = gen_rtx_VEC_SELECT (d->vmode, d->op0, x);
8505 x = gen_rtx_SET (d->target, x);
8506
8507 insn = emit_insn (x);
8508 if (recog_memoized (insn) < 0)
8509 {
8510 remove_insn (insn);
8511 return false;
8512 }
8513 return true;
8514 }
8515
8516 static bool
8517 loongarch_expand_vec_perm_even_odd (struct expand_vec_perm_d *);
8518
8519 /* Try to match and expand all kinds of 128-bit const vector permutation
8520 cases. */
8521
8522 static bool
8523 loongarch_expand_lsx_shuffle (struct expand_vec_perm_d *d)
8524 {
8525 if (!ISA_HAS_LSX && GET_MODE_SIZE (d->vmode) != 16)
8526 return false;
8527
8528 if (loongarch_is_imm_set_shuffle (d))
8529 return true;
8530
8531 if (loongarch_expand_vec_perm_even_odd (d))
8532 return true;
8533
8534 return loongarch_try_expand_lsx_vshuf_const (d);
8535 }
8536
8537 /* Try to simplify a two vector permutation using 2 intra-lane interleave
8538 insns and cross-lane shuffle for 32-byte vectors. */
8539
8540 static bool
8541 loongarch_expand_vec_perm_interleave (struct expand_vec_perm_d *d)
8542 {
8543 unsigned i, nelt;
8544 rtx t1,t2,t3;
8545 rtx (*gen_high) (rtx, rtx, rtx);
8546 rtx (*gen_low) (rtx, rtx, rtx);
8547 machine_mode mode = GET_MODE (d->target);
8548
8549 if (d->one_vector_p)
8550 return false;
8551 if (ISA_HAS_LASX && GET_MODE_SIZE (d->vmode) == 32)
8552 ;
8553 else
8554 return false;
8555
8556 nelt = d->nelt;
8557 if (d->perm[0] != 0 && d->perm[0] != nelt / 2)
8558 return false;
8559 for (i = 0; i < nelt; i += 2)
8560 if (d->perm[i] != d->perm[0] + i / 2
8561 || d->perm[i + 1] != d->perm[0] + i / 2 + nelt)
8562 return false;
8563
8564 if (d->testing_p)
8565 return true;
8566
8567 switch (d->vmode)
8568 {
8569 case E_V32QImode:
8570 gen_high = gen_lasx_xvilvh_b;
8571 gen_low = gen_lasx_xvilvl_b;
8572 break;
8573 case E_V16HImode:
8574 gen_high = gen_lasx_xvilvh_h;
8575 gen_low = gen_lasx_xvilvl_h;
8576 break;
8577 case E_V8SImode:
8578 gen_high = gen_lasx_xvilvh_w;
8579 gen_low = gen_lasx_xvilvl_w;
8580 break;
8581 case E_V4DImode:
8582 gen_high = gen_lasx_xvilvh_d;
8583 gen_low = gen_lasx_xvilvl_d;
8584 break;
8585 case E_V8SFmode:
8586 gen_high = gen_lasx_xvilvh_w_f;
8587 gen_low = gen_lasx_xvilvl_w_f;
8588 break;
8589 case E_V4DFmode:
8590 gen_high = gen_lasx_xvilvh_d_f;
8591 gen_low = gen_lasx_xvilvl_d_f;
8592 break;
8593 default:
8594 gcc_unreachable ();
8595 }
8596
8597 t1 = gen_reg_rtx (mode);
8598 t2 = gen_reg_rtx (mode);
8599 emit_insn (gen_high (t1, d->op0, d->op1));
8600 emit_insn (gen_low (t2, d->op0, d->op1));
8601 if (mode == V4DFmode || mode == V8SFmode)
8602 {
8603 t3 = gen_reg_rtx (V4DFmode);
8604 if (d->perm[0])
8605 emit_insn (gen_lasx_xvpermi_q_v4df (t3, gen_lowpart (V4DFmode, t1),
8606 gen_lowpart (V4DFmode, t2),
8607 GEN_INT (0x31)));
8608 else
8609 emit_insn (gen_lasx_xvpermi_q_v4df (t3, gen_lowpart (V4DFmode, t1),
8610 gen_lowpart (V4DFmode, t2),
8611 GEN_INT (0x20)));
8612 }
8613 else
8614 {
8615 t3 = gen_reg_rtx (V4DImode);
8616 if (d->perm[0])
8617 emit_insn (gen_lasx_xvpermi_q_v4di (t3, gen_lowpart (V4DImode, t1),
8618 gen_lowpart (V4DImode, t2),
8619 GEN_INT (0x31)));
8620 else
8621 emit_insn (gen_lasx_xvpermi_q_v4di (t3, gen_lowpart (V4DImode, t1),
8622 gen_lowpart (V4DImode, t2),
8623 GEN_INT (0x20)));
8624 }
8625 emit_move_insn (d->target, gen_lowpart (mode, t3));
8626 return true;
8627 }
8628
8629 /* Implement 128-bit and 256-bit extract-even and extract-odd permutations. */
8630
8631 static bool
8632 loongarch_expand_vec_perm_even_odd_1 (struct expand_vec_perm_d *d, unsigned odd)
8633 {
8634 rtx t1;
8635 machine_mode mode = GET_MODE (d->target);
8636
8637 if (d->testing_p)
8638 return true;
8639
8640 t1 = gen_reg_rtx (mode);
8641
8642 switch (d->vmode)
8643 {
8644 /* 128 bit. */
8645 case E_V2DFmode:
8646 if (odd)
8647 emit_insn (gen_lsx_vilvh_d_f (d->target, d->op0, d->op1));
8648 else
8649 emit_insn (gen_lsx_vilvl_d_f (d->target, d->op0, d->op1));
8650 break;
8651
8652 case E_V2DImode:
8653 if (odd)
8654 emit_insn (gen_lsx_vilvh_d (d->target, d->op0, d->op1));
8655 else
8656 emit_insn (gen_lsx_vilvl_d (d->target, d->op0, d->op1));
8657 break;
8658
8659 case E_V4SFmode:
8660 if (odd)
8661 emit_insn (gen_lsx_vpickod_w_f (d->target, d->op0, d->op1));
8662 else
8663 emit_insn (gen_lsx_vpickev_w_f (d->target, d->op0, d->op1));
8664 break;
8665
8666 case E_V4SImode:
8667 if (odd)
8668 emit_insn (gen_lsx_vpickod_w (d->target, d->op0, d->op1));
8669 else
8670 emit_insn (gen_lsx_vpickev_w (d->target, d->op0, d->op1));
8671 break;
8672
8673 case E_V8HImode:
8674 if (odd)
8675 emit_insn (gen_lsx_vpickod_h (d->target, d->op0, d->op1));
8676 else
8677 emit_insn (gen_lsx_vpickev_h (d->target, d->op0, d->op1));
8678 break;
8679
8680 case E_V16QImode:
8681 if (odd)
8682 emit_insn (gen_lsx_vpickod_b (d->target, d->op0, d->op1));
8683 else
8684 emit_insn (gen_lsx_vpickev_b (d->target, d->op0, d->op1));
8685 break;
8686
8687 /* 256 bit. */
8688 case E_V4DFmode:
8689 /* Shuffle the lanes around into { 0 4 2 6 } and { 1 5 3 7 }. */
8690 if (odd)
8691 emit_insn (gen_lasx_xvilvh_d_f (t1, d->op0, d->op1));
8692 else
8693 emit_insn (gen_lasx_xvilvl_d_f (t1, d->op0, d->op1));
8694
8695 /* Shuffle within the 256-bit lanes to produce the result required.
8696 { 0 2 4 6 } | { 1 3 5 7 }. */
8697 emit_insn (gen_lasx_xvpermi_d_v4df (d->target, t1, GEN_INT (0xd8)));
8698 break;
8699
8700 case E_V4DImode:
8701 if (odd)
8702 emit_insn (gen_lasx_xvilvh_d (t1, d->op0, d->op1));
8703 else
8704 emit_insn (gen_lasx_xvilvl_d (t1, d->op0, d->op1));
8705
8706 emit_insn (gen_lasx_xvpermi_d_v4di (d->target, t1, GEN_INT (0xd8)));
8707 break;
8708
8709 case E_V8SFmode:
8710 /* Shuffle the lanes around into:
8711 { 0 2 8 a 4 6 c e } | { 1 3 9 b 5 7 d f }. */
8712 if (odd)
8713 emit_insn (gen_lasx_xvpickod_w_f (t1, d->op0, d->op1));
8714 else
8715 emit_insn (gen_lasx_xvpickev_w_f (t1, d->op0, d->op1));
8716
8717 /* Shuffle within the 256-bit lanes to produce the result required.
8718 { 0 2 4 6 8 a c e } | { 1 3 5 7 9 b d f }. */
8719 emit_insn (gen_lasx_xvpermi_d_v8sf (d->target, t1, GEN_INT (0xd8)));
8720 break;
8721
8722 case E_V8SImode:
8723 if (odd)
8724 emit_insn (gen_lasx_xvpickod_w (t1, d->op0, d->op1));
8725 else
8726 emit_insn (gen_lasx_xvpickev_w (t1, d->op0, d->op1));
8727
8728 emit_insn (gen_lasx_xvpermi_d_v8si (d->target, t1, GEN_INT (0xd8)));
8729 break;
8730
8731 case E_V16HImode:
8732 if (odd)
8733 emit_insn (gen_lasx_xvpickod_h (t1, d->op0, d->op1));
8734 else
8735 emit_insn (gen_lasx_xvpickev_h (t1, d->op0, d->op1));
8736
8737 emit_insn (gen_lasx_xvpermi_d_v16hi (d->target, t1, GEN_INT (0xd8)));
8738 break;
8739
8740 case E_V32QImode:
8741 if (odd)
8742 emit_insn (gen_lasx_xvpickod_b (t1, d->op0, d->op1));
8743 else
8744 emit_insn (gen_lasx_xvpickev_b (t1, d->op0, d->op1));
8745
8746 emit_insn (gen_lasx_xvpermi_d_v32qi (d->target, t1, GEN_INT (0xd8)));
8747 break;
8748
8749 default:
8750 gcc_unreachable ();
8751 }
8752
8753 return true;
8754 }
8755
8756 /* Pattern match extract-even and extract-odd permutations. */
8757
8758 static bool
8759 loongarch_expand_vec_perm_even_odd (struct expand_vec_perm_d *d)
8760 {
8761 unsigned i, odd, nelt = d->nelt;
8762 if (!ISA_HAS_LASX && !ISA_HAS_LSX)
8763 return false;
8764
8765 odd = d->perm[0];
8766 if (odd != 0 && odd != 1)
8767 return false;
8768
8769 for (i = 1; i < nelt; ++i)
8770 if (d->perm[i] != 2 * i + odd)
8771 return false;
8772
8773 return loongarch_expand_vec_perm_even_odd_1 (d, odd);
8774 }
8775
8776 static void
8777 loongarch_expand_vec_interleave (rtx target, rtx op0, rtx op1, bool high_p)
8778 {
8779 struct expand_vec_perm_d d;
8780 unsigned i, nelt, base;
8781 bool ok;
8782
8783 d.target = target;
8784 d.op0 = op0;
8785 d.op1 = op1;
8786 d.vmode = GET_MODE (target);
8787 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
8788 d.one_vector_p = false;
8789 d.testing_p = false;
8790
8791 base = high_p ? nelt / 2 : 0;
8792 for (i = 0; i < nelt / 2; ++i)
8793 {
8794 d.perm[i * 2] = i + base;
8795 d.perm[i * 2 + 1] = i + base + nelt;
8796 }
8797
8798 ok = loongarch_expand_vec_perm_interleave (&d);
8799 gcc_assert (ok);
8800 }
8801
8802 /* The loongarch lasx instructions xvmulwev and xvmulwod return the even or odd
8803 parts of the double sized result elements in the corresponding elements of
8804 the target register. That's NOT what the vec_widen_umult_lo/hi patterns are
8805 expected to do. We emulate the widening lo/hi multiplies with the even/odd
8806 versions followed by a vector merge. */
8807
8808 void
8809 loongarch_expand_vec_widen_hilo (rtx dest, rtx op1, rtx op2,
8810 bool uns_p, bool high_p, const char *optab)
8811 {
8812 machine_mode wmode = GET_MODE (dest);
8813 machine_mode mode = GET_MODE (op1);
8814 rtx t1, t2, t3;
8815
8816 t1 = gen_reg_rtx (wmode);
8817 t2 = gen_reg_rtx (wmode);
8818 t3 = gen_reg_rtx (wmode);
8819 switch (mode)
8820 {
8821 case V16HImode:
8822 if (!strcmp (optab, "add"))
8823 {
8824 if (!uns_p)
8825 {
8826 emit_insn (gen_lasx_xvaddwev_w_h (t1, op1, op2));
8827 emit_insn (gen_lasx_xvaddwod_w_h (t2, op1, op2));
8828 }
8829 else
8830 {
8831 emit_insn (gen_lasx_xvaddwev_w_hu (t1, op1, op2));
8832 emit_insn (gen_lasx_xvaddwod_w_hu (t2, op1, op2));
8833 }
8834 }
8835 else if (!strcmp (optab, "mult"))
8836 {
8837 if (!uns_p)
8838 {
8839 emit_insn (gen_lasx_xvmulwev_w_h (t1, op1, op2));
8840 emit_insn (gen_lasx_xvmulwod_w_h (t2, op1, op2));
8841 }
8842 else
8843 {
8844 emit_insn (gen_lasx_xvmulwev_w_hu (t1, op1, op2));
8845 emit_insn (gen_lasx_xvmulwod_w_hu (t2, op1, op2));
8846 }
8847 }
8848 else if (!strcmp (optab, "sub"))
8849 {
8850 if (!uns_p)
8851 {
8852 emit_insn (gen_lasx_xvsubwev_w_h (t1, op1, op2));
8853 emit_insn (gen_lasx_xvsubwod_w_h (t2, op1, op2));
8854 }
8855 else
8856 {
8857 emit_insn (gen_lasx_xvsubwev_w_hu (t1, op1, op2));
8858 emit_insn (gen_lasx_xvsubwod_w_hu (t2, op1, op2));
8859 }
8860 }
8861 break;
8862
8863 case V32QImode:
8864 if (!strcmp (optab, "add"))
8865 {
8866 if (!uns_p)
8867 {
8868 emit_insn (gen_lasx_xvaddwev_h_b (t1, op1, op2));
8869 emit_insn (gen_lasx_xvaddwod_h_b (t2, op1, op2));
8870 }
8871 else
8872 {
8873 emit_insn (gen_lasx_xvaddwev_h_bu (t1, op1, op2));
8874 emit_insn (gen_lasx_xvaddwod_h_bu (t2, op1, op2));
8875 }
8876 }
8877 else if (!strcmp (optab, "mult"))
8878 {
8879 if (!uns_p)
8880 {
8881 emit_insn (gen_lasx_xvmulwev_h_b (t1, op1, op2));
8882 emit_insn (gen_lasx_xvmulwod_h_b (t2, op1, op2));
8883 }
8884 else
8885 {
8886 emit_insn (gen_lasx_xvmulwev_h_bu (t1, op1, op2));
8887 emit_insn (gen_lasx_xvmulwod_h_bu (t2, op1, op2));
8888 }
8889 }
8890 else if (!strcmp (optab, "sub"))
8891 {
8892 if (!uns_p)
8893 {
8894 emit_insn (gen_lasx_xvsubwev_h_b (t1, op1, op2));
8895 emit_insn (gen_lasx_xvsubwod_h_b (t2, op1, op2));
8896 }
8897 else
8898 {
8899 emit_insn (gen_lasx_xvsubwev_h_bu (t1, op1, op2));
8900 emit_insn (gen_lasx_xvsubwod_h_bu (t2, op1, op2));
8901 }
8902 }
8903 break;
8904
8905 default:
8906 gcc_unreachable ();
8907 }
8908
8909 loongarch_expand_vec_interleave (t3, t1, t2, high_p);
8910 emit_move_insn (dest, gen_lowpart (wmode, t3));
8911 }
8912
8913 /* Expand a variable vector permutation for LASX. */
8914
8915 void
8916 loongarch_expand_vec_perm_1 (rtx operands[])
8917 {
8918 rtx target = operands[0];
8919 rtx op0 = operands[1];
8920 rtx op1 = operands[2];
8921 rtx mask = operands[3];
8922
8923 bool one_operand_shuffle = rtx_equal_p (op0, op1);
8924 rtx t1 = NULL;
8925 rtx t2 = NULL;
8926 rtx t3, t4, t5, t6, vt = NULL;
8927 rtx vec[32] = {NULL};
8928 machine_mode mode = GET_MODE (op0);
8929 machine_mode maskmode = GET_MODE (mask);
8930 int w, i;
8931
8932 /* Number of elements in the vector. */
8933 w = GET_MODE_NUNITS (mode);
8934
8935 /* If we are using xvshuf.*, clamp the selector to avoid unpredictable
8936 output. */
8937 if (maskmode != V8SImode && maskmode != V4DImode)
8938 {
8939 rtx t = gen_const_vec_duplicate (maskmode, GEN_INT (0x1f));
8940 mask = expand_binop (maskmode, and_optab, mask, t, NULL_RTX, false,
8941 OPTAB_DIRECT);
8942 }
8943
8944 if (mode == V4DImode || mode == V4DFmode)
8945 {
8946 maskmode = mode = V8SImode;
8947 w = 8;
8948 t1 = gen_reg_rtx (maskmode);
8949
8950 /* Replicate the low bits of the V4DImode mask into V8SImode:
8951 mask = { A B C D }
8952 t1 = { A A B B C C D D }. */
8953 for (i = 0; i < w / 2; ++i)
8954 vec[i*2 + 1] = vec[i*2] = GEN_INT (i * 2);
8955 vt = gen_rtx_CONST_VECTOR (maskmode, gen_rtvec_v (w, vec));
8956 vt = force_reg (maskmode, vt);
8957 mask = gen_lowpart (maskmode, mask);
8958 emit_insn (gen_lasx_xvperm_w (t1, mask, vt));
8959
8960 /* Multiply the shuffle indicies by two. */
8961 t1 = expand_simple_binop (maskmode, PLUS, t1, t1, t1, 1,
8962 OPTAB_DIRECT);
8963
8964 /* Add one to the odd shuffle indicies:
8965 t1 = { A*2, A*2+1, B*2, B*2+1, ... }. */
8966 for (i = 0; i < w / 2; ++i)
8967 {
8968 vec[i * 2] = const0_rtx;
8969 vec[i * 2 + 1] = const1_rtx;
8970 }
8971 vt = gen_rtx_CONST_VECTOR (maskmode, gen_rtvec_v (w, vec));
8972 vt = validize_mem (force_const_mem (maskmode, vt));
8973 t1 = expand_simple_binop (maskmode, PLUS, t1, vt, t1, 1,
8974 OPTAB_DIRECT);
8975
8976 /* Continue as if V8SImode (resp. V32QImode) was used initially. */
8977 operands[3] = mask = t1;
8978 target = gen_reg_rtx (mode);
8979 op0 = gen_lowpart (mode, op0);
8980 op1 = gen_lowpart (mode, op1);
8981 }
8982
8983 switch (mode)
8984 {
8985 case E_V8SImode:
8986 if (one_operand_shuffle)
8987 {
8988 emit_insn (gen_lasx_xvperm_w (target, op0, mask));
8989 if (target != operands[0])
8990 emit_move_insn (operands[0],
8991 gen_lowpart (GET_MODE (operands[0]), target));
8992 }
8993 else
8994 {
8995 t1 = gen_reg_rtx (V8SImode);
8996 t2 = gen_reg_rtx (V8SImode);
8997 emit_insn (gen_lasx_xvperm_w (t1, op0, mask));
8998 emit_insn (gen_lasx_xvperm_w (t2, op1, mask));
8999 goto merge_two;
9000 }
9001 return;
9002
9003 case E_V8SFmode:
9004 mask = gen_lowpart (V8SImode, mask);
9005 if (one_operand_shuffle)
9006 emit_insn (gen_lasx_xvperm_w_f (target, op0, mask));
9007 else
9008 {
9009 t1 = gen_reg_rtx (V8SFmode);
9010 t2 = gen_reg_rtx (V8SFmode);
9011 emit_insn (gen_lasx_xvperm_w_f (t1, op0, mask));
9012 emit_insn (gen_lasx_xvperm_w_f (t2, op1, mask));
9013 goto merge_two;
9014 }
9015 return;
9016
9017 case E_V16HImode:
9018 if (one_operand_shuffle)
9019 {
9020 t1 = gen_reg_rtx (V16HImode);
9021 t2 = gen_reg_rtx (V16HImode);
9022 emit_insn (gen_lasx_xvpermi_d_v16hi (t1, op0, GEN_INT (0x44)));
9023 emit_insn (gen_lasx_xvpermi_d_v16hi (t2, op0, GEN_INT (0xee)));
9024 emit_insn (gen_lasx_xvshuf_h (target, mask, t2, t1));
9025 }
9026 else
9027 {
9028 t1 = gen_reg_rtx (V16HImode);
9029 t2 = gen_reg_rtx (V16HImode);
9030 t3 = gen_reg_rtx (V16HImode);
9031 t4 = gen_reg_rtx (V16HImode);
9032 t5 = gen_reg_rtx (V16HImode);
9033 t6 = gen_reg_rtx (V16HImode);
9034 emit_insn (gen_lasx_xvpermi_d_v16hi (t3, op0, GEN_INT (0x44)));
9035 emit_insn (gen_lasx_xvpermi_d_v16hi (t4, op0, GEN_INT (0xee)));
9036 emit_insn (gen_lasx_xvshuf_h (t1, mask, t4, t3));
9037 emit_insn (gen_lasx_xvpermi_d_v16hi (t5, op1, GEN_INT (0x44)));
9038 emit_insn (gen_lasx_xvpermi_d_v16hi (t6, op1, GEN_INT (0xee)));
9039 emit_insn (gen_lasx_xvshuf_h (t2, mask, t6, t5));
9040 goto merge_two;
9041 }
9042 return;
9043
9044 case E_V32QImode:
9045 if (one_operand_shuffle)
9046 {
9047 t1 = gen_reg_rtx (V32QImode);
9048 t2 = gen_reg_rtx (V32QImode);
9049 emit_insn (gen_lasx_xvpermi_d_v32qi (t1, op0, GEN_INT (0x44)));
9050 emit_insn (gen_lasx_xvpermi_d_v32qi (t2, op0, GEN_INT (0xee)));
9051 emit_insn (gen_lasx_xvshuf_b (target, t2, t1, mask));
9052 }
9053 else
9054 {
9055 t1 = gen_reg_rtx (V32QImode);
9056 t2 = gen_reg_rtx (V32QImode);
9057 t3 = gen_reg_rtx (V32QImode);
9058 t4 = gen_reg_rtx (V32QImode);
9059 t5 = gen_reg_rtx (V32QImode);
9060 t6 = gen_reg_rtx (V32QImode);
9061 emit_insn (gen_lasx_xvpermi_d_v32qi (t3, op0, GEN_INT (0x44)));
9062 emit_insn (gen_lasx_xvpermi_d_v32qi (t4, op0, GEN_INT (0xee)));
9063 emit_insn (gen_lasx_xvshuf_b (t1, t4, t3, mask));
9064 emit_insn (gen_lasx_xvpermi_d_v32qi (t5, op1, GEN_INT (0x44)));
9065 emit_insn (gen_lasx_xvpermi_d_v32qi (t6, op1, GEN_INT (0xee)));
9066 emit_insn (gen_lasx_xvshuf_b (t2, t6, t5, mask));
9067 goto merge_two;
9068 }
9069 return;
9070
9071 default:
9072 gcc_assert (GET_MODE_SIZE (mode) == 32);
9073 break;
9074 }
9075
9076 merge_two:
9077 /* Then merge them together. The key is whether any given control
9078 element contained a bit set that indicates the second word. */
9079 rtx xops[6];
9080 mask = operands[3];
9081 vt = GEN_INT (w);
9082 vt = gen_const_vec_duplicate (maskmode, vt);
9083 vt = force_reg (maskmode, vt);
9084 mask = expand_simple_binop (maskmode, AND, mask, vt,
9085 NULL_RTX, 0, OPTAB_DIRECT);
9086 if (GET_MODE (target) != mode)
9087 target = gen_reg_rtx (mode);
9088 xops[0] = target;
9089 xops[1] = gen_lowpart (mode, t2);
9090 xops[2] = gen_lowpart (mode, t1);
9091 xops[3] = gen_rtx_EQ (maskmode, mask, vt);
9092 xops[4] = mask;
9093 xops[5] = vt;
9094
9095 loongarch_expand_vec_cond_expr (mode, maskmode, xops);
9096 if (target != operands[0])
9097 emit_move_insn (operands[0],
9098 gen_lowpart (GET_MODE (operands[0]), target));
9099 }
9100
9101 void
9102 loongarch_expand_vec_perm (rtx target, rtx op0, rtx op1, rtx sel)
9103 {
9104 machine_mode vmode = GET_MODE (target);
9105 machine_mode vimode = GET_MODE (sel);
9106 auto nelt = GET_MODE_NUNITS (vmode);
9107 auto round_reg = gen_reg_rtx (vimode);
9108 rtx round_data[MAX_VECT_LEN];
9109
9110 for (int i = 0; i < nelt; i += 1)
9111 {
9112 round_data[i] = GEN_INT (0x1f);
9113 }
9114
9115 rtx round_data_rtx = gen_rtx_CONST_VECTOR (vimode, gen_rtvec_v (nelt, round_data));
9116 emit_move_insn (round_reg, round_data_rtx);
9117
9118 if (vmode != vimode)
9119 {
9120 target = lowpart_subreg (vimode, target, vmode);
9121 op0 = lowpart_subreg (vimode, op0, vmode);
9122 op1 = lowpart_subreg (vimode, op1, vmode);
9123 }
9124
9125 switch (vmode)
9126 {
9127 case E_V16QImode:
9128 emit_insn (gen_andv16qi3 (sel, sel, round_reg));
9129 emit_insn (gen_lsx_vshuf_b (target, op1, op0, sel));
9130 break;
9131 case E_V2DFmode:
9132 case E_V2DImode:
9133 emit_insn (gen_andv2di3 (sel, sel, round_reg));
9134 emit_insn (gen_lsx_vshuf_d (target, sel, op1, op0));
9135 break;
9136 case E_V4SFmode:
9137 case E_V4SImode:
9138 emit_insn (gen_andv4si3 (sel, sel, round_reg));
9139 emit_insn (gen_lsx_vshuf_w (target, sel, op1, op0));
9140 break;
9141 case E_V8HImode:
9142 emit_insn (gen_andv8hi3 (sel, sel, round_reg));
9143 emit_insn (gen_lsx_vshuf_h (target, sel, op1, op0));
9144 break;
9145 default:
9146 break;
9147 }
9148 }
9149
9150 /* Following are the assist function for const vector permutation support. */
9151 static bool
9152 loongarch_is_quad_duplicate (struct expand_vec_perm_d *d)
9153 {
9154 if (d->perm[0] >= d->nelt / 2)
9155 return false;
9156
9157 bool result = true;
9158 unsigned char lhs = d->perm[0];
9159 unsigned char rhs = d->perm[d->nelt / 2];
9160
9161 if ((rhs - lhs) != d->nelt / 2)
9162 return false;
9163
9164 for (int i = 1; i < d->nelt; i += 1)
9165 {
9166 if ((i < d->nelt / 2) && (d->perm[i] != lhs))
9167 {
9168 result = false;
9169 break;
9170 }
9171 if ((i > d->nelt / 2) && (d->perm[i] != rhs))
9172 {
9173 result = false;
9174 break;
9175 }
9176 }
9177
9178 return result;
9179 }
9180
9181 static bool
9182 loongarch_is_extraction_permutation (struct expand_vec_perm_d *d)
9183 {
9184 bool result = true;
9185 unsigned char buf = d->perm[0];
9186
9187 if (buf != 0 || buf != d->nelt)
9188 return false;
9189
9190 for (int i = 0; i < d->nelt; i += 1)
9191 {
9192 if (buf != d->perm[i])
9193 {
9194 result = false;
9195 break;
9196 }
9197 buf += 1;
9198 }
9199
9200 return result;
9201 }
9202
9203 static bool
9204 loongarch_is_lasx_lowpart_interleave (struct expand_vec_perm_d *d)
9205 {
9206 bool result = true;
9207 unsigned char buf = 0;
9208
9209 for (int i = 0;i < d->nelt; i += 2)
9210 {
9211 if (buf != d->perm[i])
9212 {
9213 result = false;
9214 break;
9215 }
9216 buf += 1;
9217 }
9218
9219 if (result)
9220 {
9221 buf = d->nelt;
9222 for (int i = 1; i < d->nelt; i += 2)
9223 {
9224 if (buf != d->perm[i])
9225 {
9226 result = false;
9227 break;
9228 }
9229 buf += 1;
9230 }
9231 }
9232
9233 return result;
9234 }
9235
9236 static bool
9237 loongarch_is_lasx_lowpart_interleave_2 (struct expand_vec_perm_d *d)
9238 {
9239 if (d->vmode != E_V32QImode)
9240 return false;
9241 bool result = true;
9242 unsigned char buf = 0;
9243
9244 #define COMPARE_SELECTOR(INIT, BEGIN, END) \
9245 buf = INIT; \
9246 for (int i = BEGIN; i < END && result; i += 1) \
9247 { \
9248 if (buf != d->perm[i]) \
9249 { \
9250 result = false; \
9251 break; \
9252 } \
9253 buf += 1; \
9254 }
9255
9256 COMPARE_SELECTOR (0, 0, 8);
9257 COMPARE_SELECTOR (32, 8, 16);
9258 COMPARE_SELECTOR (8, 16, 24);
9259 COMPARE_SELECTOR (40, 24, 32);
9260
9261 #undef COMPARE_SELECTOR
9262 return result;
9263 }
9264
9265 static bool
9266 loongarch_is_lasx_highpart_interleave (expand_vec_perm_d *d)
9267 {
9268 bool result = true;
9269 unsigned char buf = d->nelt / 2;
9270
9271 for (int i = 0; i < d->nelt; i += 2)
9272 {
9273 if (buf != d->perm[i])
9274 {
9275 result = false;
9276 break;
9277 }
9278 buf += 1;
9279 }
9280
9281 if (result)
9282 {
9283 buf = d->nelt + d->nelt / 2;
9284 for (int i = 1; i < d->nelt;i += 2)
9285 {
9286 if (buf != d->perm[i])
9287 {
9288 result = false;
9289 break;
9290 }
9291 buf += 1;
9292 }
9293 }
9294
9295 return result;
9296 }
9297
9298 static bool
9299 loongarch_is_lasx_highpart_interleave_2 (struct expand_vec_perm_d *d)
9300 {
9301 if (d->vmode != E_V32QImode)
9302 return false;
9303
9304 bool result = true;
9305 unsigned char buf = 0;
9306
9307 #define COMPARE_SELECTOR(INIT, BEGIN, END) \
9308 buf = INIT; \
9309 for (int i = BEGIN; i < END && result; i += 1) \
9310 { \
9311 if (buf != d->perm[i]) \
9312 { \
9313 result = false; \
9314 break; \
9315 } \
9316 buf += 1; \
9317 }
9318
9319 COMPARE_SELECTOR (16, 0, 8);
9320 COMPARE_SELECTOR (48, 8, 16);
9321 COMPARE_SELECTOR (24, 16, 24);
9322 COMPARE_SELECTOR (56, 24, 32);
9323
9324 #undef COMPARE_SELECTOR
9325 return result;
9326 }
9327
9328 static bool
9329 loongarch_is_elem_duplicate (struct expand_vec_perm_d *d)
9330 {
9331 bool result = true;
9332 unsigned char buf = d->perm[0];
9333
9334 for (int i = 0; i < d->nelt; i += 1)
9335 {
9336 if (buf != d->perm[i])
9337 {
9338 result = false;
9339 break;
9340 }
9341 }
9342
9343 return result;
9344 }
9345
9346 /* In LASX, some permutation insn does not have the behavior that gcc expects
9347 when compiler wants to emit a vector permutation.
9348
9349 1. What GCC provides via vectorize_vec_perm_const ()'s paramater:
9350 When GCC wants to performs a vector permutation, it provides two op
9351 reigster, one target register, and a selector.
9352 In const vector permutation case, GCC provides selector as a char array
9353 that contains original value; in variable vector permuatation
9354 (performs via vec_perm<mode> insn template), it provides a vector register.
9355 We assume that nelt is the elements numbers inside single vector in current
9356 256bit vector mode.
9357
9358 2. What GCC expects to perform:
9359 Two op registers (op0, op1) will "combine" into a 512bit temp vector storage
9360 that has 2*nelt elements inside it; the low 256bit is op0, and high 256bit
9361 is op1, then the elements are indexed as below:
9362 0 ~ nelt - 1 nelt ~ 2 * nelt - 1
9363 |-------------------------|-------------------------|
9364 Low 256bit (op0) High 256bit (op1)
9365 For example, the second element in op1 (V8SImode) will be indexed with 9.
9366 Selector is a vector that has the same mode and number of elements with
9367 op0,op1 and target, it's look like this:
9368 0 ~ nelt - 1
9369 |-------------------------|
9370 256bit (selector)
9371 It describes which element from 512bit temp vector storage will fit into
9372 target's every element slot.
9373 GCC expects that every element in selector can be ANY indices of 512bit
9374 vector storage (Selector can pick literally any element from op0 and op1, and
9375 then fits into any place of target register). This is also what LSX 128bit
9376 vshuf.* instruction do similarly, so we can handle 128bit vector permutation
9377 by single instruction easily.
9378
9379 3. What LASX permutation instruction does:
9380 In short, it just execute two independent 128bit vector permuatation, and
9381 it's the reason that we need to do the jobs below. We will explain it.
9382 op0, op1, target, and selector will be separate into high 128bit and low
9383 128bit, and do permutation as the description below:
9384
9385 a) op0's low 128bit and op1's low 128bit "combines" into a 256bit temp
9386 vector storage (TVS1), elements are indexed as below:
9387 0 ~ nelt / 2 - 1 nelt / 2 ~ nelt - 1
9388 |---------------------|---------------------| TVS1
9389 op0's low 128bit op1's low 128bit
9390 op0's high 128bit and op1's high 128bit are "combined" into TVS2 in the
9391 same way.
9392 0 ~ nelt / 2 - 1 nelt / 2 ~ nelt - 1
9393 |---------------------|---------------------| TVS2
9394 op0's high 128bit op1's high 128bit
9395 b) Selector's low 128bit describes which elements from TVS1 will fit into
9396 target vector's low 128bit. No TVS2 elements are allowed.
9397 c) Selector's high 128bit describes which elements from TVS2 will fit into
9398 target vector's high 128bit. No TVS1 elements are allowed.
9399
9400 As we can see, if we want to handle vector permutation correctly, we can
9401 achieve it in three ways:
9402 a) Modify selector's elements, to make sure that every elements can inform
9403 correct value that will put into target vector.
9404 b) Generate extra instruction before/after permutation instruction, for
9405 adjusting op vector or target vector, to make sure target vector's value is
9406 what GCC expects.
9407 c) Use other instructions to process op and put correct result into target.
9408 */
9409
9410 /* Implementation of constant vector permuatation. This function identifies
9411 recognized pattern of permuation selector argument, and use one or more
9412 instruction (s) to finish the permutation job correctly. For unsupported
9413 patterns, it will return false. */
9414
9415 static bool
9416 loongarch_expand_vec_perm_const (struct expand_vec_perm_d *d)
9417 {
9418 bool flag = false;
9419 unsigned int i;
9420 unsigned char idx;
9421 rtx target, op0, op1;
9422 rtx rperm[MAX_VECT_LEN];
9423 unsigned int remapped[MAX_VECT_LEN];
9424 unsigned char perm2[MAX_VECT_LEN];
9425
9426 if (GET_MODE_SIZE (d->vmode) == 16)
9427 return loongarch_expand_lsx_shuffle (d);
9428 else
9429 {
9430 if (d->one_vector_p)
9431 {
9432 /* Try interleave with alternating operands. */
9433 memcpy (perm2, d->perm, sizeof (perm2));
9434 for (i = 1; i < d->nelt; i += 2)
9435 perm2[i] += d->nelt;
9436 if (loongarch_expand_vselect_vconcat (d->target, d->op0, d->op1,
9437 perm2, d->nelt, d->testing_p))
9438 return true;
9439 }
9440 else
9441 {
9442 if (loongarch_expand_vselect_vconcat (d->target, d->op0, d->op1,
9443 d->perm, d->nelt,
9444 d->testing_p))
9445 return true;
9446
9447 /* Try again with swapped operands. */
9448 for (i = 0; i < d->nelt; ++i)
9449 perm2[i] = (d->perm[i] + d->nelt) & (2 * d->nelt - 1);
9450 if (loongarch_expand_vselect_vconcat (d->target, d->op1, d->op0,
9451 perm2, d->nelt, d->testing_p))
9452 return true;
9453 }
9454
9455 if (loongarch_is_imm_set_shuffle (d))
9456 return true;
9457
9458 if (loongarch_expand_vec_perm_even_odd (d))
9459 return true;
9460
9461 if (loongarch_is_lasx_lowpart_interleave (d)
9462 || loongarch_is_lasx_lowpart_interleave_2 (d)
9463 || loongarch_is_lasx_highpart_interleave (d)
9464 || loongarch_is_lasx_highpart_interleave_2 (d))
9465 {
9466 if (loongarch_expand_vec_perm_interleave (d))
9467 return true;
9468 }
9469
9470 if (loongarch_is_quad_duplicate (d))
9471 {
9472 if (d->testing_p)
9473 return true;
9474 /* Selector example: E_V8SImode, { 0, 0, 0, 0, 4, 4, 4, 4 }. */
9475 for (i = 0; i < d->nelt; i += 1)
9476 {
9477 rperm[i] = GEN_INT (d->perm[0]);
9478 }
9479 /* Selector after: { 0, 0, 0, 0, 0, 0, 0, 0 }. */
9480 flag = true;
9481 goto expand_perm_const_end;
9482 }
9483
9484 if (loongarch_is_extraction_permutation (d))
9485 {
9486 if (d->testing_p)
9487 return true;
9488 /* Selector sample: E_V8SImode, { 0, 1, 2, 3, 4, 5, 6, 7 }. */
9489 if (d->perm[0] == 0)
9490 {
9491 for (i = 0; i < d->nelt / 2; i += 1)
9492 {
9493 remapped[i] = i;
9494 remapped[i + d->nelt / 2] = i;
9495 }
9496 }
9497 else
9498 {
9499 /* { 8, 9, 10, 11, 12, 13, 14, 15 }. */
9500 for (i = 0; i < d->nelt / 2; i += 1)
9501 {
9502 idx = i + d->nelt / 2;
9503 remapped[i] = idx;
9504 remapped[i + d->nelt / 2] = idx;
9505 }
9506 }
9507 /* Selector after: { 0, 1, 2, 3, 0, 1, 2, 3 }
9508 { 8, 9, 10, 11, 8, 9, 10, 11 } */
9509
9510 /* Convert remapped selector array to RTL array. */
9511 for (i = 0; i < d->nelt; i += 1)
9512 {
9513 rperm[i] = GEN_INT (remapped[i]);
9514 }
9515
9516 flag = true;
9517 goto expand_perm_const_end;
9518 }
9519
9520 if (loongarch_is_elem_duplicate (d))
9521 {
9522 if (d->testing_p)
9523 return true;
9524 /* Brocast single element (from op0 or op1) to all slot of target
9525 register.
9526 Selector sample:E_V8SImode, { 2, 2, 2, 2, 2, 2, 2, 2 } */
9527 rtx conv_op1 = simplify_gen_subreg (E_V4DImode, d->op1, d->vmode, 0);
9528 rtx conv_op0 = simplify_gen_subreg (E_V4DImode, d->op0, d->vmode, 0);
9529 rtx temp_reg = gen_reg_rtx (d->vmode);
9530 rtx conv_temp = simplify_gen_subreg (E_V4DImode, temp_reg,
9531 d->vmode, 0);
9532 emit_move_insn (temp_reg, d->op0);
9533
9534 idx = d->perm[0];
9535 /* We will use xvrepl128vei.* insn to achieve the result, but we need
9536 to make the high/low 128bit has the same contents that contain the
9537 value that we need to broardcast, because xvrepl128vei does the
9538 broardcast job from every 128bit of source register to
9539 corresponded part of target register! (A deep sigh.) */
9540 if (idx < d->nelt / 2)
9541 {
9542 emit_insn (gen_lasx_xvpermi_q_v4di (conv_temp, conv_temp,
9543 conv_op0, GEN_INT (0x0)));
9544 }
9545 else if (idx >= d->nelt / 2 && idx < d->nelt)
9546 {
9547 emit_insn (gen_lasx_xvpermi_q_v4di (conv_temp, conv_temp,
9548 conv_op0, GEN_INT (0x11)));
9549 idx -= d->nelt / 2;
9550 }
9551 else if (idx >= d->nelt && idx < (d->nelt + d->nelt / 2))
9552 {
9553 emit_insn (gen_lasx_xvpermi_q_v4di (conv_temp, conv_temp,
9554 conv_op1, GEN_INT (0x0)));
9555 }
9556 else if (idx >= (d->nelt + d->nelt / 2) && idx < d->nelt * 2)
9557 {
9558 emit_insn (gen_lasx_xvpermi_q_v4di (conv_temp, conv_temp,
9559 conv_op1, GEN_INT (0x11)));
9560 idx -= d->nelt / 2;
9561 }
9562
9563 /* Then we can finally generate this insn. */
9564 switch (d->vmode)
9565 {
9566 case E_V4DImode:
9567 emit_insn (gen_lasx_xvrepl128vei_d (d->target, temp_reg,
9568 GEN_INT (idx)));
9569 break;
9570 case E_V4DFmode:
9571 emit_insn (gen_lasx_xvrepl128vei_d_f (d->target, temp_reg,
9572 GEN_INT (idx)));
9573 break;
9574 case E_V8SImode:
9575 emit_insn (gen_lasx_xvrepl128vei_w (d->target, temp_reg,
9576 GEN_INT (idx)));
9577 break;
9578 case E_V8SFmode:
9579 emit_insn (gen_lasx_xvrepl128vei_w_f (d->target, temp_reg,
9580 GEN_INT (idx)));
9581 break;
9582 case E_V16HImode:
9583 emit_insn (gen_lasx_xvrepl128vei_h (d->target, temp_reg,
9584 GEN_INT (idx)));
9585 break;
9586 case E_V32QImode:
9587 emit_insn (gen_lasx_xvrepl128vei_b (d->target, temp_reg,
9588 GEN_INT (idx)));
9589 break;
9590 default:
9591 gcc_unreachable ();
9592 break;
9593 }
9594
9595 return true;
9596 }
9597
9598 expand_perm_const_end:
9599 if (flag)
9600 {
9601 target = d->target;
9602 op0 = d->op0;
9603 op1 = d->one_vector_p ? d->op0 : d->op1;
9604
9605 machine_mode sel_mode = related_int_vector_mode (d->vmode)
9606 .require ();
9607 rtvec sel_v = gen_rtvec_v (d->nelt, rperm);
9608
9609 /* See the comment in loongarch_expand_lsx_shuffle for why
9610 we don't simply use a SUBREG to pun target. */
9611 rtx sel = force_reg (sel_mode,
9612 gen_rtx_CONST_VECTOR (sel_mode, sel_v));
9613
9614 if (d->vmode == E_V32QImode)
9615 emit_insn (gen_lasx_xvshuf_b (target, op1, op0, sel));
9616 else
9617 emit_insn (gen_lasx_xvshuf (d->vmode, target, sel, op1, op0));
9618
9619 return true;
9620 }
9621 }
9622
9623 return false;
9624 }
9625
9626 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST. */
9627
9628 static bool
9629 loongarch_vectorize_vec_perm_const (machine_mode vmode, machine_mode op_mode,
9630 rtx target, rtx op0, rtx op1,
9631 const vec_perm_indices &sel)
9632 {
9633 if (vmode != op_mode)
9634 return false;
9635
9636 struct expand_vec_perm_d d;
9637 int i, nelt, which;
9638 unsigned char orig_perm[MAX_VECT_LEN];
9639 bool ok;
9640
9641 d.target = target;
9642 if (op0)
9643 {
9644 rtx nop0 = force_reg (vmode, op0);
9645 if (op0 == op1)
9646 op1 = nop0;
9647 op0 = nop0;
9648 }
9649 if (op1)
9650 op1 = force_reg (vmode, op1);
9651 d.op0 = op0;
9652 d.op1 = op1;
9653
9654 d.vmode = vmode;
9655 gcc_assert (VECTOR_MODE_P (vmode));
9656 d.nelt = nelt = GET_MODE_NUNITS (vmode);
9657 d.testing_p = !target;
9658
9659 /* This is overly conservative, but ensures we don't get an
9660 uninitialized warning on ORIG_PERM. */
9661 memset (orig_perm, 0, MAX_VECT_LEN);
9662 for (i = which = 0; i < nelt; ++i)
9663 {
9664 int ei = sel[i] & (2 * nelt - 1);
9665 which |= (ei < nelt ? 1 : 2);
9666 orig_perm[i] = ei;
9667 }
9668 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
9669
9670 switch (which)
9671 {
9672 default:
9673 gcc_unreachable ();
9674
9675 case 3:
9676 d.one_vector_p = false;
9677 if (d.testing_p || !rtx_equal_p (d.op0, d.op1))
9678 break;
9679 /* FALLTHRU */
9680
9681 case 2:
9682 for (i = 0; i < nelt; ++i)
9683 d.perm[i] &= nelt - 1;
9684 d.op0 = d.op1;
9685 d.one_vector_p = true;
9686 break;
9687
9688 case 1:
9689 d.op1 = d.op0;
9690 d.one_vector_p = true;
9691 break;
9692 }
9693
9694 // Do rounding for selector to avoid vshuf undefined behavior.
9695 for (i = 0; i < d.nelt; i += 1)
9696 {
9697 d.perm[i] %= (d.nelt * 2);
9698 }
9699
9700 if (d.testing_p)
9701 {
9702 d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
9703 d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
9704 if (!d.one_vector_p)
9705 d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
9706
9707 start_sequence ();
9708 ok = loongarch_expand_vec_perm_const (&d);
9709 end_sequence ();
9710 return ok;
9711 }
9712
9713 ok = loongarch_expand_vec_perm_const (&d);
9714
9715 /* If we were given a two-vector permutation which just happened to
9716 have both input vectors equal, we folded this into a one-vector
9717 permutation. There are several loongson patterns that are matched
9718 via direct vec_select+vec_concat expansion, but we do not have
9719 support in loongarch_expand_vec_perm_const to guess the adjustment
9720 that should be made for a single operand. Just try again with
9721 the original permutation. */
9722 if (!ok && which == 3)
9723 {
9724 d.op0 = op0;
9725 d.op1 = op1;
9726 d.one_vector_p = false;
9727 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
9728 ok = loongarch_expand_vec_perm_const (&d);
9729 }
9730
9731 return ok;
9732 }
9733
9734 static int
9735 loongarch_cpu_sched_reassociation_width (struct loongarch_target *target,
9736 unsigned int opc, machine_mode mode)
9737 {
9738 /* unreferenced argument */
9739 (void) opc;
9740
9741 switch (target->cpu_tune)
9742 {
9743 case TUNE_GENERIC:
9744 case TUNE_LOONGARCH64:
9745 case TUNE_LA464:
9746 case TUNE_LA664:
9747 /* Vector part. */
9748 if (LSX_SUPPORTED_MODE_P (mode) || LASX_SUPPORTED_MODE_P (mode))
9749 {
9750 /* Integer vector instructions execute in FP unit.
9751 The width of integer/float-point vector instructions is 3. */
9752 return 3;
9753 }
9754
9755 /* Scalar part. */
9756 else if (INTEGRAL_MODE_P (mode))
9757 return 1;
9758 else if (FLOAT_MODE_P (mode))
9759 {
9760 if (opc == PLUS_EXPR)
9761 {
9762 return 2;
9763 }
9764 return 4;
9765 }
9766 break;
9767 default:
9768 break;
9769 }
9770
9771 /* default is 1 */
9772 return 1;
9773 }
9774
9775 /* Implement TARGET_SCHED_REASSOCIATION_WIDTH. */
9776
9777 static int
9778 loongarch_sched_reassociation_width (unsigned int opc, machine_mode mode)
9779 {
9780 return loongarch_cpu_sched_reassociation_width (&la_target, opc, mode);
9781 }
9782
9783 /* Implement extract a scalar element from vecotr register */
9784
9785 void
9786 loongarch_expand_vector_extract (rtx target, rtx vec, int elt)
9787 {
9788 machine_mode mode = GET_MODE (vec);
9789 machine_mode inner_mode = GET_MODE_INNER (mode);
9790 rtx tmp;
9791
9792 switch (mode)
9793 {
9794 case E_V8HImode:
9795 case E_V16QImode:
9796 break;
9797
9798 case E_V32QImode:
9799 if (ISA_HAS_LASX)
9800 {
9801 if (elt >= 16)
9802 {
9803 tmp = gen_reg_rtx (V32QImode);
9804 emit_insn (gen_lasx_xvpermi_d_v32qi (tmp, vec, GEN_INT (0xe)));
9805 loongarch_expand_vector_extract (target,
9806 gen_lowpart (V16QImode, tmp),
9807 elt & 15);
9808 }
9809 else
9810 loongarch_expand_vector_extract (target,
9811 gen_lowpart (V16QImode, vec),
9812 elt & 15);
9813 return;
9814 }
9815 break;
9816
9817 case E_V16HImode:
9818 if (ISA_HAS_LASX)
9819 {
9820 if (elt >= 8)
9821 {
9822 tmp = gen_reg_rtx (V16HImode);
9823 emit_insn (gen_lasx_xvpermi_d_v16hi (tmp, vec, GEN_INT (0xe)));
9824 loongarch_expand_vector_extract (target,
9825 gen_lowpart (V8HImode, tmp),
9826 elt & 7);
9827 }
9828 else
9829 loongarch_expand_vector_extract (target,
9830 gen_lowpart (V8HImode, vec),
9831 elt & 7);
9832 return;
9833 }
9834 break;
9835
9836 default:
9837 break;
9838 }
9839
9840 tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
9841 tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
9842
9843 /* Let the rtl optimizers know about the zero extension performed. */
9844 if (inner_mode == QImode || inner_mode == HImode)
9845 {
9846 tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
9847 target = gen_lowpart (SImode, target);
9848 }
9849 if (inner_mode == SImode || inner_mode == DImode)
9850 {
9851 tmp = gen_rtx_SIGN_EXTEND (inner_mode, tmp);
9852 }
9853
9854 emit_insn (gen_rtx_SET (target, tmp));
9855 }
9856
9857 /* Generate code to copy vector bits i / 2 ... i - 1 from vector SRC
9858 to bits 0 ... i / 2 - 1 of vector DEST, which has the same mode.
9859 The upper bits of DEST are undefined, though they shouldn't cause
9860 exceptions (some bits from src or all zeros are ok). */
9861
9862 static void
9863 emit_reduc_half (rtx dest, rtx src, int i)
9864 {
9865 rtx tem, d = dest;
9866 switch (GET_MODE (src))
9867 {
9868 case E_V4SFmode:
9869 tem = gen_lsx_vbsrl_w_f (dest, src, GEN_INT (i == 128 ? 8 : 4));
9870 break;
9871 case E_V2DFmode:
9872 tem = gen_lsx_vbsrl_d_f (dest, src, GEN_INT (8));
9873 break;
9874 case E_V8SFmode:
9875 if (i == 256)
9876 tem = gen_lasx_xvpermi_d_v8sf (dest, src, GEN_INT (0xe));
9877 else
9878 tem = gen_lasx_xvshuf4i_w_f (dest, src,
9879 GEN_INT (i == 128 ? 2 + (3 << 2) : 1));
9880 break;
9881 case E_V4DFmode:
9882 if (i == 256)
9883 tem = gen_lasx_xvpermi_d_v4df (dest, src, GEN_INT (0xe));
9884 else
9885 tem = gen_lasx_xvpermi_d_v4df (dest, src, const1_rtx);
9886 break;
9887 case E_V32QImode:
9888 case E_V16HImode:
9889 case E_V8SImode:
9890 case E_V4DImode:
9891 d = gen_reg_rtx (V4DImode);
9892 if (i == 256)
9893 tem = gen_lasx_xvpermi_d_v4di (d, gen_lowpart (V4DImode, src),
9894 GEN_INT (0xe));
9895 else
9896 tem = gen_lasx_xvbsrl_d (d, gen_lowpart (V4DImode, src),
9897 GEN_INT (i/16));
9898 break;
9899 case E_V16QImode:
9900 case E_V8HImode:
9901 case E_V4SImode:
9902 case E_V2DImode:
9903 d = gen_reg_rtx (V2DImode);
9904 tem = gen_lsx_vbsrl_d (d, gen_lowpart (V2DImode, src), GEN_INT (i/16));
9905 break;
9906 default:
9907 gcc_unreachable ();
9908 }
9909 emit_insn (tem);
9910 if (d != dest)
9911 emit_move_insn (dest, gen_lowpart (GET_MODE (dest), d));
9912 }
9913
9914 /* Expand a vector reduction. FN is the binary pattern to reduce;
9915 DEST is the destination; IN is the input vector. */
9916
9917 void
9918 loongarch_expand_vector_reduc (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
9919 {
9920 rtx half, dst, vec = in;
9921 machine_mode mode = GET_MODE (in);
9922 int i;
9923
9924 for (i = GET_MODE_BITSIZE (mode);
9925 i > GET_MODE_UNIT_BITSIZE (mode);
9926 i >>= 1)
9927 {
9928 half = gen_reg_rtx (mode);
9929 emit_reduc_half (half, vec, i);
9930 if (i == GET_MODE_UNIT_BITSIZE (mode) * 2)
9931 dst = dest;
9932 else
9933 dst = gen_reg_rtx (mode);
9934 emit_insn (fn (dst, half, vec));
9935 vec = dst;
9936 }
9937 }
9938
9939 /* Expand an integral vector unpack operation. */
9940
9941 void
9942 loongarch_expand_vec_unpack (rtx operands[2], bool unsigned_p)
9943 {
9944 machine_mode imode = GET_MODE (operands[1]);
9945 rtx (*unpack) (rtx, rtx, rtx);
9946 rtx (*extend) (rtx, rtx);
9947 rtx (*cmpFunc) (rtx, rtx, rtx);
9948 rtx (*swap_hi_lo) (rtx, rtx, rtx, rtx);
9949 rtx tmp, dest;
9950
9951 /* In LASX, only vec_unpacks_hi_<mode> requires expander. */
9952 if (ISA_HAS_LASX && GET_MODE_SIZE (imode) == 32)
9953 {
9954 switch (imode)
9955 {
9956 case E_V8SImode:
9957 if (unsigned_p)
9958 extend = gen_vec_unpacku_lo_v8si;
9959 else
9960 extend = gen_vec_unpacks_lo_v8si;
9961 swap_hi_lo = gen_lasx_xvpermi_q_v8si;
9962 break;
9963
9964 case E_V16HImode:
9965 if (unsigned_p)
9966 extend = gen_vec_unpacku_lo_v16hi;
9967 else
9968 extend = gen_vec_unpacks_lo_v16hi;
9969 swap_hi_lo = gen_lasx_xvpermi_q_v16hi;
9970 break;
9971
9972 case E_V32QImode:
9973 if (unsigned_p)
9974 extend = gen_vec_unpacku_lo_v32qi;
9975 else
9976 extend = gen_vec_unpacks_lo_v32qi;
9977 swap_hi_lo = gen_lasx_xvpermi_q_v32qi;
9978 break;
9979
9980 default:
9981 gcc_unreachable ();
9982 break;
9983 }
9984
9985 tmp = gen_reg_rtx (imode);
9986 emit_insn (swap_hi_lo (tmp, tmp, operands[1], const1_rtx));
9987 emit_insn (extend (operands[0], tmp));
9988 return;
9989 }
9990 /* In LSX, only vec_unpacks_lo_<mode> requires expander. */
9991 else if (ISA_HAS_LSX && !ISA_HAS_LASX)
9992 {
9993 switch (imode)
9994 {
9995 case E_V4SImode:
9996 unpack = gen_lsx_vilvl_w;
9997 cmpFunc = gen_lsx_vslt_w;
9998 break;
9999
10000 case E_V8HImode:
10001 unpack = gen_lsx_vilvl_h;
10002 cmpFunc = gen_lsx_vslt_h;
10003 break;
10004
10005 case E_V16QImode:
10006 unpack = gen_lsx_vilvl_b;
10007 cmpFunc = gen_lsx_vslt_b;
10008 break;
10009
10010 default:
10011 gcc_unreachable ();
10012 break;
10013 }
10014
10015 if (!unsigned_p)
10016 {
10017 /* Extract sign extention for each element comparing each element
10018 with immediate zero. */
10019 tmp = gen_reg_rtx (imode);
10020 emit_insn (cmpFunc (tmp, operands[1], CONST0_RTX (imode)));
10021 }
10022 else
10023 tmp = force_reg (imode, CONST0_RTX (imode));
10024
10025 dest = gen_reg_rtx (imode);
10026
10027 emit_insn (unpack (dest, operands[1], tmp));
10028 emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
10029 return;
10030 }
10031 gcc_unreachable ();
10032 }
10033
10034 /* Construct and return PARALLEL RTX with CONST_INTs for HIGH (high_p == TRUE)
10035 or LOW (high_p == FALSE) half of a vector for mode MODE. */
10036
10037 rtx
10038 loongarch_lsx_vec_parallel_const_half (machine_mode mode, bool high_p)
10039 {
10040 int nunits = GET_MODE_NUNITS (mode);
10041 rtvec v = rtvec_alloc (nunits / 2);
10042 int base;
10043 int i;
10044
10045 base = high_p ? nunits / 2 : 0;
10046
10047 for (i = 0; i < nunits / 2; i++)
10048 RTVEC_ELT (v, i) = GEN_INT (base + i);
10049
10050 return gen_rtx_PARALLEL (VOIDmode, v);
10051 }
10052
10053 /* A subroutine of loongarch_expand_vec_init, match constant vector
10054 elements. */
10055
10056 static inline bool
10057 loongarch_constant_elt_p (rtx x)
10058 {
10059 return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
10060 }
10061
10062 rtx
10063 loongarch_gen_const_int_vector_shuffle (machine_mode mode, int val)
10064 {
10065 int nunits = GET_MODE_NUNITS (mode);
10066 int nsets = nunits / 4;
10067 rtx elts[MAX_VECT_LEN];
10068 int set = 0;
10069 int i, j;
10070
10071 /* Generate a const_int vector replicating the same 4-element set
10072 from an immediate. */
10073 for (j = 0; j < nsets; j++, set = 4 * j)
10074 for (i = 0; i < 4; i++)
10075 elts[set + i] = GEN_INT (set + ((val >> (2 * i)) & 0x3));
10076
10077 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nunits, elts));
10078 }
10079
10080
10081 /* Expand a vector initialization. */
10082
10083 void
10084 loongarch_expand_vector_group_init (rtx target, rtx vals)
10085 {
10086 machine_mode vmode = GET_MODE (target);
10087 machine_mode half_mode = VOIDmode;
10088 rtx low = XVECEXP (vals, 0, 0);
10089 rtx high = XVECEXP (vals, 0, 1);
10090
10091 switch (vmode)
10092 {
10093 case E_V32QImode:
10094 half_mode = V16QImode;
10095 break;
10096 case E_V16HImode:
10097 half_mode = V8HImode;
10098 break;
10099 case E_V8SImode:
10100 half_mode = V4SImode;
10101 break;
10102 case E_V4DImode:
10103 half_mode = V2DImode;
10104 break;
10105 case E_V8SFmode:
10106 half_mode = V4SFmode;
10107 break;
10108 case E_V4DFmode:
10109 half_mode = V2DFmode;
10110 break;
10111 default:
10112 gcc_unreachable ();
10113 }
10114
10115 if (!register_operand (low, half_mode))
10116 low = force_reg (half_mode, low);
10117 if (!register_operand (high, half_mode))
10118 high = force_reg (half_mode, high);
10119 emit_insn (gen_rtx_SET (target,
10120 gen_rtx_VEC_CONCAT (vmode, low, high)));
10121 }
10122
10123 /* Expand initialization of a vector which has all same elements. */
10124
10125 void
10126 loongarch_expand_vector_init_same (rtx target, rtx vals, unsigned nvar)
10127 {
10128 machine_mode vmode = GET_MODE (target);
10129 machine_mode imode = GET_MODE_INNER (vmode);
10130 rtx same = XVECEXP (vals, 0, 0);
10131 rtx temp;
10132
10133 if (CONST_INT_P (same) && nvar == 0
10134 && loongarch_signed_immediate_p (INTVAL (same), 10, 0))
10135 {
10136 switch (vmode)
10137 {
10138 case E_V32QImode:
10139 case E_V16HImode:
10140 case E_V8SImode:
10141 case E_V4DImode:
10142 case E_V16QImode:
10143 case E_V8HImode:
10144 case E_V4SImode:
10145 case E_V2DImode:
10146 temp = gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0));
10147 emit_move_insn (target, temp);
10148 return;
10149 default:
10150 gcc_unreachable ();
10151 }
10152 }
10153
10154 temp = force_reg (imode, same);
10155
10156 switch (vmode)
10157 {
10158 case E_V32QImode:
10159 case E_V16HImode:
10160 case E_V8SImode:
10161 case E_V4DImode:
10162 case E_V16QImode:
10163 case E_V8HImode:
10164 case E_V4SImode:
10165 case E_V2DImode:
10166 loongarch_emit_move (target, gen_rtx_VEC_DUPLICATE (vmode, temp));
10167 break;
10168
10169 case E_V8SFmode:
10170 emit_insn (gen_lasx_xvreplve0_w_f_scalar (target, temp));
10171 break;
10172
10173 case E_V4DFmode:
10174 emit_insn (gen_lasx_xvreplve0_d_f_scalar (target, temp));
10175 break;
10176
10177 case E_V4SFmode:
10178 emit_insn (gen_lsx_vreplvei_w_f_scalar (target, temp));
10179 break;
10180
10181 case E_V2DFmode:
10182 emit_insn (gen_lsx_vreplvei_d_f_scalar (target, temp));
10183 break;
10184
10185 default:
10186 gcc_unreachable ();
10187 }
10188 }
10189
10190 /* Expand a vector initialization. */
10191
10192 void
10193 loongarch_expand_vector_init (rtx target, rtx vals)
10194 {
10195 machine_mode vmode = GET_MODE (target);
10196 machine_mode imode = GET_MODE_INNER (vmode);
10197 unsigned i, nelt = GET_MODE_NUNITS (vmode);
10198 /* VALS is divided into high and low half-part. */
10199 /* Number of non constant elements in corresponding parts of VALS. */
10200 unsigned nvar = 0, hi_nvar = 0, lo_nvar = 0;
10201 /* all_same : true if all elements of VALS are the same.
10202 hi_same : true if all elements of the high half-part are the same.
10203 lo_same : true if all elements of the low half-part are the same.
10204 half_same : true if the high half-part is the same as the low one. */
10205 bool all_same = false, hi_same = true, lo_same = true, half_same = true;
10206 rtx val[32], val_hi[32], val_lo[16];
10207 rtx x, op0, op1;
10208 /* Copy one element of vals to per element of target vector. */
10209 typedef rtx (*loongarch_vec_repl1_fn) (rtx, rtx);
10210 /* Copy two elements of vals to target vector. */
10211 typedef rtx (*loongarch_vec_repl2_fn) (rtx, rtx, rtx);
10212 /* Insert scalar operands into the specified position of the vector. */
10213 typedef rtx (*loongarch_vec_set_fn) (rtx, rtx, rtx);
10214 /* Copy 64bit lowpart to highpart. */
10215 typedef rtx (*loongarch_vec_mirror_fn) (rtx, rtx, rtx);
10216 /* Merge lowpart and highpart into target. */
10217 typedef rtx (*loongarch_vec_merge_fn) (rtx, rtx, rtx, rtx);
10218
10219 loongarch_vec_repl1_fn loongarch_vec_repl1_128 = NULL,
10220 loongarch_vec_repl1_256 = NULL;
10221 loongarch_vec_repl2_fn loongarch_vec_repl2_128 = NULL,
10222 loongarch_vec_repl2_256 = NULL;
10223 loongarch_vec_set_fn loongarch_vec_set128 = NULL, loongarch_vec_set256 = NULL;
10224 loongarch_vec_mirror_fn loongarch_vec_mirror = NULL;
10225 loongarch_vec_merge_fn loongarch_lasx_vecinit_merge = NULL;
10226 machine_mode half_mode = VOIDmode;
10227
10228 /* Check whether elements of each part are the same. */
10229 for (i = 0; i < nelt / 2; ++i)
10230 {
10231 val_hi[i] = val_hi[i + nelt / 2] = val[i + nelt / 2]
10232 = XVECEXP (vals, 0, i + nelt / 2);
10233 val_lo[i] = val[i] = XVECEXP (vals, 0, i);
10234 if (!loongarch_constant_elt_p (val_hi[i]))
10235 hi_nvar++;
10236 if (!loongarch_constant_elt_p (val_lo[i]))
10237 lo_nvar++;
10238 if (i > 0 && !rtx_equal_p (val_hi[i], val_hi[0]))
10239 hi_same = false;
10240 if (i > 0 && !rtx_equal_p (val_lo[i], val_lo[0]))
10241 lo_same = false;
10242 if (!rtx_equal_p (val_hi[i], val_lo[i]))
10243 half_same = false;
10244 }
10245
10246 /* If all elements are the same, set all_same true. */
10247 if (hi_same && lo_same && half_same)
10248 all_same = true;
10249
10250 nvar = hi_nvar + lo_nvar;
10251
10252 switch (vmode)
10253 {
10254 case E_V32QImode:
10255 half_mode = E_V16QImode;
10256 loongarch_vec_set256 = gen_vec_setv32qi_internal;
10257 loongarch_vec_repl1_256 = gen_lasx_xvreplgr2vr_b;
10258 loongarch_lasx_vecinit_merge
10259 = half_same ? gen_lasx_xvpermi_q_v32qi : gen_lasx_vecinit_merge_v32qi;
10260 /* FALLTHRU. */
10261 case E_V16QImode:
10262 loongarch_vec_set128 = gen_vec_setv16qi;
10263 loongarch_vec_repl1_128 = gen_lsx_vreplgr2vr_b;
10264 loongarch_vec_mirror = gen_lsx_vreplvei_mirror_b;
10265 break;
10266
10267 case E_V16HImode:
10268 half_mode = E_V8HImode;
10269 loongarch_vec_set256 = gen_vec_setv16hi_internal;
10270 loongarch_vec_repl1_256 = gen_lasx_xvreplgr2vr_h;
10271 loongarch_lasx_vecinit_merge
10272 = half_same ? gen_lasx_xvpermi_q_v16hi : gen_lasx_vecinit_merge_v16hi;
10273 /* FALLTHRU. */
10274 case E_V8HImode:
10275 loongarch_vec_set128 = gen_vec_setv8hi;
10276 loongarch_vec_repl1_128 = gen_lsx_vreplgr2vr_h;
10277 loongarch_vec_mirror = gen_lsx_vreplvei_mirror_h;
10278 break;
10279
10280 case E_V8SImode:
10281 half_mode = V4SImode;
10282 loongarch_vec_set256 = gen_vec_setv8si;
10283 loongarch_vec_repl1_256 = gen_lasx_xvreplgr2vr_w;
10284 loongarch_lasx_vecinit_merge
10285 = half_same ? gen_lasx_xvpermi_q_v8si : gen_lasx_vecinit_merge_v8si;
10286 /* FALLTHRU. */
10287 case E_V4SImode:
10288 loongarch_vec_set128 = gen_vec_setv4si;
10289 loongarch_vec_repl1_128 = gen_lsx_vreplgr2vr_w;
10290 loongarch_vec_mirror = gen_lsx_vreplvei_mirror_w;
10291 break;
10292
10293 case E_V4DImode:
10294 half_mode = E_V2DImode;
10295 loongarch_vec_set256 = gen_vec_setv4di;
10296 loongarch_vec_repl1_256 = gen_lasx_xvreplgr2vr_d;
10297 loongarch_lasx_vecinit_merge
10298 = half_same ? gen_lasx_xvpermi_q_v4di : gen_lasx_vecinit_merge_v4di;
10299 /* FALLTHRU. */
10300 case E_V2DImode:
10301 loongarch_vec_set128 = gen_vec_setv2di;
10302 loongarch_vec_repl1_128 = gen_lsx_vreplgr2vr_d;
10303 loongarch_vec_mirror = gen_lsx_vreplvei_mirror_d;
10304 break;
10305
10306 case E_V8SFmode:
10307 half_mode = E_V4SFmode;
10308 loongarch_vec_set256 = gen_vec_setv8sf;
10309 loongarch_vec_repl1_128 = gen_lsx_vreplvei_w_f_scalar;
10310 loongarch_vec_repl2_256 = gen_lasx_xvilvl_w_f_internal;
10311 loongarch_lasx_vecinit_merge
10312 = half_same ? gen_lasx_xvpermi_q_v8sf : gen_lasx_vecinit_merge_v8sf;
10313 /* FALLTHRU. */
10314 case E_V4SFmode:
10315 loongarch_vec_set128 = gen_vec_setv4sf;
10316 loongarch_vec_repl2_128 = gen_lsx_vilvl_w_f_internal;
10317 loongarch_vec_mirror = gen_lsx_vreplvei_mirror_w_f;
10318 break;
10319
10320 case E_V4DFmode:
10321 half_mode = E_V2DFmode;
10322 loongarch_vec_set256 = gen_vec_setv4df;
10323 loongarch_vec_repl1_128 = gen_lsx_vreplvei_d_f_scalar;
10324 loongarch_vec_repl2_256 = gen_lasx_xvilvl_d_f_internal;
10325 loongarch_lasx_vecinit_merge
10326 = half_same ? gen_lasx_xvpermi_q_v4df : gen_lasx_vecinit_merge_v4df;
10327 /* FALLTHRU. */
10328 case E_V2DFmode:
10329 loongarch_vec_set128 = gen_vec_setv2df;
10330 loongarch_vec_repl2_128 = gen_lsx_vilvl_d_f_internal;
10331 loongarch_vec_mirror = gen_lsx_vreplvei_mirror_d_f;
10332 break;
10333
10334 default:
10335 gcc_unreachable ();
10336 }
10337
10338 if (ISA_HAS_LASX && GET_MODE_SIZE (vmode) == 32)
10339 {
10340 /* If all elements are the same, just do a broadcost. */
10341 if (all_same)
10342 loongarch_expand_vector_init_same (target, vals, nvar);
10343 else
10344 {
10345 gcc_assert (nelt >= 4);
10346
10347 rtx target_hi, target_lo;
10348 /* Write elements of high half-part in target directly. */
10349 target_hi = target;
10350 target_lo = gen_reg_rtx (half_mode);
10351
10352 /* If all elements of high half-part are the same,
10353 just do a broadcost. Also applicable to low half-part. */
10354 if (hi_same)
10355 {
10356 rtx vtmp = gen_rtx_PARALLEL (vmode, gen_rtvec_v (nelt, val_hi));
10357 loongarch_expand_vector_init_same (target_hi, vtmp, hi_nvar);
10358 }
10359 if (lo_same)
10360 {
10361 rtx vtmp
10362 = gen_rtx_PARALLEL (half_mode, gen_rtvec_v (nelt / 2, val_lo));
10363 loongarch_expand_vector_init_same (target_lo, vtmp, lo_nvar);
10364 }
10365
10366 for (i = 0; i < nelt / 2; ++i)
10367 {
10368 if (!hi_same)
10369 {
10370 if (vmode == E_V8SFmode || vmode == E_V4DFmode)
10371 {
10372 /* Using xvilvl to load lowest 2 elements simultaneously
10373 to reduce the number of instructions. */
10374 if (i == 1)
10375 {
10376 op0 = force_reg (imode, val_hi[0]);
10377 op1 = force_reg (imode, val_hi[1]);
10378 emit_insn (
10379 loongarch_vec_repl2_256 (target_hi, op0, op1));
10380 }
10381 else if (i > 1)
10382 {
10383 op0 = force_reg (imode, val_hi[i]);
10384 emit_insn (
10385 loongarch_vec_set256 (target_hi, op0, GEN_INT (i)));
10386 }
10387 }
10388 else
10389 {
10390 op0 = force_reg (imode, val_hi[i]);
10391 /* Assign the lowest element of val_hi to all elements
10392 of target_hi. */
10393 if (i == 0)
10394 {
10395 emit_insn (loongarch_vec_repl1_256 (target_hi, op0));
10396 }
10397 else if (!rtx_equal_p (val_hi[i], val_hi[0]))
10398 {
10399 emit_insn (
10400 loongarch_vec_set256 (target_hi, op0, GEN_INT (i)));
10401 }
10402 }
10403 }
10404 if (!lo_same && !half_same)
10405 {
10406 op0 = force_reg (imode, val_lo[i]);
10407 /* Assign the lowest element of val_lo to all elements
10408 of target_lo. */
10409 if (i == 0)
10410 {
10411 emit_insn (loongarch_vec_repl1_128 (target_lo, op0));
10412 }
10413 else if (!rtx_equal_p (val_lo[i], val_lo[0]))
10414 {
10415 emit_insn (
10416 loongarch_vec_set128 (target_lo, op0, GEN_INT (i)));
10417 }
10418 }
10419 }
10420 if (half_same)
10421 {
10422 emit_insn (loongarch_lasx_vecinit_merge (target, target_hi,
10423 target_hi, const0_rtx));
10424 return;
10425 }
10426 emit_insn (loongarch_lasx_vecinit_merge (target, target_hi, target_lo,
10427 GEN_INT (0x20)));
10428 }
10429 return;
10430 }
10431
10432 if (ISA_HAS_LSX)
10433 {
10434 if (all_same)
10435 loongarch_expand_vector_init_same (target, vals, nvar);
10436 else
10437 {
10438 for (i = 0; i < nelt; ++i)
10439 {
10440 if (vmode == E_V4SFmode || vmode == E_V2DFmode)
10441 {
10442 /* Using vilvl to load lowest 2 elements simultaneously to
10443 reduce the number of instructions. */
10444 if (i == 1)
10445 {
10446 op0 = force_reg (imode, val[0]);
10447 op1 = force_reg (imode, val[1]);
10448 emit_insn (loongarch_vec_repl2_128 (target, op0, op1));
10449 }
10450 else if (i > 1)
10451 {
10452 op0 = force_reg (imode, val[i]);
10453 emit_insn (
10454 loongarch_vec_set128 (target, op0, GEN_INT (i)));
10455 }
10456 }
10457 else
10458 {
10459 if (half_same && i == nelt / 2)
10460 {
10461 emit_insn (
10462 loongarch_vec_mirror (target, target, const0_rtx));
10463 return;
10464 }
10465 op0 = force_reg (imode, val[i]);
10466 /* Assign the lowest element of val to all elements of
10467 target. */
10468 if (i == 0)
10469 {
10470 emit_insn (loongarch_vec_repl1_128 (target, op0));
10471 }
10472 else if (!rtx_equal_p (val[i], val[0]))
10473 {
10474 emit_insn (
10475 loongarch_vec_set128 (target, op0, GEN_INT (i)));
10476 }
10477 }
10478 }
10479 }
10480 return;
10481 }
10482
10483 /* Load constants from the pool, or whatever's handy. */
10484 if (nvar == 0)
10485 {
10486 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
10487 return;
10488 }
10489
10490 /* For two-part initialization, always use CONCAT. */
10491 if (nelt == 2)
10492 {
10493 rtx op0 = force_reg (imode, val[0]);
10494 rtx op1 = force_reg (imode, val[1]);
10495 x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
10496 emit_insn (gen_rtx_SET (target, x));
10497 return;
10498 }
10499
10500 /* No LoongArch CPU supports vectors with more elements as at now. */
10501 gcc_unreachable ();
10502 }
10503
10504 /* Implement HARD_REGNO_CALLER_SAVE_MODE. */
10505
10506 machine_mode
10507 loongarch_hard_regno_caller_save_mode (unsigned int regno, unsigned int nregs,
10508 machine_mode mode)
10509 {
10510 /* For performance, avoid saving/restoring upper parts of a register
10511 by returning MODE as save mode when the mode is known. */
10512 if (mode == VOIDmode)
10513 return choose_hard_reg_mode (regno, nregs, NULL);
10514 else
10515 return mode;
10516 }
10517
10518 /* Generate RTL for comparing CMP_OP0 and CMP_OP1 using condition COND and
10519 store the result -1 or 0 in DEST. */
10520
10521 static void
10522 loongarch_expand_lsx_cmp (rtx dest, enum rtx_code cond, rtx op0, rtx op1)
10523 {
10524 machine_mode cmp_mode = GET_MODE (op0);
10525 bool negate = false;
10526
10527 switch (cmp_mode)
10528 {
10529 case E_V16QImode:
10530 case E_V32QImode:
10531 case E_V8HImode:
10532 case E_V16HImode:
10533 case E_V4SImode:
10534 case E_V8SImode:
10535 case E_V2DImode:
10536 case E_V4DImode:
10537 switch (cond)
10538 {
10539 case NE:
10540 if (!loongarch_const_vector_same_int_p (op1, cmp_mode, -16, 15))
10541 op1 = force_reg (cmp_mode, op1);
10542 cond = reverse_condition (cond);
10543 negate = true;
10544 break;
10545 case EQ:
10546 case LT:
10547 case LE:
10548 if (!loongarch_const_vector_same_int_p (op1, cmp_mode, -16, 15))
10549 op1 = force_reg (cmp_mode, op1);
10550 break;
10551 case LTU:
10552 case LEU:
10553 if (!loongarch_const_vector_same_int_p (op1, cmp_mode, 0, 31))
10554 op1 = force_reg (cmp_mode, op1);
10555 break;
10556 case GE:
10557 case GT:
10558 case GEU:
10559 case GTU:
10560 /* Only supports reg-reg comparison. */
10561 if (!register_operand (op1, cmp_mode))
10562 op1 = force_reg (cmp_mode, op1);
10563 std::swap (op0, op1);
10564 cond = swap_condition (cond);
10565 break;
10566 default:
10567 gcc_unreachable ();
10568 }
10569 loongarch_emit_binary (cond, dest, op0, op1);
10570 if (negate)
10571 emit_move_insn (dest, gen_rtx_NOT (GET_MODE (dest), dest));
10572 break;
10573
10574 case E_V4SFmode:
10575 case E_V2DFmode:
10576 case E_V8SFmode:
10577 case E_V4DFmode:
10578 if (!register_operand (op1, cmp_mode))
10579 op1 = force_reg (cmp_mode, op1);
10580 loongarch_emit_binary (cond, dest, op0, op1);
10581 break;
10582
10583 default:
10584 gcc_unreachable ();
10585 break;
10586 }
10587 }
10588
10589 /* Expand VEC_COND_EXPR, where:
10590 MODE is mode of the result
10591 VIMODE equivalent integer mode
10592 OPERANDS operands of VEC_COND_EXPR. */
10593
10594 void
10595 loongarch_expand_vec_cond_expr (machine_mode mode, machine_mode vimode,
10596 rtx *operands)
10597 {
10598 rtx cond = operands[3];
10599 rtx cmp_op0 = operands[4];
10600 rtx cmp_op1 = operands[5];
10601 rtx cmp_res = gen_reg_rtx (vimode);
10602
10603 loongarch_expand_lsx_cmp (cmp_res, GET_CODE (cond), cmp_op0, cmp_op1);
10604
10605 /* We handle the following cases:
10606 1) r = a CMP b ? -1 : 0
10607 2) r = a CMP b ? -1 : v
10608 3) r = a CMP b ? v : 0
10609 4) r = a CMP b ? v1 : v2 */
10610
10611 /* Case (1) above. We only move the results. */
10612 if (operands[1] == CONSTM1_RTX (vimode)
10613 && operands[2] == CONST0_RTX (vimode))
10614 emit_move_insn (operands[0], cmp_res);
10615 else
10616 {
10617 rtx src1 = gen_reg_rtx (vimode);
10618 rtx src2 = gen_reg_rtx (vimode);
10619 rtx mask = gen_reg_rtx (vimode);
10620 rtx bsel;
10621
10622 /* Move the vector result to use it as a mask. */
10623 emit_move_insn (mask, cmp_res);
10624
10625 if (register_operand (operands[1], mode))
10626 {
10627 rtx xop1 = operands[1];
10628 if (mode != vimode)
10629 {
10630 xop1 = gen_reg_rtx (vimode);
10631 emit_move_insn (xop1,
10632 simplify_gen_subreg (vimode, operands[1],
10633 mode, 0));
10634 }
10635 emit_move_insn (src1, xop1);
10636 }
10637 else
10638 {
10639 gcc_assert (operands[1] == CONSTM1_RTX (vimode));
10640 /* Case (2) if the below doesn't move the mask to src2. */
10641 emit_move_insn (src1, mask);
10642 }
10643
10644 if (register_operand (operands[2], mode))
10645 {
10646 rtx xop2 = operands[2];
10647 if (mode != vimode)
10648 {
10649 xop2 = gen_reg_rtx (vimode);
10650 emit_move_insn (xop2,
10651 simplify_gen_subreg (vimode, operands[2],
10652 mode, 0));
10653 }
10654 emit_move_insn (src2, xop2);
10655 }
10656 else
10657 {
10658 gcc_assert (operands[2] == CONST0_RTX (mode));
10659 /* Case (3) if the above didn't move the mask to src1. */
10660 emit_move_insn (src2, mask);
10661 }
10662
10663 /* We deal with case (4) if the mask wasn't moved to either src1 or src2.
10664 In any case, we eventually do vector mask-based copy. */
10665 bsel = gen_rtx_IOR (vimode,
10666 gen_rtx_AND (vimode,
10667 gen_rtx_NOT (vimode, mask), src2),
10668 gen_rtx_AND (vimode, mask, src1));
10669 /* The result is placed back to a register with the mask. */
10670 emit_insn (gen_rtx_SET (mask, bsel));
10671 emit_move_insn (operands[0],
10672 simplify_gen_subreg (mode, mask, vimode, 0));
10673 }
10674 }
10675
10676 void
10677 loongarch_expand_vec_cond_mask_expr (machine_mode mode, machine_mode vimode,
10678 rtx *operands)
10679 {
10680 rtx cmp_res = operands[3];
10681
10682 /* We handle the following cases:
10683 1) r = a CMP b ? -1 : 0
10684 2) r = a CMP b ? -1 : v
10685 3) r = a CMP b ? v : 0
10686 4) r = a CMP b ? v1 : v2 */
10687
10688 /* Case (1) above. We only move the results. */
10689 if (operands[1] == CONSTM1_RTX (vimode)
10690 && operands[2] == CONST0_RTX (vimode))
10691 emit_move_insn (operands[0], cmp_res);
10692 else
10693 {
10694 rtx src1 = gen_reg_rtx (vimode);
10695 rtx src2 = gen_reg_rtx (vimode);
10696 rtx mask = gen_reg_rtx (vimode);
10697 rtx bsel;
10698
10699 /* Move the vector result to use it as a mask. */
10700 emit_move_insn (mask, cmp_res);
10701
10702 if (register_operand (operands[1], mode))
10703 {
10704 rtx xop1 = operands[1];
10705 if (mode != vimode)
10706 {
10707 xop1 = gen_reg_rtx (vimode);
10708 emit_move_insn (xop1,
10709 simplify_gen_subreg (vimode, operands[1],
10710 mode, 0));
10711 }
10712 emit_move_insn (src1, xop1);
10713 }
10714 else
10715 {
10716 gcc_assert (operands[1] == CONSTM1_RTX (vimode));
10717 /* Case (2) if the below doesn't move the mask to src2. */
10718 emit_move_insn (src1, mask);
10719 }
10720
10721 if (register_operand (operands[2], mode))
10722 {
10723 rtx xop2 = operands[2];
10724 if (mode != vimode)
10725 {
10726 xop2 = gen_reg_rtx (vimode);
10727 emit_move_insn (xop2,
10728 simplify_gen_subreg (vimode, operands[2],
10729 mode, 0));
10730 }
10731 emit_move_insn (src2, xop2);
10732 }
10733 else
10734 {
10735 gcc_assert (operands[2] == CONST0_RTX (mode));
10736 /* Case (3) if the above didn't move the mask to src1. */
10737 emit_move_insn (src2, mask);
10738 }
10739
10740 /* We deal with case (4) if the mask wasn't moved to either src1 or src2.
10741 In any case, we eventually do vector mask-based copy. */
10742 bsel = gen_rtx_IOR (vimode,
10743 gen_rtx_AND (vimode,
10744 gen_rtx_NOT (vimode, mask), src2),
10745 gen_rtx_AND (vimode, mask, src1));
10746 /* The result is placed back to a register with the mask. */
10747 emit_insn (gen_rtx_SET (mask, bsel));
10748 emit_move_insn (operands[0], simplify_gen_subreg (mode, mask,
10749 vimode, 0));
10750 }
10751 }
10752
10753 /* Expand integer vector comparison */
10754 void
10755 loongarch_expand_vec_cmp (rtx operands[])
10756 {
10757
10758 rtx_code code = GET_CODE (operands[1]);
10759 loongarch_expand_lsx_cmp (operands[0], code, operands[2], operands[3]);
10760 }
10761
10762 /* Implement TARGET_PROMOTE_FUNCTION_MODE. */
10763
10764 /* This function is equivalent to default_promote_function_mode_always_promote
10765 except that it returns a promoted mode even if type is NULL_TREE. This is
10766 needed by libcalls which have no type (only a mode) such as fixed conversion
10767 routines that take a signed or unsigned char/short argument and convert it
10768 to a fixed type. */
10769
10770 static machine_mode
10771 loongarch_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
10772 machine_mode mode,
10773 int *punsignedp ATTRIBUTE_UNUSED,
10774 const_tree fntype ATTRIBUTE_UNUSED,
10775 int for_return ATTRIBUTE_UNUSED)
10776 {
10777 int unsignedp;
10778
10779 if (type != NULL_TREE)
10780 return promote_mode (type, mode, punsignedp);
10781
10782 unsignedp = *punsignedp;
10783 PROMOTE_MODE (mode, unsignedp, type);
10784 *punsignedp = unsignedp;
10785 return mode;
10786 }
10787
10788 /* Implement TARGET_STARTING_FRAME_OFFSET. See loongarch_compute_frame_info
10789 for details about the frame layout. */
10790
10791 static HOST_WIDE_INT
10792 loongarch_starting_frame_offset (void)
10793 {
10794 if (FRAME_GROWS_DOWNWARD)
10795 return 0;
10796 return crtl->outgoing_args_size;
10797 }
10798
10799 /* A subroutine of loongarch_build_signbit_mask. If VECT is true,
10800 then replicate the value for all elements of the vector
10801 register. */
10802
10803 rtx
10804 loongarch_build_const_vector (machine_mode mode, bool vect, rtx value)
10805 {
10806 int i, n_elt;
10807 rtvec v;
10808 machine_mode scalar_mode;
10809
10810 switch (mode)
10811 {
10812 case E_V32QImode:
10813 case E_V16QImode:
10814 case E_V32HImode:
10815 case E_V16HImode:
10816 case E_V8HImode:
10817 case E_V8SImode:
10818 case E_V4SImode:
10819 case E_V8DImode:
10820 case E_V4DImode:
10821 case E_V2DImode:
10822 gcc_assert (vect);
10823 /* FALLTHRU */
10824 case E_V8SFmode:
10825 case E_V4SFmode:
10826 case E_V8DFmode:
10827 case E_V4DFmode:
10828 case E_V2DFmode:
10829 n_elt = GET_MODE_NUNITS (mode);
10830 v = rtvec_alloc (n_elt);
10831 scalar_mode = GET_MODE_INNER (mode);
10832
10833 RTVEC_ELT (v, 0) = value;
10834
10835 for (i = 1; i < n_elt; ++i)
10836 RTVEC_ELT (v, i) = vect ? value : CONST0_RTX (scalar_mode);
10837
10838 return gen_rtx_CONST_VECTOR (mode, v);
10839
10840 default:
10841 gcc_unreachable ();
10842 }
10843 }
10844
10845 /* Create a mask for the sign bit in MODE
10846 for an register. If VECT is true, then replicate the mask for
10847 all elements of the vector register. If INVERT is true, then create
10848 a mask excluding the sign bit. */
10849
10850 rtx
10851 loongarch_build_signbit_mask (machine_mode mode, bool vect, bool invert)
10852 {
10853 machine_mode vec_mode, imode;
10854 wide_int w;
10855 rtx mask, v;
10856
10857 switch (mode)
10858 {
10859 case E_V16SImode:
10860 case E_V16SFmode:
10861 case E_V8SImode:
10862 case E_V4SImode:
10863 case E_V8SFmode:
10864 case E_V4SFmode:
10865 vec_mode = mode;
10866 imode = SImode;
10867 break;
10868
10869 case E_V8DImode:
10870 case E_V4DImode:
10871 case E_V2DImode:
10872 case E_V8DFmode:
10873 case E_V4DFmode:
10874 case E_V2DFmode:
10875 vec_mode = mode;
10876 imode = DImode;
10877 break;
10878
10879 case E_TImode:
10880 case E_TFmode:
10881 vec_mode = VOIDmode;
10882 imode = TImode;
10883 break;
10884
10885 default:
10886 gcc_unreachable ();
10887 }
10888
10889 machine_mode inner_mode = GET_MODE_INNER (mode);
10890 w = wi::set_bit_in_zero (GET_MODE_BITSIZE (inner_mode) - 1,
10891 GET_MODE_BITSIZE (inner_mode));
10892 if (invert)
10893 w = wi::bit_not (w);
10894
10895 /* Force this value into the low part of a fp vector constant. */
10896 mask = immed_wide_int_const (w, imode);
10897 mask = gen_lowpart (inner_mode, mask);
10898
10899 if (vec_mode == VOIDmode)
10900 return force_reg (inner_mode, mask);
10901
10902 v = loongarch_build_const_vector (vec_mode, vect, mask);
10903 return v;
10904 }
10905
10906 /* Use rsqrte instruction and Newton-Rhapson to compute the approximation of
10907 a single precision floating point [reciprocal] square root. */
10908
10909 void loongarch_emit_swrsqrtsf (rtx res, rtx a, machine_mode mode, bool recip)
10910 {
10911 rtx x0, e0, e1, e2, mhalf, monehalf;
10912 REAL_VALUE_TYPE r;
10913 int unspec;
10914
10915 x0 = gen_reg_rtx (mode);
10916 e0 = gen_reg_rtx (mode);
10917 e1 = gen_reg_rtx (mode);
10918 e2 = gen_reg_rtx (mode);
10919
10920 real_arithmetic (&r, ABS_EXPR, &dconsthalf, NULL);
10921 mhalf = const_double_from_real_value (r, SFmode);
10922
10923 real_arithmetic (&r, PLUS_EXPR, &dconsthalf, &dconst1);
10924 monehalf = const_double_from_real_value (r, SFmode);
10925 unspec = UNSPEC_RSQRTE;
10926
10927 if (VECTOR_MODE_P (mode))
10928 {
10929 mhalf = loongarch_build_const_vector (mode, true, mhalf);
10930 monehalf = loongarch_build_const_vector (mode, true, monehalf);
10931 unspec = GET_MODE_SIZE (mode) == 32 ? UNSPEC_LASX_XVFRSQRTE
10932 : UNSPEC_LSX_VFRSQRTE;
10933 }
10934
10935 /* rsqrt(a) = rsqrte(a) * (1.5 - 0.5 * a * rsqrte(a) * rsqrte(a))
10936 sqrt(a) = a * rsqrte(a) * (1.5 - 0.5 * a * rsqrte(a) * rsqrte(a)) */
10937
10938 a = force_reg (mode, a);
10939
10940 /* x0 = rsqrt(a) estimate. */
10941 emit_insn (gen_rtx_SET (x0, gen_rtx_UNSPEC (mode, gen_rtvec (1, a),
10942 unspec)));
10943
10944 /* If (a == 0.0) Filter out infinity to prevent NaN for sqrt(0.0). */
10945 if (!recip)
10946 {
10947 rtx zero = force_reg (mode, CONST0_RTX (mode));
10948
10949 if (VECTOR_MODE_P (mode))
10950 {
10951 machine_mode imode = related_int_vector_mode (mode).require ();
10952 rtx mask = force_reg (imode, gen_rtx_NE (imode, a, zero));
10953 emit_move_insn (gen_lowpart (imode, x0),
10954 gen_rtx_AND (imode,
10955 gen_lowpart (imode, x0),
10956 mask));
10957 }
10958 else
10959 {
10960 rtx target = emit_conditional_move (x0, { GT, a, zero, mode },
10961 x0, zero, mode, 0);
10962 if (target != x0)
10963 emit_move_insn (x0, target);
10964 }
10965 }
10966
10967 /* e0 = x0 * a */
10968 emit_insn (gen_rtx_SET (e0, gen_rtx_MULT (mode, x0, a)));
10969 /* e1 = e0 * x0 */
10970 emit_insn (gen_rtx_SET (e1, gen_rtx_MULT (mode, e0, x0)));
10971
10972 /* e2 = 1.5 - e1 * 0.5 */
10973 mhalf = force_reg (mode, mhalf);
10974 monehalf = force_reg (mode, monehalf);
10975 emit_insn (gen_rtx_SET (e2, gen_rtx_FMA (mode,
10976 gen_rtx_NEG (mode, e1),
10977 mhalf, monehalf)));
10978
10979 if (recip)
10980 /* res = e2 * x0 */
10981 emit_insn (gen_rtx_SET (res, gen_rtx_MULT (mode, x0, e2)));
10982 else
10983 /* res = e2 * e0 */
10984 emit_insn (gen_rtx_SET (res, gen_rtx_MULT (mode, e2, e0)));
10985 }
10986
10987 /* Use recipe instruction and Newton-Rhapson to compute the approximation of
10988 a single precision floating point divide. */
10989
10990 void loongarch_emit_swdivsf (rtx res, rtx a, rtx b, machine_mode mode)
10991 {
10992 rtx x0, e0, mtwo;
10993 REAL_VALUE_TYPE r;
10994 x0 = gen_reg_rtx (mode);
10995 e0 = gen_reg_rtx (mode);
10996 int unspec = UNSPEC_RECIPE;
10997
10998 real_arithmetic (&r, ABS_EXPR, &dconst2, NULL);
10999 mtwo = const_double_from_real_value (r, SFmode);
11000
11001 if (VECTOR_MODE_P (mode))
11002 {
11003 mtwo = loongarch_build_const_vector (mode, true, mtwo);
11004 unspec = GET_MODE_SIZE (mode) == 32 ? UNSPEC_LASX_XVFRECIPE
11005 : UNSPEC_LSX_VFRECIPE;
11006 }
11007
11008 mtwo = force_reg (mode, mtwo);
11009
11010 /* a / b = a * recipe(b) * (2.0 - b * recipe(b)) */
11011
11012 /* x0 = 1./b estimate. */
11013 emit_insn (gen_rtx_SET (x0, gen_rtx_UNSPEC (mode, gen_rtvec (1, b),
11014 unspec)));
11015 /* e0 = 2.0 - b * x0. */
11016 emit_insn (gen_rtx_SET (e0, gen_rtx_FMA (mode,
11017 gen_rtx_NEG (mode, b), x0, mtwo)));
11018
11019 if (a != CONST1_RTX (mode))
11020 {
11021 rtx e1 = gen_reg_rtx (mode);
11022 /* e1 = a * x0. */
11023 emit_insn (gen_rtx_SET (e1, gen_rtx_MULT (mode, a, x0)));
11024 /* res = e0 * e1. */
11025 emit_insn (gen_rtx_SET (res, gen_rtx_MULT (mode, e0, e1)));
11026 }
11027 else
11028 {
11029 /* res = e0 * x0. */
11030 emit_insn (gen_rtx_SET (res, gen_rtx_MULT (mode, e0, x0)));
11031 }
11032 }
11033
11034 static bool
11035 loongarch_builtin_support_vector_misalignment (machine_mode mode,
11036 const_tree type,
11037 int misalignment,
11038 bool is_packed)
11039 {
11040 if ((ISA_HAS_LSX || ISA_HAS_LASX) && STRICT_ALIGNMENT)
11041 {
11042 if (optab_handler (movmisalign_optab, mode) == CODE_FOR_nothing)
11043 return false;
11044 if (misalignment == -1)
11045 return false;
11046 }
11047 return default_builtin_support_vector_misalignment (mode, type, misalignment,
11048 is_packed);
11049 }
11050
11051 /* Return a PARALLEL containing NELTS elements, with element I equal
11052 to BASE + I * STEP. */
11053 rtx
11054 loongarch_gen_stepped_int_parallel (unsigned int nelts, int base,
11055 int step)
11056 {
11057 rtvec vec = rtvec_alloc (nelts);
11058 for (unsigned int i = 0; i < nelts; i++)
11059 RTVEC_ELT (vec, i) = GEN_INT (base + i * step);
11060 return gen_rtx_PARALLEL (VOIDmode, vec);
11061 }
11062
11063 /* Implement TARGET_C_MODE_FOR_FLOATING_TYPE. Return TFmode or DFmode
11064 for TI_LONG_DOUBLE_TYPE which is for long double type, go with the
11065 default one for the others. */
11066
11067 static machine_mode
11068 loongarch_c_mode_for_floating_type (enum tree_index ti)
11069 {
11070 if (ti == TI_LONG_DOUBLE_TYPE)
11071 return TARGET_64BIT ? TFmode : DFmode;
11072 return default_mode_for_floating_type (ti);
11073 }
11074
11075 static bool
11076 use_rsqrt_p (void)
11077 {
11078 return (flag_finite_math_only
11079 && !flag_trapping_math
11080 && flag_unsafe_math_optimizations);
11081 }
11082
11083 /* Implement the TARGET_OPTAB_SUPPORTED_P hook. */
11084
11085 static bool
11086 loongarch_optab_supported_p (int op, machine_mode, machine_mode,
11087 optimization_type opt_type)
11088 {
11089 switch (op)
11090 {
11091 case rsqrt_optab:
11092 return opt_type == OPTIMIZE_FOR_SPEED && use_rsqrt_p ();
11093
11094 default:
11095 return true;
11096 }
11097 }
11098
11099 /* If -fverbose-asm, dump some info for debugging. */
11100 static void
11101 loongarch_asm_code_end (void)
11102 {
11103 #define DUMP_FEATURE(PRED) \
11104 fprintf (asm_out_file, "%s %s: %s\n", ASM_COMMENT_START, #PRED, \
11105 (PRED) ? "enabled" : "disabled")
11106
11107 if (flag_verbose_asm)
11108 {
11109 fprintf (asm_out_file, "\n%s CPU: %s\n", ASM_COMMENT_START,
11110 loongarch_arch_strings[la_target.cpu_arch]);
11111 fprintf (asm_out_file, "%s Tune: %s\n", ASM_COMMENT_START,
11112 loongarch_tune_strings[la_target.cpu_tune]);
11113 fprintf (asm_out_file, "%s Base ISA: %s\n", ASM_COMMENT_START,
11114 loongarch_isa_base_strings [la_target.isa.base]);
11115 DUMP_FEATURE (ISA_HAS_FRECIPE);
11116 DUMP_FEATURE (ISA_HAS_DIV32);
11117 DUMP_FEATURE (ISA_HAS_LAM_BH);
11118 DUMP_FEATURE (ISA_HAS_LAMCAS);
11119 DUMP_FEATURE (ISA_HAS_LD_SEQ_SA);
11120 }
11121
11122 fputs ("\n\n", asm_out_file);
11123 #undef DUMP_FEATURE
11124 }
11125
11126 /* Target hook for c_mode_for_suffix. */
11127 static machine_mode
11128 loongarch_c_mode_for_suffix (char suffix)
11129 {
11130 if (suffix == 'q')
11131 return TFmode;
11132
11133 return VOIDmode;
11134 }
11135
11136 /* Implement TARGET_COMPUTE_PRESSURE_CLASSES. */
11137
11138 static int
11139 loongarch_compute_pressure_classes (reg_class *classes)
11140 {
11141 int i = 0;
11142 classes[i++] = GENERAL_REGS;
11143 classes[i++] = FP_REGS;
11144 classes[i++] = FCC_REGS;
11145 return i;
11146 }
11147
11148 /* Implement TARGET_CAN_INLINE_P. Determine whether inlining the function
11149 CALLER into the function CALLEE is safe. Inlining should be rejected if
11150 there is no always_inline attribute and the target options differ except
11151 for differences in ISA extensions or performance tuning options like the
11152 code model, TLS dialect, etc. */
11153
11154 static bool
11155 loongarch_can_inline_p (tree caller, tree callee)
11156 {
11157 tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
11158 tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
11159
11160 if (!callee_tree)
11161 callee_tree = target_option_default_node;
11162
11163 if (!caller_tree)
11164 caller_tree = target_option_default_node;
11165
11166 /* If both caller and callee have attributes, assume that if the
11167 pointer is different, the two functions have different target
11168 options since build_target_option_node uses a hash table for the
11169 options. */
11170 if (callee_tree == caller_tree)
11171 return true;
11172
11173 struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
11174 struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
11175
11176 /* Callee and caller should have the same target options. */
11177 int callee_target_flags = callee_opts->x_target_flags;
11178 int caller_target_flags = caller_opts->x_target_flags;
11179
11180 if (callee_target_flags != caller_target_flags)
11181 return false;
11182
11183 /* If callee enables the isa extension that the caller does not enable,
11184 inlining is disabled. */
11185 if (~caller_opts->x_la_isa_evolution
11186 & callee_opts->x_la_isa_evolution)
11187 return false;
11188
11189 /* If simd extensions are enabled for the callee but not for the caller,
11190 inlining is disabled. */
11191 if ((caller_opts->x_la_opt_simd == ISA_EXT_NONE
11192 && callee_opts->x_la_opt_simd != ISA_EXT_NONE)
11193 || (caller_opts->x_la_opt_simd == ISA_EXT_SIMD_LSX
11194 && callee_opts->x_la_opt_simd == ISA_EXT_SIMD_LASX))
11195 return false;
11196
11197 bool always_inline
11198 = lookup_attribute ("always_inline", DECL_ATTRIBUTES (callee));
11199
11200 /* If the architectural features match up and the callee is always_inline
11201 then the other attributes don't matter. */
11202 if (always_inline)
11203 return true;
11204
11205 if (caller_opts->x_la_opt_cmodel != callee_opts->x_la_opt_cmodel)
11206 return false;
11207
11208 return true;
11209 }
11210
11211 /* Output assembly to materialize the address of the stack canary value
11212 into reg. The third argument, tmp, should be and should only be
11213 non-NULL if the extreme code model is effective for the canary. If
11214 the fourth arugment, load, is true, the canary value is loaded into
11215 the register.
11216
11217 The assembly cannot be splitted due to security reason. */
11218 void
11219 loongarch_output_asm_load_canary (rtx reg, rtx canary, rtx tmp)
11220 {
11221 gcc_checking_assert (ssp_operand (canary, VOIDmode));
11222 gcc_checking_assert ((!tmp) == ssp_normal_operand (canary, VOIDmode));
11223 gcc_checking_assert (register_operand (reg, Pmode));
11224
11225 rtx op[] = {reg, canary, tmp};
11226 bool got = (loongarch_classify_symbol (canary) == SYMBOL_GOT_DISP);
11227 bool need_ld = false;
11228
11229 if (la_opt_explicit_relocs != EXPLICIT_RELOCS_ALWAYS)
11230 {
11231 if (got)
11232 output_asm_insn (tmp ? "la.global\t%0,%2,%1" : "la.global\t%0,%1",
11233 op);
11234 else
11235 output_asm_insn (tmp ? "la.local\t%0,%2,%1" : "la.local\t%0,%1",
11236 op);
11237
11238 need_ld = true;
11239 }
11240 else
11241 {
11242 output_asm_insn ("pcalau12i\t%0,%r1", op);
11243 if (!tmp)
11244 output_asm_insn ("ld.%v0\t%0,%0,%L1", op);
11245 else
11246 {
11247 output_asm_insn ("addi.d\t%2,$r0,%L1", op);
11248 output_asm_insn ("lu32i.d\t%2,%R1", op);
11249 output_asm_insn ("lu52i.d\t%2,%2,%H1", op);
11250 output_asm_insn ("ldx.d\t%0,%0,%2", op);
11251 }
11252
11253 need_ld = got;
11254 }
11255
11256 if (need_ld)
11257 output_asm_insn ("ld.%v0\t%0,%0,0", op);
11258 }
11259
11260 /* Initialize the GCC target structure. */
11261 #undef TARGET_ASM_ALIGNED_HI_OP
11262 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
11263 #undef TARGET_ASM_ALIGNED_SI_OP
11264 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
11265 #undef TARGET_ASM_ALIGNED_DI_OP
11266 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
11267
11268 #undef TARGET_OPTION_OVERRIDE
11269 #define TARGET_OPTION_OVERRIDE loongarch_option_override
11270 #undef TARGET_OPTION_SAVE
11271 #define TARGET_OPTION_SAVE loongarch_option_save
11272 #undef TARGET_OPTION_RESTORE
11273 #define TARGET_OPTION_RESTORE loongarch_option_restore
11274
11275 #undef TARGET_SET_CURRENT_FUNCTION
11276 #define TARGET_SET_CURRENT_FUNCTION loongarch_set_current_function
11277
11278 #undef TARGET_LEGITIMIZE_ADDRESS
11279 #define TARGET_LEGITIMIZE_ADDRESS loongarch_legitimize_address
11280
11281 #undef TARGET_ASM_SELECT_RTX_SECTION
11282 #define TARGET_ASM_SELECT_RTX_SECTION loongarch_select_rtx_section
11283 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
11284 #define TARGET_ASM_FUNCTION_RODATA_SECTION loongarch_function_rodata_section
11285
11286 #undef TARGET_ASM_CODE_END
11287 #define TARGET_ASM_CODE_END loongarch_asm_code_end
11288
11289 #undef TARGET_SCHED_INIT
11290 #define TARGET_SCHED_INIT loongarch_sched_init
11291 #undef TARGET_SCHED_REORDER
11292 #define TARGET_SCHED_REORDER loongarch_sched_reorder
11293 #undef TARGET_SCHED_REORDER2
11294 #define TARGET_SCHED_REORDER2 loongarch_sched_reorder2
11295 #undef TARGET_SCHED_VARIABLE_ISSUE
11296 #define TARGET_SCHED_VARIABLE_ISSUE loongarch_variable_issue
11297 #undef TARGET_SCHED_ADJUST_COST
11298 #define TARGET_SCHED_ADJUST_COST loongarch_adjust_cost
11299 #undef TARGET_SCHED_ISSUE_RATE
11300 #define TARGET_SCHED_ISSUE_RATE loongarch_issue_rate
11301 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
11302 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
11303 loongarch_multipass_dfa_lookahead
11304
11305 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
11306 #define TARGET_FUNCTION_OK_FOR_SIBCALL loongarch_function_ok_for_sibcall
11307
11308 #undef TARGET_VALID_POINTER_MODE
11309 #define TARGET_VALID_POINTER_MODE loongarch_valid_pointer_mode
11310 #undef TARGET_REGISTER_MOVE_COST
11311 #define TARGET_REGISTER_MOVE_COST loongarch_register_move_cost
11312 #undef TARGET_MEMORY_MOVE_COST
11313 #define TARGET_MEMORY_MOVE_COST loongarch_memory_move_cost
11314 #undef TARGET_RTX_COSTS
11315 #define TARGET_RTX_COSTS loongarch_rtx_costs
11316 #undef TARGET_ADDRESS_COST
11317 #define TARGET_ADDRESS_COST loongarch_address_cost
11318 #undef TARGET_INSN_COST
11319 #define TARGET_INSN_COST loongarch_insn_cost
11320 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
11321 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
11322 loongarch_builtin_vectorization_cost
11323 #undef TARGET_VECTORIZE_CREATE_COSTS
11324 #define TARGET_VECTORIZE_CREATE_COSTS loongarch_vectorize_create_costs
11325
11326
11327 #undef TARGET_IN_SMALL_DATA_P
11328 #define TARGET_IN_SMALL_DATA_P loongarch_in_small_data_p
11329
11330 #undef TARGET_PREFERRED_RELOAD_CLASS
11331 #define TARGET_PREFERRED_RELOAD_CLASS loongarch_preferred_reload_class
11332
11333 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
11334 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
11335
11336 #undef TARGET_EXPAND_BUILTIN_VA_START
11337 #define TARGET_EXPAND_BUILTIN_VA_START loongarch_va_start
11338
11339 #undef TARGET_PROMOTE_FUNCTION_MODE
11340 #define TARGET_PROMOTE_FUNCTION_MODE loongarch_promote_function_mode
11341 #undef TARGET_RETURN_IN_MEMORY
11342 #define TARGET_RETURN_IN_MEMORY loongarch_return_in_memory
11343
11344 #undef TARGET_FUNCTION_VALUE
11345 #define TARGET_FUNCTION_VALUE loongarch_function_value
11346 #undef TARGET_LIBCALL_VALUE
11347 #define TARGET_LIBCALL_VALUE loongarch_libcall_value
11348
11349 #undef TARGET_ASM_OUTPUT_MI_THUNK
11350 #define TARGET_ASM_OUTPUT_MI_THUNK loongarch_output_mi_thunk
11351 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
11352 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK \
11353 hook_bool_const_tree_hwi_hwi_const_tree_true
11354
11355 #undef TARGET_PRINT_OPERAND
11356 #define TARGET_PRINT_OPERAND loongarch_print_operand
11357 #undef TARGET_PRINT_OPERAND_ADDRESS
11358 #define TARGET_PRINT_OPERAND_ADDRESS loongarch_print_operand_address
11359 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
11360 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P \
11361 loongarch_print_operand_punct_valid_p
11362
11363 #undef TARGET_SETUP_INCOMING_VARARGS
11364 #define TARGET_SETUP_INCOMING_VARARGS loongarch_setup_incoming_varargs
11365 #undef TARGET_STRICT_ARGUMENT_NAMING
11366 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
11367 #undef TARGET_MUST_PASS_IN_STACK
11368 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
11369 #undef TARGET_PASS_BY_REFERENCE
11370 #define TARGET_PASS_BY_REFERENCE loongarch_pass_by_reference
11371 #undef TARGET_ARG_PARTIAL_BYTES
11372 #define TARGET_ARG_PARTIAL_BYTES loongarch_arg_partial_bytes
11373 #undef TARGET_FUNCTION_ARG
11374 #define TARGET_FUNCTION_ARG loongarch_function_arg
11375 #undef TARGET_FUNCTION_ARG_ADVANCE
11376 #define TARGET_FUNCTION_ARG_ADVANCE loongarch_function_arg_advance
11377 #undef TARGET_FUNCTION_ARG_BOUNDARY
11378 #define TARGET_FUNCTION_ARG_BOUNDARY loongarch_function_arg_boundary
11379
11380 #undef TARGET_VECTOR_MODE_SUPPORTED_P
11381 #define TARGET_VECTOR_MODE_SUPPORTED_P loongarch_vector_mode_supported_p
11382
11383 #undef TARGET_SCALAR_MODE_SUPPORTED_P
11384 #define TARGET_SCALAR_MODE_SUPPORTED_P loongarch_scalar_mode_supported_p
11385
11386 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
11387 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE loongarch_preferred_simd_mode
11388
11389 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_MODES
11390 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_MODES \
11391 loongarch_autovectorize_vector_modes
11392
11393 #undef TARGET_OPTAB_SUPPORTED_P
11394 #define TARGET_OPTAB_SUPPORTED_P loongarch_optab_supported_p
11395
11396 #undef TARGET_INIT_BUILTINS
11397 #define TARGET_INIT_BUILTINS loongarch_init_builtins
11398 #undef TARGET_BUILTIN_DECL
11399 #define TARGET_BUILTIN_DECL loongarch_builtin_decl
11400 #undef TARGET_EXPAND_BUILTIN
11401 #define TARGET_EXPAND_BUILTIN loongarch_expand_builtin
11402
11403 /* The generic ELF target does not always have TLS support. */
11404 #ifdef HAVE_AS_TLS
11405 #undef TARGET_HAVE_TLS
11406 #define TARGET_HAVE_TLS HAVE_AS_TLS
11407 #endif
11408
11409 #undef TARGET_CANNOT_FORCE_CONST_MEM
11410 #define TARGET_CANNOT_FORCE_CONST_MEM loongarch_cannot_force_const_mem
11411
11412 #undef TARGET_LEGITIMATE_CONSTANT_P
11413 #define TARGET_LEGITIMATE_CONSTANT_P loongarch_legitimate_constant_p
11414
11415 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
11416 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
11417
11418 #ifdef HAVE_AS_DTPRELWORD
11419 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
11420 #define TARGET_ASM_OUTPUT_DWARF_DTPREL loongarch_output_dwarf_dtprel
11421 #endif
11422
11423 #undef TARGET_LEGITIMATE_ADDRESS_P
11424 #define TARGET_LEGITIMATE_ADDRESS_P loongarch_legitimate_address_p
11425
11426 #undef TARGET_FRAME_POINTER_REQUIRED
11427 #define TARGET_FRAME_POINTER_REQUIRED loongarch_frame_pointer_required
11428
11429 #undef TARGET_CAN_ELIMINATE
11430 #define TARGET_CAN_ELIMINATE loongarch_can_eliminate
11431
11432 #undef TARGET_CONDITIONAL_REGISTER_USAGE
11433 #define TARGET_CONDITIONAL_REGISTER_USAGE loongarch_conditional_register_usage
11434
11435 #undef TARGET_TRAMPOLINE_INIT
11436 #define TARGET_TRAMPOLINE_INIT loongarch_trampoline_init
11437
11438 #undef TARGET_MIN_ANCHOR_OFFSET
11439 #define TARGET_MIN_ANCHOR_OFFSET (-IMM_REACH/2)
11440
11441 #undef TARGET_MAX_ANCHOR_OFFSET
11442 #define TARGET_MAX_ANCHOR_OFFSET (IMM_REACH/2-1)
11443 #undef TARGET_VECTORIZE_VEC_PERM_CONST
11444 #define TARGET_VECTORIZE_VEC_PERM_CONST loongarch_vectorize_vec_perm_const
11445
11446 #undef TARGET_SCHED_REASSOCIATION_WIDTH
11447 #define TARGET_SCHED_REASSOCIATION_WIDTH loongarch_sched_reassociation_width
11448
11449 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
11450 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV loongarch_atomic_assign_expand_fenv
11451
11452 #undef TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS
11453 #define TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS true
11454
11455 #undef TARGET_HARD_REGNO_NREGS
11456 #define TARGET_HARD_REGNO_NREGS loongarch_hard_regno_nregs
11457 #undef TARGET_HARD_REGNO_MODE_OK
11458 #define TARGET_HARD_REGNO_MODE_OK loongarch_hard_regno_mode_ok
11459
11460 #undef TARGET_MODES_TIEABLE_P
11461 #define TARGET_MODES_TIEABLE_P loongarch_modes_tieable_p
11462
11463 #undef TARGET_HARD_REGNO_CALL_PART_CLOBBERED
11464 #define TARGET_HARD_REGNO_CALL_PART_CLOBBERED \
11465 loongarch_hard_regno_call_part_clobbered
11466
11467 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
11468 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 2
11469
11470 #undef TARGET_CAN_CHANGE_MODE_CLASS
11471 #define TARGET_CAN_CHANGE_MODE_CLASS loongarch_can_change_mode_class
11472
11473 #undef TARGET_CONSTANT_ALIGNMENT
11474 #define TARGET_CONSTANT_ALIGNMENT loongarch_constant_alignment
11475
11476 #undef TARGET_STARTING_FRAME_OFFSET
11477 #define TARGET_STARTING_FRAME_OFFSET loongarch_starting_frame_offset
11478
11479 #undef TARGET_SECONDARY_RELOAD
11480 #define TARGET_SECONDARY_RELOAD loongarch_secondary_reload
11481
11482 #undef TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS
11483 #define TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS \
11484 loongarch_ira_change_pseudo_allocno_class
11485
11486 #undef TARGET_HAVE_SPECULATION_SAFE_VALUE
11487 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
11488
11489 #undef TARGET_ATTRIBUTE_TABLE
11490 #define TARGET_ATTRIBUTE_TABLE loongarch_attribute_table
11491
11492 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
11493 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P loongarch_use_anchors_for_symbol_p
11494
11495 #undef TARGET_ASAN_SHADOW_OFFSET
11496 #define TARGET_ASAN_SHADOW_OFFSET loongarch_asan_shadow_offset
11497
11498 #undef TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS
11499 #define TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS \
11500 loongarch_get_separate_components
11501
11502 #undef TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB
11503 #define TARGET_SHRINK_WRAP_COMPONENTS_FOR_BB loongarch_components_for_bb
11504
11505 #undef TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS
11506 #define TARGET_SHRINK_WRAP_DISQUALIFY_COMPONENTS \
11507 loongarch_disqualify_components
11508
11509 #undef TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS
11510 #define TARGET_SHRINK_WRAP_EMIT_PROLOGUE_COMPONENTS \
11511 loongarch_emit_prologue_components
11512
11513 #undef TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS
11514 #define TARGET_SHRINK_WRAP_EMIT_EPILOGUE_COMPONENTS \
11515 loongarch_emit_epilogue_components
11516
11517 #undef TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS
11518 #define TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS \
11519 loongarch_set_handled_components
11520
11521 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
11522 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
11523 loongarch_builtin_support_vector_misalignment
11524
11525 #undef TARGET_C_MODE_FOR_FLOATING_TYPE
11526 #define TARGET_C_MODE_FOR_FLOATING_TYPE loongarch_c_mode_for_floating_type
11527
11528 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
11529 #define TARGET_OPTION_VALID_ATTRIBUTE_P loongarch_option_valid_attribute_p
11530
11531 #undef TARGET_C_MODE_FOR_SUFFIX
11532 #define TARGET_C_MODE_FOR_SUFFIX loongarch_c_mode_for_suffix
11533
11534 #undef TARGET_COMPUTE_PRESSURE_CLASSES
11535 #define TARGET_COMPUTE_PRESSURE_CLASSES loongarch_compute_pressure_classes
11536
11537 #undef TARGET_CAN_INLINE_P
11538 #define TARGET_CAN_INLINE_P loongarch_can_inline_p
11539
11540 struct gcc_target targetm = TARGET_INITIALIZER;
11541
11542 #include "gt-loongarch.h"