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