[llvm] 1d530b9 - [InstSimplify] Don't fold phi of poison and trapping const expr (PR49839)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 12 08:32:35 PDT 2022


Author: Nikita Popov
Date: 2022-04-12T17:32:25+02:00
New Revision: 1d530b914ef16ca0e920efe9c483ea44a8c62fbb

URL: https://github.com/llvm/llvm-project/commit/1d530b914ef16ca0e920efe9c483ea44a8c62fbb
DIFF: https://github.com/llvm/llvm-project/commit/1d530b914ef16ca0e920efe9c483ea44a8c62fbb.diff

LOG: [InstSimplify] Don't fold phi of poison and trapping const expr (PR49839)

Folding this case would result in the constant expression being
executed unconditionally, which may introduce a new trap.

Fixes https://github.com/llvm/llvm-project/issues/49839.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 80fe656367faa..4efc7c3050bb4 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4817,11 +4817,18 @@ static Value *SimplifyPHINode(PHINode *PN, ArrayRef<Value *> IncomingValues,
   if (!CommonValue)
     return UndefValue::get(PN->getType());
 
-  // If we have a PHI node like phi(X, undef, X), where X is defined by some
-  // instruction, we cannot return X as the result of the PHI node unless it
-  // dominates the PHI block.
-  if (HasUndefInput)
+  if (HasUndefInput) {
+    // We cannot start executing a trapping constant expression on more control
+    // flow paths.
+    auto *CE = dyn_cast<ConstantExpr>(CommonValue);
+    if (CE && CE->canTrap())
+      return nullptr;
+
+    // If we have a PHI node like phi(X, undef, X), where X is defined by some
+    // instruction, we cannot return X as the result of the PHI node unless it
+    // dominates the PHI block.
     return valueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr;
+  }
 
   return CommonValue;
 }

diff  --git a/llvm/test/Transforms/InstSimplify/phi.ll b/llvm/test/Transforms/InstSimplify/phi.ll
index 5d2ef05c7d319..d91b555db612b 100644
--- a/llvm/test/Transforms/InstSimplify/phi.ll
+++ b/llvm/test/Transforms/InstSimplify/phi.ll
@@ -162,7 +162,8 @@ define i64 @pr49839_with_poison(i1 %c) {
 ; CHECK:       if:
 ; CHECK-NEXT:    br label [[JOIN]]
 ; CHECK:       join:
-; CHECK-NEXT:    ret i64 srem (i64 1, i64 ptrtoint (i32* @g to i64))
+; CHECK-NEXT:    [[PHI:%.*]] = phi i64 [ poison, [[IF]] ], [ srem (i64 1, i64 ptrtoint (i32* @g to i64)), [[ENTRY:%.*]] ]
+; CHECK-NEXT:    ret i64 [[PHI]]
 ;
 entry:
   br i1 %c, label %if, label %join


        


More information about the llvm-commits mailing list