Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 1 | //===- RegAllocFast.cpp - A fast register allocator for debug code --------===// |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 10 | /// \file This register allocator allocates registers to a basic block at a |
| 11 | /// time, attempting to keep values in registers and reusing registers as |
| 12 | /// appropriate. |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/IndexedMap.h" |
| 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SparseSet.h" |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | #include "llvm/CodeGen/MachineInstr.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineOperand.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 31 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 32 | #include "llvm/CodeGen/RegisterClassInfo.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/TargetOpcodes.h" |
| 35 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 36 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 37 | #include "llvm/IR/DebugLoc.h" |
| 38 | #include "llvm/IR/Metadata.h" |
| 39 | #include "llvm/MC/MCInstrDesc.h" |
| 40 | #include "llvm/MC/MCRegisterInfo.h" |
| 41 | #include "llvm/Pass.h" |
| 42 | #include "llvm/Support/Casting.h" |
| 43 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Debug.h" |
| 45 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 46 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 47 | #include <cassert> |
| 48 | #include <tuple> |
| 49 | #include <vector> |
| 50 | |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 51 | using namespace llvm; |
| 52 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 53 | #define DEBUG_TYPE "regalloc" |
| 54 | |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 55 | STATISTIC(NumStores, "Number of stores added"); |
| 56 | STATISTIC(NumLoads , "Number of loads added"); |
Matthias Braun | 14af82a | 2018-11-07 02:04:07 +0000 | [diff] [blame] | 57 | STATISTIC(NumCoalesced, "Number of copies coalesced"); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 58 | |
| 59 | static RegisterRegAlloc |
| 60 | fastRegAlloc("fast", "fast register allocator", createFastRegisterAllocator); |
| 61 | |
| 62 | namespace { |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 63 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 64 | class RegAllocFast : public MachineFunctionPass { |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 65 | public: |
| 66 | static char ID; |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 67 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 68 | RegAllocFast() : MachineFunctionPass(ID), StackSlotForVirtReg(-1) {} |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 69 | |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 70 | private: |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 71 | MachineFrameInfo *MFI; |
Jakob Stoklund Olesen | 0ba2e2a | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 72 | MachineRegisterInfo *MRI; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 73 | const TargetRegisterInfo *TRI; |
| 74 | const TargetInstrInfo *TII; |
Jakob Stoklund Olesen | 50663b7 | 2011-06-02 18:35:30 +0000 | [diff] [blame] | 75 | RegisterClassInfo RegClassInfo; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 76 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 77 | /// Basic block currently being allocated. |
Jakob Stoklund Olesen | fb43e06 | 2010-05-17 02:07:22 +0000 | [diff] [blame] | 78 | MachineBasicBlock *MBB; |
| 79 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 80 | /// Maps virtual regs to the frame index where these values are spilled. |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 81 | IndexedMap<int, VirtReg2IndexFunctor> StackSlotForVirtReg; |
| 82 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 83 | /// Everything we know about a live virtual register. |
Jakob Stoklund Olesen | 1326681 | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 84 | struct LiveReg { |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 85 | MachineInstr *LastUse = nullptr; ///< Last instr to use reg. |
| 86 | unsigned VirtReg; ///< Virtual register number. |
| 87 | MCPhysReg PhysReg = 0; ///< Currently held here. |
| 88 | unsigned short LastOpNum = 0; ///< OpNum on LastUse. |
| 89 | bool Dirty = false; ///< Register needs spill. |
Jakob Stoklund Olesen | 1326681 | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 90 | |
Matthias Braun | ebcf543 | 2018-11-07 02:04:11 +0000 | [diff] [blame] | 91 | explicit LiveReg(unsigned VirtReg) : VirtReg(VirtReg) {} |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 92 | |
Andrew Trick | 1eb4a0d | 2012-04-20 20:05:28 +0000 | [diff] [blame] | 93 | unsigned getSparseSetIndex() const { |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 94 | return TargetRegisterInfo::virtReg2Index(VirtReg); |
| 95 | } |
Jakob Stoklund Olesen | 1326681 | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 98 | using LiveRegMap = SparseSet<LiveReg>; |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 99 | /// This map contains entries for each virtual register that is currently |
| 100 | /// available in a physical register. |
Jakob Stoklund Olesen | 1326681 | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 101 | LiveRegMap LiveVirtRegs; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 102 | |
Matthias Braun | ebcf543 | 2018-11-07 02:04:11 +0000 | [diff] [blame] | 103 | DenseMap<unsigned, SmallVector<MachineInstr *, 2>> LiveDbgValueMap; |
Devang Patel | d71bc1a | 2010-08-04 18:42:02 +0000 | [diff] [blame] | 104 | |
Matthias Braun | ebcf543 | 2018-11-07 02:04:11 +0000 | [diff] [blame] | 105 | /// State of a physical register. |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 106 | enum RegState { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 107 | /// A disabled register is not available for allocation, but an alias may |
| 108 | /// be in use. A register can only be moved out of the disabled state if |
| 109 | /// all aliases are disabled. |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 110 | regDisabled, |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 111 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 112 | /// A free register is not currently in use and can be allocated |
| 113 | /// immediately without checking aliases. |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 114 | regFree, |
| 115 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 116 | /// A reserved register has been assigned explicitly (e.g., setting up a |
| 117 | /// call parameter), and it remains reserved until it is used. |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 118 | regReserved |
| 119 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 120 | /// A register state may also be a virtual register number, indication |
| 121 | /// that the physical register is currently allocated to a virtual |
| 122 | /// register. In that case, LiveVirtRegs contains the inverse mapping. |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
Matthias Braun | ebcf543 | 2018-11-07 02:04:11 +0000 | [diff] [blame] | 125 | /// Maps each physical register to a RegState enum or a virtual register. |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 126 | std::vector<unsigned> PhysRegState; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 127 | |
Matthias Braun | a09d18d | 2017-09-09 00:52:45 +0000 | [diff] [blame] | 128 | SmallVector<unsigned, 16> VirtDead; |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 129 | SmallVector<MachineInstr *, 32> Coalesced; |
Matthias Braun | a09d18d | 2017-09-09 00:52:45 +0000 | [diff] [blame] | 130 | |
Matthias Braun | ebcf543 | 2018-11-07 02:04:11 +0000 | [diff] [blame] | 131 | using RegUnitSet = SparseSet<uint16_t, identity<uint16_t>>; |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 132 | /// Set of register units that are used in the current instruction, and so |
| 133 | /// cannot be allocated. |
Matthias Braun | ebcf543 | 2018-11-07 02:04:11 +0000 | [diff] [blame] | 134 | RegUnitSet UsedInInstr; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 135 | |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 136 | void setPhysRegState(MCPhysReg PhysReg, unsigned NewState); |
| 137 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 138 | /// Mark a physreg as used in this instruction. |
| 139 | void markRegUsedInInstr(MCPhysReg PhysReg) { |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 140 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) |
| 141 | UsedInInstr.insert(*Units); |
| 142 | } |
| 143 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 144 | /// Check if a physreg or any of its aliases are used in this instruction. |
| 145 | bool isRegUsedInInstr(MCPhysReg PhysReg) const { |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 146 | for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) |
| 147 | if (UsedInInstr.count(*Units)) |
| 148 | return true; |
| 149 | return false; |
| 150 | } |
| 151 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 152 | /// This flag is set when LiveRegMap will be cleared completely after |
| 153 | /// spilling all live registers. LiveRegMap entries should not be erased. |
| 154 | bool isBulkSpilling = false; |
Jakob Stoklund Olesen | 41f8dc8 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 155 | |
Alp Toker | 61007d8 | 2014-03-02 03:20:38 +0000 | [diff] [blame] | 156 | enum : unsigned { |
Matthias Braun | ebcf543 | 2018-11-07 02:04:11 +0000 | [diff] [blame] | 157 | spillClean = 50, |
Jakob Stoklund Olesen | 6649cda | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 158 | spillDirty = 100, |
| 159 | spillImpossible = ~0u |
| 160 | }; |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 161 | |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 162 | public: |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 163 | StringRef getPassName() const override { return "Fast Register Allocator"; } |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 164 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 165 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 166 | AU.setPreservesCFG(); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 167 | MachineFunctionPass::getAnalysisUsage(AU); |
| 168 | } |
| 169 | |
Matthias Braun | 90799ce | 2016-08-23 21:19:49 +0000 | [diff] [blame] | 170 | MachineFunctionProperties getRequiredProperties() const override { |
| 171 | return MachineFunctionProperties().set( |
| 172 | MachineFunctionProperties::Property::NoPHIs); |
| 173 | } |
| 174 | |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 175 | MachineFunctionProperties getSetProperties() const override { |
| 176 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 177 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | ad154c8 | 2016-03-28 17:05:30 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 180 | private: |
Fangrui Song | cb0bab8 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 181 | bool runOnMachineFunction(MachineFunction &MF) override; |
Matthias Braun | ebcf543 | 2018-11-07 02:04:11 +0000 | [diff] [blame] | 182 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 183 | void allocateBasicBlock(MachineBasicBlock &MBB); |
| 184 | void handleThroughOperands(MachineInstr &MI, |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 185 | SmallVectorImpl<unsigned> &VirtDead); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 186 | bool isLastUseOfLocalReg(const MachineOperand &MO) const; |
Jakob Stoklund Olesen | 84ce290 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 187 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 188 | void addKillFlag(const LiveReg &LRI); |
| 189 | void killVirtReg(LiveRegMap::iterator LRI); |
Jakob Stoklund Olesen | 955a0e7 | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 190 | void killVirtReg(unsigned VirtReg); |
Jakob Stoklund Olesen | 397068d | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 191 | void spillVirtReg(MachineBasicBlock::iterator MI, LiveRegMap::iterator); |
Jakob Stoklund Olesen | 8044c98 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 192 | void spillVirtReg(MachineBasicBlock::iterator MI, unsigned VirtReg); |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 193 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 194 | void usePhysReg(MachineOperand &MO); |
Quentin Colombet | 72f6d59 | 2018-01-29 23:42:37 +0000 | [diff] [blame] | 195 | void definePhysReg(MachineBasicBlock::iterator MI, MCPhysReg PhysReg, |
| 196 | RegState NewState); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 197 | unsigned calcSpillCost(MCPhysReg PhysReg) const; |
Quentin Colombet | 72f6d59 | 2018-01-29 23:42:37 +0000 | [diff] [blame] | 198 | void assignVirtToPhysReg(LiveReg &, MCPhysReg PhysReg); |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 199 | |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 200 | LiveRegMap::iterator findLiveVirtReg(unsigned VirtReg) { |
| 201 | return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg)); |
| 202 | } |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 203 | |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 204 | LiveRegMap::const_iterator findLiveVirtReg(unsigned VirtReg) const { |
| 205 | return LiveVirtRegs.find(TargetRegisterInfo::virtReg2Index(VirtReg)); |
| 206 | } |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 207 | |
Fangrui Song | cb0bab8 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 208 | LiveRegMap::iterator assignVirtToPhysReg(unsigned VirtReg, MCPhysReg PhysReg); |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 209 | LiveRegMap::iterator allocVirtReg(MachineInstr &MI, LiveRegMap::iterator, |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 210 | unsigned Hint); |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 211 | LiveRegMap::iterator defineVirtReg(MachineInstr &MI, unsigned OpNum, |
Jakob Stoklund Olesen | f915d14 | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 212 | unsigned VirtReg, unsigned Hint); |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 213 | LiveRegMap::iterator reloadVirtReg(MachineInstr &MI, unsigned OpNum, |
Jakob Stoklund Olesen | f915d14 | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 214 | unsigned VirtReg, unsigned Hint); |
Akira Hatanaka | d837be7 | 2012-10-31 00:56:01 +0000 | [diff] [blame] | 215 | void spillAll(MachineBasicBlock::iterator MI); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 216 | bool setPhysReg(MachineInstr &MI, unsigned OpNum, MCPhysReg PhysReg); |
| 217 | |
Matthias Braun | b4c76ff7 | 2018-11-07 02:04:12 +0000 | [diff] [blame] | 218 | int getStackSpaceFor(unsigned VirtReg); |
| 219 | void spill(MachineBasicBlock::iterator Before, unsigned VirtReg, |
| 220 | MCPhysReg AssignedReg, bool Kill); |
| 221 | void reload(MachineBasicBlock::iterator Before, unsigned VirtReg, |
| 222 | MCPhysReg PhysReg); |
| 223 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 224 | void dumpState(); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 225 | }; |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 226 | |
| 227 | } // end anonymous namespace |
| 228 | |
| 229 | char RegAllocFast::ID = 0; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 230 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 231 | INITIALIZE_PASS(RegAllocFast, "regallocfast", "Fast Register Allocator", false, |
| 232 | false) |
Quentin Colombet | 8155114 | 2017-07-07 19:25:42 +0000 | [diff] [blame] | 233 | |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 234 | void RegAllocFast::setPhysRegState(MCPhysReg PhysReg, unsigned NewState) { |
| 235 | PhysRegState[PhysReg] = NewState; |
| 236 | } |
| 237 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 238 | /// This allocates space for the specified virtual register to be held on the |
| 239 | /// stack. |
Matthias Braun | ebcf543 | 2018-11-07 02:04:11 +0000 | [diff] [blame] | 240 | int RegAllocFast::getStackSpaceFor(unsigned VirtReg) { |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 241 | // Find the location Reg would belong... |
| 242 | int SS = StackSlotForVirtReg[VirtReg]; |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 243 | // Already has space allocated? |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 244 | if (SS != -1) |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 245 | return SS; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 246 | |
| 247 | // Allocate a new stack object for this spill location... |
Matthias Braun | ebcf543 | 2018-11-07 02:04:11 +0000 | [diff] [blame] | 248 | const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 249 | unsigned Size = TRI->getSpillSize(RC); |
| 250 | unsigned Align = TRI->getSpillAlignment(RC); |
| 251 | int FrameIdx = MFI->CreateSpillStackObject(Size, Align); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 252 | |
| 253 | // Assign the slot. |
| 254 | StackSlotForVirtReg[VirtReg] = FrameIdx; |
| 255 | return FrameIdx; |
| 256 | } |
| 257 | |
Matthias Braun | b4c76ff7 | 2018-11-07 02:04:12 +0000 | [diff] [blame] | 258 | /// Insert spill instruction for \p AssignedReg before \p Before. Update |
| 259 | /// DBG_VALUEs with \p VirtReg operands with the stack slot. |
| 260 | void RegAllocFast::spill(MachineBasicBlock::iterator Before, unsigned VirtReg, |
| 261 | MCPhysReg AssignedReg, bool Kill) { |
| 262 | LLVM_DEBUG(dbgs() << "Spilling " << printReg(VirtReg, TRI) |
| 263 | << " in " << printReg(AssignedReg, TRI)); |
| 264 | int FI = getStackSpaceFor(VirtReg); |
| 265 | LLVM_DEBUG(dbgs() << " to stack slot #" << FI << "\n"); |
| 266 | |
| 267 | const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg); |
| 268 | TII->storeRegToStackSlot(*MBB, Before, AssignedReg, Kill, FI, &RC, TRI); |
| 269 | ++NumStores; |
| 270 | |
| 271 | // If this register is used by DBG_VALUE then insert new DBG_VALUE to |
| 272 | // identify spilled location as the place to find corresponding variable's |
| 273 | // value. |
| 274 | SmallVectorImpl<MachineInstr *> &LRIDbgValues = LiveDbgValueMap[VirtReg]; |
| 275 | for (MachineInstr *DBG : LRIDbgValues) { |
| 276 | MachineInstr *NewDV = buildDbgValueForSpill(*MBB, Before, *DBG, FI); |
| 277 | assert(NewDV->getParent() == MBB && "dangling parent pointer"); |
| 278 | (void)NewDV; |
| 279 | LLVM_DEBUG(dbgs() << "Inserting debug info due to spill:\n" << *NewDV); |
| 280 | } |
| 281 | // Now this register is spilled there is should not be any DBG_VALUE |
| 282 | // pointing to this register because they are all pointing to spilled value |
| 283 | // now. |
| 284 | LRIDbgValues.clear(); |
| 285 | } |
| 286 | |
| 287 | /// Insert reload instruction for \p PhysReg before \p Before. |
| 288 | void RegAllocFast::reload(MachineBasicBlock::iterator Before, unsigned VirtReg, |
| 289 | MCPhysReg PhysReg) { |
| 290 | LLVM_DEBUG(dbgs() << "Reloading " << printReg(VirtReg, TRI) << " into " |
| 291 | << printReg(PhysReg, TRI) << "\n"); |
| 292 | int FI = getStackSpaceFor(VirtReg); |
| 293 | const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg); |
| 294 | TII->loadRegFromStackSlot(*MBB, Before, PhysReg, FI, &RC, TRI); |
| 295 | ++NumLoads; |
| 296 | } |
| 297 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 298 | /// Return true if MO is the only remaining reference to its virtual register, |
| 299 | /// and it is guaranteed to be a block-local register. |
| 300 | bool RegAllocFast::isLastUseOfLocalReg(const MachineOperand &MO) const { |
Jakob Stoklund Olesen | 84ce290 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 301 | // If the register has ever been spilled or reloaded, we conservatively assume |
| 302 | // it is a global register used in multiple blocks. |
| 303 | if (StackSlotForVirtReg[MO.getReg()] != -1) |
| 304 | return false; |
| 305 | |
| 306 | // Check that the use/def chain has exactly one operand - MO. |
Jakob Stoklund Olesen | f71bc7b | 2012-08-08 23:44:01 +0000 | [diff] [blame] | 307 | MachineRegisterInfo::reg_nodbg_iterator I = MRI->reg_nodbg_begin(MO.getReg()); |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 308 | if (&*I != &MO) |
Jakob Stoklund Olesen | f71bc7b | 2012-08-08 23:44:01 +0000 | [diff] [blame] | 309 | return false; |
| 310 | return ++I == MRI->reg_nodbg_end(); |
Jakob Stoklund Olesen | 84ce290 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 313 | /// Set kill flags on last use of a virtual register. |
| 314 | void RegAllocFast::addKillFlag(const LiveReg &LR) { |
Jakob Stoklund Olesen | d2ef1fb | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 315 | if (!LR.LastUse) return; |
| 316 | MachineOperand &MO = LR.LastUse->getOperand(LR.LastOpNum); |
Jakob Stoklund Olesen | e0eddb2 | 2010-05-19 21:36:05 +0000 | [diff] [blame] | 317 | if (MO.isUse() && !LR.LastUse->isRegTiedToDefOperand(LR.LastOpNum)) { |
| 318 | if (MO.getReg() == LR.PhysReg) |
Jakob Stoklund Olesen | 663543b4 | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 319 | MO.setIsKill(); |
Quentin Colombet | 868ef84 | 2017-07-07 19:25:45 +0000 | [diff] [blame] | 320 | // else, don't do anything we are problably redefining a |
| 321 | // subreg of this register and given we don't track which |
| 322 | // lanes are actually dead, we cannot insert a kill flag here. |
| 323 | // Otherwise we may end up in a situation like this: |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 324 | // ... = (MO) physreg:sub1, implicit killed physreg |
Quentin Colombet | 868ef84 | 2017-07-07 19:25:45 +0000 | [diff] [blame] | 325 | // ... <== Here we would allow later pass to reuse physreg:sub1 |
| 326 | // which is potentially wrong. |
| 327 | // LR:sub0 = ... |
| 328 | // ... = LR.sub1 <== This is going to use physreg:sub1 |
Jakob Stoklund Olesen | 663543b4 | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 329 | } |
Jakob Stoklund Olesen | 955a0e7 | 2010-05-12 18:46:03 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 332 | /// Mark virtreg as no longer available. |
| 333 | void RegAllocFast::killVirtReg(LiveRegMap::iterator LRI) { |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 334 | addKillFlag(*LRI); |
Jakob Stoklund Olesen | bd5e076 | 2012-02-22 16:50:46 +0000 | [diff] [blame] | 335 | assert(PhysRegState[LRI->PhysReg] == LRI->VirtReg && |
| 336 | "Broken RegState mapping"); |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 337 | setPhysRegState(LRI->PhysReg, regFree); |
Jakob Stoklund Olesen | 8044c98 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 338 | // Erase from LiveVirtRegs unless we're spilling in bulk. |
| 339 | if (!isBulkSpilling) |
Jakob Stoklund Olesen | 397068d | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 340 | LiveVirtRegs.erase(LRI); |
Jakob Stoklund Olesen | 1326681 | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 343 | /// Mark virtreg as no longer available. |
| 344 | void RegAllocFast::killVirtReg(unsigned VirtReg) { |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 345 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 346 | "killVirtReg needs a virtual register"); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 347 | LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg); |
Jakob Stoklund Olesen | 397068d | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 348 | if (LRI != LiveVirtRegs.end()) |
| 349 | killVirtReg(LRI); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 352 | /// This method spills the value specified by VirtReg into the corresponding |
| 353 | /// stack slot if needed. |
| 354 | void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI, |
| 355 | unsigned VirtReg) { |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 356 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 357 | "Spilling a physical register is illegal!"); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 358 | LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg); |
Jakob Stoklund Olesen | 397068d | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 359 | assert(LRI != LiveVirtRegs.end() && "Spilling unmapped virtual register"); |
| 360 | spillVirtReg(MI, LRI); |
Jakob Stoklund Olesen | 41f8dc8 | 2010-05-14 00:02:20 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 363 | /// Do the actual work of spilling. |
| 364 | void RegAllocFast::spillVirtReg(MachineBasicBlock::iterator MI, |
| 365 | LiveRegMap::iterator LRI) { |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 366 | LiveReg &LR = *LRI; |
| 367 | assert(PhysRegState[LR.PhysReg] == LRI->VirtReg && "Broken RegState mapping"); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 368 | |
Jakob Stoklund Olesen | 11f1ba1 | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 369 | if (LR.Dirty) { |
Jakob Stoklund Olesen | 8044c98 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 370 | // If this physreg is used by the instruction, we want to kill it on the |
| 371 | // instruction, not on the spill. |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 372 | bool SpillKill = MachineBasicBlock::iterator(LR.LastUse) != MI; |
Jakob Stoklund Olesen | 11f1ba1 | 2010-05-11 23:24:47 +0000 | [diff] [blame] | 373 | LR.Dirty = false; |
Jakob Stoklund Olesen | 1326681 | 2010-05-11 23:24:45 +0000 | [diff] [blame] | 374 | |
Matthias Braun | b4c76ff7 | 2018-11-07 02:04:12 +0000 | [diff] [blame] | 375 | spill(MI, LRI->VirtReg, LR.PhysReg, SpillKill); |
| 376 | |
Jakob Stoklund Olesen | 397068d | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 377 | if (SpillKill) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 378 | LR.LastUse = nullptr; // Don't kill register again |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 379 | } |
Jakob Stoklund Olesen | 397068d | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 380 | killVirtReg(LRI); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 383 | /// Spill all dirty virtregs without killing them. |
| 384 | void RegAllocFast::spillAll(MachineBasicBlock::iterator MI) { |
Jakob Stoklund Olesen | f5e8c86 | 2010-05-17 15:30:37 +0000 | [diff] [blame] | 385 | if (LiveVirtRegs.empty()) return; |
Jakob Stoklund Olesen | 8044c98 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 386 | isBulkSpilling = true; |
Jakob Stoklund Olesen | 70563bb | 2010-05-17 20:01:22 +0000 | [diff] [blame] | 387 | // The LiveRegMap is keyed by an unsigned (the virtreg number), so the order |
| 388 | // of spilling here is deterministic, if arbitrary. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 389 | for (LiveRegMap::iterator I = LiveVirtRegs.begin(), E = LiveVirtRegs.end(); |
| 390 | I != E; ++I) |
| 391 | spillVirtReg(MI, I); |
Jakob Stoklund Olesen | 8044c98 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 392 | LiveVirtRegs.clear(); |
| 393 | isBulkSpilling = false; |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 394 | } |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 395 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 396 | /// Handle the direct use of a physical register. Check that the register is |
| 397 | /// not used by a virtreg. Kill the physreg, marking it free. This may add |
| 398 | /// implicit kills to MO->getParent() and invalidate MO. |
| 399 | void RegAllocFast::usePhysReg(MachineOperand &MO) { |
Hans Wennborg | 8eb336c | 2016-05-18 16:10:17 +0000 | [diff] [blame] | 400 | // Ignore undef uses. |
| 401 | if (MO.isUndef()) |
| 402 | return; |
| 403 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 404 | unsigned PhysReg = MO.getReg(); |
| 405 | assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) && |
| 406 | "Bad usePhysReg operand"); |
| 407 | |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 408 | markRegUsedInInstr(PhysReg); |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 409 | switch (PhysRegState[PhysReg]) { |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 410 | case regDisabled: |
| 411 | break; |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 412 | case regReserved: |
| 413 | PhysRegState[PhysReg] = regFree; |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 414 | LLVM_FALLTHROUGH; |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 415 | case regFree: |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 416 | MO.setIsKill(); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 417 | return; |
| 418 | default: |
Eric Christopher | 66a8bf5 | 2010-12-08 21:35:09 +0000 | [diff] [blame] | 419 | // The physreg was allocated to a virtual register. That means the value we |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 420 | // wanted has been clobbered. |
| 421 | llvm_unreachable("Instruction uses an allocated register"); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 424 | // Maybe a superregister is reserved? |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 425 | for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 426 | MCPhysReg Alias = *AI; |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 427 | switch (PhysRegState[Alias]) { |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 428 | case regDisabled: |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 429 | break; |
| 430 | case regReserved: |
Quentin Colombet | 079aba7 | 2014-12-03 23:38:08 +0000 | [diff] [blame] | 431 | // Either PhysReg is a subregister of Alias and we mark the |
| 432 | // whole register as free, or PhysReg is the superregister of |
| 433 | // Alias and we mark all the aliases as disabled before freeing |
| 434 | // PhysReg. |
| 435 | // In the latter case, since PhysReg was disabled, this means that |
| 436 | // its value is defined only by physical sub-registers. This check |
| 437 | // is performed by the assert of the default case in this loop. |
| 438 | // Note: The value of the superregister may only be partial |
| 439 | // defined, that is why regDisabled is a valid state for aliases. |
| 440 | assert((TRI->isSuperRegister(PhysReg, Alias) || |
| 441 | TRI->isSuperRegister(Alias, PhysReg)) && |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 442 | "Instruction is not using a subregister of a reserved register"); |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 443 | LLVM_FALLTHROUGH; |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 444 | case regFree: |
| 445 | if (TRI->isSuperRegister(PhysReg, Alias)) { |
| 446 | // Leave the superregister in the working set. |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 447 | setPhysRegState(Alias, regFree); |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 448 | MO.getParent()->addRegisterKilled(Alias, TRI, true); |
| 449 | return; |
| 450 | } |
| 451 | // Some other alias was in the working set - clear it. |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 452 | setPhysRegState(Alias, regDisabled); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 453 | break; |
| 454 | default: |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 455 | llvm_unreachable("Instruction uses an alias of an allocated register"); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 456 | } |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 457 | } |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 458 | |
| 459 | // All aliases are disabled, bring register into working set. |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 460 | setPhysRegState(PhysReg, regFree); |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 461 | MO.setIsKill(); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 464 | /// Mark PhysReg as reserved or free after spilling any virtregs. This is very |
| 465 | /// similar to defineVirtReg except the physreg is reserved instead of |
| 466 | /// allocated. |
Quentin Colombet | 72f6d59 | 2018-01-29 23:42:37 +0000 | [diff] [blame] | 467 | void RegAllocFast::definePhysReg(MachineBasicBlock::iterator MI, |
| 468 | MCPhysReg PhysReg, RegState NewState) { |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 469 | markRegUsedInInstr(PhysReg); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 470 | switch (unsigned VirtReg = PhysRegState[PhysReg]) { |
| 471 | case regDisabled: |
| 472 | break; |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 473 | default: |
Jakob Stoklund Olesen | 8044c98 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 474 | spillVirtReg(MI, VirtReg); |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 475 | LLVM_FALLTHROUGH; |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 476 | case regFree: |
| 477 | case regReserved: |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 478 | setPhysRegState(PhysReg, NewState); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 479 | return; |
| 480 | } |
| 481 | |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 482 | // This is a disabled register, disable all aliases. |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 483 | setPhysRegState(PhysReg, NewState); |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 484 | for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 485 | MCPhysReg Alias = *AI; |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 486 | switch (unsigned VirtReg = PhysRegState[Alias]) { |
| 487 | case regDisabled: |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 488 | break; |
| 489 | default: |
Jakob Stoklund Olesen | 8044c98 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 490 | spillVirtReg(MI, VirtReg); |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 491 | LLVM_FALLTHROUGH; |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 492 | case regFree: |
| 493 | case regReserved: |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 494 | setPhysRegState(Alias, regDisabled); |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 495 | if (TRI->isSuperRegister(PhysReg, Alias)) |
| 496 | return; |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 497 | break; |
| 498 | } |
| 499 | } |
| 500 | } |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 501 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 502 | /// Return the cost of spilling clearing out PhysReg and aliases so it is |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 503 | /// free for allocation. Returns 0 when PhysReg is free or disabled with all |
| 504 | /// aliases disabled - it can be allocated directly. |
| 505 | /// \returns spillImpossible when PhysReg or an alias can't be spilled. |
| 506 | unsigned RegAllocFast::calcSpillCost(MCPhysReg PhysReg) const { |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 507 | if (isRegUsedInInstr(PhysReg)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 508 | LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) |
| 509 | << " is already used in instr.\n"); |
Jakob Stoklund Olesen | 5857927 | 2010-05-17 21:02:08 +0000 | [diff] [blame] | 510 | return spillImpossible; |
Eric Christopher | de9d585 | 2011-04-12 22:17:44 +0000 | [diff] [blame] | 511 | } |
Jakob Stoklund Olesen | 6649cda | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 512 | switch (unsigned VirtReg = PhysRegState[PhysReg]) { |
| 513 | case regDisabled: |
| 514 | break; |
| 515 | case regFree: |
| 516 | return 0; |
| 517 | case regReserved: |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 518 | LLVM_DEBUG(dbgs() << printReg(VirtReg, TRI) << " corresponding " |
| 519 | << printReg(PhysReg, TRI) << " is reserved already.\n"); |
Jakob Stoklund Olesen | 6649cda | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 520 | return spillImpossible; |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 521 | default: { |
| 522 | LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg); |
| 523 | assert(I != LiveVirtRegs.end() && "Missing VirtReg entry"); |
| 524 | return I->Dirty ? spillDirty : spillClean; |
| 525 | } |
Jakob Stoklund Olesen | 6649cda | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Eric Christopher | c378336 | 2011-04-12 00:48:08 +0000 | [diff] [blame] | 528 | // This is a disabled register, add up cost of aliases. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 529 | LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is disabled.\n"); |
Jakob Stoklund Olesen | 6649cda | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 530 | unsigned Cost = 0; |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 531 | for (MCRegAliasIterator AI(PhysReg, TRI, false); AI.isValid(); ++AI) { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 532 | MCPhysReg Alias = *AI; |
Jakob Stoklund Olesen | 6649cda | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 533 | switch (unsigned VirtReg = PhysRegState[Alias]) { |
| 534 | case regDisabled: |
| 535 | break; |
| 536 | case regFree: |
| 537 | ++Cost; |
| 538 | break; |
| 539 | case regReserved: |
| 540 | return spillImpossible; |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 541 | default: { |
| 542 | LiveRegMap::const_iterator I = findLiveVirtReg(VirtReg); |
| 543 | assert(I != LiveVirtRegs.end() && "Missing VirtReg entry"); |
| 544 | Cost += I->Dirty ? spillDirty : spillClean; |
Jakob Stoklund Olesen | 6649cda | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 545 | break; |
| 546 | } |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 547 | } |
Jakob Stoklund Olesen | 6649cda | 2010-05-17 15:30:32 +0000 | [diff] [blame] | 548 | } |
| 549 | return Cost; |
| 550 | } |
| 551 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 552 | /// This method updates local state so that we know that PhysReg is the |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 553 | /// proper container for VirtReg now. The physical register must not be used |
| 554 | /// for anything else when this is called. |
| 555 | void RegAllocFast::assignVirtToPhysReg(LiveReg &LR, MCPhysReg PhysReg) { |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 556 | unsigned VirtReg = LR.VirtReg; |
| 557 | LLVM_DEBUG(dbgs() << "Assigning " << printReg(VirtReg, TRI) << " to " |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 558 | << printReg(PhysReg, TRI) << "\n"); |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 559 | assert(LR.PhysReg == 0 && "Already assigned a physreg"); |
| 560 | assert(PhysReg != 0 && "Trying to assign no register"); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 561 | LR.PhysReg = PhysReg; |
Matthias Braun | 0804dca | 2018-11-07 06:57:00 +0000 | [diff] [blame^] | 562 | setPhysRegState(PhysReg, VirtReg); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 565 | RegAllocFast::LiveRegMap::iterator |
| 566 | RegAllocFast::assignVirtToPhysReg(unsigned VirtReg, MCPhysReg PhysReg) { |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 567 | LiveRegMap::iterator LRI = findLiveVirtReg(VirtReg); |
| 568 | assert(LRI != LiveVirtRegs.end() && "VirtReg disappeared"); |
| 569 | assignVirtToPhysReg(*LRI, PhysReg); |
| 570 | return LRI; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 573 | /// Allocates a physical register for VirtReg. |
| 574 | RegAllocFast::LiveRegMap::iterator RegAllocFast::allocVirtReg(MachineInstr &MI, |
| 575 | LiveRegMap::iterator LRI, unsigned Hint) { |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 576 | const unsigned VirtReg = LRI->VirtReg; |
Jakob Stoklund Olesen | d2ef1fb | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 577 | |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 578 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 579 | "Can only allocate virtual registers"); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 580 | |
Jakob Stoklund Olesen | 0ba2e2a | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 581 | // Take hint when possible. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 582 | const TargetRegisterClass &RC = *MRI->getRegClass(VirtReg); |
| 583 | if (TargetRegisterInfo::isPhysicalRegister(Hint) && |
| 584 | MRI->isAllocatable(Hint) && RC.contains(Hint)) { |
Jakob Stoklund Olesen | fb03a92 | 2011-06-13 03:26:46 +0000 | [diff] [blame] | 585 | // Ignore the hint if we would have to spill a dirty register. |
| 586 | unsigned Cost = calcSpillCost(Hint); |
| 587 | if (Cost < spillDirty) { |
| 588 | if (Cost) |
| 589 | definePhysReg(MI, Hint, regFree); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 590 | // definePhysReg may kill virtual registers and modify LiveVirtRegs. |
| 591 | // That invalidates LRI, so run a new lookup for VirtReg. |
| 592 | return assignVirtToPhysReg(VirtReg, Hint); |
Jakob Stoklund Olesen | 0ba2e2a | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 596 | // First try to find a completely free register. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 597 | ArrayRef<MCPhysReg> AO = RegClassInfo.getOrder(&RC); |
| 598 | for (MCPhysReg PhysReg : AO) { |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 599 | if (PhysRegState[PhysReg] == regFree && !isRegUsedInInstr(PhysReg)) { |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 600 | assignVirtToPhysReg(*LRI, PhysReg); |
| 601 | return LRI; |
| 602 | } |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 605 | LLVM_DEBUG(dbgs() << "Allocating " << printReg(VirtReg) << " from " |
| 606 | << TRI->getRegClassName(&RC) << "\n"); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 607 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 608 | unsigned BestReg = 0; |
| 609 | unsigned BestCost = spillImpossible; |
| 610 | for (MCPhysReg PhysReg : AO) { |
| 611 | unsigned Cost = calcSpillCost(PhysReg); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 612 | LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << "\n"); |
| 613 | LLVM_DEBUG(dbgs() << "\tCost: " << Cost << "\n"); |
| 614 | LLVM_DEBUG(dbgs() << "\tBestCost: " << BestCost << "\n"); |
Jakob Stoklund Olesen | f5e8c86 | 2010-05-17 15:30:37 +0000 | [diff] [blame] | 615 | // Cost is 0 when all aliases are already disabled. |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 616 | if (Cost == 0) { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 617 | assignVirtToPhysReg(*LRI, PhysReg); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 618 | return LRI; |
| 619 | } |
Jakob Stoklund Olesen | f5e8c86 | 2010-05-17 15:30:37 +0000 | [diff] [blame] | 620 | if (Cost < BestCost) |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 621 | BestReg = PhysReg, BestCost = Cost; |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | if (BestReg) { |
Jakob Stoklund Olesen | f5e8c86 | 2010-05-17 15:30:37 +0000 | [diff] [blame] | 625 | definePhysReg(MI, BestReg, regFree); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 626 | // definePhysReg may kill virtual registers and modify LiveVirtRegs. |
| 627 | // That invalidates LRI, so run a new lookup for VirtReg. |
| 628 | return assignVirtToPhysReg(VirtReg, BestReg); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Jakob Stoklund Olesen | 54f7c59 | 2011-07-02 07:17:37 +0000 | [diff] [blame] | 631 | // Nothing we can do. Report an error and keep going with a bad allocation. |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 632 | if (MI.isInlineAsm()) |
| 633 | MI.emitError("inline assembly requires more registers than available"); |
Benjamin Kramer | 7200a46 | 2013-10-05 19:33:37 +0000 | [diff] [blame] | 634 | else |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 635 | MI.emitError("ran out of registers during register allocation"); |
Jakob Stoklund Olesen | 54f7c59 | 2011-07-02 07:17:37 +0000 | [diff] [blame] | 636 | definePhysReg(MI, *AO.begin(), regFree); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 637 | return assignVirtToPhysReg(VirtReg, *AO.begin()); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 640 | /// Allocates a register for VirtReg and mark it as dirty. |
| 641 | RegAllocFast::LiveRegMap::iterator RegAllocFast::defineVirtReg(MachineInstr &MI, |
| 642 | unsigned OpNum, |
| 643 | unsigned VirtReg, |
| 644 | unsigned Hint) { |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 645 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 646 | "Not a virtual register"); |
Jakob Stoklund Olesen | 397068d | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 647 | LiveRegMap::iterator LRI; |
Jakob Stoklund Olesen | d2ef1fb | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 648 | bool New; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 649 | std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg)); |
Jakob Stoklund Olesen | 7d22a81b | 2010-05-17 04:50:57 +0000 | [diff] [blame] | 650 | if (New) { |
| 651 | // If there is no hint, peek at the only use of this register. |
| 652 | if ((!Hint || !TargetRegisterInfo::isPhysicalRegister(Hint)) && |
| 653 | MRI->hasOneNonDBGUse(VirtReg)) { |
Owen Anderson | 16c6bf4 | 2014-03-13 23:12:04 +0000 | [diff] [blame] | 654 | const MachineInstr &UseMI = *MRI->use_instr_nodbg_begin(VirtReg); |
Jakob Stoklund Olesen | 7d22a81b | 2010-05-17 04:50:57 +0000 | [diff] [blame] | 655 | // It's a copy, use the destination register as a hint. |
Jakob Stoklund Olesen | 4c82a9e | 2010-07-03 00:04:37 +0000 | [diff] [blame] | 656 | if (UseMI.isCopyLike()) |
| 657 | Hint = UseMI.getOperand(0).getReg(); |
Jakob Stoklund Olesen | 7d22a81b | 2010-05-17 04:50:57 +0000 | [diff] [blame] | 658 | } |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 659 | LRI = allocVirtReg(MI, LRI, Hint); |
| 660 | } else if (LRI->LastUse) { |
Jakob Stoklund Olesen | 663543b4 | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 661 | // Redefining a live register - kill at the last use, unless it is this |
| 662 | // instruction defining VirtReg multiple times. |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 663 | if (LRI->LastUse != &MI || LRI->LastUse->getOperand(LRI->LastOpNum).isUse()) |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 664 | addKillFlag(*LRI); |
Jakob Stoklund Olesen | 663543b4 | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 665 | } |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 666 | assert(LRI->PhysReg && "Register not assigned"); |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 667 | LRI->LastUse = &MI; |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 668 | LRI->LastOpNum = OpNum; |
| 669 | LRI->Dirty = true; |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 670 | markRegUsedInInstr(LRI->PhysReg); |
Jakob Stoklund Olesen | f915d14 | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 671 | return LRI; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 674 | /// Make sure VirtReg is available in a physreg and return it. |
| 675 | RegAllocFast::LiveRegMap::iterator RegAllocFast::reloadVirtReg(MachineInstr &MI, |
| 676 | unsigned OpNum, |
| 677 | unsigned VirtReg, |
| 678 | unsigned Hint) { |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 679 | assert(TargetRegisterInfo::isVirtualRegister(VirtReg) && |
| 680 | "Not a virtual register"); |
Jakob Stoklund Olesen | 397068d | 2010-05-17 02:49:15 +0000 | [diff] [blame] | 681 | LiveRegMap::iterator LRI; |
Jakob Stoklund Olesen | d2ef1fb | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 682 | bool New; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 683 | std::tie(LRI, New) = LiveVirtRegs.insert(LiveReg(VirtReg)); |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 684 | MachineOperand &MO = MI.getOperand(OpNum); |
Jakob Stoklund Olesen | d2ef1fb | 2010-05-17 02:07:29 +0000 | [diff] [blame] | 685 | if (New) { |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 686 | LRI = allocVirtReg(MI, LRI, Hint); |
Matthias Braun | b4c76ff7 | 2018-11-07 02:04:12 +0000 | [diff] [blame] | 687 | reload(MI, VirtReg, LRI->PhysReg); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 688 | } else if (LRI->Dirty) { |
Jakob Stoklund Olesen | 84ce290 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 689 | if (isLastUseOfLocalReg(MO)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 690 | LLVM_DEBUG(dbgs() << "Killing last use: " << MO << "\n"); |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 691 | if (MO.isUse()) |
| 692 | MO.setIsKill(); |
| 693 | else |
| 694 | MO.setIsDead(); |
Jakob Stoklund Olesen | 84ce290 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 695 | } else if (MO.isKill()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 696 | LLVM_DEBUG(dbgs() << "Clearing dubious kill: " << MO << "\n"); |
Jakob Stoklund Olesen | 84ce290 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 697 | MO.setIsKill(false); |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 698 | } else if (MO.isDead()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 699 | LLVM_DEBUG(dbgs() << "Clearing dubious dead: " << MO << "\n"); |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 700 | MO.setIsDead(false); |
Jakob Stoklund Olesen | 84ce290 | 2010-05-15 06:09:08 +0000 | [diff] [blame] | 701 | } |
Jakob Stoklund Olesen | edd3d9d | 2010-05-17 03:26:06 +0000 | [diff] [blame] | 702 | } else if (MO.isKill()) { |
| 703 | // We must remove kill flags from uses of reloaded registers because the |
| 704 | // register would be killed immediately, and there might be a second use: |
Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 705 | // %foo = OR killed %x, %x |
Jakob Stoklund Olesen | edd3d9d | 2010-05-17 03:26:06 +0000 | [diff] [blame] | 706 | // This would cause a second reload of %x into a different register. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 707 | LLVM_DEBUG(dbgs() << "Clearing clean kill: " << MO << "\n"); |
Jakob Stoklund Olesen | edd3d9d | 2010-05-17 03:26:06 +0000 | [diff] [blame] | 708 | MO.setIsKill(false); |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 709 | } else if (MO.isDead()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 710 | LLVM_DEBUG(dbgs() << "Clearing clean dead: " << MO << "\n"); |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 711 | MO.setIsDead(false); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 712 | } |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 713 | assert(LRI->PhysReg && "Register not assigned"); |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 714 | LRI->LastUse = &MI; |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 715 | LRI->LastOpNum = OpNum; |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 716 | markRegUsedInInstr(LRI->PhysReg); |
Jakob Stoklund Olesen | f915d14 | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 717 | return LRI; |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 718 | } |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 719 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 720 | /// Changes operand OpNum in MI the refer the PhysReg, considering subregs. This |
| 721 | /// may invalidate any operand pointers. Return true if the operand kills its |
| 722 | /// register. |
| 723 | bool RegAllocFast::setPhysReg(MachineInstr &MI, unsigned OpNum, |
| 724 | MCPhysReg PhysReg) { |
| 725 | MachineOperand &MO = MI.getOperand(OpNum); |
Jakob Stoklund Olesen | a13fd12 | 2012-05-14 21:30:58 +0000 | [diff] [blame] | 726 | bool Dead = MO.isDead(); |
Jakob Stoklund Olesen | e07a408 | 2010-05-17 02:49:21 +0000 | [diff] [blame] | 727 | if (!MO.getSubReg()) { |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 728 | MO.setReg(PhysReg); |
Geoff Berry | f8bf2ec | 2018-02-23 18:25:08 +0000 | [diff] [blame] | 729 | MO.setIsRenamable(true); |
Jakob Stoklund Olesen | a13fd12 | 2012-05-14 21:30:58 +0000 | [diff] [blame] | 730 | return MO.isKill() || Dead; |
Jakob Stoklund Olesen | e07a408 | 2010-05-17 02:49:21 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | // Handle subregister index. |
| 734 | MO.setReg(PhysReg ? TRI->getSubReg(PhysReg, MO.getSubReg()) : 0); |
Geoff Berry | f8bf2ec | 2018-02-23 18:25:08 +0000 | [diff] [blame] | 735 | MO.setIsRenamable(true); |
Jakob Stoklund Olesen | e07a408 | 2010-05-17 02:49:21 +0000 | [diff] [blame] | 736 | MO.setSubReg(0); |
Jakob Stoklund Olesen | e0eddb2 | 2010-05-19 21:36:05 +0000 | [diff] [blame] | 737 | |
| 738 | // A kill flag implies killing the full register. Add corresponding super |
| 739 | // register kill. |
| 740 | if (MO.isKill()) { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 741 | MI.addRegisterKilled(PhysReg, TRI, true); |
Jakob Stoklund Olesen | e07a408 | 2010-05-17 02:49:21 +0000 | [diff] [blame] | 742 | return true; |
| 743 | } |
Jakob Stoklund Olesen | dc2e0cd | 2012-05-14 21:10:25 +0000 | [diff] [blame] | 744 | |
| 745 | // A <def,read-undef> of a sub-register requires an implicit def of the full |
| 746 | // register. |
| 747 | if (MO.isDef() && MO.isUndef()) |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 748 | MI.addRegisterDefined(PhysReg, TRI); |
Jakob Stoklund Olesen | dc2e0cd | 2012-05-14 21:10:25 +0000 | [diff] [blame] | 749 | |
Jakob Stoklund Olesen | a13fd12 | 2012-05-14 21:30:58 +0000 | [diff] [blame] | 750 | return Dead; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 753 | // Handles special instruction operand like early clobbers and tied ops when |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 754 | // there are additional physreg defines. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 755 | void RegAllocFast::handleThroughOperands(MachineInstr &MI, |
| 756 | SmallVectorImpl<unsigned> &VirtDead) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 757 | LLVM_DEBUG(dbgs() << "Scanning for through registers:"); |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 758 | SmallSet<unsigned, 8> ThroughRegs; |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 759 | for (const MachineOperand &MO : MI.operands()) { |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 760 | if (!MO.isReg()) continue; |
| 761 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 762 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 763 | continue; |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 764 | if (MO.isEarlyClobber() || (MO.isUse() && MO.isTied()) || |
| 765 | (MO.getSubReg() && MI.readsVirtualRegister(Reg))) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 766 | if (ThroughRegs.insert(Reg).second) |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 767 | LLVM_DEBUG(dbgs() << ' ' << printReg(Reg)); |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
| 771 | // If any physreg defines collide with preallocated through registers, |
| 772 | // we must spill and reallocate. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 773 | LLVM_DEBUG(dbgs() << "\nChecking for physdef collisions.\n"); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 774 | for (const MachineOperand &MO : MI.operands()) { |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 775 | if (!MO.isReg() || !MO.isDef()) continue; |
| 776 | unsigned Reg = MO.getReg(); |
| 777 | if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 778 | markRegUsedInInstr(Reg); |
Jakob Stoklund Olesen | 9b09cf0 | 2012-06-01 22:38:17 +0000 | [diff] [blame] | 779 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { |
Jakob Stoklund Olesen | 9b09cf0 | 2012-06-01 22:38:17 +0000 | [diff] [blame] | 780 | if (ThroughRegs.count(PhysRegState[*AI])) |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 781 | definePhysReg(MI, *AI, regFree); |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 785 | SmallVector<unsigned, 8> PartialDefs; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 786 | LLVM_DEBUG(dbgs() << "Allocating tied uses.\n"); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 787 | for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { |
| 788 | const MachineOperand &MO = MI.getOperand(I); |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 789 | if (!MO.isReg()) continue; |
| 790 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 791 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue; |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 792 | if (MO.isUse()) { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 793 | if (!MO.isTied()) continue; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 794 | LLVM_DEBUG(dbgs() << "Operand " << I << "(" << MO |
| 795 | << ") is tied to operand " << MI.findTiedOperandIdx(I) |
| 796 | << ".\n"); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 797 | LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0); |
| 798 | MCPhysReg PhysReg = LRI->PhysReg; |
| 799 | setPhysReg(MI, I, PhysReg); |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 800 | // Note: we don't update the def operand yet. That would cause the normal |
| 801 | // def-scan to attempt spilling. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 802 | } else if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 803 | LLVM_DEBUG(dbgs() << "Partial redefine: " << MO << "\n"); |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 804 | // Reload the register, but don't assign to the operand just yet. |
| 805 | // That would confuse the later phys-def processing pass. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 806 | LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, 0); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 807 | PartialDefs.push_back(LRI->PhysReg); |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 811 | LLVM_DEBUG(dbgs() << "Allocating early clobbers.\n"); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 812 | for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { |
| 813 | const MachineOperand &MO = MI.getOperand(I); |
Rafael Espindola | 2021f38 | 2011-11-22 06:27:18 +0000 | [diff] [blame] | 814 | if (!MO.isReg()) continue; |
| 815 | unsigned Reg = MO.getReg(); |
| 816 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue; |
| 817 | if (!MO.isEarlyClobber()) |
| 818 | continue; |
| 819 | // Note: defineVirtReg may invalidate MO. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 820 | LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, 0); |
| 821 | MCPhysReg PhysReg = LRI->PhysReg; |
| 822 | if (setPhysReg(MI, I, PhysReg)) |
Rafael Espindola | 2021f38 | 2011-11-22 06:27:18 +0000 | [diff] [blame] | 823 | VirtDead.push_back(Reg); |
| 824 | } |
| 825 | |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 826 | // Restore UsedInInstr to a state usable for allocating normal virtual uses. |
Jakob Stoklund Olesen | a2136be | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 827 | UsedInInstr.clear(); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 828 | for (const MachineOperand &MO : MI.operands()) { |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 829 | if (!MO.isReg() || (MO.isDef() && !MO.isEarlyClobber())) continue; |
| 830 | unsigned Reg = MO.getReg(); |
| 831 | if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 832 | LLVM_DEBUG(dbgs() << "\tSetting " << printReg(Reg, TRI) |
| 833 | << " as used in instr\n"); |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 834 | markRegUsedInInstr(Reg); |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 835 | } |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 836 | |
| 837 | // Also mark PartialDefs as used to avoid reallocation. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 838 | for (unsigned PartialDef : PartialDefs) |
| 839 | markRegUsedInInstr(PartialDef); |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 842 | #ifndef NDEBUG |
| 843 | void RegAllocFast::dumpState() { |
| 844 | for (unsigned Reg = 1, E = TRI->getNumRegs(); Reg != E; ++Reg) { |
| 845 | if (PhysRegState[Reg] == regDisabled) continue; |
Francis Visoiu Mistrih | c71cced | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 846 | dbgs() << " " << printReg(Reg, TRI); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 847 | switch(PhysRegState[Reg]) { |
| 848 | case regFree: |
| 849 | break; |
| 850 | case regReserved: |
| 851 | dbgs() << "*"; |
| 852 | break; |
| 853 | default: { |
Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 854 | dbgs() << '=' << printReg(PhysRegState[Reg]); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 855 | LiveRegMap::iterator I = findLiveVirtReg(PhysRegState[Reg]); |
| 856 | assert(I != LiveVirtRegs.end() && "Missing VirtReg entry"); |
| 857 | if (I->Dirty) |
| 858 | dbgs() << "*"; |
| 859 | assert(I->PhysReg == Reg && "Bad inverse map"); |
| 860 | break; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | dbgs() << '\n'; |
| 865 | // Check that LiveVirtRegs is the inverse. |
| 866 | for (LiveRegMap::iterator i = LiveVirtRegs.begin(), |
| 867 | e = LiveVirtRegs.end(); i != e; ++i) { |
| 868 | assert(TargetRegisterInfo::isVirtualRegister(i->VirtReg) && |
| 869 | "Bad map key"); |
| 870 | assert(TargetRegisterInfo::isPhysicalRegister(i->PhysReg) && |
| 871 | "Bad map value"); |
| 872 | assert(PhysRegState[i->PhysReg] == i->VirtReg && "Bad inverse map"); |
| 873 | } |
| 874 | } |
| 875 | #endif |
| 876 | |
| 877 | void RegAllocFast::allocateBasicBlock(MachineBasicBlock &MBB) { |
| 878 | this->MBB = &MBB; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 879 | LLVM_DEBUG(dbgs() << "\nAllocating " << MBB); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 880 | |
| 881 | PhysRegState.assign(TRI->getNumRegs(), regDisabled); |
Jakob Stoklund Olesen | 9c4cd1b | 2012-02-22 01:02:37 +0000 | [diff] [blame] | 882 | assert(LiveVirtRegs.empty() && "Mapping not cleared from last block?"); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 883 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 884 | MachineBasicBlock::iterator MII = MBB.begin(); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 885 | |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 886 | // Add live-in registers as live. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 887 | for (const MachineBasicBlock::RegisterMaskPair LI : MBB.liveins()) |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 888 | if (MRI->isAllocatable(LI.PhysReg)) |
Quentin Colombet | 72f6d59 | 2018-01-29 23:42:37 +0000 | [diff] [blame] | 889 | definePhysReg(MII, LI.PhysReg, regReserved); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 890 | |
Matthias Braun | a09d18d | 2017-09-09 00:52:45 +0000 | [diff] [blame] | 891 | VirtDead.clear(); |
| 892 | Coalesced.clear(); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 893 | |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 894 | // Otherwise, sequentially allocate each instruction in the MBB. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 895 | for (MachineInstr &MI : MBB) { |
| 896 | const MCInstrDesc &MCID = MI.getDesc(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 897 | LLVM_DEBUG(dbgs() << "\n>> " << MI << "Regs:"; dumpState()); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 898 | |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 899 | // Debug values are not allowed to change codegen in any way. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 900 | if (MI.isDebugValue()) { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 901 | MachineInstr *DebugMI = &MI; |
Reid Kleckner | 9e6c309 | 2017-09-15 21:49:56 +0000 | [diff] [blame] | 902 | MachineOperand &MO = DebugMI->getOperand(0); |
| 903 | |
| 904 | // Ignore DBG_VALUEs that aren't based on virtual registers. These are |
| 905 | // mostly constants and frame indices. |
| 906 | if (!MO.isReg()) |
| 907 | continue; |
| 908 | unsigned Reg = MO.getReg(); |
| 909 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 910 | continue; |
| 911 | |
| 912 | // See if this virtual register has already been allocated to a physical |
| 913 | // register or spilled to a stack slot. |
| 914 | LiveRegMap::iterator LRI = findLiveVirtReg(Reg); |
| 915 | if (LRI != LiveVirtRegs.end()) |
| 916 | setPhysReg(*DebugMI, 0, LRI->PhysReg); |
| 917 | else { |
| 918 | int SS = StackSlotForVirtReg[Reg]; |
| 919 | if (SS != -1) { |
| 920 | // Modify DBG_VALUE now that the value is in a spill slot. |
| 921 | updateDbgValueForSpill(*DebugMI, SS); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 922 | LLVM_DEBUG(dbgs() << "Modifying debug info due to spill:" |
| 923 | << "\t" << *DebugMI); |
Reid Kleckner | 9e6c309 | 2017-09-15 21:49:56 +0000 | [diff] [blame] | 924 | continue; |
Devang Patel | 57e7237 | 2010-07-09 21:48:31 +0000 | [diff] [blame] | 925 | } |
Reid Kleckner | 9e6c309 | 2017-09-15 21:49:56 +0000 | [diff] [blame] | 926 | |
| 927 | // We can't allocate a physreg for a DebugValue, sorry! |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 928 | LLVM_DEBUG(dbgs() << "Unable to allocate vreg used by DBG_VALUE"); |
Reid Kleckner | 9e6c309 | 2017-09-15 21:49:56 +0000 | [diff] [blame] | 929 | MO.setReg(0); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 930 | } |
Reid Kleckner | 9e6c309 | 2017-09-15 21:49:56 +0000 | [diff] [blame] | 931 | |
| 932 | // If Reg hasn't been spilled, put this DBG_VALUE in LiveDbgValueMap so |
| 933 | // that future spills of Reg will have DBG_VALUEs. |
| 934 | LiveDbgValueMap[Reg].push_back(DebugMI); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 935 | continue; |
| 936 | } |
| 937 | |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 938 | if (MI.isDebugLabel()) |
| 939 | continue; |
| 940 | |
Jakob Stoklund Olesen | 0ba2e2a | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 941 | // If this is a copy, we may be able to coalesce. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 942 | unsigned CopySrcReg = 0; |
| 943 | unsigned CopyDstReg = 0; |
| 944 | unsigned CopySrcSub = 0; |
| 945 | unsigned CopyDstSub = 0; |
| 946 | if (MI.isCopy()) { |
| 947 | CopyDstReg = MI.getOperand(0).getReg(); |
| 948 | CopySrcReg = MI.getOperand(1).getReg(); |
| 949 | CopyDstSub = MI.getOperand(0).getSubReg(); |
| 950 | CopySrcSub = MI.getOperand(1).getSubReg(); |
Jakob Stoklund Olesen | 37c42a3 | 2010-07-16 04:45:42 +0000 | [diff] [blame] | 951 | } |
Jakob Stoklund Olesen | 0ba2e2a | 2010-05-13 00:19:43 +0000 | [diff] [blame] | 952 | |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 953 | // Track registers used by instruction. |
Jakob Stoklund Olesen | a2136be | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 954 | UsedInInstr.clear(); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 955 | |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 956 | // First scan. |
| 957 | // Mark physreg uses and early clobbers as used. |
Jakob Stoklund Olesen | e68b814 | 2010-05-14 21:55:52 +0000 | [diff] [blame] | 958 | // Find the end of the virtreg operands |
| 959 | unsigned VirtOpEnd = 0; |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 960 | bool hasTiedOps = false; |
| 961 | bool hasEarlyClobbers = false; |
| 962 | bool hasPartialRedefs = false; |
| 963 | bool hasPhysDefs = false; |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 964 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 965 | MachineOperand &MO = MI.getOperand(i); |
Chad Rosier | 8d2c229 | 2012-11-06 22:52:42 +0000 | [diff] [blame] | 966 | // Make sure MRI knows about registers clobbered by regmasks. |
| 967 | if (MO.isRegMask()) { |
| 968 | MRI->addPhysRegsUsedFromRegMask(MO.getRegMask()); |
| 969 | continue; |
| 970 | } |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 971 | if (!MO.isReg()) continue; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 972 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | e68b814 | 2010-05-14 21:55:52 +0000 | [diff] [blame] | 973 | if (!Reg) continue; |
| 974 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 975 | VirtOpEnd = i+1; |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 976 | if (MO.isUse()) { |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 977 | hasTiedOps = hasTiedOps || |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 978 | MCID.getOperandConstraint(i, MCOI::TIED_TO) != -1; |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 979 | } else { |
| 980 | if (MO.isEarlyClobber()) |
| 981 | hasEarlyClobbers = true; |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 982 | if (MO.getSubReg() && MI.readsVirtualRegister(Reg)) |
Jakob Stoklund Olesen | dadea5b | 2010-06-29 19:15:30 +0000 | [diff] [blame] | 983 | hasPartialRedefs = true; |
| 984 | } |
Jakob Stoklund Olesen | e68b814 | 2010-05-14 21:55:52 +0000 | [diff] [blame] | 985 | continue; |
| 986 | } |
Jakob Stoklund Olesen | f67bf3e | 2012-10-15 22:41:03 +0000 | [diff] [blame] | 987 | if (!MRI->isAllocatable(Reg)) continue; |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 988 | if (MO.isUse()) { |
Jakob Stoklund Olesen | 4d5c106 | 2010-05-14 18:03:25 +0000 | [diff] [blame] | 989 | usePhysReg(MO); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 990 | } else if (MO.isEarlyClobber()) { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 991 | definePhysReg(MI, Reg, |
Duncan P. N. Exon Smith | 44ed0de | 2016-07-01 15:03:37 +0000 | [diff] [blame] | 992 | (MO.isImplicit() || MO.isDead()) ? regFree : regReserved); |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 993 | hasEarlyClobbers = true; |
| 994 | } else |
| 995 | hasPhysDefs = true; |
| 996 | } |
| 997 | |
| 998 | // The instruction may have virtual register operands that must be allocated |
| 999 | // the same register at use-time and def-time: early clobbers and tied |
| 1000 | // operands. If there are also physical defs, these registers must avoid |
| 1001 | // both physical defs and uses, making them more constrained than normal |
| 1002 | // operands. |
Jim Grosbach | cb2e56f | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 1003 | // Similarly, if there are multiple defs and tied operands, we must make |
| 1004 | // sure the same register is allocated to uses and defs. |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 1005 | // We didn't detect inline asm tied operands above, so just make this extra |
| 1006 | // pass for all inline asm. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1007 | if (MI.isInlineAsm() || hasEarlyClobbers || hasPartialRedefs || |
Evan Cheng | 6cc775f | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1008 | (hasTiedOps && (hasPhysDefs || MCID.getNumDefs() > 1))) { |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 1009 | handleThroughOperands(MI, VirtDead); |
| 1010 | // Don't attempt coalescing when we have funny stuff going on. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1011 | CopyDstReg = 0; |
Jakob Stoklund Olesen | 36cf119 | 2010-07-29 00:52:19 +0000 | [diff] [blame] | 1012 | // Pretend we have early clobbers so the use operands get marked below. |
| 1013 | // This is not necessary for the common case of a single tied use. |
| 1014 | hasEarlyClobbers = true; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1017 | // Second scan. |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 1018 | // Allocate virtreg uses. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1019 | for (unsigned I = 0; I != VirtOpEnd; ++I) { |
| 1020 | const MachineOperand &MO = MI.getOperand(I); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1021 | if (!MO.isReg()) continue; |
| 1022 | unsigned Reg = MO.getReg(); |
Jakob Stoklund Olesen | 2fb5b31 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 1023 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) continue; |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1024 | if (MO.isUse()) { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1025 | LiveRegMap::iterator LRI = reloadVirtReg(MI, I, Reg, CopyDstReg); |
| 1026 | MCPhysReg PhysReg = LRI->PhysReg; |
| 1027 | CopySrcReg = (CopySrcReg == Reg || CopySrcReg == PhysReg) ? PhysReg : 0; |
| 1028 | if (setPhysReg(MI, I, PhysReg)) |
Jakob Stoklund Olesen | f915d14 | 2010-05-17 03:26:09 +0000 | [diff] [blame] | 1029 | killVirtReg(LRI); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
Jakob Stoklund Olesen | 36cf119 | 2010-07-29 00:52:19 +0000 | [diff] [blame] | 1033 | // Track registers defined by instruction - early clobbers and tied uses at |
| 1034 | // this point. |
Jakob Stoklund Olesen | a2136be | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 1035 | UsedInInstr.clear(); |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 1036 | if (hasEarlyClobbers) { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1037 | for (const MachineOperand &MO : MI.operands()) { |
Jakob Stoklund Olesen | 36cf119 | 2010-07-29 00:52:19 +0000 | [diff] [blame] | 1038 | if (!MO.isReg()) continue; |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 1039 | unsigned Reg = MO.getReg(); |
| 1040 | if (!Reg || !TargetRegisterInfo::isPhysicalRegister(Reg)) continue; |
Jakob Stoklund Olesen | 36cf119 | 2010-07-29 00:52:19 +0000 | [diff] [blame] | 1041 | // Look for physreg defs and tied uses. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1042 | if (!MO.isDef() && !MO.isTied()) continue; |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 1043 | markRegUsedInInstr(Reg); |
Jakob Stoklund Olesen | 0d94d7a | 2010-06-28 18:34:34 +0000 | [diff] [blame] | 1044 | } |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1047 | unsigned DefOpEnd = MI.getNumOperands(); |
| 1048 | if (MI.isCall()) { |
Quentin Colombet | e611698 | 2016-02-20 00:32:29 +0000 | [diff] [blame] | 1049 | // Spill all virtregs before a call. This serves one purpose: If an |
Jim Grosbach | cb2e56f | 2010-09-01 19:16:29 +0000 | [diff] [blame] | 1050 | // exception is thrown, the landing pad is going to expect to find |
Quentin Colombet | e611698 | 2016-02-20 00:32:29 +0000 | [diff] [blame] | 1051 | // registers in their spill slots. |
| 1052 | // Note: although this is appealing to just consider all definitions |
| 1053 | // as call-clobbered, this is not correct because some of those |
| 1054 | // definitions may be used later on and we do not want to reuse |
| 1055 | // those for virtual registers in between. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1056 | LLVM_DEBUG(dbgs() << " Spilling remaining registers before call.\n"); |
Jakob Stoklund Olesen | 1069a09 | 2010-05-17 02:49:18 +0000 | [diff] [blame] | 1057 | spillAll(MI); |
| 1058 | } |
| 1059 | |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1060 | // Third scan. |
| 1061 | // Allocate defs and collect dead defs. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1062 | for (unsigned I = 0; I != DefOpEnd; ++I) { |
| 1063 | const MachineOperand &MO = MI.getOperand(I); |
Jakob Stoklund Olesen | 246e9a0 | 2010-06-15 16:20:57 +0000 | [diff] [blame] | 1064 | if (!MO.isReg() || !MO.isDef() || !MO.getReg() || MO.isEarlyClobber()) |
| 1065 | continue; |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1066 | unsigned Reg = MO.getReg(); |
| 1067 | |
| 1068 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Jakob Stoklund Olesen | f67bf3e | 2012-10-15 22:41:03 +0000 | [diff] [blame] | 1069 | if (!MRI->isAllocatable(Reg)) continue; |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1070 | definePhysReg(MI, Reg, MO.isDead() ? regFree : regReserved); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1071 | continue; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1072 | } |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1073 | LiveRegMap::iterator LRI = defineVirtReg(MI, I, Reg, CopySrcReg); |
| 1074 | MCPhysReg PhysReg = LRI->PhysReg; |
| 1075 | if (setPhysReg(MI, I, PhysReg)) { |
Jakob Stoklund Olesen | 663543b4 | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 1076 | VirtDead.push_back(Reg); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1077 | CopyDstReg = 0; // cancel coalescing; |
Jakob Stoklund Olesen | ceb5a7a | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1078 | } else |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1079 | CopyDstReg = (CopyDstReg == Reg || CopyDstReg == PhysReg) ? PhysReg : 0; |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Jakob Stoklund Olesen | 663543b4 | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 1082 | // Kill dead defs after the scan to ensure that multiple defs of the same |
| 1083 | // register are allocated identically. We didn't need to do this for uses |
| 1084 | // because we are crerating our own kill flags, and they are always at the |
| 1085 | // last use. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1086 | for (unsigned VirtReg : VirtDead) |
| 1087 | killVirtReg(VirtReg); |
Jakob Stoklund Olesen | 663543b4 | 2010-05-18 21:10:50 +0000 | [diff] [blame] | 1088 | VirtDead.clear(); |
| 1089 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1090 | if (CopyDstReg && CopyDstReg == CopySrcReg && CopyDstSub == CopySrcSub) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1091 | LLVM_DEBUG(dbgs() << "-- coalescing: " << MI); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1092 | Coalesced.push_back(&MI); |
Jakob Stoklund Olesen | ceb5a7a | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1093 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1094 | LLVM_DEBUG(dbgs() << "<< " << MI); |
Jakob Stoklund Olesen | ceb5a7a | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1095 | } |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1098 | // Spill all physical registers holding virtual registers now. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1099 | LLVM_DEBUG(dbgs() << "Spilling live registers at end of block.\n"); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1100 | spillAll(MBB.getFirstTerminator()); |
Jakob Stoklund Olesen | f1b3029 | 2010-05-11 18:54:45 +0000 | [diff] [blame] | 1101 | |
Jakob Stoklund Olesen | ceb5a7a | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1102 | // Erase all the coalesced copies. We are delaying it until now because |
Jakob Stoklund Olesen | 8044c98 | 2010-05-17 02:07:32 +0000 | [diff] [blame] | 1103 | // LiveVirtRegs might refer to the instrs. |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1104 | for (MachineInstr *MI : Coalesced) |
| 1105 | MBB.erase(MI); |
Matthias Braun | 14af82a | 2018-11-07 02:04:07 +0000 | [diff] [blame] | 1106 | NumCoalesced += Coalesced.size(); |
Jakob Stoklund Olesen | ceb5a7a | 2010-05-14 04:30:51 +0000 | [diff] [blame] | 1107 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1108 | LLVM_DEBUG(MBB.dump()); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1111 | bool RegAllocFast::runOnMachineFunction(MachineFunction &MF) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1112 | LLVM_DEBUG(dbgs() << "********** FAST REGISTER ALLOCATION **********\n" |
| 1113 | << "********** Function: " << MF.getName() << '\n'); |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1114 | MRI = &MF.getRegInfo(); |
| 1115 | const TargetSubtargetInfo &STI = MF.getSubtarget(); |
| 1116 | TRI = STI.getRegisterInfo(); |
| 1117 | TII = STI.getInstrInfo(); |
| 1118 | MFI = &MF.getFrameInfo(); |
| 1119 | MRI->freezeReservedRegs(MF); |
| 1120 | RegClassInfo.runOnMachineFunction(MF); |
Jakob Stoklund Olesen | a2136be | 2012-10-17 01:37:59 +0000 | [diff] [blame] | 1121 | UsedInInstr.clear(); |
Jakob Stoklund Olesen | 2ff4dc0 | 2013-02-21 19:35:21 +0000 | [diff] [blame] | 1122 | UsedInInstr.setUniverse(TRI->getNumRegUnits()); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1123 | |
| 1124 | // initialize the virtual->physical register map to have a 'null' |
| 1125 | // mapping for all virtual registers |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1126 | unsigned NumVirtRegs = MRI->getNumVirtRegs(); |
| 1127 | StackSlotForVirtReg.resize(NumVirtRegs); |
| 1128 | LiveVirtRegs.setUniverse(NumVirtRegs); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1129 | |
| 1130 | // Loop over all of the basic blocks, eliminating virtual register references |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1131 | for (MachineBasicBlock &MBB : MF) |
| 1132 | allocateBasicBlock(MBB); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1133 | |
Andrew Trick | da84e64 | 2012-02-21 04:51:23 +0000 | [diff] [blame] | 1134 | // All machine operands and other references to virtual registers have been |
| 1135 | // replaced. Remove the virtual registers. |
| 1136 | MRI->clearVirtRegs(); |
| 1137 | |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1138 | StackSlotForVirtReg.clear(); |
Devang Patel | d71bc1a | 2010-08-04 18:42:02 +0000 | [diff] [blame] | 1139 | LiveDbgValueMap.clear(); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1140 | return true; |
| 1141 | } |
| 1142 | |
| 1143 | FunctionPass *llvm::createFastRegisterAllocator() { |
Matthias Braun | 864cf58 | 2017-09-09 00:52:46 +0000 | [diff] [blame] | 1144 | return new RegAllocFast(); |
Jakob Stoklund Olesen | 8a070a5 | 2010-04-21 18:02:42 +0000 | [diff] [blame] | 1145 | } |