[DAG] Remove SelectionDAG::GetDemandedBits and use SimplifyMultipleUseDemandedBits directly.

GetDemandedBits is mainly a wrapper around SimplifyMultipleUseDemandedBits now, and is only used by DAGCombiner::visitSTORE so I've moved all remaining functionality there.

visitSTORE was making use of this to 'simplify' constants for a trunc-store. Just removing this code left to a mixture of regressions and gains - it came down to whether a target preferred a sign or zero extended constant for materialization/truncation. I've just moved the code over for now, but a next step would be to move this to targetShrinkDemandedConstant, but some targets that override the method expect a basic binop, and might react badly to a store node.....
This commit is contained in:
Simon Pilgrim 2022-07-28 17:03:35 +01:00
parent 82c1b136db
commit 8c99cef1e7
4 changed files with 16 additions and 37 deletions

View File

@ -1856,14 +1856,6 @@ public:
SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond,
const SDLoc &dl);
/// See if the specified operand can be simplified with the knowledge that
/// only the bits specified by DemandedBits are used. If so, return the
/// simpler operand, otherwise return a null SDValue.
///
/// (This exists alongside SimplifyDemandedBits because GetDemandedBits can
/// simplify nodes with multiple uses more aggressively.)
SDValue GetDemandedBits(SDValue V, const APInt &DemandedBits);
/// Return true if the sign bit of Op is known to be zero.
/// We use this predicate to simplify operations downstream.
bool SignBitIsZero(SDValue Op, unsigned Depth = 0) const;

View File

@ -19057,9 +19057,24 @@ SDValue DAGCombiner::visitSTORE(SDNode *N) {
// Otherwise, see if we can simplify the input to this truncstore with
// knowledge that only the low bits are being used. For example:
// "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
if (SDValue Shorter = DAG.GetDemandedBits(Value, TruncDemandedBits))
if (SDValue Shorter =
TLI.SimplifyMultipleUseDemandedBits(Value, TruncDemandedBits, DAG))
return DAG.getTruncStore(Chain, SDLoc(N), Shorter, Ptr, ST->getMemoryVT(),
ST->getMemOperand());
// If we're storing a truncated constant, see if we can simplify it.
// TODO: Move this to targetShrinkDemandedConstant?
if (auto *Cst = dyn_cast<ConstantSDNode>(Value))
if (!Cst->isOpaque()) {
const APInt &CValue = Cst->getAPIntValue();
APInt NewVal = CValue & TruncDemandedBits;
if (NewVal != CValue) {
SDValue Shorter =
DAG.getConstant(NewVal, SDLoc(N), Value.getValueType());
return DAG.getTruncStore(Chain, SDLoc(N), Shorter, Ptr,
ST->getMemoryVT(), ST->getMemOperand());
}
}
}
// If this is a load followed by a store to the same location, then the store

View File

@ -2459,33 +2459,6 @@ SDValue SelectionDAG::FoldSetCC(EVT VT, SDValue N1, SDValue N2,
return SDValue();
}
/// See if the specified operand can be simplified with the knowledge that only
/// the bits specified by DemandedBits are used.
/// TODO: really we should be making this into the DAG equivalent of
/// SimplifyMultipleUseDemandedBits and not generate any new nodes.
SDValue SelectionDAG::GetDemandedBits(SDValue V, const APInt &DemandedBits) {
EVT VT = V.getValueType();
if (VT.isScalableVector())
return SDValue();
switch (V.getOpcode()) {
default:
return TLI->SimplifyMultipleUseDemandedBits(V, DemandedBits, *this);
case ISD::Constant: {
auto *C = cast<ConstantSDNode>(V);
if (C->isOpaque())
break;
const APInt &CVal = C->getAPIntValue();
APInt NewVal = CVal & DemandedBits;
if (NewVal != CVal)
return getConstant(NewVal, SDLoc(V), V.getValueType());
break;
}
}
return SDValue();
}
/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
/// use this predicate to simplify operations downstream.
bool SelectionDAG::SignBitIsZero(SDValue Op, unsigned Depth) const {

View File

@ -649,7 +649,6 @@ bool TargetLowering::SimplifyDemandedBits(SDValue Op, const APInt &DemandedBits,
AssumeSingleUse);
}
// TODO: Can we merge SelectionDAG::GetDemandedBits into this?
// TODO: Under what circumstances can we create nodes? Constant folding?
SDValue TargetLowering::SimplifyMultipleUseDemandedBits(
SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts,