blob: fc5dcfae863d399e19b3cf70507a1974c8d58d39 [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
Evan Cheng7f8e5632011-12-07 07:15:52 +0000145 bool IsReturnBlock = (!BB->empty() && BB->back().isReturn());
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)
152 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
153 E = (*SI)->livein_end(); I != E; ++I) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000154 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
155 unsigned Reg = *AI;
Jakob Stoklund Olesenbe1c8d32010-12-14 23:23:15 +0000156 State->UnionGroups(Reg, 0);
157 KillIndices[Reg] = BB->size();
158 DefIndices[Reg] = ~0u;
Evan Chengf128bdc2010-06-16 07:35:02 +0000159 }
160 }
161
David Goodwine056d102009-10-26 22:31:16 +0000162 // Mark live-out callee-saved registers. In a return block this is
163 // all callee-saved registers. In non-return this is any
164 // callee-saved register that is not saved in the prolog.
165 const MachineFrameInfo *MFI = MF.getFrameInfo();
Matthias Braun111f5d82015-05-28 23:20:35 +0000166 BitVector Pristine = MFI->getPristineRegs(MF);
Craig Topper840beec2014-04-04 05:16:06 +0000167 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
David Goodwine056d102009-10-26 22:31:16 +0000168 unsigned Reg = *I;
169 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000170 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
171 unsigned AliasReg = *AI;
David Goodwine056d102009-10-26 22:31:16 +0000172 State->UnionGroups(AliasReg, 0);
173 KillIndices[AliasReg] = BB->size();
174 DefIndices[AliasReg] = ~0u;
175 }
176 }
177}
178
179void AggressiveAntiDepBreaker::FinishBlock() {
180 delete State;
Craig Topperc0196b12014-04-14 00:51:57 +0000181 State = nullptr;
David Goodwine056d102009-10-26 22:31:16 +0000182}
183
184void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000185 unsigned InsertPosIndex) {
David Goodwine056d102009-10-26 22:31:16 +0000186 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
187
David Goodwinfaa76602009-10-29 23:30:59 +0000188 std::set<unsigned> PassthruRegs;
189 GetPassthruRegs(MI, PassthruRegs);
190 PrescanInstruction(MI, Count, PassthruRegs);
191 ScanInstruction(MI, Count);
192
David Greene75a2efb2009-12-24 00:14:25 +0000193 DEBUG(dbgs() << "Observe: ");
David Goodwine056d102009-10-26 22:31:16 +0000194 DEBUG(MI->dump());
David Greene75a2efb2009-12-24 00:14:25 +0000195 DEBUG(dbgs() << "\tRegs:");
David Goodwine056d102009-10-26 22:31:16 +0000196
Bill Wendling030b0282010-07-15 18:43:09 +0000197 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwina45fe672009-12-09 17:18:22 +0000198 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine056d102009-10-26 22:31:16 +0000199 // If Reg is current live, then mark that it can't be renamed as
200 // we don't know the extent of its live-range anymore (now that it
201 // has been scheduled). If it is not live but was defined in the
202 // previous schedule region, then set its def index to the most
203 // conservative location (i.e. the beginning of the previous
204 // schedule region).
205 if (State->IsLive(Reg)) {
206 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbacheb431da2010-01-06 16:48:02 +0000207 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine056d102009-10-26 22:31:16 +0000208 State->GetGroup(Reg) << "->g0(region live-out)");
209 State->UnionGroups(Reg, 0);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000210 } else if ((DefIndices[Reg] < InsertPosIndex)
211 && (DefIndices[Reg] >= Count)) {
David Goodwine056d102009-10-26 22:31:16 +0000212 DefIndices[Reg] = Count;
213 }
214 }
David Greene75a2efb2009-12-24 00:14:25 +0000215 DEBUG(dbgs() << '\n');
David Goodwine056d102009-10-26 22:31:16 +0000216}
217
David Goodwinde11f362009-10-26 19:32:42 +0000218bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000219 MachineOperand& MO)
David Goodwinde11f362009-10-26 19:32:42 +0000220{
221 if (!MO.isReg() || !MO.isImplicit())
222 return false;
223
224 unsigned Reg = MO.getReg();
225 if (Reg == 0)
226 return false;
227
Craig Topperc0196b12014-04-14 00:51:57 +0000228 MachineOperand *Op = nullptr;
David Goodwinde11f362009-10-26 19:32:42 +0000229 if (MO.isDef())
230 Op = MI->findRegisterUseOperand(Reg, true);
231 else
232 Op = MI->findRegisterDefOperand(Reg);
233
Craig Topperc0196b12014-04-14 00:51:57 +0000234 return(Op && Op->isImplicit());
David Goodwinde11f362009-10-26 19:32:42 +0000235}
236
237void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
238 std::set<unsigned>& PassthruRegs) {
239 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
240 MachineOperand &MO = MI->getOperand(i);
241 if (!MO.isReg()) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +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);
David Goodwin9f1b2d42009-10-29 19:17:04 +0000317 }
318 // Repeat for subregisters.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000319 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
320 unsigned SubregReg = *SubRegs;
David Goodwin9f1b2d42009-10-29 19:17:04 +0000321 if (!State->IsLive(SubregReg)) {
322 KillIndices[SubregReg] = KillIdx;
323 DefIndices[SubregReg] = ~0u;
324 RegRefs.erase(SubregReg);
325 State->LeaveGroup(SubregReg);
Craig Topperc0196b12014-04-14 00:51:57 +0000326 DEBUG(if (header) {
327 dbgs() << header << TRI->getName(Reg); header = nullptr; });
David Greene75a2efb2009-12-24 00:14:25 +0000328 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin9f1b2d42009-10-29 19:17:04 +0000329 State->GetGroup(SubregReg) << tag);
330 }
331 }
David Goodwindd1c6192009-11-19 23:12:37 +0000332
Craig Topperc0196b12014-04-14 00:51:57 +0000333 DEBUG(if (!header && footer) dbgs() << footer);
David Goodwin9f1b2d42009-10-29 19:17:04 +0000334}
335
Jim Grosbacheb431da2010-01-06 16:48:02 +0000336void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
337 unsigned Count,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000338 std::set<unsigned>& PassthruRegs) {
Bill Wendling030b0282010-07-15 18:43:09 +0000339 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000340 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000341 RegRefs = State->GetRegRefs();
342
David Goodwin9f1b2d42009-10-29 19:17:04 +0000343 // Handle dead defs by simulating a last-use of the register just
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000344 // after the def. A dead def can occur because the def is truly
David Goodwin9f1b2d42009-10-29 19:17:04 +0000345 // dead, or because only a subregister is live at the def. If we
346 // don't do this the dead def will be incorrectly merged into the
347 // previous def.
David Goodwinde11f362009-10-26 19:32:42 +0000348 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
349 MachineOperand &MO = MI->getOperand(i);
350 if (!MO.isReg() || !MO.isDef()) continue;
351 unsigned Reg = MO.getReg();
352 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000353
David Goodwindd1c6192009-11-19 23:12:37 +0000354 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwinde11f362009-10-26 19:32:42 +0000355 }
356
David Greene75a2efb2009-12-24 00:14:25 +0000357 DEBUG(dbgs() << "\tDef Groups:");
David Goodwinde11f362009-10-26 19:32:42 +0000358 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
359 MachineOperand &MO = MI->getOperand(i);
360 if (!MO.isReg() || !MO.isDef()) continue;
361 unsigned Reg = MO.getReg();
362 if (Reg == 0) continue;
363
Jim Grosbacheb431da2010-01-06 16:48:02 +0000364 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000365
David Goodwin9f1b2d42009-10-29 19:17:04 +0000366 // If MI's defs have a special allocation requirement, don't allow
David Goodwinde11f362009-10-26 19:32:42 +0000367 // any def registers to be changed. Also assume all registers
368 // defined in a call must not be changed (ABI).
Evan Cheng7f8e5632011-12-07 07:15:52 +0000369 if (MI->isCall() || MI->hasExtraDefRegAllocReq() ||
Evan Chengf128bdc2010-06-16 07:35:02 +0000370 TII->isPredicated(MI)) {
David Greene75a2efb2009-12-24 00:14:25 +0000371 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine056d102009-10-26 22:31:16 +0000372 State->UnionGroups(Reg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000373 }
374
375 // Any aliased that are live at this point are completely or
David Goodwin9f1b2d42009-10-29 19:17:04 +0000376 // partially defined here, so group those aliases with Reg.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000377 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
378 unsigned AliasReg = *AI;
David Goodwine056d102009-10-26 22:31:16 +0000379 if (State->IsLive(AliasReg)) {
380 State->UnionGroups(Reg, AliasReg);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000381 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwinde11f362009-10-26 19:32:42 +0000382 TRI->getName(AliasReg) << ")");
383 }
384 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000385
David Goodwinde11f362009-10-26 19:32:42 +0000386 // Note register reference...
Craig Topperc0196b12014-04-14 00:51:57 +0000387 const TargetRegisterClass *RC = nullptr;
David Goodwinde11f362009-10-26 19:32:42 +0000388 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000389 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwine056d102009-10-26 22:31:16 +0000390 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwinde11f362009-10-26 19:32:42 +0000391 RegRefs.insert(std::make_pair(Reg, RR));
392 }
393
David Greene75a2efb2009-12-24 00:14:25 +0000394 DEBUG(dbgs() << '\n');
David Goodwin9f1b2d42009-10-29 19:17:04 +0000395
396 // Scan the register defs for this instruction and update
397 // live-ranges.
398 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
399 MachineOperand &MO = MI->getOperand(i);
400 if (!MO.isReg() || !MO.isDef()) continue;
401 unsigned Reg = MO.getReg();
402 if (Reg == 0) continue;
David Goodwindd1c6192009-11-19 23:12:37 +0000403 // Ignore KILLs and passthru registers for liveness...
Chris Lattnerb06015a2010-02-09 19:54:29 +0000404 if (MI->isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwindd1c6192009-11-19 23:12:37 +0000405 continue;
David Goodwin9f1b2d42009-10-29 19:17:04 +0000406
David Goodwindd1c6192009-11-19 23:12:37 +0000407 // Update def for Reg and aliases.
Hal Finkel121caf62014-02-26 20:20:30 +0000408 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
409 // We need to be careful here not to define already-live super registers.
410 // If the super register is already live, then this definition is not
411 // a definition of the whole super register (just a partial insertion
412 // into it). Earlier subregister definitions (which we've not yet visited
413 // because we're iterating bottom-up) need to be linked to the same group
414 // as this definition.
415 if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI))
416 continue;
417
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000418 DefIndices[*AI] = Count;
Hal Finkel121caf62014-02-26 20:20:30 +0000419 }
David Goodwin9f1b2d42009-10-29 19:17:04 +0000420 }
David Goodwinde11f362009-10-26 19:32:42 +0000421}
422
423void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000424 unsigned Count) {
David Greene75a2efb2009-12-24 00:14:25 +0000425 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000426 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000427 RegRefs = State->GetRegRefs();
David Goodwinde11f362009-10-26 19:32:42 +0000428
Evan Chengf128bdc2010-06-16 07:35:02 +0000429 // If MI's uses have special allocation requirement, don't allow
430 // any use registers to be changed. Also assume all registers
431 // used in a call must not be changed (ABI).
432 // FIXME: The issue with predicated instruction is more complex. We are being
433 // conservatively here because the kill markers cannot be trusted after
434 // if-conversion:
435 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
436 // ...
437 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
438 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
439 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
440 //
441 // The first R6 kill is not really a kill since it's killed by a predicated
442 // instruction which may not be executed. The second R6 def may or may not
443 // re-define R6 so it's not safe to change it since the last R6 use cannot be
444 // changed.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000445 bool Special = MI->isCall() ||
446 MI->hasExtraSrcRegAllocReq() ||
Evan Chengf128bdc2010-06-16 07:35:02 +0000447 TII->isPredicated(MI);
448
David Goodwinde11f362009-10-26 19:32:42 +0000449 // Scan the register uses for this instruction and update
450 // live-ranges, groups and RegRefs.
451 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
452 MachineOperand &MO = MI->getOperand(i);
453 if (!MO.isReg() || !MO.isUse()) continue;
454 unsigned Reg = MO.getReg();
455 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000456
457 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
458 State->GetGroup(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000459
460 // It wasn't previously live but now it is, this is a kill. Forget
461 // the previous live-range information and start a new live-range
462 // for the register.
David Goodwin9f1b2d42009-10-29 19:17:04 +0000463 HandleLastUse(Reg, Count, "(last-use)");
David Goodwinde11f362009-10-26 19:32:42 +0000464
Evan Chengf128bdc2010-06-16 07:35:02 +0000465 if (Special) {
David Greene75a2efb2009-12-24 00:14:25 +0000466 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine056d102009-10-26 22:31:16 +0000467 State->UnionGroups(Reg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000468 }
469
470 // Note register reference...
Craig Topperc0196b12014-04-14 00:51:57 +0000471 const TargetRegisterClass *RC = nullptr;
David Goodwinde11f362009-10-26 19:32:42 +0000472 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000473 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwine056d102009-10-26 22:31:16 +0000474 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwinde11f362009-10-26 19:32:42 +0000475 RegRefs.insert(std::make_pair(Reg, RR));
476 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000477
David Greene75a2efb2009-12-24 00:14:25 +0000478 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000479
480 // Form a group of all defs and uses of a KILL instruction to ensure
481 // that all registers are renamed as a group.
Chris Lattnerb06015a2010-02-09 19:54:29 +0000482 if (MI->isKill()) {
David Greene75a2efb2009-12-24 00:14:25 +0000483 DEBUG(dbgs() << "\tKill Group:");
David Goodwinde11f362009-10-26 19:32:42 +0000484
485 unsigned FirstReg = 0;
486 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
487 MachineOperand &MO = MI->getOperand(i);
488 if (!MO.isReg()) continue;
489 unsigned Reg = MO.getReg();
490 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000491
David Goodwinde11f362009-10-26 19:32:42 +0000492 if (FirstReg != 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000493 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine056d102009-10-26 22:31:16 +0000494 State->UnionGroups(FirstReg, Reg);
David Goodwinde11f362009-10-26 19:32:42 +0000495 } else {
David Greene75a2efb2009-12-24 00:14:25 +0000496 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000497 FirstReg = Reg;
498 }
499 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000500
David Greene75a2efb2009-12-24 00:14:25 +0000501 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000502 }
503}
504
505BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
506 BitVector BV(TRI->getNumRegs(), false);
507 bool first = true;
508
509 // Check all references that need rewriting for Reg. For each, use
510 // the corresponding register class to narrow the set of registers
511 // that are appropriate for renaming.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000512 for (const auto &Q : make_range(State->GetRegRefs().equal_range(Reg))) {
513 const TargetRegisterClass *RC = Q.second.RC;
Craig Topperc0196b12014-04-14 00:51:57 +0000514 if (!RC) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000515
516 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
517 if (first) {
518 BV |= RCBV;
519 first = false;
520 } else {
521 BV &= RCBV;
522 }
523
Craig Toppercf0444b2014-11-17 05:50:14 +0000524 DEBUG(dbgs() << " " << TRI->getRegClassName(RC));
David Goodwinde11f362009-10-26 19:32:42 +0000525 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000526
David Goodwinde11f362009-10-26 19:32:42 +0000527 return BV;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000528}
David Goodwinde11f362009-10-26 19:32:42 +0000529
530bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin7d8878a2009-11-05 01:19:35 +0000531 unsigned AntiDepGroupIndex,
532 RenameOrderType& RenameOrder,
533 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendling030b0282010-07-15 18:43:09 +0000534 std::vector<unsigned> &KillIndices = State->GetKillIndices();
535 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000536 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000537 RegRefs = State->GetRegRefs();
538
David Goodwinb9fe5d52009-11-13 19:52:48 +0000539 // Collect all referenced registers in the same group as
540 // AntiDepReg. These all need to be renamed together if we are to
541 // break the anti-dependence.
David Goodwinde11f362009-10-26 19:32:42 +0000542 std::vector<unsigned> Regs;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000543 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwinde11f362009-10-26 19:32:42 +0000544 assert(Regs.size() > 0 && "Empty register group!");
545 if (Regs.size() == 0)
546 return false;
547
548 // Find the "superest" register in the group. At the same time,
549 // collect the BitVector of registers that can be used to rename
550 // each register.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000551 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
552 << ":\n");
David Goodwinde11f362009-10-26 19:32:42 +0000553 std::map<unsigned, BitVector> RenameRegisterMap;
554 unsigned SuperReg = 0;
555 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
556 unsigned Reg = Regs[i];
557 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
558 SuperReg = Reg;
559
560 // If Reg has any references, then collect possible rename regs
561 if (RegRefs.count(Reg) > 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000562 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000563
David Goodwinde11f362009-10-26 19:32:42 +0000564 BitVector BV = GetRenameRegisters(Reg);
565 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
566
David Greene75a2efb2009-12-24 00:14:25 +0000567 DEBUG(dbgs() << " ::");
David Goodwinde11f362009-10-26 19:32:42 +0000568 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene75a2efb2009-12-24 00:14:25 +0000569 dbgs() << " " << TRI->getName(r));
570 DEBUG(dbgs() << "\n");
David Goodwinde11f362009-10-26 19:32:42 +0000571 }
572 }
573
574 // All group registers should be a subreg of SuperReg.
575 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
576 unsigned Reg = Regs[i];
577 if (Reg == SuperReg) continue;
578 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
Will Schmidt44ff8f02014-07-31 19:50:53 +0000579 // FIXME: remove this once PR18663 has been properly fixed. For now,
580 // return a conservative answer:
581 // assert(IsSub && "Expecting group subregister");
David Goodwinde11f362009-10-26 19:32:42 +0000582 if (!IsSub)
583 return false;
584 }
585
David Goodwin5305dc02009-11-20 23:33:54 +0000586#ifndef NDEBUG
587 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
588 if (DebugDiv > 0) {
589 static int renamecnt = 0;
590 if (renamecnt++ % DebugDiv != DebugMod)
591 return false;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000592
David Greene75a2efb2009-12-24 00:14:25 +0000593 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin5305dc02009-11-20 23:33:54 +0000594 " for debug ***\n";
595 }
596#endif
597
David Goodwin7d8878a2009-11-05 01:19:35 +0000598 // Check each possible rename register for SuperReg in round-robin
599 // order. If that register is available, and the corresponding
600 // registers are available for the other group subregisters, then we
601 // can use those registers to rename.
Rafael Espindola871c7242010-07-12 02:55:34 +0000602
603 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
604 // check every use of the register and find the largest register class
605 // that can be used in all of them.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000606 const TargetRegisterClass *SuperRC =
Rafael Espindola871c7242010-07-12 02:55:34 +0000607 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000608
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000609 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000610 if (Order.empty()) {
David Greene75a2efb2009-12-24 00:14:25 +0000611 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin7d8878a2009-11-05 01:19:35 +0000612 return false;
613 }
614
David Greene75a2efb2009-12-24 00:14:25 +0000615 DEBUG(dbgs() << "\tFind Registers:");
David Goodwindd1c6192009-11-19 23:12:37 +0000616
Benjamin Kramer2c99e412014-10-10 15:32:50 +0000617 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
David Goodwin7d8878a2009-11-05 01:19:35 +0000618
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000619 unsigned OrigR = RenameOrder[SuperRC];
620 unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
621 unsigned R = OrigR;
David Goodwin7d8878a2009-11-05 01:19:35 +0000622 do {
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000623 if (R == 0) R = Order.size();
David Goodwin7d8878a2009-11-05 01:19:35 +0000624 --R;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000625 const unsigned NewSuperReg = Order[R];
Jim Grosbach944aece2010-09-02 17:12:55 +0000626 // Don't consider non-allocatable registers
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000627 if (!MRI.isAllocatable(NewSuperReg)) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000628 // Don't replace a register with itself.
David Goodwin5305dc02009-11-20 23:33:54 +0000629 if (NewSuperReg == SuperReg) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000630
David Greene75a2efb2009-12-24 00:14:25 +0000631 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin5305dc02009-11-20 23:33:54 +0000632 RenameMap.clear();
633
634 // For each referenced group register (which must be a SuperReg or
635 // a subregister of SuperReg), find the corresponding subregister
636 // of NewSuperReg and make sure it is free to be renamed.
637 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
638 unsigned Reg = Regs[i];
639 unsigned NewReg = 0;
640 if (Reg == SuperReg) {
641 NewReg = NewSuperReg;
642 } else {
643 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
644 if (NewSubRegIdx != 0)
645 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwinde11f362009-10-26 19:32:42 +0000646 }
David Goodwin5305dc02009-11-20 23:33:54 +0000647
David Greene75a2efb2009-12-24 00:14:25 +0000648 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbacheb431da2010-01-06 16:48:02 +0000649
David Goodwin5305dc02009-11-20 23:33:54 +0000650 // Check if Reg can be renamed to NewReg.
651 BitVector BV = RenameRegisterMap[Reg];
652 if (!BV.test(NewReg)) {
David Greene75a2efb2009-12-24 00:14:25 +0000653 DEBUG(dbgs() << "(no rename)");
David Goodwin5305dc02009-11-20 23:33:54 +0000654 goto next_super_reg;
655 }
656
657 // If NewReg is dead and NewReg's most recent def is not before
658 // Regs's kill, it's safe to replace Reg with NewReg. We
659 // must also check all aliases of NewReg, because we can't define a
660 // register when any sub or super is already live.
661 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene75a2efb2009-12-24 00:14:25 +0000662 DEBUG(dbgs() << "(live)");
David Goodwin5305dc02009-11-20 23:33:54 +0000663 goto next_super_reg;
664 } else {
665 bool found = false;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000666 for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
667 unsigned AliasReg = *AI;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000668 if (State->IsLive(AliasReg) ||
669 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene75a2efb2009-12-24 00:14:25 +0000670 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin5305dc02009-11-20 23:33:54 +0000671 found = true;
672 break;
673 }
674 }
675 if (found)
676 goto next_super_reg;
677 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000678
Hal Finkelc8cf2b82014-12-09 01:00:59 +0000679 // We cannot rename 'Reg' to 'NewReg' if one of the uses of 'Reg' also
680 // defines 'NewReg' via an early-clobber operand.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000681 for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
682 MachineInstr *UseMI = Q.second.Operand->getParent();
Hal Finkelc8cf2b82014-12-09 01:00:59 +0000683 int Idx = UseMI->findRegisterDefOperandIdx(NewReg, false, true, TRI);
684 if (Idx == -1)
685 continue;
686
687 if (UseMI->getOperand(Idx).isEarlyClobber()) {
688 DEBUG(dbgs() << "(ec)");
689 goto next_super_reg;
690 }
691 }
692
David Goodwin5305dc02009-11-20 23:33:54 +0000693 // Record that 'Reg' can be renamed to 'NewReg'.
694 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwinde11f362009-10-26 19:32:42 +0000695 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000696
David Goodwin5305dc02009-11-20 23:33:54 +0000697 // If we fall-out here, then every register in the group can be
698 // renamed, as recorded in RenameMap.
699 RenameOrder.erase(SuperRC);
700 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene75a2efb2009-12-24 00:14:25 +0000701 DEBUG(dbgs() << "]\n");
David Goodwin5305dc02009-11-20 23:33:54 +0000702 return true;
703
704 next_super_reg:
David Greene75a2efb2009-12-24 00:14:25 +0000705 DEBUG(dbgs() << ']');
David Goodwin7d8878a2009-11-05 01:19:35 +0000706 } while (R != EndR);
David Goodwinde11f362009-10-26 19:32:42 +0000707
David Greene75a2efb2009-12-24 00:14:25 +0000708 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000709
710 // No registers are free and available!
711 return false;
712}
713
714/// BreakAntiDependencies - Identifiy anti-dependencies within the
715/// ScheduleDAG and break them by renaming registers.
716///
David Goodwine056d102009-10-26 22:31:16 +0000717unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman35bc4d42010-04-19 23:11:58 +0000718 const std::vector<SUnit>& SUnits,
719 MachineBasicBlock::iterator Begin,
720 MachineBasicBlock::iterator End,
Devang Patelf02a3762011-06-02 21:26:52 +0000721 unsigned InsertPosIndex,
722 DbgValueVector &DbgValues) {
723
Bill Wendling030b0282010-07-15 18:43:09 +0000724 std::vector<unsigned> &KillIndices = State->GetKillIndices();
725 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000726 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000727 RegRefs = State->GetRegRefs();
728
David Goodwinde11f362009-10-26 19:32:42 +0000729 // The code below assumes that there is at least one instruction,
730 // so just duck out immediately if the block is empty.
David Goodwin8501dbbe2009-11-03 20:57:50 +0000731 if (SUnits.empty()) return 0;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000732
David Goodwin7d8878a2009-11-05 01:19:35 +0000733 // For each regclass the next register to use for renaming.
734 RenameOrderType RenameOrder;
David Goodwinde11f362009-10-26 19:32:42 +0000735
736 // ...need a map from MI to SUnit.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000737 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwinde11f362009-10-26 19:32:42 +0000738 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000739 const SUnit *SU = &SUnits[i];
740 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
741 SU));
David Goodwinde11f362009-10-26 19:32:42 +0000742 }
743
David Goodwinb9fe5d52009-11-13 19:52:48 +0000744 // Track progress along the critical path through the SUnit graph as
745 // we walk the instructions. This is needed for regclasses that only
746 // break critical-path anti-dependencies.
Craig Topperc0196b12014-04-14 00:51:57 +0000747 const SUnit *CriticalPathSU = nullptr;
748 MachineInstr *CriticalPathMI = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000749 if (CriticalPathSet.any()) {
750 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000751 const SUnit *SU = &SUnits[i];
Jim Grosbacheb431da2010-01-06 16:48:02 +0000752 if (!CriticalPathSU ||
753 ((SU->getDepth() + SU->Latency) >
David Goodwinb9fe5d52009-11-13 19:52:48 +0000754 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
755 CriticalPathSU = SU;
756 }
757 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000758
David Goodwinb9fe5d52009-11-13 19:52:48 +0000759 CriticalPathMI = CriticalPathSU->getInstr();
760 }
761
Jim Grosbacheb431da2010-01-06 16:48:02 +0000762#ifndef NDEBUG
David Greene75a2efb2009-12-24 00:14:25 +0000763 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
764 DEBUG(dbgs() << "Available regs:");
David Goodwin80a03cc2009-11-20 19:32:48 +0000765 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
766 if (!State->IsLive(Reg))
David Greene75a2efb2009-12-24 00:14:25 +0000767 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000768 }
David Greene75a2efb2009-12-24 00:14:25 +0000769 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000770#endif
771
772 // Attempt to break anti-dependence edges. Walk the instructions
773 // from the bottom up, tracking information about liveness as we go
774 // to help determine which registers are available.
775 unsigned Broken = 0;
776 unsigned Count = InsertPosIndex - 1;
777 for (MachineBasicBlock::iterator I = End, E = Begin;
778 I != E; --Count) {
779 MachineInstr *MI = --I;
780
Hal Finkel8606e3c2012-01-16 22:53:41 +0000781 if (MI->isDebugValue())
782 continue;
783
David Greene75a2efb2009-12-24 00:14:25 +0000784 DEBUG(dbgs() << "Anti: ");
David Goodwinde11f362009-10-26 19:32:42 +0000785 DEBUG(MI->dump());
786
787 std::set<unsigned> PassthruRegs;
788 GetPassthruRegs(MI, PassthruRegs);
789
790 // Process the defs in MI...
791 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000792
David Goodwin80a03cc2009-11-20 19:32:48 +0000793 // The dependence edges that represent anti- and output-
David Goodwinb9fe5d52009-11-13 19:52:48 +0000794 // dependencies that are candidates for breaking.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000795 std::vector<const SDep *> Edges;
796 const SUnit *PathSU = MISUnitMap[MI];
David Goodwin80a03cc2009-11-20 19:32:48 +0000797 AntiDepEdges(PathSU, Edges);
David Goodwinb9fe5d52009-11-13 19:52:48 +0000798
799 // If MI is not on the critical path, then we don't rename
800 // registers in the CriticalPathSet.
Craig Topperc0196b12014-04-14 00:51:57 +0000801 BitVector *ExcludeRegs = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000802 if (MI == CriticalPathMI) {
803 CriticalPathSU = CriticalPathStep(CriticalPathSU);
Craig Topperc0196b12014-04-14 00:51:57 +0000804 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr;
Hal Finkel6f1ff8e2013-09-12 04:22:31 +0000805 } else if (CriticalPathSet.any()) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000806 ExcludeRegs = &CriticalPathSet;
807 }
808
David Goodwinde11f362009-10-26 19:32:42 +0000809 // Ignore KILL instructions (they form a group in ScanInstruction
810 // but don't cause any anti-dependence breaking themselves)
Chris Lattnerb06015a2010-02-09 19:54:29 +0000811 if (!MI->isKill()) {
David Goodwinde11f362009-10-26 19:32:42 +0000812 // Attempt to break each anti-dependency...
813 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000814 const SDep *Edge = Edges[i];
David Goodwinde11f362009-10-26 19:32:42 +0000815 SUnit *NextSU = Edge->getSUnit();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000816
David Goodwinda83f7d2009-11-12 19:08:21 +0000817 if ((Edge->getKind() != SDep::Anti) &&
818 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000819
David Goodwinde11f362009-10-26 19:32:42 +0000820 unsigned AntiDepReg = Edge->getReg();
David Greene75a2efb2009-12-24 00:14:25 +0000821 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwinde11f362009-10-26 19:32:42 +0000822 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000823
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000824 if (!MRI.isAllocatable(AntiDepReg)) {
David Goodwinde11f362009-10-26 19:32:42 +0000825 // Don't break anti-dependencies on non-allocatable registers.
David Greene75a2efb2009-12-24 00:14:25 +0000826 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000827 continue;
Craig Topperc0196b12014-04-14 00:51:57 +0000828 } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000829 // Don't break anti-dependencies for critical path registers
830 // if not on the critical path
David Greene75a2efb2009-12-24 00:14:25 +0000831 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwinb9fe5d52009-11-13 19:52:48 +0000832 continue;
David Goodwinde11f362009-10-26 19:32:42 +0000833 } else if (PassthruRegs.count(AntiDepReg) != 0) {
834 // If the anti-dep register liveness "passes-thru", then
835 // don't try to change it. It will be changed along with
836 // the use if required to break an earlier antidep.
David Greene75a2efb2009-12-24 00:14:25 +0000837 DEBUG(dbgs() << " (passthru)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000838 continue;
839 } else {
840 // No anti-dep breaking for implicit deps
841 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
Craig Topperc0196b12014-04-14 00:51:57 +0000842 assert(AntiDepOp && "Can't find index for defined register operand");
843 if (!AntiDepOp || AntiDepOp->isImplicit()) {
David Greene75a2efb2009-12-24 00:14:25 +0000844 DEBUG(dbgs() << " (implicit)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000845 continue;
846 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000847
David Goodwinde11f362009-10-26 19:32:42 +0000848 // If the SUnit has other dependencies on the SUnit that
849 // it anti-depends on, don't bother breaking the
850 // anti-dependency since those edges would prevent such
851 // units from being scheduled past each other
852 // regardless.
David Goodwin80a03cc2009-11-20 19:32:48 +0000853 //
854 // Also, if there are dependencies on other SUnits with the
855 // same register as the anti-dependency, don't attempt to
856 // break it.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000857 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwinde11f362009-10-26 19:32:42 +0000858 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000859 if (P->getSUnit() == NextSU ?
860 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
861 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
862 AntiDepReg = 0;
863 break;
864 }
865 }
Dan Gohman35bc4d42010-04-19 23:11:58 +0000866 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin80a03cc2009-11-20 19:32:48 +0000867 PE = PathSU->Preds.end(); P != PE; ++P) {
868 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
869 (P->getKind() != SDep::Output)) {
David Greene75a2efb2009-12-24 00:14:25 +0000870 DEBUG(dbgs() << " (real dependency)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000871 AntiDepReg = 0;
872 break;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000873 } else if ((P->getSUnit() != NextSU) &&
874 (P->getKind() == SDep::Data) &&
David Goodwin80a03cc2009-11-20 19:32:48 +0000875 (P->getReg() == AntiDepReg)) {
David Greene75a2efb2009-12-24 00:14:25 +0000876 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin80a03cc2009-11-20 19:32:48 +0000877 AntiDepReg = 0;
878 break;
David Goodwinde11f362009-10-26 19:32:42 +0000879 }
880 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000881
David Goodwinde11f362009-10-26 19:32:42 +0000882 if (AntiDepReg == 0) continue;
883 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000884
David Goodwinde11f362009-10-26 19:32:42 +0000885 assert(AntiDepReg != 0);
886 if (AntiDepReg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000887
David Goodwinde11f362009-10-26 19:32:42 +0000888 // Determine AntiDepReg's register group.
David Goodwine056d102009-10-26 22:31:16 +0000889 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwinde11f362009-10-26 19:32:42 +0000890 if (GroupIndex == 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000891 DEBUG(dbgs() << " (zero group)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000892 continue;
893 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000894
David Greene75a2efb2009-12-24 00:14:25 +0000895 DEBUG(dbgs() << '\n');
Jim Grosbacheb431da2010-01-06 16:48:02 +0000896
David Goodwinde11f362009-10-26 19:32:42 +0000897 // Look for a suitable register to use to break the anti-dependence.
898 std::map<unsigned, unsigned> RenameMap;
David Goodwin7d8878a2009-11-05 01:19:35 +0000899 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene75a2efb2009-12-24 00:14:25 +0000900 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwinde11f362009-10-26 19:32:42 +0000901 << TRI->getName(AntiDepReg) << ":");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000902
David Goodwinde11f362009-10-26 19:32:42 +0000903 // Handle each group register...
904 for (std::map<unsigned, unsigned>::iterator
905 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
906 unsigned CurrReg = S->first;
907 unsigned NewReg = S->second;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000908
909 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
910 TRI->getName(NewReg) << "(" <<
David Goodwinde11f362009-10-26 19:32:42 +0000911 RegRefs.count(CurrReg) << " refs)");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000912
David Goodwinde11f362009-10-26 19:32:42 +0000913 // Update the references to the old register CurrReg to
914 // refer to the new register NewReg.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000915 for (const auto &Q : make_range(RegRefs.equal_range(CurrReg))) {
916 Q.second.Operand->setReg(NewReg);
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000917 // If the SU for the instruction being updated has debug
918 // information related to the anti-dependency register, make
919 // sure to update that as well.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000920 const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()];
Jim Grosbach84854832010-06-02 15:29:36 +0000921 if (!SU) continue;
Devang Patelf02a3762011-06-02 21:26:52 +0000922 for (DbgValueVector::iterator DVI = DbgValues.begin(),
923 DVE = DbgValues.end(); DVI != DVE; ++DVI)
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000924 if (DVI->second == Q.second.Operand->getParent())
Devang Patelf02a3762011-06-02 21:26:52 +0000925 UpdateDbgValue(DVI->first, AntiDepReg, NewReg);
David Goodwinde11f362009-10-26 19:32:42 +0000926 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000927
David Goodwinde11f362009-10-26 19:32:42 +0000928 // We just went back in time and modified history; the
929 // liveness information for CurrReg is now inconsistent. Set
930 // the state as if it were dead.
David Goodwine056d102009-10-26 22:31:16 +0000931 State->UnionGroups(NewReg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000932 RegRefs.erase(NewReg);
933 DefIndices[NewReg] = DefIndices[CurrReg];
934 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbacheb431da2010-01-06 16:48:02 +0000935
David Goodwine056d102009-10-26 22:31:16 +0000936 State->UnionGroups(CurrReg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000937 RegRefs.erase(CurrReg);
938 DefIndices[CurrReg] = KillIndices[CurrReg];
939 KillIndices[CurrReg] = ~0u;
940 assert(((KillIndices[CurrReg] == ~0u) !=
941 (DefIndices[CurrReg] == ~0u)) &&
942 "Kill and Def maps aren't consistent for AntiDepReg!");
943 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000944
David Goodwinde11f362009-10-26 19:32:42 +0000945 ++Broken;
David Greene75a2efb2009-12-24 00:14:25 +0000946 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000947 }
948 }
949 }
950
951 ScanInstruction(MI, Count);
952 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000953
David Goodwinde11f362009-10-26 19:32:42 +0000954 return Broken;
955}