[llvm] 57f0eed - [InstSimplify] allow insertelement-with-undef fold if poison-safe

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Fri May 1 07:43:34 PDT 2020


Author: Sanjay Patel
Date: 2020-05-01T10:34:29-04:00
New Revision: 57f0eed98d02cd684c358a45f68e716179f4bfa3

URL: https://github.com/llvm/llvm-project/commit/57f0eed98d02cd684c358a45f68e716179f4bfa3
DIFF: https://github.com/llvm/llvm-project/commit/57f0eed98d02cd684c358a45f68e716179f4bfa3.diff

LOG: [InstSimplify] allow insertelement-with-undef fold if poison-safe

The more general fold was not poison-safe, so it was removed:
rG5486e00
...but it is ok to have this transform if analysis can determine
the vector contains no poison. The test shows a simple example
of that: constant integer elements are not poison.

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/insertelement.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 6eb995d8b788..7de4a0744c29 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4302,6 +4302,11 @@ Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
   if (isa<UndefValue>(Idx))
     return UndefValue::get(Vec->getType());
 
+  // If the scalar is undef, and there is no risk of propagating poison from the
+  // vector value, simplify to the vector value.
+  if (isa<UndefValue>(Val) && isGuaranteedNotToBeUndefOrPoison(Vec))
+    return Vec;
+
   // If we are extracting a value from a vector, then inserting it into the same
   // place, that's the input vector:
   // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec

diff  --git a/llvm/test/Transforms/InstSimplify/insertelement.ll b/llvm/test/Transforms/InstSimplify/insertelement.ll
index e54a03f29c9f..c0c91b26f7a3 100644
--- a/llvm/test/Transforms/InstSimplify/insertelement.ll
+++ b/llvm/test/Transforms/InstSimplify/insertelement.ll
@@ -53,10 +53,11 @@ define <4 x i32> @PR1286(<4 x i32> %A) {
   ret <4 x i32> %B
 }
 
+; Constant is not poison, so this can simplify.
+
 define <2 x i32> @undef_into_constant_vector_with_variable_index(<2 x i32> %A, i32 %Index) {
 ; CHECK-LABEL: @undef_into_constant_vector_with_variable_index(
-; CHECK-NEXT:    [[B:%.*]] = insertelement <2 x i32> <i32 42, i32 -42>, i32 undef, i32 [[INDEX:%.*]]
-; CHECK-NEXT:    ret <2 x i32> [[B]]
+; CHECK-NEXT:    ret <2 x i32> <i32 42, i32 -42>
 ;
   %B = insertelement <2 x i32> <i32 42, i32 -42>, i32 undef, i32 %Index
   ret <2 x i32> %B


        


More information about the llvm-commits mailing list