github.com/corona10/go@v0.0.0-20180224231303-7a218942be57/src/cmd/compile/internal/ssa/gen/MIPS.rules (about)

     1  // Copyright 2016 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  (Add(Ptr|32|16|8) x y) -> (ADD x y)
     6  (Add(32|64)F x y) -> (ADD(F|D) x y)
     7  
     8  (Select0 (Add32carry <t> x y)) -> (ADD <t.FieldType(0)> x y)
     9  (Select1 (Add32carry <t> x y)) -> (SGTU <typ.Bool> x (ADD <t.FieldType(0)> x y))
    10  (Add32withcarry <t> x y c) -> (ADD c (ADD <t> x y))
    11  
    12  (Sub(Ptr|32|16|8) x y) -> (SUB x y)
    13  (Sub(32|64)F x y) -> (SUB(F|D) x y)
    14  
    15  (Select0 (Sub32carry <t> x y)) -> (SUB <t.FieldType(0)> x y)
    16  (Select1 (Sub32carry <t> x y)) -> (SGTU <typ.Bool> (SUB <t.FieldType(0)> x y) x)
    17  (Sub32withcarry <t> x y c) -> (SUB (SUB <t> x y) c)
    18  
    19  (Mul(32|16|8) x y) -> (MUL x y)
    20  (Mul(32|64)F x y) -> (MUL(F|D) x y)
    21  
    22  (Hmul(32|32u) x y) -> (Select0 (MUL(T|TU) x y))
    23  (Mul32uhilo x y) -> (MULTU x y)
    24  
    25  (Div32 x y) -> (Select1 (DIV x y))
    26  (Div32u x y) -> (Select1 (DIVU x y))
    27  (Div16 x y) -> (Select1 (DIV (SignExt16to32 x) (SignExt16to32 y)))
    28  (Div16u x y) -> (Select1 (DIVU (ZeroExt16to32 x) (ZeroExt16to32 y)))
    29  (Div8 x y) -> (Select1 (DIV (SignExt8to32 x) (SignExt8to32 y)))
    30  (Div8u x y) -> (Select1 (DIVU (ZeroExt8to32 x) (ZeroExt8to32 y)))
    31  (Div(32|64)F x y) -> (DIV(F|D) x y)
    32  
    33  (Mod32 x y) -> (Select0 (DIV x y))
    34  (Mod32u x y) -> (Select0 (DIVU x y))
    35  (Mod16 x y) -> (Select0 (DIV (SignExt16to32 x) (SignExt16to32 y)))
    36  (Mod16u x y) -> (Select0 (DIVU (ZeroExt16to32 x) (ZeroExt16to32 y)))
    37  (Mod8 x y) -> (Select0 (DIV (SignExt8to32 x) (SignExt8to32 y)))
    38  (Mod8u x y) -> (Select0 (DIVU (ZeroExt8to32 x) (ZeroExt8to32 y)))
    39  
    40  // (x + y) / 2 with x>=y -> (x - y) / 2 + y
    41  (Avg32u <t> x y) -> (ADD (SRLconst <t> (SUB <t> x y) [1]) y)
    42  
    43  (And(32|16|8) x y) -> (AND x y)
    44  (Or(32|16|8) x y) -> (OR x y)
    45  (Xor(32|16|8) x y) -> (XOR x y)
    46  
    47  // constant shifts
    48  // generic opt rewrites all constant shifts to shift by Const64
    49  (Lsh32x64 x (Const64 [c])) && uint32(c) < 32 -> (SLLconst x [c])
    50  (Rsh32x64 x (Const64 [c])) && uint32(c) < 32 -> (SRAconst x [c])
    51  (Rsh32Ux64 x (Const64 [c])) && uint32(c) < 32 -> (SRLconst x [c])
    52  (Lsh16x64 x (Const64 [c])) && uint32(c) < 16 -> (SLLconst x [c])
    53  (Rsh16x64 x (Const64 [c])) && uint32(c) < 16 -> (SRAconst (SLLconst <typ.UInt32> x [16]) [c+16])
    54  (Rsh16Ux64 x (Const64 [c])) && uint32(c) < 16 -> (SRLconst (SLLconst <typ.UInt32> x [16]) [c+16])
    55  (Lsh8x64 x (Const64 [c])) && uint32(c) < 8 -> (SLLconst x [c])
    56  (Rsh8x64 x (Const64 [c])) && uint32(c) < 8 -> (SRAconst (SLLconst <typ.UInt32> x [24]) [c+24])
    57  (Rsh8Ux64 x (Const64 [c])) && uint32(c) < 8 -> (SRLconst (SLLconst <typ.UInt32> x [24]) [c+24])
    58  
    59  // large constant shifts
    60  (Lsh32x64 _ (Const64 [c])) && uint32(c) >= 32 -> (MOVWconst [0])
    61  (Rsh32Ux64 _ (Const64 [c])) && uint32(c) >= 32 -> (MOVWconst [0])
    62  (Lsh16x64 _ (Const64 [c])) && uint32(c) >= 16 -> (MOVWconst [0])
    63  (Rsh16Ux64 _ (Const64 [c])) && uint32(c) >= 16 -> (MOVWconst [0])
    64  (Lsh8x64 _ (Const64 [c])) && uint32(c) >= 8 -> (MOVWconst [0])
    65  (Rsh8Ux64 _ (Const64 [c])) && uint32(c) >= 8 -> (MOVWconst [0])
    66  
    67  // large constant signed right shift, we leave the sign bit
    68  (Rsh32x64 x (Const64 [c])) && uint32(c) >= 32 -> (SRAconst x [31])
    69  (Rsh16x64 x (Const64 [c])) && uint32(c) >= 16 -> (SRAconst (SLLconst <typ.UInt32> x [16]) [31])
    70  (Rsh8x64 x (Const64 [c])) && uint32(c) >= 8 -> (SRAconst (SLLconst <typ.UInt32> x [24]) [31])
    71  
    72  // shifts
    73  // hardware instruction uses only the low 5 bits of the shift
    74  // we compare to 32 to ensure Go semantics for large shifts
    75  (Lsh32x32 <t> x y) -> (CMOVZ (SLL <t> x y) (MOVWconst [0]) (SGTUconst [32] y))
    76  (Lsh32x16 <t> x y) -> (CMOVZ (SLL <t> x (ZeroExt16to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt16to32 y)))
    77  (Lsh32x8 <t> x y) -> (CMOVZ (SLL <t> x (ZeroExt8to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt8to32 y)))
    78  
    79  (Lsh16x32 <t> x y) -> (CMOVZ (SLL <t> x y) (MOVWconst [0]) (SGTUconst [32] y))
    80  (Lsh16x16 <t> x y) -> (CMOVZ (SLL <t> x (ZeroExt16to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt16to32 y)))
    81  (Lsh16x8 <t> x y) -> (CMOVZ (SLL <t> x (ZeroExt8to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt8to32 y)))
    82  
    83  (Lsh8x32 <t> x y) -> (CMOVZ (SLL <t> x y) (MOVWconst [0]) (SGTUconst [32] y))
    84  (Lsh8x16 <t> x y) -> (CMOVZ (SLL <t> x (ZeroExt16to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt16to32 y)))
    85  (Lsh8x8 <t> x y) -> (CMOVZ (SLL <t> x (ZeroExt8to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt8to32 y)))
    86  
    87  (Rsh32Ux32 <t> x y) -> (CMOVZ (SRL <t> x y) (MOVWconst [0]) (SGTUconst [32] y))
    88  (Rsh32Ux16 <t> x y) -> (CMOVZ (SRL <t> x (ZeroExt16to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt16to32 y)))
    89  (Rsh32Ux8 <t> x y) -> (CMOVZ (SRL <t> x (ZeroExt8to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt8to32 y)))
    90  
    91  (Rsh16Ux32 <t> x y) -> (CMOVZ (SRL <t> (ZeroExt16to32 x) y) (MOVWconst [0]) (SGTUconst [32] y))
    92  (Rsh16Ux16 <t> x y) -> (CMOVZ (SRL <t> (ZeroExt16to32 x) (ZeroExt16to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt16to32 y)))
    93  (Rsh16Ux8 <t> x y) -> (CMOVZ (SRL <t> (ZeroExt16to32 x) (ZeroExt8to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt8to32 y)))
    94  
    95  (Rsh8Ux32 <t> x y) -> (CMOVZ (SRL <t> (ZeroExt8to32 x) y) (MOVWconst [0]) (SGTUconst [32] y))
    96  (Rsh8Ux16 <t> x y) -> (CMOVZ (SRL <t> (ZeroExt8to32 x) (ZeroExt16to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt16to32 y)))
    97  (Rsh8Ux8 <t> x y) -> (CMOVZ (SRL <t> (ZeroExt8to32 x) (ZeroExt8to32 y) ) (MOVWconst [0]) (SGTUconst [32] (ZeroExt8to32 y)))
    98  
    99  (Rsh32x32 x y) -> (SRA x ( CMOVZ <typ.UInt32> y (MOVWconst [-1]) (SGTUconst [32] y)))
   100  (Rsh32x16 x y) -> (SRA x ( CMOVZ <typ.UInt32> (ZeroExt16to32 y) (MOVWconst [-1]) (SGTUconst [32] (ZeroExt16to32 y))))
   101  (Rsh32x8 x y) -> (SRA x ( CMOVZ <typ.UInt32> (ZeroExt8to32 y) (MOVWconst [-1]) (SGTUconst [32] (ZeroExt8to32 y))))
   102  
   103  (Rsh16x32 x y) -> (SRA (SignExt16to32 x) ( CMOVZ <typ.UInt32> y (MOVWconst [-1]) (SGTUconst [32] y)))
   104  (Rsh16x16 x y) -> (SRA (SignExt16to32 x) ( CMOVZ <typ.UInt32> (ZeroExt16to32 y) (MOVWconst [-1]) (SGTUconst [32] (ZeroExt16to32 y))))
   105  (Rsh16x8 x y) -> (SRA (SignExt16to32 x) ( CMOVZ <typ.UInt32> (ZeroExt8to32 y) (MOVWconst [-1]) (SGTUconst [32] (ZeroExt8to32 y))))
   106  
   107  (Rsh8x32 x y) -> (SRA (SignExt16to32 x) ( CMOVZ <typ.UInt32> y (MOVWconst [-1]) (SGTUconst [32] y)))
   108  (Rsh8x16 x y) -> (SRA (SignExt16to32 x) ( CMOVZ <typ.UInt32> (ZeroExt16to32 y) (MOVWconst [-1]) (SGTUconst [32] (ZeroExt16to32 y))))
   109  (Rsh8x8 x y) -> (SRA (SignExt16to32 x) ( CMOVZ <typ.UInt32> (ZeroExt8to32 y) (MOVWconst [-1]) (SGTUconst [32] (ZeroExt8to32 y))))
   110  
   111  // unary ops
   112  (Neg(32|16|8) x) -> (NEG x)
   113  (Neg(32|64)F x) -> (NEG(F|D) x)
   114  
   115  (Com(32|16|8) x) -> (NORconst [0] x)
   116  
   117  (Sqrt x) -> (SQRTD x)
   118  
   119  // count trailing zero
   120  // 32 - CLZ(x&-x - 1)
   121  (Ctz32 <t> x) -> (SUB (MOVWconst [32]) (CLZ <t> (SUBconst <t> [1] (AND <t> x (NEG <t> x)))))
   122  
   123  // bit length
   124  (BitLen32 <t> x) -> (SUB (MOVWconst [32]) (CLZ <t> x))
   125  
   126  // boolean ops -- booleans are represented with 0=false, 1=true
   127  (AndB x y) -> (AND x y)
   128  (OrB x y) -> (OR x y)
   129  (EqB x y) -> (XORconst [1] (XOR <typ.Bool> x y))
   130  (NeqB x y) -> (XOR x y)
   131  (Not x) -> (XORconst [1] x)
   132  
   133  // constants
   134  (Const(32|16|8) [val]) -> (MOVWconst [val])
   135  (Const(32|64)F [val]) -> (MOV(F|D)const [val])
   136  (ConstNil) -> (MOVWconst [0])
   137  (ConstBool [b]) -> (MOVWconst [b])
   138  
   139  // truncations
   140  // Because we ignore high parts of registers, truncates are just copies.
   141  (Trunc16to8 x) -> x
   142  (Trunc32to8 x) -> x
   143  (Trunc32to16 x) -> x
   144  
   145  // Zero-/Sign-extensions
   146  (ZeroExt8to16 x) -> (MOVBUreg x)
   147  (ZeroExt8to32 x) -> (MOVBUreg x)
   148  (ZeroExt16to32 x) -> (MOVHUreg x)
   149  
   150  (SignExt8to16 x) -> (MOVBreg x)
   151  (SignExt8to32 x) -> (MOVBreg x)
   152  (SignExt16to32 x) -> (MOVHreg x)
   153  
   154  (Signmask x) -> (SRAconst x [31])
   155  (Zeromask x) -> (NEG (SGTU x (MOVWconst [0])))
   156  (Slicemask <t> x) -> (SRAconst (NEG <t> x) [31])
   157  
   158  // float <-> int conversion
   159  (Cvt32to(32|64)F x) -> (MOVW(F|D) x)
   160  (Cvt(32|64)Fto32 x) -> (TRUNC(F|D)W x)
   161  (Cvt32Fto64F x) -> (MOVFD x)
   162  (Cvt64Fto32F x) -> (MOVDF x)
   163  
   164  (Round(32|64)F x) -> x
   165  
   166  // comparisons
   167  (Eq8 x y)  -> (SGTUconst [1] (XOR (ZeroExt8to32 x) (ZeroExt8to32 y)))
   168  (Eq16 x y) -> (SGTUconst [1] (XOR (ZeroExt16to32 x) (ZeroExt16to32 y)))
   169  (Eq32 x y) -> (SGTUconst [1] (XOR x y))
   170  (EqPtr x y) -> (SGTUconst [1] (XOR x y))
   171  (Eq(32|64)F x y) -> (FPFlagTrue (CMPEQ(F|D) x y))
   172  
   173  (Neq8 x y)  -> (SGTU (XOR (ZeroExt8to32 x) (ZeroExt8to32 y)) (MOVWconst [0]))
   174  (Neq16 x y) -> (SGTU (XOR (ZeroExt16to32 x) (ZeroExt16to32 y)) (MOVWconst [0]))
   175  (Neq32 x y) -> (SGTU (XOR x y) (MOVWconst [0]))
   176  (NeqPtr x y) -> (SGTU (XOR x y) (MOVWconst [0]))
   177  (Neq(32|64)F x y) -> (FPFlagFalse (CMPEQ(F|D) x y))
   178  
   179  (Less8 x y)  -> (SGT (SignExt8to32 y) (SignExt8to32 x))
   180  (Less16 x y) -> (SGT (SignExt16to32 y) (SignExt16to32 x))
   181  (Less32 x y) -> (SGT y x)
   182  (Less(32|64)F x y) -> (FPFlagTrue (CMPGT(F|D) y x)) // reverse operands to work around NaN
   183  
   184  (Less8U x y)  -> (SGTU (ZeroExt8to32 y) (ZeroExt8to32 x))
   185  (Less16U x y) -> (SGTU (ZeroExt16to32 y) (ZeroExt16to32 x))
   186  (Less32U x y) -> (SGTU y x)
   187  
   188  (Leq8 x y)  -> (XORconst [1] (SGT (SignExt8to32 x) (SignExt8to32 y)))
   189  (Leq16 x y) -> (XORconst [1] (SGT (SignExt16to32 x) (SignExt16to32 y)))
   190  (Leq32 x y) -> (XORconst [1] (SGT x y))
   191  (Leq(32|64)F x y) -> (FPFlagTrue (CMPGE(F|D) y x)) // reverse operands to work around NaN
   192  
   193  (Leq8U x y)  -> (XORconst [1] (SGTU (ZeroExt8to32 x) (ZeroExt8to32 y)))
   194  (Leq16U x y) -> (XORconst [1] (SGTU (ZeroExt16to32 x) (ZeroExt16to32 y)))
   195  (Leq32U x y) -> (XORconst [1] (SGTU x y))
   196  
   197  (Greater8 x y)  -> (SGT (SignExt8to32 x) (SignExt8to32 y))
   198  (Greater16 x y) -> (SGT (SignExt16to32 x) (SignExt16to32 y))
   199  (Greater32 x y) -> (SGT x y)
   200  (Greater(32|64)F x y) -> (FPFlagTrue (CMPGT(F|D) x y))
   201  
   202  (Greater8U x y)  -> (SGTU (ZeroExt8to32 x) (ZeroExt8to32 y))
   203  (Greater16U x y) -> (SGTU (ZeroExt16to32 x) (ZeroExt16to32 y))
   204  (Greater32U x y) -> (SGTU x y)
   205  
   206  (Geq8 x y)  -> (XORconst [1] (SGT (SignExt8to32 y) (SignExt8to32 x)))
   207  (Geq16 x y) -> (XORconst [1] (SGT (SignExt16to32 y) (SignExt16to32 x)))
   208  (Geq32 x y) -> (XORconst [1] (SGT y x))
   209  (Geq(32|64)F x y) -> (FPFlagTrue (CMPGE(F|D) x y))
   210  
   211  (Geq8U x y)  -> (XORconst [1] (SGTU (ZeroExt8to32 y) (ZeroExt8to32 x)))
   212  (Geq16U x y) -> (XORconst [1] (SGTU (ZeroExt16to32 y) (ZeroExt16to32 x)))
   213  (Geq32U x y) -> (XORconst [1] (SGTU y x))
   214  
   215  (OffPtr [off] ptr:(SP)) -> (MOVWaddr [off] ptr)
   216  (OffPtr [off] ptr) -> (ADDconst [off] ptr)
   217  
   218  (Addr {sym} base) -> (MOVWaddr {sym} base)
   219  
   220  // loads
   221  (Load <t> ptr mem) && t.IsBoolean() -> (MOVBUload ptr mem)
   222  (Load <t> ptr mem) && (is8BitInt(t) && isSigned(t)) -> (MOVBload ptr mem)
   223  (Load <t> ptr mem) && (is8BitInt(t) && !isSigned(t)) -> (MOVBUload ptr mem)
   224  (Load <t> ptr mem) && (is16BitInt(t) && isSigned(t)) -> (MOVHload ptr mem)
   225  (Load <t> ptr mem) && (is16BitInt(t) && !isSigned(t)) -> (MOVHUload ptr mem)
   226  (Load <t> ptr mem) && (is32BitInt(t) || isPtr(t)) -> (MOVWload ptr mem)
   227  (Load <t> ptr mem) && is32BitFloat(t) -> (MOVFload ptr mem)
   228  (Load <t> ptr mem) && is64BitFloat(t) -> (MOVDload ptr mem)
   229  
   230  // stores
   231  (Store {t} ptr val mem) && t.(*types.Type).Size() == 1 -> (MOVBstore ptr val mem)
   232  (Store {t} ptr val mem) && t.(*types.Type).Size() == 2 -> (MOVHstore ptr val mem)
   233  (Store {t} ptr val mem) && t.(*types.Type).Size() == 4 && !is32BitFloat(val.Type) -> (MOVWstore ptr val mem)
   234  (Store {t} ptr val mem) && t.(*types.Type).Size() == 4 && is32BitFloat(val.Type) -> (MOVFstore ptr val mem)
   235  (Store {t} ptr val mem) && t.(*types.Type).Size() == 8 && is64BitFloat(val.Type) -> (MOVDstore ptr val mem)
   236  
   237  // zero instructions
   238  (Zero [0] _ mem) -> mem
   239  (Zero [1] ptr mem) -> (MOVBstore ptr (MOVWconst [0]) mem)
   240  (Zero [2] {t} ptr mem) && t.(*types.Type).Alignment()%2 == 0 ->
   241  	(MOVHstore ptr (MOVWconst [0]) mem)
   242  (Zero [2] ptr mem) ->
   243  	(MOVBstore [1] ptr (MOVWconst [0])
   244  		(MOVBstore [0] ptr (MOVWconst [0]) mem))
   245  (Zero [4] {t} ptr mem) && t.(*types.Type).Alignment()%4 == 0 ->
   246  	(MOVWstore ptr (MOVWconst [0]) mem)
   247  (Zero [4] {t} ptr mem) && t.(*types.Type).Alignment()%2 == 0 ->
   248  	(MOVHstore [2] ptr (MOVWconst [0])
   249  		(MOVHstore [0] ptr (MOVWconst [0]) mem))
   250  (Zero [4] ptr mem) ->
   251  	(MOVBstore [3] ptr (MOVWconst [0])
   252  		(MOVBstore [2] ptr (MOVWconst [0])
   253  			(MOVBstore [1] ptr (MOVWconst [0])
   254  				(MOVBstore [0] ptr (MOVWconst [0]) mem))))
   255  (Zero [3] ptr mem) ->
   256  	(MOVBstore [2] ptr (MOVWconst [0])
   257  		(MOVBstore [1] ptr (MOVWconst [0])
   258  			(MOVBstore [0] ptr (MOVWconst [0]) mem)))
   259  (Zero [6] {t} ptr mem) && t.(*types.Type).Alignment()%2 == 0 ->
   260  	(MOVHstore [4] ptr (MOVWconst [0])
   261  		(MOVHstore [2] ptr (MOVWconst [0])
   262  			(MOVHstore [0] ptr (MOVWconst [0]) mem)))
   263  (Zero [8] {t} ptr mem) && t.(*types.Type).Alignment()%4 == 0 ->
   264  		(MOVWstore [4] ptr (MOVWconst [0])
   265  			(MOVWstore [0] ptr (MOVWconst [0]) mem))
   266  (Zero [12] {t} ptr mem) && t.(*types.Type).Alignment()%4 == 0 ->
   267  	(MOVWstore [8] ptr (MOVWconst [0])
   268  		(MOVWstore [4] ptr (MOVWconst [0])
   269  			(MOVWstore [0] ptr (MOVWconst [0]) mem)))
   270  (Zero [16] {t} ptr mem) && t.(*types.Type).Alignment()%4 == 0 ->
   271  	(MOVWstore [12] ptr (MOVWconst [0])
   272  		(MOVWstore [8] ptr (MOVWconst [0])
   273  			(MOVWstore [4] ptr (MOVWconst [0])
   274  				(MOVWstore [0] ptr (MOVWconst [0]) mem))))
   275  
   276  // large or unaligned zeroing uses a loop
   277  (Zero [s] {t} ptr mem)
   278  	&& (s > 16  || t.(*types.Type).Alignment()%4 != 0) ->
   279  	(LoweredZero [t.(*types.Type).Alignment()]
   280  		ptr
   281  		(ADDconst <ptr.Type> ptr [s-moveSize(t.(*types.Type).Alignment(), config)])
   282  		mem)
   283  
   284  // moves
   285  (Move [0] _ _ mem) -> mem
   286  (Move [1] dst src mem) -> (MOVBstore dst (MOVBUload src mem) mem)
   287  (Move [2] {t} dst src mem) && t.(*types.Type).Alignment()%2 == 0 ->
   288  	(MOVHstore dst (MOVHUload src mem) mem)
   289  (Move [2] dst src mem) ->
   290  	(MOVBstore [1] dst (MOVBUload [1] src mem)
   291  		(MOVBstore dst (MOVBUload src mem) mem))
   292  (Move [4] {t} dst src mem) && t.(*types.Type).Alignment()%4 == 0 ->
   293  	(MOVWstore dst (MOVWload src mem) mem)
   294  (Move [4] {t} dst src mem) && t.(*types.Type).Alignment()%2 == 0 ->
   295  	(MOVHstore [2] dst (MOVHUload [2] src mem)
   296  		(MOVHstore dst (MOVHUload src mem) mem))
   297  (Move [4] dst src mem) ->
   298  	(MOVBstore [3] dst (MOVBUload [3] src mem)
   299  		(MOVBstore [2] dst (MOVBUload [2] src mem)
   300  			(MOVBstore [1] dst (MOVBUload [1] src mem)
   301  				(MOVBstore dst (MOVBUload src mem) mem))))
   302  (Move [3] dst src mem) ->
   303  	(MOVBstore [2] dst (MOVBUload [2] src mem)
   304  		(MOVBstore [1] dst (MOVBUload [1] src mem)
   305  			(MOVBstore dst (MOVBUload src mem) mem)))
   306  (Move [8] {t} dst src mem) && t.(*types.Type).Alignment()%4 == 0 ->
   307  	(MOVWstore [4] dst (MOVWload [4] src mem)
   308  		(MOVWstore dst (MOVWload src mem) mem))
   309  (Move [8] {t} dst src mem) && t.(*types.Type).Alignment()%2 == 0 ->
   310  	(MOVHstore [6] dst (MOVHload [6] src mem)
   311  		(MOVHstore [4] dst (MOVHload [4] src mem)
   312  			(MOVHstore [2] dst (MOVHload [2] src mem)
   313  				(MOVHstore dst (MOVHload src mem) mem))))
   314  (Move [6] {t} dst src mem) && t.(*types.Type).Alignment()%2 == 0 ->
   315  	(MOVHstore [4] dst (MOVHload [4] src mem)
   316  		(MOVHstore [2] dst (MOVHload [2] src mem)
   317  			(MOVHstore dst (MOVHload src mem) mem)))
   318  (Move [12] {t} dst src mem) && t.(*types.Type).Alignment()%4 == 0 ->
   319  	(MOVWstore [8] dst (MOVWload [8] src mem)
   320  		(MOVWstore [4] dst (MOVWload [4] src mem)
   321  			(MOVWstore dst (MOVWload src mem) mem)))
   322  (Move [16] {t} dst src mem) && t.(*types.Type).Alignment()%4 == 0 ->
   323  	(MOVWstore [12] dst (MOVWload [12] src mem)
   324  		(MOVWstore [8] dst (MOVWload [8] src mem)
   325  			(MOVWstore [4] dst (MOVWload [4] src mem)
   326  				(MOVWstore dst (MOVWload src mem) mem))))
   327  
   328  
   329  // large or unaligned move uses a loop
   330  (Move [s] {t} dst src mem)
   331  	&& (s > 16 || t.(*types.Type).Alignment()%4 != 0) ->
   332  	(LoweredMove [t.(*types.Type).Alignment()]
   333  		dst
   334  		src
   335  		(ADDconst <src.Type> src [s-moveSize(t.(*types.Type).Alignment(), config)])
   336  		mem)
   337  
   338  // calls
   339  (StaticCall [argwid] {target} mem) -> (CALLstatic [argwid] {target} mem)
   340  (ClosureCall [argwid] entry closure mem) -> (CALLclosure [argwid] entry closure mem)
   341  (InterCall [argwid] entry mem) -> (CALLinter [argwid] entry mem)
   342  
   343  // atomic intrinsics
   344  (AtomicLoad32  ptr mem) -> (LoweredAtomicLoad ptr mem)
   345  (AtomicLoadPtr ptr mem) -> (LoweredAtomicLoad  ptr mem)
   346  
   347  (AtomicStore32      ptr val mem) -> (LoweredAtomicStore ptr val mem)
   348  (AtomicStorePtrNoWB ptr val mem) -> (LoweredAtomicStore  ptr val mem)
   349  
   350  (AtomicExchange32 ptr val mem) -> (LoweredAtomicExchange ptr val mem)
   351  (AtomicAdd32 ptr val mem) -> (LoweredAtomicAdd ptr val mem)
   352  
   353  (AtomicCompareAndSwap32 ptr old new_ mem) -> (LoweredAtomicCas ptr old new_ mem)
   354  
   355  // AtomicOr8(ptr,val) -> LoweredAtomicOr(ptr&^3,uint32(val) << ((ptr & 3) * 8))
   356  (AtomicOr8 ptr val mem) && !config.BigEndian ->
   357  	(LoweredAtomicOr (AND <typ.UInt32Ptr> (MOVWconst [^3]) ptr)
   358  		(SLL <typ.UInt32> (ZeroExt8to32 val)
   359  			(SLLconst <typ.UInt32> [3]
   360  				(ANDconst <typ.UInt32> [3] ptr))) mem)
   361  
   362  // AtomicAnd8(ptr,val) -> LoweredAtomicAnd(ptr&^3,(uint32(val) << ((ptr & 3) * 8)) | ^(uint32(0xFF) << ((ptr & 3) * 8))))
   363  (AtomicAnd8  ptr val mem) && !config.BigEndian ->
   364  	(LoweredAtomicAnd (AND <typ.UInt32Ptr> (MOVWconst [^3]) ptr)
   365  		(OR <typ.UInt32> (SLL <typ.UInt32> (ZeroExt8to32 val)
   366  			(SLLconst <typ.UInt32> [3]
   367  				(ANDconst  <typ.UInt32> [3] ptr)))
   368  		(NORconst [0] <typ.UInt32> (SLL <typ.UInt32>
   369  			(MOVWconst [0xff]) (SLLconst <typ.UInt32> [3]
   370  				(ANDconst <typ.UInt32> [3] ptr))))) mem)
   371  
   372  // AtomicOr8(ptr,val) -> LoweredAtomicOr(ptr&^3,uint32(val) << (((ptr^3) & 3) * 8))
   373  (AtomicOr8 ptr val mem) && config.BigEndian ->
   374  	(LoweredAtomicOr (AND <typ.UInt32Ptr> (MOVWconst [^3]) ptr)
   375  		(SLL <typ.UInt32> (ZeroExt8to32 val)
   376  			(SLLconst <typ.UInt32> [3]
   377  				(ANDconst <typ.UInt32> [3]
   378  					(XORconst <typ.UInt32> [3] ptr)))) mem)
   379  
   380  // AtomicAnd8(ptr,val) -> LoweredAtomicAnd(ptr&^3,(uint32(val) << (((ptr^3) & 3) * 8)) | ^(uint32(0xFF) << (((ptr^3) & 3) * 8))))
   381  (AtomicAnd8  ptr val mem) && config.BigEndian ->
   382  	(LoweredAtomicAnd (AND <typ.UInt32Ptr> (MOVWconst [^3]) ptr)
   383  		(OR <typ.UInt32> (SLL <typ.UInt32> (ZeroExt8to32 val)
   384  			(SLLconst <typ.UInt32> [3]
   385  				(ANDconst  <typ.UInt32> [3]
   386  					(XORconst <typ.UInt32> [3] ptr))))
   387  		(NORconst [0] <typ.UInt32> (SLL <typ.UInt32>
   388  			(MOVWconst [0xff]) (SLLconst <typ.UInt32> [3]
   389  				(ANDconst <typ.UInt32> [3]
   390  					(XORconst <typ.UInt32> [3] ptr)))))) mem)
   391  
   392  
   393  // checks
   394  (NilCheck ptr mem) -> (LoweredNilCheck ptr mem)
   395  (IsNonNil ptr) -> (SGTU ptr (MOVWconst [0]))
   396  (IsInBounds idx len) -> (SGTU len idx)
   397  (IsSliceInBounds idx len) -> (XORconst [1] (SGTU idx len))
   398  
   399  // pseudo-ops
   400  (GetClosurePtr) -> (LoweredGetClosurePtr)
   401  (GetCallerSP) -> (LoweredGetCallerSP)
   402  (Convert x mem) -> (MOVWconvert x mem)
   403  
   404  (If cond yes no) -> (NE cond yes no)
   405  
   406  // Write barrier.
   407  (WB {fn} destptr srcptr mem) -> (LoweredWB {fn} destptr srcptr mem)
   408  
   409  
   410  // Optimizations
   411  
   412  // Absorb boolean tests into block
   413  (NE (FPFlagTrue cmp) yes no) -> (FPT cmp yes no)
   414  (NE (FPFlagFalse cmp) yes no) -> (FPF cmp yes no)
   415  (EQ (FPFlagTrue cmp) yes no) -> (FPF cmp yes no)
   416  (EQ (FPFlagFalse cmp) yes no) -> (FPT cmp yes no)
   417  (NE (XORconst [1] cmp:(SGT _ _)) yes no) -> (EQ cmp yes no)
   418  (NE (XORconst [1] cmp:(SGTU _ _)) yes no) -> (EQ cmp yes no)
   419  (NE (XORconst [1] cmp:(SGTconst _)) yes no) -> (EQ cmp yes no)
   420  (NE (XORconst [1] cmp:(SGTUconst _)) yes no) -> (EQ cmp yes no)
   421  (NE (XORconst [1] cmp:(SGTzero _)) yes no) -> (EQ cmp yes no)
   422  (NE (XORconst [1] cmp:(SGTUzero _)) yes no) -> (EQ cmp yes no)
   423  (EQ (XORconst [1] cmp:(SGT _ _)) yes no) -> (NE cmp yes no)
   424  (EQ (XORconst [1] cmp:(SGTU _ _)) yes no) -> (NE cmp yes no)
   425  (EQ (XORconst [1] cmp:(SGTconst _)) yes no) -> (NE cmp yes no)
   426  (EQ (XORconst [1] cmp:(SGTUconst _)) yes no) -> (NE cmp yes no)
   427  (EQ (XORconst [1] cmp:(SGTzero _)) yes no) -> (NE cmp yes no)
   428  (EQ (XORconst [1] cmp:(SGTUzero _)) yes no) -> (NE cmp yes no)
   429  (NE (SGTUconst [1] x) yes no) -> (EQ x yes no)
   430  (EQ (SGTUconst [1] x) yes no) -> (NE x yes no)
   431  (NE (SGTUzero x) yes no) -> (NE x yes no)
   432  (EQ (SGTUzero x) yes no) -> (EQ x yes no)
   433  (NE (SGTconst [0] x) yes no) -> (LTZ x yes no)
   434  (EQ (SGTconst [0] x) yes no) -> (GEZ x yes no)
   435  (NE (SGTzero x) yes no) -> (GTZ x yes no)
   436  (EQ (SGTzero x) yes no) -> (LEZ x yes no)
   437  
   438  // fold offset into address
   439  (ADDconst [off1] (MOVWaddr [off2] {sym} ptr)) -> (MOVWaddr [off1+off2] {sym} ptr)
   440  
   441  // fold address into load/store
   442  (MOVBload  [off1] {sym} x:(ADDconst [off2] ptr) mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVBload  [off1+off2] {sym} ptr mem)
   443  (MOVBUload [off1] {sym} x:(ADDconst [off2] ptr) mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVBUload [off1+off2] {sym} ptr mem)
   444  (MOVHload  [off1] {sym} x:(ADDconst [off2] ptr) mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVHload  [off1+off2] {sym} ptr mem)
   445  (MOVHUload [off1] {sym} x:(ADDconst [off2] ptr) mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVHUload [off1+off2] {sym} ptr mem)
   446  (MOVWload  [off1] {sym} x:(ADDconst [off2] ptr) mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVWload  [off1+off2] {sym} ptr mem)
   447  (MOVFload  [off1] {sym} x:(ADDconst [off2] ptr) mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVFload  [off1+off2] {sym} ptr mem)
   448  (MOVDload  [off1] {sym} x:(ADDconst [off2] ptr) mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVDload  [off1+off2] {sym} ptr mem)
   449  
   450  (MOVBstore [off1] {sym} x:(ADDconst [off2] ptr) val mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVBstore [off1+off2] {sym} ptr val mem)
   451  (MOVHstore [off1] {sym} x:(ADDconst [off2] ptr) val mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVHstore [off1+off2] {sym} ptr val mem)
   452  (MOVWstore [off1] {sym} x:(ADDconst [off2] ptr) val mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVWstore [off1+off2] {sym} ptr val mem)
   453  (MOVFstore [off1] {sym} x:(ADDconst [off2] ptr) val mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVFstore [off1+off2] {sym} ptr val mem)
   454  (MOVDstore [off1] {sym} x:(ADDconst [off2] ptr) val mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVDstore [off1+off2] {sym} ptr val mem)
   455  
   456  (MOVBstorezero [off1] {sym} x:(ADDconst [off2] ptr) mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVBstorezero [off1+off2] {sym} ptr mem)
   457  (MOVHstorezero [off1] {sym} x:(ADDconst [off2] ptr) mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVHstorezero [off1+off2] {sym} ptr mem)
   458  (MOVWstorezero [off1] {sym} x:(ADDconst [off2] ptr) mem) && (is16Bit(off1+off2) || x.Uses == 1) -> (MOVWstorezero [off1+off2] {sym} ptr mem)
   459  
   460  (MOVBload [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   461  	(MOVBload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   462  (MOVBUload [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   463  	(MOVBUload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   464  (MOVHload [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   465  	(MOVHload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   466  (MOVHUload [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   467  	(MOVHUload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   468  (MOVWload [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   469  	(MOVWload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   470  (MOVFload [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   471  	(MOVFload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   472  (MOVDload [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   473  	(MOVDload [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   474  
   475  (MOVBstore [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   476  	(MOVBstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   477  (MOVHstore [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   478  	(MOVHstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   479  (MOVWstore [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   480  	(MOVWstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   481  (MOVFstore [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   482  	(MOVFstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   483  (MOVDstore [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) val mem) && canMergeSym(sym1,sym2) ->
   484  	(MOVDstore [off1+off2] {mergeSym(sym1,sym2)} ptr val mem)
   485  (MOVBstorezero [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   486  	(MOVBstorezero [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   487  (MOVHstorezero [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   488  	(MOVHstorezero [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   489  (MOVWstorezero [off1] {sym1} (MOVWaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) ->
   490  	(MOVWstorezero [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
   491  
   492  // replace load from same location as preceding store with zero/sign extension (or copy in case of full width)
   493  (MOVBload [off] {sym} ptr (MOVBstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> (MOVBreg x)
   494  (MOVBUload [off] {sym} ptr (MOVBstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> (MOVBUreg x)
   495  (MOVHload [off] {sym} ptr (MOVHstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> (MOVHreg x)
   496  (MOVHUload [off] {sym} ptr (MOVHstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> (MOVHUreg x)
   497  (MOVWload [off] {sym} ptr (MOVWstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> x
   498  (MOVFload [off] {sym} ptr (MOVFstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> x
   499  (MOVDload [off] {sym} ptr (MOVDstore [off2] {sym2} ptr2 x _)) && sym == sym2 && off == off2 && isSamePtr(ptr, ptr2) -> x
   500  
   501  // store zero
   502  (MOVBstore [off] {sym} ptr (MOVWconst [0]) mem) -> (MOVBstorezero [off] {sym} ptr mem)
   503  (MOVHstore [off] {sym} ptr (MOVWconst [0]) mem) -> (MOVHstorezero [off] {sym} ptr mem)
   504  (MOVWstore [off] {sym} ptr (MOVWconst [0]) mem) -> (MOVWstorezero [off] {sym} ptr mem)
   505  
   506  // don't extend after proper load
   507  (MOVBreg x:(MOVBload _ _)) -> (MOVWreg x)
   508  (MOVBUreg x:(MOVBUload _ _)) -> (MOVWreg x)
   509  (MOVHreg x:(MOVBload _ _)) -> (MOVWreg x)
   510  (MOVHreg x:(MOVBUload _ _)) -> (MOVWreg x)
   511  (MOVHreg x:(MOVHload _ _)) -> (MOVWreg x)
   512  (MOVHUreg x:(MOVBUload _ _)) -> (MOVWreg x)
   513  (MOVHUreg x:(MOVHUload _ _)) -> (MOVWreg x)
   514  
   515  // fold double extensions
   516  (MOVBreg x:(MOVBreg _)) -> (MOVWreg x)
   517  (MOVBUreg x:(MOVBUreg _)) -> (MOVWreg x)
   518  (MOVHreg x:(MOVBreg _)) -> (MOVWreg x)
   519  (MOVHreg x:(MOVBUreg _)) -> (MOVWreg x)
   520  (MOVHreg x:(MOVHreg _)) -> (MOVWreg x)
   521  (MOVHUreg x:(MOVBUreg _)) -> (MOVWreg x)
   522  (MOVHUreg x:(MOVHUreg _)) -> (MOVWreg x)
   523  
   524  // sign extended loads
   525  // Note: The combined instruction must end up in the same block
   526  // as the original load. If not, we end up making a value with
   527  // memory type live in two different blocks, which can lead to
   528  // multiple memory values alive simultaneously.
   529  // Make sure we don't combine these ops if the load has another use.
   530  // This prevents a single load from being split into multiple loads
   531  // which then might return different values.  See test/atomicload.go.
   532  (MOVBreg <t> x:(MOVBUload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBload <t> [off] {sym} ptr mem)
   533  (MOVBUreg <t> x:(MOVBload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVBUload <t> [off] {sym} ptr mem)
   534  (MOVHreg <t> x:(MOVHUload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVHload <t> [off] {sym} ptr mem)
   535  (MOVHUreg <t> x:(MOVHload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) -> @x.Block (MOVHUload <t> [off] {sym} ptr mem)
   536  
   537  // fold extensions and ANDs together
   538  (MOVBUreg (ANDconst [c] x)) -> (ANDconst [c&0xff] x)
   539  (MOVHUreg (ANDconst [c] x)) -> (ANDconst [c&0xffff] x)
   540  (MOVBreg (ANDconst [c] x)) && c & 0x80 == 0 -> (ANDconst [c&0x7f] x)
   541  (MOVHreg (ANDconst [c] x)) && c & 0x8000 == 0 -> (ANDconst [c&0x7fff] x)
   542  
   543  // don't extend before store
   544  (MOVBstore [off] {sym} ptr (MOVBreg x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   545  (MOVBstore [off] {sym} ptr (MOVBUreg x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   546  (MOVBstore [off] {sym} ptr (MOVHreg x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   547  (MOVBstore [off] {sym} ptr (MOVHUreg x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   548  (MOVBstore [off] {sym} ptr (MOVWreg x) mem) -> (MOVBstore [off] {sym} ptr x mem)
   549  (MOVHstore [off] {sym} ptr (MOVHreg x) mem) -> (MOVHstore [off] {sym} ptr x mem)
   550  (MOVHstore [off] {sym} ptr (MOVHUreg x) mem) -> (MOVHstore [off] {sym} ptr x mem)
   551  (MOVHstore [off] {sym} ptr (MOVWreg x) mem) -> (MOVHstore [off] {sym} ptr x mem)
   552  (MOVWstore [off] {sym} ptr (MOVWreg x) mem) -> (MOVWstore [off] {sym} ptr x mem)
   553  
   554  // if a register move has only 1 use, just use the same register without emitting instruction
   555  // MOVWnop doesn't emit instruction, only for ensuring the type.
   556  (MOVWreg x) && x.Uses == 1 -> (MOVWnop x)
   557  
   558  // fold constant into arithmatic ops
   559  (ADD x (MOVWconst [c])) -> (ADDconst [c] x)
   560  (SUB x (MOVWconst [c])) -> (SUBconst [c] x)
   561  (AND x (MOVWconst [c])) -> (ANDconst [c] x)
   562  (OR  x (MOVWconst [c])) -> (ORconst  [c] x)
   563  (XOR x (MOVWconst [c])) -> (XORconst [c] x)
   564  (NOR x (MOVWconst [c])) -> (NORconst [c] x)
   565  
   566  (SLL _ (MOVWconst [c])) && uint32(c)>=32 -> (MOVWconst [0])
   567  (SRL _ (MOVWconst [c])) && uint32(c)>=32 -> (MOVWconst [0])
   568  (SRA x (MOVWconst [c])) && uint32(c)>=32 -> (SRAconst x [31])
   569  (SLL x (MOVWconst [c])) -> (SLLconst x [c])
   570  (SRL x (MOVWconst [c])) -> (SRLconst x [c])
   571  (SRA x (MOVWconst [c])) -> (SRAconst x [c])
   572  
   573  (SGT  (MOVWconst [c]) x) -> (SGTconst  [c] x)
   574  (SGTU (MOVWconst [c]) x) -> (SGTUconst [c] x)
   575  (SGT x (MOVWconst [0])) -> (SGTzero x)
   576  (SGTU x (MOVWconst [0])) -> (SGTUzero x)
   577  
   578  // mul with constant
   579  (Select1 (MULTU (MOVWconst [0]) _ )) -> (MOVWconst [0])
   580  (Select0 (MULTU (MOVWconst [0]) _ )) -> (MOVWconst [0])
   581  (Select1 (MULTU (MOVWconst [1]) x )) -> x
   582  (Select0 (MULTU (MOVWconst [1]) _ )) -> (MOVWconst [0])
   583  (Select1 (MULTU (MOVWconst [-1]) x )) -> (NEG <x.Type> x)
   584  (Select0 (MULTU (MOVWconst [-1]) x )) -> (CMOVZ (ADDconst <x.Type> [-1] x) (MOVWconst [0]) x)
   585  (Select1 (MULTU (MOVWconst [c]) x )) && isPowerOfTwo(int64(uint32(c))) -> (SLLconst [log2(int64(uint32(c)))] x)
   586  (Select0 (MULTU (MOVWconst [c]) x )) && isPowerOfTwo(int64(uint32(c))) -> (SRLconst [32-log2(int64(uint32(c)))] x)
   587  
   588  (MUL (MOVWconst [0]) _ ) -> (MOVWconst [0])
   589  (MUL (MOVWconst [1]) x ) -> x
   590  (MUL (MOVWconst [-1]) x ) -> (NEG x)
   591  (MUL (MOVWconst [c]) x ) && isPowerOfTwo(int64(uint32(c))) -> (SLLconst [log2(int64(uint32(c)))] x)
   592  
   593  // generic simplifications
   594  (ADD x (NEG y)) -> (SUB x y)
   595  (SUB x x) -> (MOVWconst [0])
   596  (SUB (MOVWconst [0]) x) -> (NEG x)
   597  (AND x x) -> x
   598  (OR  x x) -> x
   599  (XOR x x) -> (MOVWconst [0])
   600  
   601  // miscellaneous patterns generated by dec64
   602  (AND (SGTUconst [1] x) (SGTUconst [1] y)) ->  (SGTUconst [1] (OR <x.Type> x y))
   603  (OR (SGTUzero x) (SGTUzero y)) ->  (SGTUzero (OR <x.Type> x y))
   604  
   605  // remove redundant *const ops
   606  (ADDconst [0]  x) -> x
   607  (SUBconst [0]  x) -> x
   608  (ANDconst [0]  _) -> (MOVWconst [0])
   609  (ANDconst [-1] x) -> x
   610  (ORconst  [0]  x) -> x
   611  (ORconst  [-1] _) -> (MOVWconst [-1])
   612  (XORconst [0]  x) -> x
   613  (XORconst [-1] x) -> (NORconst [0] x)
   614  
   615  // generic constant folding
   616  (ADDconst [c] (MOVWconst [d]))  -> (MOVWconst [int64(int32(c+d))])
   617  (ADDconst [c] (ADDconst [d] x)) -> (ADDconst [int64(int32(c+d))] x)
   618  (ADDconst [c] (SUBconst [d] x)) -> (ADDconst [int64(int32(c-d))] x)
   619  (SUBconst [c] (MOVWconst [d]))  -> (MOVWconst [int64(int32(d-c))])
   620  (SUBconst [c] (SUBconst [d] x)) -> (ADDconst [int64(int32(-c-d))] x)
   621  (SUBconst [c] (ADDconst [d] x)) -> (ADDconst [int64(int32(-c+d))] x)
   622  (SLLconst [c] (MOVWconst [d]))  -> (MOVWconst [int64(int32(uint32(d)<<uint32(c)))])
   623  (SRLconst [c] (MOVWconst [d]))  -> (MOVWconst [int64(uint32(d)>>uint32(c))])
   624  (SRAconst [c] (MOVWconst [d]))  -> (MOVWconst [int64(int32(d)>>uint32(c))])
   625  (MUL (MOVWconst [c]) (MOVWconst [d])) -> (MOVWconst [int64(int32(c)*int32(d))])
   626  (Select1 (MULTU  (MOVWconst [c]) (MOVWconst [d]))) -> (MOVWconst [int64(int32(uint32(c)*uint32(d)))])
   627  (Select0 (MULTU  (MOVWconst [c]) (MOVWconst [d]))) -> (MOVWconst [(c*d)>>32])
   628  (Select1 (DIV  (MOVWconst [c]) (MOVWconst [d]))) -> (MOVWconst [int64(int32(c)/int32(d))])
   629  (Select1 (DIVU (MOVWconst [c]) (MOVWconst [d]))) -> (MOVWconst [int64(int32(uint32(c)/uint32(d)))])
   630  (Select0 (DIV  (MOVWconst [c]) (MOVWconst [d]))) -> (MOVWconst [int64(int32(c)%int32(d))])
   631  (Select0 (DIVU (MOVWconst [c]) (MOVWconst [d]))) -> (MOVWconst [int64(int32(uint32(c)%uint32(d)))])
   632  (ANDconst [c] (MOVWconst [d])) -> (MOVWconst [c&d])
   633  (ANDconst [c] (ANDconst [d] x)) -> (ANDconst [c&d] x)
   634  (ORconst [c] (MOVWconst [d])) -> (MOVWconst [c|d])
   635  (ORconst [c] (ORconst [d] x)) -> (ORconst [c|d] x)
   636  (XORconst [c] (MOVWconst [d])) -> (MOVWconst [c^d])
   637  (XORconst [c] (XORconst [d] x)) -> (XORconst [c^d] x)
   638  (NORconst [c] (MOVWconst [d])) -> (MOVWconst [^(c|d)])
   639  (NEG (MOVWconst [c])) -> (MOVWconst [int64(int32(-c))])
   640  (MOVBreg  (MOVWconst [c])) -> (MOVWconst [int64(int8(c))])
   641  (MOVBUreg (MOVWconst [c])) -> (MOVWconst [int64(uint8(c))])
   642  (MOVHreg  (MOVWconst [c])) -> (MOVWconst [int64(int16(c))])
   643  (MOVHUreg (MOVWconst [c])) -> (MOVWconst [int64(uint16(c))])
   644  (MOVWreg  (MOVWconst [c])) -> (MOVWconst [c])
   645  
   646  // constant comparisons
   647  (SGTconst [c] (MOVWconst [d])) && int32(c) > int32(d) -> (MOVWconst [1])
   648  (SGTconst [c] (MOVWconst [d])) && int32(c) <= int32(d) -> (MOVWconst [0])
   649  (SGTUconst [c] (MOVWconst [d])) && uint32(c)>uint32(d) -> (MOVWconst [1])
   650  (SGTUconst [c] (MOVWconst [d])) && uint32(c)<=uint32(d) -> (MOVWconst [0])
   651  (SGTzero (MOVWconst [d])) && int32(d) > 0 -> (MOVWconst [1])
   652  (SGTzero (MOVWconst [d])) && int32(d) <= 0 -> (MOVWconst [0])
   653  (SGTUzero (MOVWconst [d])) && uint32(d) != 0 -> (MOVWconst [1])
   654  (SGTUzero (MOVWconst [d])) && uint32(d) == 0 -> (MOVWconst [0])
   655  
   656  // other known comparisons
   657  (SGTconst [c] (MOVBreg _)) && 0x7f < int32(c) -> (MOVWconst [1])
   658  (SGTconst [c] (MOVBreg _)) && int32(c) <= -0x80 -> (MOVWconst [0])
   659  (SGTconst [c] (MOVBUreg _)) && 0xff < int32(c) -> (MOVWconst [1])
   660  (SGTconst [c] (MOVBUreg _)) && int32(c) < 0 -> (MOVWconst [0])
   661  (SGTUconst [c] (MOVBUreg _)) && 0xff < uint32(c) -> (MOVWconst [1])
   662  (SGTconst [c] (MOVHreg _)) && 0x7fff < int32(c) -> (MOVWconst [1])
   663  (SGTconst [c] (MOVHreg _)) && int32(c) <= -0x8000 -> (MOVWconst [0])
   664  (SGTconst [c] (MOVHUreg _)) && 0xffff < int32(c) -> (MOVWconst [1])
   665  (SGTconst [c] (MOVHUreg _)) && int32(c) < 0 -> (MOVWconst [0])
   666  (SGTUconst [c] (MOVHUreg _)) && 0xffff < uint32(c) -> (MOVWconst [1])
   667  (SGTconst [c] (ANDconst [m] _)) && 0 <= int32(m) && int32(m) < int32(c) -> (MOVWconst [1])
   668  (SGTUconst [c] (ANDconst [m] _)) && uint32(m) < uint32(c) -> (MOVWconst [1])
   669  (SGTconst [c] (SRLconst _ [d])) && 0 <= int32(c) && uint32(d) <= 31 && 1<<(32-uint32(d)) <= int32(c) -> (MOVWconst [1])
   670  (SGTUconst [c] (SRLconst _ [d])) && uint32(d) <= 31 && 1<<(32-uint32(d)) <= uint32(c) -> (MOVWconst [1])
   671  
   672  // absorb constants into branches
   673  (EQ  (MOVWconst [0]) yes no) -> (First nil yes no)
   674  (EQ  (MOVWconst [c]) yes no) && c != 0 -> (First nil no yes)
   675  (NE  (MOVWconst [0]) yes no) -> (First nil no yes)
   676  (NE  (MOVWconst [c]) yes no) && c != 0 -> (First nil yes no)
   677  (LTZ (MOVWconst [c]) yes no) && int32(c) <  0 -> (First nil yes no)
   678  (LTZ (MOVWconst [c]) yes no) && int32(c) >= 0 -> (First nil no yes)
   679  (LEZ (MOVWconst [c]) yes no) && int32(c) <= 0 -> (First nil yes no)
   680  (LEZ (MOVWconst [c]) yes no) && int32(c) >  0 -> (First nil no yes)
   681  (GTZ (MOVWconst [c]) yes no) && int32(c) >  0 -> (First nil yes no)
   682  (GTZ (MOVWconst [c]) yes no) && int32(c) <= 0 -> (First nil no yes)
   683  (GEZ (MOVWconst [c]) yes no) && int32(c) >= 0 -> (First nil yes no)
   684  (GEZ (MOVWconst [c]) yes no) && int32(c) <  0 -> (First nil no yes)
   685  
   686  // conditional move
   687  (CMOVZ _ b (MOVWconst [0])) -> b
   688  (CMOVZ a _ (MOVWconst [c])) && c!=0 -> a
   689  (CMOVZzero _ (MOVWconst [0])) -> (MOVWconst [0])
   690  (CMOVZzero a (MOVWconst [c])) && c!=0 -> a
   691  (CMOVZ a (MOVWconst [0]) c) -> (CMOVZzero a c)
   692  
   693  // atomic
   694  (LoweredAtomicStore ptr (MOVWconst [0]) mem) -> (LoweredAtomicStorezero ptr mem)
   695  (LoweredAtomicAdd ptr (MOVWconst [c]) mem) && is16Bit(c) -> (LoweredAtomicAddconst [c] ptr mem)
   696