Text file src/cmd/compile/internal/ssa/_gen/PPC64.rules

     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  // GOPPC64 values indicate power8, power9, etc.
     6  // That means the code is compiled for that target,
     7  // and will not run on earlier targets.
     8  //
     9  (Add(Ptr|64|32|16|8) ...) => (ADD ...)
    10  (Add64F ...) => (FADD ...)
    11  (Add32F ...) => (FADDS ...)
    12  
    13  (Sub(Ptr|64|32|16|8) ...) => (SUB ...)
    14  (Sub32F ...) => (FSUBS ...)
    15  (Sub64F ...) => (FSUB ...)
    16  
    17  (Min(32|64)F x y) && buildcfg.GOPPC64 >= 9 => (XSMINJDP x y)
    18  (Max(32|64)F x y) && buildcfg.GOPPC64 >= 9 => (XSMAXJDP x y)
    19  
    20  // Combine 64 bit integer multiply and adds
    21  (ADD            z  l:(MULLD            x  y)) && buildcfg.GOPPC64 >= 9 && l.Uses == 1 && ssa.Clobber(l) => (MADDLD                        x    y                        z   )
    22  (ADD            z  l:(MULLDconst <mt> [x] y)) && buildcfg.GOPPC64 >= 9 && l.Uses == 1 && ssa.Clobber(l) => (MADDLD (MOVDconst <mt> [int64(x)]) y                        z   )
    23  (ADDconst <at> [z] l:(MULLD            x  y)) && buildcfg.GOPPC64 >= 9 && l.Uses == 1 && ssa.Clobber(l) => (MADDLD                        x    y (MOVDconst <at> [int64(z)]))
    24  (ADDconst <at> [z] l:(MULLDconst <mt> [x] y)) && buildcfg.GOPPC64 >= 9 && l.Uses == 1 && ssa.Clobber(l) => (MADDLD (MOVDconst <mt> [int64(x)]) y (MOVDconst <at> [int64(z)]))
    25  
    26  (Mod16 x y) => (Mod32 (SignExt16to32 x) (SignExt16to32 y))
    27  (Mod16u x y) => (Mod32u (ZeroExt16to32 x) (ZeroExt16to32 y))
    28  (Mod8 x y) => (Mod32 (SignExt8to32 x) (SignExt8to32 y))
    29  (Mod8u x y) => (Mod32u (ZeroExt8to32 x) (ZeroExt8to32 y))
    30  (Mod64 x y) && buildcfg.GOPPC64 >=9 => (MODSD x y)
    31  (Mod64 x y) && buildcfg.GOPPC64 <=8 => (SUB x (MULLD y (DIVD x y)))
    32  (Mod64u x y) && buildcfg.GOPPC64 >= 9 => (MODUD x y)
    33  (Mod64u x y) && buildcfg.GOPPC64 <= 8 => (SUB x (MULLD y (DIVDU x y)))
    34  (Mod32 x y) && buildcfg.GOPPC64 >= 9 => (MODSW x y)
    35  (Mod32 x y) && buildcfg.GOPPC64 <= 8 => (SUB x (MULLW y (DIVW x y)))
    36  (Mod32u x y) && buildcfg.GOPPC64 >= 9 => (MODUW x y)
    37  (Mod32u x y) && buildcfg.GOPPC64 <= 8 => (SUB x (MULLW y (DIVWU x y)))
    38  
    39  // (x + y) / 2 with x>=y => (x - y) / 2 + y
    40  (Avg64u <t> x y) => (ADD (SRDconst <t> (SUB <t> x y) [1]) y)
    41  
    42  (Mul64 ...) => (MULLD ...)
    43  (Mul(32|16|8) ...) => (MULLW ...)
    44  (Select0 (Mul64uhilo x y)) => (MULHDU x y)
    45  (Select1 (Mul64uhilo x y)) => (MULLD x y)
    46  (Select0 (Mul64uover x y)) => (MULLD x y)
    47  (Select1 (Mul64uover x y)) => (SETBCR [2] (CMPconst [0] (MULHDU <x.Type> x y)))
    48  
    49  (Div64 [false] x y) => (DIVD x y)
    50  (Div64u ...) => (DIVDU ...)
    51  (Div32 [false] x y) => (DIVW x y)
    52  (Div32u ...) => (DIVWU ...)
    53  (Div16 [false]  x y) => (DIVW  (SignExt16to32 x) (SignExt16to32 y))
    54  (Div16u x y) => (DIVWU (ZeroExt16to32 x) (ZeroExt16to32 y))
    55  (Div8 x y) => (DIVW  (SignExt8to32 x) (SignExt8to32 y))
    56  (Div8u x y) => (DIVWU (ZeroExt8to32 x) (ZeroExt8to32 y))
    57  
    58  (Hmul(64|64u|32|32u) ...) => (MULH(D|DU|W|WU) ...)
    59  
    60  (Mul(32|64)F ...) => ((FMULS|FMUL) ...)
    61  
    62  (Div(32|64)F ...) => ((FDIVS|FDIV) ...)
    63  
    64  // Lowering float <=> int
    65  (Cvt32to(32|64)F x) => ((FCFIDS|FCFID) (MTVSRD (SignExt32to64 x)))
    66  (Cvt64to(32|64)F x) => ((FCFIDS|FCFID) (MTVSRD x))
    67  
    68  (Cvt32Fto(32|64) x) => (MFVSRD (FCTI(W|D)Z x))
    69  (Cvt64Fto(32|64) x) => (MFVSRD (FCTI(W|D)Z x))
    70  
    71  (Cvt32Fto64F ...) => (Copy ...) // Note v will have the wrong type for patterns dependent on Float32/Float64
    72  (Cvt64Fto32F ...) => (FRSP ...)
    73  
    74  (CvtBoolToUint8 ...) => (Copy ...)
    75  
    76  (Round(32|64)F ...) => (LoweredRound(32|64)F ...)
    77  
    78  (Sqrt ...) => (FSQRT ...)
    79  (Sqrt32 ...) => (FSQRTS ...)
    80  (Floor ...) => (FFLOOR ...)
    81  (Ceil ...) => (FCEIL ...)
    82  (Trunc ...) => (FTRUNC ...)
    83  (Round ...) => (FROUND ...)
    84  (Copysign x y) => (FCPSGN y x)
    85  (Abs ...) => (FABS ...)
    86  (FMA ...) => (FMADD ...)
    87  (FMA32 ...) => (FMADDS ...)
    88  
    89  // Lowering extension
    90  // Note: we always extend to 64 bits even though some ops don't need that many result bits.
    91  (SignExt8to(16|32|64) ...) => (MOVBreg ...)
    92  (SignExt16to(32|64) ...) => (MOVHreg ...)
    93  (SignExt32to64 ...) => (MOVWreg ...)
    94  
    95  (ZeroExt8to(16|32|64) ...) => (MOVBZreg ...)
    96  (ZeroExt16to(32|64) ...) => (MOVHZreg ...)
    97  (ZeroExt32to64 ...) => (MOVWZreg ...)
    98  
    99  (Trunc(16|32|64)to8 <t> x) && t.IsSigned() => (MOVBreg x)
   100  (Trunc(16|32|64)to8  x) => (MOVBZreg x)
   101  (Trunc(32|64)to16 <t> x) && t.IsSigned() => (MOVHreg x)
   102  (Trunc(32|64)to16 x) => (MOVHZreg x)
   103  (Trunc64to32 <t> x) && t.IsSigned() => (MOVWreg x)
   104  (Trunc64to32 x) => (MOVWZreg x)
   105  
   106  // Lowering constants
   107  (Const(64|32|16|8) [val]) => (MOVDconst [int64(val)])
   108  (Const(32|64)F ...) => (FMOV(S|D)const ...)
   109  (ConstNil) => (MOVDconst [0])
   110  (ConstBool [t]) => (MOVDconst [ssa.B2i(t)])
   111  
   112  // Carrying addition.
   113  (Select0 (Add64carry x y c)) =>            (Select0 <typ.UInt64> (ADDE x y (Select1 <typ.UInt64> (ADDCconst c [-1]))))
   114  (Select1 (Add64carry x y c)) => (ADDZEzero (Select1 <typ.UInt64> (ADDE x y (Select1 <typ.UInt64> (ADDCconst c [-1])))))
   115  // Fold initial carry bit if 0.
   116  (ADDE x y (Select1 <typ.UInt64> (ADDCconst (MOVDconst [0]) [-1]))) => (ADDC x y)
   117  // Fold transfer of CA -> GPR -> CA. Note 2 uses when feeding into a chained Add64carry.
   118  (Select1 (ADDCconst n:(ADDZEzero x) [-1])) && n.Uses <= 2 => x
   119  (ADDE (MOVDconst [0]) y c) => (ADDZE y c)
   120  (ADDC x (MOVDconst [y])) && ssa.Is16Bit(y) => (ADDCconst [y] x)
   121  
   122  // Borrowing subtraction.
   123  (Select0 (Sub64borrow x y c)) =>                 (Select0 <typ.UInt64> (SUBE x y (Select1 <typ.UInt64> (SUBCconst c [0]))))
   124  (Select1 (Sub64borrow x y c)) => (NEG (SUBZEzero (Select1 <typ.UInt64> (SUBE x y (Select1 <typ.UInt64> (SUBCconst c [0]))))))
   125  // Fold initial borrow bit if 0.
   126  (SUBE x y (Select1 <typ.UInt64> (SUBCconst (MOVDconst [0]) [0]))) => (SUBC x y)
   127  // Fold transfer of CA -> GPR -> CA. Note 2 uses when feeding into a chained Sub64borrow.
   128  (Select1 (SUBCconst n:(NEG (SUBZEzero x)) [0])) && n.Uses <= 2 => x
   129  
   130  // Constant folding
   131  (FABS (FMOVDconst [x])) => (FMOVDconst [math.Abs(x)])
   132  (FSQRT (FMOVDconst [x])) && x >= 0 => (FMOVDconst [math.Sqrt(x)])
   133  (FFLOOR (FMOVDconst [x])) => (FMOVDconst [math.Floor(x)])
   134  (FCEIL (FMOVDconst [x])) => (FMOVDconst [math.Ceil(x)])
   135  (FTRUNC (FMOVDconst [x])) => (FMOVDconst [math.Trunc(x)])
   136  
   137  // Rotates
   138  (RotateLeft8 <t> x (MOVDconst [c])) => (Or8 (Lsh8x64 <t> x (MOVDconst [c&7])) (Rsh8Ux64 <t> x (MOVDconst [-c&7])))
   139  (RotateLeft16 <t> x (MOVDconst [c])) => (Or16 (Lsh16x64 <t> x (MOVDconst [c&15])) (Rsh16Ux64 <t> x (MOVDconst [-c&15])))
   140  (RotateLeft(32|64) ...) => ((ROTLW|ROTL) ...)
   141  
   142  // Constant rotate generation
   143  (ROTLW  x (MOVDconst [c])) => (ROTLWconst  x [c&31])
   144  (ROTL   x (MOVDconst [c])) => (ROTLconst   x [c&63])
   145  
   146  // uint8: logical ops with constant -> UI immediates (only if truncation needed).
   147  (AND <t> x (MOVDconst [m])) && t.IsUnsigned() && t.Size() == 1 && m != int64(uint8(m)) => (ANDconst [int64(uint8(m))] x)
   148  (OR <t> x (MOVDconst [m])) && t.IsUnsigned() && t.Size() == 1 && m != int64(uint8(m))  => (ORconst [int64(uint8(m))] x)
   149  (XOR <t> x (MOVDconst [m])) && t.IsUnsigned() && t.Size() == 1 && m != int64(uint8(m)) => (XORconst [int64(uint8(m))] x)
   150  
   151  // uint16: logical ops with constant -> UI immediates (only if truncation needed).
   152  (AND <t> x (MOVDconst [m])) && t.IsUnsigned() && t.Size() == 2 && m != int64(uint16(m)) => (ANDconst [int64(uint16(m))] x)
   153  (OR <t> x (MOVDconst [m])) && t.IsUnsigned() && t.Size() == 2 && m != int64(uint16(m)) => (ORconst [int64(uint16(m))] x)
   154  (XOR <t> x (MOVDconst [m])) && t.IsUnsigned() && t.Size() == 2 && m != int64(uint16(m)) => (XORconst [int64(uint16(m))] x)
   155  
   156  // Combine rotate and mask operations
   157  (ANDconst [m] (ROTLWconst [r] x)) && ssa.IsPPC64WordRotateMask(m) => (RLWINM [ssa.EncodePPC64RotateMask(r,m,32)] x)
   158  (AND (MOVDconst [m]) (ROTLWconst [r] x)) && ssa.IsPPC64WordRotateMask(m) => (RLWINM [ssa.EncodePPC64RotateMask(r,m,32)] x)
   159  (ANDconst [m] (ROTLW x r)) && ssa.IsPPC64WordRotateMask(m) => (RLWNM [ssa.EncodePPC64RotateMask(0,m,32)] x r)
   160  (AND (MOVDconst [m]) (ROTLW x r)) && ssa.IsPPC64WordRotateMask(m) => (RLWNM [ssa.EncodePPC64RotateMask(0,m,32)] x r)
   161  
   162  // Note, any rotated word bitmask is still a valid word bitmask.
   163  (ROTLWconst [r] (AND (MOVDconst [m]) x)) && ssa.IsPPC64WordRotateMask(m) => (RLWINM [ssa.EncodePPC64RotateMask(r,rotateLeft32(m,r),32)] x)
   164  (ROTLWconst [r] (ANDconst [m] x)) && ssa.IsPPC64WordRotateMask(m) => (RLWINM [ssa.EncodePPC64RotateMask(r,rotateLeft32(m,r),32)] x)
   165  
   166  (ANDconst [m] (SRWconst x [s])) && ssa.MergePPC64RShiftMask(m,s,32) == 0 => (MOVDconst [0])
   167  (ANDconst [m] (SRWconst x [s])) && ssa.MergePPC64AndSrwi(m,s) != 0 => (RLWINM [ssa.MergePPC64AndSrwi(m,s)] x)
   168  (ANDconst [m] (SRDconst x [s])) && mergePPC64AndSrdi(m,s) != 0 => (RLWINM [mergePPC64AndSrdi(m,s)] x)
   169  (AND (MOVDconst [m]) (SRWconst x [s])) && ssa.MergePPC64RShiftMask(m,s,32) == 0 => (MOVDconst [0])
   170  (AND (MOVDconst [m]) (SRWconst x [s])) && ssa.MergePPC64AndSrwi(m,s) != 0 => (RLWINM [ssa.MergePPC64AndSrwi(m,s)] x)
   171  (AND (MOVDconst [m]) (SRDconst x [s])) && mergePPC64AndSrdi(m,s) != 0 => (RLWINM [mergePPC64AndSrdi(m,s)] x)
   172  
   173  (SRWconst (ANDconst [m] x) [s]) && ssa.MergePPC64RShiftMask(m>>uint(s),s,32) == 0 => (MOVDconst [0])
   174  (SRWconst (ANDconst [m] x) [s]) && ssa.MergePPC64AndSrwi(m>>uint(s),s) != 0 => (RLWINM [ssa.MergePPC64AndSrwi(m>>uint(s),s)] x)
   175  (SRWconst (AND (MOVDconst [m]) x) [s]) && ssa.MergePPC64RShiftMask(m>>uint(s),s,32) == 0 => (MOVDconst [0])
   176  (SRWconst (AND (MOVDconst [m]) x) [s]) && ssa.MergePPC64AndSrwi(m>>uint(s),s) != 0 => (RLWINM [ssa.MergePPC64AndSrwi(m>>uint(s),s)] x)
   177  
   178  (ANDconst [m] (SLDconst x [s])) && mergePPC64AndSldi(m,s) != 0 => (RLWINM [mergePPC64AndSldi(m,s)] x)
   179  (AND (MOVDconst [m]) (SLDconst x [s])) && mergePPC64AndSldi(m,s) != 0 => (RLWINM [mergePPC64AndSldi(m,s)] x)
   180  
   181  // Merge shift right + shift left and clear left (e.g for a table lookup)
   182  (CLRLSLDI [c] (SRWconst [s] x)) && ssa.MergePPC64ClrlsldiSrw(int64(c),s) != 0 => (RLWINM [ssa.MergePPC64ClrlsldiSrw(int64(c),s)] x)
   183  (CLRLSLDI [c] (SRDconst [s] x)) && mergePPC64ClrlsldiSrd(int64(c),s) != 0 => (RLWINM [mergePPC64ClrlsldiSrd(int64(c),s)] x)
   184  (SLDconst [l] (SRWconst [r] x)) && ssa.MergePPC64SldiSrw(l,r) != 0 => (RLWINM [ssa.MergePPC64SldiSrw(l,r)] x)
   185  // The following reduction shows up frequently too. e.g b[(x>>14)&0xFF]
   186  (CLRLSLDI [c] i:(RLWINM [s] x)) && ssa.MergePPC64ClrlsldiRlwinm(c,s) != 0 => (RLWINM [ssa.MergePPC64ClrlsldiRlwinm(c,s)] x)
   187  
   188  // large constant signed right shift, we leave the sign bit
   189  (Rsh64x64 x (MOVDconst [c])) && uint64(c) >= 64 => (SRADconst x [63])
   190  (Rsh32x64 x (MOVDconst [c])) && uint64(c) >= 32 => (SRAWconst x [63])
   191  (Rsh16x64 x (MOVDconst [c])) && uint64(c) >= 16 => (SRAWconst (SignExt16to32 x) [63])
   192  (Rsh8x64  x (MOVDconst [c])) && uint64(c) >= 8  => (SRAWconst (SignExt8to32  x) [63])
   193  
   194  // constant shifts
   195  ((Lsh64|Rsh64|Rsh64U)x64  x (MOVDconst [c])) && uint64(c) < 64 => (S(L|RA|R)Dconst x [c])
   196  ((Lsh32|Rsh32|Rsh32U)x64  x (MOVDconst [c])) && uint64(c) < 32 => (S(L|RA|R)Wconst x [c])
   197  ((Rsh16|Rsh16U)x64  x (MOVDconst [c])) && uint64(c) < 16 => (SR(AW|W)const ((Sign|Zero)Ext16to32 x) [c])
   198  (Lsh16x64  x (MOVDconst [c])) && uint64(c) < 16 => (SLWconst x [c])
   199  ((Rsh8|Rsh8U)x64  x (MOVDconst [c])) && uint64(c) < 8 => (SR(AW|W)const ((Sign|Zero)Ext8to32 x) [c])
   200  (Lsh8x64  x (MOVDconst [c])) && uint64(c) < 8 => (SLWconst x [c])
   201  
   202  // Lower bounded shifts first. No need to check shift value.
   203  (Lsh64x(64|32|16|8)  x y) && ssa.ShiftIsBounded(v) => (SLD x y)
   204  (Lsh32x(64|32|16|8)  x y) && ssa.ShiftIsBounded(v) => (SLW x y)
   205  (Lsh16x(64|32|16|8)  x y) && ssa.ShiftIsBounded(v) => (SLD x y)
   206  (Lsh8x(64|32|16|8)   x y) && ssa.ShiftIsBounded(v) => (SLD x y)
   207  (Rsh64Ux(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRD x y)
   208  (Rsh32Ux(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRW x y)
   209  (Rsh16Ux(64|32|16|8) x y) && ssa.ShiftIsBounded(v) => (SRD (MOVHZreg x) y)
   210  (Rsh8Ux(64|32|16|8)  x y) && ssa.ShiftIsBounded(v) => (SRD (MOVBZreg x) y)
   211  (Rsh64x(64|32|16|8)  x y) && ssa.ShiftIsBounded(v) => (SRAD x y)
   212  (Rsh32x(64|32|16|8)  x y) && ssa.ShiftIsBounded(v) => (SRAW x y)
   213  (Rsh16x(64|32|16|8)  x y) && ssa.ShiftIsBounded(v) => (SRAD (MOVHreg x) y)
   214  (Rsh8x(64|32|16|8)   x y) && ssa.ShiftIsBounded(v) => (SRAD (MOVBreg x) y)
   215  
   216  // Unbounded shifts. Go shifts saturate to 0 or -1 when shifting beyond the number of
   217  // bits in a type, PPC64 shifts do not (see the ISA for details).
   218  //
   219  // Note, y is always non-negative.
   220  //
   221  // Note, ISELZ is intentionally not used in lower. Where possible, ISEL is converted to ISELZ in late lower
   222  // after all the ISEL folding rules have been exercised.
   223  
   224  ((Rsh64U|Lsh64)x64 <t> x y)  => (ISEL [0] (S(R|L)D <t> x y) (MOVDconst [0])        (CMPUconst y [64]))
   225  ((Rsh64U|Lsh64)x32 <t> x y)  => (ISEL [0] (S(R|L)D <t> x y) (MOVDconst [0])        (CMPWUconst y [64]))
   226  ((Rsh64U|Lsh64)x16 <t> x y)  => (ISEL [2] (S(R|L)D <t> x y) (MOVDconst [0])        (CMPconst [0] (ANDconst [0xFFC0] y)))
   227  ((Rsh64U|Lsh64)x8  <t> x y)  => (ISEL [2] (S(R|L)D <t> x y) (MOVDconst [0])        (CMPconst [0] (ANDconst [0x00C0] y)))
   228  (Rsh64x(64|32)     <t> x y)  => (ISEL [0] (SRAD    <t> x y) (SRADconst <t> x [63]) (CMP(U|WU)const y [64]))
   229  (Rsh64x16          <t> x y)  => (ISEL [2] (SRAD    <t> x y) (SRADconst <t> x [63]) (CMPconst [0] (ANDconst [0xFFC0] y)))
   230  (Rsh64x8           <t> x y)  => (ISEL [2] (SRAD    <t> x y) (SRADconst <t> x [63]) (CMPconst [0] (ANDconst [0x00C0] y)))
   231  
   232  ((Rsh32U|Lsh32)x64 <t> x y)  => (ISEL [0] (S(R|L)W <t> x y) (MOVDconst [0])        (CMPUconst y [32]))
   233  ((Rsh32U|Lsh32)x32 <t> x y)  => (ISEL [0] (S(R|L)W <t> x y) (MOVDconst [0])        (CMPWUconst y [32]))
   234  ((Rsh32U|Lsh32)x16 <t> x y)  => (ISEL [2] (S(R|L)W <t> x y) (MOVDconst [0])        (CMPconst [0] (ANDconst [0xFFE0] y)))
   235  ((Rsh32U|Lsh32)x8  <t> x y)  => (ISEL [2] (S(R|L)W <t> x y) (MOVDconst [0])        (CMPconst [0] (ANDconst [0x00E0] y)))
   236  (Rsh32x(64|32)     <t> x y)  => (ISEL [0] (SRAW    <t> x y) (SRAWconst <t> x [31]) (CMP(U|WU)const y [32]))
   237  (Rsh32x16          <t> x y)  => (ISEL [2] (SRAW    <t> x y) (SRAWconst <t> x [31]) (CMPconst [0] (ANDconst [0xFFE0] y)))
   238  (Rsh32x8           <t> x y)  => (ISEL [2] (SRAW    <t> x y) (SRAWconst <t> x [31]) (CMPconst [0] (ANDconst [0x00E0] y)))
   239  
   240  ((Rsh16U|Lsh16)x64 <t> x y) => (ISEL [0] (S(R|L)D  <t> (MOVHZreg x) y) (MOVDconst [0])                   (CMPUconst  y [16]))
   241  ((Rsh16U|Lsh16)x32 <t> x y) => (ISEL [0] (S(R|L)D  <t> (MOVHZreg x) y) (MOVDconst [0])                   (CMPWUconst y [16]))
   242  ((Rsh16U|Lsh16)x16 <t> x y) => (ISEL [2] (S(R|L)D  <t> (MOVHZreg x) y) (MOVDconst [0])                   (CMPconst [0] (ANDconst [0xFFF0] y)))
   243  ((Rsh16U|Lsh16)x8  <t> x y) => (ISEL [2] (S(R|L)D  <t> (MOVHZreg x) y) (MOVDconst [0])                   (CMPconst [0] (ANDconst [0x00F0] y)))
   244  (Rsh16x(64|32)     <t> x y) => (ISEL [0] (SRAD     <t> (MOVHreg  x) y) (SRADconst <t>  (MOVHreg x) [15]) (CMP(U|WU)const  y [16]))
   245  (Rsh16x16          <t> x y) => (ISEL [2] (SRAD     <t> (MOVHreg  x) y) (SRADconst <t>  (MOVHreg x) [15]) (CMPconst [0] (ANDconst [0xFFF0] y)))
   246  (Rsh16x8           <t> x y) => (ISEL [2] (SRAD     <t> (MOVHreg  x) y) (SRADconst <t>  (MOVHreg x) [15]) (CMPconst [0] (ANDconst [0x00F0] y)))
   247  
   248  ((Rsh8U|Lsh8)x64 <t> x y) => (ISEL [0] (S(R|L)D  <t> (MOVBZreg x) y) (MOVDconst [0])                  (CMPUconst  y [8]))
   249  ((Rsh8U|Lsh8)x32 <t> x y) => (ISEL [0] (S(R|L)D  <t> (MOVBZreg x) y) (MOVDconst [0])                  (CMPWUconst y [8]))
   250  ((Rsh8U|Lsh8)x16 <t> x y) => (ISEL [2] (S(R|L)D  <t> (MOVBZreg x) y) (MOVDconst [0])                  (CMPconst [0] (ANDconst [0xFFF8] y)))
   251  ((Rsh8U|Lsh8)x8  <t> x y) => (ISEL [2] (S(R|L)D  <t> (MOVBZreg x) y) (MOVDconst [0])                  (CMPconst [0] (ANDconst [0x00F8] y)))
   252  (Rsh8x(64|32)    <t> x y) => (ISEL [0] (SRAD     <t> (MOVBreg  x) y) (SRADconst <t>  (MOVBreg x) [7]) (CMP(U|WU)const  y [8]))
   253  (Rsh8x16         <t> x y) => (ISEL [2] (SRAD     <t> (MOVBreg  x) y) (SRADconst <t>  (MOVBreg x) [7]) (CMPconst [0] (ANDconst [0xFFF8] y)))
   254  (Rsh8x8          <t> x y) => (ISEL [2] (SRAD     <t> (MOVBreg  x) y) (SRADconst <t>  (MOVBreg x) [7]) (CMPconst [0] (ANDconst [0x00F8] y)))
   255  
   256  // Catch bounded shifts in situations like foo<<uint(shift&63) which might not be caught by the prove pass.
   257  (CMP(U|WU)const [d] (ANDconst z [c])) && uint64(d) > uint64(c) => (FlagLT)
   258  
   259  (ORN x (MOVDconst [-1])) => x
   260  
   261  (S(RAD|RD|LD) x (MOVDconst [c])) => (S(RAD|RD|LD)const [c&63 | (c>>6&1*63)] x)
   262  (S(RAW|RW|LW) x (MOVDconst [c])) => (S(RAW|RW|LW)const [c&31 | (c>>5&1*31)] x)
   263  
   264  (Addr {sym} base) => (MOVDaddr {sym} [0] base)
   265  (LocalAddr <t> {sym} base mem) && t.Elem().HasPointers() => (MOVDaddr {sym} (SPanchored base mem))
   266  (LocalAddr <t> {sym} base _)  && !t.Elem().HasPointers() => (MOVDaddr {sym} base)
   267  (OffPtr [off] ptr) => (ADD (MOVDconst <typ.Int64> [off]) ptr)
   268  (MOVDaddr {sym} [n] p:(ADD x y)) && sym == nil && n == 0 => p
   269  (MOVDaddr {sym} [n] ptr) && sym == nil && n == 0 && (ptr.Op == ssaop.OpArgIntReg || ptr.Op == ssaop.OpPhi) => ptr
   270  
   271  (Ctz64NonZero ...) => (Ctz64 ...)
   272  // Extend argument to ensure subsequent calculations have 64 correct bits.
   273  (Ctz(32|16|8)NonZero x) => (Ctz64 (ZeroExt(32|16|8)to64 x))
   274  
   275  (Ctz64 x) && buildcfg.GOPPC64 <= 8 => (POPCNTD (ANDN <typ.Int64> (ADDconst <typ.Int64> [-1] x) x))
   276  (Ctz32 x) && buildcfg.GOPPC64 <= 8 => (POPCNTW (MOVWZreg (ANDN <typ.UInt32> (ADDconst <typ.UInt32> [-1] x) x)))
   277  (Ctz16 x) && buildcfg.GOPPC64 <= 8 => (POPCNTW (MOVHZreg (ANDN <typ.Int16> (ADDconst <typ.Int16> [-1] x) x)))
   278  (Ctz8  x) && buildcfg.GOPPC64 <= 8 => (POPCNTB (MOVBZreg (ANDN <typ.UInt8> (ADDconst <typ.UInt8> [-1] x) x)))
   279  
   280  (Ctz64 x) && buildcfg.GOPPC64 >= 9 => (CNTTZD x)
   281  (Ctz32 x) && buildcfg.GOPPC64 >= 9 => (CNTTZW (MOVWZreg x))
   282  (Ctz16 x) && buildcfg.GOPPC64 >= 9 => (CNTTZD (OR <typ.UInt64> x (MOVDconst [1<<16])))
   283  (Ctz8  x) && buildcfg.GOPPC64 >= 9 => (CNTTZD (OR <typ.UInt64> x (MOVDconst [1<<8])))
   284  
   285  (BitLen64 x) => (SUBFCconst [64] (CNTLZD <typ.Int> x))
   286  (BitLen32 x) => (SUBFCconst [32] (CNTLZW <typ.Int> x))
   287  (BitLen(16|8) x) => (BitLen64 (ZeroExt(16|8)to64 x))
   288  
   289  (PopCount64 ...) => (POPCNTD ...)
   290  (PopCount(32|16|8) x) => (POPCNT(W|W|B) (MOV(W|H|B)Zreg x))
   291  
   292  (And(64|32|16|8) ...) => (AND ...)
   293  (Or(64|32|16|8) ...) => (OR ...)
   294  (Xor(64|32|16|8) ...) => (XOR ...)
   295  
   296  (Neg(64|32|16|8) ...) => (NEG ...)
   297  (Neg(64|32)F ...) => (FNEG ...)
   298  
   299  (Com(64|32|16|8) x) => (NOR x x)
   300  
   301  // Lowering boolean ops
   302  (AndB ...) => (AND ...)
   303  (OrB ...) => (OR ...)
   304  (Not x) => (XORconst [1] x)
   305  
   306  // Merge logical operations
   307  (AND x (NOR y y)) => (ANDN x y)
   308  (OR x (NOR y y)) => (ORN x y)
   309  
   310  // Lowering comparisons
   311  (EqB x y)  => (ANDconst [1] (EQV x y))
   312  // Sign extension dependence on operand sign sets up for sign/zero-extension elision later
   313  (Eq(8|16) x y) && x.Type.IsSigned() && y.Type.IsSigned() => (Equal (CMPW (SignExt(8|16)to32 x) (SignExt(8|16)to32 y)))
   314  (Eq(8|16) x y) => (Equal (CMPW (ZeroExt(8|16)to32 x) (ZeroExt(8|16)to32 y)))
   315  (Eq(32|64|Ptr) x y) => (Equal ((CMPW|CMP|CMP) x y))
   316  (Eq(32|64)F x y) => (Equal (FCMPU x y))
   317  
   318  (NeqB ...) => (XOR ...)
   319  // Like Eq8 and Eq16, prefer sign extension likely to enable later elision.
   320  (Neq(8|16) x y) && x.Type.IsSigned() && y.Type.IsSigned() => (NotEqual (CMPW (SignExt(8|16)to32 x) (SignExt(8|16)to32 y)))
   321  (Neq(8|16) x y)  => (NotEqual (CMPW (ZeroExt(8|16)to32 x) (ZeroExt(8|16)to32 y)))
   322  (Neq(32|64|Ptr) x y) => (NotEqual ((CMPW|CMP|CMP) x y))
   323  (Neq(32|64)F x y) => (NotEqual (FCMPU x y))
   324  
   325  (Less(8|16) x y)  => (LessThan (CMPW (SignExt(8|16)to32 x) (SignExt(8|16)to32 y)))
   326  (Less(32|64) x y) => (LessThan ((CMPW|CMP) x y))
   327  (Less(32|64)F x y) => (FLessThan (FCMPU x y))
   328  
   329  (Less(8|16)U x y)  => (LessThan (CMPWU (ZeroExt(8|16)to32 x) (ZeroExt(8|16)to32 y)))
   330  (Less(32|64)U x y) => (LessThan ((CMPWU|CMPU) x y))
   331  
   332  (Leq(8|16) x y)  => (LessEqual (CMPW (SignExt(8|16)to32 x) (SignExt(8|16)to32 y)))
   333  (Leq(32|64) x y) => (LessEqual ((CMPW|CMP) x y))
   334  (Leq(32|64)F x y) => (FLessEqual (FCMPU x y))
   335  
   336  (Leq(8|16)U x y)  => (LessEqual (CMPWU (ZeroExt(8|16)to32 x) (ZeroExt(8|16)to32 y)))
   337  (Leq(32|64)U x y) => (LessEqual (CMP(WU|U) x y))
   338  
   339  // Absorb pseudo-ops into blocks.
   340  (If (Equal cc) yes no) => (EQ cc yes no)
   341  (If (NotEqual cc) yes no) => (NE cc yes no)
   342  (If (LessThan cc) yes no) => (LT cc yes no)
   343  (If (LessEqual cc) yes no) => (LE cc yes no)
   344  (If (GreaterThan cc) yes no) => (GT cc yes no)
   345  (If (GreaterEqual cc) yes no) => (GE cc yes no)
   346  (If (FLessThan cc) yes no) => (FLT cc yes no)
   347  (If (FLessEqual cc) yes no) => (FLE cc yes no)
   348  (If (FGreaterThan cc) yes no) => (FGT cc yes no)
   349  (If (FGreaterEqual cc) yes no) => (FGE cc yes no)
   350  
   351  (If cond yes no) => (NE (CMPconst [0] (ANDconst [1] cond)) yes no)
   352  
   353  // Absorb boolean tests into block
   354  (NE (CMPconst [0] (ANDconst [1] ((Equal|NotEqual|LessThan|LessEqual|GreaterThan|GreaterEqual) cc))) yes no) => ((EQ|NE|LT|LE|GT|GE) cc yes no)
   355  (NE (CMPconst [0] (ANDconst [1] ((FLessThan|FLessEqual|FGreaterThan|FGreaterEqual) cc))) yes no) => ((FLT|FLE|FGT|FGE) cc yes no)
   356  
   357  // absorb flag constants into branches
   358  (EQ (FlagEQ) yes no) => (First yes no)
   359  (EQ (FlagLT) yes no) => (First no yes)
   360  (EQ (FlagGT) yes no) => (First no yes)
   361  
   362  (NE (FlagEQ) yes no) => (First no yes)
   363  (NE (FlagLT) yes no) => (First yes no)
   364  (NE (FlagGT) yes no) => (First yes no)
   365  
   366  (LT (FlagEQ) yes no) => (First no yes)
   367  (LT (FlagLT) yes no) => (First yes no)
   368  (LT (FlagGT) yes no) => (First no yes)
   369  
   370  (LE (FlagEQ) yes no) => (First yes no)
   371  (LE (FlagLT) yes no) => (First yes no)
   372  (LE (FlagGT) yes no) => (First no yes)
   373  
   374  (GT (FlagEQ) yes no) => (First no yes)
   375  (GT (FlagLT) yes no) => (First no yes)
   376  (GT (FlagGT) yes no) => (First yes no)
   377  
   378  (GE (FlagEQ) yes no) => (First yes no)
   379  (GE (FlagLT) yes no) => (First no yes)
   380  (GE (FlagGT) yes no) => (First yes no)
   381  
   382  // absorb InvertFlags into branches
   383  (LT (InvertFlags cmp) yes no) => (GT cmp yes no)
   384  (GT (InvertFlags cmp) yes no) => (LT cmp yes no)
   385  (LE (InvertFlags cmp) yes no) => (GE cmp yes no)
   386  (GE (InvertFlags cmp) yes no) => (LE cmp yes no)
   387  (EQ (InvertFlags cmp) yes no) => (EQ cmp yes no)
   388  (NE (InvertFlags cmp) yes no) => (NE cmp yes no)
   389  
   390  // constant comparisons
   391  (CMPWconst (MOVDconst [x]) [y]) && int32(x)==int32(y) => (FlagEQ)
   392  (CMPWconst (MOVDconst [x]) [y]) && int32(x)<int32(y)  => (FlagLT)
   393  (CMPWconst (MOVDconst [x]) [y]) && int32(x)>int32(y)  => (FlagGT)
   394  
   395  (CMPconst (MOVDconst [x]) [y]) && x==y => (FlagEQ)
   396  (CMPconst (MOVDconst [x]) [y]) && x<y  => (FlagLT)
   397  (CMPconst (MOVDconst [x]) [y]) && x>y  => (FlagGT)
   398  
   399  (CMPWUconst (MOVDconst [x]) [y]) && int32(x)==int32(y)  => (FlagEQ)
   400  (CMPWUconst (MOVDconst [x]) [y]) && uint32(x)<uint32(y) => (FlagLT)
   401  (CMPWUconst (MOVDconst [x]) [y]) && uint32(x)>uint32(y) => (FlagGT)
   402  
   403  (CMPUconst (MOVDconst [x]) [y]) && x==y  => (FlagEQ)
   404  (CMPUconst (MOVDconst [x]) [y]) && uint64(x)<uint64(y) => (FlagLT)
   405  (CMPUconst (MOVDconst [x]) [y]) && uint64(x)>uint64(y) => (FlagGT)
   406  
   407  // absorb flag constants into boolean values
   408  (Equal (FlagEQ)) => (MOVDconst [1])
   409  (Equal (FlagLT)) => (MOVDconst [0])
   410  (Equal (FlagGT)) => (MOVDconst [0])
   411  
   412  (NotEqual (FlagEQ)) => (MOVDconst [0])
   413  (NotEqual (FlagLT)) => (MOVDconst [1])
   414  (NotEqual (FlagGT)) => (MOVDconst [1])
   415  
   416  (LessThan (FlagEQ)) => (MOVDconst [0])
   417  (LessThan (FlagLT)) => (MOVDconst [1])
   418  (LessThan (FlagGT)) => (MOVDconst [0])
   419  
   420  (LessEqual (FlagEQ)) => (MOVDconst [1])
   421  (LessEqual (FlagLT)) => (MOVDconst [1])
   422  (LessEqual (FlagGT)) => (MOVDconst [0])
   423  
   424  (GreaterThan (FlagEQ)) => (MOVDconst [0])
   425  (GreaterThan (FlagLT)) => (MOVDconst [0])
   426  (GreaterThan (FlagGT)) => (MOVDconst [1])
   427  
   428  (GreaterEqual (FlagEQ)) => (MOVDconst [1])
   429  (GreaterEqual (FlagLT)) => (MOVDconst [0])
   430  (GreaterEqual (FlagGT)) => (MOVDconst [1])
   431  
   432  // absorb InvertFlags into boolean values
   433  ((Equal|NotEqual|LessThan|GreaterThan|LessEqual|GreaterEqual) (InvertFlags x)) => ((Equal|NotEqual|GreaterThan|LessThan|GreaterEqual|LessEqual) x)
   434  
   435  
   436  // Elide compares of bit tests
   437  ((EQ|NE|LT|LE|GT|GE) (CMPconst [0] z:(AND x y)) yes no) && z.Uses == 1 => ((EQ|NE|LT|LE|GT|GE) (Select1 <types.TypeFlags> (ANDCC x y)) yes no)
   438  ((EQ|NE|LT|LE|GT|GE) (CMPconst [0] z:(OR x y)) yes no) && z.Uses == 1 => ((EQ|NE|LT|LE|GT|GE) (Select1 <types.TypeFlags> (ORCC x y)) yes no)
   439  ((EQ|NE|LT|LE|GT|GE) (CMPconst [0] z:(XOR x y)) yes no) && z.Uses == 1 => ((EQ|NE|LT|LE|GT|GE) (Select1 <types.TypeFlags> (XORCC x y)) yes no)
   440  
   441  (CondSelect x y (SETBC [a] cmp))  => (ISEL [a] x y cmp)
   442  (CondSelect x y (SETBCR [a] cmp))  => (ISEL [a+4] x y cmp)
   443  // Only lower after bool is lowered. It should always lower. This helps ensure the folding below happens reliably.
   444  (CondSelect x y bool) && ssa.FlagArg(bool) == nil => (ISEL [6] x y (CMPconst [0] (ANDconst [1] bool)))
   445  // Fold any CR -> GPR -> CR transfers when applying the above rule.
   446  (ISEL [6] x y (CMPconst [0] (ANDconst [1] (SETBC [c] cmp)))) => (ISEL [c] x y cmp)
   447  (ISEL [6] x y ((CMP|CMPW)const [0] (SETBC [c] cmp))) => (ISEL [c] x y cmp)
   448  (ISEL [6] x y ((CMP|CMPW)const [0] (SETBCR [c] cmp))) => (ISEL [c+4] x y cmp)
   449  
   450  // Lowering loads
   451  (Load <t> ptr mem) && (ssa.Is64BitInt(t) ||ssa.IsPtr(t)) => (MOVDload ptr mem)
   452  (Load <t> ptr mem) && ssa.Is32BitInt(t) &&  t.IsSigned() => (MOVWload ptr mem)
   453  (Load <t> ptr mem) && ssa.Is32BitInt(t) && !t.IsSigned() => (MOVWZload ptr mem)
   454  (Load <t> ptr mem) && ssa.Is16BitInt(t) &&  t.IsSigned() => (MOVHload ptr mem)
   455  (Load <t> ptr mem) && ssa.Is16BitInt(t) && !t.IsSigned() => (MOVHZload ptr mem)
   456  (Load <t> ptr mem) && t.IsBoolean() => (MOVBZload ptr mem)
   457  (Load <t> ptr mem) && ssa.Is8BitInt(t) &&  t.IsSigned() => (MOVBreg (MOVBZload ptr mem)) // PPC has no signed-byte load.
   458  (Load <t> ptr mem) && ssa.Is8BitInt(t) && !t.IsSigned() => (MOVBZload ptr mem)
   459  
   460  (Load <t> ptr mem) && ssa.Is32BitFloat(t) => (FMOVSload ptr mem)
   461  (Load <t> ptr mem) && ssa.Is64BitFloat(t) => (FMOVDload ptr mem)
   462  
   463  (Store {t} ptr val mem) && t.Size() == 8 &&  t.IsFloat() => (FMOVDstore ptr val mem)
   464  (Store {t} ptr val mem) && t.Size() == 4 &&  t.IsFloat() => (FMOVSstore ptr val mem)
   465  (Store {t} ptr val mem) && t.Size() == 8 && !t.IsFloat() => (MOVDstore ptr val mem)
   466  (Store {t} ptr val mem) && t.Size() == 4 && !t.IsFloat() => (MOVWstore ptr val mem)
   467  (Store {t} ptr val mem) && t.Size() == 2 => (MOVHstore ptr val mem)
   468  (Store {t} ptr val mem) && t.Size() == 1 => (MOVBstore ptr val mem)
   469  
   470  // Using Zero instead of LoweredZero allows the
   471  // target address to be folded where possible.
   472  (Zero [0] _ mem) => mem
   473  (Zero [1] destptr mem) => (MOVBstorezero destptr mem)
   474  (Zero [2] destptr mem) =>
   475  	(MOVHstorezero destptr mem)
   476  (Zero [3] destptr mem) =>
   477  	(MOVBstorezero [2] destptr
   478  		(MOVHstorezero destptr mem))
   479  (Zero [4] destptr mem) =>
   480  	(MOVWstorezero destptr mem)
   481  (Zero [5] destptr mem) =>
   482  	(MOVBstorezero [4] destptr
   483          	(MOVWstorezero destptr mem))
   484  (Zero [6] destptr mem) =>
   485  	(MOVHstorezero [4] destptr
   486  		(MOVWstorezero destptr mem))
   487  (Zero [7] destptr mem) =>
   488  	(MOVBstorezero [6] destptr
   489  		(MOVHstorezero [4] destptr
   490  			(MOVWstorezero destptr mem)))
   491  
   492  (Zero [8] {t} destptr mem) => (MOVDstorezero destptr mem)
   493  (Zero [12] {t} destptr mem) =>
   494          (MOVWstorezero [8] destptr
   495                  (MOVDstorezero [0] destptr mem))
   496  (Zero [16] {t} destptr mem) =>
   497         (MOVDstorezero [8] destptr
   498                  (MOVDstorezero [0] destptr mem))
   499  (Zero [24] {t} destptr mem) =>
   500         (MOVDstorezero [16] destptr
   501                 (MOVDstorezero [8] destptr
   502                         (MOVDstorezero [0] destptr mem)))
   503  (Zero [32] {t} destptr mem) =>
   504         (MOVDstorezero [24] destptr
   505                 (MOVDstorezero [16] destptr
   506                         (MOVDstorezero [8] destptr
   507                                 (MOVDstorezero [0] destptr mem))))
   508  
   509  // Handle cases not handled above
   510  // Lowered Short cases do not generate loops, and as a result don't clobber
   511  // the address registers or flags.
   512  (Zero [s] ptr mem) && buildcfg.GOPPC64 <= 8 && s < 64 => (LoweredZeroShort [s] ptr mem)
   513  (Zero [s] ptr mem) && buildcfg.GOPPC64 <= 8 => (LoweredZero [s] ptr mem)
   514  // Add check so that sizes below 16 are cleared with scalar stores only and not Zero128
   515  (Zero [s] ptr mem) && s < 16 && buildcfg.GOPPC64 >= 9 => (LoweredZeroShort [s] ptr mem)
   516  (Zero [s] ptr mem) && s < 128 && buildcfg.GOPPC64 >= 9 => (LoweredQuadZeroShort [s] ptr (Zero128 <typ.Vec128>) mem)
   517  (Zero [s] ptr mem) && buildcfg.GOPPC64 >= 9 => (LoweredQuadZero [s] ptr (Zero128 <typ.Vec128>) mem)
   518  
   519  // moves
   520  (Move [0] _ _ mem) => mem
   521  (Move [1] dst src mem) => (MOVBstore dst (MOVBZload src mem) mem)
   522  (Move [2] dst src mem) =>
   523          (MOVHstore dst (MOVHZload src mem) mem)
   524  (Move [4] dst src mem) =>
   525  	(MOVWstore dst (MOVWZload src mem) mem)
   526  // MOVD for load and store must have offsets that are multiple of 4
   527  (Move [8] {t} dst src mem) =>
   528  	(MOVDstore dst (MOVDload src mem) mem)
   529  (Move [3] dst src mem) =>
   530          (MOVBstore [2] dst (MOVBZload [2] src mem)
   531                  (MOVHstore dst (MOVHload src mem) mem))
   532  (Move [5] dst src mem) =>
   533          (MOVBstore [4] dst (MOVBZload [4] src mem)
   534                  (MOVWstore dst (MOVWZload src mem) mem))
   535  (Move [6] dst src mem) =>
   536          (MOVHstore [4] dst (MOVHZload [4] src mem)
   537                  (MOVWstore dst (MOVWZload src mem) mem))
   538  (Move [7] dst src mem) =>
   539          (MOVBstore [6] dst (MOVBZload [6] src mem)
   540                  (MOVHstore [4] dst (MOVHZload [4] src mem)
   541                          (MOVWstore dst (MOVWZload src mem) mem)))
   542  
   543  // Large move uses a loop. Since the address is computed and the
   544  // offset is zero, any alignment can be used.
   545  (Move [s] dst src mem) && s > 8 && buildcfg.GOPPC64 <= 8 && ssa.LogLargeCopyValue(v, s) =>
   546          (LoweredMove [s] dst src mem)
   547  (Move [s] dst src mem) && s > 8 && s <= 64 && buildcfg.GOPPC64 >= 9 =>
   548          (LoweredQuadMoveShort [s] dst src mem)
   549  (Move [s] dst src mem) && s > 8 && buildcfg.GOPPC64 >= 9 && ssa.LogLargeCopyValue(v, s) =>
   550          (LoweredQuadMove [s] dst src mem)
   551  
   552  // Calls
   553  // Lowering calls
   554  (StaticCall ...) => (CALLstatic ...)
   555  (ClosureCall ...) => (CALLclosure ...)
   556  (InterCall ...) => (CALLinter ...)
   557  (TailCall ...) => (CALLtail ...)
   558  (TailCallInter ...) => (CALLtailinter ...)
   559  
   560  // Miscellaneous
   561  (GetClosurePtr ...) => (LoweredGetClosurePtr ...)
   562  (GetCallerSP ...) => (LoweredGetCallerSP ...)
   563  (GetCallerPC ...) => (LoweredGetCallerPC ...)
   564  (IsNonNil ptr) => (NotEqual (CMPconst [0] ptr))
   565  (IsInBounds idx len) => (LessThan (CMPU idx len))
   566  (IsSliceInBounds idx len) => (LessEqual (CMPU idx len))
   567  (NilCheck ...) => (LoweredNilCheck ...)
   568  
   569  // Write barrier.
   570  (WB ...) => (LoweredWB ...)
   571  
   572  // Publication barrier as intrinsic
   573  (PubBarrier ...) => (LoweredPubBarrier ...)
   574  
   575  (PanicBounds ...) => (LoweredPanicBoundsRR ...)
   576  (LoweredPanicBoundsRR [kind] x (MOVDconst [c]) mem) => (LoweredPanicBoundsRC [kind] x {ssa.PanicBoundsC{C:c}} mem)
   577  (LoweredPanicBoundsRR [kind] (MOVDconst [c]) y mem) => (LoweredPanicBoundsCR [kind] {ssa.PanicBoundsC{C:c}} y mem)
   578  (LoweredPanicBoundsRC [kind] {p} (MOVDconst [c]) mem) => (LoweredPanicBoundsCC [kind] {ssa.PanicBoundsCC{Cx:c, Cy:p.C}} mem)
   579  (LoweredPanicBoundsCR [kind] {p} (MOVDconst [c]) mem) => (LoweredPanicBoundsCC [kind] {ssa.PanicBoundsCC{Cx:p.C, Cy:c}} mem)
   580  
   581  // Optimizations
   582  // Note that PPC "logical" immediates come in 0:15 and 16:31 unsigned immediate forms,
   583  // so ORconst, XORconst easily expand into a pair.
   584  
   585  // Include very-large constants in the const-const case.
   586  (AND (MOVDconst [c]) (MOVDconst [d])) => (MOVDconst [c&d])
   587  (OR (MOVDconst [c]) (MOVDconst [d])) => (MOVDconst [c|d])
   588  (XOR (MOVDconst [c]) (MOVDconst [d])) => (MOVDconst [c^d])
   589  (ORN (MOVDconst [c]) (MOVDconst [d])) => (MOVDconst [c|^d])
   590  (ANDN (MOVDconst [c]) (MOVDconst [d])) => (MOVDconst [c&^d])
   591  (NOR (MOVDconst [c]) (MOVDconst [d])) => (MOVDconst [^(c|d)])
   592  
   593  // Discover consts
   594  (AND x (MOVDconst [-1])) => x
   595  (AND x (MOVDconst [c])) && isU16Bit(c) => (ANDconst [c] x)
   596  (XOR x (MOVDconst [c])) && ssa.IsU32Bit(c) => (XORconst [c] x)
   597  (OR x (MOVDconst [c])) && ssa.IsU32Bit(c) => (ORconst [c] x)
   598  
   599  // Simplify consts
   600  (ANDconst [c] (ANDconst [d] x)) => (ANDconst [c&d] x)
   601  (ORconst [c] (ORconst [d] x)) => (ORconst [c|d] x)
   602  (XORconst [c] (XORconst [d] x)) => (XORconst [c^d] x)
   603  (ANDconst [-1] x) => x
   604  (ANDconst [0] _) => (MOVDconst [0])
   605  (XORconst [0] x) => x
   606  (ORconst [-1] _) => (MOVDconst [-1])
   607  (ORconst [0] x) => x
   608  
   609  // zero-extend of small and => small and
   610  (MOVBZreg y:(ANDconst [c] _)) && uint64(c) <= 0xFF => y
   611  (MOVHZreg y:(ANDconst [c] _)) && uint64(c) <= 0xFFFF => y
   612  (MOVWZreg y:(ANDconst [c] _)) && uint64(c) <= 0xFFFFFFFF => y
   613  (MOVWZreg y:(AND (MOVDconst [c]) _)) && uint64(c) <= 0xFFFFFFFF => y
   614  
   615  // sign extend of small-positive and => small-positive-and
   616  (MOVBreg y:(ANDconst [c] _)) && uint64(c) <= 0x7F => y
   617  (MOVHreg y:(ANDconst [c] _)) && uint64(c) <= 0x7FFF => y
   618  (MOVWreg y:(ANDconst [c] _)) && uint64(c) <= 0xFFFF => y // 0xFFFF is largest immediate constant, when regarded as 32-bit is > 0
   619  (MOVWreg y:(AND (MOVDconst [c]) _)) && uint64(c) <= 0x7FFFFFFF => y
   620  
   621  // small and of zero-extend => either zero-extend or small and
   622  (ANDconst [c] y:(MOVBZreg _)) && c&0xFF == 0xFF => y
   623  (ANDconst [0xFF] (MOVBreg x)) => (MOVBZreg x)
   624  (ANDconst [c] y:(MOVHZreg _))  && c&0xFFFF == 0xFFFF => y
   625  (ANDconst [0xFFFF] (MOVHreg x)) => (MOVHZreg x)
   626  
   627  (AND (MOVDconst [c]) y:(MOVWZreg _))  && c&0xFFFFFFFF == 0xFFFFFFFF => y
   628  (AND (MOVDconst [0xFFFFFFFF]) y:(MOVWreg x)) => (MOVWZreg x)
   629  // normal case
   630  (ANDconst [c] (MOVBZreg x)) => (ANDconst [c&0xFF] x)
   631  (ANDconst [c] (MOVHZreg x)) => (ANDconst [c&0xFFFF] x)
   632  (ANDconst [c] (MOVWZreg x)) => (ANDconst [c&0xFFFFFFFF] x)
   633  
   634  // Eliminate unnecessary sign/zero extend following right shift
   635  (MOV(B|H|W)Zreg (SRWconst [c] (MOVBZreg x))) => (SRWconst [c] (MOVBZreg x))
   636  (MOV(H|W)Zreg (SRWconst [c] (MOVHZreg x))) => (SRWconst [c] (MOVHZreg x))
   637  (MOVWZreg (SRWconst [c] (MOVWZreg x))) => (SRWconst [c] (MOVWZreg x))
   638  (MOV(B|H|W)reg (SRAWconst [c] (MOVBreg x))) => (SRAWconst [c] (MOVBreg x))
   639  (MOV(H|W)reg (SRAWconst [c] (MOVHreg x))) => (SRAWconst [c] (MOVHreg x))
   640  (MOVWreg (SRAWconst [c] (MOVWreg x))) => (SRAWconst [c] (MOVWreg x))
   641  
   642  (MOV(WZ|W)reg (S(R|RA)Wconst [c] x)) && x.Type.Size() <= 32 => (S(R|RA)Wconst [c] x)
   643  (MOV(HZ|H)reg (S(R|RA)Wconst [c] x)) && x.Type.Size() <= 16 => (S(R|RA)Wconst [c] x)
   644  (MOV(BZ|B)reg (S(R|RA)Wconst [c] x)) && x.Type.Size() == 8 => (S(R|RA)Wconst [c] x)
   645  
   646  // initial right shift will handle sign/zero extend
   647  (MOVBZreg (SRDconst [c] x)) && c>=56 => (SRDconst [c] x)
   648  (MOVBreg (SRDconst [c] x)) && c>56 => (SRDconst [c] x)
   649  (MOVBreg (SRDconst [c] x)) && c==56 => (SRADconst [c] x)
   650  (MOVBreg (SRADconst [c] x)) && c>=56 => (SRADconst [c] x)
   651  (MOVBZreg (SRWconst [c] x)) && c>=24 => (SRWconst [c] x)
   652  (MOVBreg (SRWconst [c] x)) && c>24 => (SRWconst [c] x)
   653  (MOVBreg (SRWconst [c] x)) && c==24 => (SRAWconst [c] x)
   654  (MOVBreg (SRAWconst [c] x)) && c>=24 => (SRAWconst [c] x)
   655  
   656  (MOVHZreg (SRDconst [c] x)) && c>=48 => (SRDconst [c] x)
   657  (MOVHreg (SRDconst [c] x)) && c>48 => (SRDconst [c] x)
   658  (MOVHreg (SRDconst [c] x)) && c==48 => (SRADconst [c] x)
   659  (MOVHreg (SRADconst [c] x)) && c>=48 => (SRADconst [c] x)
   660  (MOVHZreg (SRWconst [c] x)) && c>=16 => (SRWconst [c] x)
   661  (MOVHreg (SRWconst [c] x)) && c>16 => (SRWconst [c] x)
   662  (MOVHreg (SRAWconst [c] x)) && c>=16 => (SRAWconst [c] x)
   663  (MOVHreg (SRWconst [c] x)) && c==16 => (SRAWconst [c] x)
   664  
   665  (MOVWZreg (SRDconst [c] x)) && c>=32 => (SRDconst [c] x)
   666  (MOVWreg (SRDconst [c] x)) && c>32 => (SRDconst [c] x)
   667  (MOVWreg (SRADconst [c] x)) && c>=32 => (SRADconst [c] x)
   668  (MOVWreg (SRDconst [c] x)) && c==32 => (SRADconst [c] x)
   669  
   670  // Various redundant zero/sign extension combinations.
   671  (MOVBZreg y:(MOVBZreg _)) => y  // repeat
   672  (MOVBreg y:(MOVBreg _)) => y // repeat
   673  (MOVBreg (MOVBZreg x)) => (MOVBreg x)
   674  (MOVBZreg (MOVBreg x)) => (MOVBZreg x)
   675  
   676  // Catch any remaining rotate+shift cases
   677  (MOVBZreg (SRWconst x [s])) && ssa.MergePPC64AndSrwi(0xFF,s) != 0 => (RLWINM [ssa.MergePPC64AndSrwi(0xFF,s)] x)
   678  (MOVBZreg (RLWINM [r] y)) && mergePPC64AndRlwinm(0xFF,r) != 0 => (RLWINM [mergePPC64AndRlwinm(0xFF,r)] y)
   679  (MOVHZreg (RLWINM [r] y)) && mergePPC64AndRlwinm(0xFFFF,r) != 0 => (RLWINM [mergePPC64AndRlwinm(0xFFFF,r)] y)
   680  (MOVWZreg (RLWINM [r] y)) && mergePPC64MovwzregRlwinm(r) != 0 => (RLWINM [mergePPC64MovwzregRlwinm(r)] y)
   681  (ANDconst [m] (RLWINM [r] y)) && mergePPC64AndRlwinm(uint32(m),r) != 0 => (RLWINM [mergePPC64AndRlwinm(uint32(m),r)] y)
   682  (SLDconst [s] (RLWINM [r] y)) && mergePPC64SldiRlwinm(s,r) != 0 => (RLWINM [mergePPC64SldiRlwinm(s,r)] y)
   683  (RLWINM [r] (MOVHZreg u)) && mergePPC64RlwinmAnd(r,0xFFFF) != 0 => (RLWINM [mergePPC64RlwinmAnd(r,0xFFFF)] u)
   684  (RLWINM [r] (ANDconst [a] u)) && mergePPC64RlwinmAnd(r,uint32(a)) != 0 => (RLWINM [mergePPC64RlwinmAnd(r,uint32(a))] u)
   685  // SLWconst is a special case of RLWNM which always zero-extends the result.
   686  (SLWconst [s] (MOVWZreg w)) => (SLWconst [s] w)
   687  (MOVWZreg w:(SLWconst u)) => w
   688  
   689  // H - there are more combinations than these
   690  
   691  (MOVHZreg y:(MOV(H|B)Zreg _)) => y // repeat
   692  (MOVHZreg y:(MOVHBRload _ _)) => y
   693  
   694  (MOVHreg y:(MOV(H|B)reg _)) => y // repeat
   695  
   696  (MOV(H|HZ)reg y:(MOV(HZ|H)reg x)) => (MOV(H|HZ)reg x)
   697  
   698  // W - there are more combinations than these
   699  
   700  (MOV(WZ|WZ|WZ|W|W|W)reg y:(MOV(WZ|HZ|BZ|W|H|B)reg _)) => y // repeat
   701  (MOVWZreg y:(MOV(H|W)BRload _ _)) => y
   702  
   703  (MOV(W|WZ)reg y:(MOV(WZ|W)reg x)) => (MOV(W|WZ)reg x)
   704  
   705  // Truncate then logical then truncate: omit first, lesser or equal truncate
   706  (MOVWZreg ((OR|XOR|AND) x (MOVWZreg y))) => (MOVWZreg ((OR|XOR|AND) <typ.UInt32> x y))
   707  (MOVHZreg ((OR|XOR|AND) x (MOVWZreg y))) => (MOVHZreg ((OR|XOR|AND) <typ.UInt16> x y))
   708  (MOVHZreg ((OR|XOR|AND) x (MOVHZreg y))) => (MOVHZreg ((OR|XOR|AND) <typ.UInt16> x y))
   709  (MOVBZreg ((OR|XOR|AND) x (MOVWZreg y))) => (MOVBZreg ((OR|XOR|AND) <typ.UInt8> x y))
   710  (MOVBZreg ((OR|XOR|AND) x (MOVHZreg y))) => (MOVBZreg ((OR|XOR|AND) <typ.UInt8> x y))
   711  (MOVBZreg ((OR|XOR|AND) x (MOVBZreg y))) => (MOVBZreg ((OR|XOR|AND) <typ.UInt8> x y))
   712  
   713  (MOV(B|H|W)Zreg z:(ANDconst [c] (MOVBZload ptr x))) => z
   714  (MOV(B|H|W)Zreg z:(AND y (MOV(B|H|W)Zload ptr x))) => z
   715  (MOV(H|W)Zreg z:(ANDconst [c] (MOVHZload ptr x))) => z
   716  (MOVWZreg z:(ANDconst [c] (MOVWZload ptr x))) => z
   717  
   718  // Arithmetic constant ops
   719  
   720  (ADD x (MOVDconst <t> [c])) && ssa.Is32Bit(c) && !t.IsPtr() => (ADDconst [c] x)
   721  (ADDconst [c] (ADDconst [d] x)) && ssa.Is32Bit(c+d) => (ADDconst [c+d] x)
   722  (ADDconst [0] x) => x
   723  (SUB x (MOVDconst [c])) && ssa.Is32Bit(-c) => (ADDconst [-c] x)
   724  
   725  (ADDconst [c] (MOVDaddr [d] {sym} x)) && ssa.Is32Bit(c+int64(d)) => (MOVDaddr [int32(c+int64(d))] {sym} x)
   726  (ADDconst [c] x:(SP)) && ssa.Is32Bit(c) => (MOVDaddr [int32(c)] x) // so it is rematerializeable
   727  
   728  (MULL(W|D) x (MOVDconst [c])) && ssa.Is16Bit(c) => (MULL(W|D)const [int32(c)] x)
   729  
   730  // Subtract from (with carry, but ignored) constant.
   731  // Note, these clobber the carry bit.
   732  (SUB (MOVDconst [c]) x) && ssa.Is32Bit(c) => (SUBFCconst [c] x)
   733  (SUBFCconst [c] (NEG x)) => (ADDconst [c] x)
   734  (SUBFCconst [c] (SUBFCconst [d] x)) && ssa.Is32Bit(c-d) => (ADDconst [c-d] x)
   735  (SUBFCconst [0] x) => (NEG x)
   736  (ADDconst [c] (SUBFCconst [d] x)) && ssa.Is32Bit(c+d) => (SUBFCconst [c+d] x)
   737  (NEG (ADDconst [c] x)) && ssa.Is32Bit(-c) => (SUBFCconst [-c] x)
   738  (NEG (SUBFCconst [c] x)) && ssa.Is32Bit(-c) => (ADDconst [-c] x)
   739  (NEG (SUB x y)) => (SUB y x)
   740  (NEG (NEG x)) => x
   741  
   742  // Use register moves instead of stores and loads to move int<=>float values
   743  // Common with math Float64bits, Float64frombits
   744  (MOVDload [off] {sym} ptr (FMOVDstore [off] {sym} ptr x _)) => (MFVSRD x)
   745  (FMOVDload [off] {sym} ptr (MOVDstore [off] {sym} ptr x _)) => (MTVSRD x)
   746  
   747  (FMOVDstore [off] {sym} ptr (MTVSRD x) mem) => (MOVDstore [off] {sym} ptr x mem)
   748  (MOVDstore [off] {sym} ptr (MFVSRD x) mem) => (FMOVDstore [off] {sym} ptr x mem)
   749  
   750  (MTVSRD (MOVDconst [c])) && !math.IsNaN(math.Float64frombits(uint64(c))) => (FMOVDconst [math.Float64frombits(uint64(c))])
   751  (MFVSRD (FMOVDconst [c])) => (MOVDconst [int64(math.Float64bits(c))])
   752  
   753  (MTVSRD x:(MOVDload [off] {sym} ptr mem)) && x.Uses == 1 && ssa.Clobber(x) => @x.Block (FMOVDload [off] {sym} ptr mem)
   754  (MFVSRD x:(FMOVDload [off] {sym} ptr mem)) && x.Uses == 1 && ssa.Clobber(x) => @x.Block (MOVDload [off] {sym} ptr mem)
   755  
   756  // Rules for MOV* or FMOV* ops determine when indexed (MOV*loadidx or MOV*storeidx)
   757  // or non-indexed (MOV*load or MOV*store) should be used. Indexed instructions
   758  // require an extra instruction and register to load the index so non-indexed is preferred.
   759  // Indexed ops generate indexed load or store instructions for all GOPPC64 values.
   760  // Non-indexed ops generate DS-form loads and stores when the offset fits in 16 bits,
   761  // and on power8 and power9, a multiple of 4 is required for MOVW and MOVD ops.
   762  // On power10, prefixed loads and stores can be used for offsets > 16 bits and <= 32 bits.
   763  // and support for PC relative addressing must be available if relocation is needed.
   764  // On power10, the assembler will determine when to use DS-form or prefixed
   765  // instructions for non-indexed ops depending on the value of the offset.
   766  //
   767  // Fold offsets for stores.
   768  (MOV(D|W|H|B)store [off1] {sym} (ADDconst [off2] x) val mem) && (ssa.Is16Bit(int64(off1)+off2) || (ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1)+off2))) => (MOV(D|W|H|B)store [off1+int32(off2)] {sym} x val mem)
   769  
   770  (FMOV(S|D)store [off1] {sym} (ADDconst [off2] ptr) val mem) && (ssa.Is16Bit(int64(off1)+off2) || (ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1)+off2))) => (FMOV(S|D)store [off1+int32(off2)] {sym} ptr val mem)
   771  
   772  // Fold address into load/store.
   773  // If power10 with PCRel is not available, then
   774  // the assembler needs to generate several instructions and use
   775  // temp register for accessing global, and each time it will reload
   776  // the temp register. So don't fold address of global in that case if there is more than
   777  // one use.
   778  (MOV(B|H|W|D)store [off1] {sym1} p:(MOVDaddr [off2] {sym2} ptr) val mem) && ssa.CanMergeSym(sym1,sym2)
   779  	&& ((ssa.Is16Bit(int64(off1+off2)) && (ptr.Op != ssaop.OpSB || p.Uses == 1)) || (ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1+off2)))) =>
   780          (MOV(B|H|W|D)store [off1+off2] {ssa.MergeSym(sym1,sym2)} ptr val mem)
   781  
   782  (FMOV(S|D)store [off1] {sym1} p:(MOVDaddr [off2] {sym2} ptr) val mem) && ssa.CanMergeSym(sym1,sym2)
   783  	&& ((ssa.Is16Bit(int64(off1+off2)) && (ptr.Op != ssaop.OpSB || p.Uses == 1)) || (ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1+off2)))) =>
   784          (FMOV(S|D)store [off1+off2] {ssa.MergeSym(sym1,sym2)} ptr val mem)
   785  
   786  (MOV(B|H|W)Zload [off1] {sym1} p:(MOVDaddr [off2] {sym2} ptr) mem) && ssa.CanMergeSym(sym1,sym2)
   787  	&& ((ssa.Is16Bit(int64(off1+off2)) && (ptr.Op != ssaop.OpSB || p.Uses == 1)) || (ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1+off2)))) =>
   788          (MOV(B|H|W)Zload [off1+off2] {ssa.MergeSym(sym1,sym2)} ptr mem)
   789  (MOV(H|W|D)load [off1] {sym1} p:(MOVDaddr [off2] {sym2} ptr) mem) && ssa.CanMergeSym(sym1,sym2)
   790  	&& ((ssa.Is16Bit(int64(off1+off2)) && (ptr.Op != ssaop.OpSB || p.Uses == 1)) || (ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1+off2)))) =>
   791          (MOV(H|W|D)load [off1+off2] {ssa.MergeSym(sym1,sym2)} ptr mem)
   792  (FMOV(S|D)load [off1] {sym1} p:(MOVDaddr [off2] {sym2} ptr) mem) && ssa.CanMergeSym(sym1,sym2)
   793  	&& ((ssa.Is16Bit(int64(off1+off2)) && (ptr.Op != ssaop.OpSB || p.Uses == 1)) || (ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1+off2)))) =>
   794          (FMOV(S|D)load [off1+off2] {ssa.MergeSym(sym1,sym2)} ptr mem)
   795  
   796  // Fold offsets for loads.
   797  (FMOV(S|D)load [off1] {sym} (ADDconst [off2] ptr) mem) && (ssa.Is16Bit(int64(off1)+off2) || (ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1)+off2))) => (FMOV(S|D)load [off1+int32(off2)] {sym} ptr mem)
   798  
   799  (MOV(D|W|WZ|H|HZ|BZ)load [off1] {sym} (ADDconst [off2] x) mem) && (ssa.Is16Bit(int64(off1)+off2) || (ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1)+off2))) => (MOV(D|W|WZ|H|HZ|BZ)load [off1+int32(off2)] {sym} x mem)
   800  
   801  // Determine load + addressing that can be done as a register indexed load
   802  (MOV(D|W|WZ|H|HZ|BZ)load [0] {sym} p:(ADD ptr idx) mem) && sym == nil && p.Uses == 1 => (MOV(D|W|WZ|H|HZ|BZ)loadidx ptr idx mem)
   803  
   804  // See comments above concerning selection of indexed vs. non-indexed ops.
   805  // These cases don't have relocation.
   806  (MOV(D|W)loadidx ptr (MOVDconst [c]) mem) && ((ssa.Is16Bit(c) && c%4 == 0) || (buildcfg.GOPPC64 >= 10 && ssa.Is32Bit(c))) => (MOV(D|W)load [int32(c)] ptr mem)
   807  (MOV(WZ|H|HZ|BZ)loadidx ptr (MOVDconst [c]) mem) && (ssa.Is16Bit(c) || (buildcfg.GOPPC64 >= 10 && ssa.Is32Bit(c))) => (MOV(WZ|H|HZ|BZ)load [int32(c)] ptr mem)
   808  (MOV(D|W)loadidx (MOVDconst [c]) ptr mem) && ((ssa.Is16Bit(c) && c%4 == 0) || (buildcfg.GOPPC64 >= 10 && ssa.Is32Bit(c))) => (MOV(D|W)load [int32(c)] ptr mem)
   809  (MOV(WZ|H|HZ|BZ)loadidx (MOVDconst [c]) ptr mem) && (ssa.Is16Bit(c) || (buildcfg.GOPPC64 >= 10 && ssa.Is32Bit(c))) => (MOV(WZ|H|HZ|BZ)load [int32(c)] ptr mem)
   810  
   811  // Store of zero => storezero
   812  (MOV(D|W|H|B)store [off] {sym} ptr (MOVDconst [0]) mem) => (MOV(D|W|H|B)storezero [off] {sym} ptr mem)
   813  
   814  // Fold offsets for storezero
   815  (MOV(D|W|H|B)storezero [off1] {sym} (ADDconst [off2] x) mem) && ((ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1)+off2)) || (ssa.Is16Bit(int64(off1)+off2))) =>
   816      (MOV(D|W|H|B)storezero [off1+int32(off2)] {sym} x mem)
   817  
   818  // Stores with addressing that can be done as indexed stores
   819  (MOV(D|W|H|B)store [0] {sym} p:(ADD ptr idx) val mem) && sym == nil && p.Uses == 1 => (MOV(D|W|H|B)storeidx ptr idx val mem)
   820  
   821  (MOVDstoreidx ptr (MOVDconst [c]) val mem) && ((ssa.Is16Bit(c) && c%4 == 0) || (buildcfg.GOPPC64 >= 10 && ssa.Is32Bit(c))) => (MOVDstore [int32(c)] ptr val mem)
   822  (MOV(W|H|B)storeidx ptr (MOVDconst [c]) val mem) && (ssa.Is16Bit(c) || (buildcfg.GOPPC64 >= 10 && ssa.Is32Bit(c))) => (MOV(W|H|B)store [int32(c)] ptr val mem)
   823  (MOVDstoreidx (MOVDconst [c]) ptr val mem) && ((ssa.Is16Bit(c) && c%4 == 0) || (buildcfg.GOPPC64 >= 10 && ssa.Is32Bit(c))) => (MOVDstore [int32(c)] ptr val mem)
   824  (MOV(W|H|B)storeidx (MOVDconst [c]) ptr val mem) && (ssa.Is16Bit(c) || (buildcfg.GOPPC64 >= 10 && ssa.Is32Bit(c))) => (MOV(W|H|B)store [int32(c)] ptr val mem)
   825  
   826  // Fold symbols into storezero
   827  (MOV(D|W|H|B)storezero [off1] {sym1} p:(MOVDaddr [off2] {sym2} x) mem) && ssa.CanMergeSym(sym1,sym2)
   828  	&& ((ssa.Is16Bit(int64(off1+off2)) && (x.Op != ssaop.OpSB || p.Uses == 1)) || (ssa.SupportsPPC64PCRel() && ssa.Is32Bit(int64(off1+off2)))) =>
   829      (MOV(D|W|H|B)storezero [off1+off2] {ssa.MergeSym(sym1,sym2)} x mem)
   830  
   831  // atomic intrinsics
   832  (AtomicLoad(8|32|64|Ptr)  ptr mem) => (LoweredAtomicLoad(8|32|64|Ptr) [1] ptr mem)
   833  (AtomicLoadAcq(32|64)     ptr mem) => (LoweredAtomicLoad(32|64) [0] ptr mem)
   834  
   835  (AtomicStore(8|32|64)    ptr val mem) => (LoweredAtomicStore(8|32|64) [1] ptr val mem)
   836  (AtomicStoreRel(32|64)   ptr val mem) => (LoweredAtomicStore(32|64) [0] ptr val mem)
   837  
   838  (AtomicExchange(8|32|64) ...) => (LoweredAtomicExchange(8|32|64) ...)
   839  
   840  (AtomicAdd(32|64) ...) => (LoweredAtomicAdd(32|64) ...)
   841  
   842  (AtomicCompareAndSwap(32|64) ptr old new_ mem) => (LoweredAtomicCas(32|64) [1] ptr old new_ mem)
   843  (AtomicCompareAndSwapRel32   ptr old new_ mem) => (LoweredAtomicCas32 [0] ptr old new_ mem)
   844  
   845  (AtomicAnd(8|32)  ...) => (LoweredAtomicAnd(8|32)  ...)
   846  (AtomicOr(8|32)   ...) => (LoweredAtomicOr(8|32)   ...)
   847  
   848  (Slicemask <t> x) => (SRADconst (NEG <t> x) [63])
   849  (ANDconst [1] z:(SRADconst [63] x)) && z.Uses == 1  => (SRDconst [63] x)
   850  
   851  // Note that MOV??reg returns a 64-bit int, x is not necessarily that wide
   852  // This may interact with other patterns in the future. (Compare with arm64)
   853  (MOV(B|H|W)Zreg x:(MOVBZload _ _)) => x
   854  (MOV(B|H|W)Zreg x:(MOVBZloadidx _ _ _)) => x
   855  (MOV(H|W)Zreg x:(MOVHZload _ _)) => x
   856  (MOV(H|W)Zreg x:(MOVHZloadidx _ _ _)) => x
   857  (MOV(H|W)reg x:(MOVHload _ _)) => x
   858  (MOV(H|W)reg x:(MOVHloadidx _ _ _)) => x
   859  (MOV(WZ|W)reg x:(MOV(WZ|W)load _ _)) => x
   860  (MOV(WZ|W)reg x:(MOV(WZ|W)loadidx _ _ _)) => x
   861  (MOV(B|W)Zreg x:(Select0 (LoweredAtomicLoad(8|32) _ _))) => x
   862  
   863  // don't extend if argument is already extended
   864  (MOVBreg x:(Arg <t>)) && ssa.Is8BitInt(t) && t.IsSigned() => x
   865  (MOVBZreg x:(Arg <t>)) && ssa.Is8BitInt(t) && !t.IsSigned() => x
   866  (MOVHreg x:(Arg <t>)) && (ssa.Is8BitInt(t) || ssa.Is16BitInt(t)) && t.IsSigned() => x
   867  (MOVHZreg x:(Arg <t>)) && (ssa.Is8BitInt(t) || ssa.Is16BitInt(t)) && !t.IsSigned() => x
   868  (MOVWreg x:(Arg <t>)) && (ssa.Is8BitInt(t) || ssa.Is16BitInt(t) || ssa.Is32BitInt(t)) && t.IsSigned() => x
   869  (MOVWZreg x:(Arg <t>)) && (ssa.Is8BitInt(t) || ssa.Is16BitInt(t) || ssa.Is32BitInt(t)) && !t.IsSigned() => x
   870  
   871  (MOVBZreg (MOVDconst [c]))  => (MOVDconst [int64(uint8(c))])
   872  (MOVBreg (MOVDconst [c]))  => (MOVDconst [int64(int8(c))])
   873  (MOVHZreg (MOVDconst [c]))  => (MOVDconst [int64(uint16(c))])
   874  (MOVHreg (MOVDconst [c]))  => (MOVDconst [int64(int16(c))])
   875  (MOVWreg (MOVDconst [c])) => (MOVDconst [int64(int32(c))])
   876  (MOVWZreg (MOVDconst [c])) => (MOVDconst [int64(uint32(c))])
   877  
   878  // Implement clrsldi and clrslwi extended mnemonics as described in
   879  // ISA 3.0 section C.8. AuxInt field contains values needed for
   880  // the instructions, packed together since there is only one available.
   881  (SLDconst [c] z:(MOVBZreg x)) && c < 8 && z.Uses == 1 => (CLRLSLDI [ssa.NewPPC64ShiftAuxInt(c,56,63,64)] x)
   882  (SLDconst [c] z:(MOVHZreg x)) && c < 16 && z.Uses == 1 => (CLRLSLDI [ssa.NewPPC64ShiftAuxInt(c,48,63,64)] x)
   883  (SLDconst [c] z:(MOVWZreg x)) && c < 32 && z.Uses == 1 => (CLRLSLDI [ssa.NewPPC64ShiftAuxInt(c,32,63,64)] x)
   884  
   885  (SLDconst [c] z:(ANDconst [d] x)) && z.Uses == 1 && ssa.IsPPC64ValidShiftMask(d) && c <= (64-getPPC64ShiftMaskLength(d)) => (CLRLSLDI [ssa.NewPPC64ShiftAuxInt(c,64-getPPC64ShiftMaskLength(d),63,64)] x)
   886  (SLDconst [c] z:(AND (MOVDconst [d]) x)) && z.Uses == 1 && ssa.IsPPC64ValidShiftMask(d) && c<=(64-getPPC64ShiftMaskLength(d)) => (CLRLSLDI [ssa.NewPPC64ShiftAuxInt(c,64-getPPC64ShiftMaskLength(d),63,64)] x)
   887  // (x + x) << c  →  x << (c+1)
   888  (SLDconst [c] (ADD x x)) && c < 63 => (SLDconst [c+1] x)
   889  (SLWconst [c] z:(MOVBZreg x)) && z.Uses == 1 && c < 8 => (CLRLSLWI [ssa.NewPPC64ShiftAuxInt(c,24,31,32)] x)
   890  (SLWconst [c] z:(MOVHZreg x)) && z.Uses == 1 && c < 16 => (CLRLSLWI [ssa.NewPPC64ShiftAuxInt(c,16,31,32)] x)
   891  (SLWconst [c] z:(ANDconst [d] x)) && z.Uses == 1 && ssa.IsPPC64ValidShiftMask(d) && c<=(32-getPPC64ShiftMaskLength(d)) => (CLRLSLWI [ssa.NewPPC64ShiftAuxInt(c,32-getPPC64ShiftMaskLength(d),31,32)] x)
   892  (SLWconst [c] z:(AND (MOVDconst [d]) x)) && z.Uses == 1 && ssa.IsPPC64ValidShiftMask(d) && c<=(32-getPPC64ShiftMaskLength(d)) => (CLRLSLWI [ssa.NewPPC64ShiftAuxInt(c,32-getPPC64ShiftMaskLength(d),31,32)] x)
   893  // (x + x) << c  →  x << (c+1)
   894  (SLWconst [c] (ADD x x)) && c < 31 => (SLWconst [c+1] x)
   895  // special case for power9
   896  (SL(W|D)const [c] z:(MOVWreg x)) && c < 32 && buildcfg.GOPPC64 >= 9 => (EXTSWSLconst [c] x)
   897  
   898  // Lose widening ops fed to stores
   899  (MOVBstore [off] {sym} ptr (MOV(B|BZ|H|HZ|W|WZ)reg x) mem) => (MOVBstore [off] {sym} ptr x mem)
   900  (MOVHstore [off] {sym} ptr (MOV(H|HZ|W|WZ)reg x) mem) => (MOVHstore [off] {sym} ptr x mem)
   901  (MOVWstore [off] {sym} ptr (MOV(W|WZ)reg x) mem) => (MOVWstore [off] {sym} ptr x mem)
   902  (MOVBstore [off] {sym} ptr (SRWconst (MOV(H|HZ)reg x) [c]) mem) && c <= 8 => (MOVBstore [off] {sym} ptr (SRWconst <typ.UInt32> x [c]) mem)
   903  (MOVBstore [off] {sym} ptr (SRWconst (MOV(W|WZ)reg x) [c]) mem) && c <= 24 => (MOVBstore [off] {sym} ptr (SRWconst <typ.UInt32> x [c]) mem)
   904  (MOVBstoreidx ptr idx (MOV(B|BZ|H|HZ|W|WZ)reg x) mem) => (MOVBstoreidx ptr idx x mem)
   905  (MOVHstoreidx ptr idx (MOV(H|HZ|W|WZ)reg x) mem) => (MOVHstoreidx ptr idx x mem)
   906  (MOVWstoreidx ptr idx (MOV(W|WZ)reg x) mem) => (MOVWstoreidx ptr idx x mem)
   907  (MOVBstoreidx ptr idx (SRWconst (MOV(H|HZ)reg x) [c]) mem) && c <= 8 => (MOVBstoreidx ptr idx (SRWconst <typ.UInt32> x [c]) mem)
   908  (MOVBstoreidx ptr idx (SRWconst (MOV(W|WZ)reg x) [c]) mem) && c <= 24 => (MOVBstoreidx ptr idx (SRWconst <typ.UInt32> x [c]) mem)
   909  (MOVHBRstore ptr (MOV(H|HZ|W|WZ)reg x) mem) => (MOVHBRstore ptr x mem)
   910  (MOVWBRstore ptr (MOV(W|WZ)reg x) mem) => (MOVWBRstore ptr x mem)
   911  
   912  // Lose W-widening ops fed to compare-W
   913  (CMP(W|WU) x (MOV(W|WZ)reg y)) => (CMP(W|WU) x y)
   914  (CMP(W|WU) (MOV(W|WZ)reg x) y) => (CMP(W|WU) x y)
   915  
   916  (CMP x (MOVDconst [c])) && ssa.Is16Bit(c) => (CMPconst x [c])
   917  (CMP (MOVDconst [c]) y) && ssa.Is16Bit(c) => (InvertFlags (CMPconst y [c]))
   918  (CMPW x (MOVDconst [c])) && ssa.Is16Bit(c) => (CMPWconst x [int32(c)])
   919  (CMPW (MOVDconst [c]) y) && ssa.Is16Bit(c) => (InvertFlags (CMPWconst y [int32(c)]))
   920  
   921  (CMPU x (MOVDconst [c])) && isU16Bit(c) => (CMPUconst x [c])
   922  (CMPU (MOVDconst [c]) y) && isU16Bit(c) => (InvertFlags (CMPUconst y [c]))
   923  (CMPWU x (MOVDconst [c])) && isU16Bit(c) => (CMPWUconst x [int32(c)])
   924  (CMPWU (MOVDconst [c]) y) && isU16Bit(c) => (InvertFlags (CMPWUconst y [int32(c)]))
   925  
   926  // Canonicalize the order of arguments to comparisons - helps with CSE.
   927  ((CMP|CMPW|CMPU|CMPWU) x y) && ssa.CanonLessThan(x,y) => (InvertFlags ((CMP|CMPW|CMPU|CMPWU) y x))
   928  
   929  // n is always a zero-extended uint16 value, so n & z is always a non-negative 32 or 64 bit value.
   930  // Rewrite to a cmp int64(0) to lower into ANDCCconst in the latelower pass.
   931  (CMP(W|U|WU)const [0] a:(ANDconst [n] z)) => (CMPconst [0] a)
   932  
   933  // SETBC auxInt values 0=LT 1=GT 2=EQ   Crbit==1 ? 1 : 0
   934  // SETBCR auxInt values 0=LT 1=GT 2=EQ   Crbit==1 ? 0 : 1
   935  (Equal cmp) => (SETBC [2] cmp)
   936  (NotEqual cmp) => (SETBCR [2] cmp)
   937  (LessThan cmp) => (SETBC [0] cmp)
   938  (FLessThan cmp) => (SETBC [0] cmp)
   939  (FLessEqual cmp) => (OR (SETBC [2] cmp) (SETBC [0] cmp))
   940  (GreaterEqual cmp) => (SETBCR [0] cmp)
   941  (GreaterThan cmp)  => (SETBC [1] cmp)
   942  (FGreaterEqual cmp) => (OR (SETBC [2] cmp) (SETBC [1] cmp))
   943  (FGreaterThan cmp)  => (SETBC [1] cmp)
   944  (LessEqual cmp) => (SETBCR [1] cmp)
   945  
   946  (SETBC [0] (FlagLT)) => (MOVDconst [1])
   947  (SETBC [0] (Flag(GT|EQ))) => (MOVDconst [0])
   948  (SETBC [1] (FlagGT)) => (MOVDconst [1])
   949  (SETBC [1] (Flag(LT|EQ))) => (MOVDconst [0])
   950  (SETBC [2] (FlagEQ)) => (MOVDconst [1])
   951  (SETBC [2] (Flag(LT|GT))) => (MOVDconst [0])
   952  
   953  (SETBCR [0] (FlagLT)) => (MOVDconst [0])
   954  (SETBCR [0] (Flag(GT|EQ))) => (MOVDconst [1])
   955  (SETBCR [1] (FlagGT)) => (MOVDconst [0])
   956  (SETBCR [1] (Flag(LT|EQ))) => (MOVDconst [1])
   957  (SETBCR [2] (FlagEQ)) => (MOVDconst [0])
   958  (SETBCR [2] (Flag(LT|GT))) => (MOVDconst [1])
   959  
   960  (SETBC [0] (InvertFlags bool)) => (SETBC [1] bool)
   961  (SETBC [1] (InvertFlags bool)) => (SETBC [0] bool)
   962  (SETBC [2] (InvertFlags bool)) => (SETBC [2] bool)
   963  
   964  (SETBCR [0] (InvertFlags bool)) => (SETBCR [1] bool)
   965  (SETBCR [1] (InvertFlags bool)) => (SETBCR [0] bool)
   966  (SETBCR [2] (InvertFlags bool)) => (SETBCR [2] bool)
   967  
   968  // ISEL auxInt values 0=LT 1=GT 2=EQ   arg2 ? arg0 : arg1
   969  // ISEL auxInt values 4=GE 5=LE 6=NE   !arg2 ? arg1 : arg0
   970  
   971  (ISEL [2] x _ (FlagEQ)) => x
   972  (ISEL [2] _ y (Flag(LT|GT))) => y
   973  
   974  (ISEL [6] _ y (FlagEQ)) => y
   975  (ISEL [6] x _ (Flag(LT|GT))) => x
   976  
   977  (ISEL [0] _ y (Flag(EQ|GT))) => y
   978  (ISEL [0] x _ (FlagLT)) => x
   979  
   980  (ISEL [5] _ x (Flag(EQ|LT))) => x
   981  (ISEL [5] y _ (FlagGT)) => y
   982  
   983  (ISEL [1] _ y (Flag(EQ|LT))) => y
   984  (ISEL [1] x _ (FlagGT)) => x
   985  
   986  (ISEL [4] x _ (Flag(EQ|GT))) => x
   987  (ISEL [4] _ y (FlagLT)) => y
   988  
   989  (SETBC [n] (InvertFlags bool)) => (SETBCR [n] bool)
   990  (SETBCR [n] (InvertFlags bool)) => (SETBC [n] bool)
   991  
   992  (ISEL [n] x y (InvertFlags bool)) && n%4 == 0 => (ISEL [n+1] x y bool)
   993  (ISEL [n] x y (InvertFlags bool)) && n%4 == 1 => (ISEL [n-1] x y bool)
   994  (ISEL [n] x y (InvertFlags bool)) && n%4 == 2 => (ISEL [n] x y bool)
   995  (XORconst [1] (SETBCR [n] cmp)) => (SETBC [n] cmp)
   996  (XORconst [1] (SETBC [n] cmp)) => (SETBCR [n] cmp)
   997  
   998  (SETBC  [2] (CMPconst [0] a:(ANDconst [1] _))) => (XORconst [1] a)
   999  (SETBCR [2] (CMPconst [0] a:(ANDconst [1] _))) => a
  1000  
  1001  // Only CMPconst for these in case AND|OR|XOR result is > 32 bits
  1002  (SETBC [2] (CMPconst [0] a:(AND y z))) && a.Uses == 1 => (SETBC [2] (Select1 <types.TypeFlags> (ANDCC y z )))
  1003  (SETBCR [2] (CMPconst [0] a:(AND y z))) && a.Uses == 1 => (SETBCR [2] (Select1 <types.TypeFlags> (ANDCC y z )))
  1004  
  1005  (SETBC [2] (CMPconst [0] o:(OR y z))) && o.Uses == 1 => (SETBC [2] (Select1 <types.TypeFlags> (ORCC y z )))
  1006  (SETBCR [2] (CMPconst [0] o:(OR y z))) && o.Uses == 1 => (SETBCR [2] (Select1 <types.TypeFlags> (ORCC y z )))
  1007  
  1008  (SETBC [2] (CMPconst [0] a:(XOR y z))) && a.Uses == 1 => (SETBC [2] (Select1 <types.TypeFlags> (XORCC y z )))
  1009  (SETBCR [2] (CMPconst [0] a:(XOR y z))) && a.Uses == 1 => (SETBCR [2] (Select1 <types.TypeFlags> (XORCC y z )))
  1010  
  1011  // A particular pattern seen in cgo code:
  1012  (AND (MOVDconst [c]) x:(MOVBZload _ _)) => (ANDconst [c&0xFF] x)
  1013  
  1014  // floating point negative abs
  1015  (FNEG (F(ABS|NABS) x)) => (F(NABS|ABS) x)
  1016  
  1017  // floating-point fused multiply-add/sub
  1018  (F(ADD|SUB) (FMUL x y) z) && x.Block.Func.UseFMA(v) => (FM(ADD|SUB) x y z)
  1019  (F(ADDS|SUBS) (FMULS x y) z) && x.Block.Func.UseFMA(v) => (FM(ADDS|SUBS) x y z)
  1020  
  1021  // Arch-specific inlining for small or disjoint runtime.memmove
  1022  (SelectN [0] call:(CALLstatic {sym} s1:(MOVDstore _ (MOVDconst [sz]) s2:(MOVDstore  _ src s3:(MOVDstore {t} _ dst mem)))))
  1023          && sz >= 0
  1024          && ssa.IsSameCall(sym, "runtime.memmove")
  1025          && s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1
  1026          && ssa.IsInlinableMemmove(dst, src, sz, config)
  1027          && ssa.Clobber(s1, s2, s3, call)
  1028          => (Move [sz] dst src mem)
  1029  
  1030  // Match post-lowering calls, register version.
  1031  (SelectN [0] call:(CALLstatic {sym} dst src (MOVDconst [sz]) mem))
  1032          && sz >= 0
  1033          && ssa.IsSameCall(sym, "runtime.memmove")
  1034          && call.Uses == 1
  1035          && ssa.IsInlinableMemmove(dst, src, sz, config)
  1036          && ssa.Clobber(call)
  1037          => (Move [sz] dst src mem)
  1038  
  1039  // Prefetch instructions (TH specified using aux field)
  1040  // For DCBT Ra,Rb,TH, A value of TH indicates:
  1041  //     0, hint this cache line will be used soon. (PrefetchCache)
  1042  //     16, hint this cache line will not be used for long. (PrefetchCacheStreamed)
  1043  // See ISA 3.0 Book II 4.3.2 for more detail. https://openpower.foundation/specifications/isa/
  1044  (PrefetchCache ptr mem)          => (DCBT ptr mem [0])
  1045  (PrefetchCacheStreamed ptr mem)  => (DCBT ptr mem [16])
  1046  
  1047  // Use byte reverse instructions on Power10
  1048  (Bswap(16|32|64) x) && buildcfg.GOPPC64>=10 => (BR(H|W|D) x)
  1049  
  1050  // Fold bit reversal into loads.
  1051  (BR(W|H) x:(MOV(W|H)Zload [off] {sym} ptr mem)) && x.Uses == 1 => @x.Block (MOV(W|H)BRload (MOVDaddr <ptr.Type> [off] {sym} ptr) mem)
  1052  (BR(W|H) x:(MOV(W|H)Zloadidx ptr idx      mem)) && x.Uses == 1 => @x.Block (MOV(W|H)BRloadidx ptr idx mem)
  1053  (BRD x:(MOVDload [off] {sym} ptr mem)) && x.Uses == 1 => @x.Block (MOVDBRload (MOVDaddr <ptr.Type> [off] {sym} ptr) mem)
  1054  (BRD x:(MOVDloadidx ptr idx      mem)) && x.Uses == 1 => @x.Block (MOVDBRloadidx ptr idx mem)
  1055  
  1056  // Fold bit reversal into stores.
  1057  (MOV(D|W|H)store [off] {sym} ptr r:(BR(D|W|H) val) mem) && r.Uses == 1 => (MOV(D|W|H)BRstore (MOVDaddr <ptr.Type> [off] {sym} ptr) val mem)
  1058  (MOV(D|W|H)storeidx ptr idx      r:(BR(D|W|H) val) mem) && r.Uses == 1 => (MOV(D|W|H)BRstoreidx ptr idx val mem)
  1059  
  1060  // GOPPC64<10 rules.
  1061  // These Bswap operations should only be introduced by the memcombine pass in places where they can be folded into loads or stores.
  1062  (Bswap(32|16) x:(MOV(W|H)Zload [off] {sym} ptr mem)) => @x.Block (MOV(W|H)BRload (MOVDaddr <ptr.Type> [off] {sym} ptr) mem)
  1063  (Bswap(32|16) x:(MOV(W|H)Zloadidx ptr idx      mem)) => @x.Block (MOV(W|H)BRloadidx ptr idx mem)
  1064  (Bswap64 x:(MOVDload [off] {sym} ptr mem)) => @x.Block (MOVDBRload (MOVDaddr <ptr.Type> [off] {sym} ptr) mem)
  1065  (Bswap64 x:(MOVDloadidx ptr idx      mem)) => @x.Block (MOVDBRloadidx ptr idx mem)
  1066  (MOV(D|W|H)store [off] {sym} ptr (Bswap(64|32|16) val) mem) => (MOV(D|W|H)BRstore (MOVDaddr <ptr.Type> [off] {sym} ptr) val mem)
  1067  (MOV(D|W|H)storeidx ptr idx      (Bswap(64|32|16) val) mem) => (MOV(D|W|H)BRstoreidx ptr idx val mem)
  1068  

View as plain text