linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] linearize __builtin_isdigit()
@ 2020-10-30 16:36 Luc Van Oostenryck
  0 siblings, 0 replies; only message in thread
From: Luc Van Oostenryck @ 2020-10-30 16:36 UTC (permalink / raw)
  To: linux-sparse; +Cc: Luc Van Oostenryck

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-10-30 16:36 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-10-30 16:36 [PATCH] linearize __builtin_isdigit() Luc Van Oostenryck

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