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