From patchwork Mon Nov 6 21:35:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marc Glisse X-Patchwork-Id: 834951 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-466072-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="PstIgxR1"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3yW5T92QDnz9s7B for ; Tue, 7 Nov 2017 08:35:39 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=P10jpPYI1gLyDjVCnzo0sWX+6FLmINXsXcCCbiLuKAg5QFqtVdSyE F8t0lrYCWBbmwFYsG3YJmJUJWNIcPXZ4sHEV5kgMvqMJ9jZH+H0POPrivdl7OVQA 30AvLYFj0uTTK+LPrEunO+M2ouHv44qR5AXwit91doobrl8ygsq2fM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=PM3qfNdZj1aBbclEuixddcS1N3o=; b=PstIgxR11hpQYegDW7ht gXGYSV5721nEt9qbPZQEPauruuASTQeZZ2KLm6Bp1sQyvIfFGXkDNmAI1m4kk5aO BaQUycpuhhP1Y3IZCGxQMNg+mzF2XqzpdLlKaRWfuIoAr2LgZvgph4K0jGWpHcH3 bT24yG+++t9EhfzmpypIYOM= Received: (qmail 117304 invoked by alias); 6 Nov 2017 21:35:31 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 117290 invoked by uid 89); 6 Nov 2017 21:35:30 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-9.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=organize, H*c:HHHHH, VOL, vol X-HELO: mail2-relais-roc.national.inria.fr Received: from mail2-relais-roc.national.inria.fr (HELO mail2-relais-roc.national.inria.fr) (192.134.164.83) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 06 Nov 2017 21:35:28 +0000 Received: from ip-195.net-89-2-234.rev.numericable.fr (HELO stedding) ([89.2.234.195]) by mail2-relais-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 06 Nov 2017 22:35:25 +0100 Date: Mon, 6 Nov 2017 22:35:19 +0100 (CET) From: Marc Glisse To: gcc-patches@gcc.gnu.org Subject: More bitop simplifications in match.pd Message-ID: User-Agent: Alpine 2.02 (DEB 1266 2009-07-14) MIME-Version: 1.0 Hello, those have been on my TODO-list for a long time (found in LLVM IIRC). We were not doing any of those transformations, even in GENERIC, so nothing to remove from fold-const.c. The idea is that any expression involving only 2 variables and operators &|^~ should simplify to at most 2 insn (modulo some single_use issues), not sure if we are there yet. The second transformation became almost redundant when I added the last canonicalization, but since the (rather arbitrary) single_use checks are not the same, I left it. The number of transformations on bit operations in match.pd is large (and I still have some on that TODO-list), if someone can think of a nice way to organize them, please go ahead. I was tempted to use C++ templates for the testcase... Bootstrap+regtest on powerpc64le-unknown-linux-gnu. 2017-11-07 Marc Glisse gcc/ * match.pd ((a&~b)|(a^b),(a&~b)^~a,(a|b)&~(a^b),a|~(a^b), (a|b)|(a&^b),(a&b)|~(a^b),~(~a&b),~X^Y): New transformations. gcc/testsuite/ * gcc.dg/tree-ssa/bitops-1.c: New file. Index: gcc/match.pd =================================================================== --- gcc/match.pd (revision 254446) +++ gcc/match.pd (working copy) @@ -678,20 +678,56 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (simplify (op:c (bit_and @0 INTEGER_CST@2) (bit_and (bit_not @0) INTEGER_CST@1)) (if (~wi::to_wide (@2) == wi::to_wide (@1)) (bit_xor @0 @1)))) /* PR53979: Transform ((a ^ b) | a) -> (a | b) */ (simplify (bit_ior:c (bit_xor:c @0 @1) @0) (bit_ior @0 @1)) +/* (a & ~b) | (a ^ b) --> a ^ b */ +(simplify + (bit_ior:c (bit_and:c @0 (bit_not @1)) (bit_xor:c@2 @0 @1)) + @2) + +/* (a & ~b) ^ ~a --> ~(a & b) */ +(simplify + (bit_xor:c (bit_and:cs @0 (bit_not @1)) (bit_not @0)) + (bit_not (bit_and @0 @1))) + +/* (a | b) & ~(a ^ b) --> a & b */ +(simplify + (bit_and:c (bit_ior @0 @1) (bit_not (bit_xor:c @0 @1))) + (bit_and @0 @1)) + +/* a | ~(a ^ b) --> a | ~b */ +(simplify + (bit_ior:c @0 (bit_not:s (bit_xor:c @0 @1))) + (bit_ior @0 (bit_not @1))) + +/* (a | b) | (a &^ b) --> a | b */ +(for op (bit_and bit_xor) + (simplify + (bit_ior:c (bit_ior@2 @0 @1) (op:c @0 @1)) + @2)) + +/* (a & b) | ~(a ^ b) --> ~(a ^ b) */ +(simplify + (bit_ior:c (bit_and:c @0 @1) (bit_not@2 (bit_xor @0 @1))) + @2) + +/* ~(~a & b) --> a | ~b */ +(simplify + (bit_not (bit_and:cs (bit_not @0) @1)) + (bit_ior @0 (bit_not @1))) + /* Simplify (~X & Y) to X ^ Y if we know that (X & ~Y) is 0. */ #if GIMPLE (simplify (bit_and (bit_not SSA_NAME@0) INTEGER_CST@1) (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) && wi::bit_and_not (get_nonzero_bits (@0), wi::to_wide (@1)) == 0) (bit_xor @0 @1))) #endif /* X % Y is smaller than Y. */ @@ -1097,20 +1133,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Part of convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */ (simplify (bit_not (convert? (bit_xor @0 INTEGER_CST@1))) (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) (convert (bit_xor @0 (bit_not @1))))) (simplify (bit_not (convert? (bit_xor:c (bit_not @0) @1))) (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) (convert (bit_xor @0 @1)))) +/* Otherwise prefer ~(X ^ Y) to ~X ^ Y as more canonical. */ +(simplify + (bit_xor:c (nop_convert:s (bit_not:s @0)) @1) + (if (tree_nop_conversion_p (type, TREE_TYPE (@0))) + (bit_not (bit_xor (view_convert @0) @1)))) + /* (x & ~m) | (y & m) -> ((x ^ y) & m) ^ x */ (simplify (bit_ior:c (bit_and:cs @0 (bit_not @2)) (bit_and:cs @1 @2)) (bit_xor (bit_and (bit_xor @0 @1) @2) @0)) /* Fold A - (A & B) into ~B & A. */ (simplify (minus (convert1? @0) (convert2?:s (bit_and:cs @@0 @1))) (if (tree_nop_conversion_p (type, TREE_TYPE (@0)) && tree_nop_conversion_p (type, TREE_TYPE (@1))) Index: gcc/testsuite/gcc.dg/tree-ssa/bitops-1.c =================================================================== --- gcc/testsuite/gcc.dg/tree-ssa/bitops-1.c (nonexistent) +++ gcc/testsuite/gcc.dg/tree-ssa/bitops-1.c (working copy) @@ -0,0 +1,72 @@ +/* { dg-do run } */ +/* { dg-options "-O -fdump-tree-optimized-raw" } */ + +#define DECLS(n,VOL) \ +__attribute__((noinline,noclone)) \ +int f##n(int A,int B){ \ + VOL int C = A & ~B; \ + VOL int D = A ^ B; \ + return C | D; \ +} \ +__attribute__((noinline,noclone)) \ +int g##n(int A,int B){ \ + VOL int C = A & ~B; \ + return C ^ ~A; \ +} \ +__attribute__((noinline,noclone)) \ +int h##n(int A,int B){ \ + VOL int C = A | B; \ + VOL int D = A ^ B; \ + return C & ~D; \ +} \ +__attribute__((noinline,noclone)) \ +int i##n(int A,int B){ \ + VOL int C = A ^ B; \ + return A | ~C; \ +} \ +__attribute__((noinline,noclone)) \ +int J##n(int A,int B){ \ + VOL int C = A | B; \ + VOL int D = A & B; \ + return C | D; \ +} \ +__attribute__((noinline,noclone)) \ +int k##n(int A,int B){ \ + VOL int C = A & B; \ + VOL int D = A ^ B; \ + return C | ~D; \ +} \ +__attribute__((noinline,noclone)) \ +int l##n(int A,int B){ \ + VOL int C = A & ~B; \ + return ~C; \ +} \ +__attribute__((noinline,noclone)) \ +int m##n(int A,int B){ \ + VOL int C = A & B; \ + VOL int D = A ^ B; \ + return C ^ D; \ +} + +DECLS(0,) +DECLS(1,volatile) + +int main(){ + for(int A = 0; A <= 1; ++A) + for(int B = 0; B <= 1; ++B) + { + if (f0 (A, B) != f1 (A, B)) __builtin_abort(); + if (g0 (A, B) != g1 (A, B)) __builtin_abort(); + if (h0 (A, B) != h1 (A, B)) __builtin_abort(); + if (i0 (A, B) != i1 (A, B)) __builtin_abort(); + if (J0 (A, B) != J1 (A, B)) __builtin_abort(); + if (k0 (A, B) != k1 (A, B)) __builtin_abort(); + if (l0 (A, B) != l1 (A, B)) __builtin_abort(); + if (m0 (A, B) != m1 (A, B)) __builtin_abort(); + } +} + +/* { dg-final { scan-tree-dump-times "bit_not_expr" 12 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_and_expr" 9 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_ior_expr" 10 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_xor_expr" 9 "optimized"} } */