Missed optimization: InstCombine is blocked by other uses
The following reduced IR is derived from https://github.com/Kitware/CMake/blob/bbaa25bca7ef2a969c034a0c3c5cd47a6037b56f/Utilities/cmcurl/lib/ftp.c#L1386
Godbolt: https://godbolt.org/z/1ojq9Ynd4 alive2 proof: https://alive2.llvm.org/ce/z/VWCrxT
missed optimization: tail call void @use(i64 %3) --> tail call void @use(i64 0)
InstCombine can optimize as expected when %3 is not used in block common.ret.
declare void @use(i64)
define i64 @ftp_state_list(i64 %0, i64 %1) local_unnamed_addr {
%3 = sub i64 %0, %1
%4 = icmp eq i64 %0, %1
br i1 %4, label %5, label %common.ret
common.ret: ; preds = %2, %5
ret i64 %3
5: ; preds = %2
tail call void @use(i64 %3)
br label %common.ret
}
expected:
define i64 @tgt(i64 %0, i64 %1) local_unnamed_addr {
%3 = sub i64 %0, %1
%4 = icmp eq i64 %0, %1
br i1 %4, label %5, label %common.ret
common.ret: ; preds = %2, %5
ret i64 %3
5: ; preds = %2
tail call void @use(i64 0)
br label %common.ret
}
Is this issue still open? I would like to try.
@nikic @dtcxzyw
I expect without the extra use in common.ret the sub gets sunk, and then the dominating condition is used to fold it to 0. I'm not sure where the best place to address this is in case where sinking is not possible.
@nikic So instcombine is not the one where the code should be changed? Are there any other places I can make changes?