OSDN Git Service

Latest updates from FSF 4.7 branch
[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, 2005, 2006, 2007, 2008, 2010
4 ;; Free Software Foundation, Inc.
5
6 ;;   Contributed by Steve Chamberlain (sac@cygnus.com),
7 ;;   Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
8
9 ;; This file is part of GCC.
10
11 ;; GCC is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GCC is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GCC; see the file COPYING3.  If not see
23 ;; <http://www.gnu.org/licenses/>.
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   [(UNSPEC_MOVMD        100)
57    (UNSPEC_STPCPY       101)])
58
59 (define_constants
60   [(R0_REG       0)
61    (SC_REG       3)
62    (COUNTER_REG  4)
63    (SOURCE_REG   5)
64    (DESTINATION_REG 6)
65    (HFP_REG      6)
66    (SP_REG       7)
67    (MAC_REG      8)
68    (AP_REG       9)
69    (RAP_REG     10)
70    (FP_REG      11)])
71
72 ;; ----------------------------------------------------------------------
73 ;; ATTRIBUTES
74 ;; ----------------------------------------------------------------------
75
76 (define_attr "cpu" "h8300,h8300h"
77   (const (symbol_ref "cpu_type")))
78
79 (define_attr "type" "branch,arith,bitbranch,call"
80   (const_string "arith"))
81
82 (define_attr "length_table" "none,addb,addw,addl,logicb,movb,movw,movl,mova_zero,mova,unary,mov_imm4,short_immediate,bitfield,bitbranch"
83   (const_string "none"))
84
85 ;; The size of instructions in bytes.
86
87 (define_attr "length" ""
88   (cond [(eq_attr "type" "branch")
89          ;; In a forward delayed branch, (pc) represents the end of the
90          ;; delay sequence, not the end of the branch itself.
91          (if_then_else (and (ge (minus (match_dup 0) (pc))
92                                 (const_int -126))
93                             (le (plus (minus (match_dup 0) (pc))
94                                       (symbol_ref "DELAY_SLOT_LENGTH (insn)"))
95                                 (const_int 125)))
96                        (const_int 2)
97                        (if_then_else (and (eq_attr "cpu" "h8300h")
98                                           (and (ge (minus (pc) (match_dup 0))
99                                                    (const_int -32000))
100                                                (le (minus (pc) (match_dup 0))
101                                                    (const_int 32000))))
102                                      (const_int 4)
103                                      (const_int 6)))
104          (eq_attr "type" "bitbranch")
105          (if_then_else
106           (and (ge (minus (match_dup 0) (pc))
107                    (const_int -126))
108                (le (minus (match_dup 0) (pc))
109                    (const_int 126)))
110           (plus
111            (symbol_ref "h8300_insn_length_from_table (insn, operands)")
112            (const_int 2))
113           (if_then_else
114            (and (eq_attr "cpu" "h8300h")
115                 (and (ge (minus (pc) (match_dup 0))
116                          (const_int -32000))
117                      (le (minus (pc) (match_dup 0))
118                          (const_int 32000))))
119            (plus
120             (symbol_ref "h8300_insn_length_from_table (insn, operands)")
121             (const_int 4))
122            (plus
123             (symbol_ref "h8300_insn_length_from_table (insn, operands)")
124             (const_int 6))))
125          (eq_attr "length_table" "!none")
126          (symbol_ref "h8300_insn_length_from_table (insn, operands)")]
127         (const_int 200)))
128
129 ;; Condition code settings.
130 ;;
131 ;; none - insn does not affect cc
132 ;; none_0hit - insn does not affect cc but it does modify operand 0
133 ;;      This attribute is used to keep track of when operand 0 changes.
134 ;;      See the description of NOTICE_UPDATE_CC for more info.
135 ;; set_znv - insn sets z,n,v to usable values (like a tst insn); c is unknown.
136 ;; set_zn  - insn sets z,n to usable values; v,c are unknown.
137 ;; compare - compare instruction
138 ;; clobber - value of cc is unknown
139
140 (define_attr "cc" "none,none_0hit,set_znv,set_zn,compare,clobber"
141   (const_string "clobber"))
142
143 ;; Type of delay slot.  NONE means the instruction has no delay slot.
144 ;; JUMP means it is an unconditional jump that (if short enough)
145 ;; could be implemented using bra/s.
146 (define_attr "delay_slot" "none,jump"
147   (const_string "none"))
148
149 ;; "yes" if the instruction can be put into a delay slot.  It's not
150 ;; entirely clear that jsr is not valid in delay slots, but it
151 ;; definitely doesn't have the effect of causing the called function
152 ;; to return to the target of the delayed branch.
153 (define_attr "can_delay" "no,yes"
154   (cond [(eq_attr "type" "branch,bitbranch,call")
155            (const_string "no")
156          (geu (symbol_ref "get_attr_length (insn)") (const_int 2))
157            (const_string "no")]
158         (const_string "yes")))
159
160 ;; Only allow jumps to have a delay slot if we think they might
161 ;; be short enough.  This is just an optimization: we don't know
162 ;; for certain whether they will be or not.
163 (define_delay (and (eq_attr "delay_slot" "jump")
164                    (eq (symbol_ref "get_attr_length (insn)") (const_int 2)))
165   [(eq_attr "can_delay" "yes")
166    (nil)
167    (nil)])
168
169 ;; Provide the maximum length of an assembly instruction in an asm
170 ;; statement.  The maximum length of 14 bytes is achieved on H8SX.
171
172 (define_asm_attributes
173   [(set (attr "length")
174         (cond [(match_test "TARGET_H8300") (const_int 4)
175                (match_test "TARGET_H8300H") (const_int 10)
176                (match_test "TARGET_H8300S") (const_int 10)]
177               (const_int 14)))])
178
179 (include "predicates.md")
180 (include "constraints.md")
181 \f
182 ;; ----------------------------------------------------------------------
183 ;; MACRO DEFINITIONS
184 ;; ----------------------------------------------------------------------
185
186 ;; This mode iterator allows :P to be used for patterns that operate on
187 ;; pointer-sized quantities.  Exactly one of the two alternatives will match.
188 (define_mode_iterator P [(HI "Pmode == HImode") (SI "Pmode == SImode")])
189
190 \f
191 ;; ----------------------------------------------------------------------
192 ;; MOVE INSTRUCTIONS
193 ;; ----------------------------------------------------------------------
194
195 ;; movqi
196
197 (define_insn "*movqi_h8300"
198   [(set (match_operand:QI 0 "general_operand_dst" "=r,r ,<,r,r,m")
199         (match_operand:QI 1 "general_operand_src" " I,r>,r,n,m,r"))]
200   "TARGET_H8300
201    && h8300_move_ok (operands[0], operands[1])"
202   "@
203    sub.b        %X0,%X0
204    mov.b        %R1,%X0
205    mov.b        %X1,%R0
206    mov.b        %R1,%X0
207    mov.b        %R1,%X0
208    mov.b        %X1,%R0"
209   [(set_attr "length" "2,2,2,2,4,4")
210    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
211
212 (define_insn "*movqi_h8300hs"
213   [(set (match_operand:QI 0 "general_operand_dst" "=r,r ,<,r,r,m")
214         (match_operand:QI 1 "general_operand_src" " I,r>,r,n,m,r"))]
215   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX
216    && h8300_move_ok (operands[0], operands[1])"
217   "@
218    sub.b        %X0,%X0
219    mov.b        %R1,%X0
220    mov.b        %X1,%R0
221    mov.b        %R1,%X0
222    mov.b        %R1,%X0
223    mov.b        %X1,%R0"
224   [(set (attr "length")
225         (symbol_ref "compute_mov_length (operands)"))
226    (set_attr "cc" "set_zn,set_znv,set_znv,clobber,set_znv,set_znv")])
227
228 (define_insn "*movqi_h8sx"
229   [(set (match_operand:QI 0 "general_operand_dst" "=Z,rQ")
230         (match_operand:QI 1 "general_operand_src" "P4>X,rQi"))]
231   "TARGET_H8300SX"
232   "@
233     mov.b       %X1:4,%X0
234     mov.b       %X1,%X0"
235   [(set_attr "length_table" "mov_imm4,movb")
236    (set_attr "cc" "set_znv")])
237
238 (define_expand "movqi"
239   [(set (match_operand:QI 0 "general_operand_dst" "")
240         (match_operand:QI 1 "general_operand_src" ""))]
241   ""
242 {
243   /* One of the ops has to be in a register.  */
244   if (!TARGET_H8300SX && !h8300_move_ok (operands[0], operands[1]))
245     operands[1] = copy_to_mode_reg (QImode, operands[1]);
246 })
247
248 (define_insn "movstrictqi"
249   [(set (strict_low_part (match_operand:QI 0 "general_operand_dst" "+r,r"))
250                          (match_operand:QI 1 "general_operand_src" "I,rmi>"))]
251   ""
252   "@
253    sub.b        %X0,%X0
254    mov.b        %X1,%X0"
255   [(set_attr "length" "2,*")
256    (set_attr "length_table" "*,movb")
257    (set_attr "cc" "set_zn,set_znv")])
258
259 ;; movhi
260
261 (define_insn "*movhi_h8300"
262   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,<,r,r,m")
263         (match_operand:HI 1 "general_operand_src" "I,r>,r,i,m,r"))]
264   "TARGET_H8300
265    && h8300_move_ok (operands[0], operands[1])"
266   "@
267    sub.w        %T0,%T0
268    mov.w        %T1,%T0
269    mov.w        %T1,%T0
270    mov.w        %T1,%T0
271    mov.w        %T1,%T0
272    mov.w        %T1,%T0"
273   [(set (attr "length")
274         (symbol_ref "compute_mov_length (operands)"))
275    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
276
277 (define_insn "*movhi_h8300hs"
278   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,<,r,r,m")
279         (match_operand:HI 1 "general_operand_src" "I,r>,r,i,m,r"))]
280   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX
281    && h8300_move_ok (operands[0], operands[1])"
282   "@
283    sub.w        %T0,%T0
284    mov.w        %T1,%T0
285    mov.w        %T1,%T0
286    mov.w        %T1,%T0
287    mov.w        %T1,%T0
288    mov.w        %T1,%T0"
289   [(set (attr "length")
290         (symbol_ref "compute_mov_length (operands)"))
291    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
292
293 (define_insn "*movhi_h8sx"
294   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,Z,Q,rQ")
295         (match_operand:HI 1 "general_operand_src" "I,P3>X,P4>X,IP8>X,rQi"))]
296   "TARGET_H8300SX"
297   "@
298    sub.w        %T0,%T0
299    mov.w        %T1:3,%T0
300    mov.w        %T1:4,%T0
301    mov.w        %T1,%T0
302    mov.w        %T1,%T0"
303   [(set_attr "length_table" "*,*,mov_imm4,short_immediate,movw")
304    (set_attr "length" "2,2,*,*,*")
305    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv")])
306
307 (define_expand "movhi"
308   [(set (match_operand:HI 0 "general_operand_dst" "")
309         (match_operand:HI 1 "general_operand_src" ""))]
310   ""
311 {
312   /* One of the ops has to be in a register.  */
313   if (!h8300_move_ok (operands[0], operands[1]))
314     operands[1] = copy_to_mode_reg (HImode, operand1);
315 })
316
317 (define_insn "movstricthi"
318   [(set (strict_low_part (match_operand:HI 0 "general_operand_dst" "+r,r,r"))
319                          (match_operand:HI 1 "general_operand_src" "I,P3>X,rmi"))]
320   ""
321   "@
322    sub.w        %T0,%T0
323    mov.w        %T1,%T0
324    mov.w        %T1,%T0"
325   [(set_attr "length" "2,2,*")
326    (set_attr "length_table" "*,*,movw")
327    (set_attr "cc" "set_zn,set_znv,set_znv")])
328
329 ;; movsi
330
331 (define_expand "movsi"
332   [(set (match_operand:SI 0 "general_operand_dst" "")
333         (match_operand:SI 1 "general_operand_src" ""))]
334   ""
335 {
336   if (TARGET_H8300)
337     {
338       if (h8300_expand_movsi (operands))
339         DONE;
340     }
341   else if (!TARGET_H8300SX)
342     {
343       /* One of the ops has to be in a register.  */
344       if (! h8300_move_ok (operands[0], operands[1]))
345         operands[1] = copy_to_mode_reg (SImode, operand1);
346     }
347 })
348
349 (define_insn "*movsi_h8300"
350   [(set (match_operand:SI 0 "general_operand_dst" "=r,r,r,o,<,r")
351         (match_operand:SI 1 "general_operand_src" "I,r,io,r,r,>"))]
352   "TARGET_H8300
353    && h8300_move_ok (operands[0], operands[1])"
354 {
355   unsigned int rn = -1;
356   switch (which_alternative)
357     {
358     case 0:
359       return "sub.w     %e0,%e0\;sub.w  %f0,%f0";
360     case 1:
361       if (REGNO (operands[0]) < REGNO (operands[1]))
362         return "mov.w   %e1,%e0\;mov.w  %f1,%f0";
363       else
364         return "mov.w   %f1,%f0\;mov.w  %e1,%e0";
365     case 2:
366       /* Make sure we don't trample the register we index with.  */
367       if (GET_CODE (operands[1]) == MEM)
368         {
369           rtx inside = XEXP (operands[1], 0);
370           if (REG_P (inside))
371             {
372               rn = REGNO (inside);
373             }
374           else if (GET_CODE (inside) == PLUS)
375             {
376               rtx lhs = XEXP (inside, 0);
377               rtx rhs = XEXP (inside, 1);
378               if (REG_P (lhs)) rn = REGNO (lhs);
379               if (REG_P (rhs)) rn = REGNO (rhs);
380             }
381         }
382       if (rn == REGNO (operands[0]))
383         {
384           /* Move the second word first.  */
385           return "mov.w %f1,%f0\;mov.w  %e1,%e0";
386         }
387       else
388         {
389           if (GET_CODE (operands[1]) == CONST_INT)
390             {
391               /* If either half is zero, use sub.w to clear that
392                  half.  */
393               if ((INTVAL (operands[1]) & 0xffff) == 0)
394                 return "mov.w   %e1,%e0\;sub.w  %f0,%f0";
395               if (((INTVAL (operands[1]) >> 16) & 0xffff) == 0)
396                 return "sub.w   %e0,%e0\;mov.w  %f1,%f0";
397               /* If the upper half and the lower half are the same,
398                  copy one half to the other.  */
399               if ((INTVAL (operands[1]) & 0xffff)
400                   == ((INTVAL (operands[1]) >> 16) & 0xffff))
401                 return "mov.w\\t%e1,%e0\;mov.w\\t%e0,%f0";
402             }
403           return "mov.w %e1,%e0\;mov.w  %f1,%f0";
404         }
405     case 3:
406       return "mov.w     %e1,%e0\;mov.w  %f1,%f0";
407     case 4:
408       return "mov.w     %f1,%T0\;mov.w  %e1,%T0";
409     case 5:
410       return "mov.w     %T1,%e0\;mov.w  %T1,%f0";
411     default:
412       gcc_unreachable ();
413     }
414 }
415   [(set (attr "length")
416         (symbol_ref "compute_mov_length (operands)"))])
417
418 (define_insn "*movsi_h8300hs"
419   [(set (match_operand:SI 0 "general_operand_dst" "=r,r,r,<,r,r,m,*a,*a,r")
420         (match_operand:SI 1 "general_operand_src" "I,r,i,r,>,m,r,I,r,*a"))]
421   "(TARGET_H8300S || TARGET_H8300H) && !TARGET_H8300SX
422     && h8300_move_ok (operands[0], operands[1])"
423 {
424   switch (which_alternative)
425     {
426     case 0:
427       return "sub.l     %S0,%S0";
428     case 7:
429       return "clrmac";
430     case 8:
431       return "clrmac\;ldmac %1,macl";
432     case 9:
433       return "stmac     macl,%0";
434     default:
435       if (GET_CODE (operands[1]) == CONST_INT)
436         {
437           int val = INTVAL (operands[1]);
438
439           /* Look for constants which can be made by adding an 8-bit
440              number to zero in one of the two low bytes.  */
441           if (val == (val & 0xff))
442             {
443               operands[1] = GEN_INT ((char) val & 0xff);
444               return "sub.l\\t%S0,%S0\;add.b\\t%1,%w0";
445             }
446
447           if (val == (val & 0xff00))
448             {
449               operands[1] = GEN_INT ((char) (val >> 8) & 0xff);
450               return "sub.l\\t%S0,%S0\;add.b\\t%1,%x0";
451             }
452
453           /* Look for constants that can be obtained by subs, inc, and
454              dec to 0.  */
455           switch (val & 0xffffffff)
456             {
457             case 0xffffffff:
458               return "sub.l\\t%S0,%S0\;subs\\t#1,%S0";
459             case 0xfffffffe:
460               return "sub.l\\t%S0,%S0\;subs\\t#2,%S0";
461             case 0xfffffffc:
462               return "sub.l\\t%S0,%S0\;subs\\t#4,%S0";
463
464             case 0x0000ffff:
465               return "sub.l\\t%S0,%S0\;dec.w\\t#1,%f0";
466             case 0x0000fffe:
467               return "sub.l\\t%S0,%S0\;dec.w\\t#2,%f0";
468
469             case 0xffff0000:
470               return "sub.l\\t%S0,%S0\;dec.w\\t#1,%e0";
471             case 0xfffe0000:
472               return "sub.l\\t%S0,%S0\;dec.w\\t#2,%e0";
473
474             case 0x00010000:
475               return "sub.l\\t%S0,%S0\;inc.w\\t#1,%e0";
476             case 0x00020000:
477               return "sub.l\\t%S0,%S0\;inc.w\\t#2,%e0";
478             }
479         }
480     }
481    return "mov.l        %S1,%S0";
482 }
483   [(set (attr "length")
484         (symbol_ref "compute_mov_length (operands)"))
485    (set_attr "cc" "set_zn,set_znv,clobber,set_znv,set_znv,set_znv,set_znv,none_0hit,none_0hit,set_znv")])
486
487 (define_insn "*movsi_h8sx"
488   [(set (match_operand:SI 0 "general_operand_dst" "=r,r,Q,rQ,*a,*a,r")
489         (match_operand:SI 1 "general_operand_src" "I,P3>X,IP8>X,rQi,I,r,*a"))]
490   "TARGET_H8300SX"
491   "@
492    sub.l        %S0,%S0
493    mov.l        %S1:3,%S0
494    mov.l        %S1,%S0
495    mov.l        %S1,%S0
496    clrmac
497    clrmac\;ldmac        %1,macl
498    stmac        macl,%0"
499   [(set_attr "length_table" "*,*,short_immediate,movl,*,*,*")
500    (set_attr "length" "2,2,*,*,2,6,4")
501    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,none_0hit,none_0hit,set_znv")])
502
503 (define_insn "*movsf_h8sx"
504   [(set (match_operand:SF 0 "general_operand_dst" "=r,rQ")
505         (match_operand:SF 1 "general_operand_src" "G,rQi"))]
506   "TARGET_H8300SX"
507   "@
508     sub.l       %S0,%S0
509     mov.l       %S1,%S0"
510   [(set_attr "length" "2,*")
511    (set_attr "length_table" "*,movl")
512    (set_attr "cc" "set_zn,set_znv")])
513
514 ;; Implement block moves using movmd.  Defining movmemsi allows the full
515 ;; range of constant lengths (up to 0x40000 bytes when using movmd.l).
516 ;; See h8sx_emit_movmd for details.
517
518 (define_expand "movmemsi"
519   [(use (match_operand:BLK 0 "memory_operand" ""))
520    (use (match_operand:BLK 1 "memory_operand" ""))
521    (use (match_operand:SI 2 "" ""))
522    (use (match_operand:SI 3 "const_int_operand" ""))]
523   "TARGET_H8300SX"
524   {
525     if (h8sx_emit_movmd (operands[0], operands[1], operands[2], INTVAL (operands[3])))
526       DONE;
527     else
528       FAIL;
529   })
530
531 ;; Expander for generating movmd insns.  Operand 0 is the destination
532 ;; memory region, operand 1 is the source, operand 2 is the counter
533 ;; register and operand 3 is the chunk size (1, 2 or 4).
534
535 (define_expand "movmd"
536   [(parallel
537     [(set (match_operand:BLK 0 "memory_operand" "")
538           (match_operand:BLK 1 "memory_operand" ""))
539      (unspec [(match_operand:HI 2 "register_operand" "")
540               (match_operand:HI 3 "const_int_operand" "")] UNSPEC_MOVMD)
541      (clobber (match_dup 4))
542      (clobber (match_dup 5))
543      (set (match_dup 2)
544           (const_int 0))])]
545   "TARGET_H8300SX"
546   {
547     operands[4] = copy_rtx (XEXP (operands[0], 0));
548     operands[5] = copy_rtx (XEXP (operands[1], 0));
549   })
550
551 ;; This is a difficult instruction to reload since operand 0 must be the
552 ;; frame pointer.  See h8300_reg_class_from_letter for an explanation.
553
554 (define_insn "movmd_internal_normal"
555   [(set (mem:BLK (match_operand:HI 3 "register_operand" "0,r"))
556         (mem:BLK (match_operand:HI 4 "register_operand" "1,1")))
557    (unspec [(match_operand:HI 5 "register_operand" "2,2")
558             (match_operand:HI 6 "const_int_operand" "n,n")] UNSPEC_MOVMD)
559    (clobber (match_operand:HI 0 "register_operand" "=d,??D"))
560    (clobber (match_operand:HI 1 "register_operand" "=f,f"))
561    (set (match_operand:HI 2 "register_operand" "=c,c")
562         (const_int 0))]
563   "TARGET_H8300SX && TARGET_NORMAL_MODE"
564   "@
565     movmd%m6
566     #"
567   [(set_attr "length" "2,14")
568    (set_attr "can_delay" "no")
569    (set_attr "cc" "none,clobber")])
570
571 (define_insn "movmd_internal"
572   [(set (mem:BLK (match_operand:SI 3 "register_operand" "0,r"))
573         (mem:BLK (match_operand:SI 4 "register_operand" "1,1")))
574    (unspec [(match_operand:HI 5 "register_operand" "2,2")
575             (match_operand:HI 6 "const_int_operand" "n,n")] UNSPEC_MOVMD)
576    (clobber (match_operand:SI 0 "register_operand" "=d,??D"))
577    (clobber (match_operand:SI 1 "register_operand" "=f,f"))
578    (set (match_operand:HI 2 "register_operand" "=c,c")
579         (const_int 0))]
580   "TARGET_H8300SX && !TARGET_NORMAL_MODE"
581   "@
582     movmd%m6
583     #"
584   [(set_attr "length" "2,14")
585    (set_attr "can_delay" "no")
586    (set_attr "cc" "none,clobber")])
587
588 ;; Split the above instruction if the destination register isn't er6.
589 ;; We need a sequence like:
590 ;;
591 ;;      mov.l   er6,@-er7
592 ;;      mov.l   <dest>,er6
593 ;;      movmd.sz
594 ;;      mov.l   er6,<dest>
595 ;;      mov.l   @er7+,er6
596 ;;
597 ;; where <dest> is the current destination register (operand 4).
598 ;; The fourth instruction will be deleted if <dest> dies here.
599
600 (define_split
601   [(set (match_operand:BLK 0 "memory_operand" "")
602         (match_operand:BLK 1 "memory_operand" ""))
603    (unspec [(match_operand:HI 2 "register_operand" "")
604             (match_operand:HI 3 "const_int_operand" "")] UNSPEC_MOVMD)
605    (clobber (match_operand:HI 4 "register_operand" ""))
606    (clobber (match_operand:HI 5 "register_operand" ""))
607    (set (match_dup 2)
608         (const_int 0))]
609   "TARGET_H8300SX && TARGET_NORMAL_MODE && reload_completed
610    && REGNO (operands[4]) != DESTINATION_REG"
611   [(const_int 0)]
612   {
613     rtx dest;
614
615     h8300_swap_into_er6 (XEXP (operands[0], 0));
616     dest = replace_equiv_address (operands[0], hard_frame_pointer_rtx);
617     emit_insn (gen_movmd (dest, operands[1], operands[2], operands[3]));
618     if (REGNO (operands[4]) != DESTINATION_REG)
619       h8300_swap_out_of_er6 (operands[4]);
620     DONE;
621   })
622
623 (define_split
624   [(set (match_operand:BLK 0 "memory_operand" "")
625         (match_operand:BLK 1 "memory_operand" ""))
626    (unspec [(match_operand:HI 2 "register_operand" "")
627             (match_operand:HI 3 "const_int_operand" "")] UNSPEC_MOVMD)
628    (clobber (match_operand:SI 4 "register_operand" ""))
629    (clobber (match_operand:SI 5 "register_operand" ""))
630    (set (match_dup 2)
631         (const_int 0))]
632   "TARGET_H8300SX && !TARGET_NORMAL_MODE && reload_completed
633    && REGNO (operands[4]) != DESTINATION_REG"
634   [(const_int 0)]
635   {
636     rtx dest;
637
638     h8300_swap_into_er6 (XEXP (operands[0], 0));
639     dest = replace_equiv_address (operands[0], hard_frame_pointer_rtx);
640     emit_insn (gen_movmd (dest, operands[1], operands[2], operands[3]));
641     if (REGNO (operands[4]) != DESTINATION_REG)
642     h8300_swap_out_of_er6 (operands[4]);
643     DONE;
644   })
645
646 ;; Expand a call to stpcpy() using movsd.  Operand 0 should point to
647 ;; the final character, but movsd leaves it pointing to the character
648 ;; after that.
649
650 (define_expand "movstr"
651   [(use (match_operand 0 "register_operand" ""))
652    (use (match_operand:BLK 1 "memory_operand" ""))
653    (use (match_operand:BLK 2 "memory_operand" ""))]
654   "TARGET_H8300SX"
655   {
656     operands[1] = replace_equiv_address
657       (operands[1], copy_to_mode_reg (Pmode, XEXP (operands[1], 0)));
658     operands[2] = replace_equiv_address
659       (operands[2], copy_to_mode_reg (Pmode, XEXP (operands[2], 0)));
660     emit_insn (gen_movsd (operands[1], operands[2], gen_reg_rtx (Pmode)));
661     emit_insn (gen_add3_insn (operands[0], XEXP (operands[1], 0), constm1_rtx));
662     DONE;
663   })
664
665 ;; Expander for generating a movsd instruction.  Operand 0 is the
666 ;; destination string, operand 1 is the source string and operand 2
667 ;; is a scratch register.
668
669 (define_expand "movsd"
670   [(parallel
671     [(set (match_operand:BLK 0 "memory_operand" "")
672           (unspec:BLK [(match_operand:BLK 1 "memory_operand" "")]
673           UNSPEC_STPCPY))
674      (clobber (match_dup 3))
675      (clobber (match_dup 4))
676      (clobber (match_operand 2 "register_operand" ""))])]
677   "TARGET_H8300SX"
678   {
679     operands[3] = copy_rtx (XEXP (operands[0], 0));
680     operands[4] = copy_rtx (XEXP (operands[1], 0));
681   })
682
683 ;; See comments above memcpy_internal().
684
685 (define_insn "stpcpy_internal_normal"
686   [(set (mem:BLK (match_operand:HI 3 "register_operand" "0,r"))
687         (unspec:BLK [(mem:BLK (match_operand:HI 4 "register_operand" "1,1"))]
688         UNSPEC_STPCPY))
689    (clobber (match_operand:HI 0 "register_operand" "=d,??D"))
690    (clobber (match_operand:HI 1 "register_operand" "=f,f"))
691    (clobber (match_operand:HI 2 "register_operand" "=c,c"))]
692   "TARGET_H8300SX && TARGET_NORMAL_MODE"
693   "@
694     \n1:\tmovsd\t2f\;bra\t1b\n2:
695     #"
696   [(set_attr "length" "6,18")
697    (set_attr "cc" "none,clobber")])
698
699 (define_insn "stpcpy_internal"
700   [(set (mem:BLK (match_operand:SI 3 "register_operand" "0,r"))
701         (unspec:BLK [(mem:BLK (match_operand:SI 4 "register_operand" "1,1"))]
702         UNSPEC_STPCPY))
703    (clobber (match_operand:SI 0 "register_operand" "=d,??D"))
704    (clobber (match_operand:SI 1 "register_operand" "=f,f"))
705    (clobber (match_operand:SI 2 "register_operand" "=c,c"))]
706   "TARGET_H8300SX && !TARGET_NORMAL_MODE"
707   "@
708     \n1:\tmovsd\t2f\;bra\t1b\n2:
709     #"
710   [(set_attr "length" "6,18")
711    (set_attr "cc" "none,clobber")])
712
713 ;; Split the above instruction if the destination isn't er6.  This works
714 ;; in the same way as the movmd splitter.
715
716 (define_split
717   [(set (match_operand:BLK 0 "memory_operand" "")
718         (unspec:BLK [(match_operand:BLK 1 "memory_operand" "")] UNSPEC_STPCPY))
719    (clobber (match_operand:HI 2 "register_operand" ""))
720    (clobber (match_operand:HI 3 "register_operand" ""))
721    (clobber (match_operand:HI 4 "register_operand" ""))]
722   "TARGET_H8300SX && TARGET_NORMAL_MODE && reload_completed
723    && REGNO (operands[2]) != DESTINATION_REG"
724   [(const_int 0)]
725   {
726     rtx dest;
727
728     h8300_swap_into_er6 (XEXP (operands[0], 0));
729     dest = replace_equiv_address (operands[0], hard_frame_pointer_rtx);
730     emit_insn (gen_movsd (dest, operands[1], operands[4]));
731     h8300_swap_out_of_er6 (operands[2]);
732     DONE;
733   })
734
735 (define_split
736   [(set (match_operand:BLK 0 "memory_operand" "")
737         (unspec:BLK [(match_operand:BLK 1 "memory_operand" "")] UNSPEC_STPCPY))
738    (clobber (match_operand:SI 2 "register_operand" ""))
739    (clobber (match_operand:SI 3 "register_operand" ""))
740    (clobber (match_operand:SI 4 "register_operand" ""))]
741   "TARGET_H8300SX && !TARGET_NORMAL_MODE && reload_completed
742    && REGNO (operands[2]) != DESTINATION_REG"
743   [(const_int 0)]
744   {
745     rtx dest;
746
747     h8300_swap_into_er6 (XEXP (operands[0], 0));
748     dest = replace_equiv_address (operands[0], hard_frame_pointer_rtx);
749     emit_insn (gen_movsd (dest, operands[1], operands[4]));
750     h8300_swap_out_of_er6 (operands[2]);
751     DONE;
752   })
753
754 (include "mova.md")
755
756 (define_expand "movsf"
757   [(set (match_operand:SF 0 "general_operand_dst" "")
758         (match_operand:SF 1 "general_operand_src" ""))]
759   ""
760   {
761     if (TARGET_H8300)
762       {
763         if (h8300_expand_movsi (operands))
764           DONE;
765       }
766     else if (!TARGET_H8300SX)
767       {
768         /* One of the ops has to be in a register.  */
769         if (!register_operand (operand1, SFmode)
770             && !register_operand (operand0, SFmode))
771           {
772             operands[1] = copy_to_mode_reg (SFmode, operand1);
773           }
774       }
775   })
776
777 (define_insn "*movsf_h8300"
778   [(set (match_operand:SF 0 "general_operand_dst" "=r,r,r,o,<,r")
779         (match_operand:SF 1 "general_operand_src" "G,r,io,r,r,>"))]
780   "TARGET_H8300
781    && (register_operand (operands[0], SFmode)
782        || register_operand (operands[1], SFmode))"
783 {
784   /* Copy of the movsi stuff.  */
785   unsigned int rn = -1;
786   switch (which_alternative)
787     {
788     case 0:
789       return "sub.w     %e0,%e0\;sub.w  %f0,%f0";
790     case 1:
791       if (REGNO (operands[0]) < REGNO (operands[1]))
792         return "mov.w   %e1,%e0\;mov.w  %f1,%f0";
793       else
794         return "mov.w   %f1,%f0\;mov.w  %e1,%e0";
795     case 2:
796       /* Make sure we don't trample the register we index with.  */
797       if (GET_CODE (operands[1]) == MEM)
798         {
799           rtx inside = XEXP (operands[1], 0);
800           if (REG_P (inside))
801             {
802               rn = REGNO (inside);
803             }
804           else if (GET_CODE (inside) == PLUS)
805             {
806               rtx lhs = XEXP (inside, 0);
807               rtx rhs = XEXP (inside, 1);
808               if (REG_P (lhs)) rn = REGNO (lhs);
809               if (REG_P (rhs)) rn = REGNO (rhs);
810             }
811         }
812       if (rn == REGNO (operands[0]))
813         /* Move the second word first.  */
814         return "mov.w   %f1,%f0\;mov.w  %e1,%e0";
815       else
816         /* Move the first word first.  */
817         return "mov.w   %e1,%e0\;mov.w  %f1,%f0";
818
819     case 3:
820       return "mov.w     %e1,%e0\;mov.w  %f1,%f0";
821     case 4:
822       return "mov.w     %f1,%T0\;mov.w  %e1,%T0";
823     case 5:
824       return "mov.w     %T1,%e0\;mov.w  %T1,%f0";
825     default:
826       gcc_unreachable ();
827     }
828 }
829   [(set (attr "length")
830         (symbol_ref "compute_mov_length (operands)"))])
831
832 (define_insn "*movsf_h8300hs"
833   [(set (match_operand:SF 0 "general_operand_dst" "=r,r,r,m,<,r")
834         (match_operand:SF 1 "general_operand_src" "G,r,im,r,r,>"))]
835   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX
836     && (register_operand (operands[0], SFmode)
837         || register_operand (operands[1], SFmode))"
838   "@
839    sub.l        %S0,%S0
840    mov.l        %S1,%S0
841    mov.l        %S1,%S0
842    mov.l        %S1,%S0
843    mov.l        %S1,%S0
844    mov.l        %S1,%S0"
845   [(set (attr "length")
846         (symbol_ref "compute_mov_length (operands)"))
847    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
848 \f
849 ;; ----------------------------------------------------------------------
850 ;; PUSH INSTRUCTIONS
851 ;; ----------------------------------------------------------------------
852
853 (define_insn "*pushqi1_h8300"
854   [(set (mem:QI
855         (pre_modify:HI
856           (reg:HI SP_REG)
857           (plus:HI (reg:HI SP_REG) (const_int -2))))
858         (match_operand:QI 0 "register_no_sp_elim_operand" "r"))]
859   "TARGET_H8300"
860   "mov.w\\t%T0,@-r7"
861   [(set_attr "length" "2")])
862
863 (define_insn "*pushqi1_h8300hs_<mode>"
864   [(set (mem:QI
865         (pre_modify:P
866           (reg:P SP_REG)
867           (plus:P (reg:P SP_REG) (const_int -4))))
868         (match_operand:QI 0 "register_no_sp_elim_operand" "r"))]
869   "TARGET_H8300H || TARGET_H8300S"
870   "mov.l\\t%S0,@-er7"
871   [(set_attr "length" "4")])
872
873 (define_insn "*pushhi1_h8300hs_<mode>"
874   [(set (mem:HI
875         (pre_modify:P
876           (reg:P SP_REG)
877           (plus:P (reg:P SP_REG) (const_int -4))))
878         (match_operand:HI 0 "register_no_sp_elim_operand" "r"))]
879   "TARGET_H8300H || TARGET_H8300S"
880   "mov.l\\t%S0,@-er7"
881   [(set_attr "length" "4")])
882 \f
883 ;; ----------------------------------------------------------------------
884 ;; TEST INSTRUCTIONS
885 ;; ----------------------------------------------------------------------
886
887 (define_insn ""
888   [(set (cc0) 
889         (compare (zero_extract:QI (match_operand:QI 0 "bit_memory_operand" "r,U")
890                                   (const_int 1)
891                                   (match_operand 1 "const_int_operand" "n,n"))
892                  (const_int 0)))]
893   "TARGET_H8300"
894   "btst %Z1,%Y0"
895   [(set_attr "length" "2,4")
896    (set_attr "cc" "set_zn,set_zn")])
897
898 (define_insn ""
899   [(set (cc0)
900         (compare (zero_extract:HI (match_operand:HI 0 "register_operand" "r")
901                                   (const_int 1)
902                                   (match_operand 1 "const_int_operand" "n"))
903                  (const_int 0)))]
904   "TARGET_H8300"
905   "btst %Z1,%Y0"
906   [(set_attr "length" "2")
907    (set_attr "cc" "set_zn")])
908
909 (define_insn_and_split "*tst_extzv_1_n"
910   [(set (cc0) 
911         (compare (zero_extract:SI (match_operand:QI 0 "general_operand_src" "r,U,mn>")
912                                   (const_int 1)
913                                   (match_operand 1 "const_int_operand" "n,n,n"))
914                  (const_int 0)))
915    (clobber (match_scratch:QI 2 "=X,X,&r"))]
916   "TARGET_H8300H || TARGET_H8300S"
917   "@
918    btst\\t%Z1,%Y0
919    btst\\t%Z1,%Y0
920    #"
921   "&& reload_completed
922    && !satisfies_constraint_U (operands[0])"
923   [(set (match_dup 2)
924         (match_dup 0))
925    (parallel [(set (cc0) (compare (zero_extract:SI (match_dup 2)
926                                                    (const_int 1)
927                                                    (match_dup 1))
928                                   (const_int 0)))
929               (clobber (scratch:QI))])]
930   ""
931   [(set_attr "length" "2,8,10")
932    (set_attr "cc" "set_zn,set_zn,set_zn")])
933
934 (define_insn ""
935   [(set (cc0) 
936         (compare (zero_extract:SI (match_operand:SI 0 "register_operand" "r")
937                                   (const_int 1)
938                                   (match_operand 1 "const_int_operand" "n"))
939                  (const_int 0)))]
940   "(TARGET_H8300H || TARGET_H8300S)
941     && INTVAL (operands[1]) <= 15"
942   "btst %Z1,%Y0"
943   [(set_attr "length" "2")
944    (set_attr "cc" "set_zn")])
945
946 (define_insn_and_split "*tstsi_upper_bit"
947   [(set (cc0) 
948         (compare (zero_extract:SI (match_operand:SI 0 "register_operand" "r")
949                                   (const_int 1)
950                                   (match_operand 1 "const_int_operand" "n"))
951                  (const_int 0)))
952    (clobber (match_scratch:SI 2 "=&r"))]
953   "(TARGET_H8300H || TARGET_H8300S)
954     && INTVAL (operands[1]) >= 16"
955   "#"
956   "&& reload_completed"
957   [(set (match_dup 2)
958         (ior:SI (and:SI (match_dup 2)
959                         (const_int -65536))
960                 (lshiftrt:SI (match_dup 0)
961                              (const_int 16))))
962    (set (cc0)
963         (compare (zero_extract:SI (match_dup 2)
964                                   (const_int 1)
965                                   (match_dup 3))
966                  (const_int 0)))]
967   {
968     operands[3] = GEN_INT (INTVAL (operands[1]) - 16);
969   })
970
971 (define_insn "*tstsi_variable_bit"
972   [(set (cc0)
973         (compare (zero_extract:SI (match_operand:SI 0 "register_operand" "r")
974                                   (const_int 1)
975                                   (and:SI (match_operand:SI 1 "register_operand" "r")
976                                           (const_int 7)))
977                  (const_int 0)))]
978   "TARGET_H8300H || TARGET_H8300S"
979   "btst %w1,%w0"
980   [(set_attr "length" "2")
981    (set_attr "cc" "set_zn")])
982
983 (define_insn_and_split "*tstsi_variable_bit_qi"
984   [(set (cc0)
985         (compare (zero_extract:SI (zero_extend:SI (match_operand:QI 0 "general_operand_src" "r,U,mn>"))
986                                   (const_int 1)
987                                   (and:SI (match_operand:SI 1 "register_operand" "r,r,r")
988                                           (const_int 7)))
989                  (const_int 0)))
990    (clobber (match_scratch:QI 2 "=X,X,&r"))]
991   "TARGET_H8300H || TARGET_H8300S"
992   "@
993    btst\\t%w1,%X0
994    btst\\t%w1,%X0
995    #"
996   "&& reload_completed
997    && !satisfies_constraint_U (operands[0])"
998   [(set (match_dup 2)
999         (match_dup 0))
1000    (parallel [(set (cc0)
1001                    (compare (zero_extract:SI (zero_extend:SI (match_dup 2))
1002                                              (const_int 1)
1003                                              (and:SI (match_dup 1)
1004                                                      (const_int 7)))
1005                             (const_int 0)))
1006               (clobber (scratch:QI))])]
1007   ""
1008   [(set_attr "length" "2,8,10")
1009    (set_attr "cc" "set_zn,set_zn,set_zn")])
1010
1011 (define_insn "*tstqi"
1012   [(set (cc0) 
1013         (compare (match_operand:QI 0 "register_operand" "r")
1014                  (const_int 0)))]
1015   ""
1016   "mov.b        %X0,%X0"
1017   [(set_attr "length" "2")
1018    (set_attr "cc" "set_znv")])
1019
1020 (define_insn "*tsthi"
1021   [(set (cc0)
1022         (compare (match_operand:HI 0 "register_operand" "r")
1023                  (const_int 0)))]
1024   ""
1025   "mov.w        %T0,%T0"
1026   [(set_attr "length" "2")
1027    (set_attr "cc" "set_znv")])
1028
1029 (define_insn "*tsthi_upper"
1030   [(set (cc0)
1031         (compare (and:HI (match_operand:HI 0 "register_operand" "r")
1032                          (const_int -256))
1033                  (const_int 0)))]
1034   ""
1035   "mov.b        %t0,%t0"
1036   [(set_attr "length" "2")
1037    (set_attr "cc" "set_znv")])
1038
1039 (define_insn "*tstsi"
1040   [(set (cc0)
1041         (compare (match_operand:SI 0 "register_operand" "r")
1042                  (const_int 0)))]
1043   "TARGET_H8300H || TARGET_H8300S"
1044   "mov.l        %S0,%S0"
1045   [(set_attr "length" "2")
1046    (set_attr "cc" "set_znv")])
1047
1048 (define_insn "*tstsi_upper"
1049   [(set (cc0)
1050         (compare (and:SI (match_operand:SI 0 "register_operand" "r")
1051                          (const_int -65536))
1052                  (const_int 0)))]
1053   ""
1054   "mov.w        %e0,%e0"
1055   [(set_attr "length" "2")
1056    (set_attr "cc" "set_znv")])
1057
1058 (define_insn "*cmpqi"
1059   [(set (cc0)
1060         (compare (match_operand:QI 0 "h8300_dst_operand" "rQ")
1061                  (match_operand:QI 1 "h8300_src_operand" "rQi")))]
1062   ""
1063   "cmp.b        %X1,%X0"
1064   [(set_attr "length_table" "addb")
1065    (set_attr "cc" "compare")])
1066
1067 (define_insn "*cmphi_h8300_znvc"
1068   [(set (cc0)
1069         (compare (match_operand:HI 0 "register_operand" "r")
1070                  (match_operand:HI 1 "register_operand" "r")))]
1071   "TARGET_H8300"
1072   "cmp.w        %T1,%T0"
1073   [(set_attr "length" "2")
1074    (set_attr "cc" "compare")])
1075
1076 (define_insn "*cmphi_h8300hs_znvc"
1077   [(set (cc0)
1078         (compare (match_operand:HI 0 "h8300_dst_operand" "rU,rQ")
1079                  (match_operand:HI 1 "h8300_src_operand" "P3>X,rQi")))]
1080   "TARGET_H8300H || TARGET_H8300S"
1081 {
1082   switch (which_alternative)
1083     {
1084     case 0:
1085       if (!TARGET_H8300SX)
1086         return "cmp.w   %T1,%T0";
1087       else
1088         return "cmp.w   %T1:3,%T0";
1089     case 1:
1090       return "cmp.w     %T1,%T0";
1091     default:
1092       gcc_unreachable ();
1093       }
1094 }
1095   [(set_attr "length_table" "short_immediate,addw")
1096    (set_attr "cc" "compare,compare")])
1097
1098 (define_insn "cmpsi"
1099   [(set (cc0)
1100         (compare (match_operand:SI 0 "h8300_dst_operand" "r,rQ")
1101                  (match_operand:SI 1 "h8300_src_operand" "P3>X,rQi")))]
1102   "TARGET_H8300H || TARGET_H8300S"
1103 {
1104   switch (which_alternative)
1105     {
1106     case 0:
1107       if (!TARGET_H8300SX)
1108         return "cmp.l   %S1,%S0";
1109       else
1110         return "cmp.l   %S1:3,%S0";
1111     case 1:
1112       return "cmp.l     %S1,%S0";
1113     default:
1114       gcc_unreachable ();
1115     }
1116 }
1117   [(set_attr "length" "2,*")
1118    (set_attr "length_table" "*,addl")
1119    (set_attr "cc" "compare,compare")])
1120 \f
1121 ;; ----------------------------------------------------------------------
1122 ;; ADD INSTRUCTIONS
1123 ;; ----------------------------------------------------------------------
1124
1125 (define_expand "addqi3"
1126   [(set (match_operand:QI 0 "register_operand" "")
1127         (plus:QI (match_operand:QI 1 "register_operand" "")
1128                  (match_operand:QI 2 "h8300_src_operand" "")))]
1129   ""
1130   "")
1131
1132 (define_insn "*addqi3"
1133   [(set (match_operand:QI 0 "h8300_dst_operand" "=rQ")
1134         (plus:QI (match_operand:QI 1 "h8300_dst_operand" "%0")
1135                  (match_operand:QI 2 "h8300_src_operand" "rQi")))]
1136   "h8300_operands_match_p (operands)"
1137   "add.b        %X2,%X0"
1138   [(set_attr "length_table" "addb")
1139    (set_attr "cc" "set_zn")])
1140
1141 (define_expand "addhi3"
1142   [(set (match_operand:HI 0 "register_operand" "")
1143         (plus:HI (match_operand:HI 1 "register_operand" "")
1144                  (match_operand:HI 2 "h8300_src_operand" "")))]
1145   ""
1146   "")
1147
1148 (define_insn "*addhi3_h8300"
1149   [(set (match_operand:HI 0 "register_operand" "=r,r,r,r,r")
1150         (plus:HI (match_operand:HI 1 "register_operand" "%0,0,0,0,0")
1151                  (match_operand:HI 2 "h8300_src_operand" "L,N,J,n,r")))]
1152   "TARGET_H8300"
1153   "@
1154    adds %2,%T0
1155    subs %G2,%T0
1156    add.b        %t2,%t0
1157    add.b        %s2,%s0\;addx   %t2,%t0
1158    add.w        %T2,%T0"
1159   [(set_attr "length" "2,2,2,4,2")
1160    (set_attr "cc" "none_0hit,none_0hit,clobber,clobber,set_zn")])
1161
1162 ;; This splitter is very important to make the stack adjustment
1163 ;; interrupt-safe.  The combination of add.b and addx is unsafe!
1164 ;;
1165 ;; We apply this split after the peephole2 pass so that we won't end
1166 ;; up creating too many adds/subs when a scratch register is
1167 ;; available, which is actually a common case because stack unrolling
1168 ;; tends to happen immediately after a function call.
1169
1170 (define_split
1171   [(set (match_operand:HI 0 "stack_pointer_operand" "")
1172         (plus:HI (match_dup 0)
1173                  (match_operand 1 "const_int_gt_2_operand" "")))]
1174   "TARGET_H8300 && epilogue_completed"
1175   [(const_int 0)]
1176   {
1177     split_adds_subs (HImode, operands); 
1178     DONE;
1179   })
1180
1181 (define_peephole2
1182   [(match_scratch:HI 2 "r")
1183    (set (match_operand:HI 0 "stack_pointer_operand" "")
1184         (plus:HI (match_dup 0)
1185                  (match_operand:HI 1 "const_int_ge_8_operand" "")))]
1186   "TARGET_H8300"
1187   [(set (match_dup 2)
1188         (match_dup 1))
1189    (set (match_dup 0)
1190         (plus:HI (match_dup 0)
1191                  (match_dup 2)))]
1192   "")
1193
1194 (define_insn "*addhi3_h8300hs"
1195   [(set (match_operand:HI 0 "register_operand" "=r,r,r,r,r")
1196         (plus:HI (match_operand:HI 1 "register_operand" "%0,0,0,0,0")
1197                  (match_operand:HI 2 "h8300_src_operand" "L,N,J,n,r")))]
1198   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX"
1199   "@
1200    adds %2,%S0
1201    subs %G2,%S0
1202    add.b        %t2,%t0
1203    add.w        %T2,%T0
1204    add.w        %T2,%T0"
1205   [(set_attr "length" "2,2,2,4,2")
1206    (set_attr "cc" "none_0hit,none_0hit,clobber,set_zn,set_zn")])
1207
1208 (define_insn "*addhi3_incdec"
1209   [(set (match_operand:HI 0 "register_operand" "=r,r")
1210         (unspec:HI [(match_operand:HI 1 "register_operand" "0,0")
1211                     (match_operand:HI 2 "incdec_operand" "M,O")]
1212                    UNSPEC_INCDEC))]
1213   "TARGET_H8300H || TARGET_H8300S"
1214   "@
1215    inc.w        %2,%T0
1216    dec.w        %G2,%T0"
1217   [(set_attr "length" "2,2")
1218    (set_attr "cc" "set_zn,set_zn")])
1219
1220 (define_insn "*addhi3_h8sx"
1221   [(set (match_operand:HI 0 "h8300_dst_operand" "=rU,rU,r,rQ")
1222         (plus:HI (match_operand:HI 1 "h8300_dst_operand" "%0,0,0,0")
1223                  (match_operand:HI 2 "h8300_src_operand" "P3>X,P3<X,J,rQi")))]
1224   "TARGET_H8300SX && h8300_operands_match_p (operands)"
1225   "@
1226    add.w        %T2:3,%T0
1227    sub.w        %G2:3,%T0
1228    add.b        %t2,%t0
1229    add.w        %T2,%T0"
1230   [(set_attr "length_table" "short_immediate,short_immediate,*,addw")
1231    (set_attr "length" "*,*,2,*")
1232    (set_attr "cc" "set_zn")])
1233
1234 (define_split
1235   [(set (match_operand:HI 0 "register_operand" "")
1236         (plus:HI (match_dup 0)
1237                  (match_operand:HI 1 "two_insn_adds_subs_operand" "")))]
1238   ""
1239   [(const_int 0)]
1240   {
1241     split_adds_subs (HImode, operands); 
1242     DONE;
1243   })
1244
1245 (define_expand "addsi3"
1246   [(set (match_operand:SI 0 "register_operand" "")
1247         (plus:SI (match_operand:SI 1 "register_operand" "")
1248                  (match_operand:SI 2 "h8300_src_operand" "")))]
1249   ""
1250   "")
1251
1252 (define_insn "*addsi_h8300"
1253   [(set (match_operand:SI 0 "register_operand" "=r,r")
1254         (plus:SI (match_operand:SI 1 "register_operand" "%0,0")
1255                  (match_operand:SI 2 "h8300_src_operand" "n,r")))]
1256   "TARGET_H8300"
1257 {
1258   return output_plussi (operands);
1259 }
1260   [(set (attr "length")
1261         (symbol_ref "compute_plussi_length (operands)"))
1262    (set (attr "cc")
1263         (symbol_ref "compute_plussi_cc (operands)"))])
1264
1265 (define_insn "*addsi_h8300hs"
1266   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ,rQ")
1267         (plus:SI (match_operand:SI 1 "h8300_dst_operand" "%0,0")
1268                  (match_operand:SI 2 "h8300_src_operand" "i,rQ")))]
1269   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
1270 {  
1271   return output_plussi (operands);
1272 }
1273   [(set (attr "length")
1274         (symbol_ref "compute_plussi_length (operands)"))
1275    (set (attr "cc")
1276         (symbol_ref "compute_plussi_cc (operands)"))])
1277
1278 (define_insn "*addsi3_incdec"
1279   [(set (match_operand:SI 0 "register_operand" "=r,r")
1280         (unspec:SI [(match_operand:SI 1 "register_operand" "0,0")
1281                     (match_operand:SI 2 "incdec_operand" "M,O")]
1282                    UNSPEC_INCDEC))]
1283   "TARGET_H8300H || TARGET_H8300S"
1284   "@
1285    inc.l        %2,%S0
1286    dec.l        %G2,%S0"
1287   [(set_attr "length" "2,2")
1288    (set_attr "cc" "set_zn,set_zn")])
1289
1290 (define_split
1291   [(set (match_operand:SI 0 "register_operand" "")
1292         (plus:SI (match_dup 0)
1293                  (match_operand:SI 1 "two_insn_adds_subs_operand" "")))]
1294   "TARGET_H8300H || TARGET_H8300S"
1295   [(const_int 0)]
1296   {
1297     split_adds_subs (SImode, operands); 
1298     DONE;
1299   })
1300
1301 ;; ----------------------------------------------------------------------
1302 ;; SUBTRACT INSTRUCTIONS
1303 ;; ----------------------------------------------------------------------
1304
1305 (define_expand "subqi3"
1306   [(set (match_operand:QI 0 "register_operand" "")
1307         (minus:QI (match_operand:QI 1 "register_operand" "")
1308                   (match_operand:QI 2 "h8300_src_operand" "")))]
1309   ""
1310   "")
1311
1312 (define_insn "*subqi3"
1313   [(set (match_operand:QI 0 "h8300_dst_operand" "=rQ")
1314         (minus:QI (match_operand:QI 1 "h8300_dst_operand" "0")
1315                   (match_operand:QI 2 "h8300_dst_operand" "rQ")))]
1316   "h8300_operands_match_p (operands)"
1317   "sub.b        %X2,%X0"
1318   [(set_attr "length_table" "addb")
1319    (set_attr "cc" "set_zn")])
1320
1321 (define_expand "subhi3"
1322   [(set (match_operand:HI 0 "register_operand" "")
1323         (minus:HI (match_operand:HI 1 "register_operand" "")
1324                   (match_operand:HI 2 "h8300_src_operand" "")))]
1325   ""
1326   "")
1327
1328 (define_insn "*subhi3_h8300"
1329   [(set (match_operand:HI 0 "register_operand" "=r,r")
1330         (minus:HI (match_operand:HI 1 "register_operand" "0,0")
1331                   (match_operand:HI 2 "h8300_src_operand" "r,n")))]
1332   "TARGET_H8300"
1333   "@
1334    sub.w        %T2,%T0
1335    add.b        %E2,%s0\;addx   %F2,%t0"
1336   [(set_attr "length" "2,4")
1337    (set_attr "cc" "set_zn,clobber")])
1338
1339 (define_insn "*subhi3_h8300hs"
1340   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ,rQ")
1341         (minus:HI (match_operand:HI 1 "h8300_dst_operand" "0,0")
1342                   (match_operand:HI 2 "h8300_src_operand" "rQ,i")))]
1343   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
1344   "@
1345    sub.w        %T2,%T0
1346    sub.w        %T2,%T0"
1347   [(set_attr "length_table" "addw")
1348    (set_attr "cc" "set_zn")])
1349
1350 (define_expand "subsi3"
1351   [(set (match_operand:SI 0 "register_operand" "")
1352         (minus:SI (match_operand:SI 1 "register_operand" "")
1353                   (match_operand:SI 2 "h8300_src_operand" "")))]
1354   ""
1355   {
1356     if (TARGET_H8300)
1357       operands[2] = force_reg (SImode, operands[2]);
1358   })
1359
1360 (define_insn "*subsi3_h8300"
1361   [(set (match_operand:SI 0 "register_operand" "=r")
1362         (minus:SI (match_operand:SI 1 "register_operand" "0")
1363                   (match_operand:SI 2 "register_operand" "r")))]
1364   "TARGET_H8300"
1365   "sub.w        %f2,%f0\;subx   %y2,%y0\;subx   %z2,%z0"
1366   [(set_attr "length" "6")])
1367
1368 (define_insn "*subsi3_h8300hs"
1369   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ,rQ")
1370         (minus:SI (match_operand:SI 1 "h8300_dst_operand" "0,0")
1371                   (match_operand:SI 2 "h8300_src_operand" "rQ,i")))]
1372   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
1373   "@
1374    sub.l        %S2,%S0
1375    sub.l        %S2,%S0"
1376   [(set_attr "length_table" "addl")
1377    (set_attr "cc" "set_zn")])
1378 \f
1379 ;; ----------------------------------------------------------------------
1380 ;; MULTIPLY INSTRUCTIONS
1381 ;; ----------------------------------------------------------------------
1382
1383 ;; Note that the H8/300 can only handle umulqihi3.
1384
1385 (define_expand "mulqihi3"
1386   [(set (match_operand:HI 0 "register_operand" "")
1387         (mult:HI (sign_extend:HI (match_operand:QI 1 "register_operand" ""))
1388                  ;; intentionally-mismatched modes
1389                  (match_operand:QI 2 "reg_or_nibble_operand" "")))]
1390   "TARGET_H8300H || TARGET_H8300S"
1391   {
1392     if (GET_MODE (operands[2]) != VOIDmode)
1393       operands[2] = gen_rtx_SIGN_EXTEND (HImode, operands[2]);
1394   })
1395
1396 (define_insn "*mulqihi3_const"
1397   [(set (match_operand:HI 0 "register_operand" "=r")
1398         (mult:HI (sign_extend:HI (match_operand:QI 1 "register_operand" "%0"))
1399                  (match_operand:QI 2 "nibble_operand" "IP4>X")))]
1400   "TARGET_H8300SX"
1401   "mulxs.b      %X2,%T0"
1402   [(set_attr "length" "4")
1403    (set_attr "cc" "set_zn")])
1404
1405 (define_insn "*mulqihi3"
1406   [(set (match_operand:HI 0 "register_operand" "=r")
1407         (mult:HI (sign_extend:HI (match_operand:QI 1 "register_operand" "%0"))
1408                  (sign_extend:HI (match_operand:QI 2 "register_operand" "r"))))]
1409   "TARGET_H8300H || TARGET_H8300S"
1410   "mulxs.b      %X2,%T0"
1411   [(set_attr "length" "4")
1412    (set_attr "cc" "set_zn")])
1413
1414 (define_expand "mulhisi3"
1415   [(set (match_operand:SI 0 "register_operand" "")
1416         (mult:SI (sign_extend:SI (match_operand:HI 1 "register_operand" ""))
1417                  ;; intentionally-mismatched modes
1418                  (match_operand:HI 2 "reg_or_nibble_operand" "")))]
1419   "TARGET_H8300H || TARGET_H8300S"
1420   {
1421     if (GET_MODE (operands[2]) != VOIDmode)
1422       operands[2] = gen_rtx_SIGN_EXTEND (SImode, operands[2]);
1423   })
1424
1425 (define_insn "*mulhisi3_const"
1426   [(set (match_operand:SI 0 "register_operand" "=r")
1427         (mult:SI (sign_extend:SI (match_operand:HI 1 "register_operand" "%0"))
1428                  (match_operand:SI 2 "nibble_operand" "IP4>X")))]
1429   "TARGET_H8300SX"
1430   "mulxs.w      %T2,%S0"
1431   [(set_attr "length" "4")
1432    (set_attr "cc" "set_zn")])
1433
1434 (define_insn "*mulhisi3"
1435   [(set (match_operand:SI 0 "register_operand" "=r")
1436         (mult:SI (sign_extend:SI (match_operand:HI 1 "register_operand" "%0"))
1437                  (sign_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
1438   "TARGET_H8300H || TARGET_H8300S"
1439   "mulxs.w      %T2,%S0"
1440   [(set_attr "length" "4")
1441    (set_attr "cc" "set_zn")])
1442
1443 (define_expand "umulqihi3"
1444   [(set (match_operand:HI 0 "register_operand" "")
1445         (mult:HI (zero_extend:HI (match_operand:QI 1 "register_operand" ""))
1446                  ;; intentionally-mismatched modes
1447                  (match_operand:QI 2 "reg_or_nibble_operand" "")))]
1448   "TARGET_H8300H || TARGET_H8300S"
1449   {
1450     if (GET_MODE (operands[2]) != VOIDmode)
1451       operands[2] = gen_rtx_ZERO_EXTEND (HImode, operands[2]);
1452   })
1453
1454 (define_insn "*umulqihi3_const"
1455   [(set (match_operand:HI 0 "register_operand" "=r")
1456         (mult:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "%0"))
1457                  (match_operand:QI 2 "nibble_operand" "IP4>X")))]
1458   "TARGET_H8300SX"
1459   "mulxu.b      %X2,%T0"
1460   [(set_attr "length" "4")
1461    (set_attr "cc" "set_zn")])
1462
1463 (define_insn "*umulqihi3"
1464   [(set (match_operand:HI 0 "register_operand" "=r")
1465         (mult:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "%0"))
1466                  (zero_extend:HI (match_operand:QI 2 "register_operand" "r"))))]
1467   ""
1468   "mulxu.b      %X2,%T0"
1469   [(set_attr "length" "2")
1470    (set_attr "cc" "none_0hit")])
1471
1472 (define_expand "umulhisi3"
1473   [(set (match_operand:SI 0 "register_operand" "")
1474         (mult:SI (zero_extend:SI (match_operand:HI 1 "register_operand" ""))
1475                  ;; intentionally-mismatched modes
1476                  (match_operand:HI 2 "reg_or_nibble_operand" "")))]
1477   "TARGET_H8300H || TARGET_H8300S"
1478   {
1479     if (GET_MODE (operands[2]) != VOIDmode)
1480       operands[2] = gen_rtx_ZERO_EXTEND (SImode, operands[2]);
1481   })
1482
1483 (define_insn "*umulhisi3_const"
1484   [(set (match_operand:SI 0 "register_operand" "=r")
1485         (mult:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "%0"))
1486                  (match_operand:SI 2 "nibble_operand" "IP4>X")))]
1487   "TARGET_H8300SX"
1488   "mulxu.w      %T2,%S0"
1489   [(set_attr "length" "4")
1490    (set_attr "cc" "set_zn")])
1491
1492 (define_insn "*umulhisi3"
1493   [(set (match_operand:SI 0 "register_operand" "=r")
1494         (mult:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "%0"))
1495                  (zero_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
1496   "TARGET_H8300H || TARGET_H8300S"
1497   "mulxu.w      %T2,%S0"
1498   [(set_attr "length" "2")
1499    (set_attr "cc" "none_0hit")])
1500
1501 ;; We could have used mulu.[wl] here, but mulu.[lw] is only available
1502 ;; on a H8SX with a multiplier, whereas muls.w seems to be available
1503 ;; on all H8SX variants.
1504
1505 (define_insn "mulhi3"
1506   [(set (match_operand:HI 0 "register_operand" "=r")
1507         (mult:HI (match_operand:HI 1 "register_operand" "%0")
1508                  (match_operand:HI 2 "reg_or_nibble_operand" "r IP4>X")))]
1509   "TARGET_H8300SX"
1510   "muls.w\\t%T2,%T0"
1511   [(set_attr "length" "2")
1512    (set_attr "cc" "set_zn")])
1513
1514 (define_insn "mulsi3"
1515   [(set (match_operand:SI 0 "register_operand" "=r")
1516         (mult:SI (match_operand:SI 1 "register_operand" "%0")
1517                  (match_operand:SI 2 "reg_or_nibble_operand" "r IP4>X")))]
1518   "TARGET_H8300SX"
1519   "muls.l\\t%S2,%S0"
1520   [(set_attr "length" "2")
1521    (set_attr "cc" "set_zn")])
1522
1523 (define_insn "smulsi3_highpart"
1524   [(set (match_operand:SI 0 "register_operand" "=r")
1525         (truncate:SI
1526          (lshiftrt:DI
1527           (mult:DI
1528            (sign_extend:DI (match_operand:SI 1 "register_operand" "%0"))
1529            (sign_extend:DI (match_operand:SI 2 "reg_or_nibble_operand" "r IP4>X")))
1530           (const_int 32))))]
1531   "TARGET_H8300SXMUL"
1532   "muls/u.l\\t%S2,%S0"
1533   [(set_attr "length" "2")
1534    (set_attr "cc" "set_zn")])
1535
1536 (define_insn "umulsi3_highpart"
1537   [(set (match_operand:SI 0 "register_operand" "=r")
1538         (truncate:SI
1539           (ashiftrt:DI
1540             (mult:DI
1541               (zero_extend:DI (match_operand:SI 1 "register_operand" "%0"))
1542               (zero_extend:DI (match_operand:SI 2 "reg_or_nibble_operand" "r IP4>X")))
1543             (const_int 32))))]
1544   "TARGET_H8300SX"
1545   "mulu/u.l\\t%S2,%S0"
1546   [(set_attr "length" "2")
1547    (set_attr "cc" "none_0hit")])
1548
1549 ;; This is a "bridge" instruction.  Combine can't cram enough insns
1550 ;; together to crate a MAC instruction directly, but it can create
1551 ;; this instruction, which then allows combine to create the real
1552 ;; MAC insn.
1553 ;;
1554 ;; Unfortunately, if combine doesn't create a MAC instruction, this
1555 ;; insn must generate reasonably correct code.  Egad.
1556
1557 (define_insn ""
1558   [(set (match_operand:SI 0 "register_operand" "=a")
1559         (mult:SI
1560           (sign_extend:SI
1561             (mem:HI (post_inc:SI (match_operand:SI 1 "register_operand" "r"))))
1562           (sign_extend:SI
1563             (mem:HI (post_inc:SI (match_operand:SI 2 "register_operand" "r"))))))]
1564   "TARGET_MAC"
1565   "clrmac\;mac  @%2+,@%1+"
1566   [(set_attr "length" "6")
1567    (set_attr "cc" "none_0hit")])
1568
1569 (define_insn ""
1570   [(set (match_operand:SI 0 "register_operand" "=a")
1571         (plus:SI (mult:SI
1572           (sign_extend:SI (mem:HI
1573             (post_inc:SI (match_operand:SI 1 "register_operand" "r"))))
1574           (sign_extend:SI (mem:HI
1575             (post_inc:SI (match_operand:SI 2 "register_operand" "r")))))
1576               (match_operand:SI 3 "register_operand" "0")))]
1577   "TARGET_MAC"
1578   "mac  @%2+,@%1+"
1579   [(set_attr "length" "4")
1580    (set_attr "cc" "none_0hit")])
1581
1582 ;; ----------------------------------------------------------------------
1583 ;; DIVIDE/MOD INSTRUCTIONS
1584 ;; ----------------------------------------------------------------------
1585
1586 (define_insn "udivhi3"
1587   [(set (match_operand:HI 0 "register_operand" "=r")
1588         (udiv:HI (match_operand:HI 1 "register_operand" "0")
1589                  (match_operand:HI 2 "reg_or_nibble_operand" "r IP4>X")))]
1590   "TARGET_H8300SX"
1591   "divu.w\\t%T2,%T0"
1592   [(set_attr "length" "2")])
1593   
1594 (define_insn "divhi3"
1595   [(set (match_operand:HI 0 "register_operand" "=r")
1596         (div:HI (match_operand:HI 1 "register_operand" "0")
1597                 (match_operand:HI 2 "reg_or_nibble_operand" "r IP4>X")))]
1598   "TARGET_H8300SX"
1599   "divs.w\\t%T2,%T0"
1600   [(set_attr "length" "2")])
1601   
1602 (define_insn "udivsi3"
1603   [(set (match_operand:SI 0 "register_operand" "=r")
1604         (udiv:SI (match_operand:SI 1 "register_operand" "0")
1605                  (match_operand:SI 2 "reg_or_nibble_operand" "r IP4>X")))]
1606   "TARGET_H8300SX"
1607   "divu.l\\t%S2,%S0"
1608   [(set_attr "length" "2")])
1609   
1610 (define_insn "divsi3"
1611   [(set (match_operand:SI 0 "register_operand" "=r")
1612         (div:SI (match_operand:SI 1 "register_operand" "0")
1613                 (match_operand:SI 2 "reg_or_nibble_operand" "r IP4>X")))]
1614   "TARGET_H8300SX"
1615   "divs.l\\t%S2,%S0"
1616   [(set_attr "length" "2")])
1617   
1618 (define_insn "udivmodqi4"
1619   [(set (match_operand:QI 0 "register_operand" "=r")
1620         (truncate:QI
1621           (udiv:HI
1622             (match_operand:HI 1 "register_operand" "0")
1623             (zero_extend:HI (match_operand:QI 2 "register_operand" "r")))))
1624    (set (match_operand:QI 3 "register_operand" "=r")
1625         (truncate:QI
1626           (umod:HI
1627             (match_dup 1)
1628             (zero_extend:HI (match_dup 2)))))]
1629   ""
1630 {
1631   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1632     return "divxu.b\\t%X2,%T0";
1633   else
1634     return "divxu.b\\t%X2,%T0\;mov.b\\t%t0,%s3";
1635 }
1636   [(set_attr "length" "4")])
1637
1638 (define_insn "divmodqi4"
1639   [(set (match_operand:QI 0 "register_operand" "=r")
1640         (truncate:QI
1641           (div:HI
1642             (match_operand:HI 1 "register_operand" "0")
1643             (sign_extend:HI (match_operand:QI 2 "register_operand" "r")))))
1644    (set (match_operand:QI 3 "register_operand" "=r")
1645         (truncate:QI
1646           (mod:HI
1647             (match_dup 1)
1648             (sign_extend:HI (match_dup 2)))))]
1649   "TARGET_H8300H || TARGET_H8300S"
1650 {
1651   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1652     return "divxs.b\\t%X2,%T0";
1653   else
1654     return "divxs.b\\t%X2,%T0\;mov.b\\t%t0,%s3";
1655 }
1656   [(set_attr "length" "6")])
1657
1658 (define_insn "udivmodhi4"
1659   [(set (match_operand:HI 0 "register_operand" "=r")
1660         (truncate:HI
1661           (udiv:SI
1662             (match_operand:SI 1 "register_operand" "0")
1663             (zero_extend:SI (match_operand:HI 2 "register_operand" "r")))))
1664    (set (match_operand:HI 3 "register_operand" "=r")
1665         (truncate:HI
1666           (umod:SI
1667             (match_dup 1)
1668             (zero_extend:SI (match_dup 2)))))]
1669   "TARGET_H8300H || TARGET_H8300S"
1670 {
1671   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1672     return "divxu.w\\t%T2,%S0";
1673   else
1674     return "divxu.w\\t%T2,%S0\;mov.w\\t%e0,%f3";
1675 }
1676   [(set_attr "length" "4")])
1677
1678 (define_insn "divmodhi4"
1679   [(set (match_operand:HI 0 "register_operand" "=r")
1680         (truncate:HI
1681           (div:SI
1682             (match_operand:SI 1 "register_operand" "0")
1683             (sign_extend:SI (match_operand:HI 2 "register_operand" "r")))))
1684    (set (match_operand:HI 3 "register_operand" "=r")
1685         (truncate:HI
1686           (mod:SI
1687             (match_dup 1)
1688             (sign_extend:SI (match_dup 2)))))]
1689   "TARGET_H8300H || TARGET_H8300S"
1690 {
1691   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1692     return "divxs.w\\t%T2,%S0";
1693   else
1694     return "divxs.w\\t%T2,%S0\;mov.w\\t%e0,%f3";
1695 }
1696   [(set_attr "length" "6")])
1697 \f
1698 ;; ----------------------------------------------------------------------
1699 ;; AND INSTRUCTIONS
1700 ;; ----------------------------------------------------------------------
1701
1702 (define_insn "bclrqi_msx"
1703   [(set (match_operand:QI 0 "bit_register_indirect_operand" "=WU")
1704         (and:QI (match_operand:QI 1 "bit_register_indirect_operand" "%0")
1705                 (match_operand:QI 2 "single_zero_operand" "Y0")))]
1706   "TARGET_H8300SX && rtx_equal_p (operands[0], operands[1])"
1707   "bclr\\t%W2,%0"
1708   [(set_attr "length" "8")])
1709
1710 (define_split
1711   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=U")
1712         (and:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1713                 (match_operand:HI 2 "single_zero_operand" "Y0")))]
1714   "TARGET_H8300SX"
1715   [(set (match_dup 0)
1716         (and:QI (match_dup 1)
1717                 (match_dup 2)))]
1718   {
1719     if (abs (INTVAL (operands[2])) > 0xFF)
1720       {
1721         operands[0] = adjust_address (operands[0], QImode, 0);
1722         operands[1] = adjust_address (operands[1], QImode, 0);
1723         operands[2] = GEN_INT ((INTVAL (operands[2])) >> 8);
1724       }
1725     else
1726       {
1727         operands[0] = adjust_address (operands[0], QImode, 1);
1728         operands[1] = adjust_address (operands[1], QImode, 1);
1729       }
1730   })
1731
1732 (define_insn "bclrhi_msx"
1733   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=m")
1734         (and:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1735                 (match_operand:HI 2 "single_zero_operand" "Y0")))]
1736   "TARGET_H8300SX"
1737   "bclr\\t%W2,%0"
1738   [(set_attr "length" "8")])
1739
1740 (define_insn "*andqi3_2"
1741   [(set (match_operand:QI 0 "bit_operand" "=U,rQ,r")
1742         (and:QI (match_operand:QI 1 "bit_operand" "%0,0,WU")
1743                 (match_operand:QI 2 "h8300_src_operand" "Y0,rQi,IP1>X")))]
1744   "TARGET_H8300SX"
1745   "@
1746    bclr\\t %W2,%R0
1747    and  %X2,%X0
1748    bfld %2,%1,%R0"
1749   [(set_attr "length" "8,*,8")
1750    (set_attr "length_table" "*,logicb,*")
1751    (set_attr "cc" "none_0hit,set_znv,none_0hit")])
1752
1753 (define_insn "andqi3_1"
1754   [(set (match_operand:QI 0 "bit_operand" "=U,r")
1755         (and:QI (match_operand:QI 1 "bit_operand" "%0,0")
1756                 (match_operand:QI 2 "h8300_src_operand" "Y0,rn")))]
1757   "register_operand (operands[0], QImode)
1758    || single_zero_operand (operands[2], QImode)"
1759   "@
1760    bclr %W2,%R0
1761    and  %X2,%X0"
1762   [(set_attr "length" "2,8")
1763    (set_attr "cc" "none_0hit,set_znv")])
1764
1765 (define_expand "andqi3"
1766   [(set (match_operand:QI 0 "register_operand" "")
1767         (and:QI (match_operand:QI 1 "register_operand" "")
1768                 (match_operand:QI 2 "h8300_src_operand" "")))]
1769   ""
1770   "")
1771
1772 (define_expand "andhi3"
1773   [(set (match_operand:HI 0 "register_operand" "")
1774         (and:HI (match_operand:HI 1 "register_operand" "")
1775                 (match_operand:HI 2 "h8300_src_operand" "")))]
1776   ""
1777   "")
1778
1779 (define_insn "*andorqi3"
1780   [(set (match_operand:QI 0 "register_operand" "=r")
1781         (ior:QI (and:QI (match_operand:QI 2 "register_operand" "r")
1782                         (match_operand:QI 3 "single_one_operand" "n"))
1783                 (match_operand:QI 1 "register_operand" "0")))]
1784   ""
1785   "bld\\t%V3,%X2\;bor\\t%V3,%X0\;bst\\t%V3,%X0"
1786   [(set_attr "length" "6")])
1787
1788 (define_insn "*andorhi3"
1789   [(set (match_operand:HI 0 "register_operand" "=r")
1790         (ior:HI (and:HI (match_operand:HI 2 "register_operand" "r")
1791                         (match_operand:HI 3 "single_one_operand" "n"))
1792                 (match_operand:HI 1 "register_operand" "0")))]
1793   ""
1794 {
1795   operands[3] = GEN_INT (INTVAL (operands[3]) & 0xffff);
1796   if (INTVAL (operands[3]) > 128)
1797     {
1798       operands[3] = GEN_INT (INTVAL (operands[3]) >> 8);
1799       return "bld\\t%V3,%t2\;bor\\t%V3,%t0\;bst\\t%V3,%t0";
1800     }
1801   return "bld\\t%V3,%s2\;bor\\t%V3,%s0\;bst\\t%V3,%s0";
1802 }
1803   [(set_attr "length" "6")])
1804
1805 (define_insn "*andorsi3"
1806   [(set (match_operand:SI 0 "register_operand" "=r")
1807         (ior:SI (and:SI (match_operand:SI 2 "register_operand" "r")
1808                         (match_operand:SI 3 "single_one_operand" "n"))
1809                 (match_operand:SI 1 "register_operand" "0")))]
1810   "(INTVAL (operands[3]) & 0xffff) != 0"
1811 {
1812   operands[3] = GEN_INT (INTVAL (operands[3]) & 0xffff);
1813   if (INTVAL (operands[3]) > 128)
1814     {
1815       operands[3] = GEN_INT (INTVAL (operands[3]) >> 8);
1816       return "bld\\t%V3,%x2\;bor\\t%V3,%x0\;bst\\t%V3,%x0";
1817     }
1818   return "bld\\t%V3,%w2\;bor\\t%V3,%w0\;bst\\t%V3,%w0";
1819 }
1820   [(set_attr "length" "6")])
1821
1822 (define_insn "*andorsi3_shift_8"
1823   [(set (match_operand:SI 0 "register_operand" "=r")
1824         (ior:SI (and:SI (ashift:SI (match_operand:SI 2 "register_operand" "r")
1825                                    (const_int 8))
1826                         (const_int 65280))
1827                 (match_operand:SI 1 "register_operand" "0")))]
1828   ""
1829   "or.b\\t%w2,%x0"
1830   [(set_attr "length" "2")])
1831
1832 (define_expand "andsi3"
1833   [(set (match_operand:SI 0 "register_operand" "")
1834         (and:SI (match_operand:SI 1 "register_operand" "")
1835                 (match_operand:SI 2 "h8300_src_operand" "")))]
1836   ""
1837   "")
1838
1839 ;; ----------------------------------------------------------------------
1840 ;; OR INSTRUCTIONS
1841 ;; ----------------------------------------------------------------------
1842
1843 (define_insn "bsetqi_msx"
1844   [(set (match_operand:QI 0 "bit_register_indirect_operand" "=WU")
1845         (ior:QI (match_operand:QI 1 "bit_register_indirect_operand" "%0")
1846                 (match_operand:QI 2 "single_one_operand" "Y2")))]
1847   "TARGET_H8300SX && rtx_equal_p (operands[0], operands[1])"
1848   "bset\\t%V2,%0"
1849   [(set_attr "length" "8")])
1850
1851 (define_split
1852   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=U")
1853         (ior:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1854                 (match_operand:HI 2 "single_one_operand" "Y2")))]
1855   "TARGET_H8300SX"
1856   [(set (match_dup 0)
1857         (ior:QI (match_dup 1)
1858                 (match_dup 2)))]
1859   {
1860     if (abs (INTVAL (operands[2])) > 0xFF)
1861       {
1862         operands[0] = adjust_address (operands[0], QImode, 0);
1863         operands[1] = adjust_address (operands[1], QImode, 0);
1864         operands[2] = GEN_INT ((INTVAL (operands[2])) >> 8);
1865       }
1866     else
1867       {
1868         operands[0] = adjust_address (operands[0], QImode, 1);
1869         operands[1] = adjust_address (operands[1], QImode, 1);
1870       }
1871   })
1872
1873 (define_insn "bsethi_msx"
1874   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=m")
1875         (ior:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1876                 (match_operand:HI 2 "single_one_operand" "Y2")))]
1877   "TARGET_H8300SX"
1878   "bset\\t%V2,%0"
1879   [(set_attr "length" "8")])
1880
1881 (define_insn "iorqi3_1"
1882   [(set (match_operand:QI 0 "bit_operand" "=U,rQ")
1883         (ior:QI (match_operand:QI 1 "bit_operand" "%0,0")
1884                 (match_operand:QI 2 "h8300_src_operand" "Y2,rQi")))]
1885   "TARGET_H8300SX || register_operand (operands[0], QImode)
1886    || single_one_operand (operands[2], QImode)"
1887   "@
1888    bset\\t%V2,%R0
1889    or\\t%X2,%X0"
1890   [(set_attr "length" "8,*")
1891    (set_attr "length_table" "*,logicb")
1892    (set_attr "cc" "none_0hit,set_znv")])
1893
1894
1895 (define_expand "iorqi3"
1896   [(set (match_operand:QI 0 "register_operand" "")
1897         (ior:QI (match_operand:QI 1 "register_operand" "")
1898                 (match_operand:QI 2 "h8300_src_operand" "")))]
1899   ""
1900   "")
1901
1902 (define_expand "iorhi3"
1903   [(set (match_operand:HI 0 "register_operand" "")
1904         (ior:HI (match_operand:HI 1 "register_operand" "")
1905                 (match_operand:HI 2 "h8300_src_operand" "")))]
1906   ""
1907   "")
1908
1909 (define_expand "iorsi3"
1910   [(set (match_operand:SI 0 "register_operand" "")
1911         (ior:SI (match_operand:SI 1 "register_operand" "")
1912                 (match_operand:SI 2 "h8300_src_operand" "")))]
1913   ""
1914   "")
1915
1916 ;; ----------------------------------------------------------------------
1917 ;; XOR INSTRUCTIONS
1918 ;; ----------------------------------------------------------------------
1919
1920 (define_insn "bnotqi_msx"
1921   [(set (match_operand:QI 0 "bit_register_indirect_operand" "=WU")
1922         (xor:QI (match_operand:QI 1 "bit_register_indirect_operand" "%0")
1923                 (match_operand:QI 2 "single_one_operand" "Y2")))]
1924   "TARGET_H8300SX
1925    && rtx_equal_p (operands[0], operands[1])"
1926   "bnot\\t%V2,%0"
1927   [(set_attr "length" "8")])
1928
1929 (define_split
1930   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=U")
1931         (xor:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1932                 (match_operand:HI 2 "single_one_operand" "Y2")))]
1933   "TARGET_H8300SX"
1934   [(set (match_dup 0)
1935         (xor:QI (match_dup 1)
1936                 (match_dup 2)))]
1937   {
1938     if (abs (INTVAL (operands[2])) > 0xFF)
1939       {
1940         operands[0] = adjust_address (operands[0], QImode, 0);
1941         operands[1] = adjust_address (operands[1], QImode, 0);
1942         operands[2] = GEN_INT ((INTVAL (operands[2])) >> 8);
1943       }
1944     else
1945       {
1946         operands[0] = adjust_address (operands[0], QImode, 1);
1947         operands[1] = adjust_address (operands[1], QImode, 1);
1948       }
1949   })
1950
1951 (define_insn "bnothi_msx"
1952   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=m")
1953         (xor:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1954                 (match_operand:HI 2 "single_one_operand" "Y2")))]
1955   "TARGET_H8300SX"
1956   "bnot\\t%V2,%0"
1957   [(set_attr "length" "8")])
1958
1959 (define_insn "xorqi3_1"
1960   [(set (match_operand:QI 0 "bit_operand" "=U,r")
1961         (xor:QI (match_operand:QI 1 "bit_operand" "%0,0")
1962                 (match_operand:QI 2 "h8300_src_operand" "Y2,rQi")))]
1963   "TARGET_H8300SX || register_operand (operands[0], QImode)
1964    || single_one_operand (operands[2], QImode)"
1965   "@
1966    bnot\\t%V2,%R0
1967    xor\\t%X2,%X0"
1968   [(set_attr "length" "8,*")
1969    (set_attr "length_table" "*,logicb")
1970    (set_attr "cc" "none_0hit,set_znv")])
1971
1972 (define_expand "xorqi3"
1973   [(set (match_operand:QI 0 "register_operand" "")
1974         (xor:QI (match_operand:QI 1 "register_operand" "")
1975                 (match_operand:QI 2 "h8300_src_operand" "")))]
1976   ""
1977   "")
1978
1979 (define_expand "xorhi3"
1980   [(set (match_operand:HI 0 "register_operand" "")
1981         (xor:HI (match_operand:HI 1 "register_operand" "")
1982                 (match_operand:HI 2 "h8300_src_operand" "")))]
1983   ""
1984   "")
1985
1986 (define_expand "xorsi3"
1987   [(set (match_operand:SI 0 "register_operand" "")
1988         (xor:SI (match_operand:SI 1 "register_operand" "")
1989                 (match_operand:SI 2 "h8300_src_operand" "")))]
1990   ""
1991   "")
1992
1993 ;; ----------------------------------------------------------------------
1994 ;; {AND,IOR,XOR}{HI3,SI3} PATTERNS
1995 ;; ----------------------------------------------------------------------
1996
1997 ;; We need a separate pattern here because machines other than the
1998 ;; original H8300 don't have to split the 16-bit operand into a pair
1999 ;; of high/low instructions, so we can accept literal addresses, that
2000 ;; have to be loaded into a register on H8300.
2001
2002 (define_insn "*logicalhi3_sn"
2003   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ")
2004         (match_operator:HI 3 "bit_operator"
2005          [(match_operand:HI 1 "h8300_dst_operand" "%0")
2006           (match_operand:HI 2 "h8300_src_operand" "rQi")]))]
2007   "(TARGET_H8300S || TARGET_H8300H) && h8300_operands_match_p (operands)"
2008 {
2009   return output_logical_op (HImode, operands);
2010 }
2011   [(set (attr "length")
2012         (symbol_ref "compute_logical_op_length (HImode, operands)"))
2013    (set (attr "cc")
2014         (symbol_ref "compute_logical_op_cc (HImode, operands)"))])
2015
2016 (define_insn "*logicalsi3_sn"
2017   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ")
2018         (match_operator:SI 3 "bit_operator"
2019          [(match_operand:SI 1 "h8300_dst_operand" "%0")
2020           (match_operand:SI 2 "h8300_src_operand" "rQi")]))]
2021   "(TARGET_H8300S || TARGET_H8300H) && h8300_operands_match_p (operands)"
2022 {
2023   return output_logical_op (SImode, operands);
2024 }
2025   [(set (attr "length")
2026         (symbol_ref "compute_logical_op_length (SImode, operands)"))
2027    (set (attr "cc")
2028         (symbol_ref "compute_logical_op_cc (SImode, operands)"))])
2029
2030 (define_insn "*logicalhi3"
2031   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ")
2032         (match_operator:HI 3 "bit_operator"
2033           [(match_operand:HI 1 "h8300_dst_operand" "%0")
2034            (match_operand:HI 2 "h8300_src_operand" "rQi")]))]
2035   "h8300_operands_match_p (operands)"
2036 {
2037   return output_logical_op (HImode, operands);
2038 }
2039   [(set (attr "length")
2040         (symbol_ref "compute_logical_op_length (HImode, operands)"))
2041    (set (attr "cc")
2042         (symbol_ref "compute_logical_op_cc (HImode, operands)"))])
2043
2044 (define_insn "*logicalsi3"
2045   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ")
2046         (match_operator:SI 3 "bit_operator"
2047          [(match_operand:SI 1 "h8300_dst_operand" "%0")
2048           (match_operand:SI 2 "h8300_src_operand" "rQi")]))]
2049   "h8300_operands_match_p (operands)"
2050 {
2051   return output_logical_op (SImode, operands);
2052 }
2053   [(set (attr "length")
2054         (symbol_ref "compute_logical_op_length (SImode, operands)"))
2055    (set (attr "cc")
2056         (symbol_ref "compute_logical_op_cc (SImode, operands)"))])
2057 \f
2058 ;; ----------------------------------------------------------------------
2059 ;; NEGATION INSTRUCTIONS
2060 ;; ----------------------------------------------------------------------
2061
2062 (define_expand "negqi2"
2063   [(set (match_operand:QI 0 "register_operand" "")
2064         (neg:QI (match_operand:QI 1 "register_operand" "")))]
2065   ""
2066   "")
2067
2068 (define_insn "*negqi2"
2069   [(set (match_operand:QI 0 "h8300_dst_operand" "=rQ")
2070         (neg:QI (match_operand:QI 1 "h8300_dst_operand" "0")))]
2071   ""
2072   "neg  %X0"
2073   [(set_attr "length_table" "unary")
2074    (set_attr "cc" "set_zn")])
2075
2076 (define_expand "neghi2"
2077   [(set (match_operand:HI 0 "register_operand" "")
2078         (neg:HI (match_operand:HI 1 "register_operand" "")))]
2079   ""
2080   {
2081     if (TARGET_H8300)
2082       {
2083         emit_insn (gen_neghi2_h8300 (operands[0], operands[1]));
2084         DONE;
2085       }
2086   })
2087
2088 (define_expand "neghi2_h8300"
2089   [(set (match_dup 2)
2090         (not:HI (match_operand:HI 1 "register_operand" "")))
2091    (set (match_dup 2) (plus:HI (match_dup 2) (const_int 1)))
2092    (set (match_operand:HI 0 "register_operand" "")
2093         (match_dup 2))]
2094   ""
2095   {
2096     operands[2] = gen_reg_rtx (HImode);
2097   })
2098
2099 (define_insn "*neghi2_h8300hs"
2100   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ")
2101         (neg:HI (match_operand:HI 1 "h8300_dst_operand" "0")))]
2102   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
2103   "neg.w        %T0"
2104   [(set_attr "length_table" "unary")
2105    (set_attr "cc" "set_zn")])
2106
2107 (define_expand "negsi2"
2108   [(set (match_operand:SI 0 "register_operand" "")
2109         (neg:SI (match_operand:SI 1 "register_operand" "")))]
2110   ""
2111   {
2112     if (TARGET_H8300)
2113       {
2114         emit_insn (gen_negsi2_h8300 (operands[0], operands[1]));
2115         DONE;
2116       }
2117   })
2118
2119 (define_expand "negsi2_h8300"
2120   [(set (match_dup 2)
2121         (not:SI (match_operand:SI 1 "register_operand" "")))
2122    (set (match_dup 2) (plus:SI (match_dup 2) (const_int 1)))
2123    (set (match_operand:SI 0 "register_operand" "")
2124         (match_dup 2))]
2125   ""
2126   {
2127     operands[2] = gen_reg_rtx (SImode);
2128   })
2129
2130 (define_insn "*negsi2_h8300hs"
2131   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ")
2132         (neg:SI (match_operand:SI 1 "h8300_dst_operand" "0")))]
2133   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
2134   "neg.l        %S0"
2135   [(set_attr "length_table" "unary")
2136    (set_attr "cc" "set_zn")])
2137
2138 (define_expand "negsf2"
2139   [(set (match_operand:SF 0 "register_operand" "")
2140         (neg:SF (match_operand:SF 1 "register_operand" "")))]
2141   ""
2142   "")
2143
2144 (define_insn "*negsf2_h8300"
2145   [(set (match_operand:SF 0 "register_operand" "=r")
2146         (neg:SF (match_operand:SF 1 "register_operand" "0")))]
2147   "TARGET_H8300"
2148   "xor.b\\t#128,%z0"
2149   [(set_attr "length" "2")])
2150
2151 (define_insn "*negsf2_h8300hs"
2152   [(set (match_operand:SF 0 "register_operand" "=r")
2153         (neg:SF (match_operand:SF 1 "register_operand" "0")))]
2154   "TARGET_H8300H || TARGET_H8300S"
2155   "xor.w\\t#32768,%e0"
2156   [(set_attr "length" "4")])
2157 \f
2158 ;; ----------------------------------------------------------------------
2159 ;; ABSOLUTE VALUE INSTRUCTIONS
2160 ;; ----------------------------------------------------------------------
2161
2162 (define_expand "abssf2"
2163   [(set (match_operand:SF 0 "register_operand" "")
2164         (abs:SF (match_operand:SF 1 "register_operand" "")))]
2165   ""
2166   "")
2167
2168 (define_insn "*abssf2_h8300"
2169   [(set (match_operand:SF 0 "register_operand" "=r")
2170         (abs:SF (match_operand:SF 1 "register_operand" "0")))]
2171   "TARGET_H8300"
2172   "and.b\\t#127,%z0"
2173   [(set_attr "length" "2")])
2174
2175 (define_insn "*abssf2_h8300hs"
2176   [(set (match_operand:SF 0 "register_operand" "=r")
2177         (abs:SF (match_operand:SF 1 "register_operand" "0")))]
2178   "TARGET_H8300H || TARGET_H8300S"
2179   "and.w\\t#32767,%e0"
2180   [(set_attr "length" "4")])
2181 \f
2182 ;; ----------------------------------------------------------------------
2183 ;; NOT INSTRUCTIONS
2184 ;; ----------------------------------------------------------------------
2185
2186 (define_expand "one_cmplqi2"
2187   [(set (match_operand:QI 0 "register_operand" "")
2188         (not:QI (match_operand:QI 1 "register_operand" "")))]
2189   ""
2190   "")
2191
2192 (define_insn "*one_cmplqi2"
2193   [(set (match_operand:QI 0 "h8300_dst_operand" "=rQ")
2194         (not:QI (match_operand:QI 1 "h8300_dst_operand" "0")))]
2195   ""
2196   "not  %X0"
2197   [(set_attr "length_table" "unary")
2198    (set_attr "cc" "set_znv")])
2199
2200 (define_expand "one_cmplhi2"
2201   [(set (match_operand:HI 0 "register_operand" "")
2202         (not:HI (match_operand:HI 1 "register_operand" "")))]
2203   ""
2204   "")
2205
2206 (define_insn "*one_cmplhi2_h8300"
2207   [(set (match_operand:HI 0 "register_operand" "=r")
2208         (not:HI (match_operand:HI 1 "register_operand" "0")))]
2209   "TARGET_H8300"
2210   "not  %s0\;not        %t0"
2211   [(set_attr "length" "4")])
2212
2213 (define_insn "*one_cmplhi2_h8300hs"
2214   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ")
2215         (not:HI (match_operand:HI 1 "h8300_dst_operand" "0")))]
2216   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
2217   "not.w        %T0"
2218   [(set_attr "cc" "set_znv")
2219    (set_attr "length_table" "unary")])
2220
2221 (define_expand "one_cmplsi2"
2222   [(set (match_operand:SI 0 "register_operand" "")
2223         (not:SI (match_operand:SI 1 "register_operand" "")))]
2224   ""
2225   "")
2226
2227 (define_insn "*one_cmplsi2_h8300"
2228   [(set (match_operand:SI 0 "register_operand" "=r")
2229         (not:SI (match_operand:SI 1 "register_operand" "0")))]
2230   "TARGET_H8300"
2231   "not  %w0\;not        %x0\;not        %y0\;not        %z0"
2232   [(set_attr "length" "8")])
2233
2234 (define_insn "*one_cmplsi2_h8300hs"
2235   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ")
2236         (not:SI (match_operand:SI 1 "h8300_dst_operand" "0")))]
2237   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
2238   "not.l        %S0"
2239   [(set_attr "cc" "set_znv")
2240    (set_attr "length_table" "unary")])
2241 \f
2242 ;; ----------------------------------------------------------------------
2243 ;; JUMP INSTRUCTIONS
2244 ;; ----------------------------------------------------------------------
2245
2246 ;; Conditional jump instructions
2247
2248 (define_expand "cbranchqi4"
2249   [(use (match_operator 0 "ordered_comparison_operator"
2250          [(match_operand:QI 1 "h8300_dst_operand" "")
2251           (match_operand:QI 2 "h8300_src_operand" "")]))
2252    (use (match_operand 3 ""))]
2253   ""
2254   {
2255     h8300_expand_branch (operands);
2256     DONE;
2257   })
2258
2259 (define_expand "cbranchhi4"
2260   [(use (match_operator 0 "ordered_comparison_operator"
2261          [(match_operand:HI 1 "h8300_dst_operand" "")
2262           (match_operand:HI 2 "h8300_src_operand" "")]))
2263    (use (match_operand 3 ""))]
2264   ""
2265   {
2266     /* Force operand1 into a register if we're compiling
2267        for the H8/300.  */
2268     if ((GET_CODE (operands[2]) != REG && operands[2] != const0_rtx)
2269         && TARGET_H8300)
2270       operands[2] = force_reg (HImode, operands[2]);
2271     h8300_expand_branch (operands); 
2272     DONE;
2273   })
2274
2275 (define_expand "cbranchsi4"
2276   [(use (match_operator 0 "ordered_comparison_operator"
2277          [(match_operand:SI 1 "h8300_dst_operand" "")
2278           (match_operand:SI 2 "h8300_src_operand" "")]))
2279    (use (match_operand 3 ""))]
2280   "TARGET_H8300H || TARGET_H8300S"
2281   {
2282     h8300_expand_branch (operands);
2283     DONE;
2284   })
2285
2286 (define_insn "branch_true"
2287   [(set (pc)
2288         (if_then_else (match_operator 1 "comparison_operator"
2289                        [(cc0) (const_int 0)])
2290                       (label_ref (match_operand 0 "" ""))
2291                       (pc)))]
2292   ""
2293 {
2294   if ((cc_status.flags & CC_OVERFLOW_UNUSABLE) != 0
2295       && (GET_CODE (operands[1]) == GT
2296           || GET_CODE (operands[1]) == GE
2297           || GET_CODE (operands[1]) == LE
2298           || GET_CODE (operands[1]) == LT))
2299     {
2300       cc_status.flags &= ~CC_OVERFLOW_UNUSABLE;
2301       return 0;
2302     }
2303
2304   if (get_attr_length (insn) == 2)
2305     return "b%j1        %l0";
2306   else if (get_attr_length (insn) == 4)
2307     return "b%j1        %l0:16";
2308   else
2309     return "b%k1        .Lh8BR%=\;jmp   @%l0\\n.Lh8BR%=:";
2310 }
2311  [(set_attr "type" "branch")
2312    (set_attr "cc" "none")])
2313
2314 (define_insn "branch_false"
2315   [(set (pc)
2316         (if_then_else (match_operator 1 "comparison_operator"
2317                        [(cc0) (const_int 0)])
2318                       (pc)
2319                       (label_ref (match_operand 0 "" ""))))]
2320   ""
2321 {
2322   if ((cc_status.flags & CC_OVERFLOW_UNUSABLE) != 0
2323       && (GET_CODE (operands[1]) == GT
2324           || GET_CODE (operands[1]) == GE
2325           || GET_CODE (operands[1]) == LE
2326           || GET_CODE (operands[1]) == LT))
2327     {
2328       cc_status.flags &= ~CC_OVERFLOW_UNUSABLE;
2329       return 0;
2330     }
2331
2332   if (get_attr_length (insn) == 2)
2333     return "b%k1        %l0";
2334   else if (get_attr_length (insn) == 4)
2335     return "b%k1        %l0:16";
2336   else
2337     return "b%j1        .Lh8BR%=\;jmp   @%l0\\n.Lh8BR%=:";
2338 }
2339   [(set_attr "type" "branch")
2340    (set_attr "cc" "none")])
2341
2342 (define_insn "*brabc"
2343   [(set (pc)
2344         (if_then_else (eq (zero_extract (match_operand:QI 1 "bit_memory_operand" "WU")
2345                                         (const_int 1)
2346                                         (match_operand:QI 2 "immediate_operand" "n"))
2347                           (const_int 0))
2348                       (label_ref (match_operand 0 "" ""))
2349                       (pc)))]
2350   "TARGET_H8300SX"
2351 {
2352   switch (get_attr_length (insn)
2353           - h8300_insn_length_from_table (insn, operands))
2354     {
2355     case 2:
2356       return "bra/bc    %2,%R1,%l0";
2357     case 4:
2358       return "bra/bc    %2,%R1,%l0:16";
2359     default:
2360       return "bra/bs    %2,%R1,.Lh8BR%=\;jmp    @%l0\\n.Lh8BR%=:";
2361     }
2362 }
2363   [(set_attr "type" "bitbranch")
2364    (set_attr "length_table" "bitbranch")
2365    (set_attr "cc" "none")])
2366
2367 (define_insn "*brabs"
2368   [(set (pc)
2369         (if_then_else (ne (zero_extract (match_operand:QI 1 "bit_memory_operand" "WU")
2370                                         (const_int 1)
2371                                         (match_operand:QI 2 "immediate_operand" "n"))
2372                           (const_int 0))
2373                       (label_ref (match_operand 0 "" ""))
2374                       (pc)))]
2375   "TARGET_H8300SX"
2376 {
2377   switch (get_attr_length (insn)
2378           - h8300_insn_length_from_table (insn, operands))
2379     {
2380     case 2:
2381       return "bra/bs    %2,%R1,%l0";
2382     case 4:
2383       return "bra/bs    %2,%R1,%l0:16";
2384     default:
2385       return "bra/bc    %2,%R1,.Lh8BR%=\;jmp    @%l0\\n.Lh8BR%=:";
2386     }
2387 }
2388   [(set_attr "type" "bitbranch")
2389    (set_attr "length_table" "bitbranch")
2390    (set_attr "cc" "none")])
2391
2392 ;; Unconditional and other jump instructions.
2393
2394 (define_insn "jump"
2395   [(set (pc)
2396         (label_ref (match_operand 0 "" "")))]
2397   ""
2398 {
2399   if (final_sequence != 0)
2400     {
2401       if (get_attr_length (insn) == 2)
2402         return "bra/s   %l0";
2403       else
2404         {
2405           /* The branch isn't short enough to use bra/s.  Output the
2406              branch and delay slot in their normal order.
2407
2408              If this is a backward branch, it will now be branching two
2409              bytes further than previously thought.  The length-based
2410              test for bra vs. jump is very conservative though, so the
2411              branch will still be within range.  */
2412           rtvec vec;
2413           int seen;
2414
2415           vec = XVEC (final_sequence, 0);
2416           final_sequence = 0;
2417           final_scan_insn (RTVEC_ELT (vec, 1), asm_out_file, optimize, 1, & seen);
2418           final_scan_insn (RTVEC_ELT (vec, 0), asm_out_file, optimize, 1, & seen);
2419           INSN_DELETED_P (RTVEC_ELT (vec, 1)) = 1;
2420           return "";
2421         }
2422     }
2423   else if (get_attr_length (insn) == 2)
2424     return "bra %l0";
2425   else if (get_attr_length (insn) == 4)
2426     return "bra %l0:16";
2427   else
2428     return "jmp @%l0";
2429 }
2430   [(set_attr "type" "branch")
2431    (set (attr "delay_slot")
2432         (if_then_else (match_test "TARGET_H8300SX")
2433                       (const_string "jump")
2434                       (const_string "none")))
2435    (set_attr "cc" "none")])
2436
2437 ;; This is a define expand, because pointers may be either 16 or 32 bits.
2438
2439 (define_expand "tablejump"
2440   [(parallel [(set (pc) (match_operand 0 "register_operand" ""))
2441               (use (label_ref (match_operand 1 "" "")))])]
2442   ""
2443   "")
2444
2445 (define_insn "*tablejump_h8300"
2446   [(set (pc) (match_operand:HI 0 "register_operand" "r"))
2447    (use (label_ref (match_operand 1 "" "")))]
2448   "TARGET_H8300"
2449   "jmp  @%0"
2450   [(set_attr "cc" "none")
2451    (set_attr "length" "2")])
2452
2453 (define_insn "*tablejump_h8300hs_advanced"
2454   [(set (pc) (match_operand:SI 0 "register_operand" "r"))
2455    (use (label_ref (match_operand 1 "" "")))]
2456   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_NORMAL_MODE"
2457   "jmp  @%0"
2458   [(set_attr "cc" "none")
2459    (set_attr "length" "2")])
2460
2461 (define_insn "*tablejump_h8300hs_normal"
2462   [(set (pc) (match_operand:HI 0 "register_operand" "r"))
2463    (use (label_ref (match_operand 1 "" "")))]
2464   "(TARGET_H8300H || TARGET_H8300S) && TARGET_NORMAL_MODE"
2465   "jmp @%S0"
2466   [(set_attr "cc" "none")
2467    (set_attr "length" "2")])
2468
2469 ;; This is a define expand, because pointers may be either 16 or 32 bits.
2470
2471 (define_expand "indirect_jump"
2472   [(set (pc) (match_operand 0 "jump_address_operand" ""))]
2473   ""
2474   "")
2475
2476 (define_insn "*indirect_jump_h8300"
2477   [(set (pc) (match_operand:HI 0 "jump_address_operand" "Vr"))]
2478   "TARGET_H8300"
2479   "jmp  @%0"
2480   [(set_attr "cc" "none")
2481    (set_attr "length" "2")])
2482
2483 (define_insn "*indirect_jump_h8300hs_advanced"
2484   [(set (pc) (match_operand:SI 0 "jump_address_operand" "Vr"))]
2485   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_NORMAL_MODE"
2486   "jmp @%0"
2487   [(set_attr "cc" "none")
2488    (set_attr "length" "2")])
2489
2490 (define_insn "*indirect_jump_h8300hs_normal"
2491   [(set (pc) (match_operand:HI 0 "jump_address_operand" "Vr"))]
2492   "(TARGET_H8300H || TARGET_H8300S) && TARGET_NORMAL_MODE"
2493   "jmp @%S0"
2494   [(set_attr "cc" "none")
2495    (set_attr "length" "2")])
2496
2497 ;; Call subroutine with no return value.
2498
2499 ;; ??? Even though we use HImode here, this works on the H8/300H and H8S.
2500
2501 (define_insn "call"
2502   [(call (match_operand:QI 0 "call_insn_operand" "or")
2503          (match_operand:HI 1 "general_operand" "g"))]
2504   ""
2505 {
2506   if (GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF
2507       && SYMBOL_REF_FLAG (XEXP (operands[0], 0)))
2508     return "jsr\\t@%0:8";
2509   else
2510     return "jsr\\t%0";
2511 }
2512   [(set_attr "type" "call")
2513    (set (attr "length")
2514         (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
2515                       (const_int 2)
2516                       (const_int 4)))])
2517
2518 ;; Call subroutine, returning value in operand 0
2519 ;; (which must be a hard register).
2520
2521 ;; ??? Even though we use HImode here, this works on the H8/300H and H8S.
2522
2523 (define_insn "call_value"
2524   [(set (match_operand 0 "" "=r")
2525         (call (match_operand:QI 1 "call_insn_operand" "or")
2526               (match_operand:HI 2 "general_operand" "g")))]
2527   ""
2528 {
2529   if (GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
2530       && SYMBOL_REF_FLAG (XEXP (operands[1], 0)))
2531     return "jsr\\t@%1:8";
2532   else
2533     return "jsr\\t%1";
2534 }
2535   [(set_attr "type" "call")
2536    (set (attr "length")
2537         (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
2538                       (const_int 2)
2539                       (const_int 4)))])
2540
2541 (define_insn "nop"
2542   [(const_int 0)]
2543   ""
2544   "nop"
2545   [(set_attr "cc" "none")
2546    (set_attr "length" "2")])
2547 \f
2548 ;; ----------------------------------------------------------------------
2549 ;; PROLOGUE/EPILOGUE-RELATED INSTRUCTIONS
2550 ;; ----------------------------------------------------------------------
2551
2552 (define_expand "push_h8300"
2553   [(set (mem:HI (pre_dec:HI (reg:HI SP_REG)))
2554         (match_operand:HI 0 "register_operand" ""))]
2555   "TARGET_H8300"
2556   "")
2557
2558 (define_expand "push_h8300hs_advanced"
2559   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
2560         (match_operand:SI 0 "register_operand" ""))]
2561   "TARGET_H8300H && TARGET_H8300S && !TARGET_NORMAL_MODE"
2562   "")
2563
2564 (define_expand "push_h8300hs_normal"
2565   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
2566         (match_operand:SI 0 "register_operand" ""))]
2567   "TARGET_H8300H && TARGET_H8300S && TARGET_NORMAL_MODE"
2568   "")
2569
2570 (define_expand "pop_h8300"
2571   [(set (match_operand:HI 0 "register_operand" "")
2572         (mem:HI (post_inc:HI (reg:HI SP_REG))))]
2573   "TARGET_H8300"
2574   "")
2575
2576 (define_expand "pop_h8300hs_advanced"
2577   [(set (match_operand:SI 0 "register_operand" "")
2578         (mem:SI (post_inc:SI (reg:SI SP_REG))))]
2579   "TARGET_H8300H && TARGET_H8300S && !TARGET_NORMAL_MODE"
2580   "")
2581
2582 (define_expand "pop_h8300hs_normal"
2583   [(set (match_operand:SI 0 "register_operand" "")
2584         (mem:SI (post_inc:HI (reg:HI SP_REG))))]
2585   "TARGET_H8300H && TARGET_H8300S && TARGET_NORMAL_MODE"
2586   "")
2587
2588 (define_insn "ldm_h8300sx"
2589   [(match_parallel           0 "h8300_ldm_parallel"
2590     [(set (match_operand:SI 1 "register_operand" "")
2591           (match_operand:SI 2 "memory_operand" ""))])]
2592   "TARGET_H8300S"
2593 {
2594   operands[3] = SET_DEST (XVECEXP (operands[0], 0,
2595                                    XVECLEN (operands[0], 0) - 2));
2596   return "ldm.l\t@er7+,%S1-%S3";
2597 }
2598   [(set_attr "cc" "none")
2599    (set_attr "length" "4")])
2600
2601 (define_insn "stm_h8300sx"
2602   [(match_parallel           0 "h8300_stm_parallel"
2603     [(set (match_operand:SI 1 "memory_operand" "")
2604           (match_operand:SI 2 "register_operand" ""))])]
2605   "TARGET_H8300S"
2606 {
2607   operands[3] = SET_SRC (XVECEXP (operands[0], 0,
2608                                   XVECLEN (operands[0], 0) - 2));
2609   return "stm.l\t%S2-%S3,@-er7";
2610 }
2611   [(set_attr "cc" "none")
2612    (set_attr "length" "4")])
2613
2614 (define_insn "return_h8sx"
2615   [(match_parallel           0 "h8300_return_parallel"
2616     [(return)
2617      (set (match_operand:SI 1 "register_operand" "")
2618           (match_operand:SI 2 "memory_operand" ""))])]
2619   "TARGET_H8300SX"
2620 {
2621   operands[3] = SET_DEST (XVECEXP (operands[0], 0,
2622                                    XVECLEN (operands[0], 0) - 2));
2623   if (h8300_current_function_interrupt_function_p ()
2624       || h8300_current_function_monitor_function_p ())
2625     return "rte/l\t%S1-%S3";
2626   else
2627     return "rts/l\t%S1-%S3";
2628 }
2629   [(set_attr "cc" "none")
2630    (set_attr "can_delay" "no")
2631    (set_attr "length" "2")])
2632
2633 (define_expand "return"
2634   [(return)]
2635   "h8300_can_use_return_insn_p ()"
2636   "")
2637
2638 (define_insn "*return_1"
2639   [(return)]
2640   "reload_completed"
2641 {
2642   if (h8300_current_function_interrupt_function_p ()
2643       || h8300_current_function_monitor_function_p ())
2644     return "rte";
2645   else
2646     return "rts";
2647 }
2648   [(set_attr "cc" "none")
2649    (set_attr "can_delay" "no")
2650    (set_attr "length" "2")])
2651
2652 (define_expand "prologue"
2653   [(const_int 0)]
2654   ""
2655   {
2656     h8300_expand_prologue (); 
2657     DONE;
2658   })
2659
2660 (define_expand "epilogue"
2661   [(return)]
2662   ""
2663   {
2664     h8300_expand_epilogue (); 
2665     DONE;
2666   })
2667
2668 (define_insn "monitor_prologue"
2669   [(unspec_volatile [(const_int 0)] UNSPEC_MONITOR)]
2670   ""
2671 {
2672   if (TARGET_H8300)
2673     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";
2674   else if (TARGET_H8300H && TARGET_NORMAL_MODE)
2675     return "subs\\t#2,er7\;mov.l\\ter0,@-er7\;stc\\tccr,r0l\;mov.b\\tr0l,@(4,er7)\;mov.l\\t@er7+,er0\;orc\\t#128,ccr";
2676   else if (TARGET_H8300H)
2677     return "mov.l\\ter0,@-er7\;stc\\tccr,r0l\;mov.b\\tr0l,@(4,er7)\;mov.l\\t@er7+,er0\;orc\\t#128,ccr";
2678   else if (TARGET_H8300S && TARGET_NORMAL_MODE)
2679     return "subs\\t#2,er7\;stc\texr,@-er7\;mov.l\\ter0,@-er7\;stc\tccr,r0l\;mov.b\tr0l,@(6,er7)\;mov.l\\t@er7+,er0\;orc\t#128,ccr";
2680   else if (TARGET_H8300S)
2681     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";
2682   gcc_unreachable ();
2683 }
2684   [(set_attr "length" "20")])
2685 \f
2686 ;; ----------------------------------------------------------------------
2687 ;; EXTEND INSTRUCTIONS
2688 ;; ----------------------------------------------------------------------
2689
2690 (define_expand "zero_extendqihi2"
2691   [(set (match_operand:HI 0 "register_operand" "")
2692         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "")))]
2693   ""
2694   "")
2695
2696 (define_insn "*zero_extendqihi2_h8300"
2697   [(set (match_operand:HI 0 "register_operand" "=r,r")
2698         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2699   "TARGET_H8300"
2700   "@
2701   mov.b #0,%t0
2702   #"
2703   [(set_attr "length" "2,10")])
2704
2705 (define_insn "*zero_extendqihi2_h8300hs"
2706   [(set (match_operand:HI 0 "register_operand" "=r,r")
2707         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2708   "TARGET_H8300H || TARGET_H8300S"
2709   "@
2710   extu.w        %T0
2711   #"
2712   [(set_attr "length" "2,10")
2713    (set_attr "cc" "set_znv,set_znv")])
2714
2715 ;; Split the zero extension of a general operand (actually a memory
2716 ;; operand) into a load of the operand and the actual zero extension
2717 ;; so that 1) the length will be accurate, and 2) the zero extensions
2718 ;; appearing at the end of basic blocks may be merged.
2719
2720 (define_split
2721   [(set (match_operand:HI 0 "register_operand" "")
2722         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "")))]
2723   "reload_completed"
2724   [(set (match_dup 2)
2725         (match_dup 1))
2726    (set (match_dup 0)
2727         (zero_extend:HI (match_dup 2)))]
2728   {
2729     operands[2] = gen_rtx_REG (QImode, REGNO (operands[0]));
2730   })
2731
2732 (define_expand "zero_extendqisi2"
2733   [(set (match_operand:SI 0 "register_operand" "")
2734         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "")))]
2735   ""
2736   {
2737     if (TARGET_H8300SX)
2738       operands[1] = force_reg (QImode, operands[1]);
2739   })
2740
2741 (define_insn "*zero_extendqisi2_h8300"
2742   [(set (match_operand:SI 0 "register_operand" "=r,r")
2743         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2744   "TARGET_H8300"
2745   "@
2746   mov.b #0,%x0\;sub.w   %e0,%e0
2747   mov.b %R1,%w0\;mov.b  #0,%x0\;sub.w   %e0,%e0"
2748   [(set_attr "length" "4,8")])
2749
2750 (define_insn "*zero_extendqisi2_h8300hs"
2751   [(set (match_operand:SI 0 "register_operand" "=r,r")
2752         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2753   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX"
2754   "#")
2755
2756 (define_split
2757   [(set (match_operand:SI 0 "register_operand" "")
2758         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "")))]
2759   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX
2760     && reg_overlap_mentioned_p (operands[0], operands[1])
2761     && reload_completed"
2762   [(set (match_dup 2)
2763         (match_dup 1))
2764    (set (match_dup 3)
2765         (zero_extend:HI (match_dup 2)))
2766    (set (match_dup 0)
2767         (zero_extend:SI (match_dup 3)))]
2768   {
2769     operands[2] = gen_lowpart (QImode, operands[0]);
2770     operands[3] = gen_lowpart (HImode, operands[0]);
2771   })
2772
2773 (define_split
2774   [(set (match_operand:SI 0 "register_operand" "")
2775         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "")))]
2776   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX
2777     && !reg_overlap_mentioned_p (operands[0], operands[1])
2778     && reload_completed"
2779   [(set (match_dup 0)
2780         (const_int 0))
2781    (set (strict_low_part (match_dup 2))
2782         (match_dup 1))]
2783   {
2784     operands[2] = gen_rtx_REG (QImode, REGNO (operands[0]));
2785   })
2786
2787 (define_insn "*zero_extendqisi2_h8sx"
2788   [(set (match_operand:SI 0 "register_operand" "=r")
2789         (zero_extend:SI (match_operand:QI 1 "register_operand" "0")))]
2790   "TARGET_H8300SX"
2791   "extu.l\t#2,%0"
2792   [(set_attr "length" "2")
2793    (set_attr "cc" "set_znv")])
2794
2795 (define_expand "zero_extendhisi2"
2796   [(set (match_operand:SI 0 "register_operand" "")
2797         (zero_extend:SI (match_operand:HI 1 "register_operand" "")))]
2798   ""
2799   "")
2800
2801 ;; %e prints the high part of a CONST_INT, not the low part.  Arggh.
2802 (define_insn "*zero_extendhisi2_h8300"
2803   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
2804         (zero_extend:SI (match_operand:HI 1 "general_operand_src" "0,i,g>")))]
2805   "TARGET_H8300"
2806   "@
2807   sub.w %e0,%e0
2808   mov.w %f1,%f0\;sub.w  %e0,%e0
2809   mov.w %e1,%f0\;sub.w  %e0,%e0"
2810   [(set_attr "length" "2,4,6")])
2811
2812 (define_insn "*zero_extendhisi2_h8300hs"
2813   [(set (match_operand:SI 0 "register_operand" "=r")
2814         (zero_extend:SI (match_operand:HI 1 "register_operand" "0")))]
2815   "TARGET_H8300H || TARGET_H8300S"
2816   "extu.l       %S0"
2817   [(set_attr "length" "2")
2818    (set_attr "cc" "set_znv")])
2819
2820 (define_expand "extendqihi2"
2821   [(set (match_operand:HI 0 "register_operand" "")
2822         (sign_extend:HI (match_operand:QI 1 "register_operand" "")))]
2823   ""
2824   "")
2825
2826 (define_insn "*extendqihi2_h8300"
2827   [(set (match_operand:HI 0 "register_operand" "=r,r")
2828         (sign_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2829   "TARGET_H8300"
2830   "@
2831   bld   #7,%s0\;subx    %t0,%t0
2832   mov.b %R1,%s0\;bld    #7,%s0\;subx    %t0,%t0"
2833   [(set_attr "length" "4,8")])
2834
2835 (define_insn "*extendqihi2_h8300hs"
2836   [(set (match_operand:HI 0 "register_operand" "=r")
2837         (sign_extend:HI (match_operand:QI 1 "register_operand" "0")))]
2838   "TARGET_H8300H || TARGET_H8300S"
2839   "exts.w       %T0"
2840   [(set_attr "length" "2")
2841    (set_attr "cc" "set_znv")])
2842
2843 (define_expand "extendqisi2"
2844   [(set (match_operand:SI 0 "register_operand" "")
2845         (sign_extend:SI (match_operand:QI 1 "register_operand" "")))]
2846   ""
2847   "")
2848
2849 (define_insn "*extendqisi2_h8300"
2850   [(set (match_operand:SI 0 "register_operand" "=r,r")
2851         (sign_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2852   "TARGET_H8300"
2853   "@
2854   bld   #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0
2855   mov.b %R1,%w0\;bld    #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0"
2856   [(set_attr "length" "8,12")])
2857
2858 ;; The following pattern is needed because without the pattern, the
2859 ;; combiner would split (sign_extend:SI (reg:QI)) into two 24-bit
2860 ;; shifts, one ashift and one ashiftrt.
2861
2862 (define_insn_and_split "*extendqisi2_h8300hs"
2863   [(set (match_operand:SI 0 "register_operand" "=r")
2864         (sign_extend:SI (match_operand:QI 1 "register_operand" "0")))]
2865   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX"
2866   "#"
2867   "&& reload_completed"
2868   [(set (match_dup 2)
2869         (sign_extend:HI (match_dup 1)))
2870    (set (match_dup 0)
2871         (sign_extend:SI (match_dup 2)))]
2872   {
2873     operands[2] = gen_rtx_REG (HImode, REGNO (operands[0]));
2874   })
2875
2876 (define_insn "*extendqisi2_h8sx"
2877   [(set (match_operand:SI 0 "register_operand" "=r")
2878         (sign_extend:SI (match_operand:QI 1 "register_operand" "0")))]
2879   "TARGET_H8300SX"
2880   "exts.l\t#2,%0"
2881   [(set_attr "length" "2")
2882    (set_attr "cc" "set_znv")])
2883
2884 (define_expand "extendhisi2"
2885   [(set (match_operand:SI 0 "register_operand" "")
2886         (sign_extend:SI (match_operand:HI 1 "register_operand" "")))]
2887   ""
2888   "")
2889
2890 (define_insn "*extendhisi2_h8300"
2891   [(set (match_operand:SI 0 "register_operand" "=r,r")
2892         (sign_extend:SI (match_operand:HI 1 "general_operand_src" "0,g>")))]
2893   "TARGET_H8300"
2894   "@
2895   bld   #7,%x0\;subx    %y0,%y0\;subx   %z0,%z0
2896   mov.w %T1,%f0\;bld    #7,%x0\;subx    %y0,%y0\;subx   %z0,%z0"
2897   [(set_attr "length" "6,10")])
2898
2899 (define_insn "*extendhisi2_h8300hs"
2900   [(set (match_operand:SI 0 "register_operand" "=r")
2901         (sign_extend:SI (match_operand:HI 1 "register_operand" "0")))]
2902   "TARGET_H8300H || TARGET_H8300S"
2903   "exts.l       %S0"
2904   [(set_attr "length" "2")
2905    (set_attr "cc" "set_znv")])
2906 \f
2907 ;; ----------------------------------------------------------------------
2908 ;; SHIFTS
2909 ;; ----------------------------------------------------------------------
2910 ;;
2911 ;; We make some attempt to provide real efficient shifting.  One example is
2912 ;; doing an 8-bit shift of a 16-bit value by moving a byte reg into the other
2913 ;; reg and moving 0 into the former reg.
2914 ;;
2915 ;; We also try to achieve this in a uniform way.  IE: We don't try to achieve
2916 ;; this in both rtl and at insn emit time.  Ideally, we'd use rtl as that would
2917 ;; give the optimizer more cracks at the code.  However, we wish to do things
2918 ;; like optimizing shifting the sign bit to bit 0 by rotating the other way.
2919 ;; There is rtl to handle this (rotate + and), but the H8/300 doesn't handle
2920 ;; 16-bit rotates.  Also, if we emit complicated rtl, combine may not be able
2921 ;; to detect cases it can optimize.
2922 ;;
2923 ;; For these and other fuzzy reasons, I've decided to go the less pretty but
2924 ;; easier "do it at insn emit time" route.
2925
2926 ;; QI BIT SHIFTS
2927
2928 (define_expand "ashlqi3"
2929   [(set (match_operand:QI 0 "register_operand" "")
2930         (ashift:QI (match_operand:QI 1 "register_operand" "")
2931                    (match_operand:QI 2 "nonmemory_operand" "")))]
2932   ""
2933   {
2934     if (expand_a_shift (QImode, ASHIFT, operands)) 
2935     DONE;
2936   })
2937
2938 (define_expand "ashrqi3"
2939   [(set (match_operand:QI 0 "register_operand" "")
2940         (ashiftrt:QI (match_operand:QI 1 "register_operand" "")
2941                      (match_operand:QI 2 "nonmemory_operand" "")))]
2942   ""
2943   {
2944     if (expand_a_shift (QImode, ASHIFTRT, operands)) 
2945     DONE;
2946   })
2947
2948 (define_expand "lshrqi3"
2949   [(set (match_operand:QI 0 "register_operand" "")
2950         (lshiftrt:QI (match_operand:QI 1 "register_operand" "")
2951                      (match_operand:QI 2 "nonmemory_operand" "")))]
2952   ""
2953   {
2954     if (expand_a_shift (QImode, LSHIFTRT, operands)) 
2955     DONE;
2956   })
2957
2958 (define_insn ""
2959   [(set (match_operand:QI 0 "h8300_dst_operand" "=rQ")
2960         (match_operator:QI 3 "h8sx_unary_shift_operator"
2961          [(match_operand:QI 1 "h8300_dst_operand" "0")
2962           (match_operand:QI 2 "const_int_operand" "")]))]
2963   "h8300_operands_match_p (operands)"
2964
2965   return output_h8sx_shift (operands, 'b', 'X'); 
2966 }
2967   [(set_attr "length_table" "unary")
2968    (set_attr "cc" "set_znv")])
2969
2970 (define_insn ""
2971   [(set (match_operand:QI 0 "register_operand" "=r")
2972         (match_operator:QI 3 "h8sx_binary_shift_operator"
2973          [(match_operand:QI 1 "register_operand" "0")
2974           (match_operand:QI 2 "nonmemory_operand" "r P3>X")]))]
2975   ""
2976 {
2977   return output_h8sx_shift (operands, 'b', 'X'); 
2978 }
2979   [(set_attr "length" "4")
2980    (set_attr "cc" "set_znv")])
2981
2982 (define_insn "*shiftqi"
2983   [(set (match_operand:QI 0 "register_operand" "=r,r")
2984         (match_operator:QI 3 "nshift_operator"
2985          [(match_operand:QI 1 "register_operand" "0,0")
2986           (match_operand:QI 2 "nonmemory_operand" "R,rn")]))
2987    (clobber (match_scratch:QI 4 "=X,&r"))]
2988   ""
2989 {
2990   return output_a_shift (operands);
2991 }
2992   [(set (attr "length")
2993         (symbol_ref "compute_a_shift_length (insn, operands)"))
2994    (set (attr "cc")
2995         (symbol_ref "compute_a_shift_cc (insn, operands)"))])
2996
2997 ;; HI BIT SHIFTS
2998
2999 (define_expand "ashlhi3"
3000   [(set (match_operand:HI 0 "register_operand" "")
3001         (ashift:HI (match_operand:HI 1 "register_operand" "")
3002                    (match_operand:QI 2 "nonmemory_operand" "")))]
3003   ""
3004   {
3005     if (expand_a_shift (HImode, ASHIFT, operands)) 
3006     DONE;
3007   })
3008
3009 (define_expand "lshrhi3"
3010   [(set (match_operand:HI 0 "register_operand" "")
3011         (lshiftrt:HI (match_operand:HI 1 "register_operand" "")
3012                      (match_operand:QI 2 "nonmemory_operand" "")))]
3013   ""
3014   {
3015     if (expand_a_shift (HImode, LSHIFTRT, operands)) 
3016     DONE;
3017   })
3018
3019 (define_expand "ashrhi3"
3020   [(set (match_operand:HI 0 "register_operand" "")
3021         (ashiftrt:HI (match_operand:HI 1 "register_operand" "")
3022                      (match_operand:QI 2 "nonmemory_operand" "")))]
3023   ""
3024   {
3025     if (expand_a_shift (HImode, ASHIFTRT, operands)) 
3026     DONE;
3027   })
3028
3029 (define_insn ""
3030   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ")
3031         (match_operator:HI 3 "h8sx_unary_shift_operator"
3032          [(match_operand:HI 1 "h8300_dst_operand" "0")
3033           (match_operand:QI 2 "const_int_operand" "")]))]
3034   "h8300_operands_match_p (operands)"
3035
3036   return output_h8sx_shift (operands, 'w', 'T'); 
3037 }
3038   [(set_attr "length_table" "unary")
3039    (set_attr "cc" "set_znv")])
3040
3041 (define_insn ""
3042   [(set (match_operand:HI 0 "register_operand" "=r")
3043         (match_operator:HI 3 "h8sx_binary_shift_operator"
3044          [(match_operand:HI 1 "register_operand" "0")
3045           (match_operand:QI 2 "nonmemory_operand" "r P4>X")]))]
3046   ""
3047 {
3048   return output_h8sx_shift (operands, 'w', 'T'); 
3049 }
3050   [(set_attr "length" "4")
3051    (set_attr "cc" "set_znv")])
3052
3053 (define_insn "*shifthi"
3054   [(set (match_operand:HI 0 "register_operand" "=r,r")
3055         (match_operator:HI 3 "nshift_operator"
3056          [(match_operand:HI 1 "register_operand" "0,0")
3057           (match_operand:QI 2 "nonmemory_operand" "S,rn")]))
3058    (clobber (match_scratch:QI 4 "=X,&r"))]
3059   ""
3060 {
3061   return output_a_shift (operands);
3062 }
3063   [(set (attr "length")
3064         (symbol_ref "compute_a_shift_length (insn, operands)"))
3065    (set (attr "cc")
3066         (symbol_ref "compute_a_shift_cc (insn, operands)"))])
3067
3068 ;;  SI BIT SHIFTS
3069
3070 (define_expand "ashlsi3"
3071   [(set (match_operand:SI 0 "register_operand" "")
3072         (ashift:SI (match_operand:SI 1 "register_operand" "")
3073                    (match_operand:QI 2 "nonmemory_operand" "")))]
3074   ""
3075   {
3076     if (expand_a_shift (SImode, ASHIFT, operands)) 
3077     DONE;
3078   })
3079
3080 (define_expand "lshrsi3"
3081   [(set (match_operand:SI 0 "register_operand" "")
3082         (lshiftrt:SI (match_operand:SI 1 "register_operand" "")
3083                      (match_operand:QI 2 "nonmemory_operand" "")))]
3084   ""
3085   {
3086     if (expand_a_shift (SImode, LSHIFTRT, operands)) 
3087     DONE;
3088   })
3089
3090 (define_expand "ashrsi3"
3091   [(set (match_operand:SI 0 "register_operand" "")
3092         (ashiftrt:SI (match_operand:SI 1 "register_operand" "")
3093                      (match_operand:QI 2 "nonmemory_operand" "")))]
3094   ""
3095   {
3096     if (expand_a_shift (SImode, ASHIFTRT, operands)) 
3097     DONE;
3098   })
3099
3100 (define_insn ""
3101   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ")
3102         (match_operator:SI 3 "h8sx_unary_shift_operator"
3103          [(match_operand:SI 1 "h8300_dst_operand" "0")
3104           (match_operand:QI 2 "const_int_operand" "")]))]
3105   "h8300_operands_match_p (operands)"
3106 {
3107   return output_h8sx_shift (operands, 'l', 'S'); 
3108 }
3109   [(set_attr "length_table" "unary")
3110    (set_attr "cc" "set_znv")])
3111
3112 (define_insn ""
3113   [(set (match_operand:SI 0 "register_operand" "=r")
3114         (match_operator:SI 3 "h8sx_binary_shift_operator"
3115          [(match_operand:SI 1 "register_operand" "0")
3116           (match_operand:QI 2 "nonmemory_operand" "r P5>X")]))]
3117   ""
3118
3119   return output_h8sx_shift (operands, 'l', 'S'); 
3120 }
3121   [(set_attr "length" "4")
3122    (set_attr "cc" "set_znv")])
3123
3124 (define_insn "*shiftsi"
3125   [(set (match_operand:SI 0 "register_operand" "=r,r")
3126         (match_operator:SI 3 "nshift_operator"
3127          [(match_operand:SI 1 "register_operand" "0,0")
3128           (match_operand:QI 2 "nonmemory_operand" "T,rn")]))
3129    (clobber (match_scratch:QI 4 "=X,&r"))]
3130   ""
3131 {
3132   return output_a_shift (operands);
3133 }
3134   [(set (attr "length")
3135         (symbol_ref "compute_a_shift_length (insn, operands)"))
3136    (set (attr "cc")
3137         (symbol_ref "compute_a_shift_cc (insn, operands)"))])
3138
3139 ;; Split a variable shift into a loop.  If the register containing
3140 ;; the shift count dies, then we just use that register.
3141
3142 (define_split
3143   [(set (match_operand 0 "register_operand" "")
3144         (match_operator 2 "nshift_operator"
3145          [(match_dup 0)
3146           (match_operand:QI 1 "register_operand" "")]))
3147    (clobber (match_operand:QI 3 "register_operand" ""))]
3148   "epilogue_completed
3149    && find_regno_note (insn, REG_DEAD, REGNO (operands[1]))"
3150   [(set (cc0) (compare (match_dup 1) (const_int 0)))
3151    (set (pc)
3152         (if_then_else (le (cc0) (const_int 0))
3153                       (label_ref (match_dup 5))
3154                       (pc)))
3155    (match_dup 4)
3156    (parallel
3157      [(set (match_dup 0)
3158            (match_op_dup 2 [(match_dup 0) (const_int 1)]))
3159       (clobber (scratch:QI))])
3160    (set (match_dup 1) (plus:QI (match_dup 1) (const_int -1)))
3161    (set (cc0) (compare (match_dup 1) (const_int 0)))
3162    (set (pc)
3163         (if_then_else (ne (cc0) (const_int 0))
3164                       (label_ref (match_dup 4))
3165                       (pc)))
3166    (match_dup 5)]
3167   {
3168     operands[4] = gen_label_rtx ();
3169     operands[5] = gen_label_rtx ();
3170   })
3171
3172 (define_split
3173   [(set (match_operand 0 "register_operand" "")
3174         (match_operator 2 "nshift_operator"
3175          [(match_dup 0)
3176           (match_operand:QI 1 "register_operand" "")]))
3177    (clobber (match_operand:QI 3 "register_operand" ""))]
3178   "epilogue_completed
3179    && !find_regno_note (insn, REG_DEAD, REGNO (operands[1]))"
3180   [(set (match_dup 3)
3181         (match_dup 1))
3182    (set (cc0) (compare (match_dup 3) (const_int 0)))
3183    (set (pc)
3184         (if_then_else (le (cc0) (const_int 0))
3185                       (label_ref (match_dup 5))
3186                       (pc)))
3187    (match_dup 4)
3188    (parallel
3189      [(set (match_dup 0)
3190            (match_op_dup 2 [(match_dup 0) (const_int 1)]))
3191       (clobber (scratch:QI))])
3192    (set (match_dup 3) (plus:QI (match_dup 3) (const_int -1)))
3193    (set (cc0) (compare (match_dup 3) (const_int 0)))
3194    (set (pc)
3195         (if_then_else (ne (cc0) (const_int 0))
3196                       (label_ref (match_dup 4))
3197                       (pc)))
3198    (match_dup 5)]
3199   {
3200     operands[4] = gen_label_rtx ();
3201     operands[5] = gen_label_rtx ();
3202   })
3203 \f
3204 ;; ----------------------------------------------------------------------
3205 ;; ROTATIONS
3206 ;; ----------------------------------------------------------------------
3207
3208 (define_expand "rotlqi3"
3209   [(set (match_operand:QI 0 "register_operand" "")
3210         (rotate:QI (match_operand:QI 1 "register_operand" "")
3211                    (match_operand:QI 2 "nonmemory_operand" "")))]
3212   ""
3213   {
3214     if (expand_a_rotate (operands)) 
3215     DONE;
3216   })
3217
3218 (define_insn "rotlqi3_1"
3219   [(set (match_operand:QI 0 "register_operand" "=r")
3220         (rotate:QI (match_operand:QI 1 "register_operand" "0")
3221                    (match_operand:QI 2 "immediate_operand" "")))]
3222   ""
3223 {
3224   return output_a_rotate (ROTATE, operands);
3225 }
3226   [(set (attr "length")
3227         (symbol_ref "compute_a_rotate_length (operands)"))])
3228
3229 (define_expand "rotlhi3"
3230   [(set (match_operand:HI 0 "register_operand" "")
3231         (rotate:HI (match_operand:HI 1 "register_operand" "")
3232                    (match_operand:QI 2 "nonmemory_operand" "")))]
3233   ""
3234   {
3235     if (expand_a_rotate (operands)) 
3236     DONE;
3237   })
3238
3239 (define_insn "rotlhi3_1"
3240   [(set (match_operand:HI 0 "register_operand" "=r")
3241         (rotate:HI (match_operand:HI 1 "register_operand" "0")
3242                    (match_operand:QI 2 "immediate_operand" "")))]
3243   ""
3244 {
3245   return output_a_rotate (ROTATE, operands);
3246 }
3247   [(set (attr "length")
3248         (symbol_ref "compute_a_rotate_length (operands)"))])
3249
3250 (define_expand "rotlsi3"
3251   [(set (match_operand:SI 0 "register_operand" "")
3252         (rotate:SI (match_operand:SI 1 "register_operand" "")
3253                    (match_operand:QI 2 "nonmemory_operand" "")))]
3254   "TARGET_H8300H || TARGET_H8300S"
3255   {
3256     if (expand_a_rotate (operands)) 
3257     DONE;
3258   })
3259
3260 (define_insn "rotlsi3_1"
3261   [(set (match_operand:SI 0 "register_operand" "=r")
3262         (rotate:SI (match_operand:SI 1 "register_operand" "0")
3263                    (match_operand:QI 2 "immediate_operand" "")))]
3264   "TARGET_H8300H || TARGET_H8300S"
3265 {
3266   return output_a_rotate (ROTATE, operands);
3267 }
3268   [(set (attr "length")
3269         (symbol_ref "compute_a_rotate_length (operands)"))])
3270 \f
3271 ;; -----------------------------------------------------------------
3272 ;; BIT FIELDS
3273 ;; -----------------------------------------------------------------
3274 ;; The H8/300 has given 1/8th of its opcode space to bitfield
3275 ;; instructions so let's use them as well as we can.
3276
3277 ;; You'll never believe all these patterns perform one basic action --
3278 ;; load a bit from the source, optionally invert the bit, then store it
3279 ;; in the destination (which is known to be zero).
3280 ;;
3281 ;; Combine obviously need some work to better identify this situation and
3282 ;; canonicalize the form better.
3283
3284 ;;
3285 ;; Normal loads with a 16bit destination.
3286 ;;
3287
3288 (define_insn ""
3289   [(set (match_operand:HI 0 "register_operand" "=&r")
3290         (zero_extract:HI (match_operand:HI 1 "register_operand" "r")
3291                          (const_int 1)
3292                          (match_operand:HI 2 "immediate_operand" "n")))]
3293   "TARGET_H8300"
3294   "sub.w        %0,%0\;bld      %Z2,%Y1\;bst    #0,%X0"
3295   [(set_attr "length" "6")])
3296
3297 ;;
3298 ;; Inverted loads with a 16bit destination.
3299 ;;
3300
3301 (define_insn ""
3302   [(set (match_operand:HI 0 "register_operand" "=&r")
3303         (zero_extract:HI (xor:HI (match_operand:HI 1 "register_operand" "r")
3304                                  (match_operand:HI 3 "const_int_operand" "n"))
3305                          (const_int 1)
3306                          (match_operand:HI 2 "const_int_operand" "n")))]
3307   "(TARGET_H8300 || TARGET_H8300SX)
3308     && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
3309   "sub.w        %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
3310   [(set_attr "length" "8")])
3311
3312 ;;
3313 ;; Normal loads with a 32bit destination.
3314 ;;
3315
3316 (define_insn "*extzv_1_r_h8300"
3317   [(set (match_operand:SI 0 "register_operand" "=&r")
3318         (zero_extract:SI (match_operand:HI 1 "register_operand" "r")
3319                          (const_int 1)
3320                          (match_operand 2 "const_int_operand" "n")))]
3321   "TARGET_H8300 && INTVAL (operands[2]) < 16"
3322 {
3323   return output_simode_bld (0, operands);
3324 }
3325   [(set_attr "length" "8")])
3326
3327 (define_insn "*extzv_1_r_h8300hs"
3328   [(set (match_operand:SI 0 "register_operand" "=r,r")
3329         (zero_extract:SI (match_operand:SI 1 "register_operand" "?0,r")
3330                          (const_int 1)
3331                          (match_operand 2 "const_int_operand" "n,n")))]
3332   "(TARGET_H8300H || TARGET_H8300S) && INTVAL (operands[2]) < 16"
3333 {
3334   return output_simode_bld (0, operands);
3335 }
3336   [(set_attr "cc" "set_znv,set_znv")
3337    (set_attr "length" "8,6")])
3338
3339 ;;
3340 ;; Inverted loads with a 32bit destination.
3341 ;;
3342
3343 (define_insn "*extzv_1_r_inv_h8300"
3344   [(set (match_operand:SI 0 "register_operand" "=&r")
3345         (zero_extract:SI (xor:HI (match_operand:HI 1 "register_operand" "r")
3346                                  (match_operand:HI 3 "const_int_operand" "n"))
3347                          (const_int 1)
3348                          (match_operand 2 "const_int_operand" "n")))]
3349   "TARGET_H8300 && INTVAL (operands[2]) < 16
3350    && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
3351 {
3352   return output_simode_bld (1, operands);
3353 }
3354   [(set_attr "length" "8")])
3355
3356 (define_insn "*extzv_1_r_inv_h8300hs"
3357   [(set (match_operand:SI 0 "register_operand" "=r,r")
3358         (zero_extract:SI (xor:SI (match_operand:SI 1 "register_operand" "?0,r")
3359                                  (match_operand 3 "const_int_operand" "n,n"))
3360                          (const_int 1)
3361                          (match_operand 2 "const_int_operand" "n,n")))]
3362   "(TARGET_H8300H || TARGET_H8300S) && INTVAL (operands[2]) < 16
3363     && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
3364 {
3365   return output_simode_bld (1, operands);
3366 }
3367   [(set_attr "cc" "set_znv,set_znv")
3368    (set_attr "length" "8,6")])
3369
3370 (define_expand "insv"
3371   [(set (zero_extract:HI (match_operand:HI 0 "general_operand" "")
3372                          (match_operand:HI 1 "general_operand" "")
3373                          (match_operand:HI 2 "general_operand" ""))
3374         (match_operand:HI 3 "register_operand" ""))]
3375   "TARGET_H8300 || TARGET_H8300SX"
3376   {
3377     if (TARGET_H8300SX)
3378       {
3379         if (GET_CODE (operands[1]) == CONST_INT
3380             && GET_CODE (operands[2]) == CONST_INT
3381             && INTVAL (operands[1]) <= 8
3382             && INTVAL (operands[2]) >= 0
3383             && INTVAL (operands[1]) + INTVAL (operands[2]) <= 8
3384             && memory_operand (operands[0], GET_MODE (operands[0])))
3385           {
3386             /* If the source operand is zero, it's better to use AND rather
3387                than BFST.  Likewise OR if the operand is all ones.  */
3388             if (GET_CODE (operands[3]) == CONST_INT)
3389               {
3390                 HOST_WIDE_INT mask = (1 << INTVAL (operands[1])) - 1;
3391                 if ((INTVAL (operands[3]) & mask) == 0)
3392                   FAIL;
3393                 if ((INTVAL (operands[3]) & mask) == mask)
3394                   FAIL;
3395               }
3396             if (! bit_memory_operand (operands[0], GET_MODE (operands[0])))
3397               {
3398                 if (!can_create_pseudo_p ())
3399                   FAIL;
3400                 operands[0] =  replace_equiv_address (operands[0], force_reg (Pmode,
3401                                                       XEXP (operands[0], 0)));
3402               }
3403             operands[3] = gen_lowpart (QImode, operands[3]);
3404             if (! operands[3])
3405               FAIL;
3406             if (! register_operand (operands[3], QImode))
3407               {
3408                 if (!can_create_pseudo_p ())
3409                   FAIL;
3410                 operands[3] = force_reg (QImode, operands[3]);
3411               }
3412             emit_insn (gen_bfst (adjust_address (operands[0], QImode, 0),
3413                                                  operands[3], operands[1], operands[2]));
3414             DONE;
3415           }
3416         FAIL;
3417       }
3418
3419     /* We only have single bit bit-field instructions.  */
3420     if (INTVAL (operands[1]) != 1)
3421       FAIL;
3422
3423     /* For now, we don't allow memory operands.  */
3424     if (GET_CODE (operands[0]) == MEM
3425         || GET_CODE (operands[3]) == MEM)
3426       FAIL;
3427
3428     if (GET_CODE (operands[3]) != REG)
3429       operands[3] = force_reg (HImode, operands[3]);
3430   })
3431
3432 (define_insn ""
3433   [(set (zero_extract:HI (match_operand:HI 0 "register_operand" "+r")
3434                          (const_int 1)
3435                          (match_operand:HI 1 "immediate_operand" "n"))
3436         (match_operand:HI 2 "register_operand" "r"))]
3437   ""
3438   "bld  #0,%R2\;bst     %Z1,%Y0 ; i1"
3439   [(set_attr "length" "4")])
3440
3441 (define_expand "extzv"
3442   [(set (match_operand:HI 0 "register_operand" "")
3443         (zero_extract:HI (match_operand:HI 1 "bit_operand" "")
3444                          (match_operand:HI 2 "general_operand" "")
3445                          (match_operand:HI 3 "general_operand" "")))]
3446   "TARGET_H8300 || TARGET_H8300SX"
3447   {
3448     if (TARGET_H8300SX)
3449       {
3450         if (GET_CODE (operands[2]) == CONST_INT
3451             && GET_CODE (operands[3]) == CONST_INT
3452             && INTVAL (operands[2]) <= 8
3453             && INTVAL (operands[3]) >= 0
3454             && INTVAL (operands[2]) + INTVAL (operands[3]) <= 8
3455             && memory_operand (operands[1], QImode))
3456           {
3457             rtx temp;
3458
3459             /* Optimize the case where we're extracting into a paradoxical
3460                subreg.  It's only necessary to extend to the inner reg.  */
3461             if (GET_CODE (operands[0]) == SUBREG
3462                 && subreg_lowpart_p (operands[0])
3463                 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (operands[0])))
3464                     < GET_MODE_SIZE (GET_MODE (operands[0])))
3465                 && (GET_MODE_CLASS (GET_MODE (SUBREG_REG (operands[0])))
3466                     == MODE_INT))
3467               operands[0] = SUBREG_REG (operands[0]);
3468
3469             if (!can_create_pseudo_p ())
3470               temp = gen_lowpart (QImode, operands[0]);
3471             else
3472               temp = gen_reg_rtx (QImode);
3473             if (! temp)
3474               FAIL;
3475             if (! bit_memory_operand (operands[1], QImode))
3476               {
3477                 if (!can_create_pseudo_p ())
3478                   FAIL;
3479                 operands[1] = replace_equiv_address (operands[1],
3480                                                      force_reg (Pmode, XEXP (operands[1], 0)));
3481               }
3482             emit_insn (gen_bfld (temp, operands[1], operands[2], operands[3]));
3483             convert_move (operands[0], temp, 1);
3484             DONE;
3485           }
3486         FAIL;
3487       }
3488
3489     /* We only have single bit bit-field instructions.  */
3490     if (INTVAL (operands[2]) != 1)
3491       FAIL;
3492
3493     /* For now, we don't allow memory operands.  */
3494     if (GET_CODE (operands[1]) == MEM)
3495       FAIL;
3496   })
3497
3498 ;; BAND, BOR, and BXOR patterns
3499
3500 (define_insn ""
3501   [(set (match_operand:HI 0 "bit_operand" "=Ur")
3502         (match_operator:HI 4 "bit_operator"
3503          [(zero_extract:HI (match_operand:HI 1 "register_operand" "r")
3504                            (const_int 1)
3505                            (match_operand:HI 2 "immediate_operand" "n"))
3506           (match_operand:HI 3 "bit_operand" "0")]))]
3507   ""
3508   "bld  %Z2,%Y1\;b%c4   #0,%R0\;bst     #0,%R0; bl1"
3509   [(set_attr "length" "6")])
3510
3511 (define_insn ""
3512   [(set (match_operand:HI 0 "bit_operand" "=Ur")
3513         (match_operator:HI 5 "bit_operator"
3514          [(zero_extract:HI (match_operand:HI 1 "register_operand" "r")
3515                            (const_int 1)
3516                            (match_operand:HI 2 "immediate_operand" "n"))
3517           (zero_extract:HI (match_operand:HI 3 "register_operand" "r")
3518                            (const_int 1)
3519                            (match_operand:HI 4 "immediate_operand" "n"))]))]
3520   ""
3521   "bld  %Z2,%Y1\;b%c5   %Z4,%Y3\;bst    #0,%R0; bl3"
3522   [(set_attr "length" "6")])
3523
3524 (define_insn "bfld"
3525   [(set (match_operand:QI 0 "register_operand" "=r")
3526         (zero_extract:QI (match_operand:QI 1 "bit_memory_operand" "WU")
3527                          (match_operand:QI 2 "immediate_operand" "n")
3528                          (match_operand:QI 3 "immediate_operand" "n")))]
3529   "TARGET_H8300SX && INTVAL (operands[2]) + INTVAL (operands[3]) <= 8"
3530 {
3531   operands[2] = GEN_INT ((1 << (INTVAL (operands[2]) + INTVAL (operands[3])))
3532                          - (1 << INTVAL (operands[3])));
3533   return "bfld  %2,%1,%R0";
3534 }
3535   [(set_attr "cc" "none_0hit")
3536    (set_attr "length_table" "bitfield")])
3537
3538 (define_insn "bfst"
3539   [(set (zero_extract:QI (match_operand:QI 0 "bit_memory_operand" "+WU")
3540                          (match_operand:QI 2 "immediate_operand" "n")
3541                          (match_operand:QI 3 "immediate_operand" "n"))
3542         (match_operand:QI 1 "register_operand" "r"))]
3543   "TARGET_H8300SX && INTVAL (operands[2]) + INTVAL (operands[3]) <= 8"
3544 {
3545   operands[2] = GEN_INT ((1 << (INTVAL (operands[2]) + INTVAL (operands[3])))
3546                          - (1 << INTVAL (operands[3])));
3547   return "bfst  %R1,%2,%0";
3548 }
3549   [(set_attr "cc" "none_0hit")
3550    (set_attr "length_table" "bitfield")])
3551
3552 (define_expand "cstoreqi4"
3553   [(use (match_operator 1 "eqne_operator"
3554          [(match_operand:QI 2 "h8300_dst_operand" "")
3555           (match_operand:QI 3 "h8300_src_operand" "")]))
3556    (clobber (match_operand:HI 0 "register_operand"))]
3557   "TARGET_H8300SX"
3558   {
3559     h8300_expand_store (operands); 
3560     DONE;
3561   })
3562
3563 (define_expand "cstorehi4"
3564   [(use (match_operator 1 "eqne_operator"
3565          [(match_operand:HI 2 "h8300_dst_operand" "")
3566           (match_operand:HI 3 "h8300_src_operand" "")]))
3567    (clobber (match_operand:HI 0 "register_operand"))]
3568   "TARGET_H8300SX"
3569   {
3570     h8300_expand_store (operands); 
3571     DONE;
3572   })
3573
3574 (define_expand "cstoresi4"
3575   [(use (match_operator 1 "eqne_operator"
3576          [(match_operand:SI 2 "h8300_dst_operand" "")
3577           (match_operand:SI 3 "h8300_src_operand" "")]))
3578    (clobber (match_operand:HI 0 "register_operand"))]
3579   "TARGET_H8300SX"
3580   {
3581     h8300_expand_store (operands); 
3582     DONE;
3583   })
3584
3585 (define_insn "*bstzhireg"
3586   [(set (match_operand:HI 0 "register_operand" "=r")
3587         (match_operator:HI 1 "eqne_operator" [(cc0) (const_int 0)]))]
3588   "TARGET_H8300SX"
3589   "mulu.w       #0,%T0\;b%k1    .Lh8BR%=\;inc.w #1,%T0\\n.Lh8BR%=:"
3590   [(set_attr "cc" "clobber")])
3591
3592 (define_insn_and_split "*cmpstz"
3593   [(set (zero_extract:QI (match_operand:QI 0 "bit_memory_operand" "+WU,+WU")
3594                          (const_int 1)
3595                          (match_operand:QI 1 "immediate_operand" "n,n"))
3596         (match_operator:QI 2 "eqne_operator"
3597          [(match_operand 3 "h8300_dst_operand" "r,rQ")
3598           (match_operand 4 "h8300_src_operand" "I,rQi")]))]
3599   "TARGET_H8300SX
3600    && (GET_MODE (operands[3]) == GET_MODE (operands[4])
3601        || GET_CODE (operands[4]) == CONST_INT)
3602    && GET_MODE_CLASS (GET_MODE (operands[3])) == MODE_INT
3603    && GET_MODE_SIZE (GET_MODE (operands[3])) <= 4"
3604   "#"
3605   "reload_completed"
3606   [(set (cc0) (match_dup 5))
3607    (set (zero_extract:QI (match_dup 0) (const_int 1) (match_dup 1))
3608         (match_op_dup:QI 2 [(cc0) (const_int 0)]))]
3609   {
3610     operands[5] = gen_rtx_COMPARE (VOIDmode, operands[3], operands[4]);
3611   }
3612   [(set_attr "cc" "set_znv,compare")])
3613    
3614 (define_insn "*bstz"
3615   [(set (zero_extract:QI (match_operand:QI 0 "bit_memory_operand" "+WU")
3616                          (const_int 1)
3617                          (match_operand:QI 1 "immediate_operand" "n"))
3618         (eq:QI (cc0) (const_int 0)))]
3619   "TARGET_H8300SX && reload_completed"
3620   "bstz %1,%0"
3621   [(set_attr "cc" "none_0hit")
3622    (set_attr "length_table" "unary")])
3623
3624 (define_insn "*bistz"
3625   [(set (zero_extract:QI (match_operand:QI 0 "bit_memory_operand" "+WU")
3626                          (const_int 1)
3627                          (match_operand:QI 1 "immediate_operand" "n"))
3628         (ne:QI (cc0) (const_int 0)))]
3629   "TARGET_H8300SX && reload_completed"
3630   "bistz        %1,%0"
3631   [(set_attr "cc" "none_0hit")
3632    (set_attr "length_table" "unary")])
3633
3634 (define_insn_and_split "*cmpcondbset"
3635   [(set (match_operand:QI 0 "nonimmediate_operand" "=WU,WU")
3636         (if_then_else:QI (match_operator 1 "eqne_operator"
3637                           [(match_operand 2 "h8300_dst_operand" "r,rQ")
3638                            (match_operand 3 "h8300_src_operand" "I,rQi")])
3639                          (ior:QI (match_operand:QI 4 "bit_memory_operand" "0,0")
3640                                  (match_operand:QI 5 "single_one_operand" "n,n"))
3641                          (match_dup 4)))]
3642   "TARGET_H8300SX"
3643   "#"
3644   "reload_completed"
3645   [(set (cc0) (match_dup 6))
3646    (set (match_dup 0)
3647         (if_then_else:QI (match_op_dup 1 [(cc0) (const_int 0)])
3648                          (ior:QI (match_dup 4) (match_dup 5))
3649                          (match_dup 4)))]
3650   {
3651     operands[6] = gen_rtx_COMPARE (VOIDmode, operands[2], operands[3]);
3652   }
3653   [(set_attr "cc" "set_znv,compare")])
3654    
3655 (define_insn "*condbset"
3656   [(set (match_operand:QI 0 "bit_memory_operand" "=WU")
3657         (if_then_else:QI (match_operator:QI 2 "eqne_operator"
3658                           [(cc0) (const_int 0)])
3659                          (ior:QI (match_operand:QI 3 "bit_memory_operand" "0")
3660                                  (match_operand:QI 1 "single_one_operand" "n"))
3661                          (match_dup 3)))]
3662   "TARGET_H8300SX && reload_completed"
3663   "bset/%j2\t%V1,%0"
3664   [(set_attr "cc" "none_0hit")
3665    (set_attr "length_table" "logicb")])
3666
3667 (define_insn_and_split "*cmpcondbclr"
3668   [(set (match_operand:QI 0 "nonimmediate_operand" "=WU,WU")
3669         (if_then_else:QI (match_operator 1 "eqne_operator"
3670                           [(match_operand 2 "h8300_dst_operand" "r,rQ")
3671                            (match_operand 3 "h8300_src_operand" "I,rQi")])
3672                          (and:QI (match_operand:QI 4 "bit_memory_operand" "0,0")
3673                                  (match_operand:QI 5 "single_zero_operand" "n,n"))
3674                          (match_dup 4)))]
3675   "TARGET_H8300SX"
3676   "#"
3677   "reload_completed"
3678   [(set (cc0) (match_dup 6))
3679    (set (match_dup 0)
3680         (if_then_else:QI (match_op_dup 1 [(cc0) (const_int 0)])
3681                          (and:QI (match_dup 4) (match_dup 5))
3682                          (match_dup 4)))]
3683   {
3684     operands[6] = gen_rtx_COMPARE (VOIDmode, operands[2], operands[3]);
3685   }
3686   [(set_attr "cc" "set_znv,compare")])
3687    
3688 (define_insn "*condbclr"
3689   [(set (match_operand:QI 0 "bit_memory_operand" "=WU")
3690         (if_then_else:QI (match_operator:QI 2 "eqne_operator"
3691                           [(cc0) (const_int 0)])
3692                          (and:QI (match_operand:QI 3 "bit_memory_operand" "0")
3693                                  (match_operand:QI 1 "single_zero_operand" "n"))
3694                          (match_dup 3)))]
3695   "TARGET_H8300SX && reload_completed"
3696   "bclr/%j2\t%W1,%0"
3697   [(set_attr "cc" "none_0hit")
3698    (set_attr "length_table" "logicb")])
3699
3700 (define_insn_and_split "*cmpcondbsetreg"
3701   [(set (match_operand:QI 0 "nonimmediate_operand" "=WU,WU")
3702         (if_then_else:QI (match_operator 1 "eqne_operator"
3703                           [(match_operand 2 "h8300_dst_operand" "r,rQ")
3704                            (match_operand 3 "h8300_src_operand" "I,rQi")])
3705                          (ior:QI (match_operand:QI 4 "bit_memory_operand" "0,0")
3706                                  (ashift:QI (const_int 1)
3707                                             (match_operand:QI 5 "register_operand" "r,r")))
3708                          (match_dup 4)))]
3709   "TARGET_H8300SX"
3710   "#"
3711   "reload_completed"
3712   [(set (cc0) (match_dup 6))
3713    (set (match_dup 0)
3714         (if_then_else:QI (match_op_dup 1 [(cc0) (const_int 0)])
3715                          (ior:QI (match_dup 4)
3716                                  (ashift:QI (const_int 1)
3717                                             (match_operand:QI 5 "register_operand" "r,r")))
3718                          (match_dup 4)))]
3719   {
3720     operands[6] = gen_rtx_COMPARE (VOIDmode, operands[2], operands[3]);
3721   }
3722   [(set_attr "cc" "set_znv,compare")])
3723    
3724 (define_insn "*condbsetreg"
3725   [(set (match_operand:QI 0 "bit_memory_operand" "=WU")
3726         (if_then_else:QI (match_operator:QI 2 "eqne_operator"
3727                           [(cc0) (const_int 0)])
3728                          (ior:QI (match_operand:QI 3 "bit_memory_operand" "0")
3729                                  (ashift:QI (const_int 1)
3730                                             (match_operand:QI 1 "register_operand" "r")))
3731                          (match_dup 3)))]
3732   "TARGET_H8300SX && reload_completed"
3733   "bset/%j2\t%R1,%0"
3734   [(set_attr "cc" "none_0hit")
3735    (set_attr "length_table" "logicb")])
3736
3737 (define_insn_and_split "*cmpcondbclrreg"
3738   [(set (match_operand:QI 0 "nonimmediate_operand" "=WU,WU")
3739         (if_then_else:QI (match_operator 1 "eqne_operator"
3740                           [(match_operand 2 "h8300_dst_operand" "r,rQ")
3741                            (match_operand 3 "h8300_src_operand" "I,rQi")])
3742                          (and:QI (match_operand:QI 4 "bit_memory_operand" "0,0")
3743                                  (ashift:QI (const_int 1)
3744                                             (match_operand:QI 5 "register_operand" "r,r")))
3745                          (match_dup 4)))]
3746   "TARGET_H8300SX"
3747   "#"
3748   "reload_completed"
3749   [(set (cc0) (match_dup 6))
3750    (set (match_dup 0)
3751         (if_then_else:QI (match_op_dup 1 [(cc0) (const_int 0)])
3752                          (and:QI (match_dup 4)
3753                                  (ashift:QI (const_int 1)
3754                                             (match_operand:QI 5 "register_operand" "r,r")))
3755                          (match_dup 4)))]
3756   {
3757     operands[6] = gen_rtx_COMPARE (VOIDmode, operands[2], operands[3]);
3758   }
3759   [(set_attr "cc" "set_znv,compare")])
3760    
3761 (define_insn "*condbclrreg"
3762   [(set (match_operand:QI 0 "bit_memory_operand" "=WU")
3763         (if_then_else:QI (match_operator:QI 2 "eqne_operator"
3764                           [(cc0) (const_int 0)])
3765                          (and:QI (match_operand:QI 3 "bit_memory_operand" "0")
3766                                  (ashift:QI (const_int 1)
3767                                             (match_operand:QI 1 "register_operand" "r")))
3768                          (match_dup 3)))]
3769   "TARGET_H8300SX && reload_completed"
3770   "bclr/%j2\t%R1,%0"
3771   [(set_attr "cc" "none_0hit")
3772    (set_attr "length_table" "logicb")])
3773
3774 \f
3775 ;; -----------------------------------------------------------------
3776 ;; COMBINE PATTERNS
3777 ;; -----------------------------------------------------------------
3778
3779 ;; insv:SI
3780
3781 (define_insn "*insv_si_1_n"
3782   [(set (zero_extract:SI (match_operand:SI 0 "register_operand" "+r")
3783                          (const_int 1)
3784                          (match_operand:SI 1 "const_int_operand" "n"))
3785         (match_operand:SI 2 "register_operand" "r"))]
3786   "(TARGET_H8300H || TARGET_H8300S) && INTVAL (operands[1]) < 16"
3787   "bld\\t#0,%w2\;bst\\t%Z1,%Y0"
3788   [(set_attr "length" "4")])
3789
3790 (define_insn "*insv_si_1_n_lshiftrt"
3791   [(set (zero_extract:SI (match_operand:SI 0 "register_operand" "+r")
3792                          (const_int 1)
3793                          (match_operand:SI 1 "const_int_operand" "n"))
3794         (lshiftrt:SI (match_operand:SI 2 "register_operand" "r")
3795                      (match_operand:SI 3 "const_int_operand" "n")))]
3796   "(TARGET_H8300H || TARGET_H8300S)
3797     && INTVAL (operands[1]) < 16
3798     && INTVAL (operands[3]) < 16"
3799   "bld\\t%Z3,%Y2\;bst\\t%Z1,%Y0"
3800   [(set_attr "length" "4")])
3801
3802 (define_insn "*insv_si_1_n_lshiftrt_16"
3803   [(set (zero_extract:SI (match_operand:SI 0 "register_operand" "+r")
3804                          (const_int 1)
3805                          (match_operand:SI 1 "const_int_operand" "n"))
3806         (lshiftrt:SI (match_operand:SI 2 "register_operand" "r")
3807                      (const_int 16)))]
3808   "(TARGET_H8300H || TARGET_H8300S) && INTVAL (operands[1]) < 16"
3809   "rotr.w\\t%e2\;rotl.w\\t%e2\;bst\\t%Z1,%Y0"
3810   [(set_attr "length" "6")])
3811
3812 (define_insn "*insv_si_8_8"
3813   [(set (zero_extract:SI (match_operand:SI 0 "register_operand" "+r")
3814                          (const_int 8)
3815                          (const_int 8))
3816         (match_operand:SI 1 "register_operand" "r"))]
3817   "TARGET_H8300H || TARGET_H8300S"
3818   "mov.b\\t%w1,%x0"
3819   [(set_attr "length" "2")])
3820
3821 (define_insn "*insv_si_8_8_lshiftrt_8"
3822   [(set (zero_extract:SI (match_operand:SI 0 "register_operand" "+r")
3823                          (const_int 8)
3824                          (const_int 8))
3825         (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
3826                      (const_int 8)))]
3827   "TARGET_H8300H || TARGET_H8300S"
3828   "mov.b\\t%x1,%x0"
3829   [(set_attr "length" "2")])
3830
3831 ;; extzv:SI
3832
3833 (define_insn "*extzv_8_8"
3834   [(set (match_operand:SI 0 "register_operand" "=r,r")
3835         (zero_extract:SI (match_operand:SI 1 "register_operand" "?0,r")
3836                          (const_int 8)
3837                          (const_int 8)))]
3838   "TARGET_H8300H || TARGET_H8300S"
3839   "@
3840    mov.b\\t%x1,%w0\;extu.w\\t%f0\;extu.l\\t%S0
3841    sub.l\\t%S0,%S0\;mov.b\\t%x1,%w0"
3842   [(set_attr "cc" "set_znv,clobber")
3843    (set_attr "length" "6,4")])
3844
3845 (define_insn "*extzv_8_16"
3846   [(set (match_operand:SI 0 "register_operand" "=r")
3847         (zero_extract:SI (match_operand:SI 1 "register_operand" "r")
3848                          (const_int 8)
3849                          (const_int 16)))]
3850   "TARGET_H8300H || TARGET_H8300S"
3851   "mov.w\\t%e1,%f0\;extu.w\\t%f0\;extu.l\\t%S0"
3852   [(set_attr "cc" "set_znv")
3853    (set_attr "length" "6")])
3854
3855 (define_insn "*extzv_16_8"
3856   [(set (match_operand:SI 0 "register_operand" "=r")
3857         (zero_extract:SI (match_operand:SI 1 "register_operand" "r")
3858                          (const_int 16)
3859                          (const_int 8)))
3860    (clobber (match_scratch:SI 2 "=&r"))]
3861   "TARGET_H8300H"
3862   "mov.w\\t%e1,%f2\;mov.b\\t%x1,%w0\;mov.b\\t%w2,%x0\;extu.l\\t%S0"
3863   [(set_attr "length" "8")
3864    (set_attr "cc" "set_znv")])
3865
3866 ;; Extract the exponent of a float.
3867
3868 (define_insn_and_split "*extzv_8_23"
3869   [(set (match_operand:SI 0 "register_operand" "=r")
3870         (zero_extract:SI (match_operand:SI 1 "register_operand" "0")
3871                          (const_int 8)
3872                          (const_int 23)))]
3873   "(TARGET_H8300H || TARGET_H8300S)"
3874   "#"
3875   "&& reload_completed"
3876   [(parallel [(set (match_dup 0)
3877                    (ashift:SI (match_dup 0)
3878                               (const_int 1)))
3879               (clobber (scratch:QI))])
3880    (parallel [(set (match_dup 0)
3881                    (lshiftrt:SI (match_dup 0)
3882                                 (const_int 24)))
3883               (clobber (scratch:QI))])]
3884   "")
3885
3886 ;; and:SI
3887
3888 ;; ((SImode) HImode) << 15
3889
3890 (define_insn_and_split "*twoshifts_l16_r1"
3891   [(set (match_operand:SI 0 "register_operand" "=r")
3892         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "0")
3893                            (const_int 15))
3894                 (const_int 2147450880)))]
3895   "(TARGET_H8300H || TARGET_H8300S)"
3896   "#"
3897   "&& reload_completed"
3898   [(parallel [(set (match_dup 0)
3899                    (ashift:SI (match_dup 0)
3900                               (const_int 16)))
3901               (clobber (scratch:QI))])
3902    (parallel [(set (match_dup 0)
3903                    (lshiftrt:SI (match_dup 0)
3904                                 (const_int 1)))
3905               (clobber (scratch:QI))])]
3906   "")
3907
3908 ;; Transform (SImode << B) & 0xffff into (SImode) (HImode << B).
3909
3910 (define_insn_and_split "*andsi3_ashift_n_lower"
3911   [(set (match_operand:SI 0 "register_operand" "=r,r")
3912         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "0,0")
3913                            (match_operand:QI 2 "const_int_operand" "S,n"))
3914                 (match_operand:SI 3 "const_int_operand" "n,n")))
3915    (clobber (match_scratch:QI 4 "=X,&r"))]
3916   "(TARGET_H8300H || TARGET_H8300S)
3917     && INTVAL (operands[2]) <= 15
3918     && INTVAL (operands[3]) == ((-1 << INTVAL (operands[2])) & 0xffff)"
3919   "#"
3920   "&& reload_completed"
3921   [(parallel [(set (match_dup 5)
3922                    (ashift:HI (match_dup 5)
3923                               (match_dup 2)))
3924               (clobber (match_dup 4))])
3925    (set (match_dup 0)
3926         (zero_extend:SI (match_dup 5)))]
3927   {
3928     operands[5] = gen_rtx_REG (HImode, REGNO (operands[0]));
3929   })
3930
3931 ;; Accept (A >> 30) & 2 and the like.
3932
3933 (define_insn "*andsi3_lshiftrt_n_sb"
3934   [(set (match_operand:SI 0 "register_operand" "=r")
3935         (and:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "0")
3936                              (match_operand:SI 2 "const_int_operand" "n"))
3937                 (match_operand:SI 3 "single_one_operand" "n")))]
3938   "(TARGET_H8300H || TARGET_H8300S)
3939     && exact_log2 (INTVAL (operands[3])) < 16
3940     && INTVAL (operands[2]) + exact_log2 (INTVAL (operands[3])) == 31"
3941 {
3942   operands[3] = GEN_INT (exact_log2 (INTVAL (operands[3])));
3943   return "shll.l\\t%S0\;xor.l\\t%S0,%S0\;bst\\t%Z3,%Y0";
3944 }
3945   [(set_attr "length" "8")])
3946
3947 (define_insn_and_split "*andsi3_lshiftrt_9_sb"
3948   [(set (match_operand:SI 0 "register_operand" "=r")
3949         (and:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "0")
3950                              (const_int 9))
3951                 (const_int 4194304)))]
3952   "TARGET_H8300H || TARGET_H8300S"
3953   "#"
3954   "&& reload_completed"
3955   [(set (match_dup 0)
3956         (and:SI (lshiftrt:SI (match_dup 0)
3957                              (const_int 25))
3958                 (const_int 64)))
3959    (parallel [(set (match_dup 0)
3960                    (ashift:SI (match_dup 0)
3961                               (const_int 16)))
3962               (clobber (scratch:QI))])]
3963   "")
3964
3965 ;; plus:SI
3966
3967 (define_insn "*addsi3_upper"
3968   [(set (match_operand:SI 0 "register_operand" "=r")
3969         (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "r")
3970                           (const_int 65536))
3971                  (match_operand:SI 2 "register_operand" "0")))]
3972   "TARGET_H8300H || TARGET_H8300S"
3973   "add.w\\t%f1,%e0"
3974   [(set_attr "length" "2")])
3975
3976 (define_insn "*addsi3_lshiftrt_16_zexthi"
3977   [(set (match_operand:SI 0 "register_operand" "=r")
3978         (plus:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
3979                               (const_int 16))
3980                  (zero_extend:SI (match_operand:HI 2 "register_operand" "0"))))]
3981   "TARGET_H8300H || TARGET_H8300S"
3982   "add.w\\t%e1,%f0\;xor.w\\t%e0,%e0\;rotxl.w\\t%e0"
3983   [(set_attr "length" "6")])
3984
3985 (define_insn_and_split "*addsi3_and_r_1"
3986   [(set (match_operand:SI 0 "register_operand" "=r")
3987         (plus:SI (and:SI (match_operand:SI 1 "register_operand" "r")
3988                          (const_int 1))
3989                  (match_operand:SI 2 "register_operand" "0")))]
3990   "TARGET_H8300H || TARGET_H8300S"
3991   "#"
3992   "&& reload_completed"
3993   [(set (cc0) (compare (zero_extract:SI (match_dup 1)
3994                                         (const_int 1)
3995                                         (const_int 0))
3996                        (const_int 0)))
3997    (set (pc)
3998         (if_then_else (eq (cc0)
3999                           (const_int 0))
4000                       (label_ref (match_dup 3))
4001                       (pc)))
4002    (set (match_dup 2)
4003         (plus:SI (match_dup 2)
4004                  (const_int 1)))
4005    (match_dup 3)]
4006   {
4007     operands[3] = gen_label_rtx ();
4008   })
4009
4010 (define_insn_and_split "*addsi3_and_not_r_1"
4011   [(set (match_operand:SI 0 "register_operand" "=r")
4012         (plus:SI (and:SI (not:SI (match_operand:SI 1 "register_operand" "r"))
4013                          (const_int 1))
4014                  (match_operand:SI 2 "register_operand" "0")))]
4015   "TARGET_H8300H || TARGET_H8300S"
4016   "#"
4017   "&& reload_completed"
4018   [(set (cc0) (compare (zero_extract:SI (match_dup 1)
4019                                         (const_int 1)
4020                                         (const_int 0))
4021                        (const_int 0)))
4022    (set (pc)
4023         (if_then_else (ne (cc0)
4024                           (const_int 0))
4025                       (label_ref (match_dup 3))
4026                       (pc)))
4027    (set (match_dup 2)
4028         (plus:SI (match_dup 2)
4029                  (const_int 1)))
4030    (match_dup 3)]
4031   {
4032     operands[3] = gen_label_rtx ();
4033   })
4034
4035 ;; [ix]or:HI
4036
4037 (define_insn "*ixorhi3_zext"
4038   [(set (match_operand:HI 0 "register_operand" "=r")
4039         (match_operator:HI 1 "iorxor_operator"
4040          [(zero_extend:HI (match_operand:QI 2 "register_operand" "r"))
4041           (match_operand:HI 3 "register_operand" "0")]))]
4042   ""
4043   "%c1.b\\t%X2,%s0"
4044   [(set_attr "length" "2")])
4045
4046 ;; [ix]or:SI
4047
4048 (define_insn "*ixorsi3_zext_qi"
4049   [(set (match_operand:SI 0 "register_operand" "=r")
4050         (match_operator:SI 1 "iorxor_operator"
4051          [(zero_extend:SI (match_operand:QI 2 "register_operand" "r"))
4052           (match_operand:SI 3 "register_operand" "0")]))]
4053   ""
4054   "%c1.b\\t%X2,%w0"
4055   [(set_attr "length" "2")])
4056
4057 (define_insn "*ixorsi3_zext_hi"
4058   [(set (match_operand:SI 0 "register_operand" "=r")
4059         (match_operator:SI 1 "iorxor_operator"
4060          [(zero_extend:SI (match_operand:HI 2 "register_operand" "r"))
4061           (match_operand:SI 3 "register_operand" "0")]))]
4062   "TARGET_H8300H || TARGET_H8300S"
4063   "%c1.w\\t%T2,%f0"
4064   [(set_attr "length" "2")])
4065
4066 (define_insn "*ixorsi3_ashift_16"
4067   [(set (match_operand:SI 0 "register_operand" "=r")
4068         (match_operator:SI 1 "iorxor_operator"
4069          [(ashift:SI (match_operand:SI 2 "register_operand" "r")
4070                      (const_int 16))
4071           (match_operand:SI 3 "register_operand" "0")]))]
4072   "TARGET_H8300H || TARGET_H8300S"
4073   "%c1.w\\t%f2,%e0"
4074   [(set_attr "length" "2")])
4075
4076 (define_insn "*ixorsi3_lshiftrt_16"
4077   [(set (match_operand:SI 0 "register_operand" "=r")
4078         (match_operator:SI 1 "iorxor_operator"
4079          [(lshiftrt:SI (match_operand:SI 2 "register_operand" "r")
4080                        (const_int 16))
4081           (match_operand:SI 3 "register_operand" "0")]))]
4082   "TARGET_H8300H || TARGET_H8300S"
4083   "%c1.w\\t%e2,%f0"
4084   [(set_attr "length" "2")])
4085
4086 ;; ior:HI
4087
4088 (define_insn "*iorhi3_ashift_8"
4089   [(set (match_operand:HI 0 "register_operand" "=r")
4090         (ior:HI (ashift:HI (match_operand:HI 1 "register_operand" "r")
4091                            (const_int 8))
4092                 (match_operand:HI 2 "register_operand" "0")))]
4093   ""
4094   "or.b\\t%s1,%t0"
4095   [(set_attr "length" "2")])
4096
4097 (define_insn "*iorhi3_lshiftrt_8"
4098   [(set (match_operand:HI 0 "register_operand" "=r")
4099         (ior:HI (lshiftrt:HI (match_operand:HI 1 "register_operand" "r")
4100                              (const_int 8))
4101                 (match_operand:HI 2 "register_operand" "0")))]
4102   ""
4103   "or.b\\t%t1,%s0"
4104   [(set_attr "length" "2")])
4105
4106 (define_insn "*iorhi3_two_qi"
4107   [(set (match_operand:HI 0 "register_operand" "=r")
4108         (ior:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "0"))
4109                 (ashift:HI (match_operand:HI 2 "register_operand" "r")
4110                            (const_int 8))))]
4111   ""
4112   "mov.b\\t%s2,%t0"
4113   [(set_attr "length" "2")])
4114
4115 (define_insn "*iorhi3_two_qi_mem"
4116   [(set (match_operand:HI 0 "register_operand" "=&r")
4117         (ior:HI (zero_extend:HI (match_operand:QI 1 "memory_operand" "m"))
4118                 (ashift:HI (subreg:HI (match_operand:QI 2 "memory_operand" "m") 0)
4119                            (const_int 8))))]
4120   ""
4121   "mov.b\\t%X2,%t0\;mov.b\\t%X1,%s0"
4122   [(set_attr "length" "16")])
4123
4124 (define_split
4125   [(set (match_operand:HI 0 "register_operand" "")
4126         (ior:HI (zero_extend:HI (match_operand:QI 1 "memory_operand" ""))
4127                 (ashift:HI (subreg:HI (match_operand:QI 2 "memory_operand" "") 0)
4128                            (const_int 8))))]
4129   "(TARGET_H8300H || TARGET_H8300S)
4130     && reload_completed
4131     && byte_accesses_mergeable_p (XEXP (operands[2], 0), XEXP (operands[1], 0))"
4132   [(set (match_dup 0)
4133         (match_dup 3))]
4134   {
4135     operands[3] = gen_rtx_MEM (HImode, XEXP (operands[2], 0));
4136   })
4137
4138 ;; ior:SI
4139
4140 (define_insn "*iorsi3_two_hi"
4141   [(set (match_operand:SI 0 "register_operand" "=r")
4142         (ior:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "0"))
4143                 (ashift:SI (match_operand:SI 2 "register_operand" "r")
4144                            (const_int 16))))]
4145   "TARGET_H8300H || TARGET_H8300S"
4146   "mov.w\\t%f2,%e0"
4147   [(set_attr "length" "2")])
4148
4149 (define_insn_and_split "*iorsi3_two_qi_zext"
4150   [(set (match_operand:SI 0 "register_operand" "=&r")
4151         (ior:SI (zero_extend:SI (match_operand:QI 1 "memory_operand" "m"))
4152                 (and:SI (ashift:SI (subreg:SI (match_operand:QI 2 "memory_operand" "m") 0)
4153                                    (const_int 8))
4154                         (const_int 65280))))]
4155   "TARGET_H8300H || TARGET_H8300S"
4156   "#"
4157   "&& reload_completed"
4158   [(set (match_dup 3)
4159         (ior:HI (zero_extend:HI (match_dup 1))
4160                 (ashift:HI (subreg:HI (match_dup 2) 0)
4161                            (const_int 8))))
4162    (set (match_dup 0)
4163         (zero_extend:SI (match_dup 3)))]
4164   {
4165     operands[3] = gen_rtx_REG (HImode, REGNO (operands[0]));
4166   })
4167
4168 (define_insn "*iorsi3_e2f"
4169   [(set (match_operand:SI 0 "register_operand" "=r")
4170         (ior:SI (and:SI (match_operand:SI 1 "register_operand" "0")
4171                         (const_int -65536))
4172                 (lshiftrt:SI (match_operand:SI 2 "register_operand" "r")
4173                              (const_int 16))))]
4174   "TARGET_H8300H || TARGET_H8300S"
4175   "mov.w\\t%e2,%f0"
4176   [(set_attr "length" "2")])
4177
4178 (define_insn_and_split "*iorsi3_two_qi_sext"
4179   [(set (match_operand:SI 0 "register_operand" "=r")
4180         (ior:SI (zero_extend:SI (match_operand:QI 1 "register_operand" "0"))
4181                 (ashift:SI (sign_extend:SI (match_operand:QI 2 "register_operand" "r"))
4182                            (const_int 8))))]
4183   "TARGET_H8300H || TARGET_H8300S"
4184   "#"
4185   "&& reload_completed"
4186   [(set (match_dup 3)
4187         (ior:HI (zero_extend:HI (match_dup 1))
4188                 (ashift:HI (match_dup 4)
4189                            (const_int 8))))
4190    (set (match_dup 0)
4191         (sign_extend:SI (match_dup 3)))]
4192   {
4193     operands[3] = gen_rtx_REG (HImode, REGNO (operands[0]));
4194     operands[4] = gen_rtx_REG (HImode, REGNO (operands[2]));
4195   })
4196
4197 (define_insn "*iorsi3_w"
4198   [(set (match_operand:SI 0 "register_operand" "=r,&r")
4199         (ior:SI (and:SI (match_operand:SI 1 "register_operand" "0,0")
4200                         (const_int -256))
4201                 (zero_extend:SI (match_operand:QI 2 "general_operand_src" "r,g>"))))]
4202   "TARGET_H8300H || TARGET_H8300S"
4203   "mov.b\\t%X2,%w0"
4204   [(set_attr "length" "2,8")])
4205
4206 (define_insn "*iorsi3_ashift_31"
4207   [(set (match_operand:SI 0 "register_operand" "=&r")
4208         (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
4209                            (const_int 31))
4210                 (match_operand:SI 2 "register_operand" "0")))]
4211   "TARGET_H8300H || TARGET_H8300S"
4212   "rotxl.l\\t%S0\;bor\\t#0,%w1\;rotxr.l\\t%S0"
4213   [(set_attr "length" "6")
4214    (set_attr "cc" "set_znv")])
4215
4216 (define_insn "*iorsi3_and_ashift"
4217   [(set (match_operand:SI 0 "register_operand" "=r")
4218         (ior:SI (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
4219                                    (match_operand:SI 2 "const_int_operand" "n"))
4220                         (match_operand:SI 3 "single_one_operand" "n"))
4221                 (match_operand:SI 4 "register_operand" "0")))]
4222   "(TARGET_H8300H || TARGET_H8300S)
4223     && (INTVAL (operands[3]) & ~0xffff) == 0"
4224 {
4225   rtx srcpos = GEN_INT (exact_log2 (INTVAL (operands[3]))
4226                         - INTVAL (operands[2]));
4227   rtx dstpos = GEN_INT (exact_log2 (INTVAL (operands[3])));
4228   operands[2] = srcpos;
4229   operands[3] = dstpos;
4230   return "bld\\t%Z2,%Y1\;bor\\t%Z3,%Y0\;bst\\t%Z3,%Y0";
4231 }
4232   [(set_attr "length" "6")])
4233
4234 (define_insn "*iorsi3_and_lshiftrt"
4235   [(set (match_operand:SI 0 "register_operand" "=r")
4236         (ior:SI (and:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
4237                                      (match_operand:SI 2 "const_int_operand" "n"))
4238                         (match_operand:SI 3 "single_one_operand" "n"))
4239                 (match_operand:SI 4 "register_operand" "0")))]
4240   "(TARGET_H8300H || TARGET_H8300S)
4241     && ((INTVAL (operands[3]) << INTVAL (operands[2])) & ~0xffff) == 0"
4242 {
4243   rtx srcpos = GEN_INT (exact_log2 (INTVAL (operands[3]))
4244                         + INTVAL (operands[2]));
4245   rtx dstpos = GEN_INT (exact_log2 (INTVAL (operands[3])));
4246   operands[2] = srcpos;
4247   operands[3] = dstpos;
4248   return "bld\\t%Z2,%Y1\;bor\\t%Z3,%Y0\;bst\\t%Z3,%Y0";
4249 }
4250   [(set_attr "length" "6")])
4251
4252 (define_insn "*iorsi3_zero_extract"
4253   [(set (match_operand:SI 0 "register_operand" "=r")
4254         (ior:SI (zero_extract:SI (match_operand:SI 1 "register_operand" "r")
4255                                  (const_int 1)
4256                                  (match_operand:SI 2 "const_int_operand" "n"))
4257                 (match_operand:SI 3 "register_operand" "0")))]
4258   "(TARGET_H8300H || TARGET_H8300S) && INTVAL (operands[2]) < 16"
4259   "bld\\t%Z2,%Y1\;bor\\t#0,%w0\;bst\\t#0,%w0"
4260   [(set_attr "length" "6")])
4261
4262 (define_insn "*iorsi3_and_lshiftrt_n_sb"
4263   [(set (match_operand:SI 0 "register_operand" "=r")
4264         (ior:SI (and:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
4265                                      (const_int 30))
4266                         (const_int 2))
4267                 (match_operand:SI 2 "register_operand" "0")))]
4268   "TARGET_H8300H || TARGET_H8300S"
4269   "rotl.l\\t%S1\;rotr.l\\t%S1\;bor\\t#1,%w0\;bst\\t#1,%w0"
4270   [(set_attr "length" "8")])
4271
4272 (define_insn "*iorsi3_and_lshiftrt_9_sb"
4273   [(set (match_operand:SI 0 "register_operand" "=r")
4274         (ior:SI (and:SI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
4275                                      (const_int 9))
4276                         (const_int 4194304))
4277                 (match_operand:SI 2 "register_operand" "0")))
4278    (clobber (match_scratch:HI 3 "=&r"))]
4279   "TARGET_H8300H || TARGET_H8300S"
4280 {
4281   if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
4282     return "shll.l\\t%S1\;xor.w\\t%T3,%T3\;bst\\t#6,%s3\;or.w\\t%T3,%e0";
4283   else
4284     return "rotl.l\\t%S1\;rotr.l\\t%S1\;xor.w\\t%T3,%T3\;bst\\t#6,%s3\;or.w\\t%T3,%e0";
4285 }
4286   [(set_attr "length" "10")])
4287
4288 ;; Used to OR the exponent of a float.
4289
4290 (define_insn "*iorsi3_shift"
4291   [(set (match_operand:SI 0 "register_operand" "=r")
4292         (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
4293                            (const_int 23))
4294                 (match_operand:SI 2 "register_operand" "0")))
4295    (clobber (match_scratch:SI 3 "=&r"))]
4296   "TARGET_H8300H || TARGET_H8300S"
4297   "#")
4298
4299 (define_split
4300   [(set (match_operand:SI 0 "register_operand" "")
4301         (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "")
4302                            (const_int 23))
4303                 (match_dup 0)))
4304    (clobber (match_operand:SI 2 "register_operand" ""))]
4305   "(TARGET_H8300H || TARGET_H8300S)
4306     && epilogue_completed
4307     && find_regno_note (insn, REG_DEAD, REGNO (operands[1]))
4308     && REGNO (operands[0]) != REGNO (operands[1])"
4309   [(parallel [(set (match_dup 3)
4310                    (ashift:HI (match_dup 3)
4311                               (const_int 7)))
4312               (clobber (scratch:QI))])
4313    (set (match_dup 0)
4314         (ior:SI (ashift:SI (match_dup 1)
4315                            (const_int 16))
4316                 (match_dup 0)))]
4317   {
4318     operands[3] = gen_rtx_REG (HImode, REGNO (operands[1]));
4319   })
4320
4321 (define_split
4322   [(set (match_operand:SI 0 "register_operand" "")
4323         (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "")
4324                            (const_int 23))
4325                 (match_dup 0)))
4326    (clobber (match_operand:SI 2 "register_operand" ""))]
4327   "(TARGET_H8300H || TARGET_H8300S)
4328     && epilogue_completed
4329     && !(find_regno_note (insn, REG_DEAD, REGNO (operands[1]))
4330          && REGNO (operands[0]) != REGNO (operands[1]))"
4331   [(set (match_dup 2)
4332         (match_dup 1))
4333    (parallel [(set (match_dup 3)
4334                    (ashift:HI (match_dup 3)
4335                               (const_int 7)))
4336               (clobber (scratch:QI))])
4337    (set (match_dup 0)
4338         (ior:SI (ashift:SI (match_dup 2)
4339                            (const_int 16))
4340                 (match_dup 0)))]
4341   {
4342     operands[3] = gen_rtx_REG (HImode, REGNO (operands[2]));
4343   })
4344
4345 (define_insn "*iorsi2_and_1_lshiftrt_1"
4346   [(set (match_operand:SI 0 "register_operand" "=r")
4347         (ior:SI (and:SI (match_operand:SI 1 "register_operand" "0")
4348                         (const_int 1))
4349                 (lshiftrt:SI (match_dup 1)
4350                              (const_int 1))))]
4351   "TARGET_H8300H || TARGET_H8300S"
4352   "shlr.l\\t%S0\;bor\\t#0,%w0\;bst\\t#0,%w0"
4353   [(set_attr "length" "6")])
4354
4355 (define_insn_and_split "*iorsi3_ashift_16_ashift_24"
4356   [(set (match_operand:SI 0 "register_operand" "=r")
4357         (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "0")
4358                            (const_int 16))
4359                 (ashift:SI (match_operand:SI 2 "register_operand" "r")
4360                            (const_int 24))))]
4361   "TARGET_H8300H || TARGET_H8300S"
4362   "#"
4363   "&& reload_completed"
4364   [(set (match_dup 3)
4365         (ior:HI (ashift:HI (match_dup 4)
4366                            (const_int 8))
4367                 (match_dup 3)))
4368    (parallel [(set (match_dup 0)
4369                    (ashift:SI (match_dup 0)
4370                               (const_int 16)))
4371               (clobber (scratch:QI))])]
4372   {
4373     operands[3] = gen_rtx_REG (HImode, REGNO (operands[0]));
4374     operands[4] = gen_rtx_REG (HImode, REGNO (operands[2]));
4375   })
4376
4377 (define_insn_and_split "*iorsi3_ashift_16_ashift_24_mem"
4378   [(set (match_operand:SI 0 "register_operand" "=&r")
4379         (ior:SI (and:SI (ashift:SI (subreg:SI (match_operand:QI 1 "memory_operand" "m") 0)
4380                                    (const_int 16))
4381                         (const_int 16711680))
4382                 (ashift:SI (subreg:SI (match_operand:QI 2 "memory_operand" "m") 0)
4383                            (const_int 24))))]
4384   "TARGET_H8300H || TARGET_H8300S"
4385   "#"
4386   "&& reload_completed"
4387   [(set (match_dup 3)
4388         (ior:HI (zero_extend:HI (match_dup 1))
4389                 (ashift:HI (subreg:HI (match_dup 2) 0)
4390                            (const_int 8))))
4391    (parallel [(set (match_dup 0)
4392                    (ashift:SI (match_dup 0)
4393                               (const_int 16)))
4394               (clobber (scratch:QI))])]
4395   {
4396     operands[3] = gen_rtx_REG (HImode, REGNO (operands[0]));
4397   })
4398
4399 ;; Used to add the exponent of a float.
4400
4401 (define_insn "*addsi3_shift"
4402   [(set (match_operand:SI 0 "register_operand" "=r")
4403         (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "r")
4404                           (const_int 8388608))
4405                  (match_operand:SI 2 "register_operand" "0")))
4406    (clobber (match_scratch:SI 3 "=&r"))]
4407   "TARGET_H8300H || TARGET_H8300S"
4408   "#")
4409
4410 (define_split
4411   [(set (match_operand:SI 0 "register_operand" "")
4412         (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "")
4413                           (const_int 8388608))
4414                  (match_dup 0)))
4415    (clobber (match_operand:SI 2 "register_operand" ""))]
4416   "(TARGET_H8300H || TARGET_H8300S)
4417     && epilogue_completed
4418     && find_regno_note (insn, REG_DEAD, REGNO (operands[1]))
4419     && REGNO (operands[0]) != REGNO (operands[1])"
4420   [(parallel [(set (match_dup 3)
4421                    (ashift:HI (match_dup 3)
4422                               (const_int 7)))
4423               (clobber (scratch:QI))])
4424    (set (match_dup 0)
4425         (plus:SI (mult:SI (match_dup 1)
4426                           (const_int 65536))
4427                  (match_dup 0)))]
4428   {
4429     operands[3] = gen_rtx_REG (HImode, REGNO (operands[1]));
4430   })
4431
4432 (define_split
4433   [(set (match_operand:SI 0 "register_operand" "")
4434         (plus:SI (mult:SI (match_operand:SI 1 "register_operand" "")
4435                           (const_int 8388608))
4436                  (match_dup 0)))
4437    (clobber (match_operand:SI 2 "register_operand" ""))]
4438   "(TARGET_H8300H || TARGET_H8300S)
4439     && epilogue_completed
4440     && !(find_regno_note (insn, REG_DEAD, REGNO (operands[1]))
4441          && REGNO (operands[0]) != REGNO (operands[1]))"
4442   [(set (match_dup 2)
4443         (match_dup 1))
4444    (parallel [(set (match_dup 3)
4445                    (ashift:HI (match_dup 3)
4446                               (const_int 7)))
4447               (clobber (scratch:QI))])
4448    (set (match_dup 0)
4449         (plus:SI (mult:SI (match_dup 2)
4450                           (const_int 65536))
4451                  (match_dup 0)))]
4452   {
4453     operands[3] = gen_rtx_REG (HImode, REGNO (operands[2]));
4454   })
4455
4456 ;; ashift:SI
4457
4458 (define_insn_and_split "*ashiftsi_sextqi_7"
4459   [(set (match_operand:SI 0 "register_operand" "=r")
4460         (ashift:SI (sign_extend:SI (match_operand:QI 1 "register_operand" "0"))
4461                    (const_int 7)))]
4462   "TARGET_H8300H || TARGET_H8300S"
4463   "#"
4464   "&& reload_completed"
4465   [(parallel [(set (match_dup 2)
4466                    (ashift:HI (match_dup 2)
4467                               (const_int 8)))
4468               (clobber (scratch:QI))])
4469    (set (match_dup 0)
4470         (sign_extend:SI (match_dup 2)))
4471    (parallel [(set (match_dup 0)
4472                    (ashiftrt:SI (match_dup 0)
4473                                 (const_int 1)))
4474               (clobber (scratch:QI))])]
4475   { 
4476     operands[2] = gen_rtx_REG (HImode, REGNO (operands[0]));
4477   })
4478
4479 ;; Storing a part of HImode to QImode.
4480
4481 (define_insn ""
4482   [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
4483         (subreg:QI (lshiftrt:HI (match_operand:HI 1 "register_operand" "r")
4484                                 (const_int 8)) 1))]
4485   ""
4486   "mov.b\\t%t1,%R0"
4487   [(set_attr "cc" "set_znv")
4488    (set_attr "length" "8")])
4489
4490 ;; Storing a part of SImode to QImode.
4491
4492 (define_insn ""
4493   [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
4494         (subreg:QI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
4495                                 (const_int 8)) 3))]
4496   ""
4497   "mov.b\\t%x1,%R0"
4498   [(set_attr "cc" "set_znv")
4499    (set_attr "length" "8")])
4500
4501 (define_insn ""
4502   [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
4503         (subreg:QI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
4504                                 (const_int 16)) 3))
4505    (clobber (match_scratch:SI 2 "=&r"))]
4506   "TARGET_H8300H || TARGET_H8300S"
4507   "mov.w\\t%e1,%f2\;mov.b\\t%w2,%R0"
4508   [(set_attr "cc" "set_znv")
4509    (set_attr "length" "10")])
4510
4511 (define_insn ""
4512   [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
4513         (subreg:QI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
4514                                 (const_int 24)) 3))
4515    (clobber (match_scratch:SI 2 "=&r"))]
4516   "TARGET_H8300H || TARGET_H8300S"
4517   "mov.w\\t%e1,%f2\;mov.b\\t%x2,%R0"
4518   [(set_attr "cc" "set_znv")
4519    (set_attr "length" "10")])
4520
4521 (define_insn_and_split ""
4522   [(set (pc)
4523         (if_then_else (eq (zero_extract:SI (subreg:SI (match_operand:QI 0 "register_operand" "") 0)
4524                                            (const_int 1)
4525                                            (const_int 7))
4526                           (const_int 0))
4527                       (label_ref (match_operand 1 "" ""))
4528                       (pc)))]
4529   ""
4530   "#"
4531   ""
4532   [(set (cc0) (compare (match_dup 0)
4533                        (const_int 0)))
4534    (set (pc)
4535         (if_then_else (ge (cc0)
4536                           (const_int 0))
4537                       (label_ref (match_dup 1))
4538                       (pc)))]
4539   "")
4540
4541 (define_insn_and_split ""
4542   [(set (pc)
4543         (if_then_else (ne (zero_extract:SI (subreg:SI (match_operand:QI 0 "register_operand" "") 0)
4544                                            (const_int 1)
4545                                            (const_int 7))
4546                           (const_int 0))
4547                       (label_ref (match_operand 1 "" ""))
4548                       (pc)))]
4549   ""
4550   "#"
4551   ""
4552   [(set (cc0) (compare (match_dup 0)
4553                        (const_int 0)))
4554    (set (pc)
4555         (if_then_else (lt (cc0)
4556                           (const_int 0))
4557                       (label_ref (match_dup 1))
4558                       (pc)))]
4559   "")
4560 \f
4561 ;; -----------------------------------------------------------------
4562 ;; PEEPHOLE PATTERNS
4563 ;; -----------------------------------------------------------------
4564
4565 ;; Convert (A >> B) & C to (A & 255) >> B if C == 255 >> B.
4566
4567 (define_peephole2
4568   [(parallel [(set (match_operand:HI 0 "register_operand" "")
4569                    (lshiftrt:HI (match_dup 0)
4570                                 (match_operand:HI 1 "const_int_operand" "")))
4571               (clobber (match_operand:HI 2 "" ""))])
4572    (set (match_dup 0)
4573         (and:HI (match_dup 0)
4574                 (match_operand:HI 3 "const_int_operand" "")))]
4575   "INTVAL (operands[3]) == (255 >> INTVAL (operands[1]))"
4576   [(set (match_dup 0)
4577         (and:HI (match_dup 0)
4578                 (const_int 255)))
4579    (parallel [(set (match_dup 0)
4580                    (lshiftrt:HI (match_dup 0) (match_dup 1)))
4581               (clobber (match_dup 2))])]
4582   "")
4583
4584 ;; Convert (A << B) & C to (A & 255) << B if C == 255 << B.
4585
4586 (define_peephole2
4587   [(parallel [(set (match_operand:HI 0 "register_operand" "")
4588                    (ashift:HI (match_dup 0)
4589                               (match_operand:HI 1 "const_int_operand" "")))
4590               (clobber (match_operand:HI 2 "" ""))])
4591    (set (match_dup 0)
4592         (and:HI (match_dup 0)
4593                 (match_operand:HI 3 "const_int_operand" "")))]
4594   "INTVAL (operands[3]) == (255 << INTVAL (operands[1]))"
4595   [(set (match_dup 0)
4596         (and:HI (match_dup 0)
4597                 (const_int 255)))
4598    (parallel [(set (match_dup 0)
4599                    (ashift:HI (match_dup 0) (match_dup 1)))
4600               (clobber (match_dup 2))])]
4601   "")
4602
4603 ;; Convert (A >> B) & C to (A & 255) >> B if C == 255 >> B.
4604
4605 (define_peephole2
4606   [(parallel [(set (match_operand:SI 0 "register_operand" "")
4607                    (lshiftrt:SI (match_dup 0)
4608                                 (match_operand:SI 1 "const_int_operand" "")))
4609               (clobber (match_operand:SI 2 "" ""))])
4610    (set (match_dup 0)
4611         (and:SI (match_dup 0)
4612                 (match_operand:SI 3 "const_int_operand" "")))]
4613   "INTVAL (operands[3]) == (255 >> INTVAL (operands[1]))"
4614   [(set (match_dup 0)
4615         (and:SI (match_dup 0)
4616                 (const_int 255)))
4617    (parallel [(set (match_dup 0)
4618                    (lshiftrt:SI (match_dup 0) (match_dup 1)))
4619               (clobber (match_dup 2))])]
4620   "")
4621
4622 ;; Convert (A << B) & C to (A & 255) << B if C == 255 << B.
4623
4624 (define_peephole2
4625   [(parallel [(set (match_operand:SI 0 "register_operand" "")
4626                    (ashift:SI (match_dup 0)
4627                               (match_operand:SI 1 "const_int_operand" "")))
4628               (clobber (match_operand:SI 2 "" ""))])
4629    (set (match_dup 0)
4630         (and:SI (match_dup 0)
4631                 (match_operand:SI 3 "const_int_operand" "")))]
4632   "INTVAL (operands[3]) == (255 << INTVAL (operands[1]))"
4633   [(set (match_dup 0)
4634         (and:SI (match_dup 0)
4635                 (const_int 255)))
4636    (parallel [(set (match_dup 0)
4637                    (ashift:SI (match_dup 0) (match_dup 1)))
4638               (clobber (match_dup 2))])]
4639   "")
4640
4641 ;; Convert (A >> B) & C to (A & 65535) >> B if C == 65535 >> B.
4642
4643 (define_peephole2
4644   [(parallel [(set (match_operand:SI 0 "register_operand" "")
4645                    (lshiftrt:SI (match_dup 0)
4646                                 (match_operand:SI 1 "const_int_operand" "")))
4647               (clobber (match_operand:SI 2 "" ""))])
4648    (set (match_dup 0)
4649         (and:SI (match_dup 0)
4650                 (match_operand:SI 3 "const_int_operand" "")))]
4651   "INTVAL (operands[3]) == (65535 >> INTVAL (operands[1]))"
4652   [(set (match_dup 0)
4653         (and:SI (match_dup 0)
4654                 (const_int 65535)))
4655    (parallel [(set (match_dup 0)
4656                    (lshiftrt:SI (match_dup 0) (match_dup 1)))
4657               (clobber (match_dup 2))])]
4658   "")
4659
4660 ;; Convert (A << B) & C to (A & 65535) << B if C == 65535 << B.
4661
4662 (define_peephole2
4663   [(parallel [(set (match_operand:SI 0 "register_operand" "")
4664                    (ashift:SI (match_dup 0)
4665                               (match_operand:SI 1 "const_int_operand" "")))
4666               (clobber (match_operand:SI 2 "" ""))])
4667    (set (match_dup 0)
4668         (and:SI (match_dup 0)
4669                 (match_operand:SI 3 "const_int_operand" "")))]
4670   "INTVAL (operands[3]) == (65535 << INTVAL (operands[1]))"
4671   [(set (match_dup 0)
4672         (and:SI (match_dup 0)
4673                 (const_int 65535)))
4674    (parallel [(set (match_dup 0)
4675                    (ashift:SI (match_dup 0) (match_dup 1)))
4676               (clobber (match_dup 2))])]
4677   "")
4678
4679 ;; Convert a QImode push into an SImode push so that the
4680 ;; define_peephole2 below can cram multiple pushes into one stm.l.
4681
4682 (define_peephole2
4683   [(parallel [(set (reg:SI SP_REG)
4684                    (plus:SI (reg:SI SP_REG) (const_int -4)))
4685               (set (mem:QI (plus:SI (reg:SI SP_REG) (const_int -3)))
4686                    (match_operand:QI 0 "register_operand" ""))])]
4687   "TARGET_H8300S && !TARGET_NORMAL_MODE && REGNO (operands[0]) != SP_REG"
4688   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4689         (match_dup 0))]
4690   { 
4691     operands[0] = gen_rtx_REG (SImode, REGNO (operands[0]));
4692   })
4693
4694 (define_peephole2
4695   [(parallel [(set (reg:HI SP_REG)
4696                    (plus:HI (reg:HI SP_REG) (const_int -4)))
4697               (set (mem:QI (plus:HI (reg:HI SP_REG) (const_int -3)))
4698                    (match_operand:QI 0 "register_operand" ""))])]
4699   "TARGET_H8300S && TARGET_NORMAL_MODE && REGNO (operands[0]) != SP_REG"
4700   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4701         (match_dup 0))]
4702   {
4703     operands[0] = gen_rtx_REG (SImode, REGNO (operands[0]));
4704   })
4705
4706 ;; Convert a HImode push into an SImode push so that the
4707 ;; define_peephole2 below can cram multiple pushes into one stm.l.
4708
4709 (define_peephole2
4710   [(parallel [(set (reg:SI SP_REG)
4711                    (plus:SI (reg:SI SP_REG) (const_int -4)))
4712               (set (mem:HI (plus:SI (reg:SI SP_REG) (const_int -2)))
4713                    (match_operand:HI 0 "register_operand" ""))])]
4714   "TARGET_H8300S && !TARGET_NORMAL_MODE && REGNO (operands[0]) != SP_REG"
4715   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4716         (match_dup 0))]
4717   {
4718     operands[0] = gen_rtx_REG (SImode, REGNO (operands[0]));
4719   })
4720
4721 (define_peephole2
4722   [(parallel [(set (reg:HI SP_REG)
4723                    (plus:HI (reg:HI SP_REG) (const_int -4)))
4724               (set (mem:HI (plus:HI (reg:HI SP_REG) (const_int -2)))
4725                    (match_operand:HI 0 "register_operand" ""))])]
4726   "TARGET_H8300S && TARGET_NORMAL_MODE && REGNO (operands[0]) != SP_REG"
4727   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4728         (match_dup 0))]
4729   {
4730     operands[0] = gen_rtx_REG (SImode, REGNO (operands[0]));
4731   })
4732
4733 ;; Cram four pushes into stm.l.
4734
4735 (define_peephole2
4736   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4737         (match_operand:SI 0 "register_operand" ""))
4738    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4739         (match_operand:SI 1 "register_operand" ""))
4740    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4741         (match_operand:SI 2 "register_operand" ""))
4742    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4743         (match_operand:SI 3 "register_operand" ""))]
4744   "TARGET_H8300S && !TARGET_NORMAL_MODE
4745    && (REGNO_REG_CLASS (REGNO (operands[3])) == GENERAL_REGS
4746        && REGNO (operands[1]) == REGNO (operands[0]) + 1
4747        && REGNO (operands[2]) == REGNO (operands[0]) + 2
4748        && REGNO (operands[3]) == REGNO (operands[0]) + 3
4749        && (TARGET_H8300SX || REGNO (operands[0]) == 0))"
4750   [(parallel [(set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -4)))
4751                    (match_dup 0))
4752               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -8)))
4753                    (match_dup 1))
4754               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -12)))
4755                    (match_dup 2))
4756               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -16)))
4757                    (match_dup 3))
4758               (set (reg:SI SP_REG)
4759                    (plus:SI (reg:SI SP_REG)
4760                             (const_int -16)))])]
4761   "")
4762
4763 (define_peephole2
4764   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4765         (match_operand:SI 0 "register_operand" ""))
4766    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4767         (match_operand:SI 1 "register_operand" ""))
4768    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4769         (match_operand:SI 2 "register_operand" ""))
4770    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4771         (match_operand:SI 3 "register_operand" ""))]
4772   "TARGET_H8300S && TARGET_NORMAL_MODE
4773    && (REGNO_REG_CLASS (REGNO (operands[3])) == GENERAL_REGS
4774        && REGNO (operands[1]) == REGNO (operands[0]) + 1
4775        && REGNO (operands[2]) == REGNO (operands[0]) + 2
4776        && REGNO (operands[3]) == REGNO (operands[0]) + 3
4777        && (TARGET_H8300SX || REGNO (operands[0]) == 0))"
4778   [(parallel [(set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -4)))
4779                    (match_dup 0))
4780               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -8)))
4781                    (match_dup 1))
4782               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -12)))
4783                    (match_dup 2))
4784               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -16)))
4785                    (match_dup 3))
4786               (set (reg:HI SP_REG)
4787                    (plus:HI (reg:HI SP_REG)
4788                             (const_int -16)))])]
4789   "")
4790
4791 ;; Cram three pushes into stm.l.
4792
4793 (define_peephole2
4794   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4795         (match_operand:SI 0 "register_operand" ""))
4796    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4797         (match_operand:SI 1 "register_operand" ""))
4798    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4799         (match_operand:SI 2 "register_operand" ""))]
4800   "TARGET_H8300S && !TARGET_NORMAL_MODE
4801    && (REGNO_REG_CLASS (REGNO (operands[2])) == GENERAL_REGS
4802        && REGNO (operands[1]) == REGNO (operands[0]) + 1
4803        && REGNO (operands[2]) == REGNO (operands[0]) + 2
4804        && (TARGET_H8300SX || (REGNO (operands[0]) & 3) == 0))"
4805   [(parallel [(set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -4)))
4806                    (match_dup 0))
4807               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -8)))
4808                    (match_dup 1))
4809               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -12)))
4810                    (match_dup 2))
4811               (set (reg:SI SP_REG)
4812                    (plus:SI (reg:SI SP_REG)
4813                             (const_int -12)))])]
4814   "")
4815
4816 (define_peephole2
4817   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4818         (match_operand:SI 0 "register_operand" ""))
4819    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4820         (match_operand:SI 1 "register_operand" ""))
4821    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4822         (match_operand:SI 2 "register_operand" ""))]
4823   "TARGET_H8300S && TARGET_NORMAL_MODE
4824    && (REGNO_REG_CLASS (REGNO (operands[2])) == GENERAL_REGS
4825        && REGNO (operands[1]) == REGNO (operands[0]) + 1
4826        && REGNO (operands[2]) == REGNO (operands[0]) + 2
4827        && (TARGET_H8300SX || (REGNO (operands[0]) & 3) == 0))"
4828   [(parallel [(set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -4)))
4829                    (match_dup 0))
4830               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -8)))
4831                    (match_dup 1))
4832               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -12)))
4833                    (match_dup 2))
4834               (set (reg:HI SP_REG)
4835                    (plus:HI (reg:HI SP_REG)
4836                             (const_int -12)))])]
4837   "")
4838
4839 ;; Cram two pushes into stm.l.
4840
4841 (define_peephole2
4842   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4843         (match_operand:SI 0 "register_operand" ""))
4844    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
4845         (match_operand:SI 1 "register_operand" ""))]
4846   "TARGET_H8300S && !TARGET_NORMAL_MODE
4847    && (REGNO_REG_CLASS (REGNO (operands[1])) == GENERAL_REGS
4848        && REGNO (operands[1]) == REGNO (operands[0]) + 1
4849        && (TARGET_H8300SX || (REGNO (operands[0]) & 1) == 0))"
4850   [(parallel [(set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -4)))
4851                    (match_dup 0))
4852               (set (mem:SI (plus:SI (reg:SI SP_REG) (const_int -8)))
4853                    (match_dup 1))
4854               (set (reg:SI SP_REG)
4855                    (plus:SI (reg:SI SP_REG)
4856                             (const_int -8)))])]
4857   "")
4858
4859 (define_peephole2
4860   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4861         (match_operand:SI 0 "register_operand" ""))
4862    (set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
4863         (match_operand:SI 1 "register_operand" ""))]
4864   "TARGET_H8300S && TARGET_NORMAL_MODE
4865    && (REGNO_REG_CLASS (REGNO (operands[1])) == GENERAL_REGS
4866        && REGNO (operands[1]) == REGNO (operands[0]) + 1
4867        && (TARGET_H8300SX || (REGNO (operands[0]) & 1) == 0))"
4868   [(parallel [(set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -4)))
4869                    (match_dup 0))
4870               (set (mem:SI (plus:HI (reg:HI SP_REG) (const_int -8)))
4871                    (match_dup 1))
4872               (set (reg:HI SP_REG)
4873                    (plus:HI (reg:HI SP_REG)
4874                             (const_int -8)))])]
4875   "")
4876
4877 ;; Turn
4878 ;;
4879 ;;   mov.w #2,r0
4880 ;;   add.w r7,r0  (6 bytes)
4881 ;;
4882 ;; into
4883 ;;
4884 ;;   mov.w r7,r0
4885 ;;   adds  #2,r0  (4 bytes)
4886
4887 (define_peephole2
4888   [(set (match_operand:HI 0 "register_operand" "")
4889         (match_operand:HI 1 "const_int_operand" ""))
4890    (set (match_dup 0)
4891         (plus:HI (match_dup 0)
4892                  (match_operand:HI 2 "register_operand" "")))]
4893   "REG_P (operands[0]) && REG_P (operands[2])
4894    && REGNO (operands[0]) != REGNO (operands[2])
4895    && (satisfies_constraint_J (operands[1])
4896        || satisfies_constraint_L (operands[1])
4897        || satisfies_constraint_N (operands[1]))"
4898   [(set (match_dup 0)
4899         (match_dup 2))
4900    (set (match_dup 0)
4901         (plus:HI (match_dup 0)
4902                  (match_dup 1)))]
4903   "")
4904
4905 ;; Turn
4906 ;;
4907 ;;   sub.l  er0,er0
4908 ;;   add.b  #4,r0l
4909 ;;   add.l  er7,er0  (6 bytes)
4910 ;;
4911 ;; into
4912 ;;
4913 ;;   mov.l  er7,er0
4914 ;;   adds   #4,er0   (4 bytes)
4915
4916 (define_peephole2
4917   [(set (match_operand:SI 0 "register_operand" "")
4918         (match_operand:SI 1 "const_int_operand" ""))
4919    (set (match_dup 0)
4920         (plus:SI (match_dup 0)
4921                  (match_operand:SI 2 "register_operand" "")))]
4922   "(TARGET_H8300H || TARGET_H8300S)
4923     && REG_P (operands[0]) && REG_P (operands[2])
4924     && REGNO (operands[0]) != REGNO (operands[2])
4925     && (satisfies_constraint_L (operands[1])
4926         || satisfies_constraint_N (operands[1]))"
4927   [(set (match_dup 0)
4928         (match_dup 2))
4929    (set (match_dup 0)
4930         (plus:SI (match_dup 0)
4931                  (match_dup 1)))]
4932   "")
4933
4934 ;; Turn
4935 ;;
4936 ;;   mov.l er7,er0
4937 ;;   add.l #10,er0  (takes 8 bytes)
4938 ;;
4939 ;; into
4940 ;;
4941 ;;   sub.l er0,er0
4942 ;;   add.b #10,r0l
4943 ;;   add.l er7,er0  (takes 6 bytes)
4944
4945 (define_peephole2
4946   [(set (match_operand:SI 0 "register_operand" "")
4947         (match_operand:SI 1 "register_operand" ""))
4948    (set (match_dup 0)
4949         (plus:SI (match_dup 0)
4950                  (match_operand:SI 2 "const_int_operand" "")))]
4951   "(TARGET_H8300H || TARGET_H8300S)
4952     && REG_P (operands[0]) && REG_P (operands[1])
4953     && REGNO (operands[0]) != REGNO (operands[1])
4954     && !satisfies_constraint_L (operands[2])
4955     && !satisfies_constraint_N (operands[2])
4956     && ((INTVAL (operands[2]) & 0xff) == INTVAL (operands[2])
4957         || (INTVAL (operands[2]) & 0xff00) == INTVAL (operands[2])
4958         || INTVAL (operands[2]) == 0xffff
4959         || INTVAL (operands[2]) == 0xfffe)"
4960   [(set (match_dup 0)
4961         (match_dup 2))
4962    (set (match_dup 0)
4963         (plus:SI (match_dup 0)
4964                  (match_dup 1)))]
4965   "")
4966
4967 ;; Turn
4968 ;;
4969 ;;   subs   #1,er4
4970 ;;   mov.w  r4,r4
4971 ;;   bne    .L2028
4972 ;;
4973 ;; into
4974 ;;
4975 ;;   dec.w  #1,r4
4976 ;;   bne    .L2028
4977
4978 (define_peephole2
4979   [(set (match_operand:HI 0 "register_operand" "")
4980         (plus:HI (match_dup 0)
4981                  (match_operand 1 "incdec_operand" "")))
4982    (set (cc0) (compare (match_dup 0)
4983                        (const_int 0)))
4984    (set (pc)
4985         (if_then_else (match_operator 3 "eqne_operator"
4986                        [(cc0) (const_int 0)])
4987                       (label_ref (match_operand 2 "" ""))
4988                       (pc)))]
4989   "TARGET_H8300H || TARGET_H8300S"
4990   [(set (match_operand:HI 0 "register_operand" "")
4991         (unspec:HI [(match_dup 0)
4992                     (match_dup 1)]
4993                    UNSPEC_INCDEC))
4994    (set (cc0) (compare (match_dup 0)
4995                        (const_int 0)))
4996    (set (pc)
4997         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
4998                       (label_ref (match_dup 2))
4999                       (pc)))]
5000   "")
5001
5002 ;; The SImode version of the previous pattern.
5003
5004 (define_peephole2
5005   [(set (match_operand:SI 0 "register_operand" "")
5006         (plus:SI (match_dup 0)
5007                  (match_operand 1 "incdec_operand" "")))
5008    (set (cc0) (compare (match_dup 0)
5009                        (const_int 0)))
5010    (set (pc)
5011         (if_then_else (match_operator 3 "eqne_operator"
5012                        [(cc0) (const_int 0)])
5013                       (label_ref (match_operand 2 "" ""))
5014                       (pc)))]
5015   "TARGET_H8300H || TARGET_H8300S"
5016   [(set (match_operand:SI 0 "register_operand" "")
5017         (unspec:SI [(match_dup 0)
5018                     (match_dup 1)]
5019                    UNSPEC_INCDEC))
5020    (set (cc0) (compare (match_dup 0)
5021                        (const_int 0)))
5022    (set (pc)
5023         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
5024                       (label_ref (match_dup 2))
5025                       (pc)))]
5026   "")
5027
5028 (define_peephole2
5029   [(parallel [(set (cc0)
5030                    (compare (zero_extract:SI (match_operand:QI 0 "register_operand" "")
5031                                              (const_int 1)
5032                                              (const_int 7))
5033                             (const_int 0)))
5034               (clobber (scratch:QI))])
5035    (set (pc)
5036         (if_then_else (match_operator 1 "eqne_operator"
5037                        [(cc0) (const_int 0)])
5038                       (label_ref (match_operand 2 "" ""))
5039                       (pc)))]
5040   "TARGET_H8300H || TARGET_H8300S"
5041   [(set (cc0) (compare (match_dup 0)
5042                        (const_int 0)))
5043    (set (pc)
5044         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
5045                       (label_ref (match_dup 2))
5046                       (pc)))]
5047   {
5048     operands[3] = ((GET_CODE (operands[1]) == EQ)
5049                    ? gen_rtx_GE (VOIDmode, cc0_rtx, const0_rtx)
5050                    : gen_rtx_LT (VOIDmode, cc0_rtx, const0_rtx));
5051   })
5052
5053 ;; The next three peephole2's will try to transform
5054 ;;
5055 ;;   mov.b A,r0l    (or mov.l A,er0)
5056 ;;   and.l #CST,er0
5057 ;;
5058 ;; into
5059 ;;
5060 ;;   sub.l er0
5061 ;;   mov.b A,r0l
5062 ;;   and.b #CST,r0l (if CST is not 255)
5063
5064 (define_peephole2
5065   [(set (match_operand:QI 0 "register_operand" "")
5066         (match_operand:QI 1 "general_operand" ""))
5067    (set (match_operand:SI 2 "register_operand" "")
5068         (and:SI (match_dup 2)
5069                 (const_int 255)))]
5070   "(TARGET_H8300H || TARGET_H8300S)
5071     && !reg_overlap_mentioned_p (operands[2], operands[1])
5072     && REGNO (operands[0]) == REGNO (operands[2])"
5073   [(set (match_dup 2)
5074         (const_int 0))
5075    (set (strict_low_part (match_dup 0))
5076         (match_dup 1))]
5077   "")
5078
5079 (define_peephole2
5080   [(set (match_operand:SI 0 "register_operand" "")
5081         (match_operand:SI 1 "general_operand" ""))
5082    (set (match_dup 0)
5083         (and:SI (match_dup 0)
5084                 (const_int 255)))]
5085   "(TARGET_H8300H || TARGET_H8300S)
5086     && !reg_overlap_mentioned_p (operands[0], operands[1])
5087     && !(GET_CODE (operands[1]) == MEM && !offsettable_memref_p (operands[1]))
5088     && !(GET_CODE (operands[1]) == MEM && MEM_VOLATILE_P (operands[1]))"
5089   [(set (match_dup 0)
5090         (const_int 0))
5091    (set (strict_low_part (match_dup 2))
5092         (match_dup 3))]
5093   {
5094     operands[2] = gen_lowpart (QImode, operands[0]);
5095     operands[3] = gen_lowpart (QImode, operands[1]);
5096   })
5097
5098 (define_peephole2
5099   [(set (match_operand 0 "register_operand" "")
5100         (match_operand 1 "register_operand" ""))
5101    (set (match_operand:SI 2 "register_operand" "")
5102         (and:SI (match_dup 2)
5103                 (match_operand:SI 3 "const_int_qi_operand" "")))]
5104   "(TARGET_H8300H || TARGET_H8300S)
5105     && (GET_MODE (operands[0]) == QImode
5106         || GET_MODE (operands[0]) == HImode
5107         || GET_MODE (operands[0]) == SImode)
5108     && GET_MODE (operands[0]) == GET_MODE (operands[1])
5109     && REGNO (operands[0]) == REGNO (operands[2])
5110     && !reg_overlap_mentioned_p (operands[2], operands[1])
5111     && !(GET_CODE (operands[1]) == MEM
5112          && !offsettable_memref_p (operands[1]))
5113     && !(GET_CODE (operands[1]) == MEM
5114          && MEM_VOLATILE_P (operands[1]))"
5115   [(set (match_dup 2)
5116         (const_int 0))
5117    (set (strict_low_part (match_dup 4))
5118         (match_dup 5))
5119    (set (match_dup 2)
5120         (and:SI (match_dup 2)
5121                 (match_dup 6)))]
5122   {
5123     operands[4] = gen_lowpart (QImode, operands[0]);
5124     operands[5] = gen_lowpart (QImode, operands[1]);
5125     operands[6] = GEN_INT (~0xff | INTVAL (operands[3]));
5126   })
5127
5128 (define_peephole2
5129   [(set (match_operand:SI 0 "register_operand" "")
5130         (match_operand:SI 1 "register_operand" ""))
5131    (set (match_dup 0)
5132         (and:SI (match_dup 0)
5133                 (const_int 65280)))]
5134   "(TARGET_H8300H || TARGET_H8300S)
5135    && !reg_overlap_mentioned_p (operands[0], operands[1])"
5136   [(set (match_dup 0)
5137         (const_int 0))
5138    (set (zero_extract:SI (match_dup 0)
5139                          (const_int 8)
5140                          (const_int 8))
5141         (lshiftrt:SI (match_dup 1)
5142                      (const_int 8)))]
5143   "")
5144
5145 ;; If a load of mem:SI is followed by an AND that turns off the upper
5146 ;; half, then we can load mem:HI instead.
5147
5148 (define_peephole2
5149   [(set (match_operand:SI 0 "register_operand" "")
5150         (match_operand:SI 1 "memory_operand" ""))
5151    (set (match_dup 0)
5152         (and:SI (match_dup 0)
5153                 (match_operand:SI 2 "const_int_operand" "")))]
5154   "(TARGET_H8300H || TARGET_H8300S)
5155     && !MEM_VOLATILE_P (operands[1])
5156     && offsettable_memref_p (operands[1])
5157     && (INTVAL (operands[2]) & ~0xffff) == 0
5158     && INTVAL (operands[2]) != 255"
5159   [(set (match_dup 3)
5160         (match_dup 4))
5161    (set (match_dup 0)
5162         (and:SI (match_dup 0)
5163                 (match_dup 2)))]
5164   {
5165     operands[3] = gen_lowpart (HImode, operands[0]);
5166     operands[4] = gen_lowpart (HImode, operands[1]);
5167   })
5168
5169 ;; Convert a memory comparison to a move if there is a scratch register.
5170
5171 (define_peephole2
5172   [(match_scratch:QI 1 "r")
5173    (set (cc0)
5174         (compare (match_operand:QI 0 "memory_operand" "")
5175                  (const_int 0)))]
5176   ""
5177   [(set (match_dup 1)
5178         (match_dup 0))
5179    (set (cc0) (compare (match_dup 1)
5180                        (const_int 0)))]
5181   "")
5182
5183 (define_peephole2
5184   [(match_scratch:HI 1 "r")
5185    (set (cc0)
5186         (compare (match_operand:HI 0 "memory_operand" "")
5187                  (const_int 0)))]
5188   "TARGET_H8300H || TARGET_H8300S"
5189   [(set (match_dup 1)
5190         (match_dup 0))
5191    (set (cc0) (compare (match_dup 1)
5192                        (const_int 0)))]
5193   "")
5194
5195 (define_peephole2
5196   [(match_scratch:SI 1 "r")
5197    (set (cc0)
5198         (compare (match_operand:SI 0 "memory_operand" "")
5199                  (const_int 0)))]
5200   "TARGET_H8300H || TARGET_H8300S"
5201   [(set (match_dup 1)
5202         (match_dup 0))
5203    (set (cc0) (compare (match_dup 1)
5204                        (const_int 0)))]
5205   "")
5206
5207
5208 ;; (compare (reg:HI) (const_int)) takes 4 bytes, so we try to achieve
5209 ;; the equivalent with shorter sequences.  Here is the summary.  Cases
5210 ;; are grouped for each define_peephole2.
5211 ;;
5212 ;; reg  const_int                   use     insn
5213 ;; --------------------------------------------------------
5214 ;; dead    -2                       eq/ne   inc.l
5215 ;; dead    -1                       eq/ne   inc.l
5216 ;; dead     1                       eq/ne   dec.l
5217 ;; dead     2                       eq/ne   dec.l
5218 ;;
5219 ;; dead     1                       ge/lt shar.l
5220 ;; dead     3 (H8S)                 ge/lt shar.l
5221 ;;
5222 ;; dead     1                       geu/ltu shar.l
5223 ;; dead     3 (H8S)                 geu/ltu shar.l
5224 ;;
5225 ;; ----   255                       ge/lt mov.b
5226 ;;
5227 ;; ----   255                       geu/ltu mov.b
5228
5229 ;; Transform
5230 ;;
5231 ;;      cmp.w   #1,r0
5232 ;;      bne     .L1
5233 ;;
5234 ;; into
5235 ;;
5236 ;;      dec.w   #1,r0
5237 ;;      bne     .L1
5238
5239 (define_peephole2
5240   [(set (cc0)
5241         (compare (match_operand:HI 0 "register_operand" "")
5242                  (match_operand:HI 1 "incdec_operand" "")))
5243    (set (pc)
5244         (if_then_else (match_operator 3 "eqne_operator"
5245                        [(cc0) (const_int 0)])
5246                       (label_ref (match_operand 2 "" ""))
5247                       (pc)))]
5248   "(TARGET_H8300H || TARGET_H8300S)
5249     && INTVAL (operands[1]) != 0
5250     && peep2_reg_dead_p (1, operands[0])"
5251   [(set (match_dup 0)
5252         (unspec:HI [(match_dup 0)
5253                     (match_dup 4)]
5254                    UNSPEC_INCDEC))
5255    (set (cc0) (compare (match_dup 0)
5256                        (const_int 0)))
5257    (set (pc)
5258         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
5259                       (label_ref (match_dup 2))
5260                       (pc)))]
5261   {
5262     operands[4] = GEN_INT (- INTVAL (operands[1]));
5263   })
5264
5265 ;; Transform
5266 ;;
5267 ;;      cmp.w   #1,r0
5268 ;;      bgt     .L1
5269 ;;
5270 ;; into
5271 ;;
5272 ;;      shar.w  r0
5273 ;;      bgt     .L1
5274
5275 (define_peephole2
5276   [(set (cc0)
5277         (compare (match_operand:HI 0 "register_operand" "")
5278                  (match_operand:HI 1 "const_int_operand" "")))
5279    (set (pc)
5280         (if_then_else (match_operator 2 "gtle_operator"
5281                        [(cc0) (const_int 0)])
5282                       (label_ref (match_operand 3 "" ""))
5283                       (pc)))]
5284   "(TARGET_H8300H || TARGET_H8300S)
5285     && peep2_reg_dead_p (1, operands[0])
5286     && (INTVAL (operands[1]) == 1
5287         || (TARGET_H8300S && INTVAL (operands[1]) == 3))"
5288   [(parallel [(set (match_dup 0)
5289                    (ashiftrt:HI (match_dup 0)
5290                                 (match_dup 4)))
5291               (clobber (scratch:QI))])
5292    (set (cc0) (compare (match_dup 0)
5293                        (const_int 0)))
5294    (set (pc)
5295         (if_then_else (match_dup 2)
5296                       (label_ref (match_dup 3))
5297                       (pc)))]
5298   {
5299     operands[4] = GEN_INT (exact_log2 (INTVAL (operands[1]) + 1));
5300   })
5301
5302 ;; Transform
5303 ;;
5304 ;;      cmp.w   #1,r0
5305 ;;      bhi     .L1
5306 ;;
5307 ;; into
5308 ;;
5309 ;;      shar.w  r0
5310 ;;      bne     .L1
5311
5312 (define_peephole2
5313   [(set (cc0)
5314         (compare (match_operand:HI 0 "register_operand" "")
5315                  (match_operand:HI 1 "const_int_operand" "")))
5316    (set (pc)
5317         (if_then_else (match_operator 2 "gtuleu_operator"
5318                        [(cc0) (const_int 0)])
5319                       (label_ref (match_operand 3 "" ""))
5320                       (pc)))]
5321   "(TARGET_H8300H || TARGET_H8300S)
5322     && peep2_reg_dead_p (1, operands[0])
5323     && (INTVAL (operands[1]) == 1
5324         || (TARGET_H8300S && INTVAL (operands[1]) == 3))"
5325   [(parallel [(set (match_dup 0)
5326                    (ashiftrt:HI (match_dup 0)
5327                                 (match_dup 4)))
5328               (clobber (scratch:QI))])
5329    (set (cc0) (compare (match_dup 0)
5330                        (const_int 0)))
5331    (set (pc)
5332         (if_then_else (match_dup 5)
5333                       (label_ref (match_dup 3))
5334                       (pc)))]
5335   {
5336     operands[4] = GEN_INT (exact_log2 (INTVAL (operands[1]) + 1));
5337     operands[5] = gen_rtx_fmt_ee (GET_CODE (operands[2]) == GTU ? NE : EQ,
5338                                   VOIDmode, cc0_rtx, const0_rtx);
5339   })
5340
5341 ;; Transform
5342 ;;
5343 ;;      cmp.w   #255,r0
5344 ;;      bgt     .L1
5345 ;;
5346 ;; into
5347 ;;
5348 ;;      mov.b   r0h,r0h
5349 ;;      bgt     .L1
5350
5351 (define_peephole2
5352   [(set (cc0)
5353         (compare (match_operand:HI 0 "register_operand" "")
5354                  (const_int 255)))
5355    (set (pc)
5356         (if_then_else (match_operator 1 "gtle_operator"
5357                        [(cc0) (const_int 0)])
5358                       (label_ref (match_operand 2 "" ""))
5359                       (pc)))]
5360   "TARGET_H8300H || TARGET_H8300S"
5361   [(set (cc0) (compare (and:HI (match_dup 0)
5362                                (const_int -256))
5363                        (const_int 0)))
5364    (set (pc)
5365         (if_then_else (match_dup 1)
5366                       (label_ref (match_dup 2))
5367                       (pc)))]
5368   "")
5369
5370 ;; Transform
5371 ;;
5372 ;;      cmp.w   #255,r0
5373 ;;      bhi     .L1
5374 ;;
5375 ;; into
5376 ;;
5377 ;;      mov.b   r0h,r0h
5378 ;;      bne     .L1
5379
5380 (define_peephole2
5381   [(set (cc0)
5382         (compare (match_operand:HI 0 "register_operand" "")
5383                  (const_int 255)))
5384    (set (pc)
5385         (if_then_else (match_operator 1 "gtuleu_operator"
5386                        [(cc0) (const_int 0)])
5387                       (label_ref (match_operand 2 "" ""))
5388                       (pc)))]
5389   "TARGET_H8300H || TARGET_H8300S"
5390   [(set (cc0) (compare (and:HI (match_dup 0)
5391                                (const_int -256))
5392                        (const_int 0)))
5393    (set (pc)
5394         (if_then_else (match_dup 3)
5395                       (label_ref (match_dup 2))
5396                       (pc)))]
5397   {
5398     operands[3] = gen_rtx_fmt_ee (GET_CODE (operands[1]) == GTU ? NE : EQ,
5399                                   VOIDmode, cc0_rtx, const0_rtx);
5400   })
5401
5402 ;; (compare (reg:SI) (const_int)) takes 6 bytes, so we try to achieve
5403 ;; the equivalent with shorter sequences.  Here is the summary.  Cases
5404 ;; are grouped for each define_peephole2.
5405 ;;
5406 ;; reg  const_int                   use     insn
5407 ;; --------------------------------------------------------
5408 ;; live    -2                       eq/ne   copy and inc.l
5409 ;; live    -1                       eq/ne   copy and inc.l
5410 ;; live     1                       eq/ne   copy and dec.l
5411 ;; live     2                       eq/ne   copy and dec.l
5412 ;;
5413 ;; dead    -2                       eq/ne   inc.l
5414 ;; dead    -1                       eq/ne   inc.l
5415 ;; dead     1                       eq/ne   dec.l
5416 ;; dead     2                       eq/ne   dec.l
5417 ;;
5418 ;; dead -131072                     eq/ne   inc.w and test
5419 ;; dead  -65536                     eq/ne   inc.w and test
5420 ;; dead   65536                     eq/ne   dec.w and test
5421 ;; dead  131072                     eq/ne   dec.w and test
5422 ;;
5423 ;; dead 0x000000?? except 1 and 2   eq/ne   xor.b and test
5424 ;; dead 0x0000??00                  eq/ne   xor.b and test
5425 ;; dead 0x0000ffff                  eq/ne   not.w and test
5426 ;;
5427 ;; dead 0xffffff?? except -1 and -2 eq/ne   xor.b and not.l
5428 ;; dead 0xffff??ff                  eq/ne   xor.b and not.l
5429 ;; dead 0x40000000 (H8S)            eq/ne   rotl.l and dec.l
5430 ;; dead 0x80000000                  eq/ne   rotl.l and dec.l
5431 ;;
5432 ;; live     1                       ge/lt copy and shar.l
5433 ;; live     3 (H8S)                 ge/lt copy and shar.l
5434 ;;
5435 ;; live     1                       geu/ltu copy and shar.l
5436 ;; live     3 (H8S)                 geu/ltu copy and shar.l
5437 ;;
5438 ;; dead     1                       ge/lt shar.l
5439 ;; dead     3 (H8S)                 ge/lt shar.l
5440 ;;
5441 ;; dead     1                       geu/ltu shar.l
5442 ;; dead     3 (H8S)                 geu/ltu shar.l
5443 ;;
5444 ;; dead     3 (H8/300H)             ge/lt and.b and test
5445 ;; dead     7                       ge/lt and.b and test
5446 ;; dead    15                       ge/lt and.b and test
5447 ;; dead    31                       ge/lt and.b and test
5448 ;; dead    63                       ge/lt and.b and test
5449 ;; dead   127                       ge/lt and.b and test
5450 ;; dead   255                       ge/lt and.b and test
5451 ;;
5452 ;; dead     3 (H8/300H)             geu/ltu and.b and test
5453 ;; dead     7                       geu/ltu and.b and test
5454 ;; dead    15                       geu/ltu and.b and test
5455 ;; dead    31                       geu/ltu and.b and test
5456 ;; dead    63                       geu/ltu and.b and test
5457 ;; dead   127                       geu/ltu and.b and test
5458 ;; dead   255                       geu/ltu and.b and test
5459 ;;
5460 ;; ---- 65535                       ge/lt mov.w
5461 ;;
5462 ;; ---- 65535                       geu/ltu mov.w
5463
5464 ;; Transform
5465 ;;
5466 ;;      cmp.l   #1,er0
5467 ;;      beq     .L1
5468 ;;
5469 ;; into
5470 ;;
5471 ;;      dec.l   #1,er0
5472 ;;      beq     .L1
5473
5474 (define_peephole2
5475   [(set (cc0)
5476         (compare (match_operand:SI 0 "register_operand" "")
5477                  (match_operand:SI 1 "incdec_operand" "")))
5478    (set (pc)
5479         (if_then_else (match_operator 3 "eqne_operator"
5480                        [(cc0) (const_int 0)])
5481                       (label_ref (match_operand 2 "" ""))
5482                       (pc)))]
5483   "(TARGET_H8300H || TARGET_H8300S)
5484     && INTVAL (operands[1]) != 0
5485     && peep2_reg_dead_p (1, operands[0])"
5486   [(set (match_dup 0)
5487         (unspec:SI [(match_dup 0)
5488                     (match_dup 4)]
5489                    UNSPEC_INCDEC))
5490    (set (cc0) (compare (match_dup 0)
5491                        (const_int 0)))
5492    (set (pc)
5493         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
5494                       (label_ref (match_dup 2))
5495                       (pc)))]
5496   {
5497     operands[4] = GEN_INT (- INTVAL (operands[1]));
5498   })
5499
5500 ;; Transform
5501 ;;
5502 ;;      cmp.l   #65536,er0
5503 ;;      beq     .L1
5504 ;;
5505 ;; into
5506 ;;
5507 ;;      dec.l   #1,e0
5508 ;;      beq     .L1
5509
5510 (define_peephole2
5511   [(set (cc0)
5512         (compare (match_operand:SI 0 "register_operand" "")
5513                  (match_operand:SI 1 "const_int_operand" "")))
5514    (set (pc)
5515         (if_then_else (match_operator 3 "eqne_operator"
5516                        [(cc0) (const_int 0)])
5517                       (label_ref (match_operand 2 "" ""))
5518                       (pc)))]
5519   "(TARGET_H8300H || TARGET_H8300S)
5520     && peep2_reg_dead_p (1, operands[0])
5521     && (INTVAL (operands[1]) == -131072
5522         || INTVAL (operands[1]) == -65536
5523         || INTVAL (operands[1]) == 65536
5524         || INTVAL (operands[1]) == 131072)"
5525   [(set (match_dup 0)
5526         (plus:SI (match_dup 0)
5527                  (match_dup 4)))
5528    (set (cc0) (compare (match_dup 0)
5529                        (const_int 0)))
5530    (set (pc)
5531         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
5532                       (label_ref (match_dup 2))
5533                       (pc)))]
5534   {
5535     operands[4] = GEN_INT (- INTVAL (operands[1]));
5536   })
5537
5538 ;; Transform
5539 ;;
5540 ;;      cmp.l   #100,er0
5541 ;;      beq     .L1
5542 ;;
5543 ;; into
5544 ;;
5545 ;;      xor.b   #100,er0
5546 ;;      mov.l   er0,er0
5547 ;;      beq     .L1
5548
5549 (define_peephole2
5550   [(set (cc0)
5551         (compare (match_operand:SI 0 "register_operand" "")
5552                  (match_operand:SI 1 "const_int_operand" "")))
5553    (set (pc)
5554         (if_then_else (match_operator 3 "eqne_operator"
5555                        [(cc0) (const_int 0)])
5556                       (label_ref (match_operand 2 "" ""))
5557                       (pc)))]
5558   "(TARGET_H8300H || TARGET_H8300S)
5559     && peep2_reg_dead_p (1, operands[0])
5560     && ((INTVAL (operands[1]) & 0x00ff) == INTVAL (operands[1])
5561         || (INTVAL (operands[1]) & 0xff00) == INTVAL (operands[1])
5562         || INTVAL (operands[1]) == 0x0000ffff)
5563     && INTVAL (operands[1]) != 0
5564     && INTVAL (operands[1]) != 1
5565     && INTVAL (operands[1]) != 2"
5566   [(set (match_dup 0)
5567         (xor:SI (match_dup 0)
5568                 (match_dup 1)))
5569    (set (cc0) (compare (match_dup 0)
5570                        (const_int 0)))
5571    (set (pc)
5572         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
5573                       (label_ref (match_dup 2))
5574                       (pc)))]
5575   "")
5576
5577 ;; Transform
5578 ;;
5579 ;;      cmp.l   #-100,er0
5580 ;;      beq     .L1
5581 ;;
5582 ;; into
5583 ;;
5584 ;;      xor.b   #99,er0
5585 ;;      not.l   er0
5586 ;;      beq     .L1
5587
5588 (define_peephole2
5589   [(set (cc0)
5590         (compare (match_operand:SI 0 "register_operand" "")
5591                  (match_operand:SI 1 "const_int_operand" "")))
5592    (set (pc)
5593         (if_then_else (match_operator 3 "eqne_operator"
5594                        [(cc0) (const_int 0)])
5595                       (label_ref (match_operand 2 "" ""))
5596                       (pc)))]
5597   "(TARGET_H8300H || TARGET_H8300S)
5598     && peep2_reg_dead_p (1, operands[0])
5599     && ((INTVAL (operands[1]) | 0x00ff) == -1
5600         || (INTVAL (operands[1]) | 0xff00) == -1)
5601     && INTVAL (operands[1]) != -1
5602     && INTVAL (operands[1]) != -2"
5603   [(set (match_dup 0)
5604         (xor:SI (match_dup 0)
5605                 (match_dup 4)))
5606    (set (match_dup 0)
5607         (not:SI (match_dup 0)))
5608    (set (cc0) (compare (match_dup 0)
5609                        (const_int 0)))
5610    (set (pc)
5611         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
5612                       (label_ref (match_dup 2))
5613                       (pc)))]
5614   {
5615     operands[4] = GEN_INT (INTVAL (operands[1]) ^ -1);
5616   })
5617
5618 ;; Transform
5619 ;;
5620 ;;      cmp.l   #-2147483648,er0
5621 ;;      beq     .L1
5622 ;;
5623 ;; into
5624 ;;
5625 ;;      rotl.l  er0
5626 ;;      dec.l   #1,er0
5627 ;;      beq     .L1
5628
5629 (define_peephole2
5630   [(set (cc0)
5631         (compare (match_operand:SI 0 "register_operand" "")
5632                  (match_operand:SI 1 "const_int_operand" "")))
5633    (set (pc)
5634         (if_then_else (match_operator 3 "eqne_operator"
5635                        [(cc0) (const_int 0)])
5636                       (label_ref (match_operand 2 "" ""))
5637                       (pc)))]
5638   "(TARGET_H8300H || TARGET_H8300S)
5639     && peep2_reg_dead_p (1, operands[0])
5640     && (INTVAL (operands[1]) == -2147483647 - 1
5641         || (TARGET_H8300S && INTVAL (operands[1]) == 1073741824))"
5642   [(set (match_dup 0)
5643         (rotate:SI (match_dup 0)
5644                    (match_dup 4)))
5645    (set (match_dup 0)
5646         (unspec:SI [(match_dup 0)
5647                     (const_int -1)]
5648                    UNSPEC_INCDEC))
5649    (set (cc0) (compare (match_dup 0)
5650                        (const_int 0)))
5651    (set (pc)
5652         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
5653                       (label_ref (match_dup 2))
5654                       (pc)))]
5655   {
5656     operands[4] = GEN_INT (INTVAL (operands[1]) == -2147483647 - 1 ? 1 : 2);
5657   })
5658
5659 ;; Transform
5660 ;;
5661 ;;      cmp.l   #1,er0
5662 ;;      bgt     .L1
5663 ;;
5664 ;; into
5665 ;;
5666 ;;      mov.l   er0,er1
5667 ;;      shar.l  er1
5668 ;;      bgt     .L1
5669
5670 ;; We avoid this transformation if we see more than one copy of the
5671 ;; same compare insn immediately before this one.
5672
5673 (define_peephole2
5674   [(match_scratch:SI 4 "r")
5675    (set (cc0)
5676         (compare (match_operand:SI 0 "register_operand" "")
5677                  (match_operand:SI 1 "const_int_operand" "")))
5678    (set (pc)
5679         (if_then_else (match_operator 2 "gtle_operator"
5680                        [(cc0) (const_int 0)])
5681                       (label_ref (match_operand 3 "" ""))
5682                       (pc)))]
5683   "(TARGET_H8300H || TARGET_H8300S)
5684     && !peep2_reg_dead_p (1, operands[0])
5685     && (INTVAL (operands[1]) == 1
5686         || (TARGET_H8300S && INTVAL (operands[1]) == 3))
5687     && !same_cmp_preceding_p (insn)"
5688   [(set (match_dup 4)
5689         (match_dup 0))
5690    (parallel [(set (match_dup 4)
5691                    (ashiftrt:SI (match_dup 4)
5692                                 (match_dup 5)))
5693               (clobber (scratch:QI))])
5694    (set (cc0) (compare (match_dup 4)
5695                        (const_int 0)))
5696    (set (pc)
5697         (if_then_else (match_dup 2)
5698                       (label_ref (match_dup 3))
5699                       (pc)))]
5700   {
5701     operands[5] = GEN_INT (exact_log2 (INTVAL (operands[1]) + 1));
5702   })
5703
5704 ;; Transform
5705 ;;
5706 ;;      cmp.l   #1,er0
5707 ;;      bhi     .L1
5708 ;;
5709 ;; into
5710 ;;
5711 ;;      mov.l   er0,er1
5712 ;;      shar.l  er1
5713 ;;      bne     .L1
5714
5715 ;; We avoid this transformation if we see more than one copy of the
5716 ;; same compare insn immediately before this one.
5717
5718 (define_peephole2
5719   [(match_scratch:SI 4 "r")
5720    (set (cc0)
5721         (compare (match_operand:SI 0 "register_operand" "")
5722                  (match_operand:SI 1 "const_int_operand" "")))
5723    (set (pc)
5724         (if_then_else (match_operator 2 "gtuleu_operator"
5725                          [(cc0) (const_int 0)])
5726                       (label_ref (match_operand 3 "" ""))
5727                       (pc)))]
5728   "(TARGET_H8300H || TARGET_H8300S)
5729     && !peep2_reg_dead_p (1, operands[0])
5730     && (INTVAL (operands[1]) == 1
5731         || (TARGET_H8300S && INTVAL (operands[1]) == 3))
5732     && !same_cmp_preceding_p (insn)"
5733   [(set (match_dup 4)
5734         (match_dup 0))
5735    (parallel [(set (match_dup 4)
5736                    (ashiftrt:SI (match_dup 4)
5737                                 (match_dup 5)))
5738               (clobber (scratch:QI))])
5739    (set (cc0) (compare (match_dup 4)
5740                        (const_int 0)))
5741    (set (pc)
5742         (if_then_else (match_dup 6)
5743                       (label_ref (match_dup 3))
5744                       (pc)))]
5745   {
5746     operands[5] = GEN_INT (exact_log2 (INTVAL (operands[1]) + 1));
5747     operands[6] = gen_rtx_fmt_ee (GET_CODE (operands[2]) == GTU ? NE : EQ,
5748                                   VOIDmode, cc0_rtx, const0_rtx);
5749   })
5750
5751 ;; Transform
5752 ;;
5753 ;;      cmp.l   #1,er0
5754 ;;      bgt     .L1
5755 ;;
5756 ;; into
5757 ;;
5758 ;;      shar.l  er0
5759 ;;      bgt     .L1
5760
5761 (define_peephole2
5762   [(set (cc0)
5763         (compare (match_operand:SI 0 "register_operand" "")
5764                  (match_operand:SI 1 "const_int_operand" "")))
5765    (set (pc)
5766         (if_then_else (match_operator 2 "gtle_operator"
5767                        [(cc0) (const_int 0)])
5768                       (label_ref (match_operand 3 "" ""))
5769                       (pc)))]
5770   "(TARGET_H8300H || TARGET_H8300S)
5771     && peep2_reg_dead_p (1, operands[0])
5772     && (INTVAL (operands[1]) == 1
5773         || (TARGET_H8300S && INTVAL (operands[1]) == 3))"
5774   [(parallel [(set (match_dup 0)
5775                    (ashiftrt:SI (match_dup 0)
5776                                 (match_dup 4)))
5777               (clobber (scratch:QI))])
5778    (set (cc0) (compare (match_dup 0)
5779                        (const_int 0)))
5780    (set (pc)
5781         (if_then_else (match_dup 2)
5782                       (label_ref (match_dup 3))
5783                       (pc)))]
5784   {
5785     operands[4] = GEN_INT (exact_log2 (INTVAL (operands[1]) + 1));
5786   })
5787
5788 ;; Transform
5789 ;;
5790 ;;      cmp.l   #1,er0
5791 ;;      bhi     .L1
5792 ;;
5793 ;; into
5794 ;;
5795 ;;      shar.l  er0
5796 ;;      bne     .L1
5797
5798 (define_peephole2
5799   [(set (cc0)
5800         (compare (match_operand:SI 0 "register_operand" "")
5801                  (match_operand:SI 1 "const_int_operand" "")))
5802    (set (pc)
5803         (if_then_else (match_operator 2 "gtuleu_operator"
5804                        [(cc0) (const_int 0)])
5805                       (label_ref (match_operand 3 "" ""))
5806                       (pc)))]
5807   "(TARGET_H8300H || TARGET_H8300S)
5808     && peep2_reg_dead_p (1, operands[0])
5809     && (INTVAL (operands[1]) == 1
5810         || (TARGET_H8300S && INTVAL (operands[1]) == 3))"
5811   [(parallel [(set (match_dup 0)
5812                    (ashiftrt:SI (match_dup 0)
5813                                 (match_dup 4)))
5814               (clobber (scratch:QI))])
5815    (set (cc0) (compare (match_dup 0)
5816                        (const_int 0)))
5817    (set (pc)
5818         (if_then_else (match_dup 5)
5819                       (label_ref (match_dup 3))
5820                       (pc)))]
5821   {
5822     operands[4] = GEN_INT (exact_log2 (INTVAL (operands[1]) + 1));
5823     operands[5] = gen_rtx_fmt_ee (GET_CODE (operands[2]) == GTU ? NE : EQ,
5824                                   VOIDmode, cc0_rtx, const0_rtx);
5825   })
5826
5827 ;; Transform
5828 ;;
5829 ;;      cmp.l   #15,er0
5830 ;;      bgt     .L1
5831 ;;
5832 ;; into
5833 ;;
5834 ;;      and     #240,r0l
5835 ;;      mov.l   er0,er0
5836 ;;      bgt     .L1
5837
5838 (define_peephole2
5839   [(set (cc0)
5840         (compare (match_operand:SI 0 "register_operand" "")
5841                  (match_operand:SI 1 "const_int_operand" "")))
5842    (set (pc)
5843         (if_then_else (match_operator 2 "gtle_operator"
5844                        [(cc0) (const_int 0)])
5845                       (label_ref (match_operand 3 "" ""))
5846                       (pc)))]
5847   "(TARGET_H8300H || TARGET_H8300S)
5848     && peep2_reg_dead_p (1, operands[0])
5849     && ((TARGET_H8300H && INTVAL (operands[1]) == 3)
5850          || INTVAL (operands[1]) == 7
5851          || INTVAL (operands[1]) == 15
5852          || INTVAL (operands[1]) == 31
5853          || INTVAL (operands[1]) == 63
5854          || INTVAL (operands[1]) == 127
5855          || INTVAL (operands[1]) == 255)"
5856   [(set (match_dup 0)
5857         (and:SI (match_dup 0)
5858                 (match_dup 4)))
5859    (set (cc0) (compare (match_dup 0)
5860                        (const_int 0)))
5861    (set (pc)
5862         (if_then_else (match_dup 2)
5863                       (label_ref (match_dup 3))
5864                       (pc)))]
5865   {
5866     operands[4] = GEN_INT (~INTVAL (operands[1]));
5867   })
5868
5869 ;; Transform
5870 ;;
5871 ;;      cmp.l   #15,er0
5872 ;;      bhi     .L1
5873 ;;
5874 ;; into
5875 ;;
5876 ;;      and     #240,r0l
5877 ;;      mov.l   er0,er0
5878 ;;      bne     .L1
5879
5880 (define_peephole2
5881   [(set (cc0)
5882         (compare (match_operand:SI 0 "register_operand" "")
5883                  (match_operand:SI 1 "const_int_operand" "")))
5884    (set (pc)
5885         (if_then_else (match_operator 2 "gtuleu_operator"
5886                        [(cc0) (const_int 0)])
5887                       (label_ref (match_operand 3 "" ""))
5888                       (pc)))]
5889   "(TARGET_H8300H || TARGET_H8300S)
5890     && peep2_reg_dead_p (1, operands[0])
5891     && ((TARGET_H8300H && INTVAL (operands[1]) == 3)
5892          || INTVAL (operands[1]) == 7
5893          || INTVAL (operands[1]) == 15
5894          || INTVAL (operands[1]) == 31
5895          || INTVAL (operands[1]) == 63
5896          || INTVAL (operands[1]) == 127
5897          || INTVAL (operands[1]) == 255)"
5898   [(set (match_dup 0)
5899         (and:SI (match_dup 0)
5900                 (match_dup 4)))
5901    (set (cc0) (compare (match_dup 0)
5902                        (const_int 0)))
5903    (set (pc)
5904         (if_then_else (match_dup 5)
5905                       (label_ref (match_dup 3))
5906                       (pc)))]
5907   {
5908     operands[4] = GEN_INT (~INTVAL (operands[1]));
5909     operands[5] = gen_rtx_fmt_ee (GET_CODE (operands[2]) == GTU ? NE : EQ,
5910                                   VOIDmode, cc0_rtx, const0_rtx);
5911   })
5912
5913 ;; Transform
5914 ;;
5915 ;;      cmp.l   #65535,er0
5916 ;;      bgt     .L1
5917 ;;
5918 ;; into
5919 ;;
5920 ;;      mov.l   e0,e0
5921 ;;      bgt     .L1
5922
5923 (define_peephole2
5924   [(set (cc0)
5925         (compare (match_operand:SI 0 "register_operand" "")
5926                  (const_int 65535)))
5927    (set (pc)
5928         (if_then_else (match_operator 1 "gtle_operator"
5929                        [(cc0) (const_int 0)])
5930                       (label_ref (match_operand 2 "" ""))
5931                       (pc)))]
5932   "TARGET_H8300H || TARGET_H8300S"
5933   [(set (cc0) (compare (and:SI (match_dup 0)
5934                                (const_int -65536))
5935                        (const_int 0)))
5936    (set (pc)
5937         (if_then_else (match_dup 1)
5938                       (label_ref (match_dup 2))
5939                       (pc)))]
5940   "")
5941
5942 ;; Transform
5943 ;;
5944 ;;      cmp.l   #65535,er0
5945 ;;      bhi     .L1
5946 ;;
5947 ;; into
5948 ;;
5949 ;;      mov.l   e0,e0
5950 ;;      bne     .L1
5951
5952 (define_peephole2
5953   [(set (cc0)
5954         (compare (match_operand:SI 0 "register_operand" "")
5955                  (const_int 65535)))
5956    (set (pc)
5957         (if_then_else (match_operator 1 "gtuleu_operator"
5958                        [(cc0) (const_int 0)])
5959                       (label_ref (match_operand 2 "" ""))
5960                       (pc)))]
5961   "TARGET_H8300H || TARGET_H8300S"
5962   [(set (cc0) (compare (and:SI (match_dup 0)
5963                                (const_int -65536))
5964                        (const_int 0)))
5965    (set (pc)
5966         (if_then_else (match_dup 3)
5967                       (label_ref (match_dup 2))
5968                       (pc)))]
5969   {  
5970     operands[3] = gen_rtx_fmt_ee (GET_CODE (operands[1]) == GTU ? NE : EQ,
5971                                   VOIDmode, cc0_rtx, const0_rtx);
5972   })
5973
5974 ;; Transform
5975 ;;
5976 ;;      cmp.l   #1,er0
5977 ;;      beq     .L1
5978 ;;
5979 ;; into
5980 ;;
5981 ;;      mov.l   er0,er1
5982 ;;      dec.l   #1,er1
5983 ;;      beq     .L1
5984
5985 ;; We avoid this transformation if we see more than one copy of the
5986 ;; same compare insn.
5987
5988 (define_peephole2
5989   [(match_scratch:SI 4 "r")
5990    (set (cc0)
5991         (compare (match_operand:SI 0 "register_operand" "")
5992                  (match_operand:SI 1 "incdec_operand" "")))
5993    (set (pc)
5994         (if_then_else (match_operator 3 "eqne_operator"
5995                        [(cc0) (const_int 0)])
5996                       (label_ref (match_operand 2 "" ""))
5997                       (pc)))]
5998   "(TARGET_H8300H || TARGET_H8300S)
5999     && INTVAL (operands[1]) != 0
6000     && !peep2_reg_dead_p (1, operands[0])
6001     && !same_cmp_following_p (insn)"
6002   [(set (match_dup 4)
6003         (match_dup 0))
6004    (set (match_dup 4)
6005         (unspec:SI [(match_dup 4)
6006                     (match_dup 5)]
6007                    UNSPEC_INCDEC))
6008    (set (cc0) (compare (match_dup 4)
6009                        (const_int 0)))
6010    (set (pc)
6011         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
6012                       (label_ref (match_dup 2))
6013                       (pc)))]
6014   {
6015     operands[5] = GEN_INT (- INTVAL (operands[1]));
6016   })
6017 ;; Narrow the mode of testing if possible.
6018
6019 (define_peephole2
6020   [(set (match_operand:HI 0 "register_operand" "")
6021         (and:HI (match_dup 0)
6022                 (match_operand:HI 1 "const_int_qi_operand" "")))
6023    (set (cc0) (compare (match_dup 0)
6024                        (const_int 0)))
6025    (set (pc)
6026         (if_then_else (match_operator 3 "eqne_operator"
6027                        [(cc0) (const_int 0)])
6028                       (label_ref (match_operand 2 "" ""))
6029                       (pc)))]
6030   "peep2_reg_dead_p (2, operands[0])"
6031   [(set (match_dup 4)
6032         (and:QI (match_dup 4)
6033                 (match_dup 5)))
6034    (set (cc0) (compare (match_dup 4)
6035                        (const_int 0)))
6036    (set (pc)
6037         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
6038                       (label_ref (match_dup 2))
6039                       (pc)))]
6040   {
6041     operands[4] = gen_rtx_REG (QImode, REGNO (operands[0]));
6042     operands[5] = gen_int_mode (INTVAL (operands[1]), QImode);
6043   })
6044
6045 (define_peephole2
6046   [(set (match_operand:SI 0 "register_operand" "")
6047         (and:SI (match_dup 0)
6048                 (match_operand:SI 1 "const_int_qi_operand" "")))
6049    (set (cc0) (compare (match_dup 0)
6050                        (const_int 0)))
6051    (set (pc)
6052         (if_then_else (match_operator 3 "eqne_operator"
6053                        [(cc0) (const_int 0)])
6054                       (label_ref (match_operand 2 "" ""))
6055                       (pc)))]
6056   "peep2_reg_dead_p (2, operands[0])"
6057   [(set (match_dup 4)
6058         (and:QI (match_dup 4)
6059                 (match_dup 5)))
6060    (set (cc0) (compare (match_dup 4)
6061                        (const_int 0)))
6062    (set (pc)
6063         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
6064                       (label_ref (match_dup 2))
6065                       (pc)))]
6066   {
6067     operands[4] = gen_rtx_REG (QImode, REGNO (operands[0]));
6068     operands[5] = gen_int_mode (INTVAL (operands[1]), QImode);
6069   })
6070
6071 (define_peephole2
6072   [(set (match_operand:SI 0 "register_operand" "")
6073         (and:SI (match_dup 0)
6074                 (match_operand:SI 1 "const_int_hi_operand" "")))
6075    (set (cc0) (compare (match_dup 0)
6076                        (const_int 0)))
6077    (set (pc)
6078         (if_then_else (match_operator 3 "eqne_operator"
6079                        [(cc0) (const_int 0)])
6080                       (label_ref (match_operand 2 "" ""))
6081                       (pc)))]
6082   "peep2_reg_dead_p (2, operands[0])"
6083   [(set (match_dup 4)
6084         (and:HI (match_dup 4)
6085                 (match_dup 5)))
6086    (set (cc0) (compare (match_dup 4)
6087                        (const_int 0)))
6088    (set (pc)
6089         (if_then_else (match_op_dup 3 [(cc0) (const_int 0)])
6090                       (label_ref (match_dup 2))
6091                       (pc)))]
6092   {
6093     operands[4] = gen_rtx_REG (HImode, REGNO (operands[0]));
6094     operands[5] = gen_int_mode (INTVAL (operands[1]), HImode);
6095   })
6096
6097 (define_peephole2
6098   [(set (match_operand:SI 0 "register_operand" "")
6099         (and:SI (match_dup 0)
6100                 (match_operand:SI 1 "const_int_qi_operand" "")))
6101    (set (match_dup 0)
6102         (xor:SI (match_dup 0)
6103                 (match_operand:SI 2 "const_int_qi_operand" "")))
6104    (set (cc0) (compare (match_dup 0)
6105                        (const_int 0)))
6106    (set (pc)
6107         (if_then_else (match_operator 4 "eqne_operator"
6108                        [(cc0) (const_int 0)])
6109                       (label_ref (match_operand 3 "" ""))
6110                       (pc)))]
6111   "peep2_reg_dead_p (3, operands[0])
6112    && (~INTVAL (operands[1]) & INTVAL (operands[2])) == 0"
6113   [(set (match_dup 5)
6114         (and:QI (match_dup 5)
6115                 (match_dup 6)))
6116    (set (match_dup 5)
6117         (xor:QI (match_dup 5)
6118                 (match_dup 7)))
6119    (set (cc0) (compare (match_dup 5)
6120                        (const_int 0)))
6121    (set (pc)
6122         (if_then_else (match_op_dup 4 [(cc0) (const_int 0)])
6123                       (label_ref (match_dup 3))
6124                       (pc)))]
6125   {
6126     operands[5] = gen_rtx_REG (QImode, REGNO (operands[0]));
6127     operands[6] = gen_int_mode (INTVAL (operands[1]), QImode);
6128     operands[7] = gen_int_mode (INTVAL (operands[2]), QImode);
6129   })
6130
6131 ;; These triggers right at the end of allocation of locals in the
6132 ;; prologue (and possibly at other places).
6133
6134 ;; stack adjustment of -4, generate one push
6135 ;;
6136 ;; before : 6 bytes, 10 clocks
6137 ;; after  : 4 bytes, 10 clocks
6138
6139 (define_peephole2
6140   [(set (reg:SI SP_REG)
6141         (plus:SI (reg:SI SP_REG)
6142                  (const_int -4)))
6143    (set (mem:SI (reg:SI SP_REG))
6144         (match_operand:SI 0 "register_operand" ""))]
6145   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_NORMAL_MODE
6146     && REGNO (operands[0]) != SP_REG"
6147   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
6148         (match_dup 0))]
6149   "")
6150
6151 ;; stack adjustment of -12, generate one push
6152 ;;
6153 ;; before : 10 bytes, 14 clocks
6154 ;; after  :  8 bytes, 14 clocks
6155
6156 (define_peephole2
6157   [(set (reg:SI SP_REG)
6158         (plus:SI (reg:SI SP_REG)
6159                  (const_int -12)))
6160    (set (mem:SI (reg:SI SP_REG))
6161         (match_operand:SI 0 "register_operand" ""))]
6162   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_NORMAL_MODE
6163     && REGNO (operands[0]) != SP_REG"
6164   [(set (reg:SI SP_REG)
6165         (plus:SI (reg:SI SP_REG)
6166                  (const_int -4)))
6167    (set (reg:SI SP_REG)
6168         (plus:SI (reg:SI SP_REG)
6169                  (const_int -4)))
6170    (set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
6171         (match_dup 0))]
6172   "")
6173
6174 ;; Transform
6175 ;;
6176 ;;      mov     dst,reg
6177 ;;      op      src,reg
6178 ;;      mov     reg,dst
6179 ;;
6180 ;; into
6181 ;;
6182 ;;      op      src,dst
6183 ;;
6184 ;; if "reg" dies at the end of the sequence.
6185
6186 (define_peephole2
6187   [(set (match_operand 0 "register_operand" "")
6188         (match_operand 1 "memory_operand" ""))
6189    (set (match_dup 0)
6190         (match_operator 2 "h8sx_binary_memory_operator"
6191          [(match_dup 0)
6192           (match_operand 3 "h8300_src_operand" "")]))
6193    (set (match_operand 4 "memory_operand" "")
6194         (match_dup 0))]
6195   "0 /* Disable because it breaks compiling fp-bit.c.  */
6196    && TARGET_H8300SX
6197    && peep2_reg_dead_p (3, operands[0])
6198    && !reg_overlap_mentioned_p (operands[0], operands[3])
6199    && !reg_overlap_mentioned_p (operands[0], operands[4])
6200    && h8sx_mergeable_memrefs_p (operands[4], operands[1])"
6201   [(set (match_dup 4)
6202         (match_dup 5))]
6203   {
6204     operands[5] = shallow_copy_rtx (operands[2]);
6205     XEXP (operands[5], 0) = operands[1];
6206   })
6207
6208 ;; Transform
6209 ;;
6210 ;;      mov     src,reg
6211 ;;      op      reg,dst
6212 ;;
6213 ;; into
6214 ;;
6215 ;;      op      src,dst
6216 ;;
6217 ;; if "reg" dies in the second insn.
6218
6219 (define_peephole2
6220   [(set (match_operand 0 "register_operand" "")
6221         (match_operand 1 "h8300_src_operand" ""))
6222    (set (match_operand 2 "h8300_dst_operand" "")
6223         (match_operator 3 "h8sx_binary_memory_operator"
6224          [(match_operand 4 "h8300_dst_operand" "")
6225           (match_dup 0)]))]
6226   "0 /* Disable because it breaks compiling fp-bit.c.  */
6227    && TARGET_H8300SX
6228    && peep2_reg_dead_p (2, operands[0])
6229    && !reg_overlap_mentioned_p (operands[0], operands[4])"
6230   [(set (match_dup 2)
6231         (match_dup 5))]
6232   {
6233     operands[5] = shallow_copy_rtx (operands[3]);
6234     XEXP (operands[5], 1) = operands[1];
6235   })
6236
6237 ;; Transform
6238 ;;
6239 ;;      mov     dst,reg
6240 ;;      op      reg
6241 ;;      mov     reg,dst
6242 ;;
6243 ;; into
6244 ;;
6245 ;;      op      dst
6246 ;;
6247 ;; if "reg" dies at the end of the sequence.
6248
6249 (define_peephole2
6250   [(set (match_operand 0 "register_operand" "")
6251         (match_operand 1 "memory_operand" ""))
6252    (set (match_dup 0)
6253         (match_operator 2 "h8sx_unary_memory_operator"
6254          [(match_dup 0)]))
6255    (set (match_operand 3 "memory_operand" "")
6256         (match_dup 0))]
6257   "TARGET_H8300SX
6258    && peep2_reg_dead_p (3, operands[0])
6259    && !reg_overlap_mentioned_p (operands[0], operands[3])
6260    && h8sx_mergeable_memrefs_p (operands[3], operands[1])"
6261   [(set (match_dup 3)
6262         (match_dup 4))]
6263   {
6264     operands[4] = shallow_copy_rtx (operands[2]);
6265     XEXP (operands[4], 0) = operands[1];
6266   })
6267
6268 ;; Transform
6269 ;;
6270 ;;      mov     src1,reg
6271 ;;      cmp     reg,src2
6272 ;;
6273 ;; into
6274 ;;
6275 ;;      cmp     src1,src2
6276 ;;
6277 ;; if "reg" dies in the comparison.
6278
6279 (define_peephole2
6280   [(set (match_operand 0 "register_operand" "")
6281         (match_operand 1 "h8300_dst_operand" ""))
6282    (set (cc0)
6283         (compare (match_dup 0)
6284                  (match_operand 2 "h8300_src_operand" "")))]
6285   "TARGET_H8300SX
6286    && peep2_reg_dead_p (2, operands[0])
6287    && !reg_overlap_mentioned_p (operands[0], operands[2])
6288    && operands[2] != const0_rtx"
6289   [(set (cc0)
6290         (compare (match_dup 1)
6291                  (match_dup 2)))])
6292
6293 ;; Likewise for the second operand.
6294
6295 (define_peephole2
6296   [(set (match_operand 0 "register_operand" "")
6297         (match_operand 1 "h8300_src_operand" ""))
6298    (set (cc0)
6299         (compare (match_operand 2 "h8300_dst_operand" "")
6300                  (match_dup 0)))]
6301   "TARGET_H8300SX
6302    && peep2_reg_dead_p (2, operands[0])
6303    && !reg_overlap_mentioned_p (operands[0], operands[2])"
6304   [(set (cc0)
6305         (compare (match_dup 2)
6306                  (match_dup 1)))])
6307
6308 ;; Combine two moves.
6309
6310 (define_peephole2
6311   [(set (match_operand 0 "register_operand" "")
6312         (match_operand 1 "h8300_src_operand" ""))
6313    (set (match_operand 2 "h8300_dst_operand" "")
6314         (match_dup 0))]
6315   "TARGET_H8300SX
6316    && peep2_reg_dead_p (2, operands[0])
6317    && !reg_overlap_mentioned_p (operands[0], operands[2])"
6318   [(set (match_dup 2)
6319         (match_dup 1))])