github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/cmd/compile/internal/ssa/gen/AMD64.rules (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Lowering arithmetic
     6  (Add64  x y) -> (ADDQ  x y)
     7  (AddPtr x y) && config.PtrSize == 8 -> (ADDQ x y)
     8  (AddPtr x y) && config.PtrSize == 4 -> (ADDL x y)
     9  (Add32  x y) -> (ADDL  x y)
    10  (Add16  x y) -> (ADDL  x y)
    11  (Add8   x y) -> (ADDL  x y)
    12  (Add32F x y) -> (ADDSS x y)
    13  (Add64F x y) -> (ADDSD x y)
    14  
    15  (Sub64  x y) -> (SUBQ  x y)
    16  (SubPtr x y) && config.PtrSize == 8 -> (SUBQ x y)
    17  (SubPtr x y) && config.PtrSize == 4 -> (SUBL x y)
    18  (Sub32  x y) -> (SUBL  x y)
    19  (Sub16  x y) -> (SUBL  x y)
    20  (Sub8   x y) -> (SUBL  x y)
    21  (Sub32F x y) -> (SUBSS x y)
    22  (Sub64F x y) -> (SUBSD x y)
    23  
    24  (Mul64  x y) -> (MULQ  x y)
    25  (Mul32  x y) -> (MULL  x y)
    26  (Mul16  x y) -> (MULL  x y)
    27  (Mul8   x y) -> (MULL  x y)
    28  (Mul32F x y) -> (MULSS x y)
    29  (Mul64F x y) -> (MULSD x y)
    30  
    31  (Div32F x y) -> (DIVSS x y)
    32  (Div64F x y) -> (DIVSD x y)
    33  
    34  (Div64  x y) -> (Select0 (DIVQ  x y))
    35  (Div64u x y) -> (Select0 (DIVQU x y))
    36  (Div32  x y) -> (Select0 (DIVL  x y))
    37  (Div32u x y) -> (Select0 (DIVLU x y))
    38  (Div16  x y) -> (Select0 (DIVW  x y))
    39  (Div16u x y) -> (Select0 (DIVWU x y))
    40  (Div8   x y) -> (Select0 (DIVW  (SignExt8to16 x) (SignExt8to16 y)))
    41  (Div8u  x y) -> (Select0 (DIVWU (ZeroExt8to16 x) (ZeroExt8to16 y)))
    42  
    43  (Hmul64  x y) -> (HMULQ  x y)
    44  (Hmul64u x y) -> (HMULQU x y)
    45  (Hmul32  x y) -> (HMULL  x y)
    46  (Hmul32u x y) -> (HMULLU x y)
    47  
    48  (Mul64uhilo x y) -> (MULQU2 x y)
    49  (Div128u xhi xlo y) -> (DIVQU2 xhi xlo y)
    50  
    51  (Avg64u x y) -> (AVGQU x y)
    52  
    53  (Mod64  x y) -> (Select1 (DIVQ  x y))
    54  (Mod64u x y) -> (Select1 (DIVQU x y))
    55  (Mod32  x y) -> (Select1 (DIVL  x y))
    56  (Mod32u x y) -> (Select1 (DIVLU x y))
    57  (Mod16  x y) -> (Select1 (DIVW  x y))
    58  (Mod16u x y) -> (Select1 (DIVWU x y))
    59  (Mod8   x y) -> (Select1 (DIVW  (SignExt8to16 x) (SignExt8to16 y)))
    60  (Mod8u  x y) -> (Select1 (DIVWU (ZeroExt8to16 x) (ZeroExt8to16 y)))
    61  
    62  (And64 x y) -> (ANDQ x y)
    63  (And32 x y) -> (ANDL x y)
    64  (And16 x y) -> (ANDL x y)
    65  (And8  x y) -> (ANDL x y)
    66  
    67  (Or64 x y) -> (ORQ x y)
    68  (Or32 x y) -> (ORL x y)
    69  (Or16 x y) -> (ORL x y)
    70  (Or8  x y) -> (ORL x y)
    71  
    72  (Xor64 x y) -> (XORQ x y)
    73  (Xor32 x y) -> (XORL x y)
    74  (Xor16 x y) -> (XORL x y)
    75  (Xor8  x y) -> (XORL x y)
    76  
    77  (Neg64  x) -> (NEGQ x)
    78  (Neg32  x) -> (NEGL x)
    79  (Neg16  x) -> (NEGL x)
    80  (Neg8   x) -> (NEGL x)
    81  (Neg32F x) -> (PXOR x (MOVSSconst <typ.Float32> [f2i(math.Copysign(0, -1))]))
    82  (Neg64F x) -> (PXOR x (MOVSDconst <typ.Float64> [f2i(math.Copysign(0, -1))]))
    83  
    84  (Com64 x) -> (NOTQ x)
    85  (Com32 x) -> (NOTL x)
    86  (Com16 x) -> (NOTL x)
    87  (Com8  x) -> (NOTL x)
    88  
    89  // Lowering boolean ops
    90  (AndB x y) -> (ANDL x y)
    91  (OrB x y) -> (ORL x y)
    92  (Not x) -> (XORLconst [1] x)
    93  
    94  // Lowering pointer arithmetic
    95  (OffPtr [off] ptr) && config.PtrSize == 8 && is32Bit(off) -> (ADDQconst [off] ptr)
    96  (OffPtr [off] ptr) && config.PtrSize == 8 -> (ADDQ (MOVQconst [off]) ptr)
    97  (OffPtr [off] ptr) && config.PtrSize == 4 -> (ADDLconst [off] ptr)
    98  
    99  // Lowering other arithmetic
   100  (Ctz64 <t> x) -> (CMOVQEQ (Select0 <t> (BSFQ x)) (MOVQconst <t> [64]) (Select1 <types.TypeFlags> (BSFQ x)))
   101  (Ctz32 x) -> (Select0 (BSFQ (ORQ <typ.UInt64> (MOVQconst [1<<32]) x)))
   102  
   103  (BitLen64 <t> x) -> (ADDQconst [1] (CMOVQEQ <t> (Select0 <t> (BSRQ x)) (MOVQconst <t> [-1]) (Select1 <types.TypeFlags> (BSRQ x))))
   104  (BitLen32 x) -> (BitLen64 (MOVLQZX <typ.UInt64> x))
   105  
   106  (Bswap64 x) -> (BSWAPQ x)
   107  (Bswap32 x) -> (BSWAPL x)
   108  
   109  (PopCount64 x) -> (POPCNTQ x)
   110  (PopCount32 x) -> (POPCNTL x)
   111  (PopCount16 x) -> (POPCNTL (MOVWQZX <typ.UInt32> x))
   112  (PopCount8 x) -> (POPCNTL (MOVBQZX <typ.UInt32> x))
   113  
   114  (Sqrt x) -> (SQRTSD x)
   115  
   116  // Lowering extension
   117  // Note: we always extend to 64 bits even though some ops don't need that many result bits.
   118  (SignExt8to16  x) -> (MOVBQSX x)
   119  (SignExt8to32  x) -> (MOVBQSX x)
   120  (SignExt8to64  x) -> (MOVBQSX x)
   121  (SignExt16to32 x) -> (MOVWQSX x)
   122  (SignExt16to64 x) -> (MOVWQSX x)
   123  (SignExt32to64 x) -> (MOVLQSX x)
   124  
   125  (ZeroExt8to16  x) -> (MOVBQZX x)
   126  (ZeroExt8to32  x) -> (MOVBQZX x)
   127  (ZeroExt8to64  x) -> (MOVBQZX x)
   128  (ZeroExt16to32 x) -> (MOVWQZX x)
   129  (ZeroExt16to64 x) -> (MOVWQZX x)
   130  (ZeroExt32to64 x) -> (MOVLQZX x)
   131  
   132  (Slicemask <t> x) -> (SARQconst (NEGQ <t> x) [63])
   133  
   134  // Lowering truncation
   135  // Because we ignore high parts of registers, truncates are just copies.
   136  (Trunc16to8  x) -> x
   137  (Trunc32to8  x) -> x
   138  (Trunc32to16 x) -> x
   139  (Trunc64to8  x) -> x
   140  (Trunc64to16 x) -> x
   141  (Trunc64to32 x) -> x
   142  
   143  // Lowering float <-> int
   144  (Cvt32to32F x) -> (CVTSL2SS x)
   145  (Cvt32to64F x) -> (CVTSL2SD x)
   146  (Cvt64to32F x) -> (CVTSQ2SS x)
   147  (Cvt64to64F x) -> (CVTSQ2SD x)
   148  
   149  (Cvt32Fto32 x) -> (CVTTSS2SL x)
   150  (Cvt32Fto64 x) -> (CVTTSS2SQ x)
   151  (Cvt64Fto32 x) -> (CVTTSD2SL x)
   152  (Cvt64Fto64 x) -> (CVTTSD2SQ x)
   153  
   154  (Cvt32Fto64F x) -> (CVTSS2SD x)
   155  (Cvt64Fto32F x) -> (CVTSD2SS x)
   156  
   157  (Round32F x) -> x
   158  (Round64F x) -> x
   159  
   160  // Lowering shifts
   161  // Unsigned shifts need to return 0 if shift amount is >= width of shifted value.
   162  //   result = (arg << shift) & (shift >= argbits ? 0 : 0xffffffffffffffff)
   163  (Lsh64x64 <t> x y) -> (ANDQ (SHLQ <t> x y) (SBBQcarrymask <t> (CMPQconst y [64])))
   164  (Lsh64x32 <t> x y) -> (ANDQ (SHLQ <t> x y) (SBBQcarrymask <t> (CMPLconst y [64])))
   165  (Lsh64x16 <t> x y) -> (ANDQ (SHLQ <t> x y) (SBBQcarrymask <t> (CMPWconst y [64])))
   166  (Lsh64x8  <t> x y) -> (ANDQ (SHLQ <t> x y) (SBBQcarrymask <t> (CMPBconst y [64])))
   167  
   168  (Lsh32x64 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPQconst y [32])))
   169  (Lsh32x32 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPLconst y [32])))
   170  (Lsh32x16 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPWconst y [32])))
   171  (Lsh32x8  <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPBconst y [32])))
   172  
   173  (Lsh16x64 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPQconst y [32])))
   174  (Lsh16x32 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPLconst y [32])))
   175  (Lsh16x16 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPWconst y [32])))
   176  (Lsh16x8  <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPBconst y [32])))
   177  
   178  (Lsh8x64 <t> x y)  -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPQconst y [32])))
   179  (Lsh8x32 <t> x y)  -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPLconst y [32])))
   180  (Lsh8x16 <t> x y)  -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPWconst y [32])))
   181  (Lsh8x8  <t> x y)  -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPBconst y [32])))
   182  
   183  (Rsh64Ux64 <t> x y) -> (ANDQ (SHRQ <t> x y) (SBBQcarrymask <t> (CMPQconst y [64])))
   184  (Rsh64Ux32 <t> x y) -> (ANDQ (SHRQ <t> x y) (SBBQcarrymask <t> (CMPLconst y [64])))
   185  (Rsh64Ux16 <t> x y) -> (ANDQ (SHRQ <t> x y) (SBBQcarrymask <t> (CMPWconst y [64])))
   186  (Rsh64Ux8  <t> x y) -> (ANDQ (SHRQ <t> x y) (SBBQcarrymask <t> (CMPBconst y [64])))
   187  
   188  (Rsh32Ux64 <t> x y) -> (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMPQconst y [32])))
   189  (Rsh32Ux32 <t> x y) -> (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMPLconst y [32])))
   190  (Rsh32Ux16 <t> x y) -> (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMPWconst y [32])))
   191  (Rsh32Ux8  <t> x y) -> (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMPBconst y [32])))
   192  
   193  (Rsh16Ux64 <t> x y) -> (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMPQconst y [16])))
   194  (Rsh16Ux32 <t> x y) -> (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMPLconst y [16])))
   195  (Rsh16Ux16 <t> x y) -> (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMPWconst y [16])))
   196  (Rsh16Ux8  <t> x y) -> (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMPBconst y [16])))
   197  
   198  (Rsh8Ux64 <t> x y)  -> (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMPQconst y [8])))
   199  (Rsh8Ux32 <t> x y)  -> (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMPLconst y [8])))
   200  (Rsh8Ux16 <t> x y)  -> (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMPWconst y [8])))
   201  (Rsh8Ux8  <t> x y)  -> (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMPBconst y [8])))
   202  
   203  // Signed right shift needs to return 0/-1 if shift amount is >= width of shifted value.
   204  // We implement this by setting the shift value to -1 (all ones) if the shift value is >= width.
   205  (Rsh64x64 <t> x y) -> (SARQ <t> x (ORQ <y.Type> y (NOTQ <y.Type> (SBBQcarrymask <y.Type> (CMPQconst y [64])))))
   206  (Rsh64x32 <t> x y) -> (SARQ <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPLconst y [64])))))
   207  (Rsh64x16 <t> x y) -> (SARQ <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPWconst y [64])))))
   208  (Rsh64x8  <t> x y) -> (SARQ <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPBconst y [64])))))
   209  
   210  (Rsh32x64 <t> x y) -> (SARL <t> x (ORQ <y.Type> y (NOTQ <y.Type> (SBBQcarrymask <y.Type> (CMPQconst y [32])))))
   211  (Rsh32x32 <t> x y) -> (SARL <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPLconst y [32])))))
   212  (Rsh32x16 <t> x y) -> (SARL <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPWconst y [32])))))
   213  (Rsh32x8  <t> x y) -> (SARL <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPBconst y [32])))))
   214  
   215  (Rsh16x64 <t> x y) -> (SARW <t> x (ORQ <y.Type> y (NOTQ <y.Type> (SBBQcarrymask <y.Type> (CMPQconst y [16])))))
   216  (Rsh16x32 <t> x y) -> (SARW <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPLconst y [16])))))
   217  (Rsh16x16 <t> x y) -> (SARW <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPWconst y [16])))))
   218  (Rsh16x8  <t> x y) -> (SARW <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPBconst y [16])))))
   219  
   220  (Rsh8x64 <t> x y)  -> (SARB <t> x (ORQ <y.Type> y (NOTQ <y.Type> (SBBQcarrymask <y.Type> (CMPQconst y [8])))))
   221  (Rsh8x32 <t> x y)  -> (SARB <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPLconst y [8])))))
   222  (Rsh8x16 <t> x y)  -> (SARB <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPWconst y [8])))))
   223  (Rsh8x8  <t> x y)  -> (SARB <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPBconst y [8])))))
   224  
   225  // Lowering comparisons
   226  (Less64  x y) -> (SETL (CMPQ x y))
   227  (Less32  x y) -> (SETL (CMPL x y))
   228  (Less16  x y) -> (SETL (CMPW x y))
   229  (Less8   x y) -> (SETL (CMPB x y))
   230  (Less64U x y) -> (SETB (CMPQ x y))
   231  (Less32U x y) -> (SETB (CMPL x y))
   232  (Less16U x y) -> (SETB (CMPW x y))
   233  (Less8U  x y) -> (SETB (CMPB x y))
   234  // Use SETGF with reversed operands to dodge NaN case
   235  (Less64F x y) -> (SETGF (UCOMISD y x))
   236  (Less32F x y) -> (SETGF (UCOMISS y x))
   237  
   238  (Leq64  x y) -> (SETLE (CMPQ x y))
   239  (Leq32  x y) -> (SETLE (CMPL x y))
   240  (Leq16  x y) -> (SETLE (CMPW x y))
   241  (Leq8   x y) -> (SETLE (CMPB x y))
   242  (Leq64U x y) -> (SETBE (CMPQ x y))
   243  (Leq32U x y) -> (SETBE (CMPL x y))
   244  (Leq16U x y) -> (SETBE (CMPW x y))
   245  (Leq8U  x y) -> (SETBE (CMPB x y))
   246  // Use SETGEF with reversed operands to dodge NaN case
   247  (Leq64F x y) -> (SETGEF (UCOMISD y x))
   248  (Leq32F x y) -> (SETGEF (UCOMISS y x))
   249  
   250  (Greater64  x y) -> (SETG (CMPQ x y))
   251  (Greater32  x y) -> (SETG (CMPL x y))
   252  (Greater16  x y) -> (SETG (CMPW x y))
   253  (Greater8   x y) -> (SETG (CMPB x y))
   254  (Greater64U x y) -> (SETA (CMPQ x y))
   255  (Greater32U x y) -> (SETA (CMPL x y))
   256  (Greater16U x y) -> (SETA (CMPW x y))
   257  (Greater8U  x y) -> (SETA (CMPB x y))
   258  // Note Go assembler gets UCOMISx operand order wrong, but it is right here
   259  // Bug is accommodated at generation of assembly language.
   260  (Greater64F x y) -> (SETGF (UCOMISD x y))
   261  (Greater32F x y) -> (SETGF (UCOMISS x y))
   262  
   263  (Geq64  x y) -> (SETGE (CMPQ x y))
   264  (Geq32  x y) -> (SETGE (CMPL x y))
   265  (Geq16  x y) -> (SETGE (CMPW x y))
   266  (Geq8   x y) -> (SETGE (CMPB x y))
   267  (Geq64U x y) -> (SETAE (CMPQ x y))
   268  (Geq32U x y) -> (SETAE (CMPL x y))
   269  (Geq16U x y) -> (SETAE (CMPW x y))
   270  (Geq8U  x y) -> (SETAE (CMPB x y))
   271  // Note Go assembler gets UCOMISx operand order wrong, but it is right here
   272  // Bug is accommodated at generation of assembly language.
   273  (Geq64F x y) -> (SETGEF (UCOMISD x y))
   274  (Geq32F x y) -> (SETGEF (UCOMISS x y))
   275  
   276  (Eq64  x y) -> (SETEQ (CMPQ x y))
   277  (Eq32  x y) -> (SETEQ (CMPL x y))
   278  (Eq16  x y) -> (SETEQ (CMPW x y))
   279  (Eq8   x y) -> (SETEQ (CMPB x y))
   280  (EqB   x y) -> (SETEQ (CMPB x y))
   281  (EqPtr x y) && config.PtrSize == 8 -> (SETEQ (CMPQ x y))
   282  (EqPtr x y) && config.PtrSize == 4 -> (SETEQ (CMPL x y))
   283  (Eq64F x y) -> (SETEQF (UCOMISD x y))
   284  (Eq32F x y) -> (SETEQF (UCOMISS x y))
   285  
   286  (Neq64  x y) -> (SETNE (CMPQ x y))
   287  (Neq32  x y) -> (SETNE (CMPL x y))
   288  (Neq16  x y) -> (SETNE (CMPW x y))
   289  (Neq8   x y) -> (SETNE (CMPB x y))
   290  (NeqB   x y) -> (SETNE (CMPB x y))
   291  (NeqPtr x y) && config.PtrSize == 8 -> (SETNE (CMPQ x y))
   292  (NeqPtr x y) && config.PtrSize == 4 -> (SETNE (CMPL x y))
   293  (Neq64F x y) -> (SETNEF (UCOMISD x y))
   294  (Neq32F x y) -> (SETNEF (UCOMISS x y))
   295  
   296  (Int64Hi x) -> (SHRQconst [32] x) // needed for amd64p32
   297  
   298  // Lowering loads
   299  (Load <t> ptr mem) && (is64BitInt(t) || isPtr(t) && config.PtrSize == 8) -> (MOVQload ptr mem)
   300  (Load <t> ptr mem) && (is32BitInt(t) || isPtr(t) && config.PtrSize == 4) -> (MOVLload ptr mem)
   301  (Load <t> ptr mem) && is16BitInt(t) -> (MOVWload ptr mem)
   302  (Load <t> ptr mem) && (t.IsBoolean() || is8BitInt(t)) -> (MOVBload ptr mem)
   303  (Load <t> ptr mem) && is32BitFloat(t) -> (MOVSSload ptr mem)
   304  (Load <t> ptr mem) && is64BitFloat(t) -> (MOVSDload ptr mem)
   305  
   306  // Lowering stores
   307  // These more-specific FP versions of Store pattern should come first.
   308  (Store {t} ptr val mem) && t.(*types.Type).Size() == 8 && is64BitFloat(val.Type) -> (MOVSDstore ptr val mem)
   309  (Store {t} ptr val mem) && t.(*types.Type).Size() == 4 && is32BitFloat(val.Type) -> (MOVSSstore ptr val mem)
   310  
   311  (Store {t} ptr val mem) && t.(*types.Type).Size() == 8 -> (MOVQstore ptr val mem)
   312  (Store {t} ptr val mem) && t.(*types.Type).Size() == 4 -> (MOVLstore ptr val mem)
   313  (Store {t} ptr val mem) && t.(*types.Type).Size() == 2 -> (MOVWstore ptr val mem)
   314  (Store {t} ptr val mem) && t.(*types.Type).Size() == 1 -> (MOVBstore ptr val mem)
   315  
   316  // Lowering moves
   317  (Move [0] _ _ mem) -> mem
   318  (Move [1] dst src mem) -> (MOVBstore dst (MOVBload src mem) mem)
   319  (Move [2] dst src mem) -> (MOVWstore dst (MOVWload src mem) mem)
   320  (Move [4] dst src mem) -> (MOVLstore dst (MOVLload src mem) mem)
   321  (Move [8] dst src mem) -> (MOVQstore dst (MOVQload src mem) mem)
   322  (Move [16] dst src mem) -> (MOVOstore dst (MOVOload src mem) mem)
   323  (Move [3] dst src mem) ->
   324  	(MOVBstore [2] dst (MOVBload [2] src mem)
   325  		(MOVWstore dst (MOVWload src mem) mem))
   326  (Move [5] dst src mem) ->
   327  	(MOVBstore [4] dst (MOVBload [4] src mem)
   328  		(MOVLstore dst (MOVLload src mem) mem))
   329  (Move [6] dst src mem) ->
   330  	(MOVWstore [4] dst (MOVWload [4] src mem)
   331  		(MOVLstore dst (MOVLload src mem) mem))
   332  (Move [7] dst src mem) ->
   333  	(MOVLstore [3] dst (MOVLload [3] src mem)
   334  		(MOVLstore dst (MOVLload src mem) mem))
   335  (Move [s] dst src mem) && s > 8 && s < 16 ->
   336  	(MOVQstore [s-8] dst (MOVQload [s-8] src mem)
   337  		(MOVQstore dst (MOVQload src mem) mem))
   338  
   339  // Adjust moves to be a multiple of 16 bytes.
   340  (Move [s] dst src mem)
   341  	&& s > 16 && s%16 != 0 && s%16 <= 8 ->
   342  	(Move [s-s%16]
   343  		(OffPtr <dst.Type> dst [s%16])
   344  		(OffPtr <src.Type> src [s%16])
   345  		(MOVQstore dst (MOVQload src mem) mem))
   346  (Move [s] dst src mem)
   347  	&& s > 16 && s%16 != 0 && s%16 > 8 ->
   348  	(Move [s-s%16]
   349  		(OffPtr <dst.Type> dst [s%16])
   350  		(OffPtr <src.Type> src [s%16])
   351  		(MOVOstore dst (MOVOload src mem) mem))
   352  
   353  // Medium copying uses a duff device.
   354  (Move [s] dst src mem)
   355  	&& s >= 32 && s <= 16*64 && s%16 == 0
   356  	&& !config.noDuffDevice ->
   357  	(DUFFCOPY [14*(64-s/16)] dst src mem)
   358  // 14 and 64 are magic constants.  14 is the number of bytes to encode:
   359  //	MOVUPS	(SI), X0
   360  //	ADDQ	$16, SI
   361  //	MOVUPS	X0, (DI)
   362  //	ADDQ	$16, DI
   363  // and 64 is the number of such blocks. See src/runtime/duff_amd64.s:duffcopy.
   364  
   365  // Large copying uses REP MOVSQ.
   366  (Move [s] dst src mem) && (s > 16*64 || config.noDuffDevice) && s%8 == 0 ->
   367  	(REPMOVSQ dst src (MOVQconst [s/8]) mem)
   368  
   369  // Lowering Zero instructions
   370  (Zero [0] _ mem) -> mem
   371  (Zero [1] destptr mem) -> (MOVBstoreconst [0] destptr mem)
   372  (Zero [2] destptr mem) -> (MOVWstoreconst [0] destptr mem)
   373  (Zero [4] destptr mem) -> (MOVLstoreconst [0] destptr mem)
   374  (Zero [8] destptr mem) -> (MOVQstoreconst [0] destptr mem)
   375  
   376  (Zero [3] destptr mem) ->
   377  	(MOVBstoreconst [makeValAndOff(0,2)] destptr
   378  		(MOVWstoreconst [0] destptr mem))
   379  (Zero [5] destptr mem) ->
   380  	(MOVBstoreconst [makeValAndOff(0,4)] destptr
   381  		(MOVLstoreconst [0] destptr mem))
   382  (Zero [6] destptr mem) ->
   383  	(MOVWstoreconst [makeValAndOff(0,4)] destptr
   384  		(MOVLstoreconst [0] destptr mem))
   385  (Zero [7] destptr mem) ->
   386  	(MOVLstoreconst [makeValAndOff(0,3)] destptr
   387  		(MOVLstoreconst [0] destptr mem))
   388  
   389  // Strip off any fractional word zeroing.
   390  (Zero [s] destptr mem) && s%8 != 0 && s > 8 ->
   391  	(Zero [s-s%8] (OffPtr <destptr.Type> destptr [s%8])
   392  		(MOVQstoreconst [0] destptr mem))
   393  
   394  // Zero small numbers of words directly.
   395  (Zero [16] destptr mem) ->
   396  	(MOVQstoreconst [makeValAndOff(0,8)] destptr
   397  		(MOVQstoreconst [0] destptr mem))
   398  (Zero [24] destptr mem) ->
   399  	(MOVQstoreconst [makeValAndOff(0,16)] destptr
   400  		(MOVQstoreconst [makeValAndOff(0,8)] destptr
   401  			(MOVQstoreconst [0] destptr mem)))
   402  (Zero [32] destptr mem) ->
   403  	(MOVQstoreconst [makeValAndOff(0,24)] destptr
   404  		(MOVQstoreconst [makeValAndOff(0,16)] destptr
   405  			(MOVQstoreconst [makeValAndOff(0,8)] destptr
   406  				(MOVQstoreconst [0] destptr mem))))
   407  
   408  // Medium zeroing uses a duff device.
   409  (Zero [s] destptr mem)
   410  	&& s <= 1024 && s%8 == 0 && s%16 != 0
   411  	&& !config.noDuffDevice ->
   412  	(Zero [s-8] (OffPtr <destptr.Type> [8] destptr) (MOVQstore destptr (MOVQconst [0]) mem))
   413  (Zero [s] destptr mem)
   414  	&& s <= 1024 && s%16 == 0 && !config.noDuffDevice ->
   415  	(DUFFZERO [s] destptr (MOVOconst [0]) mem)
   416  
   417  // Large zeroing uses REP STOSQ.
   418  (Zero [s] destptr mem)
   419  	&& (s > 1024 || (config.noDuffDevice && s > 32))
   420  	&& s%8 == 0 ->
   421  	(REPSTOSQ destptr (MOVQconst [s/8]) (MOVQconst [0]) mem)
   422  
   423  // Lowering constants
   424  (Const8   [val]) -> (MOVLconst [val])
   425  (Const16  [val]) -> (MOVLconst [val])
   426  (Const32  [val]) -> (MOVLconst [val])
   427  (Const64  [val]) -> (MOVQconst [val])
   428  (Const32F [val]) -> (MOVSSconst [val])
   429  (Const64F [val]) -> (MOVSDconst [val])
   430  (ConstNil) && config.PtrSize == 8 -> (MOVQconst [0])
   431  (ConstNil) && config.PtrSize == 4 -> (MOVLconst [0])
   432  (ConstBool [b]) -> (MOVLconst [b])
   433  
   434  // Lowering calls
   435  (StaticCall [argwid] {target} mem) -> (CALLstatic [argwid] {target} mem)
   436  (ClosureCall [argwid] entry closure mem) -> (CALLclosure [argwid] entry closure mem)
   437  (InterCall [argwid] entry mem) -> (CALLinter [argwid] entry mem)
   438  
   439  // Miscellaneous
   440  (Convert <t> x mem) && config.PtrSize == 8 -> (MOVQconvert <t> x mem)
   441  (Convert <t> x mem) && config.PtrSize == 4 -> (MOVLconvert <t> x mem)
   442  (IsNonNil p) && config.PtrSize == 8 -> (SETNE (TESTQ p p))
   443  (IsNonNil p) && config.PtrSize == 4 -> (SETNE (TESTL p p))
   444  (IsInBounds idx len) && config.PtrSize == 8 -> (SETB (CMPQ idx len))
   445  (IsInBounds idx len) && config.PtrSize == 4 -> (SETB (CMPL idx len))
   446  (IsSliceInBounds idx len) && config.PtrSize == 8 -> (SETBE (CMPQ idx len))
   447  (IsSliceInBounds idx len) && config.PtrSize == 4 -> (SETBE (CMPL idx len))
   448  (NilCheck ptr mem) -> (LoweredNilCheck ptr mem)
   449  (GetG mem) -> (LoweredGetG mem)
   450  (GetClosurePtr) -> (LoweredGetClosurePtr)
   451  (Addr {sym} base) && config.PtrSize == 8 -> (LEAQ {sym} base)
   452  (Addr {sym} base) && config.PtrSize == 4 -> (LEAL {sym} base)
   453  
   454  // block rewrites
   455  (If (SETL  cmp) yes no) -> (LT  cmp yes no)
   456  (If (SETLE cmp) yes no) -> (LE  cmp yes no)
   457  (If (SETG  cmp) yes no) -> (GT  cmp yes no)
   458  (If (SETGE cmp) yes no) -> (GE  cmp yes no)
   459  (If (SETEQ cmp) yes no) -> (EQ  cmp yes no)
   460  (If (SETNE cmp) yes no) -> (NE  cmp yes no)
   461  (If (SETB  cmp) yes no) -> (ULT cmp yes no)
   462  (If (SETBE cmp) yes no) -> (ULE cmp yes no)
   463  (If (SETA  cmp) yes no) -> (UGT cmp yes no)
   464  (If (SETAE cmp) yes no) -> (UGE cmp yes no)
   465  
   466  // Special case for floating point - LF/LEF not generated
   467  (If (SETGF  cmp) yes no) -> (UGT  cmp yes no)
   468  (If (SETGEF cmp) yes no) -> (UGE  cmp yes no)
   469  (If (SETEQF cmp) yes no) -> (EQF  cmp yes no)
   470  (If (SETNEF cmp) yes no) -> (NEF  cmp yes no)
   471  
   472  (If cond yes no) -> (NE (TESTB cond cond) yes no)
   473  
   474  // Atomic loads.  Other than preserving their ordering with respect to other loads, nothing special here.
   475  (AtomicLoad32 ptr mem) -> (MOVLatomicload ptr mem)
   476  (AtomicLoad64 ptr mem) -> (MOVQatomicload ptr mem)
   477  (AtomicLoadPtr ptr mem) && config.PtrSize == 8 -> (MOVQatomicload ptr mem)
   478  (AtomicLoadPtr ptr mem) && config.PtrSize == 4 -> (MOVLatomicload ptr mem)
   479  
   480  // Atomic stores.  We use XCHG to prevent the hardware reordering a subsequent load.
   481  // TODO: most runtime uses of atomic stores don't need that property.  Use normal stores for those?
   482  (AtomicStore32 ptr val mem) -> (Select1 (XCHGL <types.NewTuple(typ.UInt32,types.TypeMem)> val ptr mem))
   483  (AtomicStore64 ptr val mem) -> (Select1 (XCHGQ <types.NewTuple(typ.UInt64,types.TypeMem)> val ptr mem))
   484  (AtomicStorePtrNoWB ptr val mem) && config.PtrSize == 8 -> (Select1 (XCHGQ <types.NewTuple(typ.BytePtr,types.TypeMem)> val ptr mem))
   485  (AtomicStorePtrNoWB ptr val mem) && config.PtrSize == 4 -> (Select1 (XCHGL <types.NewTuple(typ.BytePtr,types.TypeMem)> val ptr mem))
   486  
   487  // Atomic exchanges.
   488  (AtomicExchange32 ptr val mem) -> (XCHGL val ptr mem)
   489  (AtomicExchange64 ptr val mem) -> (XCHGQ val ptr mem)
   490  
   491  // Atomic adds.
   492  (AtomicAdd32 ptr val mem) -> (AddTupleFirst32 val (XADDLlock val ptr mem))
   493  (AtomicAdd64 ptr val mem) -> (AddTupleFirst64 val (XADDQlock val ptr mem))
   494  (Select0 <t> (AddTupleFirst32 val tuple)) -> (ADDL val (Select0 <t> tuple))
   495  (Select1     (AddTupleFirst32   _ tuple)) -> (Select1 tuple)
   496  (Select0 <t> (AddTupleFirst64 val tuple)) -> (ADDQ val (Select0 <t> tuple))
   497  (Select1     (AddTupleFirst64   _ tuple)) -> (Select1 tuple)
   498  
   499  // Atomic compare and swap.
   500  (AtomicCompareAndSwap32 ptr old new_ mem) -> (CMPXCHGLlock ptr old new_ mem)
   501  (AtomicCompareAndSwap64 ptr old new_ mem) -> (CMPXCHGQlock ptr old new_ mem)
   502  
   503  // Atomic memory updates.
   504  (AtomicAnd8 ptr val mem) -> (ANDBlock ptr val mem)
   505  (AtomicOr8 ptr val mem) -> (ORBlock ptr val mem)
   506  
   507  // ***************************
   508  // Above: lowering rules
   509  // Below: optimizations
   510  // ***************************
   511  // TODO: Should the optimizations be a separate pass?
   512  
   513  // Fold boolean tests into blocks
   514  (NE (TESTB (SETL  cmp) (SETL  cmp)) yes no) -> (LT  cmp yes no)
   515  (NE (TESTB (SETLE cmp) (SETLE cmp)) yes no) -> (LE  cmp yes no)
   516  (NE (TESTB (SETG  cmp) (SETG  cmp)) yes no) -> (GT  cmp yes no)
   517  (NE (TESTB (SETGE cmp) (SETGE cmp)) yes no) -> (GE  cmp yes no)
   518  (NE (TESTB (SETEQ cmp) (SETEQ cmp)) yes no) -> (EQ  cmp yes no)
   519  (NE (TESTB (SETNE cmp) (SETNE cmp)) yes no) -> (NE  cmp yes no)
   520  (NE (TESTB (SETB  cmp) (SETB  cmp)) yes no) -> (ULT cmp yes no)
   521  (NE (TESTB (SETBE cmp) (SETBE cmp)) yes no) -> (ULE cmp yes no)
   522  (NE (TESTB (SETA  cmp) (SETA  cmp)) yes no) -> (UGT cmp yes no)
   523  (NE (TESTB (SETAE cmp) (SETAE cmp)) yes no) -> (UGE cmp yes no)
   524  
   525  // Recognize bit tests: a&(1<<b) != 0 for b suitably bounded
   526  // Note that ULT and SETB check the carry flag; they are identical to CS and SETCS.
   527  // Same, mutatis mutandis, for UGE and SETAE, and CC and SETCC.
   528  (NE (TESTL (SHLL (MOVLconst [1]) x) y)) && !config.nacl -> (ULT (BTL x y))
   529  (EQ (TESTL (SHLL (MOVLconst [1]) x) y)) && !config.nacl -> (UGE (BTL x y))
   530  (NE (TESTQ (SHLQ (MOVQconst [1]) x) y)) && !config.nacl -> (ULT (BTQ x y))
   531  (EQ (TESTQ (SHLQ (MOVQconst [1]) x) y)) && !config.nacl -> (UGE (BTQ x y))
   532  (NE (TESTLconst [c] x)) && isPowerOfTwo(c) && log2(c) < 32 && !config.nacl -> (ULT (BTLconst [log2(c)] x))
   533  (EQ (TESTLconst [c] x)) && isPowerOfTwo(c) && log2(c) < 32 && !config.nacl -> (UGE (BTLconst [log2(c)] x))
   534  (NE (TESTQconst [c] x)) && isPowerOfTwo(c) && log2(c) < 64 && !config.nacl -> (ULT (BTQconst [log2(c)] x))
   535  (EQ (TESTQconst [c] x)) && isPowerOfTwo(c) && log2(c) < 64 && !config.nacl -> (UGE (BTQconst [log2(c)] x))
   536  (NE (TESTQ (MOVQconst [c]) x)) && isPowerOfTwo(c) && log2(c) < 64 && !config.nacl -> (ULT (BTQconst [log2(c)] x))
   537  (EQ (TESTQ (MOVQconst [c]) x)) && isPowerOfTwo(c) && log2(c) < 64 && !config.nacl -> (UGE (BTQconst [log2(c)] x))
   538  (SETNE (TESTL (SHLL (MOVLconst [1]) x) y)) && !config.nacl -> (SETB  (BTL x y))
   539  (SETEQ (TESTL (SHLL (MOVLconst [1]) x) y)) && !config.nacl -> (SETAE (BTL x y))
   540  (SETNE (TESTQ (SHLQ (MOVQconst [1]) x) y)) && !config.nacl -> (SETB  (BTQ x y))
   541  (SETEQ (TESTQ (SHLQ (MOVQconst [1]) x) y)) && !config.nacl -> (SETAE (BTQ x y))
   542  (SETNE (TESTLconst [c] x)) && isPowerOfTwo(c) && log2(c) < 32 && !config.nacl -> (SETB  (BTLconst [log2(c)] x))
   543  (SETEQ (TESTLconst [c] x)) && isPowerOfTwo(c) && log2(c) < 32 && !config.nacl -> (SETAE (BTLconst [log2(c)] x))
   544  (SETNE (TESTQconst [c] x)) && isPowerOfTwo(c) && log2(c) < 64 && !config.nacl -> (SETB  (BTQconst [log2(c)] x))
   545  (SETEQ (TESTQconst [c] x)) && isPowerOfTwo(c) && log2(c) < 64 && !config.nacl -> (SETAE (BTQconst [log2(c)] x))
   546  (SETNE (TESTQ (MOVQconst [c]) x)) && isPowerOfTwo(c) && log2(c) < 64 && !config.nacl -> (SETB  (BTQconst [log2(c)] x))
   547  (SETEQ (TESTQ (MOVQconst [c]) x)) && isPowerOfTwo(c) && log2(c) < 64 && !config.nacl -> (SETAE (BTQconst [log2(c)] x))
   548  
   549  // Fold boolean negation into SETcc.
   550  (XORLconst [1] (SETNE x)) -> (SETEQ x)
   551  (XORLconst [1] (SETEQ x)) -> (SETNE x)
   552  (XORLconst [1] (SETL  x)) -> (SETGE x)
   553  (XORLconst [1] (SETGE x)) -> (SETL  x)
   554  (XORLconst [1] (SETLE x)) -> (SETG  x)
   555  (XORLconst [1] (SETG  x)) -> (SETLE x)
   556  (XORLconst [1] (SETB  x)) -> (SETAE x)
   557  (XORLconst [1] (SETAE x)) -> (SETB  x)
   558  (XORLconst [1] (SETBE x)) -> (SETA  x)
   559  (XORLconst [1] (SETA  x)) -> (SETBE x)
   560  
   561  // Convert BTQconst to BTLconst if possible. It has a shorter encoding.
   562  (BTQconst [c] x) && c < 32 -> (BTLconst [c] x)
   563  
   564  // Special case for floating point - LF/LEF not generated
   565  (NE (TESTB (SETGF  cmp) (SETGF  cmp)) yes no) -> (UGT  cmp yes no)
   566  (NE (TESTB (SETGEF cmp) (SETGEF cmp)) yes no) -> (UGE  cmp yes no)
   567  (NE (TESTB (SETEQF cmp) (SETEQF cmp)) yes no) -> (EQF  cmp yes no)
   568  (NE (TESTB (SETNEF cmp) (SETNEF cmp)) yes no) -> (NEF  cmp yes no)
   569  
   570  // Disabled because it interferes with the pattern match above and makes worse code.
   571  // (SETNEF x) -> (ORQ (SETNE <typ.Int8> x) (SETNAN <typ.Int8> x))
   572  // (SETEQF x) -> (ANDQ (SETEQ <typ.Int8> x) (SETORD <typ.Int8> x))
   573  
   574  // fold constants into instructions
   575  (ADDQ x (MOVQconst [c])) && is32Bit(c) -> (ADDQconst [c] x)
   576  (ADDL x (MOVLconst [c])) -> (ADDLconst [c] x)
   577  
   578  (SUBQ x (MOVQconst [c])) && is32Bit(c) -> (SUBQconst x [c])
   579  (SUBQ (MOVQconst [c]) x) && is32Bit(c) -> (NEGQ (SUBQconst <v.Type> x [c]))
   580  (SUBL x (MOVLconst [c])) -> (SUBLconst x [c])
   581  (SUBL (MOVLconst [c]) x) -> (NEGL (SUBLconst <v.Type> x [c]))
   582  
   583  (MULQ x (MOVQconst [c])) && is32Bit(c) -> (MULQconst [c] x)
   584  (MULL x (MOVLconst [c])) -> (MULLconst [c] x)
   585  
   586  (ANDQ x (MOVQconst [c])) && is32Bit(c) -> (ANDQconst [c] x)
   587  (ANDL x (MOVLconst [c])) -> (ANDLconst [c] x)
   588  
   589  (ANDLconst [c] (ANDLconst [d] x)) -> (ANDLconst [c & d] x)
   590  (ANDQconst [c] (ANDQconst [d] x)) -> (ANDQconst [c & d] x)
   591  
   592  (XORLconst [c] (XORLconst [d] x)) -> (XORLconst [c ^ d] x)
   593  (XORQconst [c] (XORQconst [d] x)) -> (XORQconst [c ^ d] x)
   594  
   595  (MULLconst [c] (MULLconst [d] x)) -> (MULLconst [int64(int32(c * d))] x)
   596  (MULQconst [c] (MULQconst [d] x)) && is32Bit(c*d) -> (MULQconst [c * d] x)
   597  
   598  (ORQ x (MOVQconst [c])) && is32Bit(c) -> (ORQconst [c] x)
   599  (ORL x (MOVLconst [c])) -> (ORLconst [c] x)
   600  
   601  (XORQ x (MOVQconst [c])) && is32Bit(c) -> (XORQconst [c] x)
   602  (XORL x (MOVLconst [c])) -> (XORLconst [c] x)
   603  
   604  (SHLQ x (MOVQconst [c])) -> (SHLQconst [c&63] x)
   605  (SHLQ x (MOVLconst [c])) -> (SHLQconst [c&63] x)
   606  
   607  (SHLL x (MOVQconst [c])) -> (SHLLconst [c&31] x)
   608  (SHLL x (MOVLconst [c])) -> (SHLLconst [c&31] x)
   609  
   610  (SHRQ x (MOVQconst [c])) -> (SHRQconst [c&63] x)
   611  (SHRQ x (MOVLconst [c])) -> (SHRQconst [c&63] x)
   612  
   613  (SHRL x (MOVQconst [c])) -> (SHRLconst [c&31] x)
   614  (SHRL x (MOVLconst [c])) -> (SHRLconst [c&31] x)
   615  
   616  (SHRW x (MOVQconst [c])) && c&31 < 16 -> (SHRWconst [c&31] x)
   617  (SHRW x (MOVLconst [c])) && c&31 < 16 -> (SHRWconst [c&31] x)
   618  (SHRW _ (MOVQconst [c])) && c&31 >= 16 -> (MOVLconst [0])
   619  (SHRW _ (MOVLconst [c])) && c&31 >= 16 -> (MOVLconst [0])
   620  
   621  (SHRB x (MOVQconst [c])) && c&31 < 8 -> (SHRBconst [c&31] x)
   622  (SHRB x (MOVLconst [c])) && c&31 < 8 -> (SHRBconst [c&31] x)
   623  (SHRB _ (MOVQconst [c])) && c&31 >= 8 -> (MOVLconst [0])
   624  (SHRB _ (MOVLconst [c])) && c&31 >= 8 -> (MOVLconst [0])
   625  
   626  (SARQ x (MOVQconst [c])) -> (SARQconst [c&63] x)
   627  (SARQ x (MOVLconst [c])) -> (SARQconst [c&63] x)
   628  
   629  (SARL x (MOVQconst [c])) -> (SARLconst [c&31] x)
   630  (SARL x (MOVLconst [c])) -> (SARLconst [c&31] x)
   631  
   632  (SARW x (MOVQconst [c])) -> (SARWconst [min(c&31,15)] x)
   633  (SARW x (MOVLconst [c])) -> (SARWconst [min(c&31,15)] x)
   634  
   635  (SARB x (MOVQconst [c])) -> (SARBconst [min(c&31,7)] x)
   636  (SARB x (MOVLconst [c])) -> (SARBconst [min(c&31,7)] x)
   637  
   638  // Operations which don't affect the low 6/5 bits of the shift amount are NOPs.
   639  (SHLQ x (ADDQconst [c] y)) && c & 63 == 0 -> (SHLQ x y)
   640  (SHRQ x (ADDQconst [c] y)) && c & 63 == 0 -> (SHRQ x y)
   641  (SARQ x (ADDQconst [c] y)) && c & 63 == 0 -> (SARQ x y)
   642  (SHLQ x (NEGQ <t> (ADDQconst [c] y))) && c & 63 == 0 -> (SHLQ x (NEGQ <t> y))
   643  (SHRQ x (NEGQ <t> (ADDQconst [c] y))) && c & 63 == 0 -> (SHRQ x (NEGQ <t> y))
   644  (SARQ x (NEGQ <t> (ADDQconst [c] y))) && c & 63 == 0 -> (SARQ x (NEGQ <t> y))
   645  (SHLQ x (ANDQconst [c] y)) && c & 63 == 63 -> (SHLQ x y)
   646  (SHRQ x (ANDQconst [c] y)) && c & 63 == 63 -> (SHRQ x y)
   647  (SARQ x (ANDQconst [c] y)) && c & 63 == 63 -> (SARQ x y)
   648  (SHLQ x (NEGQ <t> (ANDQconst [c] y))) && c & 63 == 63 -> (SHLQ x (NEGQ <t> y))
   649  (SHRQ x (NEGQ <t> (ANDQconst [c] y))) && c & 63 == 63 -> (SHRQ x (NEGQ <t> y))
   650  (SARQ x (NEGQ <t> (ANDQconst [c] y))) && c & 63 == 63 -> (SARQ x (NEGQ <t> y))
   651  
   652  (SHLL x (ADDQconst [c] y)) && c & 31 == 0 -> (SHLL x y)
   653  (SHRL x (ADDQconst [c] y)) && c & 31 == 0 -> (SHRL x y)
   654  (SARL x (ADDQconst [c] y)) && c & 31 == 0 -> (SARL x y)
   655  (SHLL x (NEGQ <t> (ADDQconst [c] y))) && c & 31 == 0 -> (SHLL x (NEGQ <t> y))
   656  (SHRL x (NEGQ <t> (ADDQconst [c] y))) && c & 31 == 0 -> (SHRL x (NEGQ <t> y))
   657  (SARL x (NEGQ <t> (ADDQconst [c] y))) && c & 31 == 0 -> (SARL x (NEGQ <t> y))
   658  (SHLL x (ANDQconst [c] y)) && c & 31 == 31 -> (SHLL x y)
   659  (SHRL x (ANDQconst [c] y)) && c & 31 == 31 -> (SHRL x y)
   660  (SARL x (ANDQconst [c] y)) && c & 31 == 31 -> (SARL x y)
   661  (SHLL x (NEGQ <t> (ANDQconst [c] y))) && c & 31 == 31 -> (SHLL x (NEGQ <t> y))
   662  (SHRL x (NEGQ <t> (ANDQconst [c] y))) && c & 31 == 31 -> (SHRL x (NEGQ <t> y))
   663  (SARL x (NEGQ <t> (ANDQconst [c] y))) && c & 31 == 31 -> (SARL x (NEGQ <t> y))
   664  
   665  (SHLQ x (ADDLconst [c] y)) && c & 63 == 0 -> (SHLQ x y)
   666  (SHRQ x (ADDLconst [c] y)) && c & 63 == 0 -> (SHRQ x y)
   667  (SARQ x (ADDLconst [c] y)) && c & 63 == 0 -> (SARQ x y)
   668  (SHLQ x (NEGL <t> (ADDLconst [c] y))) && c & 63 == 0 -> (SHLQ x (NEGL <t> y))
   669  (SHRQ x (NEGL <t> (ADDLconst [c] y))) && c & 63 == 0 -> (SHRQ x (NEGL <t> y))
   670  (SARQ x (NEGL <t> (ADDLconst [c] y))) && c & 63 == 0 -> (SARQ x (NEGL <t> y))
   671  (SHLQ x (ANDLconst [c] y)) && c & 63 == 63 -> (SHLQ x y)
   672  (SHRQ x (ANDLconst [c] y)) && c & 63 == 63 -> (SHRQ x y)
   673  (SARQ x (ANDLconst [c] y)) && c & 63 == 63 -> (SARQ x y)
   674  (SHLQ x (NEGL <t> (ANDLconst [c] y))) && c & 63 == 63 -> (SHLQ x (NEGL <t> y))
   675  (SHRQ x (NEGL <t> (ANDLconst [c] y))) && c & 63 == 63 -> (SHRQ x (NEGL <t> y))
   676  (SARQ x (NEGL <t> (ANDLconst [c] y))) && c & 63 == 63 -> (SARQ x (NEGL <t> y))
   677  
   678  (SHLL x (ADDLconst [c] y)) && c & 31 == 0 -> (SHLL x y)
   679  (SHRL x (ADDLconst [c] y)) && c & 31 == 0 -> (SHRL x y)
   680  (SARL x (ADDLconst [c] y)) && c & 31 == 0 -> (SARL x y)
   681  (SHLL x (NEGL <t> (ADDLconst [c] y))) && c & 31 == 0 -> (SHLL x (NEGL <t> y))
   682  (SHRL x (NEGL <t> (ADDLconst [c] y))) && c & 31 == 0 -> (SHRL x (NEGL <t> y))
   683  (SARL x (NEGL <t> (ADDLconst [c] y))) && c & 31 == 0 -> (SARL x (NEGL <t> y))
   684  (SHLL x (ANDLconst [c] y)) && c & 31 == 31 -> (SHLL x y)
   685  (SHRL x (ANDLconst [c] y)) && c & 31 == 31 -> (SHRL x y)
   686  (SARL x (ANDLconst [c] y)) && c & 31 == 31 -> (SARL x y)
   687  (SHLL x (NEGL <t> (ANDLconst [c] y))) && c & 31 == 31 -> (SHLL x (NEGL <t> y))
   688  (SHRL x (NEGL <t> (ANDLconst [c] y))) && c & 31 == 31 -> (SHRL x (NEGL <t> y))
   689  (SARL x (NEGL <t> (ANDLconst [c] y))) && c & 31 == 31 -> (SARL x (NEGL <t> y))
   690  
   691  // Constant rotate instructions
   692  (ADDQ (SHLQconst x [c]) (SHRQconst x [d])) && d==64-c -> (ROLQconst x [c])
   693  ( ORQ (SHLQconst x [c]) (SHRQconst x [d])) && d==64-c -> (ROLQconst x [c])
   694  (XORQ (SHLQconst x [c]) (SHRQconst x [d])) && d==64-c -> (ROLQconst x [c])
   695  
   696  (ADDL (SHLLconst x [c]) (SHRLconst x [d])) && d==32-c -> (ROLLconst x [c])
   697  ( ORL (SHLLconst x [c]) (SHRLconst x [d])) && d==32-c -> (ROLLconst x [c])
   698  (XORL (SHLLconst x [c]) (SHRLconst x [d])) && d==32-c -> (ROLLconst x [c])
   699  
   700  (ADDL <t> (SHLLconst x [c]) (SHRWconst x [d])) && d==16-c && c < 16 && t.Size() == 2 -> (ROLWconst x [c])
   701  ( ORL <t> (SHLLconst x [c]) (SHRWconst x [d])) && d==16-c && c < 16 && t.Size() == 2 -> (ROLWconst x [c])
   702  (XORL <t> (SHLLconst x [c]) (SHRWconst x [d])) && d==16-c && c < 16 && t.Size() == 2 -> (ROLWconst x [c])
   703  
   704  (ADDL <t> (SHLLconst x [c]) (SHRBconst x [d])) && d==8-c  && c < 8 && t.Size() == 1 -> (ROLBconst x [c])
   705  ( ORL <t> (SHLLconst x [c]) (SHRBconst x [d])) && d==8-c  && c < 8 && t.Size() == 1 -> (ROLBconst x [c])
   706  (XORL <t> (SHLLconst x [c]) (SHRBconst x [d])) && d==8-c  && c < 8 && t.Size() == 1 -> (ROLBconst x [c])
   707  
   708  (ROLQconst [c] (ROLQconst [d] x)) -> (ROLQconst [(c+d)&63] x)
   709  (ROLLconst [c] (ROLLconst [d] x)) -> (ROLLconst [(c+d)&31] x)
   710  (ROLWconst [c] (ROLWconst [d] x)) -> (ROLWconst [(c+d)&15] x)
   711  (ROLBconst [c] (ROLBconst [d] x)) -> (ROLBconst [(c+d)& 7] x)
   712  
   713  // Non-constant rotates.
   714  // We want to issue a rotate when the Go source contains code like
   715  //     y &= 63
   716  //     x << y | x >> (64-y)
   717  // The shift rules above convert << to SHLx and >> to SHRx.
   718  // SHRx converts its shift argument from 64-y to -y.
   719  // A tricky situation occurs when y==0. Then the original code would be:
   720  //     x << 0 | x >> 64
   721  // But x >> 64 is 0, not x. So there's an additional mask that is ANDed in
   722  // to force the second term to 0. We don't need that mask, but we must match
   723  // it in order to strip it out.
   724  (ORQ (SHLQ x y) (ANDQ (SHRQ x (NEGQ y)) (SBBQcarrymask (CMPQconst (NEGQ (ADDQconst (ANDQconst y [63]) [-64])) [64])))) -> (ROLQ x y)
   725  (ORQ (SHLQ x y) (ANDQ (SHRQ x (NEGL y)) (SBBQcarrymask (CMPLconst (NEGL (ADDLconst (ANDLconst y [63]) [-64])) [64])))) -> (ROLQ x y)
   726  (ORQ (SHRQ x y) (ANDQ (SHLQ x (NEGQ y)) (SBBQcarrymask (CMPQconst (NEGQ (ADDQconst (ANDQconst y [63]) [-64])) [64])))) -> (RORQ x y)
   727  (ORQ (SHRQ x y) (ANDQ (SHLQ x (NEGL y)) (SBBQcarrymask (CMPLconst (NEGL (ADDLconst (ANDLconst y [63]) [-64])) [64])))) -> (RORQ x y)
   728  
   729  (ORL (SHLL x y) (ANDL (SHRL x (NEGQ y)) (SBBLcarrymask (CMPQconst (NEGQ (ADDQconst (ANDQconst y [31]) [-32])) [32])))) -> (ROLL x y)
   730  (ORL (SHLL x y) (ANDL (SHRL x (NEGL y)) (SBBLcarrymask (CMPLconst (NEGL (ADDLconst (ANDLconst y [31]) [-32])) [32])))) -> (ROLL x y)
   731  (ORL (SHRL x y) (ANDL (SHLL x (NEGQ y)) (SBBLcarrymask (CMPQconst (NEGQ (ADDQconst (ANDQconst y [31]) [-32])) [32])))) -> (RORL x y)
   732  (ORL (SHRL x y) (ANDL (SHLL x (NEGL y)) (SBBLcarrymask (CMPLconst (NEGL (ADDLconst (ANDLconst y [31]) [-32])) [32])))) -> (RORL x y)
   733  
   734  // Help with rotate detection
   735  (CMPQconst (NEGQ (ADDQconst [-16] (ANDQconst [15] _))) [32]) -> (FlagLT_ULT)
   736  (CMPQconst (NEGQ (ADDQconst [ -8] (ANDQconst  [7] _))) [32]) -> (FlagLT_ULT)
   737  
   738  (ORL (SHLL x (ANDQconst y [15]))
   739       (ANDL (SHRW x (NEGQ (ADDQconst (ANDQconst y [15]) [-16])))
   740             (SBBLcarrymask (CMPQconst (NEGQ (ADDQconst (ANDQconst y [15]) [-16])) [16]))))
   741    && v.Type.Size() == 2
   742    -> (ROLW x y)
   743  (ORL (SHLL x (ANDLconst y [15]))
   744       (ANDL (SHRW x (NEGL (ADDLconst (ANDLconst y [15]) [-16])))
   745             (SBBLcarrymask (CMPLconst (NEGL (ADDLconst (ANDLconst y [15]) [-16])) [16]))))
   746    && v.Type.Size() == 2
   747    -> (ROLW x y)
   748  (ORL (SHRW x (ANDQconst y [15]))
   749       (SHLL x (NEGQ (ADDQconst (ANDQconst y [15]) [-16]))))
   750    && v.Type.Size() == 2
   751    -> (RORW x y)
   752  (ORL (SHRW x (ANDLconst y [15]))
   753       (SHLL x (NEGL (ADDLconst (ANDLconst y [15]) [-16]))))
   754    && v.Type.Size() == 2
   755    -> (RORW x y)
   756  
   757  (ORL (SHLL x (ANDQconst y [ 7]))
   758       (ANDL (SHRB x (NEGQ (ADDQconst (ANDQconst y [ 7]) [ -8])))
   759             (SBBLcarrymask (CMPQconst (NEGQ (ADDQconst (ANDQconst y [ 7]) [ -8])) [ 8]))))
   760    && v.Type.Size() == 1
   761    -> (ROLB x y)
   762  (ORL (SHLL x (ANDLconst y [ 7]))
   763       (ANDL (SHRB x (NEGL (ADDLconst (ANDLconst y [ 7]) [ -8])))
   764             (SBBLcarrymask (CMPLconst (NEGL (ADDLconst (ANDLconst y [ 7]) [ -8])) [ 8]))))
   765    && v.Type.Size() == 1
   766    -> (ROLB x y)
   767  (ORL (SHRB x (ANDQconst y [ 7]))
   768       (SHLL x (NEGQ (ADDQconst (ANDQconst y [ 7]) [ -8]))))
   769    && v.Type.Size() == 1
   770    -> (RORB x y)
   771  (ORL (SHRB x (ANDLconst y [ 7]))
   772       (SHLL x (NEGL (ADDLconst (ANDLconst y [ 7]) [ -8]))))
   773    && v.Type.Size() == 1
   774    -> (RORB x y)
   775  
   776  // rotate left negative = rotate right
   777  (ROLQ x (NEGQ y)) -> (RORQ x y)
   778  (ROLQ x (NEGL y)) -> (RORQ x y)
   779  (ROLL x (NEGQ y)) -> (RORL x y)
   780  (ROLL x (NEGL y)) -> (RORL x y)
   781  (ROLW x (NEGQ y)) -> (RORW x y)
   782  (ROLW x (NEGL y)) -> (RORW x y)
   783  (ROLB x (NEGQ y)) -> (RORB x y)
   784  (ROLB x (NEGL y)) -> (RORB x y)
   785  
   786  // rotate right negative = rotate left
   787  (RORQ x (NEGQ y)) -> (ROLQ x y)
   788  (RORQ x (NEGL y)) -> (ROLQ x y)
   789  (RORL x (NEGQ y)) -> (ROLL x y)
   790  (RORL x (NEGL y)) -> (ROLL x y)
   791  (RORW x (NEGQ y)) -> (ROLW x y)
   792  (RORW x (NEGL y)) -> (ROLW x y)
   793  (RORB x (NEGQ y)) -> (ROLB x y)
   794  (RORB x (NEGL y)) -> (ROLB x y)
   795  
   796  // rotate by constants
   797  (ROLQ x (MOVQconst [c])) -> (ROLQconst [c&63] x)
   798  (ROLQ x (MOVLconst [c])) -> (ROLQconst [c&63] x)
   799  (ROLL x (MOVQconst [c])) -> (ROLLconst [c&31] x)
   800  (ROLL x (MOVLconst [c])) -> (ROLLconst [c&31] x)
   801  (ROLW x (MOVQconst [c])) -> (ROLWconst [c&15] x)
   802  (ROLW x (MOVLconst [c])) -> (ROLWconst [c&15] x)
   803  (ROLB x (MOVQconst [c])) -> (ROLBconst [c&7 ] x)
   804  (ROLB x (MOVLconst [c])) -> (ROLBconst [c&7 ] x)
   805  
   806  (RORQ x (MOVQconst [c])) -> (ROLQconst [(-c)&63] x)
   807  (RORQ x (MOVLconst [c])) -> (ROLQconst [(-c)&63] x)
   808  (RORL x (MOVQconst [c])) -> (ROLLconst [(-c)&31] x)
   809  (RORL x (MOVLconst [c])) -> (ROLLconst [(-c)&31] x)
   810  (RORW x (MOVQconst [c])) -> (ROLWconst [(-c)&15] x)
   811  (RORW x (MOVLconst [c])) -> (ROLWconst [(-c)&15] x)
   812  (RORB x (MOVQconst [c])) -> (ROLBconst [(-c)&7 ] x)
   813  (RORB x (MOVLconst [c])) -> (ROLBconst [(-c)&7 ] x)
   814  
   815  // Constant shift simplifications
   816  (SHLQconst x [0]) -> x
   817  (SHRQconst x [0]) -> x
   818  (SARQconst x [0]) -> x
   819  
   820  (SHLLconst x [0]) -> x
   821  (SHRLconst x [0]) -> x
   822  (SARLconst x [0]) -> x
   823  
   824  (SHRWconst x [0]) -> x
   825  (SARWconst x [0]) -> x
   826  
   827  (SHRBconst x [0]) -> x
   828  (SARBconst x [0]) -> x
   829  
   830  (ROLQconst x [0]) -> x
   831  (ROLLconst x [0]) -> x
   832  (ROLWconst x [0]) -> x
   833  (ROLBconst x [0]) -> x
   834  
   835  // Note: the word and byte shifts keep the low 5 bits (not the low 4 or 3 bits)
   836  // because the x86 instructions are defined to use all 5 bits of the shift even
   837  // for the small shifts. I don't think we'll ever generate a weird shift (e.g.
   838  // (SHRW x (MOVLconst [24])), but just in case.
   839  
   840  (CMPQ x (MOVQconst [c])) && is32Bit(c) -> (CMPQconst x [c])
   841  (CMPQ (MOVQconst [c]) x) && is32Bit(c) -> (InvertFlags (CMPQconst x [c]))
   842  (CMPL x (MOVLconst [c])) -> (CMPLconst x [c])
   843  (CMPL (MOVLconst [c]) x) -> (InvertFlags (CMPLconst x [c]))
   844  (CMPW x (MOVLconst [c])) -> (CMPWconst x [int64(int16(c))])
   845  (CMPW (MOVLconst [c]) x) -> (InvertFlags (CMPWconst x [int64(int16(c))]))
   846  (CMPB x (MOVLconst [c])) -> (CMPBconst x [int64(int8(c))])
   847  (CMPB (MOVLconst [c]) x) -> (InvertFlags (CMPBconst x [int64(int8(c))]))
   848  
   849  // Using MOVZX instead of AND is cheaper.
   850  (ANDLconst [0xFF] x) -> (MOVBQZX x)
   851  (ANDLconst [0xFFFF] x) -> (MOVWQZX x)
   852  (ANDQconst [0xFF] x) -> (MOVBQZX x)
   853  (ANDQconst [0xFFFF] x) -> (MOVWQZX x)
   854  (ANDQconst [0xFFFFFFFF] x) -> (MOVLQZX x)
   855  
   856  // strength reduction
   857  // Assumes that the following costs from https://gmplib.org/~tege/x86-timing.pdf:
   858  //    1 - addq, shlq, leaq, negq
   859  //    3 - imulq
   860  // This limits the rewrites to two instructions.
   861  // TODO: 27, 81
   862  (MULQconst [-1] x) -> (NEGQ x)
   863  (MULQconst [0] _) -> (MOVQconst [0])
   864  (MULQconst [1] x) -> x
   865  (MULQconst [3] x) -> (LEAQ2 x x)
   866  (MULQconst [5] x) -> (LEAQ4 x x)
   867  (MULQconst [7] x) -> (LEAQ8 (NEGQ <v.Type> x) x)
   868  (MULQconst [9] x) -> (LEAQ8 x x)
   869  (MULQconst [11] x) -> (LEAQ2 x (LEAQ4 <v.Type> x x))
   870  (MULQconst [13] x) -> (LEAQ4 x (LEAQ2 <v.Type> x x))
   871  (MULQconst [21] x) -> (LEAQ4 x (LEAQ4 <v.Type> x x))
   872  (MULQconst [25] x) -> (LEAQ8 x (LEAQ2 <v.Type> x x))
   873  (MULQconst [37] x) -> (LEAQ4 x (LEAQ8 <v.Type> x x))
   874  (MULQconst [41] x) -> (LEAQ8 x (LEAQ4 <v.Type> x x))
   875  (MULQconst [73] x) -> (LEAQ8 x (LEAQ8 <v.Type> x x))
   876  
   877  (MULQconst [c] x) && isPowerOfTwo(c) -> (SHLQconst [log2(c)] x)
   878  (MULQconst [c] x) && isPowerOfTwo(c+1) && c >= 15 -> (SUBQ (SHLQconst <v.Type> [log2(c+1)] x) x)
   879  (MULQconst [c] x) && isPowerOfTwo(c-1) && c >= 17 -> (LEAQ1 (SHLQconst <v.Type> [log2(c-1)] x) x)
   880  (MULQconst [c] x) && isPowerOfTwo(c-2) && c >= 34 -> (LEAQ2 (SHLQconst <v.Type> [log2(c-2)] x) x)
   881  (MULQconst [c] x) && isPowerOfTwo(c-4) && c >= 68 -> (LEAQ4 (SHLQconst <v.Type> [log2(c-4)] x) x)
   882  (MULQconst [c] x) && isPowerOfTwo(c-8) && c >= 136 -> (LEAQ8 (SHLQconst <v.Type> [log2(c-8)] x) x)
   883  (MULQconst [c] x) && c%3 == 0 && isPowerOfTwo(c/3) -> (SHLQconst [log2(c/3)] (LEAQ2 <v.Type> x x))
   884  (MULQconst [c] x) && c%5 == 0 && isPowerOfTwo(c/5) -> (SHLQconst [log2(c/5)] (LEAQ4 <v.Type> x x))
   885  (MULQconst [c] x) && c%9 == 0 && isPowerOfTwo(c/9) -> (SHLQconst [log2(c/9)] (LEAQ8 <v.Type> x x))
   886  
   887  // combine add/shift into LEAQ
   888  (ADDQ x (SHLQconst [3] y)) -> (LEAQ8 x y)
   889  (ADDQ x (SHLQconst [2] y)) -> (LEAQ4 x y)
   890  (ADDQ x (SHLQconst [1] y)) -> (LEAQ2 x y)
   891  (ADDQ x (ADDQ y y)) -> (LEAQ2 x y)
   892  (ADDQ x (ADDQ x y)) -> (LEAQ2 y x)
   893  
   894  // combine ADDQ/ADDQconst into LEAQ1
   895  (ADDQconst [c] (ADDQ x y)) -> (LEAQ1 [c] x y)
   896  (ADDQ (ADDQconst [c] x) y) -> (LEAQ1 [c] x y)
   897  
   898  // fold ADDQ into LEAQ
   899  (ADDQconst [c] (LEAQ [d] {s} x)) && is32Bit(c+d) -> (LEAQ [c+d] {s} x)
   900  (LEAQ [c] {s} (ADDQconst [d] x)) && is32Bit(c+d) -> (LEAQ [c+d] {s} x)
   901  (LEAQ [c] {s} (ADDQ x y)) && x.Op != OpSB && y.Op != OpSB -> (LEAQ1 [c] {s} x y)
   902  (ADDQ x (LEAQ [c] {s} y)) && x.Op != OpSB && y.Op != OpSB -> (LEAQ1 [c] {s} x y)
   903  
   904  // fold ADDQconst into LEAQx
   905  (ADDQconst [c] (LEAQ1 [d] {s} x y)) && is32Bit(c+d) -> (LEAQ1 [c+d] {s} x y)
   906  (ADDQconst [c] (LEAQ2 [d] {s} x y)) && is32Bit(c+d) -> (LEAQ2 [c+d] {s} x y)
   907  (ADDQconst [c] (LEAQ4 [d] {s} x y)) && is32Bit(c+d) -> (LEAQ4 [c+d] {s} x y)
   908  (ADDQconst [c] (LEAQ8 [d] {s} x y)) && is32Bit(c+d) -> (LEAQ8 [c+d] {s} x y)
   909  (LEAQ1 [c] {s} (ADDQconst [d] x) y) && is32Bit(c+d)   && x.Op != OpSB -> (LEAQ1 [c+d] {s} x y)
   910  (LEAQ2 [c] {s} (ADDQconst [d] x) y) && is32Bit(c+d)   && x.Op != OpSB -> (LEAQ2 [c+d] {s} x y)
   911  (LEAQ2 [c] {s} x (ADDQconst [d] y)) && is32Bit(c+2*d) && y.Op != OpSB -> (LEAQ2 [c+2*d] {s} x y)
   912  (LEAQ4 [c] {s} (ADDQconst [d] x) y) && is32Bit(c+d)   && x.Op != OpSB -> (LEAQ4 [c+d] {s} x y)
   913  (LEAQ4 [c] {s} x (ADDQconst [d] y)) && is32Bit(c+4*d) && y.Op != OpSB -> (LEAQ4 [c+4*d] {s} x y)
   914  (LEAQ8 [c] {s} (ADDQconst [d] x) y) && is32Bit(c+d)   && x.Op != OpSB -> (LEAQ8 [c+d] {s} x y)
   915  (LEAQ8 [c] {s} x (ADDQconst [d] y)) && is32Bit(c+8*d) && y.Op != OpSB -> (LEAQ8 [c+8*d] {s} x y)
   916  
   917  // fold shifts into LEAQx
   918  (LEAQ1 [c] {s} x (SHLQconst [1] y)) -> (LEAQ2 [c] {s} x y)
   919  (LEAQ1 [c] {s} x (SHLQconst [2] y)) -> (LEAQ4 [c] {s} x y)
   920  (LEAQ1 [c] {s} x (SHLQconst [3] y)) -> (LEAQ8 [c] {s} x y)
   921  (LEAQ2 [c] {s} x (SHLQconst [1] y)) -> (LEAQ4 [c] {s} x y)
   922  (LEAQ2 [c] {s} x (SHLQconst [2] y)) -> (LEAQ8 [c] {s} x y)
   923  (LEAQ4 [c] {s} x (SHLQconst [1] y)) -> (LEAQ8 [c] {s} x y)
   924  
   925  // reverse ordering of compare instruction
   926  (SETL (InvertFlags x)) -> (SETG x)
   927  (SETG (InvertFlags x)) -> (SETL x)
   928  (SETB (InvertFlags x)) -> (SETA x)
   929  (SETA (InvertFlags x)) -> (SETB x)
   930  (SETLE (InvertFlags x)) -> (SETGE x)
   931  (SETGE (InvertFlags x)) -> (SETLE x)
   932  (SETBE (InvertFlags x)) -> (SETAE x)
   933  (SETAE (InvertFlags x)) -> (SETBE x)
   934  (SETEQ (InvertFlags x)) -> (SETEQ x)
   935  (SETNE (InvertFlags x)) -> (SETNE x)
   936  
   937  // sign extended loads
   938  // Note: The combined instruction must end up in the same block
   939  // as the original load. If not, we end up making a value with
   940  // memory type live in two different blocks, which can lead to
   941  // multiple memory values alive simultaneously.
   942  // Make sure we don't combine these ops if the load has another use.
   943  // This prevents a single load from being split into multiple loads
   944  // which then might return different values.  See test/atomicload.go.
   945  (MOVBQSX x:(MOVBload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
   946  (MOVBQSX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
   947  (MOVBQSX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
   948  (MOVBQSX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
   949  (MOVBQZX x:(MOVBload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
   950  (MOVBQZX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
   951  (MOVBQZX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
   952  (MOVBQZX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
   953  (MOVWQSX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWQSXload <v.Type> [off] {sym} ptr mem)
   954  (MOVWQSX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWQSXload <v.Type> [off] {sym} ptr mem)
   955  (MOVWQSX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWQSXload <v.Type> [off] {sym} ptr mem)
   956  (MOVWQZX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWload <v.Type> [off] {sym} ptr mem)
   957  (MOVWQZX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWload <v.Type> [off] {sym} ptr mem)
   958  (MOVWQZX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWload <v.Type> [off] {sym} ptr mem)
   959  (MOVLQSX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLQSXload <v.Type> [off] {sym} ptr mem)
   960  (MOVLQSX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLQSXload <v.Type> [off] {sym} ptr mem)
   961  (MOVLQZX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLload <v.Type> [off] {sym} ptr mem)
   962  (MOVLQZX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLload <v.Type> [off] {sym} ptr mem)
   963  
   964  (MOVBQZX x:(MOVBloadidx1 [off] {sym} ptr idx mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBloadidx1 <v.Type> [off] {sym} ptr idx mem)
   965  (MOVWQZX x:(MOVWloadidx1 [off] {sym} ptr idx mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWloadidx1 <v.Type> [off] {sym} ptr idx mem)
   966  (MOVWQZX x:(MOVWloadidx2 [off] {sym} ptr idx mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWloadidx2 <v.Type> [off] {sym} ptr idx mem)
   967  (MOVLQZX x:(MOVLloadidx1 [off] {sym} ptr idx mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLloadidx1 <v.Type> [off] {sym} ptr idx mem)
   968  (MOVLQZX x:(MOVLloadidx4 [off] {sym} ptr idx mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLloadidx4 <v.Type> [off] {sym} ptr idx mem)
   969  
   970  // replace load from same location as preceding store with zero/sign extension (or copy in case of full width)
   971  (MOVBload [off] {sym} ptr (MOVBstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> (MOVBQZX x)
   972  (MOVWload [off] {sym} ptr (MOVWstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> (MOVWQZX x)
   973  (MOVLload [off] {sym} ptr (MOVLstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> (MOVLQZX x)
   974  (MOVQload [off] {sym} ptr (MOVQstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> x
   975  (MOVBQSXload [off] {sym} ptr (MOVBstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> (MOVBQSX x)
   976  (MOVWQSXload [off] {sym} ptr (MOVWstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> (MOVWQSX x)
   977  (MOVLQSXload [off] {sym} ptr (MOVLstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> (MOVLQSX x)
   978  
   979  // Fold extensions and ANDs together.
   980  (MOVBQZX (ANDLconst [c] x)) -> (ANDLconst [c & 0xff] x)
   981  (MOVWQZX (ANDLconst [c] x)) -> (ANDLconst [c & 0xffff] x)
   982  (MOVLQZX (ANDLconst [c] x)) -> (ANDLconst [c] x)
   983  (MOVBQSX (ANDLconst [c] x)) && c & 0x80 == 0 -> (ANDLconst [c & 0x7f] x)
   984  (MOVWQSX (ANDLconst [c] x)) && c & 0x8000 == 0 -> (ANDLconst [c & 0x7fff] x)
   985  (MOVLQSX (ANDLconst [c] x)) && c & 0x80000000 == 0 -> (ANDLconst [c & 0x7fffffff] x)
   986  
   987  // Don't extend before storing
   988  (MOVLstore [off] {sym} ptr (MOVLQSX x) mem) -> (MOVLstore [off] {sym} ptr x mem)
   989  (MOVWstore [off] {sym} ptr (MOVWQSX x) mem) -> (MOVWstore [off] {sym} ptr x mem)
   990  (MOVBstore [off] {sym} ptr (MOVBQSX x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   991  (MOVLstore [off] {sym} ptr (MOVLQZX x) mem) -> (MOVLstore [off] {sym} ptr x mem)
   992  (MOVWstore [off] {sym} ptr (MOVWQZX x) mem) -> (MOVWstore [off] {sym} ptr x mem)
   993  (MOVBstore [off] {sym} ptr (MOVBQZX x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   994  
   995  // fold constants into memory operations
   996  // Note that this is not always a good idea because if not all the uses of
   997  // the ADDQconst get eliminated, we still have to compute the ADDQconst and we now
   998  // have potentially two live values (ptr and (ADDQconst [off] ptr)) instead of one.
   999  // Nevertheless, let's do it!
  1000  (MOVQload  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVQload  [off1+off2] {sym} ptr mem)
  1001  (MOVLload  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVLload  [off1+off2] {sym} ptr mem)
  1002  (MOVWload  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVWload  [off1+off2] {sym} ptr mem)
  1003  (MOVBload  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVBload  [off1+off2] {sym} ptr mem)
  1004  (MOVSSload [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVSSload [off1+off2] {sym} ptr mem)
  1005  (MOVSDload [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVSDload [off1+off2] {sym} ptr mem)
  1006  (MOVOload  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVOload  [off1+off2] {sym} ptr mem)
  1007  
  1008  (MOVQstore  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVQstore  [off1+off2] {sym} ptr val mem)
  1009  (MOVLstore  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVLstore  [off1+off2] {sym} ptr val mem)
  1010  (MOVWstore  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVWstore  [off1+off2] {sym} ptr val mem)
  1011  (MOVBstore  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVBstore  [off1+off2] {sym} ptr val mem)
  1012  (MOVSSstore [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVSSstore [off1+off2] {sym} ptr val mem)
  1013  (MOVSDstore [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVSDstore [off1+off2] {sym} ptr val mem)
  1014  (MOVOstore  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVOstore  [off1+off2] {sym} ptr val mem)
  1015  
  1016  // Fold constants into stores.
  1017  (MOVQstore [off] {sym} ptr (MOVQconst [c]) mem) && validValAndOff(c,off) ->
  1018  	(MOVQstoreconst [makeValAndOff(c,off)] {sym} ptr mem)
  1019  (MOVLstore [off] {sym} ptr (MOVLconst [c]) mem) && validOff(off) ->
  1020  	(MOVLstoreconst [makeValAndOff(int64(int32(c)),off)] {sym} ptr mem)
  1021  (MOVWstore [off] {sym} ptr (MOVLconst [c]) mem) && validOff(off) ->
  1022  	(MOVWstoreconst [makeValAndOff(int64(int16(c)),off)] {sym} ptr mem)
  1023  (MOVBstore [off] {sym} ptr (MOVLconst [c]) mem) && validOff(off) ->
  1024  	(MOVBstoreconst [makeValAndOff(int64(int8(c)),off)] {sym} ptr mem)
  1025  
  1026  // Fold address offsets into constant stores.
  1027  (MOVQstoreconst [sc] {s} (ADDQconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  1028  	(MOVQstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  1029  (MOVLstoreconst [sc] {s} (ADDQconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  1030  	(MOVLstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  1031  (MOVWstoreconst [sc] {s} (ADDQconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  1032  	(MOVWstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  1033  (MOVBstoreconst [sc] {s} (ADDQconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  1034  	(MOVBstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  1035  
  1036  // We need to fold LEAQ into the MOVx ops so that the live variable analysis knows
  1037  // what variables are being read/written by the ops.
  1038  (MOVQload  [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1039  	(MOVQload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1040  (MOVLload  [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1041  	(MOVLload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1042  (MOVWload  [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1043  	(MOVWload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1044  (MOVBload  [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1045  	(MOVBload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1046  (MOVSSload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1047  	(MOVSSload [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1048  (MOVSDload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1049  	(MOVSDload [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1050  (MOVOload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1051  	(MOVOload [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1052  
  1053  (MOVBQSXload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1054  	(MOVBQSXload [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1055  (MOVWQSXload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1056  	(MOVWQSXload [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1057  (MOVLQSXload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1058  	(MOVLQSXload [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1059  
  1060  (MOVQstore  [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1061  	(MOVQstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1062  (MOVLstore  [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1063  	(MOVLstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1064  (MOVWstore  [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1065  	(MOVWstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1066  (MOVBstore  [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1067  	(MOVBstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1068  (MOVSSstore [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1069  	(MOVSSstore [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1070  (MOVSDstore [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1071  	(MOVSDstore [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1072  (MOVOstore [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1073  	(MOVOstore [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1074  
  1075  (MOVQstoreconst [sc] {sym1} (LEAQ [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  1076  	(MOVQstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  1077  (MOVLstoreconst [sc] {sym1} (LEAQ [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  1078  	(MOVLstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  1079  (MOVWstoreconst [sc] {sym1} (LEAQ [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  1080  	(MOVWstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  1081  (MOVBstoreconst [sc] {sym1} (LEAQ [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  1082  	(MOVBstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  1083  
  1084  // generating indexed loads and stores
  1085  (MOVBload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1086  	(MOVBloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1087  (MOVWload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1088  	(MOVWloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1089  (MOVWload [off1] {sym1} (LEAQ2 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1090  	(MOVWloadidx2 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1091  (MOVLload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1092  	(MOVLloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1093  (MOVLload [off1] {sym1} (LEAQ4 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1094  	(MOVLloadidx4 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1095  (MOVQload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1096  	(MOVQloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1097  (MOVQload [off1] {sym1} (LEAQ8 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1098  	(MOVQloadidx8 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1099  (MOVSSload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1100  	(MOVSSloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1101  (MOVSSload [off1] {sym1} (LEAQ4 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1102  	(MOVSSloadidx4 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1103  (MOVSDload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1104  	(MOVSDloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1105  (MOVSDload [off1] {sym1} (LEAQ8 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1106  	(MOVSDloadidx8 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
  1107  
  1108  (MOVBstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1109  	(MOVBstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1110  (MOVWstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1111  	(MOVWstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1112  (MOVWstore [off1] {sym1} (LEAQ2 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1113  	(MOVWstoreidx2 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1114  (MOVLstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1115  	(MOVLstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1116  (MOVLstore [off1] {sym1} (LEAQ4 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1117  	(MOVLstoreidx4 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1118  (MOVQstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1119  	(MOVQstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1120  (MOVQstore [off1] {sym1} (LEAQ8 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1121  	(MOVQstoreidx8 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1122  (MOVSSstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1123  	(MOVSSstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1124  (MOVSSstore [off1] {sym1} (LEAQ4 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1125  	(MOVSSstoreidx4 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1126  (MOVSDstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1127  	(MOVSDstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1128  (MOVSDstore [off1] {sym1} (LEAQ8 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1129  	(MOVSDstoreidx8 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
  1130  
  1131  (MOVBload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVBloadidx1 [off] {sym} ptr idx mem)
  1132  (MOVWload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVWloadidx1 [off] {sym} ptr idx mem)
  1133  (MOVLload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVLloadidx1 [off] {sym} ptr idx mem)
  1134  (MOVQload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVQloadidx1 [off] {sym} ptr idx mem)
  1135  (MOVSSload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVSSloadidx1 [off] {sym} ptr idx mem)
  1136  (MOVSDload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVSDloadidx1 [off] {sym} ptr idx mem)
  1137  (MOVBstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVBstoreidx1 [off] {sym} ptr idx val mem)
  1138  (MOVWstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVWstoreidx1 [off] {sym} ptr idx val mem)
  1139  (MOVLstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVLstoreidx1 [off] {sym} ptr idx val mem)
  1140  (MOVQstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVQstoreidx1 [off] {sym} ptr idx val mem)
  1141  (MOVSSstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVSSstoreidx1 [off] {sym} ptr idx val mem)
  1142  (MOVSDstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVSDstoreidx1 [off] {sym} ptr idx val mem)
  1143  
  1144  (MOVBstoreconst [x] {sym1} (LEAQ1 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
  1145  	(MOVBstoreconstidx1 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
  1146  (MOVWstoreconst [x] {sym1} (LEAQ1 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
  1147  	(MOVWstoreconstidx1 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
  1148  (MOVWstoreconst [x] {sym1} (LEAQ2 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
  1149  	(MOVWstoreconstidx2 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
  1150  (MOVLstoreconst [x] {sym1} (LEAQ1 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
  1151  	(MOVLstoreconstidx1 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
  1152  (MOVLstoreconst [x] {sym1} (LEAQ4 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
  1153  	(MOVLstoreconstidx4 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
  1154  (MOVQstoreconst [x] {sym1} (LEAQ1 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
  1155  	(MOVQstoreconstidx1 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
  1156  (MOVQstoreconst [x] {sym1} (LEAQ8 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
  1157  	(MOVQstoreconstidx8 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
  1158  
  1159  (MOVBstoreconst [x] {sym} (ADDQ ptr idx) mem) -> (MOVBstoreconstidx1 [x] {sym} ptr idx mem)
  1160  (MOVWstoreconst [x] {sym} (ADDQ ptr idx) mem) -> (MOVWstoreconstidx1 [x] {sym} ptr idx mem)
  1161  (MOVLstoreconst [x] {sym} (ADDQ ptr idx) mem) -> (MOVLstoreconstidx1 [x] {sym} ptr idx mem)
  1162  (MOVQstoreconst [x] {sym} (ADDQ ptr idx) mem) -> (MOVQstoreconstidx1 [x] {sym} ptr idx mem)
  1163  
  1164  // combine SHLQ into indexed loads and stores
  1165  (MOVWloadidx1 [c] {sym} ptr (SHLQconst [1] idx) mem) -> (MOVWloadidx2 [c] {sym} ptr idx mem)
  1166  (MOVLloadidx1 [c] {sym} ptr (SHLQconst [2] idx) mem) -> (MOVLloadidx4 [c] {sym} ptr idx mem)
  1167  (MOVQloadidx1 [c] {sym} ptr (SHLQconst [3] idx) mem) -> (MOVQloadidx8 [c] {sym} ptr idx mem)
  1168  (MOVSSloadidx1 [c] {sym} ptr (SHLQconst [2] idx) mem) -> (MOVSSloadidx4 [c] {sym} ptr idx mem)
  1169  (MOVSDloadidx1 [c] {sym} ptr (SHLQconst [3] idx) mem) -> (MOVSDloadidx8 [c] {sym} ptr idx mem)
  1170  (MOVWstoreidx1 [c] {sym} ptr (SHLQconst [1] idx) val mem) -> (MOVWstoreidx2 [c] {sym} ptr idx val mem)
  1171  (MOVLstoreidx1 [c] {sym} ptr (SHLQconst [2] idx) val mem) -> (MOVLstoreidx4 [c] {sym} ptr idx val mem)
  1172  (MOVQstoreidx1 [c] {sym} ptr (SHLQconst [3] idx) val mem) -> (MOVQstoreidx8 [c] {sym} ptr idx val mem)
  1173  (MOVSSstoreidx1 [c] {sym} ptr (SHLQconst [2] idx) val mem) -> (MOVSSstoreidx4 [c] {sym} ptr idx val mem)
  1174  (MOVSDstoreidx1 [c] {sym} ptr (SHLQconst [3] idx) val mem) -> (MOVSDstoreidx8 [c] {sym} ptr idx val mem)
  1175  (MOVWstoreconstidx1 [c] {sym} ptr (SHLQconst [1] idx) mem) -> (MOVWstoreconstidx2 [c] {sym} ptr idx mem)
  1176  (MOVLstoreconstidx1 [c] {sym} ptr (SHLQconst [2] idx) mem) -> (MOVLstoreconstidx4 [c] {sym} ptr idx mem)
  1177  (MOVQstoreconstidx1 [c] {sym} ptr (SHLQconst [3] idx) mem) -> (MOVQstoreconstidx8 [c] {sym} ptr idx mem)
  1178  
  1179  // combine ADDQ into indexed loads and stores
  1180  (MOVBloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVBloadidx1 [c+d] {sym} ptr idx mem)
  1181  (MOVWloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVWloadidx1 [c+d] {sym} ptr idx mem)
  1182  (MOVWloadidx2 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVWloadidx2 [c+d] {sym} ptr idx mem)
  1183  (MOVLloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVLloadidx1 [c+d] {sym} ptr idx mem)
  1184  (MOVLloadidx4 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVLloadidx4 [c+d] {sym} ptr idx mem)
  1185  (MOVQloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVQloadidx1 [c+d] {sym} ptr idx mem)
  1186  (MOVQloadidx8 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVQloadidx8 [c+d] {sym} ptr idx mem)
  1187  (MOVSSloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVSSloadidx1 [c+d] {sym} ptr idx mem)
  1188  (MOVSSloadidx4 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVSSloadidx4 [c+d] {sym} ptr idx mem)
  1189  (MOVSDloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVSDloadidx1 [c+d] {sym} ptr idx mem)
  1190  (MOVSDloadidx8 [c] {sym} (ADDQconst [d] ptr) idx mem) && is32Bit(c+d) -> (MOVSDloadidx8 [c+d] {sym} ptr idx mem)
  1191  
  1192  (MOVBstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVBstoreidx1 [c+d] {sym} ptr idx val mem)
  1193  (MOVWstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVWstoreidx1 [c+d] {sym} ptr idx val mem)
  1194  (MOVWstoreidx2 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVWstoreidx2 [c+d] {sym} ptr idx val mem)
  1195  (MOVLstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVLstoreidx1 [c+d] {sym} ptr idx val mem)
  1196  (MOVLstoreidx4 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVLstoreidx4 [c+d] {sym} ptr idx val mem)
  1197  (MOVQstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVQstoreidx1 [c+d] {sym} ptr idx val mem)
  1198  (MOVQstoreidx8 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVQstoreidx8 [c+d] {sym} ptr idx val mem)
  1199  (MOVSSstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVSSstoreidx1 [c+d] {sym} ptr idx val mem)
  1200  (MOVSSstoreidx4 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVSSstoreidx4 [c+d] {sym} ptr idx val mem)
  1201  (MOVSDstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVSDstoreidx1 [c+d] {sym} ptr idx val mem)
  1202  (MOVSDstoreidx8 [c] {sym} (ADDQconst [d] ptr) idx val mem) && is32Bit(c+d) -> (MOVSDstoreidx8 [c+d] {sym} ptr idx val mem)
  1203  
  1204  (MOVBloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem)  && is32Bit(c+d)   -> (MOVBloadidx1 [c+d] {sym} ptr idx mem)
  1205  (MOVWloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem)  && is32Bit(c+d)   -> (MOVWloadidx1 [c+d] {sym} ptr idx mem)
  1206  (MOVWloadidx2 [c] {sym} ptr (ADDQconst [d] idx) mem)  && is32Bit(c+2*d) -> (MOVWloadidx2 [c+2*d] {sym} ptr idx mem)
  1207  (MOVLloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem)  && is32Bit(c+d)   -> (MOVLloadidx1 [c+d] {sym} ptr idx mem)
  1208  (MOVLloadidx4 [c] {sym} ptr (ADDQconst [d] idx) mem)  && is32Bit(c+4*d) -> (MOVLloadidx4 [c+4*d] {sym} ptr idx mem)
  1209  (MOVQloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem)  && is32Bit(c+d)   -> (MOVQloadidx1 [c+d] {sym} ptr idx mem)
  1210  (MOVQloadidx8 [c] {sym} ptr (ADDQconst [d] idx) mem)  && is32Bit(c+8*d) -> (MOVQloadidx8 [c+8*d] {sym} ptr idx mem)
  1211  (MOVSSloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem) && is32Bit(c+d)   -> (MOVSSloadidx1 [c+d] {sym} ptr idx mem)
  1212  (MOVSSloadidx4 [c] {sym} ptr (ADDQconst [d] idx) mem) && is32Bit(c+4*d) -> (MOVSSloadidx4 [c+4*d] {sym} ptr idx mem)
  1213  (MOVSDloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem) && is32Bit(c+d)   -> (MOVSDloadidx1 [c+d] {sym} ptr idx mem)
  1214  (MOVSDloadidx8 [c] {sym} ptr (ADDQconst [d] idx) mem) && is32Bit(c+8*d) -> (MOVSDloadidx8 [c+8*d] {sym} ptr idx mem)
  1215  
  1216  (MOVBstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem)  && is32Bit(c+d)   -> (MOVBstoreidx1 [c+d] {sym} ptr idx val mem)
  1217  (MOVWstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem)  && is32Bit(c+d)   -> (MOVWstoreidx1 [c+d] {sym} ptr idx val mem)
  1218  (MOVWstoreidx2 [c] {sym} ptr (ADDQconst [d] idx) val mem)  && is32Bit(c+2*d) -> (MOVWstoreidx2 [c+2*d] {sym} ptr idx val mem)
  1219  (MOVLstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem)  && is32Bit(c+d)   -> (MOVLstoreidx1 [c+d] {sym} ptr idx val mem)
  1220  (MOVLstoreidx4 [c] {sym} ptr (ADDQconst [d] idx) val mem)  && is32Bit(c+4*d) -> (MOVLstoreidx4 [c+4*d] {sym} ptr idx val mem)
  1221  (MOVQstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem)  && is32Bit(c+d)   -> (MOVQstoreidx1 [c+d] {sym} ptr idx val mem)
  1222  (MOVQstoreidx8 [c] {sym} ptr (ADDQconst [d] idx) val mem)  && is32Bit(c+8*d) -> (MOVQstoreidx8 [c+8*d] {sym} ptr idx val mem)
  1223  (MOVSSstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem) && is32Bit(c+d)   -> (MOVSSstoreidx1 [c+d] {sym} ptr idx val mem)
  1224  (MOVSSstoreidx4 [c] {sym} ptr (ADDQconst [d] idx) val mem) && is32Bit(c+4*d) -> (MOVSSstoreidx4 [c+4*d] {sym} ptr idx val mem)
  1225  (MOVSDstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem) && is32Bit(c+d)   -> (MOVSDstoreidx1 [c+d] {sym} ptr idx val mem)
  1226  (MOVSDstoreidx8 [c] {sym} ptr (ADDQconst [d] idx) val mem) && is32Bit(c+8*d) -> (MOVSDstoreidx8 [c+8*d] {sym} ptr idx val mem)
  1227  
  1228  (MOVBstoreconstidx1 [x] {sym} (ADDQconst [c] ptr) idx mem) && ValAndOff(x).canAdd(c) ->
  1229  	(MOVBstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1230  (MOVWstoreconstidx1 [x] {sym} (ADDQconst [c] ptr) idx mem) && ValAndOff(x).canAdd(c) ->
  1231  	(MOVWstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1232  (MOVWstoreconstidx2 [x] {sym} (ADDQconst [c] ptr) idx mem) && ValAndOff(x).canAdd(c) ->
  1233  	(MOVWstoreconstidx2 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1234  (MOVLstoreconstidx1 [x] {sym} (ADDQconst [c] ptr) idx mem) && ValAndOff(x).canAdd(c) ->
  1235  	(MOVLstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1236  (MOVLstoreconstidx4 [x] {sym} (ADDQconst [c] ptr) idx mem) && ValAndOff(x).canAdd(c) ->
  1237  	(MOVLstoreconstidx4 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1238  (MOVQstoreconstidx1 [x] {sym} (ADDQconst [c] ptr) idx mem) && ValAndOff(x).canAdd(c) ->
  1239  	(MOVQstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1240  (MOVQstoreconstidx8 [x] {sym} (ADDQconst [c] ptr) idx mem) && ValAndOff(x).canAdd(c) ->
  1241  	(MOVQstoreconstidx8 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1242  
  1243  (MOVBstoreconstidx1 [x] {sym} ptr (ADDQconst [c] idx) mem) && ValAndOff(x).canAdd(c) ->
  1244  	(MOVBstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1245  (MOVWstoreconstidx1 [x] {sym} ptr (ADDQconst [c] idx) mem) && ValAndOff(x).canAdd(c) ->
  1246  	(MOVWstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1247  (MOVWstoreconstidx2 [x] {sym} ptr (ADDQconst [c] idx) mem) && ValAndOff(x).canAdd(2*c) ->
  1248  	(MOVWstoreconstidx2 [ValAndOff(x).add(2*c)] {sym} ptr idx mem)
  1249  (MOVLstoreconstidx1 [x] {sym} ptr (ADDQconst [c] idx) mem) && ValAndOff(x).canAdd(c) ->
  1250  	(MOVLstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1251  (MOVLstoreconstidx4 [x] {sym} ptr (ADDQconst [c] idx) mem) && ValAndOff(x).canAdd(4*c) ->
  1252  	(MOVLstoreconstidx4 [ValAndOff(x).add(4*c)] {sym} ptr idx mem)
  1253  (MOVQstoreconstidx1 [x] {sym} ptr (ADDQconst [c] idx) mem) && ValAndOff(x).canAdd(c) ->
  1254  	(MOVQstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1255  (MOVQstoreconstidx8 [x] {sym} ptr (ADDQconst [c] idx) mem) && ValAndOff(x).canAdd(8*c) ->
  1256  	(MOVQstoreconstidx8 [ValAndOff(x).add(8*c)] {sym} ptr idx mem)
  1257  
  1258  // fold LEAQs together
  1259  (LEAQ [off1] {sym1} (LEAQ [off2] {sym2} x)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1260        (LEAQ [off1+off2] {mergeSym(sym1,sym2)} x)
  1261  
  1262  // LEAQ into LEAQ1
  1263  (LEAQ1 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && x.Op != OpSB ->
  1264         (LEAQ1 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1265  
  1266  // LEAQ1 into LEAQ
  1267  (LEAQ [off1] {sym1} (LEAQ1 [off2] {sym2} x y)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1268         (LEAQ1 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1269  
  1270  // LEAQ into LEAQ[248]
  1271  (LEAQ2 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && x.Op != OpSB ->
  1272         (LEAQ2 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1273  (LEAQ4 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && x.Op != OpSB ->
  1274         (LEAQ4 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1275  (LEAQ8 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && x.Op != OpSB ->
  1276         (LEAQ8 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1277  
  1278  // LEAQ[248] into LEAQ
  1279  (LEAQ [off1] {sym1} (LEAQ2 [off2] {sym2} x y)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1280        (LEAQ2 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1281  (LEAQ [off1] {sym1} (LEAQ4 [off2] {sym2} x y)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1282        (LEAQ4 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1283  (LEAQ [off1] {sym1} (LEAQ8 [off2] {sym2} x y)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1284        (LEAQ8 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1285  
  1286  // Absorb InvertFlags into branches.
  1287  (LT (InvertFlags cmp) yes no) -> (GT cmp yes no)
  1288  (GT (InvertFlags cmp) yes no) -> (LT cmp yes no)
  1289  (LE (InvertFlags cmp) yes no) -> (GE cmp yes no)
  1290  (GE (InvertFlags cmp) yes no) -> (LE cmp yes no)
  1291  (ULT (InvertFlags cmp) yes no) -> (UGT cmp yes no)
  1292  (UGT (InvertFlags cmp) yes no) -> (ULT cmp yes no)
  1293  (ULE (InvertFlags cmp) yes no) -> (UGE cmp yes no)
  1294  (UGE (InvertFlags cmp) yes no) -> (ULE cmp yes no)
  1295  (EQ (InvertFlags cmp) yes no) -> (EQ cmp yes no)
  1296  (NE (InvertFlags cmp) yes no) -> (NE cmp yes no)
  1297  
  1298  // Constant comparisons.
  1299  (CMPQconst (MOVQconst [x]) [y]) && x==y -> (FlagEQ)
  1300  (CMPQconst (MOVQconst [x]) [y]) && x<y && uint64(x)<uint64(y) -> (FlagLT_ULT)
  1301  (CMPQconst (MOVQconst [x]) [y]) && x<y && uint64(x)>uint64(y) -> (FlagLT_UGT)
  1302  (CMPQconst (MOVQconst [x]) [y]) && x>y && uint64(x)<uint64(y) -> (FlagGT_ULT)
  1303  (CMPQconst (MOVQconst [x]) [y]) && x>y && uint64(x)>uint64(y) -> (FlagGT_UGT)
  1304  (CMPLconst (MOVLconst [x]) [y]) && int32(x)==int32(y) -> (FlagEQ)
  1305  (CMPLconst (MOVLconst [x]) [y]) && int32(x)<int32(y) && uint32(x)<uint32(y) -> (FlagLT_ULT)
  1306  (CMPLconst (MOVLconst [x]) [y]) && int32(x)<int32(y) && uint32(x)>uint32(y) -> (FlagLT_UGT)
  1307  (CMPLconst (MOVLconst [x]) [y]) && int32(x)>int32(y) && uint32(x)<uint32(y) -> (FlagGT_ULT)
  1308  (CMPLconst (MOVLconst [x]) [y]) && int32(x)>int32(y) && uint32(x)>uint32(y) -> (FlagGT_UGT)
  1309  (CMPWconst (MOVLconst [x]) [y]) && int16(x)==int16(y) -> (FlagEQ)
  1310  (CMPWconst (MOVLconst [x]) [y]) && int16(x)<int16(y) && uint16(x)<uint16(y) -> (FlagLT_ULT)
  1311  (CMPWconst (MOVLconst [x]) [y]) && int16(x)<int16(y) && uint16(x)>uint16(y) -> (FlagLT_UGT)
  1312  (CMPWconst (MOVLconst [x]) [y]) && int16(x)>int16(y) && uint16(x)<uint16(y) -> (FlagGT_ULT)
  1313  (CMPWconst (MOVLconst [x]) [y]) && int16(x)>int16(y) && uint16(x)>uint16(y) -> (FlagGT_UGT)
  1314  (CMPBconst (MOVLconst [x]) [y]) && int8(x)==int8(y) -> (FlagEQ)
  1315  (CMPBconst (MOVLconst [x]) [y]) && int8(x)<int8(y) && uint8(x)<uint8(y) -> (FlagLT_ULT)
  1316  (CMPBconst (MOVLconst [x]) [y]) && int8(x)<int8(y) && uint8(x)>uint8(y) -> (FlagLT_UGT)
  1317  (CMPBconst (MOVLconst [x]) [y]) && int8(x)>int8(y) && uint8(x)<uint8(y) -> (FlagGT_ULT)
  1318  (CMPBconst (MOVLconst [x]) [y]) && int8(x)>int8(y) && uint8(x)>uint8(y) -> (FlagGT_UGT)
  1319  
  1320  // Other known comparisons.
  1321  (CMPQconst (MOVBQZX _) [c]) && 0xFF < c -> (FlagLT_ULT)
  1322  (CMPQconst (MOVWQZX _) [c]) && 0xFFFF < c -> (FlagLT_ULT)
  1323  (CMPQconst (MOVLQZX _) [c]) && 0xFFFFFFFF < c -> (FlagLT_ULT)
  1324  (CMPLconst (SHRLconst _ [c]) [n]) && 0 <= n && 0 < c && c <= 32 && (1<<uint64(32-c)) <= uint64(n) -> (FlagLT_ULT)
  1325  (CMPQconst (SHRQconst _ [c]) [n]) && 0 <= n && 0 < c && c <= 64 && (1<<uint64(64-c)) <= uint64(n) -> (FlagLT_ULT)
  1326  (CMPQconst (ANDQconst _ [m]) [n]) && 0 <= m && m < n -> (FlagLT_ULT)
  1327  (CMPQconst (ANDLconst _ [m]) [n]) && 0 <= m && m < n -> (FlagLT_ULT)
  1328  (CMPLconst (ANDLconst _ [m]) [n]) && 0 <= int32(m) && int32(m) < int32(n) -> (FlagLT_ULT)
  1329  (CMPWconst (ANDLconst _ [m]) [n]) && 0 <= int16(m) && int16(m) < int16(n) -> (FlagLT_ULT)
  1330  (CMPBconst (ANDLconst _ [m]) [n]) && 0 <= int8(m) && int8(m) < int8(n) -> (FlagLT_ULT)
  1331  
  1332  // TODO: DIVxU also.
  1333  
  1334  // Absorb flag constants into SBB ops.
  1335  (SBBQcarrymask (FlagEQ)) -> (MOVQconst [0])
  1336  (SBBQcarrymask (FlagLT_ULT)) -> (MOVQconst [-1])
  1337  (SBBQcarrymask (FlagLT_UGT)) -> (MOVQconst [0])
  1338  (SBBQcarrymask (FlagGT_ULT)) -> (MOVQconst [-1])
  1339  (SBBQcarrymask (FlagGT_UGT)) -> (MOVQconst [0])
  1340  (SBBLcarrymask (FlagEQ)) -> (MOVLconst [0])
  1341  (SBBLcarrymask (FlagLT_ULT)) -> (MOVLconst [-1])
  1342  (SBBLcarrymask (FlagLT_UGT)) -> (MOVLconst [0])
  1343  (SBBLcarrymask (FlagGT_ULT)) -> (MOVLconst [-1])
  1344  (SBBLcarrymask (FlagGT_UGT)) -> (MOVLconst [0])
  1345  
  1346  // Absorb flag constants into branches.
  1347  (EQ (FlagEQ) yes no) -> (First nil yes no)
  1348  (EQ (FlagLT_ULT) yes no) -> (First nil no yes)
  1349  (EQ (FlagLT_UGT) yes no) -> (First nil no yes)
  1350  (EQ (FlagGT_ULT) yes no) -> (First nil no yes)
  1351  (EQ (FlagGT_UGT) yes no) -> (First nil no yes)
  1352  
  1353  (NE (FlagEQ) yes no) -> (First nil no yes)
  1354  (NE (FlagLT_ULT) yes no) -> (First nil yes no)
  1355  (NE (FlagLT_UGT) yes no) -> (First nil yes no)
  1356  (NE (FlagGT_ULT) yes no) -> (First nil yes no)
  1357  (NE (FlagGT_UGT) yes no) -> (First nil yes no)
  1358  
  1359  (LT (FlagEQ) yes no) -> (First nil no yes)
  1360  (LT (FlagLT_ULT) yes no) -> (First nil yes no)
  1361  (LT (FlagLT_UGT) yes no) -> (First nil yes no)
  1362  (LT (FlagGT_ULT) yes no) -> (First nil no yes)
  1363  (LT (FlagGT_UGT) yes no) -> (First nil no yes)
  1364  
  1365  (LE (FlagEQ) yes no) -> (First nil yes no)
  1366  (LE (FlagLT_ULT) yes no) -> (First nil yes no)
  1367  (LE (FlagLT_UGT) yes no) -> (First nil yes no)
  1368  (LE (FlagGT_ULT) yes no) -> (First nil no yes)
  1369  (LE (FlagGT_UGT) yes no) -> (First nil no yes)
  1370  
  1371  (GT (FlagEQ) yes no) -> (First nil no yes)
  1372  (GT (FlagLT_ULT) yes no) -> (First nil no yes)
  1373  (GT (FlagLT_UGT) yes no) -> (First nil no yes)
  1374  (GT (FlagGT_ULT) yes no) -> (First nil yes no)
  1375  (GT (FlagGT_UGT) yes no) -> (First nil yes no)
  1376  
  1377  (GE (FlagEQ) yes no) -> (First nil yes no)
  1378  (GE (FlagLT_ULT) yes no) -> (First nil no yes)
  1379  (GE (FlagLT_UGT) yes no) -> (First nil no yes)
  1380  (GE (FlagGT_ULT) yes no) -> (First nil yes no)
  1381  (GE (FlagGT_UGT) yes no) -> (First nil yes no)
  1382  
  1383  (ULT (FlagEQ) yes no) -> (First nil no yes)
  1384  (ULT (FlagLT_ULT) yes no) -> (First nil yes no)
  1385  (ULT (FlagLT_UGT) yes no) -> (First nil no yes)
  1386  (ULT (FlagGT_ULT) yes no) -> (First nil yes no)
  1387  (ULT (FlagGT_UGT) yes no) -> (First nil no yes)
  1388  
  1389  (ULE (FlagEQ) yes no) -> (First nil yes no)
  1390  (ULE (FlagLT_ULT) yes no) -> (First nil yes no)
  1391  (ULE (FlagLT_UGT) yes no) -> (First nil no yes)
  1392  (ULE (FlagGT_ULT) yes no) -> (First nil yes no)
  1393  (ULE (FlagGT_UGT) yes no) -> (First nil no yes)
  1394  
  1395  (UGT (FlagEQ) yes no) -> (First nil no yes)
  1396  (UGT (FlagLT_ULT) yes no) -> (First nil no yes)
  1397  (UGT (FlagLT_UGT) yes no) -> (First nil yes no)
  1398  (UGT (FlagGT_ULT) yes no) -> (First nil no yes)
  1399  (UGT (FlagGT_UGT) yes no) -> (First nil yes no)
  1400  
  1401  (UGE (FlagEQ) yes no) -> (First nil yes no)
  1402  (UGE (FlagLT_ULT) yes no) -> (First nil no yes)
  1403  (UGE (FlagLT_UGT) yes no) -> (First nil yes no)
  1404  (UGE (FlagGT_ULT) yes no) -> (First nil no yes)
  1405  (UGE (FlagGT_UGT) yes no) -> (First nil yes no)
  1406  
  1407  // Absorb flag constants into SETxx ops.
  1408  (SETEQ (FlagEQ)) -> (MOVLconst [1])
  1409  (SETEQ (FlagLT_ULT)) -> (MOVLconst [0])
  1410  (SETEQ (FlagLT_UGT)) -> (MOVLconst [0])
  1411  (SETEQ (FlagGT_ULT)) -> (MOVLconst [0])
  1412  (SETEQ (FlagGT_UGT)) -> (MOVLconst [0])
  1413  
  1414  (SETNE (FlagEQ)) -> (MOVLconst [0])
  1415  (SETNE (FlagLT_ULT)) -> (MOVLconst [1])
  1416  (SETNE (FlagLT_UGT)) -> (MOVLconst [1])
  1417  (SETNE (FlagGT_ULT)) -> (MOVLconst [1])
  1418  (SETNE (FlagGT_UGT)) -> (MOVLconst [1])
  1419  
  1420  (SETL (FlagEQ)) -> (MOVLconst [0])
  1421  (SETL (FlagLT_ULT)) -> (MOVLconst [1])
  1422  (SETL (FlagLT_UGT)) -> (MOVLconst [1])
  1423  (SETL (FlagGT_ULT)) -> (MOVLconst [0])
  1424  (SETL (FlagGT_UGT)) -> (MOVLconst [0])
  1425  
  1426  (SETLE (FlagEQ)) -> (MOVLconst [1])
  1427  (SETLE (FlagLT_ULT)) -> (MOVLconst [1])
  1428  (SETLE (FlagLT_UGT)) -> (MOVLconst [1])
  1429  (SETLE (FlagGT_ULT)) -> (MOVLconst [0])
  1430  (SETLE (FlagGT_UGT)) -> (MOVLconst [0])
  1431  
  1432  (SETG (FlagEQ)) -> (MOVLconst [0])
  1433  (SETG (FlagLT_ULT)) -> (MOVLconst [0])
  1434  (SETG (FlagLT_UGT)) -> (MOVLconst [0])
  1435  (SETG (FlagGT_ULT)) -> (MOVLconst [1])
  1436  (SETG (FlagGT_UGT)) -> (MOVLconst [1])
  1437  
  1438  (SETGE (FlagEQ)) -> (MOVLconst [1])
  1439  (SETGE (FlagLT_ULT)) -> (MOVLconst [0])
  1440  (SETGE (FlagLT_UGT)) -> (MOVLconst [0])
  1441  (SETGE (FlagGT_ULT)) -> (MOVLconst [1])
  1442  (SETGE (FlagGT_UGT)) -> (MOVLconst [1])
  1443  
  1444  (SETB (FlagEQ)) -> (MOVLconst [0])
  1445  (SETB (FlagLT_ULT)) -> (MOVLconst [1])
  1446  (SETB (FlagLT_UGT)) -> (MOVLconst [0])
  1447  (SETB (FlagGT_ULT)) -> (MOVLconst [1])
  1448  (SETB (FlagGT_UGT)) -> (MOVLconst [0])
  1449  
  1450  (SETBE (FlagEQ)) -> (MOVLconst [1])
  1451  (SETBE (FlagLT_ULT)) -> (MOVLconst [1])
  1452  (SETBE (FlagLT_UGT)) -> (MOVLconst [0])
  1453  (SETBE (FlagGT_ULT)) -> (MOVLconst [1])
  1454  (SETBE (FlagGT_UGT)) -> (MOVLconst [0])
  1455  
  1456  (SETA (FlagEQ)) -> (MOVLconst [0])
  1457  (SETA (FlagLT_ULT)) -> (MOVLconst [0])
  1458  (SETA (FlagLT_UGT)) -> (MOVLconst [1])
  1459  (SETA (FlagGT_ULT)) -> (MOVLconst [0])
  1460  (SETA (FlagGT_UGT)) -> (MOVLconst [1])
  1461  
  1462  (SETAE (FlagEQ)) -> (MOVLconst [1])
  1463  (SETAE (FlagLT_ULT)) -> (MOVLconst [0])
  1464  (SETAE (FlagLT_UGT)) -> (MOVLconst [1])
  1465  (SETAE (FlagGT_ULT)) -> (MOVLconst [0])
  1466  (SETAE (FlagGT_UGT)) -> (MOVLconst [1])
  1467  
  1468  // Remove redundant *const ops
  1469  (ADDQconst [0] x) -> x
  1470  (ADDLconst [c] x) && int32(c)==0 -> x
  1471  (SUBQconst [0] x) -> x
  1472  (SUBLconst [c] x) && int32(c) == 0 -> x
  1473  (ANDQconst [0] _)                 -> (MOVQconst [0])
  1474  (ANDLconst [c] _) && int32(c)==0  -> (MOVLconst [0])
  1475  (ANDQconst [-1] x)                -> x
  1476  (ANDLconst [c] x) && int32(c)==-1 -> x
  1477  (ORQconst [0] x)                  -> x
  1478  (ORLconst [c] x) && int32(c)==0   -> x
  1479  (ORQconst [-1] _)                 -> (MOVQconst [-1])
  1480  (ORLconst [c] _) && int32(c)==-1  -> (MOVLconst [-1])
  1481  (XORQconst [0] x)                  -> x
  1482  (XORLconst [c] x) && int32(c)==0   -> x
  1483  // TODO: since we got rid of the W/B versions, we might miss
  1484  // things like (ANDLconst [0x100] x) which were formerly
  1485  // (ANDBconst [0] x).  Probably doesn't happen very often.
  1486  // If we cared, we might do:
  1487  //  (ANDLconst <t> [c] x) && t.Size()==1 && int8(x)==0 -> (MOVLconst [0])
  1488  
  1489  // Convert constant subtracts to constant adds
  1490  (SUBQconst [c] x) && c != -(1<<31) -> (ADDQconst [-c] x)
  1491  (SUBLconst [c] x) -> (ADDLconst [int64(int32(-c))] x)
  1492  
  1493  // generic constant folding
  1494  // TODO: more of this
  1495  (ADDQconst [c] (MOVQconst [d])) -> (MOVQconst [c+d])
  1496  (ADDLconst [c] (MOVLconst [d])) -> (MOVLconst [int64(int32(c+d))])
  1497  (ADDQconst [c] (ADDQconst [d] x)) && is32Bit(c+d) -> (ADDQconst [c+d] x)
  1498  (ADDLconst [c] (ADDLconst [d] x)) -> (ADDLconst [int64(int32(c+d))] x)
  1499  (SUBQconst (MOVQconst [d]) [c]) -> (MOVQconst [d-c])
  1500  (SUBQconst (SUBQconst x [d]) [c]) && is32Bit(-c-d) -> (ADDQconst [-c-d] x)
  1501  (SARQconst [c] (MOVQconst [d])) -> (MOVQconst [d>>uint64(c)])
  1502  (SARLconst [c] (MOVQconst [d])) -> (MOVQconst [d>>uint64(c)])
  1503  (SARWconst [c] (MOVQconst [d])) -> (MOVQconst [d>>uint64(c)])
  1504  (SARBconst [c] (MOVQconst [d])) -> (MOVQconst [d>>uint64(c)])
  1505  (NEGQ (MOVQconst [c])) -> (MOVQconst [-c])
  1506  (NEGL (MOVLconst [c])) -> (MOVLconst [int64(int32(-c))])
  1507  (MULQconst [c] (MOVQconst [d])) -> (MOVQconst [c*d])
  1508  (MULLconst [c] (MOVLconst [d])) -> (MOVLconst [int64(int32(c*d))])
  1509  (ANDQconst [c] (MOVQconst [d])) -> (MOVQconst [c&d])
  1510  (ANDLconst [c] (MOVLconst [d])) -> (MOVLconst [c&d])
  1511  (ORQconst [c] (MOVQconst [d])) -> (MOVQconst [c|d])
  1512  (ORLconst [c] (MOVLconst [d])) -> (MOVLconst [c|d])
  1513  (XORQconst [c] (MOVQconst [d])) -> (MOVQconst [c^d])
  1514  (XORLconst [c] (MOVLconst [d])) -> (MOVLconst [c^d])
  1515  (NOTQ (MOVQconst [c])) -> (MOVQconst [^c])
  1516  (NOTL (MOVLconst [c])) -> (MOVLconst [^c])
  1517  
  1518  // generic simplifications
  1519  // TODO: more of this
  1520  (ADDQ x (NEGQ y)) -> (SUBQ x y)
  1521  (ADDL x (NEGL y)) -> (SUBL x y)
  1522  (SUBQ x x) -> (MOVQconst [0])
  1523  (SUBL x x) -> (MOVLconst [0])
  1524  (ANDQ x x) -> x
  1525  (ANDL x x) -> x
  1526  (ORQ x x) -> x
  1527  (ORL x x) -> x
  1528  (XORQ x x) -> (MOVQconst [0])
  1529  (XORL x x) -> (MOVLconst [0])
  1530  (NEGQ (ADDQconst [c] (NEGQ x))) && c != -(1<<31) -> (ADDQconst [-c] x)
  1531  
  1532  // checking AND against 0.
  1533  (CMPQconst (ANDQ x y) [0]) -> (TESTQ x y)
  1534  (CMPLconst (ANDL x y) [0]) -> (TESTL x y)
  1535  (CMPWconst (ANDL x y) [0]) -> (TESTW x y)
  1536  (CMPBconst (ANDL x y) [0]) -> (TESTB x y)
  1537  (CMPQconst (ANDQconst [c] x) [0]) -> (TESTQconst [c] x)
  1538  (CMPLconst (ANDLconst [c] x) [0]) -> (TESTLconst [c] x)
  1539  (CMPWconst (ANDLconst [c] x) [0]) -> (TESTWconst [int64(int16(c))] x)
  1540  (CMPBconst (ANDLconst [c] x) [0]) -> (TESTBconst [int64(int8(c))] x)
  1541  
  1542  // Convert TESTx to TESTxconst if possible.
  1543  (TESTQ (MOVQconst [c]) x) && is32Bit(c) -> (TESTQconst [c] x)
  1544  (TESTL (MOVLconst [c]) x) -> (TESTLconst [c] x)
  1545  (TESTW (MOVLconst [c]) x) -> (TESTWconst [c] x)
  1546  (TESTB (MOVLconst [c]) x) -> (TESTBconst [c] x)
  1547  
  1548  // TEST %reg,%reg is shorter than CMP
  1549  (CMPQconst x [0]) -> (TESTQ x x)
  1550  (CMPLconst x [0]) -> (TESTL x x)
  1551  (CMPWconst x [0]) -> (TESTW x x)
  1552  (CMPBconst x [0]) -> (TESTB x x)
  1553  
  1554  // Combining byte loads into larger (unaligned) loads.
  1555  // There are many ways these combinations could occur.  This is
  1556  // designed to match the way encoding/binary.LittleEndian does it.
  1557  
  1558  // Little-endian loads
  1559  
  1560  (ORL                  x0:(MOVBload [i0] {s} p mem)
  1561      sh:(SHLLconst [8] x1:(MOVBload [i1] {s} p mem)))
  1562    && i1 == i0+1
  1563    && x0.Uses == 1
  1564    && x1.Uses == 1
  1565    && sh.Uses == 1
  1566    && mergePoint(b,x0,x1) != nil
  1567    && clobber(x0)
  1568    && clobber(x1)
  1569    && clobber(sh)
  1570    -> @mergePoint(b,x0,x1) (MOVWload [i0] {s} p mem)
  1571  
  1572  (ORQ                  x0:(MOVBload [i0] {s} p mem)
  1573      sh:(SHLQconst [8] x1:(MOVBload [i1] {s} p mem)))
  1574    && i1 == i0+1
  1575    && x0.Uses == 1
  1576    && x1.Uses == 1
  1577    && sh.Uses == 1
  1578    && mergePoint(b,x0,x1) != nil
  1579    && clobber(x0)
  1580    && clobber(x1)
  1581    && clobber(sh)
  1582    -> @mergePoint(b,x0,x1) (MOVWload [i0] {s} p mem)
  1583  
  1584  (ORL                   x0:(MOVWload [i0] {s} p mem)
  1585      sh:(SHLLconst [16] x1:(MOVWload [i1] {s} p mem)))
  1586    && i1 == i0+2
  1587    && x0.Uses == 1
  1588    && x1.Uses == 1
  1589    && sh.Uses == 1
  1590    && mergePoint(b,x0,x1) != nil
  1591    && clobber(x0)
  1592    && clobber(x1)
  1593    && clobber(sh)
  1594    -> @mergePoint(b,x0,x1) (MOVLload [i0] {s} p mem)
  1595  
  1596  (ORQ                   x0:(MOVWload [i0] {s} p mem)
  1597      sh:(SHLQconst [16] x1:(MOVWload [i1] {s} p mem)))
  1598    && i1 == i0+2
  1599    && x0.Uses == 1
  1600    && x1.Uses == 1
  1601    && sh.Uses == 1
  1602    && mergePoint(b,x0,x1) != nil
  1603    && clobber(x0)
  1604    && clobber(x1)
  1605    && clobber(sh)
  1606    -> @mergePoint(b,x0,x1) (MOVLload [i0] {s} p mem)
  1607  
  1608  (ORQ                   x0:(MOVLload [i0] {s} p mem)
  1609      sh:(SHLQconst [32] x1:(MOVLload [i1] {s} p mem)))
  1610    && i1 == i0+4
  1611    && x0.Uses == 1
  1612    && x1.Uses == 1
  1613    && sh.Uses == 1
  1614    && mergePoint(b,x0,x1) != nil
  1615    && clobber(x0)
  1616    && clobber(x1)
  1617    && clobber(sh)
  1618    -> @mergePoint(b,x0,x1) (MOVQload [i0] {s} p mem)
  1619  
  1620  (ORL
  1621      s1:(SHLLconst [j1] x1:(MOVBload [i1] {s} p mem))
  1622      or:(ORL
  1623          s0:(SHLLconst [j0] x0:(MOVBload [i0] {s} p mem))
  1624  	y))
  1625    && i1 == i0+1
  1626    && j1 == j0+8
  1627    && j0 % 16 == 0
  1628    && x0.Uses == 1
  1629    && x1.Uses == 1
  1630    && s0.Uses == 1
  1631    && s1.Uses == 1
  1632    && or.Uses == 1
  1633    && mergePoint(b,x0,x1) != nil
  1634    && clobber(x0)
  1635    && clobber(x1)
  1636    && clobber(s0)
  1637    && clobber(s1)
  1638    && clobber(or)
  1639    -> @mergePoint(b,x0,x1) (ORL <v.Type> (SHLLconst <v.Type> [j0] (MOVWload [i0] {s} p mem)) y)
  1640  
  1641  (ORQ
  1642      s1:(SHLQconst [j1] x1:(MOVBload [i1] {s} p mem))
  1643      or:(ORQ
  1644          s0:(SHLQconst [j0] x0:(MOVBload [i0] {s} p mem))
  1645  	y))
  1646    && i1 == i0+1
  1647    && j1 == j0+8
  1648    && j0 % 16 == 0
  1649    && x0.Uses == 1
  1650    && x1.Uses == 1
  1651    && s0.Uses == 1
  1652    && s1.Uses == 1
  1653    && or.Uses == 1
  1654    && mergePoint(b,x0,x1) != nil
  1655    && clobber(x0)
  1656    && clobber(x1)
  1657    && clobber(s0)
  1658    && clobber(s1)
  1659    && clobber(or)
  1660    -> @mergePoint(b,x0,x1) (ORQ <v.Type> (SHLQconst <v.Type> [j0] (MOVWload [i0] {s} p mem)) y)
  1661  
  1662  (ORQ
  1663      s1:(SHLQconst [j1] x1:(MOVWload [i1] {s} p mem))
  1664      or:(ORQ
  1665          s0:(SHLQconst [j0] x0:(MOVWload [i0] {s} p mem))
  1666  	y))
  1667    && i1 == i0+2
  1668    && j1 == j0+16
  1669    && j0 % 32 == 0
  1670    && x0.Uses == 1
  1671    && x1.Uses == 1
  1672    && s0.Uses == 1
  1673    && s1.Uses == 1
  1674    && or.Uses == 1
  1675    && mergePoint(b,x0,x1) != nil
  1676    && clobber(x0)
  1677    && clobber(x1)
  1678    && clobber(s0)
  1679    && clobber(s1)
  1680    && clobber(or)
  1681    -> @mergePoint(b,x0,x1) (ORQ <v.Type> (SHLQconst <v.Type> [j0] (MOVLload [i0] {s} p mem)) y)
  1682  
  1683  // Little-endian indexed loads
  1684  
  1685  (ORL                  x0:(MOVBloadidx1 [i0] {s} p idx mem)
  1686      sh:(SHLLconst [8] x1:(MOVBloadidx1 [i1] {s} p idx mem)))
  1687    && i1 == i0+1
  1688    && x0.Uses == 1
  1689    && x1.Uses == 1
  1690    && sh.Uses == 1
  1691    && mergePoint(b,x0,x1) != nil
  1692    && clobber(x0)
  1693    && clobber(x1)
  1694    && clobber(sh)
  1695    -> @mergePoint(b,x0,x1) (MOVWloadidx1 <v.Type> [i0] {s} p idx mem)
  1696  
  1697  (ORQ                  x0:(MOVBloadidx1 [i0] {s} p idx mem)
  1698      sh:(SHLQconst [8] x1:(MOVBloadidx1 [i1] {s} p idx mem)))
  1699    && i1 == i0+1
  1700    && x0.Uses == 1
  1701    && x1.Uses == 1
  1702    && sh.Uses == 1
  1703    && mergePoint(b,x0,x1) != nil
  1704    && clobber(x0)
  1705    && clobber(x1)
  1706    && clobber(sh)
  1707    -> @mergePoint(b,x0,x1) (MOVWloadidx1 <v.Type> [i0] {s} p idx mem)
  1708  
  1709  (ORL                   x0:(MOVWloadidx1 [i0] {s} p idx mem)
  1710      sh:(SHLLconst [16] x1:(MOVWloadidx1 [i1] {s} p idx mem)))
  1711    && i1 == i0+2
  1712    && x0.Uses == 1
  1713    && x1.Uses == 1
  1714    && sh.Uses == 1
  1715    && mergePoint(b,x0,x1) != nil
  1716    && clobber(x0)
  1717    && clobber(x1)
  1718    && clobber(sh)
  1719    -> @mergePoint(b,x0,x1) (MOVLloadidx1 [i0] {s} p idx mem)
  1720  
  1721  (ORQ                   x0:(MOVWloadidx1 [i0] {s} p idx mem)
  1722      sh:(SHLQconst [16] x1:(MOVWloadidx1 [i1] {s} p idx mem)))
  1723    && i1 == i0+2
  1724    && x0.Uses == 1
  1725    && x1.Uses == 1
  1726    && sh.Uses == 1
  1727    && mergePoint(b,x0,x1) != nil
  1728    && clobber(x0)
  1729    && clobber(x1)
  1730    && clobber(sh)
  1731    -> @mergePoint(b,x0,x1) (MOVLloadidx1 [i0] {s} p idx mem)
  1732  
  1733  (ORQ                   x0:(MOVLloadidx1 [i0] {s} p idx mem)
  1734      sh:(SHLQconst [32] x1:(MOVLloadidx1 [i1] {s} p idx mem)))
  1735    && i1 == i0+4
  1736    && x0.Uses == 1
  1737    && x1.Uses == 1
  1738    && sh.Uses == 1
  1739    && mergePoint(b,x0,x1) != nil
  1740    && clobber(x0)
  1741    && clobber(x1)
  1742    && clobber(sh)
  1743    -> @mergePoint(b,x0,x1) (MOVQloadidx1 [i0] {s} p idx mem)
  1744  
  1745  (ORL
  1746      s1:(SHLLconst [j1] x1:(MOVBloadidx1 [i1] {s} p idx mem))
  1747      or:(ORL
  1748          s0:(SHLLconst [j0] x0:(MOVBloadidx1 [i0] {s} p idx mem))
  1749  	y))
  1750    && i1 == i0+1
  1751    && j1 == j0+8
  1752    && j0 % 16 == 0
  1753    && x0.Uses == 1
  1754    && x1.Uses == 1
  1755    && s0.Uses == 1
  1756    && s1.Uses == 1
  1757    && or.Uses == 1
  1758    && mergePoint(b,x0,x1) != nil
  1759    && clobber(x0)
  1760    && clobber(x1)
  1761    && clobber(s0)
  1762    && clobber(s1)
  1763    && clobber(or)
  1764    -> @mergePoint(b,x0,x1) (ORL <v.Type> (SHLLconst <v.Type> [j0] (MOVWloadidx1 [i0] {s} p idx mem)) y)
  1765  
  1766  (ORQ
  1767      s1:(SHLQconst [j1] x1:(MOVBloadidx1 [i1] {s} p idx mem))
  1768      or:(ORQ
  1769          s0:(SHLQconst [j0] x0:(MOVBloadidx1 [i0] {s} p idx mem))
  1770  	y))
  1771    && i1 == i0+1
  1772    && j1 == j0+8
  1773    && j0 % 16 == 0
  1774    && x0.Uses == 1
  1775    && x1.Uses == 1
  1776    && s0.Uses == 1
  1777    && s1.Uses == 1
  1778    && or.Uses == 1
  1779    && mergePoint(b,x0,x1) != nil
  1780    && clobber(x0)
  1781    && clobber(x1)
  1782    && clobber(s0)
  1783    && clobber(s1)
  1784    && clobber(or)
  1785    -> @mergePoint(b,x0,x1) (ORQ <v.Type> (SHLQconst <v.Type> [j0] (MOVWloadidx1 [i0] {s} p idx mem)) y)
  1786  
  1787  (ORQ
  1788      s1:(SHLQconst [j1] x1:(MOVWloadidx1 [i1] {s} p idx mem))
  1789      or:(ORQ
  1790          s0:(SHLQconst [j0] x0:(MOVWloadidx1 [i0] {s} p idx mem))
  1791  	y))
  1792    && i1 == i0+2
  1793    && j1 == j0+16
  1794    && j0 % 32 == 0
  1795    && x0.Uses == 1
  1796    && x1.Uses == 1
  1797    && s0.Uses == 1
  1798    && s1.Uses == 1
  1799    && or.Uses == 1
  1800    && mergePoint(b,x0,x1) != nil
  1801    && clobber(x0)
  1802    && clobber(x1)
  1803    && clobber(s0)
  1804    && clobber(s1)
  1805    && clobber(or)
  1806    -> @mergePoint(b,x0,x1) (ORQ <v.Type> (SHLQconst <v.Type> [j0] (MOVLloadidx1 [i0] {s} p idx mem)) y)
  1807  
  1808  // Big-endian loads
  1809  
  1810  (ORL
  1811                         x1:(MOVBload [i1] {s} p mem)
  1812      sh:(SHLLconst [8]  x0:(MOVBload [i0] {s} p mem)))
  1813    && i1 == i0+1
  1814    && x0.Uses == 1
  1815    && x1.Uses == 1
  1816    && sh.Uses == 1
  1817    && mergePoint(b,x0,x1) != nil
  1818    && clobber(x0)
  1819    && clobber(x1)
  1820    && clobber(sh)
  1821    -> @mergePoint(b,x0,x1) (ROLWconst <v.Type> [8] (MOVWload [i0] {s} p mem))
  1822  
  1823  (ORQ
  1824                         x1:(MOVBload [i1] {s} p mem)
  1825      sh:(SHLQconst [8]  x0:(MOVBload [i0] {s} p mem)))
  1826    && i1 == i0+1
  1827    && x0.Uses == 1
  1828    && x1.Uses == 1
  1829    && sh.Uses == 1
  1830    && mergePoint(b,x0,x1) != nil
  1831    && clobber(x0)
  1832    && clobber(x1)
  1833    && clobber(sh)
  1834    -> @mergePoint(b,x0,x1) (ROLWconst <v.Type> [8] (MOVWload [i0] {s} p mem))
  1835  
  1836  (ORL
  1837                          r1:(ROLWconst [8] x1:(MOVWload [i1] {s} p mem))
  1838      sh:(SHLLconst [16]  r0:(ROLWconst [8] x0:(MOVWload [i0] {s} p mem))))
  1839    && i1 == i0+2
  1840    && x0.Uses == 1
  1841    && x1.Uses == 1
  1842    && r0.Uses == 1
  1843    && r1.Uses == 1
  1844    && sh.Uses == 1
  1845    && mergePoint(b,x0,x1) != nil
  1846    && clobber(x0)
  1847    && clobber(x1)
  1848    && clobber(r0)
  1849    && clobber(r1)
  1850    && clobber(sh)
  1851    -> @mergePoint(b,x0,x1) (BSWAPL <v.Type> (MOVLload [i0] {s} p mem))
  1852  
  1853  (ORQ
  1854                          r1:(ROLWconst [8] x1:(MOVWload [i1] {s} p mem))
  1855      sh:(SHLQconst [16]  r0:(ROLWconst [8] x0:(MOVWload [i0] {s} p mem))))
  1856    && i1 == i0+2
  1857    && x0.Uses == 1
  1858    && x1.Uses == 1
  1859    && r0.Uses == 1
  1860    && r1.Uses == 1
  1861    && sh.Uses == 1
  1862    && mergePoint(b,x0,x1) != nil
  1863    && clobber(x0)
  1864    && clobber(x1)
  1865    && clobber(r0)
  1866    && clobber(r1)
  1867    && clobber(sh)
  1868    -> @mergePoint(b,x0,x1) (BSWAPL <v.Type> (MOVLload [i0] {s} p mem))
  1869  
  1870  (ORQ
  1871                          r1:(BSWAPL x1:(MOVLload [i1] {s} p mem))
  1872      sh:(SHLQconst [32]  r0:(BSWAPL x0:(MOVLload [i0] {s} p mem))))
  1873    && i1 == i0+4
  1874    && x0.Uses == 1
  1875    && x1.Uses == 1
  1876    && r0.Uses == 1
  1877    && r1.Uses == 1
  1878    && sh.Uses == 1
  1879    && mergePoint(b,x0,x1) != nil
  1880    && clobber(x0)
  1881    && clobber(x1)
  1882    && clobber(r0)
  1883    && clobber(r1)
  1884    && clobber(sh)
  1885    -> @mergePoint(b,x0,x1) (BSWAPQ <v.Type> (MOVQload [i0] {s} p mem))
  1886  
  1887  (ORL
  1888      s0:(SHLLconst [j0] x0:(MOVBload [i0] {s} p mem))
  1889      or:(ORL
  1890          s1:(SHLLconst [j1] x1:(MOVBload [i1] {s} p mem))
  1891  	y))
  1892    && i1 == i0+1
  1893    && j1 == j0-8
  1894    && j1 % 16 == 0
  1895    && x0.Uses == 1
  1896    && x1.Uses == 1
  1897    && s0.Uses == 1
  1898    && s1.Uses == 1
  1899    && or.Uses == 1
  1900    && mergePoint(b,x0,x1) != nil
  1901    && clobber(x0)
  1902    && clobber(x1)
  1903    && clobber(s0)
  1904    && clobber(s1)
  1905    && clobber(or)
  1906    -> @mergePoint(b,x0,x1) (ORL <v.Type> (SHLLconst <v.Type> [j1] (ROLWconst <typ.UInt16> [8] (MOVWload [i0] {s} p mem))) y)
  1907  
  1908  (ORQ
  1909      s0:(SHLQconst [j0] x0:(MOVBload [i0] {s} p mem))
  1910      or:(ORQ
  1911          s1:(SHLQconst [j1] x1:(MOVBload [i1] {s} p mem))
  1912  	y))
  1913    && i1 == i0+1
  1914    && j1 == j0-8
  1915    && j1 % 16 == 0
  1916    && x0.Uses == 1
  1917    && x1.Uses == 1
  1918    && s0.Uses == 1
  1919    && s1.Uses == 1
  1920    && or.Uses == 1
  1921    && mergePoint(b,x0,x1) != nil
  1922    && clobber(x0)
  1923    && clobber(x1)
  1924    && clobber(s0)
  1925    && clobber(s1)
  1926    && clobber(or)
  1927    -> @mergePoint(b,x0,x1) (ORQ <v.Type> (SHLQconst <v.Type> [j1] (ROLWconst <typ.UInt16> [8] (MOVWload [i0] {s} p mem))) y)
  1928  
  1929  (ORQ
  1930      s0:(SHLQconst [j0] r0:(ROLWconst [8] x0:(MOVWload [i0] {s} p mem)))
  1931      or:(ORQ
  1932          s1:(SHLQconst [j1] r1:(ROLWconst [8] x1:(MOVWload [i1] {s} p mem)))
  1933  	y))
  1934    && i1 == i0+2
  1935    && j1 == j0-16
  1936    && j1 % 32 == 0
  1937    && x0.Uses == 1
  1938    && x1.Uses == 1
  1939    && r0.Uses == 1
  1940    && r1.Uses == 1
  1941    && s0.Uses == 1
  1942    && s1.Uses == 1
  1943    && or.Uses == 1
  1944    && mergePoint(b,x0,x1) != nil
  1945    && clobber(x0)
  1946    && clobber(x1)
  1947    && clobber(r0)
  1948    && clobber(r1)
  1949    && clobber(s0)
  1950    && clobber(s1)
  1951    && clobber(or)
  1952    -> @mergePoint(b,x0,x1) (ORQ <v.Type> (SHLQconst <v.Type> [j1] (BSWAPL <typ.UInt32> (MOVLload [i0] {s} p mem))) y)
  1953  
  1954  // Big-endian indexed loads
  1955  
  1956  (ORL
  1957                         x1:(MOVBloadidx1 [i1] {s} p idx mem)
  1958      sh:(SHLLconst [8]  x0:(MOVBloadidx1 [i0] {s} p idx mem)))
  1959    && i1 == i0+1
  1960    && x0.Uses == 1
  1961    && x1.Uses == 1
  1962    && sh.Uses == 1
  1963    && mergePoint(b,x0,x1) != nil
  1964    && clobber(x0)
  1965    && clobber(x1)
  1966    && clobber(sh)
  1967    -> @mergePoint(b,x0,x1) (ROLWconst <v.Type> [8] (MOVWloadidx1 [i0] {s} p idx mem))
  1968  
  1969  (ORQ
  1970                         x1:(MOVBloadidx1 [i1] {s} p idx mem)
  1971      sh:(SHLQconst [8]  x0:(MOVBloadidx1 [i0] {s} p idx mem)))
  1972    && i1 == i0+1
  1973    && x0.Uses == 1
  1974    && x1.Uses == 1
  1975    && sh.Uses == 1
  1976    && mergePoint(b,x0,x1) != nil
  1977    && clobber(x0)
  1978    && clobber(x1)
  1979    && clobber(sh)
  1980    -> @mergePoint(b,x0,x1) (ROLWconst <v.Type> [8] (MOVWloadidx1 [i0] {s} p idx mem))
  1981  
  1982  (ORL
  1983                          r1:(ROLWconst [8] x1:(MOVWloadidx1 [i1] {s} p idx mem))
  1984      sh:(SHLLconst [16]  r0:(ROLWconst [8] x0:(MOVWloadidx1 [i0] {s} p idx mem))))
  1985    && i1 == i0+2
  1986    && x0.Uses == 1
  1987    && x1.Uses == 1
  1988    && r0.Uses == 1
  1989    && r1.Uses == 1
  1990    && sh.Uses == 1
  1991    && mergePoint(b,x0,x1) != nil
  1992    && clobber(x0)
  1993    && clobber(x1)
  1994    && clobber(r0)
  1995    && clobber(r1)
  1996    && clobber(sh)
  1997    -> @mergePoint(b,x0,x1) (BSWAPL <v.Type> (MOVLloadidx1 [i0] {s} p idx mem))
  1998  
  1999  (ORQ
  2000                          r1:(ROLWconst [8] x1:(MOVWloadidx1 [i1] {s} p idx mem))
  2001      sh:(SHLQconst [16]  r0:(ROLWconst [8] x0:(MOVWloadidx1 [i0] {s} p idx mem))))
  2002    && i1 == i0+2
  2003    && x0.Uses == 1
  2004    && x1.Uses == 1
  2005    && r0.Uses == 1
  2006    && r1.Uses == 1
  2007    && sh.Uses == 1
  2008    && mergePoint(b,x0,x1) != nil
  2009    && clobber(x0)
  2010    && clobber(x1)
  2011    && clobber(r0)
  2012    && clobber(r1)
  2013    && clobber(sh)
  2014    -> @mergePoint(b,x0,x1) (BSWAPL <v.Type> (MOVLloadidx1 [i0] {s} p idx mem))
  2015  
  2016  (ORQ
  2017                          r1:(BSWAPL x1:(MOVLloadidx1 [i1] {s} p idx mem))
  2018      sh:(SHLQconst [32]  r0:(BSWAPL x0:(MOVLloadidx1 [i0] {s} p idx mem))))
  2019    && i1 == i0+4
  2020    && x0.Uses == 1
  2021    && x1.Uses == 1
  2022    && r0.Uses == 1
  2023    && r1.Uses == 1
  2024    && sh.Uses == 1
  2025    && mergePoint(b,x0,x1) != nil
  2026    && clobber(x0)
  2027    && clobber(x1)
  2028    && clobber(r0)
  2029    && clobber(r1)
  2030    && clobber(sh)
  2031    -> @mergePoint(b,x0,x1) (BSWAPQ <v.Type> (MOVQloadidx1 [i0] {s} p idx mem))
  2032  
  2033  (ORL
  2034      s0:(SHLLconst [j0] x0:(MOVBloadidx1 [i0] {s} p idx mem))
  2035      or:(ORL
  2036          s1:(SHLLconst [j1] x1:(MOVBloadidx1 [i1] {s} p idx mem))
  2037  	y))
  2038    && i1 == i0+1
  2039    && j1 == j0-8
  2040    && j1 % 16 == 0
  2041    && x0.Uses == 1
  2042    && x1.Uses == 1
  2043    && s0.Uses == 1
  2044    && s1.Uses == 1
  2045    && or.Uses == 1
  2046    && mergePoint(b,x0,x1) != nil
  2047    && clobber(x0)
  2048    && clobber(x1)
  2049    && clobber(s0)
  2050    && clobber(s1)
  2051    && clobber(or)
  2052    -> @mergePoint(b,x0,x1) (ORL <v.Type> (SHLLconst <v.Type> [j1] (ROLWconst <typ.UInt16> [8] (MOVWloadidx1 [i0] {s} p idx mem))) y)
  2053  
  2054  (ORQ
  2055      s0:(SHLQconst [j0] x0:(MOVBloadidx1 [i0] {s} p idx mem))
  2056      or:(ORQ
  2057          s1:(SHLQconst [j1] x1:(MOVBloadidx1 [i1] {s} p idx mem))
  2058  	y))
  2059    && i1 == i0+1
  2060    && j1 == j0-8
  2061    && j1 % 16 == 0
  2062    && x0.Uses == 1
  2063    && x1.Uses == 1
  2064    && s0.Uses == 1
  2065    && s1.Uses == 1
  2066    && or.Uses == 1
  2067    && mergePoint(b,x0,x1) != nil
  2068    && clobber(x0)
  2069    && clobber(x1)
  2070    && clobber(s0)
  2071    && clobber(s1)
  2072    && clobber(or)
  2073    -> @mergePoint(b,x0,x1) (ORQ <v.Type> (SHLQconst <v.Type> [j1] (ROLWconst <typ.UInt16> [8] (MOVWloadidx1 [i0] {s} p idx mem))) y)
  2074  
  2075  (ORQ
  2076      s0:(SHLQconst [j0] r0:(ROLWconst [8] x0:(MOVWloadidx1 [i0] {s} p idx mem)))
  2077      or:(ORQ
  2078          s1:(SHLQconst [j1] r1:(ROLWconst [8] x1:(MOVWloadidx1 [i1] {s} p idx mem)))
  2079  	y))
  2080    && i1 == i0+2
  2081    && j1 == j0-16
  2082    && j1 % 32 == 0
  2083    && x0.Uses == 1
  2084    && x1.Uses == 1
  2085    && r0.Uses == 1
  2086    && r1.Uses == 1
  2087    && s0.Uses == 1
  2088    && s1.Uses == 1
  2089    && or.Uses == 1
  2090    && mergePoint(b,x0,x1) != nil
  2091    && clobber(x0)
  2092    && clobber(x1)
  2093    && clobber(r0)
  2094    && clobber(r1)
  2095    && clobber(s0)
  2096    && clobber(s1)
  2097    && clobber(or)
  2098    -> @mergePoint(b,x0,x1) (ORQ <v.Type> (SHLQconst <v.Type> [j1] (BSWAPL <typ.UInt32> (MOVLloadidx1 [i0] {s} p idx mem))) y)
  2099  
  2100  // Combine 2 byte stores + shift into rolw 8 + word store
  2101  (MOVBstore [i] {s} p w
  2102    x0:(MOVBstore [i-1] {s} p (SHRWconst [8] w) mem))
  2103    && x0.Uses == 1
  2104    && clobber(x0)
  2105    -> (MOVWstore [i-1] {s} p (ROLWconst <w.Type> [8] w) mem)
  2106  
  2107  (MOVBstoreidx1 [i] {s} p idx w
  2108    x0:(MOVBstoreidx1 [i-1] {s} p idx (SHRWconst [8] w) mem))
  2109    && x0.Uses == 1
  2110    && clobber(x0)
  2111    -> (MOVWstoreidx1 [i-1] {s} p idx (ROLWconst <w.Type> [8] w) mem)
  2112  
  2113  // Combine stores + shifts into bswap and larger (unaligned) stores
  2114  (MOVBstore [i] {s} p w
  2115    x2:(MOVBstore [i-1] {s} p (SHRLconst [8] w)
  2116    x1:(MOVBstore [i-2] {s} p (SHRLconst [16] w)
  2117    x0:(MOVBstore [i-3] {s} p (SHRLconst [24] w) mem))))
  2118    && x0.Uses == 1
  2119    && x1.Uses == 1
  2120    && x2.Uses == 1
  2121    && clobber(x0)
  2122    && clobber(x1)
  2123    && clobber(x2)
  2124    -> (MOVLstore [i-3] {s} p (BSWAPL <w.Type> w) mem)
  2125  
  2126  (MOVBstoreidx1 [i] {s} p idx w
  2127    x2:(MOVBstoreidx1 [i-1] {s} p idx (SHRLconst [8] w)
  2128    x1:(MOVBstoreidx1 [i-2] {s} p idx (SHRLconst [16] w)
  2129    x0:(MOVBstoreidx1 [i-3] {s} p idx (SHRLconst [24] w) mem))))
  2130    && x0.Uses == 1
  2131    && x1.Uses == 1
  2132    && x2.Uses == 1
  2133    && clobber(x0)
  2134    && clobber(x1)
  2135    && clobber(x2)
  2136    -> (MOVLstoreidx1 [i-3] {s} p idx (BSWAPL <w.Type> w) mem)
  2137  
  2138  (MOVBstore [i] {s} p w
  2139    x6:(MOVBstore [i-1] {s} p (SHRQconst [8] w)
  2140    x5:(MOVBstore [i-2] {s} p (SHRQconst [16] w)
  2141    x4:(MOVBstore [i-3] {s} p (SHRQconst [24] w)
  2142    x3:(MOVBstore [i-4] {s} p (SHRQconst [32] w)
  2143    x2:(MOVBstore [i-5] {s} p (SHRQconst [40] w)
  2144    x1:(MOVBstore [i-6] {s} p (SHRQconst [48] w)
  2145    x0:(MOVBstore [i-7] {s} p (SHRQconst [56] w) mem))))))))
  2146    && x0.Uses == 1
  2147    && x1.Uses == 1
  2148    && x2.Uses == 1
  2149    && x3.Uses == 1
  2150    && x4.Uses == 1
  2151    && x5.Uses == 1
  2152    && x6.Uses == 1
  2153    && clobber(x0)
  2154    && clobber(x1)
  2155    && clobber(x2)
  2156    && clobber(x3)
  2157    && clobber(x4)
  2158    && clobber(x5)
  2159    && clobber(x6)
  2160    -> (MOVQstore [i-7] {s} p (BSWAPQ <w.Type> w) mem)
  2161  
  2162  (MOVBstoreidx1 [i] {s} p idx w
  2163    x6:(MOVBstoreidx1 [i-1] {s} p idx (SHRQconst [8] w)
  2164    x5:(MOVBstoreidx1 [i-2] {s} p idx (SHRQconst [16] w)
  2165    x4:(MOVBstoreidx1 [i-3] {s} p idx (SHRQconst [24] w)
  2166    x3:(MOVBstoreidx1 [i-4] {s} p idx (SHRQconst [32] w)
  2167    x2:(MOVBstoreidx1 [i-5] {s} p idx (SHRQconst [40] w)
  2168    x1:(MOVBstoreidx1 [i-6] {s} p idx (SHRQconst [48] w)
  2169    x0:(MOVBstoreidx1 [i-7] {s} p idx (SHRQconst [56] w) mem))))))))
  2170    && x0.Uses == 1
  2171    && x1.Uses == 1
  2172    && x2.Uses == 1
  2173    && x3.Uses == 1
  2174    && x4.Uses == 1
  2175    && x5.Uses == 1
  2176    && x6.Uses == 1
  2177    && clobber(x0)
  2178    && clobber(x1)
  2179    && clobber(x2)
  2180    && clobber(x3)
  2181    && clobber(x4)
  2182    && clobber(x5)
  2183    && clobber(x6)
  2184    -> (MOVQstoreidx1 [i-7] {s} p idx (BSWAPQ <w.Type> w) mem)
  2185  
  2186  // Combine constant stores into larger (unaligned) stores.
  2187  (MOVBstoreconst [c] {s} p x:(MOVBstoreconst [a] {s} p mem))
  2188    && x.Uses == 1
  2189    && ValAndOff(a).Off() + 1 == ValAndOff(c).Off()
  2190    && clobber(x)
  2191    -> (MOVWstoreconst [makeValAndOff(ValAndOff(a).Val()&0xff | ValAndOff(c).Val()<<8, ValAndOff(a).Off())] {s} p mem)
  2192  (MOVWstoreconst [c] {s} p x:(MOVWstoreconst [a] {s} p mem))
  2193    && x.Uses == 1
  2194    && ValAndOff(a).Off() + 2 == ValAndOff(c).Off()
  2195    && clobber(x)
  2196    -> (MOVLstoreconst [makeValAndOff(ValAndOff(a).Val()&0xffff | ValAndOff(c).Val()<<16, ValAndOff(a).Off())] {s} p mem)
  2197  (MOVLstoreconst [c] {s} p x:(MOVLstoreconst [a] {s} p mem))
  2198    && x.Uses == 1
  2199    && ValAndOff(a).Off() + 4 == ValAndOff(c).Off()
  2200    && clobber(x)
  2201    -> (MOVQstore [ValAndOff(a).Off()] {s} p (MOVQconst [ValAndOff(a).Val()&0xffffffff | ValAndOff(c).Val()<<32]) mem)
  2202  
  2203  (MOVBstoreconstidx1 [c] {s} p i x:(MOVBstoreconstidx1 [a] {s} p i mem))
  2204    && x.Uses == 1
  2205    && ValAndOff(a).Off() + 1 == ValAndOff(c).Off()
  2206    && clobber(x)
  2207    -> (MOVWstoreconstidx1 [makeValAndOff(ValAndOff(a).Val()&0xff | ValAndOff(c).Val()<<8, ValAndOff(a).Off())] {s} p i mem)
  2208  (MOVWstoreconstidx1 [c] {s} p i x:(MOVWstoreconstidx1 [a] {s} p i mem))
  2209    && x.Uses == 1
  2210    && ValAndOff(a).Off() + 2 == ValAndOff(c).Off()
  2211    && clobber(x)
  2212    -> (MOVLstoreconstidx1 [makeValAndOff(ValAndOff(a).Val()&0xffff | ValAndOff(c).Val()<<16, ValAndOff(a).Off())] {s} p i mem)
  2213  (MOVLstoreconstidx1 [c] {s} p i x:(MOVLstoreconstidx1 [a] {s} p i mem))
  2214    && x.Uses == 1
  2215    && ValAndOff(a).Off() + 4 == ValAndOff(c).Off()
  2216    && clobber(x)
  2217    -> (MOVQstoreidx1 [ValAndOff(a).Off()] {s} p i (MOVQconst [ValAndOff(a).Val()&0xffffffff | ValAndOff(c).Val()<<32]) mem)
  2218  
  2219  (MOVWstoreconstidx2 [c] {s} p i x:(MOVWstoreconstidx2 [a] {s} p i mem))
  2220    && x.Uses == 1
  2221    && ValAndOff(a).Off() + 2 == ValAndOff(c).Off()
  2222    && clobber(x)
  2223    -> (MOVLstoreconstidx1 [makeValAndOff(ValAndOff(a).Val()&0xffff | ValAndOff(c).Val()<<16, ValAndOff(a).Off())] {s} p (SHLQconst <i.Type> [1] i) mem)
  2224  (MOVLstoreconstidx4 [c] {s} p i x:(MOVLstoreconstidx4 [a] {s} p i mem))
  2225    && x.Uses == 1
  2226    && ValAndOff(a).Off() + 4 == ValAndOff(c).Off()
  2227    && clobber(x)
  2228    -> (MOVQstoreidx1 [ValAndOff(a).Off()] {s} p (SHLQconst <i.Type> [2] i) (MOVQconst [ValAndOff(a).Val()&0xffffffff | ValAndOff(c).Val()<<32]) mem)
  2229  
  2230  // Combine stores into larger (unaligned) stores.
  2231  (MOVBstore [i] {s} p (SHRQconst [8] w) x:(MOVBstore [i-1] {s} p w mem))
  2232    && x.Uses == 1
  2233    && clobber(x)
  2234    -> (MOVWstore [i-1] {s} p w mem)
  2235  (MOVBstore [i] {s} p (SHRQconst [j] w) x:(MOVBstore [i-1] {s} p w0:(SHRQconst [j-8] w) mem))
  2236    && x.Uses == 1
  2237    && clobber(x)
  2238    -> (MOVWstore [i-1] {s} p w0 mem)
  2239  (MOVWstore [i] {s} p (SHRQconst [16] w) x:(MOVWstore [i-2] {s} p w mem))
  2240    && x.Uses == 1
  2241    && clobber(x)
  2242    -> (MOVLstore [i-2] {s} p w mem)
  2243  (MOVWstore [i] {s} p (SHRQconst [j] w) x:(MOVWstore [i-2] {s} p w0:(SHRQconst [j-16] w) mem))
  2244    && x.Uses == 1
  2245    && clobber(x)
  2246    -> (MOVLstore [i-2] {s} p w0 mem)
  2247  (MOVLstore [i] {s} p (SHRQconst [32] w) x:(MOVLstore [i-4] {s} p w mem))
  2248    && x.Uses == 1
  2249    && clobber(x)
  2250    -> (MOVQstore [i-4] {s} p w mem)
  2251  (MOVLstore [i] {s} p (SHRQconst [j] w) x:(MOVLstore [i-4] {s} p w0:(SHRQconst [j-32] w) mem))
  2252    && x.Uses == 1
  2253    && clobber(x)
  2254    -> (MOVQstore [i-4] {s} p w0 mem)
  2255  
  2256  (MOVBstoreidx1 [i] {s} p idx (SHRQconst [8] w) x:(MOVBstoreidx1 [i-1] {s} p idx w mem))
  2257    && x.Uses == 1
  2258    && clobber(x)
  2259    -> (MOVWstoreidx1 [i-1] {s} p idx w mem)
  2260  (MOVBstoreidx1 [i] {s} p idx (SHRQconst [j] w) x:(MOVBstoreidx1 [i-1] {s} p idx w0:(SHRQconst [j-8] w) mem))
  2261    && x.Uses == 1
  2262    && clobber(x)
  2263    -> (MOVWstoreidx1 [i-1] {s} p idx w0 mem)
  2264  (MOVWstoreidx1 [i] {s} p idx (SHRQconst [16] w) x:(MOVWstoreidx1 [i-2] {s} p idx w mem))
  2265    && x.Uses == 1
  2266    && clobber(x)
  2267    -> (MOVLstoreidx1 [i-2] {s} p idx w mem)
  2268  (MOVWstoreidx1 [i] {s} p idx (SHRQconst [j] w) x:(MOVWstoreidx1 [i-2] {s} p idx w0:(SHRQconst [j-16] w) mem))
  2269    && x.Uses == 1
  2270    && clobber(x)
  2271    -> (MOVLstoreidx1 [i-2] {s} p idx w0 mem)
  2272  (MOVLstoreidx1 [i] {s} p idx (SHRQconst [32] w) x:(MOVLstoreidx1 [i-4] {s} p idx w mem))
  2273    && x.Uses == 1
  2274    && clobber(x)
  2275    -> (MOVQstoreidx1 [i-4] {s} p idx w mem)
  2276  (MOVLstoreidx1 [i] {s} p idx (SHRQconst [j] w) x:(MOVLstoreidx1 [i-4] {s} p idx w0:(SHRQconst [j-32] w) mem))
  2277    && x.Uses == 1
  2278    && clobber(x)
  2279    -> (MOVQstoreidx1 [i-4] {s} p idx w0 mem)
  2280  
  2281  (MOVWstoreidx2 [i] {s} p idx (SHRQconst [16] w) x:(MOVWstoreidx2 [i-2] {s} p idx w mem))
  2282    && x.Uses == 1
  2283    && clobber(x)
  2284    -> (MOVLstoreidx1 [i-2] {s} p (SHLQconst <idx.Type> [1] idx) w mem)
  2285  (MOVWstoreidx2 [i] {s} p idx (SHRQconst [j] w) x:(MOVWstoreidx2 [i-2] {s} p idx w0:(SHRQconst [j-16] w) mem))
  2286    && x.Uses == 1
  2287    && clobber(x)
  2288    -> (MOVLstoreidx1 [i-2] {s} p (SHLQconst <idx.Type> [1] idx) w0 mem)
  2289  (MOVLstoreidx4 [i] {s} p idx (SHRQconst [32] w) x:(MOVLstoreidx4 [i-4] {s} p idx w mem))
  2290    && x.Uses == 1
  2291    && clobber(x)
  2292    -> (MOVQstoreidx1 [i-4] {s} p (SHLQconst <idx.Type> [2] idx) w mem)
  2293  (MOVLstoreidx4 [i] {s} p idx (SHRQconst [j] w) x:(MOVLstoreidx4 [i-4] {s} p idx w0:(SHRQconst [j-32] w) mem))
  2294    && x.Uses == 1
  2295    && clobber(x)
  2296    -> (MOVQstoreidx1 [i-4] {s} p (SHLQconst <idx.Type> [2] idx) w0 mem)
  2297  
  2298  // amd64p32 rules
  2299  // same as the rules above, but with 32 instead of 64 bit pointer arithmetic.
  2300  // LEAQ,ADDQ -> LEAL,ADDL
  2301  (ADDLconst [c] (LEAL [d] {s} x)) && is32Bit(c+d) -> (LEAL [c+d] {s} x)
  2302  (LEAL [c] {s} (ADDLconst [d] x)) && is32Bit(c+d) -> (LEAL [c+d] {s} x)
  2303  
  2304  (MOVQload  [off1] {sym1} (LEAL [off2] {sym2} base) mem) && canMergeSym(sym1, sym2) && is32Bit(off1+off2) ->
  2305  	(MOVQload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  2306  (MOVLload  [off1] {sym1} (LEAL [off2] {sym2} base) mem) && canMergeSym(sym1, sym2) && is32Bit(off1+off2) ->
  2307  	(MOVLload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  2308  (MOVWload  [off1] {sym1} (LEAL [off2] {sym2} base) mem) && canMergeSym(sym1, sym2) && is32Bit(off1+off2) ->
  2309  	(MOVWload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  2310  (MOVBload  [off1] {sym1} (LEAL [off2] {sym2} base) mem) && canMergeSym(sym1, sym2) && is32Bit(off1+off2) ->
  2311  	(MOVBload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  2312  
  2313  (MOVQstore  [off1] {sym1} (LEAL [off2] {sym2} base) val mem) && canMergeSym(sym1, sym2) && is32Bit(off1+off2) ->
  2314  	(MOVQstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  2315  (MOVLstore  [off1] {sym1} (LEAL [off2] {sym2} base) val mem) && canMergeSym(sym1, sym2) && is32Bit(off1+off2) ->
  2316  	(MOVLstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  2317  (MOVWstore  [off1] {sym1} (LEAL [off2] {sym2} base) val mem) && canMergeSym(sym1, sym2) && is32Bit(off1+off2) ->
  2318  	(MOVWstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  2319  (MOVBstore  [off1] {sym1} (LEAL [off2] {sym2} base) val mem) && canMergeSym(sym1, sym2) && is32Bit(off1+off2) ->
  2320  	(MOVBstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  2321  
  2322  (MOVQstoreconst [sc] {sym1} (LEAL [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  2323  	(MOVQstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  2324  (MOVLstoreconst [sc] {sym1} (LEAL [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  2325  	(MOVLstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  2326  (MOVWstoreconst [sc] {sym1} (LEAL [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  2327  	(MOVWstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  2328  (MOVBstoreconst [sc] {sym1} (LEAL [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  2329  	(MOVBstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  2330  
  2331  (MOVQload  [off1] {sym} (ADDLconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVQload  [off1+off2] {sym} ptr mem)
  2332  (MOVLload  [off1] {sym} (ADDLconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVLload  [off1+off2] {sym} ptr mem)
  2333  (MOVWload  [off1] {sym} (ADDLconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVWload  [off1+off2] {sym} ptr mem)
  2334  (MOVBload  [off1] {sym} (ADDLconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVBload  [off1+off2] {sym} ptr mem)
  2335  (MOVQstore  [off1] {sym} (ADDLconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVQstore  [off1+off2] {sym} ptr val mem)
  2336  (MOVLstore  [off1] {sym} (ADDLconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVLstore  [off1+off2] {sym} ptr val mem)
  2337  (MOVWstore  [off1] {sym} (ADDLconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVWstore  [off1+off2] {sym} ptr val mem)
  2338  (MOVBstore  [off1] {sym} (ADDLconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVBstore  [off1+off2] {sym} ptr val mem)
  2339  (MOVQstoreconst [sc] {s} (ADDLconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  2340  	(MOVQstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  2341  (MOVLstoreconst [sc] {s} (ADDLconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  2342  	(MOVLstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  2343  (MOVWstoreconst [sc] {s} (ADDLconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  2344  	(MOVWstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  2345  (MOVBstoreconst [sc] {s} (ADDLconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  2346  	(MOVBstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  2347  
  2348  // Merge load and op
  2349  // TODO: add indexed variants?
  2350  (ADDQ x l:(MOVQload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (ADDQmem x [off] {sym} ptr mem)
  2351  (ADDL x l:(MOVLload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (ADDLmem x [off] {sym} ptr mem)
  2352  (SUBQ x l:(MOVQload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (SUBQmem x [off] {sym} ptr mem)
  2353  (SUBL x l:(MOVLload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (SUBLmem x [off] {sym} ptr mem)
  2354  (ANDQ x l:(MOVQload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (ANDQmem x [off] {sym} ptr mem)
  2355  (ANDL x l:(MOVLload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (ANDLmem x [off] {sym} ptr mem)
  2356  (ORQ  x l:(MOVQload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (ORQmem x [off] {sym} ptr mem)
  2357  (ORL  x l:(MOVLload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (ORLmem x [off] {sym} ptr mem)
  2358  (XORQ x l:(MOVQload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (XORQmem x [off] {sym} ptr mem)
  2359  (XORL x l:(MOVLload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (XORLmem x [off] {sym} ptr mem)
  2360  (ADDSD x l:(MOVSDload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (ADDSDmem x [off] {sym} ptr mem)
  2361  (ADDSS x l:(MOVSSload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (ADDSSmem x [off] {sym} ptr mem)
  2362  (SUBSD x l:(MOVSDload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (SUBSDmem x [off] {sym} ptr mem)
  2363  (SUBSS x l:(MOVSSload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (SUBSSmem x [off] {sym} ptr mem)
  2364  (MULSD x l:(MOVSDload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (MULSDmem x [off] {sym} ptr mem)
  2365  (MULSS x l:(MOVSSload [off] {sym} ptr mem)) && canMergeLoad(v, l, x) && clobber(l) -> (MULSSmem x [off] {sym} ptr mem)
  2366  
  2367  // Merge ADDQconst and LEAQ into atomic loads.
  2368  (MOVQatomicload [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  2369  	(MOVQatomicload [off1+off2] {sym} ptr mem)
  2370  (MOVLatomicload [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  2371  	(MOVLatomicload [off1+off2] {sym} ptr mem)
  2372  (MOVQatomicload [off1] {sym1} (LEAQ [off2] {sym2} ptr) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  2373  	(MOVQatomicload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
  2374  (MOVLatomicload [off1] {sym1} (LEAQ [off2] {sym2} ptr) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  2375  	(MOVLatomicload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
  2376  
  2377  // Merge ADDQconst and LEAQ into atomic stores.
  2378  (XCHGQ [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  2379  	(XCHGQ [off1+off2] {sym} val ptr mem)
  2380  (XCHGQ [off1] {sym1} val (LEAQ [off2] {sym2} ptr) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && ptr.Op != OpSB ->
  2381  	(XCHGQ [off1+off2] {mergeSym(sym1,sym2)} val ptr mem)
  2382  (XCHGL [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  2383  	(XCHGL [off1+off2] {sym} val ptr mem)
  2384  (XCHGL [off1] {sym1} val (LEAQ [off2] {sym2} ptr) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && ptr.Op != OpSB ->
  2385  	(XCHGL [off1+off2] {mergeSym(sym1,sym2)} val ptr mem)
  2386  
  2387  // Merge ADDQconst into atomic adds.
  2388  // TODO: merging LEAQ doesn't work, assembler doesn't like the resulting instructions.
  2389  (XADDQlock [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  2390  	(XADDQlock [off1+off2] {sym} val ptr mem)
  2391  (XADDLlock [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  2392  	(XADDLlock [off1+off2] {sym} val ptr mem)
  2393  
  2394  // Merge ADDQconst into atomic compare and swaps.
  2395  // TODO: merging LEAQ doesn't work, assembler doesn't like the resulting instructions.
  2396  (CMPXCHGQlock [off1] {sym} (ADDQconst [off2] ptr) old new_ mem) && is32Bit(off1+off2) ->
  2397  	(CMPXCHGQlock [off1+off2] {sym} ptr old new_ mem)
  2398  (CMPXCHGLlock [off1] {sym} (ADDQconst [off2] ptr) old new_ mem) && is32Bit(off1+off2) ->
  2399  	(CMPXCHGLlock [off1+off2] {sym} ptr old new_ mem)
  2400  
  2401  // We don't need the conditional move if we know the arg of BSF is not zero.
  2402  (CMOVQEQ x _ (Select1 (BSFQ (ORQconst [c] _)))) && c != 0 -> x
  2403  // Extension is unnecessary for trailing zeros.
  2404  (BSFQ (ORQconst <t> [1<<8] (MOVBQZX x))) -> (BSFQ (ORQconst <t> [1<<8] x))
  2405  (BSFQ (ORQconst <t> [1<<16] (MOVWQZX x))) -> (BSFQ (ORQconst <t> [1<<16] x))
  2406  
  2407  // Redundant sign/zero extensions
  2408  // Note: see issue 21963. We have to make sure we use the right type on
  2409  // the resulting extension (the outer type, not the inner type).
  2410  (MOVLQSX (MOVLQSX x)) -> (MOVLQSX x)
  2411  (MOVLQSX (MOVWQSX x)) -> (MOVWQSX x)
  2412  (MOVLQSX (MOVBQSX x)) -> (MOVBQSX x)
  2413  (MOVWQSX (MOVWQSX x)) -> (MOVWQSX x)
  2414  (MOVWQSX (MOVBQSX x)) -> (MOVBQSX x)
  2415  (MOVBQSX (MOVBQSX x)) -> (MOVBQSX x)
  2416  (MOVLQZX (MOVLQZX x)) -> (MOVLQZX x)
  2417  (MOVLQZX (MOVWQZX x)) -> (MOVWQZX x)
  2418  (MOVLQZX (MOVBQZX x)) -> (MOVBQZX x)
  2419  (MOVWQZX (MOVWQZX x)) -> (MOVWQZX x)
  2420  (MOVWQZX (MOVBQZX x)) -> (MOVBQZX x)
  2421  (MOVBQZX (MOVBQZX x)) -> (MOVBQZX x)