1 //===- StackSlotColoring.cpp - Stack slot coloring pass. ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the stack slot coloring pass.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/BitVector.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/CodeGen/LiveInterval.h"
17 #include "llvm/CodeGen/LiveIntervals.h"
18 #include "llvm/CodeGen/LiveStacks.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineMemOperand.h"
26 #include "llvm/CodeGen/MachineOperand.h"
27 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/CodeGen/PseudoSourceValue.h"
29 #include "llvm/CodeGen/SlotIndexes.h"
30 #include "llvm/CodeGen/TargetInstrInfo.h"
31 #include "llvm/CodeGen/TargetSubtargetInfo.h"
32 #include "llvm/InitializePasses.h"
33 #include "llvm/Pass.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include <algorithm>
39 #include <cassert>
40 #include <cstdint>
41 #include <iterator>
42 #include <vector>
43 
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "stack-slot-coloring"
47 
48 static cl::opt<bool>
49 DisableSharing("no-stack-slot-sharing",
50              cl::init(false), cl::Hidden,
51              cl::desc("Suppress slot sharing during stack coloring"));
52 
53 static cl::opt<int> DCELimit("ssc-dce-limit", cl::init(-1), cl::Hidden);
54 
55 STATISTIC(NumEliminated, "Number of stack slots eliminated due to coloring");
56 STATISTIC(NumDead,       "Number of trivially dead stack accesses eliminated");
57 
58 namespace {
59 
60   class StackSlotColoring : public MachineFunctionPass {
61     LiveStacks* LS;
62     MachineFrameInfo *MFI;
63     const TargetInstrInfo  *TII;
64     const MachineBlockFrequencyInfo *MBFI;
65 
66     // SSIntervals - Spill slot intervals.
67     std::vector<LiveInterval*> SSIntervals;
68 
69     // SSRefs - Keep a list of MachineMemOperands for each spill slot.
70     // MachineMemOperands can be shared between instructions, so we need
71     // to be careful that renames like [FI0, FI1] -> [FI1, FI2] do not
72     // become FI0 -> FI1 -> FI2.
73     SmallVector<SmallVector<MachineMemOperand *, 8>, 16> SSRefs;
74 
75     // OrigAlignments - Alignments of stack objects before coloring.
76     SmallVector<Align, 16> OrigAlignments;
77 
78     // OrigSizes - Sizes of stack objects before coloring.
79     SmallVector<unsigned, 16> OrigSizes;
80 
81     // AllColors - If index is set, it's a spill slot, i.e. color.
82     // FIXME: This assumes PEI locate spill slot with smaller indices
83     // closest to stack pointer / frame pointer. Therefore, smaller
84     // index == better color. This is per stack ID.
85     SmallVector<BitVector, 2> AllColors;
86 
87     // NextColor - Next "color" that's not yet used. This is per stack ID.
88     SmallVector<int, 2> NextColors = { -1 };
89 
90     // UsedColors - "Colors" that have been assigned. This is per stack ID
91     SmallVector<BitVector, 2> UsedColors;
92 
93     // Assignments - Color to intervals mapping.
94     SmallVector<SmallVector<LiveInterval*,4>, 16> Assignments;
95 
96   public:
97     static char ID; // Pass identification
98 
99     StackSlotColoring() : MachineFunctionPass(ID) {
100       initializeStackSlotColoringPass(*PassRegistry::getPassRegistry());
101     }
102 
103     void getAnalysisUsage(AnalysisUsage &AU) const override {
104       AU.setPreservesCFG();
105       AU.addRequired<SlotIndexes>();
106       AU.addPreserved<SlotIndexes>();
107       AU.addRequired<LiveStacks>();
108       AU.addRequired<MachineBlockFrequencyInfo>();
109       AU.addPreserved<MachineBlockFrequencyInfo>();
110       AU.addPreservedID(MachineDominatorsID);
111       MachineFunctionPass::getAnalysisUsage(AU);
112     }
113 
114     bool runOnMachineFunction(MachineFunction &MF) override;
115 
116   private:
117     void InitializeSlots();
118     void ScanForSpillSlotRefs(MachineFunction &MF);
119     bool OverlapWithAssignments(LiveInterval *li, int Color) const;
120     int ColorSlot(LiveInterval *li);
121     bool ColorSlots(MachineFunction &MF);
122     void RewriteInstruction(MachineInstr &MI, SmallVectorImpl<int> &SlotMapping,
123                             MachineFunction &MF);
124     bool RemoveDeadStores(MachineBasicBlock* MBB);
125   };
126 
127 } // end anonymous namespace
128 
129 char StackSlotColoring::ID = 0;
130 
131 char &llvm::StackSlotColoringID = StackSlotColoring::ID;
132 
133 INITIALIZE_PASS_BEGIN(StackSlotColoring, DEBUG_TYPE,
134                 "Stack Slot Coloring", false, false)
135 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
136 INITIALIZE_PASS_DEPENDENCY(LiveStacks)
137 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
138 INITIALIZE_PASS_END(StackSlotColoring, DEBUG_TYPE,
139                 "Stack Slot Coloring", false, false)
140 
141 namespace {
142 
143 // IntervalSorter - Comparison predicate that sort live intervals by
144 // their weight.
145 struct IntervalSorter {
146   bool operator()(LiveInterval* LHS, LiveInterval* RHS) const {
147     return LHS->weight() > RHS->weight();
148   }
149 };
150 
151 } // end anonymous namespace
152 
153 /// ScanForSpillSlotRefs - Scan all the machine instructions for spill slot
154 /// references and update spill slot weights.
155 void StackSlotColoring::ScanForSpillSlotRefs(MachineFunction &MF) {
156   SSRefs.resize(MFI->getObjectIndexEnd());
157 
158   // FIXME: Need the equivalent of MachineRegisterInfo for frameindex operands.
159   for (MachineBasicBlock &MBB : MF) {
160     for (MachineInstr &MI : MBB) {
161       for (const MachineOperand &MO : MI.operands()) {
162         if (!MO.isFI())
163           continue;
164         int FI = MO.getIndex();
165         if (FI < 0)
166           continue;
167         if (!LS->hasInterval(FI))
168           continue;
169         LiveInterval &li = LS->getInterval(FI);
170         if (!MI.isDebugInstr())
171           li.incrementWeight(
172               LiveIntervals::getSpillWeight(false, true, MBFI, MI));
173       }
174       for (MachineInstr::mmo_iterator MMOI = MI.memoperands_begin(),
175                                       EE = MI.memoperands_end();
176            MMOI != EE; ++MMOI) {
177         MachineMemOperand *MMO = *MMOI;
178         if (const FixedStackPseudoSourceValue *FSV =
179             dyn_cast_or_null<FixedStackPseudoSourceValue>(
180                 MMO->getPseudoValue())) {
181           int FI = FSV->getFrameIndex();
182           if (FI >= 0)
183             SSRefs[FI].push_back(MMO);
184         }
185       }
186     }
187   }
188 }
189 
190 /// InitializeSlots - Process all spill stack slot liveintervals and add them
191 /// to a sorted (by weight) list.
192 void StackSlotColoring::InitializeSlots() {
193   int LastFI = MFI->getObjectIndexEnd();
194 
195   // There is always at least one stack ID.
196   AllColors.resize(1);
197   UsedColors.resize(1);
198 
199   OrigAlignments.resize(LastFI);
200   OrigSizes.resize(LastFI);
201   AllColors[0].resize(LastFI);
202   UsedColors[0].resize(LastFI);
203   Assignments.resize(LastFI);
204 
205   using Pair = std::iterator_traits<LiveStacks::iterator>::value_type;
206 
207   SmallVector<Pair *, 16> Intervals;
208 
209   Intervals.reserve(LS->getNumIntervals());
210   for (auto &I : *LS)
211     Intervals.push_back(&I);
212   llvm::sort(Intervals,
213              [](Pair *LHS, Pair *RHS) { return LHS->first < RHS->first; });
214 
215   // Gather all spill slots into a list.
216   LLVM_DEBUG(dbgs() << "Spill slot intervals:\n");
217   for (auto *I : Intervals) {
218     LiveInterval &li = I->second;
219     LLVM_DEBUG(li.dump());
220     int FI = Register::stackSlot2Index(li.reg());
221     if (MFI->isDeadObjectIndex(FI))
222       continue;
223 
224     SSIntervals.push_back(&li);
225     OrigAlignments[FI] = MFI->getObjectAlign(FI);
226     OrigSizes[FI]      = MFI->getObjectSize(FI);
227 
228     auto StackID = MFI->getStackID(FI);
229     if (StackID != 0) {
230       AllColors.resize(StackID + 1);
231       UsedColors.resize(StackID + 1);
232       AllColors[StackID].resize(LastFI);
233       UsedColors[StackID].resize(LastFI);
234     }
235 
236     AllColors[StackID].set(FI);
237   }
238   LLVM_DEBUG(dbgs() << '\n');
239 
240   // Sort them by weight.
241   llvm::stable_sort(SSIntervals, IntervalSorter());
242 
243   NextColors.resize(AllColors.size());
244 
245   // Get first "color".
246   for (unsigned I = 0, E = AllColors.size(); I != E; ++I)
247     NextColors[I] = AllColors[I].find_first();
248 }
249 
250 /// OverlapWithAssignments - Return true if LiveInterval overlaps with any
251 /// LiveIntervals that have already been assigned to the specified color.
252 bool
253 StackSlotColoring::OverlapWithAssignments(LiveInterval *li, int Color) const {
254   const SmallVectorImpl<LiveInterval *> &OtherLIs = Assignments[Color];
255   for (unsigned i = 0, e = OtherLIs.size(); i != e; ++i) {
256     LiveInterval *OtherLI = OtherLIs[i];
257     if (OtherLI->overlaps(*li))
258       return true;
259   }
260   return false;
261 }
262 
263 /// ColorSlot - Assign a "color" (stack slot) to the specified stack slot.
264 int StackSlotColoring::ColorSlot(LiveInterval *li) {
265   int Color = -1;
266   bool Share = false;
267   int FI = Register::stackSlot2Index(li->reg());
268   uint8_t StackID = MFI->getStackID(FI);
269 
270   if (!DisableSharing) {
271 
272     // Check if it's possible to reuse any of the used colors.
273     Color = UsedColors[StackID].find_first();
274     while (Color != -1) {
275       if (!OverlapWithAssignments(li, Color)) {
276         Share = true;
277         ++NumEliminated;
278         break;
279       }
280       Color = UsedColors[StackID].find_next(Color);
281     }
282   }
283 
284   if (Color != -1 && MFI->getStackID(Color) != MFI->getStackID(FI)) {
285     LLVM_DEBUG(dbgs() << "cannot share FIs with different stack IDs\n");
286     Share = false;
287   }
288 
289   // Assign it to the first available color (assumed to be the best) if it's
290   // not possible to share a used color with other objects.
291   if (!Share) {
292     assert(NextColors[StackID] != -1 && "No more spill slots?");
293     Color = NextColors[StackID];
294     UsedColors[StackID].set(Color);
295     NextColors[StackID] = AllColors[StackID].find_next(NextColors[StackID]);
296   }
297 
298   assert(MFI->getStackID(Color) == MFI->getStackID(FI));
299 
300   // Record the assignment.
301   Assignments[Color].push_back(li);
302   LLVM_DEBUG(dbgs() << "Assigning fi#" << FI << " to fi#" << Color << "\n");
303 
304   // Change size and alignment of the allocated slot. If there are multiple
305   // objects sharing the same slot, then make sure the size and alignment
306   // are large enough for all.
307   Align Alignment = OrigAlignments[FI];
308   if (!Share || Alignment > MFI->getObjectAlign(Color))
309     MFI->setObjectAlignment(Color, Alignment);
310   int64_t Size = OrigSizes[FI];
311   if (!Share || Size > MFI->getObjectSize(Color))
312     MFI->setObjectSize(Color, Size);
313   return Color;
314 }
315 
316 /// Colorslots - Color all spill stack slots and rewrite all frameindex machine
317 /// operands in the function.
318 bool StackSlotColoring::ColorSlots(MachineFunction &MF) {
319   unsigned NumObjs = MFI->getObjectIndexEnd();
320   SmallVector<int, 16> SlotMapping(NumObjs, -1);
321   SmallVector<float, 16> SlotWeights(NumObjs, 0.0);
322   SmallVector<SmallVector<int, 4>, 16> RevMap(NumObjs);
323   BitVector UsedColors(NumObjs);
324 
325   LLVM_DEBUG(dbgs() << "Color spill slot intervals:\n");
326   bool Changed = false;
327   for (LiveInterval *li : SSIntervals) {
328     int SS = Register::stackSlot2Index(li->reg());
329     int NewSS = ColorSlot(li);
330     assert(NewSS >= 0 && "Stack coloring failed?");
331     SlotMapping[SS] = NewSS;
332     RevMap[NewSS].push_back(SS);
333     SlotWeights[NewSS] += li->weight();
334     UsedColors.set(NewSS);
335     Changed |= (SS != NewSS);
336   }
337 
338   LLVM_DEBUG(dbgs() << "\nSpill slots after coloring:\n");
339   for (LiveInterval *li : SSIntervals) {
340     int SS = Register::stackSlot2Index(li->reg());
341     li->setWeight(SlotWeights[SS]);
342   }
343   // Sort them by new weight.
344   llvm::stable_sort(SSIntervals, IntervalSorter());
345 
346 #ifndef NDEBUG
347   for (LiveInterval *li : SSIntervals)
348     LLVM_DEBUG(li->dump());
349   LLVM_DEBUG(dbgs() << '\n');
350 #endif
351 
352   if (!Changed)
353     return false;
354 
355   // Rewrite all MachineMemOperands.
356   for (unsigned SS = 0, SE = SSRefs.size(); SS != SE; ++SS) {
357     int NewFI = SlotMapping[SS];
358     if (NewFI == -1 || (NewFI == (int)SS))
359       continue;
360 
361     const PseudoSourceValue *NewSV = MF.getPSVManager().getFixedStack(NewFI);
362     SmallVectorImpl<MachineMemOperand *> &RefMMOs = SSRefs[SS];
363     for (unsigned i = 0, e = RefMMOs.size(); i != e; ++i)
364       RefMMOs[i]->setValue(NewSV);
365   }
366 
367   // Rewrite all MO_FrameIndex operands.  Look for dead stores.
368   for (MachineBasicBlock &MBB : MF) {
369     for (MachineInstr &MI : MBB)
370       RewriteInstruction(MI, SlotMapping, MF);
371     RemoveDeadStores(&MBB);
372   }
373 
374   // Delete unused stack slots.
375   for (int StackID = 0, E = AllColors.size(); StackID != E; ++StackID) {
376     int NextColor = NextColors[StackID];
377     while (NextColor != -1) {
378       LLVM_DEBUG(dbgs() << "Removing unused stack object fi#" << NextColor << "\n");
379       MFI->RemoveStackObject(NextColor);
380       NextColor = AllColors[StackID].find_next(NextColor);
381     }
382   }
383 
384   return true;
385 }
386 
387 /// RewriteInstruction - Rewrite specified instruction by replacing references
388 /// to old frame index with new one.
389 void StackSlotColoring::RewriteInstruction(MachineInstr &MI,
390                                            SmallVectorImpl<int> &SlotMapping,
391                                            MachineFunction &MF) {
392   // Update the operands.
393   for (MachineOperand &MO : MI.operands()) {
394     if (!MO.isFI())
395       continue;
396     int OldFI = MO.getIndex();
397     if (OldFI < 0)
398       continue;
399     int NewFI = SlotMapping[OldFI];
400     if (NewFI == -1 || NewFI == OldFI)
401       continue;
402 
403     assert(MFI->getStackID(OldFI) == MFI->getStackID(NewFI));
404     MO.setIndex(NewFI);
405   }
406 
407   // The MachineMemOperands have already been updated.
408 }
409 
410 /// RemoveDeadStores - Scan through a basic block and look for loads followed
411 /// by stores.  If they're both using the same stack slot, then the store is
412 /// definitely dead.  This could obviously be much more aggressive (consider
413 /// pairs with instructions between them), but such extensions might have a
414 /// considerable compile time impact.
415 bool StackSlotColoring::RemoveDeadStores(MachineBasicBlock* MBB) {
416   // FIXME: This could be much more aggressive, but we need to investigate
417   // the compile time impact of doing so.
418   bool changed = false;
419 
420   SmallVector<MachineInstr*, 4> toErase;
421 
422   for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
423        I != E; ++I) {
424     if (DCELimit != -1 && (int)NumDead >= DCELimit)
425       break;
426     int FirstSS, SecondSS;
427     if (TII->isStackSlotCopy(*I, FirstSS, SecondSS) && FirstSS == SecondSS &&
428         FirstSS != -1) {
429       ++NumDead;
430       changed = true;
431       toErase.push_back(&*I);
432       continue;
433     }
434 
435     MachineBasicBlock::iterator NextMI = std::next(I);
436     MachineBasicBlock::iterator ProbableLoadMI = I;
437 
438     unsigned LoadReg = 0;
439     unsigned StoreReg = 0;
440     unsigned LoadSize = 0;
441     unsigned StoreSize = 0;
442     if (!(LoadReg = TII->isLoadFromStackSlot(*I, FirstSS, LoadSize)))
443       continue;
444     // Skip the ...pseudo debugging... instructions between a load and store.
445     while ((NextMI != E) && NextMI->isDebugInstr()) {
446       ++NextMI;
447       ++I;
448     }
449     if (NextMI == E) continue;
450     if (!(StoreReg = TII->isStoreToStackSlot(*NextMI, SecondSS, StoreSize)))
451       continue;
452     if (FirstSS != SecondSS || LoadReg != StoreReg || FirstSS == -1 ||
453         LoadSize != StoreSize)
454       continue;
455 
456     ++NumDead;
457     changed = true;
458 
459     if (NextMI->findRegisterUseOperandIdx(LoadReg, true, nullptr) != -1) {
460       ++NumDead;
461       toErase.push_back(&*ProbableLoadMI);
462     }
463 
464     toErase.push_back(&*NextMI);
465     ++I;
466   }
467 
468   for (MachineInstr *MI : toErase)
469     MI->eraseFromParent();
470 
471   return changed;
472 }
473 
474 bool StackSlotColoring::runOnMachineFunction(MachineFunction &MF) {
475   LLVM_DEBUG({
476     dbgs() << "********** Stack Slot Coloring **********\n"
477            << "********** Function: " << MF.getName() << '\n';
478   });
479 
480   if (skipFunction(MF.getFunction()))
481     return false;
482 
483   MFI = &MF.getFrameInfo();
484   TII = MF.getSubtarget().getInstrInfo();
485   LS = &getAnalysis<LiveStacks>();
486   MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
487 
488   bool Changed = false;
489 
490   unsigned NumSlots = LS->getNumIntervals();
491   if (NumSlots == 0)
492     // Nothing to do!
493     return false;
494 
495   // If there are calls to setjmp or sigsetjmp, don't perform stack slot
496   // coloring. The stack could be modified before the longjmp is executed,
497   // resulting in the wrong value being used afterwards. (See
498   // <rdar://problem/8007500>.)
499   if (MF.exposesReturnsTwice())
500     return false;
501 
502   // Gather spill slot references
503   ScanForSpillSlotRefs(MF);
504   InitializeSlots();
505   Changed = ColorSlots(MF);
506 
507   for (int &Next : NextColors)
508     Next = -1;
509 
510   SSIntervals.clear();
511   for (unsigned i = 0, e = SSRefs.size(); i != e; ++i)
512     SSRefs[i].clear();
513   SSRefs.clear();
514   OrigAlignments.clear();
515   OrigSizes.clear();
516   AllColors.clear();
517   UsedColors.clear();
518   for (unsigned i = 0, e = Assignments.size(); i != e; ++i)
519     Assignments[i].clear();
520   Assignments.clear();
521 
522   return Changed;
523 }
524