OSDN Git Service

* config/h8300/h8300.c (print_operand) : Modify case 'V' and
[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 [(ne (symbol_ref "TARGET_H8300")  (const_int 0)) (const_int 4)
175                (ne (symbol_ref "TARGET_H8300H") (const_int 0)) (const_int 10)
176                (ne (symbol_ref "TARGET_H8300S") (const_int 0)) (const_int 10)]
177               (const_int 14)))])
178
179 (include "predicates.md")
180 \f
181 ;; ----------------------------------------------------------------------
182 ;; MOVE INSTRUCTIONS
183 ;; ----------------------------------------------------------------------
184
185 ;; movqi
186
187 (define_insn "*movqi_h8300"
188   [(set (match_operand:QI 0 "general_operand_dst" "=r,r ,<,r,r,m")
189         (match_operand:QI 1 "general_operand_src" " I,r>,r,n,m,r"))]
190   "TARGET_H8300
191    && (register_operand (operands[0], QImode)
192        || register_operand (operands[1], QImode))"
193   "@
194    sub.b        %X0,%X0
195    mov.b        %R1,%X0
196    mov.b        %X1,%R0
197    mov.b        %R1,%X0
198    mov.b        %R1,%X0
199    mov.b        %X1,%R0"
200   [(set_attr "length" "2,2,2,2,4,4")
201    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
202
203 (define_insn "*movqi_h8300hs"
204   [(set (match_operand:QI 0 "general_operand_dst" "=r,r ,<,r,r,m")
205         (match_operand:QI 1 "general_operand_src" " I,r>,r,n,m,r"))]
206   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX
207    && (register_operand (operands[0], QImode)
208        || register_operand (operands[1], QImode))"
209   "@
210    sub.b        %X0,%X0
211    mov.b        %R1,%X0
212    mov.b        %X1,%R0
213    mov.b        %R1,%X0
214    mov.b        %R1,%X0
215    mov.b        %X1,%R0"
216   [(set (attr "length")
217         (symbol_ref "compute_mov_length (operands)"))
218    (set_attr "cc" "set_zn,set_znv,set_znv,clobber,set_znv,set_znv")])
219
220 (define_insn "*movqi_h8sx"
221   [(set (match_operand:QI 0 "general_operand_dst" "=Z,rQ")
222         (match_operand:QI 1 "general_operand_src" "P4>X,rQi"))]
223   "TARGET_H8300SX"
224   "@
225     mov.b       %X1:4,%X0
226     mov.b       %X1,%X0"
227   [(set_attr "length_table" "mov_imm4,movb")
228    (set_attr "cc" "set_znv")])
229
230 (define_expand "movqi"
231   [(set (match_operand:QI 0 "general_operand_dst" "")
232         (match_operand:QI 1 "general_operand_src" ""))]
233   ""
234   "
235 {
236   /* One of the ops has to be in a register.  */
237   if (!TARGET_H8300SX
238       && !register_operand (operand0, QImode)
239       && !register_operand (operand1, QImode))
240     {
241       operands[1] = copy_to_mode_reg (QImode, operand1);
242     }
243 }")
244
245 (define_insn "movstrictqi"
246   [(set (strict_low_part (match_operand:QI 0 "general_operand_dst" "+r,r"))
247                          (match_operand:QI 1 "general_operand_src" "I,rmi>"))]
248   ""
249   "@
250    sub.b        %X0,%X0
251    mov.b        %X1,%X0"
252   [(set_attr "length" "2,*")
253    (set_attr "length_table" "*,movb")
254    (set_attr "cc" "set_zn,set_znv")])
255
256 ;; movhi
257
258 (define_insn "*movhi_h8300"
259   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,<,r,r,m")
260         (match_operand:HI 1 "general_operand_src" "I,r>,r,i,m,r"))]
261   "TARGET_H8300
262    && (register_operand (operands[0], HImode)
263        || register_operand (operands[1], HImode))
264    && !(GET_CODE (operands[0]) == MEM
265         && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
266         && GET_CODE (XEXP (XEXP (operands[0], 0), 0)) == REG
267         && GET_CODE (operands[1]) == REG
268         && REGNO (XEXP (XEXP (operands[0], 0), 0)) == REGNO (operands[1]))"
269   "@
270    sub.w        %T0,%T0
271    mov.w        %T1,%T0
272    mov.w        %T1,%T0
273    mov.w        %T1,%T0
274    mov.w        %T1,%T0
275    mov.w        %T1,%T0"
276   [(set (attr "length")
277         (symbol_ref "compute_mov_length (operands)"))
278    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
279
280 (define_insn "*movhi_h8300hs"
281   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,<,r,r,m")
282         (match_operand:HI 1 "general_operand_src" "I,r>,r,i,m,r"))]
283   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX
284    && (register_operand (operands[0], HImode)
285        || register_operand (operands[1], HImode))"
286   "@
287    sub.w        %T0,%T0
288    mov.w        %T1,%T0
289    mov.w        %T1,%T0
290    mov.w        %T1,%T0
291    mov.w        %T1,%T0
292    mov.w        %T1,%T0"
293   [(set (attr "length")
294         (symbol_ref "compute_mov_length (operands)"))
295    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
296
297 (define_insn "*movhi_h8sx"
298   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,Z,Q,rQ")
299         (match_operand:HI 1 "general_operand_src" "I,P3>X,P4>X,IP8>X,rQi"))]
300   "TARGET_H8300SX"
301   "@
302    sub.w        %T0,%T0
303    mov.w        %T1:3,%T0
304    mov.w        %T1:4,%T0
305    mov.w        %T1,%T0
306    mov.w        %T1,%T0"
307   [(set_attr "length_table" "*,*,mov_imm4,short_immediate,movw")
308    (set_attr "length" "2,2,*,*,*")
309    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv")])
310
311 (define_expand "movhi"
312   [(set (match_operand:HI 0 "general_operand_dst" "")
313         (match_operand:HI 1 "general_operand_src" ""))]
314   ""
315   "
316 {
317   /* One of the ops has to be in a register.  */
318   if (!register_operand (operand1, HImode)
319       && !register_operand (operand0, HImode))
320     {
321       operands[1] = copy_to_mode_reg (HImode, operand1);
322     }
323 }")
324
325 (define_insn "movstricthi"
326   [(set (strict_low_part (match_operand:HI 0 "general_operand_dst" "+r,r,r"))
327                          (match_operand:HI 1 "general_operand_src" "I,P3>X,rmi"))]
328   ""
329   "@
330    sub.w        %T0,%T0
331    mov.w        %T1,%T0
332    mov.w        %T1,%T0"
333   [(set_attr "length" "2,2,*")
334    (set_attr "length_table" "*,*,movw")
335    (set_attr "cc" "set_zn,set_znv,set_znv")])
336
337 ;; movsi
338
339 (define_expand "movsi"
340   [(set (match_operand:SI 0 "general_operand_dst" "")
341         (match_operand:SI 1 "general_operand_src" ""))]
342   ""
343   "
344 {
345   if (TARGET_H8300)
346     {
347       if (h8300_expand_movsi (operands))
348         DONE;
349     }
350   else if (!TARGET_H8300SX)
351     {
352       /* One of the ops has to be in a register.  */
353       if (!register_operand (operand1, SImode)
354           && !register_operand (operand0, SImode))
355         {
356           operands[1] = copy_to_mode_reg (SImode, operand1);
357         }
358     }
359 }")
360
361 (define_insn "*movsi_h8300"
362   [(set (match_operand:SI 0 "general_operand_dst" "=r,r,r,o,<,r")
363         (match_operand:SI 1 "general_operand_src" "I,r,io,r,r,>"))]
364   "TARGET_H8300
365    && (register_operand (operands[0], SImode)
366        || register_operand (operands[1], SImode))"
367   "*
368 {
369   unsigned int rn = -1;
370   switch (which_alternative)
371     {
372     case 0:
373       return \"sub.w    %e0,%e0\;sub.w  %f0,%f0\";
374     case 1:
375       if (REGNO (operands[0]) < REGNO (operands[1]))
376         return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
377       else
378         return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
379     case 2:
380       /* Make sure we don't trample the register we index with.  */
381       if (GET_CODE (operands[1]) == MEM)
382         {
383           rtx inside = XEXP (operands[1], 0);
384           if (REG_P (inside))
385             {
386               rn = REGNO (inside);
387             }
388           else if (GET_CODE (inside) == PLUS)
389             {
390               rtx lhs = XEXP (inside, 0);
391               rtx rhs = XEXP (inside, 1);
392               if (REG_P (lhs)) rn = REGNO (lhs);
393               if (REG_P (rhs)) rn = REGNO (rhs);
394             }
395         }
396       if (rn == REGNO (operands[0]))
397         {
398           /* Move the second word first.  */
399           return \"mov.w        %f1,%f0\;mov.w  %e1,%e0\";
400         }
401       else
402         {
403           if (GET_CODE (operands[1]) == CONST_INT)
404             {
405               /* If either half is zero, use sub.w to clear that
406                  half.  */
407               if ((INTVAL (operands[1]) & 0xffff) == 0)
408                 return \"mov.w  %e1,%e0\;sub.w  %f0,%f0\";
409               if (((INTVAL (operands[1]) >> 16) & 0xffff) == 0)
410                 return \"sub.w  %e0,%e0\;mov.w  %f1,%f0\";
411               /* If the upper half and the lower half are the same,
412                  copy one half to the other.  */
413               if ((INTVAL (operands[1]) & 0xffff)
414                   == ((INTVAL (operands[1]) >> 16) & 0xffff))
415                 return \"mov.w\\t%e1,%e0\;mov.w\\t%e0,%f0\";
416             }
417           return \"mov.w        %e1,%e0\;mov.w  %f1,%f0\";
418         }
419     case 3:
420       return \"mov.w    %e1,%e0\;mov.w  %f1,%f0\";
421     case 4:
422       return \"mov.w    %f1,%T0\;mov.w  %e1,%T0\";
423     case 5:
424       return \"mov.w    %T1,%e0\;mov.w  %T1,%f0\";
425     default:
426       gcc_unreachable ();
427     }
428 }"
429   [(set (attr "length")
430         (symbol_ref "compute_mov_length (operands)"))])
431
432 (define_insn "*movsi_h8300hs"
433   [(set (match_operand:SI 0 "general_operand_dst" "=r,r,r,<,r,r,m,*a,*a,r")
434         (match_operand:SI 1 "general_operand_src" "I,r,i,r,>,m,r,I,r,*a"))]
435   "(TARGET_H8300S || TARGET_H8300H) && !TARGET_H8300SX
436    && (register_operand (operands[0], SImode)
437        || register_operand (operands[1], SImode))
438    && !(GET_CODE (operands[0]) == MEM
439         && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
440         && GET_CODE (XEXP (XEXP (operands[0], 0), 0)) == REG
441         && GET_CODE (operands[1]) == REG
442         && REGNO (XEXP (XEXP (operands[0], 0), 0)) == REGNO (operands[1]))"
443   "*
444 {
445   switch (which_alternative)
446     {
447     case 0:
448       return \"sub.l    %S0,%S0\";
449     case 7:
450       return \"clrmac\";
451     case 8:
452       return \"clrmac\;ldmac %1,macl\";
453     case 9:
454       return \"stmac    macl,%0\";
455     default:
456       if (GET_CODE (operands[1]) == CONST_INT)
457         {
458           int val = INTVAL (operands[1]);
459
460           /* Look for constants which can be made by adding an 8-bit
461              number to zero in one of the two low bytes.  */
462           if (val == (val & 0xff))
463             {
464               operands[1] = GEN_INT ((char) val & 0xff);
465               return \"sub.l\\t%S0,%S0\;add.b\\t%1,%w0\";
466             }
467
468           if (val == (val & 0xff00))
469             {
470               operands[1] = GEN_INT ((char) (val >> 8) & 0xff);
471               return \"sub.l\\t%S0,%S0\;add.b\\t%1,%x0\";
472             }
473
474           /* Look for constants that can be obtained by subs, inc, and
475              dec to 0.  */
476           switch (val & 0xffffffff)
477             {
478             case 0xffffffff:
479               return \"sub.l\\t%S0,%S0\;subs\\t#1,%S0\";
480             case 0xfffffffe:
481               return \"sub.l\\t%S0,%S0\;subs\\t#2,%S0\";
482             case 0xfffffffc:
483               return \"sub.l\\t%S0,%S0\;subs\\t#4,%S0\";
484
485             case 0x0000ffff:
486               return \"sub.l\\t%S0,%S0\;dec.w\\t#1,%f0\";
487             case 0x0000fffe:
488               return \"sub.l\\t%S0,%S0\;dec.w\\t#2,%f0\";
489
490             case 0xffff0000:
491               return \"sub.l\\t%S0,%S0\;dec.w\\t#1,%e0\";
492             case 0xfffe0000:
493               return \"sub.l\\t%S0,%S0\;dec.w\\t#2,%e0\";
494
495             case 0x00010000:
496               return \"sub.l\\t%S0,%S0\;inc.w\\t#1,%e0\";
497             case 0x00020000:
498               return \"sub.l\\t%S0,%S0\;inc.w\\t#2,%e0\";
499             }
500         }
501     }
502    return \"mov.l       %S1,%S0\";
503 }"
504   [(set (attr "length")
505         (symbol_ref "compute_mov_length (operands)"))
506    (set_attr "cc" "set_zn,set_znv,clobber,set_znv,set_znv,set_znv,set_znv,none_0hit,none_0hit,set_znv")])
507
508 (define_insn "*movsi_h8sx"
509   [(set (match_operand:SI 0 "general_operand_dst" "=r,r,Q,rQ,*a,*a,r")
510         (match_operand:SI 1 "general_operand_src" "I,P3>X,IP8>X,rQi,I,r,*a"))]
511   "TARGET_H8300SX"
512   "@
513    sub.l        %S0,%S0
514    mov.l        %S1:3,%S0
515    mov.l        %S1,%S0
516    mov.l        %S1,%S0
517    clrmac
518    clrmac\;ldmac        %1,macl
519    stmac        macl,%0"
520   [(set_attr "length_table" "*,*,short_immediate,movl,*,*,*")
521    (set_attr "length" "2,2,*,*,2,6,4")
522    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,none_0hit,none_0hit,set_znv")])
523
524 (define_insn "*movsf_h8sx"
525   [(set (match_operand:SF 0 "general_operand_dst" "=r,rQ")
526         (match_operand:SF 1 "general_operand_src" "G,rQi"))]
527   "TARGET_H8300SX"
528   "@
529     sub.l       %S0,%S0
530     mov.l       %S1,%S0"
531   [(set_attr "length" "2,*")
532    (set_attr "length_table" "*,movl")
533    (set_attr "cc" "set_zn,set_znv")])
534
535 ;; Implement block moves using movmd.  Defining movmemsi allows the full
536 ;; range of constant lengths (up to 0x40000 bytes when using movmd.l).
537 ;; See h8sx_emit_movmd for details.
538 (define_expand "movmemsi"
539   [(use (match_operand:BLK 0 "memory_operand" ""))
540    (use (match_operand:BLK 1 "memory_operand" ""))
541    (use (match_operand:SI 2 "" ""))
542    (use (match_operand:SI 3 "const_int_operand" ""))]
543   "TARGET_H8300SX"
544   {
545     if (h8sx_emit_movmd (operands[0], operands[1], operands[2],
546                          INTVAL (operands[3])))
547       DONE;
548     else
549       FAIL;
550   })
551
552 ;; Expander for generating movmd insns.  Operand 0 is the destination
553 ;; memory region, operand 1 is the source, operand 2 is the counter
554 ;; register and operand 3 is the chunk size (1, 2 or 4).
555 (define_expand "movmd"
556   [(parallel
557        [(set (match_operand:BLK 0 "memory_operand" "")
558              (match_operand:BLK 1 "memory_operand" ""))
559         (unspec [(match_operand:HI 2 "register_operand" "")
560                  (match_operand:HI 3 "const_int_operand" "")] UNSPEC_MOVMD)
561         (clobber (match_dup 4))
562         (clobber (match_dup 5))
563         (set (match_dup 2)
564              (const_int 0))])]
565   "TARGET_H8300SX"
566   {
567     operands[4] = copy_rtx (XEXP (operands[0], 0));
568     operands[5] = copy_rtx (XEXP (operands[1], 0));
569   })
570
571
572 ;; This is a difficult instruction to reload since operand 0 must be the
573 ;; frame pointer.  See h8300_reg_class_from_letter for an explanation.
574 (define_insn "movmd_internal_normal"
575   [(set (mem:BLK (match_operand:HI 3 "register_operand" "0,r"))
576         (mem:BLK (match_operand:HI 4 "register_operand" "1,1")))
577    (unspec [(match_operand:HI 5 "register_operand" "2,2")
578             (match_operand:HI 6 "const_int_operand" "n,n")] UNSPEC_MOVMD)
579    (clobber (match_operand:HI 0 "register_operand" "=d,??D"))
580    (clobber (match_operand:HI 1 "register_operand" "=f,f"))
581    (set (match_operand:HI 2 "register_operand" "=c,c")
582         (const_int 0))]
583   "TARGET_H8300SX && TARGET_NORMAL_MODE"
584   "@
585     movmd%m6
586     #"
587   [(set_attr "length" "2,14")
588    (set_attr "can_delay" "no")
589    (set_attr "cc" "none,clobber")])
590
591 (define_insn "movmd_internal"
592   [(set (mem:BLK (match_operand:SI 3 "register_operand" "0,r"))
593         (mem:BLK (match_operand:SI 4 "register_operand" "1,1")))
594    (unspec [(match_operand:HI 5 "register_operand" "2,2")
595             (match_operand:HI 6 "const_int_operand" "n,n")] UNSPEC_MOVMD)
596    (clobber (match_operand:SI 0 "register_operand" "=d,??D"))
597    (clobber (match_operand:SI 1 "register_operand" "=f,f"))
598    (set (match_operand:HI 2 "register_operand" "=c,c")
599         (const_int 0))]
600   "TARGET_H8300SX && !TARGET_NORMAL_MODE"
601   "@
602     movmd%m6
603     #"
604   [(set_attr "length" "2,14")
605    (set_attr "can_delay" "no")
606    (set_attr "cc" "none,clobber")])
607
608 ;; Split the above instruction if the destination register isn't er6.
609 ;; We need a sequence like:
610 ;;
611 ;;      mov.l   er6,@-er7
612 ;;      mov.l   <dest>,er6
613 ;;      movmd.sz
614 ;;      mov.l   er6,<dest>
615 ;;      mov.l   @er7+,er6
616 ;;
617 ;; where <dest> is the current destination register (operand 4).
618 ;; The fourth instruction will be deleted if <dest> dies here.
619 (define_split
620   [(set (match_operand:BLK 0 "memory_operand" "")
621         (match_operand:BLK 1 "memory_operand" ""))
622    (unspec [(match_operand:HI 2 "register_operand" "")
623             (match_operand:HI 3 "const_int_operand" "")] UNSPEC_MOVMD)
624    (clobber (match_operand:HI 4 "register_operand" ""))
625    (clobber (match_operand:HI 5 "register_operand" ""))
626    (set (match_dup 2)
627         (const_int 0))]
628   "TARGET_H8300SX && TARGET_NORMAL_MODE
629    && reload_completed
630    && REGNO (operands[4]) != DESTINATION_REG"
631   [(const_int 0)]
632   {
633     rtx dest;
634
635     h8300_swap_into_er6 (XEXP (operands[0], 0));
636     dest = replace_equiv_address (operands[0], hard_frame_pointer_rtx);
637     emit_insn (gen_movmd (dest, operands[1], operands[2], operands[3]));
638     h8300_swap_out_of_er6 (operands[4]);
639     DONE;
640   })
641
642 (define_split
643   [(set (match_operand:BLK 0 "memory_operand" "")
644         (match_operand:BLK 1 "memory_operand" ""))
645    (unspec [(match_operand:HI 2 "register_operand" "")
646             (match_operand:HI 3 "const_int_operand" "")] UNSPEC_MOVMD)
647    (clobber (match_operand:SI 4 "register_operand" ""))
648    (clobber (match_operand:SI 5 "register_operand" ""))
649    (set (match_dup 2)
650         (const_int 0))]
651   "TARGET_H8300SX && !TARGET_NORMAL_MODE
652    && reload_completed
653    && REGNO (operands[4]) != DESTINATION_REG"
654   [(const_int 0)]
655   {
656     rtx dest;
657
658     h8300_swap_into_er6 (XEXP (operands[0], 0));
659     dest = replace_equiv_address (operands[0], hard_frame_pointer_rtx);
660     emit_insn (gen_movmd (dest, operands[1], operands[2], operands[3]));
661     h8300_swap_out_of_er6 (operands[4]);
662     DONE;
663   })
664
665 ;; Expand a call to stpcpy() using movsd.  Operand 0 should point to
666 ;; the final character, but movsd leaves it pointing to the character
667 ;; after that.
668 (define_expand "movstr"
669   [(use (match_operand 0 "register_operand" ""))
670    (use (match_operand:BLK 1 "memory_operand" ""))
671    (use (match_operand:BLK 2 "memory_operand" ""))]
672   "TARGET_H8300SX"
673   {
674     operands[1] = replace_equiv_address
675       (operands[1], copy_to_mode_reg (Pmode, XEXP (operands[1], 0)));
676     operands[2] = replace_equiv_address
677       (operands[2], copy_to_mode_reg (Pmode, XEXP (operands[2], 0)));
678     emit_insn (gen_movsd (operands[1], operands[2], gen_reg_rtx (Pmode)));
679     emit_insn (gen_add3_insn (operands[0],
680                               XEXP (operands[1], 0),
681                               constm1_rtx));
682     DONE;
683   })
684
685 ;; Expander for generating a movsd instruction.  Operand 0 is the
686 ;; destination string, operand 1 is the source string and operand 2
687 ;; is a scratch register.
688 (define_expand "movsd"
689   [(parallel
690      [(set (match_operand:BLK 0 "memory_operand" "")
691            (unspec:BLK [(match_operand:BLK 1 "memory_operand" "")]
692                        UNSPEC_STPCPY))
693       (clobber (match_dup 3))
694       (clobber (match_dup 4))
695       (clobber (match_operand 2 "register_operand" ""))])]
696   "TARGET_H8300SX"
697   {
698     operands[3] = copy_rtx (XEXP (operands[0], 0));
699     operands[4] = copy_rtx (XEXP (operands[1], 0));
700   })
701
702 ;; See comments above memcpy_internal().
703 (define_insn "stpcpy_internal_normal"
704   [(set (mem:BLK (match_operand:HI 3 "register_operand" "0,r"))
705         (unspec:BLK [(mem:BLK (match_operand:HI 4 "register_operand" "1,1"))]
706                 UNSPEC_STPCPY))
707    (clobber (match_operand:HI 0 "register_operand" "=d,??D"))
708    (clobber (match_operand:HI 1 "register_operand" "=f,f"))
709    (clobber (match_operand:HI 2 "register_operand" "=c,c"))]
710   "TARGET_H8300SX && TARGET_NORMAL_MODE"
711   "@
712     \n1:\tmovsd\t2f\;bra\t1b\n2:
713     #"
714   [(set_attr "length" "6,18")
715    (set_attr "cc" "none,clobber")])
716
717 (define_insn "stpcpy_internal"
718   [(set (mem:BLK (match_operand:SI 3 "register_operand" "0,r"))
719         (unspec:BLK [(mem:BLK (match_operand:SI 4 "register_operand" "1,1"))]
720                 UNSPEC_STPCPY))
721    (clobber (match_operand:SI 0 "register_operand" "=d,??D"))
722    (clobber (match_operand:SI 1 "register_operand" "=f,f"))
723    (clobber (match_operand:SI 2 "register_operand" "=c,c"))]
724   "TARGET_H8300SX && !TARGET_NORMAL_MODE"
725   "@
726     \n1:\tmovsd\t2f\;bra\t1b\n2:
727     #"
728   [(set_attr "length" "6,18")
729    (set_attr "cc" "none,clobber")])
730
731 ;; Split the above instruction if the destination isn't er6.  This works
732 ;; in the same way as the movmd splitter.
733 (define_split
734   [(set (match_operand:BLK 0 "memory_operand" "")
735         (unspec:BLK [(match_operand:BLK 1 "memory_operand" "")] UNSPEC_STPCPY))
736    (clobber (match_operand:HI 2 "register_operand" ""))
737    (clobber (match_operand:HI 3 "register_operand" ""))
738    (clobber (match_operand:HI 4 "register_operand" ""))]
739   "TARGET_H8300SX && TARGET_NORMAL_MODE
740    && reload_completed
741    && REGNO (operands[2]) != DESTINATION_REG"
742   [(const_int 0)]
743   {
744     rtx dest;
745
746     h8300_swap_into_er6 (XEXP (operands[0], 0));
747     dest = replace_equiv_address (operands[0], hard_frame_pointer_rtx);
748     emit_insn (gen_movsd (dest, operands[1], operands[4]));
749     h8300_swap_out_of_er6 (operands[2]);
750     DONE;
751   })
752
753 (define_split
754   [(set (match_operand:BLK 0 "memory_operand" "")
755         (unspec:BLK [(match_operand:BLK 1 "memory_operand" "")] UNSPEC_STPCPY))
756    (clobber (match_operand:SI 2 "register_operand" ""))
757    (clobber (match_operand:SI 3 "register_operand" ""))
758    (clobber (match_operand:SI 4 "register_operand" ""))]
759   "TARGET_H8300SX && !TARGET_NORMAL_MODE
760    && reload_completed
761    && REGNO (operands[2]) != DESTINATION_REG"
762   [(const_int 0)]
763   {
764     rtx dest;
765
766     h8300_swap_into_er6 (XEXP (operands[0], 0));
767     dest = replace_equiv_address (operands[0], hard_frame_pointer_rtx);
768     emit_insn (gen_movsd (dest, operands[1], operands[4]));
769     h8300_swap_out_of_er6 (operands[2]);
770     DONE;
771   })
772
773 (include "mova.md")
774
775 (define_expand "movsf"
776   [(set (match_operand:SF 0 "general_operand_dst" "")
777         (match_operand:SF 1 "general_operand_src" ""))]
778   ""
779   "
780 {
781   if (TARGET_H8300)
782     {
783       if (h8300_expand_movsi (operands))
784         DONE;
785     }
786   else if (!TARGET_H8300SX)
787     {
788       /* One of the ops has to be in a register.  */
789       if (!register_operand (operand1, SFmode)
790           && !register_operand (operand0, SFmode))
791         {
792           operands[1] = copy_to_mode_reg (SFmode, operand1);
793         }
794     }
795 }")
796
797 (define_insn "*movsf_h8300"
798   [(set (match_operand:SF 0 "general_operand_dst" "=r,r,r,o,<,r")
799         (match_operand:SF 1 "general_operand_src" "G,r,io,r,r,>"))]
800   "TARGET_H8300
801    && (register_operand (operands[0], SFmode)
802        || register_operand (operands[1], SFmode))"
803   "*
804 {
805   /* Copy of the movsi stuff.  */
806   unsigned int rn = -1;
807   switch (which_alternative)
808     {
809     case 0:
810       return \"sub.w    %e0,%e0\;sub.w  %f0,%f0\";
811     case 1:
812       if (REGNO (operands[0]) < REGNO (operands[1]))
813         return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
814       else
815         return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
816     case 2:
817       /* Make sure we don't trample the register we index with.  */
818       if (GET_CODE (operands[1]) == MEM)
819         {
820           rtx inside = XEXP (operands[1], 0);
821           if (REG_P (inside))
822             {
823               rn = REGNO (inside);
824             }
825           else if (GET_CODE (inside) == PLUS)
826             {
827               rtx lhs = XEXP (inside, 0);
828               rtx rhs = XEXP (inside, 1);
829               if (REG_P (lhs)) rn = REGNO (lhs);
830               if (REG_P (rhs)) rn = REGNO (rhs);
831             }
832         }
833       if (rn == REGNO (operands[0]))
834         /* Move the second word first.  */
835         return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
836       else
837         /* Move the first word first.  */
838         return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
839
840     case 3:
841       return \"mov.w    %e1,%e0\;mov.w  %f1,%f0\";
842     case 4:
843       return \"mov.w    %f1,%T0\;mov.w  %e1,%T0\";
844     case 5:
845       return \"mov.w    %T1,%e0\;mov.w  %T1,%f0\";
846     default:
847       gcc_unreachable ();
848     }
849 }"
850   [(set (attr "length")
851         (symbol_ref "compute_mov_length (operands)"))])
852
853 (define_insn "*movsf_h8300hs"
854   [(set (match_operand:SF 0 "general_operand_dst" "=r,r,r,m,<,r")
855         (match_operand:SF 1 "general_operand_src" "G,r,im,r,r,>"))]
856   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX
857    && (register_operand (operands[0], SFmode)
858        || register_operand (operands[1], SFmode))"
859   "@
860    sub.l        %S0,%S0
861    mov.l        %S1,%S0
862    mov.l        %S1,%S0
863    mov.l        %S1,%S0
864    mov.l        %S1,%S0
865    mov.l        %S1,%S0"
866   [(set (attr "length")
867         (symbol_ref "compute_mov_length (operands)"))
868    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
869 \f
870 ;; ----------------------------------------------------------------------
871 ;; PUSH INSTRUCTIONS
872 ;; ----------------------------------------------------------------------
873
874 (define_insn "pushqi1_h8300"
875   [(set (reg:HI SP_REG)
876         (plus:HI (reg:HI SP_REG) (const_int -2)))
877    (set (mem:QI (plus:HI (reg:HI SP_REG) (const_int -1)))
878         (match_operand:QI 0 "register_operand" "r"))]
879   "TARGET_H8300
880    && operands[0] != stack_pointer_rtx"
881   "mov.w\\t%T0,@-r7"
882   [(set_attr "length" "2")])
883
884 (define_insn "pushqi1_h8300hs_advanced"
885   [(set (reg:SI SP_REG)
886         (plus:SI (reg:SI SP_REG) (const_int -4)))
887    (set (mem:QI (plus:SI (reg:SI SP_REG) (const_int -3)))
888         (match_operand:QI 0 "register_operand" "r"))]
889   "(TARGET_H8300H || TARGET_H8300S)
890    && operands[0] != stack_pointer_rtx"
891   "mov.l\\t%S0,@-er7"
892   [(set_attr "length" "4")])
893
894 (define_insn "pushqi1_h8300hs_normal"
895   [(set (reg:HI SP_REG)
896         (plus:HI (reg:HI SP_REG) (const_int -4)))
897    (set (mem:QI (plus:HI (reg:HI SP_REG) (const_int -3)))
898         (match_operand:QI 0 "register_operand" "r"))]
899   "(TARGET_H8300H || TARGET_H8300S)
900    && operands[0] != stack_pointer_rtx"
901   "mov.l\\t%S0,@-er7"
902   [(set_attr "length" "4")])
903
904 (define_expand "pushqi1"
905   [(match_operand:QI 0 "register_operand" "")]
906   ""
907   "
908 {
909   if (TARGET_H8300)
910     emit_insn (gen_pushqi1_h8300 (operands[0]));
911   else if (!TARGET_NORMAL_MODE)
912     emit_insn (gen_pushqi1_h8300hs_advanced (operands[0]));
913   else
914     emit_insn (gen_pushqi1_h8300hs_normal (operands[0]));
915   DONE;
916 }")
917
918 (define_expand "pushhi1_h8300"
919   [(set (mem:HI (pre_dec:HI (reg:HI SP_REG)))
920         (match_operand:HI 0 "register_operand" ""))]
921   "TARGET_H8300
922    && operands[0] != stack_pointer_rtx"
923   "")
924
925 (define_insn "pushhi1_h8300hs_advanced"
926   [(set (reg:SI SP_REG)
927         (plus:SI (reg:SI SP_REG) (const_int -4)))
928    (set (mem:HI (plus:SI (reg:SI SP_REG) (const_int -2)))
929         (match_operand:HI 0 "register_operand" "r"))]
930   "(TARGET_H8300H || TARGET_H8300S)
931    && operands[0] != stack_pointer_rtx"
932   "mov.l\\t%S0,@-er7"
933   [(set_attr "length" "4")])
934
935 (define_insn "pushhi1_h8300hs_normal"
936   [(set (reg:HI SP_REG)
937         (plus:HI (reg:HI SP_REG) (const_int -4)))
938    (set (mem:HI (plus:HI (reg:HI SP_REG) (const_int -2)))
939         (match_operand:HI 0 "register_operand" "r"))]
940   "(TARGET_H8300H || TARGET_H8300S)
941    && operands[0] != stack_pointer_rtx"
942   "mov.l\\t%S0,@-er7"
943   [(set_attr "length" "4")])
944
945 (define_expand "pushhi1"
946   [(match_operand:HI 0 "register_operand" "")]
947   ""
948   "
949 {
950   if (TARGET_H8300)
951     emit_insn (gen_pushhi1_h8300 (operands[0]));
952   else if (!TARGET_NORMAL_MODE)
953     emit_insn (gen_pushhi1_h8300hs_advanced (operands[0]));
954   else
955     emit_insn (gen_pushhi1_h8300hs_normal (operands[0]));
956   DONE;
957 }")
958 \f
959 ;; ----------------------------------------------------------------------
960 ;; TEST INSTRUCTIONS
961 ;; ----------------------------------------------------------------------
962
963 (define_insn ""
964   [(set (cc0) (compare
965                (zero_extract:QI (match_operand:QI 0 "bit_memory_operand" "r,U")
966                                 (const_int 1)
967                                 (match_operand 1 "const_int_operand" "n,n"))
968                (const_int 0)))]
969   "TARGET_H8300"
970   "btst %Z1,%Y0"
971   [(set_attr "length" "2,4")
972    (set_attr "cc" "set_zn,set_zn")])
973
974 (define_insn ""
975   [(set (cc0) (compare
976                (zero_extract:HI (match_operand:HI 0 "register_operand" "r")
977                                 (const_int 1)
978                                 (match_operand 1 "const_int_operand" "n"))
979                (const_int 0)))]
980   "TARGET_H8300"
981   "btst %Z1,%Y0"
982   [(set_attr "length" "2")
983    (set_attr "cc" "set_zn")])
984
985 (define_insn_and_split "*tst_extzv_1_n"
986   [(set (cc0) (compare
987                (zero_extract:SI (match_operand:QI 0 "general_operand_src" "r,U,mn>")
988                                 (const_int 1)
989                                 (match_operand 1 "const_int_operand" "n,n,n"))
990                (const_int 0)))
991    (clobber (match_scratch:QI 2 "=X,X,&r"))]
992   "(TARGET_H8300H || TARGET_H8300S)"
993   "@
994    btst\\t%Z1,%Y0
995    btst\\t%Z1,%Y0
996    #"
997   "&& reload_completed
998    && !OK_FOR_U (operands[0])"
999   [(set (match_dup 2)
1000         (match_dup 0))
1001    (parallel [(set (cc0) (compare (zero_extract:SI (match_dup 2)
1002                                                    (const_int 1)
1003                                                    (match_dup 1))
1004                                   (const_int 0)))
1005               (clobber (scratch:QI))])]
1006   ""
1007   [(set_attr "length" "2,8,10")
1008    (set_attr "cc" "set_zn,set_zn,set_zn")])
1009
1010 (define_insn ""
1011   [(set (cc0) (compare (zero_extract:SI (match_operand:SI 0 "register_operand" "r")
1012                                         (const_int 1)
1013                                         (match_operand 1 "const_int_operand" "n"))
1014                        (const_int 0)))]
1015   "(TARGET_H8300H || TARGET_H8300S)
1016    && INTVAL (operands[1]) <= 15"
1017   "btst %Z1,%Y0"
1018   [(set_attr "length" "2")
1019    (set_attr "cc" "set_zn")])
1020
1021 (define_insn_and_split "*tstsi_upper_bit"
1022   [(set (cc0) (compare (zero_extract:SI (match_operand:SI 0 "register_operand" "r")
1023                                         (const_int 1)
1024                                         (match_operand 1 "const_int_operand" "n"))
1025                        (const_int 0)))
1026    (clobber (match_scratch:SI 2 "=&r"))]
1027   "(TARGET_H8300H || TARGET_H8300S)
1028    && INTVAL (operands[1]) >= 16"
1029   "#"
1030   "&& reload_completed"
1031   [(set (match_dup 2)
1032         (ior:SI (and:SI (match_dup 2)
1033                         (const_int -65536))
1034                 (lshiftrt:SI (match_dup 0)
1035                              (const_int 16))))
1036    (set (cc0) (compare (zero_extract:SI (match_dup 2)
1037                                         (const_int 1)
1038                                         (match_dup 3))
1039                        (const_int 0)))]
1040   "operands[3] = GEN_INT (INTVAL (operands[1]) - 16);")
1041
1042 (define_insn "*tstsi_variable_bit"
1043   [(set (cc0) (compare (zero_extract:SI (match_operand:SI 0 "register_operand" "r")
1044                                         (const_int 1)
1045                                         (and:SI (match_operand:SI 1 "register_operand" "r")
1046                                                 (const_int 7)))
1047                        (const_int 0)))]
1048   "TARGET_H8300H || TARGET_H8300S"
1049   "btst %w1,%w0"
1050   [(set_attr "length" "2")
1051    (set_attr "cc" "set_zn")])
1052
1053 (define_insn_and_split "*tstsi_variable_bit_qi"
1054   [(set (cc0)
1055         (compare
1056          (zero_extract:SI (zero_extend:SI (match_operand:QI 0 "general_operand_src" "r,U,mn>"))
1057                           (const_int 1)
1058                           (and:SI (match_operand:SI 1 "register_operand" "r,r,r")
1059                                   (const_int 7)))
1060          (const_int 0)))
1061    (clobber (match_scratch:QI 2 "=X,X,&r"))]
1062   "(TARGET_H8300H || TARGET_H8300S)"
1063   "@
1064    btst\\t%w1,%X0
1065    btst\\t%w1,%X0
1066    #"
1067   "&& reload_completed
1068    && !OK_FOR_U (operands[0])"
1069   [(set (match_dup 2)
1070         (match_dup 0))
1071    (parallel [(set (cc0) (compare (zero_extract:SI (zero_extend:SI (match_dup 2))
1072                                                    (const_int 1)
1073                                                    (and:SI (match_dup 1)
1074                                                            (const_int 7)))
1075                                   (const_int 0)))
1076               (clobber (scratch:QI))])]
1077   ""
1078   [(set_attr "length" "2,8,10")
1079    (set_attr "cc" "set_zn,set_zn,set_zn")])
1080
1081 (define_insn "*tstqi"
1082   [(set (cc0) (compare (match_operand:QI 0 "register_operand" "r")
1083                        (const_int 0)))]
1084   ""
1085   "mov.b        %X0,%X0"
1086   [(set_attr "length" "2")
1087    (set_attr "cc" "set_znv")])
1088
1089 (define_insn "*tsthi"
1090   [(set (cc0) (compare (match_operand:HI 0 "register_operand" "r")
1091                        (const_int 0)))]
1092   ""
1093   "mov.w        %T0,%T0"
1094   [(set_attr "length" "2")
1095    (set_attr "cc" "set_znv")])
1096
1097 (define_insn "*tsthi_upper"
1098   [(set (cc0) (compare (and:HI (match_operand:HI 0 "register_operand" "r")
1099                                (const_int -256))
1100                        (const_int 0)))]
1101   ""
1102   "mov.b        %t0,%t0"
1103   [(set_attr "length" "2")
1104    (set_attr "cc" "set_znv")])
1105
1106 (define_insn "*tstsi"
1107   [(set (cc0) (compare (match_operand:SI 0 "register_operand" "r")
1108                        (const_int 0)))]
1109   "TARGET_H8300H || TARGET_H8300S"
1110   "mov.l        %S0,%S0"
1111   [(set_attr "length" "2")
1112    (set_attr "cc" "set_znv")])
1113
1114 (define_insn "*tstsi_upper"
1115   [(set (cc0) (compare (and:SI (match_operand:SI 0 "register_operand" "r")
1116                                (const_int -65536))
1117                        (const_int 0)))]
1118   ""
1119   "mov.w        %e0,%e0"
1120   [(set_attr "length" "2")
1121    (set_attr "cc" "set_znv")])
1122
1123 (define_insn "*cmpqi"
1124   [(set (cc0)
1125         (compare (match_operand:QI 0 "h8300_dst_operand" "rQ")
1126                  (match_operand:QI 1 "h8300_src_operand" "rQi")))]
1127   ""
1128   "cmp.b        %X1,%X0"
1129   [(set_attr "length_table" "addb")
1130    (set_attr "cc" "compare")])
1131
1132 (define_insn "*cmphi_h8300_znvc"
1133   [(set (cc0)
1134         (compare (match_operand:HI 0 "register_operand" "r")
1135                  (match_operand:HI 1 "register_operand" "r")))]
1136   "TARGET_H8300"
1137   "cmp.w        %T1,%T0"
1138   [(set_attr "length" "2")
1139    (set_attr "cc" "compare")])
1140
1141 (define_insn "*cmphi_h8300hs_znvc"
1142   [(set (cc0)
1143         (compare (match_operand:HI 0 "h8300_dst_operand" "rU,rQ")
1144                  (match_operand:HI 1 "h8300_src_operand" "P3>X,rQi")))]
1145   "TARGET_H8300H || TARGET_H8300S"
1146   "*
1147 {
1148   switch (which_alternative)
1149     {
1150     case 0:
1151       if (!TARGET_H8300SX)
1152         return \"cmp.w  %T1,%T0\";
1153       else
1154         return \"cmp.w  %T1:3,%T0\";
1155     case 1:
1156       return \"cmp.w    %T1,%T0\";
1157     default:
1158       gcc_unreachable ();
1159       }
1160 }"
1161   [(set_attr "length_table" "short_immediate,addw")
1162    (set_attr "cc" "compare,compare")])
1163
1164 (define_insn "cmpsi"
1165   [(set (cc0)
1166         (compare (match_operand:SI 0 "h8300_dst_operand" "r,rQ")
1167                  (match_operand:SI 1 "h8300_src_operand" "P3>X,rQi")))]
1168   "TARGET_H8300H || TARGET_H8300S"
1169   "*
1170 {
1171   switch (which_alternative)
1172     {
1173     case 0:
1174       if (!TARGET_H8300SX)
1175         return \"cmp.l  %S1,%S0\";
1176       else
1177         return \"cmp.l  %S1:3,%S0\";
1178     case 1:
1179       return \"cmp.l    %S1,%S0\";
1180     default:
1181       gcc_unreachable ();
1182     }
1183 }"
1184   [(set_attr "length" "2,*")
1185    (set_attr "length_table" "*,addl")
1186    (set_attr "cc" "compare,compare")])
1187 \f
1188 ;; ----------------------------------------------------------------------
1189 ;; ADD INSTRUCTIONS
1190 ;; ----------------------------------------------------------------------
1191
1192 (define_expand "addqi3"
1193   [(set (match_operand:QI 0 "register_operand" "")
1194         (plus:QI (match_operand:QI 1 "register_operand" "")
1195                  (match_operand:QI 2 "h8300_src_operand" "")))]
1196   ""
1197   "")
1198
1199 (define_insn "*addqi3"
1200   [(set (match_operand:QI 0 "h8300_dst_operand" "=rQ")
1201         (plus:QI (match_operand:QI 1 "h8300_dst_operand" "%0")
1202                  (match_operand:QI 2 "h8300_src_operand" "rQi")))]
1203   "h8300_operands_match_p (operands)"
1204   "add.b        %X2,%X0"
1205   [(set_attr "length_table" "addb")
1206    (set_attr "cc" "set_zn")])
1207
1208 (define_expand "addhi3"
1209   [(set (match_operand:HI 0 "register_operand" "")
1210         (plus:HI (match_operand:HI 1 "register_operand" "")
1211                  (match_operand:HI 2 "h8300_src_operand" "")))]
1212   ""
1213   "")
1214
1215 (define_insn "*addhi3_h8300"
1216   [(set (match_operand:HI 0 "register_operand" "=r,r,r,r,r")
1217         (plus:HI (match_operand:HI 1 "register_operand" "%0,0,0,0,0")
1218                  (match_operand:HI 2 "h8300_src_operand" "L,N,J,n,r")))]
1219   "TARGET_H8300"
1220   "@
1221    adds %2,%T0
1222    subs %G2,%T0
1223    add.b        %t2,%t0
1224    add.b        %s2,%s0\;addx   %t2,%t0
1225    add.w        %T2,%T0"
1226   [(set_attr "length" "2,2,2,4,2")
1227    (set_attr "cc" "none_0hit,none_0hit,clobber,clobber,set_zn")])
1228
1229 ;; This splitter is very important to make the stack adjustment
1230 ;; interrupt-safe.  The combination of add.b and addx is unsafe!
1231 ;;
1232 ;; We apply this split after the peephole2 pass so that we won't end
1233 ;; up creating too many adds/subs when a scratch register is
1234 ;; available, which is actually a common case because stack unrolling
1235 ;; tends to happen immediately after a function call.
1236
1237 (define_split
1238   [(set (match_operand:HI 0 "stack_pointer_operand" "")
1239         (plus:HI (match_dup 0)
1240                  (match_operand 1 "const_int_gt_2_operand" "")))]
1241   "TARGET_H8300 && epilogue_completed"
1242   [(const_int 0)]
1243   "split_adds_subs (HImode, operands); DONE;")
1244
1245 (define_peephole2
1246   [(match_scratch:HI 2 "r")
1247    (set (match_operand:HI 0 "stack_pointer_operand" "")
1248         (plus:HI (match_dup 0)
1249                  (match_operand:HI 1 "const_int_ge_8_operand" "")))]
1250   "TARGET_H8300"
1251   [(set (match_dup 2)
1252         (match_dup 1))
1253    (set (match_dup 0)
1254         (plus:HI (match_dup 0)
1255                  (match_dup 2)))]
1256   "")
1257
1258 (define_insn "*addhi3_h8300hs"
1259   [(set (match_operand:HI 0 "register_operand" "=r,r,r,r,r")
1260         (plus:HI (match_operand:HI 1 "register_operand" "%0,0,0,0,0")
1261                  (match_operand:HI 2 "h8300_src_operand" "L,N,J,n,r")))]
1262   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX"
1263   "@
1264    adds %2,%S0
1265    subs %G2,%S0
1266    add.b        %t2,%t0
1267    add.w        %T2,%T0
1268    add.w        %T2,%T0"
1269   [(set_attr "length" "2,2,2,4,2")
1270    (set_attr "cc" "none_0hit,none_0hit,clobber,set_zn,set_zn")])
1271
1272 (define_insn "*addhi3_incdec"
1273   [(set (match_operand:HI 0 "register_operand" "=r,r")
1274         (unspec:HI [(match_operand:HI 1 "register_operand" "0,0")
1275                     (match_operand:HI 2 "incdec_operand" "M,O")]
1276                    UNSPEC_INCDEC))]
1277   "TARGET_H8300H || TARGET_H8300S"
1278   "@
1279    inc.w        %2,%T0
1280    dec.w        %G2,%T0"
1281   [(set_attr "length" "2,2")
1282    (set_attr "cc" "set_zn,set_zn")])
1283
1284 (define_insn "*addhi3_h8sx"
1285   [(set (match_operand:HI 0 "h8300_dst_operand" "=rU,rU,r,rQ")
1286         (plus:HI (match_operand:HI 1 "h8300_dst_operand" "%0,0,0,0")
1287                  (match_operand:HI 2 "h8300_src_operand" "P3>X,P3<X,J,rQi")))]
1288   "TARGET_H8300SX && h8300_operands_match_p (operands)"
1289   "@
1290    add.w        %T2:3,%T0
1291    sub.w        %G2:3,%T0
1292    add.b        %t2,%t0
1293    add.w        %T2,%T0"
1294   [(set_attr "length_table" "short_immediate,short_immediate,*,addw")
1295    (set_attr "length" "*,*,2,*")
1296    (set_attr "cc" "set_zn")])
1297
1298 (define_split
1299   [(set (match_operand:HI 0 "register_operand" "")
1300         (plus:HI (match_dup 0)
1301                  (match_operand:HI 1 "two_insn_adds_subs_operand" "")))]
1302   ""
1303   [(const_int 0)]
1304   "split_adds_subs (HImode, operands); DONE;")
1305
1306 (define_expand "addsi3"
1307   [(set (match_operand:SI 0 "register_operand" "")
1308         (plus:SI (match_operand:SI 1 "register_operand" "")
1309                  (match_operand:SI 2 "h8300_src_operand" "")))]
1310   ""
1311   "")
1312
1313 (define_insn "*addsi_h8300"
1314   [(set (match_operand:SI 0 "register_operand" "=r,r")
1315         (plus:SI (match_operand:SI 1 "register_operand" "%0,0")
1316                  (match_operand:SI 2 "h8300_src_operand" "n,r")))]
1317   "TARGET_H8300"
1318   "* return output_plussi (operands);"
1319   [(set (attr "length")
1320         (symbol_ref "compute_plussi_length (operands)"))
1321    (set (attr "cc")
1322         (symbol_ref "compute_plussi_cc (operands)"))])
1323
1324 (define_insn "*addsi_h8300hs"
1325   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ,rQ")
1326         (plus:SI (match_operand:SI 1 "h8300_dst_operand" "%0,0")
1327                  (match_operand:SI 2 "h8300_src_operand" "i,rQ")))]
1328   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
1329   "* return output_plussi (operands);"
1330   [(set (attr "length")
1331         (symbol_ref "compute_plussi_length (operands)"))
1332    (set (attr "cc")
1333         (symbol_ref "compute_plussi_cc (operands)"))])
1334
1335 (define_insn "*addsi3_incdec"
1336   [(set (match_operand:SI 0 "register_operand" "=r,r")
1337         (unspec:SI [(match_operand:SI 1 "register_operand" "0,0")
1338                     (match_operand:SI 2 "incdec_operand" "M,O")]
1339                    UNSPEC_INCDEC))]
1340   "TARGET_H8300H || TARGET_H8300S"
1341   "@
1342    inc.l        %2,%S0
1343    dec.l        %G2,%S0"
1344   [(set_attr "length" "2,2")
1345    (set_attr "cc" "set_zn,set_zn")])
1346
1347 (define_split
1348   [(set (match_operand:SI 0 "register_operand" "")
1349         (plus:SI (match_dup 0)
1350                  (match_operand:SI 1 "two_insn_adds_subs_operand" "")))]
1351   "TARGET_H8300H || TARGET_H8300S"
1352   [(const_int 0)]
1353   "split_adds_subs (SImode, operands); DONE;")
1354
1355 ;; ----------------------------------------------------------------------
1356 ;; SUBTRACT INSTRUCTIONS
1357 ;; ----------------------------------------------------------------------
1358
1359 (define_expand "subqi3"
1360   [(set (match_operand:QI 0 "register_operand" "")
1361         (minus:QI (match_operand:QI 1 "register_operand" "")
1362                   (match_operand:QI 2 "h8300_src_operand" "")))]
1363   ""
1364   "")
1365
1366 (define_insn "*subqi3"
1367   [(set (match_operand:QI 0 "h8300_dst_operand" "=rQ")
1368         (minus:QI (match_operand:QI 1 "h8300_dst_operand" "0")
1369                   (match_operand:QI 2 "h8300_dst_operand" "rQ")))]
1370   "h8300_operands_match_p (operands)"
1371   "sub.b        %X2,%X0"
1372   [(set_attr "length_table" "addb")
1373    (set_attr "cc" "set_zn")])
1374
1375 (define_expand "subhi3"
1376   [(set (match_operand:HI 0 "register_operand" "")
1377         (minus:HI (match_operand:HI 1 "register_operand" "")
1378                   (match_operand:HI 2 "h8300_src_operand" "")))]
1379   ""
1380   "")
1381
1382 (define_insn "*subhi3_h8300"
1383   [(set (match_operand:HI 0 "register_operand" "=r,r")
1384         (minus:HI (match_operand:HI 1 "register_operand" "0,0")
1385                   (match_operand:HI 2 "h8300_src_operand" "r,n")))]
1386   "TARGET_H8300"
1387   "@
1388    sub.w        %T2,%T0
1389    add.b        %E2,%s0\;addx   %F2,%t0"
1390   [(set_attr "length" "2,4")
1391    (set_attr "cc" "set_zn,clobber")])
1392
1393 (define_insn "*subhi3_h8300hs"
1394   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ,rQ")
1395         (minus:HI (match_operand:HI 1 "h8300_dst_operand" "0,0")
1396                   (match_operand:HI 2 "h8300_src_operand" "rQ,i")))]
1397   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
1398   "@
1399    sub.w        %T2,%T0
1400    sub.w        %T2,%T0"
1401   [(set_attr "length_table" "addw")
1402    (set_attr "cc" "set_zn")])
1403
1404 (define_expand "subsi3"
1405   [(set (match_operand:SI 0 "register_operand" "")
1406         (minus:SI (match_operand:SI 1 "register_operand" "")
1407                   (match_operand:SI 2 "h8300_src_operand" "")))]
1408   ""
1409 {
1410   if (TARGET_H8300)
1411     operands[2] = force_reg (SImode, operands[2]);
1412 })
1413
1414 (define_insn "*subsi3_h8300"
1415   [(set (match_operand:SI 0 "register_operand" "=r")
1416         (minus:SI (match_operand:SI 1 "register_operand" "0")
1417                   (match_operand:SI 2 "register_operand" "r")))]
1418   "TARGET_H8300"
1419   "sub.w        %f2,%f0\;subx   %y2,%y0\;subx   %z2,%z0"
1420   [(set_attr "length" "6")])
1421
1422 (define_insn "*subsi3_h8300hs"
1423   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ,rQ")
1424         (minus:SI (match_operand:SI 1 "h8300_dst_operand" "0,0")
1425                   (match_operand:SI 2 "h8300_src_operand" "rQ,i")))]
1426   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
1427   "@
1428    sub.l        %S2,%S0
1429    sub.l        %S2,%S0"
1430   [(set_attr "length_table" "addl")
1431    (set_attr "cc" "set_zn")])
1432 \f
1433 ;; ----------------------------------------------------------------------
1434 ;; MULTIPLY INSTRUCTIONS
1435 ;; ----------------------------------------------------------------------
1436
1437 ;; Note that the H8/300 can only handle umulqihi3.
1438
1439 (define_expand "mulqihi3"
1440   [(set (match_operand:HI 0 "register_operand" "")
1441         (mult:HI (sign_extend:HI (match_operand:QI 1 "register_operand" ""))
1442                  ;; intentionally-mismatched modes
1443                  (match_operand:QI 2 "reg_or_nibble_operand" "")))]
1444   "TARGET_H8300H || TARGET_H8300S"
1445   "
1446 {
1447   if (GET_MODE (operands[2]) != VOIDmode)
1448     operands[2] = gen_rtx_SIGN_EXTEND (HImode, operands[2]);
1449 }")
1450
1451 (define_insn "*mulqihi3_const"
1452   [(set (match_operand:HI 0 "register_operand" "=r")
1453         (mult:HI (sign_extend:HI (match_operand:QI 1 "register_operand" "%0"))
1454                  (match_operand:QI 2 "nibble_operand" "IP4>X")))]
1455   "TARGET_H8300SX"
1456   "mulxs.b      %X2,%T0"
1457   [(set_attr "length" "4")
1458    (set_attr "cc" "set_zn")])
1459
1460 (define_insn "*mulqihi3"
1461   [(set (match_operand:HI 0 "register_operand" "=r")
1462         (mult:HI (sign_extend:HI (match_operand:QI 1 "register_operand" "%0"))
1463                  (sign_extend:HI (match_operand:QI 2 "register_operand" "r"))))]
1464   "TARGET_H8300H || TARGET_H8300S"
1465   "mulxs.b      %X2,%T0"
1466   [(set_attr "length" "4")
1467    (set_attr "cc" "set_zn")])
1468
1469 (define_expand "mulhisi3"
1470   [(set (match_operand:SI 0 "register_operand" "")
1471         (mult:SI (sign_extend:SI (match_operand:HI 1 "register_operand" ""))
1472                  ;; intentionally-mismatched modes
1473                  (match_operand:HI 2 "reg_or_nibble_operand" "")))]
1474   "TARGET_H8300H || TARGET_H8300S"
1475   "
1476 {
1477   if (GET_MODE (operands[2]) != VOIDmode)
1478     operands[2] = gen_rtx_SIGN_EXTEND (SImode, operands[2]);
1479 }")
1480
1481 (define_insn "*mulhisi3_const"
1482   [(set (match_operand:SI 0 "register_operand" "=r")
1483         (mult:SI (sign_extend:SI (match_operand:HI 1 "register_operand" "%0"))
1484                  (match_operand:SI 2 "nibble_operand" "IP4>X")))]
1485   "TARGET_H8300SX"
1486   "mulxs.w      %T2,%S0"
1487   [(set_attr "length" "4")
1488    (set_attr "cc" "set_zn")])
1489
1490 (define_insn "*mulhisi3"
1491   [(set (match_operand:SI 0 "register_operand" "=r")
1492         (mult:SI (sign_extend:SI (match_operand:HI 1 "register_operand" "%0"))
1493                  (sign_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
1494   "TARGET_H8300H || TARGET_H8300S"
1495   "mulxs.w      %T2,%S0"
1496   [(set_attr "length" "4")
1497    (set_attr "cc" "set_zn")])
1498
1499 (define_expand "umulqihi3"
1500   [(set (match_operand:HI 0 "register_operand" "")
1501         (mult:HI (zero_extend:HI (match_operand:QI 1 "register_operand" ""))
1502                  ;; intentionally-mismatched modes
1503                  (match_operand:QI 2 "reg_or_nibble_operand" "")))]
1504   "TARGET_H8300H || TARGET_H8300S"
1505   "
1506 {
1507   if (GET_MODE (operands[2]) != VOIDmode)
1508     operands[2] = gen_rtx_ZERO_EXTEND (HImode, operands[2]);
1509 }")
1510
1511 (define_insn "*umulqihi3_const"
1512   [(set (match_operand:HI 0 "register_operand" "=r")
1513         (mult:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "%0"))
1514                  (match_operand:QI 2 "nibble_operand" "IP4>X")))]
1515   "TARGET_H8300SX"
1516   "mulxu.b      %X2,%T0"
1517   [(set_attr "length" "4")
1518    (set_attr "cc" "set_zn")])
1519
1520 (define_insn "*umulqihi3"
1521   [(set (match_operand:HI 0 "register_operand" "=r")
1522         (mult:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "%0"))
1523                  (zero_extend:HI (match_operand:QI 2 "register_operand" "r"))))]
1524   ""
1525   "mulxu.b      %X2,%T0"
1526   [(set_attr "length" "2")
1527    (set_attr "cc" "none_0hit")])
1528
1529 (define_expand "umulhisi3"
1530   [(set (match_operand:SI 0 "register_operand" "")
1531         (mult:SI (zero_extend:SI (match_operand:HI 1 "register_operand" ""))
1532                  ;; intentionally-mismatched modes
1533                  (match_operand:HI 2 "reg_or_nibble_operand" "")))]
1534   "TARGET_H8300H || TARGET_H8300S"
1535   "
1536 {
1537   if (GET_MODE (operands[2]) != VOIDmode)
1538     operands[2] = gen_rtx_ZERO_EXTEND (SImode, operands[2]);
1539 }")
1540
1541 (define_insn "*umulhisi3_const"
1542   [(set (match_operand:SI 0 "register_operand" "=r")
1543         (mult:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "%0"))
1544                  (match_operand:SI 2 "nibble_operand" "IP4>X")))]
1545   "TARGET_H8300SX"
1546   "mulxu.w      %T2,%S0"
1547   [(set_attr "length" "4")
1548    (set_attr "cc" "set_zn")])
1549
1550 (define_insn "*umulhisi3"
1551   [(set (match_operand:SI 0 "register_operand" "=r")
1552         (mult:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "%0"))
1553                  (zero_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
1554   "TARGET_H8300H || TARGET_H8300S"
1555   "mulxu.w      %T2,%S0"
1556   [(set_attr "length" "2")
1557    (set_attr "cc" "none_0hit")])
1558
1559 ;; We could have used mulu.[wl] here, but mulu.[lw] is only available
1560 ;; on a H8SX with a multiplier, whereas muls.w seems to be available
1561 ;; on all H8SX variants.
1562 (define_insn "mulhi3"
1563   [(set (match_operand:HI 0 "register_operand" "=r")
1564         (mult:HI (match_operand:HI 1 "register_operand" "%0")
1565                  (match_operand:HI 2 "reg_or_nibble_operand" "r IP4>X")))]
1566   "TARGET_H8300SX"
1567   "muls.w\\t%T2,%T0"
1568   [(set_attr "length" "2")
1569    (set_attr "cc" "set_zn")])
1570
1571 (define_insn "mulsi3"
1572   [(set (match_operand:SI 0 "register_operand" "=r")
1573         (mult:SI (match_operand:SI 1 "register_operand" "%0")
1574                  (match_operand:SI 2 "reg_or_nibble_operand" "r IP4>X")))]
1575   "TARGET_H8300SX"
1576   "muls.l\\t%S2,%S0"
1577   [(set_attr "length" "2")
1578    (set_attr "cc" "set_zn")])
1579
1580 (define_insn "smulsi3_highpart"
1581   [(set (match_operand:SI 0 "register_operand" "=r")
1582         (truncate:SI
1583          (lshiftrt:DI
1584           (mult:DI
1585            (sign_extend:DI (match_operand:SI 1 "register_operand" "%0"))
1586            (sign_extend:DI (match_operand:SI 2 "reg_or_nibble_operand" "r IP4>X")))
1587           (const_int 32))))]
1588   "TARGET_H8300SXMUL"
1589   "muls/u.l\\t%S2,%S0"
1590   [(set_attr "length" "2")
1591    (set_attr "cc" "set_zn")])
1592
1593 (define_insn "umulsi3_highpart"
1594   [(set (match_operand:SI 0 "register_operand" "=r")
1595         (truncate:SI
1596          (ashiftrt:DI
1597           (mult:DI
1598            (zero_extend:DI (match_operand:SI 1 "register_operand" "%0"))
1599            (zero_extend:DI (match_operand:SI 2 "reg_or_nibble_operand" "r IP4>X")))
1600           (const_int 32))))]
1601   "TARGET_H8300SX"
1602   "mulu/u.l\\t%S2,%S0"
1603   [(set_attr "length" "2")
1604    (set_attr "cc" "none_0hit")])
1605
1606 ;; This is a "bridge" instruction.  Combine can't cram enough insns
1607 ;; together to crate a MAC instruction directly, but it can create
1608 ;; this instruction, which then allows combine to create the real
1609 ;; MAC insn.
1610 ;;
1611 ;; Unfortunately, if combine doesn't create a MAC instruction, this
1612 ;; insn must generate reasonably correct code.  Egad.
1613 (define_insn ""
1614   [(set (match_operand:SI 0 "register_operand" "=a")
1615         (mult:SI
1616           (sign_extend:SI
1617             (mem:HI (post_inc:SI (match_operand:SI 1 "register_operand" "r"))))
1618           (sign_extend:SI
1619             (mem:HI (post_inc:SI (match_operand:SI 2 "register_operand" "r"))))))]
1620   "TARGET_MAC"
1621   "clrmac\;mac  @%2+,@%1+"
1622   [(set_attr "length" "6")
1623    (set_attr "cc" "none_0hit")])
1624
1625 (define_insn ""
1626   [(set (match_operand:SI 0 "register_operand" "=a")
1627         (plus:SI (mult:SI
1628           (sign_extend:SI (mem:HI
1629             (post_inc:SI (match_operand:SI 1 "register_operand" "r"))))
1630           (sign_extend:SI (mem:HI
1631             (post_inc:SI (match_operand:SI 2 "register_operand" "r")))))
1632               (match_operand:SI 3 "register_operand" "0")))]
1633   "TARGET_MAC"
1634   "mac  @%2+,@%1+"
1635   [(set_attr "length" "4")
1636    (set_attr "cc" "none_0hit")])
1637
1638 ;; ----------------------------------------------------------------------
1639 ;; DIVIDE/MOD INSTRUCTIONS
1640 ;; ----------------------------------------------------------------------
1641
1642 (define_insn "udivhi3"
1643   [(set (match_operand:HI 0 "register_operand" "=r")
1644         (udiv:HI
1645          (match_operand:HI 1 "register_operand" "0")
1646          (match_operand:HI 2 "reg_or_nibble_operand" "r IP4>X")))]
1647   "TARGET_H8300SX"
1648   "divu.w\\t%T2,%T0"
1649   [(set_attr "length" "2")])
1650   
1651 (define_insn "divhi3"
1652   [(set (match_operand:HI 0 "register_operand" "=r")
1653         (div:HI
1654          (match_operand:HI 1 "register_operand" "0")
1655          (match_operand:HI 2 "reg_or_nibble_operand" "r IP4>X")))]
1656   "TARGET_H8300SX"
1657   "divs.w\\t%T2,%T0"
1658   [(set_attr "length" "2")])
1659   
1660 (define_insn "udivsi3"
1661   [(set (match_operand:SI 0 "register_operand" "=r")
1662         (udiv:SI
1663          (match_operand:SI 1 "register_operand" "0")
1664          (match_operand:SI 2 "reg_or_nibble_operand" "r IP4>X")))]
1665   "TARGET_H8300SX"
1666   "divu.l\\t%S2,%S0"
1667   [(set_attr "length" "2")])
1668   
1669 (define_insn "divsi3"
1670   [(set (match_operand:SI 0 "register_operand" "=r")
1671         (div:SI
1672          (match_operand:SI 1 "register_operand" "0")
1673          (match_operand:SI 2 "reg_or_nibble_operand" "r IP4>X")))]
1674   "TARGET_H8300SX"
1675   "divs.l\\t%S2,%S0"
1676   [(set_attr "length" "2")])
1677   
1678 (define_insn "udivmodqi4"
1679   [(set (match_operand:QI 0 "register_operand" "=r")
1680         (truncate:QI
1681           (udiv:HI
1682             (match_operand:HI 1 "register_operand" "0")
1683             (zero_extend:HI (match_operand:QI 2 "register_operand" "r")))))
1684    (set (match_operand:QI 3 "register_operand" "=r")
1685         (truncate:QI
1686           (umod:HI
1687             (match_dup 1)
1688             (zero_extend:HI (match_dup 2)))))]
1689   ""
1690   "*
1691 {
1692   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1693     return \"divxu.b\\t%X2,%T0\";
1694   else
1695     return \"divxu.b\\t%X2,%T0\;mov.b\\t%t0,%s3\";
1696 }"
1697   [(set_attr "length" "4")])
1698
1699 (define_insn "divmodqi4"
1700   [(set (match_operand:QI 0 "register_operand" "=r")
1701         (truncate:QI
1702           (div:HI
1703             (match_operand:HI 1 "register_operand" "0")
1704             (sign_extend:HI (match_operand:QI 2 "register_operand" "r")))))
1705    (set (match_operand:QI 3 "register_operand" "=r")
1706         (truncate:QI
1707           (mod:HI
1708             (match_dup 1)
1709             (sign_extend:HI (match_dup 2)))))]
1710   "TARGET_H8300H || TARGET_H8300S"
1711   "*
1712 {
1713   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1714     return \"divxs.b\\t%X2,%T0\";
1715   else
1716     return \"divxs.b\\t%X2,%T0\;mov.b\\t%t0,%s3\";
1717 }"
1718   [(set_attr "length" "6")])
1719
1720 (define_insn "udivmodhi4"
1721   [(set (match_operand:HI 0 "register_operand" "=r")
1722         (truncate:HI
1723           (udiv:SI
1724             (match_operand:SI 1 "register_operand" "0")
1725             (zero_extend:SI (match_operand:HI 2 "register_operand" "r")))))
1726    (set (match_operand:HI 3 "register_operand" "=r")
1727         (truncate:HI
1728           (umod:SI
1729             (match_dup 1)
1730             (zero_extend:SI (match_dup 2)))))]
1731   "TARGET_H8300H || TARGET_H8300S"
1732   "*
1733 {
1734   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1735     return \"divxu.w\\t%T2,%S0\";
1736   else
1737     return \"divxu.w\\t%T2,%S0\;mov.w\\t%e0,%f3\";
1738 }"
1739   [(set_attr "length" "4")])
1740
1741 (define_insn "divmodhi4"
1742   [(set (match_operand:HI 0 "register_operand" "=r")
1743         (truncate:HI
1744           (div:SI
1745             (match_operand:SI 1 "register_operand" "0")
1746             (sign_extend:SI (match_operand:HI 2 "register_operand" "r")))))
1747    (set (match_operand:HI 3 "register_operand" "=r")
1748         (truncate:HI
1749           (mod:SI
1750             (match_dup 1)
1751             (sign_extend:SI (match_dup 2)))))]
1752   "TARGET_H8300H || TARGET_H8300S"
1753   "*
1754 {
1755   if (find_reg_note (insn, REG_UNUSED, operands[3]))
1756     return \"divxs.w\\t%T2,%S0\";
1757   else
1758     return \"divxs.w\\t%T2,%S0\;mov.w\\t%e0,%f3\";
1759 }"
1760   [(set_attr "length" "6")])
1761 \f
1762 ;; ----------------------------------------------------------------------
1763 ;; AND INSTRUCTIONS
1764 ;; ----------------------------------------------------------------------
1765 (define_insn "bclrqi_msx"
1766   [(set (match_operand:QI 0 "bit_register_indirect_operand" "=WU")
1767         (and:QI (match_operand:QI 1 "bit_register_indirect_operand" "%0")
1768                 (match_operand:QI 2 "single_zero_operand" "Y0")))]
1769   "TARGET_H8300SX"
1770   "bclr\\t%W2,%0"
1771   [(set_attr "length" "8")])
1772
1773 (define_split
1774   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=U")
1775         (and:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1776                 (match_operand:HI 2 "single_zero_operand" "Y0")))]
1777   "TARGET_H8300SX"
1778   [(set (match_dup 0)
1779         (and:QI (match_dup 1)
1780                 (match_dup 2)))]
1781 {
1782   operands[0] = adjust_address (operands[0], QImode, 1);
1783   operands[1] = adjust_address (operands[1], QImode, 1);
1784 })
1785
1786 (define_insn "bclrhi_msx"
1787   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=m")
1788         (and:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1789                 (match_operand:HI 2 "single_zero_operand" "Y0")))]
1790   "TARGET_H8300SX"
1791   "bclr\\t%W2,%0"
1792   [(set_attr "length" "8")])
1793 (define_insn "*andqi3_2"
1794   [(set (match_operand:QI 0 "bit_operand" "=rQ,r")
1795         (and:QI (match_operand:QI 1 "bit_operand" "%0,WU")
1796                 (match_operand:QI 2 "h8300_src_operand" "rQi,IP1>X")))]
1797   "TARGET_H8300SX"
1798   "@
1799    and  %X2,%X0
1800    bfld %2,%1,%R0"
1801   [(set_attr "length" "*,8")
1802    (set_attr "length_table" "logicb,*")
1803    (set_attr "cc" "set_znv,none_0hit")])
1804
1805 (define_insn "andqi3_1"
1806   [(set (match_operand:QI 0 "bit_operand" "=r,U")
1807         (and:QI (match_operand:QI 1 "bit_operand" "%0,0")
1808                 (match_operand:QI 2 "h8300_src_operand" "rn,n")))]
1809   "register_operand (operands[0], QImode)
1810    || single_zero_operand (operands[2], QImode)"
1811   "@
1812    and  %X2,%X0
1813    bclr %W2,%R0"
1814   [(set_attr "length" "2,8")
1815    (set_attr "cc" "set_znv,none_0hit")])
1816
1817 (define_expand "andqi3"
1818   [(set (match_operand:QI 0 "register_operand" "")
1819         (and:QI (match_operand:QI 1 "register_operand" "")
1820                 (match_operand:QI 2 "h8300_src_operand" "")))]
1821   ""
1822   "")
1823
1824 (define_expand "andhi3"
1825   [(set (match_operand:HI 0 "register_operand" "")
1826         (and:HI (match_operand:HI 1 "register_operand" "")
1827                 (match_operand:HI 2 "h8300_src_operand" "")))]
1828   ""
1829   "")
1830
1831 (define_insn "*andorqi3"
1832   [(set (match_operand:QI 0 "register_operand" "=r")
1833         (ior:QI (and:QI (match_operand:QI 2 "register_operand" "r")
1834                         (match_operand:QI 3 "single_one_operand" "n"))
1835                 (match_operand:QI 1 "register_operand" "0")))]
1836   ""
1837   "bld\\t%V3,%X2\;bor\\t%V3,%X0\;bst\\t%V3,%X0"
1838   [(set_attr "length" "6")])
1839
1840 (define_insn "*andorhi3"
1841   [(set (match_operand:HI 0 "register_operand" "=r")
1842         (ior:HI (and:HI (match_operand:HI 2 "register_operand" "r")
1843                         (match_operand:HI 3 "single_one_operand" "n"))
1844                 (match_operand:HI 1 "register_operand" "0")))]
1845   ""
1846   "*
1847 {
1848   operands[3] = GEN_INT (INTVAL (operands[3]) & 0xffff);
1849   if (INTVAL (operands[3]) > 128)
1850     {
1851       operands[3] = GEN_INT (INTVAL (operands[3]) >> 8);
1852       return \"bld\\t%V3,%t2\;bor\\t%V3,%t0\;bst\\t%V3,%t0\";
1853     }
1854   return \"bld\\t%V3,%s2\;bor\\t%V3,%s0\;bst\\t%V3,%s0\";
1855 }"
1856   [(set_attr "length" "6")])
1857
1858 (define_insn "*andorsi3"
1859   [(set (match_operand:SI 0 "register_operand" "=r")
1860         (ior:SI (and:SI (match_operand:SI 2 "register_operand" "r")
1861                         (match_operand:SI 3 "single_one_operand" "n"))
1862                 (match_operand:SI 1 "register_operand" "0")))]
1863   "(INTVAL (operands[3]) & 0xffff) != 0"
1864   "*
1865 {
1866   operands[3] = GEN_INT (INTVAL (operands[3]) & 0xffff);
1867   if (INTVAL (operands[3]) > 128)
1868     {
1869       operands[3] = GEN_INT (INTVAL (operands[3]) >> 8);
1870       return \"bld\\t%V3,%x2\;bor\\t%V3,%x0\;bst\\t%V3,%x0\";
1871     }
1872   return \"bld\\t%V3,%w2\;bor\\t%V3,%w0\;bst\\t%V3,%w0\";
1873 }"
1874   [(set_attr "length" "6")])
1875
1876 (define_insn "*andorsi3_shift_8"
1877   [(set (match_operand:SI 0 "register_operand" "=r")
1878         (ior:SI (and:SI (ashift:SI (match_operand:SI 2 "register_operand" "r")
1879                                    (const_int 8))
1880                         (const_int 65280))
1881                 (match_operand:SI 1 "register_operand" "0")))]
1882   ""
1883   "or.b\\t%w2,%x0"
1884   [(set_attr "length" "2")])
1885
1886 (define_expand "andsi3"
1887   [(set (match_operand:SI 0 "register_operand" "")
1888         (and:SI (match_operand:SI 1 "register_operand" "")
1889                 (match_operand:SI 2 "h8300_src_operand" "")))]
1890   ""
1891   "")
1892
1893 ;; ----------------------------------------------------------------------
1894 ;; OR INSTRUCTIONS
1895 ;; ----------------------------------------------------------------------
1896 (define_insn "bsetqi_msx"
1897   [(set (match_operand:QI 0 "bit_register_indirect_operand" "=WU")
1898         (ior:QI (match_operand:QI 1 "bit_register_indirect_operand" "%0")
1899                 (match_operand:QI 2 "single_one_operand" "Y2")))]
1900   "TARGET_H8300SX" 
1901   "bset\\t%V2,%0"
1902   [(set_attr "length" "8")])
1903
1904 (define_split
1905   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=U")
1906         (ior:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1907                 (match_operand:HI 2 "single_one_operand" "Y2")))]
1908   "TARGET_H8300SX"
1909   [(set (match_dup 0)
1910         (ior:QI (match_dup 1)
1911                 (match_dup 2)))]
1912 {
1913   operands[0] = adjust_address (operands[0], QImode, 1);
1914   operands[1] = adjust_address (operands[1], QImode, 1);
1915 })
1916
1917 (define_insn "bsethi_msx"
1918   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=m")
1919         (ior:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1920                 (match_operand:HI 2 "single_one_operand" "Y2")))]
1921   "TARGET_H8300SX"
1922   "bset\\t%V2,%0"
1923   [(set_attr "length" "8")])
1924
1925 (define_insn "iorqi3_1"
1926   [(set (match_operand:QI 0 "bit_operand" "=rQ,U")
1927         (ior:QI (match_operand:QI 1 "bit_operand" "%0,0")
1928                 (match_operand:QI 2 "h8300_src_operand" "rQi,n")))]
1929   "TARGET_H8300SX || register_operand (operands[0], QImode)
1930    || single_one_operand (operands[2], QImode)"
1931   "@
1932    or\\t%X2,%X0
1933    bset\\t%V2,%R0"
1934   [(set_attr "length" "*,8")
1935    (set_attr "length_table" "logicb,*")
1936    (set_attr "cc" "set_znv,none_0hit")])
1937
1938 (define_expand "iorqi3"
1939   [(set (match_operand:QI 0 "register_operand" "")
1940         (ior:QI (match_operand:QI 1 "register_operand" "")
1941                 (match_operand:QI 2 "h8300_src_operand" "")))]
1942   ""
1943   "")
1944
1945 (define_expand "iorhi3"
1946   [(set (match_operand:HI 0 "register_operand" "")
1947         (ior:HI (match_operand:HI 1 "register_operand" "")
1948                 (match_operand:HI 2 "h8300_src_operand" "")))]
1949   ""
1950   "")
1951
1952 (define_expand "iorsi3"
1953   [(set (match_operand:SI 0 "register_operand" "")
1954         (ior:SI (match_operand:SI 1 "register_operand" "")
1955                 (match_operand:SI 2 "h8300_src_operand" "")))]
1956   ""
1957   "")
1958
1959 ;; ----------------------------------------------------------------------
1960 ;; XOR INSTRUCTIONS
1961 ;; ----------------------------------------------------------------------
1962 (define_insn "bnotqi_msx"
1963   [(set (match_operand:QI 0 "bit_register_indirect_operand" "=WU")
1964         (xor:QI (match_operand:QI 1 "bit_register_indirect_operand" "%0")
1965                 (match_operand:QI 2 "single_one_operand" "Y2")))]
1966   "TARGET_H8300SX"
1967   "bnot\\t%V2,%0"
1968   [(set_attr "length" "8")])
1969
1970 (define_split
1971   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=U")
1972         (xor:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1973                 (match_operand:HI 2 "single_one_operand" "Y2")))]
1974   "TARGET_H8300SX"
1975   [(set (match_dup 0)
1976         (xor:QI (match_dup 1)
1977                 (match_dup 2)))]
1978 {
1979   operands[0] = adjust_address (operands[0], QImode, 1);
1980   operands[1] = adjust_address (operands[1], QImode, 1);
1981 })
1982
1983 (define_insn "bnothi_msx"
1984   [(set (match_operand:HI 0 "bit_register_indirect_operand" "=m")
1985         (xor:HI (match_operand:HI 1 "bit_register_indirect_operand" "%0")
1986                 (match_operand:HI 2 "single_one_operand" "Y2")))]
1987   "TARGET_H8300SX"
1988   "bnot\\t%V2,%0"
1989   [(set_attr "length" "8")])
1990
1991 (define_insn "xorqi3_1"
1992   [(set (match_operand:QI 0 "bit_operand" "=r,U")
1993         (xor:QI (match_operand:QI 1 "bit_operand" "%0,0")
1994                 (match_operand:QI 2 "h8300_src_operand" "rQi,n")))]
1995   "TARGET_H8300SX || register_operand (operands[0], QImode)
1996    || single_one_operand (operands[2], QImode)"
1997   "@
1998    xor\\t%X2,%X0
1999    bnot\\t%V2,%R0"
2000   [(set_attr "length" "*,8")
2001    (set_attr "length_table" "logicb,*")
2002    (set_attr "cc" "set_znv,none_0hit")])
2003
2004 (define_expand "xorqi3"
2005   [(set (match_operand:QI 0 "register_operand" "")
2006         (xor:QI (match_operand:QI 1 "register_operand" "")
2007                 (match_operand:QI 2 "h8300_src_operand" "")))]
2008   ""
2009   "")
2010
2011 (define_expand "xorhi3"
2012   [(set (match_operand:HI 0 "register_operand" "")
2013         (xor:HI (match_operand:HI 1 "register_operand" "")
2014                 (match_operand:HI 2 "h8300_src_operand" "")))]
2015   ""
2016   "")
2017
2018 (define_expand "xorsi3"
2019   [(set (match_operand:SI 0 "register_operand" "")
2020         (xor:SI (match_operand:SI 1 "register_operand" "")
2021                 (match_operand:SI 2 "h8300_src_operand" "")))]
2022   ""
2023   "")
2024
2025 ;; ----------------------------------------------------------------------
2026 ;; {AND,IOR,XOR}{HI3,SI3} PATTERNS
2027 ;; ----------------------------------------------------------------------
2028
2029 ;; We need a separate pattern here because machines other than the
2030 ;; original H8300 don't have to split the 16-bit operand into a pair
2031 ;; of high/low instructions, so we can accept literal addresses, that
2032 ;; have to be loaded into a register on H8300.
2033 (define_insn "*logicalhi3_sn"
2034   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ")
2035         (match_operator:HI 3 "bit_operator"
2036           [(match_operand:HI 1 "h8300_dst_operand" "%0")
2037            (match_operand:HI 2 "h8300_src_operand" "rQi")]))]
2038   "(TARGET_H8300S || TARGET_H8300H) && h8300_operands_match_p (operands)"
2039   "* return output_logical_op (HImode, operands);"
2040   [(set (attr "length")
2041         (symbol_ref "compute_logical_op_length (HImode, operands)"))
2042    (set (attr "cc")
2043         (symbol_ref "compute_logical_op_cc (HImode, operands)"))])
2044
2045 (define_insn "*logicalsi3_sn"
2046   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ")
2047         (match_operator:SI 3 "bit_operator"
2048           [(match_operand:SI 1 "h8300_dst_operand" "%0")
2049            (match_operand:SI 2 "h8300_src_operand" "rQi")]))]
2050   "(TARGET_H8300S || TARGET_H8300H) && h8300_operands_match_p (operands)"
2051   "* return output_logical_op (SImode, operands);"
2052   [(set (attr "length")
2053         (symbol_ref "compute_logical_op_length (SImode, operands)"))
2054    (set (attr "cc")
2055         (symbol_ref "compute_logical_op_cc (SImode, operands)"))])
2056
2057 (define_insn "*logicalhi3"
2058   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ")
2059         (match_operator:HI 3 "bit_operator"
2060           [(match_operand:HI 1 "h8300_dst_operand" "%0")
2061            (match_operand:HI 2 "h8300_src_operand" "rQi")]))]
2062   "h8300_operands_match_p (operands)"
2063   "* return output_logical_op (HImode, operands);"
2064   [(set (attr "length")
2065         (symbol_ref "compute_logical_op_length (HImode, operands)"))
2066    (set (attr "cc")
2067         (symbol_ref "compute_logical_op_cc (HImode, operands)"))])
2068
2069 (define_insn "*logicalsi3"
2070   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ")
2071         (match_operator:SI 3 "bit_operator"
2072           [(match_operand:SI 1 "h8300_dst_operand" "%0")
2073            (match_operand:SI 2 "h8300_src_operand" "rQi")]))]
2074   "h8300_operands_match_p (operands)"
2075   "* return output_logical_op (SImode, operands);"
2076   [(set (attr "length")
2077         (symbol_ref "compute_logical_op_length (SImode, operands)"))
2078    (set (attr "cc")
2079         (symbol_ref "compute_logical_op_cc (SImode, operands)"))])
2080 \f
2081 ;; ----------------------------------------------------------------------
2082 ;; NEGATION INSTRUCTIONS
2083 ;; ----------------------------------------------------------------------
2084
2085 (define_expand "negqi2"
2086   [(set (match_operand:QI 0 "register_operand" "")
2087         (neg:QI (match_operand:QI 1 "register_operand" "")))]
2088   ""
2089   "")
2090
2091 (define_insn "*negqi2"
2092   [(set (match_operand:QI 0 "h8300_dst_operand" "=rQ")
2093         (neg:QI (match_operand:QI 1 "h8300_dst_operand" "0")))]
2094   ""
2095   "neg  %X0"
2096   [(set_attr "length_table" "unary")
2097    (set_attr "cc" "set_zn")])
2098
2099 (define_expand "neghi2"
2100   [(set (match_operand:HI 0 "register_operand" "")
2101         (neg:HI (match_operand:HI 1 "register_operand" "")))]
2102   ""
2103   "
2104 {
2105   if (TARGET_H8300)
2106     {
2107       emit_insn (gen_neghi2_h8300 (operands[0], operands[1]));
2108       DONE;
2109     }
2110 }")
2111
2112 (define_expand "neghi2_h8300"
2113   [(set (match_dup 2)
2114         (not:HI (match_operand:HI 1 "register_operand" "")))
2115    (set (match_dup 2) (plus:HI (match_dup 2) (const_int 1)))
2116    (set (match_operand:HI 0 "register_operand" "")
2117         (match_dup 2))]
2118   ""
2119   "operands[2] = gen_reg_rtx (HImode);")
2120
2121 (define_insn "*neghi2_h8300hs"
2122   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ")
2123         (neg:HI (match_operand:HI 1 "h8300_dst_operand" "0")))]
2124   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
2125   "neg.w        %T0"
2126   [(set_attr "length_table" "unary")
2127    (set_attr "cc" "set_zn")])
2128
2129 (define_expand "negsi2"
2130   [(set (match_operand:SI 0 "register_operand" "")
2131         (neg:SI (match_operand:SI 1 "register_operand" "")))]
2132   ""
2133   "
2134 {
2135   if (TARGET_H8300)
2136     {
2137       emit_insn (gen_negsi2_h8300 (operands[0], operands[1]));
2138       DONE;
2139     }
2140 }")
2141
2142 (define_expand "negsi2_h8300"
2143   [(set (match_dup 2)
2144         (not:SI (match_operand:SI 1 "register_operand" "")))
2145    (set (match_dup 2) (plus:SI (match_dup 2) (const_int 1)))
2146    (set (match_operand:SI 0 "register_operand" "")
2147         (match_dup 2))]
2148   ""
2149   "operands[2] = gen_reg_rtx (SImode);")
2150
2151 (define_insn "*negsi2_h8300hs"
2152   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ")
2153         (neg:SI (match_operand:SI 1 "h8300_dst_operand" "0")))]
2154   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
2155   "neg.l        %S0"
2156   [(set_attr "length_table" "unary")
2157    (set_attr "cc" "set_zn")])
2158
2159 (define_expand "negsf2"
2160   [(set (match_operand:SF 0 "register_operand" "")
2161         (neg:SF (match_operand:SF 1 "register_operand" "")))]
2162   ""
2163   "")
2164
2165 (define_insn "*negsf2_h8300"
2166   [(set (match_operand:SF 0 "register_operand" "=r")
2167         (neg:SF (match_operand:SF 1 "register_operand" "0")))]
2168   "TARGET_H8300"
2169   "xor.b\\t#128,%z0"
2170   [(set_attr "length" "2")])
2171
2172 (define_insn "*negsf2_h8300hs"
2173   [(set (match_operand:SF 0 "register_operand" "=r")
2174         (neg:SF (match_operand:SF 1 "register_operand" "0")))]
2175   "TARGET_H8300H || TARGET_H8300S"
2176   "xor.w\\t#32768,%e0"
2177   [(set_attr "length" "4")])
2178 \f
2179 ;; ----------------------------------------------------------------------
2180 ;; ABSOLUTE VALUE INSTRUCTIONS
2181 ;; ----------------------------------------------------------------------
2182
2183 (define_expand "abssf2"
2184   [(set (match_operand:SF 0 "register_operand" "")
2185         (abs:SF (match_operand:SF 1 "register_operand" "")))]
2186   ""
2187   "")
2188
2189 (define_insn "*abssf2_h8300"
2190   [(set (match_operand:SF 0 "register_operand" "=r")
2191         (abs:SF (match_operand:SF 1 "register_operand" "0")))]
2192   "TARGET_H8300"
2193   "and.b\\t#127,%z0"
2194   [(set_attr "length" "2")])
2195
2196 (define_insn "*abssf2_h8300hs"
2197   [(set (match_operand:SF 0 "register_operand" "=r")
2198         (abs:SF (match_operand:SF 1 "register_operand" "0")))]
2199   "TARGET_H8300H || TARGET_H8300S"
2200   "and.w\\t#32767,%e0"
2201   [(set_attr "length" "4")])
2202 \f
2203 ;; ----------------------------------------------------------------------
2204 ;; NOT INSTRUCTIONS
2205 ;; ----------------------------------------------------------------------
2206
2207 (define_expand "one_cmplqi2"
2208   [(set (match_operand:QI 0 "register_operand" "")
2209         (not:QI (match_operand:QI 1 "register_operand" "")))]
2210   ""
2211   "")
2212
2213 (define_insn "*one_cmplqi2"
2214   [(set (match_operand:QI 0 "h8300_dst_operand" "=rQ")
2215         (not:QI (match_operand:QI 1 "h8300_dst_operand" "0")))]
2216   ""
2217   "not  %X0"
2218   [(set_attr "length_table" "unary")
2219    (set_attr "cc" "set_znv")])
2220
2221 (define_expand "one_cmplhi2"
2222   [(set (match_operand:HI 0 "register_operand" "")
2223         (not:HI (match_operand:HI 1 "register_operand" "")))]
2224   ""
2225   "")
2226
2227 (define_insn "*one_cmplhi2_h8300"
2228   [(set (match_operand:HI 0 "register_operand" "=r")
2229         (not:HI (match_operand:HI 1 "register_operand" "0")))]
2230   "TARGET_H8300"
2231   "not  %s0\;not        %t0"
2232   [(set_attr "length" "4")])
2233
2234 (define_insn "*one_cmplhi2_h8300hs"
2235   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ")
2236         (not:HI (match_operand:HI 1 "h8300_dst_operand" "0")))]
2237   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
2238   "not.w        %T0"
2239   [(set_attr "cc" "set_znv")
2240    (set_attr "length_table" "unary")])
2241
2242 (define_expand "one_cmplsi2"
2243   [(set (match_operand:SI 0 "register_operand" "")
2244         (not:SI (match_operand:SI 1 "register_operand" "")))]
2245   ""
2246   "")
2247
2248 (define_insn "*one_cmplsi2_h8300"
2249   [(set (match_operand:SI 0 "register_operand" "=r")
2250         (not:SI (match_operand:SI 1 "register_operand" "0")))]
2251   "TARGET_H8300"
2252   "not  %w0\;not        %x0\;not        %y0\;not        %z0"
2253   [(set_attr "length" "8")])
2254
2255 (define_insn "*one_cmplsi2_h8300hs"
2256   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ")
2257         (not:SI (match_operand:SI 1 "h8300_dst_operand" "0")))]
2258   "(TARGET_H8300H || TARGET_H8300S) && h8300_operands_match_p (operands)"
2259   "not.l        %S0"
2260   [(set_attr "cc" "set_znv")
2261    (set_attr "length_table" "unary")])
2262 \f
2263 ;; ----------------------------------------------------------------------
2264 ;; JUMP INSTRUCTIONS
2265 ;; ----------------------------------------------------------------------
2266
2267 ;; Conditional jump instructions
2268
2269 (define_expand "cbranchqi4"
2270   [(use (match_operator 0 "ordered_comparison_operator"
2271          [(match_operand:QI 1 "h8300_dst_operand" "")
2272           (match_operand:QI 2 "h8300_src_operand" "")]))
2273    (use (match_operand 3 ""))]
2274   ""
2275   "h8300_expand_branch (operands); DONE;")
2276
2277 (define_expand "cbranchhi4"
2278   [(use (match_operator 0 "ordered_comparison_operator"
2279          [(match_operand:HI 1 "h8300_dst_operand" "")
2280           (match_operand:HI 2 "h8300_src_operand" "")]))
2281    (use (match_operand 3 ""))]
2282   ""
2283   "
2284 {
2285   /* Force operand1 into a register if we're compiling
2286      for the H8/300.  */
2287   if ((GET_CODE (operands[2]) != REG && operands[2] != const0_rtx)
2288       && TARGET_H8300)
2289     operands[2] = force_reg (HImode, operands[2]);
2290   h8300_expand_branch (operands); DONE;
2291 }")
2292
2293 (define_expand "cbranchsi4"
2294   [(use (match_operator 0 "ordered_comparison_operator"
2295          [(match_operand:SI 1 "h8300_dst_operand" "")
2296           (match_operand:SI 2 "h8300_src_operand" "")]))
2297    (use (match_operand 3 ""))]
2298   "TARGET_H8300H || TARGET_H8300S"
2299   "h8300_expand_branch (operands); DONE;")
2300
2301 (define_insn "branch_true"
2302   [(set (pc)
2303         (if_then_else (match_operator 1 "comparison_operator"
2304                                       [(cc0) (const_int 0)])
2305                       (label_ref (match_operand 0 "" ""))
2306                       (pc)))]
2307   ""
2308   "*
2309 {
2310   if ((cc_status.flags & CC_OVERFLOW_UNUSABLE) != 0
2311       && (GET_CODE (operands[1]) == GT
2312           || GET_CODE (operands[1]) == GE
2313           || GET_CODE (operands[1]) == LE
2314           || GET_CODE (operands[1]) == LT))
2315     {
2316       cc_status.flags &= ~CC_OVERFLOW_UNUSABLE;
2317       return 0;
2318     }
2319
2320   if (get_attr_length (insn) == 2)
2321     return \"b%j1       %l0\";
2322   else if (get_attr_length (insn) == 4)
2323     return \"b%j1       %l0:16\";
2324   else
2325     return \"b%k1       .Lh8BR%=\;jmp   @%l0\\n.Lh8BR%=:\";
2326 }"
2327  [(set_attr "type" "branch")
2328    (set_attr "cc" "none")])
2329
2330 (define_insn "branch_false"
2331   [(set (pc)
2332         (if_then_else (match_operator 1 "comparison_operator"
2333                                       [(cc0) (const_int 0)])
2334                       (pc)
2335                       (label_ref (match_operand 0 "" ""))))]
2336   ""
2337   "*
2338 {
2339   if ((cc_status.flags & CC_OVERFLOW_UNUSABLE) != 0
2340       && (GET_CODE (operands[1]) == GT
2341           || GET_CODE (operands[1]) == GE
2342           || GET_CODE (operands[1]) == LE
2343           || GET_CODE (operands[1]) == LT))
2344     {
2345       cc_status.flags &= ~CC_OVERFLOW_UNUSABLE;
2346       return 0;
2347     }
2348
2349   if (get_attr_length (insn) == 2)
2350     return \"b%k1       %l0\";
2351   else if (get_attr_length (insn) == 4)
2352     return \"b%k1       %l0:16\";
2353   else
2354     return \"b%j1       .Lh8BR%=\;jmp   @%l0\\n.Lh8BR%=:\";
2355 }"
2356   [(set_attr "type" "branch")
2357    (set_attr "cc" "none")])
2358
2359 (define_insn "*brabc"
2360   [(set (pc)
2361         (if_then_else
2362          (eq (zero_extract (match_operand:QI 1 "bit_memory_operand" "WU")
2363                            (const_int 1)
2364                            (match_operand:QI 2 "immediate_operand" "n"))
2365              (const_int 0))
2366          (label_ref (match_operand 0 "" ""))
2367          (pc)))]
2368   "TARGET_H8300SX"
2369   "*
2370 {
2371   switch (get_attr_length (insn)
2372           - h8300_insn_length_from_table (insn, operands))
2373     {
2374     case 2:
2375       return \"bra/bc   %2,%R1,%l0\";
2376
2377     case 4:
2378       return \"bra/bc   %2,%R1,%l0:16\";
2379
2380     default:
2381       return \"bra/bs   %2,%R1,.Lh8BR%=\;jmp    @%l0\\n.Lh8BR%=:\";
2382     }
2383 }"
2384   [(set_attr "type" "bitbranch")
2385    (set_attr "length_table" "bitbranch")
2386    (set_attr "cc" "none")])
2387
2388 (define_insn "*brabs"
2389   [(set (pc)
2390         (if_then_else
2391          (ne (zero_extract (match_operand:QI 1 "bit_memory_operand" "WU")
2392                            (const_int 1)
2393                            (match_operand:QI 2 "immediate_operand" "n"))
2394              (const_int 0))
2395          (label_ref (match_operand 0 "" ""))
2396          (pc)))]
2397   "TARGET_H8300SX"
2398   "*
2399 {
2400   switch (get_attr_length (insn)
2401           - h8300_insn_length_from_table (insn, operands))
2402     {
2403     case 2:
2404       return \"bra/bs   %2,%R1,%l0\";
2405
2406     case 4:
2407       return \"bra/bs   %2,%R1,%l0:16\";
2408
2409     default:
2410       return \"bra/bc   %2,%R1,.Lh8BR%=\;jmp    @%l0\\n.Lh8BR%=:\";
2411     }
2412 }"
2413   [(set_attr "type" "bitbranch")
2414    (set_attr "length_table" "bitbranch")
2415    (set_attr "cc" "none")])
2416
2417 ;; Unconditional and other jump instructions.
2418
2419 (define_insn "jump"
2420   [(set (pc)
2421         (label_ref (match_operand 0 "" "")))]
2422   ""
2423   "*
2424 {
2425   if (final_sequence != 0)
2426     {
2427       if (get_attr_length (insn) == 2)
2428         return \"bra/s  %l0\";
2429       else
2430         {
2431           /* The branch isn't short enough to use bra/s.  Output the
2432              branch and delay slot in their normal order.
2433
2434              If this is a backward branch, it will now be branching two
2435              bytes further than previously thought.  The length-based
2436              test for bra vs. jump is very conservative though, so the
2437              branch will still be within range.  */
2438           rtvec vec;
2439           int seen;
2440
2441           vec = XVEC (final_sequence, 0);
2442           final_sequence = 0;
2443           final_scan_insn (RTVEC_ELT (vec, 1), asm_out_file, optimize, 1, & seen);
2444           final_scan_insn (RTVEC_ELT (vec, 0), asm_out_file, optimize, 1, & seen);
2445           INSN_DELETED_P (RTVEC_ELT (vec, 1)) = 1;
2446           return \"\";
2447         }
2448     }
2449   else if (get_attr_length (insn) == 2)
2450     return \"bra        %l0\";
2451   else if (get_attr_length (insn) == 4)
2452     return \"bra        %l0:16\";
2453   else
2454     return \"jmp        @%l0\";
2455 }"
2456   [(set_attr "type" "branch")
2457    (set (attr "delay_slot")
2458         (if_then_else (ne (symbol_ref "TARGET_H8300SX") (const_int 0))
2459                       (const_string "jump")
2460                       (const_string "none")))
2461    (set_attr "cc" "none")])
2462
2463 ;; This is a define expand, because pointers may be either 16 or 32 bits.
2464
2465 (define_expand "tablejump"
2466   [(parallel [(set (pc) (match_operand 0 "register_operand" ""))
2467               (use (label_ref (match_operand 1 "" "")))])]
2468   ""
2469   "")
2470
2471 (define_insn "*tablejump_h8300"
2472   [(set (pc) (match_operand:HI 0 "register_operand" "r"))
2473    (use (label_ref (match_operand 1 "" "")))]
2474   "TARGET_H8300"
2475   "jmp  @%0"
2476   [(set_attr "cc" "none")
2477    (set_attr "length" "2")])
2478
2479 (define_insn "*tablejump_h8300hs_advanced"
2480   [(set (pc) (match_operand:SI 0 "register_operand" "r"))
2481    (use (label_ref (match_operand 1 "" "")))]
2482   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_NORMAL_MODE"
2483   "jmp  @%0"
2484   [(set_attr "cc" "none")
2485    (set_attr "length" "2")])
2486
2487 (define_insn "*tablejump_h8300hs_normal"
2488   [(set (pc) (match_operand:HI 0 "register_operand" "r"))
2489    (use (label_ref (match_operand 1 "" "")))]
2490   "(TARGET_H8300H || TARGET_H8300S) && TARGET_NORMAL_MODE"
2491   "jmp @%S0"
2492   [(set_attr "cc" "none")
2493    (set_attr "length" "2")])
2494
2495 ;; This is a define expand, because pointers may be either 16 or 32 bits.
2496
2497 (define_expand "indirect_jump"
2498   [(set (pc) (match_operand 0 "jump_address_operand" ""))]
2499   ""
2500   "")
2501
2502 (define_insn "*indirect_jump_h8300"
2503   [(set (pc) (match_operand:HI 0 "jump_address_operand" "Vr"))]
2504   "TARGET_H8300"
2505   "jmp  @%0"
2506   [(set_attr "cc" "none")
2507    (set_attr "length" "2")])
2508
2509 (define_insn "*indirect_jump_h8300hs_advanced"
2510   [(set (pc) (match_operand:SI 0 "jump_address_operand" "Vr"))]
2511   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_NORMAL_MODE"
2512   "jmp @%0"
2513   [(set_attr "cc" "none")
2514    (set_attr "length" "2")])
2515
2516 (define_insn "*indirect_jump_h8300hs_normal"
2517   [(set (pc) (match_operand:HI 0 "jump_address_operand" "Vr"))]
2518   "(TARGET_H8300H || TARGET_H8300S) && TARGET_NORMAL_MODE"
2519   "jmp @%S0"
2520   [(set_attr "cc" "none")
2521    (set_attr "length" "2")])
2522
2523 ;; Call subroutine with no return value.
2524
2525 ;; ??? Even though we use HImode here, this works on the H8/300H and H8S.
2526
2527 (define_insn "call"
2528   [(call (match_operand:QI 0 "call_insn_operand" "or")
2529          (match_operand:HI 1 "general_operand" "g"))]
2530   ""
2531   "*
2532 {
2533   if (GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF
2534       && SYMBOL_REF_FLAG (XEXP (operands[0], 0)))
2535     return \"jsr\\t@%0:8\";
2536   else
2537     return \"jsr\\t%0\";
2538 }"
2539   [(set_attr "type" "call")
2540    (set (attr "length")
2541         (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
2542                       (const_int 2)
2543                       (const_int 4)))])
2544
2545 ;; Call subroutine, returning value in operand 0
2546 ;; (which must be a hard register).
2547
2548 ;; ??? Even though we use HImode here, this works on the H8/300H and H8S.
2549
2550 (define_insn "call_value"
2551   [(set (match_operand 0 "" "=r")
2552         (call (match_operand:QI 1 "call_insn_operand" "or")
2553               (match_operand:HI 2 "general_operand" "g")))]
2554   ""
2555   "*
2556 {
2557   if (GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
2558       && SYMBOL_REF_FLAG (XEXP (operands[1], 0)))
2559     return \"jsr\\t@%1:8\";
2560   else
2561     return \"jsr\\t%1\";
2562 }"
2563   [(set_attr "type" "call")
2564    (set (attr "length")
2565         (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
2566                       (const_int 2)
2567                       (const_int 4)))])
2568
2569 (define_insn "nop"
2570   [(const_int 0)]
2571   ""
2572   "nop"
2573   [(set_attr "cc" "none")
2574    (set_attr "length" "2")])
2575 \f
2576 ;; ----------------------------------------------------------------------
2577 ;; PROLOGUE/EPILOGUE-RELATED INSTRUCTIONS
2578 ;; ----------------------------------------------------------------------
2579
2580 (define_expand "push_h8300"
2581   [(set (mem:HI (pre_dec:HI (reg:HI SP_REG)))
2582         (match_operand:HI 0 "register_operand" ""))]
2583   "TARGET_H8300"
2584   "")
2585
2586 (define_expand "push_h8300hs_advanced"
2587   [(set (mem:SI (pre_dec:SI (reg:SI SP_REG)))
2588         (match_operand:SI 0 "register_operand" ""))]
2589   "TARGET_H8300H && TARGET_H8300S && !TARGET_NORMAL_MODE"
2590   "")
2591
2592 (define_expand "push_h8300hs_normal"
2593   [(set (mem:SI (pre_dec:HI (reg:HI SP_REG)))
2594         (match_operand:SI 0 "register_operand" ""))]
2595   "TARGET_H8300H && TARGET_H8300S && TARGET_NORMAL_MODE"
2596   "")
2597
2598 (define_expand "pop_h8300"
2599   [(set (match_operand:HI 0 "register_operand" "")
2600         (mem:HI (post_inc:HI (reg:HI SP_REG))))]
2601   "TARGET_H8300"
2602   "")
2603
2604 (define_expand "pop_h8300hs_advanced"
2605   [(set (match_operand:SI 0 "register_operand" "")
2606         (mem:SI (post_inc:SI (reg:SI SP_REG))))]
2607   "TARGET_H8300H && TARGET_H8300S && !TARGET_NORMAL_MODE"
2608   "")
2609
2610 (define_expand "pop_h8300hs_normal"
2611   [(set (match_operand:SI 0 "register_operand" "")
2612         (mem:SI (post_inc:HI (reg:HI SP_REG))))]
2613   "TARGET_H8300H && TARGET_H8300S && TARGET_NORMAL_MODE"
2614   "")
2615
2616 (define_insn "ldm_h8300sx"
2617   [(match_parallel           0 "h8300_ldm_parallel"
2618      [(set (match_operand:SI 1 "register_operand" "")
2619            (match_operand:SI 2 "memory_operand" ""))])]
2620   "TARGET_H8300S"
2621   {
2622     operands[3] = SET_DEST (XVECEXP (operands[0], 0,
2623                                      XVECLEN (operands[0], 0) - 2));
2624     return "ldm.l\t@er7+,%S1-%S3";
2625   }
2626   [(set_attr "cc" "none")
2627    (set_attr "length" "4")])
2628
2629 (define_insn "stm_h8300sx"
2630   [(match_parallel           0 "h8300_stm_parallel"
2631      [(set (match_operand:SI 1 "memory_operand" "")
2632            (match_operand:SI 2 "register_operand" ""))])]
2633   "TARGET_H8300S"
2634   {
2635     operands[3] = SET_SRC (XVECEXP (operands[0], 0,
2636                                     XVECLEN (operands[0], 0) - 2));
2637     return "stm.l\t%S2-%S3,@-er7";
2638   }
2639   [(set_attr "cc" "none")
2640    (set_attr "length" "4")])
2641
2642 (define_insn "return_h8sx"
2643   [(match_parallel           0 "h8300_return_parallel"
2644      [(return)
2645       (set (match_operand:SI 1 "register_operand" "")
2646            (match_operand:SI 2 "memory_operand" ""))])]
2647   "TARGET_H8300SX"
2648   {
2649     operands[3] = SET_DEST (XVECEXP (operands[0], 0,
2650                                      XVECLEN (operands[0], 0) - 2));
2651     if (h8300_current_function_interrupt_function_p ())
2652       return "rte/l\t%S1-%S3";
2653     else
2654       return "rts/l\t%S1-%S3";
2655   }
2656   [(set_attr "cc" "none")
2657    (set_attr "can_delay" "no")
2658    (set_attr "length" "2")])
2659
2660 (define_expand "return"
2661   [(return)]
2662   "h8300_can_use_return_insn_p ()"
2663   "")
2664
2665 (define_insn "*return_1"
2666   [(return)]
2667   "reload_completed"
2668   "*
2669 {
2670   if (h8300_current_function_interrupt_function_p ())
2671     return \"rte\";
2672   else
2673     return \"rts\";
2674 }"
2675   [(set_attr "cc" "none")
2676    (set_attr "can_delay" "no")
2677    (set_attr "length" "2")])
2678
2679 (define_expand "prologue"
2680   [(const_int 0)]
2681   ""
2682   "h8300_expand_prologue (); DONE;")
2683
2684 (define_expand "epilogue"
2685   [(return)]
2686   ""
2687   "h8300_expand_epilogue (); DONE;")
2688
2689 (define_insn "monitor_prologue"
2690   [(unspec_volatile [(const_int 0)] UNSPEC_MONITOR)]
2691   ""
2692   "*
2693 {
2694   if (TARGET_H8300)
2695     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\";
2696   else if (TARGET_H8300H)
2697     return \"mov.l\\ter0,@-er7\;stc\\tccr,r0l\;mov.b\\tr0l,@(4,er7)\;mov.l\\t@er7+,er0\;orc\\t#128,ccr\";
2698   else if (TARGET_H8300S)
2699     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\";
2700   gcc_unreachable ();
2701 }"
2702   [(set_attr "length" "20")])
2703 \f
2704 ;; ----------------------------------------------------------------------
2705 ;; EXTEND INSTRUCTIONS
2706 ;; ----------------------------------------------------------------------
2707
2708 (define_expand "zero_extendqihi2"
2709   [(set (match_operand:HI 0 "register_operand" "")
2710         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "")))]
2711   ""
2712   "")
2713
2714 (define_insn "*zero_extendqihi2_h8300"
2715   [(set (match_operand:HI 0 "register_operand" "=r,r")
2716         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2717   "TARGET_H8300"
2718   "@
2719   mov.b #0,%t0
2720   #"
2721   [(set_attr "length" "2,10")])
2722
2723 (define_insn "*zero_extendqihi2_h8300hs"
2724   [(set (match_operand:HI 0 "register_operand" "=r,r")
2725         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2726   "TARGET_H8300H || TARGET_H8300S"
2727   "@
2728   extu.w        %T0
2729   #"
2730   [(set_attr "length" "2,10")
2731    (set_attr "cc" "set_znv,set_znv")])
2732
2733 ;; Split the zero extension of a general operand (actually a memory
2734 ;; operand) into a load of the operand and the actual zero extension
2735 ;; so that 1) the length will be accurate, and 2) the zero extensions
2736 ;; appearing at the end of basic blocks may be merged.
2737
2738 (define_split
2739   [(set (match_operand:HI 0 "register_operand" "")
2740         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "")))]
2741   "reload_completed"
2742   [(set (match_dup 2)
2743         (match_dup 1))
2744    (set (match_dup 0)
2745         (zero_extend:HI (match_dup 2)))]
2746   "operands[2] = gen_rtx_REG (QImode, REGNO (operands[0]));")
2747
2748 (define_expand "zero_extendqisi2"
2749   [(set (match_operand:SI 0 "register_operand" "")
2750         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "")))]
2751   ""
2752   {
2753     if (TARGET_H8300SX)
2754       operands[1] = force_reg (QImode, operands[1]);
2755   })
2756
2757 (define_insn "*zero_extendqisi2_h8300"
2758   [(set (match_operand:SI 0 "register_operand" "=r,r")
2759         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2760   "TARGET_H8300"
2761   "@
2762   mov.b #0,%x0\;sub.w   %e0,%e0
2763   mov.b %R1,%w0\;mov.b  #0,%x0\;sub.w   %e0,%e0"
2764   [(set_attr "length" "4,8")])
2765
2766 (define_insn "*zero_extendqisi2_h8300hs"
2767   [(set (match_operand:SI 0 "register_operand" "=r,r")
2768         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2769   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX"
2770   "#")
2771
2772 (define_split
2773   [(set (match_operand:SI 0 "register_operand" "")
2774         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "")))]
2775   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX
2776    && reg_overlap_mentioned_p (operands[0], operands[1])
2777    && reload_completed"
2778   [(set (match_dup 2)
2779         (match_dup 1))
2780    (set (match_dup 3)
2781         (zero_extend:HI (match_dup 2)))
2782    (set (match_dup 0)
2783         (zero_extend:SI (match_dup 3)))]
2784   "operands[2] = gen_lowpart (QImode, operands[0]);
2785    operands[3] = gen_lowpart (HImode, operands[0]);")
2786
2787 (define_split
2788   [(set (match_operand:SI 0 "register_operand" "")
2789         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "")))]
2790   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX
2791    && !reg_overlap_mentioned_p (operands[0], operands[1])
2792    && reload_completed"
2793   [(set (match_dup 0)
2794         (const_int 0))
2795    (set (strict_low_part (match_dup 2))
2796         (match_dup 1))]
2797   "operands[2] = gen_rtx_REG (QImode, REGNO (operands[0]));")
2798
2799 (define_insn "*zero_extendqisi2_h8sx"
2800   [(set (match_operand:SI 0 "register_operand" "=r")
2801         (zero_extend:SI (match_operand:QI 1 "register_operand" "0")))]
2802   "TARGET_H8300SX"
2803   "extu.l\t#2,%0"
2804   [(set_attr "length" "2")
2805    (set_attr "cc" "set_znv")])
2806
2807 (define_expand "zero_extendhisi2"
2808   [(set (match_operand:SI 0 "register_operand" "")
2809         (zero_extend:SI (match_operand:HI 1 "register_operand" "")))]
2810   ""
2811   "")
2812
2813 ;; %e prints the high part of a CONST_INT, not the low part.  Arggh.
2814 (define_insn "*zero_extendhisi2_h8300"
2815   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
2816         (zero_extend:SI (match_operand:HI 1 "general_operand_src" "0,i,g>")))]
2817   "TARGET_H8300"
2818   "@
2819   sub.w %e0,%e0
2820   mov.w %f1,%f0\;sub.w  %e0,%e0
2821   mov.w %e1,%f0\;sub.w  %e0,%e0"
2822   [(set_attr "length" "2,4,6")])
2823
2824 (define_insn "*zero_extendhisi2_h8300hs"
2825   [(set (match_operand:SI 0 "register_operand" "=r")
2826         (zero_extend:SI (match_operand:HI 1 "register_operand" "0")))]
2827   "TARGET_H8300H || TARGET_H8300S"
2828   "extu.l       %S0"
2829   [(set_attr "length" "2")
2830    (set_attr "cc" "set_znv")])
2831
2832 (define_expand "extendqihi2"
2833   [(set (match_operand:HI 0 "register_operand" "")
2834         (sign_extend:HI (match_operand:QI 1 "register_operand" "")))]
2835   ""
2836   "")
2837
2838 (define_insn "*extendqihi2_h8300"
2839   [(set (match_operand:HI 0 "register_operand" "=r,r")
2840         (sign_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2841   "TARGET_H8300"
2842   "@
2843   bld   #7,%s0\;subx    %t0,%t0
2844   mov.b %R1,%s0\;bld    #7,%s0\;subx    %t0,%t0"
2845   [(set_attr "length" "4,8")])
2846
2847 (define_insn "*extendqihi2_h8300hs"
2848   [(set (match_operand:HI 0 "register_operand" "=r")
2849         (sign_extend:HI (match_operand:QI 1 "register_operand" "0")))]
2850   "TARGET_H8300H || TARGET_H8300S"
2851   "exts.w       %T0"
2852   [(set_attr "length" "2")
2853    (set_attr "cc" "set_znv")])
2854
2855 (define_expand "extendqisi2"
2856   [(set (match_operand:SI 0 "register_operand" "")
2857         (sign_extend:SI (match_operand:QI 1 "register_operand" "")))]
2858   ""
2859   "")
2860
2861 (define_insn "*extendqisi2_h8300"
2862   [(set (match_operand:SI 0 "register_operand" "=r,r")
2863         (sign_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
2864   "TARGET_H8300"
2865   "@
2866   bld   #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0
2867   mov.b %R1,%w0\;bld    #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0"
2868   [(set_attr "length" "8,12")])
2869
2870 ;; The following pattern is needed because without the pattern, the
2871 ;; combiner would split (sign_extend:SI (reg:QI)) into two 24-bit
2872 ;; shifts, one ashift and one ashiftrt.
2873
2874 (define_insn_and_split "*extendqisi2_h8300hs"
2875   [(set (match_operand:SI 0 "register_operand" "=r")
2876         (sign_extend:SI (match_operand:QI 1 "register_operand" "0")))]
2877   "(TARGET_H8300H || TARGET_H8300S) && !TARGET_H8300SX"
2878   "#"
2879   "&& reload_completed"
2880   [(set (match_dup 2)
2881         (sign_extend:HI (match_dup 1)))
2882    (set (match_dup 0)
2883         (sign_extend:SI (match_dup 2)))]
2884   "operands[2] = gen_rtx_REG (HImode, REGNO (operands[0]));")
2885
2886 (define_insn "*extendqisi2_h8sx"
2887   [(set (match_operand:SI 0 "register_operand" "=r")
2888         (sign_extend:SI (match_operand:QI 1 "register_operand" "0")))]
2889   "TARGET_H8300SX"
2890   "exts.l\t#2,%0"
2891   [(set_attr "length" "2")
2892    (set_attr "cc" "set_znv")])
2893
2894 (define_expand "extendhisi2"
2895   [(set (match_operand:SI 0 "register_operand" "")
2896         (sign_extend:SI (match_operand:HI 1 "register_operand" "")))]
2897   ""
2898   "")
2899
2900 (define_insn "*extendhisi2_h8300"
2901   [(set (match_operand:SI 0 "register_operand" "=r,r")
2902         (sign_extend:SI (match_operand:HI 1 "general_operand_src" "0,g>")))]
2903   "TARGET_H8300"
2904   "@
2905   bld   #7,%x0\;subx    %y0,%y0\;subx   %z0,%z0
2906   mov.w %T1,%f0\;bld    #7,%x0\;subx    %y0,%y0\;subx   %z0,%z0"
2907   [(set_attr "length" "6,10")])
2908
2909 (define_insn "*extendhisi2_h8300hs"
2910   [(set (match_operand:SI 0 "register_operand" "=r")
2911         (sign_extend:SI (match_operand:HI 1 "register_operand" "0")))]
2912   "TARGET_H8300H || TARGET_H8300S"
2913   "exts.l       %S0"
2914   [(set_attr "length" "2")
2915    (set_attr "cc" "set_znv")])
2916 \f
2917 ;; ----------------------------------------------------------------------
2918 ;; SHIFTS
2919 ;; ----------------------------------------------------------------------
2920 ;;
2921 ;; We make some attempt to provide real efficient shifting.  One example is
2922 ;; doing an 8-bit shift of a 16-bit value by moving a byte reg into the other
2923 ;; reg and moving 0 into the former reg.
2924 ;;
2925 ;; We also try to achieve this in a uniform way.  IE: We don't try to achieve
2926 ;; this in both rtl and at insn emit time.  Ideally, we'd use rtl as that would
2927 ;; give the optimizer more cracks at the code.  However, we wish to do things
2928 ;; like optimizing shifting the sign bit to bit 0 by rotating the other way.
2929 ;; There is rtl to handle this (rotate + and), but the H8/300 doesn't handle
2930 ;; 16-bit rotates.  Also, if we emit complicated rtl, combine may not be able
2931 ;; to detect cases it can optimize.
2932 ;;
2933 ;; For these and other fuzzy reasons, I've decided to go the less pretty but
2934 ;; easier "do it at insn emit time" route.
2935
2936 ;; QI BIT SHIFTS
2937
2938 (define_expand "ashlqi3"
2939   [(set (match_operand:QI 0 "register_operand" "")
2940         (ashift:QI (match_operand:QI 1 "register_operand" "")
2941                    (match_operand:QI 2 "nonmemory_operand" "")))]
2942   ""
2943   "if (expand_a_shift (QImode, ASHIFT, operands)) DONE;")
2944
2945 (define_expand "ashrqi3"
2946   [(set (match_operand:QI 0 "register_operand" "")
2947         (ashiftrt:QI (match_operand:QI 1 "register_operand" "")
2948                      (match_operand:QI 2 "nonmemory_operand" "")))]
2949   ""
2950   "if (expand_a_shift (QImode, ASHIFTRT, operands)) DONE;")
2951
2952 (define_expand "lshrqi3"
2953   [(set (match_operand:QI 0 "register_operand" "")
2954         (lshiftrt:QI (match_operand:QI 1 "register_operand" "")
2955                      (match_operand:QI 2 "nonmemory_operand" "")))]
2956   ""
2957   "if (expand_a_shift (QImode, LSHIFTRT, operands)) DONE;")
2958
2959 (define_insn ""
2960   [(set (match_operand:QI 0 "h8300_dst_operand" "=rQ")
2961         (match_operator:QI 3 "h8sx_unary_shift_operator"
2962                         [(match_operand:QI 1 "h8300_dst_operand" "0")
2963                          (match_operand:QI 2 "const_int_operand" "")]))]
2964   "h8300_operands_match_p (operands)"
2965   { return output_h8sx_shift (operands, 'b', 'X'); }
2966   [(set_attr "length_table" "unary")
2967    (set_attr "cc" "set_znv")])
2968
2969 (define_insn ""
2970   [(set (match_operand:QI 0 "register_operand" "=r")
2971         (match_operator:QI 3 "h8sx_binary_shift_operator"
2972                         [(match_operand:QI 1 "register_operand" "0")
2973                          (match_operand:QI 2 "nonmemory_operand" "r P3>X")]))]
2974   ""
2975   { return output_h8sx_shift (operands, 'b', 'X'); }
2976   [(set_attr "length" "4")
2977    (set_attr "cc" "set_znv")])
2978
2979 (define_insn "*shiftqi"
2980   [(set (match_operand:QI 0 "register_operand" "=r,r")
2981         (match_operator:QI 3 "nshift_operator"
2982                         [ (match_operand:QI 1 "register_operand" "0,0")
2983                           (match_operand:QI 2 "nonmemory_operand" "R,rn")]))
2984    (clobber (match_scratch:QI 4 "=X,&r"))]
2985   ""
2986   "* return output_a_shift (operands);"
2987   [(set (attr "length")
2988         (symbol_ref "compute_a_shift_length (insn, operands)"))
2989    (set (attr "cc")
2990         (symbol_ref "compute_a_shift_cc (insn, operands)"))])
2991
2992 ;; HI BIT SHIFTS
2993
2994 (define_expand "ashlhi3"
2995   [(set (match_operand:HI 0 "register_operand" "")
2996         (ashift:HI (match_operand:HI 1 "register_operand" "")
2997                    (match_operand:QI 2 "nonmemory_operand" "")))]
2998   ""
2999   "if (expand_a_shift (HImode, ASHIFT, operands)) DONE;")
3000
3001 (define_expand "lshrhi3"
3002   [(set (match_operand:HI 0 "register_operand" "")
3003         (lshiftrt:HI (match_operand:HI 1 "register_operand" "")
3004                      (match_operand:QI 2 "nonmemory_operand" "")))]
3005   ""
3006   "if (expand_a_shift (HImode, LSHIFTRT, operands)) DONE;")
3007
3008 (define_expand "ashrhi3"
3009   [(set (match_operand:HI 0 "register_operand" "")
3010         (ashiftrt:HI (match_operand:HI 1 "register_operand" "")
3011                      (match_operand:QI 2 "nonmemory_operand" "")))]
3012   ""
3013   "if (expand_a_shift (HImode, ASHIFTRT, operands)) DONE;")
3014
3015 (define_insn ""
3016   [(set (match_operand:HI 0 "h8300_dst_operand" "=rQ")
3017         (match_operator:HI 3 "h8sx_unary_shift_operator"
3018                         [(match_operand:HI 1 "h8300_dst_operand" "0")
3019                          (match_operand:QI 2 "const_int_operand" "")]))]
3020   "h8300_operands_match_p (operands)"
3021   { return output_h8sx_shift (operands, 'w', 'T'); }
3022   [(set_attr "length_table" "unary")
3023    (set_attr "cc" "set_znv")])
3024
3025 (define_insn ""
3026   [(set (match_operand:HI 0 "register_operand" "=r")
3027         (match_operator:HI 3 "h8sx_binary_shift_operator"
3028                         [(match_operand:HI 1 "register_operand" "0")
3029                          (match_operand:QI 2 "nonmemory_operand" "r P4>X")]))]
3030   ""
3031   { return output_h8sx_shift (operands, 'w', 'T'); }
3032   [(set_attr "length" "4")
3033    (set_attr "cc" "set_znv")])
3034
3035 (define_insn "*shifthi"
3036   [(set (match_operand:HI 0 "register_operand" "=r,r")
3037         (match_operator:HI 3 "nshift_operator"
3038                         [ (match_operand:HI 1 "register_operand" "0,0")
3039                           (match_operand:QI 2 "nonmemory_operand" "S,rn")]))
3040    (clobber (match_scratch:QI 4 "=X,&r"))]
3041   ""
3042   "* return output_a_shift (operands);"
3043   [(set (attr "length")
3044         (symbol_ref "compute_a_shift_length (insn, operands)"))
3045    (set (attr "cc")
3046         (symbol_ref "compute_a_shift_cc (insn, operands)"))])
3047
3048 ;;  SI BIT SHIFTS
3049
3050 (define_expand "ashlsi3"
3051   [(set (match_operand:SI 0 "register_operand" "")
3052         (ashift:SI (match_operand:SI 1 "register_operand" "")
3053                    (match_operand:QI 2 "nonmemory_operand" "")))]
3054   ""
3055   "if (expand_a_shift (SImode, ASHIFT, operands)) DONE;")
3056
3057 (define_expand "lshrsi3"
3058   [(set (match_operand:SI 0 "register_operand" "")
3059         (lshiftrt:SI (match_operand:SI 1 "register_operand" "")
3060                      (match_operand:QI 2 "nonmemory_operand" "")))]
3061   ""
3062   "if (expand_a_shift (SImode, LSHIFTRT, operands)) DONE;")
3063
3064 (define_expand "ashrsi3"
3065   [(set (match_operand:SI 0 "register_operand" "")
3066         (ashiftrt:SI (match_operand:SI 1 "register_operand" "")
3067                      (match_operand:QI 2 "nonmemory_operand" "")))]
3068   ""
3069   "if (expand_a_shift (SImode, ASHIFTRT, operands)) DONE;")
3070
3071 (define_insn ""
3072   [(set (match_operand:SI 0 "h8300_dst_operand" "=rQ")
3073         (match_operator:SI 3 "h8sx_unary_shift_operator"
3074                         [(match_operand:SI 1 "h8300_dst_operand" "0")
3075                          (match_operand:QI 2 "const_int_operand" "")]))]
3076   "h8300_operands_match_p (operands)"
3077   { return output_h8sx_shift (operands, 'l', 'S'); }
3078   [(set_attr "length_table" "unary")
3079    (set_attr "cc" "set_znv")])
3080
3081 (define_insn ""
3082   [(set (match_operand:SI 0 "register_operand" "=r")
3083         (match_operator:SI 3 "h8sx_binary_shift_operator"
3084                         [(match_operand:SI 1 "register_operand" "0")
3085                          (match_operand:QI 2 "nonmemory_operand" "r P5>X")]))]
3086   ""
3087   { return output_h8sx_shift (operands, 'l', 'S'); }
3088   [(set_attr "length" "4")
3089    (set_attr "cc" "set_znv")])
3090
3091 (define_insn "*shiftsi"
3092   [(set (match_operand:SI 0 "register_operand" "=r,r")
3093         (match_operator:SI 3 "nshift_operator"
3094                         [ (match_operand:SI 1 "register_operand" "0,0")
3095                           (match_operand:QI 2 "nonmemory_operand" "T,rn")]))
3096    (clobber (match_scratch:QI 4 "=X,&r"))]
3097   ""
3098   "* return output_a_shift (operands);"
3099   [(set (attr "length")
3100         (symbol_ref "compute_a_shift_length (insn, operands)"))
3101    (set (attr "cc")
3102         (symbol_ref "compute_a_shift_cc (insn, operands)"))])
3103
3104 ;; Split a variable shift into a loop.  If the register containing
3105 ;; the shift count dies, then we just use that register.
3106
3107 (define_split
3108   [(set (match_operand 0 "register_operand" "")
3109         (match_operator 2 "nshift_operator"
3110                         [(match_dup 0)
3111                          (match_operand:QI 1 "register_operand" "")]))
3112    (clobber (match_operand:QI 3 "register_operand" ""))]
3113   "epilogue_completed
3114    && find_regno_note (insn, REG_DEAD, REGNO (operands[1]))"
3115   [(set (cc0) (compare (match_dup 1)
3116                        (const_int 0)))
3117    (set (pc)
3118         (if_then_else (le (cc0) (const_int 0))
3119                       (label_ref (match_dup 5))
3120                       (pc)))
3121    (match_dup 4)
3122    (parallel
3123      [(set (match_dup 0)
3124            (match_op_dup 2 [(match_dup 0) (const_int 1)]))
3125       (clobber (scratch:QI))])
3126    (set (match_dup 1)
3127         (plus:QI (match_dup 1) (const_int -1)))
3128    (set (cc0) (compare (match_dup 1)
3129                        (const_int 0)))
3130    (set (pc)
3131         (if_then_else (ne (cc0) (const_int 0))
3132                       (label_ref (match_dup 4))
3133                       (pc)))
3134    (match_dup 5)]
3135   "operands[4] = gen_label_rtx ();
3136    operands[5] = gen_label_rtx ();")
3137
3138 (define_split
3139   [(set (match_operand 0 "register_operand" "")
3140         (match_operator 2 "nshift_operator"
3141                         [(match_dup 0)
3142                          (match_operand:QI 1 "register_operand" "")]))
3143    (clobber (match_operand:QI 3 "register_operand" ""))]
3144   "epilogue_completed
3145    && !find_regno_note (insn, REG_DEAD, REGNO (operands[1]))"
3146   [(set (match_dup 3)
3147         (match_dup 1))
3148    (set (cc0) (compare (match_dup 3)
3149                        (const_int 0)))
3150    (set (pc)
3151         (if_then_else (le (cc0) (const_int 0))
3152                       (label_ref (match_dup 5))
3153                       (pc)))
3154    (match_dup 4)
3155    (parallel
3156      [(set (match_dup 0)
3157            (match_op_dup 2 [(match_dup 0) (const_int 1)]))
3158       (clobber (scratch:QI))])
3159    (set (match_dup 3)
3160         (plus:QI (match_dup 3) (const_int -1)))
3161    (set (cc0) (compare (match_dup 3)
3162                        (const_int 0)))
3163    (set (pc)
3164         (if_then_else (ne (cc0) (const_int 0))
3165                       (label_ref (match_dup 4))
3166                       (pc)))
3167    (match_dup 5)]
3168   "operands[4] = gen_label_rtx ();
3169    operands[5] = gen_label_rtx ();")
3170 \f
3171 ;; ----------------------------------------------------------------------
3172 ;; ROTATIONS
3173 ;; ----------------------------------------------------------------------
3174
3175 (define_expand "rotlqi3"
3176   [(set (match_operand:QI 0 "register_operand" "")
3177         (rotate:QI (match_operand:QI 1 "register_operand" "")
3178                    (match_operand:QI 2 "nonmemory_operand" "")))]
3179   ""
3180   "if (expand_a_rotate (operands)) DONE;")
3181
3182 (define_insn "rotlqi3_1"
3183   [(set (match_operand:QI 0 "register_operand" "=r")
3184         (rotate:QI (match_operand:QI 1 "register_operand" "0")
3185                    (match_operand:QI 2 "immediate_operand" "")))]
3186   ""
3187   "* return output_a_rotate (ROTATE, operands);"
3188   [(set (attr "length")
3189         (symbol_ref "compute_a_rotate_length (operands)"))])
3190
3191 (define_expand "rotlhi3"
3192   [(set (match_operand:HI 0 "register_operand" "")
3193         (rotate:HI (match_operand:HI 1 "register_operand" "")
3194                    (match_operand:QI 2 "nonmemory_operand" "")))]
3195   ""
3196   "if (expand_a_rotate (operands)) DONE;")
3197
3198 (define_insn "rotlhi3_1"
3199   [(set (match_operand:HI 0 "register_operand" "=r")
3200         (rotate:HI (match_operand:HI 1 "register_operand" "0")
3201                    (match_operand:QI 2 "immediate_operand" "")))]
3202   ""
3203   "* return output_a_rotate (ROTATE, operands);"
3204   [(set (attr "length")
3205         (symbol_ref "compute_a_rotate_length (operands)"))])
3206
3207 (define_expand "rotlsi3"
3208   [(set (match_operand:SI 0 "register_operand" "")
3209         (rotate:SI (match_operand:SI 1 "register_operand" "")
3210                    (match_operand:QI 2 "nonmemory_operand" "")))]
3211   "TARGET_H8300H || TARGET_H8300S"
3212   "if (expand_a_rotate (operands)) DONE;")
3213
3214 (define_insn "rotlsi3_1"
3215   [(set (match_operand:SI 0 "register_operand" "=r")
3216         (rotate:SI (match_operand:SI 1 "register_operand" "0")
3217                    (match_operand:QI 2 "immediate_operand" "")))]
3218   "TARGET_H8300H || TARGET_H8300S"
3219   "* return output_a_rotate (ROTATE, operands);"
3220   [(set (attr "length")
3221         (symbol_ref "compute_a_rotate_length (operands)"))])
3222 \f
3223 ;; -----------------------------------------------------------------
3224 ;; BIT FIELDS
3225 ;; -----------------------------------------------------------------
3226 ;; The H8/300 has given 1/8th of its opcode space to bitfield
3227 ;; instructions so let's use them as well as we can.
3228
3229 ;; You'll never believe all these patterns perform one basic action --
3230 ;; load a bit from the source, optionally invert the bit, then store it
3231 ;; in the destination (which is known to be zero).
3232 ;;
3233 ;; Combine obviously need some work to better identify this situation and
3234 ;; canonicalize the form better.
3235
3236 ;;
3237 ;; Normal loads with a 16bit destination.
3238 ;;
3239
3240 (define_insn ""
3241   [(set (match_operand:HI 0 "register_operand" "=&r")
3242         (zero_extract:HI (match_operand:HI 1 "register_operand" "r")
3243                          (const_int 1)
3244                          (match_operand:HI 2 "immediate_operand" "n")))]
3245   "TARGET_H8300"
3246   "sub.w        %0,%0\;bld      %Z2,%Y1\;bst    #0,%X0"
3247   [(set_attr "length" "6")])
3248
3249 ;;
3250 ;; Inverted loads with a 16bit destination.
3251 ;;
3252
3253 (define_insn ""
3254   [(set (match_operand:HI 0 "register_operand" "=&r")
3255         (zero_extract:HI (xor:HI (match_operand:HI 1 "register_operand" "r")
3256                                  (match_operand:HI 3 "const_int_operand" "n"))
3257                          (const_int 1)
3258                          (match_operand:HI 2 "const_int_operand" "n")))]
3259   "(TARGET_H8300 || TARGET_H8300SX)
3260    && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
3261   "sub.w        %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
3262   [(set_attr "length" "8")])
3263
3264 ;;
3265 ;; Normal loads with a 32bit destination.
3266 ;;
3267
3268 (define_insn "*extzv_1_r_h8300"
3269   [(set (match_operand:SI 0 "register_operand" "=&r")
3270         (zero_extract:SI (match_operand:HI 1 "register_operand" "r")
3271                          (const_int 1)
3272                          (match_operand 2 "const_int_operand" "n")))]
3273   "TARGET_H8300
3274    && INTVAL (operands[2]) < 16"
3275   "* return output_simode_bld (0, operands);"
3276   [(set_attr "length" "8")])
3277
3278 (define_insn "*extzv_1_r_h8300hs"
3279   [(set (match_operand:SI 0 "register_operand" "=r,r")
3280         (zero_extract:SI (match_operand:SI 1 "register_operand" "?0,r")
3281                          (const_int 1)
3282                          (match_operand 2 "const_int_operand" "n,n")))]
3283   "(TARGET_H8300H || TARGET_H8300S)
3284    && INTVAL (operands[2]) < 16"
3285   "* return output_simode_bld (0, operands);"
3286   [(set_attr "cc" "set_znv,set_znv")
3287    (set_attr "length" "8,6")])
3288
3289 ;;
3290 ;; Inverted loads with a 32bit destination.
3291 ;;
3292
3293 (define_insn "*extzv_1_r_inv_h8300"
3294   [(set (match_operand:SI 0 "register_operand" "=&r")
3295         (zero_extract:SI (xor:HI (match_operand:HI 1 "register_operand" "r")
3296                                  (match_operand:HI 3 "const_int_operand" "n"))
3297                          (const_int 1)
3298                          (match_operand 2 "const_int_operand" "n")))]
3299   "TARGET_H8300
3300    && INTVAL (operands[2]) < 16
3301    && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
3302   "* return output_simode_bld (1, operands);"
3303   [(set_attr "length" "8")])
3304
3305 (define_insn "*extzv_1_r_inv_h8300hs"
3306   [(set (match_operand:SI 0 "register_operand" "=r,r")
3307         (zero_extract:SI (xor:SI (match_operand:SI 1 "register_operand" "?0,r")
3308                                  (match_operand 3 "const_int_operand" "n,n"))
3309                          (const_int 1)
3310                          (match_operand 2 "const_int_operand" "n,n")))]
3311   "(TARGET_H8300H || TARGET_H8300S)
3312    && INTVAL (operands[2]) < 16
3313    && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
3314   "* return output_simode_bld (1, operands);"
3315   [(set_attr "cc" "set_znv,set_znv")
3316    (set_attr "length" "8,6")])
3317
3318 (define_expand "insv"
3319   [(set (zero_extract:HI (match_operand:HI 0 "general_operand" "")
3320                          (match_operand:HI 1 "general_operand" "")
3321                          (match_operand:HI 2 "general_operand" ""))
3322         (match_operand:HI 3 "general_operand" ""))]
3323   "TARGET_H8300 || TARGET_H8300SX"
3324   "
3325 {
3326   if (TARGET_H8300SX)
3327     {
3328       if (GET_CODE (operands[1]) == CONST_INT
3329           && GET_CODE (operands[2]) == CONST_INT
3330           && INTVAL (operands[1]) <= 8
3331           && INTVAL (operands[2]) >= 0
3332           && INTVAL (operands[1]) + INTVAL (operands[2]) <= 8
3333           && memory_operand (operands[0], GET_MODE (operands[0])))
3334         {
3335           /* If the source operand is zero, it's better to use AND rather
3336              than BFST.  Likewise OR if the operand is all ones.  */
3337           if (GET_CODE (operands[3]) == CONST_INT)
3338             {
3339               HOST_WIDE_INT mask = (1 << INTVAL (operands[1])) - 1;
3340               if ((INTVAL (operands[3]) & mask) == 0)
3341                 FAIL;
3342               if ((INTVAL (operands[3]) & mask) == mask)
3343                 FAIL;
3344             }
3345           if (! bit_memory_operand (operands[0], GET_MODE (operands[0])))
3346             {
3347               if (!can_create_pseudo_p ())
3348                 FAIL;
3349               operands[0] =
3350                 replace_equiv_address (operands[0],
3351                                        force_reg (Pmode,
3352                                                   XEXP (operands[0], 0)));
3353             }
3354           operands[3] = gen_lowpart (QImode, operands[3]);
3355           if (! operands[3])
3356             FAIL;
3357           if (! register_operand (operands[3], QImode))
3358             {
3359               if (!can_create_pseudo_p ())
3360                 FAIL;
3361               operands[3] = force_reg (QImode, operands[3]);
3362             }
3363           emit_insn (gen_bfst (adjust_address (operands[0], QImode, 0),
3364                                operands[3], operands[1], operands[2]));
3365           DONE;
3366         }
3367
3368       FAIL;
3369     }
3370
3371   /* We only have single bit bit-field instructions.  */
3372   if (INTVAL (operands[1]) != 1)
3373     FAIL;
3374
3375   /* For now, we don't allow memory operands.  */
3376   if (GET_CODE (operands[0]) == MEM
3377       || GET_CODE (operands[3]) == MEM)
3378     FAIL;
3379
3380   if (GET_CODE (operands[3]) != REG)
3381     operands[3] = force_reg (HImode, operands[3]);
3382 }")
3383
3384 (define_insn ""
3385   [(set (zero_extract:HI (match_operand:HI 0 "register_operand" "+r")
3386                          (const_int 1)
3387                          (match_operand:HI 1 "immediate_operand" "n"))