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

View as plain text