Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions llvm/include/llvm/CodeGen/SelectionDAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -2488,8 +2488,7 @@ class SelectionDAG {

/// Check if a value \op N is a constant using the target's BooleanContent for
/// its type.
LLVM_ABI std::optional<bool>
isBoolConstant(SDValue N, bool AllowTruncation = false) const;
LLVM_ABI std::optional<bool> isBoolConstant(SDValue N) const;

/// Set CallSiteInfo to be associated with Node.
void addCallSiteInfo(const SDNode *Node, CallSiteInfo &&CallInfo) {
Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/CodeGen/TargetLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -4371,6 +4371,11 @@ class LLVM_ABI TargetLowering : public TargetLoweringBase {
Op.getOpcode() == ISD::SPLAT_VECTOR_PARTS;
}

/// Return true if the given select/vselect should be considered canonical and
/// not be transformed. Currently only used for "vselect (not Cond), N1, N2 ->
/// vselect Cond, N2, N1".
virtual bool isTargetCanonicalSelect(SDNode *N) const { return false; }

struct DAGCombinerInfo {
void *DC; // The DAG Combiner object.
CombineLevel Level;
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13194,8 +13194,9 @@ SDValue DAGCombiner::visitVSELECT(SDNode *N) {
return V;

// vselect (not Cond), N1, N2 -> vselect Cond, N2, N1
if (SDValue F = extractBooleanFlip(N0, DAG, TLI, false))
return DAG.getSelect(DL, VT, F, N2, N1);
if (!TLI.isTargetCanonicalSelect(N))
if (SDValue F = extractBooleanFlip(N0, DAG, TLI, false))
return DAG.getSelect(DL, VT, F, N2, N1);

// select (sext m), (add X, C), X --> (add X, (and C, (sext m))))
if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N2 && N1->hasOneUse() &&
Expand Down
11 changes: 6 additions & 5 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10459,7 +10459,7 @@ SDValue SelectionDAG::simplifySelect(SDValue Cond, SDValue T, SDValue F) {

// select true, T, F --> T
// select false, T, F --> F
if (auto C = isBoolConstant(Cond, /*AllowTruncation=*/true))
if (auto C = isBoolConstant(Cond))
return *C ? T : F;

// select ?, T, T --> T
Expand Down Expand Up @@ -13688,13 +13688,14 @@ bool SelectionDAG::isConstantFPBuildVectorOrConstantFP(SDValue N) const {
return false;
}

std::optional<bool> SelectionDAG::isBoolConstant(SDValue N,
bool AllowTruncation) const {
ConstantSDNode *Const = isConstOrConstSplat(N, false, AllowTruncation);
std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
ConstantSDNode *Const =
isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
if (!Const)
return std::nullopt;

const APInt &CVal = Const->getAPIntValue();
EVT VT = N->getValueType(0);
const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
switch (TLI->getBooleanContents(N.getValueType())) {
case TargetLowering::ZeroOrOneBooleanContent:
if (CVal.isOne())
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4975,6 +4975,16 @@ X86TargetLowering::getTargetConstantFromLoad(LoadSDNode *LD) const {
return getTargetConstantFromNode(LD);
}

bool X86TargetLowering::isTargetCanonicalSelect(SDNode *N) const {
// Do not fold (vselect not(C), X, 0s) to (vselect C, Os, X)
SDValue Cond = N->getOperand(0);
SDValue RHS = N->getOperand(2);
EVT CondVT = Cond.getValueType();
return N->getOpcode() == ISD::VSELECT && Subtarget.hasAVX512() &&
CondVT.getVectorElementType() == MVT::i1 &&
ISD::isBuildVectorAllZeros(RHS.getNode());
}

// Extract raw constant bits from constant pools.
static bool getTargetConstantBitsFromNode(SDValue Op, unsigned EltSizeInBits,
APInt &UndefElts,
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/X86/X86ISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,8 @@ namespace llvm {
TargetLowering::isTargetCanonicalConstantNode(Op);
}

bool isTargetCanonicalSelect(SDNode *N) const override;

const Constant *getTargetConstantFromLoad(LoadSDNode *LD) const override;

SDValue unwrapAddress(SDValue N) const override;
Expand Down
Loading
Loading