Optimize max/min pattern with comparison
gcc/ PR tree-optimization/96708 * match.pd: New pattern for comparing X with MAX (X, Y) or MIN (X, y). gcc/testsuite * gcc.dg/pr96708-negative.c: New test. * gcc.dg/pr96708-positive.c: New test.
This commit is contained in:
parent
db365b61c5
commit
abef36919f
10
gcc/match.pd
10
gcc/match.pd
@ -2883,6 +2883,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
|
||||
(cmp (minmax @0 INTEGER_CST@1) INTEGER_CST@2)
|
||||
(comb (cmp @0 @2) (cmp @1 @2))))
|
||||
|
||||
/* X <= MAX(X, Y) -> true
|
||||
X > MAX(X, Y) -> false
|
||||
X >= MIN(X, Y) -> true
|
||||
X < MIN(X, Y) -> false */
|
||||
(for minmax (min min max max )
|
||||
cmp (ge lt le gt )
|
||||
(simplify
|
||||
(cmp @0 (minmax:c @0 @1))
|
||||
{ constant_boolean_node (cmp == GE_EXPR || cmp == LE_EXPR, type); } ))
|
||||
|
||||
/* Undo fancy way of writing max/min or other ?: expressions,
|
||||
like a - ((a - b) & -(a < b)), in this case into (a < b) ? b : a.
|
||||
People normally use ?: and that is what we actually try to optimize. */
|
||||
|
||||
48
gcc/testsuite/gcc.dg/pr96708-negative.c
Normal file
48
gcc/testsuite/gcc.dg/pr96708-negative.c
Normal file
@ -0,0 +1,48 @@
|
||||
/* { dg-do run } */
|
||||
/* { dg-options "-O -fdump-tree-optimized" } */
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
bool __attribute__ ((noinline))
|
||||
test1 (int a, int b)
|
||||
{
|
||||
int tmp = (a < b) ? b : a;
|
||||
return tmp <= a;
|
||||
}
|
||||
|
||||
bool __attribute__ ((noinline))
|
||||
test2 (int a, int b)
|
||||
{
|
||||
int tmp = (a < b) ? b : a;
|
||||
return tmp > a;
|
||||
}
|
||||
|
||||
bool __attribute__ ((noinline))
|
||||
test3 (int a, int b)
|
||||
{
|
||||
int tmp = (a > b) ? b : a;
|
||||
return tmp >= a;
|
||||
}
|
||||
|
||||
bool __attribute__ ((noinline))
|
||||
test4 (int a, int b)
|
||||
{
|
||||
int tmp = (a > b) ? b : a;
|
||||
return tmp < a;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
if (test1 (1, 2) || !test1 (2, 1) ||
|
||||
!test2 (1, 2) || test2 (2, 1) ||
|
||||
!test3 (1, 2) || test3 (2, 1) ||
|
||||
test4 (1, 2) || !test4 (2, 1)) {
|
||||
__builtin_abort();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* { dg-final { scan-tree-dump-times "MAX_EXPR" 2 "optimized" } } */
|
||||
/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "optimized" } } */
|
||||
/* { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } } */
|
||||
/* { dg-final { scan-tree-dump-not { "return 1;" } "optimized" } } */
|
||||
48
gcc/testsuite/gcc.dg/pr96708-positive.c
Normal file
48
gcc/testsuite/gcc.dg/pr96708-positive.c
Normal file
@ -0,0 +1,48 @@
|
||||
/* { dg-do run } */
|
||||
/* { dg-options "-O -fdump-tree-optimized" } */
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
bool __attribute__ ((noinline))
|
||||
test1(int a, int b)
|
||||
{
|
||||
int tmp = (a < b) ? b : a;
|
||||
return tmp >= a;
|
||||
}
|
||||
|
||||
bool __attribute__ ((noinline))
|
||||
test2(int a, int b)
|
||||
{
|
||||
int tmp = (a < b) ? b : a;
|
||||
return tmp < a;
|
||||
}
|
||||
|
||||
bool __attribute__ ((noinline))
|
||||
test3(int a, int b)
|
||||
{
|
||||
int tmp = (a > b) ? b : a;
|
||||
return tmp <= a;
|
||||
}
|
||||
|
||||
bool __attribute__ ((noinline))
|
||||
test4(int a, int b)
|
||||
{
|
||||
int tmp = (a > b) ? b : a;
|
||||
return tmp > a;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
if (!test1 (1, 2) || !test1 (2, 1) ||
|
||||
test2 (1, 2) || test2 (2, 1) ||
|
||||
!test3 (1, 2) || !test3 (2, 1) ||
|
||||
test4 (1, 2) || test4 (2, 1)) {
|
||||
__builtin_abort();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* { dg-final { scan-tree-dump-times "return 0;" 3 "optimized" } } */
|
||||
/* { dg-final { scan-tree-dump-times "return 1;" 2 "optimized" } } */
|
||||
/* { dg-final { scan-tree-dump-not { "MAX_EXPR" } "optimized" } } */
|
||||
/* { dg-final { scan-tree-dump-not { "MIN_EXPR" } "optimized" } } */
|
||||
Loading…
Reference in New Issue
Block a user