Skip to content

Commit 55172b7

Browse files
authored
[GlobalISel] Improve combines for extend operation by taking hint ins… (#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.
1 parent da0755f commit 55172b7

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,6 +2374,9 @@ bool CombinerHelper::matchCombineAnyExtTrunc(MachineInstr &MI, Register &Reg) {
23742374
assert(MI.getOpcode() == TargetOpcode::G_ANYEXT && "Expected a G_ANYEXT");
23752375
Register DstReg = MI.getOperand(0).getReg();
23762376
Register SrcReg = MI.getOperand(1).getReg();
2377+
Register OriginalSrcReg = getSrcRegIgnoringCopies(SrcReg, MRI);
2378+
if (OriginalSrcReg.isValid())
2379+
SrcReg = OriginalSrcReg;
23772380
LLT DstTy = MRI.getType(DstReg);
23782381
return mi_match(SrcReg, MRI,
23792382
m_GTrunc(m_all_of(m_Reg(Reg), m_SpecificType(DstTy))));
@@ -2400,6 +2403,9 @@ bool CombinerHelper::matchCombineExtOfExt(
24002403
MI.getOpcode() == TargetOpcode::G_ZEXT) &&
24012404
"Expected a G_[ASZ]EXT");
24022405
Register SrcReg = MI.getOperand(1).getReg();
2406+
Register OriginalSrcReg = getSrcRegIgnoringCopies(SrcReg, MRI);
2407+
if (OriginalSrcReg.isValid())
2408+
SrcReg = OriginalSrcReg;
24032409
MachineInstr *SrcMI = MRI.getVRegDef(SrcReg);
24042410
// Match exts with the same opcode, anyext([sz]ext) and sext(zext).
24052411
unsigned Opc = MI.getOpcode();

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ body: |
1616
$x1 = COPY %2(s64)
1717
...
1818
---
19+
name: test_combine_anyext_trunc_with_hint
20+
body: |
21+
bb.1:
22+
liveins: $x0
23+
; CHECK-LABEL: name: test_combine_anyext_trunc_with_hint
24+
; CHECK: liveins: $x0
25+
; CHECK-NEXT: {{ $}}
26+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
27+
; CHECK-NEXT: $x1 = COPY [[COPY]](s64)
28+
%0:_(s64) = COPY $x0
29+
%1:_(s32) = G_TRUNC %0(s64)
30+
%2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
31+
%3:_(s64) = G_ANYEXT %2(s32)
32+
$x1 = COPY %3(s64)
33+
...
34+
---
1935
name: test_combine_anyext_trunc_vec
2036
body: |
2137
bb.1:
@@ -47,6 +63,23 @@ body: |
4763
$x0 = COPY %2(s64)
4864
...
4965
---
66+
name: test_combine_anyext_anyext_with_hint
67+
body: |
68+
bb.1:
69+
liveins: $h0
70+
; CHECK-LABEL: name: test_combine_anyext_anyext_with_hint
71+
; CHECK: liveins: $h0
72+
; CHECK-NEXT: {{ $}}
73+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
74+
; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[COPY]](s16)
75+
; CHECK-NEXT: $x0 = COPY [[ANYEXT]](s64)
76+
%0:_(s16) = COPY $h0
77+
%1:_(s32) = G_ANYEXT %0(s16)
78+
%2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
79+
%3:_(s64) = G_ANYEXT %2(s32)
80+
$x0 = COPY %3(s64)
81+
...
82+
---
5083
name: test_combine_anyext_anyext_vec
5184
body: |
5285
bb.1:
@@ -79,6 +112,23 @@ body: |
79112
$x0 = COPY %2(s64)
80113
...
81114
---
115+
name: test_combine_anyext_sext_with_hint
116+
body: |
117+
bb.1:
118+
liveins: $h0
119+
; CHECK-LABEL: name: test_combine_anyext_sext_with_hint
120+
; CHECK: liveins: $h0
121+
; CHECK-NEXT: {{ $}}
122+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
123+
; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY]](s16)
124+
; CHECK-NEXT: $x0 = COPY [[SEXT]](s64)
125+
%0:_(s16) = COPY $h0
126+
%1:_(s32) = G_SEXT %0(s16)
127+
%2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
128+
%3:_(s64) = G_ANYEXT %2(s32)
129+
$x0 = COPY %3(s64)
130+
...
131+
---
82132
name: test_combine_anyext_sext_vec
83133
body: |
84134
bb.1:
@@ -111,6 +161,23 @@ body: |
111161
$x0 = COPY %2(s64)
112162
...
113163
---
164+
name: test_combine_anyext_zext_with_hint
165+
body: |
166+
bb.1:
167+
liveins: $h0
168+
; CHECK-LABEL: name: test_combine_anyext_zext_with_hint
169+
; CHECK: liveins: $h0
170+
; CHECK-NEXT: {{ $}}
171+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
172+
; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
173+
; CHECK-NEXT: $x0 = COPY [[ZEXT]](s64)
174+
%0:_(s16) = COPY $h0
175+
%1:_(s32) = G_ZEXT %0(s16)
176+
%2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
177+
%3:_(s64) = G_ANYEXT %2(s32)
178+
$x0 = COPY %3(s64)
179+
...
180+
---
114181
name: test_combine_anyext_zext_vec
115182
body: |
116183
bb.1:
@@ -143,6 +210,23 @@ body: |
143210
$x0 = COPY %2(s64)
144211
...
145212
---
213+
name: test_combine_sext_sext_with_hint
214+
body: |
215+
bb.1:
216+
liveins: $h0
217+
; CHECK-LABEL: name: test_combine_sext_sext_with_hint
218+
; CHECK: liveins: $h0
219+
; CHECK-NEXT: {{ $}}
220+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
221+
; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY]](s16)
222+
; CHECK-NEXT: $x0 = COPY [[SEXT]](s64)
223+
%0:_(s16) = COPY $h0
224+
%1:_(s32) = G_SEXT %0(s16)
225+
%2:_(s32) = G_ASSERT_SEXT %1(s32), 11
226+
%3:_(s64) = G_SEXT %2(s32)
227+
$x0 = COPY %3(s64)
228+
...
229+
---
146230
name: test_combine_sext_sext_vec
147231
body: |
148232
bb.1:
@@ -175,6 +259,23 @@ body: |
175259
$x0 = COPY %2(s64)
176260
...
177261
---
262+
name: test_combine_sext_zext_with_hint
263+
body: |
264+
bb.1:
265+
liveins: $h0
266+
; CHECK-LABEL: name: test_combine_sext_zext_with_hint
267+
; CHECK: liveins: $h0
268+
; CHECK-NEXT: {{ $}}
269+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
270+
; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
271+
; CHECK-NEXT: $x0 = COPY [[ZEXT]](s64)
272+
%0:_(s16) = COPY $h0
273+
%1:_(s32) = G_ZEXT %0(s16)
274+
%2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
275+
%3:_(s64) = G_SEXT %2(s32)
276+
$x0 = COPY %3(s64)
277+
...
278+
---
178279
name: test_combine_sext_zext_vec
179280
body: |
180281
bb.1:
@@ -207,6 +308,23 @@ body: |
207308
$x0 = COPY %2(s64)
208309
...
209310
---
311+
name: test_combine_zext_zext_with_hint
312+
body: |
313+
bb.1:
314+
liveins: $h0
315+
; CHECK-LABEL: name: test_combine_zext_zext_with_hint
316+
; CHECK: liveins: $h0
317+
; CHECK-NEXT: {{ $}}
318+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s16) = COPY $h0
319+
; CHECK-NEXT: [[ZEXT:%[0-9]+]]:_(s64) = G_ZEXT [[COPY]](s16)
320+
; CHECK-NEXT: $x0 = COPY [[ZEXT]](s64)
321+
%0:_(s16) = COPY $h0
322+
%1:_(s32) = G_ZEXT %0(s16)
323+
%2:_(s32) = G_ASSERT_ZEXT %1(s32), 11
324+
%3:_(s64) = G_ZEXT %2(s32)
325+
$x0 = COPY %3(s64)
326+
...
327+
---
210328
name: test_combine_zext_zext_vec
211329
body: |
212330
bb.1:

0 commit comments

Comments
 (0)