| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 1 | //===- RegAllocBase.cpp - Register Allocator Base Class -------------------===// | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 2 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | // See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 | // | 
| Manman Ren | 2867140 | 2014-02-22 19:31:28 +0000 | [diff] [blame] | 9 | // This file defines the RegAllocBase class which provides common functionality | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 10 | // for LiveIntervalUnion-based register allocators. | 
|  | 11 | // | 
|  | 12 | //===----------------------------------------------------------------------===// | 
|  | 13 |  | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 14 | #include "RegAllocBase.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallVector.h" | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/LiveInterval.h" | 
| Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/LiveIntervals.h" | 
| Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/LiveRegMatrix.h" | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstr.h" | 
| Nicolai Haehnle | 664ceed | 2019-05-15 17:29:58 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineModuleInfo.h" | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" | 
| Marcello Maggioni | e520507 | 2020-03-08 09:36:29 -0700 | [diff] [blame] | 23 | #include "llvm/CodeGen/Spiller.h" | 
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/TargetRegisterInfo.h" | 
| Jakob Stoklund Olesen | 26c9d70 | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/VirtRegMap.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" | 
|  | 28 | #include "llvm/Support/Debug.h" | 
|  | 29 | #include "llvm/Support/ErrorHandling.h" | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Timer.h" | 
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 32 | #include <cassert> | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 33 |  | 
|  | 34 | using namespace llvm; | 
|  | 35 |  | 
| Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "regalloc" | 
|  | 37 |  | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 38 | STATISTIC(NumNewQueued    , "Number of new live ranges queued"); | 
|  | 39 |  | 
|  | 40 | // Temporary verification option until we can put verification inside | 
|  | 41 | // MachineVerifier. | 
|  | 42 | static cl::opt<bool, true> | 
| Zachary Turner | 8065f0b | 2017-12-01 00:53:10 +0000 | [diff] [blame] | 43 | VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled), | 
|  | 44 | cl::Hidden, cl::desc("Verify during register allocation")); | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 45 |  | 
| Matthias Braun | 9f15a79 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 46 | const char RegAllocBase::TimerGroupName[] = "regalloc"; | 
|  | 47 | const char RegAllocBase::TimerGroupDescription[] = "Register Allocation"; | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 48 | bool RegAllocBase::VerifyEnabled = false; | 
|  | 49 |  | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// | 
|  | 51 | //                         RegAllocBase Implementation | 
|  | 52 | //===----------------------------------------------------------------------===// | 
|  | 53 |  | 
| Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 54 | // Pin the vtable to this file. | 
|  | 55 | void RegAllocBase::anchor() {} | 
|  | 56 |  | 
| Jakob Stoklund Olesen | 2d2dec9 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 57 | void RegAllocBase::init(VirtRegMap &vrm, | 
|  | 58 | LiveIntervals &lis, | 
|  | 59 | LiveRegMatrix &mat) { | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 60 | TRI = &vrm.getTargetRegInfo(); | 
|  | 61 | MRI = &vrm.getRegInfo(); | 
|  | 62 | VRM = &vrm; | 
|  | 63 | LIS = &lis; | 
| Jakob Stoklund Olesen | 2d2dec9 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 64 | Matrix = &mat; | 
| Chad Rosier | ed119d5 | 2012-11-28 00:21:29 +0000 | [diff] [blame] | 65 | MRI->freezeReservedRegs(vrm.getMachineFunction()); | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 66 | RegClassInfo.runOnMachineFunction(vrm.getMachineFunction()); | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
|  | 69 | // Visit all the live registers. If they are already assigned to a physical | 
|  | 70 | // register, unify them with the corresponding LiveIntervalUnion, otherwise push | 
|  | 71 | // them on the priority queue for later assignment. | 
|  | 72 | void RegAllocBase::seedLiveRegs() { | 
| Matthias Braun | 9f15a79 | 2016-11-18 19:43:18 +0000 | [diff] [blame] | 73 | NamedRegionTimer T("seed", "Seed Live Regs", TimerGroupName, | 
|  | 74 | TimerGroupDescription, TimePassesIsEnabled); | 
| Jakob Stoklund Olesen | a1f43dc | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 75 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { | 
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 76 | unsigned Reg = Register::index2VirtReg(i); | 
| Jakob Stoklund Olesen | a1f43dc | 2012-06-20 21:25:05 +0000 | [diff] [blame] | 77 | if (MRI->reg_nodbg_empty(Reg)) | 
|  | 78 | continue; | 
|  | 79 | enqueue(&LIS->getInterval(Reg)); | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 80 | } | 
|  | 81 | } | 
|  | 82 |  | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 83 | // Top-level driver to manage the queue of unassigned VirtRegs and call the | 
|  | 84 | // selectOrSplit implementation. | 
|  | 85 | void RegAllocBase::allocatePhysRegs() { | 
|  | 86 | seedLiveRegs(); | 
|  | 87 |  | 
|  | 88 | // Continue assigning vregs one at a time to available physical registers. | 
|  | 89 | while (LiveInterval *VirtReg = dequeue()) { | 
|  | 90 | assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned"); | 
|  | 91 |  | 
|  | 92 | // Unused registers can appear when the spiller coalesces snippets. | 
|  | 93 | if (MRI->reg_nodbg_empty(VirtReg->reg)) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 94 | LLVM_DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n'); | 
| Quentin Colombet | a799e2e | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 95 | aboutToRemoveInterval(*VirtReg); | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 96 | LIS->removeInterval(VirtReg->reg); | 
|  | 97 | continue; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | // Invalidate all interference queries, live ranges could have changed. | 
| Jakob Stoklund Olesen | 2d2dec9 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 101 | Matrix->invalidateVirtRegs(); | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 102 |  | 
|  | 103 | // selectOrSplit requests the allocator to return an available physical | 
|  | 104 | // register if possible and populate a list of new live intervals that | 
|  | 105 | // result from splitting. | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 106 | LLVM_DEBUG(dbgs() << "\nselectOrSplit " | 
|  | 107 | << TRI->getRegClassName(MRI->getRegClass(VirtReg->reg)) | 
|  | 108 | << ':' << *VirtReg << " w=" << VirtReg->weight << '\n'); | 
| Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 109 |  | 
|  | 110 | using VirtRegVec = SmallVector<unsigned, 4>; | 
|  | 111 |  | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 112 | VirtRegVec SplitVRegs; | 
|  | 113 | unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs); | 
|  | 114 |  | 
|  | 115 | if (AvailablePhysReg == ~0u) { | 
|  | 116 | // selectOrSplit failed to find a register! | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 117 | // Probably caused by an inline asm. | 
| Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 118 | MachineInstr *MI = nullptr; | 
| Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 119 | for (MachineRegisterInfo::reg_instr_iterator | 
|  | 120 | I = MRI->reg_instr_begin(VirtReg->reg), E = MRI->reg_instr_end(); | 
|  | 121 | I != E; ) { | 
| Nicolai Haehnle | 664ceed | 2019-05-15 17:29:58 +0000 | [diff] [blame] | 122 | MI = &*(I++); | 
|  | 123 | if (MI->isInlineAsm()) | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 124 | break; | 
| Owen Anderson | abb90c9 | 2014-03-13 06:02:25 +0000 | [diff] [blame] | 125 | } | 
| Nicolai Haehnle | 664ceed | 2019-05-15 17:29:58 +0000 | [diff] [blame] | 126 | if (MI && MI->isInlineAsm()) { | 
| Benjamin Kramer | 7200a46 | 2013-10-05 19:33:37 +0000 | [diff] [blame] | 127 | MI->emitError("inline assembly requires more registers than available"); | 
| Nicolai Haehnle | 664ceed | 2019-05-15 17:29:58 +0000 | [diff] [blame] | 128 | } else if (MI) { | 
|  | 129 | LLVMContext &Context = | 
|  | 130 | MI->getParent()->getParent()->getMMI().getModule()->getContext(); | 
|  | 131 | Context.emitError("ran out of registers during register allocation"); | 
|  | 132 | } else { | 
| Benjamin Kramer | 7200a46 | 2013-10-05 19:33:37 +0000 | [diff] [blame] | 133 | report_fatal_error("ran out of registers during register allocation"); | 
| Nicolai Haehnle | 664ceed | 2019-05-15 17:29:58 +0000 | [diff] [blame] | 134 | } | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 135 | // Keep going after reporting the error. | 
|  | 136 | VRM->assignVirt2Phys(VirtReg->reg, | 
|  | 137 | RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front()); | 
|  | 138 | continue; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | if (AvailablePhysReg) | 
| Jakob Stoklund Olesen | 2d2dec9 | 2012-06-20 22:52:29 +0000 | [diff] [blame] | 142 | Matrix->assign(*VirtReg, AvailablePhysReg); | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 143 |  | 
| Matt Arsenault | 5fbc870 | 2017-07-24 18:07:55 +0000 | [diff] [blame] | 144 | for (unsigned Reg : SplitVRegs) { | 
|  | 145 | assert(LIS->hasInterval(Reg)); | 
|  | 146 |  | 
|  | 147 | LiveInterval *SplitVirtReg = &LIS->getInterval(Reg); | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 148 | assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned"); | 
|  | 149 | if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) { | 
| Matt Arsenault | 5fbc870 | 2017-07-24 18:07:55 +0000 | [diff] [blame] | 150 | assert(SplitVirtReg->empty() && "Non-empty but used interval"); | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 151 | LLVM_DEBUG(dbgs() << "not queueing unused  " << *SplitVirtReg << '\n'); | 
| Quentin Colombet | a799e2e | 2015-01-08 01:16:39 +0000 | [diff] [blame] | 152 | aboutToRemoveInterval(*SplitVirtReg); | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 153 | LIS->removeInterval(SplitVirtReg->reg); | 
|  | 154 | continue; | 
|  | 155 | } | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 156 | LLVM_DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n"); | 
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 157 | assert(Register::isVirtualRegister(SplitVirtReg->reg) && | 
| Jakob Stoklund Olesen | a818d80 | 2012-01-11 22:28:30 +0000 | [diff] [blame] | 158 | "expect split value in virtual register"); | 
|  | 159 | enqueue(SplitVirtReg); | 
|  | 160 | ++NumNewQueued; | 
|  | 161 | } | 
|  | 162 | } | 
|  | 163 | } | 
| Wei Mi | 9a16d65 | 2016-04-13 03:08:27 +0000 | [diff] [blame] | 164 |  | 
|  | 165 | void RegAllocBase::postOptimization() { | 
|  | 166 | spiller().postOptimization(); | 
|  | 167 | for (auto DeadInst : DeadRemats) { | 
|  | 168 | LIS->RemoveMachineInstrFromMaps(*DeadInst); | 
|  | 169 | DeadInst->eraseFromParent(); | 
|  | 170 | } | 
|  | 171 | DeadRemats.clear(); | 
|  | 172 | } |