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

     1  // Copyright 2016 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) -> (ADD  x y)
     7  (AddPtr x y) -> (ADD  x y)
     8  (Add32  x y) -> (ADD x y)
     9  (Add16  x y) -> (ADD x y)
    10  (Add8   x y) -> (ADD x y)
    11  (Add64F x y) -> (FADD x y)
    12  (Add32F x y) -> (FADDS x y)
    13  
    14  (Sub64  x y) -> (SUB  x y)
    15  (SubPtr x y) -> (SUB  x y)
    16  (Sub32  x y) -> (SUB x y)
    17  (Sub16  x y) -> (SUB x y)
    18  (Sub8   x y) -> (SUB x y)
    19  (Sub32F x y) -> (FSUBS x y)
    20  (Sub64F x y) -> (FSUB x y)
    21  
    22  (Mod16 x y) -> (Mod32 (SignExt16to32 x) (SignExt16to32 y))
    23  (Mod16u x y) -> (Mod32u (ZeroExt16to32 x) (ZeroExt16to32 y))
    24  (Mod8 x y) -> (Mod32 (SignExt8to32 x) (SignExt8to32 y))
    25  (Mod8u x y) -> (Mod32u (ZeroExt8to32 x) (ZeroExt8to32 y))
    26  (Mod64 x y) -> (SUB x (MULLD y (DIVD x y)))
    27  (Mod64u x y) -> (SUB x (MULLD y (DIVDU x y)))
    28  (Mod32 x y) -> (SUB x (MULLW y (DIVW x y)))
    29  (Mod32u x y) -> (SUB x (MULLW y (DIVWU x y)))
    30  
    31  // (x + y) / 2 with x>=y -> (x - y) / 2 + y
    32  (Avg64u <t> x y) -> (ADD (SRDconst <t> (SUB <t> x y) [1]) y)
    33  
    34  (Mul64  x y) -> (MULLD  x y)
    35  (Mul32  x y) -> (MULLW  x y)
    36  (Mul16  x y) -> (MULLW x y)
    37  (Mul8   x y) -> (MULLW x y)
    38  
    39  (Div64  x y) -> (DIVD  x y)
    40  (Div64u x y) -> (DIVDU x y)
    41  (Div32  x y) -> (DIVW  x y)
    42  (Div32u x y) -> (DIVWU x y)
    43  (Div16  x y) -> (DIVW  (SignExt16to32 x) (SignExt16to32 y))
    44  (Div16u x y) -> (DIVWU (ZeroExt16to32 x) (ZeroExt16to32 y))
    45  (Div8   x y) -> (DIVW  (SignExt8to32 x) (SignExt8to32 y))
    46  (Div8u  x y) -> (DIVWU (ZeroExt8to32 x) (ZeroExt8to32 y))
    47  
    48  (Hmul64  x y) -> (MULHD  x y)
    49  (Hmul64u  x y) -> (MULHDU x y)
    50  (Hmul32  x y) -> (MULHW  x y)
    51  (Hmul32u  x y) -> (MULHWU x y)
    52  
    53  (Mul32F x y) -> (FMULS x y)
    54  (Mul64F x y) -> (FMUL x y)
    55  
    56  (Div32F x y) -> (FDIVS x y)
    57  (Div64F x y) -> (FDIV x y)
    58  
    59  // Lowering float <-> int
    60  (Cvt32to32F x) -> (FRSP (FCFID (Xi2f64 (SignExt32to64 x))))
    61  (Cvt32to64F x) -> (FCFID (Xi2f64 (SignExt32to64 x)))
    62  (Cvt64to32F x) -> (FRSP (FCFID (Xi2f64 x)))
    63  (Cvt64to64F x) -> (FCFID (Xi2f64 x))
    64  
    65  (Cvt32Fto32 x) -> (Xf2i64 (FCTIWZ x))
    66  (Cvt32Fto64 x) -> (Xf2i64 (FCTIDZ x))
    67  (Cvt64Fto32 x) -> (Xf2i64 (FCTIWZ x))
    68  (Cvt64Fto64 x) -> (Xf2i64 (FCTIDZ x))
    69  
    70  (Cvt32Fto64F x) -> x // Note x will have the wrong type for patterns dependent on Float32/Float64
    71  (Cvt64Fto32F x) -> (FRSP x)
    72  
    73  (Round32F x) -> (LoweredRound32F x)
    74  (Round64F x) -> (LoweredRound64F x)
    75  
    76  (Sqrt x) -> (FSQRT x)
    77  
    78  // Lowering constants
    79  (Const8   [val]) -> (MOVDconst [val])
    80  (Const16  [val]) -> (MOVDconst [val])
    81  (Const32  [val]) -> (MOVDconst [val])
    82  (Const64  [val]) -> (MOVDconst [val])
    83  (Const32F [val]) -> (FMOVSconst [val])
    84  (Const64F [val]) -> (FMOVDconst [val])
    85  (ConstNil) -> (MOVDconst [0])
    86  (ConstBool [b]) -> (MOVDconst [b])
    87  
    88  // Rotate generation
    89  (ADD (SLDconst x [c]) (SRDconst x [d])) && d == 64-c -> (ROTLconst [c] x)
    90  ( OR (SLDconst x [c]) (SRDconst x [d])) && d == 64-c -> (ROTLconst [c] x)
    91  (XOR (SLDconst x [c]) (SRDconst x [d])) && d == 64-c -> (ROTLconst [c] x)
    92  
    93  (ADD (SLWconst x [c]) (SRWconst x [d])) && d == 32-c -> (ROTLWconst [c] x)
    94  ( OR (SLWconst x [c]) (SRWconst x [d])) && d == 32-c -> (ROTLWconst [c] x)
    95  (XOR (SLWconst x [c]) (SRWconst x [d])) && d == 32-c -> (ROTLWconst [c] x)
    96  
    97  (Lsh64x64  x (Const64 [c])) && uint64(c) < 64 -> (SLDconst x [c])
    98  (Rsh64x64  x (Const64 [c])) && uint64(c) < 64 -> (SRADconst x [c])
    99  (Rsh64Ux64 x (Const64 [c])) && uint64(c) < 64 -> (SRDconst x [c])
   100  (Lsh32x64  x (Const64 [c])) && uint64(c) < 32 -> (SLWconst x [c])
   101  (Rsh32x64  x (Const64 [c])) && uint64(c) < 32 -> (SRAWconst x [c])
   102  (Rsh32Ux64 x (Const64 [c])) && uint64(c) < 32 -> (SRWconst x [c])
   103  (Lsh16x64  x (Const64 [c])) && uint64(c) < 16 -> (SLWconst x [c])
   104  (Rsh16x64  x (Const64 [c])) && uint64(c) < 16 -> (SRAWconst (SignExt16to32 x) [c])
   105  (Rsh16Ux64 x (Const64 [c])) && uint64(c) < 16 -> (SRWconst (ZeroExt16to32 x) [c])
   106  (Lsh8x64   x (Const64 [c])) && uint64(c) < 8  -> (SLWconst x [c])
   107  (Rsh8x64   x (Const64 [c])) && uint64(c) < 8  -> (SRAWconst (SignExt8to32  x) [c])
   108  (Rsh8Ux64  x (Const64 [c])) && uint64(c) < 8  -> (SRWconst (ZeroExt8to32  x) [c])
   109  
   110  (Lsh64x32  x (Const64 [c])) && uint32(c) < 64 -> (SLDconst x [c])
   111  (Rsh64x32  x (Const64 [c])) && uint32(c) < 64 -> (SRADconst x [c])
   112  (Rsh64Ux32 x (Const64 [c])) && uint32(c) < 64 -> (SRDconst x [c])
   113  (Lsh32x32  x (Const64 [c])) && uint32(c) < 32 -> (SLWconst x [c])
   114  (Rsh32x32  x (Const64 [c])) && uint32(c) < 32 -> (SRAWconst x [c])
   115  (Rsh32Ux32 x (Const64 [c])) && uint32(c) < 32 -> (SRWconst x [c])
   116  (Lsh16x32  x (Const64 [c])) && uint32(c) < 16 -> (SLWconst x [c])
   117  (Rsh16x32  x (Const64 [c])) && uint32(c) < 16 -> (SRAWconst (SignExt16to32 x) [c])
   118  (Rsh16Ux32 x (Const64 [c])) && uint32(c) < 16 -> (SRWconst (ZeroExt16to32 x) [c])
   119  (Lsh8x32   x (Const64 [c])) && uint32(c) < 8  -> (SLWconst x [c])
   120  (Rsh8x32   x (Const64 [c])) && uint32(c) < 8  -> (SRAWconst (SignExt8to32  x) [c])
   121  (Rsh8Ux32  x (Const64 [c])) && uint32(c) < 8  -> (SRWconst (ZeroExt8to32  x) [c])
   122  
   123  // large constant shifts
   124  (Lsh64x64  _ (Const64 [c])) && uint64(c) >= 64 -> (MOVDconst [0])
   125  (Rsh64Ux64 _ (Const64 [c])) && uint64(c) >= 64 -> (MOVDconst [0])
   126  (Lsh32x64  _ (Const64 [c])) && uint64(c) >= 32 -> (MOVDconst [0])
   127  (Rsh32Ux64 _ (Const64 [c])) && uint64(c) >= 32 -> (MOVDconst [0])
   128  (Lsh16x64  _ (Const64 [c])) && uint64(c) >= 16 -> (MOVDconst [0])
   129  (Rsh16Ux64 _ (Const64 [c])) && uint64(c) >= 16 -> (MOVDconst [0])
   130  (Lsh8x64   _ (Const64 [c])) && uint64(c) >= 8  -> (MOVDconst [0])
   131  (Rsh8Ux64  _ (Const64 [c])) && uint64(c) >= 8  -> (MOVDconst [0])
   132  
   133  // large constant signed right shift, we leave the sign bit
   134  (Rsh64x64 x (Const64 [c])) && uint64(c) >= 64 -> (SRADconst x [63])
   135  (Rsh32x64 x (Const64 [c])) && uint64(c) >= 32 -> (SRAWconst x [63])
   136  (Rsh16x64 x (Const64 [c])) && uint64(c) >= 16 -> (SRAWconst (SignExt16to32 x) [63])
   137  (Rsh8x64  x (Const64 [c])) && uint64(c) >= 8  -> (SRAWconst (SignExt8to32  x) [63])
   138  
   139  // constant shifts
   140  (Lsh64x64  x (MOVDconst [c])) && uint64(c) < 64 -> (SLDconst x [c])
   141  (Rsh64x64  x (MOVDconst [c])) && uint64(c) < 64 -> (SRADconst x [c])
   142  (Rsh64Ux64 x (MOVDconst [c])) && uint64(c) < 64 -> (SRDconst x [c])
   143  (Lsh32x64  x (MOVDconst [c])) && uint64(c) < 32 -> (SLWconst x [c])
   144  (Rsh32x64  x (MOVDconst [c])) && uint64(c) < 32 -> (SRAWconst x [c])
   145  (Rsh32Ux64 x (MOVDconst [c])) && uint64(c) < 32 -> (SRWconst x [c])
   146  (Lsh16x64  x (MOVDconst [c])) && uint64(c) < 16 -> (SLWconst x [c])
   147  (Rsh16x64  x (MOVDconst [c])) && uint64(c) < 16 -> (SRAWconst (SignExt16to32 x) [c])
   148  (Rsh16Ux64 x (MOVDconst [c])) && uint64(c) < 16 -> (SRWconst (ZeroExt16to32 x) [c])
   149  (Lsh8x64   x (MOVDconst [c])) && uint64(c) < 8  -> (SLWconst x [c])
   150  (Rsh8x64   x (MOVDconst [c])) && uint64(c) < 8  -> (SRAWconst (SignExt8to32  x) [c])
   151  (Rsh8Ux64  x (MOVDconst [c])) && uint64(c) < 8  -> (SRWconst (ZeroExt8to32  x) [c])
   152  
   153  (Lsh64x32  x (MOVDconst [c])) && uint32(c) < 64 -> (SLDconst x [c])
   154  (Rsh64x32  x (MOVDconst [c])) && uint32(c) < 64 -> (SRADconst x [c])
   155  (Rsh64Ux32 x (MOVDconst [c])) && uint32(c) < 64 -> (SRDconst x [c])
   156  (Lsh32x32  x (MOVDconst [c])) && uint32(c) < 32 -> (SLWconst x [c])
   157  (Rsh32x32  x (MOVDconst [c])) && uint32(c) < 32 -> (SRAWconst x [c])
   158  (Rsh32Ux32 x (MOVDconst [c])) && uint32(c) < 32 -> (SRWconst x [c])
   159  (Lsh16x32  x (MOVDconst [c])) && uint32(c) < 16 -> (SLWconst x [c])
   160  (Rsh16x32  x (MOVDconst [c])) && uint32(c) < 16 -> (SRAWconst (SignExt16to32 x) [c])
   161  (Rsh16Ux32 x (MOVDconst [c])) && uint32(c) < 16 -> (SRWconst (ZeroExt16to32 x) [c])
   162  (Lsh8x32   x (MOVDconst [c])) && uint32(c) < 8  -> (SLWconst x [c])
   163  (Rsh8x32   x (MOVDconst [c])) && uint32(c) < 8  -> (SRAWconst (SignExt8to32  x) [c])
   164  (Rsh8Ux32  x (MOVDconst [c])) && uint32(c) < 8  -> (SRWconst (ZeroExt8to32  x) [c])
   165  
   166  (Rsh64x64 x y)  -> (SRAD x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] y))))
   167  (Rsh64Ux64 x y) -> (SRD  x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] y))))
   168  (Lsh64x64 x y)  -> (SLD  x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] y))))
   169  
   170  (Rsh32x64 x y)  -> (SRAW x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] y))))
   171  (Rsh32Ux64 x y) -> (SRW  x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] y))))
   172  (Lsh32x64 x y)  -> (SLW  x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] y))))
   173  
   174  (Rsh16x64 x y)  -> (SRAW (SignExt16to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] y))))
   175  (Rsh16Ux64 x y) -> (SRW  (ZeroExt16to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] y))))
   176  (Lsh16x64 x y)  -> (SLW  x                 (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] y))))
   177  
   178  (Rsh8x64 x y)  -> (SRAW (SignExt8to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] y))))
   179  (Rsh8Ux64 x y) -> (SRW  (ZeroExt8to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] y))))
   180  (Lsh8x64 x y)  -> (SLW  x                (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] y))))
   181  
   182  
   183  (Rsh64x32 x y)  -> (SRAD x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] (ZeroExt32to64 y)))))
   184  (Rsh64Ux32 x y) -> (SRD x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] (ZeroExt32to64 y)))))
   185  (Lsh64x32 x y)  -> (SLD x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] (ZeroExt32to64 y)))))
   186  
   187  (Rsh32x32 x y)  -> (SRAW x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] (ZeroExt32to64 y)))))
   188  (Rsh32Ux32 x y) -> (SRW x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] (ZeroExt32to64 y)))))
   189  (Lsh32x32 x y)  -> (SLW x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] (ZeroExt32to64 y)))))
   190  
   191  (Rsh16x32 x y)  -> (SRAW (SignExt16to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] (ZeroExt32to64 y)))))
   192  (Rsh16Ux32 x y) -> (SRW  (ZeroExt16to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] (ZeroExt32to64 y)))))
   193  (Lsh16x32 x y)  -> (SLW  x                 (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] (ZeroExt32to64 y)))))
   194  
   195  (Rsh8x32 x y)  -> (SRAW (SignExt8to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] (ZeroExt32to64 y)))))
   196  (Rsh8Ux32 x y) -> (SRW  (ZeroExt8to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] (ZeroExt32to64 y)))))
   197  (Lsh8x32 x y)  -> (SLW  x                (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] (ZeroExt32to64 y)))))
   198  
   199  
   200  (Rsh64x16 x y)  -> (SRAD x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] (ZeroExt16to64 y)))))
   201  (Rsh64Ux16 x y) -> (SRD x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] (ZeroExt16to64 y)))))
   202  (Lsh64x16 x y)  -> (SLD x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] (ZeroExt16to64 y)))))
   203  
   204  (Rsh32x16 x y)  -> (SRAW x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] (ZeroExt16to64 y)))))
   205  (Rsh32Ux16 x y) -> (SRW x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] (ZeroExt16to64 y)))))
   206  (Lsh32x16 x y)  -> (SLW x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] (ZeroExt16to64 y)))))
   207  
   208  (Rsh16x16 x y)  -> (SRAW (SignExt16to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] (ZeroExt16to64 y)))))
   209  (Rsh16Ux16 x y) -> (SRW  (ZeroExt16to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] (ZeroExt16to64 y)))))
   210  (Lsh16x16 x y)  -> (SLW  x                 (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] (ZeroExt16to64 y)))))
   211  
   212  (Rsh8x16 x y)  -> (SRAW (SignExt8to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] (ZeroExt16to64 y)))))
   213  (Rsh8Ux16 x y) -> (SRW  (ZeroExt8to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] (ZeroExt16to64 y)))))
   214  (Lsh8x16 x y)  -> (SLW  x                (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] (ZeroExt16to64 y)))))
   215  
   216  
   217  (Rsh64x8 x y)  -> (SRAD x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] (ZeroExt8to64 y)))))
   218  (Rsh64Ux8 x y) -> (SRD x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] (ZeroExt8to64 y)))))
   219  (Lsh64x8 x y)  -> (SLD x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-64] (ZeroExt8to64 y)))))
   220  
   221  (Rsh32x8 x y)  -> (SRAW x (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] (ZeroExt8to64 y)))))
   222  (Rsh32Ux8 x y) -> (SRW x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] (ZeroExt8to64 y)))))
   223  (Lsh32x8 x y)  -> (SLW x  (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-32] (ZeroExt8to64 y)))))
   224  
   225  (Rsh16x8 x y)  -> (SRAW (SignExt16to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] (ZeroExt8to64 y)))))
   226  (Rsh16Ux8 x y) -> (SRW  (ZeroExt16to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] (ZeroExt8to64 y)))))
   227  (Lsh16x8 x y)  -> (SLW  x                 (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-16] (ZeroExt8to64 y)))))
   228  
   229  (Rsh8x8 x y)  -> (SRAW (SignExt8to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] (ZeroExt8to64 y)))))
   230  (Rsh8Ux8 x y) -> (SRW  (ZeroExt8to32 x) (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] (ZeroExt8to64 y)))))
   231  (Lsh8x8 x y)  -> (SLW  x                (ORN y <typ.Int64> (MaskIfNotCarry (ADDconstForCarry [-8] (ZeroExt8to64 y)))))
   232  
   233  // Cleaning up shift ops when input is masked
   234  (MaskIfNotCarry (ADDconstForCarry [c] (ANDconst [d] _))) && c < 0 && d > 0 && c + d < 0 -> (MOVDconst [-1])
   235  (ORN x (MOVDconst [-1])) -> x
   236  
   237  // Potentially useful optimizing rewrites.
   238  // (ADDconstForCarry [k] c), k < 0 && (c < 0 || k+c >= 0) -> CarrySet
   239  // (ADDconstForCarry [k] c), K < 0 && (c >= 0 && k+c < 0) -> CarryClear
   240  // (MaskIfNotCarry CarrySet) -> 0
   241  // (MaskIfNotCarry CarrySet) -> -1
   242  
   243  (Addr {sym} base) -> (MOVDaddr {sym} base)
   244  // (Addr {sym} base) -> (ADDconst {sym} base)
   245  (OffPtr [off] ptr) -> (ADD (MOVDconst <typ.Int64> [off]) ptr)
   246  
   247  (Ctz64 x) -> (POPCNTD (ANDN <typ.Int64> (ADDconst <typ.Int64> [-1] x) x))
   248  (Ctz32 x) -> (POPCNTW (MOVWZreg (ANDN <typ.Int> (ADDconst <typ.Int> [-1] x) x)))
   249  
   250  (BitLen64 x) -> (SUB (MOVDconst [64]) (CNTLZD <typ.Int> x))
   251  (BitLen32 x) -> (SUB (MOVDconst [32]) (CNTLZW <typ.Int> x))
   252  
   253  (PopCount64 x) -> (POPCNTD x)
   254  (PopCount32 x) -> (POPCNTW (MOVWZreg x))
   255  (PopCount16 x) -> (POPCNTW (MOVHZreg x))
   256  (PopCount8 x) -> (POPCNTB (MOVBreg x))
   257  
   258  (And64 x y) -> (AND x y)
   259  (And32 x y) -> (AND x y)
   260  (And16 x y) -> (AND x y)
   261  (And8  x y) -> (AND x y)
   262  
   263  (Or64 x y) -> (OR x y)
   264  (Or32 x y) -> (OR x y)
   265  (Or16 x y) -> (OR x y)
   266  (Or8  x y) -> (OR x y)
   267  
   268  (Xor64 x y) -> (XOR x y)
   269  (Xor32 x y) -> (XOR x y)
   270  (Xor16 x y) -> (XOR x y)
   271  (Xor8  x y) -> (XOR x y)
   272  
   273  (Neg64F x) -> (FNEG x)
   274  (Neg32F x) -> (FNEG x)
   275  (Neg64  x) -> (NEG x)
   276  (Neg32  x) -> (NEG x)
   277  (Neg16  x) -> (NEG x)
   278  (Neg8   x) -> (NEG x)
   279  
   280  (Com64 x) -> (NOR x x)
   281  (Com32 x) -> (NOR x x)
   282  (Com16 x) -> (NOR x x)
   283  (Com8  x) -> (NOR x x)
   284  
   285  // Lowering boolean ops
   286  (AndB x y) -> (AND x y)
   287  (OrB x y) -> (OR x y)
   288  (Not x) -> (XORconst [1] x)
   289  
   290  // Use ANDN for AND x NOT y
   291  (AND x (NOR y y)) -> (ANDN x y)
   292  
   293  // Lowering comparisons
   294  (EqB x y)  -> (ANDconst [1] (EQV x y))
   295  // Sign extension dependence on operand sign sets up for sign/zero-extension elision later
   296  (Eq8 x y) && isSigned(x.Type) && isSigned(y.Type) -> (Equal (CMPW (SignExt8to32 x) (SignExt8to32 y)))
   297  (Eq16 x y) && isSigned(x.Type) && isSigned(y.Type) -> (Equal (CMPW (SignExt16to32 x) (SignExt16to32 y)))
   298  (Eq8 x y)  -> (Equal (CMPW (ZeroExt8to32 x) (ZeroExt8to32 y)))
   299  (Eq16 x y) -> (Equal (CMPW (ZeroExt16to32 x) (ZeroExt16to32 y)))
   300  (Eq32 x y) -> (Equal (CMPW x y))
   301  (Eq64 x y) -> (Equal (CMP x y))
   302  (Eq32F x y) -> (Equal (FCMPU x y))
   303  (Eq64F x y) -> (Equal (FCMPU x y))
   304  (EqPtr x y) -> (Equal (CMP x y))
   305  
   306  (NeqB x y)  -> (XOR x y)
   307  // Like Eq8 and Eq16, prefer sign extension likely to enable later elision.
   308  (Neq8 x y) && isSigned(x.Type) && isSigned(y.Type) -> (NotEqual (CMPW (SignExt8to32 x) (SignExt8to32 y)))
   309  (Neq16 x y) && isSigned(x.Type) && isSigned(y.Type) -> (NotEqual (CMPW (SignExt16to32 x) (SignExt16to32 y)))
   310  (Neq8 x y)  -> (NotEqual (CMPW (ZeroExt8to32 x) (ZeroExt8to32 y)))
   311  (Neq16 x y) -> (NotEqual (CMPW (ZeroExt16to32 x) (ZeroExt16to32 y)))
   312  (Neq32 x y) -> (NotEqual (CMPW x y))
   313  (Neq64 x y) -> (NotEqual (CMP x y))
   314  (Neq32F x y) -> (NotEqual (FCMPU x y))
   315  (Neq64F x y) -> (NotEqual (FCMPU x y))
   316  (NeqPtr x y) -> (NotEqual (CMP x y))
   317  
   318  (Less8 x y)  -> (LessThan (CMPW (SignExt8to32 x) (SignExt8to32 y)))
   319  (Less16 x y) -> (LessThan (CMPW (SignExt16to32 x) (SignExt16to32 y)))
   320  (Less32 x y) -> (LessThan (CMPW x y))
   321  (Less64 x y) -> (LessThan (CMP x y))
   322  (Less32F x y) -> (FLessThan (FCMPU x y))
   323  (Less64F x y) -> (FLessThan (FCMPU x y))
   324  
   325  (Less8U x y)  -> (LessThan (CMPWU (ZeroExt8to32 x) (ZeroExt8to32 y)))
   326  (Less16U x y) -> (LessThan (CMPWU (ZeroExt16to32 x) (ZeroExt16to32 y)))
   327  (Less32U x y) -> (LessThan (CMPWU x y))
   328  (Less64U x y) -> (LessThan (CMPU x y))
   329  
   330  (Leq8 x y)  -> (LessEqual (CMPW (SignExt8to32 x) (SignExt8to32 y)))
   331  (Leq16 x y) -> (LessEqual (CMPW (SignExt16to32 x) (SignExt16to32 y)))
   332  (Leq32 x y) -> (LessEqual (CMPW x y))
   333  (Leq64 x y) -> (LessEqual (CMP x y))
   334  (Leq32F x y) -> (FLessEqual (FCMPU x y))
   335  (Leq64F x y) -> (FLessEqual (FCMPU x y))
   336  
   337  (Leq8U x y)  -> (LessEqual (CMPWU (ZeroExt8to32 x) (ZeroExt8to32 y)))
   338  (Leq16U x y) -> (LessEqual (CMPWU (ZeroExt16to32 x) (ZeroExt16to32 y)))
   339  (Leq32U x y) -> (LessEqual (CMPWU x y))
   340  (Leq64U x y) -> (LessEqual (CMPU x y))
   341  
   342  (Greater8 x y)  -> (GreaterThan (CMPW (SignExt8to32 x) (SignExt8to32 y)))
   343  (Greater16 x y) -> (GreaterThan (CMPW (SignExt16to32 x) (SignExt16to32 y)))
   344  (Greater32 x y) -> (GreaterThan (CMPW x y))
   345  (Greater64 x y) -> (GreaterThan (CMP x y))
   346  (Greater32F x y) -> (FGreaterThan (FCMPU x y))
   347  (Greater64F x y) -> (FGreaterThan (FCMPU x y))
   348  
   349  (Greater8U x y)  -> (GreaterThan (CMPWU (ZeroExt8to32 x) (ZeroExt8to32 y)))
   350  (Greater16U x y) -> (GreaterThan (CMPWU (ZeroExt16to32 x) (ZeroExt16to32 y)))
   351  (Greater32U x y) -> (GreaterThan (CMPWU x y))
   352  (Greater64U x y) -> (GreaterThan (CMPU x y))
   353  
   354  (Geq8 x y)  -> (GreaterEqual (CMPW (SignExt8to32 x) (SignExt8to32 y)))
   355  (Geq16 x y) -> (GreaterEqual (CMPW (SignExt16to32 x) (SignExt16to32 y)))
   356  (Geq32 x y) -> (GreaterEqual (CMPW x y))
   357  (Geq64 x y) -> (GreaterEqual (CMP x y))
   358  (Geq32F x y) -> (FGreaterEqual (FCMPU x y))
   359  (Geq64F x y) -> (FGreaterEqual (FCMPU x y))
   360  
   361  (Geq8U x y)  -> (GreaterEqual (CMPWU (ZeroExt8to32 x) (ZeroExt8to32 y)))
   362  (Geq16U x y) -> (GreaterEqual (CMPWU (ZeroExt16to32 x) (ZeroExt16to32 y)))
   363  (Geq32U x y) -> (GreaterEqual (CMPWU x y))
   364  (Geq64U x y) -> (GreaterEqual (CMPU x y))
   365  
   366  // Absorb pseudo-ops into blocks.
   367  (If (Equal cc) yes no) -> (EQ cc yes no)
   368  (If (NotEqual cc) yes no) -> (NE cc yes no)
   369  (If (LessThan cc) yes no) -> (LT cc yes no)
   370  (If (LessEqual cc) yes no) -> (LE cc yes no)
   371  (If (GreaterThan cc) yes no) -> (GT cc yes no)
   372  (If (GreaterEqual cc) yes no) -> (GE cc yes no)
   373  (If (FLessThan cc) yes no) -> (FLT cc yes no)
   374  (If (FLessEqual cc) yes no) -> (FLE cc yes no)
   375  (If (FGreaterThan cc) yes no) -> (FGT cc yes no)
   376  (If (FGreaterEqual cc) yes no) -> (FGE cc yes no)
   377  
   378  (If cond yes no) -> (NE (CMPWconst [0] cond) yes no)
   379  
   380  // Absorb boolean tests into block
   381  (NE (CMPWconst [0] (Equal cc)) yes no) -> (EQ cc yes no)
   382  (NE (CMPWconst [0] (NotEqual cc)) yes no) -> (NE cc yes no)
   383  (NE (CMPWconst [0] (LessThan cc)) yes no) -> (LT cc yes no)
   384  (NE (CMPWconst [0] (LessEqual cc)) yes no) -> (LE cc yes no)
   385  (NE (CMPWconst [0] (GreaterThan cc)) yes no) -> (GT cc yes no)
   386  (NE (CMPWconst [0] (GreaterEqual cc)) yes no) -> (GE cc yes no)
   387  (NE (CMPWconst [0] (FLessThan cc)) yes no) -> (FLT cc yes no)
   388  (NE (CMPWconst [0] (FLessEqual cc)) yes no) -> (FLE cc yes no)
   389  (NE (CMPWconst [0] (FGreaterThan cc)) yes no) -> (FGT cc yes no)
   390  (NE (CMPWconst [0] (FGreaterEqual cc)) yes no) -> (FGE cc yes no)
   391  
   392  // Elide compares of bit tests // TODO need to make both CC and result of ANDCC available.
   393  (EQ (CMPconst [0] (ANDconst [c] x)) yes no) -> (EQ (ANDCCconst [c] x) yes no)
   394  (NE (CMPconst [0] (ANDconst [c] x)) yes no) -> (NE (ANDCCconst [c] x) yes no)
   395  (EQ (CMPWconst [0] (ANDconst [c] x)) yes no) -> (EQ (ANDCCconst [c] x) yes no)
   396  (NE (CMPWconst [0] (ANDconst [c] x)) yes no) -> (NE (ANDCCconst [c] x) yes no)
   397  
   398  // absorb flag constants into branches
   399  (EQ (FlagEQ) yes no) -> (First nil yes no)
   400  (EQ (FlagLT) yes no) -> (First nil no yes)
   401  (EQ (FlagGT) yes no) -> (First nil no yes)
   402  
   403  (NE (FlagEQ) yes no) -> (First nil no yes)
   404  (NE (FlagLT) yes no) -> (First nil yes no)
   405  (NE (FlagGT) yes no) -> (First nil yes no)
   406  
   407  (LT (FlagEQ) yes no) -> (First nil no yes)
   408  (LT (FlagLT) yes no) -> (First nil yes no)
   409  (LT (FlagGT) yes no) -> (First nil no yes)
   410  
   411  (LE (FlagEQ) yes no) -> (First nil yes no)
   412  (LE (FlagLT) yes no) -> (First nil yes no)
   413  (LE (FlagGT) yes no) -> (First nil no yes)
   414  
   415  (GT (FlagEQ) yes no) -> (First nil no yes)
   416  (GT (FlagLT) yes no) -> (First nil no yes)
   417  (GT (FlagGT) yes no) -> (First nil yes no)
   418  
   419  (GE (FlagEQ) yes no) -> (First nil yes no)
   420  (GE (FlagLT) yes no) -> (First nil no yes)
   421  (GE (FlagGT) yes no) -> (First nil yes no)
   422  
   423  // absorb InvertFlags into branches
   424  (LT (InvertFlags cmp) yes no) -> (GT cmp yes no)
   425  (GT (InvertFlags cmp) yes no) -> (LT cmp yes no)
   426  (LE (InvertFlags cmp) yes no) -> (GE cmp yes no)
   427  (GE (InvertFlags cmp) yes no) -> (LE cmp yes no)
   428  (EQ (InvertFlags cmp) yes no) -> (EQ cmp yes no)
   429  (NE (InvertFlags cmp) yes no) -> (NE cmp yes no)
   430  
   431  // constant comparisons
   432  (CMPWconst (MOVDconst [x]) [y]) && int32(x)==int32(y) -> (FlagEQ)
   433  (CMPWconst (MOVDconst [x]) [y]) && int32(x)<int32(y)  -> (FlagLT)
   434  (CMPWconst (MOVDconst [x]) [y]) && int32(x)>int32(y)  -> (FlagGT)
   435  
   436  (CMPconst (MOVDconst [x]) [y]) && int64(x)==int64(y) -> (FlagEQ)
   437  (CMPconst (MOVDconst [x]) [y]) && int64(x)<int64(y)  -> (FlagLT)
   438  (CMPconst (MOVDconst [x]) [y]) && int64(x)>int64(y)  -> (FlagGT)
   439  
   440  (CMPWUconst (MOVDconst [x]) [y]) && int32(x)==int32(y)  -> (FlagEQ)
   441  (CMPWUconst (MOVDconst [x]) [y]) && uint32(x)<uint32(y) -> (FlagLT)
   442  (CMPWUconst (MOVDconst [x]) [y]) && uint32(x)>uint32(y) -> (FlagGT)
   443  
   444  (CMPUconst (MOVDconst [x]) [y]) && int64(x)==int64(y)  -> (FlagEQ)
   445  (CMPUconst (MOVDconst [x]) [y]) && uint64(x)<uint64(y) -> (FlagLT)
   446  (CMPUconst (MOVDconst [x]) [y]) && uint64(x)>uint64(y) -> (FlagGT)
   447  
   448  // other known comparisons
   449  //(CMPconst (MOVBUreg _) [c]) && 0xff < c -> (FlagLT)
   450  //(CMPconst (MOVHUreg _) [c]) && 0xffff < c -> (FlagLT)
   451  //(CMPconst (ANDconst _ [m]) [n]) && 0 <= int32(m) && int32(m) < int32(n) -> (FlagLT)
   452  //(CMPconst (SRLconst _ [c]) [n]) && 0 <= n && 0 < c && c <= 32 && (1<<uint32(32-c)) <= uint32(n) -> (FlagLT)
   453  
   454  // absorb flag constants into boolean values
   455  (Equal (FlagEQ)) -> (MOVDconst [1])
   456  (Equal (FlagLT)) -> (MOVDconst [0])
   457  (Equal (FlagGT)) -> (MOVDconst [0])
   458  
   459  (NotEqual (FlagEQ)) -> (MOVDconst [0])
   460  (NotEqual (FlagLT)) -> (MOVDconst [1])
   461  (NotEqual (FlagGT)) -> (MOVDconst [1])
   462  
   463  (LessThan (FlagEQ)) -> (MOVDconst [0])
   464  (LessThan (FlagLT)) -> (MOVDconst [1])
   465  (LessThan (FlagGT)) -> (MOVDconst [0])
   466  
   467  (LessEqual (FlagEQ)) -> (MOVDconst [1])
   468  (LessEqual (FlagLT)) -> (MOVDconst [1])
   469  (LessEqual (FlagGT)) -> (MOVDconst [0])
   470  
   471  (GreaterThan (FlagEQ)) -> (MOVDconst [0])
   472  (GreaterThan (FlagLT)) -> (MOVDconst [0])
   473  (GreaterThan (FlagGT)) -> (MOVDconst [1])
   474  
   475  (GreaterEqual (FlagEQ)) -> (MOVDconst [1])
   476  (GreaterEqual (FlagLT)) -> (MOVDconst [0])
   477  (GreaterEqual (FlagGT)) -> (MOVDconst [1])
   478  
   479  // absorb InvertFlags into boolean values
   480  (Equal (InvertFlags x)) -> (Equal x)
   481  (NotEqual (InvertFlags x)) -> (NotEqual x)
   482  (LessThan (InvertFlags x)) -> (GreaterThan x)
   483  (GreaterThan (InvertFlags x)) -> (LessThan x)
   484  (LessEqual (InvertFlags x)) -> (GreaterEqual x)
   485  (GreaterEqual (InvertFlags x)) -> (LessEqual x)
   486  
   487  // Lowering loads
   488  (Load <t> ptr mem) && (is64BitInt(t) || isPtr(t)) -> (MOVDload ptr mem)
   489  (Load <t> ptr mem) && is32BitInt(t) && isSigned(t) -> (MOVWload ptr mem)
   490  (Load <t> ptr mem) && is32BitInt(t) && !isSigned(t) -> (MOVWZload ptr mem)
   491  (Load <t> ptr mem) && is16BitInt(t) && isSigned(t) -> (MOVHload ptr mem)
   492  (Load <t> ptr mem) && is16BitInt(t) && !isSigned(t) -> (MOVHZload ptr mem)
   493  (Load <t> ptr mem) && t.IsBoolean() -> (MOVBZload ptr mem)
   494  (Load <t> ptr mem) && is8BitInt(t) && isSigned(t) -> (MOVBreg (MOVBZload ptr mem)) // PPC has no signed-byte load.
   495  (Load <t> ptr mem) && is8BitInt(t) && !isSigned(t) -> (MOVBZload ptr mem)
   496  
   497  (Load <t> ptr mem) && is32BitFloat(t) -> (FMOVSload ptr mem)
   498  (Load <t> ptr mem) && is64BitFloat(t) -> (FMOVDload ptr mem)
   499  
   500  (Store {t} ptr val mem) && t.(*types.Type).Size() == 8 && is64BitFloat(val.Type) -> (FMOVDstore ptr val mem)
   501  (Store {t} ptr val mem) && t.(*types.Type).Size() == 8 && is32BitFloat(val.Type) -> (FMOVDstore ptr val mem) // glitch from (Cvt32Fto64F x) -> x -- type is wrong
   502  (Store {t} ptr val mem) && t.(*types.Type).Size() == 4 && is32BitFloat(val.Type) -> (FMOVSstore ptr val mem)
   503  (Store {t} ptr val mem) && t.(*types.Type).Size() == 8 && (is64BitInt(val.Type) || isPtr(val.Type)) -> (MOVDstore ptr val mem)
   504  (Store {t} ptr val mem) && t.(*types.Type).Size() == 4 && is32BitInt(val.Type) -> (MOVWstore ptr val mem)
   505  (Store {t} ptr val mem) && t.(*types.Type).Size() == 2 -> (MOVHstore ptr val mem)
   506  (Store {t} ptr val mem) && t.(*types.Type).Size() == 1 -> (MOVBstore ptr val mem)
   507  
   508  // Using Zero instead of LoweredZero allows the
   509  // target address to be folded where possible.
   510  (Zero [0] _ mem) -> mem
   511  (Zero [1] destptr mem) -> (MOVBstorezero destptr mem)
   512  (Zero [2] destptr mem) ->
   513  	(MOVHstorezero destptr mem)
   514  (Zero [3] destptr mem) ->
   515  	(MOVBstorezero [2] destptr
   516  		(MOVHstorezero destptr mem))
   517  (Zero [4] destptr mem) ->
   518  	(MOVWstorezero destptr mem)
   519  (Zero [5] destptr mem) ->
   520  	(MOVBstorezero [4] destptr
   521          	(MOVWstorezero destptr mem))
   522  (Zero [6] destptr mem) ->
   523  	(MOVHstorezero [4] destptr
   524  		(MOVWstorezero destptr mem))
   525  (Zero [7] destptr mem) ->
   526  	(MOVBstorezero [6] destptr
   527  		(MOVHstorezero [4] destptr
   528  			(MOVWstorezero destptr mem)))
   529  
   530  // MOVD for store with DS must have offsets that are multiple of 4
   531  (Zero [8] {t} destptr mem) && t.(*types.Type).Alignment()%4 == 0 ->
   532          (MOVDstorezero destptr mem)
   533  (Zero [8] destptr mem) ->
   534          (MOVWstorezero [4] destptr
   535                  (MOVWstorezero [0] destptr mem))
   536  // Handle these cases only if aligned properly, otherwise use general case below
   537  (Zero [12] {t} destptr mem) && t.(*types.Type).Alignment()%4 == 0 ->
   538          (MOVWstorezero [8] destptr
   539                  (MOVDstorezero [0] destptr mem))
   540  (Zero [16] {t} destptr mem) && t.(*types.Type).Alignment()%4 == 0 ->
   541         (MOVDstorezero [8] destptr
   542                  (MOVDstorezero [0] destptr mem))
   543  (Zero [24] {t} destptr mem) && t.(*types.Type).Alignment()%4 == 0 ->
   544         (MOVDstorezero [16] destptr
   545                 (MOVDstorezero [8] destptr
   546                         (MOVDstorezero [0] destptr mem)))
   547  (Zero [32] {t} destptr mem) && t.(*types.Type).Alignment()%4 == 0 ->
   548         (MOVDstorezero [24] destptr
   549                 (MOVDstorezero [16] destptr
   550                         (MOVDstorezero [8] destptr
   551                                 (MOVDstorezero [0] destptr mem))))
   552  
   553  // Handle cases not handled above
   554  (Zero [s] ptr mem) -> (LoweredZero [s] ptr mem)
   555  
   556  // moves
   557  // Only the MOVD and MOVW instructions require 4 byte
   558  // alignment in the offset field.  The other MOVx instructions
   559  // allow any alignment.
   560  (Move [0] _ _ mem) -> mem
   561  (Move [1] dst src mem) -> (MOVBstore dst (MOVBZload src mem) mem)
   562  (Move [2] dst src mem) ->
   563          (MOVHstore dst (MOVHZload src mem) mem)
   564  (Move [4] dst src mem) ->
   565  	(MOVWstore dst (MOVWZload src mem) mem)
   566  // MOVD for load and store must have offsets that are multiple of 4
   567  (Move [8] {t} dst src mem) && t.(*types.Type).Alignment()%4 == 0 ->
   568  	(MOVDstore dst (MOVDload src mem) mem)
   569  (Move [8] dst src mem) ->
   570  	(MOVWstore [4] dst (MOVWZload [4] src mem)
   571  		(MOVWstore dst (MOVWZload src mem) mem))
   572  (Move [3] dst src mem) ->
   573          (MOVBstore [2] dst (MOVBZload [2] src mem)
   574                  (MOVHstore dst (MOVHload src mem) mem))
   575  (Move [5] dst src mem) ->
   576          (MOVBstore [4] dst (MOVBZload [4] src mem)
   577                  (MOVWstore dst (MOVWZload src mem) mem))
   578  (Move [6] dst src mem) ->
   579          (MOVHstore [4] dst (MOVHZload [4] src mem)
   580                  (MOVWstore dst (MOVWZload src mem) mem))
   581  (Move [7] dst src mem) ->
   582          (MOVBstore [6] dst (MOVBZload [6] src mem)
   583                  (MOVHstore [4] dst (MOVHZload [4] src mem)
   584                          (MOVWstore dst (MOVWZload src mem) mem)))
   585  
   586  // Large move uses a loop. Since the address is computed and the
   587  // offset is zero, any alignment can be used.
   588  (Move [s] dst src mem) && s > 8 ->
   589          (LoweredMove [s] dst src mem)
   590  
   591  // Calls
   592  // Lowering calls
   593  (StaticCall [argwid] {target} mem) -> (CALLstatic [argwid] {target} mem)
   594  (ClosureCall [argwid] entry closure mem) -> (CALLclosure [argwid] entry closure mem)
   595  (InterCall [argwid] entry mem) -> (CALLinter [argwid] entry mem)
   596  
   597  // Miscellaneous
   598  (Convert <t> x mem) -> (MOVDconvert <t> x mem)
   599  (GetClosurePtr) -> (LoweredGetClosurePtr)
   600  (IsNonNil ptr) -> (NotEqual (CMPconst [0] ptr))
   601  (IsInBounds idx len) -> (LessThan (CMPU idx len))
   602  (IsSliceInBounds idx len) -> (LessEqual (CMPU idx len))
   603  (NilCheck ptr mem) -> (LoweredNilCheck ptr mem)
   604  
   605  // Optimizations
   606  // Note that PPC "logical" immediates come in 0:15 and 16:31 unsigned immediate forms,
   607  // so ORconst, XORconst easily expand into a pair.
   608  
   609  // Include very-large constants in the const-const case.
   610  (AND (MOVDconst [c]) (MOVDconst [d])) -> (MOVDconst [c&d])
   611  (OR (MOVDconst [c]) (MOVDconst [d])) -> (MOVDconst [c|d])
   612  (XOR (MOVDconst [c]) (MOVDconst [d])) -> (MOVDconst [c^d])
   613  
   614  // Discover consts
   615  (AND x (MOVDconst [c])) && isU16Bit(c) -> (ANDconst [c] x)
   616  (XOR x (MOVDconst [c])) && isU32Bit(c) -> (XORconst [c] x)
   617  (OR x (MOVDconst [c])) && isU32Bit(c) -> (ORconst [c] x)
   618  
   619  // Simplify consts
   620  (ANDconst [c] (ANDconst [d] x)) -> (ANDconst [c&d] x)
   621  (ORconst [c] (ORconst [d] x)) -> (ORconst [c|d] x)
   622  (XORconst [c] (XORconst [d] x)) -> (XORconst [c^d] x)
   623  (ANDconst [-1] x) -> x
   624  (ANDconst [0] _) -> (MOVDconst [0])
   625  (XORconst [0] x) -> x
   626  (ORconst [-1] _) -> (MOVDconst [-1])
   627  (ORconst [0] x) -> x
   628  
   629  // zero-extend of small and -> small and
   630  (MOVBZreg y:(ANDconst [c] _)) && uint64(c) <= 0xFF -> y
   631  (MOVHZreg y:(ANDconst [c] _)) && uint64(c) <= 0xFFFF -> y
   632  (MOVWZreg y:(ANDconst [c] _)) && uint64(c) <= 0xFFFFFFFF -> y
   633  (MOVWZreg y:(AND (MOVDconst [c]) _)) && uint64(c) <= 0xFFFFFFFF -> y
   634  
   635  // sign extend of small-positive and -> small-positive-and
   636  (MOVBreg y:(ANDconst [c] _)) && uint64(c) <= 0x7F -> y
   637  (MOVHreg y:(ANDconst [c] _)) && uint64(c) <= 0x7FFF -> y
   638  (MOVWreg y:(ANDconst [c] _)) && uint64(c) <= 0xFFFF -> y // 0xFFFF is largest immediate constant, when regarded as 32-bit is > 0
   639  (MOVWreg y:(AND (MOVDconst [c]) _)) && uint64(c) <= 0x7FFFFFFF -> y
   640  
   641  // small and of zero-extend -> either zero-extend or small and
   642    // degenerate-and
   643  (ANDconst [c] y:(MOVBZreg _)) && c&0xFF == 0xFF -> y
   644  (ANDconst [c] y:(MOVHZreg _))  && c&0xFFFF == 0xFFFF -> y
   645  (ANDconst [c] y:(MOVWZreg _))  && c&0xFFFFFFFF == 0xFFFFFFFF -> y
   646    // normal case
   647  (ANDconst [c] (MOVBZreg x)) -> (ANDconst [c&0xFF] x)
   648  (ANDconst [c] (MOVHZreg x)) -> (ANDconst [c&0xFFFF] x)
   649  (ANDconst [c] (MOVWZreg x)) -> (ANDconst [c&0xFFFFFFFF] x)
   650  
   651  // Various redundant zero/sign extension combinations.
   652  (MOVBZreg y:(MOVBZreg _)) -> y  // repeat
   653  (MOVBreg y:(MOVBreg _)) -> y // repeat
   654  (MOVBreg (MOVBZreg x)) -> (MOVBreg x)
   655  (MOVBZreg (MOVBreg x)) -> (MOVBZreg x)
   656  
   657  // H - there are more combinations than these
   658  
   659  (MOVHZreg y:(MOVHZreg _)) -> y // repeat
   660  (MOVHZreg y:(MOVBZreg _)) -> y // wide of narrow
   661  
   662  (MOVHreg y:(MOVHreg _)) -> y // repeat
   663  (MOVHreg y:(MOVBreg _)) -> y // wide of narrow
   664  
   665  (MOVHreg y:(MOVHZreg x)) -> (MOVHreg x)
   666  (MOVHZreg y:(MOVHreg x)) -> (MOVHZreg x)
   667  
   668  // W - there are more combinations than these
   669  
   670  (MOVWZreg y:(MOVWZreg _)) -> y // repeat
   671  (MOVWZreg y:(MOVHZreg _)) -> y // wide of narrow
   672  (MOVWZreg y:(MOVBZreg _)) -> y // wide of narrow
   673  
   674  (MOVWreg y:(MOVWreg _)) -> y // repeat
   675  (MOVWreg y:(MOVHreg _)) -> y // wide of narrow
   676  (MOVWreg y:(MOVBreg _)) -> y // wide of narrow
   677  
   678  (MOVWreg y:(MOVWZreg x)) -> (MOVWreg x)
   679  (MOVWZreg y:(MOVWreg x)) -> (MOVWZreg x)
   680  
   681  // Arithmetic constant ops
   682  
   683  (ADD x (MOVDconst [c])) && is32Bit(c) -> (ADDconst [c] x)
   684  (ADDconst [c] (ADDconst [d] x)) && is32Bit(c+d) -> (ADDconst [c+d] x)
   685  (ADDconst [0] x) -> x
   686  (SUB x (MOVDconst [c])) && is32Bit(-c) -> (ADDconst [-c] x)
   687  // TODO deal with subtract-from-const
   688  
   689  (ADDconst [c] (MOVDaddr [d] {sym} x)) -> (MOVDaddr [c+d] {sym} x)
   690  
   691  // Fold offsets for stores.
   692  (MOVDstore [off1] {sym} (ADDconst [off2] x) val mem) && is16Bit(off1+off2) -> (MOVDstore [off1+off2] {sym} x val mem)
   693  (MOVWstore [off1] {sym} (ADDconst [off2] x) val mem) && is16Bit(off1+off2) -> (MOVWstore [off1+off2] {sym} x val mem)
   694  (MOVHstore [off1] {sym} (ADDconst [off2] x) val mem) && is16Bit(off1+off2) -> (MOVHstore [off1+off2] {sym} x val mem)
   695  (MOVBstore [off1] {sym} (ADDconst [off2] x) val mem) && is16Bit(off1+off2) -> (MOVBstore [off1+off2] {sym} x val mem)
   696  
   697  (FMOVSstore [off1] {sym} (ADDconst [off2] ptr) val mem) && is16Bit(off1+off2) -> (FMOVSstore [off1+off2] {sym} ptr val mem)
   698  (FMOVDstore [off1] {sym} (ADDconst [off2] ptr) val mem) && is16Bit(off1+off2) -> (FMOVDstore [off1+off2] {sym} ptr val mem)
   699  
   700  (MOVBstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   701          (MOVBstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   702  (MOVHstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   703          (MOVHstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   704  (MOVWstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   705          (MOVWstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   706  (MOVDstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   707          (MOVDstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   708  
   709  (FMOVSstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   710          (FMOVSstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   711  (FMOVDstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   712          (FMOVDstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   713  
   714  (MOVBZload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   715          (MOVBZload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   716  (MOVHload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   717          (MOVHload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   718  (MOVHZload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   719          (MOVHZload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   720  (MOVWload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   721          (MOVWload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   722  (MOVWZload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   723          (MOVWZload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   724  (MOVDload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   725          (MOVDload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   726  (FMOVSload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   727          (FMOVSload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   728  (FMOVDload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   729          (FMOVDload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   730  
   731  // Fold offsets for loads.
   732  (FMOVSload [off1] {sym} (ADDconst [off2] ptr) mem) && is16Bit(off1+off2) -> (FMOVSload [off1+off2] {sym} ptr mem)
   733  (FMOVDload [off1] {sym} (ADDconst [off2] ptr) mem) && is16Bit(off1+off2) -> (FMOVDload [off1+off2] {sym} ptr mem)
   734  
   735  (MOVDload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVDload [off1+off2] {sym} x mem)
   736  (MOVWload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVWload [off1+off2] {sym} x mem)
   737  (MOVWZload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVWZload [off1+off2] {sym} x mem)
   738  (MOVHload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVHload [off1+off2] {sym} x mem)
   739  (MOVHZload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVHZload [off1+off2] {sym} x mem)
   740  (MOVBZload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVBZload [off1+off2] {sym} x mem)
   741  
   742  // Store of zero -> storezero
   743  (MOVDstore [off] {sym} ptr (MOVDconst [c]) mem) && c == 0 -> (MOVDstorezero [off] {sym} ptr mem)
   744  (MOVWstore [off] {sym} ptr (MOVDconst [c]) mem) && c == 0 -> (MOVWstorezero [off] {sym} ptr mem)
   745  (MOVHstore [off] {sym} ptr (MOVDconst [c]) mem) && c == 0 -> (MOVHstorezero [off] {sym} ptr mem)
   746  (MOVBstore [off] {sym} ptr (MOVDconst [c]) mem) && c == 0 -> (MOVBstorezero [off] {sym} ptr mem)
   747  
   748  // Fold offsets for storezero
   749  (MOVDstorezero [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) ->
   750      (MOVDstorezero [off1+off2] {sym} x mem)
   751  (MOVWstorezero [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) ->
   752      (MOVWstorezero [off1+off2] {sym} x mem)
   753  (MOVHstorezero [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) ->
   754      (MOVHstorezero [off1+off2] {sym} x mem)
   755  (MOVBstorezero [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) ->
   756      (MOVBstorezero [off1+off2] {sym} x mem)
   757  
   758  // Fold symbols into storezero
   759  (MOVDstorezero [off1] {sym1} (MOVDaddr [off2] {sym2} x) mem) && canMergeSym(sym1,sym2) ->
   760      (MOVDstorezero [off1+off2] {mergeSym(sym1,sym2)} x mem)
   761  (MOVWstorezero [off1] {sym1} (MOVDaddr [off2] {sym2} x) mem) && canMergeSym(sym1,sym2) ->
   762      (MOVWstorezero [off1+off2] {mergeSym(sym1,sym2)} x mem)
   763  (MOVHstorezero [off1] {sym1} (MOVDaddr [off2] {sym2} x) mem) && canMergeSym(sym1,sym2) ->
   764      (MOVHstorezero [off1+off2] {mergeSym(sym1,sym2)} x mem)
   765  (MOVBstorezero [off1] {sym1} (MOVDaddr [off2] {sym2} x) mem) && canMergeSym(sym1,sym2) ->
   766      (MOVBstorezero [off1+off2] {mergeSym(sym1,sym2)} x mem)
   767  
   768  // atomic intrinsics
   769  (AtomicLoad32  ptr mem) -> (LoweredAtomicLoad32 ptr mem)
   770  (AtomicLoad64  ptr mem) -> (LoweredAtomicLoad64 ptr mem)
   771  (AtomicLoadPtr ptr mem) -> (LoweredAtomicLoadPtr ptr mem)
   772  
   773  (AtomicStore32      ptr val mem) -> (LoweredAtomicStore32 ptr val mem)
   774  (AtomicStore64      ptr val mem) -> (LoweredAtomicStore64 ptr val mem)
   775  //(AtomicStorePtrNoWB ptr val mem) -> (STLR  ptr val mem)
   776  
   777  (AtomicExchange32 ptr val mem) -> (LoweredAtomicExchange32 ptr val mem)
   778  (AtomicExchange64 ptr val mem) -> (LoweredAtomicExchange64 ptr val mem)
   779  
   780  (AtomicAdd32 ptr val mem) -> (LoweredAtomicAdd32 ptr val mem)
   781  (AtomicAdd64 ptr val mem) -> (LoweredAtomicAdd64 ptr val mem)
   782  
   783  (AtomicCompareAndSwap32 ptr old new_ mem) -> (LoweredAtomicCas32 ptr old new_ mem)
   784  (AtomicCompareAndSwap64 ptr old new_ mem) -> (LoweredAtomicCas64 ptr old new_ mem)
   785  
   786  (AtomicAnd8 ptr val mem) -> (LoweredAtomicAnd8 ptr val mem)
   787  (AtomicOr8  ptr val mem) -> (LoweredAtomicOr8  ptr val mem)
   788  
   789  // Lowering extension
   790  // Note: we always extend to 64 bits even though some ops don't need that many result bits.
   791  (SignExt8to16  x) -> (MOVBreg x)
   792  (SignExt8to32  x) -> (MOVBreg x)
   793  (SignExt8to64  x) -> (MOVBreg x)
   794  (SignExt16to32 x) -> (MOVHreg x)
   795  (SignExt16to64 x) -> (MOVHreg x)
   796  (SignExt32to64 x) -> (MOVWreg x)
   797  
   798  (ZeroExt8to16  x) -> (MOVBZreg x)
   799  (ZeroExt8to32  x) -> (MOVBZreg x)
   800  (ZeroExt8to64  x) -> (MOVBZreg x)
   801  (ZeroExt16to32 x) -> (MOVHZreg x)
   802  (ZeroExt16to64 x) -> (MOVHZreg x)
   803  (ZeroExt32to64 x) -> (MOVWZreg x)
   804  
   805  (Trunc16to8  x) -> (MOVBreg x)
   806  (Trunc32to8  x) -> (MOVBreg x)
   807  (Trunc32to16 x) -> (MOVHreg x)
   808  (Trunc64to8  x) -> (MOVBreg x)
   809  (Trunc64to16 x) -> (MOVHreg x)
   810  (Trunc64to32 x) -> (MOVWreg x)
   811  
   812  (Slicemask <t> x) -> (SRADconst (NEG <t> x) [63])
   813  
   814  // Note that MOV??reg returns a 64-bit int, x is not necessarily that wide
   815  // This may interact with other patterns in the future. (Compare with arm64)
   816  (MOVBZreg x:(MOVBZload _ _))  -> x
   817  (MOVHZreg x:(MOVHZload _ _))  -> x
   818  (MOVHreg x:(MOVHload _ _))  -> x
   819  
   820  (MOVBZreg (MOVDconst [c]))  -> (MOVDconst [int64(uint8(c))])
   821  (MOVBreg (MOVDconst [c]))  -> (MOVDconst [int64(int8(c))])
   822  (MOVHZreg (MOVDconst [c]))  -> (MOVDconst [int64(uint16(c))])
   823  (MOVHreg (MOVDconst [c]))  -> (MOVDconst [int64(int16(c))])
   824  
   825  // Lose widening ops fed to to stores
   826  (MOVBstore [off] {sym} ptr (MOVBreg x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   827  (MOVBstore [off] {sym} ptr (MOVBZreg x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   828  (MOVHstore [off] {sym} ptr (MOVHreg x) mem) -> (MOVHstore [off] {sym} ptr x mem)
   829  (MOVHstore [off] {sym} ptr (MOVHZreg x) mem) -> (MOVHstore [off] {sym} ptr x mem)
   830  (MOVWstore [off] {sym} ptr (MOVWreg x) mem) -> (MOVWstore [off] {sym} ptr x mem)
   831  (MOVWstore [off] {sym} ptr (MOVWZreg x) mem) -> (MOVWstore [off] {sym} ptr x mem)
   832  
   833  // Lose W-widening ops fed to compare-W
   834  (CMPW x (MOVWreg y)) -> (CMPW x y)
   835  (CMPW (MOVWreg x) y) -> (CMPW x y)
   836  (CMPWU x (MOVWZreg y)) -> (CMPWU x y)
   837  (CMPWU (MOVWZreg x) y) -> (CMPWU x y)
   838  
   839  (CMP x (MOVDconst [c])) && is16Bit(c) -> (CMPconst x [c])
   840  (CMP (MOVDconst [c]) y) && is16Bit(c) -> (InvertFlags (CMPconst y [c]))
   841  (CMPW x (MOVDconst [c])) && is16Bit(c) -> (CMPWconst x [c])
   842  (CMPW (MOVDconst [c]) y) && is16Bit(c) -> (InvertFlags (CMPWconst y [c]))
   843  
   844  (CMPU x (MOVDconst [c])) && isU16Bit(c) -> (CMPUconst x [c])
   845  (CMPU (MOVDconst [c]) y) && isU16Bit(c) -> (InvertFlags (CMPUconst y [c]))
   846  (CMPWU x (MOVDconst [c])) && isU16Bit(c) -> (CMPWUconst x [c])
   847  (CMPWU (MOVDconst [c]) y) && isU16Bit(c) -> (InvertFlags (CMPWUconst y [c]))
   848  
   849  // A particular pattern seen in cgo code:
   850  (AND (MOVDconst [c]) x:(MOVBZload _ _)) -> (ANDconst [c&0xFF] x)
   851  (AND x:(MOVBZload _ _) (MOVDconst [c])) -> (ANDconst [c&0xFF] x)
   852  
   853  // floating-point fused multiply-add/sub
   854  (FADD (FMUL x y) z) -> (FMADD x y z)
   855  (FSUB (FMUL x y) z) -> (FMSUB x y z)
   856  (FADDS (FMULS x y) z) -> (FMADDS x y z)
   857  (FSUBS (FMULS x y) z) -> (FMSUBS x y z)