I have a possible code generation bug that I've reproduced on compilers 6.2.0, 6.2.6, and 6.2.9. I have a reproducible test case. It appears to only manifest itself with -O2. I'll try to summarize the bug the best I can. It's an odd one that had me chasing my tail for a few hours today, and it took me a little while to get to the test case.I hope my report will prove of some use.
I have some legacy code that uses an array of 16-bit words to store offsets into other arrays (realndx in the test case). This array is iterated through in a loop. The bug is that the compiler caches the pointer to realndx into an XAR register. The generated assembly uses two post-increment address modes against the XAR register which causes the next iteration of the loop to use an incorrect offset into the realndx array. In the case that caused us to notice the bug, the second array element of realndx is never modified.
Things I noticed while debugging and creating the test case:
- The bug occurs with the call to output_data(). Without the call, the generated code is fine.
- If the final term of the data array is even, the bug does not occur. When the final term is odd, the generated code is incorrect.
- The bug seems to manifest itself on -O2.
The compiler flags I have been using are included as a comment in the test case.
Best regards.