Skip to content

Commit

Permalink
Revert "[DAGCombine] Fold redundant select"
Browse files Browse the repository at this point in the history
This reverts commit bbdf243.
  • Loading branch information
sparker-arm committed Feb 7, 2023
1 parent ced5a38 commit 91f8289
Show file tree
Hide file tree
Showing 11 changed files with 1,633 additions and 1,288 deletions.
67 changes: 0 additions & 67 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Expand Up @@ -10956,73 +10956,6 @@ SDValue DAGCombiner::visitSELECT(SDNode *N) {
}
}

// If we have a chain of two selects, which share a true/false value and
// both are controlled from the two setcc nodes which cannot produce the
// same value, we can fold away N.
// select (setcc X), Y, (select (setcc X), Z, Y) -> select (setcc X), Z, Y
auto IsSelect = [](SDValue Op) {
return Op->getOpcode() == ISD::SELECT;
};
if ((IsSelect(N1) || IsSelect(N2)) && (N1.getOpcode() != N2.getOpcode())) {
auto AreSame = [](SDValue Op0, SDValue Op1) {
if (Op0 == Op1)
return true;
auto *C0 = dyn_cast<ConstantSDNode>(Op0);
auto *C1 = dyn_cast<ConstantSDNode>(Op1);
return C0 && C1 &&
APInt::isSameValue(C0->getAPIntValue(), C1->getAPIntValue());
};

SDValue OtherSelect;
bool SelectsShareOp = false;
if (IsSelect(N1)) {
OtherSelect = N1;
SelectsShareOp = AreSame(OtherSelect.getOperand(1), N2);
} else {
OtherSelect = N2;
SelectsShareOp = AreSame(OtherSelect.getOperand(2), N1);
}

auto CanNeverBeEqual = [](SDValue SetCC0, SDValue SetCC1) {
if (SetCC0->getOpcode() != ISD::SETCC ||
SetCC1->getOpcode() != ISD::SETCC ||
SetCC0->getOperand(0) != SetCC1->getOperand(0))
return false;

ISD::CondCode CC0 = cast<CondCodeSDNode>(SetCC0.getOperand(2))->get();
ISD::CondCode CC1 = cast<CondCodeSDNode>(SetCC1.getOperand(2))->get();
auto *C0 = dyn_cast<ConstantSDNode>(SetCC0.getOperand(1));
auto *C1 = dyn_cast<ConstantSDNode>(SetCC1.getOperand(1));
if (!C0 || !C1)
return false;

bool ConstantsAreSame =
APInt::isSameValue(C0->getAPIntValue(), C1->getAPIntValue());
auto IsEqual = [](ISD::CondCode CC) {
return CC == ISD::SETEQ;
};
auto IsNotEqual = [](ISD::CondCode CC) {
return CC == ISD::SETLT || CC == ISD::SETULT ||
CC == ISD::SETGT || CC == ISD::SETUGT ||
CC == ISD::SETNE;
};

if (ConstantsAreSame && IsNotEqual(CC0) && IsEqual(CC1))
return true;
if (ConstantsAreSame && IsNotEqual(CC1) && IsEqual(CC0))
return true;
if (!ConstantsAreSame && IsEqual(CC0) && IsEqual(CC1))
return true;

return false;
};

SDValue SetCC0 = N0;
SDValue SetCC1 = OtherSelect.getOperand(0);
if (SelectsShareOp && CanNeverBeEqual(SetCC0, SetCC1))
return OtherSelect;
}

if (TLI.isOperationLegal(ISD::SELECT_CC, VT) ||
(!LegalOperations &&
TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))) {
Expand Down

0 comments on commit 91f8289

Please sign in to comment.