Skip to content

Commit c8cc790

Browse files
authored
[SelectionDAG] Replace some basic patterns in visitADDLike with SDPatternMatch (#84759)
Resolves #84745. Based on SDPatternMatch introduced by #78654, this patch replaces some of basic patterns in `visitADDLike` with corresponding patterns in SDPatternMatch. This patch only replaces original folds, instead of introducing new ones.
1 parent fe8cf2f commit c8cc790

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "llvm/CodeGen/MachineFunction.h"
3939
#include "llvm/CodeGen/MachineMemOperand.h"
4040
#include "llvm/CodeGen/RuntimeLibcalls.h"
41+
#include "llvm/CodeGen/SDPatternMatch.h"
4142
#include "llvm/CodeGen/SelectionDAG.h"
4243
#include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
4344
#include "llvm/CodeGen/SelectionDAGNodes.h"
@@ -79,6 +80,7 @@
7980
#include "MatchContext.h"
8081

8182
using namespace llvm;
83+
using namespace llvm::SDPatternMatch;
8284

8385
#define DEBUG_TYPE "dagcombine"
8486

@@ -2697,52 +2699,45 @@ SDValue DAGCombiner::visitADDLike(SDNode *N) {
26972699
reassociateReduction(ISD::VECREDUCE_ADD, ISD::ADD, DL, VT, N0, N1))
26982700
return SD;
26992701
}
2702+
2703+
SDValue A, B, C;
2704+
27002705
// fold ((0-A) + B) -> B-A
2701-
if (N0.getOpcode() == ISD::SUB && isNullOrNullSplat(N0.getOperand(0)))
2702-
return DAG.getNode(ISD::SUB, DL, VT, N1, N0.getOperand(1));
2706+
if (sd_match(N0, m_Sub(m_Zero(), m_Value(A))))
2707+
return DAG.getNode(ISD::SUB, DL, VT, N1, A);
27032708

27042709
// fold (A + (0-B)) -> A-B
2705-
if (N1.getOpcode() == ISD::SUB && isNullOrNullSplat(N1.getOperand(0)))
2706-
return DAG.getNode(ISD::SUB, DL, VT, N0, N1.getOperand(1));
2710+
if (sd_match(N1, m_Sub(m_Zero(), m_Value(B))))
2711+
return DAG.getNode(ISD::SUB, DL, VT, N0, B);
27072712

27082713
// fold (A+(B-A)) -> B
2709-
if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
2710-
return N1.getOperand(0);
2714+
if (sd_match(N1, m_Sub(m_Value(B), m_Specific(N0))))
2715+
return B;
27112716

27122717
// fold ((B-A)+A) -> B
2713-
if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
2714-
return N0.getOperand(0);
2718+
if (sd_match(N0, m_Sub(m_Value(B), m_Specific(N1))))
2719+
return B;
27152720

27162721
// fold ((A-B)+(C-A)) -> (C-B)
2717-
if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB &&
2718-
N0.getOperand(0) == N1.getOperand(1))
2719-
return DAG.getNode(ISD::SUB, DL, VT, N1.getOperand(0),
2720-
N0.getOperand(1));
2722+
if (sd_match(N0, m_Sub(m_Value(A), m_Value(B))) &&
2723+
sd_match(N1, m_Sub(m_Value(C), m_Specific(A))))
2724+
return DAG.getNode(ISD::SUB, DL, VT, C, B);
27212725

27222726
// fold ((A-B)+(B-C)) -> (A-C)
2723-
if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB &&
2724-
N0.getOperand(1) == N1.getOperand(0))
2725-
return DAG.getNode(ISD::SUB, DL, VT, N0.getOperand(0),
2726-
N1.getOperand(1));
2727+
if (sd_match(N0, m_Sub(m_Value(A), m_Value(B))) &&
2728+
sd_match(N1, m_Sub(m_Specific(B), m_Value(C))))
2729+
return DAG.getNode(ISD::SUB, DL, VT, A, C);
27272730

27282731
// fold (A+(B-(A+C))) to (B-C)
2729-
if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
2730-
N0 == N1.getOperand(1).getOperand(0))
2731-
return DAG.getNode(ISD::SUB, DL, VT, N1.getOperand(0),
2732-
N1.getOperand(1).getOperand(1));
2733-
27342732
// fold (A+(B-(C+A))) to (B-C)
2735-
if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
2736-
N0 == N1.getOperand(1).getOperand(1))
2737-
return DAG.getNode(ISD::SUB, DL, VT, N1.getOperand(0),
2738-
N1.getOperand(1).getOperand(0));
2733+
if (sd_match(N1, m_Sub(m_Value(B), m_Add(m_Specific(N0), m_Value(C)))))
2734+
return DAG.getNode(ISD::SUB, DL, VT, B, C);
27392735

27402736
// fold (A+((B-A)+or-C)) to (B+or-C)
2741-
if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
2742-
N1.getOperand(0).getOpcode() == ISD::SUB &&
2743-
N0 == N1.getOperand(0).getOperand(1))
2744-
return DAG.getNode(N1.getOpcode(), DL, VT, N1.getOperand(0).getOperand(0),
2745-
N1.getOperand(1));
2737+
if (sd_match(N1,
2738+
m_AnyOf(m_Add(m_Sub(m_Value(B), m_Specific(N0)), m_Value(C)),
2739+
m_Sub(m_Sub(m_Value(B), m_Specific(N0)), m_Value(C)))))
2740+
return DAG.getNode(N1.getOpcode(), DL, VT, B, C);
27462741

27472742
// fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
27482743
if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB &&

0 commit comments

Comments
 (0)