This folds (a << k) ? 2^k * a : 0 to 2^k * a.
https://alive2.llvm.org/ce/z/_dDRjo
Fix #62155.
Depends on D150069.
Differential D148420
[InstCombine] Enhance select icmp and folding peixin on Apr 15 2023, 2:17 AM. Authored by
Details This folds (a << k) ? 2^k * a : 0 to 2^k * a. https://alive2.llvm.org/ce/z/_dDRjo Fix #62155. Depends on D150069.
Diff Detail Event TimelineComment Actions As this does not create a new instruction, it should be part of InstSimplify (probably in simplifySelectWithICmpCond).
Comment Actions It looks like this should be part of simplifySelectBitTest(), which covers other "select of icmp and" patterns. Comment Actions It seems not, the CondVal in simplifySelectWithICmpCond is ICmp of value and zero. For cases in simplifySelectBitTest, the CondVal is ICmp of value and value. Comment Actions Not sure I follow. Isn't it matching the (X & C) == 0 pattern here? https://github.com/llvm/llvm-project/blob/f6ec56ac5be26ad862e22d0eebb2645d1ad78218/llvm/lib/Analysis/InstructionSimplify.cpp#L4494-L4500 Comment Actions Just took a closer look at this. This is one of those transforms where we need to be very careful about derefinement, because we're replacing a constant 0 with an instruction that (under the given condition) may refine to zero but is not necessarily equivalent to zero. In particular, if the shl has nowrap flags, this transform is incorrect: https://alive2.llvm.org/ce/z/EjBRVW This means we need to drop nowrap flags as part of the transform. I'm afraid this means that my original recommendation to move this to InstSimplify was wrong and this should be in InstCombine after all, as we can only drop nowrap flags there. Comment Actions @nikic Thanks for the notice. I was on vacation and just came back today. I will continue this work.
Comment Actions Thanks @goldstein.w.n and @nikic for the comments. Addressed them.
Comment Actions Sorry for the continued ping. I hope to finish this patch by the end of next week due to work changes. Comment Actions Implementation looks fine, but this is missing some test coverage. Can you please also add an alive2 proof to the patch description?
Comment Actions
Generating a new shl isn't wrong, but I think your previous approach was better. The reason is that the shl with and without nowrap flags are going to be GVN/CSEd anyway, and the nowrap flags will be dropped in the process. So it makes more sense to me to directly go to the final form. Losing some nowrap flags is just the cost of this transform.
Comment Actions Good to know it. Thanks for it.
Comment Actions LGTM I think the alive2 link should be more along these lines: https://alive2.llvm.org/ce/z/dEW3Ak We should check the generic transform, not specific constants. |
Why exclude vectors? Looks like they should work out of the box?