]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/ssa/_gen/RISCV64.rules
4cacabb2369c9e575b213e4a013ad3cfbeef9c13
[gostls13.git] / src / cmd / compile / internal / ssa / _gen / RISCV64.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 // Lowering arithmetic
6 (Add(Ptr|64|32|16|8) ...) => (ADD ...)
7 (Add(64|32)F ...) => (FADD(D|S) ...)
8
9 (Sub(Ptr|64|32|16|8) ...) => (SUB ...)
10 (Sub(64|32)F ...) => (FSUB(D|S) ...)
11
12 (Mul64 ...) => (MUL  ...)
13 (Mul64uhilo ...) => (LoweredMuluhilo ...)
14 (Mul64uover ...) => (LoweredMuluover ...)
15 (Mul32 ...) => (MULW ...)
16 (Mul16 x y) => (MULW (SignExt16to32 x) (SignExt16to32 y))
17 (Mul8 x y)  => (MULW (SignExt8to32 x)  (SignExt8to32 y))
18 (Mul(64|32)F ...) => (FMUL(D|S) ...)
19
20 (Div(64|32)F ...) => (FDIV(D|S) ...)
21
22 (Div64 x y [false])  => (DIV x y)
23 (Div64u ...) => (DIVU ...)
24 (Div32 x y [false])  => (DIVW x y)
25 (Div32u ...) => (DIVUW ...)
26 (Div16 x y [false])  => (DIVW  (SignExt16to32 x) (SignExt16to32 y))
27 (Div16u x y) => (DIVUW (ZeroExt16to32 x) (ZeroExt16to32 y))
28 (Div8 x y)   => (DIVW  (SignExt8to32 x)  (SignExt8to32 y))
29 (Div8u x y)  => (DIVUW (ZeroExt8to32 x)  (ZeroExt8to32 y))
30
31 (Hmul64 ...)  => (MULH  ...)
32 (Hmul64u ...) => (MULHU ...)
33 (Hmul32 x y)  => (SRAI [32] (MUL  (SignExt32to64 x) (SignExt32to64 y)))
34 (Hmul32u x y) => (SRLI [32] (MUL  (ZeroExt32to64 x) (ZeroExt32to64 y)))
35
36 (Select0 (Add64carry x y c)) => (ADD (ADD <typ.UInt64> x y) c)
37 (Select1 (Add64carry x y c)) =>
38         (OR (SLTU <typ.UInt64> s:(ADD <typ.UInt64> x y) x) (SLTU <typ.UInt64> (ADD <typ.UInt64> s c) s))
39
40 (Select0 (Sub64borrow x y c)) => (SUB (SUB <typ.UInt64> x y) c)
41 (Select1 (Sub64borrow x y c)) =>
42         (OR (SLTU <typ.UInt64> x s:(SUB <typ.UInt64> x y)) (SLTU <typ.UInt64> s (SUB <typ.UInt64> s c)))
43
44 // (x + y) / 2 => (x / 2) + (y / 2) + (x & y & 1)
45 (Avg64u <t> x y) => (ADD (ADD <t> (SRLI <t> [1] x) (SRLI <t> [1] y)) (ANDI <t> [1] (AND <t> x y)))
46
47 (Mod64 x y [false])  => (REM x y)
48 (Mod64u ...) => (REMU  ...)
49 (Mod32 x y [false])  => (REMW x y)
50 (Mod32u ...) => (REMUW ...)
51 (Mod16 x y [false])  => (REMW  (SignExt16to32 x) (SignExt16to32 y))
52 (Mod16u x y) => (REMUW (ZeroExt16to32 x) (ZeroExt16to32 y))
53 (Mod8 x y)   => (REMW  (SignExt8to32  x) (SignExt8to32  y))
54 (Mod8u x y)  => (REMUW (ZeroExt8to32  x) (ZeroExt8to32  y))
55
56 (And(64|32|16|8) ...) => (AND ...)
57 (Or(64|32|16|8) ...) => (OR ...)
58 (Xor(64|32|16|8) ...) => (XOR ...)
59
60 (Neg(64|32|16|8) ...) => (NEG ...)
61 (Neg(64|32)F ...) => (FNEG(D|S) ...)
62
63 (Com(64|32|16|8) ...) => (NOT ...)
64
65
66 (Sqrt ...) => (FSQRTD ...)
67 (Sqrt32 ...) => (FSQRTS ...)
68
69 (Copysign ...) => (FSGNJD ...)
70
71 (Abs ...) => (FABSD ...)
72
73 (FMA ...) => (FMADDD ...)
74
75 // Sign and zero extension.
76
77 (SignExt8to16  ...) => (MOVBreg ...)
78 (SignExt8to32  ...) => (MOVBreg ...)
79 (SignExt8to64  ...) => (MOVBreg ...)
80 (SignExt16to32 ...) => (MOVHreg ...)
81 (SignExt16to64 ...) => (MOVHreg ...)
82 (SignExt32to64 ...) => (MOVWreg ...)
83
84 (ZeroExt8to16  ...) => (MOVBUreg ...)
85 (ZeroExt8to32  ...) => (MOVBUreg ...)
86 (ZeroExt8to64  ...) => (MOVBUreg ...)
87 (ZeroExt16to32 ...) => (MOVHUreg ...)
88 (ZeroExt16to64 ...) => (MOVHUreg ...)
89 (ZeroExt32to64 ...) => (MOVWUreg ...)
90
91 (Cvt32to32F ...) => (FCVTSW ...)
92 (Cvt32to64F ...) => (FCVTDW ...)
93 (Cvt64to32F ...) => (FCVTSL ...)
94 (Cvt64to64F ...) => (FCVTDL ...)
95
96 (Cvt32Fto32 ...) => (FCVTWS ...)
97 (Cvt32Fto64 ...) => (FCVTLS ...)
98 (Cvt64Fto32 ...) => (FCVTWD ...)
99 (Cvt64Fto64 ...) => (FCVTLD ...)
100
101 (Cvt32Fto64F ...) => (FCVTDS ...)
102 (Cvt64Fto32F ...) => (FCVTSD ...)
103
104 (CvtBoolToUint8 ...) => (Copy ...)
105
106 (Round(32|64)F ...) => (LoweredRound(32|64)F ...)
107
108 (Slicemask <t> x) => (SRAI [63] (NEG <t> x))
109
110 // Truncations
111 // We ignore the unused high parts of registers, so truncates are just copies.
112 (Trunc16to8  ...) => (Copy ...)
113 (Trunc32to8  ...) => (Copy ...)
114 (Trunc32to16 ...) => (Copy ...)
115 (Trunc64to8  ...) => (Copy ...)
116 (Trunc64to16 ...) => (Copy ...)
117 (Trunc64to32 ...) => (Copy ...)
118
119 // Shifts
120
121 // SLL only considers the bottom 6 bits of y. If y > 64, the result should
122 // always be 0.
123 //
124 // Breaking down the operation:
125 //
126 // (SLL x y) generates x << (y & 63).
127 //
128 // If y < 64, this is the value we want. Otherwise, we want zero.
129 //
130 // So, we AND with -1 * uint64(y < 64), which is 0xfffff... if y < 64 and 0 otherwise.
131 (Lsh8x8   <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg8  <t> (SLTIU <t> [64] (ZeroExt8to64  y))))
132 (Lsh8x16  <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg8  <t> (SLTIU <t> [64] (ZeroExt16to64 y))))
133 (Lsh8x32  <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg8  <t> (SLTIU <t> [64] (ZeroExt32to64 y))))
134 (Lsh8x64  <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg8  <t> (SLTIU <t> [64] y)))
135 (Lsh16x8  <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg16 <t> (SLTIU <t> [64] (ZeroExt8to64  y))))
136 (Lsh16x16 <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg16 <t> (SLTIU <t> [64] (ZeroExt16to64 y))))
137 (Lsh16x32 <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg16 <t> (SLTIU <t> [64] (ZeroExt32to64 y))))
138 (Lsh16x64 <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg16 <t> (SLTIU <t> [64] y)))
139 (Lsh32x8  <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg32 <t> (SLTIU <t> [64] (ZeroExt8to64  y))))
140 (Lsh32x16 <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg32 <t> (SLTIU <t> [64] (ZeroExt16to64 y))))
141 (Lsh32x32 <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg32 <t> (SLTIU <t> [64] (ZeroExt32to64 y))))
142 (Lsh32x64 <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg32 <t> (SLTIU <t> [64] y)))
143 (Lsh64x8  <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg64 <t> (SLTIU <t> [64] (ZeroExt8to64  y))))
144 (Lsh64x16 <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg64 <t> (SLTIU <t> [64] (ZeroExt16to64 y))))
145 (Lsh64x32 <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg64 <t> (SLTIU <t> [64] (ZeroExt32to64 y))))
146 (Lsh64x64 <t> x y) && !shiftIsBounded(v) => (AND (SLL <t> x y) (Neg64 <t> (SLTIU <t> [64] y)))
147
148 (Lsh8x(64|32|16|8)  x y) && shiftIsBounded(v) => (SLL x y)
149 (Lsh16x(64|32|16|8) x y) && shiftIsBounded(v) => (SLL x y)
150 (Lsh32x(64|32|16|8) x y) && shiftIsBounded(v) => (SLL x y)
151 (Lsh64x(64|32|16|8) x y) && shiftIsBounded(v) => (SLL x y)
152
153 // SRL only considers the bottom 6 bits of y, similarly SRLW only considers the
154 // bottom 5 bits of y. Ensure that the result is always zero if the shift exceeds
155 // the maximum value. See Lsh above for a detailed description.
156 (Rsh8Ux8   <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt8to64  x) y) (Neg8  <t> (SLTIU <t> [64] (ZeroExt8to64  y))))
157 (Rsh8Ux16  <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt8to64  x) y) (Neg8  <t> (SLTIU <t> [64] (ZeroExt16to64 y))))
158 (Rsh8Ux32  <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt8to64  x) y) (Neg8  <t> (SLTIU <t> [64] (ZeroExt32to64 y))))
159 (Rsh8Ux64  <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt8to64  x) y) (Neg8  <t> (SLTIU <t> [64] y)))
160 (Rsh16Ux8  <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt16to64 x) y) (Neg16 <t> (SLTIU <t> [64] (ZeroExt8to64  y))))
161 (Rsh16Ux16 <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt16to64 x) y) (Neg16 <t> (SLTIU <t> [64] (ZeroExt16to64 y))))
162 (Rsh16Ux32 <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt16to64 x) y) (Neg16 <t> (SLTIU <t> [64] (ZeroExt32to64 y))))
163 (Rsh16Ux64 <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt16to64 x) y) (Neg16 <t> (SLTIU <t> [64] y)))
164 (Rsh32Ux8  <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt32to64 x) y) (Neg32 <t> (SLTIU <t> [32] (ZeroExt8to64  y))))
165 (Rsh32Ux16 <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt32to64 x) y) (Neg32 <t> (SLTIU <t> [32] (ZeroExt16to64 y))))
166 (Rsh32Ux32 <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt32to64 x) y) (Neg32 <t> (SLTIU <t> [32] (ZeroExt32to64 y))))
167 (Rsh32Ux64 <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> (ZeroExt32to64 x) y) (Neg32 <t> (SLTIU <t> [32] y)))
168 (Rsh64Ux8  <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> x                 y) (Neg64 <t> (SLTIU <t> [64] (ZeroExt8to64  y))))
169 (Rsh64Ux16 <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> x                 y) (Neg64 <t> (SLTIU <t> [64] (ZeroExt16to64 y))))
170 (Rsh64Ux32 <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> x                 y) (Neg64 <t> (SLTIU <t> [64] (ZeroExt32to64 y))))
171 (Rsh64Ux64 <t> x y) && !shiftIsBounded(v) => (AND (SRL <t> x                 y) (Neg64 <t> (SLTIU <t> [64] y)))
172
173 (Rsh8Ux(64|32|16|8)  x y) && shiftIsBounded(v) => (SRL (ZeroExt8to64  x) y)
174 (Rsh16Ux(64|32|16|8) x y) && shiftIsBounded(v) => (SRL (ZeroExt16to64 x) y)
175 (Rsh32Ux(64|32|16|8) x y) && shiftIsBounded(v) => (SRL (ZeroExt32to64 x) y)
176 (Rsh64Ux(64|32|16|8) x y) && shiftIsBounded(v) => (SRL x                 y)
177
178 // SRA only considers the bottom 6 bits of y. If y > 64, the result should
179 // be either 0 or -1 based on the sign bit.
180 //
181 // We implement this by performing the max shift (-1) if y >= 64.
182 //
183 // We OR (uint64(y < 64) - 1) into y before passing it to SRA. This leaves
184 // us with -1 (0xffff...) if y >= 64.
185 //
186 // We don't need to sign-extend the OR result, as it will be at minimum 8 bits,
187 // more than the 6 bits SRA cares about.
188 (Rsh8x8   <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt8to64  x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt8to64  y)))))
189 (Rsh8x16  <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt8to64  x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt16to64 y)))))
190 (Rsh8x32  <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt8to64  x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt32to64 y)))))
191 (Rsh8x64  <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt8to64  x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] y))))
192 (Rsh16x8  <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt16to64 x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt8to64  y)))))
193 (Rsh16x16 <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt16to64 x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt16to64 y)))))
194 (Rsh16x32 <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt16to64 x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt32to64 y)))))
195 (Rsh16x64 <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt16to64 x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] y))))
196 (Rsh32x8  <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt32to64 x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt8to64  y)))))
197 (Rsh32x16 <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt32to64 x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt16to64 y)))))
198 (Rsh32x32 <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt32to64 x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt32to64 y)))))
199 (Rsh32x64 <t> x y) && !shiftIsBounded(v) => (SRA <t> (SignExt32to64 x) (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] y))))
200 (Rsh64x8  <t> x y) && !shiftIsBounded(v) => (SRA <t> x                 (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt8to64  y)))))
201 (Rsh64x16 <t> x y) && !shiftIsBounded(v) => (SRA <t> x                 (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt16to64 y)))))
202 (Rsh64x32 <t> x y) && !shiftIsBounded(v) => (SRA <t> x                 (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] (ZeroExt32to64 y)))))
203 (Rsh64x64 <t> x y) && !shiftIsBounded(v) => (SRA <t> x                 (OR <y.Type> y (ADDI <y.Type> [-1] (SLTIU <y.Type> [64] y))))
204
205 (Rsh8x(64|32|16|8)  x y) && shiftIsBounded(v) => (SRA (SignExt8to64  x) y)
206 (Rsh16x(64|32|16|8) x y) && shiftIsBounded(v) => (SRA (SignExt16to64 x) y)
207 (Rsh32x(64|32|16|8) x y) && shiftIsBounded(v) => (SRA (SignExt32to64 x) y)
208 (Rsh64x(64|32|16|8) x y) && shiftIsBounded(v) => (SRA x                 y)
209
210 // Rotates.
211 (RotateLeft8  <t> x (MOVDconst [c])) => (Or8  (Lsh8x64  <t> x (MOVDconst [c&7]))  (Rsh8Ux64  <t> x (MOVDconst [-c&7])))
212 (RotateLeft16 <t> x (MOVDconst [c])) => (Or16 (Lsh16x64 <t> x (MOVDconst [c&15])) (Rsh16Ux64 <t> x (MOVDconst [-c&15])))
213 (RotateLeft32 <t> x (MOVDconst [c])) => (Or32 (Lsh32x64 <t> x (MOVDconst [c&31])) (Rsh32Ux64 <t> x (MOVDconst [-c&31])))
214 (RotateLeft64 <t> x (MOVDconst [c])) => (Or64 (Lsh64x64 <t> x (MOVDconst [c&63])) (Rsh64Ux64 <t> x (MOVDconst [-c&63])))
215
216 (Less64  ...) => (SLT  ...)
217 (Less32  x y) => (SLT  (SignExt32to64 x) (SignExt32to64 y))
218 (Less16  x y) => (SLT  (SignExt16to64 x) (SignExt16to64 y))
219 (Less8   x y) => (SLT  (SignExt8to64  x) (SignExt8to64  y))
220 (Less64U ...) => (SLTU ...)
221 (Less32U x y) => (SLTU (ZeroExt32to64 x) (ZeroExt32to64 y))
222 (Less16U x y) => (SLTU (ZeroExt16to64 x) (ZeroExt16to64 y))
223 (Less8U  x y) => (SLTU (ZeroExt8to64  x) (ZeroExt8to64  y))
224 (Less(64|32)F ...) => (FLT(D|S) ...)
225
226 // Convert x <= y to !(y > x).
227 (Leq(64|32|16|8)  x y) => (Not (Less(64|32|16|8)  y x))
228 (Leq(64|32|16|8)U x y) => (Not (Less(64|32|16|8)U y x))
229 (Leq(64|32)F ...) => (FLE(D|S) ...)
230
231 (EqPtr x y) => (SEQZ (SUB <typ.Uintptr> x y))
232 (Eq64  x y) => (SEQZ (SUB <x.Type> x y))
233 (Eq32  x y) &&  x.Type.IsSigned() => (SEQZ (SUB <x.Type> (SignExt32to64 x) (SignExt32to64 y)))
234 (Eq32  x y) && !x.Type.IsSigned() => (SEQZ (SUB <x.Type> (ZeroExt32to64 x) (ZeroExt32to64 y)))
235 (Eq16  x y) => (SEQZ (SUB <x.Type> (ZeroExt16to64 x) (ZeroExt16to64 y)))
236 (Eq8   x y) => (SEQZ (SUB <x.Type> (ZeroExt8to64  x) (ZeroExt8to64  y)))
237 (Eq(64|32)F ...) => (FEQ(D|S) ...)
238
239 (NeqPtr x y) => (Not (EqPtr x y))
240 (Neq64  x y) => (Not (Eq64  x y))
241 (Neq32  x y) => (Not (Eq32  x y))
242 (Neq16  x y) => (Not (Eq16  x y))
243 (Neq8   x y) => (Not (Eq8   x y))
244 (Neq(64|32)F ...) => (FNE(D|S) ...)
245
246 // Loads
247 (Load <t> ptr mem) &&  t.IsBoolean()                   => (MOVBUload ptr mem)
248 (Load <t> ptr mem) && ( is8BitInt(t) &&  t.IsSigned()) => (MOVBload  ptr mem)
249 (Load <t> ptr mem) && ( is8BitInt(t) && !t.IsSigned()) => (MOVBUload ptr mem)
250 (Load <t> ptr mem) && (is16BitInt(t) &&  t.IsSigned()) => (MOVHload  ptr mem)
251 (Load <t> ptr mem) && (is16BitInt(t) && !t.IsSigned()) => (MOVHUload ptr mem)
252 (Load <t> ptr mem) && (is32BitInt(t) &&  t.IsSigned()) => (MOVWload  ptr mem)
253 (Load <t> ptr mem) && (is32BitInt(t) && !t.IsSigned()) => (MOVWUload ptr mem)
254 (Load <t> ptr mem) && (is64BitInt(t) || isPtr(t))      => (MOVDload  ptr mem)
255 (Load <t> ptr mem) &&  is32BitFloat(t)                 => (FMOVWload ptr mem)
256 (Load <t> ptr mem) &&  is64BitFloat(t)                 => (FMOVDload ptr mem)
257
258 // Stores
259 (Store {t} ptr val mem) && t.Size() == 1 => (MOVBstore ptr val mem)
260 (Store {t} ptr val mem) && t.Size() == 2 => (MOVHstore ptr val mem)
261 (Store {t} ptr val mem) && t.Size() == 4 && !t.IsFloat() => (MOVWstore ptr val mem)
262 (Store {t} ptr val mem) && t.Size() == 8 && !t.IsFloat() => (MOVDstore ptr val mem)
263 (Store {t} ptr val mem) && t.Size() == 4 &&  t.IsFloat() => (FMOVWstore ptr val mem)
264 (Store {t} ptr val mem) && t.Size() == 8 &&  t.IsFloat() => (FMOVDstore ptr val mem)
265
266 // We need to fold MOVaddr into the LD/MOVDstore ops so that the live variable analysis
267 // knows what variables are being read/written by the ops.
268 (MOVBUload [off1] {sym1} (MOVaddr [off2] {sym2} base) mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
269         (MOVBUload [off1+off2] {mergeSym(sym1,sym2)} base mem)
270 (MOVBload  [off1] {sym1} (MOVaddr [off2] {sym2} base) mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
271         (MOVBload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
272 (MOVHUload [off1] {sym1} (MOVaddr [off2] {sym2} base) mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
273         (MOVHUload [off1+off2] {mergeSym(sym1,sym2)} base mem)
274 (MOVHload  [off1] {sym1} (MOVaddr [off2] {sym2} base) mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
275         (MOVHload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
276 (MOVWUload [off1] {sym1} (MOVaddr [off2] {sym2} base) mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
277         (MOVWUload [off1+off2] {mergeSym(sym1,sym2)} base mem)
278 (MOVWload  [off1] {sym1} (MOVaddr [off2] {sym2} base) mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
279         (MOVWload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
280 (MOVDload  [off1] {sym1} (MOVaddr [off2] {sym2} base) mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
281         (MOVDload  [off1+off2] {mergeSym(sym1,sym2)} base mem)
282
283 (MOVBstore [off1] {sym1} (MOVaddr [off2] {sym2} base) val mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
284         (MOVBstore [off1+off2] {mergeSym(sym1,sym2)} base val mem)
285 (MOVHstore [off1] {sym1} (MOVaddr [off2] {sym2} base) val mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
286         (MOVHstore [off1+off2] {mergeSym(sym1,sym2)} base val mem)
287 (MOVWstore [off1] {sym1} (MOVaddr [off2] {sym2} base) val mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
288         (MOVWstore [off1+off2] {mergeSym(sym1,sym2)} base val mem)
289 (MOVDstore [off1] {sym1} (MOVaddr [off2] {sym2} base) val mem) && is32Bit(int64(off1)+int64(off2)) && canMergeSym(sym1, sym2) =>
290         (MOVDstore [off1+off2] {mergeSym(sym1,sym2)} base val mem)
291 (MOVBstorezero [off1] {sym1} (MOVaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) && is32Bit(int64(off1)+int64(off2)) =>
292         (MOVBstorezero [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
293 (MOVHstorezero [off1] {sym1} (MOVaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) && is32Bit(int64(off1)+int64(off2)) =>
294         (MOVHstorezero [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
295 (MOVWstorezero [off1] {sym1} (MOVaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) && is32Bit(int64(off1)+int64(off2)) =>
296         (MOVWstorezero [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
297 (MOVDstorezero [off1] {sym1} (MOVaddr [off2] {sym2} ptr) mem) && canMergeSym(sym1,sym2) && is32Bit(int64(off1)+int64(off2)) =>
298         (MOVDstorezero [off1+off2] {mergeSym(sym1,sym2)} ptr mem)
299
300 (MOVBUload [off1] {sym} (ADDI [off2] base) mem) && is32Bit(int64(off1)+off2) =>
301         (MOVBUload [off1+int32(off2)] {sym} base mem)
302 (MOVBload  [off1] {sym} (ADDI [off2] base) mem) && is32Bit(int64(off1)+off2) =>
303         (MOVBload  [off1+int32(off2)] {sym} base mem)
304 (MOVHUload [off1] {sym} (ADDI [off2] base) mem) && is32Bit(int64(off1)+off2) =>
305         (MOVHUload [off1+int32(off2)] {sym} base mem)
306 (MOVHload  [off1] {sym} (ADDI [off2] base) mem) && is32Bit(int64(off1)+off2) =>
307         (MOVHload  [off1+int32(off2)] {sym} base mem)
308 (MOVWUload [off1] {sym} (ADDI [off2] base) mem) && is32Bit(int64(off1)+off2) =>
309         (MOVWUload [off1+int32(off2)] {sym} base mem)
310 (MOVWload  [off1] {sym} (ADDI [off2] base) mem) && is32Bit(int64(off1)+off2) =>
311         (MOVWload  [off1+int32(off2)] {sym} base mem)
312 (MOVDload  [off1] {sym} (ADDI [off2] base) mem) && is32Bit(int64(off1)+off2) =>
313         (MOVDload  [off1+int32(off2)] {sym} base mem)
314
315 (MOVBstore [off1] {sym} (ADDI [off2] base) val mem) && is32Bit(int64(off1)+off2) =>
316         (MOVBstore [off1+int32(off2)] {sym} base val mem)
317 (MOVHstore [off1] {sym} (ADDI [off2] base) val mem) && is32Bit(int64(off1)+off2) =>
318         (MOVHstore [off1+int32(off2)] {sym} base val mem)
319 (MOVWstore [off1] {sym} (ADDI [off2] base) val mem) && is32Bit(int64(off1)+off2) =>
320         (MOVWstore [off1+int32(off2)] {sym} base val mem)
321 (MOVDstore [off1] {sym} (ADDI [off2] base) val mem) && is32Bit(int64(off1)+off2) =>
322         (MOVDstore [off1+int32(off2)] {sym} base val mem)
323 (MOVBstorezero [off1] {sym} (ADDI [off2] ptr) mem) && is32Bit(int64(off1)+off2) => (MOVBstorezero [off1+int32(off2)] {sym} ptr mem)
324 (MOVHstorezero [off1] {sym} (ADDI [off2] ptr) mem) && is32Bit(int64(off1)+off2) => (MOVHstorezero [off1+int32(off2)] {sym} ptr mem)
325 (MOVWstorezero [off1] {sym} (ADDI [off2] ptr) mem) && is32Bit(int64(off1)+off2) => (MOVWstorezero [off1+int32(off2)] {sym} ptr mem)
326 (MOVDstorezero [off1] {sym} (ADDI [off2] ptr) mem) && is32Bit(int64(off1)+off2) => (MOVDstorezero [off1+int32(off2)] {sym} ptr mem)
327
328 // Similarly, fold ADDI into MOVaddr to avoid confusing live variable analysis
329 // with OffPtr -> ADDI.
330 (ADDI [c] (MOVaddr [d] {s} x)) && is32Bit(c+int64(d)) => (MOVaddr [int32(c)+d] {s} x)
331
332 // Small zeroing
333 (Zero [0] _ mem) => mem
334 (Zero [1] ptr mem) => (MOVBstore ptr (MOVDconst [0]) mem)
335 (Zero [2] {t} ptr mem) && t.Alignment()%2 == 0 =>
336         (MOVHstore ptr (MOVDconst [0]) mem)
337 (Zero [2] ptr mem) =>
338         (MOVBstore [1] ptr (MOVDconst [0])
339                 (MOVBstore ptr (MOVDconst [0]) mem))
340 (Zero [4] {t} ptr mem) && t.Alignment()%4 == 0 =>
341         (MOVWstore ptr (MOVDconst [0]) mem)
342 (Zero [4] {t} ptr mem) && t.Alignment()%2 == 0 =>
343         (MOVHstore [2] ptr (MOVDconst [0])
344                 (MOVHstore ptr (MOVDconst [0]) mem))
345 (Zero [4] ptr mem) =>
346         (MOVBstore [3] ptr (MOVDconst [0])
347                 (MOVBstore [2] ptr (MOVDconst [0])
348                         (MOVBstore [1] ptr (MOVDconst [0])
349                                 (MOVBstore ptr (MOVDconst [0]) mem))))
350 (Zero [8] {t} ptr mem) && t.Alignment()%8 == 0 =>
351         (MOVDstore ptr (MOVDconst [0]) mem)
352 (Zero [8] {t} ptr mem) && t.Alignment()%4 == 0 =>
353         (MOVWstore [4] ptr (MOVDconst [0])
354                 (MOVWstore ptr (MOVDconst [0]) mem))
355 (Zero [8] {t} ptr mem) && t.Alignment()%2 == 0 =>
356         (MOVHstore [6] ptr (MOVDconst [0])
357                 (MOVHstore [4] ptr (MOVDconst [0])
358                         (MOVHstore [2] ptr (MOVDconst [0])
359                                 (MOVHstore ptr (MOVDconst [0]) mem))))
360
361 (Zero [3] ptr mem) =>
362         (MOVBstore [2] ptr (MOVDconst [0])
363                 (MOVBstore [1] ptr (MOVDconst [0])
364                         (MOVBstore ptr (MOVDconst [0]) mem)))
365 (Zero [6] {t} ptr mem) && t.Alignment()%2 == 0 =>
366         (MOVHstore [4] ptr (MOVDconst [0])
367                 (MOVHstore [2] ptr (MOVDconst [0])
368                         (MOVHstore ptr (MOVDconst [0]) mem)))
369 (Zero [12] {t} ptr mem) && t.Alignment()%4 == 0 =>
370         (MOVWstore [8] ptr (MOVDconst [0])
371                 (MOVWstore [4] ptr (MOVDconst [0])
372                         (MOVWstore ptr (MOVDconst [0]) mem)))
373 (Zero [16] {t} ptr mem) && t.Alignment()%8 == 0 =>
374         (MOVDstore [8] ptr (MOVDconst [0])
375                 (MOVDstore ptr (MOVDconst [0]) mem))
376 (Zero [24] {t} ptr mem) && t.Alignment()%8 == 0 =>
377         (MOVDstore [16] ptr (MOVDconst [0])
378                 (MOVDstore [8] ptr (MOVDconst [0])
379                         (MOVDstore ptr (MOVDconst [0]) mem)))
380 (Zero [32] {t} ptr mem) && t.Alignment()%8 == 0 =>
381         (MOVDstore [24] ptr (MOVDconst [0])
382                 (MOVDstore [16] ptr (MOVDconst [0])
383                         (MOVDstore [8] ptr (MOVDconst [0])
384                                 (MOVDstore ptr (MOVDconst [0]) mem))))
385
386 // Medium 8-aligned zeroing uses a Duff's device
387 // 8 and 128 are magic constants, see runtime/mkduff.go
388 (Zero [s] {t} ptr mem)
389         && s%8 == 0 && s <= 8*128
390         && t.Alignment()%8 == 0 && !config.noDuffDevice =>
391         (DUFFZERO [8 * (128 - s/8)] ptr mem)
392
393 // Generic zeroing uses a loop
394 (Zero [s] {t} ptr mem) =>
395         (LoweredZero [t.Alignment()]
396                 ptr
397                 (ADD <ptr.Type> ptr (MOVDconst [s-moveSize(t.Alignment(), config)]))
398                 mem)
399
400 // Checks
401 (IsNonNil ...) => (SNEZ ...)
402 (IsInBounds ...) => (Less64U ...)
403 (IsSliceInBounds ...) => (Leq64U ...)
404
405 // Trivial lowering
406 (NilCheck ...) => (LoweredNilCheck ...)
407 (GetClosurePtr ...) => (LoweredGetClosurePtr ...)
408 (GetCallerSP ...) => (LoweredGetCallerSP ...)
409 (GetCallerPC ...) => (LoweredGetCallerPC ...)
410
411 // Write barrier.
412 (WB ...) => (LoweredWB ...)
413
414 // Publication barrier as intrinsic
415 (PubBarrier ...) => (LoweredPubBarrier ...)
416
417 (PanicBounds [kind] x y mem) && boundsABI(kind) == 0 => (LoweredPanicBoundsA [kind] x y mem)
418 (PanicBounds [kind] x y mem) && boundsABI(kind) == 1 => (LoweredPanicBoundsB [kind] x y mem)
419 (PanicBounds [kind] x y mem) && boundsABI(kind) == 2 => (LoweredPanicBoundsC [kind] x y mem)
420
421 // Small moves
422 (Move [0] _ _ mem) => mem
423 (Move [1] dst src mem) => (MOVBstore dst (MOVBload src mem) mem)
424 (Move [2] {t} dst src mem) && t.Alignment()%2 == 0 =>
425         (MOVHstore dst (MOVHload src mem) mem)
426 (Move [2] dst src mem) =>
427         (MOVBstore [1] dst (MOVBload [1] src mem)
428                 (MOVBstore dst (MOVBload src mem) mem))
429 (Move [4] {t} dst src mem) && t.Alignment()%4 == 0 =>
430         (MOVWstore dst (MOVWload src mem) mem)
431 (Move [4] {t} dst src mem) && t.Alignment()%2 == 0 =>
432         (MOVHstore [2] dst (MOVHload [2] src mem)
433                 (MOVHstore dst (MOVHload src mem) mem))
434 (Move [4] dst src mem) =>
435         (MOVBstore [3] dst (MOVBload [3] src mem)
436                 (MOVBstore [2] dst (MOVBload [2] src mem)
437                         (MOVBstore [1] dst (MOVBload [1] src mem)
438                                 (MOVBstore dst (MOVBload src mem) mem))))
439 (Move [8] {t} dst src mem) && t.Alignment()%8 == 0 =>
440         (MOVDstore dst (MOVDload src mem) mem)
441 (Move [8] {t} dst src mem) && t.Alignment()%4 == 0 =>
442         (MOVWstore [4] dst (MOVWload [4] src mem)
443                 (MOVWstore dst (MOVWload src mem) mem))
444 (Move [8] {t} dst src mem) && t.Alignment()%2 == 0 =>
445         (MOVHstore [6] dst (MOVHload [6] src mem)
446                 (MOVHstore [4] dst (MOVHload [4] src mem)
447                         (MOVHstore [2] dst (MOVHload [2] src mem)
448                                 (MOVHstore dst (MOVHload src mem) mem))))
449
450 (Move [3] dst src mem) =>
451         (MOVBstore [2] dst (MOVBload [2] src mem)
452                 (MOVBstore [1] dst (MOVBload [1] src mem)
453                         (MOVBstore dst (MOVBload src mem) mem)))
454 (Move [6] {t} dst src mem) && t.Alignment()%2 == 0 =>
455         (MOVHstore [4] dst (MOVHload [4] src mem)
456                 (MOVHstore [2] dst (MOVHload [2] src mem)
457                         (MOVHstore dst (MOVHload src mem) mem)))
458 (Move [12] {t} dst src mem) && t.Alignment()%4 == 0 =>
459         (MOVWstore [8] dst (MOVWload [8] src mem)
460                 (MOVWstore [4] dst (MOVWload [4] src mem)
461                         (MOVWstore dst (MOVWload src mem) mem)))
462 (Move [16] {t} dst src mem) && t.Alignment()%8 == 0 =>
463         (MOVDstore [8] dst (MOVDload [8] src mem)
464                 (MOVDstore dst (MOVDload src mem) mem))
465 (Move [24] {t} dst src mem) && t.Alignment()%8 == 0 =>
466         (MOVDstore [16] dst (MOVDload [16] src mem)
467                 (MOVDstore [8] dst (MOVDload [8] src mem)
468                         (MOVDstore dst (MOVDload src mem) mem)))
469 (Move [32] {t} dst src mem) && t.Alignment()%8 == 0 =>
470         (MOVDstore [24] dst (MOVDload [24] src mem)
471                 (MOVDstore [16] dst (MOVDload [16] src mem)
472                         (MOVDstore [8] dst (MOVDload [8] src mem)
473                                 (MOVDstore dst (MOVDload src mem) mem))))
474
475 // Medium 8-aligned move uses a Duff's device
476 // 16 and 128 are magic constants, see runtime/mkduff.go
477 (Move [s] {t} dst src mem)
478         && s%8 == 0 && s <= 8*128 && t.Alignment()%8 == 0
479         && !config.noDuffDevice && logLargeCopy(v, s) =>
480         (DUFFCOPY [16 * (128 - s/8)] dst src mem)
481
482 // Generic move uses a loop
483 (Move [s] {t} dst src mem) && (s <= 16 || logLargeCopy(v, s)) =>
484         (LoweredMove [t.Alignment()]
485                 dst
486                 src
487                 (ADDI <src.Type> [s-moveSize(t.Alignment(), config)] src)
488                 mem)
489
490 // Boolean ops; 0=false, 1=true
491 (AndB ...) => (AND ...)
492 (OrB  ...) => (OR  ...)
493 (EqB  x y) => (SEQZ (SUB <typ.Bool> x y))
494 (NeqB x y) => (SNEZ (SUB <typ.Bool> x y))
495 (Not  ...) => (SEQZ ...)
496
497 // Lowering pointer arithmetic
498 // TODO: Special handling for SP offsets, like ARM
499 (OffPtr [off] ptr:(SP)) && is32Bit(off) => (MOVaddr [int32(off)] ptr)
500 (OffPtr [off] ptr) && is32Bit(off) => (ADDI [off] ptr)
501 (OffPtr [off] ptr) => (ADD (MOVDconst [off]) ptr)
502
503 (Const(64|32|16|8) [val]) => (MOVDconst [int64(val)])
504 (Const32F [val]) => (FMVSX (MOVDconst [int64(math.Float32bits(val))]))
505 (Const64F [val]) => (FMVDX (MOVDconst [int64(math.Float64bits(val))]))
506 (ConstNil) => (MOVDconst [0])
507 (ConstBool [val]) => (MOVDconst [int64(b2i(val))])
508
509 (Addr {sym} base) => (MOVaddr {sym} [0] base)
510 (LocalAddr <t> {sym} base mem) && t.Elem().HasPointers() => (MOVaddr {sym} (SPanchored base mem))
511 (LocalAddr <t> {sym} base _)  && !t.Elem().HasPointers() => (MOVaddr {sym} base)
512
513 // Calls
514 (StaticCall  ...) => (CALLstatic  ...)
515 (ClosureCall ...) => (CALLclosure ...)
516 (InterCall   ...) => (CALLinter   ...)
517 (TailCall ...) => (CALLtail ...)
518
519 // Atomic Intrinsics
520 (AtomicLoad(Ptr|64|32|8)  ...) => (LoweredAtomicLoad(64|64|32|8) ...)
521 (AtomicStore(PtrNoWB|64|32|8) ...) => (LoweredAtomicStore(64|64|32|8) ...)
522 (AtomicAdd(64|32) ...) => (LoweredAtomicAdd(64|32) ...)
523
524 // AtomicAnd8(ptr,val) => LoweredAtomicAnd32(ptr&^3, ^((uint8(val) ^ 0xff) << ((ptr & 3) * 8)))
525 (AtomicAnd8 ptr val mem) =>
526         (LoweredAtomicAnd32 (ANDI <typ.Uintptr> [^3] ptr)
527                 (NOT <typ.UInt32> (SLL <typ.UInt32> (XORI <typ.UInt32> [0xff] (ZeroExt8to32 val))
528                         (SLLI <typ.UInt64> [3] (ANDI <typ.UInt64> [3] ptr)))) mem)
529
530 (AtomicAnd32 ...) => (LoweredAtomicAnd32 ...)
531
532 (AtomicCompareAndSwap32 ptr old new mem) => (LoweredAtomicCas32 ptr (SignExt32to64 old) new mem)
533 (AtomicCompareAndSwap64 ...) => (LoweredAtomicCas64 ...)
534
535 (AtomicExchange(64|32) ...) => (LoweredAtomicExchange(64|32) ...)
536
537 // AtomicOr8(ptr,val)  => LoweredAtomicOr32(ptr&^3, uint32(val)<<((ptr&3)*8))
538 (AtomicOr8 ptr val mem) =>
539         (LoweredAtomicOr32 (ANDI <typ.Uintptr> [^3] ptr)
540                 (SLL <typ.UInt32> (ZeroExt8to32 val)
541                         (SLLI <typ.UInt64> [3] (ANDI <typ.UInt64> [3] ptr))) mem)
542
543 (AtomicOr32  ...) => (LoweredAtomicOr32  ...)
544
545 // Conditional branches
546 (If cond yes no) => (BNEZ (MOVBUreg <typ.UInt64> cond) yes no)
547
548 // Optimizations
549
550 // Absorb SEQZ/SNEZ into branch.
551 (BEQZ (SEQZ x) yes no) => (BNEZ x yes no)
552 (BEQZ (SNEZ x) yes no) => (BEQZ x yes no)
553 (BNEZ (SEQZ x) yes no) => (BEQZ x yes no)
554 (BNEZ (SNEZ x) yes no) => (BNEZ x yes no)
555
556 // Remove redundant NEG from BEQZ/BNEZ.
557 (BEQZ (NEG x) yes no) => (BEQZ x yes no)
558 (BNEZ (NEG x) yes no) => (BNEZ x yes no)
559
560 // Negate comparison with FNES/FNED.
561 (BEQZ (FNES <t> x y) yes no) => (BNEZ (FEQS <t> x y) yes no)
562 (BNEZ (FNES <t> x y) yes no) => (BEQZ (FEQS <t> x y) yes no)
563 (BEQZ (FNED <t> x y) yes no) => (BNEZ (FEQD <t> x y) yes no)
564 (BNEZ (FNED <t> x y) yes no) => (BEQZ (FEQD <t> x y) yes no)
565
566 // Convert BEQZ/BNEZ into more optimal branch conditions.
567 (BEQZ (SUB x y) yes no) => (BEQ x y yes no)
568 (BNEZ (SUB x y) yes no) => (BNE x y yes no)
569 (BEQZ (SLT x y) yes no) => (BGE x y yes no)
570 (BNEZ (SLT x y) yes no) => (BLT x y yes no)
571 (BEQZ (SLTU x y) yes no) => (BGEU x y yes no)
572 (BNEZ (SLTU x y) yes no) => (BLTU x y yes no)
573 (BEQZ (SLTI [x] y) yes no) => (BGE y (MOVDconst [x]) yes no)
574 (BNEZ (SLTI [x] y) yes no) => (BLT y (MOVDconst [x]) yes no)
575 (BEQZ (SLTIU [x] y) yes no) => (BGEU y (MOVDconst [x]) yes no)
576 (BNEZ (SLTIU [x] y) yes no) => (BLTU y (MOVDconst [x]) yes no)
577
578 // Convert branch with zero to more optimal branch zero.
579 (BEQ (MOVDconst [0]) cond yes no) => (BEQZ cond yes no)
580 (BEQ cond (MOVDconst [0]) yes no) => (BEQZ cond yes no)
581 (BNE (MOVDconst [0]) cond yes no) => (BNEZ cond yes no)
582 (BNE cond (MOVDconst [0]) yes no) => (BNEZ cond yes no)
583 (BLT (MOVDconst [0]) cond yes no) => (BGTZ cond yes no)
584 (BLT cond (MOVDconst [0]) yes no) => (BLTZ cond yes no)
585 (BGE (MOVDconst [0]) cond yes no) => (BLEZ cond yes no)
586 (BGE cond (MOVDconst [0]) yes no) => (BGEZ cond yes no)
587
588 // Remove redundant NEG from SEQZ/SNEZ.
589 (SEQZ (NEG x)) => (SEQZ x)
590 (SNEZ (NEG x)) => (SNEZ x)
591
592 // Remove redundant SEQZ/SNEZ.
593 (SEQZ (SEQZ x)) => (SNEZ x)
594 (SEQZ (SNEZ x)) => (SEQZ x)
595 (SNEZ (SEQZ x)) => (SEQZ x)
596 (SNEZ (SNEZ x)) => (SNEZ x)
597
598 // Store zero.
599 (MOVBstore [off] {sym} ptr (MOVDconst [0]) mem) => (MOVBstorezero [off] {sym} ptr mem)
600 (MOVHstore [off] {sym} ptr (MOVDconst [0]) mem) => (MOVHstorezero [off] {sym} ptr mem)
601 (MOVWstore [off] {sym} ptr (MOVDconst [0]) mem) => (MOVWstorezero [off] {sym} ptr mem)
602 (MOVDstore [off] {sym} ptr (MOVDconst [0]) mem) => (MOVDstorezero [off] {sym} ptr mem)
603
604 // Boolean ops are already extended.
605 (MOVBUreg x:((FLES|FLTS|FEQS|FNES) _ _)) => x
606 (MOVBUreg x:((FLED|FLTD|FEQD|FNED) _ _)) => x
607 (MOVBUreg x:((SEQZ|SNEZ) _)) => x
608 (MOVBUreg x:((SLT|SLTU) _ _)) => x
609
610 // Avoid extending when already sufficiently masked.
611 (MOVBreg  x:(ANDI [c] y)) && c >= 0 && int64(int8(c)) == c => x
612 (MOVHreg  x:(ANDI [c] y)) && c >= 0 && int64(int16(c)) == c => x
613 (MOVWreg  x:(ANDI [c] y)) && c >= 0 && int64(int32(c)) == c => x
614 (MOVBUreg x:(ANDI [c] y)) && c >= 0 && int64(uint8(c)) == c => x
615 (MOVHUreg x:(ANDI [c] y)) && c >= 0 && int64(uint16(c)) == c => x
616 (MOVWUreg x:(ANDI [c] y)) && c >= 0 && int64(uint32(c)) == c => x
617
618 // Combine masking and zero extension.
619 (MOVBUreg (ANDI [c] x)) && c < 0 => (ANDI [int64(uint8(c))] x)
620 (MOVHUreg (ANDI [c] x)) && c < 0 => (ANDI [int64(uint16(c))] x)
621 (MOVWUreg (ANDI [c] x)) && c < 0 => (AND (MOVDconst [int64(uint32(c))]) x)
622
623 // Avoid sign/zero extension for consts.
624 (MOVBreg  (MOVDconst [c])) => (MOVDconst [int64(int8(c))])
625 (MOVHreg  (MOVDconst [c])) => (MOVDconst [int64(int16(c))])
626 (MOVWreg  (MOVDconst [c])) => (MOVDconst [int64(int32(c))])
627 (MOVBUreg (MOVDconst [c])) => (MOVDconst [int64(uint8(c))])
628 (MOVHUreg (MOVDconst [c])) => (MOVDconst [int64(uint16(c))])
629 (MOVWUreg (MOVDconst [c])) => (MOVDconst [int64(uint32(c))])
630
631 // Avoid sign/zero extension after properly typed load.
632 (MOVBreg  x:(MOVBload  _ _)) => (MOVDreg x)
633 (MOVHreg  x:(MOVBload  _ _)) => (MOVDreg x)
634 (MOVHreg  x:(MOVBUload _ _)) => (MOVDreg x)
635 (MOVHreg  x:(MOVHload  _ _)) => (MOVDreg x)
636 (MOVWreg  x:(MOVBload  _ _)) => (MOVDreg x)
637 (MOVWreg  x:(MOVBUload _ _)) => (MOVDreg x)
638 (MOVWreg  x:(MOVHload  _ _)) => (MOVDreg x)
639 (MOVWreg  x:(MOVHUload _ _)) => (MOVDreg x)
640 (MOVWreg  x:(MOVWload  _ _)) => (MOVDreg x)
641 (MOVBUreg x:(MOVBUload _ _)) => (MOVDreg x)
642 (MOVHUreg x:(MOVBUload _ _)) => (MOVDreg x)
643 (MOVHUreg x:(MOVHUload _ _)) => (MOVDreg x)
644 (MOVWUreg x:(MOVBUload _ _)) => (MOVDreg x)
645 (MOVWUreg x:(MOVHUload _ _)) => (MOVDreg x)
646 (MOVWUreg x:(MOVWUload _ _)) => (MOVDreg x)
647
648 // Avoid zero extension after properly typed atomic operation.
649 (MOVBUreg x:(Select0 (LoweredAtomicLoad8 _ _))) => (MOVDreg x)
650 (MOVBUreg x:(Select0 (LoweredAtomicCas32 _ _ _ _))) => (MOVDreg x)
651 (MOVBUreg x:(Select0 (LoweredAtomicCas64 _ _ _ _))) => (MOVDreg x)
652
653 // Avoid sign extension after word arithmetic.
654 (MOVWreg x:(ADDIW   _)) => (MOVDreg x)
655 (MOVWreg x:(SUBW  _ _)) => (MOVDreg x)
656 (MOVWreg x:(NEGW    _)) => (MOVDreg x)
657 (MOVWreg x:(MULW  _ _)) => (MOVDreg x)
658 (MOVWreg x:(DIVW  _ _)) => (MOVDreg x)
659 (MOVWreg x:(DIVUW _ _)) => (MOVDreg x)
660 (MOVWreg x:(REMW  _ _)) => (MOVDreg x)
661 (MOVWreg x:(REMUW _ _)) => (MOVDreg x)
662
663 // Fold double extensions.
664 (MOVBreg  x:(MOVBreg  _)) => (MOVDreg x)
665 (MOVHreg  x:(MOVBreg  _)) => (MOVDreg x)
666 (MOVHreg  x:(MOVBUreg _)) => (MOVDreg x)
667 (MOVHreg  x:(MOVHreg  _)) => (MOVDreg x)
668 (MOVWreg  x:(MOVBreg  _)) => (MOVDreg x)
669 (MOVWreg  x:(MOVBUreg _)) => (MOVDreg x)
670 (MOVWreg  x:(MOVHreg  _)) => (MOVDreg x)
671 (MOVWreg  x:(MOVWreg  _)) => (MOVDreg x)
672 (MOVBUreg x:(MOVBUreg _)) => (MOVDreg x)
673 (MOVHUreg x:(MOVBUreg _)) => (MOVDreg x)
674 (MOVHUreg x:(MOVHUreg _)) => (MOVDreg x)
675 (MOVWUreg x:(MOVBUreg _)) => (MOVDreg x)
676 (MOVWUreg x:(MOVHUreg _)) => (MOVDreg x)
677 (MOVWUreg x:(MOVWUreg _)) => (MOVDreg x)
678
679 // Do not extend before store.
680 (MOVBstore [off] {sym} ptr (MOVBreg  x) mem) => (MOVBstore [off] {sym} ptr x mem)
681 (MOVBstore [off] {sym} ptr (MOVHreg  x) mem) => (MOVBstore [off] {sym} ptr x mem)
682 (MOVBstore [off] {sym} ptr (MOVWreg  x) mem) => (MOVBstore [off] {sym} ptr x mem)
683 (MOVBstore [off] {sym} ptr (MOVBUreg x) mem) => (MOVBstore [off] {sym} ptr x mem)
684 (MOVBstore [off] {sym} ptr (MOVHUreg x) mem) => (MOVBstore [off] {sym} ptr x mem)
685 (MOVBstore [off] {sym} ptr (MOVWUreg x) mem) => (MOVBstore [off] {sym} ptr x mem)
686 (MOVHstore [off] {sym} ptr (MOVHreg  x) mem) => (MOVHstore [off] {sym} ptr x mem)
687 (MOVHstore [off] {sym} ptr (MOVWreg  x) mem) => (MOVHstore [off] {sym} ptr x mem)
688 (MOVHstore [off] {sym} ptr (MOVHUreg x) mem) => (MOVHstore [off] {sym} ptr x mem)
689 (MOVHstore [off] {sym} ptr (MOVWUreg x) mem) => (MOVHstore [off] {sym} ptr x mem)
690 (MOVWstore [off] {sym} ptr (MOVWreg  x) mem) => (MOVWstore [off] {sym} ptr x mem)
691 (MOVWstore [off] {sym} ptr (MOVWUreg x) mem) => (MOVWstore [off] {sym} ptr x mem)
692
693 // Replace extend after load with alternate load where possible.
694 (MOVBreg  <t> x:(MOVBUload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVBload  <t> [off] {sym} ptr mem)
695 (MOVHreg  <t> x:(MOVHUload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVHload  <t> [off] {sym} ptr mem)
696 (MOVWreg  <t> x:(MOVWUload [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVWload  <t> [off] {sym} ptr mem)
697 (MOVBUreg <t> x:(MOVBload  [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVBUload <t> [off] {sym} ptr mem)
698 (MOVHUreg <t> x:(MOVHload  [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVHUload <t> [off] {sym} ptr mem)
699 (MOVWUreg <t> x:(MOVWload  [off] {sym} ptr mem)) && x.Uses == 1 && clobber(x) => @x.Block (MOVWUload <t> [off] {sym} ptr mem)
700
701 // If a register move has only 1 use, just use the same register without emitting instruction
702 // MOVnop does not emit an instruction, only for ensuring the type.
703 (MOVDreg x) && x.Uses == 1 => (MOVDnop x)
704
705 // TODO: we should be able to get rid of MOVDnop all together.
706 // But for now, this is enough to get rid of lots of them.
707 (MOVDnop (MOVDconst [c])) => (MOVDconst [c])
708
709 // Avoid unnecessary zero extension when right shifting.
710 (SRL <t> (MOVWUreg x) y) => (SRLW <t> x y)
711 (SRLI <t> [x] (MOVWUreg y)) => (SRLIW <t> [int64(x&31)] y)
712
713 // Fold constant into immediate instructions where possible.
714 (ADD (MOVDconst <t> [val]) x) && is32Bit(val) && !t.IsPtr() => (ADDI [val] x)
715 (AND (MOVDconst [val]) x) && is32Bit(val) => (ANDI [val] x)
716 (OR  (MOVDconst [val]) x) && is32Bit(val) => (ORI  [val] x)
717 (XOR (MOVDconst [val]) x) && is32Bit(val) => (XORI [val] x)
718 (SLL  x (MOVDconst [val])) => (SLLI [int64(val&63)] x)
719 (SRL  x (MOVDconst [val])) => (SRLI [int64(val&63)] x)
720 (SRLW x (MOVDconst [val])) => (SRLIW [int64(val&31)] x)
721 (SRA  x (MOVDconst [val])) => (SRAI [int64(val&63)] x)
722 (SLT  x (MOVDconst [val])) && val >= -2048 && val <= 2047 => (SLTI  [val] x)
723 (SLTU x (MOVDconst [val])) && val >= -2048 && val <= 2047 => (SLTIU [val] x)
724
725 // Convert const subtraction into ADDI with negative immediate, where possible.
726 (SUB x (MOVDconst [val])) && is32Bit(-val) => (ADDI [-val] x)
727 (SUB <t> (MOVDconst [val]) y) && is32Bit(-val) => (NEG (ADDI <t> [-val] y))
728
729 // Subtraction of zero.
730 (SUB  x (MOVDconst [0])) => x
731 (SUBW x (MOVDconst [0])) => (ADDIW [0] x)
732
733 // Subtraction from zero.
734 (SUB  (MOVDconst [0]) x) => (NEG x)
735 (SUBW (MOVDconst [0]) x) => (NEGW x)
736
737 // Fold negation into subtraction.
738 (NEG (SUB x y)) => (SUB y x)
739 (NEG <t> s:(ADDI [val] (SUB x y))) && s.Uses == 1 && is32Bit(-val) => (ADDI [-val] (SUB <t> y x))
740
741 // Double negation.
742 (NEG (NEG x)) => x
743
744 // Addition of zero or two constants.
745 (ADDI [0] x) => x
746 (ADDI [x] (MOVDconst [y])) && is32Bit(x + y) => (MOVDconst [x + y])
747
748 // ANDI with all zeros, all ones or two constants.
749 (ANDI [0]  x) => (MOVDconst [0])
750 (ANDI [-1] x) => x
751 (ANDI [x] (MOVDconst [y])) => (MOVDconst [x & y])
752
753 // ORI with all zeroes, all ones or two constants.
754 (ORI [0]  x) => x
755 (ORI [-1] x) => (MOVDconst [-1])
756 (ORI [x] (MOVDconst [y])) => (MOVDconst [x | y])
757
758 // Combine operations with immediate.
759 (ADDI [x] (ADDI [y] z)) && is32Bit(x + y) => (ADDI [x + y] z)
760 (ANDI [x] (ANDI [y] z)) => (ANDI [x & y] z)
761 (ORI  [x] (ORI  [y] z)) => (ORI  [x | y] z)
762
763 // Negation of a constant.
764 (NEG  (MOVDconst [x])) => (MOVDconst [-x])
765 (NEGW (MOVDconst [x])) => (MOVDconst [int64(int32(-x))])
766
767 // Shift of a constant.
768 (SLLI [x] (MOVDconst [y])) && is32Bit(y << uint32(x)) => (MOVDconst [y << uint32(x)])
769 (SRLI [x] (MOVDconst [y])) => (MOVDconst [int64(uint64(y) >> uint32(x))])
770 (SRAI [x] (MOVDconst [y])) => (MOVDconst [int64(y) >> uint32(x)])
771
772 // SLTI/SLTIU with constants.
773 (SLTI  [x] (MOVDconst [y])) => (MOVDconst [b2i(int64(y) < int64(x))])
774 (SLTIU [x] (MOVDconst [y])) => (MOVDconst [b2i(uint64(y) < uint64(x))])
775
776 // SLTI/SLTIU with known outcomes.
777 (SLTI  [x] (ANDI [y] _)) && y >= 0 && int64(y) < int64(x) => (MOVDconst [1])
778 (SLTIU [x] (ANDI [y] _)) && y >= 0 && uint64(y) < uint64(x) => (MOVDconst [1])
779 (SLTI  [x] (ORI  [y] _)) && y >= 0 && int64(y) >= int64(x) => (MOVDconst [0])
780 (SLTIU [x] (ORI  [y] _)) && y >= 0 && uint64(y) >= uint64(x) => (MOVDconst [0])
781
782 // SLT/SLTU with known outcomes.
783 (SLT  x x) => (MOVDconst [0])
784 (SLTU x x) => (MOVDconst [0])
785
786 // Deadcode for LoweredMuluhilo
787 (Select0 m:(LoweredMuluhilo x y)) && m.Uses == 1 => (MULHU x y)
788 (Select1 m:(LoweredMuluhilo x y)) && m.Uses == 1 => (MUL x y)
789
790 (FADD(S|D) a (FMUL(S|D) x y)) && a.Block.Func.useFMA(v) => (FMADD(S|D) x y a)
791 (FSUB(S|D) a (FMUL(S|D) x y)) && a.Block.Func.useFMA(v) => (FNMSUB(S|D) x y a)
792 (FSUB(S|D) (FMUL(S|D) x y) a) && a.Block.Func.useFMA(v) => (FMSUB(S|D) x y a)
793
794 // Merge negation into fused multiply-add and multiply-subtract.
795 //
796 // Key:
797 //
798 //   [+ -](x * y [+ -] z).
799 //    _ N         A S
800 //                D U
801 //                D B
802 //
803 // Note: multiplication commutativity handled by rule generator.
804 (F(MADD|NMADD|MSUB|NMSUB)S neg:(FNEGS x) y z) && neg.Uses == 1 => (F(NMSUB|MSUB|NMADD|MADD)S x y z)
805 (F(MADD|NMADD|MSUB|NMSUB)S x y neg:(FNEGS z)) && neg.Uses == 1 => (F(MSUB|NMSUB|MADD|NMADD)S x y z)
806 (F(MADD|NMADD|MSUB|NMSUB)D neg:(FNEGD x) y z) && neg.Uses == 1 => (F(NMSUB|MSUB|NMADD|MADD)D x y z)
807 (F(MADD|NMADD|MSUB|NMSUB)D x y neg:(FNEGD z)) && neg.Uses == 1 => (F(MSUB|NMSUB|MADD|NMADD)D x y z)