Skip to content

Commit 9760a8d

Browse files
committed
[GlobalISel] Check whether G_CTLZ is legal in matchUMulHToLShr
We need to check `G_CTLZ` because the combine uses `G_CTLZ` to get log base 2, and it is not always legal for on a target.
1 parent 9679735 commit 9760a8d

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5641,6 +5641,7 @@ bool CombinerHelper::matchUMulHToLShr(MachineInstr &MI) const {
56415641
Register RHS = MI.getOperand(2).getReg();
56425642
Register Dst = MI.getOperand(0).getReg();
56435643
LLT Ty = MRI.getType(Dst);
5644+
LLT RHSTy = MRI.getType(RHS);
56445645
LLT ShiftAmtTy = getTargetLowering().getPreferredShiftAmountTy(Ty);
56455646
auto MatchPow2ExceptOne = [&](const Constant *C) {
56465647
if (auto *CI = dyn_cast<ConstantInt>(C))
@@ -5649,7 +5650,10 @@ bool CombinerHelper::matchUMulHToLShr(MachineInstr &MI) const {
56495650
};
56505651
if (!matchUnaryPredicate(MRI, RHS, MatchPow2ExceptOne, false))
56515652
return false;
5652-
return isLegalOrBeforeLegalizer({TargetOpcode::G_LSHR, {Ty, ShiftAmtTy}});
5653+
// We need to check both G_LSHR and G_CTLZ because the combine uses G_CTLZ to
5654+
// get log base 2, and it is not always legal for on a target.
5655+
return isLegalOrBeforeLegalizer({TargetOpcode::G_LSHR, {Ty, ShiftAmtTy}}) &&
5656+
isLegalOrBeforeLegalizer({TargetOpcode::G_CTLZ, {RHSTy, RHSTy}});
56535657
}
56545658

56555659
void CombinerHelper::applyUMulHToLShr(MachineInstr &MI) const {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 5
2+
# RUN: llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx90a -run-pass=amdgpu-postlegalizer-combiner %s -o - | FileCheck %s
3+
4+
---
5+
name: test
6+
tracksRegLiveness: true
7+
legalized: true
8+
body: |
9+
bb.0:
10+
liveins: $vgpr0, $vgpr1
11+
; CHECK-LABEL: name: test
12+
; CHECK: liveins: $vgpr0, $vgpr1
13+
; CHECK-NEXT: {{ $}}
14+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $vgpr0
15+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 4
16+
; CHECK-NEXT: [[UMULH:%[0-9]+]]:_(s32) = G_UMULH [[COPY]], [[C]]
17+
; CHECK-NEXT: $vgpr0 = COPY [[UMULH]](s32)
18+
; CHECK-NEXT: SI_RETURN implicit $vgpr0
19+
%0:_(s32) = COPY $vgpr0
20+
%1:_(s32) = G_CONSTANT i32 4
21+
%2:_(s32) = G_UMULH %0:_, %1:_
22+
$vgpr0 = COPY %2:_(s32)
23+
SI_RETURN implicit $vgpr0

0 commit comments

Comments
 (0)