Chris Lattner | 67efeb9 | 2003-09-30 18:05:30 +0000 | [diff] [blame] | 1 | //===-- CodeGen/FunctionLiveVarInfo.h - LiveVar Analysis --------*- C++ -*-===// |
John Criswell | b644598 | 2003-10-20 20:19:47 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | 67efeb9 | 2003-09-30 18:05:30 +0000 | [diff] [blame] | 9 | // |
| 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 Sasanka | 9f18119 | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 34 | |
Chris Lattner | d858d80 | 2002-07-26 21:11:42 +0000 | [diff] [blame] | 35 | #ifndef FUNCTION_LIVE_VAR_INFO_H |
| 36 | #define FUNCTION_LIVE_VAR_INFO_H |
Ruchira Sasanka | 9f18119 | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 37 | |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/hash_map" |
Chris Lattner | 494266e | 2002-02-04 20:00:08 +0000 | [diff] [blame] | 39 | #include "llvm/Pass.h" |
Chris Lattner | de1d729 | 2003-01-14 22:56:37 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/ValueSet.h" |
Vikram S. Adve | 7e51201 | 2002-03-19 00:59:08 +0000 | [diff] [blame] | 41 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 42 | namespace llvm { |
| 43 | |
Chris Lattner | 94b8baf | 2002-02-05 00:33:19 +0000 | [diff] [blame] | 44 | class BBLiveVar; |
| 45 | class MachineInstr; |
Ruchira Sasanka | 9f18119 | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 46 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 47 | class FunctionLiveVarInfo : public FunctionPass { |
Ruchira Sasanka | ec1a541 | 2001-08-20 21:11:01 +0000 | [diff] [blame] | 48 | // Machine Instr to LiveVarSet Map for providing LVset BEFORE each inst |
Vikram S. Adve | 26a1f5f | 2003-08-12 23:39:08 +0000 | [diff] [blame] | 49 | // These sets are owned by this map and will be freed in releaseMemory(). |
| 50 | hash_map<const MachineInstr *, ValueSet *> MInst2LVSetBI; |
Ruchira Sasanka | 9f18119 | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 51 | |
Vikram S. Adve | 26a1f5f | 2003-08-12 23:39:08 +0000 | [diff] [blame] | 52 | // Machine Instr to LiveVarSet Map for providing LVset AFTER each inst. |
| 53 | // These sets are just pointers to sets in MInst2LVSetBI or BBLiveVar. |
Chris Lattner | 826afe9 | 2003-10-20 20:55:13 +0000 | [diff] [blame] | 54 | hash_map<const MachineInstr *, ValueSet *> MInst2LVSetAI; |
| 55 | |
| 56 | hash_map<const BasicBlock*, BBLiveVar*> BBLiveVarInfo; |
Ruchira Sasanka | ec1a541 | 2001-08-20 21:11:01 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 58 | // Stored Function that the data is computed with respect to |
| 59 | const Function *M; |
Ruchira Sasanka | ec1a541 | 2001-08-20 21:11:01 +0000 | [diff] [blame] | 60 | |
| 61 | // --------- private methods ----------------------------------------- |
| 62 | |
| 63 | // constructs BBLiveVars and init Def and In sets |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 64 | void constructBBs(const Function *F); |
Ruchira Sasanka | ec1a541 | 2001-08-20 21:11:01 +0000 | [diff] [blame] | 65 | |
| 66 | // do one backward pass over the CFG |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 67 | bool doSingleBackwardPass(const Function *F, unsigned int iter); |
Ruchira Sasanka | ec1a541 | 2001-08-20 21:11:01 +0000 | [diff] [blame] | 68 | |
| 69 | // calculates live var sets for instructions in a BB |
Chris Lattner | 339a3f6 | 2002-02-04 16:32:40 +0000 | [diff] [blame] | 70 | void calcLiveVarSetsForBB(const BasicBlock *BB); |
Ruchira Sasanka | 9f18119 | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 494266e | 2002-02-04 20:00:08 +0000 | [diff] [blame] | 72 | public: |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 73 | // --------- Implement the FunctionPass interface ---------------------- |
Chris Lattner | 494266e | 2002-02-04 20:00:08 +0000 | [diff] [blame] | 74 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 75 | // runOnFunction - Perform analysis, update internal data structures. |
Chris Lattner | fda72b1 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 76 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 494266e | 2002-02-04 20:00:08 +0000 | [diff] [blame] | 77 | |
| 78 | // releaseMemory - After LiveVariable analysis has been used, forget! |
| 79 | virtual void releaseMemory(); |
| 80 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 81 | // getAnalysisUsage - Provide self! |
| 82 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 83 | AU.setPreservesAll(); |
Chris Lattner | 494266e | 2002-02-04 20:00:08 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // --------- Functions to access analysis results ------------------- |
Ruchira Sasanka | 9f18119 | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 87 | |
Vikram S. Adve | 26a1f5f | 2003-08-12 23:39:08 +0000 | [diff] [blame] | 88 | // get OutSet of a BB |
Chris Lattner | 7e5ee42 | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 89 | const ValueSet &getOutSetOfBB(const BasicBlock *BB) const; |
Vikram S. Adve | 26a1f5f | 2003-08-12 23:39:08 +0000 | [diff] [blame] | 90 | ValueSet &getOutSetOfBB(const BasicBlock *BB) ; |
Ruchira Sasanka | 9f18119 | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 91 | |
Vikram S. Adve | 26a1f5f | 2003-08-12 23:39:08 +0000 | [diff] [blame] | 92 | // get InSet of a BB |
Chris Lattner | 7e5ee42 | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 93 | const ValueSet &getInSetOfBB(const BasicBlock *BB) const; |
Vikram S. Adve | 26a1f5f | 2003-08-12 23:39:08 +0000 | [diff] [blame] | 94 | ValueSet &getInSetOfBB(const BasicBlock *BB) ; |
Ruchira Sasanka | ec1a541 | 2001-08-20 21:11:01 +0000 | [diff] [blame] | 95 | |
Vikram S. Adve | 44119ac | 2003-07-29 19:32:04 +0000 | [diff] [blame] | 96 | // 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 Lattner | 7e5ee42 | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 99 | const ValueSet &getLiveVarSetBeforeMInst(const MachineInstr *MI, |
Vikram S. Adve | 44119ac | 2003-07-29 19:32:04 +0000 | [diff] [blame] | 100 | const BasicBlock *BB = 0); |
Ruchira Sasanka | ec1a541 | 2001-08-20 21:11:01 +0000 | [diff] [blame] | 101 | |
| 102 | // gets the Live var set AFTER an instruction |
Vikram S. Adve | 44119ac | 2003-07-29 19:32:04 +0000 | [diff] [blame] | 103 | // if BB is specified and the live var set has not yet been computed, |
| 104 | // it will be computed on demand. |
Chris Lattner | 7e5ee42 | 2002-02-05 04:20:12 +0000 | [diff] [blame] | 105 | const ValueSet &getLiveVarSetAfterMInst(const MachineInstr *MI, |
Vikram S. Adve | 44119ac | 2003-07-29 19:32:04 +0000 | [diff] [blame] | 106 | const BasicBlock *BB = 0); |
Ruchira Sasanka | 9f18119 | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 109 | } // End llvm namespace |
| 110 | |
Ruchira Sasanka | 9f18119 | 2001-07-24 17:14:13 +0000 | [diff] [blame] | 111 | #endif |