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 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Declares the LinearScan data structure used during linear-scan |
| 12 | /// register allocation. |
| 13 | /// |
| 14 | /// This holds the various work queues for the linear-scan algorithm. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 15 | /// |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #ifndef SUBZERO_SRC_ICEREGALLOC_H |
| 19 | #define SUBZERO_SRC_ICEREGALLOC_H |
| 20 | |
| 21 | #include "IceDefs.h" |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 22 | #include "IceBitVector.h" |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 23 | #include "IceOperand.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 24 | #include "IceTypes.h" |
| 25 | |
| 26 | namespace Ice { |
| 27 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 28 | class LinearScan { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 29 | LinearScan() = delete; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 30 | LinearScan(const LinearScan &) = delete; |
| 31 | LinearScan &operator=(const LinearScan &) = delete; |
| 32 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 33 | public: |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 34 | explicit LinearScan(Cfg *Func); |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 35 | void init(RegAllocKind Kind, CfgSet<Variable *> ExcludeVars = {}); |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 36 | void scan(const SmallBitVector &RegMask, bool Randomized); |
Jim Stichnoth | 4001c93 | 2015-10-09 14:33:26 -0700 | [diff] [blame] | 37 | // Returns the number of times some variable has been assigned a register but |
| 38 | // later evicted because of a higher-priority allocation. The idea is that we |
| 39 | // can implement "second-chance bin-packing" by rerunning register allocation |
| 40 | // until there are no more evictions. |
| 41 | SizeT getNumEvictions() const { return Evicted.size(); } |
| 42 | bool hasEvictions() const { return !Evicted.empty(); } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 43 | void dump(Cfg *Func) const; |
| 44 | |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 45 | // TODO(stichnot): Statically choose the size based on the target being |
Jim Stichnoth | 843142f | 2016-03-09 15:19:40 -0800 | [diff] [blame] | 46 | // compiled. For now, choose a value large enough to fit into the |
| 47 | // SmallVector's fixed portion, which is 32 for x86-32, 84 for x86-64, and 102 |
| 48 | // for ARM32. |
| 49 | static constexpr size_t REGS_SIZE = 128; |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 50 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 51 | private: |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 52 | using OrderedRanges = CfgVector<Variable *>; |
| 53 | using UnorderedRanges = CfgVector<Variable *>; |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 54 | using DefUseErrorList = llvm::SmallVector<SizeT, 10>; |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 55 | |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 56 | class IterationState { |
| 57 | IterationState(const IterationState &) = delete; |
| 58 | IterationState operator=(const IterationState &) = delete; |
| 59 | |
| 60 | public: |
| 61 | IterationState() = default; |
| 62 | Variable *Cur = nullptr; |
| 63 | Variable *Prefer = nullptr; |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 64 | RegNumT PreferReg; |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 65 | bool AllowOverlap = false; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 66 | SmallBitVector RegMask; |
| 67 | SmallBitVector RegMaskUnfiltered; |
| 68 | SmallBitVector Free; |
| 69 | SmallBitVector FreeUnfiltered; |
| 70 | SmallBitVector PrecoloredUnhandledMask; // Note: only used for dumping |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 71 | llvm::SmallVector<RegWeight, REGS_SIZE> Weights; |
| 72 | }; |
| 73 | |
Jim Stichnoth | 230d410 | 2015-09-25 17:40:32 -0700 | [diff] [blame] | 74 | bool livenessValidateIntervals(const DefUseErrorList &DefsWithoutUses, |
| 75 | const DefUseErrorList &UsesBeforeDefs, |
| 76 | const CfgVector<InstNumberT> &LRBegin, |
Jim Stichnoth | 318f4cd | 2015-10-01 21:02:37 -0700 | [diff] [blame] | 77 | const CfgVector<InstNumberT> &LREnd) const; |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 78 | void initForGlobal(); |
| 79 | void initForInfOnly(); |
Jim Stichnoth | 4001c93 | 2015-10-09 14:33:26 -0700 | [diff] [blame] | 80 | void initForSecondChance(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 81 | /// Move an item from the From set to the To set. From[Index] is pushed onto |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 82 | /// the end of To[], then the item is efficiently removed from From[] by |
| 83 | /// effectively swapping it with the last item in From[] and then popping it |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 84 | /// from the back. As such, the caller is best off iterating over From[] in |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 85 | /// reverse order to avoid the need for special handling of the iterator. |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 86 | void moveItem(UnorderedRanges &From, SizeT Index, UnorderedRanges &To) { |
| 87 | To.push_back(From[Index]); |
| 88 | From[Index] = From.back(); |
| 89 | From.pop_back(); |
| 90 | } |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 91 | |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 92 | /// \name scan helper functions. |
| 93 | /// @{ |
| 94 | /// Free up a register for infinite-weight Cur by spilling and reloading some |
| 95 | /// register that isn't used during Cur's live range. |
| 96 | void addSpillFill(IterationState &Iter); |
| 97 | /// Check for active ranges that have expired or become inactive. |
| 98 | void handleActiveRangeExpiredOrInactive(const Variable *Cur); |
| 99 | /// Check for inactive ranges that have expired or reactivated. |
| 100 | void handleInactiveRangeExpiredOrReactivated(const Variable *Cur); |
| 101 | void findRegisterPreference(IterationState &Iter); |
| 102 | void filterFreeWithInactiveRanges(IterationState &Iter); |
| 103 | void filterFreeWithPrecoloredRanges(IterationState &Iter); |
| 104 | void allocatePrecoloredRegister(Variable *Cur); |
| 105 | void allocatePreferredRegister(IterationState &Iter); |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 106 | void allocateFreeRegister(IterationState &Iter, bool Filtered); |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 107 | void handleNoFreeRegisters(IterationState &Iter); |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 108 | void assignFinalRegisters(const SmallBitVector &RegMaskFull, |
| 109 | const SmallBitVector &PreDefinedRegisters, |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 110 | bool Randomized); |
| 111 | /// @} |
| 112 | |
| 113 | void dumpLiveRangeTrace(const char *Label, const Variable *Item); |
| 114 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 115 | Cfg *const Func; |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 116 | GlobalContext *const Ctx; |
John Porto | bb0a5fe | 2015-09-04 11:23:41 -0700 | [diff] [blame] | 117 | TargetLowering *const Target; |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 118 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 119 | OrderedRanges Unhandled; |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 120 | /// UnhandledPrecolored is a subset of Unhandled, specially collected for |
| 121 | /// faster processing. |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 122 | OrderedRanges UnhandledPrecolored; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 123 | UnorderedRanges Active, Inactive, Handled; |
Jim Stichnoth | 4001c93 | 2015-10-09 14:33:26 -0700 | [diff] [blame] | 124 | UnorderedRanges Evicted; |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 125 | CfgVector<InstNumberT> Kills; |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 126 | RegAllocKind Kind = RAK_Unknown; |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 127 | /// RegUses[I] is the number of live ranges (variables) that register I is |
| 128 | /// currently assigned to. It can be greater than 1 as a result of |
| 129 | /// AllowOverlap inference. |
| 130 | llvm::SmallVector<int32_t, REGS_SIZE> RegUses; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 131 | llvm::SmallVector<const SmallBitVector *, REGS_SIZE> RegAliases; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 132 | bool FindPreference = false; |
| 133 | bool FindOverlap = false; |
Andrew Scull | d24cfda | 2015-08-25 10:31:15 -0700 | [diff] [blame] | 134 | const bool Verbose; |
Jim Stichnoth | b40595a | 2016-01-29 06:14:31 -0800 | [diff] [blame] | 135 | const bool UseReserve; |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 136 | CfgVector<Variable *> Vars; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | } // end of namespace Ice |
| 140 | |
| 141 | #endif // SUBZERO_SRC_ICEREGALLOC_H |