blob: 3a57772cc7f551572e20f9a750365b3365b46adb [file] [log] [blame]
Jim Grosbacheb431da2010-01-06 16:48:02 +00001//===----- AggressiveAntiDepBreaker.cpp - Anti-dep breaker ----------------===//
David Goodwinde11f362009-10-26 19:32:42 +00002//
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 Goodwinde11f362009-10-26 19:32:42 +000017#include "AggressiveAntiDepBreaker.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineInstr.h"
Andrew Trick05ff4662012-06-06 20:29:31 +000021#include "llvm/CodeGen/RegisterClassInfo.h"
David Goodwine056d102009-10-26 22:31:16 +000022#include "llvm/Support/CommandLine.h"
David Goodwinde11f362009-10-26 19:32:42 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetRegisterInfo.h"
David Goodwinde11f362009-10-26 19:32:42 +000028using namespace llvm;
29
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030#define DEBUG_TYPE "post-RA-sched"
31
David Goodwindd1c6192009-11-19 23:12:37 +000032// If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod
33static cl::opt<int>
34DebugDiv("agg-antidep-debugdiv",
Bob Wilson67dd3a42010-04-09 21:38:26 +000035 cl::desc("Debug control for aggressive anti-dep breaker"),
36 cl::init(0), cl::Hidden);
David Goodwindd1c6192009-11-19 23:12:37 +000037static cl::opt<int>
38DebugMod("agg-antidep-debugmod",
Bob Wilson67dd3a42010-04-09 21:38:26 +000039 cl::desc("Debug control for aggressive anti-dep breaker"),
40 cl::init(0), cl::Hidden);
David Goodwindd1c6192009-11-19 23:12:37 +000041
David Goodwina45fe672009-12-09 17:18:22 +000042AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
43 MachineBasicBlock *BB) :
Bill Wendling51a9c0a2010-07-15 19:58:14 +000044 NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0),
45 GroupNodeIndices(TargetRegs, 0),
46 KillIndices(TargetRegs, 0),
47 DefIndices(TargetRegs, 0)
48{
David Goodwina45fe672009-12-09 17:18:22 +000049 const unsigned BBSize = BB->size();
50 for (unsigned i = 0; i < NumTargetRegs; ++i) {
51 // Initialize all registers to be in their own group. Initially we
52 // assign the register to the same-indexed GroupNode.
53 GroupNodeIndices[i] = i;
54 // Initialize the indices to indicate that no registers are live.
55 KillIndices[i] = ~0u;
56 DefIndices[i] = BBSize;
57 }
David Goodwinde11f362009-10-26 19:32:42 +000058}
59
Bill Wendling5a8d15c2010-07-15 19:41:20 +000060unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) {
David Goodwinde11f362009-10-26 19:32:42 +000061 unsigned Node = GroupNodeIndices[Reg];
62 while (GroupNodes[Node] != Node)
63 Node = GroupNodes[Node];
64
65 return Node;
66}
67
David Goodwinb9fe5d52009-11-13 19:52:48 +000068void AggressiveAntiDepState::GetGroupRegs(
69 unsigned Group,
70 std::vector<unsigned> &Regs,
71 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
David Goodwinde11f362009-10-26 19:32:42 +000072{
David Goodwina45fe672009-12-09 17:18:22 +000073 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
David Goodwinb9fe5d52009-11-13 19:52:48 +000074 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
David Goodwinde11f362009-10-26 19:32:42 +000075 Regs.push_back(Reg);
76 }
77}
78
David Goodwine056d102009-10-26 22:31:16 +000079unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2)
David Goodwinde11f362009-10-26 19:32:42 +000080{
81 assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
82 assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
Jim Grosbacheb431da2010-01-06 16:48:02 +000083
David Goodwinde11f362009-10-26 19:32:42 +000084 // find group for each register
85 unsigned Group1 = GetGroup(Reg1);
86 unsigned Group2 = GetGroup(Reg2);
Jim Grosbacheb431da2010-01-06 16:48:02 +000087
David Goodwinde11f362009-10-26 19:32:42 +000088 // if either group is 0, then that must become the parent
89 unsigned Parent = (Group1 == 0) ? Group1 : Group2;
90 unsigned Other = (Parent == Group1) ? Group2 : Group1;
91 GroupNodes.at(Other) = Parent;
92 return Parent;
93}
Jim Grosbacheb431da2010-01-06 16:48:02 +000094
David Goodwine056d102009-10-26 22:31:16 +000095unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg)
David Goodwinde11f362009-10-26 19:32:42 +000096{
97 // Create a new GroupNode for Reg. Reg's existing GroupNode must
98 // stay as is because there could be other GroupNodes referring to
99 // it.
100 unsigned idx = GroupNodes.size();
101 GroupNodes.push_back(idx);
102 GroupNodeIndices[Reg] = idx;
103 return idx;
104}
105
David Goodwine056d102009-10-26 22:31:16 +0000106bool AggressiveAntiDepState::IsLive(unsigned Reg)
David Goodwinde11f362009-10-26 19:32:42 +0000107{
108 // KillIndex must be defined and DefIndex not defined for a register
109 // to be live.
110 return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
111}
112
Eric Christopherd9134482014-08-04 21:25:23 +0000113AggressiveAntiDepBreaker::AggressiveAntiDepBreaker(
114 MachineFunction &MFi, const RegisterClassInfo &RCI,
115 TargetSubtargetInfo::RegClassVector &CriticalPathRCs)
116 : AntiDepBreaker(), MF(MFi), MRI(MF.getRegInfo()),
Eric Christopherfc6de422014-08-05 02:39:49 +0000117 TII(MF.getSubtarget().getInstrInfo()),
118 TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
119 State(nullptr) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000120 /* Collect a bitset of all registers that are only broken if they
121 are on the critical path. */
122 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
123 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
124 if (CriticalPathSet.none())
125 CriticalPathSet = CPSet;
126 else
127 CriticalPathSet |= CPSet;
128 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000129
David Greene75a2efb2009-12-24 00:14:25 +0000130 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000131 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000132 r = CriticalPathSet.find_next(r))
David Greene75a2efb2009-12-24 00:14:25 +0000133 dbgs() << " " << TRI->getName(r));
134 DEBUG(dbgs() << '\n');
David Goodwine056d102009-10-26 22:31:16 +0000135}
136
137AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
138 delete State;
David Goodwine056d102009-10-26 22:31:16 +0000139}
140
141void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000142 assert(!State);
David Goodwina45fe672009-12-09 17:18:22 +0000143 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine056d102009-10-26 22:31:16 +0000144
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000145 bool IsReturnBlock = BB->isReturnBlock();
Bill Wendling030b0282010-07-15 18:43:09 +0000146 std::vector<unsigned> &KillIndices = State->GetKillIndices();
147 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwine056d102009-10-26 22:31:16 +0000148
Jakob Stoklund Olesenc3386792013-02-05 18:21:52 +0000149 // Examine the live-in regs of all successors.
Evan Chengf128bdc2010-06-16 07:35:02 +0000150 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
151 SE = BB->succ_end(); SI != SE; ++SI)
Matthias Braund9da1622015-09-09 18:08:03 +0000152 for (const auto &LI : (*SI)->liveins()) {
153 for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000154 unsigned Reg = *AI;
Jakob Stoklund Olesenbe1c8d32010-12-14 23:23:15 +0000155 State->UnionGroups(Reg, 0);
156 KillIndices[Reg] = BB->size();
157 DefIndices[Reg] = ~0u;
Evan Chengf128bdc2010-06-16 07:35:02 +0000158 }
159 }
160
David Goodwine056d102009-10-26 22:31:16 +0000161 // Mark live-out callee-saved registers. In a return block this is
162 // all callee-saved registers. In non-return this is any
163 // callee-saved register that is not saved in the prolog.
Matthias Braun941a7052016-07-28 18:40:00 +0000164 const MachineFrameInfo &MFI = MF.getFrameInfo();
165 BitVector Pristine = MFI.getPristineRegs(MF);
Oren Ben Simhonfe34c5e2017-03-14 09:09:26 +0000166 for (const MCPhysReg *I = MF.getRegInfo().getCalleeSavedRegs(); *I;
167 ++I) {
David Goodwine056d102009-10-26 22:31:16 +0000168 unsigned Reg = *I;
Eric Christopherb9c56d12017-03-30 22:34:20 +0000169 if (!IsReturnBlock && !(Pristine.test(Reg) || BB->isLiveIn(Reg)))
170 continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000171 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
172 unsigned AliasReg = *AI;
David Goodwine056d102009-10-26 22:31:16 +0000173 State->UnionGroups(AliasReg, 0);
174 KillIndices[AliasReg] = BB->size();
175 DefIndices[AliasReg] = ~0u;
176 }
177 }
178}
179
180void AggressiveAntiDepBreaker::FinishBlock() {
181 delete State;
Craig Topperc0196b12014-04-14 00:51:57 +0000182 State = nullptr;
David Goodwine056d102009-10-26 22:31:16 +0000183}
184
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000185void AggressiveAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000186 unsigned InsertPosIndex) {
David Goodwine056d102009-10-26 22:31:16 +0000187 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
188
David Goodwinfaa76602009-10-29 23:30:59 +0000189 std::set<unsigned> PassthruRegs;
190 GetPassthruRegs(MI, PassthruRegs);
191 PrescanInstruction(MI, Count, PassthruRegs);
192 ScanInstruction(MI, Count);
193
David Greene75a2efb2009-12-24 00:14:25 +0000194 DEBUG(dbgs() << "Observe: ");
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000195 DEBUG(MI.dump());
David Greene75a2efb2009-12-24 00:14:25 +0000196 DEBUG(dbgs() << "\tRegs:");
David Goodwine056d102009-10-26 22:31:16 +0000197
Bill Wendling030b0282010-07-15 18:43:09 +0000198 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwina45fe672009-12-09 17:18:22 +0000199 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine056d102009-10-26 22:31:16 +0000200 // If Reg is current live, then mark that it can't be renamed as
201 // we don't know the extent of its live-range anymore (now that it
202 // has been scheduled). If it is not live but was defined in the
203 // previous schedule region, then set its def index to the most
204 // conservative location (i.e. the beginning of the previous
205 // schedule region).
206 if (State->IsLive(Reg)) {
207 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbacheb431da2010-01-06 16:48:02 +0000208 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine056d102009-10-26 22:31:16 +0000209 State->GetGroup(Reg) << "->g0(region live-out)");
210 State->UnionGroups(Reg, 0);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000211 } else if ((DefIndices[Reg] < InsertPosIndex)
212 && (DefIndices[Reg] >= Count)) {
David Goodwine056d102009-10-26 22:31:16 +0000213 DefIndices[Reg] = Count;
214 }
215 }
David Greene75a2efb2009-12-24 00:14:25 +0000216 DEBUG(dbgs() << '\n');
David Goodwine056d102009-10-26 22:31:16 +0000217}
218
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000219bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr &MI,
220 MachineOperand &MO) {
David Goodwinde11f362009-10-26 19:32:42 +0000221 if (!MO.isReg() || !MO.isImplicit())
222 return false;
223
224 unsigned Reg = MO.getReg();
225 if (Reg == 0)
226 return false;
227
Chad Rosier47eba052015-10-09 19:48:48 +0000228 MachineOperand *Op = nullptr;
229 if (MO.isDef())
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000230 Op = MI.findRegisterUseOperand(Reg, true);
Chad Rosier47eba052015-10-09 19:48:48 +0000231 else
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000232 Op = MI.findRegisterDefOperand(Reg);
Chad Rosier47eba052015-10-09 19:48:48 +0000233
Craig Topperc0196b12014-04-14 00:51:57 +0000234 return(Op && Op->isImplicit());
David Goodwinde11f362009-10-26 19:32:42 +0000235}
236
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000237void AggressiveAntiDepBreaker::GetPassthruRegs(
238 MachineInstr &MI, std::set<unsigned> &PassthruRegs) {
239 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
240 MachineOperand &MO = MI.getOperand(i);
David Goodwinde11f362009-10-26 19:32:42 +0000241 if (!MO.isReg()) continue;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000242 if ((MO.isDef() && MI.isRegTiedToUseOperand(i)) ||
David Goodwinde11f362009-10-26 19:32:42 +0000243 IsImplicitDefUse(MI, MO)) {
244 const unsigned Reg = MO.getReg();
Chad Rosierabdb1d62013-05-22 23:17:36 +0000245 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
246 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000247 PassthruRegs.insert(*SubRegs);
David Goodwinde11f362009-10-26 19:32:42 +0000248 }
249 }
250}
251
David Goodwin80a03cc2009-11-20 19:32:48 +0000252/// AntiDepEdges - Return in Edges the anti- and output- dependencies
253/// in SU that we want to consider for breaking.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000254static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000255 SmallSet<unsigned, 4> RegSet;
Dan Gohman35bc4d42010-04-19 23:11:58 +0000256 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwinde11f362009-10-26 19:32:42 +0000257 P != PE; ++P) {
David Goodwinda83f7d2009-11-12 19:08:21 +0000258 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Blaikie70573dc2014-11-19 07:49:26 +0000259 if (RegSet.insert(P->getReg()).second)
David Goodwinde11f362009-10-26 19:32:42 +0000260 Edges.push_back(&*P);
David Goodwinde11f362009-10-26 19:32:42 +0000261 }
262 }
263}
264
David Goodwinb9fe5d52009-11-13 19:52:48 +0000265/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
266/// critical path.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000267static const SUnit *CriticalPathStep(const SUnit *SU) {
Craig Topperc0196b12014-04-14 00:51:57 +0000268 const SDep *Next = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000269 unsigned NextDepth = 0;
270 // Find the predecessor edge with the greatest depth.
Craig Topperc0196b12014-04-14 00:51:57 +0000271 if (SU) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000272 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwinb9fe5d52009-11-13 19:52:48 +0000273 P != PE; ++P) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000274 const SUnit *PredSU = P->getSUnit();
David Goodwinb9fe5d52009-11-13 19:52:48 +0000275 unsigned PredLatency = P->getLatency();
276 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
277 // In the case of a latency tie, prefer an anti-dependency edge over
278 // other types of edges.
279 if (NextDepth < PredTotalLatency ||
280 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
281 NextDepth = PredTotalLatency;
282 Next = &*P;
283 }
284 }
285 }
286
Craig Topperc0196b12014-04-14 00:51:57 +0000287 return (Next) ? Next->getSUnit() : nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000288}
289
David Goodwin9f1b2d42009-10-29 19:17:04 +0000290void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbacheb431da2010-01-06 16:48:02 +0000291 const char *tag,
292 const char *header,
David Goodwindd1c6192009-11-19 23:12:37 +0000293 const char *footer) {
Bill Wendling030b0282010-07-15 18:43:09 +0000294 std::vector<unsigned> &KillIndices = State->GetKillIndices();
295 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000296 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin9f1b2d42009-10-29 19:17:04 +0000297 RegRefs = State->GetRegRefs();
298
Hal Finkel34c94d52015-01-28 14:44:14 +0000299 // FIXME: We must leave subregisters of live super registers as live, so that
300 // we don't clear out the register tracking information for subregisters of
301 // super registers we're still tracking (and with which we're unioning
302 // subregister definitions).
303 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
304 if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) {
305 DEBUG(if (!header && footer) dbgs() << footer);
306 return;
307 }
308
David Goodwin9f1b2d42009-10-29 19:17:04 +0000309 if (!State->IsLive(Reg)) {
310 KillIndices[Reg] = KillIdx;
311 DefIndices[Reg] = ~0u;
312 RegRefs.erase(Reg);
313 State->LeaveGroup(Reg);
Craig Topperc0196b12014-04-14 00:51:57 +0000314 DEBUG(if (header) {
315 dbgs() << header << TRI->getName(Reg); header = nullptr; });
David Greene75a2efb2009-12-24 00:14:25 +0000316 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
Chuang-Yu Cheng35c61812016-04-01 02:05:29 +0000317 // Repeat for subregisters. Note that we only do this if the superregister
318 // was not live because otherwise, regardless whether we have an explicit
319 // use of the subregister, the subregister's contents are needed for the
320 // uses of the superregister.
321 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
322 unsigned SubregReg = *SubRegs;
323 if (!State->IsLive(SubregReg)) {
324 KillIndices[SubregReg] = KillIdx;
325 DefIndices[SubregReg] = ~0u;
326 RegRefs.erase(SubregReg);
327 State->LeaveGroup(SubregReg);
328 DEBUG(if (header) {
329 dbgs() << header << TRI->getName(Reg); header = nullptr; });
330 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
331 State->GetGroup(SubregReg) << tag);
332 }
David Goodwin9f1b2d42009-10-29 19:17:04 +0000333 }
334 }
David Goodwindd1c6192009-11-19 23:12:37 +0000335
Craig Topperc0196b12014-04-14 00:51:57 +0000336 DEBUG(if (!header && footer) dbgs() << footer);
David Goodwin9f1b2d42009-10-29 19:17:04 +0000337}
338
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000339void AggressiveAntiDepBreaker::PrescanInstruction(
340 MachineInstr &MI, unsigned Count, std::set<unsigned> &PassthruRegs) {
Bill Wendling030b0282010-07-15 18:43:09 +0000341 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000342 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000343 RegRefs = State->GetRegRefs();
344
David Goodwin9f1b2d42009-10-29 19:17:04 +0000345 // Handle dead defs by simulating a last-use of the register just
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000346 // after the def. A dead def can occur because the def is truly
David Goodwin9f1b2d42009-10-29 19:17:04 +0000347 // dead, or because only a subregister is live at the def. If we
348 // don't do this the dead def will be incorrectly merged into the
349 // previous def.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000350 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
351 MachineOperand &MO = MI.getOperand(i);
David Goodwinde11f362009-10-26 19:32:42 +0000352 if (!MO.isReg() || !MO.isDef()) continue;
353 unsigned Reg = MO.getReg();
354 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000355
David Goodwindd1c6192009-11-19 23:12:37 +0000356 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwinde11f362009-10-26 19:32:42 +0000357 }
358
David Greene75a2efb2009-12-24 00:14:25 +0000359 DEBUG(dbgs() << "\tDef Groups:");
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000360 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
361 MachineOperand &MO = MI.getOperand(i);
David Goodwinde11f362009-10-26 19:32:42 +0000362 if (!MO.isReg() || !MO.isDef()) continue;
363 unsigned Reg = MO.getReg();
364 if (Reg == 0) continue;
365
Jim Grosbacheb431da2010-01-06 16:48:02 +0000366 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000367
David Goodwin9f1b2d42009-10-29 19:17:04 +0000368 // If MI's defs have a special allocation requirement, don't allow
David Goodwinde11f362009-10-26 19:32:42 +0000369 // any def registers to be changed. Also assume all registers
Kyle Buttcf6a8bf2015-12-02 18:58:51 +0000370 // defined in a call must not be changed (ABI). Inline assembly may
371 // reference either system calls or the register directly. Skip it until we
372 // can tell user specified registers from compiler-specified.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000373 if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI) ||
374 MI.isInlineAsm()) {
David Greene75a2efb2009-12-24 00:14:25 +0000375 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine056d102009-10-26 22:31:16 +0000376 State->UnionGroups(Reg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000377 }
378
379 // Any aliased that are live at this point are completely or
David Goodwin9f1b2d42009-10-29 19:17:04 +0000380 // partially defined here, so group those aliases with Reg.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000381 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
382 unsigned AliasReg = *AI;
David Goodwine056d102009-10-26 22:31:16 +0000383 if (State->IsLive(AliasReg)) {
384 State->UnionGroups(Reg, AliasReg);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000385 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwinde11f362009-10-26 19:32:42 +0000386 TRI->getName(AliasReg) << ")");
387 }
388 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000389
David Goodwinde11f362009-10-26 19:32:42 +0000390 // Note register reference...
Craig Topperc0196b12014-04-14 00:51:57 +0000391 const TargetRegisterClass *RC = nullptr;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000392 if (i < MI.getDesc().getNumOperands())
393 RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
David Goodwine056d102009-10-26 22:31:16 +0000394 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwinde11f362009-10-26 19:32:42 +0000395 RegRefs.insert(std::make_pair(Reg, RR));
396 }
397
David Greene75a2efb2009-12-24 00:14:25 +0000398 DEBUG(dbgs() << '\n');
David Goodwin9f1b2d42009-10-29 19:17:04 +0000399
400 // Scan the register defs for this instruction and update
401 // live-ranges.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000402 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
403 MachineOperand &MO = MI.getOperand(i);
David Goodwin9f1b2d42009-10-29 19:17:04 +0000404 if (!MO.isReg() || !MO.isDef()) continue;
405 unsigned Reg = MO.getReg();
406 if (Reg == 0) continue;
David Goodwindd1c6192009-11-19 23:12:37 +0000407 // Ignore KILLs and passthru registers for liveness...
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000408 if (MI.isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwindd1c6192009-11-19 23:12:37 +0000409 continue;
David Goodwin9f1b2d42009-10-29 19:17:04 +0000410
David Goodwindd1c6192009-11-19 23:12:37 +0000411 // Update def for Reg and aliases.
Hal Finkel121caf62014-02-26 20:20:30 +0000412 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
413 // We need to be careful here not to define already-live super registers.
414 // If the super register is already live, then this definition is not
415 // a definition of the whole super register (just a partial insertion
416 // into it). Earlier subregister definitions (which we've not yet visited
417 // because we're iterating bottom-up) need to be linked to the same group
418 // as this definition.
419 if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI))
420 continue;
421
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000422 DefIndices[*AI] = Count;
Hal Finkel121caf62014-02-26 20:20:30 +0000423 }
David Goodwin9f1b2d42009-10-29 19:17:04 +0000424 }
David Goodwinde11f362009-10-26 19:32:42 +0000425}
426
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000427void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr &MI,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000428 unsigned Count) {
David Greene75a2efb2009-12-24 00:14:25 +0000429 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000430 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000431 RegRefs = State->GetRegRefs();
David Goodwinde11f362009-10-26 19:32:42 +0000432
Evan Chengf128bdc2010-06-16 07:35:02 +0000433 // If MI's uses have special allocation requirement, don't allow
434 // any use registers to be changed. Also assume all registers
435 // used in a call must not be changed (ABI).
Kyle Buttcf6a8bf2015-12-02 18:58:51 +0000436 // Inline Assembly register uses also cannot be safely changed.
Evan Chengf128bdc2010-06-16 07:35:02 +0000437 // FIXME: The issue with predicated instruction is more complex. We are being
438 // conservatively here because the kill markers cannot be trusted after
439 // if-conversion:
440 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
441 // ...
442 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
443 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
444 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
445 //
446 // The first R6 kill is not really a kill since it's killed by a predicated
447 // instruction which may not be executed. The second R6 def may or may not
448 // re-define R6 so it's not safe to change it since the last R6 use cannot be
449 // changed.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000450 bool Special = MI.isCall() || MI.hasExtraSrcRegAllocReq() ||
451 TII->isPredicated(MI) || MI.isInlineAsm();
Evan Chengf128bdc2010-06-16 07:35:02 +0000452
David Goodwinde11f362009-10-26 19:32:42 +0000453 // Scan the register uses for this instruction and update
454 // live-ranges, groups and RegRefs.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000455 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
456 MachineOperand &MO = MI.getOperand(i);
David Goodwinde11f362009-10-26 19:32:42 +0000457 if (!MO.isReg() || !MO.isUse()) continue;
458 unsigned Reg = MO.getReg();
459 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000460
461 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
462 State->GetGroup(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000463
464 // It wasn't previously live but now it is, this is a kill. Forget
465 // the previous live-range information and start a new live-range
466 // for the register.
David Goodwin9f1b2d42009-10-29 19:17:04 +0000467 HandleLastUse(Reg, Count, "(last-use)");
David Goodwinde11f362009-10-26 19:32:42 +0000468
Evan Chengf128bdc2010-06-16 07:35:02 +0000469 if (Special) {
David Greene75a2efb2009-12-24 00:14:25 +0000470 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine056d102009-10-26 22:31:16 +0000471 State->UnionGroups(Reg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000472 }
473
474 // Note register reference...
Craig Topperc0196b12014-04-14 00:51:57 +0000475 const TargetRegisterClass *RC = nullptr;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000476 if (i < MI.getDesc().getNumOperands())
477 RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
David Goodwine056d102009-10-26 22:31:16 +0000478 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwinde11f362009-10-26 19:32:42 +0000479 RegRefs.insert(std::make_pair(Reg, RR));
480 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000481
David Greene75a2efb2009-12-24 00:14:25 +0000482 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000483
484 // Form a group of all defs and uses of a KILL instruction to ensure
485 // that all registers are renamed as a group.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000486 if (MI.isKill()) {
David Greene75a2efb2009-12-24 00:14:25 +0000487 DEBUG(dbgs() << "\tKill Group:");
David Goodwinde11f362009-10-26 19:32:42 +0000488
489 unsigned FirstReg = 0;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000490 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
491 MachineOperand &MO = MI.getOperand(i);
David Goodwinde11f362009-10-26 19:32:42 +0000492 if (!MO.isReg()) continue;
493 unsigned Reg = MO.getReg();
494 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000495
David Goodwinde11f362009-10-26 19:32:42 +0000496 if (FirstReg != 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000497 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine056d102009-10-26 22:31:16 +0000498 State->UnionGroups(FirstReg, Reg);
David Goodwinde11f362009-10-26 19:32:42 +0000499 } else {
David Greene75a2efb2009-12-24 00:14:25 +0000500 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000501 FirstReg = Reg;
502 }
503 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000504
David Greene75a2efb2009-12-24 00:14:25 +0000505 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000506 }
507}
508
509BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
510 BitVector BV(TRI->getNumRegs(), false);
511 bool first = true;
512
513 // Check all references that need rewriting for Reg. For each, use
514 // the corresponding register class to narrow the set of registers
515 // that are appropriate for renaming.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000516 for (const auto &Q : make_range(State->GetRegRefs().equal_range(Reg))) {
517 const TargetRegisterClass *RC = Q.second.RC;
Craig Topperc0196b12014-04-14 00:51:57 +0000518 if (!RC) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000519
520 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
521 if (first) {
522 BV |= RCBV;
523 first = false;
524 } else {
525 BV &= RCBV;
526 }
527
Craig Toppercf0444b2014-11-17 05:50:14 +0000528 DEBUG(dbgs() << " " << TRI->getRegClassName(RC));
David Goodwinde11f362009-10-26 19:32:42 +0000529 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000530
David Goodwinde11f362009-10-26 19:32:42 +0000531 return BV;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000532}
David Goodwinde11f362009-10-26 19:32:42 +0000533
534bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin7d8878a2009-11-05 01:19:35 +0000535 unsigned AntiDepGroupIndex,
536 RenameOrderType& RenameOrder,
537 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendling030b0282010-07-15 18:43:09 +0000538 std::vector<unsigned> &KillIndices = State->GetKillIndices();
539 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000540 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000541 RegRefs = State->GetRegRefs();
542
David Goodwinb9fe5d52009-11-13 19:52:48 +0000543 // Collect all referenced registers in the same group as
544 // AntiDepReg. These all need to be renamed together if we are to
545 // break the anti-dependence.
David Goodwinde11f362009-10-26 19:32:42 +0000546 std::vector<unsigned> Regs;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000547 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwinde11f362009-10-26 19:32:42 +0000548 assert(Regs.size() > 0 && "Empty register group!");
549 if (Regs.size() == 0)
550 return false;
551
552 // Find the "superest" register in the group. At the same time,
553 // collect the BitVector of registers that can be used to rename
554 // each register.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000555 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
556 << ":\n");
David Goodwinde11f362009-10-26 19:32:42 +0000557 std::map<unsigned, BitVector> RenameRegisterMap;
558 unsigned SuperReg = 0;
559 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
560 unsigned Reg = Regs[i];
561 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
562 SuperReg = Reg;
563
564 // If Reg has any references, then collect possible rename regs
565 if (RegRefs.count(Reg) > 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000566 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000567
Benjamin Kramer7f75e942016-02-13 16:39:39 +0000568 BitVector &BV = RenameRegisterMap[Reg];
569 assert(BV.empty());
570 BV = GetRenameRegisters(Reg);
David Goodwinde11f362009-10-26 19:32:42 +0000571
Benjamin Kramer7f75e942016-02-13 16:39:39 +0000572 DEBUG({
573 dbgs() << " ::";
574 for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
575 dbgs() << " " << TRI->getName(r);
576 dbgs() << "\n";
577 });
David Goodwinde11f362009-10-26 19:32:42 +0000578 }
579 }
580
581 // All group registers should be a subreg of SuperReg.
582 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
583 unsigned Reg = Regs[i];
584 if (Reg == SuperReg) continue;
585 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
Will Schmidt44ff8f02014-07-31 19:50:53 +0000586 // FIXME: remove this once PR18663 has been properly fixed. For now,
587 // return a conservative answer:
588 // assert(IsSub && "Expecting group subregister");
David Goodwinde11f362009-10-26 19:32:42 +0000589 if (!IsSub)
590 return false;
591 }
592
David Goodwin5305dc02009-11-20 23:33:54 +0000593#ifndef NDEBUG
594 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
595 if (DebugDiv > 0) {
596 static int renamecnt = 0;
597 if (renamecnt++ % DebugDiv != DebugMod)
598 return false;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000599
David Greene75a2efb2009-12-24 00:14:25 +0000600 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin5305dc02009-11-20 23:33:54 +0000601 " for debug ***\n";
602 }
603#endif
604
David Goodwin7d8878a2009-11-05 01:19:35 +0000605 // Check each possible rename register for SuperReg in round-robin
606 // order. If that register is available, and the corresponding
607 // registers are available for the other group subregisters, then we
608 // can use those registers to rename.
Rafael Espindola871c7242010-07-12 02:55:34 +0000609
610 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
611 // check every use of the register and find the largest register class
612 // that can be used in all of them.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000613 const TargetRegisterClass *SuperRC =
Rafael Espindola871c7242010-07-12 02:55:34 +0000614 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000615
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000616 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000617 if (Order.empty()) {
David Greene75a2efb2009-12-24 00:14:25 +0000618 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin7d8878a2009-11-05 01:19:35 +0000619 return false;
620 }
621
David Greene75a2efb2009-12-24 00:14:25 +0000622 DEBUG(dbgs() << "\tFind Registers:");
David Goodwindd1c6192009-11-19 23:12:37 +0000623
Benjamin Kramer2c99e412014-10-10 15:32:50 +0000624 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
David Goodwin7d8878a2009-11-05 01:19:35 +0000625
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000626 unsigned OrigR = RenameOrder[SuperRC];
627 unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
628 unsigned R = OrigR;
David Goodwin7d8878a2009-11-05 01:19:35 +0000629 do {
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000630 if (R == 0) R = Order.size();
David Goodwin7d8878a2009-11-05 01:19:35 +0000631 --R;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000632 const unsigned NewSuperReg = Order[R];
Jim Grosbach944aece2010-09-02 17:12:55 +0000633 // Don't consider non-allocatable registers
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000634 if (!MRI.isAllocatable(NewSuperReg)) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000635 // Don't replace a register with itself.
David Goodwin5305dc02009-11-20 23:33:54 +0000636 if (NewSuperReg == SuperReg) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000637
David Greene75a2efb2009-12-24 00:14:25 +0000638 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin5305dc02009-11-20 23:33:54 +0000639 RenameMap.clear();
640
641 // For each referenced group register (which must be a SuperReg or
642 // a subregister of SuperReg), find the corresponding subregister
643 // of NewSuperReg and make sure it is free to be renamed.
644 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
645 unsigned Reg = Regs[i];
646 unsigned NewReg = 0;
647 if (Reg == SuperReg) {
648 NewReg = NewSuperReg;
649 } else {
650 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
651 if (NewSubRegIdx != 0)
652 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwinde11f362009-10-26 19:32:42 +0000653 }
David Goodwin5305dc02009-11-20 23:33:54 +0000654
David Greene75a2efb2009-12-24 00:14:25 +0000655 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbacheb431da2010-01-06 16:48:02 +0000656
David Goodwin5305dc02009-11-20 23:33:54 +0000657 // Check if Reg can be renamed to NewReg.
Benjamin Kramer7f75e942016-02-13 16:39:39 +0000658 if (!RenameRegisterMap[Reg].test(NewReg)) {
David Greene75a2efb2009-12-24 00:14:25 +0000659 DEBUG(dbgs() << "(no rename)");
David Goodwin5305dc02009-11-20 23:33:54 +0000660 goto next_super_reg;
661 }
662
663 // If NewReg is dead and NewReg's most recent def is not before
664 // Regs's kill, it's safe to replace Reg with NewReg. We
665 // must also check all aliases of NewReg, because we can't define a
666 // register when any sub or super is already live.
667 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene75a2efb2009-12-24 00:14:25 +0000668 DEBUG(dbgs() << "(live)");
David Goodwin5305dc02009-11-20 23:33:54 +0000669 goto next_super_reg;
670 } else {
671 bool found = false;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000672 for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
673 unsigned AliasReg = *AI;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000674 if (State->IsLive(AliasReg) ||
675 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene75a2efb2009-12-24 00:14:25 +0000676 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin5305dc02009-11-20 23:33:54 +0000677 found = true;
678 break;
679 }
680 }
681 if (found)
682 goto next_super_reg;
683 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000684
Hal Finkelc8cf2b82014-12-09 01:00:59 +0000685 // We cannot rename 'Reg' to 'NewReg' if one of the uses of 'Reg' also
686 // defines 'NewReg' via an early-clobber operand.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000687 for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
688 MachineInstr *UseMI = Q.second.Operand->getParent();
Hal Finkelc8cf2b82014-12-09 01:00:59 +0000689 int Idx = UseMI->findRegisterDefOperandIdx(NewReg, false, true, TRI);
690 if (Idx == -1)
691 continue;
692
693 if (UseMI->getOperand(Idx).isEarlyClobber()) {
694 DEBUG(dbgs() << "(ec)");
695 goto next_super_reg;
696 }
697 }
698
Hal Finkele0a28e52015-08-31 07:51:36 +0000699 // Also, we cannot rename 'Reg' to 'NewReg' if the instruction defining
700 // 'Reg' is an early-clobber define and that instruction also uses
701 // 'NewReg'.
702 for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
703 if (!Q.second.Operand->isDef() || !Q.second.Operand->isEarlyClobber())
704 continue;
705
706 MachineInstr *DefMI = Q.second.Operand->getParent();
707 if (DefMI->readsRegister(NewReg, TRI)) {
708 DEBUG(dbgs() << "(ec)");
709 goto next_super_reg;
710 }
711 }
712
David Goodwin5305dc02009-11-20 23:33:54 +0000713 // Record that 'Reg' can be renamed to 'NewReg'.
714 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwinde11f362009-10-26 19:32:42 +0000715 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000716
David Goodwin5305dc02009-11-20 23:33:54 +0000717 // If we fall-out here, then every register in the group can be
718 // renamed, as recorded in RenameMap.
719 RenameOrder.erase(SuperRC);
720 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene75a2efb2009-12-24 00:14:25 +0000721 DEBUG(dbgs() << "]\n");
David Goodwin5305dc02009-11-20 23:33:54 +0000722 return true;
723
724 next_super_reg:
David Greene75a2efb2009-12-24 00:14:25 +0000725 DEBUG(dbgs() << ']');
David Goodwin7d8878a2009-11-05 01:19:35 +0000726 } while (R != EndR);
David Goodwinde11f362009-10-26 19:32:42 +0000727
David Greene75a2efb2009-12-24 00:14:25 +0000728 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000729
730 // No registers are free and available!
731 return false;
732}
733
734/// BreakAntiDependencies - Identifiy anti-dependencies within the
735/// ScheduleDAG and break them by renaming registers.
736///
David Goodwine056d102009-10-26 22:31:16 +0000737unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman35bc4d42010-04-19 23:11:58 +0000738 const std::vector<SUnit>& SUnits,
739 MachineBasicBlock::iterator Begin,
740 MachineBasicBlock::iterator End,
Devang Patelf02a3762011-06-02 21:26:52 +0000741 unsigned InsertPosIndex,
742 DbgValueVector &DbgValues) {
743
Bill Wendling030b0282010-07-15 18:43:09 +0000744 std::vector<unsigned> &KillIndices = State->GetKillIndices();
745 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000746 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000747 RegRefs = State->GetRegRefs();
748
David Goodwinde11f362009-10-26 19:32:42 +0000749 // The code below assumes that there is at least one instruction,
750 // so just duck out immediately if the block is empty.
David Goodwin8501dbbe2009-11-03 20:57:50 +0000751 if (SUnits.empty()) return 0;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000752
David Goodwin7d8878a2009-11-05 01:19:35 +0000753 // For each regclass the next register to use for renaming.
754 RenameOrderType RenameOrder;
David Goodwinde11f362009-10-26 19:32:42 +0000755
756 // ...need a map from MI to SUnit.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000757 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwinde11f362009-10-26 19:32:42 +0000758 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000759 const SUnit *SU = &SUnits[i];
760 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
761 SU));
David Goodwinde11f362009-10-26 19:32:42 +0000762 }
763
David Goodwinb9fe5d52009-11-13 19:52:48 +0000764 // Track progress along the critical path through the SUnit graph as
765 // we walk the instructions. This is needed for regclasses that only
766 // break critical-path anti-dependencies.
Craig Topperc0196b12014-04-14 00:51:57 +0000767 const SUnit *CriticalPathSU = nullptr;
768 MachineInstr *CriticalPathMI = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000769 if (CriticalPathSet.any()) {
770 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000771 const SUnit *SU = &SUnits[i];
Jim Grosbacheb431da2010-01-06 16:48:02 +0000772 if (!CriticalPathSU ||
773 ((SU->getDepth() + SU->Latency) >
David Goodwinb9fe5d52009-11-13 19:52:48 +0000774 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
775 CriticalPathSU = SU;
776 }
777 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000778
David Goodwinb9fe5d52009-11-13 19:52:48 +0000779 CriticalPathMI = CriticalPathSU->getInstr();
780 }
781
Jim Grosbacheb431da2010-01-06 16:48:02 +0000782#ifndef NDEBUG
David Greene75a2efb2009-12-24 00:14:25 +0000783 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
784 DEBUG(dbgs() << "Available regs:");
David Goodwin80a03cc2009-11-20 19:32:48 +0000785 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
786 if (!State->IsLive(Reg))
David Greene75a2efb2009-12-24 00:14:25 +0000787 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000788 }
David Greene75a2efb2009-12-24 00:14:25 +0000789 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000790#endif
791
Krzysztof Parzyszek143f6842016-05-26 18:22:53 +0000792 BitVector RegAliases(TRI->getNumRegs());
793
David Goodwinde11f362009-10-26 19:32:42 +0000794 // Attempt to break anti-dependence edges. Walk the instructions
795 // from the bottom up, tracking information about liveness as we go
796 // to help determine which registers are available.
797 unsigned Broken = 0;
798 unsigned Count = InsertPosIndex - 1;
799 for (MachineBasicBlock::iterator I = End, E = Begin;
800 I != E; --Count) {
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000801 MachineInstr &MI = *--I;
David Goodwinde11f362009-10-26 19:32:42 +0000802
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000803 if (MI.isDebugValue())
Hal Finkel8606e3c2012-01-16 22:53:41 +0000804 continue;
805
David Greene75a2efb2009-12-24 00:14:25 +0000806 DEBUG(dbgs() << "Anti: ");
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000807 DEBUG(MI.dump());
David Goodwinde11f362009-10-26 19:32:42 +0000808
809 std::set<unsigned> PassthruRegs;
810 GetPassthruRegs(MI, PassthruRegs);
811
812 // Process the defs in MI...
813 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000814
David Goodwin80a03cc2009-11-20 19:32:48 +0000815 // The dependence edges that represent anti- and output-
David Goodwinb9fe5d52009-11-13 19:52:48 +0000816 // dependencies that are candidates for breaking.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000817 std::vector<const SDep *> Edges;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000818 const SUnit *PathSU = MISUnitMap[&MI];
David Goodwin80a03cc2009-11-20 19:32:48 +0000819 AntiDepEdges(PathSU, Edges);
David Goodwinb9fe5d52009-11-13 19:52:48 +0000820
821 // If MI is not on the critical path, then we don't rename
822 // registers in the CriticalPathSet.
Craig Topperc0196b12014-04-14 00:51:57 +0000823 BitVector *ExcludeRegs = nullptr;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000824 if (&MI == CriticalPathMI) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000825 CriticalPathSU = CriticalPathStep(CriticalPathSU);
Craig Topperc0196b12014-04-14 00:51:57 +0000826 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr;
Hal Finkel6f1ff8e2013-09-12 04:22:31 +0000827 } else if (CriticalPathSet.any()) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000828 ExcludeRegs = &CriticalPathSet;
829 }
830
David Goodwinde11f362009-10-26 19:32:42 +0000831 // Ignore KILL instructions (they form a group in ScanInstruction
832 // but don't cause any anti-dependence breaking themselves)
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000833 if (!MI.isKill()) {
David Goodwinde11f362009-10-26 19:32:42 +0000834 // Attempt to break each anti-dependency...
835 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000836 const SDep *Edge = Edges[i];
David Goodwinde11f362009-10-26 19:32:42 +0000837 SUnit *NextSU = Edge->getSUnit();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000838
David Goodwinda83f7d2009-11-12 19:08:21 +0000839 if ((Edge->getKind() != SDep::Anti) &&
840 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000841
David Goodwinde11f362009-10-26 19:32:42 +0000842 unsigned AntiDepReg = Edge->getReg();
David Greene75a2efb2009-12-24 00:14:25 +0000843 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwinde11f362009-10-26 19:32:42 +0000844 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000845
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000846 if (!MRI.isAllocatable(AntiDepReg)) {
David Goodwinde11f362009-10-26 19:32:42 +0000847 // Don't break anti-dependencies on non-allocatable registers.
David Greene75a2efb2009-12-24 00:14:25 +0000848 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000849 continue;
Craig Topperc0196b12014-04-14 00:51:57 +0000850 } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000851 // Don't break anti-dependencies for critical path registers
852 // if not on the critical path
David Greene75a2efb2009-12-24 00:14:25 +0000853 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwinb9fe5d52009-11-13 19:52:48 +0000854 continue;
David Goodwinde11f362009-10-26 19:32:42 +0000855 } else if (PassthruRegs.count(AntiDepReg) != 0) {
856 // If the anti-dep register liveness "passes-thru", then
857 // don't try to change it. It will be changed along with
858 // the use if required to break an earlier antidep.
David Greene75a2efb2009-12-24 00:14:25 +0000859 DEBUG(dbgs() << " (passthru)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000860 continue;
861 } else {
862 // No anti-dep breaking for implicit deps
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000863 MachineOperand *AntiDepOp = MI.findRegisterDefOperand(AntiDepReg);
Craig Topperc0196b12014-04-14 00:51:57 +0000864 assert(AntiDepOp && "Can't find index for defined register operand");
865 if (!AntiDepOp || AntiDepOp->isImplicit()) {
David Greene75a2efb2009-12-24 00:14:25 +0000866 DEBUG(dbgs() << " (implicit)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000867 continue;
868 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000869
David Goodwinde11f362009-10-26 19:32:42 +0000870 // If the SUnit has other dependencies on the SUnit that
871 // it anti-depends on, don't bother breaking the
872 // anti-dependency since those edges would prevent such
873 // units from being scheduled past each other
874 // regardless.
David Goodwin80a03cc2009-11-20 19:32:48 +0000875 //
876 // Also, if there are dependencies on other SUnits with the
877 // same register as the anti-dependency, don't attempt to
878 // break it.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000879 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwinde11f362009-10-26 19:32:42 +0000880 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000881 if (P->getSUnit() == NextSU ?
882 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
883 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
884 AntiDepReg = 0;
885 break;
886 }
887 }
Dan Gohman35bc4d42010-04-19 23:11:58 +0000888 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin80a03cc2009-11-20 19:32:48 +0000889 PE = PathSU->Preds.end(); P != PE; ++P) {
890 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
891 (P->getKind() != SDep::Output)) {
David Greene75a2efb2009-12-24 00:14:25 +0000892 DEBUG(dbgs() << " (real dependency)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000893 AntiDepReg = 0;
894 break;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000895 } else if ((P->getSUnit() != NextSU) &&
896 (P->getKind() == SDep::Data) &&
David Goodwin80a03cc2009-11-20 19:32:48 +0000897 (P->getReg() == AntiDepReg)) {
David Greene75a2efb2009-12-24 00:14:25 +0000898 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin80a03cc2009-11-20 19:32:48 +0000899 AntiDepReg = 0;
900 break;
David Goodwinde11f362009-10-26 19:32:42 +0000901 }
902 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000903
David Goodwinde11f362009-10-26 19:32:42 +0000904 if (AntiDepReg == 0) continue;
Krzysztof Parzyszek143f6842016-05-26 18:22:53 +0000905
906 // If the definition of the anti-dependency register does not start
907 // a new live range, bail out. This can happen if the anti-dep
908 // register is a sub-register of another register whose live range
909 // spans over PathSU. In such case, PathSU defines only a part of
910 // the larger register.
911 RegAliases.reset();
912 for (MCRegAliasIterator AI(AntiDepReg, TRI, true); AI.isValid(); ++AI)
913 RegAliases.set(*AI);
914 for (SDep S : PathSU->Succs) {
915 SDep::Kind K = S.getKind();
916 if (K != SDep::Data && K != SDep::Output && K != SDep::Anti)
917 continue;
918 unsigned R = S.getReg();
919 if (!RegAliases[R])
920 continue;
921 if (R == AntiDepReg || TRI->isSubRegister(AntiDepReg, R))
922 continue;
923 AntiDepReg = 0;
924 break;
925 }
926
927 if (AntiDepReg == 0) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000928 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000929
David Goodwinde11f362009-10-26 19:32:42 +0000930 assert(AntiDepReg != 0);
931 if (AntiDepReg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000932
David Goodwinde11f362009-10-26 19:32:42 +0000933 // Determine AntiDepReg's register group.
David Goodwine056d102009-10-26 22:31:16 +0000934 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwinde11f362009-10-26 19:32:42 +0000935 if (GroupIndex == 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000936 DEBUG(dbgs() << " (zero group)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000937 continue;
938 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000939
David Greene75a2efb2009-12-24 00:14:25 +0000940 DEBUG(dbgs() << '\n');
Jim Grosbacheb431da2010-01-06 16:48:02 +0000941
David Goodwinde11f362009-10-26 19:32:42 +0000942 // Look for a suitable register to use to break the anti-dependence.
943 std::map<unsigned, unsigned> RenameMap;
David Goodwin7d8878a2009-11-05 01:19:35 +0000944 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene75a2efb2009-12-24 00:14:25 +0000945 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwinde11f362009-10-26 19:32:42 +0000946 << TRI->getName(AntiDepReg) << ":");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000947
David Goodwinde11f362009-10-26 19:32:42 +0000948 // Handle each group register...
949 for (std::map<unsigned, unsigned>::iterator
950 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
951 unsigned CurrReg = S->first;
952 unsigned NewReg = S->second;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000953
954 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
955 TRI->getName(NewReg) << "(" <<
David Goodwinde11f362009-10-26 19:32:42 +0000956 RegRefs.count(CurrReg) << " refs)");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000957
David Goodwinde11f362009-10-26 19:32:42 +0000958 // Update the references to the old register CurrReg to
959 // refer to the new register NewReg.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000960 for (const auto &Q : make_range(RegRefs.equal_range(CurrReg))) {
961 Q.second.Operand->setReg(NewReg);
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000962 // If the SU for the instruction being updated has debug
963 // information related to the anti-dependency register, make
964 // sure to update that as well.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000965 const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()];
Jim Grosbach84854832010-06-02 15:29:36 +0000966 if (!SU) continue;
Andrew Ng10ebfe02017-04-25 15:39:57 +0000967 UpdateDbgValues(DbgValues, Q.second.Operand->getParent(),
968 AntiDepReg, NewReg);
David Goodwinde11f362009-10-26 19:32:42 +0000969 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000970
David Goodwinde11f362009-10-26 19:32:42 +0000971 // We just went back in time and modified history; the
972 // liveness information for CurrReg is now inconsistent. Set
973 // the state as if it were dead.
David Goodwine056d102009-10-26 22:31:16 +0000974 State->UnionGroups(NewReg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000975 RegRefs.erase(NewReg);
976 DefIndices[NewReg] = DefIndices[CurrReg];
977 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbacheb431da2010-01-06 16:48:02 +0000978
David Goodwine056d102009-10-26 22:31:16 +0000979 State->UnionGroups(CurrReg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000980 RegRefs.erase(CurrReg);
981 DefIndices[CurrReg] = KillIndices[CurrReg];
982 KillIndices[CurrReg] = ~0u;
983 assert(((KillIndices[CurrReg] == ~0u) !=
984 (DefIndices[CurrReg] == ~0u)) &&
985 "Kill and Def maps aren't consistent for AntiDepReg!");
986 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000987
David Goodwinde11f362009-10-26 19:32:42 +0000988 ++Broken;
David Greene75a2efb2009-12-24 00:14:25 +0000989 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000990 }
991 }
992 }
993
994 ScanInstruction(MI, Count);
995 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000996
David Goodwinde11f362009-10-26 19:32:42 +0000997 return Broken;
998}