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" |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 17 | #include "IceCfgNode.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 18 | #include "IceInst.h" |
| 19 | #include "IceOperand.h" |
| 20 | #include "IceRegAlloc.h" |
| 21 | #include "IceTargetLowering.h" |
| 22 | |
| 23 | namespace Ice { |
| 24 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 25 | namespace { |
| 26 | |
Jim Stichnoth | e34d79d | 2015-01-12 09:00:50 -0800 | [diff] [blame^] | 27 | // TODO(stichnot): Statically choose the size based on the target |
| 28 | // being compiled. |
| 29 | const size_t REGS_SIZE = 32; |
| 30 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 31 | // Returns true if Var has any definitions within Item's live range. |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 32 | // TODO(stichnot): Consider trimming the Definitions list similar to |
| 33 | // how the live ranges are trimmed, since all the overlapsDefs() tests |
| 34 | // are whether some variable's definitions overlap Cur, and trimming |
| 35 | // is with respect Cur.start. Initial tests show no measurable |
| 36 | // performance difference, so we'll keep the code simple for now. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 37 | bool overlapsDefs(const Cfg *Func, const Variable *Item, const Variable *Var) { |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 38 | const bool UseTrimmed = true; |
| 39 | VariablesMetadata *VMetadata = Func->getVMetadata(); |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 40 | if (const Inst *FirstDef = VMetadata->getFirstDefinition(Var)) |
| 41 | if (Item->getLiveRange().overlapsInst(FirstDef->getNumber(), UseTrimmed)) |
| 42 | return true; |
| 43 | const InstDefList &Defs = VMetadata->getLatterDefinitions(Var); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 44 | for (size_t i = 0; i < Defs.size(); ++i) { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 45 | if (Item->getLiveRange().overlapsInst(Defs[i]->getNumber(), UseTrimmed)) |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 46 | return true; |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | void dumpDisableOverlap(const Cfg *Func, const Variable *Var, |
| 52 | const char *Reason) { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 53 | if (!ALLOW_DUMP) |
| 54 | return; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 55 | if (Func->getContext()->isVerbose(IceV_LinearScan)) { |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 56 | VariablesMetadata *VMetadata = Func->getVMetadata(); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 57 | Ostream &Str = Func->getContext()->getStrDump(); |
| 58 | Str << "Disabling Overlap due to " << Reason << " " << *Var |
| 59 | << " LIVE=" << Var->getLiveRange() << " Defs="; |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 60 | if (const Inst *FirstDef = VMetadata->getFirstDefinition(Var)) |
| 61 | Str << FirstDef->getNumber(); |
| 62 | const InstDefList &Defs = VMetadata->getLatterDefinitions(Var); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 63 | for (size_t i = 0; i < Defs.size(); ++i) { |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 64 | Str << "," << Defs[i]->getNumber(); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 65 | } |
| 66 | Str << "\n"; |
| 67 | } |
| 68 | } |
| 69 | |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 70 | void dumpLiveRange(const Variable *Var, const Cfg *Func) { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 71 | if (!ALLOW_DUMP) |
| 72 | return; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 73 | Ostream &Str = Func->getContext()->getStrDump(); |
| 74 | const static size_t BufLen = 30; |
| 75 | char buf[BufLen]; |
| 76 | snprintf(buf, BufLen, "%2d", Var->getRegNumTmp()); |
| 77 | Str << "R=" << buf << " V="; |
| 78 | Var->dump(Func); |
| 79 | Str << " Range=" << Var->getLiveRange(); |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 82 | } // end of anonymous namespace |
| 83 | |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 84 | // Prepare for full register allocation of all variables. We depend |
| 85 | // on liveness analysis to have calculated live ranges. |
| 86 | void LinearScan::initForGlobal() { |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 87 | TimerMarker T(TimerStack::TT_initUnhandled, Func); |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 88 | FindPreference = true; |
| 89 | FindOverlap = true; |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 90 | const VarList &Vars = Func->getVariables(); |
| 91 | Unhandled.reserve(Vars.size()); |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 92 | UnhandledPrecolored.reserve(Vars.size()); |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 93 | // Gather the live ranges of all variables and add them to the |
| 94 | // Unhandled set. |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 95 | for (Variable *Var : Vars) { |
| 96 | // Explicitly don't consider zero-weight variables, which are |
| 97 | // meant to be spill slots. |
| 98 | if (Var->getWeight() == RegWeight::Zero) |
| 99 | continue; |
| 100 | // Don't bother if the variable has a null live range, which means |
| 101 | // it was never referenced. |
| 102 | if (Var->getLiveRange().isEmpty()) |
| 103 | continue; |
| 104 | Var->untrimLiveRange(); |
| 105 | Unhandled.push_back(Var); |
| 106 | if (Var->hasReg()) { |
| 107 | Var->setRegNumTmp(Var->getRegNum()); |
| 108 | Var->setLiveRangeInfiniteWeight(); |
| 109 | UnhandledPrecolored.push_back(Var); |
| 110 | } |
| 111 | } |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 112 | |
| 113 | // Build the (ordered) list of FakeKill instruction numbers. |
| 114 | Kills.clear(); |
| 115 | for (CfgNode *Node : Func->getNodes()) { |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 116 | for (Inst &I : Node->getInsts()) { |
| 117 | if (auto Kill = llvm::dyn_cast<InstFakeKill>(&I)) { |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 118 | if (!Kill->isDeleted() && !Kill->getLinked()->isDeleted()) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 119 | Kills.push_back(I.getNumber()); |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Prepare for very simple register allocation of only infinite-weight |
| 126 | // Variables while respecting pre-colored Variables. Some properties |
| 127 | // we take advantage of: |
| 128 | // |
| 129 | // * Live ranges of interest consist of a single segment. |
| 130 | // |
| 131 | // * Live ranges of interest never span a call instruction. |
| 132 | // |
| 133 | // * Phi instructions are not considered because either phis have |
| 134 | // already been lowered, or they don't contain any pre-colored or |
| 135 | // infinite-weight Variables. |
| 136 | // |
| 137 | // * We don't need to renumber instructions before computing live |
| 138 | // ranges because all the high-level ICE instructions are deleted |
| 139 | // prior to lowering, and the low-level instructions are added in |
| 140 | // monotonically increasing order. |
| 141 | // |
| 142 | // * There are no opportunities for register preference or allowing |
| 143 | // overlap. |
| 144 | // |
| 145 | // Some properties we aren't (yet) taking advantage of: |
| 146 | // |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 147 | // * Because live ranges are a single segment, the Inactive set will |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 148 | // always be empty, and the live range trimming operation is |
| 149 | // unnecessary. |
| 150 | // |
| 151 | // * Calculating overlap of single-segment live ranges could be |
| 152 | // optimized a bit. |
| 153 | void LinearScan::initForInfOnly() { |
| 154 | TimerMarker T(TimerStack::TT_initUnhandled, Func); |
| 155 | FindPreference = false; |
| 156 | FindOverlap = false; |
| 157 | SizeT NumVars = 0; |
| 158 | const VarList &Vars = Func->getVariables(); |
| 159 | |
| 160 | // Iterate across all instructions and record the begin and end of |
| 161 | // the live range for each variable that is pre-colored or infinite |
| 162 | // weight. |
| 163 | std::vector<InstNumberT> LRBegin(Vars.size(), Inst::NumberSentinel); |
| 164 | std::vector<InstNumberT> LREnd(Vars.size(), Inst::NumberSentinel); |
| 165 | for (CfgNode *Node : Func->getNodes()) { |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 166 | for (Inst &Inst : Node->getInsts()) { |
| 167 | if (Inst.isDeleted()) |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 168 | continue; |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 169 | if (const Variable *Var = Inst.getDest()) { |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 170 | if (Var->hasReg() || Var->getWeight() == RegWeight::Inf) { |
| 171 | if (LRBegin[Var->getIndex()] == Inst::NumberSentinel) { |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 172 | LRBegin[Var->getIndex()] = Inst.getNumber(); |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 173 | ++NumVars; |
| 174 | } |
| 175 | } |
| 176 | } |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 177 | for (SizeT I = 0; I < Inst.getSrcSize(); ++I) { |
| 178 | Operand *Src = Inst.getSrc(I); |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 179 | SizeT NumVars = Src->getNumVars(); |
| 180 | for (SizeT J = 0; J < NumVars; ++J) { |
| 181 | const Variable *Var = Src->getVar(J); |
| 182 | if (Var->hasReg() || Var->getWeight() == RegWeight::Inf) |
Jim Stichnoth | 29841e8 | 2014-12-23 12:26:24 -0800 | [diff] [blame] | 183 | LREnd[Var->getIndex()] = Inst.getNumber(); |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | Unhandled.reserve(NumVars); |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 190 | UnhandledPrecolored.reserve(NumVars); |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 191 | for (SizeT i = 0; i < Vars.size(); ++i) { |
| 192 | Variable *Var = Vars[i]; |
| 193 | if (LRBegin[i] != Inst::NumberSentinel) { |
| 194 | assert(LREnd[i] != Inst::NumberSentinel); |
| 195 | Unhandled.push_back(Var); |
| 196 | Var->resetLiveRange(); |
| 197 | const uint32_t WeightDelta = 1; |
| 198 | Var->addLiveRange(LRBegin[i], LREnd[i], WeightDelta); |
| 199 | Var->untrimLiveRange(); |
| 200 | if (Var->hasReg()) { |
| 201 | Var->setRegNumTmp(Var->getRegNum()); |
| 202 | Var->setLiveRangeInfiniteWeight(); |
| 203 | UnhandledPrecolored.push_back(Var); |
| 204 | } |
| 205 | --NumVars; |
| 206 | } |
| 207 | } |
| 208 | // This isn't actually a fatal condition, but it would be nice to |
| 209 | // know if we somehow pre-calculated Unhandled's size wrong. |
| 210 | assert(NumVars == 0); |
| 211 | |
| 212 | // Don't build up the list of Kills because we know that no |
| 213 | // infinite-weight Variable has a live range spanning a call. |
| 214 | Kills.clear(); |
| 215 | } |
| 216 | |
| 217 | void LinearScan::init(RegAllocKind Kind) { |
| 218 | Unhandled.clear(); |
| 219 | UnhandledPrecolored.clear(); |
| 220 | Handled.clear(); |
| 221 | Inactive.clear(); |
| 222 | Active.clear(); |
| 223 | |
| 224 | switch (Kind) { |
| 225 | case RAK_Global: |
| 226 | initForGlobal(); |
| 227 | break; |
| 228 | case RAK_InfOnly: |
| 229 | initForInfOnly(); |
| 230 | break; |
| 231 | } |
| 232 | |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 233 | struct CompareRanges { |
| 234 | bool operator()(const Variable *L, const Variable *R) { |
| 235 | InstNumberT Lstart = L->getLiveRange().getStart(); |
| 236 | InstNumberT Rstart = R->getLiveRange().getStart(); |
| 237 | if (Lstart == Rstart) |
| 238 | return L->getIndex() < R->getIndex(); |
| 239 | return Lstart < Rstart; |
| 240 | } |
| 241 | }; |
| 242 | // Do a reverse sort so that erasing elements (from the end) is fast. |
| 243 | std::sort(Unhandled.rbegin(), Unhandled.rend(), CompareRanges()); |
| 244 | std::sort(UnhandledPrecolored.rbegin(), UnhandledPrecolored.rend(), |
| 245 | CompareRanges()); |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 246 | |
| 247 | Handled.reserve(Unhandled.size()); |
| 248 | Inactive.reserve(Unhandled.size()); |
| 249 | Active.reserve(Unhandled.size()); |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 252 | // Implements the linear-scan algorithm. Based on "Linear Scan |
| 253 | // Register Allocation in the Context of SSA Form and Register |
| 254 | // Constraints" by Hanspeter Mössenböck and Michael Pfeiffer, |
| 255 | // ftp://ftp.ssw.uni-linz.ac.at/pub/Papers/Moe02.PDF . This |
| 256 | // implementation is modified to take affinity into account and allow |
| 257 | // two interfering variables to share the same register in certain |
| 258 | // cases. |
| 259 | // |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 260 | // Requires running Cfg::liveness(Liveness_Intervals) in |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 261 | // preparation. Results are assigned to Variable::RegNum for each |
| 262 | // Variable. |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 263 | void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull, |
| 264 | bool Randomized) { |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 265 | TimerMarker T(TimerStack::TT_linearScan, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 266 | assert(RegMaskFull.any()); // Sanity check |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 267 | Ostream &Str = Func->getContext()->getStrDump(); |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 268 | const bool Verbose = |
| 269 | ALLOW_DUMP && Func->getContext()->isVerbose(IceV_LinearScan); |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 270 | Func->resetCurrentNode(); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 271 | VariablesMetadata *VMetadata = Func->getVMetadata(); |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 272 | const size_t NumRegisters = RegMaskFull.size(); |
| 273 | llvm::SmallBitVector PreDefinedRegisters(NumRegisters); |
| 274 | if (Randomized) { |
| 275 | for (Variable *Var : UnhandledPrecolored) { |
| 276 | PreDefinedRegisters[Var->getRegNum()] = true; |
| 277 | } |
| 278 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 279 | |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 280 | // Build a LiveRange representing the Kills list. |
Jim Stichnoth | 2a7fcbb | 2014-12-04 11:45:03 -0800 | [diff] [blame] | 281 | LiveRange KillsRange(Kills); |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 282 | KillsRange.untrim(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 283 | |
| 284 | // RegUses[I] is the number of live ranges (variables) that register |
| 285 | // 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] | 286 | // of AllowOverlap inference below. |
Jim Stichnoth | e34d79d | 2015-01-12 09:00:50 -0800 | [diff] [blame^] | 287 | llvm::SmallVector<int, REGS_SIZE> RegUses(NumRegisters); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 288 | // Unhandled is already set to all ranges in increasing order of |
| 289 | // start points. |
| 290 | assert(Active.empty()); |
| 291 | assert(Inactive.empty()); |
| 292 | assert(Handled.empty()); |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 293 | const TargetLowering::RegSetMask RegsInclude = |
| 294 | TargetLowering::RegSet_CallerSave; |
| 295 | const TargetLowering::RegSetMask RegsExclude = TargetLowering::RegSet_None; |
| 296 | const llvm::SmallBitVector KillsMask = |
| 297 | Func->getTarget()->getRegisterSet(RegsInclude, RegsExclude); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 298 | |
| 299 | while (!Unhandled.empty()) { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 300 | Variable *Cur = Unhandled.back(); |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 301 | Unhandled.pop_back(); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 302 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 303 | Str << "\nConsidering "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 304 | dumpLiveRange(Cur, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 305 | Str << "\n"; |
| 306 | } |
| 307 | const llvm::SmallBitVector RegMask = |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 308 | RegMaskFull & Func->getTarget()->getRegisterSetForType(Cur->getType()); |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 309 | KillsRange.trim(Cur->getLiveRange().getStart()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 310 | |
| 311 | // Check for precolored ranges. If Cur is precolored, it |
| 312 | // definitely gets that register. Previously processed live |
| 313 | // ranges would have avoided that register due to it being |
| 314 | // precolored. Future processed live ranges won't evict that |
| 315 | // register because the live range has infinite weight. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 316 | if (Cur->hasReg()) { |
| 317 | int32_t RegNum = Cur->getRegNum(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 318 | // RegNumTmp should have already been set above. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 319 | assert(Cur->getRegNumTmp() == RegNum); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 320 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 321 | Str << "Precoloring "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 322 | dumpLiveRange(Cur, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 323 | Str << "\n"; |
| 324 | } |
| 325 | Active.push_back(Cur); |
| 326 | assert(RegUses[RegNum] >= 0); |
| 327 | ++RegUses[RegNum]; |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 328 | assert(!UnhandledPrecolored.empty()); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 329 | assert(UnhandledPrecolored.back() == Cur); |
Jim Stichnoth | e22f823 | 2014-10-13 16:20:59 -0700 | [diff] [blame] | 330 | UnhandledPrecolored.pop_back(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 331 | continue; |
| 332 | } |
| 333 | |
| 334 | // Check for active ranges that have expired or become inactive. |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 335 | for (SizeT I = Active.size(); I > 0; --I) { |
| 336 | const SizeT Index = I - 1; |
| 337 | Variable *Item = Active[Index]; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 338 | Item->trimLiveRange(Cur->getLiveRange().getStart()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 339 | bool Moved = false; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 340 | if (Item->rangeEndsBefore(Cur)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 341 | // Move Item from Active to Handled list. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 342 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 343 | Str << "Expiring "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 344 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 345 | Str << "\n"; |
| 346 | } |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 347 | moveItem(Active, Index, Handled); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 348 | Moved = true; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 349 | } else if (!Item->rangeOverlapsStart(Cur)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 350 | // Move Item from Active to Inactive list. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 351 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 352 | Str << "Inactivating "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 353 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 354 | Str << "\n"; |
| 355 | } |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 356 | moveItem(Active, Index, Inactive); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 357 | Moved = true; |
| 358 | } |
| 359 | if (Moved) { |
| 360 | // Decrement Item from RegUses[]. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 361 | assert(Item->hasRegTmp()); |
| 362 | int32_t RegNum = Item->getRegNumTmp(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 363 | --RegUses[RegNum]; |
| 364 | assert(RegUses[RegNum] >= 0); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // Check for inactive ranges that have expired or reactivated. |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 369 | for (SizeT I = Inactive.size(); I > 0; --I) { |
| 370 | const SizeT Index = I - 1; |
| 371 | Variable *Item = Inactive[Index]; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 372 | Item->trimLiveRange(Cur->getLiveRange().getStart()); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 373 | if (Item->rangeEndsBefore(Cur)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 374 | // Move Item from Inactive to Handled list. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 375 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 376 | Str << "Expiring "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 377 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 378 | Str << "\n"; |
| 379 | } |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 380 | moveItem(Inactive, Index, Handled); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 381 | } else if (Item->rangeOverlapsStart(Cur)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 382 | // Move Item from Inactive to Active list. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 383 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 384 | Str << "Reactivating "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 385 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 386 | Str << "\n"; |
| 387 | } |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 388 | moveItem(Inactive, Index, Active); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 389 | // Increment Item in RegUses[]. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 390 | assert(Item->hasRegTmp()); |
| 391 | int32_t RegNum = Item->getRegNumTmp(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 392 | assert(RegUses[RegNum] >= 0); |
| 393 | ++RegUses[RegNum]; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | // Calculate available registers into Free[]. |
| 398 | llvm::SmallBitVector Free = RegMask; |
| 399 | for (SizeT i = 0; i < RegMask.size(); ++i) { |
| 400 | if (RegUses[i] > 0) |
| 401 | Free[i] = false; |
| 402 | } |
| 403 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 404 | // Infer register preference and allowable overlap. Only form a |
| 405 | // preference when the current Variable has an unambiguous "first" |
| 406 | // definition. The preference is some source Variable of the |
| 407 | // defining instruction that either is assigned a register that is |
| 408 | // currently free, or that is assigned a register that is not free |
| 409 | // but overlap is allowed. Overlap is allowed when the Variable |
| 410 | // under consideration is single-definition, and its definition is |
| 411 | // a simple assignment - i.e., the register gets copied/aliased |
| 412 | // but is never modified. Furthermore, overlap is only allowed |
| 413 | // when preferred Variable definition instructions do not appear |
| 414 | // within the current Variable's live range. |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 415 | Variable *Prefer = nullptr; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 416 | int32_t PreferReg = Variable::NoRegister; |
| 417 | bool AllowOverlap = false; |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 418 | if (FindPreference) { |
| 419 | if (const Inst *DefInst = VMetadata->getFirstDefinition(Cur)) { |
| 420 | assert(DefInst->getDest() == Cur); |
| 421 | bool IsAssign = DefInst->isSimpleAssign(); |
| 422 | bool IsSingleDef = !VMetadata->isMultiDef(Cur); |
| 423 | for (SizeT i = 0; i < DefInst->getSrcSize(); ++i) { |
| 424 | // TODO(stichnot): Iterate through the actual Variables of the |
| 425 | // instruction, not just the source operands. This could |
| 426 | // capture Load instructions, including address mode |
| 427 | // optimization, for Prefer (but not for AllowOverlap). |
| 428 | if (Variable *SrcVar = llvm::dyn_cast<Variable>(DefInst->getSrc(i))) { |
| 429 | int32_t SrcReg = SrcVar->getRegNumTmp(); |
| 430 | // Only consider source variables that have (so far) been |
| 431 | // assigned a register. That register must be one in the |
| 432 | // RegMask set, e.g. don't try to prefer the stack pointer |
| 433 | // as a result of the stacksave intrinsic. |
| 434 | if (SrcVar->hasRegTmp() && RegMask[SrcReg]) { |
| 435 | if (FindOverlap && !Free[SrcReg]) { |
| 436 | // Don't bother trying to enable AllowOverlap if the |
| 437 | // register is already free. |
| 438 | AllowOverlap = |
| 439 | IsSingleDef && IsAssign && !overlapsDefs(Func, Cur, SrcVar); |
| 440 | } |
| 441 | if (AllowOverlap || Free[SrcReg]) { |
| 442 | Prefer = SrcVar; |
| 443 | PreferReg = SrcReg; |
| 444 | } |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 448 | if (Verbose && Prefer) { |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 449 | Str << "Initial Prefer="; |
| 450 | Prefer->dump(Func); |
| 451 | Str << " R=" << PreferReg << " LIVE=" << Prefer->getLiveRange() |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 452 | << " Overlap=" << AllowOverlap << "\n"; |
| 453 | } |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 457 | // Remove registers from the Free[] list where an Inactive range |
| 458 | // overlaps with the current range. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 459 | for (const Variable *Item : Inactive) { |
| 460 | if (Item->rangeOverlaps(Cur)) { |
| 461 | int32_t RegNum = Item->getRegNumTmp(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 462 | // Don't assert(Free[RegNum]) because in theory (though |
| 463 | // probably never in practice) there could be two inactive |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 464 | // variables that were marked with AllowOverlap. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 465 | Free[RegNum] = false; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 466 | // Disable AllowOverlap if an Inactive variable, which is not |
| 467 | // Prefer, shares Prefer's register, and has a definition |
| 468 | // within Cur's live range. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 469 | if (AllowOverlap && Item != Prefer && RegNum == PreferReg && |
| 470 | overlapsDefs(Func, Cur, Item)) { |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 471 | AllowOverlap = false; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 472 | dumpDisableOverlap(Func, Item, "Inactive"); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // Disable AllowOverlap if an Active variable, which is not |
| 478 | // Prefer, shares Prefer's register, and has a definition within |
| 479 | // Cur's live range. |
Jim Stichnoth | 70d0a05 | 2014-11-14 15:53:46 -0800 | [diff] [blame] | 480 | if (AllowOverlap) { |
| 481 | for (const Variable *Item : Active) { |
| 482 | int32_t RegNum = Item->getRegNumTmp(); |
| 483 | if (Item != Prefer && RegNum == PreferReg && |
| 484 | overlapsDefs(Func, Cur, Item)) { |
| 485 | AllowOverlap = false; |
| 486 | dumpDisableOverlap(Func, Item, "Active"); |
| 487 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
Jim Stichnoth | e34d79d | 2015-01-12 09:00:50 -0800 | [diff] [blame^] | 491 | llvm::SmallVector<RegWeight, REGS_SIZE> Weights(RegMask.size()); |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 492 | |
| 493 | // Remove registers from the Free[] list where an Unhandled |
| 494 | // precolored range overlaps with the current range, and set those |
| 495 | // registers to infinite weight so that they aren't candidates for |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 496 | // eviction. Cur->rangeEndsBefore(Item) is an early exit check |
| 497 | // that turns a guaranteed O(N^2) algorithm into expected linear |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 498 | // complexity. |
| 499 | llvm::SmallBitVector PrecoloredUnhandledMask(RegMask.size()); |
| 500 | // Note: PrecoloredUnhandledMask is only used for dumping. |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 501 | for (Variable *Item : reverse_range(UnhandledPrecolored)) { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 502 | assert(Item->hasReg()); |
| 503 | if (Cur->rangeEndsBefore(Item)) |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 504 | break; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 505 | if (Item->rangeOverlaps(Cur)) { |
| 506 | int32_t ItemReg = Item->getRegNum(); // Note: not getRegNumTmp() |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 507 | Weights[ItemReg].setWeight(RegWeight::Inf); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 508 | Free[ItemReg] = false; |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 509 | PrecoloredUnhandledMask[ItemReg] = true; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 510 | // Disable AllowOverlap if the preferred register is one of |
| 511 | // these precolored unhandled overlapping ranges. |
| 512 | if (AllowOverlap && ItemReg == PreferReg) { |
| 513 | AllowOverlap = false; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 514 | dumpDisableOverlap(Func, Item, "PrecoloredUnhandled"); |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 515 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 519 | // Remove scratch registers from the Free[] list, and mark their |
| 520 | // Weights[] as infinite, if KillsRange overlaps Cur's live range. |
| 521 | const bool UseTrimmed = true; |
| 522 | if (Cur->getLiveRange().overlaps(KillsRange, UseTrimmed)) { |
| 523 | Free.reset(KillsMask); |
| 524 | for (int i = KillsMask.find_first(); i != -1; |
| 525 | i = KillsMask.find_next(i)) { |
| 526 | Weights[i].setWeight(RegWeight::Inf); |
| 527 | if (PreferReg == i) |
| 528 | AllowOverlap = false; |
| 529 | } |
| 530 | } |
| 531 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 532 | // Print info about physical register availability. |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 533 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 534 | for (SizeT i = 0; i < RegMask.size(); ++i) { |
| 535 | if (RegMask[i]) { |
| 536 | Str << Func->getTarget()->getRegName(i, IceType_i32) |
Jim Stichnoth | ca662e9 | 2014-07-10 15:32:36 -0700 | [diff] [blame] | 537 | << "(U=" << RegUses[i] << ",F=" << Free[i] |
Jim Stichnoth | 541ba66 | 2014-10-02 12:58:21 -0700 | [diff] [blame] | 538 | << ",P=" << PrecoloredUnhandledMask[i] << ") "; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | Str << "\n"; |
| 542 | } |
| 543 | |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 544 | if (Prefer && (AllowOverlap || Free[PreferReg])) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 545 | // First choice: a preferred register that is either free or is |
| 546 | // allowed to overlap with its linked variable. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 547 | Cur->setRegNumTmp(PreferReg); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 548 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 549 | Str << "Preferring "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 550 | dumpLiveRange(Cur, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 551 | Str << "\n"; |
| 552 | } |
| 553 | assert(RegUses[PreferReg] >= 0); |
| 554 | ++RegUses[PreferReg]; |
| 555 | Active.push_back(Cur); |
| 556 | } else if (Free.any()) { |
| 557 | // Second choice: any free register. TODO: After explicit |
| 558 | // affinity is considered, is there a strategy better than just |
| 559 | // picking the lowest-numbered available register? |
| 560 | int32_t RegNum = Free.find_first(); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 561 | Cur->setRegNumTmp(RegNum); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 562 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 563 | Str << "Allocating "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 564 | dumpLiveRange(Cur, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 565 | Str << "\n"; |
| 566 | } |
| 567 | assert(RegUses[RegNum] >= 0); |
| 568 | ++RegUses[RegNum]; |
| 569 | Active.push_back(Cur); |
| 570 | } else { |
| 571 | // Fallback: there are no free registers, so we look for the |
| 572 | // lowest-weight register and see if Cur has higher weight. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 573 | // Check Active ranges. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 574 | for (const Variable *Item : Active) { |
| 575 | assert(Item->rangeOverlaps(Cur)); |
| 576 | int32_t RegNum = Item->getRegNumTmp(); |
| 577 | assert(Item->hasRegTmp()); |
| 578 | Weights[RegNum].addWeight(Item->getLiveRange().getWeight()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 579 | } |
| 580 | // Same as above, but check Inactive ranges instead of Active. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 581 | for (const Variable *Item : Inactive) { |
| 582 | int32_t RegNum = Item->getRegNumTmp(); |
| 583 | assert(Item->hasRegTmp()); |
| 584 | if (Item->rangeOverlaps(Cur)) |
| 585 | Weights[RegNum].addWeight(Item->getLiveRange().getWeight()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 586 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 587 | |
| 588 | // All the weights are now calculated. Find the register with |
| 589 | // smallest weight. |
| 590 | int32_t MinWeightIndex = RegMask.find_first(); |
| 591 | // MinWeightIndex must be valid because of the initial |
| 592 | // RegMask.any() test. |
| 593 | assert(MinWeightIndex >= 0); |
| 594 | for (SizeT i = MinWeightIndex + 1; i < Weights.size(); ++i) { |
| 595 | if (RegMask[i] && Weights[i] < Weights[MinWeightIndex]) |
| 596 | MinWeightIndex = i; |
| 597 | } |
| 598 | |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 599 | if (Cur->getLiveRange().getWeight() <= Weights[MinWeightIndex]) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 600 | // Cur doesn't have priority over any other live ranges, so |
| 601 | // don't allocate any register to it, and move it to the |
| 602 | // Handled state. |
| 603 | Handled.push_back(Cur); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 604 | if (Cur->getLiveRange().getWeight().isInf()) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 605 | Func->setError("Unable to find a physical register for an " |
| 606 | "infinite-weight live range"); |
| 607 | } |
| 608 | } else { |
| 609 | // Evict all live ranges in Active that register number |
| 610 | // MinWeightIndex is assigned to. |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 611 | for (SizeT I = Active.size(); I > 0; --I) { |
| 612 | const SizeT Index = I - 1; |
| 613 | Variable *Item = Active[Index]; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 614 | if (Item->getRegNumTmp() == MinWeightIndex) { |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 615 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 616 | Str << "Evicting "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 617 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 618 | Str << "\n"; |
| 619 | } |
| 620 | --RegUses[MinWeightIndex]; |
| 621 | assert(RegUses[MinWeightIndex] >= 0); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 622 | Item->setRegNumTmp(Variable::NoRegister); |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 623 | moveItem(Active, Index, Handled); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | // Do the same for Inactive. |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 627 | for (SizeT I = Inactive.size(); I > 0; --I) { |
| 628 | const SizeT Index = I - 1; |
| 629 | Variable *Item = Inactive[Index]; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 630 | // Note: The Item->rangeOverlaps(Cur) clause is not part of the |
Jim Stichnoth | 68e2819 | 2014-07-24 08:48:15 -0700 | [diff] [blame] | 631 | // description of AssignMemLoc() in the original paper. But |
| 632 | // there doesn't seem to be any need to evict an inactive |
| 633 | // live range that doesn't overlap with the live range |
| 634 | // currently being considered. It's especially bad if we |
| 635 | // would end up evicting an infinite-weight but |
| 636 | // currently-inactive live range. The most common situation |
| 637 | // for this would be a scratch register kill set for call |
| 638 | // instructions. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 639 | if (Item->getRegNumTmp() == MinWeightIndex && |
| 640 | Item->rangeOverlaps(Cur)) { |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 641 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 642 | Str << "Evicting "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 643 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 644 | Str << "\n"; |
| 645 | } |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 646 | Item->setRegNumTmp(Variable::NoRegister); |
Jim Stichnoth | 4ead35a | 2014-12-03 20:30:34 -0800 | [diff] [blame] | 647 | moveItem(Inactive, Index, Handled); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | // Assign the register to Cur. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 651 | Cur->setRegNumTmp(MinWeightIndex); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 652 | assert(RegUses[MinWeightIndex] >= 0); |
| 653 | ++RegUses[MinWeightIndex]; |
| 654 | Active.push_back(Cur); |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 655 | if (Verbose) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 656 | Str << "Allocating "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 657 | dumpLiveRange(Cur, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 658 | Str << "\n"; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | dump(Func); |
| 663 | } |
| 664 | // Move anything Active or Inactive to Handled for easier handling. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 665 | for (Variable *I : Active) |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 666 | Handled.push_back(I); |
| 667 | Active.clear(); |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 668 | for (Variable *I : Inactive) |
Jim Stichnoth | f44f371 | 2014-10-01 14:05:51 -0700 | [diff] [blame] | 669 | Handled.push_back(I); |
| 670 | Inactive.clear(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 671 | dump(Func); |
| 672 | |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 673 | llvm::SmallVector<int32_t, REGS_SIZE> Permutation(NumRegisters); |
| 674 | if (Randomized) { |
| 675 | Func->getTarget()->makeRandomRegisterPermutation( |
| 676 | Permutation, PreDefinedRegisters | ~RegMaskFull); |
| 677 | } |
| 678 | |
| 679 | // Finish up by assigning RegNumTmp->RegNum (or a random permutation |
| 680 | // thereof) for each Variable. |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 681 | for (Variable *Item : Handled) { |
| 682 | int32_t RegNum = Item->getRegNumTmp(); |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 683 | int32_t AssignedRegNum = RegNum; |
| 684 | |
| 685 | if (Randomized && Item->hasRegTmp() && !Item->hasReg()) { |
| 686 | AssignedRegNum = Permutation[RegNum]; |
| 687 | } |
Jim Stichnoth | c4554d7 | 2014-09-30 16:49:38 -0700 | [diff] [blame] | 688 | if (Verbose) { |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 689 | if (!Item->hasRegTmp()) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 690 | Str << "Not assigning "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 691 | Item->dump(Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 692 | Str << "\n"; |
| 693 | } else { |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 694 | Str << (AssignedRegNum == Item->getRegNum() ? "Reassigning " |
| 695 | : "Assigning ") |
| 696 | << Func->getTarget()->getRegName(AssignedRegNum, IceType_i32) |
| 697 | << "(r" << AssignedRegNum << ") to "; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 698 | Item->dump(Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 699 | Str << "\n"; |
| 700 | } |
| 701 | } |
Jim Stichnoth | e6d2478 | 2014-12-19 05:42:24 -0800 | [diff] [blame] | 702 | Item->setRegNum(AssignedRegNum); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | // TODO: Consider running register allocation one more time, with |
| 706 | // infinite registers, for two reasons. First, evicted live ranges |
| 707 | // get a second chance for a register. Second, it allows coalescing |
| 708 | // of stack slots. If there is no time budget for the second |
| 709 | // register allocation run, each unallocated variable just gets its |
| 710 | // own slot. |
| 711 | // |
| 712 | // Another idea for coalescing stack slots is to initialize the |
| 713 | // Unhandled list with just the unallocated variables, saving time |
| 714 | // but not offering second-chance opportunities. |
| 715 | } |
| 716 | |
| 717 | // ======================== Dump routines ======================== // |
| 718 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 719 | void LinearScan::dump(Cfg *Func) const { |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 720 | if (!ALLOW_DUMP) |
| 721 | return; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 722 | Ostream &Str = Func->getContext()->getStrDump(); |
| 723 | if (!Func->getContext()->isVerbose(IceV_LinearScan)) |
| 724 | return; |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 725 | Func->resetCurrentNode(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 726 | Str << "**** Current regalloc state:\n"; |
| 727 | Str << "++++++ Handled:\n"; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 728 | for (const Variable *Item : Handled) { |
| 729 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 730 | Str << "\n"; |
| 731 | } |
| 732 | Str << "++++++ Unhandled:\n"; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 733 | for (const Variable *Item : reverse_range(Unhandled)) { |
| 734 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 735 | Str << "\n"; |
| 736 | } |
| 737 | Str << "++++++ Active:\n"; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 738 | for (const Variable *Item : Active) { |
| 739 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 740 | Str << "\n"; |
| 741 | } |
| 742 | Str << "++++++ Inactive:\n"; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 743 | for (const Variable *Item : Inactive) { |
| 744 | dumpLiveRange(Item, Func); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 745 | Str << "\n"; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | } // end of namespace Ice |