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

Comments

Projects
None yet
2 participants
@benshi001
Member

benshi001 commented Nov 19, 2018

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 from cmd/compile: less optimized code generated to cmd/compile: fuse constant comparison with division by a constant Nov 19, 2018

@bcmills bcmills added the Performance label Nov 19, 2018

@bcmills bcmills added this to the Unplanned milestone Nov 19, 2018

@bcmills

This comment has been minimized.

Member

bcmills commented Nov 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment