github.com/corona10/go@v0.0.0-20180224231303-7a218942be57/src/cmd/compile/internal/ssa/gen/generic.rules (about)

     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   [int64(int8(c))])
    42  (Trunc32to8  (Const32  [c])) -> (Const8   [int64(int8(c))])
    43  (Trunc32to16 (Const32  [c])) -> (Const16  [int64(int16(c))])
    44  (Trunc64to8  (Const64  [c])) -> (Const8   [int64(int8(c))])
    45  (Trunc64to16 (Const64  [c])) -> (Const16  [int64(int16(c))])
    46  (Trunc64to32 (Const64  [c])) -> (Const32  [int64(int32(c))])
    47  (Cvt64Fto32F (Const64F [c])) -> (Const32F [f2i(float64(i2f32(c)))])
    48  (Cvt32Fto64F (Const32F [c])) -> (Const64F [c]) // c is already a 64 bit float
    49  (Cvt32to32F  (Const32  [c])) -> (Const32F [f2i(float64(float32(int32(c))))])
    50  (Cvt32to64F  (Const32  [c])) -> (Const64F [f2i(float64(int32(c)))])
    51  (Cvt64to32F  (Const64  [c])) -> (Const32F [f2i(float64(float32(c)))])
    52  (Cvt64to64F  (Const64  [c])) -> (Const64F [f2i(float64(c))])
    53  (Cvt32Fto32  (Const32F [c])) -> (Const32  [int64(int32(i2f(c)))])
    54  (Cvt32Fto64  (Const32F [c])) -> (Const64  [int64(i2f(c))])
    55  (Cvt64Fto32  (Const64F [c])) -> (Const32  [int64(int32(i2f(c)))])
    56  (Cvt64Fto64  (Const64F [c])) -> (Const64  [int64(i2f(c))])
    57  (Round32F x:(Const32F)) -> x
    58  (Round64F x:(Const64F)) -> x
    59  
    60  (Trunc16to8  (ZeroExt8to16  x)) -> x
    61  (Trunc32to8  (ZeroExt8to32  x)) -> x
    62  (Trunc32to16 (ZeroExt8to32  x)) -> (ZeroExt8to16  x)
    63  (Trunc32to16 (ZeroExt16to32 x)) -> x
    64  (Trunc64to8  (ZeroExt8to64  x)) -> x
    65  (Trunc64to16 (ZeroExt8to64  x)) -> (ZeroExt8to16  x)
    66  (Trunc64to16 (ZeroExt16to64 x)) -> x
    67  (Trunc64to32 (ZeroExt8to64  x)) -> (ZeroExt8to32  x)
    68  (Trunc64to32 (ZeroExt16to64 x)) -> (ZeroExt16to32 x)
    69  (Trunc64to32 (ZeroExt32to64 x)) -> x
    70  (Trunc16to8  (SignExt8to16  x)) -> x
    71  (Trunc32to8  (SignExt8to32  x)) -> x
    72  (Trunc32to16 (SignExt8to32  x)) -> (SignExt8to16  x)
    73  (Trunc32to16 (SignExt16to32 x)) -> x
    74  (Trunc64to8  (SignExt8to64  x)) -> x
    75  (Trunc64to16 (SignExt8to64  x)) -> (SignExt8to16  x)
    76  (Trunc64to16 (SignExt16to64 x)) -> x
    77  (Trunc64to32 (SignExt8to64  x)) -> (SignExt8to32  x)
    78  (Trunc64to32 (SignExt16to64 x)) -> (SignExt16to32 x)
    79  (Trunc64to32 (SignExt32to64 x)) -> x
    80  
    81  (ZeroExt8to16  (Const8  [c])) -> (Const16 [int64( uint8(c))])
    82  (ZeroExt8to32  (Const8  [c])) -> (Const32 [int64( uint8(c))])
    83  (ZeroExt8to64  (Const8  [c])) -> (Const64 [int64( uint8(c))])
    84  (ZeroExt16to32 (Const16 [c])) -> (Const32 [int64(uint16(c))])
    85  (ZeroExt16to64 (Const16 [c])) -> (Const64 [int64(uint16(c))])
    86  (ZeroExt32to64 (Const32 [c])) -> (Const64 [int64(uint32(c))])
    87  (SignExt8to16  (Const8  [c])) -> (Const16 [int64(  int8(c))])
    88  (SignExt8to32  (Const8  [c])) -> (Const32 [int64(  int8(c))])
    89  (SignExt8to64  (Const8  [c])) -> (Const64 [int64(  int8(c))])
    90  (SignExt16to32 (Const16 [c])) -> (Const32 [int64( int16(c))])
    91  (SignExt16to64 (Const16 [c])) -> (Const64 [int64( int16(c))])
    92  (SignExt32to64 (Const32 [c])) -> (Const64 [int64( int32(c))])
    93  
    94  (Neg8   (Const8   [c])) -> (Const8   [int64( -int8(c))])
    95  (Neg16  (Const16  [c])) -> (Const16  [int64(-int16(c))])
    96  (Neg32  (Const32  [c])) -> (Const32  [int64(-int32(c))])
    97  (Neg64  (Const64  [c])) -> (Const64  [-c])
    98  (Neg32F (Const32F [c])) && i2f(c) != 0 -> (Const32F [f2i(-i2f(c))])
    99  (Neg64F (Const64F [c])) && i2f(c) != 0 -> (Const64F [f2i(-i2f(c))])
   100  
   101  (Add8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c+d))])
   102  (Add16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c+d))])
   103  (Add32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c+d))])
   104  (Add64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c+d])
   105  (Add32F (Const32F [c]) (Const32F [d])) ->
   106          (Const32F [f2i(float64(i2f32(c) + i2f32(d)))]) // ensure we combine the operands with 32 bit precision
   107  (Add64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) + i2f(d))])
   108  (AddPtr <t> x (Const64 [c])) -> (OffPtr <t> x [c])
   109  (AddPtr <t> x (Const32 [c])) -> (OffPtr <t> x [c])
   110  
   111  (Sub8   (Const8 [c]) (Const8 [d]))     -> (Const8 [int64(int8(c-d))])
   112  (Sub16  (Const16 [c]) (Const16 [d]))   -> (Const16 [int64(int16(c-d))])
   113  (Sub32  (Const32 [c]) (Const32 [d]))   -> (Const32 [int64(int32(c-d))])
   114  (Sub64  (Const64 [c]) (Const64 [d]))   -> (Const64 [c-d])
   115  (Sub32F (Const32F [c]) (Const32F [d])) ->
   116          (Const32F [f2i(float64(i2f32(c) - i2f32(d)))])
   117  (Sub64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) - i2f(d))])
   118  
   119  (Mul8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c*d))])
   120  (Mul16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c*d))])
   121  (Mul32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c*d))])
   122  (Mul64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c*d])
   123  (Mul32F (Const32F [c]) (Const32F [d])) ->
   124          (Const32F [f2i(float64(i2f32(c) * i2f32(d)))])
   125  (Mul64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) * i2f(d))])
   126  
   127  (And8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c&d))])
   128  (And16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c&d))])
   129  (And32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c&d))])
   130  (And64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c&d])
   131  
   132  (Or8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c|d))])
   133  (Or16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c|d))])
   134  (Or32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c|d))])
   135  (Or64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c|d])
   136  
   137  (Xor8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c^d))])
   138  (Xor16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c^d))])
   139  (Xor32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c^d))])
   140  (Xor64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c^d])
   141  
   142  (Div8   (Const8  [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(int8(c)/int8(d))])
   143  (Div16  (Const16 [c])  (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c)/int16(d))])
   144  (Div32  (Const32 [c])  (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c)/int32(d))])
   145  (Div64  (Const64 [c])  (Const64 [d])) && d != 0 -> (Const64 [c/d])
   146  (Div8u  (Const8  [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(int8(uint8(c)/uint8(d)))])
   147  (Div16u (Const16 [c])  (Const16 [d])) && d != 0 -> (Const16 [int64(int16(uint16(c)/uint16(d)))])
   148  (Div32u (Const32 [c])  (Const32 [d])) && d != 0 -> (Const32 [int64(int32(uint32(c)/uint32(d)))])
   149  (Div64u (Const64 [c])  (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c)/uint64(d))])
   150  (Div32F (Const32F [c]) (Const32F [d])) -> (Const32F [f2i(float64(i2f32(c) / i2f32(d)))])
   151  (Div64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) / i2f(d))])
   152  
   153  (Not (ConstBool [c])) -> (ConstBool [1-c])
   154  
   155  // Convert x * 1 to x.
   156  (Mul(8|16|32|64)  (Const(8|16|32|64)  [1]) x) -> x
   157  
   158  // Convert x * -1 to -x.
   159  (Mul(8|16|32|64)  (Const(8|16|32|64)  [-1]) x) -> (Neg(8|16|32|64)  x)
   160  
   161  // Convert multiplication by a power of two to a shift.
   162  (Mul8  <t> n (Const8  [c])) && isPowerOfTwo(c) -> (Lsh8x64  <t> n (Const64 <typ.UInt64> [log2(c)]))
   163  (Mul16 <t> n (Const16 [c])) && isPowerOfTwo(c) -> (Lsh16x64 <t> n (Const64 <typ.UInt64> [log2(c)]))
   164  (Mul32 <t> n (Const32 [c])) && isPowerOfTwo(c) -> (Lsh32x64 <t> n (Const64 <typ.UInt64> [log2(c)]))
   165  (Mul64 <t> n (Const64 [c])) && isPowerOfTwo(c) -> (Lsh64x64 <t> n (Const64 <typ.UInt64> [log2(c)]))
   166  (Mul8  <t> n (Const8  [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg8  (Lsh8x64  <t> n (Const64 <typ.UInt64> [log2(-c)])))
   167  (Mul16 <t> n (Const16 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg16 (Lsh16x64 <t> n (Const64 <typ.UInt64> [log2(-c)])))
   168  (Mul32 <t> n (Const32 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg32 (Lsh32x64 <t> n (Const64 <typ.UInt64> [log2(-c)])))
   169  (Mul64 <t> n (Const64 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg64 (Lsh64x64 <t> n (Const64 <typ.UInt64> [log2(-c)])))
   170  
   171  (Mod8  (Const8  [c]) (Const8  [d])) && d != 0 -> (Const8  [int64(int8(c % d))])
   172  (Mod16 (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c % d))])
   173  (Mod32 (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c % d))])
   174  (Mod64 (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [c % d])
   175  
   176  (Mod8u  (Const8 [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(uint8(c) % uint8(d))])
   177  (Mod16u (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(uint16(c) % uint16(d))])
   178  (Mod32u (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(uint32(c) % uint32(d))])
   179  (Mod64u (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c) % uint64(d))])
   180  
   181  (Lsh64x64  (Const64 [c]) (Const64 [d])) -> (Const64 [c << uint64(d)])
   182  (Rsh64x64  (Const64 [c]) (Const64 [d])) -> (Const64 [c >> uint64(d)])
   183  (Rsh64Ux64 (Const64 [c]) (Const64 [d])) -> (Const64 [int64(uint64(c) >> uint64(d))])
   184  (Lsh32x64  (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) << uint64(d))])
   185  (Rsh32x64  (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) >> uint64(d))])
   186  (Rsh32Ux64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(uint32(c) >> uint64(d)))])
   187  (Lsh16x64  (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) << uint64(d))])
   188  (Rsh16x64  (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) >> uint64(d))])
   189  (Rsh16Ux64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(uint16(c) >> uint64(d)))])
   190  (Lsh8x64   (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(c) << uint64(d))])
   191  (Rsh8x64   (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(c) >> uint64(d))])
   192  (Rsh8Ux64  (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(uint8(c) >> uint64(d)))])
   193  
   194  // Fold IsInBounds when the range of the index cannot exceed the limit.
   195  (IsInBounds (ZeroExt8to32  _) (Const32 [c])) && (1 << 8)  <= c -> (ConstBool [1])
   196  (IsInBounds (ZeroExt8to64  _) (Const64 [c])) && (1 << 8)  <= c -> (ConstBool [1])
   197  (IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= c -> (ConstBool [1])
   198  (IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c -> (ConstBool [1])
   199  (IsInBounds x x) -> (ConstBool [0])
   200  (IsInBounds                (And8  (Const8  [c]) _)  (Const8  [d])) && 0 <= c && c < d -> (ConstBool [1])
   201  (IsInBounds (ZeroExt8to16  (And8  (Const8  [c]) _)) (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1])
   202  (IsInBounds (ZeroExt8to32  (And8  (Const8  [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
   203  (IsInBounds (ZeroExt8to64  (And8  (Const8  [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   204  (IsInBounds                (And16 (Const16 [c]) _)  (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1])
   205  (IsInBounds (ZeroExt16to32 (And16 (Const16 [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
   206  (IsInBounds (ZeroExt16to64 (And16 (Const16 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   207  (IsInBounds                (And32 (Const32 [c]) _)  (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
   208  (IsInBounds (ZeroExt32to64 (And32 (Const32 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   209  (IsInBounds                (And64 (Const64 [c]) _)  (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   210  (IsInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c < d)])
   211  (IsInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c < d)])
   212  // (Mod64u x y) is always between 0 (inclusive) and y (exclusive).
   213  (IsInBounds (Mod32u _ y) y) -> (ConstBool [1])
   214  (IsInBounds (Mod64u _ y) y) -> (ConstBool [1])
   215  // Right shifting a unsigned number limits its value.
   216  (IsInBounds (ZeroExt8to64  (Rsh8Ux64  _ (Const64 [c]))) (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
   217  (IsInBounds (ZeroExt8to32  (Rsh8Ux64  _ (Const64 [c]))) (Const32 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
   218  (IsInBounds (ZeroExt8to16  (Rsh8Ux64  _ (Const64 [c]))) (Const16 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
   219  (IsInBounds                (Rsh8Ux64  _ (Const64 [c]))  (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
   220  (IsInBounds (ZeroExt16to64 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1])
   221  (IsInBounds (ZeroExt16to32 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1])
   222  (IsInBounds                (Rsh16Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1])
   223  (IsInBounds (ZeroExt32to64 (Rsh32Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d -> (ConstBool [1])
   224  (IsInBounds                (Rsh32Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d -> (ConstBool [1])
   225  (IsInBounds                (Rsh64Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 64 && 1<<uint(64-c)-1 < d -> (ConstBool [1])
   226  
   227  (IsSliceInBounds x x) -> (ConstBool [1])
   228  (IsSliceInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c <= d -> (ConstBool [1])
   229  (IsSliceInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c <= d -> (ConstBool [1])
   230  (IsSliceInBounds (Const32 [0]) _) -> (ConstBool [1])
   231  (IsSliceInBounds (Const64 [0]) _) -> (ConstBool [1])
   232  (IsSliceInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c <= d)])
   233  (IsSliceInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c <= d)])
   234  (IsSliceInBounds (SliceLen x) (SliceCap x)) -> (ConstBool [1])
   235  
   236  (Eq(64|32|16|8) x x) -> (ConstBool [1])
   237  (EqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c == d)])
   238  (EqB (ConstBool [0]) x) -> (Not x)
   239  (EqB (ConstBool [1]) x) -> x
   240  
   241  (Neq(64|32|16|8) x x) -> (ConstBool [0])
   242  (NeqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c != d)])
   243  (NeqB (ConstBool [0]) x) -> x
   244  (NeqB (ConstBool [1]) x) -> (Not x)
   245  (NeqB (Not x) (Not y)) -> (NeqB x y)
   246  
   247  (Eq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Eq64 (Const64 <t> [c-d]) x)
   248  (Eq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Eq32 (Const32 <t> [int64(int32(c-d))]) x)
   249  (Eq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Eq16 (Const16 <t> [int64(int16(c-d))]) x)
   250  (Eq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Eq8  (Const8 <t> [int64(int8(c-d))]) x)
   251  
   252  (Neq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Neq64 (Const64 <t> [c-d]) x)
   253  (Neq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Neq32 (Const32 <t> [int64(int32(c-d))]) x)
   254  (Neq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Neq16 (Const16 <t> [int64(int16(c-d))]) x)
   255  (Neq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Neq8 (Const8 <t> [int64(int8(c-d))]) x)
   256  
   257  // Canonicalize x-const to x+(-const)
   258  (Sub64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Add64 (Const64 <t> [-c]) x)
   259  (Sub32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Add32 (Const32 <t> [int64(int32(-c))]) x)
   260  (Sub16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Add16 (Const16 <t> [int64(int16(-c))]) x)
   261  (Sub8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (Add8  (Const8  <t> [int64(int8(-c))]) x)
   262  
   263  // fold negation into comparison operators
   264  (Not (Eq(64|32|16|8|B) x y)) -> (Neq(64|32|16|8|B) x y)
   265  (Not (Neq(64|32|16|8|B) x y)) -> (Eq(64|32|16|8|B) x y)
   266  
   267  (Not (Greater(64|32|16|8) x y)) -> (Leq(64|32|16|8) x y)
   268  (Not (Greater(64|32|16|8)U x y)) -> (Leq(64|32|16|8)U x y)
   269  (Not (Geq(64|32|16|8) x y)) -> (Less(64|32|16|8) x y)
   270  (Not (Geq(64|32|16|8)U x y)) -> (Less(64|32|16|8)U x y)
   271  
   272  (Not (Less(64|32|16|8) x y)) -> (Geq(64|32|16|8) x y)
   273  (Not (Less(64|32|16|8)U x y)) -> (Geq(64|32|16|8)U x y)
   274  (Not (Leq(64|32|16|8) x y)) -> (Greater(64|32|16|8) x y)
   275  (Not (Leq(64|32|16|8)U x y)) -> (Greater(64|32|16|8)U x y)
   276  
   277  
   278  // Distribute multiplication c * (d+x) -> c*d + c*x. Useful for:
   279  // a[i].b = ...; a[i+1].b = ...
   280  (Mul64 (Const64 <t> [c]) (Add64 <t> (Const64 <t> [d]) x)) ->
   281    (Add64 (Const64 <t> [c*d]) (Mul64 <t> (Const64 <t> [c]) x))
   282  (Mul32 (Const32 <t> [c]) (Add32 <t> (Const32 <t> [d]) x)) ->
   283    (Add32 (Const32 <t> [int64(int32(c*d))]) (Mul32 <t> (Const32 <t> [c]) x))
   284  
   285  // Rewrite x*y + x*z  to  x*(y+z)
   286  (Add64 <t> (Mul64 x y) (Mul64 x z)) -> (Mul64 x (Add64 <t> y z))
   287  (Add32 <t> (Mul32 x y) (Mul32 x z)) -> (Mul32 x (Add32 <t> y z))
   288  (Add16 <t> (Mul16 x y) (Mul16 x z)) -> (Mul16 x (Add16 <t> y z))
   289  (Add8  <t> (Mul8  x y) (Mul8  x z)) -> (Mul8  x (Add8  <t> y z))
   290  
   291  // Rewrite x*y - x*z  to  x*(y-z)
   292  (Sub64 <t> (Mul64 x y) (Mul64 x z)) -> (Mul64 x (Sub64 <t> y z))
   293  (Sub32 <t> (Mul32 x y) (Mul32 x z)) -> (Mul32 x (Sub32 <t> y z))
   294  (Sub16 <t> (Mul16 x y) (Mul16 x z)) -> (Mul16 x (Sub16 <t> y z))
   295  (Sub8  <t> (Mul8  x y) (Mul8  x z)) -> (Mul8  x (Sub8  <t> y z))
   296  
   297  // rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce
   298  // the number of the other rewrite rules for const shifts
   299  (Lsh64x32  <t> x (Const32 [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint32(c))]))
   300  (Lsh64x16  <t> x (Const16 [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint16(c))]))
   301  (Lsh64x8   <t> x (Const8  [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint8(c))]))
   302  (Rsh64x32  <t> x (Const32 [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint32(c))]))
   303  (Rsh64x16  <t> x (Const16 [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint16(c))]))
   304  (Rsh64x8   <t> x (Const8  [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint8(c))]))
   305  (Rsh64Ux32 <t> x (Const32 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))]))
   306  (Rsh64Ux16 <t> x (Const16 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))]))
   307  (Rsh64Ux8  <t> x (Const8  [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))]))
   308  
   309  (Lsh32x32  <t> x (Const32 [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint32(c))]))
   310  (Lsh32x16  <t> x (Const16 [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint16(c))]))
   311  (Lsh32x8   <t> x (Const8  [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint8(c))]))
   312  (Rsh32x32  <t> x (Const32 [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint32(c))]))
   313  (Rsh32x16  <t> x (Const16 [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint16(c))]))
   314  (Rsh32x8   <t> x (Const8  [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint8(c))]))
   315  (Rsh32Ux32 <t> x (Const32 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))]))
   316  (Rsh32Ux16 <t> x (Const16 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))]))
   317  (Rsh32Ux8  <t> x (Const8  [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))]))
   318  
   319  (Lsh16x32  <t> x (Const32 [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint32(c))]))
   320  (Lsh16x16  <t> x (Const16 [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint16(c))]))
   321  (Lsh16x8   <t> x (Const8  [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint8(c))]))
   322  (Rsh16x32  <t> x (Const32 [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint32(c))]))
   323  (Rsh16x16  <t> x (Const16 [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint16(c))]))
   324  (Rsh16x8   <t> x (Const8  [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint8(c))]))
   325  (Rsh16Ux32 <t> x (Const32 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))]))
   326  (Rsh16Ux16 <t> x (Const16 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))]))
   327  (Rsh16Ux8  <t> x (Const8  [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))]))
   328  
   329  (Lsh8x32  <t> x (Const32 [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint32(c))]))
   330  (Lsh8x16  <t> x (Const16 [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint16(c))]))
   331  (Lsh8x8   <t> x (Const8  [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint8(c))]))
   332  (Rsh8x32  <t> x (Const32 [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint32(c))]))
   333  (Rsh8x16  <t> x (Const16 [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint16(c))]))
   334  (Rsh8x8   <t> x (Const8  [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint8(c))]))
   335  (Rsh8Ux32 <t> x (Const32 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))]))
   336  (Rsh8Ux16 <t> x (Const16 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))]))
   337  (Rsh8Ux8  <t> x (Const8  [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))]))
   338  
   339  // shifts by zero
   340  (Lsh64x64  x (Const64 [0])) -> x
   341  (Rsh64x64  x (Const64 [0])) -> x
   342  (Rsh64Ux64 x (Const64 [0])) -> x
   343  (Lsh32x64  x (Const64 [0])) -> x
   344  (Rsh32x64  x (Const64 [0])) -> x
   345  (Rsh32Ux64 x (Const64 [0])) -> x
   346  (Lsh16x64  x (Const64 [0])) -> x
   347  (Rsh16x64  x (Const64 [0])) -> x
   348  (Rsh16Ux64 x (Const64 [0])) -> x
   349  (Lsh8x64   x (Const64 [0])) -> x
   350  (Rsh8x64   x (Const64 [0])) -> x
   351  (Rsh8Ux64  x (Const64 [0])) -> x
   352  
   353  // zero shifted.
   354  (Lsh64x(64|32|16|8)  (Const64 [0]) _) -> (Const64 [0])
   355  (Rsh64x(64|32|16|8)  (Const64 [0]) _) -> (Const64 [0])
   356  (Rsh64Ux(64|32|16|8) (Const64 [0]) _) -> (Const64 [0])
   357  (Lsh32x(64|32|16|8)  (Const32 [0]) _) -> (Const32 [0])
   358  (Rsh32x(64|32|16|8)  (Const32 [0]) _) -> (Const32 [0])
   359  (Rsh32Ux(64|32|16|8) (Const32 [0]) _) -> (Const32 [0])
   360  (Lsh16x(64|32|16|8)  (Const16 [0]) _) -> (Const16 [0])
   361  (Rsh16x(64|32|16|8)  (Const16 [0]) _) -> (Const16 [0])
   362  (Rsh16Ux(64|32|16|8) (Const16 [0]) _) -> (Const16 [0])
   363  (Lsh8x(64|32|16|8)   (Const8  [0]) _) -> (Const8  [0])
   364  (Rsh8x(64|32|16|8)   (Const8  [0]) _) -> (Const8  [0])
   365  (Rsh8Ux(64|32|16|8)  (Const8  [0]) _) -> (Const8  [0])
   366  
   367  // large left shifts of all values, and right shifts of unsigned values
   368  ((Lsh64|Rsh64U)x64  _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0])
   369  ((Lsh32|Rsh32U)x64  _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0])
   370  ((Lsh16|Rsh16U)x64  _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0])
   371  ((Lsh8|Rsh8U)x64    _ (Const64 [c])) && uint64(c) >= 8  -> (Const8  [0])
   372  
   373  // combine const shifts
   374  (Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh64x64 x (Const64 <t> [c+d]))
   375  (Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh32x64 x (Const64 <t> [c+d]))
   376  (Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh16x64 x (Const64 <t> [c+d]))
   377  (Lsh8x64  <t> (Lsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh8x64  x (Const64 <t> [c+d]))
   378  
   379  (Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64x64 x (Const64 <t> [c+d]))
   380  (Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32x64 x (Const64 <t> [c+d]))
   381  (Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16x64 x (Const64 <t> [c+d]))
   382  (Rsh8x64  <t> (Rsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8x64  x (Const64 <t> [c+d]))
   383  
   384  (Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64Ux64 x (Const64 <t> [c+d]))
   385  (Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32Ux64 x (Const64 <t> [c+d]))
   386  (Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16Ux64 x (Const64 <t> [c+d]))
   387  (Rsh8Ux64  <t> (Rsh8Ux64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8Ux64  x (Const64 <t> [c+d]))
   388  
   389  // ((x >> c1) << c2) >> c3
   390  (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]))
   391    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   392    -> (Rsh(64|32|16|8)Ux64 x (Const64 <typ.UInt64> [c1-c2+c3]))
   393  
   394  // ((x << c1) >> c2) << c3
   395  (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]))
   396    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   397    -> (Lsh(64|32|16|8)x64 x (Const64 <typ.UInt64> [c1-c2+c3]))
   398  
   399  // replace shifts with zero extensions
   400  (Rsh16Ux64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) -> (ZeroExt8to16  (Trunc16to8  <typ.UInt8>  x))
   401  (Rsh32Ux64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (ZeroExt8to32  (Trunc32to8  <typ.UInt8>  x))
   402  (Rsh64Ux64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (ZeroExt8to64  (Trunc64to8  <typ.UInt8>  x))
   403  (Rsh32Ux64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (ZeroExt16to32 (Trunc32to16 <typ.UInt16> x))
   404  (Rsh64Ux64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (ZeroExt16to64 (Trunc64to16 <typ.UInt16> x))
   405  (Rsh64Ux64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (ZeroExt32to64 (Trunc64to32 <typ.UInt32> x))
   406  
   407  // replace shifts with sign extensions
   408  (Rsh16x64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) -> (SignExt8to16  (Trunc16to8  <typ.Int8>  x))
   409  (Rsh32x64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (SignExt8to32  (Trunc32to8  <typ.Int8>  x))
   410  (Rsh64x64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (SignExt8to64  (Trunc64to8  <typ.Int8>  x))
   411  (Rsh32x64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (SignExt16to32 (Trunc32to16 <typ.Int16> x))
   412  (Rsh64x64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (SignExt16to64 (Trunc64to16 <typ.Int16> x))
   413  (Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (SignExt32to64 (Trunc64to32 <typ.Int32> x))
   414  
   415  // constant comparisons
   416  (Eq(64|32|16|8)      (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c == d)])
   417  (Neq(64|32|16|8)     (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c != d)])
   418  (Greater(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c > d)])
   419  (Geq(64|32|16|8)     (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c >= d)])
   420  (Less(64|32|16|8)    (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c < d)])
   421  (Leq(64|32|16|8)     (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c <= d)])
   422  
   423  (Greater64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) > uint64(d))])
   424  (Greater32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) > uint32(d))])
   425  (Greater16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) > uint16(d))])
   426  (Greater8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  > uint8(d))])
   427  
   428  (Geq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) >= uint64(d))])
   429  (Geq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) >= uint32(d))])
   430  (Geq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) >= uint16(d))])
   431  (Geq8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  >= uint8(d))])
   432  
   433  (Less64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) < uint64(d))])
   434  (Less32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) < uint32(d))])
   435  (Less16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) < uint16(d))])
   436  (Less8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  < uint8(d))])
   437  
   438  (Leq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) <= uint64(d))])
   439  (Leq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) <= uint32(d))])
   440  (Leq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) <= uint16(d))])
   441  (Leq8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  <= uint8(d))])
   442  
   443  // constant floating point comparisons
   444  (Eq(64|32)F      (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) == i2f(d))])
   445  (Neq(64|32)F     (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) != i2f(d))])
   446  (Greater(64|32)F (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) > i2f(d))])
   447  (Geq(64|32)F     (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) >= i2f(d))])
   448  (Less(64|32)F    (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) < i2f(d))])
   449  (Leq(64|32)F     (Const(64|32)F [c]) (Const(64|32)F [d])) -> (ConstBool [b2i(i2f(c) <= i2f(d))])
   450  
   451  // simplifications
   452  (Or(64|32|16|8) x x) -> x
   453  (Or(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x
   454  (Or(64|32|16|8) (Const(64|32|16|8) [-1]) _) -> (Const(64|32|16|8) [-1])
   455  
   456  (And(64|32|16|8) x x) -> x
   457  (And(64|32|16|8) (Const(64|32|16|8) [-1]) x) -> x
   458  (And(64|32|16|8) (Const(64|32|16|8) [0]) _) -> (Const(64|32|16|8) [0])
   459  
   460  (Xor(64|32|16|8) x x) -> (Const(64|32|16|8) [0])
   461  (Xor(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x
   462  
   463  (Add(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x
   464  (Sub(64|32|16|8) x x) -> (Const(64|32|16|8) [0])
   465  (Mul(64|32|16|8) (Const(64|32|16|8) [0]) _) -> (Const(64|32|16|8) [0])
   466  
   467  (Com(64|32|16|8) (Com(64|32|16|8)  x)) -> x
   468  (Com(64|32|16|8) (Const(64|32|16|8) [c])) -> (Const(64|32|16|8) [^c])
   469  
   470  (Neg(64|32|16|8) (Sub(64|32|16|8) x y)) -> (Sub(64|32|16|8) y x)
   471  
   472  (Add8  (Const8  [1]) (Com8  x)) -> (Neg8  x)
   473  (Add16 (Const16 [1]) (Com16 x)) -> (Neg16 x)
   474  (Add32 (Const32 [1]) (Com32 x)) -> (Neg32 x)
   475  (Add64 (Const64 [1]) (Com64 x)) -> (Neg64 x)
   476  
   477  (And64 x (And64 x y)) -> (And64 x y)
   478  (And32 x (And32 x y)) -> (And32 x y)
   479  (And16 x (And16 x y)) -> (And16 x y)
   480  (And8  x (And8  x y)) -> (And8  x y)
   481  (Or64 x (Or64 x y)) -> (Or64 x y)
   482  (Or32 x (Or32 x y)) -> (Or32 x y)
   483  (Or16 x (Or16 x y)) -> (Or16 x y)
   484  (Or8  x (Or8  x y)) -> (Or8  x y)
   485  (Xor(64|32|16|8) x (Xor(64|32|16|8) x y)) -> y
   486  
   487  // Ands clear bits. Ors set bits.
   488  // If a subsequent Or will set all the bits
   489  // that an And cleared, we can skip the And.
   490  // This happens in bitmasking code like:
   491  //   x &^= 3 << shift // clear two old bits
   492  //   x  |= v << shift // set two new bits
   493  // when shift is a small constant and v ends up a constant 3.
   494  (Or8  (And8  x (Const8  [c2])) (Const8  <t> [c1])) && ^(c1 | c2) == 0 -> (Or8  (Const8  <t> [c1]) x)
   495  (Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 -> (Or16 (Const16 <t> [c1]) x)
   496  (Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 -> (Or32 (Const32 <t> [c1]) x)
   497  (Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 -> (Or64 (Const64 <t> [c1]) x)
   498  
   499  (Trunc64to8  (And64 (Const64 [y]) x)) && y&0xFF == 0xFF -> (Trunc64to8 x)
   500  (Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc64to16 x)
   501  (Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF -> (Trunc64to32 x)
   502  (Trunc32to8  (And32 (Const32 [y]) x)) && y&0xFF == 0xFF -> (Trunc32to8 x)
   503  (Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc32to16 x)
   504  (Trunc16to8  (And16 (Const16 [y]) x)) && y&0xFF == 0xFF -> (Trunc16to8 x)
   505  
   506  (ZeroExt8to64  (Trunc64to8  x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 -> x
   507  (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 -> x
   508  (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 -> x
   509  (ZeroExt8to32  (Trunc32to8  x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 -> x
   510  (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 -> x
   511  (ZeroExt8to16  (Trunc16to8  x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 -> x
   512  
   513  (SignExt8to64  (Trunc64to8  x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 -> x
   514  (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 -> x
   515  (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 -> x
   516  (SignExt8to32  (Trunc32to8  x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 -> x
   517  (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 -> x
   518  (SignExt8to16  (Trunc16to8  x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 -> x
   519  
   520  (Slicemask (Const32 [x])) && x > 0 -> (Const32 [-1])
   521  (Slicemask (Const32 [0]))          -> (Const32 [0])
   522  (Slicemask (Const64 [x])) && x > 0 -> (Const64 [-1])
   523  (Slicemask (Const64 [0]))          -> (Const64 [0])
   524  
   525  // Rewrite AND of consts as shifts if possible, slightly faster for 64 bit operands
   526  // leading zeros can be shifted left, then right
   527  (And64 <t> (Const64 [y]) x) && nlz(y) + nto(y) == 64 && nto(y) >= 32
   528    -> (Rsh64Ux64 (Lsh64x64 <t> x (Const64 <t> [nlz(y)])) (Const64 <t> [nlz(y)]))
   529  // trailing zeros can be shifted right, then left
   530  (And64 <t> (Const64 [y]) x) && nlo(y) + ntz(y) == 64 && ntz(y) >= 32
   531    -> (Lsh64x64 (Rsh64Ux64 <t> x (Const64 <t> [ntz(y)])) (Const64 <t> [ntz(y)]))
   532  
   533  // simplifications often used for lengths.  e.g. len(s[i:i+5])==5
   534  (Sub(64|32|16|8) (Add(64|32|16|8) x y) x) -> y
   535  (Sub(64|32|16|8) (Add(64|32|16|8) x y) y) -> x
   536  
   537  // basic phi simplifications
   538  (Phi (Const8  [c]) (Const8  [c])) -> (Const8  [c])
   539  (Phi (Const16 [c]) (Const16 [c])) -> (Const16 [c])
   540  (Phi (Const32 [c]) (Const32 [c])) -> (Const32 [c])
   541  (Phi (Const64 [c]) (Const64 [c])) -> (Const64 [c])
   542  
   543  // user nil checks
   544  (NeqPtr p (ConstNil)) -> (IsNonNil p)
   545  (EqPtr p (ConstNil)) -> (Not (IsNonNil p))
   546  (IsNonNil (ConstNil)) -> (ConstBool [0])
   547  
   548  // slice and interface comparisons
   549  // The frontend ensures that we can only compare against nil,
   550  // so we need only compare the first word (interface type or slice ptr).
   551  (EqInter x y)  -> (EqPtr  (ITab x) (ITab y))
   552  (NeqInter x y) -> (NeqPtr (ITab x) (ITab y))
   553  (EqSlice x y)  -> (EqPtr  (SlicePtr x) (SlicePtr y))
   554  (NeqSlice x y) -> (NeqPtr (SlicePtr x) (SlicePtr y))
   555  
   556  // Load of store of same address, with compatibly typed value and same size
   557  (Load <t1> p1 (Store {t2} p2 x _)) && isSamePtr(p1,p2) && t1.Compare(x.Type) == types.CMPeq && t1.Size() == t2.(*types.Type).Size() -> x
   558  
   559  // Pass constants through math.Float{32,64}bits and math.Float{32,64}frombits
   560  (Load <t1> p1 (Store {t2} p2 (Const64  [x]) _)) && isSamePtr(p1,p2) && t2.(*types.Type).Size() == 8 && is64BitFloat(t1) -> (Const64F [x])
   561  (Load <t1> p1 (Store {t2} p2 (Const32  [x]) _)) && isSamePtr(p1,p2) && t2.(*types.Type).Size() == 4 && is32BitFloat(t1) -> (Const32F [f2i(float64(math.Float32frombits(uint32(x))))])
   562  (Load <t1> p1 (Store {t2} p2 (Const64F [x]) _)) && isSamePtr(p1,p2) && t2.(*types.Type).Size() == 8 && is64BitInt(t1)   -> (Const64  [x])
   563  (Load <t1> p1 (Store {t2} p2 (Const32F [x]) _)) && isSamePtr(p1,p2) && t2.(*types.Type).Size() == 4 && is32BitInt(t1)   -> (Const32  [int64(int32(math.Float32bits(float32(i2f(x)))))])
   564  
   565  // Eliminate stores of values that have just been loaded from the same location.
   566  // We also handle the common case where there are some intermediate stores to non-overlapping struct fields.
   567  (Store {t1} p1 (Load <t2> p2 mem) mem) &&
   568  	isSamePtr(p1, p2) &&
   569  	t2.Size() == t1.(*types.Type).Size() -> mem
   570  (Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ oldmem)) &&
   571  	isSamePtr(p1, p2) &&
   572  	isSamePtr(p1, p3) &&
   573  	t2.Size() == t1.(*types.Type).Size() &&
   574  	!overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) -> mem
   575  (Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ (Store {t4} (OffPtr [o4] p4) _ oldmem))) &&
   576  	isSamePtr(p1, p2) &&
   577  	isSamePtr(p1, p3) &&
   578  	isSamePtr(p1, p4) &&
   579  	t2.Size() == t1.(*types.Type).Size() &&
   580  	!overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) &&
   581  	!overlap(o1, t2.Size(), o4, t4.(*types.Type).Size()) -> mem
   582  (Store {t1} (OffPtr [o1] p1) (Load <t2> (OffPtr [o1] p2) oldmem) mem:(Store {t3} (OffPtr [o3] p3) _ (Store {t4} (OffPtr [o4] p4) _ (Store {t5} (OffPtr [o5] p5) _ oldmem)))) &&
   583  	isSamePtr(p1, p2) &&
   584  	isSamePtr(p1, p3) &&
   585  	isSamePtr(p1, p4) &&
   586  	isSamePtr(p1, p5) &&
   587  	t2.Size() == t1.(*types.Type).Size() &&
   588  	!overlap(o1, t2.Size(), o3, t3.(*types.Type).Size()) &&
   589  	!overlap(o1, t2.Size(), o4, t4.(*types.Type).Size()) &&
   590  	!overlap(o1, t2.Size(), o5, t5.(*types.Type).Size()) -> mem
   591  
   592  // Collapse OffPtr
   593  (OffPtr (OffPtr p [b]) [a]) -> (OffPtr p [a+b])
   594  (OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq -> p
   595  
   596  // indexing operations
   597  // Note: bounds check has already been done
   598  (PtrIndex <t> ptr idx) && config.PtrSize == 4 -> (AddPtr ptr (Mul32 <typ.Int> idx (Const32 <typ.Int> [t.ElemType().Size()])))
   599  (PtrIndex <t> ptr idx) && config.PtrSize == 8 -> (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.ElemType().Size()])))
   600  
   601  // struct operations
   602  (StructSelect (StructMake1 x)) -> x
   603  (StructSelect [0] (StructMake2 x _)) -> x
   604  (StructSelect [1] (StructMake2 _ x)) -> x
   605  (StructSelect [0] (StructMake3 x _ _)) -> x
   606  (StructSelect [1] (StructMake3 _ x _)) -> x
   607  (StructSelect [2] (StructMake3 _ _ x)) -> x
   608  (StructSelect [0] (StructMake4 x _ _ _)) -> x
   609  (StructSelect [1] (StructMake4 _ x _ _)) -> x
   610  (StructSelect [2] (StructMake4 _ _ x _)) -> x
   611  (StructSelect [3] (StructMake4 _ _ _ x)) -> x
   612  
   613  (Load <t> _ _) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) ->
   614    (StructMake0)
   615  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) ->
   616    (StructMake1
   617      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem))
   618  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) ->
   619    (StructMake2
   620      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
   621      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem))
   622  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) ->
   623    (StructMake3
   624      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
   625      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
   626      (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem))
   627  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) ->
   628    (StructMake4
   629      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
   630      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
   631      (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem)
   632      (Load <t.FieldType(3)> (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] ptr) mem))
   633  
   634  (StructSelect [i] x:(Load <t> ptr mem)) && !fe.CanSSA(t) ->
   635    @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
   636  
   637  (Store _ (StructMake0) mem) -> mem
   638  (Store dst (StructMake1 <t> f0) mem) ->
   639    (Store {t.FieldType(0)} (OffPtr <t.FieldType(0).PtrTo()> [0] dst) f0 mem)
   640  (Store dst (StructMake2 <t> f0 f1) mem) ->
   641    (Store {t.FieldType(1)}
   642      (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   643      f1
   644      (Store {t.FieldType(0)}
   645        (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
   646          f0 mem))
   647  (Store dst (StructMake3 <t> f0 f1 f2) mem) ->
   648    (Store {t.FieldType(2)}
   649      (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
   650      f2
   651      (Store {t.FieldType(1)}
   652        (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   653        f1
   654        (Store {t.FieldType(0)}
   655          (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
   656            f0 mem)))
   657  (Store dst (StructMake4 <t> f0 f1 f2 f3) mem) ->
   658    (Store {t.FieldType(3)}
   659      (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] dst)
   660      f3
   661      (Store {t.FieldType(2)}
   662        (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
   663        f2
   664        (Store {t.FieldType(1)}
   665          (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   666          f1
   667          (Store {t.FieldType(0)}
   668            (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
   669              f0 mem))))
   670  
   671  // Putting struct{*byte} and similar into direct interfaces.
   672  (IMake typ (StructMake1 val)) -> (IMake typ val)
   673  (StructSelect [0] x:(IData _)) -> x
   674  
   675  // un-SSAable values use mem->mem copies
   676  (Store {t} dst (Load src mem) mem) && !fe.CanSSA(t.(*types.Type)) ->
   677  	(Move {t} [t.(*types.Type).Size()] dst src mem)
   678  (Store {t} dst (Load src mem) (VarDef {x} mem)) && !fe.CanSSA(t.(*types.Type)) ->
   679  	(Move {t} [t.(*types.Type).Size()] dst src (VarDef {x} mem))
   680  
   681  // array ops
   682  (ArraySelect (ArrayMake1 x)) -> x
   683  
   684  (Load <t> _ _) && t.IsArray() && t.NumElem() == 0 ->
   685    (ArrayMake0)
   686  
   687  (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) ->
   688    (ArrayMake1 (Load <t.ElemType()> ptr mem))
   689  
   690  (Store _ (ArrayMake0) mem) -> mem
   691  (Store dst (ArrayMake1 e) mem) -> (Store {e.Type} dst e mem)
   692  
   693  // Putting [1]{*byte} and similar into direct interfaces.
   694  (IMake typ (ArrayMake1 val)) -> (IMake typ val)
   695  (ArraySelect [0] x:(IData _)) -> x
   696  
   697  // string ops
   698  // Decomposing StringMake and lowering of StringPtr and StringLen
   699  // happens in a later pass, dec, so that these operations are available
   700  // to other passes for optimizations.
   701  (StringPtr (StringMake (Const64 <t> [c]) _)) -> (Const64 <t> [c])
   702  (StringLen (StringMake _ (Const64 <t> [c]))) -> (Const64 <t> [c])
   703  (ConstString {s}) && config.PtrSize == 4 && s.(string) == "" ->
   704    (StringMake (ConstNil) (Const32 <typ.Int> [0]))
   705  (ConstString {s}) && config.PtrSize == 8 && s.(string) == "" ->
   706    (StringMake (ConstNil) (Const64 <typ.Int> [0]))
   707  (ConstString {s}) && config.PtrSize == 4 && s.(string) != "" ->
   708    (StringMake
   709      (Addr <typ.BytePtr> {fe.StringData(s.(string))}
   710        (SB))
   711      (Const32 <typ.Int> [int64(len(s.(string)))]))
   712  (ConstString {s}) && config.PtrSize == 8 && s.(string) != "" ->
   713    (StringMake
   714      (Addr <typ.BytePtr> {fe.StringData(s.(string))}
   715        (SB))
   716      (Const64 <typ.Int> [int64(len(s.(string)))]))
   717  
   718  // slice ops
   719  // Only a few slice rules are provided here.  See dec.rules for
   720  // a more comprehensive set.
   721  (SliceLen (SliceMake _ (Const64 <t> [c]) _)) -> (Const64 <t> [c])
   722  (SliceCap (SliceMake _ _ (Const64 <t> [c]))) -> (Const64 <t> [c])
   723  (SliceLen (SliceMake _ (Const32 <t> [c]) _)) -> (Const32 <t> [c])
   724  (SliceCap (SliceMake _ _ (Const32 <t> [c]))) -> (Const32 <t> [c])
   725  (SlicePtr (SliceMake (SlicePtr x) _ _)) -> (SlicePtr x)
   726  (SliceLen (SliceMake _ (SliceLen x) _)) -> (SliceLen x)
   727  (SliceCap (SliceMake _ _ (SliceCap x))) -> (SliceCap x)
   728  (SliceCap (SliceMake _ _ (SliceLen x))) -> (SliceLen x)
   729  (ConstSlice) && config.PtrSize == 4 ->
   730    (SliceMake
   731      (ConstNil <v.Type.ElemType().PtrTo()>)
   732      (Const32 <typ.Int> [0])
   733      (Const32 <typ.Int> [0]))
   734  (ConstSlice) && config.PtrSize == 8 ->
   735    (SliceMake
   736      (ConstNil <v.Type.ElemType().PtrTo()>)
   737      (Const64 <typ.Int> [0])
   738      (Const64 <typ.Int> [0]))
   739  
   740  // interface ops
   741  (ConstInterface) ->
   742    (IMake
   743      (ConstNil <typ.BytePtr>)
   744      (ConstNil <typ.BytePtr>))
   745  
   746  (NilCheck (GetG mem) mem) -> mem
   747  
   748  (If (Not cond) yes no) -> (If cond no yes)
   749  (If (ConstBool [c]) yes no) && c == 1 -> (First nil yes no)
   750  (If (ConstBool [c]) yes no) && c == 0 -> (First nil no yes)
   751  
   752  // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
   753  (Convert (Add64 (Convert ptr mem) off) mem) -> (Add64 ptr off)
   754  (Convert (Convert ptr mem) mem) -> ptr
   755  
   756  // Decompose compound argument values
   757  (Arg {n} [off]) && v.Type.IsString() ->
   758    (StringMake
   759      (Arg <typ.BytePtr> {n} [off])
   760      (Arg <typ.Int> {n} [off+config.PtrSize]))
   761  
   762  (Arg {n} [off]) && v.Type.IsSlice() ->
   763    (SliceMake
   764      (Arg <v.Type.ElemType().PtrTo()> {n} [off])
   765      (Arg <typ.Int> {n} [off+config.PtrSize])
   766      (Arg <typ.Int> {n} [off+2*config.PtrSize]))
   767  
   768  (Arg {n} [off]) && v.Type.IsInterface() ->
   769    (IMake
   770      (Arg <typ.BytePtr> {n} [off])
   771      (Arg <typ.BytePtr> {n} [off+config.PtrSize]))
   772  
   773  (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 16 ->
   774    (ComplexMake
   775      (Arg <typ.Float64> {n} [off])
   776      (Arg <typ.Float64> {n} [off+8]))
   777  
   778  (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 8 ->
   779    (ComplexMake
   780      (Arg <typ.Float32> {n} [off])
   781      (Arg <typ.Float32> {n} [off+4]))
   782  
   783  (Arg <t>) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) ->
   784    (StructMake0)
   785  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) ->
   786    (StructMake1
   787      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]))
   788  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) ->
   789    (StructMake2
   790      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
   791      (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)]))
   792  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) ->
   793    (StructMake3
   794      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
   795      (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])
   796      (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)]))
   797  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) ->
   798    (StructMake4
   799      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
   800      (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])
   801      (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)])
   802      (Arg <t.FieldType(3)> {n} [off+t.FieldOff(3)]))
   803  
   804  (Arg <t>) && t.IsArray() && t.NumElem() == 0 ->
   805    (ArrayMake0)
   806  (Arg <t> {n} [off]) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) ->
   807    (ArrayMake1 (Arg <t.ElemType()> {n} [off]))
   808  
   809  // strength reduction of divide by a constant.
   810  // See ../magic.go for a detailed description of these algorithms.
   811  
   812  // Unsigned divide by power of 2.  Strength reduce to a shift.
   813  (Div8u  n (Const8  [c])) && isPowerOfTwo(c&0xff)       -> (Rsh8Ux64 n  (Const64 <typ.UInt64> [log2(c&0xff)]))
   814  (Div16u n (Const16 [c])) && isPowerOfTwo(c&0xffff)     -> (Rsh16Ux64 n (Const64 <typ.UInt64> [log2(c&0xffff)]))
   815  (Div32u n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <typ.UInt64> [log2(c&0xffffffff)]))
   816  (Div64u n (Const64 [c])) && isPowerOfTwo(c)            -> (Rsh64Ux64 n (Const64 <typ.UInt64> [log2(c)]))
   817  (Div64u n (Const64 [-1<<63]))                          -> (Rsh64Ux64 n (Const64 <typ.UInt64> [63]))
   818  
   819  // Signed non-negative divide by power of 2.
   820  (Div8  n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo(c&0xff)       -> (Rsh8Ux64 n  (Const64 <typ.UInt64> [log2(c&0xff)]))
   821  (Div16 n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffff)     -> (Rsh16Ux64 n (Const64 <typ.UInt64> [log2(c&0xffff)]))
   822  (Div32 n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <typ.UInt64> [log2(c&0xffffffff)]))
   823  (Div64 n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c)            -> (Rsh64Ux64 n (Const64 <typ.UInt64> [log2(c)]))
   824  (Div64 n (Const64 [-1<<63])) && isNonNegative(n)                          -> (Const64 [0])
   825  
   826  // Unsigned divide, not a power of 2.  Strength reduce to a multiply.
   827  // For 8-bit divides, we just do a direct 9-bit by 8-bit multiply.
   828  (Div8u x (Const8 [c])) && umagicOK(8, c) ->
   829    (Trunc32to8
   830      (Rsh32Ux64 <typ.UInt32>
   831        (Mul32 <typ.UInt32>
   832          (Const32 <typ.UInt32> [int64(1<<8+umagic(8,c).m)])
   833          (ZeroExt8to32 x))
   834        (Const64 <typ.UInt64> [8+umagic(8,c).s])))
   835  
   836  // For 16-bit divides on 64-bit machines, we do a direct 17-bit by 16-bit multiply.
   837  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 8 ->
   838    (Trunc64to16
   839      (Rsh64Ux64 <typ.UInt64>
   840        (Mul64 <typ.UInt64>
   841          (Const64 <typ.UInt64> [int64(1<<16+umagic(16,c).m)])
   842          (ZeroExt16to64 x))
   843        (Const64 <typ.UInt64> [16+umagic(16,c).s])))
   844  
   845  // For 16-bit divides on 32-bit machines
   846  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && umagic(16,c).m&1 == 0 ->
   847    (Trunc32to16
   848      (Rsh32Ux64 <typ.UInt32>
   849        (Mul32 <typ.UInt32>
   850          (Const32 <typ.UInt32> [int64(1<<15+umagic(16,c).m/2)])
   851          (ZeroExt16to32 x))
   852        (Const64 <typ.UInt64> [16+umagic(16,c).s-1])))
   853  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && c&1 == 0 ->
   854    (Trunc32to16
   855      (Rsh32Ux64 <typ.UInt32>
   856        (Mul32 <typ.UInt32>
   857          (Const32 <typ.UInt32> [int64(1<<15+(umagic(16,c).m+1)/2)])
   858          (Rsh32Ux64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [1])))
   859        (Const64 <typ.UInt64> [16+umagic(16,c).s-2])))
   860  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 ->
   861    (Trunc32to16
   862      (Rsh32Ux64 <typ.UInt32>
   863        (Avg32u
   864          (Lsh32x64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [16]))
   865          (Mul32 <typ.UInt32>
   866            (Const32 <typ.UInt32> [int64(umagic(16,c).m)])
   867            (ZeroExt16to32 x)))
   868        (Const64 <typ.UInt64> [16+umagic(16,c).s-1])))
   869  
   870  // For 32-bit divides on 32-bit machines
   871  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && umagic(32,c).m&1 == 0 ->
   872    (Rsh32Ux64 <typ.UInt32>
   873      (Hmul32u <typ.UInt32>
   874        (Const32 <typ.UInt32> [int64(int32(1<<31+umagic(32,c).m/2))])
   875        x)
   876      (Const64 <typ.UInt64> [umagic(32,c).s-1]))
   877  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && c&1 == 0 ->
   878    (Rsh32Ux64 <typ.UInt32>
   879      (Hmul32u <typ.UInt32>
   880        (Const32 <typ.UInt32> [int64(int32(1<<31+(umagic(32,c).m+1)/2))])
   881        (Rsh32Ux64 <typ.UInt32> x (Const64 <typ.UInt64> [1])))
   882      (Const64 <typ.UInt64> [umagic(32,c).s-2]))
   883  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 ->
   884    (Rsh32Ux64 <typ.UInt32>
   885      (Avg32u
   886        x
   887        (Hmul32u <typ.UInt32>
   888          (Const32 <typ.UInt32> [int64(int32(umagic(32,c).m))])
   889          x))
   890      (Const64 <typ.UInt64> [umagic(32,c).s-1]))
   891  
   892  // For 32-bit divides on 64-bit machines
   893  // We'll use a regular (non-hi) multiply for this case.
   894  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && umagic(32,c).m&1 == 0 ->
   895    (Trunc64to32
   896      (Rsh64Ux64 <typ.UInt64>
   897        (Mul64 <typ.UInt64>
   898          (Const64 <typ.UInt64> [int64(1<<31+umagic(32,c).m/2)])
   899          (ZeroExt32to64 x))
   900        (Const64 <typ.UInt64> [32+umagic(32,c).s-1])))
   901  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && c&1 == 0 ->
   902    (Trunc64to32
   903      (Rsh64Ux64 <typ.UInt64>
   904        (Mul64 <typ.UInt64>
   905          (Const64 <typ.UInt64> [int64(1<<31+(umagic(32,c).m+1)/2)])
   906          (Rsh64Ux64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [1])))
   907        (Const64 <typ.UInt64> [32+umagic(32,c).s-2])))
   908  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 ->
   909    (Trunc64to32
   910      (Rsh64Ux64 <typ.UInt64>
   911        (Avg64u
   912          (Lsh64x64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [32]))
   913          (Mul64 <typ.UInt64>
   914            (Const64 <typ.UInt32> [int64(umagic(32,c).m)])
   915            (ZeroExt32to64 x)))
   916        (Const64 <typ.UInt64> [32+umagic(32,c).s-1])))
   917  
   918  // For 64-bit divides on 64-bit machines
   919  // (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.)
   920  (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && umagic(64,c).m&1 == 0 ->
   921    (Rsh64Ux64 <typ.UInt64>
   922      (Hmul64u <typ.UInt64>
   923        (Const64 <typ.UInt64> [int64(1<<63+umagic(64,c).m/2)])
   924        x)
   925      (Const64 <typ.UInt64> [umagic(64,c).s-1]))
   926  (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && c&1 == 0 ->
   927    (Rsh64Ux64 <typ.UInt64>
   928      (Hmul64u <typ.UInt64>
   929        (Const64 <typ.UInt64> [int64(1<<63+(umagic(64,c).m+1)/2)])
   930        (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [1])))
   931      (Const64 <typ.UInt64> [umagic(64,c).s-2]))
   932  (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 ->
   933    (Rsh64Ux64 <typ.UInt64>
   934      (Avg64u
   935        x
   936        (Hmul64u <typ.UInt64>
   937          (Const64 <typ.UInt64> [int64(umagic(64,c).m)])
   938          x))
   939      (Const64 <typ.UInt64> [umagic(64,c).s-1]))
   940  
   941  // Signed divide by a negative constant.  Rewrite to divide by a positive constant.
   942  (Div8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  -> (Neg8  (Div8  <t> n (Const8  <t> [-c])))
   943  (Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Neg16 (Div16 <t> n (Const16 <t> [-c])))
   944  (Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Neg32 (Div32 <t> n (Const32 <t> [-c])))
   945  (Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Neg64 (Div64 <t> n (Const64 <t> [-c])))
   946  
   947  // Dividing by the most-negative number.  Result is always 0 except
   948  // if the input is also the most-negative number.
   949  // We can detect that using the sign bit of x & -x.
   950  (Div8  <t> x (Const8  [-1<<7 ])) -> (Rsh8Ux64  (And8  <t> x (Neg8  <t> x)) (Const64 <typ.UInt64> [7 ]))
   951  (Div16 <t> x (Const16 [-1<<15])) -> (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15]))
   952  (Div32 <t> x (Const32 [-1<<31])) -> (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31]))
   953  (Div64 <t> x (Const64 [-1<<63])) -> (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63]))
   954  
   955  // Signed divide by power of 2.
   956  // n / c =       n >> log(c) if n >= 0
   957  //       = (n+c-1) >> log(c) if n < 0
   958  // We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned).
   959  (Div8  <t> n (Const8  [c])) && isPowerOfTwo(c) ->
   960    (Rsh8x64
   961      (Add8  <t> n (Rsh8Ux64  <t> (Rsh8x64  <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [ 8-log2(c)])))
   962      (Const64 <typ.UInt64> [log2(c)]))
   963  (Div16 <t> n (Const16 [c])) && isPowerOfTwo(c) ->
   964    (Rsh16x64
   965      (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [16-log2(c)])))
   966      (Const64 <typ.UInt64> [log2(c)]))
   967  (Div32 <t> n (Const32 [c])) && isPowerOfTwo(c) ->
   968    (Rsh32x64
   969      (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [32-log2(c)])))
   970      (Const64 <typ.UInt64> [log2(c)]))
   971  (Div64 <t> n (Const64 [c])) && isPowerOfTwo(c) ->
   972    (Rsh64x64
   973      (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [64-log2(c)])))
   974      (Const64 <typ.UInt64> [log2(c)]))
   975  
   976  // Signed divide, not a power of 2.  Strength reduce to a multiply.
   977  (Div8 <t> x (Const8 [c])) && smagicOK(8,c) ->
   978    (Sub8 <t>
   979      (Rsh32x64 <t>
   980        (Mul32 <typ.UInt32>
   981          (Const32 <typ.UInt32> [int64(smagic(8,c).m)])
   982          (SignExt8to32 x))
   983        (Const64 <typ.UInt64> [8+smagic(8,c).s]))
   984      (Rsh32x64 <t>
   985        (SignExt8to32 x)
   986        (Const64 <typ.UInt64> [31])))
   987  (Div16 <t> x (Const16 [c])) && smagicOK(16,c) ->
   988    (Sub16 <t>
   989      (Rsh32x64 <t>
   990        (Mul32 <typ.UInt32>
   991          (Const32 <typ.UInt32> [int64(smagic(16,c).m)])
   992          (SignExt16to32 x))
   993        (Const64 <typ.UInt64> [16+smagic(16,c).s]))
   994      (Rsh32x64 <t>
   995        (SignExt16to32 x)
   996        (Const64 <typ.UInt64> [31])))
   997  (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 8 ->
   998    (Sub32 <t>
   999      (Rsh64x64 <t>
  1000        (Mul64 <typ.UInt64>
  1001          (Const64 <typ.UInt64> [int64(smagic(32,c).m)])
  1002          (SignExt32to64 x))
  1003        (Const64 <typ.UInt64> [32+smagic(32,c).s]))
  1004      (Rsh64x64 <t>
  1005        (SignExt32to64 x)
  1006        (Const64 <typ.UInt64> [63])))
  1007  (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 == 0 ->
  1008    (Sub32 <t>
  1009      (Rsh32x64 <t>
  1010        (Hmul32 <t>
  1011          (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m/2))])
  1012          x)
  1013        (Const64 <typ.UInt64> [smagic(32,c).s-1]))
  1014      (Rsh32x64 <t>
  1015        x
  1016        (Const64 <typ.UInt64> [31])))
  1017  (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 != 0 ->
  1018    (Sub32 <t>
  1019      (Rsh32x64 <t>
  1020        (Add32 <t>
  1021          (Hmul32 <t>
  1022            (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m))])
  1023            x)
  1024          x)
  1025        (Const64 <typ.UInt64> [smagic(32,c).s]))
  1026      (Rsh32x64 <t>
  1027        x
  1028        (Const64 <typ.UInt64> [31])))
  1029  (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 == 0 ->
  1030    (Sub64 <t>
  1031      (Rsh64x64 <t>
  1032        (Hmul64 <t>
  1033          (Const64 <typ.UInt64> [int64(smagic(64,c).m/2)])
  1034          x)
  1035        (Const64 <typ.UInt64> [smagic(64,c).s-1]))
  1036      (Rsh64x64 <t>
  1037        x
  1038        (Const64 <typ.UInt64> [63])))
  1039  (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 != 0 ->
  1040    (Sub64 <t>
  1041      (Rsh64x64 <t>
  1042        (Add64 <t>
  1043          (Hmul64 <t>
  1044            (Const64 <typ.UInt64> [int64(smagic(64,c).m)])
  1045            x)
  1046          x)
  1047        (Const64 <typ.UInt64> [smagic(64,c).s]))
  1048      (Rsh64x64 <t>
  1049        x
  1050        (Const64 <typ.UInt64> [63])))
  1051  
  1052  // Unsigned mod by power of 2 constant.
  1053  (Mod8u  <t> n (Const8  [c])) && isPowerOfTwo(c&0xff)       -> (And8 n (Const8 <t> [(c&0xff)-1]))
  1054  (Mod16u <t> n (Const16 [c])) && isPowerOfTwo(c&0xffff)     -> (And16 n (Const16 <t> [(c&0xffff)-1]))
  1055  (Mod32u <t> n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1]))
  1056  (Mod64u <t> n (Const64 [c])) && isPowerOfTwo(c)            -> (And64 n (Const64 <t> [c-1]))
  1057  (Mod64u <t> n (Const64 [-1<<63]))                          -> (And64 n (Const64 <t> [1<<63-1]))
  1058  
  1059  // Signed non-negative mod by power of 2 constant.
  1060  (Mod8  <t> n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo(c&0xff)       -> (And8 n (Const8 <t> [(c&0xff)-1]))
  1061  (Mod16 <t> n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffff)     -> (And16 n (Const16 <t> [(c&0xffff)-1]))
  1062  (Mod32 <t> n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1]))
  1063  (Mod64 <t> n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c)            -> (And64 n (Const64 <t> [c-1]))
  1064  (Mod64 n (Const64 [-1<<63])) && isNonNegative(n)                              -> n
  1065  
  1066  // Signed mod by negative constant.
  1067  (Mod8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  -> (Mod8  <t> n (Const8  <t> [-c]))
  1068  (Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Mod16 <t> n (Const16 <t> [-c]))
  1069  (Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Mod32 <t> n (Const32 <t> [-c]))
  1070  (Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Mod64 <t> n (Const64 <t> [-c]))
  1071  
  1072  // All other mods by constants, do A%B = A-(A/B*B).
  1073  // This implements % with two * and a bunch of ancillary ops.
  1074  // One of the * is free if the user's code also computes A/B.
  1075  (Mod8   <t> x (Const8  [c])) && x.Op != OpConst8  && (c > 0 || c == -1<<7)
  1076    -> (Sub8  x (Mul8  <t> (Div8   <t> x (Const8  <t> [c])) (Const8  <t> [c])))
  1077  (Mod16  <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15)
  1078    -> (Sub16 x (Mul16 <t> (Div16  <t> x (Const16 <t> [c])) (Const16 <t> [c])))
  1079  (Mod32  <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31)
  1080    -> (Sub32 x (Mul32 <t> (Div32  <t> x (Const32 <t> [c])) (Const32 <t> [c])))
  1081  (Mod64  <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63)
  1082    -> (Sub64 x (Mul64 <t> (Div64  <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1083  (Mod8u  <t> x (Const8  [c])) && x.Op != OpConst8  && c > 0 && umagicOK(8 ,c)
  1084    -> (Sub8  x (Mul8  <t> (Div8u  <t> x (Const8  <t> [c])) (Const8  <t> [c])))
  1085  (Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c > 0 && umagicOK(16,c)
  1086    -> (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c])))
  1087  (Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c > 0 && umagicOK(32,c)
  1088    -> (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c])))
  1089  (Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c > 0 && umagicOK(64,c)
  1090    -> (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1091  
  1092  // Reassociate expressions involving
  1093  // constants such that constants come first,
  1094  // exposing obvious constant-folding opportunities.
  1095  // Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C
  1096  // is constant, which pushes constants to the outside
  1097  // of the expression. At that point, any constant-folding
  1098  // opportunities should be obvious.
  1099  
  1100  // x + (C + z) -> C + (x + z)
  1101  (Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Add64 <t> z x))
  1102  (Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Add32 <t> z x))
  1103  (Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Add16 <t> z x))
  1104  (Add8  (Add8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Add8  <t> z x))
  1105  
  1106  // x + (C - z) -> C + (x - z)
  1107  (Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
  1108  (Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
  1109  (Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
  1110  (Add8  (Sub8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))
  1111  (Add64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
  1112  (Add32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
  1113  (Add16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
  1114  (Add8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))
  1115  
  1116  // x + (z - C) -> (x + z) - C
  1117  (Add64 (Sub64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
  1118  (Add32 (Sub32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
  1119  (Add16 (Sub16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
  1120  (Add8  (Sub8  z i:(Const8  <t>)) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)
  1121  (Add64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
  1122  (Add32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
  1123  (Add16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
  1124  (Add8  x (Sub8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)
  1125  
  1126  // x - (C - z) -> x + (z - C) -> (x + z) - C
  1127  (Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
  1128  (Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
  1129  (Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
  1130  (Sub8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)
  1131  
  1132  // x - (z - C) -> x + (C - z) -> (x - z) + C
  1133  (Sub64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
  1134  (Sub32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
  1135  (Sub16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
  1136  (Sub8  x (Sub8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))
  1137  
  1138  // x & (C & z) -> C & (x & z)
  1139  (And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (And64 i (And64 <t> z x))
  1140  (And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (And32 i (And32 <t> z x))
  1141  (And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (And16 i (And16 <t> z x))
  1142  (And8  (And8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (And8  i (And8  <t> z x))
  1143  
  1144  // x | (C | z) -> C | (x | z)
  1145  (Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Or64 i (Or64 <t> z x))
  1146  (Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Or32 i (Or32 <t> z x))
  1147  (Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Or16 i (Or16 <t> z x))
  1148  (Or8  (Or8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Or8  i (Or8  <t> z x))
  1149  
  1150  // x ^ (C ^ z) -> C ^ (x ^ z)
  1151  (Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Xor64 i (Xor64 <t> z x))
  1152  (Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Xor32 i (Xor32 <t> z x))
  1153  (Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Xor16 i (Xor16 <t> z x))
  1154  (Xor8  (Xor8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Xor8  i (Xor8  <t> z x))
  1155  
  1156  // C + (D + x) -> (C + D) + x
  1157  (Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c+d]) x)
  1158  (Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c+d))]) x)
  1159  (Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c+d))]) x)
  1160  (Add8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Add8  (Const8  <t> [int64(int8(c+d))]) x)
  1161  
  1162  // C + (D - x) -> (C + D) - x
  1163  (Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Sub64 (Const64 <t> [c+d]) x)
  1164  (Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x)
  1165  (Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x)
  1166  (Add8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) -> (Sub8  (Const8  <t> [int64(int8(c+d))]) x)
  1167  
  1168  // C + (x - D) -> (C - D) + x
  1169  (Add64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Add64 (Const64 <t> [c-d]) x)
  1170  (Add32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x)
  1171  (Add16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x)
  1172  (Add8  (Const8  <t> [c]) (Sub8  x (Const8  <t> [d]))) -> (Add8  (Const8  <t> [int64(int8(c-d))]) x)
  1173  
  1174  // C - (x - D) -> (C + D) - x
  1175  (Sub64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Sub64 (Const64 <t> [c+d]) x)
  1176  (Sub32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x)
  1177  (Sub16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x)
  1178  (Sub8  (Const8  <t> [c]) (Sub8  x (Const8  <t> [d]))) -> (Sub8  (Const8  <t> [int64(int8(c+d))]) x)
  1179  
  1180  // C - (D - x) -> (C - D) + x
  1181  (Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c-d]) x)
  1182  (Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x)
  1183  (Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x)
  1184  (Sub8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) -> (Add8  (Const8  <t> [int64(int8(c-d))]) x)
  1185  
  1186  // C & (D & x) -> (C & D) & x
  1187  (And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) -> (And64 (Const64 <t> [c&d]) x)
  1188  (And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) -> (And32 (Const32 <t> [int64(int32(c&d))]) x)
  1189  (And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) -> (And16 (Const16 <t> [int64(int16(c&d))]) x)
  1190  (And8  (Const8  <t> [c]) (And8  (Const8  <t> [d]) x)) -> (And8  (Const8  <t> [int64(int8(c&d))]) x)
  1191  
  1192  // C | (D | x) -> (C | D) | x
  1193  (Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) -> (Or64 (Const64 <t> [c|d]) x)
  1194  (Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) -> (Or32 (Const32 <t> [int64(int32(c|d))]) x)
  1195  (Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) -> (Or16 (Const16 <t> [int64(int16(c|d))]) x)
  1196  (Or8  (Const8  <t> [c]) (Or8  (Const8  <t> [d]) x)) -> (Or8  (Const8  <t> [int64(int8(c|d))]) x)
  1197  
  1198  // C ^ (D ^ x) -> (C ^ D) ^ x
  1199  (Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) -> (Xor64 (Const64 <t> [c^d]) x)
  1200  (Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) -> (Xor32 (Const32 <t> [int64(int32(c^d))]) x)
  1201  (Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) -> (Xor16 (Const16 <t> [int64(int16(c^d))]) x)
  1202  (Xor8  (Const8  <t> [c]) (Xor8  (Const8  <t> [d]) x)) -> (Xor8  (Const8  <t> [int64(int8(c^d))]) x)
  1203  
  1204  // C * (D * x) = (C * D) * x
  1205  (Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) -> (Mul64 (Const64 <t> [c*d]) x)
  1206  (Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) -> (Mul32 (Const32 <t> [int64(int32(c*d))]) x)
  1207  (Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) -> (Mul16 (Const16 <t> [int64(int16(c*d))]) x)
  1208  (Mul8  (Const8  <t> [c]) (Mul8  (Const8  <t> [d]) x)) -> (Mul8  (Const8  <t> [int64(int8(c*d))]) x)
  1209  
  1210  // floating point optimizations
  1211  (Add(32|64)F x (Const(32|64)F [0])) -> x
  1212  (Sub(32|64)F x (Const(32|64)F [0])) -> x
  1213  
  1214  (Mul(32|64)F x (Const(32|64)F [f2i(1)])) -> x
  1215  (Mul32F x (Const32F [f2i(-1)])) -> (Neg32F x)
  1216  (Mul64F x (Const64F [f2i(-1)])) -> (Neg64F x)
  1217  (Mul32F x (Const32F [f2i(2)])) -> (Add32F x x)
  1218  (Mul64F x (Const64F [f2i(2)])) -> (Add64F x x)
  1219  
  1220  (Div32F x (Const32F <t> [c])) && reciprocalExact32(float32(i2f(c))) -> (Mul32F x (Const32F <t> [f2i(1/i2f(c))]))
  1221  (Div64F x (Const64F <t> [c])) && reciprocalExact64(i2f(c))          -> (Mul64F x (Const64F <t> [f2i(1/i2f(c))]))
  1222  
  1223  (Sqrt (Const64F [c])) -> (Const64F [f2i(math.Sqrt(i2f(c)))])
  1224  
  1225  // recognize runtime.newobject and don't Zero/Nilcheck it
  1226  (Zero (Load (OffPtr [c] (SP)) mem) mem)
  1227  	&& mem.Op == OpStaticCall
  1228  	&& isSameSym(mem.Aux, "runtime.newobject")
  1229  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1230  	-> mem
  1231  (Store (Load (OffPtr [c] (SP)) mem) x mem)
  1232  	&& isConstZero(x)
  1233  	&& mem.Op == OpStaticCall
  1234  	&& isSameSym(mem.Aux, "runtime.newobject")
  1235  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1236  	-> mem
  1237  (Store (OffPtr (Load (OffPtr [c] (SP)) mem)) x mem)
  1238  	&& isConstZero(x)
  1239  	&& mem.Op == OpStaticCall
  1240  	&& isSameSym(mem.Aux, "runtime.newobject")
  1241  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1242  	-> mem
  1243  // nil checks just need to rewrite to something useless.
  1244  // they will be deadcode eliminated soon afterwards.
  1245  (NilCheck (Load (OffPtr [c] (SP)) (StaticCall {sym} _)) _)
  1246  	&& isSameSym(sym, "runtime.newobject")
  1247  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1248  	&& warnRule(fe.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check")
  1249  	-> (Invalid)
  1250  (NilCheck (OffPtr (Load (OffPtr [c] (SP)) (StaticCall {sym} _))) _)
  1251  	&& isSameSym(sym, "runtime.newobject")
  1252  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1253  	&& warnRule(fe.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check")
  1254  	-> (Invalid)
  1255  
  1256  // Address comparison shows up in type assertions.
  1257  (EqPtr x x) -> (ConstBool [1])
  1258  (EqPtr (Addr {a} x) (Addr {b} x)) -> (ConstBool [b2i(a == b)])
  1259  
  1260  // Inline small runtime.memmove calls with constant length.
  1261  (StaticCall {sym} s1:(Store _ (Const64 [sz]) s2:(Store  _ src s3:(Store {t} _ dst mem))))
  1262       && isSameSym(sym,"runtime.memmove") && s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1 && isInlinableMemmoveSize(sz,config)
  1263       -> (Move {t.(*types.Type).Elem()} [sz] dst src mem)
  1264  (StaticCall {sym} s1:(Store _ (Const32 [sz]) s2:(Store  _ src s3:(Store {t} _ dst mem))))
  1265       && isSameSym(sym,"runtime.memmove") && s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1 && isInlinableMemmoveSize(sz,config)
  1266       -> (Move {t.(*types.Type).Elem()} [sz] dst src mem)
  1267  
  1268  // De-virtualize interface calls into static calls.
  1269  // Note that (ITab (IMake)) doesn't get
  1270  // rewritten until after the first opt pass,
  1271  // so this rule should trigger reliably.
  1272  (InterCall [argsize] (Load (OffPtr [off] (ITab (IMake (Addr {itab} (SB)) _))) _) mem) && devirt(v, itab, off) != nil ->
  1273  	(StaticCall [argsize] {devirt(v, itab, off)} mem)