Skip to content

Commit 2827aa9

Browse files
committed
[InstCombine] Fix evaluation order dependent fold
Make sure the function arguments are evaluated in a predictable order.
1 parent 4192c41 commit 2827aa9

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -926,13 +926,17 @@ InstCombinerImpl::foldBinOpOfSelectAndCastOfSelectCondition(BinaryOperator &I) {
926926

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

933-
if (match(A, m_Not(m_Specific(CondVal))))
934-
return SelectInst::Create(CondVal, NewFoldedConst(true, TrueVal),
935+
if (match(A, m_Not(m_Specific(CondVal)))) {
936+
Value *NewTrueVal = NewFoldedConst(true, TrueVal);
937+
return SelectInst::Create(CondVal, NewTrueVal,
935938
NewFoldedConst(false, FalseVal));
939+
}
936940

937941
return nullptr;
938942
}

llvm/test/Transforms/InstCombine/binop-select-cast-of-select-cond.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ define i64 @select_non_const_sides(i1 %c, i64 %arg1, i64 %arg2) {
187187
define i6 @sub_select_sext_op_swapped_non_const_args(i1 %c, i6 %argT, i6 %argF) {
188188
; CHECK-LABEL: define i6 @sub_select_sext_op_swapped_non_const_args
189189
; CHECK-SAME: (i1 [[C:%.*]], i6 [[ARGT:%.*]], i6 [[ARGF:%.*]]) {
190-
; CHECK-DAG: [[TMP1:%.*]] = xor i6 [[ARGT]], -1
191-
; CHECK-DAG: [[TMP2:%.*]] = sub i6 0, [[ARGF]]
190+
; CHECK-NEXT: [[TMP1:%.*]] = xor i6 [[ARGT]], -1
191+
; CHECK-NEXT: [[TMP2:%.*]] = sub i6 0, [[ARGF]]
192192
; CHECK-NEXT: [[SUB:%.*]] = select i1 [[C]], i6 [[TMP1]], i6 [[TMP2]]
193193
; CHECK-NEXT: ret i6 [[SUB]]
194194
;
@@ -201,8 +201,8 @@ define i6 @sub_select_sext_op_swapped_non_const_args(i1 %c, i6 %argT, i6 %argF)
201201
define i6 @sub_select_zext_op_swapped_non_const_args(i1 %c, i6 %argT, i6 %argF) {
202202
; CHECK-LABEL: define i6 @sub_select_zext_op_swapped_non_const_args
203203
; CHECK-SAME: (i1 [[C:%.*]], i6 [[ARGT:%.*]], i6 [[ARGF:%.*]]) {
204-
; CHECK-DAG: [[TMP1:%.*]] = sub i6 1, [[ARGT]]
205-
; CHECK-DAG: [[TMP2:%.*]] = sub i6 0, [[ARGF]]
204+
; CHECK-NEXT: [[TMP1:%.*]] = sub i6 1, [[ARGT]]
205+
; CHECK-NEXT: [[TMP2:%.*]] = sub i6 0, [[ARGF]]
206206
; CHECK-NEXT: [[SUB:%.*]] = select i1 [[C]], i6 [[TMP1]], i6 [[TMP2]]
207207
; CHECK-NEXT: ret i6 [[SUB]]
208208
;

0 commit comments

Comments
 (0)