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