Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceRegAlloc.h - Linear-scan reg. allocation --*- C++ -*-===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares the data structures used during linear-scan |
| 11 | // register allocation. This includes LiveRangeWrapper which |
| 12 | // encapsulates a variable and its live range, and LinearScan which |
| 13 | // holds the various work queues for the linear-scan algorithm. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #ifndef SUBZERO_SRC_ICEREGALLOC_H |
| 18 | #define SUBZERO_SRC_ICEREGALLOC_H |
| 19 | |
| 20 | #include "IceDefs.h" |
| 21 | #include "IceTypes.h" |
| 22 | |
| 23 | namespace Ice { |
| 24 | |
| 25 | // Currently this just wraps a Variable pointer, so in principle we |
| 26 | // could use containers of Variable* instead of LiveRangeWrapper. But |
| 27 | // in the future, we may want to do more complex things such as live |
| 28 | // range splitting, and keeping a wrapper should make that simpler. |
| 29 | class LiveRangeWrapper { |
| 30 | public: |
| 31 | LiveRangeWrapper(Variable *Var) : Var(Var) {} |
| 32 | const LiveRange &range() const { return Var->getLiveRange(); } |
| 33 | bool endsBefore(const LiveRangeWrapper &Other) const { |
| 34 | return range().endsBefore(Other.range()); |
| 35 | } |
| 36 | bool overlaps(const LiveRangeWrapper &Other) const { |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 37 | const bool UseTrimmed = true; |
| 38 | return range().overlaps(Other.range(), UseTrimmed); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 39 | } |
| 40 | bool overlapsStart(const LiveRangeWrapper &Other) const { |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 41 | const bool UseTrimmed = true; |
| 42 | return range().overlapsInst(Other.range().getStart(), UseTrimmed); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 43 | } |
| 44 | Variable *const Var; |
| 45 | void dump(const Cfg *Func) const; |
| 46 | |
| 47 | private: |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 48 | // LiveRangeWrapper(const LiveRangeWrapper &) = delete; |
| 49 | LiveRangeWrapper &operator=(const LiveRangeWrapper &) = delete; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | class LinearScan { |
| 53 | public: |
| 54 | LinearScan(Cfg *Func) : Func(Func) {} |
| 55 | void scan(const llvm::SmallBitVector &RegMask); |
| 56 | void dump(Cfg *Func) const; |
| 57 | |
| 58 | private: |
| 59 | Cfg *const Func; |
| 60 | // RangeCompare is the comparator for sorting an LiveRangeWrapper |
| 61 | // by starting point in a std::set<>. Ties are broken by variable |
| 62 | // number so that sorting is stable. |
| 63 | struct RangeCompare { |
| 64 | bool operator()(const LiveRangeWrapper &L, |
| 65 | const LiveRangeWrapper &R) const { |
| 66 | InstNumberT Lstart = L.Var->getLiveRange().getStart(); |
| 67 | InstNumberT Rstart = R.Var->getLiveRange().getStart(); |
| 68 | if (Lstart == Rstart) |
| 69 | return L.Var->getIndex() < R.Var->getIndex(); |
| 70 | return Lstart < Rstart; |
| 71 | } |
| 72 | }; |
| 73 | typedef std::set<LiveRangeWrapper, RangeCompare> OrderedRanges; |
| 74 | typedef std::list<LiveRangeWrapper> UnorderedRanges; |
| 75 | OrderedRanges Unhandled; |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 76 | // UnhandledPrecolored is a subset of Unhandled, specially collected |
| 77 | // for faster processing. |
| 78 | OrderedRanges UnhandledPrecolored; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 79 | UnorderedRanges Active, Inactive, Handled; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 80 | LinearScan(const LinearScan &) = delete; |
| 81 | LinearScan &operator=(const LinearScan &) = delete; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | } // end of namespace Ice |
| 85 | |
| 86 | #endif // SUBZERO_SRC_ICEREGALLOC_H |