[llvm] [InstCombine] Fixing wrong merge of vector operands with undef elements (PR #102742)

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 11 14:10:44 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Jorge Botto (jf-botto)

<details>
<summary>Changes</summary>

Fixes https://github.com/llvm/llvm-project/issues/97730.
`VisitMaskedMerge` shouldn't canonicalize `((x ^ y) & ~M) ^ y` when `x` is a vector that contains `undef`.
https://github.com/llvm/llvm-project/blob/979abf142f606bf43a5500e59d72f1286a7180c7/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp#L4247-L4263


Proof: https://alive2.llvm.org/ce/z/aUmwq-



---
Full diff: https://github.com/llvm/llvm-project/pull/102742.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (+5) 
- (added) llvm/test/Transforms/InstCombine/pr97730.ll (+20) 


``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 2db05c669145b9..2eab0ca1d1c0c3 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -4257,6 +4257,11 @@ static Instruction *visitMaskedMerge(BinaryOperator &I,
 
   Value *NotM;
   if (match(M, m_Not(m_Value(NotM)))) {
+
+    if (ConstantVector *C = dyn_cast<ConstantVector>(X))
+      if (C->containsUndefElement())
+        return nullptr;
+
     // De-invert the mask and swap the value in B part.
     Value *NewA = Builder.CreateAnd(D, NotM);
     return BinaryOperator::CreateXor(NewA, X);
diff --git a/llvm/test/Transforms/InstCombine/pr97730.ll b/llvm/test/Transforms/InstCombine/pr97730.ll
new file mode 100644
index 00000000000000..f6ed0413c31f82
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/pr97730.ll
@@ -0,0 +1,20 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+define <4 x i1> @pr97730-1(<4 x i1> %val0, <4 x i1> %val1, <4 x i1> %val2) {
+; CHECK-LABEL: define <4 x i1> @pr97730-1(
+; CHECK-SAME: <4 x i1> [[VAL0:%.*]], <4 x i1> [[VAL1:%.*]], <4 x i1> [[VAL2:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[VAL3:%.*]] = xor <4 x i1> [[VAL1]], <i1 true, i1 true, i1 true, i1 true>
+; CHECK-NEXT:    [[VAL4:%.*]] = xor <4 x i1> [[VAL0]], <i1 false, i1 undef, i1 undef, i1 true>
+; CHECK-NEXT:    [[VAL5:%.*]] = and <4 x i1> [[VAL4]], [[VAL3]]
+; CHECK-NEXT:    [[VAL6:%.*]] = xor <4 x i1> [[VAL5]], [[VAL0]]
+; CHECK-NEXT:    ret <4 x i1> [[VAL6]]
+;
+entry:
+  %val3 = add <4 x i1> %val1, <i1 true, i1 true, i1 true, i1 true>
+  %val4 = xor <4 x i1> <i1 false, i1 undef, i1 undef, i1 true>, %val0
+  %val5 = and <4 x i1> %val4, %val3
+  %val6 = sub <4 x i1> %val5, %val0
+  ret <4 x i1> %val6
+}
\ No newline at end of file

``````````

</details>


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


More information about the llvm-commits mailing list