OSDN Git Service

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