[LLVMdev] Is infinite empty loop dead code?

Nick Lewycky nicholas at mxc.ca
Wed Nov 14 13:16:35 PST 2012

Shuxin Yang wrote:
> Hi, dear Wenren:
> Thank you so much for sharing this info. I really appreciate it.
> Now I can move on deleting dead non-countable loops.
> Thank you again!

Shuxin, please don't delete infinite loops, since it allows the code 
after them to run when it shouldn't. In C++03 and C89, it's invalid to 
do so. C++11 has text that to the effect that every thread must continue 
towards some side-effect, but then you'll need to make llvm only delete 
dead loops when you know the language permits it. In particular, 
ignoring C and C++ and just focussing on LLVM IR itself, there is 
nothing in LLVM IR that permits you to delete an empty 
potentially-infinite loop. Maybe you could write such a new optimization 
but put it in its own pass and the frontend chooses whether to run it 
based on its knowledge of the language semantics at hand.

Finite loops are always fair game.

See http://blog.regehr.org/archives/140 and 
http://blog.regehr.org/archives/161 . When that article was published we 
fixed the bug that caused us to delete infinite loops. Please don't 
re-introduce the bug.

Also, the text in C99 appears to allow us to terminate *iteration 
statements*, but does not allow us to terminate infinite recursion. So 
if llvm transforms the recursion into iteration, we need to make sure 
not to remove it. For this reason, I consider the text in C99 
broken/useless. I haven't looked at C11.


> Shuxin
> On 11/14/12 12:56 AM, 陳韋任 (Wei-Ren Chen) wrote:
>> On Wed, Nov 14, 2012 at 12:22:33AM -0800, Shuxin Yang wrote:
>>> I do some google, I cannot find the answer...
>>> I check C std, I cannot find answer either.
>>> Delete infinite empty loop is boring, but if C/C++ lawyers could tell it
>>> is safe to to so,
>>> it would obviate the need to prove a non-countable loop infinite or not
>>> before
>>> DCE can delete it.
>>> That is the answer I'm waiting for to delete a disgusting dead
>>> non-countable loop in my way.
>> Perhaps Duncan will give you a proper keyword to search in GCC/LLVM
>> ML archieve. I found a page [1], and iiuc, the C standard allow the
>> implementation (i.e., the compiler) to remove such empty infinite loop.
>> Regards,
>> chenwj
>> [1]
>> https://www.securecoding.cert.org/confluence/display/seccode/MSC40-C.+Do+not+use+an+empty+infinite+loop
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

More information about the llvm-dev mailing list