[llvm-bugs] [Bug 37416] New: Convert indirect switch-to-jump/call tables to direct jump/call tables

via llvm-bugs llvm-bugs at lists.llvm.org
Fri May 11 04:16:57 PDT 2018


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

            Bug ID: 37416
           Summary: Convert indirect switch-to-jump/call tables to direct
                    jump/call tables
           Product: libraries
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: dave at znu.io
                CC: llvm-bugs at lists.llvm.org

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;
}

-- 
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/20180511/45eea43c/attachment.html>


More information about the llvm-bugs mailing list