Skip to content

Commit 216c048

Browse files
committed
[DAGCombiner] Freeze maybe poison operands when folding select to logic
Work-in-progress, to fix #84653
1 parent c18e121 commit 216c048

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11307,28 +11307,34 @@ static SDValue foldBoolSelectToLogic(SDNode *N, SelectionDAG &DAG) {
1130711307
if (VT != Cond.getValueType() || VT.getScalarSizeInBits() != 1)
1130811308
return SDValue();
1130911309

11310-
// select Cond, Cond, F --> or Cond, F
11311-
// select Cond, 1, F --> or Cond, F
11310+
auto FreezeIfNeeded = [&](SDValue V) {
11311+
if (!DAG.isGuaranteedNotToBePoison(V))
11312+
return DAG.getFreeze(V);
11313+
return V;
11314+
};
11315+
11316+
// select Cond, Cond, F --> or Cond, freeze(F)
11317+
// select Cond, 1, F --> or Cond, freeze(F)
1131211318
if (Cond == T || isOneOrOneSplat(T, /* AllowUndefs */ true))
11313-
return matcher.getNode(ISD::OR, SDLoc(N), VT, Cond, F);
11319+
return matcher.getNode(ISD::OR, SDLoc(N), VT, Cond, FreezeIfNeeded(F));
1131411320

1131511321
// select Cond, T, Cond --> and Cond, T
1131611322
// select Cond, T, 0 --> and Cond, T
1131711323
if (Cond == F || isNullOrNullSplat(F, /* AllowUndefs */ true))
11318-
return matcher.getNode(ISD::AND, SDLoc(N), VT, Cond, T);
11324+
return matcher.getNode(ISD::AND, SDLoc(N), VT, Cond, FreezeIfNeeded(T));
1131911325

1132011326
// select Cond, T, 1 --> or (not Cond), T
1132111327
if (isOneOrOneSplat(F, /* AllowUndefs */ true)) {
1132211328
SDValue NotCond = matcher.getNode(ISD::XOR, SDLoc(N), VT, Cond,
1132311329
DAG.getAllOnesConstant(SDLoc(N), VT));
11324-
return matcher.getNode(ISD::OR, SDLoc(N), VT, NotCond, T);
11330+
return matcher.getNode(ISD::OR, SDLoc(N), VT, NotCond, FreezeIfNeeded(T));
1132511331
}
1132611332

1132711333
// select Cond, 0, F --> and (not Cond), F
1132811334
if (isNullOrNullSplat(T, /* AllowUndefs */ true)) {
1132911335
SDValue NotCond = matcher.getNode(ISD::XOR, SDLoc(N), VT, Cond,
1133011336
DAG.getAllOnesConstant(SDLoc(N), VT));
11331-
return matcher.getNode(ISD::AND, SDLoc(N), VT, NotCond, F);
11337+
return matcher.getNode(ISD::AND, SDLoc(N), VT, NotCond, FreezeIfNeeded(F));
1133211338
}
1133311339

1133411340
return SDValue();
@@ -11357,20 +11363,26 @@ static SDValue foldVSelectToSignBitSplatMask(SDNode *N, SelectionDAG &DAG) {
1135711363
else
1135811364
return SDValue();
1135911365

11366+
auto FreezeIfNeeded = [&](SDValue V) {
11367+
if (!DAG.isGuaranteedNotToBePoison(V))
11368+
return DAG.getFreeze(V);
11369+
return V;
11370+
};
11371+
1136011372
// (Cond0 s< 0) ? N1 : 0 --> (Cond0 s>> BW-1) & N1
1136111373
if (isNullOrNullSplat(N2)) {
1136211374
SDLoc DL(N);
1136311375
SDValue ShiftAmt = DAG.getConstant(VT.getScalarSizeInBits() - 1, DL, VT);
1136411376
SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Cond0, ShiftAmt);
11365-
return DAG.getNode(ISD::AND, DL, VT, Sra, N1);
11377+
return DAG.getNode(ISD::AND, DL, VT, Sra, FreezeIfNeeded(N1));
1136611378
}
1136711379

1136811380
// (Cond0 s< 0) ? -1 : N2 --> (Cond0 s>> BW-1) | N2
1136911381
if (isAllOnesOrAllOnesSplat(N1)) {
1137011382
SDLoc DL(N);
1137111383
SDValue ShiftAmt = DAG.getConstant(VT.getScalarSizeInBits() - 1, DL, VT);
1137211384
SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Cond0, ShiftAmt);
11373-
return DAG.getNode(ISD::OR, DL, VT, Sra, N2);
11385+
return DAG.getNode(ISD::OR, DL, VT, Sra, FreezeIfNeeded(N2));
1137411386
}
1137511387

1137611388
// If we have to invert the sign bit mask, only do that transform if the
@@ -11382,7 +11394,7 @@ static SDValue foldVSelectToSignBitSplatMask(SDNode *N, SelectionDAG &DAG) {
1138211394
SDValue ShiftAmt = DAG.getConstant(VT.getScalarSizeInBits() - 1, DL, VT);
1138311395
SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Cond0, ShiftAmt);
1138411396
SDValue Not = DAG.getNOT(DL, Sra, VT);
11385-
return DAG.getNode(ISD::AND, DL, VT, Not, N2);
11397+
return DAG.getNode(ISD::AND, DL, VT, Not, FreezeIfNeeded(N2));
1138611398
}
1138711399

1138811400
// TODO: There's another pattern in this family, but it may require

0 commit comments

Comments
 (0)