From patchwork Wed Oct 25 03:37:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 157810 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2346811vqx; Tue, 24 Oct 2023 20:37:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGHTqzrcn3ugeRbD8K8leIGlY8VJOuAONfrsDmPG16NMc/vcyjsvbV+uZRb4H3Ge6BWZo0U X-Received: by 2002:ac8:7f10:0:b0:41c:dd22:e7c0 with SMTP id f16-20020ac87f10000000b0041cdd22e7c0mr15999096qtk.29.1698205044786; Tue, 24 Oct 2023 20:37:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698205044; cv=pass; d=google.com; s=arc-20160816; b=BjfH91jgD0whnDH/80cN+6P6bKqGEXql8rXDXY4zus1TeNnqCvWjzhPDHfdX90S5zn 4OQcWfD0PR08BNtMirEiwPefiE/D4Jq6yUtY8AWpDVgXpJUc1SSUe79CW2J3f9OUk3NK uGW9vCa8OMNVOx8CCn68K+MIfWQZsbjbT8FGCEx5QeBvNwoFmQoymh6+o6vUKFXCb4s+ +HjU/AN9V/S0pkj/i36aZwaYr5mw054ITkFOFmkVOe1yERM0ma8hqoYSHirZuEeYdJSD 9lMTELQm0C3hx3OF8py3dPlwQyYmbuFkkjRvIfEOkd4wOybV8Ezfdj1iZR2Be2H3u/xZ tppw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=VM/lefAatMkwMok6wemmB9nrbC7gjzg8JJ5OS1jER9k=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=PeL54mHGEIShttm6dBSjpwcsFrnr5oEkAjerul/DLydpnaDtNDvNvrF6WBNTLe6WUd wWKFywu3Et4ElYn4ioifFd358/PREbhYV+KyUnPHy3eAmNP1lOFh9oGmdXGvTOv5+YWL Id55jFjQWChfwU2VRfGJsVDiYHWGlCpUghCdMbvehs76TNJNx1jnyzQpo+xgHO6Lfe6h JymfPN8ySNUhoki1mYdID/MXqVqnGgbyhvkWLC7lFnQaY8sooLtRT31KtrocwZtPGGVt ux1/P19YuaNBslMKRNYCkeuoVfE7/DAsavR78U0BDIuOkJ9fPxpztMUEdZV1hxGtV41m 1P8A== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=FeAALknk; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id x11-20020a05622a000b00b004009a10dcd0si7738757qtw.160.2023.10.24.20.37.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 20:37:24 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=FeAALknk; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8EBF33858020 for ; Wed, 25 Oct 2023 03:37:24 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 70E1C3858CDA for ; Wed, 25 Oct 2023 03:37:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 70E1C3858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 70E1C3858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::435 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698205021; cv=none; b=CRcfRtHgln1CEZ8KDRgcCF4QB01SJZUPpOAJBBrm3dMKEy9oYoIbMcJPu1g7hyJNAlc0SHxDZ7L6yOeeoaLUxuYe4K4kb7ozgTenHtby9ONvJV9+vuO60nH5SW41OWSrwDLQGZrMn8wKVcJppS2U43PR1rk9HjypxuLwV9zMmqA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698205021; c=relaxed/simple; bh=MpkzFsMldxiNv5QLoqU5heVcsZXd/mFKdIl7lJ4yV3w=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=A+JnAFOwdnEHcCwPOMvhVIP1c0+IT52gHGF4b2pAzgTgUXSCxTx5DBbpe0vRQXrHj9b2U8b5RjVcTaB2DDbsO/rqd2CZ1chAgYk5pH9UnmGR7evCeqCy5G+k9uI/x7kV/LvbgrtWzSRg9utGPbJgJkgW8/XcggWVFzFaj7HbXIE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6b5cac99cfdso4385872b3a.2 for ; Tue, 24 Oct 2023 20:37:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698205019; x=1698809819; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=VM/lefAatMkwMok6wemmB9nrbC7gjzg8JJ5OS1jER9k=; b=FeAALknkRIKNX0KhYxYwtYMCnckERJF9lz5KvIYgFHlhHFRS9HEhvmtQPoBw+BujQE YjhRj66WSmVRSvrMnbGf9xbR+ZwMCAPZg6zZYRjxLaAtRMhH3s7P3Y/GpQLUMTT6MQKj +thXIQD50MYLa1pEzKrXSc+9DdMTcNPxu6fUWLpq7hrp3HKrlsyUaUPY5Sx6skgFlBQ7 LV8bL1MS8hdRRRXO0RV62n0EfFCm5ZLe92GcVsAulwx0JsbPrUutV5eqdUJDEdF9oGwR e6Wcq1mL48jmzlPUPweHDDoaL3yhEm8yO67D5kEGQtKEfccQlmei2YG7v7cSxXMXVNK8 rBCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698205019; x=1698809819; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=VM/lefAatMkwMok6wemmB9nrbC7gjzg8JJ5OS1jER9k=; b=crgbYxLJAo3uEYcuF2rUkH3ZJYc+u0iZlJuPmMseSdFL0T3fH95TZax7tWwqp6UUju 0Sjz6RW/zygovp8spbUCTGvUhpl6/mFi90FOo7sOWHMmmkb+HIV3D8s8EyMVy25Oyus3 ne/ttgyFuaWoAxwNjtxevLSeRAHQlKlY2sEuz2BEZZbhL0czvn4yI26egm/jLWrGMqnk /chQXllFupNRbC48MFnG8urrztdf+wtI4c+9kXZkHciiT7Iv/k+3CkjJUgW3ofZZvi+k gSQAKnxCs2I0hAWkcHc9XRzNp1bvqU8Zvf837S+KkyfbnXXkIqNA6fm8lC1aWIefOdnf sCrw== X-Gm-Message-State: AOJu0Yy8l7T2ngoIBi9Ps5U1bmEdHyansUaqxuSUO3ujcNUYBpISKNVo xIpsYgth5ijI8UzqIyvUQnTYR6PBVJFw2Q== X-Received: by 2002:a05:6a20:918b:b0:13f:c40c:379 with SMTP id v11-20020a056a20918b00b0013fc40c0379mr4943990pzd.13.1698205019040; Tue, 24 Oct 2023 20:36:59 -0700 (PDT) Received: from xeond.wrightpinski.org ([98.97.114.173]) by smtp.gmail.com with ESMTPSA id f186-20020a6251c3000000b00692754580f0sm8740666pfb.187.2023.10.24.20.36.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 20:36:58 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH] match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957] Date: Wed, 25 Oct 2023 03:37:49 +0000 Message-Id: <20231025033749.3363044-1-pinskia@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, PLING_QUERY, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780697053097612288 X-GMAIL-MSGID: 1780697053097612288 This adds a match pattern for `a != C1 ? abs(a) : C2` which gets simplified to `abs(a)`. if C1 was originally *_MIN then change it over to use absu instead of abs. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/111957 gcc/ChangeLog: * match.pd (`a != C1 ? abs(a) : C2`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-40.c: New test. --- gcc/match.pd | 10 +++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c | 25 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c diff --git a/gcc/match.pd b/gcc/match.pd index 5df04ebba77..370ee35de52 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5622,6 +5622,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2))) @3)) +/* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */ +(for op (abs absu) + (simplify + (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2) + (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2)) + (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1))) + (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); } + (convert (absu:utype @0))) + @3)))) + /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */ (simplify diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c new file mode 100644 index 00000000000..a9011ce97fb --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c @@ -0,0 +1,25 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-phiopt" } */ +/* PR tree-optimization/111957 */ + +int f(int a) +{ + if (a) + return a > 0 ? a : -a; + return 0; +} + +int f1(int x) +{ + int intmin = (-1u >> 1); + intmin = -intmin - 1; + if (x != intmin) + return x > 0 ? x : -x; + return intmin; +} + +/* { dg-final { scan-tree-dump-times "if " 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */ +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 2 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 1 "phiopt2" } } */ +/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 1 "phiopt2" } } */