strub: use opt_for_fn during ipa
[official-gcc.git] / gcc / config / xtensa / xtensa.md
blob20af1cbfbd03af7150e58849562cefda64a81fcd
1 ;; GCC machine description for Tensilica's Xtensa architecture.
2 ;; Copyright (C) 2001-2023 Free Software Foundation, Inc.
3 ;; Contributed by Bob Wilson (bwilson@tensilica.com) at Tensilica.
5 ;; This file is part of GCC.
7 ;; GCC is free software; you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 3, or (at your option)
10 ;; any later version.
12 ;; GCC is distributed in the hope that it will be useful, but WITHOUT
13 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15 ;; License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GCC; see the file COPYING3.  If not see
19 ;; <http://www.gnu.org/licenses/>.
22 (define_constants [
23   (A0_REG               0)
24   (A1_REG               1)
25   (A7_REG               7)
26   (A8_REG               8)
27   (A9_REG               9)
30 (define_c_enum "unspec" [
31   UNSPEC_NOP
32   UNSPEC_PLT
33   UNSPEC_RET_ADDR
34   UNSPEC_TPOFF
35   UNSPEC_DTPOFF
36   UNSPEC_TLS_FUNC
37   UNSPEC_TLS_ARG
38   UNSPEC_TLS_CALL
39   UNSPEC_TP
40   UNSPEC_MEMW
41   UNSPEC_LSETUP_START
42   UNSPEC_LSETUP_END
43   UNSPEC_FRAME_BLOCKAGE
46 (define_c_enum "unspecv" [
47   UNSPECV_SET_FP
48   UNSPECV_ENTRY
49   UNSPECV_S32RI
50   UNSPECV_S32C1I
51   UNSPECV_EH_RETURN
52   UNSPECV_SET_TP
53   UNSPECV_BLOCKAGE
56 ;; This code iterator allows signed and unsigned widening multiplications
57 ;; to use the same template.
58 (define_code_iterator any_extend [sign_extend zero_extend])
60 ;; <u> expands to an empty string when doing a signed operation and
61 ;; "u" when doing an unsigned operation.
62 (define_code_attr u [(sign_extend "") (zero_extend "u")])
64 ;; <su> is like <u>, but the signed form expands to "s" rather than "".
65 (define_code_attr su [(sign_extend "s") (zero_extend "u")])
67 ;; This code iterator allows four integer min/max operations to be
68 ;; generated from one template.
69 (define_code_iterator any_minmax [smin umin smax umax])
71 ;; <minmax> expands to the opcode name for any_minmax operations.
72 (define_code_attr minmax [(smin "min") (umin "minu")
73                           (smax "max") (umax "maxu")])
75 ;; This code iterator is for floating-point comparisons.
76 (define_code_iterator any_scc_sf [eq lt le uneq unlt unle unordered])
77 (define_code_attr scc_sf [(eq "oeq") (lt "olt") (le "ole")
78                           (uneq "ueq") (unlt "ult") (unle "ule")
79                           (unordered "un")])
81 ;; This iterator and attribute allow to combine most atomic operations.
82 (define_code_iterator ATOMIC [and ior xor plus minus mult])
83 (define_code_attr atomic [(and "and") (ior "ior") (xor "xor")
84                           (plus "add") (minus "sub") (mult "nand")])
86 ;; This mode iterator allows the HI and QI patterns to be defined from
87 ;; the same template.
88 (define_mode_iterator HQI [HI QI])
91 ;; Attributes.
93 (define_attr "type"
94   "unknown,jump,call,load,store,move,arith,multi,nop,farith,fmadd,fconv,fload,fstore,mul16,mul32,div32,mac16,rsr,wsr,entry,trap"
95   (const_string "unknown"))
97 (define_attr "mode"
98   "unknown,none,QI,HI,SI,DI,SF,DF,BL"
99   (const_string "unknown"))
101 (define_attr "length" "" (const_int 1))
103 ;; Describe a user's asm statement.
104 (define_asm_attributes
105   [(set_attr "type"     "multi")
106    (set_attr "mode"     "none")
107    (set_attr "length"   "3")])  ;; Should be the maximum possible length
108                                 ;; of a single machine instruction.
111 ;; Pipeline model.
113 ;; The Xtensa basically has simple 5-stage RISC pipeline.
114 ;; Most instructions complete in 1 cycle, and it is OK to assume that
115 ;; everything is fully pipelined.  The exceptions have special insn
116 ;; reservations in the pipeline description below.  The Xtensa can
117 ;; issue one instruction per cycle, so defining CPU units is unnecessary.
119 (define_insn_reservation "xtensa_any_insn" 1
120                          (eq_attr "type" "!load,fload,rsr,mul16,mul32,fmadd,fconv")
121                          "nothing")
123 (define_insn_reservation "xtensa_memory" 2
124                          (eq_attr "type" "load,fload")
125                          "nothing")
127 (define_insn_reservation "xtensa_sreg" 2
128                          (eq_attr "type" "rsr")
129                          "nothing")
131 (define_insn_reservation "xtensa_mul16" 2
132                          (eq_attr "type" "mul16")
133                          "nothing")
135 (define_insn_reservation "xtensa_mul32" 2
136                          (eq_attr "type" "mul32")
137                          "nothing")
139 (define_insn_reservation "xtensa_fmadd" 4
140                          (eq_attr "type" "fmadd")
141                          "nothing")
143 (define_insn_reservation "xtensa_fconv" 2
144                          (eq_attr "type" "fconv")
145                          "nothing")
147 ;; Include predicates and constraints.
149 (include "predicates.md")
150 (include "constraints.md")
153 ;; Addition.
155 (define_insn "addsi3"
156   [(set (match_operand:SI 0 "register_operand" "=D,D,a,a,a")
157         (plus:SI (match_operand:SI 1 "register_operand" "%d,d,r,r,r")
158                  (match_operand:SI 2 "add_operand" "d,O,r,J,N")))]
159   ""
160   "@
161    add.n\t%0, %1, %2
162    addi.n\t%0, %1, %d2
163    add\t%0, %1, %2
164    addi\t%0, %1, %d2
165    addmi\t%0, %1, %x2"
166   [(set_attr "type"     "arith,arith,arith,arith,arith")
167    (set_attr "mode"     "SI")
168    (set_attr "length"   "2,2,3,3,3")])
170 (define_insn "*addsubx"
171   [(set (match_operand:SI 0 "register_operand" "=a")
172         (match_operator:SI 4 "addsub_operator"
173                 [(ashift:SI (match_operand:SI 1 "register_operand" "r")
174                             (match_operand:SI 3 "addsubx_operand" "i"))
175                  (match_operand:SI 2 "register_operand" "r")]))]
176   "TARGET_ADDX"
178   operands[3] = GEN_INT (1 << INTVAL (operands[3]));
179   switch (GET_CODE (operands[4]))
180     {
181     case PLUS:
182       return "addx%3\t%0, %1, %2";
183     case MINUS:
184       return "subx%3\t%0, %1, %2";
185     default:
186       gcc_unreachable ();
187     }
189   [(set_attr "type"     "arith")
190    (set_attr "mode"     "SI")
191    (set_attr "length"   "3")])
193 (define_expand "adddi3"
194   [(set (match_operand:DI 0 "register_operand")
195         (plus:DI (match_operand:DI 1 "register_operand")
196                  (match_operand:DI 2 "register_operand")))]
197   ""
199   rtx lo_dest, hi_dest, lo_op0, hi_op0, lo_op1, hi_op1;
200   rtx_code_label *label;
201   if (rtx_equal_p (operands[0], operands[1])
202       || rtx_equal_p (operands[0], operands[2])
203       || ! REG_P (operands[1]) || ! REG_P (operands[2]))
204     FAIL;
205   lo_dest = gen_lowpart (SImode, operands[0]);
206   hi_dest = gen_highpart (SImode, operands[0]);
207   lo_op0 = gen_lowpart (SImode, operands[1]);
208   hi_op0 = gen_highpart (SImode, operands[1]);
209   lo_op1 = gen_lowpart (SImode, operands[2]);
210   hi_op1 = gen_highpart (SImode, operands[2]);
211   emit_insn (gen_addsi3 (hi_dest, hi_op0, hi_op1));
212   emit_insn (gen_addsi3 (lo_dest, lo_op0, lo_op1));
213   emit_cmp_and_jump_insns (lo_dest,
214                            (REGNO (operands[1]) < REGNO (operands[2])
215                             ? lo_op1 : lo_op0), GEU, const0_rtx,
216                            SImode, true, label = gen_label_rtx ());
217   emit_insn (gen_addsi3 (hi_dest, hi_dest, const1_rtx));
218   emit_label (label);
219   DONE;
222 (define_insn "addsf3"
223   [(set (match_operand:SF 0 "register_operand" "=f")
224         (plus:SF (match_operand:SF 1 "register_operand" "%f")
225                  (match_operand:SF 2 "register_operand" "f")))]
226   "TARGET_HARD_FLOAT"
227   "add.s\t%0, %1, %2"
228   [(set_attr "type"     "fmadd")
229    (set_attr "mode"     "SF")
230    (set_attr "length"   "3")])
233 ;; Subtraction.
235 (define_insn "subsi3"
236   [(set (match_operand:SI 0 "register_operand" "=a")
237         (minus:SI (match_operand:SI 1 "register_operand" "r")
238                   (match_operand:SI 2 "register_operand" "r")))]
239   ""
240   "sub\t%0, %1, %2"
241   [(set_attr "type"     "arith")
242    (set_attr "mode"     "SI")
243    (set_attr "length"   "3")])
245 (define_insn_and_split "*subsi3_from_const"
246   [(set (match_operand:SI 0 "register_operand" "=a")
247         (minus:SI (match_operand:SI 1 "const_int_operand" "i")
248                   (match_operand:SI 2 "register_operand" "r")))]
249   "xtensa_simm8 (-INTVAL (operands[1]))
250    || xtensa_simm8x256 (-INTVAL (operands[1]))"
251   "#"
252   "&& 1"
253   [(set (match_dup 0)
254         (plus:SI (match_dup 2)
255                  (match_dup 1)))
256    (set (match_dup 0)
257         (neg:SI (match_dup 0)))]
259   operands[1] = GEN_INT (-INTVAL (operands[1]));
261   [(set_attr "type"     "arith")
262    (set_attr "mode"     "SI")
263    (set (attr "length")
264         (if_then_else (match_test "TARGET_DENSITY
265                                    && xtensa_m1_or_1_thru_15 (-INTVAL (operands[1]))")
266                       (const_int 5)
267                       (const_int 6)))])
269 (define_expand "subdi3"
270   [(set (match_operand:DI 0 "register_operand")
271         (minus:DI (match_operand:DI 1 "register_operand")
272                   (match_operand:DI 2 "register_operand")))]
273   ""
275   rtx lo_dest, hi_dest, lo_op0, hi_op0, lo_op1, hi_op1;
276   rtx_code_label *label;
277   lo_dest = gen_lowpart (SImode, operands[0]);
278   hi_dest = gen_highpart (SImode, operands[0]);
279   lo_op0 = gen_lowpart (SImode, operands[1]);
280   hi_op0 = gen_highpart (SImode, operands[1]);
281   lo_op1 = gen_lowpart (SImode, operands[2]);
282   hi_op1 = gen_highpart (SImode, operands[2]);
283   emit_insn (gen_subsi3 (hi_dest, hi_op0, hi_op1));
284   emit_cmp_and_jump_insns (lo_op0, lo_op1, GEU, const0_rtx,
285                            SImode, true, label = gen_label_rtx ());
286   emit_insn (gen_addsi3 (hi_dest, hi_dest, constm1_rtx));
287   emit_label (label);
288   emit_insn (gen_subsi3 (lo_dest, lo_op0, lo_op1));
289   DONE;
292 (define_insn "subsf3"
293   [(set (match_operand:SF 0 "register_operand" "=f")
294         (minus:SF (match_operand:SF 1 "register_operand" "f")
295                   (match_operand:SF 2 "register_operand" "f")))]
296   "TARGET_HARD_FLOAT"
297   "sub.s\t%0, %1, %2"
298   [(set_attr "type"     "fmadd")
299    (set_attr "mode"     "SF")
300    (set_attr "length"   "3")])
303 ;; Multiplication.
305 (define_expand "mulsidi3"
306   [(set (match_operand:DI 0 "register_operand")
307         (mult:DI (sign_extend:DI (match_operand:SI 1 "register_operand"))
308                  (sign_extend:DI (match_operand:SI 2 "register_operand"))))]
309   "TARGET_MUL32_HIGH"
311   rtx temp = gen_reg_rtx (SImode);
312   emit_insn (gen_mulsi3 (temp, operands[1], operands[2]));
313   emit_insn (gen_mulsi3_highpart (gen_highpart (SImode, operands[0]),
314                                   operands[1], operands[2]));
315   emit_insn (gen_movsi (gen_lowpart (SImode, operands[0]), temp));
316   DONE;
319 (define_expand "umulsidi3"
320   [(set (match_operand:DI 0 "register_operand")
321         (mult:DI (zero_extend:DI (match_operand:SI 1 "register_operand"))
322                  (zero_extend:DI (match_operand:SI 2 "register_operand"))))]
323   ""
325   if (TARGET_MUL32_HIGH)
326     {
327       rtx temp = gen_reg_rtx (SImode);
328       emit_insn (gen_mulsi3 (temp, operands[1], operands[2]));
329       emit_insn (gen_umulsi3_highpart (gen_highpart (SImode, operands[0]),
330                                        operands[1], operands[2]));
331       emit_insn (gen_movsi (gen_lowpart (SImode, operands[0]), temp));
332     }
333   else
334     emit_library_call_value (gen_rtx_SYMBOL_REF (Pmode, "__umulsidi3"),
335                              operands[0], LCT_NORMAL, DImode,
336                              operands[1], SImode,
337                              operands[2], SImode);
338    DONE;
341 (define_insn "<u>mulsi3_highpart"
342   [(set (match_operand:SI 0 "register_operand" "=a")
343         (truncate:SI
344          (lshiftrt:DI
345           (mult:DI (any_extend:DI (match_operand:SI 1 "register_operand" "%r"))
346                    (any_extend:DI (match_operand:SI 2 "register_operand" "r")))
347           (const_int 32))))]
348   "TARGET_MUL32_HIGH"
349   "mul<su>h\t%0, %1, %2"
350   [(set_attr "type"     "mul32")
351    (set_attr "mode"     "SI")
352    (set_attr "length"   "3")])
354 (define_insn "mulsi3"
355   [(set (match_operand:SI 0 "register_operand" "=a")
356         (mult:SI (match_operand:SI 1 "register_operand" "%r")
357                  (match_operand:SI 2 "register_operand" "r")))]
358   "TARGET_MUL32"
359   "mull\t%0, %1, %2"
360   [(set_attr "type"     "mul32")
361    (set_attr "mode"     "SI")
362    (set_attr "length"   "3")])
364 (define_insn "<u>mulhisi3"
365   [(set (match_operand:SI 0 "register_operand" "=C,A")
366         (mult:SI (any_extend:SI
367                   (match_operand:HI 1 "register_operand" "%r,r"))
368                  (any_extend:SI
369                   (match_operand:HI 2 "register_operand" "r,r"))))]
370   "TARGET_MUL16 || TARGET_MAC16"
371   "@
372    mul16<su>\t%0, %1, %2
373    <u>mul.aa.ll\t%1, %2"
374   [(set_attr "type"     "mul16,mac16")
375    (set_attr "mode"     "SI")
376    (set_attr "length"   "3,3")])
378 (define_insn "muladdhisi"
379   [(set (match_operand:SI 0 "register_operand" "=A")
380         (plus:SI (mult:SI (sign_extend:SI
381                            (match_operand:HI 1 "register_operand" "%r"))
382                           (sign_extend:SI
383                            (match_operand:HI 2 "register_operand" "r")))
384                  (match_operand:SI 3 "register_operand" "0")))]
385   "TARGET_MAC16"
386   "mula.aa.ll\t%1, %2"
387   [(set_attr "type"     "mac16")
388    (set_attr "mode"     "SI")
389    (set_attr "length"   "3")])
391 (define_insn "mulsubhisi"
392   [(set (match_operand:SI 0 "register_operand" "=A")
393         (minus:SI (match_operand:SI 1 "register_operand" "0")
394                   (mult:SI (sign_extend:SI
395                             (match_operand:HI 2 "register_operand" "%r"))
396                            (sign_extend:SI
397                             (match_operand:HI 3 "register_operand" "r")))))]
398   "TARGET_MAC16"
399   "muls.aa.ll\t%2, %3"
400   [(set_attr "type"     "mac16")
401    (set_attr "mode"     "SI")
402    (set_attr "length"   "3")])
404 (define_insn "mulsf3"
405   [(set (match_operand:SF 0 "register_operand" "=f")
406         (mult:SF (match_operand:SF 1 "register_operand" "%f")
407                  (match_operand:SF 2 "register_operand" "f")))]
408   "TARGET_HARD_FLOAT"
409   "mul.s\t%0, %1, %2"
410   [(set_attr "type"     "fmadd")
411    (set_attr "mode"     "SF")
412    (set_attr "length"   "3")])
414 (define_insn "fmasf4"
415   [(set (match_operand:SF 0 "register_operand" "=f")
416         (fma:SF (match_operand:SF 1 "register_operand" "f")
417                 (match_operand:SF 2 "register_operand" "f")
418                 (match_operand:SF 3 "register_operand" "0")))]
419   "TARGET_HARD_FLOAT"
420   "madd.s\t%0, %1, %2"
421   [(set_attr "type"     "fmadd")
422    (set_attr "mode"     "SF")
423    (set_attr "length"   "3")])
425 ;; Note that (C - A*B) = (-A*B + C)
426 (define_insn "fnmasf4"
427   [(set (match_operand:SF 0 "register_operand" "=f")
428         (fma:SF (neg:SF (match_operand:SF 1 "register_operand" "f"))
429                 (match_operand:SF 2 "register_operand" "f")
430                 (match_operand:SF 3 "register_operand" "0")))]
431   "TARGET_HARD_FLOAT"
432   "msub.s\t%0, %1, %2"
433   [(set_attr "type"     "fmadd")
434    (set_attr "mode"     "SF")
435    (set_attr "length"   "3")])
438 ;; Division.
440 (define_insn "divsi3"
441   [(set (match_operand:SI 0 "register_operand" "=a")
442         (div:SI (match_operand:SI 1 "register_operand" "r")
443                 (match_operand:SI 2 "register_operand" "r")))]
444   "TARGET_DIV32"
445   "quos\t%0, %1, %2"
446   [(set_attr "type"     "div32")
447    (set_attr "mode"     "SI")
448    (set_attr "length"   "3")])
450 (define_insn "udivsi3"
451   [(set (match_operand:SI 0 "register_operand" "=a")
452         (udiv:SI (match_operand:SI 1 "register_operand" "r")
453                  (match_operand:SI 2 "register_operand" "r")))]
454   "TARGET_DIV32"
455   "quou\t%0, %1, %2"
456   [(set_attr "type"     "div32")
457    (set_attr "mode"     "SI")
458    (set_attr "length"   "3")])
461 ;; Remainders.
463 (define_insn "modsi3"
464   [(set (match_operand:SI 0 "register_operand" "=a")
465         (mod:SI (match_operand:SI 1 "register_operand" "r")
466                 (match_operand:SI 2 "register_operand" "r")))]
467   "TARGET_DIV32"
468   "rems\t%0, %1, %2"
469   [(set_attr "type"     "div32")
470    (set_attr "mode"     "SI")
471    (set_attr "length"   "3")])
473 (define_insn "umodsi3"
474   [(set (match_operand:SI 0 "register_operand" "=a")
475         (umod:SI (match_operand:SI 1 "register_operand" "r")
476                  (match_operand:SI 2 "register_operand" "r")))]
477   "TARGET_DIV32"
478   "remu\t%0, %1, %2"
479   [(set_attr "type"     "div32")
480    (set_attr "mode"     "SI")
481    (set_attr "length"   "3")])
484 ;; Absolute value.
486 (define_insn "abssi2"
487   [(set (match_operand:SI 0 "register_operand" "=a")
488         (abs:SI (match_operand:SI 1 "register_operand" "r")))]
489   "TARGET_ABS"
490   "abs\t%0, %1"
491   [(set_attr "type"     "arith")
492    (set_attr "mode"     "SI")
493    (set_attr "length"   "3")])
495 (define_insn "abssf2"
496   [(set (match_operand:SF 0 "register_operand" "=f")
497         (abs:SF (match_operand:SF 1 "register_operand" "f")))]
498   "TARGET_HARD_FLOAT"
499   "abs.s\t%0, %1"
500   [(set_attr "type"     "farith")
501    (set_attr "mode"     "SF")
502    (set_attr "length"   "3")])
505 ;; Min and max.
507 (define_insn "<code>si3"
508   [(set (match_operand:SI 0 "register_operand" "=a")
509         (any_minmax:SI (match_operand:SI 1 "register_operand" "%r")
510                        (match_operand:SI 2 "register_operand" "r")))]
511   "TARGET_MINMAX"
512   "<minmax>\t%0, %1, %2"
513   [(set_attr "type"     "arith")
514    (set_attr "mode"     "SI")
515    (set_attr "length"   "3")])
518 ;; Signed clamp.
520 (define_insn_and_split "*xtensa_clamps"
521   [(set (match_operand:SI 0 "register_operand" "=a")
522         (smax:SI (smin:SI (match_operand:SI 1 "register_operand" "r")
523                           (match_operand:SI 2 "const_int_operand" "i"))
524                  (match_operand:SI 3 "const_int_operand" "i")))]
525   "TARGET_MINMAX && TARGET_CLAMPS
526    && xtensa_match_CLAMPS_imms_p (operands[3], operands[2])"
527   "#"
528   "&& 1"
529   [(set (match_dup 0)
530         (smin:SI (smax:SI (match_dup 1)
531                           (match_dup 3))
532                  (match_dup 2)))]
533   ""
534   [(set_attr "type"     "arith")
535    (set_attr "mode"     "SI")
536    (set_attr "length"   "3")])
538 (define_insn "*xtensa_clamps"
539   [(set (match_operand:SI 0 "register_operand" "=a")
540         (smin:SI (smax:SI (match_operand:SI 1 "register_operand" "r")
541                           (match_operand:SI 2 "const_int_operand" "i"))
542                  (match_operand:SI 3 "const_int_operand" "i")))]
543   "TARGET_MINMAX && TARGET_CLAMPS
544    && xtensa_match_CLAMPS_imms_p (operands[2], operands[3])"
546   static char result[64];
547   sprintf (result, "clamps\t%%0, %%1, %d", floor_log2 (-INTVAL (operands[2])));
548   return result;
550   [(set_attr "type"     "arith")
551    (set_attr "mode"     "SI")
552    (set_attr "length"   "3")])
555 ;; Count redundant leading sign bits and leading/trailing zeros,
556 ;; and find first bit.
558 (define_insn "clrsbsi2"
559   [(set (match_operand:SI 0 "register_operand" "=a")
560         (clrsb:SI (match_operand:SI 1 "register_operand" "r")))]
561   "TARGET_NSA"
562   "nsa\t%0, %1"
563   [(set_attr "type"     "arith")
564    (set_attr "mode"     "SI")
565    (set_attr "length"   "3")])
567 (define_insn "clzsi2"
568   [(set (match_operand:SI 0 "register_operand" "=a")
569         (clz:SI (match_operand:SI 1 "register_operand" "r")))]
570   "TARGET_NSA"
571   "nsau\t%0, %1"
572   [(set_attr "type"     "arith")
573    (set_attr "mode"     "SI")
574    (set_attr "length"   "3")])
576 (define_expand "ctzsi2"
577   [(set (match_operand:SI 0 "register_operand" "")
578         (ctz:SI (match_operand:SI 1 "register_operand" "")))]
579   "TARGET_NSA"
581   rtx temp = gen_reg_rtx (SImode);
582   emit_insn (gen_negsi2 (temp, operands[1]));
583   emit_insn (gen_andsi3 (temp, temp, operands[1]));
584   emit_insn (gen_clzsi2 (temp, temp));
585   emit_move_insn (operands[0], GEN_INT (31));
586   emit_insn (gen_subsi3 (operands[0], operands[0], temp));
587   DONE;
590 (define_expand "ffssi2"
591   [(set (match_operand:SI 0 "register_operand" "")
592         (ffs:SI (match_operand:SI 1 "register_operand" "")))]
593   "TARGET_NSA"
595   rtx temp = gen_reg_rtx (SImode);
596   emit_insn (gen_negsi2 (temp, operands[1]));
597   emit_insn (gen_andsi3 (temp, temp, operands[1]));
598   emit_insn (gen_clzsi2 (temp, temp));
599   emit_move_insn (operands[0], GEN_INT (32));
600   emit_insn (gen_subsi3 (operands[0], operands[0], temp));
601   DONE;
605 ;; Byte swap.
607 (define_insn "bswaphi2"
608   [(set (match_operand:HI 0 "register_operand" "=a")
609         (bswap:HI (match_operand:HI 1 "register_operand" "r")))
610    (clobber (match_scratch:HI 2 "=&a"))]
611   ""
612   "extui\t%2, %1, 8, 8\;slli\t%0, %1, 8\;or\t%0, %0, %2"
613    [(set_attr "type"    "arith")
614     (set_attr "mode"    "HI")
615     (set_attr "length"  "9")])
617 (define_expand "bswapsi2"
618   [(set (match_operand:SI 0 "register_operand" "")
619         (bswap:SI (match_operand:SI 1 "register_operand" "")))]
620   "!optimize_debug && optimize > 1"
622   /* GIMPLE manual byte-swapping recognition is now activated.
623      For both built-in and manual bswaps, emit corresponding library call
624      if optimizing for size, or a series of dedicated machine instructions
625      if otherwise.  */
626   if (optimize_size)
627     emit_library_call_value (optab_libfunc (bswap_optab, SImode),
628                              operands[0], LCT_NORMAL, SImode,
629                              operands[1], SImode);
630   else
631     emit_insn (gen_bswapsi2_internal (operands[0], operands[1]));
632   DONE;
635 (define_insn "bswapsi2_internal"
636   [(set (match_operand:SI 0 "register_operand" "=a,&a")
637         (bswap:SI (match_operand:SI 1 "register_operand" "0,r")))
638    (clobber (match_scratch:SI 2 "=&a,X"))]
639   "!optimize_debug && optimize > 1 && !optimize_size"
641   rtx_insn *prev_insn = prev_nonnote_nondebug_insn (insn);
642   const char *init = "ssai\t8\;";
643   static char result[128];
644   if (prev_insn && NONJUMP_INSN_P (prev_insn))
645     {
646       rtx x = PATTERN (prev_insn);
647       if (GET_CODE (x) == PARALLEL && XVECLEN (x, 0) == 2
648           && GET_CODE (XVECEXP (x, 0, 0)) == SET
649           && GET_CODE (XVECEXP (x, 0, 1)) == CLOBBER)
650         {
651           x = XEXP (XVECEXP (x, 0, 0), 1);
652           if (GET_CODE (x) == BSWAP && GET_MODE (x) == SImode)
653             init = "";
654         }
655     }
656   sprintf (result,
657            (which_alternative == 0)
658            ? "%s" "srli\t%%2, %%1, 16\;src\t%%2, %%2, %%1\;src\t%%2, %%2, %%2\;src\t%%0, %%1, %%2"
659            : "%s" "srli\t%%0, %%1, 16\;src\t%%0, %%0, %%1\;src\t%%0, %%0, %%0\;src\t%%0, %%1, %%0",
660            init);
661   return result;
663    [(set_attr "type"    "arith,arith")
664     (set_attr "mode"    "SI")
665     (set_attr "length"  "15,15")])
667 (define_expand "bswapdi2"
668   [(set (match_operand:DI 0 "register_operand" "")
669         (bswap:DI (match_operand:DI 1 "register_operand" "")))]
670   "!optimize_debug && optimize > 1 && optimize_size"
672   /* Replace with a single DImode library call.
673      Without this, two SImode library calls are emitted.  */
674   emit_library_call_value (optab_libfunc (bswap_optab, DImode),
675                            operands[0], LCT_NORMAL, DImode,
676                            operands[1], DImode);
677   DONE;
681 ;; Negation and one's complement.
683 (define_insn "negsi2"
684   [(set (match_operand:SI 0 "register_operand" "=a")
685         (neg:SI (match_operand:SI 1 "register_operand" "r")))]
686   ""
687   "neg\t%0, %1"
688   [(set_attr "type"     "arith")
689    (set_attr "mode"     "SI")
690    (set_attr "length"   "3")])
692 (define_insn_and_split "one_cmplsi2"
693   [(set (match_operand:SI 0 "register_operand" "=a")
694         (not:SI (match_operand:SI 1 "register_operand" "r")))]
695   ""
696   "#"
697   "&& can_create_pseudo_p ()"
698   [(set (match_dup 2)
699         (const_int -1))
700    (set (match_dup 0)
701         (xor:SI (match_dup 1)
702                 (match_dup 2)))]
704   operands[2] = gen_reg_rtx (SImode);
706   [(set_attr "type"     "arith")
707    (set_attr "mode"     "SI")
708    (set (attr "length")
709         (if_then_else (match_test "TARGET_DENSITY")
710                       (const_int 5)
711                       (const_int 6)))])
713 (define_insn "negsf2"
714   [(set (match_operand:SF 0 "register_operand" "=f")
715         (neg:SF (match_operand:SF 1 "register_operand" "f")))]
716   "TARGET_HARD_FLOAT"
717   "neg.s\t%0, %1"
718   [(set_attr "type"     "farith")
719    (set_attr "mode"     "SF")
720    (set_attr "length"   "3")])
723 ;; Logical instructions.
725 (define_insn "andsi3"
726   [(set (match_operand:SI 0 "register_operand" "=a,a")
727         (and:SI (match_operand:SI 1 "register_operand" "%r,r")
728                 (match_operand:SI 2 "mask_operand" "P,r")))]
729   ""
730   "@
731    extui\t%0, %1, 0, %K2
732    and\t%0, %1, %2"
733   [(set_attr "type"     "arith,arith")
734    (set_attr "mode"     "SI")
735    (set_attr "length"   "3,3")])
737 (define_insn_and_split "*andsi3_bitcmpl"
738   [(set (match_operand:SI 0 "register_operand" "=a")
739         (and:SI (not:SI (match_operand:SI 1 "register_operand" "r"))
740                 (match_operand:SI 2 "register_operand" "r")))]
741   ""
742   "#"
743   "&& can_create_pseudo_p ()"
744   [(set (match_dup 3)
745         (and:SI (match_dup 1)
746                 (match_dup 2)))
747    (set (match_dup 0)
748         (xor:SI (match_dup 3)
749                 (match_dup 2)))]
751   operands[3] = gen_reg_rtx (SImode);
753   [(set_attr "type"     "arith")
754    (set_attr "mode"     "SI")
755    (set_attr "length"   "6")])
757 (define_insn_and_split "*andsi3_const_pow2_minus_one"
758   [(set (match_operand:SI 0 "register_operand" "=a")
759         (and:SI (match_operand:SI 1 "register_operand" "r")
760                 (match_operand:SI 2 "const_int_operand" "i")))]
761   "IN_RANGE (exact_log2 (INTVAL (operands[2]) + 1), 17, 31)"
762   "#"
763   "&& 1"
764   [(set (match_dup 0)
765         (ashift:SI (match_dup 1)
766                    (match_dup 2)))
767    (set (match_dup 0)
768         (lshiftrt:SI (match_dup 0)
769                      (match_dup 2)))]
771   operands[2] = GEN_INT (32 - floor_log2 (INTVAL (operands[2]) + 1));
773   [(set_attr "type"     "arith")
774    (set_attr "mode"     "SI")
775    (set (attr "length")
776         (if_then_else (match_test "TARGET_DENSITY
777                                    && INTVAL (operands[2]) == 0x7FFFFFFF")
778                       (const_int 5)
779                       (const_int 6)))])
781 (define_insn_and_split "*andsi3_const_negative_pow2"
782   [(set (match_operand:SI 0 "register_operand" "=a")
783         (and:SI (match_operand:SI 1 "register_operand" "r")
784                 (match_operand:SI 2 "const_int_operand" "i")))]
785   "IN_RANGE (exact_log2 (-INTVAL (operands[2])), 12, 31)"
786   "#"
787   "&& 1"
788   [(set (match_dup 0)
789         (lshiftrt:SI (match_dup 1)
790                      (match_dup 2)))
791    (set (match_dup 0)
792         (ashift:SI (match_dup 0)
793                    (match_dup 2)))]
795   operands[2] = GEN_INT (floor_log2 (-INTVAL (operands[2])));
797   [(set_attr "type"     "arith")
798    (set_attr "mode"     "SI")
799    (set_attr "length"   "6")])
801 (define_insn_and_split "*andsi3_const_shifted_mask"
802   [(set (match_operand:SI 0 "register_operand" "=a")
803         (and:SI (match_operand:SI 1 "register_operand" "r")
804                 (match_operand:SI 2 "shifted_mask_operand" "i")))]
805   "! xtensa_simm12b (INTVAL (operands[2]))"
806   "#"
807   "&& 1"
808   [(set (match_dup 0)
809         (zero_extract:SI (match_dup 1)
810                          (match_dup 3)
811                          (match_dup 4)))
812    (set (match_dup 0)
813         (ashift:SI (match_dup 0)
814                    (match_dup 2)))]
816   HOST_WIDE_INT mask = INTVAL (operands[2]);
817   int shift = ctz_hwi (mask);
818   int mask_size = floor_log2 (((uint32_t)mask >> shift) + 1);
819   int mask_pos = shift;
820   if (BITS_BIG_ENDIAN)
821     mask_pos = (32 - (mask_size + shift)) & 0x1f;
822   operands[2] = GEN_INT (shift);
823   operands[3] = GEN_INT (mask_size);
824   operands[4] = GEN_INT (mask_pos);
826   [(set_attr "type"     "arith")
827    (set_attr "mode"     "SI")
828    (set (attr "length")
829         (if_then_else (match_test "TARGET_DENSITY
830                                    && ctz_hwi (INTVAL (operands[2])) == 1")
831                       (const_int 5)
832                       (const_int 6)))])
834 (define_insn "iorsi3"
835   [(set (match_operand:SI 0 "register_operand" "=a")
836         (ior:SI (match_operand:SI 1 "register_operand" "%r")
837                 (match_operand:SI 2 "register_operand" "r")))]
838   ""
839   "or\t%0, %1, %2"
840   [(set_attr "type"     "arith")
841    (set_attr "mode"     "SI")
842    (set_attr "length"   "3")])
844 (define_expand "xorsi3"
845   [(set (match_operand:SI 0 "register_operand")
846         (xor:SI (match_operand:SI 1 "register_operand")
847                 (match_operand:SI 2 "nonmemory_operand")))]
848   ""
850   if (register_operand (operands[2], SImode))
851     emit_insn (gen_xorsi3_internal (operands[0], operands[1],
852                                     operands[2]));
853   else
854     {
855       rtx (*gen_op)(rtx, rtx, rtx);
856       if (TARGET_DENSITY
857           && CONST_INT_P (operands[2])
858           && INTVAL (operands[2]) == -2147483648L)
859         gen_op = gen_addsi3;
860       else
861         gen_op = gen_xorsi3_internal;
862       emit_insn (gen_op (operands[0], operands[1],
863                          force_reg (SImode, operands[2])));
864     }
865   DONE;
868 (define_insn "xorsi3_internal"
869   [(set (match_operand:SI 0 "register_operand" "=a")
870         (xor:SI (match_operand:SI 1 "register_operand" "%r")
871                 (match_operand:SI 2 "register_operand" "r")))]
872   ""
873   "xor\t%0, %1, %2"
874   [(set_attr "type"     "arith")
875    (set_attr "mode"     "SI")
876    (set_attr "length"   "3")])
878 (define_insn_and_split "*splice_bits"
879   [(set (match_operand:SI 0 "register_operand" "=a")
880         (ior:SI (and:SI (match_operand:SI 1 "register_operand" "r")
881                         (match_operand:SI 3 "const_int_operand" "i"))
882                 (and:SI (match_operand:SI 2 "register_operand" "r")
883                         (match_operand:SI 4 "const_int_operand" "i"))))]
885   "!optimize_debug && optimize
886    && INTVAL (operands[3]) + INTVAL (operands[4]) == -1
887    && (exact_log2 (INTVAL (operands[3]) + 1) > 16
888        || exact_log2 (INTVAL (operands[4]) + 1) > 16)"
889   "#"
890   "&& can_create_pseudo_p ()"
891   [(set (match_dup 5)
892         (ashift:SI (match_dup 1)
893                    (match_dup 4)))
894    (set (match_dup 6)
895         (lshiftrt:SI (match_dup 2)
896                      (match_dup 3)))
897    (set (match_dup 0)
898         (ior:SI (lshiftrt:SI (match_dup 5)
899                              (match_dup 4))
900                 (ashift:SI (match_dup 6)
901                            (match_dup 3))))]
903   int shift;
904   if (INTVAL (operands[3]) < 0)
905     {
906       rtx x;
907       x = operands[1], operands[1] = operands[2], operands[2] = x;
908       x = operands[3], operands[3] = operands[4], operands[4] = x;
909     }
910   shift = floor_log2 (INTVAL (operands[3]) + 1);
911   operands[3] = GEN_INT (shift);
912   operands[4] = GEN_INT (32 - shift);
913   operands[5] = gen_reg_rtx (SImode);
914   operands[6] = gen_reg_rtx (SImode);
916   [(set_attr "type"     "arith")
917    (set_attr "mode"     "SI")
918    (set (attr "length")
919         (if_then_else (match_test "TARGET_DENSITY
920                                    && (INTVAL (operands[3]) == 0x7FFFFFFF
921                                        || INTVAL (operands[4]) == 0x7FFFFFFF)")
922                       (const_int 11)
923                       (const_int 12)))])
926 ;; Zero-extend instructions.
928 (define_insn "zero_extendhisi2"
929   [(set (match_operand:SI 0 "register_operand" "=a,a")
930         (zero_extend:SI (match_operand:HI 1 "nonimmed_operand" "r,U")))]
931   ""
932   "@
933    extui\t%0, %1, 0, 16
934    %v1l16ui\t%0, %1"
935   [(set_attr "type"     "arith,load")
936    (set_attr "mode"     "SI")
937    (set_attr "length"   "3,3")])
939 (define_insn "zero_extendqisi2"
940   [(set (match_operand:SI 0 "register_operand" "=a,a")
941         (zero_extend:SI (match_operand:QI 1 "nonimmed_operand" "r,U")))]
942   ""
943   "@
944    extui\t%0, %1, 0, 8
945    %v1l8ui\t%0, %1"
946   [(set_attr "type"     "arith,load")
947    (set_attr "mode"     "SI")
948    (set_attr "length"   "3,3")])
951 ;; Sign-extend instructions.
953 (define_expand "extendhisi2"
954   [(set (match_operand:SI 0 "register_operand" "")
955         (sign_extend:SI (match_operand:HI 1 "register_operand" "")))]
956   ""
958   if (sext_operand (operands[1], HImode))
959     emit_insn (gen_extendhisi2_internal (operands[0], operands[1]));
960   else
961     xtensa_extend_reg (operands[0], operands[1]);
962   DONE;
965 (define_insn "extendhisi2_internal"
966   [(set (match_operand:SI 0 "register_operand" "=B,a")
967         (sign_extend:SI (match_operand:HI 1 "sext_operand" "r,U")))]
968   ""
969   "@
970    sext\t%0, %1, 15
971    %v1l16si\t%0, %1"
972   [(set_attr "type"     "arith,load")
973    (set_attr "mode"     "SI")
974    (set_attr "length"   "3,3")])
976 (define_expand "extendqisi2"
977   [(set (match_operand:SI 0 "register_operand" "")
978         (sign_extend:SI (match_operand:QI 1 "register_operand" "")))]
979   ""
981   if (TARGET_SEXT)
982     emit_insn (gen_extendqisi2_internal (operands[0], operands[1]));
983   else
984     xtensa_extend_reg (operands[0], operands[1]);
985   DONE;
988 (define_insn "extendqisi2_internal"
989   [(set (match_operand:SI 0 "register_operand" "=B")
990         (sign_extend:SI (match_operand:QI 1 "register_operand" "r")))]
991   "TARGET_SEXT"
992   "sext\t%0, %1, 7"
993   [(set_attr "type"     "arith")
994    (set_attr "mode"     "SI")
995    (set_attr "length"   "3")])
998 ;; Field extract instructions.
1000 (define_expand "extvsi"
1001   [(set (match_operand:SI 0 "register_operand" "")
1002         (sign_extract:SI (match_operand:SI 1 "register_operand" "")
1003                          (match_operand:SI 2 "const_int_operand" "")
1004                          (match_operand:SI 3 "const_int_operand" "")))]
1005   "TARGET_SEXT"
1007   if (!sext_fldsz_operand (operands[2], SImode))
1008     FAIL;
1010   /* We could expand to a right shift followed by SEXT but that's
1011      no better than the standard left and right shift sequence.  */
1012   if (!lsbitnum_operand (operands[3], SImode))
1013     FAIL;
1015   emit_insn (gen_extvsi_internal (operands[0], operands[1],
1016                                   operands[2], operands[3]));
1017   DONE;
1020 (define_insn "extvsi_internal"
1021   [(set (match_operand:SI 0 "register_operand" "=a")
1022         (sign_extract:SI (match_operand:SI 1 "register_operand" "r")
1023                          (match_operand:SI 2 "sext_fldsz_operand" "i")
1024                          (match_operand:SI 3 "lsbitnum_operand" "i")))]
1025   "TARGET_SEXT"
1027   int fldsz = INTVAL (operands[2]);
1028   operands[2] = GEN_INT (fldsz - 1);
1029   return "sext\t%0, %1, %2";
1031   [(set_attr "type"     "arith")
1032    (set_attr "mode"     "SI")
1033    (set_attr "length"   "3")])
1035 (define_expand "extzvsi"
1036   [(set (match_operand:SI 0 "register_operand" "")
1037         (zero_extract:SI (match_operand:SI 1 "register_operand" "")
1038                          (match_operand:SI 2 "const_int_operand" "")
1039                          (match_operand:SI 3 "const_int_operand" "")))]
1040   ""
1042   if (!extui_fldsz_operand (operands[2], SImode))
1043     FAIL;
1044   emit_insn (gen_extzvsi_internal (operands[0], operands[1],
1045                                    operands[2], operands[3]));
1046   DONE;
1049 (define_insn "extzvsi_internal"
1050   [(set (match_operand:SI 0 "register_operand" "=a")
1051         (zero_extract:SI (match_operand:SI 1 "register_operand" "r")
1052                          (match_operand:SI 2 "extui_fldsz_operand" "i")
1053                          (match_operand:SI 3 "const_int_operand" "i")))]
1054   ""
1056   int shift;
1057   if (BITS_BIG_ENDIAN)
1058     shift = (32 - (INTVAL (operands[2]) + INTVAL (operands[3]))) & 0x1f;
1059   else
1060     shift = INTVAL (operands[3]) & 0x1f;
1061   operands[3] = GEN_INT (shift);
1062   return "extui\t%0, %1, %3, %2";
1064   [(set_attr "type"     "arith")
1065    (set_attr "mode"     "SI")
1066    (set_attr "length"   "3")])
1068 (define_insn_and_split "*extzvsi-1bit_ashlsi3"
1069   [(set (match_operand:SI 0 "register_operand" "=a")
1070         (and:SI (match_operator:SI 4 "logical_shift_operator"
1071                         [(match_operand:SI 1 "register_operand" "r")
1072                          (match_operand:SI 2 "const_int_operand" "i")])
1073                 (match_operand:SI 3 "const_int_operand" "i")))]
1074   "exact_log2 (INTVAL (operands[3])) > 0"
1075   "#"
1076   "&& 1"
1077   [(set (match_dup 0)
1078         (zero_extract:SI (match_dup 1)
1079                          (const_int 1)
1080                          (match_dup 2)))
1081    (set (match_dup 0)
1082         (ashift:SI (match_dup 0)
1083                    (match_dup 3)))]
1085   int pos = INTVAL (operands[2]), shift = floor_log2 (INTVAL (operands[3]));
1086   switch (GET_CODE (operands[4]))
1087     {
1088     case ASHIFT:
1089       pos = shift - pos;
1090       break;
1091     case LSHIFTRT:
1092       pos = shift + pos;
1093       break;
1094     default:
1095       gcc_unreachable ();
1096     }
1097   if (BITS_BIG_ENDIAN)
1098     pos = (32 - (1 + pos)) & 0x1f;
1099   operands[2] = GEN_INT (pos);
1100   operands[3] = GEN_INT (shift);
1102   [(set_attr "type"     "arith")
1103    (set_attr "mode"     "SI")
1104    (set (attr "length")
1105         (if_then_else (match_test "TARGET_DENSITY && INTVAL (operands[3]) == 2")
1106                       (const_int 5)
1107                       (const_int 6)))])
1109 (define_insn_and_split "*extzvsi-1bit_addsubx"
1110   [(set (match_operand:SI 0 "register_operand" "=a")
1111         (match_operator:SI 5 "addsub_operator"
1112                 [(and:SI (match_operator:SI 6 "logical_shift_operator"
1113                                 [(match_operand:SI 1 "register_operand" "r")
1114                                  (match_operand:SI 3 "const_int_operand" "i")])
1115                          (match_operand:SI 4 "const_int_operand" "i"))
1116                  (match_operand:SI 2 "register_operand" "r")]))]
1117   "TARGET_ADDX
1118    && IN_RANGE (exact_log2 (INTVAL (operands[4])), 1, 3)"
1119   "#"
1120   "&& 1"
1121   [(set (match_dup 0)
1122         (zero_extract:SI (match_dup 1)
1123                          (const_int 1)
1124                          (match_dup 3)))
1125    (set (match_dup 0)
1126         (match_op_dup 5
1127                 [(ashift:SI (match_dup 0)
1128                             (match_dup 4))
1129                  (match_dup 2)]))]
1131   int pos = INTVAL (operands[3]), shift = floor_log2 (INTVAL (operands[4]));
1132   switch (GET_CODE (operands[6]))
1133     {
1134     case ASHIFT:
1135       pos = shift - pos;
1136       break;
1137     case LSHIFTRT:
1138       pos = shift + pos;
1139       break;
1140     default:
1141       gcc_unreachable ();
1142     }
1143   if (BITS_BIG_ENDIAN)
1144     pos = (32 - (1 + pos)) & 0x1f;
1145   operands[3] = GEN_INT (pos);
1146   operands[4] = GEN_INT (shift);
1148   [(set_attr "type"     "arith")
1149    (set_attr "mode"     "SI")
1150    (set_attr "length"   "6")])
1153 ;; Conversions.
1155 (define_insn "fix_truncsfsi2"
1156   [(set (match_operand:SI 0 "register_operand" "=a")
1157         (fix:SI (match_operand:SF 1 "register_operand" "f")))]
1158   "TARGET_HARD_FLOAT"
1159   "trunc.s\t%0, %1, 0"
1160   [(set_attr "type"     "fconv")
1161    (set_attr "mode"     "SF")
1162    (set_attr "length"   "3")])
1164 (define_insn "fixuns_truncsfsi2"
1165   [(set (match_operand:SI 0 "register_operand" "=a")
1166         (unsigned_fix:SI (match_operand:SF 1 "register_operand" "f")))]
1167   "TARGET_HARD_FLOAT"
1168   "utrunc.s\t%0, %1, 0"
1169   [(set_attr "type"     "fconv")
1170    (set_attr "mode"     "SF")
1171    (set_attr "length"   "3")])
1173 (define_insn "floatsisf2"
1174   [(set (match_operand:SF 0 "register_operand" "=f")
1175         (float:SF (match_operand:SI 1 "register_operand" "a")))]
1176   "TARGET_HARD_FLOAT"
1177   "float.s\t%0, %1, 0"
1178   [(set_attr "type"     "fconv")
1179    (set_attr "mode"     "SF")
1180    (set_attr "length"   "3")])
1182 (define_insn "floatunssisf2"
1183   [(set (match_operand:SF 0 "register_operand" "=f")
1184         (unsigned_float:SF (match_operand:SI 1 "register_operand" "a")))]
1185   "TARGET_HARD_FLOAT"
1186   "ufloat.s\t%0, %1, 0"
1187   [(set_attr "type"     "fconv")
1188    (set_attr "mode"     "SF")
1189    (set_attr "length"   "3")])
1192 ;; Data movement instructions.
1194 ;; 64-bit Integer moves
1196 (define_expand "movdi"
1197   [(set (match_operand:DI 0 "nonimmed_operand" "")
1198         (match_operand:DI 1 "general_operand" ""))]
1199   ""
1201   if (CONSTANT_P (operands[1]))
1202     {
1203       /* Split in halves if 64-bit Const-to-Reg moves
1204          because of offering further optimization opportunities.  */
1205       if (register_operand (operands[0], DImode))
1206         {
1207           rtx ops[4] = { operands[0], operands[1] };
1208           xtensa_split_DI_reg_imm (ops);
1209           emit_move_insn (ops[0], ops[1]);
1210           emit_move_insn (ops[2], ops[3]);
1211           DONE;
1212         }
1214       if (!TARGET_CONST16)
1215         operands[1] = force_const_mem (DImode, operands[1]);
1216     }
1218   if (!register_operand (operands[0], DImode)
1219       && !register_operand (operands[1], DImode))
1220     operands[1] = force_reg (DImode, operands[1]);
1222   operands[1] = xtensa_copy_incoming_a7 (operands[1]);
1225 (define_insn_and_split "movdi_internal"
1226   [(set (match_operand:DI 0 "nonimmed_operand" "=a,W,a,a,U")
1227         (match_operand:DI 1 "move_operand" "r,i,T,U,r"))]
1228   "register_operand (operands[0], DImode)
1229    || register_operand (operands[1], DImode)"
1230   "#"
1231   "&& reload_completed"
1232   [(set (match_dup 0) (match_dup 2))
1233    (set (match_dup 1) (match_dup 3))]
1235   xtensa_split_operand_pair (operands, SImode);
1236   if (reg_overlap_mentioned_p (operands[0], operands[3]))
1237     {
1238       rtx tmp;
1239       tmp = operands[0], operands[0] = operands[1], operands[1] = tmp;
1240       tmp = operands[2], operands[2] = operands[3], operands[3] = tmp;
1241     }
1244 (define_split
1245   [(set (match_operand:DI 0 "register_operand")
1246         (match_operand:DI 1 "const_int_operand"))]
1247   "!TARGET_CONST16 && !TARGET_AUTO_LITPOOLS
1248    && ! xtensa_split1_finished_p ()"
1249   [(set (match_dup 0)
1250         (match_dup 1))
1251    (set (match_dup 2)
1252         (match_dup 3))]
1254   xtensa_split_DI_reg_imm (operands);
1257 ;; 32-bit Integer moves
1259 (define_expand "movsi"
1260   [(set (match_operand:SI 0 "nonimmed_operand" "")
1261         (match_operand:SI 1 "general_operand" ""))]
1262   ""
1264   if (xtensa_emit_move_sequence (operands, SImode))
1265     DONE;
1268 (define_insn "movsi_internal"
1269   [(set (match_operand:SI 0 "nonimmed_operand" "=D,D,D,D,R,R,a,q,a,a,W,a,a,U,*a,*A")
1270         (match_operand:SI 1 "move_operand" "M,D,d,R,D,d,r,r,I,Y,i,T,U,r,*A,*r"))]
1271   "xtensa_valid_move (SImode, operands)"
1272   "@
1273    movi.n\t%0, %x1
1274    mov.n\t%0, %1
1275    mov.n\t%0, %1
1276    %v1l32i.n\t%0, %1
1277    %v0s32i.n\t%1, %0
1278    %v0s32i.n\t%1, %0
1279    mov\t%0, %1
1280    movsp\t%0, %1
1281    movi\t%0, %x1
1282    movi\t%0, %1
1283    const16\t%0, %t1\;const16\t%0, %b1
1284    %v1l32r\t%0, %1
1285    %v1l32i\t%0, %1
1286    %v0s32i\t%1, %0
1287    rsr\t%0, ACCLO
1288    wsr\t%1, ACCLO"
1289   [(set_attr "type"     "move,move,move,load,store,store,move,move,move,move,move,load,load,store,rsr,wsr")
1290    (set_attr "mode"     "SI")
1291    (set_attr "length"   "2,2,2,2,2,2,3,3,3,3,6,3,3,3,3,3")])
1293 (define_split
1294   [(set (match_operand:SI 0 "register_operand")
1295         (match_operand:SI 1 "const_int_operand"))]
1296   "!TARGET_CONST16 && !TARGET_AUTO_LITPOOLS
1297    && ! xtensa_split1_finished_p ()
1298    && ! xtensa_simm12b (INTVAL (operands[1]))"
1299   [(set (match_dup 0)
1300         (match_dup 1))]
1302   operands[1] = force_const_mem (SImode, operands[1]);
1305 (define_split
1306   [(set (match_operand:SI 0 "register_operand")
1307         (match_operand:SI 1 "constantpool_operand"))]
1308   "! optimize_debug && reload_completed"
1309   [(const_int 0)]
1311   rtx x = avoid_constant_pool_reference (operands[1]);
1312   if (! CONST_INT_P (x))
1313     FAIL;
1314   if (! xtensa_constantsynth (operands[0], INTVAL (x)))
1315     emit_move_insn (operands[0], x);
1316   DONE;
1319 ;; 16-bit Integer moves
1321 (define_expand "movhi"
1322   [(set (match_operand:HI 0 "nonimmed_operand" "")
1323         (match_operand:HI 1 "general_operand" ""))]
1324   ""
1326   if (xtensa_emit_move_sequence (operands, HImode))
1327     DONE;
1330 (define_insn "movhi_internal"
1331   [(set (match_operand:HI 0 "nonimmed_operand" "=D,D,a,a,a,a,U,*a,*A")
1332         (match_operand:HI 1 "move_operand" "M,d,r,I,Y,U,r,*A,*r"))]
1333   "xtensa_valid_move (HImode, operands)"
1334   "@
1335    movi.n\t%0, %x1
1336    mov.n\t%0, %1
1337    mov\t%0, %1
1338    movi\t%0, %x1
1339    movi\t%0, %1
1340    %v1l16ui\t%0, %1
1341    %v0s16i\t%1, %0
1342    rsr\t%0, ACCLO
1343    wsr\t%1, ACCLO"
1344   [(set_attr "type"     "move,move,move,move,move,load,store,rsr,wsr")
1345    (set_attr "mode"     "HI")
1346    (set_attr "length"   "2,2,3,3,3,3,3,3,3")])
1348 ;; 8-bit Integer moves
1350 (define_expand "movqi"
1351   [(set (match_operand:QI 0 "nonimmed_operand" "")
1352         (match_operand:QI 1 "general_operand" ""))]
1353   ""
1355   if (xtensa_emit_move_sequence (operands, QImode))
1356     DONE;
1359 (define_insn "movqi_internal"
1360   [(set (match_operand:QI 0 "nonimmed_operand" "=D,D,a,a,a,U,*a,*A")
1361         (match_operand:QI 1 "move_operand" "M,d,r,I,U,r,*A,*r"))]
1362   "xtensa_valid_move (QImode, operands)"
1363   "@
1364    movi.n\t%0, %x1
1365    mov.n\t%0, %1
1366    mov\t%0, %1
1367    movi\t%0, %x1
1368    %v1l8ui\t%0, %1
1369    %v0s8i\t%1, %0
1370    rsr\t%0, ACCLO
1371    wsr\t%1, ACCLO"
1372   [(set_attr "type"     "move,move,move,move,load,store,rsr,wsr")
1373    (set_attr "mode"     "QI")
1374    (set_attr "length"   "2,2,3,3,3,3,3,3")])
1376 ;; Sub-word reloads from the constant pool.
1378 (define_expand "reload<mode>_literal"
1379   [(parallel [(match_operand:HQI 0 "register_operand" "=r")
1380               (match_operand:HQI 1 "constantpool_operand" "")
1381               (match_operand:SI 2 "register_operand" "=&r")])]
1382   ""
1384   rtx lit, scratch;
1385   unsigned word_off, byte_off;
1387   if (MEM_P (operands[1]))
1388     {
1389       lit = operands[1];
1390       word_off = 0;
1391       byte_off = 0;
1392     }
1393   else
1394     {
1395       gcc_assert (GET_CODE (operands[1]) == SUBREG);
1396       lit = SUBREG_REG (operands[1]);
1397       word_off = SUBREG_BYTE (operands[1]) & ~(UNITS_PER_WORD - 1);
1398       byte_off = SUBREG_BYTE (operands[1]) - word_off;
1399     }
1401   lit = adjust_address (lit, SImode, word_off);
1402   scratch = operands[2];
1403   emit_insn (gen_movsi (scratch, lit));
1404   emit_insn (gen_mov<mode> (operands[0],
1405                             gen_rtx_SUBREG (<MODE>mode, scratch, byte_off)));
1407   DONE;
1410 ;; 32-bit floating point moves
1412 (define_expand "movsf"
1413   [(set (match_operand:SF 0 "nonimmed_operand" "")
1414         (match_operand:SF 1 "general_operand" ""))]
1415   ""
1417   if (!TARGET_CONST16 && !TARGET_AUTO_LITPOOLS && CONSTANT_P (operands[1]))
1418     operands[1] = force_const_mem (SFmode, operands[1]);
1420   if ((!register_operand (operands[0], SFmode)
1421        && !register_operand (operands[1], SFmode))
1422       || (FP_REG_P (xt_true_regnum (operands[0]))
1423           && !(reload_in_progress | reload_completed)
1424           && (constantpool_mem_p (operands[1])
1425               || CONSTANT_P (operands[1]))))
1426     operands[1] = force_reg (SFmode, operands[1]);
1428   operands[1] = xtensa_copy_incoming_a7 (operands[1]);
1431 (define_insn "movsf_internal"
1432   [(set (match_operand:SF 0 "nonimmed_operand" "=f,f,U,D,D,R,a,f,a,a,W,a,a,U")
1433         (match_operand:SF 1 "move_operand" "f,U,f,d,R,d,r,r,f,Y,iF,T,U,r"))]
1434   "((register_operand (operands[0], SFmode)
1435      || register_operand (operands[1], SFmode))
1436     && !(FP_REG_P (xt_true_regnum (operands[0]))
1437          && (constantpool_mem_p (operands[1]) || CONSTANT_P (operands[1]))))"
1438   "@
1439    mov.s\t%0, %1
1440    %v1lsi\t%0, %1
1441    %v0ssi\t%1, %0
1442    mov.n\t%0, %1
1443    %v1l32i.n\t%0, %1
1444    %v0s32i.n\t%1, %0
1445    mov\t%0, %1
1446    wfr\t%0, %1
1447    rfr\t%0, %1
1448    movi\t%0, %y1
1449    const16\t%0, %t1\;const16\t%0, %b1
1450    %v1l32r\t%0, %1
1451    %v1l32i\t%0, %1
1452    %v0s32i\t%1, %0"
1453   [(set_attr "type"     "farith,fload,fstore,move,load,store,move,farith,farith,move,move,load,load,store")
1454    (set_attr "mode"     "SF")
1455    (set_attr "length"   "3,3,3,2,2,2,3,3,3,3,6,3,3,3")])
1457 (define_insn "*lsiu"
1458   [(set (match_operand:SF 0 "register_operand" "=f")
1459         (mem:SF (plus:SI (match_operand:SI 1 "register_operand" "+a")
1460                          (match_operand:SI 2 "fpmem_offset_operand" "i"))))
1461    (set (match_dup 1)
1462         (plus:SI (match_dup 1) (match_dup 2)))]
1463   "TARGET_HARD_FLOAT && !TARGET_HARD_FLOAT_POSTINC"
1465   if (TARGET_SERIALIZE_VOLATILE && volatile_refs_p (PATTERN (insn)))
1466     output_asm_insn ("memw", operands);
1467   return "lsiu\t%0, %1, %2";
1469   [(set_attr "type"     "fload")
1470    (set_attr "mode"     "SF")
1471    (set_attr "length"   "3")])
1473 (define_insn "*ssiu"
1474   [(set (mem:SF (plus:SI (match_operand:SI 0 "register_operand" "+a")
1475                          (match_operand:SI 1 "fpmem_offset_operand" "i")))
1476         (match_operand:SF 2 "register_operand" "f"))
1477    (set (match_dup 0)
1478         (plus:SI (match_dup 0) (match_dup 1)))]
1479   "TARGET_HARD_FLOAT && !TARGET_HARD_FLOAT_POSTINC"
1481   if (TARGET_SERIALIZE_VOLATILE && volatile_refs_p (PATTERN (insn)))
1482     output_asm_insn ("memw", operands);
1483   return "ssiu\t%2, %0, %1";
1485   [(set_attr "type"     "fstore")
1486    (set_attr "mode"     "SF")
1487    (set_attr "length"   "3")])
1489 (define_insn "*lsip"
1490   [(set (match_operand:SF 0 "register_operand" "=f")
1491         (mem:SF (match_operand:SI 1 "register_operand" "+a")))
1492    (set (match_dup 1)
1493         (plus:SI (match_dup 1)
1494                  (match_operand:SI 2 "fpmem_offset_operand" "i")))]
1495   "TARGET_HARD_FLOAT && TARGET_HARD_FLOAT_POSTINC"
1497   if (TARGET_SERIALIZE_VOLATILE && volatile_refs_p (PATTERN (insn)))
1498     output_asm_insn ("memw", operands);
1499   return "lsip\t%0, %1, %2";
1501   [(set_attr "type"     "fload")
1502    (set_attr "mode"     "SF")
1503    (set_attr "length"   "3")])
1505 (define_insn "*ssip"
1506   [(set (mem:SF (match_operand:SI 0 "register_operand" "+a"))
1507         (match_operand:SF 1 "register_operand" "f"))
1508    (set (match_dup 0)
1509         (plus:SI (match_dup 0)
1510                  (match_operand:SI 2 "fpmem_offset_operand" "i")))]
1511   "TARGET_HARD_FLOAT && TARGET_HARD_FLOAT_POSTINC"
1513   if (TARGET_SERIALIZE_VOLATILE && volatile_refs_p (PATTERN (insn)))
1514     output_asm_insn ("memw", operands);
1515   return "ssip\t%1, %0, %2";
1517   [(set_attr "type"     "fstore")
1518    (set_attr "mode"     "SF")
1519    (set_attr "length"   "3")])
1521 (define_split
1522   [(set (match_operand:SF 0 "register_operand")
1523         (match_operand:SF 1 "constantpool_operand"))]
1524   "! optimize_debug && reload_completed"
1525   [(const_int 0)]
1527   rtx x = avoid_constant_pool_reference (operands[1]);
1528   long l;
1529   HOST_WIDE_INT value;
1530   if (! CONST_DOUBLE_P (x) || GET_MODE (x) != SFmode)
1531     FAIL;
1532   REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
1533   x = gen_rtx_REG (SImode, REGNO (operands[0]));
1534   value = (int32_t)l;
1535   if (! xtensa_constantsynth (x, value))
1536     emit_move_insn (x, GEN_INT (value));
1537   DONE;
1540 ;; 64-bit floating point moves
1542 (define_expand "movdf"
1543   [(set (match_operand:DF 0 "nonimmed_operand" "")
1544         (match_operand:DF 1 "general_operand" ""))]
1545   ""
1547   if (CONSTANT_P (operands[1]) && !TARGET_CONST16 && !TARGET_AUTO_LITPOOLS)
1548     operands[1] = force_const_mem (DFmode, operands[1]);
1550   if (!register_operand (operands[0], DFmode)
1551       && !register_operand (operands[1], DFmode))
1552     operands[1] = force_reg (DFmode, operands[1]);
1554   operands[1] = xtensa_copy_incoming_a7 (operands[1]);
1557 (define_insn_and_split "movdf_internal"
1558   [(set (match_operand:DF 0 "nonimmed_operand" "=a,a,W,a,a,U")
1559         (match_operand:DF 1 "move_operand" "r,Y,iF,T,U,r"))]
1560   "register_operand (operands[0], DFmode)
1561    || register_operand (operands[1], DFmode)"
1562   "#"
1563   "&& reload_completed"
1564   [(set (match_dup 0) (match_dup 2))
1565    (set (match_dup 1) (match_dup 3))]
1567   xtensa_split_operand_pair (operands, SFmode);
1568   if (reg_overlap_mentioned_p (operands[0], operands[3]))
1569     {
1570       rtx tmp;
1571       tmp = operands[0], operands[0] = operands[1], operands[1] = tmp;
1572       tmp = operands[2], operands[2] = operands[3], operands[3] = tmp;
1573     }
1576 ;; Block moves
1578 (define_expand "cpymemsi"
1579   [(parallel [(set (match_operand:BLK 0 "" "")
1580                    (match_operand:BLK 1 "" ""))
1581               (use (match_operand:SI 2 "arith_operand" ""))
1582               (use (match_operand:SI 3 "const_int_operand" ""))])]
1583   ""
1585   if (!xtensa_expand_block_move (operands))
1586     FAIL;
1587   DONE;
1590 ;; Block sets
1592 (define_expand "setmemsi"
1593   [(match_operand:BLK 0 "memory_operand")
1594    (match_operand:SI 1 "")
1595    (match_operand:SI 2 "")
1596    (match_operand:SI 3 "const_int_operand")]
1597   "!optimize_debug && optimize"
1599   if (xtensa_expand_block_set (operands))
1600     DONE;
1601   FAIL;
1605 ;; Shift instructions.
1607 (define_expand "ashlsi3"
1608   [(set (match_operand:SI 0 "register_operand" "")
1609         (ashift:SI (match_operand:SI 1 "register_operand" "")
1610                    (match_operand:SI 2 "arith_operand" "")))]
1611   ""
1613   operands[1] = xtensa_copy_incoming_a7 (operands[1]);
1616 (define_insn "ashlsi3_internal"
1617   [(set (match_operand:SI 0 "register_operand" "=a,a")
1618         (ashift:SI (match_operand:SI 1 "register_operand" "r,r")
1619                    (match_operand:SI 2 "arith_operand" "J,r")))]
1620   ""
1621   "@
1622    slli\t%0, %1, %R2
1623    ssl\t%2\;sll\t%0, %1"
1624   [(set_attr "type"     "arith,arith")
1625    (set_attr "mode"     "SI")
1626    (set_attr "length"   "3,6")])
1628 (define_split
1629   [(set (match_operand:SI 0 "register_operand")
1630         (ashift:SI (match_operand:SI 1 "register_operand")
1631                    (const_int 1)))]
1632   "TARGET_DENSITY"
1633   [(set (match_dup 0)
1634         (plus:SI (match_dup 1)
1635                  (match_dup 1)))])
1637 (define_insn "ashrsi3"
1638   [(set (match_operand:SI 0 "register_operand" "=a,a")
1639         (ashiftrt:SI (match_operand:SI 1 "register_operand" "r,r")
1640                      (match_operand:SI 2 "arith_operand" "J,r")))]
1641   ""
1642   "@
1643    srai\t%0, %1, %R2
1644    ssr\t%2\;sra\t%0, %1"
1645   [(set_attr "type"     "arith,arith")
1646    (set_attr "mode"     "SI")
1647    (set_attr "length"   "3,6")])
1649 (define_insn "lshrsi3"
1650   [(set (match_operand:SI 0 "register_operand" "=a,a")
1651         (lshiftrt:SI (match_operand:SI 1 "register_operand" "r,r")
1652                      (match_operand:SI 2 "arith_operand" "J,r")))]
1653   ""
1655   if (which_alternative == 0)
1656     {
1657       if ((INTVAL (operands[2]) & 0x1f) < 16)
1658         return "srli\t%0, %1, %R2";
1659       else
1660         return "extui\t%0, %1, %R2, %L2";
1661     }
1662   return "ssr\t%2\;srl\t%0, %1";
1664   [(set_attr "type"     "arith,arith")
1665    (set_attr "mode"     "SI")
1666    (set_attr "length"   "3,6")])
1668 (define_insn "*shift_per_byte"
1669   [(set (match_operand:SI 0 "register_operand" "=a")
1670         (match_operator:SI 3 "xtensa_shift_per_byte_operator"
1671                 [(match_operand:SI 1 "register_operand" "r")
1672                  (ashift:SI (match_operand:SI 2 "register_operand" "r")
1673                             (const_int 3))]))]
1674   "!optimize_debug && optimize"
1676   switch (GET_CODE (operands[3]))
1677     {
1678     case ASHIFT:        return "ssa8b\t%2\;sll\t%0, %1";
1679     case ASHIFTRT:      return "ssa8l\t%2\;sra\t%0, %1";
1680     case LSHIFTRT:      return "ssa8l\t%2\;srl\t%0, %1";
1681     default:            gcc_unreachable ();
1682     }
1684   [(set_attr "type"     "arith")
1685    (set_attr "mode"     "SI")
1686    (set_attr "length"   "6")])
1688 (define_insn_and_split "*shift_per_byte_omit_AND_0"
1689   [(set (match_operand:SI 0 "register_operand" "=a")
1690         (match_operator:SI 4 "xtensa_shift_per_byte_operator"
1691                 [(match_operand:SI 1 "register_operand" "r")
1692                  (and:SI (ashift:SI (match_operand:SI 2 "register_operand" "r")
1693                                     (const_int 3))
1694                          (match_operand:SI 3 "const_int_operand" "i"))]))]
1695   "!optimize_debug && optimize
1696    && (INTVAL (operands[3]) & 0x1f) == 3 << 3"
1697   "#"
1698   "&& 1"
1699   [(set (match_dup 0)
1700         (match_op_dup 4
1701                 [(match_dup 1)
1702                  (ashift:SI (match_dup 2)
1703                             (const_int 3))]))]
1704   ""
1705   [(set_attr "type"     "arith")
1706    (set_attr "mode"     "SI")
1707    (set_attr "length"   "6")])
1709 (define_insn_and_split "*shift_per_byte_omit_AND_1"
1710   [(set (match_operand:SI 0 "register_operand" "=a")
1711         (match_operator:SI 4 "xtensa_shift_per_byte_operator"
1712                 [(match_operand:SI 1 "register_operand" "r")
1713                  (neg:SI (and:SI (ashift:SI (match_operand:SI 2 "register_operand" "r")
1714                                             (const_int 3))
1715                                  (match_operand:SI 3 "const_int_operand" "i")))]))]
1716   "!optimize_debug && optimize
1717    && (INTVAL (operands[3]) & 0x1f) == 3 << 3"
1718   "#"
1719   "&& can_create_pseudo_p ()"
1720   [(set (match_dup 5)
1721         (neg:SI (match_dup 2)))
1722    (set (match_dup 0)
1723         (match_op_dup 4
1724                 [(match_dup 1)
1725                  (ashift:SI (match_dup 5)
1726                             (const_int 3))]))]
1728   operands[5] = gen_reg_rtx (SImode);
1730   [(set_attr "type"     "arith")
1731    (set_attr "mode"     "SI")
1732    (set_attr "length"   "9")])
1734 (define_insn "*shlrd_reg"
1735   [(set (match_operand:SI 0 "register_operand" "=a")
1736         (match_operator:SI 6 "xtensa_bit_join_operator"
1737                 [(match_operator:SI 4 "logical_shift_operator"
1738                         [(match_operand:SI 1 "register_operand" "r")
1739                          (match_operand:SI 3 "register_operand" "r")])
1740                  (match_operator:SI 5 "logical_shift_operator"
1741                         [(match_operand:SI 2 "register_operand" "r")
1742                          (neg:SI (match_dup 3))])]))]
1743   "!optimize_debug && optimize
1744    && xtensa_shlrd_which_direction (operands[4], operands[5]) != UNKNOWN"
1746   switch (xtensa_shlrd_which_direction (operands[4], operands[5]))
1747     {
1748     case ASHIFT:        return "ssl\t%3\;src\t%0, %1, %2";
1749     case LSHIFTRT:      return "ssr\t%3\;src\t%0, %2, %1";
1750     default:            gcc_unreachable ();
1751     }
1753   [(set_attr "type"     "arith")
1754    (set_attr "mode"     "SI")
1755    (set_attr "length"   "6")])
1757 (define_insn_and_split "*shlrd_reg"
1758   [(set (match_operand:SI 0 "register_operand" "=a")
1759         (match_operator:SI 6 "xtensa_bit_join_operator"
1760                 [(match_operator:SI 4 "logical_shift_operator"
1761                         [(match_operand:SI 1 "register_operand" "r")
1762                          (neg:SI (match_operand:SI 3 "register_operand" "r"))])
1763                  (match_operator:SI 5 "logical_shift_operator"
1764                         [(match_operand:SI 2 "register_operand" "r")
1765                          (match_dup 3)])]))]
1766   "!optimize_debug && optimize
1767    && xtensa_shlrd_which_direction (operands[5], operands[4]) != UNKNOWN"
1768   "#"
1769   "&& 1"
1770   [(set (match_dup 0)
1771         (match_op_dup 6
1772                 [(match_op_dup 5
1773                         [(match_dup 2)
1774                          (match_dup 3)])
1775                  (match_op_dup 4
1776                         [(match_dup 1)
1777                          (neg:SI (match_dup 3))])]))]
1778   ""
1779   [(set_attr "type"     "arith")
1780    (set_attr "mode"     "SI")
1781    (set_attr "length"   "6")])
1784 (define_insn "*shlrd_const"
1785   [(set (match_operand:SI 0 "register_operand" "=a")
1786         (match_operator:SI 7 "xtensa_bit_join_operator"
1787                 [(match_operator:SI 5 "logical_shift_operator"
1788                         [(match_operand:SI 1 "register_operand" "r")
1789                          (match_operand:SI 3 "const_int_operand" "i")])
1790                  (match_operator:SI 6 "logical_shift_operator"
1791                         [(match_operand:SI 2 "register_operand" "r")
1792                          (match_operand:SI 4 "const_int_operand" "i")])]))]
1793   "!optimize_debug && optimize
1794    && xtensa_shlrd_which_direction (operands[5], operands[6]) != UNKNOWN
1795    && IN_RANGE (INTVAL (operands[3]), 1, 31)
1796    && IN_RANGE (INTVAL (operands[4]), 1, 31)
1797    && INTVAL (operands[3]) + INTVAL (operands[4]) == 32"
1799   switch (xtensa_shlrd_which_direction (operands[5], operands[6]))
1800     {
1801     case ASHIFT:        return "ssai\t%L3\;src\t%0, %1, %2";
1802     case LSHIFTRT:      return "ssai\t%R3\;src\t%0, %2, %1";
1803     default:            gcc_unreachable ();
1804     }
1806   [(set_attr "type"     "arith")
1807    (set_attr "mode"     "SI")
1808    (set_attr "length"   "6")])
1810 (define_insn "*shlrd_per_byte"
1811   [(set (match_operand:SI 0 "register_operand" "=a")
1812         (match_operator:SI 6 "xtensa_bit_join_operator"
1813                 [(match_operator:SI 4 "logical_shift_operator"
1814                         [(match_operand:SI 1 "register_operand" "r")
1815                          (ashift:SI (match_operand:SI 2 "register_operand" "r")
1816                                     (const_int 3))])
1817                  (match_operator:SI 5 "logical_shift_operator"
1818                         [(match_operand:SI 3 "register_operand" "r")
1819                          (neg:SI (ashift:SI (match_dup 2)
1820                                             (const_int 3)))])]))]
1821   "!optimize_debug && optimize
1822    && xtensa_shlrd_which_direction (operands[4], operands[5]) != UNKNOWN"
1824   switch (xtensa_shlrd_which_direction (operands[4], operands[5]))
1825     {
1826     case ASHIFT:        return "ssa8b\t%2\;src\t%0, %1, %3";
1827     case LSHIFTRT:      return "ssa8l\t%2\;src\t%0, %3, %1";
1828     default:            gcc_unreachable ();
1829     }
1831   [(set_attr "type"     "arith")
1832    (set_attr "mode"     "SI")
1833    (set_attr "length"   "6")])
1835 (define_insn_and_split "*shlrd_per_byte_omit_AND"
1836   [(set (match_operand:SI 0 "register_operand" "=a")
1837         (match_operator:SI 7 "xtensa_bit_join_operator"
1838                 [(match_operator:SI 5 "logical_shift_operator"
1839                         [(match_operand:SI 1 "register_operand" "r")
1840                          (and:SI (ashift:SI (match_operand:SI 2 "register_operand" "r")
1841                                             (const_int 3))
1842                                  (match_operand:SI 4 "const_int_operand" "i"))])
1843                  (match_operator:SI 6 "logical_shift_operator"
1844                         [(match_operand:SI 3 "register_operand" "r")
1845                          (neg:SI (and:SI (ashift:SI (match_dup 2)
1846                                                     (const_int 3))
1847                                          (match_dup 4)))])]))]
1848   "!optimize_debug && optimize
1849    && xtensa_shlrd_which_direction (operands[5], operands[6]) != UNKNOWN
1850    && (INTVAL (operands[4]) & 0x1f) == 3 << 3"
1851   "#"
1852   "&& 1"
1853   [(set (match_dup 0)
1854         (match_op_dup 7
1855                 [(match_op_dup 5
1856                         [(match_dup 1)
1857                          (ashift:SI (match_dup 2)
1858                                     (const_int 3))])
1859                  (match_op_dup 6
1860                         [(match_dup 3)
1861                          (neg:SI (ashift:SI (match_dup 2)
1862                                             (const_int 3)))])]))]
1863   ""
1864   [(set_attr "type"     "arith")
1865    (set_attr "mode"     "SI")
1866    (set_attr "length"   "6")])
1868 (define_insn "rotlsi3"
1869   [(set (match_operand:SI 0 "register_operand" "=a,a")
1870         (rotate:SI (match_operand:SI 1 "register_operand" "r,r")
1871                      (match_operand:SI 2 "arith_operand" "J,r")))]
1872   ""
1873   "@
1874    ssai\t%L2\;src\t%0, %1, %1
1875    ssl\t%2\;src\t%0, %1, %1"
1876   [(set_attr "type"     "multi,multi")
1877    (set_attr "mode"     "SI")
1878    (set_attr "length"   "6,6")])
1880 (define_insn "rotrsi3"
1881   [(set (match_operand:SI 0 "register_operand" "=a,a")
1882         (rotatert:SI (match_operand:SI 1 "register_operand" "r,r")
1883                      (match_operand:SI 2 "arith_operand" "J,r")))]
1884   ""
1885   "@
1886    ssai\t%R2\;src\t%0, %1, %1
1887    ssr\t%2\;src\t%0, %1, %1"
1888   [(set_attr "type"     "multi,multi")
1889    (set_attr "mode"     "SI")
1890    (set_attr "length"   "6,6")])
1893 ;; Comparisons.
1895 ;; Conditional branches.
1897 (define_expand "cbranchsi4"
1898   [(match_operator 0 "comparison_operator"
1899     [(match_operand:SI 1 "register_operand")
1900      (match_operand:SI 2 "nonmemory_operand")])
1901    (match_operand 3 "")]
1902   ""
1904   xtensa_expand_conditional_branch (operands, SImode);
1905   DONE;
1908 (define_expand "cbranchsf4"
1909   [(match_operator 0 "comparison_operator"
1910     [(match_operand:SF 1 "register_operand")
1911      (match_operand:SF 2 "register_operand")])
1912    (match_operand 3 "")]
1913   "TARGET_HARD_FLOAT"
1915   xtensa_expand_conditional_branch (operands, SFmode);
1916   DONE;
1919 ;; Branch patterns for standard integer comparisons
1921 (define_insn "*btrue"
1922   [(set (pc)
1923         (if_then_else (match_operator 3 "branch_operator"
1924                         [(match_operand:SI 0 "register_operand" "r,r")
1925                          (match_operand:SI 1 "branch_operand" "K,?r")])
1926                       (label_ref (match_operand 2 "" ""))
1927                       (pc)))]
1928   ""
1930   return xtensa_emit_branch (which_alternative == 0, operands);
1932   [(set_attr "type"     "jump,jump")
1933    (set_attr "mode"     "none")
1934    (set (attr "length")
1935         (if_then_else (match_test "TARGET_DENSITY
1936                                    && CONST_INT_P (operands[1])
1937                                    && INTVAL (operands[1]) == 0
1938                                    && (GET_CODE (operands[3]) == EQ
1939                                        || GET_CODE (operands[3]) == NE)")
1940                       (const_int 2)
1941                       (const_int 3)))])
1943 (define_insn_and_split "*btrue_INT_MIN"
1944   [(set (pc)
1945         (if_then_else (match_operator 2 "boolean_operator"
1946                         [(match_operand:SI 0 "register_operand" "r")
1947                          (const_int -2147483648)])
1948                       (label_ref (match_operand 1 ""))
1949                       (pc)))]
1950   "TARGET_ABS"
1951   "#"
1952   "&& can_create_pseudo_p ()"
1953   [(set (match_dup 3)
1954         (abs:SI (match_dup 0)))
1955    (set (pc)
1956         (if_then_else (match_op_dup 2
1957                         [(zero_extract:SI (match_dup 3)
1958                                           (const_int 1)
1959                                           (match_dup 4))
1960                          (const_int 0)])
1961                       (label_ref (match_dup 1))
1962                       (pc)))]
1964   operands[3] = gen_reg_rtx (SImode);
1965   operands[4] = GEN_INT (BITS_BIG_ENDIAN ? 0 : 31);
1966   operands[2] = gen_rtx_fmt_ee (reverse_condition (GET_CODE (operands[2])),
1967                                 VOIDmode, XEXP (operands[2], 0),
1968                                 const0_rtx);
1970   [(set_attr "type"     "jump")
1971    (set_attr "mode"     "none")
1972    (set_attr "length"   "6")])
1974 (define_insn "*ubtrue"
1975   [(set (pc)
1976         (if_then_else (match_operator 3 "ubranch_operator"
1977                         [(match_operand:SI 0 "register_operand" "r,r")
1978                          (match_operand:SI 1 "ubranch_operand" "L,r")])
1979                       (label_ref (match_operand 2 "" ""))
1980                       (pc)))]
1981   ""
1983   return xtensa_emit_branch (which_alternative == 0, operands);
1985   [(set_attr "type"     "jump,jump")
1986    (set_attr "mode"     "none")
1987    (set_attr "length"   "3,3")])
1989 ;; Branch patterns for bit testing
1991 (define_insn "*bittrue"
1992   [(set (pc)
1993         (if_then_else (match_operator 3 "boolean_operator"
1994                         [(zero_extract:SI (match_operand:SI 0 "register_operand" "r,r")
1995                                           (const_int 1)
1996                                           (match_operand:SI 1 "arith_operand" "J,r"))
1997                          (const_int 0)])
1998                       (label_ref (match_operand 2 "" ""))
1999                       (pc)))]
2000   ""
2002   static char result[64];
2003   char op;
2004   switch (GET_CODE (operands[3]))
2005     {
2006     case EQ:    op = 'c'; break;
2007     case NE:    op = 's'; break;
2008     default:    gcc_unreachable ();
2009     }
2010   if (which_alternative == 0)
2011     {
2012       operands[1] = GEN_INT (INTVAL (operands[1]) & 0x1f);
2013       sprintf (result, "bb%ci\t%%0, %%d1, %%2", op);
2014     }
2015   else
2016     sprintf (result, "bb%c\t%%0, %%1, %%2", op);
2017   return result;
2019   [(set_attr "type"     "jump")
2020    (set_attr "mode"     "none")
2021    (set_attr "length"   "3")])
2023 (define_insn "*masktrue"
2024   [(set (pc)
2025         (if_then_else (match_operator 3 "boolean_operator"
2026                         [(and:SI (match_operand:SI 0 "register_operand" "r")
2027                                  (match_operand:SI 1 "register_operand" "r"))
2028                          (const_int 0)])
2029                       (label_ref (match_operand 2 "" ""))
2030                       (pc)))]
2031   ""
2033   switch (GET_CODE (operands[3]))
2034     {
2035     case EQ:    return "bnone\t%0, %1, %2";
2036     case NE:    return "bany\t%0, %1, %2";
2037     default:    gcc_unreachable ();
2038     }
2040   [(set_attr "type"     "jump")
2041    (set_attr "mode"     "none")
2042    (set_attr "length"   "3")])
2044 (define_insn "*masktrue_bitcmpl"
2045   [(set (pc)
2046         (if_then_else (match_operator 3 "boolean_operator"
2047                         [(and:SI (not:SI (match_operand:SI 0 "register_operand" "r"))
2048                                  (match_operand:SI 1 "register_operand" "r"))
2049                          (const_int 0)])
2050                       (label_ref (match_operand 2 "" ""))
2051                       (pc)))]
2052   ""
2054   switch (GET_CODE (operands[3]))
2055     {
2056     case EQ:    return "ball\t%0, %1, %2";
2057     case NE:    return "bnall\t%0, %1, %2";
2058     default:    gcc_unreachable ();
2059     }
2061   [(set_attr "type"     "jump")
2062    (set_attr "mode"     "none")
2063    (set_attr "length"   "3")])
2065 (define_insn_and_split "*masktrue_const_bitcmpl"
2066   [(set (pc)
2067         (if_then_else (match_operator 3 "boolean_operator"
2068                         [(and:SI (not:SI (match_operand:SI 0 "register_operand" "r"))
2069                                  (match_operand:SI 1 "const_int_operand" "i"))
2070                          (const_int 0)])
2071                       (label_ref (match_operand 2 "" ""))
2072                       (pc)))]
2073   "exact_log2 (INTVAL (operands[1])) < 0"
2074   "#"
2075   "&& can_create_pseudo_p ()"
2076   [(set (match_dup 4)
2077         (match_dup 1))
2078    (set (pc)
2079         (if_then_else (match_op_dup 3
2080                         [(and:SI (not:SI (match_dup 0))
2081                                  (match_dup 4))
2082                          (const_int 0)])
2083                       (label_ref (match_dup 2))
2084                       (pc)))]
2086   operands[4] = gen_reg_rtx (SImode);
2088   [(set_attr "type"     "jump")
2089    (set_attr "mode"     "none")
2090    (set (attr "length")
2091         (if_then_else (match_test "TARGET_DENSITY
2092                                    && IN_RANGE (INTVAL (operands[1]), -32, 95)")
2093                       (const_int 5)
2094                       (if_then_else (match_test "xtensa_simm12b (INTVAL (operands[1]))")
2095                                     (const_int 6)
2096                                     (const_int 10))))])
2098 (define_split
2099   [(set (pc)
2100         (if_then_else (match_operator 2 "boolean_operator"
2101                         [(subreg:HQI (not:SI (match_operand:SI 0 "register_operand")) 0)
2102                          (const_int 0)])
2103                       (label_ref (match_operand 1 ""))
2104                       (pc)))]
2105   "!BYTES_BIG_ENDIAN"
2106   [(set (pc)
2107         (if_then_else (match_op_dup 2
2108                         [(and:SI (not:SI (match_dup 0))
2109                                  (match_dup 3))
2110                          (const_int 0)])
2111                       (label_ref (match_dup 1))
2112                       (pc)))]
2114   operands[3] = GEN_INT ((1 << GET_MODE_BITSIZE (<MODE>mode)) - 1);
2117 (define_split
2118   [(set (pc)
2119         (if_then_else (match_operator 2 "boolean_operator"
2120                         [(subreg:HI (not:SI (match_operand:SI 0 "register_operand")) 2)
2121                          (const_int 0)])
2122                       (label_ref (match_operand 1 ""))
2123                       (pc)))]
2124   "BYTES_BIG_ENDIAN"
2125   [(set (pc)
2126         (if_then_else (match_op_dup 2
2127                         [(and:SI (not:SI (match_dup 0))
2128                                  (const_int 65535))
2129                          (const_int 0)])
2130                       (label_ref (match_dup 1))
2131                       (pc)))])
2133 (define_split
2134   [(set (pc)
2135         (if_then_else (match_operator 2 "boolean_operator"
2136                         [(subreg:QI (not:SI (match_operand:SI 0 "register_operand")) 3)
2137                          (const_int 0)])
2138                       (label_ref (match_operand 1 ""))
2139                       (pc)))]
2140   "BYTES_BIG_ENDIAN"
2141   [(set (pc)
2142         (if_then_else (match_op_dup 2
2143                         [(and:SI (not:SI (match_dup 0))
2144                                  (const_int 255))
2145                          (const_int 0)])
2146                       (label_ref (match_dup 1))
2147                       (pc)))])
2149 (define_insn_and_split "*masktrue_const_pow2_minus_one"
2150   [(set (pc)
2151         (if_then_else (match_operator 4 "boolean_operator"
2152                         [(and:SI (match_operand:SI 0 "register_operand" "r")
2153                                  (match_operand:SI 1 "const_int_operand" "i"))
2154                          (match_operand:SI 2 "const_int_operand" "i")])
2155                       (label_ref (match_operand 3 "" ""))
2156                       (pc)))]
2157   "IN_RANGE (exact_log2 (INTVAL (operands[1]) + 1), 17, 31)
2158    /* && (~INTVAL (operands[1]) & INTVAL (operands[2])) == 0  // can be omitted */
2159    && xtensa_b4const_or_zero (INTVAL (operands[2]) << (32 - floor_log2 (INTVAL (operands[1]) + 1)))"
2160   "#"
2161   "&& can_create_pseudo_p ()"
2162   [(set (match_dup 5)
2163         (ashift:SI (match_dup 0)
2164                    (match_dup 1)))
2165    (set (pc)
2166         (if_then_else (match_op_dup 4
2167                         [(match_dup 5)
2168                          (match_dup 2)])
2169                       (label_ref (match_dup 3))
2170                       (pc)))]
2172   int shift = 32 - floor_log2 (INTVAL (operands[1]) + 1);
2173   operands[1] = GEN_INT (shift);
2174   operands[2] = GEN_INT (INTVAL (operands[2]) << shift);
2175   operands[5] = gen_reg_rtx (SImode);
2177   [(set_attr "type"     "jump")
2178    (set_attr "mode"     "none")
2179    (set (attr "length")
2180         (if_then_else (match_test "(TARGET_DENSITY && INTVAL (operands[1]) == 0x7FFFFFFF)
2181                                    && INTVAL (operands[2]) == 0")
2182                       (const_int 4)
2183                       (if_then_else (match_test "TARGET_DENSITY
2184                                                  && (INTVAL (operands[1]) == 0x7FFFFFFF
2185                                                      || INTVAL (operands[2]) == 0)")
2186                                     (const_int 5)
2187                                     (const_int 6))))])
2189 (define_insn_and_split "*masktrue_const_negative_pow2"
2190   [(set (pc)
2191         (if_then_else (match_operator 4 "boolean_operator"
2192                         [(and:SI (match_operand:SI 0 "register_operand" "r")
2193                                  (match_operand:SI 1 "const_int_operand" "i"))
2194                          (match_operand:SI 2 "const_int_operand" "i")])
2195                       (label_ref (match_operand 3 "" ""))
2196                       (pc)))]
2197   "IN_RANGE (exact_log2 (-INTVAL (operands[1])), 1, 30)
2198    /* && (~INTVAL (operands[1]) & INTVAL (operands[2])) == 0  // can be omitted */
2199    && xtensa_b4const_or_zero (INTVAL (operands[2]) >> floor_log2 (-INTVAL (operands[1])))"
2200   "#"
2201   "&& can_create_pseudo_p ()"
2202   [(set (match_dup 5)
2203         (lshiftrt:SI (match_dup 0)
2204                      (match_dup 1)))
2205    (set (pc)
2206         (if_then_else (match_op_dup 4
2207                         [(match_dup 5)
2208                          (match_dup 2)])
2209                       (label_ref (match_dup 3))
2210                       (pc)))]
2212   int shift = floor_log2 (-INTVAL (operands[1]));
2213   operands[1] = GEN_INT (shift);
2214   operands[2] = GEN_INT (INTVAL (operands[2]) >> shift);
2215   operands[5] = gen_reg_rtx (SImode);
2217   [(set_attr "type"     "jump")
2218    (set_attr "mode"     "none")
2219    (set (attr "length")
2220         (if_then_else (match_test "TARGET_DENSITY && INTVAL (operands[2]) == 0")
2221                       (const_int 5)
2222                       (const_int 6)))])
2224 (define_insn_and_split "*masktrue_const_shifted_mask"
2225   [(set (pc)
2226         (if_then_else (match_operator 4 "boolean_operator"
2227                         [(and:SI (match_operand:SI 0 "register_operand" "r")
2228                                  (match_operand:SI 1 "shifted_mask_operand" "i"))
2229                          (match_operand:SI 2 "const_int_operand" "i")])
2230                       (label_ref (match_operand 3 "" ""))
2231                       (pc)))]
2232   "/* (INTVAL (operands[2]) & ((1 << ctz_hwi (INTVAL (operands[1]))) - 1)) == 0  // can be omitted
2233    && */ xtensa_b4const_or_zero ((uint32_t)INTVAL (operands[2]) >> ctz_hwi (INTVAL (operands[1])))"
2234   "#"
2235   "&& can_create_pseudo_p ()"
2236   [(set (match_dup 6)
2237         (zero_extract:SI (match_dup 0)
2238                          (match_dup 5)
2239                          (match_dup 1)))
2240    (set (pc)
2241         (if_then_else (match_op_dup 4
2242                         [(match_dup 6)
2243                          (match_dup 2)])
2244                       (label_ref (match_dup 3))
2245                       (pc)))]
2247   HOST_WIDE_INT mask = INTVAL (operands[1]);
2248   int shift = ctz_hwi (mask);
2249   int mask_size = floor_log2 (((uint32_t)mask >> shift) + 1);
2250   int mask_pos = shift;
2251   if (BITS_BIG_ENDIAN)
2252     mask_pos = (32 - (mask_size + shift)) & 0x1f;
2253   operands[1] = GEN_INT (mask_pos);
2254   operands[2] = GEN_INT ((uint32_t)INTVAL (operands[2]) >> shift);
2255   operands[5] = GEN_INT (mask_size);
2256   operands[6] = gen_reg_rtx (SImode);
2258   [(set_attr "type"     "jump")
2259    (set_attr "mode"     "none")
2260    (set (attr "length")
2261         (if_then_else (match_test "TARGET_DENSITY
2262                                    && (uint32_t)INTVAL (operands[2]) >> ctz_hwi (INTVAL (operands[1])) == 0")
2263                       (const_int 5)
2264                       (const_int 6)))])
2267 ;; Zero-overhead looping support.
2269 ;; Define the loop insns used by bct optimization to represent the
2270 ;; start and end of a zero-overhead loop.  This start template generates
2271 ;; the loop insn; the end template doesn't generate any instructions since
2272 ;; loop end is handled in hardware.
2274 (define_insn "zero_cost_loop_start"
2275   [(set (pc)
2276         (if_then_else (ne (match_operand:SI 2 "register_operand" "0")
2277                           (const_int 1))
2278                       (label_ref (match_operand 1 "" ""))
2279                       (pc)))
2280    (set (match_operand:SI 0 "register_operand" "=a")
2281         (plus:SI (match_dup 0)
2282                  (const_int -1)))
2283    (unspec [(const_int 0)] UNSPEC_LSETUP_START)]
2284   "TARGET_LOOPS && optimize"
2285   "loop\t%0, %l1_LEND"
2286   [(set_attr "type"     "jump")
2287    (set_attr "mode"     "none")
2288    (set_attr "length"   "3")])
2290 (define_insn "zero_cost_loop_end"
2291   [(set (pc)
2292         (if_then_else (ne (match_operand:SI 2 "nonimmediate_operand" "0,0")
2293                           (const_int 1))
2294                       (label_ref (match_operand 1 "" ""))
2295                       (pc)))
2296    (set (match_operand:SI 0 "nonimmediate_operand" "=a,m")
2297         (plus:SI (match_dup 0)
2298                  (const_int -1)))
2299    (unspec [(const_int 0)] UNSPEC_LSETUP_END)
2300    (clobber (match_scratch:SI 3 "=X,&r"))]
2301   "TARGET_LOOPS && optimize"
2302   "#"
2303   [(set_attr "type"     "jump")
2304    (set_attr "mode"     "none")
2305    (set_attr "length"   "0")])
2307 (define_insn "loop_end"
2308   [(set (pc)
2309         (if_then_else (ne (match_operand:SI 2 "register_operand" "0")
2310                           (const_int 1))
2311                       (label_ref (match_operand 1 "" ""))
2312                       (pc)))
2313    (set (match_operand:SI 0 "register_operand" "=a")
2314         (plus:SI (match_dup 0)
2315                  (const_int -1)))
2316    (unspec [(const_int 0)] UNSPEC_LSETUP_END)]
2317   "TARGET_LOOPS && optimize"
2319   xtensa_emit_loop_end (insn, operands);
2320   return "";
2322   [(set_attr "type"     "jump")
2323    (set_attr "mode"     "none")
2324    (set_attr "length"   "0")])
2326 (define_split
2327   [(set (pc)
2328         (if_then_else (ne (match_operand:SI 0 "nonimmediate_operand" "")
2329                           (const_int 1))
2330                       (label_ref (match_operand 1 "" ""))
2331                       (pc)))
2332    (set (match_operand:SI 2 "nonimmediate_operand" "")
2333         (plus:SI (match_dup 0)
2334                  (const_int -1)))
2335    (unspec [(const_int 0)] UNSPEC_LSETUP_END)
2336    (clobber (match_scratch 3))]
2337   "TARGET_LOOPS && optimize && reload_completed"
2338   [(const_int 0)]
2340   if (!REG_P (operands[0]))
2341     {
2342       rtx test;
2344       /* Fallback into a normal conditional branch insn.  */
2345       emit_move_insn (operands[3], operands[0]);
2346       emit_insn (gen_addsi3 (operands[3], operands[3], constm1_rtx));
2347       emit_move_insn (operands[0], operands[3]);
2348       test = gen_rtx_NE (VOIDmode, operands[3], const0_rtx);
2349       emit_jump_insn (gen_cbranchsi4 (test, operands[3],
2350                                       const0_rtx, operands[1]));
2351     }
2352   else
2353     {
2354       emit_jump_insn (gen_loop_end (operands[0], operands[1], operands[2]));
2355     }
2357   DONE;
2360 ; operand 0 is the loop count pseudo register
2361 ; operand 1 is the label to jump to at the top of the loop
2362 (define_expand "doloop_end"
2363   [(parallel [(set (pc) (if_then_else
2364                           (ne (match_operand:SI 0 "" "")
2365                               (const_int 1))
2366                           (label_ref (match_operand 1 "" ""))
2367                           (pc)))
2368               (set (match_dup 0)
2369                    (plus:SI (match_dup 0)
2370                             (const_int -1)))
2371               (unspec [(const_int 0)] UNSPEC_LSETUP_END)
2372               (clobber (match_dup 2))])] ; match_scratch
2373   "TARGET_LOOPS && optimize"
2375   /* The loop optimizer doesn't check the predicates... */
2376   if (GET_MODE (operands[0]) != SImode)
2377     FAIL;
2378   operands[2] = gen_rtx_SCRATCH (SImode);
2382 ;; Setting a register from a comparison.
2384 (define_expand "cstoresi4"
2385   [(match_operand:SI 0 "register_operand")
2386    (match_operator 1 "xtensa_cstoresi_operator"
2387     [(match_operand:SI 2 "register_operand")
2388      (match_operand:SI 3 "nonmemory_operand")])]
2389   ""
2391   if (!xtensa_expand_scc (operands, SImode))
2392     FAIL;
2393   DONE;
2396 (define_insn "salt"
2397   [(set (match_operand:SI 0 "register_operand" "=a")
2398         (lt:SI (match_operand:SI 1 "register_operand" "r")
2399                (match_operand:SI 2 "register_operand" "r")))]
2400   "TARGET_SALT"
2401   "salt\t%0, %1, %2"
2402   [(set_attr "type"     "arith")
2403    (set_attr "mode"     "SI")
2404    (set_attr "length"   "3")])
2406 (define_insn "saltu"
2407   [(set (match_operand:SI 0 "register_operand" "=a")
2408         (ltu:SI (match_operand:SI 1 "register_operand" "r")
2409                 (match_operand:SI 2 "register_operand" "r")))]
2410   "TARGET_SALT"
2411   "saltu\t%0, %1, %2"
2412   [(set_attr "type"     "arith")
2413    (set_attr "mode"     "SI")
2414    (set_attr "length"   "3")])
2416 (define_expand "cstoresf4"
2417   [(match_operand:SI 0 "register_operand")
2418    (match_operator:SI 1 "comparison_operator"
2419     [(match_operand:SF 2 "register_operand")
2420      (match_operand:SF 3 "register_operand")])]
2421   "TARGET_HARD_FLOAT"
2423   if (!xtensa_expand_scc (operands, SFmode))
2424     FAIL;
2425   DONE;
2430 ;; Conditional moves.
2432 (define_expand "movsicc"
2433   [(set (match_operand:SI 0 "register_operand" "")
2434         (if_then_else:SI (match_operand 1 "comparison_operator" "")
2435                          (match_operand:SI 2 "register_operand" "")
2436                          (match_operand:SI 3 "register_operand" "")))]
2437   ""
2439   if (!xtensa_expand_conditional_move (operands, 0))
2440     FAIL;
2441   DONE;
2444 (define_expand "movsfcc"
2445   [(set (match_operand:SF 0 "register_operand" "")
2446         (if_then_else:SF (match_operand 1 "comparison_operator" "")
2447                          (match_operand:SF 2 "register_operand" "")
2448                          (match_operand:SF 3 "register_operand" "")))]
2449   ""
2451   if (!xtensa_expand_conditional_move (operands, 1))
2452     FAIL;
2453   DONE;
2456 (define_insn "movsicc_internal0"
2457   [(set (match_operand:SI 0 "register_operand" "=a,a")
2458         (if_then_else:SI (match_operator 4 "branch_operator"
2459                            [(match_operand:SI 1 "register_operand" "r,r")
2460                             (const_int 0)])
2461                          (match_operand:SI 2 "register_operand" "r,0")
2462                          (match_operand:SI 3 "register_operand" "0,r")))]
2463   ""
2465   return xtensa_emit_movcc (which_alternative == 1, false, false, operands);
2467   [(set_attr "type"     "move,move")
2468    (set_attr "mode"     "SI")
2469    (set_attr "length"   "3,3")])
2471 (define_insn "movsicc_internal1"
2472   [(set (match_operand:SI 0 "register_operand" "=a,a")
2473         (if_then_else:SI (match_operator 4 "boolean_operator"
2474                            [(match_operand:CC 1 "register_operand" "b,b")
2475                             (const_int 0)])
2476                          (match_operand:SI 2 "register_operand" "r,0")
2477                          (match_operand:SI 3 "register_operand" "0,r")))]
2478   "TARGET_BOOLEANS"
2480   return xtensa_emit_movcc (which_alternative == 1, false, true, operands);
2482   [(set_attr "type"     "move,move")
2483    (set_attr "mode"     "SI")
2484    (set_attr "length"   "3,3")])
2486 (define_insn "movsfcc_internal0"
2487   [(set (match_operand:SF 0 "register_operand" "=a,a,f,f")
2488         (if_then_else:SF (match_operator 4 "branch_operator"
2489                            [(match_operand:SI 1 "register_operand" "r,r,r,r")
2490                             (const_int 0)])
2491                          (match_operand:SF 2 "register_operand" "r,0,f,0")
2492                          (match_operand:SF 3 "register_operand" "0,r,0,f")))]
2493   ""
2495   return xtensa_emit_movcc ((which_alternative & 1) == 1,
2496                             which_alternative >= 2, false, operands);
2498   [(set_attr "type"     "move,move,move,move")
2499    (set_attr "mode"     "SF")
2500    (set_attr "length"   "3,3,3,3")])
2502 (define_insn "movsfcc_internal1"
2503   [(set (match_operand:SF 0 "register_operand" "=a,a,f,f")
2504         (if_then_else:SF (match_operator 4 "boolean_operator"
2505                            [(match_operand:CC 1 "register_operand" "b,b,b,b")
2506                             (const_int 0)])
2507                          (match_operand:SF 2 "register_operand" "r,0,f,0")
2508                          (match_operand:SF 3 "register_operand" "0,r,0,f")))]
2509   "TARGET_BOOLEANS"
2511   return xtensa_emit_movcc ((which_alternative & 1) == 1,
2512                             which_alternative >= 2, true, operands);
2514   [(set_attr "type"     "move,move,move,move")
2515    (set_attr "mode"     "SF")
2516    (set_attr "length"   "3,3,3,3")])
2519 ;; Floating-point comparisons.
2521 (define_insn "s<code>_sf"
2522   [(set (match_operand:CC 0 "register_operand" "=b")
2523         (any_scc_sf:CC (match_operand:SF 1 "register_operand" "f")
2524                        (match_operand:SF 2 "register_operand" "f")))]
2525   "TARGET_HARD_FLOAT"
2526   "<scc_sf>.s\t%0, %1, %2"
2527   [(set_attr "type"     "farith")
2528    (set_attr "mode"     "BL")
2529    (set_attr "length"   "3")])
2532 ;; Unconditional branches.
2534 (define_insn "jump"
2535   [(set (pc)
2536         (label_ref (match_operand 0 "" "")))]
2537   ""
2538   "j\t%l0"
2539   [(set_attr "type"     "jump")
2540    (set_attr "mode"     "none")
2541    (set_attr "length"   "3")])
2543 (define_expand "indirect_jump"
2544   [(set (pc)
2545         (match_operand 0 "register_operand" ""))]
2546   ""
2548   rtx dest = operands[0];
2549   if (GET_CODE (dest) != REG || GET_MODE (dest) != Pmode)
2550     operands[0] = copy_to_mode_reg (Pmode, dest);
2552   emit_jump_insn (gen_indirect_jump_internal (dest));
2553   DONE;
2556 (define_insn "indirect_jump_internal"
2557   [(set (pc) (match_operand:SI 0 "register_operand" "r"))]
2558   ""
2559   "jx\t%0"
2560   [(set_attr "type"     "jump")
2561    (set_attr "mode"     "none")
2562    (set_attr "length"   "3")])
2565 (define_expand "tablejump"
2566   [(use (match_operand:SI 0 "register_operand" ""))
2567    (use (label_ref (match_operand 1 "" "")))]
2568    ""
2570   rtx target = operands[0];
2571   if (flag_pic)
2572     {
2573       /* For PIC, the table entry is relative to the start of the table.  */
2574       rtx label = gen_reg_rtx (SImode);
2575       target = gen_reg_rtx (SImode);
2576       emit_move_insn (label, gen_rtx_LABEL_REF (SImode, operands[1]));
2577       emit_insn (gen_addsi3 (target, operands[0], label));
2578     }
2579   emit_jump_insn (gen_tablejump_internal (target, operands[1]));
2580   DONE;
2583 (define_insn "tablejump_internal"
2584   [(set (pc)
2585         (match_operand:SI 0 "register_operand" "r"))
2586    (use (label_ref (match_operand 1 "" "")))]
2587   ""
2588   "jx\t%0"
2589   [(set_attr "type"     "jump")
2590    (set_attr "mode"     "none")
2591    (set_attr "length"   "3")])
2594 ;; Function calls.
2596 (define_expand "sym_PLT"
2597   [(const (unspec [(match_operand:SI 0 "" "")] UNSPEC_PLT))]
2598   ""
2599   "")
2601 (define_expand "call"
2602   [(call (match_operand 0 "memory_operand" "")
2603          (match_operand 1 "" ""))]
2604   ""
2606   xtensa_expand_call (0, operands);
2607   DONE;
2610 (define_insn "call_internal"
2611   [(call (mem (match_operand:SI 0 "call_insn_operand" "nir"))
2612          (match_operand 1 "" "i"))]
2613   "!SIBLING_CALL_P (insn)"
2615   return xtensa_emit_call (0, operands);
2617   [(set_attr "type"     "call")
2618    (set_attr "mode"     "none")
2619    (set_attr "length"   "3")])
2621 (define_expand "call_value"
2622   [(set (match_operand 0 "register_operand" "")
2623         (call (match_operand 1 "memory_operand" "")
2624               (match_operand 2 "" "")))]
2625   ""
2627   xtensa_expand_call (1, operands);
2628   DONE;
2631 (define_insn "call_value_internal"
2632   [(set (match_operand 0 "register_operand" "=a")
2633         (call (mem (match_operand:SI 1 "call_insn_operand" "nir"))
2634               (match_operand 2 "" "i")))]
2635   "!SIBLING_CALL_P (insn)"
2637   return xtensa_emit_call (1, operands);
2639   [(set_attr "type"     "call")
2640    (set_attr "mode"     "none")
2641    (set_attr "length"   "3")])
2643 (define_expand "sibcall"
2644   [(call (match_operand 0 "memory_operand" "")
2645          (match_operand 1 "" ""))]
2646   "!TARGET_WINDOWED_ABI"
2648   xtensa_expand_call (0, operands);
2649   DONE;
2652 (define_insn "sibcall_internal"
2653   [(call (mem:SI (match_operand:SI 0 "call_insn_operand" "nic"))
2654          (match_operand 1 "" "i"))]
2655   "!TARGET_WINDOWED_ABI && SIBLING_CALL_P (insn)"
2657   return xtensa_emit_sibcall (0, operands);
2659   [(set_attr "type"     "call")
2660    (set_attr "mode"     "none")
2661    (set_attr "length"   "3")])
2663 (define_expand "sibcall_value"
2664   [(set (match_operand 0 "register_operand" "")
2665         (call (match_operand 1 "memory_operand" "")
2666               (match_operand 2 "" "")))]
2667   "!TARGET_WINDOWED_ABI"
2669   xtensa_expand_call (1, operands);
2670   DONE;
2673 (define_insn "sibcall_value_internal"
2674   [(set (match_operand 0 "register_operand" "=a")
2675         (call (mem:SI (match_operand:SI 1 "call_insn_operand" "nic"))
2676               (match_operand 2 "" "i")))]
2677   "!TARGET_WINDOWED_ABI && SIBLING_CALL_P (insn)"
2679   return xtensa_emit_sibcall (1, operands);
2681   [(set_attr "type"     "call")
2682    (set_attr "mode"     "none")
2683    (set_attr "length"   "3")])
2685 (define_expand "untyped_call"
2686   [(parallel [(call (match_operand 0 "")
2687                     (const_int 0))
2688               (match_operand 1 "")
2689               (match_operand 2 "")])]
2690   ""
2692   int i;
2694   emit_call_insn (gen_call (operands[0], const0_rtx));
2696   for (i = 0; i < XVECLEN (operands[2], 0); i++)
2697     {
2698       rtx set = XVECEXP (operands[2], 0, i);
2699       emit_move_insn (SET_DEST (set), SET_SRC (set));
2700     }
2702   emit_insn (gen_blockage ());
2703   DONE;
2706 (define_insn "entry"
2707   [(set (reg:SI A1_REG)
2708         (unspec_volatile:SI [(match_operand:SI 0 "const_int_operand" "i")]
2709                             UNSPECV_ENTRY))]
2710   ""
2711   "entry\tsp, %0"
2712   [(set_attr "type"     "entry")
2713    (set_attr "mode"     "SI")
2714    (set_attr "length"   "3")])
2716 (define_insn "return"
2717   [(return)
2718    (use (reg:SI A0_REG))]
2719   "xtensa_use_return_instruction_p ()"
2721   return TARGET_WINDOWED_ABI ?
2722       (TARGET_DENSITY ? "retw.n" : "retw") :
2723       (TARGET_DENSITY ? "ret.n" : "ret");
2725   [(set_attr "type"     "jump")
2726    (set_attr "mode"     "none")
2727    (set (attr "length")
2728         (if_then_else (match_test "TARGET_DENSITY")
2729                       (const_int 2)
2730                       (const_int 3)))])
2733 ;; Miscellaneous instructions.
2735 ;; In windowed ABI stack pointer adjustment must happen before any access
2736 ;; to the space allocated on stack is allowed, otherwise register spill
2737 ;; area may be clobbered.  That's what frame blockage is supposed to enforce.
2739 (define_expand "allocate_stack"
2740   [(set (match_operand 0 "nonimmed_operand")
2741         (minus (reg A1_REG) (match_operand 1 "add_operand")))
2742    (set (reg A1_REG)
2743         (minus (reg A1_REG) (match_dup 1)))]
2744   "TARGET_WINDOWED_ABI"
2746   if (CONST_INT_P (operands[1]))
2747     {
2748       rtx neg_op0 = GEN_INT (-INTVAL (operands[1]));
2749       emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, neg_op0));
2750     }
2751   else
2752     {
2753       emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx,
2754                              operands[1]));
2755     }
2756   emit_move_insn (operands[0], virtual_stack_dynamic_rtx);
2757   emit_insn (gen_frame_blockage ());
2758   DONE;
2761 (define_expand "prologue"
2762   [(const_int 0)]
2763   ""
2765   xtensa_expand_prologue ();
2766   DONE;
2769 (define_expand "epilogue"
2770   [(return)]
2771   ""
2773   xtensa_expand_epilogue (false);
2774   DONE;
2777 (define_expand "sibcall_epilogue"
2778   [(return)]
2779   "!TARGET_WINDOWED_ABI"
2781   xtensa_expand_epilogue (true);
2782   DONE;
2785 (define_insn "nop"
2786   [(const_int 0)]
2787   ""
2789   return (TARGET_DENSITY ? "nop.n" : "nop");
2791   [(set_attr "type"     "nop")
2792    (set_attr "mode"     "none")
2793    (set (attr "length")
2794         (if_then_else (match_test "TARGET_DENSITY")
2795                       (const_int 2)
2796                       (const_int 3)))])
2798 (define_expand "nonlocal_goto"
2799   [(match_operand:SI 0 "general_operand" "")
2800    (match_operand:SI 1 "general_operand" "")
2801    (match_operand:SI 2 "general_operand" "")
2802    (match_operand:SI 3 "" "")]
2803   "TARGET_WINDOWED_ABI"
2805   xtensa_expand_nonlocal_goto (operands);
2806   DONE;
2809 ;; Stuff an address into the return address register along with the window
2810 ;; size in the high bits.  Because we don't have the window size of the
2811 ;; previous frame, assume the function called out with a CALL8 since that
2812 ;; is what compilers always use.  Note: __builtin_frob_return_addr has
2813 ;; already been applied to the handler, but the generic version doesn't
2814 ;; allow us to frob it quite enough, so we just frob here.
2816 (define_expand "eh_return"
2817   [(use (match_operand 0 "general_operand"))]
2818   ""
2820   if (TARGET_WINDOWED_ABI)
2821     emit_insn (gen_eh_set_a0_windowed (operands[0]));
2822   else
2823     emit_insn (gen_eh_set_a0_call0 (operands[0]));
2824   DONE;
2827 (define_insn_and_split "eh_set_a0_windowed"
2828   [(set (reg:SI A0_REG)
2829         (unspec_volatile:SI [(match_operand:SI 0 "register_operand" "r")]
2830                             UNSPECV_EH_RETURN))
2831    (clobber (match_scratch:SI 1 "=r"))]
2832   ""
2833   "#"
2834   "reload_completed"
2835   [(set (match_dup 1) (ashift:SI (match_dup 0) (const_int 2)))
2836    (set (match_dup 1) (plus:SI (match_dup 1) (const_int 2)))
2837    (set (reg:SI A0_REG) (rotatert:SI (match_dup 1) (const_int 2)))]
2838   "")
2840 (define_insn_and_split "eh_set_a0_call0"
2841   [(unspec_volatile [(match_operand:SI 0 "register_operand" "r")]
2842                     UNSPECV_EH_RETURN)
2843    (clobber (match_scratch:SI 1 "=r"))]
2844   ""
2845   "#"
2846   "reload_completed"
2847   [(const_int 0)]
2849   xtensa_set_return_address (operands[0], operands[1]);
2850   DONE;
2853 ;; UNSPEC_VOLATILE is considered to use and clobber all hard registers and
2854 ;; all of memory.  This blocks insns from being moved across this point.
2856 (define_insn "blockage"
2857   [(unspec_volatile [(const_int 0)] UNSPECV_BLOCKAGE)]
2858   ""
2859   ""
2860   [(set_attr "type"     "nop")
2861    (set_attr "mode"     "none")
2862    (set_attr "length"   "0")])
2864 ;; Do not schedule instructions accessing memory before this point.
2866 (define_expand "frame_blockage"
2867   [(set (match_dup 0)
2868         (unspec:BLK [(match_dup 1)] UNSPEC_FRAME_BLOCKAGE))]
2869   ""
2871   operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
2872   MEM_VOLATILE_P (operands[0]) = 1;
2873   operands[1] = stack_pointer_rtx;
2876 (define_insn "*frame_blockage"
2877   [(set (match_operand:BLK 0 "" "")
2878         (unspec:BLK [(match_operand:SI 1 "" "")] UNSPEC_FRAME_BLOCKAGE))]
2879   ""
2880   ""
2881   [(set_attr "type"     "nop")
2882    (set_attr "mode"     "none")
2883    (set_attr "length"   "0")])
2885 (define_insn "trap"
2886   [(trap_if (const_int 1) (const_int 0))]
2887   ""
2889   if (TARGET_DEBUG)
2890     return "break\t1, 15";
2891   else
2892     return (TARGET_DENSITY ? "ill.n" : "ill");
2894   [(set_attr "type"     "trap")
2895    (set_attr "mode"     "none")
2896    (set (attr "length")
2897         (if_then_else (match_test "!TARGET_DEBUG && TARGET_DENSITY")
2898                       (const_int 2)
2899                       (const_int 3)))])
2901 ;; Setting up a frame pointer is tricky for Xtensa because GCC doesn't
2902 ;; know if a frame pointer is required until the reload pass, and
2903 ;; because there may be an incoming argument value in the hard frame
2904 ;; pointer register (a7).  If there is an incoming argument in that
2905 ;; register, the "set_frame_ptr" insn gets inserted immediately after
2906 ;; the insn that copies the incoming argument to a pseudo or to the
2907 ;; stack.  This serves several purposes here: (1) it keeps the
2908 ;; optimizer from copy-propagating or scheduling the use of a7 as an
2909 ;; incoming argument away from the beginning of the function; (2) we
2910 ;; can use a post-reload splitter to expand away the insn if a frame
2911 ;; pointer is not required, so that the post-reload scheduler can do
2912 ;; the right thing; and (3) it makes it easy for the prologue expander
2913 ;; to search for this insn to determine whether it should add a new insn
2914 ;; to set up the frame pointer.
2916 (define_insn "set_frame_ptr"
2917   [(set (reg:SI A7_REG) (unspec_volatile:SI [(const_int 0)] UNSPECV_SET_FP))]
2918   ""
2920   if (frame_pointer_needed)
2921     return (TARGET_DENSITY ? "mov.n\ta7, sp" : "mov\ta7, sp");
2922   return "";
2924   [(set_attr "type"     "move")
2925    (set_attr "mode"     "SI")
2926    (set (attr "length")
2927         (if_then_else (match_test "TARGET_DENSITY")
2928                       (const_int 2)
2929                       (const_int 3)))])
2931 ;; Post-reload splitter to remove fp assignment when it's not needed.
2932 (define_split
2933   [(set (reg:SI A7_REG) (unspec_volatile:SI [(const_int 0)] UNSPECV_SET_FP))]
2934   "reload_completed && !frame_pointer_needed"
2935   [(unspec [(const_int 0)] UNSPEC_NOP)]
2936   "")
2938 ;; The preceding splitter needs something to split the insn into;
2939 ;; things start breaking if the result is just a "use" so instead we
2940 ;; generate the following insn.
2941 (define_insn "*unspec_nop"
2942   [(unspec [(const_int 0)] UNSPEC_NOP)]
2943   ""
2944   ""
2945   [(set_attr "type"     "nop")
2946    (set_attr "mode"     "none")
2947    (set_attr "length"   "0")])
2950 ;; TLS support
2952 (define_expand "sym_TPOFF"
2953   [(const (unspec [(match_operand:SI 0 "" "")] UNSPEC_TPOFF))]
2954   ""
2955   "")
2957 (define_expand "sym_DTPOFF"
2958   [(const (unspec [(match_operand:SI 0 "" "")] UNSPEC_DTPOFF))]
2959   ""
2960   "")
2962 (define_insn "get_thread_pointersi"
2963   [(set (match_operand:SI 0 "register_operand" "=a")
2964         (unspec:SI [(const_int 0)] UNSPEC_TP))]
2965   "TARGET_THREADPTR"
2966   "rur\t%0, THREADPTR"
2967   [(set_attr "type"     "rsr")
2968    (set_attr "mode"     "SI")
2969    (set_attr "length"   "3")])
2971 (define_insn "set_thread_pointersi"
2972   [(unspec_volatile [(match_operand:SI 0 "register_operand" "r")]
2973                     UNSPECV_SET_TP)]
2974   "TARGET_THREADPTR"
2975   "wur\t%0, THREADPTR"
2976   [(set_attr "type"     "wsr")
2977    (set_attr "mode"     "SI")
2978    (set_attr "length"   "3")])
2980 (define_insn "tls_func"
2981   [(set (match_operand:SI 0 "register_operand" "=a")
2982         (unspec:SI [(match_operand:SI 1 "tls_symbol_operand" "")]
2983                    UNSPEC_TLS_FUNC))]
2984   "TARGET_THREADPTR && HAVE_AS_TLS"
2985   "movi\t%0, %1@TLSFUNC"
2986   [(set_attr "type"     "load")
2987    (set_attr "mode"     "SI")
2988    (set_attr "length"   "3")])
2990 (define_insn "tls_arg"
2991   [(set (match_operand:SI 0 "register_operand" "=a")
2992         (unspec:SI [(match_operand:SI 1 "tls_symbol_operand" "")]
2993                    UNSPEC_TLS_ARG))]
2994   "TARGET_THREADPTR && HAVE_AS_TLS"
2995   "movi\t%0, %1@TLSARG"
2996   [(set_attr "type"     "load")
2997    (set_attr "mode"     "SI")
2998    (set_attr "length"   "3")])
3000 (define_insn "tls_call"
3001   [(set (match_operand:SI 0 "register_operand" "=a")
3002         (call (mem:SI (unspec:SI [(match_operand:SI 1 "register_operand" "r")
3003                                   (match_operand:SI 2 "tls_symbol_operand" "")]
3004                                   UNSPEC_TLS_CALL))
3005               (match_operand 3 "" "i")))]
3006   "TARGET_THREADPTR && HAVE_AS_TLS"
3008   if (TARGET_WINDOWED_ABI)
3009     return "callx8.tls %1, %2@TLSCALL";
3010   else
3011     return "callx0.tls %1, %2@TLSCALL";
3013   [(set_attr "type"     "call")
3014    (set_attr "mode"     "none")
3015    (set_attr "length"   "3")])
3018 ;; Instructions for the Xtensa "boolean" option.
3020 (define_insn "*booltrue"
3021   [(set (pc)
3022         (if_then_else (match_operator 2 "boolean_operator"
3023                          [(match_operand:CC 0 "register_operand" "b")
3024                           (const_int 0)])
3025                       (label_ref (match_operand 1 "" ""))
3026                       (pc)))]
3027   "TARGET_BOOLEANS"
3029   if (GET_CODE (operands[2]) == EQ)
3030     return "bf\t%0, %1";
3031   else
3032     return "bt\t%0, %1";
3034   [(set_attr "type"     "jump")
3035    (set_attr "mode"     "none")
3036    (set_attr "length"   "3")])
3038 (define_insn "*boolfalse"
3039   [(set (pc)
3040         (if_then_else (match_operator 2 "boolean_operator"
3041                          [(match_operand:CC 0 "register_operand" "b")
3042                           (const_int 0)])
3043                       (pc)
3044                       (label_ref (match_operand 1 "" ""))))]
3045   "TARGET_BOOLEANS"
3047   if (GET_CODE (operands[2]) == EQ)
3048     return "bt\t%0, %1";
3049   else
3050     return "bf\t%0, %1";
3052   [(set_attr "type"     "jump")
3053    (set_attr "mode"     "none")
3054    (set_attr "length"   "3")])
3057 ;; Atomic operations
3059 (define_expand "memory_barrier"
3060   [(set (match_dup 0)
3061         (unspec:BLK [(match_dup 0)] UNSPEC_MEMW))]
3062   ""
3064   operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
3065   MEM_VOLATILE_P (operands[0]) = 1;
3068 (define_insn "*memory_barrier"
3069   [(set (match_operand:BLK 0 "" "")
3070         (unspec:BLK [(match_dup 0)] UNSPEC_MEMW))]
3071   ""
3072   "memw"
3073   [(set_attr "type"     "unknown")
3074    (set_attr "mode"     "none")
3075    (set_attr "length"   "3")])
3077 ;; sync_lock_release is only implemented for SImode.
3078 ;; For other modes, just use the default of a store with a memory_barrier.
3079 (define_insn "sync_lock_releasesi"
3080   [(set (match_operand:SI 0 "mem_operand" "=U")
3081         (unspec_volatile:SI
3082           [(match_operand:SI 1 "register_operand" "r")]
3083           UNSPECV_S32RI))]
3084   "TARGET_RELEASE_SYNC"
3085   "s32ri\t%1, %0"
3086   [(set_attr "type"     "store")
3087    (set_attr "mode"     "SI")
3088    (set_attr "length"   "3")])
3090 (define_insn "sync_compare_and_swapsi"
3091   [(parallel
3092     [(set (match_operand:SI 0 "register_operand" "=a")
3093           (match_operand:SI 1 "mem_operand" "+U"))
3094      (set (match_dup 1)
3095           (unspec_volatile:SI
3096             [(match_dup 1)
3097              (match_operand:SI 2 "register_operand" "r")
3098              (match_operand:SI 3 "register_operand" "0")]
3099             UNSPECV_S32C1I))])]
3100   "TARGET_S32C1I"
3101   "wsr\t%2, SCOMPARE1\;s32c1i\t%3, %1"
3102   [(set_attr "type"     "multi")
3103    (set_attr "mode"     "SI")
3104    (set_attr "length"   "6")])
3106 (define_expand "sync_compare_and_swap<mode>"
3107   [(parallel
3108     [(set (match_operand:HQI 0 "register_operand" "")
3109           (match_operand:HQI 1 "mem_operand" ""))
3110      (set (match_dup 1)
3111           (unspec_volatile:HQI
3112             [(match_dup 1)
3113              (match_operand:HQI 2 "register_operand" "")
3114              (match_operand:HQI 3 "register_operand" "")]
3115             UNSPECV_S32C1I))])]
3116   "TARGET_S32C1I"
3118   xtensa_expand_compare_and_swap (operands[0], operands[1],
3119                                   operands[2], operands[3]);
3120   DONE;
3123 (define_expand "sync_lock_test_and_set<mode>"
3124   [(match_operand:HQI 0 "register_operand")
3125    (match_operand:HQI 1 "memory_operand")
3126    (match_operand:HQI 2 "register_operand")]
3127   "TARGET_S32C1I"
3129   xtensa_expand_atomic (SET, operands[0], operands[1], operands[2], false);
3130   DONE;
3133 (define_expand "sync_<atomic><mode>"
3134   [(set (match_operand:HQI 0 "memory_operand")
3135         (ATOMIC:HQI (match_dup 0)
3136                     (match_operand:HQI 1 "register_operand")))]
3137   "TARGET_S32C1I"
3139   xtensa_expand_atomic (<CODE>, NULL_RTX, operands[0], operands[1], false);
3140   DONE;
3143 (define_expand "sync_old_<atomic><mode>"
3144   [(set (match_operand:HQI 0 "register_operand")
3145         (match_operand:HQI 1 "memory_operand"))
3146    (set (match_dup 1)
3147         (ATOMIC:HQI (match_dup 1)
3148                     (match_operand:HQI 2 "register_operand")))]
3149   "TARGET_S32C1I"
3151   xtensa_expand_atomic (<CODE>, operands[0], operands[1], operands[2], false);
3152   DONE;
3155 (define_expand "sync_new_<atomic><mode>"
3156   [(set (match_operand:HQI 0 "register_operand")
3157         (ATOMIC:HQI (match_operand:HQI 1 "memory_operand")
3158                     (match_operand:HQI 2 "register_operand")))
3159    (set (match_dup 1) (ATOMIC:HQI (match_dup 1) (match_dup 2)))]
3160   "TARGET_S32C1I"
3162   xtensa_expand_atomic (<CODE>, operands[0], operands[1], operands[2], true);
3163   DONE;
3166 (define_insn_and_split "*round_up_to_even"
3167   [(set (match_operand:SI 0 "register_operand" "=a")
3168         (and:SI (plus:SI (match_operand:SI 1 "register_operand" "r")
3169                          (const_int 1))
3170                 (const_int -2)))]
3171   ""
3172   "#"
3173   "can_create_pseudo_p ()"
3174   [(set (match_dup 2)
3175         (and:SI (match_dup 1)
3176                 (const_int 1)))
3177    (set (match_dup 0)
3178         (plus:SI (match_dup 2)
3179                  (match_dup 1)))]
3181   operands[2] = gen_reg_rtx (SImode);
3183   [(set_attr "type"     "arith")
3184    (set_attr "mode"     "SI")
3185    (set (attr "length")
3186         (if_then_else (match_test "TARGET_DENSITY")
3187                       (const_int 5)
3188                       (const_int 6)))])
3190 (define_insn_and_split "*signed_ge_zero"
3191   [(set (match_operand:SI 0 "register_operand" "=a")
3192         (ge:SI (match_operand:SI 1 "register_operand" "r")
3193                (const_int 0)))]
3194   ""
3195   "#"
3196   ""
3197   [(set (match_dup 0)
3198         (ashiftrt:SI (match_dup 1)
3199                      (const_int 31)))
3200    (set (match_dup 0)
3201         (plus:SI (match_dup 0)
3202                  (const_int 1)))]
3203   ""
3204   [(set_attr "type"     "arith")
3205    (set_attr "mode"     "SI")
3206    (set (attr "length")
3207         (if_then_else (match_test "TARGET_DENSITY")
3208                       (const_int 5)
3209                       (const_int 6)))])
3211 (define_insn_and_split "eq_zero_NSA"
3212   [(set (match_operand:SI 0 "register_operand" "=a")
3213         (eq:SI (match_operand:SI 1 "register_operand" "r")
3214                (const_int 0)))]
3215   "TARGET_NSA"
3216   "#"
3217   "&& 1"
3218   [(set (match_dup 0)
3219         (clz:SI (match_dup 1)))
3220    (set (match_dup 0)
3221         (lshiftrt:SI (match_dup 0)
3222                      (const_int 5)))]
3223   ""
3224   [(set_attr "type"     "move")
3225    (set_attr "mode"     "SI")
3226    (set_attr "length"   "6")])
3228 (define_insn_and_split "eqne_zero"
3229   [(set (match_operand:SI 0 "register_operand" "=a,&a")
3230         (match_operator:SI 2 "boolean_operator"
3231                 [(match_operand:SI 1 "register_operand" "0,r")
3232                  (const_int 0)]))
3233    (clobber (match_scratch:SI 3 "=&a,X"))]
3234   ""
3235   "#"
3236   "&& reload_completed"
3237   [(const_int 0)]
3239   enum rtx_code code = GET_CODE (operands[2]);
3240   int same_p = REGNO (operands[0]) == REGNO (operands[1]);
3241   emit_move_insn (same_p ? operands[3] : operands[0],
3242                   code == EQ ? constm1_rtx : const1_rtx);
3243   emit_insn (gen_movsicc_internal0 (operands[0], operands[1],
3244                                     same_p ? operands[3] : operands[1],
3245                                     operands[0],
3246                                     gen_rtx_fmt_ee (same_p ? NE : EQ,
3247                                                     VOIDmode,
3248                                                     operands[1],
3249                                                     const0_rtx)));
3250   if (code == EQ)
3251     emit_insn (gen_addsi3 (operands[0], operands[0], const1_rtx));
3252   DONE;
3254   [(set_attr "type"     "move")
3255    (set_attr "mode"     "SI")
3256    (set (attr "length")
3257         (if_then_else (match_test "GET_CODE (operands[2]) == EQ")
3258                       (if_then_else (match_test "TARGET_DENSITY")
3259                                     (const_int 7)
3260                                     (const_int 9))
3261                       (if_then_else (match_test "TARGET_DENSITY")
3262                                     (const_int 5)
3263                                     (const_int 6))))])
3265 (define_insn_and_split "*eqne_zero_masked_bits"
3266   [(set (match_operand:SI 0 "register_operand" "=a")
3267         (match_operator 3 "boolean_operator"
3268                 [(and:SI (match_operand:SI 1 "register_operand" "r")
3269                          (match_operand:SI 2 "const_int_operand" "i"))
3270                  (const_int 0)]))]
3271   "IN_RANGE (exact_log2 (INTVAL (operands[2]) + 1), 17, 31)
3272    || IN_RANGE (exact_log2 (-INTVAL (operands[2])), 1, 30)"
3273   "#"
3274   "&& 1"
3275   [(const_int 0)]
3277   HOST_WIDE_INT mask = INTVAL (operands[2]);
3278   int n;
3279   enum rtx_code code = GET_CODE (operands[3]);
3280   if (IN_RANGE (n = exact_log2 (mask + 1), 17, 31))
3281     emit_insn (gen_ashlsi3 (operands[0], operands[1], GEN_INT (32 - n)));
3282   else
3283     emit_insn (gen_lshrsi3 (operands[0], operands[1],
3284                             GEN_INT (floor_log2 (-mask))));
3285   if (TARGET_NSA && code == EQ)
3286     emit_insn (gen_eq_zero_NSA (operands[0], operands[0]));
3287   else
3288     emit_insn (gen_eqne_zero (operands[0], operands[0],
3289                               gen_rtx_fmt_ee (code, VOIDmode,
3290                                               operands[0], const0_rtx)));
3291   DONE;
3294 (define_insn_and_split "*eqne_INT_MIN"
3295   [(set (match_operand:SI 0 "register_operand" "=a")
3296         (match_operator:SI 2 "boolean_operator"
3297                 [(match_operand:SI 1 "register_operand" "r")
3298                  (const_int -2147483648)]))]
3299   "TARGET_ABS"
3300   "#"
3301   "&& 1"
3302   [(const_int 0)]
3304   emit_insn (gen_abssi2 (operands[0], operands[1]));
3305   if (GET_CODE (operands[2]) == EQ)
3306     emit_insn (gen_lshrsi3 (operands[0], operands[0], GEN_INT (31)));
3307   else
3308     {
3309       emit_insn (gen_ashrsi3 (operands[0], operands[0], GEN_INT (31)));
3310       emit_insn (gen_addsi3 (operands[0], operands[0], const1_rtx));
3311     }
3312   DONE;
3314   [(set_attr "type"     "move")
3315    (set_attr "mode"     "SI")
3316    (set (attr "length")
3317         (if_then_else (match_test "GET_CODE (operands[2]) == EQ")
3318                       (const_int 6)
3319                       (if_then_else (match_test "TARGET_DENSITY")
3320                                     (const_int 8)
3321                                     (const_int 9))))])
3323 (define_peephole2
3324   [(set (match_operand:SI 0 "register_operand")
3325         (match_operand:SI 6 "reload_operand"))
3326    (set (match_operand:SI 1 "register_operand")
3327         (match_operand:SI 7 "reload_operand"))
3328    (set (match_operand:SF 2 "register_operand")
3329         (match_operand:SF 4 "register_operand"))
3330    (set (match_operand:SF 3 "register_operand")
3331         (match_operand:SF 5 "register_operand"))]
3332   "REGNO (operands[0]) == REGNO (operands[4])
3333    && REGNO (operands[1]) == REGNO (operands[5])
3334    && peep2_reg_dead_p (4, operands[0])
3335    && peep2_reg_dead_p (4, operands[1])"
3336   [(set (match_dup 2)
3337         (match_dup 6))
3338    (set (match_dup 3)
3339         (match_dup 7))]
3341   HARD_REG_SET regs;
3342   int i;
3343   CLEAR_HARD_REG_SET (regs);
3344   for (i = 0; i <= 3; ++i)
3345     if (TEST_HARD_REG_BIT (regs, REGNO (operands[i])))
3346       FAIL;
3347     else
3348       SET_HARD_REG_BIT (regs, REGNO (operands[i]));
3349   operands[6] = gen_rtx_MEM (SFmode, XEXP (operands[6], 0));
3350   operands[7] = gen_rtx_MEM (SFmode, XEXP (operands[7], 0));
3353 (define_split
3354   [(clobber (match_operand 0 "register_operand"))]
3355   "HARD_REGISTER_P (operands[0])
3356    && COMPLEX_MODE_P (GET_MODE (operands[0]))"
3357   [(const_int 0)]
3359   auto_sbitmap bmp (FIRST_PSEUDO_REGISTER);
3360   rtx_insn *insn;
3361   rtx reg = gen_rtx_REG (SImode, 0), dest;
3362   unsigned int regno;
3363   sbitmap_iterator iter;
3364   bitmap_set_range (bmp, REGNO (operands[0]), REG_NREGS (operands[0]));
3365   for (insn = next_nonnote_nondebug_insn_bb (curr_insn);
3366        insn; insn = next_nonnote_nondebug_insn_bb (insn))
3367     if (NONJUMP_INSN_P (insn))
3368       {
3369         EXECUTE_IF_SET_IN_BITMAP (bmp, 2, regno, iter)
3370           {
3371             set_regno_raw (reg, regno, REG_NREGS (reg));
3372             if (reg_referenced_p (reg, PATTERN (insn)))
3373               goto ABORT;
3374           }
3375         if (GET_CODE (PATTERN (insn)) == SET
3376             || GET_CODE (PATTERN (insn)) == CLOBBER)
3377           {
3378             dest = SET_DEST (PATTERN (insn));
3379             if (REG_P (dest) && HARD_REGISTER_P (dest))
3380               bitmap_clear_range (bmp, REGNO (dest), REG_NREGS (dest));
3381             else if (SUBREG_P (dest)
3382                      && HARD_REGISTER_P (SUBREG_REG (dest)))
3383               {
3384                 struct subreg_info info;
3385                 subreg_get_info (regno = REGNO (SUBREG_REG (dest)),
3386                                  GET_MODE (SUBREG_REG (dest)),
3387                                  SUBREG_BYTE (dest), GET_MODE (dest),
3388                                  &info);
3389                 if (!info.representable_p)
3390                   break;
3391                 bitmap_clear_range (bmp, regno + info.offset, info.nregs);
3392               }
3393           }
3394         if (bitmap_empty_p (bmp))
3395           goto FALLTHRU;
3396       }
3397     else if (CALL_P (insn))
3398       EXECUTE_IF_SET_IN_BITMAP (bmp, 2, regno, iter)
3399         if (call_used_or_fixed_reg_p (regno))
3400           goto ABORT;
3401 ABORT:
3402   FAIL;
3403 FALLTHRU:;
3406 (define_peephole2
3407   [(set (match_operand:SI 0 "register_operand")
3408         (match_operand:SI 1 "const_int_operand"))
3409    (set (match_dup 0)
3410         (plus:SI (match_dup 0)
3411                  (match_operand:SI 2 "const_int_operand")))
3412    (set (match_operand:SI 3 "register_operand")
3413         (plus:SI (match_operand:SI 4 "register_operand")
3414                  (match_dup 0)))]
3415   "IN_RANGE (INTVAL (operands[1]) + INTVAL (operands[2]),
3416              (-128 - 32768), (127 + 32512))
3417    && REGNO (operands[0]) != REGNO (operands[3])
3418    && REGNO (operands[0]) != REGNO (operands[4])
3419    && peep2_reg_dead_p (3, operands[0])"
3420   [(set (match_dup 3)
3421         (plus:SI (match_dup 4)
3422                  (match_dup 1)))
3423    (set (match_dup 3)
3424         (plus:SI (match_dup 3)
3425                  (match_dup 2)))]
3427   HOST_WIDE_INT value = INTVAL (operands[1]) + INTVAL (operands[2]);
3428   int imm0, imm1;
3429   value += 128;
3430   if (value > 32512)
3431     imm1 = 32512;
3432   else
3433     imm1 = value & ~255;
3434   imm0 = value - imm1 - 128;
3435   operands[1] = GEN_INT (imm0);
3436   operands[2] = GEN_INT (imm1);
3439 (define_peephole2
3440   [(set (match_operand 0 "register_operand")
3441         (match_operand 1 "register_operand"))]
3442   "REG_NREGS (operands[0]) == 1 && GP_REG_P (REGNO (operands[0]))
3443    && REG_NREGS (operands[1]) == 1 && GP_REG_P (REGNO (operands[1]))
3444    && peep2_reg_dead_p (1, operands[1])"
3445   [(const_int 0)]
3447   basic_block bb = BLOCK_FOR_INSN (curr_insn);
3448   rtx_insn *head = BB_HEAD (bb), *insn;
3449   rtx dest = operands[0], src = operands[1], pattern, t_dest, dest_orig;
3450   for (insn = PREV_INSN (curr_insn);
3451        insn && insn != head;
3452        insn = PREV_INSN (insn))
3453     if (CALL_P (insn))
3454       break;
3455     else if (INSN_P (insn))
3456       {
3457         if (GET_CODE (pattern = PATTERN (insn)) == SET
3458             && REG_P (t_dest = SET_DEST (pattern))
3459             && REG_NREGS (t_dest) == 1
3460             && REGNO (t_dest) == REGNO (src))
3461         {
3462           dest_orig = SET_DEST (pattern);
3463           SET_DEST (pattern) = gen_rtx_REG (GET_MODE (t_dest),
3464                                             REGNO (dest));
3465           extract_insn (insn);
3466           if (!constrain_operands (true, get_enabled_alternatives (insn)))
3467             {
3468               SET_DEST (pattern) = dest_orig;
3469               goto ABORT;
3470             }
3471           df_insn_rescan (insn);
3472           goto FALLTHRU;
3473         }
3474         if (reg_overlap_mentioned_p (dest, pattern)
3475             || reg_overlap_mentioned_p (src, pattern)
3476             || set_of (dest, insn)
3477             || set_of (src, insn))
3478           break;
3479       }
3480 ABORT:
3481   FAIL;
3482 FALLTHRU:;