blob: 152d9fa3a72f52965a6f0aa6110dfe28f5fefc56 [file] [log] [blame]
Jim Grosbach2973b572010-01-06 16:48:02 +00001//===----- AggressiveAntiDepBreaker.cpp - Anti-dep breaker ----------------===//
David Goodwin34877712009-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 Goodwin4de099d2009-11-03 20:57:50 +000017#define DEBUG_TYPE "post-RA-sched"
David Goodwin34877712009-10-26 19:32:42 +000018#include "AggressiveAntiDepBreaker.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineInstr.h"
Andrew Trick15252602012-06-06 20:29:31 +000022#include "llvm/CodeGen/RegisterClassInfo.h"
David Goodwine10deca2009-10-26 22:31:16 +000023#include "llvm/Support/CommandLine.h"
David Goodwin34877712009-10-26 19:32:42 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000028#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetRegisterInfo.h"
David Goodwin34877712009-10-26 19:32:42 +000030using namespace llvm;
31
David Goodwin3e72d302009-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 Wilson347fa3f2010-04-09 21:38:26 +000035 cl::desc("Debug control for aggressive anti-dep breaker"),
36 cl::init(0), cl::Hidden);
David Goodwin3e72d302009-11-19 23:12:37 +000037static cl::opt<int>
38DebugMod("agg-antidep-debugmod",
Bob Wilson347fa3f2010-04-09 21:38:26 +000039 cl::desc("Debug control for aggressive anti-dep breaker"),
40 cl::init(0), cl::Hidden);
David Goodwin3e72d302009-11-19 23:12:37 +000041
David Goodwin990d2852009-12-09 17:18:22 +000042AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
43 MachineBasicBlock *BB) :
Bill Wendling9c2a0342010-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 Goodwin990d2852009-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 Goodwin34877712009-10-26 19:32:42 +000058}
59
Bill Wendlinge4a41472010-07-15 19:41:20 +000060unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) {
David Goodwin34877712009-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 Goodwin87d21b92009-11-13 19:52:48 +000068void AggressiveAntiDepState::GetGroupRegs(
69 unsigned Group,
70 std::vector<unsigned> &Regs,
71 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
David Goodwin34877712009-10-26 19:32:42 +000072{
David Goodwin990d2852009-12-09 17:18:22 +000073 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
David Goodwin87d21b92009-11-13 19:52:48 +000074 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
David Goodwin34877712009-10-26 19:32:42 +000075 Regs.push_back(Reg);
76 }
77}
78
David Goodwine10deca2009-10-26 22:31:16 +000079unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2)
David Goodwin34877712009-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 Grosbach2973b572010-01-06 16:48:02 +000083
David Goodwin34877712009-10-26 19:32:42 +000084 // find group for each register
85 unsigned Group1 = GetGroup(Reg1);
86 unsigned Group2 = GetGroup(Reg2);
Jim Grosbach2973b572010-01-06 16:48:02 +000087
David Goodwin34877712009-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 Grosbach2973b572010-01-06 16:48:02 +000094
David Goodwine10deca2009-10-26 22:31:16 +000095unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg)
David Goodwin34877712009-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 Goodwine10deca2009-10-26 22:31:16 +0000106bool AggressiveAntiDepState::IsLive(unsigned Reg)
David Goodwin34877712009-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
David Goodwine10deca2009-10-26 22:31:16 +0000113
114
115AggressiveAntiDepBreaker::
David Goodwin0855dee2009-11-10 00:15:47 +0000116AggressiveAntiDepBreaker(MachineFunction& MFi,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000117 const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000118 TargetSubtargetInfo::RegClassVector& CriticalPathRCs) :
David Goodwine10deca2009-10-26 22:31:16 +0000119 AntiDepBreaker(), MF(MFi),
120 MRI(MF.getRegInfo()),
Evan Cheng46df4eb2010-06-16 07:35:02 +0000121 TII(MF.getTarget().getInstrInfo()),
David Goodwine10deca2009-10-26 22:31:16 +0000122 TRI(MF.getTarget().getRegisterInfo()),
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000123 RegClassInfo(RCI),
David Goodwin557bbe62009-11-20 19:32:48 +0000124 State(NULL) {
David Goodwin87d21b92009-11-13 19:52:48 +0000125 /* Collect a bitset of all registers that are only broken if they
126 are on the critical path. */
127 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
128 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
129 if (CriticalPathSet.none())
130 CriticalPathSet = CPSet;
131 else
132 CriticalPathSet |= CPSet;
133 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000134
David Greene5393b252009-12-24 00:14:25 +0000135 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000136 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwin87d21b92009-11-13 19:52:48 +0000137 r = CriticalPathSet.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000138 dbgs() << " " << TRI->getName(r));
139 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000140}
141
142AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
143 delete State;
David Goodwine10deca2009-10-26 22:31:16 +0000144}
145
146void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
147 assert(State == NULL);
David Goodwin990d2852009-12-09 17:18:22 +0000148 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine10deca2009-10-26 22:31:16 +0000149
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000150 bool IsReturnBlock = (!BB->empty() && BB->back().isReturn());
Bill Wendling38306d52010-07-15 18:43:09 +0000151 std::vector<unsigned> &KillIndices = State->GetKillIndices();
152 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwine10deca2009-10-26 22:31:16 +0000153
154 // Determine the live-out physregs for this block.
155 if (IsReturnBlock) {
156 // In a return block, examine the function live-out regs.
157 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
158 E = MRI.liveout_end(); I != E; ++I) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000159 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
160 unsigned Reg = *AI;
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000161 State->UnionGroups(Reg, 0);
162 KillIndices[Reg] = BB->size();
163 DefIndices[Reg] = ~0u;
David Goodwine10deca2009-10-26 22:31:16 +0000164 }
165 }
David Goodwine10deca2009-10-26 22:31:16 +0000166 }
167
Evan Cheng46df4eb2010-06-16 07:35:02 +0000168 // In a non-return block, examine the live-in regs of all successors.
169 // Note a return block can have successors if the return instruction is
170 // predicated.
171 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
172 SE = BB->succ_end(); SI != SE; ++SI)
173 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
174 E = (*SI)->livein_end(); I != E; ++I) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000175 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
176 unsigned Reg = *AI;
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000177 State->UnionGroups(Reg, 0);
178 KillIndices[Reg] = BB->size();
179 DefIndices[Reg] = ~0u;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000180 }
181 }
182
David Goodwine10deca2009-10-26 22:31:16 +0000183 // Mark live-out callee-saved registers. In a return block this is
184 // all callee-saved registers. In non-return this is any
185 // callee-saved register that is not saved in the prolog.
186 const MachineFrameInfo *MFI = MF.getFrameInfo();
187 BitVector Pristine = MFI->getPristineRegs(BB);
Craig Topper015f2282012-03-04 03:33:22 +0000188 for (const uint16_t *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
David Goodwine10deca2009-10-26 22:31:16 +0000189 unsigned Reg = *I;
190 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000191 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
192 unsigned AliasReg = *AI;
David Goodwine10deca2009-10-26 22:31:16 +0000193 State->UnionGroups(AliasReg, 0);
194 KillIndices[AliasReg] = BB->size();
195 DefIndices[AliasReg] = ~0u;
196 }
197 }
198}
199
200void AggressiveAntiDepBreaker::FinishBlock() {
201 delete State;
202 State = NULL;
David Goodwine10deca2009-10-26 22:31:16 +0000203}
204
205void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000206 unsigned InsertPosIndex) {
David Goodwine10deca2009-10-26 22:31:16 +0000207 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
208
David Goodwin5b3c3082009-10-29 23:30:59 +0000209 std::set<unsigned> PassthruRegs;
210 GetPassthruRegs(MI, PassthruRegs);
211 PrescanInstruction(MI, Count, PassthruRegs);
212 ScanInstruction(MI, Count);
213
David Greene5393b252009-12-24 00:14:25 +0000214 DEBUG(dbgs() << "Observe: ");
David Goodwine10deca2009-10-26 22:31:16 +0000215 DEBUG(MI->dump());
David Greene5393b252009-12-24 00:14:25 +0000216 DEBUG(dbgs() << "\tRegs:");
David Goodwine10deca2009-10-26 22:31:16 +0000217
Bill Wendling38306d52010-07-15 18:43:09 +0000218 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwin990d2852009-12-09 17:18:22 +0000219 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine10deca2009-10-26 22:31:16 +0000220 // If Reg is current live, then mark that it can't be renamed as
221 // we don't know the extent of its live-range anymore (now that it
222 // has been scheduled). If it is not live but was defined in the
223 // previous schedule region, then set its def index to the most
224 // conservative location (i.e. the beginning of the previous
225 // schedule region).
226 if (State->IsLive(Reg)) {
227 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbach2973b572010-01-06 16:48:02 +0000228 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine10deca2009-10-26 22:31:16 +0000229 State->GetGroup(Reg) << "->g0(region live-out)");
230 State->UnionGroups(Reg, 0);
Jim Grosbach2973b572010-01-06 16:48:02 +0000231 } else if ((DefIndices[Reg] < InsertPosIndex)
232 && (DefIndices[Reg] >= Count)) {
David Goodwine10deca2009-10-26 22:31:16 +0000233 DefIndices[Reg] = Count;
234 }
235 }
David Greene5393b252009-12-24 00:14:25 +0000236 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000237}
238
David Goodwin34877712009-10-26 19:32:42 +0000239bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000240 MachineOperand& MO)
David Goodwin34877712009-10-26 19:32:42 +0000241{
242 if (!MO.isReg() || !MO.isImplicit())
243 return false;
244
245 unsigned Reg = MO.getReg();
246 if (Reg == 0)
247 return false;
248
249 MachineOperand *Op = NULL;
250 if (MO.isDef())
251 Op = MI->findRegisterUseOperand(Reg, true);
252 else
253 Op = MI->findRegisterDefOperand(Reg);
254
255 return((Op != NULL) && Op->isImplicit());
256}
257
258void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
259 std::set<unsigned>& PassthruRegs) {
260 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
261 MachineOperand &MO = MI->getOperand(i);
262 if (!MO.isReg()) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000263 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
David Goodwin34877712009-10-26 19:32:42 +0000264 IsImplicitDefUse(MI, MO)) {
265 const unsigned Reg = MO.getReg();
266 PassthruRegs.insert(Reg);
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000267 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
268 PassthruRegs.insert(*SubRegs);
David Goodwin34877712009-10-26 19:32:42 +0000269 }
270 }
271}
272
David Goodwin557bbe62009-11-20 19:32:48 +0000273/// AntiDepEdges - Return in Edges the anti- and output- dependencies
274/// in SU that we want to consider for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000275static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin557bbe62009-11-20 19:32:48 +0000276 SmallSet<unsigned, 4> RegSet;
Dan Gohman66db3a02010-04-19 23:11:58 +0000277 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin34877712009-10-26 19:32:42 +0000278 P != PE; ++P) {
David Goodwin12dd99d2009-11-12 19:08:21 +0000279 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Goodwin34877712009-10-26 19:32:42 +0000280 unsigned Reg = P->getReg();
David Goodwin557bbe62009-11-20 19:32:48 +0000281 if (RegSet.count(Reg) == 0) {
David Goodwin34877712009-10-26 19:32:42 +0000282 Edges.push_back(&*P);
David Goodwin557bbe62009-11-20 19:32:48 +0000283 RegSet.insert(Reg);
David Goodwin34877712009-10-26 19:32:42 +0000284 }
285 }
286 }
287}
288
David Goodwin87d21b92009-11-13 19:52:48 +0000289/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
290/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000291static const SUnit *CriticalPathStep(const SUnit *SU) {
292 const SDep *Next = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000293 unsigned NextDepth = 0;
294 // Find the predecessor edge with the greatest depth.
295 if (SU != 0) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000296 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin87d21b92009-11-13 19:52:48 +0000297 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000298 const SUnit *PredSU = P->getSUnit();
David Goodwin87d21b92009-11-13 19:52:48 +0000299 unsigned PredLatency = P->getLatency();
300 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
301 // In the case of a latency tie, prefer an anti-dependency edge over
302 // other types of edges.
303 if (NextDepth < PredTotalLatency ||
304 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
305 NextDepth = PredTotalLatency;
306 Next = &*P;
307 }
308 }
309 }
310
311 return (Next) ? Next->getSUnit() : 0;
312}
313
David Goodwin67a8a7b2009-10-29 19:17:04 +0000314void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbach2973b572010-01-06 16:48:02 +0000315 const char *tag,
316 const char *header,
David Goodwin3e72d302009-11-19 23:12:37 +0000317 const char *footer) {
Bill Wendling38306d52010-07-15 18:43:09 +0000318 std::vector<unsigned> &KillIndices = State->GetKillIndices();
319 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000320 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin67a8a7b2009-10-29 19:17:04 +0000321 RegRefs = State->GetRegRefs();
322
323 if (!State->IsLive(Reg)) {
324 KillIndices[Reg] = KillIdx;
325 DefIndices[Reg] = ~0u;
326 RegRefs.erase(Reg);
327 State->LeaveGroup(Reg);
David Goodwin3e72d302009-11-19 23:12:37 +0000328 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000329 dbgs() << header << TRI->getName(Reg); header = NULL; });
330 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000331 }
332 // Repeat for subregisters.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000333 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
334 unsigned SubregReg = *SubRegs;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000335 if (!State->IsLive(SubregReg)) {
336 KillIndices[SubregReg] = KillIdx;
337 DefIndices[SubregReg] = ~0u;
338 RegRefs.erase(SubregReg);
339 State->LeaveGroup(SubregReg);
David Goodwin3e72d302009-11-19 23:12:37 +0000340 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000341 dbgs() << header << TRI->getName(Reg); header = NULL; });
342 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin67a8a7b2009-10-29 19:17:04 +0000343 State->GetGroup(SubregReg) << tag);
344 }
345 }
David Goodwin3e72d302009-11-19 23:12:37 +0000346
David Greene5393b252009-12-24 00:14:25 +0000347 DEBUG(if ((header == NULL) && (footer != NULL)) dbgs() << footer);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000348}
349
Jim Grosbach2973b572010-01-06 16:48:02 +0000350void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
351 unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000352 std::set<unsigned>& PassthruRegs) {
Bill Wendling38306d52010-07-15 18:43:09 +0000353 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000354 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000355 RegRefs = State->GetRegRefs();
356
David Goodwin67a8a7b2009-10-29 19:17:04 +0000357 // Handle dead defs by simulating a last-use of the register just
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000358 // after the def. A dead def can occur because the def is truly
David Goodwin67a8a7b2009-10-29 19:17:04 +0000359 // dead, or because only a subregister is live at the def. If we
360 // don't do this the dead def will be incorrectly merged into the
361 // previous def.
David Goodwin34877712009-10-26 19:32:42 +0000362 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
363 MachineOperand &MO = MI->getOperand(i);
364 if (!MO.isReg() || !MO.isDef()) continue;
365 unsigned Reg = MO.getReg();
366 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000367
David Goodwin3e72d302009-11-19 23:12:37 +0000368 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwin34877712009-10-26 19:32:42 +0000369 }
370
David Greene5393b252009-12-24 00:14:25 +0000371 DEBUG(dbgs() << "\tDef Groups:");
David Goodwin34877712009-10-26 19:32:42 +0000372 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
373 MachineOperand &MO = MI->getOperand(i);
374 if (!MO.isReg() || !MO.isDef()) continue;
375 unsigned Reg = MO.getReg();
376 if (Reg == 0) continue;
377
Jim Grosbach2973b572010-01-06 16:48:02 +0000378 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000379
David Goodwin67a8a7b2009-10-29 19:17:04 +0000380 // If MI's defs have a special allocation requirement, don't allow
David Goodwin34877712009-10-26 19:32:42 +0000381 // any def registers to be changed. Also assume all registers
382 // defined in a call must not be changed (ABI).
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000383 if (MI->isCall() || MI->hasExtraDefRegAllocReq() ||
Evan Cheng46df4eb2010-06-16 07:35:02 +0000384 TII->isPredicated(MI)) {
David Greene5393b252009-12-24 00:14:25 +0000385 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000386 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000387 }
388
389 // Any aliased that are live at this point are completely or
David Goodwin67a8a7b2009-10-29 19:17:04 +0000390 // partially defined here, so group those aliases with Reg.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000391 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
392 unsigned AliasReg = *AI;
David Goodwine10deca2009-10-26 22:31:16 +0000393 if (State->IsLive(AliasReg)) {
394 State->UnionGroups(Reg, AliasReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000395 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwin34877712009-10-26 19:32:42 +0000396 TRI->getName(AliasReg) << ")");
397 }
398 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000399
David Goodwin34877712009-10-26 19:32:42 +0000400 // Note register reference...
401 const TargetRegisterClass *RC = NULL;
402 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000403 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwine10deca2009-10-26 22:31:16 +0000404 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000405 RegRefs.insert(std::make_pair(Reg, RR));
406 }
407
David Greene5393b252009-12-24 00:14:25 +0000408 DEBUG(dbgs() << '\n');
David Goodwin67a8a7b2009-10-29 19:17:04 +0000409
410 // Scan the register defs for this instruction and update
411 // live-ranges.
412 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
413 MachineOperand &MO = MI->getOperand(i);
414 if (!MO.isReg() || !MO.isDef()) continue;
415 unsigned Reg = MO.getReg();
416 if (Reg == 0) continue;
David Goodwin3e72d302009-11-19 23:12:37 +0000417 // Ignore KILLs and passthru registers for liveness...
Chris Lattner518bb532010-02-09 19:54:29 +0000418 if (MI->isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwin3e72d302009-11-19 23:12:37 +0000419 continue;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000420
David Goodwin3e72d302009-11-19 23:12:37 +0000421 // Update def for Reg and aliases.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000422 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
423 DefIndices[*AI] = Count;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000424 }
David Goodwin34877712009-10-26 19:32:42 +0000425}
426
427void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000428 unsigned Count) {
David Greene5393b252009-12-24 00:14:25 +0000429 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000430 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000431 RegRefs = State->GetRegRefs();
David Goodwin34877712009-10-26 19:32:42 +0000432
Evan Cheng46df4eb2010-06-16 07:35:02 +0000433 // If MI's uses have special allocation requirement, don't allow
434 // any use registers to be changed. Also assume all registers
435 // used in a call must not be changed (ABI).
436 // FIXME: The issue with predicated instruction is more complex. We are being
437 // conservatively here because the kill markers cannot be trusted after
438 // if-conversion:
439 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
440 // ...
441 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
442 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
443 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
444 //
445 // The first R6 kill is not really a kill since it's killed by a predicated
446 // instruction which may not be executed. The second R6 def may or may not
447 // re-define R6 so it's not safe to change it since the last R6 use cannot be
448 // changed.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000449 bool Special = MI->isCall() ||
450 MI->hasExtraSrcRegAllocReq() ||
Evan Cheng46df4eb2010-06-16 07:35:02 +0000451 TII->isPredicated(MI);
452
David Goodwin34877712009-10-26 19:32:42 +0000453 // Scan the register uses for this instruction and update
454 // live-ranges, groups and RegRefs.
455 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
456 MachineOperand &MO = MI->getOperand(i);
457 if (!MO.isReg() || !MO.isUse()) continue;
458 unsigned Reg = MO.getReg();
459 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000460
461 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
462 State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000463
464 // It wasn't previously live but now it is, this is a kill. Forget
465 // the previous live-range information and start a new live-range
466 // for the register.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000467 HandleLastUse(Reg, Count, "(last-use)");
David Goodwin34877712009-10-26 19:32:42 +0000468
Evan Cheng46df4eb2010-06-16 07:35:02 +0000469 if (Special) {
David Greene5393b252009-12-24 00:14:25 +0000470 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000471 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000472 }
473
474 // Note register reference...
475 const TargetRegisterClass *RC = NULL;
476 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000477 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwine10deca2009-10-26 22:31:16 +0000478 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000479 RegRefs.insert(std::make_pair(Reg, RR));
480 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000481
David Greene5393b252009-12-24 00:14:25 +0000482 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000483
484 // Form a group of all defs and uses of a KILL instruction to ensure
485 // that all registers are renamed as a group.
Chris Lattner518bb532010-02-09 19:54:29 +0000486 if (MI->isKill()) {
David Greene5393b252009-12-24 00:14:25 +0000487 DEBUG(dbgs() << "\tKill Group:");
David Goodwin34877712009-10-26 19:32:42 +0000488
489 unsigned FirstReg = 0;
490 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
491 MachineOperand &MO = MI->getOperand(i);
492 if (!MO.isReg()) continue;
493 unsigned Reg = MO.getReg();
494 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000495
David Goodwin34877712009-10-26 19:32:42 +0000496 if (FirstReg != 0) {
David Greene5393b252009-12-24 00:14:25 +0000497 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine10deca2009-10-26 22:31:16 +0000498 State->UnionGroups(FirstReg, Reg);
David Goodwin34877712009-10-26 19:32:42 +0000499 } else {
David Greene5393b252009-12-24 00:14:25 +0000500 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000501 FirstReg = Reg;
502 }
503 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000504
David Greene5393b252009-12-24 00:14:25 +0000505 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000506 }
507}
508
509BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
510 BitVector BV(TRI->getNumRegs(), false);
511 bool first = true;
512
513 // Check all references that need rewriting for Reg. For each, use
514 // the corresponding register class to narrow the set of registers
515 // that are appropriate for renaming.
Jim Grosbach2973b572010-01-06 16:48:02 +0000516 std::pair<std::multimap<unsigned,
David Goodwine10deca2009-10-26 22:31:16 +0000517 AggressiveAntiDepState::RegisterReference>::iterator,
518 std::multimap<unsigned,
519 AggressiveAntiDepState::RegisterReference>::iterator>
520 Range = State->GetRegRefs().equal_range(Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000521 for (std::multimap<unsigned,
522 AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first,
523 QE = Range.second; Q != QE; ++Q) {
David Goodwin34877712009-10-26 19:32:42 +0000524 const TargetRegisterClass *RC = Q->second.RC;
525 if (RC == NULL) continue;
526
527 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
528 if (first) {
529 BV |= RCBV;
530 first = false;
531 } else {
532 BV &= RCBV;
533 }
534
David Greene5393b252009-12-24 00:14:25 +0000535 DEBUG(dbgs() << " " << RC->getName());
David Goodwin34877712009-10-26 19:32:42 +0000536 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000537
David Goodwin34877712009-10-26 19:32:42 +0000538 return BV;
Jim Grosbach2973b572010-01-06 16:48:02 +0000539}
David Goodwin34877712009-10-26 19:32:42 +0000540
541bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin54097832009-11-05 01:19:35 +0000542 unsigned AntiDepGroupIndex,
543 RenameOrderType& RenameOrder,
544 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendling38306d52010-07-15 18:43:09 +0000545 std::vector<unsigned> &KillIndices = State->GetKillIndices();
546 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000547 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000548 RegRefs = State->GetRegRefs();
549
David Goodwin87d21b92009-11-13 19:52:48 +0000550 // Collect all referenced registers in the same group as
551 // AntiDepReg. These all need to be renamed together if we are to
552 // break the anti-dependence.
David Goodwin34877712009-10-26 19:32:42 +0000553 std::vector<unsigned> Regs;
David Goodwin87d21b92009-11-13 19:52:48 +0000554 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwin34877712009-10-26 19:32:42 +0000555 assert(Regs.size() > 0 && "Empty register group!");
556 if (Regs.size() == 0)
557 return false;
558
559 // Find the "superest" register in the group. At the same time,
560 // collect the BitVector of registers that can be used to rename
561 // each register.
Jim Grosbach2973b572010-01-06 16:48:02 +0000562 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
563 << ":\n");
David Goodwin34877712009-10-26 19:32:42 +0000564 std::map<unsigned, BitVector> RenameRegisterMap;
565 unsigned SuperReg = 0;
566 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
567 unsigned Reg = Regs[i];
568 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
569 SuperReg = Reg;
570
571 // If Reg has any references, then collect possible rename regs
572 if (RegRefs.count(Reg) > 0) {
David Greene5393b252009-12-24 00:14:25 +0000573 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000574
David Goodwin34877712009-10-26 19:32:42 +0000575 BitVector BV = GetRenameRegisters(Reg);
576 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
577
David Greene5393b252009-12-24 00:14:25 +0000578 DEBUG(dbgs() << " ::");
David Goodwin34877712009-10-26 19:32:42 +0000579 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000580 dbgs() << " " << TRI->getName(r));
581 DEBUG(dbgs() << "\n");
David Goodwin34877712009-10-26 19:32:42 +0000582 }
583 }
584
585 // All group registers should be a subreg of SuperReg.
586 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
587 unsigned Reg = Regs[i];
588 if (Reg == SuperReg) continue;
589 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
590 assert(IsSub && "Expecting group subregister");
591 if (!IsSub)
592 return false;
593 }
594
David Goodwin00621ef2009-11-20 23:33:54 +0000595#ifndef NDEBUG
596 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
597 if (DebugDiv > 0) {
598 static int renamecnt = 0;
599 if (renamecnt++ % DebugDiv != DebugMod)
600 return false;
Jim Grosbach2973b572010-01-06 16:48:02 +0000601
David Greene5393b252009-12-24 00:14:25 +0000602 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin00621ef2009-11-20 23:33:54 +0000603 " for debug ***\n";
604 }
605#endif
606
David Goodwin54097832009-11-05 01:19:35 +0000607 // Check each possible rename register for SuperReg in round-robin
608 // order. If that register is available, and the corresponding
609 // registers are available for the other group subregisters, then we
610 // can use those registers to rename.
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000611
612 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
613 // check every use of the register and find the largest register class
614 // that can be used in all of them.
Jim Grosbach2973b572010-01-06 16:48:02 +0000615 const TargetRegisterClass *SuperRC =
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000616 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbach2973b572010-01-06 16:48:02 +0000617
Jakob Stoklund Olesen39b5c0c2012-11-29 03:34:17 +0000618 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000619 if (Order.empty()) {
David Greene5393b252009-12-24 00:14:25 +0000620 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin54097832009-11-05 01:19:35 +0000621 return false;
622 }
623
David Greene5393b252009-12-24 00:14:25 +0000624 DEBUG(dbgs() << "\tFind Registers:");
David Goodwin3e72d302009-11-19 23:12:37 +0000625
David Goodwin54097832009-11-05 01:19:35 +0000626 if (RenameOrder.count(SuperRC) == 0)
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000627 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
David Goodwin54097832009-11-05 01:19:35 +0000628
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000629 unsigned OrigR = RenameOrder[SuperRC];
630 unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
631 unsigned R = OrigR;
David Goodwin54097832009-11-05 01:19:35 +0000632 do {
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000633 if (R == 0) R = Order.size();
David Goodwin54097832009-11-05 01:19:35 +0000634 --R;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000635 const unsigned NewSuperReg = Order[R];
Jim Grosbach9b041c92010-09-02 17:12:55 +0000636 // Don't consider non-allocatable registers
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +0000637 if (!MRI.isAllocatable(NewSuperReg)) continue;
David Goodwin34877712009-10-26 19:32:42 +0000638 // Don't replace a register with itself.
David Goodwin00621ef2009-11-20 23:33:54 +0000639 if (NewSuperReg == SuperReg) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000640
David Greene5393b252009-12-24 00:14:25 +0000641 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin00621ef2009-11-20 23:33:54 +0000642 RenameMap.clear();
643
644 // For each referenced group register (which must be a SuperReg or
645 // a subregister of SuperReg), find the corresponding subregister
646 // of NewSuperReg and make sure it is free to be renamed.
647 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
648 unsigned Reg = Regs[i];
649 unsigned NewReg = 0;
650 if (Reg == SuperReg) {
651 NewReg = NewSuperReg;
652 } else {
653 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
654 if (NewSubRegIdx != 0)
655 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwin34877712009-10-26 19:32:42 +0000656 }
David Goodwin00621ef2009-11-20 23:33:54 +0000657
David Greene5393b252009-12-24 00:14:25 +0000658 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbach2973b572010-01-06 16:48:02 +0000659
David Goodwin00621ef2009-11-20 23:33:54 +0000660 // Check if Reg can be renamed to NewReg.
661 BitVector BV = RenameRegisterMap[Reg];
662 if (!BV.test(NewReg)) {
David Greene5393b252009-12-24 00:14:25 +0000663 DEBUG(dbgs() << "(no rename)");
David Goodwin00621ef2009-11-20 23:33:54 +0000664 goto next_super_reg;
665 }
666
667 // If NewReg is dead and NewReg's most recent def is not before
668 // Regs's kill, it's safe to replace Reg with NewReg. We
669 // must also check all aliases of NewReg, because we can't define a
670 // register when any sub or super is already live.
671 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene5393b252009-12-24 00:14:25 +0000672 DEBUG(dbgs() << "(live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000673 goto next_super_reg;
674 } else {
675 bool found = false;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000676 for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
677 unsigned AliasReg = *AI;
Jim Grosbach2973b572010-01-06 16:48:02 +0000678 if (State->IsLive(AliasReg) ||
679 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene5393b252009-12-24 00:14:25 +0000680 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000681 found = true;
682 break;
683 }
684 }
685 if (found)
686 goto next_super_reg;
687 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000688
David Goodwin00621ef2009-11-20 23:33:54 +0000689 // Record that 'Reg' can be renamed to 'NewReg'.
690 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwin34877712009-10-26 19:32:42 +0000691 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000692
David Goodwin00621ef2009-11-20 23:33:54 +0000693 // If we fall-out here, then every register in the group can be
694 // renamed, as recorded in RenameMap.
695 RenameOrder.erase(SuperRC);
696 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene5393b252009-12-24 00:14:25 +0000697 DEBUG(dbgs() << "]\n");
David Goodwin00621ef2009-11-20 23:33:54 +0000698 return true;
699
700 next_super_reg:
David Greene5393b252009-12-24 00:14:25 +0000701 DEBUG(dbgs() << ']');
David Goodwin54097832009-11-05 01:19:35 +0000702 } while (R != EndR);
David Goodwin34877712009-10-26 19:32:42 +0000703
David Greene5393b252009-12-24 00:14:25 +0000704 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000705
706 // No registers are free and available!
707 return false;
708}
709
710/// BreakAntiDependencies - Identifiy anti-dependencies within the
711/// ScheduleDAG and break them by renaming registers.
712///
David Goodwine10deca2009-10-26 22:31:16 +0000713unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman66db3a02010-04-19 23:11:58 +0000714 const std::vector<SUnit>& SUnits,
715 MachineBasicBlock::iterator Begin,
716 MachineBasicBlock::iterator End,
Devang Patele29e8e12011-06-02 21:26:52 +0000717 unsigned InsertPosIndex,
718 DbgValueVector &DbgValues) {
719
Bill Wendling38306d52010-07-15 18:43:09 +0000720 std::vector<unsigned> &KillIndices = State->GetKillIndices();
721 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000722 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000723 RegRefs = State->GetRegRefs();
724
David Goodwin34877712009-10-26 19:32:42 +0000725 // The code below assumes that there is at least one instruction,
726 // so just duck out immediately if the block is empty.
David Goodwin4de099d2009-11-03 20:57:50 +0000727 if (SUnits.empty()) return 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000728
David Goodwin54097832009-11-05 01:19:35 +0000729 // For each regclass the next register to use for renaming.
730 RenameOrderType RenameOrder;
David Goodwin34877712009-10-26 19:32:42 +0000731
732 // ...need a map from MI to SUnit.
Dan Gohman66db3a02010-04-19 23:11:58 +0000733 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwin34877712009-10-26 19:32:42 +0000734 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000735 const SUnit *SU = &SUnits[i];
736 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
737 SU));
David Goodwin34877712009-10-26 19:32:42 +0000738 }
739
David Goodwin87d21b92009-11-13 19:52:48 +0000740 // Track progress along the critical path through the SUnit graph as
741 // we walk the instructions. This is needed for regclasses that only
742 // break critical-path anti-dependencies.
Dan Gohman66db3a02010-04-19 23:11:58 +0000743 const SUnit *CriticalPathSU = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000744 MachineInstr *CriticalPathMI = 0;
745 if (CriticalPathSet.any()) {
746 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000747 const SUnit *SU = &SUnits[i];
Jim Grosbach2973b572010-01-06 16:48:02 +0000748 if (!CriticalPathSU ||
749 ((SU->getDepth() + SU->Latency) >
David Goodwin87d21b92009-11-13 19:52:48 +0000750 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
751 CriticalPathSU = SU;
752 }
753 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000754
David Goodwin87d21b92009-11-13 19:52:48 +0000755 CriticalPathMI = CriticalPathSU->getInstr();
756 }
757
Jim Grosbach2973b572010-01-06 16:48:02 +0000758#ifndef NDEBUG
David Greene5393b252009-12-24 00:14:25 +0000759 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
760 DEBUG(dbgs() << "Available regs:");
David Goodwin557bbe62009-11-20 19:32:48 +0000761 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
762 if (!State->IsLive(Reg))
David Greene5393b252009-12-24 00:14:25 +0000763 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000764 }
David Greene5393b252009-12-24 00:14:25 +0000765 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000766#endif
767
768 // Attempt to break anti-dependence edges. Walk the instructions
769 // from the bottom up, tracking information about liveness as we go
770 // to help determine which registers are available.
771 unsigned Broken = 0;
772 unsigned Count = InsertPosIndex - 1;
773 for (MachineBasicBlock::iterator I = End, E = Begin;
774 I != E; --Count) {
775 MachineInstr *MI = --I;
776
Hal Finkel504d1d22012-01-16 22:53:41 +0000777 if (MI->isDebugValue())
778 continue;
779
David Greene5393b252009-12-24 00:14:25 +0000780 DEBUG(dbgs() << "Anti: ");
David Goodwin34877712009-10-26 19:32:42 +0000781 DEBUG(MI->dump());
782
783 std::set<unsigned> PassthruRegs;
784 GetPassthruRegs(MI, PassthruRegs);
785
786 // Process the defs in MI...
787 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbach2973b572010-01-06 16:48:02 +0000788
David Goodwin557bbe62009-11-20 19:32:48 +0000789 // The dependence edges that represent anti- and output-
David Goodwin87d21b92009-11-13 19:52:48 +0000790 // dependencies that are candidates for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000791 std::vector<const SDep *> Edges;
792 const SUnit *PathSU = MISUnitMap[MI];
David Goodwin557bbe62009-11-20 19:32:48 +0000793 AntiDepEdges(PathSU, Edges);
David Goodwin87d21b92009-11-13 19:52:48 +0000794
795 // If MI is not on the critical path, then we don't rename
796 // registers in the CriticalPathSet.
797 BitVector *ExcludeRegs = NULL;
798 if (MI == CriticalPathMI) {
799 CriticalPathSU = CriticalPathStep(CriticalPathSU);
800 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000801 } else {
David Goodwin87d21b92009-11-13 19:52:48 +0000802 ExcludeRegs = &CriticalPathSet;
803 }
804
David Goodwin34877712009-10-26 19:32:42 +0000805 // Ignore KILL instructions (they form a group in ScanInstruction
806 // but don't cause any anti-dependence breaking themselves)
Chris Lattner518bb532010-02-09 19:54:29 +0000807 if (!MI->isKill()) {
David Goodwin34877712009-10-26 19:32:42 +0000808 // Attempt to break each anti-dependency...
809 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000810 const SDep *Edge = Edges[i];
David Goodwin34877712009-10-26 19:32:42 +0000811 SUnit *NextSU = Edge->getSUnit();
Jim Grosbach2973b572010-01-06 16:48:02 +0000812
David Goodwin12dd99d2009-11-12 19:08:21 +0000813 if ((Edge->getKind() != SDep::Anti) &&
814 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000815
David Goodwin34877712009-10-26 19:32:42 +0000816 unsigned AntiDepReg = Edge->getReg();
David Greene5393b252009-12-24 00:14:25 +0000817 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwin34877712009-10-26 19:32:42 +0000818 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbach2973b572010-01-06 16:48:02 +0000819
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +0000820 if (!MRI.isAllocatable(AntiDepReg)) {
David Goodwin34877712009-10-26 19:32:42 +0000821 // Don't break anti-dependencies on non-allocatable registers.
David Greene5393b252009-12-24 00:14:25 +0000822 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwin34877712009-10-26 19:32:42 +0000823 continue;
David Goodwin87d21b92009-11-13 19:52:48 +0000824 } else if ((ExcludeRegs != NULL) && ExcludeRegs->test(AntiDepReg)) {
825 // Don't break anti-dependencies for critical path registers
826 // if not on the critical path
David Greene5393b252009-12-24 00:14:25 +0000827 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwin87d21b92009-11-13 19:52:48 +0000828 continue;
David Goodwin34877712009-10-26 19:32:42 +0000829 } else if (PassthruRegs.count(AntiDepReg) != 0) {
830 // If the anti-dep register liveness "passes-thru", then
831 // don't try to change it. It will be changed along with
832 // the use if required to break an earlier antidep.
David Greene5393b252009-12-24 00:14:25 +0000833 DEBUG(dbgs() << " (passthru)\n");
David Goodwin34877712009-10-26 19:32:42 +0000834 continue;
835 } else {
836 // No anti-dep breaking for implicit deps
837 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000838 assert(AntiDepOp != NULL &&
839 "Can't find index for defined register operand");
David Goodwin34877712009-10-26 19:32:42 +0000840 if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) {
David Greene5393b252009-12-24 00:14:25 +0000841 DEBUG(dbgs() << " (implicit)\n");
David Goodwin34877712009-10-26 19:32:42 +0000842 continue;
843 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000844
David Goodwin34877712009-10-26 19:32:42 +0000845 // If the SUnit has other dependencies on the SUnit that
846 // it anti-depends on, don't bother breaking the
847 // anti-dependency since those edges would prevent such
848 // units from being scheduled past each other
849 // regardless.
David Goodwin557bbe62009-11-20 19:32:48 +0000850 //
851 // Also, if there are dependencies on other SUnits with the
852 // same register as the anti-dependency, don't attempt to
853 // break it.
Dan Gohman66db3a02010-04-19 23:11:58 +0000854 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin34877712009-10-26 19:32:42 +0000855 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin557bbe62009-11-20 19:32:48 +0000856 if (P->getSUnit() == NextSU ?
857 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
858 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
859 AntiDepReg = 0;
860 break;
861 }
862 }
Dan Gohman66db3a02010-04-19 23:11:58 +0000863 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin557bbe62009-11-20 19:32:48 +0000864 PE = PathSU->Preds.end(); P != PE; ++P) {
865 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
866 (P->getKind() != SDep::Output)) {
David Greene5393b252009-12-24 00:14:25 +0000867 DEBUG(dbgs() << " (real dependency)\n");
David Goodwin34877712009-10-26 19:32:42 +0000868 AntiDepReg = 0;
869 break;
Jim Grosbach2973b572010-01-06 16:48:02 +0000870 } else if ((P->getSUnit() != NextSU) &&
871 (P->getKind() == SDep::Data) &&
David Goodwin557bbe62009-11-20 19:32:48 +0000872 (P->getReg() == AntiDepReg)) {
David Greene5393b252009-12-24 00:14:25 +0000873 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin557bbe62009-11-20 19:32:48 +0000874 AntiDepReg = 0;
875 break;
David Goodwin34877712009-10-26 19:32:42 +0000876 }
877 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000878
David Goodwin34877712009-10-26 19:32:42 +0000879 if (AntiDepReg == 0) continue;
880 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000881
David Goodwin34877712009-10-26 19:32:42 +0000882 assert(AntiDepReg != 0);
883 if (AntiDepReg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000884
David Goodwin34877712009-10-26 19:32:42 +0000885 // Determine AntiDepReg's register group.
David Goodwine10deca2009-10-26 22:31:16 +0000886 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwin34877712009-10-26 19:32:42 +0000887 if (GroupIndex == 0) {
David Greene5393b252009-12-24 00:14:25 +0000888 DEBUG(dbgs() << " (zero group)\n");
David Goodwin34877712009-10-26 19:32:42 +0000889 continue;
890 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000891
David Greene5393b252009-12-24 00:14:25 +0000892 DEBUG(dbgs() << '\n');
Jim Grosbach2973b572010-01-06 16:48:02 +0000893
David Goodwin34877712009-10-26 19:32:42 +0000894 // Look for a suitable register to use to break the anti-dependence.
895 std::map<unsigned, unsigned> RenameMap;
David Goodwin54097832009-11-05 01:19:35 +0000896 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene5393b252009-12-24 00:14:25 +0000897 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwin34877712009-10-26 19:32:42 +0000898 << TRI->getName(AntiDepReg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000899
David Goodwin34877712009-10-26 19:32:42 +0000900 // Handle each group register...
901 for (std::map<unsigned, unsigned>::iterator
902 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
903 unsigned CurrReg = S->first;
904 unsigned NewReg = S->second;
Jim Grosbach2973b572010-01-06 16:48:02 +0000905
906 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
907 TRI->getName(NewReg) << "(" <<
David Goodwin34877712009-10-26 19:32:42 +0000908 RegRefs.count(CurrReg) << " refs)");
Jim Grosbach2973b572010-01-06 16:48:02 +0000909
David Goodwin34877712009-10-26 19:32:42 +0000910 // Update the references to the old register CurrReg to
911 // refer to the new register NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000912 std::pair<std::multimap<unsigned,
913 AggressiveAntiDepState::RegisterReference>::iterator,
David Goodwine10deca2009-10-26 22:31:16 +0000914 std::multimap<unsigned,
Jim Grosbach2973b572010-01-06 16:48:02 +0000915 AggressiveAntiDepState::RegisterReference>::iterator>
David Goodwin34877712009-10-26 19:32:42 +0000916 Range = RegRefs.equal_range(CurrReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000917 for (std::multimap<unsigned,
918 AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000919 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
920 Q->second.Operand->setReg(NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000921 // If the SU for the instruction being updated has debug
922 // information related to the anti-dependency register, make
923 // sure to update that as well.
924 const SUnit *SU = MISUnitMap[Q->second.Operand->getParent()];
Jim Grosbach086723d2010-06-02 15:29:36 +0000925 if (!SU) continue;
Devang Patele29e8e12011-06-02 21:26:52 +0000926 for (DbgValueVector::iterator DVI = DbgValues.begin(),
927 DVE = DbgValues.end(); DVI != DVE; ++DVI)
928 if (DVI->second == Q->second.Operand->getParent())
929 UpdateDbgValue(DVI->first, AntiDepReg, NewReg);
David Goodwin34877712009-10-26 19:32:42 +0000930 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000931
David Goodwin34877712009-10-26 19:32:42 +0000932 // We just went back in time and modified history; the
933 // liveness information for CurrReg is now inconsistent. Set
934 // the state as if it were dead.
David Goodwine10deca2009-10-26 22:31:16 +0000935 State->UnionGroups(NewReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000936 RegRefs.erase(NewReg);
937 DefIndices[NewReg] = DefIndices[CurrReg];
938 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbach2973b572010-01-06 16:48:02 +0000939
David Goodwine10deca2009-10-26 22:31:16 +0000940 State->UnionGroups(CurrReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000941 RegRefs.erase(CurrReg);
942 DefIndices[CurrReg] = KillIndices[CurrReg];
943 KillIndices[CurrReg] = ~0u;
944 assert(((KillIndices[CurrReg] == ~0u) !=
945 (DefIndices[CurrReg] == ~0u)) &&
946 "Kill and Def maps aren't consistent for AntiDepReg!");
947 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000948
David Goodwin34877712009-10-26 19:32:42 +0000949 ++Broken;
David Greene5393b252009-12-24 00:14:25 +0000950 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000951 }
952 }
953 }
954
955 ScanInstruction(MI, Count);
956 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000957
David Goodwin34877712009-10-26 19:32:42 +0000958 return Broken;
959}