blob: 02d5c0531bbd0610d8c271dadec08d6ce8d6cf60 [file] [log] [blame]
Chris Lattner67efeb92003-09-30 18:05:30 +00001//===-- CodeGen/FunctionLiveVarInfo.h - LiveVar Analysis --------*- C++ -*-===//
John Criswellb6445982003-10-20 20:19:47 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner67efeb92003-09-30 18:05:30 +00009//
10// This is the interface for live variable info of a function that is required
11// by any other part of the compiler
12//
13// After the analysis, getInSetOfBB or getOutSetofBB can be called to get
14// live var info of a BB.
15//
16// The live var set before an instruction can be obtained in 2 ways:
17//
18// 1. Use the method getLiveVarSetAfterInst(Instruction *) to get the LV Info
19// just after an instruction. (also exists getLiveVarSetBeforeInst(..))
20//
21// This function caluclates the LV info for a BB only once and caches that
22// info. If the cache does not contain the LV info of the instruction, it
23// calculates the LV info for the whole BB and caches them.
24//
25// Getting liveVar info this way uses more memory since, LV info should be
26// cached. However, if you need LV info of nearly all the instructions of a
27// BB, this is the best and simplest interfrace.
28//
29// 2. Use the OutSet and applyTranferFuncForInst(const Instruction *const Inst)
30// declared in LiveVarSet and traverse the instructions of a basic block in
31// reverse (using const_reverse_iterator in the BB class).
32//
33//===----------------------------------------------------------------------===//
Ruchira Sasanka9f181192001-07-24 17:14:13 +000034
Chris Lattnerd858d802002-07-26 21:11:42 +000035#ifndef FUNCTION_LIVE_VAR_INFO_H
36#define FUNCTION_LIVE_VAR_INFO_H
Ruchira Sasanka9f181192001-07-24 17:14:13 +000037
Reid Spencer7c16caa2004-09-01 22:55:40 +000038#include "llvm/ADT/hash_map"
Chris Lattner494266e2002-02-04 20:00:08 +000039#include "llvm/Pass.h"
Chris Lattnerde1d7292003-01-14 22:56:37 +000040#include "llvm/CodeGen/ValueSet.h"
Vikram S. Adve7e512012002-03-19 00:59:08 +000041
Brian Gaeke960707c2003-11-11 22:41:34 +000042namespace llvm {
43
Chris Lattner94b8baf2002-02-05 00:33:19 +000044class BBLiveVar;
45class MachineInstr;
Ruchira Sasanka9f181192001-07-24 17:14:13 +000046
Chris Lattnerf9986852002-04-27 07:27:19 +000047class FunctionLiveVarInfo : public FunctionPass {
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000048 // Machine Instr to LiveVarSet Map for providing LVset BEFORE each inst
Vikram S. Adve26a1f5f2003-08-12 23:39:08 +000049 // These sets are owned by this map and will be freed in releaseMemory().
50 hash_map<const MachineInstr *, ValueSet *> MInst2LVSetBI;
Ruchira Sasanka9f181192001-07-24 17:14:13 +000051
Vikram S. Adve26a1f5f2003-08-12 23:39:08 +000052 // Machine Instr to LiveVarSet Map for providing LVset AFTER each inst.
53 // These sets are just pointers to sets in MInst2LVSetBI or BBLiveVar.
Chris Lattner826afe92003-10-20 20:55:13 +000054 hash_map<const MachineInstr *, ValueSet *> MInst2LVSetAI;
55
56 hash_map<const BasicBlock*, BBLiveVar*> BBLiveVarInfo;
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000057
Chris Lattner4e8c4872002-03-23 22:51:58 +000058 // Stored Function that the data is computed with respect to
59 const Function *M;
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000060
61 // --------- private methods -----------------------------------------
62
63 // constructs BBLiveVars and init Def and In sets
Chris Lattner4e8c4872002-03-23 22:51:58 +000064 void constructBBs(const Function *F);
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000065
66 // do one backward pass over the CFG
Chris Lattner4e8c4872002-03-23 22:51:58 +000067 bool doSingleBackwardPass(const Function *F, unsigned int iter);
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000068
69 // calculates live var sets for instructions in a BB
Chris Lattner339a3f62002-02-04 16:32:40 +000070 void calcLiveVarSetsForBB(const BasicBlock *BB);
Ruchira Sasanka9f181192001-07-24 17:14:13 +000071
Chris Lattner494266e2002-02-04 20:00:08 +000072public:
Chris Lattnerc8e66542002-04-27 06:56:12 +000073 // --------- Implement the FunctionPass interface ----------------------
Chris Lattner494266e2002-02-04 20:00:08 +000074
Chris Lattnerc8e66542002-04-27 06:56:12 +000075 // runOnFunction - Perform analysis, update internal data structures.
Chris Lattnerfda72b12002-06-25 16:12:52 +000076 virtual bool runOnFunction(Function &F);
Chris Lattner494266e2002-02-04 20:00:08 +000077
78 // releaseMemory - After LiveVariable analysis has been used, forget!
79 virtual void releaseMemory();
80
Chris Lattnerc8e66542002-04-27 06:56:12 +000081 // getAnalysisUsage - Provide self!
82 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
83 AU.setPreservesAll();
Chris Lattner494266e2002-02-04 20:00:08 +000084 }
85
86 // --------- Functions to access analysis results -------------------
Ruchira Sasanka9f181192001-07-24 17:14:13 +000087
Vikram S. Adve26a1f5f2003-08-12 23:39:08 +000088 // get OutSet of a BB
Chris Lattner7e5ee422002-02-05 04:20:12 +000089 const ValueSet &getOutSetOfBB(const BasicBlock *BB) const;
Vikram S. Adve26a1f5f2003-08-12 23:39:08 +000090 ValueSet &getOutSetOfBB(const BasicBlock *BB) ;
Ruchira Sasanka9f181192001-07-24 17:14:13 +000091
Vikram S. Adve26a1f5f2003-08-12 23:39:08 +000092 // get InSet of a BB
Chris Lattner7e5ee422002-02-05 04:20:12 +000093 const ValueSet &getInSetOfBB(const BasicBlock *BB) const;
Vikram S. Adve26a1f5f2003-08-12 23:39:08 +000094 ValueSet &getInSetOfBB(const BasicBlock *BB) ;
Ruchira Sasankaec1a5412001-08-20 21:11:01 +000095
Vikram S. Adve44119ac2003-07-29 19:32:04 +000096 // gets the Live var set BEFORE an instruction.
97 // if BB is specified and the live var set has not yet been computed,
98 // it will be computed on demand.
Chris Lattner7e5ee422002-02-05 04:20:12 +000099 const ValueSet &getLiveVarSetBeforeMInst(const MachineInstr *MI,
Vikram S. Adve44119ac2003-07-29 19:32:04 +0000100 const BasicBlock *BB = 0);
Ruchira Sasankaec1a5412001-08-20 21:11:01 +0000101
102 // gets the Live var set AFTER an instruction
Vikram S. Adve44119ac2003-07-29 19:32:04 +0000103 // if BB is specified and the live var set has not yet been computed,
104 // it will be computed on demand.
Chris Lattner7e5ee422002-02-05 04:20:12 +0000105 const ValueSet &getLiveVarSetAfterMInst(const MachineInstr *MI,
Vikram S. Adve44119ac2003-07-29 19:32:04 +0000106 const BasicBlock *BB = 0);
Ruchira Sasanka9f181192001-07-24 17:14:13 +0000107};
108
Brian Gaeke960707c2003-11-11 22:41:34 +0000109} // End llvm namespace
110
Ruchira Sasanka9f181192001-07-24 17:14:13 +0000111#endif