...

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

Documentation: cmd/compile/internal/ssa/_gen

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

View as plain text