Skip to content

Commit 942148e

Browse files
committed
[GlobalIsel] Added combine for select with constants
The SelectionDAG Isel supports the both version of combines : select Cond, Pow2, 0 --> (zext Cond) << log2(Pow2) select Cond, 0, Pow2 --> (zext !Cond) << log2(Pow2) But, here we add the latter missing combine for select in globalIsel.
1 parent 7163603 commit 942148e

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
@@ -6823,6 +6823,23 @@ bool CombinerHelper::tryFoldSelectOfConstants(GSelect *Select,
68236823
};
68246824
return true;
68256825
}
6826+
6827+
// select Cond, 0, Pow2 --> (zext (!Cond)) << log2(Pow2)
6828+
if (FalseValue.isPowerOf2() && TrueValue.isZero()) {
6829+
MatchInfo = [=](MachineIRBuilder &B) {
6830+
B.setInstrAndDebugLoc(*Select);
6831+
Register Not = MRI.createGenericVirtualRegister(CondTy);
6832+
B.buildNot(Not, Cond);
6833+
Register Inner = MRI.createGenericVirtualRegister(TrueTy);
6834+
B.buildZExtOrTrunc(Inner, Not);
6835+
// The shift amount must be scalar.
6836+
LLT ShiftTy = TrueTy.isVector() ? TrueTy.getElementType() : TrueTy;
6837+
auto ShAmtC = B.buildConstant(ShiftTy, FalseValue.exactLogBase2());
6838+
B.buildShl(Dest, Inner, ShAmtC, Flags);
6839+
};
6840+
return true;
6841+
}
6842+
68266843
// select Cond, -1, C --> or (sext Cond), C
68276844
if (TrueValue.isAllOnes()) {
68286845
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)