| OLD | NEW |
| 1 //===- subzero/src/IceRegAlloc.h - Linear-scan reg. allocation --*- C++ -*-===// | 1 //===- subzero/src/IceRegAlloc.h - Linear-scan reg. allocation --*- C++ -*-===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 /// | 9 /// |
| 10 /// \file | 10 /// \file |
| 11 /// This file declares the LinearScan data structure used during | 11 /// This file declares the LinearScan data structure used during linear-scan |
| 12 /// linear-scan register allocation, which holds the various work | 12 /// register allocation, which holds the various work queues for the linear-scan |
| 13 /// queues for the linear-scan algorithm. | 13 /// algorithm. |
| 14 /// | 14 /// |
| 15 //===----------------------------------------------------------------------===// | 15 //===----------------------------------------------------------------------===// |
| 16 | 16 |
| 17 #ifndef SUBZERO_SRC_ICEREGALLOC_H | 17 #ifndef SUBZERO_SRC_ICEREGALLOC_H |
| 18 #define SUBZERO_SRC_ICEREGALLOC_H | 18 #define SUBZERO_SRC_ICEREGALLOC_H |
| 19 | 19 |
| 20 #include "IceDefs.h" | 20 #include "IceDefs.h" |
| 21 #include "IceOperand.h" |
| 21 #include "IceTypes.h" | 22 #include "IceTypes.h" |
| 22 | 23 |
| 23 namespace Ice { | 24 namespace Ice { |
| 24 | 25 |
| 25 class LinearScan { | 26 class LinearScan { |
| 26 LinearScan() = delete; | 27 LinearScan() = delete; |
| 27 LinearScan(const LinearScan &) = delete; | 28 LinearScan(const LinearScan &) = delete; |
| 28 LinearScan &operator=(const LinearScan &) = delete; | 29 LinearScan &operator=(const LinearScan &) = delete; |
| 29 | 30 |
| 30 public: | 31 public: |
| 31 explicit LinearScan(Cfg *Func) : Func(Func) {} | 32 explicit LinearScan(Cfg *Func); |
| 32 void init(RegAllocKind Kind); | 33 void init(RegAllocKind Kind); |
| 33 void scan(const llvm::SmallBitVector &RegMask, bool Randomized); | 34 void scan(const llvm::SmallBitVector &RegMask, bool Randomized); |
| 34 void dump(Cfg *Func) const; | 35 void dump(Cfg *Func) const; |
| 35 | 36 |
| 37 // TODO(stichnot): Statically choose the size based on the target being |
| 38 // compiled. |
| 39 static constexpr size_t REGS_SIZE = 32; |
| 40 |
| 36 private: | 41 private: |
| 37 typedef std::vector<Variable *> OrderedRanges; | 42 typedef std::vector<Variable *> OrderedRanges; |
| 38 typedef std::vector<Variable *> UnorderedRanges; | 43 typedef std::vector<Variable *> UnorderedRanges; |
| 39 | 44 |
| 45 class IterationState { |
| 46 IterationState(const IterationState &) = delete; |
| 47 IterationState operator=(const IterationState &) = delete; |
| 48 |
| 49 public: |
| 50 IterationState() = default; |
| 51 Variable *Cur = nullptr; |
| 52 Variable *Prefer = nullptr; |
| 53 int32_t PreferReg = Variable::NoRegister; |
| 54 bool AllowOverlap = false; |
| 55 llvm::SmallBitVector RegMask; |
| 56 llvm::SmallBitVector Free; |
| 57 llvm::SmallBitVector PrecoloredUnhandledMask; // Note: only used for dumping |
| 58 llvm::SmallVector<RegWeight, REGS_SIZE> Weights; |
| 59 }; |
| 60 |
| 40 void initForGlobal(); | 61 void initForGlobal(); |
| 41 void initForInfOnly(); | 62 void initForInfOnly(); |
| 42 /// Free up a register for infinite-weight Cur by spilling and reloading some | 63 /// Move an item from the From set to the To set. From[Index] is pushed onto |
| 43 /// register that isn't used during Cur's live range. | 64 /// the end of To[], then the item is efficiently removed from From[] by |
| 44 void addSpillFill(Variable *Cur, llvm::SmallBitVector RegMask); | 65 /// effectively swapping it with the last item in From[] and then popping it |
| 45 /// Move an item from the From set to the To set. From[Index] is | 66 /// from the back. As such, the caller is best off iterating over From[] in |
| 46 /// pushed onto the end of To[], then the item is efficiently removed | 67 /// reverse order to avoid the need for special handling of the iterator. |
| 47 /// from From[] by effectively swapping it with the last item in | |
| 48 /// From[] and then popping it from the back. As such, the caller is | |
| 49 /// best off iterating over From[] in reverse order to avoid the need | |
| 50 /// for special handling of the iterator. | |
| 51 void moveItem(UnorderedRanges &From, SizeT Index, UnorderedRanges &To) { | 68 void moveItem(UnorderedRanges &From, SizeT Index, UnorderedRanges &To) { |
| 52 To.push_back(From[Index]); | 69 To.push_back(From[Index]); |
| 53 From[Index] = From.back(); | 70 From[Index] = From.back(); |
| 54 From.pop_back(); | 71 From.pop_back(); |
| 55 } | 72 } |
| 56 | 73 |
| 74 /// \name scan helper functions. |
| 75 /// @{ |
| 76 /// Free up a register for infinite-weight Cur by spilling and reloading some |
| 77 /// register that isn't used during Cur's live range. |
| 78 void addSpillFill(IterationState &Iter); |
| 79 /// Check for active ranges that have expired or become inactive. |
| 80 void handleActiveRangeExpiredOrInactive(const Variable *Cur); |
| 81 /// Check for inactive ranges that have expired or reactivated. |
| 82 void handleInactiveRangeExpiredOrReactivated(const Variable *Cur); |
| 83 void findRegisterPreference(IterationState &Iter); |
| 84 void filterFreeWithInactiveRanges(IterationState &Iter); |
| 85 void filterFreeWithPrecoloredRanges(IterationState &Iter); |
| 86 void allocatePrecoloredRegister(Variable *Cur); |
| 87 void allocatePreferredRegister(IterationState &Iter); |
| 88 void allocateFreeRegister(IterationState &Iter); |
| 89 void handleNoFreeRegisters(IterationState &Iter); |
| 90 void assignFinalRegisters(const llvm::SmallBitVector &RegMaskFull, |
| 91 const llvm::SmallBitVector &PreDefinedRegisters, |
| 92 bool Randomized); |
| 93 /// @} |
| 94 |
| 95 void dumpLiveRangeTrace(const char *Label, const Variable *Item); |
| 96 |
| 57 Cfg *const Func; | 97 Cfg *const Func; |
| 98 GlobalContext *const Ctx; |
| 99 |
| 58 OrderedRanges Unhandled; | 100 OrderedRanges Unhandled; |
| 59 /// UnhandledPrecolored is a subset of Unhandled, specially collected | 101 /// UnhandledPrecolored is a subset of Unhandled, specially collected for |
| 60 /// for faster processing. | 102 /// faster processing. |
| 61 OrderedRanges UnhandledPrecolored; | 103 OrderedRanges UnhandledPrecolored; |
| 62 UnorderedRanges Active, Inactive, Handled; | 104 UnorderedRanges Active, Inactive, Handled; |
| 63 std::vector<InstNumberT> Kills; | 105 std::vector<InstNumberT> Kills; |
| 64 RegAllocKind Kind = RAK_Unknown; | 106 RegAllocKind Kind = RAK_Unknown; |
| 107 /// RegUses[I] is the number of live ranges (variables) that register I is |
| 108 /// currently assigned to. It can be greater than 1 as a result of |
| 109 /// AllowOverlap inference. |
| 110 llvm::SmallVector<int32_t, REGS_SIZE> RegUses; |
| 65 bool FindPreference = false; | 111 bool FindPreference = false; |
| 66 bool FindOverlap = false; | 112 bool FindOverlap = false; |
| 113 |
| 114 const bool Verbose; |
| 67 }; | 115 }; |
| 68 | 116 |
| 69 } // end of namespace Ice | 117 } // end of namespace Ice |
| 70 | 118 |
| 71 #endif // SUBZERO_SRC_ICEREGALLOC_H | 119 #endif // SUBZERO_SRC_ICEREGALLOC_H |
| OLD | NEW |