diff options
| author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2017-02-23 22:29:20 +0100 |
|---|---|---|
| committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2018-06-28 18:55:41 +0200 |
| commit | 6c116f46f480a44c5464e5364bd8ead95f243a09 (patch) | |
| tree | fce9efa263b6e39c8fbcd6d5c8bc81266d5327a0 | |
| parent | aaefea2b1e2f1c6ce1682e6b8cbc68985d51bf38 (diff) | |
| download | sparse-dev-6c116f46f480a44c5464e5364bd8ead95f243a09.tar.gz | |
bool: simplify ZEXT in bool -> int -> bool
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@gmail.com>
| -rw-r--r-- | simplify.c | 14 | ||||
| -rw-r--r-- | validation/optim/bool-int-bool.c | 12 | ||||
| -rw-r--r-- | validation/optim/bool-simplify2.c | 50 |
3 files changed, 41 insertions, 35 deletions
@@ -615,9 +615,19 @@ static int simplify_seteq_setne(struct instruction *insn, long long value) remove_usage(old, &insn->src1); return REPEAT_CSE; - default: - return 0; + case OP_ZEXT: + if (def->orig_type->bit_size == 1) { + // Convert: + // zext.m %s <- (1) %a + // setne.1 %r <- %s, $0 + // into: + // setne.1 %s <- %a, $0 + // and same for setne/eq ... 0/1 + return replace_pseudo(insn, &insn->src1, def->src1); + } + 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 00000000..de34a68b --- /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 9113e3e5..1b429a4d 100644 --- a/validation/optim/bool-simplify2.c +++ b/validation/optim/bool-simplify2.c @@ -29,7 +29,7 @@ static bool babbb(bool a, bool b, bool c) { return a && b && c; } * * check-output-pattern(36): setne\\. * check-output-pattern(4): seteq\\. - * check-output-pattern(24): zext\\. + * check-output-pattern(8): zext\\. * check-output-pattern(12): and * check-output-pattern(12): or * check-output-end @@ -75,8 +75,7 @@ boii: setne.1 %r23 <- %arg1, $0 setne.1 %r25 <- %arg2, $0 or-bool.1 %r26 <- %r23, %r25 - zext.32 %r27 <- (1) %r26 - setne.1 %r28 <- %r27, $0 + setne.1 %r28 <- %r26, $0 ret.1 %r28 @@ -86,8 +85,7 @@ baii: setne.1 %r31 <- %arg1, $0 setne.1 %r33 <- %arg2, $0 and-bool.1 %r34 <- %r31, %r33 - zext.32 %r35 <- (1) %r34 - setne.1 %r36 <- %r35, $0 + setne.1 %r36 <- %r34, $0 ret.1 %r36 @@ -97,8 +95,7 @@ ioiii: setne.1 %r39 <- %arg1, $0 setne.1 %r41 <- %arg2, $0 or-bool.1 %r42 <- %r39, %r41 - zext.32 %r43 <- (1) %r42 - setne.1 %r44 <- %r43, $0 + setne.1 %r44 <- %r42, $0 setne.1 %r46 <- %arg3, $0 or-bool.1 %r47 <- %r44, %r46 zext.32 %r48 <- (1) %r47 @@ -111,8 +108,7 @@ iaiii: setne.1 %r51 <- %arg1, $0 setne.1 %r53 <- %arg2, $0 and-bool.1 %r54 <- %r51, %r53 - zext.32 %r55 <- (1) %r54 - setne.1 %r56 <- %r55, $0 + setne.1 %r56 <- %r54, $0 setne.1 %r58 <- %arg3, $0 and-bool.1 %r59 <- %r56, %r58 zext.32 %r60 <- (1) %r59 @@ -125,12 +121,10 @@ boiii: setne.1 %r63 <- %arg1, $0 setne.1 %r65 <- %arg2, $0 or-bool.1 %r66 <- %r63, %r65 - zext.32 %r67 <- (1) %r66 - setne.1 %r68 <- %r67, $0 + setne.1 %r68 <- %r66, $0 setne.1 %r70 <- %arg3, $0 or-bool.1 %r71 <- %r68, %r70 - zext.32 %r72 <- (1) %r71 - setne.1 %r73 <- %r72, $0 + setne.1 %r73 <- %r71, $0 ret.1 %r73 @@ -140,12 +134,10 @@ baiii: setne.1 %r76 <- %arg1, $0 setne.1 %r78 <- %arg2, $0 and-bool.1 %r79 <- %r76, %r78 - zext.32 %r80 <- (1) %r79 - setne.1 %r81 <- %r80, $0 + setne.1 %r81 <- %r79, $0 setne.1 %r83 <- %arg3, $0 and-bool.1 %r84 <- %r81, %r83 - zext.32 %r85 <- (1) %r84 - setne.1 %r86 <- %r85, $0 + setne.1 %r86 <- %r84, $0 ret.1 %r86 @@ -183,8 +175,7 @@ bobb: .L28: <entry-point> or-bool.1 %r107 <- %arg1, %arg2 - zext.32 %r108 <- (1) %r107 - setne.1 %r109 <- %r108, $0 + setne.1 %r109 <- %r107, $0 ret.1 %r109 @@ -192,8 +183,7 @@ babb: .L30: <entry-point> and-bool.1 %r113 <- %arg1, %arg2 - zext.32 %r114 <- (1) %r113 - setne.1 %r115 <- %r114, $0 + setne.1 %r115 <- %r113, $0 ret.1 %r115 @@ -201,8 +191,7 @@ iobbb: .L32: <entry-point> or-bool.1 %r119 <- %arg1, %arg2 - zext.32 %r120 <- (1) %r119 - setne.1 %r121 <- %r120, $0 + setne.1 %r121 <- %r119, $0 or-bool.1 %r123 <- %r121, %arg3 zext.32 %r124 <- (1) %r123 ret.32 %r124 @@ -212,8 +201,7 @@ iabbb: .L34: <entry-point> and-bool.1 %r128 <- %arg1, %arg2 - zext.32 %r129 <- (1) %r128 - setne.1 %r130 <- %r129, $0 + setne.1 %r130 <- %r128, $0 and-bool.1 %r132 <- %r130, %arg3 zext.32 %r133 <- (1) %r132 ret.32 %r133 @@ -223,11 +211,9 @@ bobbb: .L36: <entry-point> or-bool.1 %r137 <- %arg1, %arg2 - zext.32 %r138 <- (1) %r137 - setne.1 %r139 <- %r138, $0 + setne.1 %r139 <- %r137, $0 or-bool.1 %r141 <- %r139, %arg3 - zext.32 %r142 <- (1) %r141 - setne.1 %r143 <- %r142, $0 + setne.1 %r143 <- %r141, $0 ret.1 %r143 @@ -235,11 +221,9 @@ babbb: .L38: <entry-point> and-bool.1 %r147 <- %arg1, %arg2 - zext.32 %r148 <- (1) %r147 - setne.1 %r149 <- %r148, $0 + setne.1 %r149 <- %r147, $0 and-bool.1 %r151 <- %r149, %arg3 - zext.32 %r152 <- (1) %r151 - setne.1 %r153 <- %r152, $0 + setne.1 %r153 <- %r151, $0 ret.1 %r153 |
