[Bug target/87913] max(n, 1) code generation
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87913 Richard Biener changed: What|Removed |Added Keywords||missed-optimization Target||x86_64-*-*, i?86-*-* Status|UNCONFIRMED |NEW Last reconfirmed||2018-11-07 Component|tree-optimization |target Ever confirmed|0 |1 --- Comment #1 from Richard Biener --- Confirmed. For f we fail to detect the max() operation at the tree level because the test is using != 0 instead of < 1. f (unsigned int num) { unsigned int iftmp.0_1; [local count: 1073741825]: if (num_2(D) != 0) goto ; [50.00%] else goto ; [50.00%] [local count: 536870913]: [local count: 1073741825]: # iftmp.0_1 = PHI return iftmp.0_1; g is represented as g (unsigned int num) { _Bool _1; unsigned int _2; unsigned int _4; [local count: 1073741825]: _1 = num_3(D) == 0; _2 = (unsigned int) _1; _4 = _2 + num_3(D); return _4; expanded to (after combine) (insn 6 3 7 2 (set (reg:CCZ 17 flags) (compare:CCZ (reg/v:SI 85 [ num ]) (const_int 0 [0]))) "/tmp/t.c":13:16 7 {*cmpsi_ccno_1} (nil)) (insn 8 7 9 2 (set (reg:SI 87) (eq:SI (reg:CCZ 17 flags) (const_int 0 [0]))) "/tmp/t.c":13:16 652 {*setcc_si_1_movzbl} (insn 9 8 14 2 (parallel [ (set (reg:SI 86) (plus:SI (reg:SI 87) (reg/v:SI 85 [ num ]))) (clobber (reg:CC 17 flags)) ]) "/tmp/t.c":13:14 190 {*addsi_1} that's a target missed optimization. I'll fix the MIN/MAX detection and thus make this issue a target one.
[Bug target/87913] max(n, 1) code generation
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87913 --- Comment #2 from krux --- The case of function g is quite interesting because of the data dependencies and adc's latency: https://godbolt.org/z/0V8Dlx
[Bug target/87913] max(n, 1) code generation
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87913 --- Comment #3 from Richard Biener --- A fix for the MIN/MAX recognition generates f: .LFB0: .cfi_startproc testl %edi, %edi movl$1, %eax cmovne %edi, %eax ret it doesn't change g().
[Bug target/87913] max(n, 1) code generation
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87913 --- Comment #4 from Richard Biener --- phiopt issue fixed, target issue (not using adc) remains.
[Bug target/87913] max(n, 1) code generation
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87913 --- Comment #5 from Richard Biener --- Author: rguenth Date: Thu Nov 8 08:03:12 2018 New Revision: 265909 URL: https://gcc.gnu.org/viewcvs?rev=265909&root=gcc&view=rev Log: 2018-11-08 Richard Biener PR tree-optimization/87913 * tree-ssa-phiopt.c (minmax_replacement): Turn EQ/NE compares of extreme values to ordered comparisons. * gcc.dg/tree-ssa/phi-opt-20.c: New testcase. Added: trunk/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-20.c Modified: trunk/gcc/ChangeLog trunk/gcc/testsuite/ChangeLog trunk/gcc/tree-ssa-phiopt.c
[Bug target/87913] max(n, 1) code generation
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87913 Andrew Pinski changed: What|Removed |Added Status|NEW |RESOLVED Resolution|--- |FIXED Target Milestone|--- |10.0 --- Comment #6 from Andrew Pinski --- G was fixed in GCC 10 by r10-4004-g15643a0dfc60e8 (aka PR 92140): cmpl$1, %edi movl%edi, %eax adcl$0, %eax That was all that was left to fix here so closing as such.
[Bug target/87913] max(n, 1) code generation
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87913 --- Comment #7 from CVS Commits --- The trunk branch has been updated by Andrew Pinski : https://gcc.gnu.org/g:45466eecf5ef669164c0922e5be8fd288b144886 commit r14-1412-g45466eecf5ef669164c0922e5be8fd288b144886 Author: Andrew Pinski Date: Tue May 16 14:26:41 2023 -0700 Add a != MIN/MAX_VALUE_CST ? CST-+1 : a to minmax_from_comparison This patch adds the support for match that was implemented for PR 87913 in phiopt. It implements it by adding support to minmax_from_comparison for the check. It uses the range information if available which allows to produce MIN/MAX expression when comparing against the lower/upper bound of the range instead of lower/upper of the type. minmax-20.c is the new testcase which tests the ranges part. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * fold-const.cc (minmax_from_comparison): Add support for NE_EXPR. * match.pd ((cond (cmp (convert1? x) c1) (convert2? x) c2) pattern): Add ne as a possible cmp. ((a CMP b) ? minmax : minmax pattern): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/minmax-22.c: New test.