blob: 5506a1f526051b920f65da88f2d2c7a620135efd [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
David Goodwin5b3c3082009-10-29 23:30:59 +0000198 std::set<unsigned> PassthruRegs;
199 GetPassthruRegs(MI, PassthruRegs);
200 PrescanInstruction(MI, Count, PassthruRegs);
201 ScanInstruction(MI, Count);
202
David Goodwine10deca2009-10-26 22:31:16 +0000203 DEBUG(errs() << "Observe: ");
204 DEBUG(MI->dump());
David Goodwin5b3c3082009-10-29 23:30:59 +0000205 DEBUG(errs() << "\tRegs:");
David Goodwine10deca2009-10-26 22:31:16 +0000206
207 unsigned *DefIndices = State->GetDefIndices();
208 for (unsigned Reg = 0; Reg != TargetRegisterInfo::FirstVirtualRegister; ++Reg) {
209 // If Reg is current live, then mark that it can't be renamed as
210 // we don't know the extent of its live-range anymore (now that it
211 // has been scheduled). If it is not live but was defined in the
212 // previous schedule region, then set its def index to the most
213 // conservative location (i.e. the beginning of the previous
214 // schedule region).
215 if (State->IsLive(Reg)) {
216 DEBUG(if (State->GetGroup(Reg) != 0)
217 errs() << " " << TRI->getName(Reg) << "=g" <<
218 State->GetGroup(Reg) << "->g0(region live-out)");
219 State->UnionGroups(Reg, 0);
220 } else if ((DefIndices[Reg] < InsertPosIndex) && (DefIndices[Reg] >= Count)) {
221 DefIndices[Reg] = Count;
222 }
223 }
David Goodwin5b3c3082009-10-29 23:30:59 +0000224 DEBUG(errs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000225
226 // We're starting a new schedule region so forget any saved state.
227 delete SavedState;
228 SavedState = NULL;
229}
230
David Goodwin34877712009-10-26 19:32:42 +0000231bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
232 MachineOperand& MO)
233{
234 if (!MO.isReg() || !MO.isImplicit())
235 return false;
236
237 unsigned Reg = MO.getReg();
238 if (Reg == 0)
239 return false;
240
241 MachineOperand *Op = NULL;
242 if (MO.isDef())
243 Op = MI->findRegisterUseOperand(Reg, true);
244 else
245 Op = MI->findRegisterDefOperand(Reg);
246
247 return((Op != NULL) && Op->isImplicit());
248}
249
250void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
251 std::set<unsigned>& PassthruRegs) {
252 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
253 MachineOperand &MO = MI->getOperand(i);
254 if (!MO.isReg()) continue;
255 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
256 IsImplicitDefUse(MI, MO)) {
257 const unsigned Reg = MO.getReg();
258 PassthruRegs.insert(Reg);
259 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
260 *Subreg; ++Subreg) {
261 PassthruRegs.insert(*Subreg);
262 }
263 }
264 }
265}
266
267/// AntiDepPathStep - Return SUnit that SU has an anti-dependence on.
268static void AntiDepPathStep(SUnit *SU, std::vector<SDep*>& Edges) {
269 SmallSet<unsigned, 8> Dups;
270 for (SUnit::pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
271 P != PE; ++P) {
272 if (P->getKind() == SDep::Anti) {
273 unsigned Reg = P->getReg();
274 if (Dups.count(Reg) == 0) {
275 Edges.push_back(&*P);
276 Dups.insert(Reg);
277 }
278 }
279 }
280}
281
David Goodwin67a8a7b2009-10-29 19:17:04 +0000282void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
283 const char *tag) {
284 unsigned *KillIndices = State->GetKillIndices();
285 unsigned *DefIndices = State->GetDefIndices();
286 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
287 RegRefs = State->GetRegRefs();
288
289 if (!State->IsLive(Reg)) {
290 KillIndices[Reg] = KillIdx;
291 DefIndices[Reg] = ~0u;
292 RegRefs.erase(Reg);
293 State->LeaveGroup(Reg);
294 DEBUG(errs() << "->g" << State->GetGroup(Reg) << tag);
295 }
296 // Repeat for subregisters.
297 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
298 *Subreg; ++Subreg) {
299 unsigned SubregReg = *Subreg;
300 if (!State->IsLive(SubregReg)) {
301 KillIndices[SubregReg] = KillIdx;
302 DefIndices[SubregReg] = ~0u;
303 RegRefs.erase(SubregReg);
304 State->LeaveGroup(SubregReg);
305 DEBUG(errs() << " " << TRI->getName(SubregReg) << "->g" <<
306 State->GetGroup(SubregReg) << tag);
307 }
308 }
309}
310
David Goodwin34877712009-10-26 19:32:42 +0000311void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI, unsigned Count,
312 std::set<unsigned>& PassthruRegs) {
David Goodwine10deca2009-10-26 22:31:16 +0000313 unsigned *DefIndices = State->GetDefIndices();
314 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
315 RegRefs = State->GetRegRefs();
316
David Goodwin67a8a7b2009-10-29 19:17:04 +0000317 // Handle dead defs by simulating a last-use of the register just
318 // after the def. A dead def can occur because the def is truely
319 // dead, or because only a subregister is live at the def. If we
320 // don't do this the dead def will be incorrectly merged into the
321 // previous def.
David Goodwin34877712009-10-26 19:32:42 +0000322 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
323 MachineOperand &MO = MI->getOperand(i);
324 if (!MO.isReg() || !MO.isDef()) continue;
325 unsigned Reg = MO.getReg();
326 if (Reg == 0) continue;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000327
328 DEBUG(errs() << "\tDead Def: " << TRI->getName(Reg));
329 HandleLastUse(Reg, Count + 1, "");
330 DEBUG(errs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000331 }
332
333 DEBUG(errs() << "\tDef Groups:");
334 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
335 MachineOperand &MO = MI->getOperand(i);
336 if (!MO.isReg() || !MO.isDef()) continue;
337 unsigned Reg = MO.getReg();
338 if (Reg == 0) continue;
339
David Goodwine10deca2009-10-26 22:31:16 +0000340 DEBUG(errs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000341
David Goodwin67a8a7b2009-10-29 19:17:04 +0000342 // If MI's defs have a special allocation requirement, don't allow
David Goodwin34877712009-10-26 19:32:42 +0000343 // any def registers to be changed. Also assume all registers
344 // defined in a call must not be changed (ABI).
345 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq()) {
David Goodwine10deca2009-10-26 22:31:16 +0000346 DEBUG(if (State->GetGroup(Reg) != 0) errs() << "->g0(alloc-req)");
347 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000348 }
349
350 // Any aliased that are live at this point are completely or
David Goodwin67a8a7b2009-10-29 19:17:04 +0000351 // partially defined here, so group those aliases with Reg.
David Goodwin34877712009-10-26 19:32:42 +0000352 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
353 unsigned AliasReg = *Alias;
David Goodwine10deca2009-10-26 22:31:16 +0000354 if (State->IsLive(AliasReg)) {
355 State->UnionGroups(Reg, AliasReg);
356 DEBUG(errs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwin34877712009-10-26 19:32:42 +0000357 TRI->getName(AliasReg) << ")");
358 }
359 }
360
361 // Note register reference...
362 const TargetRegisterClass *RC = NULL;
363 if (i < MI->getDesc().getNumOperands())
364 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000365 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000366 RegRefs.insert(std::make_pair(Reg, RR));
367 }
368
369 DEBUG(errs() << '\n');
David Goodwin67a8a7b2009-10-29 19:17:04 +0000370
371 // Scan the register defs for this instruction and update
372 // live-ranges.
373 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;
378 // Ignore passthru registers for liveness...
379 if (PassthruRegs.count(Reg) != 0) continue;
380
381 // Update def for Reg and subregs.
382 DefIndices[Reg] = Count;
383 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
384 *Subreg; ++Subreg) {
385 unsigned SubregReg = *Subreg;
386 DefIndices[SubregReg] = Count;
387 }
388 }
David Goodwin34877712009-10-26 19:32:42 +0000389}
390
391void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
392 unsigned Count) {
393 DEBUG(errs() << "\tUse Groups:");
David Goodwine10deca2009-10-26 22:31:16 +0000394 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
395 RegRefs = State->GetRegRefs();
David Goodwin34877712009-10-26 19:32:42 +0000396
397 // Scan the register uses for this instruction and update
398 // live-ranges, groups and RegRefs.
399 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
400 MachineOperand &MO = MI->getOperand(i);
401 if (!MO.isReg() || !MO.isUse()) continue;
402 unsigned Reg = MO.getReg();
403 if (Reg == 0) continue;
404
David Goodwine10deca2009-10-26 22:31:16 +0000405 DEBUG(errs() << " " << TRI->getName(Reg) << "=g" <<
406 State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000407
408 // It wasn't previously live but now it is, this is a kill. Forget
409 // the previous live-range information and start a new live-range
410 // for the register.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000411 HandleLastUse(Reg, Count, "(last-use)");
David Goodwin34877712009-10-26 19:32:42 +0000412
413 // If MI's uses have special allocation requirement, don't allow
414 // any use registers to be changed. Also assume all registers
415 // used in a call must not be changed (ABI).
416 if (MI->getDesc().isCall() || MI->getDesc().hasExtraSrcRegAllocReq()) {
David Goodwine10deca2009-10-26 22:31:16 +0000417 DEBUG(if (State->GetGroup(Reg) != 0) errs() << "->g0(alloc-req)");
418 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000419 }
420
421 // Note register reference...
422 const TargetRegisterClass *RC = NULL;
423 if (i < MI->getDesc().getNumOperands())
424 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000425 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000426 RegRefs.insert(std::make_pair(Reg, RR));
427 }
428
429 DEBUG(errs() << '\n');
430
431 // Form a group of all defs and uses of a KILL instruction to ensure
432 // that all registers are renamed as a group.
433 if (MI->getOpcode() == TargetInstrInfo::KILL) {
434 DEBUG(errs() << "\tKill Group:");
435
436 unsigned FirstReg = 0;
437 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
438 MachineOperand &MO = MI->getOperand(i);
439 if (!MO.isReg()) continue;
440 unsigned Reg = MO.getReg();
441 if (Reg == 0) continue;
442
443 if (FirstReg != 0) {
444 DEBUG(errs() << "=" << TRI->getName(Reg));
David Goodwine10deca2009-10-26 22:31:16 +0000445 State->UnionGroups(FirstReg, Reg);
David Goodwin34877712009-10-26 19:32:42 +0000446 } else {
447 DEBUG(errs() << " " << TRI->getName(Reg));
448 FirstReg = Reg;
449 }
450 }
451
David Goodwine10deca2009-10-26 22:31:16 +0000452 DEBUG(errs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000453 }
454}
455
456BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
457 BitVector BV(TRI->getNumRegs(), false);
458 bool first = true;
459
460 // Check all references that need rewriting for Reg. For each, use
461 // the corresponding register class to narrow the set of registers
462 // that are appropriate for renaming.
David Goodwine10deca2009-10-26 22:31:16 +0000463 std::pair<std::multimap<unsigned,
464 AggressiveAntiDepState::RegisterReference>::iterator,
465 std::multimap<unsigned,
466 AggressiveAntiDepState::RegisterReference>::iterator>
467 Range = State->GetRegRefs().equal_range(Reg);
468 for (std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000469 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
470 const TargetRegisterClass *RC = Q->second.RC;
471 if (RC == NULL) continue;
472
473 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
474 if (first) {
475 BV |= RCBV;
476 first = false;
477 } else {
478 BV &= RCBV;
479 }
480
481 DEBUG(errs() << " " << RC->getName());
482 }
483
484 return BV;
485}
486
487bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
488 unsigned AntiDepGroupIndex,
489 std::map<unsigned, unsigned> &RenameMap) {
David Goodwine10deca2009-10-26 22:31:16 +0000490 unsigned *KillIndices = State->GetKillIndices();
491 unsigned *DefIndices = State->GetDefIndices();
492 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
493 RegRefs = State->GetRegRefs();
494
David Goodwin34877712009-10-26 19:32:42 +0000495 // Collect all registers in the same group as AntiDepReg. These all
496 // need to be renamed together if we are to break the
497 // anti-dependence.
498 std::vector<unsigned> Regs;
David Goodwine10deca2009-10-26 22:31:16 +0000499 State->GetGroupRegs(AntiDepGroupIndex, Regs);
David Goodwin34877712009-10-26 19:32:42 +0000500 assert(Regs.size() > 0 && "Empty register group!");
501 if (Regs.size() == 0)
502 return false;
503
504 // Find the "superest" register in the group. At the same time,
505 // collect the BitVector of registers that can be used to rename
506 // each register.
507 DEBUG(errs() << "\tRename Candidates for Group g" << AntiDepGroupIndex << ":\n");
508 std::map<unsigned, BitVector> RenameRegisterMap;
509 unsigned SuperReg = 0;
510 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
511 unsigned Reg = Regs[i];
512 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
513 SuperReg = Reg;
514
515 // If Reg has any references, then collect possible rename regs
516 if (RegRefs.count(Reg) > 0) {
517 DEBUG(errs() << "\t\t" << TRI->getName(Reg) << ":");
518
519 BitVector BV = GetRenameRegisters(Reg);
520 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
521
522 DEBUG(errs() << " ::");
523 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
524 errs() << " " << TRI->getName(r));
525 DEBUG(errs() << "\n");
526 }
527 }
528
529 // All group registers should be a subreg of SuperReg.
530 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
531 unsigned Reg = Regs[i];
532 if (Reg == SuperReg) continue;
533 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
534 assert(IsSub && "Expecting group subregister");
535 if (!IsSub)
536 return false;
537 }
538
539 // FIXME: for now just handle single register in group case...
David Goodwin67a8a7b2009-10-29 19:17:04 +0000540 // FIXME: check only regs that have references...
David Goodwin34877712009-10-26 19:32:42 +0000541 if (Regs.size() > 1)
542 return false;
543
544 // Check each possible rename register for SuperReg. If that register
545 // is available, and the corresponding registers are available for
546 // the other group subregisters, then we can use those registers to
547 // rename.
548 DEBUG(errs() << "\tFind Register:");
549 BitVector SuperBV = RenameRegisterMap[SuperReg];
550 for (int r = SuperBV.find_first(); r != -1; r = SuperBV.find_next(r)) {
551 const unsigned Reg = (unsigned)r;
552 // Don't replace a register with itself.
553 if (Reg == SuperReg) continue;
554
555 DEBUG(errs() << " " << TRI->getName(Reg));
556
557 // If Reg is dead and Reg's most recent def is not before
558 // SuperRegs's kill, it's safe to replace SuperReg with
559 // Reg. We must also check all subregisters of Reg.
David Goodwine10deca2009-10-26 22:31:16 +0000560 if (State->IsLive(Reg) || (KillIndices[SuperReg] > DefIndices[Reg])) {
David Goodwin34877712009-10-26 19:32:42 +0000561 DEBUG(errs() << "(live)");
562 continue;
563 } else {
564 bool found = false;
565 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
566 *Subreg; ++Subreg) {
567 unsigned SubregReg = *Subreg;
David Goodwine10deca2009-10-26 22:31:16 +0000568 if (State->IsLive(SubregReg) || (KillIndices[SuperReg] > DefIndices[SubregReg])) {
David Goodwin34877712009-10-26 19:32:42 +0000569 DEBUG(errs() << "(subreg " << TRI->getName(SubregReg) << " live)");
570 found = true;
571 break;
572 }
573 }
574 if (found)
575 continue;
576 }
577
578 if (Reg != 0) {
579 DEBUG(errs() << '\n');
580 RenameMap.insert(std::pair<unsigned, unsigned>(SuperReg, Reg));
581 return true;
582 }
583 }
584
585 DEBUG(errs() << '\n');
586
587 // No registers are free and available!
588 return false;
589}
590
591/// BreakAntiDependencies - Identifiy anti-dependencies within the
592/// ScheduleDAG and break them by renaming registers.
593///
David Goodwine10deca2009-10-26 22:31:16 +0000594unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
595 std::vector<SUnit>& SUnits,
596 MachineBasicBlock::iterator& Begin,
597 MachineBasicBlock::iterator& End,
598 unsigned InsertPosIndex) {
599 unsigned *KillIndices = State->GetKillIndices();
600 unsigned *DefIndices = State->GetDefIndices();
601 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
602 RegRefs = State->GetRegRefs();
603
David Goodwin34877712009-10-26 19:32:42 +0000604 // The code below assumes that there is at least one instruction,
605 // so just duck out immediately if the block is empty.
606 if (SUnits.empty()) return false;
David Goodwine10deca2009-10-26 22:31:16 +0000607
608 // Manage saved state to enable multiple passes...
609 if (AntiDepTrials > 1) {
610 if (SavedState == NULL) {
611 SavedState = new AggressiveAntiDepState(*State);
612 } else {
613 delete State;
614 State = new AggressiveAntiDepState(*SavedState);
615 }
616 }
David Goodwin34877712009-10-26 19:32:42 +0000617
618 // ...need a map from MI to SUnit.
619 std::map<MachineInstr *, SUnit *> MISUnitMap;
620
621 DEBUG(errs() << "Breaking all anti-dependencies\n");
622 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
623 SUnit *SU = &SUnits[i];
624 MISUnitMap.insert(std::pair<MachineInstr *, SUnit *>(SU->getInstr(), SU));
625 }
626
627#ifndef NDEBUG
628 {
629 DEBUG(errs() << "Available regs:");
630 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
David Goodwine10deca2009-10-26 22:31:16 +0000631 if (!State->IsLive(Reg))
David Goodwin34877712009-10-26 19:32:42 +0000632 DEBUG(errs() << " " << TRI->getName(Reg));
633 }
634 DEBUG(errs() << '\n');
635 }
636#endif
637
638 // Attempt to break anti-dependence edges. Walk the instructions
639 // from the bottom up, tracking information about liveness as we go
640 // to help determine which registers are available.
641 unsigned Broken = 0;
642 unsigned Count = InsertPosIndex - 1;
643 for (MachineBasicBlock::iterator I = End, E = Begin;
644 I != E; --Count) {
645 MachineInstr *MI = --I;
646
647 DEBUG(errs() << "Anti: ");
648 DEBUG(MI->dump());
649
650 std::set<unsigned> PassthruRegs;
651 GetPassthruRegs(MI, PassthruRegs);
652
653 // Process the defs in MI...
654 PrescanInstruction(MI, Count, PassthruRegs);
655
656 std::vector<SDep*> Edges;
657 SUnit *PathSU = MISUnitMap[MI];
658 if (PathSU)
659 AntiDepPathStep(PathSU, Edges);
660
661 // Ignore KILL instructions (they form a group in ScanInstruction
662 // but don't cause any anti-dependence breaking themselves)
663 if (MI->getOpcode() != TargetInstrInfo::KILL) {
664 // Attempt to break each anti-dependency...
665 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
666 SDep *Edge = Edges[i];
667 SUnit *NextSU = Edge->getSUnit();
668
669 if (Edge->getKind() != SDep::Anti) continue;
670
671 unsigned AntiDepReg = Edge->getReg();
672 DEBUG(errs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
673 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
674
675 if (!AllocatableSet.test(AntiDepReg)) {
676 // Don't break anti-dependencies on non-allocatable registers.
677 DEBUG(errs() << " (non-allocatable)\n");
678 continue;
679 } else if (PassthruRegs.count(AntiDepReg) != 0) {
680 // If the anti-dep register liveness "passes-thru", then
681 // don't try to change it. It will be changed along with
682 // the use if required to break an earlier antidep.
683 DEBUG(errs() << " (passthru)\n");
684 continue;
685 } else {
686 // No anti-dep breaking for implicit deps
687 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
688 assert(AntiDepOp != NULL && "Can't find index for defined register operand");
689 if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) {
690 DEBUG(errs() << " (implicit)\n");
691 continue;
692 }
693
694 // If the SUnit has other dependencies on the SUnit that
695 // it anti-depends on, don't bother breaking the
696 // anti-dependency since those edges would prevent such
697 // units from being scheduled past each other
698 // regardless.
699 for (SUnit::pred_iterator P = PathSU->Preds.begin(),
700 PE = PathSU->Preds.end(); P != PE; ++P) {
701 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti)) {
702 DEBUG(errs() << " (real dependency)\n");
703 AntiDepReg = 0;
704 break;
705 }
706 }
707
708 if (AntiDepReg == 0) continue;
709 }
710
711 assert(AntiDepReg != 0);
712 if (AntiDepReg == 0) continue;
713
714 // Determine AntiDepReg's register group.
David Goodwine10deca2009-10-26 22:31:16 +0000715 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwin34877712009-10-26 19:32:42 +0000716 if (GroupIndex == 0) {
717 DEBUG(errs() << " (zero group)\n");
718 continue;
719 }
720
721 DEBUG(errs() << '\n');
722
723 // Look for a suitable register to use to break the anti-dependence.
724 std::map<unsigned, unsigned> RenameMap;
725 if (FindSuitableFreeRegisters(GroupIndex, RenameMap)) {
726 DEBUG(errs() << "\tBreaking anti-dependence edge on "
727 << TRI->getName(AntiDepReg) << ":");
728
729 // Handle each group register...
730 for (std::map<unsigned, unsigned>::iterator
731 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
732 unsigned CurrReg = S->first;
733 unsigned NewReg = S->second;
734
735 DEBUG(errs() << " " << TRI->getName(CurrReg) << "->" <<
736 TRI->getName(NewReg) << "(" <<
737 RegRefs.count(CurrReg) << " refs)");
738
739 // Update the references to the old register CurrReg to
740 // refer to the new register NewReg.
David Goodwine10deca2009-10-26 22:31:16 +0000741 std::pair<std::multimap<unsigned,
742 AggressiveAntiDepState::RegisterReference>::iterator,
743 std::multimap<unsigned,
744 AggressiveAntiDepState::RegisterReference>::iterator>
David Goodwin34877712009-10-26 19:32:42 +0000745 Range = RegRefs.equal_range(CurrReg);
David Goodwine10deca2009-10-26 22:31:16 +0000746 for (std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000747 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
748 Q->second.Operand->setReg(NewReg);
749 }
750
751 // We just went back in time and modified history; the
752 // liveness information for CurrReg is now inconsistent. Set
753 // the state as if it were dead.
David Goodwine10deca2009-10-26 22:31:16 +0000754 State->UnionGroups(NewReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000755 RegRefs.erase(NewReg);
756 DefIndices[NewReg] = DefIndices[CurrReg];
757 KillIndices[NewReg] = KillIndices[CurrReg];
758
David Goodwine10deca2009-10-26 22:31:16 +0000759 State->UnionGroups(CurrReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000760 RegRefs.erase(CurrReg);
761 DefIndices[CurrReg] = KillIndices[CurrReg];
762 KillIndices[CurrReg] = ~0u;
763 assert(((KillIndices[CurrReg] == ~0u) !=
764 (DefIndices[CurrReg] == ~0u)) &&
765 "Kill and Def maps aren't consistent for AntiDepReg!");
766 }
767
768 ++Broken;
769 DEBUG(errs() << '\n');
770 }
771 }
772 }
773
774 ScanInstruction(MI, Count);
775 }
776
777 return Broken;
778}