blob: 4d8ed69ebc0a2b52ecb8d55927eef2e02a5f18f1 [file] [log] [blame]
David Goodwin34877712009-10-26 19:32:42 +00001//===----- AggressiveAntiDepBreaker.cpp - Anti-dep breaker -------- ---------===//
2//
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
17#define DEBUG_TYPE "aggressive-antidep"
18#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 Goodwine10deca2009-10-26 22:31:16 +000031static cl::opt<int>
32AntiDepTrials("agg-antidep-trials",
33 cl::desc("Maximum number of anti-dependency breaking passes"),
34 cl::init(2), cl::Hidden);
David Goodwin34877712009-10-26 19:32:42 +000035
David Goodwine10deca2009-10-26 22:31:16 +000036AggressiveAntiDepState::AggressiveAntiDepState(MachineBasicBlock *BB) :
37 GroupNodes(TargetRegisterInfo::FirstVirtualRegister, 0) {
David Goodwin34877712009-10-26 19:32:42 +000038 // Initialize all registers to be in their own group. Initially we
39 // assign the register to the same-indexed GroupNode.
40 for (unsigned i = 0; i < TargetRegisterInfo::FirstVirtualRegister; ++i)
41 GroupNodeIndices[i] = i;
42
43 // Initialize the indices to indicate that no registers are live.
44 std::fill(KillIndices, array_endof(KillIndices), ~0u);
45 std::fill(DefIndices, array_endof(DefIndices), BB->size());
David Goodwin34877712009-10-26 19:32:42 +000046}
47
David Goodwine10deca2009-10-26 22:31:16 +000048unsigned AggressiveAntiDepState::GetGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000049{
50 unsigned Node = GroupNodeIndices[Reg];
51 while (GroupNodes[Node] != Node)
52 Node = GroupNodes[Node];
53
54 return Node;
55}
56
David Goodwine10deca2009-10-26 22:31:16 +000057void AggressiveAntiDepState::GetGroupRegs(unsigned Group, std::vector<unsigned> &Regs)
David Goodwin34877712009-10-26 19:32:42 +000058{
59 for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg) {
60 if (GetGroup(Reg) == Group)
61 Regs.push_back(Reg);
62 }
63}
64
David Goodwine10deca2009-10-26 22:31:16 +000065unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2)
David Goodwin34877712009-10-26 19:32:42 +000066{
67 assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
68 assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
69
70 // find group for each register
71 unsigned Group1 = GetGroup(Reg1);
72 unsigned Group2 = GetGroup(Reg2);
73
74 // if either group is 0, then that must become the parent
75 unsigned Parent = (Group1 == 0) ? Group1 : Group2;
76 unsigned Other = (Parent == Group1) ? Group2 : Group1;
77 GroupNodes.at(Other) = Parent;
78 return Parent;
79}
80
David Goodwine10deca2009-10-26 22:31:16 +000081unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000082{
83 // Create a new GroupNode for Reg. Reg's existing GroupNode must
84 // stay as is because there could be other GroupNodes referring to
85 // it.
86 unsigned idx = GroupNodes.size();
87 GroupNodes.push_back(idx);
88 GroupNodeIndices[Reg] = idx;
89 return idx;
90}
91
David Goodwine10deca2009-10-26 22:31:16 +000092bool AggressiveAntiDepState::IsLive(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000093{
94 // KillIndex must be defined and DefIndex not defined for a register
95 // to be live.
96 return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
97}
98
David Goodwine10deca2009-10-26 22:31:16 +000099
100
101AggressiveAntiDepBreaker::
102AggressiveAntiDepBreaker(MachineFunction& MFi) :
103 AntiDepBreaker(), MF(MFi),
104 MRI(MF.getRegInfo()),
105 TRI(MF.getTarget().getRegisterInfo()),
106 AllocatableSet(TRI->getAllocatableSet(MF)),
107 State(NULL), SavedState(NULL) {
108}
109
110AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
111 delete State;
112 delete SavedState;
113}
114
115unsigned AggressiveAntiDepBreaker::GetMaxTrials() {
116 if (AntiDepTrials <= 0)
117 return 1;
118 return AntiDepTrials;
119}
120
121void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
122 assert(State == NULL);
123 State = new AggressiveAntiDepState(BB);
124
125 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
126 unsigned *KillIndices = State->GetKillIndices();
127 unsigned *DefIndices = State->GetDefIndices();
128
129 // Determine the live-out physregs for this block.
130 if (IsReturnBlock) {
131 // In a return block, examine the function live-out regs.
132 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
133 E = MRI.liveout_end(); I != E; ++I) {
134 unsigned Reg = *I;
135 State->UnionGroups(Reg, 0);
136 KillIndices[Reg] = BB->size();
137 DefIndices[Reg] = ~0u;
138 // Repeat, for all aliases.
139 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
140 unsigned AliasReg = *Alias;
141 State->UnionGroups(AliasReg, 0);
142 KillIndices[AliasReg] = BB->size();
143 DefIndices[AliasReg] = ~0u;
144 }
145 }
146 } else {
147 // In a non-return block, examine the live-in regs of all successors.
148 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
149 SE = BB->succ_end(); SI != SE; ++SI)
150 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
151 E = (*SI)->livein_end(); I != E; ++I) {
152 unsigned Reg = *I;
153 State->UnionGroups(Reg, 0);
154 KillIndices[Reg] = BB->size();
155 DefIndices[Reg] = ~0u;
156 // Repeat, for all aliases.
157 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
158 unsigned AliasReg = *Alias;
159 State->UnionGroups(AliasReg, 0);
160 KillIndices[AliasReg] = BB->size();
161 DefIndices[AliasReg] = ~0u;
162 }
163 }
164 }
165
166 // Mark live-out callee-saved registers. In a return block this is
167 // all callee-saved registers. In non-return this is any
168 // callee-saved register that is not saved in the prolog.
169 const MachineFrameInfo *MFI = MF.getFrameInfo();
170 BitVector Pristine = MFI->getPristineRegs(BB);
171 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
172 unsigned Reg = *I;
173 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
174 State->UnionGroups(Reg, 0);
175 KillIndices[Reg] = BB->size();
176 DefIndices[Reg] = ~0u;
177 // Repeat, for all aliases.
178 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
179 unsigned AliasReg = *Alias;
180 State->UnionGroups(AliasReg, 0);
181 KillIndices[AliasReg] = BB->size();
182 DefIndices[AliasReg] = ~0u;
183 }
184 }
185}
186
187void AggressiveAntiDepBreaker::FinishBlock() {
188 delete State;
189 State = NULL;
190 delete SavedState;
191 SavedState = NULL;
192}
193
194void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
195 unsigned InsertPosIndex) {
196 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
197
198 DEBUG(errs() << "Observe: ");
199 DEBUG(MI->dump());
200
201 unsigned *DefIndices = State->GetDefIndices();
202 for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg) {
203 // If Reg is current live, then mark that it can't be renamed as
204 // we don't know the extent of its live-range anymore (now that it
205 // has been scheduled). If it is not live but was defined in the
206 // previous schedule region, then set its def index to the most
207 // conservative location (i.e. the beginning of the previous
208 // schedule region).
209 if (State->IsLive(Reg)) {
210 DEBUG(if (State->GetGroup(Reg) != 0)
211 errs() << " " << TRI->getName(Reg) << "=g" <<
212 State->GetGroup(Reg) << "->g0(region live-out)");
213 State->UnionGroups(Reg, 0);
214 } else if ((DefIndices[Reg] < InsertPosIndex) && (DefIndices[Reg] >= Count)) {
215 DefIndices[Reg] = Count;
216 }
217 }
218
219 std::set<unsigned> PassthruRegs;
220 GetPassthruRegs(MI, PassthruRegs);
221 PrescanInstruction(MI, Count, PassthruRegs);
222 ScanInstruction(MI, Count);
223
224 // We're starting a new schedule region so forget any saved state.
225 delete SavedState;
226 SavedState = NULL;
227}
228
David Goodwin34877712009-10-26 19:32:42 +0000229bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
230 MachineOperand& MO)
231{
232 if (!MO.isReg() || !MO.isImplicit())
233 return false;
234
235 unsigned Reg = MO.getReg();
236 if (Reg == 0)
237 return false;
238
239 MachineOperand *Op = NULL;
240 if (MO.isDef())
241 Op = MI->findRegisterUseOperand(Reg, true);
242 else
243 Op = MI->findRegisterDefOperand(Reg);
244
245 return((Op != NULL) && Op->isImplicit());
246}
247
248void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
249 std::set<unsigned>& PassthruRegs) {
250 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
251 MachineOperand &MO = MI->getOperand(i);
252 if (!MO.isReg()) continue;
253 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
254 IsImplicitDefUse(MI, MO)) {
255 const unsigned Reg = MO.getReg();
256 PassthruRegs.insert(Reg);
257 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
258 *Subreg; ++Subreg) {
259 PassthruRegs.insert(*Subreg);
260 }
261 }
262 }
263}
264
265/// AntiDepPathStep - Return SUnit that SU has an anti-dependence on.
266static void AntiDepPathStep(SUnit *SU, std::vector<SDep*>& Edges) {
267 SmallSet<unsigned, 8> Dups;
268 for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
269 P != PE; ++P) {
270 if (P->getKind() == SDep::Anti) {
271 unsigned Reg = P->getReg();
272 if (Dups.count(Reg) == 0) {
273 Edges.push_back(&*P);
274 Dups.insert(Reg);
275 }
276 }
277 }
278}
279
David Goodwin67a8a7b2009-10-29 19:17:04 +0000280void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
281 const char *tag) {
282 unsigned *KillIndices = State->GetKillIndices();
283 unsigned *DefIndices = State->GetDefIndices();
284 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
285 RegRefs = State->GetRegRefs();
286
287 if (!State->IsLive(Reg)) {
288 KillIndices[Reg] = KillIdx;
289 DefIndices[Reg] = ~0u;
290 RegRefs.erase(Reg);
291 State->LeaveGroup(Reg);
292 DEBUG(errs() << "->g" << State->GetGroup(Reg) << tag);
293 }
294 // Repeat for subregisters.
295 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
296 *Subreg; ++Subreg) {
297 unsigned SubregReg = *Subreg;
298 if (!State->IsLive(SubregReg)) {
299 KillIndices[SubregReg] = KillIdx;
300 DefIndices[SubregReg] = ~0u;
301 RegRefs.erase(SubregReg);
302 State->LeaveGroup(SubregReg);
303 DEBUG(errs() << " " << TRI->getName(SubregReg) << "->g" <<
304 State->GetGroup(SubregReg) << tag);
305 }
306 }
307}
308
David Goodwin34877712009-10-26 19:32:42 +0000309void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI, unsigned Count,
310 std::set<unsigned>& PassthruRegs) {
David Goodwine10deca2009-10-26 22:31:16 +0000311 unsigned *DefIndices = State->GetDefIndices();
312 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
313 RegRefs = State->GetRegRefs();
314
David Goodwin67a8a7b2009-10-29 19:17:04 +0000315 // Handle dead defs by simulating a last-use of the register just
316 // after the def. A dead def can occur because the def is truely
317 // dead, or because only a subregister is live at the def. If we
318 // don't do this the dead def will be incorrectly merged into the
319 // previous def.
David Goodwin34877712009-10-26 19:32:42 +0000320 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
321 MachineOperand &MO = MI->getOperand(i);
322 if (!MO.isReg() || !MO.isDef()) continue;
323 unsigned Reg = MO.getReg();
324 if (Reg == 0) continue;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000325
326 DEBUG(errs() << "\tDead Def: " << TRI->getName(Reg));
327 HandleLastUse(Reg, Count + 1, "");
328 DEBUG(errs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000329 }
330
331 DEBUG(errs() << "\tDef Groups:");
332 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
333 MachineOperand &MO = MI->getOperand(i);
334 if (!MO.isReg() || !MO.isDef()) continue;
335 unsigned Reg = MO.getReg();
336 if (Reg == 0) continue;
337
David Goodwine10deca2009-10-26 22:31:16 +0000338 DEBUG(errs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000339
David Goodwin67a8a7b2009-10-29 19:17:04 +0000340 // If MI's defs have a special allocation requirement, don't allow
David Goodwin34877712009-10-26 19:32:42 +0000341 // any def registers to be changed. Also assume all registers
342 // defined in a call must not be changed (ABI).
343 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq()) {
David Goodwine10deca2009-10-26 22:31:16 +0000344 DEBUG(if (State->GetGroup(Reg) != 0) errs() << "->g0(alloc-req)");
345 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000346 }
347
348 // Any aliased that are live at this point are completely or
David Goodwin67a8a7b2009-10-29 19:17:04 +0000349 // partially defined here, so group those aliases with Reg.
David Goodwin34877712009-10-26 19:32:42 +0000350 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
351 unsigned AliasReg = *Alias;
David Goodwine10deca2009-10-26 22:31:16 +0000352 if (State->IsLive(AliasReg)) {
353 State->UnionGroups(Reg, AliasReg);
354 DEBUG(errs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwin34877712009-10-26 19:32:42 +0000355 TRI->getName(AliasReg) << ")");
356 }
357 }
358
359 // Note register reference...
360 const TargetRegisterClass *RC = NULL;
361 if (i < MI->getDesc().getNumOperands())
362 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000363 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000364 RegRefs.insert(std::make_pair(Reg, RR));
365 }
366
367 DEBUG(errs() << '\n');
David Goodwin67a8a7b2009-10-29 19:17:04 +0000368
369 // Scan the register defs for this instruction and update
370 // live-ranges.
371 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
372 MachineOperand &MO = MI->getOperand(i);
373 if (!MO.isReg() || !MO.isDef()) continue;
374 unsigned Reg = MO.getReg();
375 if (Reg == 0) continue;
376 // Ignore passthru registers for liveness...
377 if (PassthruRegs.count(Reg) != 0) continue;
378
379 // Update def for Reg and subregs.
380 DefIndices[Reg] = Count;
381 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
382 *Subreg; ++Subreg) {
383 unsigned SubregReg = *Subreg;
384 DefIndices[SubregReg] = Count;
385 }
386 }
David Goodwin34877712009-10-26 19:32:42 +0000387}
388
389void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
390 unsigned Count) {
391 DEBUG(errs() << "\tUse Groups:");
David Goodwine10deca2009-10-26 22:31:16 +0000392 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
393 RegRefs = State->GetRegRefs();
David Goodwin34877712009-10-26 19:32:42 +0000394
395 // Scan the register uses for this instruction and update
396 // live-ranges, groups and RegRefs.
397 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
398 MachineOperand &MO = MI->getOperand(i);
399 if (!MO.isReg() || !MO.isUse()) continue;
400 unsigned Reg = MO.getReg();
401 if (Reg == 0) continue;
402
David Goodwine10deca2009-10-26 22:31:16 +0000403 DEBUG(errs() << " " << TRI->getName(Reg) << "=g" <<
404 State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000405
406 // It wasn't previously live but now it is, this is a kill. Forget
407 // the previous live-range information and start a new live-range
408 // for the register.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000409 HandleLastUse(Reg, Count, "(last-use)");
David Goodwin34877712009-10-26 19:32:42 +0000410
411 // If MI's uses have special allocation requirement, don't allow
412 // any use registers to be changed. Also assume all registers
413 // used in a call must not be changed (ABI).
414 if (MI->getDesc().isCall() || MI->getDesc().hasExtraSrcRegAllocReq()) {
David Goodwine10deca2009-10-26 22:31:16 +0000415 DEBUG(if (State->GetGroup(Reg) != 0) errs() << "->g0(alloc-req)");
416 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000417 }
418
419 // Note register reference...
420 const TargetRegisterClass *RC = NULL;
421 if (i < MI->getDesc().getNumOperands())
422 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000423 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000424 RegRefs.insert(std::make_pair(Reg, RR));
425 }
426
427 DEBUG(errs() << '\n');
428
429 // Form a group of all defs and uses of a KILL instruction to ensure
430 // that all registers are renamed as a group.
431 if (MI->getOpcode() == TargetInstrInfo::KILL) {
432 DEBUG(errs() << "\tKill Group:");
433
434 unsigned FirstReg = 0;
435 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
436 MachineOperand &MO = MI->getOperand(i);
437 if (!MO.isReg()) continue;
438 unsigned Reg = MO.getReg();
439 if (Reg == 0) continue;
440
441 if (FirstReg != 0) {
442 DEBUG(errs() << "=" << TRI->getName(Reg));
David Goodwine10deca2009-10-26 22:31:16 +0000443 State->UnionGroups(FirstReg, Reg);
David Goodwin34877712009-10-26 19:32:42 +0000444 } else {
445 DEBUG(errs() << " " << TRI->getName(Reg));
446 FirstReg = Reg;
447 }
448 }
449
David Goodwine10deca2009-10-26 22:31:16 +0000450 DEBUG(errs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000451 }
452}
453
454BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
455 BitVector BV(TRI->getNumRegs(), false);
456 bool first = true;
457
458 // Check all references that need rewriting for Reg. For each, use
459 // the corresponding register class to narrow the set of registers
460 // that are appropriate for renaming.
David Goodwine10deca2009-10-26 22:31:16 +0000461 std::pair<std::multimap<unsigned,
462 AggressiveAntiDepState::RegisterReference>::iterator,
463 std::multimap<unsigned,
464 AggressiveAntiDepState::RegisterReference>::iterator>
465 Range = State->GetRegRefs().equal_range(Reg);
466 for (std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000467 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
468 const TargetRegisterClass *RC = Q->second.RC;
469 if (RC == NULL) continue;
470
471 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
472 if (first) {
473 BV |= RCBV;
474 first = false;
475 } else {
476 BV &= RCBV;
477 }
478
479 DEBUG(errs() << " " << RC->getName());
480 }
481
482 return BV;
483}
484
485bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
486 unsigned AntiDepGroupIndex,
487 std::map<unsigned, unsigned> &RenameMap) {
David Goodwine10deca2009-10-26 22:31:16 +0000488 unsigned *KillIndices = State->GetKillIndices();
489 unsigned *DefIndices = State->GetDefIndices();
490 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
491 RegRefs = State->GetRegRefs();
492
David Goodwin34877712009-10-26 19:32:42 +0000493 // Collect all registers in the same group as AntiDepReg. These all
494 // need to be renamed together if we are to break the
495 // anti-dependence.
496 std::vector<unsigned> Regs;
David Goodwine10deca2009-10-26 22:31:16 +0000497 State->GetGroupRegs(AntiDepGroupIndex, Regs);
David Goodwin34877712009-10-26 19:32:42 +0000498 assert(Regs.size() > 0 && "Empty register group!");
499 if (Regs.size() == 0)
500 return false;
501
502 // Find the "superest" register in the group. At the same time,
503 // collect the BitVector of registers that can be used to rename
504 // each register.
505 DEBUG(errs() << "\tRename Candidates for Group g" << AntiDepGroupIndex << ":\n");
506 std::map<unsigned, BitVector> RenameRegisterMap;
507 unsigned SuperReg = 0;
508 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
509 unsigned Reg = Regs[i];
510 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
511 SuperReg = Reg;
512
513 // If Reg has any references, then collect possible rename regs
514 if (RegRefs.count(Reg) > 0) {
515 DEBUG(errs() << "\t\t" << TRI->getName(Reg) << ":");
516
517 BitVector BV = GetRenameRegisters(Reg);
518 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
519
520 DEBUG(errs() << " ::");
521 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
522 errs() << " " << TRI->getName(r));
523 DEBUG(errs() << "\n");
524 }
525 }
526
527 // All group registers should be a subreg of SuperReg.
528 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
529 unsigned Reg = Regs[i];
530 if (Reg == SuperReg) continue;
531 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
532 assert(IsSub && "Expecting group subregister");
533 if (!IsSub)
534 return false;
535 }
536
537 // FIXME: for now just handle single register in group case...
David Goodwin67a8a7b2009-10-29 19:17:04 +0000538 // FIXME: check only regs that have references...
David Goodwin34877712009-10-26 19:32:42 +0000539 if (Regs.size() > 1)
540 return false;
541
542 // Check each possible rename register for SuperReg. If that register
543 // is available, and the corresponding registers are available for
544 // the other group subregisters, then we can use those registers to
545 // rename.
546 DEBUG(errs() << "\tFind Register:");
547 BitVector SuperBV = RenameRegisterMap[SuperReg];
548 for (int r = SuperBV.find_first(); r != -1; r = SuperBV.find_next(r)) {
549 const unsigned Reg = (unsigned)r;
550 // Don't replace a register with itself.
551 if (Reg == SuperReg) continue;
552
553 DEBUG(errs() << " " << TRI->getName(Reg));
554
555 // If Reg is dead and Reg's most recent def is not before
556 // SuperRegs's kill, it's safe to replace SuperReg with
557 // Reg. We must also check all subregisters of Reg.
David Goodwine10deca2009-10-26 22:31:16 +0000558 if (State->IsLive(Reg) || (KillIndices[SuperReg] > DefIndices[Reg])) {
David Goodwin34877712009-10-26 19:32:42 +0000559 DEBUG(errs() << "(live)");
560 continue;
561 } else {
562 bool found = false;
563 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
564 *Subreg; ++Subreg) {
565 unsigned SubregReg = *Subreg;
David Goodwine10deca2009-10-26 22:31:16 +0000566 if (State->IsLive(SubregReg) || (KillIndices[SuperReg] > DefIndices[SubregReg])) {
David Goodwin34877712009-10-26 19:32:42 +0000567 DEBUG(errs() << "(subreg " << TRI->getName(SubregReg) << " live)");
568 found = true;
569 break;
570 }
571 }
572 if (found)
573 continue;
574 }
575
576 if (Reg != 0) {
577 DEBUG(errs() << '\n');
578 RenameMap.insert(std::pair<unsigned, unsigned>(SuperReg, Reg));
579 return true;
580 }
581 }
582
583 DEBUG(errs() << '\n');
584
585 // No registers are free and available!
586 return false;
587}
588
589/// BreakAntiDependencies - Identifiy anti-dependencies within the
590/// ScheduleDAG and break them by renaming registers.
591///
David Goodwine10deca2009-10-26 22:31:16 +0000592unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
593 std::vector<SUnit>& SUnits,
594 MachineBasicBlock::iterator& Begin,
595 MachineBasicBlock::iterator& End,
596 unsigned InsertPosIndex) {
597 unsigned *KillIndices = State->GetKillIndices();
598 unsigned *DefIndices = State->GetDefIndices();
599 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
600 RegRefs = State->GetRegRefs();
601
David Goodwin34877712009-10-26 19:32:42 +0000602 // The code below assumes that there is at least one instruction,
603 // so just duck out immediately if the block is empty.
604 if (SUnits.empty()) return false;
David Goodwine10deca2009-10-26 22:31:16 +0000605
606 // Manage saved state to enable multiple passes...
607 if (AntiDepTrials > 1) {
608 if (SavedState == NULL) {
609 SavedState = new AggressiveAntiDepState(*State);
610 } else {
611 delete State;
612 State = new AggressiveAntiDepState(*SavedState);
613 }
614 }
David Goodwin34877712009-10-26 19:32:42 +0000615
616 // ...need a map from MI to SUnit.
617 std::map<MachineInstr *, SUnit *> MISUnitMap;
618
619 DEBUG(errs() << "Breaking all anti-dependencies\n");
620 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
621 SUnit *SU = &SUnits[i];
622 MISUnitMap.insert(std::pair<MachineInstr *, SUnit *>(SU->getInstr(), SU));
623 }
624
625#ifndef NDEBUG
626 {
627 DEBUG(errs() << "Available regs:");
628 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
David Goodwine10deca2009-10-26 22:31:16 +0000629 if (!State->IsLive(Reg))
David Goodwin34877712009-10-26 19:32:42 +0000630 DEBUG(errs() << " " << TRI->getName(Reg));
631 }
632 DEBUG(errs() << '\n');
633 }
634#endif
635
636 // Attempt to break anti-dependence edges. Walk the instructions
637 // from the bottom up, tracking information about liveness as we go
638 // to help determine which registers are available.
639 unsigned Broken = 0;
640 unsigned Count = InsertPosIndex - 1;
641 for (MachineBasicBlock::iterator I = End, E = Begin;
642 I != E; --Count) {
643 MachineInstr *MI = --I;
644
645 DEBUG(errs() << "Anti: ");
646 DEBUG(MI->dump());
647
648 std::set<unsigned> PassthruRegs;
649 GetPassthruRegs(MI, PassthruRegs);
650
651 // Process the defs in MI...
652 PrescanInstruction(MI, Count, PassthruRegs);
653
654 std::vector<SDep*> Edges;
655 SUnit *PathSU = MISUnitMap[MI];
656 if (PathSU)
657 AntiDepPathStep(PathSU, Edges);
658
659 // Ignore KILL instructions (they form a group in ScanInstruction
660 // but don't cause any anti-dependence breaking themselves)
661 if (MI->getOpcode() != TargetInstrInfo::KILL) {
662 // Attempt to break each anti-dependency...
663 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
664 SDep *Edge = Edges[i];
665 SUnit *NextSU = Edge->getSUnit();
666
667 if (Edge->getKind() != SDep::Anti) continue;
668
669 unsigned AntiDepReg = Edge->getReg();
670 DEBUG(errs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
671 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
672
673 if (!AllocatableSet.test(AntiDepReg)) {
674 // Don't break anti-dependencies on non-allocatable registers.
675 DEBUG(errs() << " (non-allocatable)\n");
676 continue;
677 } else if (PassthruRegs.count(AntiDepReg) != 0) {
678 // If the anti-dep register liveness "passes-thru", then
679 // don't try to change it. It will be changed along with
680 // the use if required to break an earlier antidep.
681 DEBUG(errs() << " (passthru)\n");
682 continue;
683 } else {
684 // No anti-dep breaking for implicit deps
685 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
686 assert(AntiDepOp != NULL && "Can't find index for defined register operand");
687 if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) {
688 DEBUG(errs() << " (implicit)\n");
689 continue;
690 }
691
692 // If the SUnit has other dependencies on the SUnit that
693 // it anti-depends on, don't bother breaking the
694 // anti-dependency since those edges would prevent such
695 // units from being scheduled past each other
696 // regardless.
697 for (SUnit::pred_iterator P = PathSU->Preds.begin(),
698 PE = PathSU->Preds.end(); P != PE; ++P) {
699 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti)) {
700 DEBUG(errs() << " (real dependency)\n");
701 AntiDepReg = 0;
702 break;
703 }
704 }
705
706 if (AntiDepReg == 0) continue;
707 }
708
709 assert(AntiDepReg != 0);
710 if (AntiDepReg == 0) continue;
711
712 // Determine AntiDepReg's register group.
David Goodwine10deca2009-10-26 22:31:16 +0000713 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwin34877712009-10-26 19:32:42 +0000714 if (GroupIndex == 0) {
715 DEBUG(errs() << " (zero group)\n");
716 continue;
717 }
718
719 DEBUG(errs() << '\n');
720
721 // Look for a suitable register to use to break the anti-dependence.
722 std::map<unsigned, unsigned> RenameMap;
723 if (FindSuitableFreeRegisters(GroupIndex, RenameMap)) {
724 DEBUG(errs() << "\tBreaking anti-dependence edge on "
725 << TRI->getName(AntiDepReg) << ":");
726
727 // Handle each group register...
728 for (std::map<unsigned, unsigned>::iterator
729 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
730 unsigned CurrReg = S->first;
731 unsigned NewReg = S->second;
732
733 DEBUG(errs() << " " << TRI->getName(CurrReg) << "->" <<
734 TRI->getName(NewReg) << "(" <<
735 RegRefs.count(CurrReg) << " refs)");
736
737 // Update the references to the old register CurrReg to
738 // refer to the new register NewReg.
David Goodwine10deca2009-10-26 22:31:16 +0000739 std::pair<std::multimap<unsigned,
740 AggressiveAntiDepState::RegisterReference>::iterator,
741 std::multimap<unsigned,
742 AggressiveAntiDepState::RegisterReference>::iterator>
David Goodwin34877712009-10-26 19:32:42 +0000743 Range = RegRefs.equal_range(CurrReg);
David Goodwine10deca2009-10-26 22:31:16 +0000744 for (std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000745 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
746 Q->second.Operand->setReg(NewReg);
747 }
748
749 // We just went back in time and modified history; the
750 // liveness information for CurrReg is now inconsistent. Set
751 // the state as if it were dead.
David Goodwine10deca2009-10-26 22:31:16 +0000752 State->UnionGroups(NewReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000753 RegRefs.erase(NewReg);
754 DefIndices[NewReg] = DefIndices[CurrReg];
755 KillIndices[NewReg] = KillIndices[CurrReg];
756
David Goodwine10deca2009-10-26 22:31:16 +0000757 State->UnionGroups(CurrReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000758 RegRefs.erase(CurrReg);
759 DefIndices[CurrReg] = KillIndices[CurrReg];
760 KillIndices[CurrReg] = ~0u;
761 assert(((KillIndices[CurrReg] == ~0u) !=
762 (DefIndices[CurrReg] == ~0u)) &&
763 "Kill and Def maps aren't consistent for AntiDepReg!");
764 }
765
766 ++Broken;
767 DEBUG(errs() << '\n');
768 }
769 }
770 }
771
772 ScanInstruction(MI, Count);
773 }
774
775 return Broken;
776}