Text file src/cmd/compile/internal/ssa/_gen/MIPS.rules

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

View as plain text