04bdfbe8febb7ec0404228e08ba2fef49fa6020a
[oota-llvm.git] / include / llvm / CodeGen / FunctionLiveVarInfo.h
1 //===-- CodeGen/FunctionLiveVarInfo.h - LiveVar Analysis --------*- C++ -*-===//
2 //
3 // This is the interface for live variable info of a function that is required 
4 // by any other part of the compiler
5 //
6 // After the analysis, getInSetOfBB or getOutSetofBB can be called to get 
7 // live var info of a BB.
8 //
9 // The live var set before an instruction can be obtained in 2 ways:
10 //
11 // 1. Use the method getLiveVarSetAfterInst(Instruction *) to get the LV Info 
12 //    just after an instruction. (also exists getLiveVarSetBeforeInst(..))
13 //
14 //    This function caluclates the LV info for a BB only once and caches that 
15 //    info. If the cache does not contain the LV info of the instruction, it 
16 //    calculates the LV info for the whole BB and caches them.
17 //
18 //    Getting liveVar info this way uses more memory since, LV info should be 
19 //    cached. However, if you need LV info of nearly all the instructions of a
20 //    BB, this is the best and simplest interfrace.
21 //
22 // 2. Use the OutSet and applyTranferFuncForInst(const Instruction *const Inst) 
23 //    declared in LiveVarSet and  traverse the instructions of a basic block in 
24 //    reverse (using const_reverse_iterator in the BB class). 
25 //
26 //===----------------------------------------------------------------------===//
27
28 #ifndef FUNCTION_LIVE_VAR_INFO_H
29 #define FUNCTION_LIVE_VAR_INFO_H
30
31 #include "Support/hash_map"
32 #include "llvm/Pass.h"
33 #include "llvm/CodeGen/ValueSet.h"
34
35 class BBLiveVar;
36 class MachineInstr;
37
38 class FunctionLiveVarInfo : public FunctionPass {
39   // Machine Instr to LiveVarSet Map for providing LVset BEFORE each inst
40   // These sets are owned by this map and will be freed in releaseMemory().
41   hash_map<const MachineInstr *, ValueSet *> MInst2LVSetBI; 
42
43   // Machine Instr to LiveVarSet Map for providing LVset AFTER each inst.
44   // These sets are just pointers to sets in MInst2LVSetBI or BBLiveVar.
45   hash_map<const MachineInstr *, ValueSet *> MInst2LVSetAI; 
46
47   // Stored Function that the data is computed with respect to
48   const Function *M;
49
50   // --------- private methods -----------------------------------------
51
52   // constructs BBLiveVars and init Def and In sets
53   void constructBBs(const Function *F);
54     
55   // do one backward pass over the CFG
56   bool doSingleBackwardPass(const Function *F, unsigned int iter); 
57
58   // calculates live var sets for instructions in a BB
59   void calcLiveVarSetsForBB(const BasicBlock *BB);
60   
61 public:
62   // --------- Implement the FunctionPass interface ----------------------
63
64   // runOnFunction - Perform analysis, update internal data structures.
65   virtual bool runOnFunction(Function &F);
66
67   // releaseMemory - After LiveVariable analysis has been used, forget!
68   virtual void releaseMemory();
69
70   // getAnalysisUsage - Provide self!
71   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
72     AU.setPreservesAll();
73   }
74
75   // --------- Functions to access analysis results -------------------
76
77   // get OutSet of a BB
78   const ValueSet &getOutSetOfBB(const BasicBlock *BB) const;
79         ValueSet &getOutSetOfBB(const BasicBlock *BB)      ;
80
81   // get InSet of a BB
82   const ValueSet &getInSetOfBB(const BasicBlock *BB) const;
83         ValueSet &getInSetOfBB(const BasicBlock *BB)      ;
84
85   // gets the Live var set BEFORE an instruction.
86   // if BB is specified and the live var set has not yet been computed,
87   // it will be computed on demand.
88   const ValueSet &getLiveVarSetBeforeMInst(const MachineInstr *MI,
89                                            const BasicBlock *BB = 0);
90
91   // gets the Live var set AFTER an instruction
92   // if BB is specified and the live var set has not yet been computed,
93   // it will be computed on demand.
94   const ValueSet &getLiveVarSetAfterMInst(const MachineInstr *MI,
95                                           const BasicBlock *BB = 0);
96 };
97
98 #endif