[llvm] 92fc4b4 - [InstCombine] Preserve poison in bitcast of insertelement fold

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 19 04:06:11 PST 2023


Author: Nikita Popov
Date: 2023-12-19T13:06:04+01:00
New Revision: 92fc4b482f58cb243d982d572adb7a80ceab6945

URL: https://github.com/llvm/llvm-project/commit/92fc4b482f58cb243d982d572adb7a80ceab6945
DIFF: https://github.com/llvm/llvm-project/commit/92fc4b482f58cb243d982d572adb7a80ceab6945.diff

LOG: [InstCombine] Preserve poison in bitcast of insertelement fold

If the base was poison, retain the poison value.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
    llvm/test/Transforms/InstCombine/bitcast-vec-canon-inseltpoison.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
index 735fff1f4bee95..18ab510aae7f21 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -1633,7 +1633,8 @@ Instruction *InstCombinerImpl::visitInsertElementInst(InsertElementInst &IE) {
     //   bitcast (inselt undef, ScalarSrc, IdxOp)
     Type *ScalarTy = ScalarSrc->getType();
     Type *VecTy = VectorType::get(ScalarTy, IE.getType()->getElementCount());
-    UndefValue *NewUndef = UndefValue::get(VecTy);
+    Constant *NewUndef = isa<PoisonValue>(VecOp) ? PoisonValue::get(VecTy)
+                                                 : UndefValue::get(VecTy);
     Value *NewInsElt = Builder.CreateInsertElement(NewUndef, ScalarSrc, IdxOp);
     return new BitCastInst(NewInsElt, IE.getType());
   }

diff  --git a/llvm/test/Transforms/InstCombine/bitcast-vec-canon-inseltpoison.ll b/llvm/test/Transforms/InstCombine/bitcast-vec-canon-inseltpoison.ll
index 89f0c3f96349d5..38a7391a1a1e37 100644
--- a/llvm/test/Transforms/InstCombine/bitcast-vec-canon-inseltpoison.ll
+++ b/llvm/test/Transforms/InstCombine/bitcast-vec-canon-inseltpoison.ll
@@ -74,7 +74,7 @@ entry:
 
 define <3 x i64> @bitcast_inselt_undef(double %x, i32 %idx) {
 ; CHECK-LABEL: @bitcast_inselt_undef(
-; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <3 x double> undef, double [[X:%.*]], i32 [[IDX:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <3 x double> poison, double [[X:%.*]], i32 [[IDX:%.*]]
 ; CHECK-NEXT:    [[I:%.*]] = bitcast <3 x double> [[TMP1]] to <3 x i64>
 ; CHECK-NEXT:    ret <3 x i64> [[I]]
 ;
@@ -87,7 +87,7 @@ define <3 x i64> @bitcast_inselt_undef(double %x, i32 %idx) {
 
 define <3 x float> @bitcast_inselt_undef_fp(i32 %x, i567 %idx) {
 ; CHECK-LABEL: @bitcast_inselt_undef_fp(
-; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <3 x i32> undef, i32 [[X:%.*]], i567 [[IDX:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <3 x i32> poison, i32 [[X:%.*]], i567 [[IDX:%.*]]
 ; CHECK-NEXT:    [[I:%.*]] = bitcast <3 x i32> [[TMP1]] to <3 x float>
 ; CHECK-NEXT:    ret <3 x float> [[I]]
 ;
@@ -98,7 +98,7 @@ define <3 x float> @bitcast_inselt_undef_fp(i32 %x, i567 %idx) {
 
 define <vscale x 3 x float> @bitcast_inselt_undef_vscale(i32 %x, i567 %idx) {
 ; CHECK-LABEL: @bitcast_inselt_undef_vscale(
-; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <vscale x 3 x i32> undef, i32 [[X:%.*]], i567 [[IDX:%.*]]
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <vscale x 3 x i32> poison, i32 [[X:%.*]], i567 [[IDX:%.*]]
 ; CHECK-NEXT:    [[I:%.*]] = bitcast <vscale x 3 x i32> [[TMP1]] to <vscale x 3 x float>
 ; CHECK-NEXT:    ret <vscale x 3 x float> [[I]]
 ;


        


More information about the llvm-commits mailing list