1 //===-- StackColoring.cpp -------------------------------------------------===//
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 pass implements the stack-coloring optimization that looks for
11 // lifetime markers machine instructions (LIFESTART_BEGIN and LIFESTART_END),
12 // which represent the possible lifetime of stack slots. It attempts to
13 // merge disjoint stack slots and reduce the used stack space.
14 // NOTE: This pass is not StackSlotColoring, which optimizes spill slots.
16 // TODO: In the future we plan to improve stack coloring in the following ways:
17 // 1. Allow merging multiple small slots into a single larger slot at different
19 // 2. Merge this pass with StackSlotColoring and allow merging of allocas with
22 //===----------------------------------------------------------------------===//
24 #define DEBUG_TYPE "stackcoloring"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/ADT/BitVector.h"
27 #include "llvm/ADT/DepthFirstIterator.h"
28 #include "llvm/ADT/PostOrderIterator.h"
29 #include "llvm/ADT/SetVector.h"
30 #include "llvm/ADT/SmallPtrSet.h"
31 #include "llvm/ADT/SparseSet.h"
32 #include "llvm/ADT/Statistic.h"
33 #include "llvm/Analysis/Dominators.h"
34 #include "llvm/Analysis/ValueTracking.h"
35 #include "llvm/CodeGen/LiveInterval.h"
36 #include "llvm/CodeGen/MachineBasicBlock.h"
37 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
38 #include "llvm/CodeGen/MachineDominators.h"
39 #include "llvm/CodeGen/MachineFrameInfo.h"
40 #include "llvm/CodeGen/MachineFunctionPass.h"
41 #include "llvm/CodeGen/MachineLoopInfo.h"
42 #include "llvm/CodeGen/MachineMemOperand.h"
43 #include "llvm/CodeGen/MachineModuleInfo.h"
44 #include "llvm/CodeGen/MachineRegisterInfo.h"
45 #include "llvm/CodeGen/PseudoSourceValue.h"
46 #include "llvm/CodeGen/SlotIndexes.h"
47 #include "llvm/DebugInfo.h"
48 #include "llvm/IR/Function.h"
49 #include "llvm/IR/Instructions.h"
50 #include "llvm/IR/Module.h"
51 #include "llvm/MC/MCInstrItineraries.h"
52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/Debug.h"
54 #include "llvm/Support/raw_ostream.h"
55 #include "llvm/Target/TargetInstrInfo.h"
56 #include "llvm/Target/TargetRegisterInfo.h"
61 DisableColoring("no-stack-coloring",
62 cl::init(false), cl::Hidden,
63 cl::desc("Disable stack coloring"));
65 /// The user may write code that uses allocas outside of the declared lifetime
66 /// zone. This can happen when the user returns a reference to a local
67 /// data-structure. We can detect these cases and decide not to optimize the
68 /// code. If this flag is enabled, we try to save the user.
70 ProtectFromEscapedAllocas("protect-from-escaped-allocas",
71 cl::init(false), cl::Hidden,
72 cl::desc("Do not optimize lifetime zones that "
75 STATISTIC(NumMarkerSeen, "Number of lifetime markers found.");
76 STATISTIC(StackSpaceSaved, "Number of bytes saved due to merging slots.");
77 STATISTIC(StackSlotMerged, "Number of stack slot merged.");
78 STATISTIC(EscapedAllocas, "Number of allocas that escaped the lifetime region");
80 //===----------------------------------------------------------------------===//
82 //===----------------------------------------------------------------------===//
85 /// StackColoring - A machine pass for merging disjoint stack allocations,
86 /// marked by the LIFETIME_START and LIFETIME_END pseudo instructions.
87 class StackColoring : public MachineFunctionPass {
88 MachineFrameInfo *MFI;
91 /// A class representing liveness information for a single basic block.
92 /// Each bit in the BitVector represents the liveness property
93 /// for a different stack slot.
94 struct BlockLifetimeInfo {
95 /// Which slots BEGINs in each basic block.
97 /// Which slots ENDs in each basic block.
99 /// Which slots are marked as LIVE_IN, coming into each basic block.
101 /// Which slots are marked as LIVE_OUT, coming out of each basic block.
105 /// Maps active slots (per bit) for each basic block.
106 typedef DenseMap<const MachineBasicBlock*, BlockLifetimeInfo> LivenessMap;
107 LivenessMap BlockLiveness;
109 /// Maps serial numbers to basic blocks.
110 DenseMap<const MachineBasicBlock*, int> BasicBlocks;
111 /// Maps basic blocks to a serial number.
112 SmallVector<const MachineBasicBlock*, 8> BasicBlockNumbering;
114 /// Maps liveness intervals for each slot.
115 SmallVector<LiveInterval*, 16> Intervals;
116 /// VNInfo is used for the construction of LiveIntervals.
117 VNInfo::Allocator VNInfoAllocator;
118 /// SlotIndex analysis object.
119 SlotIndexes *Indexes;
121 /// The list of lifetime markers found. These markers are to be removed
122 /// once the coloring is done.
123 SmallVector<MachineInstr*, 8> Markers;
125 /// SlotSizeSorter - A Sort utility for arranging stack slots according
127 struct SlotSizeSorter {
128 MachineFrameInfo *MFI;
129 SlotSizeSorter(MachineFrameInfo *mfi) : MFI(mfi) { }
130 bool operator()(int LHS, int RHS) {
131 // We use -1 to denote a uninteresting slot. Place these slots at the end.
132 if (LHS == -1) return false;
133 if (RHS == -1) return true;
134 // Sort according to size.
135 return MFI->getObjectSize(LHS) > MFI->getObjectSize(RHS);
141 StackColoring() : MachineFunctionPass(ID) {
142 initializeStackColoringPass(*PassRegistry::getPassRegistry());
144 void getAnalysisUsage(AnalysisUsage &AU) const;
145 bool runOnMachineFunction(MachineFunction &MF);
151 /// Removes all of the lifetime marker instructions from the function.
152 /// \returns true if any markers were removed.
153 bool removeAllMarkers();
155 /// Scan the machine function and find all of the lifetime markers.
156 /// Record the findings in the BEGIN and END vectors.
157 /// \returns the number of markers found.
158 unsigned collectMarkers(unsigned NumSlot);
160 /// Perform the dataflow calculation and calculate the lifetime for each of
161 /// the slots, based on the BEGIN/END vectors. Set the LifetimeLIVE_IN and
162 /// LifetimeLIVE_OUT maps that represent which stack slots are live coming
163 /// in and out blocks.
164 void calculateLocalLiveness();
166 /// Construct the LiveIntervals for the slots.
167 void calculateLiveIntervals(unsigned NumSlots);
169 /// Go over the machine function and change instructions which use stack
170 /// slots to use the joint slots.
171 void remapInstructions(DenseMap<int, int> &SlotRemap);
173 /// The input program may contain instructions which are not inside lifetime
174 /// markers. This can happen due to a bug in the compiler or due to a bug in
175 /// user code (for example, returning a reference to a local variable).
176 /// This procedure checks all of the instructions in the function and
177 /// invalidates lifetime ranges which do not contain all of the instructions
178 /// which access that frame slot.
179 void removeInvalidSlotRanges();
181 /// Map entries which point to other entries to their destination.
182 /// A->B->C becomes A->C.
183 void expungeSlotMap(DenseMap<int, int> &SlotRemap, unsigned NumSlots);
185 } // end anonymous namespace
187 char StackColoring::ID = 0;
188 char &llvm::StackColoringID = StackColoring::ID;
190 INITIALIZE_PASS_BEGIN(StackColoring,
191 "stack-coloring", "Merge disjoint stack slots", false, false)
192 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
193 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
194 INITIALIZE_PASS_END(StackColoring,
195 "stack-coloring", "Merge disjoint stack slots", false, false)
197 void StackColoring::getAnalysisUsage(AnalysisUsage &AU) const {
198 AU.addRequired<MachineDominatorTree>();
199 AU.addPreserved<MachineDominatorTree>();
200 AU.addRequired<SlotIndexes>();
201 MachineFunctionPass::getAnalysisUsage(AU);
204 void StackColoring::dump() const {
205 for (df_iterator<MachineFunction*> FI = df_begin(MF), FE = df_end(MF);
207 DEBUG(dbgs()<<"Inspecting block #"<<BasicBlocks.lookup(*FI)<<
208 " ["<<FI->getName()<<"]\n");
210 LivenessMap::const_iterator BI = BlockLiveness.find(*FI);
211 assert(BI != BlockLiveness.end() && "Block not found");
212 const BlockLifetimeInfo &BlockInfo = BI->second;
214 DEBUG(dbgs()<<"BEGIN : {");
215 for (unsigned i=0; i < BlockInfo.Begin.size(); ++i)
216 DEBUG(dbgs()<<BlockInfo.Begin.test(i)<<" ");
217 DEBUG(dbgs()<<"}\n");
219 DEBUG(dbgs()<<"END : {");
220 for (unsigned i=0; i < BlockInfo.End.size(); ++i)
221 DEBUG(dbgs()<<BlockInfo.End.test(i)<<" ");
223 DEBUG(dbgs()<<"}\n");
225 DEBUG(dbgs()<<"LIVE_IN: {");
226 for (unsigned i=0; i < BlockInfo.LiveIn.size(); ++i)
227 DEBUG(dbgs()<<BlockInfo.LiveIn.test(i)<<" ");
229 DEBUG(dbgs()<<"}\n");
230 DEBUG(dbgs()<<"LIVEOUT: {");
231 for (unsigned i=0; i < BlockInfo.LiveOut.size(); ++i)
232 DEBUG(dbgs()<<BlockInfo.LiveOut.test(i)<<" ");
233 DEBUG(dbgs()<<"}\n");
237 unsigned StackColoring::collectMarkers(unsigned NumSlot) {
238 unsigned MarkersFound = 0;
239 // Scan the function to find all lifetime markers.
240 // NOTE: We use the a reverse-post-order iteration to ensure that we obtain a
241 // deterministic numbering, and because we'll need a post-order iteration
242 // later for solving the liveness dataflow problem.
243 for (df_iterator<MachineFunction*> FI = df_begin(MF), FE = df_end(MF);
246 // Assign a serial number to this basic block.
247 BasicBlocks[*FI] = BasicBlockNumbering.size();
248 BasicBlockNumbering.push_back(*FI);
250 // Keep a reference to avoid repeated lookups.
251 BlockLifetimeInfo &BlockInfo = BlockLiveness[*FI];
253 BlockInfo.Begin.resize(NumSlot);
254 BlockInfo.End.resize(NumSlot);
256 for (MachineBasicBlock::iterator BI = (*FI)->begin(), BE = (*FI)->end();
259 if (BI->getOpcode() != TargetOpcode::LIFETIME_START &&
260 BI->getOpcode() != TargetOpcode::LIFETIME_END)
263 Markers.push_back(BI);
265 bool IsStart = BI->getOpcode() == TargetOpcode::LIFETIME_START;
266 const MachineOperand &MI = BI->getOperand(0);
267 unsigned Slot = MI.getIndex();
271 const AllocaInst *Allocation = MFI->getObjectAllocation(Slot);
273 DEBUG(dbgs()<<"Found a lifetime marker for slot #"<<Slot<<
274 " with allocation: "<< Allocation->getName()<<"\n");
278 BlockInfo.Begin.set(Slot);
280 if (BlockInfo.Begin.test(Slot)) {
281 // Allocas that start and end within a single block are handled
282 // specially when computing the LiveIntervals to avoid pessimizing
283 // the liveness propagation.
284 BlockInfo.Begin.reset(Slot);
286 BlockInfo.End.set(Slot);
292 // Update statistics.
293 NumMarkerSeen += MarkersFound;
297 void StackColoring::calculateLocalLiveness() {
298 // Perform a standard reverse dataflow computation to solve for
299 // global liveness. The BEGIN set here is equivalent to KILL in the standard
300 // formulation, and END is equivalent to GEN. The result of this computation
301 // is a map from blocks to bitvectors where the bitvectors represent which
302 // allocas are live in/out of that block.
303 SmallPtrSet<const MachineBasicBlock*, 8> BBSet(BasicBlockNumbering.begin(),
304 BasicBlockNumbering.end());
305 unsigned NumSSMIters = 0;
311 SmallPtrSet<const MachineBasicBlock*, 8> NextBBSet;
313 for (SmallVectorImpl<const MachineBasicBlock *>::iterator
314 PI = BasicBlockNumbering.begin(), PE = BasicBlockNumbering.end();
317 const MachineBasicBlock *BB = *PI;
318 if (!BBSet.count(BB)) continue;
320 // Use an iterator to avoid repeated lookups.
321 LivenessMap::iterator BI = BlockLiveness.find(BB);
322 assert(BI != BlockLiveness.end() && "Block not found");
323 BlockLifetimeInfo &BlockInfo = BI->second;
325 BitVector LocalLiveIn;
326 BitVector LocalLiveOut;
328 // Forward propagation from begins to ends.
329 for (MachineBasicBlock::const_pred_iterator PI = BB->pred_begin(),
330 PE = BB->pred_end(); PI != PE; ++PI) {
331 LivenessMap::const_iterator I = BlockLiveness.find(*PI);
332 assert(I != BlockLiveness.end() && "Predecessor not found");
333 LocalLiveIn |= I->second.LiveOut;
335 LocalLiveIn |= BlockInfo.End;
336 LocalLiveIn.reset(BlockInfo.Begin);
338 // Reverse propagation from ends to begins.
339 for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
340 SE = BB->succ_end(); SI != SE; ++SI) {
341 LivenessMap::const_iterator I = BlockLiveness.find(*SI);
342 assert(I != BlockLiveness.end() && "Successor not found");
343 LocalLiveOut |= I->second.LiveIn;
345 LocalLiveOut |= BlockInfo.Begin;
346 LocalLiveOut.reset(BlockInfo.End);
348 LocalLiveIn |= LocalLiveOut;
349 LocalLiveOut |= LocalLiveIn;
351 // After adopting the live bits, we need to turn-off the bits which
352 // are de-activated in this block.
353 LocalLiveOut.reset(BlockInfo.End);
354 LocalLiveIn.reset(BlockInfo.Begin);
356 // If we have both BEGIN and END markers in the same basic block then
357 // we know that the BEGIN marker comes after the END, because we already
358 // handle the case where the BEGIN comes before the END when collecting
359 // the markers (and building the BEGIN/END vectore).
360 // Want to enable the LIVE_IN and LIVE_OUT of slots that have both
361 // BEGIN and END because it means that the value lives before and after
363 BitVector LocalEndBegin = BlockInfo.End;
364 LocalEndBegin &= BlockInfo.Begin;
365 LocalLiveIn |= LocalEndBegin;
366 LocalLiveOut |= LocalEndBegin;
368 if (LocalLiveIn.test(BlockInfo.LiveIn)) {
370 BlockInfo.LiveIn |= LocalLiveIn;
372 for (MachineBasicBlock::const_pred_iterator PI = BB->pred_begin(),
373 PE = BB->pred_end(); PI != PE; ++PI)
374 NextBBSet.insert(*PI);
377 if (LocalLiveOut.test(BlockInfo.LiveOut)) {
379 BlockInfo.LiveOut |= LocalLiveOut;
381 for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
382 SE = BB->succ_end(); SI != SE; ++SI)
383 NextBBSet.insert(*SI);
391 void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
392 SmallVector<SlotIndex, 16> Starts;
393 SmallVector<SlotIndex, 16> Finishes;
395 // For each block, find which slots are active within this block
396 // and update the live intervals.
397 for (MachineFunction::iterator MBB = MF->begin(), MBBe = MF->end();
398 MBB != MBBe; ++MBB) {
400 Starts.resize(NumSlots);
402 Finishes.resize(NumSlots);
404 // Create the interval for the basic blocks with lifetime markers in them.
405 for (SmallVectorImpl<MachineInstr*>::const_iterator it = Markers.begin(),
406 e = Markers.end(); it != e; ++it) {
407 const MachineInstr *MI = *it;
408 if (MI->getParent() != MBB)
411 assert((MI->getOpcode() == TargetOpcode::LIFETIME_START ||
412 MI->getOpcode() == TargetOpcode::LIFETIME_END) &&
413 "Invalid Lifetime marker");
415 bool IsStart = MI->getOpcode() == TargetOpcode::LIFETIME_START;
416 const MachineOperand &Mo = MI->getOperand(0);
417 int Slot = Mo.getIndex();
418 assert(Slot >= 0 && "Invalid slot");
420 SlotIndex ThisIndex = Indexes->getInstructionIndex(MI);
423 if (!Starts[Slot].isValid() || Starts[Slot] > ThisIndex)
424 Starts[Slot] = ThisIndex;
426 if (!Finishes[Slot].isValid() || Finishes[Slot] < ThisIndex)
427 Finishes[Slot] = ThisIndex;
431 // Create the interval of the blocks that we previously found to be 'alive'.
432 BlockLifetimeInfo &MBBLiveness = BlockLiveness[MBB];
433 for (int pos = MBBLiveness.LiveIn.find_first(); pos != -1;
434 pos = MBBLiveness.LiveIn.find_next(pos)) {
435 Starts[pos] = Indexes->getMBBStartIdx(MBB);
437 for (int pos = MBBLiveness.LiveOut.find_first(); pos != -1;
438 pos = MBBLiveness.LiveOut.find_next(pos)) {
439 Finishes[pos] = Indexes->getMBBEndIdx(MBB);
442 for (unsigned i = 0; i < NumSlots; ++i) {
443 assert(Starts[i].isValid() == Finishes[i].isValid() && "Unmatched range");
444 if (!Starts[i].isValid())
447 assert(Starts[i] && Finishes[i] && "Invalid interval");
448 VNInfo *ValNum = Intervals[i]->getValNumInfo(0);
449 SlotIndex S = Starts[i];
450 SlotIndex F = Finishes[i];
452 // We have a single consecutive region.
453 Intervals[i]->addSegment(LiveInterval::Segment(S, F, ValNum));
455 // We have two non-consecutive regions. This happens when
456 // LIFETIME_START appears after the LIFETIME_END marker.
457 SlotIndex NewStart = Indexes->getMBBStartIdx(MBB);
458 SlotIndex NewFin = Indexes->getMBBEndIdx(MBB);
459 Intervals[i]->addSegment(LiveInterval::Segment(NewStart, F, ValNum));
460 Intervals[i]->addSegment(LiveInterval::Segment(S, NewFin, ValNum));
466 bool StackColoring::removeAllMarkers() {
468 for (unsigned i = 0; i < Markers.size(); ++i) {
469 Markers[i]->eraseFromParent();
474 DEBUG(dbgs()<<"Removed "<<Count<<" markers.\n");
478 void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
479 unsigned FixedInstr = 0;
480 unsigned FixedMemOp = 0;
481 unsigned FixedDbg = 0;
482 MachineModuleInfo *MMI = &MF->getMMI();
484 // Remap debug information that refers to stack slots.
485 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
486 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
487 VE = VMap.end(); VI != VE; ++VI) {
488 const MDNode *Var = VI->first;
490 std::pair<unsigned, DebugLoc> &VP = VI->second;
491 if (SlotRemap.count(VP.first)) {
492 DEBUG(dbgs()<<"Remapping debug info for ["<<Var->getName()<<"].\n");
493 VP.first = SlotRemap[VP.first];
498 // Keep a list of *allocas* which need to be remapped.
499 DenseMap<const AllocaInst*, const AllocaInst*> Allocas;
500 for (DenseMap<int, int>::const_iterator it = SlotRemap.begin(),
501 e = SlotRemap.end(); it != e; ++it) {
502 const AllocaInst *From = MFI->getObjectAllocation(it->first);
503 const AllocaInst *To = MFI->getObjectAllocation(it->second);
504 assert(To && From && "Invalid allocation object");
508 // Remap all instructions to the new stack slots.
509 MachineFunction::iterator BB, BBE;
510 MachineBasicBlock::iterator I, IE;
511 for (BB = MF->begin(), BBE = MF->end(); BB != BBE; ++BB)
512 for (I = BB->begin(), IE = BB->end(); I != IE; ++I) {
514 // Skip lifetime markers. We'll remove them soon.
515 if (I->getOpcode() == TargetOpcode::LIFETIME_START ||
516 I->getOpcode() == TargetOpcode::LIFETIME_END)
519 // Update the MachineMemOperand to use the new alloca.
520 for (MachineInstr::mmo_iterator MM = I->memoperands_begin(),
521 E = I->memoperands_end(); MM != E; ++MM) {
522 MachineMemOperand *MMO = *MM;
524 const Value *V = MMO->getValue();
529 const PseudoSourceValue *PSV = dyn_cast<const PseudoSourceValue>(V);
530 if (PSV && PSV->isConstant(MFI))
533 // Climb up and find the original alloca.
534 V = GetUnderlyingObject(V);
535 // If we did not find one, or if the one that we found is not in our
536 // map, then move on.
537 if (!V || !isa<AllocaInst>(V)) {
538 // Clear mem operand since we don't know for sure that it doesn't
539 // alias a merged alloca.
543 const AllocaInst *AI= cast<AllocaInst>(V);
544 if (!Allocas.count(AI))
547 MMO->setValue(Allocas[AI]);
551 // Update all of the machine instruction operands.
552 for (unsigned i = 0 ; i < I->getNumOperands(); ++i) {
553 MachineOperand &MO = I->getOperand(i);
557 int FromSlot = MO.getIndex();
559 // Don't touch arguments.
563 // Only look at mapped slots.
564 if (!SlotRemap.count(FromSlot))
567 // In a debug build, check that the instruction that we are modifying is
568 // inside the expected live range. If the instruction is not inside
569 // the calculated range then it means that the alloca usage moved
570 // outside of the lifetime markers, or that the user has a bug.
571 // NOTE: Alloca address calculations which happen outside the lifetime
572 // zone are are okay, despite the fact that we don't have a good way
573 // for validating all of the usages of the calculation.
575 bool TouchesMemory = I->mayLoad() || I->mayStore();
576 // If we *don't* protect the user from escaped allocas, don't bother
577 // validating the instructions.
578 if (!I->isDebugValue() && TouchesMemory && ProtectFromEscapedAllocas) {
579 SlotIndex Index = Indexes->getInstructionIndex(I);
580 LiveInterval *Interval = Intervals[FromSlot];
581 assert(Interval->find(Index) != Interval->end() &&
582 "Found instruction usage outside of live range.");
586 // Fix the machine instructions.
587 int ToSlot = SlotRemap[FromSlot];
593 DEBUG(dbgs()<<"Fixed "<<FixedMemOp<<" machine memory operands.\n");
594 DEBUG(dbgs()<<"Fixed "<<FixedDbg<<" debug locations.\n");
595 DEBUG(dbgs()<<"Fixed "<<FixedInstr<<" machine instructions.\n");
598 void StackColoring::removeInvalidSlotRanges() {
599 MachineFunction::const_iterator BB, BBE;
600 MachineBasicBlock::const_iterator I, IE;
601 for (BB = MF->begin(), BBE = MF->end(); BB != BBE; ++BB)
602 for (I = BB->begin(), IE = BB->end(); I != IE; ++I) {
604 if (I->getOpcode() == TargetOpcode::LIFETIME_START ||
605 I->getOpcode() == TargetOpcode::LIFETIME_END || I->isDebugValue())
608 // Some intervals are suspicious! In some cases we find address
609 // calculations outside of the lifetime zone, but not actual memory
610 // read or write. Memory accesses outside of the lifetime zone are a clear
611 // violation, but address calculations are okay. This can happen when
612 // GEPs are hoisted outside of the lifetime zone.
613 // So, in here we only check instructions which can read or write memory.
614 if (!I->mayLoad() && !I->mayStore())
617 // Check all of the machine operands.
618 for (unsigned i = 0 ; i < I->getNumOperands(); ++i) {
619 const MachineOperand &MO = I->getOperand(i);
624 int Slot = MO.getIndex();
629 if (Intervals[Slot]->empty())
632 // Check that the used slot is inside the calculated lifetime range.
633 // If it is not, warn about it and invalidate the range.
634 LiveInterval *Interval = Intervals[Slot];
635 SlotIndex Index = Indexes->getInstructionIndex(I);
636 if (Interval->find(Index) == Interval->end()) {
637 Intervals[Slot]->clear();
638 DEBUG(dbgs()<<"Invalidating range #"<<Slot<<"\n");
645 void StackColoring::expungeSlotMap(DenseMap<int, int> &SlotRemap,
647 // Expunge slot remap map.
648 for (unsigned i=0; i < NumSlots; ++i) {
649 // If we are remapping i
650 if (SlotRemap.count(i)) {
651 int Target = SlotRemap[i];
652 // As long as our target is mapped to something else, follow it.
653 while (SlotRemap.count(Target)) {
654 Target = SlotRemap[Target];
655 SlotRemap[i] = Target;
661 bool StackColoring::runOnMachineFunction(MachineFunction &Func) {
662 DEBUG(dbgs() << "********** Stack Coloring **********\n"
663 << "********** Function: "
664 << ((const Value*)Func.getFunction())->getName() << '\n');
666 MFI = MF->getFrameInfo();
667 Indexes = &getAnalysis<SlotIndexes>();
668 BlockLiveness.clear();
670 BasicBlockNumbering.clear();
673 VNInfoAllocator.Reset();
675 unsigned NumSlots = MFI->getObjectIndexEnd();
677 // If there are no stack slots then there are no markers to remove.
681 SmallVector<int, 8> SortedSlots;
683 SortedSlots.reserve(NumSlots);
684 Intervals.reserve(NumSlots);
686 unsigned NumMarkers = collectMarkers(NumSlots);
688 unsigned TotalSize = 0;
689 DEBUG(dbgs()<<"Found "<<NumMarkers<<" markers and "<<NumSlots<<" slots\n");
690 DEBUG(dbgs()<<"Slot structure:\n");
692 for (int i=0; i < MFI->getObjectIndexEnd(); ++i) {
693 DEBUG(dbgs()<<"Slot #"<<i<<" - "<<MFI->getObjectSize(i)<<" bytes.\n");
694 TotalSize += MFI->getObjectSize(i);
697 DEBUG(dbgs()<<"Total Stack size: "<<TotalSize<<" bytes\n\n");
699 // Don't continue because there are not enough lifetime markers, or the
700 // stack is too small, or we are told not to optimize the slots.
701 if (NumMarkers < 2 || TotalSize < 16 || DisableColoring) {
702 DEBUG(dbgs()<<"Will not try to merge slots.\n");
703 return removeAllMarkers();
706 for (unsigned i=0; i < NumSlots; ++i) {
707 LiveInterval *LI = new LiveInterval(i, 0);
708 Intervals.push_back(LI);
709 LI->getNextValue(Indexes->getZeroIndex(), VNInfoAllocator);
710 SortedSlots.push_back(i);
713 // Calculate the liveness of each block.
714 calculateLocalLiveness();
716 // Propagate the liveness information.
717 calculateLiveIntervals(NumSlots);
719 // Search for allocas which are used outside of the declared lifetime
721 if (ProtectFromEscapedAllocas)
722 removeInvalidSlotRanges();
724 // Maps old slots to new slots.
725 DenseMap<int, int> SlotRemap;
726 unsigned RemovedSlots = 0;
727 unsigned ReducedSize = 0;
729 // Do not bother looking at empty intervals.
730 for (unsigned I = 0; I < NumSlots; ++I) {
731 if (Intervals[SortedSlots[I]]->empty())
735 // This is a simple greedy algorithm for merging allocas. First, sort the
736 // slots, placing the largest slots first. Next, perform an n^2 scan and look
737 // for disjoint slots. When you find disjoint slots, merge the samller one
738 // into the bigger one and update the live interval. Remove the small alloca
741 // Sort the slots according to their size. Place unused slots at the end.
742 // Use stable sort to guarantee deterministic code generation.
743 std::stable_sort(SortedSlots.begin(), SortedSlots.end(),
744 SlotSizeSorter(MFI));
749 for (unsigned I = 0; I < NumSlots; ++I) {
750 if (SortedSlots[I] == -1)
753 for (unsigned J=I+1; J < NumSlots; ++J) {
754 if (SortedSlots[J] == -1)
757 int FirstSlot = SortedSlots[I];
758 int SecondSlot = SortedSlots[J];
759 LiveInterval *First = Intervals[FirstSlot];
760 LiveInterval *Second = Intervals[SecondSlot];
761 assert (!First->empty() && !Second->empty() && "Found an empty range");
763 // Merge disjoint slots.
764 if (!First->overlaps(*Second)) {
766 First->MergeSegmentsInAsValue(*Second, First->getValNumInfo(0));
767 SlotRemap[SecondSlot] = FirstSlot;
769 DEBUG(dbgs()<<"Merging #"<<FirstSlot<<" and slots #"<<
770 SecondSlot<<" together.\n");
771 unsigned MaxAlignment = std::max(MFI->getObjectAlignment(FirstSlot),
772 MFI->getObjectAlignment(SecondSlot));
774 assert(MFI->getObjectSize(FirstSlot) >=
775 MFI->getObjectSize(SecondSlot) &&
776 "Merging a small object into a larger one");
779 ReducedSize += MFI->getObjectSize(SecondSlot);
780 MFI->setObjectAlignment(FirstSlot, MaxAlignment);
781 MFI->RemoveStackObject(SecondSlot);
787 // Record statistics.
788 StackSpaceSaved += ReducedSize;
789 StackSlotMerged += RemovedSlots;
790 DEBUG(dbgs()<<"Merge "<<RemovedSlots<<" slots. Saved "<<
791 ReducedSize<<" bytes\n");
793 // Scan the entire function and update all machine operands that use frame
794 // indices to use the remapped frame index.
795 expungeSlotMap(SlotRemap, NumSlots);
796 remapInstructions(SlotRemap);
798 // Release the intervals.
799 for (unsigned I = 0; I < NumSlots; ++I) {
803 return removeAllMarkers();