[LLVMdev] DCE and external function

Frits van Bommel fvbommel at gmail.com
Tue Oct 12 05:22:34 PDT 2010


On Tue, Oct 12, 2010 at 1:21 PM, leledumbo <leledumbo_cool at yahoo.co.id> wrote:
>
>> only if the compiler can prove that the called function has
>> no side effects (such as modifying some global variables or
>> causing the program to exit).
>
> can it prove if the function resides in a shared library?

Only if the right function attributes are added. See
http://llvm.org/docs/LangRef.html#fnattrs for a list, but
'readonly'/'readnone' and 'nounwind' will be most interesting for you
here. I guess calling abort() probably disqualifies a function for
'readonly' (and it's stronger variant 'readnone').
Currently calls to any function that has either of the readnone or
readonly attributes as well as the nounwind attribute qualify as
"trivially dead" if their results aren't used, meaning lots of
optimizations will get rid of them. (in particular, this is pretty
much the entire the job of -die and -dce, but many others will delete
them rather than change or move them if they get in the way)

As Duncan said while I was typing this, putting __attribute__((pure))
or __attribute__((const)) on the declaration will add those llvm
attributes if you're using llvm-gcc or clang to generate this llvm
assembly.


Note that there doesn't seem to be a function attribute to say "this
function won't go into an infinite loop" though, which I guess means
http://llvm.org/PR965 hasn't been fixed yet.



More information about the llvm-dev mailing list