Skip to content

Commit 20bf0cf

Browse files
committed
[TargetLowering] optimizeSetCCToComparisonWithZero(): add extra sanity checks (PR43769)
We should do the fold only if both constants are plain, non-opaque constants, at least that is the DAG.FoldConstantArithmetic() requirement. And if the constant we are comparing with is zero - we shouldn't be trying to do this fold in the first place. Fixes https://bugs.llvm.org/show_bug.cgi?id=43769
1 parent d052a57 commit 20bf0cf

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3052,6 +3052,10 @@ SDValue TargetLowering::optimizeSetCCToComparisonWithZero(
30523052
assert((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
30533053
"Only for equality-comparisons.");
30543054

3055+
// The constant we are comparing with must be a non-zero, non-opaque constant.
3056+
if (N1C->isNullValue() || N1C->isOpaque())
3057+
return SDValue();
3058+
30553059
// LHS should not be used elsewhere, to avoid creating an extra node.
30563060
if (!N0.hasOneUse())
30573061
return SDValue();
@@ -3072,9 +3076,9 @@ SDValue TargetLowering::optimizeSetCCToComparisonWithZero(
30723076
break;
30733077
}
30743078

3075-
// Second operand must be a constant.
3079+
// Second operand must be a non-opaque constant.
30763080
ConstantSDNode *N01C = isConstOrConstSplat(N0.getOperand(1));
3077-
if (!N01C)
3081+
if (!N01C || N01C->isOpaque())
30783082
return SDValue();
30793083

30803084
// And let's be even more specific for now, it must be a zero constant.

llvm/test/CodeGen/X86/pr43769.ll

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; RUN: llc -mtriple=i686-w64-windows-gnu %s -o - | FileCheck %s
3+
4+
; Reduced from https://bugs.llvm.org/show_bug.cgi?id=43769
5+
6+
define i32 @b(i32 %a, i32* %c, i32 %d) {
7+
; CHECK-LABEL: b:
8+
; CHECK: # %bb.0: # %entry
9+
; CHECK-NEXT: cmpl $0, {{[0-9]+}}(%esp)
10+
; CHECK-NEXT: je LBB0_4
11+
; CHECK-NEXT: # %bb.1: # %for.body.lr.ph
12+
; CHECK-NEXT: movl {{[0-9]+}}(%esp), %eax
13+
; CHECK-NEXT: movl %eax, %ecx
14+
; CHECK-NEXT: sarl $31, %ecx
15+
; CHECK-NEXT: addl $-2147483647, %eax # imm = 0x80000001
16+
; CHECK-NEXT: adcl $0, %ecx
17+
; CHECK-NEXT: jne LBB0_4
18+
; CHECK-NEXT: # %bb.2: # %for.body.lr.ph.peel.newph
19+
; CHECK-NEXT: movl {{[0-9]+}}(%esp), %ecx
20+
; CHECK-NEXT: movl $-2147483647, %edx # imm = 0x80000001
21+
; CHECK-NEXT: movl %ecx, %eax
22+
; CHECK-NEXT: sarl $31, %eax
23+
; CHECK-NEXT: addl %ecx, %edx
24+
; CHECK-NEXT: adcl $0, %eax
25+
; CHECK-NEXT: .p2align 4, 0x90
26+
; CHECK-NEXT: LBB0_3: # %for.body
27+
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1
28+
; CHECK-NEXT: testl %eax, %eax
29+
; CHECK-NEXT: je LBB0_3
30+
; CHECK-NEXT: LBB0_4: # %for.end
31+
; CHECK-NEXT: retl
32+
entry:
33+
%tobool3 = icmp eq i32 %a, 0
34+
br i1 %tobool3, label %for.end, label %for.body.lr.ph
35+
36+
for.body.lr.ph: ; preds = %entry
37+
%0 = ptrtoint i32* %c to i32
38+
%conv.peel = sext i32 %d to i64
39+
%add.peel = add nsw i64 %conv.peel, 2147483649
40+
%tobool1.peel = icmp ugt i64 %add.peel, 4294967295
41+
br i1 %tobool1.peel, label %for.end, label %for.body.lr.ph.peel.newph
42+
43+
for.body.lr.ph.peel.newph: ; preds = %for.body.lr.ph
44+
%conv = sext i32 %0 to i64
45+
%add = add nsw i64 %conv, 2147483649
46+
%tobool1 = icmp ugt i64 %add, 4294967295
47+
br label %for.body
48+
49+
for.body: ; preds = %for.body, %for.body.lr.ph.peel.newph
50+
br i1 %tobool1, label %for.end, label %for.body
51+
52+
for.end: ; preds = %for.body.lr.ph, %for.body, %entry
53+
ret i32 undef
54+
}

0 commit comments

Comments
 (0)