github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/cmd/compile/internal/ssa/gen/generic.rules (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Simplifications that apply to all backend architectures. As an example, this
     6  // Go source code
     7  //
     8  // y := 0 * x
     9  //
    10  // can be translated into y := 0 without losing any information, which saves a
    11  // pointless multiplication instruction. Other .rules files in this directory
    12  // (for example AMD64.rules) contain rules specific to the architecture in the
    13  // filename. The rules here apply to every architecture.
    14  //
    15  // The code for parsing this file lives in rulegen.go; this file generates
    16  // ssa/rewritegeneric.go.
    17  
    18  // values are specified using the following format:
    19  // (op <type> [auxint] {aux} arg0 arg1 ...)
    20  // the type, aux, and auxint fields are optional
    21  // on the matching side
    22  //  - the type, aux, and auxint fields must match if they are specified.
    23  //  - the first occurrence of a variable defines that variable.  Subsequent
    24  //    uses must match (be == to) the first use.
    25  //  - v is defined to be the value matched.
    26  //  - an additional conditional can be provided after the match pattern with "&&".
    27  // on the generated side
    28  //  - the type of the top-level expression is the same as the one on the left-hand side.
    29  //  - the type of any subexpressions must be specified explicitly (or
    30  //    be specified in the op's type field).
    31  //  - auxint will be 0 if not specified.
    32  //  - aux will be nil if not specified.
    33  
    34  // blocks are specified using the following format:
    35  // (kind controlvalue succ0 succ1 ...)
    36  // controlvalue must be "nil" or a value expression
    37  // succ* fields must be variables
    38  // For now, the generated successors must be a permutation of the matched successors.
    39  
    40  // constant folding
    41  (Trunc16to8  (Const16 [c]))  -> (Const8   [int64(int8(c))])
    42  (Trunc32to8  (Const32 [c]))  -> (Const8   [int64(int8(c))])
    43  (Trunc32to16 (Const32 [c]))  -> (Const16  [int64(int16(c))])
    44  (Trunc64to8  (Const64 [c]))  -> (Const8   [int64(int8(c))])
    45  (Trunc64to16 (Const64 [c]))  -> (Const16  [int64(int16(c))])
    46  (Trunc64to32 (Const64 [c]))  -> (Const32  [int64(int32(c))])
    47  (Cvt64Fto32F (Const64F [c])) -> (Const32F [f2i(float64(i2f32(c)))])
    48  (Cvt32Fto64F (Const32F [c])) -> (Const64F [c]) // c is already a 64 bit float
    49  (Round32F x:(Const32F)) -> x
    50  (Round64F x:(Const64F)) -> x
    51  
    52  (Trunc16to8  (ZeroExt8to16  x)) -> x
    53  (Trunc32to8  (ZeroExt8to32  x)) -> x
    54  (Trunc32to16 (ZeroExt8to32  x)) -> (ZeroExt8to16  x)
    55  (Trunc32to16 (ZeroExt16to32 x)) -> x
    56  (Trunc64to8  (ZeroExt8to64  x)) -> x
    57  (Trunc64to16 (ZeroExt8to64  x)) -> (ZeroExt8to16  x)
    58  (Trunc64to16 (ZeroExt16to64 x)) -> x
    59  (Trunc64to32 (ZeroExt8to64  x)) -> (ZeroExt8to32  x)
    60  (Trunc64to32 (ZeroExt16to64 x)) -> (ZeroExt16to32 x)
    61  (Trunc64to32 (ZeroExt32to64 x)) -> x
    62  (Trunc16to8  (SignExt8to16  x)) -> x
    63  (Trunc32to8  (SignExt8to32  x)) -> x
    64  (Trunc32to16 (SignExt8to32  x)) -> (SignExt8to16  x)
    65  (Trunc32to16 (SignExt16to32 x)) -> x
    66  (Trunc64to8  (SignExt8to64  x)) -> x
    67  (Trunc64to16 (SignExt8to64  x)) -> (SignExt8to16  x)
    68  (Trunc64to16 (SignExt16to64 x)) -> x
    69  (Trunc64to32 (SignExt8to64  x)) -> (SignExt8to32  x)
    70  (Trunc64to32 (SignExt16to64 x)) -> (SignExt16to32 x)
    71  (Trunc64to32 (SignExt32to64 x)) -> x
    72  
    73  (ZeroExt8to16  (Const8  [c])) -> (Const16 [int64( uint8(c))])
    74  (ZeroExt8to32  (Const8  [c])) -> (Const32 [int64( uint8(c))])
    75  (ZeroExt8to64  (Const8  [c])) -> (Const64 [int64( uint8(c))])
    76  (ZeroExt16to32 (Const16 [c])) -> (Const32 [int64(uint16(c))])
    77  (ZeroExt16to64 (Const16 [c])) -> (Const64 [int64(uint16(c))])
    78  (ZeroExt32to64 (Const32 [c])) -> (Const64 [int64(uint32(c))])
    79  (SignExt8to16  (Const8  [c])) -> (Const16 [int64(  int8(c))])
    80  (SignExt8to32  (Const8  [c])) -> (Const32 [int64(  int8(c))])
    81  (SignExt8to64  (Const8  [c])) -> (Const64 [int64(  int8(c))])
    82  (SignExt16to32 (Const16 [c])) -> (Const32 [int64( int16(c))])
    83  (SignExt16to64 (Const16 [c])) -> (Const64 [int64( int16(c))])
    84  (SignExt32to64 (Const32 [c])) -> (Const64 [int64( int32(c))])
    85  
    86  (Neg8   (Const8   [c])) -> (Const8   [int64( -int8(c))])
    87  (Neg16  (Const16  [c])) -> (Const16  [int64(-int16(c))])
    88  (Neg32  (Const32  [c])) -> (Const32  [int64(-int32(c))])
    89  (Neg64  (Const64  [c])) -> (Const64  [-c])
    90  (Neg32F (Const32F [c])) && i2f(c) != 0 -> (Const32F [f2i(-i2f(c))])
    91  (Neg64F (Const64F [c])) && i2f(c) != 0 -> (Const64F [f2i(-i2f(c))])
    92  
    93  (Add8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c+d))])
    94  (Add16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c+d))])
    95  (Add32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c+d))])
    96  (Add64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c+d])
    97  (Add32F (Const32F [c]) (Const32F [d])) ->
    98          (Const32F [f2i(float64(i2f32(c) + i2f32(d)))]) // ensure we combine the operands with 32 bit precision
    99  (Add64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) + i2f(d))])
   100  (AddPtr <t> x (Const64 [c])) -> (OffPtr <t> x [c])
   101  (AddPtr <t> x (Const32 [c])) -> (OffPtr <t> x [c])
   102  
   103  (Sub8   (Const8 [c]) (Const8 [d]))     -> (Const8 [int64(int8(c-d))])
   104  (Sub16  (Const16 [c]) (Const16 [d]))   -> (Const16 [int64(int16(c-d))])
   105  (Sub32  (Const32 [c]) (Const32 [d]))   -> (Const32 [int64(int32(c-d))])
   106  (Sub64  (Const64 [c]) (Const64 [d]))   -> (Const64 [c-d])
   107  (Sub32F (Const32F [c]) (Const32F [d])) ->
   108          (Const32F [f2i(float64(i2f32(c) - i2f32(d)))])
   109  (Sub64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) - i2f(d))])
   110  
   111  (Mul8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c*d))])
   112  (Mul16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c*d))])
   113  (Mul32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c*d))])
   114  (Mul64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c*d])
   115  (Mul32F (Const32F [c]) (Const32F [d])) ->
   116          (Const32F [f2i(float64(i2f32(c) * i2f32(d)))])
   117  (Mul64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) * i2f(d))])
   118  
   119  (And8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c&d))])
   120  (And16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c&d))])
   121  (And32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c&d))])
   122  (And64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c&d])
   123  
   124  (Or8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c|d))])
   125  (Or16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c|d))])
   126  (Or32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c|d))])
   127  (Or64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c|d])
   128  
   129  (Xor8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c^d))])
   130  (Xor16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c^d))])
   131  (Xor32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c^d))])
   132  (Xor64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c^d])
   133  
   134  (Div8   (Const8  [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(int8(c)/int8(d))])
   135  (Div16  (Const16 [c])  (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c)/int16(d))])
   136  (Div32  (Const32 [c])  (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c)/int32(d))])
   137  (Div64  (Const64 [c])  (Const64 [d])) && d != 0 -> (Const64 [c/d])
   138  (Div8u  (Const8  [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(int8(uint8(c)/uint8(d)))])
   139  (Div16u (Const16 [c])  (Const16 [d])) && d != 0 -> (Const16 [int64(int16(uint16(c)/uint16(d)))])
   140  (Div32u (Const32 [c])  (Const32 [d])) && d != 0 -> (Const32 [int64(int32(uint32(c)/uint32(d)))])
   141  (Div64u (Const64 [c])  (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c)/uint64(d))])
   142  (Div32F (Const32F [c]) (Const32F [d])) -> (Const32F [f2i(float64(i2f32(c) / i2f32(d)))])
   143  (Div64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) / i2f(d))])
   144  
   145  // Convert x * 1 to x.
   146  (Mul8  (Const8  [1]) x) -> x
   147  (Mul16 (Const16 [1]) x) -> x
   148  (Mul32 (Const32 [1]) x) -> x
   149  (Mul64 (Const64 [1]) x) -> x
   150  
   151  // Convert x * -1 to -x.
   152  (Mul8  (Const8  [-1]) x) -> (Neg8  x)
   153  (Mul16 (Const16 [-1]) x) -> (Neg16 x)
   154  (Mul32 (Const32 [-1]) x) -> (Neg32 x)
   155  (Mul64 (Const64 [-1]) x) -> (Neg64 x)
   156  
   157  // Convert multiplication by a power of two to a shift.
   158  (Mul8  <t> n (Const8  [c])) && isPowerOfTwo(c) -> (Lsh8x64  <t> n (Const64 <types.UInt64> [log2(c)]))
   159  (Mul16 <t> n (Const16 [c])) && isPowerOfTwo(c) -> (Lsh16x64 <t> n (Const64 <types.UInt64> [log2(c)]))
   160  (Mul32 <t> n (Const32 [c])) && isPowerOfTwo(c) -> (Lsh32x64 <t> n (Const64 <types.UInt64> [log2(c)]))
   161  (Mul64 <t> n (Const64 [c])) && isPowerOfTwo(c) -> (Lsh64x64 <t> n (Const64 <types.UInt64> [log2(c)]))
   162  (Mul8  <t> n (Const8  [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg8  (Lsh8x64  <t> n (Const64 <types.UInt64> [log2(-c)])))
   163  (Mul16 <t> n (Const16 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg16 (Lsh16x64 <t> n (Const64 <types.UInt64> [log2(-c)])))
   164  (Mul32 <t> n (Const32 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg32 (Lsh32x64 <t> n (Const64 <types.UInt64> [log2(-c)])))
   165  (Mul64 <t> n (Const64 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg64 (Lsh64x64 <t> n (Const64 <types.UInt64> [log2(-c)])))
   166  
   167  (Mod8  (Const8  [c]) (Const8  [d])) && d != 0 -> (Const8  [int64(int8(c % d))])
   168  (Mod16 (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c % d))])
   169  (Mod32 (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c % d))])
   170  (Mod64 (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [c % d])
   171  
   172  (Mod8u  (Const8 [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(uint8(c) % uint8(d))])
   173  (Mod16u (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(uint16(c) % uint16(d))])
   174  (Mod32u (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(uint32(c) % uint32(d))])
   175  (Mod64u (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c) % uint64(d))])
   176  
   177  (Lsh64x64  (Const64 [c]) (Const64 [d])) -> (Const64 [c << uint64(d)])
   178  (Rsh64x64  (Const64 [c]) (Const64 [d])) -> (Const64 [c >> uint64(d)])
   179  (Rsh64Ux64 (Const64 [c]) (Const64 [d])) -> (Const64 [int64(uint64(c) >> uint64(d))])
   180  (Lsh32x64  (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) << uint64(d))])
   181  (Rsh32x64  (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) >> uint64(d))])
   182  (Rsh32Ux64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(uint32(c) >> uint64(d)))])
   183  (Lsh16x64  (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) << uint64(d))])
   184  (Rsh16x64  (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) >> uint64(d))])
   185  (Rsh16Ux64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(uint16(c) >> uint64(d)))])
   186  (Lsh8x64   (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(c) << uint64(d))])
   187  (Rsh8x64   (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(c) >> uint64(d))])
   188  (Rsh8Ux64  (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(uint8(c) >> uint64(d)))])
   189  
   190  // Fold IsInBounds when the range of the index cannot exceed the limit.
   191  (IsInBounds (ZeroExt8to32  _) (Const32 [c])) && (1 << 8)  <= c -> (ConstBool [1])
   192  (IsInBounds (ZeroExt8to64  _) (Const64 [c])) && (1 << 8)  <= c -> (ConstBool [1])
   193  (IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= c -> (ConstBool [1])
   194  (IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c -> (ConstBool [1])
   195  (IsInBounds x x) -> (ConstBool [0])
   196  (IsInBounds                (And8  (Const8  [c]) _)  (Const8  [d])) && 0 <= c && c < d -> (ConstBool [1])
   197  (IsInBounds (ZeroExt8to16  (And8  (Const8  [c]) _)) (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1])
   198  (IsInBounds (ZeroExt8to32  (And8  (Const8  [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
   199  (IsInBounds (ZeroExt8to64  (And8  (Const8  [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   200  (IsInBounds                (And16 (Const16 [c]) _)  (Const16 [d])) && 0 <= c && c < d -> (ConstBool [1])
   201  (IsInBounds (ZeroExt16to32 (And16 (Const16 [c]) _)) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
   202  (IsInBounds (ZeroExt16to64 (And16 (Const16 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   203  (IsInBounds                (And32 (Const32 [c]) _)  (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
   204  (IsInBounds (ZeroExt32to64 (And32 (Const32 [c]) _)) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   205  (IsInBounds                (And64 (Const64 [c]) _)  (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   206  (IsInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c < d)])
   207  (IsInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c < d)])
   208  // (Mod64u x y) is always between 0 (inclusive) and y (exclusive).
   209  (IsInBounds (Mod32u _ y) y) -> (ConstBool [1])
   210  (IsInBounds (Mod64u _ y) y) -> (ConstBool [1])
   211  
   212  (IsSliceInBounds x x) -> (ConstBool [1])
   213  (IsSliceInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c <= d -> (ConstBool [1])
   214  (IsSliceInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c <= d -> (ConstBool [1])
   215  (IsSliceInBounds (Const32 [0]) _) -> (ConstBool [1])
   216  (IsSliceInBounds (Const64 [0]) _) -> (ConstBool [1])
   217  (IsSliceInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c <= d)])
   218  (IsSliceInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c <= d)])
   219  (IsSliceInBounds (SliceLen x) (SliceCap x)) -> (ConstBool [1])
   220  
   221  (Eq64 x x) -> (ConstBool [1])
   222  (Eq32 x x) -> (ConstBool [1])
   223  (Eq16 x x) -> (ConstBool [1])
   224  (Eq8  x x) -> (ConstBool [1])
   225  (EqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c == d)])
   226  (EqB (ConstBool [0]) x) -> (Not x)
   227  (EqB (ConstBool [1]) x) -> x
   228  
   229  (Neq64 x x) -> (ConstBool [0])
   230  (Neq32 x x) -> (ConstBool [0])
   231  (Neq16 x x) -> (ConstBool [0])
   232  (Neq8  x x) -> (ConstBool [0])
   233  (NeqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c != d)])
   234  (NeqB (ConstBool [0]) x) -> x
   235  (NeqB (ConstBool [1]) x) -> (Not x)
   236  
   237  (Eq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Eq64 (Const64 <t> [c-d]) x)
   238  (Eq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Eq32 (Const32 <t> [int64(int32(c-d))]) x)
   239  (Eq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Eq16 (Const16 <t> [int64(int16(c-d))]) x)
   240  (Eq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Eq8  (Const8 <t> [int64(int8(c-d))]) x)
   241  
   242  (Neq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Neq64 (Const64 <t> [c-d]) x)
   243  (Neq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Neq32 (Const32 <t> [int64(int32(c-d))]) x)
   244  (Neq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Neq16 (Const16 <t> [int64(int16(c-d))]) x)
   245  (Neq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Neq8 (Const8 <t> [int64(int8(c-d))]) x)
   246  
   247  // Canonicalize x-const to x+(-const)
   248  (Sub64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Add64 (Const64 <t> [-c]) x)
   249  (Sub32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Add32 (Const32 <t> [int64(int32(-c))]) x)
   250  (Sub16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Add16 (Const16 <t> [int64(int16(-c))]) x)
   251  (Sub8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (Add8  (Const8  <t> [int64(int8(-c))]) x)
   252  
   253  // fold negation into comparison operators
   254  (Not (Eq64 x y)) -> (Neq64 x y)
   255  (Not (Eq32 x y)) -> (Neq32 x y)
   256  (Not (Eq16 x y)) -> (Neq16 x y)
   257  (Not (Eq8  x y)) -> (Neq8  x y)
   258  (Not (EqB  x y)) -> (NeqB  x y)
   259  
   260  (Not (Neq64 x y)) -> (Eq64 x y)
   261  (Not (Neq32 x y)) -> (Eq32 x y)
   262  (Not (Neq16 x y)) -> (Eq16 x y)
   263  (Not (Neq8  x y)) -> (Eq8  x y)
   264  (Not (NeqB  x y)) -> (EqB  x y)
   265  
   266  (Not (Greater64 x y)) -> (Leq64 x y)
   267  (Not (Greater32 x y)) -> (Leq32 x y)
   268  (Not (Greater16 x y)) -> (Leq16 x y)
   269  (Not (Greater8  x y)) -> (Leq8  x y)
   270  
   271  (Not (Greater64U x y)) -> (Leq64U x y)
   272  (Not (Greater32U x y)) -> (Leq32U x y)
   273  (Not (Greater16U x y)) -> (Leq16U x y)
   274  (Not (Greater8U  x y)) -> (Leq8U  x y)
   275  
   276  (Not (Geq64 x y)) -> (Less64 x y)
   277  (Not (Geq32 x y)) -> (Less32 x y)
   278  (Not (Geq16 x y)) -> (Less16 x y)
   279  (Not (Geq8  x y)) -> (Less8  x y)
   280  
   281  (Not (Geq64U x y)) -> (Less64U x y)
   282  (Not (Geq32U x y)) -> (Less32U x y)
   283  (Not (Geq16U x y)) -> (Less16U x y)
   284  (Not (Geq8U  x y)) -> (Less8U  x y)
   285  
   286  (Not (Less64 x y)) -> (Geq64 x y)
   287  (Not (Less32 x y)) -> (Geq32 x y)
   288  (Not (Less16 x y)) -> (Geq16 x y)
   289  (Not (Less8  x y)) -> (Geq8  x y)
   290  
   291  (Not (Less64U x y)) -> (Geq64U x y)
   292  (Not (Less32U x y)) -> (Geq32U x y)
   293  (Not (Less16U x y)) -> (Geq16U x y)
   294  (Not (Less8U  x y)) -> (Geq8U  x y)
   295  
   296  (Not (Leq64 x y)) -> (Greater64 x y)
   297  (Not (Leq32 x y)) -> (Greater32 x y)
   298  (Not (Leq16 x y)) -> (Greater16 x y)
   299  (Not (Leq8  x y)) -> (Greater8 x y)
   300  
   301  (Not (Leq64U x y)) -> (Greater64U x y)
   302  (Not (Leq32U x y)) -> (Greater32U x y)
   303  (Not (Leq16U x y)) -> (Greater16U x y)
   304  (Not (Leq8U  x y)) -> (Greater8U  x y)
   305  
   306  // Distribute multiplication c * (d+x) -> c*d + c*x. Useful for:
   307  // a[i].b = ...; a[i+1].b = ...
   308  (Mul64 (Const64 <t> [c]) (Add64 <t> (Const64 <t> [d]) x)) ->
   309    (Add64 (Const64 <t> [c*d]) (Mul64 <t> (Const64 <t> [c]) x))
   310  (Mul32 (Const32 <t> [c]) (Add32 <t> (Const32 <t> [d]) x)) ->
   311    (Add32 (Const32 <t> [int64(int32(c*d))]) (Mul32 <t> (Const32 <t> [c]) x))
   312  
   313  // rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce
   314  // the number of the other rewrite rules for const shifts
   315  (Lsh64x32  <t> x (Const32 [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint32(c))]))
   316  (Lsh64x16  <t> x (Const16 [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint16(c))]))
   317  (Lsh64x8   <t> x (Const8  [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint8(c))]))
   318  (Rsh64x32  <t> x (Const32 [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint32(c))]))
   319  (Rsh64x16  <t> x (Const16 [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint16(c))]))
   320  (Rsh64x8   <t> x (Const8  [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint8(c))]))
   321  (Rsh64Ux32 <t> x (Const32 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))]))
   322  (Rsh64Ux16 <t> x (Const16 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))]))
   323  (Rsh64Ux8  <t> x (Const8  [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))]))
   324  
   325  (Lsh32x32  <t> x (Const32 [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint32(c))]))
   326  (Lsh32x16  <t> x (Const16 [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint16(c))]))
   327  (Lsh32x8   <t> x (Const8  [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint8(c))]))
   328  (Rsh32x32  <t> x (Const32 [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint32(c))]))
   329  (Rsh32x16  <t> x (Const16 [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint16(c))]))
   330  (Rsh32x8   <t> x (Const8  [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint8(c))]))
   331  (Rsh32Ux32 <t> x (Const32 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))]))
   332  (Rsh32Ux16 <t> x (Const16 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))]))
   333  (Rsh32Ux8  <t> x (Const8  [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))]))
   334  
   335  (Lsh16x32  <t> x (Const32 [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint32(c))]))
   336  (Lsh16x16  <t> x (Const16 [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint16(c))]))
   337  (Lsh16x8   <t> x (Const8  [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint8(c))]))
   338  (Rsh16x32  <t> x (Const32 [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint32(c))]))
   339  (Rsh16x16  <t> x (Const16 [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint16(c))]))
   340  (Rsh16x8   <t> x (Const8  [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint8(c))]))
   341  (Rsh16Ux32 <t> x (Const32 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))]))
   342  (Rsh16Ux16 <t> x (Const16 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))]))
   343  (Rsh16Ux8  <t> x (Const8  [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))]))
   344  
   345  (Lsh8x32  <t> x (Const32 [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint32(c))]))
   346  (Lsh8x16  <t> x (Const16 [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint16(c))]))
   347  (Lsh8x8   <t> x (Const8  [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint8(c))]))
   348  (Rsh8x32  <t> x (Const32 [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint32(c))]))
   349  (Rsh8x16  <t> x (Const16 [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint16(c))]))
   350  (Rsh8x8   <t> x (Const8  [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint8(c))]))
   351  (Rsh8Ux32 <t> x (Const32 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))]))
   352  (Rsh8Ux16 <t> x (Const16 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))]))
   353  (Rsh8Ux8  <t> x (Const8  [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))]))
   354  
   355  // shifts by zero
   356  (Lsh64x64  x (Const64 [0])) -> x
   357  (Rsh64x64  x (Const64 [0])) -> x
   358  (Rsh64Ux64 x (Const64 [0])) -> x
   359  (Lsh32x64  x (Const64 [0])) -> x
   360  (Rsh32x64  x (Const64 [0])) -> x
   361  (Rsh32Ux64 x (Const64 [0])) -> x
   362  (Lsh16x64  x (Const64 [0])) -> x
   363  (Rsh16x64  x (Const64 [0])) -> x
   364  (Rsh16Ux64 x (Const64 [0])) -> x
   365  (Lsh8x64   x (Const64 [0])) -> x
   366  (Rsh8x64   x (Const64 [0])) -> x
   367  (Rsh8Ux64  x (Const64 [0])) -> x
   368  
   369  // zero shifted.
   370  (Lsh64x64  (Const64 [0]) _) -> (Const64 [0])
   371  (Lsh64x32  (Const64 [0]) _) -> (Const64 [0])
   372  (Lsh64x16  (Const64 [0]) _) -> (Const64 [0])
   373  (Lsh64x8  (Const64 [0]) _) -> (Const64 [0])
   374  (Rsh64x64  (Const64 [0]) _) -> (Const64 [0])
   375  (Rsh64x32  (Const64 [0]) _) -> (Const64 [0])
   376  (Rsh64x16  (Const64 [0]) _) -> (Const64 [0])
   377  (Rsh64x8  (Const64 [0]) _) -> (Const64 [0])
   378  (Rsh64Ux64 (Const64 [0]) _) -> (Const64 [0])
   379  (Rsh64Ux32 (Const64 [0]) _) -> (Const64 [0])
   380  (Rsh64Ux16 (Const64 [0]) _) -> (Const64 [0])
   381  (Rsh64Ux8 (Const64 [0]) _) -> (Const64 [0])
   382  (Lsh32x64  (Const32 [0]) _) -> (Const32 [0])
   383  (Lsh32x32  (Const32 [0]) _) -> (Const32 [0])
   384  (Lsh32x16  (Const32 [0]) _) -> (Const32 [0])
   385  (Lsh32x8  (Const32 [0]) _) -> (Const32 [0])
   386  (Rsh32x64  (Const32 [0]) _) -> (Const32 [0])
   387  (Rsh32x32  (Const32 [0]) _) -> (Const32 [0])
   388  (Rsh32x16  (Const32 [0]) _) -> (Const32 [0])
   389  (Rsh32x8  (Const32 [0]) _) -> (Const32 [0])
   390  (Rsh32Ux64 (Const32 [0]) _) -> (Const32 [0])
   391  (Rsh32Ux32 (Const32 [0]) _) -> (Const32 [0])
   392  (Rsh32Ux16 (Const32 [0]) _) -> (Const32 [0])
   393  (Rsh32Ux8 (Const32 [0]) _) -> (Const32 [0])
   394  (Lsh16x64  (Const16 [0]) _) -> (Const16 [0])
   395  (Lsh16x32  (Const16 [0]) _) -> (Const16 [0])
   396  (Lsh16x16  (Const16 [0]) _) -> (Const16 [0])
   397  (Lsh16x8  (Const16 [0]) _) -> (Const16 [0])
   398  (Rsh16x64  (Const16 [0]) _) -> (Const16 [0])
   399  (Rsh16x32  (Const16 [0]) _) -> (Const16 [0])
   400  (Rsh16x16  (Const16 [0]) _) -> (Const16 [0])
   401  (Rsh16x8  (Const16 [0]) _) -> (Const16 [0])
   402  (Rsh16Ux64 (Const16 [0]) _) -> (Const16 [0])
   403  (Rsh16Ux32 (Const16 [0]) _) -> (Const16 [0])
   404  (Rsh16Ux16 (Const16 [0]) _) -> (Const16 [0])
   405  (Rsh16Ux8 (Const16 [0]) _) -> (Const16 [0])
   406  (Lsh8x64   (Const8 [0]) _) -> (Const8  [0])
   407  (Lsh8x32   (Const8 [0]) _) -> (Const8  [0])
   408  (Lsh8x16   (Const8 [0]) _) -> (Const8  [0])
   409  (Lsh8x8   (Const8 [0]) _) -> (Const8  [0])
   410  (Rsh8x64   (Const8 [0]) _) -> (Const8  [0])
   411  (Rsh8x32   (Const8 [0]) _) -> (Const8  [0])
   412  (Rsh8x16   (Const8 [0]) _) -> (Const8  [0])
   413  (Rsh8x8   (Const8 [0]) _) -> (Const8  [0])
   414  (Rsh8Ux64  (Const8 [0]) _) -> (Const8  [0])
   415  (Rsh8Ux32  (Const8 [0]) _) -> (Const8  [0])
   416  (Rsh8Ux16  (Const8 [0]) _) -> (Const8  [0])
   417  (Rsh8Ux8  (Const8 [0]) _) -> (Const8  [0])
   418  
   419  // large left shifts of all values, and right shifts of unsigned values
   420  (Lsh64x64  _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0])
   421  (Rsh64Ux64 _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0])
   422  (Lsh32x64  _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0])
   423  (Rsh32Ux64 _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0])
   424  (Lsh16x64  _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0])
   425  (Rsh16Ux64 _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0])
   426  (Lsh8x64   _ (Const64 [c])) && uint64(c) >= 8  -> (Const8  [0])
   427  (Rsh8Ux64  _ (Const64 [c])) && uint64(c) >= 8  -> (Const8  [0])
   428  
   429  // combine const shifts
   430  (Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh64x64 x (Const64 <t> [c+d]))
   431  (Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh32x64 x (Const64 <t> [c+d]))
   432  (Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh16x64 x (Const64 <t> [c+d]))
   433  (Lsh8x64  <t> (Lsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh8x64  x (Const64 <t> [c+d]))
   434  
   435  (Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64x64 x (Const64 <t> [c+d]))
   436  (Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32x64 x (Const64 <t> [c+d]))
   437  (Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16x64 x (Const64 <t> [c+d]))
   438  (Rsh8x64  <t> (Rsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8x64  x (Const64 <t> [c+d]))
   439  
   440  (Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64Ux64 x (Const64 <t> [c+d]))
   441  (Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32Ux64 x (Const64 <t> [c+d]))
   442  (Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16Ux64 x (Const64 <t> [c+d]))
   443  (Rsh8Ux64  <t> (Rsh8Ux64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8Ux64  x (Const64 <t> [c+d]))
   444  
   445  // ((x >> c1) << c2) >> c3
   446  (Rsh64Ux64 (Lsh64x64 (Rsh64Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   447    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   448    -> (Rsh64Ux64 x (Const64 <types.UInt64> [c1-c2+c3]))
   449  (Rsh32Ux64 (Lsh32x64 (Rsh32Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   450    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   451    -> (Rsh32Ux64 x (Const64 <types.UInt64> [c1-c2+c3]))
   452  (Rsh16Ux64 (Lsh16x64 (Rsh16Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   453    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   454    -> (Rsh16Ux64 x (Const64 <types.UInt64> [c1-c2+c3]))
   455  (Rsh8Ux64 (Lsh8x64 (Rsh8Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   456    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   457    -> (Rsh8Ux64 x (Const64 <types.UInt64> [c1-c2+c3]))
   458  
   459  // ((x << c1) >> c2) << c3
   460  (Lsh64x64 (Rsh64Ux64 (Lsh64x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   461    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   462    -> (Lsh64x64 x (Const64 <types.UInt64> [c1-c2+c3]))
   463  (Lsh32x64 (Rsh32Ux64 (Lsh32x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   464    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   465    -> (Lsh32x64 x (Const64 <types.UInt64> [c1-c2+c3]))
   466  (Lsh16x64 (Rsh16Ux64 (Lsh16x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   467    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   468    -> (Lsh16x64 x (Const64 <types.UInt64> [c1-c2+c3]))
   469  (Lsh8x64 (Rsh8Ux64 (Lsh8x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   470    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   471    -> (Lsh8x64 x (Const64 <types.UInt64> [c1-c2+c3]))
   472  
   473  // replace shifts with zero extensions
   474  (Rsh16Ux64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) -> (ZeroExt8to16  (Trunc16to8  <types.UInt8>  x))
   475  (Rsh32Ux64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (ZeroExt8to32  (Trunc32to8  <types.UInt8>  x))
   476  (Rsh64Ux64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (ZeroExt8to64  (Trunc64to8  <types.UInt8>  x))
   477  (Rsh32Ux64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (ZeroExt16to32 (Trunc32to16 <types.UInt16> x))
   478  (Rsh64Ux64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (ZeroExt16to64 (Trunc64to16 <types.UInt16> x))
   479  (Rsh64Ux64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (ZeroExt32to64 (Trunc64to32 <types.UInt32> x))
   480  
   481  // replace shifts with sign extensions
   482  (Rsh16x64 (Lsh16x64 x (Const64  [8])) (Const64  [8])) -> (SignExt8to16  (Trunc16to8  <types.Int8>  x))
   483  (Rsh32x64 (Lsh32x64 x (Const64 [24])) (Const64 [24])) -> (SignExt8to32  (Trunc32to8  <types.Int8>  x))
   484  (Rsh64x64 (Lsh64x64 x (Const64 [56])) (Const64 [56])) -> (SignExt8to64  (Trunc64to8  <types.Int8>  x))
   485  (Rsh32x64 (Lsh32x64 x (Const64 [16])) (Const64 [16])) -> (SignExt16to32 (Trunc32to16 <types.Int16> x))
   486  (Rsh64x64 (Lsh64x64 x (Const64 [48])) (Const64 [48])) -> (SignExt16to64 (Trunc64to16 <types.Int16> x))
   487  (Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) -> (SignExt32to64 (Trunc64to32 <types.Int32> x))
   488  
   489  // constant comparisons
   490  (Eq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c == d)])
   491  (Eq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c == d)])
   492  (Eq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c == d)])
   493  (Eq8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c == d)])
   494  
   495  (Neq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c != d)])
   496  (Neq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c != d)])
   497  (Neq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c != d)])
   498  (Neq8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c != d)])
   499  
   500  (Greater64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c > d)])
   501  (Greater32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c > d)])
   502  (Greater16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c > d)])
   503  (Greater8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c > d)])
   504  
   505  (Greater64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) > uint64(d))])
   506  (Greater32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) > uint32(d))])
   507  (Greater16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) > uint16(d))])
   508  (Greater8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  > uint8(d))])
   509  
   510  (Geq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c >= d)])
   511  (Geq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c >= d)])
   512  (Geq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c >= d)])
   513  (Geq8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c >= d)])
   514  
   515  (Geq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) >= uint64(d))])
   516  (Geq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) >= uint32(d))])
   517  (Geq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) >= uint16(d))])
   518  (Geq8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  >= uint8(d))])
   519  
   520  (Less64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c < d)])
   521  (Less32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c < d)])
   522  (Less16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c < d)])
   523  (Less8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c < d)])
   524  
   525  (Less64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) < uint64(d))])
   526  (Less32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) < uint32(d))])
   527  (Less16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) < uint16(d))])
   528  (Less8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  < uint8(d))])
   529  
   530  (Leq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c <= d)])
   531  (Leq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c <= d)])
   532  (Leq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c <= d)])
   533  (Leq8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c <= d)])
   534  
   535  (Leq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) <= uint64(d))])
   536  (Leq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) <= uint32(d))])
   537  (Leq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) <= uint16(d))])
   538  (Leq8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  <= uint8(d))])
   539  
   540  // simplifications
   541  (Or64 x x) -> x
   542  (Or32 x x) -> x
   543  (Or16 x x) -> x
   544  (Or8  x x) -> x
   545  (Or64 (Const64 [0]) x) -> x
   546  (Or32 (Const32 [0]) x) -> x
   547  (Or16 (Const16 [0]) x) -> x
   548  (Or8  (Const8  [0]) x) -> x
   549  (Or64 (Const64 [-1]) _) -> (Const64 [-1])
   550  (Or32 (Const32 [-1]) _) -> (Const32 [-1])
   551  (Or16 (Const16 [-1]) _) -> (Const16 [-1])
   552  (Or8  (Const8  [-1]) _) -> (Const8  [-1])
   553  (And64 x x) -> x
   554  (And32 x x) -> x
   555  (And16 x x) -> x
   556  (And8  x x) -> x
   557  (And64 (Const64 [-1]) x) -> x
   558  (And32 (Const32 [-1]) x) -> x
   559  (And16 (Const16 [-1]) x) -> x
   560  (And8  (Const8  [-1]) x) -> x
   561  (And64 (Const64 [0]) _) -> (Const64 [0])
   562  (And32 (Const32 [0]) _) -> (Const32 [0])
   563  (And16 (Const16 [0]) _) -> (Const16 [0])
   564  (And8  (Const8  [0]) _) -> (Const8  [0])
   565  (Xor64 x x) -> (Const64 [0])
   566  (Xor32 x x) -> (Const32 [0])
   567  (Xor16 x x) -> (Const16 [0])
   568  (Xor8  x x) -> (Const8  [0])
   569  (Xor64 (Const64 [0]) x) -> x
   570  (Xor32 (Const32 [0]) x) -> x
   571  (Xor16 (Const16 [0]) x) -> x
   572  (Xor8  (Const8  [0]) x) -> x
   573  (Add64 (Const64 [0]) x) -> x
   574  (Add32 (Const32 [0]) x) -> x
   575  (Add16 (Const16 [0]) x) -> x
   576  (Add8  (Const8  [0]) x) -> x
   577  (Sub64 x x) -> (Const64 [0])
   578  (Sub32 x x) -> (Const32 [0])
   579  (Sub16 x x) -> (Const16 [0])
   580  (Sub8  x x) -> (Const8  [0])
   581  (Mul64 (Const64 [0]) _) -> (Const64 [0])
   582  (Mul32 (Const32 [0]) _) -> (Const32 [0])
   583  (Mul16 (Const16 [0]) _) -> (Const16 [0])
   584  (Mul8  (Const8  [0]) _) -> (Const8  [0])
   585  (Com8  (Com8  x)) -> x
   586  (Com16 (Com16 x)) -> x
   587  (Com32 (Com32 x)) -> x
   588  (Com64 (Com64 x)) -> x
   589  (Neg8  (Sub8  x y)) -> (Sub8  y x)
   590  (Neg16 (Sub16 x y)) -> (Sub16 y x)
   591  (Neg32 (Sub32 x y)) -> (Sub32 y x)
   592  (Neg64 (Sub64 x y)) -> (Sub64 y x)
   593  (Add8  (Const8  [1]) (Com8  x)) -> (Neg8  x)
   594  (Add16 (Const16 [1]) (Com16 x)) -> (Neg16 x)
   595  (Add32 (Const32 [1]) (Com32 x)) -> (Neg32 x)
   596  (Add64 (Const64 [1]) (Com64 x)) -> (Neg64 x)
   597  
   598  (And64 x (And64 x y)) -> (And64 x y)
   599  (And32 x (And32 x y)) -> (And32 x y)
   600  (And16 x (And16 x y)) -> (And16 x y)
   601  (And8  x (And8  x y)) -> (And8  x y)
   602  (Or64 x (Or64 x y)) -> (Or64 x y)
   603  (Or32 x (Or32 x y)) -> (Or32 x y)
   604  (Or16 x (Or16 x y)) -> (Or16 x y)
   605  (Or8  x (Or8  x y)) -> (Or8  x y)
   606  (Xor64 x (Xor64 x y)) -> y
   607  (Xor32 x (Xor32 x y)) -> y
   608  (Xor16 x (Xor16 x y)) -> y
   609  (Xor8  x (Xor8  x y)) -> y
   610  
   611  (Trunc64to8  (And64 (Const64 [y]) x)) && y&0xFF == 0xFF -> (Trunc64to8 x)
   612  (Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc64to16 x)
   613  (Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF -> (Trunc64to32 x)
   614  (Trunc32to8  (And32 (Const32 [y]) x)) && y&0xFF == 0xFF -> (Trunc32to8 x)
   615  (Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc32to16 x)
   616  (Trunc16to8  (And16 (Const16 [y]) x)) && y&0xFF == 0xFF -> (Trunc16to8 x)
   617  
   618  (ZeroExt8to64  (Trunc64to8  x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 -> x
   619  (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 -> x
   620  (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 -> x
   621  (ZeroExt8to32  (Trunc32to8  x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 -> x
   622  (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 -> x
   623  (ZeroExt8to16  (Trunc16to8  x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 -> x
   624  
   625  (SignExt8to64  (Trunc64to8  x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 -> x
   626  (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 -> x
   627  (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 -> x
   628  (SignExt8to32  (Trunc32to8  x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 -> x
   629  (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 -> x
   630  (SignExt8to16  (Trunc16to8  x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 -> x
   631  
   632  (Slicemask (Const32 [x])) && x > 0 -> (Const32 [-1])
   633  (Slicemask (Const32 [0]))          -> (Const32 [0])
   634  (Slicemask (Const64 [x])) && x > 0 -> (Const64 [-1])
   635  (Slicemask (Const64 [0]))          -> (Const64 [0])
   636  
   637  // Rewrite AND of consts as shifts if possible, slightly faster for 64 bit operands
   638  // leading zeros can be shifted left, then right
   639  (And64 <t> (Const64 [y]) x) && nlz(y) + nto(y) == 64 && nto(y) >= 32
   640    -> (Rsh64Ux64 (Lsh64x64 <t> x (Const64 <t> [nlz(y)])) (Const64 <t> [nlz(y)]))
   641  // trailing zeros can be shifted right, then left
   642  (And64 <t> (Const64 [y]) x) && nlo(y) + ntz(y) == 64 && ntz(y) >= 32
   643    -> (Lsh64x64 (Rsh64Ux64 <t> x (Const64 <t> [ntz(y)])) (Const64 <t> [ntz(y)]))
   644  
   645  // simplifications often used for lengths.  e.g. len(s[i:i+5])==5
   646  (Sub64 (Add64 x y) x) -> y
   647  (Sub64 (Add64 x y) y) -> x
   648  (Sub32 (Add32 x y) x) -> y
   649  (Sub32 (Add32 x y) y) -> x
   650  (Sub16 (Add16 x y) x) -> y
   651  (Sub16 (Add16 x y) y) -> x
   652  (Sub8  (Add8  x y) x) -> y
   653  (Sub8  (Add8  x y) y) -> x
   654  
   655  // basic phi simplifications
   656  (Phi (Const8  [c]) (Const8  [c])) -> (Const8  [c])
   657  (Phi (Const16 [c]) (Const16 [c])) -> (Const16 [c])
   658  (Phi (Const32 [c]) (Const32 [c])) -> (Const32 [c])
   659  (Phi (Const64 [c]) (Const64 [c])) -> (Const64 [c])
   660  
   661  // user nil checks
   662  (NeqPtr p (ConstNil)) -> (IsNonNil p)
   663  (EqPtr p (ConstNil)) -> (Not (IsNonNil p))
   664  (IsNonNil (ConstNil)) -> (ConstBool [0])
   665  
   666  // slice and interface comparisons
   667  // The frontend ensures that we can only compare against nil,
   668  // so we need only compare the first word (interface type or slice ptr).
   669  (EqInter x y)  -> (EqPtr  (ITab x) (ITab y))
   670  (NeqInter x y) -> (NeqPtr (ITab x) (ITab y))
   671  (EqSlice x y)  -> (EqPtr  (SlicePtr x) (SlicePtr y))
   672  (NeqSlice x y) -> (NeqPtr (SlicePtr x) (SlicePtr y))
   673  
   674  // Load of store of same address, with compatibly typed value and same size
   675  (Load <t1> p1 (Store {t2} p2 x _)) && isSamePtr(p1,p2) && t1.Compare(x.Type)==CMPeq && t1.Size() == t2.(Type).Size() -> x
   676  
   677  // Collapse OffPtr
   678  (OffPtr (OffPtr p [b]) [a]) -> (OffPtr p [a+b])
   679  (OffPtr p [0]) && v.Type.Compare(p.Type) == CMPeq -> p
   680  
   681  // indexing operations
   682  // Note: bounds check has already been done
   683  (PtrIndex <t> ptr idx) && config.PtrSize == 4 -> (AddPtr ptr (Mul32 <types.Int> idx (Const32 <types.Int> [t.ElemType().Size()])))
   684  (PtrIndex <t> ptr idx) && config.PtrSize == 8 -> (AddPtr ptr (Mul64 <types.Int> idx (Const64 <types.Int> [t.ElemType().Size()])))
   685  
   686  // struct operations
   687  (StructSelect (StructMake1 x)) -> x
   688  (StructSelect [0] (StructMake2 x _)) -> x
   689  (StructSelect [1] (StructMake2 _ x)) -> x
   690  (StructSelect [0] (StructMake3 x _ _)) -> x
   691  (StructSelect [1] (StructMake3 _ x _)) -> x
   692  (StructSelect [2] (StructMake3 _ _ x)) -> x
   693  (StructSelect [0] (StructMake4 x _ _ _)) -> x
   694  (StructSelect [1] (StructMake4 _ x _ _)) -> x
   695  (StructSelect [2] (StructMake4 _ _ x _)) -> x
   696  (StructSelect [3] (StructMake4 _ _ _ x)) -> x
   697  
   698  (Load <t> _ _) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) ->
   699    (StructMake0)
   700  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) ->
   701    (StructMake1
   702      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0] ptr) mem))
   703  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) ->
   704    (StructMake2
   705      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
   706      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem))
   707  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) ->
   708    (StructMake3
   709      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
   710      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
   711      (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem))
   712  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) ->
   713    (StructMake4
   714      (Load <t.FieldType(0)> (OffPtr <t.FieldType(0).PtrTo()> [0]             ptr) mem)
   715      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
   716      (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem)
   717      (Load <t.FieldType(3)> (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] ptr) mem))
   718  
   719  (StructSelect [i] x:(Load <t> ptr mem)) && !fe.CanSSA(t) ->
   720    @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
   721  
   722  (Store _ (StructMake0) mem) -> mem
   723  (Store dst (StructMake1 <t> f0) mem) ->
   724    (Store {t.FieldType(0)} (OffPtr <t.FieldType(0).PtrTo()> [0] dst) f0 mem)
   725  (Store dst (StructMake2 <t> f0 f1) mem) ->
   726    (Store {t.FieldType(1)}
   727      (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   728      f1
   729      (Store {t.FieldType(0)}
   730        (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
   731          f0 mem))
   732  (Store dst (StructMake3 <t> f0 f1 f2) mem) ->
   733    (Store {t.FieldType(2)}
   734      (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
   735      f2
   736      (Store {t.FieldType(1)}
   737        (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   738        f1
   739        (Store {t.FieldType(0)}
   740          (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
   741            f0 mem)))
   742  (Store dst (StructMake4 <t> f0 f1 f2 f3) mem) ->
   743    (Store {t.FieldType(3)}
   744      (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] dst)
   745      f3
   746      (Store {t.FieldType(2)}
   747        (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
   748        f2
   749        (Store {t.FieldType(1)}
   750          (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   751          f1
   752          (Store {t.FieldType(0)}
   753            (OffPtr <t.FieldType(0).PtrTo()> [0] dst)
   754              f0 mem))))
   755  
   756  // Putting struct{*byte} and similar into direct interfaces.
   757  (IMake typ (StructMake1 val)) -> (IMake typ val)
   758  (StructSelect [0] x:(IData _)) -> x
   759  
   760  // un-SSAable values use mem->mem copies
   761  (Store {t} dst (Load src mem) mem) && !fe.CanSSA(t.(Type)) ->
   762  	(Move {t} [t.(Type).Size()] dst src mem)
   763  (Store {t} dst (Load src mem) (VarDef {x} mem)) && !fe.CanSSA(t.(Type)) ->
   764  	(Move {t} [t.(Type).Size()] dst src (VarDef {x} mem))
   765  
   766  // array ops
   767  (ArraySelect (ArrayMake1 x)) -> x
   768  
   769  (Load <t> _ _) && t.IsArray() && t.NumElem() == 0 ->
   770    (ArrayMake0)
   771  
   772  (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) ->
   773    (ArrayMake1 (Load <t.ElemType()> ptr mem))
   774  
   775  (Store _ (ArrayMake0) mem) -> mem
   776  (Store dst (ArrayMake1 e) mem) -> (Store {e.Type} dst e mem)
   777  
   778  (ArraySelect [0] (Load ptr mem)) -> (Load ptr mem)
   779  
   780  // Putting [1]{*byte} and similar into direct interfaces.
   781  (IMake typ (ArrayMake1 val)) -> (IMake typ val)
   782  (ArraySelect [0] x:(IData _)) -> x
   783  
   784  // string ops
   785  // Decomposing StringMake and lowering of StringPtr and StringLen
   786  // happens in a later pass, dec, so that these operations are available
   787  // to other passes for optimizations.
   788  (StringPtr (StringMake (Const64 <t> [c]) _)) -> (Const64 <t> [c])
   789  (StringLen (StringMake _ (Const64 <t> [c]))) -> (Const64 <t> [c])
   790  (ConstString {s}) && config.PtrSize == 4 && s.(string) == "" ->
   791    (StringMake (ConstNil) (Const32 <types.Int> [0]))
   792  (ConstString {s}) && config.PtrSize == 8 && s.(string) == "" ->
   793    (StringMake (ConstNil) (Const64 <types.Int> [0]))
   794  (ConstString {s}) && config.PtrSize == 4 && s.(string) != "" ->
   795    (StringMake
   796      (Addr <types.BytePtr> {fe.StringData(s.(string))}
   797        (SB))
   798      (Const32 <types.Int> [int64(len(s.(string)))]))
   799  (ConstString {s}) && config.PtrSize == 8 && s.(string) != "" ->
   800    (StringMake
   801      (Addr <types.BytePtr> {fe.StringData(s.(string))}
   802        (SB))
   803      (Const64 <types.Int> [int64(len(s.(string)))]))
   804  
   805  // slice ops
   806  // Only a few slice rules are provided here.  See dec.rules for
   807  // a more comprehensive set.
   808  (SliceLen (SliceMake _ (Const64 <t> [c]) _)) -> (Const64 <t> [c])
   809  (SliceCap (SliceMake _ _ (Const64 <t> [c]))) -> (Const64 <t> [c])
   810  (SliceLen (SliceMake _ (Const32 <t> [c]) _)) -> (Const32 <t> [c])
   811  (SliceCap (SliceMake _ _ (Const32 <t> [c]))) -> (Const32 <t> [c])
   812  (SlicePtr (SliceMake (SlicePtr x) _ _)) -> (SlicePtr x)
   813  (SliceLen (SliceMake _ (SliceLen x) _)) -> (SliceLen x)
   814  (SliceCap (SliceMake _ _ (SliceCap x))) -> (SliceCap x)
   815  (SliceCap (SliceMake _ _ (SliceLen x))) -> (SliceLen x)
   816  (ConstSlice) && config.PtrSize == 4 ->
   817    (SliceMake
   818      (ConstNil <v.Type.ElemType().PtrTo()>)
   819      (Const32 <types.Int> [0])
   820      (Const32 <types.Int> [0]))
   821  (ConstSlice) && config.PtrSize == 8 ->
   822    (SliceMake
   823      (ConstNil <v.Type.ElemType().PtrTo()>)
   824      (Const64 <types.Int> [0])
   825      (Const64 <types.Int> [0]))
   826  
   827  // interface ops
   828  (ConstInterface) ->
   829    (IMake
   830      (ConstNil <types.BytePtr>)
   831      (ConstNil <types.BytePtr>))
   832  
   833  (NilCheck (GetG mem) mem) -> mem
   834  
   835  (If (Not cond) yes no) -> (If cond no yes)
   836  (If (ConstBool [c]) yes no) && c == 1 -> (First nil yes no)
   837  (If (ConstBool [c]) yes no) && c == 0 -> (First nil no yes)
   838  
   839  // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
   840  (Convert (Add64 (Convert ptr mem) off) mem) -> (Add64 ptr off)
   841  (Convert (Convert ptr mem) mem) -> ptr
   842  
   843  // Decompose compound argument values
   844  (Arg {n} [off]) && v.Type.IsString() ->
   845    (StringMake
   846      (Arg <types.BytePtr> {n} [off])
   847      (Arg <types.Int> {n} [off+config.PtrSize]))
   848  
   849  (Arg {n} [off]) && v.Type.IsSlice() ->
   850    (SliceMake
   851      (Arg <v.Type.ElemType().PtrTo()> {n} [off])
   852      (Arg <types.Int> {n} [off+config.PtrSize])
   853      (Arg <types.Int> {n} [off+2*config.PtrSize]))
   854  
   855  (Arg {n} [off]) && v.Type.IsInterface() ->
   856    (IMake
   857      (Arg <types.BytePtr> {n} [off])
   858      (Arg <types.BytePtr> {n} [off+config.PtrSize]))
   859  
   860  (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 16 ->
   861    (ComplexMake
   862      (Arg <types.Float64> {n} [off])
   863      (Arg <types.Float64> {n} [off+8]))
   864  
   865  (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 8 ->
   866    (ComplexMake
   867      (Arg <types.Float32> {n} [off])
   868      (Arg <types.Float32> {n} [off+4]))
   869  
   870  (Arg <t>) && t.IsStruct() && t.NumFields() == 0 && fe.CanSSA(t) ->
   871    (StructMake0)
   872  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 1 && fe.CanSSA(t) ->
   873    (StructMake1
   874      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]))
   875  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 2 && fe.CanSSA(t) ->
   876    (StructMake2
   877      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
   878      (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)]))
   879  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 3 && fe.CanSSA(t) ->
   880    (StructMake3
   881      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
   882      (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])
   883      (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)]))
   884  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 4 && fe.CanSSA(t) ->
   885    (StructMake4
   886      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
   887      (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])
   888      (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)])
   889      (Arg <t.FieldType(3)> {n} [off+t.FieldOff(3)]))
   890  
   891  (Arg <t>) && t.IsArray() && t.NumElem() == 0 ->
   892    (ArrayMake0)
   893  (Arg <t> {n} [off]) && t.IsArray() && t.NumElem() == 1 && fe.CanSSA(t) ->
   894    (ArrayMake1 (Arg <t.ElemType()> {n} [off]))
   895  
   896  // strength reduction of divide by a constant.
   897  // See ../magic.go for a detailed description of these algorithms.
   898  
   899  // Unsigned divide by power of 2.  Strength reduce to a shift.
   900  (Div8u  n (Const8  [c])) && isPowerOfTwo(c&0xff)       -> (Rsh8Ux64 n  (Const64 <types.UInt64> [log2(c&0xff)]))
   901  (Div16u n (Const16 [c])) && isPowerOfTwo(c&0xffff)     -> (Rsh16Ux64 n (Const64 <types.UInt64> [log2(c&0xffff)]))
   902  (Div32u n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (Rsh32Ux64 n (Const64 <types.UInt64> [log2(c&0xffffffff)]))
   903  (Div64u n (Const64 [c])) && isPowerOfTwo(c)            -> (Rsh64Ux64 n (Const64 <types.UInt64> [log2(c)]))
   904  
   905  // Unsigned divide, not a power of 2.  Strength reduce to a multiply.
   906  // For 8-bit divides, we just do a direct 9-bit by 8-bit multiply.
   907  (Div8u x (Const8 [c])) && umagicOK(8, c) ->
   908    (Trunc32to8
   909      (Rsh32Ux64 <types.UInt32>
   910        (Mul32 <types.UInt32>
   911          (Const32 <types.UInt32> [int64(1<<8+umagic(8,c).m)])
   912          (ZeroExt8to32 x))
   913        (Const64 <types.UInt64> [8+umagic(8,c).s])))
   914  
   915  // For 16-bit divides on 64-bit machines, we do a direct 17-bit by 16-bit multiply.
   916  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 8 ->
   917    (Trunc64to16
   918      (Rsh64Ux64 <types.UInt64>
   919        (Mul64 <types.UInt64>
   920          (Const64 <types.UInt64> [int64(1<<16+umagic(16,c).m)])
   921          (ZeroExt16to64 x))
   922        (Const64 <types.UInt64> [16+umagic(16,c).s])))
   923  
   924  // For 16-bit divides on 32-bit machines
   925  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && umagic(16,c).m&1 == 0 ->
   926    (Trunc32to16
   927      (Rsh32Ux64 <types.UInt32>
   928        (Mul32 <types.UInt32>
   929          (Const32 <types.UInt32> [int64(1<<15+umagic(16,c).m/2)])
   930          (ZeroExt16to32 x))
   931        (Const64 <types.UInt64> [16+umagic(16,c).s-1])))
   932  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && c&1 == 0 ->
   933    (Trunc32to16
   934      (Rsh32Ux64 <types.UInt32>
   935        (Mul32 <types.UInt32>
   936          (Const32 <types.UInt32> [int64(1<<15+(umagic(16,c).m+1)/2)])
   937          (Rsh32Ux64 <types.UInt32> (ZeroExt16to32 x) (Const64 <types.UInt64> [1])))
   938        (Const64 <types.UInt64> [16+umagic(16,c).s-2])))
   939  (Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 ->
   940    (Trunc32to16
   941      (Rsh32Ux64 <types.UInt32>
   942        (Avg32u
   943          (Lsh32x64 <types.UInt32> (ZeroExt16to32 x) (Const64 <types.UInt64> [16]))
   944          (Mul32 <types.UInt32>
   945            (Const32 <types.UInt32> [int64(umagic(16,c).m)])
   946            (ZeroExt16to32 x)))
   947        (Const64 <types.UInt64> [16+umagic(16,c).s-1])))
   948  
   949  // For 32-bit divides on 32-bit machines
   950  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && umagic(32,c).m&1 == 0 ->
   951    (Rsh32Ux64 <types.UInt32>
   952      (Hmul32u <types.UInt32>
   953        (Const32 <types.UInt32> [int64(int32(1<<31+umagic(32,c).m/2))])
   954        x)
   955      (Const64 <types.UInt64> [umagic(32,c).s-1]))
   956  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && c&1 == 0 ->
   957    (Rsh32Ux64 <types.UInt32>
   958      (Hmul32u <types.UInt32>
   959        (Const32 <types.UInt32> [int64(int32(1<<31+(umagic(32,c).m+1)/2))])
   960        (Rsh32Ux64 <types.UInt32> x (Const64 <types.UInt64> [1])))
   961      (Const64 <types.UInt64> [umagic(32,c).s-2]))
   962  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 ->
   963    (Rsh32Ux64 <types.UInt32>
   964      (Avg32u
   965        x
   966        (Hmul32u <types.UInt32>
   967          (Const32 <types.UInt32> [int64(int32(umagic(32,c).m))])
   968          x))
   969      (Const64 <types.UInt64> [umagic(32,c).s-1]))
   970  
   971  // For 32-bit divides on 64-bit machines
   972  // We'll use a regular (non-hi) multiply for this case.
   973  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && umagic(32,c).m&1 == 0 ->
   974    (Trunc64to32
   975      (Rsh64Ux64 <types.UInt64>
   976        (Mul64 <types.UInt64>
   977          (Const64 <types.UInt64> [int64(1<<31+umagic(32,c).m/2)])
   978          (ZeroExt32to64 x))
   979        (Const64 <types.UInt64> [32+umagic(32,c).s-1])))
   980  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && c&1 == 0 ->
   981    (Trunc64to32
   982      (Rsh64Ux64 <types.UInt64>
   983        (Mul64 <types.UInt64>
   984          (Const64 <types.UInt64> [int64(1<<31+(umagic(32,c).m+1)/2)])
   985          (Rsh64Ux64 <types.UInt64> (ZeroExt32to64 x) (Const64 <types.UInt64> [1])))
   986        (Const64 <types.UInt64> [32+umagic(32,c).s-2])))
   987  (Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 ->
   988    (Trunc64to32
   989      (Rsh64Ux64 <types.UInt64>
   990        (Avg64u
   991          (Lsh64x64 <types.UInt64> (ZeroExt32to64 x) (Const64 <types.UInt64> [32]))
   992          (Mul64 <types.UInt64>
   993            (Const64 <types.UInt32> [int64(umagic(32,c).m)])
   994            (ZeroExt32to64 x)))
   995        (Const64 <types.UInt64> [32+umagic(32,c).s-1])))
   996  
   997  // For 64-bit divides on 64-bit machines
   998  // (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.)
   999  (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && umagic(64,c).m&1 == 0 ->
  1000    (Rsh64Ux64 <types.UInt64>
  1001      (Hmul64u <types.UInt64>
  1002        (Const64 <types.UInt64> [int64(1<<63+umagic(64,c).m/2)])
  1003        x)
  1004      (Const64 <types.UInt64> [umagic(64,c).s-1]))
  1005  (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && c&1 == 0 ->
  1006    (Rsh64Ux64 <types.UInt64>
  1007      (Hmul64u <types.UInt64>
  1008        (Const64 <types.UInt64> [int64(1<<63+(umagic(64,c).m+1)/2)])
  1009        (Rsh64Ux64 <types.UInt64> x (Const64 <types.UInt64> [1])))
  1010      (Const64 <types.UInt64> [umagic(64,c).s-2]))
  1011  (Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 ->
  1012    (Rsh64Ux64 <types.UInt64>
  1013      (Avg64u
  1014        x
  1015        (Hmul64u <types.UInt64>
  1016          (Const64 <types.UInt64> [int64(umagic(64,c).m)])
  1017          x))
  1018      (Const64 <types.UInt64> [umagic(64,c).s-1]))
  1019  
  1020  // Signed divide by a negative constant.  Rewrite to divide by a positive constant.
  1021  (Div8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  -> (Neg8  (Div8  <t> n (Const8  <t> [-c])))
  1022  (Div16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Neg16 (Div16 <t> n (Const16 <t> [-c])))
  1023  (Div32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Neg32 (Div32 <t> n (Const32 <t> [-c])))
  1024  (Div64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Neg64 (Div64 <t> n (Const64 <t> [-c])))
  1025  
  1026  // Dividing by the most-negative number.  Result is always 0 except
  1027  // if the input is also the most-negative number.
  1028  // We can detect that using the sign bit of x & -x.
  1029  (Div8  <t> x (Const8  [-1<<7 ])) -> (Rsh8Ux64  (And8  <t> x (Neg8  <t> x)) (Const64 <types.UInt64> [7 ]))
  1030  (Div16 <t> x (Const16 [-1<<15])) -> (Rsh16Ux64 (And16 <t> x (Neg16 <t> x)) (Const64 <types.UInt64> [15]))
  1031  (Div32 <t> x (Const32 [-1<<31])) -> (Rsh32Ux64 (And32 <t> x (Neg32 <t> x)) (Const64 <types.UInt64> [31]))
  1032  (Div64 <t> x (Const64 [-1<<63])) -> (Rsh64Ux64 (And64 <t> x (Neg64 <t> x)) (Const64 <types.UInt64> [63]))
  1033  
  1034  // Signed divide by power of 2.
  1035  // n / c =       n >> log(c) if n >= 0
  1036  //       = (n+c-1) >> log(c) if n < 0
  1037  // We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned).
  1038  (Div8  <t> n (Const8  [c])) && isPowerOfTwo(c) ->
  1039    (Rsh8x64
  1040      (Add8  <t> n (Rsh8Ux64  <t> (Rsh8x64  <t> n (Const64 <types.UInt64> [ 7])) (Const64 <types.UInt64> [ 8-log2(c)])))
  1041      (Const64 <types.UInt64> [log2(c)]))
  1042  (Div16 <t> n (Const16 [c])) && isPowerOfTwo(c) ->
  1043    (Rsh16x64
  1044      (Add16 <t> n (Rsh16Ux64 <t> (Rsh16x64 <t> n (Const64 <types.UInt64> [15])) (Const64 <types.UInt64> [16-log2(c)])))
  1045      (Const64 <types.UInt64> [log2(c)]))
  1046  (Div32 <t> n (Const32 [c])) && isPowerOfTwo(c) ->
  1047    (Rsh32x64
  1048      (Add32 <t> n (Rsh32Ux64 <t> (Rsh32x64 <t> n (Const64 <types.UInt64> [31])) (Const64 <types.UInt64> [32-log2(c)])))
  1049      (Const64 <types.UInt64> [log2(c)]))
  1050  (Div64 <t> n (Const64 [c])) && isPowerOfTwo(c) ->
  1051    (Rsh64x64
  1052      (Add64 <t> n (Rsh64Ux64 <t> (Rsh64x64 <t> n (Const64 <types.UInt64> [63])) (Const64 <types.UInt64> [64-log2(c)])))
  1053      (Const64 <types.UInt64> [log2(c)]))
  1054  
  1055  // Signed divide, not a power of 2.  Strength reduce to a multiply.
  1056  (Div8 <t> x (Const8 [c])) && smagicOK(8,c) ->
  1057    (Sub8 <t>
  1058      (Rsh32x64 <t>
  1059        (Mul32 <types.UInt32>
  1060          (Const32 <types.UInt32> [int64(smagic(8,c).m)])
  1061          (SignExt8to32 x))
  1062        (Const64 <types.UInt64> [8+smagic(8,c).s]))
  1063      (Rsh32x64 <t>
  1064        (SignExt8to32 x)
  1065        (Const64 <types.UInt64> [31])))
  1066  (Div16 <t> x (Const16 [c])) && smagicOK(16,c) ->
  1067    (Sub16 <t>
  1068      (Rsh32x64 <t>
  1069        (Mul32 <types.UInt32>
  1070          (Const32 <types.UInt32> [int64(smagic(16,c).m)])
  1071          (SignExt16to32 x))
  1072        (Const64 <types.UInt64> [16+smagic(16,c).s]))
  1073      (Rsh32x64 <t>
  1074        (SignExt16to32 x)
  1075        (Const64 <types.UInt64> [31])))
  1076  (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 8 ->
  1077    (Sub32 <t>
  1078      (Rsh64x64 <t>
  1079        (Mul64 <types.UInt64>
  1080          (Const64 <types.UInt64> [int64(smagic(32,c).m)])
  1081          (SignExt32to64 x))
  1082        (Const64 <types.UInt64> [32+smagic(32,c).s]))
  1083      (Rsh64x64 <t>
  1084        (SignExt32to64 x)
  1085        (Const64 <types.UInt64> [63])))
  1086  (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 == 0 ->
  1087    (Sub32 <t>
  1088      (Rsh32x64 <t>
  1089        (Hmul32 <t>
  1090          (Const32 <types.UInt32> [int64(int32(smagic(32,c).m/2))])
  1091          x)
  1092        (Const64 <types.UInt64> [smagic(32,c).s-1]))
  1093      (Rsh32x64 <t>
  1094        x
  1095        (Const64 <types.UInt64> [31])))
  1096  (Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 != 0 ->
  1097    (Sub32 <t>
  1098      (Rsh32x64 <t>
  1099        (Add32 <t>
  1100          (Hmul32 <t>
  1101            (Const32 <types.UInt32> [int64(int32(smagic(32,c).m))])
  1102            x)
  1103          x)
  1104        (Const64 <types.UInt64> [smagic(32,c).s]))
  1105      (Rsh32x64 <t>
  1106        x
  1107        (Const64 <types.UInt64> [31])))
  1108  (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 == 0 ->
  1109    (Sub64 <t>
  1110      (Rsh64x64 <t>
  1111        (Hmul64 <t>
  1112          (Const64 <types.UInt64> [int64(smagic(64,c).m/2)])
  1113          x)
  1114        (Const64 <types.UInt64> [smagic(64,c).s-1]))
  1115      (Rsh64x64 <t>
  1116        x
  1117        (Const64 <types.UInt64> [63])))
  1118  (Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 != 0 ->
  1119    (Sub64 <t>
  1120      (Rsh64x64 <t>
  1121        (Add64 <t>
  1122          (Hmul64 <t>
  1123            (Const64 <types.UInt64> [int64(smagic(64,c).m)])
  1124            x)
  1125          x)
  1126        (Const64 <types.UInt64> [smagic(64,c).s]))
  1127      (Rsh64x64 <t>
  1128        x
  1129        (Const64 <types.UInt64> [63])))
  1130  
  1131  // Unsigned mod by power of 2 constant.
  1132  (Mod8u  <t> n (Const8  [c])) && isPowerOfTwo(c&0xff)       -> (And8 n (Const8 <t> [(c&0xff)-1]))
  1133  (Mod16u <t> n (Const16 [c])) && isPowerOfTwo(c&0xffff)     -> (And16 n (Const16 <t> [(c&0xffff)-1]))
  1134  (Mod32u <t> n (Const32 [c])) && isPowerOfTwo(c&0xffffffff) -> (And32 n (Const32 <t> [(c&0xffffffff)-1]))
  1135  (Mod64u <t> n (Const64 [c])) && isPowerOfTwo(c)            -> (And64 n (Const64 <t> [c-1]))
  1136  
  1137  // Signed mod by negative constant.
  1138  (Mod8  <t> n (Const8  [c])) && c < 0 && c != -1<<7  -> (Mod8  <t> n (Const8  <t> [-c]))
  1139  (Mod16 <t> n (Const16 [c])) && c < 0 && c != -1<<15 -> (Mod16 <t> n (Const16 <t> [-c]))
  1140  (Mod32 <t> n (Const32 [c])) && c < 0 && c != -1<<31 -> (Mod32 <t> n (Const32 <t> [-c]))
  1141  (Mod64 <t> n (Const64 [c])) && c < 0 && c != -1<<63 -> (Mod64 <t> n (Const64 <t> [-c]))
  1142  
  1143  // All other mods by constants, do A%B = A-(A/B*B).
  1144  // This implements % with two * and a bunch of ancillary ops.
  1145  // One of the * is free if the user's code also computes A/B.
  1146  (Mod8   <t> x (Const8  [c])) && x.Op != OpConst8  && (c > 0 || c == -1<<7)
  1147    -> (Sub8  x (Mul8  <t> (Div8   <t> x (Const8  <t> [c])) (Const8  <t> [c])))
  1148  (Mod16  <t> x (Const16 [c])) && x.Op != OpConst16 && (c > 0 || c == -1<<15)
  1149    -> (Sub16 x (Mul16 <t> (Div16  <t> x (Const16 <t> [c])) (Const16 <t> [c])))
  1150  (Mod32  <t> x (Const32 [c])) && x.Op != OpConst32 && (c > 0 || c == -1<<31)
  1151    -> (Sub32 x (Mul32 <t> (Div32  <t> x (Const32 <t> [c])) (Const32 <t> [c])))
  1152  (Mod64  <t> x (Const64 [c])) && x.Op != OpConst64 && (c > 0 || c == -1<<63)
  1153    -> (Sub64 x (Mul64 <t> (Div64  <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1154  (Mod8u  <t> x (Const8  [c])) && x.Op != OpConst8  && c > 0 && umagicOK(8 ,c)
  1155    -> (Sub8  x (Mul8  <t> (Div8u  <t> x (Const8  <t> [c])) (Const8  <t> [c])))
  1156  (Mod16u <t> x (Const16 [c])) && x.Op != OpConst16 && c > 0 && umagicOK(16,c)
  1157    -> (Sub16 x (Mul16 <t> (Div16u <t> x (Const16 <t> [c])) (Const16 <t> [c])))
  1158  (Mod32u <t> x (Const32 [c])) && x.Op != OpConst32 && c > 0 && umagicOK(32,c)
  1159    -> (Sub32 x (Mul32 <t> (Div32u <t> x (Const32 <t> [c])) (Const32 <t> [c])))
  1160  (Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && c > 0 && umagicOK(64,c)
  1161    -> (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1162  
  1163  // Reassociate expressions involving
  1164  // constants such that constants come first,
  1165  // exposing obvious constant-folding opportunities.
  1166  // Reassociate (op (op y C) x) to (op C (op x y)) or similar, where C
  1167  // is constant, which pushes constants to the outside
  1168  // of the expression. At that point, any constant-folding
  1169  // opportunities should be obvious.
  1170  
  1171  // x + (C + z) -> C + (x + z)
  1172  (Add64 (Add64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Add64 <t> z x))
  1173  (Add32 (Add32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Add32 <t> z x))
  1174  (Add16 (Add16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Add16 <t> z x))
  1175  (Add8  (Add8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Add8  <t> z x))
  1176  
  1177  // x + (C - z) -> C + (x - z)
  1178  (Add64 (Sub64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
  1179  (Add32 (Sub32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
  1180  (Add16 (Sub16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
  1181  (Add8  (Sub8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))
  1182  (Add64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
  1183  (Add32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
  1184  (Add16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
  1185  (Add8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))
  1186  
  1187  // x + (z - C) -> (x + z) - C
  1188  (Add64 (Sub64 z i:(Const64 <t>)) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
  1189  (Add32 (Sub32 z i:(Const32 <t>)) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
  1190  (Add16 (Sub16 z i:(Const16 <t>)) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
  1191  (Add8  (Sub8  z i:(Const8  <t>)) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)
  1192  (Add64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
  1193  (Add32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
  1194  (Add16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
  1195  (Add8  x (Sub8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)
  1196  
  1197  // x - (C - z) -> x + (z - C) -> (x + z) - C
  1198  (Sub64 x (Sub64 i:(Const64 <t>) z)) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Sub64 (Add64 <t> x z) i)
  1199  (Sub32 x (Sub32 i:(Const32 <t>) z)) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Sub32 (Add32 <t> x z) i)
  1200  (Sub16 x (Sub16 i:(Const16 <t>) z)) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Sub16 (Add16 <t> x z) i)
  1201  (Sub8  x (Sub8  i:(Const8  <t>) z)) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Sub8  (Add8  <t> x z) i)
  1202  
  1203  // x - (z - C) -> x + (C - z) -> (x - z) + C
  1204  (Sub64 x (Sub64 z i:(Const64 <t>))) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Add64 i (Sub64 <t> x z))
  1205  (Sub32 x (Sub32 z i:(Const32 <t>))) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Add32 i (Sub32 <t> x z))
  1206  (Sub16 x (Sub16 z i:(Const16 <t>))) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Add16 i (Sub16 <t> x z))
  1207  (Sub8  x (Sub8  z i:(Const8  <t>))) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Add8  i (Sub8  <t> x z))
  1208  
  1209  // x & (C & z) -> C & (x & z)
  1210  (And64 (And64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (And64 i (And64 <t> z x))
  1211  (And32 (And32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (And32 i (And32 <t> z x))
  1212  (And16 (And16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (And16 i (And16 <t> z x))
  1213  (And8  (And8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (And8  i (And8  <t> z x))
  1214  
  1215  // x | (C | z) -> C | (x | z)
  1216  (Or64 (Or64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Or64 i (Or64 <t> z x))
  1217  (Or32 (Or32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Or32 i (Or32 <t> z x))
  1218  (Or16 (Or16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Or16 i (Or16 <t> z x))
  1219  (Or8  (Or8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Or8  i (Or8  <t> z x))
  1220  
  1221  // x ^ (C ^ z) -> C ^ (x ^ z)
  1222  (Xor64 (Xor64 i:(Const64 <t>) z) x) && (z.Op != OpConst64 && x.Op != OpConst64) -> (Xor64 i (Xor64 <t> z x))
  1223  (Xor32 (Xor32 i:(Const32 <t>) z) x) && (z.Op != OpConst32 && x.Op != OpConst32) -> (Xor32 i (Xor32 <t> z x))
  1224  (Xor16 (Xor16 i:(Const16 <t>) z) x) && (z.Op != OpConst16 && x.Op != OpConst16) -> (Xor16 i (Xor16 <t> z x))
  1225  (Xor8  (Xor8  i:(Const8  <t>) z) x) && (z.Op != OpConst8  && x.Op != OpConst8)  -> (Xor8  i (Xor8  <t> z x))
  1226  
  1227  // C + (D + x) -> (C + D) + x
  1228  (Add64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c+d]) x)
  1229  (Add32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c+d))]) x)
  1230  (Add16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c+d))]) x)
  1231  (Add8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Add8  (Const8  <t> [int64(int8(c+d))]) x)
  1232  
  1233  // C + (D - x) -> (C + D) - x
  1234  (Add64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Sub64 (Const64 <t> [c+d]) x)
  1235  (Add32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x)
  1236  (Add16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x)
  1237  (Add8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) -> (Sub8  (Const8  <t> [int64(int8(c+d))]) x)
  1238  
  1239  // C + (x - D) -> (C - D) + x
  1240  (Add64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Add64 (Const64 <t> [c-d]) x)
  1241  (Add32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x)
  1242  (Add16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x)
  1243  (Add8  (Const8  <t> [c]) (Sub8  x (Const8  <t> [d]))) -> (Add8  (Const8  <t> [int64(int8(c-d))]) x)
  1244  
  1245  // C - (x - D) -> (C + D) - x
  1246  (Sub64 (Const64 <t> [c]) (Sub64 x (Const64 <t> [d]))) -> (Sub64 (Const64 <t> [c+d]) x)
  1247  (Sub32 (Const32 <t> [c]) (Sub32 x (Const32 <t> [d]))) -> (Sub32 (Const32 <t> [int64(int32(c+d))]) x)
  1248  (Sub16 (Const16 <t> [c]) (Sub16 x (Const16 <t> [d]))) -> (Sub16 (Const16 <t> [int64(int16(c+d))]) x)
  1249  (Sub8  (Const8  <t> [c]) (Sub8  x (Const8  <t> [d]))) -> (Sub8  (Const8  <t> [int64(int8(c+d))]) x)
  1250  
  1251  // C - (D - x) -> (C - D) + x
  1252  (Sub64 (Const64 <t> [c]) (Sub64 (Const64 <t> [d]) x)) -> (Add64 (Const64 <t> [c-d]) x)
  1253  (Sub32 (Const32 <t> [c]) (Sub32 (Const32 <t> [d]) x)) -> (Add32 (Const32 <t> [int64(int32(c-d))]) x)
  1254  (Sub16 (Const16 <t> [c]) (Sub16 (Const16 <t> [d]) x)) -> (Add16 (Const16 <t> [int64(int16(c-d))]) x)
  1255  (Sub8  (Const8  <t> [c]) (Sub8  (Const8  <t> [d]) x)) -> (Add8  (Const8  <t> [int64(int8(c-d))]) x)
  1256  
  1257  // C & (D & x) -> (C & D) & x
  1258  (And64 (Const64 <t> [c]) (And64 (Const64 <t> [d]) x)) -> (And64 (Const64 <t> [c&d]) x)
  1259  (And32 (Const32 <t> [c]) (And32 (Const32 <t> [d]) x)) -> (And32 (Const32 <t> [int64(int32(c&d))]) x)
  1260  (And16 (Const16 <t> [c]) (And16 (Const16 <t> [d]) x)) -> (And16 (Const16 <t> [int64(int16(c&d))]) x)
  1261  (And8  (Const8  <t> [c]) (And8  (Const8  <t> [d]) x)) -> (And8  (Const8  <t> [int64(int8(c&d))]) x)
  1262  
  1263  // C | (D | x) -> (C | D) | x
  1264  (Or64 (Const64 <t> [c]) (Or64 (Const64 <t> [d]) x)) -> (Or64 (Const64 <t> [c|d]) x)
  1265  (Or32 (Const32 <t> [c]) (Or32 (Const32 <t> [d]) x)) -> (Or32 (Const32 <t> [int64(int32(c|d))]) x)
  1266  (Or16 (Const16 <t> [c]) (Or16 (Const16 <t> [d]) x)) -> (Or16 (Const16 <t> [int64(int16(c|d))]) x)
  1267  (Or8  (Const8  <t> [c]) (Or8  (Const8  <t> [d]) x)) -> (Or8  (Const8  <t> [int64(int8(c|d))]) x)
  1268  
  1269  // C ^ (D ^ x) -> (C ^ D) ^ x
  1270  (Xor64 (Const64 <t> [c]) (Xor64 (Const64 <t> [d]) x)) -> (Xor64 (Const64 <t> [c^d]) x)
  1271  (Xor32 (Const32 <t> [c]) (Xor32 (Const32 <t> [d]) x)) -> (Xor32 (Const32 <t> [int64(int32(c^d))]) x)
  1272  (Xor16 (Const16 <t> [c]) (Xor16 (Const16 <t> [d]) x)) -> (Xor16 (Const16 <t> [int64(int16(c^d))]) x)
  1273  (Xor8  (Const8  <t> [c]) (Xor8  (Const8  <t> [d]) x)) -> (Xor8  (Const8  <t> [int64(int8(c^d))]) x)
  1274  
  1275  // C * (D * x) = (C * D) * x
  1276  (Mul64 (Const64 <t> [c]) (Mul64 (Const64 <t> [d]) x)) -> (Mul64 (Const64 <t> [c*d]) x)
  1277  (Mul32 (Const32 <t> [c]) (Mul32 (Const32 <t> [d]) x)) -> (Mul32 (Const32 <t> [int64(int32(c*d))]) x)
  1278  (Mul16 (Const16 <t> [c]) (Mul16 (Const16 <t> [d]) x)) -> (Mul16 (Const16 <t> [int64(int16(c*d))]) x)
  1279  (Mul8  (Const8  <t> [c]) (Mul8  (Const8  <t> [d]) x)) -> (Mul8  (Const8  <t> [int64(int8(c*d))]) x)
  1280  
  1281  // floating point optimizations
  1282  (Add32F x (Const32F [0])) -> x
  1283  (Add64F x (Const64F [0])) -> x
  1284  (Sub32F x (Const32F [0])) -> x
  1285  (Sub64F x (Const64F [0])) -> x
  1286  (Mul32F x (Const32F [f2i(1)])) -> x
  1287  (Mul64F x (Const64F [f2i(1)])) -> x
  1288  (Mul32F x (Const32F [f2i(-1)])) -> (Neg32F x)
  1289  (Mul64F x (Const64F [f2i(-1)])) -> (Neg64F x)
  1290  (Mul32F x (Const32F [f2i(2)])) -> (Add32F x x)
  1291  (Mul64F x (Const64F [f2i(2)])) -> (Add64F x x)
  1292  (Div32F x (Const32F <t> [c])) && reciprocalExact32(float32(i2f(c))) -> (Mul32F x (Const32F <t> [f2i(1/i2f(c))]))
  1293  (Div64F x (Const64F <t> [c])) && reciprocalExact64(i2f(c))          -> (Mul64F x (Const64F <t> [f2i(1/i2f(c))]))
  1294  
  1295  (Sqrt (Const64F [c])) -> (Const64F [f2i(math.Sqrt(i2f(c)))])
  1296  
  1297  // recognize runtime.newobject and don't Zero/Nilcheck it
  1298  (Zero (Load (OffPtr [c] (SP)) mem) mem)
  1299  	&& mem.Op == OpStaticCall
  1300  	&& isSameSym(mem.Aux, "runtime.newobject")
  1301  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1302  	-> mem
  1303  (Store (Load (OffPtr [c] (SP)) mem) x mem)
  1304  	&& isConstZero(x)
  1305  	&& mem.Op == OpStaticCall
  1306  	&& isSameSym(mem.Aux, "runtime.newobject")
  1307  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1308  	-> mem
  1309  (Store (OffPtr (Load (OffPtr [c] (SP)) mem)) x mem)
  1310  	&& isConstZero(x)
  1311  	&& mem.Op == OpStaticCall
  1312  	&& isSameSym(mem.Aux, "runtime.newobject")
  1313  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1314  	-> mem
  1315  // nil checks just need to rewrite to something useless.
  1316  // they will be deadcode eliminated soon afterwards.
  1317  (NilCheck (Load (OffPtr [c] (SP)) mem) mem)
  1318  	&& mem.Op == OpStaticCall
  1319  	&& isSameSym(mem.Aux, "runtime.newobject")
  1320  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1321  	&& warnRule(fe.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check")
  1322  	-> (Invalid)
  1323  (NilCheck (OffPtr (Load (OffPtr [c] (SP)) mem)) mem)
  1324  	&& mem.Op == OpStaticCall
  1325  	&& isSameSym(mem.Aux, "runtime.newobject")
  1326  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1327  	&& warnRule(fe.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check")
  1328  	-> (Invalid)
  1329  
  1330  // Address comparison shows up in type assertions.
  1331  (EqPtr x x) -> (ConstBool [1])
  1332  (EqPtr (Addr {a} x) (Addr {b} x)) -> (ConstBool [b2i(a == b)])
  1333  
  1334  // De-virtualize interface calls into static calls.
  1335  // Note that (ITab (IMake)) doesn't get
  1336  // rewritten until after the first opt pass,
  1337  // so this rule should trigger reliably.
  1338  (InterCall [argsize] (Load (OffPtr [off] (ITab (IMake (Addr {itab} (SB)) _))) _) mem) && devirt(v, itab, off) != nil ->
  1339  	(StaticCall [argsize] {devirt(v, itab, off)} mem)