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