blob: eabae4bddcdd26224e545e2dde7a8bd53733733b [file] [log] [blame]
Chris Lattnerc8e66542002-04-27 06:56:12 +00001/* Title: FunctionLiveVarInfo.h -*- C++ -*-
Ruchira Sasanka9f181192001-07-24 17:14:13 +00002 Author: Ruchira Sasanka
3 Date: Jun 30, 01
4 Purpose:
5
Chris Lattnerc8e66542002-04-27 06:56:12 +00006 This is the interface for live variable info of a function that is required
Ruchira Sasankaec1a5412001-08-20 21:11:01 +00007 by any other part of the compiler
Ruchira Sasanka9f181192001-07-24 17:14:13 +00008
Ruchira Sasankaec1a5412001-08-20 21:11:01 +00009 It must be called like:
Ruchira Sasanka9f181192001-07-24 17:14:13 +000010
Chris Lattnerc8e66542002-04-27 06:56:12 +000011 FunctionLiveVarInfo FLVI(Function *); // initializes data structures
12 FLVI.analyze(); // do the actural live variable anal
Ruchira Sasanka9f181192001-07-24 17:14:13 +000013
14 After the analysis, getInSetOfBB or getOutSetofBB can be called to get
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000015 live var info of a BB.
Ruchira Sasanka9f181192001-07-24 17:14:13 +000016
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000017 The live var set before an instruction can be obtained in 2 ways:
Ruchira Sasanka9f181192001-07-24 17:14:13 +000018
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000019 1. Use the method getLiveVarSetAfterInst(Instruction *) to get the LV Info
20 just after an instruction. (also exists getLiveVarSetBeforeInst(..))
21
22 This function caluclates the LV info for a BB only once and caches that
23 info. If the cache does not contain the LV info of the instruction, it
24 calculates the LV info for the whole BB and caches them.
25
26 Getting liveVar info this way uses more memory since, LV info should be
27 cached. However, if you need LV info of nearly all the instructions of a
28 BB, this is the best and simplest interfrace.
29
30
31 2. Use the OutSet and applyTranferFuncForInst(const Instruction *const Inst)
Ruchira Sasanka9f181192001-07-24 17:14:13 +000032 declared in LiveVarSet and traverse the instructions of a basic block in
33 reverse (using const_reverse_iterator in the BB class).
34
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000035 This is the most memory efficient method if you need LV info for
36 only several instructions in a BasicBlock. An example is given below:
Ruchira Sasanka9f181192001-07-24 17:14:13 +000037
38
39 LiveVarSet LVSet; // this will be the set used to traverse through each BB
40
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000041 // Initialize LVSet so that it is the same as OutSet of the BB
Ruchira Sasanka9f181192001-07-24 17:14:13 +000042 LVSet.setUnion( LVI->getOutSetOfBB( *BBI ) );
43
44 BasicBlock::InstListType::const_reverse_iterator
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000045 InstIterator = InstListInBB.rbegin(); // get the rev iter for inst in BB
Ruchira Sasanka9f181192001-07-24 17:14:13 +000046
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000047 // iterate over all the instructions in BB in reverse
Ruchira Sasanka9f181192001-07-24 17:14:13 +000048 for( ; InstIterator != InstListInBB.rend(); InstIterator++) {
49
50 //...... all code here which uses LVSet ........
51
52 LVSet.applyTranferFuncForInst(*InstIterator);
53
54 // Now LVSet contains live vars ABOVE the current instrution
55 }
56
57 See buildInterferenceGraph() for the above example.
Ruchira Sasanka9f181192001-07-24 17:14:13 +000058*/
59
60
61#ifndef METH_LIVE_VAR_INFO_H
62#define METH_LIVE_VAR_INFO_H
63
Chris Lattner494266e2002-02-04 20:00:08 +000064#include "llvm/Pass.h"
Chris Lattnerb1def732002-02-05 02:51:01 +000065#include "llvm/Analysis/LiveVar/ValueSet.h"
Vikram S. Adve7e512012002-03-19 00:59:08 +000066#include "Support/CommandLine.h"
67
Chris Lattner94b8baf2002-02-05 00:33:19 +000068class BBLiveVar;
69class MachineInstr;
Ruchira Sasanka9f181192001-07-24 17:14:13 +000070
Vikram S. Adve7e512012002-03-19 00:59:08 +000071
72enum LiveVarDebugLevel_t {
73 LV_DEBUG_None,
74 LV_DEBUG_Normal,
75 LV_DEBUG_Instr,
76 LV_DEBUG_Verbose
77};
78
79extern cl::Enum<LiveVarDebugLevel_t> DEBUG_LV;
80
81
Chris Lattnerc8e66542002-04-27 06:56:12 +000082class MethodLiveVarInfo : public FunctionPass {
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000083 // Machine Instr to LiveVarSet Map for providing LVset BEFORE each inst
Chris Lattnerb1def732002-02-05 02:51:01 +000084 std::map<const MachineInstr *, const ValueSet *> MInst2LVSetBI;
Ruchira Sasanka9f181192001-07-24 17:14:13 +000085
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000086 // Machine Instr to LiveVarSet Map for providing LVset AFTER each inst
Chris Lattnerb1def732002-02-05 02:51:01 +000087 std::map<const MachineInstr *, const ValueSet *> MInst2LVSetAI;
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000088
Chris Lattner4e8c4872002-03-23 22:51:58 +000089 // Stored Function that the data is computed with respect to
90 const Function *M;
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000091
92 // --------- private methods -----------------------------------------
93
94 // constructs BBLiveVars and init Def and In sets
Chris Lattner4e8c4872002-03-23 22:51:58 +000095 void constructBBs(const Function *F);
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000096
97 // do one backward pass over the CFG
Chris Lattner4e8c4872002-03-23 22:51:58 +000098 bool doSingleBackwardPass(const Function *F, unsigned int iter);
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000099
100 // calculates live var sets for instructions in a BB
Chris Lattner339a3f62002-02-04 16:32:40 +0000101 void calcLiveVarSetsForBB(const BasicBlock *BB);
Ruchira Sasanka9f181192001-07-24 17:14:13 +0000102
Chris Lattner494266e2002-02-04 20:00:08 +0000103public:
104 static AnalysisID ID; // We are an analysis, we must have an ID
Ruchira Sasanka9f181192001-07-24 17:14:13 +0000105
Chris Lattner439d0e52002-02-04 20:49:04 +0000106 MethodLiveVarInfo(AnalysisID id = ID) { assert(id == ID); }
Chris Lattner494266e2002-02-04 20:00:08 +0000107
Chris Lattnerc8e66542002-04-27 06:56:12 +0000108 // --------- Implement the FunctionPass interface ----------------------
Chris Lattner494266e2002-02-04 20:00:08 +0000109
Chris Lattnerc8e66542002-04-27 06:56:12 +0000110 // runOnFunction - Perform analysis, update internal data structures.
111 virtual bool runOnFunction(Function *F);
Chris Lattner494266e2002-02-04 20:00:08 +0000112
113 // releaseMemory - After LiveVariable analysis has been used, forget!
114 virtual void releaseMemory();
115
Chris Lattnerc8e66542002-04-27 06:56:12 +0000116 // getAnalysisUsage - Provide self!
117 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
118 AU.setPreservesAll();
119 AU.addProvided(ID);
Chris Lattner494266e2002-02-04 20:00:08 +0000120 }
121
122 // --------- Functions to access analysis results -------------------
Ruchira Sasanka9f181192001-07-24 17:14:13 +0000123
Ruchira Sasankaec1a5412001-08-20 21:11:01 +0000124 // gets OutSet of a BB
Chris Lattner7e5ee422002-02-05 04:20:12 +0000125 const ValueSet &getOutSetOfBB(const BasicBlock *BB) const;
Ruchira Sasanka9f181192001-07-24 17:14:13 +0000126
Ruchira Sasankaec1a5412001-08-20 21:11:01 +0000127 // gets InSet of a BB
Chris Lattner7e5ee422002-02-05 04:20:12 +0000128 const ValueSet &getInSetOfBB(const BasicBlock *BB) const;
Ruchira Sasankaec1a5412001-08-20 21:11:01 +0000129
130 // gets the Live var set BEFORE an instruction
Chris Lattner7e5ee422002-02-05 04:20:12 +0000131 const ValueSet &getLiveVarSetBeforeMInst(const MachineInstr *MI,
Chris Lattnerb1def732002-02-05 02:51:01 +0000132 const BasicBlock *BB);
Ruchira Sasankaec1a5412001-08-20 21:11:01 +0000133
134 // gets the Live var set AFTER an instruction
Chris Lattner7e5ee422002-02-05 04:20:12 +0000135 const ValueSet &getLiveVarSetAfterMInst(const MachineInstr *MI,
Chris Lattnerb1def732002-02-05 02:51:01 +0000136 const BasicBlock *BB);
Ruchira Sasanka9f181192001-07-24 17:14:13 +0000137};
138
Ruchira Sasanka9f181192001-07-24 17:14:13 +0000139#endif