Commit 9b25da73 authored by Alexandru Moșoi's avatar Alexandru Moșoi Committed by Keith Randall

[dev.ssa] cmd/compile/internal/ssa/gen: add more simplifications rules

This is a follow up on https://go-review.googlesource.com/#/c/12420/
with some rules moved to AMD64 closer to the existing rules.

Change-Id: Id346bb0fc4459b3c49b826a59cc74308a590310e
Reviewed-on: https://go-review.googlesource.com/12906Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent 93c354b6
......@@ -67,6 +67,30 @@ func testBitwiseLogic() {
println("testBitwiseXor failed, wanted", want, "got", got)
failed = true
}
if want, got := int32(832), testBitwiseLsh_ssa(13, 4, 2); want != got {
println("testBitwiseXor failed, wanted", want, "got", got)
failed = true
}
if want, got := int32(0), testBitwiseLsh_ssa(13, 25, 15); want != got {
println("testBitwiseLsh failed, wanted", want, "got", got)
failed = true
}
if want, got := int32(0), testBitwiseLsh_ssa(-13, 25, 15); want != got {
println("testBitwiseLsh failed, wanted", want, "got", got)
failed = true
}
if want, got := int32(0), testBitwiseRsh_ssa(-13, 25, 15); want != got {
println("testBitwiseLsh failed, wanted", want, "got", got)
failed = true
}
if want, got := int32(0), testBitwiseRsh_ssa(13, 25, 15); want != got {
println("testBitwiseLsh failed, wanted", want, "got", got)
failed = true
}
if want, got := int32(-1), testBitwiseRsh_ssa(-13, 25, 15); want != got {
println("testBitwiseLsh failed, wanted", want, "got", got)
failed = true
}
}
func testBitwiseAnd_ssa(a, b uint32) uint32 {
......@@ -87,6 +111,18 @@ func testBitwiseXor_ssa(a, b uint32) uint32 {
return a ^ b
}
func testBitwiseLsh_ssa(a int32, b, c uint32) int32 {
switch { // prevent inlining
}
return a << b << c
}
func testBitwiseRsh_ssa(a int32, b, c uint32) int32 {
switch { // prevent inlining
}
return a >> b >> c
}
// testSubqToNegq ensures that the SUBQ -> NEGQ translation works correctly.
func testSubqToNegq(a, b, c, d, e, f, g, h, i, j, k int64) {
want := a + 8207351403619448057 - b - 1779494519303207690 + c*8810076340510052032*d - 4465874067674546219 - e*4361839741470334295 - f + 8688847565426072650*g*8065564729145417479
......
......@@ -445,10 +445,22 @@
(ADDLconst [c] (MOVLconst [d])) -> (MOVLconst [c+d])
(ADDWconst [c] (MOVWconst [d])) -> (MOVWconst [c+d])
(ADDBconst [c] (MOVBconst [d])) -> (MOVBconst [c+d])
(ADDQconst [c] (ADDQconst [d] x)) -> (ADDQconst [c+d] x)
(ADDLconst [c] (ADDLconst [d] x)) -> (ADDLconst [c+d] x)
(ADDWconst [c] (ADDWconst [d] x)) -> (ADDWconst [c+d] x)
(ADDBconst [c] (ADDBconst [d] x)) -> (ADDBconst [c+d] x)
(SUBQconst [c] (MOVQconst [d])) -> (MOVQconst [c-d])
(SUBLconst [c] (MOVLconst [d])) -> (MOVLconst [c-d])
(SUBWconst [c] (MOVWconst [d])) -> (MOVWconst [c-d])
(SUBBconst [c] (MOVBconst [d])) -> (MOVBconst [c-d])
(SUBQconst [c] (SUBQconst [d] x)) -> (ADDQconst [c-d] x)
(SUBLconst [c] (SUBLconst [d] x)) -> (ADDLconst [c-d] x)
(SUBWconst [c] (SUBWconst [d] x)) -> (ADDWconst [c-d] x)
(SUBBconst [c] (SUBBconst [d] x)) -> (ADDBconst [c-d] x)
(NEGQ (MOVQconst [c])) -> (MOVQconst [-c])
(NEGL (MOVLconst [c])) -> (MOVLconst [-c])
(NEGW (MOVWconst [c])) -> (MOVWconst [-c])
(NEGB (MOVBconst [c])) -> (MOVBconst [-c])
(MULQconst [c] (MOVQconst [d])) -> (MOVQconst [c*d])
(MULLconst [c] (MOVLconst [d])) -> (MOVLconst [c*d])
(MULWconst [c] (MOVWconst [d])) -> (MOVWconst [c*d])
......@@ -468,3 +480,26 @@
(NOTL (MOVLconst [c])) -> (MOVLconst [^c])
(NOTW (MOVWconst [c])) -> (MOVWconst [^c])
(NOTB (MOVBconst [c])) -> (MOVBconst [^c])
// generic simplifications
// TODO: more of this
(ADDQ x (NEGQ y)) -> (SUBQ x y)
(ADDL x (NEGL y)) -> (SUBL x y)
(ADDW x (NEGW y)) -> (SUBW x y)
(ADDB x (NEGB y)) -> (SUBB x y)
(SUBQ x x) -> (MOVQconst [0])
(SUBL x x) -> (MOVLconst [0])
(SUBW x x) -> (MOVWconst [0])
(SUBB x x) -> (MOVBconst [0])
(ANDQ x x) -> (Copy x)
(ANDL x x) -> (Copy x)
(ANDW x x) -> (Copy x)
(ANDB x x) -> (Copy x)
(ORQ x x) -> (Copy x)
(ORL x x) -> (Copy x)
(ORW x x) -> (Copy x)
(ORB x x) -> (Copy x)
(XORQ x x) -> (MOVQconst [0])
(XORL x x) -> (MOVLconst [0])
(XORW x x) -> (MOVWconst [0])
(XORB x x) -> (MOVBconst [0])
......@@ -25,6 +25,14 @@
(Mul64 (Const64 [c]) (Const64 [d])) -> (Const64 [c*d])
(MulPtr (ConstPtr [c]) (ConstPtr [d])) -> (ConstPtr [c*d])
(IsInBounds (ConstPtr [c]) (ConstPtr [d])) -> (ConstPtr {inBounds(c,d)})
(Eq64 x x) -> (ConstBool {true})
(Eq32 x x) -> (ConstBool {true})
(Eq16 x x) -> (ConstBool {true})
(Eq8 x x) -> (ConstBool {true})
(Neq64 x x) -> (ConstBool {false})
(Neq32 x x) -> (ConstBool {false})
(Neq16 x x) -> (ConstBool {false})
(Neq8 x x) -> (ConstBool {false})
// tear apart slices
// TODO: anything that generates a slice needs to go in here.
......
......@@ -285,12 +285,12 @@ func genMatch(w io.Writer, arch arch, match, fail string) {
func genMatch0(w io.Writer, arch arch, match, v, fail string, m map[string]string, top bool) {
if match[0] != '(' {
if x, ok := m[match]; ok {
if _, ok := m[match]; ok {
// variable already has a definition. Check whether
// the old definition and the new definition match.
// For example, (add x x). Equality is just pointer equality
// on Values (so cse is important to do before lowering).
fmt.Fprintf(w, "if %s != %s %s", v, x, fail)
fmt.Fprintf(w, "if %s != %s %s", v, match, fail)
return
}
// remember that this variable references the given value
......
......@@ -102,6 +102,82 @@ func rewriteValuegeneric(v *Value, config *Config) bool {
goto end68cc91679848c7c30bd8b0a8ed533843
end68cc91679848c7c30bd8b0a8ed533843:
;
case OpEq16:
// match: (Eq16 x x)
// cond:
// result: (ConstBool {true})
{
x := v.Args[0]
if v.Args[1] != x {
goto enda503589f9b617e708a5ad3ddb047809f
}
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = true
return true
}
goto enda503589f9b617e708a5ad3ddb047809f
enda503589f9b617e708a5ad3ddb047809f:
;
case OpEq32:
// match: (Eq32 x x)
// cond:
// result: (ConstBool {true})
{
x := v.Args[0]
if v.Args[1] != x {
goto endc94ae3b97d0090257b02152e437b3e17
}
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = true
return true
}
goto endc94ae3b97d0090257b02152e437b3e17
endc94ae3b97d0090257b02152e437b3e17:
;
case OpEq64:
// match: (Eq64 x x)
// cond:
// result: (ConstBool {true})
{
x := v.Args[0]
if v.Args[1] != x {
goto end4d21cead60174989467a9c8202dbb91d
}
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = true
return true
}
goto end4d21cead60174989467a9c8202dbb91d
end4d21cead60174989467a9c8202dbb91d:
;
case OpEq8:
// match: (Eq8 x x)
// cond:
// result: (ConstBool {true})
{
x := v.Args[0]
if v.Args[1] != x {
goto end73dce8bba164e4f4a1dd701bf8cfb362
}
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = true
return true
}
goto end73dce8bba164e4f4a1dd701bf8cfb362
end73dce8bba164e4f4a1dd701bf8cfb362:
;
case OpEqFat:
// match: (EqFat x y)
// cond: x.Op == OpConstNil && y.Op != OpConstNil
......@@ -256,6 +332,82 @@ func rewriteValuegeneric(v *Value, config *Config) bool {
goto end808c190f346658bb1ad032bf37a1059f
end808c190f346658bb1ad032bf37a1059f:
;
case OpNeq16:
// match: (Neq16 x x)
// cond:
// result: (ConstBool {false})
{
x := v.Args[0]
if v.Args[1] != x {
goto end192755dd3c2be992e9d3deb53794a8d2
}
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = false
return true
}
goto end192755dd3c2be992e9d3deb53794a8d2
end192755dd3c2be992e9d3deb53794a8d2:
;
case OpNeq32:
// match: (Neq32 x x)
// cond:
// result: (ConstBool {false})
{
x := v.Args[0]
if v.Args[1] != x {
goto endeb23619fc85950a8df7b31126252c4dd
}
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = false
return true
}
goto endeb23619fc85950a8df7b31126252c4dd
endeb23619fc85950a8df7b31126252c4dd:
;
case OpNeq64:
// match: (Neq64 x x)
// cond:
// result: (ConstBool {false})
{
x := v.Args[0]
if v.Args[1] != x {
goto endfc6eea780fb4056afb9e4287076da60c
}
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = false
return true
}
goto endfc6eea780fb4056afb9e4287076da60c
endfc6eea780fb4056afb9e4287076da60c:
;
case OpNeq8:
// match: (Neq8 x x)
// cond:
// result: (ConstBool {false})
{
x := v.Args[0]
if v.Args[1] != x {
goto endcccf700d93c6d57765b80f92f7b3fa81
}
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = false
return true
}
goto endcccf700d93c6d57765b80f92f7b3fa81
endcccf700d93c6d57765b80f92f7b3fa81:
;
case OpNeqFat:
// match: (NeqFat x y)
// cond: x.Op == OpConstNil && y.Op != OpConstNil
......@@ -422,7 +574,7 @@ func rewriteValuegeneric(v *Value, config *Config) bool {
t := v.Args[1].Type
src := v.Args[1].Args[0]
mem := v.Args[1].Args[1]
if v.Args[2] != v.Args[1].Args[1] {
if v.Args[2] != mem {
goto end324ffb6d2771808da4267f62c854e9c8
}
if !(t.Size() > 8) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment