LiveStackAnalysis.h revision 193323
1193323Sed//===-- LiveStackAnalysis.h - Live Stack Slot Analysis ----------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file implements the live stack slot analysis pass. It is analogous to
11193323Sed// live interval analysis except it's analyzing liveness of stack slots rather
12193323Sed// than registers.
13193323Sed//
14193323Sed//===----------------------------------------------------------------------===//
15193323Sed
16193323Sed#ifndef LLVM_CODEGEN_LIVESTACK_ANALYSIS_H
17193323Sed#define LLVM_CODEGEN_LIVESTACK_ANALYSIS_H
18193323Sed
19193323Sed#include "llvm/CodeGen/MachineFunctionPass.h"
20193323Sed#include "llvm/CodeGen/LiveInterval.h"
21193323Sed#include "llvm/Target/TargetRegisterInfo.h"
22193323Sed#include "llvm/Support/Allocator.h"
23193323Sed#include <map>
24193323Sed
25193323Sednamespace llvm {
26193323Sed
27193323Sed  class LiveStacks : public MachineFunctionPass {
28193323Sed    /// Special pool allocator for VNInfo's (LiveInterval val#).
29193323Sed    ///
30193323Sed    BumpPtrAllocator VNInfoAllocator;
31193323Sed
32193323Sed    /// S2IMap - Stack slot indices to live interval mapping.
33193323Sed    ///
34193323Sed    typedef std::map<int, LiveInterval> SS2IntervalMap;
35193323Sed    SS2IntervalMap S2IMap;
36193323Sed
37193323Sed    /// S2RCMap - Stack slot indices to register class mapping.
38193323Sed    std::map<int, const TargetRegisterClass*> S2RCMap;
39193323Sed
40193323Sed  public:
41193323Sed    static char ID; // Pass identification, replacement for typeid
42193323Sed    LiveStacks() : MachineFunctionPass(&ID) {}
43193323Sed
44193323Sed    typedef SS2IntervalMap::iterator iterator;
45193323Sed    typedef SS2IntervalMap::const_iterator const_iterator;
46193323Sed    const_iterator begin() const { return S2IMap.begin(); }
47193323Sed    const_iterator end() const { return S2IMap.end(); }
48193323Sed    iterator begin() { return S2IMap.begin(); }
49193323Sed    iterator end() { return S2IMap.end(); }
50193323Sed
51193323Sed    void scaleNumbering(int factor);
52193323Sed
53193323Sed    unsigned getNumIntervals() const { return (unsigned)S2IMap.size(); }
54193323Sed
55193323Sed    LiveInterval &getOrCreateInterval(int Slot, const TargetRegisterClass *RC) {
56193323Sed      assert(Slot >= 0 && "Spill slot indice must be >= 0");
57193323Sed      SS2IntervalMap::iterator I = S2IMap.find(Slot);
58193323Sed      if (I == S2IMap.end()) {
59193323Sed        I = S2IMap.insert(I,std::make_pair(Slot, LiveInterval(Slot,0.0F,true)));
60193323Sed        S2RCMap.insert(std::make_pair(Slot, RC));
61193323Sed      } else {
62193323Sed        // Use the largest common subclass register class.
63193323Sed        const TargetRegisterClass *OldRC = S2RCMap[Slot];
64193323Sed        S2RCMap[Slot] = getCommonSubClass(OldRC, RC);
65193323Sed      }
66193323Sed      return I->second;
67193323Sed    }
68193323Sed
69193323Sed    LiveInterval &getInterval(int Slot) {
70193323Sed      assert(Slot >= 0 && "Spill slot indice must be >= 0");
71193323Sed      SS2IntervalMap::iterator I = S2IMap.find(Slot);
72193323Sed      assert(I != S2IMap.end() && "Interval does not exist for stack slot");
73193323Sed      return I->second;
74193323Sed    }
75193323Sed
76193323Sed    const LiveInterval &getInterval(int Slot) const {
77193323Sed      assert(Slot >= 0 && "Spill slot indice must be >= 0");
78193323Sed      SS2IntervalMap::const_iterator I = S2IMap.find(Slot);
79193323Sed      assert(I != S2IMap.end() && "Interval does not exist for stack slot");
80193323Sed      return I->second;
81193323Sed    }
82193323Sed
83193323Sed    bool hasInterval(int Slot) const {
84193323Sed      return S2IMap.count(Slot);
85193323Sed    }
86193323Sed
87193323Sed    const TargetRegisterClass *getIntervalRegClass(int Slot) const {
88193323Sed      assert(Slot >= 0 && "Spill slot indice must be >= 0");
89193323Sed      std::map<int, const TargetRegisterClass*>::const_iterator
90193323Sed        I = S2RCMap.find(Slot);
91193323Sed      assert(I != S2RCMap.end() &&
92193323Sed             "Register class info does not exist for stack slot");
93193323Sed      return I->second;
94193323Sed    }
95193323Sed
96193323Sed    BumpPtrAllocator& getVNInfoAllocator() { return VNInfoAllocator; }
97193323Sed
98193323Sed    virtual void getAnalysisUsage(AnalysisUsage &AU) const;
99193323Sed    virtual void releaseMemory();
100193323Sed
101193323Sed    /// runOnMachineFunction - pass entry point
102193323Sed    virtual bool runOnMachineFunction(MachineFunction&);
103193323Sed
104193323Sed    /// print - Implement the dump method.
105193323Sed    virtual void print(std::ostream &O, const Module* = 0) const;
106193323Sed    void print(std::ostream *O, const Module* M = 0) const {
107193323Sed      if (O) print(*O, M);
108193323Sed    }
109193323Sed  };
110193323Sed}
111193323Sed
112193323Sed#endif /* LLVM_CODEGEN_LIVESTACK_ANALYSIS_H */
113