Skip to content

[DAG] isConstantIntBuildVectorOrConstantInt - peek through bitcasts #112710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2024
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
5 changes: 3 additions & 2 deletions llvm/include/llvm/CodeGen/SelectionDAG.h
Original file line number Diff line number Diff line change
Expand Up @@ -2301,10 +2301,11 @@ class SelectionDAG {
Align getEVTAlign(EVT MemoryVT) const;

/// Test whether the given value is a constant int or similar node.
SDNode *isConstantIntBuildVectorOrConstantInt(SDValue N) const;
bool isConstantIntBuildVectorOrConstantInt(SDValue N,
bool AllowOpaques = true) const;

/// Test whether the given value is a constant FP or similar node.
SDNode *isConstantFPBuildVectorOrConstantFP(SDValue N) const ;
bool isConstantFPBuildVectorOrConstantFP(SDValue N) const;

/// \returns true if \p N is any kind of constant or build_vector of
/// constants, int or float. If a vector, it may not necessarily be a splat.
Expand Down
34 changes: 14 additions & 20 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,13 +1205,13 @@ SDValue DAGCombiner::reassociateOpsCommutative(unsigned Opc, const SDLoc &DL,
SDValue N00 = N0.getOperand(0);
SDValue N01 = N0.getOperand(1);

if (DAG.isConstantIntBuildVectorOrConstantInt(peekThroughBitcasts(N01))) {
if (DAG.isConstantIntBuildVectorOrConstantInt(N01)) {
SDNodeFlags NewFlags;
if (N0.getOpcode() == ISD::ADD && N0->getFlags().hasNoUnsignedWrap() &&
Flags.hasNoUnsignedWrap())
NewFlags.setNoUnsignedWrap(true);

if (DAG.isConstantIntBuildVectorOrConstantInt(peekThroughBitcasts(N1))) {
if (DAG.isConstantIntBuildVectorOrConstantInt(N1)) {
// Reassociate: (op (op x, c1), c2) -> (op x, (op c1, c2))
if (SDValue OpNode = DAG.FoldConstantArithmetic(Opc, DL, VT, {N01, N1}))
return DAG.getNode(Opc, DL, VT, N00, OpNode, NewFlags);
Expand Down Expand Up @@ -9931,10 +9931,10 @@ SDValue DAGCombiner::visitRotate(SDNode *N) {
// fold (rot* (rot* x, c2), c1)
// -> (rot* x, ((c1 % bitsize) +- (c2 % bitsize) + bitsize) % bitsize)
if (NextOp == ISD::ROTL || NextOp == ISD::ROTR) {
SDNode *C1 = DAG.isConstantIntBuildVectorOrConstantInt(N1);
SDNode *C2 = DAG.isConstantIntBuildVectorOrConstantInt(N0.getOperand(1));
if (C1 && C2 && C1->getValueType(0) == C2->getValueType(0)) {
EVT ShiftVT = C1->getValueType(0);
bool C1 = DAG.isConstantIntBuildVectorOrConstantInt(N1);
bool C2 = DAG.isConstantIntBuildVectorOrConstantInt(N0.getOperand(1));
if (C1 && C2 && N1.getValueType() == N0.getOperand(1).getValueType()) {
EVT ShiftVT = N1.getValueType();
bool SameSide = (N->getOpcode() == NextOp);
unsigned CombineOp = SameSide ? ISD::ADD : ISD::SUB;
SDValue BitsizeC = DAG.getConstant(Bitsize, dl, ShiftVT);
Expand Down Expand Up @@ -16805,8 +16805,8 @@ SDValue DAGCombiner::visitVP_FADD(SDNode *N) {
SDValue DAGCombiner::visitFADD(SDNode *N) {
SDValue N0 = N->getOperand(0);
SDValue N1 = N->getOperand(1);
SDNode *N0CFP = DAG.isConstantFPBuildVectorOrConstantFP(N0);
SDNode *N1CFP = DAG.isConstantFPBuildVectorOrConstantFP(N1);
bool N0CFP = DAG.isConstantFPBuildVectorOrConstantFP(N0);
bool N1CFP = DAG.isConstantFPBuildVectorOrConstantFP(N1);
EVT VT = N->getValueType(0);
SDLoc DL(N);
const TargetOptions &Options = DAG.getTarget().Options;
Expand Down Expand Up @@ -16903,10 +16903,8 @@ SDValue DAGCombiner::visitFADD(SDNode *N) {
// of rounding steps.
if (TLI.isOperationLegalOrCustom(ISD::FMUL, VT) && !N0CFP && !N1CFP) {
if (N0.getOpcode() == ISD::FMUL) {
SDNode *CFP00 =
DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(0));
SDNode *CFP01 =
DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(1));
bool CFP00 = DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(0));
bool CFP01 = DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(1));

// (fadd (fmul x, c), x) -> (fmul x, c+1)
if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Expand All @@ -16926,10 +16924,8 @@ SDValue DAGCombiner::visitFADD(SDNode *N) {
}

if (N1.getOpcode() == ISD::FMUL) {
SDNode *CFP10 =
DAG.isConstantFPBuildVectorOrConstantFP(N1.getOperand(0));
SDNode *CFP11 =
DAG.isConstantFPBuildVectorOrConstantFP(N1.getOperand(1));
bool CFP10 = DAG.isConstantFPBuildVectorOrConstantFP(N1.getOperand(0));
bool CFP11 = DAG.isConstantFPBuildVectorOrConstantFP(N1.getOperand(1));

// (fadd x, (fmul x, c)) -> (fmul x, c+1)
if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Expand All @@ -16949,8 +16945,7 @@ SDValue DAGCombiner::visitFADD(SDNode *N) {
}

if (N0.getOpcode() == ISD::FADD) {
SDNode *CFP00 =
DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(0));
bool CFP00 = DAG.isConstantFPBuildVectorOrConstantFP(N0.getOperand(0));
// (fadd (fadd x, x), x) -> (fmul x, 3.0)
if (!CFP00 && N0.getOperand(0) == N0.getOperand(1) &&
(N0.getOperand(0) == N1)) {
Expand All @@ -16960,8 +16955,7 @@ SDValue DAGCombiner::visitFADD(SDNode *N) {
}

if (N1.getOpcode() == ISD::FADD) {
SDNode *CFP10 =
DAG.isConstantFPBuildVectorOrConstantFP(N1.getOperand(0));
bool CFP10 = DAG.isConstantFPBuildVectorOrConstantFP(N1.getOperand(0));
// (fadd x, (fadd x, x)) -> (fmul x, 3.0)
if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
N1.getOperand(0) == N0) {
Expand Down
47 changes: 26 additions & 21 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6962,10 +6962,10 @@ void SelectionDAG::canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1,

// Canonicalize:
// binop(const, nonconst) -> binop(nonconst, const)
SDNode *N1C = isConstantIntBuildVectorOrConstantInt(N1);
SDNode *N2C = isConstantIntBuildVectorOrConstantInt(N2);
SDNode *N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
SDNode *N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
bool N1C = isConstantIntBuildVectorOrConstantInt(N1);
bool N2C = isConstantIntBuildVectorOrConstantInt(N2);
bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
if ((N1C && !N2C) || (N1CFP && !N2CFP))
std::swap(N1, N2);

Expand Down Expand Up @@ -13197,39 +13197,44 @@ bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
return true;
}

// Returns the SDNode if it is a constant integer BuildVector
// or constant integer.
SDNode *SelectionDAG::isConstantIntBuildVectorOrConstantInt(SDValue N) const {
if (isa<ConstantSDNode>(N))
return N.getNode();
// Returns true if it is a constant integer BuildVector or constant integer,
// possibly hidden by a bitcast.
bool SelectionDAG::isConstantIntBuildVectorOrConstantInt(
SDValue N, bool AllowOpaques) const {
N = peekThroughBitcasts(N);

if (auto *C = dyn_cast<ConstantSDNode>(N))
return AllowOpaques || !C->isOpaque();

if (ISD::isBuildVectorOfConstantSDNodes(N.getNode()))
return N.getNode();
return true;

// Treat a GlobalAddress supporting constant offset folding as a
// constant integer.
if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
if (GA->getOpcode() == ISD::GlobalAddress &&
TLI->isOffsetFoldingLegal(GA))
return GA;
return true;

if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
isa<ConstantSDNode>(N.getOperand(0)))
return N.getNode();
return nullptr;
return true;
return false;
}

// Returns the SDNode if it is a constant float BuildVector
// or constant float.
SDNode *SelectionDAG::isConstantFPBuildVectorOrConstantFP(SDValue N) const {
// Returns true if it is a constant float BuildVector or constant float.
bool SelectionDAG::isConstantFPBuildVectorOrConstantFP(SDValue N) const {
if (isa<ConstantFPSDNode>(N))
return N.getNode();
return true;

if (ISD::isBuildVectorOfConstantFPSDNodes(N.getNode()))
return N.getNode();
return true;

if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
isa<ConstantFPSDNode>(N.getOperand(0)))
return N.getNode();
return true;

return nullptr;
return false;
}

std::optional<bool> SelectionDAG::isBoolConstant(SDValue N,
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20713,7 +20713,7 @@ static SDValue performSubAddMULCombine(SDNode *N, SelectionDAG &DAG) {

if (!Add.hasOneUse())
return SDValue();
if (DAG.isConstantIntBuildVectorOrConstantInt(peekThroughBitcasts(X)))
if (DAG.isConstantIntBuildVectorOrConstantInt(X))
return SDValue();

SDValue M1 = Add.getOperand(0);
Expand Down
9 changes: 2 additions & 7 deletions llvm/lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56550,14 +56550,9 @@ static SDValue combineSub(SDNode *N, SelectionDAG &DAG,
SDValue Op1 = N->getOperand(1);
SDLoc DL(N);

// TODO: Add NoOpaque handling to isConstantIntBuildVectorOrConstantInt.
auto IsNonOpaqueConstant = [&](SDValue Op) {
if (SDNode *C = DAG.isConstantIntBuildVectorOrConstantInt(Op)) {
if (auto *Cst = dyn_cast<ConstantSDNode>(C))
return !Cst->isOpaque();
return true;
}
return false;
return DAG.isConstantIntBuildVectorOrConstantInt(Op,
/*AllowOpaques*/ false);
};

// X86 can't encode an immediate LHS of a sub. See if we can push the
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/X86/avx2-arith.ll
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ define <32 x i8> @mul_v32i8(<32 x i8> %i, <32 x i8> %j) nounwind readnone {
; CHECK-LABEL: mul_v32i8:
; CHECK: # %bb.0:
; CHECK-NEXT: vpbroadcastw {{.*#+}} ymm2 = [255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]
; CHECK-NEXT: vpand %ymm1, %ymm2, %ymm3
; CHECK-NEXT: vpand %ymm2, %ymm1, %ymm3
; CHECK-NEXT: vpmaddubsw %ymm3, %ymm0, %ymm3
; CHECK-NEXT: vpand %ymm2, %ymm3, %ymm3
; CHECK-NEXT: vpandn %ymm1, %ymm2, %ymm1
Expand Down
9 changes: 4 additions & 5 deletions llvm/test/CodeGen/X86/combine-sra.ll
Original file line number Diff line number Diff line change
Expand Up @@ -725,12 +725,11 @@ define <4 x i64> @combine_vec4i64_ashr_clamped(<4 x i64> %x, <4 x i64> %y) {
; SSE41: # %bb.0:
; SSE41-NEXT: movdqa %xmm0, %xmm4
; SSE41-NEXT: movdqa {{.*#+}} xmm7 = [9223372039002259456,9223372039002259456]
; SSE41-NEXT: movdqa %xmm3, %xmm0
; SSE41-NEXT: pxor %xmm7, %xmm0
; SSE41-NEXT: movdqa %xmm3, %xmm6
; SSE41-NEXT: pxor %xmm7, %xmm6
; SSE41-NEXT: movdqa {{.*#+}} xmm8 = [9223372039002259519,9223372039002259519]
; SSE41-NEXT: movdqa %xmm8, %xmm6
; SSE41-NEXT: pcmpeqd %xmm0, %xmm6
; SSE41-NEXT: pshufd {{.*#+}} xmm9 = xmm0[0,0,2,2]
; SSE41-NEXT: pshufd {{.*#+}} xmm9 = xmm6[0,0,2,2]
; SSE41-NEXT: pcmpeqd %xmm8, %xmm6
; SSE41-NEXT: movdqa {{.*#+}} xmm5 = [2147483711,2147483711,2147483711,2147483711]
; SSE41-NEXT: movdqa %xmm5, %xmm0
; SSE41-NEXT: pcmpgtd %xmm9, %xmm0
Expand Down
Loading
Loading