Debian Codegraph @

gcc/config/loongarch/loongarch.md

raw
1 ;; Machine Description for LoongArch for GNU compiler.
2 ;; Copyright (C) 2021-2025 Free Software Foundation, Inc.
3 ;; Contributed by Loongson Ltd.
4 ;; Based on MIPS target for GNU compiler.
5
6 ;; This file is part of GCC.
7
8 ;; GCC is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 3, or (at your option)
11 ;; any later version.
12
13 ;; GCC is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GCC; see the file COPYING3. If not see
20 ;; <http://www.gnu.org/licenses/>.
21
22 (define_c_enum "unspec" [
23 ;; Floating-point moves.
24 UNSPEC_LOAD_LOW
25 UNSPEC_LOAD_HIGH
26 UNSPEC_STORE_WORD
27 UNSPEC_MOVGR2FRH
28 UNSPEC_MOVFRH2GR
29
30 ;; Floating point unspecs.
31 UNSPEC_FRINT
32 UNSPEC_FCLASS
33 UNSPEC_FMAX
34 UNSPEC_FMIN
35 UNSPEC_FTINT
36 UNSPEC_FTINTRM
37 UNSPEC_FTINTRP
38 UNSPEC_FSCALEB
39 UNSPEC_FLOGB
40
41 ;; Override return address for exception handling.
42 UNSPEC_EH_RETURN
43
44 ;; Bit operation
45 UNSPEC_BITREV_4B
46 UNSPEC_BITREV_8B
47
48 ;; TLS
49 UNSPEC_TLS
50 UNSPEC_TLS_DESC
51 UNSPEC_TLS_DESC_OFF64
52
53 ;; Stack tie
54 UNSPEC_TIE
55
56 ;; RSQRT
57 UNSPEC_RSQRT
58 UNSPEC_RSQRTE
59
60 ;; RECIP
61 UNSPEC_RECIPE
62
63 ;; CRC
64 UNSPEC_CRC
65 UNSPEC_CRCC
66
67 UNSPEC_LOAD_FROM_GOT
68 UNSPEC_PCALAU12I
69 UNSPEC_PCALAU12I_GR
70 UNSPEC_ADD_TLS_LE_RELAX
71 UNSPEC_ORI_L_LO12
72 UNSPEC_LUI_L_HI20
73 UNSPEC_LUI_H_LO20
74 UNSPEC_LUI_H_HI12
75 UNSPEC_TLS_LOW
76
77 ;; Fake div.w[u] mod.w[u]
78 UNSPEC_FAKE_ANY_DIV
79
80 UNSPEC_SIBCALL_VALUE_MULTIPLE_INTERNAL_1
81 UNSPEC_CALL_VALUE_MULTIPLE_INTERNAL_1
82
83 UNSPEC_LOAD_SYMBOL_OFFSET64
84 UNSPEC_LA_PCREL_64_PART1
85 UNSPEC_LA_PCREL_64_PART2
86
87 UNSPEC_SSP
88 ])
89
90 (define_c_enum "unspecv" [
91 ;; Blockage and synchronisation.
92 UNSPECV_BLOCKAGE
93 UNSPECV_DBAR
94 UNSPECV_IBAR
95
96 ;; Privileged instructions
97 UNSPECV_CSRRD
98 UNSPECV_CSRWR
99 UNSPECV_CSRXCHG
100 UNSPECV_IOCSRRD
101 UNSPECV_IOCSRWR
102 UNSPECV_CACOP
103 UNSPECV_LDDIR
104 UNSPECV_LDPTE
105 UNSPECV_ERTN
106
107 ;; Stack checking.
108 UNSPECV_PROBE_STACK_RANGE
109
110 ;; Floating-point environment.
111 UNSPECV_MOVFCSR2GR
112 UNSPECV_MOVGR2FCSR
113
114 ;; Others
115 UNSPECV_CPUCFG
116 UNSPECV_ASRTLE_D
117 UNSPECV_ASRTGT_D
118 UNSPECV_SYSCALL
119 UNSPECV_BREAK
120 ])
121
122 (define_constants
123 [(RETURN_ADDR_REGNUM 1)
124 (TP_REGNUM 2)
125 (T0_REGNUM 12)
126 (T1_REGNUM 13)
127 (S0_REGNUM 23)
128
129 (FCC0_REGNUM 64)
130 (FCC1_REGNUM 65)
131 (FCC2_REGNUM 66)
132 (FCC3_REGNUM 67)
133 (FCC4_REGNUM 68)
134 (FCC5_REGNUM 69)
135 (FCC6_REGNUM 70)
136 (FCC7_REGNUM 71)
137
138 ;; Return path styles
139 (NORMAL_RETURN 0)
140 (SIBCALL_RETURN 1)
141 (EXCEPTION_RETURN 2)
142
143 ;; PIC long branch sequences are never longer than 100 bytes.
144 (MAX_PIC_BRANCH_LENGTH 100)
145 ])
146
147 (include "predicates.md")
148 (include "constraints.md")
149
150 ;; ....................
151 ;;
152 ;; Attributes
153 ;;
154 ;; ....................
155
156 (define_attr "enabled" "no,yes" (const_string "yes"))
157
158 (define_attr "got" "unset,load"
159 (const_string "unset"))
160
161 ;; For jirl instructions, this attribute is DIRECT when the target address
162 ;; is symbolic and INDIRECT when it is a register.
163 (define_attr "jirl" "unset,direct,indirect"
164 (const_string "unset"))
165
166
167 ;; Classification of moves, extensions and truncations. Most values
168 ;; are as for "type" (see below) but there are also the following
169 ;; move-specific values:
170 ;;
171 ;; sll0 "slli.w DEST,SRC,0", which on 64-bit targets is guaranteed
172 ;; to produce a sign-extended DEST, even if SRC is not
173 ;; properly sign-extended
174 ;; pick_ins BSTRPICK.W, BSTRPICK.D, BSTRINS.W or BSTRINS.D instruction
175 ;; andi a single ANDI instruction
176 ;; shift_shift a shift left followed by a shift right
177 ;;
178 ;; This attribute is used to determine the instruction's length and
179 ;; scheduling type. For doubleword moves, the attribute always describes
180 ;; the split instructions; in some cases, it is more appropriate for the
181 ;; scheduling type to be "multi" instead.
182 (define_attr "move_type"
183 "unknown,load,fpload,store,fpstore,mgtf,mftg,imul,move,fmove,
184 const,signext,pick_ins,logical,arith,sll0,andi,shift_shift"
185 (const_string "unknown"))
186
187 (define_attr "alu_type" "unknown,add,sub,not,nor,and,or,xor,simd_add"
188 (const_string "unknown"))
189
190 ;; Main data type used by the insn
191 (define_attr "mode" "unknown,none,QI,HI,SI,DI,TI,SF,DF,TF,FCC,
192 V1TI,V2DI,V4SI,V8HI,V16QI,V2DF,V4SF,V2TI,V4DI,V8SI,V16HI,V32QI,V4DF,V8SF"
193 (const_string "unknown"))
194
195 ;; True if the main data type is twice the size of a word.
196 (define_attr "dword_mode" "no,yes"
197 (cond [(and (eq_attr "mode" "DI,DF")
198 (not (match_test "TARGET_64BIT")))
199 (const_string "yes")
200
201 (and (eq_attr "mode" "TI,TF")
202 (match_test "TARGET_64BIT"))
203 (const_string "yes")]
204 (const_string "no")))
205
206 ;; True if the main data type is four times of the size of a word.
207 (define_attr "qword_mode" "no,yes"
208 (cond [(and (eq_attr "mode" "TI,TF")
209 (not (match_test "TARGET_64BIT")))
210 (const_string "yes")]
211 (const_string "no")))
212
213 ;; Classification of each insn.
214 ;; branch conditional branch
215 ;; jump unconditional jump
216 ;; call unconditional call
217 ;; load load instruction(s)
218 ;; fpload floating point load
219 ;; fpidxload floating point indexed load
220 ;; store store instruction(s)
221 ;; fpstore floating point store
222 ;; fpidxstore floating point indexed store
223 ;; prefetch memory prefetch (register + offset)
224 ;; prefetchx memory indexed prefetch (register + register)
225 ;; condmove conditional moves
226 ;; mgtf move general-purpose register to floating point register
227 ;; mftg move floating point register to general-purpose register
228 ;; const load constant
229 ;; arith integer arithmetic instructions
230 ;; logical integer logical instructions
231 ;; shift integer shift instructions
232 ;; slt set less than instructions
233 ;; signext sign extend instructions
234 ;; clz the clz and clo instructions
235 ;; trap trap if instructions
236 ;; imul integer multiply
237 ;; idiv integer divide
238 ;; move integer move
239 ;; fmove floating point register move
240 ;; fadd floating point add/subtract
241 ;; fmul floating point multiply
242 ;; fmadd floating point multiply-add
243 ;; fdiv floating point divide
244 ;; frdiv floating point reciprocal divide
245 ;; frecipe floating point approximate reciprocal
246 ;; fabs floating point absolute value
247 ;; flogb floating point exponent extract
248 ;; fneg floating point negation
249 ;; fcmp floating point compare
250 ;; fcopysign floating point copysign
251 ;; fcvt floating point convert
252 ;; fscaleb floating point scale
253 ;; fsqrt floating point square root
254 ;; frsqrt floating point reciprocal square root
255 ;; frsqrte floating point approximate reciprocal square root
256 ;; multi multiword sequence (or user asm statements)
257 ;; atomic atomic memory update instruction
258 ;; syncloop memory atomic operation implemented as a sync loop
259 ;; nop no operation
260 ;; ghost an instruction that produces no real code
261 (define_attr "type"
262 "unknown,branch,jump,call,load,fpload,fpidxload,store,fpstore,fpidxstore,
263 prefetch,prefetchx,condmove,mgtf,mftg,const,arith,logical,
264 shift,slt,signext,clz,trap,imul,idiv,move,
265 fmove,fadd,fmul,fmadd,fdiv,frdiv,frecipe,fabs,flogb,fneg,fcmp,fcopysign,fcvt,
266 fscaleb,fsqrt,frsqrt,frsqrte,accext,accmod,multi,atomic,syncloop,nop,ghost,
267 simd_div,simd_fclass,simd_flog2,simd_fadd,simd_fcvt,simd_fmul,simd_fmadd,
268 simd_fdiv,simd_bitins,simd_bitmov,simd_insert,simd_sld,simd_mul,simd_fcmp,
269 simd_fexp2,simd_int_arith,simd_bit,simd_shift,simd_splat,simd_fill,
270 simd_permute,simd_shf,simd_sat,simd_pcnt,simd_copy,simd_branch,simd_clsx,
271 simd_fminmax,simd_logic,simd_move,simd_load,simd_store"
272 (cond [(eq_attr "jirl" "!unset") (const_string "call")
273 (eq_attr "got" "load") (const_string "load")
274
275 (eq_attr "alu_type" "add,sub") (const_string "arith")
276
277 (eq_attr "alu_type" "not,nor,and,or,xor") (const_string "logical")
278
279 ;; If a doubleword move uses these expensive instructions,
280 ;; it is usually better to schedule them in the same way
281 ;; as the singleword form, rather than as "multi".
282 (eq_attr "move_type" "load") (const_string "load")
283 (eq_attr "move_type" "fpload") (const_string "fpload")
284 (eq_attr "move_type" "store") (const_string "store")
285 (eq_attr "move_type" "fpstore") (const_string "fpstore")
286 (eq_attr "move_type" "mgtf") (const_string "mgtf")
287 (eq_attr "move_type" "mftg") (const_string "mftg")
288
289 ;; These types of move are always single insns.
290 (eq_attr "move_type" "imul") (const_string "imul")
291 (eq_attr "move_type" "fmove") (const_string "fmove")
292 (eq_attr "move_type" "signext") (const_string "signext")
293 (eq_attr "move_type" "pick_ins") (const_string "arith")
294 (eq_attr "move_type" "arith") (const_string "arith")
295 (eq_attr "move_type" "logical") (const_string "logical")
296 (eq_attr "move_type" "sll0") (const_string "shift")
297 (eq_attr "move_type" "andi") (const_string "logical")
298
299 ;; These types of move are always split.
300 (eq_attr "move_type" "shift_shift")
301 (const_string "multi")
302
303 ;; These types of move are split for quadword modes only.
304 (and (eq_attr "move_type" "move,const")
305 (eq_attr "qword_mode" "yes"))
306 (const_string "multi")
307
308 ;; These types of move are split for doubleword modes only.
309 (and (eq_attr "move_type" "move,const")
310 (eq_attr "dword_mode" "yes"))
311 (const_string "multi")
312 (eq_attr "move_type" "move") (const_string "move")
313 (eq_attr "move_type" "const") (const_string "const")]
314 (const_string "unknown")))
315
316 ;; Mode for conversion types (fcvt)
317 ;; I2S integer to float single (SI/DI to SF)
318 ;; I2D integer to float double (SI/DI to DF)
319 ;; S2I float to integer (SF to SI/DI)
320 ;; D2I float to integer (DF to SI/DI)
321 ;; D2S double to float single
322 ;; S2D float single to double
323
324 (define_attr "cnv_mode" "unknown,I2S,I2D,S2I,D2I,D2S,S2D"
325 (const_string "unknown"))
326
327 ;; The number of individual instructions that a non-branch pattern generates
328 (define_attr "insn_count" ""
329 (cond [;; "Ghost" instructions occupy no space.
330 (eq_attr "type" "ghost")
331 (const_int 0)
332
333 ;; Check for doubleword moves that are decomposed into two
334 ;; instructions.
335 (and (eq_attr "move_type" "mgtf,mftg,move")
336 (eq_attr "dword_mode" "yes"))
337 (const_int 2)
338
339 ;; Check for quadword moves that are decomposed into four
340 ;; instructions.
341 (and (eq_attr "move_type" "mgtf,mftg,move")
342 (eq_attr "qword_mode" "yes"))
343 (const_int 4)
344
345 ;; Constants, loads and stores are handled by external routines.
346 (and (eq_attr "move_type" "const")
347 (eq_attr "dword_mode" "yes"))
348 (symbol_ref "loongarch_split_const_insns (operands[1])")
349 (eq_attr "move_type" "const")
350 (symbol_ref "loongarch_const_insns (operands[1])")
351 (eq_attr "move_type" "load,fpload")
352 (symbol_ref "loongarch_load_store_insns (operands[1], insn)")
353 (eq_attr "move_type" "store,fpstore")
354 (symbol_ref "loongarch_load_store_insns (operands[0], insn)")
355
356 (eq_attr "type" "idiv")
357 (symbol_ref "loongarch_idiv_insns (GET_MODE (PATTERN (insn)))")]
358 (const_int 1)))
359
360 ;; Length of instruction in bytes.
361 (define_attr "length" ""
362 (cond [
363 ;; Branch futher than +/- 128 KiB require two instructions.
364 (eq_attr "type" "branch")
365 (if_then_else (and (le (minus (match_dup 0) (pc)) (const_int 131064))
366 (le (minus (pc) (match_dup 0)) (const_int 131068)))
367 (const_int 4)
368 (const_int 8))]
369 (symbol_ref "get_attr_insn_count (insn) * 4")))
370
371 ;; Describe a user's asm statement.
372 (define_asm_attributes
373 [(set_attr "type" "multi")])
374
375 ;; This mode iterator allows 32-bit and 64-bit GPR patterns to be generated
376 ;; from the same template.
377 (define_mode_iterator GPR [SI (DI "TARGET_64BIT")])
378
379 ;; This mode iterator allows 16-bit and 32-bit GPR patterns and 32-bit 64-bit
380 ;; FPR patterns to be generated from the same template.
381 (define_mode_iterator JOIN_MODE [HI
382 SI
383 (SF "TARGET_HARD_FLOAT")
384 (DF "TARGET_DOUBLE_FLOAT")])
385
386 ;; This mode iterator allows :P to be used for patterns that operate on
387 ;; pointer-sized quantities. Exactly one of the two alternatives will match.
388 (define_mode_iterator P [(SI "Pmode == SImode") (DI "Pmode == DImode")])
389
390 ;; Likewise, but for GRLEN-sized quantities.
391 (define_mode_iterator X [(SI "!TARGET_64BIT") (DI "TARGET_64BIT")])
392
393 ;; 64-bit modes for which we provide move patterns.
394 (define_mode_iterator MOVE64 [DI DF])
395
396 ;; Iterator for sub-32-bit integer modes.
397 (define_mode_iterator SHORT [QI HI])
398
399 ;; Likewise the 64-bit truncate-and-shift patterns.
400 (define_mode_iterator SUBDI [QI HI SI])
401
402 ;; Iterator for scalar fixed point modes.
403 (define_mode_iterator QHWD [QI HI SI (DI "TARGET_64BIT")])
404
405 ;; Iterator for hardware-supported floating-point modes.
406 (define_mode_iterator ANYF [(SF "TARGET_HARD_FLOAT")
407 (DF "TARGET_DOUBLE_FLOAT")])
408
409 ;; Iterator for fixed-point modes which can be hold by a hardware
410 ;; floating-point register.
411 (define_mode_iterator ANYFI [(SI "TARGET_HARD_FLOAT")
412 (DI "TARGET_DOUBLE_FLOAT")])
413
414 ;; A mode for anything with 32 bits or more, and able to be loaded with
415 ;; the same addressing mode as ld.w.
416 (define_mode_iterator LD_AT_LEAST_32_BIT [GPR ANYF])
417
418 ;; A mode for anything able to be stored with the same addressing mode as
419 ;; st.w.
420 (define_mode_iterator ST_ANY [QHWD ANYF])
421
422 ;; A mode for anything legal as a input of a div or mod instruction.
423 (define_mode_iterator DIV [(DI "TARGET_64BIT")
424 (SI "!TARGET_64BIT || ISA_HAS_DIV32")])
425
426 ;; In GPR templates, a string like "mul.<d>" will expand to "mul.w" in the
427 ;; 32-bit version and "mul.d" in the 64-bit version.
428 (define_mode_attr d [(SI "w") (DI "d")])
429
430 ;; This attribute gives the length suffix for a load or store instruction.
431 ;; The same suffixes work for zero and sign extensions.
432 (define_mode_attr size [(QI "b") (HI "h") (SI "w") (DI "d")])
433 (define_mode_attr SIZE [(QI "B") (HI "H") (SI "W") (DI "D")])
434
435 ;; This attribute gives the mode mask of a SHORT.
436 (define_mode_attr mask [(QI "0x00ff") (HI "0xffff")])
437
438 ;; This attribute gives the size (bits) of a SHORT.
439 (define_mode_attr 7_or_15 [(QI "7") (HI "15")])
440
441 ;; Instruction names for stores.
442 (define_mode_attr store [(QI "sb") (HI "sh") (SI "sw") (DI "sd")])
443
444 ;; This attribute gives the format suffix for floating-point operations.
445 (define_mode_attr fmt [(SF "s") (DF "d")])
446 (define_mode_attr ifmt [(SI "w") (DI "l")])
447
448 ;; This attribute gives the upper-case mode name for one unit of a
449 ;; floating-point mode or vector mode.
450 (define_mode_attr UNITMODE [(SF "SF") (DF "DF") (V2SF "SF") (V4SF "SF")
451 (V16QI "QI") (V8HI "HI") (V4SI "SI") (V2DI "DI")
452 (V2DF "DF")(V8SF "SF")(V32QI "QI")(V16HI "HI")(V8SI "SI")(V4DI "DI")(V4DF "DF")])
453
454 ;; As above, but in lower case.
455 (define_mode_attr unitmode [(SF "sf") (DF "df") (V2SF "sf") (V4SF "sf")
456 (V16QI "qi") (V8QI "qi") (V8HI "hi") (V4HI "hi")
457 (V4SI "si") (V2SI "si") (V2DI "di") (V2DF "df")
458 (V8SI "si") (V4DI "di") (V32QI "qi") (V16HI "hi")
459 (V8SF "sf") (V4DF "df")])
460
461 ;; This attribute gives the integer mode that has half the size of
462 ;; the controlling mode.
463 (define_mode_attr HALFMODE [(DF "SI") (DI "SI") (V2SF "SI")
464 (V2SI "SI") (V4HI "SI") (V8QI "SI")
465 (TF "DI")])
466
467 ;; This attribute gives the integer mode that has the same size of a
468 ;; floating-point mode.
469 (define_mode_attr IMODE [(SF "SI") (DF "DI")])
470
471 ;; This code iterator allows signed and unsigned widening multiplications
472 ;; to use the same template.
473 (define_code_iterator any_extend [sign_extend zero_extend])
474
475 ;; This code iterator allows the two right shift instructions to be
476 ;; generated from the same template.
477 (define_code_iterator any_shiftrt [ashiftrt lshiftrt])
478
479 ;; This code iterator allows the three shift instructions to be generated
480 ;; from the same template.
481 (define_code_iterator any_shift [ashift ashiftrt lshiftrt])
482
483 ;; This code iterator allows the three bitwise instructions to be generated
484 ;; from the same template.
485 (define_code_iterator any_bitwise [and ior xor])
486 (define_code_iterator any_or [ior xor])
487 (define_code_iterator neg_bitwise [and ior])
488 (define_code_attr bitwise_operand [(and "and_operand")
489 (ior "uns_arith_operand")
490 (xor "uns_arith_operand")])
491 (define_code_attr is_and [(and "true") (ior "false") (xor "false")])
492
493 ;; If we know the operands does not have overlapping bits, use this
494 ;; instead of just ior to cover more cases.
495 (define_code_iterator any_or_plus [any_or plus])
496
497 ;; This code iterator allows unsigned and signed division to be generated
498 ;; from the same template.
499 (define_code_iterator any_div [div udiv mod umod])
500
501 ;; This code iterator allows addition and subtraction to be generated
502 ;; from the same template.
503 (define_code_iterator addsub [plus minus])
504
505 ;; This code iterator allows addition and multiplication to be generated
506 ;; from the same template.
507 (define_code_iterator addmul [plus mult])
508
509 ;; This code iterator allows addition subtraction and multiplication to be
510 ;; generated from the same template
511 (define_code_iterator addsubmul [plus minus mult])
512
513 ;; This code iterator allows all native floating-point comparisons to be
514 ;; generated from the same template.
515 (define_code_iterator fcond [unordered uneq unlt unle eq lt le
516 ordered ltgt ne ge gt unge ungt])
517
518 ;; Equality operators.
519 (define_code_iterator equality_op [eq ne])
520
521 ;; These code iterators allow the signed and unsigned scc operations to use
522 ;; the same template.
523 (define_code_iterator any_gt [gt gtu])
524 (define_code_iterator any_lt [lt ltu])
525 (define_code_iterator any_le [le leu])
526
527 (define_code_iterator any_return [return simple_return])
528
529 ;; <u> expands to an empty string when doing a signed operation and
530 ;; "u" when doing an unsigned operation.
531 (define_code_attr u [(sign_extend "") (zero_extend "u")
532 (div "") (udiv "u")
533 (mod "") (umod "u")
534 (gt "") (gtu "u")
535 (ge "") (geu "u")
536 (lt "") (ltu "u")
537 (le "") (leu "u")
538 (smax "") (umax "u")])
539
540 ;; <U> is like <u> except uppercase.
541 (define_code_attr U [(sign_extend "") (zero_extend "U")])
542
543 ;; <su> is like <u>, but the signed form expands to "s" rather than "".
544 (define_code_attr su [(sign_extend "s") (zero_extend "u")
545 (smax "s") (umax "u")])
546
547 (define_code_attr u_bool [(sign_extend "false") (zero_extend "true")])
548
549 ;; <optab> expands to the name of the optab for a particular code.
550 (define_code_attr optab [(ashift "ashl")
551 (ashiftrt "ashr")
552 (lshiftrt "lshr")
553 (rotatert "rotr")
554 (ior "ior")
555 (xor "xor")
556 (and "and")
557 (plus "add")
558 (minus "sub")
559 (mult "mul")
560 (div "div")
561 (udiv "udiv")
562 (mod "mod")
563 (umod "umod")
564 (return "return")
565 (simple_return "simple_return")])
566
567 ;; <insn> expands to the name of the insn that implements a particular code.
568 (define_code_attr insn [(ashift "sll")
569 (ashiftrt "sra")
570 (lshiftrt "srl")
571 (rotatert "rotr")
572 (ior "or")
573 (xor "xor")
574 (and "and")
575 (plus "addu")
576 (minus "subu")
577 (div "div")
578 (udiv "div")
579 (mod "mod")
580 (umod "mod")])
581
582 ;; <fcond> is the fcmp.cond.fmt condition associated with a particular code.
583 (define_code_attr fcond [(unordered "cun")
584 (uneq "cueq")
585 (unlt "cult")
586 (unle "cule")
587 (eq "ceq")
588 (lt "slt")
589 (le "sle")
590 (ordered "cor")
591 (ltgt "sne")
592 (ne "cune")
593 (ge "sge")
594 (gt "sgt")
595 (unge "cuge")
596 (ungt "cugt")])
597
598 ;; The sel mnemonic to use depending on the condition test.
599 (define_code_attr sel [(eq "masknez") (ne "maskeqz")])
600 (define_code_attr selinv [(eq "maskeqz") (ne "masknez")])
601
602 ;; Iterator and attributes for floating-point to fixed-point conversion
603 ;; instructions.
604 (define_int_iterator LRINT [UNSPEC_FTINT UNSPEC_FTINTRM UNSPEC_FTINTRP])
605 (define_int_attr lrint_pattern [(UNSPEC_FTINT "lrint")
606 (UNSPEC_FTINTRM "lfloor")
607 (UNSPEC_FTINTRP "lceil")])
608 (define_int_attr lrint_submenmonic [(UNSPEC_FTINT "")
609 (UNSPEC_FTINTRM "rm")
610 (UNSPEC_FTINTRP "rp")])
611
612 ;; Iterator and attributes for bytepick.d
613 (define_int_iterator bytepick_w_ashift_amount [8 16 24])
614 (define_int_attr bytepick_w_lshiftrt_amount [(8 "24")
615 (16 "16")
616 (24 "8")])
617 (define_int_iterator bytepick_d_ashift_amount [8 16 24 32 40 48 56])
618 (define_int_attr bytepick_d_lshiftrt_amount [(8 "56")
619 (16 "48")
620 (24 "40")
621 (32 "32")
622 (40 "24")
623 (48 "16")
624 (56 "8")])
625 (define_int_attr bytepick_imm [(8 "1")
626 (16 "2")
627 (24 "3")
628 (32 "4")
629 (40 "5")
630 (48 "6")
631 (56 "7")])
632
633 ;; Expand some 32-bit operations to si3_extend operations if TARGET_64BIT
634 ;; so the redundant sign extension can be removed if the output is used as
635 ;; an input of a bitwise operation. Note plus, rotl, and div are handled
636 ;; separately.
637 (define_code_iterator shift_w [any_shift rotatert])
638 (define_code_iterator arith_w [minus mult])
639
640 (define_expand "<optab><mode>3"
641 [(set (match_operand:GPR 0 "register_operand" "=r")
642 (shift_w:GPR (match_operand:GPR 1 "register_operand" "r")
643 (match_operand:SI 2 "arith_operand" "rI")))]
644 ""
645 {
646 if (TARGET_64BIT && <MODE>mode == SImode)
647 {
648 rtx t = gen_reg_rtx (DImode);
649 emit_insn (gen_<optab>si3_extend (t, operands[1], operands[2]));
650 t = gen_lowpart (SImode, t);
651 SUBREG_PROMOTED_VAR_P (t) = 1;
652 SUBREG_PROMOTED_SET (t, SRP_SIGNED);
653 emit_move_insn (operands[0], t);
654 DONE;
655 }
656 })
657
658 (define_expand "<optab><mode>3"
659 [(set (match_operand:GPR 0 "register_operand" "=r")
660 (arith_w:GPR (match_operand:GPR 1 "register_operand" "r")
661 (match_operand:GPR 2 "register_operand" "r")))]
662 ""
663 {
664 if (TARGET_64BIT && <MODE>mode == SImode)
665 {
666 rtx t = gen_reg_rtx (DImode);
667 emit_insn (gen_<optab>si3_extend (t, operands[1], operands[2]));
668 t = gen_lowpart (SImode, t);
669 SUBREG_PROMOTED_VAR_P (t) = 1;
670 SUBREG_PROMOTED_SET (t, SRP_SIGNED);
671 emit_move_insn (operands[0], t);
672 DONE;
673 }
674 })
675
676 ;;
677 ;; ....................
678 ;;
679 ;; CONDITIONAL TRAPS
680 ;;
681 ;; ....................
682 ;;
683
684 (define_insn "trap"
685 [(trap_if (const_int 1) (const_int 0))]
686 ""
687 {
688 return "break\t0";
689 }
690 [(set_attr "type" "trap")])
691
692
693
694 ;;
695 ;; ....................
696 ;;
697 ;; ADDITION
698 ;;
699 ;; ....................
700 ;;
701
702 (define_insn "add<mode>3"
703 [(set (match_operand:ANYF 0 "register_operand" "=f")
704 (plus:ANYF (match_operand:ANYF 1 "register_operand" "f")
705 (match_operand:ANYF 2 "register_operand" "f")))]
706 ""
707 "fadd.<fmt>\t%0,%1,%2"
708 [(set_attr "type" "fadd")
709 (set_attr "mode" "<UNITMODE>")])
710
711 (define_insn_and_split "*addsi3"
712 [(set (match_operand:SI 0 "register_operand" "=r,r,r,r,r")
713 (plus:SI (match_operand:SI 1 "register_operand" "r,r,r,r,r")
714 (match_operand:SI 2 "plus_si_operand"
715 "r,I,La,Lb,Le")))]
716 ""
717 "@
718 add.w\t%0,%1,%2
719 addi.w\t%0,%1,%2
720 #
721 * operands[2] = GEN_INT (INTVAL (operands[2]) / 65536); \
722 return \"addu16i.d\t%0,%1,%2\";
723 #"
724 "CONST_INT_P (operands[2]) && !IMM12_INT (operands[2]) \
725 && !ADDU16I_OPERAND (INTVAL (operands[2]))"
726 [(set (match_dup 0) (plus:SI (match_dup 1) (match_dup 3)))
727 (set (match_dup 0) (plus:SI (match_dup 0) (match_dup 4)))]
728 {
729 loongarch_split_plus_constant (&operands[2], SImode);
730 }
731 [(set_attr "alu_type" "add")
732 (set_attr "mode" "SI")
733 (set_attr "insn_count" "1,1,2,1,2")])
734
735 (define_expand "addsi3"
736 [(set (match_operand:SI 0 "register_operand" "=r,r,r,r,r")
737 (plus:SI (match_operand:SI 1 "register_operand" "r,r,r,r,r")
738 (match_operand:SI 2 "plus_si_operand" "r,I,La,Le,Lb")))]
739 "TARGET_64BIT"
740 {
741 if (CONST_INT_P (operands[2]) && !IMM12_INT (operands[2])
742 && ADDU16I_OPERAND (INTVAL (operands[2])))
743 {
744 rtx t1 = gen_reg_rtx (DImode);
745 rtx t2 = gen_reg_rtx (DImode);
746 rtx t3 = gen_reg_rtx (DImode);
747 emit_insn (gen_extend_insn (t1, operands[1], DImode, SImode, 0));
748 t2 = operands[2];
749 emit_insn (gen_adddi3 (t3, t1, t2));
750 t3 = gen_lowpart (SImode, t3);
751 emit_move_insn (operands[0], t3);
752 DONE;
753 }
754 else
755 {
756 rtx t = gen_reg_rtx (DImode);
757 emit_insn (gen_addsi3_extended (t, operands[1], operands[2]));
758 t = gen_lowpart (SImode, t);
759 SUBREG_PROMOTED_VAR_P (t) = 1;
760 SUBREG_PROMOTED_SET (t, SRP_SIGNED);
761 emit_move_insn (operands[0], t);
762 DONE;
763 }
764 })
765
766 (define_insn_and_split "adddi3"
767 [(set (match_operand:DI 0 "register_operand" "=r,r,r,r,r,r")
768 (plus:DI (match_operand:DI 1 "register_operand" "r,r,r,r,r,r")
769 (match_operand:DI 2 "plus_di_operand"
770 "r,I,La,Lb,Lc,Ld")))]
771 "TARGET_64BIT"
772 "@
773 add.d\t%0,%1,%2
774 addi.d\t%0,%1,%2
775 #
776 * operands[2] = GEN_INT (INTVAL (operands[2]) / 65536); \
777 return \"addu16i.d\t%0,%1,%2\";
778 #
779 #"
780 "&& CONST_INT_P (operands[2]) && !IMM12_INT (operands[2]) \
781 && !ADDU16I_OPERAND (INTVAL (operands[2]))"
782 [(set (match_dup 0) (plus:DI (match_dup 1) (match_dup 3)))
783 (set (match_dup 0) (plus:DI (match_dup 0) (match_dup 4)))]
784 {
785 loongarch_split_plus_constant (&operands[2], DImode);
786 }
787 [(set_attr "alu_type" "add")
788 (set_attr "mode" "DI")
789 (set_attr "insn_count" "1,1,2,1,2,2")])
790
791 (define_insn_and_split "addsi3_extended"
792 [(set (match_operand:DI 0 "register_operand" "=r,r,r,r")
793 (sign_extend:DI
794 (plus:SI (match_operand:SI 1 "register_operand" "r,r,r,r")
795 (match_operand:SI 2 "plus_si_extend_operand"
796 "r,I,La,Le"))))]
797 "TARGET_64BIT"
798 "@
799 add.w\t%0,%1,%2
800 addi.w\t%0,%1,%2
801 #
802 #"
803 "CONST_INT_P (operands[2]) && !IMM12_INT (operands[2])"
804 [(set (subreg:SI (match_dup 0) 0) (plus:SI (match_dup 1) (match_dup 3)))
805 (set (match_dup 0)
806 (sign_extend:DI (plus:SI (subreg:SI (match_dup 0) 0)
807 (match_dup 4))))]
808 {
809 loongarch_split_plus_constant (&operands[2], SImode);
810 }
811 [(set_attr "alu_type" "add")
812 (set_attr "mode" "SI")
813 (set_attr "insn_count" "1,1,2,2")])
814
815
816 ;;
817 ;; ....................
818 ;;
819 ;; SUBTRACTION
820 ;;
821 ;; ....................
822 ;;
823
824 (define_insn "sub<mode>3"
825 [(set (match_operand:ANYF 0 "register_operand" "=f")
826 (minus:ANYF (match_operand:ANYF 1 "register_operand" "f")
827 (match_operand:ANYF 2 "register_operand" "f")))]
828 ""
829 "fsub.<fmt>\t%0,%1,%2"
830 [(set_attr "type" "fadd")
831 (set_attr "mode" "<UNITMODE>")])
832
833 (define_insn "*sub<mode>3"
834 [(set (match_operand:GPR 0 "register_operand" "=r")
835 (minus:GPR (match_operand:GPR 1 "register_operand" "r")
836 (match_operand:GPR 2 "register_operand" "r")))]
837 ""
838 "sub.<d>\t%0,%z1,%2"
839 [(set_attr "alu_type" "sub")
840 (set_attr "mode" "<MODE>")])
841
842
843 (define_insn "subsi3_extend"
844 [(set (match_operand:DI 0 "register_operand" "=r")
845 (sign_extend:DI
846 (minus:SI (match_operand:SI 1 "reg_or_0_operand" "rJ")
847 (match_operand:SI 2 "register_operand" "r"))))]
848 "TARGET_64BIT"
849 "sub.w\t%0,%z1,%2"
850 [(set_attr "type" "arith")
851 (set_attr "mode" "SI")])
852
853 ;;
854 ;; ....................
855 ;;
856 ;; MULTIPLICATION
857 ;;
858 ;; ....................
859 ;;
860
861 (define_insn "mul<mode>3"
862 [(set (match_operand:ANYF 0 "register_operand" "=f")
863 (mult:ANYF (match_operand:ANYF 1 "register_operand" "f")
864 (match_operand:ANYF 2 "register_operand" "f")))]
865 ""
866 "fmul.<fmt>\t%0,%1,%2"
867 [(set_attr "type" "fmul")
868 (set_attr "mode" "<MODE>")])
869
870 (define_insn "*mul<mode>3"
871 [(set (match_operand:GPR 0 "register_operand" "=r")
872 (mult:GPR (match_operand:GPR 1 "register_operand" "r")
873 (match_operand:GPR 2 "register_operand" "r")))]
874 ""
875 "mul.<d>\t%0,%1,%2"
876 [(set_attr "type" "imul")
877 (set_attr "mode" "<MODE>")])
878
879 (define_insn "mulsi3_extend"
880 [(set (match_operand:DI 0 "register_operand" "=r")
881 (sign_extend:DI
882 (mult:SI (match_operand:SI 1 "register_operand" "r")
883 (match_operand:SI 2 "register_operand" "r"))))]
884 "TARGET_64BIT"
885 "mul.w\t%0,%1,%2"
886 [(set_attr "type" "imul")
887 (set_attr "mode" "SI")])
888
889 ;;
890 ;; ........................
891 ;;
892 ;; MULTIPLICATION HIGH-PART
893 ;;
894 ;; ........................
895 ;;
896
897 (define_expand "<u>mulditi3"
898 [(set (match_operand:TI 0 "register_operand")
899 (mult:TI (any_extend:TI (match_operand:DI 1 "register_operand"))
900 (any_extend:TI (match_operand:DI 2 "register_operand"))))]
901 "TARGET_64BIT"
902 {
903 rtx low = gen_reg_rtx (DImode);
904 emit_insn (gen_muldi3 (low, operands[1], operands[2]));
905
906 rtx high = gen_reg_rtx (DImode);
907 emit_insn (gen_<su>muldi3_highpart (high, operands[1], operands[2]));
908
909 emit_move_insn (gen_lowpart (DImode, operands[0]), low);
910 emit_move_insn (gen_highpart (DImode, operands[0]), high);
911 DONE;
912 })
913
914 (define_insn "<su>muldi3_highpart"
915 [(set (match_operand:DI 0 "register_operand" "=r")
916 (truncate:DI
917 (lshiftrt:TI
918 (mult:TI (any_extend:TI
919 (match_operand:DI 1 "register_operand" " r"))
920 (any_extend:TI
921 (match_operand:DI 2 "register_operand" " r")))
922 (const_int 64))))]
923 "TARGET_64BIT"
924 "mulh.d<u>\t%0,%1,%2"
925 [(set_attr "type" "imul")
926 (set_attr "mode" "DI")])
927
928 (define_expand "<u>mulsidi3"
929 [(set (match_operand:DI 0 "register_operand")
930 (mult:DI (any_extend:DI
931 (match_operand:SI 1 "register_operand"))
932 (any_extend:DI
933 (match_operand:SI 2 "register_operand"))))]
934 ""
935 {
936 if (!TARGET_64BIT)
937 {
938 rtx temp = gen_reg_rtx (SImode);
939 emit_insn (gen_mulsi3 (temp, operands[1], operands[2]));
940 emit_insn (gen_<su>mulsi3_highpart (loongarch_subword (operands[0], true),
941 operands[1], operands[2]));
942 emit_insn (gen_movsi (loongarch_subword (operands[0], false), temp));
943 DONE;
944 }
945 })
946
947 (define_insn "<u>mulsidi3_64bit"
948 [(set (match_operand:DI 0 "register_operand" "=r")
949 (mult:DI (any_extend:DI (match_operand:SI 1 "register_operand" "r"))
950 (any_extend:DI (match_operand:SI 2 "register_operand" "r"))))]
951 "TARGET_64BIT"
952 "mulw.d.w<u>\t%0,%1,%2"
953 [(set_attr "type" "imul")
954 (set_attr "mode" "DI")])
955
956 (define_insn "<su>mulsi3_highpart"
957 [(set (match_operand:SI 0 "register_operand" "=r")
958 (truncate:SI
959 (lshiftrt:DI
960 (mult:DI (any_extend:DI
961 (match_operand:SI 1 "register_operand" " r"))
962 (any_extend:DI
963 (match_operand:SI 2 "register_operand" " r")))
964 (const_int 32))))]
965 ""
966 "mulh.w<u>\t%0,%1,%2"
967 [(set_attr "type" "imul")
968 (set_attr "mode" "SI")])
969
970 ;; Under the LoongArch architecture, the mulh.w[u] instruction performs
971 ;; sign extension by default, so the sign extension instruction can be
972 ;; eliminated.
973 (define_peephole
974 [(set (match_operand:SI 0 "register_operand")
975 (truncate:SI
976 (lshiftrt:DI
977 (mult:DI (any_extend:DI
978 (match_operand:SI 1 "register_operand"))
979 (any_extend:DI
980 (match_operand:SI 2 "register_operand")))
981 (const_int 32))))
982 (set (match_operand:DI 3 "register_operand")
983 (sign_extend:DI (match_dup 0)))]
984 "TARGET_64BIT && REGNO (operands[0]) == REGNO (operands[3])"
985 "mulh.w<u>\t%0,%1,%2")
986
987 ;;
988 ;; ....................
989 ;;
990 ;; DIVISION and REMAINDER
991 ;;
992 ;; ....................
993 ;;
994
995 ;; Float division and modulus.
996 (define_expand "div<mode>3"
997 [(set (match_operand:ANYF 0 "register_operand")
998 (div:ANYF (match_operand:ANYF 1 "reg_or_1_operand")
999 (match_operand:ANYF 2 "register_operand")))]
1000 ""
1001 {
1002 if (<MODE>mode == SFmode
1003 && TARGET_RECIP_DIV
1004 && optimize_insn_for_speed_p ()
1005 && flag_finite_math_only && !flag_trapping_math
1006 && flag_unsafe_math_optimizations)
1007 {
1008 loongarch_emit_swdivsf (operands[0], operands[1],
1009 operands[2], SFmode);
1010 DONE;
1011 }
1012 })
1013
1014 (define_insn "*div<mode>3"
1015 [(set (match_operand:ANYF 0 "register_operand" "=f")
1016 (div:ANYF (match_operand:ANYF 1 "register_operand" "f")
1017 (match_operand:ANYF 2 "register_operand" "f")))]
1018 ""
1019 "fdiv.<fmt>\t%0,%1,%2"
1020 [(set_attr "type" "fdiv")
1021 (set_attr "mode" "<UNITMODE>")])
1022
1023 ;; In 3A5000, the reciprocal operation is the same as the division operation.
1024
1025 (define_insn "*recip<mode>3"
1026 [(set (match_operand:ANYF 0 "register_operand" "=f")
1027 (div:ANYF (match_operand:ANYF 1 "const_1_operand" "")
1028 (match_operand:ANYF 2 "register_operand" "f")))]
1029 ""
1030 "frecip.<fmt>\t%0,%2"
1031 [(set_attr "type" "frdiv")
1032 (set_attr "mode" "<UNITMODE>")])
1033
1034 ;; Approximate Reciprocal Instructions.
1035
1036 (define_insn "loongarch_frecipe_<fmt>"
1037 [(set (match_operand:ANYF 0 "register_operand" "=f")
1038 (unspec:ANYF [(match_operand:ANYF 1 "register_operand" "f")]
1039 UNSPEC_RECIPE))]
1040 "ISA_HAS_FRECIPE"
1041 "frecipe.<fmt>\t%0,%1"
1042 [(set_attr "type" "frecipe")
1043 (set_attr "mode" "<UNITMODE>")
1044 (set_attr "insn_count" "1")])
1045
1046 ;; Integer division and modulus.
1047 (define_expand "<optab><mode>3"
1048 [(set (match_operand:GPR 0 "register_operand")
1049 (any_div:GPR (match_operand:GPR 1 "register_operand")
1050 (match_operand:GPR 2 "register_operand")))]
1051 ""
1052 {
1053 if (GET_MODE (operands[0]) == SImode && TARGET_64BIT)
1054 {
1055 if (ISA_HAS_DIV32)
1056 {
1057 rtx t = gen_reg_rtx (DImode);
1058 emit_insn (gen_<optab>si3_extended (t, operands[1], operands[2]));
1059 t = gen_lowpart (SImode, t);
1060 SUBREG_PROMOTED_VAR_P (t) = 1;
1061 SUBREG_PROMOTED_SET (t, SRP_SIGNED);
1062 emit_move_insn (operands[0], t);
1063 DONE;
1064 }
1065
1066 rtx reg1 = gen_reg_rtx (DImode);
1067 rtx reg2 = gen_reg_rtx (DImode);
1068 rtx rd = gen_reg_rtx (DImode);
1069
1070 operands[1] = gen_rtx_SIGN_EXTEND (word_mode, operands[1]);
1071 operands[2] = gen_rtx_SIGN_EXTEND (word_mode, operands[2]);
1072
1073 emit_insn (gen_rtx_SET (reg1, operands[1]));
1074 emit_insn (gen_rtx_SET (reg2, operands[2]));
1075
1076 emit_insn (gen_<optab>di3_fake (rd, reg1, reg2));
1077 emit_insn (gen_rtx_SET (operands[0],
1078 simplify_gen_subreg (SImode, rd, DImode, 0)));
1079 DONE;
1080 }
1081 })
1082
1083 (define_insn "*<optab><mode>3"
1084 [(set (match_operand:DIV 0 "register_operand" "=r,&r,&r")
1085 (any_div:DIV (match_operand:DIV 1 "register_operand" "r,r,0")
1086 (match_operand:DIV 2 "register_operand" "r,r,r")))]
1087 ""
1088 {
1089 return loongarch_output_division ("<insn>.<d><u>\t%0,%1,%2", operands);
1090 }
1091 [(set_attr "type" "idiv")
1092 (set_attr "mode" "<MODE>")
1093 (set (attr "enabled")
1094 (if_then_else
1095 (match_test "!!which_alternative == loongarch_check_zero_div_p()")
1096 (const_string "yes")
1097 (const_string "no")))])
1098
1099 (define_insn "<optab>si3_extended"
1100 [(set (match_operand:DI 0 "register_operand" "=r,&r,&r")
1101 (sign_extend:DI
1102 (any_div:SI (match_operand:SI 1 "register_operand" "r,r,0")
1103 (match_operand:SI 2 "register_operand" "r,r,r"))))]
1104 "TARGET_64BIT && ISA_HAS_DIV32"
1105 {
1106 return loongarch_output_division ("<insn>.w<u>\t%0,%1,%2", operands);
1107 }
1108 [(set_attr "type" "idiv")
1109 (set_attr "mode" "SI")
1110 (set (attr "enabled")
1111 (if_then_else
1112 (match_test "!!which_alternative == loongarch_check_zero_div_p()")
1113 (const_string "yes")
1114 (const_string "no")))])
1115
1116 (define_insn "<optab>di3_fake"
1117 [(set (match_operand:DI 0 "register_operand" "=r,&r,&r")
1118 (sign_extend:DI
1119 (unspec:SI
1120 [(subreg:SI
1121 (any_div:DI (match_operand:DI 1 "register_operand" "r,r,0")
1122 (match_operand:DI 2 "register_operand" "r,r,r")) 0)]
1123 UNSPEC_FAKE_ANY_DIV)))]
1124 "TARGET_64BIT && !ISA_HAS_DIV32"
1125 {
1126 return loongarch_output_division ("<insn>.w<u>\t%0,%1,%2", operands);
1127 }
1128 [(set_attr "type" "idiv")
1129 (set_attr "mode" "SI")
1130 (set (attr "enabled")
1131 (if_then_else
1132 (match_test "!!which_alternative == loongarch_check_zero_div_p()")
1133 (const_string "yes")
1134 (const_string "no")))])
1135
1136 ;; Floating point multiply accumulate instructions.
1137
1138 ;; a * b + c
1139 (define_insn "fma<mode>4"
1140 [(set (match_operand:ANYF 0 "register_operand" "=f")
1141 (fma:ANYF (match_operand:ANYF 1 "register_operand" "f")
1142 (match_operand:ANYF 2 "register_operand" "f")
1143 (match_operand:ANYF 3 "register_operand" "f")))]
1144 ""
1145 "fmadd.<fmt>\t%0,%1,%2,%3"
1146 [(set_attr "type" "fmadd")
1147 (set_attr "mode" "<UNITMODE>")])
1148
1149 ;; a * b - c
1150 (define_insn "fms<mode>4"
1151 [(set (match_operand:ANYF 0 "register_operand" "=f")
1152 (fma:ANYF (match_operand:ANYF 1 "register_operand" "f")
1153 (match_operand:ANYF 2 "register_operand" "f")
1154 (neg:ANYF (match_operand:ANYF 3 "register_operand" "f"))))]
1155 ""
1156 "fmsub.<fmt>\t%0,%1,%2,%3"
1157 [(set_attr "type" "fmadd")
1158 (set_attr "mode" "<UNITMODE>")])
1159
1160 ;; fnma is defined in GCC as (fma (neg op1) op2 op3)
1161 ;; (-op1 * op2) + op3 ==> -(op1 * op2) + op3 ==> -((op1 * op2) - op3)
1162 ;; The loongarch nmsub instructions implement -((op1 * op2) - op3)
1163 ;; This transformation means we may return the wrong signed zero
1164 ;; so we check HONOR_SIGNED_ZEROS.
1165
1166 ;; -a * b + c
1167 (define_insn "fnma<mode>4"
1168 [(set (match_operand:ANYF 0 "register_operand" "=f")
1169 (fma:ANYF (neg:ANYF (match_operand:ANYF 1 "register_operand" "f"))
1170 (match_operand:ANYF 2 "register_operand" "f")
1171 (match_operand:ANYF 3 "register_operand" "f")))]
1172 "!HONOR_SIGNED_ZEROS (<MODE>mode)"
1173 "fnmsub.<fmt>\t%0,%1,%2,%3"
1174 [(set_attr "type" "fmadd")
1175 (set_attr "mode" "<UNITMODE>")])
1176
1177 ;; fnms is defined as: (fma (neg op1) op2 (neg op3))
1178 ;; ((-op1) * op2) - op3 ==> -(op1 * op2) - op3 ==> -((op1 * op2) + op3)
1179 ;; The loongarch nmadd instructions implement -((op1 * op2) + op3)
1180 ;; This transformation means we may return the wrong signed zero
1181 ;; so we check HONOR_SIGNED_ZEROS.
1182
1183 ;; -a * b - c
1184 (define_insn "fnms<mode>4"
1185 [(set (match_operand:ANYF 0 "register_operand" "=f")
1186 (fma:ANYF
1187 (neg:ANYF (match_operand:ANYF 1 "register_operand" "f"))
1188 (match_operand:ANYF 2 "register_operand" "f")
1189 (neg:ANYF (match_operand:ANYF 3 "register_operand" "f"))))]
1190 "!HONOR_SIGNED_ZEROS (<MODE>mode)"
1191 "fnmadd.<fmt>\t%0,%1,%2,%3"
1192 [(set_attr "type" "fmadd")
1193 (set_attr "mode" "<UNITMODE>")])
1194
1195 ;; -(-a * b - c), modulo signed zeros
1196 (define_insn "*fma<mode>4"
1197 [(set (match_operand:ANYF 0 "register_operand" "=f")
1198 (neg:ANYF
1199 (fma:ANYF
1200 (neg:ANYF (match_operand:ANYF 1 "register_operand" " f"))
1201 (match_operand:ANYF 2 "register_operand" " f")
1202 (neg:ANYF (match_operand:ANYF 3 "register_operand" " f")))))]
1203 "!HONOR_SIGNED_ZEROS (<MODE>mode)"
1204 "fmadd.<fmt>\t%0,%1,%2,%3"
1205 [(set_attr "type" "fmadd")
1206 (set_attr "mode" "<UNITMODE>")])
1207
1208 ;; -(-a * b + c), modulo signed zeros
1209 (define_insn "*fms<mode>4"
1210 [(set (match_operand:ANYF 0 "register_operand" "=f")
1211 (neg:ANYF
1212 (fma:ANYF
1213 (neg:ANYF (match_operand:ANYF 1 "register_operand" " f"))
1214 (match_operand:ANYF 2 "register_operand" " f")
1215 (match_operand:ANYF 3 "register_operand" " f"))))]
1216 "!HONOR_SIGNED_ZEROS (<MODE>mode)"
1217 "fmsub.<fmt>\t%0,%1,%2,%3"
1218 [(set_attr "type" "fmadd")
1219 (set_attr "mode" "<UNITMODE>")])
1220
1221 ;; -(a * b + c)
1222 (define_insn "*fnms<mode>4"
1223 [(set (match_operand:ANYF 0 "register_operand" "=f")
1224 (neg:ANYF
1225 (fma:ANYF
1226 (match_operand:ANYF 1 "register_operand" " f")
1227 (match_operand:ANYF 2 "register_operand" " f")
1228 (match_operand:ANYF 3 "register_operand" " f"))))]
1229 ""
1230 "fnmadd.<fmt>\t%0,%1,%2,%3"
1231 [(set_attr "type" "fmadd")
1232 (set_attr "mode" "<UNITMODE>")])
1233
1234 ;; -(a * b - c)
1235 (define_insn "*fnma<mode>4"
1236 [(set (match_operand:ANYF 0 "register_operand" "=f")
1237 (neg:ANYF
1238 (fma:ANYF
1239 (match_operand:ANYF 1 "register_operand" " f")
1240 (match_operand:ANYF 2 "register_operand" " f")
1241 (neg:ANYF (match_operand:ANYF 3 "register_operand" " f")))))]
1242 ""
1243 "fnmsub.<fmt>\t%0,%1,%2,%3"
1244 [(set_attr "type" "fmadd")
1245 (set_attr "mode" "<UNITMODE>")])
1246
1247 ;;
1248 ;; ....................
1249 ;;
1250 ;; SQUARE ROOT
1251 ;;
1252 ;; ....................
1253
1254 (define_expand "sqrt<mode>2"
1255 [(set (match_operand:ANYF 0 "register_operand")
1256 (sqrt:ANYF (match_operand:ANYF 1 "register_operand")))]
1257 ""
1258 {
1259 if (<MODE>mode == SFmode
1260 && TARGET_RECIP_SQRT
1261 && flag_unsafe_math_optimizations
1262 && !optimize_insn_for_size_p ()
1263 && flag_finite_math_only && !flag_trapping_math)
1264 {
1265 loongarch_emit_swrsqrtsf (operands[0], operands[1], SFmode, 0);
1266 DONE;
1267 }
1268 })
1269
1270 (define_insn "*sqrt<mode>2"
1271 [(set (match_operand:ANYF 0 "register_operand" "=f")
1272 (sqrt:ANYF (match_operand:ANYF 1 "register_operand" "f")))]
1273 ""
1274 "fsqrt.<fmt>\t%0,%1"
1275 [(set_attr "type" "fsqrt")
1276 (set_attr "mode" "<UNITMODE>")
1277 (set_attr "insn_count" "1")])
1278
1279 (define_expand "rsqrt<mode>2"
1280 [(set (match_operand:ANYF 0 "register_operand")
1281 (unspec:ANYF [(match_operand:ANYF 1 "register_operand")]
1282 UNSPEC_RSQRT))]
1283 "TARGET_HARD_FLOAT"
1284 {
1285 if (<MODE>mode == SFmode && TARGET_RECIP_RSQRT)
1286 {
1287 loongarch_emit_swrsqrtsf (operands[0], operands[1], SFmode, 1);
1288 DONE;
1289 }
1290 })
1291
1292 (define_insn "*rsqrt<mode>2"
1293 [(set (match_operand:ANYF 0 "register_operand" "=f")
1294 (unspec:ANYF [(match_operand:ANYF 1 "register_operand" "f")]
1295 UNSPEC_RSQRT))]
1296 "TARGET_HARD_FLOAT"
1297 "frsqrt.<fmt>\t%0,%1"
1298 [(set_attr "type" "frsqrt")
1299 (set_attr "mode" "<UNITMODE>")])
1300
1301 ;; Approximate Reciprocal Square Root Instructions.
1302
1303 (define_insn "loongarch_frsqrte_<fmt>"
1304 [(set (match_operand:ANYF 0 "register_operand" "=f")
1305 (unspec:ANYF [(match_operand:ANYF 1 "register_operand" "f")]
1306 UNSPEC_RSQRTE))]
1307 "ISA_HAS_FRECIPE"
1308 "frsqrte.<fmt>\t%0,%1"
1309 [(set_attr "type" "frsqrte")
1310 (set_attr "mode" "<UNITMODE>")])
1311
1312 ;;
1313 ;; ....................
1314 ;;
1315 ;; ABSOLUTE VALUE
1316 ;;
1317 ;; ....................
1318
1319 (define_insn "abs<mode>2"
1320 [(set (match_operand:ANYF 0 "register_operand" "=f")
1321 (abs:ANYF (match_operand:ANYF 1 "register_operand" "f")))]
1322 ""
1323 "fabs.<fmt>\t%0,%1"
1324 [(set_attr "type" "fabs")
1325 (set_attr "mode" "<UNITMODE>")])
1326
1327 ;;
1328 ;; ....................
1329 ;;
1330 ;; FLOATING POINT COPYSIGN
1331 ;;
1332 ;; ....................
1333
1334 (define_insn "copysign<mode>3"
1335 [(set (match_operand:ANYF 0 "register_operand" "=f")
1336 (copysign:ANYF (match_operand:ANYF 1 "register_operand" "f")
1337 (match_operand:ANYF 2 "register_operand" "f")))]
1338 "TARGET_HARD_FLOAT"
1339 "fcopysign.<fmt>\t%0,%1,%2"
1340 [(set_attr "type" "fcopysign")
1341 (set_attr "mode" "<UNITMODE>")])
1342
1343 (define_expand "@xorsign<mode>3"
1344 [(match_operand:ANYF 0 "register_operand")
1345 (match_operand:ANYF 1 "register_operand")
1346 (match_operand:ANYF 2 "register_operand")]
1347 "ISA_HAS_LSX"
1348 {
1349 machine_mode lsx_mode
1350 = <MODE>mode == SFmode ? V4SFmode : V2DFmode;
1351 rtx tmp = gen_reg_rtx (lsx_mode);
1352 rtx op1 = force_lowpart_subreg (lsx_mode, operands[1], <MODE>mode);
1353 rtx op2 = force_lowpart_subreg (lsx_mode, operands[2], <MODE>mode);
1354 emit_insn (gen_xorsign3 (lsx_mode, tmp, op1, op2));
1355 emit_move_insn (operands[0],
1356 lowpart_subreg (<MODE>mode, tmp, lsx_mode));
1357 DONE;
1358 })
1359
1360 ;;
1361 ;; ....................
1362 ;;
1363 ;; FLOATING POINT SCALE
1364 ;;
1365 ;; ....................
1366
1367 (define_insn "ldexp<mode>3"
1368 [(set (match_operand:ANYF 0 "register_operand" "=f")
1369 (unspec:ANYF [(match_operand:ANYF 1 "register_operand" "f")
1370 (match_operand:<IMODE> 2 "register_operand" "f")]
1371 UNSPEC_FSCALEB))]
1372 "TARGET_HARD_FLOAT"
1373 "fscaleb.<fmt>\t%0,%1,%2"
1374 [(set_attr "type" "fscaleb")
1375 (set_attr "mode" "<UNITMODE>")])
1376
1377 ;;
1378 ;; ....................
1379 ;;
1380 ;; FLOATING POINT EXPONENT EXTRACT
1381 ;;
1382 ;; ....................
1383
1384 (define_insn "logb_non_negative<mode>2"
1385 [(set (match_operand:ANYF 0 "register_operand" "=f")
1386 (unspec:ANYF [(match_operand:ANYF 1 "register_operand" "f")]
1387 UNSPEC_FLOGB))]
1388 "TARGET_HARD_FLOAT"
1389 "flogb.<fmt>\t%0,%1"
1390 [(set_attr "type" "flogb")
1391 (set_attr "mode" "<UNITMODE>")])
1392
1393 (define_expand "logb<mode>2"
1394 [(set (match_operand:ANYF 0 "register_operand")
1395 (unspec:ANYF [(abs:ANYF (match_operand:ANYF 1 "register_operand"))]
1396 UNSPEC_FLOGB))]
1397 "TARGET_HARD_FLOAT"
1398 {
1399 rtx tmp = gen_reg_rtx (<MODE>mode);
1400
1401 emit_insn (gen_abs<mode>2 (tmp, operands[1]));
1402 emit_insn (gen_logb_non_negative<mode>2 (operands[0], tmp));
1403 DONE;
1404 })
1405
1406 ;;
1407 ;; ...................
1408 ;;
1409 ;; Count leading zeroes.
1410 ;;
1411 ;; ...................
1412 ;;
1413
1414 (define_insn "clz<mode>2"
1415 [(set (match_operand:GPR 0 "register_operand" "=r")
1416 (clz:GPR (match_operand:GPR 1 "register_operand" "r")))]
1417 ""
1418 "clz.<d>\t%0,%1"
1419 [(set_attr "type" "clz")
1420 (set_attr "mode" "<MODE>")])
1421
1422 ;;
1423 ;; ...................
1424 ;;
1425 ;; Count trailing zeroes.
1426 ;;
1427 ;; ...................
1428 ;;
1429
1430 (define_insn "ctz<mode>2"
1431 [(set (match_operand:GPR 0 "register_operand" "=r")
1432 (ctz:GPR (match_operand:GPR 1 "register_operand" "r")))]
1433 ""
1434 "ctz.<d>\t%0,%1"
1435 [(set_attr "type" "clz")
1436 (set_attr "mode" "<MODE>")])
1437
1438 ;;
1439 ;; ....................
1440 ;;
1441 ;; MIN/MAX
1442 ;;
1443 ;; ....................
1444
1445 (define_insn "smax<mode>3"
1446 [(set (match_operand:ANYF 0 "register_operand" "=f")
1447 (smax:ANYF (match_operand:ANYF 1 "register_operand" "f")
1448 (match_operand:ANYF 2 "register_operand" "f")))]
1449 ""
1450 "fmax.<fmt>\t%0,%1,%2"
1451 [(set_attr "type" "fmove")
1452 (set_attr "mode" "<MODE>")])
1453
1454 (define_insn "smin<mode>3"
1455 [(set (match_operand:ANYF 0 "register_operand" "=f")
1456 (smin:ANYF (match_operand:ANYF 1 "register_operand" "f")
1457 (match_operand:ANYF 2 "register_operand" "f")))]
1458 ""
1459 "fmin.<fmt>\t%0,%1,%2"
1460 [(set_attr "type" "fmove")
1461 (set_attr "mode" "<MODE>")])
1462
1463 (define_insn "fmax<mode>3"
1464 [(set (match_operand:ANYF 0 "register_operand" "=f")
1465 (unspec:ANYF [(use (match_operand:ANYF 1 "register_operand" "f"))
1466 (use (match_operand:ANYF 2 "register_operand" "f"))]
1467 UNSPEC_FMAX))]
1468 ""
1469 "fmax.<fmt>\t%0,%1,%2"
1470 [(set_attr "type" "fmove")
1471 (set_attr "mode" "<MODE>")])
1472
1473 (define_insn "fmin<mode>3"
1474 [(set (match_operand:ANYF 0 "register_operand" "=f")
1475 (unspec:ANYF [(use (match_operand:ANYF 1 "register_operand" "f"))
1476 (use (match_operand:ANYF 2 "register_operand" "f"))]
1477 UNSPEC_FMIN))]
1478 ""
1479 "fmin.<fmt>\t%0,%1,%2"
1480 [(set_attr "type" "fmove")
1481 (set_attr "mode" "<MODE>")])
1482
1483 (define_insn "smaxa<mode>3"
1484 [(set (match_operand:ANYF 0 "register_operand" "=f")
1485 (if_then_else:ANYF
1486 (gt (abs:ANYF (match_operand:ANYF 1 "register_operand" "f"))
1487 (abs:ANYF (match_operand:ANYF 2 "register_operand" "f")))
1488 (match_dup 1)
1489 (match_dup 2)))]
1490 ""
1491 "fmaxa.<fmt>\t%0,%1,%2"
1492 [(set_attr "type" "fmove")
1493 (set_attr "mode" "<MODE>")])
1494
1495 (define_insn "smina<mode>3"
1496 [(set (match_operand:ANYF 0 "register_operand" "=f")
1497 (if_then_else:ANYF
1498 (lt (abs:ANYF (match_operand:ANYF 1 "register_operand" "f"))
1499 (abs:ANYF (match_operand:ANYF 2 "register_operand" "f")))
1500 (match_dup 1)
1501 (match_dup 2)))]
1502 ""
1503 "fmina.<fmt>\t%0,%1,%2"
1504 [(set_attr "type" "fmove")
1505 (set_attr "mode" "<MODE>")])
1506
1507 ;;
1508 ;; ....................
1509 ;;
1510 ;; NEGATION and ONE'S COMPLEMENT
1511 ;;
1512 ;; ....................
1513
1514 (define_insn "neg<mode>2"
1515 [(set (match_operand:GPR 0 "register_operand" "=r")
1516 (neg:GPR (match_operand:GPR 1 "register_operand" "r")))]
1517 ""
1518 "sub.<d>\t%0,%.,%1"
1519 [(set_attr "alu_type" "sub")
1520 (set_attr "mode" "<MODE>")])
1521
1522 (define_insn "*negsi2_extended"
1523 [(set (match_operand:DI 0 "register_operand" "=r")
1524 (sign_extend:DI (neg:SI (match_operand:SI 1 "register_operand" "r"))))]
1525 "TARGET_64BIT"
1526 "sub.w\t%0,%.,%1"
1527 [(set_attr "alu_type" "sub")
1528 (set_attr "mode" "SI")])
1529
1530 (define_insn "neg<mode>2"
1531 [(set (match_operand:ANYF 0 "register_operand" "=f")
1532 (neg:ANYF (match_operand:ANYF 1 "register_operand" "f")))]
1533 ""
1534 "fneg.<fmt>\t%0,%1"
1535 [(set_attr "type" "fneg")
1536 (set_attr "mode" "<UNITMODE>")])
1537
1538
1539 ;;
1540 ;; ....................
1541 ;;
1542 ;; LOGICAL
1543 ;;
1544 ;; ....................
1545 ;;
1546
1547 (define_insn "*<optab><mode>3"
1548 [(set (match_operand:GPR 0 "register_operand" "=r,r")
1549 (any_or:GPR (match_operand:GPR 1 "register_operand" "%r,r")
1550 (match_operand:GPR 2 "uns_arith_operand" "r,K")))]
1551 ""
1552 "<insn>%i2\t%0,%1,%2"
1553 [(set_attr "type" "logical")
1554 (set_attr "mode" "<MODE>")])
1555
1556 (define_insn "*and<mode>3"
1557 [(set (match_operand:GPR 0 "register_operand" "=r,r,r,r")
1558 (and:GPR (match_operand:GPR 1 "register_operand" "%r,r,r,0")
1559 (match_operand:GPR 2 "and_operand" "r,K,Yx,Yy")))]
1560 ""
1561 "@
1562 and\t%0,%1,%2
1563 andi\t%0,%1,%2
1564 * operands[2] = GEN_INT (INTVAL (operands[2]) \
1565 & GET_MODE_MASK (<MODE>mode)); \
1566 return \"bstrpick.<d>\t%0,%1,%M2\";
1567 * operands[2] = GEN_INT (~INTVAL (operands[2]) \
1568 & GET_MODE_MASK (<MODE>mode)); \
1569 return \"bstrins.<d>\t%0,%.,%M2\";"
1570 [(set_attr "move_type" "logical,logical,pick_ins,pick_ins")
1571 (set_attr "mode" "<MODE>")])
1572
1573 (define_expand "<optab><mode>3"
1574 [(set (match_operand:X 0 "register_operand")
1575 (any_bitwise:X (match_operand:X 1 "register_operand")
1576 (match_operand:X 2 "<bitwise_operand>")))]
1577 "")
1578
1579 (define_insn "one_cmpl<mode>2"
1580 [(set (match_operand:X 0 "register_operand" "=r")
1581 (not:X (match_operand:X 1 "register_operand" "r")))]
1582 ""
1583 "nor\t%0,%.,%1"
1584 [(set_attr "alu_type" "not")
1585 (set_attr "mode" "<MODE>")])
1586
1587 (define_insn "*one_cmplsi2_internal"
1588 [(set (match_operand:SI 0 "register_operand" "=r")
1589 (not:SI (match_operand:SI 1 "register_operand" " r")))]
1590 "TARGET_64BIT"
1591 "nor\t%0,%.,%1"
1592 [(set_attr "type" "logical")
1593 (set_attr "mode" "SI")])
1594
1595 (define_insn_and_split "*bstrins_<mode>_for_ior_mask"
1596 [(set (match_operand:GPR 0 "register_operand" "=r")
1597 (any_or_plus:GPR
1598 (and:GPR (match_operand:GPR 1 "register_operand" "r")
1599 (match_operand:GPR 2 "const_int_operand" "i"))
1600 (and:GPR (match_operand:GPR 3 "register_operand" "r")
1601 (match_operand:GPR 4 "const_int_operand" "i"))))]
1602 "loongarch_pre_reload_split ()
1603 && loongarch_use_bstrins_for_ior_with_mask (<MODE>mode, operands)"
1604 "#"
1605 "&& true"
1606 [(set (match_dup 0) (match_dup 1))
1607 (set (zero_extract:GPR (match_dup 0) (match_dup 2) (match_dup 4))
1608 (match_dup 3))]
1609 {
1610 if (loongarch_use_bstrins_for_ior_with_mask (<MODE>mode, operands) < 0)
1611 {
1612 std::swap (operands[1], operands[3]);
1613 std::swap (operands[2], operands[4]);
1614 }
1615
1616 unsigned HOST_WIDE_INT mask = ~UINTVAL (operands[2]);
1617 int lo = ffs_hwi (mask) - 1;
1618 int len = low_bitmask_len (<MODE>mode, mask >> lo);
1619
1620 len = MIN (len, GET_MODE_BITSIZE (<MODE>mode) - lo);
1621 operands[2] = GEN_INT (len);
1622 operands[4] = GEN_INT (lo);
1623
1624 /* Use a new pseudo register even if lo == 0 or we'll wreck havoc
1625 when operands[0] is same as operands[3]. See PR 121906. */
1626 rtx tmp = gen_reg_rtx (<MODE>mode);
1627 rtx val = lo ? gen_rtx_ASHIFTRT (<MODE>mode, operands[3], GEN_INT (lo))
1628 : operands[3];
1629 emit_move_insn (tmp, val);
1630 operands[3] = tmp;
1631 })
1632
1633 ;; We always avoid the shift operation in bstrins_<mode>_for_ior_mask
1634 ;; if possible, but the result may be sub-optimal when one of the masks
1635 ;; is (1 << N) - 1 and one of the src register is the dest register.
1636 ;; For example:
1637 ;; move t0, a0
1638 ;; move a0, a1
1639 ;; bstrins.d a0, t0, 42, 0
1640 ;; ret
1641 ;; using a shift operation would be better:
1642 ;; srai.d t0, a1, 43
1643 ;; bstrins.d a0, t0, 63, 43
1644 ;; ret
1645 ;; unfortunately we cannot figure it out in split1: before reload we cannot
1646 ;; know if the dest register is one of the src register. Fix it up in
1647 ;; peephole2.
1648 (define_peephole2
1649 [(set (match_operand:GPR 0 "register_operand")
1650 (match_operand:GPR 1 "register_operand"))
1651 (set (match_dup 1) (match_operand:GPR 2 "register_operand"))
1652 (set (zero_extract:GPR (match_dup 1)
1653 (match_operand:SI 3 "const_int_operand")
1654 (const_int 0))
1655 (match_dup 0))]
1656 "peep2_reg_dead_p (3, operands[0])"
1657 [(const_int 0)]
1658 {
1659 int len = GET_MODE_BITSIZE (<MODE>mode) - INTVAL (operands[3]);
1660
1661 emit_insn (gen_ashr<mode>3 (operands[0], operands[2], operands[3]));
1662 emit_insn (gen_insv<mode> (operands[1], GEN_INT (len), operands[3],
1663 operands[0]));
1664 DONE;
1665 })
1666
1667 (define_insn "*iorhi3"
1668 [(set (match_operand:HI 0 "register_operand" "=r,r")
1669 (ior:HI (match_operand:HI 1 "register_operand" "%r,r")
1670 (match_operand:HI 2 "uns_arith_operand" "r,K")))]
1671 ""
1672 "or%i2\t%0,%1,%2"
1673 [(set_attr "type" "logical")
1674 (set_attr "mode" "HI")])
1675
1676 (define_insn "nor<mode>3"
1677 [(set (match_operand:X 0 "register_operand" "=r")
1678 (and:X (not:X (match_operand:X 1 "register_operand" "%r"))
1679 (not:X (match_operand:X 2 "register_operand" "r"))))]
1680 ""
1681 "nor\t%0,%1,%2"
1682 [(set_attr "type" "logical")
1683 (set_attr "mode" "<MODE>")])
1684
1685 (define_insn "*norsi3_internal"
1686 [(set (match_operand:SI 0 "register_operand" "=r")
1687 (and:SI (not:SI (match_operand:SI 1 "register_operand" "%r"))
1688 (not:SI (match_operand:SI 2 "register_operand" "r"))))]
1689 "TARGET_64BIT"
1690 "nor\t%0,%1,%2"
1691 [(set_attr "type" "logical")
1692 (set_attr "mode" "SI")])
1693
1694 (define_insn "<optab>n<mode>3"
1695 [(set (match_operand:X 0 "register_operand" "=r")
1696 (neg_bitwise:X
1697 (not:X (match_operand:X 2 "register_operand" "r"))
1698 (match_operand:X 1 "register_operand" "r")))]
1699 ""
1700 "<insn>n\t%0,%1,%2"
1701 [(set_attr "type" "logical")
1702 (set_attr "mode" "<MODE>")])
1703
1704 (define_insn "*<optab>nsi_internal"
1705 [(set (match_operand:SI 0 "register_operand" "=r")
1706 (neg_bitwise:SI
1707 (not:SI (match_operand:SI 1 "register_operand" "r"))
1708 (match_operand:SI 2 "register_operand" "r")))]
1709 "TARGET_64BIT"
1710 "<insn>n\t%0,%2,%1"
1711 [(set_attr "type" "logical")
1712 (set_attr "mode" "SI")])
1713
1714 ;;
1715 ;; ....................
1716 ;;
1717 ;; TRUNCATION
1718 ;;
1719 ;; ....................
1720
1721 (define_insn "truncdfsf2"
1722 [(set (match_operand:SF 0 "register_operand" "=f")
1723 (float_truncate:SF (match_operand:DF 1 "register_operand" "f")))]
1724 "TARGET_DOUBLE_FLOAT"
1725 "fcvt.s.d\t%0,%1"
1726 [(set_attr "type" "fcvt")
1727 (set_attr "cnv_mode" "D2S")
1728 (set_attr "mode" "SF")])
1729
1730 ;; In vector registers, popcount can be implemented directly through
1731 ;; the vector instruction [X]VPCNT. For GP registers, we can implement
1732 ;; it through the following method. Compared with loop implementation
1733 ;; of popcount, the following method has better performance.
1734
1735 ;; This attribute used for get connection of scalar mode and corresponding
1736 ;; vector mode.
1737 (define_mode_attr cntmap [(SI "v4si") (DI "v2di")])
1738
1739 (define_expand "popcount<mode>2"
1740 [(set (match_operand:GPR 0 "register_operand")
1741 (popcount:GPR (match_operand:GPR 1 "register_operand")))]
1742 "ISA_HAS_LSX"
1743 {
1744 rtx in = operands[1];
1745 rtx out = operands[0];
1746 rtx vreg = <MODE>mode == SImode ? gen_reg_rtx (V4SImode) :
1747 gen_reg_rtx (V2DImode);
1748 emit_insn (gen_lsx_vinsgr2vr_<size> (vreg, in, vreg, GEN_INT (1)));
1749 emit_insn (gen_popcount<cntmap>2 (vreg, vreg));
1750 emit_insn (gen_lsx_vpickve2gr_<size> (out, vreg, GEN_INT (0)));
1751 DONE;
1752 })
1753
1754 ;;
1755 ;; ....................
1756 ;;
1757 ;; ZERO EXTENSION
1758 ;;
1759 ;; ....................
1760 (define_expand "zero_extendsidi2"
1761 [(set (match_operand:DI 0 "register_operand")
1762 (zero_extend:DI (match_operand:SI 1 "nonimmediate_operand")))]
1763 "TARGET_64BIT")
1764
1765 (define_insn_and_split "*zero_extendsidi2_internal"
1766 [(set (match_operand:DI 0 "register_operand" "=r,r,r,r")
1767 (zero_extend:DI (match_operand:SI 1 "nonimmediate_operand" "r,m,ZC,k")))]
1768 "TARGET_64BIT"
1769 "@
1770 bstrpick.d\t%0,%1,31,0
1771 ld.wu\t%0,%1
1772 #
1773 ldx.wu\t%0,%1"
1774 "&& reload_completed
1775 && MEM_P (operands[1])
1776 && (loongarch_14bit_shifted_offset_address_p (XEXP (operands[1], 0), SImode)
1777 && !loongarch_12bit_offset_address_p (XEXP (operands[1], 0), SImode))
1778 && !paradoxical_subreg_p (operands[0])"
1779 [(set (match_dup 3) (match_dup 1))
1780 (set (match_dup 0)
1781 (ior:DI (zero_extend:DI
1782 (subreg:SI (match_dup 0) 0))
1783 (match_dup 2)))]
1784 {
1785 operands[1] = gen_lowpart (SImode, operands[1]);
1786 operands[3] = gen_lowpart (SImode, operands[0]);
1787 operands[2] = const0_rtx;
1788 }
1789 [(set_attr "move_type" "arith,load,load,load")
1790 (set_attr "mode" "DI")])
1791
1792 (define_insn "zero_extend<SHORT:mode><GPR:mode>2"
1793 [(set (match_operand:GPR 0 "register_operand" "=r,r,r")
1794 (zero_extend:GPR
1795 (match_operand:SHORT 1 "nonimmediate_operand" "r,m,k")))]
1796 ""
1797 "@
1798 bstrpick.w\t%0,%1,<SHORT:7_or_15>,0
1799 ld.<SHORT:size>u\t%0,%1
1800 ldx.<SHORT:size>u\t%0,%1"
1801 [(set_attr "move_type" "pick_ins,load,load")
1802 (set_attr "mode" "<GPR:MODE>")])
1803
1804 (define_insn "zero_extendqihi2"
1805 [(set (match_operand:HI 0 "register_operand" "=r,r,r")
1806 (zero_extend:HI (match_operand:QI 1 "nonimmediate_operand" "r,k,m")))]
1807 ""
1808 "@
1809 andi\t%0,%1,0xff
1810 ldx.bu\t%0,%1
1811 ld.bu\t%0,%1"
1812 [(set_attr "move_type" "andi,load,load")
1813 (set_attr "mode" "HI")])
1814
1815 ;; Combiner patterns to optimize truncate/zero_extend combinations.
1816
1817 (define_insn "*zero_extend<GPR:mode>_trunc<SHORT:mode>"
1818 [(set (match_operand:GPR 0 "register_operand" "=r")
1819 (zero_extend:GPR
1820 (truncate:SHORT (match_operand:DI 1 "register_operand" "r"))))]
1821 "TARGET_64BIT"
1822 "bstrpick.w\t%0,%1,<SHORT:7_or_15>,0"
1823 [(set_attr "move_type" "pick_ins")
1824 (set_attr "mode" "<GPR:MODE>")])
1825
1826 (define_insn "*zero_extendhi_truncqi"
1827 [(set (match_operand:HI 0 "register_operand" "=r")
1828 (zero_extend:HI
1829 (truncate:QI (match_operand:DI 1 "register_operand" "r"))))]
1830 "TARGET_64BIT"
1831 "andi\t%0,%1,0xff"
1832 [(set_attr "alu_type" "and")
1833 (set_attr "mode" "HI")])
1834
1835 ;;
1836 ;; ....................
1837 ;;
1838 ;; SIGN EXTENSION
1839 ;;
1840 ;; ....................
1841
1842 (define_insn "extendsidi2"
1843 [(set (match_operand:DI 0 "register_operand" "=r,r,r,r,r")
1844 (sign_extend:DI
1845 (match_operand:SI 1 "nonimmediate_operand" "r,ZC,m,k,f")))]
1846 "TARGET_64BIT"
1847 "@
1848 slli.w\t%0,%1,0
1849 ldptr.w\t%0,%1
1850 ld.w\t%0,%1
1851 ldx.w\t%0,%1
1852 movfr2gr.s\t%0,%1"
1853 [(set_attr "move_type" "sll0,load,load,load,mftg")
1854 (set_attr "mode" "DI")])
1855
1856 (define_insn "extend<SHORT:mode><GPR:mode>2"
1857 [(set (match_operand:GPR 0 "register_operand" "=r,r,r")
1858 (sign_extend:GPR
1859 (match_operand:SHORT 1 "nonimmediate_operand" "r,m,k")))]
1860 ""
1861 "@
1862 ext.w.<SHORT:size>\t%0,%1
1863 ld.<SHORT:size>\t%0,%1
1864 ldx.<SHORT:size>\t%0,%1"
1865 [(set_attr "move_type" "signext,load,load")
1866 (set_attr "mode" "<GPR:MODE>")])
1867
1868 (define_insn "extendqihi2"
1869 [(set (match_operand:HI 0 "register_operand" "=r,r,r")
1870 (sign_extend:HI
1871 (match_operand:QI 1 "nonimmediate_operand" "r,m,k")))]
1872 ""
1873 "@
1874 ext.w.b\t%0,%1
1875 ld.b\t%0,%1
1876 ldx.b\t%0,%1"
1877 [(set_attr "move_type" "signext,load,load")
1878 (set_attr "mode" "SI")])
1879
1880 (define_insn "extendsfdf2"
1881 [(set (match_operand:DF 0 "register_operand" "=f")
1882 (float_extend:DF (match_operand:SF 1 "register_operand" "f")))]
1883 "TARGET_DOUBLE_FLOAT"
1884 "fcvt.d.s\t%0,%1"
1885 [(set_attr "type" "fcvt")
1886 (set_attr "cnv_mode" "S2D")
1887 (set_attr "mode" "DF")])
1888
1889 ;;
1890 ;; ....................
1891 ;;
1892 ;; CONVERSIONS
1893 ;;
1894 ;; ....................
1895
1896 ;; conversion of a floating-point value to a integer
1897
1898 (define_insn "fix_trunc<ANYF:mode><GPR:mode>2"
1899 [(set (match_operand:GPR 0 "register_operand" "=f")
1900 (fix:GPR (match_operand:ANYF 1 "register_operand" "f")))]
1901 ""
1902 "ftintrz.<GPR:ifmt>.<ANYF:fmt> %0,%1"
1903 [(set_attr "type" "fcvt")
1904 (set_attr "mode" "<ANYF:MODE>")])
1905
1906 ;; conversion of an integeral (or boolean) value to a floating-point value
1907
1908 (define_insn "floatsidf2"
1909 [(set (match_operand:DF 0 "register_operand" "=f")
1910 (float:DF (match_operand:SI 1 "register_operand" "f")))]
1911 "TARGET_DOUBLE_FLOAT"
1912 "ffint.d.w\t%0,%1"
1913 [(set_attr "type" "fcvt")
1914 (set_attr "mode" "DF")
1915 (set_attr "cnv_mode" "I2D")])
1916
1917 (define_insn "floatdidf2"
1918 [(set (match_operand:DF 0 "register_operand" "=f")
1919 (float:DF (match_operand:DI 1 "register_operand" "f")))]
1920 "TARGET_DOUBLE_FLOAT"
1921 "ffint.d.l\t%0,%1"
1922 [(set_attr "type" "fcvt")
1923 (set_attr "mode" "DF")
1924 (set_attr "cnv_mode" "I2D")])
1925
1926 (define_insn "floatsisf2"
1927 [(set (match_operand:SF 0 "register_operand" "=f")
1928 (float:SF (match_operand:SI 1 "register_operand" "f")))]
1929 "TARGET_HARD_FLOAT"
1930 "ffint.s.w\t%0,%1"
1931 [(set_attr "type" "fcvt")
1932 (set_attr "mode" "SF")
1933 (set_attr "cnv_mode" "I2S")])
1934
1935 (define_insn "floatdisf2"
1936 [(set (match_operand:SF 0 "register_operand" "=f")
1937 (float:SF (match_operand:DI 1 "register_operand" "f")))]
1938 "TARGET_DOUBLE_FLOAT"
1939 "ffint.s.l\t%0,%1"
1940 [(set_attr "type" "fcvt")
1941 (set_attr "mode" "SF")
1942 (set_attr "cnv_mode" "I2S")])
1943
1944 ;; Convert a floating-point value to an unsigned integer.
1945
1946 (define_expand "fixuns_truncdfsi2"
1947 [(set (match_operand:SI 0 "register_operand")
1948 (unsigned_fix:SI (match_operand:DF 1 "register_operand")))]
1949 "TARGET_DOUBLE_FLOAT"
1950 {
1951 rtx reg1 = gen_reg_rtx (DFmode);
1952 rtx reg2 = gen_reg_rtx (DFmode);
1953 rtx reg3 = gen_reg_rtx (SImode);
1954 rtx_code_label *label1 = gen_label_rtx ();
1955 rtx_code_label *label2 = gen_label_rtx ();
1956 rtx test;
1957 REAL_VALUE_TYPE offset;
1958
1959 real_2expN (&offset, 31, DFmode);
1960
1961 loongarch_emit_move (reg1,
1962 const_double_from_real_value (offset, DFmode));
1963 do_pending_stack_adjust ();
1964
1965 test = gen_rtx_GE (VOIDmode, operands[1], reg1);
1966 emit_jump_insn (gen_cbranchdf4 (test, operands[1], reg1, label1));
1967
1968 emit_insn (gen_fix_truncdfsi2 (operands[0], operands[1]));
1969 emit_jump_insn (gen_rtx_SET (pc_rtx,
1970 gen_rtx_LABEL_REF (VOIDmode, label2)));
1971 emit_barrier ();
1972
1973 emit_label (label1);
1974 loongarch_emit_move (reg2, gen_rtx_MINUS (DFmode, operands[1], reg1));
1975 loongarch_emit_move (reg3, GEN_INT (trunc_int_for_mode
1976 (BITMASK_HIGH, SImode)));
1977
1978 emit_insn (gen_fix_truncdfsi2 (operands[0], reg2));
1979 emit_insn (gen_iorsi3 (operands[0], operands[0], reg3));
1980
1981 emit_label (label2);
1982
1983 /* Allow REG_NOTES to be set on last insn (labels don't have enough
1984 fields, and can't be used for REG_NOTES anyway). */
1985 emit_use (stack_pointer_rtx);
1986 DONE;
1987 })
1988
1989 (define_expand "fixuns_truncdfdi2"
1990 [(set (match_operand:DI 0 "register_operand")
1991 (unsigned_fix:DI (match_operand:DF 1 "register_operand")))]
1992 "TARGET_DOUBLE_FLOAT"
1993 {
1994 rtx reg1 = gen_reg_rtx (DFmode);
1995 rtx reg2 = gen_reg_rtx (DFmode);
1996 rtx reg3 = gen_reg_rtx (DImode);
1997 rtx_code_label *label1 = gen_label_rtx ();
1998 rtx_code_label *label2 = gen_label_rtx ();
1999 rtx test;
2000 REAL_VALUE_TYPE offset;
2001
2002 real_2expN (&offset, 63, DFmode);
2003
2004 loongarch_emit_move (reg1, const_double_from_real_value (offset, DFmode));
2005 do_pending_stack_adjust ();
2006
2007 test = gen_rtx_GE (VOIDmode, operands[1], reg1);
2008 emit_jump_insn (gen_cbranchdf4 (test, operands[1], reg1, label1));
2009
2010 emit_insn (gen_fix_truncdfdi2 (operands[0], operands[1]));
2011 emit_jump_insn (gen_rtx_SET (pc_rtx, gen_rtx_LABEL_REF (VOIDmode, label2)));
2012 emit_barrier ();
2013
2014 emit_label (label1);
2015 loongarch_emit_move (reg2, gen_rtx_MINUS (DFmode, operands[1], reg1));
2016 loongarch_emit_move (reg3, GEN_INT (BITMASK_HIGH));
2017 emit_insn (gen_ashldi3 (reg3, reg3, GEN_INT (32)));
2018
2019 emit_insn (gen_fix_truncdfdi2 (operands[0], reg2));
2020 emit_insn (gen_iordi3 (operands[0], operands[0], reg3));
2021
2022 emit_label (label2);
2023
2024 /* Allow REG_NOTES to be set on last insn (labels don't have enough
2025 fields, and can't be used for REG_NOTES anyway). */
2026 emit_use (stack_pointer_rtx);
2027 DONE;
2028 })
2029
2030 (define_expand "fixuns_truncsfsi2"
2031 [(set (match_operand:SI 0 "register_operand")
2032 (unsigned_fix:SI (match_operand:SF 1 "register_operand")))]
2033 "TARGET_HARD_FLOAT"
2034 {
2035 rtx reg1 = gen_reg_rtx (SFmode);
2036 rtx reg2 = gen_reg_rtx (SFmode);
2037 rtx reg3 = gen_reg_rtx (SImode);
2038 rtx_code_label *label1 = gen_label_rtx ();
2039 rtx_code_label *label2 = gen_label_rtx ();
2040 rtx test;
2041 REAL_VALUE_TYPE offset;
2042
2043 real_2expN (&offset, 31, SFmode);
2044
2045 loongarch_emit_move (reg1, const_double_from_real_value (offset, SFmode));
2046 do_pending_stack_adjust ();
2047
2048 test = gen_rtx_GE (VOIDmode, operands[1], reg1);
2049 emit_jump_insn (gen_cbranchsf4 (test, operands[1], reg1, label1));
2050
2051 emit_insn (gen_fix_truncsfsi2 (operands[0], operands[1]));
2052 emit_jump_insn (gen_rtx_SET (pc_rtx, gen_rtx_LABEL_REF (VOIDmode, label2)));
2053 emit_barrier ();
2054
2055 emit_label (label1);
2056 loongarch_emit_move (reg2, gen_rtx_MINUS (SFmode, operands[1], reg1));
2057 loongarch_emit_move (reg3, GEN_INT (trunc_int_for_mode
2058 (BITMASK_HIGH, SImode)));
2059
2060 emit_insn (gen_fix_truncsfsi2 (operands[0], reg2));
2061 emit_insn (gen_iorsi3 (operands[0], operands[0], reg3));
2062
2063 emit_label (label2);
2064
2065 /* Allow REG_NOTES to be set on last insn (labels don't have enough
2066 fields, and can't be used for REG_NOTES anyway). */
2067 emit_use (stack_pointer_rtx);
2068 DONE;
2069 })
2070
2071 (define_expand "fixuns_truncsfdi2"
2072 [(set (match_operand:DI 0 "register_operand")
2073 (unsigned_fix:DI (match_operand:SF 1 "register_operand")))]
2074 "TARGET_DOUBLE_FLOAT"
2075 {
2076 rtx reg1 = gen_reg_rtx (SFmode);
2077 rtx reg2 = gen_reg_rtx (SFmode);
2078 rtx reg3 = gen_reg_rtx (DImode);
2079 rtx_code_label *label1 = gen_label_rtx ();
2080 rtx_code_label *label2 = gen_label_rtx ();
2081 rtx test;
2082 REAL_VALUE_TYPE offset;
2083
2084 real_2expN (&offset, 63, SFmode);
2085
2086 loongarch_emit_move (reg1, const_double_from_real_value (offset, SFmode));
2087 do_pending_stack_adjust ();
2088
2089 test = gen_rtx_GE (VOIDmode, operands[1], reg1);
2090 emit_jump_insn (gen_cbranchsf4 (test, operands[1], reg1, label1));
2091
2092 emit_insn (gen_fix_truncsfdi2 (operands[0], operands[1]));
2093 emit_jump_insn (gen_rtx_SET (pc_rtx, gen_rtx_LABEL_REF (VOIDmode, label2)));
2094 emit_barrier ();
2095
2096 emit_label (label1);
2097 loongarch_emit_move (reg2, gen_rtx_MINUS (SFmode, operands[1], reg1));
2098 loongarch_emit_move (reg3, GEN_INT (BITMASK_HIGH));
2099 emit_insn (gen_ashldi3 (reg3, reg3, GEN_INT (32)));
2100
2101 emit_insn (gen_fix_truncsfdi2 (operands[0], reg2));
2102 emit_insn (gen_iordi3 (operands[0], operands[0], reg3));
2103
2104 emit_label (label2);
2105
2106 /* Allow REG_NOTES to be set on last insn (labels don't have enough
2107 fields, and can't be used for REG_NOTES anyway). */
2108 emit_use (stack_pointer_rtx);
2109 DONE;
2110 })
2111
2112 ;;
2113 ;; ....................
2114 ;;
2115 ;; EXTRACT AND INSERT
2116 ;;
2117 ;; ....................
2118
2119 (define_expand "extzv<mode>"
2120 [(set (match_operand:X 0 "register_operand")
2121 (zero_extract:X (match_operand:X 1 "register_operand")
2122 (match_operand 2 "const_int_operand")
2123 (match_operand 3 "const_int_operand")))]
2124 ""
2125 {
2126 if (!loongarch_use_ins_ext_p (operands[1], INTVAL (operands[2]),
2127 INTVAL (operands[3])))
2128 FAIL;
2129 })
2130
2131 (define_insn "*extzv<mode>"
2132 [(set (match_operand:X 0 "register_operand" "=r")
2133 (zero_extract:X (match_operand:X 1 "register_operand" "r")
2134 (match_operand 2 "const_int_operand" "")
2135 (match_operand 3 "const_int_operand" "")))]
2136 "loongarch_use_ins_ext_p (operands[1], INTVAL (operands[2]),
2137 INTVAL (operands[3]))"
2138 {
2139 operands[2] = GEN_INT (INTVAL (operands[2]) + INTVAL (operands[3]) - 1);
2140 return "bstrpick.<d>\t%0,%1,%2,%3";
2141 }
2142 [(set_attr "type" "arith")
2143 (set_attr "mode" "<MODE>")])
2144
2145 (define_expand "insv<mode>"
2146 [(set (zero_extract:GPR (match_operand:GPR 0 "register_operand")
2147 (match_operand 1 "const_int_operand")
2148 (match_operand 2 "const_int_operand"))
2149 (match_operand:GPR 3 "reg_or_0_operand"))]
2150 ""
2151 {
2152 if (!loongarch_use_ins_ext_p (operands[0], INTVAL (operands[1]),
2153 INTVAL (operands[2])))
2154 FAIL;
2155 })
2156
2157 (define_insn "*insv<mode>"
2158 [(set (zero_extract:GPR (match_operand:GPR 0 "register_operand" "+r")
2159 (match_operand:SI 1 "const_int_operand" "")
2160 (match_operand:SI 2 "const_int_operand" ""))
2161 (match_operand:GPR 3 "reg_or_0_operand" "rJ"))]
2162 "loongarch_use_ins_ext_p (operands[0], INTVAL (operands[1]),
2163 INTVAL (operands[2]))"
2164 {
2165 operands[1] = GEN_INT (INTVAL (operands[1]) + INTVAL (operands[2]) - 1);
2166 return "bstrins.<d>\t%0,%z3,%1,%2";
2167 }
2168 [(set_attr "type" "arith")
2169 (set_attr "mode" "<MODE>")])
2170
2171 ;;
2172 ;; ....................
2173 ;;
2174 ;; DATA MOVEMENT
2175 ;;
2176 ;; ....................
2177
2178 ;; 64-bit integer moves
2179
2180 ;; Unlike most other insns, the move insns can't be split with
2181 ;; different predicates, because register spilling and other parts of
2182 ;; the compiler, have memoized the insn number already.
2183
2184 (define_expand "movdi"
2185 [(set (match_operand:DI 0 "")
2186 (match_operand:DI 1 ""))]
2187 ""
2188 {
2189 if (loongarch_legitimize_move (DImode, operands[0], operands[1]))
2190 DONE;
2191 })
2192
2193 (define_insn_and_split "*movdi_32bit"
2194 [(set (match_operand:DI 0 "nonimmediate_operand" "=r,r,r,w,*f,*f,*r,*m")
2195 (match_operand:DI 1 "move_operand" "r,i,w,r,*J*r,*m,*f,*f"))]
2196 "!TARGET_64BIT
2197 && (register_operand (operands[0], DImode)
2198 || reg_or_0_operand (operands[1], DImode))"
2199 { return loongarch_output_move (operands); }
2200 "CONST_INT_P (operands[1]) && REG_P (operands[0]) && GP_REG_P (REGNO
2201 (operands[0]))"
2202 [(const_int 0)]
2203 "
2204 {
2205 loongarch_move_integer (operands[0], operands[0], INTVAL (operands[1]));
2206 DONE;
2207 }
2208 "
2209 [(set_attr "move_type" "move,const,load,store,mgtf,fpload,mftg,fpstore")
2210 (set_attr "mode" "DI")])
2211
2212 (define_insn_and_split "*movdi_64bit"
2213 [(set (match_operand:DI 0 "nonimmediate_operand" "=r,r,r,w,*f,*f,*r,*m")
2214 (match_operand:DI 1 "move_operand" "r,Yd,w,rJ,*r*J,*m,*f,*f"))]
2215 "TARGET_64BIT
2216 && (register_operand (operands[0], DImode)
2217 || reg_or_0_operand (operands[1], DImode))"
2218 {
2219 return loongarch_output_move (operands);
2220 }
2221 "CONST_INT_P (operands[1]) && REG_P (operands[0]) && GP_REG_P (REGNO
2222 (operands[0]))"
2223 [(const_int 0)]
2224 "
2225 {
2226 loongarch_move_integer (operands[0], operands[0], INTVAL (operands[1]));
2227 DONE;
2228 }
2229 "
2230 [(set_attr "move_type" "move,const,load,store,mgtf,fpload,mftg,fpstore")
2231 (set_attr "mode" "DI")])
2232
2233 ;; Use two registers to get the global symbol address from the got table.
2234 ;; la.global rd, rt, sym
2235
2236 (define_insn_and_split "movdi_symbolic_off64"
2237 [(set (match_operand:DI 0 "register_operand" "=r,r")
2238 (match_operand:DI 1 "symbolic_off64_or_reg_operand" "Yd,r"))
2239 (unspec:DI [(const_int 0)]
2240 UNSPEC_LOAD_SYMBOL_OFFSET64)
2241 (clobber (match_operand:DI 2 "register_operand" "=&r,r"))]
2242 "TARGET_64BIT"
2243 {
2244 if (which_alternative == 1)
2245 return "#";
2246
2247 enum loongarch_symbol_type symbol_type;
2248 gcc_assert (loongarch_symbolic_constant_p (operands[1], &symbol_type));
2249
2250 switch (symbol_type)
2251 {
2252 case SYMBOL_PCREL64:
2253 return "la.local\t%0,%2,%1";
2254 case SYMBOL_GOT_DISP:
2255 return "la.global\t%0,%2,%1";
2256 case SYMBOL_TLS_IE:
2257 return "la.tls.ie\t%0,%2,%1";
2258 case SYMBOL_TLSGD:
2259 return "la.tls.gd\t%0,%2,%1";
2260 case SYMBOL_TLSLDM:
2261 return "la.tls.ld\t%0,%2,%1";
2262
2263 default:
2264 gcc_unreachable ();
2265 }
2266 }
2267 "&& REG_P (operands[1]) && find_reg_note (insn, REG_UNUSED, operands[2]) != 0"
2268 [(set (match_dup 0) (match_dup 1))]
2269 ""
2270 [(set_attr "mode" "DI")
2271 (set_attr "insn_count" "5")])
2272
2273 ;; The 64-bit PC-relative part of address loading.
2274 ;; Note that the psABI does not allow splitting it.
2275 (define_insn "la_pcrel64_two_parts"
2276 [(set (match_operand:DI 0 "register_operand" "=r")
2277 (unspec:DI [(match_operand:DI 2 "") (pc)] UNSPEC_LA_PCREL_64_PART1))
2278 (set (match_operand:DI 1 "register_operand" "=r")
2279 (unspec:DI [(match_dup 2) (pc)] UNSPEC_LA_PCREL_64_PART2))]
2280 "TARGET_ABI_LP64 && la_opt_explicit_relocs != EXPLICIT_RELOCS_NONE"
2281 {
2282 return "pcalau12i\t%0,%r2\n\t"
2283 "addi.d\t%1,$r0,%L2\n\t"
2284 "lu32i.d\t%1,%R2\n\t"
2285 "lu52i.d\t%1,%1,%H2";
2286 }
2287 [(set_attr "move_type" "move")
2288 (set_attr "mode" "DI")
2289 (set_attr "length" "16")])
2290
2291 ;; 32-bit Integer moves
2292
2293 (define_expand "movsi"
2294 [(set (match_operand:SI 0 "")
2295 (match_operand:SI 1 ""))]
2296 ""
2297 {
2298 if (loongarch_legitimize_move (SImode, operands[0], operands[1]))
2299 DONE;
2300 })
2301
2302 (define_insn_and_split "*movsi_internal"
2303 [(set (match_operand:SI 0 "nonimmediate_operand" "=r,r,r,w,*f,f,*r,*m")
2304 (match_operand:SI 1 "move_operand" "r,Yd,w,rJ,*r*J,m,*f,*f"))]
2305 "(register_operand (operands[0], SImode)
2306 || reg_or_0_operand (operands[1], SImode))"
2307 { return loongarch_output_move (operands); }
2308 "CONST_INT_P (operands[1]) && REG_P (operands[0]) && GP_REG_P (REGNO
2309 (operands[0]))"
2310 [(const_int 0)]
2311 "
2312 {
2313 loongarch_move_integer (operands[0], operands[0], INTVAL (operands[1]));
2314 DONE;
2315 }
2316 "
2317 [(set_attr "move_type" "move,const,load,store,mgtf,fpload,mftg,fpstore")
2318 (set_attr "mode" "SI")])
2319
2320 ;; 16-bit Integer moves
2321
2322 ;; Unlike most other insns, the move insns can't be split with
2323 ;; different predicates, because register spilling and other parts of
2324 ;; the compiler, have memoized the insn number already.
2325 ;; Unsigned loads are used because LOAD_EXTEND_OP returns ZERO_EXTEND.
2326
2327 (define_expand "movhi"
2328 [(set (match_operand:HI 0 "")
2329 (match_operand:HI 1 ""))]
2330 ""
2331 {
2332 if (loongarch_legitimize_move (HImode, operands[0], operands[1]))
2333 DONE;
2334 })
2335
2336 (define_insn_and_split "*movhi_internal"
2337 [(set (match_operand:HI 0 "nonimmediate_operand" "=r,r,r,r,m,r,k")
2338 (match_operand:HI 1 "move_operand" "r,Yd,I,m,rJ,k,rJ"))]
2339 "(register_operand (operands[0], HImode)
2340 || reg_or_0_operand (operands[1], HImode))"
2341 { return loongarch_output_move (operands); }
2342 "CONST_INT_P (operands[1]) && REG_P (operands[0]) && GP_REG_P (REGNO
2343 (operands[0]))"
2344 [(const_int 0)]
2345 "
2346 {
2347 loongarch_move_integer (operands[0], operands[0], INTVAL (operands[1]));
2348 DONE;
2349 }
2350 "
2351 [(set_attr "move_type" "move,const,const,load,store,load,store")
2352 (set_attr "mode" "HI")])
2353
2354 ;; 8-bit Integer moves
2355
2356 ;; Unlike most other insns, the move insns can't be split with
2357 ;; different predicates, because register spilling and other parts of
2358 ;; the compiler, have memoized the insn number already.
2359 ;; Unsigned loads are used because LOAD_EXTEND_OP returns ZERO_EXTEND.
2360
2361 (define_expand "movqi"
2362 [(set (match_operand:QI 0 "")
2363 (match_operand:QI 1 ""))]
2364 ""
2365 {
2366 if (loongarch_legitimize_move (QImode, operands[0], operands[1]))
2367 DONE;
2368 })
2369
2370 (define_insn "*movqi_internal"
2371 [(set (match_operand:QI 0 "nonimmediate_operand" "=r,r,r,m,r,k")
2372 (match_operand:QI 1 "move_operand" "r,I,m,rJ,k,rJ"))]
2373 "(register_operand (operands[0], QImode)
2374 || reg_or_0_operand (operands[1], QImode))"
2375 { return loongarch_output_move (operands); }
2376 [(set_attr "move_type" "move,const,load,store,load,store")
2377 (set_attr "mode" "QI")])
2378
2379 ;; 32-bit floating point moves
2380
2381 (define_expand "movsf"
2382 [(set (match_operand:SF 0 "")
2383 (match_operand:SF 1 ""))]
2384 ""
2385 {
2386 if (loongarch_legitimize_move (SFmode, operands[0], operands[1]))
2387 DONE;
2388 })
2389
2390 (define_insn "*movsf_hardfloat"
2391 [(set (match_operand:SF 0 "nonimmediate_operand" "=f,f,f,m,f,k,m,k,*f,*r,*r,*r,*m")
2392 (match_operand:SF 1 "move_operand" "f,G,m,f,k,f,G,G,*r,*f,*G*r,*m,*r"))]
2393 "TARGET_HARD_FLOAT
2394 && (register_operand (operands[0], SFmode)
2395 || reg_or_0_operand (operands[1], SFmode))"
2396 { return loongarch_output_move (operands); }
2397 [(set_attr "move_type" "fmove,mgtf,fpload,fpstore,fpload,fpstore,store,store,mgtf,mftg,move,load,store")
2398 (set_attr "mode" "SF")])
2399
2400 (define_insn "*movsf_softfloat"
2401 [(set (match_operand:SF 0 "nonimmediate_operand" "=r,r,m")
2402 (match_operand:SF 1 "move_operand" "Gr,m,r"))]
2403 "TARGET_SOFT_FLOAT
2404 && (register_operand (operands[0], SFmode)
2405 || reg_or_0_operand (operands[1], SFmode))"
2406 { return loongarch_output_move (operands); }
2407 [(set_attr "move_type" "move,load,store")
2408 (set_attr "mode" "SF")])
2409
2410 ;; 64-bit floating point moves
2411
2412 (define_expand "movdf"
2413 [(set (match_operand:DF 0 "")
2414 (match_operand:DF 1 ""))]
2415 ""
2416 {
2417 if (loongarch_legitimize_move (DFmode, operands[0], operands[1]))
2418 DONE;
2419 })
2420
2421 (define_insn "*movdf_hardfloat"
2422 [(set (match_operand:DF 0 "nonimmediate_operand" "=f,f,f,m,f,k,m,k,*f,*r,*r,*r,*m")
2423 (match_operand:DF 1 "move_operand" "f,G,m,f,k,f,G,G,*r,*f,*r*G,*m,*r"))]
2424 "TARGET_DOUBLE_FLOAT
2425 && (register_operand (operands[0], DFmode)
2426 || reg_or_0_operand (operands[1], DFmode))"
2427 { return loongarch_output_move (operands); }
2428 [(set_attr "move_type" "fmove,mgtf,fpload,fpstore,fpload,fpstore,store,store,mgtf,mftg,move,load,store")
2429 (set_attr "mode" "DF")])
2430
2431 (define_insn "*movdf_softfloat"
2432 [(set (match_operand:DF 0 "nonimmediate_operand" "=r,r,m")
2433 (match_operand:DF 1 "move_operand" "rG,m,rG"))]
2434 "(TARGET_SOFT_FLOAT || TARGET_SINGLE_FLOAT)
2435 && TARGET_64BIT
2436 && (register_operand (operands[0], DFmode)
2437 || reg_or_0_operand (operands[1], DFmode))"
2438 { return loongarch_output_move (operands); }
2439 [(set_attr "move_type" "move,load,store")
2440 (set_attr "mode" "DF")])
2441
2442 ;; Clear one FCC register
2443
2444 (define_expand "movfcc"
2445 [(set (match_operand:FCC 0 "")
2446 (match_operand:FCC 1 ""))]
2447 "TARGET_HARD_FLOAT"
2448 {
2449 if (memory_operand (operands[0], FCCmode)
2450 && memory_operand (operands[1], FCCmode))
2451 operands[1] = force_reg (FCCmode, operands[1]);
2452 })
2453
2454 (define_insn "movfcc_internal"
2455 [(set (match_operand:FCC 0 "nonimmediate_operand"
2456 "=z,z,*f,*f,*r,*r,*m,*f,*r,z,*r")
2457 (match_operand:FCC 1 "reg_or_0_operand"
2458 "J,*f,z,*f,J*r,*m,J*r,J*r,*f,*r,z"))]
2459 "TARGET_HARD_FLOAT"
2460 "@
2461 fcmp.caf.s\t%0,$f0,$f0
2462 movfr2cf\t%0,%1
2463 movcf2fr\t%0,%1
2464 fmov.s\t%0,%1
2465 or\t%0,%z1,$r0
2466 ld.b\t%0,%1
2467 st.b\t%z1,%0
2468 movgr2fr.w\t%0,%1
2469 movfr2gr.s\t%0,%1
2470 movgr2cf\t%0,%1
2471 movcf2gr\t%0,%1"
2472 [(set_attr "type" "move")
2473 (set_attr "mode" "FCC")])
2474
2475 (define_insn "fcc_to_<X:mode>"
2476 [(set (match_operand:X 0 "register_operand" "=r")
2477 (if_then_else:X (ne (match_operand:FCC 1 "register_operand" "0")
2478 (const_int 0))
2479 (const_int 1)
2480 (const_int 0)))]
2481 "TARGET_HARD_FLOAT"
2482 ""
2483 [(set_attr "length" "0")
2484 (set_attr "type" "ghost")])
2485
2486 (define_expand "cstore<ANYF:mode>4"
2487 [(set (match_operand:SI 0 "register_operand")
2488 (match_operator:SI 1 "loongarch_fcmp_operator"
2489 [(match_operand:ANYF 2 "register_operand")
2490 (match_operand:ANYF 3 "register_operand")]))]
2491 ""
2492 {
2493 rtx fcc = gen_reg_rtx (FCCmode);
2494 rtx cmp = gen_rtx_fmt_ee (GET_CODE (operands[1]), FCCmode,
2495 operands[2], operands[3]);
2496
2497 emit_insn (gen_rtx_SET (fcc, cmp));
2498 if (TARGET_64BIT)
2499 {
2500 rtx gpr = gen_reg_rtx (DImode);
2501 emit_insn (gen_fcc_to_di (gpr, fcc));
2502 emit_insn (gen_rtx_SET (operands[0],
2503 lowpart_subreg (SImode, gpr, DImode)));
2504 }
2505 else
2506 emit_insn (gen_fcc_to_si (operands[0], fcc));
2507
2508 DONE;
2509 })
2510
2511 ;; Conditional move instructions.
2512
2513 (define_insn "*sel<code><GPR:mode>_using_<X:mode>"
2514 [(set (match_operand:GPR 0 "register_operand" "=r,r")
2515 (if_then_else:GPR
2516 (equality_op:X (match_operand:X 1 "register_operand" "r,r")
2517 (const_int 0))
2518 (match_operand:GPR 2 "reg_or_0_operand" "r,J")
2519 (match_operand:GPR 3 "reg_or_0_operand" "J,r")))]
2520 "register_operand (operands[2], <GPR:MODE>mode)
2521 != register_operand (operands[3], <GPR:MODE>mode)"
2522 "@
2523 <sel>\t%0,%2,%1
2524 <selinv>\t%0,%3,%1"
2525 [(set_attr "type" "condmove")
2526 (set_attr "mode" "<GPR:MODE>")])
2527
2528 ;; fsel copies the 3rd argument when the 1st is non-zero and the 2nd
2529 ;; argument if the 1st is zero. This means operand 2 and 3 are
2530 ;; inverted in the instruction.
2531
2532 (define_insn "*sel<mode>"
2533 [(set (match_operand:ANYF 0 "register_operand" "=f")
2534 (if_then_else:ANYF
2535 (ne:FCC (match_operand:FCC 1 "register_operand" "z")
2536 (const_int 0))
2537 (match_operand:ANYF 2 "reg_or_0_operand" "f")
2538 (match_operand:ANYF 3 "reg_or_0_operand" "f")))]
2539 ""
2540 "fsel\t%0,%3,%2,%1"
2541 [(set_attr "type" "condmove")
2542 (set_attr "mode" "<ANYF:MODE>")])
2543
2544 ;; These are the main define_expand's used to make conditional moves.
2545
2546 (define_expand "mov<mode>cc"
2547 [(set (match_operand:GPR 0 "register_operand")
2548 (if_then_else:GPR (match_operator 1 "comparison_operator"
2549 [(match_operand:GPR 2 "reg_or_0_operand")
2550 (match_operand:GPR 3 "reg_or_0_operand")])))]
2551 "TARGET_COND_MOVE_INT"
2552 {
2553 if (!INTEGRAL_MODE_P (GET_MODE (XEXP (operands[1], 0))))
2554 FAIL;
2555
2556 loongarch_expand_conditional_move (operands);
2557 DONE;
2558 })
2559
2560 (define_expand "mov<mode>cc"
2561 [(set (match_operand:ANYF 0 "register_operand")
2562 (if_then_else:ANYF (match_operator 1 "comparison_operator"
2563 [(match_operand:ANYF 2 "reg_or_0_operand")
2564 (match_operand:ANYF 3 "reg_or_0_operand")])))]
2565 "TARGET_COND_MOVE_FLOAT"
2566 {
2567 if (!FLOAT_MODE_P (GET_MODE (XEXP (operands[1], 0))))
2568 FAIL;
2569
2570 loongarch_expand_conditional_move (operands);
2571 DONE;
2572 })
2573
2574 (define_insn "lu32i_d"
2575 [(set (match_operand:DI 0 "register_operand" "=r")
2576 (ior:DI
2577 (zero_extend:DI
2578 (subreg:SI (match_operand:DI 1 "register_operand" "0") 0))
2579 (match_operand:DI 2 "const_lu32i_operand" "u")))]
2580 "TARGET_64BIT"
2581 {
2582 operands[2] = GEN_INT (INTVAL (operands[2]) >> 32);
2583 return "lu32i.d\t%0,%X2";
2584 }
2585 [(set_attr "type" "arith")
2586 (set_attr "mode" "DI")])
2587
2588 (define_insn "lu52i_d"
2589 [(set (match_operand:DI 0 "register_operand" "=r")
2590 (ior:DI
2591 (and:DI (match_operand:DI 1 "register_operand" "r")
2592 (match_operand 2 "lu52i_mask_operand"))
2593 (match_operand 3 "const_lu52i_operand" "v")))]
2594 "TARGET_64BIT"
2595 {
2596 operands[3] = GEN_INT (INTVAL (operands[3]) >> 52);
2597 return "lu52i.d\t%0,%1,%X3";
2598 }
2599 [(set_attr "type" "arith")
2600 (set_attr "mode" "DI")])
2601
2602 ;; Instructions for adding the low 12 bits of an address to a register.
2603 ;; Operand 2 is the address: loongarch_print_operand works out which relocation
2604 ;; should be applied.
2605
2606 (define_insn "*low<mode>"
2607 [(set (match_operand:P 0 "register_operand" "=r")
2608 (lo_sum:P (match_operand:P 1 "register_operand" " r")
2609 (match_operand:P 2 "symbolic_operand" "")))]
2610 ""
2611 "addi.<d>\t%0,%1,%L2"
2612 [(set_attr "type" "arith")
2613 (set_attr "mode" "<MODE>")])
2614
2615 (define_insn "@tls_low<mode>"
2616 [(set (match_operand:P 0 "register_operand" "=r")
2617 (unspec:P [(mem:P (lo_sum:P (match_operand:P 1 "register_operand" "r")
2618 (match_operand:P 2 "symbolic_operand" "")))]
2619 UNSPEC_TLS_LOW))]
2620 ""
2621 "addi.<d>\t%0,%1,%L2"
2622 [(set_attr "type" "arith")
2623 (set_attr "mode" "<MODE>")])
2624
2625 ;; Instructions for loading address from GOT entry.
2626 ;; operands[1] is pc plus the high half of the address difference with the got
2627 ;; entry;
2628 ;; operands[2] is low 12 bits for low 12 bit of the address difference with the
2629 ;; got entry.
2630 ;; loongarch_print_operand works out which relocation should be applied.
2631
2632 (define_insn "@ld_from_got<mode>"
2633 [(set (match_operand:P 0 "register_operand" "=r")
2634 (unspec:P [(mem:P (lo_sum:P
2635 (match_operand:P 1 "register_operand" "r")
2636 (match_operand:P 2 "symbolic_operand")))]
2637 UNSPEC_LOAD_FROM_GOT))]
2638 ""
2639 "%Q2ld.<d>\t%0,%1,%L2"
2640 [(set_attr "type" "move")]
2641 )
2642
2643 (define_insn "@lui_l_hi20<mode>"
2644 [(set (match_operand:P 0 "register_operand" "=r")
2645 (unspec:P [(match_operand:P 1 "symbolic_operand")]
2646 UNSPEC_LUI_L_HI20))]
2647 ""
2648 "lu12i.w\t%0,%r1"
2649 [(set_attr "type" "move")]
2650 )
2651
2652 (define_insn "@pcalau12i<mode>"
2653 [(set (match_operand:P 0 "register_operand" "=j")
2654 (unspec:P [(match_operand:P 1 "symbolic_operand" "")]
2655 UNSPEC_PCALAU12I))]
2656 ""
2657 "pcalau12i\t%0,%%pc_hi20(%1)"
2658 [(set_attr "type" "move")])
2659
2660 ;; @pcalau12i may be used for sibcall so it has a strict constraint. This
2661 ;; allows any general register as the operand.
2662 (define_insn "@pcalau12i_gr<mode>"
2663 [(set (match_operand:P 0 "register_operand" "=r")
2664 (unspec:P [(match_operand:P 1 "symbolic_operand" "")]
2665 UNSPEC_PCALAU12I_GR))]
2666 ""
2667 "pcalau12i\t%0,%%pc_hi20(%1)"
2668 [(set_attr "type" "move")])
2669
2670 (define_insn "@add_tls_le_relax<mode>"
2671 [(set (match_operand:P 0 "register_operand" "=r")
2672 (unspec:P [(match_operand:P 1 "register_operand" "r")
2673 (match_operand:P 2 "register_operand" "r")
2674 (match_operand:P 3 "symbolic_operand")]
2675 UNSPEC_ADD_TLS_LE_RELAX))]
2676 "HAVE_AS_TLS_LE_RELAXATION"
2677 "add.<d>\t%0,%1,%2,%%le_add_r(%3)"
2678 [(set_attr "type" "move")]
2679 )
2680
2681 (define_insn "@ori_l_lo12<mode>"
2682 [(set (match_operand:P 0 "register_operand" "=r")
2683 (unspec:P [(match_operand:P 1 "register_operand" "r")
2684 (match_operand:P 2 "symbolic_operand")]
2685 UNSPEC_ORI_L_LO12))]
2686 ""
2687 "ori\t%0,%1,%L2"
2688 [(set_attr "type" "move")]
2689 )
2690
2691 (define_insn "lui_h_lo20"
2692 [(set (match_operand:DI 0 "register_operand" "=r")
2693 (unspec:DI [(match_operand:DI 1 "register_operand" "0")
2694 (match_operand:DI 2 "symbolic_operand")]
2695 UNSPEC_LUI_H_LO20))]
2696 "TARGET_64BIT"
2697 "lu32i.d\t%0,%R2"
2698 [(set_attr "type" "move")]
2699 )
2700
2701 (define_insn "lui_h_hi12"
2702 [(set (match_operand:DI 0 "register_operand" "=r")
2703 (unspec:DI [(match_operand:DI 1 "register_operand" "r")
2704 (match_operand:DI 2 "symbolic_operand")]
2705 UNSPEC_LUI_H_HI12))]
2706 "TARGET_64BIT"
2707 "lu52i.d\t%0,%1,%H2"
2708 [(set_attr "type" "move")]
2709 )
2710
2711 ;; Round floating-point numbers to integers
2712 (define_insn "rint<mode>2"
2713 [(set (match_operand:ANYF 0 "register_operand" "=f")
2714 (unspec:ANYF [(match_operand:ANYF 1 "register_operand" "f")]
2715 UNSPEC_FRINT))]
2716 ""
2717 "frint.<fmt>\t%0,%1"
2718 [(set_attr "type" "fcvt")
2719 (set_attr "mode" "<MODE>")])
2720
2721 ;; Convert floating-point numbers to integers
2722 (define_insn "<lrint_pattern><ANYF:mode><ANYFI:mode>2"
2723 [(set (match_operand:ANYFI 0 "register_operand" "=f")
2724 (unspec:ANYFI [(match_operand:ANYF 1 "register_operand" "f")]
2725 LRINT))]
2726 "TARGET_HARD_FLOAT &&
2727 (<LRINT> == UNSPEC_FTINT
2728 || flag_fp_int_builtin_inexact
2729 || !flag_trapping_math)"
2730 "ftint<lrint_submenmonic>.<ANYFI:ifmt>.<ANYF:fmt> %0,%1"
2731 [(set_attr "type" "fcvt")
2732 (set_attr "mode" "<ANYF:MODE>")])
2733
2734 ;; Thread-Local Storage
2735
2736 (define_insn "@got_load_tls_desc<mode>"
2737 [(set (reg:P 4)
2738 (unspec:P
2739 [(match_operand:P 0 "symbolic_operand" "")]
2740 UNSPEC_TLS_DESC))
2741 (clobber (reg:SI FCC0_REGNUM))
2742 (clobber (reg:SI FCC1_REGNUM))
2743 (clobber (reg:SI FCC2_REGNUM))
2744 (clobber (reg:SI FCC3_REGNUM))
2745 (clobber (reg:SI FCC4_REGNUM))
2746 (clobber (reg:SI FCC5_REGNUM))
2747 (clobber (reg:SI FCC6_REGNUM))
2748 (clobber (reg:SI FCC7_REGNUM))
2749 (clobber (reg:SI RETURN_ADDR_REGNUM))]
2750 "TARGET_TLS_DESC"
2751 {
2752 return TARGET_EXPLICIT_RELOCS
2753 ? "pcalau12i\t$r4,%%desc_pc_hi20(%0)\n\t"
2754 "addi.d\t$r4,$r4,%%desc_pc_lo12(%0)\n\t"
2755 "ld.d\t$r1,$r4,%%desc_ld(%0)\n\t"
2756 "jirl\t$r1,$r1,%%desc_call(%0)"
2757 : "la.tls.desc\t$r4,%0";
2758 }
2759 [(set_attr "got" "load")
2760 (set_attr "mode" "<MODE>")
2761 (set_attr "length" "16")])
2762
2763 (define_insn "got_load_tls_desc_off64"
2764 [(set (reg:DI 4)
2765 (unspec:DI
2766 [(match_operand:DI 0 "symbolic_operand" "")]
2767 UNSPEC_TLS_DESC_OFF64))
2768 (clobber (reg:SI FCC0_REGNUM))
2769 (clobber (reg:SI FCC1_REGNUM))
2770 (clobber (reg:SI FCC2_REGNUM))
2771 (clobber (reg:SI FCC3_REGNUM))
2772 (clobber (reg:SI FCC4_REGNUM))
2773 (clobber (reg:SI FCC5_REGNUM))
2774 (clobber (reg:SI FCC6_REGNUM))
2775 (clobber (reg:SI FCC7_REGNUM))
2776 (clobber (reg:SI RETURN_ADDR_REGNUM))
2777 (clobber (match_operand:DI 1 "register_operand" "=&r"))]
2778 "TARGET_TLS_DESC && TARGET_CMODEL_EXTREME"
2779 {
2780 return TARGET_EXPLICIT_RELOCS
2781 ? "pcalau12i\t$r4,%%desc_pc_hi20(%0)\n\t"
2782 "addi.d\t%1,$r0,%%desc_pc_lo12(%0)\n\t"
2783 "lu32i.d\t%1,%%desc64_pc_lo20(%0)\n\t"
2784 "lu52i.d\t%1,%1,%%desc64_pc_hi12(%0)\n\t"
2785 "add.d\t$r4,$r4,%1\n\t"
2786 "ld.d\t$r1,$r4,%%desc_ld(%0)\n\t"
2787 "jirl\t$r1,$r1,%%desc_call(%0)"
2788 : "la.tls.desc\t$r4,%1,%0";
2789 }
2790 [(set_attr "got" "load")
2791 (set_attr "length" "28")])
2792
2793 (define_insn "@load_tls<mode>"
2794 [(set (match_operand:P 0 "register_operand" "=r")
2795 (unspec:P
2796 [(match_operand:P 1 "symbolic_operand" "")]
2797 UNSPEC_TLS))]
2798 ""
2799 {
2800 enum loongarch_symbol_type symbol_type;
2801 gcc_assert (loongarch_symbolic_constant_p (operands[1], &symbol_type));
2802
2803 switch (symbol_type)
2804 {
2805 case SYMBOL_TLS_LE:
2806 return "la.tls.le\t%0,%1";
2807 case SYMBOL_TLS_IE:
2808 return "la.tls.ie\t%0,%1";
2809 case SYMBOL_TLSLDM:
2810 return "la.tls.ld\t%0,%1";
2811 case SYMBOL_TLSGD:
2812 return "la.tls.gd\t%0,%1";
2813
2814 default:
2815 gcc_unreachable ();
2816 }
2817 }
2818 [(set_attr "mode" "<MODE>")
2819 (set (attr "insn_count")
2820 (if_then_else
2821 (match_test "TARGET_CMODEL_EXTREME")
2822 (const_int 4)
2823 (const_int 2)))])
2824
2825
2826 ;; Expand in-line code to clear the instruction cache between operand[0] and
2827 ;; operand[1].
2828 (define_expand "clear_cache"
2829 [(match_operand 0 "pmode_register_operand")
2830 (match_operand 1 "pmode_register_operand")]
2831 ""
2832 {
2833 emit_insn (gen_loongarch_ibar (const0_rtx));
2834 DONE;
2835 })
2836
2837 (define_insn "loongarch_ibar"
2838 [(unspec_volatile:SI
2839 [(match_operand 0 "const_uimm15_operand")]
2840 UNSPECV_IBAR)
2841 (clobber (mem:BLK (scratch)))]
2842 ""
2843 "ibar\t%0")
2844
2845 (define_insn "loongarch_dbar"
2846 [(unspec_volatile:SI
2847 [(match_operand 0 "const_uimm15_operand")]
2848 UNSPECV_DBAR)
2849 (clobber (mem:BLK (scratch)))]
2850 ""
2851 "dbar\t%0")
2852
2853
2854
2855 ;; Privileged state instruction
2856
2857 (define_insn "loongarch_cpucfg"
2858 [(set (match_operand:SI 0 "register_operand" "=r")
2859 (unspec_volatile:SI [(match_operand:SI 1 "register_operand" "r")]
2860 UNSPECV_CPUCFG))]
2861 ""
2862 "cpucfg\t%0,%1"
2863 [(set_attr "type" "load")
2864 (set_attr "mode" "SI")])
2865
2866 (define_insn "loongarch_syscall"
2867 [(unspec_volatile:SI
2868 [(match_operand 0 "const_uimm15_operand")]
2869 UNSPECV_SYSCALL)
2870 (clobber (mem:BLK (scratch)))]
2871 ""
2872 "syscall\t%0")
2873
2874 (define_insn "loongarch_break"
2875 [(unspec_volatile:SI
2876 [(match_operand 0 "const_uimm15_operand")]
2877 UNSPECV_BREAK)
2878 (clobber (mem:BLK (scratch)))]
2879 ""
2880 "break\t%0")
2881
2882 (define_insn "loongarch_asrtle_d"
2883 [(unspec_volatile:DI [(match_operand:DI 0 "register_operand" "r")
2884 (match_operand:DI 1 "register_operand" "r")]
2885 UNSPECV_ASRTLE_D)]
2886 "TARGET_64BIT"
2887 "asrtle.d\t%0,%1"
2888 [(set_attr "type" "load")
2889 (set_attr "mode" "DI")])
2890
2891 (define_insn "loongarch_asrtgt_d"
2892 [(unspec_volatile:DI [(match_operand:DI 0 "register_operand" "r")
2893 (match_operand:DI 1 "register_operand" "r")]
2894 UNSPECV_ASRTGT_D)]
2895 "TARGET_64BIT"
2896 "asrtgt.d\t%0,%1"
2897 [(set_attr "type" "load")
2898 (set_attr "mode" "DI")])
2899
2900 (define_insn "loongarch_csrrd_<d>"
2901 [(set (match_operand:GPR 0 "register_operand" "=r")
2902 (unspec_volatile:GPR [(match_operand 1 "const_uimm14_operand")]
2903 UNSPECV_CSRRD))
2904 (clobber (mem:BLK (scratch)))]
2905 ""
2906 "csrrd\t%0,%1"
2907 [(set_attr "type" "load")
2908 (set_attr "mode" "<MODE>")])
2909
2910 (define_insn "loongarch_csrwr_<d>"
2911 [(set (match_operand:GPR 0 "register_operand" "=r")
2912 (unspec_volatile:GPR
2913 [(match_operand:GPR 1 "register_operand" "0")
2914 (match_operand 2 "const_uimm14_operand")]
2915 UNSPECV_CSRWR))
2916 (clobber (mem:BLK (scratch)))]
2917 ""
2918 "csrwr\t%0,%2"
2919 [(set_attr "type" "store")
2920 (set_attr "mode" "<MODE>")])
2921
2922 (define_insn "loongarch_csrxchg_<d>"
2923 [(set (match_operand:GPR 0 "register_operand" "=r")
2924 (unspec_volatile:GPR
2925 [(match_operand:GPR 1 "register_operand" "0")
2926 (match_operand:GPR 2 "register_operand" "q")
2927 (match_operand 3 "const_uimm14_operand")]
2928 UNSPECV_CSRXCHG))
2929 (clobber (mem:BLK (scratch)))]
2930 ""
2931 "csrxchg\t%0,%2,%3"
2932 [(set_attr "type" "load")
2933 (set_attr "mode" "<MODE>")])
2934
2935 (define_insn "loongarch_iocsrrd_<size>"
2936 [(set (match_operand:QHWD 0 "register_operand" "=r")
2937 (unspec_volatile:QHWD [(match_operand:SI 1 "register_operand" "r")]
2938 UNSPECV_IOCSRRD))
2939 (clobber (mem:BLK (scratch)))]
2940 ""
2941 "iocsrrd.<size>\t%0,%1"
2942 [(set_attr "type" "load")
2943 (set_attr "mode" "<MODE>")])
2944
2945 (define_insn "loongarch_iocsrwr_<size>"
2946 [(unspec_volatile:QHWD [(match_operand:QHWD 0 "register_operand" "r")
2947 (match_operand:SI 1 "register_operand" "r")]
2948 UNSPECV_IOCSRWR)
2949 (clobber (mem:BLK (scratch)))]
2950 ""
2951 "iocsrwr.<size>\t%0,%1"
2952 [(set_attr "type" "load")
2953 (set_attr "mode" "<MODE>")])
2954
2955 (define_insn "loongarch_cacop_<d>"
2956 [(unspec_volatile:X [(match_operand 0 "const_uimm5_operand")
2957 (match_operand:X 1 "register_operand" "r")
2958 (match_operand 2 "const_imm12_operand")]
2959 UNSPECV_CACOP)
2960 (clobber (mem:BLK (scratch)))]
2961 ""
2962 "cacop\t%0,%1,%2"
2963 [(set_attr "type" "load")
2964 (set_attr "mode" "<MODE>")])
2965
2966 (define_insn "loongarch_lddir_<d>"
2967 [(unspec_volatile:X [(match_operand:X 0 "register_operand" "r")
2968 (match_operand:X 1 "register_operand" "r")
2969 (match_operand 2 "const_uimm5_operand")]
2970 UNSPECV_LDDIR)
2971 (clobber (mem:BLK (scratch)))]
2972 ""
2973 "lddir\t%0,%1,%2"
2974 [(set_attr "type" "load")
2975 (set_attr "mode" "<MODE>")])
2976
2977 (define_insn "loongarch_ldpte_<d>"
2978 [(unspec_volatile:X [(match_operand:X 0 "register_operand" "r")
2979 (match_operand 1 "const_uimm5_operand")]
2980 UNSPECV_LDPTE)
2981 (clobber (mem:BLK (scratch)))]
2982 ""
2983 "ldpte\t%0,%1"
2984 [(set_attr "type" "load")
2985 (set_attr "mode" "<MODE>")])
2986
2987
2988 ;; Block moves, see loongarch.c for more details.
2989 ;; Argument 0 is the destination.
2990 ;; Argument 1 is the source.
2991 ;; Argument 2 is the length.
2992 ;; Argument 3 is the alignment.
2993
2994 (define_expand "cpymemsi"
2995 [(parallel [(set (match_operand:BLK 0 "general_operand")
2996 (match_operand:BLK 1 "general_operand"))
2997 (use (match_operand:SI 2 ""))
2998 (use (match_operand:SI 3 "const_int_operand"))])]
2999 ""
3000 {
3001 if (TARGET_DO_OPTIMIZE_BLOCK_MOVE_P
3002 && loongarch_expand_block_move (operands[0], operands[1],
3003 operands[2], operands[3]))
3004 DONE;
3005 else
3006 FAIL;
3007 })
3008
3009 ;;
3010 ;; ....................
3011 ;;
3012 ;; SHIFTS
3013 ;;
3014 ;; ....................
3015
3016 (define_insn "*<optab><mode>3"
3017 [(set (match_operand:GPR 0 "register_operand" "=r")
3018 (any_shift:GPR (match_operand:GPR 1 "register_operand" "r")
3019 (match_operand:SI 2 "arith_operand" "rI")))]
3020 ""
3021 {
3022 if (CONST_INT_P (operands[2]))
3023 operands[2] = GEN_INT (INTVAL (operands[2])
3024 & (GET_MODE_BITSIZE (<MODE>mode) - 1));
3025
3026 return "<insn>%i2.<d>\t%0,%1,%2";
3027 }
3028 [(set_attr "type" "shift")
3029 (set_attr "mode" "<MODE>")])
3030
3031 (define_insn "<optab>si3_extend"
3032 [(set (match_operand:DI 0 "register_operand" "=r")
3033 (sign_extend:DI
3034 (any_shift:SI (match_operand:SI 1 "register_operand" "r")
3035 (match_operand:SI 2 "arith_operand" "rI"))))]
3036 "TARGET_64BIT"
3037 {
3038 if (CONST_INT_P (operands[2]))
3039 operands[2] = GEN_INT (INTVAL (operands[2]) & 0x1f);
3040
3041 return "<insn>%i2.w\t%0,%1,%2";
3042 }
3043 [(set_attr "type" "shift")
3044 (set_attr "mode" "SI")])
3045
3046 (define_insn "*rotr<mode>3"
3047 [(set (match_operand:GPR 0 "register_operand" "=r,r")
3048 (rotatert:GPR (match_operand:GPR 1 "register_operand" "r,r")
3049 (match_operand:SI 2 "arith_operand" "r,I")))]
3050 ""
3051 "rotr%i2.<d>\t%0,%1,%2"
3052 [(set_attr "type" "shift,shift")
3053 (set_attr "mode" "<MODE>")])
3054
3055 (define_insn "rotrsi3_extend"
3056 [(set (match_operand:DI 0 "register_operand" "=r,r")
3057 (sign_extend:DI
3058 (rotatert:SI (match_operand:SI 1 "register_operand" "r,r")
3059 (match_operand:SI 2 "arith_operand" "r,I"))))]
3060 "TARGET_64BIT"
3061 "rotr%i2.w\t%0,%1,%2"
3062 [(set_attr "type" "shift,shift")
3063 (set_attr "mode" "SI")])
3064
3065 ;; Expand left rotate to right rotate.
3066 (define_expand "rotl<mode>3"
3067 [(set (match_dup 3)
3068 (neg:SI (match_operand:SI 2 "register_operand")))
3069 (set (match_operand:GPR 0 "register_operand")
3070 (rotatert:GPR (match_operand:GPR 1 "register_operand")
3071 (match_dup 3)))]
3072 ""
3073 {
3074 operands[3] = gen_reg_rtx (SImode);
3075
3076 if (TARGET_64BIT && <MODE>mode == SImode)
3077 {
3078 rtx t = gen_reg_rtx (DImode);
3079
3080 emit_insn (gen_negsi2 (operands[3], operands[2]));
3081 emit_insn (gen_rotrsi3_extend (t, operands[1], operands[3]));
3082 t = gen_lowpart (SImode, t);
3083 SUBREG_PROMOTED_VAR_P (t) = 1;
3084 SUBREG_PROMOTED_SET (t, SRP_SIGNED);
3085 emit_move_insn (operands[0], t);
3086 DONE;
3087 }
3088 });
3089
3090 (define_insn "alsl<mode>3"
3091 [(set (match_operand:GPR 0 "register_operand" "=r")
3092 (plus:GPR (ashift:GPR (match_operand:GPR 1 "register_operand" "r")
3093 (match_operand 2 "const_immalsl_operand" ""))
3094 (match_operand:GPR 3 "register_operand" "r")))]
3095 ""
3096 "alsl.<d>\t%0,%1,%3,%2"
3097 [(set_attr "type" "arith")
3098 (set_attr "mode" "<MODE>")])
3099
3100 (define_insn "*alslsi3_extend"
3101 [(set (match_operand:DI 0 "register_operand" "=r")
3102 (any_extend:DI
3103 (plus:SI
3104 (ashift:SI (match_operand:SI 1 "register_operand" "r")
3105 (match_operand 2 "const_immalsl_operand" ""))
3106 (match_operand:SI 3 "register_operand" "r"))))]
3107 "TARGET_64BIT"
3108 "alsl.w<u>\t%0,%1,%3,%2"
3109 [(set_attr "type" "arith")
3110 (set_attr "mode" "SI")])
3111
3112 (define_insn "*alslsi3_extend_subreg"
3113 [(set (match_operand:DI 0 "register_operand" "=r")
3114 (any_extend:DI
3115 (plus:SI
3116 (subreg:SI
3117 (ashift:DI (match_operand:DI 1 "register_operand" "r")
3118 (match_operand 2 "const_immalsl_operand" ""))
3119 0)
3120 (subreg:SI (match_operand:DI 3 "register_operand" "r") 0))))]
3121 "TARGET_64BIT"
3122 "alsl.w<u>\t%0,%1,%3,%2"
3123 [(set_attr "type" "arith")
3124 (set_attr "mode" "SI")])
3125
3126 ;; The generic code prefers "(reg << shamt) [&|^] (mask << shamt)"
3127 ;; instead of "(reg [&|^] mask) << shamt" but we want the latter if
3128 ;; we don't need to load mask into an register, and either:
3129 ;; - (mask << shamt) needs to be loaded into an register, or
3130 ;; - shamt is a const_immalsl_operand, so the outer shift may be further
3131 ;; combined with an add.
3132 (define_insn_and_split "<optab>_shift_reverse<X:mode>"
3133 [(set (match_operand:X 0 "register_operand" "=r")
3134 (any_bitwise:X
3135 (ashift:X (match_operand:X 1 "register_operand" "r")
3136 (match_operand:SI 2 "const_int_operand" "i"))
3137 (match_operand:X 3 "const_int_operand" "i")))]
3138 "(const_immalsl_operand (operands[2], SImode)
3139 || !<bitwise_operand> (operands[3], <MODE>mode))
3140 && loongarch_reassoc_shift_bitwise (<is_and>, operands[2], operands[3],
3141 <MODE>mode)"
3142 "#"
3143 "&& true"
3144 [(set (match_dup 0) (any_bitwise:X (match_dup 1) (match_dup 3)))
3145 (set (match_dup 0) (ashift:X (match_dup 0) (match_dup 2)))]
3146 {
3147 operands[3] = loongarch_reassoc_shift_bitwise (<is_and>,
3148 operands[2],
3149 operands[3],
3150 <MODE>mode);
3151
3152 if (ins_zero_bitmask_operand (operands[3], <MODE>mode))
3153 {
3154 gcc_checking_assert (<is_and>);
3155 emit_move_insn (operands[0], operands[1]);
3156 operands[1] = operands[0];
3157 }
3158 })
3159
3160 ;; The late_combine2 pass can handle slli.d + add.d => alsl.d, so we
3161 ;; already have slli.d + any_bitwise + add.d => any_bitwise + slli.d +
3162 ;; add.d => any_bitwise + alsl.d. But late_combine2 cannot handle slli.d +
3163 ;; add.w => alsl.w, so implement slli.d + and + add.w => and + alsl.w on
3164 ;; our own.
3165 (define_insn_and_split "<optab>_alsl_reversesi_extended"
3166 [(set (match_operand:DI 0 "register_operand" "=&r")
3167 (sign_extend:DI
3168 (plus:SI
3169 (subreg:SI
3170 (any_bitwise:DI
3171 (ashift:DI
3172 (match_operand:DI 1 "register_operand" "r0")
3173 (match_operand:SI 2 "const_immalsl_operand" ""))
3174 (match_operand:DI 3 "const_int_operand" "i"))
3175 0)
3176 (match_operand:SI 4 "register_operand" "r"))))]
3177 "TARGET_64BIT
3178 && loongarch_reassoc_shift_bitwise (<is_and>, operands[2], operands[3],
3179 SImode)"
3180 "#"
3181 "&& reload_completed"
3182 [; r0 = r1 [&|^] r3 is emitted in PREPARATION-STATEMENTS because we
3183 ; need to handle a special case, see below.
3184 (set (match_dup 0)
3185 (sign_extend:DI
3186 (plus:SI (ashift:SI (subreg:SI (match_dup 0) 0) (match_dup 2))
3187 (match_dup 4))))]
3188 {
3189 operands[3] = loongarch_reassoc_shift_bitwise (<is_and>,
3190 operands[2],
3191 operands[3],
3192 SImode);
3193
3194 if (ins_zero_bitmask_operand (operands[3], SImode))
3195 {
3196 gcc_checking_assert (<is_and>);
3197 emit_move_insn (operands[0], operands[1]);
3198 operands[1] = operands[0];
3199 }
3200
3201 if (operands[3] != CONSTM1_RTX (SImode))
3202 emit_insn (gen_<optab>di3 (operands[0], operands[1], operands[3]));
3203 else
3204 {
3205 /* We can end up here with things like:
3206 x:DI = sign_extend(a:SI + ((b:DI << 2) & 0xfffffffc)#0) */
3207 gcc_checking_assert (<is_and>);
3208 emit_move_insn (operands[0], operands[1]);
3209 }
3210 })
3211
3212
3213
3214 ;; Reverse the order of bytes of operand 1 and store the result in operand 0.
3215
3216 (define_insn "revb_2h"
3217 [(set (match_operand:SI 0 "register_operand" "=r")
3218 (rotatert:SI (bswap:SI (match_operand:SI 1 "register_operand" "r"))
3219 (const_int 16)))]
3220 ""
3221 "revb.2h\t%0,%1"
3222 [(set_attr "type" "shift")])
3223
3224 (define_insn "revb_2h_extend"
3225 [(set (match_operand:DI 0 "register_operand" "=r")
3226 (sign_extend:DI
3227 (rotatert:SI
3228 (bswap:SI (match_operand:SI 1 "register_operand" "r"))
3229 (const_int 16))))]
3230 "TARGET_64BIT"
3231 "revb.2h\t%0,%1"
3232 [(set_attr "type" "shift")])
3233
3234 (define_insn "bswaphi2"
3235 [(set (match_operand:HI 0 "register_operand" "=r")
3236 (bswap:HI (match_operand:HI 1 "register_operand" "r")))]
3237 ""
3238 "revb.2h\t%0,%1"
3239 [(set_attr "type" "shift")])
3240
3241 (define_insn "revb_2w"
3242 [(set (match_operand:DI 0 "register_operand" "=r")
3243 (rotatert:DI (bswap:DI (match_operand:DI 1 "register_operand" "r"))
3244 (const_int 32)))]
3245 "TARGET_64BIT"
3246 "revb.2w\t%0,%1"
3247 [(set_attr "type" "shift")])
3248
3249 (define_insn "*bswapsi2"
3250 [(set (match_operand:SI 0 "register_operand" "=r")
3251 (bswap:SI (match_operand:SI 1 "register_operand" "r")))]
3252 "TARGET_64BIT"
3253 "revb.2w\t%0,%1"
3254 [(set_attr "type" "shift")])
3255
3256 (define_expand "bswapsi2"
3257 [(set (match_operand:SI 0 "register_operand" "=r")
3258 (bswap:SI (match_operand:SI 1 "register_operand" "r")))]
3259 ""
3260 {
3261 if (!TARGET_64BIT)
3262 {
3263 rtx t = gen_reg_rtx (SImode);
3264 emit_insn (gen_revb_2h (t, operands[1]));
3265 emit_insn (gen_rotrsi3 (operands[0], t, GEN_INT (16)));
3266 DONE;
3267 }
3268 })
3269
3270 (define_insn "bswapdi2"
3271 [(set (match_operand:DI 0 "register_operand" "=r")
3272 (bswap:DI (match_operand:DI 1 "register_operand" "r")))]
3273 "TARGET_64BIT"
3274 "revb.d\t%0,%1"
3275 [(set_attr "type" "shift")])
3276
3277
3278 ;;
3279 ;; ....................
3280 ;;
3281 ;; CONDITIONAL BRANCHES
3282 ;;
3283 ;; ....................
3284
3285 ;; Conditional branches
3286
3287 (define_insn "*branch_fp_FCCmode"
3288 [(set (pc)
3289 (if_then_else
3290 (match_operator 1 "equality_operator"
3291 [(match_operand:FCC 2 "register_operand" "z")
3292 (const_int 0)])
3293 (label_ref (match_operand 0 "" ""))
3294 (pc)))]
3295 "TARGET_HARD_FLOAT"
3296 {
3297 return loongarch_output_conditional_branch (insn, operands,
3298 LARCH_BRANCH ("b%F1", "%Z2%0"),
3299 LARCH_BRANCH ("b%W1", "%Z2%0"));
3300 }
3301 [(set_attr "type" "branch")])
3302
3303 (define_insn "*branch_fp_inverted_FCCmode"
3304 [(set (pc)
3305 (if_then_else
3306 (match_operator 1 "equality_operator"
3307 [(match_operand:FCC 2 "register_operand" "z")
3308 (const_int 0)])
3309 (pc)
3310 (label_ref (match_operand 0 "" ""))))]
3311 "TARGET_HARD_FLOAT"
3312 {
3313 return loongarch_output_conditional_branch (insn, operands,
3314 LARCH_BRANCH ("b%W1", "%Z2%0"),
3315 LARCH_BRANCH ("b%F1", "%Z2%0"));
3316 }
3317 [(set_attr "type" "branch")])
3318
3319 ;; Conditional branches on ordered comparisons with zero.
3320
3321 (define_insn "*branch_order<mode>"
3322 [(set (pc)
3323 (if_then_else
3324 (match_operator 1 "order_operator"
3325 [(match_operand:X 2 "register_operand" "r,r")
3326 (match_operand:X 3 "reg_or_0_operand" "J,r")])
3327 (label_ref (match_operand 0 "" ""))
3328 (pc)))]
3329 ""
3330 { return loongarch_output_order_conditional_branch (insn, operands, false); }
3331 [(set_attr "type" "branch")])
3332
3333 (define_insn "*branch_order<mode>_inverted"
3334 [(set (pc)
3335 (if_then_else
3336 (match_operator 1 "order_operator"
3337 [(match_operand:X 2 "register_operand" "r,r")
3338 (match_operand:X 3 "reg_or_0_operand" "J,r")])
3339 (pc)
3340 (label_ref (match_operand 0 "" ""))))]
3341 ""
3342 { return loongarch_output_order_conditional_branch (insn, operands, true); }
3343 [(set_attr "type" "branch")])
3344
3345 ;; Conditional branch on equality comparison.
3346
3347 (define_insn "branch_equality<mode>"
3348 [(set (pc)
3349 (if_then_else
3350 (match_operator 1 "equality_operator"
3351 [(match_operand:X 2 "register_operand" "r")
3352 (match_operand:X 3 "reg_or_0_operand" "rJ")])
3353 (label_ref (match_operand 0 "" ""))
3354 (pc)))]
3355 ""
3356 { return loongarch_output_equal_conditional_branch (insn, operands, false); }
3357 [(set_attr "type" "branch")])
3358
3359
3360 (define_insn "*branch_equality<mode>_inverted"
3361 [(set (pc)
3362 (if_then_else
3363 (match_operator 1 "equality_operator"
3364 [(match_operand:X 2 "register_operand" "r")
3365 (match_operand:X 3 "reg_or_0_operand" "rJ")])
3366 (pc)
3367 (label_ref (match_operand 0 "" ""))))]
3368 ""
3369 { return loongarch_output_equal_conditional_branch (insn, operands, true); }
3370 [(set_attr "type" "branch")])
3371
3372
3373 ;; Branches operate on GRLEN-sized quantities, but for LoongArch64 we accept
3374 ;; QImode values so we can force zero-extension.
3375 (define_mode_iterator BR [(QI "TARGET_64BIT") SI (DI "TARGET_64BIT")])
3376
3377 (define_expand "@cbranch<mode>4"
3378 [(set (pc)
3379 (if_then_else (match_operator 0 "comparison_operator"
3380 [(match_operand:BR 1 "register_operand")
3381 (match_operand:BR 2 "nonmemory_operand")])
3382 (label_ref (match_operand 3 ""))
3383 (pc)))]
3384 ""
3385 {
3386 loongarch_expand_conditional_branch (operands);
3387 DONE;
3388 })
3389
3390 (define_expand "cbranch<mode>4"
3391 [(set (pc)
3392 (if_then_else (match_operator 0 "comparison_operator"
3393 [(match_operand:ANYF 1 "register_operand")
3394 (match_operand:ANYF 2 "register_operand")])
3395 (label_ref (match_operand 3 ""))
3396 (pc)))]
3397 ""
3398 {
3399 loongarch_expand_conditional_branch (operands);
3400 DONE;
3401 })
3402
3403 ;; Used to implement built-in functions.
3404 (define_expand "condjump"
3405 [(set (pc)
3406 (if_then_else (match_operand 0)
3407 (label_ref (match_operand 1))
3408 (pc)))])
3409
3410
3411 ;;
3412 ;; ....................
3413 ;;
3414 ;; SETTING A REGISTER FROM A COMPARISON
3415 ;;
3416 ;; ....................
3417
3418 ;; Destination is always set in SI mode.
3419
3420 (define_expand "cstore<mode>4"
3421 [(set (match_operand:SI 0 "register_operand")
3422 (match_operator:SI 1 "loongarch_cstore_operator"
3423 [(match_operand:GPR 2 "register_operand")
3424 (match_operand:GPR 3 "nonmemory_operand")]))]
3425 ""
3426 {
3427 loongarch_expand_scc (operands);
3428 DONE;
3429 })
3430
3431 (define_insn "*seq_zero_<X:mode><GPR:mode>"
3432 [(set (match_operand:GPR 0 "register_operand" "=r")
3433 (eq:GPR (match_operand:X 1 "register_operand" "r")
3434 (const_int 0)))]
3435 ""
3436 "sltui\t%0,%1,1"
3437 [(set_attr "type" "slt")
3438 (set_attr "mode" "<X:MODE>")])
3439
3440
3441 (define_insn "*sne_zero_<X:mode><GPR:mode>"
3442 [(set (match_operand:GPR 0 "register_operand" "=r")
3443 (ne:GPR (match_operand:X 1 "register_operand" "r")
3444 (const_int 0)))]
3445 ""
3446 "sltu\t%0,%.,%1"
3447 [(set_attr "type" "slt")
3448 (set_attr "mode" "<X:MODE>")])
3449
3450 (define_insn "*sgt<u>_<X:mode><GPR:mode>"
3451 [(set (match_operand:GPR 0 "register_operand" "=r")
3452 (any_gt:GPR (match_operand:X 1 "register_operand" "r")
3453 (match_operand:X 2 "reg_or_0_operand" "rJ")))]
3454 ""
3455 "slt<u>\t%0,%z2,%1"
3456 [(set_attr "type" "slt")
3457 (set_attr "mode" "<X:MODE>")])
3458
3459 (define_insn "*slt<u>_<X:mode><GPR:mode>"
3460 [(set (match_operand:GPR 0 "register_operand" "=r")
3461 (any_lt:GPR (match_operand:X 1 "register_operand" "r")
3462 (match_operand:X 2 "arith_operand" "rI")))]
3463 ""
3464 "slt<u>%i2\t%0,%1,%2";
3465 [(set_attr "type" "slt")
3466 (set_attr "mode" "<X:MODE>")])
3467
3468 (define_insn "*sle<u>_<X:mode><GPR:mode>"
3469 [(set (match_operand:GPR 0 "register_operand" "=r")
3470 (any_le:GPR (match_operand:X 1 "register_operand" "r")
3471 (match_operand:X 2 "sle_operand" "")))]
3472 ""
3473 {
3474 operands[2] = GEN_INT (INTVAL (operands[2]) + 1);
3475 return "slt<u>i\t%0,%1,%2";
3476 }
3477 [(set_attr "type" "slt")
3478 (set_attr "mode" "<X:MODE>")])
3479
3480
3481 ;;
3482 ;; ....................
3483 ;;
3484 ;; FLOATING POINT COMPARISONS
3485 ;;
3486 ;; ....................
3487
3488 (define_insn "s<code>_<ANYF:mode>_using_FCCmode"
3489 [(set (match_operand:FCC 0 "register_operand" "=z")
3490 (fcond:FCC (match_operand:ANYF 1 "register_operand" "f")
3491 (match_operand:ANYF 2 "register_operand" "f")))]
3492 ""
3493 "fcmp.<fcond>.<fmt>\t%Z0%1,%2"
3494 [(set_attr "type" "fcmp")
3495 (set_attr "mode" "FCC")])
3496
3497
3498 ;;
3499 ;; ....................
3500 ;;
3501 ;; UNCONDITIONAL BRANCHES
3502 ;;
3503 ;; ....................
3504
3505 ;; Unconditional branches.
3506
3507 (define_expand "jump"
3508 [(set (pc)
3509 (label_ref (match_operand 0)))])
3510
3511 (define_insn "*jump_absolute"
3512 [(set (pc)
3513 (label_ref (match_operand 0)))]
3514 "!flag_pic"
3515 {
3516 return "b\t%l0";
3517 }
3518 [(set_attr "type" "branch")])
3519
3520 (define_insn "*jump_pic"
3521 [(set (pc)
3522 (label_ref (match_operand 0)))]
3523 "flag_pic"
3524 {
3525 return "b\t%0";
3526 }
3527 [(set_attr "type" "branch")])
3528
3529 ;; Micro-architecture unconditionally treats a "jr $ra" as "return from subroutine",
3530 ;; non-returning indirect jumps through $ra would interfere with both subroutine
3531 ;; return prediction and the more general indirect branch prediction.
3532
3533 (define_expand "indirect_jump"
3534 [(set (pc) (match_operand 0 "register_operand"))]
3535 ""
3536 {
3537 operands[0] = force_reg (Pmode, operands[0]);
3538 emit_jump_insn (gen_indirect_jump (Pmode, operands[0]));
3539 DONE;
3540 })
3541
3542 (define_insn "@indirect_jump<mode>"
3543 [(set (pc) (match_operand:P 0 "register_operand" "e"))]
3544 ""
3545 "jr\t%0"
3546 [(set_attr "type" "jump")
3547 (set_attr "mode" "none")])
3548
3549 (define_expand "tablejump"
3550 [(set (pc)
3551 (match_operand 0 "register_operand"))
3552 (use (label_ref (match_operand 1 "")))]
3553 ""
3554 {
3555 if (flag_pic)
3556 operands[0] = expand_simple_binop (Pmode, PLUS, operands[0],
3557 gen_rtx_LABEL_REF (Pmode,
3558 operands[1]),
3559 NULL_RTX, 0, OPTAB_DIRECT);
3560 emit_jump_insn (gen_tablejump (Pmode, operands[0], operands[1]));
3561 DONE;
3562 })
3563
3564 (define_mode_attr mode_size [(DI "8") (SI "4")])
3565
3566 (define_insn "@tablejump<mode>"
3567 [(set (pc)
3568 (match_operand:P 0 "register_operand" "e"))
3569 (use (label_ref (match_operand 1 "" "")))]
3570 ""
3571 {
3572 return TARGET_ANNOTATE_TABLEJUMP
3573 ? "1:jr\t%0\n\t"
3574 ".pushsection\t.discard.tablejump_annotate\n\t"
3575 "\t.<mode_size>byte\t1b\n\t"
3576 "\t.<mode_size>byte\t%1\n\t"
3577 ".popsection"
3578 : "jr\t%0";
3579 }
3580 [(set_attr "type" "jump")
3581 (set_attr "mode" "none")])
3582
3583
3584
3585 ;;
3586 ;; ....................
3587 ;;
3588 ;; Function prologue/epilogue
3589 ;;
3590 ;; ....................
3591 ;;
3592
3593 (define_expand "prologue"
3594 [(const_int 1)]
3595 ""
3596 {
3597 loongarch_expand_prologue ();
3598 DONE;
3599 })
3600
3601 ;; Block any insns from being moved before this point, since the
3602 ;; profiling call to mcount can use various registers that aren't
3603 ;; saved or used to pass arguments.
3604
3605 (define_insn "blockage"
3606 [(unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)]
3607 ""
3608 ""
3609 [(set_attr "type" "ghost")
3610 (set_attr "mode" "none")])
3611
3612 (define_insn "@probe_stack_range<P:mode>"
3613 [(set (match_operand:P 0 "register_operand" "=r")
3614 (unspec_volatile:P [(match_operand:P 1 "register_operand" "0")
3615 (match_operand:P 2 "register_operand" "r")
3616 (match_operand:P 3 "register_operand" "r")]
3617 UNSPECV_PROBE_STACK_RANGE))]
3618 ""
3619 {
3620 return loongarch_output_probe_stack_range (operands[0],
3621 operands[2],
3622 operands[3]);
3623 }
3624 [(set_attr "type" "unknown")
3625 (set_attr "mode" "<MODE>")])
3626
3627 (define_expand "epilogue"
3628 [(const_int 2)]
3629 ""
3630 {
3631 loongarch_expand_epilogue (NORMAL_RETURN);
3632 DONE;
3633 })
3634
3635 (define_expand "sibcall_epilogue"
3636 [(const_int 2)]
3637 ""
3638 {
3639 loongarch_expand_epilogue (SIBCALL_RETURN);
3640 DONE;
3641 })
3642
3643 ;; Trivial return. Make it look like a normal return insn as that
3644 ;; allows jump optimizations to work better.
3645
3646 (define_expand "return"
3647 [(simple_return)]
3648 "loongarch_can_use_return_insn ()"
3649 { })
3650
3651 (define_expand "simple_return"
3652 [(simple_return)]
3653 ""
3654 { })
3655
3656 (define_insn "*<optab>"
3657 [(any_return)]
3658 ""
3659 {
3660 operands[0] = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
3661 return "jr\t%0";
3662 }
3663 [(set_attr "type" "jump")
3664 (set_attr "mode" "none")])
3665
3666 ;; Normal return.
3667
3668 (define_insn "<optab>_internal"
3669 [(any_return)
3670 (use (match_operand 0 "pmode_register_operand" ""))]
3671 ""
3672 "jr\t%0"
3673 [(set_attr "type" "jump")
3674 (set_attr "mode" "none")])
3675
3676 ;; Exception return.
3677 (define_insn "loongarch_ertn"
3678 [(return)
3679 (unspec_volatile [(const_int 0)] UNSPECV_ERTN)]
3680 ""
3681 "ertn"
3682 [(set_attr "type" "trap")
3683 (set_attr "mode" "none")])
3684
3685 ;; This is used in compiling the unwind routines.
3686 (define_expand "eh_return"
3687 [(use (match_operand 0 "general_operand"))]
3688 ""
3689 {
3690 if (GET_MODE (operands[0]) != word_mode)
3691 operands[0] = convert_to_mode (word_mode, operands[0], 0);
3692 if (TARGET_64BIT)
3693 emit_insn (gen_eh_set_ra_di (operands[0]));
3694 else
3695 emit_insn (gen_eh_set_ra_si (operands[0]));
3696
3697 emit_jump_insn (gen_eh_return_internal ());
3698 emit_barrier ();
3699 DONE;
3700 })
3701
3702 (define_insn_and_split "eh_return_internal"
3703 [(eh_return)]
3704 ""
3705 "#"
3706 "epilogue_completed"
3707 [(const_int 0)]
3708 {
3709 loongarch_expand_epilogue (EXCEPTION_RETURN);
3710 DONE;
3711 })
3712
3713 ;; Clobber the return address on the stack. We can't expand this
3714 ;; until we know where it will be put in the stack frame.
3715
3716 (define_insn "eh_set_ra_si"
3717 [(unspec [(match_operand:SI 0 "register_operand" "r")] UNSPEC_EH_RETURN)
3718 (clobber (match_scratch:SI 1 "=&r"))]
3719 "! TARGET_64BIT"
3720 "#")
3721
3722 (define_insn "eh_set_ra_di"
3723 [(unspec [(match_operand:DI 0 "register_operand" "r")] UNSPEC_EH_RETURN)
3724 (clobber (match_scratch:DI 1 "=&r"))]
3725 "TARGET_64BIT"
3726 "#")
3727
3728 (define_split
3729 [(unspec [(match_operand 0 "register_operand")] UNSPEC_EH_RETURN)
3730 (clobber (match_scratch 1))]
3731 "reload_completed"
3732 [(const_int 0)]
3733 {
3734 loongarch_set_return_address (operands[0], operands[1]);
3735 DONE;
3736 })
3737
3738
3739
3740 ;;
3741 ;; ....................
3742 ;;
3743 ;; FUNCTION CALLS
3744 ;;
3745 ;; ....................
3746
3747 ;; Sibling calls. All these patterns use jump instructions.
3748
3749 (define_expand "sibcall"
3750 [(parallel [(call (match_operand 0 "")
3751 (match_operand 1 ""))
3752 (use (match_operand 2 "")) ;; next_arg_reg
3753 (use (match_operand 3 ""))])] ;; struct_value_size_rtx
3754 ""
3755 {
3756 rtx target = loongarch_legitimize_call_address (XEXP (operands[0], 0));
3757
3758 if (GET_CODE (target) == LO_SUM)
3759 emit_call_insn (gen_sibcall_internal_1 (Pmode, XEXP (target, 0),
3760 XEXP (target, 1),
3761 operands[1]));
3762 else
3763 {
3764 rtx call = emit_call_insn (gen_sibcall_internal (target, operands[1]));
3765
3766 if (TARGET_CMODEL_MEDIUM && !REG_P (target))
3767 clobber_reg (&CALL_INSN_FUNCTION_USAGE (call),
3768 gen_rtx_REG (Pmode, T0_REGNUM));
3769 }
3770 DONE;
3771 })
3772
3773 (define_insn "sibcall_internal"
3774 [(call (mem:SI (match_operand 0 "call_insn_operand" "j,c,b"))
3775 (match_operand 1 "" ""))]
3776 "SIBLING_CALL_P (insn)"
3777 {
3778 switch (which_alternative)
3779 {
3780 case 0:
3781 return "jr\t%0";
3782 case 1:
3783 if (TARGET_CMODEL_MEDIUM)
3784 return "pcaddu18i\t$r12,%%call36(%0)\n\tjirl\t$r0,$r12,0";
3785 else
3786 return "b\t%0";
3787 case 2:
3788 if (TARGET_CMODEL_MEDIUM)
3789 return "pcaddu18i\t$r12,%%call36(%0)\n\tjirl\t$r0,$r12,0";
3790 else
3791 return "b\t%%plt(%0)";
3792 default:
3793 gcc_unreachable ();
3794 }
3795 }
3796 [(set_attr "jirl" "indirect,direct,direct")])
3797
3798 (define_insn "@sibcall_internal_1<mode>"
3799 [(call (mem:P (lo_sum:P (match_operand:P 0 "register_operand" "j")
3800 (match_operand:P 1 "symbolic_operand" "")))
3801 (match_operand 2 "" ""))]
3802 "SIBLING_CALL_P (insn) && TARGET_CMODEL_MEDIUM"
3803 "jirl\t$r0,%0,%%pc_lo12(%1)"
3804 [(set_attr "jirl" "indirect")])
3805
3806 (define_expand "sibcall_value"
3807 [(parallel [(set (match_operand 0 "")
3808 (call (match_operand 1 "")
3809 (match_operand 2 "")))
3810 (use (match_operand 3 ""))])] ;; next_arg_reg
3811 ""
3812 {
3813 rtx target = loongarch_legitimize_call_address (XEXP (operands[1], 0));
3814
3815 /* Handle return values created by loongarch_pass_fpr_pair. */
3816 if (GET_CODE (operands[0]) == PARALLEL && XVECLEN (operands[0], 0) == 2)
3817 {
3818 rtx arg1 = XEXP (XVECEXP (operands[0],0, 0), 0);
3819 rtx arg2 = XEXP (XVECEXP (operands[0],0, 1), 0);
3820
3821 if (GET_CODE (target) == LO_SUM)
3822 emit_call_insn (gen_sibcall_value_multiple_internal_1 (Pmode, arg1,
3823 XEXP (target, 0),
3824 XEXP (target, 1),
3825 operands[2],
3826 arg2));
3827 else
3828 {
3829 rtx call
3830 = emit_call_insn (gen_sibcall_value_multiple_internal (arg1,
3831 target,
3832 operands[2],
3833 arg2));
3834
3835 if (TARGET_CMODEL_MEDIUM && !REG_P (target))
3836 clobber_reg (&CALL_INSN_FUNCTION_USAGE (call),
3837 gen_rtx_REG (Pmode, T0_REGNUM));
3838 }
3839 }
3840 else
3841 {
3842 /* Handle return values created by loongarch_return_fpr_single. */
3843 if (GET_CODE (operands[0]) == PARALLEL && XVECLEN (operands[0], 0) == 1)
3844 operands[0] = XEXP (XVECEXP (operands[0], 0, 0), 0);
3845
3846 if (GET_CODE (target) == LO_SUM)
3847 emit_call_insn (gen_sibcall_value_internal_1 (Pmode, operands[0],
3848 XEXP (target, 0),
3849 XEXP (target, 1),
3850 operands[2]));
3851 else
3852 {
3853 rtx call = emit_call_insn (gen_sibcall_value_internal (operands[0],
3854 target,
3855 operands[2]));
3856
3857 if (TARGET_CMODEL_MEDIUM && !REG_P (target))
3858 clobber_reg (&CALL_INSN_FUNCTION_USAGE (call),
3859 gen_rtx_REG (Pmode, T0_REGNUM));
3860 }
3861 }
3862 DONE;
3863 })
3864
3865 (define_insn "sibcall_value_internal"
3866 [(set (match_operand 0 "register_operand" "")
3867 (call (mem:SI (match_operand 1 "call_insn_operand" "j,c,b"))
3868 (match_operand 2 "" "")))]
3869 "SIBLING_CALL_P (insn)"
3870 {
3871 switch (which_alternative)
3872 {
3873 case 0:
3874 return "jr\t%1";
3875 case 1:
3876 if (TARGET_CMODEL_MEDIUM)
3877 return "pcaddu18i\t$r12,%%call36(%1)\n\tjirl\t$r0,$r12,0";
3878 else
3879 return "b\t%1";
3880 case 2:
3881 if (TARGET_CMODEL_MEDIUM)
3882 return "pcaddu18i\t$r12,%%call36(%1)\n\tjirl\t$r0,$r12,0";
3883 else
3884 return "b\t%%plt(%1)";
3885 default:
3886 gcc_unreachable ();
3887 }
3888 }
3889 [(set_attr "jirl" "indirect,direct,direct")])
3890
3891 (define_insn "@sibcall_value_internal_1<mode>"
3892 [(set (match_operand 0 "register_operand" "")
3893 (call (mem:P (lo_sum:P (match_operand:P 1 "register_operand" "j")
3894 (match_operand:P 2 "symbolic_operand" "")))
3895 (match_operand 3 "" "")))]
3896 "SIBLING_CALL_P (insn) && TARGET_CMODEL_MEDIUM"
3897 "jirl\t$r0,%1,%%pc_lo12(%2)"
3898 [(set_attr "jirl" "indirect")])
3899
3900 (define_insn "sibcall_value_multiple_internal"
3901 [(set (match_operand 0 "register_operand" "")
3902 (call (mem:SI (match_operand 1 "call_insn_operand" "j,c,b"))
3903 (match_operand 2 "" "")))
3904 (set (match_operand 3 "register_operand" "")
3905 (call (mem:SI (match_dup 1))
3906 (match_dup 2)))]
3907 "SIBLING_CALL_P (insn)"
3908 {
3909 switch (which_alternative)
3910 {
3911 case 0:
3912 return "jr\t%1";
3913 case 1:
3914 if (TARGET_CMODEL_MEDIUM)
3915 return "pcaddu18i\t$r12,%%call36(%1)\n\tjirl\t$r0,$r12,0";
3916 else
3917 return "b\t%1";
3918 case 2:
3919 if (TARGET_CMODEL_MEDIUM)
3920 return "pcaddu18i\t$r12,%%call36(%1)\n\tjirl\t$r0,$r12,0";
3921 else
3922 return "b\t%%plt(%1)";
3923 default:
3924 gcc_unreachable ();
3925 }
3926 }
3927 [(set_attr "jirl" "indirect,direct,direct")])
3928
3929 (define_insn "@sibcall_value_multiple_internal_1<mode>"
3930 [(set (match_operand 0 "register_operand" "")
3931 (call (mem:P (unspec:P [(match_operand:P 1 "register_operand" "j")
3932 (match_operand:P 2 "symbolic_operand" "")]
3933 UNSPEC_SIBCALL_VALUE_MULTIPLE_INTERNAL_1))
3934 (match_operand 3 "" "")))
3935 (set (match_operand 4 "register_operand" "")
3936 (call (mem:P (unspec:P [(match_dup 1)
3937 (match_dup 2)]
3938 UNSPEC_SIBCALL_VALUE_MULTIPLE_INTERNAL_1))
3939 (match_dup 3)))]
3940 "SIBLING_CALL_P (insn) && TARGET_CMODEL_MEDIUM"
3941 "jirl\t$r0,%1,%%pc_lo12(%2)"
3942 [(set_attr "jirl" "indirect")])
3943
3944 (define_expand "call"
3945 [(parallel [(call (match_operand 0 "")
3946 (match_operand 1 ""))
3947 (use (match_operand 2 "")) ;; next_arg_reg
3948 (use (match_operand 3 ""))])] ;; struct_value_size_rtx
3949 ""
3950 {
3951 rtx target = loongarch_legitimize_call_address (XEXP (operands[0], 0));
3952
3953 if (GET_CODE (target) == LO_SUM)
3954 emit_call_insn (gen_call_internal_1 (Pmode, XEXP (target, 0),
3955 XEXP (target, 1), operands[1]));
3956 else
3957 emit_call_insn (gen_call_internal (target, operands[1]));
3958 DONE;
3959 })
3960
3961 (define_insn "call_internal"
3962 [(call (mem:SI (match_operand 0 "call_insn_operand" "e,c,b"))
3963 (match_operand 1 "" ""))
3964 (clobber (reg:SI RETURN_ADDR_REGNUM))]
3965 ""
3966 {
3967 switch (which_alternative)
3968 {
3969 case 0:
3970 return "jirl\t$r1,%0,0";
3971 case 1:
3972 if (TARGET_CMODEL_MEDIUM)
3973 return "pcaddu18i\t$r1,%%call36(%0)\n\tjirl\t$r1,$r1,0";
3974 else
3975 return "bl\t%0";
3976 case 2:
3977 if (TARGET_CMODEL_MEDIUM)
3978 return "pcaddu18i\t$r1,%%call36(%0)\n\tjirl\t$r1,$r1,0";
3979 else
3980 return "bl\t%%plt(%0)";
3981 default:
3982 gcc_unreachable ();
3983 }
3984 }
3985 [(set_attr "jirl" "indirect,direct,direct")])
3986
3987 (define_insn "@call_internal_1<mode>"
3988 [(call (mem:P (lo_sum:P (match_operand:P 0 "register_operand" "j")
3989 (match_operand:P 1 "symbolic_operand" "")))
3990 (match_operand 2 "" ""))
3991 (clobber (reg:SI RETURN_ADDR_REGNUM))]
3992 "TARGET_CMODEL_MEDIUM"
3993 "jirl\t$r1,%0,%%pc_lo12(%1)"
3994 [(set_attr "jirl" "indirect")])
3995
3996 (define_expand "call_value"
3997 [(parallel [(set (match_operand 0 "")
3998 (call (match_operand 1 "")
3999 (match_operand 2 "")))
4000 (use (match_operand 3 ""))])] ;; next_arg_reg
4001 ""
4002 {
4003 rtx target = loongarch_legitimize_call_address (XEXP (operands[1], 0));
4004 /* Handle return values created by loongarch_pass_fpr_pair. */
4005 if (GET_CODE (operands[0]) == PARALLEL && XVECLEN (operands[0], 0) == 2)
4006 {
4007 rtx arg1 = XEXP (XVECEXP (operands[0], 0, 0), 0);
4008 rtx arg2 = XEXP (XVECEXP (operands[0], 0, 1), 0);
4009
4010 if (GET_CODE (target) == LO_SUM)
4011 emit_call_insn (gen_call_value_multiple_internal_1 (Pmode, arg1,
4012 XEXP (target, 0),
4013 XEXP (target, 1),
4014 operands[2], arg2));
4015 else
4016 emit_call_insn (gen_call_value_multiple_internal (arg1, target,
4017 operands[2], arg2));
4018 }
4019 else
4020 {
4021 /* Handle return values created by loongarch_return_fpr_single. */
4022 if (GET_CODE (operands[0]) == PARALLEL && XVECLEN (operands[0], 0) == 1)
4023 operands[0] = XEXP (XVECEXP (operands[0], 0, 0), 0);
4024
4025 if (GET_CODE (target) == LO_SUM)
4026 emit_call_insn (gen_call_value_internal_1 (Pmode, operands[0],
4027 XEXP (target, 0),
4028 XEXP (target, 1),
4029 operands[2]));
4030 else
4031 emit_call_insn (gen_call_value_internal (operands[0], target,
4032 operands[2]));
4033 }
4034 DONE;
4035 })
4036
4037 (define_insn "call_value_internal"
4038 [(set (match_operand 0 "register_operand" "")
4039 (call (mem:SI (match_operand 1 "call_insn_operand" "e,c,b"))
4040 (match_operand 2 "" "")))
4041 (clobber (reg:SI RETURN_ADDR_REGNUM))]
4042 ""
4043 {
4044 switch (which_alternative)
4045 {
4046 case 0:
4047 return "jirl\t$r1,%1,0";
4048 case 1:
4049 if (TARGET_CMODEL_MEDIUM)
4050 return "pcaddu18i\t$r1,%%call36(%1)\n\tjirl\t$r1,$r1,0";
4051 else
4052 return "bl\t%1";
4053 case 2:
4054 if (TARGET_CMODEL_MEDIUM)
4055 return "pcaddu18i\t$r1,%%call36(%1)\n\tjirl\t$r1,$r1,0";
4056 else
4057 return "bl\t%%plt(%1)";
4058 default:
4059 gcc_unreachable ();
4060 }
4061 }
4062 [(set_attr "jirl" "indirect,direct,direct")])
4063
4064 (define_insn "@call_value_internal_1<mode>"
4065 [(set (match_operand 0 "register_operand" "")
4066 (call (mem:P (lo_sum:P (match_operand:P 1 "register_operand" "j")
4067 (match_operand:P 2 "symbolic_operand" "")))
4068 (match_operand 3 "" "")))
4069 (clobber (reg:SI RETURN_ADDR_REGNUM))]
4070 "TARGET_CMODEL_MEDIUM"
4071 "jirl\t$r1,%1,%%pc_lo12(%2)"
4072 [(set_attr "jirl" "indirect")])
4073
4074 (define_insn "call_value_multiple_internal"
4075 [(set (match_operand 0 "register_operand" "")
4076 (call (mem:SI (match_operand 1 "call_insn_operand" "e,c,b"))
4077 (match_operand 2 "" "")))
4078 (set (match_operand 3 "register_operand" "")
4079 (call (mem:SI (match_dup 1))
4080 (match_dup 2)))
4081 (clobber (reg:SI RETURN_ADDR_REGNUM))]
4082 ""
4083 {
4084 switch (which_alternative)
4085 {
4086 case 0:
4087 return "jirl\t$r1,%1,0";
4088 case 1:
4089 if (TARGET_CMODEL_MEDIUM)
4090 return "pcaddu18i\t$r1,%%call36(%1)\n\tjirl\t$r1,$r1,0";
4091 else
4092 return "bl\t%1";
4093 case 2:
4094 if (TARGET_CMODEL_MEDIUM)
4095 return "pcaddu18i\t$r1,%%call36(%1)\n\tjirl\t$r1,$r1,0";
4096 else
4097 return "bl\t%%plt(%1)";
4098 default:
4099 gcc_unreachable ();
4100 }
4101 }
4102 [(set_attr "jirl" "indirect,direct,direct")])
4103
4104 (define_insn "@call_value_multiple_internal_1<mode>"
4105 [(set (match_operand 0 "register_operand" "")
4106 (call (mem:P (unspec:P [(match_operand:P 1 "register_operand" "j")
4107 (match_operand:P 2 "symbolic_operand" "")]
4108 UNSPEC_CALL_VALUE_MULTIPLE_INTERNAL_1))
4109 (match_operand 3 "" "")))
4110 (set (match_operand 4 "register_operand" "")
4111 (call (mem:P (unspec:P [(match_dup 1)
4112 (match_dup 2)]
4113 UNSPEC_CALL_VALUE_MULTIPLE_INTERNAL_1))
4114 (match_dup 3)))
4115 (clobber (reg:SI RETURN_ADDR_REGNUM))]
4116 "TARGET_CMODEL_MEDIUM"
4117 "jirl\t$r1,%1,%%pc_lo12(%2)"
4118 [(set_attr "jirl" "indirect")])
4119
4120
4121 ;; Call subroutine returning any type.
4122 (define_expand "untyped_call"
4123 [(parallel [(call (match_operand 0 "")
4124 (const_int 0))
4125 (match_operand 1 "")
4126 (match_operand 2 "")])]
4127 ""
4128 {
4129 int i;
4130
4131 emit_call_insn (gen_call (operands[0], const0_rtx, NULL, const0_rtx));
4132
4133 for (i = 0; i < XVECLEN (operands[2], 0); i++)
4134 {
4135 rtx set = XVECEXP (operands[2], 0, i);
4136 loongarch_emit_move (SET_DEST (set), SET_SRC (set));
4137 }
4138
4139 emit_insn (gen_blockage ());
4140 DONE;
4141 })
4142
4143 ;;
4144 ;; ....................
4145 ;;
4146 ;; MISC.
4147 ;;
4148 ;; ....................
4149 ;;
4150
4151 (define_insn "prefetch"
4152 [(prefetch (match_operand 0 "address_operand" "ZD")
4153 (match_operand 1 "const_int_operand" "n")
4154 (match_operand 2 "const_int_operand" "n"))]
4155 ""
4156 {
4157 switch (INTVAL (operands[1]))
4158 {
4159 case 0:
4160 case 2: return "preld\t0,%a0";
4161 case 1: return "preld\t8,%a0";
4162 default: gcc_unreachable ();
4163 }
4164 })
4165
4166 (define_insn "nop"
4167 [(const_int 0)]
4168 ""
4169 "nop"
4170 [(set_attr "type" "nop")
4171 (set_attr "mode" "none")])
4172
4173 ;; __builtin_loongarch_movfcsr2gr: move the FCSR into operand 0.
4174 (define_insn "loongarch_movfcsr2gr"
4175 [(set (match_operand:SI 0 "register_operand" "=r")
4176 (unspec_volatile:SI [(match_operand 1 "const_uimm5_operand")]
4177 UNSPECV_MOVFCSR2GR))]
4178 "TARGET_HARD_FLOAT"
4179 "movfcsr2gr\t%0,$r%1")
4180
4181 ;; __builtin_loongarch_movgr2fcsr: move operand 0 into the FCSR.
4182 (define_insn "loongarch_movgr2fcsr"
4183 [(unspec_volatile [(match_operand 0 "const_uimm5_operand")
4184 (match_operand:SI 1 "register_operand" "r")]
4185 UNSPECV_MOVGR2FCSR)]
4186 "TARGET_HARD_FLOAT"
4187 "movgr2fcsr\t$r%0,%1")
4188
4189 (define_insn "fclass_<fmt>"
4190 [(set (match_operand:SI 0 "register_operand" "=f")
4191 (unspec:SI [(match_operand:ANYF 1 "register_operand" "f")]
4192 UNSPEC_FCLASS))]
4193 "TARGET_HARD_FLOAT"
4194 "fclass.<fmt>\t%0,%1"
4195 [(set_attr "type" "unknown")
4196 (set_attr "mode" "<MODE>")])
4197
4198 (define_int_iterator FCLASS_MASK [68 136 952])
4199 (define_int_attr fclass_optab
4200 [(68 "isinf")
4201 (136 "isnormal")
4202 (952 "isfinite")])
4203
4204 (define_expand "<FCLASS_MASK:fclass_optab><ANYF:mode>2"
4205 [(match_operand:SI 0 "register_operand" "=r")
4206 (match_operand:ANYF 1 "register_operand" " f")
4207 (const_int FCLASS_MASK)]
4208 "TARGET_HARD_FLOAT"
4209 {
4210 rtx ft0 = gen_reg_rtx (SImode);
4211 rtx t0 = gen_reg_rtx (word_mode);
4212 rtx mask = GEN_INT (<FCLASS_MASK>);
4213
4214 emit_insn (gen_fclass_<ANYF:fmt> (ft0, operands[1]));
4215
4216 if (TARGET_64BIT)
4217 emit_insn (gen_extend_insn (t0, ft0, DImode, SImode, 0));
4218 else
4219 emit_move_insn (t0, ft0);
4220
4221 emit_move_insn (t0, gen_rtx_AND (word_mode, t0, mask));
4222 emit_move_insn (t0, gen_rtx_NE (word_mode, t0, const0_rtx));
4223
4224 if (TARGET_64BIT)
4225 {
4226 t0 = lowpart_subreg (SImode, t0, DImode);
4227 SUBREG_PROMOTED_VAR_P (t0) = 1;
4228 SUBREG_PROMOTED_SET (t0, SRP_SIGNED);
4229 }
4230
4231 emit_move_insn (operands[0], t0);
4232
4233 DONE;
4234 })
4235
4236 (define_insn "*bytepick_w_<bytepick_imm>"
4237 [(set (match_operand:SI 0 "register_operand" "=r")
4238 (any_or_plus:SI
4239 (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
4240 (const_int <bytepick_w_lshiftrt_amount>))
4241 (ashift:SI (match_operand:SI 2 "register_operand" "r")
4242 (const_int bytepick_w_ashift_amount))))]
4243 ""
4244 "bytepick.w\t%0,%1,%2,<bytepick_imm>"
4245 [(set_attr "mode" "SI")])
4246
4247 (define_mode_attr bitsize [(QI "8") (HI "16")])
4248 (define_mode_attr bytepick_imm [(QI "3") (HI "2")])
4249 (define_mode_attr bytepick_w_ashift_amount [(QI "24") (HI "16")])
4250
4251 (define_insn "bytepick_w_<bytepick_imm>_extend"
4252 [(set (match_operand:DI 0 "register_operand" "=r")
4253 (ior:DI
4254 (ashift:DI
4255 (sign_extend:DI
4256 (subreg:SHORT (match_operand:DI 1 "register_operand" "r") 0))
4257 (const_int <bytepick_w_ashift_amount>))
4258 (zero_extract:DI (match_operand:DI 2 "register_operand" "r")
4259 (const_int <bytepick_w_ashift_amount>)
4260 (const_int <bitsize>))))]
4261 "TARGET_64BIT"
4262 "bytepick.w\t%0,%2,%1,<bytepick_imm>"
4263 [(set_attr "mode" "SI")])
4264
4265 (define_insn "bytepick_w_1_extend"
4266 [(set (match_operand:DI 0 "register_operand" "=r")
4267 (ior:DI
4268 (ashift:DI
4269 (sign_extract:DI (match_operand:DI 1 "register_operand" "r")
4270 (const_int 24)
4271 (const_int 0))
4272 (const_int 8))
4273 (zero_extract:DI (match_operand:DI 2 "register_operand" "r")
4274 (const_int 8)
4275 (const_int 24))))]
4276 "TARGET_64BIT"
4277 "bytepick.w\t%0,%2,%1,1"
4278 [(set_attr "mode" "SI")])
4279
4280 (define_insn "*bytepick_d_<bytepick_imm>"
4281 [(set (match_operand:DI 0 "register_operand" "=r")
4282 (any_or_plus:DI
4283 (lshiftrt (match_operand:DI 1 "register_operand" "r")
4284 (const_int <bytepick_d_lshiftrt_amount>))
4285 (ashift (match_operand:DI 2 "register_operand" "r")
4286 (const_int bytepick_d_ashift_amount))))]
4287 "TARGET_64BIT"
4288 "bytepick.d\t%0,%1,%2,<bytepick_imm>"
4289 [(set_attr "mode" "DI")])
4290
4291 (define_insn "*bytepick_d_<bytepick_imm>_rev"
4292 [(set (match_operand:DI 0 "register_operand" "=r")
4293 (any_or_plus:DI
4294 (ashift (match_operand:DI 1 "register_operand" "r")
4295 (const_int bytepick_d_ashift_amount))
4296 (lshiftrt (match_operand:DI 2 "register_operand" "r")
4297 (const_int <bytepick_d_lshiftrt_amount>))))]
4298 "TARGET_64BIT"
4299 "bytepick.d\t%0,%2,%1,<bytepick_imm>"
4300 [(set_attr "mode" "DI")])
4301
4302 (define_insn "bitrev_4b"
4303 [(set (match_operand:SI 0 "register_operand" "=r")
4304 (unspec:SI [(match_operand:SI 1 "register_operand" "r")]
4305 UNSPEC_BITREV_4B))]
4306 ""
4307 "bitrev.4b\t%0,%1"
4308 [(set_attr "type" "unknown")
4309 (set_attr "mode" "SI")])
4310
4311 (define_insn "bitrev_8b"
4312 [(set (match_operand:DI 0 "register_operand" "=r")
4313 (unspec:DI [(match_operand:DI 1 "register_operand" "r")]
4314 UNSPEC_BITREV_8B))]
4315 ""
4316 "bitrev.8b\t%0,%1"
4317 [(set_attr "type" "unknown")
4318 (set_attr "mode" "DI")])
4319
4320 (define_insn "@rbit<mode>"
4321 [(set (match_operand:GPR 0 "register_operand" "=r")
4322 (bitreverse:GPR (match_operand:GPR 1 "register_operand" "r")))]
4323 ""
4324 "bitrev.<size>\t%0,%1"
4325 [(set_attr "type" "unknown")
4326 (set_attr "mode" "<MODE>")])
4327
4328 (define_insn "rbitsi_extended"
4329 [(set (match_operand:DI 0 "register_operand" "=r")
4330 (sign_extend:DI
4331 (bitreverse:SI (match_operand:SI 1 "register_operand" "r"))))]
4332 "TARGET_64BIT"
4333 "bitrev.w\t%0,%1"
4334 [(set_attr "type" "unknown")
4335 (set_attr "mode" "SI")])
4336
4337 ;; If we don't care high bits, bitrev.4b can reverse bits of values in
4338 ;; QImode.
4339 (define_insn "rbitqi"
4340 [(set (match_operand:QI 0 "register_operand" "=r")
4341 (bitreverse:QI (match_operand:QI 1 "register_operand" "r")))]
4342 ""
4343 "bitrev.4b\t%0,%1"
4344 [(set_attr "type" "unknown")
4345 (set_attr "mode" "SI")])
4346
4347 ;; For HImode it's a little complicated...
4348 (define_expand "rbithi"
4349 [(match_operand:HI 0 "register_operand")
4350 (match_operand:HI 1 "register_operand")]
4351 ""
4352 {
4353 rtx t = gen_reg_rtx (word_mode);
4354
4355 /* Oh, using paradoxical subreg. I learnt the trick from RISC-V,
4356 hoping we won't be blown up altogether one day. */
4357 emit_insn (gen_rbit(word_mode, t,
4358 gen_lowpart (word_mode, operands[1])));
4359 t = expand_simple_binop (word_mode, LSHIFTRT, t,
4360 GEN_INT (GET_MODE_BITSIZE (word_mode) - 16),
4361 NULL_RTX, false, OPTAB_DIRECT);
4362
4363 t = gen_lowpart (HImode, t);
4364 SUBREG_PROMOTED_VAR_P (t) = 1;
4365 SUBREG_PROMOTED_SET (t, SRP_UNSIGNED);
4366 emit_move_insn (operands[0], t);
4367
4368 DONE;
4369 })
4370
4371 (define_insn "@stack_tie<mode>"
4372 [(set (mem:BLK (scratch))
4373 (unspec:BLK [(match_operand:X 0 "register_operand" "r")
4374 (match_operand:X 1 "register_operand" "r")]
4375 UNSPEC_TIE))]
4376 ""
4377 ""
4378 [(set_attr "length" "0")
4379 (set_attr "type" "ghost")])
4380
4381 ;; Named pattern for expanding thread pointer reference.
4382 (define_expand "get_thread_pointer<mode>"
4383 [(set (match_operand:P 0 "register_operand" "=r")
4384 (reg:P TP_REGNUM))]
4385 "HAVE_AS_TLS"
4386 {})
4387
4388 (define_split
4389 [(match_operand 0 "small_data_pattern")]
4390 "reload_completed"
4391 [(match_dup 0)]
4392 { operands[0] = loongarch_rewrite_small_data (operands[0]); })
4393
4394
4395 ;; Match paired HI/SI/SF/DFmode load/stores.
4396 (define_insn "*join2_load_store<JOIN_MODE:mode>"
4397 [(set (match_operand:JOIN_MODE 0 "nonimmediate_operand"
4398 "=&r,f,m,m,&r,ZC")
4399 (match_operand:JOIN_MODE 1 "nonimmediate_operand" "m,m,r,f,ZC,r"))
4400 (set (match_operand:JOIN_MODE 2 "nonimmediate_operand"
4401 "=r,f,m,m,r,ZC")
4402 (match_operand:JOIN_MODE 3 "nonimmediate_operand" "m,m,r,f,ZC,r"))]
4403 "reload_completed"
4404 {
4405 /* The load destination does not overlap the source. */
4406 gcc_assert (!reg_overlap_mentioned_p (operands[0], operands[1]));
4407 output_asm_insn (loongarch_output_move (operands),
4408 operands);
4409 output_asm_insn (loongarch_output_move (&operands[2]),
4410 &operands[2]);
4411 return "";
4412 }
4413 [(set_attr "move_type"
4414 "load,fpload,store,fpstore,load,store")
4415 (set_attr "insn_count" "2,2,2,2,2,2")])
4416
4417 ;; 2 HI/SI/SF/DF loads are bonded.
4418 (define_peephole2
4419 [(set (match_operand:JOIN_MODE 0 "register_operand")
4420 (match_operand:JOIN_MODE 1 "non_volatile_mem_operand"))
4421 (set (match_operand:JOIN_MODE 2 "register_operand")
4422 (match_operand:JOIN_MODE 3 "non_volatile_mem_operand"))]
4423 "loongarch_load_store_bonding_p (operands, <JOIN_MODE:MODE>mode, true)"
4424 [(parallel [(set (match_dup 0)
4425 (match_dup 1))
4426 (set (match_dup 2)
4427 (match_dup 3))])]
4428 "")
4429
4430 ;; 2 HI/SI/SF/DF stores are bonded.
4431 (define_peephole2
4432 [(set (match_operand:JOIN_MODE 0 "memory_operand")
4433 (match_operand:JOIN_MODE 1 "register_operand"))
4434 (set (match_operand:JOIN_MODE 2 "memory_operand")
4435 (match_operand:JOIN_MODE 3 "register_operand"))]
4436 "loongarch_load_store_bonding_p (operands, <JOIN_MODE:MODE>mode, false)"
4437 [(parallel [(set (match_dup 0)
4438 (match_dup 1))
4439 (set (match_dup 2)
4440 (match_dup 3))])]
4441 "")
4442
4443 ;; Match paired HImode loads.
4444 (define_insn "*join2_loadhi"
4445 [(set (match_operand:SI 0 "register_operand" "=&r")
4446 (any_extend:SI (match_operand:HI 1 "non_volatile_mem_operand" "m")))
4447 (set (match_operand:SI 2 "register_operand" "=r")
4448 (any_extend:SI (match_operand:HI 3 "non_volatile_mem_operand" "m")))]
4449 "reload_completed"
4450 {
4451 /* The load destination does not overlap the source. */
4452 gcc_assert (!reg_overlap_mentioned_p (operands[0], operands[1]));
4453 output_asm_insn ("ld.h<u>\t%0,%1", operands);
4454 output_asm_insn ("ld.h<u>\t%2,%3", operands);
4455
4456 return "";
4457 }
4458 [(set_attr "move_type" "load")
4459 (set_attr "insn_count" "2")])
4460
4461
4462 ;; 2 HI loads are bonded.
4463 (define_peephole2
4464 [(set (match_operand:SI 0 "register_operand")
4465 (any_extend:SI (match_operand:HI 1 "non_volatile_mem_operand")))
4466 (set (match_operand:SI 2 "register_operand")
4467 (any_extend:SI (match_operand:HI 3 "non_volatile_mem_operand")))]
4468 "loongarch_load_store_bonding_p (operands, HImode, true)"
4469 [(parallel [(set (match_dup 0)
4470 (any_extend:SI (match_dup 1)))
4471 (set (match_dup 2)
4472 (any_extend:SI (match_dup 3)))])]
4473 "")
4474
4475
4476
4477 (define_int_iterator CRC [UNSPEC_CRC UNSPEC_CRCC])
4478 (define_int_attr crc [(UNSPEC_CRC "crc") (UNSPEC_CRCC "crcc")])
4479
4480 (define_insn "loongarch_<crc>_w_<size>_w"
4481 [(set (match_operand:SI 0 "register_operand" "=r")
4482 (unspec:SI [(match_operand:QHWD 1 "register_operand" "r")
4483 (match_operand:SI 2 "register_operand" "r")]
4484 CRC))]
4485 ""
4486 "<crc>.w.<size>.w\t%0,%1,%2"
4487 [(set_attr "type" "unknown")
4488 (set_attr "mode" "<MODE>")])
4489
4490 (define_insn "loongarch_<crc>_w_<size>_w_extended"
4491 [(set (match_operand:DI 0 "register_operand" "=r")
4492 (sign_extend:DI
4493 (unspec:SI [(match_operand:QHWD 1 "register_operand" "r")
4494 (match_operand:SI 2 "register_operand" "r")]
4495 CRC)))]
4496 "TARGET_64BIT"
4497 "<crc>.w.<size>.w\t%0,%1,%2"
4498 [(set_attr "type" "unknown")
4499 (set_attr "mode" "<MODE>")])
4500
4501 (define_expand "crc_rev<mode>si4"
4502 [(match_operand:SI 0 "register_operand") ; new_chksum
4503 (match_operand:SI 1 "register_operand") ; old_chksum
4504 (match_operand:SUBDI 2 "reg_or_0_operand") ; msg
4505 (match_operand 3 "const_int_operand")] ; poly
4506 ""
4507 {
4508 unsigned HOST_WIDE_INT poly = UINTVAL (operands[3]);
4509 rtx msg = operands[2];
4510 rtx (*crc_insn)(rtx, rtx, rtx) = nullptr;
4511
4512 /* TODO: Review this when adding LA32 support. If we're going to
4513 support CRC instructions on LA32 we'll need a "-mcrc" switch as
4514 they are optional on LA32. */
4515
4516 if (TARGET_64BIT)
4517 {
4518 if (poly == reflect_hwi (0xedb88320u, 32))
4519 crc_insn = gen_loongarch_crc_w_<size>_w;
4520 else if (poly == reflect_hwi (0x82f63b78u, 32))
4521 crc_insn = gen_loongarch_crcc_w_<size>_w;
4522 }
4523
4524 if (crc_insn)
4525 {
4526 /* We cannot make crc_insn to accept const0_rtx easily:
4527 it's not possible to figure out the mode of const0_rtx so we'd
4528 have to separate both UNSPEC_CRC and UNSPEC_CRCC to 4 different
4529 UNSPECs. Instead just hack it around here. */
4530 if (msg == const0_rtx)
4531 msg = gen_rtx_REG (<MODE>mode, 0);
4532
4533 emit_insn (crc_insn (operands[0], msg, operands[1]));
4534 }
4535 else
4536 {
4537 /* No CRC instruction is suitable, use the generic table-based
4538 implementation but optimize bit reversion. */
4539 auto rbit = [](rtx *r)
4540 {
4541 /* Well, this is ugly. The problem is
4542 expand_reversed_crc_table_based only accepts one helper
4543 for reversing data elements and CRC states. */
4544 auto mode = GET_MODE (*r);
4545 auto rbit = (mode == <MODE>mode ? gen_rbit<mode> : gen_rbitsi);
4546 rtx out = gen_reg_rtx (mode);
4547
4548 emit_insn (rbit (out, *r));
4549 *r = out;
4550 };
4551 expand_reversed_crc_table_based (operands[0], operands[1],
4552 msg, operands[3], <MODE>mode,
4553 rbit);
4554 }
4555 DONE;
4556 })
4557
4558 (define_insn_and_split "*crc_combine"
4559 [(set (match_operand:SI 0 "register_operand" "=r,r")
4560 (unspec:SI
4561 [(reg:SUBDI 0)
4562 (subreg:SI
4563 (xor:DI
4564 (match_operand:DI 1 "register_operand" "r,r")
4565 ; Our LOAD_EXTEND_OP makes this same as sign_extend
4566 ; if SUBDI is SI, or zero_extend if SUBDI is QI or HI.
4567 ; For the former the high bits in rk are ignored by
4568 ; crc.w.w.w anyway, for the latter the zero extension is
4569 ; necessary for the correctness of this transformation.
4570 (subreg:DI
4571 (match_operand:SUBDI 2 "memory_operand" "m,k") 0)) 0)]
4572 CRC))]
4573 "TARGET_64BIT && loongarch_pre_reload_split ()"
4574 "#"
4575 "&& true"
4576 [(set (match_dup 3) (match_dup 2))
4577 (set (match_dup 0)
4578 (unspec:SI [(match_dup 3) (match_dup 1)] CRC))]
4579 {
4580 operands[3] = gen_reg_rtx (<MODE>mode);
4581 operands[1] = lowpart_subreg (SImode, operands[1], DImode);
4582 })
4583
4584 ;; With normal or medium code models, if the only use of a pc-relative
4585 ;; address is for loading or storing a value, then relying on linker
4586 ;; relaxation is not better than emitting the machine instruction directly.
4587 ;; Even if the la.local pseudo op can be relaxed, we get:
4588 ;;
4589 ;; pcaddi $t0, %pcrel_20(x)
4590 ;; ld.d $t0, $t0, 0
4591 ;;
4592 ;; There are still two instructions, same as using the machine instructions
4593 ;; and explicit relocs:
4594 ;;
4595 ;; pcalau12i $t0, %pc_hi20(x)
4596 ;; ld.d $t0, $t0, %pc_lo12(x)
4597 ;;
4598 ;; And if the pseudo op cannot be relaxed, we'll get a worse result (with
4599 ;; 3 instructions).
4600 (define_insn_and_rewrite "simple_load<mode>"
4601 [(set (match_operand:LD_AT_LEAST_32_BIT 0 "register_operand" "=r,f")
4602 (match_operand:LD_AT_LEAST_32_BIT 1 "mem_simple_ldst_operand" ""))]
4603 "loongarch_pre_reload_split ()
4604 && la_opt_explicit_relocs == EXPLICIT_RELOCS_AUTO
4605 && (TARGET_CMODEL_NORMAL || TARGET_CMODEL_MEDIUM)"
4606 "#"
4607 "&& true"
4608 {
4609 operands[1] = loongarch_rewrite_mem_for_simple_ldst (operands[1]);
4610 })
4611
4612 (define_insn_and_rewrite "simple_load_<su>ext<SUBDI:mode><GPR:mode>"
4613 [(set (match_operand:GPR 0 "register_operand" "=r")
4614 (any_extend:GPR
4615 (match_operand:SUBDI 1 "mem_simple_ldst_operand" "")))]
4616 "loongarch_pre_reload_split ()
4617 && la_opt_explicit_relocs == EXPLICIT_RELOCS_AUTO
4618 && (TARGET_CMODEL_NORMAL || TARGET_CMODEL_MEDIUM)"
4619 "#"
4620 "&& true"
4621 {
4622 operands[1] = loongarch_rewrite_mem_for_simple_ldst (operands[1]);
4623 })
4624
4625 (define_insn_and_rewrite "simple_store<mode>"
4626 [(set (match_operand:ST_ANY 0 "mem_simple_ldst_operand" "")
4627 (match_operand:ST_ANY 1 "reg_or_0_operand" "r,f"))]
4628 "loongarch_pre_reload_split ()
4629 && la_opt_explicit_relocs == EXPLICIT_RELOCS_AUTO
4630 && (TARGET_CMODEL_NORMAL || TARGET_CMODEL_MEDIUM)"
4631 "#"
4632 "&& true"
4633 {
4634 operands[0] = loongarch_rewrite_mem_for_simple_ldst (operands[0]);
4635 })
4636
4637 ;; Set and check against stack canary without leaving it in a register.
4638 ;; DO NOT ATTEMPT TO SPLIT THESE INSNS! It's important for security reason
4639 ;; that the canary value does not live beyond the life of this sequence.
4640
4641 (define_insn "@stack_protect_combined_set_normal_<mode>"
4642 [(set (match_operand:P 0 "memory_operand" "=m,ZC")
4643 (unspec:P [(mem:P (match_operand:P 1 "ssp_normal_operand"))]
4644 UNSPEC_SSP))
4645 (set (match_scratch:P 2 "=&r,&r") (const_int 0))]
4646 ""
4647 {
4648 loongarch_output_asm_load_canary (operands[2], operands[1], NULL_RTX);
4649 output_asm_insn (which_alternative ? "stptr.d\t%2,%0" : "st.d\t%2,%0",
4650 operands);
4651 return "ori\t%2,$r0,0";
4652 }
4653 [(set_attr "type" "store")
4654 (set_attr "length" "20")])
4655
4656 (define_insn "@stack_protect_combined_set_extreme_<mode>"
4657 [(set (match_operand:P 0 "memory_operand" "=m,ZC")
4658 (unspec:P [(mem:P (match_operand:P 1 "ssp_operand"))] UNSPEC_SSP))
4659 (set (match_scratch:P 2 "=&r,&r") (const_int 0))
4660 (set (match_scratch:P 3 "=&r,&r") (const_int 0))]
4661 ""
4662 {
4663 loongarch_output_asm_load_canary (operands[2], operands[1], operands[3]);
4664 output_asm_insn (which_alternative ? "stptr.d\t%2,%0" : "st.d\t%2,%0",
4665 operands);
4666 return "ori\t%2,$r0,0\n\tori\t%3,$r0,0";
4667 }
4668 [(set_attr "type" "store")
4669 (set_attr "length" "36")])
4670
4671 (define_insn "@stack_protect_combined_test_internal_<mode>"
4672 [(set (match_operand:P 0 "register_operand" "=r,r,&r,&r")
4673 (xor:P
4674 (match_operand:P 1 "memory_operand" "=m,ZC,m,ZC")
4675 (unspec:P
4676 [(mem:P (match_operand:P 2 "ssp_operand" "ZE,ZE,ZF,ZF"))]
4677 UNSPEC_SSP)))
4678 (set (match_scratch:P 3 "=&r,&r,&r,&r") (const_int 0))]
4679 ""
4680 {
4681 rtx t = (which_alternative >= 2 ? operands[0] : NULL_RTX);
4682 loongarch_output_asm_load_canary (operands[3], operands[2], t);
4683 output_asm_insn ((which_alternative & 1) ? "ldptr.d\t%0,%1"
4684 : "ld.d\t%0,%1",
4685 operands);
4686 return "xor\t%0,%0,%3\n\tori\t%3,$r0,0";
4687 }
4688 [(set_attr "type" "load,load,load,load")
4689 (set_attr "length" "24,24,36,36")])
4690
4691 (define_expand "stack_protect_combined_set"
4692 [(match_operand 0 "memory_operand")
4693 (match_operand 1 "memory_operand")]
4694 ""
4695 {
4696 rtx canary = XEXP (operands[1], 0);
4697 auto fn = (ssp_normal_operand (canary, VOIDmode)
4698 ? gen_stack_protect_combined_set_normal
4699 : gen_stack_protect_combined_set_extreme);
4700
4701 emit_insn (fn (Pmode, operands[0], canary));
4702 DONE;
4703 })
4704
4705 (define_expand "stack_protect_combined_test"
4706 [(match_operand 0 "memory_operand")
4707 (match_operand 1 "memory_operand")
4708 (match_operand 2 "")]
4709 ""
4710 {
4711 rtx t = gen_reg_rtx (Pmode);
4712 rtx canary = XEXP (operands[1], 0);
4713 emit_insn (gen_stack_protect_combined_test_internal (Pmode, t,
4714 operands[0],
4715 canary));
4716 rtx cond = gen_rtx_EQ (VOIDmode, t, const0_rtx);
4717 emit_jump_insn (gen_cbranch4 (Pmode, cond, t, const0_rtx, operands[2]));
4718 DONE;
4719 })
4720
4721 ;; Synchronization instructions.
4722
4723 (include "sync.md")
4724
4725 (include "generic.md")
4726 (include "la464.md")
4727
4728 ; The LoongArch SIMD Instructions.
4729 (include "simd.md")
4730
4731 (define_c_enum "unspec" [
4732 UNSPEC_ADDRESS_FIRST
4733 ])