From: Alexei Starovoitov clang compiles 'for (i = zero; i < 17 && can_loop; i++)' in test_buddy_alignment() of libarena into w1 = w6 w6 += 1 if w1 < 16 goto ... Scalar evolution describes the value of a register as an expression. (linear r6 1) is a counter that starts at r6 and grows by 1 in each iteration. A 32-bit insn clears the upper half of its result, and zext32 shows that in the expression. the 32-bit counter w6 (zext32 (linear r6 1)) its 32-bit copy w1 (zext32 (zext32 (linear r6 1))) The latch (the jump back to the loop head) tests the copy. Scalar evolution recognizes a counter inside one zext32 only. Thus it cannot calculate how many times the loop runs: loop header at 1, non-linear latch at 3 The outer zext32 does not change the value. Add the rule: (zext32 (zext32 x)) -> (zext32 x) Signed-off-by: Alexei Starovoitov --- kernel/bpf/scev.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/bpf/scev.c b/kernel/bpf/scev.c index 5c2150928ff3..d0ba709694db 100644 --- a/kernel/bpf/scev.c +++ b/kernel/bpf/scev.c @@ -1507,6 +1507,10 @@ static int simplify(struct scev *scev, u32 id, void *priv) if (is_zext32(scev, id, &l) && is_imm(scev, l, &imm1)) return imm_expr(scev, (u64)(u32)imm1); + /* (zext32 (zext32 x)) -> (zext32 x) */ + if (is_zext32(scev, id, &l) && is_zext32(scev, l, &a)) + return l; + /* * (zext32 (+ (zext32 $a) $b)) -> (zext32 (+ $a $b)) * (zext32 (+ $a (zext32 $b))) -> (zext32 (+ $a $b)) -- 2.55.0