OSDN Git Service

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