blob: 5c809f7fd668d62499d8ccfb085a5a5fa3ccf398 [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"
22#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Evan Cheng46df4eb2010-06-16 07:35:02 +000024#include "llvm/Target/TargetInstrInfo.h"
Bill Wendling75a5b712010-07-15 06:05:18 +000025#include "llvm/Target/TargetRegisterInfo.h"
David Goodwine10deca2009-10-26 22:31:16 +000026#include "llvm/Support/CommandLine.h"
David Goodwin34877712009-10-26 19:32:42 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.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,
Jim Grosbach2973b572010-01-06 16:48:02 +0000117 TargetSubtarget::RegClassVector& CriticalPathRCs) :
David Goodwine10deca2009-10-26 22:31:16 +0000118 AntiDepBreaker(), MF(MFi),
119 MRI(MF.getRegInfo()),
Evan Cheng46df4eb2010-06-16 07:35:02 +0000120 TII(MF.getTarget().getInstrInfo()),
David Goodwine10deca2009-10-26 22:31:16 +0000121 TRI(MF.getTarget().getRegisterInfo()),
122 AllocatableSet(TRI->getAllocatableSet(MF)),
David Goodwin557bbe62009-11-20 19:32:48 +0000123 State(NULL) {
David Goodwin87d21b92009-11-13 19:52:48 +0000124 /* Collect a bitset of all registers that are only broken if they
125 are on the critical path. */
126 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
127 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
128 if (CriticalPathSet.none())
129 CriticalPathSet = CPSet;
130 else
131 CriticalPathSet |= CPSet;
132 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000133
David Greene5393b252009-12-24 00:14:25 +0000134 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000135 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwin87d21b92009-11-13 19:52:48 +0000136 r = CriticalPathSet.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000137 dbgs() << " " << TRI->getName(r));
138 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000139}
140
141AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
142 delete State;
David Goodwine10deca2009-10-26 22:31:16 +0000143}
144
145void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
146 assert(State == NULL);
David Goodwin990d2852009-12-09 17:18:22 +0000147 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine10deca2009-10-26 22:31:16 +0000148
149 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
Bill Wendling38306d52010-07-15 18:43:09 +0000150 std::vector<unsigned> &KillIndices = State->GetKillIndices();
151 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwine10deca2009-10-26 22:31:16 +0000152
153 // Determine the live-out physregs for this block.
154 if (IsReturnBlock) {
155 // In a return block, examine the function live-out regs.
156 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
157 E = MRI.liveout_end(); I != E; ++I) {
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000158 for (const unsigned *Alias = TRI->getOverlaps(*I);
159 unsigned Reg = *Alias; ++Alias) {
160 State->UnionGroups(Reg, 0);
161 KillIndices[Reg] = BB->size();
162 DefIndices[Reg] = ~0u;
David Goodwine10deca2009-10-26 22:31:16 +0000163 }
164 }
David Goodwine10deca2009-10-26 22:31:16 +0000165 }
166
Evan Cheng46df4eb2010-06-16 07:35:02 +0000167 // In a non-return block, examine the live-in regs of all successors.
168 // Note a return block can have successors if the return instruction is
169 // predicated.
170 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
171 SE = BB->succ_end(); SI != SE; ++SI)
172 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
173 E = (*SI)->livein_end(); I != E; ++I) {
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000174 for (const unsigned *Alias = TRI->getOverlaps(*I);
175 unsigned Reg = *Alias; ++Alias) {
176 State->UnionGroups(Reg, 0);
177 KillIndices[Reg] = BB->size();
178 DefIndices[Reg] = ~0u;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000179 }
180 }
181
David Goodwine10deca2009-10-26 22:31:16 +0000182 // Mark live-out callee-saved registers. In a return block this is
183 // all callee-saved registers. In non-return this is any
184 // callee-saved register that is not saved in the prolog.
185 const MachineFrameInfo *MFI = MF.getFrameInfo();
186 BitVector Pristine = MFI->getPristineRegs(BB);
187 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
188 unsigned Reg = *I;
189 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000190 for (const unsigned *Alias = TRI->getOverlaps(Reg);
191 unsigned AliasReg = *Alias; ++Alias) {
David Goodwine10deca2009-10-26 22:31:16 +0000192 State->UnionGroups(AliasReg, 0);
193 KillIndices[AliasReg] = BB->size();
194 DefIndices[AliasReg] = ~0u;
195 }
196 }
197}
198
199void AggressiveAntiDepBreaker::FinishBlock() {
200 delete State;
201 State = NULL;
David Goodwine10deca2009-10-26 22:31:16 +0000202}
203
204void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000205 unsigned InsertPosIndex) {
David Goodwine10deca2009-10-26 22:31:16 +0000206 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
207
David Goodwin5b3c3082009-10-29 23:30:59 +0000208 std::set<unsigned> PassthruRegs;
209 GetPassthruRegs(MI, PassthruRegs);
210 PrescanInstruction(MI, Count, PassthruRegs);
211 ScanInstruction(MI, Count);
212
David Greene5393b252009-12-24 00:14:25 +0000213 DEBUG(dbgs() << "Observe: ");
David Goodwine10deca2009-10-26 22:31:16 +0000214 DEBUG(MI->dump());
David Greene5393b252009-12-24 00:14:25 +0000215 DEBUG(dbgs() << "\tRegs:");
David Goodwine10deca2009-10-26 22:31:16 +0000216
Bill Wendling38306d52010-07-15 18:43:09 +0000217 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwin990d2852009-12-09 17:18:22 +0000218 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine10deca2009-10-26 22:31:16 +0000219 // If Reg is current live, then mark that it can't be renamed as
220 // we don't know the extent of its live-range anymore (now that it
221 // has been scheduled). If it is not live but was defined in the
222 // previous schedule region, then set its def index to the most
223 // conservative location (i.e. the beginning of the previous
224 // schedule region).
225 if (State->IsLive(Reg)) {
226 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbach2973b572010-01-06 16:48:02 +0000227 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine10deca2009-10-26 22:31:16 +0000228 State->GetGroup(Reg) << "->g0(region live-out)");
229 State->UnionGroups(Reg, 0);
Jim Grosbach2973b572010-01-06 16:48:02 +0000230 } else if ((DefIndices[Reg] < InsertPosIndex)
231 && (DefIndices[Reg] >= Count)) {
David Goodwine10deca2009-10-26 22:31:16 +0000232 DefIndices[Reg] = Count;
233 }
234 }
David Greene5393b252009-12-24 00:14:25 +0000235 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000236}
237
David Goodwin34877712009-10-26 19:32:42 +0000238bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000239 MachineOperand& MO)
David Goodwin34877712009-10-26 19:32:42 +0000240{
241 if (!MO.isReg() || !MO.isImplicit())
242 return false;
243
244 unsigned Reg = MO.getReg();
245 if (Reg == 0)
246 return false;
247
248 MachineOperand *Op = NULL;
249 if (MO.isDef())
250 Op = MI->findRegisterUseOperand(Reg, true);
251 else
252 Op = MI->findRegisterDefOperand(Reg);
253
254 return((Op != NULL) && Op->isImplicit());
255}
256
257void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
258 std::set<unsigned>& PassthruRegs) {
259 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
260 MachineOperand &MO = MI->getOperand(i);
261 if (!MO.isReg()) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000262 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
David Goodwin34877712009-10-26 19:32:42 +0000263 IsImplicitDefUse(MI, MO)) {
264 const unsigned Reg = MO.getReg();
265 PassthruRegs.insert(Reg);
266 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
267 *Subreg; ++Subreg) {
268 PassthruRegs.insert(*Subreg);
269 }
270 }
271 }
272}
273
David Goodwin557bbe62009-11-20 19:32:48 +0000274/// AntiDepEdges - Return in Edges the anti- and output- dependencies
275/// in SU that we want to consider for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000276static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin557bbe62009-11-20 19:32:48 +0000277 SmallSet<unsigned, 4> RegSet;
Dan Gohman66db3a02010-04-19 23:11:58 +0000278 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin34877712009-10-26 19:32:42 +0000279 P != PE; ++P) {
David Goodwin12dd99d2009-11-12 19:08:21 +0000280 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Goodwin34877712009-10-26 19:32:42 +0000281 unsigned Reg = P->getReg();
David Goodwin557bbe62009-11-20 19:32:48 +0000282 if (RegSet.count(Reg) == 0) {
David Goodwin34877712009-10-26 19:32:42 +0000283 Edges.push_back(&*P);
David Goodwin557bbe62009-11-20 19:32:48 +0000284 RegSet.insert(Reg);
David Goodwin34877712009-10-26 19:32:42 +0000285 }
286 }
287 }
288}
289
David Goodwin87d21b92009-11-13 19:52:48 +0000290/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
291/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000292static const SUnit *CriticalPathStep(const SUnit *SU) {
293 const SDep *Next = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000294 unsigned NextDepth = 0;
295 // Find the predecessor edge with the greatest depth.
296 if (SU != 0) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000297 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin87d21b92009-11-13 19:52:48 +0000298 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000299 const SUnit *PredSU = P->getSUnit();
David Goodwin87d21b92009-11-13 19:52:48 +0000300 unsigned PredLatency = P->getLatency();
301 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
302 // In the case of a latency tie, prefer an anti-dependency edge over
303 // other types of edges.
304 if (NextDepth < PredTotalLatency ||
305 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
306 NextDepth = PredTotalLatency;
307 Next = &*P;
308 }
309 }
310 }
311
312 return (Next) ? Next->getSUnit() : 0;
313}
314
David Goodwin67a8a7b2009-10-29 19:17:04 +0000315void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbach2973b572010-01-06 16:48:02 +0000316 const char *tag,
317 const char *header,
David Goodwin3e72d302009-11-19 23:12:37 +0000318 const char *footer) {
Bill Wendling38306d52010-07-15 18:43:09 +0000319 std::vector<unsigned> &KillIndices = State->GetKillIndices();
320 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000321 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin67a8a7b2009-10-29 19:17:04 +0000322 RegRefs = State->GetRegRefs();
323
324 if (!State->IsLive(Reg)) {
325 KillIndices[Reg] = KillIdx;
326 DefIndices[Reg] = ~0u;
327 RegRefs.erase(Reg);
328 State->LeaveGroup(Reg);
David Goodwin3e72d302009-11-19 23:12:37 +0000329 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000330 dbgs() << header << TRI->getName(Reg); header = NULL; });
331 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000332 }
333 // Repeat for subregisters.
334 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
335 *Subreg; ++Subreg) {
336 unsigned SubregReg = *Subreg;
337 if (!State->IsLive(SubregReg)) {
338 KillIndices[SubregReg] = KillIdx;
339 DefIndices[SubregReg] = ~0u;
340 RegRefs.erase(SubregReg);
341 State->LeaveGroup(SubregReg);
David Goodwin3e72d302009-11-19 23:12:37 +0000342 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000343 dbgs() << header << TRI->getName(Reg); header = NULL; });
344 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin67a8a7b2009-10-29 19:17:04 +0000345 State->GetGroup(SubregReg) << tag);
346 }
347 }
David Goodwin3e72d302009-11-19 23:12:37 +0000348
David Greene5393b252009-12-24 00:14:25 +0000349 DEBUG(if ((header == NULL) && (footer != NULL)) dbgs() << footer);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000350}
351
Jim Grosbach2973b572010-01-06 16:48:02 +0000352void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
353 unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000354 std::set<unsigned>& PassthruRegs) {
Bill Wendling38306d52010-07-15 18:43:09 +0000355 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000356 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000357 RegRefs = State->GetRegRefs();
358
David Goodwin67a8a7b2009-10-29 19:17:04 +0000359 // Handle dead defs by simulating a last-use of the register just
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000360 // after the def. A dead def can occur because the def is truly
David Goodwin67a8a7b2009-10-29 19:17:04 +0000361 // dead, or because only a subregister is live at the def. If we
362 // don't do this the dead def will be incorrectly merged into the
363 // previous def.
David Goodwin34877712009-10-26 19:32:42 +0000364 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
365 MachineOperand &MO = MI->getOperand(i);
366 if (!MO.isReg() || !MO.isDef()) continue;
367 unsigned Reg = MO.getReg();
368 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000369
David Goodwin3e72d302009-11-19 23:12:37 +0000370 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwin34877712009-10-26 19:32:42 +0000371 }
372
David Greene5393b252009-12-24 00:14:25 +0000373 DEBUG(dbgs() << "\tDef Groups:");
David Goodwin34877712009-10-26 19:32:42 +0000374 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
375 MachineOperand &MO = MI->getOperand(i);
376 if (!MO.isReg() || !MO.isDef()) continue;
377 unsigned Reg = MO.getReg();
378 if (Reg == 0) continue;
379
Jim Grosbach2973b572010-01-06 16:48:02 +0000380 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000381
David Goodwin67a8a7b2009-10-29 19:17:04 +0000382 // If MI's defs have a special allocation requirement, don't allow
David Goodwin34877712009-10-26 19:32:42 +0000383 // any def registers to be changed. Also assume all registers
384 // defined in a call must not be changed (ABI).
Evan Cheng46df4eb2010-06-16 07:35:02 +0000385 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq() ||
386 TII->isPredicated(MI)) {
David Greene5393b252009-12-24 00:14:25 +0000387 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000388 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000389 }
390
391 // Any aliased that are live at this point are completely or
David Goodwin67a8a7b2009-10-29 19:17:04 +0000392 // partially defined here, so group those aliases with Reg.
David Goodwin34877712009-10-26 19:32:42 +0000393 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
394 unsigned AliasReg = *Alias;
David Goodwine10deca2009-10-26 22:31:16 +0000395 if (State->IsLive(AliasReg)) {
396 State->UnionGroups(Reg, AliasReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000397 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwin34877712009-10-26 19:32:42 +0000398 TRI->getName(AliasReg) << ")");
399 }
400 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000401
David Goodwin34877712009-10-26 19:32:42 +0000402 // Note register reference...
403 const TargetRegisterClass *RC = NULL;
404 if (i < MI->getDesc().getNumOperands())
405 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000406 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000407 RegRefs.insert(std::make_pair(Reg, RR));
408 }
409
David Greene5393b252009-12-24 00:14:25 +0000410 DEBUG(dbgs() << '\n');
David Goodwin67a8a7b2009-10-29 19:17:04 +0000411
412 // Scan the register defs for this instruction and update
413 // live-ranges.
414 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
415 MachineOperand &MO = MI->getOperand(i);
416 if (!MO.isReg() || !MO.isDef()) continue;
417 unsigned Reg = MO.getReg();
418 if (Reg == 0) continue;
David Goodwin3e72d302009-11-19 23:12:37 +0000419 // Ignore KILLs and passthru registers for liveness...
Chris Lattner518bb532010-02-09 19:54:29 +0000420 if (MI->isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwin3e72d302009-11-19 23:12:37 +0000421 continue;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000422
David Goodwin3e72d302009-11-19 23:12:37 +0000423 // Update def for Reg and aliases.
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000424 for (const unsigned *Alias = TRI->getOverlaps(Reg);
425 unsigned AliasReg = *Alias; ++Alias)
David Goodwin3e72d302009-11-19 23:12:37 +0000426 DefIndices[AliasReg] = Count;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000427 }
David Goodwin34877712009-10-26 19:32:42 +0000428}
429
430void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000431 unsigned Count) {
David Greene5393b252009-12-24 00:14:25 +0000432 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000433 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000434 RegRefs = State->GetRegRefs();
David Goodwin34877712009-10-26 19:32:42 +0000435
Evan Cheng46df4eb2010-06-16 07:35:02 +0000436 // If MI's uses have special allocation requirement, don't allow
437 // any use registers to be changed. Also assume all registers
438 // used in a call must not be changed (ABI).
439 // FIXME: The issue with predicated instruction is more complex. We are being
440 // conservatively here because the kill markers cannot be trusted after
441 // if-conversion:
442 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
443 // ...
444 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
445 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
446 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
447 //
448 // The first R6 kill is not really a kill since it's killed by a predicated
449 // instruction which may not be executed. The second R6 def may or may not
450 // re-define R6 so it's not safe to change it since the last R6 use cannot be
451 // changed.
452 bool Special = MI->getDesc().isCall() ||
453 MI->getDesc().hasExtraSrcRegAllocReq() ||
454 TII->isPredicated(MI);
455
David Goodwin34877712009-10-26 19:32:42 +0000456 // Scan the register uses for this instruction and update
457 // live-ranges, groups and RegRefs.
458 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
459 MachineOperand &MO = MI->getOperand(i);
460 if (!MO.isReg() || !MO.isUse()) continue;
461 unsigned Reg = MO.getReg();
462 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000463
464 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
465 State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000466
467 // It wasn't previously live but now it is, this is a kill. Forget
468 // the previous live-range information and start a new live-range
469 // for the register.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000470 HandleLastUse(Reg, Count, "(last-use)");
David Goodwin34877712009-10-26 19:32:42 +0000471
Evan Cheng46df4eb2010-06-16 07:35:02 +0000472 if (Special) {
David Greene5393b252009-12-24 00:14:25 +0000473 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000474 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000475 }
476
477 // Note register reference...
478 const TargetRegisterClass *RC = NULL;
479 if (i < MI->getDesc().getNumOperands())
480 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000481 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000482 RegRefs.insert(std::make_pair(Reg, RR));
483 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000484
David Greene5393b252009-12-24 00:14:25 +0000485 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000486
487 // Form a group of all defs and uses of a KILL instruction to ensure
488 // that all registers are renamed as a group.
Chris Lattner518bb532010-02-09 19:54:29 +0000489 if (MI->isKill()) {
David Greene5393b252009-12-24 00:14:25 +0000490 DEBUG(dbgs() << "\tKill Group:");
David Goodwin34877712009-10-26 19:32:42 +0000491
492 unsigned FirstReg = 0;
493 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
494 MachineOperand &MO = MI->getOperand(i);
495 if (!MO.isReg()) continue;
496 unsigned Reg = MO.getReg();
497 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000498
David Goodwin34877712009-10-26 19:32:42 +0000499 if (FirstReg != 0) {
David Greene5393b252009-12-24 00:14:25 +0000500 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine10deca2009-10-26 22:31:16 +0000501 State->UnionGroups(FirstReg, Reg);
David Goodwin34877712009-10-26 19:32:42 +0000502 } else {
David Greene5393b252009-12-24 00:14:25 +0000503 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000504 FirstReg = Reg;
505 }
506 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000507
David Greene5393b252009-12-24 00:14:25 +0000508 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000509 }
510}
511
512BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
513 BitVector BV(TRI->getNumRegs(), false);
514 bool first = true;
515
516 // Check all references that need rewriting for Reg. For each, use
517 // the corresponding register class to narrow the set of registers
518 // that are appropriate for renaming.
Jim Grosbach2973b572010-01-06 16:48:02 +0000519 std::pair<std::multimap<unsigned,
David Goodwine10deca2009-10-26 22:31:16 +0000520 AggressiveAntiDepState::RegisterReference>::iterator,
521 std::multimap<unsigned,
522 AggressiveAntiDepState::RegisterReference>::iterator>
523 Range = State->GetRegRefs().equal_range(Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000524 for (std::multimap<unsigned,
525 AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first,
526 QE = Range.second; Q != QE; ++Q) {
David Goodwin34877712009-10-26 19:32:42 +0000527 const TargetRegisterClass *RC = Q->second.RC;
528 if (RC == NULL) continue;
529
530 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
531 if (first) {
532 BV |= RCBV;
533 first = false;
534 } else {
535 BV &= RCBV;
536 }
537
David Greene5393b252009-12-24 00:14:25 +0000538 DEBUG(dbgs() << " " << RC->getName());
David Goodwin34877712009-10-26 19:32:42 +0000539 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000540
David Goodwin34877712009-10-26 19:32:42 +0000541 return BV;
Jim Grosbach2973b572010-01-06 16:48:02 +0000542}
David Goodwin34877712009-10-26 19:32:42 +0000543
544bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin54097832009-11-05 01:19:35 +0000545 unsigned AntiDepGroupIndex,
546 RenameOrderType& RenameOrder,
547 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendling38306d52010-07-15 18:43:09 +0000548 std::vector<unsigned> &KillIndices = State->GetKillIndices();
549 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000550 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000551 RegRefs = State->GetRegRefs();
552
David Goodwin87d21b92009-11-13 19:52:48 +0000553 // Collect all referenced registers in the same group as
554 // AntiDepReg. These all need to be renamed together if we are to
555 // break the anti-dependence.
David Goodwin34877712009-10-26 19:32:42 +0000556 std::vector<unsigned> Regs;
David Goodwin87d21b92009-11-13 19:52:48 +0000557 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwin34877712009-10-26 19:32:42 +0000558 assert(Regs.size() > 0 && "Empty register group!");
559 if (Regs.size() == 0)
560 return false;
561
562 // Find the "superest" register in the group. At the same time,
563 // collect the BitVector of registers that can be used to rename
564 // each register.
Jim Grosbach2973b572010-01-06 16:48:02 +0000565 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
566 << ":\n");
David Goodwin34877712009-10-26 19:32:42 +0000567 std::map<unsigned, BitVector> RenameRegisterMap;
568 unsigned SuperReg = 0;
569 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
570 unsigned Reg = Regs[i];
571 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
572 SuperReg = Reg;
573
574 // If Reg has any references, then collect possible rename regs
575 if (RegRefs.count(Reg) > 0) {
David Greene5393b252009-12-24 00:14:25 +0000576 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000577
David Goodwin34877712009-10-26 19:32:42 +0000578 BitVector BV = GetRenameRegisters(Reg);
579 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
580
David Greene5393b252009-12-24 00:14:25 +0000581 DEBUG(dbgs() << " ::");
David Goodwin34877712009-10-26 19:32:42 +0000582 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000583 dbgs() << " " << TRI->getName(r));
584 DEBUG(dbgs() << "\n");
David Goodwin34877712009-10-26 19:32:42 +0000585 }
586 }
587
588 // All group registers should be a subreg of SuperReg.
589 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
590 unsigned Reg = Regs[i];
591 if (Reg == SuperReg) continue;
592 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
593 assert(IsSub && "Expecting group subregister");
594 if (!IsSub)
595 return false;
596 }
597
David Goodwin00621ef2009-11-20 23:33:54 +0000598#ifndef NDEBUG
599 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
600 if (DebugDiv > 0) {
601 static int renamecnt = 0;
602 if (renamecnt++ % DebugDiv != DebugMod)
603 return false;
Jim Grosbach2973b572010-01-06 16:48:02 +0000604
David Greene5393b252009-12-24 00:14:25 +0000605 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin00621ef2009-11-20 23:33:54 +0000606 " for debug ***\n";
607 }
608#endif
609
David Goodwin54097832009-11-05 01:19:35 +0000610 // Check each possible rename register for SuperReg in round-robin
611 // order. If that register is available, and the corresponding
612 // registers are available for the other group subregisters, then we
613 // can use those registers to rename.
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000614
615 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
616 // check every use of the register and find the largest register class
617 // that can be used in all of them.
Jim Grosbach2973b572010-01-06 16:48:02 +0000618 const TargetRegisterClass *SuperRC =
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000619 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbach2973b572010-01-06 16:48:02 +0000620
David Goodwin54097832009-11-05 01:19:35 +0000621 const TargetRegisterClass::iterator RB = SuperRC->allocation_order_begin(MF);
622 const TargetRegisterClass::iterator RE = SuperRC->allocation_order_end(MF);
623 if (RB == RE) {
David Greene5393b252009-12-24 00:14:25 +0000624 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin54097832009-11-05 01:19:35 +0000625 return false;
626 }
627
David Greene5393b252009-12-24 00:14:25 +0000628 DEBUG(dbgs() << "\tFind Registers:");
David Goodwin3e72d302009-11-19 23:12:37 +0000629
David Goodwin54097832009-11-05 01:19:35 +0000630 if (RenameOrder.count(SuperRC) == 0)
631 RenameOrder.insert(RenameOrderType::value_type(SuperRC, RE));
632
David Goodwin98f2f1a2009-11-05 01:45:50 +0000633 const TargetRegisterClass::iterator OrigR = RenameOrder[SuperRC];
David Goodwin54097832009-11-05 01:19:35 +0000634 const TargetRegisterClass::iterator EndR = ((OrigR == RE) ? RB : OrigR);
635 TargetRegisterClass::iterator R = OrigR;
636 do {
637 if (R == RB) R = RE;
638 --R;
David Goodwin00621ef2009-11-20 23:33:54 +0000639 const unsigned NewSuperReg = *R;
Jim Grosbach9b041c92010-09-02 17:12:55 +0000640 // Don't consider non-allocatable registers
641 if (!AllocatableSet.test(NewSuperReg)) continue;
David Goodwin34877712009-10-26 19:32:42 +0000642 // Don't replace a register with itself.
David Goodwin00621ef2009-11-20 23:33:54 +0000643 if (NewSuperReg == SuperReg) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000644
David Greene5393b252009-12-24 00:14:25 +0000645 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin00621ef2009-11-20 23:33:54 +0000646 RenameMap.clear();
647
648 // For each referenced group register (which must be a SuperReg or
649 // a subregister of SuperReg), find the corresponding subregister
650 // of NewSuperReg and make sure it is free to be renamed.
651 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
652 unsigned Reg = Regs[i];
653 unsigned NewReg = 0;
654 if (Reg == SuperReg) {
655 NewReg = NewSuperReg;
656 } else {
657 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
658 if (NewSubRegIdx != 0)
659 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwin34877712009-10-26 19:32:42 +0000660 }
David Goodwin00621ef2009-11-20 23:33:54 +0000661
David Greene5393b252009-12-24 00:14:25 +0000662 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbach2973b572010-01-06 16:48:02 +0000663
David Goodwin00621ef2009-11-20 23:33:54 +0000664 // Check if Reg can be renamed to NewReg.
665 BitVector BV = RenameRegisterMap[Reg];
666 if (!BV.test(NewReg)) {
David Greene5393b252009-12-24 00:14:25 +0000667 DEBUG(dbgs() << "(no rename)");
David Goodwin00621ef2009-11-20 23:33:54 +0000668 goto next_super_reg;
669 }
670
671 // If NewReg is dead and NewReg's most recent def is not before
672 // Regs's kill, it's safe to replace Reg with NewReg. We
673 // must also check all aliases of NewReg, because we can't define a
674 // register when any sub or super is already live.
675 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene5393b252009-12-24 00:14:25 +0000676 DEBUG(dbgs() << "(live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000677 goto next_super_reg;
678 } else {
679 bool found = false;
680 for (const unsigned *Alias = TRI->getAliasSet(NewReg);
681 *Alias; ++Alias) {
682 unsigned AliasReg = *Alias;
Jim Grosbach2973b572010-01-06 16:48:02 +0000683 if (State->IsLive(AliasReg) ||
684 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene5393b252009-12-24 00:14:25 +0000685 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000686 found = true;
687 break;
688 }
689 }
690 if (found)
691 goto next_super_reg;
692 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000693
David Goodwin00621ef2009-11-20 23:33:54 +0000694 // Record that 'Reg' can be renamed to 'NewReg'.
695 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwin34877712009-10-26 19:32:42 +0000696 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000697
David Goodwin00621ef2009-11-20 23:33:54 +0000698 // If we fall-out here, then every register in the group can be
699 // renamed, as recorded in RenameMap.
700 RenameOrder.erase(SuperRC);
701 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene5393b252009-12-24 00:14:25 +0000702 DEBUG(dbgs() << "]\n");
David Goodwin00621ef2009-11-20 23:33:54 +0000703 return true;
704
705 next_super_reg:
David Greene5393b252009-12-24 00:14:25 +0000706 DEBUG(dbgs() << ']');
David Goodwin54097832009-11-05 01:19:35 +0000707 } while (R != EndR);
David Goodwin34877712009-10-26 19:32:42 +0000708
David Greene5393b252009-12-24 00:14:25 +0000709 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000710
711 // No registers are free and available!
712 return false;
713}
714
715/// BreakAntiDependencies - Identifiy anti-dependencies within the
716/// ScheduleDAG and break them by renaming registers.
717///
David Goodwine10deca2009-10-26 22:31:16 +0000718unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman66db3a02010-04-19 23:11:58 +0000719 const std::vector<SUnit>& SUnits,
720 MachineBasicBlock::iterator Begin,
721 MachineBasicBlock::iterator End,
David Goodwine10deca2009-10-26 22:31:16 +0000722 unsigned InsertPosIndex) {
Bill Wendling38306d52010-07-15 18:43:09 +0000723 std::vector<unsigned> &KillIndices = State->GetKillIndices();
724 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000725 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000726 RegRefs = State->GetRegRefs();
727
David Goodwin34877712009-10-26 19:32:42 +0000728 // The code below assumes that there is at least one instruction,
729 // so just duck out immediately if the block is empty.
David Goodwin4de099d2009-11-03 20:57:50 +0000730 if (SUnits.empty()) return 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000731
David Goodwin54097832009-11-05 01:19:35 +0000732 // For each regclass the next register to use for renaming.
733 RenameOrderType RenameOrder;
David Goodwin34877712009-10-26 19:32:42 +0000734
735 // ...need a map from MI to SUnit.
Dan Gohman66db3a02010-04-19 23:11:58 +0000736 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwin34877712009-10-26 19:32:42 +0000737 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000738 const SUnit *SU = &SUnits[i];
739 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
740 SU));
David Goodwin34877712009-10-26 19:32:42 +0000741 }
742
David Goodwin87d21b92009-11-13 19:52:48 +0000743 // Track progress along the critical path through the SUnit graph as
744 // we walk the instructions. This is needed for regclasses that only
745 // break critical-path anti-dependencies.
Dan Gohman66db3a02010-04-19 23:11:58 +0000746 const SUnit *CriticalPathSU = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000747 MachineInstr *CriticalPathMI = 0;
748 if (CriticalPathSet.any()) {
749 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000750 const SUnit *SU = &SUnits[i];
Jim Grosbach2973b572010-01-06 16:48:02 +0000751 if (!CriticalPathSU ||
752 ((SU->getDepth() + SU->Latency) >
David Goodwin87d21b92009-11-13 19:52:48 +0000753 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
754 CriticalPathSU = SU;
755 }
756 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000757
David Goodwin87d21b92009-11-13 19:52:48 +0000758 CriticalPathMI = CriticalPathSU->getInstr();
759 }
760
Jim Grosbach2973b572010-01-06 16:48:02 +0000761#ifndef NDEBUG
David Greene5393b252009-12-24 00:14:25 +0000762 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
763 DEBUG(dbgs() << "Available regs:");
David Goodwin557bbe62009-11-20 19:32:48 +0000764 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
765 if (!State->IsLive(Reg))
David Greene5393b252009-12-24 00:14:25 +0000766 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000767 }
David Greene5393b252009-12-24 00:14:25 +0000768 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000769#endif
770
771 // Attempt to break anti-dependence edges. Walk the instructions
772 // from the bottom up, tracking information about liveness as we go
773 // to help determine which registers are available.
774 unsigned Broken = 0;
775 unsigned Count = InsertPosIndex - 1;
776 for (MachineBasicBlock::iterator I = End, E = Begin;
777 I != E; --Count) {
778 MachineInstr *MI = --I;
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
David Goodwin34877712009-10-26 19:32:42 +0000820 if (!AllocatableSet.test(AntiDepReg)) {
821 // 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;
Jim Grosbach533934e2010-06-01 23:48:44 +0000926 for (unsigned i = 0, e = SU->DbgInstrList.size() ; i < e ; ++i) {
927 MachineInstr *DI = SU->DbgInstrList[i];
928 assert (DI->getNumOperands()==3 && DI->getOperand(0).isReg() &&
929 DI->getOperand(0).getReg()
930 && "Non register dbg_value attached to SUnit!");
931 if (DI->getOperand(0).getReg() == AntiDepReg)
932 DI->getOperand(0).setReg(NewReg);
933 }
David Goodwin34877712009-10-26 19:32:42 +0000934 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000935
David Goodwin34877712009-10-26 19:32:42 +0000936 // We just went back in time and modified history; the
937 // liveness information for CurrReg is now inconsistent. Set
938 // the state as if it were dead.
David Goodwine10deca2009-10-26 22:31:16 +0000939 State->UnionGroups(NewReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000940 RegRefs.erase(NewReg);
941 DefIndices[NewReg] = DefIndices[CurrReg];
942 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbach2973b572010-01-06 16:48:02 +0000943
David Goodwine10deca2009-10-26 22:31:16 +0000944 State->UnionGroups(CurrReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000945 RegRefs.erase(CurrReg);
946 DefIndices[CurrReg] = KillIndices[CurrReg];
947 KillIndices[CurrReg] = ~0u;
948 assert(((KillIndices[CurrReg] == ~0u) !=
949 (DefIndices[CurrReg] == ~0u)) &&
950 "Kill and Def maps aren't consistent for AntiDepReg!");
951 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000952
David Goodwin34877712009-10-26 19:32:42 +0000953 ++Broken;
David Greene5393b252009-12-24 00:14:25 +0000954 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000955 }
956 }
957 }
958
959 ScanInstruction(MI, Count);
960 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000961
David Goodwin34877712009-10-26 19:32:42 +0000962 return Broken;
963}