Skip to content

Commit 2838066

Browse files
authored
[GlobalIsel] Add combine for select with constants (#121088)
The SelectionDAG Isel supports the both version of combines mentioned below : ``` select Cond, Pow2, 0 --> (zext Cond) << log2(Pow2) select Cond, 0, Pow2 --> (zext !Cond) << log2(Pow2) ``` The GlobalIsel for now only supports the first one defined in it's generic combinerHelper.cpp. This patch adds the missing second one.
1 parent f1fa292 commit 2838066

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6864,6 +6864,23 @@ bool CombinerHelper::tryFoldSelectOfConstants(GSelect *Select,
68646864
};
68656865
return true;
68666866
}
6867+
6868+
// select Cond, 0, Pow2 --> (zext (!Cond)) << log2(Pow2)
6869+
if (FalseValue.isPowerOf2() && TrueValue.isZero()) {
6870+
MatchInfo = [=](MachineIRBuilder &B) {
6871+
B.setInstrAndDebugLoc(*Select);
6872+
Register Not = MRI.createGenericVirtualRegister(CondTy);
6873+
B.buildNot(Not, Cond);
6874+
Register Inner = MRI.createGenericVirtualRegister(TrueTy);
6875+
B.buildZExtOrTrunc(Inner, Not);
6876+
// The shift amount must be scalar.
6877+
LLT ShiftTy = TrueTy.isVector() ? TrueTy.getElementType() : TrueTy;
6878+
auto ShAmtC = B.buildConstant(ShiftTy, FalseValue.exactLogBase2());
6879+
B.buildShl(Dest, Inner, ShAmtC, Flags);
6880+
};
6881+
return true;
6882+
}
6883+
68676884
// select Cond, -1, C --> or (sext Cond), C
68686885
if (TrueValue.isAllOnes()) {
68696886
MatchInfo = [=](MachineIRBuilder &B) {

llvm/test/CodeGen/AArch64/GlobalISel/combine-select.mir

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,36 @@ body: |
436436
$w0 = COPY %ext(s32)
437437
...
438438
---
439+
# select cond, 0, 64 --> (zext (!Cond)) << log2(Pow2)
440+
name: select_cond_0_64_to_shift
441+
body: |
442+
bb.1:
443+
liveins: $x0, $x1, $x2
444+
; CHECK-LABEL: name: select_cond_0_64_to_shift
445+
; CHECK: liveins: $x0, $x1, $x2
446+
; CHECK-NEXT: {{ $}}
447+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
448+
; CHECK-NEXT: %c:_(s1) = G_TRUNC [[COPY]](s64)
449+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s1) = G_CONSTANT i1 true
450+
; CHECK-NEXT: [[XOR:%[0-9]+]]:_(s1) = G_XOR %c, [[C]]
451+
; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s8) = G_ZEXT [[XOR]](s1)
452+
; CHECK-NEXT: [[C1:%[0-9]+]]:_(s8) = G_CONSTANT i8 6
453+
; CHECK-NEXT: %sel:_(s8) = G_SHL [[ZEXT]], [[C1]](s8)
454+
; CHECK-NEXT: %ext:_(s32) = G_ANYEXT %sel(s8)
455+
; CHECK-NEXT: $w0 = COPY %ext(s32)
456+
%0:_(s64) = COPY $x0
457+
%1:_(s64) = COPY $x1
458+
%2:_(s64) = COPY $x2
459+
%c:_(s1) = G_TRUNC %0
460+
%t:_(s1) = G_TRUNC %1
461+
%f:_(s1) = G_TRUNC %2
462+
%two:_(s8) = G_CONSTANT i8 0
463+
%one:_(s8) = G_CONSTANT i8 64
464+
%sel:_(s8) = G_SELECT %c, %two, %one
465+
%ext:_(s32) = G_ANYEXT %sel
466+
$w0 = COPY %ext(s32)
467+
...
468+
---
439469
# select cond, -1, 0 --> sext Cond
440470
name: select_cond_minus_1_0_to_sext_cond
441471
body: |

0 commit comments

Comments
 (0)