<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Last case of switch generated as a comparison"
   href="https://bugs.llvm.org/show_bug.cgi?id=52482">52482</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Last case of switch generated as a comparison
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>llvm@rifkin.dev
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Switch conversion fires on the following code:

void a(), b(), c(), d(), e();
void foo(int x) {
    switch(x) {
        case 0: a(); break;
        case 1: b(); break;
        case 2: c(); break;
        case 3: d(); break;
        case 4: e(); break;
        default: __builtin_unreachable();
    }
}
void bar(int x) {
    if      (x == 0) a();
    else if (x == 1) b();
    else if (x == 2) c();
    else if (x == 3) d();
    else if (x == 4) e();
    else __builtin_unreachable();
}

But bar ends up being implemented with a switch, something like:

void bar(int x) {
    if((unsigned)x > 3) {
        e();
    } else {
        switch(x) {
            case 0: a(); break;
            case 1: b(); break;
            case 2: c(); break;
            case 3: d(); break;
            default: __builtin_unreachable();
        }
    }
}

Case 4 is in its own branch rather than in the switch.

This appears to be due to SimplifyCFG transforming the last if-else in bar to

if(x == c) {
    e();
} else {
    __builtin_unreachable();
}
// into:
__builtin_assume(x == c);
e();

Then the if-else chain as a whole ends up being transformed to:

void bar(int x) {
    switch(x) {
        case 0: a(); break;
        case 1: b(); break;
        case 2: c(); break;
        case 3: d(); break;
        default:
            __builtin_assume(x == 4);
            e();
    }
}

<a href="https://godbolt.org/z/qrYb1hnh7">https://godbolt.org/z/qrYb1hnh7</a>

I'm not sure how GCC deals with this (it does transform bar() correctly), I'm
interested what the best way to address this in LLVM will be.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>