I want to get C code after going through preprocessor AND constant propagation and simple code analysis. Here is what I mean.
I use gcc’s -E option to get the code after preprocessor. However, the code I am getting is really hard to understand and a simple pass of local constant propagation would make it much easier to read. Here is an example of just one line of C code generated by the preprocessor.
(b1[0] = (kp + 1 * 4)[0] ^
( t_fn[0][(((( 0 == 0 ? ( 0 == 0 ? b0[0] : 0 == 1 ? b0[1] : 0 == 2 ? b0[2] : b0[3]) : 0 == 1 ? ( 0 == 0 ? b0[1] : 0 == 1 ? b0[2] : 0 == 2 ? b0[3] : b0[0]) : 0 == 2 ? ( 0 == 0 ? b0[2] : 0 == 1 ? b0[3] : 0 == 2 ? b0[0] : b0[1]) : ( 0 == 0 ? b0[3] : 0 == 1 ? b0[0] : 0 == 2 ? b0[1] : b0[2]))) >> (8 * ((0)))) & 0xff)]
^ t_fn[1][(((( 1 == 0 ? ( 0 == 0 ? b0[0] : 0 == 1 ? b0[1] : 0 == 2 ? b0[2] : b0[3]) : 1 == 1 ? ( 0 == 0 ? b0[1] : 0 == 1 ? b0[2] : 0 == 2 ? b0[3] : b0[0]) : 1 == 2 ? ( 0 == 0 ? b0[2] : 0 == 1 ? b0[3] : 0 == 2 ? b0[0] : b0[1]) : ( 0 == 0 ? b0[3] : 0 == 1 ? b0[0] : 0 == 2 ? b0[1] : b0[2]))) >> (8 * ((1)))) & 0xff)]
^ t_fn[2][(((( 2 == 0 ? ( 0 == 0 ? b0[0] : 0 == 1 ? b0[1] : 0 == 2 ? b0[2] : b0[3]) : 2 == 1 ? ( 0 == 0 ? b0[1] : 0 == 1 ? b0[2] : 0 == 2 ? b0[3] : b0[0]) : 2 == 2 ? ( 0 == 0 ? b0[2] : 0 == 1 ? b0[3] : 0 == 2 ? b0[0] : b0[1]) : ( 0 == 0 ? b0[3] : 0 == 1 ? b0[0] : 0 == 2 ? b0[1] : b0[2]))) >> (8 * ((2)))) & 0xff)]
^ t_fn[3][(((( 3 == 0 ? ( 0 == 0 ? b0[0] : 0 == 1 ? b0[1] : 0 == 2 ? b0[2] : b0[3]) : 3 == 1 ? ( 0 == 0 ? b0[1] : 0 == 1 ? b0[2] : 0 == 2 ? b0[3] : b0[0]) : 3 == 2 ? ( 0 == 0 ? b0[2] : 0 == 1 ? b0[3] : 0 == 2 ? b0[0] : b0[1]) : ( 0 == 0 ? b0[3] : 0 == 1 ? b0[0] : 0 == 2 ? b0[1] : b0[2]))) >> (8 * ((3)))) & 0xff)]));
pieces like 0 == 0 ? X : Y and 8*2 can be easily transformed to simpler forms. Now there is tons of such lines in my code and this is a real headache. So it would be great if I could get a simpler C code by just doing a simple local constant propagation and code analysis?
For that you would need a tool that can essentially perform code optimization at source code level. The compiler will not help you here, since compilers do not normally optimize the code at source code level. Any code transformations normally take place much later, when the source code is no longer relevant and there’s really no way back to it.
In other words, you would need a tool specifically designed to solve your problem. I’m unaware of any such tool and I doubt it exists.