This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug other/16803] New: PowerPC - invariant code motion could be removed from loop.
- From: "gcc-bugzilla at gcc dot gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 28 Jul 2004 17:01:01 -0000
- Subject: [Bug other/16803] New: PowerPC - invariant code motion could be removed from loop.
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Description:
A non-optimal code sequence is illustrated. Several invariant instructions could be hoisted from a loop. A store with update form could be used. A branch on count instruction could be used. Any or all would improve the loop's performance. Duplicate using gcc 3.5 and command line:
gcc -O3 -m64 -c test.c
Testcase:
#define SOME_CONST 20
unsigned short *x;
int y;
int main ()
{
int i;
for (i = 0; i<= y+SOME_CONST; i++)
x[i] = 0;
return 0;
}
Assembly:
Currently gcc 3.5 generates the following code:
ld 5,.LC0@toc(2) -- load base of "y"
lwz 3,0(5) -- load "y"
addi 9,3,20 -- compute "y+SOME_CONST"
cmpwi 7,9,0 -- determine if loop should be entered.
blt- 7,.L2 -- branch around loop if not.
ld 7,.LC1@toc(2) -- load address of "x"
li 8,0 -- initialize "i"
li 6,0 -- load value to store.
.L4:
ld 10,0(7) -- load base of "x" - loop invariant
sldi 12,8,1 -- compute index into "x" (i * 2)
addi 0,8,1 -- increment i
extsw 8,0 -- sign extend i
sthx 6,12,10 -- store "x[i]"
lwz 4,0(5) -- load "y" - loop invariant
addi 11,4,20 -- compute "y+SOME_CONST" - loop invariant
cmpw 0,11,8 -- compare result of add with i
bge+ 0,.L4 -- loop back.
Remove the invariant instructions and use a store with update and this code improves to:
ld 5,.LC0@toc(2) -- load base of "y"
lwz 3,0(5) -- load "y"
addi 9,3,20 -- compute "y+SOME_CONST"
cmpwi 7,9,0 -- determine if loop should be entered.
blt- 7,.L7 -- branch around loop if not.
ld 7,.LC1@toc(2) -- load address of "x"
li 8,0 -- initialize "i"
li 6,0 -- load value to store.
ld 10,0(7) -- load base of "x" - loop invariant
.L5:
addi 0,8,1 -- increment i
extsw 8,0 -- sign extend i
sthu 6,2(10) -- use store with update instead of sldi/sthx
cmpw 0,9,8 -- compare result of add with i
bge+ 0,.L5 -- loop back.
This could be further improved to the following with the use of bct:
ld 5,.LC0@toc(2) -- load base of "y"
li 8,0 -- initialize "i"
lwz 3,0(5) -- load "y"
addi 9,3,20 -- compute "y+SOME_CONST"
cmpwi 7,9,0 -- determine if loop should be entered.
blt- 7,.L7 -- branch around loop if not.
ld 7,.LC1@toc(2) -- load address of "x"
li 6,0 -- load value to store.
ld 10,0(7) -- load base of "x" - loop invariant
mtctr 11 -- load count register
.L5:
sthu 6,2(10) -- use store with update instead of sldi/sthx
bdnz+ 0,.L5 -- loop back.
The loop has gone from 8 instructions and a branch to 1 instruction and a branch.
--
Summary: PowerPC - invariant code motion could be removed from
loop.
Product: gcc
Version: 3.5.0
Status: UNCONFIRMED
Severity: enhancement
Priority: P1
Component: other
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: steinmtz at us dot ibm dot com
CC: gcc-bugs at gcc dot gnu dot org,steinmtz at us dot ibm
dot com
GCC build triplet: powerpc64-linux
GCC host triplet: powerpc64-linux
GCC target triplet: powerpc64-linux
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16803