github.com/bir3/gocompiler@v0.3.205/src/cmd/compile/internal/ssa/_gen/generic.rules (about)

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