github.com/sanprasirt/go@v0.0.0-20170607001320-a027466e4b6d/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  (Zero [8] destptr mem) ->
   530  	(MOVDstorezero destptr mem)
   531  
   532  // Zero small numbers of words directly.
   533  (Zero [12] destptr mem) ->
   534          (MOVWstorezero [8] destptr
   535                  (MOVDstorezero [0] destptr mem))
   536  (Zero [16] destptr mem) ->
   537  	(MOVDstorezero [8] destptr
   538                  (MOVDstorezero [0] destptr mem))
   539  (Zero [24] destptr mem) ->
   540  	(MOVDstorezero [16] destptr
   541  		(MOVDstorezero [8] destptr
   542  			(MOVDstorezero [0] destptr mem)))
   543  (Zero [32] destptr mem) ->
   544  	(MOVDstorezero [24] destptr
   545  		(MOVDstorezero [16] destptr
   546  			(MOVDstorezero [8] destptr
   547  				(MOVDstorezero [0] destptr mem))))
   548  
   549  (Zero [40] destptr mem) ->
   550  	(MOVDstorezero [32] destptr
   551  		(MOVDstorezero [24] destptr
   552  			(MOVDstorezero [16] destptr
   553  				(MOVDstorezero [8] destptr
   554  					(MOVDstorezero [0] destptr mem)))))
   555  
   556  (Zero [48] destptr mem) ->
   557  	(MOVDstorezero [40] destptr
   558  		(MOVDstorezero [32] destptr
   559  			(MOVDstorezero [24] destptr
   560  				(MOVDstorezero [16] destptr
   561  					(MOVDstorezero [8] destptr
   562  						(MOVDstorezero [0] destptr mem))))))
   563  
   564  (Zero [56] destptr mem) ->
   565  	(MOVDstorezero [48] destptr
   566  		(MOVDstorezero [40] destptr
   567  			(MOVDstorezero [32] destptr
   568  				(MOVDstorezero [24] destptr
   569  					(MOVDstorezero [16] destptr
   570  						(MOVDstorezero [8] destptr
   571  							(MOVDstorezero [0] destptr mem)))))))
   572  
   573  // Handle cases not handled above
   574  (Zero [s] ptr mem) -> (LoweredZero [s] ptr mem)
   575  
   576  // moves
   577  // Only the MOVD and MOVW instructions require 4 byte
   578  // alignment in the offset field.  The other MOVx instructions
   579  // allow any alignment.
   580  (Move [0] _ _ mem) -> mem
   581  (Move [1] dst src mem) -> (MOVBstore dst (MOVBZload src mem) mem)
   582  (Move [2] dst src mem) ->
   583          (MOVHstore dst (MOVHZload src mem) mem)
   584  (Move [4] dst src mem) ->
   585  	(MOVWstore dst (MOVWZload src mem) mem)
   586  // MOVD for load and store must have offsets that are multiple of 4
   587  (Move [8] {t} dst src mem) && t.(*types.Type).Alignment()%4 == 0 ->
   588  	(MOVDstore dst (MOVDload src mem) mem)
   589  (Move [8] dst src mem) ->
   590  	(MOVWstore [4] dst (MOVWZload [4] src mem)
   591  		(MOVWstore dst (MOVWZload src mem) mem))
   592  (Move [3] dst src mem) ->
   593          (MOVBstore [2] dst (MOVBZload [2] src mem)
   594                  (MOVHstore dst (MOVHload src mem) mem))
   595  (Move [5] dst src mem) ->
   596          (MOVBstore [4] dst (MOVBZload [4] src mem)
   597                  (MOVWstore dst (MOVWZload src mem) mem))
   598  (Move [6] dst src mem) ->
   599          (MOVHstore [4] dst (MOVHZload [4] src mem)
   600                  (MOVWstore dst (MOVWZload src mem) mem))
   601  (Move [7] dst src mem) ->
   602          (MOVBstore [6] dst (MOVBZload [6] src mem)
   603                  (MOVHstore [4] dst (MOVHZload [4] src mem)
   604                          (MOVWstore dst (MOVWZload src mem) mem)))
   605  
   606  // Large move uses a loop. Since the address is computed and the
   607  // offset is zero, any alignment can be used.
   608  (Move [s] dst src mem) && s > 8 ->
   609          (LoweredMove [s] dst src mem)
   610  
   611  // Calls
   612  // Lowering calls
   613  (StaticCall [argwid] {target} mem) -> (CALLstatic [argwid] {target} mem)
   614  (ClosureCall [argwid] entry closure mem) -> (CALLclosure [argwid] entry closure mem)
   615  (InterCall [argwid] entry mem) -> (CALLinter [argwid] entry mem)
   616  
   617  // Miscellaneous
   618  (Convert <t> x mem) -> (MOVDconvert <t> x mem)
   619  (GetClosurePtr) -> (LoweredGetClosurePtr)
   620  (IsNonNil ptr) -> (NotEqual (CMPconst [0] ptr))
   621  (IsInBounds idx len) -> (LessThan (CMPU idx len))
   622  (IsSliceInBounds idx len) -> (LessEqual (CMPU idx len))
   623  (NilCheck ptr mem) -> (LoweredNilCheck ptr mem)
   624  
   625  // Optimizations
   626  // Note that PPC "logical" immediates come in 0:15 and 16:31 unsigned immediate forms,
   627  // so ORconst, XORconst easily expand into a pair.
   628  
   629  // Include very-large constants in the const-const case.
   630  (AND (MOVDconst [c]) (MOVDconst [d])) -> (MOVDconst [c&d])
   631  (OR (MOVDconst [c]) (MOVDconst [d])) -> (MOVDconst [c|d])
   632  (XOR (MOVDconst [c]) (MOVDconst [d])) -> (MOVDconst [c^d])
   633  
   634  // Discover consts
   635  (AND x (MOVDconst [c])) && isU16Bit(c) -> (ANDconst [c] x)
   636  (XOR x (MOVDconst [c])) && isU32Bit(c) -> (XORconst [c] x)
   637  (OR x (MOVDconst [c])) && isU32Bit(c) -> (ORconst [c] x)
   638  
   639  // Simplify consts
   640  (ANDconst [c] (ANDconst [d] x)) -> (ANDconst [c&d] x)
   641  (ORconst [c] (ORconst [d] x)) -> (ORconst [c|d] x)
   642  (XORconst [c] (XORconst [d] x)) -> (XORconst [c^d] x)
   643  (ANDconst [-1] x) -> x
   644  (ANDconst [0] _) -> (MOVDconst [0])
   645  (XORconst [0] x) -> x
   646  (ORconst [-1] _) -> (MOVDconst [-1])
   647  (ORconst [0] x) -> x
   648  
   649  // zero-extend of small and -> small and
   650  (MOVBZreg y:(ANDconst [c] _)) && uint64(c) <= 0xFF -> y
   651  (MOVHZreg y:(ANDconst [c] _)) && uint64(c) <= 0xFFFF -> y
   652  (MOVWZreg y:(ANDconst [c] _)) && uint64(c) <= 0xFFFFFFFF -> y
   653  (MOVWZreg y:(AND (MOVDconst [c]) _)) && uint64(c) <= 0xFFFFFFFF -> y
   654  
   655  // sign extend of small-positive and -> small-positive-and
   656  (MOVBreg y:(ANDconst [c] _)) && uint64(c) <= 0x7F -> y
   657  (MOVHreg y:(ANDconst [c] _)) && uint64(c) <= 0x7FFF -> y
   658  (MOVWreg y:(ANDconst [c] _)) && uint64(c) <= 0xFFFF -> y // 0xFFFF is largest immediate constant, when regarded as 32-bit is > 0
   659  (MOVWreg y:(AND (MOVDconst [c]) _)) && uint64(c) <= 0x7FFFFFFF -> y
   660  
   661  // small and of zero-extend -> either zero-extend or small and
   662    // degenerate-and
   663  (ANDconst [c] y:(MOVBZreg _)) && c&0xFF == 0xFF -> y
   664  (ANDconst [c] y:(MOVHZreg _))  && c&0xFFFF == 0xFFFF -> y
   665  (ANDconst [c] y:(MOVWZreg _))  && c&0xFFFFFFFF == 0xFFFFFFFF -> y
   666    // normal case
   667  (ANDconst [c] (MOVBZreg x)) -> (ANDconst [c&0xFF] x)
   668  (ANDconst [c] (MOVHZreg x)) -> (ANDconst [c&0xFFFF] x)
   669  (ANDconst [c] (MOVWZreg x)) -> (ANDconst [c&0xFFFFFFFF] x)
   670  
   671  // Various redundant zero/sign extension combinations.
   672  (MOVBZreg y:(MOVBZreg _)) -> y  // repeat
   673  (MOVBreg y:(MOVBreg _)) -> y // repeat
   674  (MOVBreg (MOVBZreg x)) -> (MOVBreg x)
   675  (MOVBZreg (MOVBreg x)) -> (MOVBZreg x)
   676  
   677  // H - there are more combinations than these
   678  
   679  (MOVHZreg y:(MOVHZreg _)) -> y // repeat
   680  (MOVHZreg y:(MOVBZreg _)) -> y // wide of narrow
   681  
   682  (MOVHreg y:(MOVHreg _)) -> y // repeat
   683  (MOVHreg y:(MOVBreg _)) -> y // wide of narrow
   684  
   685  (MOVHreg y:(MOVHZreg x)) -> (MOVHreg x)
   686  (MOVHZreg y:(MOVHreg x)) -> (MOVHZreg x)
   687  
   688  // W - there are more combinations than these
   689  
   690  (MOVWZreg y:(MOVWZreg _)) -> y // repeat
   691  (MOVWZreg y:(MOVHZreg _)) -> y // wide of narrow
   692  (MOVWZreg y:(MOVBZreg _)) -> y // wide of narrow
   693  
   694  (MOVWreg y:(MOVWreg _)) -> y // repeat
   695  (MOVWreg y:(MOVHreg _)) -> y // wide of narrow
   696  (MOVWreg y:(MOVBreg _)) -> y // wide of narrow
   697  
   698  (MOVWreg y:(MOVWZreg x)) -> (MOVWreg x)
   699  (MOVWZreg y:(MOVWreg x)) -> (MOVWZreg x)
   700  
   701  // Arithmetic constant ops
   702  
   703  (ADD x (MOVDconst [c])) && is32Bit(c) -> (ADDconst [c] x)
   704  (ADDconst [c] (ADDconst [d] x)) && is32Bit(c+d) -> (ADDconst [c+d] x)
   705  (ADDconst [0] x) -> x
   706  (SUB x (MOVDconst [c])) && is32Bit(-c) -> (ADDconst [-c] x)
   707  // TODO deal with subtract-from-const
   708  
   709  (ADDconst [c] (MOVDaddr [d] {sym} x)) -> (MOVDaddr [c+d] {sym} x)
   710  
   711  // Fold offsets for stores.
   712  (MOVDstore [off1] {sym} (ADDconst [off2] x) val mem) && is16Bit(off1+off2) -> (MOVDstore [off1+off2] {sym} x val mem)
   713  (MOVWstore [off1] {sym} (ADDconst [off2] x) val mem) && is16Bit(off1+off2) -> (MOVWstore [off1+off2] {sym} x val mem)
   714  (MOVHstore [off1] {sym} (ADDconst [off2] x) val mem) && is16Bit(off1+off2) -> (MOVHstore [off1+off2] {sym} x val mem)
   715  (MOVBstore [off1] {sym} (ADDconst [off2] x) val mem) && is16Bit(off1+off2) -> (MOVBstore [off1+off2] {sym} x val mem)
   716  
   717  (FMOVSstore [off1] {sym} (ADDconst [off2] ptr) val mem) && is16Bit(off1+off2) -> (FMOVSstore [off1+off2] {sym} ptr val mem)
   718  (FMOVDstore [off1] {sym} (ADDconst [off2] ptr) val mem) && is16Bit(off1+off2) -> (FMOVDstore [off1+off2] {sym} ptr val mem)
   719  
   720  (MOVBstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   721          (MOVBstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   722  (MOVHstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   723          (MOVHstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   724  (MOVWstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   725          (MOVWstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   726  (MOVDstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   727          (MOVDstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   728  
   729  (FMOVSstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   730          (FMOVSstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   731  (FMOVDstore [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   732          (FMOVDstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   733  
   734  (MOVBZload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   735          (MOVBZload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   736  (MOVHload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   737          (MOVHload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   738  (MOVHZload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   739          (MOVHZload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   740  (MOVWload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   741          (MOVWload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   742  (MOVWZload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   743          (MOVWZload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   744  (MOVDload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   745          (MOVDload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   746  (FMOVSload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   747          (FMOVSload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   748  (FMOVDload [off1] {sym1} (MOVDaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   749          (FMOVDload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   750  
   751  // Fold offsets for loads.
   752  (FMOVSload [off1] {sym} (ADDconst [off2] ptr) mem) && is16Bit(off1+off2) -> (FMOVSload [off1+off2] {sym} ptr mem)
   753  (FMOVDload [off1] {sym} (ADDconst [off2] ptr) mem) && is16Bit(off1+off2) -> (FMOVDload [off1+off2] {sym} ptr mem)
   754  
   755  (MOVDload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVDload [off1+off2] {sym} x mem)
   756  (MOVWload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVWload [off1+off2] {sym} x mem)
   757  (MOVWZload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVWZload [off1+off2] {sym} x mem)
   758  (MOVHload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVHload [off1+off2] {sym} x mem)
   759  (MOVHZload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVHZload [off1+off2] {sym} x mem)
   760  (MOVBZload [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) -> (MOVBZload [off1+off2] {sym} x mem)
   761  
   762  // Store of zero -> storezero
   763  (MOVDstore [off] {sym} ptr (MOVDconst [c]) mem) && c == 0 -> (MOVDstorezero [off] {sym} ptr mem)
   764  (MOVWstore [off] {sym} ptr (MOVDconst [c]) mem) && c == 0 -> (MOVWstorezero [off] {sym} ptr mem)
   765  (MOVHstore [off] {sym} ptr (MOVDconst [c]) mem) && c == 0 -> (MOVHstorezero [off] {sym} ptr mem)
   766  (MOVBstore [off] {sym} ptr (MOVDconst [c]) mem) && c == 0 -> (MOVBstorezero [off] {sym} ptr mem)
   767  
   768  // Fold offsets for storezero
   769  (MOVDstorezero [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) ->
   770      (MOVDstorezero [off1+off2] {sym} x mem)
   771  (MOVWstorezero [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) ->
   772      (MOVWstorezero [off1+off2] {sym} x mem)
   773  (MOVHstorezero [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) ->
   774      (MOVHstorezero [off1+off2] {sym} x mem)
   775  (MOVBstorezero [off1] {sym} (ADDconst [off2] x) mem) && is16Bit(off1+off2) ->
   776      (MOVBstorezero [off1+off2] {sym} x mem)
   777  
   778  // Fold symbols into storezero
   779  (MOVDstorezero [off1] {sym1} (MOVDaddr [off2] {sym2} x) mem) && canMergeSym(sym1,sym2) ->
   780      (MOVDstorezero [off1+off2] {mergeSym(sym1,sym2)} x mem)
   781  (MOVWstorezero [off1] {sym1} (MOVDaddr [off2] {sym2} x) mem) && canMergeSym(sym1,sym2) ->
   782      (MOVWstorezero [off1+off2] {mergeSym(sym1,sym2)} x mem)
   783  (MOVHstorezero [off1] {sym1} (MOVDaddr [off2] {sym2} x) mem) && canMergeSym(sym1,sym2) ->
   784      (MOVHstorezero [off1+off2] {mergeSym(sym1,sym2)} x mem)
   785  (MOVBstorezero [off1] {sym1} (MOVDaddr [off2] {sym2} x) mem) && canMergeSym(sym1,sym2) ->
   786      (MOVBstorezero [off1+off2] {mergeSym(sym1,sym2)} x mem)
   787  
   788  // atomic intrinsics
   789  (AtomicLoad32  ptr mem) -> (LoweredAtomicLoad32 ptr mem)
   790  (AtomicLoad64  ptr mem) -> (LoweredAtomicLoad64 ptr mem)
   791  (AtomicLoadPtr ptr mem) -> (LoweredAtomicLoadPtr ptr mem)
   792  
   793  (AtomicStore32      ptr val mem) -> (LoweredAtomicStore32 ptr val mem)
   794  (AtomicStore64      ptr val mem) -> (LoweredAtomicStore64 ptr val mem)
   795  //(AtomicStorePtrNoWB ptr val mem) -> (STLR  ptr val mem)
   796  
   797  (AtomicExchange32 ptr val mem) -> (LoweredAtomicExchange32 ptr val mem)
   798  (AtomicExchange64 ptr val mem) -> (LoweredAtomicExchange64 ptr val mem)
   799  
   800  (AtomicAdd32 ptr val mem) -> (LoweredAtomicAdd32 ptr val mem)
   801  (AtomicAdd64 ptr val mem) -> (LoweredAtomicAdd64 ptr val mem)
   802  
   803  (AtomicCompareAndSwap32 ptr old new_ mem) -> (LoweredAtomicCas32 ptr old new_ mem)
   804  (AtomicCompareAndSwap64 ptr old new_ mem) -> (LoweredAtomicCas64 ptr old new_ mem)
   805  
   806  (AtomicAnd8 ptr val mem) -> (LoweredAtomicAnd8 ptr val mem)
   807  (AtomicOr8  ptr val mem) -> (LoweredAtomicOr8  ptr val mem)
   808  
   809  // Lowering extension
   810  // Note: we always extend to 64 bits even though some ops don't need that many result bits.
   811  (SignExt8to16  x) -> (MOVBreg x)
   812  (SignExt8to32  x) -> (MOVBreg x)
   813  (SignExt8to64  x) -> (MOVBreg x)
   814  (SignExt16to32 x) -> (MOVHreg x)
   815  (SignExt16to64 x) -> (MOVHreg x)
   816  (SignExt32to64 x) -> (MOVWreg x)
   817  
   818  (ZeroExt8to16  x) -> (MOVBZreg x)
   819  (ZeroExt8to32  x) -> (MOVBZreg x)
   820  (ZeroExt8to64  x) -> (MOVBZreg x)
   821  (ZeroExt16to32 x) -> (MOVHZreg x)
   822  (ZeroExt16to64 x) -> (MOVHZreg x)
   823  (ZeroExt32to64 x) -> (MOVWZreg x)
   824  
   825  (Trunc16to8  x) -> (MOVBreg x)
   826  (Trunc32to8  x) -> (MOVBreg x)
   827  (Trunc32to16 x) -> (MOVHreg x)
   828  (Trunc64to8  x) -> (MOVBreg x)
   829  (Trunc64to16 x) -> (MOVHreg x)
   830  (Trunc64to32 x) -> (MOVWreg x)
   831  
   832  (Slicemask <t> x) -> (SRADconst (NEG <t> x) [63])
   833  
   834  // Note that MOV??reg returns a 64-bit int, x is not necessarily that wide
   835  // This may interact with other patterns in the future. (Compare with arm64)
   836  (MOVBZreg x:(MOVBZload _ _))  -> x
   837  (MOVHZreg x:(MOVHZload _ _))  -> x
   838  (MOVHreg x:(MOVHload _ _))  -> x
   839  
   840  (MOVBZreg (MOVDconst [c]))  -> (MOVDconst [int64(uint8(c))])
   841  (MOVBreg (MOVDconst [c]))  -> (MOVDconst [int64(int8(c))])
   842  (MOVHZreg (MOVDconst [c]))  -> (MOVDconst [int64(uint16(c))])
   843  (MOVHreg (MOVDconst [c]))  -> (MOVDconst [int64(int16(c))])
   844  
   845  // Lose widening ops fed to to stores
   846  (MOVBstore [off] {sym} ptr (MOVBreg x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   847  (MOVBstore [off] {sym} ptr (MOVBZreg x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   848  (MOVHstore [off] {sym} ptr (MOVHreg x) mem) -> (MOVHstore [off] {sym} ptr x mem)
   849  (MOVHstore [off] {sym} ptr (MOVHZreg x) mem) -> (MOVHstore [off] {sym} ptr x mem)
   850  (MOVWstore [off] {sym} ptr (MOVWreg x) mem) -> (MOVWstore [off] {sym} ptr x mem)
   851  (MOVWstore [off] {sym} ptr (MOVWZreg x) mem) -> (MOVWstore [off] {sym} ptr x mem)
   852  
   853  // Lose W-widening ops fed to compare-W
   854  (CMPW x (MOVWreg y)) -> (CMPW x y)
   855  (CMPW (MOVWreg x) y) -> (CMPW x y)
   856  (CMPWU x (MOVWZreg y)) -> (CMPWU x y)
   857  (CMPWU (MOVWZreg x) y) -> (CMPWU x y)
   858  
   859  (CMP x (MOVDconst [c])) && is16Bit(c) -> (CMPconst x [c])
   860  (CMP (MOVDconst [c]) y) && is16Bit(c) -> (InvertFlags (CMPconst y [c]))
   861  (CMPW x (MOVDconst [c])) && is16Bit(c) -> (CMPWconst x [c])
   862  (CMPW (MOVDconst [c]) y) && is16Bit(c) -> (InvertFlags (CMPWconst y [c]))
   863  
   864  (CMPU x (MOVDconst [c])) && isU16Bit(c) -> (CMPUconst x [c])
   865  (CMPU (MOVDconst [c]) y) && isU16Bit(c) -> (InvertFlags (CMPUconst y [c]))
   866  (CMPWU x (MOVDconst [c])) && isU16Bit(c) -> (CMPWUconst x [c])
   867  (CMPWU (MOVDconst [c]) y) && isU16Bit(c) -> (InvertFlags (CMPWUconst y [c]))
   868  
   869  // A particular pattern seen in cgo code:
   870  (AND (MOVDconst [c]) x:(MOVBZload _ _)) -> (ANDconst [c&0xFF] x)
   871  (AND x:(MOVBZload _ _) (MOVDconst [c])) -> (ANDconst [c&0xFF] x)
   872  
   873  // floating-point fused multiply-add/sub
   874  (FADD (FMUL x y) z) -> (FMADD x y z)
   875  (FSUB (FMUL x y) z) -> (FMSUB x y z)
   876  (FADDS (FMULS x y) z) -> (FMADDS x y z)
   877  (FSUBS (FMULS x y) z) -> (FMSUBS x y z)