3 // Copyright 2018 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
9 // This file contains codegen tests related to arithmetic
10 // simplifications and optimizations on integer types.
11 // For codegen tests on float types, see floats.go.
13 // ----------------- //
15 // ----------------- //
19 func SubMem(arr []int, b, c, d int) int {
20 // 386:`SUBL\s[A-Z]+,\s8\([A-Z]+\)`
21 // amd64:`SUBQ\s[A-Z]+,\s16\([A-Z]+\)`
23 // 386:`SUBL\s[A-Z]+,\s12\([A-Z]+\)`
24 // amd64:`SUBQ\s[A-Z]+,\s24\([A-Z]+\)`
26 // 386:`DECL\s16\([A-Z]+\)`
28 // 386:`ADDL\s[$]-20,\s20\([A-Z]+\)`
30 // 386:`SUBL\s\([A-Z]+\)\([A-Z]+\*4\),\s[A-Z]+`
32 // 386:`SUBL\s[A-Z]+,\s\([A-Z]+\)\([A-Z]+\*4\)`
34 // 386:`ADDL\s[$]-15,\s\([A-Z]+\)\([A-Z]+\*4\)`
36 // 386:`DECL\s\([A-Z]+\)\([A-Z]+\*4\)`
38 // amd64:`DECQ\s64\([A-Z]+\)`
42 return arr[0] - arr[1]
45 func SubFromConst(a int) int {
46 // ppc64x: `SUBC\tR[0-9]+,\s[$]40,\sR`
51 func SubFromConstNeg(a int) int {
52 // ppc64x: `ADD\t[$]40,\sR[0-9]+,\sR`
57 func SubSubFromConst(a int) int {
58 // ppc64x: `ADD\t[$]20,\sR[0-9]+,\sR`
63 func AddSubFromConst(a int) int {
64 // ppc64x: `SUBC\tR[0-9]+,\s[$]60,\sR`
69 func NegSubFromConst(a int) int {
70 // ppc64x: `ADD\t[$]-20,\sR[0-9]+,\sR`
75 func NegAddFromConstNeg(a int) int {
76 // ppc64x: `SUBC\tR[0-9]+,\s[$]40,\sR`
81 func SubSubNegSimplify(a, b int) int {
88 func SubAddSimplify(a, b int) int {
89 // amd64:-"SUBQ",-"ADDQ"
90 // ppc64x:-"SUB",-"ADD"
95 func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
97 r := (a + b) - (a + c)
99 r1 := (a + b) - (c + a)
101 r2 := (b + a) - (a + c)
103 r3 := (b + a) - (c + a)
105 r4 := (a - c) + (c + b)
107 r5 := (a - c) + (b + c)
108 return r, r1, r2, r3, r4, r5
111 func SubAddNegSimplify(a, b int) int {
112 // amd64:"NEGQ",-"ADDQ",-"SUBQ"
113 // ppc64x:"NEG",-"ADD",-"SUB"
118 func AddAddSubSimplify(a, b, c int) int {
121 r := a + (b + (c - a))
125 // -------------------- //
127 // -------------------- //
129 func Pow2Muls(n1, n2 int) (int, int) {
130 // amd64:"SHLQ\t[$]5",-"IMULQ"
131 // 386:"SHLL\t[$]5",-"IMULL"
132 // arm:"SLL\t[$]5",-"MUL"
133 // arm64:"LSL\t[$]5",-"MUL"
134 // ppc64x:"SLD\t[$]5",-"MUL"
137 // amd64:"SHLQ\t[$]6",-"IMULQ"
138 // 386:"SHLL\t[$]6",-"IMULL"
139 // arm:"SLL\t[$]6",-"MUL"
140 // arm64:`NEG\sR[0-9]+<<6,\sR[0-9]+`,-`LSL`,-`MUL`
141 // ppc64x:"SLD\t[$]6","NEG\\sR[0-9]+,\\sR[0-9]+",-"MUL"
147 func Mul_96(n int) int {
148 // amd64:`SHLQ\t[$]5`,`LEAQ\t\(.*\)\(.*\*2\),`,-`IMULQ`
149 // 386:`SHLL\t[$]5`,`LEAL\t\(.*\)\(.*\*2\),`,-`IMULL`
150 // arm64:`LSL\t[$]5`,`ADD\sR[0-9]+<<1,\sR[0-9]+`,-`MUL`
151 // arm:`SLL\t[$]5`,`ADD\sR[0-9]+<<1,\sR[0-9]+`,-`MUL`
152 // s390x:`SLD\t[$]5`,`SLD\t[$]6`,-`MULLD`
156 func Mul_n120(n int) int {
157 // s390x:`SLD\t[$]3`,`SLD\t[$]7`,-`MULLD`
161 func MulMemSrc(a []uint32, b []float32) {
162 // 386:`IMULL\s4\([A-Z]+\),\s[A-Z]+`
164 // 386/sse2:`MULSS\s4\([A-Z]+\),\sX[0-9]+`
165 // amd64:`MULSS\s4\([A-Z]+\),\sX[0-9]+`
169 // Multiplications merging tests
171 func MergeMuls1(n int) int {
172 // amd64:"IMUL3Q\t[$]46"
173 // 386:"IMUL3L\t[$]46"
174 // ppc64x:"MULLD\t[$]46"
175 return 15*n + 31*n // 46n
178 func MergeMuls2(n int) int {
179 // amd64:"IMUL3Q\t[$]23","(ADDQ\t[$]29)|(LEAQ\t29)"
180 // 386:"IMUL3L\t[$]23","ADDL\t[$]29"
181 // ppc64x/power9:"MADDLD",-"MULLD\t[$]23",-"ADD\t[$]29"
182 // ppc64x/power8:"MULLD\t[$]23","ADD\t[$]29"
183 return 5*n + 7*(n+1) + 11*(n+2) // 23n + 29
186 func MergeMuls3(a, n int) int {
187 // amd64:"ADDQ\t[$]19",-"IMULQ\t[$]19"
188 // 386:"ADDL\t[$]19",-"IMULL\t[$]19"
189 // ppc64x:"ADD\t[$]19",-"MULLD\t[$]19"
190 return a*n + 19*n // (a+19)n
193 func MergeMuls4(n int) int {
194 // amd64:"IMUL3Q\t[$]14"
195 // 386:"IMUL3L\t[$]14"
196 // ppc64x:"MULLD\t[$]14"
197 return 23*n - 9*n // 14n
200 func MergeMuls5(a, n int) int {
201 // amd64:"ADDQ\t[$]-19",-"IMULQ\t[$]19"
202 // 386:"ADDL\t[$]-19",-"IMULL\t[$]19"
203 // ppc64x:"ADD\t[$]-19",-"MULLD\t[$]19"
204 return a*n - 19*n // (a-19)n
211 func DivMemSrc(a []float64) {
212 // 386/sse2:`DIVSD\s8\([A-Z]+\),\sX[0-9]+`
213 // amd64:`DIVSD\s8\([A-Z]+\),\sX[0-9]+`
217 func Pow2Divs(n1 uint, n2 int) (uint, int) {
218 // 386:"SHRL\t[$]5",-"DIVL"
219 // amd64:"SHRQ\t[$]5",-"DIVQ"
220 // arm:"SRL\t[$]5",-".*udiv"
221 // arm64:"LSR\t[$]5",-"UDIV"
223 a := n1 / 32 // unsigned
225 // amd64:"SARQ\t[$]6",-"IDIVQ"
226 // 386:"SARL\t[$]6",-"IDIVL"
227 // arm:"SRA\t[$]6",-".*udiv"
228 // arm64:"ASR\t[$]6",-"SDIV"
230 b := n2 / 64 // signed
235 // Check that constant divisions get turned into MULs
236 func ConstDivs(n1 uint, n2 int) (uint, int) {
237 // amd64:"MOVQ\t[$]-1085102592571150095","MULQ",-"DIVQ"
238 // 386:"MOVL\t[$]-252645135","MULL",-"DIVL"
239 // arm64:`MOVD`,`UMULH`,-`DIV`
240 // arm:`MOVW`,`MUL`,-`.*udiv`
241 a := n1 / 17 // unsigned
243 // amd64:"MOVQ\t[$]-1085102592571150095","IMULQ",-"IDIVQ"
244 // 386:"MOVL\t[$]-252645135","IMULL",-"IDIVL"
245 // arm64:`SMULH`,-`DIV`
246 // arm:`MOVW`,`MUL`,-`.*udiv`
247 b := n2 / 17 // signed
252 func FloatDivs(a []float32) float32 {
253 // amd64:`DIVSS\s8\([A-Z]+\),\sX[0-9]+`
254 // 386/sse2:`DIVSS\s8\([A-Z]+\),\sX[0-9]+`
258 func Pow2Mods(n1 uint, n2 int) (uint, int) {
259 // 386:"ANDL\t[$]31",-"DIVL"
260 // amd64:"ANDL\t[$]31",-"DIVQ"
261 // arm:"AND\t[$]31",-".*udiv"
262 // arm64:"AND\t[$]31",-"UDIV"
264 a := n1 % 32 // unsigned
266 // 386:"SHRL",-"IDIVL"
267 // amd64:"SHRQ",-"IDIVQ"
268 // arm:"SRA",-".*udiv"
269 // arm64:"ASR",-"REM"
271 b := n2 % 64 // signed
276 // Check that signed divisibility checks get converted to AND on low bits
277 func Pow2DivisibleSigned(n1, n2 int) (bool, bool) {
278 // 386:"TESTL\t[$]63",-"DIVL",-"SHRL"
279 // amd64:"TESTQ\t[$]63",-"DIVQ",-"SHRQ"
280 // arm:"AND\t[$]63",-".*udiv",-"SRA"
281 // arm64:"TST\t[$]63",-"UDIV",-"ASR",-"AND"
282 // ppc64x:"RLDICL",-"SRAD"
283 a := n1%64 == 0 // signed divisible
285 // 386:"TESTL\t[$]63",-"DIVL",-"SHRL"
286 // amd64:"TESTQ\t[$]63",-"DIVQ",-"SHRQ"
287 // arm:"AND\t[$]63",-".*udiv",-"SRA"
288 // arm64:"TST\t[$]63",-"UDIV",-"ASR",-"AND"
289 // ppc64x:"RLDICL",-"SRAD"
290 b := n2%64 != 0 // signed indivisible
295 // Check that constant modulo divs get turned into MULs
296 func ConstMods(n1 uint, n2 int) (uint, int) {
297 // amd64:"MOVQ\t[$]-1085102592571150095","MULQ",-"DIVQ"
298 // 386:"MOVL\t[$]-252645135","MULL",-"DIVL"
299 // arm64:`MOVD`,`UMULH`,-`DIV`
300 // arm:`MOVW`,`MUL`,-`.*udiv`
301 a := n1 % 17 // unsigned
303 // amd64:"MOVQ\t[$]-1085102592571150095","IMULQ",-"IDIVQ"
304 // 386:"MOVL\t[$]-252645135","IMULL",-"IDIVL"
305 // arm64:`SMULH`,-`DIV`
306 // arm:`MOVW`,`MUL`,-`.*udiv`
307 b := n2 % 17 // signed
312 // Check that divisibility checks x%c==0 are converted to MULs and rotates
313 func DivisibleU(n uint) (bool, bool) {
314 // amd64:"MOVQ\t[$]-6148914691236517205","IMULQ","ROLQ\t[$]63",-"DIVQ"
315 // 386:"IMUL3L\t[$]-1431655765","ROLL\t[$]31",-"DIVQ"
316 // arm64:"MOVD\t[$]-6148914691236517205","MOVD\t[$]3074457345618258602","MUL","ROR",-"DIV"
317 // arm:"MUL","CMP\t[$]715827882",-".*udiv"
318 // ppc64x:"MULLD","ROTL\t[$]63"
321 // amd64:"MOVQ\t[$]-8737931403336103397","IMULQ",-"ROLQ",-"DIVQ"
322 // 386:"IMUL3L\t[$]678152731",-"ROLL",-"DIVQ"
323 // arm64:"MOVD\t[$]-8737931403336103397","MUL",-"ROR",-"DIV"
324 // arm:"MUL","CMP\t[$]226050910",-".*udiv"
325 // ppc64x:"MULLD",-"ROTL"
331 func Divisible(n int) (bool, bool) {
332 // amd64:"IMULQ","ADD","ROLQ\t[$]63",-"DIVQ"
333 // 386:"IMUL3L\t[$]-1431655765","ADDL\t[$]715827882","ROLL\t[$]31",-"DIVQ"
334 // arm64:"MOVD\t[$]-6148914691236517205","MOVD\t[$]3074457345618258602","MUL","ADD\tR","ROR",-"DIV"
335 // arm:"MUL","ADD\t[$]715827882",-".*udiv"
336 // ppc64x/power8:"MULLD","ADD","ROTL\t[$]63"
337 // ppc64x/power9:"MADDLD","ROTL\t[$]63"
340 // amd64:"IMULQ","ADD",-"ROLQ",-"DIVQ"
341 // 386:"IMUL3L\t[$]678152731","ADDL\t[$]113025455",-"ROLL",-"DIVQ"
342 // arm64:"MUL","MOVD\t[$]485440633518672410","ADD",-"ROR",-"DIV"
343 // arm:"MUL","ADD\t[$]113025455",-".*udiv"
344 // ppc64x/power8:"MULLD","ADD",-"ROTL"
345 // ppc64x/power9:"MADDLD",-"ROTL"
351 // Check that fix-up code is not generated for divisions where it has been proven that
352 // that the divisor is not -1 or that the dividend is > MinIntNN.
353 func NoFix64A(divr int64) (int64, int64) {
357 d /= divr // amd64:-"JMP"
358 e %= divr // amd64:-"JMP"
359 // The following statement is to avoid conflict between the above check
360 // and the normal JMP generated at the end of the block.
366 func NoFix64B(divd int64) (int64, int64) {
370 if divd > -9223372036854775808 {
371 d = divd / divr // amd64:-"JMP"
372 e = divd % divr // amd64:-"JMP"
378 func NoFix32A(divr int32) (int32, int32) {
393 func NoFix32B(divd int32) (int32, int32) {
397 if divd > -2147483648 {
409 func NoFix16A(divr int16) (int16, int16) {
424 func NoFix16B(divd int16) (int16, int16) {
440 // Check that len() and cap() calls divided by powers of two are
441 // optimized into shifts and ands
443 func LenDiv1(a []int) int {
445 // amd64:"SHRQ\t[$]10"
446 // arm64:"LSR\t[$]10",-"SDIV"
447 // arm:"SRL\t[$]10",-".*udiv"
448 // ppc64x:"SRD"\t[$]10"
452 func LenDiv2(s string) int {
454 // amd64:"SHRQ\t[$]11"
455 // arm64:"LSR\t[$]11",-"SDIV"
456 // arm:"SRL\t[$]11",-".*udiv"
457 // ppc64x:"SRD\t[$]11"
458 return len(s) / (4097 >> 1)
461 func LenMod1(a []int) int {
462 // 386:"ANDL\t[$]1023"
463 // amd64:"ANDL\t[$]1023"
464 // arm64:"AND\t[$]1023",-"SDIV"
465 // arm/6:"AND",-".*udiv"
466 // arm/7:"BFC",-".*udiv",-"AND"
471 func LenMod2(s string) int {
472 // 386:"ANDL\t[$]2047"
473 // amd64:"ANDL\t[$]2047"
474 // arm64:"AND\t[$]2047",-"SDIV"
475 // arm/6:"AND",-".*udiv"
476 // arm/7:"BFC",-".*udiv",-"AND"
478 return len(s) % (4097 >> 1)
481 func CapDiv(a []int) int {
483 // amd64:"SHRQ\t[$]12"
484 // arm64:"LSR\t[$]12",-"SDIV"
485 // arm:"SRL\t[$]12",-".*udiv"
486 // ppc64x:"SRD\t[$]12"
487 return cap(a) / ((1 << 11) + 2048)
490 func CapMod(a []int) int {
491 // 386:"ANDL\t[$]4095"
492 // amd64:"ANDL\t[$]4095"
493 // arm64:"AND\t[$]4095",-"SDIV"
494 // arm/6:"AND",-".*udiv"
495 // arm/7:"BFC",-".*udiv",-"AND"
497 return cap(a) % ((1 << 11) + 2048)
500 func AddMul(x int) int {
505 func MULA(a, b, c uint32) (uint32, uint32, uint32) {
506 // arm:`MULA`,-`MUL\s`
507 // arm64:`MADDW`,-`MULW`
509 // arm:`MULA`,-`MUL\s`
510 // arm64:`MADDW`,-`MULW`
512 // arm:`ADD`,-`MULA`,-`MUL\s`
513 // arm64:`ADD`,-`MADD`,-`MULW`
514 // ppc64x:`ADD`,-`MULLD`
519 func MULS(a, b, c uint32) (uint32, uint32, uint32) {
520 // arm/7:`MULS`,-`MUL\s`
521 // arm/6:`SUB`,`MUL\s`,-`MULS`
522 // arm64:`MSUBW`,-`MULW`
524 // arm/7:`MULS`,-`MUL\s`
525 // arm/6:`SUB`,`MUL\s`,-`MULS`
526 // arm64:`MSUBW`,-`MULW`
528 // arm/7:`SUB`,-`MULS`,-`MUL\s`
529 // arm64:`SUB`,-`MSUBW`,-`MULW`
530 // ppc64x:`SUB`,-`MULLD`
535 func addSpecial(a, b, c uint32) (uint32, uint32, uint32) {
540 // amd64:`SUBL.*-128`
545 // Divide -> shift rules usually require fixup for negative inputs.
546 // If the input is non-negative, make sure the fixup is eliminated.
547 func divInt(v int64) int64 {
551 // amd64:-`.*SARQ.*63,`, -".*SHRQ", ".*SARQ.*[$]9,"
555 // The reassociate rules "x - (z + C) -> (x - z) - C" and
556 // "(z + C) -x -> C + (z - x)" can optimize the following cases.
557 func constantFold1(i0, j0, i1, j1, i2, j2, i3, j3 int) (int, int, int, int) {
558 // arm64:"SUB","ADD\t[$]2"
559 // ppc64x:"SUB","ADD\t[$]2"
560 r0 := (i0 + 3) - (j0 + 1)
561 // arm64:"SUB","SUB\t[$]4"
562 // ppc64x:"SUB","ADD\t[$]-4"
563 r1 := (i1 - 3) - (j1 + 1)
564 // arm64:"SUB","ADD\t[$]4"
565 // ppc64x:"SUB","ADD\t[$]4"
566 r2 := (i2 + 3) - (j2 - 1)
567 // arm64:"SUB","SUB\t[$]2"
568 // ppc64x:"SUB","ADD\t[$]-2"
569 r3 := (i3 - 3) - (j3 - 1)
570 return r0, r1, r2, r3
573 // The reassociate rules "x - (z + C) -> (x - z) - C" and
574 // "(C - z) - x -> C - (z + x)" can optimize the following cases.
575 func constantFold2(i0, j0, i1, j1 int) (int, int) {
576 // arm64:"ADD","MOVD\t[$]2","SUB"
577 // ppc64x: `SUBC\tR[0-9]+,\s[$]2,\sR`
578 r0 := (3 - i0) - (j0 + 1)
579 // arm64:"ADD","MOVD\t[$]4","SUB"
580 // ppc64x: `SUBC\tR[0-9]+,\s[$]4,\sR`
581 r1 := (3 - i1) - (j1 - 1)
585 func constantFold3(i, j int) int {
586 // arm64: "MOVD\t[$]30","MUL",-"ADD",-"LSL"
587 // ppc64x:"MULLD\t[$]30","MULLD"
588 r := (5 * i) * (6 * j)