[LLVMdev] code generation removes duplicated instructions
renato.golin at arm.com
Wed Jul 6 08:55:18 PDT 2011
On 6 July 2011 15:57, D S Khudia <daya.khudia at gmail.com> wrote:
> Since I am inserting a new basic block (contains printf statement and
> program exit) which is jumped upon based on the result of
> the comparison, the compiler cannot/shouldnot optimize that away by means of
> DCE or anything else.
It most certainly can, since the comparison yields always the same
result. The compiler can replace all that by a simple branch to
whatever block always executes.
> In the above example even the operands are not same and I guess compiler
> cannot be that smart at -O0. I sense something is wrong with the code
> generation for ARM.
There're no hard rules stopping any compiler to run DCE or DAG
combining/elimination or whatever when in O0 or any other level.
> What other way do you suggest for duplicating since you mentioned I
> shouldn't rely on duplication the way I am doing it?
> Thanks a lot. I really appreciate your help.
What you need is a way to make sure it won't be optimised away,
possibly even on higher O-levels. You need to add a dependency that
the compiler cannot see through (or is not smart enough at O0 to do
so, at least). Because this is a transient change, as a way to debug
other problems, you're allowed to do ugly stuff.
For example, you can get a fake offset from an argument, and guarantee
that this is always zero. Or you can add a random offset internally to
one of them, and then compare that one is exactly the offset higher
(or lower) than the other. Or you could pass %a and %b as parameters
and memcopy them in the caller.
Anything that would make it difficult for the compiler to see that
both are the same would do...
More information about the llvm-dev