public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "niemayer at isg dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug optimization/14824] New: removing constant assignments from loops not done even for simple conditional expressions
Date: Fri, 02 Apr 2004 15:04:00 -0000	[thread overview]
Message-ID: <20040402150422.14824.niemayer@isg.de> (raw)

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


             reply	other threads:[~2004-04-02 15:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-02 15:04 niemayer at isg dot de [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040402150422.14824.niemayer@isg.de \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).