Hi, All: Is it legal to delete empty infinite loop like this : "while(1) {}"? It is interesting that both llvm and gcc keep the loop, however Open64 delete it. If it is safe to delete this loop, it would be lot easier to delete non-obvious dead loop like following as compiler doesn't need to prove if the loop in question is infinite or not. Currently llvm is not able to delete such dead loop. while (a) { a = whatever } ; // no use of <a> after this point. Thanks Shuxin
On Tue, Nov 13, 2012 at 11:23:02PM -0800, Shuxin Yang wrote:> Hi, All: > > Is it legal to delete empty infinite loop like this : "while(1) {}"? > It is interesting that both llvm and gcc keep the loop, however Open64 > delete it.I think it's illegal to delete such infinite loop, perhapes you should file a bug report to open64. That's my 2 cents. Regards, chenwj -- Wei-Ren Chen (陳韋任) Computer Systems Lab, Institute of Information Science, Academia Sinica, Taiwan (R.O.C.) Tel:886-2-2788-3799 #1667 Homepage: http://people.cs.nctu.edu.tw/~chenwj
On Wed, Nov 14, 2012 at 03:32:53PM +0800, 陳韋任 (Wei-Ren Chen) wrote:> On Tue, Nov 13, 2012 at 11:23:02PM -0800, Shuxin Yang wrote: > > Hi, All: > > > > Is it legal to delete empty infinite loop like this : "while(1) {}"? > > It is interesting that both llvm and gcc keep the loop, however Open64 > > delete it. > > I think it's illegal to delete such infinite loop, perhapes you should > file a bug report to open64. That's my 2 cents.The reason I think it's illegal is because if you remove it, then the behavior is different. One with infinite empty loop will run infinitely, the other w/o infinite empty loop will exit immediately. That's my point. Regards, chenwj -- Wei-Ren Chen (陳韋任) Computer Systems Lab, Institute of Information Science, Academia Sinica, Taiwan (R.O.C.) Tel:886-2-2788-3799 #1667 Homepage: http://people.cs.nctu.edu.tw/~chenwj
Hi Shuxin,> Is it legal to delete empty infinite loop like this : "while(1) {}"? > It is interesting that both llvm and gcc keep the loop, however Open64 delete it.this is an endless source of discussion. I suggest you read the GCC and LLVM mailing list archives if you want to find out more about it. Ciao, Duncan.> If it is safe to delete this loop, it would be lot easier to delete non-obvious > dead loop like following as compiler doesn't need to prove if the loop in question > is infinite or not. Currently llvm is not able to delete such dead loop. > > while (a) { a = whatever } ; // no use of <a> after this point. > > Thanks > > Shuxin > > > > > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
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. On 11/14/2012 12:14 AM, Duncan Sands wrote:> Hi Shuxin, > >> Is it legal to delete empty infinite loop like this : "while(1) >> {}"? >> It is interesting that both llvm and gcc keep the loop, however >> Open64 delete it. > > this is an endless source of discussion. I suggest you read the GCC > and LLVM > mailing list archives if you want to find out more about it. > > Ciao, Duncan. > >> If it is safe to delete this loop, it would be lot easier to >> delete non-obvious >> dead loop like following as compiler doesn't need to prove if the >> loop in question >> is infinite or not. Currently llvm is not able to delete such dead loop. >> >> while (a) { a = whatever } ; // no use of <a> after this point. >> >> Thanks >> >> Shuxin >> >> >> >> >> >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
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 -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
On Wed, Nov 14, 2012 at 9:53 AM, Krzysztof Parzyszek <kparzysz at codeaurora.org> 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.Indeed, replacing it with unreachable rather than simply removing it may provide other optimization opportunities.
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 Tue, Nov 13, 2012 at 11:23 PM, Shuxin Yang <shuxin.llvm at gmail.com> wrote:> Hi, All: > > Is it legal to delete empty infinite loop like this : "while(1) {}"? > It is interesting that both llvm and gcc keep the loop, however Open64 > delete it. >Well, LLVM *sometimes* keeps infinite loops. Compile this with -O2, for example: void bar(void) { for (;;) {} } void foo(void) { bar(); } Infinite loops really are special, and infinite loops containing no side effects really are especially special. People will probably say that LLVM and Open64 and other compilers have bugs here, and they're probably right. There is room for sympathy though, both because of the unique havoc infinite loops play on compiler theory, and because of how rarely empty loops are actually an advisable idiom in the real world. Dan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121114/afab49a5/attachment.html>