[Bug tree-optimization/54505] New: RFE: Inline function tables

2012-09-06 Thread avi at redhat dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54505

 Bug #: 54505
   Summary: RFE: Inline function tables
Classification: Unclassified
   Product: gcc
   Version: unknown
Status: UNCONFIRMED
  Severity: enhancement
  Priority: P3
 Component: tree-optimization
AssignedTo: unassig...@gcc.gnu.org
ReportedBy: a...@redhat.com


It is common to see code such as

  static const int (*ftable[])(struct context *) = { 
  [V1] = f1,
  [V2] = f2,
  ...
  };

  ...

  {
  ftable[nr](context);
  }

However, this is less efficient than an equivalent switch () statement since
the calling convention must be observed.  Some registers are clobbered, others
must be set to the arguments, and the functions must have a prolog and an
epilogue.

It is easy to recognize the pattern though and convert it into an equivalent
switch:

  switch (nr) {
  case V1:
  f1(context);
  break;
  case V2:
  f2(context);
  break;
  ...
  }


[Bug tree-optimization/54505] RFE: Inline function tables

2012-09-09 Thread avi at redhat dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54505

--- Comment #4 from Avi Kivity  2012-09-09 11:12:53 UTC 
---
(In reply to comment #2)
> I don't think this transformation would always be an improvement. 

gcc should make the transformation when it improves the code (like all other
transformations).

> Had a
> developer wanted to use a switch, I'd think he/she would have used one. A
> dispatch table is much more code-size efficient compared to a switch.

gcc often transforms a switch to a dispatch table, with the difference that the
function call convention is not used.  Instead, register values are maintained
across the call, and instead of call/ret, jmp/jmp (on x86) are used.

gcc should allow the programmer to write the code in the cleanest way, and
transform it to the most performing way.  In the same way that gcc converts
some multiplies to a shift, it should convert some indirect function calls to a
non-function dispatch table.  It's inlining but on a larger scale.