Skip to content

Commit

Permalink
cmd/compile/internal/ssa/gen: eliminate unnecessary neg and xori on P…
Browse files Browse the repository at this point in the history
…PC64

This adds a few rules to PPC64 to eliminate some instructions:
- when an isel is used to generate a boolean value based on a
condition and followed by an xori to flip the result, it can
instead flip the operands in the isel and avoid the xori.
= when a neg follows a sub the operands to the sub can be
swapped and the neg avoided.

There are several opportunities in reflect.DeepEqual to omit
xori which improves some of its benchmarks by as much as
5%

Change-Id: I81bbc02c0f16995c65934b6f045867b731ab302b
Reviewed-on: https://go-review.googlesource.com/c/go/+/357509
Trust: Lynn Boger <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
  • Loading branch information
laboger committed Oct 21, 2021
1 parent 8b9c0d1 commit 8b0bea9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/cmd/compile/internal/ssa/gen/PPC64.rules
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,7 @@
(ADDconst [c] (SUBFCconst [d] x)) && is32Bit(c+d) => (SUBFCconst [c+d] x)
(NEG (ADDconst [c] x)) && is32Bit(-c) => (SUBFCconst [-c] x)
(NEG (SUBFCconst [c] x)) && is32Bit(-c) => (ADDconst [-c] x)
(NEG (SUB x y)) => (SUB y x)

// Use register moves instead of stores and loads to move int<=>float values
// Common with math Float64bits, Float64frombits
Expand Down Expand Up @@ -1087,7 +1088,7 @@
((CMP|CMPW|CMPU|CMPWU) x y) && canonLessThan(x,y) => (InvertFlags ((CMP|CMPW|CMPU|CMPWU) y x))

// ISEL auxInt values 0=LT 1=GT 2=EQ arg2 ? arg0 : arg1
// ISEL auxInt values 4=GE 5=LE 6=NE arg2 ? arg1 : arg0
// ISEL auxInt values 4=GE 5=LE 6=NE !arg2 ? arg1 : arg0
// ISELB special case where arg0, arg1 values are 0, 1

(Equal cmp) => (ISELB [2] (MOVDconst [1]) cmp)
Expand Down Expand Up @@ -1138,6 +1139,9 @@
(ISEL [n] x y (InvertFlags bool)) && n%4 == 0 => (ISEL [n+1] x y bool)
(ISEL [n] x y (InvertFlags bool)) && n%4 == 1 => (ISEL [n-1] x y bool)
(ISEL [n] x y (InvertFlags bool)) && n%4 == 2 => (ISEL [n] x y bool)
(XORconst [1] (ISELB [6] (MOVDconst [1]) cmp)) => (ISELB [2] (MOVDconst [1]) cmp)
(XORconst [1] (ISELB [5] (MOVDconst [1]) cmp)) => (ISELB [1] (MOVDconst [1]) cmp)
(XORconst [1] (ISELB [4] (MOVDconst [1]) cmp)) => (ISELB [0] (MOVDconst [1]) cmp)

// A particular pattern seen in cgo code:
(AND (MOVDconst [c]) x:(MOVBZload _ _)) => (ANDconst [c&0xFF] x)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/ssa/gen/PPC64Ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func init() {
{name: "CMPWUconst", argLength: 1, reg: gp1cr, asm: "CMPWU", aux: "Int32", typ: "Flags"},

// ISEL auxInt values 0=LT 1=GT 2=EQ arg2 ? arg0 : arg1
// ISEL auxInt values 4=GE 5=LE 6=NE arg2 ? arg1 : arg0
// ISEL auxInt values 4=GE 5=LE 6=NE !arg2 ? arg1 : arg0
// ISELB special case where arg0, arg1 values are 0, 1 for boolean result
{name: "ISEL", argLength: 3, reg: crgp21, asm: "ISEL", aux: "Int32", typ: "Int32"}, // see above
{name: "ISELB", argLength: 2, reg: crgp11, asm: "ISEL", aux: "Int32", typ: "Int32"}, // see above
Expand Down
68 changes: 68 additions & 0 deletions src/cmd/compile/internal/ssa/rewritePPC64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8b0bea9

Please sign in to comment.