Missed Optimization: -(x & c1) >> shamt | -(x & c1) —> x & c1 ? -1 : 0 when 2^shamt > c1 and shamt ≤ bitwidth(x)/2
define i64 @src(i32 %arg0) {
%1 = sub i32 0, %arg0
%2 = and i32 %1, 63
%3 = zext nneg i32 %2 to i64
%4 = sub nsw i64 0, %3
%5 = lshr i64 %4, 8
%6 = or i64 %5, %4
ret i64 %6
}
define i64 @tgt(i32 %arg0) {
%1 = and i32 %arg0, 63
%2 = icmp ne i32 %1, 0
%3 = sext i1 %2 to i64
ret i64 %3
}
Alive2: https://alive2.llvm.org/ce/z/yLQgJp Example: https://godbolt.org/z/hEKWGoeq3
Found this pattern in function bn_rshift_fixed_top in openssl/crypto/bn/sn_shift.c
I don't know how to generalize this specific fold :(
if possible I want to try my best to solve this issue.
@ZhangYudongengineer Assigned. Please check llvm.org/docs/InstCombineContributorGuide.html before making changes.
@ZhangYudongengineer Assigned. Please check llvm.org/docs/InstCombineContributorGuide.html before making changes.
ok,thx trusting me.
I don't know how to generalize this specific fold :(
@dtcxzyw Hi, I think this optimization can be generalized like this. Thanks.
"Preconditions: C1 & (C1 + 1) == 0 K = popcount(C1) K <= C2 <= BitWidth - K 0 <= val <= C1
Generalized Optimization: ((-val) >>u C2) | (-val) ==> sext(val != 0) "
Godbolt: https://godbolt.org/z/ezo66PGvj You can verify it in alive2 with timeout limit = 50000 with the following llvm ir (take bitwidth = 64 as an example):
define i64 @src(i64 %C1, i64 %C2, i64 %val) {
%W = add i64 64, 0
%C1_plus_1 = add i64 %C1, 1
%mask_check = and i64 %C1, %C1_plus_1
%is_mask = icmp eq i64 %mask_check, 0
call void @llvm.assume(i1 %is_mask)
%K = call i64 @llvm.ctpop.i64(i64 %C1)
%ge_check = icmp uge i64 %C2, %K
call void @llvm.assume(i1 %ge_check)
%diff = sub i64 %W, %K
%le_check = icmp ule i64 %C2, %diff
call void @llvm.assume(i1 %le_check)
%val_bounded = icmp ule i64 %val, %C1
call void @llvm.assume(i1 %val_bounded)
%neg = sub i64 0, %val
%shifted = lshr i64 %neg, %C2
%res = or i64 %shifted, %neg
ret i64 %res
}
define i64 @tgt(i64 %C1, i64 %C2, i64 %val) {
%nz = icmp ne i64 %val, 0
%res = sext i1 %nz to i64
ret i64 %res
}
I don't know how to generalize this specific fold :(
@dtcxzyw Hi, I think this optimization can be generalized like this. Thanks.
"Preconditions: C1 & (C1 + 1) == 0 K = popcount(C1) K <= C2 <= BitWidth - K 0 <= val <= C1
Generalized Optimization: ((-val) >>u C2) | (-val) ==> sext(val != 0) "
Godbolt: https://godbolt.org/z/ezo66PGvj You can verify it in alive2 with timeout limit = 50000 with the following llvm ir (take bitwidth = 64 as an example):
define i64 @src(i64 %C1, i64 %C2, i64 %val) { %W = add i64 64, 0 %C1_plus_1 = add i64 %C1, 1 %mask_check = and i64 %C1, %C1_plus_1 %is_mask = icmp eq i64 %mask_check, 0 call void @llvm.assume(i1 %is_mask) %K = call i64 @llvm.ctpop.i64(i64 %C1) %ge_check = icmp uge i64 %C2, %K call void @llvm.assume(i1 %ge_check) %diff = sub i64 %W, %K %le_check = icmp ule i64 %C2, %diff call void @llvm.assume(i1 %le_check) %val_bounded = icmp ule i64 %val, %C1 call void @llvm.assume(i1 %val_bounded) %neg = sub i64 0, %val %shifted = lshr i64 %neg, %C2 %res = or i64 %shifted, %neg ret i64 %res } define i64 @tgt(i64 %C1, i64 %C2, i64 %val) { %nz = icmp ne i64 %val, 0 %res = sext i1 %nz to i64 ret i64 %res }
This looks feasible. Thanks!