Skip to content

Commit 1d530b9

Browse files
committed
[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 llvm#49839.
1 parent bc6d7ed commit 1d530b9

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4817,11 +4817,18 @@ static Value *SimplifyPHINode(PHINode *PN, ArrayRef<Value *> IncomingValues,
48174817
if (!CommonValue)
48184818
return UndefValue::get(PN->getType());
48194819

4820-
// If we have a PHI node like phi(X, undef, X), where X is defined by some
4821-
// instruction, we cannot return X as the result of the PHI node unless it
4822-
// dominates the PHI block.
4823-
if (HasUndefInput)
4820+
if (HasUndefInput) {
4821+
// We cannot start executing a trapping constant expression on more control
4822+
// flow paths.
4823+
auto *CE = dyn_cast<ConstantExpr>(CommonValue);
4824+
if (CE && CE->canTrap())
4825+
return nullptr;
4826+
4827+
// If we have a PHI node like phi(X, undef, X), where X is defined by some
4828+
// instruction, we cannot return X as the result of the PHI node unless it
4829+
// dominates the PHI block.
48244830
return valueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr;
4831+
}
48254832

48264833
return CommonValue;
48274834
}

llvm/test/Transforms/InstSimplify/phi.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ define i64 @pr49839_with_poison(i1 %c) {
162162
; CHECK: if:
163163
; CHECK-NEXT: br label [[JOIN]]
164164
; CHECK: join:
165-
; CHECK-NEXT: ret i64 srem (i64 1, i64 ptrtoint (i32* @g to i64))
165+
; CHECK-NEXT: [[PHI:%.*]] = phi i64 [ poison, [[IF]] ], [ srem (i64 1, i64 ptrtoint (i32* @g to i64)), [[ENTRY:%.*]] ]
166+
; CHECK-NEXT: ret i64 [[PHI]]
166167
;
167168
entry:
168169
br i1 %c, label %if, label %join

0 commit comments

Comments
 (0)