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

View as plain text