Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -391,8 +391,8 @@ SDValue FindBetterChain(SDNode *N, SDValue Chain); /// Do FindBetterChain for a store and any possibly adjacent stores on - /// consecutive chains. - bool findBetterNeighborChains(StoreSDNode *St); + /// consecutive chains. Return true if found a better chain for St. + bool findBetterNeighborChains(SDValue &Chain, StoreSDNode *St); /// Match "(X shl/srl V1) & V2" where V2 may not be present. bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask); @@ -12106,7 +12106,7 @@ // Walk up chain skipping non-aliasing memory nodes, on this store and any // adjacent stores. - if (findBetterNeighborChains(ST)) { + if (findBetterNeighborChains(Chain, ST)) { // replaceStoreChain uses CombineTo, which handled all of the worklist // manipulation. Return the original node to not do anything else. return SDValue(ST, 0); @@ -14909,7 +14909,7 @@ return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other, Aliases); } -bool DAGCombiner::findBetterNeighborChains(StoreSDNode* St) { +bool DAGCombiner::findBetterNeighborChains(SDValue &Chain, StoreSDNode *St) { // This holds the base pointer, index, and the offset in bytes from the base // pointer. BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr(), DAG); @@ -14969,7 +14969,7 @@ } } - bool MadeChange = false; + bool MakdeChangeToSt = false; SmallVector, 8> BetterChains; for (StoreSDNode *ChainedStore : ChainedStores) { @@ -14977,7 +14977,8 @@ SDValue BetterChain = FindBetterChain(ChainedStore, Chain); if (Chain != BetterChain) { - MadeChange = true; + if (ChainedStore == St) + MakdeChangeToSt = true; BetterChains.push_back(std::make_pair(ChainedStore, BetterChain)); } } @@ -14987,7 +14988,10 @@ for (auto Replacement : BetterChains) replaceStoreChain(Replacement.first, Replacement.second); - return MadeChange; + if (!MakdeChangeToSt) + Chain = St->getChain(); + + return MakdeChangeToSt; } /// This is the entry point for the file.