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

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Simplifications that apply to all backend architectures. As an example, this
     6  // Go source code
     7  //
     8  // y := 0 * x
     9  //
    10  // can be translated into y := 0 without losing any information, which saves a
    11  // pointless multiplication instruction. Other .rules files in this directory
    12  // (for example AMD64.rules) contain rules specific to the architecture in the
    13  // filename. The rules here apply to every architecture.
    14  //
    15  // The code for parsing this file lives in rulegen.go; this file generates
    16  // ssa/rewritegeneric.go.
    17  
    18  // values are specified using the following format:
    19  // (op <type> [auxint] {aux} arg0 arg1 ...)
    20  // the type, aux, and auxint fields are optional
    21  // on the matching side
    22  //  - the type, aux, and auxint fields must match if they are specified.
    23  //  - the first occurrence of a variable defines that variable.  Subsequent
    24  //    uses must match (be == to) the first use.
    25  //  - v is defined to be the value matched.
    26  //  - an additional conditional can be provided after the match pattern with "&&".
    27  // on the generated side
    28  //  - the type of the top-level expression is the same as the one on the left-hand side.
    29  //  - the type of any subexpressions must be specified explicitly (or
    30  //    be specified in the op's type field).
    31  //  - auxint will be 0 if not specified.
    32  //  - aux will be nil if not specified.
    33  
    34  // blocks are specified using the following format:
    35  // (kind controlvalue succ0 succ1 ...)
    36  // controlvalue must be "nil" or a value expression
    37  // succ* fields must be variables
    38  // For now, the generated successors must be a permutation of the matched successors.
    39  
    40  // constant folding
    41  (Trunc16to8  (Const16  [c])) => (Const8   [int8(c)])
    42  (Trunc32to8  (Const32  [c])) => (Const8   [int8(c)])
    43  (Trunc32to16 (Const32  [c])) => (Const16  [int16(c)])
    44  (Trunc64to8  (Const64  [c])) => (Const8   [int8(c)])
    45  (Trunc64to16 (Const64  [c])) => (Const16  [int16(c)])
    46  (Trunc64to32 (Const64  [c])) => (Const32  [int32(c)])
    47  (Cvt64Fto32F (Const64F [c])) => (Const32F [float32(c)])
    48  (Cvt32Fto64F (Const32F [c])) => (Const64F [float64(c)])
    49  (Cvt32to32F  (Const32  [c])) => (Const32F [float32(c)])
    50  (Cvt32to64F  (Const32  [c])) => (Const64F [float64(c)])
    51  (Cvt64to32F  (Const64  [c])) => (Const32F [float32(c)])
    52  (Cvt64to64F  (Const64  [c])) => (Const64F [float64(c)])
    53  (Cvt32Fto32  (Const32F [c])) => (Const32  [int32(c)])
    54  (Cvt32Fto64  (Const32F [c])) => (Const64  [int64(c)])
    55  (Cvt64Fto32  (Const64F [c])) => (Const32  [int32(c)])
    56  (Cvt64Fto64  (Const64F [c])) => (Const64  [int64(c)])
    57  (Round32F x:(Const32F)) => x
    58  (Round64F x:(Const64F)) => x
    59  (CvtBoolToUint8 (ConstBool [false])) => (Const8 [0])
    60  (CvtBoolToUint8 (ConstBool [true])) => (Const8 [1])
    61  (BitLen64 (Const64 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.Len64(uint64(c)))])
    62  (BitLen32 (Const32 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.Len32(uint32(c)))])
    63  (BitLen16 (Const16 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.Len16(uint16(c)))])
    64  (BitLen8  (Const8  [c])) && config.PtrSize == 8 => (Const64 [int64(bits.Len8(uint8(c)))])
    65  (BitLen64 (Const64 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.Len64(uint64(c)))])
    66  (BitLen32 (Const32 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.Len32(uint32(c)))])
    67  (BitLen16 (Const16 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.Len16(uint16(c)))])
    68  (BitLen8  (Const8  [c])) && config.PtrSize == 4 => (Const32 [int32(bits.Len8(uint8(c)))])
    69  (PopCount64 (Const64 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.OnesCount64(uint64(c)))])
    70  (PopCount32 (Const32 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.OnesCount32(uint32(c)))])
    71  (PopCount16 (Const16 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.OnesCount16(uint16(c)))])
    72  (PopCount8  (Const8  [c])) && config.PtrSize == 8 => (Const64 [int64(bits.OnesCount8(uint8(c)))])
    73  (PopCount64 (Const64 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount64(uint64(c)))])
    74  (PopCount32 (Const32 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount32(uint32(c)))])
    75  (PopCount16 (Const16 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount16(uint16(c)))])
    76  (PopCount8  (Const8  [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount8(uint8(c)))])
    77  (Add64carry (Const64 <t> [x]) (Const64 [y]) (Const64 [c])) && c >= 0 && c <= 1 => (MakeTuple (Const64 <t> [bitsAdd64(x, y, c).sum]) (Const64 <t> [bitsAdd64(x, y, c).carry]))
    78  
    79  (Trunc16to8  (ZeroExt8to16  x)) => x
    80  (Trunc32to8  (ZeroExt8to32  x)) => x
    81  (Trunc32to16 (ZeroExt8to32  x)) => (ZeroExt8to16  x)
    82  (Trunc32to16 (ZeroExt16to32 x)) => x
    83  (Trunc64to8  (ZeroExt8to64  x)) => x
    84  (Trunc64to16 (ZeroExt8to64  x)) => (ZeroExt8to16  x)
    85  (Trunc64to16 (ZeroExt16to64 x)) => x
    86  (Trunc64to32 (ZeroExt8to64  x)) => (ZeroExt8to32  x)
    87  (Trunc64to32 (ZeroExt16to64 x)) => (ZeroExt16to32 x)
    88  (Trunc64to32 (ZeroExt32to64 x)) => x
    89  (Trunc16to8  (SignExt8to16  x)) => x
    90  (Trunc32to8  (SignExt8to32  x)) => x
    91  (Trunc32to16 (SignExt8to32  x)) => (SignExt8to16  x)
    92  (Trunc32to16 (SignExt16to32 x)) => x
    93  (Trunc64to8  (SignExt8to64  x)) => x
    94  (Trunc64to16 (SignExt8to64  x)) => (SignExt8to16  x)
    95  (Trunc64to16 (SignExt16to64 x)) => x
    96  (Trunc64to32 (SignExt8to64  x)) => (SignExt8to32  x)
    97  (Trunc64to32 (SignExt16to64 x)) => (SignExt16to32 x)
    98  (Trunc64to32 (SignExt32to64 x)) => x
    99  
   100  (ZeroExt8to16  (Const8  [c])) => (Const16 [int16( uint8(c))])
   101  (ZeroExt8to32  (Const8  [c])) => (Const32 [int32( uint8(c))])
   102  (ZeroExt8to64  (Const8  [c])) => (Const64 [int64( uint8(c))])
   103  (ZeroExt16to32 (Const16 [c])) => (Const32 [int32(uint16(c))])
   104  (ZeroExt16to64 (Const16 [c])) => (Const64 [int64(uint16(c))])
   105  (ZeroExt32to64 (Const32 [c])) => (Const64 [int64(uint32(c))])
   106  (SignExt8to16  (Const8  [c])) => (Const16 [int16(c)])
   107  (SignExt8to32  (Const8  [c])) => (Const32 [int32(c)])
   108  (SignExt8to64  (Const8  [c])) => (Const64 [int64(c)])
   109  (SignExt16to32 (Const16 [c])) => (Const32 [int32(c)])
   110  (SignExt16to64 (Const16 [c])) => (Const64 [int64(c)])
   111  (SignExt32to64 (Const32 [c])) => (Const64 [int64(c)])
   112  
   113  (Neg8   (Const8   [c])) => (Const8   [-c])
   114  (Neg16  (Const16  [c])) => (Const16  [-c])
   115  (Neg32  (Const32  [c])) => (Const32  [-c])
   116  (Neg64  (Const64  [c])) => (Const64  [-c])
   117  (Neg32F (Const32F [c])) && c != 0 => (Const32F [-c])
   118  (Neg64F (Const64F [c])) && c != 0 => (Const64F [-c])
   119  
   120  (Add8   (Const8 [c])   (Const8 [d]))   => (Const8  [c+d])
   121  (Add16  (Const16 [c])  (Const16 [d]))  => (Const16 [c+d])
   122  (Add32  (Const32 [c])  (Const32 [d]))  => (Const32 [c+d])
   123  (Add64  (Const64 [c])  (Const64 [d]))  => (Const64 [c+d])
   124  (Add32F (Const32F [c]) (Const32F [d])) && c+d == c+d => (Const32F [c+d])
   125  (Add64F (Const64F [c]) (Const64F [d])) && c+d == c+d => (Const64F [c+d])
   126  (AddPtr <t> x (Const64 [c])) => (OffPtr <t> x [c])
   127  (AddPtr <t> x (Const32 [c])) => (OffPtr <t> x [int64(c)])
   128  
   129  (Sub8   (Const8 [c]) (Const8 [d]))     => (Const8 [c-d])
   130  (Sub16  (Const16 [c]) (Const16 [d]))   => (Const16 [c-d])
   131  (Sub32  (Const32 [c]) (Const32 [d]))   => (Const32 [c-d])
   132  (Sub64  (Const64 [c]) (Const64 [d]))   => (Const64 [c-d])
   133  (Sub32F (Const32F [c]) (Const32F [d])) && c-d == c-d => (Const32F [c-d])
   134  (Sub64F (Const64F [c]) (Const64F [d])) && c-d == c-d => (Const64F [c-d])
   135  
   136  (Mul8   (Const8 [c])   (Const8 [d]))   => (Const8  [c*d])
   137  (Mul16  (Const16 [c])  (Const16 [d]))  => (Const16 [c*d])
   138  (Mul32  (Const32 [c])  (Const32 [d]))  => (Const32 [c*d])
   139  (Mul64  (Const64 [c])  (Const64 [d]))  => (Const64 [c*d])
   140  (Mul32F (Const32F [c]) (Const32F [d])) && c*d == c*d => (Const32F [c*d])
   141  (Mul64F (Const64F [c]) (Const64F [d])) && c*d == c*d => (Const64F [c*d])
   142  (Mul32uhilo (Const32 [c]) (Const32 [d])) => (MakeTuple (Const32 <typ.UInt32> [bitsMulU32(c, d).hi]) (Const32 <typ.UInt32> [bitsMulU32(c,d).lo]))
   143  (Mul64uhilo (Const64 [c]) (Const64 [d])) => (MakeTuple (Const64 <typ.UInt64> [bitsMulU64(c, d).hi]) (Const64 <typ.UInt64> [bitsMulU64(c,d).lo]))
   144  (Mul32uover (Const32 [c]) (Const32 [d])) => (MakeTuple (Const32 <typ.UInt32> [bitsMulU32(c, d).lo]) (ConstBool <typ.Bool> [bitsMulU32(c,d).hi != 0]))
   145  (Mul64uover (Const64 [c]) (Const64 [d])) => (MakeTuple (Const64 <typ.UInt64> [bitsMulU64(c, d).lo]) (ConstBool <typ.Bool> [bitsMulU64(c,d).hi != 0]))
   146  
   147  (And8   (Const8 [c])   (Const8 [d]))   => (Const8  [c&d])
   148  (And16  (Const16 [c])  (Const16 [d]))  => (Const16 [c&d])
   149  (And32  (Const32 [c])  (Const32 [d]))  => (Const32 [c&d])
   150  (And64  (Const64 [c])  (Const64 [d]))  => (Const64 [c&d])
   151  
   152  (Or8   (Const8 [c])   (Const8 [d]))   => (Const8  [c|d])
   153  (Or16  (Const16 [c])  (Const16 [d]))  => (Const16 [c|d])
   154  (Or32  (Const32 [c])  (Const32 [d]))  => (Const32 [c|d])
   155  (Or64  (Const64 [c])  (Const64 [d]))  => (Const64 [c|d])
   156  
   157  (Xor8   (Const8 [c])   (Const8 [d]))   => (Const8  [c^d])
   158  (Xor16  (Const16 [c])  (Const16 [d]))  => (Const16 [c^d])
   159  (Xor32  (Const32 [c])  (Const32 [d]))  => (Const32 [c^d])
   160  (Xor64  (Const64 [c])  (Const64 [d]))  => (Const64 [c^d])
   161  
   162  (Ctz64 (Const64 [c])) && config.PtrSize == 4 => (Const32 [int32(ntz64(c))])
   163  (Ctz32 (Const32 [c])) && config.PtrSize == 4 => (Const32 [int32(ntz32(c))])
   164  (Ctz16 (Const16 [c])) && config.PtrSize == 4 => (Const32 [int32(ntz16(c))])
   165  (Ctz8  (Const8  [c])) && config.PtrSize == 4 => (Const32 [int32(ntz8(c))])
   166  
   167  (Ctz64 (Const64 [c])) && config.PtrSize == 8 => (Const64 [int64(ntz64(c))])
   168  (Ctz32 (Const32 [c])) && config.PtrSize == 8 => (Const64 [int64(ntz32(c))])
   169  (Ctz16 (Const16 [c])) && config.PtrSize == 8 => (Const64 [int64(ntz16(c))])
   170  (Ctz8  (Const8  [c])) && config.PtrSize == 8 => (Const64 [int64(ntz8(c))])
   171  
   172  (Div8   (Const8  [c])  (Const8  [d])) && d != 0 => (Const8  [c/d])
   173  (Div16  (Const16 [c])  (Const16 [d])) && d != 0 => (Const16 [c/d])
   174  (Div32  (Const32 [c])  (Const32 [d])) && d != 0 => (Const32 [c/d])
   175  (Div64  (Const64 [c])  (Const64 [d])) && d != 0 => (Const64 [c/d])
   176  (Div8u  (Const8  [c])  (Const8  [d])) && d != 0 => (Const8  [int8(uint8(c)/uint8(d))])
   177  (Div16u (Const16 [c])  (Const16 [d])) && d != 0 => (Const16 [int16(uint16(c)/uint16(d))])
   178  (Div32u (Const32 [c])  (Const32 [d])) && d != 0 => (Const32 [int32(uint32(c)/uint32(d))])
   179  (Div64u (Const64 [c])  (Const64 [d])) && d != 0 => (Const64 [int64(uint64(c)/uint64(d))])
   180  (Div32F (Const32F [c]) (Const32F [d])) && c/d == c/d => (Const32F [c/d])
   181  (Div64F (Const64F [c]) (Const64F [d])) && c/d == c/d => (Const64F [c/d])
   182  (Div128u <t> (Const64 [0]) lo y) => (MakeTuple (Div64u <t.FieldType(0)> lo y) (Mod64u <t.FieldType(1)> lo y))
   183  
   184  (Not (ConstBool [c])) => (ConstBool [!c])
   185  
   186  (Floor       (Const64F [c])) => (Const64F [math.Floor(c)])
   187  (Ceil        (Const64F [c])) => (Const64F [math.Ceil(c)])
   188  (Trunc       (Const64F [c])) => (Const64F [math.Trunc(c)])
   189  (RoundToEven (Const64F [c])) => (Const64F [math.RoundToEven(c)])
   190  
   191  // Convert x * 1 to x.
   192  (Mul(8|16|32|64)  (Const(8|16|32|64)  [1]) x) => x
   193  (Mul(32|64)uover <t> (Const(32|64) [1]) x) => (MakeTuple x (ConstBool <t.FieldType(1)> [false]))
   194  
   195  // Convert x * -1 to -x.
   196  (Mul(8|16|32|64)  (Const(8|16|32|64)  [-1]) x) => (Neg(8|16|32|64)  x)
   197  
   198  // DeMorgan's Laws
   199  (And(8|16|32|64) <t> (Com(8|16|32|64) x) (Com(8|16|32|64) y)) => (Com(8|16|32|64) (Or(8|16|32|64) <t> x y))
   200  (Or(8|16|32|64) <t> (Com(8|16|32|64) x) (Com(8|16|32|64) y)) => (Com(8|16|32|64) (And(8|16|32|64) <t> x y))
   201  
   202  // Convert multiplication by a power of two to a shift.
   203  (Mul8  <t> n (Const8  [c])) && isPowerOfTwo(c) => (Lsh8x64  <t> n (Const64 <typ.UInt64> [log8(c)]))
   204  (Mul16 <t> n (Const16 [c])) && isPowerOfTwo(c) => (Lsh16x64 <t> n (Const64 <typ.UInt64> [log16(c)]))
   205  (Mul32 <t> n (Const32 [c])) && isPowerOfTwo(c) => (Lsh32x64 <t> n (Const64 <typ.UInt64> [log32(c)]))
   206  (Mul64 <t> n (Const64 [c])) && isPowerOfTwo(c) => (Lsh64x64 <t> n (Const64 <typ.UInt64> [log64(c)]))
   207  (Mul8  <t> n (Const8  [c])) && t.IsSigned() && isPowerOfTwo(-c) => (Neg8  (Lsh8x64  <t> n (Const64 <typ.UInt64> [log8(-c)])))
   208  (Mul16 <t> n (Const16 [c])) && t.IsSigned() && isPowerOfTwo(-c) => (Neg16 (Lsh16x64 <t> n (Const64 <typ.UInt64> [log16(-c)])))
   209  (Mul32 <t> n (Const32 [c])) && t.IsSigned() && isPowerOfTwo(-c) => (Neg32 (Lsh32x64 <t> n (Const64 <typ.UInt64> [log32(-c)])))
   210  (Mul64 <t> n (Const64 [c])) && t.IsSigned() && isPowerOfTwo(-c) => (Neg64 (Lsh64x64 <t> n (Const64 <typ.UInt64> [log64(-c)])))
   211  
   212  (Mod8  (Const8  [c]) (Const8  [d])) && d != 0 => (Const8  [c % d])
   213  (Mod16 (Const16 [c]) (Const16 [d])) && d != 0 => (Const16 [c % d])
   214  (Mod32 (Const32 [c]) (Const32 [d])) && d != 0 => (Const32 [c % d])
   215  (Mod64 (Const64 [c]) (Const64 [d])) && d != 0 => (Const64 [c % d])
   216  
   217  (Mod8u  (Const8 [c])  (Const8  [d])) && d != 0 => (Const8  [int8(uint8(c) % uint8(d))])
   218  (Mod16u (Const16 [c]) (Const16 [d])) && d != 0 => (Const16 [int16(uint16(c) % uint16(d))])
   219  (Mod32u (Const32 [c]) (Const32 [d])) && d != 0 => (Const32 [int32(uint32(c) % uint32(d))])
   220  (Mod64u (Const64 [c]) (Const64 [d])) && d != 0 => (Const64 [int64(uint64(c) % uint64(d))])
   221  
   222  (Lsh64x64  (Const64 [c]) (Const64 [d])) => (Const64 [c << uint64(d)])
   223  (Rsh64x64  (Const64 [c]) (Const64 [d])) => (Const64 [c >> uint64(d)])
   224  (Rsh64Ux64 (Const64 [c]) (Const64 [d])) => (Const64 [int64(uint64(c) >> uint64(d))])
   225  (Lsh32x64  (Const32 [c]) (Const64 [d])) => (Const32 [c << uint64(d)])
   226  (Rsh32x64  (Const32 [c]) (Const64 [d])) => (Const32 [c >> uint64(d)])
   227  (Rsh32Ux64 (Const32 [c]) (Const64 [d])) => (Const32 [int32(uint32(c) >> uint64(d))])
   228  (Lsh16x64  (Const16 [c]) (Const64 [d])) => (Const16 [c << uint64(d)])
   229  (Rsh16x64  (Const16 [c]) (Const64 [d])) => (Const16 [c >> uint64(d)])
   230  (Rsh16Ux64 (Const16 [c]) (Const64 [d])) => (Const16 [int16(uint16(c) >> uint64(d))])
   231  (Lsh8x64   (Const8  [c]) (Const64 [d])) => (Const8  [c << uint64(d)])
   232  (Rsh8x64   (Const8  [c]) (Const64 [d])) => (Const8  [c >> uint64(d)])
   233  (Rsh8Ux64  (Const8  [c]) (Const64 [d])) => (Const8  [int8(uint8(c) >> uint64(d))])
   234  
   235  // Fold IsInBounds when the range of the index cannot exceed the limit.
   236  (IsInBounds (ZeroExt8to32  _) (Const32 [c])) && (1 << 8)  <= c => (ConstBool [true])
   237  (IsInBounds (ZeroExt8to64  _) (Const64 [c])) && (1 << 8)  <= c => (ConstBool [true])
   238  (IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= c => (ConstBool [true])
   239  (IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c => (ConstBool [true])
   240  (IsInBounds x x) => (ConstBool [false])
   241  (IsInBounds                (And8  (Const8  [c]) _)  (Const8  [d])) && 0 <= c && c < d => (ConstBool [true])
   242  (IsInBounds (ZeroExt8to16  (And8  (Const8  [c]) _)) (Const16 [d])) && 0 <= c && int16(c) < d => (ConstBool [true])
   243  (IsInBounds (ZeroExt8to32  (And8  (Const8  [c]) _)) (Const32 [d])) && 0 <= c && int32(c) < d => (ConstBool [true])
   244  (IsInBounds (ZeroExt8to64  (And8  (Const8  [c]) _)) (Const64 [d])) && 0 <= c && int64(c) < d => (ConstBool [true])
   245  (IsInBounds                (And16 (Const16 [c]) _)  (Const16 [d])) && 0 <= c && c < d => (ConstBool [true])
   246  (IsInBounds (ZeroExt16to32 (And16 (Const16 [c]) _)) (Const32 [d])) && 0 <= c && int32(c) < d => (ConstBool [true])
   247  (IsInBounds (ZeroExt16to64 (And16 (Const16 [c]) _)) (Const64 [d])) && 0 <= c && int64(c) < d => (ConstBool [true])
   248  (IsInBounds                (And32 (Const32 [c]) _)  (Const32 [d])) && 0 <= c && c < d => (ConstBool [true])
   249  (IsInBounds (ZeroExt32to64 (And32 (Const32 [c]) _)) (Const64 [d])) && 0 <= c && int64(c) < d => (ConstBool [true])
   250  (IsInBounds                (And64 (Const64 [c]) _)  (Const64 [d])) && 0 <= c && c < d => (ConstBool [true])
   251  (IsInBounds (Const32 [c]) (Const32 [d])) => (ConstBool [0 <= c && c < d])
   252  (IsInBounds (Const64 [c]) (Const64 [d])) => (ConstBool [0 <= c && c < d])
   253  // (Mod64u x y) is always between 0 (inclusive) and y (exclusive).
   254  (IsInBounds (Mod32u _ y) y) => (ConstBool [true])
   255  (IsInBounds (Mod64u _ y) y) => (ConstBool [true])
   256  // Right shifting an unsigned number limits its value.
   257  (IsInBounds (ZeroExt8to64  (Rsh8Ux64  _ (Const64 [c]))) (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
   258  (IsInBounds (ZeroExt8to32  (Rsh8Ux64  _ (Const64 [c]))) (Const32 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
   259  (IsInBounds (ZeroExt8to16  (Rsh8Ux64  _ (Const64 [c]))) (Const16 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
   260  (IsInBounds                (Rsh8Ux64  _ (Const64 [c]))  (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
   261  (IsInBounds (ZeroExt16to64 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d => (ConstBool [true])
   262  (IsInBounds (ZeroExt16to32 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d => (ConstBool [true])
   263  (IsInBounds                (Rsh16Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d => (ConstBool [true])
   264  (IsInBounds (ZeroExt32to64 (Rsh32Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d => (ConstBool [true])
   265  (IsInBounds                (Rsh32Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d => (ConstBool [true])
   266  (IsInBounds                (Rsh64Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 64 && 1<<uint(64-c)-1 < d => (ConstBool [true])
   267  
   268  (IsSliceInBounds x x) => (ConstBool [true])
   269  (IsSliceInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c <= d => (ConstBool [true])
   270  (IsSliceInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c <= d => (ConstBool [true])
   271  (IsSliceInBounds (Const32 [0]) _) => (ConstBool [true])
   272  (IsSliceInBounds (Const64 [0]) _) => (ConstBool [true])
   273  (IsSliceInBounds (Const32 [c]) (Const32 [d])) => (ConstBool [0 <= c && c <= d])
   274  (IsSliceInBounds (Const64 [c]) (Const64 [d])) => (ConstBool [0 <= c && c <= d])
   275  (IsSliceInBounds (SliceLen x) (SliceCap x)) => (ConstBool [true])
   276  
   277  (Eq(64|32|16|8) x x) => (ConstBool [true])
   278  (EqB (ConstBool [c]) (ConstBool [d])) => (ConstBool [c == d])
   279  (EqB (ConstBool [false]) x) => (Not x)
   280  (EqB (ConstBool [true]) x) => x
   281  
   282  (Neq(64|32|16|8) x x) => (ConstBool [false])
   283  (NeqB (ConstBool [c]) (ConstBool [d])) => (ConstBool [c != d])
   284  (NeqB (ConstBool [false]) x) => x
   285  (NeqB (ConstBool [true]) x) => (Not x)
   286  (NeqB (Not x) (Not y)) => (NeqB x y)
   287  
   288  (Eq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Eq64 (Const64 <t> [c-d]) x)
   289  (Eq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Eq32 (Const32 <t> [c-d]) x)
   290  (Eq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Eq16 (Const16 <t> [c-d]) x)
   291  (Eq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Eq8  (Const8  <t> [c-d]) x)
   292  
   293  (Neq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Neq64 (Const64 <t> [c-d]) x)
   294  (Neq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Neq32 (Const32 <t> [c-d]) x)
   295  (Neq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Neq16 (Const16 <t> [c-d]) x)
   296  (Neq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Neq8  (Const8  <t> [c-d]) x)
   297  
   298  (CondSelect x _ (ConstBool [true ])) => x
   299  (CondSelect _ y (ConstBool [false])) => y
   300  
   301  // signed integer range: ( c <= x && x (<|<=) d ) -> ( unsigned(x-c) (<|<=) unsigned(d-c) )
   302  (AndB (Leq64 (Const64 [c]) x) ((Less|Leq)64 x (Const64 [d]))) && d >= c => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c])) (Const64 <x.Type> [d-c]))
   303  (AndB (Leq32 (Const32 [c]) x) ((Less|Leq)32 x (Const32 [d]))) && d >= c => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c])) (Const32 <x.Type> [d-c]))
   304  (AndB (Leq16 (Const16 [c]) x) ((Less|Leq)16 x (Const16 [d]))) && d >= c => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c])) (Const16 <x.Type> [d-c]))
   305  (AndB (Leq8  (Const8  [c]) x) ((Less|Leq)8  x (Const8  [d]))) && d >= c => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c])) (Const8  <x.Type> [d-c]))
   306  
   307  // signed integer range: ( c < x && x (<|<=) d ) -> ( unsigned(x-(c+1)) (<|<=) unsigned(d-(c+1)) )
   308  (AndB (Less64 (Const64 [c]) x) ((Less|Leq)64 x (Const64 [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c+1])) (Const64 <x.Type> [d-c-1]))
   309  (AndB (Less32 (Const32 [c]) x) ((Less|Leq)32 x (Const32 [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c+1])) (Const32 <x.Type> [d-c-1]))
   310  (AndB (Less16 (Const16 [c]) x) ((Less|Leq)16 x (Const16 [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c+1])) (Const16 <x.Type> [d-c-1]))
   311  (AndB (Less8  (Const8  [c]) x) ((Less|Leq)8  x (Const8  [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c+1])) (Const8  <x.Type> [d-c-1]))
   312  
   313  // unsigned integer range: ( c <= x && x (<|<=) d ) -> ( x-c (<|<=) d-c )
   314  (AndB (Leq64U (Const64 [c]) x) ((Less|Leq)64U x (Const64 [d]))) && uint64(d) >= uint64(c) => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c])) (Const64 <x.Type> [d-c]))
   315  (AndB (Leq32U (Const32 [c]) x) ((Less|Leq)32U x (Const32 [d]))) && uint32(d) >= uint32(c) => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c])) (Const32 <x.Type> [d-c]))
   316  (AndB (Leq16U (Const16 [c]) x) ((Less|Leq)16U x (Const16 [d]))) && uint16(d) >= uint16(c) => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c])) (Const16 <x.Type> [d-c]))
   317  (AndB (Leq8U  (Const8  [c]) x) ((Less|Leq)8U  x (Const8  [d]))) && uint8(d)  >= uint8(c)  => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c])) (Const8  <x.Type> [d-c]))
   318  
   319  // unsigned integer range: ( c < x && x (<|<=) d ) -> ( x-(c+1) (<|<=) d-(c+1) )
   320  (AndB (Less64U (Const64 [c]) x) ((Less|Leq)64U x (Const64 [d]))) && uint64(d) >= uint64(c+1) && uint64(c+1) > uint64(c) => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c+1])) (Const64 <x.Type> [d-c-1]))
   321  (AndB (Less32U (Const32 [c]) x) ((Less|Leq)32U x (Const32 [d]))) && uint32(d) >= uint32(c+1) && uint32(c+1) > uint32(c) => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c+1])) (Const32 <x.Type> [d-c-1]))
   322  (AndB (Less16U (Const16 [c]) x) ((Less|Leq)16U x (Const16 [d]))) && uint16(d) >= uint16(c+1) && uint16(c+1) > uint16(c) => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c+1])) (Const16 <x.Type> [d-c-1]))
   323  (AndB (Less8U  (Const8  [c]) x) ((Less|Leq)8U  x (Const8  [d]))) && uint8(d)  >= uint8(c+1)  && uint8(c+1)  > uint8(c)  => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c+1]))  (Const8  <x.Type> [d-c-1]))
   324  
   325  // signed integer range: ( c (<|<=) x || x < d ) -> ( unsigned(c-d) (<|<=) unsigned(x-d) )
   326  (OrB ((Less|Leq)64 (Const64 [c]) x) (Less64 x (Const64 [d]))) && c >= d => ((Less|Leq)64U (Const64 <x.Type> [c-d]) (Sub64 <x.Type> x (Const64 <x.Type> [d])))
   327  (OrB ((Less|Leq)32 (Const32 [c]) x) (Less32 x (Const32 [d]))) && c >= d => ((Less|Leq)32U (Const32 <x.Type> [c-d]) (Sub32 <x.Type> x (Const32 <x.Type> [d])))
   328  (OrB ((Less|Leq)16 (Const16 [c]) x) (Less16 x (Const16 [d]))) && c >= d => ((Less|Leq)16U (Const16 <x.Type> [c-d]) (Sub16 <x.Type> x (Const16 <x.Type> [d])))
   329  (OrB ((Less|Leq)8  (Const8  [c]) x) (Less8  x (Const8  [d]))) && c >= d => ((Less|Leq)8U  (Const8  <x.Type> [c-d]) (Sub8  <x.Type> x (Const8  <x.Type> [d])))
   330  
   331  // signed integer range: ( c (<|<=) x || x <= d ) -> ( unsigned(c-(d+1)) (<|<=) unsigned(x-(d+1)) )
   332  (OrB ((Less|Leq)64 (Const64 [c]) x) (Leq64 x (Const64 [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)64U (Const64 <x.Type> [c-d-1]) (Sub64 <x.Type> x (Const64 <x.Type> [d+1])))
   333  (OrB ((Less|Leq)32 (Const32 [c]) x) (Leq32 x (Const32 [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)32U (Const32 <x.Type> [c-d-1]) (Sub32 <x.Type> x (Const32 <x.Type> [d+1])))
   334  (OrB ((Less|Leq)16 (Const16 [c]) x) (Leq16 x (Const16 [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)16U (Const16 <x.Type> [c-d-1]) (Sub16 <x.Type> x (Const16 <x.Type> [d+1])))
   335  (OrB ((Less|Leq)8  (Const8  [c]) x) (Leq8  x (Const8  [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)8U  (Const8  <x.Type> [c-d-1]) (Sub8  <x.Type> x (Const8  <x.Type> [d+1])))
   336  
   337  // unsigned integer range: ( c (<|<=) x || x < d ) -> ( c-d (<|<=) x-d )
   338  (OrB ((Less|Leq)64U (Const64 [c]) x) (Less64U x (Const64 [d]))) && uint64(c) >= uint64(d) => ((Less|Leq)64U (Const64 <x.Type> [c-d]) (Sub64 <x.Type> x (Const64 <x.Type> [d])))
   339  (OrB ((Less|Leq)32U (Const32 [c]) x) (Less32U x (Const32 [d]))) && uint32(c) >= uint32(d) => ((Less|Leq)32U (Const32 <x.Type> [c-d]) (Sub32 <x.Type> x (Const32 <x.Type> [d])))
   340  (OrB ((Less|Leq)16U (Const16 [c]) x) (Less16U x (Const16 [d]))) && uint16(c) >= uint16(d) => ((Less|Leq)16U (Const16 <x.Type> [c-d]) (Sub16 <x.Type> x (Const16 <x.Type> [d])))
   341  (OrB ((Less|Leq)8U  (Const8  [c]) x) (Less8U  x (Const8  [d]))) && uint8(c)  >= uint8(d)  => ((Less|Leq)8U  (Const8  <x.Type> [c-d]) (Sub8  <x.Type> x (Const8  <x.Type> [d])))
   342  
   343  // unsigned integer range: ( c (<|<=) x || x <= d ) -> ( c-(d+1) (<|<=) x-(d+1) )
   344  (OrB ((Less|Leq)64U (Const64 [c]) x) (Leq64U x (Const64 [d]))) && uint64(c) >= uint64(d+1) && uint64(d+1) > uint64(d) => ((Less|Leq)64U (Const64 <x.Type> [c-d-1]) (Sub64 <x.Type> x (Const64 <x.Type> [d+1])))
   345  (OrB ((Less|Leq)32U (Const32 [c]) x) (Leq32U x (Const32 [d]))) && uint32(c) >= uint32(d+1) && uint32(d+1) > uint32(d) => ((Less|Leq)32U (Const32 <x.Type> [c-d-1]) (Sub32 <x.Type> x (Const32 <x.Type> [d+1])))
   346  (OrB ((Less|Leq)16U (Const16 [c]) x) (Leq16U x (Const16 [d]))) && uint16(c) >= uint16(d+1) && uint16(d+1) > uint16(d) => ((Less|Leq)16U (Const16 <x.Type> [c-d-1]) (Sub16 <x.Type> x (Const16 <x.Type> [d+1])))
   347  (OrB ((Less|Leq)8U  (Const8  [c]) x) (Leq8U  x (Const8  [d]))) && uint8(c)  >= uint8(d+1)  && uint8(d+1)  > uint8(d)  => ((Less|Leq)8U  (Const8  <x.Type> [c-d-1]) (Sub8  <x.Type> x (Const8  <x.Type> [d+1])))
   348  
   349  // Canonicalize x-const to x+(-const)
   350  (Sub64 x (Const64 <t> [c])) && x.Op != OpConst64 => (Add64 (Const64 <t> [-c]) x)
   351  (Sub32 x (Const32 <t> [c])) && x.Op != OpConst32 => (Add32 (Const32 <t> [-c]) x)
   352  (Sub16 x (Const16 <t> [c])) && x.Op != OpConst16 => (Add16 (Const16 <t> [-c]) x)
   353  (Sub8  x (Const8  <t> [c])) && x.Op != OpConst8  => (Add8  (Const8  <t> [-c]) x)
   354  
   355  // fold negation into comparison operators
   356  (Not (Eq(64|32|16|8|B|Ptr|64F|32F) x y)) => (Neq(64|32|16|8|B|Ptr|64F|32F) x y)
   357  (Not (Neq(64|32|16|8|B|Ptr|64F|32F) x y)) => (Eq(64|32|16|8|B|Ptr|64F|32F) x y)
   358  
   359  (Not (Less(64|32|16|8) x y)) => (Leq(64|32|16|8) y x)
   360  (Not (Less(64|32|16|8)U x y)) => (Leq(64|32|16|8)U y x)
   361  (Not (Leq(64|32|16|8) x y)) => (Less(64|32|16|8) y x)
   362  (Not (Leq(64|32|16|8)U x y)) => (Less(64|32|16|8)U y x)
   363  
   364  // Distribute multiplication c * (d+x) -> c*d + c*x. Useful for:
   365  // a[i].b = ...; a[i+1].b = ...
   366  (Mul64 (Const64 <t> [c]) (Add64 <t> (Const64 <t> [d]) x)) =>
   367    (Add64 (Const64 <t> [c*d]) (Mul64 <t> (Const64 <t> [c]) x))
   368  (Mul32 (Const32 <t> [c]) (Add32 <t> (Const32 <t> [d]) x)) =>
   369    (Add32 (Const32 <t> [c*d]) (Mul32 <t> (Const32 <t> [c]) x))
   370  (Mul16 (Const16 <t> [c]) (Add16 <t> (Const16 <t> [d]) x)) =>
   371    (Add16 (Const16 <t> [c*d]) (Mul16 <t> (Const16 <t> [c]) x))
   372  (Mul8 (Const8 <t> [c]) (Add8 <t> (Const8 <t> [d]) x)) =>
   373    (Add8 (Const8 <t> [c*d]) (Mul8 <t> (Const8 <t> [c]) x))
   374  
   375  // Rewrite x*y ± x*z  to  x*(y±z)
   376  (Add(64|32|16|8) <t> (Mul(64|32|16|8) x y) (Mul(64|32|16|8) x z))
   377  	=> (Mul(64|32|16|8) x (Add(64|32|16|8) <t> y z))
   378  (Sub(64|32|16|8) <t> (Mul(64|32|16|8) x y) (Mul(64|32|16|8) x z))
   379  	=> (Mul(64|32|16|8) x (Sub(64|32|16|8) <t> y z))
   380  
   381  // rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce
   382  // the number of the other rewrite rules for const shifts
   383  (Lsh64x32  <t> x (Const32 [c])) => (Lsh64x64  x (Const64 <t> [int64(uint32(c))]))
   384  (Lsh64x16  <t> x (Const16 [c])) => (Lsh64x64  x (Const64 <t> [int64(uint16(c))]))
   385  (Lsh64x8   <t> x (Const8  [c])) => (Lsh64x64  x (Const64 <t> [int64(uint8(c))]))
   386  (Rsh64x32  <t> x (Const32 [c])) => (Rsh64x64  x (Const64 <t> [int64(uint32(c))]))
   387  (Rsh64x16  <t> x (Const16 [c])) => (Rsh64x64  x (Const64 <t> [int64(uint16(c))]))
   388  (Rsh64x8   <t> x (Const8  [c])) => (Rsh64x64  x (Const64 <t> [int64(uint8(c))]))
   389  (Rsh64Ux32 <t> x (Const32 [c])) => (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))]))
   390  (Rsh64Ux16 <t> x (Const16 [c])) => (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))]))
   391  (Rsh64Ux8  <t> x (Const8  [c])) => (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))]))
   392  
   393  (Lsh32x32  <t> x (Const32 [c])) => (Lsh32x64  x (Const64 <t> [int64(uint32(c))]))
   394  (Lsh32x16  <t> x (Const16 [c])) => (Lsh32x64  x (Const64 <t> [int64(uint16(c))]))
   395  (Lsh32x8   <t> x (Const8  [c])) => (Lsh32x64  x (Const64 <t> [int64(uint8(c))]))
   396  (Rsh32x32  <t> x (Const32 [c])) => (Rsh32x64  x (Const64 <t> [int64(uint32(c))]))
   397  (Rsh32x16  <t> x (Const16 [c])) => (Rsh32x64  x (Const64 <t> [int64(uint16(c))]))
   398  (Rsh32x8   <t> x (Const8  [c])) => (Rsh32x64  x (Const64 <t> [int64(uint8(c))]))
   399  (Rsh32Ux32 <t> x (Const32 [c])) => (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))]))
   400  (Rsh32Ux16 <t> x (Const16 [c])) => (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))]))
   401  (Rsh32Ux8  <t> x (Const8  [c])) => (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))]))
   402  
   403  (Lsh16x32  <t> x (Const32 [c])) => (Lsh16x64  x (Const64 <t> [int64(uint32(c))]))
   404  (Lsh16x16  <t> x (Const16 [c])) => (Lsh16x64  x (Const64 <t> [int64(uint16(c))]))
   405  (Lsh16x8   <t> x (Const8  [c])) => (Lsh16x64  x (Const64 <t> [int64(uint8(c))]))
   406  (Rsh16x32  <t> x (Const32 [c])) => (Rsh16x64  x (Const64 <t> [int64(uint32(c))]))
   407  (Rsh16x16  <t> x (Const16 [c])) => (Rsh16x64  x (Const64 <t> [int64(uint16(c))]))
   408  (Rsh16x8   <t> x (Const8  [c])) => (Rsh16x64  x (Const64 <t> [int64(uint8(c))]))
   409  (Rsh16Ux32 <t> x (Const32 [c])) => (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))]))
   410  (Rsh16Ux16 <t> x (Const16 [c])) => (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))]))
   411  (Rsh16Ux8  <t> x (Const8  [c])) => (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))]))
   412  
   413  (Lsh8x32  <t> x (Const32 [c])) => (Lsh8x64  x (Const64 <t> [int64(uint32(c))]))
   414  (Lsh8x16  <t> x (Const16 [c])) => (Lsh8x64  x (Const64 <t> [int64(uint16(c))]))
   415  (Lsh8x8   <t> x (Const8  [c])) => (Lsh8x64  x (Const64 <t> [int64(uint8(c))]))
   416  (Rsh8x32  <t> x (Const32 [c])) => (Rsh8x64  x (Const64 <t> [int64(uint32(c))]))
   417  (Rsh8x16  <t> x (Const16 [c])) => (Rsh8x64  x (Const64 <t> [int64(uint16(c))]))
   418  (Rsh8x8   <t> x (Const8  [c])) => (Rsh8x64  x (Const64 <t> [int64(uint8(c))]))
   419  (Rsh8Ux32 <t> x (Const32 [c])) => (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))]))
   420  (Rsh8Ux16 <t> x (Const16 [c])) => (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))]))
   421  (Rsh8Ux8  <t> x (Const8  [c])) => (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))]))
   422  
   423  // shifts by zero
   424  (Lsh(64|32|16|8)x64  x (Const64 [0])) => x
   425  (Rsh(64|32|16|8)x64  x (Const64 [0])) => x
   426  (Rsh(64|32|16|8)Ux64 x (Const64 [0])) => x
   427  
   428  // rotates by multiples of register width
   429  (RotateLeft64 x (Const64 [c])) && c%64 == 0 => x
   430  (RotateLeft32 x (Const32 [c])) && c%32 == 0 => x
   431  (RotateLeft16 x (Const16 [c])) && c%16 == 0 => x
   432  (RotateLeft8  x (Const8 [c]))  && c%8  == 0 => x
   433  
   434  // zero shifted
   435  (Lsh64x(64|32|16|8)  (Const64 [0]) _) => (Const64 [0])
   436  (Rsh64x(64|32|16|8)  (Const64 [0]) _) => (Const64 [0])
   437  (Rsh64Ux(64|32|16|8) (Const64 [0]) _) => (Const64 [0])
   438  (Lsh32x(64|32|16|8)  (Const32 [0]) _) => (Const32 [0])
   439  (Rsh32x(64|32|16|8)  (Const32 [0]) _) => (Const32 [0])
   440  (Rsh32Ux(64|32|16|8) (Const32 [0]) _) => (Const32 [0])
   441  (Lsh16x(64|32|16|8)  (Const16 [0]) _) => (Const16 [0])
   442  (Rsh16x(64|32|16|8)  (Const16 [0]) _) => (Const16 [0])
   443  (Rsh16Ux(64|32|16|8) (Const16 [0]) _) => (Const16 [0])
   444  (Lsh8x(64|32|16|8)   (Const8  [0]) _) => (Const8  [0])
   445  (Rsh8x(64|32|16|8)   (Const8  [0]) _) => (Const8  [0])
   446  (Rsh8Ux(64|32|16|8)  (Const8  [0]) _) => (Const8  [0])
   447  
   448  // large left shifts of all values, and right shifts of unsigned values
   449  ((Lsh64|Rsh64U)x64  _ (Const64 [c])) && uint64(c) >= 64 => (Const64 [0])
   450  ((Lsh32|Rsh32U)x64  _ (Const64 [c])) && uint64(c) >= 32 => (Const32 [0])
   451  ((Lsh16|Rsh16U)x64  _ (Const64 [c])) && uint64(c) >= 16 => (Const16 [0])
   452  ((Lsh8|Rsh8U)x64    _ (Const64 [c])) && uint64(c) >= 8  => (Const8  [0])
   453  
   454  // combine const shifts
   455  (Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh64x64 x (Const64 <t> [c+d]))
   456  (Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh32x64 x (Const64 <t> [c+d]))
   457  (Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh16x64 x (Const64 <t> [c+d]))
   458  (Lsh8x64  <t> (Lsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh8x64  x (Const64 <t> [c+d]))
   459  
   460  (Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh64x64 x (Const64 <t> [c+d]))
   461  (Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh32x64 x (Const64 <t> [c+d]))
   462  (Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh16x64 x (Const64 <t> [c+d]))
   463  (Rsh8x64  <t> (Rsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh8x64  x (Const64 <t> [c+d]))
   464  
   465  (Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh64Ux64 x (Const64 <t> [c+d]))
   466  (Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh32Ux64 x (Const64 <t> [c+d]))
   467  (Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh16Ux64 x (Const64 <t> [c+d]))
   468  (Rsh8Ux64  <t> (Rsh8Ux64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh8Ux64  x (Const64 <t> [c+d]))
   469  
   470  // Remove signed right shift before an unsigned right shift that extracts the sign bit.
   471  (Rsh8Ux64  (Rsh8x64  x _) (Const64 <t> [7] )) => (Rsh8Ux64  x (Const64 <t> [7] ))
   472  (Rsh16Ux64 (Rsh16x64 x _) (Const64 <t> [15])) => (Rsh16Ux64 x (Const64 <t> [15]))
   473  (Rsh32Ux64 (Rsh32x64 x _) (Const64 <t> [31])) => (Rsh32Ux64 x (Const64 <t> [31]))
   474  (Rsh64Ux64 (Rsh64x64 x _) (Const64 <t> [63])) => (Rsh64Ux64 x (Const64 <t> [63]))
   475  
   476  // Convert x>>c<<c to x&^(1<<c-1)
   477  (Lsh64x64 i:(Rsh(64|64U)x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 64 && i.Uses == 1 => (And64 x (Const64 <v.Type> [int64(-1) << c]))
   478  (Lsh32x64 i:(Rsh(32|32U)x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 32 && i.Uses == 1 => (And32 x (Const32 <v.Type> [int32(-1) << c]))
   479  (Lsh16x64 i:(Rsh(16|16U)x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 16 && i.Uses == 1 => (And16 x (Const16 <v.Type> [int16(-1) << c]))
   480  (Lsh8x64  i:(Rsh(8|8U)x64    x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 8  && i.Uses == 1 => (And8  x (Const8  <v.Type> [int8(-1)  << c]))
   481  // similarly for x<<c>>c
   482  (Rsh64Ux64 i:(Lsh64x64 x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 64 && i.Uses == 1 => (And64 x (Const64 <v.Type> [int64(^uint64(0)>>c)]))
   483  (Rsh32Ux64 i:(Lsh32x64 x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 32 && i.Uses == 1 => (And32 x (Const32 <v.Type> [int32(^uint32(0)>>c)]))
   484  (Rsh16Ux64 i:(Lsh16x64 x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 16 && i.Uses == 1 => (And16 x (Const16 <v.Type> [int16(^uint16(0)>>c)]))
   485  (Rsh8Ux64  i:(Lsh8x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 8  && i.Uses == 1 => (And8  x (Const8  <v.Type> [int8 (^uint8 (0)>>c)]))
   486  
   487  // ((x >> c1) << c2) >> c3
   488  (Rsh(64|32|16|8)Ux64 (Lsh(64|32|16|8)x64 (Rsh(64|32|16|8)Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   489    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   490    => (Rsh(64|32|16|8)Ux64 x (Const64 <typ.UInt64> [c1-c2+c3]))
   491  
   492  // ((x << c1) >> c2) << c3
   493  (Lsh(64|32|16|8)x64 (Rsh(64|32|16|8)Ux64 (Lsh(64|32|16|8)x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   494    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   495    => (Lsh(64|32|16|8)x64 x (Const64 <typ.UInt64> [c1-c2+c3]))
   496  
   497  // (x >> c) & uppermask = 0
   498  (And64 (Const64 [m]) (Rsh64Ux64 _ (Const64 [c]))) && c >= int64(64-ntz64(m)) => (Const64 [0])
   499  (And32 (Const32 [m]) (Rsh32Ux64 _ (Const64 [c]))) && c >= int64(32-ntz32(m)) => (Const32 [0])
   500  (And16 (Const16 [m]) (Rsh16Ux64 _ (Const64 [c]))) && c >= int64(16-ntz16(m)) => (Const16 [0])
   501  (And8  (Const8  [m]) (Rsh8Ux64  _ (Const64 [c]))) && c >= int64(8-ntz8(m))  => (Const8  [0])
   502  
   503  // (x << c) & lowermask = 0
   504  (And64 (Const64 [m]) (Lsh64x64  _ (Const64 [c]))) && c >= int64(64-nlz64(m)) => (Const64 [0])
   505  (And32 (Const32 [m]) (Lsh32x64  _ (Const64 [c]))) && c >= int64(32-nlz32(m)) => (Const32 [0])
   506  (And16 (Const16 [m]) (Lsh16x64  _ (Const64 [c]))) && c >= int64(16-nlz16(m)) => (Const16 [0])
   507  (And8  (Const8  [m]) (Lsh8x64   _ (Const64 [c]))) && c >= int64(8-nlz8(m))  => (Const8  [0])
   508  
   509  // replace shifts with zero extensions
   510  (Rsh16Ux64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) => (ZeroExt8to16  (Trunc16to8  <typ.UInt8>  x))
   511  (Rsh32Ux64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) => (ZeroExt8to32  (Trunc32to8  <typ.UInt8>  x))
   512  (Rsh64Ux64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) => (ZeroExt8to64  (Trunc64to8  <typ.UInt8>  x))
   513  (Rsh32Ux64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) => (ZeroExt16to32 (Trunc32to16 <typ.UInt16> x))
   514  (Rsh64Ux64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) => (ZeroExt16to64 (Trunc64to16 <typ.UInt16> x))
   515  (Rsh64Ux64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) => (ZeroExt32to64 (Trunc64to32 <typ.UInt32> x))
   516  
   517  // replace shifts with sign extensions
   518  (Rsh16x64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) => (SignExt8to16  (Trunc16to8  <typ.Int8>  x))
   519  (Rsh32x64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) => (SignExt8to32  (Trunc32to8  <typ.Int8>  x))
   520  (Rsh64x64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) => (SignExt8to64  (Trunc64to8  <typ.Int8>  x))
   521  (Rsh32x64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) => (SignExt16to32 (Trunc32to16 <typ.Int16> x))
   522  (Rsh64x64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) => (SignExt16to64 (Trunc64to16 <typ.Int16> x))
   523  (Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) => (SignExt32to64 (Trunc64to32 <typ.Int32> x))
   524  
   525  // ((x >> c) & d) << e
   526  (Lsh64x64 (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e])) && c >= e => (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c-e])) (Const64 <t> [d<<e]))
   527  (Lsh32x64 (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e])) && c >= e => (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c-e])) (Const32 <t> [d<<e]))
   528  (Lsh16x64 (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e])) && c >= e => (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c-e])) (Const16 <t> [d<<e]))
   529  (Lsh8x64  (And8  (Rsh(8|8U)x64   <t> x (Const64 <t2> [c])) (Const8  [d])) (Const64 [e])) && c >= e => (And8  (Rsh(8|8U)x64   <t> x (Const64 <t2> [c-e])) (Const8  <t> [d<<e]))
   530  (Lsh64x64 (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e])) && c < e =>  (And64 (Lsh64x64 <t> x (Const64 <t2> [e-c])) (Const64 <t> [d<<e]))
   531  (Lsh32x64 (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e])) && c < e =>  (And32 (Lsh32x64 <t> x (Const64 <t2> [e-c])) (Const32 <t> [d<<e]))
   532  (Lsh16x64 (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e])) && c < e =>  (And16 (Lsh16x64 <t> x (Const64 <t2> [e-c])) (Const16 <t> [d<<e]))
   533  (Lsh8x64  (And8  (Rsh(8|8U)x64   <t> x (Const64 <t2> [c])) (Const8  [d])) (Const64 [e])) && c < e =>  (And8  (Lsh8x64  <t> x (Const64 <t2> [e-c])) (Const8  <t> [d<<e]))
   534  
   535  // constant comparisons
   536  (Eq(64|32|16|8)   (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c == d])
   537  (Neq(64|32|16|8)  (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c != d])
   538  (Less(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c < d])
   539  (Leq(64|32|16|8)  (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c <= d])
   540  
   541  (Less64U (Const64 [c]) (Const64 [d])) => (ConstBool [uint64(c) < uint64(d)])
   542  (Less32U (Const32 [c]) (Const32 [d])) => (ConstBool [uint32(c) < uint32(d)])
   543  (Less16U (Const16 [c]) (Const16 [d])) => (ConstBool [uint16(c) < uint16(d)])
   544  (Less8U  (Const8  [c]) (Const8  [d])) => (ConstBool [ uint8(c) <  uint8(d)])
   545  
   546  (Leq64U (Const64 [c]) (Const64 [d])) => (ConstBool [uint64(c) <= uint64(d)])
   547  (Leq32U (Const32 [c]) (Const32 [d])) => (ConstBool [uint32(c) <= uint32(d)])
   548  (Leq16U (Const16 [c]) (Const16 [d])) => (ConstBool [uint16(c) <= uint16(d)])
   549  (Leq8U  (Const8  [c]) (Const8  [d])) => (ConstBool [ uint8(c) <=  uint8(d)])
   550  
   551  (Leq8  (Const8  [0]) (And8  _ (Const8  [c]))) && c >= 0 => (ConstBool [true])
   552  (Leq16 (Const16 [0]) (And16 _ (Const16 [c]))) && c >= 0 => (ConstBool [true])
   553  (Leq32 (Const32 [0]) (And32 _ (Const32 [c]))) && c >= 0 => (ConstBool [true])
   554  (Leq64 (Const64 [0]) (And64 _ (Const64 [c]))) && c >= 0 => (ConstBool [true])
   555  
   556  (Leq8  (Const8  [0]) (Rsh8Ux64  _ (Const64 [c]))) && c > 0 => (ConstBool [true])
   557  (Leq16 (Const16 [0]) (Rsh16Ux64 _ (Const64 [c]))) && c > 0 => (ConstBool [true])
   558  (Leq32 (Const32 [0]) (Rsh32Ux64 _ (Const64 [c]))) && c > 0 => (ConstBool [true])
   559  (Leq64 (Const64 [0]) (Rsh64Ux64 _ (Const64 [c]))) && c > 0 => (ConstBool [true])
   560  
   561  // prefer equalities with zero
   562  (Less(64|32|16|8) (Const(64|32|16|8) <t> [0]) x) && isNonNegative(x) => (Neq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   563  (Less(64|32|16|8) x (Const(64|32|16|8) <t> [1])) && isNonNegative(x) => (Eq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   564  (Less(64|32|16|8)U x (Const(64|32|16|8) <t> [1])) => (Eq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   565  (Leq(64|32|16|8)U (Const(64|32|16|8) <t> [1]) x) => (Neq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   566  
   567  // prefer comparisons with zero
   568  (Less(64|32|16|8) x (Const(64|32|16|8) <t> [1])) => (Leq(64|32|16|8) x (Const(64|32|16|8) <t> [0]))
   569  (Leq(64|32|16|8) x (Const(64|32|16|8) <t> [-1])) => (Less(64|32|16|8) x (Const(64|32|16|8) <t> [0]))
   570  (Leq(64|32|16|8) (Const(64|32|16|8) <t> [1]) x) => (Less(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   571  (Less(64|32|16|8) (Const(64|32|16|8) <t> [-1]) x) => (Leq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   572  
   573  // constant floating point comparisons
   574  (Eq32F   (Const32F [c]) (Const32F [d])) => (ConstBool [c == d])
   575  (Eq64F   (Const64F [c]) (Const64F [d])) => (ConstBool [c == d])
   576  (Neq32F  (Const32F [c]) (Const32F [d])) => (ConstBool [c != d])
   577  (Neq64F  (Const64F [c]) (Const64F [d])) => (ConstBool [c != d])
   578  (Less32F (Const32F [c]) (Const32F [d])) => (ConstBool [c < d])
   579  (Less64F (Const64F [c]) (Const64F [d])) => (ConstBool [c < d])
   580  (Leq32F  (Const32F [c]) (Const32F [d])) => (ConstBool [c <= d])
   581  (Leq64F  (Const64F [c]) (Const64F [d])) => (ConstBool [c <= d])
   582  
   583  // simplifications
   584  (Or(64|32|16|8) x x) => x
   585  (Or(64|32|16|8) (Const(64|32|16|8)  [0]) x) => x
   586  (Or(64|32|16|8) (Const(64|32|16|8) [-1]) _) => (Const(64|32|16|8) [-1])
   587  (Or(64|32|16|8) (Com(64|32|16|8)     x)  x) => (Const(64|32|16|8) [-1])
   588  
   589  (And(64|32|16|8) x x) => x
   590  (And(64|32|16|8) (Const(64|32|16|8) [-1]) x) => x
   591  (And(64|32|16|8) (Const(64|32|16|8)  [0]) _) => (Const(64|32|16|8) [0])
   592  (And(64|32|16|8) (Com(64|32|16|8)     x)  x) => (Const(64|32|16|8) [0])
   593  
   594  (Xor(64|32|16|8) x x) => (Const(64|32|16|8) [0])
   595  (Xor(64|32|16|8) (Const(64|32|16|8) [0]) x) => x
   596  (Xor(64|32|16|8) (Com(64|32|16|8)    x)  x) => (Const(64|32|16|8) [-1])
   597  
   598  (Add(64|32|16|8) (Const(64|32|16|8) [0]) x) => x
   599  (Sub(64|32|16|8) x x) => (Const(64|32|16|8) [0])
   600  (Mul(64|32|16|8) (Const(64|32|16|8) [0]) _) => (Const(64|32|16|8) [0])
   601  (Mul(64|32)uover <t> (Const(64|32) [0]) x) => (MakeTuple (Const(64|32) <t.FieldType(0)> [0]) (ConstBool <t.FieldType(1)> [false]))
   602  
   603  (Com(64|32|16|8) (Com(64|32|16|8)  x)) => x
   604  (Com(64|32|16|8) (Const(64|32|16|8) [c])) => (Const(64|32|16|8) [^c])
   605  
   606  (Neg(64|32|16|8) (Sub(64|32|16|8) x y)) => (Sub(64|32|16|8) y x)
   607  (Add(64|32|16|8) x (Neg(64|32|16|8) y)) => (Sub(64|32|16|8) x y)
   608  
   609  (Xor(64|32|16|8) (Const(64|32|16|8) [-1]) x) => (Com(64|32|16|8) x)
   610  
   611  (Sub(64|32|16|8) (Neg(64|32|16|8) x) (Com(64|32|16|8) x)) => (Const(64|32|16|8) [1])
   612  (Sub(64|32|16|8) (Com(64|32|16|8) x) (Neg(64|32|16|8) x)) => (Const(64|32|16|8) [-1])
   613  (Add(64|32|16|8) (Com(64|32|16|8) x)                  x)  => (Const(64|32|16|8) [-1])
   614  
   615  // Simplification when involving common integer
   616  // (t + x) - (t + y) == x - y
   617  // (t + x) - (y + t) == x - y
   618  // (x + t) - (y + t) == x - y
   619  // (x + t) - (t + y) == x - y
   620  // (x - t) + (t + y) == x + y
   621  // (x - t) + (y + t) == x + y
   622  (Sub(64|32|16|8) (Add(64|32|16|8) t x) (Add(64|32|16|8) t y)) => (Sub(64|32|16|8) x y)
   623  (Add(64|32|16|8) (Sub(64|32|16|8) x t) (Add(64|32|16|8) t y)) => (Add(64|32|16|8) x y)
   624  
   625  // ^(x-1) == ^x+1 == -x
   626  (Add(64|32|16|8) (Const(64|32|16|8) [1]) (Com(64|32|16|8) x)) => (Neg(64|32|16|8) x)
   627  (Com(64|32|16|8) (Add(64|32|16|8) (Const(64|32|16|8) [-1]) x)) => (Neg(64|32|16|8) x)
   628  
   629  // -(-x) == x
   630  (Neg(64|32|16|8) (Neg(64|32|16|8) x)) => x
   631  
   632  // -^x == x+1
   633  (Neg(64|32|16|8) <t> (Com(64|32|16|8) x)) => (Add(64|32|16|8) (Const(64|32|16|8) <t> [1]) x)
   634  
   635  (And(64|32|16|8) x (And(64|32|16|8) x y)) => (And(64|32|16|8) x y)
   636  (Or(64|32|16|8) x (Or(64|32|16|8) x y)) => (Or(64|32|16|8) x y)
   637  (Xor(64|32|16|8) x (Xor(64|32|16|8) x y)) => y
   638  
   639  // Fold comparisons with numeric bounds
   640  (Less(64|32|16|8)U _ (Const(64|32|16|8) [0]))  => (ConstBool [false])
   641  (Leq(64|32|16|8)U (Const(64|32|16|8) [0]) _)   => (ConstBool [true])
   642  (Less(64|32|16|8)U (Const(64|32|16|8) [-1]) _) => (ConstBool [false])
   643  (Leq(64|32|16|8)U _ (Const(64|32|16|8) [-1]))  => (ConstBool [true])
   644  (Less64 _ (Const64 [math.MinInt64])) => (ConstBool [false])
   645  (Less32 _ (Const32 [math.MinInt32])) => (ConstBool [false])
   646  (Less16 _ (Const16 [math.MinInt16])) => (ConstBool [false])
   647  (Less8  _ (Const8  [math.MinInt8 ])) => (ConstBool [false])
   648  (Leq64 (Const64 [math.MinInt64]) _)  => (ConstBool [true])
   649  (Leq32 (Const32 [math.MinInt32]) _)  => (ConstBool [true])
   650  (Leq16 (Const16 [math.MinInt16]) _)  => (ConstBool [true])
   651  (Leq8  (Const8  [math.MinInt8 ]) _)  => (ConstBool [true])
   652  (Less64 (Const64 [math.MaxInt64]) _) => (ConstBool [false])
   653  (Less32 (Const32 [math.MaxInt32]) _) => (ConstBool [false])
   654  (Less16 (Const16 [math.MaxInt16]) _) => (ConstBool [false])
   655  (Less8  (Const8  [math.MaxInt8 ]) _) => (ConstBool [false])
   656  (Leq64 _ (Const64 [math.MaxInt64]))  => (ConstBool [true])
   657  (Leq32 _ (Const32 [math.MaxInt32]))  => (ConstBool [true])
   658  (Leq16 _ (Const16 [math.MaxInt16]))  => (ConstBool [true])
   659  (Leq8  _ (Const8  [math.MaxInt8 ]))  => (ConstBool [true])
   660  
   661  // Canonicalize <= on numeric bounds and < near numeric bounds to ==
   662  (Leq(64|32|16|8)U x c:(Const(64|32|16|8) [0]))     => (Eq(64|32|16|8) x c)
   663  (Leq(64|32|16|8)U c:(Const(64|32|16|8) [-1]) x)    => (Eq(64|32|16|8) x c)
   664  (Less(64|32|16|8)U x (Const(64|32|16|8) <t> [1]))  => (Eq(64|32|16|8) x (Const(64|32|16|8) <t> [0]))
   665  (Less(64|32|16|8)U (Const(64|32|16|8) <t> [-2]) x) => (Eq(64|32|16|8) x (Const(64|32|16|8) <t> [-1]))
   666  (Leq64 x c:(Const64 [math.MinInt64])) => (Eq64 x c)
   667  (Leq32 x c:(Const32 [math.MinInt32])) => (Eq32 x c)
   668  (Leq16 x c:(Const16 [math.MinInt16])) => (Eq16 x c)
   669  (Leq8  x c:(Const8  [math.MinInt8 ])) => (Eq8  x c)
   670  (Leq64 c:(Const64 [math.MaxInt64]) x) => (Eq64 x c)
   671  (Leq32 c:(Const32 [math.MaxInt32]) x) => (Eq32 x c)
   672  (Leq16 c:(Const16 [math.MaxInt16]) x) => (Eq16 x c)
   673  (Leq8  c:(Const8  [math.MaxInt8 ]) x) => (Eq8  x c)
   674  (Less64 x (Const64 <t> [math.MinInt64+1])) => (Eq64 x (Const64 <t> [math.MinInt64]))
   675  (Less32 x (Const32 <t> [math.MinInt32+1])) => (Eq32 x (Const32 <t> [math.MinInt32]))
   676  (Less16 x (Const16 <t> [math.MinInt16+1])) => (Eq16 x (Const16 <t> [math.MinInt16]))
   677  (Less8  x (Const8  <t> [math.MinInt8 +1])) => (Eq8  x (Const8  <t> [math.MinInt8 ]))
   678  (Less64 (Const64 <t> [math.MaxInt64-1]) x) => (Eq64 x (Const64 <t> [math.MaxInt64]))
   679  (Less32 (Const32 <t> [math.MaxInt32-1]) x) => (Eq32 x (Const32 <t> [math.MaxInt32]))
   680  (Less16 (Const16 <t> [math.MaxInt16-1]) x) => (Eq16 x (Const16 <t> [math.MaxInt16]))
   681  (Less8  (Const8  <t> [math.MaxInt8 -1]) x) => (Eq8  x (Const8  <t> [math.MaxInt8 ]))
   682  
   683  // Ands clear bits. Ors set bits.
   684  // If a subsequent Or will set all the bits
   685  // that an And cleared, we can skip the And.
   686  // This happens in bitmasking code like:
   687  //   x &^= 3 << shift // clear two old bits
   688  //   x  |= v << shift // set two new bits
   689  // when shift is a small constant and v ends up a constant 3.
   690  (Or8  (And8  x (Const8  [c2])) (Const8  <t> [c1])) && ^(c1 | c2) == 0 => (Or8  (Const8  <t> [c1]) x)
   691  (Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 => (Or16 (Const16 <t> [c1]) x)
   692  (Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 => (Or32 (Const32 <t> [c1]) x)
   693  (Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 => (Or64 (Const64 <t> [c1]) x)
   694  
   695  (Trunc64to8  (And64 (Const64 [y]) x)) && y&0xFF == 0xFF => (Trunc64to8 x)
   696  (Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF => (Trunc64to16 x)
   697  (Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF => (Trunc64to32 x)
   698  (Trunc32to8  (And32 (Const32 [y]) x)) && y&0xFF == 0xFF => (Trunc32to8 x)
   699  (Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF => (Trunc32to16 x)
   700  (Trunc16to8  (And16 (Const16 [y]) x)) && y&0xFF == 0xFF => (Trunc16to8 x)
   701  
   702  (ZeroExt8to64  (Trunc64to8  x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 => x
   703  (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 => x
   704  (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 => x
   705  (ZeroExt8to32  (Trunc32to8  x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 => x
   706  (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 => x
   707  (ZeroExt8to16  (Trunc16to8  x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 => x
   708  
   709  (SignExt8to64  (Trunc64to8  x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 => x
   710  (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 => x
   711  (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 => x
   712  (SignExt8to32  (Trunc32to8  x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 => x
   713  (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 => x
   714  (SignExt8to16  (Trunc16to8  x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 => x
   715  
   716  (Slicemask (Const32 [x])) && x > 0 => (Const32 [-1])
   717  (Slicemask (Const32 [0]))          => (Const32 [0])
   718  (Slicemask (Const64 [x])) && x > 0 => (Const64 [-1])
   719  (Slicemask (Const64 [0]))          => (Const64 [0])
   720  
   721  // simplifications often used for lengths.  e.g. len(s[i:i+5])==5
   722  (Sub(64|32|16|8) (Add(64|32|16|8) x y) x) => y
   723  (Sub(64|32|16|8) (Add(64|32|16|8) x y) y) => x
   724  (Sub(64|32|16|8) (Sub(64|32|16|8) x y) x) => (Neg(64|32|16|8) y)
   725  (Sub(64|32|16|8) x (Add(64|32|16|8) x y)) => (Neg(64|32|16|8) y)
   726  (Add(64|32|16|8) x (Sub(64|32|16|8) y x)) => y
   727  (Add(64|32|16|8) x (Add(64|32|16|8) y (Sub(64|32|16|8) z x))) => (Add(64|32|16|8) y z)
   728  
   729  // basic phi simplifications
   730  (Phi (Const8  [c]) (Const8  [c])) => (Const8  [c])
   731  (Phi (Const16 [c]) (Const16 [c])) => (Const16 [c])
   732  (Phi (Const32 [c]) (Const32 [c])) => (Const32 [c])
   733  (Phi (Const64 [c]) (Const64 [c])) => (Const64 [c])
   734  
   735  // slice and interface comparisons
   736  // The frontend ensures that we can only compare against nil,
   737  // so we need only compare the first word (interface type or slice ptr).
   738  (EqInter x y)  => (EqPtr  (ITab x) (ITab y))
   739  (NeqInter x y) => (NeqPtr (ITab x) (ITab y))
   740  (EqSlice x y)  => (EqPtr  (SlicePtr x) (SlicePtr y))
   741  (NeqSlice x y) => (NeqPtr (SlicePtr x) (SlicePtr y))
   742  
   743  // Load of store of same address, with compatibly typed value and same size
   744  (Load <t1> p1 (Store {t2} p2 x _))
   745  	&& isSamePtr(p1, p2)
   746  	&& copyCompatibleType(t1, x.Type)
   747  	&& t1.Size() == t2.Size()
   748  	=> x
   749  (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 x _)))
   750  	&& isSamePtr(p1, p3)
   751  	&& copyCompatibleType(t1, x.Type)
   752  	&& t1.Size() == t3.Size()
   753  	&& disjoint(p3, t3.Size(), p2, t2.Size())
   754  	=> x
   755  (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 x _))))
   756  	&& isSamePtr(p1, p4)
   757  	&& copyCompatibleType(t1, x.Type)
   758  	&& t1.Size() == t4.Size()
   759  	&& disjoint(p4, t4.Size(), p2, t2.Size())
   760  	&& disjoint(p4, t4.Size(), p3, t3.Size())
   761  	=> x
   762  (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 x _)))))
   763  	&& isSamePtr(p1, p5)
   764  	&& copyCompatibleType(t1, x.Type)
   765  	&& t1.Size() == t5.Size()
   766  	&& disjoint(p5, t5.Size(), p2, t2.Size())
   767  	&& disjoint(p5, t5.Size(), p3, t3.Size())
   768  	&& disjoint(p5, t5.Size(), p4, t4.Size())
   769  	=> x
   770  
   771  // Pass constants through math.Float{32,64}bits and math.Float{32,64}frombits
   772  (Load <t1> p1 (Store {t2} p2 (Const64  [x]) _)) && isSamePtr(p1,p2) && t2.Size() == 8 && is64BitFloat(t1) && !math.IsNaN(math.Float64frombits(uint64(x))) => (Const64F [math.Float64frombits(uint64(x))])
   773  (Load <t1> p1 (Store {t2} p2 (Const32  [x]) _)) && isSamePtr(p1,p2) && t2.Size() == 4 && is32BitFloat(t1) && !math.IsNaN(float64(math.Float32frombits(uint32(x)))) => (Const32F [math.Float32frombits(uint32(x))])
   774  (Load <t1> p1 (Store {t2} p2 (Const64F [x]) _)) && isSamePtr(p1,p2) && t2.Size() == 8 && is64BitInt(t1)   => (Const64  [int64(math.Float64bits(x))])
   775  (Load <t1> p1 (Store {t2} p2 (Const32F [x]) _)) && isSamePtr(p1,p2) && t2.Size() == 4 && is32BitInt(t1)   => (Const32  [int32(math.Float32bits(x))])
   776  
   777  // Float Loads up to Zeros so they can be constant folded.
   778  (Load <t1> op:(OffPtr [o1] p1)
   779  	(Store {t2} p2 _
   780  		mem:(Zero [n] p3 _)))
   781  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p3)
   782  	&& CanSSA(t1)
   783  	&& disjoint(op, t1.Size(), p2, t2.Size())
   784  	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p3) mem)
   785  (Load <t1> op:(OffPtr [o1] p1)
   786  	(Store {t2} p2 _
   787  		(Store {t3} p3 _
   788  			mem:(Zero [n] p4 _))))
   789  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p4)
   790  	&& CanSSA(t1)
   791  	&& disjoint(op, t1.Size(), p2, t2.Size())
   792  	&& disjoint(op, t1.Size(), p3, t3.Size())
   793  	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p4) mem)
   794  (Load <t1> op:(OffPtr [o1] p1)
   795  	(Store {t2} p2 _
   796  		(Store {t3} p3 _
   797  			(Store {t4} p4 _
   798  				mem:(Zero [n] p5 _)))))
   799  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p5)
   800  	&& CanSSA(t1)
   801  	&& disjoint(op, t1.Size(), p2, t2.Size())
   802  	&& disjoint(op, t1.Size(), p3, t3.Size())
   803  	&& disjoint(op, t1.Size(), p4, t4.Size())
   804  	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p5) mem)
   805  (Load <t1> op:(OffPtr [o1] p1)
   806  	(Store {t2} p2 _
   807  		(Store {t3} p3 _
   808  			(Store {t4} p4 _
   809  				(Store {t5} p5 _
   810  					mem:(Zero [n] p6 _))))))
   811  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p6)
   812  	&& CanSSA(t1)
   813  	&& disjoint(op, t1.Size(), p2, t2.Size())
   814  	&& disjoint(op, t1.Size(), p3, t3.Size())
   815  	&& disjoint(op, t1.Size(), p4, t4.Size())
   816  	&& disjoint(op, t1.Size(), p5, t5.Size())
   817  	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p6) mem)
   818  
   819  // Zero to Load forwarding.
   820  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   821  	&& t1.IsBoolean()
   822  	&& isSamePtr(p1, p2)
   823  	&& n >= o + 1
   824  	=> (ConstBool [false])
   825  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   826  	&& is8BitInt(t1)
   827  	&& isSamePtr(p1, p2)
   828  	&& n >= o + 1
   829  	=> (Const8 [0])
   830  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   831  	&& is16BitInt(t1)
   832  	&& isSamePtr(p1, p2)
   833  	&& n >= o + 2
   834  	=> (Const16 [0])
   835  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   836  	&& is32BitInt(t1)
   837  	&& isSamePtr(p1, p2)
   838  	&& n >= o + 4
   839  	=> (Const32 [0])
   840  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   841  	&& is64BitInt(t1)
   842  	&& isSamePtr(p1, p2)
   843  	&& n >= o + 8
   844  	=> (Const64 [0])
   845  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   846  	&& is32BitFloat(t1)
   847  	&& isSamePtr(p1, p2)
   848  	&& n >= o + 4
   849  	=> (Const32F [0])
   850  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   851  	&& is64BitFloat(t1)
   852  	&& isSamePtr(p1, p2)
   853  	&& n >= o + 8
   854  	=> (Const64F [0])
   855  
   856  // Eliminate stores of values that have just been loaded from the same location.
   857  // We also handle the common case where there are some intermediate stores.
   858  (Store {t1} p1 (Load <t2> p2 mem) mem)
   859  	&& isSamePtr(p1, p2)
   860  	&& t2.Size() == t1.Size()
   861  	=> mem
   862  (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ oldmem))
   863  	&& isSamePtr(p1, p2)
   864  	&& t2.Size() == t1.Size()
   865  	&& disjoint(p1, t1.Size(), p3, t3.Size())
   866  	=> mem
   867  (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ oldmem)))
   868  	&& isSamePtr(p1, p2)
   869  	&& t2.Size() == t1.Size()
   870  	&& disjoint(p1, t1.Size(), p3, t3.Size())
   871  	&& disjoint(p1, t1.Size(), p4, t4.Size())
   872  	=> mem
   873  (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 _ oldmem))))
   874  	&& isSamePtr(p1, p2)
   875  	&& t2.Size() == t1.Size()
   876  	&& disjoint(p1, t1.Size(), p3, t3.Size())
   877  	&& disjoint(p1, t1.Size(), p4, t4.Size())
   878  	&& disjoint(p1, t1.Size(), p5, t5.Size())
   879  	=> mem
   880  
   881  // Don't Store zeros to cleared variables.
   882  (Store {t} (OffPtr [o] p1) x mem:(Zero [n] p2 _))
   883  	&& isConstZero(x)
   884  	&& o >= 0 && t.Size() + o <= n && isSamePtr(p1, p2)
   885  	=> mem
   886  (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Zero [n] p3 _)))
   887  	&& isConstZero(x)
   888  	&& o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p3)
   889  	&& disjoint(op, t1.Size(), p2, t2.Size())
   890  	=> mem
   891  (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Zero [n] p4 _))))
   892  	&& isConstZero(x)
   893  	&& o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p4)
   894  	&& disjoint(op, t1.Size(), p2, t2.Size())
   895  	&& disjoint(op, t1.Size(), p3, t3.Size())
   896  	=> mem
   897  (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Zero [n] p5 _)))))
   898  	&& isConstZero(x)
   899  	&& o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p5)
   900  	&& disjoint(op, t1.Size(), p2, t2.Size())
   901  	&& disjoint(op, t1.Size(), p3, t3.Size())
   902  	&& disjoint(op, t1.Size(), p4, t4.Size())
   903  	=> mem
   904  
   905  // Collapse OffPtr
   906  (OffPtr (OffPtr p [y]) [x]) => (OffPtr p [x+y])
   907  (OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq => p
   908  
   909  // indexing operations
   910  // Note: bounds check has already been done
   911  (PtrIndex <t> ptr idx) && config.PtrSize == 4 && is32Bit(t.Elem().Size()) => (AddPtr ptr (Mul32 <typ.Int> idx (Const32 <typ.Int> [int32(t.Elem().Size())])))
   912  (PtrIndex <t> ptr idx) && config.PtrSize == 8 => (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.Elem().Size()])))
   913  
   914  // struct operations
   915  (StructSelect [i] x:(StructMake ___)) => x.Args[i]
   916  (Load <t> _ _) && t.IsStruct() && CanSSA(t) => rewriteStructLoad(v)
   917  (Store _ (StructMake ___) _) => rewriteStructStore(v)
   918  
   919  (StructSelect [i] x:(Load <t> ptr mem)) && !CanSSA(t) =>
   920    @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
   921  
   922  // Putting struct{*byte} and similar into direct interfaces.
   923  (IMake _typ (StructMake val)) => (IMake _typ val)
   924  (StructSelect [0] (IData x)) => (IData x)
   925  
   926  // un-SSAable values use mem->mem copies
   927  (Store {t} dst (Load src mem) mem) && !CanSSA(t) =>
   928  	(Move {t} [t.Size()] dst src mem)
   929  (Store {t} dst (Load src mem) (VarDef {x} mem)) && !CanSSA(t) =>
   930  	(Move {t} [t.Size()] dst src (VarDef {x} mem))
   931  
   932  // array ops
   933  (ArraySelect (ArrayMake1 x)) => x
   934  
   935  (Load <t> _ _) && t.IsArray() && t.NumElem() == 0 =>
   936    (ArrayMake0)
   937  
   938  (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && CanSSA(t) =>
   939    (ArrayMake1 (Load <t.Elem()> ptr mem))
   940  
   941  (Store _ (ArrayMake0) mem) => mem
   942  (Store dst (ArrayMake1 e) mem) => (Store {e.Type} dst e mem)
   943  
   944  // Putting [1]*byte and similar into direct interfaces.
   945  (IMake _typ (ArrayMake1 val)) => (IMake _typ val)
   946  (ArraySelect [0] (IData x)) => (IData x)
   947  
   948  // string ops
   949  // Decomposing StringMake and lowering of StringPtr and StringLen
   950  // happens in a later pass, dec, so that these operations are available
   951  // to other passes for optimizations.
   952  (StringPtr (StringMake (Addr <t> {s} base) _)) => (Addr <t> {s} base)
   953  (StringLen (StringMake _ (Const64 <t> [c]))) => (Const64 <t> [c])
   954  (ConstString {str}) && config.PtrSize == 4 && str == "" =>
   955    (StringMake (ConstNil) (Const32 <typ.Int> [0]))
   956  (ConstString {str}) && config.PtrSize == 8 && str == "" =>
   957    (StringMake (ConstNil) (Const64 <typ.Int> [0]))
   958  (ConstString {str}) && config.PtrSize == 4 && str != "" =>
   959    (StringMake
   960      (Addr <typ.BytePtr> {fe.StringData(str)}
   961        (SB))
   962      (Const32 <typ.Int> [int32(len(str))]))
   963  (ConstString {str}) && config.PtrSize == 8 && str != "" =>
   964    (StringMake
   965      (Addr <typ.BytePtr> {fe.StringData(str)}
   966        (SB))
   967      (Const64 <typ.Int> [int64(len(str))]))
   968  
   969  // slice ops
   970  // Only a few slice rules are provided here.  See dec.rules for
   971  // a more comprehensive set.
   972  (SliceLen (SliceMake _ (Const64 <t> [c]) _)) => (Const64 <t> [c])
   973  (SliceCap (SliceMake _ _ (Const64 <t> [c]))) => (Const64 <t> [c])
   974  (SliceLen (SliceMake _ (Const32 <t> [c]) _)) => (Const32 <t> [c])
   975  (SliceCap (SliceMake _ _ (Const32 <t> [c]))) => (Const32 <t> [c])
   976  (SlicePtr (SliceMake (SlicePtr x) _ _)) => (SlicePtr x)
   977  (SliceLen (SliceMake _ (SliceLen x) _)) => (SliceLen x)
   978  (SliceCap (SliceMake _ _ (SliceCap x))) => (SliceCap x)
   979  (SliceCap (SliceMake _ _ (SliceLen x))) => (SliceLen x)
   980  (ConstSlice) && config.PtrSize == 4 =>
   981    (SliceMake
   982      (ConstNil <v.Type.Elem().PtrTo()>)
   983      (Const32 <typ.Int> [0])
   984      (Const32 <typ.Int> [0]))
   985  (ConstSlice) && config.PtrSize == 8 =>
   986    (SliceMake
   987      (ConstNil <v.Type.Elem().PtrTo()>)
   988      (Const64 <typ.Int> [0])
   989      (Const64 <typ.Int> [0]))
   990  
   991  // interface ops
   992  (ConstInterface) =>
   993    (IMake
   994      (ConstNil <typ.Uintptr>)
   995      (ConstNil <typ.BytePtr>))
   996  
   997  (NilCheck ptr:(GetG mem) mem) => ptr
   998  
   999  (If (Not cond) yes no) => (If cond no yes)
  1000  (If (ConstBool [c]) yes no) && c => (First yes no)
  1001  (If (ConstBool [c]) yes no) && !c => (First no yes)
  1002  
  1003  (Phi <t> nx:(Not x) ny:(Not y)) && nx.Uses == 1 && ny.Uses == 1 => (Not (Phi <t> x y))
  1004  
  1005  // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
  1006  (Convert (Add(64|32) (Convert ptr mem) off) mem) => (AddPtr ptr off)
  1007  (Convert (Convert ptr mem) mem) => ptr
  1008  // Note: it is important that the target rewrite is ptr+(off1+off2), not (ptr+off1)+off2.
  1009  // We must ensure that no intermediate computations are invalid pointers.
  1010  (Convert a:(Add(64|32) (Add(64|32) (Convert ptr mem) off1) off2) mem) => (AddPtr ptr (Add(64|32) <a.Type> off1 off2))
  1011  
  1012  // strength reduction of divide by a constant.
  1013  // See ../magic.go for a detailed description of these algorithms.
  1014  
  1015  // Unsigned divide by power of 2.  Strength reduce to a shift.
  1016  (Div8u  n (Const8  [c])) && isPowerOfTwo(c) => (Rsh8Ux64  n (Const64 <typ.UInt64> [log8(c)]))
  1017  (Div16u n (Const16 [c])) && isPowerOfTwo(c) => (Rsh16Ux64 n (Const64 <typ.UInt64> [log16(c)]))
  1018  (Div32u n (Const32 [c])) && isPowerOfTwo(c) => (Rsh32Ux64 n (Const64 <typ.UInt64> [log32(c)]))
  1019  (Div64u n (Const64 [c])) && isPowerOfTwo(c) => (Rsh64Ux64 n (Const64 <typ.UInt64> [log64(c)]))
  1020  (Div64u n (Const64 [-1<<63]))                 => (Rsh64Ux64 n (Const64 <typ.UInt64> [63]))
  1021  
  1022  // Signed non-negative divide by power of 2.
  1023  (Div8  n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo(c) => (Rsh8Ux64  n (Const64 <typ.UInt64> [log8(c)]))
  1024  (Div16 n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c) => (Rsh16Ux64 n (Const64 <typ.UInt64> [log16(c)]))
  1025  (Div32 n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c) => (Rsh32Ux64 n (Const64 <typ.UInt64> [log32(c)]))
  1026  (Div64 n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c) => (Rsh64Ux64 n (Const64 <typ.UInt64> [log64(c)]))
  1027  (Div64 n (Const64 [-1<<63])) && isNonNegative(n)                 => (Const64 [0])
  1028  
  1029  // Unsigned divide, not a power of 2.  Strength reduce to a multiply.
  1030  // For 8-bit divides, we just do a direct 9-bit by 8-bit multiply.
  1031  (Div8u x (Const8 [c])) && umagicOK8(c) =>
  1032    (Trunc32to8
  1033      (Rsh32Ux64 <typ.UInt32>
  1034        (Mul32 <typ.UInt32>
  1035          (Const32 <typ.UInt32> [int32(1<<8+umagic8(c).m)])
  1036          (ZeroExt8to32 x))
  1037        (Const64 <typ.UInt64> [8+umagic8(c).s])))
  1038  
  1039  // For 16-bit divides on 64-bit machines, we do a direct 17-bit by 16-bit multiply.
  1040  (Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 8 =>
  1041    (Trunc64to16
  1042      (Rsh64Ux64 <typ.UInt64>
  1043        (Mul64 <typ.UInt64>
  1044          (Const64 <typ.UInt64> [int64(1<<16+umagic16(c).m)])
  1045          (ZeroExt16to64 x))
  1046        (Const64 <typ.UInt64> [16+umagic16(c).s])))
  1047  
  1048  // For 16-bit divides on 32-bit machines
  1049  (Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 4 && umagic16(c).m&1 == 0 =>
  1050    (Trunc32to16
  1051      (Rsh32Ux64 <typ.UInt32>
  1052        (Mul32 <typ.UInt32>
  1053          (Const32 <typ.UInt32> [int32(1<<15+umagic16(c).m/2)])
  1054          (ZeroExt16to32 x))
  1055        (Const64 <typ.UInt64> [16+umagic16(c).s-1])))
  1056  (Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 4 && c&1 == 0 =>
  1057    (Trunc32to16
  1058      (Rsh32Ux64 <typ.UInt32>
  1059        (Mul32 <typ.UInt32>
  1060          (Const32 <typ.UInt32> [int32(1<<15+(umagic16(c).m+1)/2)])
  1061          (Rsh32Ux64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [1])))
  1062        (Const64 <typ.UInt64> [16+umagic16(c).s-2])))
  1063  (Div16u x (Const16 [c])) && umagicOK16(c) && config.RegSize == 4 && config.useAvg =>
  1064    (Trunc32to16
  1065      (Rsh32Ux64 <typ.UInt32>
  1066        (Avg32u
  1067          (Lsh32x64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [16]))
  1068          (Mul32 <typ.UInt32>
  1069            (Const32 <typ.UInt32> [int32(umagic16(c).m)])
  1070            (ZeroExt16to32 x)))
  1071        (Const64 <typ.UInt64> [16+umagic16(c).s-1])))
  1072  
  1073  // For 32-bit divides on 32-bit machines
  1074  (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 4 && umagic32(c).m&1 == 0 && config.useHmul =>
  1075    (Rsh32Ux64 <typ.UInt32>
  1076      (Hmul32u <typ.UInt32>
  1077        (Const32 <typ.UInt32> [int32(1<<31+umagic32(c).m/2)])
  1078        x)
  1079      (Const64 <typ.UInt64> [umagic32(c).s-1]))
  1080  (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 4 && c&1 == 0 && config.useHmul =>
  1081    (Rsh32Ux64 <typ.UInt32>
  1082      (Hmul32u <typ.UInt32>
  1083        (Const32 <typ.UInt32> [int32(1<<31+(umagic32(c).m+1)/2)])
  1084        (Rsh32Ux64 <typ.UInt32> x (Const64 <typ.UInt64> [1])))
  1085      (Const64 <typ.UInt64> [umagic32(c).s-2]))
  1086  (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 4 && config.useAvg && config.useHmul =>
  1087    (Rsh32Ux64 <typ.UInt32>
  1088      (Avg32u
  1089        x
  1090        (Hmul32u <typ.UInt32>
  1091          (Const32 <typ.UInt32> [int32(umagic32(c).m)])
  1092          x))
  1093      (Const64 <typ.UInt64> [umagic32(c).s-1]))
  1094  
  1095  // For 32-bit divides on 64-bit machines
  1096  // We'll use a regular (non-hi) multiply for this case.
  1097  (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 8 && umagic32(c).m&1 == 0 =>
  1098    (Trunc64to32
  1099      (Rsh64Ux64 <typ.UInt64>
  1100        (Mul64 <typ.UInt64>
  1101          (Const64 <typ.UInt64> [int64(1<<31+umagic32(c).m/2)])
  1102          (ZeroExt32to64 x))
  1103        (Const64 <typ.UInt64> [32+umagic32(c).s-1])))
  1104  (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 8 && c&1 == 0 =>
  1105    (Trunc64to32
  1106      (Rsh64Ux64 <typ.UInt64>
  1107        (Mul64 <typ.UInt64>
  1108          (Const64 <typ.UInt64> [int64(1<<31+(umagic32(c).m+1)/2)])
  1109          (Rsh64Ux64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [1])))
  1110        (Const64 <typ.UInt64> [32+umagic32(c).s-2])))
  1111  (Div32u x (Const32 [c])) && umagicOK32(c) && config.RegSize == 8 && config.useAvg =>
  1112    (Trunc64to32
  1113      (Rsh64Ux64 <typ.UInt64>
  1114        (Avg64u
  1115          (Lsh64x64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [32]))
  1116          (Mul64 <typ.UInt64>
  1117            (Const64 <typ.UInt32> [int64(umagic32(c).m)])
  1118            (ZeroExt32to64 x)))
  1119        (Const64 <typ.UInt64> [32+umagic32(c).s-1])))
  1120  
  1121  // For unsigned 64-bit divides on 32-bit machines,
  1122  // if the constant fits in 16 bits (so that the last term
  1123  // fits in 32 bits), convert to three 32-bit divides by a constant.
  1124  //
  1125  // If 1<<32 = Q * c + R
  1126  // and    x = hi << 32 + lo
  1127  //
  1128  // Then x = (hi/c*c + hi%c) << 32 + lo
  1129  //        = hi/c*c<<32 + hi%c<<32 + lo
  1130  //        = hi/c*c<<32 + (hi%c)*(Q*c+R) + lo/c*c + lo%c
  1131  //        = hi/c*c<<32 + (hi%c)*Q*c + lo/c*c + (hi%c*R+lo%c)
  1132  // and x / c = (hi/c)<<32 + (hi%c)*Q + lo/c + (hi%c*R+lo%c)/c
  1133  (Div64u x (Const64 [c])) && c > 0 && c <= 0xFFFF && umagicOK32(int32(c)) && config.RegSize == 4 && config.useHmul =>
  1134    (Add64
  1135      (Add64 <typ.UInt64>
  1136        (Add64 <typ.UInt64>
  1137          (Lsh64x64 <typ.UInt64>
  1138            (ZeroExt32to64
  1139              (Div32u <typ.UInt32>
  1140                (Trunc64to32 <typ.UInt32> (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [32])))
  1141                (Const32 <typ.UInt32> [int32(c)])))
  1142            (Const64 <typ.UInt64> [32]))
  1143          (ZeroExt32to64 (Div32u <typ.UInt32> (Trunc64to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(c)]))))
  1144        (Mul64 <typ.UInt64>
  1145          (ZeroExt32to64 <typ.UInt64>
  1146            (Mod32u <typ.UInt32>
  1147              (Trunc64to32 <typ.UInt32> (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [32])))
  1148              (Const32 <typ.UInt32> [int32(c)])))
  1149          (Const64 <typ.UInt64> [int64((1<<32)/c)])))
  1150        (ZeroExt32to64
  1151          (Div32u <typ.UInt32>
  1152            (Add32 <typ.UInt32>
  1153              (Mod32u <typ.UInt32> (Trunc64to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(c)]))
  1154              (Mul32 <typ.UInt32>
  1155                (Mod32u <typ.UInt32>
  1156                  (Trunc64to32 <typ.UInt32> (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [32])))
  1157                  (Const32 <typ.UInt32> [int32(c)]))
  1158                (Const32 <typ.UInt32> [int32((1<<32)%c)])))
  1159            (Const32 <typ.UInt32> [int32(c)]))))
  1160  
  1161  // For 64-bit divides on 64-bit machines
  1162  // (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.)
  1163  (Div64u x (Const64 [c])) && umagicOK64(c) && config.RegSize == 8 && umagic64(c).m&1 == 0 && config.useHmul =>
  1164    (Rsh64Ux64 <typ.UInt64>
  1165      (Hmul64u <typ.UInt64>
  1166        (Const64 <typ.UInt64> [int64(1<<63+umagic64(c).m/2)])
  1167        x)
  1168      (Const64 <typ.UInt64> [umagic64(c).s-1]))
  1169  (Div64u x (Const64 [c])) && umagicOK64(c) && config.RegSize == 8 && c&1 == 0 && config.useHmul =>
  1170    (Rsh64Ux64 <typ.UInt64>
  1171      (Hmul64u <typ.UInt64>
  1172        (Const64 <typ.UInt64> [int64(1<<63+(umagic64(c).m+1)/2)])
  1173        (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [1])))
  1174      (Const64 <typ.UInt64> [umagic64(c).s-2]))
  1175  (Div64u x (Const64 [c])) && umagicOK64(c) && config.RegSize == 8 && config.useAvg && config.useHmul =>
  1176    (Rsh64Ux64 <typ.UInt64>
  1177      (Avg64u
  1178        x
  1179        (Hmul64u <typ.UInt64>
  1180          (Const64 <typ.UInt64> [int64(umagic64(c).m)])
  1181          x))
  1182      (Const64 <typ.UInt64> [umagic64(c).s-1]))
  1183  
  1184  // Signed divide by a negative constant.  Rewrite to divide by a positive constant.
  1185  (Div8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  => (Neg8  (Div8  <t> n (Const8  <t> [-c])))
  1186  (Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 => (Neg16 (Div16 <t> n (Const16 <t> [-c])))
  1187  (Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 => (Neg32 (Div32 <t> n (Const32 <t> [-c])))
  1188  (Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 => (Neg64 (Div64 <t> n (Const64 <t> [-c])))
  1189  
  1190  // Dividing by the most-negative number.  Result is always 0 except
  1191  // if the input is also the most-negative number.
  1192  // We can detect that using the sign bit of x & -x.
  1193  (Div8  <t> x (Const8  [-1<<7 ])) => (Rsh8Ux64  (And8  <t> x (Neg8  <t> x)) (Const64 <typ.UInt64> [7 ]))
  1194  (Div16 <t> x (Const16 [-1<<15])) => (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15]))
  1195  (Div32 <t> x (Const32 [-1<<31])) => (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31]))
  1196  (Div64 <t> x (Const64 [-1<<63])) => (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63]))
  1197  
  1198  // Signed divide by power of 2.
  1199  // n / c =       n >> log(c) if n >= 0
  1200  //       = (n+c-1) >> log(c) if n < 0
  1201  // We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned).
  1202  (Div8  <t> n (Const8  [c])) && isPowerOfTwo(c) =>
  1203    (Rsh8x64
  1204      (Add8  <t> n (Rsh8Ux64  <t> (Rsh8x64  <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [int64( 8-log8(c))])))
  1205      (Const64 <typ.UInt64> [int64(log8(c))]))
  1206  (Div16 <t> n (Const16 [c])) && isPowerOfTwo(c) =>
  1207    (Rsh16x64
  1208      (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [int64(16-log16(c))])))
  1209      (Const64 <typ.UInt64> [int64(log16(c))]))
  1210  (Div32 <t> n (Const32 [c])) && isPowerOfTwo(c) =>
  1211    (Rsh32x64
  1212      (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [int64(32-log32(c))])))
  1213      (Const64 <typ.UInt64> [int64(log32(c))]))
  1214  (Div64 <t> n (Const64 [c])) && isPowerOfTwo(c) =>
  1215    (Rsh64x64
  1216      (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [int64(64-log64(c))])))
  1217      (Const64 <typ.UInt64> [int64(log64(c))]))
  1218  
  1219  // Signed divide, not a power of 2.  Strength reduce to a multiply.
  1220  (Div8 <t> x (Const8 [c])) && smagicOK8(c) =>
  1221    (Sub8 <t>
  1222      (Rsh32x64 <t>
  1223        (Mul32 <typ.UInt32>
  1224          (Const32 <typ.UInt32> [int32(smagic8(c).m)])
  1225          (SignExt8to32 x))
  1226        (Const64 <typ.UInt64> [8+smagic8(c).s]))
  1227      (Rsh32x64 <t>
  1228        (SignExt8to32 x)
  1229        (Const64 <typ.UInt64> [31])))
  1230  (Div16 <t> x (Const16 [c])) && smagicOK16(c) =>
  1231    (Sub16 <t>
  1232      (Rsh32x64 <t>
  1233        (Mul32 <typ.UInt32>
  1234          (Const32 <typ.UInt32> [int32(smagic16(c).m)])
  1235          (SignExt16to32 x))
  1236        (Const64 <typ.UInt64> [16+smagic16(c).s]))
  1237      (Rsh32x64 <t>
  1238        (SignExt16to32 x)
  1239        (Const64 <typ.UInt64> [31])))
  1240  (Div32 <t> x (Const32 [c])) && smagicOK32(c) && config.RegSize == 8 =>
  1241    (Sub32 <t>
  1242      (Rsh64x64 <t>
  1243        (Mul64 <typ.UInt64>
  1244          (Const64 <typ.UInt64> [int64(smagic32(c).m)])
  1245          (SignExt32to64 x))
  1246        (Const64 <typ.UInt64> [32+smagic32(c).s]))
  1247      (Rsh64x64 <t>
  1248        (SignExt32to64 x)
  1249        (Const64 <typ.UInt64> [63])))
  1250  (Div32 <t> x (Const32 [c])) && smagicOK32(c) && config.RegSize == 4 && smagic32(c).m&1 == 0 && config.useHmul =>
  1251    (Sub32 <t>
  1252      (Rsh32x64 <t>
  1253        (Hmul32 <t>
  1254          (Const32 <typ.UInt32> [int32(smagic32(c).m/2)])
  1255          x)
  1256        (Const64 <typ.UInt64> [smagic32(c).s-1]))
  1257      (Rsh32x64 <t>
  1258        x
  1259        (Const64 <typ.UInt64> [31])))
  1260  (Div32 <t> x (Const32 [c])) && smagicOK32(c) && config.RegSize == 4 && smagic32(c).m&1 != 0 && config.useHmul =>
  1261    (Sub32 <t>
  1262      (Rsh32x64 <t>
  1263        (Add32 <t>
  1264          (Hmul32 <t>
  1265            (Const32 <typ.UInt32> [int32(smagic32(c).m)])
  1266            x)
  1267          x)
  1268        (Const64 <typ.UInt64> [smagic32(c).s]))
  1269      (Rsh32x64 <t>
  1270        x
  1271        (Const64 <typ.UInt64> [31])))
  1272  (Div64 <t> x (Const64 [c])) && smagicOK64(c) && smagic64(c).m&1 == 0 && config.useHmul =>
  1273    (Sub64 <t>
  1274      (Rsh64x64 <t>
  1275        (Hmul64 <t>
  1276          (Const64 <typ.UInt64> [int64(smagic64(c).m/2)])
  1277          x)
  1278        (Const64 <typ.UInt64> [smagic64(c).s-1]))
  1279      (Rsh64x64 <t>
  1280        x
  1281        (Const64 <typ.UInt64> [63])))
  1282  (Div64 <t> x (Const64 [c])) && smagicOK64(c) && smagic64(c).m&1 != 0 && config.useHmul =>
  1283    (Sub64 <t>
  1284      (Rsh64x64 <t>
  1285        (Add64 <t>
  1286          (Hmul64 <t>
  1287            (Const64 <typ.UInt64> [int64(smagic64(c).m)])
  1288            x)
  1289          x)
  1290        (Const64 <typ.UInt64> [smagic64(c).s]))
  1291      (Rsh64x64 <t>
  1292        x
  1293        (Const64 <typ.UInt64> [63])))
  1294  
  1295  // Unsigned mod by power of 2 constant.
  1296  (Mod8u  <t> n (Const8  [c])) && isPowerOfTwo(c) => (And8  n (Const8  <t> [c-1]))
  1297  (Mod16u <t> n (Const16 [c])) && isPowerOfTwo(c) => (And16 n (Const16 <t> [c-1]))
  1298  (Mod32u <t> n (Const32 [c])) && isPowerOfTwo(c) => (And32 n (Const32 <t> [c-1]))
  1299  (Mod64u <t> n (Const64 [c])) && isPowerOfTwo(c) => (And64 n (Const64 <t> [c-1]))
  1300  (Mod64u <t> n (Const64 [-1<<63]))                 => (And64 n (Const64 <t> [1<<63-1]))
  1301  
  1302  // Signed non-negative mod by power of 2 constant.
  1303  (Mod8  <t> n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo(c) => (And8  n (Const8  <t> [c-1]))
  1304  (Mod16 <t> n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c) => (And16 n (Const16 <t> [c-1]))
  1305  (Mod32 <t> n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c) => (And32 n (Const32 <t> [c-1]))
  1306  (Mod64 <t> n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c) => (And64 n (Const64 <t> [c-1]))
  1307  (Mod64 n (Const64 [-1<<63])) && isNonNegative(n)                   => n
  1308  
  1309  // Signed mod by negative constant.
  1310  (Mod8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  => (Mod8  <t> n (Const8  <t> [-c]))
  1311  (Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 => (Mod16 <t> n (Const16 <t> [-c]))
  1312  (Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 => (Mod32 <t> n (Const32 <t> [-c]))
  1313  (Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 => (Mod64 <t> n (Const64 <t> [-c]))
  1314  
  1315  // All other mods by constants, do A%B = A-(A/B*B).
  1316  // This implements % with two * and a bunch of ancillary ops.
  1317  // One of the * is free if the user's code also computes A/B.
  1318  (Mod8   <t> x (Const8  [c])) && x.Op != OpConst8  && (c > 0 || c == -1<<7)
  1319    => (Sub8  x (Mul8  <t> (Div8   <t> x (Const8  <t> [c])) (Const8  <t> [c])))
  1320  (Mod16  <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15)
  1321    => (Sub16 x (Mul16 <t> (Div16  <t> x (Const16 <t> [c])) (Const16 <t> [c])))
  1322  (Mod32  <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31)
  1323    => (Sub32 x (Mul32 <t> (Div32  <t> x (Const32 <t> [c])) (Const32 <t> [c])))
  1324  (Mod64  <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63)
  1325    => (Sub64 x (Mul64 <t> (Div64  <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1326  (Mod8u  <t> x (Const8  [c])) && x.Op != OpConst8  && c > 0 && umagicOK8( c)
  1327    => (Sub8  x (Mul8  <t> (Div8u  <t> x (Const8  <t> [c])) (Const8  <t> [c])))
  1328  (Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c > 0 && umagicOK16(c)
  1329    => (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c])))
  1330  (Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c > 0 && umagicOK32(c)
  1331    => (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c])))
  1332  (Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c > 0 && umagicOK64(c)
  1333    => (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1334  
  1335  // For architectures without rotates on less than 32-bits, promote these checks to 32-bit.
  1336  (Eq8 (Mod8u x (Const8  [c])) (Const8 [0])) && x.Op != OpConst8 && udivisibleOK8(c) && !hasSmallRotate(config) =>
  1337  	(Eq32 (Mod32u <typ.UInt32> (ZeroExt8to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(uint8(c))])) (Const32 <typ.UInt32> [0]))
  1338  (Eq16 (Mod16u x (Const16  [c])) (Const16 [0])) && x.Op != OpConst16 && udivisibleOK16(c) && !hasSmallRotate(config) =>
  1339  	(Eq32 (Mod32u <typ.UInt32> (ZeroExt16to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(uint16(c))])) (Const32 <typ.UInt32> [0]))
  1340  (Eq8 (Mod8 x (Const8  [c])) (Const8 [0])) && x.Op != OpConst8 && sdivisibleOK8(c) && !hasSmallRotate(config) =>
  1341  	(Eq32 (Mod32 <typ.Int32> (SignExt8to32 <typ.Int32> x) (Const32 <typ.Int32> [int32(c)])) (Const32 <typ.Int32> [0]))
  1342  (Eq16 (Mod16 x (Const16  [c])) (Const16 [0])) && x.Op != OpConst16 && sdivisibleOK16(c) && !hasSmallRotate(config) =>
  1343  	(Eq32 (Mod32 <typ.Int32> (SignExt16to32 <typ.Int32> x) (Const32 <typ.Int32> [int32(c)])) (Const32 <typ.Int32> [0]))
  1344  
  1345  // Divisibility checks x%c == 0 convert to multiply and rotate.
  1346  // Note, x%c == 0 is rewritten as x == c*(x/c) during the opt pass
  1347  // where (x/c) is performed using multiplication with magic constants.
  1348  // To rewrite x%c == 0 requires pattern matching the rewritten expression
  1349  // and checking that the division by the same constant wasn't already calculated.
  1350  // This check is made by counting uses of the magic constant multiplication.
  1351  // Note that if there were an intermediate opt pass, this rule could be applied
  1352  // directly on the Div op and magic division rewrites could be delayed to late opt.
  1353  
  1354  // Unsigned divisibility checks convert to multiply and rotate.
  1355  (Eq8 x (Mul8 (Const8 [c])
  1356    (Trunc32to8
  1357      (Rsh32Ux64
  1358        mul:(Mul32
  1359          (Const32 [m])
  1360          (ZeroExt8to32 x))
  1361        (Const64 [s])))
  1362  	)
  1363  )
  1364    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1365    && m == int32(1<<8+umagic8(c).m) && s == 8+umagic8(c).s
  1366    && x.Op != OpConst8 && udivisibleOK8(c)
  1367   => (Leq8U
  1368  			(RotateLeft8 <typ.UInt8>
  1369  				(Mul8 <typ.UInt8>
  1370  					(Const8 <typ.UInt8> [int8(udivisible8(c).m)])
  1371  					x)
  1372  				(Const8 <typ.UInt8> [int8(8-udivisible8(c).k)])
  1373  				)
  1374  			(Const8 <typ.UInt8> [int8(udivisible8(c).max)])
  1375  		)
  1376  
  1377  (Eq16 x (Mul16 (Const16 [c])
  1378    (Trunc64to16
  1379      (Rsh64Ux64
  1380        mul:(Mul64
  1381          (Const64 [m])
  1382          (ZeroExt16to64 x))
  1383        (Const64 [s])))
  1384  	)
  1385  )
  1386    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1387    && m == int64(1<<16+umagic16(c).m) && s == 16+umagic16(c).s
  1388    && x.Op != OpConst16 && udivisibleOK16(c)
  1389   => (Leq16U
  1390  			(RotateLeft16 <typ.UInt16>
  1391  				(Mul16 <typ.UInt16>
  1392  					(Const16 <typ.UInt16> [int16(udivisible16(c).m)])
  1393  					x)
  1394  				(Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
  1395  				)
  1396  			(Const16 <typ.UInt16> [int16(udivisible16(c).max)])
  1397  		)
  1398  
  1399  (Eq16 x (Mul16 (Const16 [c])
  1400    (Trunc32to16
  1401      (Rsh32Ux64
  1402        mul:(Mul32
  1403          (Const32 [m])
  1404          (ZeroExt16to32 x))
  1405        (Const64 [s])))
  1406  	)
  1407  )
  1408    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1409    && m == int32(1<<15+umagic16(c).m/2) && s == 16+umagic16(c).s-1
  1410    && x.Op != OpConst16 && udivisibleOK16(c)
  1411   => (Leq16U
  1412  			(RotateLeft16 <typ.UInt16>
  1413  				(Mul16 <typ.UInt16>
  1414  					(Const16 <typ.UInt16> [int16(udivisible16(c).m)])
  1415  					x)
  1416  				(Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
  1417  				)
  1418  			(Const16 <typ.UInt16> [int16(udivisible16(c).max)])
  1419  		)
  1420  
  1421  (Eq16 x (Mul16 (Const16 [c])
  1422    (Trunc32to16
  1423      (Rsh32Ux64
  1424        mul:(Mul32
  1425          (Const32 [m])
  1426          (Rsh32Ux64 (ZeroExt16to32 x) (Const64 [1])))
  1427        (Const64 [s])))
  1428  	)
  1429  )
  1430    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1431    && m == int32(1<<15+(umagic16(c).m+1)/2) && s == 16+umagic16(c).s-2
  1432    && x.Op != OpConst16 && udivisibleOK16(c)
  1433   => (Leq16U
  1434  			(RotateLeft16 <typ.UInt16>
  1435  				(Mul16 <typ.UInt16>
  1436  					(Const16 <typ.UInt16> [int16(udivisible16(c).m)])
  1437  					x)
  1438  				(Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
  1439  				)
  1440  			(Const16 <typ.UInt16> [int16(udivisible16(c).max)])
  1441  		)
  1442  
  1443  (Eq16 x (Mul16 (Const16 [c])
  1444    (Trunc32to16
  1445      (Rsh32Ux64
  1446        (Avg32u
  1447          (Lsh32x64 (ZeroExt16to32 x) (Const64 [16]))
  1448          mul:(Mul32
  1449            (Const32 [m])
  1450            (ZeroExt16to32 x)))
  1451        (Const64 [s])))
  1452  	)
  1453  )
  1454    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1455    && m == int32(umagic16(c).m) && s == 16+umagic16(c).s-1
  1456    && x.Op != OpConst16 && udivisibleOK16(c)
  1457   => (Leq16U
  1458  			(RotateLeft16 <typ.UInt16>
  1459  				(Mul16 <typ.UInt16>
  1460  					(Const16 <typ.UInt16> [int16(udivisible16(c).m)])
  1461  					x)
  1462  				(Const16 <typ.UInt16> [int16(16-udivisible16(c).k)])
  1463  				)
  1464  			(Const16 <typ.UInt16> [int16(udivisible16(c).max)])
  1465  		)
  1466  
  1467  (Eq32 x (Mul32 (Const32 [c])
  1468  	(Rsh32Ux64
  1469  		mul:(Hmul32u
  1470  			(Const32 [m])
  1471  			x)
  1472  		(Const64 [s]))
  1473  	)
  1474  )
  1475    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1476    && m == int32(1<<31+umagic32(c).m/2) && s == umagic32(c).s-1
  1477  	&& x.Op != OpConst32 && udivisibleOK32(c)
  1478   => (Leq32U
  1479  			(RotateLeft32 <typ.UInt32>
  1480  				(Mul32 <typ.UInt32>
  1481  					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
  1482  					x)
  1483  				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
  1484  				)
  1485  			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
  1486  		)
  1487  
  1488  (Eq32 x (Mul32 (Const32 [c])
  1489    (Rsh32Ux64
  1490      mul:(Hmul32u
  1491        (Const32 <typ.UInt32> [m])
  1492        (Rsh32Ux64 x (Const64 [1])))
  1493      (Const64 [s]))
  1494  	)
  1495  )
  1496    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1497    && m == int32(1<<31+(umagic32(c).m+1)/2) && s == umagic32(c).s-2
  1498  	&& x.Op != OpConst32 && udivisibleOK32(c)
  1499   => (Leq32U
  1500  			(RotateLeft32 <typ.UInt32>
  1501  				(Mul32 <typ.UInt32>
  1502  					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
  1503  					x)
  1504  				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
  1505  				)
  1506  			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
  1507  		)
  1508  
  1509  (Eq32 x (Mul32 (Const32 [c])
  1510    (Rsh32Ux64
  1511      (Avg32u
  1512        x
  1513        mul:(Hmul32u
  1514          (Const32 [m])
  1515          x))
  1516      (Const64 [s]))
  1517  	)
  1518  )
  1519    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1520    && m == int32(umagic32(c).m) && s == umagic32(c).s-1
  1521  	&& x.Op != OpConst32 && udivisibleOK32(c)
  1522   => (Leq32U
  1523  			(RotateLeft32 <typ.UInt32>
  1524  				(Mul32 <typ.UInt32>
  1525  					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
  1526  					x)
  1527  				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
  1528  				)
  1529  			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
  1530  		)
  1531  
  1532  (Eq32 x (Mul32 (Const32 [c])
  1533    (Trunc64to32
  1534      (Rsh64Ux64
  1535        mul:(Mul64
  1536          (Const64 [m])
  1537          (ZeroExt32to64 x))
  1538        (Const64 [s])))
  1539  	)
  1540  )
  1541    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1542    && m == int64(1<<31+umagic32(c).m/2) && s == 32+umagic32(c).s-1
  1543  	&& x.Op != OpConst32 && udivisibleOK32(c)
  1544   => (Leq32U
  1545  			(RotateLeft32 <typ.UInt32>
  1546  				(Mul32 <typ.UInt32>
  1547  					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
  1548  					x)
  1549  				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
  1550  				)
  1551  			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
  1552  		)
  1553  
  1554  (Eq32 x (Mul32 (Const32 [c])
  1555    (Trunc64to32
  1556      (Rsh64Ux64
  1557        mul:(Mul64
  1558          (Const64 [m])
  1559          (Rsh64Ux64 (ZeroExt32to64 x) (Const64 [1])))
  1560        (Const64 [s])))
  1561  	)
  1562  )
  1563    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1564    && m == int64(1<<31+(umagic32(c).m+1)/2) && s == 32+umagic32(c).s-2
  1565  	&& x.Op != OpConst32 && udivisibleOK32(c)
  1566   => (Leq32U
  1567  			(RotateLeft32 <typ.UInt32>
  1568  				(Mul32 <typ.UInt32>
  1569  					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
  1570  					x)
  1571  				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
  1572  				)
  1573  			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
  1574  		)
  1575  
  1576  (Eq32 x (Mul32 (Const32 [c])
  1577    (Trunc64to32
  1578      (Rsh64Ux64
  1579        (Avg64u
  1580          (Lsh64x64 (ZeroExt32to64 x) (Const64 [32]))
  1581          mul:(Mul64
  1582            (Const64 [m])
  1583            (ZeroExt32to64 x)))
  1584        (Const64 [s])))
  1585  	)
  1586  )
  1587    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1588    && m == int64(umagic32(c).m) && s == 32+umagic32(c).s-1
  1589  	&& x.Op != OpConst32 && udivisibleOK32(c)
  1590   => (Leq32U
  1591  			(RotateLeft32 <typ.UInt32>
  1592  				(Mul32 <typ.UInt32>
  1593  					(Const32 <typ.UInt32> [int32(udivisible32(c).m)])
  1594  					x)
  1595  				(Const32 <typ.UInt32> [int32(32-udivisible32(c).k)])
  1596  				)
  1597  			(Const32 <typ.UInt32> [int32(udivisible32(c).max)])
  1598  		)
  1599  
  1600  (Eq64 x (Mul64 (Const64 [c])
  1601  	(Rsh64Ux64
  1602  		mul:(Hmul64u
  1603  			(Const64 [m])
  1604  			x)
  1605  		(Const64 [s]))
  1606  	)
  1607  ) && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1608    && m == int64(1<<63+umagic64(c).m/2) && s == umagic64(c).s-1
  1609    && x.Op != OpConst64 && udivisibleOK64(c)
  1610   => (Leq64U
  1611  			(RotateLeft64 <typ.UInt64>
  1612  				(Mul64 <typ.UInt64>
  1613  					(Const64 <typ.UInt64> [int64(udivisible64(c).m)])
  1614  					x)
  1615  				(Const64 <typ.UInt64> [64-udivisible64(c).k])
  1616  				)
  1617  			(Const64 <typ.UInt64> [int64(udivisible64(c).max)])
  1618  		)
  1619  (Eq64 x (Mul64 (Const64 [c])
  1620  	(Rsh64Ux64
  1621  		mul:(Hmul64u
  1622  			(Const64 [m])
  1623  			(Rsh64Ux64 x (Const64 [1])))
  1624  		(Const64 [s]))
  1625  	)
  1626  ) && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1627    && m == int64(1<<63+(umagic64(c).m+1)/2) && s == umagic64(c).s-2
  1628    && x.Op != OpConst64 && udivisibleOK64(c)
  1629   => (Leq64U
  1630  			(RotateLeft64 <typ.UInt64>
  1631  				(Mul64 <typ.UInt64>
  1632  					(Const64 <typ.UInt64> [int64(udivisible64(c).m)])
  1633  					x)
  1634  				(Const64 <typ.UInt64> [64-udivisible64(c).k])
  1635  				)
  1636  			(Const64 <typ.UInt64> [int64(udivisible64(c).max)])
  1637  		)
  1638  (Eq64 x (Mul64 (Const64 [c])
  1639  	(Rsh64Ux64
  1640  		(Avg64u
  1641  			x
  1642  			mul:(Hmul64u
  1643  				(Const64 [m])
  1644  				x))
  1645  		(Const64 [s]))
  1646  	)
  1647  ) && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1648    && m == int64(umagic64(c).m) && s == umagic64(c).s-1
  1649    && x.Op != OpConst64 && udivisibleOK64(c)
  1650   => (Leq64U
  1651  			(RotateLeft64 <typ.UInt64>
  1652  				(Mul64 <typ.UInt64>
  1653  					(Const64 <typ.UInt64> [int64(udivisible64(c).m)])
  1654  					x)
  1655  				(Const64 <typ.UInt64> [64-udivisible64(c).k])
  1656  				)
  1657  			(Const64 <typ.UInt64> [int64(udivisible64(c).max)])
  1658  		)
  1659  
  1660  // Signed divisibility checks convert to multiply, add and rotate.
  1661  (Eq8 x (Mul8 (Const8 [c])
  1662    (Sub8
  1663      (Rsh32x64
  1664        mul:(Mul32
  1665          (Const32 [m])
  1666          (SignExt8to32 x))
  1667        (Const64 [s]))
  1668      (Rsh32x64
  1669        (SignExt8to32 x)
  1670        (Const64 [31])))
  1671  	)
  1672  )
  1673    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1674    && m == int32(smagic8(c).m) && s == 8+smagic8(c).s
  1675  	&& x.Op != OpConst8 && sdivisibleOK8(c)
  1676   => (Leq8U
  1677  			(RotateLeft8 <typ.UInt8>
  1678  				(Add8 <typ.UInt8>
  1679  					(Mul8 <typ.UInt8>
  1680  						(Const8 <typ.UInt8> [int8(sdivisible8(c).m)])
  1681  						x)
  1682  					(Const8 <typ.UInt8> [int8(sdivisible8(c).a)])
  1683  				)
  1684  				(Const8 <typ.UInt8> [int8(8-sdivisible8(c).k)])
  1685  			)
  1686  			(Const8 <typ.UInt8> [int8(sdivisible8(c).max)])
  1687  		)
  1688  
  1689  (Eq16 x (Mul16 (Const16 [c])
  1690    (Sub16
  1691      (Rsh32x64
  1692        mul:(Mul32
  1693          (Const32 [m])
  1694          (SignExt16to32 x))
  1695        (Const64 [s]))
  1696      (Rsh32x64
  1697        (SignExt16to32 x)
  1698        (Const64 [31])))
  1699  	)
  1700  )
  1701    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1702    && m == int32(smagic16(c).m) && s == 16+smagic16(c).s
  1703  	&& x.Op != OpConst16 && sdivisibleOK16(c)
  1704   => (Leq16U
  1705  			(RotateLeft16 <typ.UInt16>
  1706  				(Add16 <typ.UInt16>
  1707  					(Mul16 <typ.UInt16>
  1708  						(Const16 <typ.UInt16> [int16(sdivisible16(c).m)])
  1709  						x)
  1710  					(Const16 <typ.UInt16> [int16(sdivisible16(c).a)])
  1711  				)
  1712  				(Const16 <typ.UInt16> [int16(16-sdivisible16(c).k)])
  1713  			)
  1714  			(Const16 <typ.UInt16> [int16(sdivisible16(c).max)])
  1715  		)
  1716  
  1717  (Eq32 x (Mul32 (Const32 [c])
  1718    (Sub32
  1719      (Rsh64x64
  1720        mul:(Mul64
  1721          (Const64 [m])
  1722          (SignExt32to64 x))
  1723        (Const64 [s]))
  1724      (Rsh64x64
  1725        (SignExt32to64 x)
  1726        (Const64 [63])))
  1727  	)
  1728  )
  1729    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1730    && m == int64(smagic32(c).m) && s == 32+smagic32(c).s
  1731  	&& x.Op != OpConst32 && sdivisibleOK32(c)
  1732   => (Leq32U
  1733  			(RotateLeft32 <typ.UInt32>
  1734  				(Add32 <typ.UInt32>
  1735  					(Mul32 <typ.UInt32>
  1736  						(Const32 <typ.UInt32> [int32(sdivisible32(c).m)])
  1737  						x)
  1738  					(Const32 <typ.UInt32> [int32(sdivisible32(c).a)])
  1739  				)
  1740  				(Const32 <typ.UInt32> [int32(32-sdivisible32(c).k)])
  1741  			)
  1742  			(Const32 <typ.UInt32> [int32(sdivisible32(c).max)])
  1743  		)
  1744  
  1745  (Eq32 x (Mul32 (Const32 [c])
  1746    (Sub32
  1747      (Rsh32x64
  1748        mul:(Hmul32
  1749          (Const32 [m])
  1750          x)
  1751        (Const64 [s]))
  1752      (Rsh32x64
  1753        x
  1754        (Const64 [31])))
  1755  	)
  1756  )
  1757    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1758    && m == int32(smagic32(c).m/2) && s == smagic32(c).s-1
  1759  	&& x.Op != OpConst32 && sdivisibleOK32(c)
  1760   => (Leq32U
  1761  			(RotateLeft32 <typ.UInt32>
  1762  				(Add32 <typ.UInt32>
  1763  					(Mul32 <typ.UInt32>
  1764  						(Const32 <typ.UInt32> [int32(sdivisible32(c).m)])
  1765  						x)
  1766  					(Const32 <typ.UInt32> [int32(sdivisible32(c).a)])
  1767  				)
  1768  				(Const32 <typ.UInt32> [int32(32-sdivisible32(c).k)])
  1769  			)
  1770  			(Const32 <typ.UInt32> [int32(sdivisible32(c).max)])
  1771  		)
  1772  
  1773  (Eq32 x (Mul32 (Const32 [c])
  1774    (Sub32
  1775      (Rsh32x64
  1776        (Add32
  1777          mul:(Hmul32
  1778            (Const32 [m])
  1779            x)
  1780          x)
  1781        (Const64 [s]))
  1782      (Rsh32x64
  1783        x
  1784        (Const64 [31])))
  1785  	)
  1786  )
  1787    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1788    && m == int32(smagic32(c).m) && s == smagic32(c).s
  1789  	&& x.Op != OpConst32 && sdivisibleOK32(c)
  1790   => (Leq32U
  1791  			(RotateLeft32 <typ.UInt32>
  1792  				(Add32 <typ.UInt32>
  1793  					(Mul32 <typ.UInt32>
  1794  						(Const32 <typ.UInt32> [int32(sdivisible32(c).m)])
  1795  						x)
  1796  					(Const32 <typ.UInt32> [int32(sdivisible32(c).a)])
  1797  				)
  1798  				(Const32 <typ.UInt32> [int32(32-sdivisible32(c).k)])
  1799  			)
  1800  			(Const32 <typ.UInt32> [int32(sdivisible32(c).max)])
  1801  		)
  1802  
  1803  (Eq64 x (Mul64 (Const64 [c])
  1804    (Sub64
  1805      (Rsh64x64
  1806        mul:(Hmul64
  1807          (Const64 [m])
  1808          x)
  1809        (Const64 [s]))
  1810      (Rsh64x64
  1811        x
  1812        (Const64 [63])))
  1813  	)
  1814  )
  1815    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1816    && m == int64(smagic64(c).m/2) && s == smagic64(c).s-1
  1817  	&& x.Op != OpConst64 && sdivisibleOK64(c)
  1818   => (Leq64U
  1819  			(RotateLeft64 <typ.UInt64>
  1820  				(Add64 <typ.UInt64>
  1821  					(Mul64 <typ.UInt64>
  1822  						(Const64 <typ.UInt64> [int64(sdivisible64(c).m)])
  1823  						x)
  1824  					(Const64 <typ.UInt64> [int64(sdivisible64(c).a)])
  1825  				)
  1826  				(Const64 <typ.UInt64> [64-sdivisible64(c).k])
  1827  			)
  1828  			(Const64 <typ.UInt64> [int64(sdivisible64(c).max)])
  1829  		)
  1830  
  1831  (Eq64 x (Mul64 (Const64 [c])
  1832    (Sub64
  1833      (Rsh64x64
  1834        (Add64
  1835          mul:(Hmul64
  1836            (Const64 [m])
  1837            x)
  1838          x)
  1839        (Const64 [s]))
  1840      (Rsh64x64
  1841        x
  1842        (Const64 [63])))
  1843  	)
  1844  )
  1845    && v.Block.Func.pass.name != "opt" && mul.Uses == 1
  1846    && m == int64(smagic64(c).m) && s == smagic64(c).s
  1847  	&& x.Op != OpConst64 && sdivisibleOK64(c)
  1848   => (Leq64U
  1849  			(RotateLeft64 <typ.UInt64>
  1850  				(Add64 <typ.UInt64>
  1851  					(Mul64 <typ.UInt64>
  1852  						(Const64 <typ.UInt64> [int64(sdivisible64(c).m)])
  1853  						x)
  1854  					(Const64 <typ.UInt64> [int64(sdivisible64(c).a)])
  1855  				)
  1856  				(Const64 <typ.UInt64> [64-sdivisible64(c).k])
  1857  			)
  1858  			(Const64 <typ.UInt64> [int64(sdivisible64(c).max)])
  1859  		)
  1860  
  1861  // Divisibility check for signed integers for power of two constant are simple mask.
  1862  // However, we must match against the rewritten n%c == 0 -> n - c*(n/c) == 0 -> n == c*(n/c)
  1863  // where n/c contains fixup code to handle signed n.
  1864  ((Eq8|Neq8) n (Lsh8x64
  1865    (Rsh8x64
  1866      (Add8  <t> n (Rsh8Ux64  <t> (Rsh8x64  <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [kbar])))
  1867      (Const64 <typ.UInt64> [k]))
  1868  	(Const64 <typ.UInt64> [k]))
  1869  ) && k > 0 && k < 7 && kbar == 8 - k
  1870    => ((Eq8|Neq8) (And8 <t> n (Const8 <t> [1<<uint(k)-1])) (Const8 <t> [0]))
  1871  
  1872  ((Eq16|Neq16) n (Lsh16x64
  1873    (Rsh16x64
  1874      (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [kbar])))
  1875      (Const64 <typ.UInt64> [k]))
  1876  	(Const64 <typ.UInt64> [k]))
  1877  ) && k > 0 && k < 15 && kbar == 16 - k
  1878    => ((Eq16|Neq16) (And16 <t> n (Const16 <t> [1<<uint(k)-1])) (Const16 <t> [0]))
  1879  
  1880  ((Eq32|Neq32) n (Lsh32x64
  1881    (Rsh32x64
  1882      (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [kbar])))
  1883      (Const64 <typ.UInt64> [k]))
  1884  	(Const64 <typ.UInt64> [k]))
  1885  ) && k > 0 && k < 31 && kbar == 32 - k
  1886    => ((Eq32|Neq32) (And32 <t> n (Const32 <t> [1<<uint(k)-1])) (Const32 <t> [0]))
  1887  
  1888  ((Eq64|Neq64) n (Lsh64x64
  1889    (Rsh64x64
  1890      (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [kbar])))
  1891      (Const64 <typ.UInt64> [k]))
  1892  	(Const64 <typ.UInt64> [k]))
  1893  ) && k > 0 && k < 63 && kbar == 64 - k
  1894    => ((Eq64|Neq64) (And64 <t> n (Const64 <t> [1<<uint(k)-1])) (Const64 <t> [0]))
  1895  
  1896  (Eq(8|16|32|64)  s:(Sub(8|16|32|64) x y) (Const(8|16|32|64) [0])) && s.Uses == 1 => (Eq(8|16|32|64)  x y)
  1897  (Neq(8|16|32|64) s:(Sub(8|16|32|64) x y) (Const(8|16|32|64) [0])) && s.Uses == 1 => (Neq(8|16|32|64) x y)
  1898  
  1899  // Optimize bitsets
  1900  (Eq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [y])) && oneBit8(y)
  1901    => (Neq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [0]))
  1902  (Eq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [y])) && oneBit16(y)
  1903    => (Neq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [0]))
  1904  (Eq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [y])) && oneBit32(y)
  1905    => (Neq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [0]))
  1906  (Eq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [y])) && oneBit64(y)
  1907    => (Neq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [0]))
  1908  (Neq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [y])) && oneBit8(y)
  1909    => (Eq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [0]))
  1910  (Neq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [y])) && oneBit16(y)
  1911    => (Eq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [0]))
  1912  (Neq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [y])) && oneBit32(y)
  1913    => (Eq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [0]))
  1914  (Neq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [y])) && oneBit64(y)
  1915    => (Eq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [0]))
  1916  
  1917  // Reassociate expressions involving
  1918  // constants such that constants come first,
  1919  // exposing obvious constant-folding opportunities.
  1920  // Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C
  1921  // is constant, which pushes constants to the outside
  1922  // of the expression. At that point, any constant-folding
  1923  // opportunities should be obvious.
  1924  // Note: don't include AddPtr here! In order to maintain the
  1925  // invariant that pointers must stay within the pointed-to object,
  1926  // we can't pull part of a pointer computation above the AddPtr.
  1927  // See issue 37881.
  1928  // Note: we don't need to handle any (x-C) cases because we already rewrite
  1929  // (x-C) to (x+(-C)).
  1930  
  1931  // x + (C + z) -> C + (x + z)
  1932  (Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Add64 <t> z x))
  1933  (Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Add32 <t> z x))
  1934  (Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Add16 <t> z x))
  1935  (Add8  (Add8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Add8  i (Add8  <t> z x))
  1936  
  1937  // x + (C - z) -> C + (x - z)
  1938  (Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Sub64 <t> x z))
  1939  (Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Sub32 <t> x z))
  1940  (Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Sub16 <t> x z))
  1941  (Add8  (Sub8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Add8  i (Sub8  <t> x z))
  1942  
  1943  // x - (C - z) -> x + (z - C) -> (x + z) - C
  1944  (Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 (Add64 <t> x z) i)
  1945  (Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 (Add32 <t> x z) i)
  1946  (Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 (Add16 <t> x z) i)
  1947  (Sub8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Sub8  (Add8  <t> x z) i)
  1948  
  1949  // x - (z + C) -> x + (-z - C) -> (x - z) - C
  1950  (Sub64 x (Add64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 (Sub64 <t> x z) i)
  1951  (Sub32 x (Add32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 (Sub32 <t> x z) i)
  1952  (Sub16 x (Add16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 (Sub16 <t> x z) i)
  1953  (Sub8  x (Add8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Sub8 (Sub8  <t> x z) i)
  1954  
  1955  // (C - z) - x -> C - (z + x)
  1956  (Sub64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 i (Add64 <t> z x))
  1957  (Sub32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 i (Add32 <t> z x))
  1958  (Sub16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 i (Add16 <t> z x))
  1959  (Sub8  (Sub8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Sub8  i (Add8  <t> z x))
  1960  
  1961  // (z + C) -x -> C + (z - x)
  1962  (Sub64 (Add64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Sub64 <t> z x))
  1963  (Sub32 (Add32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Sub32 <t> z x))
  1964  (Sub16 (Add16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Sub16 <t> z x))
  1965  (Sub8  (Add8  z i:(Const8  <t>)) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Add8  i (Sub8  <t> z x))
  1966  
  1967  // x & (C & z) -> C & (x & z)
  1968  (And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (And64 i (And64 <t> z x))
  1969  (And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (And32 i (And32 <t> z x))
  1970  (And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (And16 i (And16 <t> z x))
  1971  (And8  (And8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (And8  i (And8  <t> z x))
  1972  
  1973  // x | (C | z) -> C | (x | z)
  1974  (Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Or64 i (Or64 <t> z x))
  1975  (Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Or32 i (Or32 <t> z x))
  1976  (Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Or16 i (Or16 <t> z x))
  1977  (Or8  (Or8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Or8  i (Or8  <t> z x))
  1978  
  1979  // x ^ (C ^ z) -> C ^ (x ^ z)
  1980  (Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Xor64 i (Xor64 <t> z x))
  1981  (Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Xor32 i (Xor32 <t> z x))
  1982  (Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Xor16 i (Xor16 <t> z x))
  1983  (Xor8  (Xor8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Xor8  i (Xor8  <t> z x))
  1984  
  1985  // x * (D * z) = D * (x * z)
  1986  (Mul64 (Mul64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Mul64 i (Mul64 <t> x z))
  1987  (Mul32 (Mul32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Mul32 i (Mul32 <t> x z))
  1988  (Mul16 (Mul16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Mul16 i (Mul16 <t> x z))
  1989  (Mul8  (Mul8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Mul8  i (Mul8  <t> x z))
  1990  
  1991  // C + (D + x) -> (C + D) + x
  1992  (Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Add64 (Const64 <t> [c+d]) x)
  1993  (Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Add32 (Const32 <t> [c+d]) x)
  1994  (Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Add16 (Const16 <t> [c+d]) x)
  1995  (Add8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Add8  (Const8  <t> [c+d]) x)
  1996  
  1997  // C + (D - x) -> (C + D) - x
  1998  (Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) => (Sub64 (Const64 <t> [c+d]) x)
  1999  (Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) => (Sub32 (Const32 <t> [c+d]) x)
  2000  (Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) => (Sub16 (Const16 <t> [c+d]) x)
  2001  (Add8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) => (Sub8  (Const8  <t> [c+d]) x)
  2002  
  2003  // C - (D - x) -> (C - D) + x
  2004  (Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) => (Add64 (Const64 <t> [c-d]) x)
  2005  (Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) => (Add32 (Const32 <t> [c-d]) x)
  2006  (Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) => (Add16 (Const16 <t> [c-d]) x)
  2007  (Sub8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) => (Add8  (Const8  <t> [c-d]) x)
  2008  
  2009  // C - (D + x) -> (C - D) - x
  2010  (Sub64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Sub64 (Const64 <t> [c-d]) x)
  2011  (Sub32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Sub32 (Const32 <t> [c-d]) x)
  2012  (Sub16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Sub16 (Const16 <t> [c-d]) x)
  2013  (Sub8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Sub8  (Const8  <t> [c-d]) x)
  2014  
  2015  // C & (D & x) -> (C & D) & x
  2016  (And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) => (And64 (Const64 <t> [c&d]) x)
  2017  (And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) => (And32 (Const32 <t> [c&d]) x)
  2018  (And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) => (And16 (Const16 <t> [c&d]) x)
  2019  (And8  (Const8  <t> [c]) (And8  (Const8  <t> [d]) x)) => (And8  (Const8  <t> [c&d]) x)
  2020  
  2021  // C | (D | x) -> (C | D) | x
  2022  (Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) => (Or64 (Const64 <t> [c|d]) x)
  2023  (Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) => (Or32 (Const32 <t> [c|d]) x)
  2024  (Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) => (Or16 (Const16 <t> [c|d]) x)
  2025  (Or8  (Const8  <t> [c]) (Or8  (Const8  <t> [d]) x)) => (Or8  (Const8  <t> [c|d]) x)
  2026  
  2027  // C ^ (D ^ x) -> (C ^ D) ^ x
  2028  (Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) => (Xor64 (Const64 <t> [c^d]) x)
  2029  (Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) => (Xor32 (Const32 <t> [c^d]) x)
  2030  (Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) => (Xor16 (Const16 <t> [c^d]) x)
  2031  (Xor8  (Const8  <t> [c]) (Xor8  (Const8  <t> [d]) x)) => (Xor8  (Const8  <t> [c^d]) x)
  2032  
  2033  // C * (D * x) = (C * D) * x
  2034  (Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) => (Mul64 (Const64 <t> [c*d]) x)
  2035  (Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) => (Mul32 (Const32 <t> [c*d]) x)
  2036  (Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) => (Mul16 (Const16 <t> [c*d]) x)
  2037  (Mul8  (Const8  <t> [c]) (Mul8  (Const8  <t> [d]) x)) => (Mul8  (Const8  <t> [c*d]) x)
  2038  
  2039  // floating point optimizations
  2040  (Mul(32|64)F x (Const(32|64)F [1])) => x
  2041  (Mul32F x (Const32F [-1])) => (Neg32F x)
  2042  (Mul64F x (Const64F [-1])) => (Neg64F x)
  2043  (Mul32F x (Const32F [2])) => (Add32F x x)
  2044  (Mul64F x (Const64F [2])) => (Add64F x x)
  2045  
  2046  (Div32F x (Const32F <t> [c])) && reciprocalExact32(c) => (Mul32F x (Const32F <t> [1/c]))
  2047  (Div64F x (Const64F <t> [c])) && reciprocalExact64(c) => (Mul64F x (Const64F <t> [1/c]))
  2048  
  2049  // rewrite single-precision sqrt expression "float32(math.Sqrt(float64(x)))"
  2050  (Cvt64Fto32F sqrt0:(Sqrt (Cvt32Fto64F x))) && sqrt0.Uses==1 => (Sqrt32 x)
  2051  
  2052  (Sqrt (Const64F [c])) && !math.IsNaN(math.Sqrt(c)) => (Const64F [math.Sqrt(c)])
  2053  
  2054  // for rewriting constant folded math/bits ops
  2055  (Select0 (MakeTuple x y)) => x
  2056  (Select1 (MakeTuple x y)) => y
  2057  
  2058  // for rewriting results of some late-expanded rewrites (below)
  2059  (SelectN [0] (MakeResult x ___)) => x
  2060  (SelectN [1] (MakeResult x y ___)) => y
  2061  (SelectN [2] (MakeResult x y z ___)) => z
  2062  
  2063  // for late-expanded calls, recognize newobject and remove zeroing and nilchecks
  2064  (Zero (SelectN [0] call:(StaticLECall _ _)) mem:(SelectN [1] call))
  2065  	&& isSameCall(call.Aux, "runtime.newobject")
  2066  	=> mem
  2067  
  2068  (Store (SelectN [0] call:(StaticLECall _ _)) x mem:(SelectN [1] call))
  2069  	&& isConstZero(x)
  2070  	&& isSameCall(call.Aux, "runtime.newobject")
  2071  	=> mem
  2072  
  2073  (Store (OffPtr (SelectN [0] call:(StaticLECall _ _))) x mem:(SelectN [1] call))
  2074  	&& isConstZero(x)
  2075  	&& isSameCall(call.Aux, "runtime.newobject")
  2076  	=> mem
  2077  
  2078  (NilCheck ptr:(SelectN [0] call:(StaticLECall _ _)) _)
  2079  	&& isSameCall(call.Aux, "runtime.newobject")
  2080  	&& warnRule(fe.Debug_checknil(), v, "removed nil check")
  2081  	=> ptr
  2082  
  2083  (NilCheck ptr:(OffPtr (SelectN [0] call:(StaticLECall _ _))) _)
  2084  	&& isSameCall(call.Aux, "runtime.newobject")
  2085  	&& warnRule(fe.Debug_checknil(), v, "removed nil check")
  2086  	=> ptr
  2087  
  2088  // Addresses of globals are always non-nil.
  2089  (NilCheck          ptr:(Addr {_} (SB))    _) => ptr
  2090  (NilCheck ptr:(Convert (Addr {_} (SB)) _) _) => ptr
  2091  
  2092  // Addresses of locals are always non-nil.
  2093  (NilCheck ptr:(LocalAddr _ _) _)
  2094  	&& warnRule(fe.Debug_checknil(), v, "removed nil check")
  2095  	=> ptr
  2096  
  2097  // Nil checks of nil checks are redundant.
  2098  // See comment at the end of https://go-review.googlesource.com/c/go/+/537775.
  2099  (NilCheck ptr:(NilCheck _ _) _ ) => ptr
  2100  
  2101  // for late-expanded calls, recognize memequal applied to a single constant byte
  2102  // Support is limited by 1, 2, 4, 8 byte sizes
  2103  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [1]) mem)
  2104    && isSameCall(callAux, "runtime.memequal")
  2105    && symIsRO(scon)
  2106    => (MakeResult (Eq8 (Load <typ.Int8> sptr mem) (Const8 <typ.Int8> [int8(read8(scon,0))])) mem)
  2107  
  2108  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [1]) mem)
  2109    && isSameCall(callAux, "runtime.memequal")
  2110    && symIsRO(scon)
  2111    => (MakeResult (Eq8 (Load <typ.Int8> sptr mem) (Const8 <typ.Int8> [int8(read8(scon,0))])) mem)
  2112  
  2113  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [2]) mem)
  2114    && isSameCall(callAux, "runtime.memequal")
  2115    && symIsRO(scon)
  2116    && canLoadUnaligned(config)
  2117    => (MakeResult (Eq16 (Load <typ.Int16> sptr mem) (Const16 <typ.Int16> [int16(read16(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  2118  
  2119  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [2]) mem)
  2120    && isSameCall(callAux, "runtime.memequal")
  2121    && symIsRO(scon)
  2122    && canLoadUnaligned(config)
  2123    => (MakeResult (Eq16 (Load <typ.Int16> sptr mem) (Const16 <typ.Int16> [int16(read16(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  2124  
  2125  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [4]) mem)
  2126    && isSameCall(callAux, "runtime.memequal")
  2127    && symIsRO(scon)
  2128    && canLoadUnaligned(config)
  2129    => (MakeResult (Eq32 (Load <typ.Int32> sptr mem) (Const32 <typ.Int32> [int32(read32(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  2130  
  2131  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [4]) mem)
  2132    && isSameCall(callAux, "runtime.memequal")
  2133    && symIsRO(scon)
  2134    && canLoadUnaligned(config)
  2135    => (MakeResult (Eq32 (Load <typ.Int32> sptr mem) (Const32 <typ.Int32> [int32(read32(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  2136  
  2137  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [8]) mem)
  2138    && isSameCall(callAux, "runtime.memequal")
  2139    && symIsRO(scon)
  2140    && canLoadUnaligned(config) && config.PtrSize == 8
  2141    => (MakeResult (Eq64 (Load <typ.Int64> sptr mem) (Const64 <typ.Int64> [int64(read64(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  2142  
  2143  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [8]) mem)
  2144    && isSameCall(callAux, "runtime.memequal")
  2145    && symIsRO(scon)
  2146    && canLoadUnaligned(config) && config.PtrSize == 8
  2147    => (MakeResult (Eq64 (Load <typ.Int64> sptr mem) (Const64 <typ.Int64> [int64(read64(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  2148  
  2149  (StaticLECall {callAux} _ _ (Const64 [0]) mem)
  2150    && isSameCall(callAux, "runtime.memequal")
  2151    => (MakeResult (ConstBool <typ.Bool> [true]) mem)
  2152  
  2153  (Static(Call|LECall) {callAux} p q _ mem)
  2154    && isSameCall(callAux, "runtime.memequal")
  2155    && isSamePtr(p, q)
  2156    => (MakeResult (ConstBool <typ.Bool> [true]) mem)
  2157  
  2158  // Turn known-size calls to memclrNoHeapPointers into a Zero.
  2159  // Note that we are using types.Types[types.TUINT8] instead of sptr.Type.Elem() - see issue 55122 and CL 431496 for more details.
  2160  (SelectN [0] call:(StaticCall {sym} sptr (Const(64|32) [c]) mem))
  2161    && isInlinableMemclr(config, int64(c))
  2162    && isSameCall(sym, "runtime.memclrNoHeapPointers")
  2163    && call.Uses == 1
  2164    && clobber(call)
  2165    => (Zero {types.Types[types.TUINT8]} [int64(c)] sptr mem)
  2166  
  2167  // Recognise make([]T, 0) and replace it with a pointer to the zerobase
  2168  (StaticLECall {callAux} _ (Const(64|32) [0]) (Const(64|32) [0]) mem)
  2169  	&& isSameCall(callAux, "runtime.makeslice")
  2170  	=> (MakeResult (Addr <v.Type.FieldType(0)> {ir.Syms.Zerobase} (SB)) mem)
  2171  
  2172  // Evaluate constant address comparisons.
  2173  (EqPtr  x x) => (ConstBool [true])
  2174  (NeqPtr x x) => (ConstBool [false])
  2175  (EqPtr  (Addr {x} _) (Addr {y} _)) => (ConstBool [x == y])
  2176  (EqPtr  (Addr {x} _) (OffPtr [o] (Addr {y} _))) => (ConstBool [x == y && o == 0])
  2177  (EqPtr  (OffPtr [o1] (Addr {x} _)) (OffPtr [o2] (Addr {y} _))) => (ConstBool [x == y && o1 == o2])
  2178  (NeqPtr (Addr {x} _) (Addr {y} _)) => (ConstBool [x != y])
  2179  (NeqPtr (Addr {x} _) (OffPtr [o] (Addr {y} _))) => (ConstBool [x != y || o != 0])
  2180  (NeqPtr (OffPtr [o1] (Addr {x} _)) (OffPtr [o2] (Addr {y} _))) => (ConstBool [x != y || o1 != o2])
  2181  (EqPtr  (LocalAddr {x} _ _) (LocalAddr {y} _ _)) => (ConstBool [x == y])
  2182  (EqPtr  (LocalAddr {x} _ _) (OffPtr [o] (LocalAddr {y} _ _))) => (ConstBool [x == y && o == 0])
  2183  (EqPtr  (OffPtr [o1] (LocalAddr {x} _ _)) (OffPtr [o2] (LocalAddr {y} _ _))) => (ConstBool [x == y && o1 == o2])
  2184  (NeqPtr (LocalAddr {x} _ _) (LocalAddr {y} _ _)) => (ConstBool [x != y])
  2185  (NeqPtr (LocalAddr {x} _ _) (OffPtr [o] (LocalAddr {y} _ _))) => (ConstBool [x != y || o != 0])
  2186  (NeqPtr (OffPtr [o1] (LocalAddr {x} _ _)) (OffPtr [o2] (LocalAddr {y} _ _))) => (ConstBool [x != y || o1 != o2])
  2187  (EqPtr  (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) => (ConstBool [o1 == 0])
  2188  (NeqPtr (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) => (ConstBool [o1 != 0])
  2189  (EqPtr  (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) => (ConstBool [o1 == o2])
  2190  (NeqPtr (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) => (ConstBool [o1 != o2])
  2191  (EqPtr  (Const(32|64) [c]) (Const(32|64) [d])) => (ConstBool [c == d])
  2192  (NeqPtr (Const(32|64) [c]) (Const(32|64) [d])) => (ConstBool [c != d])
  2193  (EqPtr  (Convert (Addr {x} _) _) (Addr {y} _)) => (ConstBool [x==y])
  2194  (NeqPtr (Convert (Addr {x} _) _) (Addr {y} _)) => (ConstBool [x!=y])
  2195  
  2196  (EqPtr  (LocalAddr _ _) (Addr _)) => (ConstBool [false])
  2197  (EqPtr  (OffPtr (LocalAddr _ _)) (Addr _)) => (ConstBool [false])
  2198  (EqPtr  (LocalAddr _ _) (OffPtr (Addr _))) => (ConstBool [false])
  2199  (EqPtr  (OffPtr (LocalAddr _ _)) (OffPtr (Addr _))) => (ConstBool [false])
  2200  (NeqPtr (LocalAddr _ _) (Addr _)) => (ConstBool [true])
  2201  (NeqPtr (OffPtr (LocalAddr _ _)) (Addr _)) => (ConstBool [true])
  2202  (NeqPtr (LocalAddr _ _) (OffPtr (Addr _))) => (ConstBool [true])
  2203  (NeqPtr (OffPtr (LocalAddr _ _)) (OffPtr (Addr _))) => (ConstBool [true])
  2204  
  2205  // Simplify address comparisons.
  2206  (EqPtr  (AddPtr p1 o1) p2) && isSamePtr(p1, p2) => (Not (IsNonNil o1))
  2207  (NeqPtr (AddPtr p1 o1) p2) && isSamePtr(p1, p2) => (IsNonNil o1)
  2208  (EqPtr  (Const(32|64) [0]) p) => (Not (IsNonNil p))
  2209  (NeqPtr (Const(32|64) [0]) p) => (IsNonNil p)
  2210  (EqPtr  (ConstNil) p) => (Not (IsNonNil p))
  2211  (NeqPtr (ConstNil) p) => (IsNonNil p)
  2212  
  2213  // Evaluate constant user nil checks.
  2214  (IsNonNil (ConstNil)) => (ConstBool [false])
  2215  (IsNonNil (Const(32|64) [c])) => (ConstBool [c != 0])
  2216  (IsNonNil          (Addr _)   ) => (ConstBool [true])
  2217  (IsNonNil (Convert (Addr _) _)) => (ConstBool [true])
  2218  (IsNonNil (LocalAddr _ _)) => (ConstBool [true])
  2219  
  2220  // Inline small or disjoint runtime.memmove calls with constant length.
  2221  // See the comment in op Move in genericOps.go for discussion of the type.
  2222  //
  2223  // Note that we've lost any knowledge of the type and alignment requirements
  2224  // of the source and destination. We only know the size, and that the type
  2225  // contains no pointers.
  2226  // The type of the move is not necessarily v.Args[0].Type().Elem()!
  2227  // See issue 55122 for details.
  2228  //
  2229  // Because expand calls runs after prove, constants useful to this pattern may not appear.
  2230  // Both versions need to exist; the memory and register variants.
  2231  //
  2232  // Match post-expansion calls, memory version.
  2233  (SelectN [0] call:(StaticCall {sym} s1:(Store _ (Const(64|32) [sz]) s2:(Store  _ src s3:(Store {t} _ dst mem)))))
  2234  	&& sz >= 0
  2235  	&& isSameCall(sym, "runtime.memmove")
  2236  	&& s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1
  2237  	&& isInlinableMemmove(dst, src, int64(sz), config)
  2238  	&& clobber(s1, s2, s3, call)
  2239  	=> (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
  2240  
  2241  // Match post-expansion calls, register version.
  2242  (SelectN [0] call:(StaticCall {sym} dst src (Const(64|32) [sz]) mem))
  2243  	&& sz >= 0
  2244  	&& call.Uses == 1 // this will exclude all calls with results
  2245  	&& isSameCall(sym, "runtime.memmove")
  2246  	&& isInlinableMemmove(dst, src, int64(sz), config)
  2247  	&& clobber(call)
  2248  	=> (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
  2249  
  2250  // Match pre-expansion calls.
  2251  (SelectN [0] call:(StaticLECall {sym} dst src (Const(64|32) [sz]) mem))
  2252  	&& sz >= 0
  2253  	&& call.Uses == 1 // this will exclude all calls with results
  2254  	&& isSameCall(sym, "runtime.memmove")
  2255  	&& isInlinableMemmove(dst, src, int64(sz), config)
  2256  	&& clobber(call)
  2257  	=> (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
  2258  
  2259  // De-virtualize late-expanded interface calls into late-expanded static calls.
  2260  (InterLECall [argsize] {auxCall} (Addr {fn} (SB)) ___) => devirtLECall(v, fn.(*obj.LSym))
  2261  
  2262  // Move and Zero optimizations.
  2263  // Move source and destination may overlap.
  2264  
  2265  // Convert Moves into Zeros when the source is known to be zeros.
  2266  (Move {t} [n] dst1 src mem:(Zero {t} [n] dst2 _)) && isSamePtr(src, dst2)
  2267  	=> (Zero {t} [n] dst1 mem)
  2268  (Move {t} [n] dst1 src mem:(VarDef (Zero {t} [n] dst0 _))) && isSamePtr(src, dst0)
  2269  	=> (Zero {t} [n] dst1 mem)
  2270  (Move {t} [n] dst (Addr {sym} (SB)) mem) && symIsROZero(sym) => (Zero {t} [n] dst mem)
  2271  
  2272  // Don't Store to variables that are about to be overwritten by Move/Zero.
  2273  (Zero {t1} [n] p1 store:(Store {t2} (OffPtr [o2] p2) _ mem))
  2274  	&& isSamePtr(p1, p2) && store.Uses == 1
  2275  	&& n >= o2 + t2.Size()
  2276  	&& clobber(store)
  2277  	=> (Zero {t1} [n] p1 mem)
  2278  (Move {t1} [n] dst1 src1 store:(Store {t2} op:(OffPtr [o2] dst2) _ mem))
  2279  	&& isSamePtr(dst1, dst2) && store.Uses == 1
  2280  	&& n >= o2 + t2.Size()
  2281  	&& disjoint(src1, n, op, t2.Size())
  2282  	&& clobber(store)
  2283  	=> (Move {t1} [n] dst1 src1 mem)
  2284  
  2285  // Don't Move to variables that are immediately completely overwritten.
  2286  (Zero {t} [n] dst1 move:(Move {t} [n] dst2 _ mem))
  2287  	&& move.Uses == 1
  2288  	&& isSamePtr(dst1, dst2)
  2289  	&& clobber(move)
  2290  	=> (Zero {t} [n] dst1 mem)
  2291  (Move {t} [n] dst1 src1 move:(Move {t} [n] dst2 _ mem))
  2292  	&& move.Uses == 1
  2293  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  2294  	&& clobber(move)
  2295  	=> (Move {t} [n] dst1 src1 mem)
  2296  (Zero {t} [n] dst1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
  2297  	&& move.Uses == 1 && vardef.Uses == 1
  2298  	&& isSamePtr(dst1, dst2)
  2299  	&& clobber(move, vardef)
  2300  	=> (Zero {t} [n] dst1 (VarDef {x} mem))
  2301  (Move {t} [n] dst1 src1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
  2302  	&& move.Uses == 1 && vardef.Uses == 1
  2303  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  2304  	&& clobber(move, vardef)
  2305  	=> (Move {t} [n] dst1 src1 (VarDef {x} mem))
  2306  (Store {t1} op1:(OffPtr [o1] p1) d1
  2307  	m2:(Store {t2} op2:(OffPtr [0] p2) d2
  2308  		m3:(Move [n] p3 _ mem)))
  2309  	&& m2.Uses == 1 && m3.Uses == 1
  2310  	&& o1 == t2.Size()
  2311  	&& n == t2.Size() + t1.Size()
  2312  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  2313  	&& clobber(m2, m3)
  2314  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
  2315  (Store {t1} op1:(OffPtr [o1] p1) d1
  2316  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  2317  		m3:(Store {t3} op3:(OffPtr [0] p3) d3
  2318  			m4:(Move [n] p4 _ mem))))
  2319  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
  2320  	&& o2 == t3.Size()
  2321  	&& o1-o2 == t2.Size()
  2322  	&& n == t3.Size() + t2.Size() + t1.Size()
  2323  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  2324  	&& clobber(m2, m3, m4)
  2325  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
  2326  (Store {t1} op1:(OffPtr [o1] p1) d1
  2327  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  2328  		m3:(Store {t3} op3:(OffPtr [o3] p3) d3
  2329  			m4:(Store {t4} op4:(OffPtr [0] p4) d4
  2330  				m5:(Move [n] p5 _ mem)))))
  2331  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
  2332  	&& o3 == t4.Size()
  2333  	&& o2-o3 == t3.Size()
  2334  	&& o1-o2 == t2.Size()
  2335  	&& n == t4.Size() + t3.Size() + t2.Size() + t1.Size()
  2336  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  2337  	&& clobber(m2, m3, m4, m5)
  2338  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
  2339  
  2340  // Don't Zero variables that are immediately completely overwritten
  2341  // before being accessed.
  2342  (Move {t} [n] dst1 src1 zero:(Zero {t} [n] dst2 mem))
  2343  	&& zero.Uses == 1
  2344  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  2345  	&& clobber(zero)
  2346  	=> (Move {t} [n] dst1 src1 mem)
  2347  (Move {t} [n] dst1 src1 vardef:(VarDef {x} zero:(Zero {t} [n] dst2 mem)))
  2348  	&& zero.Uses == 1 && vardef.Uses == 1
  2349  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  2350  	&& clobber(zero, vardef)
  2351  	=> (Move {t} [n] dst1 src1 (VarDef {x} mem))
  2352  (Store {t1} op1:(OffPtr [o1] p1) d1
  2353  	m2:(Store {t2} op2:(OffPtr [0] p2) d2
  2354  		m3:(Zero [n] p3 mem)))
  2355  	&& m2.Uses == 1 && m3.Uses == 1
  2356  	&& o1 == t2.Size()
  2357  	&& n == t2.Size() + t1.Size()
  2358  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  2359  	&& clobber(m2, m3)
  2360  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
  2361  (Store {t1} op1:(OffPtr [o1] p1) d1
  2362  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  2363  		m3:(Store {t3} op3:(OffPtr [0] p3) d3
  2364  			m4:(Zero [n] p4 mem))))
  2365  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
  2366  	&& o2 == t3.Size()
  2367  	&& o1-o2 == t2.Size()
  2368  	&& n == t3.Size() + t2.Size() + t1.Size()
  2369  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  2370  	&& clobber(m2, m3, m4)
  2371  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
  2372  (Store {t1} op1:(OffPtr [o1] p1) d1
  2373  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  2374  		m3:(Store {t3} op3:(OffPtr [o3] p3) d3
  2375  			m4:(Store {t4} op4:(OffPtr [0] p4) d4
  2376  				m5:(Zero [n] p5 mem)))))
  2377  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
  2378  	&& o3 == t4.Size()
  2379  	&& o2-o3 == t3.Size()
  2380  	&& o1-o2 == t2.Size()
  2381  	&& n == t4.Size() + t3.Size() + t2.Size() + t1.Size()
  2382  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  2383  	&& clobber(m2, m3, m4, m5)
  2384  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
  2385  
  2386  // Don't Move from memory if the values are likely to already be
  2387  // in registers.
  2388  (Move {t1} [n] dst p1
  2389  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  2390  		(Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _)))
  2391  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  2392  	&& t2.Alignment() <= t1.Alignment()
  2393  	&& t3.Alignment() <= t1.Alignment()
  2394  	&& registerizable(b, t2)
  2395  	&& registerizable(b, t3)
  2396  	&& o2 == t3.Size()
  2397  	&& n == t2.Size() + t3.Size()
  2398  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2399  		(Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
  2400  (Move {t1} [n] dst p1
  2401  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  2402  		(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  2403  			(Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _))))
  2404  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  2405  	&& t2.Alignment() <= t1.Alignment()
  2406  	&& t3.Alignment() <= t1.Alignment()
  2407  	&& t4.Alignment() <= t1.Alignment()
  2408  	&& registerizable(b, t2)
  2409  	&& registerizable(b, t3)
  2410  	&& registerizable(b, t4)
  2411  	&& o3 == t4.Size()
  2412  	&& o2-o3 == t3.Size()
  2413  	&& n == t2.Size() + t3.Size() + t4.Size()
  2414  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2415  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2416  			(Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
  2417  (Move {t1} [n] dst p1
  2418  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  2419  		(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  2420  			(Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
  2421  				(Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _)))))
  2422  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  2423  	&& t2.Alignment() <= t1.Alignment()
  2424  	&& t3.Alignment() <= t1.Alignment()
  2425  	&& t4.Alignment() <= t1.Alignment()
  2426  	&& t5.Alignment() <= t1.Alignment()
  2427  	&& registerizable(b, t2)
  2428  	&& registerizable(b, t3)
  2429  	&& registerizable(b, t4)
  2430  	&& registerizable(b, t5)
  2431  	&& o4 == t5.Size()
  2432  	&& o3-o4 == t4.Size()
  2433  	&& o2-o3 == t3.Size()
  2434  	&& n == t2.Size() + t3.Size() + t4.Size() + t5.Size()
  2435  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2436  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2437  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  2438  				(Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
  2439  
  2440  // Same thing but with VarDef in the middle.
  2441  (Move {t1} [n] dst p1
  2442  	mem:(VarDef
  2443  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  2444  			(Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _))))
  2445  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  2446  	&& t2.Alignment() <= t1.Alignment()
  2447  	&& t3.Alignment() <= t1.Alignment()
  2448  	&& registerizable(b, t2)
  2449  	&& registerizable(b, t3)
  2450  	&& o2 == t3.Size()
  2451  	&& n == t2.Size() + t3.Size()
  2452  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2453  		(Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
  2454  (Move {t1} [n] dst p1
  2455  	mem:(VarDef
  2456  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  2457  			(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  2458  				(Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _)))))
  2459  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  2460  	&& t2.Alignment() <= t1.Alignment()
  2461  	&& t3.Alignment() <= t1.Alignment()
  2462  	&& t4.Alignment() <= t1.Alignment()
  2463  	&& registerizable(b, t2)
  2464  	&& registerizable(b, t3)
  2465  	&& registerizable(b, t4)
  2466  	&& o3 == t4.Size()
  2467  	&& o2-o3 == t3.Size()
  2468  	&& n == t2.Size() + t3.Size() + t4.Size()
  2469  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2470  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2471  			(Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
  2472  (Move {t1} [n] dst p1
  2473  	mem:(VarDef
  2474  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  2475  			(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  2476  				(Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
  2477  					(Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _))))))
  2478  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  2479  	&& t2.Alignment() <= t1.Alignment()
  2480  	&& t3.Alignment() <= t1.Alignment()
  2481  	&& t4.Alignment() <= t1.Alignment()
  2482  	&& t5.Alignment() <= t1.Alignment()
  2483  	&& registerizable(b, t2)
  2484  	&& registerizable(b, t3)
  2485  	&& registerizable(b, t4)
  2486  	&& registerizable(b, t5)
  2487  	&& o4 == t5.Size()
  2488  	&& o3-o4 == t4.Size()
  2489  	&& o2-o3 == t3.Size()
  2490  	&& n == t2.Size() + t3.Size() + t4.Size() + t5.Size()
  2491  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2492  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2493  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  2494  				(Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
  2495  
  2496  // Prefer to Zero and Store than to Move.
  2497  (Move {t1} [n] dst p1
  2498  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  2499  		(Zero {t3} [n] p3 _)))
  2500  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  2501  	&& t2.Alignment() <= t1.Alignment()
  2502  	&& t3.Alignment() <= t1.Alignment()
  2503  	&& registerizable(b, t2)
  2504  	&& n >= o2 + t2.Size()
  2505  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2506  		(Zero {t1} [n] dst mem))
  2507  (Move {t1} [n] dst p1
  2508  	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
  2509  		(Store {t3} (OffPtr <tt3> [o3] p3) d2
  2510  			(Zero {t4} [n] p4 _))))
  2511  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  2512  	&& t2.Alignment() <= t1.Alignment()
  2513  	&& t3.Alignment() <= t1.Alignment()
  2514  	&& t4.Alignment() <= t1.Alignment()
  2515  	&& registerizable(b, t2)
  2516  	&& registerizable(b, t3)
  2517  	&& n >= o2 + t2.Size()
  2518  	&& n >= o3 + t3.Size()
  2519  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2520  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2521  			(Zero {t1} [n] dst mem)))
  2522  (Move {t1} [n] dst p1
  2523  	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
  2524  		(Store {t3} (OffPtr <tt3> [o3] p3) d2
  2525  			(Store {t4} (OffPtr <tt4> [o4] p4) d3
  2526  				(Zero {t5} [n] p5 _)))))
  2527  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  2528  	&& t2.Alignment() <= t1.Alignment()
  2529  	&& t3.Alignment() <= t1.Alignment()
  2530  	&& t4.Alignment() <= t1.Alignment()
  2531  	&& t5.Alignment() <= t1.Alignment()
  2532  	&& registerizable(b, t2)
  2533  	&& registerizable(b, t3)
  2534  	&& registerizable(b, t4)
  2535  	&& n >= o2 + t2.Size()
  2536  	&& n >= o3 + t3.Size()
  2537  	&& n >= o4 + t4.Size()
  2538  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2539  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2540  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  2541  				(Zero {t1} [n] dst mem))))
  2542  (Move {t1} [n] dst p1
  2543  	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
  2544  		(Store {t3} (OffPtr <tt3> [o3] p3) d2
  2545  			(Store {t4} (OffPtr <tt4> [o4] p4) d3
  2546  				(Store {t5} (OffPtr <tt5> [o5] p5) d4
  2547  					(Zero {t6} [n] p6 _))))))
  2548  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6)
  2549  	&& t2.Alignment() <= t1.Alignment()
  2550  	&& t3.Alignment() <= t1.Alignment()
  2551  	&& t4.Alignment() <= t1.Alignment()
  2552  	&& t5.Alignment() <= t1.Alignment()
  2553  	&& t6.Alignment() <= t1.Alignment()
  2554  	&& registerizable(b, t2)
  2555  	&& registerizable(b, t3)
  2556  	&& registerizable(b, t4)
  2557  	&& registerizable(b, t5)
  2558  	&& n >= o2 + t2.Size()
  2559  	&& n >= o3 + t3.Size()
  2560  	&& n >= o4 + t4.Size()
  2561  	&& n >= o5 + t5.Size()
  2562  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2563  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2564  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  2565  				(Store {t5} (OffPtr <tt5> [o5] dst) d4
  2566  					(Zero {t1} [n] dst mem)))))
  2567  (Move {t1} [n] dst p1
  2568  	mem:(VarDef
  2569  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  2570  			(Zero {t3} [n] p3 _))))
  2571  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  2572  	&& t2.Alignment() <= t1.Alignment()
  2573  	&& t3.Alignment() <= t1.Alignment()
  2574  	&& registerizable(b, t2)
  2575  	&& n >= o2 + t2.Size()
  2576  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2577  		(Zero {t1} [n] dst mem))
  2578  (Move {t1} [n] dst p1
  2579  	mem:(VarDef
  2580  		(Store {t2} (OffPtr <tt2> [o2] p2) d1
  2581  			(Store {t3} (OffPtr <tt3> [o3] p3) d2
  2582  				(Zero {t4} [n] p4 _)))))
  2583  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  2584  	&& t2.Alignment() <= t1.Alignment()
  2585  	&& t3.Alignment() <= t1.Alignment()
  2586  	&& t4.Alignment() <= t1.Alignment()
  2587  	&& registerizable(b, t2)
  2588  	&& registerizable(b, t3)
  2589  	&& n >= o2 + t2.Size()
  2590  	&& n >= o3 + t3.Size()
  2591  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2592  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2593  			(Zero {t1} [n] dst mem)))
  2594  (Move {t1} [n] dst p1
  2595  	mem:(VarDef
  2596  		(Store {t2} (OffPtr <tt2> [o2] p2) d1
  2597  			(Store {t3} (OffPtr <tt3> [o3] p3) d2
  2598  				(Store {t4} (OffPtr <tt4> [o4] p4) d3
  2599  					(Zero {t5} [n] p5 _))))))
  2600  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  2601  	&& t2.Alignment() <= t1.Alignment()
  2602  	&& t3.Alignment() <= t1.Alignment()
  2603  	&& t4.Alignment() <= t1.Alignment()
  2604  	&& t5.Alignment() <= t1.Alignment()
  2605  	&& registerizable(b, t2)
  2606  	&& registerizable(b, t3)
  2607  	&& registerizable(b, t4)
  2608  	&& n >= o2 + t2.Size()
  2609  	&& n >= o3 + t3.Size()
  2610  	&& n >= o4 + t4.Size()
  2611  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2612  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2613  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  2614  				(Zero {t1} [n] dst mem))))
  2615  (Move {t1} [n] dst p1
  2616  	mem:(VarDef
  2617  		(Store {t2} (OffPtr <tt2> [o2] p2) d1
  2618  			(Store {t3} (OffPtr <tt3> [o3] p3) d2
  2619  				(Store {t4} (OffPtr <tt4> [o4] p4) d3
  2620  					(Store {t5} (OffPtr <tt5> [o5] p5) d4
  2621  						(Zero {t6} [n] p6 _)))))))
  2622  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6)
  2623  	&& t2.Alignment() <= t1.Alignment()
  2624  	&& t3.Alignment() <= t1.Alignment()
  2625  	&& t4.Alignment() <= t1.Alignment()
  2626  	&& t5.Alignment() <= t1.Alignment()
  2627  	&& t6.Alignment() <= t1.Alignment()
  2628  	&& registerizable(b, t2)
  2629  	&& registerizable(b, t3)
  2630  	&& registerizable(b, t4)
  2631  	&& registerizable(b, t5)
  2632  	&& n >= o2 + t2.Size()
  2633  	&& n >= o3 + t3.Size()
  2634  	&& n >= o4 + t4.Size()
  2635  	&& n >= o5 + t5.Size()
  2636  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2637  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2638  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  2639  				(Store {t5} (OffPtr <tt5> [o5] dst) d4
  2640  					(Zero {t1} [n] dst mem)))))
  2641  
  2642  (SelectN [0] call:(StaticLECall {sym} a x)) && needRaceCleanup(sym, call) && clobber(call) => x
  2643  (SelectN [0] call:(StaticLECall {sym} x)) && needRaceCleanup(sym, call) && clobber(call) => x
  2644  
  2645  // When rewriting append to growslice, we use as the new length the result of
  2646  // growslice so that we don't have to spill/restore the new length around the growslice call.
  2647  // The exception here is that if the new length is a constant, avoiding spilling it
  2648  // is pointless and its constantness is sometimes useful for subsequent optimizations.
  2649  // See issue 56440.
  2650  // Note there are 2 rules here, one for the pre-decomposed []T result and one for
  2651  // the post-decomposed (*T,int,int) result. (The latter is generated after call expansion.)
  2652  (SliceLen (SelectN [0] (StaticLECall {sym} _ newLen:(Const(64|32)) _ _ _ _))) && isSameCall(sym, "runtime.growslice") => newLen
  2653  (SelectN [1] (StaticCall {sym} _ newLen:(Const(64|32)) _ _ _ _)) && v.Type.IsInteger() && isSameCall(sym, "runtime.growslice") => newLen
  2654  
  2655  // Collapse moving A -> B -> C into just A -> C.
  2656  // Later passes (deadstore, elim unread auto) will remove the A -> B move, if possible.
  2657  // This happens most commonly when B is an autotmp inserted earlier
  2658  // during compilation to ensure correctness.
  2659  // Take care that overlapping moves are preserved.
  2660  // Restrict this optimization to the stack, to avoid duplicating loads from the heap;
  2661  // see CL 145208 for discussion.
  2662  (Move {t1} [s] dst tmp1 midmem:(Move {t2} [s] tmp2 src _))
  2663  	&& t1.Compare(t2) == types.CMPeq
  2664  	&& isSamePtr(tmp1, tmp2)
  2665  	&& isStackPtr(src) && !isVolatile(src)
  2666  	&& disjoint(src, s, tmp2, s)
  2667  	&& (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
  2668  	=> (Move {t1} [s] dst src midmem)
  2669  
  2670  // Same, but for large types that require VarDefs.
  2671  (Move {t1} [s] dst tmp1 midmem:(VarDef (Move {t2} [s] tmp2 src _)))
  2672  	&& t1.Compare(t2) == types.CMPeq
  2673  	&& isSamePtr(tmp1, tmp2)
  2674  	&& isStackPtr(src) && !isVolatile(src)
  2675  	&& disjoint(src, s, tmp2, s)
  2676  	&& (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
  2677  	=> (Move {t1} [s] dst src midmem)
  2678  
  2679  // Don't zero the same bits twice.
  2680  (Zero {t} [s] dst1 zero:(Zero {t} [s] dst2 _)) && isSamePtr(dst1, dst2) => zero
  2681  (Zero {t} [s] dst1 vardef:(VarDef (Zero {t} [s] dst2 _))) && isSamePtr(dst1, dst2) => vardef
  2682  
  2683  // Elide self-moves. This only happens rarely (e.g test/fixedbugs/bug277.go).
  2684  // However, this rule is needed to prevent the previous rule from looping forever in such cases.
  2685  (Move dst src mem) && isSamePtr(dst, src) => mem
  2686  
  2687  // Constant rotate detection.
  2688  ((Add64|Or64|Xor64) (Lsh64x64 x z:(Const64 <t> [c])) (Rsh64Ux64 x (Const64 [d]))) && c < 64 && d == 64-c && canRotate(config, 64) => (RotateLeft64 x z)
  2689  ((Add32|Or32|Xor32) (Lsh32x64 x z:(Const64 <t> [c])) (Rsh32Ux64 x (Const64 [d]))) && c < 32 && d == 32-c && canRotate(config, 32) => (RotateLeft32 x z)
  2690  ((Add16|Or16|Xor16) (Lsh16x64 x z:(Const64 <t> [c])) (Rsh16Ux64 x (Const64 [d]))) && c < 16 && d == 16-c && canRotate(config, 16) => (RotateLeft16 x z)
  2691  ((Add8|Or8|Xor8) (Lsh8x64 x z:(Const64 <t> [c])) (Rsh8Ux64 x (Const64 [d]))) && c < 8 && d == 8-c && canRotate(config, 8) => (RotateLeft8 x z)
  2692  
  2693  // Non-constant rotate detection.
  2694  // We use shiftIsBounded to make sure that neither of the shifts are >64.
  2695  // Note: these rules are subtle when the shift amounts are 0/64, as Go shifts
  2696  // are different from most native shifts. But it works out.
  2697  ((Add64|Or64|Xor64) left:(Lsh64x64 x y) right:(Rsh64Ux64 x (Sub64 (Const64 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
  2698  ((Add64|Or64|Xor64) left:(Lsh64x32 x y) right:(Rsh64Ux32 x (Sub32 (Const32 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
  2699  ((Add64|Or64|Xor64) left:(Lsh64x16 x y) right:(Rsh64Ux16 x (Sub16 (Const16 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
  2700  ((Add64|Or64|Xor64) left:(Lsh64x8  x y) right:(Rsh64Ux8  x (Sub8  (Const8  [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
  2701  
  2702  ((Add64|Or64|Xor64) right:(Rsh64Ux64 x y) left:(Lsh64x64 x z:(Sub64 (Const64 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
  2703  ((Add64|Or64|Xor64) right:(Rsh64Ux32 x y) left:(Lsh64x32 x z:(Sub32 (Const32 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
  2704  ((Add64|Or64|Xor64) right:(Rsh64Ux16 x y) left:(Lsh64x16 x z:(Sub16 (Const16 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
  2705  ((Add64|Or64|Xor64) right:(Rsh64Ux8  x y) left:(Lsh64x8  x z:(Sub8  (Const8  [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
  2706  
  2707  ((Add32|Or32|Xor32) left:(Lsh32x64 x y) right:(Rsh32Ux64 x (Sub64 (Const64 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
  2708  ((Add32|Or32|Xor32) left:(Lsh32x32 x y) right:(Rsh32Ux32 x (Sub32 (Const32 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
  2709  ((Add32|Or32|Xor32) left:(Lsh32x16 x y) right:(Rsh32Ux16 x (Sub16 (Const16 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
  2710  ((Add32|Or32|Xor32) left:(Lsh32x8  x y) right:(Rsh32Ux8  x (Sub8  (Const8  [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
  2711  
  2712  ((Add32|Or32|Xor32) right:(Rsh32Ux64 x y) left:(Lsh32x64 x z:(Sub64 (Const64 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
  2713  ((Add32|Or32|Xor32) right:(Rsh32Ux32 x y) left:(Lsh32x32 x z:(Sub32 (Const32 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
  2714  ((Add32|Or32|Xor32) right:(Rsh32Ux16 x y) left:(Lsh32x16 x z:(Sub16 (Const16 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
  2715  ((Add32|Or32|Xor32) right:(Rsh32Ux8  x y) left:(Lsh32x8  x z:(Sub8  (Const8  [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
  2716  
  2717  ((Add16|Or16|Xor16) left:(Lsh16x64 x y) right:(Rsh16Ux64 x (Sub64 (Const64 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
  2718  ((Add16|Or16|Xor16) left:(Lsh16x32 x y) right:(Rsh16Ux32 x (Sub32 (Const32 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
  2719  ((Add16|Or16|Xor16) left:(Lsh16x16 x y) right:(Rsh16Ux16 x (Sub16 (Const16 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
  2720  ((Add16|Or16|Xor16) left:(Lsh16x8  x y) right:(Rsh16Ux8  x (Sub8  (Const8  [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
  2721  
  2722  ((Add16|Or16|Xor16) right:(Rsh16Ux64 x y) left:(Lsh16x64 x z:(Sub64 (Const64 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
  2723  ((Add16|Or16|Xor16) right:(Rsh16Ux32 x y) left:(Lsh16x32 x z:(Sub32 (Const32 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
  2724  ((Add16|Or16|Xor16) right:(Rsh16Ux16 x y) left:(Lsh16x16 x z:(Sub16 (Const16 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
  2725  ((Add16|Or16|Xor16) right:(Rsh16Ux8  x y) left:(Lsh16x8  x z:(Sub8  (Const8  [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
  2726  
  2727  ((Add8|Or8|Xor8) left:(Lsh8x64 x y) right:(Rsh8Ux64 x (Sub64 (Const64 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
  2728  ((Add8|Or8|Xor8) left:(Lsh8x32 x y) right:(Rsh8Ux32 x (Sub32 (Const32 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
  2729  ((Add8|Or8|Xor8) left:(Lsh8x16 x y) right:(Rsh8Ux16 x (Sub16 (Const16 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
  2730  ((Add8|Or8|Xor8) left:(Lsh8x8  x y) right:(Rsh8Ux8  x (Sub8  (Const8  [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
  2731  
  2732  ((Add8|Or8|Xor8) right:(Rsh8Ux64 x y) left:(Lsh8x64 x z:(Sub64 (Const64 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
  2733  ((Add8|Or8|Xor8) right:(Rsh8Ux32 x y) left:(Lsh8x32 x z:(Sub32 (Const32 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
  2734  ((Add8|Or8|Xor8) right:(Rsh8Ux16 x y) left:(Lsh8x16 x z:(Sub16 (Const16 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
  2735  ((Add8|Or8|Xor8) right:(Rsh8Ux8  x y) left:(Lsh8x8  x z:(Sub8  (Const8  [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
  2736  
  2737  // Rotating by y&c, with c a mask that doesn't change the bottom bits, is the same as rotating by y.
  2738  (RotateLeft64 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&63 == 63 => (RotateLeft64 x y)
  2739  (RotateLeft32 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&31 == 31 => (RotateLeft32 x y)
  2740  (RotateLeft16 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&15 == 15 => (RotateLeft16 x y)
  2741  (RotateLeft8  x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&7  == 7  => (RotateLeft8  x y)
  2742  
  2743  // Rotating by -(y&c), with c a mask that doesn't change the bottom bits, is the same as rotating by -y.
  2744  (RotateLeft64 x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&63 == 63 => (RotateLeft64 x (Neg(64|32|16|8) <y.Type> y))
  2745  (RotateLeft32 x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&31 == 31 => (RotateLeft32 x (Neg(64|32|16|8) <y.Type> y))
  2746  (RotateLeft16 x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&15 == 15 => (RotateLeft16 x (Neg(64|32|16|8) <y.Type> y))
  2747  (RotateLeft8  x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&7  == 7  => (RotateLeft8  x (Neg(64|32|16|8) <y.Type> y))
  2748  
  2749  // Rotating by y+c, with c a multiple of the value width, is the same as rotating by y.
  2750  (RotateLeft64 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&63 == 0 => (RotateLeft64 x y)
  2751  (RotateLeft32 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&31 == 0 => (RotateLeft32 x y)
  2752  (RotateLeft16 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&15 == 0 => (RotateLeft16 x y)
  2753  (RotateLeft8  x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&7  == 0 => (RotateLeft8  x y)
  2754  
  2755  // Rotating by c-y, with c a multiple of the value width, is the same as rotating by -y.
  2756  (RotateLeft64 x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&63 == 0 => (RotateLeft64 x (Neg(64|32|16|8) <y.Type> y))
  2757  (RotateLeft32 x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&31 == 0 => (RotateLeft32 x (Neg(64|32|16|8) <y.Type> y))
  2758  (RotateLeft16 x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&15 == 0 => (RotateLeft16 x (Neg(64|32|16|8) <y.Type> y))
  2759  (RotateLeft8  x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&7  == 0 => (RotateLeft8  x (Neg(64|32|16|8) <y.Type> y))
  2760  
  2761  // Ensure we don't do Const64 rotates in a 32-bit system.
  2762  (RotateLeft64 x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft64 x (Const32 <t> [int32(c)]))
  2763  (RotateLeft32 x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft32 x (Const32 <t> [int32(c)]))
  2764  (RotateLeft16 x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft16 x (Const32 <t> [int32(c)]))
  2765  (RotateLeft8  x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft8  x (Const32 <t> [int32(c)]))
  2766  
  2767  // Rotating by c, then by d, is the same as rotating by c+d.
  2768  // We're trading a rotate for an add, which seems generally a good choice. It is especially good when c and d are constants.
  2769  // This rule is a bit tricky as c and d might be different widths. We handle only cases where they are the same width.
  2770  (RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 8 && d.Type.Size() == 8 => (RotateLeft(64|32|16|8) x (Add64 <c.Type> c d))
  2771  (RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 4 && d.Type.Size() == 4 => (RotateLeft(64|32|16|8) x (Add32 <c.Type> c d))
  2772  (RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 2 && d.Type.Size() == 2 => (RotateLeft(64|32|16|8) x (Add16 <c.Type> c d))
  2773  (RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 1 && d.Type.Size() == 1 => (RotateLeft(64|32|16|8) x (Add8  <c.Type> c d))
  2774  
  2775  // Loading constant values from dictionaries and itabs.
  2776  (Load <typ.BytePtr> (OffPtr [off]                       (Addr {s} sb)       ) _)  && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
  2777  (Load <typ.BytePtr> (OffPtr [off]              (Convert (Addr {s} sb) _)    ) _)  && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
  2778  (Load <typ.BytePtr> (OffPtr [off] (ITab (IMake          (Addr {s} sb)    _))) _)  && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
  2779  (Load <typ.BytePtr> (OffPtr [off] (ITab (IMake (Convert (Addr {s} sb) _) _))) _)  && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
  2780  (Load <typ.Uintptr> (OffPtr [off]                       (Addr {s} sb)       ) _)  && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
  2781  (Load <typ.Uintptr> (OffPtr [off]              (Convert (Addr {s} sb) _)    ) _)  && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
  2782  (Load <typ.Uintptr> (OffPtr [off] (ITab (IMake          (Addr {s} sb)    _))) _)  && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
  2783  (Load <typ.Uintptr> (OffPtr [off] (ITab (IMake (Convert (Addr {s} sb) _) _))) _)  && isFixedSym(s, off) => (Addr {fixedSym(b.Func, s, off)} sb)
  2784  
  2785  // Loading constant values from runtime._type.hash.
  2786  (Load <t> (OffPtr [off]                       (Addr {sym} _)       ) _) && t.IsInteger() && t.Size() == 4 && isFixed32(config, sym, off) => (Const32 [fixed32(config, sym, off)])
  2787  (Load <t> (OffPtr [off]              (Convert (Addr {sym} _) _)    ) _) && t.IsInteger() && t.Size() == 4 && isFixed32(config, sym, off) => (Const32 [fixed32(config, sym, off)])
  2788  (Load <t> (OffPtr [off] (ITab (IMake          (Addr {sym} _)    _))) _) && t.IsInteger() && t.Size() == 4 && isFixed32(config, sym, off) => (Const32 [fixed32(config, sym, off)])
  2789  (Load <t> (OffPtr [off] (ITab (IMake (Convert (Addr {sym} _) _) _))) _) && t.IsInteger() && t.Size() == 4 && isFixed32(config, sym, off) => (Const32 [fixed32(config, sym, off)])
  2790  
  2791  // Calling cmpstring a second time with the same arguments in the
  2792  // same memory state can reuse the results of the first call.
  2793  // See issue 61725.
  2794  // Note that this could pretty easily generalize to any pure function.
  2795  (SelectN [0] (StaticLECall {f} x y (SelectN [1] c:(StaticLECall {g} x y mem))))
  2796    && isSameCall(f, "runtime.cmpstring")
  2797    && isSameCall(g, "runtime.cmpstring")
  2798  => @c.Block (SelectN [0] <typ.Int> c)
  2799  
  2800  // If we don't use the result of cmpstring, might as well not call it.
  2801  // Note that this could pretty easily generalize to any pure function.
  2802  (SelectN [1] c:(StaticLECall {f} _ _ mem)) && c.Uses == 1 && isSameCall(f, "runtime.cmpstring") && clobber(c) => mem
  2803  
  2804  // We can easily compute the result of efaceeq if
  2805  // we know the underlying type is pointer-ish.
  2806  (StaticLECall {f} typ_ x y mem)
  2807  	&& isSameCall(f, "runtime.efaceeq")
  2808  	&& isDirectType(typ_)
  2809  	&& clobber(v)
  2810  	=> (MakeResult (EqPtr x y) mem)
  2811  
  2812  // We can easily compute the result of ifaceeq if
  2813  // we know the underlying type is pointer-ish.
  2814  (StaticLECall {f} itab x y mem)
  2815  	&& isSameCall(f, "runtime.ifaceeq")
  2816  	&& isDirectIface(itab)
  2817  	&& clobber(v)
  2818  	=> (MakeResult (EqPtr x y) mem)
  2819  
  2820  // If we use the result of slicebytetostring in a map lookup operation,
  2821  // then we don't need to actually do the []byte->string conversion.
  2822  // We can just use the ptr/len of the byte slice directly as a (temporary) string.
  2823  //
  2824  // Note that this does not handle some obscure cases like
  2825  // m[[2]string{string(b1), string(b2)}]. There is code in ../walk/order.go
  2826  // which handles some of those cases.
  2827  (StaticLECall {f} [argsize] typ_ map_ key:(SelectN [0] sbts:(StaticLECall {g} _ ptr len mem)) m:(SelectN [1] sbts))
  2828    &&    (isSameCall(f, "runtime.mapaccess1_faststr")
  2829        || isSameCall(f, "runtime.mapaccess2_faststr")
  2830        || isSameCall(f, "runtime.mapdelete_faststr"))
  2831    && isSameCall(g, "runtime.slicebytetostring")
  2832    && key.Uses == 1
  2833    && sbts.Uses == 2
  2834    && resetCopy(m, mem)
  2835    && clobber(sbts)
  2836    && clobber(key)
  2837  => (StaticLECall {f} [argsize] typ_ map_ (StringMake <typ.String> ptr len) mem)
  2838  
  2839  // Similarly to map lookups, also handle unique.Make for strings, which unique.Make will clone.
  2840  (StaticLECall {f} [argsize] dict_ key:(SelectN [0] sbts:(StaticLECall {g} _ ptr len mem)) m:(SelectN [1] sbts))
  2841    && isSameCall(f, "unique.Make[go.shape.string]")
  2842    && isSameCall(g, "runtime.slicebytetostring")
  2843    && key.Uses == 1
  2844    && sbts.Uses == 2
  2845    && resetCopy(m, mem)
  2846    && clobber(sbts)
  2847    && clobber(key)
  2848  => (StaticLECall {f} [argsize] dict_ (StringMake <typ.String> ptr len) mem)
  2849  
  2850  // Transform some CondSelect into math operations.
  2851  // if b { x++ } => x += b // but not on arm64 because it has CSINC
  2852  (CondSelect (Add8 <t> x (Const8 [1])) x bool) && config.arch != "arm64" => (Add8 x (CvtBoolToUint8 <t> bool))
  2853  (CondSelect (Add(64|32|16) <t> x (Const(64|32|16) [1])) x bool) && config.arch != "arm64" => (Add(64|32|16) x (ZeroExt8to(64|32|16) <t> (CvtBoolToUint8 <types.Types[types.TUINT8]> bool)))
  2854  
  2855  // if b { x-- } => x -= b
  2856  (CondSelect (Add8 <t> x (Const8 [-1])) x bool) => (Sub8 x (CvtBoolToUint8 <t> bool))
  2857  (CondSelect (Add(64|32|16) <t> x (Const(64|32|16) [-1])) x bool) => (Sub(64|32|16) x (ZeroExt8to(64|32|16) <t> (CvtBoolToUint8 <types.Types[types.TUINT8]> bool)))
  2858  
  2859  // if b { x <<= 1 } => x <<= b
  2860  (CondSelect (Lsh(64|32|16|8)x64 x (Const64 [1])) x bool) => (Lsh(64|32|16|8)x8 [true] x (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))
  2861  
  2862  // if b { x >>= 1 } => x >>= b
  2863  (CondSelect (Rsh(64|32|16|8)x64 x (Const64 [1])) x bool) => (Rsh(64|32|16|8)x8 [true] x (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))
  2864  (CondSelect (Rsh(64|32|16|8)Ux64 x (Const64 [1])) x bool) => (Rsh(64|32|16|8)Ux8 [true] x (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))
  2865  

View as plain text