From fd59c6cf8cb600f2911864948303016581abf016 Mon Sep 17 00:00:00 2001 From: Jakub Ciolek Date: Wed, 9 Nov 2022 16:07:11 +0100 Subject: [PATCH] cmd/compile: elide unnecessary Not in Phi block controls For a BlockIf, we can change the order of the successors if all OpPhi args are an OpNot and this allows us to elide said OpNots. When compiling Go itself, there were no hits for (If (Phi (Not x) (Not y) (Not z)) or any other longer patterns. compilecmp: errors errors.As changed reflect reflect.Value.FieldByIndex changed reflect.Value.Method changed reflect.cvtI2I changed reflect.Value.FieldByIndexErr changed reflect.deepValueEqual.func1 496 -> 502 (+1.21%) reflect.deepValueEqual changed internal/fmtsort internal/fmtsort.nilCompare 652 -> 648 (-0.61%) database/sql database/sql.convertAssignRows changed encoding/json encoding/json.interfaceEncoder changed encoding/json.(*decodeState).unmarshal 574 -> 571 (-0.52%) encoding/json.mapEncoder.encode changed encoding/json.ptrEncoder.encode changed encoding/json.encodeByteSlice changed encoding/json.addrMarshalerEncoder changed encoding/json.addrTextMarshalerEncoder changed encoding/json.(*decodeState).object changed encoding/json.sliceEncoder.encode changed encoding/json.indirect 1303 -> 1286 (-1.30%) encoding/gob encoding/gob.encOpFor.func3 changed encoding/gob.encIndirect changed encoding/gob.encOpFor.func5 changed encoding/gob.(*Encoder).encodeInterface changed encoding/gob.(*Decoder).decodeMap changed encoding/xml encoding/xml.(*printer).marshalStruct changed encoding/xml.(*fieldInfo).value changed encoding/xml.(*printer).marshalAttr changed encoding/xml.indirect changed encoding/xml.(*printer).marshalValue changed encoding/asn1 encoding/asn1.UnmarshalWithParams 837 -> 845 (+0.96%) text/template text/template.indirectInterface changed text/template.indirect changed text/template.safeCall changed net/http/httptrace net/http/httptrace.(*ClientTrace).compose changed cmd/fix main.typefix.func2 changed cmd/compile/internal/ir cmd/compile/internal/ir.dumpNode changed cmd/gofmt main.match changed main.subst changed cmd/compile/internal/ssa cmd/compile/internal/ssa.rewriteBlockgeneric 626 -> 1030 (+64.54%) Change-Id: I645b3b3e37302a63e06b79ce74674882fb603ef3 Reviewed-on: https://go-review.googlesource.com/c/go/+/449055 TryBot-Result: Gopher Robot Reviewed-by: Joedian Reid Run-TryBot: Jakub Ciolek Reviewed-by: Keith Randall Reviewed-by: Keith Randall --- .../compile/internal/ssa/_gen/generic.rules | 1 + .../compile/internal/ssa/rewritegeneric.go | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/cmd/compile/internal/ssa/_gen/generic.rules b/src/cmd/compile/internal/ssa/_gen/generic.rules index d5f9e5be636b5..8d985526d13eb 100644 --- a/src/cmd/compile/internal/ssa/_gen/generic.rules +++ b/src/cmd/compile/internal/ssa/_gen/generic.rules @@ -961,6 +961,7 @@ (NilCheck (GetG mem) mem) => mem (If (Not cond) yes no) => (If cond no yes) +(If (Phi nx:(Not x) ny:(Not y)) yes no) && nx.Uses == 1 && ny.Uses == 1 => (If (Phi x y) no yes) (If (ConstBool [c]) yes no) && c => (First yes no) (If (ConstBool [c]) yes no) && !c => (First no yes) diff --git a/src/cmd/compile/internal/ssa/rewritegeneric.go b/src/cmd/compile/internal/ssa/rewritegeneric.go index a76f55813f8c8..ad8d33c97d0a3 100644 --- a/src/cmd/compile/internal/ssa/rewritegeneric.go +++ b/src/cmd/compile/internal/ssa/rewritegeneric.go @@ -32462,6 +32462,35 @@ func rewriteBlockgeneric(b *Block) bool { b.swapSuccessors() return true } + // match: (If (Phi nx:(Not x) ny:(Not y)) yes no) + // cond: nx.Uses == 1 && ny.Uses == 1 + // result: (If (Phi x y) no yes) + for b.Controls[0].Op == OpPhi { + v_0 := b.Controls[0] + if len(v_0.Args) != 2 { + break + } + t := v_0.Type + _ = v_0.Args[1] + nx := v_0.Args[0] + if nx.Op != OpNot { + break + } + x := nx.Args[0] + ny := v_0.Args[1] + if ny.Op != OpNot { + break + } + y := ny.Args[0] + if !(nx.Uses == 1 && ny.Uses == 1) { + break + } + v0 := b.NewValue0(v_0.Pos, OpPhi, t) + v0.AddArg2(x, y) + b.resetWithControl(BlockIf, v0) + b.swapSuccessors() + return true + } // match: (If (ConstBool [c]) yes no) // cond: c // result: (First yes no)