From patchwork Fri Jul 8 10:10:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Prathamesh Kulkarni X-Patchwork-Id: 71667 Delivered-To: patch@linaro.org Received: by 10.140.28.4 with SMTP id 4csp367591qgy; Fri, 8 Jul 2016 03:10:29 -0700 (PDT) X-Received: by 10.98.102.133 with SMTP id s5mr8534384pfj.75.1467972629448; Fri, 08 Jul 2016 03:10:29 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id xv1si3409493pab.257.2016.07.08.03.10.29 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 08 Jul 2016 03:10:29 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-431207-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-431207-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-431207-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=BneZrGj0R0PPYZifv0BGqh77qon14ue61rVw+uNAroMo5Q 7cNWPCyF0RFDJP60z1ausuh8T2E1qHFJ4GXq9BkJA4/+KcndTCqZZDr6FgNLnJSP g5xJGulaY22ida1Wf3YWccdl/Cn1AEv3jwO2EYRSHr5Omurnx92iaIFRoEots= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=QLxeN04uXmG/5+JANanWryHPNOY=; b=j+Ny4xQJ2kG5Xbcj+ny7 oHe/PGQ1AtO1OD9APlw2ldrT7urzYMhnb7JYeXrltopMMKo8e8t0rDDmpYs/rQEe wHnBEbtjyMcp65Bnw34uD9MshAbGGCeIM8WB7w8rnF2vzxU7QDQYdIXdr0B/8wvU B8JIc45eiqODGCSK9q8cMrQ= Received: (qmail 94369 invoked by alias); 8 Jul 2016 10:10:17 -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 94352 invoked by uid 89); 8 Jul 2016 10:10:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.0 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=Visit, visit X-HELO: mail-it0-f53.google.com Received: from mail-it0-f53.google.com (HELO mail-it0-f53.google.com) (209.85.214.53) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-GCM-SHA256 encrypted) ESMTPS; Fri, 08 Jul 2016 10:10:05 +0000 Received: by mail-it0-f53.google.com with SMTP id h190so7905687ith.1 for ; Fri, 08 Jul 2016 03:10:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=R5zRVyCU+VvP9M+ixKvTObzylMnw4Q5cXAToqqKq2zM=; b=StCU7WsqNbbMDDAzLQJTEztvqS4AQNr8mImjQ09m+jtKZez65MrSuVyxzp957Dn73F nyLrO+2AgBJHZ093j6Jj3yy0chZbDauYGNvVLq9Kj1ktMpq01MWy/+wPfv1hpn6/AQ67 wVdSDen6mYv99T+DmIOLTn8AY5nvBmJXS13+WzXkDHfqYf1C9AuL9pREl/k4B0Ie5DM7 N3PAcQc6Y7lQFDt0lIfHV5LgK2VxAM6SHu9DtA6v9ow/C9wX/O9Zjn5LQyz5rfbMvBjX NLo17uOZybrKTHsQNmMfV3c13CSB9JfEKUvCPAQAgYCjbWi5d5OE2igkwSqd8URB1q5D N1Ig== X-Gm-Message-State: ALyK8tJep7fLyB3TE8yak4rTxdcM38FvZvXxQDVmIGGB4Fz7g8PMNggSQfe8fqnuy+6EGjAkBy+8cLoeWXX9ATCh X-Received: by 10.36.122.129 with SMTP id a123mr2205028itc.44.1467972603410; Fri, 08 Jul 2016 03:10:03 -0700 (PDT) MIME-Version: 1.0 Received: by 10.36.48.133 with HTTP; Fri, 8 Jul 2016 03:10:02 -0700 (PDT) From: Prathamesh Kulkarni Date: Fri, 8 Jul 2016 15:40:02 +0530 Message-ID: Subject: fold x ^ y to 0 if x == y To: Richard Biener , gcc Patches X-IsSubscribed: yes Hi Richard, For the following test-case: int f(int x, int y) { int ret; if (x == y) ret = x ^ y; else ret = 1; return ret; } I was wondering if x ^ y should be folded to 0 since it's guarded by condition x == y ? optimized dump shows: f (int x, int y) { int iftmp.0_1; int iftmp.0_4; : if (x_2(D) == y_3(D)) goto ; else goto ; : iftmp.0_4 = x_2(D) ^ y_3(D); : # iftmp.0_1 = PHI return iftmp.0_1; } The attached patch tries to fold for above case. I am checking if op0 and op1 are equal using: if (bitmap_intersect_p (vr1->equiv, vr2->equiv) && operand_equal_p (vr1->min, vr1->max) && operand_equal_p (vr2->min, vr2->max)) { /* equal /* } I suppose intersection would check if op0 and op1 have equivalent ranges, and added operand_equal_p check to ensure that there is only one element within the range. Does that look correct ? Bootstrap+test in progress on x86_64-unknown-linux-gnu. Thanks, Prathamesh diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c index 4333d60..787d068 100644 --- a/gcc/tree-vrp.c +++ b/gcc/tree-vrp.c @@ -6965,6 +6965,59 @@ vrp_valueize_1 (tree name) return name; } +/* Try to fold op0 xor op1 == 0 if op0 == op1. */ +static tree +maybe_fold_xor (gassign *stmt) +{ + if (!stmt) + return NULL_TREE; + + enum tree_code code = gimple_assign_rhs_code (stmt); + if (code != BIT_XOR_EXPR) + return NULL_TREE; + + tree op0 = gimple_assign_rhs1 (stmt); + tree op1 = gimple_assign_rhs2 (stmt); + + if (TREE_CODE (op0) != SSA_NAME + || TREE_CODE (op1) != SSA_NAME) + return NULL_TREE; + + value_range *vr1 = get_value_range (op0); + value_range *vr2 = get_value_range (op1); + + if (vr1 == NULL || vr2 == NULL) + return NULL_TREE; + + if (vr1->type != VR_RANGE || vr2->type != VR_RANGE) + return NULL_TREE; + + if (! (symbolic_range_p (vr1) && symbolic_range_p (vr2))) + return NULL_TREE; + + if (! (TREE_CODE (vr1->min) == SSA_NAME && TREE_CODE (vr1->max) == SSA_NAME + && TREE_CODE (vr2->min) == SSA_NAME && TREE_CODE (vr2->max) == SSA_NAME)) + return NULL_TREE; + + if (! (vr1->equiv && vr2->equiv)) + return NULL_TREE; + + /* check if op0 == op1. */ + if (bitmap_intersect_p (vr1->equiv, vr2->equiv) + && operand_equal_p (vr1->min, vr1->max, 0) + && operand_equal_p (vr2->min, vr2->max, 0) + && code == BIT_XOR_EXPR) + { + gimple_stmt_iterator gsi = gsi_for_stmt (stmt); + gimple_assign_set_rhs_from_tree (&gsi, integer_zero_node); + update_stmt (stmt); + return integer_zero_node; + } + + return NULL_TREE; +} + + /* Visit assignment STMT. If it produces an interesting range, record the SSA name in *OUTPUT_P. */ @@ -6990,8 +7043,11 @@ vrp_visit_assignment_or_call (gimple *stmt, tree *output_p) /* Try folding the statement to a constant first. */ tree tem = gimple_fold_stmt_to_constant_1 (stmt, vrp_valueize, vrp_valueize_1); + if (!tem) + tem = maybe_fold_xor (dyn_cast (stmt)); if (tem && is_gimple_min_invariant (tem)) set_value_range_to_value (&new_vr, tem, NULL); + /* Then dispatch to value-range extracting functions. */ else if (code == GIMPLE_CALL) extract_range_basic (&new_vr, stmt);