1*da09e106SDimitry Andric //===-- LiveStacks.cpp - Live Stack Slot Analysis -------------------------===//
2*da09e106SDimitry Andric //
3*da09e106SDimitry Andric //                     The LLVM Compiler Infrastructure
4*da09e106SDimitry Andric //
5*da09e106SDimitry Andric // This file is distributed under the University of Illinois Open Source
6*da09e106SDimitry Andric // License. See LICENSE.TXT for details.
7*da09e106SDimitry Andric //
8*da09e106SDimitry Andric //===----------------------------------------------------------------------===//
9*da09e106SDimitry Andric //
10*da09e106SDimitry Andric // This file implements the live stack slot analysis pass. It is analogous to
11*da09e106SDimitry Andric // live interval analysis except it's analyzing liveness of stack slots rather
12*da09e106SDimitry Andric // than registers.
13*da09e106SDimitry Andric //
14*da09e106SDimitry Andric //===----------------------------------------------------------------------===//
15*da09e106SDimitry Andric 
16*da09e106SDimitry Andric #include "llvm/CodeGen/LiveStacks.h"
17*da09e106SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
18*da09e106SDimitry Andric #include "llvm/CodeGen/Passes.h"
19*da09e106SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
20*da09e106SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
21*da09e106SDimitry Andric #include "llvm/Support/Debug.h"
22*da09e106SDimitry Andric #include "llvm/Support/raw_ostream.h"
23*da09e106SDimitry Andric using namespace llvm;
24*da09e106SDimitry Andric 
25*da09e106SDimitry Andric #define DEBUG_TYPE "livestacks"
26*da09e106SDimitry Andric 
27*da09e106SDimitry Andric char LiveStacks::ID = 0;
28*da09e106SDimitry Andric INITIALIZE_PASS_BEGIN(LiveStacks, DEBUG_TYPE,
29*da09e106SDimitry Andric                 "Live Stack Slot Analysis", false, false)
30*da09e106SDimitry Andric INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
31*da09e106SDimitry Andric INITIALIZE_PASS_END(LiveStacks, DEBUG_TYPE,
32*da09e106SDimitry Andric                 "Live Stack Slot Analysis", false, false)
33*da09e106SDimitry Andric 
34*da09e106SDimitry Andric char &llvm::LiveStacksID = LiveStacks::ID;
35*da09e106SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const36*da09e106SDimitry Andric void LiveStacks::getAnalysisUsage(AnalysisUsage &AU) const {
37*da09e106SDimitry Andric   AU.setPreservesAll();
38*da09e106SDimitry Andric   AU.addPreserved<SlotIndexes>();
39*da09e106SDimitry Andric   AU.addRequiredTransitive<SlotIndexes>();
40*da09e106SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
41*da09e106SDimitry Andric }
42*da09e106SDimitry Andric 
releaseMemory()43*da09e106SDimitry Andric void LiveStacks::releaseMemory() {
44*da09e106SDimitry Andric   // Release VNInfo memory regions, VNInfo objects don't need to be dtor'd.
45*da09e106SDimitry Andric   VNInfoAllocator.Reset();
46*da09e106SDimitry Andric   S2IMap.clear();
47*da09e106SDimitry Andric   S2RCMap.clear();
48*da09e106SDimitry Andric }
49*da09e106SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)50*da09e106SDimitry Andric bool LiveStacks::runOnMachineFunction(MachineFunction &MF) {
51*da09e106SDimitry Andric   TRI = MF.getSubtarget().getRegisterInfo();
52*da09e106SDimitry Andric   // FIXME: No analysis is being done right now. We are relying on the
53*da09e106SDimitry Andric   // register allocators to provide the information.
54*da09e106SDimitry Andric   return false;
55*da09e106SDimitry Andric }
56*da09e106SDimitry Andric 
57*da09e106SDimitry Andric LiveInterval &
getOrCreateInterval(int Slot,const TargetRegisterClass * RC)58*da09e106SDimitry Andric LiveStacks::getOrCreateInterval(int Slot, const TargetRegisterClass *RC) {
59*da09e106SDimitry Andric   assert(Slot >= 0 && "Spill slot indice must be >= 0");
60*da09e106SDimitry Andric   SS2IntervalMap::iterator I = S2IMap.find(Slot);
61*da09e106SDimitry Andric   if (I == S2IMap.end()) {
62*da09e106SDimitry Andric     I = S2IMap.emplace(std::piecewise_construct, std::forward_as_tuple(Slot),
63*da09e106SDimitry Andric                        std::forward_as_tuple(
64*da09e106SDimitry Andric                            TargetRegisterInfo::index2StackSlot(Slot), 0.0F))
65*da09e106SDimitry Andric             .first;
66*da09e106SDimitry Andric     S2RCMap.insert(std::make_pair(Slot, RC));
67*da09e106SDimitry Andric   } else {
68*da09e106SDimitry Andric     // Use the largest common subclass register class.
69*da09e106SDimitry Andric     const TargetRegisterClass *OldRC = S2RCMap[Slot];
70*da09e106SDimitry Andric     S2RCMap[Slot] = TRI->getCommonSubClass(OldRC, RC);
71*da09e106SDimitry Andric   }
72*da09e106SDimitry Andric   return I->second;
73*da09e106SDimitry Andric }
74*da09e106SDimitry Andric 
75*da09e106SDimitry Andric /// print - Implement the dump method.
print(raw_ostream & OS,const Module *) const76*da09e106SDimitry Andric void LiveStacks::print(raw_ostream &OS, const Module*) const {
77*da09e106SDimitry Andric 
78*da09e106SDimitry Andric   OS << "********** INTERVALS **********\n";
79*da09e106SDimitry Andric   for (const_iterator I = begin(), E = end(); I != E; ++I) {
80*da09e106SDimitry Andric     I->second.print(OS);
81*da09e106SDimitry Andric     int Slot = I->first;
82*da09e106SDimitry Andric     const TargetRegisterClass *RC = getIntervalRegClass(Slot);
83*da09e106SDimitry Andric     if (RC)
84*da09e106SDimitry Andric       OS << " [" << TRI->getRegClassName(RC) << "]\n";
85*da09e106SDimitry Andric     else
86*da09e106SDimitry Andric       OS << " [Unknown]\n";
87*da09e106SDimitry Andric   }
88*da09e106SDimitry Andric }
89