diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index b9957a947392c..b1f2e8bbc85ee 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -3779,23 +3779,20 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) { return replaceInstUsesWith(I, NI); if (match(Op0, m_Undef())) { - // If I is freeze(undef), see its uses and fold it to the best constant. + // If I is freeze(undef), check its uses and fold it to a fixed constant. // - or: pick -1 - // - select's condition: pick the value that leads to choosing a constant - // - other ops: pick 0 + // - select's condition: if the true value is constant, choose it by making + // the condition true. + // - default: pick 0 Constant *BestValue = nullptr; Constant *NullValue = Constant::getNullValue(I.getType()); for (const auto *U : I.users()) { Constant *C = NullValue; if (match(U, m_Or(m_Value(), m_Value()))) - C = Constant::getAllOnesValue(I.getType()); - else if (const auto *SI = dyn_cast(U)) { - if (SI->getCondition() == &I) { - APInt CondVal(1, isa(SI->getFalseValue()) ? 0 : 1); - C = Constant::getIntegerValue(I.getType(), CondVal); - } - } + C = ConstantInt::getAllOnesValue(I.getType()); + else if (match(U, m_Select(m_Specific(&I), m_Constant(), m_Value()))) + C = ConstantInt::getTrue(I.getType()); if (!BestValue) BestValue = C; diff --git a/llvm/test/Transforms/InstCombine/select.ll b/llvm/test/Transforms/InstCombine/select.ll index 06ed583981710..170591378ea40 100644 --- a/llvm/test/Transforms/InstCombine/select.ll +++ b/llvm/test/Transforms/InstCombine/select.ll @@ -2588,7 +2588,7 @@ define <2 x i32> @true_undef_vec(i1 %cond, <2 x i32> %x) { define i8 @cond_freeze(i8 %x, i8 %y) { ; CHECK-LABEL: @cond_freeze( -; CHECK-NEXT: ret i8 [[X:%.*]] +; CHECK-NEXT: ret i8 [[Y:%.*]] ; %cond.fr = freeze i1 undef %s = select i1 %cond.fr, i8 %x, i8 %y @@ -2615,7 +2615,7 @@ define i8 @cond_freeze_constant_true_val(i8 %x) { define i8 @cond_freeze_both_arms_constant() { ; CHECK-LABEL: @cond_freeze_both_arms_constant( -; CHECK-NEXT: ret i8 3 +; CHECK-NEXT: ret i8 42 ; %cond.fr = freeze i1 undef %s = select i1 %cond.fr, i8 42, i8 3 @@ -2646,7 +2646,7 @@ declare void @foo2(i8, i8) define void @cond_freeze_multipleuses(i8 %x, i8 %y) { ; CHECK-LABEL: @cond_freeze_multipleuses( -; CHECK-NEXT: call void @foo2(i8 [[X:%.*]], i8 [[Y:%.*]]) +; CHECK-NEXT: call void @foo2(i8 [[Y:%.*]], i8 [[X:%.*]]) ; CHECK-NEXT: ret void ; %cond.fr = freeze i1 undef