github.com/rakyll/go@v0.0.0-20170216000551-64c02460d703/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  
    50  (Trunc16to8  (ZeroExt8to16  x)) -> x
    51  (Trunc32to8  (ZeroExt8to32  x)) -> x
    52  (Trunc32to16 (ZeroExt8to32  x)) -> (ZeroExt8to16  x)
    53  (Trunc32to16 (ZeroExt16to32 x)) -> x
    54  (Trunc64to8  (ZeroExt8to64  x)) -> x
    55  (Trunc64to16 (ZeroExt8to64  x)) -> (ZeroExt8to16  x)
    56  (Trunc64to16 (ZeroExt16to64 x)) -> x
    57  (Trunc64to32 (ZeroExt8to64  x)) -> (ZeroExt8to32  x)
    58  (Trunc64to32 (ZeroExt16to64 x)) -> (ZeroExt16to32 x)
    59  (Trunc64to32 (ZeroExt32to64 x)) -> x
    60  (Trunc16to8  (SignExt8to16  x)) -> x
    61  (Trunc32to8  (SignExt8to32  x)) -> x
    62  (Trunc32to16 (SignExt8to32  x)) -> (SignExt8to16  x)
    63  (Trunc32to16 (SignExt16to32 x)) -> x
    64  (Trunc64to8  (SignExt8to64  x)) -> x
    65  (Trunc64to16 (SignExt8to64  x)) -> (SignExt8to16  x)
    66  (Trunc64to16 (SignExt16to64 x)) -> x
    67  (Trunc64to32 (SignExt8to64  x)) -> (SignExt8to32  x)
    68  (Trunc64to32 (SignExt16to64 x)) -> (SignExt16to32 x)
    69  (Trunc64to32 (SignExt32to64 x)) -> x
    70  
    71  (ZeroExt8to16  (Const8  [c])) -> (Const16 [int64( uint8(c))])
    72  (ZeroExt8to32  (Const8  [c])) -> (Const32 [int64( uint8(c))])
    73  (ZeroExt8to64  (Const8  [c])) -> (Const64 [int64( uint8(c))])
    74  (ZeroExt16to32 (Const16 [c])) -> (Const32 [int64(uint16(c))])
    75  (ZeroExt16to64 (Const16 [c])) -> (Const64 [int64(uint16(c))])
    76  (ZeroExt32to64 (Const32 [c])) -> (Const64 [int64(uint32(c))])
    77  (SignExt8to16  (Const8  [c])) -> (Const16 [int64(  int8(c))])
    78  (SignExt8to32  (Const8  [c])) -> (Const32 [int64(  int8(c))])
    79  (SignExt8to64  (Const8  [c])) -> (Const64 [int64(  int8(c))])
    80  (SignExt16to32 (Const16 [c])) -> (Const32 [int64( int16(c))])
    81  (SignExt16to64 (Const16 [c])) -> (Const64 [int64( int16(c))])
    82  (SignExt32to64 (Const32 [c])) -> (Const64 [int64( int32(c))])
    83  
    84  // const negation is currently handled by frontend
    85  //(Neg8 (Const8 [c])) -> (Const8 [-c])
    86  //(Neg16 (Const16 [c])) -> (Const16 [-c])
    87  //(Neg32 (Const32 [c])) -> (Const32 [-c])
    88  //(Neg64 (Const64 [c])) -> (Const64 [-c])
    89  //(Neg32F (Const32F [c])) -> (Const32F [f2i(-i2f(c))])
    90  //(Neg64F (Const64F [c])) -> (Const64F [f2i(-i2f(c))])
    91  
    92  (Add8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c+d))])
    93  (Add16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c+d))])
    94  (Add32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c+d))])
    95  (Add64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c+d])
    96  (Add32F (Const32F [c]) (Const32F [d])) ->
    97          (Const32F [f2i(float64(i2f32(c) + i2f32(d)))]) // ensure we combine the operands with 32 bit precision
    98  (Add64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) + i2f(d))])
    99  (AddPtr <t> x (Const64 [c])) -> (OffPtr <t> x [c])
   100  
   101  (Sub8   (Const8 [c]) (Const8 [d]))     -> (Const8 [int64(int8(c-d))])
   102  (Sub16  (Const16 [c]) (Const16 [d]))   -> (Const16 [int64(int16(c-d))])
   103  (Sub32  (Const32 [c]) (Const32 [d]))   -> (Const32 [int64(int32(c-d))])
   104  (Sub64  (Const64 [c]) (Const64 [d]))   -> (Const64 [c-d])
   105  (Sub32F (Const32F [c]) (Const32F [d])) ->
   106          (Const32F [f2i(float64(i2f32(c) - i2f32(d)))])
   107  (Sub64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) - i2f(d))])
   108  
   109  (Mul8   (Const8 [c])   (Const8 [d]))   -> (Const8  [int64(int8(c*d))])
   110  (Mul16  (Const16 [c])  (Const16 [d]))  -> (Const16 [int64(int16(c*d))])
   111  (Mul32  (Const32 [c])  (Const32 [d]))  -> (Const32 [int64(int32(c*d))])
   112  (Mul64  (Const64 [c])  (Const64 [d]))  -> (Const64 [c*d])
   113  (Mul32F (Const32F [c]) (Const32F [d])) ->
   114          (Const32F [f2i(float64(i2f32(c) * i2f32(d)))])
   115  (Mul64F (Const64F [c]) (Const64F [d])) -> (Const64F [f2i(i2f(c) * i2f(d))])
   116  
   117  // Convert x * -1 to -x. The front-end catches some but not all of these.
   118  (Mul8  (Const8  [-1]) x) -> (Neg8  x)
   119  (Mul16 (Const16 [-1]) x) -> (Neg16 x)
   120  (Mul32 (Const32 [-1]) x) -> (Neg32 x)
   121  (Mul64 (Const64 [-1]) x) -> (Neg64 x)
   122  
   123  // Convert multiplication by a power of two to a shift.
   124  (Mul8  <t> n (Const8  [c])) && isPowerOfTwo(c) -> (Lsh8x64  <t> n (Const64 <config.fe.TypeUInt64()> [log2(c)]))
   125  (Mul16 <t> n (Const16 [c])) && isPowerOfTwo(c) -> (Lsh16x64 <t> n (Const64 <config.fe.TypeUInt64()> [log2(c)]))
   126  (Mul32 <t> n (Const32 [c])) && isPowerOfTwo(c) -> (Lsh32x64 <t> n (Const64 <config.fe.TypeUInt64()> [log2(c)]))
   127  (Mul64 <t> n (Const64 [c])) && isPowerOfTwo(c) -> (Lsh64x64 <t> n (Const64 <config.fe.TypeUInt64()> [log2(c)]))
   128  (Mul8  <t> n (Const8  [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg8  (Lsh8x64  <t> n (Const64 <config.fe.TypeUInt64()> [log2(-c)])))
   129  (Mul16 <t> n (Const16 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg16 (Lsh16x64 <t> n (Const64 <config.fe.TypeUInt64()> [log2(-c)])))
   130  (Mul32 <t> n (Const32 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg32 (Lsh32x64 <t> n (Const64 <config.fe.TypeUInt64()> [log2(-c)])))
   131  (Mul64 <t> n (Const64 [c])) && t.IsSigned() && isPowerOfTwo(-c) -> (Neg64 (Lsh64x64 <t> n (Const64 <config.fe.TypeUInt64()> [log2(-c)])))
   132  
   133  (Mod8  (Const8  [c]) (Const8  [d])) && d != 0 -> (Const8  [int64(int8(c % d))])
   134  (Mod16 (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(int16(c % d))])
   135  (Mod32 (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(int32(c % d))])
   136  (Mod64 (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [c % d])
   137  
   138  (Mod8u  (Const8 [c])  (Const8  [d])) && d != 0 -> (Const8  [int64(uint8(c) % uint8(d))])
   139  (Mod16u (Const16 [c]) (Const16 [d])) && d != 0 -> (Const16 [int64(uint16(c) % uint16(d))])
   140  (Mod32u (Const32 [c]) (Const32 [d])) && d != 0 -> (Const32 [int64(uint32(c) % uint32(d))])
   141  (Mod64u (Const64 [c]) (Const64 [d])) && d != 0 -> (Const64 [int64(uint64(c) % uint64(d))])
   142  
   143  (Lsh64x64  (Const64 [c]) (Const64 [d])) -> (Const64 [c << uint64(d)])
   144  (Rsh64x64  (Const64 [c]) (Const64 [d])) -> (Const64 [c >> uint64(d)])
   145  (Rsh64Ux64 (Const64 [c]) (Const64 [d])) -> (Const64 [int64(uint64(c) >> uint64(d))])
   146  (Lsh32x64  (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) << uint64(d))])
   147  (Rsh32x64  (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) >> uint64(d))])
   148  (Rsh32Ux64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(uint32(c) >> uint64(d)))])
   149  (Lsh16x64  (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) << uint64(d))])
   150  (Rsh16x64  (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) >> uint64(d))])
   151  (Rsh16Ux64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(uint16(c) >> uint64(d)))])
   152  (Lsh8x64   (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(c) << uint64(d))])
   153  (Rsh8x64   (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(c) >> uint64(d))])
   154  (Rsh8Ux64  (Const8  [c]) (Const64 [d])) -> (Const8  [int64(int8(uint8(c) >> uint64(d)))])
   155  
   156  // Fold IsInBounds when the range of the index cannot exceed the limit.
   157  (IsInBounds (ZeroExt8to32  _) (Const32 [c])) && (1 << 8)  <= c -> (ConstBool [1])
   158  (IsInBounds (ZeroExt8to64  _) (Const64 [c])) && (1 << 8)  <= c -> (ConstBool [1])
   159  (IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= c -> (ConstBool [1])
   160  (IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c -> (ConstBool [1])
   161  (IsInBounds x x) -> (ConstBool [0])
   162  (IsInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c < d -> (ConstBool [1])
   163  (IsInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c < d -> (ConstBool [1])
   164  (IsInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c < d)])
   165  (IsInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c < d)])
   166  // (Mod64u x y) is always between 0 (inclusive) and y (exclusive).
   167  (IsInBounds (Mod32u _ y) y) -> (ConstBool [1])
   168  (IsInBounds (Mod64u _ y) y) -> (ConstBool [1])
   169  
   170  (IsSliceInBounds x x) -> (ConstBool [1])
   171  (IsSliceInBounds (And32 (Const32 [c]) _) (Const32 [d])) && 0 <= c && c <= d -> (ConstBool [1])
   172  (IsSliceInBounds (And64 (Const64 [c]) _) (Const64 [d])) && 0 <= c && c <= d -> (ConstBool [1])
   173  (IsSliceInBounds (Const32 [0]) _) -> (ConstBool [1])
   174  (IsSliceInBounds (Const64 [0]) _) -> (ConstBool [1])
   175  (IsSliceInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(0 <= c && c <= d)])
   176  (IsSliceInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(0 <= c && c <= d)])
   177  (IsSliceInBounds (SliceLen x) (SliceCap x)) -> (ConstBool [1])
   178  
   179  (Eq64 x x) -> (ConstBool [1])
   180  (Eq32 x x) -> (ConstBool [1])
   181  (Eq16 x x) -> (ConstBool [1])
   182  (Eq8  x x) -> (ConstBool [1])
   183  (EqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c == d)])
   184  (EqB (ConstBool [0]) x) -> (Not x)
   185  (EqB (ConstBool [1]) x) -> x
   186  
   187  (Neq64 x x) -> (ConstBool [0])
   188  (Neq32 x x) -> (ConstBool [0])
   189  (Neq16 x x) -> (ConstBool [0])
   190  (Neq8  x x) -> (ConstBool [0])
   191  (NeqB (ConstBool [c]) (ConstBool [d])) -> (ConstBool [b2i(c != d)])
   192  (NeqB (ConstBool [0]) x) -> x
   193  (NeqB (ConstBool [1]) x) -> (Not x)
   194  
   195  (Eq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Eq64 (Const64 <t> [c-d]) x)
   196  (Eq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Eq32 (Const32 <t> [int64(int32(c-d))]) x)
   197  (Eq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Eq16 (Const16 <t> [int64(int16(c-d))]) x)
   198  (Eq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Eq8  (Const8 <t> [int64(int8(c-d))]) x)
   199  
   200  (Neq64 (Const64 <t> [c]) (Add64 (Const64 <t> [d]) x)) -> (Neq64 (Const64 <t> [c-d]) x)
   201  (Neq32 (Const32 <t> [c]) (Add32 (Const32 <t> [d]) x)) -> (Neq32 (Const32 <t> [int64(int32(c-d))]) x)
   202  (Neq16 (Const16 <t> [c]) (Add16 (Const16 <t> [d]) x)) -> (Neq16 (Const16 <t> [int64(int16(c-d))]) x)
   203  (Neq8  (Const8  <t> [c]) (Add8  (Const8  <t> [d]) x)) -> (Neq8 (Const8 <t> [int64(int8(c-d))]) x)
   204  
   205  // canonicalize: swap arguments for commutative operations when one argument is a constant.
   206  (Eq64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Eq64 (Const64 <t> [c]) x)
   207  (Eq32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Eq32 (Const32 <t> [c]) x)
   208  (Eq16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Eq16 (Const16 <t> [c]) x)
   209  (Eq8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (Eq8  (Const8  <t> [c]) x)
   210  
   211  (Neq64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Neq64 (Const64 <t> [c]) x)
   212  (Neq32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Neq32 (Const32 <t> [c]) x)
   213  (Neq16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Neq16 (Const16 <t> [c]) x)
   214  (Neq8  x (Const8 <t>  [c])) && x.Op != OpConst8  -> (Neq8  (Const8  <t> [c]) x)
   215  
   216  // AddPtr is not canonicalized because nilcheck ptr checks the first argument to be non-nil.
   217  (Add64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Add64 (Const64 <t> [c]) x)
   218  (Add32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Add32 (Const32 <t> [c]) x)
   219  (Add16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Add16 (Const16 <t> [c]) x)
   220  (Add8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (Add8  (Const8  <t> [c]) x)
   221  
   222  (Mul64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Mul64 (Const64 <t> [c]) x)
   223  (Mul32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Mul32 (Const32 <t> [c]) x)
   224  (Mul16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Mul16 (Const16 <t> [c]) x)
   225  (Mul8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (Mul8  (Const8  <t> [c]) x)
   226  
   227  (Sub64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Add64 (Const64 <t> [-c]) x)
   228  (Sub32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Add32 (Const32 <t> [int64(int32(-c))]) x)
   229  (Sub16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Add16 (Const16 <t> [int64(int16(-c))]) x)
   230  (Sub8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (Add8  (Const8  <t> [int64(int8(-c))]) x)
   231  
   232  (And64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (And64 (Const64 <t> [c]) x)
   233  (And32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (And32 (Const32 <t> [c]) x)
   234  (And16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (And16 (Const16 <t> [c]) x)
   235  (And8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (And8  (Const8  <t> [c]) x)
   236  
   237  (Or64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Or64 (Const64 <t> [c]) x)
   238  (Or32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Or32 (Const32 <t> [c]) x)
   239  (Or16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Or16 (Const16 <t> [c]) x)
   240  (Or8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (Or8  (Const8  <t> [c]) x)
   241  
   242  (Xor64 x (Const64 <t> [c])) && x.Op != OpConst64 -> (Xor64 (Const64 <t> [c]) x)
   243  (Xor32 x (Const32 <t> [c])) && x.Op != OpConst32 -> (Xor32 (Const32 <t> [c]) x)
   244  (Xor16 x (Const16 <t> [c])) && x.Op != OpConst16 -> (Xor16 (Const16 <t> [c]) x)
   245  (Xor8  x (Const8  <t> [c])) && x.Op != OpConst8  -> (Xor8  (Const8  <t> [c]) x)
   246  
   247  // fold negation into comparison operators
   248  (Not (Eq64 x y)) -> (Neq64 x y)
   249  (Not (Eq32 x y)) -> (Neq32 x y)
   250  (Not (Eq16 x y)) -> (Neq16 x y)
   251  (Not (Eq8  x y)) -> (Neq8  x y)
   252  (Not (EqB  x y)) -> (NeqB  x y)
   253  
   254  (Not (Neq64 x y)) -> (Eq64 x y)
   255  (Not (Neq32 x y)) -> (Eq32 x y)
   256  (Not (Neq16 x y)) -> (Eq16 x y)
   257  (Not (Neq8  x y)) -> (Eq8  x y)
   258  (Not (NeqB  x y)) -> (EqB  x y)
   259  
   260  (Not (Greater64 x y)) -> (Leq64 x y)
   261  (Not (Greater32 x y)) -> (Leq32 x y)
   262  (Not (Greater16 x y)) -> (Leq16 x y)
   263  (Not (Greater8  x y)) -> (Leq8  x y)
   264  
   265  (Not (Greater64U x y)) -> (Leq64U x y)
   266  (Not (Greater32U x y)) -> (Leq32U x y)
   267  (Not (Greater16U x y)) -> (Leq16U x y)
   268  (Not (Greater8U  x y)) -> (Leq8U  x y)
   269  
   270  (Not (Geq64 x y)) -> (Less64 x y)
   271  (Not (Geq32 x y)) -> (Less32 x y)
   272  (Not (Geq16 x y)) -> (Less16 x y)
   273  (Not (Geq8  x y)) -> (Less8  x y)
   274  
   275  (Not (Geq64U x y)) -> (Less64U x y)
   276  (Not (Geq32U x y)) -> (Less32U x y)
   277  (Not (Geq16U x y)) -> (Less16U x y)
   278  (Not (Geq8U  x y)) -> (Less8U  x y)
   279  
   280  (Not (Less64 x y)) -> (Geq64 x y)
   281  (Not (Less32 x y)) -> (Geq32 x y)
   282  (Not (Less16 x y)) -> (Geq16 x y)
   283  (Not (Less8  x y)) -> (Geq8  x y)
   284  
   285  (Not (Less64U x y)) -> (Geq64U x y)
   286  (Not (Less32U x y)) -> (Geq32U x y)
   287  (Not (Less16U x y)) -> (Geq16U x y)
   288  (Not (Less8U  x y)) -> (Geq8U  x y)
   289  
   290  (Not (Leq64 x y)) -> (Greater64 x y)
   291  (Not (Leq32 x y)) -> (Greater32 x y)
   292  (Not (Leq16 x y)) -> (Greater16 x y)
   293  (Not (Leq8  x y)) -> (Greater8 x y)
   294  
   295  (Not (Leq64U x y)) -> (Greater64U x y)
   296  (Not (Leq32U x y)) -> (Greater32U x y)
   297  (Not (Leq16U x y)) -> (Greater16U x y)
   298  (Not (Leq8U  x y)) -> (Greater8U  x y)
   299  
   300  // Distribute multiplication c * (d+x) -> c*d + c*x. Useful for:
   301  // a[i].b = ...; a[i+1].b = ...
   302  (Mul64 (Const64 <t> [c]) (Add64 <t> (Const64 <t> [d]) x)) ->
   303    (Add64 (Const64 <t> [c*d]) (Mul64 <t> (Const64 <t> [c]) x))
   304  (Mul32 (Const32 <t> [c]) (Add32 <t> (Const32 <t> [d]) x)) ->
   305    (Add32 (Const32 <t> [int64(int32(c*d))]) (Mul32 <t> (Const32 <t> [c]) x))
   306  
   307  // rewrite shifts of 8/16/32 bit consts into 64 bit consts to reduce
   308  // the number of the other rewrite rules for const shifts
   309  (Lsh64x32  <t> x (Const32 [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint32(c))]))
   310  (Lsh64x16  <t> x (Const16 [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint16(c))]))
   311  (Lsh64x8   <t> x (Const8  [c])) -> (Lsh64x64  x (Const64 <t> [int64(uint8(c))]))
   312  (Rsh64x32  <t> x (Const32 [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint32(c))]))
   313  (Rsh64x16  <t> x (Const16 [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint16(c))]))
   314  (Rsh64x8   <t> x (Const8  [c])) -> (Rsh64x64  x (Const64 <t> [int64(uint8(c))]))
   315  (Rsh64Ux32 <t> x (Const32 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint32(c))]))
   316  (Rsh64Ux16 <t> x (Const16 [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint16(c))]))
   317  (Rsh64Ux8  <t> x (Const8  [c])) -> (Rsh64Ux64 x (Const64 <t> [int64(uint8(c))]))
   318  
   319  (Lsh32x32  <t> x (Const32 [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint32(c))]))
   320  (Lsh32x16  <t> x (Const16 [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint16(c))]))
   321  (Lsh32x8   <t> x (Const8  [c])) -> (Lsh32x64  x (Const64 <t> [int64(uint8(c))]))
   322  (Rsh32x32  <t> x (Const32 [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint32(c))]))
   323  (Rsh32x16  <t> x (Const16 [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint16(c))]))
   324  (Rsh32x8   <t> x (Const8  [c])) -> (Rsh32x64  x (Const64 <t> [int64(uint8(c))]))
   325  (Rsh32Ux32 <t> x (Const32 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint32(c))]))
   326  (Rsh32Ux16 <t> x (Const16 [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint16(c))]))
   327  (Rsh32Ux8  <t> x (Const8  [c])) -> (Rsh32Ux64 x (Const64 <t> [int64(uint8(c))]))
   328  
   329  (Lsh16x32  <t> x (Const32 [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint32(c))]))
   330  (Lsh16x16  <t> x (Const16 [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint16(c))]))
   331  (Lsh16x8   <t> x (Const8  [c])) -> (Lsh16x64  x (Const64 <t> [int64(uint8(c))]))
   332  (Rsh16x32  <t> x (Const32 [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint32(c))]))
   333  (Rsh16x16  <t> x (Const16 [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint16(c))]))
   334  (Rsh16x8   <t> x (Const8  [c])) -> (Rsh16x64  x (Const64 <t> [int64(uint8(c))]))
   335  (Rsh16Ux32 <t> x (Const32 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint32(c))]))
   336  (Rsh16Ux16 <t> x (Const16 [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint16(c))]))
   337  (Rsh16Ux8  <t> x (Const8  [c])) -> (Rsh16Ux64 x (Const64 <t> [int64(uint8(c))]))
   338  
   339  (Lsh8x32  <t> x (Const32 [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint32(c))]))
   340  (Lsh8x16  <t> x (Const16 [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint16(c))]))
   341  (Lsh8x8   <t> x (Const8  [c])) -> (Lsh8x64  x (Const64 <t> [int64(uint8(c))]))
   342  (Rsh8x32  <t> x (Const32 [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint32(c))]))
   343  (Rsh8x16  <t> x (Const16 [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint16(c))]))
   344  (Rsh8x8   <t> x (Const8  [c])) -> (Rsh8x64  x (Const64 <t> [int64(uint8(c))]))
   345  (Rsh8Ux32 <t> x (Const32 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint32(c))]))
   346  (Rsh8Ux16 <t> x (Const16 [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint16(c))]))
   347  (Rsh8Ux8  <t> x (Const8  [c])) -> (Rsh8Ux64 x (Const64 <t> [int64(uint8(c))]))
   348  
   349  // shifts by zero
   350  (Lsh64x64  x (Const64 [0])) -> x
   351  (Rsh64x64  x (Const64 [0])) -> x
   352  (Rsh64Ux64 x (Const64 [0])) -> x
   353  (Lsh32x64  x (Const64 [0])) -> x
   354  (Rsh32x64  x (Const64 [0])) -> x
   355  (Rsh32Ux64 x (Const64 [0])) -> x
   356  (Lsh16x64  x (Const64 [0])) -> x
   357  (Rsh16x64  x (Const64 [0])) -> x
   358  (Rsh16Ux64 x (Const64 [0])) -> x
   359  (Lsh8x64   x (Const64 [0])) -> x
   360  (Rsh8x64   x (Const64 [0])) -> x
   361  (Rsh8Ux64  x (Const64 [0])) -> x
   362  
   363  // zero shifted.
   364  (Lsh64x64  (Const64 [0]) _) -> (Const64 [0])
   365  (Lsh64x32  (Const64 [0]) _) -> (Const64 [0])
   366  (Lsh64x16  (Const64 [0]) _) -> (Const64 [0])
   367  (Lsh64x8  (Const64 [0]) _) -> (Const64 [0])
   368  (Rsh64x64  (Const64 [0]) _) -> (Const64 [0])
   369  (Rsh64x32  (Const64 [0]) _) -> (Const64 [0])
   370  (Rsh64x16  (Const64 [0]) _) -> (Const64 [0])
   371  (Rsh64x8  (Const64 [0]) _) -> (Const64 [0])
   372  (Rsh64Ux64 (Const64 [0]) _) -> (Const64 [0])
   373  (Rsh64Ux32 (Const64 [0]) _) -> (Const64 [0])
   374  (Rsh64Ux16 (Const64 [0]) _) -> (Const64 [0])
   375  (Rsh64Ux8 (Const64 [0]) _) -> (Const64 [0])
   376  (Lsh32x64  (Const32 [0]) _) -> (Const32 [0])
   377  (Lsh32x32  (Const32 [0]) _) -> (Const32 [0])
   378  (Lsh32x16  (Const32 [0]) _) -> (Const32 [0])
   379  (Lsh32x8  (Const32 [0]) _) -> (Const32 [0])
   380  (Rsh32x64  (Const32 [0]) _) -> (Const32 [0])
   381  (Rsh32x32  (Const32 [0]) _) -> (Const32 [0])
   382  (Rsh32x16  (Const32 [0]) _) -> (Const32 [0])
   383  (Rsh32x8  (Const32 [0]) _) -> (Const32 [0])
   384  (Rsh32Ux64 (Const32 [0]) _) -> (Const32 [0])
   385  (Rsh32Ux32 (Const32 [0]) _) -> (Const32 [0])
   386  (Rsh32Ux16 (Const32 [0]) _) -> (Const32 [0])
   387  (Rsh32Ux8 (Const32 [0]) _) -> (Const32 [0])
   388  (Lsh16x64  (Const16 [0]) _) -> (Const16 [0])
   389  (Lsh16x32  (Const16 [0]) _) -> (Const16 [0])
   390  (Lsh16x16  (Const16 [0]) _) -> (Const16 [0])
   391  (Lsh16x8  (Const16 [0]) _) -> (Const16 [0])
   392  (Rsh16x64  (Const16 [0]) _) -> (Const16 [0])
   393  (Rsh16x32  (Const16 [0]) _) -> (Const16 [0])
   394  (Rsh16x16  (Const16 [0]) _) -> (Const16 [0])
   395  (Rsh16x8  (Const16 [0]) _) -> (Const16 [0])
   396  (Rsh16Ux64 (Const16 [0]) _) -> (Const16 [0])
   397  (Rsh16Ux32 (Const16 [0]) _) -> (Const16 [0])
   398  (Rsh16Ux16 (Const16 [0]) _) -> (Const16 [0])
   399  (Rsh16Ux8 (Const16 [0]) _) -> (Const16 [0])
   400  (Lsh8x64   (Const8 [0]) _) -> (Const8  [0])
   401  (Lsh8x32   (Const8 [0]) _) -> (Const8  [0])
   402  (Lsh8x16   (Const8 [0]) _) -> (Const8  [0])
   403  (Lsh8x8   (Const8 [0]) _) -> (Const8  [0])
   404  (Rsh8x64   (Const8 [0]) _) -> (Const8  [0])
   405  (Rsh8x32   (Const8 [0]) _) -> (Const8  [0])
   406  (Rsh8x16   (Const8 [0]) _) -> (Const8  [0])
   407  (Rsh8x8   (Const8 [0]) _) -> (Const8  [0])
   408  (Rsh8Ux64  (Const8 [0]) _) -> (Const8  [0])
   409  (Rsh8Ux32  (Const8 [0]) _) -> (Const8  [0])
   410  (Rsh8Ux16  (Const8 [0]) _) -> (Const8  [0])
   411  (Rsh8Ux8  (Const8 [0]) _) -> (Const8  [0])
   412  
   413  // large left shifts of all values, and right shifts of unsigned values
   414  (Lsh64x64  _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0])
   415  (Rsh64Ux64 _ (Const64 [c])) && uint64(c) >= 64 -> (Const64 [0])
   416  (Lsh32x64  _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0])
   417  (Rsh32Ux64 _ (Const64 [c])) && uint64(c) >= 32 -> (Const32 [0])
   418  (Lsh16x64  _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0])
   419  (Rsh16Ux64 _ (Const64 [c])) && uint64(c) >= 16 -> (Const16 [0])
   420  (Lsh8x64   _ (Const64 [c])) && uint64(c) >= 8  -> (Const8  [0])
   421  (Rsh8Ux64  _ (Const64 [c])) && uint64(c) >= 8  -> (Const8  [0])
   422  
   423  // combine const shifts
   424  (Lsh64x64 <t> (Lsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh64x64 x (Const64 <t> [c+d]))
   425  (Lsh32x64 <t> (Lsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh32x64 x (Const64 <t> [c+d]))
   426  (Lsh16x64 <t> (Lsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh16x64 x (Const64 <t> [c+d]))
   427  (Lsh8x64  <t> (Lsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Lsh8x64  x (Const64 <t> [c+d]))
   428  
   429  (Rsh64x64 <t> (Rsh64x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64x64 x (Const64 <t> [c+d]))
   430  (Rsh32x64 <t> (Rsh32x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32x64 x (Const64 <t> [c+d]))
   431  (Rsh16x64 <t> (Rsh16x64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16x64 x (Const64 <t> [c+d]))
   432  (Rsh8x64  <t> (Rsh8x64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8x64  x (Const64 <t> [c+d]))
   433  
   434  (Rsh64Ux64 <t> (Rsh64Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh64Ux64 x (Const64 <t> [c+d]))
   435  (Rsh32Ux64 <t> (Rsh32Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh32Ux64 x (Const64 <t> [c+d]))
   436  (Rsh16Ux64 <t> (Rsh16Ux64 x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh16Ux64 x (Const64 <t> [c+d]))
   437  (Rsh8Ux64  <t> (Rsh8Ux64  x (Const64 [c])) (Const64 [d])) && !uaddOvf(c,d) -> (Rsh8Ux64  x (Const64 <t> [c+d]))
   438  
   439  // ((x >> c1) << c2) >> c3
   440  (Rsh64Ux64 (Lsh64x64 (Rsh64Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   441    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   442    -> (Rsh64Ux64 x (Const64 <config.fe.TypeUInt64()> [c1-c2+c3]))
   443  (Rsh32Ux64 (Lsh32x64 (Rsh32Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   444    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   445    -> (Rsh32Ux64 x (Const64 <config.fe.TypeUInt64()> [c1-c2+c3]))
   446  (Rsh16Ux64 (Lsh16x64 (Rsh16Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   447    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   448    -> (Rsh16Ux64 x (Const64 <config.fe.TypeUInt64()> [c1-c2+c3]))
   449  (Rsh8Ux64 (Lsh8x64 (Rsh8Ux64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   450    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   451    -> (Rsh8Ux64 x (Const64 <config.fe.TypeUInt64()> [c1-c2+c3]))
   452  
   453  // ((x << c1) >> c2) << c3
   454  (Lsh64x64 (Rsh64Ux64 (Lsh64x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   455    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   456    -> (Lsh64x64 x (Const64 <config.fe.TypeUInt64()> [c1-c2+c3]))
   457  (Lsh32x64 (Rsh32Ux64 (Lsh32x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   458    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   459    -> (Lsh32x64 x (Const64 <config.fe.TypeUInt64()> [c1-c2+c3]))
   460  (Lsh16x64 (Rsh16Ux64 (Lsh16x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   461    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   462    -> (Lsh16x64 x (Const64 <config.fe.TypeUInt64()> [c1-c2+c3]))
   463  (Lsh8x64 (Rsh8Ux64 (Lsh8x64 x (Const64 [c1])) (Const64 [c2])) (Const64 [c3]))
   464    && uint64(c1) >= uint64(c2) && uint64(c3) >= uint64(c2) && !uaddOvf(c1-c2, c3)
   465    -> (Lsh8x64 x (Const64 <config.fe.TypeUInt64()> [c1-c2+c3]))
   466  
   467  // constant comparisons
   468  (Eq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c == d)])
   469  (Eq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c == d)])
   470  (Eq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c == d)])
   471  (Eq8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c == d)])
   472  
   473  (Neq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c != d)])
   474  (Neq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c != d)])
   475  (Neq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c != d)])
   476  (Neq8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c != d)])
   477  
   478  (Greater64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c > d)])
   479  (Greater32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c > d)])
   480  (Greater16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c > d)])
   481  (Greater8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c > d)])
   482  
   483  (Greater64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) > uint64(d))])
   484  (Greater32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) > uint32(d))])
   485  (Greater16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) > uint16(d))])
   486  (Greater8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  > uint8(d))])
   487  
   488  (Geq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c >= d)])
   489  (Geq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c >= d)])
   490  (Geq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c >= d)])
   491  (Geq8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c >= d)])
   492  
   493  (Geq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) >= uint64(d))])
   494  (Geq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) >= uint32(d))])
   495  (Geq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) >= uint16(d))])
   496  (Geq8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  >= uint8(d))])
   497  
   498  (Less64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c < d)])
   499  (Less32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c < d)])
   500  (Less16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c < d)])
   501  (Less8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c < d)])
   502  
   503  (Less64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) < uint64(d))])
   504  (Less32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) < uint32(d))])
   505  (Less16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) < uint16(d))])
   506  (Less8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  < uint8(d))])
   507  
   508  (Leq64 (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(c <= d)])
   509  (Leq32 (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(c <= d)])
   510  (Leq16 (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(c <= d)])
   511  (Leq8  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(c <= d)])
   512  
   513  (Leq64U (Const64 [c]) (Const64 [d])) -> (ConstBool [b2i(uint64(c) <= uint64(d))])
   514  (Leq32U (Const32 [c]) (Const32 [d])) -> (ConstBool [b2i(uint32(c) <= uint32(d))])
   515  (Leq16U (Const16 [c]) (Const16 [d])) -> (ConstBool [b2i(uint16(c) <= uint16(d))])
   516  (Leq8U  (Const8  [c]) (Const8  [d])) -> (ConstBool [b2i(uint8(c)  <= uint8(d))])
   517  
   518  // simplifications
   519  (Or64 x x) -> x
   520  (Or32 x x) -> x
   521  (Or16 x x) -> x
   522  (Or8  x x) -> x
   523  (Or64 (Const64 [0]) x) -> x
   524  (Or32 (Const32 [0]) x) -> x
   525  (Or16 (Const16 [0]) x) -> x
   526  (Or8  (Const8  [0]) x) -> x
   527  (Or64 (Const64 [-1]) _) -> (Const64 [-1])
   528  (Or32 (Const32 [-1]) _) -> (Const32 [-1])
   529  (Or16 (Const16 [-1]) _) -> (Const16 [-1])
   530  (Or8  (Const8  [-1]) _) -> (Const8  [-1])
   531  (And64 x x) -> x
   532  (And32 x x) -> x
   533  (And16 x x) -> x
   534  (And8  x x) -> x
   535  (And64 (Const64 [-1]) x) -> x
   536  (And32 (Const32 [-1]) x) -> x
   537  (And16 (Const16 [-1]) x) -> x
   538  (And8  (Const8  [-1]) x) -> x
   539  (And64 (Const64 [0]) _) -> (Const64 [0])
   540  (And32 (Const32 [0]) _) -> (Const32 [0])
   541  (And16 (Const16 [0]) _) -> (Const16 [0])
   542  (And8  (Const8  [0]) _) -> (Const8  [0])
   543  (Xor64 x x) -> (Const64 [0])
   544  (Xor32 x x) -> (Const32 [0])
   545  (Xor16 x x) -> (Const16 [0])
   546  (Xor8  x x) -> (Const8  [0])
   547  (Xor64 (Const64 [0]) x) -> x
   548  (Xor32 (Const32 [0]) x) -> x
   549  (Xor16 (Const16 [0]) x) -> x
   550  (Xor8  (Const8  [0]) x) -> x
   551  (Add64 (Const64 [0]) x) -> x
   552  (Add32 (Const32 [0]) x) -> x
   553  (Add16 (Const16 [0]) x) -> x
   554  (Add8  (Const8  [0]) x) -> x
   555  (Sub64 x x) -> (Const64 [0])
   556  (Sub32 x x) -> (Const32 [0])
   557  (Sub16 x x) -> (Const16 [0])
   558  (Sub8  x x) -> (Const8  [0])
   559  (Mul64 (Const64 [0]) _) -> (Const64 [0])
   560  (Mul32 (Const32 [0]) _) -> (Const32 [0])
   561  (Mul16 (Const16 [0]) _) -> (Const16 [0])
   562  (Mul8  (Const8  [0]) _) -> (Const8  [0])
   563  (Com8  (Com8  x)) -> x
   564  (Com16 (Com16 x)) -> x
   565  (Com32 (Com32 x)) -> x
   566  (Com64 (Com64 x)) -> x
   567  (Neg8  (Sub8  x y)) -> (Sub8  y x)
   568  (Neg16 (Sub16 x y)) -> (Sub16 y x)
   569  (Neg32 (Sub32 x y)) -> (Sub32 y x)
   570  (Neg64 (Sub64 x y)) -> (Sub64 y x)
   571  
   572  (And64 x (And64 x y)) -> (And64 x y)
   573  (And32 x (And32 x y)) -> (And32 x y)
   574  (And16 x (And16 x y)) -> (And16 x y)
   575  (And8  x (And8  x y)) -> (And8  x y)
   576  (And64 x (And64 y x)) -> (And64 x y)
   577  (And32 x (And32 y x)) -> (And32 x y)
   578  (And16 x (And16 y x)) -> (And16 x y)
   579  (And8  x (And8  y x)) -> (And8  x y)
   580  (And64 (And64 x y) x) -> (And64 x y)
   581  (And32 (And32 x y) x) -> (And32 x y)
   582  (And16 (And16 x y) x) -> (And16 x y)
   583  (And8  (And8  x y) x) -> (And8  x y)
   584  (And64 (And64 x y) y) -> (And64 x y)
   585  (And32 (And32 x y) y) -> (And32 x y)
   586  (And16 (And16 x y) y) -> (And16 x y)
   587  (And8  (And8  x y) y) -> (And8  x y)
   588  (Or64 x (Or64 x y)) -> (Or64 x y)
   589  (Or32 x (Or32 x y)) -> (Or32 x y)
   590  (Or16 x (Or16 x y)) -> (Or16 x y)
   591  (Or8  x (Or8  x y)) -> (Or8  x y)
   592  (Or64 x (Or64 y x)) -> (Or64 x y)
   593  (Or32 x (Or32 y x)) -> (Or32 x y)
   594  (Or16 x (Or16 y x)) -> (Or16 x y)
   595  (Or8  x (Or8  y x)) -> (Or8  x y)
   596  (Or64 (Or64 x y) x) -> (Or64 x y)
   597  (Or32 (Or32 x y) x) -> (Or32 x y)
   598  (Or16 (Or16 x y) x) -> (Or16 x y)
   599  (Or8  (Or8  x y) x) -> (Or8  x y)
   600  (Or64 (Or64 x y) y) -> (Or64 x y)
   601  (Or32 (Or32 x y) y) -> (Or32 x y)
   602  (Or16 (Or16 x y) y) -> (Or16 x y)
   603  (Or8  (Or8  x y) y) -> (Or8  x y)
   604  (Xor64 x (Xor64 x y)) -> y
   605  (Xor32 x (Xor32 x y)) -> y
   606  (Xor16 x (Xor16 x y)) -> y
   607  (Xor8  x (Xor8  x y)) -> y
   608  (Xor64 x (Xor64 y x)) -> y
   609  (Xor32 x (Xor32 y x)) -> y
   610  (Xor16 x (Xor16 y x)) -> y
   611  (Xor8  x (Xor8  y x)) -> y
   612  (Xor64 (Xor64 x y) x) -> y
   613  (Xor32 (Xor32 x y) x) -> y
   614  (Xor16 (Xor16 x y) x) -> y
   615  (Xor8  (Xor8  x y) x) -> y
   616  (Xor64 (Xor64 x y) y) -> x
   617  (Xor32 (Xor32 x y) y) -> x
   618  (Xor16 (Xor16 x y) y) -> x
   619  (Xor8  (Xor8  x y) y) -> x
   620  
   621  (Trunc64to8  (And64 (Const64 [y]) x)) && y&0xFF == 0xFF -> (Trunc64to8 x)
   622  (Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc64to16 x)
   623  (Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF -> (Trunc64to32 x)
   624  (Trunc32to8  (And32 (Const32 [y]) x)) && y&0xFF == 0xFF -> (Trunc32to8 x)
   625  (Trunc32to16 (And32 (Const32 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc32to16 x)
   626  (Trunc16to8  (And16 (Const16 [y]) x)) && y&0xFF == 0xFF -> (Trunc16to8 x)
   627  
   628  (ZeroExt8to64  (Trunc64to8  x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 56 -> x
   629  (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 48 -> x
   630  (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s])))) && s >= 32 -> x
   631  (ZeroExt8to32  (Trunc32to8  x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 24 -> x
   632  (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s])))) && s >= 16 -> x
   633  (ZeroExt8to16  (Trunc16to8  x:(Rsh16Ux64 _ (Const64 [s])))) && s >= 8 -> x
   634  
   635  (SignExt8to64  (Trunc64to8  x:(Rsh64x64 _ (Const64 [s])))) && s >= 56 -> x
   636  (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s])))) && s >= 48 -> x
   637  (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s])))) && s >= 32 -> x
   638  (SignExt8to32  (Trunc32to8  x:(Rsh32x64 _ (Const64 [s])))) && s >= 24 -> x
   639  (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s])))) && s >= 16 -> x
   640  (SignExt8to16  (Trunc16to8  x:(Rsh16x64 _ (Const64 [s])))) && s >= 8 -> x
   641  
   642  (Slicemask (Const32 [x])) && x > 0 -> (Const32 [-1])
   643  (Slicemask (Const32 [0]))          -> (Const32 [0])
   644  (Slicemask (Const64 [x])) && x > 0 -> (Const64 [-1])
   645  (Slicemask (Const64 [0]))          -> (Const64 [0])
   646  
   647  // Rewrite AND of consts as shifts if possible, slightly faster for 64 bit operands
   648  // leading zeros can be shifted left, then right
   649  (And64 <t> (Const64 [y]) x) && nlz(y) + nto(y) == 64 && nto(y) >= 32
   650    -> (Rsh64Ux64 (Lsh64x64 <t> x (Const64 <t> [nlz(y)])) (Const64 <t> [nlz(y)]))
   651  // trailing zeros can be shifted right, then left
   652  (And64 <t> (Const64 [y]) x) && nlo(y) + ntz(y) == 64 && ntz(y) >= 32
   653    -> (Lsh64x64 (Rsh64Ux64 <t> x (Const64 <t> [ntz(y)])) (Const64 <t> [ntz(y)]))
   654  
   655  // simplifications often used for lengths.  e.g. len(s[i:i+5])==5
   656  (Sub64 (Add64 x y) x) -> y
   657  (Sub64 (Add64 x y) y) -> x
   658  (Sub32 (Add32 x y) x) -> y
   659  (Sub32 (Add32 x y) y) -> x
   660  (Sub16 (Add16 x y) x) -> y
   661  (Sub16 (Add16 x y) y) -> x
   662  (Sub8  (Add8  x y) x) -> y
   663  (Sub8  (Add8  x y) y) -> x
   664  
   665  // basic phi simplifications
   666  (Phi (Const8  [c]) (Const8  [c])) -> (Const8  [c])
   667  (Phi (Const16 [c]) (Const16 [c])) -> (Const16 [c])
   668  (Phi (Const32 [c]) (Const32 [c])) -> (Const32 [c])
   669  (Phi (Const64 [c]) (Const64 [c])) -> (Const64 [c])
   670  
   671  // user nil checks
   672  (NeqPtr p (ConstNil)) -> (IsNonNil p)
   673  (NeqPtr (ConstNil) p) -> (IsNonNil p)
   674  (EqPtr p (ConstNil)) -> (Not (IsNonNil p))
   675  (EqPtr (ConstNil) p) -> (Not (IsNonNil p))
   676  (IsNonNil (ConstNil)) -> (ConstBool [0])
   677  
   678  // slice and interface comparisons
   679  // The frontend ensures that we can only compare against nil,
   680  // so we need only compare the first word (interface type or slice ptr).
   681  (EqInter x y)  -> (EqPtr  (ITab x) (ITab y))
   682  (NeqInter x y) -> (NeqPtr (ITab x) (ITab y))
   683  (EqSlice x y)  -> (EqPtr  (SlicePtr x) (SlicePtr y))
   684  (NeqSlice x y) -> (NeqPtr (SlicePtr x) (SlicePtr y))
   685  
   686  // Load of store of same address, with compatibly typed value and same size
   687  (Load <t1> p1 (Store [w] p2 x _)) && isSamePtr(p1,p2) && t1.Compare(x.Type)==CMPeq && w == t1.Size() -> x
   688  
   689  // Collapse OffPtr
   690  (OffPtr (OffPtr p [b]) [a]) -> (OffPtr p [a+b])
   691  (OffPtr p [0]) && v.Type.Compare(p.Type) == CMPeq -> p
   692  
   693  // indexing operations
   694  // Note: bounds check has already been done
   695  (PtrIndex <t> ptr idx) && config.PtrSize == 4 -> (AddPtr ptr (Mul32 <config.fe.TypeInt()> idx (Const32 <config.fe.TypeInt()> [t.ElemType().Size()])))
   696  (PtrIndex <t> ptr idx) && config.PtrSize == 8 -> (AddPtr ptr (Mul64 <config.fe.TypeInt()> idx (Const64 <config.fe.TypeInt()> [t.ElemType().Size()])))
   697  
   698  // struct operations
   699  (StructSelect (StructMake1 x)) -> x
   700  (StructSelect [0] (StructMake2 x _)) -> x
   701  (StructSelect [1] (StructMake2 _ x)) -> x
   702  (StructSelect [0] (StructMake3 x _ _)) -> x
   703  (StructSelect [1] (StructMake3 _ x _)) -> x
   704  (StructSelect [2] (StructMake3 _ _ x)) -> x
   705  (StructSelect [0] (StructMake4 x _ _ _)) -> x
   706  (StructSelect [1] (StructMake4 _ x _ _)) -> x
   707  (StructSelect [2] (StructMake4 _ _ x _)) -> x
   708  (StructSelect [3] (StructMake4 _ _ _ x)) -> x
   709  
   710  (Load <t> _ _) && t.IsStruct() && t.NumFields() == 0 && config.fe.CanSSA(t) ->
   711    (StructMake0)
   712  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 1 && config.fe.CanSSA(t) ->
   713    (StructMake1
   714      (Load <t.FieldType(0)> ptr mem))
   715  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 2 && config.fe.CanSSA(t) ->
   716    (StructMake2
   717      (Load <t.FieldType(0)> ptr mem)
   718      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem))
   719  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 3 && config.fe.CanSSA(t) ->
   720    (StructMake3
   721      (Load <t.FieldType(0)> ptr mem)
   722      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
   723      (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem))
   724  (Load <t> ptr mem) && t.IsStruct() && t.NumFields() == 4 && config.fe.CanSSA(t) ->
   725    (StructMake4
   726      (Load <t.FieldType(0)> ptr mem)
   727      (Load <t.FieldType(1)> (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] ptr) mem)
   728      (Load <t.FieldType(2)> (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] ptr) mem)
   729      (Load <t.FieldType(3)> (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] ptr) mem))
   730  
   731  (StructSelect [i] x:(Load <t> ptr mem)) && !config.fe.CanSSA(t) ->
   732    @x.Block (Load <v.Type> (OffPtr <v.Type.PtrTo()> [t.FieldOff(int(i))] ptr) mem)
   733  
   734  (Store _ (StructMake0) mem) -> mem
   735  (Store dst (StructMake1 <t> f0) mem) ->
   736    (Store [t.FieldType(0).Size()] dst f0 mem)
   737  (Store dst (StructMake2 <t> f0 f1) mem) ->
   738    (Store [t.FieldType(1).Size()]
   739      (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   740      f1
   741      (Store [t.FieldType(0).Size()] dst f0 mem))
   742  (Store dst (StructMake3 <t> f0 f1 f2) mem) ->
   743    (Store [t.FieldType(2).Size()]
   744      (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
   745      f2
   746      (Store [t.FieldType(1).Size()]
   747        (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   748        f1
   749        (Store [t.FieldType(0).Size()] dst f0 mem)))
   750  (Store dst (StructMake4 <t> f0 f1 f2 f3) mem) ->
   751    (Store [t.FieldType(3).Size()]
   752      (OffPtr <t.FieldType(3).PtrTo()> [t.FieldOff(3)] dst)
   753      f3
   754      (Store [t.FieldType(2).Size()]
   755        (OffPtr <t.FieldType(2).PtrTo()> [t.FieldOff(2)] dst)
   756        f2
   757        (Store [t.FieldType(1).Size()]
   758          (OffPtr <t.FieldType(1).PtrTo()> [t.FieldOff(1)] dst)
   759          f1
   760          (Store [t.FieldType(0).Size()] dst f0 mem))))
   761  
   762  // Putting struct{*byte} and similar into direct interfaces.
   763  (IMake typ (StructMake1 val)) -> (IMake typ val)
   764  (StructSelect [0] x:(IData _)) -> x
   765  
   766  // un-SSAable values use mem->mem copies
   767  (Store [size] dst (Load <t> src mem) mem) && !config.fe.CanSSA(t) ->
   768  	(Move [MakeSizeAndAlign(size, t.Alignment()).Int64()] dst src mem)
   769  (Store [size] dst (Load <t> src mem) (VarDef {x} mem)) && !config.fe.CanSSA(t) ->
   770  	(Move [MakeSizeAndAlign(size, t.Alignment()).Int64()] dst src (VarDef {x} mem))
   771  
   772  // array ops
   773  (ArraySelect (ArrayMake1 x)) -> x
   774  
   775  (Load <t> _ _) && t.IsArray() && t.NumElem() == 0 ->
   776    (ArrayMake0)
   777  
   778  (Load <t> ptr mem) && t.IsArray() && t.NumElem() == 1 && config.fe.CanSSA(t) ->
   779    (ArrayMake1 (Load <t.ElemType()> ptr mem))
   780  
   781  (Store _ (ArrayMake0) mem) -> mem
   782  (Store [size] dst (ArrayMake1 e) mem) -> (Store [size] dst e mem)
   783  
   784  (ArraySelect [0] (Load ptr mem)) -> (Load ptr mem)
   785  
   786  // Putting [1]{*byte} and similar into direct interfaces.
   787  (IMake typ (ArrayMake1 val)) -> (IMake typ val)
   788  (ArraySelect [0] x:(IData _)) -> x
   789  
   790  // string ops
   791  // Decomposing StringMake and lowering of StringPtr and StringLen
   792  // happens in a later pass, dec, so that these operations are available
   793  // to other passes for optimizations.
   794  (StringPtr (StringMake (Const64 <t> [c]) _)) -> (Const64 <t> [c])
   795  (StringLen (StringMake _ (Const64 <t> [c]))) -> (Const64 <t> [c])
   796  (ConstString {s}) && config.PtrSize == 4 && s.(string) == "" ->
   797    (StringMake (ConstNil) (Const32 <config.fe.TypeInt()> [0]))
   798  (ConstString {s}) && config.PtrSize == 8 && s.(string) == "" ->
   799    (StringMake (ConstNil) (Const64 <config.fe.TypeInt()> [0]))
   800  (ConstString {s}) && config.PtrSize == 4 && s.(string) != "" ->
   801    (StringMake
   802      (Addr <config.fe.TypeBytePtr()> {config.fe.StringData(s.(string))}
   803        (SB))
   804      (Const32 <config.fe.TypeInt()> [int64(len(s.(string)))]))
   805  (ConstString {s}) && config.PtrSize == 8 && s.(string) != "" ->
   806    (StringMake
   807      (Addr <config.fe.TypeBytePtr()> {config.fe.StringData(s.(string))}
   808        (SB))
   809      (Const64 <config.fe.TypeInt()> [int64(len(s.(string)))]))
   810  
   811  // slice ops
   812  // Only a few slice rules are provided here.  See dec.rules for
   813  // a more comprehensive set.
   814  (SliceLen (SliceMake _ (Const64 <t> [c]) _)) -> (Const64 <t> [c])
   815  (SliceCap (SliceMake _ _ (Const64 <t> [c]))) -> (Const64 <t> [c])
   816  (SliceLen (SliceMake _ (Const32 <t> [c]) _)) -> (Const32 <t> [c])
   817  (SliceCap (SliceMake _ _ (Const32 <t> [c]))) -> (Const32 <t> [c])
   818  (SlicePtr (SliceMake (SlicePtr x) _ _)) -> (SlicePtr x)
   819  (SliceLen (SliceMake _ (SliceLen x) _)) -> (SliceLen x)
   820  (SliceCap (SliceMake _ _ (SliceCap x))) -> (SliceCap x)
   821  (SliceCap (SliceMake _ _ (SliceLen x))) -> (SliceLen x)
   822  (ConstSlice) && config.PtrSize == 4 ->
   823    (SliceMake
   824      (ConstNil <v.Type.ElemType().PtrTo()>)
   825      (Const32 <config.fe.TypeInt()> [0])
   826      (Const32 <config.fe.TypeInt()> [0]))
   827  (ConstSlice) && config.PtrSize == 8 ->
   828    (SliceMake
   829      (ConstNil <v.Type.ElemType().PtrTo()>)
   830      (Const64 <config.fe.TypeInt()> [0])
   831      (Const64 <config.fe.TypeInt()> [0]))
   832  
   833  // interface ops
   834  (ConstInterface) ->
   835    (IMake
   836      (ConstNil <config.fe.TypeBytePtr()>)
   837      (ConstNil <config.fe.TypeBytePtr()>))
   838  
   839  (NilCheck (GetG mem) mem) -> mem
   840  
   841  (If (Not cond) yes no) -> (If cond no yes)
   842  (If (ConstBool [c]) yes no) && c == 1 -> (First nil yes no)
   843  (If (ConstBool [c]) yes no) && c == 0 -> (First nil no yes)
   844  
   845  // Get rid of Convert ops for pointer arithmetic on unsafe.Pointer.
   846  (Convert (Add64 (Convert ptr mem) off) mem) -> (Add64 ptr off)
   847  (Convert (Add64 off (Convert ptr mem)) mem) -> (Add64 ptr off)
   848  (Convert (Convert ptr mem) mem) -> ptr
   849  
   850  // Decompose compound argument values
   851  (Arg {n} [off]) && v.Type.IsString() ->
   852    (StringMake
   853      (Arg <config.fe.TypeBytePtr()> {n} [off])
   854      (Arg <config.fe.TypeInt()> {n} [off+config.PtrSize]))
   855  
   856  (Arg {n} [off]) && v.Type.IsSlice() ->
   857    (SliceMake
   858      (Arg <v.Type.ElemType().PtrTo()> {n} [off])
   859      (Arg <config.fe.TypeInt()> {n} [off+config.PtrSize])
   860      (Arg <config.fe.TypeInt()> {n} [off+2*config.PtrSize]))
   861  
   862  (Arg {n} [off]) && v.Type.IsInterface() ->
   863    (IMake
   864      (Arg <config.fe.TypeBytePtr()> {n} [off])
   865      (Arg <config.fe.TypeBytePtr()> {n} [off+config.PtrSize]))
   866  
   867  (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 16 ->
   868    (ComplexMake
   869      (Arg <config.fe.TypeFloat64()> {n} [off])
   870      (Arg <config.fe.TypeFloat64()> {n} [off+8]))
   871  
   872  (Arg {n} [off]) && v.Type.IsComplex() && v.Type.Size() == 8 ->
   873    (ComplexMake
   874      (Arg <config.fe.TypeFloat32()> {n} [off])
   875      (Arg <config.fe.TypeFloat32()> {n} [off+4]))
   876  
   877  (Arg <t>) && t.IsStruct() && t.NumFields() == 0 && config.fe.CanSSA(t) ->
   878    (StructMake0)
   879  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 1 && config.fe.CanSSA(t) ->
   880    (StructMake1
   881      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)]))
   882  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 2 && config.fe.CanSSA(t) ->
   883    (StructMake2
   884      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
   885      (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)]))
   886  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 3 && config.fe.CanSSA(t) ->
   887    (StructMake3
   888      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
   889      (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])
   890      (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)]))
   891  (Arg <t> {n} [off]) && t.IsStruct() && t.NumFields() == 4 && config.fe.CanSSA(t) ->
   892    (StructMake4
   893      (Arg <t.FieldType(0)> {n} [off+t.FieldOff(0)])
   894      (Arg <t.FieldType(1)> {n} [off+t.FieldOff(1)])
   895      (Arg <t.FieldType(2)> {n} [off+t.FieldOff(2)])
   896      (Arg <t.FieldType(3)> {n} [off+t.FieldOff(3)]))
   897  
   898  (Arg <t>) && t.IsArray() && t.NumElem() == 0 ->
   899    (ArrayMake0)
   900  (Arg <t> {n} [off]) && t.IsArray() && t.NumElem() == 1 && config.fe.CanSSA(t) ->
   901    (ArrayMake1 (Arg <t.ElemType()> {n} [off]))
   902  
   903  // strength reduction of divide by a constant.
   904  // Note: frontend does <=32 bits. We only need to do 64 bits here.
   905  // TODO: Do them all here?
   906  
   907  // Div/mod by 1.  Currently handled by frontend.
   908  //(Div64 n (Const64 [1])) -> n
   909  //(Div64u n (Const64 [1])) -> n
   910  //(Mod64 n (Const64 [1])) -> (Const64 [0])
   911  //(Mod64u n (Const64 [1])) -> (Const64 [0])
   912  
   913  // Unsigned divide by power of 2.
   914  (Div64u <t> n (Const64 [c])) && isPowerOfTwo(c) -> (Rsh64Ux64 n (Const64 <t> [log2(c)]))
   915  (Mod64u <t> n (Const64 [c])) && isPowerOfTwo(c) -> (And64 n (Const64 <t> [c-1]))
   916  
   917  // Signed divide by power of 2.  Currently handled by frontend.
   918  // n / c = n >> log(c)       if n >= 0
   919  //       = (n+c-1) >> log(c) if n < 0
   920  // We conditionally add c-1 by adding n>>63>>(64-log(c)) (first shift signed, second shift unsigned).
   921  //(Div64 <t> n (Const64 [c])) && isPowerOfTwo(c) ->
   922  //  (Rsh64x64
   923  //    (Add64 <t>
   924  //      n
   925  //      (Rsh64Ux64 <t>
   926  //        (Rsh64x64 <t> n (Const64 <t> [63]))
   927  //        (Const64 <t> [64-log2(c)])))
   928  //    (Const64 <t> [log2(c)]))
   929  
   930  // Unsigned divide, not a power of 2.  Strength reduce to a multiply.
   931  (Div64u <t> x (Const64 [c])) && umagic64ok(c) && !umagic64a(c) ->
   932    (Rsh64Ux64
   933      (Hmul64u <t>
   934        (Const64 <t> [umagic64m(c)])
   935        x)
   936      (Const64 <t> [umagic64s(c)]))
   937  (Div64u <t> x (Const64 [c])) && umagic64ok(c) && umagic64a(c) ->
   938    (Rsh64Ux64
   939      (Avg64u <t>
   940        (Hmul64u <t>
   941          x
   942          (Const64 <t> [umagic64m(c)]))
   943        x)
   944      (Const64 <t> [umagic64s(c)-1]))
   945  
   946  // Signed divide, not a power of 2.  Strength reduce to a multiply.
   947  (Div64 <t> x (Const64 [c])) && c > 0 && smagic64ok(c) && smagic64m(c) > 0 ->
   948    (Sub64 <t>
   949      (Rsh64x64 <t>
   950        (Hmul64 <t>
   951          (Const64 <t> [smagic64m(c)])
   952          x)
   953        (Const64 <t> [smagic64s(c)]))
   954      (Rsh64x64 <t>
   955        x
   956        (Const64 <t> [63])))
   957  (Div64 <t> x (Const64 [c])) && c > 0 && smagic64ok(c) && smagic64m(c) < 0 ->
   958    (Sub64 <t>
   959      (Rsh64x64 <t>
   960        (Add64 <t>
   961          (Hmul64 <t>
   962            (Const64 <t> [smagic64m(c)])
   963            x)
   964          x)
   965        (Const64 <t> [smagic64s(c)]))
   966      (Rsh64x64 <t>
   967        x
   968        (Const64 <t> [63])))
   969  (Div64 <t> x (Const64 [c])) && c < 0 && smagic64ok(c) && smagic64m(c) > 0 ->
   970    (Neg64 <t>
   971      (Sub64 <t>
   972        (Rsh64x64 <t>
   973          (Hmul64 <t>
   974            (Const64 <t> [smagic64m(c)])
   975            x)
   976          (Const64 <t> [smagic64s(c)]))
   977        (Rsh64x64 <t>
   978          x
   979          (Const64 <t> [63]))))
   980  (Div64 <t> x (Const64 [c])) && c < 0 && smagic64ok(c) && smagic64m(c) < 0 ->
   981    (Neg64 <t>
   982      (Sub64 <t>
   983        (Rsh64x64 <t>
   984          (Add64 <t>
   985            (Hmul64 <t>
   986              (Const64 <t> [smagic64m(c)])
   987              x)
   988            x)
   989          (Const64 <t> [smagic64s(c)]))
   990        (Rsh64x64 <t>
   991          x
   992          (Const64 <t> [63]))))
   993  
   994  // A%B = A-(A/B*B).
   995  // This implements % with two * and a bunch of ancillary ops.
   996  // One of the * is free if the user's code also computes A/B.
   997  (Mod64  <t> x (Const64 [c])) && x.Op != OpConst64 && smagic64ok(c)
   998    -> (Sub64 x (Mul64 <t> (Div64  <t> x (Const64 <t> [c])) (Const64 <t> [c])))
   999  (Mod64u <t> x (Const64 [c])) && x.Op != OpConst64 && umagic64ok(c)
  1000    -> (Sub64 x (Mul64 <t> (Div64u <t> x (Const64 <t> [c])) (Const64 <t> [c])))
  1001  
  1002  // floating point optimizations
  1003  (Add32F x (Const32F [0])) -> x
  1004  (Add32F (Const32F [0]) x) -> x
  1005  (Add64F x (Const64F [0])) -> x
  1006  (Add64F (Const64F [0]) x) -> x
  1007  (Sub32F x (Const32F [0])) -> x
  1008  (Sub64F x (Const64F [0])) -> x
  1009  (Mul32F x (Const32F [f2i(1)])) -> x
  1010  (Mul32F (Const32F [f2i(1)]) x) -> x
  1011  (Mul64F x (Const64F [f2i(1)])) -> x
  1012  (Mul64F (Const64F [f2i(1)]) x) -> x
  1013  (Mul32F x (Const32F [f2i(-1)])) -> (Neg32F x)
  1014  (Mul32F (Const32F [f2i(-1)]) x) -> (Neg32F x)
  1015  (Mul64F x (Const64F [f2i(-1)])) -> (Neg64F x)
  1016  (Mul64F (Const64F [f2i(-1)]) x) -> (Neg64F x)
  1017  (Div32F x (Const32F [f2i(1)])) -> x
  1018  (Div64F x (Const64F [f2i(1)])) -> x
  1019  (Div32F x (Const32F [f2i(-1)])) -> (Neg32F x)
  1020  (Div64F x (Const64F [f2i(-1)])) -> (Neg32F x)
  1021  
  1022  (Sqrt (Const64F [c])) -> (Const64F [f2i(math.Sqrt(i2f(c)))])
  1023  
  1024  // recognize runtime.newobject and don't Zero/Nilcheck it
  1025  (Zero (Load (OffPtr [c] (SP)) mem) mem)
  1026          && mem.Op == OpStaticCall
  1027  	&& isSameSym(mem.Aux, "runtime.newobject")
  1028  	&& c == config.ctxt.FixedFrameSize() + config.PtrSize // offset of return value
  1029  	-> mem
  1030  // nil checks just need to rewrite to something useless.
  1031  // they will be deadcode eliminated soon afterwards.
  1032  (NilCheck (Load (OffPtr [c] (SP)) mem) mem)
  1033  	&& mem.Op == OpStaticCall
  1034  	&& isSameSym(mem.Aux, "runtime.newobject")
  1035  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1036  	&& warnRule(config.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check")
  1037  	-> (Invalid)
  1038  (NilCheck (OffPtr (Load (OffPtr [c] (SP)) mem)) mem)
  1039  	&& mem.Op == OpStaticCall
  1040  	&& isSameSym(mem.Aux, "runtime.newobject")
  1041  	&& c == config.ctxt.FixedFrameSize() + config.RegSize // offset of return value
  1042  	&& warnRule(config.Debug_checknil() && v.Pos.Line() > 1, v, "removed nil check")
  1043  	-> (Invalid)