Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceRegAlloc.cpp - Linear-scan implementation -----------===// |
| 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 implements the LinearScan class, which performs the |
| 11 | // linear-scan register allocation after liveness analysis has been |
| 12 | // performed. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "IceCfg.h" |
| 17 | #include "IceInst.h" |
| 18 | #include "IceOperand.h" |
| 19 | #include "IceRegAlloc.h" |
| 20 | #include "IceTargetLowering.h" |
| 21 | |
| 22 | namespace Ice { |
| 23 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | // Returns true if Var has any definitions within Item's live range. |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 27 | // TODO(stichnot): Consider trimming the Definitions list similar to |
| 28 | // how the live ranges are trimmed, since all the overlapsDefs() tests |
| 29 | // are whether some variable's definitions overlap Cur, and trimming |
| 30 | // is with respect Cur.start. Initial tests show no measurable |
| 31 | // performance difference, so we'll keep the code simple for now. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 32 | bool overlapsDefs(const Cfg *Func, const Variable *Item, const Variable *Var) { |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 33 | const bool UseTrimmed = true; |
| 34 | VariablesMetadata *VMetadata = Func->getVMetadata(); |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame^] | 35 | if (const Inst *FirstDef = VMetadata->getFirstDefinition(Var)) |
| 36 | if (Item->getLiveRange().overlapsInst(FirstDef->getNumber(), UseTrimmed)) |
| 37 | return true; |
| 38 | const InstDefList &Defs = VMetadata->getLatterDefinitions(Var); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 39 | for (size_t i = 0; i < Defs.size(); ++i) { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 40 | if (Item->getLiveRange().overlapsInst(Defs[i]->getNumber(), UseTrimmed)) |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 41 | return true; |
| 42 | } |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | void dumpDisableOverlap(const Cfg *Func, const Variable *Var, |
| 47 | const char *Reason) { |
| 48 | if (Func->getContext()->isVerbose(IceV_LinearScan)) { |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 49 | VariablesMetadata *VMetadata = Func->getVMetadata(); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 50 | Ostream &Str = Func->getContext()->getStrDump(); |
| 51 | Str << "Disabling Overlap due to " << Reason << " " << *Var |
| 52 | << " LIVE=" << Var->getLiveRange() << " Defs="; |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame^] | 53 | if (const Inst *FirstDef = VMetadata->getFirstDefinition(Var)) |
| 54 | Str << FirstDef->getNumber(); |
| 55 | const InstDefList &Defs = VMetadata->getLatterDefinitions(Var); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 56 | for (size_t i = 0; i < Defs.size(); ++i) { |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame^] | 57 | Str << "," << Defs[i]->getNumber(); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 58 | } |
| 59 | Str << "\n"; |
| 60 | } |
| 61 | } |
| 62 | |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 63 | void dumpLiveRange(const Variable *Var, const Cfg *Func) { |
| 64 | Ostream &Str = Func->getContext()->getStrDump(); |
| 65 | const static size_t BufLen = 30; |
| 66 | char buf[BufLen]; |
| 67 | snprintf(buf, BufLen, "%2d", Var->getRegNumTmp()); |
| 68 | Str << "R=" << buf << " V="; |
| 69 | Var->dump(Func); |
| 70 | Str << " Range=" << Var->getLiveRange(); |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 73 | } // end of anonymous namespace |
| 74 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 75 | // Implements the linear-scan algorithm. Based on "Linear Scan |
| 76 | // Register Allocation in the Context of SSA Form and Register |
| 77 | // Constraints" by Hanspeter Mössenböck and Michael Pfeiffer, |
| 78 | // ftp://ftp.ssw.uni-linz.ac.at/pub/Papers/Moe02.PDF . This |
| 79 | // implementation is modified to take affinity into account and allow |
| 80 | // two interfering variables to share the same register in certain |
| 81 | // cases. |
| 82 | // |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 83 | // Requires running Cfg::liveness(Liveness_Intervals) in |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 84 | // preparation. Results are assigned to Variable::RegNum for each |
| 85 | // Variable. |
| 86 | void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 87 | TimerMarker T(TimerStack::TT_linearScan, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 88 | assert(RegMaskFull.any()); // Sanity check |
| 89 | Unhandled.clear(); |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 90 | UnhandledPrecolored.clear(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 91 | Handled.clear(); |
| 92 | Inactive.clear(); |
| 93 | Active.clear(); |
| 94 | Ostream &Str = Func->getContext()->getStrDump(); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 95 | bool Verbose = Func->getContext()->isVerbose(IceV_LinearScan); |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 96 | Func->resetCurrentNode(); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 97 | VariablesMetadata *VMetadata = Func->getVMetadata(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 98 | |
| 99 | // Gather the live ranges of all variables and add them to the |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 100 | // Unhandled set. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 101 | const VarList &Vars = Func->getVariables(); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 102 | { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 103 | TimerMarker T(TimerStack::TT_initUnhandled, Func); |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 104 | Unhandled.reserve(Vars.size()); |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 105 | for (Variable *Var : Vars) { |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 106 | // Explicitly don't consider zero-weight variables, which are |
| 107 | // meant to be spill slots. |
| 108 | if (Var->getWeight() == RegWeight::Zero) |
| 109 | continue; |
| 110 | // Don't bother if the variable has a null live range, which means |
| 111 | // it was never referenced. |
| 112 | if (Var->getLiveRange().isEmpty()) |
| 113 | continue; |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 114 | Var->untrimLiveRange(); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 115 | Unhandled.push_back(Var); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 116 | if (Var->hasReg()) { |
| 117 | Var->setRegNumTmp(Var->getRegNum()); |
| 118 | Var->setLiveRangeInfiniteWeight(); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 119 | UnhandledPrecolored.push_back(Var); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 120 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 121 | } |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 122 | struct CompareRanges { |
| 123 | bool operator()(const Variable *L, const Variable *R) { |
| 124 | InstNumberT Lstart = L->getLiveRange().getStart(); |
| 125 | InstNumberT Rstart = R->getLiveRange().getStart(); |
| 126 | if (Lstart == Rstart) |
| 127 | return L->getIndex() < R->getIndex(); |
| 128 | return Lstart < Rstart; |
| 129 | } |
| 130 | }; |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 131 | // Do a reverse sort so that erasing elements (from the end) is fast. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 132 | std::sort(Unhandled.rbegin(), Unhandled.rend(), CompareRanges()); |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 133 | std::sort(UnhandledPrecolored.rbegin(), UnhandledPrecolored.rend(), |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 134 | CompareRanges()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // RegUses[I] is the number of live ranges (variables) that register |
| 138 | // I is currently assigned to. It can be greater than 1 as a result |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 139 | // of AllowOverlap inference below. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 140 | std::vector<int> RegUses(RegMaskFull.size()); |
| 141 | // Unhandled is already set to all ranges in increasing order of |
| 142 | // start points. |
| 143 | assert(Active.empty()); |
| 144 | assert(Inactive.empty()); |
| 145 | assert(Handled.empty()); |
| 146 | UnorderedRanges::iterator Next; |
| 147 | |
| 148 | while (!Unhandled.empty()) { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 149 | Variable *Cur = Unhandled.back(); |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 150 | Unhandled.pop_back(); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 151 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 152 | Str << "\nConsidering "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 153 | dumpLiveRange(Cur, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 154 | Str << "\n"; |
| 155 | } |
| 156 | const llvm::SmallBitVector RegMask = |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 157 | RegMaskFull & Func->getTarget()->getRegisterSetForType(Cur->getType()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 158 | |
| 159 | // Check for precolored ranges. If Cur is precolored, it |
| 160 | // definitely gets that register. Previously processed live |
| 161 | // ranges would have avoided that register due to it being |
| 162 | // precolored. Future processed live ranges won't evict that |
| 163 | // register because the live range has infinite weight. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 164 | if (Cur->hasReg()) { |
| 165 | int32_t RegNum = Cur->getRegNum(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 166 | // RegNumTmp should have already been set above. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 167 | assert(Cur->getRegNumTmp() == RegNum); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 168 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 169 | Str << "Precoloring "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 170 | dumpLiveRange(Cur, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 171 | Str << "\n"; |
| 172 | } |
| 173 | Active.push_back(Cur); |
| 174 | assert(RegUses[RegNum] >= 0); |
| 175 | ++RegUses[RegNum]; |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 176 | assert(!UnhandledPrecolored.empty()); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 177 | assert(UnhandledPrecolored.back() == Cur); |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 178 | UnhandledPrecolored.pop_back(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 179 | continue; |
| 180 | } |
| 181 | |
| 182 | // Check for active ranges that have expired or become inactive. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 183 | for (auto I = Active.begin(), E = Active.end(); I != E; I = Next) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 184 | Next = I; |
| 185 | ++Next; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 186 | Variable *Item = *I; |
| 187 | Item->trimLiveRange(Cur->getLiveRange().getStart()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 188 | bool Moved = false; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 189 | if (Item->rangeEndsBefore(Cur)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 190 | // Move Item from Active to Handled list. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 191 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 192 | Str << "Expiring "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 193 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 194 | Str << "\n"; |
| 195 | } |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 196 | Handled.splice(Handled.end(), Active, I); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 197 | Moved = true; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 198 | } else if (!Item->rangeOverlapsStart(Cur)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 199 | // Move Item from Active to Inactive list. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 200 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 201 | Str << "Inactivating "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 202 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 203 | Str << "\n"; |
| 204 | } |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 205 | Inactive.splice(Inactive.end(), Active, I); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 206 | Moved = true; |
| 207 | } |
| 208 | if (Moved) { |
| 209 | // Decrement Item from RegUses[]. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 210 | assert(Item->hasRegTmp()); |
| 211 | int32_t RegNum = Item->getRegNumTmp(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 212 | --RegUses[RegNum]; |
| 213 | assert(RegUses[RegNum] >= 0); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Check for inactive ranges that have expired or reactivated. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 218 | for (auto I = Inactive.begin(), E = Inactive.end(); I != E; I = Next) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 219 | Next = I; |
| 220 | ++Next; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 221 | Variable *Item = *I; |
| 222 | Item->trimLiveRange(Cur->getLiveRange().getStart()); |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 223 | // As an optimization, don't bother checking pure point-valued |
| 224 | // Inactive ranges, because the overlapsStart() test will never |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 225 | // succeed, and the rangeEndsBefore() test will generally only |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 226 | // succeed after the last call instruction, which statistically |
| 227 | // happens near the end. TODO(stichnot): Consider suppressing |
| 228 | // this check every N iterations in case calls are only at the |
| 229 | // beginning of the function. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 230 | if (!Item->getLiveRange().isNonpoints()) |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 231 | continue; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 232 | if (Item->rangeEndsBefore(Cur)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 233 | // Move Item from Inactive to Handled list. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 234 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 235 | Str << "Expiring "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 236 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 237 | Str << "\n"; |
| 238 | } |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 239 | Handled.splice(Handled.end(), Inactive, I); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 240 | } else if (Item->rangeOverlapsStart(Cur)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 241 | // Move Item from Inactive to Active list. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 242 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 243 | Str << "Reactivating "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 244 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 245 | Str << "\n"; |
| 246 | } |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 247 | Active.splice(Active.end(), Inactive, I); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 248 | // Increment Item in RegUses[]. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 249 | assert(Item->hasRegTmp()); |
| 250 | int32_t RegNum = Item->getRegNumTmp(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 251 | assert(RegUses[RegNum] >= 0); |
| 252 | ++RegUses[RegNum]; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // Calculate available registers into Free[]. |
| 257 | llvm::SmallBitVector Free = RegMask; |
| 258 | for (SizeT i = 0; i < RegMask.size(); ++i) { |
| 259 | if (RegUses[i] > 0) |
| 260 | Free[i] = false; |
| 261 | } |
| 262 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 263 | // Infer register preference and allowable overlap. Only form a |
| 264 | // preference when the current Variable has an unambiguous "first" |
| 265 | // definition. The preference is some source Variable of the |
| 266 | // defining instruction that either is assigned a register that is |
| 267 | // currently free, or that is assigned a register that is not free |
| 268 | // but overlap is allowed. Overlap is allowed when the Variable |
| 269 | // under consideration is single-definition, and its definition is |
| 270 | // a simple assignment - i.e., the register gets copied/aliased |
| 271 | // but is never modified. Furthermore, overlap is only allowed |
| 272 | // when preferred Variable definition instructions do not appear |
| 273 | // within the current Variable's live range. |
| 274 | Variable *Prefer = NULL; |
| 275 | int32_t PreferReg = Variable::NoRegister; |
| 276 | bool AllowOverlap = false; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 277 | if (const Inst *DefInst = VMetadata->getFirstDefinition(Cur)) { |
| 278 | assert(DefInst->getDest() == Cur); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 279 | bool IsAssign = DefInst->isSimpleAssign(); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 280 | bool IsSingleDef = !VMetadata->isMultiDef(Cur); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 281 | for (SizeT i = 0; i < DefInst->getSrcSize(); ++i) { |
| 282 | // TODO(stichnot): Iterate through the actual Variables of the |
| 283 | // instruction, not just the source operands. This could |
| 284 | // capture Load instructions, including address mode |
| 285 | // optimization, for Prefer (but not for AllowOverlap). |
| 286 | if (Variable *SrcVar = llvm::dyn_cast<Variable>(DefInst->getSrc(i))) { |
| 287 | int32_t SrcReg = SrcVar->getRegNumTmp(); |
| 288 | // Only consider source variables that have (so far) been |
| 289 | // assigned a register. That register must be one in the |
| 290 | // RegMask set, e.g. don't try to prefer the stack pointer |
| 291 | // as a result of the stacksave intrinsic. |
| 292 | if (SrcVar->hasRegTmp() && RegMask[SrcReg]) { |
| 293 | if (!Free[SrcReg]) { |
| 294 | // Don't bother trying to enable AllowOverlap if the |
| 295 | // register is already free. |
| 296 | AllowOverlap = |
| 297 | IsSingleDef && IsAssign && !overlapsDefs(Func, Cur, SrcVar); |
| 298 | } |
| 299 | if (AllowOverlap || Free[SrcReg]) { |
| 300 | Prefer = SrcVar; |
| 301 | PreferReg = SrcReg; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 307 | if (Verbose) { |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 308 | if (Prefer) { |
| 309 | Str << "Initial Prefer=" << *Prefer << " R=" << PreferReg |
| 310 | << " LIVE=" << Prefer->getLiveRange() << " Overlap=" << AllowOverlap |
| 311 | << "\n"; |
| 312 | } |
| 313 | } |
| 314 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 315 | // Remove registers from the Free[] list where an Inactive range |
| 316 | // overlaps with the current range. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 317 | for (const Variable *Item : Inactive) { |
| 318 | if (Item->rangeOverlaps(Cur)) { |
| 319 | int32_t RegNum = Item->getRegNumTmp(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 320 | // Don't assert(Free[RegNum]) because in theory (though |
| 321 | // probably never in practice) there could be two inactive |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 322 | // variables that were marked with AllowOverlap. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 323 | Free[RegNum] = false; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 324 | // Disable AllowOverlap if an Inactive variable, which is not |
| 325 | // Prefer, shares Prefer's register, and has a definition |
| 326 | // within Cur's live range. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 327 | if (AllowOverlap && Item != Prefer && RegNum == PreferReg && |
| 328 | overlapsDefs(Func, Cur, Item)) { |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 329 | AllowOverlap = false; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 330 | dumpDisableOverlap(Func, Item, "Inactive"); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // Disable AllowOverlap if an Active variable, which is not |
| 336 | // Prefer, shares Prefer's register, and has a definition within |
| 337 | // Cur's live range. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 338 | for (const Variable *Item : Active) { |
| 339 | int32_t RegNum = Item->getRegNumTmp(); |
| 340 | if (Item != Prefer && RegNum == PreferReg && |
| 341 | overlapsDefs(Func, Cur, Item)) { |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 342 | AllowOverlap = false; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 343 | dumpDisableOverlap(Func, Item, "Active"); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 347 | std::vector<RegWeight> Weights(RegMask.size()); |
| 348 | |
| 349 | // Remove registers from the Free[] list where an Unhandled |
| 350 | // precolored range overlaps with the current range, and set those |
| 351 | // registers to infinite weight so that they aren't candidates for |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 352 | // eviction. Cur->rangeEndsBefore(Item) is an early exit check |
| 353 | // that turns a guaranteed O(N^2) algorithm into expected linear |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 354 | // complexity. |
| 355 | llvm::SmallBitVector PrecoloredUnhandledMask(RegMask.size()); |
| 356 | // Note: PrecoloredUnhandledMask is only used for dumping. |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 357 | for (auto I = UnhandledPrecolored.rbegin(), E = UnhandledPrecolored.rend(); |
| 358 | I != E; ++I) { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 359 | Variable *Item = *I; |
| 360 | assert(Item->hasReg()); |
| 361 | if (Cur->rangeEndsBefore(Item)) |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 362 | break; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 363 | if (Item->rangeOverlaps(Cur)) { |
| 364 | int32_t ItemReg = Item->getRegNum(); // Note: not getRegNumTmp() |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 365 | Weights[ItemReg].setWeight(RegWeight::Inf); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 366 | Free[ItemReg] = false; |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 367 | PrecoloredUnhandledMask[ItemReg] = true; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 368 | // Disable AllowOverlap if the preferred register is one of |
| 369 | // these precolored unhandled overlapping ranges. |
| 370 | if (AllowOverlap && ItemReg == PreferReg) { |
| 371 | AllowOverlap = false; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 372 | dumpDisableOverlap(Func, Item, "PrecoloredUnhandled"); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 373 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | // Print info about physical register availability. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 378 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 379 | for (SizeT i = 0; i < RegMask.size(); ++i) { |
| 380 | if (RegMask[i]) { |
| 381 | Str << Func->getTarget()->getRegName(i, IceType_i32) |
Jim Stichnoth | ca662e9 | 2014-07-10 15:32:36 -0700 | [diff] [blame] | 382 | << "(U=" << RegUses[i] << ",F=" << Free[i] |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 383 | << ",P=" << PrecoloredUnhandledMask[i] << ") "; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | Str << "\n"; |
| 387 | } |
| 388 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 389 | if (Prefer && (AllowOverlap || Free[PreferReg])) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 390 | // First choice: a preferred register that is either free or is |
| 391 | // allowed to overlap with its linked variable. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 392 | Cur->setRegNumTmp(PreferReg); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 393 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 394 | Str << "Preferring "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 395 | dumpLiveRange(Cur, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 396 | Str << "\n"; |
| 397 | } |
| 398 | assert(RegUses[PreferReg] >= 0); |
| 399 | ++RegUses[PreferReg]; |
| 400 | Active.push_back(Cur); |
| 401 | } else if (Free.any()) { |
| 402 | // Second choice: any free register. TODO: After explicit |
| 403 | // affinity is considered, is there a strategy better than just |
| 404 | // picking the lowest-numbered available register? |
| 405 | int32_t RegNum = Free.find_first(); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 406 | Cur->setRegNumTmp(RegNum); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 407 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 408 | Str << "Allocating "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 409 | dumpLiveRange(Cur, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 410 | Str << "\n"; |
| 411 | } |
| 412 | assert(RegUses[RegNum] >= 0); |
| 413 | ++RegUses[RegNum]; |
| 414 | Active.push_back(Cur); |
| 415 | } else { |
| 416 | // Fallback: there are no free registers, so we look for the |
| 417 | // lowest-weight register and see if Cur has higher weight. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 418 | // Check Active ranges. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 419 | for (const Variable *Item : Active) { |
| 420 | assert(Item->rangeOverlaps(Cur)); |
| 421 | int32_t RegNum = Item->getRegNumTmp(); |
| 422 | assert(Item->hasRegTmp()); |
| 423 | Weights[RegNum].addWeight(Item->getLiveRange().getWeight()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 424 | } |
| 425 | // Same as above, but check Inactive ranges instead of Active. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 426 | for (const Variable *Item : Inactive) { |
| 427 | int32_t RegNum = Item->getRegNumTmp(); |
| 428 | assert(Item->hasRegTmp()); |
| 429 | if (Item->rangeOverlaps(Cur)) |
| 430 | Weights[RegNum].addWeight(Item->getLiveRange().getWeight()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 431 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 432 | |
| 433 | // All the weights are now calculated. Find the register with |
| 434 | // smallest weight. |
| 435 | int32_t MinWeightIndex = RegMask.find_first(); |
| 436 | // MinWeightIndex must be valid because of the initial |
| 437 | // RegMask.any() test. |
| 438 | assert(MinWeightIndex >= 0); |
| 439 | for (SizeT i = MinWeightIndex + 1; i < Weights.size(); ++i) { |
| 440 | if (RegMask[i] && Weights[i] < Weights[MinWeightIndex]) |
| 441 | MinWeightIndex = i; |
| 442 | } |
| 443 | |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 444 | if (Cur->getLiveRange().getWeight() <= Weights[MinWeightIndex]) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 445 | // Cur doesn't have priority over any other live ranges, so |
| 446 | // don't allocate any register to it, and move it to the |
| 447 | // Handled state. |
| 448 | Handled.push_back(Cur); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 449 | if (Cur->getLiveRange().getWeight().isInf()) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 450 | Func->setError("Unable to find a physical register for an " |
| 451 | "infinite-weight live range"); |
| 452 | } |
| 453 | } else { |
| 454 | // Evict all live ranges in Active that register number |
| 455 | // MinWeightIndex is assigned to. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 456 | for (auto I = Active.begin(), E = Active.end(); I != E; I = Next) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 457 | Next = I; |
| 458 | ++Next; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 459 | Variable *Item = *I; |
| 460 | if (Item->getRegNumTmp() == MinWeightIndex) { |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 461 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 462 | Str << "Evicting "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 463 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 464 | Str << "\n"; |
| 465 | } |
| 466 | --RegUses[MinWeightIndex]; |
| 467 | assert(RegUses[MinWeightIndex] >= 0); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 468 | Item->setRegNumTmp(Variable::NoRegister); |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 469 | Handled.splice(Handled.end(), Active, I); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | // Do the same for Inactive. |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 473 | for (auto I = Inactive.begin(), E = Inactive.end(); I != E; I = Next) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 474 | Next = I; |
| 475 | ++Next; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 476 | Variable *Item = *I; |
| 477 | // Note: The Item->rangeOverlaps(Cur) clause is not part of the |
Jim Stichnoth | 68e2819 | 2014-07-24 08:48:15 -0700 | [diff] [blame] | 478 | // description of AssignMemLoc() in the original paper. But |
| 479 | // there doesn't seem to be any need to evict an inactive |
| 480 | // live range that doesn't overlap with the live range |
| 481 | // currently being considered. It's especially bad if we |
| 482 | // would end up evicting an infinite-weight but |
| 483 | // currently-inactive live range. The most common situation |
| 484 | // for this would be a scratch register kill set for call |
| 485 | // instructions. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 486 | if (Item->getRegNumTmp() == MinWeightIndex && |
| 487 | Item->rangeOverlaps(Cur)) { |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 488 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 489 | Str << "Evicting "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 490 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 491 | Str << "\n"; |
| 492 | } |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 493 | Item->setRegNumTmp(Variable::NoRegister); |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 494 | Handled.splice(Handled.end(), Inactive, I); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | // Assign the register to Cur. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 498 | Cur->setRegNumTmp(MinWeightIndex); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 499 | assert(RegUses[MinWeightIndex] >= 0); |
| 500 | ++RegUses[MinWeightIndex]; |
| 501 | Active.push_back(Cur); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 502 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 503 | Str << "Allocating "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 504 | dumpLiveRange(Cur, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 505 | Str << "\n"; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | dump(Func); |
| 510 | } |
| 511 | // Move anything Active or Inactive to Handled for easier handling. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 512 | for (Variable *I : Active) |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 513 | Handled.push_back(I); |
| 514 | Active.clear(); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 515 | for (Variable *I : Inactive) |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 516 | Handled.push_back(I); |
| 517 | Inactive.clear(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 518 | dump(Func); |
| 519 | |
| 520 | // Finish up by assigning RegNumTmp->RegNum for each Variable. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 521 | for (Variable *Item : Handled) { |
| 522 | int32_t RegNum = Item->getRegNumTmp(); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 523 | if (Verbose) { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 524 | if (!Item->hasRegTmp()) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 525 | Str << "Not assigning "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 526 | Item->dump(Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 527 | Str << "\n"; |
| 528 | } else { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 529 | Str << (RegNum == Item->getRegNum() ? "Reassigning " : "Assigning ") |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 530 | << Func->getTarget()->getRegName(RegNum, IceType_i32) << "(r" |
| 531 | << RegNum << ") to "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 532 | Item->dump(Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 533 | Str << "\n"; |
| 534 | } |
| 535 | } |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 536 | Item->setRegNum(Item->getRegNumTmp()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // TODO: Consider running register allocation one more time, with |
| 540 | // infinite registers, for two reasons. First, evicted live ranges |
| 541 | // get a second chance for a register. Second, it allows coalescing |
| 542 | // of stack slots. If there is no time budget for the second |
| 543 | // register allocation run, each unallocated variable just gets its |
| 544 | // own slot. |
| 545 | // |
| 546 | // Another idea for coalescing stack slots is to initialize the |
| 547 | // Unhandled list with just the unallocated variables, saving time |
| 548 | // but not offering second-chance opportunities. |
| 549 | } |
| 550 | |
| 551 | // ======================== Dump routines ======================== // |
| 552 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 553 | void LinearScan::dump(Cfg *Func) const { |
| 554 | Ostream &Str = Func->getContext()->getStrDump(); |
| 555 | if (!Func->getContext()->isVerbose(IceV_LinearScan)) |
| 556 | return; |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 557 | Func->resetCurrentNode(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 558 | Str << "**** Current regalloc state:\n"; |
| 559 | Str << "++++++ Handled:\n"; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 560 | for (const Variable *Item : Handled) { |
| 561 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 562 | Str << "\n"; |
| 563 | } |
| 564 | Str << "++++++ Unhandled:\n"; |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 565 | for (auto I = Unhandled.rbegin(), E = Unhandled.rend(); I != E; ++I) { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 566 | dumpLiveRange(*I, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 567 | Str << "\n"; |
| 568 | } |
| 569 | Str << "++++++ Active:\n"; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 570 | for (const Variable *Item : Active) { |
| 571 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 572 | Str << "\n"; |
| 573 | } |
| 574 | Str << "++++++ Inactive:\n"; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 575 | for (const Variable *Item : Inactive) { |
| 576 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 577 | Str << "\n"; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | } // end of namespace Ice |