1 //===-- LiveStackAnalysis.h - Live Stack Slot Analysis ----------*- C++ -*-===//
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 live stack slot analysis pass. It is analogous to
11 // live interval analysis except it's analyzing liveness of stack slots rather
12 // than registers.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CODEGEN_LIVESTACKANALYSIS_H
17 #define LLVM_CODEGEN_LIVESTACKANALYSIS_H
18 
19 #include "llvm/CodeGen/LiveInterval.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/Support/Allocator.h"
22 #include "llvm/Target/TargetRegisterInfo.h"
23 #include <map>
24 #include <unordered_map>
25 
26 namespace llvm {
27 
28   class LiveStacks : public MachineFunctionPass {
29     const TargetRegisterInfo *TRI;
30 
31     /// Special pool allocator for VNInfo's (LiveInterval val#).
32     ///
33     VNInfo::Allocator VNInfoAllocator;
34 
35     /// S2IMap - Stack slot indices to live interval mapping.
36     ///
37     typedef std::unordered_map<int, LiveInterval> SS2IntervalMap;
38     SS2IntervalMap S2IMap;
39 
40     /// S2RCMap - Stack slot indices to register class mapping.
41     std::map<int, const TargetRegisterClass*> S2RCMap;
42 
43   public:
44     static char ID; // Pass identification, replacement for typeid
LiveStacks()45     LiveStacks() : MachineFunctionPass(ID) {
46       initializeLiveStacksPass(*PassRegistry::getPassRegistry());
47     }
48 
49     typedef SS2IntervalMap::iterator iterator;
50     typedef SS2IntervalMap::const_iterator const_iterator;
begin()51     const_iterator begin() const { return S2IMap.begin(); }
end()52     const_iterator end() const { return S2IMap.end(); }
begin()53     iterator begin() { return S2IMap.begin(); }
end()54     iterator end() { return S2IMap.end(); }
55 
getNumIntervals()56     unsigned getNumIntervals() const { return (unsigned)S2IMap.size(); }
57 
58     LiveInterval &getOrCreateInterval(int Slot, const TargetRegisterClass *RC);
59 
getInterval(int Slot)60     LiveInterval &getInterval(int Slot) {
61       assert(Slot >= 0 && "Spill slot indice must be >= 0");
62       SS2IntervalMap::iterator I = S2IMap.find(Slot);
63       assert(I != S2IMap.end() && "Interval does not exist for stack slot");
64       return I->second;
65     }
66 
getInterval(int Slot)67     const LiveInterval &getInterval(int Slot) const {
68       assert(Slot >= 0 && "Spill slot indice must be >= 0");
69       SS2IntervalMap::const_iterator I = S2IMap.find(Slot);
70       assert(I != S2IMap.end() && "Interval does not exist for stack slot");
71       return I->second;
72     }
73 
hasInterval(int Slot)74     bool hasInterval(int Slot) const {
75       return S2IMap.count(Slot);
76     }
77 
getIntervalRegClass(int Slot)78     const TargetRegisterClass *getIntervalRegClass(int Slot) const {
79       assert(Slot >= 0 && "Spill slot indice must be >= 0");
80       std::map<int, const TargetRegisterClass*>::const_iterator
81         I = S2RCMap.find(Slot);
82       assert(I != S2RCMap.end() &&
83              "Register class info does not exist for stack slot");
84       return I->second;
85     }
86 
getVNInfoAllocator()87     VNInfo::Allocator& getVNInfoAllocator() { return VNInfoAllocator; }
88 
89     void getAnalysisUsage(AnalysisUsage &AU) const override;
90     void releaseMemory() override;
91 
92     /// runOnMachineFunction - pass entry point
93     bool runOnMachineFunction(MachineFunction&) override;
94 
95     /// print - Implement the dump method.
96     void print(raw_ostream &O, const Module* = nullptr) const override;
97   };
98 }
99 
100 #endif /* LLVM_CODEGEN_LIVESTACK_ANALYSIS_H */
101