Skip to content

Commit

Permalink
[InstCombine] Fix evaluation order dependent fold
Browse files Browse the repository at this point in the history
Make sure the function arguments are evaluated in a predictable
order.
  • Loading branch information
nikic committed Aug 14, 2023
1 parent 4192c41 commit 2827aa9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,13 +926,17 @@ InstCombinerImpl::foldBinOpOfSelectAndCastOfSelectCondition(BinaryOperator &I) {

// If the value used in the zext/sext is the select condition, or the negated
// of the select condition, the binop can be simplified.
if (CondVal == A)
return SelectInst::Create(CondVal, NewFoldedConst(false, TrueVal),
if (CondVal == A) {
Value *NewTrueVal = NewFoldedConst(false, TrueVal);
return SelectInst::Create(CondVal, NewTrueVal,
NewFoldedConst(true, FalseVal));
}

if (match(A, m_Not(m_Specific(CondVal))))
return SelectInst::Create(CondVal, NewFoldedConst(true, TrueVal),
if (match(A, m_Not(m_Specific(CondVal)))) {
Value *NewTrueVal = NewFoldedConst(true, TrueVal);
return SelectInst::Create(CondVal, NewTrueVal,
NewFoldedConst(false, FalseVal));
}

return nullptr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ define i64 @select_non_const_sides(i1 %c, i64 %arg1, i64 %arg2) {
define i6 @sub_select_sext_op_swapped_non_const_args(i1 %c, i6 %argT, i6 %argF) {
; CHECK-LABEL: define i6 @sub_select_sext_op_swapped_non_const_args
; CHECK-SAME: (i1 [[C:%.*]], i6 [[ARGT:%.*]], i6 [[ARGF:%.*]]) {
; CHECK-DAG: [[TMP1:%.*]] = xor i6 [[ARGT]], -1
; CHECK-DAG: [[TMP2:%.*]] = sub i6 0, [[ARGF]]
; CHECK-NEXT: [[TMP1:%.*]] = xor i6 [[ARGT]], -1
; CHECK-NEXT: [[TMP2:%.*]] = sub i6 0, [[ARGF]]
; CHECK-NEXT: [[SUB:%.*]] = select i1 [[C]], i6 [[TMP1]], i6 [[TMP2]]
; CHECK-NEXT: ret i6 [[SUB]]
;
Expand All @@ -201,8 +201,8 @@ define i6 @sub_select_sext_op_swapped_non_const_args(i1 %c, i6 %argT, i6 %argF)
define i6 @sub_select_zext_op_swapped_non_const_args(i1 %c, i6 %argT, i6 %argF) {
; CHECK-LABEL: define i6 @sub_select_zext_op_swapped_non_const_args
; CHECK-SAME: (i1 [[C:%.*]], i6 [[ARGT:%.*]], i6 [[ARGF:%.*]]) {
; CHECK-DAG: [[TMP1:%.*]] = sub i6 1, [[ARGT]]
; CHECK-DAG: [[TMP2:%.*]] = sub i6 0, [[ARGF]]
; CHECK-NEXT: [[TMP1:%.*]] = sub i6 1, [[ARGT]]
; CHECK-NEXT: [[TMP2:%.*]] = sub i6 0, [[ARGF]]
; CHECK-NEXT: [[SUB:%.*]] = select i1 [[C]], i6 [[TMP1]], i6 [[TMP2]]
; CHECK-NEXT: ret i6 [[SUB]]
;
Expand Down

0 comments on commit 2827aa9

Please sign in to comment.