LLVM  3.7.0
StackSlotColoring.cpp
Go to the documentation of this file.
1 //===-- StackSlotColoring.cpp - Stack slot coloring pass. -----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the stack slot coloring pass.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/Passes.h"
15 #include "llvm/ADT/BitVector.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/Statistic.h"
26 #include "llvm/IR/Module.h"
28 #include "llvm/Support/Debug.h"
32 #include <vector>
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "stackslotcoloring"
36 
37 static cl::opt<bool>
38 DisableSharing("no-stack-slot-sharing",
39  cl::init(false), cl::Hidden,
40  cl::desc("Suppress slot sharing during stack coloring"));
41 
42 static cl::opt<int> DCELimit("ssc-dce-limit", cl::init(-1), cl::Hidden);
43 
44 STATISTIC(NumEliminated, "Number of stack slots eliminated due to coloring");
45 STATISTIC(NumDead, "Number of trivially dead stack accesses eliminated");
46 
47 namespace {
48  class StackSlotColoring : public MachineFunctionPass {
49  LiveStacks* LS;
50  MachineFrameInfo *MFI;
51  const TargetInstrInfo *TII;
52  const MachineBlockFrequencyInfo *MBFI;
53 
54  // SSIntervals - Spill slot intervals.
55  std::vector<LiveInterval*> SSIntervals;
56 
57  // SSRefs - Keep a list of MachineMemOperands for each spill slot.
58  // MachineMemOperands can be shared between instructions, so we need
59  // to be careful that renames like [FI0, FI1] -> [FI1, FI2] do not
60  // become FI0 -> FI1 -> FI2.
62 
63  // OrigAlignments - Alignments of stack objects before coloring.
64  SmallVector<unsigned, 16> OrigAlignments;
65 
66  // OrigSizes - Sizess of stack objects before coloring.
67  SmallVector<unsigned, 16> OrigSizes;
68 
69  // AllColors - If index is set, it's a spill slot, i.e. color.
70  // FIXME: This assumes PEI locate spill slot with smaller indices
71  // closest to stack pointer / frame pointer. Therefore, smaller
72  // index == better color.
73  BitVector AllColors;
74 
75  // NextColor - Next "color" that's not yet used.
76  int NextColor;
77 
78  // UsedColors - "Colors" that have been assigned.
79  BitVector UsedColors;
80 
81  // Assignments - Color to intervals mapping.
83 
84  public:
85  static char ID; // Pass identification
86  StackSlotColoring() :
87  MachineFunctionPass(ID), NextColor(-1) {
89  }
90 
91  void getAnalysisUsage(AnalysisUsage &AU) const override {
92  AU.setPreservesCFG();
95  AU.addRequired<LiveStacks>();
100  }
101 
102  bool runOnMachineFunction(MachineFunction &MF) override;
103 
104  private:
105  void InitializeSlots();
106  void ScanForSpillSlotRefs(MachineFunction &MF);
107  bool OverlapWithAssignments(LiveInterval *li, int Color) const;
108  int ColorSlot(LiveInterval *li);
109  bool ColorSlots(MachineFunction &MF);
110  void RewriteInstruction(MachineInstr *MI, SmallVectorImpl<int> &SlotMapping,
111  MachineFunction &MF);
112  bool RemoveDeadStores(MachineBasicBlock* MBB);
113  };
114 } // end anonymous namespace
115 
116 char StackSlotColoring::ID = 0;
118 
119 INITIALIZE_PASS_BEGIN(StackSlotColoring, "stack-slot-coloring",
120  "Stack Slot Coloring", false, false)
124 INITIALIZE_PASS_END(StackSlotColoring, "stack-slot-coloring",
125  "Stack Slot Coloring", false, false)
126 
127 namespace {
128  // IntervalSorter - Comparison predicate that sort live intervals by
129  // their weight.
130  struct IntervalSorter {
131  bool operator()(LiveInterval* LHS, LiveInterval* RHS) const {
132  return LHS->weight > RHS->weight;
133  }
134  };
135 }
136 
137 /// ScanForSpillSlotRefs - Scan all the machine instructions for spill slot
138 /// references and update spill slot weights.
139 void StackSlotColoring::ScanForSpillSlotRefs(MachineFunction &MF) {
140  SSRefs.resize(MFI->getObjectIndexEnd());
141 
142  // FIXME: Need the equivalent of MachineRegisterInfo for frameindex operands.
143  for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
144  MBBI != E; ++MBBI) {
145  MachineBasicBlock *MBB = &*MBBI;
146  for (MachineBasicBlock::iterator MII = MBB->begin(), EE = MBB->end();
147  MII != EE; ++MII) {
148  MachineInstr *MI = &*MII;
149  for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
150  MachineOperand &MO = MI->getOperand(i);
151  if (!MO.isFI())
152  continue;
153  int FI = MO.getIndex();
154  if (FI < 0)
155  continue;
156  if (!LS->hasInterval(FI))
157  continue;
158  LiveInterval &li = LS->getInterval(FI);
159  if (!MI->isDebugValue())
160  li.weight += LiveIntervals::getSpillWeight(false, true, MBFI, MI);
161  }
163  EE = MI->memoperands_end(); MMOI != EE; ++MMOI) {
164  MachineMemOperand *MMO = *MMOI;
165  if (const FixedStackPseudoSourceValue *FSV =
166  dyn_cast_or_null<FixedStackPseudoSourceValue>(
167  MMO->getPseudoValue())) {
168  int FI = FSV->getFrameIndex();
169  if (FI >= 0)
170  SSRefs[FI].push_back(MMO);
171  }
172  }
173  }
174  }
175 }
176 
177 /// InitializeSlots - Process all spill stack slot liveintervals and add them
178 /// to a sorted (by weight) list.
179 void StackSlotColoring::InitializeSlots() {
180  int LastFI = MFI->getObjectIndexEnd();
181  OrigAlignments.resize(LastFI);
182  OrigSizes.resize(LastFI);
183  AllColors.resize(LastFI);
184  UsedColors.resize(LastFI);
185  Assignments.resize(LastFI);
186 
187  typedef std::iterator_traits<LiveStacks::iterator>::value_type Pair;
189  Intervals.reserve(LS->getNumIntervals());
190  for (auto &I : *LS)
191  Intervals.push_back(&I);
192  std::sort(Intervals.begin(), Intervals.end(),
193  [](Pair *LHS, Pair *RHS) { return LHS->first < RHS->first; });
194 
195  // Gather all spill slots into a list.
196  DEBUG(dbgs() << "Spill slot intervals:\n");
197  for (auto *I : Intervals) {
198  LiveInterval &li = I->second;
199  DEBUG(li.dump());
201  if (MFI->isDeadObjectIndex(FI))
202  continue;
203  SSIntervals.push_back(&li);
204  OrigAlignments[FI] = MFI->getObjectAlignment(FI);
205  OrigSizes[FI] = MFI->getObjectSize(FI);
206  AllColors.set(FI);
207  }
208  DEBUG(dbgs() << '\n');
209 
210  // Sort them by weight.
211  std::stable_sort(SSIntervals.begin(), SSIntervals.end(), IntervalSorter());
212 
213  // Get first "color".
214  NextColor = AllColors.find_first();
215 }
216 
217 /// OverlapWithAssignments - Return true if LiveInterval overlaps with any
218 /// LiveIntervals that have already been assigned to the specified color.
219 bool
220 StackSlotColoring::OverlapWithAssignments(LiveInterval *li, int Color) const {
221  const SmallVectorImpl<LiveInterval *> &OtherLIs = Assignments[Color];
222  for (unsigned i = 0, e = OtherLIs.size(); i != e; ++i) {
223  LiveInterval *OtherLI = OtherLIs[i];
224  if (OtherLI->overlaps(*li))
225  return true;
226  }
227  return false;
228 }
229 
230 /// ColorSlot - Assign a "color" (stack slot) to the specified stack slot.
231 ///
232 int StackSlotColoring::ColorSlot(LiveInterval *li) {
233  int Color = -1;
234  bool Share = false;
235  if (!DisableSharing) {
236  // Check if it's possible to reuse any of the used colors.
237  Color = UsedColors.find_first();
238  while (Color != -1) {
239  if (!OverlapWithAssignments(li, Color)) {
240  Share = true;
241  ++NumEliminated;
242  break;
243  }
244  Color = UsedColors.find_next(Color);
245  }
246  }
247 
248  // Assign it to the first available color (assumed to be the best) if it's
249  // not possible to share a used color with other objects.
250  if (!Share) {
251  assert(NextColor != -1 && "No more spill slots?");
252  Color = NextColor;
253  UsedColors.set(Color);
254  NextColor = AllColors.find_next(NextColor);
255  }
256 
257  // Record the assignment.
258  Assignments[Color].push_back(li);
260  DEBUG(dbgs() << "Assigning fi#" << FI << " to fi#" << Color << "\n");
261 
262  // Change size and alignment of the allocated slot. If there are multiple
263  // objects sharing the same slot, then make sure the size and alignment
264  // are large enough for all.
265  unsigned Align = OrigAlignments[FI];
266  if (!Share || Align > MFI->getObjectAlignment(Color))
267  MFI->setObjectAlignment(Color, Align);
268  int64_t Size = OrigSizes[FI];
269  if (!Share || Size > MFI->getObjectSize(Color))
270  MFI->setObjectSize(Color, Size);
271  return Color;
272 }
273 
274 /// Colorslots - Color all spill stack slots and rewrite all frameindex machine
275 /// operands in the function.
276 bool StackSlotColoring::ColorSlots(MachineFunction &MF) {
277  unsigned NumObjs = MFI->getObjectIndexEnd();
278  SmallVector<int, 16> SlotMapping(NumObjs, -1);
279  SmallVector<float, 16> SlotWeights(NumObjs, 0.0);
280  SmallVector<SmallVector<int, 4>, 16> RevMap(NumObjs);
281  BitVector UsedColors(NumObjs);
282 
283  DEBUG(dbgs() << "Color spill slot intervals:\n");
284  bool Changed = false;
285  for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i) {
286  LiveInterval *li = SSIntervals[i];
288  int NewSS = ColorSlot(li);
289  assert(NewSS >= 0 && "Stack coloring failed?");
290  SlotMapping[SS] = NewSS;
291  RevMap[NewSS].push_back(SS);
292  SlotWeights[NewSS] += li->weight;
293  UsedColors.set(NewSS);
294  Changed |= (SS != NewSS);
295  }
296 
297  DEBUG(dbgs() << "\nSpill slots after coloring:\n");
298  for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i) {
299  LiveInterval *li = SSIntervals[i];
301  li->weight = SlotWeights[SS];
302  }
303  // Sort them by new weight.
304  std::stable_sort(SSIntervals.begin(), SSIntervals.end(), IntervalSorter());
305 
306 #ifndef NDEBUG
307  for (unsigned i = 0, e = SSIntervals.size(); i != e; ++i)
308  DEBUG(SSIntervals[i]->dump());
309  DEBUG(dbgs() << '\n');
310 #endif
311 
312  if (!Changed)
313  return false;
314 
315  // Rewrite all MachineMemOperands.
316  for (unsigned SS = 0, SE = SSRefs.size(); SS != SE; ++SS) {
317  int NewFI = SlotMapping[SS];
318  if (NewFI == -1 || (NewFI == (int)SS))
319  continue;
320 
322  SmallVectorImpl<MachineMemOperand *> &RefMMOs = SSRefs[SS];
323  for (unsigned i = 0, e = RefMMOs.size(); i != e; ++i)
324  RefMMOs[i]->setValue(NewSV);
325  }
326 
327  // Rewrite all MO_FrameIndex operands. Look for dead stores.
328  for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
329  MBBI != E; ++MBBI) {
330  MachineBasicBlock *MBB = &*MBBI;
331  for (MachineBasicBlock::iterator MII = MBB->begin(), EE = MBB->end();
332  MII != EE; ++MII)
333  RewriteInstruction(MII, SlotMapping, MF);
334  RemoveDeadStores(MBB);
335  }
336 
337  // Delete unused stack slots.
338  while (NextColor != -1) {
339  DEBUG(dbgs() << "Removing unused stack object fi#" << NextColor << "\n");
340  MFI->RemoveStackObject(NextColor);
341  NextColor = AllColors.find_next(NextColor);
342  }
343 
344  return true;
345 }
346 
347 /// RewriteInstruction - Rewrite specified instruction by replacing references
348 /// to old frame index with new one.
349 void StackSlotColoring::RewriteInstruction(MachineInstr *MI,
351  MachineFunction &MF) {
352  // Update the operands.
353  for (unsigned i = 0, ee = MI->getNumOperands(); i != ee; ++i) {
354  MachineOperand &MO = MI->getOperand(i);
355  if (!MO.isFI())
356  continue;
357  int OldFI = MO.getIndex();
358  if (OldFI < 0)
359  continue;
360  int NewFI = SlotMapping[OldFI];
361  if (NewFI == -1 || NewFI == OldFI)
362  continue;
363  MO.setIndex(NewFI);
364  }
365 
366  // The MachineMemOperands have already been updated.
367 }
368 
369 
370 /// RemoveDeadStores - Scan through a basic block and look for loads followed
371 /// by stores. If they're both using the same stack slot, then the store is
372 /// definitely dead. This could obviously be much more aggressive (consider
373 /// pairs with instructions between them), but such extensions might have a
374 /// considerable compile time impact.
375 bool StackSlotColoring::RemoveDeadStores(MachineBasicBlock* MBB) {
376  // FIXME: This could be much more aggressive, but we need to investigate
377  // the compile time impact of doing so.
378  bool changed = false;
379 
381 
382  for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
383  I != E; ++I) {
384  if (DCELimit != -1 && (int)NumDead >= DCELimit)
385  break;
386 
387  int FirstSS, SecondSS;
388  if (TII->isStackSlotCopy(I, FirstSS, SecondSS) &&
389  FirstSS == SecondSS &&
390  FirstSS != -1) {
391  ++NumDead;
392  changed = true;
393  toErase.push_back(I);
394  continue;
395  }
396 
397  MachineBasicBlock::iterator NextMI = std::next(I);
398  if (NextMI == MBB->end()) continue;
399 
400  unsigned LoadReg = 0;
401  unsigned StoreReg = 0;
402  if (!(LoadReg = TII->isLoadFromStackSlot(I, FirstSS))) continue;
403  if (!(StoreReg = TII->isStoreToStackSlot(NextMI, SecondSS))) continue;
404  if (FirstSS != SecondSS || LoadReg != StoreReg || FirstSS == -1) continue;
405 
406  ++NumDead;
407  changed = true;
408 
409  if (NextMI->findRegisterUseOperandIdx(LoadReg, true, nullptr) != -1) {
410  ++NumDead;
411  toErase.push_back(I);
412  }
413 
414  toErase.push_back(NextMI);
415  ++I;
416  }
417 
419  E = toErase.end(); I != E; ++I)
420  (*I)->eraseFromParent();
421 
422  return changed;
423 }
424 
425 
426 bool StackSlotColoring::runOnMachineFunction(MachineFunction &MF) {
427  DEBUG({
428  dbgs() << "********** Stack Slot Coloring **********\n"
429  << "********** Function: " << MF.getName() << '\n';
430  });
431 
432  MFI = MF.getFrameInfo();
433  TII = MF.getSubtarget().getInstrInfo();
434  LS = &getAnalysis<LiveStacks>();
435  MBFI = &getAnalysis<MachineBlockFrequencyInfo>();
436 
437  bool Changed = false;
438 
439  unsigned NumSlots = LS->getNumIntervals();
440  if (NumSlots == 0)
441  // Nothing to do!
442  return false;
443 
444  // If there are calls to setjmp or sigsetjmp, don't perform stack slot
445  // coloring. The stack could be modified before the longjmp is executed,
446  // resulting in the wrong value being used afterwards. (See
447  // <rdar://problem/8007500>.)
448  if (MF.exposesReturnsTwice())
449  return false;
450 
451  // Gather spill slot references
452  ScanForSpillSlotRefs(MF);
453  InitializeSlots();
454  Changed = ColorSlots(MF);
455 
456  NextColor = -1;
457  SSIntervals.clear();
458  for (unsigned i = 0, e = SSRefs.size(); i != e; ++i)
459  SSRefs[i].clear();
460  SSRefs.clear();
461  OrigAlignments.clear();
462  OrigSizes.clear();
463  AllColors.clear();
464  UsedColors.clear();
465  for (unsigned i = 0, e = Assignments.size(); i != e; ++i)
466  Assignments[i].clear();
467  Assignments.clear();
468 
469  return Changed;
470 }
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
const unsigned reg
Definition: LiveInterval.h:616
STATISTIC(NumFunctions,"Total number of functions")
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:588
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
void reserve(size_type N)
Definition: SmallVector.h:401
static int stackSlot2Index(unsigned Reg)
stackSlot2Index - Compute the frame index from a register value representing a stack slot...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
MachineMemOperand - A description of a memory reference used in the backend.
unsigned isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const override
isLoadFromStackSlot - If the specified machine instruction is a direct load from a stack slot...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
void setIndex(int Idx)
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
static const PseudoSourceValue * getFixedStack(int FI)
A pseudo source value referencing a fixed stack frame entry, e.g., a spill slot.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
Definition: MachineInstr.h:271
SlotIndexes pass.
Definition: SlotIndexes.h:334
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
char & StackSlotColoringID
StackSlotColoring - This pass performs stack slot coloring.
AnalysisUsage & addPreservedID(const void *ID)
TargetInstrInfo - Interface to description of machine instruction set.
bool isDebugValue() const
Definition: MachineInstr.h:748
mmo_iterator memoperands_end() const
Definition: MachineInstr.h:341
void initializeStackSlotColoringPass(PassRegistry &)
bundle_iterator< MachineInstr, instr_iterator > iterator
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:325
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:273
static cl::opt< int > DCELimit("ssc-dce-limit", cl::init(-1), cl::Hidden)
bool overlaps(const LiveRange &other) const
overlaps - Return true if the intersection of the two live ranges is not empty.
Definition: LiveInterval.h:419
Represent the analysis usage information of a pass.
static float getSpillWeight(bool isDef, bool isUse, const MachineBlockFrequencyInfo *MBFI, const MachineInstr *Instr)
const PseudoSourceValue * getPseudoValue() const
stack slot Stack Slot false
static cl::opt< bool > DisableSharing("no-stack-slot-sharing", cl::init(false), cl::Hidden, cl::desc("Suppress slot sharing during stack coloring"))
Color
A "color", which is either even or odd.
bool operator()(LiveInterval *LHS, LiveInterval *RHS) const
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
SI Fix CF Live Intervals
MachineFrameInfo * getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
PseudoSourceValue - Special value supplied for machine level alias analysis.
stack slot coloring
This struct contains the mapping from the slot numbers to unnamed metadata nodes and global values...
Definition: SlotMapping.h:27
Representation of each machine instruction.
Definition: MachineInstr.h:51
stack slot Stack Slot Coloring
bool exposesReturnsTwice() const
exposesReturnsTwice - Returns true if the function calls setjmp or any other similar functions with a...
unsigned isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const override
isStoreToStackSlot - If the specified machine instruction is a direct store to a stack slot...
#define I(x, y, z)
Definition: MD5.cpp:54
INITIALIZE_PASS_BEGIN(StackSlotColoring,"stack-slot-coloring","Stack Slot Coloring", false, false) INITIALIZE_PASS_END(StackSlotColoring
safe stack
Definition: SafeStack.cpp:606
virtual const TargetInstrInfo * getInstrInfo() const
BasicBlockListType::iterator iterator
#define DEBUG(X)
Definition: Debug.h:92
FixedStackPseudoSourceValue - A specialized PseudoSourceValue for holding FixedStack values...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
void dump() const
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
Definition: MachineInstr.h:340