blob: b0c3906b02e1dd949b416ccc92a7959de89c56e1 [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/TargetMachine.h"
28#include "llvm/Target/TargetRegisterInfo.h"
David Goodwinde11f362009-10-26 19:32:42 +000029using namespace llvm;
30
Chandler Carruth1b9dde02014-04-22 02:02:50 +000031#define DEBUG_TYPE "post-RA-sched"
32
David Goodwindd1c6192009-11-19 23:12:37 +000033// If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod
34static cl::opt<int>
35DebugDiv("agg-antidep-debugdiv",
Bob Wilson67dd3a42010-04-09 21:38:26 +000036 cl::desc("Debug control for aggressive anti-dep breaker"),
37 cl::init(0), cl::Hidden);
David Goodwindd1c6192009-11-19 23:12:37 +000038static cl::opt<int>
39DebugMod("agg-antidep-debugmod",
Bob Wilson67dd3a42010-04-09 21:38:26 +000040 cl::desc("Debug control for aggressive anti-dep breaker"),
41 cl::init(0), cl::Hidden);
David Goodwindd1c6192009-11-19 23:12:37 +000042
David Goodwina45fe672009-12-09 17:18:22 +000043AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
44 MachineBasicBlock *BB) :
Bill Wendling51a9c0a2010-07-15 19:58:14 +000045 NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0),
46 GroupNodeIndices(TargetRegs, 0),
47 KillIndices(TargetRegs, 0),
48 DefIndices(TargetRegs, 0)
49{
David Goodwina45fe672009-12-09 17:18:22 +000050 const unsigned BBSize = BB->size();
51 for (unsigned i = 0; i < NumTargetRegs; ++i) {
52 // Initialize all registers to be in their own group. Initially we
53 // assign the register to the same-indexed GroupNode.
54 GroupNodeIndices[i] = i;
55 // Initialize the indices to indicate that no registers are live.
56 KillIndices[i] = ~0u;
57 DefIndices[i] = BBSize;
58 }
David Goodwinde11f362009-10-26 19:32:42 +000059}
60
Bill Wendling5a8d15c2010-07-15 19:41:20 +000061unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) {
David Goodwinde11f362009-10-26 19:32:42 +000062 unsigned Node = GroupNodeIndices[Reg];
63 while (GroupNodes[Node] != Node)
64 Node = GroupNodes[Node];
65
66 return Node;
67}
68
David Goodwinb9fe5d52009-11-13 19:52:48 +000069void AggressiveAntiDepState::GetGroupRegs(
70 unsigned Group,
71 std::vector<unsigned> &Regs,
72 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
David Goodwinde11f362009-10-26 19:32:42 +000073{
David Goodwina45fe672009-12-09 17:18:22 +000074 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
David Goodwinb9fe5d52009-11-13 19:52:48 +000075 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
David Goodwinde11f362009-10-26 19:32:42 +000076 Regs.push_back(Reg);
77 }
78}
79
David Goodwine056d102009-10-26 22:31:16 +000080unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2)
David Goodwinde11f362009-10-26 19:32:42 +000081{
82 assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
83 assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
Jim Grosbacheb431da2010-01-06 16:48:02 +000084
David Goodwinde11f362009-10-26 19:32:42 +000085 // find group for each register
86 unsigned Group1 = GetGroup(Reg1);
87 unsigned Group2 = GetGroup(Reg2);
Jim Grosbacheb431da2010-01-06 16:48:02 +000088
David Goodwinde11f362009-10-26 19:32:42 +000089 // if either group is 0, then that must become the parent
90 unsigned Parent = (Group1 == 0) ? Group1 : Group2;
91 unsigned Other = (Parent == Group1) ? Group2 : Group1;
92 GroupNodes.at(Other) = Parent;
93 return Parent;
94}
Jim Grosbacheb431da2010-01-06 16:48:02 +000095
David Goodwine056d102009-10-26 22:31:16 +000096unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg)
David Goodwinde11f362009-10-26 19:32:42 +000097{
98 // Create a new GroupNode for Reg. Reg's existing GroupNode must
99 // stay as is because there could be other GroupNodes referring to
100 // it.
101 unsigned idx = GroupNodes.size();
102 GroupNodes.push_back(idx);
103 GroupNodeIndices[Reg] = idx;
104 return idx;
105}
106
David Goodwine056d102009-10-26 22:31:16 +0000107bool AggressiveAntiDepState::IsLive(unsigned Reg)
David Goodwinde11f362009-10-26 19:32:42 +0000108{
109 // KillIndex must be defined and DefIndex not defined for a register
110 // to be live.
111 return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
112}
113
Eric Christopherd9134482014-08-04 21:25:23 +0000114AggressiveAntiDepBreaker::AggressiveAntiDepBreaker(
115 MachineFunction &MFi, const RegisterClassInfo &RCI,
116 TargetSubtargetInfo::RegClassVector &CriticalPathRCs)
117 : AntiDepBreaker(), MF(MFi), MRI(MF.getRegInfo()),
Eric Christopherfc6de422014-08-05 02:39:49 +0000118 TII(MF.getSubtarget().getInstrInfo()),
119 TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
120 State(nullptr) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000121 /* Collect a bitset of all registers that are only broken if they
122 are on the critical path. */
123 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
124 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
125 if (CriticalPathSet.none())
126 CriticalPathSet = CPSet;
127 else
128 CriticalPathSet |= CPSet;
129 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000130
David Greene75a2efb2009-12-24 00:14:25 +0000131 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000132 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000133 r = CriticalPathSet.find_next(r))
David Greene75a2efb2009-12-24 00:14:25 +0000134 dbgs() << " " << TRI->getName(r));
135 DEBUG(dbgs() << '\n');
David Goodwine056d102009-10-26 22:31:16 +0000136}
137
138AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
139 delete State;
David Goodwine056d102009-10-26 22:31:16 +0000140}
141
142void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000143 assert(!State);
David Goodwina45fe672009-12-09 17:18:22 +0000144 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine056d102009-10-26 22:31:16 +0000145
Evan Cheng7f8e5632011-12-07 07:15:52 +0000146 bool IsReturnBlock = (!BB->empty() && BB->back().isReturn());
Bill Wendling030b0282010-07-15 18:43:09 +0000147 std::vector<unsigned> &KillIndices = State->GetKillIndices();
148 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwine056d102009-10-26 22:31:16 +0000149
Jakob Stoklund Olesenc3386792013-02-05 18:21:52 +0000150 // Examine the live-in regs of all successors.
Evan Chengf128bdc2010-06-16 07:35:02 +0000151 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
152 SE = BB->succ_end(); SI != SE; ++SI)
153 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
154 E = (*SI)->livein_end(); I != E; ++I) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000155 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
156 unsigned Reg = *AI;
Jakob Stoklund Olesenbe1c8d32010-12-14 23:23:15 +0000157 State->UnionGroups(Reg, 0);
158 KillIndices[Reg] = BB->size();
159 DefIndices[Reg] = ~0u;
Evan Chengf128bdc2010-06-16 07:35:02 +0000160 }
161 }
162
David Goodwine056d102009-10-26 22:31:16 +0000163 // Mark live-out callee-saved registers. In a return block this is
164 // all callee-saved registers. In non-return this is any
165 // callee-saved register that is not saved in the prolog.
166 const MachineFrameInfo *MFI = MF.getFrameInfo();
167 BitVector Pristine = MFI->getPristineRegs(BB);
Craig Topper840beec2014-04-04 05:16:06 +0000168 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
David Goodwine056d102009-10-26 22:31:16 +0000169 unsigned Reg = *I;
170 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000171 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
172 unsigned AliasReg = *AI;
David Goodwine056d102009-10-26 22:31:16 +0000173 State->UnionGroups(AliasReg, 0);
174 KillIndices[AliasReg] = BB->size();
175 DefIndices[AliasReg] = ~0u;
176 }
177 }
178}
179
180void AggressiveAntiDepBreaker::FinishBlock() {
181 delete State;
Craig Topperc0196b12014-04-14 00:51:57 +0000182 State = nullptr;
David Goodwine056d102009-10-26 22:31:16 +0000183}
184
185void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000186 unsigned InsertPosIndex) {
David Goodwine056d102009-10-26 22:31:16 +0000187 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
188
David Goodwinfaa76602009-10-29 23:30:59 +0000189 std::set<unsigned> PassthruRegs;
190 GetPassthruRegs(MI, PassthruRegs);
191 PrescanInstruction(MI, Count, PassthruRegs);
192 ScanInstruction(MI, Count);
193
David Greene75a2efb2009-12-24 00:14:25 +0000194 DEBUG(dbgs() << "Observe: ");
David Goodwine056d102009-10-26 22:31:16 +0000195 DEBUG(MI->dump());
David Greene75a2efb2009-12-24 00:14:25 +0000196 DEBUG(dbgs() << "\tRegs:");
David Goodwine056d102009-10-26 22:31:16 +0000197
Bill Wendling030b0282010-07-15 18:43:09 +0000198 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwina45fe672009-12-09 17:18:22 +0000199 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine056d102009-10-26 22:31:16 +0000200 // If Reg is current live, then mark that it can't be renamed as
201 // we don't know the extent of its live-range anymore (now that it
202 // has been scheduled). If it is not live but was defined in the
203 // previous schedule region, then set its def index to the most
204 // conservative location (i.e. the beginning of the previous
205 // schedule region).
206 if (State->IsLive(Reg)) {
207 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbacheb431da2010-01-06 16:48:02 +0000208 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine056d102009-10-26 22:31:16 +0000209 State->GetGroup(Reg) << "->g0(region live-out)");
210 State->UnionGroups(Reg, 0);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000211 } else if ((DefIndices[Reg] < InsertPosIndex)
212 && (DefIndices[Reg] >= Count)) {
David Goodwine056d102009-10-26 22:31:16 +0000213 DefIndices[Reg] = Count;
214 }
215 }
David Greene75a2efb2009-12-24 00:14:25 +0000216 DEBUG(dbgs() << '\n');
David Goodwine056d102009-10-26 22:31:16 +0000217}
218
David Goodwinde11f362009-10-26 19:32:42 +0000219bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000220 MachineOperand& MO)
David Goodwinde11f362009-10-26 19:32:42 +0000221{
222 if (!MO.isReg() || !MO.isImplicit())
223 return false;
224
225 unsigned Reg = MO.getReg();
226 if (Reg == 0)
227 return false;
228
Craig Topperc0196b12014-04-14 00:51:57 +0000229 MachineOperand *Op = nullptr;
David Goodwinde11f362009-10-26 19:32:42 +0000230 if (MO.isDef())
231 Op = MI->findRegisterUseOperand(Reg, true);
232 else
233 Op = MI->findRegisterDefOperand(Reg);
234
Craig Topperc0196b12014-04-14 00:51:57 +0000235 return(Op && Op->isImplicit());
David Goodwinde11f362009-10-26 19:32:42 +0000236}
237
238void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
239 std::set<unsigned>& PassthruRegs) {
240 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
241 MachineOperand &MO = MI->getOperand(i);
242 if (!MO.isReg()) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000243 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
David Goodwinde11f362009-10-26 19:32:42 +0000244 IsImplicitDefUse(MI, MO)) {
245 const unsigned Reg = MO.getReg();
Chad Rosierabdb1d62013-05-22 23:17:36 +0000246 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
247 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000248 PassthruRegs.insert(*SubRegs);
David Goodwinde11f362009-10-26 19:32:42 +0000249 }
250 }
251}
252
David Goodwin80a03cc2009-11-20 19:32:48 +0000253/// AntiDepEdges - Return in Edges the anti- and output- dependencies
254/// in SU that we want to consider for breaking.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000255static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000256 SmallSet<unsigned, 4> RegSet;
Dan Gohman35bc4d42010-04-19 23:11:58 +0000257 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwinde11f362009-10-26 19:32:42 +0000258 P != PE; ++P) {
David Goodwinda83f7d2009-11-12 19:08:21 +0000259 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Goodwinde11f362009-10-26 19:32:42 +0000260 unsigned Reg = P->getReg();
David Goodwin80a03cc2009-11-20 19:32:48 +0000261 if (RegSet.count(Reg) == 0) {
David Goodwinde11f362009-10-26 19:32:42 +0000262 Edges.push_back(&*P);
David Goodwin80a03cc2009-11-20 19:32:48 +0000263 RegSet.insert(Reg);
David Goodwinde11f362009-10-26 19:32:42 +0000264 }
265 }
266 }
267}
268
David Goodwinb9fe5d52009-11-13 19:52:48 +0000269/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
270/// critical path.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000271static const SUnit *CriticalPathStep(const SUnit *SU) {
Craig Topperc0196b12014-04-14 00:51:57 +0000272 const SDep *Next = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000273 unsigned NextDepth = 0;
274 // Find the predecessor edge with the greatest depth.
Craig Topperc0196b12014-04-14 00:51:57 +0000275 if (SU) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000276 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwinb9fe5d52009-11-13 19:52:48 +0000277 P != PE; ++P) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000278 const SUnit *PredSU = P->getSUnit();
David Goodwinb9fe5d52009-11-13 19:52:48 +0000279 unsigned PredLatency = P->getLatency();
280 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
281 // In the case of a latency tie, prefer an anti-dependency edge over
282 // other types of edges.
283 if (NextDepth < PredTotalLatency ||
284 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
285 NextDepth = PredTotalLatency;
286 Next = &*P;
287 }
288 }
289 }
290
Craig Topperc0196b12014-04-14 00:51:57 +0000291 return (Next) ? Next->getSUnit() : nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000292}
293
David Goodwin9f1b2d42009-10-29 19:17:04 +0000294void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbacheb431da2010-01-06 16:48:02 +0000295 const char *tag,
296 const char *header,
David Goodwindd1c6192009-11-19 23:12:37 +0000297 const char *footer) {
Bill Wendling030b0282010-07-15 18:43:09 +0000298 std::vector<unsigned> &KillIndices = State->GetKillIndices();
299 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000300 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin9f1b2d42009-10-29 19:17:04 +0000301 RegRefs = State->GetRegRefs();
302
303 if (!State->IsLive(Reg)) {
304 KillIndices[Reg] = KillIdx;
305 DefIndices[Reg] = ~0u;
306 RegRefs.erase(Reg);
307 State->LeaveGroup(Reg);
Craig Topperc0196b12014-04-14 00:51:57 +0000308 DEBUG(if (header) {
309 dbgs() << header << TRI->getName(Reg); header = nullptr; });
David Greene75a2efb2009-12-24 00:14:25 +0000310 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
David Goodwin9f1b2d42009-10-29 19:17:04 +0000311 }
312 // Repeat for subregisters.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000313 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
314 unsigned SubregReg = *SubRegs;
David Goodwin9f1b2d42009-10-29 19:17:04 +0000315 if (!State->IsLive(SubregReg)) {
316 KillIndices[SubregReg] = KillIdx;
317 DefIndices[SubregReg] = ~0u;
318 RegRefs.erase(SubregReg);
319 State->LeaveGroup(SubregReg);
Craig Topperc0196b12014-04-14 00:51:57 +0000320 DEBUG(if (header) {
321 dbgs() << header << TRI->getName(Reg); header = nullptr; });
David Greene75a2efb2009-12-24 00:14:25 +0000322 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin9f1b2d42009-10-29 19:17:04 +0000323 State->GetGroup(SubregReg) << tag);
324 }
325 }
David Goodwindd1c6192009-11-19 23:12:37 +0000326
Craig Topperc0196b12014-04-14 00:51:57 +0000327 DEBUG(if (!header && footer) dbgs() << footer);
David Goodwin9f1b2d42009-10-29 19:17:04 +0000328}
329
Jim Grosbacheb431da2010-01-06 16:48:02 +0000330void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
331 unsigned Count,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000332 std::set<unsigned>& PassthruRegs) {
Bill Wendling030b0282010-07-15 18:43:09 +0000333 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000334 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000335 RegRefs = State->GetRegRefs();
336
David Goodwin9f1b2d42009-10-29 19:17:04 +0000337 // Handle dead defs by simulating a last-use of the register just
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000338 // after the def. A dead def can occur because the def is truly
David Goodwin9f1b2d42009-10-29 19:17:04 +0000339 // dead, or because only a subregister is live at the def. If we
340 // don't do this the dead def will be incorrectly merged into the
341 // previous def.
David Goodwinde11f362009-10-26 19:32:42 +0000342 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
343 MachineOperand &MO = MI->getOperand(i);
344 if (!MO.isReg() || !MO.isDef()) continue;
345 unsigned Reg = MO.getReg();
346 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000347
David Goodwindd1c6192009-11-19 23:12:37 +0000348 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwinde11f362009-10-26 19:32:42 +0000349 }
350
David Greene75a2efb2009-12-24 00:14:25 +0000351 DEBUG(dbgs() << "\tDef Groups:");
David Goodwinde11f362009-10-26 19:32:42 +0000352 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
353 MachineOperand &MO = MI->getOperand(i);
354 if (!MO.isReg() || !MO.isDef()) continue;
355 unsigned Reg = MO.getReg();
356 if (Reg == 0) continue;
357
Jim Grosbacheb431da2010-01-06 16:48:02 +0000358 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000359
David Goodwin9f1b2d42009-10-29 19:17:04 +0000360 // If MI's defs have a special allocation requirement, don't allow
David Goodwinde11f362009-10-26 19:32:42 +0000361 // any def registers to be changed. Also assume all registers
362 // defined in a call must not be changed (ABI).
Evan Cheng7f8e5632011-12-07 07:15:52 +0000363 if (MI->isCall() || MI->hasExtraDefRegAllocReq() ||
Evan Chengf128bdc2010-06-16 07:35:02 +0000364 TII->isPredicated(MI)) {
David Greene75a2efb2009-12-24 00:14:25 +0000365 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine056d102009-10-26 22:31:16 +0000366 State->UnionGroups(Reg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000367 }
368
369 // Any aliased that are live at this point are completely or
David Goodwin9f1b2d42009-10-29 19:17:04 +0000370 // partially defined here, so group those aliases with Reg.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000371 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
372 unsigned AliasReg = *AI;
David Goodwine056d102009-10-26 22:31:16 +0000373 if (State->IsLive(AliasReg)) {
374 State->UnionGroups(Reg, AliasReg);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000375 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwinde11f362009-10-26 19:32:42 +0000376 TRI->getName(AliasReg) << ")");
377 }
378 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000379
David Goodwinde11f362009-10-26 19:32:42 +0000380 // Note register reference...
Craig Topperc0196b12014-04-14 00:51:57 +0000381 const TargetRegisterClass *RC = nullptr;
David Goodwinde11f362009-10-26 19:32:42 +0000382 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000383 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwine056d102009-10-26 22:31:16 +0000384 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwinde11f362009-10-26 19:32:42 +0000385 RegRefs.insert(std::make_pair(Reg, RR));
386 }
387
David Greene75a2efb2009-12-24 00:14:25 +0000388 DEBUG(dbgs() << '\n');
David Goodwin9f1b2d42009-10-29 19:17:04 +0000389
390 // Scan the register defs for this instruction and update
391 // live-ranges.
392 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
393 MachineOperand &MO = MI->getOperand(i);
394 if (!MO.isReg() || !MO.isDef()) continue;
395 unsigned Reg = MO.getReg();
396 if (Reg == 0) continue;
David Goodwindd1c6192009-11-19 23:12:37 +0000397 // Ignore KILLs and passthru registers for liveness...
Chris Lattnerb06015a2010-02-09 19:54:29 +0000398 if (MI->isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwindd1c6192009-11-19 23:12:37 +0000399 continue;
David Goodwin9f1b2d42009-10-29 19:17:04 +0000400
David Goodwindd1c6192009-11-19 23:12:37 +0000401 // Update def for Reg and aliases.
Hal Finkel121caf62014-02-26 20:20:30 +0000402 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
403 // We need to be careful here not to define already-live super registers.
404 // If the super register is already live, then this definition is not
405 // a definition of the whole super register (just a partial insertion
406 // into it). Earlier subregister definitions (which we've not yet visited
407 // because we're iterating bottom-up) need to be linked to the same group
408 // as this definition.
409 if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI))
410 continue;
411
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000412 DefIndices[*AI] = Count;
Hal Finkel121caf62014-02-26 20:20:30 +0000413 }
David Goodwin9f1b2d42009-10-29 19:17:04 +0000414 }
David Goodwinde11f362009-10-26 19:32:42 +0000415}
416
417void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000418 unsigned Count) {
David Greene75a2efb2009-12-24 00:14:25 +0000419 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000420 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000421 RegRefs = State->GetRegRefs();
David Goodwinde11f362009-10-26 19:32:42 +0000422
Evan Chengf128bdc2010-06-16 07:35:02 +0000423 // If MI's uses have special allocation requirement, don't allow
424 // any use registers to be changed. Also assume all registers
425 // used in a call must not be changed (ABI).
426 // FIXME: The issue with predicated instruction is more complex. We are being
427 // conservatively here because the kill markers cannot be trusted after
428 // if-conversion:
429 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
430 // ...
431 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
432 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
433 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
434 //
435 // The first R6 kill is not really a kill since it's killed by a predicated
436 // instruction which may not be executed. The second R6 def may or may not
437 // re-define R6 so it's not safe to change it since the last R6 use cannot be
438 // changed.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000439 bool Special = MI->isCall() ||
440 MI->hasExtraSrcRegAllocReq() ||
Evan Chengf128bdc2010-06-16 07:35:02 +0000441 TII->isPredicated(MI);
442
David Goodwinde11f362009-10-26 19:32:42 +0000443 // Scan the register uses for this instruction and update
444 // live-ranges, groups and RegRefs.
445 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
446 MachineOperand &MO = MI->getOperand(i);
447 if (!MO.isReg() || !MO.isUse()) continue;
448 unsigned Reg = MO.getReg();
449 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000450
451 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
452 State->GetGroup(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000453
454 // It wasn't previously live but now it is, this is a kill. Forget
455 // the previous live-range information and start a new live-range
456 // for the register.
David Goodwin9f1b2d42009-10-29 19:17:04 +0000457 HandleLastUse(Reg, Count, "(last-use)");
David Goodwinde11f362009-10-26 19:32:42 +0000458
Evan Chengf128bdc2010-06-16 07:35:02 +0000459 if (Special) {
David Greene75a2efb2009-12-24 00:14:25 +0000460 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine056d102009-10-26 22:31:16 +0000461 State->UnionGroups(Reg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000462 }
463
464 // Note register reference...
Craig Topperc0196b12014-04-14 00:51:57 +0000465 const TargetRegisterClass *RC = nullptr;
David Goodwinde11f362009-10-26 19:32:42 +0000466 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000467 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwine056d102009-10-26 22:31:16 +0000468 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwinde11f362009-10-26 19:32:42 +0000469 RegRefs.insert(std::make_pair(Reg, RR));
470 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000471
David Greene75a2efb2009-12-24 00:14:25 +0000472 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000473
474 // Form a group of all defs and uses of a KILL instruction to ensure
475 // that all registers are renamed as a group.
Chris Lattnerb06015a2010-02-09 19:54:29 +0000476 if (MI->isKill()) {
David Greene75a2efb2009-12-24 00:14:25 +0000477 DEBUG(dbgs() << "\tKill Group:");
David Goodwinde11f362009-10-26 19:32:42 +0000478
479 unsigned FirstReg = 0;
480 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
481 MachineOperand &MO = MI->getOperand(i);
482 if (!MO.isReg()) continue;
483 unsigned Reg = MO.getReg();
484 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000485
David Goodwinde11f362009-10-26 19:32:42 +0000486 if (FirstReg != 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000487 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine056d102009-10-26 22:31:16 +0000488 State->UnionGroups(FirstReg, Reg);
David Goodwinde11f362009-10-26 19:32:42 +0000489 } else {
David Greene75a2efb2009-12-24 00:14:25 +0000490 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000491 FirstReg = Reg;
492 }
493 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000494
David Greene75a2efb2009-12-24 00:14:25 +0000495 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000496 }
497}
498
499BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
500 BitVector BV(TRI->getNumRegs(), false);
501 bool first = true;
502
503 // Check all references that need rewriting for Reg. For each, use
504 // the corresponding register class to narrow the set of registers
505 // that are appropriate for renaming.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000506 std::pair<std::multimap<unsigned,
David Goodwine056d102009-10-26 22:31:16 +0000507 AggressiveAntiDepState::RegisterReference>::iterator,
508 std::multimap<unsigned,
509 AggressiveAntiDepState::RegisterReference>::iterator>
510 Range = State->GetRegRefs().equal_range(Reg);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000511 for (std::multimap<unsigned,
512 AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first,
513 QE = Range.second; Q != QE; ++Q) {
David Goodwinde11f362009-10-26 19:32:42 +0000514 const TargetRegisterClass *RC = Q->second.RC;
Craig Topperc0196b12014-04-14 00:51:57 +0000515 if (!RC) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000516
517 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
518 if (first) {
519 BV |= RCBV;
520 first = false;
521 } else {
522 BV &= RCBV;
523 }
524
David Greene75a2efb2009-12-24 00:14:25 +0000525 DEBUG(dbgs() << " " << RC->getName());
David Goodwinde11f362009-10-26 19:32:42 +0000526 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000527
David Goodwinde11f362009-10-26 19:32:42 +0000528 return BV;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000529}
David Goodwinde11f362009-10-26 19:32:42 +0000530
531bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin7d8878a2009-11-05 01:19:35 +0000532 unsigned AntiDepGroupIndex,
533 RenameOrderType& RenameOrder,
534 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendling030b0282010-07-15 18:43:09 +0000535 std::vector<unsigned> &KillIndices = State->GetKillIndices();
536 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000537 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000538 RegRefs = State->GetRegRefs();
539
David Goodwinb9fe5d52009-11-13 19:52:48 +0000540 // Collect all referenced registers in the same group as
541 // AntiDepReg. These all need to be renamed together if we are to
542 // break the anti-dependence.
David Goodwinde11f362009-10-26 19:32:42 +0000543 std::vector<unsigned> Regs;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000544 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwinde11f362009-10-26 19:32:42 +0000545 assert(Regs.size() > 0 && "Empty register group!");
546 if (Regs.size() == 0)
547 return false;
548
549 // Find the "superest" register in the group. At the same time,
550 // collect the BitVector of registers that can be used to rename
551 // each register.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000552 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
553 << ":\n");
David Goodwinde11f362009-10-26 19:32:42 +0000554 std::map<unsigned, BitVector> RenameRegisterMap;
555 unsigned SuperReg = 0;
556 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
557 unsigned Reg = Regs[i];
558 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
559 SuperReg = Reg;
560
561 // If Reg has any references, then collect possible rename regs
562 if (RegRefs.count(Reg) > 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000563 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000564
David Goodwinde11f362009-10-26 19:32:42 +0000565 BitVector BV = GetRenameRegisters(Reg);
566 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
567
David Greene75a2efb2009-12-24 00:14:25 +0000568 DEBUG(dbgs() << " ::");
David Goodwinde11f362009-10-26 19:32:42 +0000569 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene75a2efb2009-12-24 00:14:25 +0000570 dbgs() << " " << TRI->getName(r));
571 DEBUG(dbgs() << "\n");
David Goodwinde11f362009-10-26 19:32:42 +0000572 }
573 }
574
575 // All group registers should be a subreg of SuperReg.
576 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
577 unsigned Reg = Regs[i];
578 if (Reg == SuperReg) continue;
579 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
Will Schmidt44ff8f02014-07-31 19:50:53 +0000580 // FIXME: remove this once PR18663 has been properly fixed. For now,
581 // return a conservative answer:
582 // assert(IsSub && "Expecting group subregister");
David Goodwinde11f362009-10-26 19:32:42 +0000583 if (!IsSub)
584 return false;
585 }
586
David Goodwin5305dc02009-11-20 23:33:54 +0000587#ifndef NDEBUG
588 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
589 if (DebugDiv > 0) {
590 static int renamecnt = 0;
591 if (renamecnt++ % DebugDiv != DebugMod)
592 return false;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000593
David Greene75a2efb2009-12-24 00:14:25 +0000594 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin5305dc02009-11-20 23:33:54 +0000595 " for debug ***\n";
596 }
597#endif
598
David Goodwin7d8878a2009-11-05 01:19:35 +0000599 // Check each possible rename register for SuperReg in round-robin
600 // order. If that register is available, and the corresponding
601 // registers are available for the other group subregisters, then we
602 // can use those registers to rename.
Rafael Espindola871c7242010-07-12 02:55:34 +0000603
604 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
605 // check every use of the register and find the largest register class
606 // that can be used in all of them.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000607 const TargetRegisterClass *SuperRC =
Rafael Espindola871c7242010-07-12 02:55:34 +0000608 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000609
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000610 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000611 if (Order.empty()) {
David Greene75a2efb2009-12-24 00:14:25 +0000612 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin7d8878a2009-11-05 01:19:35 +0000613 return false;
614 }
615
David Greene75a2efb2009-12-24 00:14:25 +0000616 DEBUG(dbgs() << "\tFind Registers:");
David Goodwindd1c6192009-11-19 23:12:37 +0000617
David Goodwin7d8878a2009-11-05 01:19:35 +0000618 if (RenameOrder.count(SuperRC) == 0)
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000619 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
David Goodwin7d8878a2009-11-05 01:19:35 +0000620
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000621 unsigned OrigR = RenameOrder[SuperRC];
622 unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
623 unsigned R = OrigR;
David Goodwin7d8878a2009-11-05 01:19:35 +0000624 do {
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000625 if (R == 0) R = Order.size();
David Goodwin7d8878a2009-11-05 01:19:35 +0000626 --R;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000627 const unsigned NewSuperReg = Order[R];
Jim Grosbach944aece2010-09-02 17:12:55 +0000628 // Don't consider non-allocatable registers
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000629 if (!MRI.isAllocatable(NewSuperReg)) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000630 // Don't replace a register with itself.
David Goodwin5305dc02009-11-20 23:33:54 +0000631 if (NewSuperReg == SuperReg) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000632
David Greene75a2efb2009-12-24 00:14:25 +0000633 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin5305dc02009-11-20 23:33:54 +0000634 RenameMap.clear();
635
636 // For each referenced group register (which must be a SuperReg or
637 // a subregister of SuperReg), find the corresponding subregister
638 // of NewSuperReg and make sure it is free to be renamed.
639 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
640 unsigned Reg = Regs[i];
641 unsigned NewReg = 0;
642 if (Reg == SuperReg) {
643 NewReg = NewSuperReg;
644 } else {
645 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
646 if (NewSubRegIdx != 0)
647 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwinde11f362009-10-26 19:32:42 +0000648 }
David Goodwin5305dc02009-11-20 23:33:54 +0000649
David Greene75a2efb2009-12-24 00:14:25 +0000650 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbacheb431da2010-01-06 16:48:02 +0000651
David Goodwin5305dc02009-11-20 23:33:54 +0000652 // Check if Reg can be renamed to NewReg.
653 BitVector BV = RenameRegisterMap[Reg];
654 if (!BV.test(NewReg)) {
David Greene75a2efb2009-12-24 00:14:25 +0000655 DEBUG(dbgs() << "(no rename)");
David Goodwin5305dc02009-11-20 23:33:54 +0000656 goto next_super_reg;
657 }
658
659 // If NewReg is dead and NewReg's most recent def is not before
660 // Regs's kill, it's safe to replace Reg with NewReg. We
661 // must also check all aliases of NewReg, because we can't define a
662 // register when any sub or super is already live.
663 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene75a2efb2009-12-24 00:14:25 +0000664 DEBUG(dbgs() << "(live)");
David Goodwin5305dc02009-11-20 23:33:54 +0000665 goto next_super_reg;
666 } else {
667 bool found = false;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000668 for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
669 unsigned AliasReg = *AI;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000670 if (State->IsLive(AliasReg) ||
671 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene75a2efb2009-12-24 00:14:25 +0000672 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin5305dc02009-11-20 23:33:54 +0000673 found = true;
674 break;
675 }
676 }
677 if (found)
678 goto next_super_reg;
679 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000680
David Goodwin5305dc02009-11-20 23:33:54 +0000681 // Record that 'Reg' can be renamed to 'NewReg'.
682 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwinde11f362009-10-26 19:32:42 +0000683 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000684
David Goodwin5305dc02009-11-20 23:33:54 +0000685 // If we fall-out here, then every register in the group can be
686 // renamed, as recorded in RenameMap.
687 RenameOrder.erase(SuperRC);
688 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene75a2efb2009-12-24 00:14:25 +0000689 DEBUG(dbgs() << "]\n");
David Goodwin5305dc02009-11-20 23:33:54 +0000690 return true;
691
692 next_super_reg:
David Greene75a2efb2009-12-24 00:14:25 +0000693 DEBUG(dbgs() << ']');
David Goodwin7d8878a2009-11-05 01:19:35 +0000694 } while (R != EndR);
David Goodwinde11f362009-10-26 19:32:42 +0000695
David Greene75a2efb2009-12-24 00:14:25 +0000696 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000697
698 // No registers are free and available!
699 return false;
700}
701
702/// BreakAntiDependencies - Identifiy anti-dependencies within the
703/// ScheduleDAG and break them by renaming registers.
704///
David Goodwine056d102009-10-26 22:31:16 +0000705unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman35bc4d42010-04-19 23:11:58 +0000706 const std::vector<SUnit>& SUnits,
707 MachineBasicBlock::iterator Begin,
708 MachineBasicBlock::iterator End,
Devang Patelf02a3762011-06-02 21:26:52 +0000709 unsigned InsertPosIndex,
710 DbgValueVector &DbgValues) {
711
Bill Wendling030b0282010-07-15 18:43:09 +0000712 std::vector<unsigned> &KillIndices = State->GetKillIndices();
713 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000714 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000715 RegRefs = State->GetRegRefs();
716
David Goodwinde11f362009-10-26 19:32:42 +0000717 // The code below assumes that there is at least one instruction,
718 // so just duck out immediately if the block is empty.
David Goodwin8501dbbe2009-11-03 20:57:50 +0000719 if (SUnits.empty()) return 0;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000720
David Goodwin7d8878a2009-11-05 01:19:35 +0000721 // For each regclass the next register to use for renaming.
722 RenameOrderType RenameOrder;
David Goodwinde11f362009-10-26 19:32:42 +0000723
724 // ...need a map from MI to SUnit.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000725 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwinde11f362009-10-26 19:32:42 +0000726 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000727 const SUnit *SU = &SUnits[i];
728 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
729 SU));
David Goodwinde11f362009-10-26 19:32:42 +0000730 }
731
David Goodwinb9fe5d52009-11-13 19:52:48 +0000732 // Track progress along the critical path through the SUnit graph as
733 // we walk the instructions. This is needed for regclasses that only
734 // break critical-path anti-dependencies.
Craig Topperc0196b12014-04-14 00:51:57 +0000735 const SUnit *CriticalPathSU = nullptr;
736 MachineInstr *CriticalPathMI = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000737 if (CriticalPathSet.any()) {
738 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000739 const SUnit *SU = &SUnits[i];
Jim Grosbacheb431da2010-01-06 16:48:02 +0000740 if (!CriticalPathSU ||
741 ((SU->getDepth() + SU->Latency) >
David Goodwinb9fe5d52009-11-13 19:52:48 +0000742 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
743 CriticalPathSU = SU;
744 }
745 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000746
David Goodwinb9fe5d52009-11-13 19:52:48 +0000747 CriticalPathMI = CriticalPathSU->getInstr();
748 }
749
Jim Grosbacheb431da2010-01-06 16:48:02 +0000750#ifndef NDEBUG
David Greene75a2efb2009-12-24 00:14:25 +0000751 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
752 DEBUG(dbgs() << "Available regs:");
David Goodwin80a03cc2009-11-20 19:32:48 +0000753 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
754 if (!State->IsLive(Reg))
David Greene75a2efb2009-12-24 00:14:25 +0000755 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000756 }
David Greene75a2efb2009-12-24 00:14:25 +0000757 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000758#endif
759
760 // Attempt to break anti-dependence edges. Walk the instructions
761 // from the bottom up, tracking information about liveness as we go
762 // to help determine which registers are available.
763 unsigned Broken = 0;
764 unsigned Count = InsertPosIndex - 1;
765 for (MachineBasicBlock::iterator I = End, E = Begin;
766 I != E; --Count) {
767 MachineInstr *MI = --I;
768
Hal Finkel8606e3c2012-01-16 22:53:41 +0000769 if (MI->isDebugValue())
770 continue;
771
David Greene75a2efb2009-12-24 00:14:25 +0000772 DEBUG(dbgs() << "Anti: ");
David Goodwinde11f362009-10-26 19:32:42 +0000773 DEBUG(MI->dump());
774
775 std::set<unsigned> PassthruRegs;
776 GetPassthruRegs(MI, PassthruRegs);
777
778 // Process the defs in MI...
779 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000780
David Goodwin80a03cc2009-11-20 19:32:48 +0000781 // The dependence edges that represent anti- and output-
David Goodwinb9fe5d52009-11-13 19:52:48 +0000782 // dependencies that are candidates for breaking.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000783 std::vector<const SDep *> Edges;
784 const SUnit *PathSU = MISUnitMap[MI];
David Goodwin80a03cc2009-11-20 19:32:48 +0000785 AntiDepEdges(PathSU, Edges);
David Goodwinb9fe5d52009-11-13 19:52:48 +0000786
787 // If MI is not on the critical path, then we don't rename
788 // registers in the CriticalPathSet.
Craig Topperc0196b12014-04-14 00:51:57 +0000789 BitVector *ExcludeRegs = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000790 if (MI == CriticalPathMI) {
791 CriticalPathSU = CriticalPathStep(CriticalPathSU);
Craig Topperc0196b12014-04-14 00:51:57 +0000792 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr;
Hal Finkel6f1ff8e2013-09-12 04:22:31 +0000793 } else if (CriticalPathSet.any()) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000794 ExcludeRegs = &CriticalPathSet;
795 }
796
David Goodwinde11f362009-10-26 19:32:42 +0000797 // Ignore KILL instructions (they form a group in ScanInstruction
798 // but don't cause any anti-dependence breaking themselves)
Chris Lattnerb06015a2010-02-09 19:54:29 +0000799 if (!MI->isKill()) {
David Goodwinde11f362009-10-26 19:32:42 +0000800 // Attempt to break each anti-dependency...
801 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000802 const SDep *Edge = Edges[i];
David Goodwinde11f362009-10-26 19:32:42 +0000803 SUnit *NextSU = Edge->getSUnit();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000804
David Goodwinda83f7d2009-11-12 19:08:21 +0000805 if ((Edge->getKind() != SDep::Anti) &&
806 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000807
David Goodwinde11f362009-10-26 19:32:42 +0000808 unsigned AntiDepReg = Edge->getReg();
David Greene75a2efb2009-12-24 00:14:25 +0000809 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwinde11f362009-10-26 19:32:42 +0000810 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000811
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000812 if (!MRI.isAllocatable(AntiDepReg)) {
David Goodwinde11f362009-10-26 19:32:42 +0000813 // Don't break anti-dependencies on non-allocatable registers.
David Greene75a2efb2009-12-24 00:14:25 +0000814 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000815 continue;
Craig Topperc0196b12014-04-14 00:51:57 +0000816 } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000817 // Don't break anti-dependencies for critical path registers
818 // if not on the critical path
David Greene75a2efb2009-12-24 00:14:25 +0000819 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwinb9fe5d52009-11-13 19:52:48 +0000820 continue;
David Goodwinde11f362009-10-26 19:32:42 +0000821 } else if (PassthruRegs.count(AntiDepReg) != 0) {
822 // If the anti-dep register liveness "passes-thru", then
823 // don't try to change it. It will be changed along with
824 // the use if required to break an earlier antidep.
David Greene75a2efb2009-12-24 00:14:25 +0000825 DEBUG(dbgs() << " (passthru)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000826 continue;
827 } else {
828 // No anti-dep breaking for implicit deps
829 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
Craig Topperc0196b12014-04-14 00:51:57 +0000830 assert(AntiDepOp && "Can't find index for defined register operand");
831 if (!AntiDepOp || AntiDepOp->isImplicit()) {
David Greene75a2efb2009-12-24 00:14:25 +0000832 DEBUG(dbgs() << " (implicit)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000833 continue;
834 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000835
David Goodwinde11f362009-10-26 19:32:42 +0000836 // If the SUnit has other dependencies on the SUnit that
837 // it anti-depends on, don't bother breaking the
838 // anti-dependency since those edges would prevent such
839 // units from being scheduled past each other
840 // regardless.
David Goodwin80a03cc2009-11-20 19:32:48 +0000841 //
842 // Also, if there are dependencies on other SUnits with the
843 // same register as the anti-dependency, don't attempt to
844 // break it.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000845 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwinde11f362009-10-26 19:32:42 +0000846 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000847 if (P->getSUnit() == NextSU ?
848 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
849 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
850 AntiDepReg = 0;
851 break;
852 }
853 }
Dan Gohman35bc4d42010-04-19 23:11:58 +0000854 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin80a03cc2009-11-20 19:32:48 +0000855 PE = PathSU->Preds.end(); P != PE; ++P) {
856 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
857 (P->getKind() != SDep::Output)) {
David Greene75a2efb2009-12-24 00:14:25 +0000858 DEBUG(dbgs() << " (real dependency)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000859 AntiDepReg = 0;
860 break;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000861 } else if ((P->getSUnit() != NextSU) &&
862 (P->getKind() == SDep::Data) &&
David Goodwin80a03cc2009-11-20 19:32:48 +0000863 (P->getReg() == AntiDepReg)) {
David Greene75a2efb2009-12-24 00:14:25 +0000864 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin80a03cc2009-11-20 19:32:48 +0000865 AntiDepReg = 0;
866 break;
David Goodwinde11f362009-10-26 19:32:42 +0000867 }
868 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000869
David Goodwinde11f362009-10-26 19:32:42 +0000870 if (AntiDepReg == 0) continue;
871 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000872
David Goodwinde11f362009-10-26 19:32:42 +0000873 assert(AntiDepReg != 0);
874 if (AntiDepReg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000875
David Goodwinde11f362009-10-26 19:32:42 +0000876 // Determine AntiDepReg's register group.
David Goodwine056d102009-10-26 22:31:16 +0000877 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwinde11f362009-10-26 19:32:42 +0000878 if (GroupIndex == 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000879 DEBUG(dbgs() << " (zero group)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000880 continue;
881 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000882
David Greene75a2efb2009-12-24 00:14:25 +0000883 DEBUG(dbgs() << '\n');
Jim Grosbacheb431da2010-01-06 16:48:02 +0000884
David Goodwinde11f362009-10-26 19:32:42 +0000885 // Look for a suitable register to use to break the anti-dependence.
886 std::map<unsigned, unsigned> RenameMap;
David Goodwin7d8878a2009-11-05 01:19:35 +0000887 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene75a2efb2009-12-24 00:14:25 +0000888 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwinde11f362009-10-26 19:32:42 +0000889 << TRI->getName(AntiDepReg) << ":");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000890
David Goodwinde11f362009-10-26 19:32:42 +0000891 // Handle each group register...
892 for (std::map<unsigned, unsigned>::iterator
893 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
894 unsigned CurrReg = S->first;
895 unsigned NewReg = S->second;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000896
897 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
898 TRI->getName(NewReg) << "(" <<
David Goodwinde11f362009-10-26 19:32:42 +0000899 RegRefs.count(CurrReg) << " refs)");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000900
David Goodwinde11f362009-10-26 19:32:42 +0000901 // Update the references to the old register CurrReg to
902 // refer to the new register NewReg.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000903 std::pair<std::multimap<unsigned,
904 AggressiveAntiDepState::RegisterReference>::iterator,
David Goodwine056d102009-10-26 22:31:16 +0000905 std::multimap<unsigned,
Jim Grosbacheb431da2010-01-06 16:48:02 +0000906 AggressiveAntiDepState::RegisterReference>::iterator>
David Goodwinde11f362009-10-26 19:32:42 +0000907 Range = RegRefs.equal_range(CurrReg);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000908 for (std::multimap<unsigned,
909 AggressiveAntiDepState::RegisterReference>::iterator
David Goodwinde11f362009-10-26 19:32:42 +0000910 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
911 Q->second.Operand->setReg(NewReg);
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000912 // If the SU for the instruction being updated has debug
913 // information related to the anti-dependency register, make
914 // sure to update that as well.
915 const SUnit *SU = MISUnitMap[Q->second.Operand->getParent()];
Jim Grosbach84854832010-06-02 15:29:36 +0000916 if (!SU) continue;
Devang Patelf02a3762011-06-02 21:26:52 +0000917 for (DbgValueVector::iterator DVI = DbgValues.begin(),
918 DVE = DbgValues.end(); DVI != DVE; ++DVI)
919 if (DVI->second == Q->second.Operand->getParent())
920 UpdateDbgValue(DVI->first, AntiDepReg, NewReg);
David Goodwinde11f362009-10-26 19:32:42 +0000921 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000922
David Goodwinde11f362009-10-26 19:32:42 +0000923 // We just went back in time and modified history; the
924 // liveness information for CurrReg is now inconsistent. Set
925 // the state as if it were dead.
David Goodwine056d102009-10-26 22:31:16 +0000926 State->UnionGroups(NewReg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000927 RegRefs.erase(NewReg);
928 DefIndices[NewReg] = DefIndices[CurrReg];
929 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbacheb431da2010-01-06 16:48:02 +0000930
David Goodwine056d102009-10-26 22:31:16 +0000931 State->UnionGroups(CurrReg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000932 RegRefs.erase(CurrReg);
933 DefIndices[CurrReg] = KillIndices[CurrReg];
934 KillIndices[CurrReg] = ~0u;
935 assert(((KillIndices[CurrReg] == ~0u) !=
936 (DefIndices[CurrReg] == ~0u)) &&
937 "Kill and Def maps aren't consistent for AntiDepReg!");
938 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000939
David Goodwinde11f362009-10-26 19:32:42 +0000940 ++Broken;
David Greene75a2efb2009-12-24 00:14:25 +0000941 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000942 }
943 }
944 }
945
946 ScanInstruction(MI, Count);
947 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000948
David Goodwinde11f362009-10-26 19:32:42 +0000949 return Broken;
950}