Bug 43529 - G++ doesn't optimize away empty loop when index is a double
Summary: G++ doesn't optimize away empty loop when index is a double
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.4.1
: P3 enhancement
Target Milestone: 10.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2010-03-25 23:42 UTC by Etienne Vouga
Modified: 2023-08-08 01:38 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-03-26 11:45:52


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Etienne Vouga 2010-03-25 23:42:15 UTC
Empty loops where the loop index is an integer are optimized away, but the following loop is not (presumably because it is more difficult to prove that it terminates in finite time?)

int main()
{
        for(double i=0; i<1e9; i+=1);
}

Command line: g++ -O3
Comment 1 Richard Biener 2010-03-26 11:45:52 UTC
Confirmed.
Comment 2 Changpeng Fang 2010-06-04 23:15:16 UTC
Interesting! What's the difference between 17 and 18?
------------
int main()
{
  double i;
  for(i=0; i<18; i+=1); /* gcc -O3, empty loop not removed */
}
----------------
int main()
{
  double i;
  for(i=0; i<17; i+=1); /* gcc -O3, empty loop removed */
}
Comment 3 Changpeng Fang 2010-06-04 23:29:39 UTC
(In reply to comment #2)
> Interesting! What's the difference between 17 and 18?
> ------------
> int main()
> {
>   double i;
>   for(i=0; i<18; i+=1); /* gcc -O3, empty loop not removed */
> }


The funny thing occurs in gcc 4, not gcc 6:
 
	.file	"empty.c"
	.text
	.p2align 4,,15
.globl main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	xorl	%eax, %eax
	.p2align 4,,10
	.p2align 3
.L2:
	addl	$1, %eax
	cmpl	$18, %eax
	jne	.L2
	rep
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 4.4.1-4ubuntu9) 4.4.1"
	.section	.note.GNU-stack,"",@progbits

Comment 4 Andrew Pinski 2010-06-05 00:05:18 UTC
>Interesting! What's the difference between 17 and 18?

Exact representation.
Comment 5 Richard Biener 2010-06-05 10:40:13 UTC
(In reply to comment #4)
> >Interesting! What's the difference between 17 and 18?
> 
> Exact representation.

Complete unrolling.
Comment 6 Andrew Pinski 2023-08-08 01:38:43 UTC
the 19 case was fixed in GCC 4.5.0 uptill somewhere between 200 and 2000.
2000 and 1e9 is fixed in GCC 10 for C++ by ... and r10-7522-g75efe9cb1f8938 .