github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/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 [auxFrom32F(float32(auxTo64F(c)))])
    48  (Cvt32Fto64F (Const32F [c])) -> (Const64F [c]) // c is already a 64 bit float
    49  (Cvt32to32F  (Const32  [c])) -> (Const32F [auxFrom32F(float32(int32(c)))])
    50  (Cvt32to64F  (Const32  [c])) -> (Const64F [auxFrom64F(float64(int32(c)))])
    51  (Cvt64to32F  (Const64  [c])) -> (Const32F [auxFrom32F(float32(c))])
    52  (Cvt64to64F  (Const64  [c])) -> (Const64F [auxFrom64F(float64(c))])
    53  (Cvt32Fto32  (Const32F [c])) -> (Const32  [int64(int32(auxTo32F(c)))])
    54  (Cvt32Fto64  (Const32F [c])) -> (Const64  [int64(auxTo32F(c))])
    55  (Cvt64Fto32  (Const64F [c])) -> (Const32  [int64(int32(auxTo64F(c)))])
    56  (Cvt64Fto64  (Const64F [c])) -> (Const64  [int64(auxTo64F(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])) && auxTo32F(c) != 0 -> (Const32F [auxFrom32F(-auxTo32F(c))])
    99  (Neg64F (Const64F [c])) && auxTo64F(c) != 0 -> (Const64F [auxFrom64F(-auxTo64F(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])) -> (Const32F [auxFrom32F(auxTo32F(c) + auxTo32F(d))])
   106  (Add64F (Const64F [c]) (Const64F [d])) -> (Const64F [auxFrom64F(auxTo64F(c) + auxTo64F(d))])
   107  (AddPtr <t> x (Const64 [c])) -> (OffPtr <t> x [c])
   108  (AddPtr <t> x (Const32 [c])) -> (OffPtr <t> x [c])
   109  
   110  (Sub8   (Const8 [c]) (Const8 [d]))     -> (Const8 [int64(int8(c-d))])
   111  (Sub16  (Const16 [c]) (Const16 [d]))   -> (Const16 [int64(int16(c-d))])
   112  (Sub32  (Const32 [c]) (Const32 [d]))   -> (Const32 [int64(int32(c-d))])
   113  (Sub64  (Const64 [c]) (Const64 [d]))   -> (Const64 [c-d])
   114  (Sub32F (Const32F [c]) (Const32F [d])) -> (Const32F [auxFrom32F(auxTo32F(c) - auxTo32F(d))])
   115  (Sub64F (Const64F [c]) (Const64F [d])) -> (Const64F [auxFrom64F(auxTo64F(c) - auxTo64F(d))])
   116  
   117  (Mul8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c*d))])
   118  (Mul16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c*d))])
   119  (Mul32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c*d))])
   120  (Mul64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c*d])
   121  (Mul32F (Const32F [c]) (Const32F [d])) -> (Const32F [auxFrom32F(auxTo32F(c) * auxTo32F(d))])
   122  (Mul64F (Const64F [c]) (Const64F [d])) -> (Const64F [auxFrom64F(auxTo64F(c) * auxTo64F(d))])
   123  
   124  (And8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c&d))])
   125  (And16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c&d))])
   126  (And32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c&d))])
   127  (And64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c&d])
   128  
   129  (Or8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c|d))])
   130  (Or16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c|d))])
   131  (Or32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c|d))])
   132  (Or64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c|d])
   133  
   134  (Xor8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c^d))])
   135  (Xor16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c^d))])
   136  (Xor32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c^d))])
   137  (Xor64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c^d])
   138  
   139  (Div8   (Const8  [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(int8(c)/int8(d))])
   140  (Div16  (Const16 [c])  (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c)/int16(d))])
   141  (Div32  (Const32 [c])  (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c)/int32(d))])
   142  (Div64  (Const64 [c])  (Const64 [d])) && d != 0 -> (Const64 [c/d])
   143  (Div8u  (Const8  [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(int8(uint8(c)/uint8(d)))])
   144  (Div16u (Const16 [c])  (Const16 [d])) && d != 0 -> (Const16 [int64(int16(uint16(c)/uint16(d)))])
   145  (Div32u (Const32 [c])  (Const32 [d])) && d != 0 -> (Const32 [int64(int32(uint32(c)/uint32(d)))])
   146  (Div64u (Const64 [c])  (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c)/uint64(d))])
   147  (Div32F (Const32F [c]) (Const32F [d])) -> (Const32F [auxFrom32F(auxTo32F(c) / auxTo32F(d))])
   148  (Div64F (Const64F [c]) (Const64F [d])) -> (Const64F [auxFrom64F(auxTo64F(c) / auxTo64F(d))])
   149  
   150  (Not (ConstBool [c])) -> (ConstBool [1-c])
   151  
   152  // Convert x * 1 to x.
   153  (Mul(8|16|32|64)  (Const(8|16|32|64)  [1]) x) -> x
   154  
   155  // Convert x * -1 to -x.
   156  (Mul(8|16|32|64)  (Const(8|16|32|64)  [-1]) x) -> (Neg(8|16|32|64)  x)
   157  
   158  // Convert multiplication by a power of two to a shift.
   159  (Mul8  <t> n (Const8  [c])) && isPowerOfTwo(c) -> (Lsh8x64  <t> n (Const64 <typ.UInt64> [log2(c)]))
   160  (Mul16 <t> n (Const16 [c])) && isPowerOfTwo(c) -> (Lsh16x64 <t> n (Const64 <typ.UInt64> [log2(c)]))
   161  (Mul32 <t> n (Const32 [c])) && isPowerOfTwo(c) -> (Lsh32x64 <t> n (Const64 <typ.UInt64> [log2(c)]))
   162  (Mul64 <t> n (Const64 [c])) && isPowerOfTwo(c) -> (Lsh64x64 <t> n (Const64 <typ.UInt64> [log2(c)]))
   163  (Mul8  <t> n (Const8  [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg8  (Lsh8x64  <t> n (Const64 <typ.UInt64> [log2(-c)])))
   164  (Mul16 <t> n (Const16 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg16 (Lsh16x64 <t> n (Const64 <typ.UInt64> [log2(-c)])))
   165  (Mul32 <t> n (Const32 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg32 (Lsh32x64 <t> n (Const64 <typ.UInt64> [log2(-c)])))
   166  (Mul64 <t> n (Const64 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg64 (Lsh64x64 <t> n (Const64 <typ.UInt64> [log2(-c)])))
   167  
   168  (Mod8  (Const8  [c]) (Const8  [d])) && d != 0 -> (Const8  [int64(int8(c % d))])
   169  (Mod16 (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c % d))])
   170  (Mod32 (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c % d))])
   171  (Mod64 (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [c % d])
   172  
   173  (Mod8u  (Const8 [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(uint8(c) % uint8(d))])
   174  (Mod16u (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(uint16(c) % uint16(d))])
   175  (Mod32u (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(uint32(c) % uint32(d))])
   176  (Mod64u (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c) % uint64(d))])
   177  
   178  (Lsh64x64  (Const64 [c]) (Const64 [d])) -> (Const64 [c << uint64(d)])
   179  (Rsh64x64  (Const64 [c]) (Const64 [d])) -> (Const64 [c >> uint64(d)])
   180  (Rsh64Ux64 (Const64 [c]) (Const64 [d])) -> (Const64 [int64(uint64(c) >> uint64(d))])
   181  (Lsh32x64  (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) << uint64(d))])
   182  (Rsh32x64  (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) >> uint64(d))])
   183  (Rsh32Ux64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(uint32(c) >> uint64(d)))])
   184  (Lsh16x64  (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) << uint64(d))])
   185  (Rsh16x64  (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) >> uint64(d))])
   186  (Rsh16Ux64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(uint16(c) >> uint64(d)))])
   187  (Lsh8x64   (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(c) << uint64(d))])
   188  (Rsh8x64   (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(c) >> uint64(d))])
   189  (Rsh8Ux64  (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(uint8(c) >> uint64(d)))])
   190  
   191  // Fold IsInBounds when the range of the index cannot exceed the limit.
   192  (IsInBounds (ZeroExt8to32  _) (Const32 [c])) && (1 << 8)  <= c -> (ConstBool [1])
   193  (IsInBounds (ZeroExt8to64  _) (Const64 [c])) && (1 << 8)  <= c -> (ConstBool [1])
   194  (IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= c -> (ConstBool [1])
   195  (IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c -> (ConstBool [1])
   196  (IsInBounds x x) -> (ConstBool [0])
   197  (IsInBounds                (And8  (Const8  [c]) _)  (Const8  [d])) && 0 <= c && c < d -> (ConstBool [1])
   198  (IsInBounds (ZeroExt8to16  (And8  (Const8  [c]) _)) (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1])
   199  (IsInBounds (ZeroExt8to32  (And8  (Const8  [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
   200  (IsInBounds (ZeroExt8to64  (And8  (Const8  [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   201  (IsInBounds                (And16 (Const16 [c]) _)  (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1])
   202  (IsInBounds (ZeroExt16to32 (And16 (Const16 [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
   203  (IsInBounds (ZeroExt16to64 (And16 (Const16 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   204  (IsInBounds                (And32 (Const32 [c]) _)  (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
   205  (IsInBounds (ZeroExt32to64 (And32 (Const32 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   206  (IsInBounds                (And64 (Const64 [c]) _)  (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   207  (IsInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c < d)])
   208  (IsInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c < d)])
   209  // (Mod64u x y) is always between 0 (inclusive) and y (exclusive).
   210  (IsInBounds (Mod32u _ y) y) -> (ConstBool [1])
   211  (IsInBounds (Mod64u _ y) y) -> (ConstBool [1])
   212  // Right shifting an unsigned number limits its value.
   213  (IsInBounds (ZeroExt8to64  (Rsh8Ux64  _ (Const64 [c]))) (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
   214  (IsInBounds (ZeroExt8to32  (Rsh8Ux64  _ (Const64 [c]))) (Const32 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
   215  (IsInBounds (ZeroExt8to16  (Rsh8Ux64  _ (Const64 [c]))) (Const16 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
   216  (IsInBounds                (Rsh8Ux64  _ (Const64 [c]))  (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d -> (ConstBool [1])
   217  (IsInBounds (ZeroExt16to64 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1])
   218  (IsInBounds (ZeroExt16to32 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1])
   219  (IsInBounds                (Rsh16Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d -> (ConstBool [1])
   220  (IsInBounds (ZeroExt32to64 (Rsh32Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d -> (ConstBool [1])
   221  (IsInBounds                (Rsh32Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d -> (ConstBool [1])
   222  (IsInBounds                (Rsh64Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 64 && 1<<uint(64-c)-1 < d -> (ConstBool [1])
   223  
   224  (IsSliceInBounds x x) -> (ConstBool [1])
   225  (IsSliceInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c <= d -> (ConstBool [1])
   226  (IsSliceInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c <= d -> (ConstBool [1])
   227  (IsSliceInBounds (Const32 [0]) _) -> (ConstBool [1])
   228  (IsSliceInBounds (Const64 [0]) _) -> (ConstBool [1])
   229  (IsSliceInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c <= d)])
   230  (IsSliceInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c <= d)])
   231  (IsSliceInBounds (SliceLen x) (SliceCap x)) -> (ConstBool [1])
   232  
   233  (Eq(64|32|16|8) x x) -> (ConstBool [1])
   234  (EqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c == d)])
   235  (EqB (ConstBool [0]) x) -> (Not x)
   236  (EqB (ConstBool [1]) x) -> x
   237  
   238  (Neq(64|32|16|8) x x) -> (ConstBool [0])
   239  (NeqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c != d)])
   240  (NeqB (ConstBool [0]) x) -> x
   241  (NeqB (ConstBool [1]) x) -> (Not x)
   242  (NeqB (Not x) (Not y)) -> (NeqB x y)
   243  
   244  (Eq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Eq64 (Const64 <t> [c-d]) x)
   245  (Eq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Eq32 (Const32 <t> [int64(int32(c-d))]) x)
   246  (Eq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Eq16 (Const16 <t> [int64(int16(c-d))]) x)
   247  (Eq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Eq8  (Const8 <t> [int64(int8(c-d))]) x)
   248  
   249  (Neq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Neq64 (Const64 <t> [c-d]) x)
   250  (Neq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Neq32 (Const32 <t> [int64(int32(c-d))]) x)
   251  (Neq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Neq16 (Const16 <t> [int64(int16(c-d))]) x)
   252  (Neq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Neq8 (Const8 <t> [int64(int8(c-d))]) x)
   253  
   254  // Canonicalize x-const to x+(-const)
   255  (Sub64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Add64 (Const64 <t> [-c]) x)
   256  (Sub32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Add32 (Const32 <t> [int64(int32(-c))]) x)
   257  (Sub16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Add16 (Const16 <t> [int64(int16(-c))]) x)
   258  (Sub8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (Add8  (Const8  <t> [int64(int8(-c))]) x)
   259  
   260  // fold negation into comparison operators
   261  (Not (Eq(64|32|16|8|B) x y)) -> (Neq(64|32|16|8|B) x y)
   262  (Not (Neq(64|32|16|8|B) x y)) -> (Eq(64|32|16|8|B) x y)
   263  
   264  (Not (Greater(64|32|16|8) x y)) -> (Leq(64|32|16|8) x y)
   265  (Not (Greater(64|32|16|8)U x y)) -> (Leq(64|32|16|8)U x y)
   266  (Not (Geq(64|32|16|8) x y)) -> (Less(64|32|16|8) x y)
   267  (Not (Geq(64|32|16|8)U x y)) -> (Less(64|32|16|8)U x y)
   268  
   269  (Not (Less(64|32|16|8) x y)) -> (Geq(64|32|16|8) x y)
   270  (Not (Less(64|32|16|8)U x y)) -> (Geq(64|32|16|8)U x y)
   271  (Not (Leq(64|32|16|8) x y)) -> (Greater(64|32|16|8) x y)
   272  (Not (Leq(64|32|16|8)U x y)) -> (Greater(64|32|16|8)U x y)
   273  
   274  
   275  // Distribute multiplication c * (d+x) -> c*d + c*x. Useful for:
   276  // a[i].b = ...; a[i+1].b = ...
   277  (Mul64 (Const64 <t> [c]) (Add64 <t> (Const64 <t> [d]) x)) ->
   278    (Add64 (Const64 <t> [c*d]) (Mul64 <t> (Const64 <t> [c]) x))
   279  (Mul32 (Const32 <t> [c]) (Add32 <t> (Const32 <t> [d]) x)) ->
   280    (Add32 (Const32 <t> [int64(int32(c*d))]) (Mul32 <t> (Const32 <t> [c]) x))
   281  
   282  // Rewrite x*y ± x*z  to  x*(y±z)
   283  (Add(64|32|16|8) <t> (Mul(64|32|16|8) x y) (Mul(64|32|16|8) x z))
   284  	-> (Mul(64|32|16|8) x (Add(64|32|16|8) <t> y z))
   285  (Sub(64|32|16|8) <t> (Mul(64|32|16|8) x y) (Mul(64|32|16|8) x z))
   286  	-> (Mul(64|32|16|8) x (Sub(64|32|16|8) <t> y z))
   287  
   288  // rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce
   289  // the number of the other rewrite rules for const shifts
   290  (Lsh64x32  <t> x (Const32 [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint32(c))]))
   291  (Lsh64x16  <t> x (Const16 [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint16(c))]))
   292  (Lsh64x8   <t> x (Const8  [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint8(c))]))
   293  (Rsh64x32  <t> x (Const32 [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint32(c))]))
   294  (Rsh64x16  <t> x (Const16 [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint16(c))]))
   295  (Rsh64x8   <t> x (Const8  [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint8(c))]))
   296  (Rsh64Ux32 <t> x (Const32 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))]))
   297  (Rsh64Ux16 <t> x (Const16 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))]))
   298  (Rsh64Ux8  <t> x (Const8  [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))]))
   299  
   300  (Lsh32x32  <t> x (Const32 [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint32(c))]))
   301  (Lsh32x16  <t> x (Const16 [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint16(c))]))
   302  (Lsh32x8   <t> x (Const8  [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint8(c))]))
   303  (Rsh32x32  <t> x (Const32 [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint32(c))]))
   304  (Rsh32x16  <t> x (Const16 [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint16(c))]))
   305  (Rsh32x8   <t> x (Const8  [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint8(c))]))
   306  (Rsh32Ux32 <t> x (Const32 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))]))
   307  (Rsh32Ux16 <t> x (Const16 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))]))
   308  (Rsh32Ux8  <t> x (Const8  [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))]))
   309  
   310  (Lsh16x32  <t> x (Const32 [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint32(c))]))
   311  (Lsh16x16  <t> x (Const16 [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint16(c))]))
   312  (Lsh16x8   <t> x (Const8  [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint8(c))]))
   313  (Rsh16x32  <t> x (Const32 [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint32(c))]))
   314  (Rsh16x16  <t> x (Const16 [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint16(c))]))
   315  (Rsh16x8   <t> x (Const8  [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint8(c))]))
   316  (Rsh16Ux32 <t> x (Const32 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))]))
   317  (Rsh16Ux16 <t> x (Const16 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))]))
   318  (Rsh16Ux8  <t> x (Const8  [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))]))
   319  
   320  (Lsh8x32  <t> x (Const32 [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint32(c))]))
   321  (Lsh8x16  <t> x (Const16 [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint16(c))]))
   322  (Lsh8x8   <t> x (Const8  [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint8(c))]))
   323  (Rsh8x32  <t> x (Const32 [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint32(c))]))
   324  (Rsh8x16  <t> x (Const16 [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint16(c))]))
   325  (Rsh8x8   <t> x (Const8  [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint8(c))]))
   326  (Rsh8Ux32 <t> x (Const32 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))]))
   327  (Rsh8Ux16 <t> x (Const16 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))]))
   328  (Rsh8Ux8  <t> x (Const8  [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))]))
   329  
   330  // shifts by zero
   331  (Lsh(64|32|16|8)x64  x (Const64 [0])) -> x
   332  (Rsh(64|32|16|8)x64  x (Const64 [0])) -> x
   333  (Rsh(64|32|16|8)Ux64 x (Const64 [0])) -> x
   334  
   335  // zero shifted
   336  (Lsh64x(64|32|16|8)  (Const64 [0]) _) -> (Const64 [0])
   337  (Rsh64x(64|32|16|8)  (Const64 [0]) _) -> (Const64 [0])
   338  (Rsh64Ux(64|32|16|8) (Const64 [0]) _) -> (Const64 [0])
   339  (Lsh32x(64|32|16|8)  (Const32 [0]) _) -> (Const32 [0])
   340  (Rsh32x(64|32|16|8)  (Const32 [0]) _) -> (Const32 [0])
   341  (Rsh32Ux(64|32|16|8) (Const32 [0]) _) -> (Const32 [0])
   342  (Lsh16x(64|32|16|8)  (Const16 [0]) _) -> (Const16 [0])
   343  (Rsh16x(64|32|16|8)  (Const16 [0]) _) -> (Const16 [0])
   344  (Rsh16Ux(64|32|16|8) (Const16 [0]) _) -> (Const16 [0])
   345  (Lsh8x(64|32|16|8)   (Const8  [0]) _) -> (Const8  [0])
   346  (Rsh8x(64|32|16|8)   (Const8  [0]) _) -> (Const8  [0])
   347  (Rsh8Ux(64|32|16|8)  (Const8  [0]) _) -> (Const8  [0])
   348  
   349  // large left shifts of all values, and right shifts of unsigned values
   350  ((Lsh64|Rsh64U)x64  _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0])
   351  ((Lsh32|Rsh32U)x64  _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0])
   352  ((Lsh16|Rsh16U)x64  _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0])
   353  ((Lsh8|Rsh8U)x64    _ (Const64 [c])) && uint64(c) >= 8  -> (Const8  [0])
   354  
   355  // combine const shifts
   356  (Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh64x64 x (Const64 <t> [c+d]))
   357  (Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh32x64 x (Const64 <t> [c+d]))
   358  (Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh16x64 x (Const64 <t> [c+d]))
   359  (Lsh8x64  <t> (Lsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh8x64  x (Const64 <t> [c+d]))
   360  
   361  (Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64x64 x (Const64 <t> [c+d]))
   362  (Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32x64 x (Const64 <t> [c+d]))
   363  (Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16x64 x (Const64 <t> [c+d]))
   364  (Rsh8x64  <t> (Rsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8x64  x (Const64 <t> [c+d]))
   365  
   366  (Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64Ux64 x (Const64 <t> [c+d]))
   367  (Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32Ux64 x (Const64 <t> [c+d]))
   368  (Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16Ux64 x (Const64 <t> [c+d]))
   369  (Rsh8Ux64  <t> (Rsh8Ux64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8Ux64  x (Const64 <t> [c+d]))
   370  
   371  // Remove signed right shift before an unsigned right shift that extracts the sign bit.
   372  (Rsh8Ux64  (Rsh8x64  x _) (Const64 <t> [7] )) -> (Rsh8Ux64  x (Const64 <t> [7] ))
   373  (Rsh16Ux64 (Rsh16x64 x _) (Const64 <t> [15])) -> (Rsh16Ux64 x (Const64 <t> [15]))
   374  (Rsh32Ux64 (Rsh32x64 x _) (Const64 <t> [31])) -> (Rsh32Ux64 x (Const64 <t> [31]))
   375  (Rsh64Ux64 (Rsh64x64 x _) (Const64 <t> [63])) -> (Rsh64Ux64 x (Const64 <t> [63]))
   376  
   377  // ((x >> c1) << c2) >> c3
   378  (Rsh(64|32|16|8)Ux64 (Lsh(64|32|16|8)x64 (Rsh(64|32|16|8)Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   379    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   380    -> (Rsh(64|32|16|8)Ux64 x (Const64 <typ.UInt64> [c1-c2+c3]))
   381  
   382  // ((x << c1) >> c2) << c3
   383  (Lsh(64|32|16|8)x64 (Rsh(64|32|16|8)Ux64 (Lsh(64|32|16|8)x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   384    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   385    -> (Lsh(64|32|16|8)x64 x (Const64 <typ.UInt64> [c1-c2+c3]))
   386  
   387  // (x >> c) & uppermask = 0
   388  (And64 (Const64 [m]) (Rsh64Ux64 _ (Const64 [c]))) && c >= 64-ntz(m) -> (Const64 [0])
   389  (And32 (Const32 [m]) (Rsh32Ux64 _ (Const64 [c]))) && c >= 64-ntz(m) -> (Const32 [0])
   390  (And16 (Const16 [m]) (Rsh16Ux64 _ (Const64 [c]))) && c >= 64-ntz(m) -> (Const16 [0])
   391  (And8  (Const8  [m]) (Rsh8Ux64  _ (Const64 [c]))) && c >= 64-ntz(m) -> (Const8  [0])
   392  
   393  // (x << c) & lowermask = 0
   394  (And64 (Const64 [m]) (Lsh64x64  _ (Const64 [c]))) && c >= 64-nlz(m) -> (Const64 [0])
   395  (And32 (Const32 [m]) (Lsh32x64  _ (Const64 [c]))) && c >= 64-nlz(m) -> (Const32 [0])
   396  (And16 (Const16 [m]) (Lsh16x64  _ (Const64 [c]))) && c >= 64-nlz(m) -> (Const16 [0])
   397  (And8  (Const8  [m]) (Lsh8x64   _ (Const64 [c]))) && c >= 64-nlz(m) -> (Const8  [0])
   398  
   399  // replace shifts with zero extensions
   400  (Rsh16Ux64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) -> (ZeroExt8to16  (Trunc16to8  <typ.UInt8>  x))
   401  (Rsh32Ux64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (ZeroExt8to32  (Trunc32to8  <typ.UInt8>  x))
   402  (Rsh64Ux64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (ZeroExt8to64  (Trunc64to8  <typ.UInt8>  x))
   403  (Rsh32Ux64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (ZeroExt16to32 (Trunc32to16 <typ.UInt16> x))
   404  (Rsh64Ux64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (ZeroExt16to64 (Trunc64to16 <typ.UInt16> x))
   405  (Rsh64Ux64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (ZeroExt32to64 (Trunc64to32 <typ.UInt32> x))
   406  
   407  // replace shifts with sign extensions
   408  (Rsh16x64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) -> (SignExt8to16  (Trunc16to8  <typ.Int8>  x))
   409  (Rsh32x64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (SignExt8to32  (Trunc32to8  <typ.Int8>  x))
   410  (Rsh64x64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (SignExt8to64  (Trunc64to8  <typ.Int8>  x))
   411  (Rsh32x64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (SignExt16to32 (Trunc32to16 <typ.Int16> x))
   412  (Rsh64x64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (SignExt16to64 (Trunc64to16 <typ.Int16> x))
   413  (Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (SignExt32to64 (Trunc64to32 <typ.Int32> x))
   414  
   415  // constant comparisons
   416  (Eq(64|32|16|8)      (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c == d)])
   417  (Neq(64|32|16|8)     (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c != d)])
   418  (Greater(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c > d)])
   419  (Geq(64|32|16|8)     (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c >= d)])
   420  (Less(64|32|16|8)    (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c < d)])
   421  (Leq(64|32|16|8)     (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) -> (ConstBool [b2i(c <= d)])
   422  
   423  (Greater64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) > uint64(d))])
   424  (Greater32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) > uint32(d))])
   425  (Greater16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) > uint16(d))])
   426  (Greater8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  > uint8(d))])
   427  
   428  (Geq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) >= uint64(d))])
   429  (Geq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) >= uint32(d))])
   430  (Geq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) >= uint16(d))])
   431  (Geq8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  >= uint8(d))])
   432  
   433  (Less64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) < uint64(d))])
   434  (Less32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) < uint32(d))])
   435  (Less16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) < uint16(d))])
   436  (Less8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  < uint8(d))])
   437  
   438  (Leq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) <= uint64(d))])
   439  (Leq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) <= uint32(d))])
   440  (Leq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) <= uint16(d))])
   441  (Leq8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  <= uint8(d))])
   442  
   443  // constant floating point comparisons
   444  (Eq32F      (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) == auxTo32F(d))])
   445  (Eq64F      (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) == auxTo64F(d))])
   446  (Neq32F     (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) != auxTo32F(d))])
   447  (Neq64F     (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) != auxTo64F(d))])
   448  (Greater32F (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) > auxTo32F(d))])
   449  (Greater64F (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) > auxTo64F(d))])
   450  (Geq32F     (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) >= auxTo32F(d))])
   451  (Geq64F     (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) >= auxTo64F(d))])
   452  (Less32F    (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) < auxTo32F(d))])
   453  (Less64F    (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) < auxTo64F(d))])
   454  (Leq32F     (Const32F [c]) (Const32F [d])) -> (ConstBool [b2i(auxTo32F(c) <= auxTo32F(d))])
   455  (Leq64F     (Const64F [c]) (Const64F [d])) -> (ConstBool [b2i(auxTo64F(c) <= auxTo64F(d))])
   456  
   457  // simplifications
   458  (Or(64|32|16|8) x x) -> x
   459  (Or(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x
   460  (Or(64|32|16|8) (Const(64|32|16|8) [-1]) _) -> (Const(64|32|16|8) [-1])
   461  
   462  (And(64|32|16|8) x x) -> x
   463  (And(64|32|16|8) (Const(64|32|16|8) [-1]) x) -> x
   464  (And(64|32|16|8) (Const(64|32|16|8) [0]) _) -> (Const(64|32|16|8) [0])
   465  
   466  (Xor(64|32|16|8) x x) -> (Const(64|32|16|8) [0])
   467  (Xor(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x
   468  
   469  (Add(64|32|16|8) (Const(64|32|16|8) [0]) x) -> x
   470  (Sub(64|32|16|8) x x) -> (Const(64|32|16|8) [0])
   471  (Mul(64|32|16|8) (Const(64|32|16|8) [0]) _) -> (Const(64|32|16|8) [0])
   472  
   473  (Com(64|32|16|8) (Com(64|32|16|8)  x)) -> x
   474  (Com(64|32|16|8) (Const(64|32|16|8) [c])) -> (Const(64|32|16|8) [^c])
   475  
   476  (Neg(64|32|16|8) (Sub(64|32|16|8) x y)) -> (Sub(64|32|16|8) y x)
   477  
   478  (Add(64|32|16|8) (Const(64|32|16|8) [1]) (Com(64|32|16|8) x)) -> (Neg(64|32|16|8) x)
   479  
   480  (And(64|32|16|8) x (And(64|32|16|8) x y)) -> (And(64|32|16|8) x y)
   481  (Or(64|32|16|8) x (Or(64|32|16|8) x y)) -> (Or(64|32|16|8) x y)
   482  (Xor(64|32|16|8) x (Xor(64|32|16|8) x y)) -> y
   483  
   484  // Ands clear bits. Ors set bits.
   485  // If a subsequent Or will set all the bits
   486  // that an And cleared, we can skip the And.
   487  // This happens in bitmasking code like:
   488  //   x &^= 3 << shift // clear two old bits
   489  //   x  |= v << shift // set two new bits
   490  // when shift is a small constant and v ends up a constant 3.
   491  (Or8  (And8  x (Const8  [c2])) (Const8  <t> [c1])) && ^(c1 | c2) == 0 -> (Or8  (Const8  <t> [c1]) x)
   492  (Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 -> (Or16 (Const16 <t> [c1]) x)
   493  (Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 -> (Or32 (Const32 <t> [c1]) x)
   494  (Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 -> (Or64 (Const64 <t> [c1]) x)
   495  
   496  (Trunc64to8  (And64 (Const64 [y]) x)) && y&0xFF == 0xFF -> (Trunc64to8 x)
   497  (Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc64to16 x)
   498  (Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF -> (Trunc64to32 x)
   499  (Trunc32to8  (And32 (Const32 [y]) x)) && y&0xFF == 0xFF -> (Trunc32to8 x)
   500  (Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc32to16 x)
   501  (Trunc16to8  (And16 (Const16 [y]) x)) && y&0xFF == 0xFF -> (Trunc16to8 x)
   502  
   503  (ZeroExt8to64  (Trunc64to8  x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 -> x
   504  (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 -> x
   505  (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 -> x
   506  (ZeroExt8to32  (Trunc32to8  x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 -> x
   507  (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 -> x
   508  (ZeroExt8to16  (Trunc16to8  x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 -> x
   509  
   510  (SignExt8to64  (Trunc64to8  x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 -> x
   511  (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 -> x
   512  (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 -> x
   513  (SignExt8to32  (Trunc32to8  x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 -> x
   514  (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 -> x
   515  (SignExt8to16  (Trunc16to8  x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 -> x
   516  
   517  (Slicemask (Const32 [x])) && x > 0 -> (Const32 [-1])
   518  (Slicemask (Const32 [0]))          -> (Const32 [0])
   519  (Slicemask (Const64 [x])) && x > 0 -> (Const64 [-1])
   520  (Slicemask (Const64 [0]))          -> (Const64 [0])
   521  
   522  // Rewrite AND of consts as shifts if possible, slightly faster for 64 bit operands
   523  // leading zeros can be shifted left, then right
   524  (And64 <t> (Const64 [y]) x) && nlz(y) + nto(y) == 64 && nto(y) >= 32
   525    -> (Rsh64Ux64 (Lsh64x64 <t> x (Const64 <t> [nlz(y)])) (Const64 <t> [nlz(y)]))
   526  // trailing zeros can be shifted right, then left
   527  (And64 <t> (Const64 [y]) x) && nlo(y) + ntz(y) == 64 && ntz(y) >= 32
   528    -> (Lsh64x64 (Rsh64Ux64 <t> x (Const64 <t> [ntz(y)])) (Const64 <t> [ntz(y)]))
   529  
   530  // simplifications often used for lengths.  e.g. len(s[i:i+5])==5
   531  (Sub(64|32|16|8) (Add(64|32|16|8) x y) x) -> y
   532  (Sub(64|32|16|8) (Add(64|32|16|8) x y) y) -> x
   533  
   534  // basic phi simplifications
   535  (Phi (Const8  [c]) (Const8  [c])) -> (Const8  [c])
   536  (Phi (Const16 [c]) (Const16 [c])) -> (Const16 [c])
   537  (Phi (Const32 [c]) (Const32 [c])) -> (Const32 [c])
   538  (Phi (Const64 [c]) (Const64 [c])) -> (Const64 [c])
   539  
   540  // slice and interface comparisons
   541  // The frontend ensures that we can only compare against nil,
   542  // so we need only compare the first word (interface type or slice ptr).
   543  (EqInter x y)  -> (EqPtr  (ITab x) (ITab y))
   544  (NeqInter x y) -> (NeqPtr (ITab x) (ITab y))
   545  (EqSlice x y)  -> (EqPtr  (SlicePtr x) (SlicePtr y))
   546  (NeqSlice x y) -> (NeqPtr (SlicePtr x) (SlicePtr y))
   547  
   548  // Load of store of same address, with compatibly typed value and same size
   549  (Load <t1> p1 (Store {t2} p2 x _))
   550  	&& isSamePtr(p1, p2)
   551  	&& t1.Compare(x.Type) == types.CMPeq
   552  	&& t1.Size() == sizeof(t2)
   553  	-> x
   554  (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 x _)))
   555  	&& isSamePtr(p1, p3)
   556  	&& t1.Compare(x.Type) == types.CMPeq
   557  	&& t1.Size() == sizeof(t2)
   558  	&& disjoint(p3, sizeof(t3), p2, sizeof(t2))
   559  	-> x
   560  (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 x _))))
   561  	&& isSamePtr(p1, p4)
   562  	&& t1.Compare(x.Type) == types.CMPeq
   563  	&& t1.Size() == sizeof(t2)
   564  	&& disjoint(p4, sizeof(t4), p2, sizeof(t2))
   565  	&& disjoint(p4, sizeof(t4), p3, sizeof(t3))
   566  	-> x
   567  (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 x _)))))
   568  	&& isSamePtr(p1, p5)
   569  	&& t1.Compare(x.Type) == types.CMPeq
   570  	&& t1.Size() == sizeof(t2)
   571  	&& disjoint(p5, sizeof(t5), p2, sizeof(t2))
   572  	&& disjoint(p5, sizeof(t5), p3, sizeof(t3))
   573  	&& disjoint(p5, sizeof(t5), p4, sizeof(t4))
   574  	-> x
   575  
   576  // Pass constants through math.Float{32,64}bits and math.Float{32,64}frombits
   577  (Load <t1> p1 (Store {t2} p2 (Const64  [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 8 && is64BitFloat(t1) -> (Const64F [x])
   578  (Load <t1> p1 (Store {t2} p2 (Const32  [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 4 && is32BitFloat(t1) -> (Const32F [auxFrom32F(math.Float32frombits(uint32(x)))])
   579  (Load <t1> p1 (Store {t2} p2 (Const64F [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 8 && is64BitInt(t1)   -> (Const64  [x])
   580  (Load <t1> p1 (Store {t2} p2 (Const32F [x]) _)) && isSamePtr(p1,p2) && sizeof(t2) == 4 && is32BitInt(t1)   -> (Const32  [int64(int32(math.Float32bits(auxTo32F(x))))])
   581  
   582  // Float Loads up to Zeros so they can be constant folded.
   583  (Load <t1> op:(OffPtr [o1] p1)
   584  	(Store {t2} p2 _
   585  		mem:(Zero [n] p3 _)))
   586  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p3)
   587  	&& fe.CanSSA(t1)
   588  	&& disjoint(op, t1.Size(), p2, sizeof(t2))
   589  	-> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p3) mem)
   590  (Load <t1> op:(OffPtr [o1] p1)
   591  	(Store {t2} p2 _
   592  		(Store {t3} p3 _
   593  			mem:(Zero [n] p4 _))))
   594  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p4)
   595  	&& fe.CanSSA(t1)
   596  	&& disjoint(op, t1.Size(), p2, sizeof(t2))
   597  	&& disjoint(op, t1.Size(), p3, sizeof(t3))
   598  	-> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p4) mem)
   599  (Load <t1> op:(OffPtr [o1] p1)
   600  	(Store {t2} p2 _
   601  		(Store {t3} p3 _
   602  			(Store {t4} p4 _
   603  				mem:(Zero [n] p5 _)))))
   604  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p5)
   605  	&& fe.CanSSA(t1)
   606  	&& disjoint(op, t1.Size(), p2, sizeof(t2))
   607  	&& disjoint(op, t1.Size(), p3, sizeof(t3))
   608  	&& disjoint(op, t1.Size(), p4, sizeof(t4))
   609  	-> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p5) mem)
   610  (Load <t1> op:(OffPtr [o1] p1)
   611  	(Store {t2} p2 _
   612  		(Store {t3} p3 _
   613  			(Store {t4} p4 _
   614  				(Store {t5} p5 _
   615  					mem:(Zero [n] p6 _))))))
   616  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p6)
   617  	&& fe.CanSSA(t1)
   618  	&& disjoint(op, t1.Size(), p2, sizeof(t2))
   619  	&& disjoint(op, t1.Size(), p3, sizeof(t3))
   620  	&& disjoint(op, t1.Size(), p4, sizeof(t4))
   621  	&& disjoint(op, t1.Size(), p5, sizeof(t5))
   622  	-> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p6) mem)
   623  
   624  // Zero to Load forwarding.
   625  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   626  	&& t1.IsBoolean()
   627  	&& isSamePtr(p1, p2)
   628  	&& n >= o + 1
   629  	-> (ConstBool [0])
   630  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   631  	&& is8BitInt(t1)
   632  	&& isSamePtr(p1, p2)
   633  	&& n >= o + 1
   634  	-> (Const8 [0])
   635  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   636  	&& is16BitInt(t1)
   637  	&& isSamePtr(p1, p2)
   638  	&& n >= o + 2
   639  	-> (Const16 [0])
   640  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   641  	&& is32BitInt(t1)
   642  	&& isSamePtr(p1, p2)
   643  	&& n >= o + 4
   644  	-> (Const32 [0])
   645  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   646  	&& is64BitInt(t1)
   647  	&& isSamePtr(p1, p2)
   648  	&& n >= o + 8
   649  	-> (Const64 [0])
   650  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   651  	&& is32BitFloat(t1)
   652  	&& isSamePtr(p1, p2)
   653  	&& n >= o + 4
   654  	-> (Const32F [0])
   655  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   656  	&& is64BitFloat(t1)
   657  	&& isSamePtr(p1, p2)
   658  	&& n >= o + 8
   659  	-> (Const64F [0])
   660  
   661  // Eliminate stores of values that have just been loaded from the same location.
   662  // We also handle the common case where there are some intermediate stores.
   663  (Store {t1} p1 (Load <t2> p2 mem) mem)
   664  	&& isSamePtr(p1, p2)
   665  	&& t2.Size() == sizeof(t1)
   666  	-> mem
   667  (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ oldmem))
   668  	&& isSamePtr(p1, p2)
   669  	&& t2.Size() == sizeof(t1)
   670  	&& disjoint(p1, sizeof(t1), p3, sizeof(t3))
   671  	-> mem
   672  (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ oldmem)))
   673  	&& isSamePtr(p1, p2)
   674  	&& t2.Size() == sizeof(t1)
   675  	&& disjoint(p1, sizeof(t1), p3, sizeof(t3))
   676  	&& disjoint(p1, sizeof(t1), p4, sizeof(t4))
   677  	-> mem
   678  (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 _ oldmem))))
   679  	&& isSamePtr(p1, p2)
   680  	&& t2.Size() == sizeof(t1)
   681  	&& disjoint(p1, sizeof(t1), p3, sizeof(t3))
   682  	&& disjoint(p1, sizeof(t1), p4, sizeof(t4))
   683  	&& disjoint(p1, sizeof(t1), p5, sizeof(t5))
   684  	-> mem
   685  
   686  // Don't Store zeros to cleared variables.
   687  (Store {t} (OffPtr [o] p1) x mem:(Zero [n] p2 _))
   688  	&& isConstZero(x)
   689  	&& o >= 0 && sizeof(t) + o <= n && isSamePtr(p1, p2)
   690  	-> mem
   691  (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Zero [n] p3 _)))
   692  	&& isConstZero(x)
   693  	&& o1 >= 0 && sizeof(t1) + o1 <= n && isSamePtr(p1, p3)
   694  	&& disjoint(op, sizeof(t1), p2, sizeof(t2))
   695  	-> mem
   696  (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Zero [n] p4 _))))
   697  	&& isConstZero(x)
   698  	&& o1 >= 0 && sizeof(t1) + o1 <= n && isSamePtr(p1, p4)
   699  	&& disjoint(op, sizeof(t1), p2, sizeof(t2))
   700  	&& disjoint(op, sizeof(t1), p3, sizeof(t3))
   701  	-> mem
   702  (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Zero [n] p5 _)))))
   703  	&& isConstZero(x)
   704  	&& o1 >= 0 && sizeof(t1) + o1 <= n && isSamePtr(p1, p5)
   705  	&& disjoint(op, sizeof(t1), p2, sizeof(t2))
   706  	&& disjoint(op, sizeof(t1), p3, sizeof(t3))
   707  	&& disjoint(op, sizeof(t1), p4, sizeof(t4))
   708  	-> mem
   709  
   710  // Collapse OffPtr
   711  (OffPtr (OffPtr p [b]) [a]) -> (OffPtr p [a+b])
   712  (OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq -> p
   713  
   714  // indexing operations
   715  // Note: bounds check has already been done
   716  (PtrIndex <t> ptr idx) && config.PtrSize == 4 -> (AddPtr ptr (Mul32 <typ.Int> idx (Const32 <typ.Int> [t.Elem().Size()])))
   717  (PtrIndex <t> ptr idx) && config.PtrSize == 8 -> (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.Elem().Size()])))
   718  
   719  // struct operations
   720  (StructSelect (StructMake1 x)) -> x
   721  (StructSelect [0] (StructMake2 x _)) -> x
   722  (StructSelect [1] (StructMake2 _ x)) -> x
   723  (StructSelect [0] (StructMake3 x _ _)) -> x
   724  (StructSelect [1] (StructMake3 _ x _)) -> x
   725  (StructSelect [2] (StructMake3 _ _ x)) -> x
   726  (StructSelect [0] (StructMake4 x _ _ _)) -> x
   727  (StructSelect [1] (StructMake4 _ x _ _)) -> x
   728  (StructSelect [2] (StructMake4 _ _ x _)) -> x
   729  (StructSelect [3] (StructMake4 _ _ _ x)) -> x
   730  
   731  (Load <t> _ _) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) ->
   732    (StructMake0)
   733  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) ->
   734    (StructMake1
   735      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem))
   736  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) ->
   737    (StructMake2
   738      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
   739      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem))
   740  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) ->
   741    (StructMake3
   742      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
   743      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
   744      (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem))
   745  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) ->
   746    (StructMake4
   747      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
   748      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
   749      (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem)
   750      (Load <t.FieldType(3)> (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] ptr) mem))
   751  
   752  (StructSelect [i] x:(Load <t> ptr mem)) && !fe.CanSSA(t) ->
   753    @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
   754  
   755  (Store _ (StructMake0) mem) -> mem
   756  (Store dst (StructMake1 <t> f0) mem) ->
   757    (Store {t.FieldType(0)} (OffPtr <t.FieldType(0).PtrTo()> [0] dst) f0 mem)
   758  (Store dst (StructMake2 <t> f0 f1) mem) ->
   759    (Store {t.FieldType(1)}
   760      (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   761      f1
   762      (Store {t.FieldType(0)}
   763        (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
   764          f0 mem))
   765  (Store dst (StructMake3 <t> f0 f1 f2) mem) ->
   766    (Store {t.FieldType(2)}
   767      (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
   768      f2
   769      (Store {t.FieldType(1)}
   770        (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   771        f1
   772        (Store {t.FieldType(0)}
   773          (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
   774            f0 mem)))
   775  (Store dst (StructMake4 <t> f0 f1 f2 f3) mem) ->
   776    (Store {t.FieldType(3)}
   777      (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] dst)
   778      f3
   779      (Store {t.FieldType(2)}
   780        (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
   781        f2
   782        (Store {t.FieldType(1)}
   783          (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   784          f1
   785          (Store {t.FieldType(0)}
   786            (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
   787              f0 mem))))
   788  
   789  // Putting struct{*byte} and similar into direct interfaces.
   790  (IMake typ (StructMake1 val)) -> (IMake typ val)
   791  (StructSelect [0] x:(IData _)) -> x
   792  
   793  // un-SSAable values use mem->mem copies
   794  (Store {t} dst (Load src mem) mem) && !fe.CanSSA(t.(*types.Type)) ->
   795  	(Move {t} [sizeof(t)] dst src mem)
   796  (Store {t} dst (Load src mem) (VarDef {x} mem)) && !fe.CanSSA(t.(*types.Type)) ->
   797  	(Move {t} [sizeof(t)] dst src (VarDef {x} mem))
   798  
   799  // array ops
   800  (ArraySelect (ArrayMake1 x)) -> x
   801  
   802  (Load <t> _ _) && t.IsArray() && t.NumElem() == 0 ->
   803    (ArrayMake0)
   804  
   805  (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) ->
   806    (ArrayMake1 (Load <t.Elem()> ptr mem))
   807  
   808  (Store _ (ArrayMake0) mem) -> mem
   809  (Store dst (ArrayMake1 e) mem) -> (Store {e.Type} dst e mem)
   810  
   811  // Putting [1]{*byte} and similar into direct interfaces.
   812  (IMake typ (ArrayMake1 val)) -> (IMake typ val)
   813  (ArraySelect [0] x:(IData _)) -> x
   814  
   815  // string ops
   816  // Decomposing StringMake and lowering of StringPtr and StringLen
   817  // happens in a later pass, dec, so that these operations are available
   818  // to other passes for optimizations.
   819  (StringPtr (StringMake (Addr <t> {s} base) _)) -> (Addr <t> {s} base)
   820  (StringLen (StringMake _ (Const64 <t> [c]))) -> (Const64 <t> [c])
   821  (ConstString {s}) && config.PtrSize == 4 && s.(string) == "" ->
   822    (StringMake (ConstNil) (Const32 <typ.Int> [0]))
   823  (ConstString {s}) && config.PtrSize == 8 && s.(string) == "" ->
   824    (StringMake (ConstNil) (Const64 <typ.Int> [0]))
   825  (ConstString {s}) && config.PtrSize == 4 && s.(string) != "" ->
   826    (StringMake
   827      (Addr <typ.BytePtr> {fe.StringData(s.(string))}
   828        (SB))
   829      (Const32 <typ.Int> [int64(len(s.(string)))]))
   830  (ConstString {s}) && config.PtrSize == 8 && s.(string) != "" ->
   831    (StringMake
   832      (Addr <typ.BytePtr> {fe.StringData(s.(string))}
   833        (SB))
   834      (Const64 <typ.Int> [int64(len(s.(string)))]))
   835  
   836  // slice ops
   837  // Only a few slice rules are provided here.  See dec.rules for
   838  // a more comprehensive set.
   839  (SliceLen (SliceMake _ (Const64 <t> [c]) _)) -> (Const64 <t> [c])
   840  (SliceCap (SliceMake _ _ (Const64 <t> [c]))) -> (Const64 <t> [c])
   841  (SliceLen (SliceMake _ (Const32 <t> [c]) _)) -> (Const32 <t> [c])
   842  (SliceCap (SliceMake _ _ (Const32 <t> [c]))) -> (Const32 <t> [c])
   843  (SlicePtr (SliceMake (SlicePtr x) _ _)) -> (SlicePtr x)
   844  (SliceLen (SliceMake _ (SliceLen x) _)) -> (SliceLen x)
   845  (SliceCap (SliceMake _ _ (SliceCap x))) -> (SliceCap x)
   846  (SliceCap (SliceMake _ _ (SliceLen x))) -> (SliceLen x)
   847  (ConstSlice) && config.PtrSize == 4 ->
   848    (SliceMake
   849      (ConstNil <v.Type.Elem().PtrTo()>)
   850      (Const32 <typ.Int> [0])
   851      (Const32 <typ.Int> [0]))
   852  (ConstSlice) && config.PtrSize == 8 ->
   853    (SliceMake
   854      (ConstNil <v.Type.Elem().PtrTo()>)
   855      (Const64 <typ.Int> [0])
   856      (Const64 <typ.Int> [0]))
   857  
   858  // interface ops
   859  (ConstInterface) ->
   860    (IMake
   861      (ConstNil <typ.Uintptr>)
   862      (ConstNil <typ.BytePtr>))
   863  
   864  (NilCheck (GetG mem) mem) -> mem
   865  
   866  (If (Not cond) yes no) -> (If cond no yes)
   867  (If (ConstBool [c]) yes no) && c == 1 -> (First nil yes no)
   868  (If (ConstBool [c]) yes no) && c == 0 -> (First nil no yes)
   869  
   870  // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
   871  (Convert (Add(64|32) (Convert ptr mem) off) mem) -> (Add(64|32) ptr off)
   872  (Convert (Convert ptr mem) mem) -> ptr
   873  
   874  // strength reduction of divide by a constant.
   875  // See ../magic.go for a detailed description of these algorithms.
   876  
   877  // Unsigned divide by power of 2.  Strength reduce to a shift.
   878  (Div8u  n (Const8  [c])) && isPowerOfTwo(c&0xff)       -> (Rsh8Ux64 n  (Const64 <typ.UInt64> [log2(c&0xff)]))
   879  (Div16u n (Const16 [c])) && isPowerOfTwo(c&0xffff)     -> (Rsh16Ux64 n (Const64 <typ.UInt64> [log2(c&0xffff)]))
   880  (Div32u n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <typ.UInt64> [log2(c&0xffffffff)]))
   881  (Div64u n (Const64 [c])) && isPowerOfTwo(c)            -> (Rsh64Ux64 n (Const64 <typ.UInt64> [log2(c)]))
   882  (Div64u n (Const64 [-1<<63]))                          -> (Rsh64Ux64 n (Const64 <typ.UInt64> [63]))
   883  
   884  // Signed non-negative divide by power of 2.
   885  (Div8  n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo(c&0xff)       -> (Rsh8Ux64 n  (Const64 <typ.UInt64> [log2(c&0xff)]))
   886  (Div16 n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffff)     -> (Rsh16Ux64 n (Const64 <typ.UInt64> [log2(c&0xffff)]))
   887  (Div32 n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <typ.UInt64> [log2(c&0xffffffff)]))
   888  (Div64 n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c)            -> (Rsh64Ux64 n (Const64 <typ.UInt64> [log2(c)]))
   889  (Div64 n (Const64 [-1<<63])) && isNonNegative(n)                          -> (Const64 [0])
   890  
   891  // Unsigned divide, not a power of 2.  Strength reduce to a multiply.
   892  // For 8-bit divides, we just do a direct 9-bit by 8-bit multiply.
   893  (Div8u x (Const8 [c])) && umagicOK(8, c) ->
   894    (Trunc32to8
   895      (Rsh32Ux64 <typ.UInt32>
   896        (Mul32 <typ.UInt32>
   897          (Const32 <typ.UInt32> [int64(1<<8+umagic(8,c).m)])
   898          (ZeroExt8to32 x))
   899        (Const64 <typ.UInt64> [8+umagic(8,c).s])))
   900  
   901  // For 16-bit divides on 64-bit machines, we do a direct 17-bit by 16-bit multiply.
   902  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 8 ->
   903    (Trunc64to16
   904      (Rsh64Ux64 <typ.UInt64>
   905        (Mul64 <typ.UInt64>
   906          (Const64 <typ.UInt64> [int64(1<<16+umagic(16,c).m)])
   907          (ZeroExt16to64 x))
   908        (Const64 <typ.UInt64> [16+umagic(16,c).s])))
   909  
   910  // For 16-bit divides on 32-bit machines
   911  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && umagic(16,c).m&1 == 0 ->
   912    (Trunc32to16
   913      (Rsh32Ux64 <typ.UInt32>
   914        (Mul32 <typ.UInt32>
   915          (Const32 <typ.UInt32> [int64(1<<15+umagic(16,c).m/2)])
   916          (ZeroExt16to32 x))
   917        (Const64 <typ.UInt64> [16+umagic(16,c).s-1])))
   918  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && c&1 == 0 ->
   919    (Trunc32to16
   920      (Rsh32Ux64 <typ.UInt32>
   921        (Mul32 <typ.UInt32>
   922          (Const32 <typ.UInt32> [int64(1<<15+(umagic(16,c).m+1)/2)])
   923          (Rsh32Ux64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [1])))
   924        (Const64 <typ.UInt64> [16+umagic(16,c).s-2])))
   925  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && config.useAvg ->
   926    (Trunc32to16
   927      (Rsh32Ux64 <typ.UInt32>
   928        (Avg32u
   929          (Lsh32x64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [16]))
   930          (Mul32 <typ.UInt32>
   931            (Const32 <typ.UInt32> [int64(umagic(16,c).m)])
   932            (ZeroExt16to32 x)))
   933        (Const64 <typ.UInt64> [16+umagic(16,c).s-1])))
   934  
   935  // For 32-bit divides on 32-bit machines
   936  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && umagic(32,c).m&1 == 0 && config.useHmul ->
   937    (Rsh32Ux64 <typ.UInt32>
   938      (Hmul32u <typ.UInt32>
   939        (Const32 <typ.UInt32> [int64(int32(1<<31+umagic(32,c).m/2))])
   940        x)
   941      (Const64 <typ.UInt64> [umagic(32,c).s-1]))
   942  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && c&1 == 0 && config.useHmul ->
   943    (Rsh32Ux64 <typ.UInt32>
   944      (Hmul32u <typ.UInt32>
   945        (Const32 <typ.UInt32> [int64(int32(1<<31+(umagic(32,c).m+1)/2))])
   946        (Rsh32Ux64 <typ.UInt32> x (Const64 <typ.UInt64> [1])))
   947      (Const64 <typ.UInt64> [umagic(32,c).s-2]))
   948  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && config.useAvg && config.useHmul ->
   949    (Rsh32Ux64 <typ.UInt32>
   950      (Avg32u
   951        x
   952        (Hmul32u <typ.UInt32>
   953          (Const32 <typ.UInt32> [int64(int32(umagic(32,c).m))])
   954          x))
   955      (Const64 <typ.UInt64> [umagic(32,c).s-1]))
   956  
   957  // For 32-bit divides on 64-bit machines
   958  // We'll use a regular (non-hi) multiply for this case.
   959  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && umagic(32,c).m&1 == 0 ->
   960    (Trunc64to32
   961      (Rsh64Ux64 <typ.UInt64>
   962        (Mul64 <typ.UInt64>
   963          (Const64 <typ.UInt64> [int64(1<<31+umagic(32,c).m/2)])
   964          (ZeroExt32to64 x))
   965        (Const64 <typ.UInt64> [32+umagic(32,c).s-1])))
   966  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && c&1 == 0 ->
   967    (Trunc64to32
   968      (Rsh64Ux64 <typ.UInt64>
   969        (Mul64 <typ.UInt64>
   970          (Const64 <typ.UInt64> [int64(1<<31+(umagic(32,c).m+1)/2)])
   971          (Rsh64Ux64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [1])))
   972        (Const64 <typ.UInt64> [32+umagic(32,c).s-2])))
   973  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && config.useAvg ->
   974    (Trunc64to32
   975      (Rsh64Ux64 <typ.UInt64>
   976        (Avg64u
   977          (Lsh64x64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [32]))
   978          (Mul64 <typ.UInt64>
   979            (Const64 <typ.UInt32> [int64(umagic(32,c).m)])
   980            (ZeroExt32to64 x)))
   981        (Const64 <typ.UInt64> [32+umagic(32,c).s-1])))
   982  
   983  // For 64-bit divides on 64-bit machines
   984  // (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.)
   985  (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && umagic(64,c).m&1 == 0 && config.useHmul ->
   986    (Rsh64Ux64 <typ.UInt64>
   987      (Hmul64u <typ.UInt64>
   988        (Const64 <typ.UInt64> [int64(1<<63+umagic(64,c).m/2)])
   989        x)
   990      (Const64 <typ.UInt64> [umagic(64,c).s-1]))
   991  (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && c&1 == 0 && config.useHmul ->
   992    (Rsh64Ux64 <typ.UInt64>
   993      (Hmul64u <typ.UInt64>
   994        (Const64 <typ.UInt64> [int64(1<<63+(umagic(64,c).m+1)/2)])
   995        (Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [1])))
   996      (Const64 <typ.UInt64> [umagic(64,c).s-2]))
   997  (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && config.useAvg && config.useHmul ->
   998    (Rsh64Ux64 <typ.UInt64>
   999      (Avg64u
  1000        x
  1001        (Hmul64u <typ.UInt64>
  1002          (Const64 <typ.UInt64> [int64(umagic(64,c).m)])
  1003          x))
  1004      (Const64 <typ.UInt64> [umagic(64,c).s-1]))
  1005  
  1006  // Signed divide by a negative constant.  Rewrite to divide by a positive constant.
  1007  (Div8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  -> (Neg8  (Div8  <t> n (Const8  <t> [-c])))
  1008  (Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Neg16 (Div16 <t> n (Const16 <t> [-c])))
  1009  (Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Neg32 (Div32 <t> n (Const32 <t> [-c])))
  1010  (Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Neg64 (Div64 <t> n (Const64 <t> [-c])))
  1011  
  1012  // Dividing by the most-negative number.  Result is always 0 except
  1013  // if the input is also the most-negative number.
  1014  // We can detect that using the sign bit of x & -x.
  1015  (Div8  <t> x (Const8  [-1<<7 ])) -> (Rsh8Ux64  (And8  <t> x (Neg8  <t> x)) (Const64 <typ.UInt64> [7 ]))
  1016  (Div16 <t> x (Const16 [-1<<15])) -> (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15]))
  1017  (Div32 <t> x (Const32 [-1<<31])) -> (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31]))
  1018  (Div64 <t> x (Const64 [-1<<63])) -> (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63]))
  1019  
  1020  // Signed divide by power of 2.
  1021  // n / c =       n >> log(c) if n >= 0
  1022  //       = (n+c-1) >> log(c) if n < 0
  1023  // We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned).
  1024  (Div8  <t> n (Const8  [c])) && isPowerOfTwo(c) ->
  1025    (Rsh8x64
  1026      (Add8  <t> n (Rsh8Ux64  <t> (Rsh8x64  <t> n (Const64 <typ.UInt64> [ 7])) (Const64 <typ.UInt64> [ 8-log2(c)])))
  1027      (Const64 <typ.UInt64> [log2(c)]))
  1028  (Div16 <t> n (Const16 [c])) && isPowerOfTwo(c) ->
  1029    (Rsh16x64
  1030      (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <typ.UInt64> [15])) (Const64 <typ.UInt64> [16-log2(c)])))
  1031      (Const64 <typ.UInt64> [log2(c)]))
  1032  (Div32 <t> n (Const32 [c])) && isPowerOfTwo(c) ->
  1033    (Rsh32x64
  1034      (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <typ.UInt64> [31])) (Const64 <typ.UInt64> [32-log2(c)])))
  1035      (Const64 <typ.UInt64> [log2(c)]))
  1036  (Div64 <t> n (Const64 [c])) && isPowerOfTwo(c) ->
  1037    (Rsh64x64
  1038      (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <typ.UInt64> [63])) (Const64 <typ.UInt64> [64-log2(c)])))
  1039      (Const64 <typ.UInt64> [log2(c)]))
  1040  
  1041  // Signed divide, not a power of 2.  Strength reduce to a multiply.
  1042  (Div8 <t> x (Const8 [c])) && smagicOK(8,c) ->
  1043    (Sub8 <t>
  1044      (Rsh32x64 <t>
  1045        (Mul32 <typ.UInt32>
  1046          (Const32 <typ.UInt32> [int64(smagic(8,c).m)])
  1047          (SignExt8to32 x))
  1048        (Const64 <typ.UInt64> [8+smagic(8,c).s]))
  1049      (Rsh32x64 <t>
  1050        (SignExt8to32 x)
  1051        (Const64 <typ.UInt64> [31])))
  1052  (Div16 <t> x (Const16 [c])) && smagicOK(16,c) ->
  1053    (Sub16 <t>
  1054      (Rsh32x64 <t>
  1055        (Mul32 <typ.UInt32>
  1056          (Const32 <typ.UInt32> [int64(smagic(16,c).m)])
  1057          (SignExt16to32 x))
  1058        (Const64 <typ.UInt64> [16+smagic(16,c).s]))
  1059      (Rsh32x64 <t>
  1060        (SignExt16to32 x)
  1061        (Const64 <typ.UInt64> [31])))
  1062  (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 8 ->
  1063    (Sub32 <t>
  1064      (Rsh64x64 <t>
  1065        (Mul64 <typ.UInt64>
  1066          (Const64 <typ.UInt64> [int64(smagic(32,c).m)])
  1067          (SignExt32to64 x))
  1068        (Const64 <typ.UInt64> [32+smagic(32,c).s]))
  1069      (Rsh64x64 <t>
  1070        (SignExt32to64 x)
  1071        (Const64 <typ.UInt64> [63])))
  1072  (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 == 0 && config.useHmul ->
  1073    (Sub32 <t>
  1074      (Rsh32x64 <t>
  1075        (Hmul32 <t>
  1076          (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m/2))])
  1077          x)
  1078        (Const64 <typ.UInt64> [smagic(32,c).s-1]))
  1079      (Rsh32x64 <t>
  1080        x
  1081        (Const64 <typ.UInt64> [31])))
  1082  (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 != 0 && config.useHmul ->
  1083    (Sub32 <t>
  1084      (Rsh32x64 <t>
  1085        (Add32 <t>
  1086          (Hmul32 <t>
  1087            (Const32 <typ.UInt32> [int64(int32(smagic(32,c).m))])
  1088            x)
  1089          x)
  1090        (Const64 <typ.UInt64> [smagic(32,c).s]))
  1091      (Rsh32x64 <t>
  1092        x
  1093        (Const64 <typ.UInt64> [31])))
  1094  (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 == 0 && config.useHmul ->
  1095    (Sub64 <t>
  1096      (Rsh64x64 <t>
  1097        (Hmul64 <t>
  1098          (Const64 <typ.UInt64> [int64(smagic(64,c).m/2)])
  1099          x)
  1100        (Const64 <typ.UInt64> [smagic(64,c).s-1]))
  1101      (Rsh64x64 <t>
  1102        x
  1103        (Const64 <typ.UInt64> [63])))
  1104  (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 != 0 && config.useHmul ->
  1105    (Sub64 <t>
  1106      (Rsh64x64 <t>
  1107        (Add64 <t>
  1108          (Hmul64 <t>
  1109            (Const64 <typ.UInt64> [int64(smagic(64,c).m)])
  1110            x)
  1111          x)
  1112        (Const64 <typ.UInt64> [smagic(64,c).s]))
  1113      (Rsh64x64 <t>
  1114        x
  1115        (Const64 <typ.UInt64> [63])))
  1116  
  1117  // Unsigned mod by power of 2 constant.
  1118  (Mod8u  <t> n (Const8  [c])) && isPowerOfTwo(c&0xff)       -> (And8 n (Const8 <t> [(c&0xff)-1]))
  1119  (Mod16u <t> n (Const16 [c])) && isPowerOfTwo(c&0xffff)     -> (And16 n (Const16 <t> [(c&0xffff)-1]))
  1120  (Mod32u <t> n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1]))
  1121  (Mod64u <t> n (Const64 [c])) && isPowerOfTwo(c)            -> (And64 n (Const64 <t> [c-1]))
  1122  (Mod64u <t> n (Const64 [-1<<63]))                          -> (And64 n (Const64 <t> [1<<63-1]))
  1123  
  1124  // Signed non-negative mod by power of 2 constant.
  1125  (Mod8  <t> n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo(c&0xff)       -> (And8 n (Const8 <t> [(c&0xff)-1]))
  1126  (Mod16 <t> n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffff)     -> (And16 n (Const16 <t> [(c&0xffff)-1]))
  1127  (Mod32 <t> n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1]))
  1128  (Mod64 <t> n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c)            -> (And64 n (Const64 <t> [c-1]))
  1129  (Mod64 n (Const64 [-1<<63])) && isNonNegative(n)                              -> n
  1130  
  1131  // Signed mod by negative constant.
  1132  (Mod8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  -> (Mod8  <t> n (Const8  <t> [-c]))
  1133  (Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Mod16 <t> n (Const16 <t> [-c]))
  1134  (Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Mod32 <t> n (Const32 <t> [-c]))
  1135  (Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Mod64 <t> n (Const64 <t> [-c]))
  1136  
  1137  // All other mods by constants, do A%B = A-(A/B*B).
  1138  // This implements % with two * and a bunch of ancillary ops.
  1139  // One of the * is free if the user's code also computes A/B.
  1140  (Mod8   <t> x (Const8  [c])) && x.Op != OpConst8  && (c > 0 || c == -1<<7)
  1141    -> (Sub8  x (Mul8  <t> (Div8   <t> x (Const8  <t> [c])) (Const8  <t> [c])))
  1142  (Mod16  <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15)
  1143    -> (Sub16 x (Mul16 <t> (Div16  <t> x (Const16 <t> [c])) (Const16 <t> [c])))
  1144  (Mod32  <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31)
  1145    -> (Sub32 x (Mul32 <t> (Div32  <t> x (Const32 <t> [c])) (Const32 <t> [c])))
  1146  (Mod64  <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63)
  1147    -> (Sub64 x (Mul64 <t> (Div64  <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1148  (Mod8u  <t> x (Const8  [c])) && x.Op != OpConst8  && c > 0 && umagicOK(8 ,c)
  1149    -> (Sub8  x (Mul8  <t> (Div8u  <t> x (Const8  <t> [c])) (Const8  <t> [c])))
  1150  (Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c > 0 && umagicOK(16,c)
  1151    -> (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c])))
  1152  (Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c > 0 && umagicOK(32,c)
  1153    -> (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c])))
  1154  (Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c > 0 && umagicOK(64,c)
  1155    -> (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1156  
  1157  (Eq(8|16|32|64)  s:(Sub(8|16|32|64) x y) (Const(8|16|32|64) [0])) && s.Uses == 1 -> (Eq(8|16|32|64)  x y)
  1158  (Neq(8|16|32|64) s:(Sub(8|16|32|64) x y) (Const(8|16|32|64) [0])) && s.Uses == 1 -> (Neq(8|16|32|64) x y)
  1159  
  1160  // Reassociate expressions involving
  1161  // constants such that constants come first,
  1162  // exposing obvious constant-folding opportunities.
  1163  // Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C
  1164  // is constant, which pushes constants to the outside
  1165  // of the expression. At that point, any constant-folding
  1166  // opportunities should be obvious.
  1167  
  1168  // x + (C + z) -> C + (x + z)
  1169  (Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Add64 <t> z x))
  1170  (Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Add32 <t> z x))
  1171  (Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Add16 <t> z x))
  1172  (Add8  (Add8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Add8  <t> z x))
  1173  
  1174  // x + (C - z) -> C + (x - z)
  1175  (Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
  1176  (Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
  1177  (Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
  1178  (Add8  (Sub8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))
  1179  (Add64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
  1180  (Add32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
  1181  (Add16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
  1182  (Add8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))
  1183  
  1184  // x + (z - C) -> (x + z) - C
  1185  (Add64 (Sub64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
  1186  (Add32 (Sub32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
  1187  (Add16 (Sub16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
  1188  (Add8  (Sub8  z i:(Const8  <t>)) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)
  1189  (Add64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
  1190  (Add32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
  1191  (Add16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
  1192  (Add8  x (Sub8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)
  1193  
  1194  // x - (C - z) -> x + (z - C) -> (x + z) - C
  1195  (Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
  1196  (Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
  1197  (Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
  1198  (Sub8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)
  1199  
  1200  // x - (z - C) -> x + (C - z) -> (x - z) + C
  1201  (Sub64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
  1202  (Sub32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
  1203  (Sub16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
  1204  (Sub8  x (Sub8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))
  1205  
  1206  // x & (C & z) -> C & (x & z)
  1207  (And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (And64 i (And64 <t> z x))
  1208  (And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (And32 i (And32 <t> z x))
  1209  (And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (And16 i (And16 <t> z x))
  1210  (And8  (And8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (And8  i (And8  <t> z x))
  1211  
  1212  // x | (C | z) -> C | (x | z)
  1213  (Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Or64 i (Or64 <t> z x))
  1214  (Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Or32 i (Or32 <t> z x))
  1215  (Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Or16 i (Or16 <t> z x))
  1216  (Or8  (Or8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Or8  i (Or8  <t> z x))
  1217  
  1218  // x ^ (C ^ z) -> C ^ (x ^ z)
  1219  (Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Xor64 i (Xor64 <t> z x))
  1220  (Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Xor32 i (Xor32 <t> z x))
  1221  (Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Xor16 i (Xor16 <t> z x))
  1222  (Xor8  (Xor8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Xor8  i (Xor8  <t> z x))
  1223  
  1224  // C + (D + x) -> (C + D) + x
  1225  (Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c+d]) x)
  1226  (Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c+d))]) x)
  1227  (Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c+d))]) x)
  1228  (Add8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Add8  (Const8  <t> [int64(int8(c+d))]) x)
  1229  
  1230  // C + (D - x) -> (C + D) - x
  1231  (Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Sub64 (Const64 <t> [c+d]) x)
  1232  (Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x)
  1233  (Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x)
  1234  (Add8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) -> (Sub8  (Const8  <t> [int64(int8(c+d))]) x)
  1235  
  1236  // C + (x - D) -> (C - D) + x
  1237  (Add64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Add64 (Const64 <t> [c-d]) x)
  1238  (Add32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x)
  1239  (Add16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x)
  1240  (Add8  (Const8  <t> [c]) (Sub8  x (Const8  <t> [d]))) -> (Add8  (Const8  <t> [int64(int8(c-d))]) x)
  1241  
  1242  // C - (x - D) -> (C + D) - x
  1243  (Sub64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Sub64 (Const64 <t> [c+d]) x)
  1244  (Sub32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x)
  1245  (Sub16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x)
  1246  (Sub8  (Const8  <t> [c]) (Sub8  x (Const8  <t> [d]))) -> (Sub8  (Const8  <t> [int64(int8(c+d))]) x)
  1247  
  1248  // C - (D - x) -> (C - D) + x
  1249  (Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c-d]) x)
  1250  (Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x)
  1251  (Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x)
  1252  (Sub8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) -> (Add8  (Const8  <t> [int64(int8(c-d))]) x)
  1253  
  1254  // C & (D & x) -> (C & D) & x
  1255  (And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) -> (And64 (Const64 <t> [c&d]) x)
  1256  (And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) -> (And32 (Const32 <t> [int64(int32(c&d))]) x)
  1257  (And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) -> (And16 (Const16 <t> [int64(int16(c&d))]) x)
  1258  (And8  (Const8  <t> [c]) (And8  (Const8  <t> [d]) x)) -> (And8  (Const8  <t> [int64(int8(c&d))]) x)
  1259  
  1260  // C | (D | x) -> (C | D) | x
  1261  (Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) -> (Or64 (Const64 <t> [c|d]) x)
  1262  (Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) -> (Or32 (Const32 <t> [int64(int32(c|d))]) x)
  1263  (Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) -> (Or16 (Const16 <t> [int64(int16(c|d))]) x)
  1264  (Or8  (Const8  <t> [c]) (Or8  (Const8  <t> [d]) x)) -> (Or8  (Const8  <t> [int64(int8(c|d))]) x)
  1265  
  1266  // C ^ (D ^ x) -> (C ^ D) ^ x
  1267  (Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) -> (Xor64 (Const64 <t> [c^d]) x)
  1268  (Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) -> (Xor32 (Const32 <t> [int64(int32(c^d))]) x)
  1269  (Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) -> (Xor16 (Const16 <t> [int64(int16(c^d))]) x)
  1270  (Xor8  (Const8  <t> [c]) (Xor8  (Const8  <t> [d]) x)) -> (Xor8  (Const8  <t> [int64(int8(c^d))]) x)
  1271  
  1272  // C * (D * x) = (C * D) * x
  1273  (Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) -> (Mul64 (Const64 <t> [c*d]) x)
  1274  (Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) -> (Mul32 (Const32 <t> [int64(int32(c*d))]) x)
  1275  (Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) -> (Mul16 (Const16 <t> [int64(int16(c*d))]) x)
  1276  (Mul8  (Const8  <t> [c]) (Mul8  (Const8  <t> [d]) x)) -> (Mul8  (Const8  <t> [int64(int8(c*d))]) x)
  1277  
  1278  // floating point optimizations
  1279  (Mul(32|64)F x (Const(32|64)F [auxFrom64F(1)])) -> x
  1280  (Mul32F x (Const32F [auxFrom32F(-1)])) -> (Neg32F x)
  1281  (Mul64F x (Const64F [auxFrom64F(-1)])) -> (Neg64F x)
  1282  (Mul32F x (Const32F [auxFrom32F(2)])) -> (Add32F x x)
  1283  (Mul64F x (Const64F [auxFrom64F(2)])) -> (Add64F x x)
  1284  
  1285  (Div32F x (Const32F <t> [c])) && reciprocalExact32(auxTo32F(c)) -> (Mul32F x (Const32F <t> [auxFrom32F(1/auxTo32F(c))]))
  1286  (Div64F x (Const64F <t> [c])) && reciprocalExact64(auxTo64F(c)) -> (Mul64F x (Const64F <t> [auxFrom64F(1/auxTo64F(c))]))
  1287  
  1288  (Sqrt (Const64F [c])) -> (Const64F [auxFrom64F(math.Sqrt(auxTo64F(c)))])
  1289  
  1290  // recognize runtime.newobject and don't Zero/Nilcheck it
  1291  (Zero (Load (OffPtr [c] (SP)) mem) mem)
  1292  	&& mem.Op == OpStaticCall
  1293  	&& isSameSym(mem.Aux, "runtime.newobject")
  1294  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1295  	-> mem
  1296  (Store (Load (OffPtr [c] (SP)) mem) x mem)
  1297  	&& isConstZero(x)
  1298  	&& mem.Op == OpStaticCall
  1299  	&& isSameSym(mem.Aux, "runtime.newobject")
  1300  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1301  	-> mem
  1302  (Store (OffPtr (Load (OffPtr [c] (SP)) mem)) x mem)
  1303  	&& isConstZero(x)
  1304  	&& mem.Op == OpStaticCall
  1305  	&& isSameSym(mem.Aux, "runtime.newobject")
  1306  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1307  	-> mem
  1308  // nil checks just need to rewrite to something useless.
  1309  // they will be deadcode eliminated soon afterwards.
  1310  (NilCheck (Load (OffPtr [c] (SP)) (StaticCall {sym} _)) _)
  1311  	&& isSameSym(sym, "runtime.newobject")
  1312  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1313  	&& warnRule(fe.Debug_checknil(), v, "removed nil check")
  1314  	-> (Invalid)
  1315  (NilCheck (OffPtr (Load (OffPtr [c] (SP)) (StaticCall {sym} _))) _)
  1316  	&& isSameSym(sym, "runtime.newobject")
  1317  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1318  	&& warnRule(fe.Debug_checknil(), v, "removed nil check")
  1319  	-> (Invalid)
  1320  
  1321  // Evaluate constant address comparisons.
  1322  (EqPtr  x x) -> (ConstBool [1])
  1323  (NeqPtr x x) -> (ConstBool [0])
  1324  (EqPtr  (Addr {a} _) (Addr {b} _)) -> (ConstBool [b2i(a == b)])
  1325  (NeqPtr (Addr {a} _) (Addr {b} _)) -> (ConstBool [b2i(a != b)])
  1326  (EqPtr  (LocalAddr {a} _ _) (LocalAddr {b} _ _)) -> (ConstBool [b2i(a == b)])
  1327  (NeqPtr (LocalAddr {a} _ _) (LocalAddr {b} _ _)) -> (ConstBool [b2i(a != b)])
  1328  (EqPtr  (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) -> (ConstBool [b2i(o1 == 0)])
  1329  (NeqPtr (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) -> (ConstBool [b2i(o1 != 0)])
  1330  (EqPtr  (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) -> (ConstBool [b2i(o1 == o2)])
  1331  (NeqPtr (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) -> (ConstBool [b2i(o1 != o2)])
  1332  (EqPtr  (Const(32|64) [c]) (Const(32|64) [d])) -> (ConstBool [b2i(c == d)])
  1333  (NeqPtr (Const(32|64) [c]) (Const(32|64) [d])) -> (ConstBool [b2i(c != d)])
  1334  
  1335  (EqPtr  (LocalAddr _ _) (Addr _)) -> (ConstBool [0])
  1336  (NeqPtr (LocalAddr _ _) (Addr _)) -> (ConstBool [1])
  1337  (EqPtr  (Addr _) (LocalAddr _ _)) -> (ConstBool [0])
  1338  (NeqPtr (Addr _) (LocalAddr _ _)) -> (ConstBool [1])
  1339  
  1340  // Simplify address comparisons.
  1341  (EqPtr  (AddPtr p1 o1) p2) && isSamePtr(p1, p2) -> (Not (IsNonNil o1))
  1342  (NeqPtr (AddPtr p1 o1) p2) && isSamePtr(p1, p2) -> (IsNonNil o1)
  1343  (EqPtr  (Const(32|64) [0]) p) -> (Not (IsNonNil p))
  1344  (NeqPtr (Const(32|64) [0]) p) -> (IsNonNil p)
  1345  (EqPtr  (ConstNil) p) -> (Not (IsNonNil p))
  1346  (NeqPtr (ConstNil) p) -> (IsNonNil p)
  1347  
  1348  // Evaluate constant user nil checks.
  1349  (IsNonNil (ConstNil)) -> (ConstBool [0])
  1350  (IsNonNil (Const(32|64) [c])) -> (ConstBool [b2i(c != 0)])
  1351  (IsNonNil (Addr _)) -> (ConstBool [1])
  1352  (IsNonNil (LocalAddr _ _)) -> (ConstBool [1])
  1353  
  1354  // Inline small or disjoint runtime.memmove calls with constant length.
  1355  (StaticCall {sym} s1:(Store _ (Const(64|32) [sz]) s2:(Store  _ src s3:(Store {t} _ dst mem))))
  1356  	&& isSameSym(sym,"runtime.memmove")
  1357  	&& t.(*types.Type).IsPtr() // avoids TUINTPTR, see issue 30061
  1358  	&& s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1
  1359  	&& isInlinableMemmove(dst,src,sz,config)
  1360  	&& clobber(s1) && clobber(s2) && clobber(s3)
  1361  	-> (Move {t.(*types.Type).Elem()} [sz] dst src mem)
  1362  
  1363  // De-virtualize interface calls into static calls.
  1364  // Note that (ITab (IMake)) doesn't get
  1365  // rewritten until after the first opt pass,
  1366  // so this rule should trigger reliably.
  1367  (InterCall [argsize] (Load (OffPtr [off] (ITab (IMake (Addr {itab} (SB)) _))) _) mem) && devirt(v, itab, off) != nil ->
  1368  	(StaticCall [argsize] {devirt(v, itab, off)} mem)
  1369  
  1370  // Move and Zero optimizations.
  1371  // Move source and destination may overlap.
  1372  
  1373  // Convert Moves into Zeros when the source is known to be zeros.
  1374  (Move {t} [n] dst1 src mem:(Zero {t} [n] dst2 _)) && isSamePtr(src, dst2)
  1375  	-> (Zero {t} [n] dst1 mem)
  1376  (Move {t} [n] dst1 src mem:(VarDef (Zero {t} [n] dst0 _))) && isSamePtr(src, dst0)
  1377  	-> (Zero {t} [n] dst1 mem)
  1378  
  1379  // Don't Store to variables that are about to be overwritten by Move/Zero.
  1380  (Zero {t1} [n] p1 store:(Store {t2} (OffPtr [o2] p2) _ mem))
  1381  	&& isSamePtr(p1, p2) && store.Uses == 1
  1382  	&& n >= o2 + sizeof(t2)
  1383  	&& clobber(store)
  1384  	-> (Zero {t1} [n] p1 mem)
  1385  (Move {t1} [n] dst1 src1 store:(Store {t2} op:(OffPtr [o2] dst2) _ mem))
  1386  	&& isSamePtr(dst1, dst2) && store.Uses == 1
  1387  	&& n >= o2 + sizeof(t2)
  1388  	&& disjoint(src1, n, op, sizeof(t2))
  1389  	&& clobber(store)
  1390  	-> (Move {t1} [n] dst1 src1 mem)
  1391  
  1392  // Don't Move to variables that are immediately completely overwritten.
  1393  (Zero {t} [n] dst1 move:(Move {t} [n] dst2 _ mem))
  1394  	&& move.Uses == 1
  1395  	&& isSamePtr(dst1, dst2)
  1396  	&& clobber(move)
  1397  	-> (Zero {t} [n] dst1 mem)
  1398  (Move {t} [n] dst1 src1 move:(Move {t} [n] dst2 _ mem))
  1399  	&& move.Uses == 1
  1400  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  1401  	&& clobber(move)
  1402  	-> (Move {t} [n] dst1 src1 mem)
  1403  (Zero {t} [n] dst1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
  1404  	&& move.Uses == 1 && vardef.Uses == 1
  1405  	&& isSamePtr(dst1, dst2)
  1406  	&& clobber(move) && clobber(vardef)
  1407  	-> (Zero {t} [n] dst1 (VarDef {x} mem))
  1408  (Move {t} [n] dst1 src1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
  1409  	&& move.Uses == 1 && vardef.Uses == 1
  1410  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  1411  	&& clobber(move) && clobber(vardef)
  1412  	-> (Move {t} [n] dst1 src1 (VarDef {x} mem))
  1413  (Store {t1} op1:(OffPtr [o1] p1) d1
  1414  	m2:(Store {t2} op2:(OffPtr [0] p2) d2
  1415  		m3:(Move [n] p3 _ mem)))
  1416  	&& m2.Uses == 1 && m3.Uses == 1
  1417  	&& o1 == sizeof(t2)
  1418  	&& n == sizeof(t2) + sizeof(t1)
  1419  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1420  	&& clobber(m2) && clobber(m3)
  1421  	-> (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
  1422  (Store {t1} op1:(OffPtr [o1] p1) d1
  1423  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  1424  		m3:(Store {t3} op3:(OffPtr [0] p3) d3
  1425  			m4:(Move [n] p4 _ mem))))
  1426  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
  1427  	&& o2 == sizeof(t3)
  1428  	&& o1-o2 == sizeof(t2)
  1429  	&& n == sizeof(t3) + sizeof(t2) + sizeof(t1)
  1430  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1431  	&& clobber(m2) && clobber(m3) && clobber(m4)
  1432  	-> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
  1433  (Store {t1} op1:(OffPtr [o1] p1) d1
  1434  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  1435  		m3:(Store {t3} op3:(OffPtr [o3] p3) d3
  1436  			m4:(Store {t4} op4:(OffPtr [0] p4) d4
  1437  				m5:(Move [n] p5 _ mem)))))
  1438  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
  1439  	&& o3 == sizeof(t4)
  1440  	&& o2-o3 == sizeof(t3)
  1441  	&& o1-o2 == sizeof(t2)
  1442  	&& n == sizeof(t4) + sizeof(t3) + sizeof(t2) + sizeof(t1)
  1443  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1444  	&& clobber(m2) && clobber(m3) && clobber(m4) && clobber(m5)
  1445  	-> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
  1446  
  1447  // Don't Zero variables that are immediately completely overwritten
  1448  // before being accessed.
  1449  (Move {t} [n] dst1 src1 zero:(Zero {t} [n] dst2 mem))
  1450  	&& zero.Uses == 1
  1451  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  1452  	&& clobber(zero)
  1453  	-> (Move {t} [n] dst1 src1 mem)
  1454  (Move {t} [n] dst1 src1 vardef:(VarDef {x} zero:(Zero {t} [n] dst2 mem)))
  1455  	&& zero.Uses == 1 && vardef.Uses == 1
  1456  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  1457  	&& clobber(zero) && clobber(vardef)
  1458  	-> (Move {t} [n] dst1 src1 (VarDef {x} mem))
  1459  (Store {t1} op1:(OffPtr [o1] p1) d1
  1460  	m2:(Store {t2} op2:(OffPtr [0] p2) d2
  1461  		m3:(Zero [n] p3 mem)))
  1462  	&& m2.Uses == 1 && m3.Uses == 1
  1463  	&& o1 == sizeof(t2)
  1464  	&& n == sizeof(t2) + sizeof(t1)
  1465  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1466  	&& clobber(m2) && clobber(m3)
  1467  	-> (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
  1468  (Store {t1} op1:(OffPtr [o1] p1) d1
  1469  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  1470  		m3:(Store {t3} op3:(OffPtr [0] p3) d3
  1471  			m4:(Zero [n] p4 mem))))
  1472  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
  1473  	&& o2 == sizeof(t3)
  1474  	&& o1-o2 == sizeof(t2)
  1475  	&& n == sizeof(t3) + sizeof(t2) + sizeof(t1)
  1476  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1477  	&& clobber(m2) && clobber(m3) && clobber(m4)
  1478  	-> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
  1479  (Store {t1} op1:(OffPtr [o1] p1) d1
  1480  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  1481  		m3:(Store {t3} op3:(OffPtr [o3] p3) d3
  1482  			m4:(Store {t4} op4:(OffPtr [0] p4) d4
  1483  				m5:(Zero [n] p5 mem)))))
  1484  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
  1485  	&& o3 == sizeof(t4)
  1486  	&& o2-o3 == sizeof(t3)
  1487  	&& o1-o2 == sizeof(t2)
  1488  	&& n == sizeof(t4) + sizeof(t3) + sizeof(t2) + sizeof(t1)
  1489  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1490  	&& clobber(m2) && clobber(m3) && clobber(m4) && clobber(m5)
  1491  	-> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
  1492  
  1493  // Don't Move from memory if the values are likely to already be
  1494  // in registers.
  1495  (Move {t1} [n] dst p1
  1496  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1497  		(Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _)))
  1498  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1499  	&& alignof(t2) <= alignof(t1)
  1500  	&& alignof(t3) <= alignof(t1)
  1501  	&& registerizable(b, t2)
  1502  	&& registerizable(b, t3)
  1503  	&& o2 == sizeof(t3)
  1504  	&& n == sizeof(t2) + sizeof(t3)
  1505  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1506  		(Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
  1507  (Move {t1} [n] dst p1
  1508  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1509  		(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  1510  			(Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _))))
  1511  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1512  	&& alignof(t2) <= alignof(t1)
  1513  	&& alignof(t3) <= alignof(t1)
  1514  	&& alignof(t4) <= alignof(t1)
  1515  	&& registerizable(b, t2)
  1516  	&& registerizable(b, t3)
  1517  	&& registerizable(b, t4)
  1518  	&& o3 == sizeof(t4)
  1519  	&& o2-o3 == sizeof(t3)
  1520  	&& n == sizeof(t2) + sizeof(t3) + sizeof(t4)
  1521  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1522  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1523  			(Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
  1524  (Move {t1} [n] dst p1
  1525  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1526  		(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  1527  			(Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
  1528  				(Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _)))))
  1529  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1530  	&& alignof(t2) <= alignof(t1)
  1531  	&& alignof(t3) <= alignof(t1)
  1532  	&& alignof(t4) <= alignof(t1)
  1533  	&& alignof(t5) <= alignof(t1)
  1534  	&& registerizable(b, t2)
  1535  	&& registerizable(b, t3)
  1536  	&& registerizable(b, t4)
  1537  	&& registerizable(b, t5)
  1538  	&& o4 == sizeof(t5)
  1539  	&& o3-o4 == sizeof(t4)
  1540  	&& o2-o3 == sizeof(t3)
  1541  	&& n == sizeof(t2) + sizeof(t3) + sizeof(t4) + sizeof(t5)
  1542  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1543  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1544  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  1545  				(Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
  1546  
  1547  // Same thing but with VarDef in the middle.
  1548  (Move {t1} [n] dst p1
  1549  	mem:(VarDef
  1550  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1551  			(Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _))))
  1552  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1553  	&& alignof(t2) <= alignof(t1)
  1554  	&& alignof(t3) <= alignof(t1)
  1555  	&& registerizable(b, t2)
  1556  	&& registerizable(b, t3)
  1557  	&& o2 == sizeof(t3)
  1558  	&& n == sizeof(t2) + sizeof(t3)
  1559  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1560  		(Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
  1561  (Move {t1} [n] dst p1
  1562  	mem:(VarDef
  1563  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1564  			(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  1565  				(Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _)))))
  1566  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1567  	&& alignof(t2) <= alignof(t1)
  1568  	&& alignof(t3) <= alignof(t1)
  1569  	&& alignof(t4) <= alignof(t1)
  1570  	&& registerizable(b, t2)
  1571  	&& registerizable(b, t3)
  1572  	&& registerizable(b, t4)
  1573  	&& o3 == sizeof(t4)
  1574  	&& o2-o3 == sizeof(t3)
  1575  	&& n == sizeof(t2) + sizeof(t3) + sizeof(t4)
  1576  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1577  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1578  			(Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
  1579  (Move {t1} [n] dst p1
  1580  	mem:(VarDef
  1581  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1582  			(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  1583  				(Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
  1584  					(Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _))))))
  1585  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1586  	&& alignof(t2) <= alignof(t1)
  1587  	&& alignof(t3) <= alignof(t1)
  1588  	&& alignof(t4) <= alignof(t1)
  1589  	&& alignof(t5) <= alignof(t1)
  1590  	&& registerizable(b, t2)
  1591  	&& registerizable(b, t3)
  1592  	&& registerizable(b, t4)
  1593  	&& registerizable(b, t5)
  1594  	&& o4 == sizeof(t5)
  1595  	&& o3-o4 == sizeof(t4)
  1596  	&& o2-o3 == sizeof(t3)
  1597  	&& n == sizeof(t2) + sizeof(t3) + sizeof(t4) + sizeof(t5)
  1598  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1599  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1600  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  1601  				(Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
  1602  
  1603  // Prefer to Zero and Store than to Move.
  1604  (Move {t1} [n] dst p1
  1605  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1606  		(Zero {t3} [n] p3 _)))
  1607  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1608  	&& alignof(t2) <= alignof(t1)
  1609  	&& alignof(t3) <= alignof(t1)
  1610  	&& registerizable(b, t2)
  1611  	&& n >= o2 + sizeof(t2)
  1612  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1613  		(Zero {t1} [n] dst mem))
  1614  (Move {t1} [n] dst p1
  1615  	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
  1616  		(Store {t3} (OffPtr <tt3> [o3] p3) d2
  1617  			(Zero {t4} [n] p4 _))))
  1618  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1619  	&& alignof(t2) <= alignof(t1)
  1620  	&& alignof(t3) <= alignof(t1)
  1621  	&& alignof(t4) <= alignof(t1)
  1622  	&& registerizable(b, t2)
  1623  	&& registerizable(b, t3)
  1624  	&& n >= o2 + sizeof(t2)
  1625  	&& n >= o3 + sizeof(t3)
  1626  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1627  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1628  			(Zero {t1} [n] dst mem)))
  1629  (Move {t1} [n] dst p1
  1630  	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
  1631  		(Store {t3} (OffPtr <tt3> [o3] p3) d2
  1632  			(Store {t4} (OffPtr <tt4> [o4] p4) d3
  1633  				(Zero {t5} [n] p5 _)))))
  1634  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1635  	&& alignof(t2) <= alignof(t1)
  1636  	&& alignof(t3) <= alignof(t1)
  1637  	&& alignof(t4) <= alignof(t1)
  1638  	&& alignof(t5) <= alignof(t1)
  1639  	&& registerizable(b, t2)
  1640  	&& registerizable(b, t3)
  1641  	&& registerizable(b, t4)
  1642  	&& n >= o2 + sizeof(t2)
  1643  	&& n >= o3 + sizeof(t3)
  1644  	&& n >= o4 + sizeof(t4)
  1645  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1646  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1647  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  1648  				(Zero {t1} [n] dst mem))))
  1649  (Move {t1} [n] dst p1
  1650  	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
  1651  		(Store {t3} (OffPtr <tt3> [o3] p3) d2
  1652  			(Store {t4} (OffPtr <tt4> [o4] p4) d3
  1653  				(Store {t5} (OffPtr <tt5> [o5] p5) d4
  1654  					(Zero {t6} [n] p6 _))))))
  1655  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6)
  1656  	&& alignof(t2) <= alignof(t1)
  1657  	&& alignof(t3) <= alignof(t1)
  1658  	&& alignof(t4) <= alignof(t1)
  1659  	&& alignof(t5) <= alignof(t1)
  1660  	&& alignof(t6) <= alignof(t1)
  1661  	&& registerizable(b, t2)
  1662  	&& registerizable(b, t3)
  1663  	&& registerizable(b, t4)
  1664  	&& registerizable(b, t5)
  1665  	&& n >= o2 + sizeof(t2)
  1666  	&& n >= o3 + sizeof(t3)
  1667  	&& n >= o4 + sizeof(t4)
  1668  	&& n >= o5 + sizeof(t5)
  1669  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1670  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1671  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  1672  				(Store {t5} (OffPtr <tt5> [o5] dst) d4
  1673  					(Zero {t1} [n] dst mem)))))
  1674  (Move {t1} [n] dst p1
  1675  	mem:(VarDef
  1676  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1677  			(Zero {t3} [n] p3 _))))
  1678  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1679  	&& alignof(t2) <= alignof(t1)
  1680  	&& alignof(t3) <= alignof(t1)
  1681  	&& registerizable(b, t2)
  1682  	&& n >= o2 + sizeof(t2)
  1683  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1684  		(Zero {t1} [n] dst mem))
  1685  (Move {t1} [n] dst p1
  1686  	mem:(VarDef
  1687  		(Store {t2} (OffPtr <tt2> [o2] p2) d1
  1688  			(Store {t3} (OffPtr <tt3> [o3] p3) d2
  1689  				(Zero {t4} [n] p4 _)))))
  1690  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1691  	&& alignof(t2) <= alignof(t1)
  1692  	&& alignof(t3) <= alignof(t1)
  1693  	&& alignof(t4) <= alignof(t1)
  1694  	&& registerizable(b, t2)
  1695  	&& registerizable(b, t3)
  1696  	&& n >= o2 + sizeof(t2)
  1697  	&& n >= o3 + sizeof(t3)
  1698  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1699  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1700  			(Zero {t1} [n] dst mem)))
  1701  (Move {t1} [n] dst p1
  1702  	mem:(VarDef
  1703  		(Store {t2} (OffPtr <tt2> [o2] p2) d1
  1704  			(Store {t3} (OffPtr <tt3> [o3] p3) d2
  1705  				(Store {t4} (OffPtr <tt4> [o4] p4) d3
  1706  					(Zero {t5} [n] p5 _))))))
  1707  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1708  	&& alignof(t2) <= alignof(t1)
  1709  	&& alignof(t3) <= alignof(t1)
  1710  	&& alignof(t4) <= alignof(t1)
  1711  	&& alignof(t5) <= alignof(t1)
  1712  	&& registerizable(b, t2)
  1713  	&& registerizable(b, t3)
  1714  	&& registerizable(b, t4)
  1715  	&& n >= o2 + sizeof(t2)
  1716  	&& n >= o3 + sizeof(t3)
  1717  	&& n >= o4 + sizeof(t4)
  1718  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1719  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1720  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  1721  				(Zero {t1} [n] dst mem))))
  1722  (Move {t1} [n] dst p1
  1723  	mem:(VarDef
  1724  		(Store {t2} (OffPtr <tt2> [o2] p2) d1
  1725  			(Store {t3} (OffPtr <tt3> [o3] p3) d2
  1726  				(Store {t4} (OffPtr <tt4> [o4] p4) d3
  1727  					(Store {t5} (OffPtr <tt5> [o5] p5) d4
  1728  						(Zero {t6} [n] p6 _)))))))
  1729  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6)
  1730  	&& alignof(t2) <= alignof(t1)
  1731  	&& alignof(t3) <= alignof(t1)
  1732  	&& alignof(t4) <= alignof(t1)
  1733  	&& alignof(t5) <= alignof(t1)
  1734  	&& alignof(t6) <= alignof(t1)
  1735  	&& registerizable(b, t2)
  1736  	&& registerizable(b, t3)
  1737  	&& registerizable(b, t4)
  1738  	&& registerizable(b, t5)
  1739  	&& n >= o2 + sizeof(t2)
  1740  	&& n >= o3 + sizeof(t3)
  1741  	&& n >= o4 + sizeof(t4)
  1742  	&& n >= o5 + sizeof(t5)
  1743  	-> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1744  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1745  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  1746  				(Store {t5} (OffPtr <tt5> [o5] dst) d4
  1747  					(Zero {t1} [n] dst mem)))))
  1748  
  1749  (StaticCall {sym} x) && needRaceCleanup(sym,v) -> x
  1750  
  1751  // Collapse moving A -> B -> C into just A -> C.
  1752  // Later passes (deadstore, elim unread auto) will remove the A -> B move, if possible.
  1753  // This happens most commonly when B is an autotmp inserted earlier
  1754  // during compilation to ensure correctness.
  1755  // Take care that overlapping moves are preserved.
  1756  // Restrict this optimization to the stack, to avoid duplicating loads from the heap;
  1757  // see CL 145208 for discussion.
  1758  (Move {t1} [s] dst tmp1 midmem:(Move {t2} [s] tmp2 src _))
  1759  	&& t1.(*types.Type).Compare(t2.(*types.Type)) == types.CMPeq
  1760  	&& isSamePtr(tmp1, tmp2)
  1761  	&& isStackPtr(src)
  1762  	&& disjoint(src, s, tmp2, s)
  1763  	&& (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
  1764  	-> (Move {t1} [s] dst src midmem)
  1765  
  1766  // Same, but for large types that require VarDefs.
  1767  (Move {t1} [s] dst tmp1 midmem:(VarDef (Move {t2} [s] tmp2 src _)))
  1768  	&& t1.(*types.Type).Compare(t2.(*types.Type)) == types.CMPeq
  1769  	&& isSamePtr(tmp1, tmp2)
  1770  	&& isStackPtr(src)
  1771  	&& disjoint(src, s, tmp2, s)
  1772  	&& (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
  1773  	-> (Move {t1} [s] dst src midmem)
  1774  
  1775  // Elide self-moves. This only happens rarely (e.g test/fixedbugs/bug277.go).
  1776  // However, this rule is needed to prevent the previous rule from looping forever in such cases.
  1777  (Move dst src mem) && isSamePtr(dst, src) -> mem