[llvm-bugs] [Bug 51323] New: Missed fold for if-else chain

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Aug 3 07:18:35 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51323

            Bug ID: 51323
           Summary: Missed fold for if-else chain
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: llvm at rifkin.dev
                CC: llvm-bugs at lists.llvm.org

LLVM identifier conv2 is a nop but not conv1 https://godbolt.org/z/q3Eq34EY6

int conv1(std::strong_ordering s){
  if(s==std::strong_ordering::less) return -1;
  if(s==std::strong_ordering::equal) return 0;
  if(s==std::strong_ordering::greater) return 1;
  __builtin_unreachable();
}

std::strong_ordering conv2(int i){
  switch(i){
    case -1: return std::strong_ordering::less;
    case 0: return std::strong_ordering::equal;
    case 1: return std::strong_ordering::greater;
    default: __builtin_unreachable();
  }
}

It's valid to optimize down to a nop here https://alive2.llvm.org/ce/z/HcSsp0.

Problem appears to be related to switch <-> if-chain conversion. If conv2 is
converted to an if-chain LLVM no longer identifies it as a nop.

I submitted a related bug report last week
https://bugs.llvm.org/show_bug.cgi?id=51285, though in this case LLVM does
correctly do a switch <-> if-chain conversion as is able to make further
optimizations because of it.


This is related to a GCC bug:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94566.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210803/33d3f772/attachment.html>


More information about the llvm-bugs mailing list