Skip to content

[InstCombine] Propagate flags in foldSelectICmpAndBinOp #127437

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 2 commits into from
Feb 19, 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
5 changes: 4 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,10 @@ static Value *foldSelectICmpAndBinOp(const ICmpInst *IC, Value *TrueVal,
if (NeedXor)
V = Builder.CreateXor(V, *C2);

return Builder.CreateBinOp(BinOp->getOpcode(), Y, V);
auto *Res = Builder.CreateBinOp(BinOp->getOpcode(), Y, V);
if (auto *BO = dyn_cast<BinaryOperator>(Res))
BO->copyIRFlags(BinOp);
return Res;
}

/// Canonicalize a set or clear of a masked set of constant bits to
Expand Down
42 changes: 42 additions & 0 deletions llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,34 @@ define i32 @select_icmp_eq_and_1_0_or_2(i32 %x, i32 %y) {
ret i32 %select
}

define i32 @select_icmp_eq_and_1_0_or_2_disjoint(i32 %x, i32 %y) {
; CHECK-LABEL: @select_icmp_eq_and_1_0_or_2_disjoint(
; CHECK-NEXT: [[AND:%.*]] = shl i32 [[X:%.*]], 1
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[AND]], 2
; CHECK-NEXT: [[SELECT:%.*]] = or disjoint i32 [[Y:%.*]], [[TMP1]]
; CHECK-NEXT: ret i32 [[SELECT]]
;
%and = and i32 %x, 1
%cmp = icmp eq i32 %and, 0
%or = or disjoint i32 %y, 2
%select = select i1 %cmp, i32 %y, i32 %or
ret i32 %select
}

define i32 @select_icmp_eq_and_1_0_add_2_nsw_nuw(i32 %x, i32 %y) {
; CHECK-LABEL: @select_icmp_eq_and_1_0_add_2_nsw_nuw(
; CHECK-NEXT: [[AND:%.*]] = shl i32 [[X:%.*]], 1
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[AND]], 2
; CHECK-NEXT: [[SELECT:%.*]] = add nuw nsw i32 [[Y:%.*]], [[TMP1]]
; CHECK-NEXT: ret i32 [[SELECT]]
;
%and = and i32 %x, 1
%cmp = icmp eq i32 %and, 0
%or = add nsw nuw i32 %y, 2
%select = select i1 %cmp, i32 %y, i32 %or
ret i32 %select
}

define <2 x i32> @select_icmp_eq_and_1_0_or_2_vec(<2 x i32> %x, <2 x i32> %y) {
; CHECK-LABEL: @select_icmp_eq_and_1_0_or_2_vec(
; CHECK-NEXT: [[AND:%.*]] = shl <2 x i32> [[X:%.*]], splat (i32 1)
Expand Down Expand Up @@ -1696,6 +1724,20 @@ define i8 @select_icmp_eq_and_1_0_lshr_fv(i8 %x, i8 %y) {
ret i8 %select
}

define i8 @select_icmp_eq_and_1_0_lshr_exact_fv(i8 %x, i8 %y) {
; CHECK-LABEL: @select_icmp_eq_and_1_0_lshr_exact_fv(
; CHECK-NEXT: [[AND:%.*]] = shl i8 [[X:%.*]], 1
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[AND]], 2
; CHECK-NEXT: [[SELECT:%.*]] = lshr exact i8 [[Y:%.*]], [[TMP1]]
; CHECK-NEXT: ret i8 [[SELECT]]
;
%and = and i8 %x, 1
%cmp = icmp eq i8 %and, 0
%blshr = lshr exact i8 %y, 2
%select = select i1 %cmp, i8 %y, i8 %blshr
ret i8 %select
}

define i8 @select_icmp_eq_and_1_0_lshr_tv(i8 %x, i8 %y) {
; CHECK-LABEL: @select_icmp_eq_and_1_0_lshr_tv(
; CHECK-NEXT: [[AND:%.*]] = shl i8 [[X:%.*]], 1
Expand Down