Skip to content

Commit 75d327c

Browse files
committed
Fix misscompile
1 parent 31ae38b commit 75d327c

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,7 @@ static Value *simplifyUsingControlFlow(InstCombiner &Self, PHINode &PN,
13391339
auto *Input = cast<ConstantInt>(std::get<0>(Pair));
13401340
BasicBlock *Pred = std::get<1>(Pair);
13411341
auto IsCorrectInput = [&](ConstantInt *Input) {
1342+
Instruction::CastOps TempCastType = CastType;
13421343
if (CastType != Instruction::BitCast) {
13431344
APInt InputValue = Input->getValue();
13441345
if (CastType == Instruction::Trunc)
@@ -1347,7 +1348,7 @@ static Value *simplifyUsingControlFlow(InstCombiner &Self, PHINode &PN,
13471348
InputValue.isIntN(CondBitWidth))
13481349
InputValue = InputValue.trunc(CondBitWidth);
13491350
else if (InputValue.isSignedIntN(CondBitWidth)) {
1350-
CastType = Instruction::SExt;
1351+
TempCastType = Instruction::SExt;
13511352
InputValue = InputValue.trunc(CondBitWidth);
13521353
} else
13531354
return false;
@@ -1357,9 +1358,12 @@ static Value *simplifyUsingControlFlow(InstCombiner &Self, PHINode &PN,
13571358
// This edge cannot be a multi-edge, as that would imply that multiple
13581359
// different condition values follow the same edge.
13591360
auto It = SuccForValue.find(Input);
1360-
return It != SuccForValue.end() && SuccCount[It->second] == 1 &&
1361-
DT.dominates(BasicBlockEdge(IDom, It->second),
1362-
BasicBlockEdge(Pred, BB));
1361+
bool Result = It != SuccForValue.end() && SuccCount[It->second] == 1 &&
1362+
DT.dominates(BasicBlockEdge(IDom, It->second),
1363+
BasicBlockEdge(Pred, BB));
1364+
if (Result)
1365+
CastType = TempCastType;
1366+
return Result;
13631367
};
13641368

13651369
// Depending on the constant, the condition may need to be inverted.

llvm/test/Transforms/InstCombine/simple_phi_condition.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,8 +713,8 @@ define i32 @test_phi_to_zext_inverted(i8 noundef %0) {
713713
; CHECK: bb1:
714714
; CHECK-NEXT: br label [[BB5]]
715715
; CHECK: bb5:
716-
; CHECK-NEXT: [[TMP1:%.*]] = xor i8 [[TMP0]], -1
717-
; CHECK-NEXT: [[DOT0:%.*]] = sext i8 [[TMP1]] to i32
716+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[TMP0]] to i32
717+
; CHECK-NEXT: [[DOT0:%.*]] = xor i32 [[TMP1]], -1
718718
; CHECK-NEXT: ret i32 [[DOT0]]
719719
;
720720
start:

0 commit comments

Comments
 (0)