From patchwork Fri Jun 16 19:18:44 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: 9793429 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 6A39660231 for ; Fri, 16 Jun 2017 19:19:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5A96C2855C for ; Fri, 16 Jun 2017 19:19:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4F27828599; Fri, 16 Jun 2017 19:19:01 +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.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, 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 A641F28573 for ; Fri, 16 Jun 2017 19:19:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751012AbdFPTTA (ORCPT ); Fri, 16 Jun 2017 15:19:00 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:34438 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751798AbdFPTS7 (ORCPT ); Fri, 16 Jun 2017 15:18:59 -0400 Received: by mail-wm0-f68.google.com with SMTP id 70so6632123wme.1 for ; Fri, 16 Jun 2017 12:18:59 -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=c1F+Et8kPX1lbBROTP7EMWDFYbKsOiqRRI3z/n/cBXs=; b=EjyafEN2gXI6H0Lvej+U1F61/3JY9XrPZRvD66uEqYtHKHTomfB2g1x04q+I7KhBWt GAhVnuqdqBAphbbAgTHtTc0x7Ta3RS1JvdzIxRS9WTsNW1vy09MS2FX/2ncr7LegRK41 LDxLYnOJC/nQ0baT+h9P86uKxglHeQw1codGgfBW3khjgi4IeInNXizw2+oUUeeaOP5S IFPRdqv9b9Kx4vfOQq7WDOiUdtSAVpq3Np5a9qpum4rrFcRfEd0vyAVV3W6pLEJhTOq2 jc4ua8mpxlednMx4y/xfbSPi4Q09ekEhsszzvUcmXSO8n2ATItfCYmSz11DnuYTzH6eq VfKg== 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=c1F+Et8kPX1lbBROTP7EMWDFYbKsOiqRRI3z/n/cBXs=; b=Mqxg68hW+Et5Uhg5u+UKoFdfj43J6fEKRpOxFxVdwkbvMBKxqyzziWp6UPMEGU7gMo wo8ssmPWnLBnsJ81Fog1dhIjcipesN3sXk5m0FwRQ+aFgsqgW9/tmG08KVn2Ml4/mOHe fYFFhegi4FeyTcR6DMmwhpwrCVr5vGbg8FpS4yUYNtz8kg1JUP1y0S8U5E8EoAPs1YEu j4+kQ9YjUuixOt3xHgX5PmGlarndscAsGu4xKvcz6CHmysPfkEcxlYnv/9JKmHUWxUOu 0UvvFOlEab/NfWmVEcvbPktbBYXBZeD80hC32b2FMa3V4RH8S/ElS7VUrxICgVQsPud8 dKGQ== X-Gm-Message-State: AKS2vOxtWUaESLNTXEWUSFXzQ6TPA2GqYuyeWDHVMTq2fDmeDj8GjEVB iCWiN1loO2W0ongvT9w= X-Received: by 10.80.179.76 with SMTP id r12mr8616122edd.1.1497640738043; Fri, 16 Jun 2017 12:18:58 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:89c:5400:a531:3442:3c25:b1bd]) by smtp.gmail.com with ESMTPSA id r28sm1598048edd.33.2017.06.16.12.18.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Jun 2017 12:18:57 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Chris Li , Ramsay Jones , Luc Van Oostenryck Subject: [PATCH 2/2] fix: try_to_simplify_bb eargerness Date: Fri, 16 Jun 2017 21:18:44 +0200 Message-Id: <20170616191844.86256-3-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170616191844.86256-1-luc.vanoostenryck@gmail.com> References: <20170616191844.86256-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 The simplification done by try_to_simplify_bb() (essentially trying to bypass a basic block containing a conditional branch if the branch is controlled by an OP_PHI when the corresponding OP_PHISRC is a constant) can only be done if some conditions are met: 1) The basic block doesn't have some side effects (in which case it must not be bypassed). Checked by bb_has_side_effects(). 2) There may be some pseudos defined in the basic block but no basic blocks may depend on them. Checked by bb_depends_on(). The second condition is efficiently checked using liveness information. However, there is no liveness information done for OP_PHI/OP_PHISRC. So if the basic block contains some other OP_PHI than the controlling one, there will surely be some other BB depending on it but this will not be reflected in the liveness info and bb_depends_on() can then wrongly report that no dependencies exist. Fix this by adding an extra check, verifiying that no other OP_PHI are defined in the BB and avoiding the simplification otherwise. Reported-by: Ramsay Jones Signed-off-by: Luc Van Oostenryck --- flow.c | 25 +++++++++++++++++++++++++ validation/crazy03.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 validation/crazy03.c diff --git a/flow.c b/flow.c index 8a9005aa6..23145d6e7 100644 --- a/flow.c +++ b/flow.c @@ -79,6 +79,29 @@ static int bb_depends_on(struct basic_block *target, struct basic_block *src) return 0; } +/* + * This is only to be used by try_to_simplify_bb(). + * It really should be handled by bb_depends_on(), only + * that there is no liveness done on OP_PHI/OP_PHISRC. + * So we consider for now that if there is an OP_PHI + * then some block in fact depends on this one. + * The OP_PHI controling the conditional branch of this bb + * is excluded since the branch will be removed. + */ +static int bb_defines_phi(struct basic_block *bb, struct instruction *def) +{ + struct instruction *insn; + FOR_EACH_PTR(bb->insns, insn) { + switch (insn->opcode) { + case OP_PHI: + if (def && insn != def) + return 1; + continue; + } + } END_FOR_EACH_PTR(insn); + return 0; +} + /* * When we reach here, we have: * - a basic block that ends in a conditional branch and @@ -127,6 +150,8 @@ static int try_to_simplify_bb(struct basic_block *bb, struct instruction *first, target = true ? second->bb_true : second->bb_false; if (bb_depends_on(target, bb)) continue; + if (bb_defines_phi(bb, first)) + continue; changed |= rewrite_branch(source, &br->bb_true, bb, target); changed |= rewrite_branch(source, &br->bb_false, bb, target); if (changed && !bogus) diff --git a/validation/crazy03.c b/validation/crazy03.c new file mode 100644 index 000000000..042033790 --- /dev/null +++ b/validation/crazy03.c @@ -0,0 +1,33 @@ +extern char a; +extern int b; +extern char *c, *d; +extern void e(void); +extern void f(char *); + +int g(int h); +int g(int h) +{ + if (h > 1) + e(); + if (h > 1) + return 0; + for (;;) { + if (a) { + while (c) ; + b = 0; + } else { + c = (void*)0; + b = 1; + } + if (b) { + f(c); + continue; + } + d = c; + while (*c++) ; + } +} + +/* + * check-name: crazy03.c + */