All of lore.kernel.org
 help / color / mirror / Atom feed
From: Christopher Li <sparse@chrisli.org>
To: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Cc: Johannes Berg <johannes@sipsolutions.net>,
	Linux-Sparse <linux-sparse@vger.kernel.org>,
	Nicolai Stange <nicstange@gmail.com>
Subject: Re: [PATCH v2] implement constant-folding in __builtin_bswap*()
Date: Thu, 24 Nov 2016 11:17:30 +0800	[thread overview]
Message-ID: <CANeU7Qm26W=si8wX0JEi5TeiHjXngoiQ4P185yDA8mvqnPuQ0Q@mail.gmail.com> (raw)
In-Reply-To: <20161124013613.GA18220@macpro.local>

[-- Attachment #1: Type: text/plain, Size: 167 bytes --]

On Thu, Nov 24, 2016 at 9:36 AM, Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
> Small typo here: "Invaild"

Good catch. The corrected patch follows:

Chris

[-- Attachment #2: implement-builtin-bswap-2.patch --]
[-- Type: text/x-patch, Size: 5948 bytes --]







--- 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.

  reply	other threads:[~2016-11-24  3:17 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-17  9:55 [PATCH v2] implement constant-folding in __builtin_bswap*() Johannes Berg
2016-11-21  2:38 ` Christopher Li
2016-11-21  9:45   ` Johannes Berg
2016-11-22 11:13     ` Christopher Li
2016-11-22 11:39       ` Christopher Li
2016-11-22 13:15       ` Luc Van Oostenryck
2016-11-22 16:32         ` Christopher Li
2016-11-22 17:12           ` Luc Van Oostenryck
2016-11-23  1:23             ` Christopher Li
2016-11-22 20:16           ` Luc Van Oostenryck
2016-11-23  1:25             ` Christopher Li
2016-11-23 20:38               ` [PATCH] add test case for builtin bswap with constant args Luc Van Oostenryck
2016-11-24  0:17                 ` Christopher Li
2016-11-24  5:30                 ` Christopher Li
2016-11-24 13:00                   ` Luc Van Oostenryck
2016-11-23 20:48               ` [PATCH v2] implement constant-folding in __builtin_bswap*() Luc Van Oostenryck
2016-11-24  0:56                 ` Christopher Li
2016-11-24  0:58                   ` Christopher Li
2016-11-24  1:36                     ` Luc Van Oostenryck
2016-11-24  3:17                       ` Christopher Li [this message]
2016-11-24  1:31                   ` Luc Van Oostenryck
2016-11-24  3:23                     ` Christopher Li
2016-11-23  6:28           ` Johannes Berg
2016-11-23 14:30             ` Christopher Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CANeU7Qm26W=si8wX0JEi5TeiHjXngoiQ4P185yDA8mvqnPuQ0Q@mail.gmail.com' \
    --to=sparse@chrisli.org \
    --cc=johannes@sipsolutions.net \
    --cc=linux-sparse@vger.kernel.org \
    --cc=luc.vanoostenryck@gmail.com \
    --cc=nicstange@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.