@@ -1142,13 +1142,28 @@ bool MemOpt::mergeStore(StoreInst* LeadingStore,
11421142 for (auto & I : StoresToMerge) {
11431143 StoreInst* ST = cast<StoreInst>(std::get<0 >(I));
11441144 Value* Ptr = ST->getPointerOperand ();
1145+ // Stores merged in the previous iterations can get merged again, so we need
1146+ // to update ToOpt vector to avoid null instruction in there
1147+ ToOpt.erase (std::remove (ToOpt.begin (), ToOpt.end (), ST), ToOpt.end ());
11451148 ST->eraseFromParent ();
11461149 RecursivelyDeleteTriviallyDeadInstructions (Ptr);
1147- // Mark it as already merged.
1148- std::get<2 >(I)->first = nullptr ;
1149- // Checking zero distance is intentionally omitted here due to the first
1150- // memory access won't be able to be checked again.
1151- std::get<2 >(I)->second -= 1 ;
1150+
1151+ if (std::get<2 >(I)->first == TailingStore)
1152+ // Writing NewStore to MemRefs for correct isSafeToMergeLoad working.
1153+ // For example if MemRefs contains this sequence: S1, S2, S3, L5, L6, L7, S4, L4
1154+ // after stores merge MemRefs contains : L5, L6, L7, S1234, L4 and loads are
1155+ // merged to L567, final instructions instructions sequence is L567, S1234, L4.
1156+ // Otherwise the sequence could be merged to sequence L4567, S1234 with
1157+ // unordered L4,S4 accesses.
1158+ std::get<2 >(I)->first = NewStore;
1159+ else {
1160+ // Mark it as already merged.
1161+ std::get<2 >(I)->first = nullptr ;
1162+ // Checking zero distance is intentionally omitted here due to the first
1163+ // memory access won't be able to be checked again.
1164+ std::get<2 >(I)->second -= 1 ;
1165+ }
1166+
11521167 }
11531168
11541169 return true ;
0 commit comments