All of lore.kernel.org
 help / color / mirror / Atom feed
From: Johannes Berg <johannes@sipsolutions.net>
To: sparse@chrisli.org
Cc: linux-sparse@vger.kernel.org
Subject: [PATCH v2] implement constant-folding in __builtin_bswap*()
Date: Thu, 17 Nov 2016 10:55:03 +0100	[thread overview]
Message-ID: <20161117095503.8754-1-johannes@sipsolutions.net> (raw)

Since gcc does this, it's apparently valid to write

 switch (x) {
 case __builtin_bswap16(12):
   break;
 }

but sparse will flag it as an error today.

The constant folding used to be done in the kernel's htons() and
friends, but due to gcc bugs that isn't done anymore since
commit 7322dd755e7d ("byteswap: try to avoid __builtin_constant_p
gcc bug").

To get rid of the sparse errors on every such instance now, just
add constant folding to __builtin_bswap*() in sparse.

Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
---
 lib.c                               | 35 ++++++++++++++++++++++++++++++++---
 validation/bswap-constant-folding.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 3 deletions(-)
 create mode 100644 validation/bswap-constant-folding.c

diff --git a/lib.c b/lib.c
index d5b56b012a21..aa2af68bf8d2 100644
--- a/lib.c
+++ b/lib.c
@@ -818,9 +818,38 @@ void declare_builtin_functions(void)
 	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("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");
diff --git a/validation/bswap-constant-folding.c b/validation/bswap-constant-folding.c
new file mode 100644
index 000000000000..c6511fe62041
--- /dev/null
+++ b/validation/bswap-constant-folding.c
@@ -0,0 +1,28 @@
+typedef unsigned short __be16;
+typedef unsigned short __u16;
+typedef unsigned short u16;
+#define __force
+
+#define __swab16(x) (__u16)__builtin_bswap16((__u16)(x))
+/* the test behaves as though it's always on a little-endian machine */
+#define __cpu_to_be16(x) ((__force __be16)__swab16((x)))
+#define ___htons(x) __cpu_to_be16(x)
+#define htons(x) ___htons(x)
+
+#define ETH_P_IPV6 0x86DD
+
+static u16 protocol;
+
+static void test(void)
+{
+	switch (protocol) {
+	case htons(ETH_P_IPV6):
+		break;
+	}
+}
+
+/*
+ * check-name: constant folding in bswap builtins
+ * check-error-start
+ * check-error-end
+ */
-- 
2.9.3


             reply	other threads:[~2016-11-17  9:55 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-17  9:55 Johannes Berg [this message]
2016-11-21  2:38 ` [PATCH v2] implement constant-folding in __builtin_bswap*() 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
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=20161117095503.8754-1-johannes@sipsolutions.net \
    --to=johannes@sipsolutions.net \
    --cc=linux-sparse@vger.kernel.org \
    --cc=sparse@chrisli.org \
    /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.