blob: ca1f4a3e0147adb35bfeccbb133cba655c6bf706 [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"
24#include "llvm/Target/TargetRegisterInfo.h"
David Goodwine10deca2009-10-26 22:31:16 +000025#include "llvm/Support/CommandLine.h"
David Goodwin34877712009-10-26 19:32:42 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/ErrorHandling.h"
28#include "llvm/Support/raw_ostream.h"
David Goodwin34877712009-10-26 19:32:42 +000029using namespace llvm;
30
David Goodwin3e72d302009-11-19 23:12:37 +000031// If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod
32static cl::opt<int>
33DebugDiv("agg-antidep-debugdiv",
34 cl::desc("Debug control for aggressive anti-dep breaker"),
35 cl::init(0), cl::Hidden);
36static cl::opt<int>
37DebugMod("agg-antidep-debugmod",
38 cl::desc("Debug control for aggressive anti-dep breaker"),
39 cl::init(0), cl::Hidden);
40
David Goodwin990d2852009-12-09 17:18:22 +000041AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
42 MachineBasicBlock *BB) :
43 NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0) {
David Goodwin34877712009-10-26 19:32:42 +000044
David Goodwin990d2852009-12-09 17:18:22 +000045 const unsigned BBSize = BB->size();
46 for (unsigned i = 0; i < NumTargetRegs; ++i) {
47 // Initialize all registers to be in their own group. Initially we
48 // assign the register to the same-indexed GroupNode.
49 GroupNodeIndices[i] = i;
50 // Initialize the indices to indicate that no registers are live.
51 KillIndices[i] = ~0u;
52 DefIndices[i] = BBSize;
53 }
David Goodwin34877712009-10-26 19:32:42 +000054}
55
David Goodwine10deca2009-10-26 22:31:16 +000056unsigned AggressiveAntiDepState::GetGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000057{
58 unsigned Node = GroupNodeIndices[Reg];
59 while (GroupNodes[Node] != Node)
60 Node = GroupNodes[Node];
61
62 return Node;
63}
64
David Goodwin87d21b92009-11-13 19:52:48 +000065void AggressiveAntiDepState::GetGroupRegs(
66 unsigned Group,
67 std::vector<unsigned> &Regs,
68 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
David Goodwin34877712009-10-26 19:32:42 +000069{
David Goodwin990d2852009-12-09 17:18:22 +000070 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
David Goodwin87d21b92009-11-13 19:52:48 +000071 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
David Goodwin34877712009-10-26 19:32:42 +000072 Regs.push_back(Reg);
73 }
74}
75
David Goodwine10deca2009-10-26 22:31:16 +000076unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2)
David Goodwin34877712009-10-26 19:32:42 +000077{
78 assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
79 assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
Jim Grosbach2973b572010-01-06 16:48:02 +000080
David Goodwin34877712009-10-26 19:32:42 +000081 // find group for each register
82 unsigned Group1 = GetGroup(Reg1);
83 unsigned Group2 = GetGroup(Reg2);
Jim Grosbach2973b572010-01-06 16:48:02 +000084
David Goodwin34877712009-10-26 19:32:42 +000085 // if either group is 0, then that must become the parent
86 unsigned Parent = (Group1 == 0) ? Group1 : Group2;
87 unsigned Other = (Parent == Group1) ? Group2 : Group1;
88 GroupNodes.at(Other) = Parent;
89 return Parent;
90}
Jim Grosbach2973b572010-01-06 16:48:02 +000091
David Goodwine10deca2009-10-26 22:31:16 +000092unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000093{
94 // Create a new GroupNode for Reg. Reg's existing GroupNode must
95 // stay as is because there could be other GroupNodes referring to
96 // it.
97 unsigned idx = GroupNodes.size();
98 GroupNodes.push_back(idx);
99 GroupNodeIndices[Reg] = idx;
100 return idx;
101}
102
David Goodwine10deca2009-10-26 22:31:16 +0000103bool AggressiveAntiDepState::IsLive(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +0000104{
105 // KillIndex must be defined and DefIndex not defined for a register
106 // to be live.
107 return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
108}
109
David Goodwine10deca2009-10-26 22:31:16 +0000110
111
112AggressiveAntiDepBreaker::
David Goodwin0855dee2009-11-10 00:15:47 +0000113AggressiveAntiDepBreaker(MachineFunction& MFi,
Jim Grosbach2973b572010-01-06 16:48:02 +0000114 TargetSubtarget::RegClassVector& CriticalPathRCs) :
David Goodwine10deca2009-10-26 22:31:16 +0000115 AntiDepBreaker(), MF(MFi),
116 MRI(MF.getRegInfo()),
117 TRI(MF.getTarget().getRegisterInfo()),
118 AllocatableSet(TRI->getAllocatableSet(MF)),
David Goodwin557bbe62009-11-20 19:32:48 +0000119 State(NULL) {
David Goodwin87d21b92009-11-13 19:52:48 +0000120 /* Collect a bitset of all registers that are only broken if they
121 are on the critical path. */
122 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
123 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
124 if (CriticalPathSet.none())
125 CriticalPathSet = CPSet;
126 else
127 CriticalPathSet |= CPSet;
128 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000129
David Greene5393b252009-12-24 00:14:25 +0000130 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000131 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwin87d21b92009-11-13 19:52:48 +0000132 r = CriticalPathSet.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000133 dbgs() << " " << TRI->getName(r));
134 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000135}
136
137AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
138 delete State;
David Goodwine10deca2009-10-26 22:31:16 +0000139}
140
141void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
142 assert(State == NULL);
David Goodwin990d2852009-12-09 17:18:22 +0000143 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine10deca2009-10-26 22:31:16 +0000144
145 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
146 unsigned *KillIndices = State->GetKillIndices();
147 unsigned *DefIndices = State->GetDefIndices();
148
149 // Determine the live-out physregs for this block.
150 if (IsReturnBlock) {
151 // In a return block, examine the function live-out regs.
152 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
153 E = MRI.liveout_end(); I != E; ++I) {
154 unsigned Reg = *I;
155 State->UnionGroups(Reg, 0);
156 KillIndices[Reg] = BB->size();
157 DefIndices[Reg] = ~0u;
158 // Repeat, for all aliases.
159 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
160 unsigned AliasReg = *Alias;
161 State->UnionGroups(AliasReg, 0);
162 KillIndices[AliasReg] = BB->size();
163 DefIndices[AliasReg] = ~0u;
164 }
165 }
166 } else {
167 // In a non-return block, examine the live-in regs of all successors.
168 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
169 SE = BB->succ_end(); SI != SE; ++SI)
170 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
171 E = (*SI)->livein_end(); I != E; ++I) {
172 unsigned Reg = *I;
173 State->UnionGroups(Reg, 0);
174 KillIndices[Reg] = BB->size();
175 DefIndices[Reg] = ~0u;
176 // Repeat, for all aliases.
177 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
178 unsigned AliasReg = *Alias;
179 State->UnionGroups(AliasReg, 0);
180 KillIndices[AliasReg] = BB->size();
181 DefIndices[AliasReg] = ~0u;
182 }
183 }
184 }
185
186 // Mark live-out callee-saved registers. In a return block this is
187 // all callee-saved registers. In non-return this is any
188 // callee-saved register that is not saved in the prolog.
189 const MachineFrameInfo *MFI = MF.getFrameInfo();
190 BitVector Pristine = MFI->getPristineRegs(BB);
191 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
192 unsigned Reg = *I;
193 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
194 State->UnionGroups(Reg, 0);
195 KillIndices[Reg] = BB->size();
196 DefIndices[Reg] = ~0u;
197 // Repeat, for all aliases.
198 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
199 unsigned AliasReg = *Alias;
200 State->UnionGroups(AliasReg, 0);
201 KillIndices[AliasReg] = BB->size();
202 DefIndices[AliasReg] = ~0u;
203 }
204 }
205}
206
207void AggressiveAntiDepBreaker::FinishBlock() {
208 delete State;
209 State = NULL;
David Goodwine10deca2009-10-26 22:31:16 +0000210}
211
212void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
213 unsigned InsertPosIndex) {
214 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
215
David Goodwin5b3c3082009-10-29 23:30:59 +0000216 std::set<unsigned> PassthruRegs;
217 GetPassthruRegs(MI, PassthruRegs);
218 PrescanInstruction(MI, Count, PassthruRegs);
219 ScanInstruction(MI, Count);
220
David Greene5393b252009-12-24 00:14:25 +0000221 DEBUG(dbgs() << "Observe: ");
David Goodwine10deca2009-10-26 22:31:16 +0000222 DEBUG(MI->dump());
David Greene5393b252009-12-24 00:14:25 +0000223 DEBUG(dbgs() << "\tRegs:");
David Goodwine10deca2009-10-26 22:31:16 +0000224
225 unsigned *DefIndices = State->GetDefIndices();
David Goodwin990d2852009-12-09 17:18:22 +0000226 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine10deca2009-10-26 22:31:16 +0000227 // If Reg is current live, then mark that it can't be renamed as
228 // we don't know the extent of its live-range anymore (now that it
229 // has been scheduled). If it is not live but was defined in the
230 // previous schedule region, then set its def index to the most
231 // conservative location (i.e. the beginning of the previous
232 // schedule region).
233 if (State->IsLive(Reg)) {
234 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbach2973b572010-01-06 16:48:02 +0000235 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine10deca2009-10-26 22:31:16 +0000236 State->GetGroup(Reg) << "->g0(region live-out)");
237 State->UnionGroups(Reg, 0);
Jim Grosbach2973b572010-01-06 16:48:02 +0000238 } else if ((DefIndices[Reg] < InsertPosIndex)
239 && (DefIndices[Reg] >= Count)) {
David Goodwine10deca2009-10-26 22:31:16 +0000240 DefIndices[Reg] = Count;
241 }
242 }
David Greene5393b252009-12-24 00:14:25 +0000243 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000244}
245
David Goodwin34877712009-10-26 19:32:42 +0000246bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
247 MachineOperand& MO)
248{
249 if (!MO.isReg() || !MO.isImplicit())
250 return false;
251
252 unsigned Reg = MO.getReg();
253 if (Reg == 0)
254 return false;
255
256 MachineOperand *Op = NULL;
257 if (MO.isDef())
258 Op = MI->findRegisterUseOperand(Reg, true);
259 else
260 Op = MI->findRegisterDefOperand(Reg);
261
262 return((Op != NULL) && Op->isImplicit());
263}
264
265void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
266 std::set<unsigned>& PassthruRegs) {
267 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
268 MachineOperand &MO = MI->getOperand(i);
269 if (!MO.isReg()) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000270 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
David Goodwin34877712009-10-26 19:32:42 +0000271 IsImplicitDefUse(MI, MO)) {
272 const unsigned Reg = MO.getReg();
273 PassthruRegs.insert(Reg);
274 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
275 *Subreg; ++Subreg) {
276 PassthruRegs.insert(*Subreg);
277 }
278 }
279 }
280}
281
David Goodwin557bbe62009-11-20 19:32:48 +0000282/// AntiDepEdges - Return in Edges the anti- and output- dependencies
283/// in SU that we want to consider for breaking.
284static void AntiDepEdges(SUnit *SU, std::vector<SDep*>& Edges) {
285 SmallSet<unsigned, 4> RegSet;
David Goodwin34877712009-10-26 19:32:42 +0000286 for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
287 P != PE; ++P) {
David Goodwin12dd99d2009-11-12 19:08:21 +0000288 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Goodwin34877712009-10-26 19:32:42 +0000289 unsigned Reg = P->getReg();
David Goodwin557bbe62009-11-20 19:32:48 +0000290 if (RegSet.count(Reg) == 0) {
David Goodwin34877712009-10-26 19:32:42 +0000291 Edges.push_back(&*P);
David Goodwin557bbe62009-11-20 19:32:48 +0000292 RegSet.insert(Reg);
David Goodwin34877712009-10-26 19:32:42 +0000293 }
294 }
295 }
296}
297
David Goodwin87d21b92009-11-13 19:52:48 +0000298/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
299/// critical path.
300static SUnit *CriticalPathStep(SUnit *SU) {
301 SDep *Next = 0;
302 unsigned NextDepth = 0;
303 // Find the predecessor edge with the greatest depth.
304 if (SU != 0) {
305 for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
306 P != PE; ++P) {
307 SUnit *PredSU = P->getSUnit();
308 unsigned PredLatency = P->getLatency();
309 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
310 // In the case of a latency tie, prefer an anti-dependency edge over
311 // other types of edges.
312 if (NextDepth < PredTotalLatency ||
313 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
314 NextDepth = PredTotalLatency;
315 Next = &*P;
316 }
317 }
318 }
319
320 return (Next) ? Next->getSUnit() : 0;
321}
322
David Goodwin67a8a7b2009-10-29 19:17:04 +0000323void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbach2973b572010-01-06 16:48:02 +0000324 const char *tag,
325 const char *header,
David Goodwin3e72d302009-11-19 23:12:37 +0000326 const char *footer) {
David Goodwin67a8a7b2009-10-29 19:17:04 +0000327 unsigned *KillIndices = State->GetKillIndices();
328 unsigned *DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000329 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin67a8a7b2009-10-29 19:17:04 +0000330 RegRefs = State->GetRegRefs();
331
332 if (!State->IsLive(Reg)) {
333 KillIndices[Reg] = KillIdx;
334 DefIndices[Reg] = ~0u;
335 RegRefs.erase(Reg);
336 State->LeaveGroup(Reg);
David Goodwin3e72d302009-11-19 23:12:37 +0000337 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000338 dbgs() << header << TRI->getName(Reg); header = NULL; });
339 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000340 }
341 // Repeat for subregisters.
342 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
343 *Subreg; ++Subreg) {
344 unsigned SubregReg = *Subreg;
345 if (!State->IsLive(SubregReg)) {
346 KillIndices[SubregReg] = KillIdx;
347 DefIndices[SubregReg] = ~0u;
348 RegRefs.erase(SubregReg);
349 State->LeaveGroup(SubregReg);
David Goodwin3e72d302009-11-19 23:12:37 +0000350 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000351 dbgs() << header << TRI->getName(Reg); header = NULL; });
352 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin67a8a7b2009-10-29 19:17:04 +0000353 State->GetGroup(SubregReg) << tag);
354 }
355 }
David Goodwin3e72d302009-11-19 23:12:37 +0000356
David Greene5393b252009-12-24 00:14:25 +0000357 DEBUG(if ((header == NULL) && (footer != NULL)) dbgs() << footer);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000358}
359
Jim Grosbach2973b572010-01-06 16:48:02 +0000360void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
361 unsigned Count,
362 std::set<unsigned>& PassthruRegs)
363{
David Goodwine10deca2009-10-26 22:31:16 +0000364 unsigned *DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000365 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000366 RegRefs = State->GetRegRefs();
367
David Goodwin67a8a7b2009-10-29 19:17:04 +0000368 // Handle dead defs by simulating a last-use of the register just
369 // after the def. A dead def can occur because the def is truely
370 // dead, or because only a subregister is live at the def. If we
371 // don't do this the dead def will be incorrectly merged into the
372 // previous def.
David Goodwin34877712009-10-26 19:32:42 +0000373 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
374 MachineOperand &MO = MI->getOperand(i);
375 if (!MO.isReg() || !MO.isDef()) continue;
376 unsigned Reg = MO.getReg();
377 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000378
David Goodwin3e72d302009-11-19 23:12:37 +0000379 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwin34877712009-10-26 19:32:42 +0000380 }
381
David Greene5393b252009-12-24 00:14:25 +0000382 DEBUG(dbgs() << "\tDef Groups:");
David Goodwin34877712009-10-26 19:32:42 +0000383 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
384 MachineOperand &MO = MI->getOperand(i);
385 if (!MO.isReg() || !MO.isDef()) continue;
386 unsigned Reg = MO.getReg();
387 if (Reg == 0) continue;
388
Jim Grosbach2973b572010-01-06 16:48:02 +0000389 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000390
David Goodwin67a8a7b2009-10-29 19:17:04 +0000391 // If MI's defs have a special allocation requirement, don't allow
David Goodwin34877712009-10-26 19:32:42 +0000392 // any def registers to be changed. Also assume all registers
393 // defined in a call must not be changed (ABI).
394 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq()) {
David Greene5393b252009-12-24 00:14:25 +0000395 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000396 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000397 }
398
399 // Any aliased that are live at this point are completely or
David Goodwin67a8a7b2009-10-29 19:17:04 +0000400 // partially defined here, so group those aliases with Reg.
David Goodwin34877712009-10-26 19:32:42 +0000401 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
402 unsigned AliasReg = *Alias;
David Goodwine10deca2009-10-26 22:31:16 +0000403 if (State->IsLive(AliasReg)) {
404 State->UnionGroups(Reg, AliasReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000405 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwin34877712009-10-26 19:32:42 +0000406 TRI->getName(AliasReg) << ")");
407 }
408 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000409
David Goodwin34877712009-10-26 19:32:42 +0000410 // Note register reference...
411 const TargetRegisterClass *RC = NULL;
412 if (i < MI->getDesc().getNumOperands())
413 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000414 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000415 RegRefs.insert(std::make_pair(Reg, RR));
416 }
417
David Greene5393b252009-12-24 00:14:25 +0000418 DEBUG(dbgs() << '\n');
David Goodwin67a8a7b2009-10-29 19:17:04 +0000419
420 // Scan the register defs for this instruction and update
421 // live-ranges.
422 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
423 MachineOperand &MO = MI->getOperand(i);
424 if (!MO.isReg() || !MO.isDef()) continue;
425 unsigned Reg = MO.getReg();
426 if (Reg == 0) continue;
David Goodwin3e72d302009-11-19 23:12:37 +0000427 // Ignore KILLs and passthru registers for liveness...
428 if ((MI->getOpcode() == TargetInstrInfo::KILL) ||
429 (PassthruRegs.count(Reg) != 0))
430 continue;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000431
David Goodwin3e72d302009-11-19 23:12:37 +0000432 // Update def for Reg and aliases.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000433 DefIndices[Reg] = Count;
David Goodwin3e72d302009-11-19 23:12:37 +0000434 for (const unsigned *Alias = TRI->getAliasSet(Reg);
435 *Alias; ++Alias) {
436 unsigned AliasReg = *Alias;
437 DefIndices[AliasReg] = Count;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000438 }
439 }
David Goodwin34877712009-10-26 19:32:42 +0000440}
441
442void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
443 unsigned Count) {
David Greene5393b252009-12-24 00:14:25 +0000444 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000445 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000446 RegRefs = State->GetRegRefs();
David Goodwin34877712009-10-26 19:32:42 +0000447
448 // Scan the register uses for this instruction and update
449 // live-ranges, groups and RegRefs.
450 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
451 MachineOperand &MO = MI->getOperand(i);
452 if (!MO.isReg() || !MO.isUse()) continue;
453 unsigned Reg = MO.getReg();
454 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000455
456 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
457 State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000458
459 // It wasn't previously live but now it is, this is a kill. Forget
460 // the previous live-range information and start a new live-range
461 // for the register.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000462 HandleLastUse(Reg, Count, "(last-use)");
David Goodwin34877712009-10-26 19:32:42 +0000463
464 // If MI's uses have special allocation requirement, don't allow
465 // any use registers to be changed. Also assume all registers
466 // used in a call must not be changed (ABI).
467 if (MI->getDesc().isCall() || MI->getDesc().hasExtraSrcRegAllocReq()) {
David Greene5393b252009-12-24 00:14:25 +0000468 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000469 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000470 }
471
472 // Note register reference...
473 const TargetRegisterClass *RC = NULL;
474 if (i < MI->getDesc().getNumOperands())
475 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000476 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000477 RegRefs.insert(std::make_pair(Reg, RR));
478 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000479
David Greene5393b252009-12-24 00:14:25 +0000480 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000481
482 // Form a group of all defs and uses of a KILL instruction to ensure
483 // that all registers are renamed as a group.
484 if (MI->getOpcode() == TargetInstrInfo::KILL) {
David Greene5393b252009-12-24 00:14:25 +0000485 DEBUG(dbgs() << "\tKill Group:");
David Goodwin34877712009-10-26 19:32:42 +0000486
487 unsigned FirstReg = 0;
488 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
489 MachineOperand &MO = MI->getOperand(i);
490 if (!MO.isReg()) continue;
491 unsigned Reg = MO.getReg();
492 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000493
David Goodwin34877712009-10-26 19:32:42 +0000494 if (FirstReg != 0) {
David Greene5393b252009-12-24 00:14:25 +0000495 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine10deca2009-10-26 22:31:16 +0000496 State->UnionGroups(FirstReg, Reg);
David Goodwin34877712009-10-26 19:32:42 +0000497 } else {
David Greene5393b252009-12-24 00:14:25 +0000498 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000499 FirstReg = Reg;
500 }
501 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000502
David Greene5393b252009-12-24 00:14:25 +0000503 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000504 }
505}
506
507BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
508 BitVector BV(TRI->getNumRegs(), false);
509 bool first = true;
510
511 // Check all references that need rewriting for Reg. For each, use
512 // the corresponding register class to narrow the set of registers
513 // that are appropriate for renaming.
Jim Grosbach2973b572010-01-06 16:48:02 +0000514 std::pair<std::multimap<unsigned,
David Goodwine10deca2009-10-26 22:31:16 +0000515 AggressiveAntiDepState::RegisterReference>::iterator,
516 std::multimap<unsigned,
517 AggressiveAntiDepState::RegisterReference>::iterator>
518 Range = State->GetRegRefs().equal_range(Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000519 for (std::multimap<unsigned,
520 AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first,
521 QE = Range.second; Q != QE; ++Q) {
David Goodwin34877712009-10-26 19:32:42 +0000522 const TargetRegisterClass *RC = Q->second.RC;
523 if (RC == NULL) continue;
524
525 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
526 if (first) {
527 BV |= RCBV;
528 first = false;
529 } else {
530 BV &= RCBV;
531 }
532
David Greene5393b252009-12-24 00:14:25 +0000533 DEBUG(dbgs() << " " << RC->getName());
David Goodwin34877712009-10-26 19:32:42 +0000534 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000535
David Goodwin34877712009-10-26 19:32:42 +0000536 return BV;
Jim Grosbach2973b572010-01-06 16:48:02 +0000537}
David Goodwin34877712009-10-26 19:32:42 +0000538
539bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin54097832009-11-05 01:19:35 +0000540 unsigned AntiDepGroupIndex,
541 RenameOrderType& RenameOrder,
542 std::map<unsigned, unsigned> &RenameMap) {
David Goodwine10deca2009-10-26 22:31:16 +0000543 unsigned *KillIndices = State->GetKillIndices();
544 unsigned *DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000545 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000546 RegRefs = State->GetRegRefs();
547
David Goodwin87d21b92009-11-13 19:52:48 +0000548 // Collect all referenced registers in the same group as
549 // AntiDepReg. These all need to be renamed together if we are to
550 // break the anti-dependence.
David Goodwin34877712009-10-26 19:32:42 +0000551 std::vector<unsigned> Regs;
David Goodwin87d21b92009-11-13 19:52:48 +0000552 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwin34877712009-10-26 19:32:42 +0000553 assert(Regs.size() > 0 && "Empty register group!");
554 if (Regs.size() == 0)
555 return false;
556
557 // Find the "superest" register in the group. At the same time,
558 // collect the BitVector of registers that can be used to rename
559 // each register.
Jim Grosbach2973b572010-01-06 16:48:02 +0000560 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
561 << ":\n");
David Goodwin34877712009-10-26 19:32:42 +0000562 std::map<unsigned, BitVector> RenameRegisterMap;
563 unsigned SuperReg = 0;
564 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
565 unsigned Reg = Regs[i];
566 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
567 SuperReg = Reg;
568
569 // If Reg has any references, then collect possible rename regs
570 if (RegRefs.count(Reg) > 0) {
David Greene5393b252009-12-24 00:14:25 +0000571 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000572
David Goodwin34877712009-10-26 19:32:42 +0000573 BitVector BV = GetRenameRegisters(Reg);
574 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
575
David Greene5393b252009-12-24 00:14:25 +0000576 DEBUG(dbgs() << " ::");
David Goodwin34877712009-10-26 19:32:42 +0000577 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000578 dbgs() << " " << TRI->getName(r));
579 DEBUG(dbgs() << "\n");
David Goodwin34877712009-10-26 19:32:42 +0000580 }
581 }
582
583 // All group registers should be a subreg of SuperReg.
584 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
585 unsigned Reg = Regs[i];
586 if (Reg == SuperReg) continue;
587 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
588 assert(IsSub && "Expecting group subregister");
589 if (!IsSub)
590 return false;
591 }
592
David Goodwin00621ef2009-11-20 23:33:54 +0000593#ifndef NDEBUG
594 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
595 if (DebugDiv > 0) {
596 static int renamecnt = 0;
597 if (renamecnt++ % DebugDiv != DebugMod)
598 return false;
Jim Grosbach2973b572010-01-06 16:48:02 +0000599
David Greene5393b252009-12-24 00:14:25 +0000600 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin00621ef2009-11-20 23:33:54 +0000601 " for debug ***\n";
602 }
603#endif
604
David Goodwin54097832009-11-05 01:19:35 +0000605 // Check each possible rename register for SuperReg in round-robin
606 // order. If that register is available, and the corresponding
607 // registers are available for the other group subregisters, then we
608 // can use those registers to rename.
Jim Grosbach2973b572010-01-06 16:48:02 +0000609 const TargetRegisterClass *SuperRC =
David Goodwin54097832009-11-05 01:19:35 +0000610 TRI->getPhysicalRegisterRegClass(SuperReg, MVT::Other);
Jim Grosbach2973b572010-01-06 16:48:02 +0000611
David Goodwin54097832009-11-05 01:19:35 +0000612 const TargetRegisterClass::iterator RB = SuperRC->allocation_order_begin(MF);
613 const TargetRegisterClass::iterator RE = SuperRC->allocation_order_end(MF);
614 if (RB == RE) {
David Greene5393b252009-12-24 00:14:25 +0000615 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin54097832009-11-05 01:19:35 +0000616 return false;
617 }
618
David Greene5393b252009-12-24 00:14:25 +0000619 DEBUG(dbgs() << "\tFind Registers:");
David Goodwin3e72d302009-11-19 23:12:37 +0000620
David Goodwin54097832009-11-05 01:19:35 +0000621 if (RenameOrder.count(SuperRC) == 0)
622 RenameOrder.insert(RenameOrderType::value_type(SuperRC, RE));
623
David Goodwin98f2f1a2009-11-05 01:45:50 +0000624 const TargetRegisterClass::iterator OrigR = RenameOrder[SuperRC];
David Goodwin54097832009-11-05 01:19:35 +0000625 const TargetRegisterClass::iterator EndR = ((OrigR == RE) ? RB : OrigR);
626 TargetRegisterClass::iterator R = OrigR;
627 do {
628 if (R == RB) R = RE;
629 --R;
David Goodwin00621ef2009-11-20 23:33:54 +0000630 const unsigned NewSuperReg = *R;
David Goodwin34877712009-10-26 19:32:42 +0000631 // Don't replace a register with itself.
David Goodwin00621ef2009-11-20 23:33:54 +0000632 if (NewSuperReg == SuperReg) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000633
David Greene5393b252009-12-24 00:14:25 +0000634 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin00621ef2009-11-20 23:33:54 +0000635 RenameMap.clear();
636
637 // For each referenced group register (which must be a SuperReg or
638 // a subregister of SuperReg), find the corresponding subregister
639 // of NewSuperReg and make sure it is free to be renamed.
640 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
641 unsigned Reg = Regs[i];
642 unsigned NewReg = 0;
643 if (Reg == SuperReg) {
644 NewReg = NewSuperReg;
645 } else {
646 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
647 if (NewSubRegIdx != 0)
648 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwin34877712009-10-26 19:32:42 +0000649 }
David Goodwin00621ef2009-11-20 23:33:54 +0000650
David Greene5393b252009-12-24 00:14:25 +0000651 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbach2973b572010-01-06 16:48:02 +0000652
David Goodwin00621ef2009-11-20 23:33:54 +0000653 // Check if Reg can be renamed to NewReg.
654 BitVector BV = RenameRegisterMap[Reg];
655 if (!BV.test(NewReg)) {
David Greene5393b252009-12-24 00:14:25 +0000656 DEBUG(dbgs() << "(no rename)");
David Goodwin00621ef2009-11-20 23:33:54 +0000657 goto next_super_reg;
658 }
659
660 // If NewReg is dead and NewReg's most recent def is not before
661 // Regs's kill, it's safe to replace Reg with NewReg. We
662 // must also check all aliases of NewReg, because we can't define a
663 // register when any sub or super is already live.
664 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene5393b252009-12-24 00:14:25 +0000665 DEBUG(dbgs() << "(live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000666 goto next_super_reg;
667 } else {
668 bool found = false;
669 for (const unsigned *Alias = TRI->getAliasSet(NewReg);
670 *Alias; ++Alias) {
671 unsigned AliasReg = *Alias;
Jim Grosbach2973b572010-01-06 16:48:02 +0000672 if (State->IsLive(AliasReg) ||
673 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene5393b252009-12-24 00:14:25 +0000674 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000675 found = true;
676 break;
677 }
678 }
679 if (found)
680 goto next_super_reg;
681 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000682
David Goodwin00621ef2009-11-20 23:33:54 +0000683 // Record that 'Reg' can be renamed to 'NewReg'.
684 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwin34877712009-10-26 19:32:42 +0000685 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000686
David Goodwin00621ef2009-11-20 23:33:54 +0000687 // If we fall-out here, then every register in the group can be
688 // renamed, as recorded in RenameMap.
689 RenameOrder.erase(SuperRC);
690 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene5393b252009-12-24 00:14:25 +0000691 DEBUG(dbgs() << "]\n");
David Goodwin00621ef2009-11-20 23:33:54 +0000692 return true;
693
694 next_super_reg:
David Greene5393b252009-12-24 00:14:25 +0000695 DEBUG(dbgs() << ']');
David Goodwin54097832009-11-05 01:19:35 +0000696 } while (R != EndR);
David Goodwin34877712009-10-26 19:32:42 +0000697
David Greene5393b252009-12-24 00:14:25 +0000698 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000699
700 // No registers are free and available!
701 return false;
702}
703
704/// BreakAntiDependencies - Identifiy anti-dependencies within the
705/// ScheduleDAG and break them by renaming registers.
706///
David Goodwine10deca2009-10-26 22:31:16 +0000707unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
708 std::vector<SUnit>& SUnits,
709 MachineBasicBlock::iterator& Begin,
710 MachineBasicBlock::iterator& End,
711 unsigned InsertPosIndex) {
712 unsigned *KillIndices = State->GetKillIndices();
713 unsigned *DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000714 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000715 RegRefs = State->GetRegRefs();
716
David Goodwin34877712009-10-26 19:32:42 +0000717 // The code below assumes that there is at least one instruction,
718 // so just duck out immediately if the block is empty.
David Goodwin4de099d2009-11-03 20:57:50 +0000719 if (SUnits.empty()) return 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000720
David Goodwin54097832009-11-05 01:19:35 +0000721 // For each regclass the next register to use for renaming.
722 RenameOrderType RenameOrder;
David Goodwin34877712009-10-26 19:32:42 +0000723
724 // ...need a map from MI to SUnit.
725 std::map<MachineInstr *, SUnit *> MISUnitMap;
David Goodwin34877712009-10-26 19:32:42 +0000726 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
727 SUnit *SU = &SUnits[i];
728 MISUnitMap.insert(std::pair<MachineInstr *, SUnit *>(SU->getInstr(), SU));
729 }
730
David Goodwin87d21b92009-11-13 19:52:48 +0000731 // Track progress along the critical path through the SUnit graph as
732 // we walk the instructions. This is needed for regclasses that only
733 // break critical-path anti-dependencies.
734 SUnit *CriticalPathSU = 0;
735 MachineInstr *CriticalPathMI = 0;
736 if (CriticalPathSet.any()) {
737 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
738 SUnit *SU = &SUnits[i];
Jim Grosbach2973b572010-01-06 16:48:02 +0000739 if (!CriticalPathSU ||
740 ((SU->getDepth() + SU->Latency) >
David Goodwin87d21b92009-11-13 19:52:48 +0000741 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
742 CriticalPathSU = SU;
743 }
744 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000745
David Goodwin87d21b92009-11-13 19:52:48 +0000746 CriticalPathMI = CriticalPathSU->getInstr();
747 }
748
Jim Grosbach2973b572010-01-06 16:48:02 +0000749#ifndef NDEBUG
David Greene5393b252009-12-24 00:14:25 +0000750 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
751 DEBUG(dbgs() << "Available regs:");
David Goodwin557bbe62009-11-20 19:32:48 +0000752 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
753 if (!State->IsLive(Reg))
David Greene5393b252009-12-24 00:14:25 +0000754 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000755 }
David Greene5393b252009-12-24 00:14:25 +0000756 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000757#endif
758
759 // Attempt to break anti-dependence edges. Walk the instructions
760 // from the bottom up, tracking information about liveness as we go
761 // to help determine which registers are available.
762 unsigned Broken = 0;
763 unsigned Count = InsertPosIndex - 1;
764 for (MachineBasicBlock::iterator I = End, E = Begin;
765 I != E; --Count) {
766 MachineInstr *MI = --I;
767
David Greene5393b252009-12-24 00:14:25 +0000768 DEBUG(dbgs() << "Anti: ");
David Goodwin34877712009-10-26 19:32:42 +0000769 DEBUG(MI->dump());
770
771 std::set<unsigned> PassthruRegs;
772 GetPassthruRegs(MI, PassthruRegs);
773
774 // Process the defs in MI...
775 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbach2973b572010-01-06 16:48:02 +0000776
David Goodwin557bbe62009-11-20 19:32:48 +0000777 // The dependence edges that represent anti- and output-
David Goodwin87d21b92009-11-13 19:52:48 +0000778 // dependencies that are candidates for breaking.
David Goodwin34877712009-10-26 19:32:42 +0000779 std::vector<SDep*> Edges;
780 SUnit *PathSU = MISUnitMap[MI];
David Goodwin557bbe62009-11-20 19:32:48 +0000781 AntiDepEdges(PathSU, Edges);
David Goodwin87d21b92009-11-13 19:52:48 +0000782
783 // If MI is not on the critical path, then we don't rename
784 // registers in the CriticalPathSet.
785 BitVector *ExcludeRegs = NULL;
786 if (MI == CriticalPathMI) {
787 CriticalPathSU = CriticalPathStep(CriticalPathSU);
788 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000789 } else {
David Goodwin87d21b92009-11-13 19:52:48 +0000790 ExcludeRegs = &CriticalPathSet;
791 }
792
David Goodwin34877712009-10-26 19:32:42 +0000793 // Ignore KILL instructions (they form a group in ScanInstruction
794 // but don't cause any anti-dependence breaking themselves)
795 if (MI->getOpcode() != TargetInstrInfo::KILL) {
796 // Attempt to break each anti-dependency...
797 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
798 SDep *Edge = Edges[i];
799 SUnit *NextSU = Edge->getSUnit();
Jim Grosbach2973b572010-01-06 16:48:02 +0000800
David Goodwin12dd99d2009-11-12 19:08:21 +0000801 if ((Edge->getKind() != SDep::Anti) &&
802 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000803
David Goodwin34877712009-10-26 19:32:42 +0000804 unsigned AntiDepReg = Edge->getReg();
David Greene5393b252009-12-24 00:14:25 +0000805 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwin34877712009-10-26 19:32:42 +0000806 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbach2973b572010-01-06 16:48:02 +0000807
David Goodwin34877712009-10-26 19:32:42 +0000808 if (!AllocatableSet.test(AntiDepReg)) {
809 // Don't break anti-dependencies on non-allocatable registers.
David Greene5393b252009-12-24 00:14:25 +0000810 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwin34877712009-10-26 19:32:42 +0000811 continue;
David Goodwin87d21b92009-11-13 19:52:48 +0000812 } else if ((ExcludeRegs != NULL) && ExcludeRegs->test(AntiDepReg)) {
813 // Don't break anti-dependencies for critical path registers
814 // if not on the critical path
David Greene5393b252009-12-24 00:14:25 +0000815 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwin87d21b92009-11-13 19:52:48 +0000816 continue;
David Goodwin34877712009-10-26 19:32:42 +0000817 } else if (PassthruRegs.count(AntiDepReg) != 0) {
818 // If the anti-dep register liveness "passes-thru", then
819 // don't try to change it. It will be changed along with
820 // the use if required to break an earlier antidep.
David Greene5393b252009-12-24 00:14:25 +0000821 DEBUG(dbgs() << " (passthru)\n");
David Goodwin34877712009-10-26 19:32:42 +0000822 continue;
823 } else {
824 // No anti-dep breaking for implicit deps
825 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000826 assert(AntiDepOp != NULL &&
827 "Can't find index for defined register operand");
David Goodwin34877712009-10-26 19:32:42 +0000828 if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) {
David Greene5393b252009-12-24 00:14:25 +0000829 DEBUG(dbgs() << " (implicit)\n");
David Goodwin34877712009-10-26 19:32:42 +0000830 continue;
831 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000832
David Goodwin34877712009-10-26 19:32:42 +0000833 // If the SUnit has other dependencies on the SUnit that
834 // it anti-depends on, don't bother breaking the
835 // anti-dependency since those edges would prevent such
836 // units from being scheduled past each other
837 // regardless.
David Goodwin557bbe62009-11-20 19:32:48 +0000838 //
839 // Also, if there are dependencies on other SUnits with the
840 // same register as the anti-dependency, don't attempt to
841 // break it.
David Goodwin34877712009-10-26 19:32:42 +0000842 for (SUnit::pred_iterator P = PathSU->Preds.begin(),
843 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin557bbe62009-11-20 19:32:48 +0000844 if (P->getSUnit() == NextSU ?
845 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
846 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
847 AntiDepReg = 0;
848 break;
849 }
850 }
851 for (SUnit::pred_iterator P = PathSU->Preds.begin(),
852 PE = PathSU->Preds.end(); P != PE; ++P) {
853 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
854 (P->getKind() != SDep::Output)) {
David Greene5393b252009-12-24 00:14:25 +0000855 DEBUG(dbgs() << " (real dependency)\n");
David Goodwin34877712009-10-26 19:32:42 +0000856 AntiDepReg = 0;
857 break;
Jim Grosbach2973b572010-01-06 16:48:02 +0000858 } else if ((P->getSUnit() != NextSU) &&
859 (P->getKind() == SDep::Data) &&
David Goodwin557bbe62009-11-20 19:32:48 +0000860 (P->getReg() == AntiDepReg)) {
David Greene5393b252009-12-24 00:14:25 +0000861 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin557bbe62009-11-20 19:32:48 +0000862 AntiDepReg = 0;
863 break;
David Goodwin34877712009-10-26 19:32:42 +0000864 }
865 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000866
David Goodwin34877712009-10-26 19:32:42 +0000867 if (AntiDepReg == 0) continue;
868 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000869
David Goodwin34877712009-10-26 19:32:42 +0000870 assert(AntiDepReg != 0);
871 if (AntiDepReg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000872
David Goodwin34877712009-10-26 19:32:42 +0000873 // Determine AntiDepReg's register group.
David Goodwine10deca2009-10-26 22:31:16 +0000874 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwin34877712009-10-26 19:32:42 +0000875 if (GroupIndex == 0) {
David Greene5393b252009-12-24 00:14:25 +0000876 DEBUG(dbgs() << " (zero group)\n");
David Goodwin34877712009-10-26 19:32:42 +0000877 continue;
878 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000879
David Greene5393b252009-12-24 00:14:25 +0000880 DEBUG(dbgs() << '\n');
Jim Grosbach2973b572010-01-06 16:48:02 +0000881
David Goodwin34877712009-10-26 19:32:42 +0000882 // Look for a suitable register to use to break the anti-dependence.
883 std::map<unsigned, unsigned> RenameMap;
David Goodwin54097832009-11-05 01:19:35 +0000884 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene5393b252009-12-24 00:14:25 +0000885 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwin34877712009-10-26 19:32:42 +0000886 << TRI->getName(AntiDepReg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000887
David Goodwin34877712009-10-26 19:32:42 +0000888 // Handle each group register...
889 for (std::map<unsigned, unsigned>::iterator
890 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
891 unsigned CurrReg = S->first;
892 unsigned NewReg = S->second;
Jim Grosbach2973b572010-01-06 16:48:02 +0000893
894 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
895 TRI->getName(NewReg) << "(" <<
David Goodwin34877712009-10-26 19:32:42 +0000896 RegRefs.count(CurrReg) << " refs)");
Jim Grosbach2973b572010-01-06 16:48:02 +0000897
David Goodwin34877712009-10-26 19:32:42 +0000898 // Update the references to the old register CurrReg to
899 // refer to the new register NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000900 std::pair<std::multimap<unsigned,
901 AggressiveAntiDepState::RegisterReference>::iterator,
David Goodwine10deca2009-10-26 22:31:16 +0000902 std::multimap<unsigned,
Jim Grosbach2973b572010-01-06 16:48:02 +0000903 AggressiveAntiDepState::RegisterReference>::iterator>
David Goodwin34877712009-10-26 19:32:42 +0000904 Range = RegRefs.equal_range(CurrReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000905 for (std::multimap<unsigned,
906 AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000907 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
908 Q->second.Operand->setReg(NewReg);
909 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000910
David Goodwin34877712009-10-26 19:32:42 +0000911 // We just went back in time and modified history; the
912 // liveness information for CurrReg is now inconsistent. Set
913 // the state as if it were dead.
David Goodwine10deca2009-10-26 22:31:16 +0000914 State->UnionGroups(NewReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000915 RegRefs.erase(NewReg);
916 DefIndices[NewReg] = DefIndices[CurrReg];
917 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbach2973b572010-01-06 16:48:02 +0000918
David Goodwine10deca2009-10-26 22:31:16 +0000919 State->UnionGroups(CurrReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000920 RegRefs.erase(CurrReg);
921 DefIndices[CurrReg] = KillIndices[CurrReg];
922 KillIndices[CurrReg] = ~0u;
923 assert(((KillIndices[CurrReg] == ~0u) !=
924 (DefIndices[CurrReg] == ~0u)) &&
925 "Kill and Def maps aren't consistent for AntiDepReg!");
926 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000927
David Goodwin34877712009-10-26 19:32:42 +0000928 ++Broken;
David Greene5393b252009-12-24 00:14:25 +0000929 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000930 }
931 }
932 }
933
934 ScanInstruction(MI, Count);
935 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000936
David Goodwin34877712009-10-26 19:32:42 +0000937 return Broken;
938}