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