[llvm] [SDag][ARM][RISCV] Allow lowering CTPOP into a libcall (PR #101786)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 9 13:20:11 PDT 2024


topperc wrote:

> > The problem seems to be that the popcount and branch end up in different basic blocks so the backend can't undo it. The code to undo it is in `simplifySetCCWithCTPOP` in TargetLowering.cpp but it doesn't trigger on this example. Similar to #94829
> 
> It seems so. This time it is SelectionDAGBuilder to blame, it [transforms](https://github.com/llvm/llvm-project/blob/ae6dc64ec670891cb15049277e43133d4df7fb4b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp#L2804) `or` of two `icmp`s in two branches.
> 
> ```
>   %2 = tail call range(i64 0, 65) i64 @llvm.ctpop.i64(i64 %1)
>   %tobool = icmp ugt i64 %2, 1
>   %cmp = icmp sgt i64 %0, 0
>   %or.cond = or i1 %cmp, %tobool
>   br i1 %or.cond, label %cleanup, label %if.end
> ```
> 
> The outcome is the same: the popcount becomes liveout of its basic block.

Should we expand the ctpop+icmp idiom during CodeGenPrepare?

https://github.com/llvm/llvm-project/pull/101786


More information about the llvm-commits mailing list