Skip to content

Commit cf275f9

Browse files
committed
[NFC][CodeGen] Cleanup lifetime in StackColoring instead of DeadMachineInstructionElim
1 parent 25e4f3f commit cf275f9

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

llvm/lib/CodeGen/MachineInstr.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,10 +1417,6 @@ bool MachineInstr::isDead(const MachineRegisterInfo &MRI,
14171417
if (isInlineAsm())
14181418
return false;
14191419

1420-
// FIXME: See issue #105950 for why LIFETIME markers are considered dead here.
1421-
if (isLifetimeMarker())
1422-
return true;
1423-
14241420
// If there are no defs with uses, then we call the instruction dead so long
14251421
// as we do not suspect it may have sideeffects.
14261422
return wouldBeTriviallyDead();

llvm/lib/CodeGen/StackColoring.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,10 @@ unsigned StackColoring::collectMarkers(unsigned NumSlot) {
656656
if (MI.getOpcode() == TargetOpcode::LIFETIME_START ||
657657
MI.getOpcode() == TargetOpcode::LIFETIME_END) {
658658
int Slot = getStartOrEndSlot(MI);
659-
if (Slot < 0)
659+
if (Slot < 0) {
660+
Markers.push_back(&MI);
660661
continue;
662+
}
661663
InterestingSlots.set(Slot);
662664
if (MI.getOpcode() == TargetOpcode::LIFETIME_START) {
663665
BetweenStartEnd.set(Slot);
@@ -896,6 +898,18 @@ bool StackColoring::removeAllMarkers() {
896898
}
897899
Markers.clear();
898900

901+
for (MachineBasicBlock &MBB : *MF) {
902+
if (BlockLiveness.contains(&MBB))
903+
continue;
904+
for (MachineInstr &MI : make_early_inc_range(MBB)) {
905+
if (MI.getOpcode() == TargetOpcode::LIFETIME_START ||
906+
MI.getOpcode() == TargetOpcode::LIFETIME_END) {
907+
Count++;
908+
MI.eraseFromParent();
909+
}
910+
}
911+
}
912+
899913
LLVM_DEBUG(dbgs() << "Removed " << Count << " markers.\n");
900914
return Count;
901915
}
@@ -1216,8 +1230,8 @@ bool StackColoring::run(MachineFunction &Func) {
12161230
unsigned NumSlots = MFI->getObjectIndexEnd();
12171231

12181232
// If there are no stack slots then there are no markers to remove.
1219-
if (!NumSlots)
1220-
return false;
1233+
if (!NumSlots || DisableColoring)
1234+
return removeAllMarkers();
12211235

12221236
SmallVector<int, 8> SortedSlots;
12231237
SortedSlots.reserve(NumSlots);
@@ -1241,7 +1255,7 @@ bool StackColoring::run(MachineFunction &Func) {
12411255

12421256
// Don't continue because there are not enough lifetime markers, or the
12431257
// stack is too small, or we are told not to optimize the slots.
1244-
if (NumMarkers < 2 || TotalSize < 16 || DisableColoring) {
1258+
if (NumMarkers < 2 || TotalSize < 16) {
12451259
LLVM_DEBUG(dbgs() << "Will not try to merge slots.\n");
12461260
return removeAllMarkers();
12471261
}

llvm/test/CodeGen/X86/StackColoring.ll

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,41 @@ onerr:
581581

582582
%Data = type { [32 x i64] }
583583

584+
declare void @throw()
585+
586+
declare i32 @__CxxFrameHandler3(...)
587+
588+
declare void @llvm.trap()
589+
590+
;CHECK-LABEL: removed_all_lifetime:
591+
;YESCOLOR-NOT: LIFETIME_END
592+
;NOFIRSTUSE-NOT: LIFETIME_END
593+
;NOCOLOR-NOT: LIFETIME_END
594+
define void @removed_all_lifetime() personality ptr @__CxxFrameHandler3 {
595+
entry:
596+
%alloca2 = alloca ptr, align 4
597+
%alloca1 = alloca ptr, align 4
598+
store volatile ptr null, ptr %alloca1
599+
invoke void @throw()
600+
to label %unreachable unwind label %catch.dispatch
601+
602+
catch.dispatch: ; preds = %entry
603+
%cs = catchswitch within none [label %catch.pad] unwind to caller
604+
605+
catch.pad: ; preds = %catch.dispatch
606+
%cp = catchpad within %cs [ptr null, i32 0, ptr %alloca1]
607+
%v = load volatile ptr, ptr %alloca1
608+
store volatile ptr null, ptr %alloca1
609+
call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %alloca1)
610+
call void @llvm.lifetime.start.p0(i64 4, ptr %alloca2)
611+
store volatile ptr null, ptr %alloca1
612+
call void @llvm.trap()
613+
unreachable
614+
615+
unreachable: ; preds = %entry
616+
unreachable
617+
}
618+
584619
declare void @destructor()
585620

586621
declare void @inita(ptr)

0 commit comments

Comments
 (0)