Skip to content

Commit 3a27b51

Browse files
committed
[InstCombine] reduce code for freeze of undef
The description was ambiguous about the behavior when boths select arms are constant or both arms are not constant. I don't think there's any evidence to support either way, but this matches the code with a more specified description. We can extend this to deal with vector constants with undef/poison elements. Currently, those don't get folded anywhere.
1 parent 60de144 commit 3a27b51

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3779,23 +3779,20 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
37793779
return replaceInstUsesWith(I, NI);
37803780

37813781
if (match(Op0, m_Undef())) {
3782-
// If I is freeze(undef), see its uses and fold it to the best constant.
3782+
// If I is freeze(undef), check its uses and fold it to a fixed constant.
37833783
// - or: pick -1
3784-
// - select's condition: pick the value that leads to choosing a constant
3785-
// - other ops: pick 0
3784+
// - select's condition: if the true value is constant, choose it by making
3785+
// the condition true.
3786+
// - default: pick 0
37863787
Constant *BestValue = nullptr;
37873788
Constant *NullValue = Constant::getNullValue(I.getType());
37883789
for (const auto *U : I.users()) {
37893790
Constant *C = NullValue;
37903791

37913792
if (match(U, m_Or(m_Value(), m_Value())))
3792-
C = Constant::getAllOnesValue(I.getType());
3793-
else if (const auto *SI = dyn_cast<SelectInst>(U)) {
3794-
if (SI->getCondition() == &I) {
3795-
APInt CondVal(1, isa<Constant>(SI->getFalseValue()) ? 0 : 1);
3796-
C = Constant::getIntegerValue(I.getType(), CondVal);
3797-
}
3798-
}
3793+
C = ConstantInt::getAllOnesValue(I.getType());
3794+
else if (match(U, m_Select(m_Specific(&I), m_Constant(), m_Value())))
3795+
C = ConstantInt::getTrue(I.getType());
37993796

38003797
if (!BestValue)
38013798
BestValue = C;

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ define <2 x i32> @true_undef_vec(i1 %cond, <2 x i32> %x) {
25882588

25892589
define i8 @cond_freeze(i8 %x, i8 %y) {
25902590
; CHECK-LABEL: @cond_freeze(
2591-
; CHECK-NEXT: ret i8 [[X:%.*]]
2591+
; CHECK-NEXT: ret i8 [[Y:%.*]]
25922592
;
25932593
%cond.fr = freeze i1 undef
25942594
%s = select i1 %cond.fr, i8 %x, i8 %y
@@ -2615,7 +2615,7 @@ define i8 @cond_freeze_constant_true_val(i8 %x) {
26152615

26162616
define i8 @cond_freeze_both_arms_constant() {
26172617
; CHECK-LABEL: @cond_freeze_both_arms_constant(
2618-
; CHECK-NEXT: ret i8 3
2618+
; CHECK-NEXT: ret i8 42
26192619
;
26202620
%cond.fr = freeze i1 undef
26212621
%s = select i1 %cond.fr, i8 42, i8 3
@@ -2646,7 +2646,7 @@ declare void @foo2(i8, i8)
26462646

26472647
define void @cond_freeze_multipleuses(i8 %x, i8 %y) {
26482648
; CHECK-LABEL: @cond_freeze_multipleuses(
2649-
; CHECK-NEXT: call void @foo2(i8 [[X:%.*]], i8 [[Y:%.*]])
2649+
; CHECK-NEXT: call void @foo2(i8 [[Y:%.*]], i8 [[X:%.*]])
26502650
; CHECK-NEXT: ret void
26512651
;
26522652
%cond.fr = freeze i1 undef

0 commit comments

Comments
 (0)