[WinEH] CatchHandler which don't have catch objects in StackColoring
[oota-llvm.git] / lib / CodeGen / StackColoring.cpp
1 //===-- StackColoring.cpp -------------------------------------------------===//
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 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.
15 //
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
18 //    offsets.
19 // 2. Merge this pass with StackSlotColoring and allow merging of allocas with
20 //    spill slots.
21 //
22 //===----------------------------------------------------------------------===//
23
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/ADT/BitVector.h"
26 #include "llvm/ADT/DepthFirstIterator.h"
27 #include "llvm/ADT/PostOrderIterator.h"
28 #include "llvm/ADT/SetVector.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SparseSet.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/Analysis/ValueTracking.h"
33 #include "llvm/CodeGen/LiveInterval.h"
34 #include "llvm/CodeGen/MachineBasicBlock.h"
35 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
36 #include "llvm/CodeGen/MachineDominators.h"
37 #include "llvm/CodeGen/MachineFrameInfo.h"
38 #include "llvm/CodeGen/MachineFunctionPass.h"
39 #include "llvm/CodeGen/MachineLoopInfo.h"
40 #include "llvm/CodeGen/MachineMemOperand.h"
41 #include "llvm/CodeGen/MachineModuleInfo.h"
42 #include "llvm/CodeGen/MachineRegisterInfo.h"
43 #include "llvm/CodeGen/PseudoSourceValue.h"
44 #include "llvm/CodeGen/SlotIndexes.h"
45 #include "llvm/CodeGen/StackProtector.h"
46 #include "llvm/CodeGen/WinEHFuncInfo.h"
47 #include "llvm/IR/DebugInfo.h"
48 #include "llvm/IR/Dominators.h"
49 #include "llvm/IR/Function.h"
50 #include "llvm/IR/Instructions.h"
51 #include "llvm/IR/Module.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"
57
58 using namespace llvm;
59
60 #define DEBUG_TYPE "stackcoloring"
61
62 static cl::opt<bool>
63 DisableColoring("no-stack-coloring",
64         cl::init(false), cl::Hidden,
65         cl::desc("Disable stack coloring"));
66
67 /// The user may write code that uses allocas outside of the declared lifetime
68 /// zone. This can happen when the user returns a reference to a local
69 /// data-structure. We can detect these cases and decide not to optimize the
70 /// code. If this flag is enabled, we try to save the user.
71 static cl::opt<bool>
72 ProtectFromEscapedAllocas("protect-from-escaped-allocas",
73                           cl::init(false), cl::Hidden,
74                           cl::desc("Do not optimize lifetime zones that "
75                                    "are broken"));
76
77 STATISTIC(NumMarkerSeen,  "Number of lifetime markers found.");
78 STATISTIC(StackSpaceSaved, "Number of bytes saved due to merging slots.");
79 STATISTIC(StackSlotMerged, "Number of stack slot merged.");
80 STATISTIC(EscapedAllocas, "Number of allocas that escaped the lifetime region");
81
82 //===----------------------------------------------------------------------===//
83 //                           StackColoring Pass
84 //===----------------------------------------------------------------------===//
85
86 namespace {
87 /// StackColoring - A machine pass for merging disjoint stack allocations,
88 /// marked by the LIFETIME_START and LIFETIME_END pseudo instructions.
89 class StackColoring : public MachineFunctionPass {
90   MachineFrameInfo *MFI;
91   MachineFunction *MF;
92
93   /// A class representing liveness information for a single basic block.
94   /// Each bit in the BitVector represents the liveness property
95   /// for a different stack slot.
96   struct BlockLifetimeInfo {
97     /// Which slots BEGINs in each basic block.
98     BitVector Begin;
99     /// Which slots ENDs in each basic block.
100     BitVector End;
101     /// Which slots are marked as LIVE_IN, coming into each basic block.
102     BitVector LiveIn;
103     /// Which slots are marked as LIVE_OUT, coming out of each basic block.
104     BitVector LiveOut;
105   };
106
107   /// Maps active slots (per bit) for each basic block.
108   typedef DenseMap<const MachineBasicBlock*, BlockLifetimeInfo> LivenessMap;
109   LivenessMap BlockLiveness;
110
111   /// Maps serial numbers to basic blocks.
112   DenseMap<const MachineBasicBlock*, int> BasicBlocks;
113   /// Maps basic blocks to a serial number.
114   SmallVector<const MachineBasicBlock*, 8> BasicBlockNumbering;
115
116   /// Maps liveness intervals for each slot.
117   SmallVector<std::unique_ptr<LiveInterval>, 16> Intervals;
118   /// VNInfo is used for the construction of LiveIntervals.
119   VNInfo::Allocator VNInfoAllocator;
120   /// SlotIndex analysis object.
121   SlotIndexes *Indexes;
122   /// The stack protector object.
123   StackProtector *SP;
124
125   /// The list of lifetime markers found. These markers are to be removed
126   /// once the coloring is done.
127   SmallVector<MachineInstr*, 8> Markers;
128
129 public:
130   static char ID;
131   StackColoring() : MachineFunctionPass(ID) {
132     initializeStackColoringPass(*PassRegistry::getPassRegistry());
133   }
134   void getAnalysisUsage(AnalysisUsage &AU) const override;
135   bool runOnMachineFunction(MachineFunction &MF) override;
136
137 private:
138   /// Debug.
139   void dump() const;
140
141   /// Removes all of the lifetime marker instructions from the function.
142   /// \returns true if any markers were removed.
143   bool removeAllMarkers();
144
145   /// Scan the machine function and find all of the lifetime markers.
146   /// Record the findings in the BEGIN and END vectors.
147   /// \returns the number of markers found.
148   unsigned collectMarkers(unsigned NumSlot);
149
150   /// Perform the dataflow calculation and calculate the lifetime for each of
151   /// the slots, based on the BEGIN/END vectors. Set the LifetimeLIVE_IN and
152   /// LifetimeLIVE_OUT maps that represent which stack slots are live coming
153   /// in and out blocks.
154   void calculateLocalLiveness();
155
156   /// Construct the LiveIntervals for the slots.
157   void calculateLiveIntervals(unsigned NumSlots);
158
159   /// Go over the machine function and change instructions which use stack
160   /// slots to use the joint slots.
161   void remapInstructions(DenseMap<int, int> &SlotRemap);
162
163   /// The input program may contain instructions which are not inside lifetime
164   /// markers. This can happen due to a bug in the compiler or due to a bug in
165   /// user code (for example, returning a reference to a local variable).
166   /// This procedure checks all of the instructions in the function and
167   /// invalidates lifetime ranges which do not contain all of the instructions
168   /// which access that frame slot.
169   void removeInvalidSlotRanges();
170
171   /// Map entries which point to other entries to their destination.
172   ///   A->B->C becomes A->C.
173    void expungeSlotMap(DenseMap<int, int> &SlotRemap, unsigned NumSlots);
174 };
175 } // end anonymous namespace
176
177 char StackColoring::ID = 0;
178 char &llvm::StackColoringID = StackColoring::ID;
179
180 INITIALIZE_PASS_BEGIN(StackColoring,
181                    "stack-coloring", "Merge disjoint stack slots", false, false)
182 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
183 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
184 INITIALIZE_PASS_DEPENDENCY(StackProtector)
185 INITIALIZE_PASS_END(StackColoring,
186                    "stack-coloring", "Merge disjoint stack slots", false, false)
187
188 void StackColoring::getAnalysisUsage(AnalysisUsage &AU) const {
189   AU.addRequired<MachineDominatorTree>();
190   AU.addPreserved<MachineDominatorTree>();
191   AU.addRequired<SlotIndexes>();
192   AU.addRequired<StackProtector>();
193   MachineFunctionPass::getAnalysisUsage(AU);
194 }
195
196 void StackColoring::dump() const {
197   for (MachineBasicBlock *MBB : depth_first(MF)) {
198     DEBUG(dbgs() << "Inspecting block #" << BasicBlocks.lookup(MBB) << " ["
199                  << MBB->getName() << "]\n");
200
201     LivenessMap::const_iterator BI = BlockLiveness.find(MBB);
202     assert(BI != BlockLiveness.end() && "Block not found");
203     const BlockLifetimeInfo &BlockInfo = BI->second;
204
205     DEBUG(dbgs()<<"BEGIN  : {");
206     for (unsigned i=0; i < BlockInfo.Begin.size(); ++i)
207       DEBUG(dbgs()<<BlockInfo.Begin.test(i)<<" ");
208     DEBUG(dbgs()<<"}\n");
209
210     DEBUG(dbgs()<<"END    : {");
211     for (unsigned i=0; i < BlockInfo.End.size(); ++i)
212       DEBUG(dbgs()<<BlockInfo.End.test(i)<<" ");
213
214     DEBUG(dbgs()<<"}\n");
215
216     DEBUG(dbgs()<<"LIVE_IN: {");
217     for (unsigned i=0; i < BlockInfo.LiveIn.size(); ++i)
218       DEBUG(dbgs()<<BlockInfo.LiveIn.test(i)<<" ");
219
220     DEBUG(dbgs()<<"}\n");
221     DEBUG(dbgs()<<"LIVEOUT: {");
222     for (unsigned i=0; i < BlockInfo.LiveOut.size(); ++i)
223       DEBUG(dbgs()<<BlockInfo.LiveOut.test(i)<<" ");
224     DEBUG(dbgs()<<"}\n");
225   }
226 }
227
228 unsigned StackColoring::collectMarkers(unsigned NumSlot) {
229   unsigned MarkersFound = 0;
230   // Scan the function to find all lifetime markers.
231   // NOTE: We use a reverse-post-order iteration to ensure that we obtain a
232   // deterministic numbering, and because we'll need a post-order iteration
233   // later for solving the liveness dataflow problem.
234   for (MachineBasicBlock *MBB : depth_first(MF)) {
235
236     // Assign a serial number to this basic block.
237     BasicBlocks[MBB] = BasicBlockNumbering.size();
238     BasicBlockNumbering.push_back(MBB);
239
240     // Keep a reference to avoid repeated lookups.
241     BlockLifetimeInfo &BlockInfo = BlockLiveness[MBB];
242
243     BlockInfo.Begin.resize(NumSlot);
244     BlockInfo.End.resize(NumSlot);
245
246     for (MachineInstr &MI : *MBB) {
247       if (MI.getOpcode() != TargetOpcode::LIFETIME_START &&
248           MI.getOpcode() != TargetOpcode::LIFETIME_END)
249         continue;
250
251       Markers.push_back(&MI);
252
253       bool IsStart = MI.getOpcode() == TargetOpcode::LIFETIME_START;
254       const MachineOperand &MO = MI.getOperand(0);
255       unsigned Slot = MO.getIndex();
256
257       MarkersFound++;
258
259       const AllocaInst *Allocation = MFI->getObjectAllocation(Slot);
260       if (Allocation) {
261         DEBUG(dbgs()<<"Found a lifetime marker for slot #"<<Slot<<
262               " with allocation: "<< Allocation->getName()<<"\n");
263       }
264
265       if (IsStart) {
266         BlockInfo.Begin.set(Slot);
267       } else {
268         if (BlockInfo.Begin.test(Slot)) {
269           // Allocas that start and end within a single block are handled
270           // specially when computing the LiveIntervals to avoid pessimizing
271           // the liveness propagation.
272           BlockInfo.Begin.reset(Slot);
273         } else {
274           BlockInfo.End.set(Slot);
275         }
276       }
277     }
278   }
279
280   // Update statistics.
281   NumMarkerSeen += MarkersFound;
282   return MarkersFound;
283 }
284
285 void StackColoring::calculateLocalLiveness() {
286   // Perform a standard reverse dataflow computation to solve for
287   // global liveness.  The BEGIN set here is equivalent to KILL in the standard
288   // formulation, and END is equivalent to GEN.  The result of this computation
289   // is a map from blocks to bitvectors where the bitvectors represent which
290   // allocas are live in/out of that block.
291   SmallPtrSet<const MachineBasicBlock*, 8> BBSet(BasicBlockNumbering.begin(),
292                                                  BasicBlockNumbering.end());
293   unsigned NumSSMIters = 0;
294   bool changed = true;
295   while (changed) {
296     changed = false;
297     ++NumSSMIters;
298
299     SmallPtrSet<const MachineBasicBlock*, 8> NextBBSet;
300
301     for (const MachineBasicBlock *BB : BasicBlockNumbering) {
302       if (!BBSet.count(BB)) continue;
303
304       // Use an iterator to avoid repeated lookups.
305       LivenessMap::iterator BI = BlockLiveness.find(BB);
306       assert(BI != BlockLiveness.end() && "Block not found");
307       BlockLifetimeInfo &BlockInfo = BI->second;
308
309       BitVector LocalLiveIn;
310       BitVector LocalLiveOut;
311
312       // Forward propagation from begins to ends.
313       for (MachineBasicBlock::const_pred_iterator PI = BB->pred_begin(),
314            PE = BB->pred_end(); PI != PE; ++PI) {
315         LivenessMap::const_iterator I = BlockLiveness.find(*PI);
316         assert(I != BlockLiveness.end() && "Predecessor not found");
317         LocalLiveIn |= I->second.LiveOut;
318       }
319       LocalLiveIn |= BlockInfo.End;
320       LocalLiveIn.reset(BlockInfo.Begin);
321
322       // Reverse propagation from ends to begins.
323       for (MachineBasicBlock::const_succ_iterator SI = BB->succ_begin(),
324            SE = BB->succ_end(); SI != SE; ++SI) {
325         LivenessMap::const_iterator I = BlockLiveness.find(*SI);
326         assert(I != BlockLiveness.end() && "Successor not found");
327         LocalLiveOut |= I->second.LiveIn;
328       }
329       LocalLiveOut |= BlockInfo.Begin;
330       LocalLiveOut.reset(BlockInfo.End);
331
332       LocalLiveIn |= LocalLiveOut;
333       LocalLiveOut |= LocalLiveIn;
334
335       // After adopting the live bits, we need to turn-off the bits which
336       // are de-activated in this block.
337       LocalLiveOut.reset(BlockInfo.End);
338       LocalLiveIn.reset(BlockInfo.Begin);
339
340       // If we have both BEGIN and END markers in the same basic block then
341       // we know that the BEGIN marker comes after the END, because we already
342       // handle the case where the BEGIN comes before the END when collecting
343       // the markers (and building the BEGIN/END vectore).
344       // Want to enable the LIVE_IN and LIVE_OUT of slots that have both
345       // BEGIN and END because it means that the value lives before and after
346       // this basic block.
347       BitVector LocalEndBegin = BlockInfo.End;
348       LocalEndBegin &= BlockInfo.Begin;
349       LocalLiveIn |= LocalEndBegin;
350       LocalLiveOut |= LocalEndBegin;
351
352       if (LocalLiveIn.test(BlockInfo.LiveIn)) {
353         changed = true;
354         BlockInfo.LiveIn |= LocalLiveIn;
355
356         NextBBSet.insert(BB->pred_begin(), BB->pred_end());
357       }
358
359       if (LocalLiveOut.test(BlockInfo.LiveOut)) {
360         changed = true;
361         BlockInfo.LiveOut |= LocalLiveOut;
362
363         NextBBSet.insert(BB->succ_begin(), BB->succ_end());
364       }
365     }
366
367     BBSet = std::move(NextBBSet);
368   }// while changed.
369 }
370
371 void StackColoring::calculateLiveIntervals(unsigned NumSlots) {
372   SmallVector<SlotIndex, 16> Starts;
373   SmallVector<SlotIndex, 16> Finishes;
374
375   // For each block, find which slots are active within this block
376   // and update the live intervals.
377   for (const MachineBasicBlock &MBB : *MF) {
378     Starts.clear();
379     Starts.resize(NumSlots);
380     Finishes.clear();
381     Finishes.resize(NumSlots);
382
383     // Create the interval for the basic blocks with lifetime markers in them.
384     for (const MachineInstr *MI : Markers) {
385       if (MI->getParent() != &MBB)
386         continue;
387
388       assert((MI->getOpcode() == TargetOpcode::LIFETIME_START ||
389               MI->getOpcode() == TargetOpcode::LIFETIME_END) &&
390              "Invalid Lifetime marker");
391
392       bool IsStart = MI->getOpcode() == TargetOpcode::LIFETIME_START;
393       const MachineOperand &Mo = MI->getOperand(0);
394       int Slot = Mo.getIndex();
395       assert(Slot >= 0 && "Invalid slot");
396
397       SlotIndex ThisIndex = Indexes->getInstructionIndex(MI);
398
399       if (IsStart) {
400         if (!Starts[Slot].isValid() || Starts[Slot] > ThisIndex)
401           Starts[Slot] = ThisIndex;
402       } else {
403         if (!Finishes[Slot].isValid() || Finishes[Slot] < ThisIndex)
404           Finishes[Slot] = ThisIndex;
405       }
406     }
407
408     // Create the interval of the blocks that we previously found to be 'alive'.
409     BlockLifetimeInfo &MBBLiveness = BlockLiveness[&MBB];
410     for (int pos = MBBLiveness.LiveIn.find_first(); pos != -1;
411          pos = MBBLiveness.LiveIn.find_next(pos)) {
412       Starts[pos] = Indexes->getMBBStartIdx(&MBB);
413     }
414     for (int pos = MBBLiveness.LiveOut.find_first(); pos != -1;
415          pos = MBBLiveness.LiveOut.find_next(pos)) {
416       Finishes[pos] = Indexes->getMBBEndIdx(&MBB);
417     }
418
419     for (unsigned i = 0; i < NumSlots; ++i) {
420       assert(Starts[i].isValid() == Finishes[i].isValid() && "Unmatched range");
421       if (!Starts[i].isValid())
422         continue;
423
424       assert(Starts[i] && Finishes[i] && "Invalid interval");
425       VNInfo *ValNum = Intervals[i]->getValNumInfo(0);
426       SlotIndex S = Starts[i];
427       SlotIndex F = Finishes[i];
428       if (S < F) {
429         // We have a single consecutive region.
430         Intervals[i]->addSegment(LiveInterval::Segment(S, F, ValNum));
431       } else {
432         // We have two non-consecutive regions. This happens when
433         // LIFETIME_START appears after the LIFETIME_END marker.
434         SlotIndex NewStart = Indexes->getMBBStartIdx(&MBB);
435         SlotIndex NewFin = Indexes->getMBBEndIdx(&MBB);
436         Intervals[i]->addSegment(LiveInterval::Segment(NewStart, F, ValNum));
437         Intervals[i]->addSegment(LiveInterval::Segment(S, NewFin, ValNum));
438       }
439     }
440   }
441 }
442
443 bool StackColoring::removeAllMarkers() {
444   unsigned Count = 0;
445   for (MachineInstr *MI : Markers) {
446     MI->eraseFromParent();
447     Count++;
448   }
449   Markers.clear();
450
451   DEBUG(dbgs()<<"Removed "<<Count<<" markers.\n");
452   return Count;
453 }
454
455 void StackColoring::remapInstructions(DenseMap<int, int> &SlotRemap) {
456   unsigned FixedInstr = 0;
457   unsigned FixedMemOp = 0;
458   unsigned FixedDbg = 0;
459   MachineModuleInfo *MMI = &MF->getMMI();
460
461   // Remap debug information that refers to stack slots.
462   for (auto &VI : MMI->getVariableDbgInfo()) {
463     if (!VI.Var)
464       continue;
465     if (SlotRemap.count(VI.Slot)) {
466       DEBUG(dbgs() << "Remapping debug info for ["
467                    << cast<DILocalVariable>(VI.Var)->getName() << "].\n");
468       VI.Slot = SlotRemap[VI.Slot];
469       FixedDbg++;
470     }
471   }
472
473   // Keep a list of *allocas* which need to be remapped.
474   DenseMap<const AllocaInst*, const AllocaInst*> Allocas;
475   for (const std::pair<int, int> &SI : SlotRemap) {
476     const AllocaInst *From = MFI->getObjectAllocation(SI.first);
477     const AllocaInst *To = MFI->getObjectAllocation(SI.second);
478     assert(To && From && "Invalid allocation object");
479     Allocas[From] = To;
480
481     // AA might be used later for instruction scheduling, and we need it to be
482     // able to deduce the correct aliasing releationships between pointers
483     // derived from the alloca being remapped and the target of that remapping.
484     // The only safe way, without directly informing AA about the remapping
485     // somehow, is to directly update the IR to reflect the change being made
486     // here.
487     Instruction *Inst = const_cast<AllocaInst *>(To);
488     if (From->getType() != To->getType()) {
489       BitCastInst *Cast = new BitCastInst(Inst, From->getType());
490       Cast->insertAfter(Inst);
491       Inst = Cast;
492     }
493
494     // Allow the stack protector to adjust its value map to account for the
495     // upcoming replacement.
496     SP->adjustForColoring(From, To);
497
498     // Note that this will not replace uses in MMOs (which we'll update below),
499     // or anywhere else (which is why we won't delete the original
500     // instruction).
501     const_cast<AllocaInst *>(From)->replaceAllUsesWith(Inst);
502   }
503
504   // Remap all instructions to the new stack slots.
505   for (MachineBasicBlock &BB : *MF)
506     for (MachineInstr &I : BB) {
507       // Skip lifetime markers. We'll remove them soon.
508       if (I.getOpcode() == TargetOpcode::LIFETIME_START ||
509           I.getOpcode() == TargetOpcode::LIFETIME_END)
510         continue;
511
512       // Update the MachineMemOperand to use the new alloca.
513       for (MachineMemOperand *MMO : I.memoperands()) {
514         // FIXME: In order to enable the use of TBAA when using AA in CodeGen,
515         // we'll also need to update the TBAA nodes in MMOs with values
516         // derived from the merged allocas. When doing this, we'll need to use
517         // the same variant of GetUnderlyingObjects that is used by the
518         // instruction scheduler (that can look through ptrtoint/inttoptr
519         // pairs).
520
521         // We've replaced IR-level uses of the remapped allocas, so we only
522         // need to replace direct uses here.
523         const AllocaInst *AI = dyn_cast_or_null<AllocaInst>(MMO->getValue());
524         if (!AI)
525           continue;
526
527         if (!Allocas.count(AI))
528           continue;
529
530         MMO->setValue(Allocas[AI]);
531         FixedMemOp++;
532       }
533
534       // Update all of the machine instruction operands.
535       for (MachineOperand &MO : I.operands()) {
536         if (!MO.isFI())
537           continue;
538         int FromSlot = MO.getIndex();
539
540         // Don't touch arguments.
541         if (FromSlot<0)
542           continue;
543
544         // Only look at mapped slots.
545         if (!SlotRemap.count(FromSlot))
546           continue;
547
548         // In a debug build, check that the instruction that we are modifying is
549         // inside the expected live range. If the instruction is not inside
550         // the calculated range then it means that the alloca usage moved
551         // outside of the lifetime markers, or that the user has a bug.
552         // NOTE: Alloca address calculations which happen outside the lifetime
553         // zone are are okay, despite the fact that we don't have a good way
554         // for validating all of the usages of the calculation.
555 #ifndef NDEBUG
556         bool TouchesMemory = I.mayLoad() || I.mayStore();
557         // If we *don't* protect the user from escaped allocas, don't bother
558         // validating the instructions.
559         if (!I.isDebugValue() && TouchesMemory && ProtectFromEscapedAllocas) {
560           SlotIndex Index = Indexes->getInstructionIndex(&I);
561           const LiveInterval *Interval = &*Intervals[FromSlot];
562           assert(Interval->find(Index) != Interval->end() &&
563                  "Found instruction usage outside of live range.");
564         }
565 #endif
566
567         // Fix the machine instructions.
568         int ToSlot = SlotRemap[FromSlot];
569         MO.setIndex(ToSlot);
570         FixedInstr++;
571       }
572     }
573
574   // Update the location of C++ catch objects for the MSVC personality routine.
575   if (WinEHFuncInfo *EHInfo = MF->getWinEHFuncInfo())
576     for (WinEHTryBlockMapEntry &TBME : EHInfo->TryBlockMap)
577       for (WinEHHandlerType &H : TBME.HandlerArray)
578         if (H.CatchObj.FrameIndex != INT_MAX &&
579             SlotRemap.count(H.CatchObj.FrameIndex))
580           H.CatchObj.FrameIndex = SlotRemap[H.CatchObj.FrameIndex];
581
582   DEBUG(dbgs()<<"Fixed "<<FixedMemOp<<" machine memory operands.\n");
583   DEBUG(dbgs()<<"Fixed "<<FixedDbg<<" debug locations.\n");
584   DEBUG(dbgs()<<"Fixed "<<FixedInstr<<" machine instructions.\n");
585 }
586
587 void StackColoring::removeInvalidSlotRanges() {
588   for (MachineBasicBlock &BB : *MF)
589     for (MachineInstr &I : BB) {
590       if (I.getOpcode() == TargetOpcode::LIFETIME_START ||
591           I.getOpcode() == TargetOpcode::LIFETIME_END || I.isDebugValue())
592         continue;
593
594       // Some intervals are suspicious! In some cases we find address
595       // calculations outside of the lifetime zone, but not actual memory
596       // read or write. Memory accesses outside of the lifetime zone are a clear
597       // violation, but address calculations are okay. This can happen when
598       // GEPs are hoisted outside of the lifetime zone.
599       // So, in here we only check instructions which can read or write memory.
600       if (!I.mayLoad() && !I.mayStore())
601         continue;
602
603       // Check all of the machine operands.
604       for (const MachineOperand &MO : I.operands()) {
605         if (!MO.isFI())
606           continue;
607
608         int Slot = MO.getIndex();
609
610         if (Slot<0)
611           continue;
612
613         if (Intervals[Slot]->empty())
614           continue;
615
616         // Check that the used slot is inside the calculated lifetime range.
617         // If it is not, warn about it and invalidate the range.
618         LiveInterval *Interval = &*Intervals[Slot];
619         SlotIndex Index = Indexes->getInstructionIndex(&I);
620         if (Interval->find(Index) == Interval->end()) {
621           Interval->clear();
622           DEBUG(dbgs()<<"Invalidating range #"<<Slot<<"\n");
623           EscapedAllocas++;
624         }
625       }
626     }
627 }
628
629 void StackColoring::expungeSlotMap(DenseMap<int, int> &SlotRemap,
630                                    unsigned NumSlots) {
631   // Expunge slot remap map.
632   for (unsigned i=0; i < NumSlots; ++i) {
633     // If we are remapping i
634     if (SlotRemap.count(i)) {
635       int Target = SlotRemap[i];
636       // As long as our target is mapped to something else, follow it.
637       while (SlotRemap.count(Target)) {
638         Target = SlotRemap[Target];
639         SlotRemap[i] = Target;
640       }
641     }
642   }
643 }
644
645 bool StackColoring::runOnMachineFunction(MachineFunction &Func) {
646   if (skipOptnoneFunction(*Func.getFunction()))
647     return false;
648
649   DEBUG(dbgs() << "********** Stack Coloring **********\n"
650                << "********** Function: "
651                << ((const Value*)Func.getFunction())->getName() << '\n');
652   MF = &Func;
653   MFI = MF->getFrameInfo();
654   Indexes = &getAnalysis<SlotIndexes>();
655   SP = &getAnalysis<StackProtector>();
656   BlockLiveness.clear();
657   BasicBlocks.clear();
658   BasicBlockNumbering.clear();
659   Markers.clear();
660   Intervals.clear();
661   VNInfoAllocator.Reset();
662
663   unsigned NumSlots = MFI->getObjectIndexEnd();
664
665   // If there are no stack slots then there are no markers to remove.
666   if (!NumSlots)
667     return false;
668
669   SmallVector<int, 8> SortedSlots;
670
671   SortedSlots.reserve(NumSlots);
672   Intervals.reserve(NumSlots);
673
674   unsigned NumMarkers = collectMarkers(NumSlots);
675
676   unsigned TotalSize = 0;
677   DEBUG(dbgs()<<"Found "<<NumMarkers<<" markers and "<<NumSlots<<" slots\n");
678   DEBUG(dbgs()<<"Slot structure:\n");
679
680   for (int i=0; i < MFI->getObjectIndexEnd(); ++i) {
681     DEBUG(dbgs()<<"Slot #"<<i<<" - "<<MFI->getObjectSize(i)<<" bytes.\n");
682     TotalSize += MFI->getObjectSize(i);
683   }
684
685   DEBUG(dbgs()<<"Total Stack size: "<<TotalSize<<" bytes\n\n");
686
687   // Don't continue because there are not enough lifetime markers, or the
688   // stack is too small, or we are told not to optimize the slots.
689   if (NumMarkers < 2 || TotalSize < 16 || DisableColoring) {
690     DEBUG(dbgs()<<"Will not try to merge slots.\n");
691     return removeAllMarkers();
692   }
693
694   for (unsigned i=0; i < NumSlots; ++i) {
695     std::unique_ptr<LiveInterval> LI(new LiveInterval(i, 0));
696     LI->getNextValue(Indexes->getZeroIndex(), VNInfoAllocator);
697     Intervals.push_back(std::move(LI));
698     SortedSlots.push_back(i);
699   }
700
701   // Calculate the liveness of each block.
702   calculateLocalLiveness();
703
704   // Propagate the liveness information.
705   calculateLiveIntervals(NumSlots);
706
707   // Search for allocas which are used outside of the declared lifetime
708   // markers.
709   if (ProtectFromEscapedAllocas)
710     removeInvalidSlotRanges();
711
712   // Maps old slots to new slots.
713   DenseMap<int, int> SlotRemap;
714   unsigned RemovedSlots = 0;
715   unsigned ReducedSize = 0;
716
717   // Do not bother looking at empty intervals.
718   for (unsigned I = 0; I < NumSlots; ++I) {
719     if (Intervals[SortedSlots[I]]->empty())
720       SortedSlots[I] = -1;
721   }
722
723   // This is a simple greedy algorithm for merging allocas. First, sort the
724   // slots, placing the largest slots first. Next, perform an n^2 scan and look
725   // for disjoint slots. When you find disjoint slots, merge the samller one
726   // into the bigger one and update the live interval. Remove the small alloca
727   // and continue.
728
729   // Sort the slots according to their size. Place unused slots at the end.
730   // Use stable sort to guarantee deterministic code generation.
731   std::stable_sort(SortedSlots.begin(), SortedSlots.end(),
732                    [this](int LHS, int RHS) {
733     // We use -1 to denote a uninteresting slot. Place these slots at the end.
734     if (LHS == -1) return false;
735     if (RHS == -1) return true;
736     // Sort according to size.
737     return MFI->getObjectSize(LHS) > MFI->getObjectSize(RHS);
738   });
739
740   bool Changed = true;
741   while (Changed) {
742     Changed = false;
743     for (unsigned I = 0; I < NumSlots; ++I) {
744       if (SortedSlots[I] == -1)
745         continue;
746
747       for (unsigned J=I+1; J < NumSlots; ++J) {
748         if (SortedSlots[J] == -1)
749           continue;
750
751         int FirstSlot = SortedSlots[I];
752         int SecondSlot = SortedSlots[J];
753         LiveInterval *First = &*Intervals[FirstSlot];
754         LiveInterval *Second = &*Intervals[SecondSlot];
755         assert (!First->empty() && !Second->empty() && "Found an empty range");
756
757         // Merge disjoint slots.
758         if (!First->overlaps(*Second)) {
759           Changed = true;
760           First->MergeSegmentsInAsValue(*Second, First->getValNumInfo(0));
761           SlotRemap[SecondSlot] = FirstSlot;
762           SortedSlots[J] = -1;
763           DEBUG(dbgs()<<"Merging #"<<FirstSlot<<" and slots #"<<
764                 SecondSlot<<" together.\n");
765           unsigned MaxAlignment = std::max(MFI->getObjectAlignment(FirstSlot),
766                                            MFI->getObjectAlignment(SecondSlot));
767
768           assert(MFI->getObjectSize(FirstSlot) >=
769                  MFI->getObjectSize(SecondSlot) &&
770                  "Merging a small object into a larger one");
771
772           RemovedSlots+=1;
773           ReducedSize += MFI->getObjectSize(SecondSlot);
774           MFI->setObjectAlignment(FirstSlot, MaxAlignment);
775           MFI->RemoveStackObject(SecondSlot);
776         }
777       }
778     }
779   }// While changed.
780
781   // Record statistics.
782   StackSpaceSaved += ReducedSize;
783   StackSlotMerged += RemovedSlots;
784   DEBUG(dbgs()<<"Merge "<<RemovedSlots<<" slots. Saved "<<
785         ReducedSize<<" bytes\n");
786
787   // Scan the entire function and update all machine operands that use frame
788   // indices to use the remapped frame index.
789   expungeSlotMap(SlotRemap, NumSlots);
790   remapInstructions(SlotRemap);
791
792   return removeAllMarkers();
793 }