GVN very slow on a large switch statement

I find that clang is very slow compiling a C function (generated by a tool). The function contains a very large switch statement within two nested loops. From opt –time-passes, I can see that almost all the time (95%) is being spent in GVN. I cannot afford to reduce the optimization level to –O1 (which will shut off GVN) as it causes the run-time to degrade by an unacceptable factor (>2.5X). However, I would like the compile time to be better than what it is. And I suspect that it can be better. gcc 4.7.2 is able to perform much better on this code (both compile-time and run-time).

The same file compiled by gcc 4.7.2 (-O3) is about 6 times faster in compile time than clang (-O2). The run-time performance is the same. gcc –O2 compile time is even faster (10X) but is significantly worse in run-time performance than clang –O2.

One thing I noticed was that if I converted the switch from:

switch (x) { … }


extern int black_box(int); // opaque identity function

switch (black_box(x)) { … }

the compile time improves significantly.

I am using clang version 3.3.

I also noticed that there have been similar reports about GVN performance in the past. See this link: http://lists.cs.uiuc.edu/pipermail/llvmbugs/2009-June/008477.html

That was 4.5 years ago. I could not find any follow up on that thread and so I don’t know if it is still an open problem.

I am unable to share the source code containing the switch statement but I am willing to spend the time to diagnose this performance problem myself. However, I would appreciate any suggestions on how to proceed.

n Riyaz

This is a known issue, I should even have a bug somewhere under my name
for it. The related issue is memory usage, e.g. the guile example
required 8GB+ last time I tried for the main part of the scheme system.

This should really get some attention as it is becoming more and more of
a problem, also affecting other categories of software.