linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Christopher Li <sparse@chrisli.org>,
	Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 3/3] simplify casts bool -> int -> bool
Date: Wed, 12 Apr 2017 16:18:02 +0200	[thread overview]
Message-ID: <20170412141802.81231-4-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20170412141802.81231-1-luc.vanoostenryck@gmail.com>

Because of C's integer promotion, in code like 'a == 0',
the operand 'a' must be promoted to int, which result in
following linearization if the type of 'a' was _Bool:
	cast.32  %t <- (1) %a
	setne.32 %r <- %t, $0
While required by the standard, this promotion is unneeded
in the given situation.

Change this by simplifying away such casts.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 simplify.c                       | 18 +++++++++++++++++-
 validation/optim/bool-int-bool.c | 12 ++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 validation/optim/bool-int-bool.c

diff --git a/simplify.c b/simplify.c
index 775c1e2dd..96448a666 100644
--- a/simplify.c
+++ b/simplify.c
@@ -445,9 +445,25 @@ static int simplify_seteq_setne(struct instruction *insn, long long value)
 		remove_usage(old, &insn->src1);
 		return REPEAT_CSE;
 
+	case OP_CAST:
+		if (def->orig_type->bit_size != 1)
+			break;
+
+		// Convert:
+		//	cast.n	%t <- (1) %a
+		//	setne.m %r <- %t, $0
+		// into:
+		//	...
+		//	setne.m %r <- %a, $0
+		// and similar for setne/eq ... 0/1
+		use_pseudo(insn, def->src1, &insn->src1);
+		remove_usage(old, &insn->src1);
+		return REPEAT_CSE;
+
 	default:
-		return 0;
+		break;
 	}
+	return 0;
 }
 
 static int simplify_constant_rightside(struct instruction *insn)
diff --git a/validation/optim/bool-int-bool.c b/validation/optim/bool-int-bool.c
new file mode 100644
index 000000000..de34a68bb
--- /dev/null
+++ b/validation/optim/bool-int-bool.c
@@ -0,0 +1,12 @@
+_Bool beq0(_Bool a) { return (a == 0); }
+_Bool beq1(_Bool a) { return (a == 1); }
+_Bool bne0(_Bool a) { return (a != 0); }
+_Bool bne1(_Bool a) { return (a != 1); }
+
+/*
+ * check-name: bool - int - bool constants
+ * check-command: test-linearize -Wno-decl $file
+ *
+ * check-output-ignore
+ * check-output-excludes: cast\\.
+ */
-- 
2.12.0


      parent reply	other threads:[~2017-04-12 14:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-12 14:17 [PATCH 0/3] Simplify booleans Luc Van Oostenryck
2017-04-12 14:18 ` [PATCH 1/3] simplify 'x | ~0' and 'x & ~0' Luc Van Oostenryck
2017-04-12 14:18 ` [PATCH 2/3] simplify 'x ^ ~0' to '~x' Luc Van Oostenryck
2017-04-12 14:18 ` Luc Van Oostenryck [this message]

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=20170412141802.81231-4-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).