Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: fuse constant comparison with division by a constant #28861

Open
benshi001 opened this issue Nov 19, 2018 · 1 comment
Open

cmd/compile: fuse constant comparison with division by a constant #28861

benshi001 opened this issue Nov 19, 2018 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Performance
Milestone

Comments

@benshi001
Copy link
Member

func ssa(a uint32) uint32 {
	if a / 60 > 45 {
		return a
	} else {
		return a - 1
	}
}

is compiled to

       	00000 (4)	TEXT	"".ssa(SB)
       	00001 (4)	FUNCDATA	$0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
       	00002 (4)	FUNCDATA	$1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
       	00003 (4)	FUNCDATA	$3, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
 v7    	00004 (5)	PCDATA	$2, $0
 v7    	00005 (5)	PCDATA	$0, $0
 v7    	00006 (5)	MOVL	$2290649225, AX
 v12   	00007 (5)	MOVL	"".a(SP), CX
 v16   	00008 (5)	IMULQ	CX, AX
 v13   	00009 (5)	SHRQ	$37, AX
 v8    	00010 (5)	CMPL	AX, $45
 b1    	00011 (5)	JLS	14
 v15   	00012 (6)	MOVL	CX, "".~r1+8(SP)
 b2    	00013 (6)	RET
 v18   	00014 (8)	LEAL	-1(CX), AX
 v21   	00015 (8)	MOVL	AX, "".~r1+8(SP)
 b4    	00016 (8)	RET
       	00017 (?)	END

maybe better should equal to something like

func ssa(a uint32) uint32 {
	if a > 2700 {
		return a
	} else {
		return a - 1
	}
}
@bcmills bcmills changed the title cmd/compile: less optimized code generated cmd/compile: fuse constant comparison with division by a constant Nov 19, 2018
@bcmills bcmills added this to the Unplanned milestone Nov 19, 2018
@bcmills
Copy link
Contributor

bcmills commented Nov 19, 2018

CC @josharian @randall77

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. Performance
Projects
None yet
Development

No branches or pull requests

3 participants