OSDN Git Service

* config/h8300/h8300.md (an anonymous pattern): New.
[pf3gnuchains/gcc-fork.git] / gcc / config / h8300 / h8300.md
index e340d94..a260083 100644 (file)
@@ -1,6 +1,6 @@
 ;; GCC machine description for Hitachi H8/300
-;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
-;; Free Software Foundation, Inc.
+;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
+;; 2001, 2002 Free Software Foundation, Inc.
 
 ;;   Contributed by Steve Chamberlain (sac@cygnus.com),
 ;;   Jim Wilson (wilson@cygnus.com), and Doug Evans (dje@cygnus.com).
 ;; the Free Software Foundation, 59 Temple Place - Suite 330,
 ;; Boston, MA 02111-1307, USA.
 
-;; The original PO technology requires these to be ordered by speed,
-;; so that assigner will pick the fastest.
-
-;; See file "rtl.def" for documentation on define_insn, match_*, et. al.
-
-(define_attr "cpu" "h8300,h8300h"
-  (const (symbol_ref "cpu_type")))
-
-;; Many logical operations should have "bit" variants if only one
-;; bit is going to be operated on.
-
-;; (and (logical op) (const_int X))
-;; If const_int only specifies a few bits (like a single byte in a 4 byte
-;; operation, then it's more efficient to only apply the and and logical_op
-;; to the bits we care about.
-
 ;; Some of the extend instructions accept a general_operand_src, which
 ;; allows all the normal memory addressing modes.  The length computations
 ;; don't take this into account.  The lengths in the MD file should be
 ;; "worst case" and then be adjusted to their correct values by
 ;; h8300_adjust_insn_length.
 
-;; On the h8300h, adds/subs operate on the 32bit "er" registers.  Right
-;; now GCC doesn't expose the "e" half to the compiler, so using add/subs
-;; for addhi and subhi is safe.
-;; Long term, we want to expose the "e" half to the compiler (gives us
-;; 8 more 16bit registers).  At that point addhi and subhi can't use adds/subs.
+;; On the H8/300H and H8S, adds/subs operate on the 32bit "er"
+;; registers.  Right now GCC doesn't expose the "e" half to the
+;; compiler, so using add/subs for addhi and subhi is safe.  Long
+;; term, we want to expose the "e" half to the compiler (gives us 8
+;; more 16bit registers).  At that point addhi and subhi can't use
+;; adds/subs.
 
-;; There's currently no way to have a insv/extzv expander for the h8/300h
-;; because word_mode is different for the h8/300 and h8/300h.
+;; There's currently no way to have an insv/extzv expander for the H8/300H
+;; because word_mode is different for the H8/300 and H8/300H.
 
 ;; Shifts/rotates by small constants should be handled by special
 ;; patterns so we get the length and cc status correct.
 
 ;; ??? Implement remaining bit ops available on the h8300
 
+;; ----------------------------------------------------------------------
+;; CONSTANTS
+;; ----------------------------------------------------------------------
+
+(define_constants
+  [(SC_REG      3)
+   (FP_REG      6)
+   (SP_REG      7)
+   (MAC_REG     8)
+   (AP_REG      9)
+   (RAP_REG    10)])
+
+;; ----------------------------------------------------------------------
+;; ATTRIBUTES
+;; ----------------------------------------------------------------------
+
+(define_attr "cpu" "h8300,h8300h"
+  (const (symbol_ref "cpu_type")))
+
 (define_attr "type" "branch,arith"
   (const_string "arith"))
 
 ;; The size of instructions in bytes.
 
-(define_attr "length" "" 
+(define_attr "length" ""
   (cond [(eq_attr "type" "branch")
-        (if_then_else (and (ge (minus (pc) (match_dup 0))
-                               (const_int -120))
-                           (le (minus (pc) (match_dup 0))
-                               (const_int 120)))
+        (if_then_else (and (ge (minus (match_dup 0) (pc))
+                               (const_int -126))
+                           (le (minus (match_dup 0) (pc))
+                               (const_int 126)))
                       (const_int 2)
                       (if_then_else (and (eq_attr "cpu" "h8300h")
                                          (and (ge (minus (pc) (match_dup 0))
 
 (define_attr "adjust_length" "yes,no"
   (cond [(eq_attr "type" "branch") (const_string "no")]
-        (const_string "yes")))
+       (const_string "yes")))
 
 ;; Condition code settings.
+;;
 ;; none - insn does not affect cc
 ;; none_0hit - insn does not affect cc but it does modify operand 0
 ;;     This attribute is used to keep track of when operand 0 changes.
-;;     See the description of NOTICE_UPDATE_CC for more info.
+;;     See the description of NOTICE_UPDATE_CC for more info.
 ;; set_znv - insn sets z,n,v to usable values (like a tst insn); c is unknown.
 ;; set_zn  - insn sets z,n to usable values; v,c are unknown.
 ;; compare - compare instruction
 ;; clobber - value of cc is unknown
+
 (define_attr "cc" "none,none_0hit,set_znv,set_zn,compare,clobber"
   (const_string "clobber"))
 \f
 
 ;; movqi
 
-(define_insn "movqi_push"
-  [(set (match_operand:QI 0 "push_operand" "=<")
-       (match_operand:QI 1 "register_operand" "r"))]
+(define_insn "pushqi1_h8300"
+  [(parallel [(set (reg:HI SP_REG)
+                  (plus:HI (reg:HI SP_REG) (const_int -2)))
+             (set (mem:QI (plus:HI (reg:HI SP_REG) (const_int -1)))
+                  (match_operand:QI 0 "register_operand" "r"))])]
+  "TARGET_H8300"
+  "mov.w\\t%T0,@-r7"
+  [(set_attr "length" "2")
+   (set_attr "cc" "clobber")])
+
+(define_insn "pushqi1_h8300hs"
+  [(parallel [(set (reg:SI SP_REG)
+                  (plus:SI (reg:SI SP_REG) (const_int -4)))
+             (set (mem:QI (plus:SI (reg:SI SP_REG) (const_int -3)))
+                  (match_operand:QI 0 "register_operand" "r"))])]
+  "TARGET_H8300H || TARGET_H8300S"
+  "mov.l\\t%S0,@-er7"
+  [(set_attr "length" "4")
+   (set_attr "cc" "clobber")])
+
+(define_expand "pushqi1"
+  [(use (match_operand:QI 0 "register_operand" ""))]
   ""
-  "*
+  "
 {
   if (TARGET_H8300)
-    return \"push.w    %T1\";
+    emit_insn (gen_pushqi1_h8300 (operands[0]));
   else
-    return \"push.l    %S1\";
-}"
-  [(set (attr "length") (if_then_else (eq_attr "cpu" "h8300") (const_int 2) (const_int 4)))
-   (set_attr "cc" "set_znv")])
+    emit_insn (gen_pushqi1_h8300hs (operands[0]));
+  DONE;
+}")
 
 (define_insn ""
-  [(set (match_operand:QI 0 "general_operand_dst" "=r,r,<,r,r,m")
-       (match_operand:QI 1 "general_operand_src" "I,r>,r,n,m,r"))]
-  "register_operand (operands[0],QImode)
-   || register_operand (operands[1], QImode)"
+  [(set (match_operand:QI 0 "general_operand_dst" "=r,r ,<,r,r,m")
+       (match_operand:QI 1 "general_operand_src" " I,r>,r,n,m,r"))]
+  "TARGET_H8300
+   && (register_operand (operands[0], QImode)
+       || register_operand (operands[1], QImode))"
   "@
    sub.b       %X0,%X0
    mov.b       %R1,%X0
    mov.b       %R1,%X0
    mov.b       %R1,%X0
    mov.b       %X1,%R0"
-  [(set_attr_alternative "length"
-     [(const_int 2) (const_int 2) (const_int 2) (const_int 2)
-      (if_then_else (eq_attr "cpu" "h8300") (const_int 4) (const_int 8))
-      (if_then_else (eq_attr "cpu" "h8300") (const_int 4) (const_int 8))])
+  [(set_attr "length" "2,2,2,2,4,4")
    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
 
+(define_insn ""
+  [(set (match_operand:QI 0 "general_operand_dst" "=r,r ,<,r,r,m")
+       (match_operand:QI 1 "general_operand_src" " I,r>,r,n,m,r"))]
+  "(TARGET_H8300H || TARGET_H8300S)
+   && (register_operand (operands[0], QImode)
+       || register_operand (operands[1], QImode))"
+  "@
+   sub.b       %X0,%X0
+   mov.b       %R1,%X0
+   mov.b       %X1,%R0
+   mov.b       %R1,%X0
+   mov.b       %R1,%X0
+   mov.b       %X1,%R0"
+  [(set_attr "length" "2,2,2,2,8,8")
+   (set_attr "cc" "set_zn,set_znv,set_znv,clobber,set_znv,set_znv")])
+
 (define_expand "movqi"
   [(set (match_operand:QI 0 "general_operand_dst" "")
        (match_operand:QI 1 "general_operand_src" ""))]
   ""
   "
 {
-  /* One of the ops has to be in a register */
-  if (!register_operand(operand0, QImode)
-      && !register_operand(operand1, QImode))
+  /* One of the ops has to be in a register */
+  if (!register_operand (operand0, QImode)
+      && !register_operand (operand1, QImode))
     {
-      operands[1] = copy_to_mode_reg(QImode, operand1);
+      operands[1] = copy_to_mode_reg (QImode, operand1);
     }
 }")
 
 (define_insn "movstrictqi"
-  [(set (strict_low_part (match_operand:QI 0 "general_operand_dst" "=r,r,r,r"))
+  [(set (strict_low_part (match_operand:QI 0 "general_operand_dst" "+r,r,r,r"))
                         (match_operand:QI 1 "general_operand_src" "I,r,n,m"))]
   ""
   "@
      [(const_int 2) (const_int 2) (const_int 2)
       (if_then_else (eq_attr "cpu" "h8300") (const_int 4) (const_int 8))])
    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv")])
-   
+
 ;; movhi
 
-;; ??? We use push.l on the h8300h to push a 16bit value?!?  We have
-;; 16bit push insns!
-(define_insn "movhi_push"
-  [(set (match_operand:HI 0 "push_operand" "=<")
-       (match_operand:HI 1 "register_operand" "r"))]
+(define_expand "pushhi1_h8300"
+  [(set (mem:HI (pre_dec:HI (reg:HI SP_REG)))
+       (match_operand:HI 0 "register_operand" ""))]
+  "TARGET_H8300"
+  "")
+
+(define_insn "pushhi1_h8300hs"
+  [(parallel [(set (reg:SI SP_REG)
+                  (plus:SI (reg:SI SP_REG) (const_int -4)))
+             (set (mem:HI (plus:SI (reg:SI SP_REG) (const_int -2)))
+                  (match_operand:HI 0 "register_operand" "r"))])]
+  "TARGET_H8300H || TARGET_H8300S"
+  "mov.l\\t%S0,@-er7"
+  [(set_attr "length" "4")
+   (set_attr "cc" "clobber")])
+
+(define_expand "pushhi1"
+  [(use (match_operand:HI 0 "register_operand" ""))]
   ""
-  "*
+  "
 {
   if (TARGET_H8300)
-    return \"push.w    %T1\";
+    emit_insn (gen_pushhi1_h8300 (operands[0]));
   else
-    return \"push.l    %S1\";
-}"
-  [(set (attr "length") (if_then_else (eq_attr "cpu" "h8300") (const_int 2) (const_int 4)))
-   (set_attr "cc" "set_znv")])
+    emit_insn (gen_pushhi1_h8300hs (operands[0]));
+  DONE;
+}")
 
 (define_insn ""
   [(set (match_operand:HI 0 "general_operand_dst" "=r,r,<,r,r,m")
        (match_operand:HI 1 "general_operand_src" "I,r>,r,i,m,r"))]
-  "register_operand (operands[0],HImode)
-   || register_operand (operands[1], HImode)"
+  "TARGET_H8300
+   && (register_operand (operands[0], HImode)
+       || register_operand (operands[1], HImode))
+   && !(GET_CODE (operands[0]) == MEM
+       && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
+       && GET_CODE (XEXP (XEXP (operands[0], 0), 0)) == REG
+       && GET_CODE (operands[1]) == REG
+       && REGNO (XEXP (XEXP (operands[0], 0), 0)) == REGNO (operands[1]))"
   "@
    sub.w       %T0,%T0
    mov.w       %T1,%T0
    mov.w       %T1,%T0
    mov.w       %T1,%T0
    mov.w       %T1,%T0"
-  [(set_attr_alternative "length"
-     [(const_int 2) (const_int 2) (const_int 2) (const_int 4)
-      (if_then_else (eq_attr "cpu" "h8300") (const_int 4) (const_int 8))
-      (if_then_else (eq_attr "cpu" "h8300") (const_int 4) (const_int 8))])
+  [(set_attr "length" "2,2,2,4,4,4")
+   (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
+
+(define_insn ""
+  [(set (match_operand:HI 0 "general_operand_dst" "=r,r,<,r,r,m")
+       (match_operand:HI 1 "general_operand_src" "I,r>,r,i,m,r"))]
+  "(TARGET_H8300H || TARGET_H8300S)
+   && (register_operand (operands[0], HImode)
+       || register_operand (operands[1], HImode))"
+  "@
+   sub.w       %T0,%T0
+   mov.w       %T1,%T0
+   mov.w       %T1,%T0
+   mov.w       %T1,%T0
+   mov.w       %T1,%T0
+   mov.w       %T1,%T0"
+  [(set_attr "length" "2,2,2,4,8,8")
    (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv")])
 
 (define_expand "movhi"
   ""
   "
 {
-  /* One of the ops has to be in a register */
-  if (!register_operand(operand1, HImode)
-      && !register_operand(operand0, HImode))
+  /* One of the ops has to be in a register */
+  if (!register_operand (operand1, HImode)
+      && !register_operand (operand0, HImode))
     {
-      operands[1] = copy_to_mode_reg(HImode, operand1);
+      operands[1] = copy_to_mode_reg (HImode, operand1);
     }
 }")
 
 (define_insn "movstricthi"
-  [(set (strict_low_part (match_operand:HI 0 "general_operand_dst" "=r,r,r,r"))
+  [(set (strict_low_part (match_operand:HI 0 "general_operand_dst" "+r,r,r,r"))
                         (match_operand:HI 1 "general_operand_src" "I,r,i,m"))]
   ""
   "@
     case 0:
       return \"sub.w   %e0,%e0\;sub.w  %f0,%f0\";
     case 1:
-      if (REGNO(operands[0]) < REGNO(operands[1]))
+      if (REGNO (operands[0]) < REGNO (operands[1]))
        return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
-      else 
+      else
        return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
     case 2:
       /* Make sure we don't trample the register we index with.  */
-    
-      if (GET_CODE(operands[1]) == MEM) 
+      if (GET_CODE (operands[1]) == MEM)
        {
-         rtx inside = XEXP (operands[1],0);
-         if  (REG_P (inside)) 
+         rtx inside = XEXP (operands[1], 0);
+         if (REG_P (inside))
            {
-             rn = REGNO(inside);
+             rn = REGNO (inside);
            }
-         else if (GET_CODE (inside) == PLUS) 
+         else if (GET_CODE (inside) == PLUS)
            {
-             rtx lhs = XEXP (inside,0);
-             rtx rhs = XEXP (inside,1);
+             rtx lhs = XEXP (inside, 0);
+             rtx rhs = XEXP (inside, 1);
              if (REG_P (lhs)) rn = REGNO (lhs);
              if (REG_P (rhs)) rn = REGNO (rhs);
            }
        }
-      if (rn == REGNO (operands[0]))   
+      if (rn == REGNO (operands[0]))
        {
          /* Move the second word first.  */
          return \"mov.w        %f1,%f0\;mov.w  %e1,%e0\";
        }
-      else 
+      else
        {
          /* See if either half is zero.  If so, use sub.w to clear
             that half.  */
-       if (GET_CODE (operands[1]) == CONST_INT)
-         {
-           if ((INTVAL (operands[1]) & 0xffff) == 0)
-             return \"mov.w    %e1,%e0\;sub.w  %f0,%f0\";
-           if (((INTVAL (operands[1]) >> 16) & 0xffff) == 0)
-             return \"sub.w    %e0,%e0\;mov.w  %f1,%f0\";
-         }
-       return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
+         if (GET_CODE (operands[1]) == CONST_INT)
+           {
+             if ((INTVAL (operands[1]) & 0xffff) == 0)
+               return \"mov.w  %e1,%e0\;sub.w  %f0,%f0\";
+             if (((INTVAL (operands[1]) >> 16) & 0xffff) == 0)
+               return \"sub.w  %e0,%e0\;mov.w  %f1,%f0\";
+           }
+         return \"mov.w        %e1,%e0\;mov.w  %f1,%f0\";
        }
     case 3:
-       return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
+      return \"mov.w   %e1,%e0\;mov.w  %f1,%f0\";
     case 4:
       return \"mov.w   %f1,%T0\;mov.w  %e1,%T0\";
     case 5:
       return \"mov.w   %T1,%e0\;mov.w  %T1,%f0\";
     default:
-      abort();
+      abort ();
     }
 }"
   [(set_attr "length" "4,4,8,8,4,4")
        || register_operand (operands[1], SFmode))"
   "*
 {
-  /* Copy of the movsi stuff */
+  /* Copy of the movsi stuff */
   int rn = -1;
   switch (which_alternative)
     {
     case 0:
       return \"sub.w   %e0,%e0\;sub.w  %f0,%f0\";
     case 1:
-      if (REGNO(operands[0]) < REGNO(operands[1]))
+      if (REGNO (operands[0]) < REGNO (operands[1]))
        return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
-      else 
+      else
        return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
     case 2:
       /* Make sure we don't trample the register we index with.  */
-    
-      if (GET_CODE (operands[1]) == MEM) 
+      if (GET_CODE (operands[1]) == MEM)
        {
-         rtx inside = XEXP (operands[1],0);
+         rtx inside = XEXP (operands[1], 0);
          if (REG_P (inside))
            {
              rn = REGNO (inside);
            }
-         else if (GET_CODE (inside) == PLUS) 
+         else if (GET_CODE (inside) == PLUS)
            {
-             rtx lhs = XEXP (inside,0);
-             rtx rhs = XEXP (inside,1);
+             rtx lhs = XEXP (inside, 0);
+             rtx rhs = XEXP (inside, 1);
              if (REG_P (lhs)) rn = REGNO (lhs);
              if (REG_P (rhs)) rn = REGNO (rhs);
            }
        }
       if (rn == REGNO (operands[0]))
-       {
-         /* move the second word first */
-         return \"mov.w        %f1,%f0\;mov.w  %e1,%e0\";
-       }
-      else 
-       {
-         return \"mov.w        %e1,%e0\;mov.w  %f1,%f0\";
-       }
-    
+       /* Move the second word first.  */
+       return \"mov.w  %f1,%f0\;mov.w  %e1,%e0\";
+      else
+       /* Move the first word first.  */
+       return \"mov.w  %e1,%e0\;mov.w  %f1,%f0\";
+
     case 3:
       return \"mov.w   %e1,%e0\;mov.w  %f1,%f0\";
     case 4:
     case 5:
       return \"mov.w   %T1,%e0\;mov.w  %T1,%f0\";
     default:
-      abort();
+      abort ();
     }
 }"
   [(set_attr "length" "4,4,8,8,4,4")
    (set_attr "cc" "clobber")])
 
 (define_insn "movsi_h8300hs"
-  [(set (match_operand:SI 0 "general_operand_dst" "=r,r,r,m,<,r,*a,*a,r")
-       (match_operand:SI 1 "general_operand_src" "I,r,im,r,r,>,I,r,*a"))]
+  [(set (match_operand:SI 0 "general_operand_dst" "=r,r,r,r,m,<,r,*a,*a,r")
+       (match_operand:SI 1 "general_operand_src" "I,r,i,m,r,r,>,I,r,*a"))]
   "(TARGET_H8300S || TARGET_H8300H)
    && (register_operand (operands[0], SImode)
-       || register_operand (operands[1], SImode))"
+       || register_operand (operands[1], SImode))
+   && !(GET_CODE (operands[0]) == MEM
+       && GET_CODE (XEXP (operands[0], 0)) == PRE_DEC
+       && GET_CODE (XEXP (XEXP (operands[0], 0), 0)) == REG
+       && GET_CODE (operands[1]) == REG
+       && REGNO (XEXP (XEXP (operands[0], 0), 0)) == REGNO (operands[1]))"
   "*
 {
   switch (which_alternative)
     {
     case 0:
       return \"sub.l   %S0,%S0\";
-    case 6:
-      return \"clrmac\";
     case 7:
-      return \"clrmac\;ldmac %1,macl\";
+      return \"clrmac\";
     case 8:
+      return \"clrmac\;ldmac %1,macl\";
+    case 9:
       return \"stmac   macl,%0\";
     default:
       if (GET_CODE (operands[1]) == CONST_INT)
             number to zero in one of the two low bytes.  */
          if (val == (val & 0xff))
            {
-             operands[1] = GEN_INT ((char)val & 0xff);
-             return \"sub.l %S0,%S0\;add.b %1,%w0\";
+             operands[1] = GEN_INT ((char) val & 0xff);
+             return \"sub.l\\t%S0,%S0\;add.b\\t%1,%w0\";
            }
-     
+
          if (val == (val & 0xff00))
            {
-             operands[1] = GEN_INT ((char)(val >> 8) & 0xff);
-             return \"sub.l %S0,%S0\;add.b %1,%x0\";
+             operands[1] = GEN_INT ((char) (val >> 8) & 0xff);
+             return \"sub.l\\t%S0,%S0\;add.b\\t%1,%x0\";
            }
 
-         /* Now look for small negative numbers.  We can subtract them
-            from zero to get the desired constant.  */
-         if (val == -4 || val == -2 || val == -1)
+         /* Look for constants that can be obtained by subs, inc, and
+            dec to 0.  */
+         switch (val & 0xffffffff)
            {
-             operands[1] = GEN_INT (-INTVAL (operands[1]));
-             return \"sub.l %S0,%S0\;subs %1,%S0\";
+           case 0xffffffff:
+             return \"sub.l\\t%S0,%S0\;subs\\t#1,%S0\";
+           case 0xfffffffe:
+             return \"sub.l\\t%S0,%S0\;subs\\t#2,%S0\";
+           case 0xfffffffc:
+             return \"sub.l\\t%S0,%S0\;subs\\t#4,%S0\";
+
+           case 0x0000ffff:
+             return \"sub.l\\t%S0,%S0\;dec.w\\t#1,%f0\";
+           case 0x0000fffe:
+             return \"sub.l\\t%S0,%S0\;dec.w\\t#2,%f0\";
+
+           case 0xffff0000:
+             return \"sub.l\\t%S0,%S0\;dec.w\\t#1,%e0\";
+           case 0xfffe0000:
+             return \"sub.l\\t%S0,%S0\;dec.w\\t#2,%e0\";
+
+           case 0x00010000:
+             return \"sub.l\\t%S0,%S0\;inc.w\\t#1,%e0\";
+           case 0x00020000:
+             return \"sub.l\\t%S0,%S0\;inc.w\\t#2,%e0\";
            }
        }
     }
    return \"mov.l      %S1,%S0\";
 }"
-  [(set_attr "length" "2,2,10,10,4,4,2,6,4")
-   (set_attr "cc" "set_zn,set_znv,set_znv,set_znv,set_znv,set_znv,none_0hit,none_0hit,set_znv")])
+  [(set_attr "length" "2,2,6,10,10,4,4,2,6,4")
+   (set_attr "cc" "set_zn,set_znv,clobber,set_znv,set_znv,set_znv,set_znv,none_0hit,none_0hit,set_znv")])
 
 (define_insn "movsf_h8300h"
   [(set (match_operand:SF 0 "general_operand_dst" "=r,r,r,m,<,r")
 ;; ----------------------------------------------------------------------
 
 (define_insn ""
-  [(set (cc0) (zero_extract:QI (match_operand:QI 0 "bit_memory_operand" "rU")
+  [(set (cc0) (zero_extract:HI (match_operand:QI 0 "bit_memory_operand" "r,U")
                               (const_int 1)
-                              (match_operand:QI 1 "const_int_operand" "n")))]
-  ""
-  "btst        %Z1,%R0"
-  [(set_attr "length" "2")
-   (set_attr "cc" "set_zn")])
+                              (match_operand 1 "const_int_operand" "n,n")))]
+  "TARGET_H8300"
+  "btst        %Z1,%Y0"
+  [(set_attr "length" "2,4")
+   (set_attr "cc" "set_zn,set_zn")])
 
 (define_insn ""
-  [(set (cc0) (zero_extract:HI (match_operand:QI 0 "bit_memory_operand" "rU")
+  [(set (cc0) (zero_extract:HI (match_operand:HI 0 "register_operand" "r")
                               (const_int 1)
-                              (match_operand:QI 1 "const_int_operand" "n")))]
-  ""
+                              (match_operand 1 "const_int_operand" "n")))]
+  "TARGET_H8300"
   "btst        %Z1,%Y0"
   [(set_attr "length" "2")
    (set_attr "cc" "set_zn")])
 
 (define_insn ""
-  [(set (cc0) (zero_extract:SI (match_operand:QI 0 "bit_memory_operand" "rU")
+  [(set (cc0) (zero_extract:SI (match_operand:QI 0 "bit_memory_operand" "r,U")
                               (const_int 1)
-                              (match_operand:QI 1 "const_int_operand" "n")))]
-  ""
+                              (match_operand 1 "const_int_operand" "n,n")))]
+  "TARGET_H8300H || TARGET_H8300S"
   "btst        %Z1,%Y0"
-  [(set_attr "length" "2")
-   (set_attr "cc" "set_zn")])
+  [(set_attr "length" "2,8")
+   (set_attr "cc" "set_zn,set_zn")])
 
 (define_insn ""
-  [(set (cc0) (zero_extract:QI (match_operand:HI 0 "register_operand" "r")
+  [(set (cc0) (zero_extract:SI (match_operand:SI 0 "register_operand" "r")
                               (const_int 1)
-                              (match_operand:HI 1 "const_int_operand" "n")))]
-  ""
-  "btst        %Z1,%R0"
+                              (match_operand 1 "const_int_operand" "n")))]
+  "(TARGET_H8300H || TARGET_H8300S)
+   && INTVAL (operands[1]) <= 15"
+  "btst        %Z1,%Y0"
   [(set_attr "length" "2")
    (set_attr "cc" "set_zn")])
-  
+
 (define_insn ""
-  [(set (cc0) (zero_extract:HI (match_operand:HI 0 "register_operand" "r")
-                              (const_int 1)
-                              (match_operand:HI 1 "const_int_operand" "n")))]
+  [(set (cc0)
+       (and:HI (match_operand:HI 0 "register_operand" "r")
+               (match_operand:HI 1 "single_one_operand" "n")))]
   ""
-  "btst        %Z1,%Y0"
+  "*
+{
+  operands[1] = GEN_INT (INTVAL (operands[1]) & 0xffff);
+  if (INTVAL (operands[1]) > 128)
+    {
+      operands[1] = GEN_INT (INTVAL (operands[1]) >> 8);
+      return \"btst\\t%V1,%t0\";
+    }
+  return \"btst\\t%V1,%s0\";
+}"
   [(set_attr "length" "2")
    (set_attr "cc" "set_zn")])
 
 (define_insn ""
-  [(set (cc0) (zero_extract:SI (match_operand:HI 0 "register_operand" "r")
-                              (const_int 1)
-                              (match_operand:HI 1 "const_int_operand" "n")))]
-  ""
-  "btst        %Z1,%Y0"
+  [(set (cc0)
+       (and:SI (match_operand:SI 0 "register_operand" "r")
+               (match_operand:SI 1 "single_one_operand" "n")))]
+  "(TARGET_H8300H || TARGET_H8300S)
+   && (INTVAL (operands[1]) & 0xffff) != 0"
+  "*
+{
+  operands[1] = GEN_INT (INTVAL (operands[1]) & 0xffff);
+  if (INTVAL (operands[1]) > 128)
+    {
+      operands[1] = GEN_INT (INTVAL (operands[1]) >> 8);
+      return \"btst\\t%V1,%x0\";
+    }
+  return \"btst\\t%V1,%w0\";
+}"
   [(set_attr "length" "2")
    (set_attr "cc" "set_zn")])
 
   [(set_attr "length" "2")
    (set_attr "cc" "set_znv")])
 
+(define_insn ""
+  [(set (cc0)
+       (and:HI (match_operand:HI 0 "register_operand" "r")
+               (const_int -256)))]
+  ""
+  "mov.b       %t0,%t0"
+  [(set_attr "length" "2")
+   (set_attr "cc" "set_znv")])
+
 (define_insn "tstsi"
   [(set (cc0) (match_operand:SI 0 "register_operand" "r"))]
   "TARGET_H8300H || TARGET_H8300S"
   [(set_attr "length" "2")
    (set_attr "cc" "set_znv")])
 
+(define_insn ""
+  [(set (cc0)
+       (and:SI (match_operand:SI 0 "register_operand" "r")
+               (const_int -65536)))]
+  ""
+  "mov.w       %e0,%e0"
+  [(set_attr "length" "2")
+   (set_attr "cc" "set_znv")])
+
 (define_insn "cmpqi"
   [(set (cc0)
        (compare:QI (match_operand:QI 0 "register_operand" "r")
   "
 {
   /* Force operand1 into a register if we're compiling
-     for the h8/300.  */
+     for the H8/300.  */
   if (GET_CODE (operands[1]) != REG && TARGET_H8300)
     operands[1] = force_reg (HImode, operands[1]);
 }")
                 (match_operand:HI 2 "nonmemory_operand" "L,N,n,r,r")))]
   "TARGET_H8300"
   "@
-   adds        %2,%A0
-   subs        %G2,%A0
-   add.b       %s2,%s0\;addx   %t2,%t0 
+   adds        %2,%T0
+   subs        %G2,%T0
+   add.b       %s2,%s0\;addx   %t2,%t0
    add.w       %T2,%T0
-   mov.w        %T1,%T0\;add.w  %T2,%T0"
+   mov.w       %T1,%T0\;add.w  %T2,%T0"
   [(set_attr "length" "2,2,4,2,6")
    (set_attr "cc" "none_0hit,none_0hit,clobber,set_zn,set_zn")])
 
 (define_insn ""
-  [(set (match_operand:HI 0 "register_operand" "=r,r,r,r,r,r")
-       (plus:HI (match_operand:HI 1 "register_operand" "%0,0,0,0,0,0")
-                (match_operand:HI 2 "nonmemory_operand" "K,T,L,N,n,r")))]
+  [(set (match_operand:HI 0 "register_operand" "=r,r,r,r")
+       (plus:HI (match_operand:HI 1 "register_operand" "%0,0,0,0")
+                (match_operand:HI 2 "nonmemory_operand" "L,N,n,r")))]
   "TARGET_H8300H || TARGET_H8300S"
   "@
-   inc.w       %2,%T0
-   dec.w       %G2,%T0
-   adds        %2,%A0
-   subs        %G2,%A0
+   adds        %2,%S0
+   subs        %G2,%S0
    add.w       %T2,%T0
    add.w       %T2,%T0"
-  [(set_attr "length" "2,2,2,2,4,2")
-   (set_attr "cc" "set_znv,set_znv,none_0hit,none_0hit,set_zn,set_zn")])
+  [(set_attr "length" "2,2,4,2")
+   (set_attr "cc" "none_0hit,none_0hit,set_zn,set_zn")])
 
 (define_split
   [(set (match_operand:HI 0 "register_operand" "")
    (set_attr "cc" "clobber")])
 
 (define_insn "addsi_h8300h"
-  [(set (match_operand:SI 0 "register_operand" "=r,r,r,r,r,r")
-       (plus:SI (match_operand:SI 1 "register_operand" "%0,0,0,0,0,0")
-                (match_operand:SI 2 "nonmemory_operand" "K,T,L,N,i,r")))]
+  [(set (match_operand:SI 0 "register_operand" "=r,r,r,r")
+       (plus:SI (match_operand:SI 1 "register_operand" "%0,0,0,0")
+                (match_operand:SI 2 "nonmemory_operand" "L,N,i,r")))]
   "TARGET_H8300H || TARGET_H8300S"
   "@
-   inc.l       %2,%S0
-   dec.l       %G2,%S0
-   adds        %2,%A0
-   subs        %G2,%A0
+   adds        %2,%S0
+   subs        %G2,%S0
    add.l       %S2,%S0
    add.l       %S2,%S0"
-  [(set_attr "length" "2,2,2,2,6,2")
-   (set_attr "cc" "set_znv,set_znv,none_0hit,none_0hit,set_zn,set_zn")])
+  [(set_attr "length" "2,2,6,2")
+   (set_attr "cc" "none_0hit,none_0hit,set_zn,set_zn")])
 
 (define_split
   [(set (match_operand:SI 0 "register_operand" "")
 ;; ----------------------------------------------------------------------
 
 (define_insn "subqi3"
-  [(set (match_operand:QI 0 "register_operand" "=r,r")
-       (minus:QI (match_operand:QI 1 "register_operand" "0,0")
-                 (match_operand:QI 2 "nonmemory_operand" "r,n")))]
+  [(set (match_operand:QI 0 "register_operand" "=r")
+       (minus:QI (match_operand:QI 1 "register_operand" "0")
+                 (match_operand:QI 2 "register_operand" "r")))]
   ""
-  "@
-   sub.b       %X2,%X0
-   add.b       %G2,%X0"
+  "sub.b       %X2,%X0"
   [(set_attr "length" "2")
    (set_attr "cc" "set_zn")])
 
 ;; MULTIPLY INSTRUCTIONS
 ;; ----------------------------------------------------------------------
 
-;; Note that the h8/300 can only handle umulqihi3.
+;; Note that the H8/300 can only handle umulqihi3.
 
 (define_insn "mulqihi3"
   [(set (match_operand:HI 0 "register_operand" "=r")
-       (mult:HI (sign_extend:HI (match_operand:QI 1 "general_operand" "%0"))
+       (mult:HI (sign_extend:HI (match_operand:QI 1 "register_operand" "%0"))
                 (sign_extend:HI (match_operand:QI 2 "register_operand" "r"))))]
   "TARGET_H8300H || TARGET_H8300S"
   "mulxs.b     %X2,%T0"
 
 (define_insn "mulhisi3"
   [(set (match_operand:SI 0 "register_operand" "=r")
-       (mult:SI (sign_extend:SI (match_operand:HI 1 "general_operand" "%0"))
+       (mult:SI (sign_extend:SI (match_operand:HI 1 "register_operand" "%0"))
                 (sign_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
   "TARGET_H8300H || TARGET_H8300S"
   "mulxs.w     %T2,%S0"
 
 (define_insn "umulqihi3"
   [(set (match_operand:HI 0 "register_operand" "=r")
-       (mult:HI (zero_extend:HI (match_operand:QI 1 "general_operand" "%0"))
+       (mult:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "%0"))
                 (zero_extend:HI (match_operand:QI 2 "register_operand" "r"))))]
   ""
   "mulxu       %X2,%T0"
 
 (define_insn "umulhisi3"
   [(set (match_operand:SI 0 "register_operand" "=r")
-       (mult:SI (zero_extend:SI (match_operand:HI 1 "general_operand" "%0"))
+       (mult:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "%0"))
                 (zero_extend:SI (match_operand:HI 2 "register_operand" "r"))))]
   "TARGET_H8300H || TARGET_H8300S"
   "mulxu.w     %T2,%S0"
            (mem:HI (post_inc:SI (match_operand:SI 1 "register_operand" "r"))))
          (sign_extend:SI
            (mem:HI (post_inc:SI (match_operand:SI 2 "register_operand" "r"))))))]
-  "TARGET_H8300S"
+  "TARGET_MAC"
   "clrmac\;mac @%2+,@%1+"
   [(set_attr "length" "6")
    (set_attr "cc" "none_0hit")])
 
 (define_insn ""
   [(set (match_operand:SI 0 "register_operand" "=a")
-       (plus (mult:SI
+       (plus:SI (mult:SI
          (sign_extend:SI (mem:HI
            (post_inc:SI (match_operand:SI 1 "register_operand" "r"))))
          (sign_extend:SI (mem:HI
            (post_inc:SI (match_operand:SI 2 "register_operand" "r")))))
              (match_operand:SI 3 "register_operand" "0")))]
-  "TARGET_H8300S"
+  "TARGET_MAC"
   "mac @%2+,@%1+"
   [(set_attr "length" "4")
    (set_attr "cc" "none_0hit")])
 
 ;; ----------------------------------------------------------------------
-;; DIVIDE INSTRUCTIONS
+;; DIVIDE/MOD INSTRUCTIONS
 ;; ----------------------------------------------------------------------
 
-(define_insn "udivqi3"
+(define_insn "udivmodqi4"
   [(set (match_operand:QI 0 "register_operand" "=r")
        (truncate:QI
          (udiv:HI
-           (match_operand:HI 1 "general_operand" "0")
-           (zero_extend:HI (match_operand:QI 2 "register_operand" "r")))))]
-  ""
-  "divxu       %X2,%T0"
-  [(set_attr "length" "2")
-   (set_attr "cc" "clobber")])
-
-;; ??? Will divxu always work here?
-
-(define_insn "divqi3"
-  [(set (match_operand:QI 0 "register_operand" "=r")
-       (truncate:QI
-         (div:HI
-           (match_operand:HI 1 "general_operand" "0")
-           (sign_extend:HI (match_operand:QI 2 "register_operand" "r")))))]
-  ""
-  "divxu       %X2,%T0"
-  [(set_attr "length" "2")
-   (set_attr "cc" "clobber")])
-
-(define_insn "udivhi3"
-  [(set (match_operand:HI 0 "register_operand" "=r")
-       (truncate:HI
-         (udiv:SI
-           (match_operand:SI 1 "general_operand" "0")
-           (zero_extend:SI (match_operand:HI 2 "register_operand" "r")))))]
-  "TARGET_H8300H || TARGET_H8300S"
-  "divxu.w     %T2,%S0"
-  [(set_attr "length" "2")
-   (set_attr "cc" "clobber")])
-
-(define_insn "divhi3"
-  [(set (match_operand:HI 0 "register_operand" "=r")
-       (truncate:HI
-         (div:SI
-           (match_operand:SI 1 "general_operand" "0")
-           (sign_extend:SI (match_operand:HI 2 "register_operand" "r")))))]
-  "TARGET_H8300H || TARGET_H8300S"
-  "divxs.w     %T2,%S0"
-  [(set_attr "length" "4")
-   (set_attr "cc" "clobber")])
-
-;; ----------------------------------------------------------------------
-;; MOD INSTRUCTIONS
-;; ----------------------------------------------------------------------
-
-(define_insn "umodqi3"
-  [(set (match_operand:QI 0 "register_operand" "=r")
+           (match_operand:HI 1 "register_operand" "0")
+           (zero_extend:HI (match_operand:QI 2 "register_operand" "r")))))
+   (set (match_operand:QI 3 "register_operand" "=r")
        (truncate:QI
          (umod:HI
-           (match_operand:HI 1 "general_operand" "0")
-           (zero_extend:HI (match_operand:QI 2 "register_operand" "r")))))]
+           (match_dup 1)
+           (zero_extend:HI (match_dup 2)))))]
   ""
-  "divxu       %X2,%T0\;mov %t0,%s0"
+  "*
+{
+  if (find_reg_note (insn, REG_UNUSED, operands[3]))
+    return \"divxu.b\\t%X2,%T0\";
+  else
+    return \"divxu.b\\t%X2,%T0\;mov.b\\t%t0,%s3\";
+}"
   [(set_attr "length" "4")
    (set_attr "cc" "clobber")])
 
-(define_insn "modqi3"
+(define_insn "divmodqi4"
   [(set (match_operand:QI 0 "register_operand" "=r")
        (truncate:QI
+         (div:HI
+           (match_operand:HI 1 "register_operand" "0")
+           (sign_extend:HI (match_operand:QI 2 "register_operand" "r")))))
+   (set (match_operand:QI 3 "register_operand" "=r")
+       (truncate:QI
          (mod:HI
-           (match_operand:HI 1 "general_operand" "0")
-           (sign_extend:HI (match_operand:QI 2 "register_operand" "r")))))]
+           (match_dup 1)
+           (sign_extend:HI (match_dup 2)))))]
   "TARGET_H8300H || TARGET_H8300S"
-  "divxs.b     %X2,%T0\;mov %t0,%s0"
+  "*
+{
+  if (find_reg_note (insn, REG_UNUSED, operands[3]))
+    return \"divxs.b\\t%X2,%T0\";
+  else
+    return \"divxs.b\\t%X2,%T0\;mov.b\\t%t0,%s3\";
+}"
   [(set_attr "length" "6")
    (set_attr "cc" "clobber")])
 
-(define_insn "umodhi3"
+(define_insn "udivmodhi4"
   [(set (match_operand:HI 0 "register_operand" "=r")
        (truncate:HI
+         (udiv:SI
+           (match_operand:SI 1 "register_operand" "0")
+           (zero_extend:SI (match_operand:HI 2 "register_operand" "r")))))
+   (set (match_operand:HI 3 "register_operand" "=r")
+       (truncate:HI
          (umod:SI
-           (match_operand:SI 1 "general_operand" "0")
-           (zero_extend:SI (match_operand:HI 2 "register_operand" "r")))))]
+           (match_dup 1)
+           (zero_extend:SI (match_dup 2)))))]
   "TARGET_H8300H || TARGET_H8300S"
-  "divxu.w     %T2,%S0\;mov %e0,%f0"
+  "*
+{
+  if (find_reg_note (insn, REG_UNUSED, operands[3]))
+    return \"divxu.w\\t%T2,%S0\";
+  else
+    return \"divxu.w\\t%T2,%S0\;mov.w\\t%e0,%f3\";
+}"
   [(set_attr "length" "4")
    (set_attr "cc" "clobber")])
 
-(define_insn "modhi3"
+(define_insn "divmodhi4"
   [(set (match_operand:HI 0 "register_operand" "=r")
        (truncate:HI
+         (div:SI
+           (match_operand:SI 1 "register_operand" "0")
+           (sign_extend:SI (match_operand:HI 2 "register_operand" "r")))))
+   (set (match_operand:HI 3 "register_operand" "=r")
+       (truncate:HI
          (mod:SI
-           (match_operand:SI 1 "general_operand" "0")
-           (sign_extend:SI (match_operand:HI 2 "register_operand" "r")))))]
+           (match_dup 1)
+           (sign_extend:SI (match_dup 2)))))]
   "TARGET_H8300H || TARGET_H8300S"
-  "divxs.w     %T2,%S0\;mov %e0,%f0"
+  "*
+{
+  if (find_reg_note (insn, REG_UNUSED, operands[3]))
+    return \"divxs.w\\t%T2,%S0\";
+  else
+    return \"divxs.w\\t%T2,%S0\;mov.w\\t%e0,%f3\";
+}"
   [(set_attr "length" "6")
    (set_attr "cc" "clobber")])
 \f
 (define_insn ""
   [(set (match_operand:QI 0 "bit_operand" "=r,U")
        (and:QI (match_operand:QI 1 "bit_operand" "%0,0")
-               (match_operand:QI 2 "nonmemory_operand" "rn,O")))]
-  "register_operand (operands[0], QImode) || o_operand (operands[2], QImode)"
+               (match_operand:QI 2 "nonmemory_operand" "rn,n")))]
+  "register_operand (operands[0], QImode)
+   || single_zero_operand (operands[2], QImode)"
   "@
    and %X2,%X0
    bclr        %W2,%R0"
-  [(set_attr "length" "2,4")
+  [(set_attr "length" "2,8")
    (set_attr "adjust_length" "no")
    (set_attr "cc" "set_znv,none_0hit")])
 
   ""
   "
 {
-  if (fix_bit_operand (operands, 'O', AND))
+  if (fix_bit_operand (operands, 0, AND))
     DONE;
 }")
 
-(define_insn "andhi3"
+(define_expand "andhi3"
+  [(set (match_operand:HI 0 "register_operand" "")
+       (and:HI (match_operand:HI 1 "register_operand" "")
+               (match_operand:HI 2 "nonmemory_operand" "")))]
+  ""
+  "")
+
+(define_insn "*andorqi3"
+  [(set (match_operand:QI 0 "register_operand" "=r")
+       (ior:QI (and:QI (match_operand:QI 2 "register_operand" "r")
+                       (match_operand:QI 3 "single_one_operand" "n"))
+               (match_operand:QI 1 "register_operand" "0")))]
+  ""
+  "bld\\t%V3,%X2\;bor\\t%V3,%X0\;bst\\t%V3,%X0"
+  [(set_attr "length" "6")
+   (set_attr "cc" "clobber")])
+
+(define_insn "*andorhi3"
   [(set (match_operand:HI 0 "register_operand" "=r")
-       (and:HI (match_operand:HI 1 "register_operand" "%0")
-               (match_operand:HI 2 "nonmemory_operand" "rn")))]
+       (ior:HI (and:HI (match_operand:HI 2 "register_operand" "r")
+                       (match_operand:HI 3 "single_one_operand" "n"))
+               (match_operand:HI 1 "register_operand" "0")))]
   ""
   "*
 {
-  if (GET_CODE (operands[2]) == CONST_INT)
+  operands[3] = GEN_INT (INTVAL (operands[3]) & 0xffff);
+  if (INTVAL (operands[3]) > 128)
     {
-      int i = INTVAL (operands[2]);
-
-      if ((i & 0x00ff) != 0x00ff) 
-       output_asm_insn (\"and  %s2,%s0\", operands);
-      if ((i & 0xff00) != 0xff00) 
-       output_asm_insn (\"and  %t2,%t0\", operands);
-      return \"\";
+      operands[3] = GEN_INT (INTVAL (operands[3]) >> 8);
+      return \"bld\\t%V3,%t2\;bor\\t%V3,%t0\;bst\\t%V3,%t0\";
     }
-  if (TARGET_H8300H || TARGET_H8300S)
-    return \"and.w %T2,%T0\";
-  return \"and %s2,%s0\;and    %t2,%t0;\";
+  return \"bld\\t%V3,%s2\;bor\\t%V3,%s0\;bst\\t%V3,%s0\";
 }"
-  [(set_attr "length" "4")
+  [(set_attr "length" "6")
    (set_attr "cc" "clobber")])
 
-(define_insn "andsi3"
+(define_insn "*andorsi3"
   [(set (match_operand:SI 0 "register_operand" "=r")
-       (and:SI (match_operand:SI 1 "register_operand" "%0")
-               (match_operand:SI 2 "nonmemory_operand" "rn")))]
-  ""
+       (ior:SI (and:SI (match_operand:SI 2 "register_operand" "r")
+                       (match_operand:SI 3 "single_one_operand" "n"))
+               (match_operand:SI 1 "register_operand" "0")))]
+  "(INTVAL (operands[3]) & 0xffff) != 0"
   "*
 {
-  if (GET_CODE (operands[2]) == CONST_INT)
+  operands[3] = GEN_INT (INTVAL (operands[3]) & 0xffff);
+  if (INTVAL (operands[3]) > 128)
     {
-      int i = INTVAL (operands[2]);
-      int upper_cleared, lower_cleared;
-
-      /* The h8300h can't do byte-wise operations on the
-        upper 16bits of 32bit registers.  However, if
-        those bits aren't going to change, or they're
-        going to be zero'd out, then we can work on the
-        low-order bits.  */
-      if ((TARGET_H8300H || TARGET_H8300S)
-         && ((i & 0xffff0000) != 0xffff0000
-             || (i & 0xffff0000) == 0x00000000))
-        return \"and.l %S2,%S0\";
-
-      lower_cleared = 0;
-      if ((i & 0x0000ffff) == 0x00000000)
-       {
-         output_asm_insn (\"sub.w      %f0,%f0\", operands);
-         lower_cleared = 1;
-       }
-
-      upper_cleared = 0;
-      if ((i & 0xffff0000) == 0x00000000)
-       {
-         output_asm_insn (\"sub.w      %e0,%e0\", operands);
-         upper_cleared = 1;
-       }
-
-      if ((i & 0x000000ff) != 0x000000ff && !lower_cleared)
-       output_asm_insn (\"and  %w2,%w0\", operands);
-      if ((i & 0x0000ff00) != 0x0000ff00 && !lower_cleared)
-       output_asm_insn (\"and  %x2,%x0\", operands);
-      if ((i & 0x00ff0000) != 0x00ff0000 && !upper_cleared) 
-       output_asm_insn (\"and  %y2,%y0\", operands);
-      if ((i & 0xff000000) != 0xff000000 && !upper_cleared) 
-       output_asm_insn (\"and  %z2,%z0\", operands);
-      return \"\";
+      operands[3] = GEN_INT (INTVAL (operands[3]) >> 8);
+      return \"bld\\t%V3,%x2\;bor\\t%V3,%x0\;bst\\t%V3,%x0\";
     }
-  if (TARGET_H8300H || TARGET_H8300S)
-    return \"and.l     %S2,%S0\";
-  return \"and %w2,%w0\;and    %x2,%x0\;and    %y2,%y0\;and    %z2,%z0\;\";
+  return \"bld\\t%V3,%w2\;bor\\t%V3,%w0\;bst\\t%V3,%w0\";
 }"
-  [(set_attr "length" "8")
+  [(set_attr "length" "6")
    (set_attr "cc" "clobber")])
 
+(define_expand "andsi3"
+  [(set (match_operand:SI 0 "register_operand" "")
+       (and:SI (match_operand:SI 1 "register_operand" "")
+               (match_operand:SI 2 "nonmemory_operand" "")))]
+  ""
+  "")
 
 ;; ----------------------------------------------------------------------
 ;; OR INSTRUCTIONS
 (define_insn ""
   [(set (match_operand:QI 0 "bit_operand" "=r,U")
        (ior:QI (match_operand:QI 1 "bit_operand" "%0,0")
-               (match_operand:QI 2 "nonmemory_operand" "rn,P")))]
-  "register_operand (operands[0], QImode) || p_operand (operands[2], QImode)"
+               (match_operand:QI 2 "nonmemory_operand" "rn,n")))]
+  "register_operand (operands[0], QImode)
+   || single_one_operand (operands[2], QImode)"
   "@
-   or  %X2,%X0
-   bset        %V2,%R0"
-  [(set_attr "length" "2,4")
+   or\\t%X2,%X0
+   bset\\t%V2,%R0"
+  [(set_attr "length" "2,8")
    (set_attr "adjust_length" "no")
    (set_attr "cc" "set_znv,none_0hit")])
 
 (define_expand "iorqi3"
-  [(set (match_operand:QI 0 "bit_operand" "=r,U")
-       (ior:QI (match_operand:QI 1 "bit_operand" "%0,0")
-               (match_operand:QI 2 "nonmemory_operand" "rn,P")))]
+  [(set (match_operand:QI 0 "bit_operand" "")
+       (ior:QI (match_operand:QI 1 "bit_operand" "")
+               (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
   "
 {
-  if (fix_bit_operand (operands, 'P', IOR))
+  if (fix_bit_operand (operands, 1, IOR))
     DONE;
 }")
 
-(define_insn "iorhi3"
-  [(set (match_operand:HI 0 "general_operand" "=r,r")
-       (ior:HI (match_operand:HI 1 "general_operand" "%0,0")
-               (match_operand:HI 2 "general_operand" "J,rn")))]
+(define_expand "iorhi3"
+  [(set (match_operand:HI 0 "register_operand" "")
+       (ior:HI (match_operand:HI 1 "register_operand" "")
+               (match_operand:HI 2 "nonmemory_operand" "")))]
   ""
-  "*
-{
-  if (GET_CODE (operands[2]) == CONST_INT)
-    {
-      int i = INTVAL (operands[2]);
-
-      if ((i & 0x00ff) != 0) 
-       output_asm_insn (\"or   %s2,%s0\", operands);
-      if ((i & 0xff00) != 0) 
-       output_asm_insn (\"or   %t2,%t0\", operands);
-      return \"\";
-    }
-  if (TARGET_H8300H || TARGET_H8300S)
-    return \"or.w      %T2,%T0\";
-  return \"or  %s2,%s0\;or     %t2,%t0; %2 or2\";
-}"
-  [(set_attr "length" "2,4")
-   (set_attr "cc" "clobber,clobber")])
+  "")
 
-(define_insn "iorsi3"
-  [(set (match_operand:SI 0 "register_operand" "=r,r")
-       (ior:SI (match_operand:SI 1 "register_operand" "%0,0")
-               (match_operand:SI 2 "nonmemory_operand" "J,rn")))]
+(define_expand "iorsi3"
+  [(set (match_operand:SI 0 "register_operand" "")
+       (ior:SI (match_operand:SI 1 "register_operand" "")
+               (match_operand:SI 2 "nonmemory_operand" "")))]
   ""
-  "*
-{
-  if (GET_CODE (operands[2]) == CONST_INT)
-    {
-      int i = INTVAL (operands[2]);
-
-      /* The h8300h can't do byte-wise operations on the
-        upper 16bits of 32bit registers.  However, if
-        those bits aren't going to change, then we can
-        work on the low-order bits.  */
-      if ((TARGET_H8300H || TARGET_H8300S)
-         && (i & 0xffff0000) != 0x00000000)
-        return \"or.l  %S2,%S0\";
-       
-      if ((i & 0x000000ff) != 0) 
-       output_asm_insn (\"or   %w2,%w0\", operands);
-      if ((i & 0x0000ff00) != 0) 
-       output_asm_insn (\"or   %x2,%x0\", operands);
-      if ((i & 0x00ff0000) != 0) 
-       output_asm_insn (\"or   %y2,%y0\", operands);
-      if ((i & 0xff000000) != 0) 
-       output_asm_insn (\"or   %z2,%z0\", operands);
-      return \"\";
-    }
-  if (TARGET_H8300H || TARGET_H8300S)
-    return \"or.l      %S2,%S0\";
-  return \"or  %w2,%w0\;or     %x2,%x0\;or     %y2,%y0\;or     %z2,%z0\;\";
-}"
-  [(set_attr "length" "2,8")
-   (set_attr "cc" "clobber,clobber")])
+  "")
 
 ;; ----------------------------------------------------------------------
 ;; XOR INSTRUCTIONS
 (define_insn ""
   [(set (match_operand:QI 0 "bit_operand" "=r,U")
        (xor:QI (match_operand:QI 1 "bit_operand" "%0,0")
-               (match_operand:QI 2 "nonmemory_operand" "rn,P")))]
-  "register_operand (operands[0], QImode) || p_operand (operands[2], QImode)"
+               (match_operand:QI 2 "nonmemory_operand" "rn,n")))]
+  "register_operand (operands[0], QImode)
+   || single_one_operand (operands[2], QImode)"
   "@
-   xor %X2,%X0
-   bnot        %V2,%R0"
-  [(set_attr "length" "2,4")
+   xor\\t%X2,%X0
+   bnot\\t%V2,%R0"
+  [(set_attr "length" "2,8")
    (set_attr "adjust_length" "no")
    (set_attr "cc" "set_znv,none_0hit")])
 
 (define_expand "xorqi3"
-  [(set (match_operand:QI 0 "bit_operand" "=r,U")
-       (xor:QI (match_operand:QI 1 "bit_operand" "%0,0")
-               (match_operand:QI 2 "nonmemory_operand" "rn,O")))]
+  [(set (match_operand:QI 0 "bit_operand" "")
+       (xor:QI (match_operand:QI 1 "bit_operand" "")
+               (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
   "
 {
-  if (fix_bit_operand (operands, 'O', XOR))
+  if (fix_bit_operand (operands, 1, XOR))
     DONE;
 }")
 
-(define_insn "xorhi3"
-  [(set (match_operand:HI 0 "register_operand" "=r,r")
-       (xor:HI (match_operand:HI 1 "general_operand" "%0,0")
-               (match_operand:HI 2 "nonmemory_operand" "J,rn")))]
+(define_expand "xorhi3"
+  [(set (match_operand:HI 0 "register_operand" "")
+       (xor:HI (match_operand:HI 1 "register_operand" "")
+               (match_operand:HI 2 "nonmemory_operand" "")))]
   ""
-  "*
-{
-  if (GET_CODE (operands[2]) == CONST_INT)
-    {
-      int i = INTVAL (operands[2]);
+  "")
 
-      if ((i & 0x00ff) != 0) 
-       output_asm_insn (\"xor  %s2,%s0\", operands);
-      if ((i & 0xff00) != 0) 
-       output_asm_insn (\"xor  %t2,%t0\", operands);
-      return \"\";
-    }
-  if (TARGET_H8300H || TARGET_H8300S)
-    return \"xor.w     %T2,%T0\";
-  return \"xor %s2,%s0\;xor    %t2,%t0\";
-}"
-  [(set_attr "length" "2,4")
-   (set_attr "cc" "clobber,clobber")])
+(define_expand "xorsi3"
+  [(set (match_operand:SI 0 "register_operand" "")
+       (xor:SI (match_operand:SI 1 "register_operand" "")
+               (match_operand:SI 2 "nonmemory_operand" "")))]
+  ""
+  "")
 
-(define_insn "xorsi3"
-  [(set (match_operand:SI 0 "register_operand" "=r,r")
-       (xor:SI (match_operand:SI 1 "register_operand" "%0,0")
-               (match_operand:SI 2 "nonmemory_operand" "J,rn")))]
+;; ----------------------------------------------------------------------
+;; {AND,IOR,XOR}{HI3,SI3} PATTERNS
+;; ----------------------------------------------------------------------
+
+(define_insn ""
+  [(set (match_operand:HI 0 "register_operand" "=r")
+       (match_operator:HI 3 "bit_operator"
+         [(match_operand:HI 1 "register_operand" "%0")
+          (match_operand:HI 2 "nonmemory_operand" "rn")]))]
   ""
-  "*
-{
-  if (GET_CODE (operands[2]) == CONST_INT)
-    {
-      int i = INTVAL (operands[2]);
-
-      /* The h8300h can't do byte-wise operations on the
-        upper 16bits of 32bit registers.  However, if
-        those bits aren't going to change, then we can
-        work on the low-order bits.  */
-      if ((TARGET_H8300H || TARGET_H8300S)
-         && (i & 0xffff0000) != 0x00000000)
-        return \"xor.l %S2,%S0\";
-
-      if ((i & 0x000000ff) != 0) 
-       output_asm_insn (\"xor  %w2,%w0\", operands);
-      if ((i & 0x0000ff00) != 0) 
-       output_asm_insn (\"xor  %x2,%x0\", operands);
-      if ((i & 0x00ff0000) != 0) 
-       output_asm_insn (\"xor  %y2,%y0\", operands);
-      if ((i & 0xff000000) != 0) 
-       output_asm_insn (\"xor  %z2,%z0\", operands);
-      return \"\";
-    }
-  if (TARGET_H8300H || TARGET_H8300S)
-    return \"xor.l     %S2,%S0\";
-  return \"xor %w2,%w0\;xor    %x2,%x0\;xor    %y2,%y0\;xor    %z2,%z0\;\";
-}"
-  [(set_attr "length" "2,8")
-   (set_attr "cc" "clobber,clobber")])
+  "* return output_logical_op (HImode, operands);"
+  [(set (attr "length")
+       (symbol_ref "compute_logical_op_length (HImode, operands)"))
+   (set (attr "cc")
+       (symbol_ref "compute_logical_op_cc (HImode, operands)"))])
+
+(define_insn ""
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (match_operator:SI 3 "bit_operator"
+         [(match_operand:SI 1 "register_operand" "%0")
+          (match_operand:SI 2 "nonmemory_operand" "rn")]))]
+  ""
+  "* return output_logical_op (SImode, operands);"
+  [(set (attr "length")
+       (symbol_ref "compute_logical_op_length (SImode, operands)"))
+   (set (attr "cc")
+       (symbol_ref "compute_logical_op_cc (SImode, operands)"))])
 \f
 ;; ----------------------------------------------------------------------
 ;; NEGATION INSTRUCTIONS
 
 (define_insn "negqi2"
   [(set (match_operand:QI 0 "register_operand" "=r")
-       (neg:QI (match_operand:QI 1 "general_operand" "0")))]
+       (neg:QI (match_operand:QI 1 "register_operand" "0")))]
   ""
   "neg %X0"
   [(set_attr "length" "2")
    (set_attr "cc" "set_zn")])
 
 (define_expand "neghi2"
-  [(set (match_operand:HI 0 "register_operand" "=r")
-       (neg:HI (match_operand:HI 1 "general_operand" "0")))]
+  [(set (match_operand:HI 0 "register_operand" "")
+       (neg:HI (match_operand:HI 1 "register_operand" "")))]
   ""
   "
 {
 
 (define_expand "neghi2_h8300"
   [(set (match_dup 2)
-       (not:HI (match_operand:HI 1 "register_operand" "r")))
+       (not:HI (match_operand:HI 1 "register_operand" "")))
    (set (match_dup 2) (plus:HI (match_dup 2) (const_int 1)))
-   (set (match_operand:HI 0 "register_operand" "=r")
+   (set (match_operand:HI 0 "register_operand" "")
        (match_dup 2))]
   ""
-  "{ operands[2] = gen_reg_rtx (HImode); }")
+  "operands[2] = gen_reg_rtx (HImode);")
 
 (define_insn "neghi2_h8300h"
   [(set (match_operand:HI 0 "register_operand" "=r")
-       (neg:HI (match_operand:HI 1 "general_operand" "0")))]
+       (neg:HI (match_operand:HI 1 "register_operand" "0")))]
   "TARGET_H8300H || TARGET_H8300S"
   "neg %T0"
   [(set_attr "length" "2")
    (set_attr "cc" "set_zn")])
 
 (define_expand "negsi2"
-  [(set (match_operand:SI 0 "register_operand" "=r")
-       (neg:SI (match_operand:SI 1 "general_operand" "0")))]
+  [(set (match_operand:SI 0 "register_operand" "")
+       (neg:SI (match_operand:SI 1 "register_operand" "")))]
   ""
   "
 {
 
 (define_expand "negsi2_h8300"
   [(set (match_dup 2)
-       (not:SI (match_operand:SI 1 "register_operand" "r")))
+       (not:SI (match_operand:SI 1 "register_operand" "")))
    (set (match_dup 2) (plus:SI (match_dup 2) (const_int 1)))
-   (set (match_operand:SI 0 "register_operand" "=r")
+   (set (match_operand:SI 0 "register_operand" "")
        (match_dup 2))]
   ""
-  "{ operands[2] = gen_reg_rtx(SImode); }")
+  "operands[2] = gen_reg_rtx (SImode);")
 
 (define_insn "negsi2_h8300h"
   [(set (match_operand:SI 0 "register_operand" "=r")
-       (neg:SI (match_operand:SI 1 "general_operand" "0")))]
+       (neg:SI (match_operand:SI 1 "register_operand" "0")))]
   "TARGET_H8300H || TARGET_H8300S"
   "neg %S0"
   [(set_attr "length" "2")
 
 (define_insn "one_cmplqi2"
   [(set (match_operand:QI 0 "register_operand" "=r")
-       (not:QI (match_operand:QI 1 "general_operand" "0")))]
+       (not:QI (match_operand:QI 1 "register_operand" "0")))]
   ""
   "not %X0"
   [(set_attr "length" "2")
    (set_attr "cc" "set_znv")])
 
-(define_insn "one_cmplhi2"
+(define_expand "one_cmplhi2"
   [(set (match_operand:HI 0 "register_operand" "=r")
-       (not:HI (match_operand:HI 1 "general_operand" "0")))]
+       (not:HI (match_operand:HI 1 "register_operand" "0")))]
   ""
-  "*
-{
-  if (TARGET_H8300)
-    return \"not       %s0\;not        %t0\";
-  else
-    return \"not       %T0\";
-}"
+  "")
+
+(define_insn ""
+  [(set (match_operand:HI 0 "register_operand" "=r")
+       (not:HI (match_operand:HI 1 "register_operand" "0")))]
+  "TARGET_H8300"
+  "not %s0\;not        %t0"
   [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 4)
-                     (const_int 2)))])
+   (set_attr "length" "4")])
+
+(define_insn ""
+  [(set (match_operand:HI 0 "register_operand" "=r")
+       (not:HI (match_operand:HI 1 "register_operand" "0")))]
+  "TARGET_H8300H || TARGET_H8300S"
+  "not %T0"
+  [(set_attr "cc" "set_znv")
+   (set_attr "length" "2")])
 
-(define_insn "one_cmplsi2"
+(define_expand "one_cmplsi2"
   [(set (match_operand:SI 0 "register_operand" "=r")
-       (not:SI (match_operand:SI 1 "general_operand" "0")))]
+       (not:SI (match_operand:SI 1 "register_operand" "0")))]
   ""
-  "*
-{
-  if (TARGET_H8300)
-    return \"not       %w0\;not        %x0\;not        %y0\;not        %z0\";
-  else
-    return \"not       %S0\";
-}"
+  "")
+
+(define_insn ""
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (not:SI (match_operand:SI 1 "register_operand" "0")))]
+  "TARGET_H8300"
+  "not %w0\;not        %x0\;not        %y0\;not        %z0"
   [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 8)
-                     (const_int 2)))])
-                       
+   (set_attr "length" "8")])
+
+(define_insn ""
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (not:SI (match_operand:SI 1 "register_operand" "0")))]
+  "TARGET_H8300H || TARGET_H8300S"
+  "not %S0"
+  [(set_attr "cc" "set_znv")
+   (set_attr "length" "2")])
 \f
 ;; ----------------------------------------------------------------------
 ;; JUMP INSTRUCTIONS
 {
   if ((cc_status.flags & CC_OVERFLOW_UNUSABLE) != 0
       && (GET_CODE (operands[1]) == GT
-          || GET_CODE (operands[1]) == GE
-          || GET_CODE (operands[1]) == LE
-          || GET_CODE (operands[1]) == LT))
+         || GET_CODE (operands[1]) == GE
+         || GET_CODE (operands[1]) == LE
+         || GET_CODE (operands[1]) == LT))
     {
       cc_status.flags &= ~CC_OVERFLOW_UNUSABLE;
       return 0;
     }
 
-  if (get_attr_length (insn) == 2) 
+  if (get_attr_length (insn) == 2)
     return \"b%j1      %l0\";
-  else if (get_attr_length (insn) == 4) 
+  else if (get_attr_length (insn) == 4)
     return \"b%j1      %l0:16\";
   else
     return \"b%k1      .Lh8BR%=\;jmp   @%l0\\n.Lh8BR%=:\";
-}" 
+}"
  [(set_attr "type" "branch")
    (set_attr "cc" "none")])
 
 {
   if ((cc_status.flags & CC_OVERFLOW_UNUSABLE) != 0
       && (GET_CODE (operands[1]) == GT
-          || GET_CODE (operands[1]) == GE
-          || GET_CODE (operands[1]) == LE
-          || GET_CODE (operands[1]) == LT))
+         || GET_CODE (operands[1]) == GE
+         || GET_CODE (operands[1]) == LE
+         || GET_CODE (operands[1]) == LT))
     {
       cc_status.flags &= ~CC_OVERFLOW_UNUSABLE;
       return 0;
     }
 
-  if (get_attr_length (insn) == 2) 
+  if (get_attr_length (insn) == 2)
     return \"b%k1      %l0\";
-  else if (get_attr_length (insn) == 4) 
+  else if (get_attr_length (insn) == 4)
     return \"b%k1      %l0:16\";
   else
     return \"b%j1      .Lh8BR%=\;jmp   @%l0\\n.Lh8BR%=:\";
 ;; This is a define expand, because pointers may be either 16 or 32 bits.
 
 (define_expand "tablejump"
-  [(parallel [(set (pc) (match_operand 0 "register_operand" "r"))
+  [(parallel [(set (pc) (match_operand 0 "register_operand" ""))
              (use (label_ref (match_operand 1 "" "")))])]
   ""
   "")
   [(set_attr "cc" "none")
    (set_attr "length" "2")])
 
+(define_insn "tablejump_normal_mode"
+   [(set (pc) (match_operand:HI 0 "register_operand" "r"))
+    (use (label_ref (match_operand 1 "" "")))]
+   "(TARGET_H8300H || TARGET_H8300S) && TARGET_NORMAL_MODE"
+   "jmp @%S0"
+   [(set_attr "cc" "none")
+    (set_attr "length" "2")])
+
 ;; This is a define expand, because pointers may be either 16 or 32 bits.
 
 (define_expand "indirect_jump"
   [(set_attr "cc" "none")
    (set_attr "length" "2")])
 
+(define_insn "indirect_jump_normal_mode"
+  [(set (pc) (match_operand:HI 0 "jump_address_operand" "Vr"))]
+  "(TARGET_H8300H || TARGET_H8300S) && TARGET_NORMAL_MODE"
+  "jmp @%S0"
+  [(set_attr "cc" "none")
+   (set_attr "length" "2")])
+
 ;; Call subroutine with no return value.
 
-;; ??? Even though we use HImode here, this works for the 300h.
+;; ??? Even though we use HImode here, this works on the H8/300H and H8S.
 
 (define_insn "call"
   [(call (match_operand:QI 0 "call_insn_operand" "or")
 {
   if (GET_CODE (XEXP (operands[0], 0)) == SYMBOL_REF
       && SYMBOL_REF_FLAG (XEXP (operands[0], 0)))
-    return \"jsr\\t\@%0:8\";
+    return \"jsr\\t@%0:8\";
   else
     return \"jsr\\t%0\";
 }"
   [(set_attr "cc" "clobber")
    (set (attr "length")
-     (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
-                  (const_int 4)
-                  (const_int 8)))])
+       (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
+                     (const_int 4)
+                     (const_int 8)))])
 
 ;; Call subroutine, returning value in operand 0
 ;; (which must be a hard register).
 
-;; ??? Even though we use HImode here, this works on the 300h.
+;; ??? Even though we use HImode here, this works on the H8/300H and H8S.
 
 (define_insn "call_value"
   [(set (match_operand 0 "" "=r")
 {
   if (GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
       && SYMBOL_REF_FLAG (XEXP (operands[1], 0)))
-    return \"jsr\\t\@%1:8\";
+    return \"jsr\\t@%1:8\";
   else
     return \"jsr\\t%1\";
 }"
   [(set_attr "cc" "clobber")
    (set (attr "length")
-     (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
-                  (const_int 4)
-                  (const_int 8)))])
+       (if_then_else (match_operand:QI 0 "small_call_insn_operand" "")
+                     (const_int 4)
+                     (const_int 8)))])
 
 (define_insn "nop"
   [(const_int 0)]
 ;; EXTEND INSTRUCTIONS
 ;; ----------------------------------------------------------------------
 
-(define_insn "zero_extendqihi2"
+(define_expand "zero_extendqihi2"
+  [(set (match_operand:HI 0 "register_operand" "")
+       (zero_extend:HI (match_operand:QI 1 "general_operand_src" "")))]
+  ""
+  "")
+
+(define_insn ""
   [(set (match_operand:HI 0 "register_operand" "=r,r")
        (zero_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
-  ""
+  "TARGET_H8300"
   "@
   mov.b        #0,%t0
   mov.b        %R1,%s0\;mov.b  #0,%t0"
-  [(set_attr "length" "2,4")
+  [(set_attr "length" "2,10")
    (set_attr "cc" "clobber,clobber")])
 
-;; The compiler can synthesize a 300H variant of this which is
+(define_insn ""
+  [(set (match_operand:HI 0 "register_operand" "=r,r")
+       (zero_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
+  "TARGET_H8300H || TARGET_H8300S"
+  "@
+  extu.w       %T0
+  mov.b        %R1,%s0\;extu.w %T0"
+  [(set_attr "length" "2,10")
+   (set_attr "cc" "set_znv,set_znv")])
+
+;; The compiler can synthesize a H8/300H variant of this which is
 ;; just as efficient as one that we'd create
 (define_insn "zero_extendqisi2"
   [(set (match_operand:SI 0 "register_operand" "=r,r")
        (zero_extend:SI (match_operand:QI 1 "general_operand_src" "0,g>")))]
   "TARGET_H8300"
   "@
-  mov.b        #0,%x0\;sub.w %e0,%e0
-  mov.b        %R1,%w0\;mov.b  #0,%x0\;sub.w %e0,%e0"
+  mov.b        #0,%x0\;sub.w   %e0,%e0
+  mov.b        %R1,%w0\;mov.b  #0,%x0\;sub.w   %e0,%e0"
   [(set_attr "length" "4,6")
    (set_attr "cc" "clobber,clobber")])
 
 (define_expand "zero_extendhisi2"
   [(set (match_operand:SI 0 "register_operand" "")
-       (zero_extend:SI (match_operand:HI 1 "general_operand" "")))]
+       (zero_extend:SI (match_operand:HI 1 "register_operand" "")))]
   ""
-  "
-{
-  if (TARGET_H8300
-      && GET_CODE (operands[1]) != CONST_INT
-      && !optimize)
-    {
-      emit_insn (gen_zero_extendhisi2_h8300 (operands[0], operands[1]));
-      DONE;
-    }
-}")
-
-;; This is used when not optimizing.  It avoids severe code explosion
-;; due to poor register allocation.
-(define_expand "zero_extendhisi2_h8300"
-  [(set (reg:HI 1) (match_operand:HI 1 "general_operand" ""))
-   (set (reg:SI 0) (zero_extend:SI (reg:HI 1)))
-   (set (match_operand:SI 0 "general_operand" "" ) (reg:SI 0))]
-  "TARGET_H8300"
   "")
 
 ;; %e prints the high part of a CONST_INT, not the low part.  Arggh.
   "TARGET_H8300"
   "@
   sub.w        %e0,%e0
-  mov.w %f1,%f0\;sub.w %e0,%e0
-  mov.w %e1,%f0\;sub.w %e0,%e0"
+  mov.w        %f1,%f0\;sub.w  %e0,%e0
+  mov.w        %e1,%f0\;sub.w  %e0,%e0"
   [(set_attr "length" "2,4,4")
    (set_attr "cc" "clobber,clobber,clobber")])
 
 (define_insn ""
-  [(set (match_operand:SI 0 "register_operand" "=r,r")
-       (zero_extend:SI (match_operand:HI 1 "general_operand_src" "0,g>")))]
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (zero_extend:SI (match_operand:HI 1 "register_operand" "0")))]
   "TARGET_H8300H || TARGET_H8300S"
-  "@
-  extu.l       %S0
-  mov.w        %T1,%T0\;extu.l %S0"
-  [(set_attr "length" "2,4")
-   (set_attr "cc" "set_znv,set_znv")])
+  "extu.l      %S0"
+  [(set_attr "length" "2")
+   (set_attr "cc" "set_znv")])
 
 (define_expand "extendqihi2"
   [(set (match_operand:HI 0 "register_operand" "")
-       (sign_extend:HI (match_operand:QI 1 "general_operand" "")))]
+       (sign_extend:HI (match_operand:QI 1 "register_operand" "")))]
   ""
   "")
 
   "@
   bld  #7,%s0\;subx    %t0,%t0
   mov.b        %R1,%s0\;bld    #7,%s0\;subx    %t0,%t0"
-  [(set_attr "length" "4,6")
+  [(set_attr "length" "4,8")
    (set_attr "cc" "clobber,clobber")])
 
 (define_insn ""
-  [(set (match_operand:HI 0 "register_operand" "=r,r")
-       (sign_extend:HI (match_operand:QI 1 "general_operand_src" "0,g>")))]
+  [(set (match_operand:HI 0 "register_operand" "=r")
+       (sign_extend:HI (match_operand:QI 1 "register_operand" "0")))]
   "TARGET_H8300H || TARGET_H8300S"
-  "@
-  exts.w       %T0
-  mov.b        %R1,%s0\;exts.w %T0"
-  [(set_attr "length" "2,4")
-   (set_attr "cc" "set_znv,set_znv")])
+  "exts.w      %T0"
+  [(set_attr "length" "2")
+   (set_attr "cc" "set_znv")])
 
-;; The compiler can synthesize a 300H variant of this which is
+;; The compiler can synthesize a H8/300H variant of this which is
 ;; just as efficient as one that we'd create
 (define_insn "extendqisi2"
   [(set (match_operand:SI 0 "register_operand" "=r,r")
   "TARGET_H8300"
   "@
   bld  #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0
-  mov.b %R1,%w0\;bld   #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0"
+  mov.b        %R1,%w0\;bld    #7,%w0\;subx    %x0,%x0\;subx   %y0,%y0\;subx   %z0,%z0"
   [(set_attr "length" "8,10")
    (set_attr "cc" "clobber,clobber")])
 
 (define_expand "extendhisi2"
   [(set (match_operand:SI 0 "register_operand" "")
-       (sign_extend:SI (match_operand:HI 1 "general_operand" "")))]
+       (sign_extend:SI (match_operand:HI 1 "register_operand" "")))]
   ""
-  "
-{
-  if (TARGET_H8300
-      && GET_CODE (operands[1]) != CONST_INT
-      && !optimize)
-    {
-      emit_insn (gen_extendhisi2_h8300 (operands[0], operands[1]));
-      DONE;
-    }
-}")
-
-;; This is used when not optimizing.  It avoids severe code explosion
-;; due to poor register allocation.
-(define_expand "extendhisi2_h8300"
-  [(set (reg:HI 1) (match_operand:HI 1 "general_operand" ""))
-   (set (reg:SI 0) (sign_extend:SI (reg:HI 1)))
-   (set (match_operand:SI 0 "general_operand" "" ) (reg:SI 0))]
-  "TARGET_H8300"
   "")
 
 (define_insn ""
    (set_attr "cc" "clobber,clobber")])
 
 (define_insn ""
-  [(set (match_operand:SI 0 "register_operand" "=r,r")
-       (sign_extend:SI (match_operand:HI 1 "general_operand_src" "0,g>")))]
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (sign_extend:SI (match_operand:HI 1 "register_operand" "0")))]
   "TARGET_H8300H || TARGET_H8300S"
-  "@
-  exts.l       %S0
-  mov.w        %T1,%T0\;exts.l %S0"
-  [(set_attr "length" "2,4")
-   (set_attr "cc" "set_znv,set_znv")])
+  "exts.l      %S0"
+  [(set_attr "length" "2")
+   (set_attr "cc" "set_znv")])
 \f
 ;; ----------------------------------------------------------------------
 ;; SHIFTS
 ;; this in both rtl and at insn emit time.  Ideally, we'd use rtl as that would
 ;; give the optimizer more cracks at the code.  However, we wish to do things
 ;; like optimizing shifting the sign bit to bit 0 by rotating the other way.
-;; There is rtl to handle this (rotate + and), but the h8/300 doesn't handle
+;; There is rtl to handle this (rotate + and), but the H8/300 doesn't handle
 ;; 16 bit rotates.  Also, if we emit complicated rtl, combine may not be able
 ;; to detect cases it can optimize.
 ;;
        (ashift:QI (match_operand:QI 1 "register_operand" "")
                   (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
-  "if (expand_a_shift (QImode, ASHIFT, operands)) DONE;else FAIL;")
+  "if (expand_a_shift (QImode, ASHIFT, operands)) DONE; else FAIL;")
 
 (define_expand "ashrqi3"
   [(set (match_operand:QI 0 "register_operand" "")
        (ashiftrt:QI (match_operand:QI 1 "register_operand" "")
                     (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
-  "if (expand_a_shift (QImode, ASHIFTRT, operands)) DONE;else FAIL;")
+  "if (expand_a_shift (QImode, ASHIFTRT, operands)) DONE; else FAIL;")
 
 (define_expand "lshrqi3"
   [(set (match_operand:QI 0 "register_operand" "")
        (lshiftrt:QI (match_operand:QI 1 "register_operand" "")
                     (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
-  "if (expand_a_shift (QImode, LSHIFTRT, operands)) DONE;else FAIL;")
+  "if (expand_a_shift (QImode, LSHIFTRT, operands)) DONE; else FAIL;")
 
 (define_insn ""
   [(set (match_operand:QI 0 "register_operand" "=r,r")
-       (match_operator:QI 3 "nshift_operator" 
+       (match_operator:QI 3 "nshift_operator"
                        [ (match_operand:QI 1 "register_operand" "0,0")
-                         (match_operand:QI 2 "nonmemory_operand" "KM,rn")]))
+                         (match_operand:QI 2 "nonmemory_operand" "R,rn")]))
    (clobber (match_scratch:QI 4 "=X,&r"))]
   ""
-  "* return emit_a_shift (insn, operands);"
-  [(set_attr "length" "20")
+  "* return output_a_shift (operands);"
+  [(set (attr "length")
+       (symbol_ref "compute_a_shift_length (insn, operands)"))
    (set_attr "cc" "clobber")])
 
 ;; HI BIT SHIFTS
        (ashift:HI (match_operand:HI 1 "nonmemory_operand" "")
                   (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
-  "if (expand_a_shift (HImode, ASHIFT, operands)) DONE;else FAIL;")
+  "if (expand_a_shift (HImode, ASHIFT, operands)) DONE; else FAIL;")
 
 (define_expand "lshrhi3"
   [(set (match_operand:HI 0 "register_operand" "")
        (lshiftrt:HI (match_operand:HI 1 "general_operand" "")
                     (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
-  "if (expand_a_shift (HImode, LSHIFTRT, operands)) DONE;else FAIL;")
+  "if (expand_a_shift (HImode, LSHIFTRT, operands)) DONE; else FAIL;")
 
 (define_expand "ashrhi3"
   [(set (match_operand:HI 0 "register_operand" "")
        (ashiftrt:HI (match_operand:HI 1 "register_operand" "")
                     (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
-  "if (expand_a_shift (HImode, ASHIFTRT, operands)) DONE;else FAIL;")
+  "if (expand_a_shift (HImode, ASHIFTRT, operands)) DONE; else FAIL;")
 
 (define_insn ""
   [(set (match_operand:HI 0 "register_operand" "=r,r")
-       (match_operator:HI 3 "nshift_operator" 
+       (match_operator:HI 3 "nshift_operator"
                        [ (match_operand:HI 1 "register_operand" "0,0")
-                         (match_operand:QI 2 "nonmemory_operand" "KM,rn")]))
+                         (match_operand:QI 2 "nonmemory_operand" "S,rn")]))
    (clobber (match_scratch:QI 4 "=X,&r"))]
   ""
-  "* return emit_a_shift (insn, operands);"
-  [(set_attr "length" "20")
+  "* return output_a_shift (operands);"
+  [(set (attr "length")
+       (symbol_ref "compute_a_shift_length (insn, operands)"))
    (set_attr "cc" "clobber")])
 
 ;;  SI BIT SHIFTS
 
 (define_expand "ashlsi3"
   [(set (match_operand:SI 0 "register_operand" "")
-       (ashift:SI
-        (match_operand:SI 1 "general_operand" "")
-        (match_operand:QI 2 "nonmemory_operand" "")))]
+       (ashift:SI (match_operand:SI 1 "general_operand" "")
+                  (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
-  "if (expand_a_shift (SImode, ASHIFT, operands)) DONE;else FAIL;")
+  "if (expand_a_shift (SImode, ASHIFT, operands)) DONE; else FAIL;")
 
 (define_expand "lshrsi3"
   [(set (match_operand:SI 0 "register_operand" "")
-       (lshiftrt:SI
-        (match_operand:SI 1 "general_operand" "")
-        (match_operand:QI 2 "nonmemory_operand" "")))]
+       (lshiftrt:SI (match_operand:SI 1 "general_operand" "")
+                    (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
-  "if (expand_a_shift (SImode, LSHIFTRT, operands)) DONE;else FAIL;")
+  "if (expand_a_shift (SImode, LSHIFTRT, operands)) DONE; else FAIL;")
 
 (define_expand "ashrsi3"
   [(set (match_operand:SI 0 "register_operand" "")
-       (ashiftrt:SI
-        (match_operand:SI 1 "general_operand" "")
-        (match_operand:QI 2 "nonmemory_operand" "")))]
+       (ashiftrt:SI (match_operand:SI 1 "general_operand" "")
+                    (match_operand:QI 2 "nonmemory_operand" "")))]
   ""
-  "if (expand_a_shift (SImode, ASHIFTRT, operands)) DONE;else FAIL;")
+  "if (expand_a_shift (SImode, ASHIFTRT, operands)) DONE; else FAIL;")
 
 (define_insn ""
   [(set (match_operand:SI 0 "register_operand" "=r,r")
-       (match_operator:SI 3 "nshift_operator" 
+       (match_operator:SI 3 "nshift_operator"
                        [ (match_operand:SI 1 "register_operand" "0,0")
-                         (match_operand:QI 2 "nonmemory_operand" "K,rn")]))
+                         (match_operand:QI 2 "nonmemory_operand" "T,rn")]))
    (clobber (match_scratch:QI 4 "=X,&r"))]
   ""
-  "* return emit_a_shift (insn, operands);"
+  "* return output_a_shift (operands);"
+  [(set (attr "length")
+       (symbol_ref "compute_a_shift_length (insn, operands)"))
+   (set_attr "cc" "clobber")])
+\f
+;; ----------------------------------------------------------------------
+;; ROTATIONS
+;; ----------------------------------------------------------------------
+
+(define_expand "rotlqi3"
+  [(set (match_operand:QI 0 "register_operand" "")
+       (rotate:QI (match_operand:QI 1 "register_operand" "")
+                  (match_operand:QI 2 "nonmemory_operand" "")))]
+  ""
+  "if (expand_a_rotate (ROTATE, operands)) DONE; else FAIL;")
+
+(define_insn "*rotlqi3_1"
+  [(set (match_operand:QI 0 "register_operand" "=r")
+       (rotate:QI (match_operand:QI 1 "register_operand" "0")
+                  (match_operand:QI 2 "immediate_operand" "")))]
+  ""
+  "* return emit_a_rotate (ROTATE, operands);"
+  [(set_attr "length" "20")
+   (set_attr "cc" "clobber")])
+
+(define_expand "rotlhi3"
+  [(set (match_operand:HI 0 "register_operand" "")
+       (rotate:HI (match_operand:HI 1 "register_operand" "")
+                  (match_operand:QI 2 "nonmemory_operand" "")))]
+  ""
+  "if (expand_a_rotate (ROTATE, operands)) DONE; else FAIL;")
+
+(define_insn "*rotlhi3_1"
+  [(set (match_operand:HI 0 "register_operand" "=r")
+       (rotate:HI (match_operand:HI 1 "register_operand" "0")
+                  (match_operand:QI 2 "immediate_operand" "")))]
+  ""
+  "* return emit_a_rotate (ROTATE, operands);"
+  [(set_attr "length" "20")
+   (set_attr "cc" "clobber")])
+
+(define_expand "rotlsi3"
+  [(set (match_operand:SI 0 "register_operand" "")
+       (rotate:SI (match_operand:SI 1 "register_operand" "")
+                  (match_operand:QI 2 "nonmemory_operand" "")))]
+  "TARGET_H8300H || TARGET_H8300S"
+  "if (expand_a_rotate (ROTATE, operands)) DONE; else FAIL;")
+
+(define_insn "*rotlsi3_1"
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (rotate:SI (match_operand:SI 1 "register_operand" "0")
+                  (match_operand:QI 2 "immediate_operand" "")))]
+  "TARGET_H8300H || TARGET_H8300S"
+  "* return emit_a_rotate (ROTATE, operands);"
   [(set_attr "length" "20")
    (set_attr "cc" "clobber")])
 \f
 
 ;; You'll never believe all these patterns perform one basic action --
 ;; load a bit from the source, optionally invert the bit, then store it
-;; in the destination (which is known to be zero)..  
+;; in the destination (which is known to be zero).
 ;;
 ;; Combine obviously need some work to better identify this situation and
 ;; canonicalize the form better.
 
-;; 
+;;
 ;; Normal loads with a 16bit destination.
-;; 
-;; Yes, both cases are needed.
 ;;
+
 (define_insn ""
   [(set (match_operand:HI 0 "register_operand" "=&r")
        (zero_extract:HI (match_operand:HI 1 "register_operand" "r")
                         (const_int 1)
                         (match_operand:HI 2 "immediate_operand" "n")))]
-  ""
-  "sub.w       %0,%0\;bld      %Z2,%Y1\;bst    #0,%X0"
-  [(set_attr "cc" "clobber")
-   (set_attr "length" "6")])
-
-(define_insn ""
-  [(set (match_operand:HI 0 "register_operand" "=&r")
-       (subreg:HI (zero_extract:SI
-                    (match_operand:HI 1 "register_operand" "r")
-                    (const_int 1)
-                    (match_operand:HI 2 "immediate_operand" "n")) 1))]
-  ""
+  "TARGET_H8300"
   "sub.w       %0,%0\;bld      %Z2,%Y1\;bst    #0,%X0"
   [(set_attr "cc" "clobber")
    (set_attr "length" "6")])
 
-;; 
+;;
 ;; Inverted loads with a 16bit destination.
-;; 
-;; Yes, all four cases are needed.
 ;;
 
 (define_insn ""
   [(set (match_operand:HI 0 "register_operand" "=&r")
        (zero_extract:HI (xor:HI (match_operand:HI 1 "register_operand" "r")
-                                (match_operand:HI 3 "p_operand" "P"))
+                                (match_operand:HI 3 "const_int_operand" "n"))
                         (const_int 1)
                         (match_operand:HI 2 "const_int_operand" "n")))]
-  "(1 << INTVAL (operands[2])) == INTVAL (operands[3])"
-  "sub.w       %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
-  [(set_attr "cc" "clobber")
-   (set_attr "length" "8")])
-
-(define_insn ""
-  [(set (match_operand:HI 0 "register_operand" "=&r")
-       (and:HI (not:HI 
-                 (lshiftrt:HI
-                   (match_operand:HI 1 "bit_operand" "Ur")
-                   (match_operand:HI 2 "const_int_operand" "n")))
-               (const_int 1)))]
-  ""
-  "sub.w       %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
-  [(set_attr "cc" "clobber")
-   (set_attr "length" "8")])
-
-(define_insn ""
-  [(set (match_operand:HI 0 "register_operand" "=&r")
-       (and:HI (not:HI 
-                 (subreg:HI 
-                   (lshiftrt:SI
-                     (match_operand:SI 1 "register_operand" "Ur")
-                     (match_operand:SI 2 "const_int_operand" "n")) 1))
-               (const_int 1)))]
-  "INTVAL (operands[2]) < 16"
-  "sub.w       %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
-  [(set_attr "cc" "clobber")
-   (set_attr "length" "8")])
-
-(define_insn ""
-  [(set (match_operand:HI 0 "register_operand" "=&r")
-       (and:HI (not:HI 
-                 (subreg:HI 
-                   (lshiftrt:SI
-                     (match_operand:SI 1 "bit_operand" "Ur")
-                     (match_operand:SI 2 "const_int_operand" "n")) 0))
-               (const_int 1)))]
-  "(TARGET_H8300H || TARGET_H8300S)
-   && INTVAL (operands[2]) < 16"
+  "TARGET_H8300
+   && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
   "sub.w       %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
   [(set_attr "cc" "clobber")
    (set_attr "length" "8")])
 
-;; 
+;;
 ;; Normal loads with a 32bit destination.
-;; 
-;; Yes, all three cases are needed.
 ;;
+
 (define_insn ""
   [(set (match_operand:SI 0 "register_operand" "=&r")
        (zero_extract:SI (match_operand:HI 1 "register_operand" "r")
                         (const_int 1)
-                        (match_operand:HI 2 "const_int_operand" "n")))]
-  ""
-  "* return output_simode_bld (0, 0, operands);"
-  [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 10)
-                     (const_int 8)))])
-
-(define_insn ""
-  [(set (match_operand:SI 0 "register_operand" "=&r")
-       (and:SI (zero_extend:SI 
-                  (lshiftrt:QI
-                    (match_operand:QI 1 "bit_operand" "Ur")
-                    (match_operand:QI 2 "const_int_operand" "n")))
-               (const_int 1)))]
-  ""
-  "* return output_simode_bld (0, 0, operands);"
+                        (match_operand 2 "const_int_operand" "n")))]
+  "TARGET_H8300
+   && INTVAL (operands[2]) < 16"
+  "* return output_simode_bld (0, operands);"
   [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 10)
-                     (const_int 8)))])
+   (set_attr "length" "6")])
 
 (define_insn ""
-  [(set (match_operand:SI 0 "register_operand" "=&r")
-       (and:SI (zero_extend:SI 
-                  (lshiftrt:HI
-                     (match_operand:HI 1 "bit_operand" "Ur")
-                     (match_operand:HI 2 "const_int_operand" "n")))
-               (const_int 1)))]
-  ""
-  "* return output_simode_bld (0, 0, operands);"
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (zero_extract:SI (match_operand:SI 1 "register_operand" "r")
+                        (const_int 1)
+                        (match_operand 2 "const_int_operand" "n")))]
+  "(TARGET_H8300H || TARGET_H8300S)
+   && INTVAL (operands[2]) < 16"
+  "* return output_simode_bld (0, operands);"
   [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 10)
-                     (const_int 8)))])
+   (set_attr "length" "6")])
 
-;; 
+;;
 ;; Inverted loads with a 32bit destination.
-;; 
-;; Yes, all seven cases are needed.
 ;;
-(define_insn ""
-  [(set (match_operand:SI 0 "register_operand" "=&r")
-       (and:SI (not:SI
-                 (zero_extend:SI (match_operand:HI 1 "register_operand" "r")))
-               (match_operand:SI 2 "p_operand" "P")))]
-  ""
-  "* return output_simode_bld (1, 1, operands);"
-  [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 10)
-                     (const_int 8)))])
-(define_insn ""
-  [(set (match_operand:SI 0 "register_operand" "=&r")
-       (and:SI (not:SI
-                 (zero_extend:SI
-                   (lshiftrt:HI (match_operand:HI 1 "bit_operand" "Ur")
-                                (match_operand:HI 2 "const_int_operand" "n"))))
-               (const_int 1)))]
-  ""
-  "* return output_simode_bld (1, 0, operands);"
-  [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 10)
-                     (const_int 8)))])
-
-(define_insn ""
-  [(set (match_operand:SI 0 "register_operand" "=&r")
-       (and:SI (not:SI
-                 (zero_extend:SI (match_operand:QI 1 "register_operand" "r")))
-               (match_operand:SI 2 "p_operand" "P")))]
-  ""
-  "* return output_simode_bld (1, 1, operands);"
-  [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 10)
-                     (const_int 8)))])
-(define_insn ""
-  [(set (match_operand:SI 0 "register_operand" "=&r")
-       (and:SI (not:SI
-                 (zero_extend:SI
-                   (lshiftrt:QI (match_operand:QI 1 "bit_operand" "Ur")
-                                (match_operand:QI 2 "const_int_operand" "n"))))
-               (const_int 1)))]
-  ""
-  "* return output_simode_bld (1, 0, operands);"
-  [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 10)
-                     (const_int 8)))])
-
-(define_insn ""
-  [(set (match_operand:SI 0 "register_operand" "=&r")
-       (and:SI (not:SI
-                 (subreg:SI 
-                    (lshiftrt:HI
-                       (match_operand:HI 1 "bit_operand" "Ur")
-                       (match_operand:HI 2 "const_int_operand" "n")) 0))
-               (const_int 1)))]
-  "1"
-  "* return output_simode_bld (1, 0, operands);"
-  [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 10)
-                     (const_int 8)))])
 
 (define_insn ""
   [(set (match_operand:SI 0 "register_operand" "=&r")
-       (and:SI (not:SI
-                 (subreg:SI 
-                    (lshiftrt:QI
-                       (match_operand:QI 1 "bit_operand" "Ur")
-                       (match_operand:QI 2 "const_int_operand" "n")) 0))
-               (const_int 1)))]
-  "1"
-  "* return output_simode_bld (1, 0, operands);"
+       (zero_extract:SI (xor:HI (match_operand:HI 1 "register_operand" "r")
+                                (match_operand:HI 3 "const_int_operand" "n"))
+                        (const_int 1)
+                        (match_operand 2 "const_int_operand" "n")))]
+  "TARGET_H8300
+   && INTVAL (operands[2]) < 16
+   && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
+  "* return output_simode_bld (1, operands);"
   [(set_attr "cc" "clobber")
-   (set (attr "length")
-       (if_then_else (eq (symbol_ref "TARGET_H8300H || TARGET_H8300S")
-                         (const_int 0))
-                     (const_int 10)
-                     (const_int 8)))])
+   (set_attr "length" "6")])
 
 (define_insn ""
-  [(set (match_operand:SI 0 "register_operand" "=&r")
-       (zero_extract:SI (xor:HI (match_operand:HI 1 "register_operand" "r")
-                                (match_operand:HI 3 "p_operand" "P"))
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (zero_extract:SI (xor:SI (match_operand:SI 1 "register_operand" "r")
+                                (match_operand 3 "const_int_operand" "n"))
                         (const_int 1)
-                        (match_operand:HI 2 "const_int_operand" "n")))]
-  "(1 << INTVAL (operands[2])) == INTVAL (operands[3])"
-  "sub.w       %0,%0\;bild     %Z2,%Y1\;bst    #0,%X0"
+                        (match_operand 2 "const_int_operand" "n")))]
+  "(TARGET_H8300H || TARGET_H8300S)
+   && INTVAL (operands[2]) < 16
+   && (1 << INTVAL (operands[2])) == INTVAL (operands[3])"
+  "* return output_simode_bld (1, operands);"
   [(set_attr "cc" "clobber")
-   (set_attr "length" "8")])
+   (set_attr "length" "6")])
 
 (define_expand "insv"
   [(set (zero_extract:HI (match_operand:HI 0 "general_operand" "")
   "TARGET_H8300"
   "
 {
-  /* We only have single bit bitfield instructions.  */
+  /* We only have single bit bit-field instructions.  */
   if (INTVAL (operands[1]) != 1)
     FAIL;
 
    (set_attr "length" "4")])
 
 (define_expand "extzv"
-  [(set (match_operand:HI 0 "register_operand" "") 
+  [(set (match_operand:HI 0 "register_operand" "")
        (zero_extract:HI (match_operand:HI 1 "bit_operand" "")
                         (match_operand:HI 2 "general_operand" "")
                         (match_operand:HI 3 "general_operand" "")))]
   "TARGET_H8300"
   "
 {
-  /* We only have single bit bitfield instructions.  */
+  /* We only have single bit bit-field instructions.  */
   if (INTVAL (operands[2]) != 1)
     FAIL;
 
   [(set_attr "cc" "clobber")
    (set_attr "length" "6")
    (set_attr "adjust_length" "no")])
-
 \f
-;; ----------------------------------------------
-;; Peepholes go at the end.
-;; ----------------------------------------------
+;; -----------------------------------------------------------------
+;; COMBINE PATTERNS
+;; -----------------------------------------------------------------
 
-;; Notice a move which could be post incremented.
+(define_insn ""
+  [(set (match_operand:HI 0 "register_operand" "=r")
+       (ior:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "r"))
+               (match_operand:HI 2 "register_operand" "0")))]
+  "REG_P (operands[0])
+   && REG_P (operands[1])
+   && REGNO (operands[0]) != REGNO (operands[1])"
+  "or\\t%X1,%s0"
+  [(set_attr "cc" "clobber")
+   (set_attr "length" "2")])
 
-(define_peephole 
-  [(set (match_operand:QI 0 "register_operand" "")
-       (mem:QI (match_operand:HI 1 "register_operand" "")))
-   (set (match_dup 1) (plus:HI (match_dup 1) (const_int 1)))]
-  "REGNO(operands[1]) != REGNO(operands[0])"
-  "mov.b       @%T1+,%X0"
-  [(set_attr "length" "2")
-   (set_attr "cc" "set_znv")])
+(define_insn ""
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (ior:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "r"))
+               (match_operand:SI 2 "register_operand" "0")))]
+  "(TARGET_H8300H || TARGET_H8300S)
+   && REG_P (operands[0])
+   && REG_P (operands[1])
+   && (REGNO (operands[0]) != REGNO (operands[1]))"
+  "or.w\\t%T1,%f0"
+  [(set_attr "cc" "clobber")
+   (set_attr "length" "2")])
 
-(define_peephole 
-  [(set (match_operand:HI 0 "register_operand" "")
-       (mem:HI (match_operand:HI 1 "register_operand" "")))
-   (set (match_dup 1) (plus:HI (match_dup 1) (const_int 2)))]
-  "REGNO(operands[1]) != REGNO(operands[0])"
-  "mov.w       @%T1+,%T0"
-  [(set_attr "length" "2")
-   (set_attr "cc" "set_znv")])
+(define_insn ""
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (ior:SI (zero_extend:SI (match_operand:QI 1 "register_operand" "r"))
+               (match_operand:SI 2 "register_operand" "0")))]
+  ""
+  "or\\t%X1,%w0"
+  [(set_attr "cc" "clobber")
+   (set_attr "length" "2")])
 
-;; Notice a move which could be predecremented.
+(define_insn ""
+  [(set (match_operand:HI 0 "register_operand" "=r")
+       (xor:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "r"))
+               (match_operand:HI 2 "register_operand" "0")))]
+  "REG_P (operands[0])
+   && REG_P (operands[1])
+   && REGNO (operands[0]) != REGNO (operands[1])"
+  "xor\\t%X1,%s0"
+  [(set_attr "cc" "clobber")
+   (set_attr "length" "2")])
 
-(define_peephole 
-  [(set (match_operand:HI 1 "register_operand" "")
-       (plus:HI (match_dup 1) (const_int -1)))
-   (set (mem:QI (match_dup 1))
-               (match_operand:QI 0 "register_operand" ""))]
-  "REGNO(operands[1]) != REGNO(operands[0])"
-  "mov.b       %X0,@-%T1"
-  [(set_attr "length" "2")
-   (set_attr "cc" "set_znv")])
+(define_insn ""
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (xor:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "r"))
+               (match_operand:SI 2 "register_operand" "0")))]
+  "(TARGET_H8300H || TARGET_H8300S)
+   && REG_P (operands[0])
+   && REG_P (operands[1])
+   && (REGNO (operands[0]) != REGNO (operands[1]))"
+  "xor.w\\t%T1,%f0"
+  [(set_attr "cc" "clobber")
+   (set_attr "length" "2")])
 
-(define_peephole 
-  [(set (match_operand:HI 1 "register_operand" "")
-       (plus:HI (match_dup 1) (const_int -2)))
-   (set (mem:HI (match_dup 1))
-               (match_operand:HI 0 "register_operand" ""))]
-  "REGNO(operands[1]) != REGNO(operands[0])"
-  "mov.w       %T0,@-%T1"
-  [(set_attr "length" "2")
-   (set_attr "cc" "set_znv")])
+(define_insn ""
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (xor:SI (zero_extend:SI (match_operand:QI 1 "register_operand" "r"))
+               (match_operand:SI 2 "register_operand" "0")))]
+  "REG_P (operands[0])
+   && REG_P (operands[1])
+   && REGNO (operands[0]) != REGNO (operands[1])"
+  "xor\\t%X1,%w0"
+  [(set_attr "cc" "clobber")
+   (set_attr "length" "2")])
+
+(define_insn ""
+  [(set (match_operand:HI 0 "register_operand" "=r")
+       (ior:HI (zero_extend:HI (match_operand:QI 1 "register_operand" "0"))
+               (ashift:HI (match_operand:HI 2 "register_operand" "r")
+                          (const_int 8))))]
+  "REG_P (operands[0])
+   && REG_P (operands[2])
+   && REGNO (operands[0]) != REGNO (operands[2])"
+  "mov.b\\t%s2,%t0"
+  [(set_attr "cc" "clobber")
+   (set_attr "length" "2")])
+
+(define_insn ""
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (ior:SI (zero_extend:SI (match_operand:HI 1 "register_operand" "0"))
+               (ashift:SI (match_operand:SI 2 "register_operand" "r")
+                          (const_int 16))))]
+  "TARGET_H8300H || TARGET_H8300S"
+  "mov.w\\t%f2,%e0"
+  [(set_attr "cc" "clobber")
+   (set_attr "length" "2")])
+
+(define_insn ""
+  [(set (match_operand:SI 0 "register_operand" "=r")
+       (ior:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
+                          (const_int 16))
+               (match_operand:SI 2 "register_operand" "0")))]
+  "TARGET_H8300H || TARGET_H8300S"
+  "or.w\\t%f1,%e0"
+  [(set_attr "cc" "clobber")
+   (set_attr "length" "2")])
+
+;; Storing a part of HImode to QImode.
+
+(define_insn ""
+  [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
+       (subreg:QI (lshiftrt:HI (match_operand:HI 1 "register_operand" "r")
+                               (const_int 8)) 1))]
+  ""
+  "mov.b\\t%t1,%R0"
+  [(set_attr "cc" "set_znv")
+   (set_attr "length" "8")])
+
+;; Storing a part of SImode to QImode.
+
+(define_insn ""
+  [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
+       (subreg:QI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
+                               (const_int 8)) 3))]
+  ""
+  "mov.b\\t%x1,%R0"
+  [(set_attr "cc" "set_znv")
+   (set_attr "length" "8")])
+
+(define_insn ""
+  [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
+       (subreg:QI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
+                               (const_int 16)) 3))
+   (clobber (match_scratch:SI 2 "=&r"))]
+  "TARGET_H8300H || TARGET_H8300S"
+  "mov.w\\t%e1,%f2\;mov.b\\t%w2,%R0"
+  [(set_attr "cc" "set_znv")
+   (set_attr "length" "10")])
 
+(define_insn ""
+  [(set (match_operand:QI 0 "general_operand_dst" "=rm<")
+       (subreg:QI (lshiftrt:SI (match_operand:SI 1 "register_operand" "r")
+                               (const_int 24)) 3))
+   (clobber (match_scratch:SI 2 "=&r"))]
+  "TARGET_H8300H || TARGET_H8300S"
+  "mov.w\\t%e1,%f2\;mov.b\\t%x2,%R0"
+  [(set_attr "cc" "set_znv")
+   (set_attr "length" "10")])
+
+(define_insn_and_split ""
+  [(set (pc)
+       (if_then_else (eq (zero_extract:SI (subreg:SI (match_operand:QI 0 "register_operand" "") 0)
+                                          (const_int 1)
+                                          (const_int 7))
+                         (const_int 0))
+                     (label_ref (match_operand 1 "" ""))
+                     (pc)))]
+  ""
+  "#"
+  ""
+  [(set (cc0)
+       (match_dup 0))
+   (set (pc)
+       (if_then_else (ge (cc0)
+                         (const_int 0))
+                     (label_ref (match_dup 1))
+                     (pc)))]
+  "")
+
+(define_insn_and_split ""
+  [(set (pc)
+       (if_then_else (ne (zero_extract:SI (subreg:SI (match_operand:QI 0 "register_operand" "") 0)
+                                          (const_int 1)
+                                          (const_int 7))
+                         (const_int 0))
+                     (label_ref (match_operand 1 "" ""))
+                     (pc)))]
+  ""
+  "#"
+  ""
+  [(set (cc0)
+       (match_dup 0))
+   (set (pc)
+       (if_then_else (lt (cc0)
+                         (const_int 0))
+                     (label_ref (match_dup 1))
+                     (pc)))]
+  "")