[llvm] 8a09875 - [InstSimplify] Do not remove insertvalue of undef into poison

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 8 02:36:07 PST 2022


Author: Nikita Popov
Date: 2022-12-08T11:35:59+01:00
New Revision: 8a09875dd1e55f1920e58b7a01f380ef35465636

URL: https://github.com/llvm/llvm-project/commit/8a09875dd1e55f1920e58b7a01f380ef35465636
DIFF: https://github.com/llvm/llvm-project/commit/8a09875dd1e55f1920e58b7a01f380ef35465636.diff

LOG: [InstSimplify] Do not remove insertvalue of undef into poison

We cannot remove an insertvalue of undef if it inserts into a
potentially poison value. The new implementation matches that of
insertelement.

See https://alive2.llvm.org/ce/z/pCcFsW for the previously
incorrect transform.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 5410a901ac6b..e2db24d55dc0 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4813,8 +4813,10 @@ static Value *simplifyInsertValueInst(Value *Agg, Value *Val,
     if (Constant *CVal = dyn_cast<Constant>(Val))
       return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
 
-  // insertvalue x, undef, n -> x
-  if (Q.isUndefValue(Val))
+  // insertvalue x, poison, n -> x
+  // insertvalue x, undef, n -> x if x cannot be poison
+  if (isa<PoisonValue>(Val) ||
+      (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Agg)))
     return Agg;
 
   // insertvalue x, (extractvalue y, n), n

diff  --git a/llvm/test/Transforms/InstSimplify/insertvalue.ll b/llvm/test/Transforms/InstSimplify/insertvalue.ll
index ab235dbc5e39..d9cf8dc4e381 100644
--- a/llvm/test/Transforms/InstSimplify/insertvalue.ll
+++ b/llvm/test/Transforms/InstSimplify/insertvalue.ll
@@ -20,7 +20,8 @@ define {i32, i32} @insert_into_poison({i32, i32} %x) {
 
 define {i32, i32} @insert_undef({i32, i32} %x) {
 ; CHECK-LABEL: @insert_undef(
-; CHECK-NEXT:    ret { i32, i32 } [[X:%.*]]
+; CHECK-NEXT:    [[V:%.*]] = insertvalue { i32, i32 } [[X:%.*]], i32 undef, 0
+; CHECK-NEXT:    ret { i32, i32 } [[V]]
 ;
   %v = insertvalue {i32, i32} %x, i32 undef, 0
   ret {i32, i32} %v


        


More information about the llvm-commits mailing list