From e13e0f881dbec891d4f57ae63649e8aa32d14773 Mon Sep 17 00:00:00 2001 From: vg0204 Date: Mon, 10 Jun 2024 17:07:35 +0530 Subject: [PATCH 1/2] [CodeGen] Preserve LiveStack analysis in StackSlotColoring pass For some target architectures, stack slot coloring pass may be invoked multiple times. It can occur due to the split of RA pass, opening up the opportunity to optimize stack slots usage at each RA invocation. In order to achieve that if we could keep stack analysis alive uptil the invocation of the RA pass for the last time, it will save up the overhead of recomputing live stack analysis after each RA. This requires it to be preserved at stack slot coloring pass which basically optimizes stack slots, but not makes the needed updates in live stack results. This has been achieved here. --- llvm/lib/CodeGen/StackSlotColoring.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/llvm/lib/CodeGen/StackSlotColoring.cpp b/llvm/lib/CodeGen/StackSlotColoring.cpp index 9fdc8a338b52a..7bf966b21ebc1 100644 --- a/llvm/lib/CodeGen/StackSlotColoring.cpp +++ b/llvm/lib/CodeGen/StackSlotColoring.cpp @@ -149,6 +149,7 @@ namespace { AU.addRequired(); AU.addPreserved(); AU.addRequired(); + AU.addPreserved(); AU.addRequired(); AU.addPreserved(); AU.addPreservedID(MachineDominatorsID); @@ -411,6 +412,23 @@ bool StackSlotColoring::ColorSlots(MachineFunction &MF) { } } + // In order to preserve LiveStack analysis, the live ranges for dead spill + // stack slots would be merged with the live range of those stack slots that + // now share the spill object of the mentioned dead stack slot. + for (unsigned SS = 0, SE = SlotMapping.size(); SS != SE; ++SS) { + int NewFI = SlotMapping[SS]; + if (SlotMapping[SS] == -1 || (NewFI == (int)SS)) { + continue; + } + + LiveRange &lrToUpdateInto = + static_cast(LS->getInterval(NewFI)); + const LiveRange &lrToUpdateFrom = + static_cast(LS->getInterval((int)SS)); + lrToUpdateInto.MergeSegmentsInAsValue(lrToUpdateFrom, + lrToUpdateInto.getValNumInfo(0)); + } + return true; } From b0c08a6a41effe5eddb39708f9a8f55c496c6f6c Mon Sep 17 00:00:00 2001 From: vg0204 Date: Tue, 11 Jun 2024 14:36:58 +0530 Subject: [PATCH 2/2] preserved LS analysis by clearing the info not needed anymore. --- llvm/lib/CodeGen/StackSlotColoring.cpp | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/llvm/lib/CodeGen/StackSlotColoring.cpp b/llvm/lib/CodeGen/StackSlotColoring.cpp index 7bf966b21ebc1..9bc28bb680157 100644 --- a/llvm/lib/CodeGen/StackSlotColoring.cpp +++ b/llvm/lib/CodeGen/StackSlotColoring.cpp @@ -412,23 +412,6 @@ bool StackSlotColoring::ColorSlots(MachineFunction &MF) { } } - // In order to preserve LiveStack analysis, the live ranges for dead spill - // stack slots would be merged with the live range of those stack slots that - // now share the spill object of the mentioned dead stack slot. - for (unsigned SS = 0, SE = SlotMapping.size(); SS != SE; ++SS) { - int NewFI = SlotMapping[SS]; - if (SlotMapping[SS] == -1 || (NewFI == (int)SS)) { - continue; - } - - LiveRange &lrToUpdateInto = - static_cast(LS->getInterval(NewFI)); - const LiveRange &lrToUpdateFrom = - static_cast(LS->getInterval((int)SS)); - lrToUpdateInto.MergeSegmentsInAsValue(lrToUpdateFrom, - lrToUpdateInto.getValNumInfo(0)); - } - return true; } @@ -552,6 +535,12 @@ bool StackSlotColoring::runOnMachineFunction(MachineFunction &MF) { InitializeSlots(); Changed = ColorSlots(MF); + // Clear LiveStack analysis as it has been changed in ways that it requires + // efforts to rectify which is equivalent to do it all over again. Also, + // this current results would not be used later, so better to clear it & + // preserve analysis. + LS->releaseMemory(); + for (int &Next : NextColors) Next = -1;