blob: 845408f15f5c052faa8824937ba80c60dde8b3c0 [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.
164 const MachineFrameInfo *MFI = MF.getFrameInfo();
Matthias Braun111f5d82015-05-28 23:20:35 +0000165 BitVector Pristine = MFI->getPristineRegs(MF);
Craig Topper840beec2014-04-04 05:16:06 +0000166 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
David Goodwine056d102009-10-26 22:31:16 +0000167 unsigned Reg = *I;
168 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000169 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
170 unsigned AliasReg = *AI;
David Goodwine056d102009-10-26 22:31:16 +0000171 State->UnionGroups(AliasReg, 0);
172 KillIndices[AliasReg] = BB->size();
173 DefIndices[AliasReg] = ~0u;
174 }
175 }
176}
177
178void AggressiveAntiDepBreaker::FinishBlock() {
179 delete State;
Craig Topperc0196b12014-04-14 00:51:57 +0000180 State = nullptr;
David Goodwine056d102009-10-26 22:31:16 +0000181}
182
183void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000184 unsigned InsertPosIndex) {
David Goodwine056d102009-10-26 22:31:16 +0000185 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
186
David Goodwinfaa76602009-10-29 23:30:59 +0000187 std::set<unsigned> PassthruRegs;
188 GetPassthruRegs(MI, PassthruRegs);
189 PrescanInstruction(MI, Count, PassthruRegs);
190 ScanInstruction(MI, Count);
191
David Greene75a2efb2009-12-24 00:14:25 +0000192 DEBUG(dbgs() << "Observe: ");
David Goodwine056d102009-10-26 22:31:16 +0000193 DEBUG(MI->dump());
David Greene75a2efb2009-12-24 00:14:25 +0000194 DEBUG(dbgs() << "\tRegs:");
David Goodwine056d102009-10-26 22:31:16 +0000195
Bill Wendling030b0282010-07-15 18:43:09 +0000196 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwina45fe672009-12-09 17:18:22 +0000197 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine056d102009-10-26 22:31:16 +0000198 // If Reg is current live, then mark that it can't be renamed as
199 // we don't know the extent of its live-range anymore (now that it
200 // has been scheduled). If it is not live but was defined in the
201 // previous schedule region, then set its def index to the most
202 // conservative location (i.e. the beginning of the previous
203 // schedule region).
204 if (State->IsLive(Reg)) {
205 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbacheb431da2010-01-06 16:48:02 +0000206 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine056d102009-10-26 22:31:16 +0000207 State->GetGroup(Reg) << "->g0(region live-out)");
208 State->UnionGroups(Reg, 0);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000209 } else if ((DefIndices[Reg] < InsertPosIndex)
210 && (DefIndices[Reg] >= Count)) {
David Goodwine056d102009-10-26 22:31:16 +0000211 DefIndices[Reg] = Count;
212 }
213 }
David Greene75a2efb2009-12-24 00:14:25 +0000214 DEBUG(dbgs() << '\n');
David Goodwine056d102009-10-26 22:31:16 +0000215}
216
David Goodwinde11f362009-10-26 19:32:42 +0000217bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000218 MachineOperand& MO)
David Goodwinde11f362009-10-26 19:32:42 +0000219{
220 if (!MO.isReg() || !MO.isImplicit())
221 return false;
222
223 unsigned Reg = MO.getReg();
224 if (Reg == 0)
225 return false;
226
Chad Rosier47eba052015-10-09 19:48:48 +0000227 MachineOperand *Op = nullptr;
228 if (MO.isDef())
229 Op = MI->findRegisterUseOperand(Reg, true);
230 else
231 Op = MI->findRegisterDefOperand(Reg);
232
Craig Topperc0196b12014-04-14 00:51:57 +0000233 return(Op && Op->isImplicit());
David Goodwinde11f362009-10-26 19:32:42 +0000234}
235
236void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
237 std::set<unsigned>& PassthruRegs) {
238 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
239 MachineOperand &MO = MI->getOperand(i);
240 if (!MO.isReg()) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000241 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
David Goodwinde11f362009-10-26 19:32:42 +0000242 IsImplicitDefUse(MI, MO)) {
243 const unsigned Reg = MO.getReg();
Chad Rosierabdb1d62013-05-22 23:17:36 +0000244 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
245 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000246 PassthruRegs.insert(*SubRegs);
David Goodwinde11f362009-10-26 19:32:42 +0000247 }
248 }
249}
250
David Goodwin80a03cc2009-11-20 19:32:48 +0000251/// AntiDepEdges - Return in Edges the anti- and output- dependencies
252/// in SU that we want to consider for breaking.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000253static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000254 SmallSet<unsigned, 4> RegSet;
Dan Gohman35bc4d42010-04-19 23:11:58 +0000255 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwinde11f362009-10-26 19:32:42 +0000256 P != PE; ++P) {
David Goodwinda83f7d2009-11-12 19:08:21 +0000257 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Blaikie70573dc2014-11-19 07:49:26 +0000258 if (RegSet.insert(P->getReg()).second)
David Goodwinde11f362009-10-26 19:32:42 +0000259 Edges.push_back(&*P);
David Goodwinde11f362009-10-26 19:32:42 +0000260 }
261 }
262}
263
David Goodwinb9fe5d52009-11-13 19:52:48 +0000264/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
265/// critical path.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000266static const SUnit *CriticalPathStep(const SUnit *SU) {
Craig Topperc0196b12014-04-14 00:51:57 +0000267 const SDep *Next = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000268 unsigned NextDepth = 0;
269 // Find the predecessor edge with the greatest depth.
Craig Topperc0196b12014-04-14 00:51:57 +0000270 if (SU) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000271 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwinb9fe5d52009-11-13 19:52:48 +0000272 P != PE; ++P) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000273 const SUnit *PredSU = P->getSUnit();
David Goodwinb9fe5d52009-11-13 19:52:48 +0000274 unsigned PredLatency = P->getLatency();
275 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
276 // In the case of a latency tie, prefer an anti-dependency edge over
277 // other types of edges.
278 if (NextDepth < PredTotalLatency ||
279 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
280 NextDepth = PredTotalLatency;
281 Next = &*P;
282 }
283 }
284 }
285
Craig Topperc0196b12014-04-14 00:51:57 +0000286 return (Next) ? Next->getSUnit() : nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000287}
288
David Goodwin9f1b2d42009-10-29 19:17:04 +0000289void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbacheb431da2010-01-06 16:48:02 +0000290 const char *tag,
291 const char *header,
David Goodwindd1c6192009-11-19 23:12:37 +0000292 const char *footer) {
Bill Wendling030b0282010-07-15 18:43:09 +0000293 std::vector<unsigned> &KillIndices = State->GetKillIndices();
294 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000295 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin9f1b2d42009-10-29 19:17:04 +0000296 RegRefs = State->GetRegRefs();
297
Hal Finkel34c94d52015-01-28 14:44:14 +0000298 // FIXME: We must leave subregisters of live super registers as live, so that
299 // we don't clear out the register tracking information for subregisters of
300 // super registers we're still tracking (and with which we're unioning
301 // subregister definitions).
302 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
303 if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) {
304 DEBUG(if (!header && footer) dbgs() << footer);
305 return;
306 }
307
David Goodwin9f1b2d42009-10-29 19:17:04 +0000308 if (!State->IsLive(Reg)) {
309 KillIndices[Reg] = KillIdx;
310 DefIndices[Reg] = ~0u;
311 RegRefs.erase(Reg);
312 State->LeaveGroup(Reg);
Craig Topperc0196b12014-04-14 00:51:57 +0000313 DEBUG(if (header) {
314 dbgs() << header << TRI->getName(Reg); header = nullptr; });
David Greene75a2efb2009-12-24 00:14:25 +0000315 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
David Goodwin9f1b2d42009-10-29 19:17:04 +0000316 }
317 // Repeat for subregisters.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000318 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
319 unsigned SubregReg = *SubRegs;
David Goodwin9f1b2d42009-10-29 19:17:04 +0000320 if (!State->IsLive(SubregReg)) {
321 KillIndices[SubregReg] = KillIdx;
322 DefIndices[SubregReg] = ~0u;
323 RegRefs.erase(SubregReg);
324 State->LeaveGroup(SubregReg);
Craig Topperc0196b12014-04-14 00:51:57 +0000325 DEBUG(if (header) {
326 dbgs() << header << TRI->getName(Reg); header = nullptr; });
David Greene75a2efb2009-12-24 00:14:25 +0000327 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin9f1b2d42009-10-29 19:17:04 +0000328 State->GetGroup(SubregReg) << tag);
329 }
330 }
David Goodwindd1c6192009-11-19 23:12:37 +0000331
Craig Topperc0196b12014-04-14 00:51:57 +0000332 DEBUG(if (!header && footer) dbgs() << footer);
David Goodwin9f1b2d42009-10-29 19:17:04 +0000333}
334
Jim Grosbacheb431da2010-01-06 16:48:02 +0000335void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
336 unsigned Count,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000337 std::set<unsigned>& PassthruRegs) {
Bill Wendling030b0282010-07-15 18:43:09 +0000338 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000339 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000340 RegRefs = State->GetRegRefs();
341
David Goodwin9f1b2d42009-10-29 19:17:04 +0000342 // Handle dead defs by simulating a last-use of the register just
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000343 // after the def. A dead def can occur because the def is truly
David Goodwin9f1b2d42009-10-29 19:17:04 +0000344 // dead, or because only a subregister is live at the def. If we
345 // don't do this the dead def will be incorrectly merged into the
346 // previous def.
David Goodwinde11f362009-10-26 19:32:42 +0000347 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
348 MachineOperand &MO = MI->getOperand(i);
349 if (!MO.isReg() || !MO.isDef()) continue;
350 unsigned Reg = MO.getReg();
351 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000352
David Goodwindd1c6192009-11-19 23:12:37 +0000353 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwinde11f362009-10-26 19:32:42 +0000354 }
355
David Greene75a2efb2009-12-24 00:14:25 +0000356 DEBUG(dbgs() << "\tDef Groups:");
David Goodwinde11f362009-10-26 19:32:42 +0000357 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
358 MachineOperand &MO = MI->getOperand(i);
359 if (!MO.isReg() || !MO.isDef()) continue;
360 unsigned Reg = MO.getReg();
361 if (Reg == 0) continue;
362
Jim Grosbacheb431da2010-01-06 16:48:02 +0000363 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000364
David Goodwin9f1b2d42009-10-29 19:17:04 +0000365 // If MI's defs have a special allocation requirement, don't allow
David Goodwinde11f362009-10-26 19:32:42 +0000366 // any def registers to be changed. Also assume all registers
367 // defined in a call must not be changed (ABI).
Evan Cheng7f8e5632011-12-07 07:15:52 +0000368 if (MI->isCall() || MI->hasExtraDefRegAllocReq() ||
Evan Chengf128bdc2010-06-16 07:35:02 +0000369 TII->isPredicated(MI)) {
David Greene75a2efb2009-12-24 00:14:25 +0000370 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine056d102009-10-26 22:31:16 +0000371 State->UnionGroups(Reg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000372 }
373
374 // Any aliased that are live at this point are completely or
David Goodwin9f1b2d42009-10-29 19:17:04 +0000375 // partially defined here, so group those aliases with Reg.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000376 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
377 unsigned AliasReg = *AI;
David Goodwine056d102009-10-26 22:31:16 +0000378 if (State->IsLive(AliasReg)) {
379 State->UnionGroups(Reg, AliasReg);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000380 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwinde11f362009-10-26 19:32:42 +0000381 TRI->getName(AliasReg) << ")");
382 }
383 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000384
David Goodwinde11f362009-10-26 19:32:42 +0000385 // Note register reference...
Craig Topperc0196b12014-04-14 00:51:57 +0000386 const TargetRegisterClass *RC = nullptr;
David Goodwinde11f362009-10-26 19:32:42 +0000387 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000388 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwine056d102009-10-26 22:31:16 +0000389 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwinde11f362009-10-26 19:32:42 +0000390 RegRefs.insert(std::make_pair(Reg, RR));
391 }
392
David Greene75a2efb2009-12-24 00:14:25 +0000393 DEBUG(dbgs() << '\n');
David Goodwin9f1b2d42009-10-29 19:17:04 +0000394
395 // Scan the register defs for this instruction and update
396 // live-ranges.
397 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
398 MachineOperand &MO = MI->getOperand(i);
399 if (!MO.isReg() || !MO.isDef()) continue;
400 unsigned Reg = MO.getReg();
401 if (Reg == 0) continue;
David Goodwindd1c6192009-11-19 23:12:37 +0000402 // Ignore KILLs and passthru registers for liveness...
Chris Lattnerb06015a2010-02-09 19:54:29 +0000403 if (MI->isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwindd1c6192009-11-19 23:12:37 +0000404 continue;
David Goodwin9f1b2d42009-10-29 19:17:04 +0000405
David Goodwindd1c6192009-11-19 23:12:37 +0000406 // Update def for Reg and aliases.
Hal Finkel121caf62014-02-26 20:20:30 +0000407 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
408 // We need to be careful here not to define already-live super registers.
409 // If the super register is already live, then this definition is not
410 // a definition of the whole super register (just a partial insertion
411 // into it). Earlier subregister definitions (which we've not yet visited
412 // because we're iterating bottom-up) need to be linked to the same group
413 // as this definition.
414 if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI))
415 continue;
416
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000417 DefIndices[*AI] = Count;
Hal Finkel121caf62014-02-26 20:20:30 +0000418 }
David Goodwin9f1b2d42009-10-29 19:17:04 +0000419 }
David Goodwinde11f362009-10-26 19:32:42 +0000420}
421
422void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000423 unsigned Count) {
David Greene75a2efb2009-12-24 00:14:25 +0000424 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000425 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000426 RegRefs = State->GetRegRefs();
David Goodwinde11f362009-10-26 19:32:42 +0000427
Evan Chengf128bdc2010-06-16 07:35:02 +0000428 // If MI's uses have special allocation requirement, don't allow
429 // any use registers to be changed. Also assume all registers
430 // used in a call must not be changed (ABI).
431 // FIXME: The issue with predicated instruction is more complex. We are being
432 // conservatively here because the kill markers cannot be trusted after
433 // if-conversion:
434 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
435 // ...
436 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
437 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
438 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
439 //
440 // The first R6 kill is not really a kill since it's killed by a predicated
441 // instruction which may not be executed. The second R6 def may or may not
442 // re-define R6 so it's not safe to change it since the last R6 use cannot be
443 // changed.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000444 bool Special = MI->isCall() ||
445 MI->hasExtraSrcRegAllocReq() ||
Evan Chengf128bdc2010-06-16 07:35:02 +0000446 TII->isPredicated(MI);
447
David Goodwinde11f362009-10-26 19:32:42 +0000448 // Scan the register uses for this instruction and update
449 // live-ranges, groups and RegRefs.
450 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
451 MachineOperand &MO = MI->getOperand(i);
452 if (!MO.isReg() || !MO.isUse()) continue;
453 unsigned Reg = MO.getReg();
454 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000455
456 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
457 State->GetGroup(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000458
459 // It wasn't previously live but now it is, this is a kill. Forget
460 // the previous live-range information and start a new live-range
461 // for the register.
David Goodwin9f1b2d42009-10-29 19:17:04 +0000462 HandleLastUse(Reg, Count, "(last-use)");
David Goodwinde11f362009-10-26 19:32:42 +0000463
Evan Chengf128bdc2010-06-16 07:35:02 +0000464 if (Special) {
David Greene75a2efb2009-12-24 00:14:25 +0000465 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine056d102009-10-26 22:31:16 +0000466 State->UnionGroups(Reg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000467 }
468
469 // Note register reference...
Craig Topperc0196b12014-04-14 00:51:57 +0000470 const TargetRegisterClass *RC = nullptr;
David Goodwinde11f362009-10-26 19:32:42 +0000471 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000472 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwine056d102009-10-26 22:31:16 +0000473 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwinde11f362009-10-26 19:32:42 +0000474 RegRefs.insert(std::make_pair(Reg, RR));
475 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000476
David Greene75a2efb2009-12-24 00:14:25 +0000477 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000478
479 // Form a group of all defs and uses of a KILL instruction to ensure
480 // that all registers are renamed as a group.
Chris Lattnerb06015a2010-02-09 19:54:29 +0000481 if (MI->isKill()) {
David Greene75a2efb2009-12-24 00:14:25 +0000482 DEBUG(dbgs() << "\tKill Group:");
David Goodwinde11f362009-10-26 19:32:42 +0000483
484 unsigned FirstReg = 0;
485 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
486 MachineOperand &MO = MI->getOperand(i);
487 if (!MO.isReg()) continue;
488 unsigned Reg = MO.getReg();
489 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000490
David Goodwinde11f362009-10-26 19:32:42 +0000491 if (FirstReg != 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000492 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine056d102009-10-26 22:31:16 +0000493 State->UnionGroups(FirstReg, Reg);
David Goodwinde11f362009-10-26 19:32:42 +0000494 } else {
David Greene75a2efb2009-12-24 00:14:25 +0000495 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000496 FirstReg = Reg;
497 }
498 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000499
David Greene75a2efb2009-12-24 00:14:25 +0000500 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000501 }
502}
503
504BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
505 BitVector BV(TRI->getNumRegs(), false);
506 bool first = true;
507
508 // Check all references that need rewriting for Reg. For each, use
509 // the corresponding register class to narrow the set of registers
510 // that are appropriate for renaming.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000511 for (const auto &Q : make_range(State->GetRegRefs().equal_range(Reg))) {
512 const TargetRegisterClass *RC = Q.second.RC;
Craig Topperc0196b12014-04-14 00:51:57 +0000513 if (!RC) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000514
515 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
516 if (first) {
517 BV |= RCBV;
518 first = false;
519 } else {
520 BV &= RCBV;
521 }
522
Craig Toppercf0444b2014-11-17 05:50:14 +0000523 DEBUG(dbgs() << " " << TRI->getRegClassName(RC));
David Goodwinde11f362009-10-26 19:32:42 +0000524 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000525
David Goodwinde11f362009-10-26 19:32:42 +0000526 return BV;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000527}
David Goodwinde11f362009-10-26 19:32:42 +0000528
529bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin7d8878a2009-11-05 01:19:35 +0000530 unsigned AntiDepGroupIndex,
531 RenameOrderType& RenameOrder,
532 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendling030b0282010-07-15 18:43:09 +0000533 std::vector<unsigned> &KillIndices = State->GetKillIndices();
534 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000535 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000536 RegRefs = State->GetRegRefs();
537
David Goodwinb9fe5d52009-11-13 19:52:48 +0000538 // Collect all referenced registers in the same group as
539 // AntiDepReg. These all need to be renamed together if we are to
540 // break the anti-dependence.
David Goodwinde11f362009-10-26 19:32:42 +0000541 std::vector<unsigned> Regs;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000542 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwinde11f362009-10-26 19:32:42 +0000543 assert(Regs.size() > 0 && "Empty register group!");
544 if (Regs.size() == 0)
545 return false;
546
547 // Find the "superest" register in the group. At the same time,
548 // collect the BitVector of registers that can be used to rename
549 // each register.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000550 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
551 << ":\n");
David Goodwinde11f362009-10-26 19:32:42 +0000552 std::map<unsigned, BitVector> RenameRegisterMap;
553 unsigned SuperReg = 0;
554 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
555 unsigned Reg = Regs[i];
556 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
557 SuperReg = Reg;
558
559 // If Reg has any references, then collect possible rename regs
560 if (RegRefs.count(Reg) > 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000561 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000562
David Goodwinde11f362009-10-26 19:32:42 +0000563 BitVector BV = GetRenameRegisters(Reg);
564 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
565
David Greene75a2efb2009-12-24 00:14:25 +0000566 DEBUG(dbgs() << " ::");
David Goodwinde11f362009-10-26 19:32:42 +0000567 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene75a2efb2009-12-24 00:14:25 +0000568 dbgs() << " " << TRI->getName(r));
569 DEBUG(dbgs() << "\n");
David Goodwinde11f362009-10-26 19:32:42 +0000570 }
571 }
572
573 // All group registers should be a subreg of SuperReg.
574 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
575 unsigned Reg = Regs[i];
576 if (Reg == SuperReg) continue;
577 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
Will Schmidt44ff8f02014-07-31 19:50:53 +0000578 // FIXME: remove this once PR18663 has been properly fixed. For now,
579 // return a conservative answer:
580 // assert(IsSub && "Expecting group subregister");
David Goodwinde11f362009-10-26 19:32:42 +0000581 if (!IsSub)
582 return false;
583 }
584
David Goodwin5305dc02009-11-20 23:33:54 +0000585#ifndef NDEBUG
586 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
587 if (DebugDiv > 0) {
588 static int renamecnt = 0;
589 if (renamecnt++ % DebugDiv != DebugMod)
590 return false;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000591
David Greene75a2efb2009-12-24 00:14:25 +0000592 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin5305dc02009-11-20 23:33:54 +0000593 " for debug ***\n";
594 }
595#endif
596
David Goodwin7d8878a2009-11-05 01:19:35 +0000597 // Check each possible rename register for SuperReg in round-robin
598 // order. If that register is available, and the corresponding
599 // registers are available for the other group subregisters, then we
600 // can use those registers to rename.
Rafael Espindola871c7242010-07-12 02:55:34 +0000601
602 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
603 // check every use of the register and find the largest register class
604 // that can be used in all of them.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000605 const TargetRegisterClass *SuperRC =
Rafael Espindola871c7242010-07-12 02:55:34 +0000606 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000607
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000608 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000609 if (Order.empty()) {
David Greene75a2efb2009-12-24 00:14:25 +0000610 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin7d8878a2009-11-05 01:19:35 +0000611 return false;
612 }
613
David Greene75a2efb2009-12-24 00:14:25 +0000614 DEBUG(dbgs() << "\tFind Registers:");
David Goodwindd1c6192009-11-19 23:12:37 +0000615
Benjamin Kramer2c99e412014-10-10 15:32:50 +0000616 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
David Goodwin7d8878a2009-11-05 01:19:35 +0000617
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000618 unsigned OrigR = RenameOrder[SuperRC];
619 unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
620 unsigned R = OrigR;
David Goodwin7d8878a2009-11-05 01:19:35 +0000621 do {
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000622 if (R == 0) R = Order.size();
David Goodwin7d8878a2009-11-05 01:19:35 +0000623 --R;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000624 const unsigned NewSuperReg = Order[R];
Jim Grosbach944aece2010-09-02 17:12:55 +0000625 // Don't consider non-allocatable registers
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000626 if (!MRI.isAllocatable(NewSuperReg)) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000627 // Don't replace a register with itself.
David Goodwin5305dc02009-11-20 23:33:54 +0000628 if (NewSuperReg == SuperReg) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000629
David Greene75a2efb2009-12-24 00:14:25 +0000630 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin5305dc02009-11-20 23:33:54 +0000631 RenameMap.clear();
632
633 // For each referenced group register (which must be a SuperReg or
634 // a subregister of SuperReg), find the corresponding subregister
635 // of NewSuperReg and make sure it is free to be renamed.
636 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
637 unsigned Reg = Regs[i];
638 unsigned NewReg = 0;
639 if (Reg == SuperReg) {
640 NewReg = NewSuperReg;
641 } else {
642 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
643 if (NewSubRegIdx != 0)
644 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwinde11f362009-10-26 19:32:42 +0000645 }
David Goodwin5305dc02009-11-20 23:33:54 +0000646
David Greene75a2efb2009-12-24 00:14:25 +0000647 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbacheb431da2010-01-06 16:48:02 +0000648
David Goodwin5305dc02009-11-20 23:33:54 +0000649 // Check if Reg can be renamed to NewReg.
650 BitVector BV = RenameRegisterMap[Reg];
651 if (!BV.test(NewReg)) {
David Greene75a2efb2009-12-24 00:14:25 +0000652 DEBUG(dbgs() << "(no rename)");
David Goodwin5305dc02009-11-20 23:33:54 +0000653 goto next_super_reg;
654 }
655
656 // If NewReg is dead and NewReg's most recent def is not before
657 // Regs's kill, it's safe to replace Reg with NewReg. We
658 // must also check all aliases of NewReg, because we can't define a
659 // register when any sub or super is already live.
660 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene75a2efb2009-12-24 00:14:25 +0000661 DEBUG(dbgs() << "(live)");
David Goodwin5305dc02009-11-20 23:33:54 +0000662 goto next_super_reg;
663 } else {
664 bool found = false;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000665 for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
666 unsigned AliasReg = *AI;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000667 if (State->IsLive(AliasReg) ||
668 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene75a2efb2009-12-24 00:14:25 +0000669 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin5305dc02009-11-20 23:33:54 +0000670 found = true;
671 break;
672 }
673 }
674 if (found)
675 goto next_super_reg;
676 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000677
Hal Finkelc8cf2b82014-12-09 01:00:59 +0000678 // We cannot rename 'Reg' to 'NewReg' if one of the uses of 'Reg' also
679 // defines 'NewReg' via an early-clobber operand.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000680 for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
681 MachineInstr *UseMI = Q.second.Operand->getParent();
Hal Finkelc8cf2b82014-12-09 01:00:59 +0000682 int Idx = UseMI->findRegisterDefOperandIdx(NewReg, false, true, TRI);
683 if (Idx == -1)
684 continue;
685
686 if (UseMI->getOperand(Idx).isEarlyClobber()) {
687 DEBUG(dbgs() << "(ec)");
688 goto next_super_reg;
689 }
690 }
691
Hal Finkele0a28e52015-08-31 07:51:36 +0000692 // Also, we cannot rename 'Reg' to 'NewReg' if the instruction defining
693 // 'Reg' is an early-clobber define and that instruction also uses
694 // 'NewReg'.
695 for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
696 if (!Q.second.Operand->isDef() || !Q.second.Operand->isEarlyClobber())
697 continue;
698
699 MachineInstr *DefMI = Q.second.Operand->getParent();
700 if (DefMI->readsRegister(NewReg, TRI)) {
701 DEBUG(dbgs() << "(ec)");
702 goto next_super_reg;
703 }
704 }
705
David Goodwin5305dc02009-11-20 23:33:54 +0000706 // Record that 'Reg' can be renamed to 'NewReg'.
707 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwinde11f362009-10-26 19:32:42 +0000708 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000709
David Goodwin5305dc02009-11-20 23:33:54 +0000710 // If we fall-out here, then every register in the group can be
711 // renamed, as recorded in RenameMap.
712 RenameOrder.erase(SuperRC);
713 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene75a2efb2009-12-24 00:14:25 +0000714 DEBUG(dbgs() << "]\n");
David Goodwin5305dc02009-11-20 23:33:54 +0000715 return true;
716
717 next_super_reg:
David Greene75a2efb2009-12-24 00:14:25 +0000718 DEBUG(dbgs() << ']');
David Goodwin7d8878a2009-11-05 01:19:35 +0000719 } while (R != EndR);
David Goodwinde11f362009-10-26 19:32:42 +0000720
David Greene75a2efb2009-12-24 00:14:25 +0000721 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000722
723 // No registers are free and available!
724 return false;
725}
726
727/// BreakAntiDependencies - Identifiy anti-dependencies within the
728/// ScheduleDAG and break them by renaming registers.
729///
David Goodwine056d102009-10-26 22:31:16 +0000730unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman35bc4d42010-04-19 23:11:58 +0000731 const std::vector<SUnit>& SUnits,
732 MachineBasicBlock::iterator Begin,
733 MachineBasicBlock::iterator End,
Devang Patelf02a3762011-06-02 21:26:52 +0000734 unsigned InsertPosIndex,
735 DbgValueVector &DbgValues) {
736
Bill Wendling030b0282010-07-15 18:43:09 +0000737 std::vector<unsigned> &KillIndices = State->GetKillIndices();
738 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000739 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000740 RegRefs = State->GetRegRefs();
741
David Goodwinde11f362009-10-26 19:32:42 +0000742 // The code below assumes that there is at least one instruction,
743 // so just duck out immediately if the block is empty.
David Goodwin8501dbbe2009-11-03 20:57:50 +0000744 if (SUnits.empty()) return 0;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000745
David Goodwin7d8878a2009-11-05 01:19:35 +0000746 // For each regclass the next register to use for renaming.
747 RenameOrderType RenameOrder;
David Goodwinde11f362009-10-26 19:32:42 +0000748
749 // ...need a map from MI to SUnit.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000750 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwinde11f362009-10-26 19:32:42 +0000751 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000752 const SUnit *SU = &SUnits[i];
753 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
754 SU));
David Goodwinde11f362009-10-26 19:32:42 +0000755 }
756
David Goodwinb9fe5d52009-11-13 19:52:48 +0000757 // Track progress along the critical path through the SUnit graph as
758 // we walk the instructions. This is needed for regclasses that only
759 // break critical-path anti-dependencies.
Craig Topperc0196b12014-04-14 00:51:57 +0000760 const SUnit *CriticalPathSU = nullptr;
761 MachineInstr *CriticalPathMI = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000762 if (CriticalPathSet.any()) {
763 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000764 const SUnit *SU = &SUnits[i];
Jim Grosbacheb431da2010-01-06 16:48:02 +0000765 if (!CriticalPathSU ||
766 ((SU->getDepth() + SU->Latency) >
David Goodwinb9fe5d52009-11-13 19:52:48 +0000767 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
768 CriticalPathSU = SU;
769 }
770 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000771
David Goodwinb9fe5d52009-11-13 19:52:48 +0000772 CriticalPathMI = CriticalPathSU->getInstr();
773 }
774
Jim Grosbacheb431da2010-01-06 16:48:02 +0000775#ifndef NDEBUG
David Greene75a2efb2009-12-24 00:14:25 +0000776 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
777 DEBUG(dbgs() << "Available regs:");
David Goodwin80a03cc2009-11-20 19:32:48 +0000778 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
779 if (!State->IsLive(Reg))
David Greene75a2efb2009-12-24 00:14:25 +0000780 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000781 }
David Greene75a2efb2009-12-24 00:14:25 +0000782 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000783#endif
784
785 // Attempt to break anti-dependence edges. Walk the instructions
786 // from the bottom up, tracking information about liveness as we go
787 // to help determine which registers are available.
788 unsigned Broken = 0;
789 unsigned Count = InsertPosIndex - 1;
790 for (MachineBasicBlock::iterator I = End, E = Begin;
791 I != E; --Count) {
792 MachineInstr *MI = --I;
793
Hal Finkel8606e3c2012-01-16 22:53:41 +0000794 if (MI->isDebugValue())
795 continue;
796
David Greene75a2efb2009-12-24 00:14:25 +0000797 DEBUG(dbgs() << "Anti: ");
David Goodwinde11f362009-10-26 19:32:42 +0000798 DEBUG(MI->dump());
799
800 std::set<unsigned> PassthruRegs;
801 GetPassthruRegs(MI, PassthruRegs);
802
803 // Process the defs in MI...
804 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000805
David Goodwin80a03cc2009-11-20 19:32:48 +0000806 // The dependence edges that represent anti- and output-
David Goodwinb9fe5d52009-11-13 19:52:48 +0000807 // dependencies that are candidates for breaking.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000808 std::vector<const SDep *> Edges;
809 const SUnit *PathSU = MISUnitMap[MI];
David Goodwin80a03cc2009-11-20 19:32:48 +0000810 AntiDepEdges(PathSU, Edges);
David Goodwinb9fe5d52009-11-13 19:52:48 +0000811
812 // If MI is not on the critical path, then we don't rename
813 // registers in the CriticalPathSet.
Craig Topperc0196b12014-04-14 00:51:57 +0000814 BitVector *ExcludeRegs = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000815 if (MI == CriticalPathMI) {
816 CriticalPathSU = CriticalPathStep(CriticalPathSU);
Craig Topperc0196b12014-04-14 00:51:57 +0000817 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr;
Hal Finkel6f1ff8e2013-09-12 04:22:31 +0000818 } else if (CriticalPathSet.any()) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000819 ExcludeRegs = &CriticalPathSet;
820 }
821
David Goodwinde11f362009-10-26 19:32:42 +0000822 // Ignore KILL instructions (they form a group in ScanInstruction
823 // but don't cause any anti-dependence breaking themselves)
Chris Lattnerb06015a2010-02-09 19:54:29 +0000824 if (!MI->isKill()) {
David Goodwinde11f362009-10-26 19:32:42 +0000825 // Attempt to break each anti-dependency...
826 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000827 const SDep *Edge = Edges[i];
David Goodwinde11f362009-10-26 19:32:42 +0000828 SUnit *NextSU = Edge->getSUnit();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000829
David Goodwinda83f7d2009-11-12 19:08:21 +0000830 if ((Edge->getKind() != SDep::Anti) &&
831 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000832
David Goodwinde11f362009-10-26 19:32:42 +0000833 unsigned AntiDepReg = Edge->getReg();
David Greene75a2efb2009-12-24 00:14:25 +0000834 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwinde11f362009-10-26 19:32:42 +0000835 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000836
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000837 if (!MRI.isAllocatable(AntiDepReg)) {
David Goodwinde11f362009-10-26 19:32:42 +0000838 // Don't break anti-dependencies on non-allocatable registers.
David Greene75a2efb2009-12-24 00:14:25 +0000839 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000840 continue;
Craig Topperc0196b12014-04-14 00:51:57 +0000841 } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000842 // Don't break anti-dependencies for critical path registers
843 // if not on the critical path
David Greene75a2efb2009-12-24 00:14:25 +0000844 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwinb9fe5d52009-11-13 19:52:48 +0000845 continue;
David Goodwinde11f362009-10-26 19:32:42 +0000846 } else if (PassthruRegs.count(AntiDepReg) != 0) {
847 // If the anti-dep register liveness "passes-thru", then
848 // don't try to change it. It will be changed along with
849 // the use if required to break an earlier antidep.
David Greene75a2efb2009-12-24 00:14:25 +0000850 DEBUG(dbgs() << " (passthru)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000851 continue;
852 } else {
853 // No anti-dep breaking for implicit deps
854 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
Craig Topperc0196b12014-04-14 00:51:57 +0000855 assert(AntiDepOp && "Can't find index for defined register operand");
856 if (!AntiDepOp || AntiDepOp->isImplicit()) {
David Greene75a2efb2009-12-24 00:14:25 +0000857 DEBUG(dbgs() << " (implicit)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000858 continue;
859 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000860
David Goodwinde11f362009-10-26 19:32:42 +0000861 // If the SUnit has other dependencies on the SUnit that
862 // it anti-depends on, don't bother breaking the
863 // anti-dependency since those edges would prevent such
864 // units from being scheduled past each other
865 // regardless.
David Goodwin80a03cc2009-11-20 19:32:48 +0000866 //
867 // Also, if there are dependencies on other SUnits with the
868 // same register as the anti-dependency, don't attempt to
869 // break it.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000870 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwinde11f362009-10-26 19:32:42 +0000871 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000872 if (P->getSUnit() == NextSU ?
873 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
874 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
875 AntiDepReg = 0;
876 break;
877 }
878 }
Dan Gohman35bc4d42010-04-19 23:11:58 +0000879 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin80a03cc2009-11-20 19:32:48 +0000880 PE = PathSU->Preds.end(); P != PE; ++P) {
881 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
882 (P->getKind() != SDep::Output)) {
David Greene75a2efb2009-12-24 00:14:25 +0000883 DEBUG(dbgs() << " (real dependency)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000884 AntiDepReg = 0;
885 break;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000886 } else if ((P->getSUnit() != NextSU) &&
887 (P->getKind() == SDep::Data) &&
David Goodwin80a03cc2009-11-20 19:32:48 +0000888 (P->getReg() == AntiDepReg)) {
David Greene75a2efb2009-12-24 00:14:25 +0000889 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin80a03cc2009-11-20 19:32:48 +0000890 AntiDepReg = 0;
891 break;
David Goodwinde11f362009-10-26 19:32:42 +0000892 }
893 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000894
David Goodwinde11f362009-10-26 19:32:42 +0000895 if (AntiDepReg == 0) continue;
896 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000897
David Goodwinde11f362009-10-26 19:32:42 +0000898 assert(AntiDepReg != 0);
899 if (AntiDepReg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000900
David Goodwinde11f362009-10-26 19:32:42 +0000901 // Determine AntiDepReg's register group.
David Goodwine056d102009-10-26 22:31:16 +0000902 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwinde11f362009-10-26 19:32:42 +0000903 if (GroupIndex == 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000904 DEBUG(dbgs() << " (zero group)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000905 continue;
906 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000907
David Greene75a2efb2009-12-24 00:14:25 +0000908 DEBUG(dbgs() << '\n');
Jim Grosbacheb431da2010-01-06 16:48:02 +0000909
David Goodwinde11f362009-10-26 19:32:42 +0000910 // Look for a suitable register to use to break the anti-dependence.
911 std::map<unsigned, unsigned> RenameMap;
David Goodwin7d8878a2009-11-05 01:19:35 +0000912 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene75a2efb2009-12-24 00:14:25 +0000913 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwinde11f362009-10-26 19:32:42 +0000914 << TRI->getName(AntiDepReg) << ":");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000915
David Goodwinde11f362009-10-26 19:32:42 +0000916 // Handle each group register...
917 for (std::map<unsigned, unsigned>::iterator
918 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
919 unsigned CurrReg = S->first;
920 unsigned NewReg = S->second;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000921
922 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
923 TRI->getName(NewReg) << "(" <<
David Goodwinde11f362009-10-26 19:32:42 +0000924 RegRefs.count(CurrReg) << " refs)");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000925
David Goodwinde11f362009-10-26 19:32:42 +0000926 // Update the references to the old register CurrReg to
927 // refer to the new register NewReg.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000928 for (const auto &Q : make_range(RegRefs.equal_range(CurrReg))) {
929 Q.second.Operand->setReg(NewReg);
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000930 // If the SU for the instruction being updated has debug
931 // information related to the anti-dependency register, make
932 // sure to update that as well.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000933 const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()];
Jim Grosbach84854832010-06-02 15:29:36 +0000934 if (!SU) continue;
Devang Patelf02a3762011-06-02 21:26:52 +0000935 for (DbgValueVector::iterator DVI = DbgValues.begin(),
936 DVE = DbgValues.end(); DVI != DVE; ++DVI)
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000937 if (DVI->second == Q.second.Operand->getParent())
Devang Patelf02a3762011-06-02 21:26:52 +0000938 UpdateDbgValue(DVI->first, AntiDepReg, NewReg);
David Goodwinde11f362009-10-26 19:32:42 +0000939 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000940
David Goodwinde11f362009-10-26 19:32:42 +0000941 // We just went back in time and modified history; the
942 // liveness information for CurrReg is now inconsistent. Set
943 // the state as if it were dead.
David Goodwine056d102009-10-26 22:31:16 +0000944 State->UnionGroups(NewReg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000945 RegRefs.erase(NewReg);
946 DefIndices[NewReg] = DefIndices[CurrReg];
947 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbacheb431da2010-01-06 16:48:02 +0000948
David Goodwine056d102009-10-26 22:31:16 +0000949 State->UnionGroups(CurrReg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000950 RegRefs.erase(CurrReg);
951 DefIndices[CurrReg] = KillIndices[CurrReg];
952 KillIndices[CurrReg] = ~0u;
953 assert(((KillIndices[CurrReg] == ~0u) !=
954 (DefIndices[CurrReg] == ~0u)) &&
955 "Kill and Def maps aren't consistent for AntiDepReg!");
956 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000957
David Goodwinde11f362009-10-26 19:32:42 +0000958 ++Broken;
David Greene75a2efb2009-12-24 00:14:25 +0000959 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000960 }
961 }
962 }
963
964 ScanInstruction(MI, Count);
965 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000966
David Goodwinde11f362009-10-26 19:32:42 +0000967 return Broken;
968}