From e7c187369149741f96c445c153be4fc6475bc365 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 23 Apr 2020 00:21:59 -0700 Subject: [PATCH] cmd/compile: optimize x & 1 != 0 to x & 1 on amd64 Triggers a handful of times in std+cmd. Change-Id: I9bb8ce9a5f8bae2547cb61157cd8f256e1b63e76 Reviewed-on: https://go-review.googlesource.com/c/go/+/229602 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- src/cmd/compile/internal/ssa/gen/AMD64.rules | 4 ++ src/cmd/compile/internal/ssa/rewriteAMD64.go | 48 ++++++++++++++++++++ test/codegen/bool.go | 33 ++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 test/codegen/bool.go diff --git a/src/cmd/compile/internal/ssa/gen/AMD64.rules b/src/cmd/compile/internal/ssa/gen/AMD64.rules index bda8429c5f..0b02301c7d 100644 --- a/src/cmd/compile/internal/ssa/gen/AMD64.rules +++ b/src/cmd/compile/internal/ssa/gen/AMD64.rules @@ -571,6 +571,10 @@ (SETB (TEST(Q|L|W|B) x x)) -> (ConstBool [0]) (SETAE (TEST(Q|L|W|B) x x)) -> (ConstBool [1]) +// x & 1 != 0 -> x & 1 +(SETNE (TEST(B|W)const [1] x)) => (AND(L|L)const [1] x) +(SETB (BT(L|Q)const [0] x)) => (AND(L|Q)const [1] x) + // Recognize bit tests: a&(1<