1 //===-- StackSlotColoring.cpp - Stack slot coloring pass. -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the stack slot coloring pass.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "stackcoloring"
15 #include "llvm/CodeGen/Passes.h"
16 #include "llvm/CodeGen/LiveStackAnalysis.h"
17 #include "llvm/CodeGen/MachineFrameInfo.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
28 DisableSharing("no-stack-slot-sharing",
29 cl::init(false), cl::Hidden,
30 cl::desc("Surpress slot sharing during stack coloring"));
32 STATISTIC(NumEliminated, "Number of stack slots eliminated due to coloring");
35 class VISIBILITY_HIDDEN StackSlotColoring : public MachineFunctionPass {
37 MachineFrameInfo *MFI;
39 // SSIntervals - Spill slot intervals.
40 std::vector<LiveInterval*> SSIntervals;
42 // OrigAlignments - Alignments of stack objects before coloring.
43 SmallVector<unsigned, 16> OrigAlignments;
45 // OrigSizes - Sizess of stack objects before coloring.
46 SmallVector<unsigned, 16> OrigSizes;
48 // AllColors - If index is set, it's a spill slot, i.e. color.
49 // FIXME: This assumes PEI locate spill slot with smaller indices
50 // closest to stack pointer / frame pointer. Therefore, smaller
51 // index == better color.
54 // NextColor - Next "color" that's not yet used.
57 // UsedColors - "Colors" that have been assigned.
60 // Assignments - Color to intervals mapping.
61 SmallVector<SmallVector<LiveInterval*,4>,16> Assignments;
64 static char ID; // Pass identification
65 StackSlotColoring() : MachineFunctionPass(&ID), NextColor(-1) {}
67 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
68 AU.addRequired<LiveStacks>();
69 AU.addPreservedID(MachineLoopInfoID);
70 AU.addPreservedID(MachineDominatorsID);
71 MachineFunctionPass::getAnalysisUsage(AU);
74 virtual bool runOnMachineFunction(MachineFunction &MF);
75 virtual const char* getPassName() const {
76 return "Stack Slot Coloring";
80 bool InitializeSlots();
81 bool OverlapWithAssignments(LiveInterval *li, int Color) const;
82 int ColorSlot(LiveInterval *li);
83 bool ColorSlots(MachineFunction &MF);
85 } // end anonymous namespace
87 char StackSlotColoring::ID = 0;
89 static RegisterPass<StackSlotColoring>
90 X("stack-slot-coloring", "Stack Slot Coloring");
92 FunctionPass *llvm::createStackSlotColoringPass() {
93 return new StackSlotColoring();
97 // IntervalSorter - Comparison predicate that sort live intervals by
99 struct IntervalSorter {
100 bool operator()(LiveInterval* LHS, LiveInterval* RHS) const {
101 return LHS->weight > RHS->weight;
106 /// InitializeSlots - Process all spill stack slot liveintervals and add them
107 /// to a sorted (by weight) list.
108 bool StackSlotColoring::InitializeSlots() {
109 if (LS->getNumIntervals() < 2)
112 int LastFI = MFI->getObjectIndexEnd();
113 OrigAlignments.resize(LastFI);
114 OrigSizes.resize(LastFI);
115 AllColors.resize(LastFI);
116 UsedColors.resize(LastFI);
117 Assignments.resize(LastFI);
119 // Gather all spill slots into a list.
120 for (LiveStacks::iterator i = LS->begin(), e = LS->end(); i != e; ++i) {
121 LiveInterval &li = i->second;
122 int FI = li.getStackSlotIndex();
123 if (MFI->isDeadObjectIndex(FI))
125 SSIntervals.push_back(&li);
126 OrigAlignments[FI] = MFI->getObjectAlignment(FI);
127 OrigSizes[FI] = MFI->getObjectSize(FI);
131 // Sort them by weight.
132 std::stable_sort(SSIntervals.begin(), SSIntervals.end(), IntervalSorter());
134 // Get first "color".
135 NextColor = AllColors.find_first();
139 /// OverlapWithAssignments - Return true if LiveInterval overlaps with any
140 /// LiveIntervals that have already been assigned to the specified color.
142 StackSlotColoring::OverlapWithAssignments(LiveInterval *li, int Color) const {
143 const SmallVector<LiveInterval*,4> &OtherLIs = Assignments[Color];
144 for (unsigned i = 0, e = OtherLIs.size(); i != e; ++i) {
145 LiveInterval *OtherLI = OtherLIs[i];
146 if (OtherLI->overlaps(*li))
152 /// ColorSlot - Assign a "color" (stack slot) to the specified stack slot.
154 int StackSlotColoring::ColorSlot(LiveInterval *li) {
157 if (!DisableSharing) {
158 // Check if it's possible to reuse any of the used colors.
159 Color = UsedColors.find_first();
160 while (Color != -1) {
161 if (!OverlapWithAssignments(li, Color)) {
166 Color = UsedColors.find_next(Color);
170 // Assign it to the first available color (assumed to be the best) if it's
171 // not possible to share a used color with other objects.
173 assert(NextColor != -1 && "No more spill slots?");
175 UsedColors.set(Color);
176 NextColor = AllColors.find_next(NextColor);
179 // Record the assignment.
180 Assignments[Color].push_back(li);
181 int FI = li->getStackSlotIndex();
182 DOUT << "Assigning fi #" << FI << " to fi #" << Color << "\n";
184 // Change size and alignment of the allocated slot. If there are multiple
185 // objects sharing the same slot, then make sure the size and alignment
186 // are large enough for all.
187 unsigned Align = OrigAlignments[FI];
188 if (!Share || Align > MFI->getObjectAlignment(Color))
189 MFI->setObjectAlignment(Color, Align);
190 int64_t Size = OrigSizes[FI];
191 if (!Share || Size > MFI->getObjectSize(Color))
192 MFI->setObjectSize(Color, Size);
196 /// Colorslots - Color all spill stack slots and rewrite all frameindex machine
197 /// operands in the function.
198 bool StackSlotColoring::ColorSlots(MachineFunction &MF) {
199 unsigned NumObjs = MFI->getObjectIndexEnd();
200 std::vector<int> SlotMapping(NumObjs, -1);
202 bool Changed = false;
203 for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i) {
204 LiveInterval *li = SSIntervals[i];
205 int SS = li->getStackSlotIndex();
206 int NewSS = ColorSlot(li);
207 SlotMapping[SS] = NewSS;
208 Changed |= (SS != NewSS);
214 // Rewrite all MO_FrameIndex operands.
215 // FIXME: Need the equivalent of MachineRegisterInfo for frameindex operands.
216 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end();
218 for (MachineBasicBlock::iterator MII = MBB->begin(), EE = MBB->end();
220 MachineInstr &MI = *MII;
221 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
222 MachineOperand &MO = MI.getOperand(i);
223 if (!MO.isFrameIndex())
225 int FI = MO.getIndex();
228 FI = SlotMapping[FI];
236 // Delete unused stack slots.
237 while (NextColor != -1) {
238 DOUT << "Removing unused stack object fi #" << NextColor << "\n";
239 MFI->RemoveStackObject(NextColor);
240 NextColor = AllColors.find_next(NextColor);
246 bool StackSlotColoring::runOnMachineFunction(MachineFunction &MF) {
247 DOUT << "********** Stack Slot Coloring **********\n";
249 MFI = MF.getFrameInfo();
250 LS = &getAnalysis<LiveStacks>();
252 bool Changed = false;
253 if (InitializeSlots())
254 Changed = ColorSlots(MF);
258 OrigAlignments.clear();
262 for (unsigned i = 0, e = Assignments.size(); i != e; ++i)
263 Assignments[i].clear();