mirror of
https://github.com/golang/go
synced 2025-04-29 02:27:46 +00:00
.github
api
doc
lib
misc
src
test
abi
alias3.dir
arenas
asmhdr.dir
chan
closure3.dir
closure5.dir
codegen
README
addrcalc.go
alloc.go
arithmetic.go
atomics.go
bitfield.go
bits.go
bmi.go
bool.go
clobberdead.go
clobberdeadreg.go
compare_and_branch.go
comparisons.go
condmove.go
constants.go
copy.go
floats.go
fuse.go
ifaces.go
issue22703.go
issue25378.go
issue31618.go
issue33580.go
issue38554.go
issue42610.go
issue48054.go
issue52635.go
issue54467.go
issue56440.go
issue58166.go
issue60324.go
issue60673.go
issue61356.go
issue63332.go
issue66585.go
logic.go
mapaccess.go
maps.go
math.go
mathbits.go
memcombine.go
memops.go
memops_bigoffset.go
noextend.go
race.go
regabi_regalloc.go
retpoline.go
rotate.go
select.go
shift.go
shortcircuit.go
slices.go
smallintiface.go
spectre.go
stack.go
strings.go
structs.go
switch.go
writebarrier.go
zerosize.go
ddd2.dir
dwarf
fixedbugs
import2.dir
import4.dir
interface
intrinsic.dir
ken
linkname.dir
linknameasm.dir
method4.dir
retjmp.dir
runtime
stress
syntax
typeparam
uintptrescapes.dir
235.go
64bit.go
README.md
alg.go
alias.go
alias1.go
alias2.go
alias3.go
align.go
append.go
append1.go
args.go
armimm.go
asmhdr.go
assign.go
assign1.go
atomicload.go
bigalg.go
bigmap.go
blank.go
blank1.go
bom.go
bombad.go
bounds.go
cannotassign.go
chancap.go
chanlinear.go
char_lit.go
char_lit1.go
checkbce.go
clear.go
clearfat.go
closedchan.go
closure.go
closure1.go
closure2.go
closure3.go
closure4.go
closure5.go
closure6.go
closure7.go
cmp.go
cmp6.go
cmplx.go
cmplxdivide.c
cmplxdivide.go
cmplxdivide1.go
complit.go
complit1.go
compos.go
const.go
const1.go
const2.go
const3.go
const4.go
const5.go
const6.go
const7.go
const8.go
convT2X.go
convert.go
convert1.go
convert2.go
convert3.go
convert4.go
convinline.go
convlit.go
convlit1.go
copy.go
copy1.go
crlf.go
ddd.go
ddd1.go
ddd2.go
decl.go
declbad.go
defer.go
defererrcheck.go
deferfin.go
defernil.go
deferprint.go
deferprint.out
devirt.go
directive.go
directive2.go
divide.go
divmod.go
embedfunc.go
embedvers.go
empty.go
env.go
eof.go
eof1.go
escape.go
escape2.go
escape2n.go
escape3.go
escape4.go
escape5.go
escape_array.go
escape_calls.go
escape_closure.go
escape_field.go
escape_goto.go
escape_hash_maphash.go
escape_iface.go
escape_indir.go
escape_level.go
escape_map.go
escape_mutations.go
escape_param.go
escape_reflect.go
escape_runtime_atomic.go
escape_selfassign.go
escape_slice.go
escape_struct_param1.go
escape_struct_param2.go
escape_struct_return.go
escape_sync_atomic.go
escape_unsafe.go
fibo.go
finprofiled.go
float_lit.go
float_lit2.go
float_lit3.go
floatcmp.go
for.go
func.go
func1.go
func2.go
func3.go
func4.go
func5.go
func6.go
func7.go
func8.go
funcdup.go
funcdup2.go
fuse.go
gc.go
gc1.go
gc2.go
gcgort.go
gcstring.go
goprint.go
goprint.out
goto.go
heapsampling.go
helloworld.go
helloworld.out
if.go
import.go
import1.go
import2.go
import4.go
import5.go
import6.go
index.go
index0.go
index1.go
index2.go
indirect.go
indirect1.go
init.go
init1.go
initcomma.go
initexp.go
initialize.go
initializerr.go
initloop.go
inline.go
inline_big.go
inline_caller.go
inline_callers.go
inline_endian.go
inline_literal.go
inline_math_bits_rotate.go
inline_sync.go
inline_variadic.go
int_lit.go
intcvt.go
intrinsic.go
intrinsic_atomic.go
iota.go
label.go
label1.go
linkmain.go
linkmain_run.go
linkname.go
linkname3.go
linknameasm.go
linkobj.go
linkx.go
linkx_run.go
literal.go
literal2.go
live.go
live1.go
live2.go
live_regabi.go
live_uintptrkeepalive.go
loopbce.go
mainsig.go
makechan.go
makemap.go
makenew.go
makeslice.go
mallocfin.go
map.go
map1.go
mapclear.go
maplinear.go
maymorestack.go
mergemul.go
method.go
method1.go
method2.go
method3.go
method4.go
method5.go
method6.go
method7.go
named.go
named1.go
newinline.go
nil.go
nilcheck.go
nilptr.go
nilptr2.go
nilptr3.go
nilptr4.go
nilptr5.go
nilptr5_aix.go
nilptr5_wasm.go
nilptr_aix.go
noinit.go
nosplit.go
nowritebarrier.go
nul1.go
opt_branchlikely.go
parentype.go
peano.go
phiopt.go
print.go
print.out
printbig.go
printbig.out
prove.go
prove_constant_folding.go
prove_invert_loop_with_unused_iterators.go
range.go
range2.go
range3.go
range4.go
rangegen.go
recover.go
recover1.go
recover2.go
recover3.go
recover4.go
recover5.go
reflectmethod1.go
reflectmethod2.go
reflectmethod3.go
reflectmethod4.go
reflectmethod5.go
reflectmethod6.go
reflectmethod7.go
reflectmethod8.go
rename.go
rename1.go
reorder.go
reorder2.go
retjmp.go
return.go
rotate.go
rotate0.go
rotate1.go
rotate2.go
rotate3.go
rune.go
runtime.go
shift1.go
shift2.go
shift3.go
sieve.go
sigchld.go
sigchld.out
simassign.go
sizeof.go
slice3.go
slice3err.go
slicecap.go
sliceopt.go
solitaire.go
stack.go
stackobj.go
stackobj2.go
stackobj3.go
strcopy.go
strength.go
string_lit.go
stringrange.go
struct0.go
switch.go
switch2.go
switch3.go
switch4.go
switch5.go
switch6.go
switch7.go
tighten.go
tinyfin.go
torture.go
turing.go
typecheck.go
typecheckloop.go
typeswitch.go
typeswitch1.go
typeswitch2.go
typeswitch2b.go
typeswitch3.go
uintptrescapes.go
uintptrescapes2.go
uintptrescapes3.go
uintptrkeepalive.go
undef.go
unsafe_slice_data.go
unsafe_string.go
unsafe_string_data.go
unsafebuiltins.go
used.go
utf.go
varerr.go
varinit.go
winbatch.go
writebarrier.go
zerodivide.go
zerosize.go
.gitattributes
.gitignore
CONTRIBUTING.md
LICENSE
PATENTS
README.md
SECURITY.md
VERSION
codereview.cfg
go.env
Avoid creating duplicate usages of ANDCCconst. This is preparation for a patch to reintroduce ANDconst to simplify the lower pass while treating ANDCCconst like other *CC* ssa opcodes. Also, move many of the similar rules wich retarget ANDCCconst users to the flag result to a common rule for all compares against zero. Change-Id: Ida86efe17ff413cb82c349d8ef69d2899361f4c0 Reviewed-on: https://go-review.googlesource.com/c/go/+/585400 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Lynn Boger <laboger@linux.vnet.ibm.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
632 lines
15 KiB
Go
632 lines
15 KiB
Go
// asmcheck
|
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package codegen
|
|
|
|
// This file contains codegen tests related to arithmetic
|
|
// simplifications and optimizations on integer types.
|
|
// For codegen tests on float types, see floats.go.
|
|
|
|
// ----------------- //
|
|
// Addition //
|
|
// ----------------- //
|
|
|
|
func AddLargeConst(a uint64, out []uint64) {
|
|
// ppc64x/power10:"ADD\t[$]4294967296,"
|
|
// ppc64x/power9:"MOVD\t[$]1", "SLD\t[$]32" "ADD\tR[0-9]*"
|
|
// ppc64x/power8:"MOVD\t[$]1", "SLD\t[$]32" "ADD\tR[0-9]*"
|
|
out[0] = a + 0x100000000
|
|
// ppc64x/power10:"ADD\t[$]-8589934592,"
|
|
// ppc64x/power9:"MOVD\t[$]-1", "SLD\t[$]33" "ADD\tR[0-9]*"
|
|
// ppc64x/power8:"MOVD\t[$]-1", "SLD\t[$]33" "ADD\tR[0-9]*"
|
|
out[1] = a + 0xFFFFFFFE00000000
|
|
// ppc64x/power10:"ADD\t[$]1234567,"
|
|
// ppc64x/power9:"ADDIS\t[$]19,", "ADD\t[$]-10617,"
|
|
// ppc64x/power8:"ADDIS\t[$]19,", "ADD\t[$]-10617,"
|
|
out[2] = a + 1234567
|
|
// ppc64x/power10:"ADD\t[$]-1234567,"
|
|
// ppc64x/power9:"ADDIS\t[$]-19,", "ADD\t[$]10617,"
|
|
// ppc64x/power8:"ADDIS\t[$]-19,", "ADD\t[$]10617,"
|
|
out[3] = a - 1234567
|
|
// ppc64x/power10:"ADD\t[$]2147450879,"
|
|
// ppc64x/power9:"ADDIS\t[$]32767,", "ADD\t[$]32767,"
|
|
// ppc64x/power8:"ADDIS\t[$]32767,", "ADD\t[$]32767,"
|
|
out[4] = a + 0x7FFF7FFF
|
|
// ppc64x/power10:"ADD\t[$]-2147483647,"
|
|
// ppc64x/power9:"ADDIS\t[$]-32768,", "ADD\t[$]1,"
|
|
// ppc64x/power8:"ADDIS\t[$]-32768,", "ADD\t[$]1,"
|
|
out[5] = a - 2147483647
|
|
// ppc64x:"ADDIS\t[$]-32768,", ^"ADD\t"
|
|
out[6] = a - 2147483648
|
|
// ppc64x:"ADD\t[$]2147450880,", ^"ADDIS\t"
|
|
out[7] = a + 0x7FFF8000
|
|
// ppc64x:"ADD\t[$]-32768,", ^"ADDIS\t"
|
|
out[8] = a - 32768
|
|
// ppc64x/power10:"ADD\t[$]-32769,"
|
|
// ppc64x/power9:"ADDIS\t[$]-1,", "ADD\t[$]32767,"
|
|
// ppc64x/power8:"ADDIS\t[$]-1,", "ADD\t[$]32767,"
|
|
out[9] = a - 32769
|
|
}
|
|
|
|
// ----------------- //
|
|
// Subtraction //
|
|
// ----------------- //
|
|
|
|
var ef int
|
|
|
|
func SubMem(arr []int, b, c, d int) int {
|
|
// 386:`SUBL\s[A-Z]+,\s8\([A-Z]+\)`
|
|
// amd64:`SUBQ\s[A-Z]+,\s16\([A-Z]+\)`
|
|
arr[2] -= b
|
|
// 386:`SUBL\s[A-Z]+,\s12\([A-Z]+\)`
|
|
// amd64:`SUBQ\s[A-Z]+,\s24\([A-Z]+\)`
|
|
arr[3] -= b
|
|
// 386:`DECL\s16\([A-Z]+\)`
|
|
arr[4]--
|
|
// 386:`ADDL\s[$]-20,\s20\([A-Z]+\)`
|
|
arr[5] -= 20
|
|
// 386:`SUBL\s\([A-Z]+\)\([A-Z]+\*4\),\s[A-Z]+`
|
|
ef -= arr[b]
|
|
// 386:`SUBL\s[A-Z]+,\s\([A-Z]+\)\([A-Z]+\*4\)`
|
|
arr[c] -= b
|
|
// 386:`ADDL\s[$]-15,\s\([A-Z]+\)\([A-Z]+\*4\)`
|
|
arr[d] -= 15
|
|
// 386:`DECL\s\([A-Z]+\)\([A-Z]+\*4\)`
|
|
arr[b]--
|
|
// amd64:`DECQ\s64\([A-Z]+\)`
|
|
arr[8]--
|
|
// 386:"SUBL\t4"
|
|
// amd64:"SUBQ\t8"
|
|
return arr[0] - arr[1]
|
|
}
|
|
|
|
func SubFromConst(a int) int {
|
|
// ppc64x: `SUBC\tR[0-9]+,\s[$]40,\sR`
|
|
b := 40 - a
|
|
return b
|
|
}
|
|
|
|
func SubFromConstNeg(a int) int {
|
|
// ppc64x: `ADD\t[$]40,\sR[0-9]+,\sR`
|
|
c := 40 - (-a)
|
|
return c
|
|
}
|
|
|
|
func SubSubFromConst(a int) int {
|
|
// ppc64x: `ADD\t[$]20,\sR[0-9]+,\sR`
|
|
c := 40 - (20 - a)
|
|
return c
|
|
}
|
|
|
|
func AddSubFromConst(a int) int {
|
|
// ppc64x: `SUBC\tR[0-9]+,\s[$]60,\sR`
|
|
c := 40 + (20 - a)
|
|
return c
|
|
}
|
|
|
|
func NegSubFromConst(a int) int {
|
|
// ppc64x: `ADD\t[$]-20,\sR[0-9]+,\sR`
|
|
c := -(20 - a)
|
|
return c
|
|
}
|
|
|
|
func NegAddFromConstNeg(a int) int {
|
|
// ppc64x: `SUBC\tR[0-9]+,\s[$]40,\sR`
|
|
c := -(-40 + a)
|
|
return c
|
|
}
|
|
|
|
func SubSubNegSimplify(a, b int) int {
|
|
// amd64:"NEGQ"
|
|
// ppc64x:"NEG"
|
|
r := (a - b) - a
|
|
return r
|
|
}
|
|
|
|
func SubAddSimplify(a, b int) int {
|
|
// amd64:-"SUBQ",-"ADDQ"
|
|
// ppc64x:-"SUB",-"ADD"
|
|
r := a + (b - a)
|
|
return r
|
|
}
|
|
|
|
func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
|
|
// amd64:-"ADDQ"
|
|
r := (a + b) - (a + c)
|
|
// amd64:-"ADDQ"
|
|
r1 := (a + b) - (c + a)
|
|
// amd64:-"ADDQ"
|
|
r2 := (b + a) - (a + c)
|
|
// amd64:-"ADDQ"
|
|
r3 := (b + a) - (c + a)
|
|
// amd64:-"SUBQ"
|
|
r4 := (a - c) + (c + b)
|
|
// amd64:-"SUBQ"
|
|
r5 := (a - c) + (b + c)
|
|
return r, r1, r2, r3, r4, r5
|
|
}
|
|
|
|
func SubAddNegSimplify(a, b int) int {
|
|
// amd64:"NEGQ",-"ADDQ",-"SUBQ"
|
|
// ppc64x:"NEG",-"ADD",-"SUB"
|
|
r := a - (b + a)
|
|
return r
|
|
}
|
|
|
|
func AddAddSubSimplify(a, b, c int) int {
|
|
// amd64:-"SUBQ"
|
|
// ppc64x:-"SUB"
|
|
r := a + (b + (c - a))
|
|
return r
|
|
}
|
|
|
|
// -------------------- //
|
|
// Multiplication //
|
|
// -------------------- //
|
|
|
|
func Pow2Muls(n1, n2 int) (int, int) {
|
|
// amd64:"SHLQ\t[$]5",-"IMULQ"
|
|
// 386:"SHLL\t[$]5",-"IMULL"
|
|
// arm:"SLL\t[$]5",-"MUL"
|
|
// arm64:"LSL\t[$]5",-"MUL"
|
|
// ppc64x:"SLD\t[$]5",-"MUL"
|
|
a := n1 * 32
|
|
|
|
// amd64:"SHLQ\t[$]6",-"IMULQ"
|
|
// 386:"SHLL\t[$]6",-"IMULL"
|
|
// arm:"SLL\t[$]6",-"MUL"
|
|
// arm64:`NEG\sR[0-9]+<<6,\sR[0-9]+`,-`LSL`,-`MUL`
|
|
// ppc64x:"SLD\t[$]6","NEG\\sR[0-9]+,\\sR[0-9]+",-"MUL"
|
|
b := -64 * n2
|
|
|
|
return a, b
|
|
}
|
|
|
|
func Mul_96(n int) int {
|
|
// amd64:`SHLQ\t[$]5`,`LEAQ\t\(.*\)\(.*\*2\),`,-`IMULQ`
|
|
// 386:`SHLL\t[$]5`,`LEAL\t\(.*\)\(.*\*2\),`,-`IMULL`
|
|
// arm64:`LSL\t[$]5`,`ADD\sR[0-9]+<<1,\sR[0-9]+`,-`MUL`
|
|
// arm:`SLL\t[$]5`,`ADD\sR[0-9]+<<1,\sR[0-9]+`,-`MUL`
|
|
// s390x:`SLD\t[$]5`,`SLD\t[$]6`,-`MULLD`
|
|
return n * 96
|
|
}
|
|
|
|
func Mul_n120(n int) int {
|
|
// s390x:`SLD\t[$]3`,`SLD\t[$]7`,-`MULLD`
|
|
return n * -120
|
|
}
|
|
|
|
func MulMemSrc(a []uint32, b []float32) {
|
|
// 386:`IMULL\s4\([A-Z]+\),\s[A-Z]+`
|
|
a[0] *= a[1]
|
|
// 386/sse2:`MULSS\s4\([A-Z]+\),\sX[0-9]+`
|
|
// amd64:`MULSS\s4\([A-Z]+\),\sX[0-9]+`
|
|
b[0] *= b[1]
|
|
}
|
|
|
|
// Multiplications merging tests
|
|
|
|
func MergeMuls1(n int) int {
|
|
// amd64:"IMUL3Q\t[$]46"
|
|
// 386:"IMUL3L\t[$]46"
|
|
// ppc64x:"MULLD\t[$]46"
|
|
return 15*n + 31*n // 46n
|
|
}
|
|
|
|
func MergeMuls2(n int) int {
|
|
// amd64:"IMUL3Q\t[$]23","(ADDQ\t[$]29)|(LEAQ\t29)"
|
|
// 386:"IMUL3L\t[$]23","ADDL\t[$]29"
|
|
// ppc64x/power9:"MADDLD",-"MULLD\t[$]23",-"ADD\t[$]29"
|
|
// ppc64x/power8:"MULLD\t[$]23","ADD\t[$]29"
|
|
return 5*n + 7*(n+1) + 11*(n+2) // 23n + 29
|
|
}
|
|
|
|
func MergeMuls3(a, n int) int {
|
|
// amd64:"ADDQ\t[$]19",-"IMULQ\t[$]19"
|
|
// 386:"ADDL\t[$]19",-"IMULL\t[$]19"
|
|
// ppc64x:"ADD\t[$]19",-"MULLD\t[$]19"
|
|
return a*n + 19*n // (a+19)n
|
|
}
|
|
|
|
func MergeMuls4(n int) int {
|
|
// amd64:"IMUL3Q\t[$]14"
|
|
// 386:"IMUL3L\t[$]14"
|
|
// ppc64x:"MULLD\t[$]14"
|
|
return 23*n - 9*n // 14n
|
|
}
|
|
|
|
func MergeMuls5(a, n int) int {
|
|
// amd64:"ADDQ\t[$]-19",-"IMULQ\t[$]19"
|
|
// 386:"ADDL\t[$]-19",-"IMULL\t[$]19"
|
|
// ppc64x:"ADD\t[$]-19",-"MULLD\t[$]19"
|
|
return a*n - 19*n // (a-19)n
|
|
}
|
|
|
|
// -------------- //
|
|
// Division //
|
|
// -------------- //
|
|
|
|
func DivMemSrc(a []float64) {
|
|
// 386/sse2:`DIVSD\s8\([A-Z]+\),\sX[0-9]+`
|
|
// amd64:`DIVSD\s8\([A-Z]+\),\sX[0-9]+`
|
|
a[0] /= a[1]
|
|
}
|
|
|
|
func Pow2Divs(n1 uint, n2 int) (uint, int) {
|
|
// 386:"SHRL\t[$]5",-"DIVL"
|
|
// amd64:"SHRQ\t[$]5",-"DIVQ"
|
|
// arm:"SRL\t[$]5",-".*udiv"
|
|
// arm64:"LSR\t[$]5",-"UDIV"
|
|
// ppc64x:"SRD"
|
|
a := n1 / 32 // unsigned
|
|
|
|
// amd64:"SARQ\t[$]6",-"IDIVQ"
|
|
// 386:"SARL\t[$]6",-"IDIVL"
|
|
// arm:"SRA\t[$]6",-".*udiv"
|
|
// arm64:"ASR\t[$]6",-"SDIV"
|
|
// ppc64x:"SRAD"
|
|
b := n2 / 64 // signed
|
|
|
|
return a, b
|
|
}
|
|
|
|
// Check that constant divisions get turned into MULs
|
|
func ConstDivs(n1 uint, n2 int) (uint, int) {
|
|
// amd64:"MOVQ\t[$]-1085102592571150095","MULQ",-"DIVQ"
|
|
// 386:"MOVL\t[$]-252645135","MULL",-"DIVL"
|
|
// arm64:`MOVD`,`UMULH`,-`DIV`
|
|
// arm:`MOVW`,`MUL`,-`.*udiv`
|
|
a := n1 / 17 // unsigned
|
|
|
|
// amd64:"MOVQ\t[$]-1085102592571150095","IMULQ",-"IDIVQ"
|
|
// 386:"MOVL\t[$]-252645135","IMULL",-"IDIVL"
|
|
// arm64:`SMULH`,-`DIV`
|
|
// arm:`MOVW`,`MUL`,-`.*udiv`
|
|
b := n2 / 17 // signed
|
|
|
|
return a, b
|
|
}
|
|
|
|
func FloatDivs(a []float32) float32 {
|
|
// amd64:`DIVSS\s8\([A-Z]+\),\sX[0-9]+`
|
|
// 386/sse2:`DIVSS\s8\([A-Z]+\),\sX[0-9]+`
|
|
return a[1] / a[2]
|
|
}
|
|
|
|
func Pow2Mods(n1 uint, n2 int) (uint, int) {
|
|
// 386:"ANDL\t[$]31",-"DIVL"
|
|
// amd64:"ANDL\t[$]31",-"DIVQ"
|
|
// arm:"AND\t[$]31",-".*udiv"
|
|
// arm64:"AND\t[$]31",-"UDIV"
|
|
// ppc64x:"RLDICL"
|
|
a := n1 % 32 // unsigned
|
|
|
|
// 386:"SHRL",-"IDIVL"
|
|
// amd64:"SHRQ",-"IDIVQ"
|
|
// arm:"SRA",-".*udiv"
|
|
// arm64:"ASR",-"REM"
|
|
// ppc64x:"SRAD"
|
|
b := n2 % 64 // signed
|
|
|
|
return a, b
|
|
}
|
|
|
|
// Check that signed divisibility checks get converted to AND on low bits
|
|
func Pow2DivisibleSigned(n1, n2 int) (bool, bool) {
|
|
// 386:"TESTL\t[$]63",-"DIVL",-"SHRL"
|
|
// amd64:"TESTQ\t[$]63",-"DIVQ",-"SHRQ"
|
|
// arm:"AND\t[$]63",-".*udiv",-"SRA"
|
|
// arm64:"TST\t[$]63",-"UDIV",-"ASR",-"AND"
|
|
// ppc64x:"ANDCC",-"RLDICL",-"SRAD",-"CMP"
|
|
a := n1%64 == 0 // signed divisible
|
|
|
|
// 386:"TESTL\t[$]63",-"DIVL",-"SHRL"
|
|
// amd64:"TESTQ\t[$]63",-"DIVQ",-"SHRQ"
|
|
// arm:"AND\t[$]63",-".*udiv",-"SRA"
|
|
// arm64:"TST\t[$]63",-"UDIV",-"ASR",-"AND"
|
|
// ppc64x:"ANDCC",-"RLDICL",-"SRAD",-"CMP"
|
|
b := n2%64 != 0 // signed indivisible
|
|
|
|
return a, b
|
|
}
|
|
|
|
// Check that constant modulo divs get turned into MULs
|
|
func ConstMods(n1 uint, n2 int) (uint, int) {
|
|
// amd64:"MOVQ\t[$]-1085102592571150095","MULQ",-"DIVQ"
|
|
// 386:"MOVL\t[$]-252645135","MULL",-"DIVL"
|
|
// arm64:`MOVD`,`UMULH`,-`DIV`
|
|
// arm:`MOVW`,`MUL`,-`.*udiv`
|
|
a := n1 % 17 // unsigned
|
|
|
|
// amd64:"MOVQ\t[$]-1085102592571150095","IMULQ",-"IDIVQ"
|
|
// 386:"MOVL\t[$]-252645135","IMULL",-"IDIVL"
|
|
// arm64:`SMULH`,-`DIV`
|
|
// arm:`MOVW`,`MUL`,-`.*udiv`
|
|
b := n2 % 17 // signed
|
|
|
|
return a, b
|
|
}
|
|
|
|
// Check that divisibility checks x%c==0 are converted to MULs and rotates
|
|
func DivisibleU(n uint) (bool, bool) {
|
|
// amd64:"MOVQ\t[$]-6148914691236517205","IMULQ","ROLQ\t[$]63",-"DIVQ"
|
|
// 386:"IMUL3L\t[$]-1431655765","ROLL\t[$]31",-"DIVQ"
|
|
// arm64:"MOVD\t[$]-6148914691236517205","MOVD\t[$]3074457345618258602","MUL","ROR",-"DIV"
|
|
// arm:"MUL","CMP\t[$]715827882",-".*udiv"
|
|
// ppc64x:"MULLD","ROTL\t[$]63"
|
|
even := n%6 == 0
|
|
|
|
// amd64:"MOVQ\t[$]-8737931403336103397","IMULQ",-"ROLQ",-"DIVQ"
|
|
// 386:"IMUL3L\t[$]678152731",-"ROLL",-"DIVQ"
|
|
// arm64:"MOVD\t[$]-8737931403336103397","MUL",-"ROR",-"DIV"
|
|
// arm:"MUL","CMP\t[$]226050910",-".*udiv"
|
|
// ppc64x:"MULLD",-"ROTL"
|
|
odd := n%19 == 0
|
|
|
|
return even, odd
|
|
}
|
|
|
|
func Divisible(n int) (bool, bool) {
|
|
// amd64:"IMULQ","ADD","ROLQ\t[$]63",-"DIVQ"
|
|
// 386:"IMUL3L\t[$]-1431655765","ADDL\t[$]715827882","ROLL\t[$]31",-"DIVQ"
|
|
// arm64:"MOVD\t[$]-6148914691236517205","MOVD\t[$]3074457345618258602","MUL","ADD\tR","ROR",-"DIV"
|
|
// arm:"MUL","ADD\t[$]715827882",-".*udiv"
|
|
// ppc64x/power8:"MULLD","ADD","ROTL\t[$]63"
|
|
// ppc64x/power9:"MADDLD","ROTL\t[$]63"
|
|
even := n%6 == 0
|
|
|
|
// amd64:"IMULQ","ADD",-"ROLQ",-"DIVQ"
|
|
// 386:"IMUL3L\t[$]678152731","ADDL\t[$]113025455",-"ROLL",-"DIVQ"
|
|
// arm64:"MUL","MOVD\t[$]485440633518672410","ADD",-"ROR",-"DIV"
|
|
// arm:"MUL","ADD\t[$]113025455",-".*udiv"
|
|
// ppc64x/power8:"MULLD","ADD",-"ROTL"
|
|
// ppc64x/power9:"MADDLD",-"ROTL"
|
|
odd := n%19 == 0
|
|
|
|
return even, odd
|
|
}
|
|
|
|
// Check that fix-up code is not generated for divisions where it has been proven that
|
|
// that the divisor is not -1 or that the dividend is > MinIntNN.
|
|
func NoFix64A(divr int64) (int64, int64) {
|
|
var d int64 = 42
|
|
var e int64 = 84
|
|
if divr > 5 {
|
|
d /= divr // amd64:-"JMP"
|
|
e %= divr // amd64:-"JMP"
|
|
// The following statement is to avoid conflict between the above check
|
|
// and the normal JMP generated at the end of the block.
|
|
d += e
|
|
}
|
|
return d, e
|
|
}
|
|
|
|
func NoFix64B(divd int64) (int64, int64) {
|
|
var d int64
|
|
var e int64
|
|
var divr int64 = -1
|
|
if divd > -9223372036854775808 {
|
|
d = divd / divr // amd64:-"JMP"
|
|
e = divd % divr // amd64:-"JMP"
|
|
d += e
|
|
}
|
|
return d, e
|
|
}
|
|
|
|
func NoFix32A(divr int32) (int32, int32) {
|
|
var d int32 = 42
|
|
var e int32 = 84
|
|
if divr > 5 {
|
|
// amd64:-"JMP"
|
|
// 386:-"JMP"
|
|
d /= divr
|
|
// amd64:-"JMP"
|
|
// 386:-"JMP"
|
|
e %= divr
|
|
d += e
|
|
}
|
|
return d, e
|
|
}
|
|
|
|
func NoFix32B(divd int32) (int32, int32) {
|
|
var d int32
|
|
var e int32
|
|
var divr int32 = -1
|
|
if divd > -2147483648 {
|
|
// amd64:-"JMP"
|
|
// 386:-"JMP"
|
|
d = divd / divr
|
|
// amd64:-"JMP"
|
|
// 386:-"JMP"
|
|
e = divd % divr
|
|
d += e
|
|
}
|
|
return d, e
|
|
}
|
|
|
|
func NoFix16A(divr int16) (int16, int16) {
|
|
var d int16 = 42
|
|
var e int16 = 84
|
|
if divr > 5 {
|
|
// amd64:-"JMP"
|
|
// 386:-"JMP"
|
|
d /= divr
|
|
// amd64:-"JMP"
|
|
// 386:-"JMP"
|
|
e %= divr
|
|
d += e
|
|
}
|
|
return d, e
|
|
}
|
|
|
|
func NoFix16B(divd int16) (int16, int16) {
|
|
var d int16
|
|
var e int16
|
|
var divr int16 = -1
|
|
if divd > -32768 {
|
|
// amd64:-"JMP"
|
|
// 386:-"JMP"
|
|
d = divd / divr
|
|
// amd64:-"JMP"
|
|
// 386:-"JMP"
|
|
e = divd % divr
|
|
d += e
|
|
}
|
|
return d, e
|
|
}
|
|
|
|
// Check that len() and cap() calls divided by powers of two are
|
|
// optimized into shifts and ands
|
|
|
|
func LenDiv1(a []int) int {
|
|
// 386:"SHRL\t[$]10"
|
|
// amd64:"SHRQ\t[$]10"
|
|
// arm64:"LSR\t[$]10",-"SDIV"
|
|
// arm:"SRL\t[$]10",-".*udiv"
|
|
// ppc64x:"SRD"\t[$]10"
|
|
return len(a) / 1024
|
|
}
|
|
|
|
func LenDiv2(s string) int {
|
|
// 386:"SHRL\t[$]11"
|
|
// amd64:"SHRQ\t[$]11"
|
|
// arm64:"LSR\t[$]11",-"SDIV"
|
|
// arm:"SRL\t[$]11",-".*udiv"
|
|
// ppc64x:"SRD\t[$]11"
|
|
return len(s) / (4097 >> 1)
|
|
}
|
|
|
|
func LenMod1(a []int) int {
|
|
// 386:"ANDL\t[$]1023"
|
|
// amd64:"ANDL\t[$]1023"
|
|
// arm64:"AND\t[$]1023",-"SDIV"
|
|
// arm/6:"AND",-".*udiv"
|
|
// arm/7:"BFC",-".*udiv",-"AND"
|
|
// ppc64x:"RLDICL"
|
|
return len(a) % 1024
|
|
}
|
|
|
|
func LenMod2(s string) int {
|
|
// 386:"ANDL\t[$]2047"
|
|
// amd64:"ANDL\t[$]2047"
|
|
// arm64:"AND\t[$]2047",-"SDIV"
|
|
// arm/6:"AND",-".*udiv"
|
|
// arm/7:"BFC",-".*udiv",-"AND"
|
|
// ppc64x:"RLDICL"
|
|
return len(s) % (4097 >> 1)
|
|
}
|
|
|
|
func CapDiv(a []int) int {
|
|
// 386:"SHRL\t[$]12"
|
|
// amd64:"SHRQ\t[$]12"
|
|
// arm64:"LSR\t[$]12",-"SDIV"
|
|
// arm:"SRL\t[$]12",-".*udiv"
|
|
// ppc64x:"SRD\t[$]12"
|
|
return cap(a) / ((1 << 11) + 2048)
|
|
}
|
|
|
|
func CapMod(a []int) int {
|
|
// 386:"ANDL\t[$]4095"
|
|
// amd64:"ANDL\t[$]4095"
|
|
// arm64:"AND\t[$]4095",-"SDIV"
|
|
// arm/6:"AND",-".*udiv"
|
|
// arm/7:"BFC",-".*udiv",-"AND"
|
|
// ppc64x:"RLDICL"
|
|
return cap(a) % ((1 << 11) + 2048)
|
|
}
|
|
|
|
func AddMul(x int) int {
|
|
// amd64:"LEAQ\t1"
|
|
return 2*x + 1
|
|
}
|
|
|
|
func MULA(a, b, c uint32) (uint32, uint32, uint32) {
|
|
// arm:`MULA`,-`MUL\s`
|
|
// arm64:`MADDW`,-`MULW`
|
|
r0 := a*b + c
|
|
// arm:`MULA`,-`MUL\s`
|
|
// arm64:`MADDW`,-`MULW`
|
|
r1 := c*79 + a
|
|
// arm:`ADD`,-`MULA`,-`MUL\s`
|
|
// arm64:`ADD`,-`MADD`,-`MULW`
|
|
// ppc64x:`ADD`,-`MULLD`
|
|
r2 := b*64 + c
|
|
return r0, r1, r2
|
|
}
|
|
|
|
func MULS(a, b, c uint32) (uint32, uint32, uint32) {
|
|
// arm/7:`MULS`,-`MUL\s`
|
|
// arm/6:`SUB`,`MUL\s`,-`MULS`
|
|
// arm64:`MSUBW`,-`MULW`
|
|
r0 := c - a*b
|
|
// arm/7:`MULS`,-`MUL\s`
|
|
// arm/6:`SUB`,`MUL\s`,-`MULS`
|
|
// arm64:`MSUBW`,-`MULW`
|
|
r1 := a - c*79
|
|
// arm/7:`SUB`,-`MULS`,-`MUL\s`
|
|
// arm64:`SUB`,-`MSUBW`,-`MULW`
|
|
// ppc64x:`SUB`,-`MULLD`
|
|
r2 := c - b*64
|
|
return r0, r1, r2
|
|
}
|
|
|
|
func addSpecial(a, b, c uint32) (uint32, uint32, uint32) {
|
|
// amd64:`INCL`
|
|
a++
|
|
// amd64:`DECL`
|
|
b--
|
|
// amd64:`SUBL.*-128`
|
|
c += 128
|
|
return a, b, c
|
|
}
|
|
|
|
// Divide -> shift rules usually require fixup for negative inputs.
|
|
// If the input is non-negative, make sure the fixup is eliminated.
|
|
func divInt(v int64) int64 {
|
|
if v < 0 {
|
|
return 0
|
|
}
|
|
// amd64:-`.*SARQ.*63,`, -".*SHRQ", ".*SARQ.*[$]9,"
|
|
return v / 512
|
|
}
|
|
|
|
// The reassociate rules "x - (z + C) -> (x - z) - C" and
|
|
// "(z + C) -x -> C + (z - x)" can optimize the following cases.
|
|
func constantFold1(i0, j0, i1, j1, i2, j2, i3, j3 int) (int, int, int, int) {
|
|
// arm64:"SUB","ADD\t[$]2"
|
|
// ppc64x:"SUB","ADD\t[$]2"
|
|
r0 := (i0 + 3) - (j0 + 1)
|
|
// arm64:"SUB","SUB\t[$]4"
|
|
// ppc64x:"SUB","ADD\t[$]-4"
|
|
r1 := (i1 - 3) - (j1 + 1)
|
|
// arm64:"SUB","ADD\t[$]4"
|
|
// ppc64x:"SUB","ADD\t[$]4"
|
|
r2 := (i2 + 3) - (j2 - 1)
|
|
// arm64:"SUB","SUB\t[$]2"
|
|
// ppc64x:"SUB","ADD\t[$]-2"
|
|
r3 := (i3 - 3) - (j3 - 1)
|
|
return r0, r1, r2, r3
|
|
}
|
|
|
|
// The reassociate rules "x - (z + C) -> (x - z) - C" and
|
|
// "(C - z) - x -> C - (z + x)" can optimize the following cases.
|
|
func constantFold2(i0, j0, i1, j1 int) (int, int) {
|
|
// arm64:"ADD","MOVD\t[$]2","SUB"
|
|
// ppc64x: `SUBC\tR[0-9]+,\s[$]2,\sR`
|
|
r0 := (3 - i0) - (j0 + 1)
|
|
// arm64:"ADD","MOVD\t[$]4","SUB"
|
|
// ppc64x: `SUBC\tR[0-9]+,\s[$]4,\sR`
|
|
r1 := (3 - i1) - (j1 - 1)
|
|
return r0, r1
|
|
}
|
|
|
|
func constantFold3(i, j int) int {
|
|
// arm64: "MOVD\t[$]30","MUL",-"ADD",-"LSL"
|
|
// ppc64x:"MULLD\t[$]30","MULLD"
|
|
r := (5 * i) * (6 * j)
|
|
return r
|
|
}
|