Skip to content

Commit 2acd3fa

Browse files
committed
cmd/compile: reduce switch cases in evconst
Reduces the number of cases that need to be tested and reduces size of the evconst function by 101 bytes. Change-Id: Ie56055a89d0dadd311fb940b51c488fc003694b9 Reviewed-on: https://go-review.googlesource.com/39950 Run-TryBot: Martin Möhrmann <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 3959e07 commit 2acd3fa

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

src/cmd/compile/internal/gc/const.go

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ func evconst(n *Node) {
705705
var rv Val
706706
var lno src.XPos
707707
var wr types.EType
708+
var ctype uint32
708709
var v Val
709710
var norig *Node
710711
var nn *Node
@@ -717,7 +718,13 @@ func evconst(n *Node) {
717718
v = copyval(v)
718719
}
719720

720-
switch uint32(n.Op)<<16 | uint32(v.Ctype()) {
721+
// rune values are int values for the purpose of constant folding.
722+
ctype = uint32(v.Ctype())
723+
if ctype == CTRUNE_ {
724+
ctype = CTINT_
725+
}
726+
727+
switch uint32(n.Op)<<16 | ctype {
721728
default:
722729
if !n.Diag() {
723730
yyerror("illegal constant expression %v %v", n.Op, nl.Type)
@@ -734,24 +741,20 @@ func evconst(n *Node) {
734741
}
735742
fallthrough
736743
case OCONV_ | CTINT_,
737-
OCONV_ | CTRUNE_,
738744
OCONV_ | CTFLT_,
739745
OCONV_ | CTCPLX_,
740746
OCONV_ | CTSTR_,
741747
OCONV_ | CTBOOL_:
742748
nl = convlit1(nl, n.Type, true, false)
743749
v = nl.Val()
744750

745-
case OPLUS_ | CTINT_,
746-
OPLUS_ | CTRUNE_:
751+
case OPLUS_ | CTINT_:
747752
break
748753

749-
case OMINUS_ | CTINT_,
750-
OMINUS_ | CTRUNE_:
754+
case OMINUS_ | CTINT_:
751755
v.U.(*Mpint).Neg()
752756

753-
case OCOM_ | CTINT_,
754-
OCOM_ | CTRUNE_:
757+
case OCOM_ | CTINT_:
755758
var et types.EType = Txxx
756759
if nl.Type != nil {
757760
et = nl.Type.Etype
@@ -899,25 +902,27 @@ func evconst(n *Node) {
899902
Fatalf("constant type mismatch %v(%d) %v(%d)", nl.Type, v.Ctype(), nr.Type, rv.Ctype())
900903
}
901904

905+
// rune values are int values for the purpose of constant folding.
906+
ctype = uint32(v.Ctype())
907+
if ctype == CTRUNE_ {
908+
ctype = CTINT_
909+
}
910+
902911
// run op
903-
switch uint32(n.Op)<<16 | uint32(v.Ctype()) {
912+
switch uint32(n.Op)<<16 | ctype {
904913
default:
905914
goto illegal
906915

907-
case OADD_ | CTINT_,
908-
OADD_ | CTRUNE_:
916+
case OADD_ | CTINT_:
909917
v.U.(*Mpint).Add(rv.U.(*Mpint))
910918

911-
case OSUB_ | CTINT_,
912-
OSUB_ | CTRUNE_:
919+
case OSUB_ | CTINT_:
913920
v.U.(*Mpint).Sub(rv.U.(*Mpint))
914921

915-
case OMUL_ | CTINT_,
916-
OMUL_ | CTRUNE_:
922+
case OMUL_ | CTINT_:
917923
v.U.(*Mpint).Mul(rv.U.(*Mpint))
918924

919-
case ODIV_ | CTINT_,
920-
ODIV_ | CTRUNE_:
925+
case ODIV_ | CTINT_:
921926
if rv.U.(*Mpint).CmpInt64(0) == 0 {
922927
yyerror("division by zero")
923928
v.U.(*Mpint).SetOverflow()
@@ -926,8 +931,7 @@ func evconst(n *Node) {
926931

927932
v.U.(*Mpint).Quo(rv.U.(*Mpint))
928933

929-
case OMOD_ | CTINT_,
930-
OMOD_ | CTRUNE_:
934+
case OMOD_ | CTINT_:
931935
if rv.U.(*Mpint).CmpInt64(0) == 0 {
932936
yyerror("division by zero")
933937
v.U.(*Mpint).SetOverflow()
@@ -936,28 +940,22 @@ func evconst(n *Node) {
936940

937941
v.U.(*Mpint).Rem(rv.U.(*Mpint))
938942

939-
case OLSH_ | CTINT_,
940-
OLSH_ | CTRUNE_:
943+
case OLSH_ | CTINT_:
941944
v.U.(*Mpint).Lsh(rv.U.(*Mpint))
942945

943-
case ORSH_ | CTINT_,
944-
ORSH_ | CTRUNE_:
946+
case ORSH_ | CTINT_:
945947
v.U.(*Mpint).Rsh(rv.U.(*Mpint))
946948

947-
case OOR_ | CTINT_,
948-
OOR_ | CTRUNE_:
949+
case OOR_ | CTINT_:
949950
v.U.(*Mpint).Or(rv.U.(*Mpint))
950951

951-
case OAND_ | CTINT_,
952-
OAND_ | CTRUNE_:
952+
case OAND_ | CTINT_:
953953
v.U.(*Mpint).And(rv.U.(*Mpint))
954954

955-
case OANDNOT_ | CTINT_,
956-
OANDNOT_ | CTRUNE_:
955+
case OANDNOT_ | CTINT_:
957956
v.U.(*Mpint).AndNot(rv.U.(*Mpint))
958957

959-
case OXOR_ | CTINT_,
960-
OXOR_ | CTRUNE_:
958+
case OXOR_ | CTINT_:
961959
v.U.(*Mpint).Xor(rv.U.(*Mpint))
962960

963961
case OADD_ | CTFLT_:
@@ -1015,43 +1013,37 @@ func evconst(n *Node) {
10151013
case ONE_ | CTNIL_:
10161014
goto setfalse
10171015

1018-
case OEQ_ | CTINT_,
1019-
OEQ_ | CTRUNE_:
1016+
case OEQ_ | CTINT_:
10201017
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) == 0 {
10211018
goto settrue
10221019
}
10231020
goto setfalse
10241021

1025-
case ONE_ | CTINT_,
1026-
ONE_ | CTRUNE_:
1022+
case ONE_ | CTINT_:
10271023
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) != 0 {
10281024
goto settrue
10291025
}
10301026
goto setfalse
10311027

1032-
case OLT_ | CTINT_,
1033-
OLT_ | CTRUNE_:
1028+
case OLT_ | CTINT_:
10341029
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) < 0 {
10351030
goto settrue
10361031
}
10371032
goto setfalse
10381033

1039-
case OLE_ | CTINT_,
1040-
OLE_ | CTRUNE_:
1034+
case OLE_ | CTINT_:
10411035
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) <= 0 {
10421036
goto settrue
10431037
}
10441038
goto setfalse
10451039

1046-
case OGE_ | CTINT_,
1047-
OGE_ | CTRUNE_:
1040+
case OGE_ | CTINT_:
10481041
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) >= 0 {
10491042
goto settrue
10501043
}
10511044
goto setfalse
10521045

1053-
case OGT_ | CTINT_,
1054-
OGT_ | CTRUNE_:
1046+
case OGT_ | CTINT_:
10551047
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) > 0 {
10561048
goto settrue
10571049
}

0 commit comments

Comments
 (0)