...

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

View as plain text