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