Skip to content

Commit e728b35

Browse files
committed
[DAGCombine] Remove OneUse restriction when folding (shl (add x, c1), c2)
1 parent eb29241 commit e728b35

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10070,17 +10070,33 @@ SDValue DAGCombiner::visitSHL(SDNode *N) {
1007010070
// Variant of version done on multiply, except mul by a power of 2 is turned
1007110071
// into a shift.
1007210072
if ((N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::OR) &&
10073-
N0->hasOneUse() && TLI.isDesirableToCommuteWithShift(N, Level)) {
10074-
SDValue N01 = N0.getOperand(1);
10075-
if (SDValue Shl1 =
10076-
DAG.FoldConstantArithmetic(ISD::SHL, SDLoc(N1), VT, {N01, N1})) {
10077-
SDValue Shl0 = DAG.getNode(ISD::SHL, SDLoc(N0), VT, N0.getOperand(0), N1);
10078-
AddToWorklist(Shl0.getNode());
10079-
SDNodeFlags Flags;
10080-
// Preserve the disjoint flag for Or.
10081-
if (N0.getOpcode() == ISD::OR && N0->getFlags().hasDisjoint())
10082-
Flags.setDisjoint(true);
10083-
return DAG.getNode(N0.getOpcode(), DL, VT, Shl0, Shl1, Flags);
10073+
TLI.isDesirableToCommuteWithShift(N, Level)) {
10074+
// LD/ST will optimize constant Offset extraction, so when AddNode
10075+
// is used by LD/ST, it can still complete the folding optimization
10076+
// operation performed above.
10077+
bool canOptAwlays = false;
10078+
if (!N0.hasOneUse() && N0.getOpcode() == ISD::ADD) {
10079+
for (SDNode *Use : N0->uses()) {
10080+
if (!isa<StoreSDNode>(Use) && !isa<LoadSDNode>(Use) && Use != N) {
10081+
canOptAwlays = false;
10082+
break;
10083+
}
10084+
canOptAwlays = true;
10085+
}
10086+
}
10087+
if (N0.hasOneUse() || canOptAwlays) {
10088+
SDValue N01 = N0.getOperand(1);
10089+
if (SDValue Shl1 =
10090+
DAG.FoldConstantArithmetic(ISD::SHL, SDLoc(N1), VT, {N01, N1})) {
10091+
SDValue Shl0 =
10092+
DAG.getNode(ISD::SHL, SDLoc(N0), VT, N0.getOperand(0), N1);
10093+
AddToWorklist(Shl0.getNode());
10094+
SDNodeFlags Flags;
10095+
// Preserve the disjoint flag for Or.
10096+
if (N0.getOpcode() == ISD::OR && N0->getFlags().hasDisjoint())
10097+
Flags.setDisjoint(true);
10098+
return DAG.getNode(N0.getOpcode(), DL, VT, Shl0, Shl1, Flags);
10099+
}
1008410100
}
1008510101
}
1008610102

llvm/test/CodeGen/RISCV/add_shl_constant.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ define void @add_shl_addmoreOneUse_in_store(ptr %array1, i32 %a, i32 %b) {
2828
; RV32-LABEL: add_shl_addmoreOneUse_in_store:
2929
; RV32: # %bb.0: # %entry
3030
; RV32-NEXT: addi a3, a1, 5
31-
; RV32-NEXT: slli a4, a3, 2
32-
; RV32-NEXT: add a4, a0, a4
33-
; RV32-NEXT: sw a2, 0(a4)
3431
; RV32-NEXT: slli a1, a1, 2
3532
; RV32-NEXT: add a0, a0, a1
33+
; RV32-NEXT: sw a2, 20(a0)
3634
; RV32-NEXT: sw a2, 24(a0)
3735
; RV32-NEXT: sw a3, 140(a0)
3836
; RV32-NEXT: ret

llvm/test/CodeGen/RISCV/riscv-shifted-extend.ll

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
33
; RUN: | FileCheck -check-prefix=RV64 %s
44

5-
define void @test(ptr nocapture noundef writeonly %array1, i32 noundef signext %a, i32 noundef signext %b) {
5+
define void @test(ptr %array1, i32 signext %a, i32 signext %b) {
66
; RV64-LABEL: test:
77
; RV64: # %bb.0: # %entry
88
; RV64-NEXT: addiw a3, a1, 5
@@ -31,7 +31,7 @@ entry:
3131
}
3232

3333
; test of jumpping, find add's operand has one more use can simplified
34-
define void @test1(ptr nocapture noundef %array1, i32 noundef signext %a, i32 noundef signext %b, i32 noundef signext %x) {
34+
define void @test1(ptr %array1, i32 signext %a, i32 signext %b, i32 signext %x) {
3535
; RV64-LABEL: test1:
3636
; RV64: # %bb.0: # %entry
3737
; RV64-NEXT: addiw a4, a1, 5
@@ -66,15 +66,13 @@ entry:
6666
ret void
6767
}
6868

69-
define void @test2(ptr nocapture noundef writeonly %array1, i64 noundef %a, i64 noundef %b) local_unnamed_addr #0 {
69+
define void @test2(ptr %array1, i64 %a, i64 %b) {
7070
; RV64-LABEL: test2:
7171
; RV64: # %bb.0: # %entry
7272
; RV64-NEXT: addi a3, a1, 5
73-
; RV64-NEXT: slli a4, a3, 3
74-
; RV64-NEXT: add a4, a0, a4
75-
; RV64-NEXT: sd a2, 0(a4)
7673
; RV64-NEXT: slli a1, a1, 3
7774
; RV64-NEXT: add a0, a1, a0
75+
; RV64-NEXT: sd a2, 40(a0)
7876
; RV64-NEXT: sd a2, 48(a0)
7977
; RV64-NEXT: sd a3, 280(a0)
8078
; RV64-NEXT: ret
@@ -91,7 +89,7 @@ entry:
9189
ret void
9290
}
9391

94-
define void @test3(ptr nocapture noundef %array1, i64 noundef %a, i64 noundef %b, i64 noundef %x) {
92+
define void @test3(ptr %array1, i64 %a, i64 %b, i64 %x) {
9593
; RV64-LABEL: test3:
9694
; RV64: # %bb.0: # %entry
9795
; RV64-NEXT: addi a4, a1, 5
@@ -100,11 +98,9 @@ define void @test3(ptr nocapture noundef %array1, i64 noundef %a, i64 noundef %b
10098
; RV64-NEXT: # %bb.1: # %entry
10199
; RV64-NEXT: mv a5, a2
102100
; RV64-NEXT: .LBB3_2: # %entry
103-
; RV64-NEXT: slli a2, a4, 3
104-
; RV64-NEXT: add a2, a0, a2
105-
; RV64-NEXT: sd a5, 0(a2)
106101
; RV64-NEXT: slli a1, a1, 3
107102
; RV64-NEXT: add a0, a1, a0
103+
; RV64-NEXT: sd a5, 40(a0)
108104
; RV64-NEXT: sd a5, 48(a0)
109105
; RV64-NEXT: sd a4, 280(a0)
110106
; RV64-NEXT: ret

0 commit comments

Comments
 (0)