Skip to content

Commit e2896c8

Browse files
committed
comments + refactoring
1 parent eaf6010 commit e2896c8

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
@@ -10974,8 +10974,7 @@ SDValue DAGCombiner::visitSRL(SDNode *N) {
1097410974

1097510975
// fold (srl (or x, (shl (zext y), c1)), c1) -> (or (srl x, c1), (zext y))
1097610976
// c1 <= leadingzeros(zext(y))
10977-
if (N1C && (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND ||
10978-
N0.getOpcode() == ISD::XOR)) {
10977+
if (N1C && ISD::isBitwiseLogicOp(N0.getOpcode())) {
1097910978
SDValue lhs = N0.getOperand(0);
1098010979
SDValue rhs = N0.getOperand(1);
1098110980
SDValue shl;
@@ -10987,19 +10986,17 @@ SDValue DAGCombiner::visitSRL(SDNode *N) {
1098710986
shl = rhs;
1098810987
other = lhs;
1098910988
}
10990-
if (shl.getNode()) {
10991-
if (shl.getOperand(1).getNode() == N1C) {
10992-
SDValue zext = shl.getOperand(0);
10993-
if (zext.getOpcode() == ISD::ZERO_EXTEND) {
10994-
unsigned numLeadingZeros =
10995-
zext.getValueType().getSizeInBits() -
10996-
zext.getOperand(0).getValueType().getSizeInBits();
10997-
if (N1C->getZExtValue() <= numLeadingZeros) {
10998-
return DAG.getNode(
10999-
N0.getOpcode(), SDLoc(N0), VT,
11000-
DAG.getNode(ISD::SRL, SDLoc(N0), VT, other, SDValue(N1C, 0)),
11001-
zext);
11002-
}
10989+
if (shl && shl.getOperand(1) == N1) {
10990+
SDValue zext = shl.getOperand(0);
10991+
if (zext.getOpcode() == ISD::ZERO_EXTEND) {
10992+
unsigned numLeadingZeros =
10993+
zext.getValueType().getScalarSizeInBits() -
10994+
zext.getOperand(0).getValueType().getScalarSizeInBits();
10995+
if (N1C->getZExtValue() <= numLeadingZeros) {
10996+
return DAG.getNode(
10997+
N0.getOpcode(), SDLoc(N0), VT,
10998+
DAG.getNode(ISD::SRL, SDLoc(N0), VT, other, SDValue(N1C, 0)),
10999+
zext);
1100311000
}
1100411001
}
1100511002
}

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)