Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1 | /* Title: LiveRangeInfo.h -*- C++ -*- |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 2 | Author: Ruchira Sasanka |
| 3 | Date: Jun 30, 01 |
| 4 | Purpose: |
| 5 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 6 | This file contains the class LiveRangeInfo which constructs and keeps |
| 7 | the LiveRangMap which contains all the live ranges used in a method. |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 8 | |
| 9 | Assumptions: |
| 10 | |
| 11 | All variables (llvm Values) are defined before they are used. However, a |
| 12 | constant may not be defined in the machine instruction stream if it can be |
| 13 | used as an immediate value within a machine instruction. However, register |
| 14 | allocation does not have to worry about immediate constants since they |
| 15 | do not require registers. |
| 16 | |
| 17 | Since an llvm Value has a list of uses associated, it is sufficient to |
| 18 | record only the defs in a Live Range. |
| 19 | |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | #ifndef LIVE_RANGE_INFO_H |
| 24 | #define LIVE_RANGE_INFO_H |
| 25 | |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 26 | #include "llvm/Type.h" |
| 27 | #include "llvm/Method.h" |
| 28 | #include "llvm/CodeGen/MachineInstr.h" |
| 29 | |
| 30 | #include "llvm/Analysis/LiveVar/LiveVarSet.h" |
| 31 | |
| 32 | #include "llvm/CodeGen/IGNode.h" |
| 33 | #include "llvm/CodeGen/LiveRange.h" |
| 34 | #include "llvm/CodeGen/RegClass.h" |
| 35 | |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 37 | typedef std::hash_map<const Value*, LiveRange*> LiveRangeMapType; |
| 38 | typedef std::vector<const MachineInstr*> CallRetInstrListType; |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 39 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | //---------------------------------------------------------------------------- |
| 43 | // Class LiveRangeInfo |
| 44 | // |
| 45 | // Constructs and keeps the LiveRangMap which contains all the live |
| 46 | // ranges used in a method. Also contain methods to coalesce live ranges. |
| 47 | //---------------------------------------------------------------------------- |
| 48 | |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 49 | class LiveRangeInfo |
| 50 | { |
| 51 | |
| 52 | private: |
| 53 | |
| 54 | const Method *const Meth; // Method for which live range info is held |
| 55 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 56 | LiveRangeMapType LiveRangeMap; // A map from Value * to LiveRange * to |
| 57 | // record all live ranges in a method |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 58 | // created by constructLiveRanges |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 59 | |
Ruchira Sasanka | f60342a | 2001-09-15 00:33:26 +0000 | [diff] [blame] | 60 | const TargetMachine& TM; // target machine description |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 62 | std::vector<RegClass *> & RegClassList;// vector containing register classess |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 63 | |
Ruchira Sasanka | 560b0ad | 2001-09-30 23:19:57 +0000 | [diff] [blame] | 64 | const MachineRegInfo& MRI; // machine reg info |
Ruchira Sasanka | f60342a | 2001-09-15 00:33:26 +0000 | [diff] [blame] | 65 | |
Ruchira Sasanka | 560b0ad | 2001-09-30 23:19:57 +0000 | [diff] [blame] | 66 | CallRetInstrListType CallRetInstrList; // a list of all call/ret instrs |
Ruchira Sasanka | f60342a | 2001-09-15 00:33:26 +0000 | [diff] [blame] | 67 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 68 | |
| 69 | //------------ Private methods (see LiveRangeInfo.cpp for description)------- |
| 70 | |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 71 | void unionAndUpdateLRs(LiveRange *L1, LiveRange *L2); |
| 72 | |
| 73 | void addInterference(const Instruction *const Inst, |
| 74 | const LiveVarSet *const LVSet); |
| 75 | |
Ruchira Sasanka | 3353577 | 2001-10-15 16:22:44 +0000 | [diff] [blame] | 76 | void suggestRegs4CallRets(); |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 77 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 78 | const Method* getMethod() { return Meth; } |
| 79 | |
| 80 | |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 81 | public: |
| 82 | |
Ruchira Sasanka | f60342a | 2001-09-15 00:33:26 +0000 | [diff] [blame] | 83 | LiveRangeInfo(const Method *const M, |
| 84 | const TargetMachine& tm, |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 85 | std::vector<RegClass *> & RCList); |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 86 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 87 | |
| 88 | // Destructor to destroy all LiveRanges in the LiveRange Map |
| 89 | ~LiveRangeInfo(); |
| 90 | |
| 91 | // Main entry point for live range construction |
| 92 | // |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 93 | void constructLiveRanges(); |
| 94 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 95 | // This method is used to add a live range created elsewhere (e.g., |
| 96 | // in machine specific code) to the common live range map |
| 97 | // |
Ruchira Sasanka | 560b0ad | 2001-09-30 23:19:57 +0000 | [diff] [blame] | 98 | inline void addLRToMap(const Value *Val, LiveRange *LR) { |
| 99 | assert( Val && LR && "Val/LR is NULL!\n"); |
| 100 | assert( (! LiveRangeMap[ Val ]) && "LR already set in map"); |
| 101 | LiveRangeMap[ Val ] = LR; |
| 102 | } |
| 103 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 104 | // return the common live range map for this method |
| 105 | // |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 106 | inline const LiveRangeMapType *const getLiveRangeMap() const |
| 107 | { return &LiveRangeMap; } |
| 108 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 109 | // Method sed to get the corresponding live range of a Value |
| 110 | // |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 111 | inline LiveRange *getLiveRangeForValue( const Value *const Val) |
| 112 | { return LiveRangeMap[ Val ]; } |
| 113 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 114 | // Method used to get the Call and Return instruction list |
| 115 | // |
Ruchira Sasanka | 560b0ad | 2001-09-30 23:19:57 +0000 | [diff] [blame] | 116 | inline CallRetInstrListType &getCallRetInstrList() { |
| 117 | return CallRetInstrList; |
| 118 | } |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 119 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 120 | // Method for coalescing live ranges. Called only after interference info |
| 121 | // is calculated. |
| 122 | // |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 123 | void coalesceLRs(); |
| 124 | |
Ruchira Sasanka | f20079d | 2002-01-07 19:16:26 +0000 | [diff] [blame] | 125 | // debugging method to print the live ranges |
| 126 | // |
Ruchira Sasanka | e5d0fb8 | 2001-09-08 14:10:34 +0000 | [diff] [blame] | 127 | void printLiveRanges(); |
| 128 | |
| 129 | }; |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | #endif |