public inbox for gcc-prs@sourceware.org help / color / mirror / Atom feed
From: tm_gccmail@kloo.net To: gcc-gnats@gcc.gnu.org Subject: optimization/10050: Weak optimization on array references in conditiona blocks Date: Wed, 12 Mar 2003 21:06:00 -0000 [thread overview] Message-ID: <20030312210251.13723.qmail@sources.redhat.com> (raw) >Number: 10050 >Category: optimization >Synopsis: Weak optimization on array references in conditiona blocks >Confidential: no >Severity: serious >Priority: medium >Responsible: unassigned >State: open >Class: pessimizes-code >Submitter-Id: net >Arrival-Date: Wed Mar 12 21:06:00 UTC 2003 >Closed-Date: >Last-Modified: >Originator: Toshi Morita >Release: unknown-1.0 >Organization: >Environment: i386-linux cross sh-elf, but probably all targets >Description: Given these functions using the options -O2 -m4 -S: int flag; int func1(char *a) { int b[3]; b[0] = func(); b[1] = b[0] * 2; a[0] = b[0]; a[1] = b[1]; return a[0] + b[1] + a[1]; } int func2(int *a) { int b[3]; b[0] = func(); b[1] = b[0] * 2; if (flag) { a[0] = b[0]; a[1] = b[1]; } return a[0] + b[1] + a[1]; } int func3(int *a) { int b[3]; int temp0, temp1; b[0] = func(); b[1] = b[0] * 2; temp0 = a[0]; temp1 = a[1]; if (flag) { temp0 = b[0]; temp1 = b[1]; } a[0] = temp0; a[1] = temp1; return a[0] + b[1] + a[1]; } GCC is able to keep b[x] in registers for func1: _func1: mov.l r8,@-r15 mov r4,r8 mov.l r14,@-r15 sts.l pr,@-r15 mov.l .L2,r0 add #-12,r15 jsr @r0 mov r15,r14 mov r8,r1 mov.b r0,@r8 mov r0,r2 add #1,r1 add r2,r2 mov.b r2,@r1 add #12,r14 mov.b @r8,r0 add r2,r0 exts.b r2,r2 add r2,r0 mov r14,r15 lds.l @r15+,pr mov.l @r15+,r14 rts However, gcc generates fairly weak code for func2. It is unable to retain the values in registers and generates memory writes within the conditional block: _func2: mov.l r8,@-r15 mov r4,r8 mov.l r14,@-r15 sts.l pr,@-r15 mov.l .L7,r0 add #-12,r15 jsr @r0 mov r15,r14 mov.l .L8,r1 mov r0,r2 add r2,r2 mov.l r0,@r14 mov.l @r1,r1 tst r1,r1 bt/s .L5 mov.l r2,@(4,r14) <- (delay slot) mov.l r0,@r8 <- conditional writes mov.l r2,@(4,r8) <- conditional writes .L5: mov.l @(4,r14),r1 add #12,r14 mov.l @r8,r0 add r1,r0 mov.l @(4,r8),r1 add r1,r0 mov r14,r15 lds.l @r15+,pr mov.l @r15+,r14 rts mov.l @r15+,r8 To elicit the desired assembly output where no writes are emitted in the conditional block, it is necessary to hand-massage the C source to something like func3, which gives this code: func3: mov.l r8,@-r15 mov r4,r8 mov.l r14,@-r15 sts.l pr,@-r15 mov.l .L12,r0 add #-12,r15 jsr @r0 mov r15,r14 mov.l .L13,r1 mov r0,r7 add r7,r7 mov r0,r3 mov.l @r1,r1 mov.l r0,@r14 tst r1,r1 mov.l r7,@(4,r14) mov.l @r8,r2 bt/s .L11 mov.l @(4,r8),r0 (delay slot) mov r3,r2 <- no memory write mov r7,r0 <- no memory write .L11: mov.l r2,@r8 add r7,r2 mov.l r0,@(4,r8) add #12,r14 add r2,r0 mov r14,r15 lds.l @r15+,pr mov.l @r15+,r14 rts mov.l @r15+,r8 Toshi >How-To-Repeat: See above. >Fix: Don't know. >Release-Note: >Audit-Trail: >Unformatted:
reply other threads:[~2003-03-12 21:06 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20030312210251.13723.qmail@sources.redhat.com \ --to=tm_gccmail@kloo.net \ --cc=gcc-gnats@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: linkBe 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).