Skip to content

Commit 07a3827

Browse files
committed
comments + refactoring
1 parent 539724b commit 07a3827

File tree

2 files changed

+75
-21
lines changed

2 files changed

+75
-21
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10981,8 +10981,7 @@ SDValue DAGCombiner::visitSRL(SDNode *N) {
1098110981

1098210982
// fold (srl (or x, (shl (zext y), c1)), c1) -> (or (srl x, c1), (zext y))
1098310983
// c1 <= leadingzeros(zext(y))
10984-
if (N1C && (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND ||
10985-
N0.getOpcode() == ISD::XOR)) {
10984+
if (N1C && ISD::isBitwiseLogicOp(N0.getOpcode())) {
1098610985
SDValue lhs = N0.getOperand(0);
1098710986
SDValue rhs = N0.getOperand(1);
1098810987
SDValue shl;
@@ -10994,19 +10993,17 @@ SDValue DAGCombiner::visitSRL(SDNode *N) {
1099410993
shl = rhs;
1099510994
other = lhs;
1099610995
}
10997-
if (shl.getNode()) {
10998-
if (shl.getOperand(1).getNode() == N1C) {
10999-
SDValue zext = shl.getOperand(0);
11000-
if (zext.getOpcode() == ISD::ZERO_EXTEND) {
11001-
unsigned numLeadingZeros =
11002-
zext.getValueType().getSizeInBits() -
11003-
zext.getOperand(0).getValueType().getSizeInBits();
11004-
if (N1C->getZExtValue() <= numLeadingZeros) {
11005-
return DAG.getNode(
11006-
N0.getOpcode(), SDLoc(N0), VT,
11007-
DAG.getNode(ISD::SRL, SDLoc(N0), VT, other, SDValue(N1C, 0)),
11008-
zext);
11009-
}
10996+
if (shl && shl.getOperand(1) == N1) {
10997+
SDValue zext = shl.getOperand(0);
10998+
if (zext.getOpcode() == ISD::ZERO_EXTEND) {
10999+
unsigned numLeadingZeros =
11000+
zext.getValueType().getScalarSizeInBits() -
11001+
zext.getOperand(0).getValueType().getScalarSizeInBits();
11002+
if (N1C->getZExtValue() <= numLeadingZeros) {
11003+
return DAG.getNode(
11004+
N0.getOpcode(), SDLoc(N0), VT,
11005+
DAG.getNode(ISD::SRL, SDLoc(N0), VT, other, SDValue(N1C, 0)),
11006+
zext);
1101011007
}
1101111008
}
1101211009
}

llvm/test/CodeGen/NVPTX/shift-opt.ll

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
; RUN: llc < %s -mtriple=nvptx64 | FileCheck %s
22

3-
define i64 @test1(i64 %x, i32 %y) {
3+
define i64 @test_or(i64 %x, i32 %y) {
44
;
55
; srl (or (x, shl(zext(y),c1)),c1) -> or(srl(x,c1), zext(y))
66
; c1 <= leadingzeros(zext(y))
77
;
8-
; CHECK-LABEL: test1
9-
; CHECK: ld.param.u64 %[[X:rd[0-9]+]], [test1_param_0];
10-
; CHECK: ld.param.u32 %[[Y:rd[0-9]+]], [test1_param_1];
8+
; CHECK-LABEL: test_or
9+
; CHECK: ld.param.u64 %[[X:rd[0-9]+]], [test_or_param_0];
10+
; CHECK: ld.param.u32 %[[Y:rd[0-9]+]], [test_or_param_1];
1111
; CHECK: shr.u64 %[[SHR:rd[0-9]+]], %[[X]], 5;
12-
; CHECK: or.b64 %[[OR:rd[0-9]+]], %[[SHR]], %[[Y]];
13-
; CHECK: st.param.b64 [func_retval0], %[[OR]];
12+
; CHECK: or.b64 %[[LOP:rd[0-9]+]], %[[SHR]], %[[Y]];
13+
; CHECK: st.param.b64 [func_retval0], %[[LOP]];
1414
;
1515
%ext = zext i32 %y to i64
1616
%shl = shl i64 %ext, 5
@@ -19,6 +19,63 @@ define i64 @test1(i64 %x, i32 %y) {
1919
ret i64 %srl
2020
}
2121

22+
define i64 @test_xor(i64 %x, i32 %y) {
23+
;
24+
; srl (xor (x, shl(zext(y),c1)),c1) -> xor(srl(x,c1), zext(y))
25+
; c1 <= leadingzeros(zext(y))
26+
;
27+
; CHECK-LABEL: test_xor
28+
; CHECK: ld.param.u64 %[[X:rd[0-9]+]], [test_xor_param_0];
29+
; CHECK: ld.param.u32 %[[Y:rd[0-9]+]], [test_xor_param_1];
30+
; CHECK: shr.u64 %[[SHR:rd[0-9]+]], %[[X]], 5;
31+
; CHECK: xor.b64 %[[LOP:rd[0-9]+]], %[[SHR]], %[[Y]];
32+
; CHECK: st.param.b64 [func_retval0], %[[LOP]];
33+
;
34+
%ext = zext i32 %y to i64
35+
%shl = shl i64 %ext, 5
36+
%or = xor i64 %x, %shl
37+
%srl = lshr i64 %or, 5
38+
ret i64 %srl
39+
}
40+
41+
define i64 @test_and(i64 %x, i32 %y) {
42+
;
43+
; srl (and (x, shl(zext(y),c1)),c1) -> and(srl(x,c1), zext(y))
44+
; c1 <= leadingzeros(zext(y))
45+
;
46+
; CHECK-LABEL: test_and
47+
; CHECK: ld.param.u64 %[[X:rd[0-9]+]], [test_and_param_0];
48+
; CHECK: ld.param.u32 %[[Y:rd[0-9]+]], [test_and_param_1];
49+
; CHECK: shr.u64 %[[SHR:rd[0-9]+]], %[[X]], 5;
50+
; CHECK: and.b64 %[[LOP:rd[0-9]+]], %[[SHR]], %[[Y]];
51+
; CHECK: st.param.b64 [func_retval0], %[[LOP]];
52+
;
53+
%ext = zext i32 %y to i64
54+
%shl = shl i64 %ext, 5
55+
%or = and i64 %x, %shl
56+
%srl = lshr i64 %or, 5
57+
ret i64 %srl
58+
}
59+
60+
define <2 x i64> @test_or_vec(<2 x i64> %x, <2 x i32> %y) {
61+
;
62+
; srl (or (x, shl(zext(y),c1)),c1) -> or(srl(x,c1), zext(y))
63+
; c1 <= leadingzeros(zext(y))
64+
;
65+
; CHECK-LABEL: test_or
66+
; CHECK: ld.param.u64 %[[X:rd[0-9]+]], [test_or_param_0];
67+
; CHECK: ld.param.u32 %[[Y:rd[0-9]+]], [test_or_param_1];
68+
; CHECK: shr.u64 %[[SHR:rd[0-9]+]], %[[X]], 5;
69+
; CHECK: or.b64 %[[LOP:rd[0-9]+]], %[[SHR]], %[[Y]];
70+
; CHECK: st.param.b64 [func_retval0], %[[LOP]];
71+
;
72+
%ext = zext <2 x i32> %y to <2 x i64>
73+
%shl = shl <2 x i64> %ext, splat(i64 5)
74+
%or = or <2 x i64> %x, %shl
75+
%srl = lshr <2 x i64> %or, splat(i64 5)
76+
ret <2 x i64> %srl
77+
}
78+
2279
define i64 @test2(i64 %x, i32 %y) {
2380
;
2481
; srl (or (x, shl(zext(y),c1)),c1) -> or(srl(x,c1), zext(y))

0 commit comments

Comments
 (0)