Because of C's integer promotion, in code like 'a == 0', the operand 'a' must be promoted to int. So, if 'a' is of type 'bool', it results in following linearization: zext.32 %t <- (1) %a setne.32 %r <- %t, $0 While this promotion is required by the standard at C level, here, from an operational PoV, the zero-extension is unneeded since the result will be the same without it. Change this by simplifying away such zero-extensions. Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx> --- simplify.c | 15 ++++++++++++++- validation/optim/bool-int-bool.c | 12 ++++++++++++ validation/optim/bool-simplify2.c | 5 ++--- 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 validation/optim/bool-int-bool.c diff --git a/simplify.c b/simplify.c index c88ea5c33..9f0d13b55 100644 --- a/simplify.c +++ b/simplify.c @@ -612,9 +612,22 @@ static int simplify_seteq_setne(struct instruction *insn, long long value) remove_usage(old, &insn->src1); return REPEAT_CSE; + case OP_ZEXT: + if (def->orig_type->bit_size == 1) { + // Convert: + // zext.n %s <- (1) %a + // setne.n %r <- %s, $0 + // into: + // setne.n %s <- %a, $0 + // and same for setne/eq ... 0/1 + return replace_pseudo(insn, &insn->src1, def->src1); + } + break; + 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\\. + */ diff --git a/validation/optim/bool-simplify2.c b/validation/optim/bool-simplify2.c index 7e7548fc5..72fc2f2ea 100644 --- a/validation/optim/bool-simplify2.c +++ b/validation/optim/bool-simplify2.c @@ -8,7 +8,7 @@ static int foo(int a, int b, int c) * check-command: test-linearize $file * * check-output-pattern(4): setne\\. - * check-output-pattern(2): zext\\. + * check-output-pattern(1): zext\\. * * check-output-start foo: @@ -17,8 +17,7 @@ foo: setne.1 %r2 <- %arg1, $0 setne.1 %r4 <- %arg2, $0 or-bool.1 %r5 <- %r2, %r4 - zext.32 %r6 <- (1) %r5 - setne.1 %r7 <- %r6, $0 + setne.1 %r7 <- %r5, $0 setne.1 %r9 <- %arg3, $0 or-bool.1 %r10 <- %r7, %r9 zext.32 %r11 <- (1) %r10 -- 2.18.0 -- To unsubscribe from this list: send the line "unsubscribe linux-sparse" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html