Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 1 | //===- AggressiveAntiDepBreaker.cpp - Anti-dep breaker --------------------===// |
David Goodwin | de11f36 | 2009-10-26 19:32: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 |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the AggressiveAntiDepBreaker class, which |
| 10 | // implements register anti-dependence breaking during post-RA |
| 11 | // scheduling. It attempts to break all anti-dependencies within a |
| 12 | // block. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 16 | #include "AggressiveAntiDepBreaker.h" |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/BitVector.h" |
| 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/ADT/iterator_range.h" |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineInstr.h" |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineOperand.h" |
| 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Andrew Trick | 05ff466 | 2012-06-06 20:29:31 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/ScheduleDAG.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 31 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCInstrDesc.h" |
| 33 | #include "llvm/MC/MCRegisterInfo.h" |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
David Blaikie | 13e77db | 2018-03-23 23:58:25 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MachineValueType.h" |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 38 | #include <cassert> |
| 39 | #include <map> |
| 40 | #include <set> |
| 41 | #include <utility> |
| 42 | #include <vector> |
| 43 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 46 | #define DEBUG_TYPE "post-RA-sched" |
| 47 | |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 48 | // If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod |
| 49 | static cl::opt<int> |
| 50 | DebugDiv("agg-antidep-debugdiv", |
Bob Wilson | 67dd3a4 | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 51 | cl::desc("Debug control for aggressive anti-dep breaker"), |
| 52 | cl::init(0), cl::Hidden); |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 53 | |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 54 | static cl::opt<int> |
| 55 | DebugMod("agg-antidep-debugmod", |
Bob Wilson | 67dd3a4 | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 56 | cl::desc("Debug control for aggressive anti-dep breaker"), |
| 57 | cl::init(0), cl::Hidden); |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 58 | |
David Goodwin | a45fe67 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 59 | AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs, |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 60 | MachineBasicBlock *BB) |
| 61 | : NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0), |
| 62 | GroupNodeIndices(TargetRegs, 0), KillIndices(TargetRegs, 0), |
| 63 | DefIndices(TargetRegs, 0) { |
David Goodwin | a45fe67 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 64 | const unsigned BBSize = BB->size(); |
| 65 | for (unsigned i = 0; i < NumTargetRegs; ++i) { |
| 66 | // Initialize all registers to be in their own group. Initially we |
| 67 | // assign the register to the same-indexed GroupNode. |
| 68 | GroupNodeIndices[i] = i; |
| 69 | // Initialize the indices to indicate that no registers are live. |
| 70 | KillIndices[i] = ~0u; |
| 71 | DefIndices[i] = BBSize; |
| 72 | } |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Bill Wendling | 5a8d15c | 2010-07-15 19:41:20 +0000 | [diff] [blame] | 75 | unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 76 | unsigned Node = GroupNodeIndices[Reg]; |
| 77 | while (GroupNodes[Node] != Node) |
| 78 | Node = GroupNodes[Node]; |
| 79 | |
| 80 | return Node; |
| 81 | } |
| 82 | |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 83 | void AggressiveAntiDepState::GetGroupRegs( |
| 84 | unsigned Group, |
| 85 | std::vector<unsigned> &Regs, |
| 86 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 87 | { |
David Goodwin | a45fe67 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 88 | for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) { |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 89 | if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0)) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 90 | Regs.push_back(Reg); |
| 91 | } |
| 92 | } |
| 93 | |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 94 | unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 95 | assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!"); |
| 96 | assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 97 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 98 | // find group for each register |
| 99 | unsigned Group1 = GetGroup(Reg1); |
| 100 | unsigned Group2 = GetGroup(Reg2); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 101 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 102 | // if either group is 0, then that must become the parent |
| 103 | unsigned Parent = (Group1 == 0) ? Group1 : Group2; |
| 104 | unsigned Other = (Parent == Group1) ? Group2 : Group1; |
| 105 | GroupNodes.at(Other) = Parent; |
| 106 | return Parent; |
| 107 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 108 | |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 109 | unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 110 | // Create a new GroupNode for Reg. Reg's existing GroupNode must |
| 111 | // stay as is because there could be other GroupNodes referring to |
| 112 | // it. |
| 113 | unsigned idx = GroupNodes.size(); |
| 114 | GroupNodes.push_back(idx); |
| 115 | GroupNodeIndices[Reg] = idx; |
| 116 | return idx; |
| 117 | } |
| 118 | |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 119 | bool AggressiveAntiDepState::IsLive(unsigned Reg) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 120 | // KillIndex must be defined and DefIndex not defined for a register |
| 121 | // to be live. |
| 122 | return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u)); |
| 123 | } |
| 124 | |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 125 | AggressiveAntiDepBreaker::AggressiveAntiDepBreaker( |
| 126 | MachineFunction &MFi, const RegisterClassInfo &RCI, |
| 127 | TargetSubtargetInfo::RegClassVector &CriticalPathRCs) |
| 128 | : AntiDepBreaker(), MF(MFi), MRI(MF.getRegInfo()), |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 129 | TII(MF.getSubtarget().getInstrInfo()), |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 130 | TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI) { |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 131 | /* Collect a bitset of all registers that are only broken if they |
| 132 | are on the critical path. */ |
| 133 | for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) { |
| 134 | BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]); |
| 135 | if (CriticalPathSet.none()) |
| 136 | CriticalPathSet = CPSet; |
| 137 | else |
| 138 | CriticalPathSet |= CPSet; |
| 139 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 140 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 141 | LLVM_DEBUG(dbgs() << "AntiDep Critical-Path Registers:"); |
| 142 | LLVM_DEBUG(for (unsigned r |
| 143 | : CriticalPathSet.set_bits()) dbgs() |
| 144 | << " " << printReg(r, TRI)); |
| 145 | LLVM_DEBUG(dbgs() << '\n'); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() { |
| 149 | delete State; |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 153 | assert(!State); |
David Goodwin | a45fe67 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 154 | State = new AggressiveAntiDepState(TRI->getNumRegs(), BB); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 155 | |
Matthias Braun | c2d4bef | 2015-09-25 21:25:19 +0000 | [diff] [blame] | 156 | bool IsReturnBlock = BB->isReturnBlock(); |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 157 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 158 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 159 | |
Jakob Stoklund Olesen | c338679 | 2013-02-05 18:21:52 +0000 | [diff] [blame] | 160 | // Examine the live-in regs of all successors. |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 161 | for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(), |
| 162 | SE = BB->succ_end(); SI != SE; ++SI) |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 163 | for (const auto &LI : (*SI)->liveins()) { |
| 164 | for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) { |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 165 | unsigned Reg = *AI; |
Jakob Stoklund Olesen | be1c8d3 | 2010-12-14 23:23:15 +0000 | [diff] [blame] | 166 | State->UnionGroups(Reg, 0); |
| 167 | KillIndices[Reg] = BB->size(); |
| 168 | DefIndices[Reg] = ~0u; |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 172 | // Mark live-out callee-saved registers. In a return block this is |
| 173 | // all callee-saved registers. In non-return this is any |
| 174 | // callee-saved register that is not saved in the prolog. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 175 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 176 | BitVector Pristine = MFI.getPristineRegs(MF); |
Oren Ben Simhon | fe34c5e | 2017-03-14 09:09:26 +0000 | [diff] [blame] | 177 | for (const MCPhysReg *I = MF.getRegInfo().getCalleeSavedRegs(); *I; |
| 178 | ++I) { |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 179 | unsigned Reg = *I; |
Tim Shen | 0bd0aa8 | 2017-05-30 22:26:52 +0000 | [diff] [blame] | 180 | if (!IsReturnBlock && !Pristine.test(Reg)) |
Eric Christopher | b9c56d1 | 2017-03-30 22:34:20 +0000 | [diff] [blame] | 181 | continue; |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 182 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { |
| 183 | unsigned AliasReg = *AI; |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 184 | State->UnionGroups(AliasReg, 0); |
| 185 | KillIndices[AliasReg] = BB->size(); |
| 186 | DefIndices[AliasReg] = ~0u; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void AggressiveAntiDepBreaker::FinishBlock() { |
| 192 | delete State; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 193 | State = nullptr; |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 196 | void AggressiveAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count, |
Bob Wilson | 67dd3a4 | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 197 | unsigned InsertPosIndex) { |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 198 | assert(Count < InsertPosIndex && "Instruction index out of expected range!"); |
| 199 | |
David Goodwin | faa7660 | 2009-10-29 23:30:59 +0000 | [diff] [blame] | 200 | std::set<unsigned> PassthruRegs; |
| 201 | GetPassthruRegs(MI, PassthruRegs); |
| 202 | PrescanInstruction(MI, Count, PassthruRegs); |
| 203 | ScanInstruction(MI, Count); |
| 204 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 205 | LLVM_DEBUG(dbgs() << "Observe: "); |
| 206 | LLVM_DEBUG(MI.dump()); |
| 207 | LLVM_DEBUG(dbgs() << "\tRegs:"); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 208 | |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 209 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
David Goodwin | a45fe67 | 2009-12-09 17:18:22 +0000 | [diff] [blame] | 210 | for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) { |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 211 | // If Reg is current live, then mark that it can't be renamed as |
| 212 | // we don't know the extent of its live-range anymore (now that it |
| 213 | // has been scheduled). If it is not live but was defined in the |
| 214 | // previous schedule region, then set its def index to the most |
| 215 | // conservative location (i.e. the beginning of the previous |
| 216 | // schedule region). |
| 217 | if (State->IsLive(Reg)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 218 | LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() |
| 219 | << " " << printReg(Reg, TRI) << "=g" << State->GetGroup(Reg) |
| 220 | << "->g0(region live-out)"); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 221 | State->UnionGroups(Reg, 0); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 222 | } else if ((DefIndices[Reg] < InsertPosIndex) |
| 223 | && (DefIndices[Reg] >= Count)) { |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 224 | DefIndices[Reg] = Count; |
| 225 | } |
| 226 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 227 | LLVM_DEBUG(dbgs() << '\n'); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 230 | bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr &MI, |
| 231 | MachineOperand &MO) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 232 | if (!MO.isReg() || !MO.isImplicit()) |
| 233 | return false; |
| 234 | |
| 235 | unsigned Reg = MO.getReg(); |
| 236 | if (Reg == 0) |
| 237 | return false; |
| 238 | |
Chad Rosier | 47eba05 | 2015-10-09 19:48:48 +0000 | [diff] [blame] | 239 | MachineOperand *Op = nullptr; |
| 240 | if (MO.isDef()) |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 241 | Op = MI.findRegisterUseOperand(Reg, true); |
Chad Rosier | 47eba05 | 2015-10-09 19:48:48 +0000 | [diff] [blame] | 242 | else |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 243 | Op = MI.findRegisterDefOperand(Reg); |
Chad Rosier | 47eba05 | 2015-10-09 19:48:48 +0000 | [diff] [blame] | 244 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 245 | return(Op && Op->isImplicit()); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 248 | void AggressiveAntiDepBreaker::GetPassthruRegs( |
| 249 | MachineInstr &MI, std::set<unsigned> &PassthruRegs) { |
| 250 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 251 | MachineOperand &MO = MI.getOperand(i); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 252 | if (!MO.isReg()) continue; |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 253 | if ((MO.isDef() && MI.isRegTiedToUseOperand(i)) || |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 254 | IsImplicitDefUse(MI, MO)) { |
| 255 | const unsigned Reg = MO.getReg(); |
Chad Rosier | abdb1d6 | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 256 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 257 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 258 | PassthruRegs.insert(*SubRegs); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 263 | /// AntiDepEdges - Return in Edges the anti- and output- dependencies |
| 264 | /// in SU that we want to consider for breaking. |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 265 | static void AntiDepEdges(const SUnit *SU, std::vector<const SDep *> &Edges) { |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 266 | SmallSet<unsigned, 4> RegSet; |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 267 | for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 268 | P != PE; ++P) { |
David Goodwin | da83f7d | 2009-11-12 19:08:21 +0000 | [diff] [blame] | 269 | if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 270 | if (RegSet.insert(P->getReg()).second) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 271 | Edges.push_back(&*P); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 276 | /// CriticalPathStep - Return the next SUnit after SU on the bottom-up |
| 277 | /// critical path. |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 278 | static const SUnit *CriticalPathStep(const SUnit *SU) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 279 | const SDep *Next = nullptr; |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 280 | unsigned NextDepth = 0; |
| 281 | // Find the predecessor edge with the greatest depth. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 282 | if (SU) { |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 283 | for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end(); |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 284 | P != PE; ++P) { |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 285 | const SUnit *PredSU = P->getSUnit(); |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 286 | unsigned PredLatency = P->getLatency(); |
| 287 | unsigned PredTotalLatency = PredSU->getDepth() + PredLatency; |
| 288 | // In the case of a latency tie, prefer an anti-dependency edge over |
| 289 | // other types of edges. |
| 290 | if (NextDepth < PredTotalLatency || |
| 291 | (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) { |
| 292 | NextDepth = PredTotalLatency; |
| 293 | Next = &*P; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 298 | return (Next) ? Next->getSUnit() : nullptr; |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 299 | } |
| 300 | |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 301 | void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx, |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 302 | const char *tag, |
| 303 | const char *header, |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 304 | const char *footer) { |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 305 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 306 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 307 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 308 | RegRefs = State->GetRegRefs(); |
| 309 | |
Hal Finkel | 34c94d5 | 2015-01-28 14:44:14 +0000 | [diff] [blame] | 310 | // FIXME: We must leave subregisters of live super registers as live, so that |
| 311 | // we don't clear out the register tracking information for subregisters of |
| 312 | // super registers we're still tracking (and with which we're unioning |
| 313 | // subregister definitions). |
| 314 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) |
| 315 | if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 316 | LLVM_DEBUG(if (!header && footer) dbgs() << footer); |
Hal Finkel | 34c94d5 | 2015-01-28 14:44:14 +0000 | [diff] [blame] | 317 | return; |
| 318 | } |
| 319 | |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 320 | if (!State->IsLive(Reg)) { |
| 321 | KillIndices[Reg] = KillIdx; |
| 322 | DefIndices[Reg] = ~0u; |
| 323 | RegRefs.erase(Reg); |
| 324 | State->LeaveGroup(Reg); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 325 | LLVM_DEBUG(if (header) { |
| 326 | dbgs() << header << printReg(Reg, TRI); |
| 327 | header = nullptr; |
| 328 | }); |
| 329 | LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag); |
Chuang-Yu Cheng | 35c6181 | 2016-04-01 02:05:29 +0000 | [diff] [blame] | 330 | // Repeat for subregisters. Note that we only do this if the superregister |
| 331 | // was not live because otherwise, regardless whether we have an explicit |
| 332 | // use of the subregister, the subregister's contents are needed for the |
| 333 | // uses of the superregister. |
| 334 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
| 335 | unsigned SubregReg = *SubRegs; |
| 336 | if (!State->IsLive(SubregReg)) { |
| 337 | KillIndices[SubregReg] = KillIdx; |
| 338 | DefIndices[SubregReg] = ~0u; |
| 339 | RegRefs.erase(SubregReg); |
| 340 | State->LeaveGroup(SubregReg); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 341 | LLVM_DEBUG(if (header) { |
| 342 | dbgs() << header << printReg(Reg, TRI); |
| 343 | header = nullptr; |
| 344 | }); |
| 345 | LLVM_DEBUG(dbgs() << " " << printReg(SubregReg, TRI) << "->g" |
| 346 | << State->GetGroup(SubregReg) << tag); |
Chuang-Yu Cheng | 35c6181 | 2016-04-01 02:05:29 +0000 | [diff] [blame] | 347 | } |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 350 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 351 | LLVM_DEBUG(if (!header && footer) dbgs() << footer); |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 354 | void AggressiveAntiDepBreaker::PrescanInstruction( |
| 355 | MachineInstr &MI, unsigned Count, std::set<unsigned> &PassthruRegs) { |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 356 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 357 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 358 | RegRefs = State->GetRegRefs(); |
| 359 | |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 360 | // Handle dead defs by simulating a last-use of the register just |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 361 | // after the def. A dead def can occur because the def is truly |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 362 | // dead, or because only a subregister is live at the def. If we |
| 363 | // don't do this the dead def will be incorrectly merged into the |
| 364 | // previous def. |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 365 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 366 | MachineOperand &MO = MI.getOperand(i); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 367 | if (!MO.isReg() || !MO.isDef()) continue; |
| 368 | unsigned Reg = MO.getReg(); |
| 369 | if (Reg == 0) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 370 | |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 371 | HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 374 | LLVM_DEBUG(dbgs() << "\tDef Groups:"); |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 375 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 376 | MachineOperand &MO = MI.getOperand(i); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 377 | if (!MO.isReg() || !MO.isDef()) continue; |
| 378 | unsigned Reg = MO.getReg(); |
| 379 | if (Reg == 0) continue; |
| 380 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 381 | LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g" |
| 382 | << State->GetGroup(Reg)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 383 | |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 384 | // If MI's defs have a special allocation requirement, don't allow |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 385 | // any def registers to be changed. Also assume all registers |
Kyle Butt | cf6a8bf | 2015-12-02 18:58:51 +0000 | [diff] [blame] | 386 | // defined in a call must not be changed (ABI). Inline assembly may |
| 387 | // reference either system calls or the register directly. Skip it until we |
| 388 | // can tell user specified registers from compiler-specified. |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 389 | if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI) || |
| 390 | MI.isInlineAsm()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 391 | LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)"); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 392 | State->UnionGroups(Reg, 0); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // Any aliased that are live at this point are completely or |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 396 | // partially defined here, so group those aliases with Reg. |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 397 | for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) { |
| 398 | unsigned AliasReg = *AI; |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 399 | if (State->IsLive(AliasReg)) { |
| 400 | State->UnionGroups(Reg, AliasReg); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 401 | LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " |
| 402 | << printReg(AliasReg, TRI) << ")"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 405 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 406 | // Note register reference... |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 407 | const TargetRegisterClass *RC = nullptr; |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 408 | if (i < MI.getDesc().getNumOperands()) |
| 409 | RC = TII->getRegClass(MI.getDesc(), i, TRI, MF); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 410 | AggressiveAntiDepState::RegisterReference RR = { &MO, RC }; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 411 | RegRefs.insert(std::make_pair(Reg, RR)); |
| 412 | } |
| 413 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 414 | LLVM_DEBUG(dbgs() << '\n'); |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 415 | |
| 416 | // Scan the register defs for this instruction and update |
| 417 | // live-ranges. |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 418 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 419 | MachineOperand &MO = MI.getOperand(i); |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 420 | if (!MO.isReg() || !MO.isDef()) continue; |
| 421 | unsigned Reg = MO.getReg(); |
| 422 | if (Reg == 0) continue; |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 423 | // Ignore KILLs and passthru registers for liveness... |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 424 | if (MI.isKill() || (PassthruRegs.count(Reg) != 0)) |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 425 | continue; |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 426 | |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 427 | // Update def for Reg and aliases. |
Hal Finkel | 121caf6 | 2014-02-26 20:20:30 +0000 | [diff] [blame] | 428 | for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) { |
| 429 | // We need to be careful here not to define already-live super registers. |
| 430 | // If the super register is already live, then this definition is not |
| 431 | // a definition of the whole super register (just a partial insertion |
| 432 | // into it). Earlier subregister definitions (which we've not yet visited |
| 433 | // because we're iterating bottom-up) need to be linked to the same group |
| 434 | // as this definition. |
| 435 | if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) |
| 436 | continue; |
| 437 | |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 438 | DefIndices[*AI] = Count; |
Hal Finkel | 121caf6 | 2014-02-26 20:20:30 +0000 | [diff] [blame] | 439 | } |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 440 | } |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 443 | void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr &MI, |
Bob Wilson | 67dd3a4 | 2010-04-09 21:38:26 +0000 | [diff] [blame] | 444 | unsigned Count) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 445 | LLVM_DEBUG(dbgs() << "\tUse Groups:"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 446 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 447 | RegRefs = State->GetRegRefs(); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 448 | |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 449 | // If MI's uses have special allocation requirement, don't allow |
| 450 | // any use registers to be changed. Also assume all registers |
| 451 | // used in a call must not be changed (ABI). |
Kyle Butt | cf6a8bf | 2015-12-02 18:58:51 +0000 | [diff] [blame] | 452 | // Inline Assembly register uses also cannot be safely changed. |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 453 | // FIXME: The issue with predicated instruction is more complex. We are being |
| 454 | // conservatively here because the kill markers cannot be trusted after |
| 455 | // if-conversion: |
Francis Visoiu Mistrih | 7d9bef8 | 2018-01-09 17:31:07 +0000 | [diff] [blame] | 456 | // %r6 = LDR %sp, %reg0, 92, 14, %reg0; mem:LD4[FixedStack14] |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 457 | // ... |
Francis Visoiu Mistrih | 7d9bef8 | 2018-01-09 17:31:07 +0000 | [diff] [blame] | 458 | // STR %r0, killed %r6, %reg0, 0, 0, %cpsr; mem:ST4[%395] |
| 459 | // %r6 = LDR %sp, %reg0, 100, 0, %cpsr; mem:LD4[FixedStack12] |
| 460 | // STR %r0, killed %r6, %reg0, 0, 14, %reg0; mem:ST4[%396](align=8) |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 461 | // |
| 462 | // The first R6 kill is not really a kill since it's killed by a predicated |
| 463 | // instruction which may not be executed. The second R6 def may or may not |
| 464 | // re-define R6 so it's not safe to change it since the last R6 use cannot be |
| 465 | // changed. |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 466 | bool Special = MI.isCall() || MI.hasExtraSrcRegAllocReq() || |
| 467 | TII->isPredicated(MI) || MI.isInlineAsm(); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 468 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 469 | // Scan the register uses for this instruction and update |
| 470 | // live-ranges, groups and RegRefs. |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 471 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 472 | MachineOperand &MO = MI.getOperand(i); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 473 | if (!MO.isReg() || !MO.isUse()) continue; |
| 474 | unsigned Reg = MO.getReg(); |
| 475 | if (Reg == 0) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 476 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 477 | LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI) << "=g" |
| 478 | << State->GetGroup(Reg)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 479 | |
| 480 | // It wasn't previously live but now it is, this is a kill. Forget |
| 481 | // the previous live-range information and start a new live-range |
| 482 | // for the register. |
David Goodwin | 9f1b2d4 | 2009-10-29 19:17:04 +0000 | [diff] [blame] | 483 | HandleLastUse(Reg, Count, "(last-use)"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 484 | |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 485 | if (Special) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 486 | LLVM_DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)"); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 487 | State->UnionGroups(Reg, 0); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | // Note register reference... |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 491 | const TargetRegisterClass *RC = nullptr; |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 492 | if (i < MI.getDesc().getNumOperands()) |
| 493 | RC = TII->getRegClass(MI.getDesc(), i, TRI, MF); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 494 | AggressiveAntiDepState::RegisterReference RR = { &MO, RC }; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 495 | RegRefs.insert(std::make_pair(Reg, RR)); |
| 496 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 497 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 498 | LLVM_DEBUG(dbgs() << '\n'); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 499 | |
| 500 | // Form a group of all defs and uses of a KILL instruction to ensure |
| 501 | // that all registers are renamed as a group. |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 502 | if (MI.isKill()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 503 | LLVM_DEBUG(dbgs() << "\tKill Group:"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 504 | |
| 505 | unsigned FirstReg = 0; |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 506 | for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { |
| 507 | MachineOperand &MO = MI.getOperand(i); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 508 | if (!MO.isReg()) continue; |
| 509 | unsigned Reg = MO.getReg(); |
| 510 | if (Reg == 0) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 511 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 512 | if (FirstReg != 0) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 513 | LLVM_DEBUG(dbgs() << "=" << printReg(Reg, TRI)); |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 514 | State->UnionGroups(FirstReg, Reg); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 515 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 516 | LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 517 | FirstReg = Reg; |
| 518 | } |
| 519 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 520 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 521 | LLVM_DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n'); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
| 525 | BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) { |
| 526 | BitVector BV(TRI->getNumRegs(), false); |
| 527 | bool first = true; |
| 528 | |
| 529 | // Check all references that need rewriting for Reg. For each, use |
| 530 | // the corresponding register class to narrow the set of registers |
| 531 | // that are appropriate for renaming. |
Benjamin Kramer | c9436ad | 2015-07-18 20:05:10 +0000 | [diff] [blame] | 532 | for (const auto &Q : make_range(State->GetRegRefs().equal_range(Reg))) { |
| 533 | const TargetRegisterClass *RC = Q.second.RC; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 534 | if (!RC) continue; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 535 | |
| 536 | BitVector RCBV = TRI->getAllocatableSet(MF, RC); |
| 537 | if (first) { |
| 538 | BV |= RCBV; |
| 539 | first = false; |
| 540 | } else { |
| 541 | BV &= RCBV; |
| 542 | } |
| 543 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 544 | LLVM_DEBUG(dbgs() << " " << TRI->getRegClassName(RC)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 545 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 546 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 547 | return BV; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 548 | } |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 549 | |
| 550 | bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters( |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 551 | unsigned AntiDepGroupIndex, |
| 552 | RenameOrderType& RenameOrder, |
| 553 | std::map<unsigned, unsigned> &RenameMap) { |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 554 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 555 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 556 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 557 | RegRefs = State->GetRegRefs(); |
| 558 | |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 559 | // Collect all referenced registers in the same group as |
| 560 | // AntiDepReg. These all need to be renamed together if we are to |
| 561 | // break the anti-dependence. |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 562 | std::vector<unsigned> Regs; |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 563 | State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs); |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 564 | assert(!Regs.empty() && "Empty register group!"); |
| 565 | if (Regs.empty()) |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 566 | return false; |
| 567 | |
| 568 | // Find the "superest" register in the group. At the same time, |
| 569 | // collect the BitVector of registers that can be used to rename |
| 570 | // each register. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 571 | LLVM_DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex |
| 572 | << ":\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 573 | std::map<unsigned, BitVector> RenameRegisterMap; |
| 574 | unsigned SuperReg = 0; |
| 575 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) { |
| 576 | unsigned Reg = Regs[i]; |
| 577 | if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg)) |
| 578 | SuperReg = Reg; |
| 579 | |
| 580 | // If Reg has any references, then collect possible rename regs |
| 581 | if (RegRefs.count(Reg) > 0) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 582 | LLVM_DEBUG(dbgs() << "\t\t" << printReg(Reg, TRI) << ":"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 583 | |
Benjamin Kramer | 7f75e94 | 2016-02-13 16:39:39 +0000 | [diff] [blame] | 584 | BitVector &BV = RenameRegisterMap[Reg]; |
| 585 | assert(BV.empty()); |
| 586 | BV = GetRenameRegisters(Reg); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 587 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 588 | LLVM_DEBUG({ |
Benjamin Kramer | 7f75e94 | 2016-02-13 16:39:39 +0000 | [diff] [blame] | 589 | dbgs() << " ::"; |
Francis Visoiu Mistrih | b52e036 | 2017-05-17 01:07:53 +0000 | [diff] [blame] | 590 | for (unsigned r : BV.set_bits()) |
Francis Visoiu Mistrih | c71cced | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 591 | dbgs() << " " << printReg(r, TRI); |
Benjamin Kramer | 7f75e94 | 2016-02-13 16:39:39 +0000 | [diff] [blame] | 592 | dbgs() << "\n"; |
| 593 | }); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
| 597 | // All group registers should be a subreg of SuperReg. |
| 598 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) { |
| 599 | unsigned Reg = Regs[i]; |
| 600 | if (Reg == SuperReg) continue; |
| 601 | bool IsSub = TRI->isSubRegister(SuperReg, Reg); |
Will Schmidt | 44ff8f0 | 2014-07-31 19:50:53 +0000 | [diff] [blame] | 602 | // FIXME: remove this once PR18663 has been properly fixed. For now, |
| 603 | // return a conservative answer: |
| 604 | // assert(IsSub && "Expecting group subregister"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 605 | if (!IsSub) |
| 606 | return false; |
| 607 | } |
| 608 | |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 609 | #ifndef NDEBUG |
| 610 | // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod |
| 611 | if (DebugDiv > 0) { |
| 612 | static int renamecnt = 0; |
| 613 | if (renamecnt++ % DebugDiv != DebugMod) |
| 614 | return false; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 615 | |
Francis Visoiu Mistrih | c71cced | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 616 | dbgs() << "*** Performing rename " << printReg(SuperReg, TRI) |
| 617 | << " for debug ***\n"; |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 618 | } |
| 619 | #endif |
| 620 | |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 621 | // Check each possible rename register for SuperReg in round-robin |
| 622 | // order. If that register is available, and the corresponding |
| 623 | // registers are available for the other group subregisters, then we |
| 624 | // can use those registers to rename. |
Rafael Espindola | 871c724 | 2010-07-12 02:55:34 +0000 | [diff] [blame] | 625 | |
| 626 | // FIXME: Using getMinimalPhysRegClass is very conservative. We should |
| 627 | // check every use of the register and find the largest register class |
| 628 | // that can be used in all of them. |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 629 | const TargetRegisterClass *SuperRC = |
Rafael Espindola | 871c724 | 2010-07-12 02:55:34 +0000 | [diff] [blame] | 630 | TRI->getMinimalPhysRegClass(SuperReg, MVT::Other); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 631 | |
Jakob Stoklund Olesen | bdb55e0 | 2012-11-29 03:34:17 +0000 | [diff] [blame] | 632 | ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC); |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 633 | if (Order.empty()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 634 | LLVM_DEBUG(dbgs() << "\tEmpty Super Regclass!!\n"); |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 635 | return false; |
| 636 | } |
| 637 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 638 | LLVM_DEBUG(dbgs() << "\tFind Registers:"); |
David Goodwin | dd1c619 | 2009-11-19 23:12:37 +0000 | [diff] [blame] | 639 | |
Benjamin Kramer | 2c99e41 | 2014-10-10 15:32:50 +0000 | [diff] [blame] | 640 | RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size())); |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 641 | |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 642 | unsigned OrigR = RenameOrder[SuperRC]; |
| 643 | unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR); |
| 644 | unsigned R = OrigR; |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 645 | do { |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 646 | if (R == 0) R = Order.size(); |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 647 | --R; |
Jakob Stoklund Olesen | 4f5f84c | 2011-06-16 21:56:21 +0000 | [diff] [blame] | 648 | const unsigned NewSuperReg = Order[R]; |
Jim Grosbach | 944aece | 2010-09-02 17:12:55 +0000 | [diff] [blame] | 649 | // Don't consider non-allocatable registers |
Jakob Stoklund Olesen | f67bf3e | 2012-10-15 22:41:03 +0000 | [diff] [blame] | 650 | if (!MRI.isAllocatable(NewSuperReg)) continue; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 651 | // Don't replace a register with itself. |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 652 | if (NewSuperReg == SuperReg) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 653 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 654 | LLVM_DEBUG(dbgs() << " [" << printReg(NewSuperReg, TRI) << ':'); |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 655 | RenameMap.clear(); |
| 656 | |
| 657 | // For each referenced group register (which must be a SuperReg or |
| 658 | // a subregister of SuperReg), find the corresponding subregister |
| 659 | // of NewSuperReg and make sure it is free to be renamed. |
| 660 | for (unsigned i = 0, e = Regs.size(); i != e; ++i) { |
| 661 | unsigned Reg = Regs[i]; |
| 662 | unsigned NewReg = 0; |
| 663 | if (Reg == SuperReg) { |
| 664 | NewReg = NewSuperReg; |
| 665 | } else { |
| 666 | unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg); |
| 667 | if (NewSubRegIdx != 0) |
| 668 | NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 669 | } |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 670 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 671 | LLVM_DEBUG(dbgs() << " " << printReg(NewReg, TRI)); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 672 | |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 673 | // Check if Reg can be renamed to NewReg. |
Benjamin Kramer | 7f75e94 | 2016-02-13 16:39:39 +0000 | [diff] [blame] | 674 | if (!RenameRegisterMap[Reg].test(NewReg)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 675 | LLVM_DEBUG(dbgs() << "(no rename)"); |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 676 | goto next_super_reg; |
| 677 | } |
| 678 | |
| 679 | // If NewReg is dead and NewReg's most recent def is not before |
| 680 | // Regs's kill, it's safe to replace Reg with NewReg. We |
| 681 | // must also check all aliases of NewReg, because we can't define a |
| 682 | // register when any sub or super is already live. |
| 683 | if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 684 | LLVM_DEBUG(dbgs() << "(live)"); |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 685 | goto next_super_reg; |
| 686 | } else { |
| 687 | bool found = false; |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 688 | for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) { |
| 689 | unsigned AliasReg = *AI; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 690 | if (State->IsLive(AliasReg) || |
| 691 | (KillIndices[Reg] > DefIndices[AliasReg])) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 692 | LLVM_DEBUG(dbgs() |
| 693 | << "(alias " << printReg(AliasReg, TRI) << " live)"); |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 694 | found = true; |
| 695 | break; |
| 696 | } |
| 697 | } |
| 698 | if (found) |
| 699 | goto next_super_reg; |
| 700 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 701 | |
Hal Finkel | c8cf2b8 | 2014-12-09 01:00:59 +0000 | [diff] [blame] | 702 | // We cannot rename 'Reg' to 'NewReg' if one of the uses of 'Reg' also |
| 703 | // defines 'NewReg' via an early-clobber operand. |
Benjamin Kramer | c9436ad | 2015-07-18 20:05:10 +0000 | [diff] [blame] | 704 | for (const auto &Q : make_range(RegRefs.equal_range(Reg))) { |
| 705 | MachineInstr *UseMI = Q.second.Operand->getParent(); |
Hal Finkel | c8cf2b8 | 2014-12-09 01:00:59 +0000 | [diff] [blame] | 706 | int Idx = UseMI->findRegisterDefOperandIdx(NewReg, false, true, TRI); |
| 707 | if (Idx == -1) |
| 708 | continue; |
| 709 | |
| 710 | if (UseMI->getOperand(Idx).isEarlyClobber()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 711 | LLVM_DEBUG(dbgs() << "(ec)"); |
Hal Finkel | c8cf2b8 | 2014-12-09 01:00:59 +0000 | [diff] [blame] | 712 | goto next_super_reg; |
| 713 | } |
| 714 | } |
| 715 | |
Hal Finkel | e0a28e5 | 2015-08-31 07:51:36 +0000 | [diff] [blame] | 716 | // Also, we cannot rename 'Reg' to 'NewReg' if the instruction defining |
| 717 | // 'Reg' is an early-clobber define and that instruction also uses |
| 718 | // 'NewReg'. |
| 719 | for (const auto &Q : make_range(RegRefs.equal_range(Reg))) { |
| 720 | if (!Q.second.Operand->isDef() || !Q.second.Operand->isEarlyClobber()) |
| 721 | continue; |
| 722 | |
| 723 | MachineInstr *DefMI = Q.second.Operand->getParent(); |
| 724 | if (DefMI->readsRegister(NewReg, TRI)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 725 | LLVM_DEBUG(dbgs() << "(ec)"); |
Hal Finkel | e0a28e5 | 2015-08-31 07:51:36 +0000 | [diff] [blame] | 726 | goto next_super_reg; |
| 727 | } |
| 728 | } |
| 729 | |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 730 | // Record that 'Reg' can be renamed to 'NewReg'. |
| 731 | RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 732 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 733 | |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 734 | // If we fall-out here, then every register in the group can be |
| 735 | // renamed, as recorded in RenameMap. |
| 736 | RenameOrder.erase(SuperRC); |
| 737 | RenameOrder.insert(RenameOrderType::value_type(SuperRC, R)); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 738 | LLVM_DEBUG(dbgs() << "]\n"); |
David Goodwin | 5305dc0 | 2009-11-20 23:33:54 +0000 | [diff] [blame] | 739 | return true; |
| 740 | |
| 741 | next_super_reg: |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 742 | LLVM_DEBUG(dbgs() << ']'); |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 743 | } while (R != EndR); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 744 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 745 | LLVM_DEBUG(dbgs() << '\n'); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 746 | |
| 747 | // No registers are free and available! |
| 748 | return false; |
| 749 | } |
| 750 | |
| 751 | /// BreakAntiDependencies - Identifiy anti-dependencies within the |
| 752 | /// ScheduleDAG and break them by renaming registers. |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 753 | unsigned AggressiveAntiDepBreaker::BreakAntiDependencies( |
Eugene Zelenko | 4f81cdd | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 754 | const std::vector<SUnit> &SUnits, |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 755 | MachineBasicBlock::iterator Begin, |
| 756 | MachineBasicBlock::iterator End, |
Devang Patel | f02a376 | 2011-06-02 21:26:52 +0000 | [diff] [blame] | 757 | unsigned InsertPosIndex, |
| 758 | DbgValueVector &DbgValues) { |
Bill Wendling | 030b028 | 2010-07-15 18:43:09 +0000 | [diff] [blame] | 759 | std::vector<unsigned> &KillIndices = State->GetKillIndices(); |
| 760 | std::vector<unsigned> &DefIndices = State->GetDefIndices(); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 761 | std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>& |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 762 | RegRefs = State->GetRegRefs(); |
| 763 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 764 | // The code below assumes that there is at least one instruction, |
| 765 | // so just duck out immediately if the block is empty. |
David Goodwin | 8501dbbe | 2009-11-03 20:57:50 +0000 | [diff] [blame] | 766 | if (SUnits.empty()) return 0; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 767 | |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 768 | // For each regclass the next register to use for renaming. |
| 769 | RenameOrderType RenameOrder; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 770 | |
| 771 | // ...need a map from MI to SUnit. |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 772 | std::map<MachineInstr *, const SUnit *> MISUnitMap; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 773 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 774 | const SUnit *SU = &SUnits[i]; |
| 775 | MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(), |
| 776 | SU)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 777 | } |
| 778 | |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 779 | // Track progress along the critical path through the SUnit graph as |
| 780 | // we walk the instructions. This is needed for regclasses that only |
| 781 | // break critical-path anti-dependencies. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 782 | const SUnit *CriticalPathSU = nullptr; |
| 783 | MachineInstr *CriticalPathMI = nullptr; |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 784 | if (CriticalPathSet.any()) { |
| 785 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) { |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 786 | const SUnit *SU = &SUnits[i]; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 787 | if (!CriticalPathSU || |
| 788 | ((SU->getDepth() + SU->Latency) > |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 789 | (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) { |
| 790 | CriticalPathSU = SU; |
| 791 | } |
| 792 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 793 | |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 794 | CriticalPathMI = CriticalPathSU->getInstr(); |
| 795 | } |
| 796 | |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 797 | #ifndef NDEBUG |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 798 | LLVM_DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n"); |
| 799 | LLVM_DEBUG(dbgs() << "Available regs:"); |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 800 | for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) { |
| 801 | if (!State->IsLive(Reg)) |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 802 | LLVM_DEBUG(dbgs() << " " << printReg(Reg, TRI)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 803 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 804 | LLVM_DEBUG(dbgs() << '\n'); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 805 | #endif |
| 806 | |
Krzysztof Parzyszek | 143f684 | 2016-05-26 18:22:53 +0000 | [diff] [blame] | 807 | BitVector RegAliases(TRI->getNumRegs()); |
| 808 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 809 | // Attempt to break anti-dependence edges. Walk the instructions |
| 810 | // from the bottom up, tracking information about liveness as we go |
| 811 | // to help determine which registers are available. |
| 812 | unsigned Broken = 0; |
| 813 | unsigned Count = InsertPosIndex - 1; |
| 814 | for (MachineBasicBlock::iterator I = End, E = Begin; |
| 815 | I != E; --Count) { |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 816 | MachineInstr &MI = *--I; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 817 | |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 818 | if (MI.isDebugInstr()) |
Hal Finkel | 8606e3c | 2012-01-16 22:53:41 +0000 | [diff] [blame] | 819 | continue; |
| 820 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 821 | LLVM_DEBUG(dbgs() << "Anti: "); |
| 822 | LLVM_DEBUG(MI.dump()); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 823 | |
| 824 | std::set<unsigned> PassthruRegs; |
| 825 | GetPassthruRegs(MI, PassthruRegs); |
| 826 | |
| 827 | // Process the defs in MI... |
| 828 | PrescanInstruction(MI, Count, PassthruRegs); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 829 | |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 830 | // The dependence edges that represent anti- and output- |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 831 | // dependencies that are candidates for breaking. |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 832 | std::vector<const SDep *> Edges; |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 833 | const SUnit *PathSU = MISUnitMap[&MI]; |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 834 | AntiDepEdges(PathSU, Edges); |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 835 | |
| 836 | // If MI is not on the critical path, then we don't rename |
| 837 | // registers in the CriticalPathSet. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 838 | BitVector *ExcludeRegs = nullptr; |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 839 | if (&MI == CriticalPathMI) { |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 840 | CriticalPathSU = CriticalPathStep(CriticalPathSU); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 841 | CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr; |
Hal Finkel | 6f1ff8e | 2013-09-12 04:22:31 +0000 | [diff] [blame] | 842 | } else if (CriticalPathSet.any()) { |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 843 | ExcludeRegs = &CriticalPathSet; |
| 844 | } |
| 845 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 846 | // Ignore KILL instructions (they form a group in ScanInstruction |
| 847 | // but don't cause any anti-dependence breaking themselves) |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 848 | if (!MI.isKill()) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 849 | // Attempt to break each anti-dependency... |
| 850 | for (unsigned i = 0, e = Edges.size(); i != e; ++i) { |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 851 | const SDep *Edge = Edges[i]; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 852 | SUnit *NextSU = Edge->getSUnit(); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 853 | |
David Goodwin | da83f7d | 2009-11-12 19:08:21 +0000 | [diff] [blame] | 854 | if ((Edge->getKind() != SDep::Anti) && |
| 855 | (Edge->getKind() != SDep::Output)) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 856 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 857 | unsigned AntiDepReg = Edge->getReg(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 858 | LLVM_DEBUG(dbgs() << "\tAntidep reg: " << printReg(AntiDepReg, TRI)); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 859 | assert(AntiDepReg != 0 && "Anti-dependence on reg0?"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 860 | |
Jakob Stoklund Olesen | f67bf3e | 2012-10-15 22:41:03 +0000 | [diff] [blame] | 861 | if (!MRI.isAllocatable(AntiDepReg)) { |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 862 | // Don't break anti-dependencies on non-allocatable registers. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 863 | LLVM_DEBUG(dbgs() << " (non-allocatable)\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 864 | continue; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 865 | } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) { |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 866 | // Don't break anti-dependencies for critical path registers |
| 867 | // if not on the critical path |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 868 | LLVM_DEBUG(dbgs() << " (not critical-path)\n"); |
David Goodwin | b9fe5d5 | 2009-11-13 19:52:48 +0000 | [diff] [blame] | 869 | continue; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 870 | } else if (PassthruRegs.count(AntiDepReg) != 0) { |
| 871 | // If the anti-dep register liveness "passes-thru", then |
| 872 | // don't try to change it. It will be changed along with |
| 873 | // the use if required to break an earlier antidep. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 874 | LLVM_DEBUG(dbgs() << " (passthru)\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 875 | continue; |
| 876 | } else { |
| 877 | // No anti-dep breaking for implicit deps |
Duncan P. N. Exon Smith | 5e6e8c7 | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 878 | MachineOperand *AntiDepOp = MI.findRegisterDefOperand(AntiDepReg); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 879 | assert(AntiDepOp && "Can't find index for defined register operand"); |
| 880 | if (!AntiDepOp || AntiDepOp->isImplicit()) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 881 | LLVM_DEBUG(dbgs() << " (implicit)\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 882 | continue; |
| 883 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 884 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 885 | // If the SUnit has other dependencies on the SUnit that |
| 886 | // it anti-depends on, don't bother breaking the |
| 887 | // anti-dependency since those edges would prevent such |
| 888 | // units from being scheduled past each other |
| 889 | // regardless. |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 890 | // |
| 891 | // Also, if there are dependencies on other SUnits with the |
| 892 | // same register as the anti-dependency, don't attempt to |
| 893 | // break it. |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 894 | for (SUnit::const_pred_iterator P = PathSU->Preds.begin(), |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 895 | PE = PathSU->Preds.end(); P != PE; ++P) { |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 896 | if (P->getSUnit() == NextSU ? |
| 897 | (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) : |
| 898 | (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) { |
| 899 | AntiDepReg = 0; |
| 900 | break; |
| 901 | } |
| 902 | } |
Dan Gohman | 35bc4d4 | 2010-04-19 23:11:58 +0000 | [diff] [blame] | 903 | for (SUnit::const_pred_iterator P = PathSU->Preds.begin(), |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 904 | PE = PathSU->Preds.end(); P != PE; ++P) { |
| 905 | if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) && |
| 906 | (P->getKind() != SDep::Output)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 907 | LLVM_DEBUG(dbgs() << " (real dependency)\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 908 | AntiDepReg = 0; |
| 909 | break; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 910 | } else if ((P->getSUnit() != NextSU) && |
| 911 | (P->getKind() == SDep::Data) && |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 912 | (P->getReg() == AntiDepReg)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 913 | LLVM_DEBUG(dbgs() << " (other dependency)\n"); |
David Goodwin | 80a03cc | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 914 | AntiDepReg = 0; |
| 915 | break; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 916 | } |
| 917 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 918 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 919 | if (AntiDepReg == 0) continue; |
Krzysztof Parzyszek | 143f684 | 2016-05-26 18:22:53 +0000 | [diff] [blame] | 920 | |
| 921 | // If the definition of the anti-dependency register does not start |
| 922 | // a new live range, bail out. This can happen if the anti-dep |
| 923 | // register is a sub-register of another register whose live range |
| 924 | // spans over PathSU. In such case, PathSU defines only a part of |
| 925 | // the larger register. |
| 926 | RegAliases.reset(); |
| 927 | for (MCRegAliasIterator AI(AntiDepReg, TRI, true); AI.isValid(); ++AI) |
| 928 | RegAliases.set(*AI); |
| 929 | for (SDep S : PathSU->Succs) { |
| 930 | SDep::Kind K = S.getKind(); |
| 931 | if (K != SDep::Data && K != SDep::Output && K != SDep::Anti) |
| 932 | continue; |
| 933 | unsigned R = S.getReg(); |
| 934 | if (!RegAliases[R]) |
| 935 | continue; |
| 936 | if (R == AntiDepReg || TRI->isSubRegister(AntiDepReg, R)) |
| 937 | continue; |
| 938 | AntiDepReg = 0; |
| 939 | break; |
| 940 | } |
| 941 | |
| 942 | if (AntiDepReg == 0) continue; |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 943 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 944 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 945 | assert(AntiDepReg != 0); |
| 946 | if (AntiDepReg == 0) continue; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 947 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 948 | // Determine AntiDepReg's register group. |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 949 | const unsigned GroupIndex = State->GetGroup(AntiDepReg); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 950 | if (GroupIndex == 0) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 951 | LLVM_DEBUG(dbgs() << " (zero group)\n"); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 952 | continue; |
| 953 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 954 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 955 | LLVM_DEBUG(dbgs() << '\n'); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 956 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 957 | // Look for a suitable register to use to break the anti-dependence. |
| 958 | std::map<unsigned, unsigned> RenameMap; |
David Goodwin | 7d8878a | 2009-11-05 01:19:35 +0000 | [diff] [blame] | 959 | if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 960 | LLVM_DEBUG(dbgs() << "\tBreaking anti-dependence edge on " |
| 961 | << printReg(AntiDepReg, TRI) << ":"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 962 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 963 | // Handle each group register... |
| 964 | for (std::map<unsigned, unsigned>::iterator |
| 965 | S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) { |
| 966 | unsigned CurrReg = S->first; |
| 967 | unsigned NewReg = S->second; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 968 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 969 | LLVM_DEBUG(dbgs() << " " << printReg(CurrReg, TRI) << "->" |
| 970 | << printReg(NewReg, TRI) << "(" |
| 971 | << RegRefs.count(CurrReg) << " refs)"); |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 972 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 973 | // Update the references to the old register CurrReg to |
| 974 | // refer to the new register NewReg. |
Benjamin Kramer | c9436ad | 2015-07-18 20:05:10 +0000 | [diff] [blame] | 975 | for (const auto &Q : make_range(RegRefs.equal_range(CurrReg))) { |
| 976 | Q.second.Operand->setReg(NewReg); |
Jim Grosbach | 12ac8f0 | 2010-06-01 23:48:44 +0000 | [diff] [blame] | 977 | // If the SU for the instruction being updated has debug |
| 978 | // information related to the anti-dependency register, make |
| 979 | // sure to update that as well. |
Benjamin Kramer | c9436ad | 2015-07-18 20:05:10 +0000 | [diff] [blame] | 980 | const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()]; |
Jim Grosbach | 8485483 | 2010-06-02 15:29:36 +0000 | [diff] [blame] | 981 | if (!SU) continue; |
Andrew Ng | 10ebfe0 | 2017-04-25 15:39:57 +0000 | [diff] [blame] | 982 | UpdateDbgValues(DbgValues, Q.second.Operand->getParent(), |
| 983 | AntiDepReg, NewReg); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 984 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 985 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 986 | // We just went back in time and modified history; the |
| 987 | // liveness information for CurrReg is now inconsistent. Set |
| 988 | // the state as if it were dead. |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 989 | State->UnionGroups(NewReg, 0); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 990 | RegRefs.erase(NewReg); |
| 991 | DefIndices[NewReg] = DefIndices[CurrReg]; |
| 992 | KillIndices[NewReg] = KillIndices[CurrReg]; |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 993 | |
David Goodwin | e056d10 | 2009-10-26 22:31:16 +0000 | [diff] [blame] | 994 | State->UnionGroups(CurrReg, 0); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 995 | RegRefs.erase(CurrReg); |
| 996 | DefIndices[CurrReg] = KillIndices[CurrReg]; |
| 997 | KillIndices[CurrReg] = ~0u; |
| 998 | assert(((KillIndices[CurrReg] == ~0u) != |
| 999 | (DefIndices[CurrReg] == ~0u)) && |
| 1000 | "Kill and Def maps aren't consistent for AntiDepReg!"); |
| 1001 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 1002 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 1003 | ++Broken; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1004 | LLVM_DEBUG(dbgs() << '\n'); |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | ScanInstruction(MI, Count); |
| 1010 | } |
Jim Grosbach | eb431da | 2010-01-06 16:48:02 +0000 | [diff] [blame] | 1011 | |
David Goodwin | de11f36 | 2009-10-26 19:32:42 +0000 | [diff] [blame] | 1012 | return Broken; |
| 1013 | } |