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