Skip to content

Commit e17f07c

Browse files
authored
[SelectionDAG] Reduce code duplication between getStore, getTruncStore, and getIndexedStore. (llvm#137435)
Create an extra overload of getStore that can handle of the 3 types of stores. This is similar to how getLoad/getExtLoad/getIndexLoad is structure.
1 parent f3a61f6 commit e17f07c

File tree

2 files changed

+41
-71
lines changed

2 files changed

+41
-71
lines changed

llvm/include/llvm/CodeGen/SelectionDAG.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,9 @@ class SelectionDAG {
14661466
SDValue Ptr, EVT SVT, MachineMemOperand *MMO);
14671467
SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base,
14681468
SDValue Offset, ISD::MemIndexedMode AM);
1469+
SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr,
1470+
SDValue Offset, EVT SVT, MachineMemOperand *MMO,
1471+
ISD::MemIndexedMode AM, bool IsTruncating = false);
14691472

14701473
SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT,
14711474
const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset,

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 38 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9439,26 +9439,51 @@ SDValue SelectionDAG::getStore(SDValue Chain, const SDLoc &dl, SDValue Val,
94399439

94409440
SDValue SelectionDAG::getStore(SDValue Chain, const SDLoc &dl, SDValue Val,
94419441
SDValue Ptr, MachineMemOperand *MMO) {
9442-
assert(Chain.getValueType() == MVT::Other &&
9443-
"Invalid chain type");
9444-
EVT VT = Val.getValueType();
9445-
SDVTList VTs = getVTList(MVT::Other);
94469442
SDValue Undef = getUNDEF(Ptr.getValueType());
9447-
SDValue Ops[] = { Chain, Val, Ptr, Undef };
9443+
return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9444+
ISD::UNINDEXED);
9445+
}
9446+
9447+
SDValue SelectionDAG::getStore(SDValue Chain, const SDLoc &dl, SDValue Val,
9448+
SDValue Ptr, SDValue Offset, EVT SVT,
9449+
MachineMemOperand *MMO, ISD::MemIndexedMode AM,
9450+
bool IsTruncating) {
9451+
assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9452+
EVT VT = Val.getValueType();
9453+
if (VT == SVT) {
9454+
IsTruncating = false;
9455+
} else if (!IsTruncating) {
9456+
assert(VT == SVT && "No-truncating store from different memory type!");
9457+
} else {
9458+
assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
9459+
"Should only be a truncating store, not extending!");
9460+
assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9461+
assert(VT.isVector() == SVT.isVector() &&
9462+
"Cannot use trunc store to convert to or from a vector!");
9463+
assert((!VT.isVector() ||
9464+
VT.getVectorElementCount() == SVT.getVectorElementCount()) &&
9465+
"Cannot use trunc store to change the number of vector elements!");
9466+
}
9467+
9468+
bool Indexed = AM != ISD::UNINDEXED;
9469+
assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9470+
SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9471+
: getVTList(MVT::Other);
9472+
SDValue Ops[] = {Chain, Val, Ptr, Offset};
94489473
FoldingSetNodeID ID;
94499474
AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9450-
ID.AddInteger(VT.getRawBits());
9475+
ID.AddInteger(SVT.getRawBits());
94519476
ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9452-
dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
9477+
dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
94539478
ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
94549479
ID.AddInteger(MMO->getFlags());
94559480
void *IP = nullptr;
94569481
if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
94579482
cast<StoreSDNode>(E)->refineAlignment(MMO);
94589483
return SDValue(E, 0);
94599484
}
9460-
auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9461-
ISD::UNINDEXED, false, VT, MMO);
9485+
auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9486+
IsTruncating, SVT, MMO);
94629487
createOperands(N, Ops);
94639488

94649489
CSEMap.InsertNode(N, IP);
@@ -9491,76 +9516,18 @@ SDValue SelectionDAG::getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
94919516
SDValue SelectionDAG::getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val,
94929517
SDValue Ptr, EVT SVT,
94939518
MachineMemOperand *MMO) {
9494-
EVT VT = Val.getValueType();
9495-
9496-
assert(Chain.getValueType() == MVT::Other &&
9497-
"Invalid chain type");
9498-
if (VT == SVT)
9499-
return getStore(Chain, dl, Val, Ptr, MMO);
9500-
9501-
assert(SVT.getScalarType().bitsLT(VT.getScalarType()) &&
9502-
"Should only be a truncating store, not extending!");
9503-
assert(VT.isInteger() == SVT.isInteger() &&
9504-
"Can't do FP-INT conversion!");
9505-
assert(VT.isVector() == SVT.isVector() &&
9506-
"Cannot use trunc store to convert to or from a vector!");
9507-
assert((!VT.isVector() ||
9508-
VT.getVectorElementCount() == SVT.getVectorElementCount()) &&
9509-
"Cannot use trunc store to change the number of vector elements!");
9510-
9511-
SDVTList VTs = getVTList(MVT::Other);
95129519
SDValue Undef = getUNDEF(Ptr.getValueType());
9513-
SDValue Ops[] = { Chain, Val, Ptr, Undef };
9514-
FoldingSetNodeID ID;
9515-
AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9516-
ID.AddInteger(SVT.getRawBits());
9517-
ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9518-
dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
9519-
ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9520-
ID.AddInteger(MMO->getFlags());
9521-
void *IP = nullptr;
9522-
if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9523-
cast<StoreSDNode>(E)->refineAlignment(MMO);
9524-
return SDValue(E, 0);
9525-
}
9526-
auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9527-
ISD::UNINDEXED, true, SVT, MMO);
9528-
createOperands(N, Ops);
9529-
9530-
CSEMap.InsertNode(N, IP);
9531-
InsertNode(N);
9532-
SDValue V(N, 0);
9533-
NewSDValueDbgMsg(V, "Creating new node: ", this);
9534-
return V;
9520+
return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
95359521
}
95369522

95379523
SDValue SelectionDAG::getIndexedStore(SDValue OrigStore, const SDLoc &dl,
95389524
SDValue Base, SDValue Offset,
95399525
ISD::MemIndexedMode AM) {
95409526
StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
95419527
assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9542-
SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9543-
SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
9544-
FoldingSetNodeID ID;
9545-
AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9546-
ID.AddInteger(ST->getMemoryVT().getRawBits());
9547-
ID.AddInteger(ST->getRawSubclassData());
9548-
ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9549-
ID.AddInteger(ST->getMemOperand()->getFlags());
9550-
void *IP = nullptr;
9551-
if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9552-
return SDValue(E, 0);
9553-
9554-
auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9555-
ST->isTruncatingStore(), ST->getMemoryVT(),
9556-
ST->getMemOperand());
9557-
createOperands(N, Ops);
9558-
9559-
CSEMap.InsertNode(N, IP);
9560-
InsertNode(N);
9561-
SDValue V(N, 0);
9562-
NewSDValueDbgMsg(V, "Creating new node: ", this);
9563-
return V;
9528+
return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9529+
ST->getMemoryVT(), ST->getMemOperand(), AM,
9530+
ST->isTruncatingStore());
95649531
}
95659532

95669533
SDValue SelectionDAG::getLoadVP(

0 commit comments

Comments
 (0)