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

View as plain text