Skip to content

Commit f686479

Browse files
authored
[GlobalISel] Adding support for handling G_ASSERT_{SEXT,ZEXT,ALIGN} i… (#74196)
…n artifact combiner These instructions are hint for optimizations and can be treated as copies and are handled as such with this change. Without it is possible to run into an assertion, since tryCombineUnmergeValues rightfully use getDefIgnoringCopies to get the source MI, which already handle these hint instructions and treat them as copies. The problem is that markDefDead only considers COPYs, which will lead to crash with assertion for cases like testUnmergeHintOfTrunc.
1 parent c56a5e8 commit f686479

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,9 @@ class LegalizationArtifactCombiner {
13661366
// Adding Use to ArtifactList.
13671367
WrapperObserver.changedInstr(Use);
13681368
break;
1369+
case TargetOpcode::G_ASSERT_SEXT:
1370+
case TargetOpcode::G_ASSERT_ZEXT:
1371+
case TargetOpcode::G_ASSERT_ALIGN:
13691372
case TargetOpcode::COPY: {
13701373
Register Copy = Use.getOperand(0).getReg();
13711374
if (Copy.isVirtual())
@@ -1392,6 +1395,9 @@ class LegalizationArtifactCombiner {
13921395
case TargetOpcode::G_ANYEXT:
13931396
case TargetOpcode::G_SEXT:
13941397
case TargetOpcode::G_EXTRACT:
1398+
case TargetOpcode::G_ASSERT_SEXT:
1399+
case TargetOpcode::G_ASSERT_ZEXT:
1400+
case TargetOpcode::G_ASSERT_ALIGN:
13951401
return MI.getOperand(1).getReg();
13961402
case TargetOpcode::G_UNMERGE_VALUES:
13971403
return MI.getOperand(MI.getNumOperands() - 1).getReg();
@@ -1425,7 +1431,8 @@ class LegalizationArtifactCombiner {
14251431
if (MRI.hasOneUse(PrevRegSrc)) {
14261432
if (TmpDef != &DefMI) {
14271433
assert((TmpDef->getOpcode() == TargetOpcode::COPY ||
1428-
isArtifactCast(TmpDef->getOpcode())) &&
1434+
isArtifactCast(TmpDef->getOpcode()) ||
1435+
isPreISelGenericOptimizationHint(TmpDef->getOpcode())) &&
14291436
"Expecting copy or artifact cast here");
14301437

14311438
DeadInsts.push_back(TmpDef);
@@ -1509,16 +1516,8 @@ class LegalizationArtifactCombiner {
15091516
/// Looks through copy instructions and returns the actual
15101517
/// source register.
15111518
Register lookThroughCopyInstrs(Register Reg) {
1512-
using namespace llvm::MIPatternMatch;
1513-
1514-
Register TmpReg;
1515-
while (mi_match(Reg, MRI, m_Copy(m_Reg(TmpReg)))) {
1516-
if (MRI.getType(TmpReg).isValid())
1517-
Reg = TmpReg;
1518-
else
1519-
break;
1520-
}
1521-
return Reg;
1519+
Register TmpReg = getSrcRegIgnoringCopies(Reg, MRI);
1520+
return TmpReg.isValid() ? TmpReg : Reg;
15221521
}
15231522
};
15241523

llvm/test/CodeGen/AArch64/GlobalISel/irtranslator-hoisted-constants.ll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,11 @@ define i32 @test(i32 %a, i1 %c) {
4646
; PRESELECTION-NEXT: {{ $}}
4747
; PRESELECTION-NEXT: [[COPY:%[0-9]+]]:gpr(s32) = COPY $w0
4848
; PRESELECTION-NEXT: [[COPY1:%[0-9]+]]:gpr(s32) = COPY $w1
49-
; PRESELECTION-NEXT: [[TRUNC:%[0-9]+]]:gpr(s8) = G_TRUNC [[COPY1]](s32)
50-
; PRESELECTION-NEXT: [[ASSERT_ZEXT:%[0-9]+]]:gpr(s8) = G_ASSERT_ZEXT [[TRUNC]], 1
5149
; PRESELECTION-NEXT: [[C:%[0-9]+]]:gpr(s32) = G_CONSTANT i32 0
5250
; PRESELECTION-NEXT: [[C1:%[0-9]+]]:gpr(s32) = G_CONSTANT i32 100000
5351
; PRESELECTION-NEXT: [[CONSTANT_FOLD_BARRIER:%[0-9]+]]:gpr(s32) = G_CONSTANT_FOLD_BARRIER [[C1]]
54-
; PRESELECTION-NEXT: [[ANYEXT:%[0-9]+]]:gpr(s32) = G_ANYEXT [[ASSERT_ZEXT]](s8)
5552
; PRESELECTION-NEXT: [[C2:%[0-9]+]]:gpr(s32) = G_CONSTANT i32 1
56-
; PRESELECTION-NEXT: [[AND:%[0-9]+]]:gpr(s32) = G_AND [[ANYEXT]], [[C2]]
53+
; PRESELECTION-NEXT: [[AND:%[0-9]+]]:gpr(s32) = G_AND [[COPY1]], [[C2]]
5754
; PRESELECTION-NEXT: G_BRCOND [[AND]](s32), %bb.3
5855
; PRESELECTION-NEXT: G_BR %bb.2
5956
; PRESELECTION-NEXT: {{ $}}

llvm/test/CodeGen/AArch64/GlobalISel/legalize-simple.mir

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,47 @@ body: |
136136
$x0 = COPY %3(s64)
137137
RET_ReallyLR implicit $x0
138138
139+
...
140+
---
141+
name: testExtOfHintOfTrunc
142+
body: |
143+
bb.0:
144+
liveins: $x0
145+
146+
; CHECK-LABEL: name: testExtOfHintOfTrunc
147+
; CHECK: liveins: $x0
148+
; CHECK-NEXT: {{ $}}
149+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
150+
; CHECK-NEXT: $x0 = COPY [[COPY]](s64)
151+
; CHECK-NEXT: RET_ReallyLR implicit $x0
152+
%0:_(s64) = COPY $x0
153+
%1:_(s8) = G_TRUNC %0(s64)
154+
%2:_(s8) = G_ASSERT_ZEXT %1(s8), 1
155+
%3:_(s64) = G_ANYEXT %2(s8)
156+
$x0 = COPY %3(s64)
157+
RET_ReallyLR implicit $x0
158+
159+
...
160+
---
161+
name: testExtOfCopyAndHintOfTrunc
162+
body: |
163+
bb.0:
164+
liveins: $x0
165+
166+
; CHECK-LABEL: name: testExtOfCopyAndHintOfTrunc
167+
; CHECK: liveins: $x0
168+
; CHECK-NEXT: {{ $}}
169+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
170+
; CHECK-NEXT: $x0 = COPY [[COPY]](s64)
171+
; CHECK-NEXT: RET_ReallyLR implicit $x0
172+
%0:_(s64) = COPY $x0
173+
%1:_(s8) = G_TRUNC %0(s64)
174+
%2:_(s8) = COPY %1(s8)
175+
%3:_(s8) = G_ASSERT_ZEXT %2(s8), 1
176+
%4:_(s64) = G_ANYEXT %3(s8)
177+
$x0 = COPY %4(s64)
178+
RET_ReallyLR implicit $x0
179+
139180
...
140181
---
141182
name: testExtOf2CopyOfTrunc
@@ -158,3 +199,26 @@ body: |
158199
RET_ReallyLR implicit $x0
159200
160201
...
202+
---
203+
name: testUnmergeHintOfTrunc
204+
body: |
205+
bb.0:
206+
liveins: $x0
207+
208+
; CHECK-LABEL: name: testUnmergeHintOfTrunc
209+
; CHECK: liveins: $x0
210+
; CHECK-NEXT: {{ $}}
211+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
212+
; CHECK-NEXT: [[UV:%[0-9]+]]:_(s8), [[UV1:%[0-9]+]]:_(s8), [[UV2:%[0-9]+]]:_(s8), [[UV3:%[0-9]+]]:_(s8), [[UV4:%[0-9]+]]:_(s8), [[UV5:%[0-9]+]]:_(s8), [[UV6:%[0-9]+]]:_(s8), [[UV7:%[0-9]+]]:_(s8) = G_UNMERGE_VALUES [[COPY]](s64)
213+
; CHECK-NEXT: [[ANYEXT:%[0-9]+]]:_(s64) = G_ANYEXT [[UV1]](s8)
214+
; CHECK-NEXT: $x0 = COPY [[ANYEXT]](s64)
215+
; CHECK-NEXT: RET_ReallyLR implicit $x0
216+
%0:_(s64) = COPY $x0
217+
%1:_(s16) = G_TRUNC %0(s64)
218+
%2:_(s16) = G_ASSERT_ZEXT %1(s16), 1
219+
%3:_(s8), %4:_(s8) = G_UNMERGE_VALUES %2(s16)
220+
%5:_(s64) = G_ANYEXT %4(s8)
221+
$x0 = COPY %5(s64)
222+
RET_ReallyLR implicit $x0
223+
224+
...

0 commit comments

Comments
 (0)