[llvm] [CodeGen] Don't merge stack slots accessed with volatile after setjmp (returns twice) calls (PR #181370)
Mikołaj Piróg via llvm-commits
llvm-commits at lists.llvm.org
Sun Feb 22 04:17:28 PST 2026
https://github.com/mikolaj-pirog updated https://github.com/llvm/llvm-project/pull/181370
>From 3587b32e432a9a6b54a4877544b690c16b85bc8f Mon Sep 17 00:00:00 2001
From: "Pirog, Mikolaj Maciej" <mikolaj.maciej.pirog at intel.com>
Date: Thu, 12 Feb 2026 02:39:11 -0800
Subject: [PATCH 1/6] Inital
---
llvm/lib/CodeGen/StackColoring.cpp | 83 ++++++++++++++++++++++++++++++
1 file changed, 83 insertions(+)
diff --git a/llvm/lib/CodeGen/StackColoring.cpp b/llvm/lib/CodeGen/StackColoring.cpp
index 65f9ae5433195..b5b1303dc91de 100644
--- a/llvm/lib/CodeGen/StackColoring.cpp
+++ b/llvm/lib/CodeGen/StackColoring.cpp
@@ -42,8 +42,10 @@
#include "llvm/CodeGen/TargetOpcodes.h"
#include "llvm/CodeGen/WinEHFuncInfo.h"
#include "llvm/Config/llvm-config.h"
+#include "llvm/IR/Attributes.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
+#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Use.h"
@@ -433,6 +435,9 @@ class StackColoring {
/// slots lifetime-start-on-first-use is disabled).
BitVector ConservativeSlots;
+ /// FI slots for volatile variables (accessed with volatile operations).
+ BitVector VolatileSlots;
+
/// Number of iterations taken during data flow analysis.
unsigned NumIterations;
@@ -632,6 +637,8 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
InterestingSlots.resize(NumSlot);
ConservativeSlots.clear();
ConservativeSlots.resize(NumSlot);
+ VolatileSlots.clear();
+ VolatileSlots.resize(NumSlot);
// number of start and end lifetime ops for each slot
SmallVector<int, 8> NumStartLifetimes(NumSlot, 0);
@@ -708,6 +715,75 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
ConservativeSlots.set(slot);
}
+ // Detect volatile accesses to stack slots in successors of setjmp blocks.
+ // According to ISO C 7.13.2.1, non-volatile variables have indeterminate
+ // values after longjmp, but volatile variables retain their values.
+ // Therefore, volatile variables used in blocks reachable after setjmp need
+ // separate stack slots and cannot be merged.
+ if (MF->exposesReturnsTwice()) {
+ // First, find all blocks containing setjmp (returns_twice) calls by checking
+ // for calls to functions with the returns_twice attribute
+ SmallPtrSet<const MachineBasicBlock *, 4> SetjmpBlocks;
+ for (const MachineBasicBlock &MBB : *MF) {
+ for (const MachineInstr &MI : MBB) {
+ if (MI.isCall()) {
+ // Check if this call has the returns_twice attribute
+ bool IsReturnsTwice = false;
+ for (const MachineOperand &MO : MI.operands()) {
+ if (MO.isGlobal()) {
+ if (const Function *Callee = dyn_cast<Function>(MO.getGlobal())) {
+ if (Callee->hasFnAttribute(Attribute::ReturnsTwice)) {
+ IsReturnsTwice = true;
+ break;
+ }
+ }
+ }
+ }
+ if (IsReturnsTwice) {
+ SetjmpBlocks.insert(&MBB);
+ }
+ }
+ }
+ }
+
+ // Now, for each setjmp block, mark volatile slots used in successor blocks
+ // as conservative (non-mergeable)
+ SmallPtrSet<const MachineBasicBlock *, 16> Visited;
+ SmallPtrSet<const MachineBasicBlock *, 16> Worklist;
+ Worklist.insert(SetjmpBlocks.begin(), SetjmpBlocks.end());
+
+
+ // Process all blocks reachable from setjmp successors
+ while (!Worklist.empty()) {
+ auto *MBB = *Worklist.begin();
+ Worklist.erase(MBB);
+ if (Visited.contains(MBB))
+ continue;
+ Visited.insert(MBB);
+ Worklist.insert(MBB->pred_begin(), MBB->pred_end());
+
+ // Check for volatile accesses in this block
+ for (const MachineInstr &MI : *MBB) {
+ if (MI.isDebugInstr())
+ continue;
+
+ MI.dump();
+ // Check all memory operands for volatile accesses
+ for (auto MMI : MI.operands()) {
+ auto *MMO = dyn_cast<MachineMemOperand*>(MMI);
+ if (MMO && MMO->isVolatile()) {
+ int Slot = MMI.getIndex();
+ if (Slot < 0)
+ continue;
+ }
+ }
+ }
+
+ }
+
+ LLVM_DEBUG(dumpBV("Volatile slots (after setjmp)", VolatileSlots));
+ }
+
// The write to the catch object by the personality function is not propely
// modeled in IR: It happens before any cleanuppads are executed, even if the
// first mention of the catch object is in a catchpad. As such, mark catch
@@ -1321,6 +1397,13 @@ bool StackColoring::run(MachineFunction &Func, bool OnlyRemoveMarkers) {
if (MFI->getStackID(FirstSlot) != MFI->getStackID(SecondSlot))
continue;
+ // If the function contains setjmp, volatile slots cannot be merged
+ // with any other slots, including other volatile slots. This preserves
+ // the identity and storage of volatile variables across setjmp/longjmp.
+ if (MF->exposesReturnsTwice() &&
+ (VolatileSlots.test(FirstSlot) || VolatileSlots.test(SecondSlot)))
+ continue;
+
LiveInterval *First = &*Intervals[FirstSlot];
LiveInterval *Second = &*Intervals[SecondSlot];
auto &FirstS = LiveStarts[FirstSlot];
>From 41a558c44879c0e4236a56401f02509a22b8c5ce Mon Sep 17 00:00:00 2001
From: "Pirog, Mikolaj Maciej" <mikolaj.maciej.pirog at intel.com>
Date: Thu, 12 Feb 2026 15:58:22 +0100
Subject: [PATCH 2/6] Somewhat working
---
llvm/lib/CodeGen/StackColoring.cpp | 83 +++++++++++-------------------
1 file changed, 30 insertions(+), 53 deletions(-)
diff --git a/llvm/lib/CodeGen/StackColoring.cpp b/llvm/lib/CodeGen/StackColoring.cpp
index b5b1303dc91de..0806f89e52f90 100644
--- a/llvm/lib/CodeGen/StackColoring.cpp
+++ b/llvm/lib/CodeGen/StackColoring.cpp
@@ -441,6 +441,8 @@ class StackColoring {
/// Number of iterations taken during data flow analysis.
unsigned NumIterations;
+ SmallMapVector<StringRef, int, 16> NameToSlot;
+
public:
StackColoring(SlotIndexes *Indexes) : Indexes(Indexes) {}
bool run(MachineFunction &Func, bool OnlyRemoveMarkers = false);
@@ -475,7 +477,7 @@ class StackColoring {
bool applyFirstUse(int Slot) {
if (!LifetimeStartOnFirstUse || ProtectFromEscapedAllocas)
return false;
- if (ConservativeSlots.test(Slot))
+ if (ConservativeSlots.test(Slot) || VolatileSlots.test(Slot))
return false;
return true;
}
@@ -643,6 +645,7 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
// number of start and end lifetime ops for each slot
SmallVector<int, 8> NumStartLifetimes(NumSlot, 0);
SmallVector<int, 8> NumEndLifetimes(NumSlot, 0);
+ SmallPtrSet<const MachineBasicBlock *, 4> SetjmpBlocks;
// Step 1: collect markers and populate the "InterestingSlots"
// and "ConservativeSlots" sets.
@@ -678,6 +681,7 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
}
const AllocaInst *Allocation = MFI->getObjectAllocation(Slot);
if (Allocation) {
+ NameToSlot.insert({Allocation->getName(), Slot});
LLVM_DEBUG(dbgs() << "Found a lifetime ");
LLVM_DEBUG(dbgs() << (MI.getOpcode() == TargetOpcode::LIFETIME_START
? "start"
@@ -690,6 +694,11 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
MarkersFound += 1;
} else {
for (const MachineOperand &MO : MI.operands()) {
+ if (MO.isGlobal()) {
+ auto *F = dyn_cast<Function>(MO.getGlobal());
+ if (F && F->hasFnAttribute(Attribute::ReturnsTwice))
+ SetjmpBlocks.insert(MBB);
+ }
if (!MO.isFI())
continue;
int Slot = MO.getIndex();
@@ -723,65 +732,33 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
if (MF->exposesReturnsTwice()) {
// First, find all blocks containing setjmp (returns_twice) calls by checking
// for calls to functions with the returns_twice attribute
- SmallPtrSet<const MachineBasicBlock *, 4> SetjmpBlocks;
- for (const MachineBasicBlock &MBB : *MF) {
- for (const MachineInstr &MI : MBB) {
- if (MI.isCall()) {
- // Check if this call has the returns_twice attribute
- bool IsReturnsTwice = false;
- for (const MachineOperand &MO : MI.operands()) {
- if (MO.isGlobal()) {
- if (const Function *Callee = dyn_cast<Function>(MO.getGlobal())) {
- if (Callee->hasFnAttribute(Attribute::ReturnsTwice)) {
- IsReturnsTwice = true;
- break;
- }
- }
- }
- }
- if (IsReturnsTwice) {
- SetjmpBlocks.insert(&MBB);
- }
- }
- }
- }
// Now, for each setjmp block, mark volatile slots used in successor blocks
// as conservative (non-mergeable)
- SmallPtrSet<const MachineBasicBlock *, 16> Visited;
- SmallPtrSet<const MachineBasicBlock *, 16> Worklist;
- Worklist.insert(SetjmpBlocks.begin(), SetjmpBlocks.end());
-
+ df_iterator_default_set<const MachineBasicBlock *> Visited;
// Process all blocks reachable from setjmp successors
- while (!Worklist.empty()) {
- auto *MBB = *Worklist.begin();
- Worklist.erase(MBB);
- if (Visited.contains(MBB))
- continue;
- Visited.insert(MBB);
- Worklist.insert(MBB->pred_begin(), MBB->pred_end());
-
- // Check for volatile accesses in this block
- for (const MachineInstr &MI : *MBB) {
- if (MI.isDebugInstr())
- continue;
-
- MI.dump();
- // Check all memory operands for volatile accesses
- for (auto MMI : MI.operands()) {
- auto *MMO = dyn_cast<MachineMemOperand*>(MMI);
- if (MMO && MMO->isVolatile()) {
- int Slot = MMI.getIndex();
- if (Slot < 0)
- continue;
- }
- }
- }
-
+ for (auto *SetJmpBlok : SetjmpBlocks) {
+
+ for (auto *MBB : depth_first_ext(SetJmpBlok, Visited)) {
+ Visited.insert(MBB);
+
+ for (const MachineInstr &MI : *MBB) {
+ if (MI.isDebugInstr())
+ continue;
+
+ for (auto *MO : MI.memoperands()) {
+ if (!MO->isVolatile())
+ continue;
+ VolatileSlots.set(NameToSlot.at(MO->getValue()->getName()));
+
+
+ }
+ }
+ }
}
- LLVM_DEBUG(dumpBV("Volatile slots (after setjmp)", VolatileSlots));
+ LLVM_DEBUG(dumpBV("Volatile slots (after setjmp)", VolatileSlots));
}
// The write to the catch object by the personality function is not propely
>From 920a1f72caf7f558cc8e047a71337b91917fd701 Mon Sep 17 00:00:00 2001
From: "Pirog, Mikolaj Maciej" <mikolaj.maciej.pirog at intel.com>
Date: Fri, 13 Feb 2026 15:06:42 +0100
Subject: [PATCH 3/6] Further work
---
llvm/lib/CodeGen/StackColoring.cpp | 112 +++++++++++++++++------------
1 file changed, 67 insertions(+), 45 deletions(-)
diff --git a/llvm/lib/CodeGen/StackColoring.cpp b/llvm/lib/CodeGen/StackColoring.cpp
index 0806f89e52f90..6e6cea02c4471 100644
--- a/llvm/lib/CodeGen/StackColoring.cpp
+++ b/llvm/lib/CodeGen/StackColoring.cpp
@@ -374,6 +374,40 @@ STATISTIC(EscapedAllocas, "Number of allocas that escaped the lifetime region");
// If in RPO ordering chosen to walk the CFG we happen to visit the b[k]
// before visiting the memcpy block (which will contain the lifetime start
// for "b" then it will appear that 'b' has a degenerate lifetime.
+//
+// Volatile after setjmp:
+// ----------------------
+//
+// Stack slots that are being accessed with volatile specifier cannot be merged
+// if they have a setjmp instruction as a predecessor. Volatile variables
+// (stack slots that are being accessed with volatile memoperand) must not be
+// clobbered by longjmp. Example:
+
+// volatile int tmp = 0;
+// volatile int volatile_counter;
+//
+// if (setjmp(jump_buffer) == 0) {
+// volatile_counter = 100;
+// longjmp(jump_buffer, 5);
+// } else {
+// if (tmp)
+// return volatile_counter;
+// tmp = 1;
+// int tmp2[100];
+// foo(tmp2);
+// longjmp(jump_buffer, 1);
+// }
+//
+// return volatile_counter;
+//
+// If tmp2 slot is merged with volatile_counter slot, then the stack slot will
+// be potentially overwritten in the call to foo function. After longjmp is
+// performed the value of volatilve_counter should still be 100, but because of
+// merging it may be overwritten.
+//
+// To handle this we collect all MBBs that have a call with return twice
+// attribute and we iterate over all successors of such blocks to record stack
+// slots that are being accessed with volatile qualifier.
namespace {
@@ -435,12 +469,14 @@ class StackColoring {
/// slots lifetime-start-on-first-use is disabled).
BitVector ConservativeSlots;
- /// FI slots for volatile variables (accessed with volatile operations).
- BitVector VolatileSlots;
+ /// FI slots that are 1) volatile 2) have setjmp (return twice) as a
+ /// predecessor
+ BitVector VolatileAfterSetjmp;
/// Number of iterations taken during data flow analysis.
unsigned NumIterations;
+ // Map between variable and its occupied slot.
SmallMapVector<StringRef, int, 16> NameToSlot;
public:
@@ -477,7 +513,7 @@ class StackColoring {
bool applyFirstUse(int Slot) {
if (!LifetimeStartOnFirstUse || ProtectFromEscapedAllocas)
return false;
- if (ConservativeSlots.test(Slot) || VolatileSlots.test(Slot))
+ if (ConservativeSlots.test(Slot) || VolatileAfterSetjmp.test(Slot))
return false;
return true;
}
@@ -639,13 +675,13 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
InterestingSlots.resize(NumSlot);
ConservativeSlots.clear();
ConservativeSlots.resize(NumSlot);
- VolatileSlots.clear();
- VolatileSlots.resize(NumSlot);
+ VolatileAfterSetjmp.clear();
+ VolatileAfterSetjmp.resize(NumSlot);
// number of start and end lifetime ops for each slot
SmallVector<int, 8> NumStartLifetimes(NumSlot, 0);
SmallVector<int, 8> NumEndLifetimes(NumSlot, 0);
- SmallPtrSet<const MachineBasicBlock *, 4> SetjmpBlocks;
+ SmallPtrSet<const MachineBasicBlock *, 4> SetjmpBlocks;
// Step 1: collect markers and populate the "InterestingSlots"
// and "ConservativeSlots" sets.
@@ -681,7 +717,7 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
}
const AllocaInst *Allocation = MFI->getObjectAllocation(Slot);
if (Allocation) {
- NameToSlot.insert({Allocation->getName(), Slot});
+ NameToSlot.insert({Allocation->getName(), Slot});
LLVM_DEBUG(dbgs() << "Found a lifetime ");
LLVM_DEBUG(dbgs() << (MI.getOpcode() == TargetOpcode::LIFETIME_START
? "start"
@@ -694,11 +730,11 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
MarkersFound += 1;
} else {
for (const MachineOperand &MO : MI.operands()) {
- if (MO.isGlobal()) {
- auto *F = dyn_cast<Function>(MO.getGlobal());
- if (F && F->hasFnAttribute(Attribute::ReturnsTwice))
- SetjmpBlocks.insert(MBB);
- }
+ if (MO.isGlobal()) {
+ auto *F = dyn_cast<Function>(MO.getGlobal());
+ if (F && F->hasFnAttribute(Attribute::ReturnsTwice))
+ SetjmpBlocks.insert(MBB);
+ }
if (!MO.isFI())
continue;
int Slot = MO.getIndex();
@@ -725,40 +761,26 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
}
// Detect volatile accesses to stack slots in successors of setjmp blocks.
- // According to ISO C 7.13.2.1, non-volatile variables have indeterminate
- // values after longjmp, but volatile variables retain their values.
- // Therefore, volatile variables used in blocks reachable after setjmp need
- // separate stack slots and cannot be merged.
+ // Such slots cannot be merged because they must survive potential longjmp.
if (MF->exposesReturnsTwice()) {
- // First, find all blocks containing setjmp (returns_twice) calls by checking
- // for calls to functions with the returns_twice attribute
-
- // Now, for each setjmp block, mark volatile slots used in successor blocks
- // as conservative (non-mergeable)
- df_iterator_default_set<const MachineBasicBlock *> Visited;
-
- // Process all blocks reachable from setjmp successors
- for (auto *SetJmpBlok : SetjmpBlocks) {
-
- for (auto *MBB : depth_first_ext(SetJmpBlok, Visited)) {
- Visited.insert(MBB);
-
- for (const MachineInstr &MI : *MBB) {
- if (MI.isDebugInstr())
- continue;
-
- for (auto *MO : MI.memoperands()) {
- if (!MO->isVolatile())
- continue;
- VolatileSlots.set(NameToSlot.at(MO->getValue()->getName()));
-
-
- }
- }
- }
+ df_iterator_default_set<const MachineBasicBlock *> Visited;
+
+ for (auto *SetJmpBlok : SetjmpBlocks) {
+ for (auto *MBB : depth_first_ext(SetJmpBlok, Visited)) {
+ for (const MachineInstr &MI : *MBB) {
+ if (MI.isDebugInstr())
+ continue;
+
+ for (auto *MO : MI.memoperands()) {
+ if (!MO->isVolatile())
+ continue;
+ VolatileAfterSetjmp.set(NameToSlot.at(MO->getValue()->getName()));
+ }
+ }
}
+ }
- LLVM_DEBUG(dumpBV("Volatile slots (after setjmp)", VolatileSlots));
+ LLVM_DEBUG(dumpBV("Volatile slots after setjmp", VolatileAfterSetjmp));
}
// The write to the catch object by the personality function is not propely
@@ -1377,8 +1399,8 @@ bool StackColoring::run(MachineFunction &Func, bool OnlyRemoveMarkers) {
// If the function contains setjmp, volatile slots cannot be merged
// with any other slots, including other volatile slots. This preserves
// the identity and storage of volatile variables across setjmp/longjmp.
- if (MF->exposesReturnsTwice() &&
- (VolatileSlots.test(FirstSlot) || VolatileSlots.test(SecondSlot)))
+ if (MF->exposesReturnsTwice() && (VolatileAfterSetjmp.test(FirstSlot) ||
+ VolatileAfterSetjmp.test(SecondSlot)))
continue;
LiveInterval *First = &*Intervals[FirstSlot];
>From 255c9c6fd8ecdc1526ed704df918858091fd60f0 Mon Sep 17 00:00:00 2001
From: "Pirog, Mikolaj Maciej" <mikolaj.maciej.pirog at intel.com>
Date: Fri, 13 Feb 2026 15:35:48 +0100
Subject: [PATCH 4/6] remove unecessary check
---
llvm/lib/CodeGen/StackColoring.cpp | 7 -------
1 file changed, 7 deletions(-)
diff --git a/llvm/lib/CodeGen/StackColoring.cpp b/llvm/lib/CodeGen/StackColoring.cpp
index 6e6cea02c4471..933eda42008fb 100644
--- a/llvm/lib/CodeGen/StackColoring.cpp
+++ b/llvm/lib/CodeGen/StackColoring.cpp
@@ -1396,13 +1396,6 @@ bool StackColoring::run(MachineFunction &Func, bool OnlyRemoveMarkers) {
if (MFI->getStackID(FirstSlot) != MFI->getStackID(SecondSlot))
continue;
- // If the function contains setjmp, volatile slots cannot be merged
- // with any other slots, including other volatile slots. This preserves
- // the identity and storage of volatile variables across setjmp/longjmp.
- if (MF->exposesReturnsTwice() && (VolatileAfterSetjmp.test(FirstSlot) ||
- VolatileAfterSetjmp.test(SecondSlot)))
- continue;
-
LiveInterval *First = &*Intervals[FirstSlot];
LiveInterval *Second = &*Intervals[SecondSlot];
auto &FirstS = LiveStarts[FirstSlot];
>From c53710609bb18d0b21fe390a2d87687253d3181f Mon Sep 17 00:00:00 2001
From: "Pirog, Mikolaj Maciej" <mikolaj.maciej.pirog at intel.com>
Date: Fri, 13 Feb 2026 15:51:41 +0100
Subject: [PATCH 5/6] Add test
---
.../X86/stack-coloring-setjmp-volatile.ll | 40 +++++++++++++++++++
1 file changed, 40 insertions(+)
create mode 100644 llvm/test/CodeGen/X86/stack-coloring-setjmp-volatile.ll
diff --git a/llvm/test/CodeGen/X86/stack-coloring-setjmp-volatile.ll b/llvm/test/CodeGen/X86/stack-coloring-setjmp-volatile.ll
new file mode 100644
index 0000000000000..af61c663fa58e
--- /dev/null
+++ b/llvm/test/CodeGen/X86/stack-coloring-setjmp-volatile.ll
@@ -0,0 +1,40 @@
+; RUN: llc -mtriple=x86_64-linux -no-stack-coloring=false -debug-only=stack-coloring < %s -o /dev/null 2>&1 | FileCheck %s
+; REQUIRES: asserts
+
+; Test that volatile stack slots accessed after setjmp are not merged.
+; Volatile variables must retain their values across longjmp, so their
+; stack slots cannot be reused even if their lifetimes don't overlap.
+
+declare i32 @setjmp(ptr) returns_twice
+
+; CHECK-LABEL: volatile_after_setjmp
+; CHECK: Volatile slots after setjmp : { {{.*}}1{{.*}} }
+; CHECK: Merge 0 slots
+
+define void @volatile_after_setjmp(ptr %jump_buffer) {
+entry:
+ %foo = alloca i32, align 4
+ %bar = alloca [100 x i32], align 4
+
+ call void @llvm.lifetime.start.p0(i64 4, ptr %foo)
+
+ %setjmp_result = call i32 @setjmp(ptr %jump_buffer)
+ %cmp = icmp eq i32 %setjmp_result, 0
+ br i1 %cmp, label %after_setjmp, label %continue
+
+after_setjmp:
+ store volatile i32 100, ptr %foo, align 4
+ br label %exit
+
+continue:
+ call void @llvm.lifetime.end.p0(i64 4, ptr %foo)
+ call void @llvm.lifetime.start.p0(i64 400, ptr %bar)
+ call void @llvm.lifetime.end.p0(i64 400, ptr %bar)
+ br label %exit
+
+exit:
+ ret void
+}
+
+declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture)
+declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture)
>From f476070eb151d2f541e26d322ee8b7594073c54e Mon Sep 17 00:00:00 2001
From: "Pirog, Mikolaj Maciej" <mikolaj.maciej.pirog at intel.com>
Date: Sun, 22 Feb 2026 12:58:55 +0100
Subject: [PATCH 6/6] Reviewer suggestions
---
llvm/lib/CodeGen/StackColoring.cpp | 23 +++++++++++++------
.../X86/stack-coloring-setjmp-volatile.ll | 6 ++++-
2 files changed, 21 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/CodeGen/StackColoring.cpp b/llvm/lib/CodeGen/StackColoring.cpp
index 933eda42008fb..467fa9c45f499 100644
--- a/llvm/lib/CodeGen/StackColoring.cpp
+++ b/llvm/lib/CodeGen/StackColoring.cpp
@@ -476,9 +476,6 @@ class StackColoring {
/// Number of iterations taken during data flow analysis.
unsigned NumIterations;
- // Map between variable and its occupied slot.
- SmallMapVector<StringRef, int, 16> NameToSlot;
-
public:
StackColoring(SlotIndexes *Indexes) : Indexes(Indexes) {}
bool run(MachineFunction &Func, bool OnlyRemoveMarkers = false);
@@ -717,7 +714,6 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
}
const AllocaInst *Allocation = MFI->getObjectAllocation(Slot);
if (Allocation) {
- NameToSlot.insert({Allocation->getName(), Slot});
LLVM_DEBUG(dbgs() << "Found a lifetime ");
LLVM_DEBUG(dbgs() << (MI.getOpcode() == TargetOpcode::LIFETIME_START
? "start"
@@ -771,10 +767,23 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
if (MI.isDebugInstr())
continue;
- for (auto *MO : MI.memoperands()) {
- if (!MO->isVolatile())
+ bool MarkVolatile = false;
+ for (auto *MMO : MI.memoperands()) {
+ if (!MMO->isVolatile())
+ continue;
+ MarkVolatile = true;
+ }
+
+ if (!MarkVolatile)
+ continue;
+
+ for (auto &MO : MI.operands()) {
+ if (!MO.isFI())
+ continue;
+ int Slot = MO.getIndex();
+ if (Slot < 0)
continue;
- VolatileAfterSetjmp.set(NameToSlot.at(MO->getValue()->getName()));
+ VolatileAfterSetjmp.set(Slot);
}
}
}
diff --git a/llvm/test/CodeGen/X86/stack-coloring-setjmp-volatile.ll b/llvm/test/CodeGen/X86/stack-coloring-setjmp-volatile.ll
index af61c663fa58e..7ef1409bece05 100644
--- a/llvm/test/CodeGen/X86/stack-coloring-setjmp-volatile.ll
+++ b/llvm/test/CodeGen/X86/stack-coloring-setjmp-volatile.ll
@@ -7,6 +7,8 @@
declare i32 @setjmp(ptr) returns_twice
+declare void @baz(ptr)
+
; CHECK-LABEL: volatile_after_setjmp
; CHECK: Volatile slots after setjmp : { {{.*}}1{{.*}} }
; CHECK: Merge 0 slots
@@ -17,6 +19,7 @@ entry:
%bar = alloca [100 x i32], align 4
call void @llvm.lifetime.start.p0(i64 4, ptr %foo)
+ call void @llvm.lifetime.start.p0(i64 400, ptr %bar)
%setjmp_result = call i32 @setjmp(ptr %jump_buffer)
%cmp = icmp eq i32 %setjmp_result, 0
@@ -27,8 +30,9 @@ after_setjmp:
br label %exit
continue:
+ store i32 100, ptr %bar, align 4
+ call void @baz(ptr %bar)
call void @llvm.lifetime.end.p0(i64 4, ptr %foo)
- call void @llvm.lifetime.start.p0(i64 400, ptr %bar)
call void @llvm.lifetime.end.p0(i64 400, ptr %bar)
br label %exit
More information about the llvm-commits
mailing list