Skip to content

Commit 415c67b

Browse files
committed
[SDAG] allow partial undef vector constants with select->logic folds
This is an enhancement suggested in the original review/commit: D97730 / 7fce332
1 parent 1b5ab13 commit 415c67b

File tree

4 files changed

+14
-21
lines changed

4 files changed

+14
-21
lines changed

llvm/include/llvm/CodeGen/SelectionDAGNodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,12 +1677,12 @@ bool isNullOrNullSplat(SDValue V, bool AllowUndefs = false);
16771677
/// Return true if the value is a constant 1 integer or a splatted vector of a
16781678
/// constant 1 integer (with no undefs).
16791679
/// Does not permit build vector implicit truncation.
1680-
bool isOneOrOneSplat(SDValue V);
1680+
bool isOneOrOneSplat(SDValue V, bool AllowUndefs = false);
16811681

16821682
/// Return true if the value is a constant -1 integer or a splatted vector of a
16831683
/// constant -1 integer (with no undefs).
16841684
/// Does not permit build vector implicit truncation.
1685-
bool isAllOnesOrAllOnesSplat(SDValue V);
1685+
bool isAllOnesOrAllOnesSplat(SDValue V, bool AllowUndefs = false);
16861686

16871687
class GlobalAddressSDNode : public SDNode {
16881688
friend class SelectionDAG;

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9312,22 +9312,22 @@ static SDValue foldBoolSelectToLogic(SDNode *N, SelectionDAG &DAG) {
93129312

93139313
// select Cond, Cond, F --> or Cond, F
93149314
// select Cond, 1, F --> or Cond, F
9315-
if (Cond == T || isOneOrOneSplat(T))
9315+
if (Cond == T || isOneOrOneSplat(T, /* AllowUndefs */ true))
93169316
return DAG.getNode(ISD::OR, SDLoc(N), VT, Cond, F);
93179317

93189318
// select Cond, T, Cond --> and Cond, T
93199319
// select Cond, T, 0 --> and Cond, T
9320-
if (Cond == F || isNullOrNullSplat(F))
9320+
if (Cond == F || isNullOrNullSplat(F, /* AllowUndefs */ true))
93219321
return DAG.getNode(ISD::AND, SDLoc(N), VT, Cond, T);
93229322

93239323
// select Cond, T, 1 --> or (not Cond), T
9324-
if (isOneOrOneSplat(F)) {
9324+
if (isOneOrOneSplat(F, /* AllowUndefs */ true)) {
93259325
SDValue NotCond = DAG.getNOT(SDLoc(N), Cond, VT);
93269326
return DAG.getNode(ISD::OR, SDLoc(N), VT, NotCond, T);
93279327
}
93289328

93299329
// select Cond, 0, F --> and (not Cond), F
9330-
if (isNullOrNullSplat(T)) {
9330+
if (isNullOrNullSplat(T, /* AllowUndefs */ true)) {
93319331
SDValue NotCond = DAG.getNOT(SDLoc(N), Cond, VT);
93329332
return DAG.getNode(ISD::AND, SDLoc(N), VT, NotCond, F);
93339333
}

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9253,17 +9253,17 @@ bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
92539253
return C && C->isNullValue();
92549254
}
92559255

9256-
bool llvm::isOneOrOneSplat(SDValue N) {
9256+
bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
92579257
// TODO: may want to use peekThroughBitcast() here.
92589258
unsigned BitWidth = N.getScalarValueSizeInBits();
9259-
ConstantSDNode *C = isConstOrConstSplat(N);
9259+
ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
92609260
return C && C->isOne() && C->getValueSizeInBits(0) == BitWidth;
92619261
}
92629262

9263-
bool llvm::isAllOnesOrAllOnesSplat(SDValue N) {
9263+
bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
92649264
N = peekThroughBitcasts(N);
92659265
unsigned BitWidth = N.getScalarValueSizeInBits();
9266-
ConstantSDNode *C = isConstOrConstSplat(N);
9266+
ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
92679267
return C && C->isAllOnesValue() && C->getValueSizeInBits(0) == BitWidth;
92689268
}
92699269

llvm/test/CodeGen/AArch64/select-with-and-or.ll

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ define <4 x i1> @and_vec_undef(<4 x i32> %x, <4 x i32> %y, <4 x i32> %z, <4 x i3
122122
; CHECK: // %bb.0:
123123
; CHECK-NEXT: cmeq v0.4s, v0.4s, v1.4s
124124
; CHECK-NEXT: cmgt v1.4s, v2.4s, v3.4s
125-
; CHECK-NEXT: and v0.16b, v1.16b, v0.16b
125+
; CHECK-NEXT: and v0.16b, v0.16b, v1.16b
126126
; CHECK-NEXT: xtn v0.4h, v0.4s
127127
; CHECK-NEXT: ret
128128
%a = icmp eq <4 x i32> %x, %y
@@ -136,10 +136,8 @@ define <4 x i1> @or_vec_undef(<4 x i32> %x, <4 x i32> %y, <4 x i32> %z, <4 x i32
136136
; CHECK: // %bb.0:
137137
; CHECK-NEXT: cmeq v0.4s, v0.4s, v1.4s
138138
; CHECK-NEXT: cmgt v1.4s, v2.4s, v3.4s
139+
; CHECK-NEXT: orr v0.16b, v0.16b, v1.16b
139140
; CHECK-NEXT: xtn v0.4h, v0.4s
140-
; CHECK-NEXT: xtn v1.4h, v1.4s
141-
; CHECK-NEXT: movi v2.4h, #1
142-
; CHECK-NEXT: bsl v0.8b, v2.8b, v1.8b
143141
; CHECK-NEXT: ret
144142
%a = icmp eq <4 x i32> %x, %y
145143
%b = icmp sgt <4 x i32> %z, %w
@@ -152,9 +150,8 @@ define <4 x i1> @and_not_vec_undef(<4 x i32> %x, <4 x i32> %y, <4 x i32> %z, <4
152150
; CHECK: // %bb.0:
153151
; CHECK-NEXT: cmeq v0.4s, v0.4s, v1.4s
154152
; CHECK-NEXT: cmgt v1.4s, v2.4s, v3.4s
153+
; CHECK-NEXT: bic v0.16b, v1.16b, v0.16b
155154
; CHECK-NEXT: xtn v0.4h, v0.4s
156-
; CHECK-NEXT: xtn v1.4h, v1.4s
157-
; CHECK-NEXT: bic v0.8b, v1.8b, v0.8b
158155
; CHECK-NEXT: ret
159156
%a = icmp eq <4 x i32> %x, %y
160157
%b = icmp sgt <4 x i32> %z, %w
@@ -167,12 +164,8 @@ define <4 x i1> @or_not_vec_undef(<4 x i32> %x, <4 x i32> %y, <4 x i32> %z, <4 x
167164
; CHECK: // %bb.0:
168165
; CHECK-NEXT: cmeq v0.4s, v0.4s, v1.4s
169166
; CHECK-NEXT: cmgt v1.4s, v2.4s, v3.4s
170-
; CHECK-NEXT: movi v2.4h, #1
171-
; CHECK-NEXT: xtn v3.4h, v0.4s
172-
; CHECK-NEXT: and v0.16b, v1.16b, v0.16b
167+
; CHECK-NEXT: orn v0.16b, v1.16b, v0.16b
173168
; CHECK-NEXT: xtn v0.4h, v0.4s
174-
; CHECK-NEXT: bic v1.8b, v2.8b, v3.8b
175-
; CHECK-NEXT: orr v0.8b, v0.8b, v1.8b
176169
; CHECK-NEXT: ret
177170
%a = icmp eq <4 x i32> %x, %y
178171
%b = icmp sgt <4 x i32> %z, %w

0 commit comments

Comments
 (0)