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: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH] linearize __builtin_isdigit()
Date: Fri, 30 Oct 2020 17:36:05 +0100	[thread overview]
Message-ID: <20201030163605.20165-1-luc.vanoostenryck@gmail.com> (raw)

As an experiment about the linearization of builtins,
try this easy one (and statically expand it if the
argument is constant).

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 builtin.c   | 21 +++++++++++++++++++++
 linearize.c | 22 ++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/builtin.c b/builtin.c
index acc49871644c..5c7321cad3e4 100644
--- a/builtin.c
+++ b/builtin.c
@@ -305,6 +305,26 @@ static struct symbol_op fp_unop_op = {
 };
 
 
+static int expand_isdigit(struct expression *expr, int cost)
+{
+	struct expression *arg = first_expression(expr->args);
+	long long val = get_expression_value_silent(arg);
+
+	if (cost)
+		return cost;
+
+	expr->value = (val >= '0') && (val <= '9');
+	expr->type = EXPR_VALUE;
+	expr->taint = 0;
+	return 0;
+}
+
+static struct symbol_op isdigit_op = {
+	.evaluate = evaluate_pure_unop,
+	.expand = expand_isdigit,
+};
+
+
 static int evaluate_overflow_gen(struct expression *expr, int ptr)
 {
 	struct expression *arg;
@@ -552,6 +572,7 @@ static const struct builtin_fn builtins_common[] = {
 	{ "__builtin_inf", &double_ctype, 0 },
 	{ "__builtin_inff", &float_ctype, 0 },
 	{ "__builtin_infl", &ldouble_ctype, 0 },
+	{ "__builtin_isdigit", &int_ctype, 0, { &int_ctype }, .op = &isdigit_op },
 	{ "__builtin_isfinite", &int_ctype, 1, .op = &fp_unop_op },
 	{ "__builtin_isgreater", &int_ctype, 0, { &float_ctype, &float_ctype }},
 	{ "__builtin_isgreaterequal", &int_ctype, 0, { &float_ctype, &float_ctype }},
diff --git a/linearize.c b/linearize.c
index bf9b6ecdc92e..b74651765f6c 100644
--- a/linearize.c
+++ b/linearize.c
@@ -2602,6 +2602,27 @@ static pseudo_t linearize_fma(struct entrypoint *ep, struct expression *expr)
 	return insn->target = alloc_pseudo(insn);
 }
 
+static pseudo_t linearize_isdigit(struct entrypoint *ep, struct expression *expr)
+{
+	struct instruction *insn;
+	pseudo_t src;
+
+	insn = alloc_typed_instruction(OP_SUB, &int_ctype);
+	src = linearize_expression(ep, first_expression(expr->args));
+	use_pseudo(insn, src, &insn->src1);
+	insn->src2 = value_pseudo('0');
+	src = insn->target = alloc_pseudo(insn);
+	add_one_insn(ep, insn);
+
+	insn = alloc_typed_instruction(OP_SET_BE, &int_ctype);
+	use_pseudo(insn, src, &insn->src1);
+	insn->src2 = value_pseudo(9);
+	insn->target = alloc_pseudo(insn);
+	add_one_insn(ep, insn);
+
+	return insn->target;
+}
+
 static pseudo_t linearize_unreachable(struct entrypoint *ep, struct expression *exp)
 {
 	add_unreachable(ep);
@@ -2617,6 +2638,7 @@ static struct sym_init {
 	{ "__builtin_fma", linearize_fma },
 	{ "__builtin_fmaf", linearize_fma },
 	{ "__builtin_fmal", linearize_fma },
+	{ "__builtin_isdigit", linearize_isdigit },
 	{ "__builtin_unreachable", linearize_unreachable },
 	{ }
 };
-- 
2.29.1


                 reply	other threads:[~2020-10-30 16:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20201030163605.20165-1-luc.vanoostenryck@gmail.com \
    --to=luc.vanoostenryck@gmail.com \
    --cc=linux-sparse@vger.kernel.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).