Skip to content

Commit 20e37f0

Browse files
authored
[GISel] Don't preserve NSW flag when converting G_MUL of INT_MIN to G_SHL. (#111230)
mul and shl have different meanings for the nsw flag. We need to drop it when converting a multiply by the minimum negative value.
1 parent bcb15d0 commit 20e37f0

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -2036,6 +2036,8 @@ void CombinerHelper::applyCombineMulToShl(MachineInstr &MI,
20362036
Observer.changingInstr(MI);
20372037
MI.setDesc(MIB.getTII().get(TargetOpcode::G_SHL));
20382038
MI.getOperand(2).setReg(ShiftCst.getReg(0));
2039+
if (ShiftVal == ShiftTy.getScalarSizeInBits() - 1)
2040+
MI.clearFlag(MachineInstr::MIFlag::NoSWrap);
20392041
Observer.changedInstr(MI);
20402042
}
20412043

llvm/test/CodeGen/AArch64/GlobalISel/combine-mul-to-shl.mir

+48
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,51 @@ body: |
9696
$x0 = COPY %2(s64)
9797
RET_ReallyLR implicit-def $x0
9898
...
99+
---
100+
name: mul_to_shl_16_nuw_nsw
101+
alignment: 4
102+
tracksRegLiveness: true
103+
frameInfo:
104+
maxAlignment: 1
105+
machineFunctionInfo: {}
106+
body: |
107+
bb.0:
108+
liveins: $x0
109+
; CHECK-LABEL: name: mul_to_shl_16_nuw_nsw
110+
; CHECK: liveins: $x0
111+
; CHECK-NEXT: {{ $}}
112+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
113+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 4
114+
; CHECK-NEXT: [[SHL:%[0-9]+]]:_(s64) = nuw nsw G_SHL [[COPY]], [[C]](s64)
115+
; CHECK-NEXT: $x0 = COPY [[SHL]](s64)
116+
; CHECK-NEXT: RET_ReallyLR implicit-def $x0
117+
%0:_(s64) = COPY $x0
118+
%1:_(s64) = G_CONSTANT i64 16
119+
%2:_(s64) = nuw nsw G_MUL %0, %1(s64)
120+
$x0 = COPY %2(s64)
121+
RET_ReallyLR implicit-def $x0
122+
...
123+
---
124+
name: mul_to_shl_int_min_nuw_nsw
125+
alignment: 4
126+
tracksRegLiveness: true
127+
frameInfo:
128+
maxAlignment: 1
129+
machineFunctionInfo: {}
130+
body: |
131+
bb.0:
132+
liveins: $x0
133+
; CHECK-LABEL: name: mul_to_shl_int_min_nuw_nsw
134+
; CHECK: liveins: $x0
135+
; CHECK-NEXT: {{ $}}
136+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
137+
; CHECK-NEXT: [[C:%[0-9]+]]:_(s64) = G_CONSTANT i64 63
138+
; CHECK-NEXT: [[SHL:%[0-9]+]]:_(s64) = nuw G_SHL [[COPY]], [[C]](s64)
139+
; CHECK-NEXT: $x0 = COPY [[SHL]](s64)
140+
; CHECK-NEXT: RET_ReallyLR implicit-def $x0
141+
%0:_(s64) = COPY $x0
142+
%1:_(s64) = G_CONSTANT i64 -9223372036854775808
143+
%2:_(s64) = nuw nsw G_MUL %0, %1(s64)
144+
$x0 = COPY %2(s64)
145+
RET_ReallyLR implicit-def $x0
146+
...

0 commit comments

Comments
 (0)