Skip to content

Commit 0787100

Browse files
committedJul 18, 2017
[DAG] Avoid deleting nodes before combining them.
When replacing a node and it's operand, replacing the operand node may cause the deletion of the original node leading to an assertion failure. Case around these replacements to avoid this without relying on inspecting the DELETED_NODE opcode in various extend dagcombiner cases. Fixes PR32515. Reviewers: dbabokin, RKSimon, davide, chandlerc Subscribers: chandlerc, llvm-commits Differential Revision: https://reviews.llvm.org/D34095 llvm-svn: 308330
1 parent afe8549 commit 0787100

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed
 

‎llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

+26-7
Original file line numberDiff line numberDiff line change
@@ -7171,7 +7171,11 @@ SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
71717171
SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
71727172
N0.getValueType(), ExtLoad);
71737173
ExtendSetCCUses(SetCCs, Trunc, ExtLoad, DL, ISD::SIGN_EXTEND);
7174-
CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
7174+
// If the load value is used only by N, replace it via CombineTo N.
7175+
if (N0.getValue(0).hasOneUse())
7176+
DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
7177+
else
7178+
CombineTo(LN0, Trunc, ExtLoad.getValue(1));
71757179
return CombineTo(N, ExtLoad); // Return N so it doesn't get rechecked!
71767180
}
71777181
}
@@ -7229,7 +7233,11 @@ SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
72297233
SDLoc(N0.getOperand(0)),
72307234
N0.getOperand(0).getValueType(), ExtLoad);
72317235
ExtendSetCCUses(SetCCs, Trunc, ExtLoad, DL, ISD::SIGN_EXTEND);
7232-
CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
7236+
// If the load value is used only by N, replace it via CombineTo N.
7237+
if (SDValue(LN0, 0).hasOneUse())
7238+
DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
7239+
else
7240+
CombineTo(LN0, Trunc, ExtLoad.getValue(1));
72337241
return CombineTo(N, And); // Return N so it doesn't get rechecked!
72347242
}
72357243
}
@@ -7470,7 +7478,11 @@ SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
74707478
SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
74717479
N0.getValueType(), ExtLoad);
74727480
ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N), ISD::ZERO_EXTEND);
7473-
CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
7481+
// If the load value is used only by N, replace it via CombineTo N.
7482+
if (SDValue(LN0, 0).hasOneUse())
7483+
DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
7484+
else
7485+
CombineTo(LN0, Trunc, ExtLoad.getValue(1));
74747486
return CombineTo(N, ExtLoad); // Return N so it doesn't get rechecked!
74757487
}
74767488
}
@@ -7522,7 +7534,11 @@ SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
75227534
SDLoc(N0.getOperand(0)),
75237535
N0.getOperand(0).getValueType(), ExtLoad);
75247536
ExtendSetCCUses(SetCCs, Trunc, ExtLoad, DL, ISD::ZERO_EXTEND);
7525-
CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
7537+
// If the load value is used only by N, replace it via CombineTo N.
7538+
if (SDValue(LN0, 0).hasOneUse())
7539+
DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
7540+
else
7541+
CombineTo(LN0, Trunc, ExtLoad.getValue(1));
75267542
return CombineTo(N, And); // Return N so it doesn't get rechecked!
75277543
}
75287544
}
@@ -7695,13 +7711,16 @@ SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
76957711
LN0->getChain(),
76967712
LN0->getBasePtr(), N0.getValueType(),
76977713
LN0->getMemOperand());
7698-
CombineTo(N, ExtLoad);
76997714
SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
77007715
N0.getValueType(), ExtLoad);
7701-
CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
77027716
ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
77037717
ISD::ANY_EXTEND);
7704-
return SDValue(N, 0); // Return N so it doesn't get rechecked!
7718+
// If the load value is used only by N, replace it via CombineTo N.
7719+
if (N0.hasOneUse())
7720+
DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), ExtLoad.getValue(1));
7721+
else
7722+
CombineTo(LN0, Trunc, ExtLoad.getValue(1));
7723+
return CombineTo(N, ExtLoad); // Return N so it doesn't get rechecked!
77057724
}
77067725
}
77077726

‎llvm/test/CodeGen/X86/avx512-mask-op.ll

+3-2
Original file line numberDiff line numberDiff line change
@@ -1630,8 +1630,9 @@ define void @f1(i32 %c) {
16301630
; CHECK-LABEL: f1:
16311631
; CHECK: ## BB#0: ## %entry
16321632
; CHECK-NEXT: movzbl {{.*}}(%rip), %edi
1633-
; CHECK-NEXT: movl %edi, %eax
1634-
; CHECK-NEXT: xorb $1, %al
1633+
; CHECK-NEXT: movb {{.*}}(%rip), %al
1634+
; CHECK-NEXT: notb %al
1635+
; CHECK-NEXT: andb $1, %al
16351636
; CHECK-NEXT: movb %al, {{.*}}(%rip)
16361637
; CHECK-NEXT: xorl $1, %edi
16371638
; CHECK-NEXT: jmp _f2 ## TAILCALL

‎llvm/test/CodeGen/X86/pr32515.ll

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; RUN: llc -O0 -mtriple=x86_64-unknown -mcpu=skx -o - %s
2+
; RUN: llc -mtriple=x86_64-unknown -mcpu=skx -o - %s
3+
; RUN: llc -O0 -mtriple=i686-unknown -mcpu=skx -o - %s
4+
; RUN: llc -mtriple=i686-unknown -mcpu=skx -o - %s
5+
; REQUIRES: asserts
6+
7+
@var_26 = external global i16, align 2
8+
9+
define void @foo() #0 {
10+
%1 = alloca i16, align 2
11+
%2 = load i16, i16* @var_26, align 2
12+
%3 = zext i16 %2 to i32
13+
%4 = icmp ne i32 %3, 7
14+
%5 = zext i1 %4 to i16
15+
store i16 %5, i16* %1, align 2
16+
%6 = load i16, i16* @var_26, align 2
17+
%7 = zext i16 %6 to i32
18+
%8 = and i32 1, %7
19+
%9 = shl i32 %8, 0
20+
%10 = load i16, i16* @var_26, align 2
21+
%11 = zext i16 %10 to i32
22+
%12 = icmp ne i32 %11, 7
23+
%13 = zext i1 %12 to i32
24+
%14 = and i32 %9, %13
25+
%15 = icmp ne i32 %14, 0
26+
%16 = zext i1 %15 to i8
27+
store i8 %16, i8* undef, align 1
28+
unreachable
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.