diff --git a/llvm/lib/CodeGen/GlobalISel/LoadStoreOpt.cpp b/llvm/lib/CodeGen/GlobalISel/LoadStoreOpt.cpp --- a/llvm/lib/CodeGen/GlobalISel/LoadStoreOpt.cpp +++ b/llvm/lib/CodeGen/GlobalISel/LoadStoreOpt.cpp @@ -355,10 +355,10 @@ LLT::scalar(NumStores * SmallTy.getSizeInBits().getFixedSize()); // For each store, compute pairwise merged debug locs. - DebugLoc MergedLoc; - for (unsigned AIdx = 0, BIdx = 1; BIdx < NumStores; ++AIdx, ++BIdx) - MergedLoc = DILocation::getMergedLocation(Stores[AIdx]->getDebugLoc(), - Stores[BIdx]->getDebugLoc()); + DebugLoc MergedLoc = Stores.front()->getDebugLoc(); + for (auto *Store : drop_begin(Stores)) + MergedLoc = DILocation::getMergedLocation(MergedLoc, Store->getDebugLoc()); + Builder.setInstr(*Stores.back()); Builder.setDebugLoc(MergedLoc); diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/store-merging-debug.mir b/llvm/test/CodeGen/AArch64/GlobalISel/store-merging-debug.mir --- a/llvm/test/CodeGen/AArch64/GlobalISel/store-merging-debug.mir +++ b/llvm/test/CodeGen/AArch64/GlobalISel/store-merging-debug.mir @@ -127,7 +127,7 @@ ; CHECK-NEXT: [[PTR_ADD2:%[0-9]+]]:_(p0) = G_PTR_ADD [[COPY]], [[C6]](s64), debug-location !DILocation(line: 14, column: 1, scope: !5) ; CHECK-NEXT: DBG_VALUE [[PTR_ADD2]](p0), $noreg, !9, !DIExpression(), debug-location !DILocation(line: 14, column: 1, scope: !5) ; CHECK-NEXT: [[C7:%[0-9]+]]:_(s64) = G_CONSTANT i64 3940688328982532 - ; CHECK-NEXT: G_STORE [[C7]](s64), [[COPY]](p0), debug-location !DILocation(line: 9, column: 4, scope: !5) :: (store (s64), align 2) + ; CHECK-NEXT: G_STORE [[C7]](s64), [[COPY]](p0), debug-location !DILocation(line: 9, scope: !5) :: (store (s64), align 2) ; CHECK-NEXT: DBG_VALUE 3, $noreg, !9, !DIExpression(), debug-location !DILocation(line: 15, column: 1, scope: !5) ; CHECK-NEXT: RET_ReallyLR debug-location !DILocation(line: 16, column: 1, scope: !5) %0:_(p0) = COPY $x0, debug-location !11