blob: 874f018db9f92e1a206b41172851df5743cfb7db [file] [log] [blame]
Jim Grosbach2973b572010-01-06 16:48:02 +00001//===----- AggressiveAntiDepBreaker.cpp - Anti-dep breaker ----------------===//
David Goodwin34877712009-10-26 19:32:42 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AggressiveAntiDepBreaker class, which
11// implements register anti-dependence breaking during post-RA
12// scheduling. It attempts to break all anti-dependencies within a
13// block.
14//
15//===----------------------------------------------------------------------===//
16
David Goodwin4de099d2009-11-03 20:57:50 +000017#define DEBUG_TYPE "post-RA-sched"
David Goodwin34877712009-10-26 19:32:42 +000018#include "AggressiveAntiDepBreaker.h"
19#include "llvm/CodeGen/MachineBasicBlock.h"
20#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineInstr.h"
22#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
Evan Cheng46df4eb2010-06-16 07:35:02 +000024#include "llvm/Target/TargetInstrInfo.h"
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",
Bob Wilson347fa3f2010-04-09 21:38:26 +000034 cl::desc("Debug control for aggressive anti-dep breaker"),
35 cl::init(0), cl::Hidden);
David Goodwin3e72d302009-11-19 23:12:37 +000036static cl::opt<int>
37DebugMod("agg-antidep-debugmod",
Bob Wilson347fa3f2010-04-09 21:38:26 +000038 cl::desc("Debug control for aggressive anti-dep breaker"),
39 cl::init(0), cl::Hidden);
David Goodwin3e72d302009-11-19 23:12:37 +000040
David Goodwin990d2852009-12-09 17:18:22 +000041AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
42 MachineBasicBlock *BB) :
43 NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0) {
Bill Wendlinge0104092010-07-15 06:04:38 +000044 KillIndices.reserve(TargetRegs);
45 DefIndices.reserve(TargetRegs);
David Goodwin34877712009-10-26 19:32:42 +000046
David Goodwin990d2852009-12-09 17:18:22 +000047 const unsigned BBSize = BB->size();
48 for (unsigned i = 0; i < NumTargetRegs; ++i) {
49 // Initialize all registers to be in their own group. Initially we
50 // assign the register to the same-indexed GroupNode.
51 GroupNodeIndices[i] = i;
52 // Initialize the indices to indicate that no registers are live.
53 KillIndices[i] = ~0u;
54 DefIndices[i] = BBSize;
55 }
David Goodwin34877712009-10-26 19:32:42 +000056}
57
David Goodwine10deca2009-10-26 22:31:16 +000058unsigned AggressiveAntiDepState::GetGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000059{
60 unsigned Node = GroupNodeIndices[Reg];
61 while (GroupNodes[Node] != Node)
62 Node = GroupNodes[Node];
63
64 return Node;
65}
66
David Goodwin87d21b92009-11-13 19:52:48 +000067void AggressiveAntiDepState::GetGroupRegs(
68 unsigned Group,
69 std::vector<unsigned> &Regs,
70 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
David Goodwin34877712009-10-26 19:32:42 +000071{
David Goodwin990d2852009-12-09 17:18:22 +000072 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
David Goodwin87d21b92009-11-13 19:52:48 +000073 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
David Goodwin34877712009-10-26 19:32:42 +000074 Regs.push_back(Reg);
75 }
76}
77
David Goodwine10deca2009-10-26 22:31:16 +000078unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2)
David Goodwin34877712009-10-26 19:32:42 +000079{
80 assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
81 assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
Jim Grosbach2973b572010-01-06 16:48:02 +000082
David Goodwin34877712009-10-26 19:32:42 +000083 // find group for each register
84 unsigned Group1 = GetGroup(Reg1);
85 unsigned Group2 = GetGroup(Reg2);
Jim Grosbach2973b572010-01-06 16:48:02 +000086
David Goodwin34877712009-10-26 19:32:42 +000087 // if either group is 0, then that must become the parent
88 unsigned Parent = (Group1 == 0) ? Group1 : Group2;
89 unsigned Other = (Parent == Group1) ? Group2 : Group1;
90 GroupNodes.at(Other) = Parent;
91 return Parent;
92}
Jim Grosbach2973b572010-01-06 16:48:02 +000093
David Goodwine10deca2009-10-26 22:31:16 +000094unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000095{
96 // Create a new GroupNode for Reg. Reg's existing GroupNode must
97 // stay as is because there could be other GroupNodes referring to
98 // it.
99 unsigned idx = GroupNodes.size();
100 GroupNodes.push_back(idx);
101 GroupNodeIndices[Reg] = idx;
102 return idx;
103}
104
David Goodwine10deca2009-10-26 22:31:16 +0000105bool AggressiveAntiDepState::IsLive(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +0000106{
107 // KillIndex must be defined and DefIndex not defined for a register
108 // to be live.
109 return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
110}
111
David Goodwine10deca2009-10-26 22:31:16 +0000112
113
114AggressiveAntiDepBreaker::
David Goodwin0855dee2009-11-10 00:15:47 +0000115AggressiveAntiDepBreaker(MachineFunction& MFi,
Jim Grosbach2973b572010-01-06 16:48:02 +0000116 TargetSubtarget::RegClassVector& CriticalPathRCs) :
David Goodwine10deca2009-10-26 22:31:16 +0000117 AntiDepBreaker(), MF(MFi),
118 MRI(MF.getRegInfo()),
Evan Cheng46df4eb2010-06-16 07:35:02 +0000119 TII(MF.getTarget().getInstrInfo()),
David Goodwine10deca2009-10-26 22:31:16 +0000120 TRI(MF.getTarget().getRegisterInfo()),
121 AllocatableSet(TRI->getAllocatableSet(MF)),
David Goodwin557bbe62009-11-20 19:32:48 +0000122 State(NULL) {
David Goodwin87d21b92009-11-13 19:52:48 +0000123 /* Collect a bitset of all registers that are only broken if they
124 are on the critical path. */
125 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
126 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
127 if (CriticalPathSet.none())
128 CriticalPathSet = CPSet;
129 else
130 CriticalPathSet |= CPSet;
131 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000132
David Greene5393b252009-12-24 00:14:25 +0000133 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000134 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwin87d21b92009-11-13 19:52:48 +0000135 r = CriticalPathSet.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000136 dbgs() << " " << TRI->getName(r));
137 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000138}
139
140AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
141 delete State;
David Goodwine10deca2009-10-26 22:31:16 +0000142}
143
144void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
145 assert(State == NULL);
David Goodwin990d2852009-12-09 17:18:22 +0000146 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine10deca2009-10-26 22:31:16 +0000147
148 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
Bill Wendlinge0104092010-07-15 06:04:38 +0000149 std::vector<unsigned> &KillIndices = State->GetKillIndices();
150 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwine10deca2009-10-26 22:31:16 +0000151
152 // Determine the live-out physregs for this block.
153 if (IsReturnBlock) {
154 // In a return block, examine the function live-out regs.
155 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
156 E = MRI.liveout_end(); I != E; ++I) {
157 unsigned Reg = *I;
158 State->UnionGroups(Reg, 0);
159 KillIndices[Reg] = BB->size();
160 DefIndices[Reg] = ~0u;
161 // Repeat, for all aliases.
162 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
163 unsigned AliasReg = *Alias;
164 State->UnionGroups(AliasReg, 0);
165 KillIndices[AliasReg] = BB->size();
166 DefIndices[AliasReg] = ~0u;
167 }
168 }
David Goodwine10deca2009-10-26 22:31:16 +0000169 }
170
Evan Cheng46df4eb2010-06-16 07:35:02 +0000171 // In a non-return block, examine the live-in regs of all successors.
172 // Note a return block can have successors if the return instruction is
173 // predicated.
174 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
175 SE = BB->succ_end(); SI != SE; ++SI)
176 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
177 E = (*SI)->livein_end(); I != E; ++I) {
178 unsigned Reg = *I;
179 State->UnionGroups(Reg, 0);
180 KillIndices[Reg] = BB->size();
181 DefIndices[Reg] = ~0u;
182 // Repeat, for all aliases.
183 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
184 unsigned AliasReg = *Alias;
185 State->UnionGroups(AliasReg, 0);
186 KillIndices[AliasReg] = BB->size();
187 DefIndices[AliasReg] = ~0u;
188 }
189 }
190
David Goodwine10deca2009-10-26 22:31:16 +0000191 // Mark live-out callee-saved registers. In a return block this is
192 // all callee-saved registers. In non-return this is any
193 // callee-saved register that is not saved in the prolog.
194 const MachineFrameInfo *MFI = MF.getFrameInfo();
195 BitVector Pristine = MFI->getPristineRegs(BB);
196 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
197 unsigned Reg = *I;
198 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
199 State->UnionGroups(Reg, 0);
200 KillIndices[Reg] = BB->size();
201 DefIndices[Reg] = ~0u;
202 // Repeat, for all aliases.
203 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
204 unsigned AliasReg = *Alias;
205 State->UnionGroups(AliasReg, 0);
206 KillIndices[AliasReg] = BB->size();
207 DefIndices[AliasReg] = ~0u;
208 }
209 }
210}
211
212void AggressiveAntiDepBreaker::FinishBlock() {
213 delete State;
214 State = NULL;
David Goodwine10deca2009-10-26 22:31:16 +0000215}
216
217void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000218 unsigned InsertPosIndex) {
David Goodwine10deca2009-10-26 22:31:16 +0000219 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
220
David Goodwin5b3c3082009-10-29 23:30:59 +0000221 std::set<unsigned> PassthruRegs;
222 GetPassthruRegs(MI, PassthruRegs);
223 PrescanInstruction(MI, Count, PassthruRegs);
224 ScanInstruction(MI, Count);
225
David Greene5393b252009-12-24 00:14:25 +0000226 DEBUG(dbgs() << "Observe: ");
David Goodwine10deca2009-10-26 22:31:16 +0000227 DEBUG(MI->dump());
David Greene5393b252009-12-24 00:14:25 +0000228 DEBUG(dbgs() << "\tRegs:");
David Goodwine10deca2009-10-26 22:31:16 +0000229
Bill Wendlinge0104092010-07-15 06:04:38 +0000230 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwin990d2852009-12-09 17:18:22 +0000231 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine10deca2009-10-26 22:31:16 +0000232 // If Reg is current live, then mark that it can't be renamed as
233 // we don't know the extent of its live-range anymore (now that it
234 // has been scheduled). If it is not live but was defined in the
235 // previous schedule region, then set its def index to the most
236 // conservative location (i.e. the beginning of the previous
237 // schedule region).
238 if (State->IsLive(Reg)) {
239 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbach2973b572010-01-06 16:48:02 +0000240 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine10deca2009-10-26 22:31:16 +0000241 State->GetGroup(Reg) << "->g0(region live-out)");
242 State->UnionGroups(Reg, 0);
Jim Grosbach2973b572010-01-06 16:48:02 +0000243 } else if ((DefIndices[Reg] < InsertPosIndex)
244 && (DefIndices[Reg] >= Count)) {
David Goodwine10deca2009-10-26 22:31:16 +0000245 DefIndices[Reg] = Count;
246 }
247 }
David Greene5393b252009-12-24 00:14:25 +0000248 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000249}
250
David Goodwin34877712009-10-26 19:32:42 +0000251bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000252 MachineOperand& MO)
David Goodwin34877712009-10-26 19:32:42 +0000253{
254 if (!MO.isReg() || !MO.isImplicit())
255 return false;
256
257 unsigned Reg = MO.getReg();
258 if (Reg == 0)
259 return false;
260
261 MachineOperand *Op = NULL;
262 if (MO.isDef())
263 Op = MI->findRegisterUseOperand(Reg, true);
264 else
265 Op = MI->findRegisterDefOperand(Reg);
266
267 return((Op != NULL) && Op->isImplicit());
268}
269
270void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
271 std::set<unsigned>& PassthruRegs) {
272 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
273 MachineOperand &MO = MI->getOperand(i);
274 if (!MO.isReg()) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000275 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
David Goodwin34877712009-10-26 19:32:42 +0000276 IsImplicitDefUse(MI, MO)) {
277 const unsigned Reg = MO.getReg();
278 PassthruRegs.insert(Reg);
279 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
280 *Subreg; ++Subreg) {
281 PassthruRegs.insert(*Subreg);
282 }
283 }
284 }
285}
286
David Goodwin557bbe62009-11-20 19:32:48 +0000287/// AntiDepEdges - Return in Edges the anti- and output- dependencies
288/// in SU that we want to consider for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000289static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin557bbe62009-11-20 19:32:48 +0000290 SmallSet<unsigned, 4> RegSet;
Dan Gohman66db3a02010-04-19 23:11:58 +0000291 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin34877712009-10-26 19:32:42 +0000292 P != PE; ++P) {
David Goodwin12dd99d2009-11-12 19:08:21 +0000293 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Goodwin34877712009-10-26 19:32:42 +0000294 unsigned Reg = P->getReg();
David Goodwin557bbe62009-11-20 19:32:48 +0000295 if (RegSet.count(Reg) == 0) {
David Goodwin34877712009-10-26 19:32:42 +0000296 Edges.push_back(&*P);
David Goodwin557bbe62009-11-20 19:32:48 +0000297 RegSet.insert(Reg);
David Goodwin34877712009-10-26 19:32:42 +0000298 }
299 }
300 }
301}
302
David Goodwin87d21b92009-11-13 19:52:48 +0000303/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
304/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000305static const SUnit *CriticalPathStep(const SUnit *SU) {
306 const SDep *Next = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000307 unsigned NextDepth = 0;
308 // Find the predecessor edge with the greatest depth.
309 if (SU != 0) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000310 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin87d21b92009-11-13 19:52:48 +0000311 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000312 const SUnit *PredSU = P->getSUnit();
David Goodwin87d21b92009-11-13 19:52:48 +0000313 unsigned PredLatency = P->getLatency();
314 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
315 // In the case of a latency tie, prefer an anti-dependency edge over
316 // other types of edges.
317 if (NextDepth < PredTotalLatency ||
318 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
319 NextDepth = PredTotalLatency;
320 Next = &*P;
321 }
322 }
323 }
324
325 return (Next) ? Next->getSUnit() : 0;
326}
327
David Goodwin67a8a7b2009-10-29 19:17:04 +0000328void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbach2973b572010-01-06 16:48:02 +0000329 const char *tag,
330 const char *header,
David Goodwin3e72d302009-11-19 23:12:37 +0000331 const char *footer) {
Bill Wendlinge0104092010-07-15 06:04:38 +0000332 std::vector<unsigned> &KillIndices = State->GetKillIndices();
333 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000334 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin67a8a7b2009-10-29 19:17:04 +0000335 RegRefs = State->GetRegRefs();
336
337 if (!State->IsLive(Reg)) {
338 KillIndices[Reg] = KillIdx;
339 DefIndices[Reg] = ~0u;
340 RegRefs.erase(Reg);
341 State->LeaveGroup(Reg);
David Goodwin3e72d302009-11-19 23:12:37 +0000342 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000343 dbgs() << header << TRI->getName(Reg); header = NULL; });
344 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000345 }
346 // Repeat for subregisters.
347 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
348 *Subreg; ++Subreg) {
349 unsigned SubregReg = *Subreg;
350 if (!State->IsLive(SubregReg)) {
351 KillIndices[SubregReg] = KillIdx;
352 DefIndices[SubregReg] = ~0u;
353 RegRefs.erase(SubregReg);
354 State->LeaveGroup(SubregReg);
David Goodwin3e72d302009-11-19 23:12:37 +0000355 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000356 dbgs() << header << TRI->getName(Reg); header = NULL; });
357 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin67a8a7b2009-10-29 19:17:04 +0000358 State->GetGroup(SubregReg) << tag);
359 }
360 }
David Goodwin3e72d302009-11-19 23:12:37 +0000361
David Greene5393b252009-12-24 00:14:25 +0000362 DEBUG(if ((header == NULL) && (footer != NULL)) dbgs() << footer);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000363}
364
Jim Grosbach2973b572010-01-06 16:48:02 +0000365void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
366 unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000367 std::set<unsigned>& PassthruRegs) {
Bill Wendlinge0104092010-07-15 06:04:38 +0000368 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000369 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000370 RegRefs = State->GetRegRefs();
371
David Goodwin67a8a7b2009-10-29 19:17:04 +0000372 // Handle dead defs by simulating a last-use of the register just
373 // after the def. A dead def can occur because the def is truely
374 // dead, or because only a subregister is live at the def. If we
375 // don't do this the dead def will be incorrectly merged into the
376 // previous def.
David Goodwin34877712009-10-26 19:32:42 +0000377 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
378 MachineOperand &MO = MI->getOperand(i);
379 if (!MO.isReg() || !MO.isDef()) continue;
380 unsigned Reg = MO.getReg();
381 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000382
David Goodwin3e72d302009-11-19 23:12:37 +0000383 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwin34877712009-10-26 19:32:42 +0000384 }
385
David Greene5393b252009-12-24 00:14:25 +0000386 DEBUG(dbgs() << "\tDef Groups:");
David Goodwin34877712009-10-26 19:32:42 +0000387 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
388 MachineOperand &MO = MI->getOperand(i);
389 if (!MO.isReg() || !MO.isDef()) continue;
390 unsigned Reg = MO.getReg();
391 if (Reg == 0) continue;
392
Jim Grosbach2973b572010-01-06 16:48:02 +0000393 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000394
David Goodwin67a8a7b2009-10-29 19:17:04 +0000395 // If MI's defs have a special allocation requirement, don't allow
David Goodwin34877712009-10-26 19:32:42 +0000396 // any def registers to be changed. Also assume all registers
397 // defined in a call must not be changed (ABI).
Evan Cheng46df4eb2010-06-16 07:35:02 +0000398 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq() ||
399 TII->isPredicated(MI)) {
David Greene5393b252009-12-24 00:14:25 +0000400 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000401 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000402 }
403
404 // Any aliased that are live at this point are completely or
David Goodwin67a8a7b2009-10-29 19:17:04 +0000405 // partially defined here, so group those aliases with Reg.
David Goodwin34877712009-10-26 19:32:42 +0000406 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
407 unsigned AliasReg = *Alias;
David Goodwine10deca2009-10-26 22:31:16 +0000408 if (State->IsLive(AliasReg)) {
409 State->UnionGroups(Reg, AliasReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000410 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwin34877712009-10-26 19:32:42 +0000411 TRI->getName(AliasReg) << ")");
412 }
413 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000414
David Goodwin34877712009-10-26 19:32:42 +0000415 // Note register reference...
416 const TargetRegisterClass *RC = NULL;
417 if (i < MI->getDesc().getNumOperands())
418 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000419 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000420 RegRefs.insert(std::make_pair(Reg, RR));
421 }
422
David Greene5393b252009-12-24 00:14:25 +0000423 DEBUG(dbgs() << '\n');
David Goodwin67a8a7b2009-10-29 19:17:04 +0000424
425 // Scan the register defs for this instruction and update
426 // live-ranges.
427 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
428 MachineOperand &MO = MI->getOperand(i);
429 if (!MO.isReg() || !MO.isDef()) continue;
430 unsigned Reg = MO.getReg();
431 if (Reg == 0) continue;
David Goodwin3e72d302009-11-19 23:12:37 +0000432 // Ignore KILLs and passthru registers for liveness...
Chris Lattner518bb532010-02-09 19:54:29 +0000433 if (MI->isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwin3e72d302009-11-19 23:12:37 +0000434 continue;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000435
David Goodwin3e72d302009-11-19 23:12:37 +0000436 // Update def for Reg and aliases.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000437 DefIndices[Reg] = Count;
David Goodwin3e72d302009-11-19 23:12:37 +0000438 for (const unsigned *Alias = TRI->getAliasSet(Reg);
439 *Alias; ++Alias) {
440 unsigned AliasReg = *Alias;
441 DefIndices[AliasReg] = Count;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000442 }
443 }
David Goodwin34877712009-10-26 19:32:42 +0000444}
445
446void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000447 unsigned Count) {
David Greene5393b252009-12-24 00:14:25 +0000448 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000449 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000450 RegRefs = State->GetRegRefs();
David Goodwin34877712009-10-26 19:32:42 +0000451
Evan Cheng46df4eb2010-06-16 07:35:02 +0000452 // If MI's uses have special allocation requirement, don't allow
453 // any use registers to be changed. Also assume all registers
454 // used in a call must not be changed (ABI).
455 // FIXME: The issue with predicated instruction is more complex. We are being
456 // conservatively here because the kill markers cannot be trusted after
457 // if-conversion:
458 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
459 // ...
460 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
461 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
462 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
463 //
464 // The first R6 kill is not really a kill since it's killed by a predicated
465 // instruction which may not be executed. The second R6 def may or may not
466 // re-define R6 so it's not safe to change it since the last R6 use cannot be
467 // changed.
468 bool Special = MI->getDesc().isCall() ||
469 MI->getDesc().hasExtraSrcRegAllocReq() ||
470 TII->isPredicated(MI);
471
David Goodwin34877712009-10-26 19:32:42 +0000472 // Scan the register uses for this instruction and update
473 // live-ranges, groups and RegRefs.
474 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
475 MachineOperand &MO = MI->getOperand(i);
476 if (!MO.isReg() || !MO.isUse()) continue;
477 unsigned Reg = MO.getReg();
478 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000479
480 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
481 State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000482
483 // It wasn't previously live but now it is, this is a kill. Forget
484 // the previous live-range information and start a new live-range
485 // for the register.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000486 HandleLastUse(Reg, Count, "(last-use)");
David Goodwin34877712009-10-26 19:32:42 +0000487
Evan Cheng46df4eb2010-06-16 07:35:02 +0000488 if (Special) {
David Greene5393b252009-12-24 00:14:25 +0000489 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000490 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000491 }
492
493 // Note register reference...
494 const TargetRegisterClass *RC = NULL;
495 if (i < MI->getDesc().getNumOperands())
496 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000497 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000498 RegRefs.insert(std::make_pair(Reg, RR));
499 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000500
David Greene5393b252009-12-24 00:14:25 +0000501 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000502
503 // Form a group of all defs and uses of a KILL instruction to ensure
504 // that all registers are renamed as a group.
Chris Lattner518bb532010-02-09 19:54:29 +0000505 if (MI->isKill()) {
David Greene5393b252009-12-24 00:14:25 +0000506 DEBUG(dbgs() << "\tKill Group:");
David Goodwin34877712009-10-26 19:32:42 +0000507
508 unsigned FirstReg = 0;
509 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
510 MachineOperand &MO = MI->getOperand(i);
511 if (!MO.isReg()) continue;
512 unsigned Reg = MO.getReg();
513 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000514
David Goodwin34877712009-10-26 19:32:42 +0000515 if (FirstReg != 0) {
David Greene5393b252009-12-24 00:14:25 +0000516 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine10deca2009-10-26 22:31:16 +0000517 State->UnionGroups(FirstReg, Reg);
David Goodwin34877712009-10-26 19:32:42 +0000518 } else {
David Greene5393b252009-12-24 00:14:25 +0000519 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000520 FirstReg = Reg;
521 }
522 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000523
David Greene5393b252009-12-24 00:14:25 +0000524 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000525 }
526}
527
528BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
529 BitVector BV(TRI->getNumRegs(), false);
530 bool first = true;
531
532 // Check all references that need rewriting for Reg. For each, use
533 // the corresponding register class to narrow the set of registers
534 // that are appropriate for renaming.
Jim Grosbach2973b572010-01-06 16:48:02 +0000535 std::pair<std::multimap<unsigned,
David Goodwine10deca2009-10-26 22:31:16 +0000536 AggressiveAntiDepState::RegisterReference>::iterator,
537 std::multimap<unsigned,
538 AggressiveAntiDepState::RegisterReference>::iterator>
539 Range = State->GetRegRefs().equal_range(Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000540 for (std::multimap<unsigned,
541 AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first,
542 QE = Range.second; Q != QE; ++Q) {
David Goodwin34877712009-10-26 19:32:42 +0000543 const TargetRegisterClass *RC = Q->second.RC;
544 if (RC == NULL) continue;
545
546 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
547 if (first) {
548 BV |= RCBV;
549 first = false;
550 } else {
551 BV &= RCBV;
552 }
553
David Greene5393b252009-12-24 00:14:25 +0000554 DEBUG(dbgs() << " " << RC->getName());
David Goodwin34877712009-10-26 19:32:42 +0000555 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000556
David Goodwin34877712009-10-26 19:32:42 +0000557 return BV;
Jim Grosbach2973b572010-01-06 16:48:02 +0000558}
David Goodwin34877712009-10-26 19:32:42 +0000559
560bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin54097832009-11-05 01:19:35 +0000561 unsigned AntiDepGroupIndex,
562 RenameOrderType& RenameOrder,
563 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendlinge0104092010-07-15 06:04:38 +0000564 std::vector<unsigned> &KillIndices = State->GetKillIndices();
565 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000566 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000567 RegRefs = State->GetRegRefs();
568
David Goodwin87d21b92009-11-13 19:52:48 +0000569 // Collect all referenced registers in the same group as
570 // AntiDepReg. These all need to be renamed together if we are to
571 // break the anti-dependence.
David Goodwin34877712009-10-26 19:32:42 +0000572 std::vector<unsigned> Regs;
David Goodwin87d21b92009-11-13 19:52:48 +0000573 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwin34877712009-10-26 19:32:42 +0000574 assert(Regs.size() > 0 && "Empty register group!");
575 if (Regs.size() == 0)
576 return false;
577
578 // Find the "superest" register in the group. At the same time,
579 // collect the BitVector of registers that can be used to rename
580 // each register.
Jim Grosbach2973b572010-01-06 16:48:02 +0000581 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
582 << ":\n");
David Goodwin34877712009-10-26 19:32:42 +0000583 std::map<unsigned, BitVector> RenameRegisterMap;
584 unsigned SuperReg = 0;
585 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
586 unsigned Reg = Regs[i];
587 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
588 SuperReg = Reg;
589
590 // If Reg has any references, then collect possible rename regs
591 if (RegRefs.count(Reg) > 0) {
David Greene5393b252009-12-24 00:14:25 +0000592 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000593
David Goodwin34877712009-10-26 19:32:42 +0000594 BitVector BV = GetRenameRegisters(Reg);
595 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
596
David Greene5393b252009-12-24 00:14:25 +0000597 DEBUG(dbgs() << " ::");
David Goodwin34877712009-10-26 19:32:42 +0000598 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000599 dbgs() << " " << TRI->getName(r));
600 DEBUG(dbgs() << "\n");
David Goodwin34877712009-10-26 19:32:42 +0000601 }
602 }
603
604 // All group registers should be a subreg of SuperReg.
605 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
606 unsigned Reg = Regs[i];
607 if (Reg == SuperReg) continue;
608 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
609 assert(IsSub && "Expecting group subregister");
610 if (!IsSub)
611 return false;
612 }
613
David Goodwin00621ef2009-11-20 23:33:54 +0000614#ifndef NDEBUG
615 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
616 if (DebugDiv > 0) {
617 static int renamecnt = 0;
618 if (renamecnt++ % DebugDiv != DebugMod)
619 return false;
Jim Grosbach2973b572010-01-06 16:48:02 +0000620
David Greene5393b252009-12-24 00:14:25 +0000621 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin00621ef2009-11-20 23:33:54 +0000622 " for debug ***\n";
623 }
624#endif
625
David Goodwin54097832009-11-05 01:19:35 +0000626 // Check each possible rename register for SuperReg in round-robin
627 // order. If that register is available, and the corresponding
628 // registers are available for the other group subregisters, then we
629 // can use those registers to rename.
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000630
631 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
632 // check every use of the register and find the largest register class
633 // that can be used in all of them.
Jim Grosbach2973b572010-01-06 16:48:02 +0000634 const TargetRegisterClass *SuperRC =
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000635 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbach2973b572010-01-06 16:48:02 +0000636
David Goodwin54097832009-11-05 01:19:35 +0000637 const TargetRegisterClass::iterator RB = SuperRC->allocation_order_begin(MF);
638 const TargetRegisterClass::iterator RE = SuperRC->allocation_order_end(MF);
639 if (RB == RE) {
David Greene5393b252009-12-24 00:14:25 +0000640 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin54097832009-11-05 01:19:35 +0000641 return false;
642 }
643
David Greene5393b252009-12-24 00:14:25 +0000644 DEBUG(dbgs() << "\tFind Registers:");
David Goodwin3e72d302009-11-19 23:12:37 +0000645
David Goodwin54097832009-11-05 01:19:35 +0000646 if (RenameOrder.count(SuperRC) == 0)
647 RenameOrder.insert(RenameOrderType::value_type(SuperRC, RE));
648
David Goodwin98f2f1a2009-11-05 01:45:50 +0000649 const TargetRegisterClass::iterator OrigR = RenameOrder[SuperRC];
David Goodwin54097832009-11-05 01:19:35 +0000650 const TargetRegisterClass::iterator EndR = ((OrigR == RE) ? RB : OrigR);
651 TargetRegisterClass::iterator R = OrigR;
652 do {
653 if (R == RB) R = RE;
654 --R;
David Goodwin00621ef2009-11-20 23:33:54 +0000655 const unsigned NewSuperReg = *R;
David Goodwin34877712009-10-26 19:32:42 +0000656 // Don't replace a register with itself.
David Goodwin00621ef2009-11-20 23:33:54 +0000657 if (NewSuperReg == SuperReg) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000658
David Greene5393b252009-12-24 00:14:25 +0000659 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin00621ef2009-11-20 23:33:54 +0000660 RenameMap.clear();
661
662 // For each referenced group register (which must be a SuperReg or
663 // a subregister of SuperReg), find the corresponding subregister
664 // of NewSuperReg and make sure it is free to be renamed.
665 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
666 unsigned Reg = Regs[i];
667 unsigned NewReg = 0;
668 if (Reg == SuperReg) {
669 NewReg = NewSuperReg;
670 } else {
671 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
672 if (NewSubRegIdx != 0)
673 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwin34877712009-10-26 19:32:42 +0000674 }
David Goodwin00621ef2009-11-20 23:33:54 +0000675
David Greene5393b252009-12-24 00:14:25 +0000676 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbach2973b572010-01-06 16:48:02 +0000677
David Goodwin00621ef2009-11-20 23:33:54 +0000678 // Check if Reg can be renamed to NewReg.
679 BitVector BV = RenameRegisterMap[Reg];
680 if (!BV.test(NewReg)) {
David Greene5393b252009-12-24 00:14:25 +0000681 DEBUG(dbgs() << "(no rename)");
David Goodwin00621ef2009-11-20 23:33:54 +0000682 goto next_super_reg;
683 }
684
685 // If NewReg is dead and NewReg's most recent def is not before
686 // Regs's kill, it's safe to replace Reg with NewReg. We
687 // must also check all aliases of NewReg, because we can't define a
688 // register when any sub or super is already live.
689 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene5393b252009-12-24 00:14:25 +0000690 DEBUG(dbgs() << "(live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000691 goto next_super_reg;
692 } else {
693 bool found = false;
694 for (const unsigned *Alias = TRI->getAliasSet(NewReg);
695 *Alias; ++Alias) {
696 unsigned AliasReg = *Alias;
Jim Grosbach2973b572010-01-06 16:48:02 +0000697 if (State->IsLive(AliasReg) ||
698 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene5393b252009-12-24 00:14:25 +0000699 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000700 found = true;
701 break;
702 }
703 }
704 if (found)
705 goto next_super_reg;
706 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000707
David Goodwin00621ef2009-11-20 23:33:54 +0000708 // Record that 'Reg' can be renamed to 'NewReg'.
709 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwin34877712009-10-26 19:32:42 +0000710 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000711
David Goodwin00621ef2009-11-20 23:33:54 +0000712 // If we fall-out here, then every register in the group can be
713 // renamed, as recorded in RenameMap.
714 RenameOrder.erase(SuperRC);
715 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene5393b252009-12-24 00:14:25 +0000716 DEBUG(dbgs() << "]\n");
David Goodwin00621ef2009-11-20 23:33:54 +0000717 return true;
718
719 next_super_reg:
David Greene5393b252009-12-24 00:14:25 +0000720 DEBUG(dbgs() << ']');
David Goodwin54097832009-11-05 01:19:35 +0000721 } while (R != EndR);
David Goodwin34877712009-10-26 19:32:42 +0000722
David Greene5393b252009-12-24 00:14:25 +0000723 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000724
725 // No registers are free and available!
726 return false;
727}
728
729/// BreakAntiDependencies - Identifiy anti-dependencies within the
730/// ScheduleDAG and break them by renaming registers.
731///
David Goodwine10deca2009-10-26 22:31:16 +0000732unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman66db3a02010-04-19 23:11:58 +0000733 const std::vector<SUnit>& SUnits,
734 MachineBasicBlock::iterator Begin,
735 MachineBasicBlock::iterator End,
David Goodwine10deca2009-10-26 22:31:16 +0000736 unsigned InsertPosIndex) {
Bill Wendlinge0104092010-07-15 06:04:38 +0000737 std::vector<unsigned> &KillIndices = State->GetKillIndices();
738 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000739 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000740 RegRefs = State->GetRegRefs();
741
David Goodwin34877712009-10-26 19:32:42 +0000742 // The code below assumes that there is at least one instruction,
743 // so just duck out immediately if the block is empty.
David Goodwin4de099d2009-11-03 20:57:50 +0000744 if (SUnits.empty()) return 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000745
David Goodwin54097832009-11-05 01:19:35 +0000746 // For each regclass the next register to use for renaming.
747 RenameOrderType RenameOrder;
David Goodwin34877712009-10-26 19:32:42 +0000748
749 // ...need a map from MI to SUnit.
Dan Gohman66db3a02010-04-19 23:11:58 +0000750 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwin34877712009-10-26 19:32:42 +0000751 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000752 const SUnit *SU = &SUnits[i];
753 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
754 SU));
David Goodwin34877712009-10-26 19:32:42 +0000755 }
756
David Goodwin87d21b92009-11-13 19:52:48 +0000757 // Track progress along the critical path through the SUnit graph as
758 // we walk the instructions. This is needed for regclasses that only
759 // break critical-path anti-dependencies.
Dan Gohman66db3a02010-04-19 23:11:58 +0000760 const SUnit *CriticalPathSU = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000761 MachineInstr *CriticalPathMI = 0;
762 if (CriticalPathSet.any()) {
763 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000764 const SUnit *SU = &SUnits[i];
Jim Grosbach2973b572010-01-06 16:48:02 +0000765 if (!CriticalPathSU ||
766 ((SU->getDepth() + SU->Latency) >
David Goodwin87d21b92009-11-13 19:52:48 +0000767 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
768 CriticalPathSU = SU;
769 }
770 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000771
David Goodwin87d21b92009-11-13 19:52:48 +0000772 CriticalPathMI = CriticalPathSU->getInstr();
773 }
774
Jim Grosbach2973b572010-01-06 16:48:02 +0000775#ifndef NDEBUG
David Greene5393b252009-12-24 00:14:25 +0000776 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
777 DEBUG(dbgs() << "Available regs:");
David Goodwin557bbe62009-11-20 19:32:48 +0000778 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
779 if (!State->IsLive(Reg))
David Greene5393b252009-12-24 00:14:25 +0000780 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000781 }
David Greene5393b252009-12-24 00:14:25 +0000782 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000783#endif
784
785 // Attempt to break anti-dependence edges. Walk the instructions
786 // from the bottom up, tracking information about liveness as we go
787 // to help determine which registers are available.
788 unsigned Broken = 0;
789 unsigned Count = InsertPosIndex - 1;
790 for (MachineBasicBlock::iterator I = End, E = Begin;
791 I != E; --Count) {
792 MachineInstr *MI = --I;
793
David Greene5393b252009-12-24 00:14:25 +0000794 DEBUG(dbgs() << "Anti: ");
David Goodwin34877712009-10-26 19:32:42 +0000795 DEBUG(MI->dump());
796
797 std::set<unsigned> PassthruRegs;
798 GetPassthruRegs(MI, PassthruRegs);
799
800 // Process the defs in MI...
801 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbach2973b572010-01-06 16:48:02 +0000802
David Goodwin557bbe62009-11-20 19:32:48 +0000803 // The dependence edges that represent anti- and output-
David Goodwin87d21b92009-11-13 19:52:48 +0000804 // dependencies that are candidates for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000805 std::vector<const SDep *> Edges;
806 const SUnit *PathSU = MISUnitMap[MI];
David Goodwin557bbe62009-11-20 19:32:48 +0000807 AntiDepEdges(PathSU, Edges);
David Goodwin87d21b92009-11-13 19:52:48 +0000808
809 // If MI is not on the critical path, then we don't rename
810 // registers in the CriticalPathSet.
811 BitVector *ExcludeRegs = NULL;
812 if (MI == CriticalPathMI) {
813 CriticalPathSU = CriticalPathStep(CriticalPathSU);
814 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000815 } else {
David Goodwin87d21b92009-11-13 19:52:48 +0000816 ExcludeRegs = &CriticalPathSet;
817 }
818
David Goodwin34877712009-10-26 19:32:42 +0000819 // Ignore KILL instructions (they form a group in ScanInstruction
820 // but don't cause any anti-dependence breaking themselves)
Chris Lattner518bb532010-02-09 19:54:29 +0000821 if (!MI->isKill()) {
David Goodwin34877712009-10-26 19:32:42 +0000822 // Attempt to break each anti-dependency...
823 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000824 const SDep *Edge = Edges[i];
David Goodwin34877712009-10-26 19:32:42 +0000825 SUnit *NextSU = Edge->getSUnit();
Jim Grosbach2973b572010-01-06 16:48:02 +0000826
David Goodwin12dd99d2009-11-12 19:08:21 +0000827 if ((Edge->getKind() != SDep::Anti) &&
828 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000829
David Goodwin34877712009-10-26 19:32:42 +0000830 unsigned AntiDepReg = Edge->getReg();
David Greene5393b252009-12-24 00:14:25 +0000831 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwin34877712009-10-26 19:32:42 +0000832 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbach2973b572010-01-06 16:48:02 +0000833
David Goodwin34877712009-10-26 19:32:42 +0000834 if (!AllocatableSet.test(AntiDepReg)) {
835 // Don't break anti-dependencies on non-allocatable registers.
David Greene5393b252009-12-24 00:14:25 +0000836 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwin34877712009-10-26 19:32:42 +0000837 continue;
David Goodwin87d21b92009-11-13 19:52:48 +0000838 } else if ((ExcludeRegs != NULL) && ExcludeRegs->test(AntiDepReg)) {
839 // Don't break anti-dependencies for critical path registers
840 // if not on the critical path
David Greene5393b252009-12-24 00:14:25 +0000841 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwin87d21b92009-11-13 19:52:48 +0000842 continue;
David Goodwin34877712009-10-26 19:32:42 +0000843 } else if (PassthruRegs.count(AntiDepReg) != 0) {
844 // If the anti-dep register liveness "passes-thru", then
845 // don't try to change it. It will be changed along with
846 // the use if required to break an earlier antidep.
David Greene5393b252009-12-24 00:14:25 +0000847 DEBUG(dbgs() << " (passthru)\n");
David Goodwin34877712009-10-26 19:32:42 +0000848 continue;
849 } else {
850 // No anti-dep breaking for implicit deps
851 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000852 assert(AntiDepOp != NULL &&
853 "Can't find index for defined register operand");
David Goodwin34877712009-10-26 19:32:42 +0000854 if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) {
David Greene5393b252009-12-24 00:14:25 +0000855 DEBUG(dbgs() << " (implicit)\n");
David Goodwin34877712009-10-26 19:32:42 +0000856 continue;
857 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000858
David Goodwin34877712009-10-26 19:32:42 +0000859 // If the SUnit has other dependencies on the SUnit that
860 // it anti-depends on, don't bother breaking the
861 // anti-dependency since those edges would prevent such
862 // units from being scheduled past each other
863 // regardless.
David Goodwin557bbe62009-11-20 19:32:48 +0000864 //
865 // Also, if there are dependencies on other SUnits with the
866 // same register as the anti-dependency, don't attempt to
867 // break it.
Dan Gohman66db3a02010-04-19 23:11:58 +0000868 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin34877712009-10-26 19:32:42 +0000869 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin557bbe62009-11-20 19:32:48 +0000870 if (P->getSUnit() == NextSU ?
871 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
872 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
873 AntiDepReg = 0;
874 break;
875 }
876 }
Dan Gohman66db3a02010-04-19 23:11:58 +0000877 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin557bbe62009-11-20 19:32:48 +0000878 PE = PathSU->Preds.end(); P != PE; ++P) {
879 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
880 (P->getKind() != SDep::Output)) {
David Greene5393b252009-12-24 00:14:25 +0000881 DEBUG(dbgs() << " (real dependency)\n");
David Goodwin34877712009-10-26 19:32:42 +0000882 AntiDepReg = 0;
883 break;
Jim Grosbach2973b572010-01-06 16:48:02 +0000884 } else if ((P->getSUnit() != NextSU) &&
885 (P->getKind() == SDep::Data) &&
David Goodwin557bbe62009-11-20 19:32:48 +0000886 (P->getReg() == AntiDepReg)) {
David Greene5393b252009-12-24 00:14:25 +0000887 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin557bbe62009-11-20 19:32:48 +0000888 AntiDepReg = 0;
889 break;
David Goodwin34877712009-10-26 19:32:42 +0000890 }
891 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000892
David Goodwin34877712009-10-26 19:32:42 +0000893 if (AntiDepReg == 0) continue;
894 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000895
David Goodwin34877712009-10-26 19:32:42 +0000896 assert(AntiDepReg != 0);
897 if (AntiDepReg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000898
David Goodwin34877712009-10-26 19:32:42 +0000899 // Determine AntiDepReg's register group.
David Goodwine10deca2009-10-26 22:31:16 +0000900 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwin34877712009-10-26 19:32:42 +0000901 if (GroupIndex == 0) {
David Greene5393b252009-12-24 00:14:25 +0000902 DEBUG(dbgs() << " (zero group)\n");
David Goodwin34877712009-10-26 19:32:42 +0000903 continue;
904 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000905
David Greene5393b252009-12-24 00:14:25 +0000906 DEBUG(dbgs() << '\n');
Jim Grosbach2973b572010-01-06 16:48:02 +0000907
David Goodwin34877712009-10-26 19:32:42 +0000908 // Look for a suitable register to use to break the anti-dependence.
909 std::map<unsigned, unsigned> RenameMap;
David Goodwin54097832009-11-05 01:19:35 +0000910 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene5393b252009-12-24 00:14:25 +0000911 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwin34877712009-10-26 19:32:42 +0000912 << TRI->getName(AntiDepReg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000913
David Goodwin34877712009-10-26 19:32:42 +0000914 // Handle each group register...
915 for (std::map<unsigned, unsigned>::iterator
916 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
917 unsigned CurrReg = S->first;
918 unsigned NewReg = S->second;
Jim Grosbach2973b572010-01-06 16:48:02 +0000919
920 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
921 TRI->getName(NewReg) << "(" <<
David Goodwin34877712009-10-26 19:32:42 +0000922 RegRefs.count(CurrReg) << " refs)");
Jim Grosbach2973b572010-01-06 16:48:02 +0000923
David Goodwin34877712009-10-26 19:32:42 +0000924 // Update the references to the old register CurrReg to
925 // refer to the new register NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000926 std::pair<std::multimap<unsigned,
927 AggressiveAntiDepState::RegisterReference>::iterator,
David Goodwine10deca2009-10-26 22:31:16 +0000928 std::multimap<unsigned,
Jim Grosbach2973b572010-01-06 16:48:02 +0000929 AggressiveAntiDepState::RegisterReference>::iterator>
David Goodwin34877712009-10-26 19:32:42 +0000930 Range = RegRefs.equal_range(CurrReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000931 for (std::multimap<unsigned,
932 AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000933 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
934 Q->second.Operand->setReg(NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000935 // If the SU for the instruction being updated has debug
936 // information related to the anti-dependency register, make
937 // sure to update that as well.
938 const SUnit *SU = MISUnitMap[Q->second.Operand->getParent()];
Jim Grosbach086723d2010-06-02 15:29:36 +0000939 if (!SU) continue;
Jim Grosbach533934e2010-06-01 23:48:44 +0000940 for (unsigned i = 0, e = SU->DbgInstrList.size() ; i < e ; ++i) {
941 MachineInstr *DI = SU->DbgInstrList[i];
942 assert (DI->getNumOperands()==3 && DI->getOperand(0).isReg() &&
943 DI->getOperand(0).getReg()
944 && "Non register dbg_value attached to SUnit!");
945 if (DI->getOperand(0).getReg() == AntiDepReg)
946 DI->getOperand(0).setReg(NewReg);
947 }
David Goodwin34877712009-10-26 19:32:42 +0000948 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000949
David Goodwin34877712009-10-26 19:32:42 +0000950 // We just went back in time and modified history; the
951 // liveness information for CurrReg is now inconsistent. Set
952 // the state as if it were dead.
David Goodwine10deca2009-10-26 22:31:16 +0000953 State->UnionGroups(NewReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000954 RegRefs.erase(NewReg);
955 DefIndices[NewReg] = DefIndices[CurrReg];
956 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbach2973b572010-01-06 16:48:02 +0000957
David Goodwine10deca2009-10-26 22:31:16 +0000958 State->UnionGroups(CurrReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000959 RegRefs.erase(CurrReg);
960 DefIndices[CurrReg] = KillIndices[CurrReg];
961 KillIndices[CurrReg] = ~0u;
962 assert(((KillIndices[CurrReg] == ~0u) !=
963 (DefIndices[CurrReg] == ~0u)) &&
964 "Kill and Def maps aren't consistent for AntiDepReg!");
965 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000966
David Goodwin34877712009-10-26 19:32:42 +0000967 ++Broken;
David Greene5393b252009-12-24 00:14:25 +0000968 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000969 }
970 }
971 }
972
973 ScanInstruction(MI, Count);
974 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000975
David Goodwin34877712009-10-26 19:32:42 +0000976 return Broken;
977}