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