diff options
| author | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2016-12-14 09:55:20 +0100 |
|---|---|---|
| committer | Luc Van Oostenryck <luc.vanoostenryck@gmail.com> | 2017-05-12 01:36:07 +0200 |
| commit | 6217cd5c130071a65c4d30d8e846f9c9ef0a9bd4 (patch) | |
| tree | b5773e1ca69a2bd3b159da6c559d7a473782b66f /validation/optim | |
| parent | e35efe330c6ae7d154197c29b127560d569016d0 (diff) | |
| download | sparse-dev-6217cd5c130071a65c4d30d8e846f9c9ef0a9bd4.tar.gz | |
fix boolean context for OP_AND_BOOL & OP_OR_BOOL
Current simplifications of 'x && 1 --> x' and its dual
'x || 0 --> x' are wrong because the '||' and '&&' operators
demand that their operands are first compared against zero
which then always give a boolean valued result.
For example: '3 && 1' is not equal to '3' but to '1' (or 'true').
The correct simplification is thus 'x && 1 --> x != 0' and
'x || 0 --> x != 0'.
Fix this by always first doing the comparison against zero
before generating the OP_AND_BOOL and OP_OR_BOOL instructions.
Note: of course, we could decide that the semantic of OP_AND_BOOL
and OP_OR_BOOL is that these ops take care themselves of
making a boolean context (which was, I think, why these
ops were created) but then these simplifications cannot be
done (or when they are done, we need to add the comparison
against zero).
Fixes: b85ec4bb7f5b1c522d7c71782dbd9cf1c4c49b2f
Fixes: a0886db12307d2633b04ec44342099a2955794a5
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Diffstat (limited to 'validation/optim')
| -rw-r--r-- | validation/optim/bool-context.c | 12 | ||||
| -rw-r--r-- | validation/optim/bool-simplify.c | 8 |
2 files changed, 18 insertions, 2 deletions
diff --git a/validation/optim/bool-context.c b/validation/optim/bool-context.c new file mode 100644 index 00000000..11326d39 --- /dev/null +++ b/validation/optim/bool-context.c @@ -0,0 +1,12 @@ +#define bool _Bool + +bool bool_ior(int a, int b) { return a || b; } +bool bool_and(int a, int b) { return a && b; } + +/* + * check-name: bool-context + * check-command: test-linearize -Wno-decl $file + * check-output-ignore + * + * check-output-pattern-4-times: setne\\..* %arg[12] + */ diff --git a/validation/optim/bool-simplify.c b/validation/optim/bool-simplify.c index e0ff1c2d..05be1149 100644 --- a/validation/optim/bool-simplify.c +++ b/validation/optim/bool-simplify.c @@ -32,13 +32,17 @@ and_0: and_1: .L2: <entry-point> - ret.32 %arg1 + setne.1 %r8 <- %arg1, $0 + cast.32 %r11 <- (1) %r8 + ret.32 %r11 or_0: .L4: <entry-point> - ret.32 %arg1 + setne.1 %r14 <- %arg1, $0 + cast.32 %r17 <- (1) %r14 + ret.32 %r17 or_1: |
