Skip to content

Commit fe07eeb

Browse files
committed
[GlobalISel] Fix crash in applyShiftOfShiftedLogic caused by CSEMIRBuilder reuse instruction
If LogicNonShiftReg is the same to Shift1Base, and shift1 const is the same to MatchInfo.Shift2 const, CSEMIRBuilder will reuse the old shift1 when build shift2. So, if we erase MatchInfo.Shift2 at the end, actually we remove old shift1. And it will cause crash later. Solution for this issue is just erase it earlier to avoid the crash. Fix llvm#58423 Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D138187
1 parent 00be357 commit fe07eeb

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,6 +1598,13 @@ void CombinerHelper::applyShiftOfShiftedLogic(MachineInstr &MI,
15981598
Register Shift1 =
15991599
Builder.buildInstr(Opcode, {DestType}, {Shift1Base, Const}).getReg(0);
16001600

1601+
// If LogicNonShiftReg is the same to Shift1Base, and shift1 const is the same
1602+
// to MatchInfo.Shift2 const, CSEMIRBuilder will reuse the old shift1 when
1603+
// build shift2. So, if we erase MatchInfo.Shift2 at the end, actually we
1604+
// remove old shift1. And it will cause crash later. So erase it earlier to
1605+
// avoid the crash.
1606+
MatchInfo.Shift2->eraseFromParent();
1607+
16011608
Register Shift2Const = MI.getOperand(2).getReg();
16021609
Register Shift2 = Builder
16031610
.buildInstr(Opcode, {DestType},
@@ -1607,8 +1614,7 @@ void CombinerHelper::applyShiftOfShiftedLogic(MachineInstr &MI,
16071614
Register Dest = MI.getOperand(0).getReg();
16081615
Builder.buildInstr(MatchInfo.Logic->getOpcode(), {Dest}, {Shift1, Shift2});
16091616

1610-
// These were one use so it's safe to remove them.
1611-
MatchInfo.Shift2->eraseFromParent();
1617+
// This was one use so it's safe to remove it.
16121618
MatchInfo.Logic->eraseFromParent();
16131619

16141620
MI.eraseFromParent();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc < %s -mtriple=aarch64-none-linux-gnu -global-isel -global-isel-abort=0 | FileCheck %s
3+
4+
; this used to crash
5+
define i32 @f(i32 %a) {
6+
; CHECK-LABEL: f:
7+
; CHECK: // %bb.0:
8+
; CHECK-NEXT: lsl w8, w0, #8
9+
; CHECK-NEXT: orr w0, w8, w0, lsl #16
10+
; CHECK-NEXT: ret
11+
%shl = shl i32 %a, 8
12+
%or = or i32 %a, %shl
13+
%r = shl i32 %or, 8
14+
ret i32 %r
15+
}

0 commit comments

Comments
 (0)