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: optimize loop invariant #24324

Closed
benshi001 opened this issue Mar 9, 2018 · 3 comments

Comments

Projects
None yet
5 participants
@benshi001
Copy link
Member

commented Mar 9, 2018

The following code

func ssa(a, b int, p []int) {
    	var i int
    	for i = 0; i < 8; i++ {
    	        p[i] = i + a * b
    	}
}

is compiled to

v19    00003 (6)       MOVD    "".b+8(RSP), R0
v20    00004 (6)       MOVD    "".p+24(RSP), R1
v24    00005 (6)       MOVD    "".p+16(RSP), R2
v21    00006 (6)       MOVD    "".a(RSP), R3
v13    00007 (6)       MOVD    $0, R4
b1     00008 (8)       JMP     12
v25    00009 (9)       ADD     R4<<3, R2, R6
v27    00010 (9)       MOVD    R5, (R6)
v30    00011 (8)       ADD     $1, R4, R4
v31    00012 (8)       CMP     $8, R4
b2     00013 (8)       BGE     19
v17    00014 (9)       MUL     R0, R3, R5
v18    00015 (9)       ADD     R4, R5, R5
v9     00016 (9)       CMP     R1, R4
b3     00017 (9)       BLO     9
b3     00018 (9)       JMP     20
b5     00019 (?)       RET

However the sub expression a*b (MUL R0, R3, R5) is a loop invariant, which should be calculated outside, not be calculated for 8 times.

@TocarIP

This comment has been minimized.

Copy link
Contributor

commented Mar 9, 2018

There is #15808

@andybons andybons changed the title Optimize loop invariant cmd/compile: optimize loop invariant Mar 9, 2018

@andybons andybons added this to the Unplanned milestone Mar 9, 2018

@andybons

This comment has been minimized.

Copy link
Member

commented Mar 9, 2018

@randall77

This comment has been minimized.

Copy link
Contributor

commented Mar 9, 2018

Yes, closing as a dup of #15808.
When we fix that issue we'll make sure this one is fixed as well.

@randall77 randall77 closed this Mar 9, 2018

@golang golang locked and limited conversation to collaborators Mar 9, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.