public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug optimization/14824] New: removing constant assignments from loops not done even for simple conditional expressions
@ 2004-04-02 15:04 niemayer at isg dot de
  2004-04-02 15:20 ` [Bug optimization/14824] " niemayer at isg dot de
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: niemayer at isg dot de @ 2004-04-02 15:04 UTC (permalink / raw)
  To: gcc-bugs

If you compile this tiny sample:

---------------------------------------------
extern int y;

const int test(void) {
	
	int i;
	int z;
	const int x = y;
	
	asm volatile ("testlabel1: ");
	for (i = 0; i < x; i++) {
		if (x) z = 4;
		else z = 5;
	}
	asm volatile ("testlabel2: ");

	return z;	
}
------------------------------------------

with "gcc -S -O3 test.c" and have a look at the assembler output, you'll notice
that the assignment of "z = ..." is done x times, though there's absolutely no
chance for the value to change from one iteration to another:

        testlabel1: 
#NO_APP
        testl   %ecx, %ecx
        jle     .L10
        movl    %ecx, %edx
        .p2align 4,,15
.L8:
        xorl    %eax, %eax
        testl   %ecx, %ecx
        sete    %al
        addl    $4, %eax
        decl    %edx
        jne     .L8
.L10:
#APP
        testlabel2: 

It seems that this is due to the usage of the "if" there, other expressions,
even more complex ones, do not prevent the assignment from being dragged in
front of the loop. The case does not change when using "z = (x)? 4 : 5".

I tried to work around this problem by using a function declared with
__attribute__((const)) instead of the "(x)? 4 : 5" expression, but to no avail -
even such a function will be evaluated x times.

Is there any chance to let the compiler remove such an assignment from the
loop-body?

-- 
           Summary: removing constant assignments from loops not done even
                    for simple conditional expressions
           Product: gcc
           Version: 3.3.3
            Status: UNCONFIRMED
          Severity: minor
          Priority: P2
         Component: optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: niemayer at isg dot de
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-linux-gnu
  GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14824


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-05-13 11:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-02 15:04 [Bug optimization/14824] New: removing constant assignments from loops not done even for simple conditional expressions niemayer at isg dot de
2004-04-02 15:20 ` [Bug optimization/14824] " niemayer at isg dot de
2004-04-02 15:31 ` [Bug optimization/14824] inlining hurting performance due to non-removing of constant assignments inside loops containing conditionals niemayer at isg dot de
2004-04-02 16:55 ` pinskia at gcc dot gnu dot org
2004-04-02 16:57 ` pinskia at gcc dot gnu dot org
2004-05-13 20:59 ` [Bug tree-optimization/14824] " pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).