https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116548
--- Comment #2 from Georg-Johann Lay <gjl at gcc dot gnu.org> --- Created attachment 64473 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=64473&action=edit C test case 2 Here is another test case: typedef __UINT8_TYPE__ uint8_t; uint8_t func (uint8_t byte) { uint8_t x = 0; for (uint8_t bit = 0x01; bit != 0; bit <<= 1) if (byte & bit) x += 1; else x -= 1; return x; } The generated assembly with avr-gcc v16 for -Os -mmcu=avr4 -dp: func: mov r20,r24 ; 63 [c=4 l=1] movqi_insn/0 ldi r18,lo8(8) ; 75 [c=4 l=1] movqi_insn/1 ldi r19,0 ; 76 [c=4 l=1] movqi_insn/0 ldi r24,0 ; 77 [c=4 l=1] movqi_insn/0 ldi r25,lo8(1) ; 78 [c=4 l=1] movqi_insn/1 .L4: mov r21,r20 ; 59 [c=4 l=1] movqi_insn/0 and r21,r25 ; 73 [c=4 l=1] *op8.for.cczn.and/1 breq .L2 ; 74 [c=4 l=1] branch_ZN subi r24,lo8(-(1)) ; 58 [c=4 l=1] *addqi3/1 .L3: lsl r25 ; 53 [c=4 l=1] *ashlqi3/1 subi r18,1 ; 71 [c=8 l=2] *add.for.cczn.hi/0 sbci r19,0 brne .L4 ; 72 [c=4 l=1] branch_ZN /* epilogue start */ ret ; 69 [c=0 l=1] return .L2: subi r24,lo8(-(-1)) ; 57 [c=4 l=1] *addqi3/1 rjmp .L3 ; 79 [c=4 l=1] jump The loop optimizer introduces a 16-bit induction variable in r18/19 (insns 75, 71) that is actually not needed because the bit variable could be used. This is the code when -fno-tree-loop-optimize: func: mov r18,r24 ; 55 [c=4 l=1] movqi_insn/0 ldi r24,0 ; 66 [c=4 l=1] movqi_insn/0 ldi r25,lo8(1) ; 67 [c=4 l=1] movqi_insn/1 .L4: mov r19,r18 ; 51 [c=4 l=1] movqi_insn/0 and r19,r25 ; 64 [c=4 l=1] *op8.for.cczn.and/1 breq .L2 ; 65 [c=4 l=1] branch_ZN subi r24,lo8(-(1)) ; 50 [c=4 l=1] *addqi3/1 .L3: lsl r25 ; 62 [c=4 l=1] *op8.for.cczn.ashift/0 brne .L4 ; 63 [c=4 l=1] branch_ZN /* epilogue start */ ret ; 60 [c=0 l=1] return .L2: subi r24,lo8(-(-1)) ; 49 [c=4 l=1] *addqi3/1 rjmp .L3 ; 68 [c=4 l=1] jump
