From 520454ec595c50050740d9375047c897f1cf3339 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 31 Jan 2021 14:51:33 +0100 Subject: change testing of signed compares against SMIN or SMAX These tests are written by testing if the comparisons are equal to their expected value: 0 or 1. So, a compare of a compare but such compares of compare have their own simplification which defeats what's tested here. So, rewrite the test to avoid such compares of compare. Signed-off-by: Luc Van Oostenryck --- validation/optim/cmps-minmax.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/validation/optim/cmps-minmax.c b/validation/optim/cmps-minmax.c index 5802cdbc..0b1a0a09 100644 --- a/validation/optim/cmps-minmax.c +++ b/validation/optim/cmps-minmax.c @@ -1,11 +1,11 @@ #define SMAX __INT_MAX__ #define SMIN (-__INT_MAX__-1) -int lt_smin(int a) { return (a < SMIN) == 0; } -int le_smax(int a) { return (a <= SMAX) == 1; } +int lt_smin(int a) { return (a < SMIN) + 1; } +int le_smax(int a) { return (a <= SMAX) + 0; } -int ge_smin(int a) { return (a >= SMIN) == 1; } -int gt_smax(int a) { return (a > SMAX) == 0; } +int ge_smin(int a) { return (a >= SMIN) + 0; } +int gt_smax(int a) { return (a > SMAX) + 1; } /* * check-name: cmps-minmax -- cgit 1.2.3-korg From b9429057955ed194b072ca181d61c47c562ebcaa Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 31 Jan 2021 14:51:33 +0100 Subject: add testcases for constant compares against AND/OR Signed-off-by: Luc Van Oostenryck --- validation/optim/cmpe-and0.c | 11 +++++++++++ validation/optim/cmpe-or0.c | 11 +++++++++++ validation/optim/cmps-and0.c | 22 ++++++++++++++++++++++ validation/optim/cmps-or0.c | 22 ++++++++++++++++++++++ validation/optim/cmps0-and0.c | 13 +++++++++++++ validation/optim/cmpu-and0.c | 18 ++++++++++++++++++ validation/optim/cmpu-or0.c | 19 +++++++++++++++++++ 7 files changed, 116 insertions(+) create mode 100644 validation/optim/cmpe-and0.c create mode 100644 validation/optim/cmpe-or0.c create mode 100644 validation/optim/cmps-and0.c create mode 100644 validation/optim/cmps-or0.c create mode 100644 validation/optim/cmps0-and0.c create mode 100644 validation/optim/cmpu-and0.c create mode 100644 validation/optim/cmpu-or0.c diff --git a/validation/optim/cmpe-and0.c b/validation/optim/cmpe-and0.c new file mode 100644 index 00000000..7db60836 --- /dev/null +++ b/validation/optim/cmpe-and0.c @@ -0,0 +1,11 @@ +int cmpe_and_eq(int a) { return ((a & 0xff00) == 0xff01) + 1; } +int cmpe_and_ne(int a) { return ((a & 0xff00) != 0xff01) + 0; } + +/* + * check-name: cmpe-and0 + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ diff --git a/validation/optim/cmpe-or0.c b/validation/optim/cmpe-or0.c new file mode 100644 index 00000000..bef23161 --- /dev/null +++ b/validation/optim/cmpe-or0.c @@ -0,0 +1,11 @@ +int cmp_eq(int a) { return ((a | 1) != 0) + 0; } +int cmp_ne(int a) { return ((a | 1) == 0) + 1; } + +/* + * check-name: cmpe-or0 + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ diff --git a/validation/optim/cmps-and0.c b/validation/optim/cmps-and0.c new file mode 100644 index 00000000..097ec2f9 --- /dev/null +++ b/validation/optim/cmps-and0.c @@ -0,0 +1,22 @@ +#define MINUS_ONE -1 +#define MASK 32 + + +int cmps_and_lt_lt0(int a) { return ((a & MASK) < MINUS_ONE) + 1; } +int cmps_and_lt_gtm(int a) { return ((a & MASK) < (MASK + 1)) + 0; } +int cmps_and_le_lt0(int a) { return ((a & MASK) <= MINUS_ONE) + 1; } +int cmps_and_le_gtm(int a) { return ((a & MASK) <= (MASK + 1)) + 0; } + +int cmps_and_gt_lt0(int a) { return ((a & MASK) > MINUS_ONE) + 0; } +int cmps_and_gt_gtm(int a) { return ((a & MASK) > (MASK + 1)) + 1; } +int cmps_and_ge_lt0(int a) { return ((a & MASK) >= MINUS_ONE) + 0; } +int cmps_and_ge_gtm(int a) { return ((a & MASK) >= (MASK + 1)) + 1; } + +/* + * check-name: cmps-and0 + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ diff --git a/validation/optim/cmps-or0.c b/validation/optim/cmps-or0.c new file mode 100644 index 00000000..40a2092c --- /dev/null +++ b/validation/optim/cmps-or0.c @@ -0,0 +1,22 @@ +#define EQ(X) + (X == 0) +#define SIGN (1 << 31) +#define MASK (SIGN | 32) + + +int cmps_ior_lt_x(int a) { return ((a | MASK) < 4) EQ(1); } +int cmps_ior_lt_0(int a) { return ((a | MASK) < 0) EQ(1); } +int cmps_ior_le_x(int a) { return ((a | MASK) <= 4) EQ(1); } +int cmps_ior_le_0(int a) { return ((a | MASK) <= 0) EQ(1); } +int cmps_ior_ge_x(int a) { return ((a | MASK) >= 4) EQ(0); } +int cmps_ior_ge_0(int a) { return ((a | MASK) >= 0) EQ(0); } +int cmps_ior_gt_x(int a) { return ((a | MASK) > 4) EQ(0); } +int cmps_ior_gt_0(int a) { return ((a | MASK) > 0) EQ(0); } + +/* + * check-name: cmps-or0 + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ diff --git a/validation/optim/cmps0-and0.c b/validation/optim/cmps0-and0.c new file mode 100644 index 00000000..819a1dc2 --- /dev/null +++ b/validation/optim/cmps0-and0.c @@ -0,0 +1,13 @@ +#define M 32 + +int cmps_and_sle0(int a) { return ((a & M) <= 0) == ((a & M) == 0); } +int cmps_and_sgt0(int a) { return ((a & M) > 0) == ((a & M) != 0); } + +/* + * check-name: cmps0-and + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ diff --git a/validation/optim/cmpu-and0.c b/validation/optim/cmpu-and0.c new file mode 100644 index 00000000..25321294 --- /dev/null +++ b/validation/optim/cmpu-and0.c @@ -0,0 +1,18 @@ +#define MASK 32U + + +int cmps_and_ltu_gt(int a) { return ((a & MASK) < (MASK + 1)) + 0; } +int cmps_and_leu_gt(int a) { return ((a & MASK) <= (MASK + 1)) + 0; } +int cmps_and_leu_eq(int a) { return ((a & MASK) <= (MASK + 0)) + 0; } +int cmps_and_geu_gt(int a) { return ((a & MASK) >= (MASK + 1)) + 1; } +int cmps_and_gtu_gt(int a) { return ((a & MASK) > (MASK + 1)) + 1; } +int cmps_and_gtu_eq(int a) { return ((a & MASK) > (MASK + 0)) + 1; } + +/* + * check-name: cmpu-and0 + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ diff --git a/validation/optim/cmpu-or0.c b/validation/optim/cmpu-or0.c new file mode 100644 index 00000000..77360d3f --- /dev/null +++ b/validation/optim/cmpu-or0.c @@ -0,0 +1,19 @@ +#define EQ(X) + (X == 0) +#define MASK 32U + + +int cmpu_ior_lt_lt(int a) { return ((a | MASK) < (MASK - 1)) EQ(0); } +int cmpu_ior_lt_eq(int a) { return ((a | MASK) < (MASK )) EQ(0); } +int cmpu_ior_le_lt(int a) { return ((a | MASK) <= (MASK - 1)) EQ(0); } +int cmpu_ior_ge_lt(int a) { return ((a | MASK) >= (MASK - 1)) EQ(1); } +int cmpu_ior_ge_eq(int a) { return ((a | MASK) >= (MASK )) EQ(1); } +int cmpu_ior_gt_lt(int a) { return ((a | MASK) > (MASK - 1)) EQ(1); } + +/* + * check-name: cmpu-or0 + * check-command: test-linearize -Wno-decl $file + * check-known-to-fail + * + * check-output-ignore + * check-output-returns: 1 + */ -- cgit 1.2.3-korg From 6f00b4edb2fb746b924db536519e31199eb57908 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 3 Jan 2021 23:19:38 +0100 Subject: simplify (x & M) cmps C Signed-off-by: Luc Van Oostenryck --- simplify.c | 25 +++++++++++++++++++++++++ validation/optim/cmps-and0.c | 1 - 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index 207af8ed..90b0c5ba 100644 --- a/simplify.c +++ b/simplify.c @@ -1258,6 +1258,31 @@ static int simplify_compare_constant(struct instruction *insn, long long value) src2 = insn->src2; value = src2->value; switch (DEF_OPCODE(def, src1)) { + case OP_AND: + if (!constant(def->src2)) + break; + bits = def->src2->value; + switch (insn->opcode) { + case OP_SET_LE: + value = sign_extend(value, def->size); + if (bits & sign_bit(def->size)) + break; + if (value < 0) + return replace_with_value(insn, 0); + if (value >= (long long)bits) + return replace_with_value(insn, 1); + break; + case OP_SET_GT: + value = sign_extend(value, def->size); + if (bits & sign_bit(def->size)) + break; + if (value < 0) + return replace_with_value(insn, 1); + if (value >= (long long)bits) + return replace_with_value(insn, 0); + break; + } + break; case OP_SEXT: // sext(x) cmp C --> x cmp trunc(C) osize = def->orig_type->bit_size; if (is_signed_constant(value, osize, size)) { diff --git a/validation/optim/cmps-and0.c b/validation/optim/cmps-and0.c index 097ec2f9..962fbd2d 100644 --- a/validation/optim/cmps-and0.c +++ b/validation/optim/cmps-and0.c @@ -15,7 +15,6 @@ int cmps_and_ge_gtm(int a) { return ((a & MASK) >= (MASK + 1)) + 1; } /* * check-name: cmps-and0 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1 -- cgit 1.2.3-korg From 3289d1c73b10b46bab66ac02096e911c3b5168d7 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 3 Jan 2021 23:56:28 +0100 Subject: simplify (x & M) cmpu C Signed-off-by: Luc Van Oostenryck --- simplify.c | 16 ++++++++++++++++ validation/optim/cmpu-and0.c | 1 - 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index 90b0c5ba..ee29c961 100644 --- a/simplify.c +++ b/simplify.c @@ -1281,6 +1281,22 @@ static int simplify_compare_constant(struct instruction *insn, long long value) if (value >= (long long)bits) return replace_with_value(insn, 0); break; + case OP_SET_B: + if (value > bits) + return replace_with_value(insn, 1); + break; + case OP_SET_BE: + if (value >= bits) + return replace_with_value(insn, 1); + break; + case OP_SET_AE: + if (value > bits) + return replace_with_value(insn, 0); + break; + case OP_SET_A: + if (value >= bits) + return replace_with_value(insn, 0); + break; } break; case OP_SEXT: // sext(x) cmp C --> x cmp trunc(C) diff --git a/validation/optim/cmpu-and0.c b/validation/optim/cmpu-and0.c index 25321294..927b9fb6 100644 --- a/validation/optim/cmpu-and0.c +++ b/validation/optim/cmpu-and0.c @@ -11,7 +11,6 @@ int cmps_and_gtu_eq(int a) { return ((a & MASK) > (MASK + 0)) + 1; } /* * check-name: cmpu-and0 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1 -- cgit 1.2.3-korg From e5ac481d66869dae85367311056a613ed0fcdb00 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 3 Jan 2021 23:40:45 +0100 Subject: simplify (x & M) cmps 0 Signed-off-by: Luc Van Oostenryck --- simplify.c | 4 ++++ validation/optim/cmps0-and0.c | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index ee29c961..6c11018e 100644 --- a/simplify.c +++ b/simplify.c @@ -1271,6 +1271,8 @@ static int simplify_compare_constant(struct instruction *insn, long long value) return replace_with_value(insn, 0); if (value >= (long long)bits) return replace_with_value(insn, 1); + if (value == 0) + return replace_opcode(insn, OP_SET_EQ); break; case OP_SET_GT: value = sign_extend(value, def->size); @@ -1280,6 +1282,8 @@ static int simplify_compare_constant(struct instruction *insn, long long value) return replace_with_value(insn, 1); if (value >= (long long)bits) return replace_with_value(insn, 0); + if (value == 0) + return replace_opcode(insn, OP_SET_NE); break; case OP_SET_B: if (value > bits) diff --git a/validation/optim/cmps0-and0.c b/validation/optim/cmps0-and0.c index 819a1dc2..8316916a 100644 --- a/validation/optim/cmps0-and0.c +++ b/validation/optim/cmps0-and0.c @@ -6,7 +6,6 @@ int cmps_and_sgt0(int a) { return ((a & M) > 0) == ((a & M) != 0); } /* * check-name: cmps0-and * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1 -- cgit 1.2.3-korg From cbafd33d81b31159929cc484a908868f2b6cc5bc Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Mon, 4 Jan 2021 00:20:06 +0100 Subject: simplify (x & M) {==,!=} C Signed-off-by: Luc Van Oostenryck --- simplify.c | 8 ++++++++ validation/optim/cmpe-and0.c | 1 - 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index 6c11018e..b46e08c3 100644 --- a/simplify.c +++ b/simplify.c @@ -1263,6 +1263,14 @@ static int simplify_compare_constant(struct instruction *insn, long long value) break; bits = def->src2->value; switch (insn->opcode) { + case OP_SET_EQ: + if ((value & bits) != value) + return replace_with_value(insn, 0); + break; + case OP_SET_NE: + if ((value & bits) != value) + return replace_with_value(insn, 1); + break; case OP_SET_LE: value = sign_extend(value, def->size); if (bits & sign_bit(def->size)) diff --git a/validation/optim/cmpe-and0.c b/validation/optim/cmpe-and0.c index 7db60836..75af7752 100644 --- a/validation/optim/cmpe-and0.c +++ b/validation/optim/cmpe-and0.c @@ -4,7 +4,6 @@ int cmpe_and_ne(int a) { return ((a & 0xff00) != 0xff01) + 0; } /* * check-name: cmpe-and0 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1 -- cgit 1.2.3-korg From 2bfb96f9259583a798306df212799c3103ab0e34 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 31 Jan 2021 15:22:56 +0100 Subject: simplify (x | M) {==,!=} C Signed-off-by: Luc Van Oostenryck --- simplify.c | 15 +++++++++++++++ validation/optim/cmpe-or0.c | 1 - 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index b46e08c3..865fadfb 100644 --- a/simplify.c +++ b/simplify.c @@ -1311,6 +1311,21 @@ static int simplify_compare_constant(struct instruction *insn, long long value) break; } break; + case OP_OR: + if (!constant(def->src2)) + break; + bits = def->src2->value; + switch (insn->opcode) { + case OP_SET_EQ: + if ((value & bits) != bits) + return replace_with_value(insn, 0); + break; + case OP_SET_NE: + if ((value & bits) != bits) + return replace_with_value(insn, 1); + break; + } + break; case OP_SEXT: // sext(x) cmp C --> x cmp trunc(C) osize = def->orig_type->bit_size; if (is_signed_constant(value, osize, size)) { diff --git a/validation/optim/cmpe-or0.c b/validation/optim/cmpe-or0.c index bef23161..2e89d611 100644 --- a/validation/optim/cmpe-or0.c +++ b/validation/optim/cmpe-or0.c @@ -4,7 +4,6 @@ int cmp_ne(int a) { return ((a | 1) == 0) + 1; } /* * check-name: cmpe-or0 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1 -- cgit 1.2.3-korg From a0709118c62cffdecc2ec34c624884287ef3c088 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 31 Jan 2021 23:16:25 +0100 Subject: simplify (x | M) cmps C Signed-off-by: Luc Van Oostenryck --- simplify.c | 14 ++++++++++++++ validation/optim/cmps-or0.c | 1 - 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index 865fadfb..e721f9f8 100644 --- a/simplify.c +++ b/simplify.c @@ -1324,6 +1324,20 @@ static int simplify_compare_constant(struct instruction *insn, long long value) if ((value & bits) != bits) return replace_with_value(insn, 1); break; + case OP_SET_LE: + value = sign_extend(value, def->size); + if (bits & sign_bit(def->size)) { + if (value >= -1) + return replace_with_value(insn, 1); + } + break; + case OP_SET_GT: + value = sign_extend(value, def->size); + if (bits & sign_bit(def->size)) { + if (value >= -1) + return replace_with_value(insn, 0); + } + break; } break; case OP_SEXT: // sext(x) cmp C --> x cmp trunc(C) diff --git a/validation/optim/cmps-or0.c b/validation/optim/cmps-or0.c index 40a2092c..70fcb024 100644 --- a/validation/optim/cmps-or0.c +++ b/validation/optim/cmps-or0.c @@ -15,7 +15,6 @@ int cmps_ior_gt_0(int a) { return ((a | MASK) > 0) EQ(0); } /* * check-name: cmps-or0 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1 -- cgit 1.2.3-korg From eb4cdd21b7d0cedbbeff7f70e24473706ccce5a6 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 31 Jan 2021 23:17:24 +0100 Subject: simplify (x | M) cmpu C Signed-off-by: Luc Van Oostenryck --- simplify.c | 16 ++++++++++++++++ validation/optim/cmpu-or0.c | 1 - 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/simplify.c b/simplify.c index e721f9f8..9e3514d8 100644 --- a/simplify.c +++ b/simplify.c @@ -1324,6 +1324,22 @@ static int simplify_compare_constant(struct instruction *insn, long long value) if ((value & bits) != bits) return replace_with_value(insn, 1); break; + case OP_SET_B: + if (bits >= value) + return replace_with_value(insn, 0); + break; + case OP_SET_BE: + if (bits > value) + return replace_with_value(insn, 0); + break; + case OP_SET_AE: + if (bits > value) + return replace_with_value(insn, 1); + break; + case OP_SET_A: + if (bits >= value) + return replace_with_value(insn, 1); + break; case OP_SET_LE: value = sign_extend(value, def->size); if (bits & sign_bit(def->size)) { diff --git a/validation/optim/cmpu-or0.c b/validation/optim/cmpu-or0.c index 77360d3f..e97e9180 100644 --- a/validation/optim/cmpu-or0.c +++ b/validation/optim/cmpu-or0.c @@ -12,7 +12,6 @@ int cmpu_ior_gt_lt(int a) { return ((a | MASK) > (MASK - 1)) EQ(1); } /* * check-name: cmpu-or0 * check-command: test-linearize -Wno-decl $file - * check-known-to-fail * * check-output-ignore * check-output-returns: 1 -- cgit 1.2.3-korg