[llvm] [GlobalISel] Improve combines for extend operation by taking hint ins… (PR #74125)
Dávid Ferenc Szabó via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 1 10:53:31 PST 2023
https://github.com/dfszabo created https://github.com/llvm/llvm-project/pull/74125
…tructions into account
Hint instructions like G_ASSERT_ZEXT cann be viewed as a copy. Including this fact into the combiner allows the match more patterns involving such instructions.
>From e9063a29b2093c1ee36c8936193642f71c5918e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?D=C3=A1vid=20Ferenc=20Szab=C3=B3?=
<szabodavidferenc at gmail.com>
Date: Fri, 1 Dec 2023 16:51:32 +0100
Subject: [PATCH] [GlobalISel] Improve combines for extend operation by taking
hint instructions into account
Hint instructions like G_ASSERT_ZEXT cann be viewed as a copy. Including this fact into the combiner allows the match more patterns involving such instructions.
---
.../lib/CodeGen/GlobalISel/CombinerHelper.cpp | 6 +
.../AArch64/GlobalISel/combine-ext.mir | 226 ++++++++++++++----
2 files changed, 192 insertions(+), 40 deletions(-)
diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
index c2a7c2d01188129..069752807f3904d 100644
--- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -2280,6 +2280,9 @@ bool CombinerHelper::matchCombineAnyExtTrunc(MachineInstr &MI, Register &Reg) {
assert(MI.getOpcode() == TargetOpcode::G_ANYEXT && "Expected a G_ANYEXT");
Register DstReg = MI.getOperand(0).getReg();
Register SrcReg = MI.getOperand(1).getReg();
+ Register OriginalSrcReg = getSrcRegIgnoringCopies(SrcReg, MRI);
+ if (OriginalSrcReg.isValid())
+ SrcReg = OriginalSrcReg;
LLT DstTy = MRI.getType(DstReg);
return mi_match(SrcReg, MRI,
m_GTrunc(m_all_of(m_Reg(Reg), m_SpecificType(DstTy))));
@@ -2306,6 +2309,9 @@ bool CombinerHelper::matchCombineExtOfExt(
MI.getOpcode() == TargetOpcode::G_ZEXT) &&
"Expected a G_[ASZ]EXT");
Register SrcReg = MI.getOperand(1).getReg();
+ Register OriginalSrcReg = getSrcRegIgnoringCopies(SrcReg, MRI);
+ if (OriginalSrcReg.isValid())
+ SrcReg = OriginalSrcReg;
MachineInstr *SrcMI = MRI.getVRegDef(SrcReg);
// Match exts with the same opcode, anyext([sz]ext) and sext(zext).
unsigned Opc = MI.getOpcode();
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-ext.mir b/llvm/test/CodeGen/AArch64/GlobalISel/combine-ext.mir
index 1b8ba9f7bec1d3a..a431a6764d06f7b 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-ext.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-ext.mir
@@ -6,21 +6,41 @@ body: |
bb.1:
liveins: $x0
; CHECK-LABEL: name: test_combine_anyext_trunc
- ; CHECK: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
- ; CHECK: $x1 = COPY [[COPY]](s64)
+ ; CHECK: liveins: $x0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
+ ; CHECK-NEXT: $x1 = COPY [[COPY]](s64)
%0:_(s64) = COPY $x0
%1:_(s32) = G_TRUNC %0(s64)
%2:_(s64) = G_ANYEXT %1(s32)
$x1 = COPY %2(s64)
...
---
+name: test_combine_anyext_trunc_with_hint
+body: |
+ bb.1:
+ liveins: $x0
+ ; CHECK-LABEL: name: test_combine_anyext_trunc_with_hint
+ ; CHECK: liveins: $x0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
+ ; CHECK-NEXT: $x1 = COPY [[COPY]](s64)
+ %0:_(s64) = COPY $x0
+ %1:_(s32) = G_TRUNC %0(s64)
+ %2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
+ %3:_(s64) = G_ANYEXT %2(s32)
+ $x1 = COPY %3(s64)
+...
+---
name: test_combine_anyext_trunc_vec
body: |
bb.1:
liveins: $q0
; CHECK-LABEL: name: test_combine_anyext_trunc_vec
- ; CHECK: [[COPY:%[0-9]+]]:_(<2 x s64>) = COPY $q0
- ; CHECK: $q0 = COPY [[COPY]](<2 x s64>)
+ ; CHECK: liveins: $q0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<2 x s64>) = COPY $q0
+ ; CHECK-NEXT: $q0 = COPY [[COPY]](<2 x s64>)
%0:_(<2 x s64>) = COPY $q0
%1:_(<2 x s32>) = G_TRUNC %0(<2 x s64>)
%2:_(<2 x s64>) = G_ANYEXT %1(<2 x s32>)
@@ -32,23 +52,44 @@ body: |
bb.1:
liveins: $h0
; CHECK-LABEL: name: test_combine_anyext_anyext
- ; CHECK: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
- ; CHECK: [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[COPY]](s16)
- ; CHECK: $x0 = COPY [[ANYEXT]](s64)
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[ANYEXT]](s64)
%0:_(s16) = COPY $h0
%1:_(s32) = G_ANYEXT %0(s16)
%2:_(s64) = G_ANYEXT %1(s32)
$x0 = COPY %2(s64)
...
---
+name: test_combine_anyext_anyext_with_hint
+body: |
+ bb.1:
+ liveins: $h0
+ ; CHECK-LABEL: name: test_combine_anyext_anyext_with_hint
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[ANYEXT]](s64)
+ %0:_(s16) = COPY $h0
+ %1:_(s32) = G_ANYEXT %0(s16)
+ %2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
+ %3:_(s64) = G_ANYEXT %2(s32)
+ $x0 = COPY %3(s64)
+...
+---
name: test_combine_anyext_anyext_vec
body: |
bb.1:
liveins: $s0
; CHECK-LABEL: name: test_combine_anyext_anyext_vec
- ; CHECK: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
- ; CHECK: [[ANYEXT:%[0-9]+]]:_(<2 x s64>) = G_ANYEXT [[COPY]](<2 x s16>)
- ; CHECK: $q0 = COPY [[ANYEXT]](<2 x s64>)
+ ; CHECK: liveins: $s0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
+ ; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(<2 x s64>) = G_ANYEXT [[COPY]](<2 x s16>)
+ ; CHECK-NEXT: $q0 = COPY [[ANYEXT]](<2 x s64>)
%0:_(<2 x s16>) = COPY $s0
%1:_(<2 x s32>) = G_ANYEXT %0(<2 x s16>)
%2:_(<2 x s64>) = G_ANYEXT %1(<2 x s32>)
@@ -60,23 +101,44 @@ body: |
bb.1:
liveins: $h0
; CHECK-LABEL: name: test_combine_anyext_sext
- ; CHECK: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
- ; CHECK: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY]](s16)
- ; CHECK: $x0 = COPY [[SEXT]](s64)
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[SEXT]](s64)
%0:_(s16) = COPY $h0
%1:_(s32) = G_SEXT %0(s16)
%2:_(s64) = G_ANYEXT %1(s32)
$x0 = COPY %2(s64)
...
---
+name: test_combine_anyext_sext_with_hint
+body: |
+ bb.1:
+ liveins: $h0
+ ; CHECK-LABEL: name: test_combine_anyext_sext_with_hint
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[SEXT]](s64)
+ %0:_(s16) = COPY $h0
+ %1:_(s32) = G_SEXT %0(s16)
+ %2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
+ %3:_(s64) = G_ANYEXT %2(s32)
+ $x0 = COPY %3(s64)
+...
+---
name: test_combine_anyext_sext_vec
body: |
bb.1:
liveins: $s0
; CHECK-LABEL: name: test_combine_anyext_sext_vec
- ; CHECK: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
- ; CHECK: [[SEXT:%[0-9]+]]:_(<2 x s64>) = G_SEXT [[COPY]](<2 x s16>)
- ; CHECK: $q0 = COPY [[SEXT]](<2 x s64>)
+ ; CHECK: liveins: $s0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
+ ; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(<2 x s64>) = G_SEXT [[COPY]](<2 x s16>)
+ ; CHECK-NEXT: $q0 = COPY [[SEXT]](<2 x s64>)
%0:_(<2 x s16>) = COPY $s0
%1:_(<2 x s32>) = G_SEXT %0(<2 x s16>)
%2:_(<2 x s64>) = G_ANYEXT %1(<2 x s32>)
@@ -88,23 +150,44 @@ body: |
bb.1:
liveins: $h0
; CHECK-LABEL: name: test_combine_anyext_zext
- ; CHECK: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
- ; CHECK: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
- ; CHECK: $x0 = COPY [[ZEXT]](s64)
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[ZEXT]](s64)
%0:_(s16) = COPY $h0
%1:_(s32) = G_ZEXT %0(s16)
%2:_(s64) = G_ANYEXT %1(s32)
$x0 = COPY %2(s64)
...
---
+name: test_combine_anyext_zext_with_hint
+body: |
+ bb.1:
+ liveins: $h0
+ ; CHECK-LABEL: name: test_combine_anyext_zext_with_hint
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[ZEXT]](s64)
+ %0:_(s16) = COPY $h0
+ %1:_(s32) = G_ZEXT %0(s16)
+ %2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
+ %3:_(s64) = G_ANYEXT %2(s32)
+ $x0 = COPY %3(s64)
+...
+---
name: test_combine_anyext_zext_vec
body: |
bb.1:
liveins: $s0
; CHECK-LABEL: name: test_combine_anyext_zext_vec
- ; CHECK: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
- ; CHECK: [[ZEXT:%[0-9]+]]:_(<2 x s64>) = G_ZEXT [[COPY]](<2 x s16>)
- ; CHECK: $q0 = COPY [[ZEXT]](<2 x s64>)
+ ; CHECK: liveins: $s0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
+ ; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(<2 x s64>) = G_ZEXT [[COPY]](<2 x s16>)
+ ; CHECK-NEXT: $q0 = COPY [[ZEXT]](<2 x s64>)
%0:_(<2 x s16>) = COPY $s0
%1:_(<2 x s32>) = G_ZEXT %0(<2 x s16>)
%2:_(<2 x s64>) = G_ANYEXT %1(<2 x s32>)
@@ -116,23 +199,44 @@ body: |
bb.1:
liveins: $h0
; CHECK-LABEL: name: test_combine_sext_sext
- ; CHECK: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
- ; CHECK: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY]](s16)
- ; CHECK: $x0 = COPY [[SEXT]](s64)
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[SEXT]](s64)
%0:_(s16) = COPY $h0
%1:_(s32) = G_SEXT %0(s16)
%2:_(s64) = G_SEXT %1(s32)
$x0 = COPY %2(s64)
...
---
+name: test_combine_sext_sext_with_hint
+body: |
+ bb.1:
+ liveins: $h0
+ ; CHECK-LABEL: name: test_combine_sext_sext_with_hint
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[SEXT]](s64)
+ %0:_(s16) = COPY $h0
+ %1:_(s32) = G_SEXT %0(s16)
+ %2:_(s32) = G_ASSERT_SEXT %1(s32), 11
+ %3:_(s64) = G_SEXT %2(s32)
+ $x0 = COPY %3(s64)
+...
+---
name: test_combine_sext_sext_vec
body: |
bb.1:
liveins: $s0
; CHECK-LABEL: name: test_combine_sext_sext_vec
- ; CHECK: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
- ; CHECK: [[SEXT:%[0-9]+]]:_(<2 x s64>) = G_SEXT [[COPY]](<2 x s16>)
- ; CHECK: $q0 = COPY [[SEXT]](<2 x s64>)
+ ; CHECK: liveins: $s0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
+ ; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(<2 x s64>) = G_SEXT [[COPY]](<2 x s16>)
+ ; CHECK-NEXT: $q0 = COPY [[SEXT]](<2 x s64>)
%0:_(<2 x s16>) = COPY $s0
%1:_(<2 x s32>) = G_SEXT %0(<2 x s16>)
%2:_(<2 x s64>) = G_SEXT %1(<2 x s32>)
@@ -144,23 +248,44 @@ body: |
bb.1:
liveins: $h0
; CHECK-LABEL: name: test_combine_sext_zext
- ; CHECK: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
- ; CHECK: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
- ; CHECK: $x0 = COPY [[ZEXT]](s64)
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[ZEXT]](s64)
%0:_(s16) = COPY $h0
%1:_(s32) = G_ZEXT %0(s16)
%2:_(s64) = G_SEXT %1(s32)
$x0 = COPY %2(s64)
...
---
+name: test_combine_sext_zext_with_hint
+body: |
+ bb.1:
+ liveins: $h0
+ ; CHECK-LABEL: name: test_combine_sext_zext_with_hint
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[ZEXT]](s64)
+ %0:_(s16) = COPY $h0
+ %1:_(s32) = G_ZEXT %0(s16)
+ %2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
+ %3:_(s64) = G_SEXT %2(s32)
+ $x0 = COPY %3(s64)
+...
+---
name: test_combine_sext_zext_vec
body: |
bb.1:
liveins: $s0
; CHECK-LABEL: name: test_combine_sext_zext_vec
- ; CHECK: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
- ; CHECK: [[ZEXT:%[0-9]+]]:_(<2 x s64>) = G_ZEXT [[COPY]](<2 x s16>)
- ; CHECK: $q0 = COPY [[ZEXT]](<2 x s64>)
+ ; CHECK: liveins: $s0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
+ ; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(<2 x s64>) = G_ZEXT [[COPY]](<2 x s16>)
+ ; CHECK-NEXT: $q0 = COPY [[ZEXT]](<2 x s64>)
%0:_(<2 x s16>) = COPY $s0
%1:_(<2 x s32>) = G_ZEXT %0(<2 x s16>)
%2:_(<2 x s64>) = G_SEXT %1(<2 x s32>)
@@ -172,23 +297,44 @@ body: |
bb.1:
liveins: $h0
; CHECK-LABEL: name: test_combine_zext_zext
- ; CHECK: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
- ; CHECK: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
- ; CHECK: $x0 = COPY [[ZEXT]](s64)
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[ZEXT]](s64)
%0:_(s16) = COPY $h0
%1:_(s32) = G_ZEXT %0(s16)
%2:_(s64) = G_ZEXT %1(s32)
$x0 = COPY %2(s64)
...
---
+name: test_combine_zext_zext_with_hint
+body: |
+ bb.1:
+ liveins: $h0
+ ; CHECK-LABEL: name: test_combine_zext_zext_with_hint
+ ; CHECK: liveins: $h0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
+ ; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
+ ; CHECK-NEXT: $x0 = COPY [[ZEXT]](s64)
+ %0:_(s16) = COPY $h0
+ %1:_(s32) = G_ZEXT %0(s16)
+ %2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
+ %3:_(s64) = G_ZEXT %2(s32)
+ $x0 = COPY %3(s64)
+...
+---
name: test_combine_zext_zext_vec
body: |
bb.1:
liveins: $s0
; CHECK-LABEL: name: test_combine_zext_zext_vec
- ; CHECK: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
- ; CHECK: [[ZEXT:%[0-9]+]]:_(<2 x s64>) = G_ZEXT [[COPY]](<2 x s16>)
- ; CHECK: $q0 = COPY [[ZEXT]](<2 x s64>)
+ ; CHECK: liveins: $s0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(<2 x s16>) = COPY $s0
+ ; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(<2 x s64>) = G_ZEXT [[COPY]](<2 x s16>)
+ ; CHECK-NEXT: $q0 = COPY [[ZEXT]](<2 x s64>)
%0:_(<2 x s16>) = COPY $s0
%1:_(<2 x s32>) = G_ZEXT %0(<2 x s16>)
%2:_(<2 x s64>) = G_ZEXT %1(<2 x s32>)
More information about the llvm-commits
mailing list