[llvm] [DAGCombiner] Freeze maybe poison operands when folding select to logic (PR #84924)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 28 22:47:04 PDT 2024


================
@@ -295,22 +295,28 @@ define float @select_icmp_sle(i32 %x, i32 %y, float %a, float %b) {
 ; Test peephole optimizations for select.
 define zeroext i1 @select_opt1(i1 zeroext %c, i1 zeroext %a) {
 ; CHECK-LABEL: select_opt1
-; CHECK:       orr {{w[0-9]+}}, w0, w1
+; SISEL:       orr [[REG:w[0-9]+]], w0, w1
+; SISEL:       and w0, [[REG]], #0x1
+; FISEL:       orr {{w[0-9]+}}, w0, w1
   %1 = select i1 %c, i1 true, i1 %a
   ret i1 %1
 }
 
 define zeroext i1 @select_opt2(i1 zeroext %c, i1 zeroext %a) {
 ; CHECK-LABEL: select_opt2
-; CHECK:       eor [[REG:w[0-9]+]], w0, #0x1
-; CHECK:       orr {{w[0-9]+}}, [[REG]], w1
+; SISEL:       orn [[REG:w[0-9]+]], w1, w0
+; SISEL:       and w0, [[REG]], #0x1
+; FISEL:       eor [[REG:w[0-9]+]], w0, #0x1
+; FISEL:       orr {{w[0-9]+}}, [[REG]], w1
   %1 = select i1 %c, i1 %a, i1 true
   ret i1 %1
 }
 
 define zeroext i1 @select_opt3(i1 zeroext %c, i1 zeroext %a) {
 ; CHECK-LABEL: select_opt3
-; CHECK:       bic {{w[0-9]+}}, w1, w0
----------------
nikic wrote:

> OTOH, considering that #66603 still is open. Maybe the revert in [8505c3b](https://github.com/llvm/llvm-project/commit/8505c3b15bfc535ff6624e71add4082680745187) wasn't needed after all?

The issue is still open due to an optimization problem -- the revert was needed to fix a correctness problem.

> Another idea would be to add some kind of boolean parameter to AssertSext/AssertZext that makes it possible to indicate that the assert node only propagates posion or if it can create poison. This flag could then be used in situations like PromoteIntRes_FP_TO_XINT.

I think to make this actually useful the statement would have to be that violating the AssertZext with the flag is immediate UB (including the case where the input is poison).

I do think that something in this direction would make sense.

Something I'm unsure about is to what degree nodes with IUB in SDAG are safe without a chain operand -- we have known miscompiles from the fact that division by zero UB is not modeled via chain.

https://github.com/llvm/llvm-project/pull/84924


More information about the llvm-commits mailing list