diff --git a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h --- a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h +++ b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h @@ -103,6 +103,7 @@ uint64_t getAllocaSizeInBytes(const AllocaInst &AI); bool alignAndPadAlloca(memtag::AllocaInfo &Info, llvm::Align Align); +void replaceAllVariableLocationOps(AllocaInfo &Info, Value *Old, Value *New); } // namespace memtag } // namespace llvm diff --git a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp --- a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp +++ b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp @@ -538,7 +538,7 @@ int NextTag = 0; for (auto &I : SInfo.AllocasToInstrument) { - const memtag::AllocaInfo &Info = I.second; + memtag::AllocaInfo &Info = I.second; AllocaInst *AI = Info.AI; int Tag = NextTag; NextTag = (NextTag + 1) % 16; @@ -593,8 +593,7 @@ } // Fixup debug intrinsics to point to the new alloca. - for (auto DVI : Info.DbgVariableIntrinsics) - DVI->replaceVariableLocationOp(Info.OldAI, Info.AI); + memtag::replaceAllVariableLocationOps(Info, Info.OldAI, Info.AI); } // If we have instrumented at least one alloca, all unrecognized lifetime diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp --- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp @@ -1379,16 +1379,7 @@ } } if (memtag::alignAndPadAlloca(Info, Align(Mapping.getObjectAlignment()))) { - for (auto DVI : Info.DbgVariableIntrinsics) { - SmallDenseSet LocationOps(DVI->location_ops().begin(), - DVI->location_ops().end()); - for (Value *V : LocationOps) { - if (auto *AI = dyn_cast_or_null(V)) { - if (V == AI) - DVI->replaceVariableLocationOp(V, Info.AI); - } - } - } + memtag::replaceAllVariableLocationOps(Info, AI, Info.AI); AI->eraseFromParent(); } } diff --git a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp --- a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp +++ b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp @@ -143,5 +143,14 @@ return true; } +void replaceAllVariableLocationOps(AllocaInfo &Info, Value *Old, Value *New) { + for (auto DVI : Info.DbgVariableIntrinsics) { + for (size_t I = 0; I < DVI->getNumVariableLocationOps(); ++I) { + if (DVI->getVariableLocationOp(I) == Old) + DVI->replaceVariableLocationOp(I, New); + } + } +} + } // namespace memtag } // namespace llvm