FAQ
Reviewers: rsc,

Message:
Hello rsc@golang.org (cc: golang-dev@googlegroups.com,
remyoudompheng@gmail.com),

I'd like you to review this change to
https://go.googlecode.com/hg/


Description:
cmd/gc: recognize small TPTR64 values as small integer constants.

Given the following Go program:

func sum(s []int) int {
ret := 0
for _, x := range s {
ret += x
}
return ret
}

6g would previously generate:

--- prog list "sum" ---
0000 (main.go:3) TEXT sum+0(SB),$0-24
0001 (main.go:5) MOVQ s+0(FP),CX
0002 (main.go:5) MOVL s+8(FP),DI
0003 (main.go:5) MOVL s+12(FP),BX
0004 (main.go:4) MOVL $0,SI
0005 (main.go:5) MOVL $0,AX
0006 (main.go:5) JMP ,8
0007 (main.go:5) INCL ,AX
0008 (main.go:5) CMPL AX,DI
0009 (main.go:5) JGE $0,16
0010 (main.go:5) MOVL (CX),DX
0011 (main.go:5) MOVQ $4,BX
0012 (main.go:5) ADDQ CX,BX
0013 (main.go:5) MOVQ BX,CX
0014 (main.go:6) ADDL DX,SI
0015 (main.go:5) JMP ,7
0016 (main.go:8) MOVL SI,.noname+16(FP)
0017 (main.go:8) RET ,

and now generates:

--- prog list "sum" ---
0000 (main.go:3) TEXT sum+0(SB),$0-24
0001 (main.go:5) MOVQ s+0(FP),CX
0002 (main.go:5) MOVL s+8(FP),DI
0003 (main.go:5) MOVL s+12(FP),BX
0004 (main.go:4) MOVL $0,SI
0005 (main.go:5) MOVL $0,AX
0006 (main.go:5) JMP ,8
0007 (main.go:5) INCL ,AX
0008 (main.go:5) CMPL AX,DI
0009 (main.go:5) JGE $0,14
0010 (main.go:5) MOVL (CX),BP
0011 (main.go:5) ADDQ $4,CX
0012 (main.go:6) ADDL BP,SI
0013 (main.go:5) JMP ,7
0014 (main.go:8) MOVL SI,.noname+16(FP)
0015 (main.go:8) RET ,

The key difference is that
0011 (main.go:5) MOVQ $4,BX
0012 (main.go:5) ADDQ CX,BX
0013 (main.go:5) MOVQ BX,CX
has changed to
0011 (main.go:5) ADDQ $4,CX

Please review this at http://codereview.appspot.com/6506089/

Affected files:
M src/cmd/gc/const.c


Index: src/cmd/gc/const.c
===================================================================
--- a/src/cmd/gc/const.c
+++ b/src/cmd/gc/const.c
@@ -1207,6 +1207,7 @@
case TIDEAL:
case TINT64:
case TUINT64:
+ case TPTR64:
if(mpcmpfixfix(n->val.u.xval, minintval[TINT32]) < 0
mpcmpfixfix(n->val.u.xval, maxintval[TINT32]) > 0)
break;

Search Discussions

  • Dave at Sep 9, 2012 at 1:38 am
    Nice. Could you explain why ret is a TPTR64? With my limited
    understanding of the compiler, ret := 0 means ret is of type int.

    http://codereview.appspot.com/6506089/
  • Remyoudompheng at Sep 9, 2012 at 5:19 am
    ret is not a PTR64. The range loop is tranformed by the frontend in:

    for i, p := 0, &s[0]; i < len(s); i++, p += sizeof(s[0]) {
    x := *p
    sum += x
    }

    hence the constant 4.

    http://codereview.appspot.com/6506089/
  • Dave Cheney at Sep 9, 2012 at 5:37 am
    Ahh, thank you. To indulge me further, why does it matter if the size of the slice element is smaller than 32 bits?
    On 09/09/2012, at 15:19, remyoudompheng@gmail.com wrote:

    ret is not a PTR64. The range loop is tranformed by the frontend in:

    for i, p := 0, &s[0]; i < len(s); i++, p += sizeof(s[0]) {
    x := *p
    sum += x
    }

    hence the constant 4.

    http://codereview.appspot.com/6506089/
  • Remyoudompheng at Sep 9, 2012 at 6:22 am

    On 2012/09/09 05:37:49, dfc wrote:
    Ahh, thank you. To indulge me further, why does it matter if the size of the
    slice element is smaller than 32 bits?
    It's because (correct me if I'm wrong) although x86-64 is 64 bits, there
    is no instruction to add a 64-bit constant to a register. You can only
    add a 32-bit constant, or load a 64-bit constant into a separate
    register and then add it.

    http://codereview.appspot.com/6506089/
  • Dave at Sep 9, 2012 at 6:35 am

    It's because (correct me if I'm wrong) although x86-64 is 64 bits,
    there is no
    instruction to add a 64-bit constant to a register. You can only add a 32-bit
    constant, or load a 64-bit constant into a separate register and then
    add it.

    That would make sense, a 64bit constant would make for a very long
    instruction. Reading the intel manual, the occurrence of imm64 is
    extremely rare, and not an option with ADD/ADC.


    http://codereview.appspot.com/6506089/
  • Rsc at Sep 11, 2012 at 2:19 am
  • Nigeltao at Sep 11, 2012 at 9:44 am
    *** Submitted as
    http://code.google.com/p/go/source/detail?r=46e193d130ce ***

    cmd/gc: recognize small TPTR64 values as small integer constants.

    Given the following Go program:

    func sum(s []int) int {
    ret := 0
    for _, x := range s {
    ret += x
    }
    return ret
    }

    6g would previously generate:

    --- prog list "sum" ---
    0000 (main.go:3) TEXT sum+0(SB),$0-24
    0001 (main.go:5) MOVQ s+0(FP),CX
    0002 (main.go:5) MOVL s+8(FP),DI
    0003 (main.go:5) MOVL s+12(FP),BX
    0004 (main.go:4) MOVL $0,SI
    0005 (main.go:5) MOVL $0,AX
    0006 (main.go:5) JMP ,8
    0007 (main.go:5) INCL ,AX
    0008 (main.go:5) CMPL AX,DI
    0009 (main.go:5) JGE $0,16
    0010 (main.go:5) MOVL (CX),DX
    0011 (main.go:5) MOVQ $4,BX
    0012 (main.go:5) ADDQ CX,BX
    0013 (main.go:5) MOVQ BX,CX
    0014 (main.go:6) ADDL DX,SI
    0015 (main.go:5) JMP ,7
    0016 (main.go:8) MOVL SI,.noname+16(FP)
    0017 (main.go:8) RET ,

    and now generates:

    --- prog list "sum" ---
    0000 (main.go:3) TEXT sum+0(SB),$0-24
    0001 (main.go:5) MOVQ s+0(FP),CX
    0002 (main.go:5) MOVL s+8(FP),DI
    0003 (main.go:5) MOVL s+12(FP),BX
    0004 (main.go:4) MOVL $0,SI
    0005 (main.go:5) MOVL $0,AX
    0006 (main.go:5) JMP ,8
    0007 (main.go:5) INCL ,AX
    0008 (main.go:5) CMPL AX,DI
    0009 (main.go:5) JGE $0,14
    0010 (main.go:5) MOVL (CX),BP
    0011 (main.go:5) ADDQ $4,CX
    0012 (main.go:6) ADDL BP,SI
    0013 (main.go:5) JMP ,7
    0014 (main.go:8) MOVL SI,.noname+16(FP)
    0015 (main.go:8) RET ,

    The key difference is that
    0011 (main.go:5) MOVQ $4,BX
    0012 (main.go:5) ADDQ CX,BX
    0013 (main.go:5) MOVQ BX,CX
    has changed to
    0011 (main.go:5) ADDQ $4,CX

    R=rsc, dave, remyoudompheng
    CC=golang-dev
    http://codereview.appspot.com/6506089


    http://codereview.appspot.com/6506089/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedSep 9, '12 at 1:33a
activeSep 11, '12 at 9:44a
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase