OSDN Git Service

* config/h8300/h8300.md: Tweak comments about peephole2's.
[pf3gnuchains/gcc-fork.git] / gcc / config / h8300 / h8300.md
1 ;; GCC machine description for Renesas H8/300
2 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 ;; 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 ;;   Contributed by Steve Chamberlain (sac@cygnus.com),
6 ;;   Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
7
8 ;; This file is part of GCC.
9
10 ;; GCC is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GCC is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GCC; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;; We compute exact length on each instruction for most of the time.
26 ;; In some case, most notably bit operations that may involve memory
27 ;; operands, the lengths in this file are "worst case".
28
29 ;; On the H8/300H and H8S, adds/subs operate on the 32bit "er"
30 ;; registers.  Right now GCC doesn't expose the "e" half to the
31 ;; compiler, so using add/subs for addhi and subhi is safe.  Long
32 ;; term, we want to expose the "e" half to the compiler (gives us 8
33 ;; more 16bit registers).  At that point addhi and subhi can't use
34 ;; adds/subs.
35
36 ;; There's currently no way to have an insv/extzv expander for the H8/300H
37 ;; because word_mode is different for the H8/300 and H8/300H.
38
39 ;; Shifts/rotates by small constants should be handled by special
40 ;; patterns so we get the length and cc status correct.
41
42 ;; Bitfield operations no longer accept memory operands.  We need
43 ;; to add variants which operate on memory back to the MD.
44
45 ;; ??? Implement remaining bit ops available on the h8300
46
47 ;; ----------------------------------------------------------------------
48 ;; CONSTANTS
49 ;; ----------------------------------------------------------------------
50
51 (define_constants
52   [(UNSPEC_INCDEC       0)
53    (UNSPEC_MONITOR      1)])
54
55 (define_constants
56   [(R0_REG       0)
57    (SC_REG       3)
58    (HFP_REG      6)
59    (SP_REG       7)
60    (MAC_REG      8)
61    (AP_REG       9)
62    (RAP_REG     10)
63    (FP_REG      11)])
64
65 ;; ----------------------------------------------------------------------
66 ;; ATTRIBUTES
67 ;; ----------------------------------------------------------------------
68
69 (define_attr "cpu" "h8300,h8300h"
70   (const (symbol_ref "cpu_type")))
71
72 (define_attr "type" "branch,arith"
73   (const_string "arith"))
74
75 ;; The size of instructions in bytes.
76
77 (define_attr "length" ""
78   (cond [(eq_attr "type" "branch")
79          (if_then_else (and (ge (minus (match_dup 0) (pc))
80                                 (const_int -126))
81                             (le (minus (match_dup 0) (pc))
82                                 (const_int 126)))
83                        (const_int 2)
84                        (if_then_else (and (eq_attr "cpu" "h8300h")
85                                           (and (ge (minus (pc) (match_dup 0))
86                                                    (const_int -32000))
87                                                (le (minus (pc) (match_dup 0))
88                                                    (const_int 32000))))
89                                      (const_int 4)
90                                      (const_int 6)))]
91         (const_int 200)))
92
93 ;; Condition code settings.
94 ;;
95 ;; none - insn does not affect cc
96 ;; none_0hit - insn does not affect cc but it does modify operand 0
97 ;;      This attribute is used to keep track of when operand 0 changes.
98 ;;      See the description of NOTICE_UPDATE_CC for more info.
99 ;; set_znv - insn sets z,n,v to usable values (like a tst insn); c is unknown.
100 ;; set_zn  - insn sets z,n to usable values; v,c are unknown.
101 ;; compare - compare instruction
102 ;; clobber - value of cc is unknown
103
104 (define_attr "cc" "none,none_0hit,set_znv,set_zn,compare,clobber"
105   (const_string "clobber"))
106
107 ;; Provide the maximum length of an assembly instruction in an asm
108 ;; statement.  The maximum length of 14 bytes is achieved on H8SX.
109
110 (define_asm_attributes
111   [(set (attr "length")
112         (cond [(ne (symbol_ref "TARGET_H8300")  (const_int 0)) (const_int 4)
113                (ne (symbol_ref "TARGET_H8300H") (const_int 0)) (const_int 10)
114                (ne (symbol_ref "TARGET_H8300S") (const_int 0)) (const_int 10)]
115               (const_int 14)))])
116 \f
117 ;; ----------------------------------------------------------------------
118 ;; MOVE INSTRUCTIONS
119 ;; ----------------------------------------------------------------------
120
121 ;; movqi
122
123 (define_insn "*movqi_h8300"
124   [(set (match_operand:QI 0 "general_operand_dst" "=r,r ,<,r,r,m")
125         (match_operand:QI 1 "general_operand_src" " I,r>,r,n,m,r"))]
126   "TARGET_H8300
127    && (register_operand (operands[0], QImode)
128        || register_operand (operands[1], QImode))"
129   "@
130    sub.b        %X0,%X0
131    mov.b        %R1,%X0
132    mov.b        %X1,%R0
133    mov.b        %R1,%X0
134    mov.b        %R1,%X0
135    mov.b        %X1,%R0"
136   [(set_attr "length" "2,2,2,2,4,4")
137    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
138
139 (define_insn "*movqi_h8300hs"
140   [(set (match_operand:QI 0 "general_operand_dst" "=r,r ,<,r,r,m")
141         (match_operand:QI 1 "general_operand_src" " I,r>,r,n,m,r"))]
142   "(TARGET_H8300H || TARGET_H8300S)
143    && (register_operand (operands[0], QImode)
144        || register_operand (operands[1], QImode))"
145   "@
146    sub.b        %X0,%X0
147    mov.b        %R1,%X0
148    mov.b        %X1,%R0
149    mov.b        %R1,%X0
150    mov.b        %R1,%X0
151    mov.b        %X1,%R0"
152   [(set (attr "length")
153         (symbol_ref "compute_mov_length (operands)"))
154    (set_attr "cc" "set_zn,set_znv,set_znv,clobber,set_znv,set_znv")])
155
156 (define_expand "movqi"
157   [(set (match_operand:QI 0 "general_operand_dst" "")
158         (match_operand:QI 1 "general_operand_src" ""))]
159   ""
160   "
161 {
162   /* One of the ops has to be in a register.  */
163   if (!register_operand (operand0, QImode)
164       && !register_operand (operand1, QImode))
165     {
166       operands[1] = copy_to_mode_reg (QImode, operand1);
167     }
168 }")
169
170 (define_insn "movstrictqi"
171   [(set (strict_low_part
172          (match_operand:QI 0 "register_operand"    "+r,r,r,r,r"))
173          (match_operand:QI 1 "general_operand_src" " I,r,n,>,m"))]
174   ""
175   "@
176    sub.b        %X0,%X0
177    mov.b        %X1,%X0
178    mov.b        %R1,%X0
179    mov.b        %X1,%X0
180    mov.b        %R1,%X0"
181   [(set (attr "length")
182         (symbol_ref "compute_mov_length (operands)"))
183    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv")])
184
185 ;; movhi
186
187 (define_insn "*movhi_h8300"
188   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,<,r,r,m")
189         (match_operand:HI 1 "general_operand_src" "I,r>,r,i,m,r"))]
190   "TARGET_H8300
191    && (register_operand (operands[0], HImode)
192        || register_operand (operands[1], HImode))
193    && !(GET_CODE (operands[0]) == MEM
194         && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
195         && GET_CODE (XEXP (XEXP (operands[0], 0), 0)) == REG
196         && GET_CODE (operands[1]) == REG
197         && REGNO (XEXP (XEXP (operands[0], 0), 0)) == REGNO (operands[1]))"
198   "@
199    sub.w        %T0,%T0
200    mov.w        %T1,%T0
201    mov.w        %T1,%T0
202    mov.w        %T1,%T0
203    mov.w        %T1,%T0
204    mov.w        %T1,%T0"
205   [(set (attr "length")
206         (symbol_ref "compute_mov_length (operands)"))
207    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
208
209 (define_insn "*movhi_h8300hs"
210   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,<,r,r,m")
211         (match_operand:HI 1 "general_operand_src" "I,r>,r,i,m,r"))]
212   "(TARGET_H8300H || TARGET_H8300S)
213    && (register_operand (operands[0], HImode)
214        || register_operand (operands[1], HImode))"
215   "@
216    sub.w        %T0,%T0
217    mov.w        %T1,%T0
218    mov.w        %T1,%T0
219    mov.w        %T1,%T0
220    mov.w        %T1,%T0
221    mov.w        %T1,%T0"
222   [(set (attr "length")
223         (symbol_ref "compute_mov_length (operands)"))
224    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
225
226 (define_expand "movhi"
227   [(set (match_operand:HI 0 "general_operand_dst" "")
228         (match_operand:HI 1 "general_operand_src" ""))]
229   ""
230   "
231 {
232   /* One of the ops has to be in a register.  */
233   if (!register_operand (operand1, HImode)
234       && !register_operand (operand0, HImode))
235     {
236       operands[1] = copy_to_mode_reg (HImode, operand1);
237     }
238 }")
239
240 (define_insn "movstricthi"
241   [(set (strict_low_part
242          (match_operand:HI 0 "register_operand"    "+r,r,r,r,r"))
243          (match_operand:HI 1 "general_operand_src" " I,r,i,>,m"))]
244   ""
245   "@
246    sub.w        %T0,%T0
247    mov.w        %T1,%T0
248    mov.w        %T1,%T0
249    mov.w        %T1,%T0
250    mov.w        %T1,%T0"
251   [(set (attr "length")
252         (symbol_ref "compute_mov_length (operands)"))
253    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv")])
254
255 ;; movsi
256
257 (define_expand "movsi"
258   [(set (match_operand:SI 0 "general_operand_dst" "")
259         (match_operand:SI 1 "general_operand_src" ""))]
260   ""
261   "
262 {
263   if (TARGET_H8300)
264     {
265       if (h8300_expand_movsi (operands))
266         DONE;
267     }
268   else
269     {
270       /* One of the ops has to be in a register.  */
271       if (!register_operand (operand1, SImode)
272           && !register_operand (operand0, SImode))
273         {
274           operands[1] = copy_to_mode_reg (SImode, operand1);
275         }
276     }
277 }")
278
279 (define_insn "*movsi_h8300"
280   [(set (match_operand:SI 0 "general_operand_dst" "=r,r,r,o,<,r")
281         (match_operand:SI 1 "general_operand_src" "I,r,io,r,r,>"))]
282   "TARGET_H8300
283    && (register_operand (operands[0], SImode)
284        || register_operand (operands[1], SImode))"
285   "*
286 {
287   unsigned int rn = -1;
288   switch (which_alternative)
289     {
290     case 0:
291       return \"sub.w    %e0,%e0\;sub.w  %f0,%f0\";
292     case 1:
293       if (REGNO (operands[0]) < REGNO (operands[1]))
294         return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
295       else
296         return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
297     case 2:
298       /* Make sure we don't trample the register we index with.  */
299       if (GET_CODE (operands[1]) == MEM)
300         {
301           rtx inside = XEXP (operands[1], 0);
302           if (REG_P (inside))
303             {
304               rn = REGNO (inside);
305             }
306           else if (GET_CODE (inside) == PLUS)
307             {
308               rtx lhs = XEXP (inside, 0);
309               rtx rhs = XEXP (inside, 1);
310               if (REG_P (lhs)) rn = REGNO (lhs);
311               if (REG_P (rhs)) rn = REGNO (rhs);
312             }
313         }
314       if (rn == REGNO (operands[0]))
315         {
316           /* Move the second word first.  */
317           return \"mov.w        %f1,%f0\;mov.w  %e1,%e0\";
318         }
319       else
320         {
321           if (GET_CODE (operands[1]) == CONST_INT)
322             {
323               /* If either half is zero, use sub.w to clear that
324                  half.  */
325               if ((INTVAL (operands[1]) & 0xffff) == 0)
326                 return \"mov.w  %e1,%e0\;sub.w  %f0,%f0\";
327               if (((INTVAL (operands[1]) >> 16) & 0xffff) == 0)
328                 return \"sub.w  %e0,%e0\;mov.w  %f1,%f0\";
329               /* If the upper half and the lower half are the same,
330                  copy one half to the other.  */
331               if ((INTVAL (operands[1]) & 0xffff)
332                   == ((INTVAL (operands[1]) >> 16) & 0xffff))
333                 return \"mov.w\\t%e1,%e0\;mov.w\\t%e0,%f0\";
334             }
335           return \"mov.w        %e1,%e0\;mov.w  %f1,%f0\";
336         }
337     case 3:
338       return \"mov.w    %e1,%e0\;mov.w  %f1,%f0\";
339     case 4:
340       return \"mov.w    %f1,%T0\;mov.w  %e1,%T0\";
341     case 5:
342       return \"mov.w    %T1,%e0\;mov.w  %T1,%f0\";
343     default:
344       abort ();
345     }
346 }"
347   [(set (attr "length")
348         (symbol_ref "compute_mov_length (operands)"))])
349
350 (define_insn "*movsi_h8300hs"
351   [(set (match_operand:SI 0 "general_operand_dst" "=r,r,r,<,r,r,m,*a,*a,r")
352         (match_operand:SI 1 "general_operand_src" "I,r,i,r,>,m,r,I,r,*a"))]
353   "(TARGET_H8300S || TARGET_H8300H)
354    && (register_operand (operands[0], SImode)
355        || register_operand (operands[1], SImode))
356    && !(GET_CODE (operands[0]) == MEM
357         && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
358         && GET_CODE (XEXP (XEXP (operands[0], 0), 0)) == REG
359         && GET_CODE (operands[1]) == REG
360         && REGNO (XEXP (XEXP (operands[0], 0), 0)) == REGNO (operands[1]))"
361   "*
362 {
363   switch (which_alternative)
364     {
365     case 0:
366       return \"sub.l    %S0,%S0\";
367     case 7:
368       return \"clrmac\";
369     case 8:
370       return \"clrmac\;ldmac %1,macl\";
371     case 9:
372       return \"stmac    macl,%0\";
373     default:
374       if (GET_CODE (operands[1]) == CONST_INT)
375         {
376           int val = INTVAL (operands[1]);
377
378           /* Look for constants which can be made by adding an 8-bit
379              number to zero in one of the two low bytes.  */
380           if (val == (val & 0xff))
381             {
382               operands[1] = GEN_INT ((char) val & 0xff);
383               return \"sub.l\\t%S0,%S0\;add.b\\t%1,%w0\";
384             }
385
386           if (val == (val & 0xff00))
387             {
388               operands[1] = GEN_INT ((char) (val >> 8) & 0xff);
389               return \"sub.l\\t%S0,%S0\;add.b\\t%1,%x0\";
390             }
391
392           /* Look for constants that can be obtained by subs, inc, and
393              dec to 0.  */
394           switch (val & 0xffffffff)
395             {
396             case 0xffffffff:
397               return \"sub.l\\t%S0,%S0\;subs\\t#1,%S0\";
398             case 0xfffffffe:
399               return \"sub.l\\t%S0,%S0\;subs\\t#2,%S0\";
400             case 0xfffffffc:
401               return \"sub.l\\t%S0,%S0\;subs\\t#4,%S0\";
402
403             case 0x0000ffff:
404               return \"sub.l\\t%S0,%S0\;dec.w\\t#1,%f0\";
405             case 0x0000fffe:
406               return \"sub.l\\t%S0,%S0\;dec.w\\t#2,%f0\";
407
408             case 0xffff0000:
409               return \"sub.l\\t%S0,%S0\;dec.w\\t#1,%e0\";
410             case 0xfffe0000:
411               return \"sub.l\\t%S0,%S0\;dec.w\\t#2,%e0\";
412
413             case 0x00010000:
414               return \"sub.l\\t%S0,%S0\;inc.w\\t#1,%e0\";
415             case 0x00020000:
416               return \"sub.l\\t%S0,%S0\;inc.w\\t#2,%e0\";
417             }
418         }
419     }
420    return \"mov.l       %S1,%S0\";
421 }"
422   [(set (attr "length")
423         (symbol_ref "compute_mov_length (operands)"))
424    (set_attr "cc" "set_zn,set_znv,clobber,set_znv,set_znv,set_znv,set_znv,none_0hit,none_0hit,set_znv")])
425
426 (define_expand "movsf"
427   [(set (match_operand:SF 0 "general_operand_dst" "")
428         (match_operand:SF 1 "general_operand_src" ""))]
429   ""
430   "
431 {
432   if (TARGET_H8300)
433     {
434       if (h8300_expand_movsi (operands))
435         DONE;
436     }
437   else
438     {
439       /* One of the ops has to be in a register.  */
440       if (!register_operand (operand1, SFmode)
441           && !register_operand (operand0, SFmode))
442         {
443           operands[1] = copy_to_mode_reg (SFmode, operand1);
444         }
445     }
446 }")
447
448 (define_insn "*movsf_h8300"
449   [(set (match_operand:SF 0 "general_operand_dst" "=r,r,r,o,<,r")
450         (match_operand:SF 1 "general_operand_src" "G,r,io,r,r,>"))]
451   "TARGET_H8300
452    && (register_operand (operands[0], SFmode)
453        || register_operand (operands[1], SFmode))"
454   "*
455 {
456   /* Copy of the movsi stuff.  */
457   unsigned int rn = -1;
458   switch (which_alternative)
459     {
460     case 0:
461       return \"sub.w    %e0,%e0\;sub.w  %f0,%f0\";
462     case 1:
463       if (REGNO (operands[0]) < REGNO (operands[1]))
464         return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
465       else
466         return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
467     case 2:
468       /* Make sure we don't trample the register we index with.  */
469       if (GET_CODE (operands[1]) == MEM)
470         {
471           rtx inside = XEXP (operands[1], 0);
472           if (REG_P (inside))
473             {
474               rn = REGNO (inside);
475             }
476           else if (GET_CODE (inside) == PLUS)
477             {
478               rtx lhs = XEXP (inside, 0);
479               rtx rhs = XEXP (inside, 1);
480               if (REG_P (lhs)) rn = REGNO (lhs);
481               if (REG_P (rhs)) rn = REGNO (rhs);
482             }
483         }
484       if (rn == REGNO (operands[0]))
485         /* Move the second word first.  */
486         return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
487       else
488         /* Move the first word first.  */
489         return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
490
491     case 3:
492       return \"mov.w    %e1,%e0\;mov.w  %f1,%f0\";
493     case 4:
494       return \"mov.w    %f1,%T0\;mov.w  %e1,%T0\";
495     case 5:
496       return \"mov.w    %T1,%e0\;mov.w  %T1,%f0\";
497     default:
498       abort ();
499     }
500 }"
501   [(set (attr "length")
502         (symbol_ref "compute_mov_length (operands)"))])
503
504 (define_insn "*movsf_h8300hs"
505   [(set (match_operand:SF 0 "general_operand_dst" "=r,r,r,m,<,r")
506         (match_operand:SF 1 "general_operand_src" "G,r,im,r,r,>"))]
507   "(TARGET_H8300H || TARGET_H8300S)
508    && (register_operand (operands[0], SFmode)
509        || register_operand (operands[1], SFmode))"
510   "@
511    sub.l        %S0,%S0
512    mov.l        %S1,%S0
513    mov.l        %S1,%S0
514    mov.l        %S1,%S0
515    mov.l        %S1,%S0
516    mov.l        %S1,%S0"
517   [(set (attr "length")
518         (symbol_ref "compute_mov_length (operands)"))
519    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
520 \f
521 ;; ----------------------------------------------------------------------
522 ;; PUSH INSTRUCTIONS
523 ;; ----------------------------------------------------------------------
524
525 (define_insn "pushqi1_h8300"
526   [(set (reg:HI SP_REG)
527         (plus:HI (reg:HI SP_REG) (const_int -2)))
528    (set (mem:QI (plus:HI (reg:HI SP_REG) (const_int -1)))
529         (match_operand:QI 0 "register_operand" "r"))]
530   "TARGET_H8300
531    && operands[0] != stack_pointer_rtx"
532   "mov.w\\t%T0,@-r7"
533   [(set_attr "length" "2")])
534
535 (define_insn "pushqi1_h8300hs"
536   [(set (reg:SI SP_REG)
537         (plus:SI (reg:SI SP_REG) (const_int -4)))
538    (set (mem:QI (plus:SI (reg:SI SP_REG) (const_int -3)))
539         (match_operand:QI 0 "register_operand" "r"))]
540   "(TARGET_H8300H || TARGET_H8300S)
541    && operands[0] != stack_pointer_rtx"
542   "mov.l\\t%S0,@-er7"
543   [(set_attr "length" "4")])
544
545 (define_insn "pushqi1_h8300hs_normal"
546   [(set (reg:HI SP_REG)
547         (plus:HI (reg:HI SP_REG) (const_int -4)))
548    (set (mem:QI (plus:HI (reg:HI SP_REG) (const_int -3)))
549         (match_operand:QI 0 "register_operand" "r"))]
550   "(TARGET_H8300H || TARGET_H8300S)
551    && operands[0] != stack_pointer_rtx"
552   "mov.l\\t%S0,@-er7"
553   [(set_attr "length" "4")])
554
555 (define_expand "pushqi1"
556   [(match_operand:QI 0 "register_operand" "")]
557   ""
558   "
559 {
560   if (TARGET_H8300)
561     emit_insn (gen_pushqi1_h8300 (operands[0]));
562   else if (!TARGET_NORMAL_MODE)
563     emit_insn (gen_pushqi1_h8300hs (operands[0]));
564   else
565     emit_insn (gen_pushqi1_h8300hs_normal (operands[0]));
566   DONE;
567 }")
568
569 (define_expand "pushhi1_h8300"
570   [(set (mem:HI (pre_dec:HI (reg:HI SP_REG)))
571         (match_operand:HI 0 "register_operand" ""))]
572   "TARGET_H8300
573    && operands[0] != stack_pointer_rtx"
574   "")
575
576 (define_insn "pushhi1_h8300hs"
577   [(set (reg:SI SP_REG)
578         (plus:SI (reg:SI SP_REG) (const_int -4)))
579    (set (mem:HI (plus:SI (reg:SI SP_REG) (const_int -2)))
580         (match_operand:HI 0 "register_operand" "r"))]
581   "(TARGET_H8300H || TARGET_H8300S)
582    && operands[0] != stack_pointer_rtx"
583   "mov.l\\t%S0,@-er7"
584   [(set_attr "length" "4")])
585
586 (define_insn "pushhi1_h8300hs_normal"
587   [(set (reg:HI SP_REG)
588         (plus:HI (reg:HI SP_REG) (const_int -4)))
589    (set (mem:HI (plus:HI (reg:HI SP_REG) (const_int -2)))
590         (match_operand:HI 0 "register_operand" "r"))]
591   "(TARGET_H8300H || TARGET_H8300S)
592    && operands[0] != stack_pointer_rtx"
593   "mov.l\\t%S0,@-er7"
594   [(set_attr "length" "4")])
595
596 (define_expand "pushhi1"
597   [(match_operand:HI 0 "register_operand" "")]
598   ""
599   "
600 {
601   if (TARGET_H8300)
602     emit_insn (gen_pushhi1_h8300 (operands[0]));
603   else if (!TARGET_NORMAL_MODE)
604     emit_insn (gen_pushhi1_h8300hs (operands[0]));
605   else
606     emit_insn (gen_pushhi1_h8300hs_normal (operands[0]));
607   DONE;
608 }")
609 \f
610 ;; ----------------------------------------------------------------------
611 ;; TEST INSTRUCTIONS
612 ;; ----------------------------------------------------------------------
613
614 (define_insn ""
615   [(set (cc0) (zero_extract:HI (match_operand:QI 0 "bit_memory_operand" "r,U")
616                                (const_int 1)
617                                (match_operand 1 "const_int_operand" "n,n")))]
618   "TARGET_H8300"
619   "btst %Z1,%Y0"
620   [(set_attr "length" "2,4")
621    (set_attr "cc" "set_zn,set_zn")])
622
623 (define_insn ""
624   [(set (cc0) (zero_extract:HI (match_operand:HI 0 "register_operand" "r")
625                                (const_int 1)
626                                (match_operand 1 "const_int_operand" "n")))]
627   "TARGET_H8300"
628   "btst %Z1,%Y0"
629   [(set_attr "length" "2")
630    (set_attr "cc" "set_zn")])
631
632 (define_insn_and_split "*tst_extzv_1_n"
633   [(set (cc0)
634         (zero_extract:SI (match_operand:QI 0 "general_operand_src" "r,U,mn>")
635                          (const_int 1)
636                          (match_operand 1 "const_int_operand" "n,n,n")))
637    (clobber (match_scratch:QI 2 "=X,X,&r"))]
638   "(TARGET_H8300H || TARGET_H8300S)"
639   "@
640    btst\\t%Z1,%Y0
641    btst\\t%Z1,%Y0
642    #"
643   "&& reload_completed
644    && !EXTRA_CONSTRAINT (operands[0], 'U')"
645   [(set (match_dup 2)
646         (match_dup 0))
647    (parallel [(set (cc0) (zero_extract:SI (match_dup 2)
648                                           (const_int 1)
649                                           (match_dup 1)))
650               (clobber (scratch:QI))])]
651   ""
652   [(set_attr "length" "2,8,10")
653    (set_attr "cc" "set_zn,set_zn,set_zn")])
654
655 (define_insn ""
656   [(set (cc0) (zero_extract:SI (match_operand:SI 0 "register_operand" "r")
657                                (const_int 1)
658                                (match_operand 1 "const_int_operand" "n")))]
659   "(TARGET_H8300H || TARGET_H8300S)
660    && INTVAL (operands[1]) <= 15"
661   "btst %Z1,%Y0"
662   [(set_attr "length" "2")
663    (set_attr "cc" "set_zn")])
664
665 (define_insn_and_split "*tstsi_upper_bit"
666   [(set (cc0)
667         (zero_extract:SI (match_operand:SI 0 "register_operand" "r")
668                          (const_int 1)
669                          (match_operand 1 "const_int_operand" "n")))
670    (clobber (match_scratch:SI 2 "=&r"))]
671   "(TARGET_H8300H || TARGET_H8300S)
672    && INTVAL (operands[1]) >= 16"
673   "#"
674   "&& reload_completed"
675   [(set (match_dup 2)
676         (ior:SI (and:SI (match_dup 2)
677                         (const_int -65536))
678                 (lshiftrt:SI (match_dup 0)
679                              (const_int 16))))
680    (set (cc0)
681         (zero_extract:SI (match_dup 2)
682                          (const_int 1)
683                          (match_dup 3)))]
684   "operands[3] = GEN_INT (INTVAL (operands[1]) - 16);")
685
686 (define_insn "*tstsi_variable_bit"
687   [(set (cc0)
688         (zero_extract:SI (match_operand:SI 0 "register_operand" "r")
689                          (const_int 1)
690                          (and:SI (match_operand:SI 1 "register_operand" "r")
691                                  (const_int 7))))]
692   "TARGET_H8300H || TARGET_H8300S"
693   "btst %w1,%w0"
694   [(set_attr "length" "2")
695    (set_attr "cc" "set_zn")])
696
697 (define_insn_and_split "*tstsi_variable_bit_qi"
698   [(set (cc0)
699         (zero_extract:SI (zero_extend:SI (match_operand:QI 0 "general_operand_src" "r,U,mn>"))
700                          (const_int 1)
701                          (and:SI (match_operand:SI 1 "register_operand" "r,r,r")
702                                  (const_int 7))))
703    (clobber (match_scratch:QI 2 "=X,X,&r"))]
704   "(TARGET_H8300H || TARGET_H8300S)"
705   "@
706    btst\\t%w1,%X0
707    btst\\t%w1,%X0
708    #"
709   "&& reload_completed
710    && !EXTRA_CONSTRAINT (operands[0], 'U')"
711   [(set (match_dup 2)
712         (match_dup 0))
713    (parallel [(set (cc0) (zero_extract:SI (zero_extend:SI (match_dup 2))
714                                           (const_int 1)
715                                           (and:SI (match_dup 1)
716                                                   (const_int 7))))
717               (clobber (scratch:QI))])]
718   ""
719   [(set_attr "length" "2,8,10")
720    (set_attr "cc" "set_zn,set_zn,set_zn")])
721
722 (define_insn "tstqi"
723   [(set (cc0) (match_operand:QI 0 "register_operand" "r"))]
724   ""
725   "mov.b        %X0,%X0"
726   [(set_attr "length" "2")
727    (set_attr "cc" "set_znv")])
728
729 (define_insn "tsthi"
730   [(set (cc0) (match_operand:HI 0 "register_operand" "r"))]
731   ""
732   "mov.w        %T0,%T0"
733   [(set_attr "length" "2")
734    (set_attr "cc" "set_znv")])
735
736 (define_insn "*tsthi_upper"
737   [(set (cc0)
738         (and:HI (match_operand:HI 0 "register_operand" "r")
739                 (const_int -256)))]
740   ""
741   "mov.b        %t0,%t0"
742   [(set_attr "length" "2")
743    (set_attr "cc" "set_znv")])
744
745 (define_insn "tstsi"
746   [(set (cc0) (match_operand:SI 0 "register_operand" "r"))]
747   "TARGET_H8300H || TARGET_H8300S"
748   "mov.l        %S0,%S0"
749   [(set_attr "length" "2")
750    (set_attr "cc" "set_znv")])
751
752 (define_insn "*tstsi_upper"
753   [(set (cc0)
754         (and:SI (match_operand:SI 0 "register_operand" "r")
755                 (const_int -65536)))]
756   ""
757   "mov.w        %e0,%e0"
758   [(set_attr "length" "2")
759    (set_attr "cc" "set_znv")])
760
761 (define_insn "cmpqi"
762   [(set (cc0)
763         (compare (match_operand:QI 0 "register_operand" "r")
764                  (match_operand:QI 1 "nonmemory_operand" "rn")))]
765   ""
766   "cmp.b        %X1,%X0"
767   [(set_attr "length" "2")
768    (set_attr "cc" "compare")])
769
770 (define_expand "cmphi"
771   [(set (cc0)
772         (compare (match_operand:HI 0 "register_operand" "")
773                  (match_operand:HI 1 "nonmemory_operand" "")))]
774   ""
775   "
776 {
777   /* Force operand1 into a register if we're compiling
778      for the H8/300.  */
779   if (GET_CODE (operands[1]) != REG && TARGET_H8300)
780     operands[1] = force_reg (HImode, operands[1]);
781 }")
782
783 (define_insn "*cmphi_h8300"
784   [(set (cc0)
785         (compare (match_operand:HI 0 "register_operand" "r")
786                  (match_operand:HI 1 "register_operand" "r")))]
787   "TARGET_H8300"
788   "cmp.w        %T1,%T0"
789   [(set_attr "length" "2")
790    (set_attr "cc" "compare")])
791
792 (define_insn "*cmphi_h8300hs"
793   [(set (cc0)
794         (compare (match_operand:HI 0 "register_operand" "r,r")
795                  (match_operand:HI 1 "nonmemory_operand" "r,n")))]
796   "TARGET_H8300H || TARGET_H8300S"
797   "cmp.w        %T1,%T0"
798   [(set_attr "length" "2,4")
799    (set_attr "cc" "compare,compare")])
800
801 (define_insn "cmpsi"
802   [(set (cc0)
803         (compare (match_operand:SI 0 "register_operand" "r,r")
804                  (match_operand:SI 1 "nonmemory_operand" "r,i")))]
805   "TARGET_H8300H || TARGET_H8300S"
806   "cmp.l        %S1,%S0"
807   [(set_attr "length" "2,6")
808    (set_attr "cc" "compare,compare")])
809 \f
810 ;; ----------------------------------------------------------------------
811 ;; ADD INSTRUCTIONS
812 ;; ----------------------------------------------------------------------
813
814 (define_insn "addqi3"
815   [(set (match_operand:QI 0 "register_operand" "=r")
816         (plus:QI (match_operand:QI 1 "register_operand" "%0")
817                  (match_operand:QI 2 "nonmemory_operand" "rn")))]
818   ""
819   "add.b        %X2,%X0"
820   [(set_attr "length" "2")
821    (set_attr "cc" "set_zn")])
822
823 (define_expand "addhi3"
824   [(set (match_operand:HI 0 "register_operand" "")
825         (plus:HI (match_operand:HI 1 "register_operand" "")
826                  (match_operand:HI 2 "nonmemory_operand" "")))]
827   ""
828   "")
829
830 (define_insn "*addhi3_h8300"
831   [(set (match_operand:HI 0 "register_operand" "=r,r,r,r,r")
832         (plus:HI (match_operand:HI 1 "register_operand" "%0,0,0,0,0")
833                  (match_operand:HI 2 "nonmemory_operand" "L,N,J,n,r")))]
834   "TARGET_H8300"
835   "@
836    adds %2,%T0
837    subs %G2,%T0
838    add.b        %t2,%t0
839    add.b        %s2,%s0\;addx   %t2,%t0
840    add.w        %T2,%T0"
841   [(set_attr "length" "2,2,2,4,2")
842    (set_attr "cc" "none_0hit,none_0hit,clobber,clobber,set_zn")])
843
844 ;; This splitter is very important to make the stack adjustment
845 ;; interrupt-safe.  The combination of add.b and addx is unsafe!
846 ;;
847 ;; We apply this split after the peephole2 pass so that we won't end
848 ;; up creating too many adds/subs when a scratch register is
849 ;; available, which is actually a common case because stack unrolling
850 ;; tends to happen immediately after a function call.
851
852 (define_split
853   [(set (match_operand:HI 0 "stack_pointer_operand" "")
854         (plus:HI (match_dup 0)
855                  (match_operand 1 "const_int_gt_2_operand" "")))]
856   "TARGET_H8300 && flow2_completed"
857   [(const_int 0)]
858   "split_adds_subs (HImode, operands); DONE;")
859
860 (define_peephole2
861   [(match_scratch:HI 2 "r")
862    (set (match_operand:HI 0 "stack_pointer_operand" "")
863         (plus:HI (match_dup 0)
864                  (match_operand:HI 1 "const_int_ge_8_operand" "")))]
865   "TARGET_H8300"
866   [(set (match_dup 2)
867         (match_dup 1))
868    (set (match_dup 0)
869         (plus:HI (match_dup 0)
870                  (match_dup 2)))]
871   "")
872
873 (define_insn "*addhi3_h8300hs"
874   [(set (match_operand:HI 0 "register_operand" "=r,r,r,r,r")
875         (plus:HI (match_operand:HI 1 "register_operand" "%0,0,0,0,0")
876                  (match_operand:HI 2 "nonmemory_operand" "L,N,J,n,r")))]
877   "TARGET_H8300H || TARGET_H8300S"
878   "@
879    adds %2,%S0
880    subs %G2,%S0
881    add.b        %t2,%t0
882    add.w        %T2,%T0
883    add.w        %T2,%T0"
884   [(set_attr "length" "2,2,2,4,2")
885    (set_attr "cc" "none_0hit,none_0hit,clobber,set_zn,set_zn")])
886
887 (define_insn "*addhi3_incdec"
888   [(set (match_operand:HI 0 "register_operand" "=r,r")
889         (unspec:HI [(match_operand:HI 1 "register_operand" "0,0")
890                     (match_operand:HI 2 "incdec_operand" "M,O")]
891                    UNSPEC_INCDEC))]
892   "TARGET_H8300H || TARGET_H8300S"
893   "@
894    inc.w        %2,%T0
895    dec.w        %G2,%T0"
896   [(set_attr "length" "2,2")
897    (set_attr "cc" "set_zn,set_zn")])
898
899 (define_split
900   [(set (match_operand:HI 0 "register_operand" "")
901         (plus:HI (match_dup 0)
902                  (match_operand:HI 1 "two_insn_adds_subs_operand" "")))]
903   ""
904   [(const_int 0)]
905   "split_adds_subs (HImode, operands); DONE;")
906
907 (define_expand "addsi3"
908   [(set (match_operand:SI 0 "register_operand" "")
909         (plus:SI (match_operand:SI 1 "register_operand" "")
910                  (match_operand:SI 2 "nonmemory_operand" "")))]
911   ""
912   "")
913
914 (define_insn "*addsi_h8300"
915   [(set (match_operand:SI 0 "register_operand" "=r,r")
916         (plus:SI (match_operand:SI 1 "register_operand" "%0,0")
917                  (match_operand:SI 2 "nonmemory_operand" "n,r")))]
918   "TARGET_H8300"
919   "* return output_plussi (operands);"
920   [(set (attr "length")
921         (symbol_ref "compute_plussi_length (operands)"))
922    (set (attr "cc")
923         (symbol_ref "compute_plussi_cc (operands)"))])
924
925 (define_insn "*addsi_h8300hs"
926   [(set (match_operand:SI 0 "register_operand" "=r,r")
927         (plus:SI (match_operand:SI 1 "register_operand" "%0,0")
928                  (match_operand:SI 2 "nonmemory_operand" "i,r")))]
929   "TARGET_H8300H || TARGET_H8300S"
930   "* return output_plussi (operands);"
931   [(set (attr "length")
932         (symbol_ref "compute_plussi_length (operands)"))
933    (set (attr "cc")
934         (symbol_ref "compute_plussi_cc (operands)"))])
935
936 (define_insn "*addsi3_incdec"
937   [(set (match_operand:SI 0 "register_operand" "=r,r")
938         (unspec:SI [(match_operand:SI 1 "register_operand" "0,0")
939                     (match_operand:SI 2 "incdec_operand" "M,O")]
940                    UNSPEC_INCDEC))]
941   "TARGET_H8300H || TARGET_H8300S"
942   "@
943    inc.l        %2,%S0
944    dec.l        %G2,%S0"
945   [(set_attr "length" "2,2")
946    (set_attr "cc" "set_zn,set_zn")])
947
948 (define_split
949   [(set (match_operand:SI 0 "register_operand" "")
950         (plus:SI (match_dup 0)
951                  (match_operand:SI 1 "two_insn_adds_subs_operand" "")))]
952   "TARGET_H8300H || TARGET_H8300S"
953   [(const_int 0)]
954   "split_adds_subs (SImode, operands); DONE;")
955
956 ;; ----------------------------------------------------------------------
957 ;; SUBTRACT INSTRUCTIONS
958 ;; ----------------------------------------------------------------------
959
960 (define_insn "subqi3"
961   [(set (match_operand:QI 0 "register_operand" "=r")
962         (minus:QI (match_operand:QI 1 "register_operand" "0")
963                   (match_operand:QI 2 "register_operand" "r")))]
964   ""
965   "sub.b        %X2,%X0"
966   [(set_attr "length" "2")
967    (set_attr "cc" "set_zn")])
968
969 (define_expand "subhi3"
970   [(set (match_operand:HI 0 "register_operand" "")
971         (minus:HI (match_operand:HI 1 "general_operand" "")
972                   (match_operand:HI 2 "nonmemory_operand" "")))]
973   ""
974   "")
975
976 (define_insn "*subhi3_h8300"
977   [(set (match_operand:HI 0 "register_operand" "=r,r")
978         (minus:HI (match_operand:HI 1 "general_operand" "0,0")
979                   (match_operand:HI 2 "nonmemory_operand" "r,n")))]
980   "TARGET_H8300"
981   "@
982    sub.w        %T2,%T0
983    add.b        %E2,%s0\;addx   %F2,%t0"
984   [(set_attr "length" "2,4")
985    (set_attr "cc" "set_zn,clobber")])
986
987 (define_insn "*subhi3_h8300hs"
988   [(set (match_operand:HI 0 "register_operand" "=r,r")
989         (minus:HI (match_operand:HI 1 "general_operand" "0,0")
990                   (match_operand:HI 2 "nonmemory_operand" "r,n")))]
991   "TARGET_H8300H || TARGET_H8300S"
992   "@
993    sub.w        %T2,%T0
994    sub.w        %T2,%T0"
995   [(set_attr "length" "2,4")
996    (set_attr "cc" "set_zn,set_zn")])
997
998 (define_expand "subsi3"
999   [(set (match_operand:SI 0 "register_operand" "")
1000         (minus:SI (match_operand:SI 1 "register_operand" "")
1001                   (match_operand:SI 2 "nonmemory_operand" "")))]
1002   ""
1003   "")
1004
1005 (define_insn "*subsi3_h8300"
1006   [(set (match_operand:SI 0 "register_operand" "=r")
1007         (minus:SI (match_operand:SI 1 "register_operand" "0")
1008                   (match_operand:SI 2 "register_operand" "r")))]
1009   "TARGET_H8300"
1010   "sub.w        %f2,%f0\;subx   %y2,%y0\;subx   %z2,%z0"
1011   [(set_attr "length" "6")])
1012
1013 (define_insn "*subsi3_h8300hs"
1014   [(set (match_operand:SI 0 "register_operand" "=r,r")
1015         (minus:SI (match_operand:SI 1 "general_operand" "0,0")
1016                   (match_operand:SI 2 "nonmemory_operand" "r,i")))]
1017   "TARGET_H8300H || TARGET_H8300S"
1018   "@
1019    sub.l        %S2,%S0
1020    sub.l        %S2,%S0"
1021   [(set_attr "length" "2,6")
1022    (set_attr "cc" "set_zn,set_zn")])
1023 \f
1024 ;; ----------------------------------------------------------------------
1025 ;; MULTIPLY INSTRUCTIONS
1026 ;; ----------------------------------------------------------------------
1027
1028 ;; Note that the H8/300 can only handle umulqihi3.
1029
1030 (define_insn "mulqihi3"
1031   [(set (match_operand:HI 0 "register_operand" "=r")
1032         (mult:HI (sign_extend:HI (match_operand:QI 1 "register_operand" "%0"))
1033                  (sign_extend:HI (match_operand:QI 2 "register_operand" "r"))))]
1034   "TARGET_H8300H || TARGET_H8300S"
1035   "mulxs.b      %X2,%T0"
1036   [(set_attr "length" "4")
1037    (set_attr "cc" "set_zn")])
1038
1039 (define_insn "mulhisi3"
1040   [(set (match_operand:SI 0 "register_operand" "=r")
1041         (mult:SI (sign_extend:SI (match_operand:HI 1 "register_operand" "%0"))
1042                  (sign_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
1043   "TARGET_H8300H || TARGET_H8300S"
1044   "mulxs.w      %T2,%S0"
1045   [(set_attr "length" "4")
1046    (set_attr "cc" "set_zn")])
1047
1048 (define_insn "umulqihi3"
1049   [(set (match_operand:HI 0 "register_operand" "=r")
1050         (mult:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "%0"))
1051                  (zero_extend:HI (match_operand:QI 2 "register_operand" "r"))))]
1052   ""
1053   "mulxu        %X2,%T0"
1054   [(set_attr "length" "2")
1055    (set_attr "cc" "none_0hit")])
1056
1057 (define_insn "umulhisi3"
1058   [(set (match_operand:SI 0 "register_operand" "=r")
1059         (mult:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "%0"))
1060                  (zero_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
1061   "TARGET_H8300H || TARGET_H8300S"
1062   "mulxu.w      %T2,%S0"
1063   [(set_attr "length" "2")
1064    (set_attr "cc" "none_0hit")])
1065
1066 ;; This is a "bridge" instruction.  Combine can't cram enough insns
1067 ;; together to crate a MAC instruction directly, but it can create
1068 ;; this instruction, which then allows combine to create the real
1069 ;; MAC insn.
1070 ;;
1071 ;; Unfortunately, if combine doesn't create a MAC instruction, this
1072 ;; insn must generate reasonably correct code.  Egad.
1073 (define_insn ""
1074   [(set (match_operand:SI 0 "register_operand" "=a")
1075         (mult:SI
1076           (sign_extend:SI
1077             (mem:HI (post_inc:SI (match_operand:SI 1 "register_operand" "r"))))
1078           (sign_extend:SI
1079             (mem:HI (post_inc:SI (match_operand:SI 2 "register_operand" "r"))))))]
1080   "TARGET_MAC"
1081   "clrmac\;mac  @%2+,@%1+"
1082   [(set_attr "length" "6")
1083    (set_attr "cc" "none_0hit")])
1084
1085 (define_insn ""
1086   [(set (match_operand:SI 0 "register_operand" "=a")
1087         (plus:SI (mult:SI
1088           (sign_extend:SI (mem:HI
1089             (post_inc:SI (match_operand:SI 1 "register_operand" "r"))))
1090           (sign_extend:SI (mem:HI
1091             (post_inc:SI (match_operand:SI 2 "register_operand" "r")))))
1092               (match_operand:SI 3 "register_operand" "0")))]
1093   "TARGET_MAC"
1094   "mac  @%2+,@%1+"
1095   [(set_attr "length" "4")
1096    (set_attr "cc" "none_0hit")])
1097
1098 ;; ----------------------------------------------------------------------
1099 ;; DIVIDE/MOD INSTRUCTIONS
1100 ;; ----------------------------------------------------------------------
1101
1102 (define_insn "udivmodqi4"
1103   [(set (match_operand:QI 0 "register_operand" "=r")
1104         (truncate:QI
1105           (udiv:HI
1106             (match_operand:HI 1 "register_operand" "0")
1107             (zero_extend:HI (match_operand:QI 2 "register_operand" "r")))))
1108    (set (match_operand:QI 3 "register_operand" "=r")
1109         (truncate:QI
1110           (umod:HI
1111             (match_dup 1)
1112             (zero_extend:HI (match_dup 2)))))]
1113   ""
1114   "*
1115 {
1116   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1117     return \"divxu.b\\t%X2,%T0\";
1118   else
1119     return \"divxu.b\\t%X2,%T0\;mov.b\\t%t0,%s3\";
1120 }"
1121   [(set_attr "length" "4")])
1122
1123 (define_insn "divmodqi4"
1124   [(set (match_operand:QI 0 "register_operand" "=r")
1125         (truncate:QI
1126           (div:HI
1127             (match_operand:HI 1 "register_operand" "0")
1128             (sign_extend:HI (match_operand:QI 2 "register_operand" "r")))))
1129    (set (match_operand:QI 3 "register_operand" "=r")
1130         (truncate:QI
1131           (mod:HI
1132             (match_dup 1)
1133             (sign_extend:HI (match_dup 2)))))]
1134   "TARGET_H8300H || TARGET_H8300S"
1135   "*
1136 {
1137   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1138     return \"divxs.b\\t%X2,%T0\";
1139   else
1140     return \"divxs.b\\t%X2,%T0\;mov.b\\t%t0,%s3\";
1141 }"
1142   [(set_attr "length" "6")])
1143
1144 (define_insn "udivmodhi4"
1145   [(set (match_operand:HI 0 "register_operand" "=r")
1146         (truncate:HI
1147           (udiv:SI
1148             (match_operand:SI 1 "register_operand" "0")
1149             (zero_extend:SI (match_operand:HI 2 "register_operand" "r")))))
1150    (set (match_operand:HI 3 "register_operand" "=r")
1151         (truncate:HI
1152           (umod:SI
1153             (match_dup 1)
1154             (zero_extend:SI (match_dup 2)))))]
1155   "TARGET_H8300H || TARGET_H8300S"
1156   "*
1157 {
1158   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1159     return \"divxu.w\\t%T2,%S0\";
1160   else
1161     return \"divxu.w\\t%T2,%S0\;mov.w\\t%e0,%f3\";
1162 }"
1163   [(set_attr "length" "4")])
1164
1165 (define_insn "divmodhi4"
1166   [(set (match_operand:HI 0 "register_operand" "=r")
1167         (truncate:HI
1168           (div:SI
1169             (match_operand:SI 1 "register_operand" "0")
1170             (sign_extend:SI (match_operand:HI 2 "register_operand" "r")))))
1171    (set (match_operand:HI 3 "register_operand" "=r")
1172         (truncate:HI
1173           (mod:SI
1174             (match_dup 1)
1175             (sign_extend:SI (match_dup 2)))))]
1176   "TARGET_H8300H || TARGET_H8300S"
1177   "*
1178 {
1179   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1180     return \"divxs.w\\t%T2,%S0\";
1181   else
1182     return \"divxs.w\\t%T2,%S0\;mov.w\\t%e0,%f3\";
1183 }"
1184   [(set_attr "length" "6")])
1185 \f
1186 ;; ----------------------------------------------------------------------
1187 ;; AND INSTRUCTIONS
1188 ;; ----------------------------------------------------------------------
1189
1190 (define_insn "andqi3_1"
1191   [(set (match_operand:QI 0 "bit_operand" "=r,U")
1192         (and:QI (match_operand:QI 1 "bit_operand" "%0,0")
1193                 (match_operand:QI 2 "nonmemory_operand" "rn,n")))]
1194   "register_operand (operands[0], QImode)
1195    || single_zero_operand (operands[2], QImode)"
1196   "@
1197    and  %X2,%X0
1198    bclr %W2,%R0"
1199   [(set_attr "length" "2,8")
1200    (set_attr "cc" "set_znv,none_0hit")])
1201
1202 (define_expand "andqi3"
1203   [(set (match_operand:QI 0 "bit_operand" "")
1204         (and:QI (match_operand:QI 1 "bit_operand" "")
1205                 (match_operand:QI 2 "nonmemory_operand" "")))]
1206   ""
1207   "
1208 {
1209   if (fix_bit_operand (operands, AND))
1210     DONE;
1211 }")
1212
1213 (define_expand "andhi3"
1214   [(set (match_operand:HI 0 "register_operand" "")
1215         (and:HI (match_operand:HI 1 "register_operand" "")
1216                 (match_operand:HI 2 "nonmemory_operand" "")))]
1217   ""
1218   "")
1219
1220 (define_insn "*andorqi3"
1221   [(set (match_operand:QI 0 "register_operand" "=r")
1222         (ior:QI (and:QI (match_operand:QI 2 "register_operand" "r")
1223                         (match_operand:QI 3 "single_one_operand" "n"))
1224                 (match_operand:QI 1 "register_operand" "0")))]
1225   ""
1226   "bld\\t%V3,%X2\;bor\\t%V3,%X0\;bst\\t%V3,%X0"
1227   [(set_attr "length" "6")])
1228
1229 (define_insn "*andorhi3"
1230   [(set (match_operand:HI 0 "register_operand" "=r")
1231         (ior:HI (and:HI (match_operand:HI 2 "register_operand" "r")
1232                         (match_operand:HI 3 "single_one_operand" "n"))
1233                 (match_operand:HI 1 "register_operand" "0")))]
1234   ""
1235   "*
1236 {
1237   operands[3] = GEN_INT (INTVAL (operands[3]) & 0xffff);
1238   if (INTVAL (operands[3]) > 128)
1239     {
1240       operands[3] = GEN_INT (INTVAL (operands[3]) >> 8);
1241       return \"bld\\t%V3,%t2\;bor\\t%V3,%t0\;bst\\t%V3,%t0\";
1242     }
1243   return \"bld\\t%V3,%s2\;bor\\t%V3,%s0\;bst\\t%V3,%s0\";
1244 }"
1245   [(set_attr "length" "6")])
1246
1247 (define_insn "*andorsi3"
1248   [(set (match_operand:SI 0 "register_operand" "=r")
1249         (ior:SI (and:SI (match_operand:SI 2 "register_operand" "r")
1250                         (match_operand:SI 3 "single_one_operand" "n"))
1251                 (match_operand:SI 1 "register_operand" "0")))]
1252   "(INTVAL (operands[3]) & 0xffff) != 0"
1253   "*
1254 {
1255   operands[3] = GEN_INT (INTVAL (operands[3]) & 0xffff);
1256   if (INTVAL (operands[3]) > 128)
1257     {
1258       operands[3] = GEN_INT (INTVAL (operands[3]) >> 8);
1259       return \"bld\\t%V3,%x2\;bor\\t%V3,%x0\;bst\\t%V3,%x0\";
1260     }
1261   return \"bld\\t%V3,%w2\;bor\\t%V3,%w0\;bst\\t%V3,%w0\";
1262 }"
1263   [(set_attr "length" "6")])
1264
1265 (define_insn "*andorsi3_shift_8"
1266   [(set (match_operand:SI 0 "register_operand" "=r")
1267         (ior:SI (and:SI (ashift:SI (match_operand:SI 2 "register_operand" "r")
1268                                    (const_int 8))
1269                         (const_int 65280))
1270                 (match_operand:SI 1 "register_operand" "0")))]
1271   ""
1272   "or.b\\t%w2,%x0"
1273   [(set_attr "length" "2")])
1274
1275 (define_expand "andsi3"
1276   [(set (match_operand:SI 0 "register_operand" "")
1277         (and:SI (match_operand:SI 1 "register_operand" "")
1278                 (match_operand:SI 2 "nonmemory_operand" "")))]
1279   ""
1280   "")
1281
1282 ;; ----------------------------------------------------------------------
1283 ;; OR INSTRUCTIONS
1284 ;; ----------------------------------------------------------------------
1285
1286 (define_insn "iorqi3_1"
1287   [(set (match_operand:QI 0 "bit_operand" "=r,U")
1288         (ior:QI (match_operand:QI 1 "bit_operand" "%0,0")
1289                 (match_operand:QI 2 "nonmemory_operand" "rn,n")))]
1290   "register_operand (operands[0], QImode)
1291    || single_one_operand (operands[2], QImode)"
1292   "@
1293    or\\t%X2,%X0
1294    bset\\t%V2,%R0"
1295   [(set_attr "length" "2,8")
1296    (set_attr "cc" "set_znv,none_0hit")])
1297
1298 (define_expand "iorqi3"
1299   [(set (match_operand:QI 0 "bit_operand" "")
1300         (ior:QI (match_operand:QI 1 "bit_operand" "")
1301                 (match_operand:QI 2 "nonmemory_operand" "")))]
1302   ""
1303   "
1304 {
1305   if (fix_bit_operand (operands, IOR))
1306     DONE;
1307 }")
1308
1309 (define_expand "iorhi3"
1310   [(set (match_operand:HI 0 "register_operand" "")
1311         (ior:HI (match_operand:HI 1 "register_operand" "")
1312                 (match_operand:HI 2 "nonmemory_operand" "")))]
1313   ""
1314   "")
1315
1316 (define_expand "iorsi3"
1317   [(set (match_operand:SI 0 "register_operand" "")
1318         (ior:SI (match_operand:SI 1 "register_operand" "")
1319                 (match_operand:SI 2 "nonmemory_operand" "")))]
1320   ""
1321   "")
1322
1323 ;; ----------------------------------------------------------------------
1324 ;; XOR INSTRUCTIONS
1325 ;; ----------------------------------------------------------------------
1326
1327 (define_insn "xorqi3_1"
1328   [(set (match_operand:QI 0 "bit_operand" "=r,U")
1329         (xor:QI (match_operand:QI 1 "bit_operand" "%0,0")
1330                 (match_operand:QI 2 "nonmemory_operand" "rn,n")))]
1331   "register_operand (operands[0], QImode)
1332    || single_one_operand (operands[2], QImode)"
1333   "@
1334    xor\\t%X2,%X0
1335    bnot\\t%V2,%R0"
1336   [(set_attr "length" "2,8")
1337    (set_attr "cc" "set_znv,none_0hit")])
1338
1339 (define_expand "xorqi3"
1340   [(set (match_operand:QI 0 "bit_operand" "")
1341         (xor:QI (match_operand:QI 1 "bit_operand" "")
1342                 (match_operand:QI 2 "nonmemory_operand" "")))]
1343   ""
1344   "
1345 {
1346   if (fix_bit_operand (operands, XOR))
1347     DONE;
1348 }")
1349
1350 (define_expand "xorhi3"
1351   [(set (match_operand:HI 0 "register_operand" "")
1352         (xor:HI (match_operand:HI 1 "register_operand" "")
1353                 (match_operand:HI 2 "nonmemory_operand" "")))]
1354   ""
1355   "")
1356
1357 (define_expand "xorsi3"
1358   [(set (match_operand:SI 0 "register_operand" "")
1359         (xor:SI (match_operand:SI 1 "register_operand" "")
1360                 (match_operand:SI 2 "nonmemory_operand" "")))]
1361   ""
1362   "")
1363
1364 ;; ----------------------------------------------------------------------
1365 ;; {AND,IOR,XOR}{HI3,SI3} PATTERNS
1366 ;; ----------------------------------------------------------------------
1367
1368 (define_insn "*logicalhi3"
1369   [(set (match_operand:HI 0 "register_operand" "=r")
1370         (match_operator:HI 3 "bit_operator"
1371           [(match_operand:HI 1 "register_operand" "%0")
1372            (match_operand:HI 2 "nonmemory_operand" "rn")]))]
1373   ""
1374   "* return output_logical_op (HImode, operands);"
1375   [(set (attr "length")
1376         (symbol_ref "compute_logical_op_length (HImode, operands)"))
1377    (set (attr "cc")
1378         (symbol_ref "compute_logical_op_cc (HImode, operands)"))])
1379
1380 (define_insn "*logicalsi3"
1381   [(set (match_operand:SI 0 "register_operand" "=r")
1382         (match_operator:SI 3 "bit_operator"
1383           [(match_operand:SI 1 "register_operand" "%0")
1384            (match_operand:SI 2 "nonmemory_operand" "rn")]))]
1385   ""
1386   "* return output_logical_op (SImode, operands);"
1387   [(set (attr "length")
1388         (symbol_ref "compute_logical_op_length (SImode, operands)"))
1389    (set (attr "cc")
1390         (symbol_ref "compute_logical_op_cc (SImode, operands)"))])
1391 \f
1392 ;; ----------------------------------------------------------------------
1393 ;; NEGATION INSTRUCTIONS
1394 ;; ----------------------------------------------------------------------
1395
1396 (define_insn "negqi2"
1397   [(set (match_operand:QI 0 "register_operand" "=r")
1398         (neg:QI (match_operand:QI 1 "register_operand" "0")))]
1399   ""
1400   "neg  %X0"
1401   [(set_attr "length" "2")
1402    (set_attr "cc" "set_zn")])
1403
1404 (define_expand "neghi2"
1405   [(set (match_operand:HI 0 "register_operand" "")
1406         (neg:HI (match_operand:HI 1 "register_operand" "")))]
1407   ""
1408   "
1409 {
1410   if (TARGET_H8300)
1411     {
1412       emit_insn (gen_neghi2_h8300 (operands[0], operands[1]));
1413       DONE;
1414     }
1415 }")
1416
1417 (define_expand "neghi2_h8300"
1418   [(set (match_dup 2)
1419         (not:HI (match_operand:HI 1 "register_operand" "")))
1420    (set (match_dup 2) (plus:HI (match_dup 2) (const_int 1)))
1421    (set (match_operand:HI 0 "register_operand" "")
1422         (match_dup 2))]
1423   ""
1424   "operands[2] = gen_reg_rtx (HImode);")
1425
1426 (define_insn "*neghi2_h8300hs"
1427   [(set (match_operand:HI 0 "register_operand" "=r")
1428         (neg:HI (match_operand:HI 1 "register_operand" "0")))]
1429   "TARGET_H8300H || TARGET_H8300S"
1430   "neg  %T0"
1431   [(set_attr "length" "2")
1432    (set_attr "cc" "set_zn")])
1433
1434 (define_expand "negsi2"
1435   [(set (match_operand:SI 0 "register_operand" "")
1436         (neg:SI (match_operand:SI 1 "register_operand" "")))]
1437   ""
1438   "
1439 {
1440   if (TARGET_H8300)
1441     {
1442       emit_insn (gen_negsi2_h8300 (operands[0], operands[1]));
1443       DONE;
1444     }
1445 }")
1446
1447 (define_expand "negsi2_h8300"
1448   [(set (match_dup 2)
1449         (not:SI (match_operand:SI 1 "register_operand" "")))
1450    (set (match_dup 2) (plus:SI (match_dup 2) (const_int 1)))
1451    (set (match_operand:SI 0 "register_operand" "")
1452         (match_dup 2))]
1453   ""
1454   "operands[2] = gen_reg_rtx (SImode);")
1455
1456 (define_insn "*negsi2_h8300hs"
1457   [(set (match_operand:SI 0 "register_operand" "=r")
1458         (neg:SI (match_operand:SI 1 "register_operand" "0")))]
1459   "TARGET_H8300H || TARGET_H8300S"
1460   "neg  %S0"
1461   [(set_attr "length" "2")
1462    (set_attr "cc" "set_zn")])
1463
1464 (define_expand "negsf2"
1465   [(set (match_operand:SF 0 "register_operand" "")
1466         (neg:SF (match_operand:SF 1 "register_operand" "")))]
1467   ""
1468   "")
1469
1470 (define_insn "*negsf2_h8300"
1471   [(set (match_operand:SF 0 "register_operand" "=r")
1472         (neg:SF (match_operand:SF 1 "register_operand" "0")))]
1473   "TARGET_H8300"
1474   "xor.b\\t#128,%z0"
1475   [(set_attr "length" "2")])
1476
1477 (define_insn "*negsf2_h8300hs"
1478   [(set (match_operand:SF 0 "register_operand" "=r")
1479         (neg:SF (match_operand:SF 1 "register_operand" "0")))]
1480   "TARGET_H8300H || TARGET_H8300S"
1481   "xor.w\\t#32768,%e0"
1482   [(set_attr "length" "4")])
1483 \f
1484 ;; ----------------------------------------------------------------------
1485 ;; ABSOLUTE VALUE INSTRUCTIONS
1486 ;; ----------------------------------------------------------------------
1487
1488 (define_expand "abssf2"
1489   [(set (match_operand:SF 0 "register_operand" "")
1490         (abs:SF (match_operand:SF 1 "register_operand" "")))]
1491   ""
1492   "")
1493
1494 (define_insn "*abssf2_h8300"
1495   [(set (match_operand:SF 0 "register_operand" "=r")
1496         (abs:SF (match_operand:SF 1 "register_operand" "0")))]
1497   "TARGET_H8300"
1498   "and.b\\t#127,%z0"
1499   [(set_attr "length" "2")])
1500
1501 (define_insn "*abssf2_h8300hs"
1502   [(set (match_operand:SF 0 "register_operand" "=r")
1503         (abs:SF (match_operand:SF 1 "register_operand" "0")))]
1504   "TARGET_H8300H || TARGET_H8300S"
1505   "and.w\\t#32767,%e0"
1506   [(set_attr "length" "4")])
1507 \f
1508 ;; ----------------------------------------------------------------------
1509 ;; NOT INSTRUCTIONS
1510 ;; ----------------------------------------------------------------------
1511
1512 (define_insn "one_cmplqi2"
1513   [(set (match_operand:QI 0 "register_operand" "=r")
1514         (not:QI (match_operand:QI 1 "register_operand" "0")))]
1515   ""
1516   "not  %X0"
1517   [(set_attr "length" "2")
1518    (set_attr "cc" "set_znv")])
1519
1520 (define_expand "one_cmplhi2"
1521   [(set (match_operand:HI 0 "register_operand" "=r")
1522         (not:HI (match_operand:HI 1 "register_operand" "0")))]
1523   ""
1524   "")
1525
1526 (define_insn "*one_cmplhi2_h8300"
1527   [(set (match_operand:HI 0 "register_operand" "=r")
1528         (not:HI (match_operand:HI 1 "register_operand" "0")))]
1529   "TARGET_H8300"
1530   "not  %s0\;not        %t0"
1531   [(set_attr "length" "4")])
1532
1533 (define_insn "*one_cmplhi2_h8300hs"
1534   [(set (match_operand:HI 0 "register_operand" "=r")
1535         (not:HI (match_operand:HI 1 "register_operand" "0")))]
1536   "TARGET_H8300H || TARGET_H8300S"
1537   "not  %T0"
1538   [(set_attr "cc" "set_znv")
1539    (set_attr "length" "2")])
1540
1541 (define_expand "one_cmplsi2"
1542   [(set (match_operand:SI 0 "register_operand" "=r")
1543         (not:SI (match_operand:SI 1 "register_operand" "0")))]
1544   ""
1545   "")
1546
1547 (define_insn "*one_cmplsi2_h8300"
1548   [(set (match_operand:SI 0 "register_operand" "=r")
1549         (not:SI (match_operand:SI 1 "register_operand" "0")))]
1550   "TARGET_H8300"
1551   "not  %w0\;not        %x0\;not        %y0\;not        %z0"
1552   [(set_attr "length" "8")])
1553
1554 (define_insn "*one_cmplsi2_h8300hs"
1555   [(set (match_operand:SI 0 "register_operand" "=r")
1556         (not:SI (match_operand:SI 1 "register_operand" "0")))]
1557   "TARGET_H8300H || TARGET_H8300S"
1558   "not  %S0"
1559   [(set_attr "cc" "set_znv")
1560    (set_attr "length" "2")])
1561 \f
1562 ;; ----------------------------------------------------------------------
1563 ;; JUMP INSTRUCTIONS
1564 ;; ----------------------------------------------------------------------
1565
1566 ;; Conditional jump instructions
1567
1568 (define_expand "ble"
1569   [(match_operand 0 "" "")]
1570   ""
1571   "h8300_expand_branch (LE, operands[0]); DONE;")
1572
1573 (define_expand "bleu"
1574   [(match_operand 0 "" "")]
1575   ""
1576   "h8300_expand_branch (LEU, operands[0]); DONE;")
1577
1578 (define_expand "bge"
1579   [(match_operand 0 "" "")]
1580   ""
1581   "h8300_expand_branch (GE, operands[0]); DONE;")
1582
1583 (define_expand "bgeu"
1584   [(match_operand 0 "" "")]
1585   ""
1586   "h8300_expand_branch (GEU, operands[0]); DONE;")
1587
1588 (define_expand "blt"
1589   [(match_operand 0 "" "")]
1590   ""
1591   "h8300_expand_branch (LT, operands[0]); DONE;")
1592
1593 (define_expand "bltu"
1594   [(match_operand 0 "" "")]
1595   ""
1596   "h8300_expand_branch (LTU, operands[0]); DONE;")
1597
1598 (define_expand "bgt"
1599   [(match_operand 0 "" "")]
1600   ""
1601   "h8300_expand_branch (GT, operands[0]); DONE;")
1602
1603 (define_expand "bgtu"
1604   [(match_operand 0 "" "")]
1605   ""
1606   "h8300_expand_branch (GTU, operands[0]); DONE;")
1607
1608 (define_expand "beq"
1609   [(match_operand 0 "" "")]
1610   ""
1611   "h8300_expand_branch (EQ, operands[0]); DONE;")
1612
1613 (define_expand "bne"
1614   [(match_operand 0 "" "")]
1615   ""
1616   "h8300_expand_branch (NE, operands[0]); DONE;")
1617
1618 (define_insn "branch_true"
1619   [(set (pc)
1620         (if_then_else (match_operator 1 "comparison_operator"
1621                                       [(cc0) (const_int 0)])
1622                       (label_ref (match_operand 0 "" ""))
1623                       (pc)))]
1624   ""
1625   "*
1626 {
1627   if ((cc_status.flags & CC_OVERFLOW_UNUSABLE) != 0
1628       && (GET_CODE (operands[1]) == GT
1629           || GET_CODE (operands[1]) == GE
1630           || GET_CODE (operands[1]) == LE
1631           || GET_CODE (operands[1]) == LT))
1632     {
1633       cc_status.flags &= ~CC_OVERFLOW_UNUSABLE;
1634       return 0;
1635     }
1636
1637   if (get_attr_length (insn) == 2)
1638     return \"b%j1       %l0\";
1639   else if (get_attr_length (insn) == 4)
1640     return \"b%j1       %l0:16\";
1641   else
1642     return \"b%k1       .Lh8BR%=\;jmp   @%l0\\n.Lh8BR%=:\";
1643 }"
1644  [(set_attr "type" "branch")
1645    (set_attr "cc" "none")])
1646
1647 (define_insn "branch_false"
1648   [(set (pc)
1649         (if_then_else (match_operator 1 "comparison_operator"
1650                                       [(cc0) (const_int 0)])
1651                       (pc)
1652                       (label_ref (match_operand 0 "" ""))))]
1653   ""
1654   "*
1655 {
1656   if ((cc_status.flags & CC_OVERFLOW_UNUSABLE) != 0
1657       && (GET_CODE (operands[1]) == GT
1658           || GET_CODE (operands[1]) == GE
1659           || GET_CODE (operands[1]) == LE
1660           || GET_CODE (operands[1]) == LT))
1661     {
1662       cc_status.flags &= ~CC_OVERFLOW_UNUSABLE;
1663       return 0;
1664     }
1665
1666   if (get_attr_length (insn) == 2)
1667     return \"b%k1       %l0\";
1668   else if (get_attr_length (insn) == 4)
1669     return \"b%k1       %l0:16\";
1670   else
1671     return \"b%j1       .Lh8BR%=\;jmp   @%l0\\n.Lh8BR%=:\";
1672 }"
1673   [(set_attr "type" "branch")
1674    (set_attr "cc" "none")])
1675
1676 ;; Unconditional and other jump instructions.
1677
1678 (define_insn "jump"
1679   [(set (pc)
1680         (label_ref (match_operand 0 "" "")))]
1681   ""
1682   "*
1683 {
1684   if (get_attr_length (insn) == 2)
1685     return \"bra        %l0\";
1686   else if (get_attr_length (insn) == 4)
1687     return \"bra        %l0:16\";
1688   else
1689     return \"jmp        @%l0\";
1690 }"
1691   [(set_attr "type" "branch")
1692    (set_attr "cc" "none")])
1693
1694 ;; This is a define expand, because pointers may be either 16 or 32 bits.
1695
1696 (define_expand "tablejump"
1697   [(parallel [(set (pc) (match_operand 0 "register_operand" ""))
1698               (use (label_ref (match_operand 1 "" "")))])]
1699   ""
1700   "")
1701
1702 (define_insn "*tablejump_h8300"
1703   [(set (pc) (match_operand:HI 0 "register_operand" "r"))
1704    (use (label_ref (match_operand 1 "" "")))]
1705   "TARGET_H8300"
1706   "jmp  @%0"
1707   [(set_attr "cc" "none")
1708    (set_attr "length" "2")])
1709
1710 (define_insn "*tablejump_h8300hs_advanced"
1711   [(set (pc) (match_operand:SI 0 "register_operand" "r"))
1712    (use (label_ref (match_operand 1 "" "")))]
1713   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_NORMAL_MODE"
1714   "jmp  @%0"
1715   [(set_attr "cc" "none")
1716    (set_attr "length" "2")])
1717
1718 (define_insn "*tablejump_h8300hs_normal"
1719   [(set (pc) (match_operand:HI 0 "register_operand" "r"))
1720    (use (label_ref (match_operand 1 "" "")))]
1721   "(TARGET_H8300H || TARGET_H8300S) && TARGET_NORMAL_MODE"
1722   "jmp @%S0"
1723   [(set_attr "cc" "none")
1724    (set_attr "length" "2")])
1725
1726 ;; This is a define expand, because pointers may be either 16 or 32 bits.
1727
1728 (define_expand "indirect_jump"
1729   [(set (pc) (match_operand 0 "jump_address_operand" ""))]
1730   ""
1731   "")
1732
1733 (define_insn "*indirect_jump_h8300"
1734   [(set (pc) (match_operand:HI 0 "jump_address_operand" "Vr"))]
1735   "TARGET_H8300"
1736   "jmp  @%0"
1737   [(set_attr "cc" "none")
1738    (set_attr "length" "2")])
1739
1740 (define_insn "*indirect_jump_h8300hs_advanced"
1741   [(set (pc) (match_operand:SI 0 "jump_address_operand" "Vr"))]
1742   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_NORMAL_MODE"
1743   "jmp @%0"
1744   [(set_attr "cc" "none")
1745    (set_attr "length" "2")])
1746
1747 (define_insn "*indirect_jump_h8300hs_normal"
1748   [(set (pc) (match_operand:HI 0 "jump_address_operand" "Vr"))]
1749   "(TARGET_H8300H || TARGET_H8300S) && TARGET_NORMAL_MODE"
1750   "jmp @%S0"
1751   [(set_attr "cc" "none")
1752    (set_attr "length" "2")])
1753
1754 ;; Call subroutine with no return value.
1755
1756 ;; ??? Even though we use HImode here, this works on the H8/300H and H8S.
1757
1758 (define_insn "call"
1759   [(call (match_operand:QI 0 "call_insn_operand" "or")
1760          (match_operand:HI 1 "general_operand" "g"))]
1761   ""
1762   "*
1763 {
1764   if (GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF
1765       && SYMBOL_REF_FLAG (XEXP (operands[0], 0)))
1766     return \"jsr\\t@%0:8\";
1767   else
1768     return \"jsr\\t%0\";
1769 }"
1770   [(set (attr "length")
1771         (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
1772                       (const_int 2)
1773                       (const_int 4)))])
1774
1775 ;; Call subroutine, returning value in operand 0
1776 ;; (which must be a hard register).
1777
1778 ;; ??? Even though we use HImode here, this works on the H8/300H and H8S.
1779
1780 (define_insn "call_value"
1781   [(set (match_operand 0 "" "=r")
1782         (call (match_operand:QI 1 "call_insn_operand" "or")
1783               (match_operand:HI 2 "general_operand" "g")))]
1784   ""
1785   "*
1786 {
1787   if (GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
1788       && SYMBOL_REF_FLAG (XEXP (operands[1], 0)))
1789     return \"jsr\\t@%1:8\";
1790   else
1791     return \"jsr\\t%1\";
1792 }"
1793   [(set (attr "length")
1794         (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
1795                       (const_int 2)
1796                       (const_int 4)))])
1797
1798 (define_insn "nop"
1799   [(const_int 0)]
1800   ""
1801   "nop"
1802   [(set_attr "cc" "none")
1803    (set_attr "length" "2")])
1804 \f
1805 ;; ----------------------------------------------------------------------
1806 ;; PROLOGUE/EPILOGUE-RELATED INSTRUCTIONS
1807 ;; ----------------------------------------------------------------------
1808
1809 (define_expand "push_h8300"
1810   [(set (mem:HI (pre_dec:HI (reg:HI SP_REG)))
1811         (match_operand:HI 0 "register_operand" ""))]
1812   "TARGET_H8300"
1813   "")
1814
1815 (define_expand "push_h8300hs_advanced"
1816   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
1817         (match_operand:SI 0 "register_operand" ""))]
1818   "TARGET_H8300H && TARGET_H8300S && !TARGET_NORMAL_MODE"
1819   "")
1820
1821 (define_expand "push_h8300hs_normal"
1822   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
1823         (match_operand:SI 0 "register_operand" ""))]
1824   "TARGET_H8300H && TARGET_H8300S && TARGET_NORMAL_MODE"
1825   "")
1826
1827 (define_expand "pop_h8300"
1828   [(set (match_operand:HI 0 "register_operand" "")
1829         (mem:HI (post_inc:HI (reg:HI SP_REG))))]
1830   "TARGET_H8300"
1831   "")
1832
1833 (define_expand "pop_h8300hs_advanced"
1834   [(set (match_operand:SI 0 "register_operand" "")
1835         (mem:SI (post_inc:SI (reg:SI SP_REG))))]
1836   "TARGET_H8300H && TARGET_H8300S && !TARGET_NORMAL_MODE"
1837   "")
1838
1839 (define_expand "pop_h8300hs_normal"
1840   [(set (match_operand:SI 0 "register_operand" "")
1841         (mem:SI (post_inc:HI (reg:HI SP_REG))))]
1842   "TARGET_H8300H && TARGET_H8300S && TARGET_NORMAL_MODE"
1843   "")
1844
1845 (define_insn "stm_h8300s_2_advanced"
1846   [(set (reg:SI SP_REG)
1847         (plus:SI (reg:SI SP_REG) (const_int -8)))
1848    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -4)))
1849         (match_operand:SI 0 "register_operand" ""))
1850    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -8)))
1851         (match_operand:SI 1 "register_operand" ""))]
1852   "TARGET_H8300S && !TARGET_NORMAL_MODE
1853    && h8300_regs_ok_for_stm (2, operands)"
1854   "stm.l\\t%S0-%S1,@-er7"
1855   [(set_attr "cc" "none")
1856    (set_attr "length" "4")])
1857
1858 (define_insn "stm_h8300s_2_normal"
1859   [(set (reg:HI SP_REG)
1860         (plus:HI (reg:HI SP_REG) (const_int -8)))
1861    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -4)))
1862         (match_operand:SI 0 "register_operand" ""))
1863    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -8)))
1864         (match_operand:SI 1 "register_operand" ""))]
1865   "TARGET_H8300S && TARGET_NORMAL_MODE
1866    && h8300_regs_ok_for_stm (2, operands)"
1867   "stm.l\\t%S0-%S1,@-er7"
1868   [(set_attr "cc" "none")
1869    (set_attr "length" "4")])
1870
1871 (define_expand "stm_h8300s_2"
1872   [(match_operand:SI 0 "register_operand" "")
1873    (match_operand:SI 1 "register_operand" "")]
1874   "TARGET_H8300S
1875    && h8300_regs_ok_for_stm (2, operands)"
1876   "
1877 {
1878   if (!TARGET_NORMAL_MODE)
1879     emit_insn (gen_stm_h8300s_2_advanced (operands[0], operands[1]));
1880   else
1881     emit_insn (gen_stm_h8300s_2_normal (operands[0], operands[1]));
1882   DONE;
1883 }")
1884
1885 (define_insn "stm_h8300s_3_advanced"
1886   [(set (reg:SI SP_REG)
1887         (plus:SI (reg:SI SP_REG) (const_int -12)))
1888    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -4)))
1889         (match_operand:SI 0 "register_operand" ""))
1890    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -8)))
1891         (match_operand:SI 1 "register_operand" ""))
1892    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -12)))
1893         (match_operand:SI 2 "register_operand" ""))]
1894   "TARGET_H8300S && !TARGET_NORMAL_MODE
1895    && h8300_regs_ok_for_stm (3, operands)"
1896   "stm.l\\t%S0-%S2,@-er7"
1897   [(set_attr "cc" "none")
1898    (set_attr "length" "4")])
1899
1900 (define_insn "stm_h8300s_3_normal"
1901   [(set (reg:HI SP_REG)
1902         (plus:HI (reg:HI SP_REG) (const_int -12)))
1903    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -4)))
1904         (match_operand:SI 0 "register_operand" ""))
1905    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -8)))
1906         (match_operand:SI 1 "register_operand" ""))
1907    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -12)))
1908         (match_operand:SI 2 "register_operand" ""))]
1909   "TARGET_H8300S && TARGET_NORMAL_MODE
1910    && h8300_regs_ok_for_stm (3, operands)"
1911   "stm.l\\t%S0-%S2,@-er7"
1912   [(set_attr "cc" "none")
1913    (set_attr "length" "4")])
1914
1915 (define_expand "stm_h8300s_3"
1916   [(match_operand:SI 0 "register_operand" "")
1917    (match_operand:SI 1 "register_operand" "")
1918    (match_operand:SI 2 "register_operand" "")]
1919   "TARGET_H8300S
1920    && h8300_regs_ok_for_stm (3, operands)"
1921   "
1922 {
1923   if (!TARGET_NORMAL_MODE)
1924     emit_insn (gen_stm_h8300s_3_advanced (operands[0], operands[1],
1925                                           operands[2]));
1926   else
1927     emit_insn (gen_stm_h8300s_3_normal (operands[0], operands[1],
1928                                         operands[2]));
1929   DONE;
1930 }")
1931
1932 (define_insn "stm_h8300s_4_advanced"
1933   [(set (reg:SI SP_REG)
1934         (plus:SI (reg:SI SP_REG) (const_int -16)))
1935    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -4)))
1936         (match_operand:SI 0 "register_operand" ""))
1937    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -8)))
1938         (match_operand:SI 1 "register_operand" ""))
1939    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -12)))
1940         (match_operand:SI 2 "register_operand" ""))
1941    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -16)))
1942         (match_operand:SI 3 "register_operand" ""))]
1943   "TARGET_H8300S && !TARGET_NORMAL_MODE
1944    && h8300_regs_ok_for_stm (4, operands)"
1945   "stm.l\\t%S0-%S3,@-er7"
1946   [(set_attr "cc" "none")
1947    (set_attr "length" "4")])
1948
1949 (define_insn "stm_h8300s_4_normal"
1950   [(set (reg:HI SP_REG)
1951         (plus:HI (reg:HI SP_REG) (const_int -16)))
1952    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -4)))
1953         (match_operand:SI 0 "register_operand" ""))
1954    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -8)))
1955         (match_operand:SI 1 "register_operand" ""))
1956    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -12)))
1957         (match_operand:SI 2 "register_operand" ""))
1958    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -16)))
1959         (match_operand:SI 3 "register_operand" ""))]
1960   "TARGET_H8300S && TARGET_NORMAL_MODE
1961    && h8300_regs_ok_for_stm (4, operands)"
1962   "stm.l\\t%S0-%S3,@-er7"
1963   [(set_attr "cc" "none")
1964    (set_attr "length" "4")])
1965
1966 (define_expand "stm_h8300s_4"
1967   [(match_operand:SI 0 "register_operand" "")
1968    (match_operand:SI 1 "register_operand" "")
1969    (match_operand:SI 2 "register_operand" "")
1970    (match_operand:SI 3 "register_operand" "")]
1971   "TARGET_H8300S
1972    && h8300_regs_ok_for_stm (4, operands)"
1973   "
1974 {
1975   if (!TARGET_NORMAL_MODE)
1976     emit_insn (gen_stm_h8300s_4_advanced (operands[0], operands[1],
1977                                           operands[2], operands[3]));
1978   else
1979     emit_insn (gen_stm_h8300s_4_normal (operands[0], operands[1],
1980                                         operands[2], operands[3]));
1981   DONE;
1982 }")
1983
1984 (define_insn "ldm_h8300s_2_advanced"
1985   [(set (reg:SI SP_REG)
1986         (plus:SI (reg:SI SP_REG) (const_int 8)))
1987    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int 4)))
1988         (match_operand:SI 0 "register_operand" ""))
1989    (set (mem:SI (reg:SI SP_REG))
1990         (match_operand:SI 1 "register_operand" ""))]
1991   "TARGET_H8300S && !TARGET_NORMAL_MODE
1992    && h8300_regs_ok_for_stm (2, operands)"
1993   "ldm.l\\t@er7+,%S0-%S1"
1994   [(set_attr "cc" "none")
1995    (set_attr "length" "4")])
1996
1997 (define_insn "ldm_h8300s_2_normal"
1998   [(set (reg:HI SP_REG)
1999         (plus:HI (reg:HI SP_REG) (const_int 8)))
2000    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int 4)))
2001         (match_operand:SI 0 "register_operand" ""))
2002    (set (mem:SI (reg:HI SP_REG))
2003         (match_operand:SI 1 "register_operand" ""))]
2004   "TARGET_H8300S && TARGET_NORMAL_MODE
2005    && h8300_regs_ok_for_stm (2, operands)"
2006   "ldm.l\\t@er7+,%S0-%S1"
2007   [(set_attr "cc" "none")
2008    (set_attr "length" "4")])
2009
2010 (define_expand "ldm_h8300s_2"
2011   [(match_operand:SI 0 "register_operand" "")
2012    (match_operand:SI 1 "register_operand" "")]
2013   "TARGET_H8300S
2014    && h8300_regs_ok_for_stm (2, operands)"
2015   "
2016 {
2017   if (!TARGET_NORMAL_MODE)
2018     emit_insn (gen_ldm_h8300s_2_advanced (operands[0], operands[1]));
2019   else
2020     emit_insn (gen_ldm_h8300s_2_normal (operands[0], operands[1]));
2021   DONE;
2022 }")
2023
2024 (define_insn "ldm_h8300s_3_advanced"
2025   [(set (reg:SI SP_REG)
2026         (plus:SI (reg:SI SP_REG) (const_int 12)))
2027    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int 8)))
2028         (match_operand:SI 0 "register_operand" ""))
2029    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int 4)))
2030         (match_operand:SI 1 "register_operand" ""))
2031    (set (mem:SI (reg:SI SP_REG))
2032         (match_operand:SI 2 "register_operand" ""))]
2033   "TARGET_H8300S && !TARGET_NORMAL_MODE
2034    && h8300_regs_ok_for_stm (3, operands)"
2035   "ldm.l\\t@er7+,%S0-%S2"
2036   [(set_attr "cc" "none")
2037    (set_attr "length" "4")])
2038
2039 (define_insn "ldm_h8300s_3_normal"
2040   [(set (reg:HI SP_REG)
2041         (plus:HI (reg:HI SP_REG) (const_int 12)))
2042    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int 8)))
2043         (match_operand:SI 0 "register_operand" ""))
2044    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int 4)))
2045         (match_operand:SI 1 "register_operand" ""))
2046    (set (mem:SI (reg:HI SP_REG))
2047         (match_operand:SI 2 "register_operand" ""))]
2048   "TARGET_H8300S && TARGET_NORMAL_MODE
2049    && h8300_regs_ok_for_stm (3, operands)"
2050   "ldm.l\\t@er7+,%S0-%S2"
2051   [(set_attr "cc" "none")
2052    (set_attr "length" "4")])
2053
2054 (define_expand "ldm_h8300s_3"
2055   [(match_operand:SI 0 "register_operand" "")
2056    (match_operand:SI 1 "register_operand" "")
2057    (match_operand:SI 2 "register_operand" "")]
2058   "TARGET_H8300S
2059    && h8300_regs_ok_for_stm (3, operands)"
2060   "
2061 {
2062   if (!TARGET_NORMAL_MODE)
2063     emit_insn (gen_ldm_h8300s_3_advanced (operands[0], operands[1],
2064                                           operands[2]));
2065   else
2066     emit_insn (gen_ldm_h8300s_3_normal (operands[0], operands[1],
2067                                         operands[2]));
2068   DONE;
2069 }")
2070
2071 (define_insn "ldm_h8300s_4_advanced"
2072   [(set (reg:SI SP_REG)
2073         (plus:SI (reg:SI SP_REG) (const_int 16)))
2074    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int 12)))
2075         (match_operand:SI 0 "register_operand" ""))
2076    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int 8)))
2077         (match_operand:SI 1 "register_operand" ""))
2078    (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int 4)))
2079         (match_operand:SI 2 "register_operand" ""))
2080    (set (mem:SI (reg:SI SP_REG))
2081         (match_operand:SI 3 "register_operand" ""))]
2082   "TARGET_H8300S && !TARGET_NORMAL_MODE
2083    && h8300_regs_ok_for_stm (4, operands)"
2084   "ldm.l\\t@er7+,%S0-%S3"
2085   [(set_attr "cc" "none")
2086    (set_attr "length" "4")])
2087
2088 (define_insn "ldm_h8300s_4_normal"
2089   [(set (reg:HI SP_REG)
2090         (plus:HI (reg:HI SP_REG) (const_int 16)))
2091    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int 12)))
2092         (match_operand:SI 0 "register_operand" ""))
2093    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int 8)))
2094         (match_operand:SI 1 "register_operand" ""))
2095    (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int 4)))
2096         (match_operand:SI 2 "register_operand" ""))
2097    (set (mem:SI (reg:HI SP_REG))
2098         (match_operand:SI 3 "register_operand" ""))]
2099   "TARGET_H8300S && !TARGET_NORMAL_MODE
2100    && h8300_regs_ok_for_stm (4, operands)"
2101   "ldm.l\\t@er7+,%S0-%S3"
2102   [(set_attr "cc" "none")
2103    (set_attr "length" "4")])
2104
2105 (define_expand "ldm_h8300s_4"
2106   [(match_operand:SI 0 "register_operand" "")
2107    (match_operand:SI 1 "register_operand" "")
2108    (match_operand:SI 2 "register_operand" "")
2109    (match_operand:SI 3 "register_operand" "")]
2110   "TARGET_H8300S && !TARGET_NORMAL_MODE
2111    && h8300_regs_ok_for_stm (4, operands)"
2112   "
2113 {
2114   if (!TARGET_NORMAL_MODE)
2115     emit_insn (gen_ldm_h8300s_4_advanced (operands[0], operands[1],
2116                                           operands[2], operands[3]));
2117   else
2118     emit_insn (gen_ldm_h8300s_4_normal (operands[0], operands[1],
2119                                         operands[2], operands[3]));
2120   DONE;
2121 }")
2122
2123 (define_expand "return"
2124   [(return)]
2125   "h8300_can_use_return_insn_p ()"
2126   "")
2127
2128 (define_insn "*return_1"
2129   [(return)]
2130   "reload_completed"
2131   "*
2132 {
2133   if (h8300_current_function_interrupt_function_p ())
2134     return \"rte\";
2135   else
2136     return \"rts\";
2137 }"
2138   [(set_attr "cc" "none")
2139    (set_attr "length" "2")])
2140
2141 (define_expand "prologue"
2142   [(const_int 0)]
2143   ""
2144   "h8300_expand_prologue (); DONE;")
2145
2146 (define_expand "epilogue"
2147   [(return)]
2148   ""
2149   "h8300_expand_epilogue ();")
2150
2151 (define_insn "monitor_prologue"
2152   [(unspec_volatile [(const_int 0)] UNSPEC_MONITOR)]
2153   ""
2154   "*
2155 {
2156   if (TARGET_H8300)
2157     return \"subs\\t#2,r7\;mov.w\\tr0,@-r7\;stc\\tccr,r0l\;mov.b\tr0l,@(2,r7)\;mov.w\\t@r7+,r0\;orc\t#128,ccr\";
2158   else if (TARGET_H8300H)
2159     return \"mov.l\\ter0,@-er7\;stc\\tccr,r0l\;mov.b\\tr0l,@(4,er7)\;mov.l\\t@er7+,er0\;orc\\t#128,ccr\";
2160   else if (TARGET_H8300S)
2161     return \"stc\texr,@-er7\;mov.l\\ter0,@-er7\;stc\tccr,r0l\;mov.b\tr0l,@(6,er7)\;mov.l\\t@er7+,er0\;orc\t#128,ccr\";
2162     abort ();
2163 }"
2164   [(set_attr "length" "20")])
2165 \f
2166 ;; ----------------------------------------------------------------------
2167 ;; EXTEND INSTRUCTIONS
2168 ;; ----------------------------------------------------------------------
2169
2170 (define_expand "zero_extendqihi2"
2171   [(set (match_operand:HI 0 "register_operand" "")
2172         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "")))]
2173   ""
2174   "")
2175
2176 (define_insn "*zero_extendqihi2_h8300"
2177   [(set (match_operand:HI 0 "register_operand" "=r,r")
2178         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2179   "TARGET_H8300"
2180   "@
2181   mov.b #0,%t0
2182   #"
2183   [(set_attr "length" "2,10")])
2184
2185 (define_insn "*zero_extendqihi2_h8300hs"
2186   [(set (match_operand:HI 0 "register_operand" "=r,r")
2187         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2188   "TARGET_H8300H || TARGET_H8300S"
2189   "@
2190   extu.w        %T0
2191   #"
2192   [(set_attr "length" "2,10")
2193    (set_attr "cc" "set_znv,set_znv")])
2194
2195 ;; Split the zero extension of a general operand (actually a memory
2196 ;; operand) into a load of the operand and the actual zero extension
2197 ;; so that 1) the length will be accurate, and 2) the zero extensions
2198 ;; appearing at the end of basic blocks may be merged.
2199
2200 (define_split
2201   [(set (match_operand:HI 0 "register_operand" "")
2202         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "")))]
2203   "reload_completed"
2204   [(set (match_dup 2)
2205         (match_dup 1))
2206    (set (match_dup 0)
2207         (zero_extend:HI (match_dup 2)))]
2208   "operands[2] = gen_rtx_REG (QImode, REGNO (operands[0]));")
2209
2210 (define_expand "zero_extendqisi2"
2211   [(set (match_operand:SI 0 "register_operand" "")
2212         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "")))]
2213   ""
2214   "")
2215
2216 (define_insn "*zero_extendqisi2_h8300"
2217   [(set (match_operand:SI 0 "register_operand" "=r,r")
2218         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2219   "TARGET_H8300"
2220   "@
2221   mov.b #0,%x0\;sub.w   %e0,%e0
2222   mov.b %R1,%w0\;mov.b  #0,%x0\;sub.w   %e0,%e0"
2223   [(set_attr "length" "4,8")])
2224
2225 (define_insn "*zero_extendqisi2_h8300hs"
2226   [(set (match_operand:SI 0 "register_operand" "=r,r")
2227         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2228   "TARGET_H8300H || TARGET_H8300S"
2229   "#")
2230
2231 (define_split
2232   [(set (match_operand:SI 0 "register_operand" "")
2233         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "")))]
2234   "(TARGET_H8300H || TARGET_H8300S)
2235    && reg_overlap_mentioned_p (operands[0], operands[1])
2236    && reload_completed"
2237   [(set (match_dup 2)
2238         (match_dup 1))
2239    (set (match_dup 3)
2240         (zero_extend:HI (match_dup 2)))
2241    (set (match_dup 0)
2242         (zero_extend:SI (match_dup 3)))]
2243   "operands[2] = gen_lowpart (QImode, operands[0]);
2244    operands[3] = gen_lowpart (HImode, operands[0]);")
2245
2246 (define_split
2247   [(set (match_operand:SI 0 "register_operand" "")
2248         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "")))]
2249   "(TARGET_H8300H || TARGET_H8300S)
2250    && !reg_overlap_mentioned_p (operands[0], operands[1])
2251    && reload_completed"
2252   [(set (match_dup 0)
2253         (const_int 0))
2254    (set (strict_low_part (match_dup 2))
2255         (match_dup 1))]
2256   "operands[2] = gen_rtx_REG (QImode, REGNO (operands[0]));")
2257
2258 (define_expand "zero_extendhisi2"
2259   [(set (match_operand:SI 0 "register_operand" "")
2260         (zero_extend:SI (match_operand:HI 1 "register_operand" "")))]
2261   ""
2262   "")
2263
2264 ;; %e prints the high part of a CONST_INT, not the low part.  Arggh.
2265 (define_insn "*zero_extendhisi2_h8300"
2266   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
2267         (zero_extend:SI (match_operand:HI 1 "general_operand_src" "0,i,g>")))]
2268   "TARGET_H8300"
2269   "@
2270   sub.w %e0,%e0
2271   mov.w %f1,%f0\;sub.w  %e0,%e0
2272   mov.w %e1,%f0\;sub.w  %e0,%e0"
2273   [(set_attr "length" "2,4,6")])
2274
2275 (define_insn "*zero_extendhisi2_h8300hs"
2276   [(set (match_operand:SI 0 "register_operand" "=r")
2277         (zero_extend:SI (match_operand:HI 1 "register_operand" "0")))]
2278   "TARGET_H8300H || TARGET_H8300S"
2279   "extu.l       %S0"
2280   [(set_attr "length" "2")
2281    (set_attr "cc" "set_znv")])
2282
2283 (define_expand "extendqihi2"
2284   [(set (match_operand:HI 0 "register_operand" "")
2285         (sign_extend:HI (match_operand:QI 1 "register_operand" "")))]
2286   ""
2287   "")
2288
2289 (define_insn "*extendqihi2_h8300"
2290   [(set (match_operand:HI 0 "register_operand" "=r,r")
2291         (sign_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2292   "TARGET_H8300"
2293   "@
2294   bld   #7,%s0\;subx    %t0,%t0
2295   mov.b %R1,%s0\;bld    #7,%s0\;subx    %t0,%t0"
2296   [(set_attr "length" "4,8")])
2297
2298 (define_insn "*extendqihi2_h8300hs"
2299   [(set (match_operand:HI 0 "register_operand" "=r")
2300         (sign_extend:HI (match_operand:QI 1 "register_operand" "0")))]
2301   "TARGET_H8300H || TARGET_H8300S"
2302   "exts.w       %T0"
2303   [(set_attr "length" "2")
2304    (set_attr "cc" "set_znv")])
2305
2306 (define_expand "extendqisi2"
2307   [(set (match_operand:SI 0 "register_operand" "")
2308         (sign_extend:SI (match_operand:QI 1 "register_operand" "")))]
2309   ""
2310   "")
2311
2312 (define_insn "*extendqisi2_h8300"
2313   [(set (match_operand:SI 0 "register_operand" "")
2314         (sign_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2315   "TARGET_H8300"
2316   "@
2317   bld   #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0
2318   mov.b %R1,%w0\;bld    #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0"
2319   [(set_attr "length" "8,12")])
2320
2321 ;; The following pattern is needed because without the pattern, the
2322 ;; combiner would split (sign_extend:SI (reg:QI)) into into two 24-bit
2323 ;; shifts, one ashift and one ashiftrt.
2324
2325 (define_insn_and_split "*extendqisi2_h8300hs"
2326   [(set (match_operand:SI 0 "register_operand" "=r")
2327         (sign_extend:SI (match_operand:QI 1 "register_operand" "0")))]
2328   "(TARGET_H8300H || TARGET_H8300S)"
2329   "#"
2330   "&& reload_completed"
2331   [(set (match_dup 2)
2332         (sign_extend:HI (match_dup 1)))
2333    (set (match_dup 0)
2334         (sign_extend:SI (match_dup 2)))]
2335   "operands[2] = gen_rtx_REG (HImode, REGNO (operands[0]));")
2336
2337 (define_expand "extendhisi2"
2338   [(set (match_operand:SI 0 "register_operand" "")
2339         (sign_extend:SI (match_operand:HI 1 "register_operand" "")))]
2340   ""
2341   "")
2342
2343 (define_insn "*extendhisi2_h8300"
2344   [(set (match_operand:SI 0 "register_operand" "=r,r")
2345         (sign_extend:SI (match_operand:HI 1 "general_operand_src" "0,g>")))]
2346   "TARGET_H8300"
2347   "@
2348   bld   #7,%x0\;subx    %y0,%y0\;subx   %z0,%z0
2349   mov.w %T1,%f0\;bld    #7,%x0\;subx    %y0,%y0\;subx   %z0,%z0"
2350   [(set_attr "length" "6,10")])
2351
2352 (define_insn "*extendhisi2_h8300hs"
2353   [(set (match_operand:SI 0 "register_operand" "=r")
2354         (sign_extend:SI (match_operand:HI 1 "register_operand" "0")))]
2355   "TARGET_H8300H || TARGET_H8300S"
2356   "exts.l       %S0"
2357   [(set_attr "length" "2")
2358    (set_attr "cc" "set_znv")])
2359 \f
2360 ;; ----------------------------------------------------------------------
2361 ;; SHIFTS
2362 ;; ----------------------------------------------------------------------
2363 ;;
2364 ;; We make some attempt to provide real efficient shifting.  One example is
2365 ;; doing an 8 bit shift of a 16 bit value by moving a byte reg into the other
2366 ;; reg and moving 0 into the former reg.
2367 ;;
2368 ;; We also try to achieve this in a uniform way.  IE: We don't try to achieve
2369 ;; this in both rtl and at insn emit time.  Ideally, we'd use rtl as that would
2370 ;; give the optimizer more cracks at the code.  However, we wish to do things
2371 ;; like optimizing shifting the sign bit to bit 0 by rotating the other way.
2372 ;; There is rtl to handle this (rotate + and), but the H8/300 doesn't handle
2373 ;; 16 bit rotates.  Also, if we emit complicated rtl, combine may not be able
2374 ;; to detect cases it can optimize.
2375 ;;
2376 ;; For these and other fuzzy reasons, I've decided to go the less pretty but
2377 ;; easier "do it at insn emit time" route.
2378
2379 ;; QI BIT SHIFTS
2380
2381 (define_expand "ashlqi3"
2382   [(set (match_operand:QI 0 "register_operand" "")
2383         (ashift:QI (match_operand:QI 1 "register_operand" "")
2384                    (match_operand:QI 2 "nonmemory_operand" "")))]
2385   ""
2386   "expand_a_shift (QImode, ASHIFT, operands); DONE;")
2387
2388 (define_expand "ashrqi3"
2389   [(set (match_operand:QI 0 "register_operand" "")
2390         (ashiftrt:QI (match_operand:QI 1 "register_operand" "")
2391                      (match_operand:QI 2 "nonmemory_operand" "")))]
2392   ""
2393   "expand_a_shift (QImode, ASHIFTRT, operands); DONE;")
2394
2395 (define_expand "lshrqi3"
2396   [(set (match_operand:QI 0 "register_operand" "")
2397         (lshiftrt:QI (match_operand:QI 1 "register_operand" "")
2398                      (match_operand:QI 2 "nonmemory_operand" "")))]
2399   ""
2400   "expand_a_shift (QImode, LSHIFTRT, operands); DONE;")
2401
2402 (define_insn "*shiftqi"
2403   [(set (match_operand:QI 0 "register_operand" "=r,r")
2404         (match_operator:QI 3 "nshift_operator"
2405                         [ (match_operand:QI 1 "register_operand" "0,0")
2406                           (match_operand:QI 2 "nonmemory_operand" "R,rn")]))
2407    (clobber (match_scratch:QI 4 "=X,&r"))]
2408   ""
2409   "* return output_a_shift (operands);"
2410   [(set (attr "length")
2411         (symbol_ref "compute_a_shift_length (insn, operands)"))
2412    (set (attr "cc")
2413         (symbol_ref "compute_a_shift_cc (insn, operands)"))])
2414
2415 ;; HI BIT SHIFTS
2416
2417 (define_expand "ashlhi3"
2418   [(set (match_operand:HI 0 "register_operand" "")
2419         (ashift:HI (match_operand:HI 1 "nonmemory_operand" "")
2420                    (match_operand:QI 2 "nonmemory_operand" "")))]
2421   ""
2422   "expand_a_shift (HImode, ASHIFT, operands); DONE;")
2423
2424 (define_expand "lshrhi3"
2425   [(set (match_operand:HI 0 "register_operand" "")
2426         (lshiftrt:HI (match_operand:HI 1 "general_operand" "")
2427                      (match_operand:QI 2 "nonmemory_operand" "")))]
2428   ""
2429   "expand_a_shift (HImode, LSHIFTRT, operands); DONE;")
2430
2431 (define_expand "ashrhi3"
2432   [(set (match_operand:HI 0 "register_operand" "")
2433         (ashiftrt:HI (match_operand:HI 1 "register_operand" "")
2434                      (match_operand:QI 2 "nonmemory_operand" "")))]
2435   ""
2436   "expand_a_shift (HImode, ASHIFTRT, operands); DONE;")
2437
2438 (define_insn "*shifthi"
2439   [(set (match_operand:HI 0 "register_operand" "=r,r")
2440         (match_operator:HI 3 "nshift_operator"
2441                         [ (match_operand:HI 1 "register_operand" "0,0")
2442                           (match_operand:QI 2 "nonmemory_operand" "S,rn")]))
2443    (clobber (match_scratch:QI 4 "=X,&r"))]
2444   ""
2445   "* return output_a_shift (operands);"
2446   [(set (attr "length")
2447         (symbol_ref "compute_a_shift_length (insn, operands)"))
2448    (set (attr "cc")
2449         (symbol_ref "compute_a_shift_cc (insn, operands)"))])
2450
2451 ;;  SI BIT SHIFTS
2452
2453 (define_expand "ashlsi3"
2454   [(set (match_operand:SI 0 "register_operand" "")
2455         (ashift:SI (match_operand:SI 1 "general_operand" "")
2456                    (match_operand:QI 2 "nonmemory_operand" "")))]
2457   ""
2458   "expand_a_shift (SImode, ASHIFT, operands); DONE;")
2459
2460 (define_expand "lshrsi3"
2461   [(set (match_operand:SI 0 "register_operand" "")
2462         (lshiftrt:SI (match_operand:SI 1 "general_operand" "")
2463                      (match_operand:QI 2 "nonmemory_operand" "")))]
2464   ""
2465   "expand_a_shift (SImode, LSHIFTRT, operands); DONE;")
2466
2467 (define_expand "ashrsi3"
2468   [(set (match_operand:SI 0 "register_operand" "")
2469         (ashiftrt:SI (match_operand:SI 1 "general_operand" "")
2470                      (match_operand:QI 2 "nonmemory_operand" "")))]
2471   ""
2472   "expand_a_shift (SImode, ASHIFTRT, operands); DONE;")
2473
2474 (define_insn "*shiftsi"
2475   [(set (match_operand:SI 0 "register_operand" "=r,r")
2476         (match_operator:SI 3 "nshift_operator"
2477                         [ (match_operand:SI 1 "register_operand" "0,0")
2478                           (match_operand:QI 2 "nonmemory_operand" "T,rn")]))
2479    (clobber (match_scratch:QI 4 "=X,&r"))]
2480   ""
2481   "* return output_a_shift (operands);"
2482   [(set (attr "length")
2483         (symbol_ref "compute_a_shift_length (insn, operands)"))
2484    (set (attr "cc")
2485         (symbol_ref "compute_a_shift_cc (insn, operands)"))])
2486
2487 ;; Split a variable shift into a loop.  If the register containing
2488 ;; the shift count dies, then we just use that register.
2489
2490 (define_split
2491   [(set (match_operand 0 "register_operand" "")
2492         (match_operator 2 "nshift_operator"
2493                         [(match_dup 0)
2494                          (match_operand:QI 1 "register_operand" "")]))
2495    (clobber (match_operand:QI 3 "register_operand" ""))]
2496   "flow2_completed
2497    && find_regno_note (insn, REG_DEAD, REGNO (operands[1]))"
2498   [(set (cc0)
2499         (match_dup 1))
2500    (set (pc)
2501         (if_then_else (le (cc0) (const_int 0))
2502                       (label_ref (match_dup 5))
2503                       (pc)))
2504    (match_dup 4)
2505    (parallel
2506      [(set (match_dup 0)
2507            (match_op_dup 2 [(match_dup 0) (const_int 1)]))
2508       (clobber (scratch:QI))])
2509    (set (match_dup 1)
2510         (plus:QI (match_dup 1) (const_int -1)))
2511    (set (cc0)
2512         (match_dup 1))
2513    (set (pc)
2514         (if_then_else (ne (cc0) (const_int 0))
2515                       (label_ref (match_dup 4))
2516                       (pc)))
2517    (match_dup 5)]
2518   "operands[4] = gen_label_rtx ();
2519    operands[5] = gen_label_rtx ();")
2520
2521 (define_split
2522   [(set (match_operand 0 "register_operand" "")
2523         (match_operator 2 "nshift_operator"
2524                         [(match_dup 0)
2525                          (match_operand:QI 1 "register_operand" "")]))
2526    (clobber (match_operand:QI 3 "register_operand" ""))]
2527   "flow2_completed
2528    && !find_regno_note (insn, REG_DEAD, REGNO (operands[1]))"
2529   [(set (match_dup 3)
2530         (match_dup 1))
2531    (set (cc0)
2532         (match_dup 3))
2533    (set (pc)
2534         (if_then_else (le (cc0) (const_int 0))
2535                       (label_ref (match_dup 5))
2536                       (pc)))
2537    (match_dup 4)
2538    (parallel
2539      [(set (match_dup 0)
2540            (match_op_dup 2 [(match_dup 0) (const_int 1)]))
2541       (clobber (scratch:QI))])
2542    (set (match_dup 3)
2543         (plus:QI (match_dup 3) (const_int -1)))
2544    (set (cc0)
2545         (match_dup 3))
2546    (set (pc)
2547         (if_then_else (ne (cc0) (const_int 0))
2548                       (label_ref (match_dup 4))
2549                       (pc)))
2550    (match_dup 5)]
2551   "operands[4] = gen_label_rtx ();
2552    operands[5] = gen_label_rtx ();")
2553 \f
2554 ;; ----------------------------------------------------------------------
2555 ;; ROTATIONS
2556 ;; ----------------------------------------------------------------------
2557
2558 (define_expand "rotlqi3"
2559   [(set (match_operand:QI 0 "register_operand" "")
2560         (rotate:QI (match_operand:QI 1 "register_operand" "")
2561                    (match_operand:QI 2 "nonmemory_operand" "")))]
2562   ""
2563   "if (expand_a_rotate (operands)) DONE; else FAIL;")
2564
2565 (define_insn "rotlqi3_1"
2566   [(set (match_operand:QI 0 "register_operand" "=r")
2567         (rotate:QI (match_operand:QI 1 "register_operand" "0")
2568                    (match_operand:QI 2 "immediate_operand" "")))]
2569   ""
2570   "* return output_a_rotate (ROTATE, operands);"
2571   [(set (attr "length")
2572         (symbol_ref "compute_a_rotate_length (operands)"))])
2573
2574 (define_expand "rotlhi3"
2575   [(set (match_operand:HI 0 "register_operand" "")
2576         (rotate:HI (match_operand:HI 1 "register_operand" "")
2577                    (match_operand:QI 2 "nonmemory_operand" "")))]
2578   ""
2579   "if (expand_a_rotate (operands)) DONE; else FAIL;")
2580
2581 (define_insn "rotlhi3_1"
2582   [(set (match_operand:HI 0 "register_operand" "=r")
2583         (rotate:HI (match_operand:HI 1 "register_operand" "0")
2584                    (match_operand:QI 2 "immediate_operand" "")))]
2585   ""
2586   "* return output_a_rotate (ROTATE, operands);"
2587   [(set (attr "length")
2588         (symbol_ref "compute_a_rotate_length (operands)"))])
2589
2590 (define_expand "rotlsi3"
2591   [(set (match_operand:SI 0 "register_operand" "")
2592         (rotate:SI (match_operand:SI 1 "register_operand" "")
2593                    (match_operand:QI 2 "nonmemory_operand" "")))]
2594   "TARGET_H8300H || TARGET_H8300S"
2595   "if (expand_a_rotate (operands)) DONE; else FAIL;")
2596
2597 (define_insn "rotlsi3_1"
2598   [(set (match_operand:SI 0 "register_operand" "=r")
2599         (rotate:SI (match_operand:SI 1 "register_operand" "0")
2600                    (match_operand:QI 2 "immediate_operand" "")))]
2601   "TARGET_H8300H || TARGET_H8300S"
2602   "* return output_a_rotate (ROTATE, operands);"
2603   [(set (attr "length")
2604         (symbol_ref "compute_a_rotate_length (operands)"))])
2605 \f
2606 ;; -----------------------------------------------------------------
2607 ;; BIT FIELDS
2608 ;; -----------------------------------------------------------------
2609 ;; The H8/300 has given 1/8th of its opcode space to bitfield
2610 ;; instructions so let's use them as well as we can.
2611
2612 ;; You'll never believe all these patterns perform one basic action --
2613 ;; load a bit from the source, optionally invert the bit, then store it
2614 ;; in the destination (which is known to be zero).
2615 ;;
2616 ;; Combine obviously need some work to better identify this situation and
2617 ;; canonicalize the form better.
2618
2619 ;;
2620 ;; Normal loads with a 16bit destination.
2621 ;;
2622
2623 (define_insn ""
2624   [(set (match_operand:HI 0 "register_operand" "=&r")
2625         (zero_extract:HI (match_operand:HI 1 "register_operand" "r")
2626                          (const_int 1)
2627                          (match_operand:HI 2 "immediate_operand" "n")))]
2628   "TARGET_H8300"
2629   "sub.w        %0,%0\;bld      %Z2,%Y1\;bst    #0,%X0"
2630   [(set_attr "length" "6")])
2631
2632 ;;
2633 ;; Inverted loads with a 16bit destination.
2634 ;;
2635
2636 (define_insn ""
2637   [(set (match_operand:HI 0 "register_operand" "=&r")
2638         (zero_extract:HI (xor:HI (match_operand:HI 1 "register_operand" "r")
2639                                  (match_operand:HI 3 "const_int_operand" "n"))
2640                          (const_int 1)
2641                          (match_operand:HI 2 "const_int_operand" "n")))]
2642   "TARGET_H8300
2643    && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
2644   "sub.w        %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
2645   [(set_attr "length" "8")])
2646
2647 ;;
2648 ;; Normal loads with a 32bit destination.
2649 ;;
2650
2651 (define_insn "*extzv_1_r_h8300"
2652   [(set (match_operand:SI 0 "register_operand" "=&r")
2653         (zero_extract:SI (match_operand:HI 1 "register_operand" "r")
2654                          (const_int 1)
2655                          (match_operand 2 "const_int_operand" "n")))]
2656   "TARGET_H8300
2657    && INTVAL (operands[2]) < 16"
2658   "* return output_simode_bld (0, operands);"
2659   [(set_attr "length" "8")])
2660
2661 (define_insn "*extzv_1_r_h8300hs"
2662   [(set (match_operand:SI 0 "register_operand" "=r,r")
2663         (zero_extract:SI (match_operand:SI 1 "register_operand" "?0,r")
2664                          (const_int 1)
2665                          (match_operand 2 "const_int_operand" "n,n")))]
2666   "(TARGET_H8300H || TARGET_H8300S)
2667    && INTVAL (operands[2]) < 16"
2668   "* return output_simode_bld (0, operands);"
2669   [(set_attr "cc" "set_znv,set_znv")
2670    (set_attr "length" "8,6")])
2671
2672 ;;
2673 ;; Inverted loads with a 32bit destination.
2674 ;;
2675
2676 (define_insn "*extzv_1_r_inv_h8300"
2677   [(set (match_operand:SI 0 "register_operand" "=&r")
2678         (zero_extract:SI (xor:HI (match_operand:HI 1 "register_operand" "r")
2679                                  (match_operand:HI 3 "const_int_operand" "n"))
2680                          (const_int 1)
2681                          (match_operand 2 "const_int_operand" "n")))]
2682   "TARGET_H8300
2683    && INTVAL (operands[2]) < 16
2684    && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
2685   "* return output_simode_bld (1, operands);"
2686   [(set_attr "length" "8")])
2687
2688 (define_insn "*extzv_1_r_inv_h8300hs"
2689   [(set (match_operand:SI 0 "register_operand" "=r,r")
2690         (zero_extract:SI (xor:SI (match_operand:SI 1 "register_operand" "?0,r")
2691                                  (match_operand 3 "const_int_operand" "n,n"))
2692                          (const_int 1)
2693                          (match_operand 2 "const_int_operand" "n,n")))]
2694   "(TARGET_H8300H || TARGET_H8300S)
2695    && INTVAL (operands[2]) < 16
2696    && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
2697   "* return output_simode_bld (1, operands);"
2698   [(set_attr "cc" "set_znv,set_znv")
2699    (set_attr "length" "8,6")])
2700
2701 (define_expand "insv"
2702   [(set (zero_extract:HI (match_operand:HI 0 "general_operand" "")
2703                          (match_operand:HI 1 "general_operand" "")
2704                          (match_operand:HI 2 "general_operand" ""))
2705         (match_operand:HI 3 "general_operand" ""))]
2706   "TARGET_H8300"
2707   "
2708 {
2709   /* We only have single bit bit-field instructions.  */
2710   if (INTVAL (operands[1]) != 1)
2711     FAIL;
2712
2713   /* For now, we don't allow memory operands.  */
2714   if (GET_CODE (operands[0]) == MEM
2715       || GET_CODE (operands[3]) == MEM)
2716     FAIL;
2717 }")
2718
2719 (define_insn ""
2720   [(set (zero_extract:HI (match_operand:HI 0 "register_operand" "+r")
2721                          (const_int 1)
2722                          (match_operand:HI 1 "immediate_operand" "n"))
2723         (match_operand:HI 2 "register_operand" "r"))]
2724   ""
2725   "bld  #0,%R2\;bst     %Z1,%Y0 ; i1"
2726   [(set_attr "length" "4")])
2727
2728 (define_expand "extzv"
2729   [(set (match_operand:HI 0 "register_operand" "")
2730         (zero_extract:HI (match_operand:HI 1 "bit_operand" "")
2731                          (match_operand:HI 2 "general_operand" "")
2732                          (match_operand:HI 3 "general_operand" "")))]
2733   "TARGET_H8300"
2734   "
2735 {
2736   /* We only have single bit bit-field instructions.  */
2737   if (INTVAL (operands[2]) != 1)
2738     FAIL;
2739
2740   /* For now, we don't allow memory operands.  */
2741   if (GET_CODE (operands[1]) == MEM)
2742     FAIL;
2743 }")
2744
2745 ;; BAND, BOR, and BXOR patterns
2746
2747 (define_insn ""
2748   [(set (match_operand:HI 0 "bit_operand" "=Ur")
2749         (match_operator:HI 4 "bit_operator"
2750            [(zero_extract:HI (match_operand:HI 1 "register_operand" "r")
2751                              (const_int 1)
2752                              (match_operand:HI 2 "immediate_operand" "n"))
2753             (match_operand:HI 3 "bit_operand" "0")]))]
2754   ""
2755   "bld  %Z2,%Y1\;b%c4   #0,%R0\;bst     #0,%R0; bl1"
2756   [(set_attr "length" "6")])
2757
2758 (define_insn ""
2759   [(set (match_operand:HI 0 "bit_operand" "=Ur")
2760         (match_operator:HI 5 "bit_operator"
2761            [(zero_extract:HI (match_operand:HI 1 "register_operand" "r")
2762                              (const_int 1)
2763                              (match_operand:HI 2 "immediate_operand" "n"))
2764             (zero_extract:HI (match_operand:HI 3 "register_operand" "r")
2765                              (const_int 1)
2766                              (match_operand:HI 4 "immediate_operand" "n"))]))]
2767   ""
2768   "bld  %Z2,%Y1\;b%c5   %Z4,%Y3\;bst    #0,%R0; bl3"
2769   [(set_attr "length" "6")])
2770 \f
2771 ;; -----------------------------------------------------------------
2772 ;; COMBINE PATTERNS
2773 ;; -----------------------------------------------------------------
2774
2775 ;; insv:SI
2776
2777 (define_insn "*insv_si_1_n"
2778   [(set (zero_extract:SI (match_operand:SI 0 "register_operand" "+r")
2779                          (const_int 1)
2780                          (match_operand:SI 1 "const_int_operand" "n"))
2781         (match_operand:SI 2 "register_operand" "r"))]
2782   "(TARGET_H8300H || TARGET_H8300S)
2783    && INTVAL (operands[1]) < 16"
2784   "bld\\t#0,%w2\;bst\\t%Z1,%Y0"
2785   [(set_attr "length" "4")])
2786
2787 (define_insn "*insv_si_1_n_lshiftrt"
2788   [(set (zero_extract:SI (match_operand:SI 0 "register_operand" "+r")
2789                          (const_int 1)
2790                          (match_operand:SI 1 "const_int_operand" "n"))
2791         (lshiftrt:SI (match_operand:SI 2 "register_operand" "r")
2792                      (match_operand:SI 3 "const_int_operand" "n")))]
2793   "(TARGET_H8300H || TARGET_H8300S)
2794    && INTVAL (operands[1]) < 16
2795    && INTVAL (operands[3]) < 16"
2796   "bld\\t%Z3,%Y2\;bst\\t%Z1,%Y0"
2797   [(set_attr "length" "4")])
2798
2799 (define_insn "*insv_si_1_n_lshiftrt_16"
2800   [(set (zero_extract:SI (match_operand:SI 0 "register_operand" "+r")
2801                          (const_int 1)
2802                          (match_operand:SI 1 "const_int_operand" "n"))
2803         (lshiftrt:SI (match_operand:SI 2 "register_operand" "r")
2804                      (const_int 16)))]
2805   "(TARGET_H8300H || TARGET_H8300S)
2806    && INTVAL (operands[1]) < 16"
2807   "rotr.w\\t%e2\;rotl.w\\t%e2\;bst\\t%Z1,%Y0"
2808   [(set_attr "length" "6")])
2809
2810 (define_insn "*insv_si_8_8"
2811   [(set (zero_extract:SI (match_operand:SI 0 "register_operand" "+r")
2812                          (const_int 8)
2813                          (const_int 8))
2814         (match_operand:SI 1 "register_operand" "r"))]
2815   "TARGET_H8300H || TARGET_H8300S"
2816   "mov.b\\t%w1,%x0"
2817   [(set_attr "length" "2")])
2818
2819 (define_insn "*insv_si_8_8_lshiftrt_8"
2820   [(set (zero_extract:SI (match_operand:SI 0 "register_operand" "+r")
2821                          (const_int 8)
2822                          (const_int 8))
2823         (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
2824                      (const_int 8)))]
2825   "TARGET_H8300H || TARGET_H8300S"
2826   "mov.b\\t%x1,%x0"
2827   [(set_attr "length" "2")])
2828
2829 ;; extzv:SI
2830
2831 (define_insn "*extzv_8_8"
2832   [(set (match_operand:SI 0 "register_operand" "=r,r")
2833         (zero_extract:SI (match_operand:SI 1 "register_operand" "?0,r")
2834                          (const_int 8)
2835                          (const_int 8)))]
2836   "TARGET_H8300H || TARGET_H8300S"
2837   "@
2838    mov.b\\t%x1,%w0\;extu.w\\t%f0\;extu.l\\t%S0
2839    sub.l\\t%S0,%S0\;mov.b\\t%x1,%w0"
2840   [(set_attr "cc" "set_znv,clobber")
2841    (set_attr "length" "6,4")])
2842
2843 (define_insn "*extzv_8_16"
2844   [(set (match_operand:SI 0 "register_operand" "=r")
2845         (zero_extract:SI (match_operand:SI 1 "register_operand" "r")
2846                          (const_int 8)
2847                          (const_int 16)))]
2848   "TARGET_H8300H || TARGET_H8300S"
2849   "mov.w\\t%e1,%f0\;extu.w\\t%f0\;extu.l\\t%S0"
2850   [(set_attr "cc" "set_znv")
2851    (set_attr "length" "6")])
2852
2853 (define_insn "*extzv_16_8"
2854   [(set (match_operand:SI 0 "register_operand" "=r")
2855         (zero_extract:SI (match_operand:SI 1 "register_operand" "r")
2856                          (const_int 16)
2857                          (const_int 8)))
2858    (clobber (match_scratch:SI 2 "=&r"))]
2859   "TARGET_H8300H"
2860   "mov.w\\t%e1,%f2\;mov.b\\t%x1,%w0\;mov.b\\t%w2,%x0\;extu.l\\t%S0"
2861   [(set_attr "length" "8")
2862    (set_attr "cc" "set_znv")])
2863
2864 ;; Extract the exponent of a float.
2865
2866 (define_insn_and_split "*extzv_8_23"
2867   [(set (match_operand:SI 0 "register_operand" "=r")
2868         (zero_extract:SI (match_operand:SI 1 "register_operand" "0")
2869                          (const_int 8)
2870                          (const_int 23)))]
2871   "(TARGET_H8300H || TARGET_H8300S)"
2872   "#"
2873   "&& reload_completed"
2874   [(parallel [(set (match_dup 0)
2875                    (ashift:SI (match_dup 0)
2876                               (const_int 1)))
2877               (clobber (scratch:QI))])
2878    (parallel [(set (match_dup 0)
2879                    (lshiftrt:SI (match_dup 0)
2880                                 (const_int 24)))
2881               (clobber (scratch:QI))])]
2882   "")
2883
2884 ;; and:SI
2885
2886 ;; ((SImode) HImode) << 15
2887
2888 (define_insn_and_split "*twoshifts_l16_r1"
2889   [(set (match_operand:SI 0 "register_operand" "=r")
2890         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "0")
2891                            (const_int 15))
2892                 (const_int 2147450880)))]
2893   "(TARGET_H8300H || TARGET_H8300S)"
2894   "#"
2895   "&& reload_completed"
2896   [(parallel [(set (match_dup 0)
2897                    (ashift:SI (match_dup 0)
2898                               (const_int 16)))
2899               (clobber (scratch:QI))])
2900    (parallel [(set (match_dup 0)
2901                    (lshiftrt:SI (match_dup 0)
2902                                 (const_int 1)))
2903               (clobber (scratch:QI))])]
2904   "")
2905
2906 ;; Transform (SImode << B) & 0xffff into (SImode) (HImode << B).
2907
2908 (define_insn_and_split "*andsi3_ashift_n_lower"
2909   [(set (match_operand:SI 0 "register_operand" "=r,r")
2910         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "0,0")
2911                            (match_operand:QI 2 "const_int_operand" "S,n"))
2912                 (match_operand:SI 3 "const_int_operand" "n,n")))
2913    (clobber (match_scratch:QI 4 "=X,&r"))]
2914   "(TARGET_H8300H || TARGET_H8300S)
2915    && INTVAL (operands[2]) <= 15
2916    && INTVAL (operands[3]) == ((-1 << INTVAL (operands[2])) & 0xffff)"
2917   "#"
2918   "&& reload_completed"
2919   [(parallel [(set (match_dup 5)
2920                    (ashift:HI (match_dup 5)
2921                               (match_dup 2)))
2922               (clobber (match_dup 4))])
2923    (set (match_dup 0)
2924         (zero_extend:SI (match_dup 5)))]
2925   "operands[5] = gen_rtx_REG (HImode, REGNO (operands[0]));")
2926
2927 ;; Accept (A >> 30) & 2 and the like.
2928
2929 (define_insn "*andsi3_lshiftrt_n_sb"
2930   [(set (match_operand:SI 0 "register_operand" "=r")
2931         (and:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "0")
2932                              (match_operand:SI 2 "const_int_operand" "n"))
2933                 (match_operand:SI 3 "single_one_operand" "n")))]
2934   "(TARGET_H8300H || TARGET_H8300S)
2935    && exact_log2 (INTVAL (operands[3])) < 16
2936    && INTVAL (operands[2]) + exact_log2 (INTVAL (operands[3])) == 31"
2937   "*
2938 {
2939   operands[3] = GEN_INT (exact_log2 (INTVAL (operands[3])));
2940   return \"shll.l\\t%S0\;xor.l\\t%S0,%S0\;bst\\t%Z3,%Y0\";
2941 }"
2942   [(set_attr "length" "8")])
2943
2944 (define_insn_and_split "*andsi3_lshiftrt_9_sb"
2945   [(set (match_operand:SI 0 "register_operand" "=r")
2946         (and:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "0")
2947                              (const_int 9))
2948                 (const_int 4194304)))]
2949   "(TARGET_H8300H || TARGET_H8300S)"
2950   "#"
2951   "&& reload_completed"
2952   [(set (match_dup 0)
2953         (and:SI (lshiftrt:SI (match_dup 0)
2954                              (const_int 25))
2955                 (const_int 64)))
2956    (parallel [(set (match_dup 0)
2957                    (ashift:SI (match_dup 0)
2958                               (const_int 16)))
2959               (clobber (scratch:QI))])]
2960   "")
2961
2962 ;; plus:SI
2963
2964 (define_insn "*addsi3_upper"
2965   [(set (match_operand:SI 0 "register_operand" "=r")
2966         (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "r")
2967                           (const_int 65536))
2968                  (match_operand:SI 2 "register_operand" "0")))]
2969   "TARGET_H8300H || TARGET_H8300S"
2970   "add.w\\t%f1,%e0"
2971   [(set_attr "length" "2")])
2972
2973 (define_insn "*addsi3_lshiftrt_16_zexthi"
2974   [(set (match_operand:SI 0 "register_operand" "=r")
2975         (plus:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
2976                               (const_int 16))
2977                  (zero_extend:SI (match_operand:HI 2 "register_operand" "0"))))]
2978   "TARGET_H8300H || TARGET_H8300S"
2979   "add.w\\t%e1,%f0\;xor.w\\t%e0,%e0\;rotxl.w\\t%e0"
2980   [(set_attr "length" "6")])
2981
2982 (define_insn_and_split "*addsi3_and_r_1"
2983   [(set (match_operand:SI 0 "register_operand" "=r")
2984         (plus:SI (and:SI (match_operand:SI 1 "register_operand" "r")
2985                          (const_int 1))
2986                  (match_operand:SI 2 "register_operand" "0")))]
2987   "(TARGET_H8300H || TARGET_H8300S)"
2988   "#"
2989   "&& reload_completed"
2990   [(set (cc0)
2991         (zero_extract:SI (match_dup 1)
2992                          (const_int 1)
2993                          (const_int 0)))
2994    (set (pc)
2995         (if_then_else (eq (cc0)
2996                           (const_int 0))
2997                       (label_ref (match_dup 3))
2998                       (pc)))
2999    (set (match_dup 2)
3000         (plus:SI (match_dup 2)
3001                  (const_int 1)))
3002    (match_dup 3)]
3003   "operands[3] = gen_label_rtx ();")
3004
3005 (define_insn_and_split "*addsi3_and_not_r_1"
3006   [(set (match_operand:SI 0 "register_operand" "=r")
3007         (plus:SI (and:SI (not:SI (match_operand:SI 1 "register_operand" "r"))
3008                          (const_int 1))
3009                  (match_operand:SI 2 "register_operand" "0")))]
3010   "(TARGET_H8300H || TARGET_H8300S)"
3011   "#"
3012   "&& reload_completed"
3013   [(set (cc0)
3014         (zero_extract:SI (match_dup 1)
3015                          (const_int 1)
3016                          (const_int 0)))
3017    (set (pc)
3018         (if_then_else (ne (cc0)
3019                           (const_int 0))
3020                       (label_ref (match_dup 3))
3021                       (pc)))
3022    (set (match_dup 2)
3023         (plus:SI (match_dup 2)
3024                  (const_int 1)))
3025    (match_dup 3)]
3026   "operands[3] = gen_label_rtx ();")
3027
3028 ;; [ix]or:HI
3029
3030 (define_insn "*ixorhi3_zext"
3031   [(set (match_operand:HI 0 "register_operand" "=r")
3032         (match_operator:HI 1 "iorxor_operator"
3033           [(zero_extend:HI (match_operand:QI 2 "register_operand" "r"))
3034            (match_operand:HI 3 "register_operand" "0")]))]
3035   ""
3036   "%c1.b\\t%X2,%s0"
3037   [(set_attr "length" "2")])
3038
3039 ;; [ix]or:SI
3040
3041 (define_insn "*ixorsi3_zext_qi"
3042   [(set (match_operand:SI 0 "register_operand" "=r")
3043         (match_operator:SI 1 "iorxor_operator"
3044           [(zero_extend:SI (match_operand:QI 2 "register_operand" "r"))
3045            (match_operand:SI 3 "register_operand" "0")]))]
3046   ""
3047   "%c1.b\\t%X2,%w0"
3048   [(set_attr "length" "2")])
3049
3050 (define_insn "*ixorsi3_zext_hi"
3051   [(set (match_operand:SI 0 "register_operand" "=r")
3052         (match_operator:SI 1 "iorxor_operator"
3053           [(zero_extend:SI (match_operand:HI 2 "register_operand" "r"))
3054            (match_operand:SI 3 "register_operand" "0")]))]
3055   "TARGET_H8300H || TARGET_H8300S"
3056   "%c1.w\\t%T2,%f0"
3057   [(set_attr "length" "2")])
3058
3059 (define_insn "*ixorsi3_ashift_16"
3060   [(set (match_operand:SI 0 "register_operand" "=r")
3061         (match_operator:SI 1 "iorxor_operator"
3062           [(ashift:SI (match_operand:SI 2 "register_operand" "r")
3063                       (const_int 16))
3064            (match_operand:SI 3 "register_operand" "0")]))]
3065   "TARGET_H8300H || TARGET_H8300S"
3066   "%c1.w\\t%f2,%e0"
3067   [(set_attr "length" "2")])
3068
3069 (define_insn "*ixorsi3_lshiftrt_16"
3070   [(set (match_operand:SI 0 "register_operand" "=r")
3071         (match_operator:SI 1 "iorxor_operator"
3072           [(lshiftrt:SI (match_operand:SI 2 "register_operand" "r")
3073                         (const_int 16))
3074            (match_operand:SI 3 "register_operand" "0")]))]
3075   "TARGET_H8300H || TARGET_H8300S"
3076   "%c1.w\\t%e2,%f0"
3077   [(set_attr "length" "2")])
3078
3079 ;; ior:HI
3080
3081 (define_insn "*iorhi3_ashift_8"
3082   [(set (match_operand:HI 0 "register_operand" "=r")
3083         (ior:HI (ashift:HI (match_operand:HI 1 "register_operand" "r")
3084                            (const_int 8))
3085                 (match_operand:HI 2 "register_operand" "0")))]
3086   ""
3087   "or.b\\t%s1,%t0"
3088   [(set_attr "length" "2")])
3089
3090 (define_insn "*iorhi3_lshiftrt_8"
3091   [(set (match_operand:HI 0 "register_operand" "=r")
3092         (ior:HI (lshiftrt:HI (match_operand:HI 1 "register_operand" "r")
3093                              (const_int 8))
3094                 (match_operand:HI 2 "register_operand" "0")))]
3095   ""
3096   "or.b\\t%t1,%s0"
3097   [(set_attr "length" "2")])
3098
3099 (define_insn "*iorhi3_two_qi"
3100   [(set (match_operand:HI 0 "register_operand" "=r")
3101         (ior:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "0"))
3102                 (ashift:HI (match_operand:HI 2 "register_operand" "r")
3103                            (const_int 8))))]
3104   ""
3105   "mov.b\\t%s2,%t0"
3106   [(set_attr "length" "2")])
3107
3108 (define_insn "*iorhi3_two_qi_mem"
3109   [(set (match_operand:HI 0 "register_operand" "=&r")
3110         (ior:HI (zero_extend:HI (match_operand:QI 1 "memory_operand" "m"))
3111                 (ashift:HI (subreg:HI (match_operand:QI 2 "memory_operand" "m") 0)
3112                            (const_int 8))))]
3113   ""
3114   "mov.b\\t%X2,%t0\;mov.b\\t%X1,%s0"
3115   [(set_attr "length" "16")])
3116
3117 (define_split
3118   [(set (match_operand:HI 0 "register_operand" "")
3119         (ior:HI (zero_extend:HI (match_operand:QI 1 "memory_operand" ""))
3120                 (ashift:HI (subreg:HI (match_operand:QI 2 "memory_operand" "") 0)
3121                            (const_int 8))))]
3122   "(TARGET_H8300H || TARGET_H8300S)
3123    && reload_completed
3124    && byte_accesses_mergeable_p (XEXP (operands[2], 0), XEXP (operands[1], 0))"
3125   [(set (match_dup 0)
3126         (match_dup 3))]
3127   "operands[3] = gen_rtx_MEM (HImode, XEXP (operands[2], 0));")
3128
3129 ;; ior:SI
3130
3131 (define_insn "*iorsi3_two_hi"
3132   [(set (match_operand:SI 0 "register_operand" "=r")
3133         (ior:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "0"))
3134                 (ashift:SI (match_operand:SI 2 "register_operand" "r")
3135                            (const_int 16))))]
3136   "TARGET_H8300H || TARGET_H8300S"
3137   "mov.w\\t%f2,%e0"
3138   [(set_attr "length" "2")])
3139
3140 (define_insn_and_split "*iorsi3_two_qi_zext"
3141   [(set (match_operand:SI 0 "register_operand" "=&r")
3142         (ior:SI (zero_extend:SI (match_operand:QI 1 "memory_operand" "m"))
3143
3144                 (and:SI (ashift:SI (subreg:SI (match_operand:QI 2 "memory_operand" "m") 0)
3145                                    (const_int 8))
3146                         (const_int 65280))))]
3147   "(TARGET_H8300H || TARGET_H8300S)"
3148   "#"
3149   "&& reload_completed"
3150   [(set (match_dup 3)
3151         (ior:HI (zero_extend:HI (match_dup 1))
3152                 (ashift:HI (subreg:HI (match_dup 2) 0)
3153                            (const_int 8))))
3154    (set (match_dup 0)
3155         (zero_extend:SI (match_dup 3)))]
3156   "operands[3] = gen_rtx_REG (HImode, REGNO (operands[0]));")
3157
3158 (define_insn "*iorsi3_e2f"
3159   [(set (match_operand:SI 0 "register_operand" "=r")
3160         (ior:SI (and:SI (match_operand:SI 1 "register_operand" "0")
3161                         (const_int -65536))
3162                 (lshiftrt:SI (match_operand:SI 2 "register_operand" "r")
3163                              (const_int 16))))]
3164   "TARGET_H8300H || TARGET_H8300S"
3165   "mov.w\\t%e2,%f0"
3166   [(set_attr "length" "2")])
3167
3168 (define_insn_and_split "*iorsi3_two_qi_sext"
3169   [(set (match_operand:SI 0 "register_operand" "=r")
3170         (ior:SI (zero_extend:SI (match_operand:QI 1 "register_operand" "0"))
3171                 (ashift:SI (sign_extend:SI (match_operand:QI 2 "register_operand" "r"))
3172                            (const_int 8))))]
3173   "(TARGET_H8300H || TARGET_H8300S)"
3174   "#"
3175   "&& reload_completed"
3176   [(set (match_dup 3)
3177         (ior:HI (zero_extend:HI (match_dup 1))
3178                 (ashift:HI (match_dup 4)
3179                            (const_int 8))))
3180    (set (match_dup 0)
3181         (sign_extend:SI (match_dup 3)))]
3182   "operands[3] = gen_rtx_REG (HImode, REGNO (operands[0]));
3183    operands[4] = gen_rtx_REG (HImode, REGNO (operands[2]));")
3184
3185 (define_insn "*iorsi3_w"
3186   [(set (match_operand:SI 0 "register_operand" "=r,&r")
3187         (ior:SI (and:SI (match_operand:SI 1 "register_operand" "0,0")
3188                         (const_int -256))
3189                 (zero_extend:SI (match_operand:QI 2 "general_operand_src" "r,g>"))))]
3190   "TARGET_H8300H || TARGET_H8300S"
3191   "mov.b\\t%X2,%w0"
3192   [(set_attr "length" "2,8")])
3193
3194 (define_insn "*iorsi3_ashift_31"
3195   [(set (match_operand:SI 0 "register_operand" "=&r")
3196         (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
3197                            (const_int 31))
3198                 (match_operand:SI 2 "register_operand" "0")))]
3199   "TARGET_H8300H || TARGET_H8300S"
3200   "rotxl.l\\t%S0\;bor\\t#0,%w1\;rotxr.l\\t%S0"
3201   [(set_attr "length" "6")
3202    (set_attr "cc" "set_znv")])
3203
3204 (define_insn "*iorsi3_and_ashift"
3205   [(set (match_operand:SI 0 "register_operand" "=r")
3206         (ior:SI (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
3207                                    (match_operand:SI 2 "const_int_operand" "n"))
3208                         (match_operand:SI 3 "single_one_operand" "n"))
3209                 (match_operand:SI 4 "register_operand" "0")))]
3210   "(TARGET_H8300H || TARGET_H8300S)
3211    && (INTVAL (operands[3]) & ~0xffff) == 0"
3212   "*
3213 {
3214   rtx srcpos = GEN_INT (exact_log2 (INTVAL (operands[3]))
3215                         - INTVAL (operands[2]));
3216   rtx dstpos = GEN_INT (exact_log2 (INTVAL (operands[3])));
3217   operands[2] = srcpos;
3218   operands[3] = dstpos;
3219   return \"bld\\t%Z2,%Y1\;bor\\t%Z3,%Y0\;bst\\t%Z3,%Y0\";
3220 }"
3221   [(set_attr "length" "6")])
3222
3223 (define_insn "*iorsi3_and_lshiftrt"
3224   [(set (match_operand:SI 0 "register_operand" "=r")
3225         (ior:SI (and:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
3226                                      (match_operand:SI 2 "const_int_operand" "n"))
3227                         (match_operand:SI 3 "single_one_operand" "n"))
3228                 (match_operand:SI 4 "register_operand" "0")))]
3229   "(TARGET_H8300H || TARGET_H8300S)
3230    && ((INTVAL (operands[3]) << INTVAL (operands[2])) & ~0xffff) == 0"
3231   "*
3232 {
3233   rtx srcpos = GEN_INT (exact_log2 (INTVAL (operands[3]))
3234                         + INTVAL (operands[2]));
3235   rtx dstpos = GEN_INT (exact_log2 (INTVAL (operands[3])));
3236   operands[2] = srcpos;
3237   operands[3] = dstpos;
3238   return \"bld\\t%Z2,%Y1\;bor\\t%Z3,%Y0\;bst\\t%Z3,%Y0\";
3239 }"
3240   [(set_attr "length" "6")])
3241
3242 (define_insn "*iorsi3_zero_extract"
3243   [(set (match_operand:SI 0 "register_operand" "=r")
3244         (ior:SI (zero_extract:SI (match_operand:SI 1 "register_operand" "r")
3245                                  (const_int 1)
3246                                  (match_operand:SI 2 "const_int_operand" "n"))
3247                 (match_operand:SI 3 "register_operand" "0")))]
3248   "(TARGET_H8300H || TARGET_H8300S)
3249    && INTVAL (operands[2]) < 16"
3250   "bld\\t%Z2,%Y1\;bor\\t#0,%w0\;bst\\t#0,%w0"
3251   [(set_attr "length" "6")])
3252
3253 (define_insn "*iorsi3_and_lshiftrt_n_sb"
3254   [(set (match_operand:SI 0 "register_operand" "=r")
3255         (ior:SI (and:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
3256                                      (const_int 30))
3257                         (const_int 2))
3258                 (match_operand:SI 2 "register_operand" "0")))]
3259   "(TARGET_H8300H || TARGET_H8300S)"
3260   "rotl.l\\t%S1\;rotr.l\\t%S1\;bor\\t#1,%w0\;bst\\t#1,%w0"
3261   [(set_attr "length" "8")])
3262
3263 (define_insn "*iorsi3_and_lshiftrt_9_sb"
3264   [(set (match_operand:SI 0 "register_operand" "=r")
3265         (ior:SI (and:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
3266                                      (const_int 9))
3267                         (const_int 4194304))
3268                 (match_operand:SI 2 "register_operand" "0")))
3269    (clobber (match_scratch:HI 3 "=&r"))]
3270   "(TARGET_H8300H || TARGET_H8300S)"
3271   "*
3272 {
3273   if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
3274     return \"shll.l\\t%S1\;xor.w\\t%T3,%T3\;bst\\t#6,%s3\;or.w\\t%T3,%e0\";
3275   else
3276     return \"rotl.l\\t%S1\;rotr.l\\t%S1\;xor.w\\t%T3,%T3\;bst\\t#6,%s3\;or.w\\t%T3,%e0\";
3277 }"
3278   [(set_attr "length" "10")])
3279
3280 ;; Used to OR the exponent of a float.
3281
3282 (define_insn "*iorsi3_shift"
3283   [(set (match_operand:SI 0 "register_operand" "=r")
3284         (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
3285                            (const_int 23))
3286                 (match_operand:SI 2 "register_operand" "0")))
3287    (clobber (match_scratch:SI 3 "=&r"))]
3288   "TARGET_H8300H || TARGET_H8300S"
3289   "#")
3290
3291 (define_split
3292   [(set (match_operand:SI 0 "register_operand" "")
3293         (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "")
3294                            (const_int 23))
3295                 (match_dup 0)))
3296    (clobber (match_operand:SI 2 "register_operand" ""))]
3297   "(TARGET_H8300H || TARGET_H8300S)
3298    && flow2_completed
3299    && find_regno_note (insn, REG_DEAD, REGNO (operands[1]))
3300    && REGNO (operands[0]) != REGNO (operands[1])"
3301   [(parallel [(set (match_dup 3)
3302                    (ashift:HI (match_dup 3)
3303                               (const_int 7)))
3304               (clobber (scratch:QI))])
3305    (set (match_dup 0)
3306         (ior:SI (ashift:SI (match_dup 1)
3307                            (const_int 16))
3308                 (match_dup 0)))]
3309   "operands[3] = gen_rtx_REG (HImode, REGNO (operands[1]));")
3310
3311 (define_split
3312   [(set (match_operand:SI 0 "register_operand" "")
3313         (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "")
3314                            (const_int 23))
3315                 (match_dup 0)))
3316    (clobber (match_operand:SI 2 "register_operand" ""))]
3317   "(TARGET_H8300H || TARGET_H8300S)
3318    && flow2_completed
3319    && !(find_regno_note (insn, REG_DEAD, REGNO (operands[1]))
3320         && REGNO (operands[0]) != REGNO (operands[1]))"
3321   [(set (match_dup 2)
3322         (match_dup 1))
3323    (parallel [(set (match_dup 3)
3324                    (ashift:HI (match_dup 3)
3325                               (const_int 7)))
3326               (clobber (scratch:QI))])
3327    (set (match_dup 0)
3328         (ior:SI (ashift:SI (match_dup 2)
3329                            (const_int 16))
3330                 (match_dup 0)))]
3331   "operands[3] = gen_rtx_REG (HImode, REGNO (operands[2]));")
3332
3333 (define_insn "*iorsi2_and_1_lshiftrt_1"
3334   [(set (match_operand:SI 0 "register_operand" "=r")
3335         (ior:SI (and:SI (match_operand:SI 1 "register_operand" "0")
3336                         (const_int 1))
3337                 (lshiftrt:SI (match_dup 1)
3338                              (const_int 1))))]
3339   "TARGET_H8300H || TARGET_H8300S"
3340   "shlr.l\\t%S0\;bor\\t#0,%w0\;bst\\t#0,%w0"
3341   [(set_attr "length" "6")])
3342
3343 (define_insn_and_split "*iorsi3_ashift_16_ashift_24"
3344   [(set (match_operand:SI 0 "register_operand" "=r")
3345         (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "0")
3346                            (const_int 16))
3347                 (ashift:SI (match_operand:SI 2 "register_operand" "r")
3348                            (const_int 24))))]
3349   "(TARGET_H8300H || TARGET_H8300S)"
3350   "#"
3351   "&& reload_completed"
3352   [(set (match_dup 3)
3353         (ior:HI (ashift:HI (match_dup 4)
3354                            (const_int 8))
3355                 (match_dup 3)))
3356    (parallel [(set (match_dup 0)
3357                    (ashift:SI (match_dup 0)
3358                               (const_int 16)))
3359               (clobber (scratch:QI))])]
3360   "operands[3] = gen_rtx_REG (HImode, REGNO (operands[0]));
3361    operands[4] = gen_rtx_REG (HImode, REGNO (operands[2]));")
3362
3363 (define_insn_and_split "*iorsi3_ashift_16_ashift_24_mem"
3364   [(set (match_operand:SI 0 "register_operand" "=&r")
3365         (ior:SI (and:SI (ashift:SI (subreg:SI (match_operand:QI 1 "memory_operand" "m") 0)
3366                                    (const_int 16))
3367                         (const_int 16711680))
3368                 (ashift:SI (subreg:SI (match_operand:QI 2 "memory_operand" "m") 0)
3369                            (const_int 24))))]
3370   "(TARGET_H8300H || TARGET_H8300S)"
3371   "#"
3372   "&& reload_completed"
3373   [(set (match_dup 3)
3374         (ior:HI (zero_extend:HI (match_dup 1))
3375                 (ashift:HI (subreg:HI (match_dup 2) 0)
3376                            (const_int 8))))
3377    (parallel [(set (match_dup 0)
3378                    (ashift:SI (match_dup 0)
3379                               (const_int 16)))
3380               (clobber (scratch:QI))])]
3381   "operands[3] = gen_rtx_REG (HImode, REGNO (operands[0]));")
3382
3383 ;; Used to add the exponent of a float.
3384
3385 (define_insn "*addsi3_shift"
3386   [(set (match_operand:SI 0 "register_operand" "=r")
3387         (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "r")
3388                           (const_int 8388608))
3389                  (match_operand:SI 2 "register_operand" "0")))
3390    (clobber (match_scratch:SI 3 "=&r"))]
3391   "TARGET_H8300H || TARGET_H8300S"
3392   "#")
3393
3394 (define_split
3395   [(set (match_operand:SI 0 "register_operand" "")
3396         (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "")
3397                           (const_int 8388608))
3398                  (match_dup 0)))
3399    (clobber (match_operand:SI 2 "register_operand" ""))]
3400   "(TARGET_H8300H || TARGET_H8300S)
3401    && flow2_completed
3402    && find_regno_note (insn, REG_DEAD, REGNO (operands[1]))
3403    && REGNO (operands[0]) != REGNO (operands[1])"
3404   [(parallel [(set (match_dup 3)
3405                    (ashift:HI (match_dup 3)
3406                               (const_int 7)))
3407               (clobber (scratch:QI))])
3408    (set (match_dup 0)
3409         (plus:SI (mult:SI (match_dup 1)
3410                           (const_int 65536))
3411                  (match_dup 0)))]
3412   "operands[3] = gen_rtx_REG (HImode, REGNO (operands[1]));")
3413
3414 (define_split
3415   [(set (match_operand:SI 0 "register_operand" "")
3416         (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "")
3417                           (const_int 8388608))
3418                  (match_dup 0)))
3419    (clobber (match_operand:SI 2 "register_operand" ""))]
3420   "(TARGET_H8300H || TARGET_H8300S)
3421    && flow2_completed
3422    && !(find_regno_note (insn, REG_DEAD, REGNO (operands[1]))
3423         && REGNO (operands[0]) != REGNO (operands[1]))"
3424   [(set (match_dup 2)
3425         (match_dup 1))
3426    (parallel [(set (match_dup 3)
3427                    (ashift:HI (match_dup 3)
3428                               (const_int 7)))
3429               (clobber (scratch:QI))])
3430    (set (match_dup 0)
3431         (plus:SI (mult:SI (match_dup 2)
3432                           (const_int 65536))
3433                  (match_dup 0)))]
3434   "operands[3] = gen_rtx_REG (HImode, REGNO (operands[2]));")
3435
3436 ;; ashift:SI
3437
3438 (define_insn_and_split "*ashiftsi_sextqi_7"
3439   [(set (match_operand:SI 0 "register_operand" "=r")
3440         (ashift:SI (sign_extend:SI (match_operand:QI 1 "register_operand" "0"))
3441                    (const_int 7)))]
3442   "(TARGET_H8300H || TARGET_H8300S)"
3443   "#"
3444   "&& reload_completed"
3445   [(parallel [(set (match_dup 2)
3446                    (ashift:HI (match_dup 2)
3447                               (const_int 8)))
3448               (clobber (scratch:QI))])
3449    (set (match_dup 0)
3450         (sign_extend:SI (match_dup 2)))
3451    (parallel [(set (match_dup 0)
3452                    (ashiftrt:SI (match_dup 0)
3453                                 (const_int 1)))
3454               (clobber (scratch:QI))])]
3455   "operands[2] = gen_rtx_REG (HImode, REGNO (operands[0]));")
3456
3457 ;; Storing a part of HImode to QImode.
3458
3459 (define_insn ""
3460   [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
3461         (subreg:QI (lshiftrt:HI (match_operand:HI 1 "register_operand" "r")
3462                                 (const_int 8)) 1))]
3463   ""
3464   "mov.b\\t%t1,%R0"
3465   [(set_attr "cc" "set_znv")
3466    (set_attr "length" "8")])
3467
3468 ;; Storing a part of SImode to QImode.
3469
3470 (define_insn ""
3471   [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
3472         (subreg:QI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
3473                                 (const_int 8)) 3))]
3474   ""
3475   "mov.b\\t%x1,%R0"
3476   [(set_attr "cc" "set_znv")
3477    (set_attr "length" "8")])
3478
3479 (define_insn ""
3480   [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
3481         (subreg:QI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
3482                                 (const_int 16)) 3))
3483    (clobber (match_scratch:SI 2 "=&r"))]
3484   "TARGET_H8300H || TARGET_H8300S"
3485   "mov.w\\t%e1,%f2\;mov.b\\t%w2,%R0"
3486   [(set_attr "cc" "set_znv")
3487    (set_attr "length" "10")])
3488
3489 (define_insn ""
3490   [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
3491         (subreg:QI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
3492                                 (const_int 24)) 3))
3493    (clobber (match_scratch:SI 2 "=&r"))]
3494   "TARGET_H8300H || TARGET_H8300S"
3495   "mov.w\\t%e1,%f2\;mov.b\\t%x2,%R0"
3496   [(set_attr "cc" "set_znv")
3497    (set_attr "length" "10")])
3498
3499 (define_insn_and_split ""
3500   [(set (pc)
3501         (if_then_else (eq (zero_extract:SI (subreg:SI (match_operand:QI 0 "register_operand" "") 0)
3502                                            (const_int 1)
3503                                            (const_int 7))
3504                           (const_int 0))
3505                       (label_ref (match_operand 1 "" ""))
3506                       (pc)))]
3507   ""
3508   "#"
3509   ""
3510   [(set (cc0)
3511         (match_dup 0))
3512    (set (pc)
3513         (if_then_else (ge (cc0)
3514                           (const_int 0))
3515                       (label_ref (match_dup 1))
3516                       (pc)))]
3517   "")
3518
3519 (define_insn_and_split ""
3520   [(set (pc)
3521         (if_then_else (ne (zero_extract:SI (subreg:SI (match_operand:QI 0 "register_operand" "") 0)
3522                                            (const_int 1)
3523                                            (const_int 7))
3524                           (const_int 0))
3525                       (label_ref (match_operand 1 "" ""))
3526                       (pc)))]
3527   ""
3528   "#"
3529   ""
3530   [(set (cc0)
3531         (match_dup 0))
3532    (set (pc)
3533         (if_then_else (lt (cc0)
3534                           (const_int 0))
3535                       (label_ref (match_dup 1))
3536                       (pc)))]
3537   "")
3538 \f
3539 ;; -----------------------------------------------------------------
3540 ;; PEEPHOLE PATTERNS
3541 ;; -----------------------------------------------------------------
3542
3543 ;; Convert (A >> B) & C to (A & 255) >> B if C == 255 >> B.
3544
3545 (define_peephole2
3546   [(parallel
3547      [(set (match_operand:HI 0 "register_operand" "")
3548            (lshiftrt:HI (match_dup 0)
3549                         (match_operand:HI 1 "const_int_operand" "")))
3550       (clobber (match_operand:HI 2 "" ""))])
3551    (set (match_dup 0)
3552         (and:HI (match_dup 0)
3553                 (match_operand:HI 3 "const_int_operand" "")))]
3554   "INTVAL (operands[3]) == (255 >> INTVAL (operands[1]))"
3555   [(set (match_dup 0)
3556         (and:HI (match_dup 0)
3557                 (const_int 255)))
3558    (parallel
3559      [(set (match_dup 0)
3560            (lshiftrt:HI (match_dup 0)
3561                         (match_dup 1)))
3562       (clobber (match_dup 2))])]
3563   "")
3564
3565 ;; Convert (A << B) & C to (A & 255) << B if C == 255 << B.
3566
3567 (define_peephole2
3568   [(parallel
3569      [(set (match_operand:HI 0 "register_operand" "")
3570            (ashift:HI (match_dup 0)
3571                       (match_operand:HI 1 "const_int_operand" "")))
3572       (clobber (match_operand:HI 2 "" ""))])
3573    (set (match_dup 0)
3574         (and:HI (match_dup 0)
3575                 (match_operand:HI 3 "const_int_operand" "")))]
3576   "INTVAL (operands[3]) == (255 << INTVAL (operands[1]))"
3577   [(set (match_dup 0)
3578         (and:HI (match_dup 0)
3579                 (const_int 255)))
3580    (parallel
3581      [(set (match_dup 0)
3582            (ashift:HI (match_dup 0)
3583                       (match_dup 1)))
3584       (clobber (match_dup 2))])]
3585   "")
3586
3587 ;; Convert (A >> B) & C to (A & 255) >> B if C == 255 >> B.
3588
3589 (define_peephole2
3590   [(parallel
3591      [(set (match_operand:SI 0 "register_operand" "")
3592            (lshiftrt:SI (match_dup 0)
3593                         (match_operand:SI 1 "const_int_operand" "")))
3594       (clobber (match_operand:SI 2 "" ""))])
3595    (set (match_dup 0)
3596         (and:SI (match_dup 0)
3597                 (match_operand:SI 3 "const_int_operand" "")))]
3598   "INTVAL (operands[3]) == (255 >> INTVAL (operands[1]))"
3599   [(set (match_dup 0)
3600         (and:SI (match_dup 0)
3601                 (const_int 255)))
3602    (parallel
3603      [(set (match_dup 0)
3604            (lshiftrt:SI (match_dup 0)
3605                         (match_dup 1)))
3606       (clobber (match_dup 2))])]
3607   "")
3608
3609 ;; Convert (A << B) & C to (A & 255) << B if C == 255 << B.
3610
3611 (define_peephole2
3612   [(parallel
3613      [(set (match_operand:SI 0 "register_operand" "")
3614            (ashift:SI (match_dup 0)
3615                       (match_operand:SI 1 "const_int_operand" "")))
3616       (clobber (match_operand:SI 2 "" ""))])
3617    (set (match_dup 0)
3618         (and:SI (match_dup 0)
3619                 (match_operand:SI 3 "const_int_operand" "")))]
3620   "INTVAL (operands[3]) == (255 << INTVAL (operands[1]))"
3621   [(set (match_dup 0)
3622         (and:SI (match_dup 0)
3623                 (const_int 255)))
3624    (parallel
3625      [(set (match_dup 0)
3626            (ashift:SI (match_dup 0)
3627                       (match_dup 1)))
3628       (clobber (match_dup 2))])]
3629   "")
3630
3631 ;; Convert (A >> B) & C to (A & 65535) >> B if C == 65535 >> B.
3632
3633 (define_peephole2
3634   [(parallel
3635      [(set (match_operand:SI 0 "register_operand" "")
3636            (lshiftrt:SI (match_dup 0)
3637                         (match_operand:SI 1 "const_int_operand" "")))
3638       (clobber (match_operand:SI 2 "" ""))])
3639    (set (match_dup 0)
3640         (and:SI (match_dup 0)
3641                 (match_operand:SI 3 "const_int_operand" "")))]
3642   "INTVAL (operands[3]) == (65535 >> INTVAL (operands[1]))"
3643   [(set (match_dup 0)
3644         (and:SI (match_dup 0)
3645                 (const_int 65535)))
3646    (parallel
3647      [(set (match_dup 0)
3648            (lshiftrt:SI (match_dup 0)
3649                         (match_dup 1)))
3650       (clobber (match_dup 2))])]
3651   "")
3652
3653 ;; Convert (A << B) & C to (A & 65535) << B if C == 65535 << B.
3654
3655 (define_peephole2
3656   [(parallel
3657      [(set (match_operand:SI 0 "register_operand" "")
3658            (ashift:SI (match_dup 0)
3659                       (match_operand:SI 1 "const_int_operand" "")))
3660       (clobber (match_operand:SI 2 "" ""))])
3661    (set (match_dup 0)
3662         (and:SI (match_dup 0)
3663                 (match_operand:SI 3 "const_int_operand" "")))]
3664   "INTVAL (operands[3]) == (65535 << INTVAL (operands[1]))"
3665   [(set (match_dup 0)
3666         (and:SI (match_dup 0)
3667                 (const_int 65535)))
3668    (parallel
3669      [(set (match_dup 0)
3670            (ashift:SI (match_dup 0)
3671                       (match_dup 1)))
3672       (clobber (match_dup 2))])]
3673   "")
3674
3675 ;; Convert a QImode push into an SImode push so that the
3676 ;; define_peephole2 below can cram multiple pushes into one stm.l.
3677
3678 (define_peephole2
3679   [(parallel [(set (reg:SI SP_REG)
3680                    (plus:SI (reg:SI SP_REG) (const_int -4)))
3681               (set (mem:QI (plus:SI (reg:SI SP_REG) (const_int -3)))
3682                    (match_operand:QI 0 "register_operand" ""))])]
3683   "TARGET_H8300S && !TARGET_NORMAL_MODE"
3684   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3685         (match_dup 0))]
3686   "operands[0] = gen_rtx_REG (SImode, REGNO (operands[0]));")
3687
3688 (define_peephole2
3689   [(parallel [(set (reg:HI SP_REG)
3690                    (plus:HI (reg:HI SP_REG) (const_int -4)))
3691               (set (mem:QI (plus:HI (reg:HI SP_REG) (const_int -3)))
3692                    (match_operand:QI 0 "register_operand" ""))])]
3693   "TARGET_H8300S && TARGET_NORMAL_MODE"
3694   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3695         (match_dup 0))]
3696   "operands[0] = gen_rtx_REG (SImode, REGNO (operands[0]));")
3697
3698 ;; Convert a HImode push into an SImode push so that the
3699 ;; define_peephole2 below can cram multiple pushes into one stm.l.
3700
3701 (define_peephole2
3702   [(parallel [(set (reg:SI SP_REG)
3703                    (plus:SI (reg:SI SP_REG) (const_int -4)))
3704               (set (mem:HI (plus:SI (reg:SI SP_REG) (const_int -2)))
3705                    (match_operand:HI 0 "register_operand" ""))])]
3706   "TARGET_H8300S && !TARGET_NORMAL_MODE"
3707   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3708         (match_dup 0))]
3709   "operands[0] = gen_rtx_REG (SImode, REGNO (operands[0]));")
3710
3711 (define_peephole2
3712   [(parallel [(set (reg:HI SP_REG)
3713                    (plus:HI (reg:HI SP_REG) (const_int -4)))
3714               (set (mem:HI (plus:HI (reg:HI SP_REG) (const_int -2)))
3715                    (match_operand:HI 0 "register_operand" ""))])]
3716   "TARGET_H8300S && TARGET_NORMAL_MODE"
3717   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3718         (match_dup 0))]
3719   "operands[0] = gen_rtx_REG (SImode, REGNO (operands[0]));")
3720
3721 ;; Cram four pushes into stm.l.
3722
3723 (define_peephole2
3724   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3725         (match_operand:SI 0 "register_operand" ""))
3726    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3727         (match_operand:SI 1 "register_operand" ""))
3728    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3729         (match_operand:SI 2 "register_operand" ""))
3730    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3731         (match_operand:SI 3 "register_operand" ""))]
3732   "TARGET_H8300S && !TARGET_NORMAL_MODE
3733    && h8300_regs_ok_for_stm (4, operands)"
3734   [(parallel [(set (reg:SI SP_REG)
3735                    (plus:SI (reg:SI SP_REG)
3736                             (const_int -16)))
3737               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -4)))
3738                    (match_dup 0))
3739               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -8)))
3740                    (match_dup 1))
3741               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -12)))
3742                    (match_dup 2))
3743               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -16)))
3744                    (match_dup 3))])]
3745   "")
3746
3747 (define_peephole2
3748   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3749         (match_operand:SI 0 "register_operand" ""))
3750    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3751         (match_operand:SI 1 "register_operand" ""))
3752    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3753         (match_operand:SI 2 "register_operand" ""))
3754    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3755         (match_operand:SI 3 "register_operand" ""))]
3756   "TARGET_H8300S && TARGET_NORMAL_MODE
3757    && h8300_regs_ok_for_stm (4, operands)"
3758   [(parallel [(set (reg:HI SP_REG)
3759                    (plus:HI (reg:HI SP_REG)
3760                             (const_int -16)))
3761               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -4)))
3762                    (match_dup 0))
3763               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -8)))
3764                    (match_dup 1))
3765               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -12)))
3766                    (match_dup 2))
3767               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -16)))
3768                    (match_dup 3))])]
3769   "")
3770
3771 ;; Cram three pushes into stm.l.
3772
3773 (define_peephole2
3774   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3775         (match_operand:SI 0 "register_operand" ""))
3776    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3777         (match_operand:SI 1 "register_operand" ""))
3778    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3779         (match_operand:SI 2 "register_operand" ""))]
3780   "TARGET_H8300S && !TARGET_NORMAL_MODE
3781    && h8300_regs_ok_for_stm (3, operands)"
3782   [(parallel [(set (reg:SI SP_REG)
3783                    (plus:SI (reg:SI SP_REG)
3784                             (const_int -12)))
3785               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -4)))
3786                    (match_dup 0))
3787               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -8)))
3788                    (match_dup 1))
3789               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -12)))
3790                    (match_dup 2))])]
3791   "")
3792
3793 (define_peephole2
3794   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3795         (match_operand:SI 0 "register_operand" ""))
3796    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3797         (match_operand:SI 1 "register_operand" ""))
3798    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3799         (match_operand:SI 2 "register_operand" ""))]
3800   "TARGET_H8300S && TARGET_NORMAL_MODE
3801    && h8300_regs_ok_for_stm (3, operands)"
3802   [(parallel [(set (reg:HI SP_REG)
3803                    (plus:HI (reg:HI SP_REG)
3804                             (const_int -12)))
3805               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -4)))
3806                    (match_dup 0))
3807               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -8)))
3808                    (match_dup 1))
3809               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -12)))
3810                    (match_dup 2))])]
3811   "")
3812
3813 ;; Cram two pushes into stm.l.
3814
3815 (define_peephole2
3816   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3817         (match_operand:SI 0 "register_operand" ""))
3818    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
3819         (match_operand:SI 1 "register_operand" ""))]
3820   "TARGET_H8300S && !TARGET_NORMAL_MODE
3821    && h8300_regs_ok_for_stm (2, operands)"
3822   [(parallel [(set (reg:SI SP_REG)
3823                    (plus:SI (reg:SI SP_REG)
3824                             (const_int -8)))
3825               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -4)))
3826                    (match_dup 0))
3827               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -8)))
3828                    (match_dup 1))])]
3829   "")
3830
3831 (define_peephole2
3832   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3833         (match_operand:SI 0 "register_operand" ""))
3834    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
3835         (match_operand:SI 1 "register_operand" ""))]
3836   "TARGET_H8300S && TARGET_NORMAL_MODE
3837    && h8300_regs_ok_for_stm (2, operands)"
3838   [(parallel [(set (reg:HI SP_REG)
3839                    (plus:HI (reg:HI SP_REG)
3840                             (const_int -8)))
3841               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -4)))
3842                    (match_dup 0))
3843               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -8)))
3844                    (match_dup 1))])]
3845   "")
3846
3847 ;; Turn
3848 ;;
3849 ;;   mov.w #2,r0
3850 ;;   add.w r7,r0  (6 bytes)
3851 ;;
3852 ;; into
3853 ;;
3854 ;;   mov.w r7,r0
3855 ;;   adds  #2,r0  (4 bytes)
3856
3857 (define_peephole2
3858   [(set (match_operand:HI 0 "register_operand" "")
3859         (match_operand:HI 1 "const_int_operand" ""))
3860    (set (match_dup 0)
3861         (plus:HI (match_dup 0)
3862                  (match_operand:HI 2 "register_operand" "")))]
3863   "REG_P (operands[0]) && REG_P (operands[2])
3864    && REGNO (operands[0]) != REGNO (operands[2])
3865    && (CONST_OK_FOR_J (INTVAL (operands[1]))
3866        || CONST_OK_FOR_L (INTVAL (operands[1]))
3867        || CONST_OK_FOR_N (INTVAL (operands[1])))"
3868   [(set (match_dup 0)
3869         (match_dup 2))
3870    (set (match_dup 0)
3871         (plus:HI (match_dup 0)
3872                  (match_dup 1)))]
3873   "")
3874
3875 ;; Turn
3876 ;;
3877 ;;   sub.l  er0,er0
3878 ;;   add.b  #4,r0l
3879 ;;   add.l  er7,er0  (6 bytes)
3880 ;;
3881 ;; into
3882 ;;
3883 ;;   mov.l  er7,er0
3884 ;;   adds   #4,er0   (4 bytes)
3885
3886 (define_peephole2
3887   [(set (match_operand:SI 0 "register_operand" "")
3888         (match_operand:SI 1 "const_int_operand" ""))
3889    (set (match_dup 0)
3890         (plus:SI (match_dup 0)
3891                  (match_operand:SI 2 "register_operand" "")))]
3892   "(TARGET_H8300H || TARGET_H8300S)
3893    && REG_P (operands[0]) && REG_P (operands[2])
3894    && REGNO (operands[0]) != REGNO (operands[2])
3895    && (CONST_OK_FOR_L (INTVAL (operands[1]))
3896        || CONST_OK_FOR_N (INTVAL (operands[1])))"
3897   [(set (match_dup 0)
3898         (match_dup 2))
3899    (set (match_dup 0)
3900         (plus:SI (match_dup 0)
3901                  (match_dup 1)))]
3902   "")
3903
3904 ;; Turn
3905 ;;
3906 ;;   mov.l er7,er0
3907 ;;   add.l #10,er0  (takes 8 bytes)
3908 ;;
3909 ;; into
3910 ;;
3911 ;;   sub.l er0,er0
3912 ;;   add.b #10,r0l
3913 ;;   add.l er7,er0  (takes 6 bytes)
3914
3915 (define_peephole2
3916   [(set (match_operand:SI 0 "register_operand" "")
3917         (match_operand:SI 1 "register_operand" ""))
3918    (set (match_dup 0)
3919         (plus:SI (match_dup 0)
3920                  (match_operand:SI 2 "const_int_operand" "")))]
3921   "(TARGET_H8300H || TARGET_H8300S)
3922    && REG_P (operands[0]) && REG_P (operands[1])
3923    && REGNO (operands[0]) != REGNO (operands[1])
3924    && !CONST_OK_FOR_L (INTVAL (operands[2]))
3925    && !CONST_OK_FOR_N (INTVAL (operands[2]))
3926    && ((INTVAL (operands[2]) & 0xff) == INTVAL (operands[2])
3927        || (INTVAL (operands[2]) & 0xff00) == INTVAL (operands[2])
3928        || INTVAL (operands[2]) == 0xffff
3929        || INTVAL (operands[2]) == 0xfffe)"
3930   [(set (match_dup 0)
3931         (match_dup 2))
3932    (set (match_dup 0)
3933         (plus:SI (match_dup 0)
3934                  (match_dup 1)))]
3935   "")
3936
3937 ;; Turn
3938 ;;
3939 ;;   subs   #1,er4
3940 ;;   mov.w  r4,r4
3941 ;;   bne    .L2028
3942 ;;
3943 ;; into
3944 ;;
3945 ;;   dec.w  #1,r4
3946 ;;   bne    .L2028
3947
3948 (define_peephole2
3949   [(set (match_operand:HI 0 "register_operand" "")
3950         (plus:HI (match_dup 0)
3951                  (match_operand 1 "incdec_operand" "")))
3952    (set (cc0)
3953         (match_dup 0))
3954    (set (pc)
3955         (if_then_else (match_operator 3 "eqne_operator"
3956                         [(cc0) (const_int 0)])
3957                       (label_ref (match_operand 2 "" ""))
3958                       (pc)))]
3959   "TARGET_H8300H || TARGET_H8300S"
3960   [(set (match_operand:HI 0 "register_operand" "")
3961         (unspec:HI [(match_dup 0)
3962                     (match_dup 1)]
3963                    UNSPEC_INCDEC))
3964    (set (cc0)
3965         (match_dup 0))
3966    (set (pc)
3967         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
3968                       (label_ref (match_dup 2))
3969                       (pc)))]
3970   "")
3971
3972 ;; The SImode version of the previous pattern.
3973
3974 (define_peephole2
3975   [(set (match_operand:SI 0 "register_operand" "")
3976         (plus:SI (match_dup 0)
3977                  (match_operand 1 "incdec_operand" "")))
3978    (set (cc0)
3979         (match_dup 0))
3980    (set (pc)
3981         (if_then_else (match_operator 3 "eqne_operator"
3982                         [(cc0) (const_int 0)])
3983                       (label_ref (match_operand 2 "" ""))
3984                       (pc)))]
3985   "TARGET_H8300H || TARGET_H8300S"
3986   [(set (match_operand:SI 0 "register_operand" "")
3987         (unspec:SI [(match_dup 0)
3988                     (match_dup 1)]
3989                    UNSPEC_INCDEC))
3990    (set (cc0)
3991         (match_dup 0))
3992    (set (pc)
3993         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
3994                       (label_ref (match_dup 2))
3995                       (pc)))]
3996   "")
3997
3998 (define_peephole2
3999   [(parallel [(set (cc0)
4000                    (zero_extract:SI (match_operand:QI 0 "register_operand" "")
4001                                     (const_int 1)
4002                                     (const_int 7)))
4003               (clobber (scratch:QI))])
4004    (set (pc)
4005         (if_then_else (match_operator 1 "eqne_operator"
4006                         [(cc0) (const_int 0)])
4007                       (label_ref (match_operand 2 "" ""))
4008                       (pc)))]
4009   "(TARGET_H8300H || TARGET_H8300S)"
4010   [(set (cc0)
4011         (match_dup 0))
4012    (set (pc)
4013         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4014                       (label_ref (match_dup 2))
4015                       (pc)))]
4016   "operands[3] = ((GET_CODE (operands[1]) == EQ)
4017                   ? gen_rtx_GE (VOIDmode, cc0_rtx, const0_rtx)
4018                   : gen_rtx_LT (VOIDmode, cc0_rtx, const0_rtx));")
4019
4020 ;; The next three peephole2's will try to transform
4021 ;;
4022 ;;   mov.b A,r0l    (or mov.l A,er0)
4023 ;;   and.l #CST,er0
4024 ;;
4025 ;; into
4026 ;;
4027 ;;   sub.l er0
4028 ;;   mov.b A,r0l
4029 ;;   and.b #CST,r0l (if CST is not 255)
4030
4031 (define_peephole2
4032   [(set (match_operand:QI 0 "register_operand" "")
4033         (match_operand:QI 1 "general_operand" ""))
4034    (set (match_operand:SI 2 "register_operand" "")
4035         (and:SI (match_dup 2)
4036                 (const_int 255)))]
4037   "(TARGET_H8300H || TARGET_H8300S)
4038    && !reg_overlap_mentioned_p (operands[2], operands[1])
4039    && REGNO (operands[0]) == REGNO (operands[2])"
4040   [(set (match_dup 2)
4041         (const_int 0))
4042    (set (strict_low_part (match_dup 0))
4043         (match_dup 1))]
4044   "")
4045
4046 (define_peephole2
4047   [(set (match_operand:SI 0 "register_operand" "")
4048         (match_operand:SI 1 "general_operand" ""))
4049    (set (match_dup 0)
4050         (and:SI (match_dup 0)
4051                 (const_int 255)))]
4052   "(TARGET_H8300H || TARGET_H8300S)
4053    && !reg_overlap_mentioned_p (operands[0], operands[1])
4054    && !(GET_CODE (operands[1]) == MEM && MEM_VOLATILE_P (operands[1]))"
4055   [(set (match_dup 0)
4056         (const_int 0))
4057    (set (strict_low_part (match_dup 2))
4058         (match_dup 3))]
4059   "operands[2] = gen_lowpart (QImode, operands[0]);
4060    operands[3] = gen_lowpart (QImode, operands[1]);")
4061
4062 (define_peephole2
4063   [(set (match_operand 0 "register_operand" "")
4064         (match_operand 1 "general_operand" ""))
4065    (set (match_operand:SI 2 "register_operand" "")
4066         (and:SI (match_dup 2)
4067                 (match_operand:SI 3 "const_int_qi_operand" "")))]
4068   "(TARGET_H8300H || TARGET_H8300S)
4069    && (GET_MODE (operands[0]) == QImode
4070        || GET_MODE (operands[0]) == HImode
4071        || GET_MODE (operands[0]) == SImode)
4072    && GET_MODE (operands[0]) == GET_MODE (operands[1])
4073    && REGNO (operands[0]) == REGNO (operands[2])
4074    && !reg_overlap_mentioned_p (operands[2], operands[1])
4075    && !(GET_MODE (operands[1]) != QImode
4076         && GET_CODE (operands[1]) == MEM
4077         && MEM_VOLATILE_P (operands[1]))"
4078   [(set (match_dup 2)
4079         (const_int 0))
4080    (set (strict_low_part (match_dup 4))
4081         (match_dup 5))
4082    (set (match_dup 2)
4083         (and:SI (match_dup 2)
4084                 (match_dup 6)))]
4085   "operands[4] = gen_lowpart (QImode, operands[0]);
4086    operands[5] = gen_lowpart (QImode, operands[1]);
4087    operands[6] = GEN_INT (~0xff | INTVAL (operands[3]));")
4088
4089 (define_peephole2
4090   [(set (match_operand:SI 0 "register_operand" "")
4091         (match_operand:SI 1 "register_operand" ""))
4092    (set (match_dup 0)
4093         (and:SI (match_dup 0)
4094                 (const_int 65280)))]
4095   "(TARGET_H8300H || TARGET_H8300S)
4096    && !reg_overlap_mentioned_p (operands[0], operands[1])"
4097   [(set (match_dup 0)
4098         (const_int 0))
4099    (set (zero_extract:SI (match_dup 0)
4100                          (const_int 8)
4101                          (const_int 8))
4102         (lshiftrt:SI (match_dup 1)
4103                      (const_int 8)))]
4104   "")
4105
4106 ;; If a load of mem:SI is followed by an AND that turns off the upper
4107 ;; half, then we can load mem:HI instead.
4108
4109 (define_peephole2
4110   [(set (match_operand:SI 0 "register_operand" "")
4111         (match_operand:SI 1 "memory_operand" ""))
4112    (set (match_dup 0)
4113         (and:SI (match_dup 0)
4114                 (match_operand:SI 2 "const_int_operand" "")))]
4115   "(TARGET_H8300H || TARGET_H8300S)
4116    && !MEM_VOLATILE_P (operands[1])
4117    && (INTVAL (operands[2]) & ~0xffff) == 0
4118    && INTVAL (operands[2]) != 255"
4119   [(set (match_dup 3)
4120         (match_dup 4))
4121    (set (match_dup 0)
4122         (and:SI (match_dup 0)
4123                 (match_dup 2)))]
4124   "operands[3] = gen_lowpart (HImode, operands[0]);
4125    operands[4] = gen_lowpart (HImode, operands[1]);")
4126
4127 ;; (compare (reg:HI) (const_int)) takes 4 bytes, so we try to achieve
4128 ;; the equivalent with shorter sequences.  Here is the summary.  Cases
4129 ;; are grouped for each define_peephole2.
4130 ;;
4131 ;; reg  const_int                   use     insn
4132 ;; --------------------------------------------------------
4133 ;; dead    -2                       eq/ne   inc.l
4134 ;; dead    -1                       eq/ne   inc.l
4135 ;; dead     1                       eq/ne   dec.l
4136 ;; dead     2                       eq/ne   dec.l
4137 ;;
4138 ;; dead     1                       geu/ltu shar.l
4139 ;; dead     3 (H8S)                 geu/ltu shar.l
4140 ;;
4141 ;; ----   255                       geu/ltu mov.b
4142
4143 ;; Transform
4144 ;;
4145 ;;      cmp.w   #1,r0
4146 ;;      bne     .L1
4147 ;;
4148 ;; into
4149 ;;
4150 ;;      dec.w   #1,r0
4151 ;;      bne     .L1
4152
4153 (define_peephole2
4154   [(set (cc0)
4155         (compare (match_operand:HI 0 "register_operand" "")
4156                  (match_operand:HI 1 "incdec_operand" "")))
4157    (set (pc)
4158         (if_then_else (match_operator 3 "eqne_operator"
4159                         [(cc0) (const_int 0)])
4160                       (label_ref (match_operand 2 "" ""))
4161                       (pc)))]
4162   "(TARGET_H8300H || TARGET_H8300S)
4163    && peep2_reg_dead_p (1, operands[0])"
4164   [(set (match_dup 0)
4165         (unspec:HI [(match_dup 0)
4166                     (match_dup 4)]
4167                    UNSPEC_INCDEC))
4168    (set (cc0)
4169         (match_dup 0))
4170    (set (pc)
4171         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4172                       (label_ref (match_dup 2))
4173                       (pc)))]
4174   "operands[4] = GEN_INT (- INTVAL (operands[1]));")
4175
4176 ;; Transform
4177 ;;
4178 ;;      cmp.w   #1,r0
4179 ;;      bgt     .L1
4180 ;;
4181 ;; into
4182 ;;
4183 ;;      shar.w  r0
4184 ;;      bgt     .L1
4185
4186 (define_peephole2
4187   [(set (cc0)
4188         (compare (match_operand:HI 0 "register_operand" "")
4189                  (match_operand:HI 1 "const_int_operand" "")))
4190    (set (pc)
4191         (if_then_else (match_operator 2 "gtle_operator"
4192                         [(cc0) (const_int 0)])
4193                       (label_ref (match_operand 3 "" ""))
4194                       (pc)))]
4195   "(TARGET_H8300H || TARGET_H8300S)
4196    && peep2_reg_dead_p (1, operands[0])
4197    && (INTVAL (operands[1]) == 1
4198         || (TARGET_H8300S && INTVAL (operands[1]) == 3))"
4199   [(parallel [(set (match_dup 0)
4200                    (ashiftrt:HI (match_dup 0)
4201                                 (match_dup 5)))
4202               (clobber (scratch:QI))])
4203    (set (cc0)
4204         (match_dup 0))
4205    (set (pc)
4206         (if_then_else (match_dup 4)
4207                       (label_ref (match_dup 3))
4208                       (pc)))]
4209   "switch (GET_CODE (operands[2]))
4210      {
4211      case GTU:
4212        operands[4] = gen_rtx_NE (VOIDmode, cc0_rtx, const0_rtx);
4213        break;
4214      case LEU:
4215        operands[4] = gen_rtx_EQ (VOIDmode, cc0_rtx, const0_rtx);
4216        break;
4217      default:
4218        operands[4] = operands[2];
4219        break;
4220      }
4221    operands[5] = GEN_INT (exact_log2 (INTVAL (operands[1]) + 1));")
4222
4223 ;; Transform
4224 ;;
4225 ;;      cmp.w   #255,r0
4226 ;;      bgt     .L1
4227 ;;
4228 ;; into
4229 ;;
4230 ;;      mov.b   r0h,r0h
4231 ;;      bgt     .L1
4232
4233 (define_peephole2
4234   [(set (cc0)
4235         (compare (match_operand:HI 0 "register_operand" "")
4236                  (const_int 255)))
4237    (set (pc)
4238         (if_then_else (match_operator 1 "gtle_operator"
4239                         [(cc0) (const_int 0)])
4240                       (label_ref (match_operand 2 "" ""))
4241                       (pc)))]
4242   "TARGET_H8300H || TARGET_H8300S"
4243   [(set (cc0)
4244         (and:HI (match_dup 0)
4245                 (const_int -256)))
4246    (set (pc)
4247         (if_then_else (match_dup 3)
4248                       (label_ref (match_dup 2))
4249                       (pc)))]
4250   "switch (GET_CODE (operands[1]))
4251      {
4252      case GTU:
4253        operands[3] = gen_rtx_NE (VOIDmode, cc0_rtx, const0_rtx);
4254        break;
4255      case LEU:
4256        operands[3] = gen_rtx_EQ (VOIDmode, cc0_rtx, const0_rtx);
4257        break;
4258      default:
4259        operands[3] = operands[1];
4260        break;
4261      }")
4262
4263 ;; (compare (reg:SI) (const_int)) takes 6 bytes, so we try to achieve
4264 ;; the equivalent with shorter sequences.  Here is the summary.  Cases
4265 ;; are grouped for each define_peephole2.
4266 ;;
4267 ;; reg  const_int                   use     insn
4268 ;; --------------------------------------------------------
4269 ;; live    -2                       eq/ne   copy and inc.l
4270 ;; live    -1                       eq/ne   copy and inc.l
4271 ;; live     1                       eq/ne   copy and dec.l
4272 ;; live     2                       eq/ne   copy and dec.l
4273 ;;
4274 ;; dead    -2                       eq/ne   inc.l
4275 ;; dead    -1                       eq/ne   inc.l
4276 ;; dead     1                       eq/ne   dec.l
4277 ;; dead     2                       eq/ne   dec.l
4278 ;;
4279 ;; dead -131072                     eq/ne   inc.w and test
4280 ;; dead  -65536                     eq/ne   inc.w and test
4281 ;; dead   65536                     eq/ne   dec.w and test
4282 ;; dead  131072                     eq/ne   dec.w and test
4283 ;;
4284 ;; dead 0x000000?? except 1 and 2   eq/ne   xor.b and test
4285 ;; dead 0x0000??00                  eq/ne   xor.b and test
4286 ;; dead 0x0000ffff                  eq/ne   not.w and test
4287 ;;
4288 ;; dead 0xffffff?? except -1 and -2 eq/ne   xor.b and not.l
4289 ;; dead 0xffff??ff                  eq/ne   xor.b and not.l
4290 ;; dead 0x40000000 (H8S)            eq/ne   rotl.l and dec.l
4291 ;; dead 0x80000000                  eq/ne   rotl.l and dec.l
4292 ;;
4293 ;; live     1                       geu/ltu copy and shar.l
4294 ;; live     3 (H8S)                 geu/ltu copy and shar.l
4295 ;;
4296 ;; dead     1                       geu/ltu shar.l
4297 ;; dead     3 (H8S)                 geu/ltu shar.l
4298 ;;
4299 ;; dead     3 (H8/300H)             geu/ltu and.b and test
4300 ;; dead     7                       geu/ltu and.b and test
4301 ;; dead    15                       geu/ltu and.b and test
4302 ;; dead    31                       geu/ltu and.b and test
4303 ;; dead    63                       geu/ltu and.b and test
4304 ;; dead   127                       geu/ltu and.b and test
4305 ;; dead   255                       geu/ltu and.b and test
4306 ;;
4307 ;; ---- 65535                       geu/ltu mov.w
4308
4309 ;; For a small constant, it is cheaper to actually do the subtraction
4310 ;; and then test the register.
4311
4312 (define_peephole2
4313   [(set (cc0)
4314         (compare (match_operand:SI 0 "register_operand" "")
4315                  (match_operand:SI 1 "incdec_operand" "")))
4316    (set (pc)
4317         (if_then_else (match_operator 3 "eqne_operator"
4318                         [(cc0) (const_int 0)])
4319                       (label_ref (match_operand 2 "" ""))
4320                       (pc)))]
4321   "(TARGET_H8300H || TARGET_H8300S)
4322    && peep2_reg_dead_p (1, operands[0])"
4323   [(set (match_dup 0)
4324         (unspec:SI [(match_dup 0)
4325                     (match_dup 4)]
4326                    UNSPEC_INCDEC))
4327    (set (cc0)
4328         (match_dup 0))
4329    (set (pc)
4330         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4331                       (label_ref (match_dup 2))
4332                       (pc)))]
4333   "operands[4] = GEN_INT (- INTVAL (operands[1]));")
4334
4335 (define_peephole2
4336   [(set (cc0)
4337         (compare (match_operand:SI 0 "register_operand" "")
4338                  (match_operand:SI 1 "const_int_operand" "")))
4339    (set (pc)
4340         (if_then_else (match_operator 3 "eqne_operator"
4341                         [(cc0) (const_int 0)])
4342                       (label_ref (match_operand 2 "" ""))
4343                       (pc)))]
4344   "(TARGET_H8300H || TARGET_H8300S)
4345    && peep2_reg_dead_p (1, operands[0])
4346    && (INTVAL (operands[1]) == -131072
4347        || INTVAL (operands[1]) == -65536
4348        || INTVAL (operands[1]) == 65536
4349        || INTVAL (operands[1]) == 131072)"
4350   [(set (match_dup 0)
4351         (plus:SI (match_dup 0)
4352                  (match_dup 4)))
4353    (set (cc0)
4354         (match_dup 0))
4355    (set (pc)
4356         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4357                       (label_ref (match_dup 2))
4358                       (pc)))]
4359   "operands[4] = GEN_INT (- INTVAL (operands[1]));")
4360
4361 ;; For certain (in)equality comparisons against a constant, we can
4362 ;; XOR the register with the constant, and test the register against
4363 ;; 0.
4364
4365 (define_peephole2
4366   [(set (cc0)
4367         (compare (match_operand:SI 0 "register_operand" "")
4368                  (match_operand:SI 1 "const_int_operand" "")))
4369    (set (pc)
4370         (if_then_else (match_operator 3 "eqne_operator"
4371                         [(cc0) (const_int 0)])
4372                       (label_ref (match_operand 2 "" ""))
4373                       (pc)))]
4374   "(TARGET_H8300H || TARGET_H8300S)
4375    && peep2_reg_dead_p (1, operands[0])
4376    && ((INTVAL (operands[1]) & 0x00ff) == INTVAL (operands[1])
4377        || (INTVAL (operands[1]) & 0xff00) == INTVAL (operands[1])
4378        || INTVAL (operands[1]) == 0x0000ffff)
4379    && INTVAL (operands[1]) != 1
4380    && INTVAL (operands[1]) != 2"
4381   [(set (match_dup 0)
4382         (xor:SI (match_dup 0)
4383                 (match_dup 1)))
4384    (set (cc0)
4385         (match_dup 0))
4386    (set (pc)
4387         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4388                       (label_ref (match_dup 2))
4389                       (pc)))]
4390   "")
4391
4392 (define_peephole2
4393   [(set (cc0)
4394         (compare (match_operand:SI 0 "register_operand" "")
4395                  (match_operand:SI 1 "const_int_operand" "")))
4396    (set (pc)
4397         (if_then_else (match_operator 3 "eqne_operator"
4398                         [(cc0) (const_int 0)])
4399                       (label_ref (match_operand 2 "" ""))
4400                       (pc)))]
4401   "(TARGET_H8300H || TARGET_H8300S)
4402    && peep2_reg_dead_p (1, operands[0])
4403    && ((INTVAL (operands[1]) | 0x00ff) == -1
4404         || (INTVAL (operands[1]) | 0xff00) == -1)
4405    && INTVAL (operands[1]) != -1
4406    && INTVAL (operands[1]) != -2"
4407   [(set (match_dup 0)
4408         (xor:SI (match_dup 0)
4409                 (match_dup 4)))
4410    (set (match_dup 0)
4411         (not:SI (match_dup 0)))
4412    (set (cc0)
4413         (match_dup 0))
4414    (set (pc)
4415         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4416                       (label_ref (match_dup 2))
4417                       (pc)))]
4418   "operands[4] = GEN_INT (INTVAL (operands[1]) ^ -1);")
4419
4420 (define_peephole2
4421   [(set (cc0)
4422         (compare (match_operand:SI 0 "register_operand" "")
4423                  (match_operand:SI 1 "const_int_operand" "")))
4424    (set (pc)
4425         (if_then_else (match_operator 3 "eqne_operator"
4426                         [(cc0) (const_int 0)])
4427                       (label_ref (match_operand 2 "" ""))
4428                       (pc)))]
4429   "(TARGET_H8300H || TARGET_H8300S)
4430    && peep2_reg_dead_p (1, operands[0])
4431    && (INTVAL (operands[1]) == -2147483647 - 1
4432        || (TARGET_H8300S && INTVAL (operands[1]) == 1073741824))"
4433   [(set (match_dup 0)
4434         (rotate:SI (match_dup 0)
4435                    (match_dup 4)))
4436    (set (match_dup 0)
4437         (unspec:SI [(match_dup 0)
4438                     (const_int -1)]
4439                    UNSPEC_INCDEC))
4440    (set (cc0)
4441         (match_dup 0))
4442    (set (pc)
4443         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4444                       (label_ref (match_dup 2))
4445                       (pc)))]
4446   "operands[4] = GEN_INT (INTVAL (operands[1]) == -2147483647 - 1 ? 1 : 2);")
4447
4448 ;; Transform
4449 ;;
4450 ;;      cmp.l   #1,er0
4451 ;;      bgt     .L1
4452 ;;
4453 ;; into
4454 ;;
4455 ;;      mov.l   er0,er1
4456 ;;      shar.l  er1
4457 ;;      bgt     .L1
4458
4459 ;; We avoid this transformation if we see more than one copy of the
4460 ;; same compare insn immediately before this one.
4461
4462 (define_peephole2
4463   [(match_scratch:SI 4 "r")
4464    (set (cc0)
4465         (compare (match_operand:SI 0 "register_operand" "")
4466                  (match_operand:SI 1 "const_int_operand" "")))
4467    (set (pc)
4468         (if_then_else (match_operator 2 "gtle_operator"
4469                         [(cc0) (const_int 0)])
4470                       (label_ref (match_operand 3 "" ""))
4471                       (pc)))]
4472   "(TARGET_H8300H || TARGET_H8300S)
4473    && !peep2_reg_dead_p (1, operands[0])
4474    && (INTVAL (operands[1]) == 1
4475         || (TARGET_H8300S && INTVAL (operands[1]) == 3))
4476    && !same_cmp_preceding_p (insn)"
4477   [(set (match_dup 4)
4478         (match_dup 0))
4479    (parallel [(set (match_dup 4)
4480                    (ashiftrt:SI (match_dup 4)
4481                                 (match_dup 6)))
4482               (clobber (scratch:QI))])
4483    (set (cc0)
4484         (match_dup 4))
4485    (set (pc)
4486         (if_then_else (match_dup 5)
4487                       (label_ref (match_dup 3))
4488                       (pc)))]
4489   "switch (GET_CODE (operands[2]))
4490      {
4491      case GTU:
4492        operands[5] = gen_rtx_NE (VOIDmode, cc0_rtx, const0_rtx);
4493        break;
4494      case LEU:
4495        operands[5] = gen_rtx_EQ (VOIDmode, cc0_rtx, const0_rtx);
4496        break;
4497      default:
4498        operands[5] = operands[2];
4499        break;
4500      }
4501    operands[6] = GEN_INT (exact_log2 (INTVAL (operands[1]) + 1));")
4502
4503 ;; Transform
4504 ;;
4505 ;;      cmp.l   #1,er0
4506 ;;      bgt     .L1
4507 ;;
4508 ;; into
4509 ;;
4510 ;;      shar.l  er0
4511 ;;      bgt     .L1
4512
4513 (define_peephole2
4514   [(set (cc0)
4515         (compare (match_operand:SI 0 "register_operand" "")
4516                  (match_operand:SI 1 "const_int_operand" "")))
4517    (set (pc)
4518         (if_then_else (match_operator 2 "gtle_operator"
4519                         [(cc0) (const_int 0)])
4520                       (label_ref (match_operand 3 "" ""))
4521                       (pc)))]
4522   "(TARGET_H8300H || TARGET_H8300S)
4523    && peep2_reg_dead_p (1, operands[0])
4524    && (INTVAL (operands[1]) == 1
4525         || (TARGET_H8300S && INTVAL (operands[1]) == 3))"
4526   [(parallel [(set (match_dup 0)
4527                    (ashiftrt:SI (match_dup 0)
4528                                 (match_dup 5)))
4529               (clobber (scratch:QI))])
4530    (set (cc0)
4531         (match_dup 0))
4532    (set (pc)
4533         (if_then_else (match_dup 4)
4534                       (label_ref (match_dup 3))
4535                       (pc)))]
4536   "switch (GET_CODE (operands[2]))
4537      {
4538      case GTU:
4539        operands[4] = gen_rtx_NE (VOIDmode, cc0_rtx, const0_rtx);
4540        break;
4541      case LEU:
4542        operands[4] = gen_rtx_EQ (VOIDmode, cc0_rtx, const0_rtx);
4543        break;
4544      default:
4545        operands[4] = operands[2];
4546        break;
4547      }
4548    operands[5] = GEN_INT (exact_log2 (INTVAL (operands[1]) + 1));")
4549
4550 ;; Transform
4551 ;;
4552 ;;      cmp.l   #15,er0
4553 ;;      bgt     .L1
4554 ;;
4555 ;; into
4556 ;;
4557 ;;      and     #240,r0l
4558 ;;      mov.l   er0,er0
4559 ;;      bgt     .L1
4560
4561 (define_peephole2
4562   [(set (cc0)
4563         (compare (match_operand:SI 0 "register_operand" "")
4564                  (match_operand:SI 1 "const_int_operand" "")))
4565    (set (pc)
4566         (if_then_else (match_operator 2 "gtle_operator"
4567                         [(cc0) (const_int 0)])
4568                       (label_ref (match_operand 3 "" ""))
4569                       (pc)))]
4570   "(TARGET_H8300H || TARGET_H8300S)
4571    && peep2_reg_dead_p (1, operands[0])
4572    && ((TARGET_H8300H && INTVAL (operands[1]) == 3)
4573        || INTVAL (operands[1]) == 7
4574        || INTVAL (operands[1]) == 15
4575        || INTVAL (operands[1]) == 31
4576        || INTVAL (operands[1]) == 63
4577        || INTVAL (operands[1]) == 127
4578        || INTVAL (operands[1]) == 255)"
4579   [(set (match_dup 0)
4580         (and:SI (match_dup 0)
4581                 (match_dup 5)))
4582    (set (cc0)
4583         (match_dup 0))
4584    (set (pc)
4585         (if_then_else (match_dup 4)
4586                       (label_ref (match_dup 3))
4587                       (pc)))]
4588   "switch (GET_CODE (operands[2]))
4589      {
4590      case GTU:
4591        operands[4] = gen_rtx_NE (VOIDmode, cc0_rtx, const0_rtx);
4592        break;
4593      case LEU:
4594        operands[4] = gen_rtx_EQ (VOIDmode, cc0_rtx, const0_rtx);
4595        break;
4596      default:
4597        operands[4] = operands[2];
4598        break;
4599      }
4600    operands[5] = GEN_INT (~INTVAL (operands[1]));")
4601
4602 ;; Transform
4603 ;;
4604 ;;      cmp.l   #65535,er0
4605 ;;      bgt     .L1
4606 ;;
4607 ;; into
4608 ;;
4609 ;;      mov.l   e0,e0
4610 ;;      bgt     .L1
4611
4612 (define_peephole2
4613   [(set (cc0)
4614         (compare (match_operand:SI 0 "register_operand" "")
4615                  (const_int 65535)))
4616    (set (pc)
4617         (if_then_else (match_operator 1 "gtle_operator"
4618                         [(cc0) (const_int 0)])
4619                       (label_ref (match_operand 2 "" ""))
4620                       (pc)))]
4621   "TARGET_H8300H || TARGET_H8300S"
4622   [(set (cc0)
4623         (and:SI (match_dup 0)
4624                 (const_int -65536)))
4625    (set (pc)
4626         (if_then_else (match_dup 3)
4627                       (label_ref (match_dup 2))
4628                       (pc)))]
4629   "switch (GET_CODE (operands[1]))
4630      {
4631      case GTU:
4632        operands[3] = gen_rtx_NE (VOIDmode, cc0_rtx, const0_rtx);
4633        break;
4634      case LEU:
4635        operands[3] = gen_rtx_EQ (VOIDmode, cc0_rtx, const0_rtx);
4636        break;
4637      default:
4638        operands[3] = operands[1];
4639        break;
4640      }")
4641
4642 ;; For constants like -1, -2, 1, 2, it is still cheaper to make a copy
4643 ;; of the register being tested, do the subtraction on the copy, and
4644 ;; then test the copy.  We avoid this transformation if we see more
4645 ;; than one copy of the same compare insn.
4646
4647 (define_peephole2
4648   [(match_scratch:SI 4 "r")
4649    (set (cc0)
4650         (compare (match_operand:SI 0 "register_operand" "")
4651                  (match_operand:SI 1 "incdec_operand" "")))
4652    (set (pc)
4653         (if_then_else (match_operator 3 "eqne_operator"
4654                         [(cc0) (const_int 0)])
4655                       (label_ref (match_operand 2 "" ""))
4656                       (pc)))]
4657   "(TARGET_H8300H || TARGET_H8300S)
4658    && !peep2_reg_dead_p (1, operands[0])
4659    && !same_cmp_following_p (insn)"
4660   [(set (match_dup 4)
4661         (match_dup 0))
4662    (set (match_dup 4)
4663         (unspec:SI [(match_dup 4)
4664                     (match_dup 5)]
4665                    UNSPEC_INCDEC))
4666    (set (cc0)
4667         (match_dup 4))
4668    (set (pc)
4669         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4670                       (label_ref (match_dup 2))
4671                       (pc)))]
4672   "operands[5] = GEN_INT (- INTVAL (operands[1]));")
4673
4674 ;; Narrow the mode of testing if possible.
4675
4676 (define_peephole2
4677   [(set (match_operand:HI 0 "register_operand" "")
4678         (and:HI (match_dup 0)
4679                 (match_operand:HI 1 "const_int_qi_operand" "")))
4680    (set (cc0)
4681         (match_dup 0))
4682    (set (pc)
4683         (if_then_else (match_operator 3 "eqne_operator"
4684                         [(cc0) (const_int 0)])
4685                       (label_ref (match_operand 2 "" ""))
4686                       (pc)))]
4687   "peep2_reg_dead_p (2, operands[0])"
4688   [(set (match_dup 4)
4689         (and:QI (match_dup 4)
4690                 (match_dup 5)))
4691    (set (cc0)
4692         (match_dup 4))
4693    (set (pc)
4694         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4695                       (label_ref (match_dup 2))
4696                       (pc)))]
4697   "operands[4] = gen_rtx_REG (QImode, REGNO (operands[0]));
4698    operands[5] = gen_int_mode (INTVAL (operands[1]), QImode);")
4699
4700 (define_peephole2
4701   [(set (match_operand:SI 0 "register_operand" "")
4702         (and:SI (match_dup 0)
4703                 (match_operand:SI 1 "const_int_qi_operand" "")))
4704    (set (cc0)
4705         (match_dup 0))
4706    (set (pc)
4707         (if_then_else (match_operator 3 "eqne_operator"
4708                         [(cc0) (const_int 0)])
4709                       (label_ref (match_operand 2 "" ""))
4710                       (pc)))]
4711   "peep2_reg_dead_p (2, operands[0])"
4712   [(set (match_dup 4)
4713         (and:QI (match_dup 4)
4714                 (match_dup 5)))
4715    (set (cc0)
4716         (match_dup 4))
4717    (set (pc)
4718         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4719                       (label_ref (match_dup 2))
4720                       (pc)))]
4721   "operands[4] = gen_rtx_REG (QImode, REGNO (operands[0]));
4722    operands[5] = gen_int_mode (INTVAL (operands[1]), QImode);")
4723
4724 (define_peephole2
4725   [(set (match_operand:SI 0 "register_operand" "")
4726         (and:SI (match_dup 0)
4727                 (match_operand:SI 1 "const_int_hi_operand" "")))
4728    (set (cc0)
4729         (match_dup 0))
4730    (set (pc)
4731         (if_then_else (match_operator 3 "eqne_operator"
4732                         [(cc0) (const_int 0)])
4733                       (label_ref (match_operand 2 "" ""))
4734                       (pc)))]
4735   "peep2_reg_dead_p (2, operands[0])"
4736   [(set (match_dup 4)
4737         (and:HI (match_dup 4)
4738                 (match_dup 5)))
4739    (set (cc0)
4740         (match_dup 4))
4741    (set (pc)
4742         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4743                       (label_ref (match_dup 2))
4744                       (pc)))]
4745   "operands[4] = gen_rtx_REG (HImode, REGNO (operands[0]));
4746    operands[5] = gen_int_mode (INTVAL (operands[1]), HImode);")
4747
4748 (define_peephole2
4749   [(set (match_operand:SI 0 "register_operand" "")
4750         (and:SI (match_dup 0)
4751                 (match_operand:SI 1 "const_int_qi_operand" "")))
4752    (set (match_dup 0)
4753         (xor:SI (match_dup 0)
4754                 (match_operand:SI 2 "const_int_qi_operand" "")))
4755    (set (cc0)
4756         (match_dup 0))
4757    (set (pc)
4758         (if_then_else (match_operator 4 "eqne_operator"
4759                         [(cc0) (const_int 0)])
4760                       (label_ref (match_operand 3 "" ""))
4761                       (pc)))]
4762   "peep2_reg_dead_p (3, operands[0])
4763    && (~INTVAL (operands[1]) & INTVAL (operands[2])) == 0"
4764   [(set (match_dup 5)
4765         (and:QI (match_dup 5)
4766                 (match_dup 6)))
4767    (set (match_dup 5)
4768         (xor:QI (match_dup 5)
4769                 (match_dup 7)))
4770    (set (cc0)
4771         (match_dup 5))
4772    (set (pc)
4773         (if_then_else (match_op_dup 4 [(cc0) (const_int 0)])
4774                       (label_ref (match_dup 3))
4775                       (pc)))]
4776   "operands[5] = gen_rtx_REG (QImode, REGNO (operands[0]));
4777    operands[6] = gen_int_mode (INTVAL (operands[1]), QImode);
4778    operands[7] = gen_int_mode (INTVAL (operands[2]), QImode);")
4779
4780 ;; These triggers right at the end of allocation of locals in the
4781 ;; prologue (and possibly at other places).
4782
4783 ;; stack adjustment of -4, generate one push
4784 ;;
4785 ;; before : 6 bytes, 10 clocks
4786 ;; after  : 4 bytes, 10 clocks
4787
4788 (define_peephole2
4789   [(set (reg:SI SP_REG)
4790         (plus:SI (reg:SI SP_REG)
4791                  (const_int -4)))
4792    (set (mem:SI (reg:SI SP_REG))
4793         (match_operand:SI 0 "register_operand" ""))]
4794   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_NORMAL_MODE
4795    && REGNO (operands[0]) != SP_REG"
4796   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4797         (match_dup 0))]
4798   "")
4799
4800 ;; stack adjustment of -12, generate one push
4801 ;;
4802 ;; before : 10 bytes, 14 clocks
4803 ;; after  :  8 bytes, 14 clocks
4804
4805 (define_peephole2
4806   [(set (reg:SI SP_REG)
4807         (plus:SI (reg:SI SP_REG)
4808                  (const_int -12)))
4809    (set (mem:SI (reg:SI SP_REG))
4810         (match_operand:SI 0 "register_operand" ""))]
4811   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_NORMAL_MODE
4812    && REGNO (operands[0]) != SP_REG"
4813   [(set (reg:SI SP_REG)
4814         (plus:SI (reg:SI SP_REG)
4815                  (const_int -4)))
4816    (set (reg:SI SP_REG)
4817         (plus:SI (reg:SI SP_REG)
4818                  (const_int -4)))
4819    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4820         (match_dup 0))]
4821   "")