OSDN Git Service

8b6b39ad1d79d2247c3ec09f1a10e34967eedbce
[pf3gnuchains/gcc-fork.git] / gcc / config / h8300 / h8300.md
1 ;; GCC machine description for Hitachi H8/300
2 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 ;; 2001 Free Software Foundation, Inc.
4
5 ;;   Contributed by Steve Chamberlain (sac@cygnus.com),
6 ;;   Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
7
8 ;; This file is part of GNU CC.
9
10 ;; GNU CC is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU CC is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU CC; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;; The original PO technology requires these to be ordered by speed,
26 ;; so that assigner will pick the fastest.
27
28 ;; See file "rtl.def" for documentation on define_insn, match_*, et. al.
29
30 (define_attr "cpu" "h8300,h8300h"
31   (const (symbol_ref "cpu_type")))
32
33 ;; Many logical operations should have "bit" variants if only one
34 ;; bit is going to be operated on.
35
36 ;; (and (logical op) (const_int X))
37 ;; If const_int only specifies a few bits (like a single byte in a 4 byte
38 ;; operation, then it's more efficient to only apply the and and logical_op
39 ;; to the bits we care about.
40
41 ;; Some of the extend instructions accept a general_operand_src, which
42 ;; allows all the normal memory addressing modes.  The length computations
43 ;; don't take this into account.  The lengths in the MD file should be
44 ;; "worst case" and then be adjusted to their correct values by
45 ;; h8300_adjust_insn_length.
46
47 ;; On the h8300h, adds/subs operate on the 32bit "er" registers.  Right
48 ;; now GCC doesn't expose the "e" half to the compiler, so using add/subs
49 ;; for addhi and subhi is safe.
50 ;; Long term, we want to expose the "e" half to the compiler (gives us
51 ;; 8 more 16bit registers).  At that point addhi and subhi can't use adds/subs.
52
53 ;; There's currently no way to have a insv/extzv expander for the H8/300H
54 ;; because word_mode is different for the H8/300 and H8/300H.
55
56 ;; Shifts/rotates by small constants should be handled by special
57 ;; patterns so we get the length and cc status correct.
58
59 ;; Bitfield operations no longer accept memory operands.  We need
60 ;; to add variants which operate on memory back to the MD.
61
62 ;; ??? Implement remaining bit ops available on the h8300
63
64 (define_attr "type" "branch,arith"
65   (const_string "arith"))
66
67 ;; The size of instructions in bytes.
68
69 (define_attr "length" ""
70   (cond [(eq_attr "type" "branch")
71          (if_then_else (and (ge (minus (pc) (match_dup 0))
72                                 (const_int -120))
73                             (le (minus (pc) (match_dup 0))
74                                 (const_int 120)))
75                        (const_int 2)
76                        (if_then_else (and (eq_attr "cpu" "h8300h")
77                                           (and (ge (minus (pc) (match_dup 0))
78                                                    (const_int -32000))
79                                                (le (minus (pc) (match_dup 0))
80                                                    (const_int 32000))))
81                                      (const_int 4)
82                                      (const_int 6)))]
83         (const_int 200)))
84
85 ;; The necessity of instruction length adjustment.
86
87 (define_attr "adjust_length" "yes,no"
88   (cond [(eq_attr "type" "branch") (const_string "no")]
89         (const_string "yes")))
90
91 ;; Condition code settings.
92 ;; none - insn does not affect cc
93 ;; none_0hit - insn does not affect cc but it does modify operand 0
94 ;;      This attribute is used to keep track of when operand 0 changes.
95 ;;      See the description of NOTICE_UPDATE_CC for more info.
96 ;; set_znv - insn sets z,n,v to usable values (like a tst insn); c is unknown.
97 ;; set_zn  - insn sets z,n to usable values; v,c are unknown.
98 ;; compare - compare instruction
99 ;; clobber - value of cc is unknown
100 (define_attr "cc" "none,none_0hit,set_znv,set_zn,compare,clobber"
101   (const_string "clobber"))
102 \f
103 ;; ----------------------------------------------------------------------
104 ;; MOVE INSTRUCTIONS
105 ;; ----------------------------------------------------------------------
106
107 ;; movqi
108
109 (define_insn "pushqi1_h8300"
110   [(parallel [(set (reg:HI 7)
111                    (plus:HI (reg:HI 7) (const_int -2)))
112               (set (mem:QI (plus:HI (reg:HI 7) (const_int -1)))
113                    (match_operand:QI 0 "register_operand" "r"))])]
114   "TARGET_H8300"
115   "mov.w       %T0,@-r7"
116   [(set_attr "length" "2")
117    (set_attr "cc" "clobber")])
118
119 (define_insn "pushqi1_h8300hs"
120   [(parallel [(set (reg:SI 7)
121                    (plus:SI (reg:SI 7) (const_int -4)))
122               (set (mem:QI (plus:SI (reg:SI 7) (const_int -3)))
123                    (match_operand:QI 0 "register_operand" "r"))])]
124   "TARGET_H8300H || TARGET_H8300S"
125   "mov.l       %S0,@-er7"
126   [(set_attr "length" "4")
127    (set_attr "cc" "clobber")])
128
129 (define_expand "pushqi1"
130   [(use (match_operand:QI 0 "register_operand" "r"))]
131   ""
132   "
133 {
134   if (TARGET_H8300)
135     emit_insn (gen_pushqi1_h8300 (operands[0]));
136   else
137     emit_insn (gen_pushqi1_h8300hs (operands[0]));
138   DONE;
139 }")
140
141 (define_insn ""
142   [(set (match_operand:QI 0 "general_operand_dst" "=r,r ,<,r,r,m")
143         (match_operand:QI 1 "general_operand_src"      " I,r>,r,n,m,r"))]
144   "TARGET_H8300
145    && (register_operand (operands[0],QImode)
146        || register_operand (operands[1], QImode))"
147   "@
148    sub.b        %X0,%X0
149    mov.b        %R1,%X0
150    mov.b        %X1,%R0
151    mov.b        %R1,%X0
152    mov.b        %R1,%X0
153    mov.b        %X1,%R0"
154   [(set_attr "length" "2,2,2,2,4,4")
155    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
156
157 (define_insn ""
158   [(set (match_operand:QI 0 "general_operand_dst" "=r,r ,<,r,r,m")
159         (match_operand:QI 1 "general_operand_src"      " I,r>,r,n,m,r"))]
160   "(TARGET_H8300H || TARGET_H8300S)
161    && (register_operand (operands[0],QImode)
162        || register_operand (operands[1], QImode))"
163   "@
164    sub.b        %X0,%X0
165    mov.b        %R1,%X0
166    mov.b        %X1,%R0
167    mov.b        %R1,%X0
168    mov.b        %R1,%X0
169    mov.b        %X1,%R0"
170   [(set_attr "length" "2,2,2,2,8,8")
171    (set_attr "cc" "set_zn,set_znv,set_znv,clobber,set_znv,set_znv")])
172
173 (define_expand "movqi"
174   [(set (match_operand:QI 0 "general_operand_dst" "")
175         (match_operand:QI 1 "general_operand_src" ""))]
176   ""
177   "
178 {
179   /* One of the ops has to be in a register.  */
180   if (!register_operand (operand0, QImode)
181       && !register_operand (operand1, QImode))
182     {
183       operands[1] = copy_to_mode_reg (QImode, operand1);
184     }
185 }")
186
187 (define_insn "movstrictqi"
188   [(set (strict_low_part (match_operand:QI 0 "general_operand_dst" "+r,r,r,r"))
189                          (match_operand:QI 1 "general_operand_src" "I,r,n,m"))]
190   ""
191   "@
192    sub.b        %X0,%X0
193    mov.b        %X1,%X0
194    mov.b        %R1,%X0
195    mov.b        %R1,%X0"
196   [(set_attr_alternative "length"
197      [(const_int 2) (const_int 2) (const_int 2)
198       (if_then_else (eq_attr "cpu" "h8300") (const_int 4) (const_int 8))])
199    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv")])
200
201 ;; movhi
202
203 (define_expand "pushhi1_h8300"
204   [(set (mem:QI (pre_dec:HI (reg:HI 7)))
205         (match_operand:QI 0 "register_operand" "r"))]
206   "TARGET_H8300"
207   "")
208
209 (define_insn "pushhi1_h8300hs"
210   [(parallel [(set (reg:SI 7)
211                    (plus:SI (reg:SI 7) (const_int -4)))
212               (set (mem:HI (plus:SI (reg:SI 7) (const_int -2)))
213                    (match_operand:HI 0 "register_operand" "r"))])]
214   "TARGET_H8300H || TARGET_H8300S"
215   "mov.l       %S0,@-er7"
216   [(set_attr "length" "4")
217    (set_attr "cc" "clobber")])
218
219 (define_expand "pushhi1"
220   [(use (match_operand:QI 0 "register_operand" "r"))]
221   ""
222   "
223 {
224   if (TARGET_H8300)
225     emit_insn (gen_pushhi1_h8300 (operands[0]));
226   else
227     emit_insn (gen_pushhi1_h8300hs (operands[0]));
228   DONE;
229 }")
230
231 (define_insn ""
232   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,<,r,r,m")
233         (match_operand:HI 1 "general_operand_src" "I,r>,r,i,m,r"))]
234   "TARGET_H8300
235    && (register_operand (operands[0],HImode)
236        || register_operand (operands[1], HImode))
237    && !(GET_CODE (operands[0]) == MEM
238         && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
239         && GET_CODE (XEXP (XEXP (operands[0], 0), 0)) == REG
240         && GET_CODE (operands[1]) == REG
241         && REGNO (XEXP (XEXP (operands[0], 0), 0)) == REGNO (operands[1]))"
242   "@
243    sub.w        %T0,%T0
244    mov.w        %T1,%T0
245    mov.w        %T1,%T0
246    mov.w        %T1,%T0
247    mov.w        %T1,%T0
248    mov.w        %T1,%T0"
249   [(set_attr "length" "2,2,2,4,4,4")
250    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
251
252 (define_insn ""
253   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,<,r,r,m")
254         (match_operand:HI 1 "general_operand_src" "I,r>,r,i,m,r"))]
255   "(TARGET_H8300H || TARGET_H8300S)
256    && (register_operand (operands[0],HImode)
257        || register_operand (operands[1], HImode))"
258   "@
259    sub.w        %T0,%T0
260    mov.w        %T1,%T0
261    mov.w        %T1,%T0
262    mov.w        %T1,%T0
263    mov.w        %T1,%T0
264    mov.w        %T1,%T0"
265   [(set_attr "length" "2,2,2,4,8,8")
266    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
267
268 (define_expand "movhi"
269   [(set (match_operand:HI 0 "general_operand_dst" "")
270         (match_operand:HI 1 "general_operand_src" ""))]
271   ""
272   "
273 {
274   /* One of the ops has to be in a register.  */
275   if (!register_operand (operand1, HImode)
276       && !register_operand (operand0, HImode))
277     {
278       operands[1] = copy_to_mode_reg (HImode, operand1);
279     }
280 }")
281
282 (define_insn "movstricthi"
283   [(set (strict_low_part (match_operand:HI 0 "general_operand_dst" "+r,r,r,r"))
284                          (match_operand:HI 1 "general_operand_src" "I,r,i,m"))]
285   ""
286   "@
287    sub.w        %T0,%T0
288    mov.w        %T1,%T0
289    mov.w        %T1,%T0
290    mov.w        %T1,%T0"
291   [(set_attr_alternative "length"
292      [(const_int 2) (const_int 2) (const_int 4)
293       (if_then_else (eq_attr "cpu" "h8300") (const_int 4) (const_int 8))])
294    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv")])
295
296 ;; movsi
297
298 (define_expand "movsi"
299   [(set (match_operand:SI 0 "general_operand_dst" "")
300         (match_operand:SI 1 "general_operand_src" ""))]
301   ""
302   "
303 {
304   if (TARGET_H8300)
305     {
306       if (do_movsi (operands))
307         DONE;
308     }
309   else
310     {
311       /* One of the ops has to be in a register.  */
312       if (!register_operand (operand1, SImode)
313           && !register_operand (operand0, SImode))
314         {
315           operands[1] = copy_to_mode_reg (SImode, operand1);
316         }
317     }
318 }")
319
320 (define_expand "movsf"
321   [(set (match_operand:SF 0 "general_operand_dst" "")
322         (match_operand:SF 1 "general_operand_src" ""))]
323   ""
324   "
325 {
326   if (TARGET_H8300)
327     {
328       if (do_movsi (operands))
329         DONE;
330     }
331   else
332     {
333       /* One of the ops has to be in a register.  */
334       if (!register_operand (operand1, SFmode)
335           && !register_operand (operand0, SFmode))
336         {
337           operands[1] = copy_to_mode_reg (SFmode, operand1);
338         }
339     }
340 }")
341
342 (define_insn "movsi_h8300"
343   [(set (match_operand:SI 0 "general_operand_dst" "=r,r,r,o,<,r")
344         (match_operand:SI 1 "general_operand_src" "I,r,io,r,r,>"))]
345   "TARGET_H8300
346    && (register_operand (operands[0], SImode)
347        || register_operand (operands[1], SImode))"
348   "*
349 {
350   int rn = -1;
351   switch (which_alternative)
352     {
353     case 0:
354       return \"sub.w    %e0,%e0\;sub.w  %f0,%f0\";
355     case 1:
356       if (REGNO (operands[0]) < REGNO (operands[1]))
357         return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
358       else
359         return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
360     case 2:
361       /* Make sure we don't trample the register we index with.  */
362       if (GET_CODE (operands[1]) == MEM)
363         {
364           rtx inside = XEXP (operands[1],0);
365           if (REG_P (inside))
366             {
367               rn = REGNO (inside);
368             }
369           else if (GET_CODE (inside) == PLUS)
370             {
371               rtx lhs = XEXP (inside,0);
372               rtx rhs = XEXP (inside,1);
373               if (REG_P (lhs)) rn = REGNO (lhs);
374               if (REG_P (rhs)) rn = REGNO (rhs);
375             }
376         }
377       if (rn == REGNO (operands[0]))
378         {
379           /* Move the second word first.  */
380           return \"mov.w        %f1,%f0\;mov.w  %e1,%e0\";
381         }
382       else
383         {
384           /* See if either half is zero.  If so, use sub.w to clear
385              that half.  */
386           if (GET_CODE (operands[1]) == CONST_INT)
387             {
388               if ((INTVAL (operands[1]) & 0xffff) == 0)
389                 return \"mov.w  %e1,%e0\;sub.w  %f0,%f0\";
390               if (((INTVAL (operands[1]) >> 16) & 0xffff) == 0)
391                 return \"sub.w  %e0,%e0\;mov.w  %f1,%f0\";
392             }
393           return \"mov.w        %e1,%e0\;mov.w  %f1,%f0\";
394         }
395     case 3:
396       return \"mov.w    %e1,%e0\;mov.w  %f1,%f0\";
397     case 4:
398       return \"mov.w    %f1,%T0\;mov.w  %e1,%T0\";
399     case 5:
400       return \"mov.w    %T1,%e0\;mov.w  %T1,%f0\";
401     default:
402       abort();
403     }
404 }"
405   [(set_attr "length" "4,4,8,8,4,4")
406    (set_attr "cc" "clobber")])
407
408 (define_insn "movsf_h8300"
409   [(set (match_operand:SF 0 "general_operand_dst" "=r,r,r,o,<,r")
410         (match_operand:SF 1 "general_operand_src" "I,r,io,r,r,>"))]
411   "TARGET_H8300
412    && (register_operand (operands[0], SFmode)
413        || register_operand (operands[1], SFmode))"
414   "*
415 {
416   /* Copy of the movsi stuff.  */
417   int rn = -1;
418   switch (which_alternative)
419     {
420     case 0:
421       return \"sub.w    %e0,%e0\;sub.w  %f0,%f0\";
422     case 1:
423       if (REGNO (operands[0]) < REGNO (operands[1]))
424         return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
425       else
426         return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
427     case 2:
428       /* Make sure we don't trample the register we index with.  */
429       if (GET_CODE (operands[1]) == MEM)
430         {
431           rtx inside = XEXP (operands[1],0);
432           if (REG_P (inside))
433             {
434               rn = REGNO (inside);
435             }
436           else if (GET_CODE (inside) == PLUS)
437             {
438               rtx lhs = XEXP (inside,0);
439               rtx rhs = XEXP (inside,1);
440               if (REG_P (lhs)) rn = REGNO (lhs);
441               if (REG_P (rhs)) rn = REGNO (rhs);
442             }
443         }
444       if (rn == REGNO (operands[0]))
445         /* Move the second word first.  */
446         return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
447       else
448         /* Move the first word first.  */
449         return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
450
451     case 3:
452       return \"mov.w    %e1,%e0\;mov.w  %f1,%f0\";
453     case 4:
454       return \"mov.w    %f1,%T0\;mov.w  %e1,%T0\";
455     case 5:
456       return \"mov.w    %T1,%e0\;mov.w  %T1,%f0\";
457     default:
458       abort();
459     }
460 }"
461   [(set_attr "length" "4,4,8,8,4,4")
462    (set_attr "cc" "clobber")])
463
464 (define_insn "movsi_h8300hs"
465   [(set (match_operand:SI 0 "general_operand_dst" "=r,r,r,r,m,<,r,*a,*a,r")
466         (match_operand:SI 1 "general_operand_src" "I,r,i,m,r,r,>,I,r,*a"))]
467   "(TARGET_H8300S || TARGET_H8300H)
468    && (register_operand (operands[0], SImode)
469        || register_operand (operands[1], SImode))
470    && !(GET_CODE (operands[0]) == MEM
471         && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
472         && GET_CODE (XEXP (XEXP (operands[0], 0), 0)) == REG
473         && GET_CODE (operands[1]) == REG
474         && REGNO (XEXP (XEXP (operands[0], 0), 0)) == REGNO (operands[1]))"
475   "*
476 {
477   switch (which_alternative)
478     {
479     case 0:
480       return \"sub.l    %S0,%S0\";
481     case 7:
482       return \"clrmac\";
483     case 8:
484       return \"clrmac\;ldmac %1,macl\";
485     case 9:
486       return \"stmac    macl,%0\";
487     default:
488       if (GET_CODE (operands[1]) == CONST_INT)
489         {
490           int val = INTVAL (operands[1]);
491
492           /* Look for constants which can be made by adding an 8-bit
493              number to zero in one of the two low bytes.  */
494           if (val == (val & 0xff))
495             {
496               operands[1] = GEN_INT ((char) val & 0xff);
497               return \"sub.l\\t%S0,%S0\;add.b\\t%1,%w0\";
498             }
499
500           if (val == (val & 0xff00))
501             {
502               operands[1] = GEN_INT ((char) (val >> 8) & 0xff);
503               return \"sub.l\\t%S0,%S0\;add.b\\t%1,%x0\";
504             }
505
506           /* Look for constants that can be obtained by subs, inc, and
507              dec to 0.  */
508           switch (val & 0xffffffff)
509             {
510             case 0xffffffff:
511               return \"sub.l\\t%S0,%S0\;subs\\t#1,%S0\";
512             case 0xfffffffe:
513               return \"sub.l\\t%S0,%S0\;subs\\t#2,%S0\";
514             case 0xfffffffc:
515               return \"sub.l\\t%S0,%S0\;subs\\t#4,%S0\";
516
517             case 0x0000ffff:
518               return \"sub.l\\t%S0,%S0\;dec.w\\t#1,%f0\";
519             case 0x0000fffe:
520               return \"sub.l\\t%S0,%S0\;dec.w\\t#2,%f0\";
521
522             case 0xffff0000:
523               return \"sub.l\\t%S0,%S0\;dec.w\\t#1,%e0\";
524             case 0xfffe0000:
525               return \"sub.l\\t%S0,%S0\;dec.w\\t#2,%e0\";
526
527             case 0x00010000:
528               return \"sub.l\\t%S0,%S0\;inc.w\\t#1,%e0\";
529             case 0x00020000:
530               return \"sub.l\\t%S0,%S0\;inc.w\\t#2,%e0\";
531             }
532         }
533     }
534    return \"mov.l       %S1,%S0\";
535 }"
536   [(set_attr "length" "2,2,10,10,10,4,4,2,6,4")
537    (set_attr "cc" "set_zn,set_znv,clobber,set_znv,set_znv,set_znv,set_znv,none_0hit,none_0hit,set_znv")])
538
539 (define_insn "movsf_h8300h"
540   [(set (match_operand:SF 0 "general_operand_dst" "=r,r,r,m,<,r")
541         (match_operand:SF 1 "general_operand_src" "I,r,im,r,r,>"))]
542   "(TARGET_H8300H || TARGET_H8300S)
543    && (register_operand (operands[0], SFmode)
544        || register_operand (operands[1], SFmode))"
545   "@
546    sub.l        %S0,%S0
547    mov.l        %S1,%S0
548    mov.l        %S1,%S0
549    mov.l        %S1,%S0
550    mov.l        %S1,%S0
551    mov.l        %S1,%S0"
552   [(set_attr "length" "2,2,10,10,4,4")
553    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
554 \f
555 ;; ----------------------------------------------------------------------
556 ;; TEST INSTRUCTIONS
557 ;; ----------------------------------------------------------------------
558
559 (define_insn ""
560   [(set (cc0) (zero_extract:QI (match_operand:QI 0 "bit_memory_operand" "rU")
561                                (const_int 1)
562                                (match_operand:QI 1 "const_int_operand" "n")))]
563   ""
564   "btst %Z1,%R0"
565   [(set_attr "length" "2")
566    (set_attr "cc" "set_zn")])
567
568 (define_insn ""
569   [(set (cc0) (zero_extract:HI (match_operand:QI 0 "bit_memory_operand" "rU")
570                                (const_int 1)
571                                (match_operand:QI 1 "const_int_operand" "n")))]
572   ""
573   "btst %Z1,%Y0"
574   [(set_attr "length" "2")
575    (set_attr "cc" "set_zn")])
576
577 (define_insn ""
578   [(set (cc0) (zero_extract:SI (match_operand:QI 0 "bit_memory_operand" "rU")
579                                (const_int 1)
580                                (match_operand:QI 1 "const_int_operand" "n")))]
581   ""
582   "btst %Z1,%Y0"
583   [(set_attr "length" "2")
584    (set_attr "cc" "set_zn")])
585
586 (define_insn ""
587   [(set (cc0) (zero_extract:QI (match_operand:HI 0 "register_operand" "r")
588                                (const_int 1)
589                                (match_operand:HI 1 "const_int_operand" "n")))]
590   ""
591   "btst %Z1,%R0"
592   [(set_attr "length" "2")
593    (set_attr "cc" "set_zn")])
594
595 (define_insn ""
596   [(set (cc0) (zero_extract:HI (match_operand:HI 0 "register_operand" "r")
597                                (const_int 1)
598                                (match_operand:HI 1 "const_int_operand" "n")))]
599   ""
600   "btst %Z1,%Y0"
601   [(set_attr "length" "2")
602    (set_attr "cc" "set_zn")])
603
604 (define_insn ""
605   [(set (cc0) (zero_extract:SI (match_operand:HI 0 "register_operand" "r")
606                                (const_int 1)
607                                (match_operand:HI 1 "const_int_operand" "n")))]
608   ""
609   "btst %Z1,%Y0"
610   [(set_attr "length" "2")
611    (set_attr "cc" "set_zn")])
612
613 (define_insn "tstqi"
614   [(set (cc0) (match_operand:QI 0 "register_operand" "r"))]
615   ""
616   "mov.b        %X0,%X0"
617   [(set_attr "length" "2")
618    (set_attr "cc" "set_znv")])
619
620 (define_insn "tsthi"
621   [(set (cc0) (match_operand:HI 0 "register_operand" "r"))]
622   ""
623   "mov.w        %T0,%T0"
624   [(set_attr "length" "2")
625    (set_attr "cc" "set_znv")])
626
627 (define_insn "tstsi"
628   [(set (cc0) (match_operand:SI 0 "register_operand" "r"))]
629   "TARGET_H8300H || TARGET_H8300S"
630   "mov.l        %S0,%S0"
631   [(set_attr "length" "2")
632    (set_attr "cc" "set_znv")])
633
634 (define_insn "cmpqi"
635   [(set (cc0)
636         (compare:QI (match_operand:QI 0 "register_operand" "r")
637                     (match_operand:QI 1 "nonmemory_operand" "rn")))]
638   ""
639   "cmp.b        %X1,%X0"
640   [(set_attr "length" "2")
641    (set_attr "cc" "compare")])
642
643 (define_expand "cmphi"
644   [(set (cc0)
645         (compare:HI (match_operand:HI 0 "register_operand" "")
646                     (match_operand:HI 1 "nonmemory_operand" "")))]
647   ""
648   "
649 {
650   /* Force operand1 into a register if we're compiling
651      for the H8/300.  */
652   if (GET_CODE (operands[1]) != REG && TARGET_H8300)
653     operands[1] = force_reg (HImode, operands[1]);
654 }")
655
656 (define_insn ""
657   [(set (cc0)
658         (compare:HI (match_operand:HI 0 "register_operand" "r")
659                     (match_operand:HI 1 "register_operand" "r")))]
660   "TARGET_H8300"
661   "cmp.w        %T1,%T0"
662   [(set_attr "length" "2")
663    (set_attr "cc" "compare")])
664
665 (define_insn ""
666   [(set (cc0)
667         (compare:HI (match_operand:HI 0 "register_operand" "r,r")
668                     (match_operand:HI 1 "nonmemory_operand" "r,n")))]
669   "TARGET_H8300H || TARGET_H8300S"
670   "cmp.w        %T1,%T0"
671   [(set_attr "length" "2,4")
672    (set_attr "cc" "compare,compare")])
673
674 (define_insn "cmpsi"
675   [(set (cc0)
676         (compare:SI (match_operand:SI 0 "register_operand" "r,r")
677                     (match_operand:SI 1 "nonmemory_operand" "r,i")))]
678   "TARGET_H8300H || TARGET_H8300S"
679   "cmp.l        %S1,%S0"
680   [(set_attr "length" "2,6")
681    (set_attr "cc" "compare,compare")])
682 \f
683 ;; ----------------------------------------------------------------------
684 ;; ADD INSTRUCTIONS
685 ;; ----------------------------------------------------------------------
686
687 (define_insn "addqi3"
688   [(set (match_operand:QI 0 "register_operand" "=r")
689         (plus:QI (match_operand:QI 1 "register_operand" "%0")
690                  (match_operand:QI 2 "nonmemory_operand" "rn")))]
691   ""
692   "add.b        %X2,%X0"
693   [(set_attr "length" "2")
694    (set_attr "cc" "set_zn")])
695
696 (define_expand "addhi3"
697   [(set (match_operand:HI 0 "register_operand" "")
698         (plus:HI (match_operand:HI 1 "register_operand" "")
699                  (match_operand:HI 2 "nonmemory_operand" "")))]
700   ""
701   "")
702
703 (define_insn ""
704   [(set (match_operand:HI 0 "register_operand" "=r,r,r,r,&r")
705         (plus:HI (match_operand:HI 1 "register_operand" "%0,0,0,0,g")
706                  (match_operand:HI 2 "nonmemory_operand" "L,N,n,r,r")))]
707   "TARGET_H8300"
708   "@
709    adds %2,%A0
710    subs %G2,%A0
711    add.b        %s2,%s0\;addx   %t2,%t0
712    add.w        %T2,%T0
713    mov.w        %T1,%T0\;add.w  %T2,%T0"
714   [(set_attr "length" "2,2,4,2,6")
715    (set_attr "cc" "none_0hit,none_0hit,clobber,set_zn,set_zn")])
716
717 (define_insn ""
718   [(set (match_operand:HI 0 "register_operand" "=r,r,r,r")
719         (plus:HI (match_operand:HI 1 "register_operand" "%0,0,0,0")
720                  (match_operand:HI 2 "nonmemory_operand" "L,N,n,r")))]
721   "TARGET_H8300H || TARGET_H8300S"
722   "@
723    adds %2,%A0
724    subs %G2,%A0
725    add.w        %T2,%T0
726    add.w        %T2,%T0"
727   [(set_attr "length" "2,2,4,2")
728    (set_attr "cc" "none_0hit,none_0hit,set_zn,set_zn")])
729
730 (define_split
731   [(set (match_operand:HI 0 "register_operand" "")
732         (plus:HI (match_dup 0)
733                  (match_operand:HI 1 "two_insn_adds_subs_operand" "")))]
734   ""
735   [(const_int 0)]
736   "split_adds_subs (HImode, operands); DONE;")
737
738 (define_expand "addsi3"
739   [(set (match_operand:SI 0 "register_operand" "")
740         (plus:SI (match_operand:SI 1 "register_operand" "")
741                  (match_operand:SI 2 "nonmemory_operand" "")))]
742   ""
743   "")
744
745 (define_insn "addsi_h8300"
746   [(set (match_operand:SI 0 "register_operand" "=r,r,&r")
747         (plus:SI (match_operand:SI 1 "register_operand" "%0,0,r")
748                  (match_operand:SI 2 "nonmemory_operand" "n,r,r")))]
749   "TARGET_H8300"
750   "@
751    add  %w2,%w0\;addx   %x2,%x0\;addx   %y2,%y0\;addx   %z2,%z0
752    add.w        %f2,%f0\;addx   %y2,%y0\;addx   %z2,%z0
753    mov.w        %f1,%f0\;mov.w  %e1,%e0\;add.w  %f2,%f0\;addx   %y2,%y0\;addx   %z2,%z0"
754   [(set_attr "length" "8,6,10")
755    (set_attr "cc" "clobber")])
756
757 (define_insn "addsi_h8300h"
758   [(set (match_operand:SI 0 "register_operand" "=r,r,r,r")
759         (plus:SI (match_operand:SI 1 "register_operand" "%0,0,0,0")
760                  (match_operand:SI 2 "nonmemory_operand" "L,N,i,r")))]
761   "TARGET_H8300H || TARGET_H8300S"
762   "@
763    adds %2,%A0
764    subs %G2,%A0
765    add.l        %S2,%S0
766    add.l        %S2,%S0"
767   [(set_attr "length" "2,2,6,2")
768    (set_attr "cc" "none_0hit,none_0hit,set_zn,set_zn")])
769
770 (define_split
771   [(set (match_operand:SI 0 "register_operand" "")
772         (plus:SI (match_dup 0)
773                  (match_operand:SI 1 "two_insn_adds_subs_operand" "")))]
774   "TARGET_H8300H || TARGET_H8300S"
775   [(const_int 0)]
776   "split_adds_subs (SImode, operands); DONE;")
777
778 ;; ----------------------------------------------------------------------
779 ;; SUBTRACT INSTRUCTIONS
780 ;; ----------------------------------------------------------------------
781
782 (define_insn "subqi3"
783   [(set (match_operand:QI 0 "register_operand" "=r,r")
784         (minus:QI (match_operand:QI 1 "register_operand" "0,0")
785                   (match_operand:QI 2 "nonmemory_operand" "r,n")))]
786   ""
787   "@
788    sub.b        %X2,%X0
789    add.b        %G2,%X0"
790   [(set_attr "length" "2")
791    (set_attr "cc" "set_zn")])
792
793 (define_expand "subhi3"
794   [(set (match_operand:HI 0 "register_operand" "")
795         (minus:HI (match_operand:HI 1 "general_operand" "")
796                   (match_operand:HI 2 "nonmemory_operand" "")))]
797   ""
798   "")
799
800 (define_insn ""
801   [(set (match_operand:HI 0 "register_operand" "=r,&r")
802         (minus:HI (match_operand:HI 1 "general_operand" "0,0")
803                   (match_operand:HI 2 "nonmemory_operand" "r,n")))]
804   "TARGET_H8300"
805   "@
806    sub.w        %T2,%T0
807    add.b        %E2,%s0\;addx   %F2,%t0"
808   [(set_attr "length" "2,4")
809    (set_attr "cc" "set_zn,clobber")])
810
811 (define_insn ""
812   [(set (match_operand:HI 0 "register_operand" "=r,&r")
813         (minus:HI (match_operand:HI 1 "general_operand" "0,0")
814                   (match_operand:HI 2 "nonmemory_operand" "r,n")))]
815   "TARGET_H8300H || TARGET_H8300S"
816   "@
817    sub.w        %T2,%T0
818    sub.w        %T2,%T0"
819   [(set_attr "length" "2,4")
820    (set_attr "cc" "set_zn,set_zn")])
821
822 (define_expand "subsi3"
823   [(set (match_operand:SI 0 "register_operand" "")
824         (minus:SI (match_operand:SI 1 "register_operand" "")
825                   (match_operand:SI 2 "nonmemory_operand" "")))]
826   ""
827   "")
828
829 (define_insn "subsi3_h8300"
830   [(set (match_operand:SI 0 "register_operand" "=r")
831         (minus:SI (match_operand:SI 1 "register_operand" "0")
832                   (match_operand:SI 2 "register_operand" "r")))]
833   "TARGET_H8300"
834   "sub.w        %f2,%f0\;subx   %y2,%y0\;subx   %z2,%z0"
835   [(set_attr "length" "6")
836    (set_attr "cc" "clobber")])
837
838 (define_insn "subsi3_h8300h"
839   [(set (match_operand:SI 0 "register_operand" "=r,r")
840         (minus:SI (match_operand:SI 1 "general_operand" "0,0")
841                   (match_operand:SI 2 "nonmemory_operand" "r,i")))]
842   "TARGET_H8300H || TARGET_H8300S"
843   "@
844    sub.l        %S2,%S0
845    sub.l        %S2,%S0"
846   [(set_attr "length" "2,6")
847    (set_attr "cc" "set_zn,set_zn")])
848 \f
849 ;; ----------------------------------------------------------------------
850 ;; MULTIPLY INSTRUCTIONS
851 ;; ----------------------------------------------------------------------
852
853 ;; Note that the H8/300 can only handle umulqihi3.
854
855 (define_insn "mulqihi3"
856   [(set (match_operand:HI 0 "register_operand" "=r")
857         (mult:HI (sign_extend:HI (match_operand:QI 1 "general_operand" "%0"))
858                  (sign_extend:HI (match_operand:QI 2 "register_operand" "r"))))]
859   "TARGET_H8300H || TARGET_H8300S"
860   "mulxs.b      %X2,%T0"
861   [(set_attr "length" "4")
862    (set_attr "cc" "set_zn")])
863
864 (define_insn "mulhisi3"
865   [(set (match_operand:SI 0 "register_operand" "=r")
866         (mult:SI (sign_extend:SI (match_operand:HI 1 "general_operand" "%0"))
867                  (sign_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
868   "TARGET_H8300H || TARGET_H8300S"
869   "mulxs.w      %T2,%S0"
870   [(set_attr "length" "4")
871    (set_attr "cc" "set_zn")])
872
873 (define_insn "umulqihi3"
874   [(set (match_operand:HI 0 "register_operand" "=r")
875         (mult:HI (zero_extend:HI (match_operand:QI 1 "general_operand" "%0"))
876                  (zero_extend:HI (match_operand:QI 2 "register_operand" "r"))))]
877   ""
878   "mulxu        %X2,%T0"
879   [(set_attr "length" "2")
880    (set_attr "cc" "none_0hit")])
881
882 (define_insn "umulhisi3"
883   [(set (match_operand:SI 0 "register_operand" "=r")
884         (mult:SI (zero_extend:SI (match_operand:HI 1 "general_operand" "%0"))
885                  (zero_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
886   "TARGET_H8300H || TARGET_H8300S"
887   "mulxu.w      %T2,%S0"
888   [(set_attr "length" "2")
889    (set_attr "cc" "none_0hit")])
890
891 ;; This is a "bridge" instruction.  Combine can't cram enough insns
892 ;; together to crate a MAC instruction directly, but it can create
893 ;; this instruction, which then allows combine to create the real
894 ;; MAC insn.
895 ;;
896 ;; Unfortunately, if combine doesn't create a MAC instruction, this
897 ;; insn must generate reasonably correct code.  Egad.
898 (define_insn ""
899   [(set (match_operand:SI 0 "register_operand" "=a")
900         (mult:SI
901           (sign_extend:SI
902             (mem:HI (post_inc:SI (match_operand:SI 1 "register_operand" "r"))))
903           (sign_extend:SI
904             (mem:HI (post_inc:SI (match_operand:SI 2 "register_operand" "r"))))))]
905   "TARGET_MAC"
906   "clrmac\;mac  @%2+,@%1+"
907   [(set_attr "length" "6")
908    (set_attr "cc" "none_0hit")])
909
910 (define_insn ""
911   [(set (match_operand:SI 0 "register_operand" "=a")
912         (plus (mult:SI
913           (sign_extend:SI (mem:HI
914             (post_inc:SI (match_operand:SI 1 "register_operand" "r"))))
915           (sign_extend:SI (mem:HI
916             (post_inc:SI (match_operand:SI 2 "register_operand" "r")))))
917               (match_operand:SI 3 "register_operand" "0")))]
918   "TARGET_MAC"
919   "mac  @%2+,@%1+"
920   [(set_attr "length" "4")
921    (set_attr "cc" "none_0hit")])
922
923 ;; ----------------------------------------------------------------------
924 ;; DIVIDE INSTRUCTIONS
925 ;; ----------------------------------------------------------------------
926
927 (define_insn "udivqi3"
928   [(set (match_operand:QI 0 "register_operand" "=r")
929         (truncate:QI
930           (udiv:HI
931             (match_operand:HI 1 "general_operand" "0")
932             (zero_extend:HI (match_operand:QI 2 "register_operand" "r")))))]
933   ""
934   "divxu        %X2,%T0"
935   [(set_attr "length" "2")
936    (set_attr "cc" "clobber")])
937
938 ;; ??? Will divxu always work here?
939
940 (define_insn "divqi3"
941   [(set (match_operand:QI 0 "register_operand" "=r")
942         (truncate:QI
943           (div:HI
944             (match_operand:HI 1 "general_operand" "0")
945             (sign_extend:HI (match_operand:QI 2 "register_operand" "r")))))]
946   ""
947   "divxu        %X2,%T0"
948   [(set_attr "length" "2")
949    (set_attr "cc" "clobber")])
950
951 (define_insn "udivhi3"
952   [(set (match_operand:HI 0 "register_operand" "=r")
953         (truncate:HI
954           (udiv:SI
955             (match_operand:SI 1 "general_operand" "0")
956             (zero_extend:SI (match_operand:HI 2 "register_operand" "r")))))]
957   "TARGET_H8300H || TARGET_H8300S"
958   "divxu.w      %T2,%S0"
959   [(set_attr "length" "2")
960    (set_attr "cc" "clobber")])
961
962 (define_insn "divhi3"
963   [(set (match_operand:HI 0 "register_operand" "=r")
964         (truncate:HI
965           (div:SI
966             (match_operand:SI 1 "general_operand" "0")
967             (sign_extend:SI (match_operand:HI 2 "register_operand" "r")))))]
968   "TARGET_H8300H || TARGET_H8300S"
969   "divxs.w      %T2,%S0"
970   [(set_attr "length" "4")
971    (set_attr "cc" "clobber")])
972
973 ;; ----------------------------------------------------------------------
974 ;; MOD INSTRUCTIONS
975 ;; ----------------------------------------------------------------------
976
977 (define_insn "umodqi3"
978   [(set (match_operand:QI 0 "register_operand" "=r")
979         (truncate:QI
980           (umod:HI
981             (match_operand:HI 1 "general_operand" "0")
982             (zero_extend:HI (match_operand:QI 2 "register_operand" "r")))))]
983   ""
984   "divxu        %X2,%T0\;mov    %t0,%s0"
985   [(set_attr "length" "4")
986    (set_attr "cc" "clobber")])
987
988 (define_insn "modqi3"
989   [(set (match_operand:QI 0 "register_operand" "=r")
990         (truncate:QI
991           (mod:HI
992             (match_operand:HI 1 "general_operand" "0")
993             (sign_extend:HI (match_operand:QI 2 "register_operand" "r")))))]
994   "TARGET_H8300H || TARGET_H8300S"
995   "divxs.b      %X2,%T0\;mov    %t0,%s0"
996   [(set_attr "length" "6")
997    (set_attr "cc" "clobber")])
998
999 (define_insn "umodhi3"
1000   [(set (match_operand:HI 0 "register_operand" "=r")
1001         (truncate:HI
1002           (umod:SI
1003             (match_operand:SI 1 "general_operand" "0")
1004             (zero_extend:SI (match_operand:HI 2 "register_operand" "r")))))]
1005   "TARGET_H8300H || TARGET_H8300S"
1006   "divxu.w      %T2,%S0\;mov    %e0,%f0"
1007   [(set_attr "length" "4")
1008    (set_attr "cc" "clobber")])
1009
1010 (define_insn "modhi3"
1011   [(set (match_operand:HI 0 "register_operand" "=r")
1012         (truncate:HI
1013           (mod:SI
1014             (match_operand:SI 1 "general_operand" "0")
1015             (sign_extend:SI (match_operand:HI 2 "register_operand" "r")))))]
1016   "TARGET_H8300H || TARGET_H8300S"
1017   "divxs.w      %T2,%S0\;mov    %e0,%f0"
1018   [(set_attr "length" "6")
1019    (set_attr "cc" "clobber")])
1020 \f
1021 ;; ----------------------------------------------------------------------
1022 ;; AND INSTRUCTIONS
1023 ;; ----------------------------------------------------------------------
1024
1025 (define_insn ""
1026   [(set (match_operand:QI 0 "bit_operand" "=r,U")
1027         (and:QI (match_operand:QI 1 "bit_operand" "%0,0")
1028                 (match_operand:QI 2 "nonmemory_operand" "rn,O")))]
1029   "register_operand (operands[0], QImode) || o_operand (operands[2], QImode)"
1030   "@
1031    and  %X2,%X0
1032    bclr %W2,%R0"
1033   [(set_attr "length" "2,4")
1034    (set_attr "adjust_length" "no")
1035    (set_attr "cc" "set_znv,none_0hit")])
1036
1037 (define_expand "andqi3"
1038   [(set (match_operand:QI 0 "bit_operand" "")
1039         (and:QI (match_operand:QI 1 "bit_operand" "")
1040                 (match_operand:QI 2 "nonmemory_operand" "")))]
1041   ""
1042   "
1043 {
1044   if (fix_bit_operand (operands, 'O', AND))
1045     DONE;
1046 }")
1047
1048 (define_expand "andhi3"
1049   [(set (match_operand:HI 0 "register_operand" "")
1050         (and:HI (match_operand:HI 1 "register_operand" "")
1051                 (match_operand:HI 2 "nonmemory_operand" "")))]
1052   ""
1053   "")
1054
1055 (define_insn ""
1056   [(set (match_operand:HI 0 "register_operand" "=r")
1057         (and:HI (match_operand:HI 1 "register_operand" "%0")
1058                 (match_operand:HI 2 "nonmemory_operand" "rn")))]
1059   "TARGET_H8300"
1060   "* return output_logical_op (HImode, AND, operands);"
1061   [(set_attr "length" "4")
1062    (set_attr "cc" "clobber")])
1063
1064 (define_insn ""
1065   [(set (match_operand:HI 0 "register_operand" "=r,r")
1066         (and:HI (match_operand:HI 1 "register_operand" "%0,0")
1067                 (match_operand:HI 2 "nonmemory_operand" "r,n")))]
1068   "TARGET_H8300H || TARGET_H8300S"
1069   "* return output_logical_op (HImode, AND, operands);"
1070   [(set_attr "length" "2,4")
1071    (set_attr "cc" "set_znv,clobber")])
1072
1073 (define_insn "*andorhi3"
1074   [(set (match_operand:HI 0 "register_operand" "=r")
1075         (ior:HI (and:HI (match_operand:HI 2 "register_operand" "r")
1076                         (match_operand:HI 3 "p_operand" "P"))
1077         (match_operand:HI 1 "register_operand" "0")))]
1078   ""
1079   "*
1080 {
1081   if (INTVAL (operands[3]) > 128)
1082     {
1083       operands[3] = GEN_INT (INTVAL (operands[3]) >> 8);
1084       return \"bld\\t%V3,%t2\;bst\\t%V3,%t0\";
1085     }
1086   return \"bld\\t%V3,%s2\;bst\\t%V3,%s0\";
1087 }"
1088   [(set_attr "length" "4")
1089    (set_attr "cc" "clobber")])
1090
1091 (define_expand "andsi3"
1092   [(set (match_operand:SI 0 "register_operand" "")
1093         (and:SI (match_operand:SI 1 "register_operand" "")
1094                 (match_operand:SI 2 "nonmemory_operand" "")))]
1095   ""
1096   "")
1097
1098 (define_insn ""
1099   [(set (match_operand:SI 0 "register_operand" "=r")
1100         (and:SI (match_operand:SI 1 "register_operand" "%0")
1101                 (match_operand:SI 2 "nonmemory_operand" "rn")))]
1102   "TARGET_H8300"
1103   "* return output_logical_op (SImode, AND, operands);"
1104   [(set_attr "length" "8")
1105    (set_attr "cc" "clobber")])
1106
1107 (define_insn ""
1108   [(set (match_operand:SI 0 "register_operand" "=r,r")
1109         (and:SI (match_operand:SI 1 "register_operand" "%0,0")
1110                 (match_operand:SI 2 "nonmemory_operand" "r,n")))]
1111   "TARGET_H8300H || TARGET_H8300S"
1112   "* return output_logical_op (SImode, AND, operands);"
1113   [(set_attr "length" "4,6")
1114    (set_attr "cc" "set_znv,clobber")])
1115
1116 ;; ----------------------------------------------------------------------
1117 ;; OR INSTRUCTIONS
1118 ;; ----------------------------------------------------------------------
1119
1120 (define_insn ""
1121   [(set (match_operand:QI 0 "bit_operand" "=r,U")
1122         (ior:QI (match_operand:QI 1 "bit_operand" "%0,0")
1123                 (match_operand:QI 2 "nonmemory_operand" "rn,P")))]
1124   "register_operand (operands[0], QImode) || p_operand (operands[2], QImode)"
1125   "@
1126    or   %X2,%X0
1127    bset %V2,%R0"
1128   [(set_attr "length" "2,4")
1129    (set_attr "adjust_length" "no")
1130    (set_attr "cc" "set_znv,none_0hit")])
1131
1132 (define_expand "iorqi3"
1133   [(set (match_operand:QI 0 "bit_operand" "=r,U")
1134         (ior:QI (match_operand:QI 1 "bit_operand" "%0,0")
1135                 (match_operand:QI 2 "nonmemory_operand" "rn,P")))]
1136   ""
1137   "
1138 {
1139   if (fix_bit_operand (operands, 'P', IOR))
1140     DONE;
1141 }")
1142
1143 (define_expand "iorhi3"
1144   [(set (match_operand:HI 0 "general_operand" "=r,r")
1145         (ior:HI (match_operand:HI 1 "general_operand" "%0,0")
1146                 (match_operand:HI 2 "general_operand" "J,rn")))]
1147   ""
1148   "")
1149
1150 (define_insn ""
1151   [(set (match_operand:HI 0 "general_operand" "=r,r")
1152         (ior:HI (match_operand:HI 1 "general_operand" "%0,0")
1153                 (match_operand:HI 2 "general_operand" "J,rn")))]
1154   "TARGET_H8300"
1155   "* return output_logical_op (HImode, IOR, operands);"
1156   [(set_attr "length" "2,4")
1157    (set_attr "cc" "clobber,clobber")])
1158
1159 (define_insn ""
1160   [(set (match_operand:HI 0 "general_operand" "=r,r,r")
1161         (ior:HI (match_operand:HI 1 "general_operand" "%0,0,0")
1162                 (match_operand:HI 2 "general_operand" "J,r,n")))]
1163   "TARGET_H8300H || TARGET_H8300S"
1164   "* return output_logical_op (HImode, IOR, operands);"
1165   [(set_attr "length" "2,2,4")
1166    (set_attr "cc" "clobber,set_znv,clobber")])
1167
1168 (define_expand "iorsi3"
1169   [(set (match_operand:SI 0 "register_operand" "")
1170         (ior:SI (match_operand:SI 1 "register_operand" "")
1171                 (match_operand:SI 2 "nonmemory_operand" "")))]
1172   ""
1173   "")
1174
1175 (define_insn ""
1176   [(set (match_operand:SI 0 "register_operand" "=r,r")
1177         (ior:SI (match_operand:SI 1 "register_operand" "%0,0")
1178                 (match_operand:SI 2 "nonmemory_operand" "J,rn")))]
1179   "TARGET_H8300"
1180   "* return output_logical_op (SImode, IOR, operands);"
1181   [(set_attr "length" "2,8")
1182    (set_attr "cc" "clobber,clobber")])
1183
1184 (define_insn ""
1185   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
1186         (ior:SI (match_operand:SI 1 "register_operand" "%0,0,0")
1187                 (match_operand:SI 2 "nonmemory_operand" "J,r,n")))]
1188   "TARGET_H8300H || TARGET_H8300S"
1189   "* return output_logical_op (SImode, IOR, operands);"
1190   [(set_attr "length" "2,4,6")
1191    (set_attr "cc" "clobber,set_znv,clobber")])
1192
1193 ;; ----------------------------------------------------------------------
1194 ;; XOR INSTRUCTIONS
1195 ;; ----------------------------------------------------------------------
1196
1197 (define_insn ""
1198   [(set (match_operand:QI 0 "bit_operand" "=r,U")
1199         (xor:QI (match_operand:QI 1 "bit_operand" "%0,0")
1200                 (match_operand:QI 2 "nonmemory_operand" "rn,P")))]
1201   "register_operand (operands[0], QImode) || p_operand (operands[2], QImode)"
1202   "@
1203    xor  %X2,%X0
1204    bnot %V2,%R0"
1205   [(set_attr "length" "2,4")
1206    (set_attr "adjust_length" "no")
1207    (set_attr "cc" "set_znv,none_0hit")])
1208
1209 (define_expand "xorqi3"
1210   [(set (match_operand:QI 0 "bit_operand" "=r,U")
1211         (xor:QI (match_operand:QI 1 "bit_operand" "%0,0")
1212                 (match_operand:QI 2 "nonmemory_operand" "rn,O")))]
1213   ""
1214   "
1215 {
1216   if (fix_bit_operand (operands, 'O', XOR))
1217     DONE;
1218 }")
1219
1220 (define_expand "xorhi3"
1221   [(set (match_operand:HI 0 "register_operand" "")
1222         (xor:HI (match_operand:HI 1 "general_operand" "")
1223                 (match_operand:HI 2 "nonmemory_operand" "")))]
1224   ""
1225   "")
1226
1227 (define_insn ""
1228   [(set (match_operand:HI 0 "register_operand" "=r,r")
1229         (xor:HI (match_operand:HI 1 "general_operand" "%0,0")
1230                 (match_operand:HI 2 "nonmemory_operand" "J,rn")))]
1231   "TARGET_H8300"
1232   "* return output_logical_op (HImode, XOR, operands);"
1233   [(set_attr "length" "2,4")
1234    (set_attr "cc" "clobber,clobber")])
1235
1236 (define_insn ""
1237   [(set (match_operand:HI 0 "register_operand" "=r,r,r")
1238         (xor:HI (match_operand:HI 1 "general_operand" "%0,0,0")
1239                 (match_operand:HI 2 "nonmemory_operand" "J,r,n")))]
1240   "TARGET_H8300H || TARGET_H8300S"
1241   "* return output_logical_op (HImode, XOR, operands);"
1242   [(set_attr "length" "2,2,4")
1243    (set_attr "cc" "clobber,set_znv,clobber")])
1244
1245 (define_expand "xorsi3"
1246   [(set (match_operand:SI 0 "register_operand" "")
1247         (xor:SI (match_operand:SI 1 "register_operand" "")
1248                 (match_operand:SI 2 "nonmemory_operand" "")))]
1249   ""
1250   "")
1251
1252 (define_insn ""
1253   [(set (match_operand:SI 0 "register_operand" "=r,r")
1254         (xor:SI (match_operand:SI 1 "register_operand" "%0,0")
1255                 (match_operand:SI 2 "nonmemory_operand" "J,rn")))]
1256   "TARGET_H8300"
1257   "* return output_logical_op (SImode, XOR, operands);"
1258   [(set_attr "length" "2,8")
1259    (set_attr "cc" "clobber,clobber")])
1260
1261 (define_insn ""
1262   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
1263         (xor:SI (match_operand:SI 1 "register_operand" "%0,0,0")
1264                 (match_operand:SI 2 "nonmemory_operand" "J,r,n")))]
1265   "TARGET_H8300H || TARGET_H8300S"
1266   "* return output_logical_op (SImode, XOR, operands);"
1267   [(set_attr "length" "2,4,6")
1268    (set_attr "cc" "clobber,set_znv,clobber")])
1269 \f
1270 ;; ----------------------------------------------------------------------
1271 ;; NEGATION INSTRUCTIONS
1272 ;; ----------------------------------------------------------------------
1273
1274 (define_insn "negqi2"
1275   [(set (match_operand:QI 0 "register_operand" "=r")
1276         (neg:QI (match_operand:QI 1 "general_operand" "0")))]
1277   ""
1278   "neg  %X0"
1279   [(set_attr "length" "2")
1280    (set_attr "cc" "set_zn")])
1281
1282 (define_expand "neghi2"
1283   [(set (match_operand:HI 0 "register_operand" "=r")
1284         (neg:HI (match_operand:HI 1 "general_operand" "0")))]
1285   ""
1286   "
1287 {
1288   if (TARGET_H8300)
1289     {
1290       emit_insn (gen_neghi2_h8300 (operands[0], operands[1]));
1291       DONE;
1292     }
1293 }")
1294
1295 (define_expand "neghi2_h8300"
1296   [(set (match_dup 2)
1297         (not:HI (match_operand:HI 1 "register_operand" "r")))
1298    (set (match_dup 2) (plus:HI (match_dup 2) (const_int 1)))
1299    (set (match_operand:HI 0 "register_operand" "=r")
1300         (match_dup 2))]
1301   ""
1302   "{ operands[2] = gen_reg_rtx (HImode); }")
1303
1304 (define_insn "neghi2_h8300h"
1305   [(set (match_operand:HI 0 "register_operand" "=r")
1306         (neg:HI (match_operand:HI 1 "general_operand" "0")))]
1307   "TARGET_H8300H || TARGET_H8300S"
1308   "neg  %T0"
1309   [(set_attr "length" "2")
1310    (set_attr "cc" "set_zn")])
1311
1312 (define_expand "negsi2"
1313   [(set (match_operand:SI 0 "register_operand" "=r")
1314         (neg:SI (match_operand:SI 1 "general_operand" "0")))]
1315   ""
1316   "
1317 {
1318   if (TARGET_H8300)
1319     {
1320       emit_insn (gen_negsi2_h8300 (operands[0], operands[1]));
1321       DONE;
1322     }
1323 }")
1324
1325 (define_expand "negsi2_h8300"
1326   [(set (match_dup 2)
1327         (not:SI (match_operand:SI 1 "register_operand" "r")))
1328    (set (match_dup 2) (plus:SI (match_dup 2) (const_int 1)))
1329    (set (match_operand:SI 0 "register_operand" "=r")
1330         (match_dup 2))]
1331   ""
1332   "{ operands[2] = gen_reg_rtx(SImode); }")
1333
1334 (define_insn "negsi2_h8300h"
1335   [(set (match_operand:SI 0 "register_operand" "=r")
1336         (neg:SI (match_operand:SI 1 "general_operand" "0")))]
1337   "TARGET_H8300H || TARGET_H8300S"
1338   "neg  %S0"
1339   [(set_attr "length" "2")
1340    (set_attr "cc" "set_zn")])
1341
1342 ;; ----------------------------------------------------------------------
1343 ;; NOT INSTRUCTIONS
1344 ;; ----------------------------------------------------------------------
1345
1346 (define_insn "one_cmplqi2"
1347   [(set (match_operand:QI 0 "register_operand" "=r")
1348         (not:QI (match_operand:QI 1 "general_operand" "0")))]
1349   ""
1350   "not  %X0"
1351   [(set_attr "length" "2")
1352    (set_attr "cc" "set_znv")])
1353
1354 (define_insn "one_cmplhi2"
1355   [(set (match_operand:HI 0 "register_operand" "=r")
1356         (not:HI (match_operand:HI 1 "general_operand" "0")))]
1357   ""
1358   "*
1359 {
1360   if (TARGET_H8300)
1361     return \"not        %s0\;not        %t0\";
1362   else
1363     return \"not        %T0\";
1364 }"
1365   [(set_attr "cc" "clobber")
1366    (set (attr "length")
1367         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
1368                           (const_int 0))
1369                       (const_int 4)
1370                       (const_int 2)))])
1371
1372 (define_insn "one_cmplsi2"
1373   [(set (match_operand:SI 0 "register_operand" "=r")
1374         (not:SI (match_operand:SI 1 "general_operand" "0")))]
1375   ""
1376   "*
1377 {
1378   if (TARGET_H8300)
1379     return \"not        %w0\;not        %x0\;not        %y0\;not        %z0\";
1380   else
1381     return \"not        %S0\";
1382 }"
1383   [(set_attr "cc" "clobber")
1384    (set (attr "length")
1385         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
1386                           (const_int 0))
1387                       (const_int 8)
1388                       (const_int 2)))])
1389 \f
1390 ;; ----------------------------------------------------------------------
1391 ;; JUMP INSTRUCTIONS
1392 ;; ----------------------------------------------------------------------
1393
1394 ;; Conditional jump instructions
1395
1396 (define_expand "ble"
1397   [(set (pc)
1398         (if_then_else (le (cc0)
1399                           (const_int 0))
1400                       (label_ref (match_operand 0 "" ""))
1401                       (pc)))]
1402   ""
1403   "")
1404
1405 (define_expand "bleu"
1406   [(set (pc)
1407         (if_then_else (leu (cc0)
1408                            (const_int 0))
1409                       (label_ref (match_operand 0 "" ""))
1410                       (pc)))]
1411   ""
1412   "")
1413
1414 (define_expand "bge"
1415   [(set (pc)
1416         (if_then_else (ge (cc0)
1417                           (const_int 0))
1418                       (label_ref (match_operand 0 "" ""))
1419                       (pc)))]
1420   ""
1421   "")
1422
1423 (define_expand "bgeu"
1424   [(set (pc)
1425         (if_then_else (geu (cc0)
1426                            (const_int 0))
1427                       (label_ref (match_operand 0 "" ""))
1428                       (pc)))]
1429   ""
1430   "")
1431
1432 (define_expand "blt"
1433   [(set (pc)
1434         (if_then_else (lt (cc0)
1435                           (const_int 0))
1436                       (label_ref (match_operand 0 "" ""))
1437                       (pc)))]
1438   ""
1439   "")
1440
1441 (define_expand "bltu"
1442   [(set (pc)
1443         (if_then_else (ltu (cc0)
1444                            (const_int 0))
1445                       (label_ref (match_operand 0 "" ""))
1446                       (pc)))]
1447   ""
1448   "")
1449
1450 (define_expand "bgt"
1451   [(set (pc)
1452         (if_then_else (gt (cc0)
1453                           (const_int 0))
1454                       (label_ref (match_operand 0 "" ""))
1455                       (pc)))]
1456   ""
1457   "")
1458
1459 (define_expand "bgtu"
1460   [(set (pc)
1461         (if_then_else (gtu (cc0)
1462                            (const_int 0))
1463                       (label_ref (match_operand 0 "" ""))
1464                       (pc)))]
1465   ""
1466   "")
1467
1468 (define_expand "beq"
1469   [(set (pc)
1470         (if_then_else (eq (cc0)
1471                           (const_int 0))
1472                       (label_ref (match_operand 0 "" ""))
1473                       (pc)))]
1474   ""
1475   "")
1476
1477 (define_expand "bne"
1478   [(set (pc)
1479         (if_then_else (ne (cc0)
1480                           (const_int 0))
1481                       (label_ref (match_operand 0 "" ""))
1482                       (pc)))]
1483   ""
1484   "")
1485
1486 (define_insn "branch_true"
1487   [(set (pc)
1488         (if_then_else (match_operator 1 "comparison_operator"
1489                                       [(cc0) (const_int 0)])
1490                       (label_ref (match_operand 0 "" ""))
1491                       (pc)))]
1492   ""
1493   "*
1494 {
1495   if ((cc_status.flags & CC_OVERFLOW_UNUSABLE) != 0
1496       && (GET_CODE (operands[1]) == GT
1497           || GET_CODE (operands[1]) == GE
1498           || GET_CODE (operands[1]) == LE
1499           || GET_CODE (operands[1]) == LT))
1500     {
1501       cc_status.flags &= ~CC_OVERFLOW_UNUSABLE;
1502       return 0;
1503     }
1504
1505   if (get_attr_length (insn) == 2)
1506     return \"b%j1       %l0\";
1507   else if (get_attr_length (insn) == 4)
1508     return \"b%j1       %l0:16\";
1509   else
1510     return \"b%k1       .Lh8BR%=\;jmp   @%l0\\n.Lh8BR%=:\";
1511 }"
1512  [(set_attr "type" "branch")
1513    (set_attr "cc" "none")])
1514
1515 (define_insn "branch_false"
1516   [(set (pc)
1517         (if_then_else (match_operator 1 "comparison_operator"
1518                                       [(cc0) (const_int 0)])
1519                       (pc)
1520                       (label_ref (match_operand 0 "" ""))))]
1521   ""
1522   "*
1523 {
1524   if ((cc_status.flags & CC_OVERFLOW_UNUSABLE) != 0
1525       && (GET_CODE (operands[1]) == GT
1526           || GET_CODE (operands[1]) == GE
1527           || GET_CODE (operands[1]) == LE
1528           || GET_CODE (operands[1]) == LT))
1529     {
1530       cc_status.flags &= ~CC_OVERFLOW_UNUSABLE;
1531       return 0;
1532     }
1533
1534   if (get_attr_length (insn) == 2)
1535     return \"b%k1       %l0\";
1536   else if (get_attr_length (insn) == 4)
1537     return \"b%k1       %l0:16\";
1538   else
1539     return \"b%j1       .Lh8BR%=\;jmp   @%l0\\n.Lh8BR%=:\";
1540 }"
1541   [(set_attr "type" "branch")
1542    (set_attr "cc" "none")])
1543
1544 ;; Unconditional and other jump instructions.
1545
1546 (define_insn "jump"
1547   [(set (pc)
1548         (label_ref (match_operand 0 "" "")))]
1549   ""
1550   "*
1551 {
1552   if (get_attr_length (insn) == 2)
1553     return \"bra        %l0\";
1554   else if (get_attr_length (insn) == 4)
1555     return \"bra        %l0:16\";
1556   else
1557     return \"jmp        @%l0\";
1558 }"
1559   [(set_attr "type" "branch")
1560    (set_attr "cc" "none")])
1561
1562 ;; This is a define expand, because pointers may be either 16 or 32 bits.
1563
1564 (define_expand "tablejump"
1565   [(parallel [(set (pc) (match_operand 0 "register_operand" "r"))
1566               (use (label_ref (match_operand 1 "" "")))])]
1567   ""
1568   "")
1569
1570 (define_insn "tablejump_h8300"
1571   [(set (pc) (match_operand:HI 0 "register_operand" "r"))
1572    (use (label_ref (match_operand 1 "" "")))]
1573   "TARGET_H8300"
1574   "jmp  @%0"
1575   [(set_attr "cc" "none")
1576    (set_attr "length" "2")])
1577
1578 (define_insn "tablejump_h8300h"
1579   [(set (pc) (match_operand:SI 0 "register_operand" "r"))
1580    (use (label_ref (match_operand 1 "" "")))]
1581   "TARGET_H8300H || TARGET_H8300S"
1582   "jmp  @%0"
1583   [(set_attr "cc" "none")
1584    (set_attr "length" "2")])
1585
1586 ;; This is a define expand, because pointers may be either 16 or 32 bits.
1587
1588 (define_expand "indirect_jump"
1589   [(set (pc) (match_operand 0 "jump_address_operand" ""))]
1590   ""
1591   "")
1592
1593 (define_insn "indirect_jump_h8300"
1594   [(set (pc) (match_operand:HI 0 "jump_address_operand" "Vr"))]
1595   "TARGET_H8300"
1596   "jmp  @%0"
1597   [(set_attr "cc" "none")
1598    (set_attr "length" "2")])
1599
1600 (define_insn "indirect_jump_h8300h"
1601   [(set (pc) (match_operand:SI 0 "jump_address_operand" "Vr"))]
1602   "TARGET_H8300H || TARGET_H8300S"
1603   "jmp @%0"
1604   [(set_attr "cc" "none")
1605    (set_attr "length" "2")])
1606
1607 ;; Call subroutine with no return value.
1608
1609 ;; ??? Even though we use HImode here, this works for the 300h.
1610
1611 (define_insn "call"
1612   [(call (match_operand:QI 0 "call_insn_operand" "or")
1613          (match_operand:HI 1 "general_operand" "g"))]
1614   ""
1615   "*
1616 {
1617   if (GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF
1618       && SYMBOL_REF_FLAG (XEXP (operands[0], 0)))
1619     return \"jsr\\t\@%0:8\";
1620   else
1621     return \"jsr\\t%0\";
1622 }"
1623   [(set_attr "cc" "clobber")
1624    (set (attr "length")
1625      (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
1626                    (const_int 4)
1627                    (const_int 8)))])
1628
1629 ;; Call subroutine, returning value in operand 0
1630 ;; (which must be a hard register).
1631
1632 ;; ??? Even though we use HImode here, this works on the 300h.
1633
1634 (define_insn "call_value"
1635   [(set (match_operand 0 "" "=r")
1636         (call (match_operand:QI 1 "call_insn_operand" "or")
1637               (match_operand:HI 2 "general_operand" "g")))]
1638   ""
1639   "*
1640 {
1641   if (GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
1642       && SYMBOL_REF_FLAG (XEXP (operands[1], 0)))
1643     return \"jsr\\t\@%1:8\";
1644   else
1645     return \"jsr\\t%1\";
1646 }"
1647   [(set_attr "cc" "clobber")
1648    (set (attr "length")
1649      (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
1650                    (const_int 4)
1651                    (const_int 8)))])
1652
1653 (define_insn "nop"
1654   [(const_int 0)]
1655   ""
1656   "nop"
1657   [(set_attr "cc" "none")
1658    (set_attr "length" "2")])
1659 \f
1660 ;; ----------------------------------------------------------------------
1661 ;; EXTEND INSTRUCTIONS
1662 ;; ----------------------------------------------------------------------
1663
1664 (define_expand "zero_extendqihi2"
1665   [(set (match_operand:HI 0 "register_operand" "")
1666         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "")))]
1667   ""
1668   "")
1669
1670 (define_insn ""
1671   [(set (match_operand:HI 0 "register_operand" "=r,r")
1672         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
1673   "TARGET_H8300"
1674   "@
1675   mov.b #0,%t0
1676   mov.b %R1,%s0\;mov.b  #0,%t0"
1677   [(set_attr "length" "2,10")
1678    (set_attr "cc" "clobber,clobber")])
1679
1680 (define_insn ""
1681   [(set (match_operand:HI 0 "register_operand" "=r,r")
1682         (zero_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
1683   "TARGET_H8300H || TARGET_H8300S"
1684   "@
1685   extu.w        %T0
1686   mov.b %R1,%s0\;extu.w %T0"
1687   [(set_attr "length" "2,10")
1688    (set_attr "cc" "set_znv,set_znv")])
1689
1690 ;; The compiler can synthesize a 300H variant of this which is
1691 ;; just as efficient as one that we'd create
1692 (define_insn "zero_extendqisi2"
1693   [(set (match_operand:SI 0 "register_operand" "=r,r")
1694         (zero_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
1695   "TARGET_H8300"
1696   "@
1697   mov.b #0,%x0\;sub.w   %e0,%e0
1698   mov.b %R1,%w0\;mov.b  #0,%x0\;sub.w   %e0,%e0"
1699   [(set_attr "length" "4,6")
1700    (set_attr "cc" "clobber,clobber")])
1701
1702 (define_expand "zero_extendhisi2"
1703   [(set (match_operand:SI 0 "register_operand" "")
1704         (zero_extend:SI (match_operand:HI 1 "register_operand" "")))]
1705   ""
1706   "")
1707
1708 ;; %e prints the high part of a CONST_INT, not the low part.  Arggh.
1709 (define_insn ""
1710   [(set (match_operand:SI 0 "register_operand" "=r,r,r")
1711         (zero_extend:SI (match_operand:HI 1 "general_operand_src" "0,i,g>")))]
1712   "TARGET_H8300"
1713   "@
1714   sub.w %e0,%e0
1715   mov.w %f1,%f0\;sub.w  %e0,%e0
1716   mov.w %e1,%f0\;sub.w  %e0,%e0"
1717   [(set_attr "length" "2,4,4")
1718    (set_attr "cc" "clobber,clobber,clobber")])
1719
1720 (define_insn ""
1721   [(set (match_operand:SI 0 "register_operand" "=r")
1722         (zero_extend:SI (match_operand:HI 1 "register_operand" "0")))]
1723   "TARGET_H8300H || TARGET_H8300S"
1724   "extu.l       %S0"
1725   [(set_attr "length" "2")
1726    (set_attr "cc" "set_znv")])
1727
1728 (define_expand "extendqihi2"
1729   [(set (match_operand:HI 0 "register_operand" "")
1730         (sign_extend:HI (match_operand:QI 1 "register_operand" "")))]
1731   ""
1732   "")
1733
1734 (define_insn ""
1735   [(set (match_operand:HI 0 "register_operand" "=r,r")
1736         (sign_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
1737   "TARGET_H8300"
1738   "@
1739   bld   #7,%s0\;subx    %t0,%t0
1740   mov.b %R1,%s0\;bld    #7,%s0\;subx    %t0,%t0"
1741   [(set_attr "length" "4,6")
1742    (set_attr "cc" "clobber,clobber")])
1743
1744 (define_insn ""
1745   [(set (match_operand:HI 0 "register_operand" "=r")
1746         (sign_extend:HI (match_operand:QI 1 "register_operand" "0")))]
1747   "TARGET_H8300H || TARGET_H8300S"
1748   "exts.w       %T0"
1749   [(set_attr "length" "2")
1750    (set_attr "cc" "set_znv")])
1751
1752 ;; The compiler can synthesize a 300H variant of this which is
1753 ;; just as efficient as one that we'd create
1754 (define_insn "extendqisi2"
1755   [(set (match_operand:SI 0 "register_operand" "=r,r")
1756         (sign_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
1757   "TARGET_H8300"
1758   "@
1759   bld   #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0
1760   mov.b %R1,%w0\;bld    #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0"
1761   [(set_attr "length" "8,10")
1762    (set_attr "cc" "clobber,clobber")])
1763
1764 (define_expand "extendhisi2"
1765   [(set (match_operand:SI 0 "register_operand" "")
1766         (sign_extend:SI (match_operand:HI 1 "register_operand" "")))]
1767   ""
1768   "")
1769
1770 (define_insn ""
1771   [(set (match_operand:SI 0 "register_operand" "=r,r")
1772         (sign_extend:SI (match_operand:HI 1 "general_operand_src" "0,g>")))]
1773   "TARGET_H8300"
1774   "@
1775   bld   #7,%x0\;subx    %y0,%y0\;subx   %z0,%z0
1776   mov.w %T1,%f0\;bld    #7,%x0\;subx    %y0,%y0\;subx   %z0,%z0"
1777   [(set_attr "length" "6,8")
1778    (set_attr "cc" "clobber,clobber")])
1779
1780 (define_insn ""
1781   [(set (match_operand:SI 0 "register_operand" "=r")
1782         (sign_extend:SI (match_operand:HI 1 "register_operand" "0")))]
1783   "TARGET_H8300H || TARGET_H8300S"
1784   "exts.l       %S0"
1785   [(set_attr "length" "2")
1786    (set_attr "cc" "set_znv")])
1787 \f
1788 ;; ----------------------------------------------------------------------
1789 ;; SHIFTS
1790 ;; ----------------------------------------------------------------------
1791 ;;
1792 ;; We make some attempt to provide real efficient shifting.  One example is
1793 ;; doing an 8 bit shift of a 16 bit value by moving a byte reg into the other
1794 ;; reg and moving 0 into the former reg.
1795 ;;
1796 ;; We also try to achieve this in a uniform way.  IE: We don't try to achieve
1797 ;; this in both rtl and at insn emit time.  Ideally, we'd use rtl as that would
1798 ;; give the optimizer more cracks at the code.  However, we wish to do things
1799 ;; like optimizing shifting the sign bit to bit 0 by rotating the other way.
1800 ;; There is rtl to handle this (rotate + and), but the H8/300 doesn't handle
1801 ;; 16 bit rotates.  Also, if we emit complicated rtl, combine may not be able
1802 ;; to detect cases it can optimize.
1803 ;;
1804 ;; For these and other fuzzy reasons, I've decided to go the less pretty but
1805 ;; easier "do it at insn emit time" route.
1806
1807 ;; QI BIT SHIFTS
1808
1809 (define_expand "ashlqi3"
1810   [(set (match_operand:QI 0 "register_operand" "")
1811         (ashift:QI (match_operand:QI 1 "register_operand" "")
1812                    (match_operand:QI 2 "nonmemory_operand" "")))]
1813   ""
1814   "if (expand_a_shift (QImode, ASHIFT, operands)) DONE;else FAIL;")
1815
1816 (define_expand "ashrqi3"
1817   [(set (match_operand:QI 0 "register_operand" "")
1818         (ashiftrt:QI (match_operand:QI 1 "register_operand" "")
1819                      (match_operand:QI 2 "nonmemory_operand" "")))]
1820   ""
1821   "if (expand_a_shift (QImode, ASHIFTRT, operands)) DONE;else FAIL;")
1822
1823 (define_expand "lshrqi3"
1824   [(set (match_operand:QI 0 "register_operand" "")
1825         (lshiftrt:QI (match_operand:QI 1 "register_operand" "")
1826                      (match_operand:QI 2 "nonmemory_operand" "")))]
1827   ""
1828   "if (expand_a_shift (QImode, LSHIFTRT, operands)) DONE;else FAIL;")
1829
1830 (define_insn ""
1831   [(set (match_operand:QI 0 "register_operand" "=r,r")
1832         (match_operator:QI 3 "nshift_operator"
1833                         [ (match_operand:QI 1 "register_operand" "0,0")
1834                           (match_operand:QI 2 "nonmemory_operand" "KM,rn")]))
1835    (clobber (match_scratch:QI 4 "=X,&r"))]
1836   ""
1837   "* return emit_a_shift (insn, operands);"
1838   [(set_attr "length" "20")
1839    (set_attr "cc" "clobber")])
1840
1841 ;; HI BIT SHIFTS
1842
1843 (define_expand "ashlhi3"
1844   [(set (match_operand:HI 0 "register_operand" "")
1845         (ashift:HI (match_operand:HI 1 "nonmemory_operand" "")
1846                    (match_operand:QI 2 "nonmemory_operand" "")))]
1847   ""
1848   "if (expand_a_shift (HImode, ASHIFT, operands)) DONE;else FAIL;")
1849
1850 (define_expand "lshrhi3"
1851   [(set (match_operand:HI 0 "register_operand" "")
1852         (lshiftrt:HI (match_operand:HI 1 "general_operand" "")
1853                      (match_operand:QI 2 "nonmemory_operand" "")))]
1854   ""
1855   "if (expand_a_shift (HImode, LSHIFTRT, operands)) DONE;else FAIL;")
1856
1857 (define_expand "ashrhi3"
1858   [(set (match_operand:HI 0 "register_operand" "")
1859         (ashiftrt:HI (match_operand:HI 1 "register_operand" "")
1860                      (match_operand:QI 2 "nonmemory_operand" "")))]
1861   ""
1862   "if (expand_a_shift (HImode, ASHIFTRT, operands)) DONE;else FAIL;")
1863
1864 (define_insn ""
1865   [(set (match_operand:HI 0 "register_operand" "=r,r")
1866         (match_operator:HI 3 "nshift_operator"
1867                         [ (match_operand:HI 1 "register_operand" "0,0")
1868                           (match_operand:QI 2 "nonmemory_operand" "KM,rn")]))
1869    (clobber (match_scratch:QI 4 "=X,&r"))]
1870   ""
1871   "* return emit_a_shift (insn, operands);"
1872   [(set_attr "length" "20")
1873    (set_attr "cc" "clobber")])
1874
1875 ;;  SI BIT SHIFTS
1876
1877 (define_expand "ashlsi3"
1878   [(set (match_operand:SI 0 "register_operand" "")
1879         (ashift:SI
1880          (match_operand:SI 1 "general_operand" "")
1881          (match_operand:QI 2 "nonmemory_operand" "")))]
1882   ""
1883   "if (expand_a_shift (SImode, ASHIFT, operands)) DONE;else FAIL;")
1884
1885 (define_expand "lshrsi3"
1886   [(set (match_operand:SI 0 "register_operand" "")
1887         (lshiftrt:SI
1888          (match_operand:SI 1 "general_operand" "")
1889          (match_operand:QI 2 "nonmemory_operand" "")))]
1890   ""
1891   "if (expand_a_shift (SImode, LSHIFTRT, operands)) DONE;else FAIL;")
1892
1893 (define_expand "ashrsi3"
1894   [(set (match_operand:SI 0 "register_operand" "")
1895         (ashiftrt:SI
1896          (match_operand:SI 1 "general_operand" "")
1897          (match_operand:QI 2 "nonmemory_operand" "")))]
1898   ""
1899   "if (expand_a_shift (SImode, ASHIFTRT, operands)) DONE;else FAIL;")
1900
1901 (define_insn ""
1902   [(set (match_operand:SI 0 "register_operand" "=r,r")
1903         (match_operator:SI 3 "nshift_operator"
1904                         [ (match_operand:SI 1 "register_operand" "0,0")
1905                           (match_operand:QI 2 "nonmemory_operand" "K,rn")]))
1906    (clobber (match_scratch:QI 4 "=X,&r"))]
1907   ""
1908   "* return emit_a_shift (insn, operands);"
1909   [(set_attr "length" "20")
1910    (set_attr "cc" "clobber")])
1911 \f
1912 ;; ----------------------------------------------------------------------
1913 ;; ROTATIONS
1914 ;; ----------------------------------------------------------------------
1915
1916 (define_expand "rotlqi3"
1917   [(set (match_operand:QI 0 "register_operand" "")
1918         (rotate:QI (match_operand:QI 1 "register_operand" "")
1919                    (match_operand:QI 2 "nonmemory_operand" "")))]
1920   ""
1921   "if (expand_a_rotate (ROTATE, operands)) DONE;else FAIL;")
1922
1923 (define_insn "*rotlqi3_1"
1924   [(set (match_operand:QI 0 "register_operand" "=r")
1925         (rotate:QI (match_operand:QI 1 "register_operand" "0")
1926                    (match_operand:QI 2 "immediate_operand" "")))]
1927   ""
1928   "* return emit_a_rotate (ROTATE, operands);"
1929   [(set_attr "length" "20")
1930    (set_attr "cc" "clobber")])
1931
1932 (define_expand "rotlhi3"
1933   [(set (match_operand:HI 0 "register_operand" "")
1934         (rotate:HI (match_operand:HI 1 "register_operand" "")
1935                    (match_operand:QI 2 "nonmemory_operand" "")))]
1936   ""
1937   "if (expand_a_rotate (ROTATE, operands)) DONE;else FAIL;")
1938
1939 (define_insn "*rotlhi3_1"
1940   [(set (match_operand:HI 0 "register_operand" "=r")
1941         (rotate:HI (match_operand:HI 1 "register_operand" "0")
1942                    (match_operand:QI 2 "immediate_operand" "")))]
1943   ""
1944   "* return emit_a_rotate (ROTATE, operands);"
1945   [(set_attr "length" "20")
1946    (set_attr "cc" "clobber")])
1947
1948 (define_expand "rotlsi3"
1949   [(set (match_operand:SI 0 "register_operand" "")
1950         (rotate:SI (match_operand:SI 1 "register_operand" "")
1951                    (match_operand:QI 2 "nonmemory_operand" "")))]
1952   "TARGET_H8300H || TARGET_H8300S"
1953   "if (expand_a_rotate (ROTATE, operands)) DONE;else FAIL;")
1954
1955 (define_insn "*rotlsi3_1"
1956   [(set (match_operand:SI 0 "register_operand" "=r")
1957         (rotate:SI (match_operand:SI 1 "register_operand" "0")
1958                    (match_operand:QI 2 "immediate_operand" "")))]
1959   "TARGET_H8300H || TARGET_H8300S"
1960   "* return emit_a_rotate (ROTATE, operands);"
1961   [(set_attr "length" "20")
1962    (set_attr "cc" "clobber")])
1963 \f
1964 ;; -----------------------------------------------------------------
1965 ;; BIT FIELDS
1966 ;; -----------------------------------------------------------------
1967 ;; The H8/300 has given 1/8th of its opcode space to bitfield
1968 ;; instructions so let's use them as well as we can.
1969
1970 ;; You'll never believe all these patterns perform one basic action --
1971 ;; load a bit from the source, optionally invert the bit, then store it
1972 ;; in the destination (which is known to be zero).
1973 ;;
1974 ;; Combine obviously need some work to better identify this situation and
1975 ;; canonicalize the form better.
1976
1977 ;;
1978 ;; Normal loads with a 16bit destination.
1979 ;;
1980 ;; Yes, both cases are needed.
1981 ;;
1982 (define_insn ""
1983   [(set (match_operand:HI 0 "register_operand" "=&r")
1984         (zero_extract:HI (match_operand:HI 1 "register_operand" "r")
1985                          (const_int 1)
1986                          (match_operand:HI 2 "immediate_operand" "n")))]
1987   ""
1988   "sub.w        %0,%0\;bld      %Z2,%Y1\;bst    #0,%X0"
1989   [(set_attr "cc" "clobber")
1990    (set_attr "length" "6")])
1991
1992 (define_insn ""
1993   [(set (match_operand:HI 0 "register_operand" "=&r")
1994         (subreg:HI (zero_extract:SI
1995                      (match_operand:HI 1 "register_operand" "r")
1996                      (const_int 1)
1997                      (match_operand:HI 2 "immediate_operand" "n")) 2))]
1998   ""
1999   "sub.w        %0,%0\;bld      %Z2,%Y1\;bst    #0,%X0"
2000   [(set_attr "cc" "clobber")
2001    (set_attr "length" "6")])
2002
2003 ;;
2004 ;; Inverted loads with a 16bit destination.
2005 ;;
2006 ;; Yes, all four cases are needed.
2007 ;;
2008
2009 (define_insn ""
2010   [(set (match_operand:HI 0 "register_operand" "=&r")
2011         (zero_extract:HI (xor:HI (match_operand:HI 1 "register_operand" "r")
2012                                  (match_operand:HI 3 "p_operand" "P"))
2013                          (const_int 1)
2014                          (match_operand:HI 2 "const_int_operand" "n")))]
2015   "(1 << INTVAL (operands[2])) == INTVAL (operands[3])"
2016   "sub.w        %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
2017   [(set_attr "cc" "clobber")
2018    (set_attr "length" "8")])
2019
2020 (define_insn ""
2021   [(set (match_operand:HI 0 "register_operand" "=&r")
2022         (and:HI (not:HI
2023                   (lshiftrt:HI
2024                     (match_operand:HI 1 "bit_operand" "Ur")
2025                     (match_operand:HI 2 "const_int_operand" "n")))
2026                 (const_int 1)))]
2027   ""
2028   "sub.w        %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
2029   [(set_attr "cc" "clobber")
2030    (set_attr "length" "8")])
2031
2032 (define_insn ""
2033   [(set (match_operand:HI 0 "register_operand" "=&r")
2034         (and:HI (not:HI
2035                   (subreg:HI
2036                     (lshiftrt:SI
2037                       (match_operand:SI 1 "register_operand" "Ur")
2038                       (match_operand:SI 2 "const_int_operand" "n")) 2))
2039                 (const_int 1)))]
2040   "INTVAL (operands[2]) < 16"
2041   "sub.w        %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
2042   [(set_attr "cc" "clobber")
2043    (set_attr "length" "8")])
2044
2045 (define_insn ""
2046   [(set (match_operand:HI 0 "register_operand" "=&r")
2047         (and:HI (not:HI
2048                   (subreg:HI
2049                     (lshiftrt:SI
2050                       (match_operand:SI 1 "bit_operand" "Ur")
2051                       (match_operand:SI 2 "const_int_operand" "n")) 0))
2052                 (const_int 1)))]
2053   "(TARGET_H8300H || TARGET_H8300S)
2054    && INTVAL (operands[2]) < 16"
2055   "sub.w        %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
2056   [(set_attr "cc" "clobber")
2057    (set_attr "length" "8")])
2058
2059 ;;
2060 ;; Normal loads with a 32bit destination.
2061 ;;
2062 ;; Yes, all three cases are needed.
2063 ;;
2064 (define_insn ""
2065   [(set (match_operand:SI 0 "register_operand" "=&r")
2066         (zero_extract:SI (match_operand:HI 1 "register_operand" "r")
2067                          (const_int 1)
2068                          (match_operand:HI 2 "const_int_operand" "n")))]
2069   ""
2070   "* return output_simode_bld (0, 0, operands);"
2071   [(set_attr "cc" "clobber")
2072    (set (attr "length")
2073         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
2074                           (const_int 0))
2075                       (const_int 10)
2076                       (const_int 8)))])
2077
2078 (define_insn ""
2079   [(set (match_operand:SI 0 "register_operand" "=&r")
2080         (and:SI (zero_extend:SI
2081                    (lshiftrt:QI
2082                      (match_operand:QI 1 "bit_operand" "Ur")
2083                      (match_operand:QI 2 "const_int_operand" "n")))
2084                 (const_int 1)))]
2085   ""
2086   "* return output_simode_bld (0, 0, operands);"
2087   [(set_attr "cc" "clobber")
2088    (set (attr "length")
2089         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
2090                           (const_int 0))
2091                       (const_int 10)
2092                       (const_int 8)))])
2093
2094 (define_insn ""
2095   [(set (match_operand:SI 0 "register_operand" "=&r")
2096         (and:SI (zero_extend:SI
2097                    (lshiftrt:HI
2098                       (match_operand:HI 1 "bit_operand" "Ur")
2099                       (match_operand:HI 2 "const_int_operand" "n")))
2100                 (const_int 1)))]
2101   ""
2102   "* return output_simode_bld (0, 0, operands);"
2103   [(set_attr "cc" "clobber")
2104    (set (attr "length")
2105         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
2106                           (const_int 0))
2107                       (const_int 10)
2108                       (const_int 8)))])
2109
2110 ;;
2111 ;; Inverted loads with a 32bit destination.
2112 ;;
2113 ;; Yes, all seven cases are needed.
2114 ;;
2115 (define_insn ""
2116   [(set (match_operand:SI 0 "register_operand" "=&r")
2117         (and:SI (not:SI
2118                   (zero_extend:SI (match_operand:HI 1 "register_operand" "r")))
2119                 (match_operand:SI 2 "p_operand" "P")))]
2120   ""
2121   "* return output_simode_bld (1, 1, operands);"
2122   [(set_attr "cc" "clobber")
2123    (set (attr "length")
2124         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
2125                           (const_int 0))
2126                       (const_int 10)
2127                       (const_int 8)))])
2128 (define_insn ""
2129   [(set (match_operand:SI 0 "register_operand" "=&r")
2130         (and:SI (not:SI
2131                   (zero_extend:SI
2132                     (lshiftrt:HI (match_operand:HI 1 "bit_operand" "Ur")
2133                                  (match_operand:HI 2 "const_int_operand" "n"))))
2134                 (const_int 1)))]
2135   ""
2136   "* return output_simode_bld (1, 0, operands);"
2137   [(set_attr "cc" "clobber")
2138    (set (attr "length")
2139         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
2140                           (const_int 0))
2141                       (const_int 10)
2142                       (const_int 8)))])
2143
2144 (define_insn ""
2145   [(set (match_operand:SI 0 "register_operand" "=&r")
2146         (and:SI (not:SI
2147                   (zero_extend:SI (match_operand:QI 1 "register_operand" "r")))
2148                 (match_operand:SI 2 "p_operand" "P")))]
2149   ""
2150   "* return output_simode_bld (1, 1, operands);"
2151   [(set_attr "cc" "clobber")
2152    (set (attr "length")
2153         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
2154                           (const_int 0))
2155                       (const_int 10)
2156                       (const_int 8)))])
2157 (define_insn ""
2158   [(set (match_operand:SI 0 "register_operand" "=&r")
2159         (and:SI (not:SI
2160                   (zero_extend:SI
2161                     (lshiftrt:QI (match_operand:QI 1 "bit_operand" "Ur")
2162                                  (match_operand:QI 2 "const_int_operand" "n"))))
2163                 (const_int 1)))]
2164   ""
2165   "* return output_simode_bld (1, 0, operands);"
2166   [(set_attr "cc" "clobber")
2167    (set (attr "length")
2168         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
2169                           (const_int 0))
2170                       (const_int 10)
2171                       (const_int 8)))])
2172
2173 (define_insn ""
2174   [(set (match_operand:SI 0 "register_operand" "=&r")
2175         (and:SI (not:SI
2176                   (subreg:SI
2177                      (lshiftrt:HI
2178                         (match_operand:HI 1 "bit_operand" "Ur")
2179                         (match_operand:HI 2 "const_int_operand" "n")) 0))
2180                 (const_int 1)))]
2181   "1"
2182   "* return output_simode_bld (1, 0, operands);"
2183   [(set_attr "cc" "clobber")
2184    (set (attr "length")
2185         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
2186                           (const_int 0))
2187                       (const_int 10)
2188                       (const_int 8)))])
2189
2190 (define_insn ""
2191   [(set (match_operand:SI 0 "register_operand" "=&r")
2192         (and:SI (not:SI
2193                   (subreg:SI
2194                      (lshiftrt:QI
2195                         (match_operand:QI 1 "bit_operand" "Ur")
2196                         (match_operand:QI 2 "const_int_operand" "n")) 0))
2197                 (const_int 1)))]
2198   "1"
2199   "* return output_simode_bld (1, 0, operands);"
2200   [(set_attr "cc" "clobber")
2201    (set (attr "length")
2202         (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
2203                           (const_int 0))
2204                       (const_int 10)
2205                       (const_int 8)))])
2206
2207 (define_insn ""
2208   [(set (match_operand:SI 0 "register_operand" "=&r")
2209         (zero_extract:SI (xor:HI (match_operand:HI 1 "register_operand" "r")
2210                                  (match_operand:HI 3 "p_operand" "P"))
2211                          (const_int 1)
2212                          (match_operand:HI 2 "const_int_operand" "n")))]
2213   "(1 << INTVAL (operands[2])) == INTVAL (operands[3])"
2214   "sub.w        %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
2215   [(set_attr "cc" "clobber")
2216    (set_attr "length" "8")])
2217
2218 (define_expand "insv"
2219   [(set (zero_extract:HI (match_operand:HI 0 "general_operand" "")
2220                          (match_operand:HI 1 "general_operand" "")
2221                          (match_operand:HI 2 "general_operand" ""))
2222         (match_operand:HI 3 "general_operand" ""))]
2223   "TARGET_H8300"
2224   "
2225 {
2226   /* We only have single bit bitfield instructions.  */
2227   if (INTVAL (operands[1]) != 1)
2228     FAIL;
2229
2230   /* For now, we don't allow memory operands.  */
2231   if (GET_CODE (operands[0]) == MEM
2232       || GET_CODE (operands[3]) == MEM)
2233     FAIL;
2234 }")
2235
2236 (define_insn ""
2237   [(set (zero_extract:HI (match_operand:HI 0 "register_operand" "+r")
2238                          (const_int 1)
2239                          (match_operand:HI 1 "immediate_operand" "n"))
2240         (match_operand:HI 2 "register_operand" "r"))]
2241   ""
2242   "bld  #0,%R2\;bst     %Z1,%Y0 ; i1"
2243   [(set_attr "cc" "clobber")
2244    (set_attr "length" "4")])
2245
2246 (define_expand "extzv"
2247   [(set (match_operand:HI 0 "register_operand" "")
2248         (zero_extract:HI (match_operand:HI 1 "bit_operand" "")
2249                          (match_operand:HI 2 "general_operand" "")
2250                          (match_operand:HI 3 "general_operand" "")))]
2251   "TARGET_H8300"
2252   "
2253 {
2254   /* We only have single bit bitfield instructions.  */
2255   if (INTVAL (operands[2]) != 1)
2256     FAIL;
2257
2258   /* For now, we don't allow memory operands.  */
2259   if (GET_CODE (operands[1]) == MEM)
2260     FAIL;
2261 }")
2262
2263 ;; BAND, BOR, and BXOR patterns
2264
2265 (define_insn ""
2266   [(set (match_operand:HI 0 "bit_operand" "=Ur")
2267         (match_operator:HI 4 "bit_operator"
2268            [(zero_extract:HI (match_operand:HI 1 "register_operand" "r")
2269                              (const_int 1)
2270                              (match_operand:HI 2 "immediate_operand" "n"))
2271             (match_operand:HI 3 "bit_operand" "0")]))]
2272   ""
2273   "bld  %Z2,%Y1\;%b4    #0,%R0\;bst     #0,%R0; bl1"
2274   [(set_attr "cc" "clobber")
2275    (set_attr "length" "6")
2276    (set_attr "adjust_length" "no")])
2277
2278 (define_insn ""
2279   [(set (match_operand:HI 0 "bit_operand" "=Ur")
2280         (match_operator:HI 5 "bit_operator"
2281            [(zero_extract:HI (match_operand:HI 1 "register_operand" "r")
2282                              (const_int 1)
2283                              (match_operand:HI 2 "immediate_operand" "n"))
2284             (zero_extract:HI (match_operand:HI 3 "register_operand" "r")
2285                              (const_int 1)
2286                              (match_operand:HI 4 "immediate_operand" "n"))]))]
2287   ""
2288   "bld  %Z2,%Y1\;%b5    %Z4,%Y3\;bst    #0,%R0; bl3"
2289   [(set_attr "cc" "clobber")
2290    (set_attr "length" "6")
2291    (set_attr "adjust_length" "no")])