From patchwork Thu May 11 22:26:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luc Van Oostenryck X-Patchwork-Id: 9723211 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AEB376031B for ; Thu, 11 May 2017 22:26:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A99832870A for ; Thu, 11 May 2017 22:26:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9E3CA2873C; Thu, 11 May 2017 22:26:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3F5692870A for ; Thu, 11 May 2017 22:26:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756030AbdEKW0c (ORCPT ); Thu, 11 May 2017 18:26:32 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:35448 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754689AbdEKW0c (ORCPT ); Thu, 11 May 2017 18:26:32 -0400 Received: by mail-wr0-f196.google.com with SMTP id g12so5395234wrg.2 for ; Thu, 11 May 2017 15:26:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bn9l6XmStz9eaK9bQcJo65771be8umpO2nVxq34UUT4=; b=Zoms/VQNP5OBYMkabEjeHVT75/YTwLxIMgdPCH/8fT+Nog/V4AIUdUTiZweG/G9pUn +YuLr1LKKNE2bKeiQmKPY6BZpYB3Ue/svDkaFXGyOc0S4GleZKNxxEeI4GBpa3jbOxrG quNb3VS6eQ14ogF82pcc3dn1cGt3mnTf8YOqAj+mX/kGcHAP0WtpluAnbWcs05FyYuIO AaUtWrUJHAuUo0wgbAfoAh/XCxc3bwehYcQrfmTm3O6aYeRX+sRpRN9P48JIONgtIpxK qLoi7UVTieGRYlsuCLVlYMnLbKaxIMZZW6Xw+dNel8MgnY5iGt8h9AM969T0e1M2t27m OyTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bn9l6XmStz9eaK9bQcJo65771be8umpO2nVxq34UUT4=; b=CKzuMbKv0EscLW6YWwT5Ncfva8bJczCQNgHHRKP7n1CXMFVZ6K+HJMl0iQ+Ls5kWbE fS56Casq8yFGxKMNUVwinHnLKMLbK/DY2urqILxbfteJqYydbZWAz4d8tlFyKGAkU1a3 bVERkK/uLBmrmmAIGvVkdZflIVlqux81ONhfkGk2Qj6mV9aOxCsmU/S0BF8RmZ2TRBOR Tnk6eVxQHMbvjdT4iA4tiAQaBjUjHuXEzoa9iG85NgOIpd5TP5q6HNALvLipU/yTlTki p0ddNNYztyTZtvToEfrYBxDs66Zzdafu+/1xUUOt0KtOd6bhMJxeIjB4mK/ODhd3d7VH nUFA== X-Gm-Message-State: AODbwcAF8iZyMXTNyeptREHFVoAaylFfZrPrTQ3tAMZldVV9uINxhWqO iEckl63rOqbqJg== X-Received: by 10.223.177.140 with SMTP id q12mr496740wra.68.1494541590996; Thu, 11 May 2017 15:26:30 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:8a9:3a00:fdd7:bb97:1eb8:845e]) by smtp.gmail.com with ESMTPSA id u88sm1498029wrc.20.2017.05.11.15.26.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 May 2017 15:26:30 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Luc Van Oostenryck Subject: [PATCH v2 3/3] fix: kill unreachable BBs after killing a child Date: Fri, 12 May 2017 00:26:21 +0200 Message-Id: <20170511222621.88420-4-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.12.2 In-Reply-To: <20170511222621.88420-1-luc.vanoostenryck@gmail.com> References: <20170511222621.88420-1-luc.vanoostenryck@gmail.com> Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When simplifying a switch into a simple branch all the now unused children of the current BB must be removed. If one of these children become now orphaned, it is directly killed (it will need to be killed soon or later since it is unreachable). However, if one of the killed children is the header of a loop where some variables are updated this may cause problems. Indeed, by killing the header (which contains the phisrc of the entry value of the variable) the whole loop may become unreachable but is not killed yet, OTOH simplification of the associated OP_PHI may create a cycle which may then be detected later by simplify_one_memop() which will issue a "crazy programmer" warning while the programmer was innocent. This situation can be seen in code like: int *p; switch (i - i) { // will be optimized to 0 case 0: // will be the simple branch return 0; case 1: // will be optimized away p = ptr; do { // will be an unreachable loop *p++ = 123; } while (--i); } Fix this by calling kill_unreachable_bbs() after having simplified the switch into a branch. This will avoid to create a cycle with because of the removed phisrc in the header and as an added benefit will avoid to waste time trying to simplify BBs that are unreachable. In addition, it's now useless to call kill_bb() for each removed switch's children as kill_unreachable_bbs() will do that too. Signed-off-by: Luc Van Oostenryck --- linearize.c | 5 ++++- validation/crazy02-not-so.c | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 validation/crazy02-not-so.c diff --git a/linearize.c b/linearize.c index a9f36b823..a36ab48c3 100644 --- a/linearize.c +++ b/linearize.c @@ -643,7 +643,7 @@ static void remove_parent(struct basic_block *child, struct basic_block *parent) { remove_bb_from_list(&child->parents, parent, 1); if (!child->parents) - kill_bb(child); + repeat_phase |= REPEAT_CFG_CLEANUP; } /* Change a "switch" into a branch */ @@ -670,6 +670,9 @@ void insert_branch(struct basic_block *bb, struct instruction *jmp, struct basic remove_parent(child, bb); } END_FOR_EACH_PTR(child); PACK_PTR_LIST(&bb->children); + + if (repeat_phase & REPEAT_CFG_CLEANUP) + kill_unreachable_bbs(bb->ep); } diff --git a/validation/crazy02-not-so.c b/validation/crazy02-not-so.c new file mode 100644 index 000000000..fe7133587 --- /dev/null +++ b/validation/crazy02-not-so.c @@ -0,0 +1,22 @@ +int foo(int *ptr, int i) +{ + int *p; + + switch (i - i) { // will be optimized to 0 + case 0: + return 0; + case 1: // will be optimized away + p = ptr; + do { // will be an unreachable loop + *p++ = 123; + } while (--i); + break; + } + + return 1; +} + +/* + * check-name: crazy02-not-so.c + * check-command: sparse -Wno-decl $file + */