[AggressiveInstCombine] Split cttz/ctlz fold misses the half-word pattern when zero has a defined result
godbolt - https://godbolt.org/z/8r39M39ff
define i32 @src(i64 %val) {
entry:
%conv1 = trunc i64 %val to i32
%cmp = icmp eq i32 %conv1, 0
br i1 %cmp, label %if.then, label %if.end
if.then: ; preds = %entry
%shr = lshr exact i64 %val, 32
%conv = trunc nuw i64 %shr to i32
%0 = call i32 @llvm.cttz.i32(i32 %conv, i1 true)
%1 = icmp eq i64 %val, 0
%2 = or disjoint i32 %0, 32
%add = select i1 %1, i32 32, i32 %2
br label %cleanup
if.end: ; preds = %entry
%3 = call i32 @llvm.cttz.i32(i32 %conv1, i1 true)
br label %cleanup
cleanup: ; preds = %if.end, %if.then
%retval.0 = phi i32 [ %add, %if.then ], [ %3, %if.end ]
ret %relval.0
}
can be optimized to
define i32 @tgt(i64 %val) {
entry:
%cttz64 = call i64 @llvm.cttz.i64(i64 %val, i1 true)
%ret = trunc i64 %cttz64 to i32
%cmp = icmp eq i64 %val, 0
%sel = select i1 %cmp, i32 32, i32 %ret
ret i32 %sel
}
foldSelectSplitCTLZCTTZ misses folding the half-width cttz/ctlz when its defined value at zero differs from the wide intrinsic and also here the phi node is not folded to select (increasing the --two-entry-phi-node-folding-threshold=6 folds them to select) alive2 proofs cttz - https://alive2.llvm.org/ce/z/4H9gqk ctlz - https://alive2.llvm.org/ce/z/6fmbHf
source: SPEC CPU2026 777.zstd_r
@Andarwinux Hi, I'd like to take this one. I'll start with the select-form miss in foldSelectSplitCTLZCTTZ (cttz and ctlz variants) and keep the phi -> select threshold behaviour out of scope, since that looks like a separate cost-model question. Will post a PR once I have tests and Alive2 proofs for the variants.