github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/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  (Hmul16  x y) -> (HMULW  x y)
    48  (Hmul16u x y) -> (HMULWU x y)
    49  (Hmul8   x y) -> (HMULB  x y)
    50  (Hmul8u  x y) -> (HMULBU x y)
    51  
    52  (Mul64uhilo x y) -> (MULQU2 x y)
    53  (Div128u xhi xlo y) -> (DIVQU2 xhi xlo y)
    54  
    55  (Avg64u x y) -> (AVGQU x y)
    56  
    57  (Mod64  x y) -> (Select1 (DIVQ  x y))
    58  (Mod64u x y) -> (Select1 (DIVQU x y))
    59  (Mod32  x y) -> (Select1 (DIVL  x y))
    60  (Mod32u x y) -> (Select1 (DIVLU x y))
    61  (Mod16  x y) -> (Select1 (DIVW  x y))
    62  (Mod16u x y) -> (Select1 (DIVWU x y))
    63  (Mod8   x y) -> (Select1 (DIVW  (SignExt8to16 x) (SignExt8to16 y)))
    64  (Mod8u  x y) -> (Select1 (DIVWU (ZeroExt8to16 x) (ZeroExt8to16 y)))
    65  
    66  (And64 x y) -> (ANDQ x y)
    67  (And32 x y) -> (ANDL x y)
    68  (And16 x y) -> (ANDL x y)
    69  (And8  x y) -> (ANDL x y)
    70  
    71  (Or64 x y) -> (ORQ x y)
    72  (Or32 x y) -> (ORL x y)
    73  (Or16 x y) -> (ORL x y)
    74  (Or8  x y) -> (ORL x y)
    75  
    76  (Xor64 x y) -> (XORQ x y)
    77  (Xor32 x y) -> (XORL x y)
    78  (Xor16 x y) -> (XORL x y)
    79  (Xor8  x y) -> (XORL x y)
    80  
    81  (Neg64  x) -> (NEGQ x)
    82  (Neg32  x) -> (NEGL x)
    83  (Neg16  x) -> (NEGL x)
    84  (Neg8   x) -> (NEGL x)
    85  (Neg32F x) -> (PXOR x (MOVSSconst <config.Frontend().TypeFloat32()> [f2i(math.Copysign(0, -1))]))
    86  (Neg64F x) -> (PXOR x (MOVSDconst <config.Frontend().TypeFloat64()> [f2i(math.Copysign(0, -1))]))
    87  
    88  (Com64 x) -> (NOTQ x)
    89  (Com32 x) -> (NOTL x)
    90  (Com16 x) -> (NOTL x)
    91  (Com8  x) -> (NOTL x)
    92  
    93  // Lowering boolean ops
    94  (AndB x y) -> (ANDL x y)
    95  (OrB x y) -> (ORL x y)
    96  (Not x) -> (XORLconst [1] x)
    97  
    98  // Lowering pointer arithmetic
    99  (OffPtr [off] ptr) && config.PtrSize == 8 && is32Bit(off) -> (ADDQconst [off] ptr)
   100  (OffPtr [off] ptr) && config.PtrSize == 8 -> (ADDQ (MOVQconst [off]) ptr)
   101  (OffPtr [off] ptr) && config.PtrSize == 4 -> (ADDLconst [off] ptr)
   102  
   103  // Lowering other arithmetic
   104  (Ctz64 <t> x) -> (CMOVQEQ (Select0 <t> (BSFQ x)) (MOVQconst <t> [64]) (Select1 <TypeFlags> (BSFQ x)))
   105  (Ctz32 <t> x) -> (CMOVLEQ (Select0 <t> (BSFL x)) (MOVLconst <t> [32]) (Select1 <TypeFlags> (BSFL x)))
   106  
   107  (Bswap64 x) -> (BSWAPQ x)
   108  (Bswap32 x) -> (BSWAPL x)
   109  
   110  (Sqrt x) -> (SQRTSD x)
   111  
   112  // Lowering extension
   113  // Note: we always extend to 64 bits even though some ops don't need that many result bits.
   114  (SignExt8to16  x) -> (MOVBQSX x)
   115  (SignExt8to32  x) -> (MOVBQSX x)
   116  (SignExt8to64  x) -> (MOVBQSX x)
   117  (SignExt16to32 x) -> (MOVWQSX x)
   118  (SignExt16to64 x) -> (MOVWQSX x)
   119  (SignExt32to64 x) -> (MOVLQSX x)
   120  
   121  (ZeroExt8to16  x) -> (MOVBQZX x)
   122  (ZeroExt8to32  x) -> (MOVBQZX x)
   123  (ZeroExt8to64  x) -> (MOVBQZX x)
   124  (ZeroExt16to32 x) -> (MOVWQZX x)
   125  (ZeroExt16to64 x) -> (MOVWQZX x)
   126  (ZeroExt32to64 x) -> (MOVLQZX x)
   127  
   128  (Slicemask <t> x) -> (SARQconst (NEGQ <t> x) [63])
   129  
   130  // Lowering truncation
   131  // Because we ignore high parts of registers, truncates are just copies.
   132  (Trunc16to8  x) -> x
   133  (Trunc32to8  x) -> x
   134  (Trunc32to16 x) -> x
   135  (Trunc64to8  x) -> x
   136  (Trunc64to16 x) -> x
   137  (Trunc64to32 x) -> x
   138  
   139  // Lowering float <-> int
   140  (Cvt32to32F x) -> (CVTSL2SS x)
   141  (Cvt32to64F x) -> (CVTSL2SD x)
   142  (Cvt64to32F x) -> (CVTSQ2SS x)
   143  (Cvt64to64F x) -> (CVTSQ2SD x)
   144  
   145  (Cvt32Fto32 x) -> (CVTTSS2SL x)
   146  (Cvt32Fto64 x) -> (CVTTSS2SQ x)
   147  (Cvt64Fto32 x) -> (CVTTSD2SL x)
   148  (Cvt64Fto64 x) -> (CVTTSD2SQ x)
   149  
   150  (Cvt32Fto64F x) -> (CVTSS2SD x)
   151  (Cvt64Fto32F x) -> (CVTSD2SS x)
   152  
   153  // Lowering shifts
   154  // Unsigned shifts need to return 0 if shift amount is >= width of shifted value.
   155  //   result = (arg << shift) & (shift >= argbits ? 0 : 0xffffffffffffffff)
   156  (Lsh64x64 <t> x y) -> (ANDQ (SHLQ <t> x y) (SBBQcarrymask <t> (CMPQconst y [64])))
   157  (Lsh64x32 <t> x y) -> (ANDQ (SHLQ <t> x y) (SBBQcarrymask <t> (CMPLconst y [64])))
   158  (Lsh64x16 <t> x y) -> (ANDQ (SHLQ <t> x y) (SBBQcarrymask <t> (CMPWconst y [64])))
   159  (Lsh64x8  <t> x y) -> (ANDQ (SHLQ <t> x y) (SBBQcarrymask <t> (CMPBconst y [64])))
   160  
   161  (Lsh32x64 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPQconst y [32])))
   162  (Lsh32x32 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPLconst y [32])))
   163  (Lsh32x16 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPWconst y [32])))
   164  (Lsh32x8  <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPBconst y [32])))
   165  
   166  (Lsh16x64 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPQconst y [32])))
   167  (Lsh16x32 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPLconst y [32])))
   168  (Lsh16x16 <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPWconst y [32])))
   169  (Lsh16x8  <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPBconst y [32])))
   170  
   171  (Lsh8x64 <t> x y)  -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPQconst y [32])))
   172  (Lsh8x32 <t> x y)  -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPLconst y [32])))
   173  (Lsh8x16 <t> x y)  -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPWconst y [32])))
   174  (Lsh8x8  <t> x y)  -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMPBconst y [32])))
   175  
   176  (Rsh64Ux64 <t> x y) -> (ANDQ (SHRQ <t> x y) (SBBQcarrymask <t> (CMPQconst y [64])))
   177  (Rsh64Ux32 <t> x y) -> (ANDQ (SHRQ <t> x y) (SBBQcarrymask <t> (CMPLconst y [64])))
   178  (Rsh64Ux16 <t> x y) -> (ANDQ (SHRQ <t> x y) (SBBQcarrymask <t> (CMPWconst y [64])))
   179  (Rsh64Ux8  <t> x y) -> (ANDQ (SHRQ <t> x y) (SBBQcarrymask <t> (CMPBconst y [64])))
   180  
   181  (Rsh32Ux64 <t> x y) -> (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMPQconst y [32])))
   182  (Rsh32Ux32 <t> x y) -> (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMPLconst y [32])))
   183  (Rsh32Ux16 <t> x y) -> (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMPWconst y [32])))
   184  (Rsh32Ux8  <t> x y) -> (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMPBconst y [32])))
   185  
   186  (Rsh16Ux64 <t> x y) -> (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMPQconst y [16])))
   187  (Rsh16Ux32 <t> x y) -> (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMPLconst y [16])))
   188  (Rsh16Ux16 <t> x y) -> (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMPWconst y [16])))
   189  (Rsh16Ux8  <t> x y) -> (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMPBconst y [16])))
   190  
   191  (Rsh8Ux64 <t> x y)  -> (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMPQconst y [8])))
   192  (Rsh8Ux32 <t> x y)  -> (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMPLconst y [8])))
   193  (Rsh8Ux16 <t> x y)  -> (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMPWconst y [8])))
   194  (Rsh8Ux8  <t> x y)  -> (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMPBconst y [8])))
   195  
   196  // Signed right shift needs to return 0/-1 if shift amount is >= width of shifted value.
   197  // We implement this by setting the shift value to -1 (all ones) if the shift value is >= width.
   198  (Rsh64x64 <t> x y) -> (SARQ <t> x (ORQ <y.Type> y (NOTQ <y.Type> (SBBQcarrymask <y.Type> (CMPQconst y [64])))))
   199  (Rsh64x32 <t> x y) -> (SARQ <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPLconst y [64])))))
   200  (Rsh64x16 <t> x y) -> (SARQ <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPWconst y [64])))))
   201  (Rsh64x8  <t> x y) -> (SARQ <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPBconst y [64])))))
   202  
   203  (Rsh32x64 <t> x y) -> (SARL <t> x (ORQ <y.Type> y (NOTQ <y.Type> (SBBQcarrymask <y.Type> (CMPQconst y [32])))))
   204  (Rsh32x32 <t> x y) -> (SARL <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPLconst y [32])))))
   205  (Rsh32x16 <t> x y) -> (SARL <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPWconst y [32])))))
   206  (Rsh32x8  <t> x y) -> (SARL <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPBconst y [32])))))
   207  
   208  (Rsh16x64 <t> x y) -> (SARW <t> x (ORQ <y.Type> y (NOTQ <y.Type> (SBBQcarrymask <y.Type> (CMPQconst y [16])))))
   209  (Rsh16x32 <t> x y) -> (SARW <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPLconst y [16])))))
   210  (Rsh16x16 <t> x y) -> (SARW <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPWconst y [16])))))
   211  (Rsh16x8  <t> x y) -> (SARW <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPBconst y [16])))))
   212  
   213  (Rsh8x64 <t> x y)  -> (SARB <t> x (ORQ <y.Type> y (NOTQ <y.Type> (SBBQcarrymask <y.Type> (CMPQconst y [8])))))
   214  (Rsh8x32 <t> x y)  -> (SARB <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPLconst y [8])))))
   215  (Rsh8x16 <t> x y)  -> (SARB <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPWconst y [8])))))
   216  (Rsh8x8  <t> x y)  -> (SARB <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMPBconst y [8])))))
   217  
   218  // Lowering comparisons
   219  (Less64  x y) -> (SETL (CMPQ x y))
   220  (Less32  x y) -> (SETL (CMPL x y))
   221  (Less16  x y) -> (SETL (CMPW x y))
   222  (Less8   x y) -> (SETL (CMPB x y))
   223  (Less64U x y) -> (SETB (CMPQ x y))
   224  (Less32U x y) -> (SETB (CMPL x y))
   225  (Less16U x y) -> (SETB (CMPW x y))
   226  (Less8U  x y) -> (SETB (CMPB x y))
   227  // Use SETGF with reversed operands to dodge NaN case
   228  (Less64F x y) -> (SETGF (UCOMISD y x))
   229  (Less32F x y) -> (SETGF (UCOMISS y x))
   230  
   231  (Leq64  x y) -> (SETLE (CMPQ x y))
   232  (Leq32  x y) -> (SETLE (CMPL x y))
   233  (Leq16  x y) -> (SETLE (CMPW x y))
   234  (Leq8   x y) -> (SETLE (CMPB x y))
   235  (Leq64U x y) -> (SETBE (CMPQ x y))
   236  (Leq32U x y) -> (SETBE (CMPL x y))
   237  (Leq16U x y) -> (SETBE (CMPW x y))
   238  (Leq8U  x y) -> (SETBE (CMPB x y))
   239  // Use SETGEF with reversed operands to dodge NaN case
   240  (Leq64F x y) -> (SETGEF (UCOMISD y x))
   241  (Leq32F x y) -> (SETGEF (UCOMISS y x))
   242  
   243  (Greater64  x y) -> (SETG (CMPQ x y))
   244  (Greater32  x y) -> (SETG (CMPL x y))
   245  (Greater16  x y) -> (SETG (CMPW x y))
   246  (Greater8   x y) -> (SETG (CMPB x y))
   247  (Greater64U x y) -> (SETA (CMPQ x y))
   248  (Greater32U x y) -> (SETA (CMPL x y))
   249  (Greater16U x y) -> (SETA (CMPW x y))
   250  (Greater8U  x y) -> (SETA (CMPB x y))
   251  // Note Go assembler gets UCOMISx operand order wrong, but it is right here
   252  // Bug is accommodated at generation of assembly language.
   253  (Greater64F x y) -> (SETGF (UCOMISD x y))
   254  (Greater32F x y) -> (SETGF (UCOMISS x y))
   255  
   256  (Geq64  x y) -> (SETGE (CMPQ x y))
   257  (Geq32  x y) -> (SETGE (CMPL x y))
   258  (Geq16  x y) -> (SETGE (CMPW x y))
   259  (Geq8   x y) -> (SETGE (CMPB x y))
   260  (Geq64U x y) -> (SETAE (CMPQ x y))
   261  (Geq32U x y) -> (SETAE (CMPL x y))
   262  (Geq16U x y) -> (SETAE (CMPW x y))
   263  (Geq8U  x y) -> (SETAE (CMPB x y))
   264  // Note Go assembler gets UCOMISx operand order wrong, but it is right here
   265  // Bug is accommodated at generation of assembly language.
   266  (Geq64F x y) -> (SETGEF (UCOMISD x y))
   267  (Geq32F x y) -> (SETGEF (UCOMISS x y))
   268  
   269  (Eq64  x y) -> (SETEQ (CMPQ x y))
   270  (Eq32  x y) -> (SETEQ (CMPL x y))
   271  (Eq16  x y) -> (SETEQ (CMPW x y))
   272  (Eq8   x y) -> (SETEQ (CMPB x y))
   273  (EqB   x y) -> (SETEQ (CMPB x y))
   274  (EqPtr x y) && config.PtrSize == 8 -> (SETEQ (CMPQ x y))
   275  (EqPtr x y) && config.PtrSize == 4 -> (SETEQ (CMPL x y))
   276  (Eq64F x y) -> (SETEQF (UCOMISD x y))
   277  (Eq32F x y) -> (SETEQF (UCOMISS x y))
   278  
   279  (Neq64  x y) -> (SETNE (CMPQ x y))
   280  (Neq32  x y) -> (SETNE (CMPL x y))
   281  (Neq16  x y) -> (SETNE (CMPW x y))
   282  (Neq8   x y) -> (SETNE (CMPB x y))
   283  (NeqB   x y) -> (SETNE (CMPB x y))
   284  (NeqPtr x y) && config.PtrSize == 8 -> (SETNE (CMPQ x y))
   285  (NeqPtr x y) && config.PtrSize == 4 -> (SETNE (CMPL x y))
   286  (Neq64F x y) -> (SETNEF (UCOMISD x y))
   287  (Neq32F x y) -> (SETNEF (UCOMISS x y))
   288  
   289  (Int64Hi x) -> (SHRQconst [32] x) // needed for amd64p32
   290  
   291  // Lowering loads
   292  (Load <t> ptr mem) && (is64BitInt(t) || isPtr(t) && config.PtrSize == 8) -> (MOVQload ptr mem)
   293  (Load <t> ptr mem) && (is32BitInt(t) || isPtr(t) && config.PtrSize == 4) -> (MOVLload ptr mem)
   294  (Load <t> ptr mem) && is16BitInt(t) -> (MOVWload ptr mem)
   295  (Load <t> ptr mem) && (t.IsBoolean() || is8BitInt(t)) -> (MOVBload ptr mem)
   296  (Load <t> ptr mem) && is32BitFloat(t) -> (MOVSSload ptr mem)
   297  (Load <t> ptr mem) && is64BitFloat(t) -> (MOVSDload ptr mem)
   298  
   299  // Lowering stores
   300  // These more-specific FP versions of Store pattern should come first.
   301  (Store [8] ptr val mem) && is64BitFloat(val.Type) -> (MOVSDstore ptr val mem)
   302  (Store [4] ptr val mem) && is32BitFloat(val.Type) -> (MOVSSstore ptr val mem)
   303  
   304  (Store [8] ptr val mem) -> (MOVQstore ptr val mem)
   305  (Store [4] ptr val mem) -> (MOVLstore ptr val mem)
   306  (Store [2] ptr val mem) -> (MOVWstore ptr val mem)
   307  (Store [1] ptr val mem) -> (MOVBstore ptr val mem)
   308  
   309  // Lowering moves
   310  (Move [s] _ _ mem) && SizeAndAlign(s).Size() == 0 -> mem
   311  (Move [s] dst src mem) && SizeAndAlign(s).Size() == 1 -> (MOVBstore dst (MOVBload src mem) mem)
   312  (Move [s] dst src mem) && SizeAndAlign(s).Size() == 2 -> (MOVWstore dst (MOVWload src mem) mem)
   313  (Move [s] dst src mem) && SizeAndAlign(s).Size() == 4 -> (MOVLstore dst (MOVLload src mem) mem)
   314  (Move [s] dst src mem) && SizeAndAlign(s).Size() == 8 -> (MOVQstore dst (MOVQload src mem) mem)
   315  (Move [s] dst src mem) && SizeAndAlign(s).Size() == 16 -> (MOVOstore dst (MOVOload src mem) mem)
   316  (Move [s] dst src mem) && SizeAndAlign(s).Size() == 3 ->
   317  	(MOVBstore [2] dst (MOVBload [2] src mem)
   318  		(MOVWstore dst (MOVWload src mem) mem))
   319  (Move [s] dst src mem) && SizeAndAlign(s).Size() == 5 ->
   320  	(MOVBstore [4] dst (MOVBload [4] src mem)
   321  		(MOVLstore dst (MOVLload src mem) mem))
   322  (Move [s] dst src mem) && SizeAndAlign(s).Size() == 6 ->
   323  	(MOVWstore [4] dst (MOVWload [4] src mem)
   324  		(MOVLstore dst (MOVLload src mem) mem))
   325  (Move [s] dst src mem) && SizeAndAlign(s).Size() == 7 ->
   326  	(MOVLstore [3] dst (MOVLload [3] src mem)
   327  		(MOVLstore dst (MOVLload src mem) mem))
   328  (Move [s] dst src mem) && SizeAndAlign(s).Size() > 8 && SizeAndAlign(s).Size() < 16 ->
   329  	(MOVQstore [SizeAndAlign(s).Size()-8] dst (MOVQload [SizeAndAlign(s).Size()-8] src mem)
   330  		(MOVQstore dst (MOVQload src mem) mem))
   331  
   332  // Adjust moves to be a multiple of 16 bytes.
   333  (Move [s] dst src mem)
   334  	&& SizeAndAlign(s).Size() > 16 && SizeAndAlign(s).Size()%16 != 0 && SizeAndAlign(s).Size()%16 <= 8 ->
   335  	(Move [SizeAndAlign(s).Size()-SizeAndAlign(s).Size()%16]
   336  		(OffPtr <dst.Type> dst [SizeAndAlign(s).Size()%16])
   337  		(OffPtr <src.Type> src [SizeAndAlign(s).Size()%16])
   338  		(MOVQstore dst (MOVQload src mem) mem))
   339  (Move [s] dst src mem)
   340  	&& SizeAndAlign(s).Size() > 16 && SizeAndAlign(s).Size()%16 != 0 && SizeAndAlign(s).Size()%16 > 8 ->
   341  	(Move [SizeAndAlign(s).Size()-SizeAndAlign(s).Size()%16]
   342  		(OffPtr <dst.Type> dst [SizeAndAlign(s).Size()%16])
   343  		(OffPtr <src.Type> src [SizeAndAlign(s).Size()%16])
   344  		(MOVOstore dst (MOVOload src mem) mem))
   345  
   346  // Medium copying uses a duff device.
   347  (Move [s] dst src mem)
   348  	&& SizeAndAlign(s).Size() >= 32 && SizeAndAlign(s).Size() <= 16*64 && SizeAndAlign(s).Size()%16 == 0
   349  	&& !config.noDuffDevice ->
   350  	(DUFFCOPY [14*(64-SizeAndAlign(s).Size()/16)] dst src mem)
   351  // 14 and 64 are magic constants.  14 is the number of bytes to encode:
   352  //	MOVUPS	(SI), X0
   353  //	ADDQ	$16, SI
   354  //	MOVUPS	X0, (DI)
   355  //	ADDQ	$16, DI
   356  // and 64 is the number of such blocks. See src/runtime/duff_amd64.s:duffcopy.
   357  
   358  // Large copying uses REP MOVSQ.
   359  (Move [s] dst src mem) && (SizeAndAlign(s).Size() > 16*64 || config.noDuffDevice) && SizeAndAlign(s).Size()%8 == 0 ->
   360  	(REPMOVSQ dst src (MOVQconst [SizeAndAlign(s).Size()/8]) mem)
   361  
   362  // Lowering Zero instructions
   363  (Zero [s] _ mem) && SizeAndAlign(s).Size() == 0 -> mem
   364  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 1 -> (MOVBstoreconst [0] destptr mem)
   365  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 2 -> (MOVWstoreconst [0] destptr mem)
   366  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 4 -> (MOVLstoreconst [0] destptr mem)
   367  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 8 -> (MOVQstoreconst [0] destptr mem)
   368  
   369  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 3 ->
   370  	(MOVBstoreconst [makeValAndOff(0,2)] destptr
   371  		(MOVWstoreconst [0] destptr mem))
   372  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 5 ->
   373  	(MOVBstoreconst [makeValAndOff(0,4)] destptr
   374  		(MOVLstoreconst [0] destptr mem))
   375  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 6 ->
   376  	(MOVWstoreconst [makeValAndOff(0,4)] destptr
   377  		(MOVLstoreconst [0] destptr mem))
   378  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 7 ->
   379  	(MOVLstoreconst [makeValAndOff(0,3)] destptr
   380  		(MOVLstoreconst [0] destptr mem))
   381  
   382  // Strip off any fractional word zeroing.
   383  (Zero [s] destptr mem) && SizeAndAlign(s).Size()%8 != 0 && SizeAndAlign(s).Size() > 8 ->
   384  	(Zero [SizeAndAlign(s).Size()-SizeAndAlign(s).Size()%8] (OffPtr <destptr.Type> destptr [SizeAndAlign(s).Size()%8])
   385  		(MOVQstoreconst [0] destptr mem))
   386  
   387  // Zero small numbers of words directly.
   388  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 16 ->
   389  	(MOVQstoreconst [makeValAndOff(0,8)] destptr
   390  		(MOVQstoreconst [0] destptr mem))
   391  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 24 ->
   392  	(MOVQstoreconst [makeValAndOff(0,16)] destptr
   393  		(MOVQstoreconst [makeValAndOff(0,8)] destptr
   394  			(MOVQstoreconst [0] destptr mem)))
   395  (Zero [s] destptr mem) && SizeAndAlign(s).Size() == 32 ->
   396  	(MOVQstoreconst [makeValAndOff(0,24)] destptr
   397  		(MOVQstoreconst [makeValAndOff(0,16)] destptr
   398  			(MOVQstoreconst [makeValAndOff(0,8)] destptr
   399  				(MOVQstoreconst [0] destptr mem))))
   400  
   401  // Medium zeroing uses a duff device.
   402  (Zero [s] destptr mem)
   403  	&& SizeAndAlign(s).Size() <= 1024 && SizeAndAlign(s).Size()%8 == 0 && SizeAndAlign(s).Size()%16 != 0
   404  	&& !config.noDuffDevice ->
   405  	(Zero [SizeAndAlign(s).Size()-8] (OffPtr <destptr.Type> [8] destptr) (MOVQstore destptr (MOVQconst [0]) mem))
   406  (Zero [s] destptr mem)
   407  	&& SizeAndAlign(s).Size() <= 1024 && SizeAndAlign(s).Size()%16 == 0 && !config.noDuffDevice ->
   408  	(DUFFZERO [SizeAndAlign(s).Size()] destptr (MOVOconst [0]) mem)
   409  
   410  // Large zeroing uses REP STOSQ.
   411  (Zero [s] destptr mem)
   412  	&& (SizeAndAlign(s).Size() > 1024 || (config.noDuffDevice && SizeAndAlign(s).Size() > 32))
   413  	&& SizeAndAlign(s).Size()%8 == 0 ->
   414  	(REPSTOSQ destptr (MOVQconst [SizeAndAlign(s).Size()/8]) (MOVQconst [0]) mem)
   415  
   416  // Lowering constants
   417  (Const8   [val]) -> (MOVLconst [val])
   418  (Const16  [val]) -> (MOVLconst [val])
   419  (Const32  [val]) -> (MOVLconst [val])
   420  (Const64  [val]) -> (MOVQconst [val])
   421  (Const32F [val]) -> (MOVSSconst [val])
   422  (Const64F [val]) -> (MOVSDconst [val])
   423  (ConstNil) && config.PtrSize == 8 -> (MOVQconst [0])
   424  (ConstNil) && config.PtrSize == 4 -> (MOVLconst [0])
   425  (ConstBool [b]) -> (MOVLconst [b])
   426  
   427  // Lowering calls
   428  (StaticCall [argwid] {target} mem) -> (CALLstatic [argwid] {target} mem)
   429  (ClosureCall [argwid] entry closure mem) -> (CALLclosure [argwid] entry closure mem)
   430  (DeferCall [argwid] mem) -> (CALLdefer [argwid] mem)
   431  (GoCall [argwid] mem) -> (CALLgo [argwid] mem)
   432  (InterCall [argwid] entry mem) -> (CALLinter [argwid] entry mem)
   433  
   434  // Miscellaneous
   435  (Convert <t> x mem) && config.PtrSize == 8 -> (MOVQconvert <t> x mem)
   436  (Convert <t> x mem) && config.PtrSize == 4 -> (MOVLconvert <t> x mem)
   437  (IsNonNil p) && config.PtrSize == 8 -> (SETNE (TESTQ p p))
   438  (IsNonNil p) && config.PtrSize == 4 -> (SETNE (TESTL p p))
   439  (IsInBounds idx len) -> (SETB (CMPQ idx len))
   440  (IsSliceInBounds idx len) -> (SETBE (CMPQ idx len))
   441  (NilCheck ptr mem) -> (LoweredNilCheck ptr mem)
   442  (GetG mem) -> (LoweredGetG mem)
   443  (GetClosurePtr) -> (LoweredGetClosurePtr)
   444  (Addr {sym} base) && config.PtrSize == 8 -> (LEAQ {sym} base)
   445  (Addr {sym} base) && config.PtrSize == 4 -> (LEAL {sym} base)
   446  
   447  // block rewrites
   448  (If (SETL  cmp) yes no) -> (LT  cmp yes no)
   449  (If (SETLE cmp) yes no) -> (LE  cmp yes no)
   450  (If (SETG  cmp) yes no) -> (GT  cmp yes no)
   451  (If (SETGE cmp) yes no) -> (GE  cmp yes no)
   452  (If (SETEQ cmp) yes no) -> (EQ  cmp yes no)
   453  (If (SETNE cmp) yes no) -> (NE  cmp yes no)
   454  (If (SETB  cmp) yes no) -> (ULT cmp yes no)
   455  (If (SETBE cmp) yes no) -> (ULE cmp yes no)
   456  (If (SETA  cmp) yes no) -> (UGT cmp yes no)
   457  (If (SETAE cmp) yes no) -> (UGE cmp yes no)
   458  
   459  // Special case for floating point - LF/LEF not generated
   460  (If (SETGF  cmp) yes no) -> (UGT  cmp yes no)
   461  (If (SETGEF cmp) yes no) -> (UGE  cmp yes no)
   462  (If (SETEQF cmp) yes no) -> (EQF  cmp yes no)
   463  (If (SETNEF cmp) yes no) -> (NEF  cmp yes no)
   464  
   465  (If cond yes no) -> (NE (TESTB cond cond) yes no)
   466  
   467  // Atomic loads.  Other than preserving their ordering with respect to other loads, nothing special here.
   468  (AtomicLoad32 ptr mem) -> (MOVLatomicload ptr mem)
   469  (AtomicLoad64 ptr mem) -> (MOVQatomicload ptr mem)
   470  (AtomicLoadPtr ptr mem) && config.PtrSize == 8 -> (MOVQatomicload ptr mem)
   471  (AtomicLoadPtr ptr mem) && config.PtrSize == 4 -> (MOVLatomicload ptr mem)
   472  
   473  // Atomic stores.  We use XCHG to prevent the hardware reordering a subsequent load.
   474  // TODO: most runtime uses of atomic stores don't need that property.  Use normal stores for those?
   475  (AtomicStore32 ptr val mem) -> (Select1 (XCHGL <MakeTuple(config.Frontend().TypeUInt32(),TypeMem)> val ptr mem))
   476  (AtomicStore64 ptr val mem) -> (Select1 (XCHGQ <MakeTuple(config.Frontend().TypeUInt64(),TypeMem)> val ptr mem))
   477  (AtomicStorePtrNoWB ptr val mem) && config.PtrSize == 8 -> (Select1 (XCHGQ <MakeTuple(config.Frontend().TypeBytePtr(),TypeMem)> val ptr mem))
   478  (AtomicStorePtrNoWB ptr val mem) && config.PtrSize == 4 -> (Select1 (XCHGL <MakeTuple(config.Frontend().TypeBytePtr(),TypeMem)> val ptr mem))
   479  
   480  // Atomic exchanges.
   481  (AtomicExchange32 ptr val mem) -> (XCHGL val ptr mem)
   482  (AtomicExchange64 ptr val mem) -> (XCHGQ val ptr mem)
   483  
   484  // Atomic adds.
   485  (AtomicAdd32 ptr val mem) -> (AddTupleFirst32 (XADDLlock val ptr mem) val)
   486  (AtomicAdd64 ptr val mem) -> (AddTupleFirst64 (XADDQlock val ptr mem) val)
   487  (Select0 <t> (AddTupleFirst32 tuple val)) -> (ADDL val (Select0 <t> tuple))
   488  (Select1     (AddTupleFirst32 tuple _  )) -> (Select1 tuple)
   489  (Select0 <t> (AddTupleFirst64 tuple val)) -> (ADDQ val (Select0 <t> tuple))
   490  (Select1     (AddTupleFirst64 tuple _  )) -> (Select1 tuple)
   491  
   492  // Atomic compare and swap.
   493  (AtomicCompareAndSwap32 ptr old new_ mem) -> (CMPXCHGLlock ptr old new_ mem)
   494  (AtomicCompareAndSwap64 ptr old new_ mem) -> (CMPXCHGQlock ptr old new_ mem)
   495  
   496  // Atomic memory updates.
   497  (AtomicAnd8 ptr val mem) -> (ANDBlock ptr val mem)
   498  (AtomicOr8 ptr val mem) -> (ORBlock ptr val mem)
   499  
   500  // ***************************
   501  // Above: lowering rules
   502  // Below: optimizations
   503  // ***************************
   504  // TODO: Should the optimizations be a separate pass?
   505  
   506  // Fold boolean tests into blocks
   507  (NE (TESTB (SETL  cmp) (SETL  cmp)) yes no) -> (LT  cmp yes no)
   508  (NE (TESTB (SETLE cmp) (SETLE cmp)) yes no) -> (LE  cmp yes no)
   509  (NE (TESTB (SETG  cmp) (SETG  cmp)) yes no) -> (GT  cmp yes no)
   510  (NE (TESTB (SETGE cmp) (SETGE cmp)) yes no) -> (GE  cmp yes no)
   511  (NE (TESTB (SETEQ cmp) (SETEQ cmp)) yes no) -> (EQ  cmp yes no)
   512  (NE (TESTB (SETNE cmp) (SETNE cmp)) yes no) -> (NE  cmp yes no)
   513  (NE (TESTB (SETB  cmp) (SETB  cmp)) yes no) -> (ULT cmp yes no)
   514  (NE (TESTB (SETBE cmp) (SETBE cmp)) yes no) -> (ULE cmp yes no)
   515  (NE (TESTB (SETA  cmp) (SETA  cmp)) yes no) -> (UGT cmp yes no)
   516  (NE (TESTB (SETAE cmp) (SETAE cmp)) yes no) -> (UGE cmp yes no)
   517  
   518  // Special case for floating point - LF/LEF not generated
   519  (NE (TESTB (SETGF  cmp) (SETGF  cmp)) yes no) -> (UGT  cmp yes no)
   520  (NE (TESTB (SETGEF cmp) (SETGEF cmp)) yes no) -> (UGE  cmp yes no)
   521  (NE (TESTB (SETEQF cmp) (SETEQF cmp)) yes no) -> (EQF  cmp yes no)
   522  (NE (TESTB (SETNEF cmp) (SETNEF cmp)) yes no) -> (NEF  cmp yes no)
   523  
   524  // Disabled because it interferes with the pattern match above and makes worse code.
   525  // (SETNEF x) -> (ORQ (SETNE <config.Frontend().TypeInt8()> x) (SETNAN <config.Frontend().TypeInt8()> x))
   526  // (SETEQF x) -> (ANDQ (SETEQ <config.Frontend().TypeInt8()> x) (SETORD <config.Frontend().TypeInt8()> x))
   527  
   528  // fold constants into instructions
   529  (ADDQ x (MOVQconst [c])) && is32Bit(c) -> (ADDQconst [c] x)
   530  (ADDQ (MOVQconst [c]) x) && is32Bit(c) -> (ADDQconst [c] x)
   531  (ADDL x (MOVLconst [c])) -> (ADDLconst [c] x)
   532  (ADDL (MOVLconst [c]) x) -> (ADDLconst [c] x)
   533  
   534  (SUBQ x (MOVQconst [c])) && is32Bit(c) -> (SUBQconst x [c])
   535  (SUBQ (MOVQconst [c]) x) && is32Bit(c) -> (NEGQ (SUBQconst <v.Type> x [c]))
   536  (SUBL x (MOVLconst [c])) -> (SUBLconst x [c])
   537  (SUBL (MOVLconst [c]) x) -> (NEGL (SUBLconst <v.Type> x [c]))
   538  
   539  (MULQ x (MOVQconst [c])) && is32Bit(c) -> (MULQconst [c] x)
   540  (MULQ (MOVQconst [c]) x) && is32Bit(c) -> (MULQconst [c] x)
   541  (MULL x (MOVLconst [c])) -> (MULLconst [c] x)
   542  (MULL (MOVLconst [c]) x) -> (MULLconst [c] x)
   543  
   544  (ANDQ x (MOVQconst [c])) && is32Bit(c) -> (ANDQconst [c] x)
   545  (ANDQ (MOVQconst [c]) x) && is32Bit(c) -> (ANDQconst [c] x)
   546  (ANDL x (MOVLconst [c])) -> (ANDLconst [c] x)
   547  (ANDL (MOVLconst [c]) x) -> (ANDLconst [c] x)
   548  
   549  (ANDLconst [c] (ANDLconst [d] x)) -> (ANDLconst [c & d] x)
   550  (ANDQconst [c] (ANDQconst [d] x)) -> (ANDQconst [c & d] x)
   551  
   552  (XORLconst [c] (XORLconst [d] x)) -> (XORLconst [c ^ d] x)
   553  (XORQconst [c] (XORQconst [d] x)) -> (XORQconst [c ^ d] x)
   554  
   555  (MULLconst [c] (MULLconst [d] x)) -> (MULLconst [int64(int32(c * d))] x)
   556  (MULQconst [c] (MULQconst [d] x)) && is32Bit(c*d) -> (MULQconst [c * d] x)
   557  
   558  (ORQ x (MOVQconst [c])) && is32Bit(c) -> (ORQconst [c] x)
   559  (ORQ (MOVQconst [c]) x) && is32Bit(c) -> (ORQconst [c] x)
   560  (ORL x (MOVLconst [c])) -> (ORLconst [c] x)
   561  (ORL (MOVLconst [c]) x) -> (ORLconst [c] x)
   562  
   563  (XORQ x (MOVQconst [c])) && is32Bit(c) -> (XORQconst [c] x)
   564  (XORQ (MOVQconst [c]) x) && is32Bit(c) -> (XORQconst [c] x)
   565  (XORL x (MOVLconst [c])) -> (XORLconst [c] x)
   566  (XORL (MOVLconst [c]) x) -> (XORLconst [c] x)
   567  
   568  (SHLQ x (MOVQconst [c])) -> (SHLQconst [c&63] x)
   569  (SHLQ x (MOVLconst [c])) -> (SHLQconst [c&63] x)
   570  
   571  (SHLL x (MOVQconst [c])) -> (SHLLconst [c&31] x)
   572  (SHLL x (MOVLconst [c])) -> (SHLLconst [c&31] x)
   573  
   574  (SHRQ x (MOVQconst [c])) -> (SHRQconst [c&63] x)
   575  (SHRQ x (MOVLconst [c])) -> (SHRQconst [c&63] x)
   576  
   577  (SHRL x (MOVQconst [c])) -> (SHRLconst [c&31] x)
   578  (SHRL x (MOVLconst [c])) -> (SHRLconst [c&31] x)
   579  
   580  (SHRW x (MOVQconst [c])) && c&31 < 16 -> (SHRWconst [c&31] x)
   581  (SHRW x (MOVLconst [c])) && c&31 < 16 -> (SHRWconst [c&31] x)
   582  (SHRW _ (MOVQconst [c])) && c&31 >= 16 -> (MOVLconst [0])
   583  (SHRW _ (MOVLconst [c])) && c&31 >= 16 -> (MOVLconst [0])
   584  
   585  (SHRB x (MOVQconst [c])) && c&31 < 8 -> (SHRBconst [c&31] x)
   586  (SHRB x (MOVLconst [c])) && c&31 < 8 -> (SHRBconst [c&31] x)
   587  (SHRB _ (MOVQconst [c])) && c&31 >= 8 -> (MOVLconst [0])
   588  (SHRB _ (MOVLconst [c])) && c&31 >= 8 -> (MOVLconst [0])
   589  
   590  (SARQ x (MOVQconst [c])) -> (SARQconst [c&63] x)
   591  (SARQ x (MOVLconst [c])) -> (SARQconst [c&63] x)
   592  
   593  (SARL x (MOVQconst [c])) -> (SARLconst [c&31] x)
   594  (SARL x (MOVLconst [c])) -> (SARLconst [c&31] x)
   595  
   596  (SARW x (MOVQconst [c])) -> (SARWconst [min(c&31,15)] x)
   597  (SARW x (MOVLconst [c])) -> (SARWconst [min(c&31,15)] x)
   598  
   599  (SARB x (MOVQconst [c])) -> (SARBconst [min(c&31,7)] x)
   600  (SARB x (MOVLconst [c])) -> (SARBconst [min(c&31,7)] x)
   601  
   602  (SARL x (ANDLconst [31] y)) -> (SARL x y)
   603  (SARQ x (ANDQconst [63] y)) -> (SARQ x y)
   604  
   605  (SHLL x (ANDLconst [31] y)) -> (SHLL x y)
   606  (SHLQ x (ANDQconst [63] y)) -> (SHLQ x y)
   607  
   608  (SHRL x (ANDLconst [31] y)) -> (SHRL x y)
   609  (SHRQ x (ANDQconst [63] y)) -> (SHRQ x y)
   610  
   611  // Rotate instructions
   612  
   613  (ADDQ (SHLQconst x [c]) (SHRQconst x [64-c])) -> (ROLQconst x [   c])
   614  ( ORQ (SHLQconst x [c]) (SHRQconst x [64-c])) -> (ROLQconst x [   c])
   615  (XORQ (SHLQconst x [c]) (SHRQconst x [64-c])) -> (ROLQconst x [   c])
   616  (ADDQ (SHRQconst x [c]) (SHLQconst x [64-c])) -> (ROLQconst x [64-c])
   617  ( ORQ (SHRQconst x [c]) (SHLQconst x [64-c])) -> (ROLQconst x [64-c])
   618  (XORQ (SHRQconst x [c]) (SHLQconst x [64-c])) -> (ROLQconst x [64-c])
   619  
   620  (ADDL (SHLLconst x [c]) (SHRLconst x [32-c])) -> (ROLLconst x [   c])
   621  ( ORL (SHLLconst x [c]) (SHRLconst x [32-c])) -> (ROLLconst x [   c])
   622  (XORL (SHLLconst x [c]) (SHRLconst x [32-c])) -> (ROLLconst x [   c])
   623  (ADDL (SHRLconst x [c]) (SHLLconst x [32-c])) -> (ROLLconst x [32-c])
   624  ( ORL (SHRLconst x [c]) (SHLLconst x [32-c])) -> (ROLLconst x [32-c])
   625  (XORL (SHRLconst x [c]) (SHLLconst x [32-c])) -> (ROLLconst x [32-c])
   626  
   627  (ADDL <t> (SHLLconst x [c]) (SHRWconst x [16-c])) && c < 16 && t.Size() == 2 -> (ROLWconst x [   c])
   628  ( ORL <t> (SHLLconst x [c]) (SHRWconst x [16-c])) && c < 16 && t.Size() == 2 -> (ROLWconst x [   c])
   629  (XORL <t> (SHLLconst x [c]) (SHRWconst x [16-c])) && c < 16 && t.Size() == 2 -> (ROLWconst x [   c])
   630  (ADDL <t> (SHRWconst x [c]) (SHLLconst x [16-c])) && c > 0  && t.Size() == 2 -> (ROLWconst x [16-c])
   631  ( ORL <t> (SHRWconst x [c]) (SHLLconst x [16-c])) && c > 0  && t.Size() == 2 -> (ROLWconst x [16-c])
   632  (XORL <t> (SHRWconst x [c]) (SHLLconst x [16-c])) && c > 0  && t.Size() == 2 -> (ROLWconst x [16-c])
   633  
   634  (ADDL <t> (SHLLconst x [c]) (SHRBconst x [ 8-c])) && c < 8 && t.Size() == 1 -> (ROLBconst x [   c])
   635  ( ORL <t> (SHLLconst x [c]) (SHRBconst x [ 8-c])) && c < 8 && t.Size() == 1 -> (ROLBconst x [   c])
   636  (XORL <t> (SHLLconst x [c]) (SHRBconst x [ 8-c])) && c < 8 && t.Size() == 1 -> (ROLBconst x [   c])
   637  (ADDL <t> (SHRBconst x [c]) (SHLLconst x [ 8-c])) && c > 0 && t.Size() == 1 -> (ROLBconst x [ 8-c])
   638  ( ORL <t> (SHRBconst x [c]) (SHLLconst x [ 8-c])) && c > 0 && t.Size() == 1 -> (ROLBconst x [ 8-c])
   639  (XORL <t> (SHRBconst x [c]) (SHLLconst x [ 8-c])) && c > 0 && t.Size() == 1 -> (ROLBconst x [ 8-c])
   640  
   641  (ROLQconst [c] (ROLQconst [d] x)) -> (ROLQconst [(c+d)&63] x)
   642  (ROLLconst [c] (ROLLconst [d] x)) -> (ROLLconst [(c+d)&31] x)
   643  (ROLWconst [c] (ROLWconst [d] x)) -> (ROLWconst [(c+d)&15] x)
   644  (ROLBconst [c] (ROLBconst [d] x)) -> (ROLBconst [(c+d)& 7] x)
   645  
   646  // TODO: non-constant rotates if shift amount is known to be bounded (shift & 63 or something).
   647  
   648  // Constant shift simplifications
   649  
   650  (SHLQconst x [0]) -> x
   651  (SHRQconst x [0]) -> x
   652  (SARQconst x [0]) -> x
   653  
   654  (SHLLconst x [0]) -> x
   655  (SHRLconst x [0]) -> x
   656  (SARLconst x [0]) -> x
   657  
   658  (SHRWconst x [0]) -> x
   659  (SARWconst x [0]) -> x
   660  
   661  (SHRBconst x [0]) -> x
   662  (SARBconst x [0]) -> x
   663  
   664  (ROLQconst x [0]) -> x
   665  (ROLLconst x [0]) -> x
   666  (ROLWconst x [0]) -> x
   667  (ROLBconst x [0]) -> x
   668  
   669  // Note: the word and byte shifts keep the low 5 bits (not the low 4 or 3 bits)
   670  // because the x86 instructions are defined to use all 5 bits of the shift even
   671  // for the small shifts. I don't think we'll ever generate a weird shift (e.g.
   672  // (SHRW x (MOVLconst [24])), but just in case.
   673  
   674  (CMPQ x (MOVQconst [c])) && is32Bit(c) -> (CMPQconst x [c])
   675  (CMPQ (MOVQconst [c]) x) && is32Bit(c) -> (InvertFlags (CMPQconst x [c]))
   676  (CMPL x (MOVLconst [c])) -> (CMPLconst x [c])
   677  (CMPL (MOVLconst [c]) x) -> (InvertFlags (CMPLconst x [c]))
   678  (CMPW x (MOVLconst [c])) -> (CMPWconst x [int64(int16(c))])
   679  (CMPW (MOVLconst [c]) x) -> (InvertFlags (CMPWconst x [int64(int16(c))]))
   680  (CMPB x (MOVLconst [c])) -> (CMPBconst x [int64(int8(c))])
   681  (CMPB (MOVLconst [c]) x) -> (InvertFlags (CMPBconst x [int64(int8(c))]))
   682  
   683  // Using MOVZX instead of AND is cheaper.
   684  (ANDLconst [0xFF] x) -> (MOVBQZX x)
   685  (ANDLconst [0xFFFF] x) -> (MOVWQZX x)
   686  (ANDQconst [0xFF] x) -> (MOVBQZX x)
   687  (ANDQconst [0xFFFF] x) -> (MOVWQZX x)
   688  (ANDQconst [0xFFFFFFFF] x) -> (MOVLQZX x)
   689  
   690  // strength reduction
   691  // Assumes that the following costs from https://gmplib.org/~tege/x86-timing.pdf:
   692  //    1 - addq, shlq, leaq, negq
   693  //    3 - imulq
   694  // This limits the rewrites to two instructions.
   695  // TODO: 27, 81
   696  (MULQconst [-1] x) -> (NEGQ x)
   697  (MULQconst [0] _) -> (MOVQconst [0])
   698  (MULQconst [1] x) -> x
   699  (MULQconst [3] x) -> (LEAQ2 x x)
   700  (MULQconst [5] x) -> (LEAQ4 x x)
   701  (MULQconst [7] x) -> (LEAQ8 (NEGQ <v.Type> x) x)
   702  (MULQconst [9] x) -> (LEAQ8 x x)
   703  (MULQconst [11] x) -> (LEAQ2 x (LEAQ4 <v.Type> x x))
   704  (MULQconst [13] x) -> (LEAQ4 x (LEAQ2 <v.Type> x x))
   705  (MULQconst [21] x) -> (LEAQ4 x (LEAQ4 <v.Type> x x))
   706  (MULQconst [25] x) -> (LEAQ8 x (LEAQ2 <v.Type> x x))
   707  (MULQconst [37] x) -> (LEAQ4 x (LEAQ8 <v.Type> x x))
   708  (MULQconst [41] x) -> (LEAQ8 x (LEAQ4 <v.Type> x x))
   709  (MULQconst [73] x) -> (LEAQ8 x (LEAQ8 <v.Type> x x))
   710  
   711  (MULQconst [c] x) && isPowerOfTwo(c) -> (SHLQconst [log2(c)] x)
   712  (MULQconst [c] x) && isPowerOfTwo(c+1) && c >= 15 -> (SUBQ (SHLQconst <v.Type> [log2(c+1)] x) x)
   713  (MULQconst [c] x) && isPowerOfTwo(c-1) && c >= 17 -> (LEAQ1 (SHLQconst <v.Type> [log2(c-1)] x) x)
   714  (MULQconst [c] x) && isPowerOfTwo(c-2) && c >= 34 -> (LEAQ2 (SHLQconst <v.Type> [log2(c-2)] x) x)
   715  (MULQconst [c] x) && isPowerOfTwo(c-4) && c >= 68 -> (LEAQ4 (SHLQconst <v.Type> [log2(c-4)] x) x)
   716  (MULQconst [c] x) && isPowerOfTwo(c-8) && c >= 136 -> (LEAQ8 (SHLQconst <v.Type> [log2(c-8)] x) x)
   717  (MULQconst [c] x) && c%3 == 0 && isPowerOfTwo(c/3)-> (SHLQconst [log2(c/3)] (LEAQ2 <v.Type> x x))
   718  (MULQconst [c] x) && c%5 == 0 && isPowerOfTwo(c/5)-> (SHLQconst [log2(c/5)] (LEAQ4 <v.Type> x x))
   719  (MULQconst [c] x) && c%9 == 0 && isPowerOfTwo(c/9)-> (SHLQconst [log2(c/9)] (LEAQ8 <v.Type> x x))
   720  
   721  // combine add/shift into LEAQ
   722  (ADDQ x (SHLQconst [3] y)) -> (LEAQ8 x y)
   723  (ADDQ x (SHLQconst [2] y)) -> (LEAQ4 x y)
   724  (ADDQ x (SHLQconst [1] y)) -> (LEAQ2 x y)
   725  (ADDQ x (ADDQ y y)) -> (LEAQ2 x y)
   726  (ADDQ x (ADDQ x y)) -> (LEAQ2 y x)
   727  (ADDQ x (ADDQ y x)) -> (LEAQ2 y x)
   728  
   729  // combine ADDQ/ADDQconst into LEAQ1
   730  (ADDQconst [c] (ADDQ x y)) -> (LEAQ1 [c] x y)
   731  (ADDQ (ADDQconst [c] x) y) -> (LEAQ1 [c] x y)
   732  (ADDQ x (ADDQconst [c] y)) -> (LEAQ1 [c] x y)
   733  
   734  // fold ADDQ into LEAQ
   735  (ADDQconst [c] (LEAQ [d] {s} x)) && is32Bit(c+d) -> (LEAQ [c+d] {s} x)
   736  (LEAQ [c] {s} (ADDQconst [d] x)) && is32Bit(c+d) -> (LEAQ [c+d] {s} x)
   737  (LEAQ [c] {s} (ADDQ x y)) && x.Op != OpSB && y.Op != OpSB -> (LEAQ1 [c] {s} x y)
   738  (ADDQ x (LEAQ [c] {s} y)) && x.Op != OpSB && y.Op != OpSB -> (LEAQ1 [c] {s} x y)
   739  (ADDQ (LEAQ [c] {s} x) y) && x.Op != OpSB && y.Op != OpSB -> (LEAQ1 [c] {s} x y)
   740  
   741  // fold ADDQconst into LEAQx
   742  (ADDQconst [c] (LEAQ1 [d] {s} x y)) && is32Bit(c+d) -> (LEAQ1 [c+d] {s} x y)
   743  (ADDQconst [c] (LEAQ2 [d] {s} x y)) && is32Bit(c+d) -> (LEAQ2 [c+d] {s} x y)
   744  (ADDQconst [c] (LEAQ4 [d] {s} x y)) && is32Bit(c+d) -> (LEAQ4 [c+d] {s} x y)
   745  (ADDQconst [c] (LEAQ8 [d] {s} x y)) && is32Bit(c+d) -> (LEAQ8 [c+d] {s} x y)
   746  (LEAQ1 [c] {s} (ADDQconst [d] x) y) && is32Bit(c+d)   && x.Op != OpSB -> (LEAQ1 [c+d] {s} x y)
   747  (LEAQ1 [c] {s} x (ADDQconst [d] y)) && is32Bit(c+d)   && y.Op != OpSB -> (LEAQ1 [c+d] {s} x y)
   748  (LEAQ2 [c] {s} (ADDQconst [d] x) y) && is32Bit(c+d)   && x.Op != OpSB -> (LEAQ2 [c+d] {s} x y)
   749  (LEAQ2 [c] {s} x (ADDQconst [d] y)) && is32Bit(c+2*d) && y.Op != OpSB -> (LEAQ2 [c+2*d] {s} x y)
   750  (LEAQ4 [c] {s} (ADDQconst [d] x) y) && is32Bit(c+d)   && x.Op != OpSB -> (LEAQ4 [c+d] {s} x y)
   751  (LEAQ4 [c] {s} x (ADDQconst [d] y)) && is32Bit(c+4*d) && y.Op != OpSB -> (LEAQ4 [c+4*d] {s} x y)
   752  (LEAQ8 [c] {s} (ADDQconst [d] x) y) && is32Bit(c+d)   && x.Op != OpSB -> (LEAQ8 [c+d] {s} x y)
   753  (LEAQ8 [c] {s} x (ADDQconst [d] y)) && is32Bit(c+8*d) && y.Op != OpSB -> (LEAQ8 [c+8*d] {s} x y)
   754  
   755  // fold shifts into LEAQx
   756  (LEAQ1 [c] {s} x (SHLQconst [1] y)) -> (LEAQ2 [c] {s} x y)
   757  (LEAQ1 [c] {s} (SHLQconst [1] x) y) -> (LEAQ2 [c] {s} y x)
   758  (LEAQ1 [c] {s} x (SHLQconst [2] y)) -> (LEAQ4 [c] {s} x y)
   759  (LEAQ1 [c] {s} (SHLQconst [2] x) y) -> (LEAQ4 [c] {s} y x)
   760  (LEAQ1 [c] {s} x (SHLQconst [3] y)) -> (LEAQ8 [c] {s} x y)
   761  (LEAQ1 [c] {s} (SHLQconst [3] x) y) -> (LEAQ8 [c] {s} y x)
   762  
   763  (LEAQ2 [c] {s} x (SHLQconst [1] y)) -> (LEAQ4 [c] {s} x y)
   764  (LEAQ2 [c] {s} x (SHLQconst [2] y)) -> (LEAQ8 [c] {s} x y)
   765  (LEAQ4 [c] {s} x (SHLQconst [1] y)) -> (LEAQ8 [c] {s} x y)
   766  
   767  // reverse ordering of compare instruction
   768  (SETL (InvertFlags x)) -> (SETG x)
   769  (SETG (InvertFlags x)) -> (SETL x)
   770  (SETB (InvertFlags x)) -> (SETA x)
   771  (SETA (InvertFlags x)) -> (SETB x)
   772  (SETLE (InvertFlags x)) -> (SETGE x)
   773  (SETGE (InvertFlags x)) -> (SETLE x)
   774  (SETBE (InvertFlags x)) -> (SETAE x)
   775  (SETAE (InvertFlags x)) -> (SETBE x)
   776  (SETEQ (InvertFlags x)) -> (SETEQ x)
   777  (SETNE (InvertFlags x)) -> (SETNE x)
   778  
   779  // sign extended loads
   780  // Note: The combined instruction must end up in the same block
   781  // as the original load. If not, we end up making a value with
   782  // memory type live in two different blocks, which can lead to
   783  // multiple memory values alive simultaneously.
   784  // Make sure we don't combine these ops if the load has another use.
   785  // This prevents a single load from being split into multiple loads
   786  // which then might return different values.  See test/atomicload.go.
   787  (MOVBQSX x:(MOVBload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
   788  (MOVBQSX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
   789  (MOVBQSX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
   790  (MOVBQSX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBQSXload <v.Type> [off] {sym} ptr mem)
   791  (MOVBQZX x:(MOVBload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
   792  (MOVBQZX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
   793  (MOVBQZX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
   794  (MOVBQZX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBload <v.Type> [off] {sym} ptr mem)
   795  (MOVWQSX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWQSXload <v.Type> [off] {sym} ptr mem)
   796  (MOVWQSX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWQSXload <v.Type> [off] {sym} ptr mem)
   797  (MOVWQSX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWQSXload <v.Type> [off] {sym} ptr mem)
   798  (MOVWQZX x:(MOVWload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWload <v.Type> [off] {sym} ptr mem)
   799  (MOVWQZX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWload <v.Type> [off] {sym} ptr mem)
   800  (MOVWQZX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWload <v.Type> [off] {sym} ptr mem)
   801  (MOVLQSX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLQSXload <v.Type> [off] {sym} ptr mem)
   802  (MOVLQSX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLQSXload <v.Type> [off] {sym} ptr mem)
   803  (MOVLQZX x:(MOVLload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLload <v.Type> [off] {sym} ptr mem)
   804  (MOVLQZX x:(MOVQload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLload <v.Type> [off] {sym} ptr mem)
   805  
   806  (MOVBQZX x:(MOVBloadidx1 [off] {sym} ptr idx mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBloadidx1 <v.Type> [off] {sym} ptr idx mem)
   807  (MOVWQZX x:(MOVWloadidx1 [off] {sym} ptr idx mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWloadidx1 <v.Type> [off] {sym} ptr idx mem)
   808  (MOVWQZX x:(MOVWloadidx2 [off] {sym} ptr idx mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVWloadidx2 <v.Type> [off] {sym} ptr idx mem)
   809  (MOVLQZX x:(MOVLloadidx1 [off] {sym} ptr idx mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLloadidx1 <v.Type> [off] {sym} ptr idx mem)
   810  (MOVLQZX x:(MOVLloadidx4 [off] {sym} ptr idx mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVLloadidx4 <v.Type> [off] {sym} ptr idx mem)
   811  
   812  // replace load from same location as preceding store with copy
   813  (MOVBload [off] {sym} ptr (MOVBstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> x
   814  (MOVWload [off] {sym} ptr (MOVWstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> x
   815  (MOVLload [off] {sym} ptr (MOVLstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> x
   816  (MOVQload [off] {sym} ptr (MOVQstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> x
   817  
   818  // Fold extensions and ANDs together.
   819  (MOVBQZX (ANDLconst [c] x)) -> (ANDLconst [c & 0xff] x)
   820  (MOVWQZX (ANDLconst [c] x)) -> (ANDLconst [c & 0xffff] x)
   821  (MOVLQZX (ANDLconst [c] x)) -> (ANDLconst [c] x)
   822  (MOVBQSX (ANDLconst [c] x)) && c & 0x80 == 0 -> (ANDLconst [c & 0x7f] x)
   823  (MOVWQSX (ANDLconst [c] x)) && c & 0x8000 == 0 -> (ANDLconst [c & 0x7fff] x)
   824  (MOVLQSX (ANDLconst [c] x)) && c & 0x80000000 == 0 -> (ANDLconst [c & 0x7fffffff] x)
   825  
   826  // Don't extend before storing
   827  (MOVLstore [off] {sym} ptr (MOVLQSX x) mem) -> (MOVLstore [off] {sym} ptr x mem)
   828  (MOVWstore [off] {sym} ptr (MOVWQSX x) mem) -> (MOVWstore [off] {sym} ptr x mem)
   829  (MOVBstore [off] {sym} ptr (MOVBQSX x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   830  (MOVLstore [off] {sym} ptr (MOVLQZX x) mem) -> (MOVLstore [off] {sym} ptr x mem)
   831  (MOVWstore [off] {sym} ptr (MOVWQZX x) mem) -> (MOVWstore [off] {sym} ptr x mem)
   832  (MOVBstore [off] {sym} ptr (MOVBQZX x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   833  
   834  // fold constants into memory operations
   835  // Note that this is not always a good idea because if not all the uses of
   836  // the ADDQconst get eliminated, we still have to compute the ADDQconst and we now
   837  // have potentially two live values (ptr and (ADDQconst [off] ptr)) instead of one.
   838  // Nevertheless, let's do it!
   839  (MOVQload  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVQload  [off1+off2] {sym} ptr mem)
   840  (MOVLload  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVLload  [off1+off2] {sym} ptr mem)
   841  (MOVWload  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVWload  [off1+off2] {sym} ptr mem)
   842  (MOVBload  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVBload  [off1+off2] {sym} ptr mem)
   843  (MOVSSload [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVSSload [off1+off2] {sym} ptr mem)
   844  (MOVSDload [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVSDload [off1+off2] {sym} ptr mem)
   845  (MOVOload  [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVOload  [off1+off2] {sym} ptr mem)
   846  
   847  (MOVQstore  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVQstore  [off1+off2] {sym} ptr val mem)
   848  (MOVLstore  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVLstore  [off1+off2] {sym} ptr val mem)
   849  (MOVWstore  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVWstore  [off1+off2] {sym} ptr val mem)
   850  (MOVBstore  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVBstore  [off1+off2] {sym} ptr val mem)
   851  (MOVSSstore [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVSSstore [off1+off2] {sym} ptr val mem)
   852  (MOVSDstore [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVSDstore [off1+off2] {sym} ptr val mem)
   853  (MOVOstore  [off1] {sym} (ADDQconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVOstore  [off1+off2] {sym} ptr val mem)
   854  
   855  // Fold constants into stores.
   856  (MOVQstore [off] {sym} ptr (MOVQconst [c]) mem) && validValAndOff(c,off) ->
   857  	(MOVQstoreconst [makeValAndOff(c,off)] {sym} ptr mem)
   858  (MOVLstore [off] {sym} ptr (MOVLconst [c]) mem) && validOff(off) ->
   859  	(MOVLstoreconst [makeValAndOff(int64(int32(c)),off)] {sym} ptr mem)
   860  (MOVWstore [off] {sym} ptr (MOVLconst [c]) mem) && validOff(off) ->
   861  	(MOVWstoreconst [makeValAndOff(int64(int16(c)),off)] {sym} ptr mem)
   862  (MOVBstore [off] {sym} ptr (MOVLconst [c]) mem) && validOff(off) ->
   863  	(MOVBstoreconst [makeValAndOff(int64(int8(c)),off)] {sym} ptr mem)
   864  
   865  // Fold address offsets into constant stores.
   866  (MOVQstoreconst [sc] {s} (ADDQconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
   867  	(MOVQstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
   868  (MOVLstoreconst [sc] {s} (ADDQconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
   869  	(MOVLstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
   870  (MOVWstoreconst [sc] {s} (ADDQconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
   871  	(MOVWstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
   872  (MOVBstoreconst [sc] {s} (ADDQconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
   873  	(MOVBstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
   874  
   875  // We need to fold LEAQ into the MOVx ops so that the live variable analysis knows
   876  // what variables are being read/written by the ops.
   877  (MOVQload  [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   878  	(MOVQload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
   879  (MOVLload  [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   880  	(MOVLload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
   881  (MOVWload  [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   882  	(MOVWload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
   883  (MOVBload  [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   884  	(MOVBload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
   885  (MOVSSload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   886  	(MOVSSload [off1+off2] {mergeSym(sym1,sym2)} base mem)
   887  (MOVSDload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   888  	(MOVSDload [off1+off2] {mergeSym(sym1,sym2)} base mem)
   889  (MOVOload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   890  	(MOVOload [off1+off2] {mergeSym(sym1,sym2)} base mem)
   891  
   892  (MOVBQSXload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   893  	(MOVBQSXload [off1+off2] {mergeSym(sym1,sym2)} base mem)
   894  (MOVWQSXload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   895  	(MOVWQSXload [off1+off2] {mergeSym(sym1,sym2)} base mem)
   896  (MOVLQSXload [off1] {sym1} (LEAQ [off2] {sym2} base) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   897  	(MOVLQSXload [off1+off2] {mergeSym(sym1,sym2)} base mem)
   898  
   899  (MOVQstore  [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   900  	(MOVQstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
   901  (MOVLstore  [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   902  	(MOVLstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
   903  (MOVWstore  [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   904  	(MOVWstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
   905  (MOVBstore  [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   906  	(MOVBstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
   907  (MOVSSstore [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   908  	(MOVSSstore [off1+off2] {mergeSym(sym1,sym2)} base val mem)
   909  (MOVSDstore [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   910  	(MOVSDstore [off1+off2] {mergeSym(sym1,sym2)} base val mem)
   911  (MOVOstore [off1] {sym1} (LEAQ [off2] {sym2} base) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   912  	(MOVOstore [off1+off2] {mergeSym(sym1,sym2)} base val mem)
   913  
   914  (MOVQstoreconst [sc] {sym1} (LEAQ [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
   915  	(MOVQstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
   916  (MOVLstoreconst [sc] {sym1} (LEAQ [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
   917  	(MOVLstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
   918  (MOVWstoreconst [sc] {sym1} (LEAQ [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
   919  	(MOVWstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
   920  (MOVBstoreconst [sc] {sym1} (LEAQ [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
   921  	(MOVBstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
   922  
   923  // generating indexed loads and stores
   924  (MOVBload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   925  	(MOVBloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   926  (MOVWload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   927  	(MOVWloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   928  (MOVWload [off1] {sym1} (LEAQ2 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   929  	(MOVWloadidx2 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   930  (MOVLload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   931  	(MOVLloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   932  (MOVLload [off1] {sym1} (LEAQ4 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   933  	(MOVLloadidx4 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   934  (MOVQload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   935  	(MOVQloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   936  (MOVQload [off1] {sym1} (LEAQ8 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   937  	(MOVQloadidx8 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   938  (MOVSSload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   939  	(MOVSSloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   940  (MOVSSload [off1] {sym1} (LEAQ4 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   941  	(MOVSSloadidx4 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   942  (MOVSDload [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   943  	(MOVSDloadidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   944  (MOVSDload [off1] {sym1} (LEAQ8 [off2] {sym2} ptr idx) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   945  	(MOVSDloadidx8 [off1+off2] {mergeSym(sym1,sym2)} ptr idx mem)
   946  
   947  (MOVBstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   948  	(MOVBstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   949  (MOVWstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   950  	(MOVWstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   951  (MOVWstore [off1] {sym1} (LEAQ2 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   952  	(MOVWstoreidx2 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   953  (MOVLstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   954  	(MOVLstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   955  (MOVLstore [off1] {sym1} (LEAQ4 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   956  	(MOVLstoreidx4 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   957  (MOVQstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   958  	(MOVQstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   959  (MOVQstore [off1] {sym1} (LEAQ8 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   960  	(MOVQstoreidx8 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   961  (MOVSSstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   962  	(MOVSSstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   963  (MOVSSstore [off1] {sym1} (LEAQ4 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   964  	(MOVSSstoreidx4 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   965  (MOVSDstore [off1] {sym1} (LEAQ1 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   966  	(MOVSDstoreidx1 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   967  (MOVSDstore [off1] {sym1} (LEAQ8 [off2] {sym2} ptr idx) val mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
   968  	(MOVSDstoreidx8 [off1+off2] {mergeSym(sym1,sym2)} ptr idx val mem)
   969  
   970  (MOVBload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVBloadidx1 [off] {sym} ptr idx mem)
   971  (MOVWload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVWloadidx1 [off] {sym} ptr idx mem)
   972  (MOVLload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVLloadidx1 [off] {sym} ptr idx mem)
   973  (MOVQload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVQloadidx1 [off] {sym} ptr idx mem)
   974  (MOVSSload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVSSloadidx1 [off] {sym} ptr idx mem)
   975  (MOVSDload [off] {sym} (ADDQ ptr idx) mem) && ptr.Op != OpSB -> (MOVSDloadidx1 [off] {sym} ptr idx mem)
   976  (MOVBstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVBstoreidx1 [off] {sym} ptr idx val mem)
   977  (MOVWstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVWstoreidx1 [off] {sym} ptr idx val mem)
   978  (MOVLstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVLstoreidx1 [off] {sym} ptr idx val mem)
   979  (MOVQstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVQstoreidx1 [off] {sym} ptr idx val mem)
   980  (MOVSSstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVSSstoreidx1 [off] {sym} ptr idx val mem)
   981  (MOVSDstore [off] {sym} (ADDQ ptr idx) val mem) && ptr.Op != OpSB -> (MOVSDstoreidx1 [off] {sym} ptr idx val mem)
   982  
   983  (MOVBstoreconst [x] {sym1} (LEAQ1 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
   984  	(MOVBstoreconstidx1 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
   985  (MOVWstoreconst [x] {sym1} (LEAQ1 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
   986  	(MOVWstoreconstidx1 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
   987  (MOVWstoreconst [x] {sym1} (LEAQ2 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
   988  	(MOVWstoreconstidx2 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
   989  (MOVLstoreconst [x] {sym1} (LEAQ1 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
   990  	(MOVLstoreconstidx1 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
   991  (MOVLstoreconst [x] {sym1} (LEAQ4 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
   992  	(MOVLstoreconstidx4 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
   993  (MOVQstoreconst [x] {sym1} (LEAQ1 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
   994  	(MOVQstoreconstidx1 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
   995  (MOVQstoreconst [x] {sym1} (LEAQ8 [off] {sym2} ptr idx) mem) && canMergeSym(sym1, sym2) ->
   996  	(MOVQstoreconstidx8 [ValAndOff(x).add(off)] {mergeSym(sym1,sym2)} ptr idx mem)
   997  
   998  (MOVBstoreconst [x] {sym} (ADDQ ptr idx) mem) -> (MOVBstoreconstidx1 [x] {sym} ptr idx mem)
   999  (MOVWstoreconst [x] {sym} (ADDQ ptr idx) mem) -> (MOVWstoreconstidx1 [x] {sym} ptr idx mem)
  1000  (MOVLstoreconst [x] {sym} (ADDQ ptr idx) mem) -> (MOVLstoreconstidx1 [x] {sym} ptr idx mem)
  1001  (MOVQstoreconst [x] {sym} (ADDQ ptr idx) mem) -> (MOVQstoreconstidx1 [x] {sym} ptr idx mem)
  1002  
  1003  // combine SHLQ into indexed loads and stores
  1004  (MOVWloadidx1 [c] {sym} ptr (SHLQconst [1] idx) mem) -> (MOVWloadidx2 [c] {sym} ptr idx mem)
  1005  (MOVLloadidx1 [c] {sym} ptr (SHLQconst [2] idx) mem) -> (MOVLloadidx4 [c] {sym} ptr idx mem)
  1006  (MOVQloadidx1 [c] {sym} ptr (SHLQconst [3] idx) mem) -> (MOVQloadidx8 [c] {sym} ptr idx mem)
  1007  (MOVSSloadidx1 [c] {sym} ptr (SHLQconst [2] idx) mem) -> (MOVSSloadidx4 [c] {sym} ptr idx mem)
  1008  (MOVSDloadidx1 [c] {sym} ptr (SHLQconst [3] idx) mem) -> (MOVSDloadidx8 [c] {sym} ptr idx mem)
  1009  (MOVWstoreidx1 [c] {sym} ptr (SHLQconst [1] idx) val mem) -> (MOVWstoreidx2 [c] {sym} ptr idx val mem)
  1010  (MOVLstoreidx1 [c] {sym} ptr (SHLQconst [2] idx) val mem) -> (MOVLstoreidx4 [c] {sym} ptr idx val mem)
  1011  (MOVQstoreidx1 [c] {sym} ptr (SHLQconst [3] idx) val mem) -> (MOVQstoreidx8 [c] {sym} ptr idx val mem)
  1012  (MOVSSstoreidx1 [c] {sym} ptr (SHLQconst [2] idx) val mem) -> (MOVSSstoreidx4 [c] {sym} ptr idx val mem)
  1013  (MOVSDstoreidx1 [c] {sym} ptr (SHLQconst [3] idx) val mem) -> (MOVSDstoreidx8 [c] {sym} ptr idx val mem)
  1014  (MOVWstoreconstidx1 [c] {sym} ptr (SHLQconst [1] idx) mem) -> (MOVWstoreconstidx2 [c] {sym} ptr idx mem)
  1015  (MOVLstoreconstidx1 [c] {sym} ptr (SHLQconst [2] idx) mem) -> (MOVLstoreconstidx4 [c] {sym} ptr idx mem)
  1016  (MOVQstoreconstidx1 [c] {sym} ptr (SHLQconst [3] idx) mem) -> (MOVQstoreconstidx8 [c] {sym} ptr idx mem)
  1017  
  1018  // combine ADDQ into indexed loads and stores
  1019  (MOVBloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVBloadidx1 [c+d] {sym} ptr idx mem)
  1020  (MOVWloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVWloadidx1 [c+d] {sym} ptr idx mem)
  1021  (MOVWloadidx2 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVWloadidx2 [c+d] {sym} ptr idx mem)
  1022  (MOVLloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVLloadidx1 [c+d] {sym} ptr idx mem)
  1023  (MOVLloadidx4 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVLloadidx4 [c+d] {sym} ptr idx mem)
  1024  (MOVQloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVQloadidx1 [c+d] {sym} ptr idx mem)
  1025  (MOVQloadidx8 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVQloadidx8 [c+d] {sym} ptr idx mem)
  1026  (MOVSSloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVSSloadidx1 [c+d] {sym} ptr idx mem)
  1027  (MOVSSloadidx4 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVSSloadidx4 [c+d] {sym} ptr idx mem)
  1028  (MOVSDloadidx1 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVSDloadidx1 [c+d] {sym} ptr idx mem)
  1029  (MOVSDloadidx8 [c] {sym} (ADDQconst [d] ptr) idx mem) -> (MOVSDloadidx8 [c+d] {sym} ptr idx mem)
  1030  
  1031  (MOVBstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVBstoreidx1 [c+d] {sym} ptr idx val mem)
  1032  (MOVWstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVWstoreidx1 [c+d] {sym} ptr idx val mem)
  1033  (MOVWstoreidx2 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVWstoreidx2 [c+d] {sym} ptr idx val mem)
  1034  (MOVLstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVLstoreidx1 [c+d] {sym} ptr idx val mem)
  1035  (MOVLstoreidx4 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVLstoreidx4 [c+d] {sym} ptr idx val mem)
  1036  (MOVQstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVQstoreidx1 [c+d] {sym} ptr idx val mem)
  1037  (MOVQstoreidx8 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVQstoreidx8 [c+d] {sym} ptr idx val mem)
  1038  (MOVSSstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVSSstoreidx1 [c+d] {sym} ptr idx val mem)
  1039  (MOVSSstoreidx4 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVSSstoreidx4 [c+d] {sym} ptr idx val mem)
  1040  (MOVSDstoreidx1 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVSDstoreidx1 [c+d] {sym} ptr idx val mem)
  1041  (MOVSDstoreidx8 [c] {sym} (ADDQconst [d] ptr) idx val mem) -> (MOVSDstoreidx8 [c+d] {sym} ptr idx val mem)
  1042  
  1043  (MOVBloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVBloadidx1 [c+d] {sym} ptr idx mem)
  1044  (MOVWloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVWloadidx1 [c+d] {sym} ptr idx mem)
  1045  (MOVWloadidx2 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVWloadidx2 [c+2*d] {sym} ptr idx mem)
  1046  (MOVLloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVLloadidx1 [c+d] {sym} ptr idx mem)
  1047  (MOVLloadidx4 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVLloadidx4 [c+4*d] {sym} ptr idx mem)
  1048  (MOVQloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVQloadidx1 [c+d] {sym} ptr idx mem)
  1049  (MOVQloadidx8 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVQloadidx8 [c+8*d] {sym} ptr idx mem)
  1050  (MOVSSloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVSSloadidx1 [c+d] {sym} ptr idx mem)
  1051  (MOVSSloadidx4 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVSSloadidx4 [c+4*d] {sym} ptr idx mem)
  1052  (MOVSDloadidx1 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVSDloadidx1 [c+d] {sym} ptr idx mem)
  1053  (MOVSDloadidx8 [c] {sym} ptr (ADDQconst [d] idx) mem) -> (MOVSDloadidx8 [c+8*d] {sym} ptr idx mem)
  1054  
  1055  (MOVBstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVBstoreidx1 [c+d] {sym} ptr idx val mem)
  1056  (MOVWstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVWstoreidx1 [c+d] {sym} ptr idx val mem)
  1057  (MOVWstoreidx2 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVWstoreidx2 [c+2*d] {sym} ptr idx val mem)
  1058  (MOVLstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVLstoreidx1 [c+d] {sym} ptr idx val mem)
  1059  (MOVLstoreidx4 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVLstoreidx4 [c+4*d] {sym} ptr idx val mem)
  1060  (MOVQstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVQstoreidx1 [c+d] {sym} ptr idx val mem)
  1061  (MOVQstoreidx8 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVQstoreidx8 [c+8*d] {sym} ptr idx val mem)
  1062  (MOVSSstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVSSstoreidx1 [c+d] {sym} ptr idx val mem)
  1063  (MOVSSstoreidx4 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVSSstoreidx4 [c+4*d] {sym} ptr idx val mem)
  1064  (MOVSDstoreidx1 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVSDstoreidx1 [c+d] {sym} ptr idx val mem)
  1065  (MOVSDstoreidx8 [c] {sym} ptr (ADDQconst [d] idx) val mem) -> (MOVSDstoreidx8 [c+8*d] {sym} ptr idx val mem)
  1066  
  1067  (MOVBstoreconstidx1 [x] {sym} (ADDQconst [c] ptr) idx mem) ->
  1068  	(MOVBstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1069  (MOVWstoreconstidx1 [x] {sym} (ADDQconst [c] ptr) idx mem) ->
  1070  	(MOVWstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1071  (MOVWstoreconstidx2 [x] {sym} (ADDQconst [c] ptr) idx mem) ->
  1072  	(MOVWstoreconstidx2 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1073  (MOVLstoreconstidx1 [x] {sym} (ADDQconst [c] ptr) idx mem) ->
  1074  	(MOVLstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1075  (MOVLstoreconstidx4 [x] {sym} (ADDQconst [c] ptr) idx mem) ->
  1076  	(MOVLstoreconstidx4 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1077  (MOVQstoreconstidx1 [x] {sym} (ADDQconst [c] ptr) idx mem) ->
  1078  	(MOVQstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1079  (MOVQstoreconstidx8 [x] {sym} (ADDQconst [c] ptr) idx mem) ->
  1080  	(MOVQstoreconstidx8 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1081  
  1082  (MOVBstoreconstidx1 [x] {sym} ptr (ADDQconst [c] idx) mem) ->
  1083  	(MOVBstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1084  (MOVWstoreconstidx1 [x] {sym} ptr (ADDQconst [c] idx) mem) ->
  1085  	(MOVWstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1086  (MOVWstoreconstidx2 [x] {sym} ptr (ADDQconst [c] idx) mem) ->
  1087  	(MOVWstoreconstidx2 [ValAndOff(x).add(2*c)] {sym} ptr idx mem)
  1088  (MOVLstoreconstidx1 [x] {sym} ptr (ADDQconst [c] idx) mem) ->
  1089  	(MOVLstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1090  (MOVLstoreconstidx4 [x] {sym} ptr (ADDQconst [c] idx) mem) ->
  1091  	(MOVLstoreconstidx4 [ValAndOff(x).add(4*c)] {sym} ptr idx mem)
  1092  (MOVQstoreconstidx1 [x] {sym} ptr (ADDQconst [c] idx) mem) ->
  1093  	(MOVQstoreconstidx1 [ValAndOff(x).add(c)] {sym} ptr idx mem)
  1094  (MOVQstoreconstidx8 [x] {sym} ptr (ADDQconst [c] idx) mem) ->
  1095  	(MOVQstoreconstidx8 [ValAndOff(x).add(8*c)] {sym} ptr idx mem)
  1096  
  1097  // fold LEAQs together
  1098  (LEAQ [off1] {sym1} (LEAQ [off2] {sym2} x)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1099        (LEAQ [off1+off2] {mergeSym(sym1,sym2)} x)
  1100  
  1101  // LEAQ into LEAQ1
  1102  (LEAQ1 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && x.Op != OpSB ->
  1103         (LEAQ1 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1104  (LEAQ1 [off1] {sym1} x (LEAQ [off2] {sym2} y)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && y.Op != OpSB ->
  1105         (LEAQ1 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1106  
  1107  // LEAQ1 into LEAQ
  1108  (LEAQ [off1] {sym1} (LEAQ1 [off2] {sym2} x y)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1109         (LEAQ1 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1110  
  1111  // LEAQ into LEAQ[248]
  1112  (LEAQ2 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && x.Op != OpSB ->
  1113         (LEAQ2 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1114  (LEAQ4 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && x.Op != OpSB ->
  1115         (LEAQ4 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1116  (LEAQ8 [off1] {sym1} (LEAQ [off2] {sym2} x) y) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && x.Op != OpSB ->
  1117         (LEAQ8 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1118  
  1119  // LEAQ[248] into LEAQ
  1120  (LEAQ [off1] {sym1} (LEAQ2 [off2] {sym2} x y)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1121        (LEAQ2 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1122  (LEAQ [off1] {sym1} (LEAQ4 [off2] {sym2} x y)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1123        (LEAQ4 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1124  (LEAQ [off1] {sym1} (LEAQ8 [off2] {sym2} x y)) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1125        (LEAQ8 [off1+off2] {mergeSym(sym1,sym2)} x y)
  1126  
  1127  // Absorb InvertFlags into branches.
  1128  (LT (InvertFlags cmp) yes no) -> (GT cmp yes no)
  1129  (GT (InvertFlags cmp) yes no) -> (LT cmp yes no)
  1130  (LE (InvertFlags cmp) yes no) -> (GE cmp yes no)
  1131  (GE (InvertFlags cmp) yes no) -> (LE cmp yes no)
  1132  (ULT (InvertFlags cmp) yes no) -> (UGT cmp yes no)
  1133  (UGT (InvertFlags cmp) yes no) -> (ULT cmp yes no)
  1134  (ULE (InvertFlags cmp) yes no) -> (UGE cmp yes no)
  1135  (UGE (InvertFlags cmp) yes no) -> (ULE cmp yes no)
  1136  (EQ (InvertFlags cmp) yes no) -> (EQ cmp yes no)
  1137  (NE (InvertFlags cmp) yes no) -> (NE cmp yes no)
  1138  
  1139  // Constant comparisons.
  1140  (CMPQconst (MOVQconst [x]) [y]) && x==y -> (FlagEQ)
  1141  (CMPQconst (MOVQconst [x]) [y]) && x<y && uint64(x)<uint64(y) -> (FlagLT_ULT)
  1142  (CMPQconst (MOVQconst [x]) [y]) && x<y && uint64(x)>uint64(y) -> (FlagLT_UGT)
  1143  (CMPQconst (MOVQconst [x]) [y]) && x>y && uint64(x)<uint64(y) -> (FlagGT_ULT)
  1144  (CMPQconst (MOVQconst [x]) [y]) && x>y && uint64(x)>uint64(y) -> (FlagGT_UGT)
  1145  (CMPLconst (MOVLconst [x]) [y]) && int32(x)==int32(y) -> (FlagEQ)
  1146  (CMPLconst (MOVLconst [x]) [y]) && int32(x)<int32(y) && uint32(x)<uint32(y) -> (FlagLT_ULT)
  1147  (CMPLconst (MOVLconst [x]) [y]) && int32(x)<int32(y) && uint32(x)>uint32(y) -> (FlagLT_UGT)
  1148  (CMPLconst (MOVLconst [x]) [y]) && int32(x)>int32(y) && uint32(x)<uint32(y) -> (FlagGT_ULT)
  1149  (CMPLconst (MOVLconst [x]) [y]) && int32(x)>int32(y) && uint32(x)>uint32(y) -> (FlagGT_UGT)
  1150  (CMPWconst (MOVLconst [x]) [y]) && int16(x)==int16(y) -> (FlagEQ)
  1151  (CMPWconst (MOVLconst [x]) [y]) && int16(x)<int16(y) && uint16(x)<uint16(y) -> (FlagLT_ULT)
  1152  (CMPWconst (MOVLconst [x]) [y]) && int16(x)<int16(y) && uint16(x)>uint16(y) -> (FlagLT_UGT)
  1153  (CMPWconst (MOVLconst [x]) [y]) && int16(x)>int16(y) && uint16(x)<uint16(y) -> (FlagGT_ULT)
  1154  (CMPWconst (MOVLconst [x]) [y]) && int16(x)>int16(y) && uint16(x)>uint16(y) -> (FlagGT_UGT)
  1155  (CMPBconst (MOVLconst [x]) [y]) && int8(x)==int8(y) -> (FlagEQ)
  1156  (CMPBconst (MOVLconst [x]) [y]) && int8(x)<int8(y) && uint8(x)<uint8(y) -> (FlagLT_ULT)
  1157  (CMPBconst (MOVLconst [x]) [y]) && int8(x)<int8(y) && uint8(x)>uint8(y) -> (FlagLT_UGT)
  1158  (CMPBconst (MOVLconst [x]) [y]) && int8(x)>int8(y) && uint8(x)<uint8(y) -> (FlagGT_ULT)
  1159  (CMPBconst (MOVLconst [x]) [y]) && int8(x)>int8(y) && uint8(x)>uint8(y) -> (FlagGT_UGT)
  1160  
  1161  // Other known comparisons.
  1162  (CMPQconst (MOVBQZX _) [c]) && 0xFF < c -> (FlagLT_ULT)
  1163  (CMPQconst (MOVWQZX _) [c]) && 0xFFFF < c -> (FlagLT_ULT)
  1164  (CMPQconst (MOVLQZX _) [c]) && 0xFFFFFFFF < c -> (FlagLT_ULT)
  1165  (CMPLconst (SHRLconst _ [c]) [n]) && 0 <= n && 0 < c && c <= 32 && (1<<uint64(32-c)) <= uint64(n) -> (FlagLT_ULT)
  1166  (CMPQconst (SHRQconst _ [c]) [n]) && 0 <= n && 0 < c && c <= 64 && (1<<uint64(64-c)) <= uint64(n) -> (FlagLT_ULT)
  1167  (CMPQconst (ANDQconst _ [m]) [n]) && 0 <= m && m < n -> (FlagLT_ULT)
  1168  (CMPLconst (ANDLconst _ [m]) [n]) && 0 <= int32(m) && int32(m) < int32(n) -> (FlagLT_ULT)
  1169  (CMPWconst (ANDLconst _ [m]) [n]) && 0 <= int16(m) && int16(m) < int16(n) -> (FlagLT_ULT)
  1170  (CMPBconst (ANDLconst _ [m]) [n]) && 0 <= int8(m) && int8(m) < int8(n) -> (FlagLT_ULT)
  1171  // TODO: DIVxU also.
  1172  
  1173  // Absorb flag constants into SBB ops.
  1174  (SBBQcarrymask (FlagEQ)) -> (MOVQconst [0])
  1175  (SBBQcarrymask (FlagLT_ULT)) -> (MOVQconst [-1])
  1176  (SBBQcarrymask (FlagLT_UGT)) -> (MOVQconst [0])
  1177  (SBBQcarrymask (FlagGT_ULT)) -> (MOVQconst [-1])
  1178  (SBBQcarrymask (FlagGT_UGT)) -> (MOVQconst [0])
  1179  (SBBLcarrymask (FlagEQ)) -> (MOVLconst [0])
  1180  (SBBLcarrymask (FlagLT_ULT)) -> (MOVLconst [-1])
  1181  (SBBLcarrymask (FlagLT_UGT)) -> (MOVLconst [0])
  1182  (SBBLcarrymask (FlagGT_ULT)) -> (MOVLconst [-1])
  1183  (SBBLcarrymask (FlagGT_UGT)) -> (MOVLconst [0])
  1184  
  1185  // Absorb flag constants into branches.
  1186  (EQ (FlagEQ) yes no) -> (First nil yes no)
  1187  (EQ (FlagLT_ULT) yes no) -> (First nil no yes)
  1188  (EQ (FlagLT_UGT) yes no) -> (First nil no yes)
  1189  (EQ (FlagGT_ULT) yes no) -> (First nil no yes)
  1190  (EQ (FlagGT_UGT) yes no) -> (First nil no yes)
  1191  
  1192  (NE (FlagEQ) yes no) -> (First nil no yes)
  1193  (NE (FlagLT_ULT) yes no) -> (First nil yes no)
  1194  (NE (FlagLT_UGT) yes no) -> (First nil yes no)
  1195  (NE (FlagGT_ULT) yes no) -> (First nil yes no)
  1196  (NE (FlagGT_UGT) yes no) -> (First nil yes no)
  1197  
  1198  (LT (FlagEQ) yes no) -> (First nil no yes)
  1199  (LT (FlagLT_ULT) yes no) -> (First nil yes no)
  1200  (LT (FlagLT_UGT) yes no) -> (First nil yes no)
  1201  (LT (FlagGT_ULT) yes no) -> (First nil no yes)
  1202  (LT (FlagGT_UGT) yes no) -> (First nil no yes)
  1203  
  1204  (LE (FlagEQ) yes no) -> (First nil yes no)
  1205  (LE (FlagLT_ULT) yes no) -> (First nil yes no)
  1206  (LE (FlagLT_UGT) yes no) -> (First nil yes no)
  1207  (LE (FlagGT_ULT) yes no) -> (First nil no yes)
  1208  (LE (FlagGT_UGT) yes no) -> (First nil no yes)
  1209  
  1210  (GT (FlagEQ) yes no) -> (First nil no yes)
  1211  (GT (FlagLT_ULT) yes no) -> (First nil no yes)
  1212  (GT (FlagLT_UGT) yes no) -> (First nil no yes)
  1213  (GT (FlagGT_ULT) yes no) -> (First nil yes no)
  1214  (GT (FlagGT_UGT) yes no) -> (First nil yes no)
  1215  
  1216  (GE (FlagEQ) yes no) -> (First nil yes no)
  1217  (GE (FlagLT_ULT) yes no) -> (First nil no yes)
  1218  (GE (FlagLT_UGT) yes no) -> (First nil no yes)
  1219  (GE (FlagGT_ULT) yes no) -> (First nil yes no)
  1220  (GE (FlagGT_UGT) yes no) -> (First nil yes no)
  1221  
  1222  (ULT (FlagEQ) yes no) -> (First nil no yes)
  1223  (ULT (FlagLT_ULT) yes no) -> (First nil yes no)
  1224  (ULT (FlagLT_UGT) yes no) -> (First nil no yes)
  1225  (ULT (FlagGT_ULT) yes no) -> (First nil yes no)
  1226  (ULT (FlagGT_UGT) yes no) -> (First nil no yes)
  1227  
  1228  (ULE (FlagEQ) yes no) -> (First nil yes no)
  1229  (ULE (FlagLT_ULT) yes no) -> (First nil yes no)
  1230  (ULE (FlagLT_UGT) yes no) -> (First nil no yes)
  1231  (ULE (FlagGT_ULT) yes no) -> (First nil yes no)
  1232  (ULE (FlagGT_UGT) yes no) -> (First nil no yes)
  1233  
  1234  (UGT (FlagEQ) yes no) -> (First nil no yes)
  1235  (UGT (FlagLT_ULT) yes no) -> (First nil no yes)
  1236  (UGT (FlagLT_UGT) yes no) -> (First nil yes no)
  1237  (UGT (FlagGT_ULT) yes no) -> (First nil no yes)
  1238  (UGT (FlagGT_UGT) yes no) -> (First nil yes no)
  1239  
  1240  (UGE (FlagEQ) yes no) -> (First nil yes no)
  1241  (UGE (FlagLT_ULT) yes no) -> (First nil no yes)
  1242  (UGE (FlagLT_UGT) yes no) -> (First nil yes no)
  1243  (UGE (FlagGT_ULT) yes no) -> (First nil no yes)
  1244  (UGE (FlagGT_UGT) yes no) -> (First nil yes no)
  1245  
  1246  // Absorb flag constants into SETxx ops.
  1247  (SETEQ (FlagEQ)) -> (MOVLconst [1])
  1248  (SETEQ (FlagLT_ULT)) -> (MOVLconst [0])
  1249  (SETEQ (FlagLT_UGT)) -> (MOVLconst [0])
  1250  (SETEQ (FlagGT_ULT)) -> (MOVLconst [0])
  1251  (SETEQ (FlagGT_UGT)) -> (MOVLconst [0])
  1252  
  1253  (SETNE (FlagEQ)) -> (MOVLconst [0])
  1254  (SETNE (FlagLT_ULT)) -> (MOVLconst [1])
  1255  (SETNE (FlagLT_UGT)) -> (MOVLconst [1])
  1256  (SETNE (FlagGT_ULT)) -> (MOVLconst [1])
  1257  (SETNE (FlagGT_UGT)) -> (MOVLconst [1])
  1258  
  1259  (SETL (FlagEQ)) -> (MOVLconst [0])
  1260  (SETL (FlagLT_ULT)) -> (MOVLconst [1])
  1261  (SETL (FlagLT_UGT)) -> (MOVLconst [1])
  1262  (SETL (FlagGT_ULT)) -> (MOVLconst [0])
  1263  (SETL (FlagGT_UGT)) -> (MOVLconst [0])
  1264  
  1265  (SETLE (FlagEQ)) -> (MOVLconst [1])
  1266  (SETLE (FlagLT_ULT)) -> (MOVLconst [1])
  1267  (SETLE (FlagLT_UGT)) -> (MOVLconst [1])
  1268  (SETLE (FlagGT_ULT)) -> (MOVLconst [0])
  1269  (SETLE (FlagGT_UGT)) -> (MOVLconst [0])
  1270  
  1271  (SETG (FlagEQ)) -> (MOVLconst [0])
  1272  (SETG (FlagLT_ULT)) -> (MOVLconst [0])
  1273  (SETG (FlagLT_UGT)) -> (MOVLconst [0])
  1274  (SETG (FlagGT_ULT)) -> (MOVLconst [1])
  1275  (SETG (FlagGT_UGT)) -> (MOVLconst [1])
  1276  
  1277  (SETGE (FlagEQ)) -> (MOVLconst [1])
  1278  (SETGE (FlagLT_ULT)) -> (MOVLconst [0])
  1279  (SETGE (FlagLT_UGT)) -> (MOVLconst [0])
  1280  (SETGE (FlagGT_ULT)) -> (MOVLconst [1])
  1281  (SETGE (FlagGT_UGT)) -> (MOVLconst [1])
  1282  
  1283  (SETB (FlagEQ)) -> (MOVLconst [0])
  1284  (SETB (FlagLT_ULT)) -> (MOVLconst [1])
  1285  (SETB (FlagLT_UGT)) -> (MOVLconst [0])
  1286  (SETB (FlagGT_ULT)) -> (MOVLconst [1])
  1287  (SETB (FlagGT_UGT)) -> (MOVLconst [0])
  1288  
  1289  (SETBE (FlagEQ)) -> (MOVLconst [1])
  1290  (SETBE (FlagLT_ULT)) -> (MOVLconst [1])
  1291  (SETBE (FlagLT_UGT)) -> (MOVLconst [0])
  1292  (SETBE (FlagGT_ULT)) -> (MOVLconst [1])
  1293  (SETBE (FlagGT_UGT)) -> (MOVLconst [0])
  1294  
  1295  (SETA (FlagEQ)) -> (MOVLconst [0])
  1296  (SETA (FlagLT_ULT)) -> (MOVLconst [0])
  1297  (SETA (FlagLT_UGT)) -> (MOVLconst [1])
  1298  (SETA (FlagGT_ULT)) -> (MOVLconst [0])
  1299  (SETA (FlagGT_UGT)) -> (MOVLconst [1])
  1300  
  1301  (SETAE (FlagEQ)) -> (MOVLconst [1])
  1302  (SETAE (FlagLT_ULT)) -> (MOVLconst [0])
  1303  (SETAE (FlagLT_UGT)) -> (MOVLconst [1])
  1304  (SETAE (FlagGT_ULT)) -> (MOVLconst [0])
  1305  (SETAE (FlagGT_UGT)) -> (MOVLconst [1])
  1306  
  1307  // Remove redundant *const ops
  1308  (ADDQconst [0] x) -> x
  1309  (ADDLconst [c] x) && int32(c)==0 -> x
  1310  (SUBQconst [0] x) -> x
  1311  (SUBLconst [c] x) && int32(c) == 0 -> x
  1312  (ANDQconst [0] _)                 -> (MOVQconst [0])
  1313  (ANDLconst [c] _) && int32(c)==0  -> (MOVLconst [0])
  1314  (ANDQconst [-1] x)                -> x
  1315  (ANDLconst [c] x) && int32(c)==-1 -> x
  1316  (ORQconst [0] x)                  -> x
  1317  (ORLconst [c] x) && int32(c)==0   -> x
  1318  (ORQconst [-1] _)                 -> (MOVQconst [-1])
  1319  (ORLconst [c] _) && int32(c)==-1  -> (MOVLconst [-1])
  1320  (XORQconst [0] x)                  -> x
  1321  (XORLconst [c] x) && int32(c)==0   -> x
  1322  // TODO: since we got rid of the W/B versions, we might miss
  1323  // things like (ANDLconst [0x100] x) which were formerly
  1324  // (ANDBconst [0] x).  Probably doesn't happen very often.
  1325  // If we cared, we might do:
  1326  //  (ANDLconst <t> [c] x) && t.Size()==1 && int8(x)==0 -> (MOVLconst [0])
  1327  
  1328  // Convert constant subtracts to constant adds
  1329  (SUBQconst [c] x) && c != -(1<<31) -> (ADDQconst [-c] x)
  1330  (SUBLconst [c] x) -> (ADDLconst [int64(int32(-c))] x)
  1331  
  1332  // generic constant folding
  1333  // TODO: more of this
  1334  (ADDQconst [c] (MOVQconst [d])) -> (MOVQconst [c+d])
  1335  (ADDLconst [c] (MOVLconst [d])) -> (MOVLconst [int64(int32(c+d))])
  1336  (ADDQconst [c] (ADDQconst [d] x)) && is32Bit(c+d) -> (ADDQconst [c+d] x)
  1337  (ADDLconst [c] (ADDLconst [d] x)) -> (ADDLconst [int64(int32(c+d))] x)
  1338  (SUBQconst (MOVQconst [d]) [c]) -> (MOVQconst [d-c])
  1339  (SUBQconst (SUBQconst x [d]) [c]) && is32Bit(-c-d) -> (ADDQconst [-c-d] x)
  1340  (SARQconst [c] (MOVQconst [d])) -> (MOVQconst [d>>uint64(c)])
  1341  (SARLconst [c] (MOVQconst [d])) -> (MOVQconst [d>>uint64(c)])
  1342  (SARWconst [c] (MOVQconst [d])) -> (MOVQconst [d>>uint64(c)])
  1343  (SARBconst [c] (MOVQconst [d])) -> (MOVQconst [d>>uint64(c)])
  1344  (NEGQ (MOVQconst [c])) -> (MOVQconst [-c])
  1345  (NEGL (MOVLconst [c])) -> (MOVLconst [int64(int32(-c))])
  1346  (MULQconst [c] (MOVQconst [d])) -> (MOVQconst [c*d])
  1347  (MULLconst [c] (MOVLconst [d])) -> (MOVLconst [int64(int32(c*d))])
  1348  (ANDQconst [c] (MOVQconst [d])) -> (MOVQconst [c&d])
  1349  (ANDLconst [c] (MOVLconst [d])) -> (MOVLconst [c&d])
  1350  (ORQconst [c] (MOVQconst [d])) -> (MOVQconst [c|d])
  1351  (ORLconst [c] (MOVLconst [d])) -> (MOVLconst [c|d])
  1352  (XORQconst [c] (MOVQconst [d])) -> (MOVQconst [c^d])
  1353  (XORLconst [c] (MOVLconst [d])) -> (MOVLconst [c^d])
  1354  (NOTQ (MOVQconst [c])) -> (MOVQconst [^c])
  1355  (NOTL (MOVLconst [c])) -> (MOVLconst [^c])
  1356  
  1357  // generic simplifications
  1358  // TODO: more of this
  1359  (ADDQ x (NEGQ y)) -> (SUBQ x y)
  1360  (ADDL x (NEGL y)) -> (SUBL x y)
  1361  (SUBQ x x) -> (MOVQconst [0])
  1362  (SUBL x x) -> (MOVLconst [0])
  1363  (ANDQ x x) -> x
  1364  (ANDL x x) -> x
  1365  (ORQ x x) -> x
  1366  (ORL x x) -> x
  1367  (XORQ x x) -> (MOVQconst [0])
  1368  (XORL x x) -> (MOVLconst [0])
  1369  
  1370  // checking AND against 0.
  1371  (CMPQconst (ANDQ x y) [0]) -> (TESTQ x y)
  1372  (CMPLconst (ANDL x y) [0]) -> (TESTL x y)
  1373  (CMPWconst (ANDL x y) [0]) -> (TESTW x y)
  1374  (CMPBconst (ANDL x y) [0]) -> (TESTB x y)
  1375  (CMPQconst (ANDQconst [c] x) [0]) -> (TESTQconst [c] x)
  1376  (CMPLconst (ANDLconst [c] x) [0]) -> (TESTLconst [c] x)
  1377  (CMPWconst (ANDLconst [c] x) [0]) -> (TESTWconst [int64(int16(c))] x)
  1378  (CMPBconst (ANDLconst [c] x) [0]) -> (TESTBconst [int64(int8(c))] x)
  1379  
  1380  // TEST %reg,%reg is shorter than CMP
  1381  (CMPQconst x [0]) -> (TESTQ x x)
  1382  (CMPLconst x [0]) -> (TESTL x x)
  1383  (CMPWconst x [0]) -> (TESTW x x)
  1384  (CMPBconst x [0]) -> (TESTB x x)
  1385  
  1386  // Combining byte loads into larger (unaligned) loads.
  1387  // There are many ways these combinations could occur.  This is
  1388  // designed to match the way encoding/binary.LittleEndian does it.
  1389  (ORL                  x0:(MOVBload [i]   {s} p mem)
  1390      s0:(SHLLconst [8] x1:(MOVBload [i+1] {s} p mem)))
  1391    && x0.Uses == 1
  1392    && x1.Uses == 1
  1393    && s0.Uses == 1
  1394    && mergePoint(b,x0,x1) != nil
  1395    && clobber(x0)
  1396    && clobber(x1)
  1397    && clobber(s0)
  1398    -> @mergePoint(b,x0,x1) (MOVWload [i] {s} p mem)
  1399  
  1400  (ORL o0:(ORL
  1401                         x0:(MOVWload [i]   {s} p mem)
  1402      s0:(SHLLconst [16] x1:(MOVBload [i+2] {s} p mem)))
  1403      s1:(SHLLconst [24] x2:(MOVBload [i+3] {s} p mem)))
  1404    && x0.Uses == 1
  1405    && x1.Uses == 1
  1406    && x2.Uses == 1
  1407    && s0.Uses == 1
  1408    && s1.Uses == 1
  1409    && o0.Uses == 1
  1410    && mergePoint(b,x0,x1,x2) != nil
  1411    && clobber(x0)
  1412    && clobber(x1)
  1413    && clobber(x2)
  1414    && clobber(s0)
  1415    && clobber(s1)
  1416    && clobber(o0)
  1417    -> @mergePoint(b,x0,x1,x2) (MOVLload [i] {s} p mem)
  1418  
  1419  (ORQ o0:(ORQ o1:(ORQ o2:(ORQ o3:(ORQ o4:(ORQ o5:(ORQ
  1420                         x0:(MOVBload [i]   {s} p mem)
  1421      s0:(SHLQconst [8]  x1:(MOVBload [i+1] {s} p mem)))
  1422      s1:(SHLQconst [16] x2:(MOVBload [i+2] {s} p mem)))
  1423      s2:(SHLQconst [24] x3:(MOVBload [i+3] {s} p mem)))
  1424      s3:(SHLQconst [32] x4:(MOVBload [i+4] {s} p mem)))
  1425      s4:(SHLQconst [40] x5:(MOVBload [i+5] {s} p mem)))
  1426      s5:(SHLQconst [48] x6:(MOVBload [i+6] {s} p mem)))
  1427      s6:(SHLQconst [56] x7:(MOVBload [i+7] {s} p mem)))
  1428    && x0.Uses == 1
  1429    && x1.Uses == 1
  1430    && x2.Uses == 1
  1431    && x3.Uses == 1
  1432    && x4.Uses == 1
  1433    && x5.Uses == 1
  1434    && x6.Uses == 1
  1435    && x7.Uses == 1
  1436    && s0.Uses == 1
  1437    && s1.Uses == 1
  1438    && s2.Uses == 1
  1439    && s3.Uses == 1
  1440    && s4.Uses == 1
  1441    && s5.Uses == 1
  1442    && s6.Uses == 1
  1443    && o0.Uses == 1
  1444    && o1.Uses == 1
  1445    && o2.Uses == 1
  1446    && o3.Uses == 1
  1447    && o4.Uses == 1
  1448    && o5.Uses == 1
  1449    && mergePoint(b,x0,x1,x2,x3,x4,x5,x6,x7) != nil
  1450    && clobber(x0)
  1451    && clobber(x1)
  1452    && clobber(x2)
  1453    && clobber(x3)
  1454    && clobber(x4)
  1455    && clobber(x5)
  1456    && clobber(x6)
  1457    && clobber(x7)
  1458    && clobber(s0)
  1459    && clobber(s1)
  1460    && clobber(s2)
  1461    && clobber(s3)
  1462    && clobber(s4)
  1463    && clobber(s5)
  1464    && clobber(s6)
  1465    && clobber(o0)
  1466    && clobber(o1)
  1467    && clobber(o2)
  1468    && clobber(o3)
  1469    && clobber(o4)
  1470    && clobber(o5)
  1471    -> @mergePoint(b,x0,x1,x2,x3,x4,x5,x6,x7) (MOVQload [i] {s} p mem)
  1472  
  1473  (ORL                  x0:(MOVBloadidx1 [i]   {s} p idx mem)
  1474      s0:(SHLLconst [8] x1:(MOVBloadidx1 [i+1] {s} p idx mem)))
  1475    && x0.Uses == 1
  1476    && x1.Uses == 1
  1477    && s0.Uses == 1
  1478    && mergePoint(b,x0,x1) != nil
  1479    && clobber(x0)
  1480    && clobber(x1)
  1481    && clobber(s0)
  1482    -> @mergePoint(b,x0,x1) (MOVWloadidx1 <v.Type> [i] {s} p idx mem)
  1483  
  1484  (ORL o0:(ORL
  1485                         x0:(MOVWloadidx1 [i]   {s} p idx mem)
  1486      s0:(SHLLconst [16] x1:(MOVBloadidx1 [i+2] {s} p idx mem)))
  1487      s1:(SHLLconst [24] x2:(MOVBloadidx1 [i+3] {s} p idx mem)))
  1488    && x0.Uses == 1
  1489    && x1.Uses == 1
  1490    && x2.Uses == 1
  1491    && s0.Uses == 1
  1492    && s1.Uses == 1
  1493    && o0.Uses == 1
  1494    && mergePoint(b,x0,x1,x2) != nil
  1495    && clobber(x0)
  1496    && clobber(x1)
  1497    && clobber(x2)
  1498    && clobber(s0)
  1499    && clobber(s1)
  1500    && clobber(o0)
  1501    -> @mergePoint(b,x0,x1,x2) (MOVLloadidx1 <v.Type> [i] {s} p idx mem)
  1502  
  1503  (ORQ o0:(ORQ o1:(ORQ o2:(ORQ o3:(ORQ o4:(ORQ o5:(ORQ
  1504                         x0:(MOVBloadidx1 [i]   {s} p idx mem)
  1505      s0:(SHLQconst [8]  x1:(MOVBloadidx1 [i+1] {s} p idx mem)))
  1506      s1:(SHLQconst [16] x2:(MOVBloadidx1 [i+2] {s} p idx mem)))
  1507      s2:(SHLQconst [24] x3:(MOVBloadidx1 [i+3] {s} p idx mem)))
  1508      s3:(SHLQconst [32] x4:(MOVBloadidx1 [i+4] {s} p idx mem)))
  1509      s4:(SHLQconst [40] x5:(MOVBloadidx1 [i+5] {s} p idx mem)))
  1510      s5:(SHLQconst [48] x6:(MOVBloadidx1 [i+6] {s} p idx mem)))
  1511      s6:(SHLQconst [56] x7:(MOVBloadidx1 [i+7] {s} p idx mem)))
  1512    && x0.Uses == 1
  1513    && x1.Uses == 1
  1514    && x2.Uses == 1
  1515    && x3.Uses == 1
  1516    && x4.Uses == 1
  1517    && x5.Uses == 1
  1518    && x6.Uses == 1
  1519    && x7.Uses == 1
  1520    && s0.Uses == 1
  1521    && s1.Uses == 1
  1522    && s2.Uses == 1
  1523    && s3.Uses == 1
  1524    && s4.Uses == 1
  1525    && s5.Uses == 1
  1526    && s6.Uses == 1
  1527    && o0.Uses == 1
  1528    && o1.Uses == 1
  1529    && o2.Uses == 1
  1530    && o3.Uses == 1
  1531    && o4.Uses == 1
  1532    && o5.Uses == 1
  1533    && mergePoint(b,x0,x1,x2,x3,x4,x5,x6,x7) != nil
  1534    && clobber(x0)
  1535    && clobber(x1)
  1536    && clobber(x2)
  1537    && clobber(x3)
  1538    && clobber(x4)
  1539    && clobber(x5)
  1540    && clobber(x6)
  1541    && clobber(x7)
  1542    && clobber(s0)
  1543    && clobber(s1)
  1544    && clobber(s2)
  1545    && clobber(s3)
  1546    && clobber(s4)
  1547    && clobber(s5)
  1548    && clobber(s6)
  1549    && clobber(o0)
  1550    && clobber(o1)
  1551    && clobber(o2)
  1552    && clobber(o3)
  1553    && clobber(o4)
  1554    && clobber(o5)
  1555    -> @mergePoint(b,x0,x1,x2,x3,x4,x5,x6,x7) (MOVQloadidx1 <v.Type> [i] {s} p idx mem)
  1556  
  1557  // Combine byte loads + shifts into larger (unaligned) loads + bswap
  1558  (ORL o1:(ORL o0:(ORL
  1559                         x0:(MOVBload [i] {s} p mem)
  1560      s0:(SHLLconst [8]  x1:(MOVBload [i-1] {s} p mem)))
  1561      s1:(SHLLconst [16] x2:(MOVBload [i-2] {s} p mem)))
  1562      s2:(SHLLconst [24] x3:(MOVBload [i-3] {s} p mem)))
  1563    && x0.Uses == 1
  1564    && x1.Uses == 1
  1565    && x2.Uses == 1
  1566    && x3.Uses == 1
  1567    && s0.Uses == 1
  1568    && s1.Uses == 1
  1569    && s2.Uses == 1
  1570    && o0.Uses == 1
  1571    && o1.Uses == 1
  1572    && mergePoint(b,x0,x1,x2,x3) != nil
  1573    && clobber(x0)
  1574    && clobber(x1)
  1575    && clobber(x2)
  1576    && clobber(x3)
  1577    && clobber(s0)
  1578    && clobber(s1)
  1579    && clobber(s2)
  1580    && clobber(o0)
  1581    && clobber(o1)
  1582    -> @mergePoint(b,x0,x1,x2,x3) (BSWAPL <v.Type> (MOVLload [i-3] {s} p mem))
  1583  
  1584  (ORL o1:(ORL o0:(ORL
  1585                         x0:(MOVBloadidx1 [i] {s} p idx mem)
  1586      s0:(SHLLconst [8]  x1:(MOVBloadidx1 [i-1] {s} p idx mem)))
  1587      s1:(SHLLconst [16] x2:(MOVBloadidx1 [i-2] {s} p idx mem)))
  1588      s2:(SHLLconst [24] x3:(MOVBloadidx1 [i-3] {s} p idx mem)))
  1589    && x0.Uses == 1
  1590    && x1.Uses == 1
  1591    && x2.Uses == 1
  1592    && x3.Uses == 1
  1593    && s0.Uses == 1
  1594    && s1.Uses == 1
  1595    && s2.Uses == 1
  1596    && o0.Uses == 1
  1597    && o1.Uses == 1
  1598    && mergePoint(b,x0,x1,x2,x3) != nil
  1599    && clobber(x0)
  1600    && clobber(x1)
  1601    && clobber(x2)
  1602    && clobber(x3)
  1603    && clobber(s0)
  1604    && clobber(s1)
  1605    && clobber(s2)
  1606    && clobber(o0)
  1607    && clobber(o1)
  1608    -> @mergePoint(b,x0,x1,x2,x3) (BSWAPL <v.Type> (MOVLloadidx1 <v.Type> [i-3] {s} p idx mem))
  1609  
  1610  (ORQ o5:(ORQ o4:(ORQ o3:(ORQ o2:(ORQ o1:(ORQ o0:(ORQ
  1611                         x0:(MOVBload [i] {s} p mem)
  1612      s0:(SHLQconst [8]  x1:(MOVBload [i-1] {s} p mem)))
  1613      s1:(SHLQconst [16] x2:(MOVBload [i-2] {s} p mem)))
  1614      s2:(SHLQconst [24] x3:(MOVBload [i-3] {s} p mem)))
  1615      s3:(SHLQconst [32] x4:(MOVBload [i-4] {s} p mem)))
  1616      s4:(SHLQconst [40] x5:(MOVBload [i-5] {s} p mem)))
  1617      s5:(SHLQconst [48] x6:(MOVBload [i-6] {s} p mem)))
  1618      s6:(SHLQconst [56] x7:(MOVBload [i-7] {s} p mem)))
  1619    && x0.Uses == 1
  1620    && x1.Uses == 1
  1621    && x2.Uses == 1
  1622    && x3.Uses == 1
  1623    && x4.Uses == 1
  1624    && x5.Uses == 1
  1625    && x6.Uses == 1
  1626    && x7.Uses == 1
  1627    && s0.Uses == 1
  1628    && s1.Uses == 1
  1629    && s2.Uses == 1
  1630    && s3.Uses == 1
  1631    && s4.Uses == 1
  1632    && s5.Uses == 1
  1633    && s6.Uses == 1
  1634    && o0.Uses == 1
  1635    && o1.Uses == 1
  1636    && o2.Uses == 1
  1637    && o3.Uses == 1
  1638    && o4.Uses == 1
  1639    && o5.Uses == 1
  1640    && mergePoint(b,x0,x1,x2,x3,x4,x5,x6,x7) != nil
  1641    && clobber(x0)
  1642    && clobber(x1)
  1643    && clobber(x2)
  1644    && clobber(x3)
  1645    && clobber(x4)
  1646    && clobber(x5)
  1647    && clobber(x6)
  1648    && clobber(x7)
  1649    && clobber(s0)
  1650    && clobber(s1)
  1651    && clobber(s2)
  1652    && clobber(s3)
  1653    && clobber(s4)
  1654    && clobber(s5)
  1655    && clobber(s6)
  1656    && clobber(o0)
  1657    && clobber(o1)
  1658    && clobber(o2)
  1659    && clobber(o3)
  1660    && clobber(o4)
  1661    && clobber(o5)
  1662    -> @mergePoint(b,x0,x1,x2,x3,x4,x5,x6,x7) (BSWAPQ <v.Type> (MOVQload [i-7] {s} p mem))
  1663  
  1664  (ORQ o5:(ORQ o4:(ORQ o3:(ORQ o2:(ORQ o1:(ORQ o0:(ORQ
  1665                         x0:(MOVBloadidx1 [i] {s} p idx mem)
  1666      s0:(SHLQconst [8]  x1:(MOVBloadidx1 [i-1] {s} p idx mem)))
  1667      s1:(SHLQconst [16] x2:(MOVBloadidx1 [i-2] {s} p idx mem)))
  1668      s2:(SHLQconst [24] x3:(MOVBloadidx1 [i-3] {s} p idx mem)))
  1669      s3:(SHLQconst [32] x4:(MOVBloadidx1 [i-4] {s} p idx mem)))
  1670      s4:(SHLQconst [40] x5:(MOVBloadidx1 [i-5] {s} p idx mem)))
  1671      s5:(SHLQconst [48] x6:(MOVBloadidx1 [i-6] {s} p idx mem)))
  1672      s6:(SHLQconst [56] x7:(MOVBloadidx1 [i-7] {s} p idx mem)))
  1673    && x0.Uses == 1
  1674    && x1.Uses == 1
  1675    && x2.Uses == 1
  1676    && x3.Uses == 1
  1677    && x4.Uses == 1
  1678    && x5.Uses == 1
  1679    && x6.Uses == 1
  1680    && x7.Uses == 1
  1681    && s0.Uses == 1
  1682    && s1.Uses == 1
  1683    && s2.Uses == 1
  1684    && s3.Uses == 1
  1685    && s4.Uses == 1
  1686    && s5.Uses == 1
  1687    && s6.Uses == 1
  1688    && o0.Uses == 1
  1689    && o1.Uses == 1
  1690    && o2.Uses == 1
  1691    && o3.Uses == 1
  1692    && o4.Uses == 1
  1693    && o5.Uses == 1
  1694    && mergePoint(b,x0,x1,x2,x3,x4,x5,x6,x7) != nil
  1695    && clobber(x0)
  1696    && clobber(x1)
  1697    && clobber(x2)
  1698    && clobber(x3)
  1699    && clobber(x4)
  1700    && clobber(x5)
  1701    && clobber(x6)
  1702    && clobber(x7)
  1703    && clobber(s0)
  1704    && clobber(s1)
  1705    && clobber(s2)
  1706    && clobber(s3)
  1707    && clobber(s4)
  1708    && clobber(s5)
  1709    && clobber(s6)
  1710    && clobber(o0)
  1711    && clobber(o1)
  1712    && clobber(o2)
  1713    && clobber(o3)
  1714    && clobber(o4)
  1715    && clobber(o5)
  1716    -> @mergePoint(b,x0,x1,x2,x3,x4,x5,x6,x7) (BSWAPQ <v.Type> (MOVQloadidx1 <v.Type> [i-7] {s} p idx mem))
  1717  
  1718  // Combine stores + shifts into bswap and larger (unaligned) stores
  1719  (MOVBstore [i] {s} p w
  1720    x2:(MOVBstore [i-1] {s} p (SHRLconst [8] w)
  1721    x1:(MOVBstore [i-2] {s} p (SHRLconst [16] w)
  1722    x0:(MOVBstore [i-3] {s} p (SHRLconst [24] w) mem))))
  1723    && x0.Uses == 1
  1724    && x1.Uses == 1
  1725    && x2.Uses == 1
  1726    && clobber(x0)
  1727    && clobber(x1)
  1728    && clobber(x2)
  1729    -> (MOVLstore [i-3] {s} p (BSWAPL <w.Type> w) mem)
  1730  
  1731  (MOVBstore [i] {s} p w
  1732    x6:(MOVBstore [i-1] {s} p (SHRQconst [8] w)
  1733    x5:(MOVBstore [i-2] {s} p (SHRQconst [16] w)
  1734    x4:(MOVBstore [i-3] {s} p (SHRQconst [24] w)
  1735    x3:(MOVBstore [i-4] {s} p (SHRQconst [32] w)
  1736    x2:(MOVBstore [i-5] {s} p (SHRQconst [40] w)
  1737    x1:(MOVBstore [i-6] {s} p (SHRQconst [48] w)
  1738    x0:(MOVBstore [i-7] {s} p (SHRQconst [56] w) mem))))))))
  1739    && x0.Uses == 1
  1740    && x1.Uses == 1
  1741    && x2.Uses == 1
  1742    && x3.Uses == 1
  1743    && x4.Uses == 1
  1744    && x5.Uses == 1
  1745    && x6.Uses == 1
  1746    && clobber(x0)
  1747    && clobber(x1)
  1748    && clobber(x2)
  1749    && clobber(x3)
  1750    && clobber(x4)
  1751    && clobber(x5)
  1752    && clobber(x6)
  1753    -> (MOVQstore [i-7] {s} p (BSWAPQ <w.Type> w) mem)
  1754  
  1755  // Combine constant stores into larger (unaligned) stores.
  1756  (MOVBstoreconst [c] {s} p x:(MOVBstoreconst [a] {s} p mem))
  1757    && x.Uses == 1
  1758    && ValAndOff(a).Off() + 1 == ValAndOff(c).Off()
  1759    && clobber(x)
  1760    -> (MOVWstoreconst [makeValAndOff(ValAndOff(a).Val()&0xff | ValAndOff(c).Val()<<8, ValAndOff(a).Off())] {s} p mem)
  1761  (MOVWstoreconst [c] {s} p x:(MOVWstoreconst [a] {s} p mem))
  1762    && x.Uses == 1
  1763    && ValAndOff(a).Off() + 2 == ValAndOff(c).Off()
  1764    && clobber(x)
  1765    -> (MOVLstoreconst [makeValAndOff(ValAndOff(a).Val()&0xffff | ValAndOff(c).Val()<<16, ValAndOff(a).Off())] {s} p mem)
  1766  (MOVLstoreconst [c] {s} p x:(MOVLstoreconst [a] {s} p mem))
  1767    && x.Uses == 1
  1768    && ValAndOff(a).Off() + 4 == ValAndOff(c).Off()
  1769    && clobber(x)
  1770    -> (MOVQstore [ValAndOff(a).Off()] {s} p (MOVQconst [ValAndOff(a).Val()&0xffffffff | ValAndOff(c).Val()<<32]) mem)
  1771  
  1772  (MOVBstoreconstidx1 [c] {s} p i x:(MOVBstoreconstidx1 [a] {s} p i mem))
  1773    && x.Uses == 1
  1774    && ValAndOff(a).Off() + 1 == ValAndOff(c).Off()
  1775    && clobber(x)
  1776    -> (MOVWstoreconstidx1 [makeValAndOff(ValAndOff(a).Val()&0xff | ValAndOff(c).Val()<<8, ValAndOff(a).Off())] {s} p i mem)
  1777  (MOVWstoreconstidx1 [c] {s} p i x:(MOVWstoreconstidx1 [a] {s} p i mem))
  1778    && x.Uses == 1
  1779    && ValAndOff(a).Off() + 2 == ValAndOff(c).Off()
  1780    && clobber(x)
  1781    -> (MOVLstoreconstidx1 [makeValAndOff(ValAndOff(a).Val()&0xffff | ValAndOff(c).Val()<<16, ValAndOff(a).Off())] {s} p i mem)
  1782  (MOVLstoreconstidx1 [c] {s} p i x:(MOVLstoreconstidx1 [a] {s} p i mem))
  1783    && x.Uses == 1
  1784    && ValAndOff(a).Off() + 4 == ValAndOff(c).Off()
  1785    && clobber(x)
  1786    -> (MOVQstoreidx1 [ValAndOff(a).Off()] {s} p i (MOVQconst [ValAndOff(a).Val()&0xffffffff | ValAndOff(c).Val()<<32]) mem)
  1787  
  1788  (MOVWstoreconstidx2 [c] {s} p i x:(MOVWstoreconstidx2 [a] {s} p i mem))
  1789    && x.Uses == 1
  1790    && ValAndOff(a).Off() + 2 == ValAndOff(c).Off()
  1791    && clobber(x)
  1792    -> (MOVLstoreconstidx1 [makeValAndOff(ValAndOff(a).Val()&0xffff | ValAndOff(c).Val()<<16, ValAndOff(a).Off())] {s} p (SHLQconst <i.Type> [1] i) mem)
  1793  (MOVLstoreconstidx4 [c] {s} p i x:(MOVLstoreconstidx4 [a] {s} p i mem))
  1794    && x.Uses == 1
  1795    && ValAndOff(a).Off() + 4 == ValAndOff(c).Off()
  1796    && clobber(x)
  1797    -> (MOVQstoreidx1 [ValAndOff(a).Off()] {s} p (SHLQconst <i.Type> [2] i) (MOVQconst [ValAndOff(a).Val()&0xffffffff | ValAndOff(c).Val()<<32]) mem)
  1798  
  1799  // Combine stores into larger (unaligned) stores.
  1800  (MOVBstore [i] {s} p (SHRQconst [8] w) x:(MOVBstore [i-1] {s} p w mem))
  1801    && x.Uses == 1
  1802    && clobber(x)
  1803    -> (MOVWstore [i-1] {s} p w mem)
  1804  (MOVBstore [i] {s} p (SHRQconst [j] w) x:(MOVBstore [i-1] {s} p w0:(SHRQconst [j-8] w) mem))
  1805    && x.Uses == 1
  1806    && clobber(x)
  1807    -> (MOVWstore [i-1] {s} p w0 mem)
  1808  (MOVWstore [i] {s} p (SHRQconst [16] w) x:(MOVWstore [i-2] {s} p w mem))
  1809    && x.Uses == 1
  1810    && clobber(x)
  1811    -> (MOVLstore [i-2] {s} p w mem)
  1812  (MOVWstore [i] {s} p (SHRQconst [j] w) x:(MOVWstore [i-2] {s} p w0:(SHRQconst [j-16] w) mem))
  1813    && x.Uses == 1
  1814    && clobber(x)
  1815    -> (MOVLstore [i-2] {s} p w0 mem)
  1816  (MOVLstore [i] {s} p (SHRQconst [32] w) x:(MOVLstore [i-4] {s} p w mem))
  1817    && x.Uses == 1
  1818    && clobber(x)
  1819    -> (MOVQstore [i-4] {s} p w mem)
  1820  (MOVLstore [i] {s} p (SHRQconst [j] w) x:(MOVLstore [i-4] {s} p w0:(SHRQconst [j-32] w) mem))
  1821    && x.Uses == 1
  1822    && clobber(x)
  1823    -> (MOVQstore [i-4] {s} p w0 mem)
  1824  
  1825  (MOVBstoreidx1 [i] {s} p idx (SHRQconst [8] w) x:(MOVBstoreidx1 [i-1] {s} p idx w mem))
  1826    && x.Uses == 1
  1827    && clobber(x)
  1828    -> (MOVWstoreidx1 [i-1] {s} p idx w mem)
  1829  (MOVBstoreidx1 [i] {s} p idx (SHRQconst [j] w) x:(MOVBstoreidx1 [i-1] {s} p idx w0:(SHRQconst [j-8] w) mem))
  1830    && x.Uses == 1
  1831    && clobber(x)
  1832    -> (MOVWstoreidx1 [i-1] {s} p idx w0 mem)
  1833  (MOVWstoreidx1 [i] {s} p idx (SHRQconst [16] w) x:(MOVWstoreidx1 [i-2] {s} p idx w mem))
  1834    && x.Uses == 1
  1835    && clobber(x)
  1836    -> (MOVLstoreidx1 [i-2] {s} p idx w mem)
  1837  (MOVWstoreidx1 [i] {s} p idx (SHRQconst [j] w) x:(MOVWstoreidx1 [i-2] {s} p idx w0:(SHRQconst [j-16] w) mem))
  1838    && x.Uses == 1
  1839    && clobber(x)
  1840    -> (MOVLstoreidx1 [i-2] {s} p idx w0 mem)
  1841  (MOVLstoreidx1 [i] {s} p idx (SHRQconst [32] w) x:(MOVLstoreidx1 [i-4] {s} p idx w mem))
  1842    && x.Uses == 1
  1843    && clobber(x)
  1844    -> (MOVQstoreidx1 [i-4] {s} p idx w mem)
  1845  (MOVLstoreidx1 [i] {s} p idx (SHRQconst [j] w) x:(MOVLstoreidx1 [i-4] {s} p idx w0:(SHRQconst [j-32] w) mem))
  1846    && x.Uses == 1
  1847    && clobber(x)
  1848    -> (MOVQstoreidx1 [i-4] {s} p idx w0 mem)
  1849  
  1850  (MOVWstoreidx2 [i] {s} p idx (SHRQconst [16] w) x:(MOVWstoreidx2 [i-2] {s} p idx w mem))
  1851    && x.Uses == 1
  1852    && clobber(x)
  1853    -> (MOVLstoreidx1 [i-2] {s} p (SHLQconst <idx.Type> [1] idx) w mem)
  1854  (MOVWstoreidx2 [i] {s} p idx (SHRQconst [j] w) x:(MOVWstoreidx2 [i-2] {s} p idx w0:(SHRQconst [j-16] w) mem))
  1855    && x.Uses == 1
  1856    && clobber(x)
  1857    -> (MOVLstoreidx1 [i-2] {s} p (SHLQconst <idx.Type> [1] idx) w0 mem)
  1858  (MOVLstoreidx4 [i] {s} p idx (SHRQconst [32] w) x:(MOVLstoreidx4 [i-4] {s} p idx w mem))
  1859    && x.Uses == 1
  1860    && clobber(x)
  1861    -> (MOVQstoreidx1 [i-4] {s} p (SHLQconst <idx.Type> [2] idx) w mem)
  1862  (MOVLstoreidx4 [i] {s} p idx (SHRQconst [j] w) x:(MOVLstoreidx4 [i-4] {s} p idx w0:(SHRQconst [j-32] w) mem))
  1863    && x.Uses == 1
  1864    && clobber(x)
  1865    -> (MOVQstoreidx1 [i-4] {s} p (SHLQconst <idx.Type> [2] idx) w0 mem)
  1866  
  1867  // amd64p32 rules
  1868  // same as the rules above, but with 32 instead of 64 bit pointer arithmetic.
  1869  // LEAQ,ADDQ -> LEAL,ADDL
  1870  (ADDLconst [c] (LEAL [d] {s} x)) && is32Bit(c+d) -> (LEAL [c+d] {s} x)
  1871  (LEAL [c] {s} (ADDLconst [d] x)) && is32Bit(c+d) -> (LEAL [c+d] {s} x)
  1872  
  1873  (MOVQload  [off1] {sym1} (LEAL [off2] {sym2} base) mem) && canMergeSym(sym1, sym2) ->
  1874  	(MOVQload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1875  (MOVLload  [off1] {sym1} (LEAL [off2] {sym2} base) mem) && canMergeSym(sym1, sym2) ->
  1876  	(MOVLload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1877  (MOVWload  [off1] {sym1} (LEAL [off2] {sym2} base) mem) && canMergeSym(sym1, sym2) ->
  1878  	(MOVWload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1879  (MOVBload  [off1] {sym1} (LEAL [off2] {sym2} base) mem) && canMergeSym(sym1, sym2) ->
  1880  	(MOVBload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
  1881  
  1882  (MOVQstore  [off1] {sym1} (LEAL [off2] {sym2} base) val mem) && canMergeSym(sym1, sym2) ->
  1883  	(MOVQstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1884  (MOVLstore  [off1] {sym1} (LEAL [off2] {sym2} base) val mem) && canMergeSym(sym1, sym2) ->
  1885  	(MOVLstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1886  (MOVWstore  [off1] {sym1} (LEAL [off2] {sym2} base) val mem) && canMergeSym(sym1, sym2) ->
  1887  	(MOVWstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1888  (MOVBstore  [off1] {sym1} (LEAL [off2] {sym2} base) val mem) && canMergeSym(sym1, sym2) ->
  1889  	(MOVBstore  [off1+off2] {mergeSym(sym1,sym2)} base val mem)
  1890  
  1891  (MOVQstoreconst [sc] {sym1} (LEAL [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  1892  	(MOVQstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  1893  (MOVLstoreconst [sc] {sym1} (LEAL [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  1894  	(MOVLstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  1895  (MOVWstoreconst [sc] {sym1} (LEAL [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  1896  	(MOVWstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  1897  (MOVBstoreconst [sc] {sym1} (LEAL [off] {sym2} ptr) mem) && canMergeSym(sym1, sym2) && ValAndOff(sc).canAdd(off) ->
  1898  	(MOVBstoreconst [ValAndOff(sc).add(off)] {mergeSym(sym1, sym2)} ptr mem)
  1899  
  1900  (MOVQload  [off1] {sym} (ADDLconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVQload  [off1+off2] {sym} ptr mem)
  1901  (MOVLload  [off1] {sym} (ADDLconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVLload  [off1+off2] {sym} ptr mem)
  1902  (MOVWload  [off1] {sym} (ADDLconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVWload  [off1+off2] {sym} ptr mem)
  1903  (MOVBload  [off1] {sym} (ADDLconst [off2] ptr) mem) && is32Bit(off1+off2) -> (MOVBload  [off1+off2] {sym} ptr mem)
  1904  (MOVQstore  [off1] {sym} (ADDLconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVQstore  [off1+off2] {sym} ptr val mem)
  1905  (MOVLstore  [off1] {sym} (ADDLconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVLstore  [off1+off2] {sym} ptr val mem)
  1906  (MOVWstore  [off1] {sym} (ADDLconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVWstore  [off1+off2] {sym} ptr val mem)
  1907  (MOVBstore  [off1] {sym} (ADDLconst [off2] ptr) val mem) && is32Bit(off1+off2) -> (MOVBstore  [off1+off2] {sym} ptr val mem)
  1908  (MOVQstoreconst [sc] {s} (ADDLconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  1909  	(MOVQstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  1910  (MOVLstoreconst [sc] {s} (ADDLconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  1911  	(MOVLstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  1912  (MOVWstoreconst [sc] {s} (ADDLconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  1913  	(MOVWstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  1914  (MOVBstoreconst [sc] {s} (ADDLconst [off] ptr) mem) && ValAndOff(sc).canAdd(off) ->
  1915  	(MOVBstoreconst [ValAndOff(sc).add(off)] {s} ptr mem)
  1916  
  1917  // Merge ADDQconst and LEAQ into atomic loads.
  1918  (MOVQatomicload [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  1919  	(MOVQatomicload [off1+off2] {sym} ptr mem)
  1920  (MOVLatomicload [off1] {sym} (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  1921  	(MOVLatomicload [off1+off2] {sym} ptr mem)
  1922  (MOVQatomicload [off1] {sym1} (LEAQ [off2] {sym2} ptr) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1923  	(MOVQatomicload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
  1924  (MOVLatomicload [off1] {sym1} (LEAQ [off2] {sym2} ptr) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) ->
  1925  	(MOVLatomicload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
  1926  
  1927  // Merge ADDQconst and LEAQ into atomic stores.
  1928  (XCHGQ [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  1929  	(XCHGQ [off1+off2] {sym} val ptr mem)
  1930  (XCHGQ [off1] {sym1} val (LEAQ [off2] {sym2} ptr) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && ptr.Op != OpSB ->
  1931  	(XCHGQ [off1+off2] {mergeSym(sym1,sym2)} val ptr mem)
  1932  (XCHGL [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  1933  	(XCHGL [off1+off2] {sym} val ptr mem)
  1934  (XCHGL [off1] {sym1} val (LEAQ [off2] {sym2} ptr) mem) && is32Bit(off1+off2) && canMergeSym(sym1, sym2) && ptr.Op != OpSB ->
  1935  	(XCHGL [off1+off2] {mergeSym(sym1,sym2)} val ptr mem)
  1936  
  1937  // Merge ADDQconst into atomic adds.
  1938  // TODO: merging LEAQ doesn't work, assembler doesn't like the resulting instructions.
  1939  (XADDQlock [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  1940  	(XADDQlock [off1+off2] {sym} val ptr mem)
  1941  (XADDLlock [off1] {sym} val (ADDQconst [off2] ptr) mem) && is32Bit(off1+off2) ->
  1942  	(XADDLlock [off1+off2] {sym} val ptr mem)
  1943  
  1944  // Merge ADDQconst into atomic compare and swaps.
  1945  // TODO: merging LEAQ doesn't work, assembler doesn't like the resulting instructions.
  1946  (CMPXCHGQlock [off1] {sym} (ADDQconst [off2] ptr) old new_ mem) && is32Bit(off1+off2) ->
  1947  	(CMPXCHGQlock [off1+off2] {sym} ptr old new_ mem)
  1948  (CMPXCHGLlock [off1] {sym} (ADDQconst [off2] ptr) old new_ mem) && is32Bit(off1+off2) ->
  1949  	(CMPXCHGLlock [off1+off2] {sym} ptr old new_ mem)