Skip to content

Commit

Permalink
Fixing miscompilation due to undef picking the wrong branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jf-botto committed Aug 9, 2024
1 parent e7e70dc commit 83cec7f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
11 changes: 3 additions & 8 deletions llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1735,17 +1735,12 @@ Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V,
APInt DemandedLHS(DemandedElts), DemandedRHS(DemandedElts);
if (auto *CV = dyn_cast<ConstantVector>(Sel->getCondition())) {
for (unsigned i = 0; i < VWidth; i++) {
// isNullValue() always returns false when called on a ConstantExpr.
// Skip constant expressions to avoid propagating incorrect information.
Constant *CElt = CV->getAggregateElement(i);
if (isa<ConstantExpr>(CElt))
continue;
// TODO: If a select condition element is undef, we can demand from
// either side. If one side is known undef, choosing that side would
// propagate undef.

// isNullValue() always returns false when called on a ConstantExpr.
if (CElt->isNullValue())
DemandedLHS.clearBit(i);
else
else if (CElt->isOneValue())
DemandedRHS.clearBit(i);
}
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/test/Transforms/InstCombine/pr98435.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
define <2 x i1> @pr98435(<2 x i1> %val) {
; CHECK-LABEL: define <2 x i1> @pr98435(
; CHECK-SAME: <2 x i1> [[VAL:%.*]]) {
; CHECK-NEXT: ret <2 x i1> <i1 poison, i1 true>
; CHECK-NEXT: [[VAL1:%.*]] = select <2 x i1> <i1 undef, i1 true>, <2 x i1> <i1 true, i1 true>, <2 x i1> [[VAL]]
; CHECK-NEXT: ret <2 x i1> [[VAL1]]
;
%val1 = select <2 x i1> <i1 undef, i1 true>, <2 x i1> <i1 true, i1 true>, <2 x i1> %val
ret <2 x i1> %val1
Expand Down

0 comments on commit 83cec7f

Please sign in to comment.