https://github.com/ndrewh updated 
https://github.com/llvm/llvm-project/pull/236278

>From d1e7030ba4bb9b35a030a1b9f9cf6278b1007e60 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <[email protected]>
Date: Tue, 27 Oct 2026 11:11:51 -0700
Subject: [PATCH 1/4] [AArch64][MTE] Tag dynamic allocas in AArch64StackTagging

Instrument dynamic allocas in the memtag-stack pass, more or less
following the the way AddressSanitizer does: by instrumenting
stackrestore to un(poison/tag) allocas when free'd.

- Dynamic allocas are given 16-byte alignment and padded to 16-bytes.
- At lifetime.start (or at the alloca if no lifetimes), the dynamic
alloca is tagged.
- The most recently executed dynamic alloca is tracked in an extra
per-func static alloca. The address of this static alloca is used
as the bottom (highest address) of the dynamic alloca section of
the frame, while the value stored there is treated as the current
top (lowest address) of the frame.
- At llvm.stackrestores, memory is untagged from the restored sp
to the last executed dynamic alloca.
- At function returns, memory is untagged from the last executed
dynamic alloca to the bottom of the (dynamic alloca section of) the
frame.

Assisted-By: Claude
---
 .../Transforms/Utils/MemoryTaggingSupport.h   |   8 +-
 llvm/lib/Target/AArch64/AArch64Options.td     |   2 +
 .../Target/AArch64/AArch64StackTagging.cpp    | 176 +++-
 .../Transforms/Utils/MemoryTaggingSupport.cpp |  24 +-
 .../stack-tagging-dbg-declare-tag-offset.ll   |  18 +
 .../stack-tagging-dynamic-alloca-history.ll   | 100 +++
 .../AArch64/stack-tagging-dynamic-alloca.ll   | 779 ++++++++++++++++++
 llvm/test/CodeGen/AArch64/stack-tagging.ll    |  12 +-
 8 files changed, 1097 insertions(+), 22 deletions(-)
 create mode 100644 
llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca-history.ll
 create mode 100644 llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll

diff --git a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h 
b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
index 5590109adcbc5b..02582dbb8489bb 100644
--- a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
+++ b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
@@ -62,6 +62,7 @@ LLVM_ABI Instruction 
*getUntagLocationIfFunctionExit(Instruction &Inst);
 struct StackInfo {
   MapVector<AllocaInst *, AllocaInfo> AllocasToInstrument;
   SmallVector<Instruction *, 8> RetVec;
+  SmallVector<IntrinsicInst *, 4> StackRestores;
   bool CallsReturnTwice = false;
 };
 
@@ -76,8 +77,10 @@ enum class AllocaInterestingness {
 
 class StackInfoBuilder {
 public:
-  StackInfoBuilder(const StackSafetyGlobalInfo *SSI, const char *DebugType)
-      : SSI(SSI), DebugType(DebugType) {}
+  StackInfoBuilder(const StackSafetyGlobalInfo *SSI, const char *DebugType,
+                   bool InstrumentDynamicAllocas = false)
+      : SSI(SSI), DebugType(DebugType),
+        InstrumentDynamicAllocas(InstrumentDynamicAllocas) {}
 
   LLVM_ABI void visit(OptimizationRemarkEmitter &ORE, Instruction &Inst);
   LLVM_ABI AllocaInterestingness getAllocaInterestingness(const AllocaInst 
&AI);
@@ -87,6 +90,7 @@ class StackInfoBuilder {
   StackInfo Info;
   const StackSafetyGlobalInfo *SSI;
   const char *DebugType;
+  bool InstrumentDynamicAllocas;
 };
 
 LLVM_ABI uint64_t getAllocaSizeInBytes(const AllocaInst &AI);
diff --git a/llvm/lib/Target/AArch64/AArch64Options.td 
b/llvm/lib/Target/AArch64/AArch64Options.td
index 681c751d4dc60f..8c0142ac5b8ce1 100644
--- a/llvm/lib/Target/AArch64/AArch64Options.td
+++ b/llvm/lib/Target/AArch64/AArch64Options.td
@@ -186,6 +186,8 @@ defm : BoolField<"aarch64-slh-loads", "true",
   "Sanitize loads from memory">;
 
 // AArch64StackTagging.cpp
+defm : BoolField<"stack-tagging-dynamic-allocas", "true",
+  "Instrument dynamic allocas">;
 defm : OptionalBoolField<"stack-tagging-merge-init",
   "merge stack variable initializers with tagging when possible">;
 defm : ValueField<"stack-tagging-merge-init-scan-limit", "unsigned", "40",
diff --git a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp 
b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
index 7acb45333b2913..9a647563755d42 100644
--- a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
+++ b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
@@ -282,7 +282,7 @@ class AArch64StackTagging : public FunctionPass {
   Instruction *insertBaseTaggedPointer(
       const Module &M,
       const MapVector<AllocaInst *, memtag::AllocaInfo> &Allocas,
-      const DominatorTree *DT);
+      const DominatorTree *DT, const LoopInfo *LI);
   bool runOnFunction(Function &F) override;
 
   StringRef getPassName() const override { return "AArch64 Stack Tagging"; }
@@ -295,6 +295,16 @@ class AArch64StackTagging : public FunctionPass {
   AAResults *AA = nullptr;
   const StackSafetyGlobalInfo *SSI = nullptr;
 
+  // Dynamic allocas are handled as in AddressSanitizer. This holds the address
+  // of the most recent one.
+  AllocaInst *DynamicAllocaLayout = nullptr;
+
+  void createDynamicAllocasInitStorage();
+  void handleDynamicAllocaCall(memtag::AllocaInfo &Info, Value *Base,
+                               unsigned Tag);
+  void untagDynamicAllocasBeforeInst(Instruction *InstBefore, Value *End);
+  void untagDynamicAllocas(const memtag::StackInfo &SInfo);
+
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.setPreservesCFG();
     if (UseStackSafety)
@@ -431,10 +441,134 @@ static Value *getSlotPtr(const AArch64Options &CLOpts, 
IRBuilder<> &IRB,
   return nullptr;
 }
 
+void AArch64StackTagging::createDynamicAllocasInitStorage() {
+  IRBuilder<> IRB(&*F->getEntryBlock().getFirstInsertionPt());
+  Type *IntptrTy = IRB.getIntPtrTy(*DL);
+  DynamicAllocaLayout =
+      IRB.CreateAlloca(IntptrTy, nullptr, "dynamic.alloca.layout");
+  // 0 means that no alloca has been made.
+  IRB.CreateStore(Constant::getNullValue(IntptrTy), DynamicAllocaLayout);
+  // Its address is the end of the memory untagged on return, which must be
+  // granule aligned.
+  DynamicAllocaLayout->setAlignment(kTagGranuleSize);
+}
+
+// Replace the dynamic alloca with one whose size is a multiple of the tag
+// granule, and tag the memory it returns.
+void AArch64StackTagging::handleDynamicAllocaCall(memtag::AllocaInfo &Info,
+                                                  Value *Base, unsigned Tag) {
+  AllocaInst *AI = Info.AI;
+  IRBuilder<> IRB(AI);
+  Type *IntptrTy = IRB.getIntPtrTy(*DL);
+  const uint64_t Granule = kTagGranuleSize.value();
+  Value *Size = IRB.CreateAllocationSize(IntptrTy, AI);
+  // Rounded up to a multiple of the granule.
+  Value *PaddedSize = IRB.CreateAnd(
+      IRB.CreateAdd(Size, ConstantInt::get(IntptrTy, Granule - 1)),
+      ConstantInt::get(IntptrTy, ~(Granule - 1)));
+
+  AllocaInst *NewAI =
+      IRB.CreateAlloca(IRB.getInt8Ty(), AI->getAddressSpace(), PaddedSize);
+  NewAI->setAlignment(std::max(AI->getAlign(), kTagGranuleSize));
+  NewAI->takeName(AI);
+  NewAI->copyMetadata(*AI);
+  AI->replaceAllUsesWith(NewAI);
+  AI->eraseFromParent();
+  Info.AI = NewAI;
+
+  // Replace alloca with tagp(alloca).
+  IRB.SetInsertPoint(NewAI->getNextNode());
+  Instruction *TagPCall = IRB.CreateIntrinsicWithoutFolding(
+      Intrinsic::aarch64_tagp, {NewAI->getType()},
+      {Constant::getNullValue(NewAI->getType()), Base, IRB.getInt64(Tag)});
+  if (NewAI->hasName())
+    TagPCall->setName(NewAI->getName() + ".tag");
+  // Does not replace metadata, so we don't have to handle DbgVariableRecords.
+  NewAI->replaceUsesWithIf(TagPCall, [&](const Use &U) {
+    return !isa<LifetimeIntrinsic>(U.getUser());
+  });
+  TagPCall->setOperand(0, NewAI);
+
+  // Store the last alloca's address to DynamicAllocaLayout. This points to the
+  // most recently executed dynamic alloca, which is used when untagging. It is
+  // granule aligned.
+  IRB.CreateStore(IRB.CreatePtrToInt(NewAI, IntptrTy), DynamicAllocaLayout);
+
+  // Tag the memory at llvm.lifetime.start if there is one, and untag it at
+  // llvm.lifetime.end. NewAI, and so TagPCall, is granule aligned, and TagSize
+  // is a multiple of the granule.
+  Value *TagSize = IRB.CreateZExtOrTrunc(PaddedSize, IRB.getInt64Ty());
+  SmallVector<LifetimeIntrinsic *, 4> Lifetimes;
+  for (User *U : NewAI->users())
+    if (auto *II = dyn_cast<LifetimeIntrinsic>(U))
+      Lifetimes.push_back(II);
+  if (none_of(Lifetimes, [](LifetimeIntrinsic *II) {
+        return II->getIntrinsicID() == Intrinsic::lifetime_start;
+      }))
+    IRB.CreateCall(SetTagFunc, {TagPCall, TagSize});
+  for (LifetimeIntrinsic *II : Lifetimes) {
+    if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
+      IRB.SetInsertPoint(II->getNextNode());
+      IRB.CreateCall(SetTagFunc, {TagPCall, TagSize});
+    } else {
+      // NewAI has tag 0.
+      IRB.SetInsertPoint(II);
+      IRB.CreateCall(SetTagFunc, {NewAI, TagSize});
+    }
+  }
+}
+
+// Untag the memory from the most recent dynamic alloca up to End, a granule
+// aligned address, unless the range is empty or no alloca has been made.
+void AArch64StackTagging::untagDynamicAllocasBeforeInst(Instruction 
*InstBefore,
+                                                        Value *End) {
+  IRBuilder<> IRB(InstBefore);
+  Type *IntptrTy = IRB.getIntPtrTy(*DL);
+  // Only 0 or the address of an alloca is stored to DynamicAllocaLayout, so
+  // this is granule aligned, and the size a multiple of the granule.
+  Value *LastAlloca =
+      IRB.CreateLoad(IntptrTy, DynamicAllocaLayout, "last.alloca");
+  Value *Zero = Constant::getNullValue(IntptrTy);
+  Value *Cond = IRB.CreateAnd(IRB.CreateICmpNE(LastAlloca, Zero),
+                              IRB.CreateICmpULE(LastAlloca, End));
+  Value *Size = IRB.CreateSelect(Cond, IRB.CreateSub(End, LastAlloca), Zero,
+                                 "untag.size");
+  // Tag 0.
+  IRB.CreateCall(SetTagFunc, {IRB.CreateIntToPtr(LastAlloca, IRB.getPtrTy()),
+                              IRB.CreateZExtOrTrunc(Size, IRB.getInt64Ty())});
+}
+
+// Untag dynamic allocas.
+void AArch64StackTagging::untagDynamicAllocas(const memtag::StackInfo &SInfo) {
+  Type *IntptrTy = DL->getIntPtrType(F->getContext());
+  // On return, untag up to DynamicAllocaLayout, which is above every dynamic
+  // alloca and granule aligned.
+  for (Instruction *Ret : SInfo.RetVec) {
+    IRBuilder<> IRB(Ret);
+    untagDynamicAllocasBeforeInst(
+        Ret, IRB.CreatePtrToInt(DynamicAllocaLayout, IntptrTy, "untag.end"));
+  }
+
+  // When we untag before @llvm.stackrestore, we need to adjust extracted SP to
+  // compute the address of the most recent alloca. We have a special
+  // @llvm.get.dynamic.area.offset intrinsic for this purpose. The SP comes 
from
+  // llvm.stacksave, SP is always 16-byte aligned on AArch64, and the offset is
+  // 0, so the result is granule aligned.
+  for (IntrinsicInst *StackRestoreInst : SInfo.StackRestores) {
+    IRBuilder<> IRB(StackRestoreInst);
+    Value *DynamicAreaOffset =
+        IRB.CreateIntrinsic(Intrinsic::get_dynamic_area_offset, {IntptrTy}, 
{});
+    Value *DynamicAreaPtr = IRB.CreateAdd(
+        IRB.CreatePtrToInt(StackRestoreInst->getOperand(0), IntptrTy),
+        DynamicAreaOffset, "untag.end");
+    untagDynamicAllocasBeforeInst(StackRestoreInst, DynamicAreaPtr);
+  }
+}
+
 Instruction *AArch64StackTagging::insertBaseTaggedPointer(
     const Module &M,
     const MapVector<AllocaInst *, memtag::AllocaInfo> &AllocasToInstrument,
-    const DominatorTree *DT) {
+    const DominatorTree *DT, const LoopInfo *LI) {
   BasicBlock *PrologueBB = nullptr;
   // Try sinking IRG as deep as possible to avoid hurting shrink wrap.
   for (auto &I : AllocasToInstrument) {
@@ -447,8 +581,12 @@ Instruction *AArch64StackTagging::insertBaseTaggedPointer(
     PrologueBB = DT->findNearestCommonDominator(PrologueBB, AI->getParent());
   }
   assert(PrologueBB);
+  // A dynamic alloca may be in a loop. Hoist IRG and the stack history record
+  // out of it, so that they run once per call.
+  while (const Loop *L = LI->getLoopFor(PrologueBB))
+    PrologueBB = DT->getNode(L->getHeader())->getIDom()->getBlock();
 
-  IRBuilder<> IRB(&PrologueBB->front());
+  IRBuilder<> IRB(&*PrologueBB->getFirstInsertionPt());
   Instruction *Base = IRB.CreateIntrinsicWithoutFolding(
       Intrinsic::aarch64_irg_sp, {},
       {Constant::getNullValue(IRB.getInt64Ty())});
@@ -504,11 +642,19 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
     ORE = DeleteORE.get();
   }
 
-  memtag::StackInfoBuilder SIB(SSI, DEBUG_TYPE);
+  memtag::StackInfoBuilder SIB(SSI, DEBUG_TYPE,
+                               CLOpts->stack_tagging_dynamic_allocas);
   for (Instruction &I : instructions(F))
     SIB.visit(*ORE, I);
   memtag::StackInfo &SInfo = SIB.get();
 
+  // A returns_twice call (e.g. setjmp, vfork or getcontext) can return again
+  // after the dynamic allocas made since were released without being untagged.
+  // Leave untagging them to whatever released them (e.g. longjmp).
+  if (SInfo.CallsReturnTwice)
+    SInfo.AllocasToInstrument.remove_if(
+        [](const auto &I) { return !I.first->isStaticAlloca(); });
+
   if (SInfo.AllocasToInstrument.empty())
     return false;
 
@@ -544,18 +690,32 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
   SetTagFunc = Intrinsic::getOrInsertDeclaration(F->getParent(),
                                                  Intrinsic::aarch64_settag);
 
-  Instruction *Base =
-      insertBaseTaggedPointer(*Fn.getParent(), SInfo.AllocasToInstrument, DT);
+  Instruction *Base = insertBaseTaggedPointer(
+      *Fn.getParent(), SInfo.AllocasToInstrument, DT, LI);
+
+  DynamicAllocaLayout = nullptr;
+  if (any_of(SInfo.AllocasToInstrument,
+             [](const auto &I) { return !I.first->isStaticAlloca(); })) {
+    createDynamicAllocasInitStorage();
+    // Untag dynamic allocas before the static ones are untagged at returns, so
+    // that those can still be merged into the epilogue.
+    untagDynamicAllocas(SInfo);
+  }
 
   unsigned int NextTag = 0;
   for (auto &I : SInfo.AllocasToInstrument) {
     memtag::AllocaInfo &Info = I.second;
     assert(Info.AI && SIB.getAllocaInterestingness(*Info.AI) ==
                           llvm::memtag::AllocaInterestingness::kInteresting);
-    memtag::alignAndPadAlloca(Info, kTagGranuleSize);
-    AllocaInst *AI = Info.AI;
     unsigned int Tag = NextTag;
     NextTag = (NextTag + 1) % 16;
+    if (!Info.AI->isStaticAlloca()) {
+      handleDynamicAllocaCall(Info, Base, Tag);
+      memtag::annotateDebugRecords(Info, Tag);
+      continue;
+    }
+    memtag::alignAndPadAlloca(Info, kTagGranuleSize);
+    AllocaInst *AI = Info.AI;
     // Replace alloca with tagp(alloca).
     IRBuilder<> IRB(Info.AI->getNextNode());
     Instruction *TagPCall = IRB.CreateIntrinsicWithoutFolding(
diff --git a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp 
b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
index ebca42f0e47577..fe8257ecbd8162 100644
--- a/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
+++ b/llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
@@ -123,6 +123,9 @@ void StackInfoBuilder::visit(OptimizationRemarkEmitter &ORE,
     if (CI->canReturnTwice()) {
       Info.CallsReturnTwice = true;
     }
+    if (auto *II = dyn_cast<IntrinsicInst>(CI))
+      if (II->getIntrinsicID() == Intrinsic::stackrestore)
+        Info.StackRestores.push_back(II);
   }
   if (AllocaInst *AI = dyn_cast<AllocaInst>(&Inst)) {
     switch (getAllocaInterestingness(*AI)) {
@@ -169,13 +172,20 @@ void StackInfoBuilder::visit(OptimizationRemarkEmitter 
&ORE,
 AllocaInterestingness
 StackInfoBuilder::getAllocaInterestingness(const AllocaInst &AI) {
   std::optional<TypeSize> Size = AI.getAllocationSize(AI.getDataLayout());
-  if (Size &&
-      // FIXME: support vscale.
-      !Size->isScalable() &&
-      // FIXME: instrument dynamic allocas, too
-      AI.isStaticAlloca() &&
-      // alloca() may be called with 0 size, ignore it.
-      !Size->isZero() &&
+  // alloca() may be called with 0 size, ignore it.
+  if (Size && Size->isZero())
+    return AllocaInterestingness::kUninteresting;
+  if (AI.isStaticAlloca()) {
+    // FIXME: support vscale.
+    if (!Size || Size->isScalable())
+      return AllocaInterestingness::kUninteresting;
+  } else {
+    // The size of a dynamic alloca is usually only known at run time.
+    // FIXME: support vscale.
+    if (!InstrumentDynamicAllocas || AI.getAllocatedType()->isScalableTy())
+      return AllocaInterestingness::kUninteresting;
+  }
+  if (
       // We are only interested in allocas not promotable to registers.
       // Promotable allocas are common under -O0.
       !isAllocaPromotable(&AI) &&
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging-dbg-declare-tag-offset.ll 
b/llvm/test/CodeGen/AArch64/stack-tagging-dbg-declare-tag-offset.ll
index 34527f52a242f0..32c3bedffc130a 100644
--- a/llvm/test/CodeGen/AArch64/stack-tagging-dbg-declare-tag-offset.ll
+++ b/llvm/test/CodeGen/AArch64/stack-tagging-dbg-declare-tag-offset.ll
@@ -25,6 +25,19 @@ entry:
   ret void, !dbg !15
 }
 
+; Dynamic allocas are annotated with their tag offsets too.
+define void @vla(i32 %n) sanitize_memtag !dbg !16 {
+entry:
+  %c = alloca i8, i32 %n
+  ; CHECK: #dbg_declare(ptr %c, {{.*}} !DIExpression(DW_OP_LLVM_tag_offset, 0)
+  call void @llvm.dbg.declare(metadata ptr %c, metadata !17, metadata 
!DIExpression()), !dbg !19
+  %d = alloca i8, i32 %n
+  ; CHECK: #dbg_declare(ptr %d, {{.*}} !DIExpression(DW_OP_LLVM_tag_offset, 1)
+  call void @llvm.dbg.declare(metadata ptr %d, metadata !18, metadata 
!DIExpression()), !dbg !19
+  call void @g(ptr %c, ptr %d, ptr null, ptr null, ptr null, ptr null)
+  ret void, !dbg !19
+}
+
 declare void @llvm.dbg.declare(metadata, metadata, metadata)
 
 !llvm.dbg.cu = !{!0}
@@ -48,3 +61,8 @@ DIFlagPrototyped, isOptimized: false, unit: !0, 
retainedNodes: !2)
 !13 = !DILocalVariable(name: "b", scope: !6, file: !1, line: 1, type: !9)
 !14 = !DILocation(line: 1, column: 29, scope: !6)
 !15 = !DILocation(line: 1, column: 37, scope: !6)
+!16 = distinct !DISubprogram(name: "vla", scope: !1, file: !1, line: 2, type: 
!20, spFlags: DISPFlagDefinition, unit: !0)
+!17 = !DILocalVariable(name: "c", scope: !16, file: !1, line: 2, type: !11)
+!18 = !DILocalVariable(name: "d", scope: !16, file: !1, line: 2, type: !11)
+!19 = !DILocation(line: 2, column: 1, scope: !16)
+!20 = !DISubroutineType(types: !2)
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca-history.ll 
b/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca-history.ll
new file mode 100644
index 00000000000000..aa810ffe7dfc2a
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca-history.ll
@@ -0,0 +1,100 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 
UTC_ARGS: --version 6
+; RUN: opt < %s -S -aarch64-stack-tagging -stack-tagging-use-stack-safety=0 -o 
- | FileCheck %s
+
+; Stack history is recorded by default on Darwin.
+
+target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128-Fn32"
+target triple = "arm64-apple-macosx"
+
+declare void @use(ptr)
+
+; With a dynamic alloca in a loop, the base tag and the stack history record 
are
+; hoisted out of the loop, so that they run once per call.
+define void @loop(i32 %n) sanitize_memtag {
+; CHECK-LABEL: define void @loop(
+; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[TMP0:%.*]] = call i64 @llvm.read_register.i64(metadata 
[[META0:![0-9]+]])
+; CHECK-NEXT:    [[TMP1:%.*]] = inttoptr i64 [[TMP0]] to ptr
+; CHECK-NEXT:    [[TMP2:%.*]] = getelementptr i8, ptr [[TMP1]], i32 1848
+; CHECK-NEXT:    [[TMP3:%.*]] = load i64, ptr [[TMP2]], align 8
+; CHECK-NEXT:    [[TMP4:%.*]] = call ptr @llvm.frameaddress.p0(i32 0)
+; CHECK-NEXT:    [[TMP5:%.*]] = ptrtoint ptr [[TMP4]] to i64
+; CHECK-NEXT:    [[TMP6:%.*]] = ptrtoint ptr [[BASETAG]] to i64
+; CHECK-NEXT:    [[TMP7:%.*]] = and i64 [[TMP6]], 1080863910568919040
+; CHECK-NEXT:    [[TMP8:%.*]] = or i64 [[TMP5]], [[TMP7]]
+; CHECK-NEXT:    [[TMP9:%.*]] = call i64 @llvm.read_register.i64(metadata 
[[META1:![0-9]+]])
+; CHECK-NEXT:    [[TMP10:%.*]] = inttoptr i64 [[TMP3]] to ptr
+; CHECK-NEXT:    store i64 [[TMP9]], ptr [[TMP10]], align 8
+; CHECK-NEXT:    [[TMP11:%.*]] = getelementptr i64, ptr [[TMP10]], i64 1
+; CHECK-NEXT:    store i64 [[TMP8]], ptr [[TMP11]], align 8
+; CHECK-NEXT:    [[TMP12:%.*]] = ashr i64 [[TMP3]], 60
+; CHECK-NEXT:    [[TMP13:%.*]] = shl nuw nsw i64 [[TMP12]], 12
+; CHECK-NEXT:    [[TMP14:%.*]] = xor i64 [[TMP13]], -1
+; CHECK-NEXT:    [[TMP15:%.*]] = add i64 [[TMP3]], 16
+; CHECK-NEXT:    [[TMP16:%.*]] = and i64 [[TMP15]], [[TMP14]]
+; CHECK-NEXT:    store i64 [[TMP16]], ptr [[TMP2]], align 8
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], 
%[[LOOP]] ]
+; CHECK-NEXT:    [[SP:%.*]] = call ptr @llvm.stacksave.p0()
+; CHECK-NEXT:    [[TMP17:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP18:%.*]] = mul i64 [[TMP17]], 1
+; CHECK-NEXT:    [[TMP19:%.*]] = add i64 [[TMP18]], 15
+; CHECK-NEXT:    [[TMP20:%.*]] = and i64 [[TMP19]], -16
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP20]], align 16
+; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP21:%.*]] = ptrtoint ptr [[VLA]] to i64
+; CHECK-NEXT:    store i64 [[TMP21]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP20]])
+; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
+; CHECK-NEXT:    [[TMP22:%.*]] = call i64 @llvm.get.dynamic.area.offset.i64()
+; CHECK-NEXT:    [[TMP23:%.*]] = ptrtoint ptr [[SP]] to i64
+; CHECK-NEXT:    [[UNTAG_END1:%.*]] = add i64 [[TMP23]], [[TMP22]]
+; CHECK-NEXT:    [[LAST_ALLOCA2:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP24:%.*]] = icmp ne i64 [[LAST_ALLOCA2]], 0
+; CHECK-NEXT:    [[TMP25:%.*]] = icmp ule i64 [[LAST_ALLOCA2]], [[UNTAG_END1]]
+; CHECK-NEXT:    [[TMP26:%.*]] = and i1 [[TMP24]], [[TMP25]]
+; CHECK-NEXT:    [[TMP27:%.*]] = sub i64 [[UNTAG_END1]], [[LAST_ALLOCA2]]
+; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP26]], i64 [[TMP27]], i64 0
+; CHECK-NEXT:    [[TMP28:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP28]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP]])
+; CHECK-NEXT:    [[I_NEXT]] = add i32 [[I]], 1
+; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[I_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[C]], label %[[LOOP]], label %[[EXIT:.*]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP29:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP30:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP31:%.*]] = and i1 [[TMP29]], [[TMP30]]
+; CHECK-NEXT:    [[TMP32:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP31]], i64 [[TMP32]], i64 0
+; CHECK-NEXT:    [[TMP33:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP33]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %loop
+
+loop:
+  %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
+  %sp = call ptr @llvm.stacksave.p0()
+  %vla = alloca i8, i32 %n, align 1
+  call void @use(ptr %vla)
+  call void @llvm.stackrestore.p0(ptr %sp)
+  %i.next = add i32 %i, 1
+  %c = icmp ult i32 %i.next, %n
+  br i1 %c, label %loop, label %exit
+
+exit:
+  ret void
+}
+;.
+; CHECK: [[META0]] = !{!"TPIDRRO_EL0"}
+; CHECK: [[META1]] = !{!"pc"}
+;.
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll 
b/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll
new file mode 100644
index 00000000000000..39b94346be9531
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll
@@ -0,0 +1,779 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 
UTC_ARGS: --version 6
+; RUN: opt < %s -S -aarch64-stack-tagging -stack-tagging-use-stack-safety=0 -o 
- | FileCheck %s
+; RUN: llc < %s -mattr=+mte -o - | FileCheck %s --check-prefix=ASM
+; RUN: opt < %s -S -aarch64-stack-tagging -stack-tagging-use-stack-safety=0 \
+; RUN:   -stack-tagging-dynamic-allocas=0 -o - | not grep dynamic.alloca.layout
+
+target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
+target triple = "aarch64"
+
+%struct.S = type { i32, i32, i32 }
+
+declare void @use(ptr)
+declare i1 @cond()
+declare void @abort() noreturn
+declare i32 @setjmp(ptr) returns_twice
+declare ptr @llvm.stacksave.p0()
+declare void @llvm.stackrestore.p0(ptr)
+declare void @llvm.lifetime.start.p0(ptr)
+declare void @llvm.lifetime.end.p0(ptr)
+
+; The size is rounded up to the tag granule and the allocation is tagged. Its
+; address is recorded, and the memory between the most recent alloca and the
+; address of the slot that holds it is untagged before returning.
+define void @vla(i32 %n) sanitize_memtag {
+; CHECK-LABEL: define void @vla(
+; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4:[0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 12
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP3]], align 16
+; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[VLA]] to i64
+; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP3]])
+; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP7:%.*]] = and i1 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    [[TMP8:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP7]], i64 [[TMP8]], i64 0
+; CHECK-NEXT:    [[TMP9:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP9]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %vla = alloca %struct.S, i32 %n, align 4
+  call void @use(ptr %vla)
+  ret void
+}
+
+; A constant-size alloca outside the entry block is dynamic.
+define void @const_size(i1 %c) sanitize_memtag {
+; CHECK-LABEL: define void @const_size(
+; CHECK-SAME: i1 [[C:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    br i1 [[C]], label %[[BODY:.*]], label %[[FAIL:.*]]
+; CHECK:       [[BODY]]:
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[BUF:%.*]] = alloca i8, i64 48, align 16
+; CHECK-NEXT:    [[BUF_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[BUF]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP0:%.*]] = ptrtoint ptr [[BUF]] to i64
+; CHECK-NEXT:    store i64 [[TMP0]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[BUF_TAG]], i64 48)
+; CHECK-NEXT:    call void @use(ptr [[BUF_TAG]])
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP3:%.*]] = and i1 [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[TMP4:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP3]], i64 [[TMP4]], i64 0
+; CHECK-NEXT:    [[TMP5:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP5]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+; CHECK:       [[FAIL]]:
+; CHECK-NEXT:    call void @abort()
+; CHECK-NEXT:    unreachable
+;
+entry:
+  br i1 %c, label %body, label %fail
+
+body:
+  %buf = alloca i8, i32 40, align 1
+  call void @use(ptr %buf)
+  ret void
+
+fail:
+  call void @abort()
+  unreachable
+}
+
+; As for static allocas, an alloca of size zero is not instrumented.
+define void @zero_size(i1 %c) sanitize_memtag {
+; CHECK-LABEL: define void @zero_size(
+; CHECK-SAME: i1 [[C:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    br i1 [[C]], label %[[BODY:.*]], label %[[FAIL:.*]]
+; CHECK:       [[BODY]]:
+; CHECK-NEXT:    [[BUF:%.*]] = alloca i8, i32 0, align 1
+; CHECK-NEXT:    call void @use(ptr [[BUF]])
+; CHECK-NEXT:    ret void
+; CHECK:       [[FAIL]]:
+; CHECK-NEXT:    call void @abort()
+; CHECK-NEXT:    unreachable
+;
+entry:
+  br i1 %c, label %body, label %fail
+
+body:
+  %buf = alloca i8, i32 0, align 1
+  call void @use(ptr %buf)
+  ret void
+
+fail:
+  call void @abort()
+  unreachable
+}
+
+; The memory released by an llvm.stackrestore is untagged before it.
+define void @scoped_vla(i32 %n) sanitize_memtag {
+; CHECK-LABEL: define void @scoped_vla(
+; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[SP:%.*]] = call ptr @llvm.stacksave.p0()
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 12
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP3]], align 16
+; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[VLA]] to i64
+; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP3]])
+; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
+; CHECK-NEXT:    [[TMP5:%.*]] = call i64 @llvm.get.dynamic.area.offset.i64()
+; CHECK-NEXT:    [[TMP6:%.*]] = ptrtoint ptr [[SP]] to i64
+; CHECK-NEXT:    [[UNTAG_END1:%.*]] = add i64 [[TMP6]], [[TMP5]]
+; CHECK-NEXT:    [[LAST_ALLOCA2:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp ne i64 [[LAST_ALLOCA2]], 0
+; CHECK-NEXT:    [[TMP8:%.*]] = icmp ule i64 [[LAST_ALLOCA2]], [[UNTAG_END1]]
+; CHECK-NEXT:    [[TMP9:%.*]] = and i1 [[TMP7]], [[TMP8]]
+; CHECK-NEXT:    [[TMP10:%.*]] = sub i64 [[UNTAG_END1]], [[LAST_ALLOCA2]]
+; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP9]], i64 [[TMP10]], i64 0
+; CHECK-NEXT:    [[TMP11:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP11]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP]])
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP12:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP13:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP14:%.*]] = and i1 [[TMP12]], [[TMP13]]
+; CHECK-NEXT:    [[TMP15:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP14]], i64 [[TMP15]], i64 0
+; CHECK-NEXT:    [[TMP16:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP16]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %sp = call ptr @llvm.stacksave.p0()
+  %vla = alloca %struct.S, i32 %n, align 4
+  call void @use(ptr %vla)
+  call void @llvm.stackrestore.p0(ptr %sp)
+  ret void
+}
+
+; Without optimization, the saved stack pointer is reloaded from a stack slot.
+define void @scoped_vla_slot(i32 %n) sanitize_memtag {
+; CHECK-LABEL: define void @scoped_vla_slot(
+; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[SAVED_STACK:%.*]] = alloca ptr, align 8
+; CHECK-NEXT:    [[SP:%.*]] = call ptr @llvm.stacksave.p0()
+; CHECK-NEXT:    store ptr [[SP]], ptr [[SAVED_STACK]], align 8
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP3]], align 16
+; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[VLA]] to i64
+; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP3]])
+; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
+; CHECK-NEXT:    [[RESTORED:%.*]] = load ptr, ptr [[SAVED_STACK]], align 8
+; CHECK-NEXT:    [[TMP5:%.*]] = call i64 @llvm.get.dynamic.area.offset.i64()
+; CHECK-NEXT:    [[TMP6:%.*]] = ptrtoint ptr [[RESTORED]] to i64
+; CHECK-NEXT:    [[UNTAG_END1:%.*]] = add i64 [[TMP6]], [[TMP5]]
+; CHECK-NEXT:    [[LAST_ALLOCA2:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp ne i64 [[LAST_ALLOCA2]], 0
+; CHECK-NEXT:    [[TMP8:%.*]] = icmp ule i64 [[LAST_ALLOCA2]], [[UNTAG_END1]]
+; CHECK-NEXT:    [[TMP9:%.*]] = and i1 [[TMP7]], [[TMP8]]
+; CHECK-NEXT:    [[TMP10:%.*]] = sub i64 [[UNTAG_END1]], [[LAST_ALLOCA2]]
+; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP9]], i64 [[TMP10]], i64 0
+; CHECK-NEXT:    [[TMP11:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP11]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[RESTORED]])
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP12:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP13:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP14:%.*]] = and i1 [[TMP12]], [[TMP13]]
+; CHECK-NEXT:    [[TMP15:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP14]], i64 [[TMP15]], i64 0
+; CHECK-NEXT:    [[TMP16:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP16]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %saved_stack = alloca ptr, align 8
+  %sp = call ptr @llvm.stacksave.p0()
+  store ptr %sp, ptr %saved_stack, align 8
+  %vla = alloca i8, i32 %n, align 1
+  call void @use(ptr %vla)
+  %restored = load ptr, ptr %saved_stack, align 8
+  call void @llvm.stackrestore.p0(ptr %restored)
+  ret void
+}
+
+; The restored stack pointer does not have to visibly come from an
+; llvm.stacksave.
+define void @unknown_restore(i32 %n, ptr %saved) sanitize_memtag {
+; CHECK-LABEL: define void @unknown_restore(
+; CHECK-SAME: i32 [[N:%.*]], ptr [[SAVED:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP3]], align 16
+; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[VLA]] to i64
+; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP3]])
+; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
+; CHECK-NEXT:    [[TMP5:%.*]] = call i64 @llvm.get.dynamic.area.offset.i64()
+; CHECK-NEXT:    [[TMP6:%.*]] = ptrtoint ptr [[SAVED]] to i64
+; CHECK-NEXT:    [[UNTAG_END1:%.*]] = add i64 [[TMP6]], [[TMP5]]
+; CHECK-NEXT:    [[LAST_ALLOCA2:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp ne i64 [[LAST_ALLOCA2]], 0
+; CHECK-NEXT:    [[TMP8:%.*]] = icmp ule i64 [[LAST_ALLOCA2]], [[UNTAG_END1]]
+; CHECK-NEXT:    [[TMP9:%.*]] = and i1 [[TMP7]], [[TMP8]]
+; CHECK-NEXT:    [[TMP10:%.*]] = sub i64 [[UNTAG_END1]], [[LAST_ALLOCA2]]
+; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP9]], i64 [[TMP10]], i64 0
+; CHECK-NEXT:    [[TMP11:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP11]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SAVED]])
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP12:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP13:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP14:%.*]] = and i1 [[TMP12]], [[TMP13]]
+; CHECK-NEXT:    [[TMP15:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP14]], i64 [[TMP15]], i64 0
+; CHECK-NEXT:    [[TMP16:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP16]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %vla = alloca i8, i32 %n, align 1
+  call void @use(ptr %vla)
+  call void @llvm.stackrestore.p0(ptr %saved)
+  ret void
+}
+
+; Allocas released in a loop body are tagged each iteration, and an inner
+; scope's restore only releases the inner alloca. The base tag is computed
+; once, before the loop.
+define void @nested_scopes(i32 %n) sanitize_memtag {
+; CHECK-LABEL: define void @nested_scopes(
+; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], 
%[[LATCH:.*]] ]
+; CHECK-NEXT:    [[SP_OUTER:%.*]] = call ptr @llvm.stacksave.p0()
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
+; CHECK-NEXT:    [[OUTER:%.*]] = alloca i8, i64 [[TMP3]], align 16
+; CHECK-NEXT:    [[OUTER_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr 
[[OUTER]], ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[OUTER]] to i64
+; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[OUTER_TAG]], i64 
[[TMP3]])
+; CHECK-NEXT:    call void @use(ptr [[OUTER_TAG]])
+; CHECK-NEXT:    [[C:%.*]] = call i1 @cond()
+; CHECK-NEXT:    br i1 [[C]], label %[[INNER_BB:.*]], label %[[LATCH]]
+; CHECK:       [[INNER_BB]]:
+; CHECK-NEXT:    [[SP_INNER:%.*]] = call ptr @llvm.stacksave.p0()
+; CHECK-NEXT:    [[TMP5:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP6:%.*]] = mul i64 [[TMP5]], 1
+; CHECK-NEXT:    [[TMP7:%.*]] = add i64 [[TMP6]], 15
+; CHECK-NEXT:    [[TMP8:%.*]] = and i64 [[TMP7]], -16
+; CHECK-NEXT:    [[INNER:%.*]] = alloca i8, i64 [[TMP8]], align 16
+; CHECK-NEXT:    [[INNER_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr 
[[INNER]], ptr [[BASETAG]], i64 1)
+; CHECK-NEXT:    [[TMP9:%.*]] = ptrtoint ptr [[INNER]] to i64
+; CHECK-NEXT:    store i64 [[TMP9]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[INNER_TAG]], i64 
[[TMP8]])
+; CHECK-NEXT:    call void @use(ptr [[INNER_TAG]])
+; CHECK-NEXT:    [[TMP10:%.*]] = call i64 @llvm.get.dynamic.area.offset.i64()
+; CHECK-NEXT:    [[TMP11:%.*]] = ptrtoint ptr [[SP_INNER]] to i64
+; CHECK-NEXT:    [[UNTAG_END1:%.*]] = add i64 [[TMP11]], [[TMP10]]
+; CHECK-NEXT:    [[LAST_ALLOCA2:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP12:%.*]] = icmp ne i64 [[LAST_ALLOCA2]], 0
+; CHECK-NEXT:    [[TMP13:%.*]] = icmp ule i64 [[LAST_ALLOCA2]], [[UNTAG_END1]]
+; CHECK-NEXT:    [[TMP14:%.*]] = and i1 [[TMP12]], [[TMP13]]
+; CHECK-NEXT:    [[TMP15:%.*]] = sub i64 [[UNTAG_END1]], [[LAST_ALLOCA2]]
+; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP14]], i64 [[TMP15]], i64 0
+; CHECK-NEXT:    [[TMP16:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP16]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP_INNER]])
+; CHECK-NEXT:    br label %[[LATCH]]
+; CHECK:       [[LATCH]]:
+; CHECK-NEXT:    [[TMP17:%.*]] = call i64 @llvm.get.dynamic.area.offset.i64()
+; CHECK-NEXT:    [[TMP18:%.*]] = ptrtoint ptr [[SP_OUTER]] to i64
+; CHECK-NEXT:    [[UNTAG_END4:%.*]] = add i64 [[TMP18]], [[TMP17]]
+; CHECK-NEXT:    [[LAST_ALLOCA5:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP19:%.*]] = icmp ne i64 [[LAST_ALLOCA5]], 0
+; CHECK-NEXT:    [[TMP20:%.*]] = icmp ule i64 [[LAST_ALLOCA5]], [[UNTAG_END4]]
+; CHECK-NEXT:    [[TMP21:%.*]] = and i1 [[TMP19]], [[TMP20]]
+; CHECK-NEXT:    [[TMP22:%.*]] = sub i64 [[UNTAG_END4]], [[LAST_ALLOCA5]]
+; CHECK-NEXT:    [[UNTAG_SIZE6:%.*]] = select i1 [[TMP21]], i64 [[TMP22]], i64 0
+; CHECK-NEXT:    [[TMP23:%.*]] = inttoptr i64 [[LAST_ALLOCA5]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP23]], i64 
[[UNTAG_SIZE6]])
+; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP_OUTER]])
+; CHECK-NEXT:    [[I_NEXT]] = add i32 [[I]], 1
+; CHECK-NEXT:    [[C2:%.*]] = icmp ult i32 [[I_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[C2]], label %[[LOOP]], label %[[EXIT:.*]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP24:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP25:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP26:%.*]] = and i1 [[TMP24]], [[TMP25]]
+; CHECK-NEXT:    [[TMP27:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP26]], i64 [[TMP27]], i64 0
+; CHECK-NEXT:    [[TMP28:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP28]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %loop
+
+loop:
+  %i = phi i32 [ 0, %entry ], [ %i.next, %latch ]
+  %sp.outer = call ptr @llvm.stacksave.p0()
+  %outer = alloca i8, i32 %n, align 1
+  call void @use(ptr %outer)
+  %c = call i1 @cond()
+  br i1 %c, label %inner.bb, label %latch
+
+inner.bb:
+  %sp.inner = call ptr @llvm.stacksave.p0()
+  %inner = alloca i8, i32 %n, align 1
+  call void @use(ptr %inner)
+  call void @llvm.stackrestore.p0(ptr %sp.inner)
+  br label %latch
+
+latch:
+  call void @llvm.stackrestore.p0(ptr %sp.outer)
+  %i.next = add i32 %i, 1
+  %c2 = icmp ult i32 %i.next, %n
+  br i1 %c2, label %loop, label %exit
+
+exit:
+  ret void
+}
+
+; Without static allocas, the base tag is inserted in the block of the dynamic
+; alloca, which may begin with PHIs. It has to go after them.
+define void @after_phi(i1 %c, i32 %a, i32 %b) sanitize_memtag {
+; CHECK-LABEL: define void @after_phi(
+; CHECK-SAME: i1 [[C:%.*]], i32 [[A:%.*]], i32 [[B:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    br i1 [[C]], label %[[T:.*]], label %[[MERGE:.*]]
+; CHECK:       [[T]]:
+; CHECK-NEXT:    br label %[[MERGE]]
+; CHECK:       [[MERGE]]:
+; CHECK-NEXT:    [[N:%.*]] = phi i32 [ [[A]], %[[ENTRY]] ], [ [[B]], %[[T]] ]
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP3]], align 16
+; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[VLA]] to i64
+; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP3]])
+; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP7:%.*]] = and i1 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    [[TMP8:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP7]], i64 [[TMP8]], i64 0
+; CHECK-NEXT:    [[TMP9:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP9]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  br i1 %c, label %t, label %merge
+
+t:
+  br label %merge
+
+merge:
+  %n = phi i32 [ %a, %entry ], [ %b, %t ]
+  %vla = alloca i8, i32 %n, align 1
+  call void @use(ptr %vla)
+  ret void
+}
+
+; An alloca that is not made on every path to the exit.
+define void @conditional(i32 %n) sanitize_memtag {
+; CHECK-LABEL: define void @conditional(
+; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[C:%.*]] = call i1 @cond()
+; CHECK-NEXT:    br i1 [[C]], label %[[THEN:.*]], label %[[EXIT:.*]]
+; CHECK:       [[THEN]]:
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP3]], align 16
+; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[VLA]] to i64
+; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP3]])
+; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
+; CHECK-NEXT:    br label %[[EXIT]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP7:%.*]] = and i1 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    [[TMP8:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP7]], i64 [[TMP8]], i64 0
+; CHECK-NEXT:    [[TMP9:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP9]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %c = call i1 @cond()
+  br i1 %c, label %then, label %exit
+
+then:
+  %vla = alloca i8, i32 %n, align 1
+  call void @use(ptr %vla)
+  br label %exit
+
+exit:
+  ret void
+}
+
+; A scope with an early exit before the alloca shares one restore, as a
+; compiler emits for a VLA declared after a conditional continue.
+define void @early_exit_in_scope(i32 %n) sanitize_memtag {
+; CHECK-LABEL: define void @early_exit_in_scope(
+; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[SP:%.*]] = call ptr @llvm.stacksave.p0()
+; CHECK-NEXT:    [[C:%.*]] = call i1 @cond()
+; CHECK-NEXT:    br i1 [[C]], label %[[CLEANUP:.*]], label %[[ALLOC:.*]]
+; CHECK:       [[ALLOC]]:
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP3]], align 16
+; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[VLA]] to i64
+; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP3]])
+; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
+; CHECK-NEXT:    br label %[[CLEANUP]]
+; CHECK:       [[CLEANUP]]:
+; CHECK-NEXT:    [[TMP5:%.*]] = call i64 @llvm.get.dynamic.area.offset.i64()
+; CHECK-NEXT:    [[TMP6:%.*]] = ptrtoint ptr [[SP]] to i64
+; CHECK-NEXT:    [[UNTAG_END1:%.*]] = add i64 [[TMP6]], [[TMP5]]
+; CHECK-NEXT:    [[LAST_ALLOCA2:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP7:%.*]] = icmp ne i64 [[LAST_ALLOCA2]], 0
+; CHECK-NEXT:    [[TMP8:%.*]] = icmp ule i64 [[LAST_ALLOCA2]], [[UNTAG_END1]]
+; CHECK-NEXT:    [[TMP9:%.*]] = and i1 [[TMP7]], [[TMP8]]
+; CHECK-NEXT:    [[TMP10:%.*]] = sub i64 [[UNTAG_END1]], [[LAST_ALLOCA2]]
+; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP9]], i64 [[TMP10]], i64 0
+; CHECK-NEXT:    [[TMP11:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP11]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP]])
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP12:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP13:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP14:%.*]] = and i1 [[TMP12]], [[TMP13]]
+; CHECK-NEXT:    [[TMP15:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP14]], i64 [[TMP15]], i64 0
+; CHECK-NEXT:    [[TMP16:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP16]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %sp = call ptr @llvm.stacksave.p0()
+  %c = call i1 @cond()
+  br i1 %c, label %cleanup, label %alloc
+
+alloc:
+  %vla = alloca i8, i32 %n, align 1
+  call void @use(ptr %vla)
+  br label %cleanup
+
+cleanup:
+  call void @llvm.stackrestore.p0(ptr %sp)
+  ret void
+}
+
+; The stacksave may be executed again after the alloca is made, in which case
+; the restore does not release it.
+define void @stacksave_reexecuted(i32 %n) sanitize_memtag {
+; CHECK-LABEL: define void @stacksave_reexecuted(
+; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    br label %[[HEADER:.*]]
+; CHECK:       [[HEADER]]:
+; CHECK-NEXT:    [[SP:%.*]] = call ptr @llvm.stacksave.p0()
+; CHECK-NEXT:    [[C:%.*]] = call i1 @cond()
+; CHECK-NEXT:    br i1 [[C]], label %[[RELEASE:.*]], label %[[EXIT:.*]]
+; CHECK:       [[RELEASE]]:
+; CHECK-NEXT:    [[TMP0:%.*]] = call i64 @llvm.get.dynamic.area.offset.i64()
+; CHECK-NEXT:    [[TMP1:%.*]] = ptrtoint ptr [[SP]] to i64
+; CHECK-NEXT:    [[UNTAG_END1:%.*]] = add i64 [[TMP1]], [[TMP0]]
+; CHECK-NEXT:    [[LAST_ALLOCA2:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i64 [[LAST_ALLOCA2]], 0
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp ule i64 [[LAST_ALLOCA2]], [[UNTAG_END1]]
+; CHECK-NEXT:    [[TMP4:%.*]] = and i1 [[TMP2]], [[TMP3]]
+; CHECK-NEXT:    [[TMP5:%.*]] = sub i64 [[UNTAG_END1]], [[LAST_ALLOCA2]]
+; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP4]], i64 [[TMP5]], i64 0
+; CHECK-NEXT:    [[TMP6:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP6]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP]])
+; CHECK-NEXT:    br label %[[MAKE:.*]]
+; CHECK:       [[MAKE]]:
+; CHECK-NEXT:    [[TMP7:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP8:%.*]] = mul i64 [[TMP7]], 1
+; CHECK-NEXT:    [[TMP9:%.*]] = add i64 [[TMP8]], 15
+; CHECK-NEXT:    [[TMP10:%.*]] = and i64 [[TMP9]], -16
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP10]], align 16
+; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP11:%.*]] = ptrtoint ptr [[VLA]] to i64
+; CHECK-NEXT:    store i64 [[TMP11]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP10]])
+; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
+; CHECK-NEXT:    [[AGAIN:%.*]] = call i1 @cond()
+; CHECK-NEXT:    br i1 [[AGAIN]], label %[[HEADER]], label %[[EXIT]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP12:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP13:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP14:%.*]] = and i1 [[TMP12]], [[TMP13]]
+; CHECK-NEXT:    [[TMP15:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP14]], i64 [[TMP15]], i64 0
+; CHECK-NEXT:    [[TMP16:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP16]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %header
+
+header:
+  %sp = call ptr @llvm.stacksave.p0()
+  %c = call i1 @cond()
+  br i1 %c, label %release, label %exit
+
+release:
+  call void @llvm.stackrestore.p0(ptr %sp)
+  br label %make
+
+make:
+  %vla = alloca i8, i32 %n, align 1
+  call void @use(ptr %vla)
+  %again = call i1 @cond()
+  br i1 %again, label %header, label %exit
+
+exit:
+  ret void
+}
+
+; An alloca in a loop that is never released is untagged, along with the
+; allocas of earlier iterations, when the function returns.
+define void @unreleased_in_loop(i32 %n) sanitize_memtag {
+; CHECK-LABEL: define void @unreleased_in_loop(
+; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*]]:
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[I:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[I_NEXT:%.*]], 
%[[LOOP]] ]
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
+; CHECK-NEXT:    [[BUF:%.*]] = alloca i8, i64 [[TMP3]], align 16
+; CHECK-NEXT:    [[BUF_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[BUF]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[BUF]] to i64
+; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[BUF_TAG]], i64 [[TMP3]])
+; CHECK-NEXT:    call void @use(ptr [[BUF_TAG]])
+; CHECK-NEXT:    [[I_NEXT]] = add i32 [[I]], 1
+; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[I_NEXT]], [[N]]
+; CHECK-NEXT:    br i1 [[C]], label %[[LOOP]], label %[[EXIT:.*]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP7:%.*]] = and i1 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    [[TMP8:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP7]], i64 [[TMP8]], i64 0
+; CHECK-NEXT:    [[TMP9:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP9]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %loop
+
+loop:
+  %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
+  %buf = alloca i8, i32 %n, align 1
+  call void @use(ptr %buf)
+  %i.next = add i32 %i, 1
+  %c = icmp ult i32 %i.next, %n
+  br i1 %c, label %loop, label %exit
+
+exit:
+  ret void
+}
+
+; With lifetime markers, the memory is tagged at llvm.lifetime.start instead of
+; at the alloca, and untagged at llvm.lifetime.end.
+define void @lifetime_markers(i32 %n) sanitize_memtag {
+; CHECK-LABEL: define void @lifetime_markers(
+; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
+; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
+; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 1
+; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
+; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP3]], align 16
+; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
+; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[VLA]] to i64
+; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    call void @llvm.lifetime.start.p0(ptr [[VLA]])
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP3]])
+; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA]], i64 [[TMP3]])
+; CHECK-NEXT:    call void @llvm.lifetime.end.p0(ptr [[VLA]])
+; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
+; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
+; CHECK-NEXT:    [[TMP5:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
+; CHECK-NEXT:    [[TMP7:%.*]] = and i1 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    [[TMP8:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
+; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP7]], i64 [[TMP8]], i64 0
+; CHECK-NEXT:    [[TMP9:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP9]], i64 
[[UNTAG_SIZE]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %vla = alloca i8, i32 %n, align 1
+  call void @llvm.lifetime.start.p0(ptr %vla)
+  call void @use(ptr %vla)
+  call void @llvm.lifetime.end.p0(ptr %vla)
+  ret void
+}
+
+; With a call to a returns_twice function, dynamic allocas are not
+; instrumented, but static ones still are.
+define void @returns_twice(i32 %n, ptr %buf) sanitize_memtag {
+; CHECK-LABEL: define void @returns_twice(
+; CHECK-SAME: i32 [[N:%.*]], ptr [[BUF:%.*]]) #[[ATTR4]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK-NEXT:    [[X:%.*]] = alloca { i32, [12 x i8] }, align 16
+; CHECK-NEXT:    [[X_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[X]], ptr 
[[BASETAG]], i64 0)
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[X_TAG]], i64 16)
+; CHECK-NEXT:    [[R:%.*]] = call i32 @setjmp(ptr [[BUF]])
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i32 [[N]], align 1
+; CHECK-NEXT:    call void @use(ptr [[X_TAG]])
+; CHECK-NEXT:    call void @use(ptr [[VLA]])
+; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[X]], i64 16)
+; CHECK-NEXT:    ret void
+;
+entry:
+  %x = alloca i32, align 4
+  %r = call i32 @setjmp(ptr %buf)
+  %vla = alloca i8, i32 %n, align 1
+  call void @use(ptr %x)
+  call void @use(ptr %vla)
+  ret void
+}
+
+; Functions without sanitize_memtag are left alone.
+define void @no_sanitizer(i32 %n) {
+; CHECK-LABEL: define void @no_sanitizer(
+; CHECK-SAME: i32 [[N:%.*]]) {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i32 [[N]], align 1
+; CHECK-NEXT:    call void @use(ptr [[VLA]])
+; CHECK-NEXT:    ret void
+;
+entry:
+  %vla = alloca i8, i32 %n, align 1
+  call void @use(ptr %vla)
+  ret void
+}
+
+; The allocation is tagged, and the memory up to the slot is untagged, with
+; loops whose size is only known at run time.
+; ASM-LABEL: vla:
+; ASM:         stur xzr, [x29, #-16]
+; ASM:         sub x10, x10, x8
+; ASM-NEXT:    mov sp, x10
+; ASM:         stur x10, [x29, #-16]
+; ASM:         addg x0,
+; ASM:         tbz w8, #4,
+; ASM:         stg x9, [x9], #16
+; ASM:         cbz x8,
+; ASM:         st2g x9, [x9], #32
+; ASM:         bl use
+; ASM-NEXT:    ldur x8, [x29, #-16]
+; ASM-NEXT:    sub x9, x29, #16
+; ASM-NEXT:    subs x9, x9, x8
+; ASM-NEXT:    ccmp x8, #0, #4, hs
+; ASM-NEXT:    csel x9, x9, xzr, ne
+; ASM-NEXT:    tbz w9, #4,
+; ASM:         stg x8, [x8], #16
+; ASM:         cbz x9,
+; ASM:         st2g x8, [x8], #32
+; ASM:         ret
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging.ll 
b/llvm/test/CodeGen/AArch64/stack-tagging.ll
index c2ff7530451eb5..19532c814bcf7e 100644
--- a/llvm/test/CodeGen/AArch64/stack-tagging.ll
+++ b/llvm/test/CodeGen/AArch64/stack-tagging.ll
@@ -136,11 +136,13 @@ l:
 }
 
 ; CHECK-LABEL: define void @DynamicAllocas(
-; CHECK-NOT: @llvm.aarch64.irg.sp
-; CHECK:     %x = alloca i32, i32 %cnt, align 4
-; CHECK-NOT: @llvm.aarch64.irg.sp
-; CHECK:     alloca i32, align 4
-; CHECK-NOT: @llvm.aarch64.irg.sp
+; CHECK:     %basetag = call ptr @llvm.aarch64.irg.sp(i64 0)
+; CHECK:     %x = alloca i8, i64 {{.*}}, align 16
+; CHECK:     %x.tag = call ptr @llvm.aarch64.tagp.p0(ptr %x, ptr %basetag, i64 
0)
+; CHECK:     %y = alloca i8, i64 {{.*}}, align 16
+; CHECK:     %y.tag = call ptr @llvm.aarch64.tagp.p0(ptr %y, ptr %basetag, i64 
1)
+; CHECK:     call void @use32(ptr %x.tag)
+; CHECK:     call void @use32(ptr %y.tag)
 ; CHECK:     ret void
 
 define void @DoubleEnd(i32 %b) sanitize_memtag {

>From 6dd4a382989764f0a281b67f329b22baf2c72894 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <[email protected]>
Date: Wed, 28 Oct 2026 11:14:00 -0700
Subject: [PATCH 2/4] [AArch64][MTE] Fix nondeterministic compare order causing
 windows failure

---
 llvm/lib/Target/AArch64/AArch64StackTagging.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp 
b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
index 9a647563755d42..b151f484f8cc19 100644
--- a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
+++ b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
@@ -529,8 +529,9 @@ void 
AArch64StackTagging::untagDynamicAllocasBeforeInst(Instruction *InstBefore,
   Value *LastAlloca =
       IRB.CreateLoad(IntptrTy, DynamicAllocaLayout, "last.alloca");
   Value *Zero = Constant::getNullValue(IntptrTy);
-  Value *Cond = IRB.CreateAnd(IRB.CreateICmpNE(LastAlloca, Zero),
-                              IRB.CreateICmpULE(LastAlloca, End));
+  Value *NonZero = IRB.CreateICmpNE(LastAlloca, Zero);
+  Value *InRange = IRB.CreateICmpULE(LastAlloca, End);
+  Value *Cond = IRB.CreateAnd(NonZero, InRange);
   Value *Size = IRB.CreateSelect(Cond, IRB.CreateSub(End, LastAlloca), Zero,
                                  "untag.size");
   // Tag 0.

>From 1f6ce16a34c5566bda99726bd85b2c7c744c1090 Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <[email protected]>
Date: Tue, 10 Nov 2026 12:36:44 -0800
Subject: [PATCH 3/4] Address review feedback

---
 .../Transforms/Utils/MemoryTaggingSupport.h   |  2 +-
 .../Target/AArch64/AArch64StackTagging.cpp    | 93 ++++++++++---------
 .../Instrumentation/HWAddressSanitizer.cpp    |  3 +-
 .../stack-tagging-dynamic-alloca-history.ll   |  2 +
 .../AArch64/stack-tagging-dynamic-alloca.ll   | 14 +++
 5 files changed, 66 insertions(+), 48 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h 
b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
index 02582dbb8489bb..8531c070e23f87 100644
--- a/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
+++ b/llvm/include/llvm/Transforms/Utils/MemoryTaggingSupport.h
@@ -78,7 +78,7 @@ enum class AllocaInterestingness {
 class StackInfoBuilder {
 public:
   StackInfoBuilder(const StackSafetyGlobalInfo *SSI, const char *DebugType,
-                   bool InstrumentDynamicAllocas = false)
+                   bool InstrumentDynamicAllocas)
       : SSI(SSI), DebugType(DebugType),
         InstrumentDynamicAllocas(InstrumentDynamicAllocas) {}
 
diff --git a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp 
b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
index b151f484f8cc19..f2df2539590218 100644
--- a/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
+++ b/llvm/lib/Target/AArch64/AArch64StackTagging.cpp
@@ -302,7 +302,8 @@ class AArch64StackTagging : public FunctionPass {
   void createDynamicAllocasInitStorage();
   void handleDynamicAllocaCall(memtag::AllocaInfo &Info, Value *Base,
                                unsigned Tag);
-  void untagDynamicAllocasBeforeInst(Instruction *InstBefore, Value *End);
+  void untagDynamicAllocasBeforeInst(Instruction *InstBefore, Value *End,
+                                     bool UpdateLayout);
   void untagDynamicAllocas(const memtag::StackInfo &SInfo);
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -453,6 +454,24 @@ void 
AArch64StackTagging::createDynamicAllocasInitStorage() {
   DynamicAllocaLayout->setAlignment(kTagGranuleSize);
 }
 
+// Insert an aarch64.tagp intrinsic at IRB that tags AI with Tag relative to
+// Base, and rewrite AI's non-lifetime uses to the tagged pointer. The caller
+// sets IRB's insert point. Returns the tagp call.
+static Instruction *insertTaggedPointer(IRBuilder<> &IRB, AllocaInst *AI,
+                                        Value *Base, unsigned Tag) {
+  Instruction *TagPCall = IRB.CreateIntrinsicWithoutFolding(
+      Intrinsic::aarch64_tagp, {AI->getType()},
+      {Constant::getNullValue(AI->getType()), Base, IRB.getInt64(Tag)});
+  if (AI->hasName())
+    TagPCall->setName(AI->getName() + ".tag");
+  // Does not replace metadata, so we don't have to handle DbgVariableRecords.
+  AI->replaceUsesWithIf(TagPCall, [&](const Use &U) {
+    return !isa<LifetimeIntrinsic>(U.getUser());
+  });
+  TagPCall->setOperand(0, AI);
+  return TagPCall;
+}
+
 // Replace the dynamic alloca with one whose size is a multiple of the tag
 // granule, and tag the memory it returns.
 void AArch64StackTagging::handleDynamicAllocaCall(memtag::AllocaInfo &Info,
@@ -478,54 +497,40 @@ void 
AArch64StackTagging::handleDynamicAllocaCall(memtag::AllocaInfo &Info,
 
   // Replace alloca with tagp(alloca).
   IRB.SetInsertPoint(NewAI->getNextNode());
-  Instruction *TagPCall = IRB.CreateIntrinsicWithoutFolding(
-      Intrinsic::aarch64_tagp, {NewAI->getType()},
-      {Constant::getNullValue(NewAI->getType()), Base, IRB.getInt64(Tag)});
-  if (NewAI->hasName())
-    TagPCall->setName(NewAI->getName() + ".tag");
-  // Does not replace metadata, so we don't have to handle DbgVariableRecords.
-  NewAI->replaceUsesWithIf(TagPCall, [&](const Use &U) {
-    return !isa<LifetimeIntrinsic>(U.getUser());
-  });
-  TagPCall->setOperand(0, NewAI);
+  Instruction *TagPCall = insertTaggedPointer(IRB, NewAI, Base, Tag);
 
   // Store the last alloca's address to DynamicAllocaLayout. This points to the
   // most recently executed dynamic alloca, which is used when untagging. It is
   // granule aligned.
   IRB.CreateStore(IRB.CreatePtrToInt(NewAI, IntptrTy), DynamicAllocaLayout);
 
-  // Tag the memory at llvm.lifetime.start if there is one, and untag it at
-  // llvm.lifetime.end. NewAI, and so TagPCall, is granule aligned, and TagSize
-  // is a multiple of the granule.
+  // Tag the memory at each llvm.lifetime.start, or at the alloca if there is
+  // none, and untag it at each llvm.lifetime.end. NewAI, and so TagPCall, is
+  // granule aligned, and TagSize is a multiple of the granule.
   Value *TagSize = IRB.CreateZExtOrTrunc(PaddedSize, IRB.getInt64Ty());
-  SmallVector<LifetimeIntrinsic *, 4> Lifetimes;
-  for (User *U : NewAI->users())
-    if (auto *II = dyn_cast<LifetimeIntrinsic>(U))
-      Lifetimes.push_back(II);
-  if (none_of(Lifetimes, [](LifetimeIntrinsic *II) {
-        return II->getIntrinsicID() == Intrinsic::lifetime_start;
-      }))
+  if (Info.LifetimeStart.empty())
     IRB.CreateCall(SetTagFunc, {TagPCall, TagSize});
-  for (LifetimeIntrinsic *II : Lifetimes) {
-    if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
-      IRB.SetInsertPoint(II->getNextNode());
-      IRB.CreateCall(SetTagFunc, {TagPCall, TagSize});
-    } else {
-      // NewAI has tag 0.
-      IRB.SetInsertPoint(II);
-      IRB.CreateCall(SetTagFunc, {NewAI, TagSize});
-    }
+  for (IntrinsicInst *Start : Info.LifetimeStart) {
+    IRB.SetInsertPoint(Start->getNextNode());
+    IRB.CreateCall(SetTagFunc, {TagPCall, TagSize});
+  }
+  for (IntrinsicInst *End : Info.LifetimeEnd) {
+    // NewAI has tag 0.
+    IRB.SetInsertPoint(End);
+    IRB.CreateCall(SetTagFunc, {NewAI, TagSize});
   }
 }
 
 // Untag the memory from the most recent dynamic alloca up to End, a granule
 // aligned address, unless the range is empty or no alloca has been made.
 void AArch64StackTagging::untagDynamicAllocasBeforeInst(Instruction 
*InstBefore,
-                                                        Value *End) {
+                                                        Value *End,
+                                                        bool UpdateLayout) {
   IRBuilder<> IRB(InstBefore);
   Type *IntptrTy = IRB.getIntPtrTy(*DL);
-  // Only 0 or the address of an alloca is stored to DynamicAllocaLayout, so
-  // this is granule aligned, and the size a multiple of the granule.
+  // Only 0, the address of an alloca, or a restored SP is stored to
+  // DynamicAllocaLayout, so this is granule aligned, and the size a multiple 
of
+  // the granule.
   Value *LastAlloca =
       IRB.CreateLoad(IntptrTy, DynamicAllocaLayout, "last.alloca");
   Value *Zero = Constant::getNullValue(IntptrTy);
@@ -537,6 +542,10 @@ void 
AArch64StackTagging::untagDynamicAllocasBeforeInst(Instruction *InstBefore,
   // Tag 0.
   IRB.CreateCall(SetTagFunc, {IRB.CreateIntToPtr(LastAlloca, IRB.getPtrTy()),
                               IRB.CreateZExtOrTrunc(Size, IRB.getInt64Ty())});
+  // A restore releases the memory up to End, so record End as the new top.
+  if (UpdateLayout)
+    IRB.CreateStore(IRB.CreateSelect(Cond, End, LastAlloca),
+                    DynamicAllocaLayout);
 }
 
 // Untag dynamic allocas.
@@ -547,7 +556,8 @@ void AArch64StackTagging::untagDynamicAllocas(const 
memtag::StackInfo &SInfo) {
   for (Instruction *Ret : SInfo.RetVec) {
     IRBuilder<> IRB(Ret);
     untagDynamicAllocasBeforeInst(
-        Ret, IRB.CreatePtrToInt(DynamicAllocaLayout, IntptrTy, "untag.end"));
+        Ret, IRB.CreatePtrToInt(DynamicAllocaLayout, IntptrTy, "untag.end"),
+        /*UpdateLayout=*/false);
   }
 
   // When we untag before @llvm.stackrestore, we need to adjust extracted SP to
@@ -562,7 +572,8 @@ void AArch64StackTagging::untagDynamicAllocas(const 
memtag::StackInfo &SInfo) {
     Value *DynamicAreaPtr = IRB.CreateAdd(
         IRB.CreatePtrToInt(StackRestoreInst->getOperand(0), IntptrTy),
         DynamicAreaOffset, "untag.end");
-    untagDynamicAllocasBeforeInst(StackRestoreInst, DynamicAreaPtr);
+    untagDynamicAllocasBeforeInst(StackRestoreInst, DynamicAreaPtr,
+                                  /*UpdateLayout=*/true);
   }
 }
 
@@ -719,17 +730,7 @@ bool AArch64StackTagging::runOnFunction(Function &Fn) {
     AllocaInst *AI = Info.AI;
     // Replace alloca with tagp(alloca).
     IRBuilder<> IRB(Info.AI->getNextNode());
-    Instruction *TagPCall = IRB.CreateIntrinsicWithoutFolding(
-        Intrinsic::aarch64_tagp, {Info.AI->getType()},
-        {Constant::getNullValue(Info.AI->getType()), Base,
-         ConstantInt::get(IRB.getInt64Ty(), Tag)});
-    if (Info.AI->hasName())
-      TagPCall->setName(Info.AI->getName() + ".tag");
-    // Does not replace metadata, so we don't have to handle 
DbgVariableRecords.
-    Info.AI->replaceUsesWithIf(TagPCall, [&](const Use &U) {
-      return !isa<LifetimeIntrinsic>(U.getUser());
-    });
-    TagPCall->setOperand(0, Info.AI);
+    Instruction *TagPCall = insertTaggedPointer(IRB, Info.AI, Base, Tag);
 
     // Calls to functions that may return twice (e.g. setjmp) confuse the
     // postdominator analysis, and will leave us to keep memory tagged after
diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp 
b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
index 0097f333c2ef63..44c6477c6e6576 100644
--- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
@@ -1489,7 +1489,8 @@ void HWAddressSanitizer::sanitizeFunction(Function &F,
   SmallVector<Instruction *, 8> LandingPadVec;
   const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
 
-  memtag::StackInfoBuilder SIB(SSI, DEBUG_TYPE);
+  memtag::StackInfoBuilder SIB(SSI, DEBUG_TYPE,
+                               /*InstrumentDynamicAllocas=*/false);
   for (auto &Inst : instructions(F)) {
     if (InstrumentStack) {
       SIB.visit(ORE, Inst);
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca-history.ll 
b/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca-history.ll
index aa810ffe7dfc2a..da1dfc5854a273 100644
--- a/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca-history.ll
+++ b/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca-history.ll
@@ -62,6 +62,8 @@ define void @loop(i32 %n) sanitize_memtag {
 ; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP26]], i64 [[TMP27]], i64 0
 ; CHECK-NEXT:    [[TMP28:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
 ; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP28]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    [[TMP34:%.*]] = select i1 [[TMP26]], i64 [[UNTAG_END1]], i64 
[[LAST_ALLOCA2]]
+; CHECK-NEXT:    store i64 [[TMP34]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
 ; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP]])
 ; CHECK-NEXT:    [[I_NEXT]] = add i32 [[I]], 1
 ; CHECK-NEXT:    [[C:%.*]] = icmp ult i32 [[I_NEXT]], [[N]]
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll 
b/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll
index 39b94346be9531..71ad6a775e3412 100644
--- a/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll
+++ b/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll
@@ -155,6 +155,8 @@ define void @scoped_vla(i32 %n) sanitize_memtag {
 ; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP9]], i64 [[TMP10]], i64 0
 ; CHECK-NEXT:    [[TMP11:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
 ; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP11]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    [[TMP17:%.*]] = select i1 [[TMP9]], i64 [[UNTAG_END1]], i64 
[[LAST_ALLOCA2]]
+; CHECK-NEXT:    store i64 [[TMP17]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
 ; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP]])
 ; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
 ; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
@@ -208,6 +210,8 @@ define void @scoped_vla_slot(i32 %n) sanitize_memtag {
 ; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP9]], i64 [[TMP10]], i64 0
 ; CHECK-NEXT:    [[TMP11:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
 ; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP11]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    [[TMP17:%.*]] = select i1 [[TMP9]], i64 [[UNTAG_END1]], i64 
[[LAST_ALLOCA2]]
+; CHECK-NEXT:    store i64 [[TMP17]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
 ; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[RESTORED]])
 ; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
 ; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
@@ -261,6 +265,8 @@ define void @unknown_restore(i32 %n, ptr %saved) 
sanitize_memtag {
 ; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP9]], i64 [[TMP10]], i64 0
 ; CHECK-NEXT:    [[TMP11:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
 ; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP11]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    [[TMP17:%.*]] = select i1 [[TMP9]], i64 [[UNTAG_END1]], i64 
[[LAST_ALLOCA2]]
+; CHECK-NEXT:    store i64 [[TMP17]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
 ; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SAVED]])
 ; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
 ; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
@@ -329,6 +335,8 @@ define void @nested_scopes(i32 %n) sanitize_memtag {
 ; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP14]], i64 [[TMP15]], i64 0
 ; CHECK-NEXT:    [[TMP16:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
 ; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP16]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    [[TMP29:%.*]] = select i1 [[TMP14]], i64 [[UNTAG_END1]], i64 
[[LAST_ALLOCA2]]
+; CHECK-NEXT:    store i64 [[TMP29]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
 ; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP_INNER]])
 ; CHECK-NEXT:    br label %[[LATCH]]
 ; CHECK:       [[LATCH]]:
@@ -343,6 +351,8 @@ define void @nested_scopes(i32 %n) sanitize_memtag {
 ; CHECK-NEXT:    [[UNTAG_SIZE6:%.*]] = select i1 [[TMP21]], i64 [[TMP22]], i64 0
 ; CHECK-NEXT:    [[TMP23:%.*]] = inttoptr i64 [[LAST_ALLOCA5]] to ptr
 ; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP23]], i64 
[[UNTAG_SIZE6]])
+; CHECK-NEXT:    [[TMP30:%.*]] = select i1 [[TMP21]], i64 [[UNTAG_END4]], i64 
[[LAST_ALLOCA5]]
+; CHECK-NEXT:    store i64 [[TMP30]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
 ; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP_OUTER]])
 ; CHECK-NEXT:    [[I_NEXT]] = add i32 [[I]], 1
 ; CHECK-NEXT:    [[C2:%.*]] = icmp ult i32 [[I_NEXT]], [[N]]
@@ -518,6 +528,8 @@ define void @early_exit_in_scope(i32 %n) sanitize_memtag {
 ; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP9]], i64 [[TMP10]], i64 0
 ; CHECK-NEXT:    [[TMP11:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
 ; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP11]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    [[TMP17:%.*]] = select i1 [[TMP9]], i64 [[UNTAG_END1]], i64 
[[LAST_ALLOCA2]]
+; CHECK-NEXT:    store i64 [[TMP17]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
 ; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP]])
 ; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
 ; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
@@ -571,6 +583,8 @@ define void @stacksave_reexecuted(i32 %n) sanitize_memtag {
 ; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP4]], i64 [[TMP5]], i64 0
 ; CHECK-NEXT:    [[TMP6:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
 ; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP6]], i64 
[[UNTAG_SIZE3]])
+; CHECK-NEXT:    [[TMP17:%.*]] = select i1 [[TMP4]], i64 [[UNTAG_END1]], i64 
[[LAST_ALLOCA2]]
+; CHECK-NEXT:    store i64 [[TMP17]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
 ; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP]])
 ; CHECK-NEXT:    br label %[[MAKE:.*]]
 ; CHECK:       [[MAKE]]:

>From 7dca1b85e415ecc87d3fd3904c0bfb6024e4716f Mon Sep 17 00:00:00 2001
From: Andrew Haberlandt <[email protected]>
Date: Tue, 10 Nov 2026 17:34:12 -0800
Subject: [PATCH 4/4] Remove redundant tests

---
 llvm/test/CodeGen/AArch64/settag.ll           |  13 --
 .../AArch64/stack-tagging-dynamic-alloca.ll   | 137 ------------------
 2 files changed, 150 deletions(-)

diff --git a/llvm/test/CodeGen/AArch64/settag.ll 
b/llvm/test/CodeGen/AArch64/settag.ll
index 9956ef640fa4ab..e0e78f6d7267f9 100644
--- a/llvm/test/CodeGen/AArch64/settag.ll
+++ b/llvm/test/CodeGen/AArch64/settag.ll
@@ -345,19 +345,6 @@ entry:
   call void @llvm.aarch64.settag(ptr %p, i64 0)
   ret void
 }
-
-define void @stzg0(ptr %p, ptr %q) {
-; CHECK-LABEL: stzg0:
-; CHECK:       // %bb.0: // %entry
-; CHECK-NEXT:    mov w8, #1 // =0x1
-; CHECK-NEXT:    str w8, [x1]
-; CHECK-NEXT:    ret
-entry:
-  store i32 1, ptr %q
-  call void @llvm.aarch64.settag.zero(ptr %p, i64 0)
-  ret void
-}
-
 declare void @b(ptr)
 declare void @c(ptr, i64)
 
diff --git a/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll 
b/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll
index 71ad6a775e3412..93ee9f8fab6eaa 100644
--- a/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll
+++ b/llvm/test/CodeGen/AArch64/stack-tagging-dynamic-alloca.ll
@@ -176,65 +176,6 @@ entry:
   call void @llvm.stackrestore.p0(ptr %sp)
   ret void
 }
-
-; Without optimization, the saved stack pointer is reloaded from a stack slot.
-define void @scoped_vla_slot(i32 %n) sanitize_memtag {
-; CHECK-LABEL: define void @scoped_vla_slot(
-; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4]] {
-; CHECK-NEXT:  [[ENTRY:.*:]]
-; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
-; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
-; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
-; CHECK-NEXT:    [[SAVED_STACK:%.*]] = alloca ptr, align 8
-; CHECK-NEXT:    [[SP:%.*]] = call ptr @llvm.stacksave.p0()
-; CHECK-NEXT:    store ptr [[SP]], ptr [[SAVED_STACK]], align 8
-; CHECK-NEXT:    [[TMP0:%.*]] = zext i32 [[N]] to i64
-; CHECK-NEXT:    [[TMP1:%.*]] = mul i64 [[TMP0]], 1
-; CHECK-NEXT:    [[TMP2:%.*]] = add i64 [[TMP1]], 15
-; CHECK-NEXT:    [[TMP3:%.*]] = and i64 [[TMP2]], -16
-; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP3]], align 16
-; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
-; CHECK-NEXT:    [[TMP4:%.*]] = ptrtoint ptr [[VLA]] to i64
-; CHECK-NEXT:    store i64 [[TMP4]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
-; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP3]])
-; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
-; CHECK-NEXT:    [[RESTORED:%.*]] = load ptr, ptr [[SAVED_STACK]], align 8
-; CHECK-NEXT:    [[TMP5:%.*]] = call i64 @llvm.get.dynamic.area.offset.i64()
-; CHECK-NEXT:    [[TMP6:%.*]] = ptrtoint ptr [[RESTORED]] to i64
-; CHECK-NEXT:    [[UNTAG_END1:%.*]] = add i64 [[TMP6]], [[TMP5]]
-; CHECK-NEXT:    [[LAST_ALLOCA2:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
-; CHECK-NEXT:    [[TMP7:%.*]] = icmp ne i64 [[LAST_ALLOCA2]], 0
-; CHECK-NEXT:    [[TMP8:%.*]] = icmp ule i64 [[LAST_ALLOCA2]], [[UNTAG_END1]]
-; CHECK-NEXT:    [[TMP9:%.*]] = and i1 [[TMP7]], [[TMP8]]
-; CHECK-NEXT:    [[TMP10:%.*]] = sub i64 [[UNTAG_END1]], [[LAST_ALLOCA2]]
-; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP9]], i64 [[TMP10]], i64 0
-; CHECK-NEXT:    [[TMP11:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
-; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP11]], i64 
[[UNTAG_SIZE3]])
-; CHECK-NEXT:    [[TMP17:%.*]] = select i1 [[TMP9]], i64 [[UNTAG_END1]], i64 
[[LAST_ALLOCA2]]
-; CHECK-NEXT:    store i64 [[TMP17]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
-; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[RESTORED]])
-; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
-; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
-; CHECK-NEXT:    [[TMP12:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
-; CHECK-NEXT:    [[TMP13:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
-; CHECK-NEXT:    [[TMP14:%.*]] = and i1 [[TMP12]], [[TMP13]]
-; CHECK-NEXT:    [[TMP15:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
-; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP14]], i64 [[TMP15]], i64 0
-; CHECK-NEXT:    [[TMP16:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
-; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP16]], i64 
[[UNTAG_SIZE]])
-; CHECK-NEXT:    ret void
-;
-entry:
-  %saved_stack = alloca ptr, align 8
-  %sp = call ptr @llvm.stacksave.p0()
-  store ptr %sp, ptr %saved_stack, align 8
-  %vla = alloca i8, i32 %n, align 1
-  call void @use(ptr %vla)
-  %restored = load ptr, ptr %saved_stack, align 8
-  call void @llvm.stackrestore.p0(ptr %restored)
-  ret void
-}
-
 ; The restored stack pointer does not have to visibly come from an
 ; llvm.stacksave.
 define void @unknown_restore(i32 %n, ptr %saved) sanitize_memtag {
@@ -556,84 +497,6 @@ cleanup:
   call void @llvm.stackrestore.p0(ptr %sp)
   ret void
 }
-
-; The stacksave may be executed again after the alloca is made, in which case
-; the restore does not release it.
-define void @stacksave_reexecuted(i32 %n) sanitize_memtag {
-; CHECK-LABEL: define void @stacksave_reexecuted(
-; CHECK-SAME: i32 [[N:%.*]]) #[[ATTR4]] {
-; CHECK-NEXT:  [[ENTRY:.*:]]
-; CHECK-NEXT:    [[DYNAMIC_ALLOCA_LAYOUT:%.*]] = alloca i64, align 16
-; CHECK-NEXT:    store i64 0, ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
-; CHECK-NEXT:    [[BASETAG:%.*]] = call ptr @llvm.aarch64.irg.sp(i64 0)
-; CHECK-NEXT:    br label %[[HEADER:.*]]
-; CHECK:       [[HEADER]]:
-; CHECK-NEXT:    [[SP:%.*]] = call ptr @llvm.stacksave.p0()
-; CHECK-NEXT:    [[C:%.*]] = call i1 @cond()
-; CHECK-NEXT:    br i1 [[C]], label %[[RELEASE:.*]], label %[[EXIT:.*]]
-; CHECK:       [[RELEASE]]:
-; CHECK-NEXT:    [[TMP0:%.*]] = call i64 @llvm.get.dynamic.area.offset.i64()
-; CHECK-NEXT:    [[TMP1:%.*]] = ptrtoint ptr [[SP]] to i64
-; CHECK-NEXT:    [[UNTAG_END1:%.*]] = add i64 [[TMP1]], [[TMP0]]
-; CHECK-NEXT:    [[LAST_ALLOCA2:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
-; CHECK-NEXT:    [[TMP2:%.*]] = icmp ne i64 [[LAST_ALLOCA2]], 0
-; CHECK-NEXT:    [[TMP3:%.*]] = icmp ule i64 [[LAST_ALLOCA2]], [[UNTAG_END1]]
-; CHECK-NEXT:    [[TMP4:%.*]] = and i1 [[TMP2]], [[TMP3]]
-; CHECK-NEXT:    [[TMP5:%.*]] = sub i64 [[UNTAG_END1]], [[LAST_ALLOCA2]]
-; CHECK-NEXT:    [[UNTAG_SIZE3:%.*]] = select i1 [[TMP4]], i64 [[TMP5]], i64 0
-; CHECK-NEXT:    [[TMP6:%.*]] = inttoptr i64 [[LAST_ALLOCA2]] to ptr
-; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP6]], i64 
[[UNTAG_SIZE3]])
-; CHECK-NEXT:    [[TMP17:%.*]] = select i1 [[TMP4]], i64 [[UNTAG_END1]], i64 
[[LAST_ALLOCA2]]
-; CHECK-NEXT:    store i64 [[TMP17]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
-; CHECK-NEXT:    call void @llvm.stackrestore.p0(ptr [[SP]])
-; CHECK-NEXT:    br label %[[MAKE:.*]]
-; CHECK:       [[MAKE]]:
-; CHECK-NEXT:    [[TMP7:%.*]] = zext i32 [[N]] to i64
-; CHECK-NEXT:    [[TMP8:%.*]] = mul i64 [[TMP7]], 1
-; CHECK-NEXT:    [[TMP9:%.*]] = add i64 [[TMP8]], 15
-; CHECK-NEXT:    [[TMP10:%.*]] = and i64 [[TMP9]], -16
-; CHECK-NEXT:    [[VLA:%.*]] = alloca i8, i64 [[TMP10]], align 16
-; CHECK-NEXT:    [[VLA_TAG:%.*]] = call ptr @llvm.aarch64.tagp.p0(ptr [[VLA]], 
ptr [[BASETAG]], i64 0)
-; CHECK-NEXT:    [[TMP11:%.*]] = ptrtoint ptr [[VLA]] to i64
-; CHECK-NEXT:    store i64 [[TMP11]], ptr [[DYNAMIC_ALLOCA_LAYOUT]], align 8
-; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[VLA_TAG]], i64 [[TMP10]])
-; CHECK-NEXT:    call void @use(ptr [[VLA_TAG]])
-; CHECK-NEXT:    [[AGAIN:%.*]] = call i1 @cond()
-; CHECK-NEXT:    br i1 [[AGAIN]], label %[[HEADER]], label %[[EXIT]]
-; CHECK:       [[EXIT]]:
-; CHECK-NEXT:    [[UNTAG_END:%.*]] = ptrtoint ptr [[DYNAMIC_ALLOCA_LAYOUT]] to 
i64
-; CHECK-NEXT:    [[LAST_ALLOCA:%.*]] = load i64, ptr 
[[DYNAMIC_ALLOCA_LAYOUT]], align 8
-; CHECK-NEXT:    [[TMP12:%.*]] = icmp ne i64 [[LAST_ALLOCA]], 0
-; CHECK-NEXT:    [[TMP13:%.*]] = icmp ule i64 [[LAST_ALLOCA]], [[UNTAG_END]]
-; CHECK-NEXT:    [[TMP14:%.*]] = and i1 [[TMP12]], [[TMP13]]
-; CHECK-NEXT:    [[TMP15:%.*]] = sub i64 [[UNTAG_END]], [[LAST_ALLOCA]]
-; CHECK-NEXT:    [[UNTAG_SIZE:%.*]] = select i1 [[TMP14]], i64 [[TMP15]], i64 0
-; CHECK-NEXT:    [[TMP16:%.*]] = inttoptr i64 [[LAST_ALLOCA]] to ptr
-; CHECK-NEXT:    call void @llvm.aarch64.settag(ptr [[TMP16]], i64 
[[UNTAG_SIZE]])
-; CHECK-NEXT:    ret void
-;
-entry:
-  br label %header
-
-header:
-  %sp = call ptr @llvm.stacksave.p0()
-  %c = call i1 @cond()
-  br i1 %c, label %release, label %exit
-
-release:
-  call void @llvm.stackrestore.p0(ptr %sp)
-  br label %make
-
-make:
-  %vla = alloca i8, i32 %n, align 1
-  call void @use(ptr %vla)
-  %again = call i1 @cond()
-  br i1 %again, label %header, label %exit
-
-exit:
-  ret void
-}
-
 ; An alloca in a loop that is never released is untagged, along with the
 ; allocas of earlier iterations, when the function returns.
 define void @unreleased_in_loop(i32 %n) sanitize_memtag {

_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to