blob: a7189acc3fecd37adb9011ab791d94ea2283a87e [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 Goodwin34877712009-10-26 19:32:42 +000025#include "llvm/Target/TargetRegisterInfo.h"
David Goodwine10deca2009-10-26 22:31:16 +000026#include "llvm/Support/CommandLine.h"
David Goodwin34877712009-10-26 19:32:42 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
David Goodwin34877712009-10-26 19:32:42 +000030using namespace llvm;
31
David Goodwin3e72d302009-11-19 23:12:37 +000032// If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod
33static cl::opt<int>
34DebugDiv("agg-antidep-debugdiv",
Bob Wilson347fa3f2010-04-09 21:38:26 +000035 cl::desc("Debug control for aggressive anti-dep breaker"),
36 cl::init(0), cl::Hidden);
David Goodwin3e72d302009-11-19 23:12:37 +000037static cl::opt<int>
38DebugMod("agg-antidep-debugmod",
Bob Wilson347fa3f2010-04-09 21:38:26 +000039 cl::desc("Debug control for aggressive anti-dep breaker"),
40 cl::init(0), cl::Hidden);
David Goodwin3e72d302009-11-19 23:12:37 +000041
David Goodwin990d2852009-12-09 17:18:22 +000042AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
43 MachineBasicBlock *BB) :
44 NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0) {
David Goodwin34877712009-10-26 19:32:42 +000045
David Goodwin990d2852009-12-09 17:18:22 +000046 const unsigned BBSize = BB->size();
47 for (unsigned i = 0; i < NumTargetRegs; ++i) {
48 // Initialize all registers to be in their own group. Initially we
49 // assign the register to the same-indexed GroupNode.
50 GroupNodeIndices[i] = i;
51 // Initialize the indices to indicate that no registers are live.
52 KillIndices[i] = ~0u;
53 DefIndices[i] = BBSize;
54 }
David Goodwin34877712009-10-26 19:32:42 +000055}
56
David Goodwine10deca2009-10-26 22:31:16 +000057unsigned AggressiveAntiDepState::GetGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000058{
59 unsigned Node = GroupNodeIndices[Reg];
60 while (GroupNodes[Node] != Node)
61 Node = GroupNodes[Node];
62
63 return Node;
64}
65
David Goodwin87d21b92009-11-13 19:52:48 +000066void AggressiveAntiDepState::GetGroupRegs(
67 unsigned Group,
68 std::vector<unsigned> &Regs,
69 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
David Goodwin34877712009-10-26 19:32:42 +000070{
David Goodwin990d2852009-12-09 17:18:22 +000071 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
David Goodwin87d21b92009-11-13 19:52:48 +000072 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
David Goodwin34877712009-10-26 19:32:42 +000073 Regs.push_back(Reg);
74 }
75}
76
David Goodwine10deca2009-10-26 22:31:16 +000077unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2)
David Goodwin34877712009-10-26 19:32:42 +000078{
79 assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
80 assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
Jim Grosbach2973b572010-01-06 16:48:02 +000081
David Goodwin34877712009-10-26 19:32:42 +000082 // find group for each register
83 unsigned Group1 = GetGroup(Reg1);
84 unsigned Group2 = GetGroup(Reg2);
Jim Grosbach2973b572010-01-06 16:48:02 +000085
David Goodwin34877712009-10-26 19:32:42 +000086 // if either group is 0, then that must become the parent
87 unsigned Parent = (Group1 == 0) ? Group1 : Group2;
88 unsigned Other = (Parent == Group1) ? Group2 : Group1;
89 GroupNodes.at(Other) = Parent;
90 return Parent;
91}
Jim Grosbach2973b572010-01-06 16:48:02 +000092
David Goodwine10deca2009-10-26 22:31:16 +000093unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000094{
95 // Create a new GroupNode for Reg. Reg's existing GroupNode must
96 // stay as is because there could be other GroupNodes referring to
97 // it.
98 unsigned idx = GroupNodes.size();
99 GroupNodes.push_back(idx);
100 GroupNodeIndices[Reg] = idx;
101 return idx;
102}
103
David Goodwine10deca2009-10-26 22:31:16 +0000104bool AggressiveAntiDepState::IsLive(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +0000105{
106 // KillIndex must be defined and DefIndex not defined for a register
107 // to be live.
108 return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
109}
110
David Goodwine10deca2009-10-26 22:31:16 +0000111
112
113AggressiveAntiDepBreaker::
David Goodwin0855dee2009-11-10 00:15:47 +0000114AggressiveAntiDepBreaker(MachineFunction& MFi,
Jim Grosbach2973b572010-01-06 16:48:02 +0000115 TargetSubtarget::RegClassVector& CriticalPathRCs) :
David Goodwine10deca2009-10-26 22:31:16 +0000116 AntiDepBreaker(), MF(MFi),
117 MRI(MF.getRegInfo()),
Evan Cheng46df4eb2010-06-16 07:35:02 +0000118 TII(MF.getTarget().getInstrInfo()),
David Goodwine10deca2009-10-26 22:31:16 +0000119 TRI(MF.getTarget().getRegisterInfo()),
120 AllocatableSet(TRI->getAllocatableSet(MF)),
David Goodwin557bbe62009-11-20 19:32:48 +0000121 State(NULL) {
David Goodwin87d21b92009-11-13 19:52:48 +0000122 /* Collect a bitset of all registers that are only broken if they
123 are on the critical path. */
124 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
125 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
126 if (CriticalPathSet.none())
127 CriticalPathSet = CPSet;
128 else
129 CriticalPathSet |= CPSet;
130 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000131
David Greene5393b252009-12-24 00:14:25 +0000132 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000133 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwin87d21b92009-11-13 19:52:48 +0000134 r = CriticalPathSet.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000135 dbgs() << " " << TRI->getName(r));
136 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000137}
138
139AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
140 delete State;
David Goodwine10deca2009-10-26 22:31:16 +0000141}
142
143void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
144 assert(State == NULL);
David Goodwin990d2852009-12-09 17:18:22 +0000145 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine10deca2009-10-26 22:31:16 +0000146
147 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
148 unsigned *KillIndices = State->GetKillIndices();
149 unsigned *DefIndices = State->GetDefIndices();
150
151 // Determine the live-out physregs for this block.
152 if (IsReturnBlock) {
153 // In a return block, examine the function live-out regs.
154 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
155 E = MRI.liveout_end(); I != E; ++I) {
156 unsigned Reg = *I;
157 State->UnionGroups(Reg, 0);
158 KillIndices[Reg] = BB->size();
159 DefIndices[Reg] = ~0u;
160 // Repeat, for all aliases.
161 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
162 unsigned AliasReg = *Alias;
163 State->UnionGroups(AliasReg, 0);
164 KillIndices[AliasReg] = BB->size();
165 DefIndices[AliasReg] = ~0u;
166 }
167 }
David Goodwine10deca2009-10-26 22:31:16 +0000168 }
169
Evan Cheng46df4eb2010-06-16 07:35:02 +0000170 // In a non-return block, examine the live-in regs of all successors.
171 // Note a return block can have successors if the return instruction is
172 // predicated.
173 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
174 SE = BB->succ_end(); SI != SE; ++SI)
175 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
176 E = (*SI)->livein_end(); I != E; ++I) {
177 unsigned Reg = *I;
178 State->UnionGroups(Reg, 0);
179 KillIndices[Reg] = BB->size();
180 DefIndices[Reg] = ~0u;
181 // Repeat, for all aliases.
182 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
183 unsigned AliasReg = *Alias;
184 State->UnionGroups(AliasReg, 0);
185 KillIndices[AliasReg] = BB->size();
186 DefIndices[AliasReg] = ~0u;
187 }
188 }
189
David Goodwine10deca2009-10-26 22:31:16 +0000190 // Mark live-out callee-saved registers. In a return block this is
191 // all callee-saved registers. In non-return this is any
192 // callee-saved register that is not saved in the prolog.
193 const MachineFrameInfo *MFI = MF.getFrameInfo();
194 BitVector Pristine = MFI->getPristineRegs(BB);
195 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
196 unsigned Reg = *I;
197 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
198 State->UnionGroups(Reg, 0);
199 KillIndices[Reg] = BB->size();
200 DefIndices[Reg] = ~0u;
201 // Repeat, for all aliases.
202 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
203 unsigned AliasReg = *Alias;
204 State->UnionGroups(AliasReg, 0);
205 KillIndices[AliasReg] = BB->size();
206 DefIndices[AliasReg] = ~0u;
207 }
208 }
209}
210
211void AggressiveAntiDepBreaker::FinishBlock() {
212 delete State;
213 State = NULL;
David Goodwine10deca2009-10-26 22:31:16 +0000214}
215
216void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000217 unsigned InsertPosIndex) {
David Goodwine10deca2009-10-26 22:31:16 +0000218 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
219
David Goodwin5b3c3082009-10-29 23:30:59 +0000220 std::set<unsigned> PassthruRegs;
221 GetPassthruRegs(MI, PassthruRegs);
222 PrescanInstruction(MI, Count, PassthruRegs);
223 ScanInstruction(MI, Count);
224
David Greene5393b252009-12-24 00:14:25 +0000225 DEBUG(dbgs() << "Observe: ");
David Goodwine10deca2009-10-26 22:31:16 +0000226 DEBUG(MI->dump());
David Greene5393b252009-12-24 00:14:25 +0000227 DEBUG(dbgs() << "\tRegs:");
David Goodwine10deca2009-10-26 22:31:16 +0000228
229 unsigned *DefIndices = State->GetDefIndices();
David Goodwin990d2852009-12-09 17:18:22 +0000230 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine10deca2009-10-26 22:31:16 +0000231 // If Reg is current live, then mark that it can't be renamed as
232 // we don't know the extent of its live-range anymore (now that it
233 // has been scheduled). If it is not live but was defined in the
234 // previous schedule region, then set its def index to the most
235 // conservative location (i.e. the beginning of the previous
236 // schedule region).
237 if (State->IsLive(Reg)) {
238 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbach2973b572010-01-06 16:48:02 +0000239 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine10deca2009-10-26 22:31:16 +0000240 State->GetGroup(Reg) << "->g0(region live-out)");
241 State->UnionGroups(Reg, 0);
Jim Grosbach2973b572010-01-06 16:48:02 +0000242 } else if ((DefIndices[Reg] < InsertPosIndex)
243 && (DefIndices[Reg] >= Count)) {
David Goodwine10deca2009-10-26 22:31:16 +0000244 DefIndices[Reg] = Count;
245 }
246 }
David Greene5393b252009-12-24 00:14:25 +0000247 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000248}
249
David Goodwin34877712009-10-26 19:32:42 +0000250bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000251 MachineOperand& MO)
David Goodwin34877712009-10-26 19:32:42 +0000252{
253 if (!MO.isReg() || !MO.isImplicit())
254 return false;
255
256 unsigned Reg = MO.getReg();
257 if (Reg == 0)
258 return false;
259
260 MachineOperand *Op = NULL;
261 if (MO.isDef())
262 Op = MI->findRegisterUseOperand(Reg, true);
263 else
264 Op = MI->findRegisterDefOperand(Reg);
265
266 return((Op != NULL) && Op->isImplicit());
267}
268
269void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
270 std::set<unsigned>& PassthruRegs) {
271 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
272 MachineOperand &MO = MI->getOperand(i);
273 if (!MO.isReg()) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000274 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
David Goodwin34877712009-10-26 19:32:42 +0000275 IsImplicitDefUse(MI, MO)) {
276 const unsigned Reg = MO.getReg();
277 PassthruRegs.insert(Reg);
278 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
279 *Subreg; ++Subreg) {
280 PassthruRegs.insert(*Subreg);
281 }
282 }
283 }
284}
285
David Goodwin557bbe62009-11-20 19:32:48 +0000286/// AntiDepEdges - Return in Edges the anti- and output- dependencies
287/// in SU that we want to consider for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000288static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin557bbe62009-11-20 19:32:48 +0000289 SmallSet<unsigned, 4> RegSet;
Dan Gohman66db3a02010-04-19 23:11:58 +0000290 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin34877712009-10-26 19:32:42 +0000291 P != PE; ++P) {
David Goodwin12dd99d2009-11-12 19:08:21 +0000292 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Goodwin34877712009-10-26 19:32:42 +0000293 unsigned Reg = P->getReg();
David Goodwin557bbe62009-11-20 19:32:48 +0000294 if (RegSet.count(Reg) == 0) {
David Goodwin34877712009-10-26 19:32:42 +0000295 Edges.push_back(&*P);
David Goodwin557bbe62009-11-20 19:32:48 +0000296 RegSet.insert(Reg);
David Goodwin34877712009-10-26 19:32:42 +0000297 }
298 }
299 }
300}
301
David Goodwin87d21b92009-11-13 19:52:48 +0000302/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
303/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000304static const SUnit *CriticalPathStep(const SUnit *SU) {
305 const SDep *Next = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000306 unsigned NextDepth = 0;
307 // Find the predecessor edge with the greatest depth.
308 if (SU != 0) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000309 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin87d21b92009-11-13 19:52:48 +0000310 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000311 const SUnit *PredSU = P->getSUnit();
David Goodwin87d21b92009-11-13 19:52:48 +0000312 unsigned PredLatency = P->getLatency();
313 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
314 // In the case of a latency tie, prefer an anti-dependency edge over
315 // other types of edges.
316 if (NextDepth < PredTotalLatency ||
317 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
318 NextDepth = PredTotalLatency;
319 Next = &*P;
320 }
321 }
322 }
323
324 return (Next) ? Next->getSUnit() : 0;
325}
326
David Goodwin67a8a7b2009-10-29 19:17:04 +0000327void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbach2973b572010-01-06 16:48:02 +0000328 const char *tag,
329 const char *header,
David Goodwin3e72d302009-11-19 23:12:37 +0000330 const char *footer) {
David Goodwin67a8a7b2009-10-29 19:17:04 +0000331 unsigned *KillIndices = State->GetKillIndices();
332 unsigned *DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000333 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin67a8a7b2009-10-29 19:17:04 +0000334 RegRefs = State->GetRegRefs();
335
336 if (!State->IsLive(Reg)) {
337 KillIndices[Reg] = KillIdx;
338 DefIndices[Reg] = ~0u;
339 RegRefs.erase(Reg);
340 State->LeaveGroup(Reg);
David Goodwin3e72d302009-11-19 23:12:37 +0000341 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000342 dbgs() << header << TRI->getName(Reg); header = NULL; });
343 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000344 }
345 // Repeat for subregisters.
346 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
347 *Subreg; ++Subreg) {
348 unsigned SubregReg = *Subreg;
349 if (!State->IsLive(SubregReg)) {
350 KillIndices[SubregReg] = KillIdx;
351 DefIndices[SubregReg] = ~0u;
352 RegRefs.erase(SubregReg);
353 State->LeaveGroup(SubregReg);
David Goodwin3e72d302009-11-19 23:12:37 +0000354 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000355 dbgs() << header << TRI->getName(Reg); header = NULL; });
356 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin67a8a7b2009-10-29 19:17:04 +0000357 State->GetGroup(SubregReg) << tag);
358 }
359 }
David Goodwin3e72d302009-11-19 23:12:37 +0000360
David Greene5393b252009-12-24 00:14:25 +0000361 DEBUG(if ((header == NULL) && (footer != NULL)) dbgs() << footer);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000362}
363
Jim Grosbach2973b572010-01-06 16:48:02 +0000364void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
365 unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000366 std::set<unsigned>& PassthruRegs) {
David Goodwine10deca2009-10-26 22:31:16 +0000367 unsigned *DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000368 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000369 RegRefs = State->GetRegRefs();
370
David Goodwin67a8a7b2009-10-29 19:17:04 +0000371 // Handle dead defs by simulating a last-use of the register just
372 // after the def. A dead def can occur because the def is truely
373 // dead, or because only a subregister is live at the def. If we
374 // don't do this the dead def will be incorrectly merged into the
375 // previous def.
David Goodwin34877712009-10-26 19:32:42 +0000376 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
377 MachineOperand &MO = MI->getOperand(i);
378 if (!MO.isReg() || !MO.isDef()) continue;
379 unsigned Reg = MO.getReg();
380 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000381
David Goodwin3e72d302009-11-19 23:12:37 +0000382 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwin34877712009-10-26 19:32:42 +0000383 }
384
David Greene5393b252009-12-24 00:14:25 +0000385 DEBUG(dbgs() << "\tDef Groups:");
David Goodwin34877712009-10-26 19:32:42 +0000386 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
387 MachineOperand &MO = MI->getOperand(i);
388 if (!MO.isReg() || !MO.isDef()) continue;
389 unsigned Reg = MO.getReg();
390 if (Reg == 0) continue;
391
Jim Grosbach2973b572010-01-06 16:48:02 +0000392 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000393
David Goodwin67a8a7b2009-10-29 19:17:04 +0000394 // If MI's defs have a special allocation requirement, don't allow
David Goodwin34877712009-10-26 19:32:42 +0000395 // any def registers to be changed. Also assume all registers
396 // defined in a call must not be changed (ABI).
Evan Cheng46df4eb2010-06-16 07:35:02 +0000397 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq() ||
398 TII->isPredicated(MI)) {
David Greene5393b252009-12-24 00:14:25 +0000399 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000400 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000401 }
402
403 // Any aliased that are live at this point are completely or
David Goodwin67a8a7b2009-10-29 19:17:04 +0000404 // partially defined here, so group those aliases with Reg.
David Goodwin34877712009-10-26 19:32:42 +0000405 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
406 unsigned AliasReg = *Alias;
David Goodwine10deca2009-10-26 22:31:16 +0000407 if (State->IsLive(AliasReg)) {
408 State->UnionGroups(Reg, AliasReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000409 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwin34877712009-10-26 19:32:42 +0000410 TRI->getName(AliasReg) << ")");
411 }
412 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000413
David Goodwin34877712009-10-26 19:32:42 +0000414 // Note register reference...
415 const TargetRegisterClass *RC = NULL;
416 if (i < MI->getDesc().getNumOperands())
417 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000418 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000419 RegRefs.insert(std::make_pair(Reg, RR));
420 }
421
David Greene5393b252009-12-24 00:14:25 +0000422 DEBUG(dbgs() << '\n');
David Goodwin67a8a7b2009-10-29 19:17:04 +0000423
424 // Scan the register defs for this instruction and update
425 // live-ranges.
426 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
427 MachineOperand &MO = MI->getOperand(i);
428 if (!MO.isReg() || !MO.isDef()) continue;
429 unsigned Reg = MO.getReg();
430 if (Reg == 0) continue;
David Goodwin3e72d302009-11-19 23:12:37 +0000431 // Ignore KILLs and passthru registers for liveness...
Chris Lattner518bb532010-02-09 19:54:29 +0000432 if (MI->isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwin3e72d302009-11-19 23:12:37 +0000433 continue;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000434
David Goodwin3e72d302009-11-19 23:12:37 +0000435 // Update def for Reg and aliases.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000436 DefIndices[Reg] = Count;
David Goodwin3e72d302009-11-19 23:12:37 +0000437 for (const unsigned *Alias = TRI->getAliasSet(Reg);
438 *Alias; ++Alias) {
439 unsigned AliasReg = *Alias;
440 DefIndices[AliasReg] = Count;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000441 }
442 }
David Goodwin34877712009-10-26 19:32:42 +0000443}
444
445void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000446 unsigned Count) {
David Greene5393b252009-12-24 00:14:25 +0000447 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000448 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000449 RegRefs = State->GetRegRefs();
David Goodwin34877712009-10-26 19:32:42 +0000450
Evan Cheng46df4eb2010-06-16 07:35:02 +0000451 // If MI's uses have special allocation requirement, don't allow
452 // any use registers to be changed. Also assume all registers
453 // used in a call must not be changed (ABI).
454 // FIXME: The issue with predicated instruction is more complex. We are being
455 // conservatively here because the kill markers cannot be trusted after
456 // if-conversion:
457 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
458 // ...
459 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
460 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
461 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
462 //
463 // The first R6 kill is not really a kill since it's killed by a predicated
464 // instruction which may not be executed. The second R6 def may or may not
465 // re-define R6 so it's not safe to change it since the last R6 use cannot be
466 // changed.
467 bool Special = MI->getDesc().isCall() ||
468 MI->getDesc().hasExtraSrcRegAllocReq() ||
469 TII->isPredicated(MI);
470
David Goodwin34877712009-10-26 19:32:42 +0000471 // Scan the register uses for this instruction and update
472 // live-ranges, groups and RegRefs.
473 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
474 MachineOperand &MO = MI->getOperand(i);
475 if (!MO.isReg() || !MO.isUse()) continue;
476 unsigned Reg = MO.getReg();
477 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000478
479 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
480 State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000481
482 // It wasn't previously live but now it is, this is a kill. Forget
483 // the previous live-range information and start a new live-range
484 // for the register.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000485 HandleLastUse(Reg, Count, "(last-use)");
David Goodwin34877712009-10-26 19:32:42 +0000486
Evan Cheng46df4eb2010-06-16 07:35:02 +0000487 if (Special) {
David Greene5393b252009-12-24 00:14:25 +0000488 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000489 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000490 }
491
492 // Note register reference...
493 const TargetRegisterClass *RC = NULL;
494 if (i < MI->getDesc().getNumOperands())
495 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000496 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000497 RegRefs.insert(std::make_pair(Reg, RR));
498 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000499
David Greene5393b252009-12-24 00:14:25 +0000500 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000501
502 // Form a group of all defs and uses of a KILL instruction to ensure
503 // that all registers are renamed as a group.
Chris Lattner518bb532010-02-09 19:54:29 +0000504 if (MI->isKill()) {
David Greene5393b252009-12-24 00:14:25 +0000505 DEBUG(dbgs() << "\tKill Group:");
David Goodwin34877712009-10-26 19:32:42 +0000506
507 unsigned FirstReg = 0;
508 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
509 MachineOperand &MO = MI->getOperand(i);
510 if (!MO.isReg()) continue;
511 unsigned Reg = MO.getReg();
512 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000513
David Goodwin34877712009-10-26 19:32:42 +0000514 if (FirstReg != 0) {
David Greene5393b252009-12-24 00:14:25 +0000515 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine10deca2009-10-26 22:31:16 +0000516 State->UnionGroups(FirstReg, Reg);
David Goodwin34877712009-10-26 19:32:42 +0000517 } else {
David Greene5393b252009-12-24 00:14:25 +0000518 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000519 FirstReg = Reg;
520 }
521 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000522
David Greene5393b252009-12-24 00:14:25 +0000523 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000524 }
525}
526
527BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
528 BitVector BV(TRI->getNumRegs(), false);
529 bool first = true;
530
531 // Check all references that need rewriting for Reg. For each, use
532 // the corresponding register class to narrow the set of registers
533 // that are appropriate for renaming.
Jim Grosbach2973b572010-01-06 16:48:02 +0000534 std::pair<std::multimap<unsigned,
David Goodwine10deca2009-10-26 22:31:16 +0000535 AggressiveAntiDepState::RegisterReference>::iterator,
536 std::multimap<unsigned,
537 AggressiveAntiDepState::RegisterReference>::iterator>
538 Range = State->GetRegRefs().equal_range(Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000539 for (std::multimap<unsigned,
540 AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first,
541 QE = Range.second; Q != QE; ++Q) {
David Goodwin34877712009-10-26 19:32:42 +0000542 const TargetRegisterClass *RC = Q->second.RC;
543 if (RC == NULL) continue;
544
545 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
546 if (first) {
547 BV |= RCBV;
548 first = false;
549 } else {
550 BV &= RCBV;
551 }
552
David Greene5393b252009-12-24 00:14:25 +0000553 DEBUG(dbgs() << " " << RC->getName());
David Goodwin34877712009-10-26 19:32:42 +0000554 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000555
David Goodwin34877712009-10-26 19:32:42 +0000556 return BV;
Jim Grosbach2973b572010-01-06 16:48:02 +0000557}
David Goodwin34877712009-10-26 19:32:42 +0000558
559bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin54097832009-11-05 01:19:35 +0000560 unsigned AntiDepGroupIndex,
561 RenameOrderType& RenameOrder,
562 std::map<unsigned, unsigned> &RenameMap) {
David Goodwine10deca2009-10-26 22:31:16 +0000563 unsigned *KillIndices = State->GetKillIndices();
564 unsigned *DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000565 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000566 RegRefs = State->GetRegRefs();
567
David Goodwin87d21b92009-11-13 19:52:48 +0000568 // Collect all referenced registers in the same group as
569 // AntiDepReg. These all need to be renamed together if we are to
570 // break the anti-dependence.
David Goodwin34877712009-10-26 19:32:42 +0000571 std::vector<unsigned> Regs;
David Goodwin87d21b92009-11-13 19:52:48 +0000572 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwin34877712009-10-26 19:32:42 +0000573 assert(Regs.size() > 0 && "Empty register group!");
574 if (Regs.size() == 0)
575 return false;
576
577 // Find the "superest" register in the group. At the same time,
578 // collect the BitVector of registers that can be used to rename
579 // each register.
Jim Grosbach2973b572010-01-06 16:48:02 +0000580 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
581 << ":\n");
David Goodwin34877712009-10-26 19:32:42 +0000582 std::map<unsigned, BitVector> RenameRegisterMap;
583 unsigned SuperReg = 0;
584 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
585 unsigned Reg = Regs[i];
586 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
587 SuperReg = Reg;
588
589 // If Reg has any references, then collect possible rename regs
590 if (RegRefs.count(Reg) > 0) {
David Greene5393b252009-12-24 00:14:25 +0000591 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000592
David Goodwin34877712009-10-26 19:32:42 +0000593 BitVector BV = GetRenameRegisters(Reg);
594 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
595
David Greene5393b252009-12-24 00:14:25 +0000596 DEBUG(dbgs() << " ::");
David Goodwin34877712009-10-26 19:32:42 +0000597 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000598 dbgs() << " " << TRI->getName(r));
599 DEBUG(dbgs() << "\n");
David Goodwin34877712009-10-26 19:32:42 +0000600 }
601 }
602
603 // All group registers should be a subreg of SuperReg.
604 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
605 unsigned Reg = Regs[i];
606 if (Reg == SuperReg) continue;
607 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
608 assert(IsSub && "Expecting group subregister");
609 if (!IsSub)
610 return false;
611 }
612
David Goodwin00621ef2009-11-20 23:33:54 +0000613#ifndef NDEBUG
614 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
615 if (DebugDiv > 0) {
616 static int renamecnt = 0;
617 if (renamecnt++ % DebugDiv != DebugMod)
618 return false;
Jim Grosbach2973b572010-01-06 16:48:02 +0000619
David Greene5393b252009-12-24 00:14:25 +0000620 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin00621ef2009-11-20 23:33:54 +0000621 " for debug ***\n";
622 }
623#endif
624
David Goodwin54097832009-11-05 01:19:35 +0000625 // Check each possible rename register for SuperReg in round-robin
626 // order. If that register is available, and the corresponding
627 // registers are available for the other group subregisters, then we
628 // can use those registers to rename.
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000629
630 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
631 // check every use of the register and find the largest register class
632 // that can be used in all of them.
Jim Grosbach2973b572010-01-06 16:48:02 +0000633 const TargetRegisterClass *SuperRC =
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000634 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbach2973b572010-01-06 16:48:02 +0000635
David Goodwin54097832009-11-05 01:19:35 +0000636 const TargetRegisterClass::iterator RB = SuperRC->allocation_order_begin(MF);
637 const TargetRegisterClass::iterator RE = SuperRC->allocation_order_end(MF);
638 if (RB == RE) {
David Greene5393b252009-12-24 00:14:25 +0000639 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin54097832009-11-05 01:19:35 +0000640 return false;
641 }
642
David Greene5393b252009-12-24 00:14:25 +0000643 DEBUG(dbgs() << "\tFind Registers:");
David Goodwin3e72d302009-11-19 23:12:37 +0000644
David Goodwin54097832009-11-05 01:19:35 +0000645 if (RenameOrder.count(SuperRC) == 0)
646 RenameOrder.insert(RenameOrderType::value_type(SuperRC, RE));
647
David Goodwin98f2f1a2009-11-05 01:45:50 +0000648 const TargetRegisterClass::iterator OrigR = RenameOrder[SuperRC];
David Goodwin54097832009-11-05 01:19:35 +0000649 const TargetRegisterClass::iterator EndR = ((OrigR == RE) ? RB : OrigR);
650 TargetRegisterClass::iterator R = OrigR;
651 do {
652 if (R == RB) R = RE;
653 --R;
David Goodwin00621ef2009-11-20 23:33:54 +0000654 const unsigned NewSuperReg = *R;
David Goodwin34877712009-10-26 19:32:42 +0000655 // Don't replace a register with itself.
David Goodwin00621ef2009-11-20 23:33:54 +0000656 if (NewSuperReg == SuperReg) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000657
David Greene5393b252009-12-24 00:14:25 +0000658 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin00621ef2009-11-20 23:33:54 +0000659 RenameMap.clear();
660
661 // For each referenced group register (which must be a SuperReg or
662 // a subregister of SuperReg), find the corresponding subregister
663 // of NewSuperReg and make sure it is free to be renamed.
664 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
665 unsigned Reg = Regs[i];
666 unsigned NewReg = 0;
667 if (Reg == SuperReg) {
668 NewReg = NewSuperReg;
669 } else {
670 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
671 if (NewSubRegIdx != 0)
672 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwin34877712009-10-26 19:32:42 +0000673 }
David Goodwin00621ef2009-11-20 23:33:54 +0000674
David Greene5393b252009-12-24 00:14:25 +0000675 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbach2973b572010-01-06 16:48:02 +0000676
David Goodwin00621ef2009-11-20 23:33:54 +0000677 // Check if Reg can be renamed to NewReg.
678 BitVector BV = RenameRegisterMap[Reg];
679 if (!BV.test(NewReg)) {
David Greene5393b252009-12-24 00:14:25 +0000680 DEBUG(dbgs() << "(no rename)");
David Goodwin00621ef2009-11-20 23:33:54 +0000681 goto next_super_reg;
682 }
683
684 // If NewReg is dead and NewReg's most recent def is not before
685 // Regs's kill, it's safe to replace Reg with NewReg. We
686 // must also check all aliases of NewReg, because we can't define a
687 // register when any sub or super is already live.
688 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene5393b252009-12-24 00:14:25 +0000689 DEBUG(dbgs() << "(live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000690 goto next_super_reg;
691 } else {
692 bool found = false;
693 for (const unsigned *Alias = TRI->getAliasSet(NewReg);
694 *Alias; ++Alias) {
695 unsigned AliasReg = *Alias;
Jim Grosbach2973b572010-01-06 16:48:02 +0000696 if (State->IsLive(AliasReg) ||
697 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene5393b252009-12-24 00:14:25 +0000698 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000699 found = true;
700 break;
701 }
702 }
703 if (found)
704 goto next_super_reg;
705 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000706
David Goodwin00621ef2009-11-20 23:33:54 +0000707 // Record that 'Reg' can be renamed to 'NewReg'.
708 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwin34877712009-10-26 19:32:42 +0000709 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000710
David Goodwin00621ef2009-11-20 23:33:54 +0000711 // If we fall-out here, then every register in the group can be
712 // renamed, as recorded in RenameMap.
713 RenameOrder.erase(SuperRC);
714 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene5393b252009-12-24 00:14:25 +0000715 DEBUG(dbgs() << "]\n");
David Goodwin00621ef2009-11-20 23:33:54 +0000716 return true;
717
718 next_super_reg:
David Greene5393b252009-12-24 00:14:25 +0000719 DEBUG(dbgs() << ']');
David Goodwin54097832009-11-05 01:19:35 +0000720 } while (R != EndR);
David Goodwin34877712009-10-26 19:32:42 +0000721
David Greene5393b252009-12-24 00:14:25 +0000722 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000723
724 // No registers are free and available!
725 return false;
726}
727
728/// BreakAntiDependencies - Identifiy anti-dependencies within the
729/// ScheduleDAG and break them by renaming registers.
730///
David Goodwine10deca2009-10-26 22:31:16 +0000731unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman66db3a02010-04-19 23:11:58 +0000732 const std::vector<SUnit>& SUnits,
733 MachineBasicBlock::iterator Begin,
734 MachineBasicBlock::iterator End,
David Goodwine10deca2009-10-26 22:31:16 +0000735 unsigned InsertPosIndex) {
736 unsigned *KillIndices = State->GetKillIndices();
737 unsigned *DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000738 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000739 RegRefs = State->GetRegRefs();
740
David Goodwin34877712009-10-26 19:32:42 +0000741 // The code below assumes that there is at least one instruction,
742 // so just duck out immediately if the block is empty.
David Goodwin4de099d2009-11-03 20:57:50 +0000743 if (SUnits.empty()) return 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000744
David Goodwin54097832009-11-05 01:19:35 +0000745 // For each regclass the next register to use for renaming.
746 RenameOrderType RenameOrder;
David Goodwin34877712009-10-26 19:32:42 +0000747
748 // ...need a map from MI to SUnit.
Dan Gohman66db3a02010-04-19 23:11:58 +0000749 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwin34877712009-10-26 19:32:42 +0000750 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000751 const SUnit *SU = &SUnits[i];
752 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
753 SU));
David Goodwin34877712009-10-26 19:32:42 +0000754 }
755
David Goodwin87d21b92009-11-13 19:52:48 +0000756 // Track progress along the critical path through the SUnit graph as
757 // we walk the instructions. This is needed for regclasses that only
758 // break critical-path anti-dependencies.
Dan Gohman66db3a02010-04-19 23:11:58 +0000759 const SUnit *CriticalPathSU = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000760 MachineInstr *CriticalPathMI = 0;
761 if (CriticalPathSet.any()) {
762 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000763 const SUnit *SU = &SUnits[i];
Jim Grosbach2973b572010-01-06 16:48:02 +0000764 if (!CriticalPathSU ||
765 ((SU->getDepth() + SU->Latency) >
David Goodwin87d21b92009-11-13 19:52:48 +0000766 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
767 CriticalPathSU = SU;
768 }
769 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000770
David Goodwin87d21b92009-11-13 19:52:48 +0000771 CriticalPathMI = CriticalPathSU->getInstr();
772 }
773
Jim Grosbach2973b572010-01-06 16:48:02 +0000774#ifndef NDEBUG
David Greene5393b252009-12-24 00:14:25 +0000775 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
776 DEBUG(dbgs() << "Available regs:");
David Goodwin557bbe62009-11-20 19:32:48 +0000777 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
778 if (!State->IsLive(Reg))
David Greene5393b252009-12-24 00:14:25 +0000779 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000780 }
David Greene5393b252009-12-24 00:14:25 +0000781 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000782#endif
783
784 // Attempt to break anti-dependence edges. Walk the instructions
785 // from the bottom up, tracking information about liveness as we go
786 // to help determine which registers are available.
787 unsigned Broken = 0;
788 unsigned Count = InsertPosIndex - 1;
789 for (MachineBasicBlock::iterator I = End, E = Begin;
790 I != E; --Count) {
791 MachineInstr *MI = --I;
792
David Greene5393b252009-12-24 00:14:25 +0000793 DEBUG(dbgs() << "Anti: ");
David Goodwin34877712009-10-26 19:32:42 +0000794 DEBUG(MI->dump());
795
796 std::set<unsigned> PassthruRegs;
797 GetPassthruRegs(MI, PassthruRegs);
798
799 // Process the defs in MI...
800 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbach2973b572010-01-06 16:48:02 +0000801
David Goodwin557bbe62009-11-20 19:32:48 +0000802 // The dependence edges that represent anti- and output-
David Goodwin87d21b92009-11-13 19:52:48 +0000803 // dependencies that are candidates for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000804 std::vector<const SDep *> Edges;
805 const SUnit *PathSU = MISUnitMap[MI];
David Goodwin557bbe62009-11-20 19:32:48 +0000806 AntiDepEdges(PathSU, Edges);
David Goodwin87d21b92009-11-13 19:52:48 +0000807
808 // If MI is not on the critical path, then we don't rename
809 // registers in the CriticalPathSet.
810 BitVector *ExcludeRegs = NULL;
811 if (MI == CriticalPathMI) {
812 CriticalPathSU = CriticalPathStep(CriticalPathSU);
813 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000814 } else {
David Goodwin87d21b92009-11-13 19:52:48 +0000815 ExcludeRegs = &CriticalPathSet;
816 }
817
David Goodwin34877712009-10-26 19:32:42 +0000818 // Ignore KILL instructions (they form a group in ScanInstruction
819 // but don't cause any anti-dependence breaking themselves)
Chris Lattner518bb532010-02-09 19:54:29 +0000820 if (!MI->isKill()) {
David Goodwin34877712009-10-26 19:32:42 +0000821 // Attempt to break each anti-dependency...
822 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000823 const SDep *Edge = Edges[i];
David Goodwin34877712009-10-26 19:32:42 +0000824 SUnit *NextSU = Edge->getSUnit();
Jim Grosbach2973b572010-01-06 16:48:02 +0000825
David Goodwin12dd99d2009-11-12 19:08:21 +0000826 if ((Edge->getKind() != SDep::Anti) &&
827 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000828
David Goodwin34877712009-10-26 19:32:42 +0000829 unsigned AntiDepReg = Edge->getReg();
David Greene5393b252009-12-24 00:14:25 +0000830 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwin34877712009-10-26 19:32:42 +0000831 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbach2973b572010-01-06 16:48:02 +0000832
David Goodwin34877712009-10-26 19:32:42 +0000833 if (!AllocatableSet.test(AntiDepReg)) {
834 // Don't break anti-dependencies on non-allocatable registers.
David Greene5393b252009-12-24 00:14:25 +0000835 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwin34877712009-10-26 19:32:42 +0000836 continue;
David Goodwin87d21b92009-11-13 19:52:48 +0000837 } else if ((ExcludeRegs != NULL) && ExcludeRegs->test(AntiDepReg)) {
838 // Don't break anti-dependencies for critical path registers
839 // if not on the critical path
David Greene5393b252009-12-24 00:14:25 +0000840 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwin87d21b92009-11-13 19:52:48 +0000841 continue;
David Goodwin34877712009-10-26 19:32:42 +0000842 } else if (PassthruRegs.count(AntiDepReg) != 0) {
843 // If the anti-dep register liveness "passes-thru", then
844 // don't try to change it. It will be changed along with
845 // the use if required to break an earlier antidep.
David Greene5393b252009-12-24 00:14:25 +0000846 DEBUG(dbgs() << " (passthru)\n");
David Goodwin34877712009-10-26 19:32:42 +0000847 continue;
848 } else {
849 // No anti-dep breaking for implicit deps
850 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000851 assert(AntiDepOp != NULL &&
852 "Can't find index for defined register operand");
David Goodwin34877712009-10-26 19:32:42 +0000853 if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) {
David Greene5393b252009-12-24 00:14:25 +0000854 DEBUG(dbgs() << " (implicit)\n");
David Goodwin34877712009-10-26 19:32:42 +0000855 continue;
856 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000857
David Goodwin34877712009-10-26 19:32:42 +0000858 // If the SUnit has other dependencies on the SUnit that
859 // it anti-depends on, don't bother breaking the
860 // anti-dependency since those edges would prevent such
861 // units from being scheduled past each other
862 // regardless.
David Goodwin557bbe62009-11-20 19:32:48 +0000863 //
864 // Also, if there are dependencies on other SUnits with the
865 // same register as the anti-dependency, don't attempt to
866 // break it.
Dan Gohman66db3a02010-04-19 23:11:58 +0000867 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin34877712009-10-26 19:32:42 +0000868 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin557bbe62009-11-20 19:32:48 +0000869 if (P->getSUnit() == NextSU ?
870 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
871 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
872 AntiDepReg = 0;
873 break;
874 }
875 }
Dan Gohman66db3a02010-04-19 23:11:58 +0000876 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin557bbe62009-11-20 19:32:48 +0000877 PE = PathSU->Preds.end(); P != PE; ++P) {
878 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
879 (P->getKind() != SDep::Output)) {
David Greene5393b252009-12-24 00:14:25 +0000880 DEBUG(dbgs() << " (real dependency)\n");
David Goodwin34877712009-10-26 19:32:42 +0000881 AntiDepReg = 0;
882 break;
Jim Grosbach2973b572010-01-06 16:48:02 +0000883 } else if ((P->getSUnit() != NextSU) &&
884 (P->getKind() == SDep::Data) &&
David Goodwin557bbe62009-11-20 19:32:48 +0000885 (P->getReg() == AntiDepReg)) {
David Greene5393b252009-12-24 00:14:25 +0000886 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin557bbe62009-11-20 19:32:48 +0000887 AntiDepReg = 0;
888 break;
David Goodwin34877712009-10-26 19:32:42 +0000889 }
890 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000891
David Goodwin34877712009-10-26 19:32:42 +0000892 if (AntiDepReg == 0) continue;
893 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000894
David Goodwin34877712009-10-26 19:32:42 +0000895 assert(AntiDepReg != 0);
896 if (AntiDepReg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000897
David Goodwin34877712009-10-26 19:32:42 +0000898 // Determine AntiDepReg's register group.
David Goodwine10deca2009-10-26 22:31:16 +0000899 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwin34877712009-10-26 19:32:42 +0000900 if (GroupIndex == 0) {
David Greene5393b252009-12-24 00:14:25 +0000901 DEBUG(dbgs() << " (zero group)\n");
David Goodwin34877712009-10-26 19:32:42 +0000902 continue;
903 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000904
David Greene5393b252009-12-24 00:14:25 +0000905 DEBUG(dbgs() << '\n');
Jim Grosbach2973b572010-01-06 16:48:02 +0000906
David Goodwin34877712009-10-26 19:32:42 +0000907 // Look for a suitable register to use to break the anti-dependence.
908 std::map<unsigned, unsigned> RenameMap;
David Goodwin54097832009-11-05 01:19:35 +0000909 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene5393b252009-12-24 00:14:25 +0000910 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwin34877712009-10-26 19:32:42 +0000911 << TRI->getName(AntiDepReg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000912
David Goodwin34877712009-10-26 19:32:42 +0000913 // Handle each group register...
914 for (std::map<unsigned, unsigned>::iterator
915 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
916 unsigned CurrReg = S->first;
917 unsigned NewReg = S->second;
Jim Grosbach2973b572010-01-06 16:48:02 +0000918
919 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
920 TRI->getName(NewReg) << "(" <<
David Goodwin34877712009-10-26 19:32:42 +0000921 RegRefs.count(CurrReg) << " refs)");
Jim Grosbach2973b572010-01-06 16:48:02 +0000922
David Goodwin34877712009-10-26 19:32:42 +0000923 // Update the references to the old register CurrReg to
924 // refer to the new register NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000925 std::pair<std::multimap<unsigned,
926 AggressiveAntiDepState::RegisterReference>::iterator,
David Goodwine10deca2009-10-26 22:31:16 +0000927 std::multimap<unsigned,
Jim Grosbach2973b572010-01-06 16:48:02 +0000928 AggressiveAntiDepState::RegisterReference>::iterator>
David Goodwin34877712009-10-26 19:32:42 +0000929 Range = RegRefs.equal_range(CurrReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000930 for (std::multimap<unsigned,
931 AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000932 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
933 Q->second.Operand->setReg(NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000934 // If the SU for the instruction being updated has debug
935 // information related to the anti-dependency register, make
936 // sure to update that as well.
937 const SUnit *SU = MISUnitMap[Q->second.Operand->getParent()];
Jim Grosbach086723d2010-06-02 15:29:36 +0000938 if (!SU) continue;
Jim Grosbach533934e2010-06-01 23:48:44 +0000939 for (unsigned i = 0, e = SU->DbgInstrList.size() ; i < e ; ++i) {
940 MachineInstr *DI = SU->DbgInstrList[i];
941 assert (DI->getNumOperands()==3 && DI->getOperand(0).isReg() &&
942 DI->getOperand(0).getReg()
943 && "Non register dbg_value attached to SUnit!");
944 if (DI->getOperand(0).getReg() == AntiDepReg)
945 DI->getOperand(0).setReg(NewReg);
946 }
David Goodwin34877712009-10-26 19:32:42 +0000947 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000948
David Goodwin34877712009-10-26 19:32:42 +0000949 // We just went back in time and modified history; the
950 // liveness information for CurrReg is now inconsistent. Set
951 // the state as if it were dead.
David Goodwine10deca2009-10-26 22:31:16 +0000952 State->UnionGroups(NewReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000953 RegRefs.erase(NewReg);
954 DefIndices[NewReg] = DefIndices[CurrReg];
955 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbach2973b572010-01-06 16:48:02 +0000956
David Goodwine10deca2009-10-26 22:31:16 +0000957 State->UnionGroups(CurrReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000958 RegRefs.erase(CurrReg);
959 DefIndices[CurrReg] = KillIndices[CurrReg];
960 KillIndices[CurrReg] = ~0u;
961 assert(((KillIndices[CurrReg] == ~0u) !=
962 (DefIndices[CurrReg] == ~0u)) &&
963 "Kill and Def maps aren't consistent for AntiDepReg!");
964 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000965
David Goodwin34877712009-10-26 19:32:42 +0000966 ++Broken;
David Greene5393b252009-12-24 00:14:25 +0000967 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000968 }
969 }
970 }
971
972 ScanInstruction(MI, Count);
973 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000974
David Goodwin34877712009-10-26 19:32:42 +0000975 return Broken;
976}