From patchwork Thu Nov 24 03:17:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christopher Li X-Patchwork-Id: 9444723 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 AB252600BA for ; Thu, 24 Nov 2016 03:17:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9819A276D6 for ; Thu, 24 Nov 2016 03:17:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8B25727C2D; Thu, 24 Nov 2016 03:17:49 +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_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_TVD_MIME_EPI 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 ECC4A276D6 for ; Thu, 24 Nov 2016 03:17:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754354AbcKXDRs (ORCPT ); Wed, 23 Nov 2016 22:17:48 -0500 Received: from mail-io0-f172.google.com ([209.85.223.172]:32988 "EHLO mail-io0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752248AbcKXDRr (ORCPT ); Wed, 23 Nov 2016 22:17:47 -0500 Received: by mail-io0-f172.google.com with SMTP id j65so58553901iof.0 for ; Wed, 23 Nov 2016 19:17:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=eU5V+jvjaocSP/w6sPxNsoJLM8WHpmNASQO/Owioub0=; b=AFi+d2/SvRLpJ9aDvd6Zy3T/1dJcUlJkCIDLBi8utLjcZnCdVkkOdFANuOei1NYj2k EKJHbG+owMyt0mel/XT0Vs/MOo+m4xT7v6y13sb5GYk9GQHHUk1/b6CSaI3SWKYTyaVQ h6J0redP6+e6cK2RScueLs+lPFdAFBesXXe51+M0Vya1PR23zeqlaEUoWyNCVSbJJOhQ y9giKa5jm4UYjjzUTPaOOUrwYpbTnZB0dglmjoWHj7fNG01cTqX8ohBym7t3eYXQuzK6 lmksMchgJb7rlN5PjcxjviHG2NST/RqfKHVgqdd54JXEnzwvhFELBzXm6VDZbi8cdqkV pOkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=eU5V+jvjaocSP/w6sPxNsoJLM8WHpmNASQO/Owioub0=; b=P826Dr64hhXCxVBN+cf7XL7gfWvf63E1U6QGqmXBpP2ZSwr16CEN4DBbxY054O9//N nIuWXKGH3GhxkTGEVZzzT1cwIUao9KGG+Ogbx1Ptbq8BUeKWGFEhymnq71OfiIluICHP flzskj2f/ZNeLtGEMklAEEbG8JfKdzGUe17V0IK4lNKYf7OrxiLRymlYRRZ2VF/JcrC3 mj7B37uhFxS/1fQ6/z7Vo2lljelRehYIvsUpL7RPGCd18bZk/Q9VnbYuXmLP9LkGvGn0 byzan9GcIfypjRZWADhvwgpX717OW7zUs1GDj35E0RtaiY8//ufnPU4ejusqlPIMW1dk shsA== X-Gm-Message-State: AKaTC0200gjgFIKog9X8MXOTkSlSDvHv8ncTwadqd1B6Yu1doYR9Iu3C8Ge860lLLfHpvuwZ+RCZoZmsh1fW0w== X-Received: by 10.36.57.20 with SMTP id l20mr174292ita.54.1479957451556; Wed, 23 Nov 2016 19:17:31 -0800 (PST) MIME-Version: 1.0 Received: by 10.36.209.68 with HTTP; Wed, 23 Nov 2016 19:17:30 -0800 (PST) In-Reply-To: <20161124013613.GA18220@macpro.local> References: <1479721551.8662.10.camel@sipsolutions.net> <20161122131543.GA8370@macbook.local> <20161122201650.GA12172@macpro.local> <20161123204838.GA14875@macpro.local> <20161124013613.GA18220@macpro.local> From: Christopher Li Date: Thu, 24 Nov 2016 11:17:30 +0800 X-Google-Sender-Auth: AK4w2DU2vThyx1te7mLhf4YbEkU Message-ID: Subject: Re: [PATCH v2] implement constant-folding in __builtin_bswap*() To: Luc Van Oostenryck Cc: Johannes Berg , Linux-Sparse , Nicolai Stange 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 On Thu, Nov 24, 2016 at 9:36 AM, Luc Van Oostenryck wrote: > Small typo here: "Invaild" Good catch. The corrected patch follows: Chris --- sparse.chrisl.orig/symbol.h +++ sparse.chrisl/symbol.h @@ -130,6 +130,7 @@ struct symbol_op { extern int expand_safe_p(struct expression *expr, int cost); extern int expand_constant_p(struct expression *expr, int cost); +extern int expand_bswap(struct expression *expr, int cost); #define SYM_ATTR_WEAK 0 #define SYM_ATTR_NORMAL 1 --- sparse.chrisl.orig/lib.c +++ sparse.chrisl/lib.c @@ -819,40 +819,6 @@ void declare_builtin_functions(void) add_pre_buffer("extern int __builtin_popcountl(unsigned long);\n"); add_pre_buffer("extern int __builtin_popcountll(unsigned long long);\n"); - /* And byte swaps.. */ - add_pre_buffer("extern unsigned short ____builtin_bswap16(unsigned short);\n"); - add_pre_buffer("extern unsigned int ____builtin_bswap32(unsigned int);\n"); - add_pre_buffer("extern unsigned long long ____builtin_bswap64(unsigned long long);\n"); - add_pre_buffer("#define __sparse_constant_swab16(x) ((unsigned short)(" - " (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) |" - " (((unsigned short)(x) & (unsigned short)0xff00U) >> 8)))\n"); - add_pre_buffer("#define __sparse_constant_swab32(x) ((unsigned int)(" - " (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) |" - " (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) |" - " (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) |" - " (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24)))\n"); - add_pre_buffer("#define __sparse_constant_swab64(x) ((unsigned long long)(" - " (((unsigned long long)(x) & (unsigned long long)0x00000000000000ffULL) << 56) |" - " (((unsigned long long)(x) & (unsigned long long)0x000000000000ff00ULL) << 40) |" - " (((unsigned long long)(x) & (unsigned long long)0x0000000000ff0000ULL) << 24) |" - " (((unsigned long long)(x) & (unsigned long long)0x00000000ff000000ULL) << 8) |" - " (((unsigned long long)(x) & (unsigned long long)0x000000ff00000000ULL) >> 8) |" - " (((unsigned long long)(x) & (unsigned long long)0x0000ff0000000000ULL) >> 24) |" - " (((unsigned long long)(x) & (unsigned long long)0x00ff000000000000ULL) >> 40) |" - " (((unsigned long long)(x) & (unsigned long long)0xff00000000000000ULL) >> 56)))\n"); - add_pre_buffer("#define __builtin_bswap16(x)" - " (__builtin_constant_p((unsigned short)(x)) ?" - " __sparse_constant_swab16(x) :" - " ____builtin_bswap16(x))\n"); - add_pre_buffer("#define __builtin_bswap32(x)" - " (__builtin_constant_p((unsigned int)(x)) ?" - " __sparse_constant_swab32(x) :" - " ____builtin_bswap32(x))\n"); - add_pre_buffer("#define __builtin_bswap64(x)" - " (__builtin_constant_p((unsigned long long)(x)) ?" - " __sparse_constant_swab64(x) :" - " ____builtin_bswap64(x))\n"); - /* And atomic memory access functions.. */ add_pre_buffer("extern int __sync_fetch_and_add(void *, ...);\n"); add_pre_buffer("extern int __sync_fetch_and_sub(void *, ...);\n"); --- sparse.chrisl.orig/ident-list.h +++ sparse.chrisl/ident-list.h @@ -35,6 +35,9 @@ IDENT_RESERVED(__sizeof_ptr__); IDENT_RESERVED(__builtin_types_compatible_p); IDENT_RESERVED(__builtin_offsetof); IDENT_RESERVED(__label__); +IDENT(__builtin_bswap16); +IDENT(__builtin_bswap32); +IDENT(__builtin_bswap64); /* Attribute names */ IDENT(packed); IDENT(__packed__); --- sparse.chrisl.orig/symbol.c +++ sparse.chrisl/symbol.c @@ -773,6 +773,11 @@ static struct symbol_op choose_op = { .args = arguments_choose, }; +static struct symbol_op bswap_op = { + .evaluate = evaluate_to_integer, + .expand = expand_bswap +}; + /* * Builtin functions */ @@ -788,6 +793,9 @@ static struct sym_init { { "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op }, { "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op }, { "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op }, + { "__builtin_bswap16", &builtin_fn_type, MOD_TOPLEVEL, &bswap_op }, + { "__builtin_bswap32", &builtin_fn_type, MOD_TOPLEVEL, &bswap_op }, + { "__builtin_bswap64", &builtin_fn_type, MOD_TOPLEVEL, &bswap_op }, { NULL, NULL, 0 } }; --- sparse.chrisl.orig/lib.h +++ sparse.chrisl/lib.h @@ -200,6 +200,11 @@ static inline struct instruction *first_ return first_ptr_list((struct ptr_list *)head); } +static inline struct expression *first_expression(struct expression_list *head) +{ + return first_ptr_list((struct ptr_list *)head); +} + static inline pseudo_t first_pseudo(struct pseudo_list *head) { return first_ptr_list((struct ptr_list *)head); --- sparse.chrisl.orig/expand.c +++ sparse.chrisl/expand.c @@ -802,6 +802,46 @@ int expand_safe_p(struct expression *exp return 0; } +/* The arguments are constant if the cost of all of them is zero */ +int expand_bswap(struct expression *expr, int cost) +{ + struct symbol *sym; + struct expression_list *args = expr->args; + long long input; + int count; + + if (cost) + return cost; + + sym = expr->fn->ctype; + count = expression_list_size(args); + if (count != 1) { + sparse_error(expr->pos, "Invalid number of arguments for function %s (expected 1 got %d)", + show_ident(sym->ident), count); + return SIDE_EFFECTS; + } + + input = const_expression_value(first_expression(args)); + + if (sym->ident == &__builtin_bswap16_ident) { + expr->value = __builtin_bswap16(input); + expr->ctype = &ushort_ctype; + } else if (sym->ident == &__builtin_bswap32_ident) { + expr->value = __builtin_bswap32(input); + expr->ctype = &uint_ctype; + } else if (sym->ident == &__builtin_bswap64_ident) { + expr->value = __builtin_bswap64(input); + expr->ctype = &ullong_ctype; + } else { + die("Unexpected __builtin_bswap symbol %s\n", show_ident(sym->ident)); + } + + expr->type = EXPR_VALUE; + expr->taint = 0; + return 0; +} + + /* * expand a call expression with a symbol. This * should expand builtins.