https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106167
Bug ID: 106167 Summary: Missed optimization (memory vs register read) Product: gcc Version: 9.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: vluchits at gmail dot com Target Milestone: --- Target: sh Created attachment 53238 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53238&action=edit Test case Hello, in the provided example, this piece of C code: if (newclipbounds) { int newfloorclipx = floorclipx; int newceilingclipx = ceilingclipx; uint16_t newclip; // rewrite clipbounds if (actionbits & AC_NEWFLOOR) newfloorclipx = low; if (actionbits & AC_NEWCEILING) newceilingclipx = high; newclip = (newceilingclipx << 8) + newfloorclipx; clipbounds[x] = newclip; newclipbounds[x] = newclip; } is compiled to the following assembler code: if (newclipbounds) 16c: 54 f7 mov.l @(28,r15),r4 16e: 24 48 tst r4,r4 170: 8d 10 bt.s 194 <_R_SegLoop+0x194> 172: e0 54 mov #84,r0 if (actionbits & AC_NEWFLOOR) 174: 05 fe mov.l @(r0,r15),r5 176: 25 58 tst r5,r5 178: 8f 01 bf.s 17e <_R_SegLoop+0x17e> 17a: e0 58 mov #88,r0 floorclipx = ceilingclipx & 0x00ff; 17c: 67 a3 mov r10,r7 if (actionbits & AC_NEWCEILING) 17e: 00 fe mov.l @(r0,r15),r0 180: 20 08 tst r0,r0 182: 8f 01 bf.s 188 <_R_SegLoop+0x188> 184: 50 fb mov.l @(44,r15),r0 int newceilingclipx = ceilingclipx; 186: 66 93 mov r9,r6 newclip = (newceilingclipx << 8) + newfloorclipx; 188: 46 18 shll8 r6 18a: 37 6c add r6,r7 18c: 67 7d extu.w r7,r7 clipbounds[x] = newclip; 18e: 08 75 mov.w r7,@(r0,r8) newclipbounds[x] = newclip; The following lines are of particular interest: if (newclipbounds) 16c: 54 f7 mov.l @(28,r15),r4 16e: 24 48 tst r4,r4 ... newclipbounds[x] = newclip; 190: 50 f7 mov.l @(28,r15),r0 192: 08 75 mov.w r7,@(r0,r8) The compiler fails to notice that it's already holding the value of @(28,r15) in r4 and re-reads the value from stack instead of doing the mov r4,r0. CFLAGS: -c -std=c11 -g -m2 -mb -Os -fomit-frame-pointer -Wall -Wextra -pedantic -Wno-unused-parameter -Wimplicit-fallthrough=0 -Wno-missing-field-initializers -Wnonnull This may or may not be relevant to bug #106161