<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 - Convert indirect switch-to-jump/call tables to direct jump/call tables"
   href="https://bugs.llvm.org/show_bug.cgi?id=37416">37416</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Convert indirect switch-to-jump/call tables to direct jump/call tables
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>dave@znu.io
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In projects that avoid formal C++ virtual dispatch (like LLVM and its
subprojects), the act of virtual dispatch is often emulated via switch
statements. The code gen is worse than it could be, because what ends up being
generated is a jump-via-table to another jump/call, which could have been
collapsed down to a single jump/call table. For example, the "example" method
below demonstrates the "jump via table to another jump/call" code gen, and the
"better" method is what the compiler should have transformed the "example"
switch statement to.

int a(int);
int b(int);
int c(int);
int d(int);
int e(int);
int f(int);
int g(int);


int example(int arg, unsigned kind) {
  int val;
  switch (kind) {
  case 0: val = a(arg); break;
  case 1: val = b(arg); break;
  case 2: val = c(arg); break;
  case 3: val = d(arg); break;
  case 4: val = e(arg); break;
  case 5: val = f(arg); break;
  case 6: val = a(arg); break;
  default:
    __builtin_unreachable();
  }
  return val + 1;
}

int better(int arg, unsigned kind) {
  typedef int (*func_t)(int);
  static func_t table[] = {
    [0] = a,
    [1] = b,
    [2] = c,
    [3] = d,
    [4] = e,
    [5] = f,
    [6] = g,
  };
  val = table[kind](arg);
  return val + 1;
}</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>