Skip to content

Commit 24633ea

Browse files
authored
[Peephole] Check instructions from CopyMIs are still COPY (#69511)
Function foldRedundantCopy records COPY instructions in CopyMIs and uses it later. But other optimizations may delete or modify it. So before using it we should check if the extracted instruction is existing and still a COPY instruction.
1 parent 17baba9 commit 24633ea

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

llvm/lib/CodeGen/PeepholeOptimizer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,9 @@ bool PeepholeOptimizer::foldRedundantCopy(
14451445
}
14461446

14471447
MachineInstr *PrevCopy = CopyMIs.find(SrcPair)->second;
1448-
if (!LocalMIs.count(PrevCopy))
1448+
// A COPY instruction can be deleted or changed by other optimizations.
1449+
// Check if the previous COPY instruction is existing and still a COPY.
1450+
if (!LocalMIs.count(PrevCopy) || !PrevCopy->isCopy())
14491451
return false;
14501452

14511453
assert(SrcSubReg == PrevCopy->getOperand(1).getSubReg() &&
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 3
2+
# RUN: llc -mtriple=i686-- -run-pass=peephole-opt %s -o - | FileCheck %s
3+
--- |
4+
define void @c() {
5+
entry:
6+
tail call void asm sideeffect "", "q,~{dirflag},~{fpsr},~{flags}"(i32 512)
7+
tail call void asm sideeffect "", "q,~{dirflag},~{fpsr},~{flags}"(i32 512)
8+
ret void
9+
}
10+
...
11+
---
12+
# In peephole optimization the modified COPY instruction should not cause
13+
# compiler failure.
14+
name: c
15+
registers:
16+
- { id: 0, class: gr32_abcd }
17+
- { id: 1, class: gr32_abcd }
18+
- { id: 2, class: gr32 }
19+
20+
body: |
21+
bb.0:
22+
; CHECK-LABEL: name: c
23+
; CHECK: [[MOV32ri:%[0-9]+]]:gr32_abcd = MOV32ri 512
24+
; CHECK-NEXT: INLINEASM &"", 1 /* sideeffect attdialect */, 2359305 /* reguse:GR32 */, [[MOV32ri]], 1 /* reguse */, implicit-def early-clobber $df
25+
; CHECK-NEXT: [[MOV32ri1:%[0-9]+]]:gr32_abcd = MOV32ri 512
26+
; CHECK-NEXT: INLINEASM &"", 1 /* sideeffect attdialect */, 2359305 /* reguse:GR32 */, [[MOV32ri1]], 1 /* reguse */, implicit-def early-clobber $df
27+
; CHECK-NEXT: RET 0
28+
%2 = MOV32ri 512
29+
%0 = COPY %2
30+
INLINEASM &"", 1 /* sideeffect attdialect */, 2359305 /* reguse:GR32_ABCD */, %0:gr32_abcd, 1 /* clobber */, implicit-def early-clobber $df
31+
%1 = COPY %2
32+
INLINEASM &"", 1 /* sideeffect attdialect */, 2359305 /* reguse:GR32_ABCD */, %1:gr32_abcd, 1 /* clobber */, implicit-def early-clobber $df
33+
RET 0
34+
...

0 commit comments

Comments
 (0)