From: <stefan@franke.ms>
To: <gcc-help@gcc.gnu.org>
Subject: autoinc / postinc not used
Date: Tue, 16 Jan 2024 21:02:38 +0100 [thread overview]
Message-ID: <00ae01da48b6$f4e5ce80$deb16b80$@franke.ms> (raw)
[-- Attachment #1: Type: text/plain, Size: 3845 bytes --]
Hi all,
I work a lot with the good old m68k target where post-increment is supported, and I was surprised that there almost no post-increments are used in the generated code.
This simple code:
void memclr (int length, long * ptr) {
for(;length--;){
*ptr++= 0;
}
}
does not use post-increments on AVR or SH. See also https://godbolt.org/z/fTvdv65rr
On M68K a post-increment appears:
memclr:
move.l 4(%sp),%d1
move.l 8(%sp),%a0
move.l %d1,%d0
subq.l #1,%d0
tst.l %d1
jeq .L1
.L3:
clr.l (%a0)+
dbra %d0,.L3
clr.w %d0
subq.l #1,%d0
jcc .L3
.L1:
rts
If you change the code and add a 2nd statement to the loop:
void memclr (int length, long * ptr) {
for(;length--;){
*ptr++= 0;
*ptr++= 0;
}
}
the post-increment disappears:
memclr:
move.l 4(%sp),%d1
move.l 8(%sp),%a0
move.l %d1,%d0
subq.l #1,%d0
tst.l %d1
jeq .L1
.L3:
clr.l (%a0)
addq.l #8,%a0
clr.l -4(%a0)
dbra %d0,.L3
clr.w %d0
subq.l #1,%d0
jcc .L3
.L1:
rts
This is caused by several unfortunate conversions/optimizations. Here comes the first:
The GIMPLE PASS converts post-increments by creating the next pointer before the current pointer is used, which looks like
ptr.0 = ptr;
ptr = ptr.0 + 4;
*ptr.0 = 0;
ptr.1 = ptr;
ptr = ptr.1 + 4;
*ptr.1 = 0;
In the following steps this gets optimized further but in the end the addition stays always in front of the last zero assignment and ends up to become a +8. Since the +8 does not match the size also the first post-increment gets lost. And the last zero assignment is done with offset -4. That explains the generated code.
Now here comes my question:
Is there a more conforming/easier/better way to swap the generated gimple instructions than patching gimplify_modify_expr and check for assignment pairs where the pointer-add can be moved behind the memory assignment?
My hack is ugly:
gimple * p2 = gimple_seq_last_stmt(*pre_p);
if (p2->code == GIMPLE_ASSIGN && p2->prev && p2->prev != p2)
{
gimple * p1 = p2->prev;
if (p1->code == GIMPLE_ASSIGN)
{
tree b = gimple_assign_lhs(p1);
tree x1 = gimple_assign_lhs(p2);
tree x2 = gimple_assign_rhs1(p2);
if (b != x2 && (TREE_CODE(b) == VAR_DECL || TREE_CODE(x2) == VAR_DECL || TREE_CODE(b) == PARM_DECL || TREE_CODE(x2) == PARM_DECL) &&
((TREE_CODE(x1) == VAR_DECL && TREE_CODE(x2) == MEM_REF && TREE_OPERAND(x2, 0) != b
&& (TREE_CODE(TREE_OPERAND(x2, 0)) == VAR_DECL || TREE_CODE(TREE_OPERAND(x2, 0)) == PARM_DECL)) ||
(TREE_CODE(x1) == MEM_REF && (TREE_CODE(x2) == INTEGER_CST || (TREE_CODE(x2) == VAR_DECL && TREE_OPERAND(x1, 0) != b)))
&& (TREE_CODE(TREE_OPERAND(x1, 0)) == VAR_DECL || TREE_CODE(TREE_OPERAND(x1, 0)) == PARM_DECL)))
{
gimple_stmt_iterator to = gsi_last (*pre_p);
gimple_stmt_iterator from = to;
from.ptr = p1;
gsi_remove (&from, false);
gsi_insert_after (&to, p1, GSI_NEW_STMT);
}
}
}
(there are more modifications necessary to create better code, but it’s possible)
Thanks
Stefan
next reply other threads:[~2024-01-16 20:02 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-16 20:02 stefan [this message]
2024-01-16 23:52 ` Oleg Endo
2024-01-17 15:19 ` AW: " stefan
2024-01-17 23:32 ` Oleg Endo
2024-01-18 16:17 ` AW: " stefan
2024-01-30 12:22 ` Oleg Endo
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='00ae01da48b6$f4e5ce80$deb16b80$@franke.ms' \
--to=stefan@franke.ms \
--cc=gcc-help@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).