aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2022-04-12 17:31:29 +0200
committerNikita Popov <npopov@redhat.com>2022-04-12 17:32:25 +0200
commit1d530b914ef16ca0e920efe9c483ea44a8c62fbb (patch)
tree55576e608e09189f1442a01c3f9f8735fd9328a8
parentbc6d7ed8a9b9501d5d977a0825629791ee802fd6 (diff)
downloadllvm-1d530b914ef16ca0e920efe9c483ea44a8c62fbb.zip
llvm-1d530b914ef16ca0e920efe9c483ea44a8c62fbb.tar.gz
llvm-1d530b914ef16ca0e920efe9c483ea44a8c62fbb.tar.bz2
[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.
-rw-r--r--llvm/lib/Analysis/InstructionSimplify.cpp15
-rw-r--r--llvm/test/Transforms/InstSimplify/phi.ll3
2 files changed, 13 insertions, 5 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 80fe656..4efc7c3 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 5d2ef05..d91b555 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