If the code is unreachable, such code is easy to be removed, and should be removed. If it is reachable, that is bit difficult to tell. Now that C std already give compiler such permit, perhaps we don't have to keep in sync with gcc. Otherwise, it is very difficult to delete dead non-countable loop. One might argue, do you see many dead non-countable loops? I don't know the answer, but in the case I'm working on the loop was not dead at beginning, but after I recognize some idiom and remove some statements out of the loop. Then the loop becomes dead. I hope DCE to get rid of it. On 11/14/12 9:53 AM, Krzysztof Parzyszek wrote:> On 11/14/2012 1:23 AM, Shuxin Yang wrote: >> >> Is it legal to delete empty infinite loop like this : "while(1) >> {}"? > > If the loop was reachable, the program will not terminate. If you > delete the loop, where is the execution going to go after this point? > You can't arbitrarily insert a branch to somewhere or instructions to > execute. If the loop was unreachable, then it doesn't matter if you > delete it or not unless you want to reduce code size, but regardless > of that, if you can prove that it's unreachable then you can delete it > as such and then it doesn't matter what it does. > > -Krzysztof > > >
On 11/14/2012 12:07 PM, Shuxin Yang wrote:> > If it is reachable, that is bit difficult to tell. Now that C std > already give compiler such permit, perhaps we don't have to keep> in sync with gcc. From the C standard (well, draft): ---- 6.8.5 Iteration statements [...] An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a "for" statement) its expression-3, may be assumed by the implementation to terminate. ---- This still doesn't allow you to remove "while(1) {}", since the controlling expression *is* a constant expression. -Krzysztof -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
Thank you all for the input. Seems that I have to prove a non-countable loop is finite before I can delete it. BTW, I think this draft is not clear as to what is "constant expr". The source code may use symbolic value, but later on the symbolic value could be proved to be constant. However, compiler could mistakenly delete the loop before that point. Thank you all for the input again! Shuxin On 11/14/12 11:01 AM, Krzysztof Parzyszek wrote:> On 11/14/2012 12:07 PM, Shuxin Yang wrote: >> >> If it is reachable, that is bit difficult to tell. Now that C std >> already give compiler such permit, perhaps we don't have to keep > > in sync with gcc. > > From the C standard (well, draft): > > ---- > 6.8.5 Iteration statements > [...] > > An iteration statement whose controlling expression is not a constant > expression, that performs no input/output operations, does not access > volatile objects, and performs no synchronization or atomic operations > in its body, controlling expression, or (in the case of a "for" > statement) its expression-3, may be assumed by the implementation to > terminate. > ---- > > This still doesn't allow you to remove "while(1) {}", since the > controlling expression *is* a constant expression. > > > -Krzysztof > >