From 52d76f7a6a02cf5834251a4ceadc686a9f83ac81 Mon Sep 17 00:00:00 2001 From: Todd Neal Date: Thu, 23 Jul 2015 18:44:09 -0500 Subject: [PATCH] [dev.ssa] cmd/compile: rewrite if not Rewrite if !cond by swapping the branches and removing the not. Change-Id: If3af1bac02bfc566faba872a8c7f7e5ce38e9f58 Reviewed-on: https://go-review.googlesource.com/12610 Reviewed-by: Josh Bleecher Snyder --- .../compile/internal/ssa/gen/generic.rules | 1 + .../compile/internal/ssa/rewritegeneric.go | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/cmd/compile/internal/ssa/gen/generic.rules b/src/cmd/compile/internal/ssa/gen/generic.rules index 492676d9b7..fc5ffb9610 100644 --- a/src/cmd/compile/internal/ssa/gen/generic.rules +++ b/src/cmd/compile/internal/ssa/gen/generic.rules @@ -51,5 +51,6 @@ (StringLen (StringMake _ len)) -> len (Store dst str mem) && str.Type.IsString() -> (Store (OffPtr [config.PtrSize] dst) (StringLen str) (Store dst (StringPtr str) mem)) +(If (Not cond) yes no) -> (If cond no yes) (If (Const {c}) yes no) && c.(bool) -> (Plain nil yes) (If (Const {c}) yes no) && !c.(bool) -> (Plain nil no) diff --git a/src/cmd/compile/internal/ssa/rewritegeneric.go b/src/cmd/compile/internal/ssa/rewritegeneric.go index 66b6c1a7a5..54358129e0 100644 --- a/src/cmd/compile/internal/ssa/rewritegeneric.go +++ b/src/cmd/compile/internal/ssa/rewritegeneric.go @@ -495,6 +495,26 @@ func rewriteValuegeneric(v *Value, config *Config) bool { func rewriteBlockgeneric(b *Block) bool { switch b.Kind { case BlockIf: + // match: (If (Not cond) yes no) + // cond: + // result: (If cond no yes) + { + v := b.Control + if v.Op != OpNot { + goto endebe19c1c3c3bec068cdb2dd29ef57f96 + } + cond := v.Args[0] + yes := b.Succs[0] + no := b.Succs[1] + b.Kind = BlockIf + b.Control = cond + b.Succs[0] = no + b.Succs[1] = yes + return true + } + goto endebe19c1c3c3bec068cdb2dd29ef57f96 + endebe19c1c3c3bec068cdb2dd29ef57f96: + ; // match: (If (Const {c}) yes no) // cond: c.(bool) // result: (Plain nil yes)