[llvm] [InstCombine] Allow freezing multiple out-of-loop values (PR #155638)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 9 06:13:42 PDT 2025
nikic wrote:
> > Can you please add a test where the two start values come from the same predecessor (e.g. dummy br with both successors equal)? You need to be careful about that because the phi needs to have identical values for the same predecessor.
> > > Perhaps I'm misunderstanding, but that's not legal IR? https://godbolt.org/z/8MYM1Yf5b
>
> Apologies I think I understand your concern now. In this example: https://godbolt.org/z/W5e3rfx1E we want to avoid:
>
> ```
> define void @fold_phi_multiple_identical_start_values(i1 %c, i32 %init, i32 %n) {
> entry:
> %init.fr.0 = freeze i32 %init
> %init.fr.1 = freeze i32 %init
> br i1 %c, label %loop, label %loop
>
> loop:
> %i = phi i32 [ %init.fr.0, %entry ], [ %init.fr.1, %entry ], [ %i.next, %loop ]
> %i.next = add nsw nuw i32 %i, 1
> %cond = icmp eq i32 %i.next, %n
> br i1 %cond, label %loop, label %exit
>
> exit:
> ret void
> }
> ```
>
> the final IR is correct with `%init.fr` for both `%entry` incoming values for this.
Yes, exactly. I think it ends up being correct by accident because we later CSE the two freezes. You may see intermediate invalid IR using `-debug-counter`.
> > By the way, this change isn't actually what I had in mind... I'd expect multiple starting values to get canonicalized away by loop simplify form.
>
> You're right, if the multiple starting values are well-defined the freeze can be removed with a combination of loop-simplify and instcombine: https://godbolt.org/z/dh7jq5qov
>
> > I think the more interesting extension here is the case where you need to freeze both the start value and the step value. So something like {start,+,step} where both values are not known non-poison.
>
> Seems this is already handled: https://godbolt.org/z/9oa4YvnYz
The case I had in mind is this one: https://godbolt.org/z/cczGhW8K9 Where we'd freeze both init and step.
https://github.com/llvm/llvm-project/pull/155638
More information about the llvm-commits
mailing list