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

View as plain text