@@ -1137,11 +1137,23 @@ bool MemOpt::mergeStore(StoreInst* LeadingStore,
11371137 Value* Ptr = ST->getPointerOperand ();
11381138 ST->eraseFromParent ();
11391139 RecursivelyDeleteTriviallyDeadInstructions (Ptr);
1140- // Mark it as already merged.
1141- std::get<2 >(I)->first = nullptr ;
1142- // Checking zero distance is intentionally omitted here due to the first
1143- // memory access won't be able to be checked again.
1144- std::get<2 >(I)->second -= 1 ;
1140+
1141+ if (std::get<2 >(I)->first == TailingStore)
1142+ // Writing NewStore to MemRefs for correct isSafeToMergeLoad working.
1143+ // For example if MemRefs contains this sequence: S1, S2, S3, L5, L6, L7, S4, L4
1144+ // after stores merge MemRefs contains : L5, L6, L7, S1234, L4 and loads are
1145+ // merged to L567, final instructions instructions sequence is L567, S1234, L4.
1146+ // Otherwise the sequence could be merged to sequence L4567, S1234 with
1147+ // unordered L4,S4 accesses.
1148+ std::get<2 >(I)->first = NewStore;
1149+ else {
1150+ // Mark it as already merged.
1151+ std::get<2 >(I)->first = nullptr ;
1152+ // Checking zero distance is intentionally omitted here due to the first
1153+ // memory access won't be able to be checked again.
1154+ std::get<2 >(I)->second -= 1 ;
1155+ }
1156+
11451157 }
11461158
11471159 return true ;
0 commit comments