Skip to content

[InstCombine] Enable select freeze poison folding when storing value #129776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4812,19 +4812,23 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
//
// TODO: This could use getBinopAbsorber() / getBinopIdentity() to avoid
// duplicating logic for binops at least.
auto getUndefReplacement = [&I](Type *Ty) {
Constant *BestValue = nullptr;
Constant *NullValue = Constant::getNullValue(Ty);
auto getUndefReplacement = [&](Type *Ty) {
Value *BestValue = nullptr;
Value *NullValue = Constant::getNullValue(Ty);
for (const auto *U : I.users()) {
Constant *C = NullValue;
Value *V = NullValue;
if (match(U, m_Or(m_Value(), m_Value())))
C = ConstantInt::getAllOnesValue(Ty);
V = ConstantInt::getAllOnesValue(Ty);
else if (match(U, m_Select(m_Specific(&I), m_Constant(), m_Value())))
C = ConstantInt::getTrue(Ty);
V = ConstantInt::getTrue(Ty);
else if (match(U, m_c_Select(m_Specific(&I), m_Value(V)))) {
if (!isGuaranteedNotToBeUndefOrPoison(V, &AC, &I, &DT))
V = NullValue;
}

if (!BestValue)
BestValue = C;
else if (BestValue != C)
BestValue = V;
else if (BestValue != V)
BestValue = NullValue;
}
assert(BestValue && "Must have at least one use");
Expand All @@ -4840,10 +4844,28 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
return replaceInstUsesWith(I, getUndefReplacement(I.getType()));
}

auto getFreezeVectorReplacement = [](Constant *C) -> Constant * {
Type *Ty = C->getType();
auto *VTy = dyn_cast<FixedVectorType>(Ty);
if (!VTy)
return nullptr;
unsigned NumElts = VTy->getNumElements();
Constant *BestValue = Constant::getNullValue(VTy->getScalarType());
for (unsigned i = 0; i != NumElts; ++i) {
Constant *EltC = C->getAggregateElement(i);
if (EltC && !match(EltC, m_Undef())) {
BestValue = EltC;
break;
}
}
return Constant::replaceUndefsWith(C, BestValue);
};

Constant *C;
if (match(Op0, m_Constant(C)) && C->containsUndefOrPoisonElement()) {
Constant *ReplaceC = getUndefReplacement(I.getType()->getScalarType());
return replaceInstUsesWith(I, Constant::replaceUndefsWith(C, ReplaceC));
if (match(Op0, m_Constant(C)) && C->containsUndefOrPoisonElement() &&
!C->containsConstantExpression()) {
if (Constant *Repl = getFreezeVectorReplacement(C))
return replaceInstUsesWith(I, Repl);
}

// Replace uses of Op with freeze(Op).
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/freeze-phi.ll
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ define <2 x i32> @vec_undef(i1 %cond) {
; CHECK: B:
; CHECK-NEXT: br label [[C]]
; CHECK: C:
; CHECK-NEXT: [[Y:%.*]] = phi <2 x i32> [ <i32 0, i32 1>, [[A]] ], [ <i32 2, i32 0>, [[B]] ]
; CHECK-NEXT: [[Y:%.*]] = phi <2 x i32> [ <i32 0, i32 1>, [[A]] ], [ splat (i32 2), [[B]] ]
; CHECK-NEXT: ret <2 x i32> [[Y]]
;
br i1 %cond, label %A, label %B
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/freeze.ll
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ define void @or_select_multipleuses_logical(i32 %x, i1 %y) {

define <3 x i4> @partial_undef_vec() {
; CHECK-LABEL: @partial_undef_vec(
; CHECK-NEXT: ret <3 x i4> <i4 0, i4 1, i4 0>
; CHECK-NEXT: ret <3 x i4> splat (i4 1)
;
%f = freeze <3 x i4> <i4 poison, i4 1, i4 undef>
ret <3 x i4> %f
Expand Down
127 changes: 118 additions & 9 deletions llvm/test/Transforms/InstCombine/select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2652,8 +2652,7 @@ define <2 x i8> @partial_cond_freeze_constant_true_val_vec(<2 x i8> %x) {

define <2 x i8> @partial_cond_freeze_constant_false_val_vec(<2 x i8> %x) {
; CHECK-LABEL: @partial_cond_freeze_constant_false_val_vec(
; CHECK-NEXT: [[S1:%.*]] = insertelement <2 x i8> [[X:%.*]], i8 2, i64 1
; CHECK-NEXT: ret <2 x i8> [[S1]]
; CHECK-NEXT: ret <2 x i8> [[X:%.*]]
;
%cond.fr = freeze <2 x i1> <i1 true, i1 undef>
%s = select <2 x i1> %cond.fr, <2 x i8> %x, <2 x i8> <i8 1, i8 2>
Expand All @@ -2662,7 +2661,7 @@ define <2 x i8> @partial_cond_freeze_constant_false_val_vec(<2 x i8> %x) {

define <2 x i8> @partial_cond_freeze_both_arms_constant_vec() {
; CHECK-LABEL: @partial_cond_freeze_both_arms_constant_vec(
; CHECK-NEXT: ret <2 x i8> <i8 42, i8 2>
; CHECK-NEXT: ret <2 x i8> <i8 42, i8 43>
;
%cond.fr = freeze <2 x i1> <i1 false, i1 undef>
%s = select <2 x i1> %cond.fr, <2 x i8> <i8 1, i8 2>, <2 x i8> <i8 42, i8 43>
Expand Down Expand Up @@ -4904,8 +4903,7 @@ define i32 @src_simplify_2x_at_once_and(i32 %x, i32 %y) {

define void @select_freeze_poison_parameter(ptr noundef %addr.src, ptr %addr.tgt, i1 %cond) {
; CHECK-LABEL: @select_freeze_poison_parameter(
; CHECK-NEXT: [[ADDR_SRC:%.*]] = select i1 [[COND:%.*]], ptr [[ADDR_SRC1:%.*]], ptr null
; CHECK-NEXT: store ptr [[ADDR_SRC]], ptr [[ADDR_TGT:%.*]], align 8
; CHECK-NEXT: store ptr [[ADDR_SRC:%.*]], ptr [[ADDR_TGT:%.*]], align 8
; CHECK-NEXT: ret void
;
%freeze = freeze ptr poison
Expand All @@ -4914,12 +4912,47 @@ define void @select_freeze_poison_parameter(ptr noundef %addr.src, ptr %addr.tgt
ret void
}

define i8 @select_freeze_poison_different_parameters(i8 noundef %x, i8 noundef %y, i1 %cond1, i1 %cond2) {
; CHECK-LABEL: @select_freeze_poison_different_parameters(
; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[COND1:%.*]], i8 [[X:%.*]], i8 0
; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[COND2:%.*]], i8 [[Y:%.*]], i8 0
; CHECK-NEXT: [[CONJ:%.*]] = and i8 [[SEL1]], [[SEL2]]
; CHECK-NEXT: ret i8 [[CONJ]]
;
%freeze = freeze i8 poison
%sel1 = select i1 %cond1, i8 %x, i8 %freeze
%sel2 = select i1 %cond2, i8 %y, i8 %freeze
%conj = and i8 %sel1, %sel2
ret i8 %conj
}

define i8 @select_or_freeze_poison_parameter(i8 noundef %x, i1 %cond1) {
; CHECK-LABEL: @select_or_freeze_poison_parameter(
; CHECK-NEXT: ret i8 -1
;
%freeze = freeze i8 poison
%sel1 = select i1 %cond1, i8 -1, i8 %freeze
%or1 = or i8 %x, %freeze
%conj = and i8 %sel1, %or1
ret i8 %conj
}

define i8 @selects_freeze_poison_parameter(i8 noundef %x, i1 %cond1, i1 %cond2) {
; CHECK-LABEL: @selects_freeze_poison_parameter(
; CHECK-NEXT: ret i8 [[X:%.*]]
;
%freeze = freeze i8 poison
%sel1 = select i1 %cond1, i8 %x, i8 %freeze
%sel2 = select i1 %cond2, i8 %x, i8 %freeze
%conj = and i8 %sel1, %sel2
ret i8 %conj
}

@glb = global ptr null

define void @select_freeze_poison_global(ptr %addr.tgt, i1 %cond) {
; CHECK-LABEL: @select_freeze_poison_global(
; CHECK-NEXT: [[SELECT_ADDR:%.*]] = select i1 [[COND:%.*]], ptr @glb, ptr null
; CHECK-NEXT: store ptr [[SELECT_ADDR]], ptr [[ADDR_TGT:%.*]], align 8
; CHECK-NEXT: store ptr @glb, ptr [[ADDR_TGT:%.*]], align 8
; CHECK-NEXT: ret void
;
%freeze = freeze ptr poison
Expand All @@ -4930,12 +4963,88 @@ define void @select_freeze_poison_global(ptr %addr.tgt, i1 %cond) {

define void @select_freeze_poison_constant(ptr %addr.tgt, i1 %cond) {
; CHECK-LABEL: @select_freeze_poison_constant(
; CHECK-NEXT: [[SELECT_ADDR:%.*]] = select i1 [[COND:%.*]], i32 72, i32 0
; CHECK-NEXT: store i32 [[SELECT_ADDR]], ptr [[ADDR_TGT:%.*]], align 4
; CHECK-NEXT: store i32 72, ptr [[ADDR_TGT:%.*]], align 4
; CHECK-NEXT: ret void
;
%freeze = freeze i32 poison
%select.addr = select i1 %cond, i32 72, i32 %freeze
store i32 %select.addr, ptr %addr.tgt
ret void
}

define <2 x i8> @select_freeze_poison_mask_vector(i1 %cond, <2 x i8> noundef %y) {
; CHECK-LABEL: @select_freeze_poison_mask_vector(
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], <2 x i8> [[Y:%.*]], <2 x i8> zeroinitializer
; CHECK-NEXT: ret <2 x i8> [[SEL]]
;
%freeze = freeze <2 x i8> <i8 0, i8 poison>
%sel = select i1 %cond, <2 x i8> %y, <2 x i8> %freeze
ret <2 x i8> %sel
}

define <2 x i8> @selects_freeze_poison_mask_vector(<2 x i8> noundef %x, i1 %cond1, i1 %cond2) {
; CHECK-LABEL: @selects_freeze_poison_mask_vector(
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[COND1:%.*]], i1 [[COND2:%.*]], i1 false
; CHECK-NEXT: [[X:%.*]] = select i1 [[TMP1]], <2 x i8> [[X1:%.*]], <2 x i8> zeroinitializer
; CHECK-NEXT: ret <2 x i8> [[X]]
;
%freeze = freeze <2 x i8> <i8 0, i8 poison>
%sel1 = select i1 %cond1, <2 x i8> %x, <2 x i8> %freeze
%sel2 = select i1 %cond2, <2 x i8> %x, <2 x i8> %freeze
%conj = and <2 x i8> %sel1, %sel2
ret <2 x i8> %conj
}

define <2 x i8> @select_freeze_poison_splat_vector(i1 %cond, <2 x i8> noundef %y) {
; CHECK-LABEL: @select_freeze_poison_splat_vector(
; CHECK-NEXT: ret <2 x i8> [[Y:%.*]]
;
%freeze = freeze <2 x i8> <i8 poison, i8 poison>
%sel = select i1 %cond, <2 x i8> %y, <2 x i8> %freeze
ret <2 x i8> %sel
}

define <2 x i8> @selects_freeze_poison_splat_vector(<2 x i8> noundef %x, i1 %cond1, i1 %cond2) {
; CHECK-LABEL: @selects_freeze_poison_splat_vector(
; CHECK-NEXT: ret <2 x i8> [[X:%.*]]
;
%freeze = freeze <2 x i8> <i8 poison, i8 poison>
%sel1 = select i1 %cond1, <2 x i8> %x, <2 x i8> %freeze
%sel2 = select i1 %cond2, <2 x i8> %x, <2 x i8> %freeze
%conj = and <2 x i8> %sel1, %sel2
ret <2 x i8> %conj
}

define <2 x i8> @select_freeze_constant_vector(i1 %cond, <2 x i8> noundef %y) {
; CHECK-LABEL: @select_freeze_constant_vector(
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], <2 x i8> [[Y:%.*]], <2 x i8> zeroinitializer
; CHECK-NEXT: ret <2 x i8> [[SEL]]
;
%freeze = freeze <2 x i8> <i8 0, i8 0>
%sel = select i1 %cond, <2 x i8> %y, <2 x i8> %freeze
ret <2 x i8> %sel
}

define <2 x i8> @select_freeze_constant_expression_vector_add(i1 %cond, <2 x i8> noundef %y) {
; CHECK-LABEL: @select_freeze_constant_expression_vector_add(
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], <2 x i8> [[Y:%.*]], <2 x i8> splat (i8 3)
; CHECK-NEXT: ret <2 x i8> [[SEL]]
;
%freeze = freeze <2 x i8> <i8 poison, i8 add (i8 1, i8 2)>
%sel = select i1 %cond, <2 x i8> %y, <2 x i8> %freeze
ret <2 x i8> %sel
}

%struct.1 = type {i32, i32}
@glb.struct.1 = global %struct.1 {i32 1, i32 2}

define <2 x ptr> @select_freeze_constant_expression_vector_gep(i1 %cond, <2 x ptr> noundef %y) {
; CHECK-LABEL: @select_freeze_constant_expression_vector_gep(
; CHECK-NEXT: [[FREEZE:%.*]] = freeze <2 x ptr> <ptr poison, ptr getelementptr inbounds nuw (i8, ptr @glb.struct.1, i64 800)>
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND:%.*]], <2 x ptr> [[Y:%.*]], <2 x ptr> [[FREEZE]]
; CHECK-NEXT: ret <2 x ptr> [[SEL]]
;
%freeze = freeze <2 x ptr> <ptr poison, ptr getelementptr inbounds (%struct.1, ptr @glb.struct.1, i64 100)>
%sel = select i1 %cond, <2 x ptr> %y, <2 x ptr> %freeze
ret <2 x ptr> %sel
}
Loading