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 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 -- 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 14/11/12 09:56, 陳韋任 (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.GCC's current policy is described in the GCC manual: * Deleting "empty" loops. Historically, GCC has not deleted "empty" loops under the assumption that the most likely reason you would put one in a program is to have a delay, so deleting them will not make real programs run any faster. However, the rationale here is that optimization of a nonempty loop cannot produce an empty one. This held for carefully written C compiled with less powerful optimizers but is not always the case for carefully written C++ or with more powerful optimizers. Thus GCC will remove operations from loops whenever it can determine those operations are not externally visible (apart from the time taken to execute them, of course). In case the loop can be proved to be finite, GCC will also remove the loop itself. Be aware of this when performing timing tests, for instance the following loop can be completely removed, provided `some_expression' can provably not change any global state. { int sum = 0; int ix; for (ix = 0; ix != 10000; ix++) sum += some_expression; } Even though `sum' is accumulated in the loop, no use is made of that summation, so the accumulation can be removed. Ciao, Duncan.
> https://www.securecoding.cert.org/confluence/display/seccode/MSC40-C.+Do+not+use+an+empty+infinite+loopInterestingly, their first example is explicitly not justified by the paragraph they quote: the controlling expression *is* a constant expression. Tim.
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 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 >
On 11/14/2012 2:22 AM, Shuxin Yang wrote:> I do some google, I cannot find the answer... > I check C std, I cannot find answer either.The C++11 standard explicitly allows compilers to assume that all loops will eventually terminate: [intro.multithread] 24: The implementation may assume that any thread will eventually do one of the following: — terminate, — make a call to a library I/O function, — access or modify a volatile object, or — perform a synchronization operation or an atomic operation. [ Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. —end note ] I don't know if C11 has a similar statement, but C99 has a statement which is ambiguous in this regard (it only refers to a requirement upon program termination). There's a blog post by John Regehr on this topic: <http://blog.regehr.org/archives/161>. -- Joshua Cranmer News submodule owner DXR coauthor
Joshua Cranmer <pidgeot18 at gmail.com> writes:> The C++11 standard explicitly allows compilers to assume that all > loops will eventually terminate: > > [intro.multithread] 24: > The implementation may assume that any thread will eventually do one > of the following: > — terminate, > — make a call to a library I/O function, > — access or modify a volatile object, or > — perform a synchronization operation or an atomic operation. > [ Note: This is intended to allow compiler transformations such as > removal of empty loops, even when > termination cannot be proven. —end note ]In embedded, single-threaded systems, sometimes you use an infinite empty loop when the purpose is to wait and handle hardware interrupts forever. Those interrupts may or may not happen. I wonder how this fits into the note above.> I don't know if C11 has a similar statement, but C99 has a statement > which is ambiguous in this regard (it only refers to a requirement > upon program termination). > > There's a blog post by John Regehr on this topic: > <http://blog.regehr.org/archives/161>.I agree with that blog post about the C++ Standard's text quoted above.