https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110791
Bug ID: 110791 Summary: [12/13/14 Regression] arm: Wrong code with -Os -march=armv8.1-m.main Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: acoplan at gcc dot gnu.org Target Milestone: --- The following testcase is miscompiled since at least GCC 12 with -Os -march=armv8.1-m.main: void __attribute__ ((noinline)) foo (char *path, int *result) { char *p = path; while (p >= path && *p != '/') p--; while (p > path && p[-1] == '/') p--; if (p < path) *result = 1; } int main(void) { char path[4] = "usr"; int x = 0; foo (path + 2, &x); if (!x) __builtin_abort (); } Below is the assembly we currently generate together with comments showing how this goes wrong at runtime: foo: mov r3, r0 @ r3 <- (p = path) ldrb r2, [r3], #-1 @ r2 <- *p; p--; cmp r2, #47 it eq moveq r3, r0 @ if (r2 == 47) p <- path subs r2, r3, r0 cmp r0, r3 add r2, r2, #1 bhi .L9 @ if (path > p) goto .L9 [taken] adds r0, r0, #1 bne .L6 .L9: movs r2, #1 .L6: subs r2, r2, #1 @ r2 <- 0 [fall through from above] bne .L3 @ [not taken, r2 was #1] bcc .L4 @ [not taken] bx lr @ [return without setting *result = 1] .L3: ldrb r0, [r3, #-1]! cmp r0, #47 beq .L6 bx lr .L4: movs r3, #1 str r3, [r1] bx lr At -O2 the code is both correct and much better quality: foo: ldrb r3, [r0] @ zero_extendqisi2 cmp r3, #47 itt ne movne r3, #1 strne r3, [r1] bx lr