https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100511
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jiangning Liu from comment #2)
> Then why gcc can't optimize this case either? sizeof (XX) <> sizeof(g) here.
The second case is a VRP issue which really should be filed seperately.
-O3 case is better than -O2 for sure.
ldr w2, [x1]
cmp w2, w0
beq L6
...
ret
L6:
mov w1, 0
.p2align 3,,7
L3:
add w1, w1, 1
cmp w0, w1
beq L12 ; loop exit 1
cmp w2, w1
bge L3; should be always true
; loop exit 2/print unreachable
-O2 seems like a mess:
L6:
mov w4, w0
add w0, w0, 1
mov w3, 1
cmp w1, w0
beq L16; loop exit
L5:
cmp w2, w1
bne L6 ; loop back
cmp w2, w0
blt L17; loop exit
add w0, w0, 1
mov w4, 2
mov w3, 1
cmp w1, w0
bne L5 ; loop back
; loop exit
; print
Note in either cases, clang 12 does not remove the condition.