Text file src/cmd/compile/internal/ssa/_gen/generic.rules

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Simplifications that apply to all backend architectures. As an example, this
     6  // Go source code
     7  //
     8  // y := 0 * x
     9  //
    10  // can be translated into y := 0 without losing any information, which saves a
    11  // pointless multiplication instruction. Other .rules files in this directory
    12  // (for example AMD64.rules) contain rules specific to the architecture in the
    13  // filename. The rules here apply to every architecture.
    14  //
    15  // The code for parsing this file lives in rulegen.go; this file generates
    16  // ssa/rewritegeneric.go.
    17  
    18  // values are specified using the following format:
    19  // (op <type> [auxint] {aux} arg0 arg1 ...)
    20  // the type, aux, and auxint fields are optional
    21  // on the matching side
    22  //  - the type, aux, and auxint fields must match if they are specified.
    23  //  - the first occurrence of a variable defines that variable.  Subsequent
    24  //    uses must match (be == to) the first use.
    25  //  - v is defined to be the value matched.
    26  //  - an additional conditional can be provided after the match pattern with "&&".
    27  // on the generated side
    28  //  - the type of the top-level expression is the same as the one on the left-hand side.
    29  //  - the type of any subexpressions must be specified explicitly (or
    30  //    be specified in the op's type field).
    31  //  - auxint will be 0 if not specified.
    32  //  - aux will be nil if not specified.
    33  
    34  // blocks are specified using the following format:
    35  // (kind controlvalue succ0 succ1 ...)
    36  // controlvalue must be "nil" or a value expression
    37  // succ* fields must be variables
    38  // For now, the generated successors must be a permutation of the matched successors.
    39  
    40  // constant folding
    41  (Trunc16to8  (Const16  [c])) => (Const8   [int8(c)])
    42  (Trunc32to8  (Const32  [c])) => (Const8   [int8(c)])
    43  (Trunc32to16 (Const32  [c])) => (Const16  [int16(c)])
    44  (Trunc64to8  (Const64  [c])) => (Const8   [int8(c)])
    45  (Trunc64to16 (Const64  [c])) => (Const16  [int16(c)])
    46  (Trunc64to32 (Const64  [c])) => (Const32  [int32(c)])
    47  (Cvt64Fto32F (Const64F [c])) => (Const32F [float32(c)])
    48  (Cvt32Fto64F (Const32F [c])) => (Const64F [float64(c)])
    49  (Cvt32to32F  (Const32  [c])) => (Const32F [float32(c)])
    50  (Cvt32to64F  (Const32  [c])) => (Const64F [float64(c)])
    51  (Cvt64to32F  (Const64  [c])) => (Const32F [float32(c)])
    52  (Cvt64to64F  (Const64  [c])) => (Const64F [float64(c)])
    53  (Cvt32Fto32  (Const32F [c])) && c >= -1<<31 && c < 1<<31 => (Const32 [int32(c)])
    54  (Cvt32Fto64  (Const32F [c])) && c >= -1<<63 && c < 1<<63 => (Const64 [int64(c)])
    55  (Cvt64Fto32  (Const64F [c])) && c >= -1<<31 && c < 1<<31 => (Const32 [int32(c)])
    56  (Cvt64Fto64  (Const64F [c])) && c >= -1<<63 && c < 1<<63 => (Const64 [int64(c)])
    57  (Round32F x:(Const32F)) => x
    58  (Round64F x:(Const64F)) => x
    59  (CvtBoolToUint8 (ConstBool [false])) => (Const8 [0])
    60  (CvtBoolToUint8 (ConstBool [true])) => (Const8 [1])
    61  (BitLen64 (Const64 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.Len64(uint64(c)))])
    62  (BitLen32 (Const32 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.Len32(uint32(c)))])
    63  (BitLen16 (Const16 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.Len16(uint16(c)))])
    64  (BitLen8  (Const8  [c])) && config.PtrSize == 8 => (Const64 [int64(bits.Len8(uint8(c)))])
    65  (BitLen64 (Const64 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.Len64(uint64(c)))])
    66  (BitLen32 (Const32 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.Len32(uint32(c)))])
    67  (BitLen16 (Const16 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.Len16(uint16(c)))])
    68  (BitLen8  (Const8  [c])) && config.PtrSize == 4 => (Const32 [int32(bits.Len8(uint8(c)))])
    69  (PopCount64 (Const64 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.OnesCount64(uint64(c)))])
    70  (PopCount32 (Const32 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.OnesCount32(uint32(c)))])
    71  (PopCount16 (Const16 [c])) && config.PtrSize == 8 => (Const64 [int64(bits.OnesCount16(uint16(c)))])
    72  (PopCount8  (Const8  [c])) && config.PtrSize == 8 => (Const64 [int64(bits.OnesCount8(uint8(c)))])
    73  (PopCount64 (Const64 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount64(uint64(c)))])
    74  (PopCount32 (Const32 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount32(uint32(c)))])
    75  (PopCount16 (Const16 [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount16(uint16(c)))])
    76  (PopCount8  (Const8  [c])) && config.PtrSize == 4 => (Const32 [int32(bits.OnesCount8(uint8(c)))])
    77  (Add64carry (Const64 <t> [x]) (Const64 [y]) (Const64 [c])) && c >= 0 && c <= 1 => (MakeTuple (Const64 <t> [bitsAdd64(x, y, c).sum]) (Const64 <t> [bitsAdd64(x, y, c).carry]))
    78  
    79  (Trunc16to8  (ZeroExt8to16  x)) => x
    80  (Trunc32to8  (ZeroExt8to32  x)) => x
    81  (Trunc32to16 (ZeroExt8to32  x)) => (ZeroExt8to16  x)
    82  (Trunc32to16 (ZeroExt16to32 x)) => x
    83  (Trunc64to8  (ZeroExt8to64  x)) => x
    84  (Trunc64to16 (ZeroExt8to64  x)) => (ZeroExt8to16  x)
    85  (Trunc64to16 (ZeroExt16to64 x)) => x
    86  (Trunc64to32 (ZeroExt8to64  x)) => (ZeroExt8to32  x)
    87  (Trunc64to32 (ZeroExt16to64 x)) => (ZeroExt16to32 x)
    88  (Trunc64to32 (ZeroExt32to64 x)) => x
    89  (Trunc16to8  (SignExt8to16  x)) => x
    90  (Trunc32to8  (SignExt8to32  x)) => x
    91  (Trunc32to16 (SignExt8to32  x)) => (SignExt8to16  x)
    92  (Trunc32to16 (SignExt16to32 x)) => x
    93  (Trunc64to8  (SignExt8to64  x)) => x
    94  (Trunc64to16 (SignExt8to64  x)) => (SignExt8to16  x)
    95  (Trunc64to16 (SignExt16to64 x)) => x
    96  (Trunc64to32 (SignExt8to64  x)) => (SignExt8to32  x)
    97  (Trunc64to32 (SignExt16to64 x)) => (SignExt16to32 x)
    98  (Trunc64to32 (SignExt32to64 x)) => x
    99  
   100  (ZeroExt8to16  (Const8  [c])) => (Const16 [int16( uint8(c))])
   101  (ZeroExt8to32  (Const8  [c])) => (Const32 [int32( uint8(c))])
   102  (ZeroExt8to64  (Const8  [c])) => (Const64 [int64( uint8(c))])
   103  (ZeroExt16to32 (Const16 [c])) => (Const32 [int32(uint16(c))])
   104  (ZeroExt16to64 (Const16 [c])) => (Const64 [int64(uint16(c))])
   105  (ZeroExt32to64 (Const32 [c])) => (Const64 [int64(uint32(c))])
   106  (SignExt8to16  (Const8  [c])) => (Const16 [int16(c)])
   107  (SignExt8to32  (Const8  [c])) => (Const32 [int32(c)])
   108  (SignExt8to64  (Const8  [c])) => (Const64 [int64(c)])
   109  (SignExt16to32 (Const16 [c])) => (Const32 [int32(c)])
   110  (SignExt16to64 (Const16 [c])) => (Const64 [int64(c)])
   111  (SignExt32to64 (Const32 [c])) => (Const64 [int64(c)])
   112  
   113  (Neg8   (Const8   [c])) => (Const8   [-c])
   114  (Neg16  (Const16  [c])) => (Const16  [-c])
   115  (Neg32  (Const32  [c])) => (Const32  [-c])
   116  (Neg64  (Const64  [c])) => (Const64  [-c])
   117  (Neg32F (Const32F [c])) && c != 0 => (Const32F [-c])
   118  (Neg64F (Const64F [c])) && c != 0 => (Const64F [-c])
   119  
   120  (Add8   (Const8 [c])   (Const8 [d]))   => (Const8  [c+d])
   121  (Add16  (Const16 [c])  (Const16 [d]))  => (Const16 [c+d])
   122  (Add32  (Const32 [c])  (Const32 [d]))  => (Const32 [c+d])
   123  (Add64  (Const64 [c])  (Const64 [d]))  => (Const64 [c+d])
   124  (Add32F (Const32F [c]) (Const32F [d])) && c+d == c+d => (Const32F [c+d])
   125  (Add64F (Const64F [c]) (Const64F [d])) && c+d == c+d => (Const64F [c+d])
   126  (AddPtr <t> x (Const64 [c])) => (OffPtr <t> x [c])
   127  (AddPtr <t> x (Const32 [c])) => (OffPtr <t> x [int64(c)])
   128  
   129  (Sub8   (Const8 [c]) (Const8 [d]))     => (Const8 [c-d])
   130  (Sub16  (Const16 [c]) (Const16 [d]))   => (Const16 [c-d])
   131  (Sub32  (Const32 [c]) (Const32 [d]))   => (Const32 [c-d])
   132  (Sub64  (Const64 [c]) (Const64 [d]))   => (Const64 [c-d])
   133  (Sub32F (Const32F [c]) (Const32F [d])) && c-d == c-d => (Const32F [c-d])
   134  (Sub64F (Const64F [c]) (Const64F [d])) && c-d == c-d => (Const64F [c-d])
   135  
   136  (Mul8   (Const8 [c])   (Const8 [d]))   => (Const8  [c*d])
   137  (Mul16  (Const16 [c])  (Const16 [d]))  => (Const16 [c*d])
   138  (Mul32  (Const32 [c])  (Const32 [d]))  => (Const32 [c*d])
   139  (Mul64  (Const64 [c])  (Const64 [d]))  => (Const64 [c*d])
   140  (Mul32F (Const32F [c]) (Const32F [d])) && c*d == c*d => (Const32F [c*d])
   141  (Mul64F (Const64F [c]) (Const64F [d])) && c*d == c*d => (Const64F [c*d])
   142  (Mul32uhilo (Const32 [c]) (Const32 [d])) => (MakeTuple (Const32 <typ.UInt32> [bitsMulU32(c, d).hi]) (Const32 <typ.UInt32> [bitsMulU32(c,d).lo]))
   143  (Mul64uhilo (Const64 [c]) (Const64 [d])) => (MakeTuple (Const64 <typ.UInt64> [bitsMulU64(c, d).hi]) (Const64 <typ.UInt64> [bitsMulU64(c,d).lo]))
   144  (Mul32uover (Const32 [c]) (Const32 [d])) => (MakeTuple (Const32 <typ.UInt32> [bitsMulU32(c, d).lo]) (ConstBool <typ.Bool> [bitsMulU32(c,d).hi != 0]))
   145  (Mul64uover (Const64 [c]) (Const64 [d])) => (MakeTuple (Const64 <typ.UInt64> [bitsMulU64(c, d).lo]) (ConstBool <typ.Bool> [bitsMulU64(c,d).hi != 0]))
   146  
   147  (And8   (Const8 [c])   (Const8 [d]))   => (Const8  [c&d])
   148  (And16  (Const16 [c])  (Const16 [d]))  => (Const16 [c&d])
   149  (And32  (Const32 [c])  (Const32 [d]))  => (Const32 [c&d])
   150  (And64  (Const64 [c])  (Const64 [d]))  => (Const64 [c&d])
   151  
   152  (Or8   (Const8 [c])   (Const8 [d]))   => (Const8  [c|d])
   153  (Or16  (Const16 [c])  (Const16 [d]))  => (Const16 [c|d])
   154  (Or32  (Const32 [c])  (Const32 [d]))  => (Const32 [c|d])
   155  (Or64  (Const64 [c])  (Const64 [d]))  => (Const64 [c|d])
   156  
   157  (Xor8   (Const8 [c])   (Const8 [d]))   => (Const8  [c^d])
   158  (Xor16  (Const16 [c])  (Const16 [d]))  => (Const16 [c^d])
   159  (Xor32  (Const32 [c])  (Const32 [d]))  => (Const32 [c^d])
   160  (Xor64  (Const64 [c])  (Const64 [d]))  => (Const64 [c^d])
   161  
   162  (Ctz64 (Const64 [c])) && config.PtrSize == 4 => (Const32 [int32(ntz64(c))])
   163  (Ctz32 (Const32 [c])) && config.PtrSize == 4 => (Const32 [int32(ntz32(c))])
   164  (Ctz16 (Const16 [c])) && config.PtrSize == 4 => (Const32 [int32(ntz16(c))])
   165  (Ctz8  (Const8  [c])) && config.PtrSize == 4 => (Const32 [int32(ntz8(c))])
   166  
   167  (Ctz64 (Const64 [c])) && config.PtrSize == 8 => (Const64 [int64(ntz64(c))])
   168  (Ctz32 (Const32 [c])) && config.PtrSize == 8 => (Const64 [int64(ntz32(c))])
   169  (Ctz16 (Const16 [c])) && config.PtrSize == 8 => (Const64 [int64(ntz16(c))])
   170  (Ctz8  (Const8  [c])) && config.PtrSize == 8 => (Const64 [int64(ntz8(c))])
   171  
   172  (Div8   (Const8  [c])  (Const8  [d])) && d != 0 => (Const8  [c/d])
   173  (Div16  (Const16 [c])  (Const16 [d])) && d != 0 => (Const16 [c/d])
   174  (Div32  (Const32 [c])  (Const32 [d])) && d != 0 => (Const32 [c/d])
   175  (Div64  (Const64 [c])  (Const64 [d])) && d != 0 => (Const64 [c/d])
   176  (Div8u  (Const8  [c])  (Const8  [d])) && d != 0 => (Const8  [int8(uint8(c)/uint8(d))])
   177  (Div16u (Const16 [c])  (Const16 [d])) && d != 0 => (Const16 [int16(uint16(c)/uint16(d))])
   178  (Div32u (Const32 [c])  (Const32 [d])) && d != 0 => (Const32 [int32(uint32(c)/uint32(d))])
   179  (Div64u (Const64 [c])  (Const64 [d])) && d != 0 => (Const64 [int64(uint64(c)/uint64(d))])
   180  (Div32F (Const32F [c]) (Const32F [d])) && c/d == c/d => (Const32F [c/d])
   181  (Div64F (Const64F [c]) (Const64F [d])) && c/d == c/d => (Const64F [c/d])
   182  (Div128u <t> (Const64 [0]) lo y) => (MakeTuple (Div64u <t.FieldType(0)> lo y) (Mod64u <t.FieldType(1)> lo y))
   183  
   184  (Not (ConstBool [c])) => (ConstBool [!c])
   185  
   186  (Floor       (Const64F [c])) => (Const64F [math.Floor(c)])
   187  (Ceil        (Const64F [c])) => (Const64F [math.Ceil(c)])
   188  (Trunc       (Const64F [c])) => (Const64F [math.Trunc(c)])
   189  (RoundToEven (Const64F [c])) => (Const64F [math.RoundToEven(c)])
   190  
   191  // Convert x * 1 to x.
   192  (Mul(8|16|32|64)  (Const(8|16|32|64)  [1]) x) => x
   193  (Mul(32|64)uover <t> (Const(32|64) [1]) x) => (MakeTuple x (ConstBool <t.FieldType(1)> [false]))
   194  
   195  // Convert x * -1 to -x.
   196  (Mul(8|16|32|64)  (Const(8|16|32|64)  [-1]) x) => (Neg(8|16|32|64)  x)
   197  
   198  // Convert -x * c to x * -c
   199  (Mul(8|16|32|64) (Const(8|16|32|64) <t> [c]) (Neg(8|16|32|64) x)) => (Mul(8|16|32|64) x (Const(8|16|32|64) <t> [-c]))
   200  
   201  (Mul(8|16|32|64) (Neg(8|16|32|64) x) (Neg(8|16|32|64) y)) => (Mul(8|16|32|64) x y)
   202  
   203  // simplify negative on mul if possible
   204  (Neg(8|16|32|64) (Mul(8|16|32|64) x (Const(8|16|32|64) <t> [c]))) => (Mul(8|16|32|64) x (Const(8|16|32|64) <t> [-c]))
   205  (Neg(8|16|32|64) (Mul(8|16|32|64) x (Neg(8|16|32|64) y))) => (Mul(8|16|32|64) x y)
   206  
   207  // DeMorgan's Laws
   208  (And(8|16|32|64) <t> (Com(8|16|32|64) x) (Com(8|16|32|64) y)) => (Com(8|16|32|64) (Or(8|16|32|64) <t> x y))
   209  (Or(8|16|32|64) <t> (Com(8|16|32|64) x) (Com(8|16|32|64) y)) => (Com(8|16|32|64) (And(8|16|32|64) <t> x y))
   210  
   211  (Mod8  (Const8  [c]) (Const8  [d])) && d != 0 => (Const8  [c % d])
   212  (Mod16 (Const16 [c]) (Const16 [d])) && d != 0 => (Const16 [c % d])
   213  (Mod32 (Const32 [c]) (Const32 [d])) && d != 0 => (Const32 [c % d])
   214  (Mod64 (Const64 [c]) (Const64 [d])) && d != 0 => (Const64 [c % d])
   215  
   216  (Mod8u  (Const8 [c])  (Const8  [d])) && d != 0 => (Const8  [int8(uint8(c) % uint8(d))])
   217  (Mod16u (Const16 [c]) (Const16 [d])) && d != 0 => (Const16 [int16(uint16(c) % uint16(d))])
   218  (Mod32u (Const32 [c]) (Const32 [d])) && d != 0 => (Const32 [int32(uint32(c) % uint32(d))])
   219  (Mod64u (Const64 [c]) (Const64 [d])) && d != 0 => (Const64 [int64(uint64(c) % uint64(d))])
   220  
   221  (Lsh64x64  (Const64 [c]) (Const64 [d])) => (Const64 [c << uint64(d)])
   222  (Rsh64x64  (Const64 [c]) (Const64 [d])) => (Const64 [c >> uint64(d)])
   223  (Rsh64Ux64 (Const64 [c]) (Const64 [d])) => (Const64 [int64(uint64(c) >> uint64(d))])
   224  (Lsh32x64  (Const32 [c]) (Const64 [d])) => (Const32 [c << uint64(d)])
   225  (Rsh32x64  (Const32 [c]) (Const64 [d])) => (Const32 [c >> uint64(d)])
   226  (Rsh32Ux64 (Const32 [c]) (Const64 [d])) => (Const32 [int32(uint32(c) >> uint64(d))])
   227  (Lsh16x64  (Const16 [c]) (Const64 [d])) => (Const16 [c << uint64(d)])
   228  (Rsh16x64  (Const16 [c]) (Const64 [d])) => (Const16 [c >> uint64(d)])
   229  (Rsh16Ux64 (Const16 [c]) (Const64 [d])) => (Const16 [int16(uint16(c) >> uint64(d))])
   230  (Lsh8x64   (Const8  [c]) (Const64 [d])) => (Const8  [c << uint64(d)])
   231  (Rsh8x64   (Const8  [c]) (Const64 [d])) => (Const8  [c >> uint64(d)])
   232  (Rsh8Ux64  (Const8  [c]) (Const64 [d])) => (Const8  [int8(uint8(c) >> uint64(d))])
   233  
   234  // Fold IsInBounds when the range of the index cannot exceed the limit.
   235  (IsInBounds (ZeroExt8to32  _) (Const32 [c])) && (1 << 8)  <= c => (ConstBool [true])
   236  (IsInBounds (ZeroExt8to64  _) (Const64 [c])) && (1 << 8)  <= c => (ConstBool [true])
   237  (IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= c => (ConstBool [true])
   238  (IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c => (ConstBool [true])
   239  (IsInBounds x x) => (ConstBool [false])
   240  (IsInBounds                (And8  (Const8  [c]) _)  (Const8  [d])) && 0 <= c && c < d => (ConstBool [true])
   241  (IsInBounds (ZeroExt8to16  (And8  (Const8  [c]) _)) (Const16 [d])) && 0 <= c && int16(c) < d => (ConstBool [true])
   242  (IsInBounds (ZeroExt8to32  (And8  (Const8  [c]) _)) (Const32 [d])) && 0 <= c && int32(c) < d => (ConstBool [true])
   243  (IsInBounds (ZeroExt8to64  (And8  (Const8  [c]) _)) (Const64 [d])) && 0 <= c && int64(c) < d => (ConstBool [true])
   244  (IsInBounds                (And16 (Const16 [c]) _)  (Const16 [d])) && 0 <= c && c < d => (ConstBool [true])
   245  (IsInBounds (ZeroExt16to32 (And16 (Const16 [c]) _)) (Const32 [d])) && 0 <= c && int32(c) < d => (ConstBool [true])
   246  (IsInBounds (ZeroExt16to64 (And16 (Const16 [c]) _)) (Const64 [d])) && 0 <= c && int64(c) < d => (ConstBool [true])
   247  (IsInBounds                (And32 (Const32 [c]) _)  (Const32 [d])) && 0 <= c && c < d => (ConstBool [true])
   248  (IsInBounds (ZeroExt32to64 (And32 (Const32 [c]) _)) (Const64 [d])) && 0 <= c && int64(c) < d => (ConstBool [true])
   249  (IsInBounds                (And64 (Const64 [c]) _)  (Const64 [d])) && 0 <= c && c < d => (ConstBool [true])
   250  (IsInBounds (Const32 [c]) (Const32 [d])) => (ConstBool [0 <= c && c < d])
   251  (IsInBounds (Const64 [c]) (Const64 [d])) => (ConstBool [0 <= c && c < d])
   252  // (Mod64u x y) is always between 0 (inclusive) and y (exclusive).
   253  (IsInBounds (Mod32u _ y) y) => (ConstBool [true])
   254  (IsInBounds (Mod64u _ y) y) => (ConstBool [true])
   255  // Right shifting an unsigned number limits its value.
   256  (IsInBounds (ZeroExt8to64  (Rsh8Ux64  _ (Const64 [c]))) (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
   257  (IsInBounds (ZeroExt8to32  (Rsh8Ux64  _ (Const64 [c]))) (Const32 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
   258  (IsInBounds (ZeroExt8to16  (Rsh8Ux64  _ (Const64 [c]))) (Const16 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
   259  (IsInBounds                (Rsh8Ux64  _ (Const64 [c]))  (Const64 [d])) && 0 < c && c <  8 && 1<<uint( 8-c)-1 < d => (ConstBool [true])
   260  (IsInBounds (ZeroExt16to64 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d => (ConstBool [true])
   261  (IsInBounds (ZeroExt16to32 (Rsh16Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d => (ConstBool [true])
   262  (IsInBounds                (Rsh16Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 16 && 1<<uint(16-c)-1 < d => (ConstBool [true])
   263  (IsInBounds (ZeroExt32to64 (Rsh32Ux64 _ (Const64 [c]))) (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d => (ConstBool [true])
   264  (IsInBounds                (Rsh32Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 32 && 1<<uint(32-c)-1 < d => (ConstBool [true])
   265  (IsInBounds                (Rsh64Ux64 _ (Const64 [c]))  (Const64 [d])) && 0 < c && c < 64 && 1<<uint(64-c)-1 < d => (ConstBool [true])
   266  
   267  (IsSliceInBounds x x) => (ConstBool [true])
   268  (IsSliceInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c <= d => (ConstBool [true])
   269  (IsSliceInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c <= d => (ConstBool [true])
   270  (IsSliceInBounds (Const32 [0]) _) => (ConstBool [true])
   271  (IsSliceInBounds (Const64 [0]) _) => (ConstBool [true])
   272  (IsSliceInBounds (Const32 [c]) (Const32 [d])) => (ConstBool [0 <= c && c <= d])
   273  (IsSliceInBounds (Const64 [c]) (Const64 [d])) => (ConstBool [0 <= c && c <= d])
   274  (IsSliceInBounds (SliceLen x) (SliceCap x)) => (ConstBool [true])
   275  
   276  (Eq(64|32|16|8|B) x x) => (ConstBool [true])
   277  (EqB (ConstBool [c]) (ConstBool [d])) => (ConstBool [c == d])
   278  (EqB (ConstBool [false]) x) => (Not x)
   279  (EqB (ConstBool [true]) x) => x
   280  (EqB (Not x) y) => (NeqB x y)
   281  
   282  (Neq(64|32|16|8|B) x x) => (ConstBool [false])
   283  (NeqB (ConstBool [c]) (ConstBool [d])) => (ConstBool [c != d])
   284  (NeqB (ConstBool [false]) x) => x
   285  (NeqB (ConstBool [true]) x) => (Not x)
   286  (NeqB (Not x) y) => (EqB x y)
   287  
   288  (Eq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Eq64 (Const64 <t> [c-d]) x)
   289  (Eq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Eq32 (Const32 <t> [c-d]) x)
   290  (Eq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Eq16 (Const16 <t> [c-d]) x)
   291  (Eq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Eq8  (Const8  <t> [c-d]) x)
   292  
   293  (Neq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Neq64 (Const64 <t> [c-d]) x)
   294  (Neq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Neq32 (Const32 <t> [c-d]) x)
   295  (Neq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Neq16 (Const16 <t> [c-d]) x)
   296  (Neq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Neq8  (Const8  <t> [c-d]) x)
   297  
   298  (CondSelect x _ (ConstBool [true ])) => x
   299  (CondSelect _ y (ConstBool [false])) => y
   300  (CondSelect x x _) => x
   301  
   302  // signed integer range: ( c <= x && x (<|<=) d ) -> ( unsigned(x-c) (<|<=) unsigned(d-c) )
   303  (AndB (Leq64 (Const64 [c]) x) ((Less|Leq)64 x (Const64 [d]))) && d >= c => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c])) (Const64 <x.Type> [d-c]))
   304  (AndB (Leq32 (Const32 [c]) x) ((Less|Leq)32 x (Const32 [d]))) && d >= c => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c])) (Const32 <x.Type> [d-c]))
   305  (AndB (Leq16 (Const16 [c]) x) ((Less|Leq)16 x (Const16 [d]))) && d >= c => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c])) (Const16 <x.Type> [d-c]))
   306  (AndB (Leq8  (Const8  [c]) x) ((Less|Leq)8  x (Const8  [d]))) && d >= c => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c])) (Const8  <x.Type> [d-c]))
   307  
   308  // signed integer range: ( c < x && x (<|<=) d ) -> ( unsigned(x-(c+1)) (<|<=) unsigned(d-(c+1)) )
   309  (AndB (Less64 (Const64 [c]) x) ((Less|Leq)64 x (Const64 [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c+1])) (Const64 <x.Type> [d-c-1]))
   310  (AndB (Less32 (Const32 [c]) x) ((Less|Leq)32 x (Const32 [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c+1])) (Const32 <x.Type> [d-c-1]))
   311  (AndB (Less16 (Const16 [c]) x) ((Less|Leq)16 x (Const16 [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c+1])) (Const16 <x.Type> [d-c-1]))
   312  (AndB (Less8  (Const8  [c]) x) ((Less|Leq)8  x (Const8  [d]))) && d >= c+1 && c+1 > c => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c+1])) (Const8  <x.Type> [d-c-1]))
   313  
   314  // unsigned integer range: ( c <= x && x (<|<=) d ) -> ( x-c (<|<=) d-c )
   315  (AndB (Leq64U (Const64 [c]) x) ((Less|Leq)64U x (Const64 [d]))) && uint64(d) >= uint64(c) => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c])) (Const64 <x.Type> [d-c]))
   316  (AndB (Leq32U (Const32 [c]) x) ((Less|Leq)32U x (Const32 [d]))) && uint32(d) >= uint32(c) => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c])) (Const32 <x.Type> [d-c]))
   317  (AndB (Leq16U (Const16 [c]) x) ((Less|Leq)16U x (Const16 [d]))) && uint16(d) >= uint16(c) => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c])) (Const16 <x.Type> [d-c]))
   318  (AndB (Leq8U  (Const8  [c]) x) ((Less|Leq)8U  x (Const8  [d]))) && uint8(d)  >= uint8(c)  => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c])) (Const8  <x.Type> [d-c]))
   319  
   320  // unsigned integer range: ( c < x && x (<|<=) d ) -> ( x-(c+1) (<|<=) d-(c+1) )
   321  (AndB (Less64U (Const64 [c]) x) ((Less|Leq)64U x (Const64 [d]))) && uint64(d) >= uint64(c+1) && uint64(c+1) > uint64(c) => ((Less|Leq)64U (Sub64 <x.Type> x (Const64 <x.Type> [c+1])) (Const64 <x.Type> [d-c-1]))
   322  (AndB (Less32U (Const32 [c]) x) ((Less|Leq)32U x (Const32 [d]))) && uint32(d) >= uint32(c+1) && uint32(c+1) > uint32(c) => ((Less|Leq)32U (Sub32 <x.Type> x (Const32 <x.Type> [c+1])) (Const32 <x.Type> [d-c-1]))
   323  (AndB (Less16U (Const16 [c]) x) ((Less|Leq)16U x (Const16 [d]))) && uint16(d) >= uint16(c+1) && uint16(c+1) > uint16(c) => ((Less|Leq)16U (Sub16 <x.Type> x (Const16 <x.Type> [c+1])) (Const16 <x.Type> [d-c-1]))
   324  (AndB (Less8U  (Const8  [c]) x) ((Less|Leq)8U  x (Const8  [d]))) && uint8(d)  >= uint8(c+1)  && uint8(c+1)  > uint8(c)  => ((Less|Leq)8U  (Sub8  <x.Type> x (Const8  <x.Type> [c+1]))  (Const8  <x.Type> [d-c-1]))
   325  
   326  // signed integer range: ( c (<|<=) x || x < d ) -> ( unsigned(c-d) (<|<=) unsigned(x-d) )
   327  (OrB ((Less|Leq)64 (Const64 [c]) x) (Less64 x (Const64 [d]))) && c >= d => ((Less|Leq)64U (Const64 <x.Type> [c-d]) (Sub64 <x.Type> x (Const64 <x.Type> [d])))
   328  (OrB ((Less|Leq)32 (Const32 [c]) x) (Less32 x (Const32 [d]))) && c >= d => ((Less|Leq)32U (Const32 <x.Type> [c-d]) (Sub32 <x.Type> x (Const32 <x.Type> [d])))
   329  (OrB ((Less|Leq)16 (Const16 [c]) x) (Less16 x (Const16 [d]))) && c >= d => ((Less|Leq)16U (Const16 <x.Type> [c-d]) (Sub16 <x.Type> x (Const16 <x.Type> [d])))
   330  (OrB ((Less|Leq)8  (Const8  [c]) x) (Less8  x (Const8  [d]))) && c >= d => ((Less|Leq)8U  (Const8  <x.Type> [c-d]) (Sub8  <x.Type> x (Const8  <x.Type> [d])))
   331  
   332  // signed integer range: ( c (<|<=) x || x <= d ) -> ( unsigned(c-(d+1)) (<|<=) unsigned(x-(d+1)) )
   333  (OrB ((Less|Leq)64 (Const64 [c]) x) (Leq64 x (Const64 [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)64U (Const64 <x.Type> [c-d-1]) (Sub64 <x.Type> x (Const64 <x.Type> [d+1])))
   334  (OrB ((Less|Leq)32 (Const32 [c]) x) (Leq32 x (Const32 [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)32U (Const32 <x.Type> [c-d-1]) (Sub32 <x.Type> x (Const32 <x.Type> [d+1])))
   335  (OrB ((Less|Leq)16 (Const16 [c]) x) (Leq16 x (Const16 [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)16U (Const16 <x.Type> [c-d-1]) (Sub16 <x.Type> x (Const16 <x.Type> [d+1])))
   336  (OrB ((Less|Leq)8  (Const8  [c]) x) (Leq8  x (Const8  [d]))) && c >= d+1 && d+1 > d => ((Less|Leq)8U  (Const8  <x.Type> [c-d-1]) (Sub8  <x.Type> x (Const8  <x.Type> [d+1])))
   337  
   338  // unsigned integer range: ( c (<|<=) x || x < d ) -> ( c-d (<|<=) x-d )
   339  (OrB ((Less|Leq)64U (Const64 [c]) x) (Less64U x (Const64 [d]))) && uint64(c) >= uint64(d) => ((Less|Leq)64U (Const64 <x.Type> [c-d]) (Sub64 <x.Type> x (Const64 <x.Type> [d])))
   340  (OrB ((Less|Leq)32U (Const32 [c]) x) (Less32U x (Const32 [d]))) && uint32(c) >= uint32(d) => ((Less|Leq)32U (Const32 <x.Type> [c-d]) (Sub32 <x.Type> x (Const32 <x.Type> [d])))
   341  (OrB ((Less|Leq)16U (Const16 [c]) x) (Less16U x (Const16 [d]))) && uint16(c) >= uint16(d) => ((Less|Leq)16U (Const16 <x.Type> [c-d]) (Sub16 <x.Type> x (Const16 <x.Type> [d])))
   342  (OrB ((Less|Leq)8U  (Const8  [c]) x) (Less8U  x (Const8  [d]))) && uint8(c)  >= uint8(d)  => ((Less|Leq)8U  (Const8  <x.Type> [c-d]) (Sub8  <x.Type> x (Const8  <x.Type> [d])))
   343  
   344  // unsigned integer range: ( c (<|<=) x || x <= d ) -> ( c-(d+1) (<|<=) x-(d+1) )
   345  (OrB ((Less|Leq)64U (Const64 [c]) x) (Leq64U x (Const64 [d]))) && uint64(c) >= uint64(d+1) && uint64(d+1) > uint64(d) => ((Less|Leq)64U (Const64 <x.Type> [c-d-1]) (Sub64 <x.Type> x (Const64 <x.Type> [d+1])))
   346  (OrB ((Less|Leq)32U (Const32 [c]) x) (Leq32U x (Const32 [d]))) && uint32(c) >= uint32(d+1) && uint32(d+1) > uint32(d) => ((Less|Leq)32U (Const32 <x.Type> [c-d-1]) (Sub32 <x.Type> x (Const32 <x.Type> [d+1])))
   347  (OrB ((Less|Leq)16U (Const16 [c]) x) (Leq16U x (Const16 [d]))) && uint16(c) >= uint16(d+1) && uint16(d+1) > uint16(d) => ((Less|Leq)16U (Const16 <x.Type> [c-d-1]) (Sub16 <x.Type> x (Const16 <x.Type> [d+1])))
   348  (OrB ((Less|Leq)8U  (Const8  [c]) x) (Leq8U  x (Const8  [d]))) && uint8(c)  >= uint8(d+1)  && uint8(d+1)  > uint8(d)  => ((Less|Leq)8U  (Const8  <x.Type> [c-d-1]) (Sub8  <x.Type> x (Const8  <x.Type> [d+1])))
   349  
   350  // single bit difference: ( x != c && x != d ) -> ( x|(c^d) != c )
   351  (AndB (Neq(64|32|16|8) x cv:(Const(64|32|16|8) [c])) (Neq(64|32|16|8) x (Const(64|32|16|8) [d]))) && c|d == c && oneBit(c^d) => (Neq(64|32|16|8) (Or(64|32|16|8) <x.Type> x (Const(64|32|16|8) <x.Type> [c^d])) cv)
   352  
   353  // single bit difference: ( x == c || x == d ) -> ( x|(c^d) == c )
   354  (OrB (Eq(64|32|16|8) x cv:(Const(64|32|16|8) [c])) (Eq(64|32|16|8) x (Const(64|32|16|8) [d]))) && c|d == c && oneBit(c^d) => (Eq(64|32|16|8) (Or(64|32|16|8) <x.Type> x (Const(64|32|16|8) <x.Type> [c^d])) cv)
   355  
   356  // NaN check: ( x != x || x (>|>=|<|<=) c ) -> ( !(c (>=|>|<=|<) x) )
   357  (OrB (Neq64F x x) ((Less|Leq)64F x y:(Const64F [c]))) => (Not ((Leq|Less)64F y x))
   358  (OrB (Neq64F x x) ((Less|Leq)64F y:(Const64F [c]) x)) => (Not ((Leq|Less)64F x y))
   359  (OrB (Neq32F x x) ((Less|Leq)32F x y:(Const32F [c]))) => (Not ((Leq|Less)32F y x))
   360  (OrB (Neq32F x x) ((Less|Leq)32F y:(Const32F [c]) x)) => (Not ((Leq|Less)32F x y))
   361  
   362  // NaN check: ( x != x || Abs(x) (>|>=|<|<=) c ) -> ( !(c (>=|>|<=|<) Abs(x) )
   363  (OrB (Neq64F x x) ((Less|Leq)64F abs:(Abs x) y:(Const64F [c]))) => (Not ((Leq|Less)64F y abs))
   364  (OrB (Neq64F x x) ((Less|Leq)64F y:(Const64F [c]) abs:(Abs x))) => (Not ((Leq|Less)64F abs y))
   365  
   366  // NaN check: ( x != x || -x (>|>=|<|<=) c ) -> ( !(c (>=|>|<=|<) -x) )
   367  (OrB (Neq64F x x) ((Less|Leq)64F neg:(Neg64F x) y:(Const64F [c]))) => (Not ((Leq|Less)64F y neg))
   368  (OrB (Neq64F x x) ((Less|Leq)64F y:(Const64F [c]) neg:(Neg64F x))) => (Not ((Leq|Less)64F neg y))
   369  (OrB (Neq32F x x) ((Less|Leq)32F neg:(Neg32F x) y:(Const32F [c]))) => (Not ((Leq|Less)32F y neg))
   370  (OrB (Neq32F x x) ((Less|Leq)32F y:(Const32F [c]) neg:(Neg32F x))) => (Not ((Leq|Less)32F neg y))
   371  
   372  // Canonicalize x-const to x+(-const)
   373  (Sub64 x (Const64 <t> [c])) && x.Op != OpConst64 => (Add64 (Const64 <t> [-c]) x)
   374  (Sub32 x (Const32 <t> [c])) && x.Op != OpConst32 => (Add32 (Const32 <t> [-c]) x)
   375  (Sub16 x (Const16 <t> [c])) && x.Op != OpConst16 => (Add16 (Const16 <t> [-c]) x)
   376  (Sub8  x (Const8  <t> [c])) && x.Op != OpConst8  => (Add8  (Const8  <t> [-c]) x)
   377  
   378  // fold negation into comparison operators
   379  (Not (Eq(64|32|16|8|B|Ptr|64F|32F) x y)) => (Neq(64|32|16|8|B|Ptr|64F|32F) x y)
   380  (Not (Neq(64|32|16|8|B|Ptr|64F|32F) x y)) => (Eq(64|32|16|8|B|Ptr|64F|32F) x y)
   381  
   382  (Not (Less(64|32|16|8) x y)) => (Leq(64|32|16|8) y x)
   383  (Not (Less(64|32|16|8)U x y)) => (Leq(64|32|16|8)U y x)
   384  (Not (Leq(64|32|16|8) x y)) => (Less(64|32|16|8) y x)
   385  (Not (Leq(64|32|16|8)U x y)) => (Less(64|32|16|8)U y x)
   386  
   387  // Distribute multiplication c * (d+x) -> c*d + c*x. Useful for:
   388  // a[i].b = ...; a[i+1].b = ...
   389  // The !isPowerOfTwo is a kludge to keep a[i+1] using an index by a multiply,
   390  // which turns into an index by a shift, which can use a shifted operand on ARM systems.
   391  (Mul64 (Const64 <t> [c]) (Add64 <t> (Const64 <t> [d]) x)) && !isPowerOfTwo(c) =>
   392    (Add64 (Const64 <t> [c*d]) (Mul64 <t> (Const64 <t> [c]) x))
   393  (Mul32 (Const32 <t> [c]) (Add32 <t> (Const32 <t> [d]) x)) && !isPowerOfTwo(c) =>
   394    (Add32 (Const32 <t> [c*d]) (Mul32 <t> (Const32 <t> [c]) x))
   395  (Mul16 (Const16 <t> [c]) (Add16 <t> (Const16 <t> [d]) x)) && !isPowerOfTwo(c) =>
   396    (Add16 (Const16 <t> [c*d]) (Mul16 <t> (Const16 <t> [c]) x))
   397  (Mul8 (Const8 <t> [c]) (Add8 <t> (Const8 <t> [d]) x)) && !isPowerOfTwo(c) =>
   398    (Add8 (Const8 <t> [c*d]) (Mul8 <t> (Const8 <t> [c]) x))
   399  
   400  // Rewrite x*y ± x*z  to  x*(y±z)
   401  (Add(64|32|16|8) <t> (Mul(64|32|16|8) x y) (Mul(64|32|16|8) x z))
   402  	=> (Mul(64|32|16|8) x (Add(64|32|16|8) <t> y z))
   403  (Sub(64|32|16|8) <t> (Mul(64|32|16|8) x y) (Mul(64|32|16|8) x z))
   404  	=> (Mul(64|32|16|8) x (Sub(64|32|16|8) <t> y z))
   405  
   406  // rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce
   407  // the number of the other rewrite rules for const shifts
   408  (Lsh64x32  <t> x (Const32 [c])) => (Lsh64x64  x (Const64 <t> [int64(uint32(c))]))
   409  (Lsh64x16  <t> x (Const16 [c])) => (Lsh64x64  x (Const64 <t> [int64(uint16(c))]))
   410  (Lsh64x8   <t> x (Const8  [c])) => (Lsh64x64  x (Const64 <t> [int64(uint8(c))]))
   411  (Rsh64x32  <t> x (Const32 [c])) => (Rsh64x64  x (Const64 <t> [int64(uint32(c))]))
   412  (Rsh64x16  <t> x (Const16 [c])) => (Rsh64x64  x (Const64 <t> [int64(uint16(c))]))
   413  (Rsh64x8   <t> x (Const8  [c])) => (Rsh64x64  x (Const64 <t> [int64(uint8(c))]))
   414  (Rsh64Ux32 <t> x (Const32 [c])) => (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))]))
   415  (Rsh64Ux16 <t> x (Const16 [c])) => (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))]))
   416  (Rsh64Ux8  <t> x (Const8  [c])) => (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))]))
   417  
   418  (Lsh32x32  <t> x (Const32 [c])) => (Lsh32x64  x (Const64 <t> [int64(uint32(c))]))
   419  (Lsh32x16  <t> x (Const16 [c])) => (Lsh32x64  x (Const64 <t> [int64(uint16(c))]))
   420  (Lsh32x8   <t> x (Const8  [c])) => (Lsh32x64  x (Const64 <t> [int64(uint8(c))]))
   421  (Rsh32x32  <t> x (Const32 [c])) => (Rsh32x64  x (Const64 <t> [int64(uint32(c))]))
   422  (Rsh32x16  <t> x (Const16 [c])) => (Rsh32x64  x (Const64 <t> [int64(uint16(c))]))
   423  (Rsh32x8   <t> x (Const8  [c])) => (Rsh32x64  x (Const64 <t> [int64(uint8(c))]))
   424  (Rsh32Ux32 <t> x (Const32 [c])) => (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))]))
   425  (Rsh32Ux16 <t> x (Const16 [c])) => (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))]))
   426  (Rsh32Ux8  <t> x (Const8  [c])) => (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))]))
   427  
   428  (Lsh16x32  <t> x (Const32 [c])) => (Lsh16x64  x (Const64 <t> [int64(uint32(c))]))
   429  (Lsh16x16  <t> x (Const16 [c])) => (Lsh16x64  x (Const64 <t> [int64(uint16(c))]))
   430  (Lsh16x8   <t> x (Const8  [c])) => (Lsh16x64  x (Const64 <t> [int64(uint8(c))]))
   431  (Rsh16x32  <t> x (Const32 [c])) => (Rsh16x64  x (Const64 <t> [int64(uint32(c))]))
   432  (Rsh16x16  <t> x (Const16 [c])) => (Rsh16x64  x (Const64 <t> [int64(uint16(c))]))
   433  (Rsh16x8   <t> x (Const8  [c])) => (Rsh16x64  x (Const64 <t> [int64(uint8(c))]))
   434  (Rsh16Ux32 <t> x (Const32 [c])) => (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))]))
   435  (Rsh16Ux16 <t> x (Const16 [c])) => (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))]))
   436  (Rsh16Ux8  <t> x (Const8  [c])) => (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))]))
   437  
   438  (Lsh8x32  <t> x (Const32 [c])) => (Lsh8x64  x (Const64 <t> [int64(uint32(c))]))
   439  (Lsh8x16  <t> x (Const16 [c])) => (Lsh8x64  x (Const64 <t> [int64(uint16(c))]))
   440  (Lsh8x8   <t> x (Const8  [c])) => (Lsh8x64  x (Const64 <t> [int64(uint8(c))]))
   441  (Rsh8x32  <t> x (Const32 [c])) => (Rsh8x64  x (Const64 <t> [int64(uint32(c))]))
   442  (Rsh8x16  <t> x (Const16 [c])) => (Rsh8x64  x (Const64 <t> [int64(uint16(c))]))
   443  (Rsh8x8   <t> x (Const8  [c])) => (Rsh8x64  x (Const64 <t> [int64(uint8(c))]))
   444  (Rsh8Ux32 <t> x (Const32 [c])) => (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))]))
   445  (Rsh8Ux16 <t> x (Const16 [c])) => (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))]))
   446  (Rsh8Ux8  <t> x (Const8  [c])) => (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))]))
   447  
   448  // shifts by zero
   449  (Lsh(64|32|16|8)x64  x (Const64 [0])) => x
   450  (Rsh(64|32|16|8)x64  x (Const64 [0])) => x
   451  (Rsh(64|32|16|8)Ux64 x (Const64 [0])) => x
   452  
   453  // rotates by multiples of register width
   454  (RotateLeft64 x (Const64 [c])) && c%64 == 0 => x
   455  (RotateLeft32 x (Const32 [c])) && c%32 == 0 => x
   456  (RotateLeft16 x (Const16 [c])) && c%16 == 0 => x
   457  (RotateLeft8  x (Const8 [c]))  && c%8  == 0 => x
   458  
   459  // zero shifted
   460  (Lsh64x(64|32|16|8)  (Const64 [0]) _) => (Const64 [0])
   461  (Rsh64x(64|32|16|8)  (Const64 [0]) _) => (Const64 [0])
   462  (Rsh64Ux(64|32|16|8) (Const64 [0]) _) => (Const64 [0])
   463  (Lsh32x(64|32|16|8)  (Const32 [0]) _) => (Const32 [0])
   464  (Rsh32x(64|32|16|8)  (Const32 [0]) _) => (Const32 [0])
   465  (Rsh32Ux(64|32|16|8) (Const32 [0]) _) => (Const32 [0])
   466  (Lsh16x(64|32|16|8)  (Const16 [0]) _) => (Const16 [0])
   467  (Rsh16x(64|32|16|8)  (Const16 [0]) _) => (Const16 [0])
   468  (Rsh16Ux(64|32|16|8) (Const16 [0]) _) => (Const16 [0])
   469  (Lsh8x(64|32|16|8)   (Const8  [0]) _) => (Const8  [0])
   470  (Rsh8x(64|32|16|8)   (Const8  [0]) _) => (Const8  [0])
   471  (Rsh8Ux(64|32|16|8)  (Const8  [0]) _) => (Const8  [0])
   472  
   473  // large left shifts of all values, and right shifts of unsigned values
   474  ((Lsh64|Rsh64U)x64  _ (Const64 [c])) && uint64(c) >= 64 => (Const64 [0])
   475  ((Lsh32|Rsh32U)x64  _ (Const64 [c])) && uint64(c) >= 32 => (Const32 [0])
   476  ((Lsh16|Rsh16U)x64  _ (Const64 [c])) && uint64(c) >= 16 => (Const16 [0])
   477  ((Lsh8|Rsh8U)x64    _ (Const64 [c])) && uint64(c) >= 8  => (Const8  [0])
   478  
   479  // combine const shifts
   480  (Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh64x64 x (Const64 <t> [c+d]))
   481  (Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh32x64 x (Const64 <t> [c+d]))
   482  (Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh16x64 x (Const64 <t> [c+d]))
   483  (Lsh8x64  <t> (Lsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Lsh8x64  x (Const64 <t> [c+d]))
   484  
   485  (Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh64x64 x (Const64 <t> [c+d]))
   486  (Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh32x64 x (Const64 <t> [c+d]))
   487  (Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh16x64 x (Const64 <t> [c+d]))
   488  (Rsh8x64  <t> (Rsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh8x64  x (Const64 <t> [c+d]))
   489  
   490  (Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh64Ux64 x (Const64 <t> [c+d]))
   491  (Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh32Ux64 x (Const64 <t> [c+d]))
   492  (Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh16Ux64 x (Const64 <t> [c+d]))
   493  (Rsh8Ux64  <t> (Rsh8Ux64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) => (Rsh8Ux64  x (Const64 <t> [c+d]))
   494  
   495  // Remove signed right shift before an unsigned right shift that extracts the sign bit.
   496  (Rsh8Ux64  (Rsh8x64  x _) (Const64 <t> [7] )) => (Rsh8Ux64  x (Const64 <t> [7] ))
   497  (Rsh16Ux64 (Rsh16x64 x _) (Const64 <t> [15])) => (Rsh16Ux64 x (Const64 <t> [15]))
   498  (Rsh32Ux64 (Rsh32x64 x _) (Const64 <t> [31])) => (Rsh32Ux64 x (Const64 <t> [31]))
   499  (Rsh64Ux64 (Rsh64x64 x _) (Const64 <t> [63])) => (Rsh64Ux64 x (Const64 <t> [63]))
   500  
   501  // Convert x>>c<<c to x&^(1<<c-1)
   502  (Lsh64x64 i:(Rsh(64|64U)x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 64 && i.Uses == 1 => (And64 x (Const64 <v.Type> [int64(-1) << c]))
   503  (Lsh32x64 i:(Rsh(32|32U)x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 32 && i.Uses == 1 => (And32 x (Const32 <v.Type> [int32(-1) << c]))
   504  (Lsh16x64 i:(Rsh(16|16U)x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 16 && i.Uses == 1 => (And16 x (Const16 <v.Type> [int16(-1) << c]))
   505  (Lsh8x64  i:(Rsh(8|8U)x64    x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 8  && i.Uses == 1 => (And8  x (Const8  <v.Type> [int8(-1)  << c]))
   506  // similarly for x<<c>>c
   507  (Rsh64Ux64 i:(Lsh64x64 x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 64 && i.Uses == 1 => (And64 x (Const64 <v.Type> [int64(^uint64(0)>>c)]))
   508  (Rsh32Ux64 i:(Lsh32x64 x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 32 && i.Uses == 1 => (And32 x (Const32 <v.Type> [int32(^uint32(0)>>c)]))
   509  (Rsh16Ux64 i:(Lsh16x64 x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 16 && i.Uses == 1 => (And16 x (Const16 <v.Type> [int16(^uint16(0)>>c)]))
   510  (Rsh8Ux64  i:(Lsh8x64  x (Const64 [c])) (Const64 [c])) && c >= 0 && c < 8  && i.Uses == 1 => (And8  x (Const8  <v.Type> [int8 (^uint8 (0)>>c)]))
   511  
   512  // ((x >> c1) << c2) >> c3
   513  (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]))
   514    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   515    => (Rsh(64|32|16|8)Ux64 x (Const64 <typ.UInt64> [c1-c2+c3]))
   516  
   517  // ((x << c1) >> c2) << c3
   518  (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]))
   519    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   520    => (Lsh(64|32|16|8)x64 x (Const64 <typ.UInt64> [c1-c2+c3]))
   521  
   522  // (x >> c) & uppermask = 0
   523  (And64 (Const64 [m]) (Rsh64Ux64 _ (Const64 [c]))) && c >= int64(64-ntz64(m)) => (Const64 [0])
   524  (And32 (Const32 [m]) (Rsh32Ux64 _ (Const64 [c]))) && c >= int64(32-ntz32(m)) => (Const32 [0])
   525  (And16 (Const16 [m]) (Rsh16Ux64 _ (Const64 [c]))) && c >= int64(16-ntz16(m)) => (Const16 [0])
   526  (And8  (Const8  [m]) (Rsh8Ux64  _ (Const64 [c]))) && c >= int64(8-ntz8(m))  => (Const8  [0])
   527  
   528  // (x << c) & lowermask = 0
   529  (And64 (Const64 [m]) (Lsh64x64  _ (Const64 [c]))) && c >= int64(64-nlz64(m)) => (Const64 [0])
   530  (And32 (Const32 [m]) (Lsh32x64  _ (Const64 [c]))) && c >= int64(32-nlz32(m)) => (Const32 [0])
   531  (And16 (Const16 [m]) (Lsh16x64  _ (Const64 [c]))) && c >= int64(16-nlz16(m)) => (Const16 [0])
   532  (And8  (Const8  [m]) (Lsh8x64   _ (Const64 [c]))) && c >= int64(8-nlz8(m))  => (Const8  [0])
   533  
   534  // replace shifts with zero extensions
   535  (Rsh16Ux64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) => (ZeroExt8to16  (Trunc16to8  <typ.UInt8>  x))
   536  (Rsh32Ux64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) => (ZeroExt8to32  (Trunc32to8  <typ.UInt8>  x))
   537  (Rsh64Ux64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) => (ZeroExt8to64  (Trunc64to8  <typ.UInt8>  x))
   538  (Rsh32Ux64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) => (ZeroExt16to32 (Trunc32to16 <typ.UInt16> x))
   539  (Rsh64Ux64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) => (ZeroExt16to64 (Trunc64to16 <typ.UInt16> x))
   540  (Rsh64Ux64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) => (ZeroExt32to64 (Trunc64to32 <typ.UInt32> x))
   541  
   542  // replace shifts with sign extensions
   543  (Rsh16x64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) => (SignExt8to16  (Trunc16to8  <typ.Int8>  x))
   544  (Rsh32x64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) => (SignExt8to32  (Trunc32to8  <typ.Int8>  x))
   545  (Rsh64x64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) => (SignExt8to64  (Trunc64to8  <typ.Int8>  x))
   546  (Rsh32x64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) => (SignExt16to32 (Trunc32to16 <typ.Int16> x))
   547  (Rsh64x64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) => (SignExt16to64 (Trunc64to16 <typ.Int16> x))
   548  (Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) => (SignExt32to64 (Trunc64to32 <typ.Int32> x))
   549  
   550  // ((x >> c) & d) << e
   551  (Lsh64x64 (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e])) && c >= e => (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c-e])) (Const64 <t> [d<<e]))
   552  (Lsh32x64 (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e])) && c >= e => (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c-e])) (Const32 <t> [d<<e]))
   553  (Lsh16x64 (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e])) && c >= e => (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c-e])) (Const16 <t> [d<<e]))
   554  (Lsh8x64  (And8  (Rsh(8|8U)x64   <t> x (Const64 <t2> [c])) (Const8  [d])) (Const64 [e])) && c >= e => (And8  (Rsh(8|8U)x64   <t> x (Const64 <t2> [c-e])) (Const8  <t> [d<<e]))
   555  (Lsh64x64 (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e])) && c < e =>  (And64 (Lsh64x64 <t> x (Const64 <t2> [e-c])) (Const64 <t> [d<<e]))
   556  (Lsh32x64 (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e])) && c < e =>  (And32 (Lsh32x64 <t> x (Const64 <t2> [e-c])) (Const32 <t> [d<<e]))
   557  (Lsh16x64 (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e])) && c < e =>  (And16 (Lsh16x64 <t> x (Const64 <t2> [e-c])) (Const16 <t> [d<<e]))
   558  (Lsh8x64  (And8  (Rsh(8|8U)x64   <t> x (Const64 <t2> [c])) (Const8  [d])) (Const64 [e])) && c < e =>  (And8  (Lsh8x64  <t> x (Const64 <t2> [e-c])) (Const8  <t> [d<<e]))
   559  
   560  // constant comparisons
   561  (Eq(64|32|16|8)   (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c == d])
   562  (Neq(64|32|16|8)  (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c != d])
   563  (Less(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c < d])
   564  (Leq(64|32|16|8)  (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c <= d])
   565  
   566  (Less64U (Const64 [c]) (Const64 [d])) => (ConstBool [uint64(c) < uint64(d)])
   567  (Less32U (Const32 [c]) (Const32 [d])) => (ConstBool [uint32(c) < uint32(d)])
   568  (Less16U (Const16 [c]) (Const16 [d])) => (ConstBool [uint16(c) < uint16(d)])
   569  (Less8U  (Const8  [c]) (Const8  [d])) => (ConstBool [ uint8(c) <  uint8(d)])
   570  
   571  (Leq64U (Const64 [c]) (Const64 [d])) => (ConstBool [uint64(c) <= uint64(d)])
   572  (Leq32U (Const32 [c]) (Const32 [d])) => (ConstBool [uint32(c) <= uint32(d)])
   573  (Leq16U (Const16 [c]) (Const16 [d])) => (ConstBool [uint16(c) <= uint16(d)])
   574  (Leq8U  (Const8  [c]) (Const8  [d])) => (ConstBool [ uint8(c) <=  uint8(d)])
   575  
   576  (Leq8  (Const8  [0]) (And8  _ (Const8  [c]))) && c >= 0 => (ConstBool [true])
   577  (Leq16 (Const16 [0]) (And16 _ (Const16 [c]))) && c >= 0 => (ConstBool [true])
   578  (Leq32 (Const32 [0]) (And32 _ (Const32 [c]))) && c >= 0 => (ConstBool [true])
   579  (Leq64 (Const64 [0]) (And64 _ (Const64 [c]))) && c >= 0 => (ConstBool [true])
   580  
   581  (Leq8  (Const8  [0]) (Rsh8Ux64  _ (Const64 [c]))) && c > 0 => (ConstBool [true])
   582  (Leq16 (Const16 [0]) (Rsh16Ux64 _ (Const64 [c]))) && c > 0 => (ConstBool [true])
   583  (Leq32 (Const32 [0]) (Rsh32Ux64 _ (Const64 [c]))) && c > 0 => (ConstBool [true])
   584  (Leq64 (Const64 [0]) (Rsh64Ux64 _ (Const64 [c]))) && c > 0 => (ConstBool [true])
   585  
   586  // prefer equalities with zero
   587  (Less(64|32|16|8) (Const(64|32|16|8) <t> [0]) x) && isNonNegative(x) => (Neq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   588  (Less(64|32|16|8) x (Const(64|32|16|8) <t> [1])) && isNonNegative(x) => (Eq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   589  (Less(64|32|16|8)U x (Const(64|32|16|8) <t> [1])) => (Eq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   590  (Leq(64|32|16|8)U (Const(64|32|16|8) <t> [1]) x) => (Neq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   591  
   592  // prefer comparisons with zero
   593  (Less(64|32|16|8) x (Const(64|32|16|8) <t> [1])) => (Leq(64|32|16|8) x (Const(64|32|16|8) <t> [0]))
   594  (Leq(64|32|16|8) x (Const(64|32|16|8) <t> [-1])) => (Less(64|32|16|8) x (Const(64|32|16|8) <t> [0]))
   595  (Leq(64|32|16|8) (Const(64|32|16|8) <t> [1]) x) => (Less(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   596  (Less(64|32|16|8) (Const(64|32|16|8) <t> [-1]) x) => (Leq(64|32|16|8) (Const(64|32|16|8) <t> [0]) x)
   597  
   598  // constant floating point comparisons
   599  (Eq32F   (Const32F [c]) (Const32F [d])) => (ConstBool [c == d])
   600  (Eq64F   (Const64F [c]) (Const64F [d])) => (ConstBool [c == d])
   601  (Neq32F  (Const32F [c]) (Const32F [d])) => (ConstBool [c != d])
   602  (Neq64F  (Const64F [c]) (Const64F [d])) => (ConstBool [c != d])
   603  (Less32F (Const32F [c]) (Const32F [d])) => (ConstBool [c < d])
   604  (Less64F (Const64F [c]) (Const64F [d])) => (ConstBool [c < d])
   605  (Leq32F  (Const32F [c]) (Const32F [d])) => (ConstBool [c <= d])
   606  (Leq64F  (Const64F [c]) (Const64F [d])) => (ConstBool [c <= d])
   607  
   608  // simplifications
   609  (Or(64|32|16|8) x x) => x
   610  (Or(64|32|16|8) (Const(64|32|16|8)  [0]) x) => x
   611  (Or(64|32|16|8) (Const(64|32|16|8) [-1]) _) => (Const(64|32|16|8) [-1])
   612  (Or(64|32|16|8) (Com(64|32|16|8)     x)  x) => (Const(64|32|16|8) [-1])
   613  
   614  (And(64|32|16|8) x x) => x
   615  (And(64|32|16|8) (Const(64|32|16|8) [-1]) x) => x
   616  (And(64|32|16|8) (Const(64|32|16|8)  [0]) _) => (Const(64|32|16|8) [0])
   617  (And(64|32|16|8) (Com(64|32|16|8)     x)  x) => (Const(64|32|16|8) [0])
   618  
   619  (Xor(64|32|16|8) x x) => (Const(64|32|16|8) [0])
   620  (Xor(64|32|16|8) (Const(64|32|16|8) [0]) x) => x
   621  (Xor(64|32|16|8) (Com(64|32|16|8)    x)  x) => (Const(64|32|16|8) [-1])
   622  
   623  (Add(64|32|16|8) (Const(64|32|16|8) [0]) x) => x
   624  (Sub(64|32|16|8) x x) => (Const(64|32|16|8) [0])
   625  (Mul(64|32|16|8) (Const(64|32|16|8) [0]) _) => (Const(64|32|16|8) [0])
   626  (Mul(64|32)uover <t> (Const(64|32) [0]) x) => (MakeTuple (Const(64|32) <t.FieldType(0)> [0]) (ConstBool <t.FieldType(1)> [false]))
   627  
   628  (Com(64|32|16|8) (Com(64|32|16|8)  x)) => x
   629  (Com(64|32|16|8) (Const(64|32|16|8) [c])) => (Const(64|32|16|8) [^c])
   630  
   631  (Neg(64|32|16|8) (Sub(64|32|16|8) x y)) => (Sub(64|32|16|8) y x)
   632  (Add(64|32|16|8) x (Neg(64|32|16|8) y)) => (Sub(64|32|16|8) x y)
   633  
   634  (Xor(64|32|16|8) (Const(64|32|16|8) [-1]) x) => (Com(64|32|16|8) x)
   635  
   636  (Sub(64|32|16|8) (Neg(64|32|16|8) x) (Com(64|32|16|8) x)) => (Const(64|32|16|8) [1])
   637  (Sub(64|32|16|8) (Com(64|32|16|8) x) (Neg(64|32|16|8) x)) => (Const(64|32|16|8) [-1])
   638  (Add(64|32|16|8) (Com(64|32|16|8) x)                  x)  => (Const(64|32|16|8) [-1])
   639  
   640  // Prove does not simplify this because x + y might overflow into carry,
   641  // however if no one care about the carry, let it overflow in a normal add.
   642  (Select0 a:(Add64carry x y (Const64 [0]))) && a.Uses == 1 => (Add64 x y)
   643  
   644  // Simplification when involving common integer
   645  // (t + x) - (t + y) == x - y
   646  // (t + x) - (y + t) == x - y
   647  // (x + t) - (y + t) == x - y
   648  // (x + t) - (t + y) == x - y
   649  // (x - t) + (t + y) == x + y
   650  // (x - t) + (y + t) == x + y
   651  (Sub(64|32|16|8) (Add(64|32|16|8) t x) (Add(64|32|16|8) t y)) => (Sub(64|32|16|8) x y)
   652  (Add(64|32|16|8) (Sub(64|32|16|8) x t) (Add(64|32|16|8) t y)) => (Add(64|32|16|8) x y)
   653  
   654  // ^(x-1) == ^x+1 == -x
   655  (Add(64|32|16|8) (Const(64|32|16|8) [1]) (Com(64|32|16|8) x)) => (Neg(64|32|16|8) x)
   656  (Com(64|32|16|8) (Add(64|32|16|8) (Const(64|32|16|8) [-1]) x)) => (Neg(64|32|16|8) x)
   657  
   658  // -(-x) == x
   659  (Neg(64|32|16|8) (Neg(64|32|16|8) x)) => x
   660  
   661  // -^x == x+1
   662  (Neg(64|32|16|8) <t> (Com(64|32|16|8) x)) => (Add(64|32|16|8) (Const(64|32|16|8) <t> [1]) x)
   663  
   664  (And(64|32|16|8) x (And(64|32|16|8) x y)) => (And(64|32|16|8) x y)
   665  (Or(64|32|16|8) x (Or(64|32|16|8) x y)) => (Or(64|32|16|8) x y)
   666  (Xor(64|32|16|8) x (Xor(64|32|16|8) x y)) => y
   667  
   668  // Fold comparisons with numeric bounds
   669  (Less(64|32|16|8)U _ (Const(64|32|16|8) [0]))  => (ConstBool [false])
   670  (Leq(64|32|16|8)U (Const(64|32|16|8) [0]) _)   => (ConstBool [true])
   671  (Less(64|32|16|8)U (Const(64|32|16|8) [-1]) _) => (ConstBool [false])
   672  (Leq(64|32|16|8)U _ (Const(64|32|16|8) [-1]))  => (ConstBool [true])
   673  (Less64 _ (Const64 [math.MinInt64])) => (ConstBool [false])
   674  (Less32 _ (Const32 [math.MinInt32])) => (ConstBool [false])
   675  (Less16 _ (Const16 [math.MinInt16])) => (ConstBool [false])
   676  (Less8  _ (Const8  [math.MinInt8 ])) => (ConstBool [false])
   677  (Leq64 (Const64 [math.MinInt64]) _)  => (ConstBool [true])
   678  (Leq32 (Const32 [math.MinInt32]) _)  => (ConstBool [true])
   679  (Leq16 (Const16 [math.MinInt16]) _)  => (ConstBool [true])
   680  (Leq8  (Const8  [math.MinInt8 ]) _)  => (ConstBool [true])
   681  (Less64 (Const64 [math.MaxInt64]) _) => (ConstBool [false])
   682  (Less32 (Const32 [math.MaxInt32]) _) => (ConstBool [false])
   683  (Less16 (Const16 [math.MaxInt16]) _) => (ConstBool [false])
   684  (Less8  (Const8  [math.MaxInt8 ]) _) => (ConstBool [false])
   685  (Leq64 _ (Const64 [math.MaxInt64]))  => (ConstBool [true])
   686  (Leq32 _ (Const32 [math.MaxInt32]))  => (ConstBool [true])
   687  (Leq16 _ (Const16 [math.MaxInt16]))  => (ConstBool [true])
   688  (Leq8  _ (Const8  [math.MaxInt8 ]))  => (ConstBool [true])
   689  
   690  // Canonicalize <= on numeric bounds and < near numeric bounds to ==
   691  (Leq(64|32|16|8)U x c:(Const(64|32|16|8) [0]))     => (Eq(64|32|16|8) x c)
   692  (Leq(64|32|16|8)U c:(Const(64|32|16|8) [-1]) x)    => (Eq(64|32|16|8) x c)
   693  (Less(64|32|16|8)U x (Const(64|32|16|8) <t> [1]))  => (Eq(64|32|16|8) x (Const(64|32|16|8) <t> [0]))
   694  (Less(64|32|16|8)U (Const(64|32|16|8) <t> [-2]) x) => (Eq(64|32|16|8) x (Const(64|32|16|8) <t> [-1]))
   695  (Leq64 x c:(Const64 [math.MinInt64])) => (Eq64 x c)
   696  (Leq32 x c:(Const32 [math.MinInt32])) => (Eq32 x c)
   697  (Leq16 x c:(Const16 [math.MinInt16])) => (Eq16 x c)
   698  (Leq8  x c:(Const8  [math.MinInt8 ])) => (Eq8  x c)
   699  (Leq64 c:(Const64 [math.MaxInt64]) x) => (Eq64 x c)
   700  (Leq32 c:(Const32 [math.MaxInt32]) x) => (Eq32 x c)
   701  (Leq16 c:(Const16 [math.MaxInt16]) x) => (Eq16 x c)
   702  (Leq8  c:(Const8  [math.MaxInt8 ]) x) => (Eq8  x c)
   703  (Less64 x (Const64 <t> [math.MinInt64+1])) => (Eq64 x (Const64 <t> [math.MinInt64]))
   704  (Less32 x (Const32 <t> [math.MinInt32+1])) => (Eq32 x (Const32 <t> [math.MinInt32]))
   705  (Less16 x (Const16 <t> [math.MinInt16+1])) => (Eq16 x (Const16 <t> [math.MinInt16]))
   706  (Less8  x (Const8  <t> [math.MinInt8 +1])) => (Eq8  x (Const8  <t> [math.MinInt8 ]))
   707  (Less64 (Const64 <t> [math.MaxInt64-1]) x) => (Eq64 x (Const64 <t> [math.MaxInt64]))
   708  (Less32 (Const32 <t> [math.MaxInt32-1]) x) => (Eq32 x (Const32 <t> [math.MaxInt32]))
   709  (Less16 (Const16 <t> [math.MaxInt16-1]) x) => (Eq16 x (Const16 <t> [math.MaxInt16]))
   710  (Less8  (Const8  <t> [math.MaxInt8 -1]) x) => (Eq8  x (Const8  <t> [math.MaxInt8 ]))
   711  
   712  // Ands clear bits. Ors set bits.
   713  // If a subsequent Or will set all the bits
   714  // that an And cleared, we can skip the And.
   715  // This happens in bitmasking code like:
   716  //   x &^= 3 << shift // clear two old bits
   717  //   x  |= v << shift // set two new bits
   718  // when shift is a small constant and v ends up a constant 3.
   719  (Or8  (And8  x (Const8  [c2])) (Const8  <t> [c1])) && ^(c1 | c2) == 0 => (Or8  (Const8  <t> [c1]) x)
   720  (Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 => (Or16 (Const16 <t> [c1]) x)
   721  (Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 => (Or32 (Const32 <t> [c1]) x)
   722  (Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 => (Or64 (Const64 <t> [c1]) x)
   723  
   724  (Trunc64to8  (And64 (Const64 [y]) x)) && y&0xFF == 0xFF => (Trunc64to8 x)
   725  (Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF => (Trunc64to16 x)
   726  (Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF => (Trunc64to32 x)
   727  (Trunc32to8  (And32 (Const32 [y]) x)) && y&0xFF == 0xFF => (Trunc32to8 x)
   728  (Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF => (Trunc32to16 x)
   729  (Trunc16to8  (And16 (Const16 [y]) x)) && y&0xFF == 0xFF => (Trunc16to8 x)
   730  
   731  (ZeroExt8to64  (Trunc64to8  x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 => x
   732  (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 => x
   733  (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 => x
   734  (ZeroExt8to32  (Trunc32to8  x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 => x
   735  (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 => x
   736  (ZeroExt8to16  (Trunc16to8  x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 => x
   737  
   738  (SignExt8to64  (Trunc64to8  x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 => x
   739  (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 => x
   740  (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 => x
   741  (SignExt8to32  (Trunc32to8  x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 => x
   742  (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 => x
   743  (SignExt8to16  (Trunc16to8  x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 => x
   744  
   745  (Slicemask (Const32 [x])) && x > 0 => (Const32 [-1])
   746  (Slicemask (Const32 [0]))          => (Const32 [0])
   747  (Slicemask (Const64 [x])) && x > 0 => (Const64 [-1])
   748  (Slicemask (Const64 [0]))          => (Const64 [0])
   749  
   750  // simplifications often used for lengths.  e.g. len(s[i:i+5])==5
   751  (Sub(64|32|16|8) (Add(64|32|16|8) x y) x) => y
   752  (Sub(64|32|16|8) (Add(64|32|16|8) x y) y) => x
   753  (Sub(64|32|16|8) (Sub(64|32|16|8) x y) x) => (Neg(64|32|16|8) y)
   754  (Sub(64|32|16|8) x (Add(64|32|16|8) x y)) => (Neg(64|32|16|8) y)
   755  (Add(64|32|16|8) x (Sub(64|32|16|8) y x)) => y
   756  (Add(64|32|16|8) x (Add(64|32|16|8) y (Sub(64|32|16|8) z x))) => (Add(64|32|16|8) y z)
   757  
   758  // basic phi simplifications
   759  (Phi (Const8  [c]) (Const8  [c])) => (Const8  [c])
   760  (Phi (Const16 [c]) (Const16 [c])) => (Const16 [c])
   761  (Phi (Const32 [c]) (Const32 [c])) => (Const32 [c])
   762  (Phi (Const64 [c]) (Const64 [c])) => (Const64 [c])
   763  
   764  // slice and interface comparisons
   765  // The frontend ensures that we can only compare against nil,
   766  // so we need only compare the first word (interface type or slice ptr).
   767  (EqInter x y)  => (EqPtr  (ITab x) (ITab y))
   768  (NeqInter x y) => (NeqPtr (ITab x) (ITab y))
   769  (EqSlice x y)  => (EqPtr  (SlicePtr x) (SlicePtr y))
   770  (NeqSlice x y) => (NeqPtr (SlicePtr x) (SlicePtr y))
   771  
   772  // Load of store of same address, with compatibly typed value and same size
   773  (Load <t1> p1 (Store {t2} p2 x _))
   774  	&& isSamePtr(p1, p2)
   775  	&& copyCompatibleType(t1, x.Type)
   776  	&& t1.Size() == t2.Size()
   777  	=> x
   778  (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 x _)))
   779  	&& isSamePtr(p1, p3)
   780  	&& copyCompatibleType(t1, x.Type)
   781  	&& t1.Size() == t3.Size()
   782  	&& disjoint(p3, t3.Size(), p2, t2.Size())
   783  	=> x
   784  (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 x _))))
   785  	&& isSamePtr(p1, p4)
   786  	&& copyCompatibleType(t1, x.Type)
   787  	&& t1.Size() == t4.Size()
   788  	&& disjoint(p4, t4.Size(), p2, t2.Size())
   789  	&& disjoint(p4, t4.Size(), p3, t3.Size())
   790  	=> x
   791  (Load <t1> p1 (Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 x _)))))
   792  	&& isSamePtr(p1, p5)
   793  	&& copyCompatibleType(t1, x.Type)
   794  	&& t1.Size() == t5.Size()
   795  	&& disjoint(p5, t5.Size(), p2, t2.Size())
   796  	&& disjoint(p5, t5.Size(), p3, t3.Size())
   797  	&& disjoint(p5, t5.Size(), p4, t4.Size())
   798  	=> x
   799  
   800  // Load from a region just copied by Move can read directly from the source.
   801  (Load <t1> op:(OffPtr [o1] p1) move:(Move [n] p2 src mem))
   802  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p2)
   803  	&& !isVolatile(src)
   804  	=> @move.Block (Load <t1> (OffPtr <op.Type> [o1] src) mem)
   805  
   806  // Pass constants through math.Float{32,64}bits and math.Float{32,64}frombits
   807  (Load <t1> p1 (Store {t2} p2 (Const64  [x]) _)) && isSamePtr(p1,p2) && t2.Size() == 8 && is64BitFloat(t1) && !math.IsNaN(math.Float64frombits(uint64(x))) => (Const64F [math.Float64frombits(uint64(x))])
   808  (Load <t1> p1 (Store {t2} p2 (Const32  [x]) _)) && isSamePtr(p1,p2) && t2.Size() == 4 && is32BitFloat(t1) && !math.IsNaN(float64(math.Float32frombits(uint32(x)))) => (Const32F [math.Float32frombits(uint32(x))])
   809  (Load <t1> p1 (Store {t2} p2 (Const64F [x]) _)) && isSamePtr(p1,p2) && t2.Size() == 8 && is64BitInt(t1)   => (Const64  [int64(math.Float64bits(x))])
   810  (Load <t1> p1 (Store {t2} p2 (Const32F [x]) _)) && isSamePtr(p1,p2) && t2.Size() == 4 && is32BitInt(t1)   => (Const32  [int32(math.Float32bits(x))])
   811  
   812  // Float Loads up to Zeros so they can be constant folded.
   813  (Load <t1> op:(OffPtr [o1] p1)
   814  	(Store {t2} p2 _
   815  		mem:(Zero [n] p3 _)))
   816  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p3)
   817  	&& CanSSA(t1)
   818  	&& disjoint(op, t1.Size(), p2, t2.Size())
   819  	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p3) mem)
   820  (Load <t1> op:(OffPtr [o1] p1)
   821  	(Store {t2} p2 _
   822  		(Store {t3} p3 _
   823  			mem:(Zero [n] p4 _))))
   824  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p4)
   825  	&& CanSSA(t1)
   826  	&& disjoint(op, t1.Size(), p2, t2.Size())
   827  	&& disjoint(op, t1.Size(), p3, t3.Size())
   828  	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p4) mem)
   829  (Load <t1> op:(OffPtr [o1] p1)
   830  	(Store {t2} p2 _
   831  		(Store {t3} p3 _
   832  			(Store {t4} p4 _
   833  				mem:(Zero [n] p5 _)))))
   834  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p5)
   835  	&& CanSSA(t1)
   836  	&& disjoint(op, t1.Size(), p2, t2.Size())
   837  	&& disjoint(op, t1.Size(), p3, t3.Size())
   838  	&& disjoint(op, t1.Size(), p4, t4.Size())
   839  	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p5) mem)
   840  (Load <t1> op:(OffPtr [o1] p1)
   841  	(Store {t2} p2 _
   842  		(Store {t3} p3 _
   843  			(Store {t4} p4 _
   844  				(Store {t5} p5 _
   845  					mem:(Zero [n] p6 _))))))
   846  	&& o1 >= 0 && o1+t1.Size() <= n && isSamePtr(p1, p6)
   847  	&& CanSSA(t1)
   848  	&& disjoint(op, t1.Size(), p2, t2.Size())
   849  	&& disjoint(op, t1.Size(), p3, t3.Size())
   850  	&& disjoint(op, t1.Size(), p4, t4.Size())
   851  	&& disjoint(op, t1.Size(), p5, t5.Size())
   852  	=> @mem.Block (Load <t1> (OffPtr <op.Type> [o1] p6) mem)
   853  
   854  // Zero to Load forwarding.
   855  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   856  	&& t1.IsBoolean()
   857  	&& isSamePtr(p1, p2)
   858  	&& n >= o + 1
   859  	=> (ConstBool [false])
   860  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   861  	&& is8BitInt(t1)
   862  	&& isSamePtr(p1, p2)
   863  	&& n >= o + 1
   864  	=> (Const8 [0])
   865  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   866  	&& is16BitInt(t1)
   867  	&& isSamePtr(p1, p2)
   868  	&& n >= o + 2
   869  	=> (Const16 [0])
   870  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   871  	&& is32BitInt(t1)
   872  	&& isSamePtr(p1, p2)
   873  	&& n >= o + 4
   874  	=> (Const32 [0])
   875  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   876  	&& is64BitInt(t1)
   877  	&& isSamePtr(p1, p2)
   878  	&& n >= o + 8
   879  	=> (Const64 [0])
   880  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   881  	&& is32BitFloat(t1)
   882  	&& isSamePtr(p1, p2)
   883  	&& n >= o + 4
   884  	=> (Const32F [0])
   885  (Load <t1> (OffPtr [o] p1) (Zero [n] p2 _))
   886  	&& is64BitFloat(t1)
   887  	&& isSamePtr(p1, p2)
   888  	&& n >= o + 8
   889  	=> (Const64F [0])
   890  
   891  // Eliminate stores of values that have just been loaded from the same location.
   892  // We also handle the common case where there are some intermediate stores.
   893  (Store {t1} p1 (Load <t2> p2 mem) mem)
   894  	&& isSamePtr(p1, p2)
   895  	&& t2.Size() == t1.Size()
   896  	=> mem
   897  (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ oldmem))
   898  	&& isSamePtr(p1, p2)
   899  	&& t2.Size() == t1.Size()
   900  	&& disjoint(p1, t1.Size(), p3, t3.Size())
   901  	=> mem
   902  (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ oldmem)))
   903  	&& isSamePtr(p1, p2)
   904  	&& t2.Size() == t1.Size()
   905  	&& disjoint(p1, t1.Size(), p3, t3.Size())
   906  	&& disjoint(p1, t1.Size(), p4, t4.Size())
   907  	=> mem
   908  (Store {t1} p1 (Load <t2> p2 oldmem) mem:(Store {t3} p3 _ (Store {t4} p4 _ (Store {t5} p5 _ oldmem))))
   909  	&& isSamePtr(p1, p2)
   910  	&& t2.Size() == t1.Size()
   911  	&& disjoint(p1, t1.Size(), p3, t3.Size())
   912  	&& disjoint(p1, t1.Size(), p4, t4.Size())
   913  	&& disjoint(p1, t1.Size(), p5, t5.Size())
   914  	=> mem
   915  
   916  // Don't Store zeros to cleared variables.
   917  (Store {t} (OffPtr [o] p1) x mem:(Zero [n] p2 _))
   918  	&& isConstZero(x)
   919  	&& o >= 0 && t.Size() + o <= n && isSamePtr(p1, p2)
   920  	=> mem
   921  (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Zero [n] p3 _)))
   922  	&& isConstZero(x)
   923  	&& o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p3)
   924  	&& disjoint(op, t1.Size(), p2, t2.Size())
   925  	=> mem
   926  (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Zero [n] p4 _))))
   927  	&& isConstZero(x)
   928  	&& o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p4)
   929  	&& disjoint(op, t1.Size(), p2, t2.Size())
   930  	&& disjoint(op, t1.Size(), p3, t3.Size())
   931  	=> mem
   932  (Store {t1} op:(OffPtr [o1] p1) x mem:(Store {t2} p2 _ (Store {t3} p3 _ (Store {t4} p4 _ (Zero [n] p5 _)))))
   933  	&& isConstZero(x)
   934  	&& o1 >= 0 && t1.Size() + o1 <= n && isSamePtr(p1, p5)
   935  	&& disjoint(op, t1.Size(), p2, t2.Size())
   936  	&& disjoint(op, t1.Size(), p3, t3.Size())
   937  	&& disjoint(op, t1.Size(), p4, t4.Size())
   938  	=> mem
   939  
   940  // Collapse OffPtr
   941  (OffPtr (OffPtr p [y]) [x]) => (OffPtr p [x+y])
   942  (OffPtr p [0]) && v.Type.Compare(p.Type) == types.CMPeq => p
   943  
   944  // indexing operations
   945  // Note: bounds check has already been done
   946  (PtrIndex <t> ptr idx) && config.PtrSize == 4 && is32Bit(t.Elem().Size()) => (AddPtr ptr (Mul32 <typ.Int> idx (Const32 <typ.Int> [int32(t.Elem().Size())])))
   947  (PtrIndex <t> ptr idx) && config.PtrSize == 8 => (AddPtr ptr (Mul64 <typ.Int> idx (Const64 <typ.Int> [t.Elem().Size()])))
   948  
   949  // struct operations
   950  (StructSelect [i] x:(StructMake ___)) => x.Args[i]
   951  (Load <t> _ _) && t.IsStruct() && t.Size() > 0 && CanSSA(t) && !t.IsSIMD() => rewriteStructLoad(v)
   952  (Store _ (StructMake ___) _) => rewriteStructStore(v)
   953  
   954  (StructSelect [i] x:(Load <t> ptr mem)) && !CanSSA(t) =>
   955    @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
   956  
   957  // Putting struct{*byte} and similar into direct interfaces.
   958  (IMake _typ (StructMake ___)) => imakeOfStructMake(v)
   959  (StructSelect (IData x)) && v.Type.Size() > 0 => (IData x)
   960  (StructSelect (IData x)) && v.Type.Size() == 0 => (Empty)
   961  
   962  // un-SSAable values use mem->mem copies
   963  (Store {t} dst (Load src mem) mem) && !CanSSA(t) =>
   964  	(Move {t} [t.Size()] dst src mem)
   965  (Store {t} dst (Load src mem) (VarDef {x} mem)) && !CanSSA(t) =>
   966  	(Move {t} [t.Size()] dst src (VarDef {x} mem))
   967  
   968  // array ops
   969  (ArraySelect (ArrayMake1 x)) => x
   970  
   971  (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && CanSSA(t) =>
   972    (ArrayMake1 (Load <t.Elem()> ptr mem))
   973  
   974  (Store dst (ArrayMake1 e) mem) => (Store {e.Type} dst e mem)
   975  
   976  // Putting [1]*byte and similar into direct interfaces.
   977  (IMake _typ (ArrayMake1 val)) => (IMake _typ val)
   978  (ArraySelect [0] (IData x)) => (IData x)
   979  
   980  // zero-sized values.
   981  (Load <t> _ _) && t.Size() == 0 => (Empty)
   982  (Store _ (Empty) mem) => mem
   983  
   984  // string ops
   985  // Decomposing StringMake and lowering of StringPtr and StringLen
   986  // happens in a later pass, dec, so that these operations are available
   987  // to other passes for optimizations.
   988  (StringPtr (StringMake (Addr <t> {s} base) _)) => (Addr <t> {s} base)
   989  (StringLen (StringMake _ (Const64 <t> [c]))) => (Const64 <t> [c])
   990  (ConstString {str}) && config.PtrSize == 4 && str == "" =>
   991    (StringMake (ConstNil) (Const32 <typ.Int> [0]))
   992  (ConstString {str}) && config.PtrSize == 8 && str == "" =>
   993    (StringMake (ConstNil) (Const64 <typ.Int> [0]))
   994  (ConstString {str}) && config.PtrSize == 4 && str != "" =>
   995    (StringMake
   996      (Addr <typ.BytePtr> {fe.StringData(str)}
   997        (SB))
   998      (Const32 <typ.Int> [int32(len(str))]))
   999  (ConstString {str}) && config.PtrSize == 8 && str != "" =>
  1000    (StringMake
  1001      (Addr <typ.BytePtr> {fe.StringData(str)}
  1002        (SB))
  1003      (Const64 <typ.Int> [int64(len(str))]))
  1004  
  1005  // slice ops
  1006  // Only a few slice rules are provided here.  See dec.rules for
  1007  // a more comprehensive set.
  1008  (SliceLen (SliceMake _ (Const64 <t> [c]) _)) => (Const64 <t> [c])
  1009  (SliceCap (SliceMake _ _ (Const64 <t> [c]))) => (Const64 <t> [c])
  1010  (SliceLen (SliceMake _ (Const32 <t> [c]) _)) => (Const32 <t> [c])
  1011  (SliceCap (SliceMake _ _ (Const32 <t> [c]))) => (Const32 <t> [c])
  1012  (SlicePtr (SliceMake (SlicePtr x) _ _)) => (SlicePtr x)
  1013  (SliceLen (SliceMake _ (SliceLen x) _)) => (SliceLen x)
  1014  (SliceCap (SliceMake _ _ (SliceCap x))) => (SliceCap x)
  1015  (SliceCap (SliceMake _ _ (SliceLen x))) => (SliceLen x)
  1016  (ConstSlice) && config.PtrSize == 4 =>
  1017    (SliceMake
  1018      (ConstNil <v.Type.Elem().PtrTo()>)
  1019      (Const32 <typ.Int> [0])
  1020      (Const32 <typ.Int> [0]))
  1021  (ConstSlice) && config.PtrSize == 8 =>
  1022    (SliceMake
  1023      (ConstNil <v.Type.Elem().PtrTo()>)
  1024      (Const64 <typ.Int> [0])
  1025      (Const64 <typ.Int> [0]))
  1026  (SliceLen (Phi (SliceMake _ x _) (SliceMake _ x _))) => x
  1027  (SliceCap (Phi (SliceMake _ _ x) (SliceMake _ _ x))) => x
  1028  
  1029  // Special rule to help constant slicing; len > 0 implies cap > 0 implies Slicemask is all 1
  1030  (SliceMake (AddPtr <t> x (And64 y (Slicemask _))) w:(Const64 [c]) z) && c > 0 => (SliceMake (AddPtr <t> x y) w z)
  1031  (SliceMake (AddPtr <t> x (And32 y (Slicemask _))) w:(Const32 [c]) z) && c > 0 => (SliceMake (AddPtr <t> x y) w z)
  1032  
  1033  // interface ops
  1034  (ConstInterface) =>
  1035    (IMake
  1036      (ConstNil <typ.Uintptr>)
  1037      (ConstNil <typ.BytePtr>))
  1038  
  1039  (NilCheck ptr:(GetG mem) mem) => ptr
  1040  
  1041  (If (Not cond) yes no) => (If cond no yes)
  1042  (If (ConstBool [c]) yes no) && c => (First yes no)
  1043  (If (ConstBool [c]) yes no) && !c => (First no yes)
  1044  
  1045  (Phi <t> nx:(Not x) ny:(Not y)) && nx.Uses == 1 && ny.Uses == 1 => (Not (Phi <t> x y))
  1046  
  1047  // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
  1048  (Convert (Add(64|32) (Convert ptr mem) off) mem) => (AddPtr ptr off)
  1049  (Convert (Convert ptr mem) mem) => ptr
  1050  // Note: it is important that the target rewrite is ptr+(off1+off2), not (ptr+off1)+off2.
  1051  // We must ensure that no intermediate computations are invalid pointers.
  1052  (Convert a:(Add(64|32) (Add(64|32) (Convert ptr mem) off1) off2) mem) => (AddPtr ptr (Add(64|32) <a.Type> off1 off2))
  1053  
  1054  // Simplification of divisions.
  1055  // Only trivial, easily analyzed (by prove) rewrites here.
  1056  // Strength reduction of div to mul is delayed to divmod.rules.
  1057  
  1058  // Signed divide by a negative constant.  Rewrite to divide by a positive constant.
  1059  (Div8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  => (Neg8  (Div8  <t> n (Const8  <t> [-c])))
  1060  (Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 => (Neg16 (Div16 <t> n (Const16 <t> [-c])))
  1061  (Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 => (Neg32 (Div32 <t> n (Const32 <t> [-c])))
  1062  (Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 => (Neg64 (Div64 <t> n (Const64 <t> [-c])))
  1063  
  1064  // Dividing by the most-negative number.  Result is always 0 except
  1065  // if the input is also the most-negative number.
  1066  // We can detect that using the sign bit of x & -x.
  1067  (Div64 x (Const64 [-1<<63])) && isNonNegative(x) => (Const64 [0])
  1068  (Div8  <t> x (Const8  [-1<<7 ])) => (Rsh8Ux64  (And8  <t> x (Neg8  <t> x)) (Const64 <typ.UInt64> [7 ]))
  1069  (Div16 <t> x (Const16 [-1<<15])) => (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <typ.UInt64> [15]))
  1070  (Div32 <t> x (Const32 [-1<<31])) => (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <typ.UInt64> [31]))
  1071  (Div64 <t> x (Const64 [-1<<63])) => (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <typ.UInt64> [63]))
  1072  
  1073  // Unsigned divide by power of 2.  Strength reduce to a shift.
  1074  (Div8u  n (Const8  [c])) && isPowerOfTwo(uint8(c)) => (Rsh8Ux64  n (Const64 <typ.UInt64> [log8u(uint8(c))]))
  1075  (Div16u n (Const16 [c])) && isPowerOfTwo(uint16(c)) => (Rsh16Ux64 n (Const64 <typ.UInt64> [log16u(uint16(c))]))
  1076  (Div32u n (Const32 [c])) && isPowerOfTwo(uint32(c)) => (Rsh32Ux64 n (Const64 <typ.UInt64> [log32u(uint32(c))]))
  1077  (Div64u n (Const64 [c])) && isPowerOfTwo(uint64(c)) => (Rsh64Ux64 n (Const64 <typ.UInt64> [log64u(uint64(c))]))
  1078  
  1079  // Strength reduce multiplication by a power of two to a shift.
  1080  // Excluded from early opt so that prove can recognize mod
  1081  // by the x - (x/d)*d pattern.
  1082  // (Runs during "middle opt" and "late opt".)
  1083  (Mul8  <t> x (Const8  [c])) && isPowerOfTwo(c) && v.Block.Func.pass.name != "opt" =>
  1084    (Lsh8x64  <t> x (Const64 <typ.UInt64> [log8(c)]))
  1085  (Mul16 <t> x (Const16 [c])) && isPowerOfTwo(c) && v.Block.Func.pass.name != "opt" =>
  1086    (Lsh16x64 <t> x (Const64 <typ.UInt64> [log16(c)]))
  1087  (Mul32 <t> x (Const32 [c])) && isPowerOfTwo(c) && v.Block.Func.pass.name != "opt" =>
  1088    (Lsh32x64 <t> x (Const64 <typ.UInt64> [log32(c)]))
  1089  (Mul64 <t> x (Const64 [c])) && isPowerOfTwo(c) && v.Block.Func.pass.name != "opt" =>
  1090    (Lsh64x64 <t> x (Const64 <typ.UInt64> [log64(c)]))
  1091  (Mul8  <t> x (Const8  [c])) && t.IsSigned() && isPowerOfTwo(-c) && v.Block.Func.pass.name != "opt" =>
  1092    (Neg8  (Lsh8x64  <t> x (Const64 <typ.UInt64> [log8(-c)])))
  1093  (Mul16 <t> x (Const16 [c])) && t.IsSigned() && isPowerOfTwo(-c) && v.Block.Func.pass.name != "opt" =>
  1094    (Neg16 (Lsh16x64 <t> x (Const64 <typ.UInt64> [log16(-c)])))
  1095  (Mul32 <t> x (Const32 [c])) && t.IsSigned() && isPowerOfTwo(-c) && v.Block.Func.pass.name != "opt" =>
  1096    (Neg32 (Lsh32x64 <t> x (Const64 <typ.UInt64> [log32(-c)])))
  1097  (Mul64 <t> x (Const64 [c])) && t.IsSigned() && isPowerOfTwo(-c) && v.Block.Func.pass.name != "opt" =>
  1098    (Neg64 (Lsh64x64 <t> x (Const64 <typ.UInt64> [log64(-c)])))
  1099  
  1100  // Strength reduction of mod to div.
  1101  // Strength reduction of div to mul is delayed to divmod.rules.
  1102  
  1103  // Unsigned mod by power of 2 constant.
  1104  (Mod8u  <t> n (Const8  [c])) && isPowerOfTwo(uint8(c)) => (And8  n (Const8  <t> [c-1]))
  1105  (Mod16u <t> n (Const16 [c])) && isPowerOfTwo(uint16(c)) => (And16 n (Const16 <t> [c-1]))
  1106  (Mod32u <t> n (Const32 [c])) && isPowerOfTwo(uint32(c)) => (And32 n (Const32 <t> [c-1]))
  1107  (Mod64u <t> n (Const64 [c])) && isPowerOfTwo(uint64(c)) => (And64 n (Const64 <t> [c-1]))
  1108  
  1109  // Signed non-negative mod by power of 2 constant.
  1110  // TODO: Replace ModN with ModNu in prove.
  1111  (Mod8  <t> n (Const8  [c])) && isNonNegative(n) && isPowerOfTwo(c) => (And8  n (Const8  <t> [c-1]))
  1112  (Mod16 <t> n (Const16 [c])) && isNonNegative(n) && isPowerOfTwo(c) => (And16 n (Const16 <t> [c-1]))
  1113  (Mod32 <t> n (Const32 [c])) && isNonNegative(n) && isPowerOfTwo(c) => (And32 n (Const32 <t> [c-1]))
  1114  (Mod64 <t> n (Const64 [c])) && isNonNegative(n) && isPowerOfTwo(c) => (And64 n (Const64 <t> [c-1]))
  1115  (Mod64 n (Const64 [-1<<63])) && isNonNegative(n)                   => n
  1116  
  1117  // Signed mod by negative constant.
  1118  (Mod8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  => (Mod8  <t> n (Const8  <t> [-c]))
  1119  (Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 => (Mod16 <t> n (Const16 <t> [-c]))
  1120  (Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 => (Mod32 <t> n (Const32 <t> [-c]))
  1121  (Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 => (Mod64 <t> n (Const64 <t> [-c]))
  1122  
  1123  // All other mods by constants, do A%B = A-(A/B*B).
  1124  // This implements % with two * and a bunch of ancillary ops.
  1125  // One of the * is free if the user's code also computes A/B.
  1126  (Mod8   <t> x (Const8  [c])) && x.Op != OpConst8  && (c > 0 || c == -1<<7)
  1127    => (Sub8  x (Mul8  <t> (Div8   <t> x (Const8  <t> [c])) (Const8  <t> [c])))
  1128  (Mod16  <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15)
  1129    => (Sub16 x (Mul16 <t> (Div16  <t> x (Const16 <t> [c])) (Const16 <t> [c])))
  1130  (Mod32  <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31)
  1131    => (Sub32 x (Mul32 <t> (Div32  <t> x (Const32 <t> [c])) (Const32 <t> [c])))
  1132  (Mod64  <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63)
  1133    => (Sub64 x (Mul64 <t> (Div64  <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1134  (Mod8u  <t> x (Const8  [c])) && x.Op != OpConst8  && c != 0
  1135    => (Sub8  x (Mul8  <t> (Div8u  <t> x (Const8  <t> [c])) (Const8  <t> [c])))
  1136  (Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c != 0
  1137    => (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c])))
  1138  (Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c != 0
  1139    => (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c])))
  1140  (Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c != 0
  1141    => (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1142  
  1143  // Set up for mod->mul+rot optimization in genericlateopt.rules.
  1144  // For architectures without rotates on less than 32-bits, promote to 32-bit.
  1145  // TODO: Also != 0 case?
  1146  (Eq8 (Mod8u x (Const8  [c])) (Const8 [0])) && x.Op != OpConst8 && udivisibleOK8(c) && !hasSmallRotate(config) =>
  1147  	(Eq32 (Mod32u <typ.UInt32> (ZeroExt8to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(uint8(c))])) (Const32 <typ.UInt32> [0]))
  1148  (Eq16 (Mod16u x (Const16  [c])) (Const16 [0])) && x.Op != OpConst16 && udivisibleOK16(c) && !hasSmallRotate(config) =>
  1149  	(Eq32 (Mod32u <typ.UInt32> (ZeroExt16to32 <typ.UInt32> x) (Const32 <typ.UInt32> [int32(uint16(c))])) (Const32 <typ.UInt32> [0]))
  1150  (Eq8 (Mod8 x (Const8  [c])) (Const8 [0])) && x.Op != OpConst8 && sdivisibleOK8(c) && !hasSmallRotate(config) =>
  1151  	(Eq32 (Mod32 <typ.Int32> (SignExt8to32 <typ.Int32> x) (Const32 <typ.Int32> [int32(c)])) (Const32 <typ.Int32> [0]))
  1152  (Eq16 (Mod16 x (Const16  [c])) (Const16 [0])) && x.Op != OpConst16 && sdivisibleOK16(c) && !hasSmallRotate(config) =>
  1153  	(Eq32 (Mod32 <typ.Int32> (SignExt16to32 <typ.Int32> x) (Const32 <typ.Int32> [int32(c)])) (Const32 <typ.Int32> [0]))
  1154  
  1155  (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)
  1156  (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)
  1157  
  1158  // Optimize bitsets
  1159  (Eq(8|16|32|64) (And(8|16|32|64) <t> x (Const(8|16|32|64) <t> [y])) (Const(8|16|32|64) <t> [y])) && oneBit(y)
  1160    => (Neq(8|16|32|64) (And(8|16|32|64) <t> x (Const(8|16|32|64) <t> [y])) (Const(8|16|32|64) <t> [0]))
  1161  (Neq(8|16|32|64) (And(8|16|32|64) <t> x (Const(8|16|32|64) <t> [y])) (Const(8|16|32|64) <t> [y])) && oneBit(y)
  1162    => (Eq(8|16|32|64) (And(8|16|32|64) <t> x (Const(8|16|32|64) <t> [y])) (Const(8|16|32|64) <t> [0]))
  1163  
  1164  // Mark newly generated bounded shifts as bounded, for opt passes after prove.
  1165  (Lsh64x(8|16|32|64)  [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 64 => (Lsh64x(8|16|32|64)  [true] x con)
  1166  (Rsh64x(8|16|32|64)  [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 64 => (Rsh64x(8|16|32|64)  [true] x con)
  1167  (Rsh64Ux(8|16|32|64) [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 64 => (Rsh64Ux(8|16|32|64) [true] x con)
  1168  (Lsh32x(8|16|32|64)  [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 32 => (Lsh32x(8|16|32|64)  [true] x con)
  1169  (Rsh32x(8|16|32|64)  [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 32 => (Rsh32x(8|16|32|64)  [true] x con)
  1170  (Rsh32Ux(8|16|32|64) [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 32 => (Rsh32Ux(8|16|32|64) [true] x con)
  1171  (Lsh16x(8|16|32|64)  [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 16 => (Lsh16x(8|16|32|64)  [true] x con)
  1172  (Rsh16x(8|16|32|64)  [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 16 => (Rsh16x(8|16|32|64)  [true] x con)
  1173  (Rsh16Ux(8|16|32|64) [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 16 => (Rsh16Ux(8|16|32|64) [true] x con)
  1174  (Lsh8x(8|16|32|64)   [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 8  => (Lsh8x(8|16|32|64)   [true] x con)
  1175  (Rsh8x(8|16|32|64)   [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 8  => (Rsh8x(8|16|32|64)   [true] x con)
  1176  (Rsh8Ux(8|16|32|64)  [false] x con:(Const(8|16|32|64) [c])) && 0 < c && c < 8  => (Rsh8Ux(8|16|32|64)  [true] x con)
  1177  
  1178  // Reassociate expressions involving
  1179  // constants such that constants come first,
  1180  // exposing obvious constant-folding opportunities.
  1181  // Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C
  1182  // is constant, which pushes constants to the outside
  1183  // of the expression. At that point, any constant-folding
  1184  // opportunities should be obvious.
  1185  // Note: don't include AddPtr here! In order to maintain the
  1186  // invariant that pointers must stay within the pointed-to object,
  1187  // we can't pull part of a pointer computation above the AddPtr.
  1188  // See issue 37881.
  1189  // Note: we don't need to handle any (x-C) cases because we already rewrite
  1190  // (x-C) to (x+(-C)).
  1191  
  1192  // x + (C + z) -> C + (x + z)
  1193  (Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Add64 <t> z x))
  1194  (Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Add32 <t> z x))
  1195  (Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Add16 <t> z x))
  1196  (Add8  (Add8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Add8  i (Add8  <t> z x))
  1197  
  1198  // x + (C - z) -> C + (x - z)
  1199  (Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Sub64 <t> x z))
  1200  (Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Sub32 <t> x z))
  1201  (Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Sub16 <t> x z))
  1202  (Add8  (Sub8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Add8  i (Sub8  <t> x z))
  1203  
  1204  // x - (C - z) -> x + (z - C) -> (x + z) - C
  1205  (Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 (Add64 <t> x z) i)
  1206  (Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 (Add32 <t> x z) i)
  1207  (Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 (Add16 <t> x z) i)
  1208  (Sub8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Sub8  (Add8  <t> x z) i)
  1209  
  1210  // x - (z + C) -> x + (-z - C) -> (x - z) - C
  1211  (Sub64 x (Add64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 (Sub64 <t> x z) i)
  1212  (Sub32 x (Add32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 (Sub32 <t> x z) i)
  1213  (Sub16 x (Add16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 (Sub16 <t> x z) i)
  1214  (Sub8  x (Add8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Sub8 (Sub8  <t> x z) i)
  1215  
  1216  // (C - z) - x -> C - (z + x)
  1217  (Sub64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Sub64 i (Add64 <t> z x))
  1218  (Sub32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Sub32 i (Add32 <t> z x))
  1219  (Sub16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Sub16 i (Add16 <t> z x))
  1220  (Sub8  (Sub8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Sub8  i (Add8  <t> z x))
  1221  
  1222  // (z + C) -x -> C + (z - x)
  1223  (Sub64 (Add64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Add64 i (Sub64 <t> z x))
  1224  (Sub32 (Add32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Add32 i (Sub32 <t> z x))
  1225  (Sub16 (Add16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Add16 i (Sub16 <t> z x))
  1226  (Sub8  (Add8  z i:(Const8  <t>)) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Add8  i (Sub8  <t> z x))
  1227  
  1228  // x & (C & z) -> C & (x & z)
  1229  (And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (And64 i (And64 <t> z x))
  1230  (And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (And32 i (And32 <t> z x))
  1231  (And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (And16 i (And16 <t> z x))
  1232  (And8  (And8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (And8  i (And8  <t> z x))
  1233  
  1234  // x | (C | z) -> C | (x | z)
  1235  (Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Or64 i (Or64 <t> z x))
  1236  (Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Or32 i (Or32 <t> z x))
  1237  (Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Or16 i (Or16 <t> z x))
  1238  (Or8  (Or8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Or8  i (Or8  <t> z x))
  1239  
  1240  // x ^ (C ^ z) -> C ^ (x ^ z)
  1241  (Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Xor64 i (Xor64 <t> z x))
  1242  (Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Xor32 i (Xor32 <t> z x))
  1243  (Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Xor16 i (Xor16 <t> z x))
  1244  (Xor8  (Xor8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Xor8  i (Xor8  <t> z x))
  1245  
  1246  // x * (D * z) = D * (x * z)
  1247  (Mul64 (Mul64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) => (Mul64 i (Mul64 <t> x z))
  1248  (Mul32 (Mul32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) => (Mul32 i (Mul32 <t> x z))
  1249  (Mul16 (Mul16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) => (Mul16 i (Mul16 <t> x z))
  1250  (Mul8  (Mul8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  => (Mul8  i (Mul8  <t> x z))
  1251  
  1252  // C + (D + x) -> (C + D) + x
  1253  (Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Add64 (Const64 <t> [c+d]) x)
  1254  (Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Add32 (Const32 <t> [c+d]) x)
  1255  (Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Add16 (Const16 <t> [c+d]) x)
  1256  (Add8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Add8  (Const8  <t> [c+d]) x)
  1257  
  1258  // C + (D - x) -> (C + D) - x
  1259  (Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) => (Sub64 (Const64 <t> [c+d]) x)
  1260  (Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) => (Sub32 (Const32 <t> [c+d]) x)
  1261  (Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) => (Sub16 (Const16 <t> [c+d]) x)
  1262  (Add8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) => (Sub8  (Const8  <t> [c+d]) x)
  1263  
  1264  // C - (D - x) -> (C - D) + x
  1265  (Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) => (Add64 (Const64 <t> [c-d]) x)
  1266  (Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) => (Add32 (Const32 <t> [c-d]) x)
  1267  (Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) => (Add16 (Const16 <t> [c-d]) x)
  1268  (Sub8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) => (Add8  (Const8  <t> [c-d]) x)
  1269  
  1270  // C - (D + x) -> (C - D) - x
  1271  (Sub64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) => (Sub64 (Const64 <t> [c-d]) x)
  1272  (Sub32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) => (Sub32 (Const32 <t> [c-d]) x)
  1273  (Sub16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) => (Sub16 (Const16 <t> [c-d]) x)
  1274  (Sub8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) => (Sub8  (Const8  <t> [c-d]) x)
  1275  
  1276  // C & (D & x) -> (C & D) & x
  1277  (And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) => (And64 (Const64 <t> [c&d]) x)
  1278  (And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) => (And32 (Const32 <t> [c&d]) x)
  1279  (And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) => (And16 (Const16 <t> [c&d]) x)
  1280  (And8  (Const8  <t> [c]) (And8  (Const8  <t> [d]) x)) => (And8  (Const8  <t> [c&d]) x)
  1281  
  1282  // C | (D | x) -> (C | D) | x
  1283  (Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) => (Or64 (Const64 <t> [c|d]) x)
  1284  (Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) => (Or32 (Const32 <t> [c|d]) x)
  1285  (Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) => (Or16 (Const16 <t> [c|d]) x)
  1286  (Or8  (Const8  <t> [c]) (Or8  (Const8  <t> [d]) x)) => (Or8  (Const8  <t> [c|d]) x)
  1287  
  1288  // C ^ (D ^ x) -> (C ^ D) ^ x
  1289  (Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) => (Xor64 (Const64 <t> [c^d]) x)
  1290  (Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) => (Xor32 (Const32 <t> [c^d]) x)
  1291  (Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) => (Xor16 (Const16 <t> [c^d]) x)
  1292  (Xor8  (Const8  <t> [c]) (Xor8  (Const8  <t> [d]) x)) => (Xor8  (Const8  <t> [c^d]) x)
  1293  
  1294  // C * (D * x) = (C * D) * x
  1295  (Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) => (Mul64 (Const64 <t> [c*d]) x)
  1296  (Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) => (Mul32 (Const32 <t> [c*d]) x)
  1297  (Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) => (Mul16 (Const16 <t> [c*d]) x)
  1298  (Mul8  (Const8  <t> [c]) (Mul8  (Const8  <t> [d]) x)) => (Mul8  (Const8  <t> [c*d]) x)
  1299  
  1300  // floating point optimizations
  1301  (Mul(32|64)F x (Const(32|64)F [1])) => x
  1302  (Mul32F x (Const32F [-1])) => (Neg32F x)
  1303  (Mul64F x (Const64F [-1])) => (Neg64F x)
  1304  (Mul32F x (Const32F [2])) => (Add32F x x)
  1305  (Mul64F x (Const64F [2])) => (Add64F x x)
  1306  
  1307  (Div32F x (Const32F <t> [c])) && reciprocalExact32(c) => (Mul32F x (Const32F <t> [1/c]))
  1308  (Div64F x (Const64F <t> [c])) && reciprocalExact64(c) => (Mul64F x (Const64F <t> [1/c]))
  1309  
  1310  // rewrite single-precision sqrt expression "float32(math.Sqrt(float64(x)))"
  1311  (Cvt64Fto32F sqrt0:(Sqrt (Cvt32Fto64F x))) && sqrt0.Uses==1 => (Sqrt32 x)
  1312  
  1313  (Sqrt (Const64F [c])) && !math.IsNaN(math.Sqrt(c)) => (Const64F [math.Sqrt(c)])
  1314  
  1315  // for rewriting constant folded math/bits ops
  1316  (Select0 (MakeTuple x y)) => x
  1317  (Select1 (MakeTuple x y)) => y
  1318  
  1319  // for rewriting results of some late-expanded rewrites (below)
  1320  (SelectN [n] m:(MakeResult ___)) => m.Args[n]
  1321  
  1322  // TODO(matloob): Try out having non-zeroing mallocs for prointerless
  1323  // memory, and leaving the zeroing here. Then the compiler can remove
  1324  // the zeroing if the user has explicit writes to the whole object.
  1325  
  1326  // for late-expanded calls, recognize newobject and remove zeroing and nilchecks
  1327  (Zero (SelectN [0] call:(StaticLECall ___)) mem:(SelectN [1] call))
  1328  	&& isMalloc(call.Aux)
  1329  	=> mem
  1330  
  1331  (Store (SelectN [0] call:(StaticLECall ___)) x mem:(SelectN [1] call))
  1332  	&& isConstZero(x)
  1333  	&& isMalloc(call.Aux)
  1334  	=> mem
  1335  
  1336  (Store (OffPtr (SelectN [0] call:(StaticLECall ___))) x mem:(SelectN [1] call))
  1337  	&& isConstZero(x)
  1338  	&& isMalloc(call.Aux)
  1339  	=> mem
  1340  
  1341  (NilCheck ptr:(SelectN [0] call:(StaticLECall ___)) _)
  1342  	&& isMalloc(call.Aux)
  1343  	&& warnRule(fe.Debug_checknil(), v, "removed nil check")
  1344  	=> ptr
  1345  
  1346  (NilCheck ptr:(OffPtr (SelectN [0] call:(StaticLECall ___))) _)
  1347  	&& isMalloc(call.Aux)
  1348  	&& warnRule(fe.Debug_checknil(), v, "removed nil check")
  1349  	=> ptr
  1350  
  1351  // Addresses of globals are always non-nil.
  1352  (NilCheck          ptr:(Addr {_} (SB))    _) => ptr
  1353  (NilCheck ptr:(Convert (Addr {_} (SB)) _) _) => ptr
  1354  
  1355  // Addresses of locals are always non-nil.
  1356  (NilCheck ptr:(LocalAddr _ _) _)
  1357  	&& warnRule(fe.Debug_checknil(), v, "removed nil check")
  1358  	=> ptr
  1359  
  1360  // .dict args are always non-nil.
  1361  (NilCheck ptr:(Arg {sym}) _) && isDictArgSym(sym) => ptr
  1362  
  1363  // Nil checks of nil checks are redundant.
  1364  // See comment at the end of https://go-review.googlesource.com/c/go/+/537775.
  1365  (NilCheck ptr:(NilCheck _ _) _ ) => ptr
  1366  
  1367  // for late-expanded calls, recognize memequal applied to a single constant byte
  1368  // Support is limited by [1-8] byte sizes
  1369  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [1]) mem)
  1370    && isSameCall(callAux, "runtime.memequal")
  1371    && symIsRO(scon)
  1372    => (MakeResult (Eq8 (Load <typ.Int8> sptr mem) (Const8 <typ.Int8> [int8(read8(scon,0))])) mem)
  1373  
  1374  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [1]) mem)
  1375    && isSameCall(callAux, "runtime.memequal")
  1376    && symIsRO(scon)
  1377    => (MakeResult (Eq8 (Load <typ.Int8> sptr mem) (Const8 <typ.Int8> [int8(read8(scon,0))])) mem)
  1378  
  1379  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [2]) mem)
  1380    && isSameCall(callAux, "runtime.memequal")
  1381    && symIsRO(scon)
  1382    && canLoadUnaligned(config)
  1383    => (MakeResult (Eq16 (Load <typ.Int16> sptr mem) (Const16 <typ.Int16> [int16(read16(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  1384  
  1385  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [2]) mem)
  1386    && isSameCall(callAux, "runtime.memequal")
  1387    && symIsRO(scon)
  1388    && canLoadUnaligned(config)
  1389    => (MakeResult (Eq16 (Load <typ.Int16> sptr mem) (Const16 <typ.Int16> [int16(read16(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  1390  
  1391  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [4]) mem)
  1392    && isSameCall(callAux, "runtime.memequal")
  1393    && symIsRO(scon)
  1394    && canLoadUnaligned(config)
  1395    => (MakeResult (Eq32 (Load <typ.Int32> sptr mem) (Const32 <typ.Int32> [int32(read32(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  1396  
  1397  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [4]) mem)
  1398    && isSameCall(callAux, "runtime.memequal")
  1399    && symIsRO(scon)
  1400    && canLoadUnaligned(config)
  1401    => (MakeResult (Eq32 (Load <typ.Int32> sptr mem) (Const32 <typ.Int32> [int32(read32(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  1402  
  1403  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [8]) mem)
  1404    && isSameCall(callAux, "runtime.memequal")
  1405    && symIsRO(scon)
  1406    && canLoadUnaligned(config) && config.PtrSize == 8
  1407    => (MakeResult (Eq64 (Load <typ.Int64> sptr mem) (Const64 <typ.Int64> [int64(read64(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  1408  
  1409  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [8]) mem)
  1410    && isSameCall(callAux, "runtime.memequal")
  1411    && symIsRO(scon)
  1412    && canLoadUnaligned(config) && config.PtrSize == 8
  1413    => (MakeResult (Eq64 (Load <typ.Int64> sptr mem) (Const64 <typ.Int64> [int64(read64(scon,0,config.ctxt.Arch.ByteOrder))])) mem)
  1414  
  1415  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [3]) mem)
  1416    && isSameCall(callAux, "runtime.memequal")
  1417    && symIsRO(scon)
  1418    && canLoadUnaligned(config) =>
  1419    (MakeResult
  1420      (Eq32
  1421        (Or32 <typ.Int32>
  1422          (ZeroExt16to32 <typ.Int32> (Load <typ.Int16> sptr mem))
  1423          (Lsh32x32 <typ.Int32>
  1424            (ZeroExt8to32 <typ.Int32> (Load <typ.Int8> (OffPtr <typ.BytePtr> [2] sptr) mem))
  1425            (Const32 <typ.Int32> [16])))
  1426        (Const32 <typ.Int32> [int32(uint32(read16(scon,0,config.ctxt.Arch.ByteOrder))|(uint32(read8(scon,2))<<16))]))
  1427      mem)
  1428  
  1429  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [3]) mem)
  1430    && isSameCall(callAux, "runtime.memequal")
  1431    && symIsRO(scon)
  1432    && canLoadUnaligned(config) =>
  1433    (MakeResult
  1434      (Eq32
  1435        (Or32 <typ.Int32>
  1436          (ZeroExt16to32 <typ.Int32> (Load <typ.Int16> sptr mem))
  1437          (Lsh32x32 <typ.Int32>
  1438            (ZeroExt8to32 <typ.Int32> (Load <typ.Int8> (OffPtr <typ.BytePtr> [2] sptr) mem))
  1439            (Const32 <typ.Int32> [16])))
  1440        (Const32 <typ.Int32> [int32(uint32(read16(scon,0,config.ctxt.Arch.ByteOrder))|(uint32(read8(scon,2))<<16))]))
  1441      mem)
  1442  
  1443  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [5]) mem)
  1444    && isSameCall(callAux, "runtime.memequal")
  1445    && symIsRO(scon)
  1446    && canLoadUnaligned(config) && config.PtrSize == 8 =>
  1447    (MakeResult
  1448      (Eq64
  1449        (Or64 <typ.Int64>
  1450          (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
  1451          (Lsh64x64 <typ.Int64>
  1452            (ZeroExt8to64 <typ.Int64> (Load <typ.Int8> (OffPtr <typ.BytePtr> [4] sptr) mem))
  1453            (Const64 <typ.Int64> [32])))
  1454        (Const64 <typ.Int64> [int64(uint64(read32(scon,0,config.ctxt.Arch.ByteOrder))|(uint64(read8(scon,4))<<32))]))
  1455      mem)
  1456  
  1457  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [5]) mem)
  1458    && isSameCall(callAux, "runtime.memequal")
  1459    && symIsRO(scon)
  1460    && canLoadUnaligned(config) && config.PtrSize == 8 =>
  1461    (MakeResult
  1462      (Eq64
  1463        (Or64 <typ.Int64>
  1464          (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
  1465          (Lsh64x64 <typ.Int64>
  1466            (ZeroExt8to64 <typ.Int64> (Load <typ.Int8> (OffPtr <typ.BytePtr> [4] sptr) mem))
  1467            (Const64 <typ.Int64> [32])))
  1468        (Const64 <typ.Int64> [int64(uint64(read32(scon,0,config.ctxt.Arch.ByteOrder))|(uint64(read8(scon,4))<<32))]))
  1469      mem)
  1470  
  1471  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [6]) mem)
  1472    && isSameCall(callAux, "runtime.memequal")
  1473    && symIsRO(scon)
  1474    && canLoadUnaligned(config) && config.PtrSize == 8 =>
  1475    (MakeResult
  1476      (Eq64
  1477        (Or64 <typ.Int64>
  1478          (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
  1479          (Lsh64x64 <typ.Int64>
  1480            (ZeroExt16to64 <typ.Int64> (Load <typ.Int16> (OffPtr <typ.BytePtr> [4] sptr) mem))
  1481            (Const64 <typ.Int64> [32])))
  1482        (Const64 <typ.Int64> [int64(uint64(read32(scon,0,config.ctxt.Arch.ByteOrder))|(uint64(read16(scon,4,config.ctxt.Arch.ByteOrder))<<32))]))
  1483      mem)
  1484  
  1485  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [6]) mem)
  1486    && isSameCall(callAux, "runtime.memequal")
  1487    && symIsRO(scon)
  1488    && canLoadUnaligned(config) && config.PtrSize == 8 =>
  1489    (MakeResult
  1490      (Eq64
  1491        (Or64 <typ.Int64>
  1492          (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
  1493          (Lsh64x64 <typ.Int64>
  1494            (ZeroExt16to64 <typ.Int64> (Load <typ.Int16> (OffPtr <typ.BytePtr> [4] sptr) mem))
  1495            (Const64 <typ.Int64> [32])))
  1496        (Const64 <typ.Int64> [int64(uint64(read32(scon,0,config.ctxt.Arch.ByteOrder))|(uint64(read16(scon,4,config.ctxt.Arch.ByteOrder))<<32))]))
  1497      mem)
  1498  
  1499  (StaticLECall {callAux} sptr (Addr {scon} (SB)) (Const64 [7]) mem)
  1500    && isSameCall(callAux, "runtime.memequal")
  1501    && symIsRO(scon)
  1502    && canLoadUnaligned(config) && config.PtrSize == 8 =>
  1503    (MakeResult
  1504      (Eq64
  1505        (Or64 <typ.Int64>
  1506          (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
  1507          (Lsh64x64 <typ.Int64>
  1508            (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> (OffPtr <typ.BytePtr> [3] sptr) mem))
  1509            (Const64 <typ.Int64> [32])))
  1510        (Const64 <typ.Int64> [int64(uint64(read32(scon,0,config.ctxt.Arch.ByteOrder))|(uint64(read32(scon,3,config.ctxt.Arch.ByteOrder))<<32))]))
  1511      mem)
  1512  
  1513  (StaticLECall {callAux} (Addr {scon} (SB)) sptr (Const64 [7]) mem)
  1514    && isSameCall(callAux, "runtime.memequal")
  1515    && symIsRO(scon)
  1516    && canLoadUnaligned(config) && config.PtrSize == 8 =>
  1517    (MakeResult
  1518      (Eq64
  1519        (Or64 <typ.Int64>
  1520          (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> sptr mem))
  1521          (Lsh64x64 <typ.Int64>
  1522            (ZeroExt32to64 <typ.Int64> (Load <typ.Int32> (OffPtr <typ.BytePtr> [3] sptr) mem))
  1523            (Const64 <typ.Int64> [32])))
  1524        (Const64 <typ.Int64> [int64(uint64(read32(scon,0,config.ctxt.Arch.ByteOrder))|(uint64(read32(scon,3,config.ctxt.Arch.ByteOrder))<<32))]))
  1525      mem)
  1526  
  1527  (StaticLECall {callAux} _ _ (Const64 [0]) mem)
  1528    && isSameCall(callAux, "runtime.memequal")
  1529    => (MakeResult (ConstBool <typ.Bool> [true]) mem)
  1530  
  1531  (Static(Call|LECall) {callAux} p q _ mem)
  1532    && isSameCall(callAux, "runtime.memequal")
  1533    && isSamePtr(p, q)
  1534    => (MakeResult (ConstBool <typ.Bool> [true]) mem)
  1535  
  1536  (MemEq sptr tptr (Const64 [1]) mem)
  1537    => (Eq8 (Load <typ.Int8> sptr mem) (Load <typ.Int8> tptr mem))
  1538  
  1539  (Load <typ.Int8> sptr:(Addr {scon} (SB)) mem)
  1540    && symIsRO(scon)
  1541    => (Const8 <typ.Int8> [int8(read8(scon,0))])
  1542  
  1543  (MemEq sptr tptr (Const64 [2]) mem)
  1544    && canLoadUnaligned(config)
  1545    => (Eq16 (Load <typ.Int16> sptr mem) (Load <typ.Int16> tptr mem))
  1546  
  1547  (Load <typ.Int16> sptr:(Addr {scon} (SB)) mem)
  1548    && symIsRO(scon)
  1549    => (Const16 <typ.Int16> [int16(read16(scon,0,config.ctxt.Arch.ByteOrder))])
  1550  
  1551  (MemEq sptr tptr (Const64 [4]) mem)
  1552    && canLoadUnaligned(config)
  1553    => (Eq32 (Load <typ.Int32> sptr mem) (Load <typ.Int32> tptr mem))
  1554  
  1555  (Load <typ.Int32> sptr:(Addr {scon} (SB)) mem)
  1556    && symIsRO(scon)
  1557    => (Const32 <typ.Int32> [int32(read32(scon,0,config.ctxt.Arch.ByteOrder))])
  1558  
  1559  (MemEq sptr tptr (Const64 [8]) mem)
  1560    && canLoadUnaligned(config) && config.PtrSize == 8
  1561    => (Eq64 (Load <typ.Int64> sptr mem) (Load <typ.Int64> tptr mem))
  1562  
  1563  (Load <typ.Int64> sptr:(Addr {scon} (SB)) mem)
  1564    && symIsRO(scon)
  1565    => (Const64 <typ.Int64> [int64(read64(scon,0,config.ctxt.Arch.ByteOrder))])
  1566  
  1567  (MemEq _ _ (Const64 [0]) _) => (ConstBool <typ.Bool> [true])
  1568  
  1569  (MemEq p q _ _) && isSamePtr(p, q) => (ConstBool <typ.Bool> [true])
  1570  
  1571  // 3-32 bytes memeq (enabled only with support of unaligned loads and 8-byte max word size)
  1572  
  1573  (MemEq p q (Const64 [c]) mem)
  1574    && (c == 3 || c == 5 || c == 9 || c == 17)
  1575    && canLoadUnaligned(config)
  1576    && config.RegSize == 8
  1577    => (AndB (MemEq p q (Const64 <typ.Int64> [c-1]) mem)
  1578             (Eq8 (Load <typ.Int8> (OffPtr <p.Type> p [c-1]) mem) (Load <typ.Int8> (OffPtr <q.Type> q [c-1]) mem)))
  1579  
  1580  (MemEq p q (Const64 [c]) mem)
  1581    && (c == 6 || c == 10 || c == 18)
  1582    && canLoadUnaligned(config)
  1583    && config.RegSize == 8
  1584    => (AndB (MemEq p q (Const64 <typ.Int64> [c-2]) mem)
  1585             (Eq16 (Load <typ.Int16> (OffPtr <p.Type> p [c-2]) mem) (Load <typ.Int16> (OffPtr <q.Type> q [c-2]) mem)))
  1586  
  1587  (MemEq p q (Const64 [c]) mem)
  1588    && (c == 7 || c == 11 || c == 19 || c == 20)
  1589    && canLoadUnaligned(config)
  1590    && config.RegSize == 8
  1591    => (AndB (MemEq p q (Const64 <typ.Int64> [min(c-3,16)]) mem)
  1592             (Eq32 (Load <typ.Int32> (OffPtr <p.Type> p [c-4]) mem) (Load <typ.Int32> (OffPtr <q.Type> q [c-4]) mem)))
  1593  
  1594  (MemEq p q (Const64 [c]) mem)
  1595    && ((c >= 12 && c <= 16) || (c >= 21 && c <= 24))
  1596    && canLoadUnaligned(config)
  1597    && config.RegSize == 8
  1598    => (AndB (MemEq p q (Const64 <typ.Int64> [8 + int64(bool2int(c>16))*8]) mem)
  1599             (Eq64 (Load <typ.Int64> (OffPtr <p.Type> p [c-8]) mem) (Load <typ.Int64> (OffPtr <q.Type> q [c-8]) mem)))
  1600  
  1601  (MemEq p q (Const64 [c]) mem)
  1602    && c >= 25 && c <= 32
  1603    && canLoadUnaligned(config)
  1604    && config.RegSize == 8
  1605    => (AndB (MemEq p q (Const64 <typ.Int64> [16]) mem)
  1606             (MemEq (OffPtr <p.Type> p [16]) (OffPtr <q.Type> q [16]) (Const64 <typ.Int64> [c-16]) mem))
  1607  
  1608  // Turn known-size calls to memclrNoHeapPointers or memclrNoHeapPointersPreemptible into a Zero.
  1609  // When the size is a known constant, inlining to OpZero is safe. Dynamic-size calls remain as
  1610  // runtime calls and go through the chunked preemptible path (memclrNoHeapPointersPreemptible).
  1611  // Note that we are using types.Types[types.TUINT8] instead of sptr.Type.Elem() - see issue 55122 and CL 431496 for more details.
  1612  (SelectN [0] call:(StaticCall {sym} sptr (Const(64|32) [c]) mem))
  1613    && isInlinableMemclr(config, int64(c))
  1614    && (isSameCall(sym, "runtime.memclrNoHeapPointers") || isSameCall(sym, "runtime.memclrNoHeapPointersPreemptible"))
  1615    && call.Uses == 1
  1616    && clobber(call)
  1617    => (Zero {types.Types[types.TUINT8]} [int64(c)] sptr mem)
  1618  
  1619  // Recognise make([]T, 0) and replace it with a pointer to the zerobase
  1620  (StaticLECall {callAux} _ (Const(64|32) [0]) (Const(64|32) [0]) mem)
  1621  	&& isSameCall(callAux, "runtime.makeslice")
  1622  	=> (MakeResult (Addr <v.Type.FieldType(0)> {ir.Syms.Zerobase} (SB)) mem)
  1623  
  1624  // Evaluate constant address comparisons.
  1625  (EqPtr  x x) => (ConstBool [true])
  1626  (NeqPtr x x) => (ConstBool [false])
  1627  (EqPtr  (Addr {x} _) (Addr {y} _)) => (ConstBool [x == y])
  1628  (EqPtr  (Addr {x} _) (OffPtr [o] (Addr {y} _))) => (ConstBool [x == y && o == 0])
  1629  (EqPtr  (OffPtr [o1] (Addr {x} _)) (OffPtr [o2] (Addr {y} _))) => (ConstBool [x == y && o1 == o2])
  1630  (NeqPtr (Addr {x} _) (Addr {y} _)) => (ConstBool [x != y])
  1631  (NeqPtr (Addr {x} _) (OffPtr [o] (Addr {y} _))) => (ConstBool [x != y || o != 0])
  1632  (NeqPtr (OffPtr [o1] (Addr {x} _)) (OffPtr [o2] (Addr {y} _))) => (ConstBool [x != y || o1 != o2])
  1633  (EqPtr  (LocalAddr {x} _ _) (LocalAddr {y} _ _)) => (ConstBool [x == y])
  1634  (EqPtr  (LocalAddr {x} _ _) (OffPtr [o] (LocalAddr {y} _ _))) => (ConstBool [x == y && o == 0])
  1635  (EqPtr  (OffPtr [o1] (LocalAddr {x} _ _)) (OffPtr [o2] (LocalAddr {y} _ _))) => (ConstBool [x == y && o1 == o2])
  1636  (NeqPtr (LocalAddr {x} _ _) (LocalAddr {y} _ _)) => (ConstBool [x != y])
  1637  (NeqPtr (LocalAddr {x} _ _) (OffPtr [o] (LocalAddr {y} _ _))) => (ConstBool [x != y || o != 0])
  1638  (NeqPtr (OffPtr [o1] (LocalAddr {x} _ _)) (OffPtr [o2] (LocalAddr {y} _ _))) => (ConstBool [x != y || o1 != o2])
  1639  (EqPtr  (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) => (ConstBool [o1 == 0])
  1640  (NeqPtr (OffPtr [o1] p1) p2) && isSamePtr(p1, p2) => (ConstBool [o1 != 0])
  1641  (EqPtr  (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) => (ConstBool [o1 == o2])
  1642  (NeqPtr (OffPtr [o1] p1) (OffPtr [o2] p2)) && isSamePtr(p1, p2) => (ConstBool [o1 != o2])
  1643  (EqPtr  (Const(32|64) [c]) (Const(32|64) [d])) => (ConstBool [c == d])
  1644  (NeqPtr (Const(32|64) [c]) (Const(32|64) [d])) => (ConstBool [c != d])
  1645  (EqPtr  (Convert (Addr {x} _) _) (Addr {y} _)) => (ConstBool [x==y])
  1646  (NeqPtr (Convert (Addr {x} _) _) (Addr {y} _)) => (ConstBool [x!=y])
  1647  
  1648  (EqPtr  (LocalAddr _ _) (Addr _)) => (ConstBool [false])
  1649  (EqPtr  (OffPtr (LocalAddr _ _)) (Addr _)) => (ConstBool [false])
  1650  (EqPtr  (LocalAddr _ _) (OffPtr (Addr _))) => (ConstBool [false])
  1651  (EqPtr  (OffPtr (LocalAddr _ _)) (OffPtr (Addr _))) => (ConstBool [false])
  1652  (NeqPtr (LocalAddr _ _) (Addr _)) => (ConstBool [true])
  1653  (NeqPtr (OffPtr (LocalAddr _ _)) (Addr _)) => (ConstBool [true])
  1654  (NeqPtr (LocalAddr _ _) (OffPtr (Addr _))) => (ConstBool [true])
  1655  (NeqPtr (OffPtr (LocalAddr _ _)) (OffPtr (Addr _))) => (ConstBool [true])
  1656  
  1657  // Simplify address comparisons.
  1658  (EqPtr  (AddPtr p1 o1) p2) && isSamePtr(p1, p2) => (Not (IsNonNil o1))
  1659  (NeqPtr (AddPtr p1 o1) p2) && isSamePtr(p1, p2) => (IsNonNil o1)
  1660  (EqPtr  (Const(32|64) [0]) p) => (Not (IsNonNil p))
  1661  (NeqPtr (Const(32|64) [0]) p) => (IsNonNil p)
  1662  (EqPtr  (ConstNil) p) => (Not (IsNonNil p))
  1663  (NeqPtr (ConstNil) p) => (IsNonNil p)
  1664  
  1665  // Evaluate constant user nil checks.
  1666  (IsNonNil (ConstNil)) => (ConstBool [false])
  1667  (IsNonNil (Const(32|64) [c])) => (ConstBool [c != 0])
  1668  (IsNonNil          (Addr _)   ) => (ConstBool [true])
  1669  (IsNonNil (Convert (Addr _) _)) => (ConstBool [true])
  1670  (IsNonNil (LocalAddr _ _)) => (ConstBool [true])
  1671  
  1672  // Inline small or disjoint runtime.memmove calls with constant length.
  1673  // See the comment in op Move in genericOps.go for discussion of the type.
  1674  //
  1675  // Note that we've lost any knowledge of the type and alignment requirements
  1676  // of the source and destination. We only know the size, and that the type
  1677  // contains no pointers.
  1678  // The type of the move is not necessarily v.Args[0].Type().Elem()!
  1679  // See issue 55122 for details.
  1680  //
  1681  // Because expand calls runs after prove, constants useful to this pattern may not appear.
  1682  // Both versions need to exist; the memory and register variants.
  1683  //
  1684  // Match post-expansion calls, memory version.
  1685  (SelectN [0] call:(StaticCall {sym} s1:(Store _ (Const(64|32) [sz]) s2:(Store  _ src s3:(Store {t} _ dst mem)))))
  1686  	&& sz >= 0
  1687  	&& isSameCall(sym, "runtime.memmove")
  1688  	&& s1.Uses == 1 && s2.Uses == 1 && s3.Uses == 1
  1689  	&& isInlinableMemmove(dst, src, int64(sz), config)
  1690  	&& clobber(s1, s2, s3, call)
  1691  	=> (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
  1692  
  1693  // Match post-expansion calls, register version.
  1694  (SelectN [0] call:(StaticCall {sym} dst src (Const(64|32) [sz]) mem))
  1695  	&& sz >= 0
  1696  	&& call.Uses == 1 // this will exclude all calls with results
  1697  	&& isSameCall(sym, "runtime.memmove")
  1698  	&& isInlinableMemmove(dst, src, int64(sz), config)
  1699  	&& clobber(call)
  1700  	=> (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
  1701  
  1702  // Match pre-expansion calls.
  1703  (SelectN [0] call:(StaticLECall {sym} dst src (Const(64|32) [sz]) mem))
  1704  	&& sz >= 0
  1705  	&& call.Uses == 1 // this will exclude all calls with results
  1706  	&& isSameCall(sym, "runtime.memmove")
  1707  	&& isInlinableMemmove(dst, src, int64(sz), config)
  1708  	&& clobber(call)
  1709  	=> (Move {types.Types[types.TUINT8]} [int64(sz)] dst src mem)
  1710  
  1711  // De-virtualize late-expanded interface calls into late-expanded static calls.
  1712  (InterLECall [argsize] {auxCall} (Addr {fn} (SB)) ___) => devirtLECall(v, fn.(*obj.LSym))
  1713  
  1714  // Move and Zero optimizations.
  1715  // Move source and destination may overlap.
  1716  
  1717  // Convert Moves into Zeros when the source is known to be zeros.
  1718  (Move {t} [n] dst1 src mem:(Zero {t} [n] dst2 _)) && isSamePtr(src, dst2)
  1719  	=> (Zero {t} [n] dst1 mem)
  1720  (Move {t} [n] dst1 src mem:(VarDef (Zero {t} [n] dst0 _))) && isSamePtr(src, dst0)
  1721  	=> (Zero {t} [n] dst1 mem)
  1722  (Move {t} [n] dst (Addr {sym} (SB)) mem) && symIsROZero(sym) => (Zero {t} [n] dst mem)
  1723  
  1724  // Don't Store to variables that are about to be overwritten by Move/Zero.
  1725  (Zero {t1} [n] p1 store:(Store {t2} (OffPtr [o2] p2) _ mem))
  1726  	&& isSamePtr(p1, p2) && store.Uses == 1
  1727  	&& n >= o2 + t2.Size()
  1728  	&& clobber(store)
  1729  	=> (Zero {t1} [n] p1 mem)
  1730  (Move {t1} [n] dst1 src1 store:(Store {t2} op:(OffPtr [o2] dst2) _ mem))
  1731  	&& isSamePtr(dst1, dst2) && store.Uses == 1
  1732  	&& n >= o2 + t2.Size()
  1733  	&& disjoint(src1, n, op, t2.Size())
  1734  	&& clobber(store)
  1735  	=> (Move {t1} [n] dst1 src1 mem)
  1736  
  1737  // Don't Move to variables that are immediately completely overwritten.
  1738  (Zero {t} [n] dst1 move:(Move {t} [n] dst2 _ mem))
  1739  	&& move.Uses == 1
  1740  	&& isSamePtr(dst1, dst2)
  1741  	&& clobber(move)
  1742  	=> (Zero {t} [n] dst1 mem)
  1743  (Move {t} [n] dst1 src1 move:(Move {t} [n] dst2 _ mem))
  1744  	&& move.Uses == 1
  1745  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  1746  	&& clobber(move)
  1747  	=> (Move {t} [n] dst1 src1 mem)
  1748  (Zero {t} [n] dst1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
  1749  	&& move.Uses == 1 && vardef.Uses == 1
  1750  	&& isSamePtr(dst1, dst2)
  1751  	&& clobber(move, vardef)
  1752  	=> (Zero {t} [n] dst1 (VarDef {x} mem))
  1753  (Move {t} [n] dst1 src1 vardef:(VarDef {x} move:(Move {t} [n] dst2 _ mem)))
  1754  	&& move.Uses == 1 && vardef.Uses == 1
  1755  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  1756  	&& clobber(move, vardef)
  1757  	=> (Move {t} [n] dst1 src1 (VarDef {x} mem))
  1758  (Store {t1} op1:(OffPtr [o1] p1) d1
  1759  	m2:(Store {t2} op2:(OffPtr [0] p2) d2
  1760  		m3:(Move [n] p3 _ mem)))
  1761  	&& m2.Uses == 1 && m3.Uses == 1
  1762  	&& o1 == t2.Size()
  1763  	&& n == t2.Size() + t1.Size()
  1764  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1765  	&& clobber(m2, m3)
  1766  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
  1767  (Store {t1} op1:(OffPtr [o1] p1) d1
  1768  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  1769  		m3:(Store {t3} op3:(OffPtr [0] p3) d3
  1770  			m4:(Move [n] p4 _ mem))))
  1771  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
  1772  	&& o2 == t3.Size()
  1773  	&& o1-o2 == t2.Size()
  1774  	&& n == t3.Size() + t2.Size() + t1.Size()
  1775  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1776  	&& clobber(m2, m3, m4)
  1777  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
  1778  (Store {t1} op1:(OffPtr [o1] p1) d1
  1779  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  1780  		m3:(Store {t3} op3:(OffPtr [o3] p3) d3
  1781  			m4:(Store {t4} op4:(OffPtr [0] p4) d4
  1782  				m5:(Move [n] p5 _ mem)))))
  1783  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
  1784  	&& o3 == t4.Size()
  1785  	&& o2-o3 == t3.Size()
  1786  	&& o1-o2 == t2.Size()
  1787  	&& n == t4.Size() + t3.Size() + t2.Size() + t1.Size()
  1788  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1789  	&& clobber(m2, m3, m4, m5)
  1790  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
  1791  
  1792  // Don't Zero variables that are immediately completely overwritten
  1793  // before being accessed.
  1794  (Move {t} [n] dst1 src1 zero:(Zero {t} [n] dst2 mem))
  1795  	&& zero.Uses == 1
  1796  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  1797  	&& clobber(zero)
  1798  	=> (Move {t} [n] dst1 src1 mem)
  1799  (Move {t} [n] dst1 src1 vardef:(VarDef {x} zero:(Zero {t} [n] dst2 mem)))
  1800  	&& zero.Uses == 1 && vardef.Uses == 1
  1801  	&& isSamePtr(dst1, dst2) && disjoint(src1, n, dst2, n)
  1802  	&& clobber(zero, vardef)
  1803  	=> (Move {t} [n] dst1 src1 (VarDef {x} mem))
  1804  (Store {t1} op1:(OffPtr [o1] p1) d1
  1805  	m2:(Store {t2} op2:(OffPtr [0] p2) d2
  1806  		m3:(Zero [n] p3 mem)))
  1807  	&& m2.Uses == 1 && m3.Uses == 1
  1808  	&& o1 == t2.Size()
  1809  	&& n == t2.Size() + t1.Size()
  1810  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1811  	&& clobber(m2, m3)
  1812  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 mem))
  1813  (Store {t1} op1:(OffPtr [o1] p1) d1
  1814  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  1815  		m3:(Store {t3} op3:(OffPtr [0] p3) d3
  1816  			m4:(Zero [n] p4 mem))))
  1817  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1
  1818  	&& o2 == t3.Size()
  1819  	&& o1-o2 == t2.Size()
  1820  	&& n == t3.Size() + t2.Size() + t1.Size()
  1821  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1822  	&& clobber(m2, m3, m4)
  1823  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 mem)))
  1824  (Store {t1} op1:(OffPtr [o1] p1) d1
  1825  	m2:(Store {t2} op2:(OffPtr [o2] p2) d2
  1826  		m3:(Store {t3} op3:(OffPtr [o3] p3) d3
  1827  			m4:(Store {t4} op4:(OffPtr [0] p4) d4
  1828  				m5:(Zero [n] p5 mem)))))
  1829  	&& m2.Uses == 1 && m3.Uses == 1 && m4.Uses == 1 && m5.Uses == 1
  1830  	&& o3 == t4.Size()
  1831  	&& o2-o3 == t3.Size()
  1832  	&& o1-o2 == t2.Size()
  1833  	&& n == t4.Size() + t3.Size() + t2.Size() + t1.Size()
  1834  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1835  	&& clobber(m2, m3, m4, m5)
  1836  	=> (Store {t1} op1 d1 (Store {t2} op2 d2 (Store {t3} op3 d3 (Store {t4} op4 d4 mem))))
  1837  
  1838  // Don't Move from memory if the values are likely to already be
  1839  // in registers.
  1840  (Move {t1} [n] dst p1
  1841  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1842  		(Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _)))
  1843  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1844  	&& t2.Alignment() <= t1.Alignment()
  1845  	&& t3.Alignment() <= t1.Alignment()
  1846  	&& registerizable(b, t2)
  1847  	&& registerizable(b, t3)
  1848  	&& o2 == t3.Size()
  1849  	&& n == t2.Size() + t3.Size()
  1850  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1851  		(Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
  1852  (Move {t1} [n] dst p1
  1853  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1854  		(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  1855  			(Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _))))
  1856  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1857  	&& t2.Alignment() <= t1.Alignment()
  1858  	&& t3.Alignment() <= t1.Alignment()
  1859  	&& t4.Alignment() <= t1.Alignment()
  1860  	&& registerizable(b, t2)
  1861  	&& registerizable(b, t3)
  1862  	&& registerizable(b, t4)
  1863  	&& o3 == t4.Size()
  1864  	&& o2-o3 == t3.Size()
  1865  	&& n == t2.Size() + t3.Size() + t4.Size()
  1866  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1867  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1868  			(Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
  1869  (Move {t1} [n] dst p1
  1870  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1871  		(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  1872  			(Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
  1873  				(Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _)))))
  1874  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1875  	&& t2.Alignment() <= t1.Alignment()
  1876  	&& t3.Alignment() <= t1.Alignment()
  1877  	&& t4.Alignment() <= t1.Alignment()
  1878  	&& t5.Alignment() <= t1.Alignment()
  1879  	&& registerizable(b, t2)
  1880  	&& registerizable(b, t3)
  1881  	&& registerizable(b, t4)
  1882  	&& registerizable(b, t5)
  1883  	&& o4 == t5.Size()
  1884  	&& o3-o4 == t4.Size()
  1885  	&& o2-o3 == t3.Size()
  1886  	&& n == t2.Size() + t3.Size() + t4.Size() + t5.Size()
  1887  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1888  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1889  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  1890  				(Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
  1891  
  1892  // Same thing but with VarDef in the middle.
  1893  (Move {t1} [n] dst p1
  1894  	mem:(VarDef
  1895  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1896  			(Store {t3} op3:(OffPtr <tt3> [0] p3) d2 _))))
  1897  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1898  	&& t2.Alignment() <= t1.Alignment()
  1899  	&& t3.Alignment() <= t1.Alignment()
  1900  	&& registerizable(b, t2)
  1901  	&& registerizable(b, t3)
  1902  	&& o2 == t3.Size()
  1903  	&& n == t2.Size() + t3.Size()
  1904  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1905  		(Store {t3} (OffPtr <tt3> [0] dst) d2 mem))
  1906  (Move {t1} [n] dst p1
  1907  	mem:(VarDef
  1908  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1909  			(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  1910  				(Store {t4} op4:(OffPtr <tt4> [0] p4) d3 _)))))
  1911  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1912  	&& t2.Alignment() <= t1.Alignment()
  1913  	&& t3.Alignment() <= t1.Alignment()
  1914  	&& t4.Alignment() <= t1.Alignment()
  1915  	&& registerizable(b, t2)
  1916  	&& registerizable(b, t3)
  1917  	&& registerizable(b, t4)
  1918  	&& o3 == t4.Size()
  1919  	&& o2-o3 == t3.Size()
  1920  	&& n == t2.Size() + t3.Size() + t4.Size()
  1921  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1922  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1923  			(Store {t4} (OffPtr <tt4> [0] dst) d3 mem)))
  1924  (Move {t1} [n] dst p1
  1925  	mem:(VarDef
  1926  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1927  			(Store {t3} op3:(OffPtr <tt3> [o3] p3) d2
  1928  				(Store {t4} op4:(OffPtr <tt4> [o4] p4) d3
  1929  					(Store {t5} op5:(OffPtr <tt5> [0] p5) d4 _))))))
  1930  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1931  	&& t2.Alignment() <= t1.Alignment()
  1932  	&& t3.Alignment() <= t1.Alignment()
  1933  	&& t4.Alignment() <= t1.Alignment()
  1934  	&& t5.Alignment() <= t1.Alignment()
  1935  	&& registerizable(b, t2)
  1936  	&& registerizable(b, t3)
  1937  	&& registerizable(b, t4)
  1938  	&& registerizable(b, t5)
  1939  	&& o4 == t5.Size()
  1940  	&& o3-o4 == t4.Size()
  1941  	&& o2-o3 == t3.Size()
  1942  	&& n == t2.Size() + t3.Size() + t4.Size() + t5.Size()
  1943  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1944  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1945  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  1946  				(Store {t5} (OffPtr <tt5> [0] dst) d4 mem))))
  1947  
  1948  // Prefer to Zero and Store than to Move.
  1949  (Move {t1} [n] dst p1
  1950  	mem:(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  1951  		(Zero {t3} [n] p3 _)))
  1952  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  1953  	&& t2.Alignment() <= t1.Alignment()
  1954  	&& t3.Alignment() <= t1.Alignment()
  1955  	&& registerizable(b, t2)
  1956  	&& n >= o2 + t2.Size()
  1957  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1958  		(Zero {t1} [n] dst mem))
  1959  (Move {t1} [n] dst p1
  1960  	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
  1961  		(Store {t3} (OffPtr <tt3> [o3] p3) d2
  1962  			(Zero {t4} [n] p4 _))))
  1963  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  1964  	&& t2.Alignment() <= t1.Alignment()
  1965  	&& t3.Alignment() <= t1.Alignment()
  1966  	&& t4.Alignment() <= t1.Alignment()
  1967  	&& registerizable(b, t2)
  1968  	&& registerizable(b, t3)
  1969  	&& n >= o2 + t2.Size()
  1970  	&& n >= o3 + t3.Size()
  1971  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1972  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1973  			(Zero {t1} [n] dst mem)))
  1974  (Move {t1} [n] dst p1
  1975  	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
  1976  		(Store {t3} (OffPtr <tt3> [o3] p3) d2
  1977  			(Store {t4} (OffPtr <tt4> [o4] p4) d3
  1978  				(Zero {t5} [n] p5 _)))))
  1979  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  1980  	&& t2.Alignment() <= t1.Alignment()
  1981  	&& t3.Alignment() <= t1.Alignment()
  1982  	&& t4.Alignment() <= t1.Alignment()
  1983  	&& t5.Alignment() <= t1.Alignment()
  1984  	&& registerizable(b, t2)
  1985  	&& registerizable(b, t3)
  1986  	&& registerizable(b, t4)
  1987  	&& n >= o2 + t2.Size()
  1988  	&& n >= o3 + t3.Size()
  1989  	&& n >= o4 + t4.Size()
  1990  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  1991  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  1992  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  1993  				(Zero {t1} [n] dst mem))))
  1994  (Move {t1} [n] dst p1
  1995  	mem:(Store {t2} (OffPtr <tt2> [o2] p2) d1
  1996  		(Store {t3} (OffPtr <tt3> [o3] p3) d2
  1997  			(Store {t4} (OffPtr <tt4> [o4] p4) d3
  1998  				(Store {t5} (OffPtr <tt5> [o5] p5) d4
  1999  					(Zero {t6} [n] p6 _))))))
  2000  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6)
  2001  	&& t2.Alignment() <= t1.Alignment()
  2002  	&& t3.Alignment() <= t1.Alignment()
  2003  	&& t4.Alignment() <= t1.Alignment()
  2004  	&& t5.Alignment() <= t1.Alignment()
  2005  	&& t6.Alignment() <= t1.Alignment()
  2006  	&& registerizable(b, t2)
  2007  	&& registerizable(b, t3)
  2008  	&& registerizable(b, t4)
  2009  	&& registerizable(b, t5)
  2010  	&& n >= o2 + t2.Size()
  2011  	&& n >= o3 + t3.Size()
  2012  	&& n >= o4 + t4.Size()
  2013  	&& n >= o5 + t5.Size()
  2014  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2015  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2016  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  2017  				(Store {t5} (OffPtr <tt5> [o5] dst) d4
  2018  					(Zero {t1} [n] dst mem)))))
  2019  (Move {t1} [n] dst p1
  2020  	mem:(VarDef
  2021  		(Store {t2} op2:(OffPtr <tt2> [o2] p2) d1
  2022  			(Zero {t3} [n] p3 _))))
  2023  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3)
  2024  	&& t2.Alignment() <= t1.Alignment()
  2025  	&& t3.Alignment() <= t1.Alignment()
  2026  	&& registerizable(b, t2)
  2027  	&& n >= o2 + t2.Size()
  2028  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2029  		(Zero {t1} [n] dst mem))
  2030  (Move {t1} [n] dst p1
  2031  	mem:(VarDef
  2032  		(Store {t2} (OffPtr <tt2> [o2] p2) d1
  2033  			(Store {t3} (OffPtr <tt3> [o3] p3) d2
  2034  				(Zero {t4} [n] p4 _)))))
  2035  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4)
  2036  	&& t2.Alignment() <= t1.Alignment()
  2037  	&& t3.Alignment() <= t1.Alignment()
  2038  	&& t4.Alignment() <= t1.Alignment()
  2039  	&& registerizable(b, t2)
  2040  	&& registerizable(b, t3)
  2041  	&& n >= o2 + t2.Size()
  2042  	&& n >= o3 + t3.Size()
  2043  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2044  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2045  			(Zero {t1} [n] dst mem)))
  2046  (Move {t1} [n] dst p1
  2047  	mem:(VarDef
  2048  		(Store {t2} (OffPtr <tt2> [o2] p2) d1
  2049  			(Store {t3} (OffPtr <tt3> [o3] p3) d2
  2050  				(Store {t4} (OffPtr <tt4> [o4] p4) d3
  2051  					(Zero {t5} [n] p5 _))))))
  2052  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5)
  2053  	&& t2.Alignment() <= t1.Alignment()
  2054  	&& t3.Alignment() <= t1.Alignment()
  2055  	&& t4.Alignment() <= t1.Alignment()
  2056  	&& t5.Alignment() <= t1.Alignment()
  2057  	&& registerizable(b, t2)
  2058  	&& registerizable(b, t3)
  2059  	&& registerizable(b, t4)
  2060  	&& n >= o2 + t2.Size()
  2061  	&& n >= o3 + t3.Size()
  2062  	&& n >= o4 + t4.Size()
  2063  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2064  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2065  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  2066  				(Zero {t1} [n] dst mem))))
  2067  (Move {t1} [n] dst p1
  2068  	mem:(VarDef
  2069  		(Store {t2} (OffPtr <tt2> [o2] p2) d1
  2070  			(Store {t3} (OffPtr <tt3> [o3] p3) d2
  2071  				(Store {t4} (OffPtr <tt4> [o4] p4) d3
  2072  					(Store {t5} (OffPtr <tt5> [o5] p5) d4
  2073  						(Zero {t6} [n] p6 _)))))))
  2074  	&& isSamePtr(p1, p2) && isSamePtr(p2, p3) && isSamePtr(p3, p4) && isSamePtr(p4, p5) && isSamePtr(p5, p6)
  2075  	&& t2.Alignment() <= t1.Alignment()
  2076  	&& t3.Alignment() <= t1.Alignment()
  2077  	&& t4.Alignment() <= t1.Alignment()
  2078  	&& t5.Alignment() <= t1.Alignment()
  2079  	&& t6.Alignment() <= t1.Alignment()
  2080  	&& registerizable(b, t2)
  2081  	&& registerizable(b, t3)
  2082  	&& registerizable(b, t4)
  2083  	&& registerizable(b, t5)
  2084  	&& n >= o2 + t2.Size()
  2085  	&& n >= o3 + t3.Size()
  2086  	&& n >= o4 + t4.Size()
  2087  	&& n >= o5 + t5.Size()
  2088  	=> (Store {t2} (OffPtr <tt2> [o2] dst) d1
  2089  		(Store {t3} (OffPtr <tt3> [o3] dst) d2
  2090  			(Store {t4} (OffPtr <tt4> [o4] dst) d3
  2091  				(Store {t5} (OffPtr <tt5> [o5] dst) d4
  2092  					(Zero {t1} [n] dst mem)))))
  2093  
  2094  (SelectN [0] call:(StaticLECall {sym} a x)) && needRaceCleanup(sym, call) && clobber(call) => x
  2095  (SelectN [0] call:(StaticLECall {sym} x)) && needRaceCleanup(sym, call) && clobber(call) => x
  2096  
  2097  // When rewriting append to growslice, we use as the new length the result of
  2098  // growslice so that we don't have to spill/restore the new length around the growslice call.
  2099  // The exception here is that if the new length is a constant, avoiding spilling it
  2100  // is pointless and its constantness is sometimes useful for subsequent optimizations.
  2101  // See issue 56440.
  2102  // Note there are 2 rules here, one for the pre-decomposed []T result and one for
  2103  // the post-decomposed (*T,int,int) result. (The latter is generated after call expansion.)
  2104  // TODO(thepudds): we probably need the new growsliceBuf and growsliceBufNoAlias here as well?
  2105  (SliceLen (SelectN [0] (StaticLECall {sym} _ newLen:(Const(64|32)) _ _ _ _)))
  2106  	&& (isSameCall(sym, "runtime.growslice") || isSameCall(sym, "runtime.growsliceNoAlias"))
  2107  	=> newLen
  2108  (SelectN [1] (StaticCall {sym} _ newLen:(Const(64|32)) _ _ _ _)) && v.Type.IsInteger()
  2109  	&& (isSameCall(sym, "runtime.growslice") || isSameCall(sym, "runtime.growsliceNoAlias"))
  2110  	=> newLen
  2111  
  2112  // Collapse moving A -> B -> C into just A -> C.
  2113  // Later passes (deadstore, elim unread auto) will remove the A -> B move, if possible.
  2114  // This happens most commonly when B is an autotmp inserted earlier
  2115  // during compilation to ensure correctness.
  2116  // Take care that overlapping moves are preserved.
  2117  // Restrict this optimization to the stack, to avoid duplicating loads from the heap;
  2118  // see CL 145208 for discussion.
  2119  (Move {t1} [s] dst tmp1 midmem:(Move {t2} [s] tmp2 src _))
  2120  	&& t1.Compare(t2) == types.CMPeq
  2121  	&& isSamePtr(tmp1, tmp2)
  2122  	&& isStackPtr(src) && !isVolatile(src)
  2123  	&& disjoint(src, s, tmp2, s)
  2124  	&& (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
  2125  	=> (Move {t1} [s] dst src midmem)
  2126  
  2127  // Same, but for large types that require VarDefs.
  2128  (Move {t1} [s] dst tmp1 midmem:(VarDef (Move {t2} [s] tmp2 src _)))
  2129  	&& t1.Compare(t2) == types.CMPeq
  2130  	&& isSamePtr(tmp1, tmp2)
  2131  	&& isStackPtr(src) && !isVolatile(src)
  2132  	&& disjoint(src, s, tmp2, s)
  2133  	&& (disjoint(src, s, dst, s) || isInlinableMemmove(dst, src, s, config))
  2134  	=> (Move {t1} [s] dst src midmem)
  2135  
  2136  // Don't zero the same bits twice.
  2137  (Zero {t} [s] dst1 zero:(Zero {t} [s] dst2 _)) && isSamePtr(dst1, dst2) => zero
  2138  (Zero {t} [s] dst1 vardef:(VarDef (Zero {t} [s] dst2 _))) && isSamePtr(dst1, dst2) => vardef
  2139  
  2140  // Elide self-moves. This only happens rarely (e.g test/fixedbugs/bug277.go).
  2141  // However, this rule is needed to prevent the previous rule from looping forever in such cases.
  2142  (Move dst src mem) && isSamePtr(dst, src) => mem
  2143  
  2144  // Constant rotate detection.
  2145  ((Add64|Or64|Xor64) (Lsh64x64 x z:(Const64 <t> [c])) (Rsh64Ux64 x (Const64 [d]))) && c < 64 && d == 64-c && canRotate(config, 64) => (RotateLeft64 x z)
  2146  ((Add32|Or32|Xor32) (Lsh32x64 x z:(Const64 <t> [c])) (Rsh32Ux64 x (Const64 [d]))) && c < 32 && d == 32-c && canRotate(config, 32) => (RotateLeft32 x z)
  2147  ((Add16|Or16|Xor16) (Lsh16x64 x z:(Const64 <t> [c])) (Rsh16Ux64 x (Const64 [d]))) && c < 16 && d == 16-c && canRotate(config, 16) => (RotateLeft16 x z)
  2148  ((Add8|Or8|Xor8) (Lsh8x64 x z:(Const64 <t> [c])) (Rsh8Ux64 x (Const64 [d]))) && c < 8 && d == 8-c && canRotate(config, 8) => (RotateLeft8 x z)
  2149  
  2150  // Non-constant rotate detection.
  2151  // We use shiftIsBounded to make sure that neither of the shifts are >64.
  2152  // Note: these rules are subtle when the shift amounts are 0/64, as Go shifts
  2153  // are different from most native shifts. But it works out.
  2154  ((Add64|Or64|Xor64) left:(Lsh64x64 x y) right:(Rsh64Ux64 x (Sub64 (Const64 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
  2155  ((Add64|Or64|Xor64) left:(Lsh64x32 x y) right:(Rsh64Ux32 x (Sub32 (Const32 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
  2156  ((Add64|Or64|Xor64) left:(Lsh64x16 x y) right:(Rsh64Ux16 x (Sub16 (Const16 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
  2157  ((Add64|Or64|Xor64) left:(Lsh64x8  x y) right:(Rsh64Ux8  x (Sub8  (Const8  [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x y)
  2158  
  2159  ((Add64|Or64|Xor64) right:(Rsh64Ux64 x y) left:(Lsh64x64 x z:(Sub64 (Const64 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
  2160  ((Add64|Or64|Xor64) right:(Rsh64Ux32 x y) left:(Lsh64x32 x z:(Sub32 (Const32 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
  2161  ((Add64|Or64|Xor64) right:(Rsh64Ux16 x y) left:(Lsh64x16 x z:(Sub16 (Const16 [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
  2162  ((Add64|Or64|Xor64) right:(Rsh64Ux8  x y) left:(Lsh64x8  x z:(Sub8  (Const8  [64]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 64) => (RotateLeft64 x z)
  2163  
  2164  ((Add32|Or32|Xor32) left:(Lsh32x64 x y) right:(Rsh32Ux64 x (Sub64 (Const64 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
  2165  ((Add32|Or32|Xor32) left:(Lsh32x32 x y) right:(Rsh32Ux32 x (Sub32 (Const32 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
  2166  ((Add32|Or32|Xor32) left:(Lsh32x16 x y) right:(Rsh32Ux16 x (Sub16 (Const16 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
  2167  ((Add32|Or32|Xor32) left:(Lsh32x8  x y) right:(Rsh32Ux8  x (Sub8  (Const8  [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x y)
  2168  
  2169  ((Add32|Or32|Xor32) right:(Rsh32Ux64 x y) left:(Lsh32x64 x z:(Sub64 (Const64 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
  2170  ((Add32|Or32|Xor32) right:(Rsh32Ux32 x y) left:(Lsh32x32 x z:(Sub32 (Const32 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
  2171  ((Add32|Or32|Xor32) right:(Rsh32Ux16 x y) left:(Lsh32x16 x z:(Sub16 (Const16 [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
  2172  ((Add32|Or32|Xor32) right:(Rsh32Ux8  x y) left:(Lsh32x8  x z:(Sub8  (Const8  [32]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 32) => (RotateLeft32 x z)
  2173  
  2174  ((Add16|Or16|Xor16) left:(Lsh16x64 x y) right:(Rsh16Ux64 x (Sub64 (Const64 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
  2175  ((Add16|Or16|Xor16) left:(Lsh16x32 x y) right:(Rsh16Ux32 x (Sub32 (Const32 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
  2176  ((Add16|Or16|Xor16) left:(Lsh16x16 x y) right:(Rsh16Ux16 x (Sub16 (Const16 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
  2177  ((Add16|Or16|Xor16) left:(Lsh16x8  x y) right:(Rsh16Ux8  x (Sub8  (Const8  [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x y)
  2178  
  2179  ((Add16|Or16|Xor16) right:(Rsh16Ux64 x y) left:(Lsh16x64 x z:(Sub64 (Const64 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
  2180  ((Add16|Or16|Xor16) right:(Rsh16Ux32 x y) left:(Lsh16x32 x z:(Sub32 (Const32 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
  2181  ((Add16|Or16|Xor16) right:(Rsh16Ux16 x y) left:(Lsh16x16 x z:(Sub16 (Const16 [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
  2182  ((Add16|Or16|Xor16) right:(Rsh16Ux8  x y) left:(Lsh16x8  x z:(Sub8  (Const8  [16]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 16) => (RotateLeft16 x z)
  2183  
  2184  ((Add8|Or8|Xor8) left:(Lsh8x64 x y) right:(Rsh8Ux64 x (Sub64 (Const64 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
  2185  ((Add8|Or8|Xor8) left:(Lsh8x32 x y) right:(Rsh8Ux32 x (Sub32 (Const32 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
  2186  ((Add8|Or8|Xor8) left:(Lsh8x16 x y) right:(Rsh8Ux16 x (Sub16 (Const16 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
  2187  ((Add8|Or8|Xor8) left:(Lsh8x8  x y) right:(Rsh8Ux8  x (Sub8  (Const8  [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x y)
  2188  
  2189  ((Add8|Or8|Xor8) right:(Rsh8Ux64 x y) left:(Lsh8x64 x z:(Sub64 (Const64 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
  2190  ((Add8|Or8|Xor8) right:(Rsh8Ux32 x y) left:(Lsh8x32 x z:(Sub32 (Const32 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
  2191  ((Add8|Or8|Xor8) right:(Rsh8Ux16 x y) left:(Lsh8x16 x z:(Sub16 (Const16 [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
  2192  ((Add8|Or8|Xor8) right:(Rsh8Ux8  x y) left:(Lsh8x8  x z:(Sub8  (Const8  [8]) y))) && (shiftIsBounded(left) || shiftIsBounded(right)) && canRotate(config, 8) => (RotateLeft8 x z)
  2193  
  2194  // Rotating by y&c, with c a mask that doesn't change the bottom bits, is the same as rotating by y.
  2195  (RotateLeft64 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&63 == 63 => (RotateLeft64 x y)
  2196  (RotateLeft32 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&31 == 31 => (RotateLeft32 x y)
  2197  (RotateLeft16 x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&15 == 15 => (RotateLeft16 x y)
  2198  (RotateLeft8  x (And(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&7  == 7  => (RotateLeft8  x y)
  2199  
  2200  // Rotating by -(y&c), with c a mask that doesn't change the bottom bits, is the same as rotating by -y.
  2201  (RotateLeft64 x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&63 == 63 => (RotateLeft64 x (Neg(64|32|16|8) <y.Type> y))
  2202  (RotateLeft32 x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&31 == 31 => (RotateLeft32 x (Neg(64|32|16|8) <y.Type> y))
  2203  (RotateLeft16 x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&15 == 15 => (RotateLeft16 x (Neg(64|32|16|8) <y.Type> y))
  2204  (RotateLeft8  x (Neg(64|32|16|8) (And(64|32|16|8) y (Const(64|32|16|8) [c])))) && c&7  == 7  => (RotateLeft8  x (Neg(64|32|16|8) <y.Type> y))
  2205  
  2206  // Rotating by y+c, with c a multiple of the value width, is the same as rotating by y.
  2207  (RotateLeft64 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&63 == 0 => (RotateLeft64 x y)
  2208  (RotateLeft32 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&31 == 0 => (RotateLeft32 x y)
  2209  (RotateLeft16 x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&15 == 0 => (RotateLeft16 x y)
  2210  (RotateLeft8  x (Add(64|32|16|8) y (Const(64|32|16|8) [c]))) && c&7  == 0 => (RotateLeft8  x y)
  2211  
  2212  // Rotating by c-y, with c a multiple of the value width, is the same as rotating by -y.
  2213  (RotateLeft64 x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&63 == 0 => (RotateLeft64 x (Neg(64|32|16|8) <y.Type> y))
  2214  (RotateLeft32 x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&31 == 0 => (RotateLeft32 x (Neg(64|32|16|8) <y.Type> y))
  2215  (RotateLeft16 x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&15 == 0 => (RotateLeft16 x (Neg(64|32|16|8) <y.Type> y))
  2216  (RotateLeft8  x (Sub(64|32|16|8) (Const(64|32|16|8) [c]) y)) && c&7  == 0 => (RotateLeft8  x (Neg(64|32|16|8) <y.Type> y))
  2217  
  2218  // Ensure we don't do Const64 rotates in a 32-bit system.
  2219  (RotateLeft64 x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft64 x (Const32 <t> [int32(c)]))
  2220  (RotateLeft32 x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft32 x (Const32 <t> [int32(c)]))
  2221  (RotateLeft16 x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft16 x (Const32 <t> [int32(c)]))
  2222  (RotateLeft8  x (Const64 <t> [c])) && config.PtrSize == 4 => (RotateLeft8  x (Const32 <t> [int32(c)]))
  2223  
  2224  // Rotating by c, then by d, is the same as rotating by c+d.
  2225  // We're trading a rotate for an add, which seems generally a good choice. It is especially good when c and d are constants.
  2226  // This rule is a bit tricky as c and d might be different widths. We handle only cases where they are the same width.
  2227  (RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 8 && d.Type.Size() == 8 => (RotateLeft(64|32|16|8) x (Add64 <c.Type> c d))
  2228  (RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 4 && d.Type.Size() == 4 => (RotateLeft(64|32|16|8) x (Add32 <c.Type> c d))
  2229  (RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 2 && d.Type.Size() == 2 => (RotateLeft(64|32|16|8) x (Add16 <c.Type> c d))
  2230  (RotateLeft(64|32|16|8) (RotateLeft(64|32|16|8) x c) d) && c.Type.Size() == 1 && d.Type.Size() == 1 => (RotateLeft(64|32|16|8) x (Add8  <c.Type> c d))
  2231  
  2232  // Loading fixed addresses and constants.
  2233  (Load                                     (Addr {s} sb)         _)  && isFixedLoad(v, s,   0) => rewriteFixedLoad(v, s, sb,   0)
  2234  (Load                            (Convert (Addr {s} sb) _)      _)  && isFixedLoad(v, s,   0) => rewriteFixedLoad(v, s, sb,   0)
  2235  (Load               (ITab (IMake          (Addr {s} sb)    _))  _)  && isFixedLoad(v, s,   0) => rewriteFixedLoad(v, s, sb,   0)
  2236  (Load               (ITab (IMake (Convert (Addr {s} sb) _) _))  _)  && isFixedLoad(v, s,   0) => rewriteFixedLoad(v, s, sb,   0)
  2237  (Load (OffPtr [off]                       (Addr {s} sb)       ) _)  && isFixedLoad(v, s, off) => rewriteFixedLoad(v, s, sb, off)
  2238  (Load (OffPtr [off]              (Convert (Addr {s} sb) _)    ) _)  && isFixedLoad(v, s, off) => rewriteFixedLoad(v, s, sb, off)
  2239  (Load (OffPtr [off] (ITab (IMake          (Addr {s} sb)    _))) _)  && isFixedLoad(v, s, off) => rewriteFixedLoad(v, s, sb, off)
  2240  (Load (OffPtr [off] (ITab (IMake (Convert (Addr {s} sb) _) _))) _)  && isFixedLoad(v, s, off) => rewriteFixedLoad(v, s, sb, off)
  2241  
  2242  // Calling cmpstring a second time with the same arguments in the
  2243  // same memory state can reuse the results of the first call.
  2244  // See issue 61725.
  2245  // Note that this could pretty easily generalize to any pure function.
  2246  (SelectN [0] (StaticLECall {f} x y (SelectN [1] c:(StaticLECall {g} x y mem))))
  2247    && isSameCall(f, "runtime.cmpstring")
  2248    && isSameCall(g, "runtime.cmpstring")
  2249  => @c.Block (SelectN [0] <typ.Int> c)
  2250  
  2251  // If we don't use the result of cmpstring, might as well not call it.
  2252  // Note that this could pretty easily generalize to any pure function.
  2253  (SelectN [1] c:(StaticLECall {f} _ _ mem)) && c.Uses == 1 && isSameCall(f, "runtime.cmpstring") && clobber(c) => mem
  2254  
  2255  // We can easily compute the result of efaceeq if
  2256  // we know the underlying type is pointer-ish.
  2257  (StaticLECall {f} typ_ x y mem)
  2258  	&& isSameCall(f, "runtime.efaceeq")
  2259  	&& isDirectAndComparableType(typ_)
  2260  	&& clobber(v)
  2261  	=> (MakeResult (EqPtr x y) mem)
  2262  
  2263  // We can easily compute the result of ifaceeq if
  2264  // we know the underlying type is pointer-ish.
  2265  (StaticLECall {f} itab x y mem)
  2266  	&& isSameCall(f, "runtime.ifaceeq")
  2267  	&& isDirectAndComparableIface(itab)
  2268  	&& clobber(v)
  2269  	=> (MakeResult (EqPtr x y) mem)
  2270  
  2271  // If we use the result of slicebytetostring in a map lookup operation,
  2272  // then we don't need to actually do the []byte->string conversion.
  2273  // We can just use the ptr/len of the byte slice directly as a (temporary) string.
  2274  //
  2275  // Note that this does not handle some obscure cases like
  2276  // m[[2]string{string(b1), string(b2)}]. There is code in ../walk/order.go
  2277  // which handles some of those cases.
  2278  (StaticLECall {f} [argsize] typ_ map_ key:(SelectN [0] sbts:(StaticLECall {g} _ ptr len mem)) m:(SelectN [1] sbts))
  2279    &&    (isSameCall(f, "runtime.mapaccess1_faststr")
  2280        || isSameCall(f, "runtime.mapaccess2_faststr")
  2281        || isSameCall(f, "runtime.mapdelete_faststr"))
  2282    && isSameCall(g, "runtime.slicebytetostring")
  2283    && key.Uses == 1
  2284    && sbts.Uses == 2
  2285    && resetCopy(m, mem)
  2286    && clobber(sbts)
  2287    && clobber(key)
  2288  => (StaticLECall {f} [argsize] typ_ map_ (StringMake <typ.String> ptr len) mem)
  2289  
  2290  // Similarly to map lookups, also handle unique.Make for strings, which unique.Make will clone.
  2291  (StaticLECall {f} [argsize] dict_ key:(SelectN [0] sbts:(StaticLECall {g} _ ptr len mem)) m:(SelectN [1] sbts))
  2292    && isSameCall(f, "unique.Make[go.shape.string]")
  2293    && isSameCall(g, "runtime.slicebytetostring")
  2294    && key.Uses == 1
  2295    && sbts.Uses == 2
  2296    && resetCopy(m, mem)
  2297    && clobber(sbts)
  2298    && clobber(key)
  2299  => (StaticLECall {f} [argsize] dict_ (StringMake <typ.String> ptr len) mem)
  2300  
  2301  // Transform some CondSelect into math operations.
  2302  // if b { x++ } => x += b // but not on arm64 because it has CSINC
  2303  (CondSelect (Add8 <t> x (Const8 [1])) x bool) && config.arch != "arm64" => (Add8 x (CvtBoolToUint8 <t> bool))
  2304  (CondSelect (Add(64|32|16) <t> x (Const(64|32|16) [1])) x bool) && config.arch != "arm64" => (Add(64|32|16) x (ZeroExt8to(64|32|16) <t> (CvtBoolToUint8 <types.Types[types.TUINT8]> bool)))
  2305  
  2306  // if b { x-- } => x -= b
  2307  (CondSelect (Add8 <t> x (Const8 [-1])) x bool) => (Sub8 x (CvtBoolToUint8 <t> bool))
  2308  (CondSelect (Add(64|32|16) <t> x (Const(64|32|16) [-1])) x bool) => (Sub(64|32|16) x (ZeroExt8to(64|32|16) <t> (CvtBoolToUint8 <types.Types[types.TUINT8]> bool)))
  2309  
  2310  // if b { x <<= 1 } => x <<= b
  2311  (CondSelect (Lsh(64|32|16|8)x64 x (Const64 [1])) x bool) => (Lsh(64|32|16|8)x8 [true] x (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))
  2312  
  2313  // if b { x >>= 1 } => x >>= b
  2314  (CondSelect (Rsh(64|32|16|8)x64 x (Const64 [1])) x bool) => (Rsh(64|32|16|8)x8 [true] x (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))
  2315  (CondSelect (Rsh(64|32|16|8)Ux64 x (Const64 [1])) x bool) => (Rsh(64|32|16|8)Ux8 [true] x (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))
  2316  
  2317  // bool(int(x)) => x
  2318  (Neq8                                (CvtBoolToUint8 x)  (Const8          [0])) => x
  2319  (Neq8                                (CvtBoolToUint8 x)  (Const8          [1])) => (Not x)
  2320  (Eq8                                 (CvtBoolToUint8 x)  (Const8          [1])) => x
  2321  (Eq8                                 (CvtBoolToUint8 x)  (Const8          [0])) => (Not x)
  2322  (Neq(64|32|16) (ZeroExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [0])) => x
  2323  (Neq(64|32|16) (ZeroExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [1])) => (Not x)
  2324  (Eq(64|32|16)  (ZeroExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [1])) => x
  2325  (Eq(64|32|16)  (ZeroExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [0])) => (Not x)
  2326  (Neq(64|32|16) (SignExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [0])) => x
  2327  (Neq(64|32|16) (SignExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [1])) => (Not x)
  2328  (Eq(64|32|16)  (SignExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [1])) => x
  2329  (Eq(64|32|16)  (SignExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [0])) => (Not x)
  2330  

View as plain text