From patchwork Fri Mar 31 01:44:59 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: 9655413 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 2B9C860113 for ; Fri, 31 Mar 2017 01:46:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1E04028688 for ; Fri, 31 Mar 2017 01:46:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 130EC28696; Fri, 31 Mar 2017 01:46:56 +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 A2C8928688 for ; Fri, 31 Mar 2017 01:46:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935043AbdCaBqz (ORCPT ); Thu, 30 Mar 2017 21:46:55 -0400 Received: from mail-wr0-f193.google.com ([209.85.128.193]:33396 "EHLO mail-wr0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934742AbdCaBqx (ORCPT ); Thu, 30 Mar 2017 21:46:53 -0400 Received: by mail-wr0-f193.google.com with SMTP id u18so16611990wrc.0 for ; Thu, 30 Mar 2017 18:46:52 -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=/QF7PlVvGMNtzV7bzqUB1lDLIkKfKEp+8LSyPX0yISI=; b=ECumJ5i3zziOLMn+F6urT8ETBB/+oCiBfyrr2NYFwcJLyOzR7RK2xEaka8deaC6JWR eLQqiL7thUNHKtXYHoEz3gTgj/4Fx06sHq1CRWhxoXYxNtXG5aMArpaqR8+mwUkpNBAx 4N8rGn8nQ2kDsHcgAfQh+GhZocm9n0sXkEppl4wFMQB5h/lCNM4LT3puSr18izGYxRFY vWUfvWOaiEXXEv8mDFyu0Bypu4uOIwtSjMc9oASdmqGNixiaL694dbYKwDJJ5C4fMH9C vasCi7//FbdrRG+wc7bguhI4pMe9GvHGKiXMgvI65NMyAJJQnRH5d1zfqgC96XvT9jQu 345A== 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=/QF7PlVvGMNtzV7bzqUB1lDLIkKfKEp+8LSyPX0yISI=; b=U9IWZHzRpYOh+q5NZDKu8jsP7RmUpzaL5l7X7vbkRJUMcbA+szXDOTz4xTBm3tD5bS bDkg1Kf/UoR3DBZJv6JjBvCPbOHGFNRtAAiOpNuHXCweQuoB8mjItEXGzYtwCY4a+upf UJGA5EogPT08366SXGcIC1w311noaUgl2gjuuhwL4uOrzI1QxEspjugIeVdOIGg7UAGQ ZBxdyO6iJzV8lo7i5HFO6LUdI3jzrt+mzyGq/CNq5HXlpmE4j0MkL3wJpa2CrYw+9GJz FU56+jep2bqV9AxRBbCwLGNZ/H48WCqBKQrHBhsDBjhBrnrYQT/s3+C9Gg0C6xb4Am5m cqeA== X-Gm-Message-State: AFeK/H1bucPuOq67W5WDsRwPU7yTejSHX9bOw3q102IhfSV12yyNntaAKIMvargSYV91BQ== X-Received: by 10.28.172.7 with SMTP id v7mr866341wme.78.1490924811760; Thu, 30 Mar 2017 18:46:51 -0700 (PDT) Received: from localhost.localdomain ([2a02:a03f:88d:1900:cdad:92b8:5279:ae19]) by smtp.gmail.com with ESMTPSA id 189sm917023wmm.31.2017.03.30.18.46.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Mar 2017 18:46:51 -0700 (PDT) From: Luc Van Oostenryck To: linux-sparse@vger.kernel.org Cc: Christopher Li , Nicolai Stange , Luc Van Oostenryck Subject: [PATCH v4 25/25] constexpr: flag __builtin_bswap() as constexpr Date: Fri, 31 Mar 2017 03:44:59 +0200 Message-Id: <20170331014459.9351-26-luc.vanoostenryck@gmail.com> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170331014459.9351-1-luc.vanoostenryck@gmail.com> References: <20170331014459.9351-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 Signed-off-by: Luc Van Oostenryck --- builtin.c | 17 +++++++++++++++++ validation/constexpr-pure-builtin.c | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 validation/constexpr-pure-builtin.c diff --git a/builtin.c b/builtin.c index a427b45ee..139448988 100644 --- a/builtin.c +++ b/builtin.c @@ -34,6 +34,22 @@ static int evaluate_to_int_const_expr(struct expression *expr) return 1; } +static int evaluate_pure_unop(struct expression *expr) +{ + struct expression *arg = first_expression(expr->args); + int flags = arg->flags; + + /* + * Allow such functions with a constant integer expression + * argument to be treated as a *constant* integer. + * This allow us to use them in switch() { case ...: + */ + flags |= (flags & CEF_ICE) ? CEF_SET_INT : 0; + expr->flags = flags; + return 1; +} + + static int evaluate_expect(struct expression *expr) { /* Should we evaluate it to return the type of the first argument? */ @@ -201,6 +217,7 @@ static int expand_bswap(struct expression *expr, int cost) } static struct symbol_op bswap_op = { + .evaluate = evaluate_pure_unop, .expand = expand_bswap, }; diff --git a/validation/constexpr-pure-builtin.c b/validation/constexpr-pure-builtin.c new file mode 100644 index 000000000..f4cd67eda --- /dev/null +++ b/validation/constexpr-pure-builtin.c @@ -0,0 +1,23 @@ +// requires constant integer expressions +static int bar[] = { + [__builtin_bswap16(0x1234)] = 0, // OK + [__builtin_bswap32(0x1234)] = 0, // OK + [__builtin_bswap64(0x1234)] = 0, // OK +}; + +// requires constant integers +static int foo(unsigned long long a) +{ + switch (a) { + case __builtin_bswap16(1 << 8): + case __builtin_bswap32(2L << 24): + case __builtin_bswap64(3LL << 56): + return 0; + default: + return 1; + } +} + +/* + * check-name: constness of pure/const builtins + */