llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Missed optimization: InstCombine is blocked by other uses

Open AAAA-I opened this issue 7 months ago • 3 comments

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
}

AAAA-I avatar Jun 11 '25 06:06 AAAA-I

Is this issue still open? I would like to try.

arrowten avatar Dec 17 '25 03:12 arrowten

@nikic @dtcxzyw

arrowten avatar Dec 17 '25 04:12 arrowten

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 avatar Dec 17 '25 11:12 nikic

@nikic So instcombine is not the one where the code should be changed? Are there any other places I can make changes?

arrowten avatar Dec 18 '25 16:12 arrowten