https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114969

            Bug ID: 114969
           Summary: [11/12/13/14/15 Regression] Missed optimization:
                    (bool)((std::max((unsigned long long) 0, (unsigned
                    long long) var_0)) | ( var_0 ?  1 : 0))  => (bool)
                    var_0
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 652023330028 at smail dot nju.edu.cn
  Target Milestone: ---

Hello, we noticed that the code below can be optimized as stated in the title
((bool)((std::max((unsigned long long) 0, (unsigned long long) var_0)) | (
var_0 ?  1 : 0))  => (bool) var_0), but gcc -O3 seems to have missed it.

This is a regression from GCC-4.8.1.


https://godbolt.org/z/zo86Gz74r

#include <algorithm>
bool result;
void func(unsigned long long var_0) {
    result = (std::max((unsigned long long) 0, (unsigned long long) var_0)) | (
var_0 ?  1 : 0);
}

GCC -O3:
  <bb 2> [local count: 1073741824]:
  # DEBUG BEGIN_STMT
  _7 = var_0_5(D) != 0;
  _8 = (long long unsigned int) _7;
  _1 = var_0_5(D) | _8;
  _2 = _1 != 0;
  result = _2;
  return;

func(unsigned long long):
        xor     eax, eax
        test    rdi, rdi
        setne   al
        or      rax, rdi
        setne   BYTE PTR result[rip]
        ret


Expected code (GCC-4.7.4):
  <bb 2>:
  # DEBUG __a => &D.9143
  # DEBUG __b => &D.9144
  D.9181_24 = var_0_2(D) != 0;
  result = D.9181_24;
  return;

func(unsigned long long):
        test    rdi, rdi
        setne   BYTE PTR result[rip]
        ret

Thank you very much for your time and effort! We look forward to hearing from
you.

Reply via email to