Skip to content

[GlobalIsel] Add combine for select with constants #121088

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
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
17 changes: 17 additions & 0 deletions llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6823,6 +6823,23 @@ bool CombinerHelper::tryFoldSelectOfConstants(GSelect *Select,
};
return true;
}

// select Cond, 0, Pow2 --> (zext (!Cond)) << log2(Pow2)
if (FalseValue.isPowerOf2() && TrueValue.isZero()) {
MatchInfo = [=](MachineIRBuilder &B) {
B.setInstrAndDebugLoc(*Select);
Register Not = MRI.createGenericVirtualRegister(CondTy);
B.buildNot(Not, Cond);
Register Inner = MRI.createGenericVirtualRegister(TrueTy);
B.buildZExtOrTrunc(Inner, Not);
// The shift amount must be scalar.
LLT ShiftTy = TrueTy.isVector() ? TrueTy.getElementType() : TrueTy;
auto ShAmtC = B.buildConstant(ShiftTy, FalseValue.exactLogBase2());
B.buildShl(Dest, Inner, ShAmtC, Flags);
};
return true;
}

// select Cond, -1, C --> or (sext Cond), C
if (TrueValue.isAllOnes()) {
MatchInfo = [=](MachineIRBuilder &B) {
Expand Down
30 changes: 30 additions & 0 deletions llvm/test/CodeGen/AArch64/GlobalISel/combine-select.mir
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,36 @@ body: |
$w0 = COPY %ext(s32)
...
---
# select cond, 0, 64 --> (zext (!Cond)) << log2(Pow2)
name: select_cond_0_64_to_shift
body: |
bb.1:
liveins: $x0, $x1, $x2
; CHECK-LABEL: name: select_cond_0_64_to_shift
; CHECK: liveins: $x0, $x1, $x2
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
; CHECK-NEXT: %c:_(s1) = G_TRUNC [[COPY]](s64)
; CHECK-NEXT: [[C:%[0-9]+]]:_(s1) = G_CONSTANT i1 true
; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s1) = G_XOR %c, [[C]]
; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s8) = G_ZEXT [[XOR]](s1)
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s8) = G_CONSTANT i8 6
; CHECK-NEXT: %sel:_(s8) = G_SHL [[ZEXT]], [[C1]](s8)
; CHECK-NEXT: %ext:_(s32) = G_ANYEXT %sel(s8)
; CHECK-NEXT: $w0 = COPY %ext(s32)
%0:_(s64) = COPY $x0
%1:_(s64) = COPY $x1
%2:_(s64) = COPY $x2
%c:_(s1) = G_TRUNC %0
%t:_(s1) = G_TRUNC %1
%f:_(s1) = G_TRUNC %2
%two:_(s8) = G_CONSTANT i8 0
%one:_(s8) = G_CONSTANT i8 64
%sel:_(s8) = G_SELECT %c, %two, %one
%ext:_(s32) = G_ANYEXT %sel
$w0 = COPY %ext(s32)
...
---
# select cond, -1, 0 --> sext Cond
name: select_cond_minus_1_0_to_sext_cond
body: |
Expand Down
Loading