simplify CMP(AND(x,M), C) and CMP(OR(x,M), C)
cover simplify CMP(AND(x,M), C) and CMP(OR(x,M), C)
This series contains simplifications of compaes between a constant and a constant AND/OR mask.
Luc Van Oostenryck (9):
change testing of signed compares against SMIN or SMAX add testcases for constant compares against AND/OR simplify (x & M) cmps C
simplify (x & M) cmpu C
simplify (x & M) cmps 0
simplify (x & M) {==,!=} C
simplify (x | M) {==,!=} C
simplify (x | M) cmps C
simplify (x | M) cmpu Csimplify.c | 98 ++++++++++++++++++++++++++++++++++ validation/optim/cmpe-and0.c | 10 ++++ validation/optim/cmpe-or0.c | 10 ++++ validation/optim/cmps-and0.c | 21 ++++++++ validation/optim/cmps-minmax.c | 8 +-- validation/optim/cmps-or0.c | 21 ++++++++ validation/optim/cmps0-and0.c | 12 +++++ validation/optim/cmpu-and0.c | 17 ++++++ validation/optim/cmpu-or0.c | 18 +++++++ 9 files changed, 211 insertions(+), 4 deletions(-) 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
Signature · 1 line
2.30.0
PATCH 1/9 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.
--- validation/optim/cmps-minmax.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
validation/optim/cmps-minmax.c +4 -4
@@ -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
Signature · 1 line
2.30.0
PATCH 2/9 add testcases for constant compares against AND/OR
In reply to 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
validation/optim/cmpe-and0.c new file +11 -0
@@ -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 +*/
validation/optim/cmpe-or0.c new file +11 -0
@@ -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 +*/
validation/optim/cmps-and0.c new file +22 -0
@@ -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 +*/
validation/optim/cmps-or0.c new file +22 -0
@@ -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 +*/
validation/optim/cmps0-and0.c new file +13 -0
@@ -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 +*/
validation/optim/cmpu-and0.c new file +18 -0
@@ -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 +*/
validation/optim/cmpu-or0.c new file +19 -0
@@ -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 +*/
Signature · 1 line
2.30.0
PATCH 3/9 simplify (x & M) cmps C
In reply to Luc Van Oostenryck--- simplify.c | 25 +++++++++++++++++++++++++ validation/optim/cmps-and0.c | 1 - 2 files changed, 25 insertions(+), 1 deletion(-)
simplify.c +25 -0
@@ -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)) {
validation/optim/cmps-and0.c +0 -1
@@ -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
Signature · 1 line
2.30.0
PATCH 4/9 simplify (x & M) cmpu C
In reply to Luc Van Oostenryck--- simplify.c | 16 ++++++++++++++++ validation/optim/cmpu-and0.c | 1 - 2 files changed, 16 insertions(+), 1 deletion(-)
simplify.c +16 -0
@@ -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)
validation/optim/cmpu-and0.c +0 -1
@@ -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
Signature · 1 line
2.30.0
PATCH 5/9 simplify (x & M) cmps 0
In reply to Luc Van Oostenryck--- simplify.c | 4 ++++ validation/optim/cmps0-and0.c | 1 - 2 files changed, 4 insertions(+), 1 deletion(-)
simplify.c +4 -0
@@ -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)
validation/optim/cmps0-and0.c +0 -1
@@ -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
Signature · 1 line
2.30.0
PATCH 6/9 simplify (x & M) {==,!=} C
In reply to Luc Van Oostenryck--- simplify.c | 8 ++++++++ validation/optim/cmpe-and0.c | 1 - 2 files changed, 8 insertions(+), 1 deletion(-)
simplify.c +8 -0
@@ -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))
validation/optim/cmpe-and0.c +0 -1
@@ -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
Signature · 1 line
2.30.0
PATCH 7/9 simplify (x | M) {==,!=} C
In reply to Luc Van Oostenryck--- simplify.c | 15 +++++++++++++++ validation/optim/cmpe-or0.c | 1 - 2 files changed, 15 insertions(+), 1 deletion(-)
simplify.c +15 -0
@@ -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)) {
validation/optim/cmpe-or0.c +0 -1
@@ -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
Signature · 1 line
2.30.0
PATCH 8/9 simplify (x | M) cmps C
In reply to Luc Van Oostenryck--- simplify.c | 14 ++++++++++++++ validation/optim/cmps-or0.c | 1 - 2 files changed, 14 insertions(+), 1 deletion(-)
simplify.c +14 -0
@@ -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)
validation/optim/cmps-or0.c +0 -1
@@ -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
Signature · 1 line
2.30.0
PATCH 9/9 simplify (x | M) cmpu C
In reply to Luc Van Oostenryck--- simplify.c | 16 ++++++++++++++++ validation/optim/cmpu-or0.c | 1 - 2 files changed, 16 insertions(+), 1 deletion(-)
simplify.c +16 -0
@@ -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)) {
validation/optim/cmpu-or0.c +0 -1
@@ -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
Signature · 1 line
2.30.0