I'm currently testing Visual C++ 10 on some trivial pieces of code. For example, like this one (taken from here):
int main()
{
int i;
clrscr();
for(i=0,i++,i<=5;i++,i<=2;i=0,i<=5,i+=3){
printf("%d ",i);
}
return 0;
}
For the code above the compiler emits machine code that in effect does this:
clrscr();
printf("%d ",2);
return 0;
and this makes me happy. Sometimes the compiler emits really dumb code and then I file a Microsoft Connect feedback item.
I have the following concern. Is testing a compiler on trivial code worth it?
On one hand, we want real code to be compiled as good as possible, not some stupid sample snippets.
On the other hand, compiler optimization is recursive in sense that if code B follows code A and the compiler can see that code B does nothing it can eliminate code B and eliminate whatever connected A and B logically and thus optimize (and maybe eliminate) code A better. And if the compiler fails to optimize away code B it will likely not be able to optimize code A either. So every improvement of generated code matters - better code at some point can mean better code elsewhere and this is beneficial.
Is it worth testing compilers on trivial samples or are only tests on "real code" worthy?