blob: c23351b6e5afbfc245f85426e3c3d9789d5982d1 [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"
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000019#include "RegisterClassInfo.h"
David Goodwin34877712009-10-26 19:32:42 +000020#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetMachine.h"
Evan Cheng46df4eb2010-06-16 07:35:02 +000025#include "llvm/Target/TargetInstrInfo.h"
Bill Wendling75a5b712010-07-15 06:05:18 +000026#include "llvm/Target/TargetRegisterInfo.h"
David Goodwine10deca2009-10-26 22:31:16 +000027#include "llvm/Support/CommandLine.h"
David Goodwin34877712009-10-26 19:32:42 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
David Goodwin34877712009-10-26 19:32:42 +000031using namespace llvm;
32
David Goodwin3e72d302009-11-19 23:12:37 +000033// If DebugDiv > 0 then only break antidep with (ID % DebugDiv) == DebugMod
34static cl::opt<int>
35DebugDiv("agg-antidep-debugdiv",
Bob Wilson347fa3f2010-04-09 21:38:26 +000036 cl::desc("Debug control for aggressive anti-dep breaker"),
37 cl::init(0), cl::Hidden);
David Goodwin3e72d302009-11-19 23:12:37 +000038static cl::opt<int>
39DebugMod("agg-antidep-debugmod",
Bob Wilson347fa3f2010-04-09 21:38:26 +000040 cl::desc("Debug control for aggressive anti-dep breaker"),
41 cl::init(0), cl::Hidden);
David Goodwin3e72d302009-11-19 23:12:37 +000042
David Goodwin990d2852009-12-09 17:18:22 +000043AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
44 MachineBasicBlock *BB) :
Bill Wendling9c2a0342010-07-15 19:58:14 +000045 NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0),
46 GroupNodeIndices(TargetRegs, 0),
47 KillIndices(TargetRegs, 0),
48 DefIndices(TargetRegs, 0)
49{
David Goodwin990d2852009-12-09 17:18:22 +000050 const unsigned BBSize = BB->size();
51 for (unsigned i = 0; i < NumTargetRegs; ++i) {
52 // Initialize all registers to be in their own group. Initially we
53 // assign the register to the same-indexed GroupNode.
54 GroupNodeIndices[i] = i;
55 // Initialize the indices to indicate that no registers are live.
56 KillIndices[i] = ~0u;
57 DefIndices[i] = BBSize;
58 }
David Goodwin34877712009-10-26 19:32:42 +000059}
60
Bill Wendlinge4a41472010-07-15 19:41:20 +000061unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) {
David Goodwin34877712009-10-26 19:32:42 +000062 unsigned Node = GroupNodeIndices[Reg];
63 while (GroupNodes[Node] != Node)
64 Node = GroupNodes[Node];
65
66 return Node;
67}
68
David Goodwin87d21b92009-11-13 19:52:48 +000069void AggressiveAntiDepState::GetGroupRegs(
70 unsigned Group,
71 std::vector<unsigned> &Regs,
72 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
David Goodwin34877712009-10-26 19:32:42 +000073{
David Goodwin990d2852009-12-09 17:18:22 +000074 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
David Goodwin87d21b92009-11-13 19:52:48 +000075 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
David Goodwin34877712009-10-26 19:32:42 +000076 Regs.push_back(Reg);
77 }
78}
79
David Goodwine10deca2009-10-26 22:31:16 +000080unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2)
David Goodwin34877712009-10-26 19:32:42 +000081{
82 assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
83 assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
Jim Grosbach2973b572010-01-06 16:48:02 +000084
David Goodwin34877712009-10-26 19:32:42 +000085 // find group for each register
86 unsigned Group1 = GetGroup(Reg1);
87 unsigned Group2 = GetGroup(Reg2);
Jim Grosbach2973b572010-01-06 16:48:02 +000088
David Goodwin34877712009-10-26 19:32:42 +000089 // if either group is 0, then that must become the parent
90 unsigned Parent = (Group1 == 0) ? Group1 : Group2;
91 unsigned Other = (Parent == Group1) ? Group2 : Group1;
92 GroupNodes.at(Other) = Parent;
93 return Parent;
94}
Jim Grosbach2973b572010-01-06 16:48:02 +000095
David Goodwine10deca2009-10-26 22:31:16 +000096unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000097{
98 // Create a new GroupNode for Reg. Reg's existing GroupNode must
99 // stay as is because there could be other GroupNodes referring to
100 // it.
101 unsigned idx = GroupNodes.size();
102 GroupNodes.push_back(idx);
103 GroupNodeIndices[Reg] = idx;
104 return idx;
105}
106
David Goodwine10deca2009-10-26 22:31:16 +0000107bool AggressiveAntiDepState::IsLive(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +0000108{
109 // KillIndex must be defined and DefIndex not defined for a register
110 // to be live.
111 return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
112}
113
David Goodwine10deca2009-10-26 22:31:16 +0000114
115
116AggressiveAntiDepBreaker::
David Goodwin0855dee2009-11-10 00:15:47 +0000117AggressiveAntiDepBreaker(MachineFunction& MFi,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000118 const RegisterClassInfo &RCI,
Jim Grosbach2973b572010-01-06 16:48:02 +0000119 TargetSubtarget::RegClassVector& CriticalPathRCs) :
David Goodwine10deca2009-10-26 22:31:16 +0000120 AntiDepBreaker(), MF(MFi),
121 MRI(MF.getRegInfo()),
Evan Cheng46df4eb2010-06-16 07:35:02 +0000122 TII(MF.getTarget().getInstrInfo()),
David Goodwine10deca2009-10-26 22:31:16 +0000123 TRI(MF.getTarget().getRegisterInfo()),
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000124 RegClassInfo(RCI),
David Goodwin557bbe62009-11-20 19:32:48 +0000125 State(NULL) {
David Goodwin87d21b92009-11-13 19:52:48 +0000126 /* Collect a bitset of all registers that are only broken if they
127 are on the critical path. */
128 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
129 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
130 if (CriticalPathSet.none())
131 CriticalPathSet = CPSet;
132 else
133 CriticalPathSet |= CPSet;
134 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000135
David Greene5393b252009-12-24 00:14:25 +0000136 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000137 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwin87d21b92009-11-13 19:52:48 +0000138 r = CriticalPathSet.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000139 dbgs() << " " << TRI->getName(r));
140 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000141}
142
143AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
144 delete State;
David Goodwine10deca2009-10-26 22:31:16 +0000145}
146
147void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
148 assert(State == NULL);
David Goodwin990d2852009-12-09 17:18:22 +0000149 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine10deca2009-10-26 22:31:16 +0000150
151 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
Bill Wendling38306d52010-07-15 18:43:09 +0000152 std::vector<unsigned> &KillIndices = State->GetKillIndices();
153 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwine10deca2009-10-26 22:31:16 +0000154
155 // Determine the live-out physregs for this block.
156 if (IsReturnBlock) {
157 // In a return block, examine the function live-out regs.
158 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
159 E = MRI.liveout_end(); I != E; ++I) {
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000160 for (const unsigned *Alias = TRI->getOverlaps(*I);
161 unsigned Reg = *Alias; ++Alias) {
162 State->UnionGroups(Reg, 0);
163 KillIndices[Reg] = BB->size();
164 DefIndices[Reg] = ~0u;
David Goodwine10deca2009-10-26 22:31:16 +0000165 }
166 }
David Goodwine10deca2009-10-26 22:31:16 +0000167 }
168
Evan Cheng46df4eb2010-06-16 07:35:02 +0000169 // In a non-return block, examine the live-in regs of all successors.
170 // Note a return block can have successors if the return instruction is
171 // predicated.
172 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
173 SE = BB->succ_end(); SI != SE; ++SI)
174 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
175 E = (*SI)->livein_end(); I != E; ++I) {
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000176 for (const unsigned *Alias = TRI->getOverlaps(*I);
177 unsigned Reg = *Alias; ++Alias) {
178 State->UnionGroups(Reg, 0);
179 KillIndices[Reg] = BB->size();
180 DefIndices[Reg] = ~0u;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000181 }
182 }
183
David Goodwine10deca2009-10-26 22:31:16 +0000184 // Mark live-out callee-saved registers. In a return block this is
185 // all callee-saved registers. In non-return this is any
186 // callee-saved register that is not saved in the prolog.
187 const MachineFrameInfo *MFI = MF.getFrameInfo();
188 BitVector Pristine = MFI->getPristineRegs(BB);
189 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
190 unsigned Reg = *I;
191 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000192 for (const unsigned *Alias = TRI->getOverlaps(Reg);
193 unsigned AliasReg = *Alias; ++Alias) {
David Goodwine10deca2009-10-26 22:31:16 +0000194 State->UnionGroups(AliasReg, 0);
195 KillIndices[AliasReg] = BB->size();
196 DefIndices[AliasReg] = ~0u;
197 }
198 }
199}
200
201void AggressiveAntiDepBreaker::FinishBlock() {
202 delete State;
203 State = NULL;
David Goodwine10deca2009-10-26 22:31:16 +0000204}
205
206void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000207 unsigned InsertPosIndex) {
David Goodwine10deca2009-10-26 22:31:16 +0000208 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
209
David Goodwin5b3c3082009-10-29 23:30:59 +0000210 std::set<unsigned> PassthruRegs;
211 GetPassthruRegs(MI, PassthruRegs);
212 PrescanInstruction(MI, Count, PassthruRegs);
213 ScanInstruction(MI, Count);
214
David Greene5393b252009-12-24 00:14:25 +0000215 DEBUG(dbgs() << "Observe: ");
David Goodwine10deca2009-10-26 22:31:16 +0000216 DEBUG(MI->dump());
David Greene5393b252009-12-24 00:14:25 +0000217 DEBUG(dbgs() << "\tRegs:");
David Goodwine10deca2009-10-26 22:31:16 +0000218
Bill Wendling38306d52010-07-15 18:43:09 +0000219 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwin990d2852009-12-09 17:18:22 +0000220 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine10deca2009-10-26 22:31:16 +0000221 // If Reg is current live, then mark that it can't be renamed as
222 // we don't know the extent of its live-range anymore (now that it
223 // has been scheduled). If it is not live but was defined in the
224 // previous schedule region, then set its def index to the most
225 // conservative location (i.e. the beginning of the previous
226 // schedule region).
227 if (State->IsLive(Reg)) {
228 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbach2973b572010-01-06 16:48:02 +0000229 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine10deca2009-10-26 22:31:16 +0000230 State->GetGroup(Reg) << "->g0(region live-out)");
231 State->UnionGroups(Reg, 0);
Jim Grosbach2973b572010-01-06 16:48:02 +0000232 } else if ((DefIndices[Reg] < InsertPosIndex)
233 && (DefIndices[Reg] >= Count)) {
David Goodwine10deca2009-10-26 22:31:16 +0000234 DefIndices[Reg] = Count;
235 }
236 }
David Greene5393b252009-12-24 00:14:25 +0000237 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000238}
239
David Goodwin34877712009-10-26 19:32:42 +0000240bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000241 MachineOperand& MO)
David Goodwin34877712009-10-26 19:32:42 +0000242{
243 if (!MO.isReg() || !MO.isImplicit())
244 return false;
245
246 unsigned Reg = MO.getReg();
247 if (Reg == 0)
248 return false;
249
250 MachineOperand *Op = NULL;
251 if (MO.isDef())
252 Op = MI->findRegisterUseOperand(Reg, true);
253 else
254 Op = MI->findRegisterDefOperand(Reg);
255
256 return((Op != NULL) && Op->isImplicit());
257}
258
259void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
260 std::set<unsigned>& PassthruRegs) {
261 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
262 MachineOperand &MO = MI->getOperand(i);
263 if (!MO.isReg()) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000264 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
David Goodwin34877712009-10-26 19:32:42 +0000265 IsImplicitDefUse(MI, MO)) {
266 const unsigned Reg = MO.getReg();
267 PassthruRegs.insert(Reg);
268 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
269 *Subreg; ++Subreg) {
270 PassthruRegs.insert(*Subreg);
271 }
272 }
273 }
274}
275
David Goodwin557bbe62009-11-20 19:32:48 +0000276/// AntiDepEdges - Return in Edges the anti- and output- dependencies
277/// in SU that we want to consider for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000278static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin557bbe62009-11-20 19:32:48 +0000279 SmallSet<unsigned, 4> RegSet;
Dan Gohman66db3a02010-04-19 23:11:58 +0000280 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin34877712009-10-26 19:32:42 +0000281 P != PE; ++P) {
David Goodwin12dd99d2009-11-12 19:08:21 +0000282 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Goodwin34877712009-10-26 19:32:42 +0000283 unsigned Reg = P->getReg();
David Goodwin557bbe62009-11-20 19:32:48 +0000284 if (RegSet.count(Reg) == 0) {
David Goodwin34877712009-10-26 19:32:42 +0000285 Edges.push_back(&*P);
David Goodwin557bbe62009-11-20 19:32:48 +0000286 RegSet.insert(Reg);
David Goodwin34877712009-10-26 19:32:42 +0000287 }
288 }
289 }
290}
291
David Goodwin87d21b92009-11-13 19:52:48 +0000292/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
293/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000294static const SUnit *CriticalPathStep(const SUnit *SU) {
295 const SDep *Next = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000296 unsigned NextDepth = 0;
297 // Find the predecessor edge with the greatest depth.
298 if (SU != 0) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000299 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin87d21b92009-11-13 19:52:48 +0000300 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000301 const SUnit *PredSU = P->getSUnit();
David Goodwin87d21b92009-11-13 19:52:48 +0000302 unsigned PredLatency = P->getLatency();
303 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
304 // In the case of a latency tie, prefer an anti-dependency edge over
305 // other types of edges.
306 if (NextDepth < PredTotalLatency ||
307 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
308 NextDepth = PredTotalLatency;
309 Next = &*P;
310 }
311 }
312 }
313
314 return (Next) ? Next->getSUnit() : 0;
315}
316
David Goodwin67a8a7b2009-10-29 19:17:04 +0000317void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbach2973b572010-01-06 16:48:02 +0000318 const char *tag,
319 const char *header,
David Goodwin3e72d302009-11-19 23:12:37 +0000320 const char *footer) {
Bill Wendling38306d52010-07-15 18:43:09 +0000321 std::vector<unsigned> &KillIndices = State->GetKillIndices();
322 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000323 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin67a8a7b2009-10-29 19:17:04 +0000324 RegRefs = State->GetRegRefs();
325
326 if (!State->IsLive(Reg)) {
327 KillIndices[Reg] = KillIdx;
328 DefIndices[Reg] = ~0u;
329 RegRefs.erase(Reg);
330 State->LeaveGroup(Reg);
David Goodwin3e72d302009-11-19 23:12:37 +0000331 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000332 dbgs() << header << TRI->getName(Reg); header = NULL; });
333 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000334 }
335 // Repeat for subregisters.
336 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
337 *Subreg; ++Subreg) {
338 unsigned SubregReg = *Subreg;
339 if (!State->IsLive(SubregReg)) {
340 KillIndices[SubregReg] = KillIdx;
341 DefIndices[SubregReg] = ~0u;
342 RegRefs.erase(SubregReg);
343 State->LeaveGroup(SubregReg);
David Goodwin3e72d302009-11-19 23:12:37 +0000344 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000345 dbgs() << header << TRI->getName(Reg); header = NULL; });
346 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin67a8a7b2009-10-29 19:17:04 +0000347 State->GetGroup(SubregReg) << tag);
348 }
349 }
David Goodwin3e72d302009-11-19 23:12:37 +0000350
David Greene5393b252009-12-24 00:14:25 +0000351 DEBUG(if ((header == NULL) && (footer != NULL)) dbgs() << footer);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000352}
353
Jim Grosbach2973b572010-01-06 16:48:02 +0000354void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
355 unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000356 std::set<unsigned>& PassthruRegs) {
Bill Wendling38306d52010-07-15 18:43:09 +0000357 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000358 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000359 RegRefs = State->GetRegRefs();
360
David Goodwin67a8a7b2009-10-29 19:17:04 +0000361 // Handle dead defs by simulating a last-use of the register just
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000362 // after the def. A dead def can occur because the def is truly
David Goodwin67a8a7b2009-10-29 19:17:04 +0000363 // dead, or because only a subregister is live at the def. If we
364 // don't do this the dead def will be incorrectly merged into the
365 // previous def.
David Goodwin34877712009-10-26 19:32:42 +0000366 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
367 MachineOperand &MO = MI->getOperand(i);
368 if (!MO.isReg() || !MO.isDef()) continue;
369 unsigned Reg = MO.getReg();
370 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000371
David Goodwin3e72d302009-11-19 23:12:37 +0000372 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwin34877712009-10-26 19:32:42 +0000373 }
374
David Greene5393b252009-12-24 00:14:25 +0000375 DEBUG(dbgs() << "\tDef Groups:");
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;
381
Jim Grosbach2973b572010-01-06 16:48:02 +0000382 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000383
David Goodwin67a8a7b2009-10-29 19:17:04 +0000384 // If MI's defs have a special allocation requirement, don't allow
David Goodwin34877712009-10-26 19:32:42 +0000385 // any def registers to be changed. Also assume all registers
386 // defined in a call must not be changed (ABI).
Evan Cheng46df4eb2010-06-16 07:35:02 +0000387 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq() ||
388 TII->isPredicated(MI)) {
David Greene5393b252009-12-24 00:14:25 +0000389 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000390 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000391 }
392
393 // Any aliased that are live at this point are completely or
David Goodwin67a8a7b2009-10-29 19:17:04 +0000394 // partially defined here, so group those aliases with Reg.
David Goodwin34877712009-10-26 19:32:42 +0000395 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
396 unsigned AliasReg = *Alias;
David Goodwine10deca2009-10-26 22:31:16 +0000397 if (State->IsLive(AliasReg)) {
398 State->UnionGroups(Reg, AliasReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000399 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwin34877712009-10-26 19:32:42 +0000400 TRI->getName(AliasReg) << ")");
401 }
402 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000403
David Goodwin34877712009-10-26 19:32:42 +0000404 // Note register reference...
405 const TargetRegisterClass *RC = NULL;
406 if (i < MI->getDesc().getNumOperands())
407 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000408 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000409 RegRefs.insert(std::make_pair(Reg, RR));
410 }
411
David Greene5393b252009-12-24 00:14:25 +0000412 DEBUG(dbgs() << '\n');
David Goodwin67a8a7b2009-10-29 19:17:04 +0000413
414 // Scan the register defs for this instruction and update
415 // live-ranges.
416 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
417 MachineOperand &MO = MI->getOperand(i);
418 if (!MO.isReg() || !MO.isDef()) continue;
419 unsigned Reg = MO.getReg();
420 if (Reg == 0) continue;
David Goodwin3e72d302009-11-19 23:12:37 +0000421 // Ignore KILLs and passthru registers for liveness...
Chris Lattner518bb532010-02-09 19:54:29 +0000422 if (MI->isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwin3e72d302009-11-19 23:12:37 +0000423 continue;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000424
David Goodwin3e72d302009-11-19 23:12:37 +0000425 // Update def for Reg and aliases.
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000426 for (const unsigned *Alias = TRI->getOverlaps(Reg);
427 unsigned AliasReg = *Alias; ++Alias)
David Goodwin3e72d302009-11-19 23:12:37 +0000428 DefIndices[AliasReg] = Count;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000429 }
David Goodwin34877712009-10-26 19:32:42 +0000430}
431
432void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000433 unsigned Count) {
David Greene5393b252009-12-24 00:14:25 +0000434 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000435 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000436 RegRefs = State->GetRegRefs();
David Goodwin34877712009-10-26 19:32:42 +0000437
Evan Cheng46df4eb2010-06-16 07:35:02 +0000438 // If MI's uses have special allocation requirement, don't allow
439 // any use registers to be changed. Also assume all registers
440 // used in a call must not be changed (ABI).
441 // FIXME: The issue with predicated instruction is more complex. We are being
442 // conservatively here because the kill markers cannot be trusted after
443 // if-conversion:
444 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
445 // ...
446 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
447 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
448 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
449 //
450 // The first R6 kill is not really a kill since it's killed by a predicated
451 // instruction which may not be executed. The second R6 def may or may not
452 // re-define R6 so it's not safe to change it since the last R6 use cannot be
453 // changed.
454 bool Special = MI->getDesc().isCall() ||
455 MI->getDesc().hasExtraSrcRegAllocReq() ||
456 TII->isPredicated(MI);
457
David Goodwin34877712009-10-26 19:32:42 +0000458 // Scan the register uses for this instruction and update
459 // live-ranges, groups and RegRefs.
460 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
461 MachineOperand &MO = MI->getOperand(i);
462 if (!MO.isReg() || !MO.isUse()) continue;
463 unsigned Reg = MO.getReg();
464 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000465
466 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
467 State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000468
469 // It wasn't previously live but now it is, this is a kill. Forget
470 // the previous live-range information and start a new live-range
471 // for the register.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000472 HandleLastUse(Reg, Count, "(last-use)");
David Goodwin34877712009-10-26 19:32:42 +0000473
Evan Cheng46df4eb2010-06-16 07:35:02 +0000474 if (Special) {
David Greene5393b252009-12-24 00:14:25 +0000475 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000476 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000477 }
478
479 // Note register reference...
480 const TargetRegisterClass *RC = NULL;
481 if (i < MI->getDesc().getNumOperands())
482 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000483 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000484 RegRefs.insert(std::make_pair(Reg, RR));
485 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000486
David Greene5393b252009-12-24 00:14:25 +0000487 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000488
489 // Form a group of all defs and uses of a KILL instruction to ensure
490 // that all registers are renamed as a group.
Chris Lattner518bb532010-02-09 19:54:29 +0000491 if (MI->isKill()) {
David Greene5393b252009-12-24 00:14:25 +0000492 DEBUG(dbgs() << "\tKill Group:");
David Goodwin34877712009-10-26 19:32:42 +0000493
494 unsigned FirstReg = 0;
495 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
496 MachineOperand &MO = MI->getOperand(i);
497 if (!MO.isReg()) continue;
498 unsigned Reg = MO.getReg();
499 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000500
David Goodwin34877712009-10-26 19:32:42 +0000501 if (FirstReg != 0) {
David Greene5393b252009-12-24 00:14:25 +0000502 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine10deca2009-10-26 22:31:16 +0000503 State->UnionGroups(FirstReg, Reg);
David Goodwin34877712009-10-26 19:32:42 +0000504 } else {
David Greene5393b252009-12-24 00:14:25 +0000505 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000506 FirstReg = Reg;
507 }
508 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000509
David Greene5393b252009-12-24 00:14:25 +0000510 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000511 }
512}
513
514BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
515 BitVector BV(TRI->getNumRegs(), false);
516 bool first = true;
517
518 // Check all references that need rewriting for Reg. For each, use
519 // the corresponding register class to narrow the set of registers
520 // that are appropriate for renaming.
Jim Grosbach2973b572010-01-06 16:48:02 +0000521 std::pair<std::multimap<unsigned,
David Goodwine10deca2009-10-26 22:31:16 +0000522 AggressiveAntiDepState::RegisterReference>::iterator,
523 std::multimap<unsigned,
524 AggressiveAntiDepState::RegisterReference>::iterator>
525 Range = State->GetRegRefs().equal_range(Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000526 for (std::multimap<unsigned,
527 AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first,
528 QE = Range.second; Q != QE; ++Q) {
David Goodwin34877712009-10-26 19:32:42 +0000529 const TargetRegisterClass *RC = Q->second.RC;
530 if (RC == NULL) continue;
531
532 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
533 if (first) {
534 BV |= RCBV;
535 first = false;
536 } else {
537 BV &= RCBV;
538 }
539
David Greene5393b252009-12-24 00:14:25 +0000540 DEBUG(dbgs() << " " << RC->getName());
David Goodwin34877712009-10-26 19:32:42 +0000541 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000542
David Goodwin34877712009-10-26 19:32:42 +0000543 return BV;
Jim Grosbach2973b572010-01-06 16:48:02 +0000544}
David Goodwin34877712009-10-26 19:32:42 +0000545
546bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin54097832009-11-05 01:19:35 +0000547 unsigned AntiDepGroupIndex,
548 RenameOrderType& RenameOrder,
549 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendling38306d52010-07-15 18:43:09 +0000550 std::vector<unsigned> &KillIndices = State->GetKillIndices();
551 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000552 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000553 RegRefs = State->GetRegRefs();
554
David Goodwin87d21b92009-11-13 19:52:48 +0000555 // Collect all referenced registers in the same group as
556 // AntiDepReg. These all need to be renamed together if we are to
557 // break the anti-dependence.
David Goodwin34877712009-10-26 19:32:42 +0000558 std::vector<unsigned> Regs;
David Goodwin87d21b92009-11-13 19:52:48 +0000559 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwin34877712009-10-26 19:32:42 +0000560 assert(Regs.size() > 0 && "Empty register group!");
561 if (Regs.size() == 0)
562 return false;
563
564 // Find the "superest" register in the group. At the same time,
565 // collect the BitVector of registers that can be used to rename
566 // each register.
Jim Grosbach2973b572010-01-06 16:48:02 +0000567 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
568 << ":\n");
David Goodwin34877712009-10-26 19:32:42 +0000569 std::map<unsigned, BitVector> RenameRegisterMap;
570 unsigned SuperReg = 0;
571 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
572 unsigned Reg = Regs[i];
573 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
574 SuperReg = Reg;
575
576 // If Reg has any references, then collect possible rename regs
577 if (RegRefs.count(Reg) > 0) {
David Greene5393b252009-12-24 00:14:25 +0000578 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000579
David Goodwin34877712009-10-26 19:32:42 +0000580 BitVector BV = GetRenameRegisters(Reg);
581 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
582
David Greene5393b252009-12-24 00:14:25 +0000583 DEBUG(dbgs() << " ::");
David Goodwin34877712009-10-26 19:32:42 +0000584 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000585 dbgs() << " " << TRI->getName(r));
586 DEBUG(dbgs() << "\n");
David Goodwin34877712009-10-26 19:32:42 +0000587 }
588 }
589
590 // All group registers should be a subreg of SuperReg.
591 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
592 unsigned Reg = Regs[i];
593 if (Reg == SuperReg) continue;
594 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
595 assert(IsSub && "Expecting group subregister");
596 if (!IsSub)
597 return false;
598 }
599
David Goodwin00621ef2009-11-20 23:33:54 +0000600#ifndef NDEBUG
601 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
602 if (DebugDiv > 0) {
603 static int renamecnt = 0;
604 if (renamecnt++ % DebugDiv != DebugMod)
605 return false;
Jim Grosbach2973b572010-01-06 16:48:02 +0000606
David Greene5393b252009-12-24 00:14:25 +0000607 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin00621ef2009-11-20 23:33:54 +0000608 " for debug ***\n";
609 }
610#endif
611
David Goodwin54097832009-11-05 01:19:35 +0000612 // Check each possible rename register for SuperReg in round-robin
613 // order. If that register is available, and the corresponding
614 // registers are available for the other group subregisters, then we
615 // can use those registers to rename.
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000616
617 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
618 // check every use of the register and find the largest register class
619 // that can be used in all of them.
Jim Grosbach2973b572010-01-06 16:48:02 +0000620 const TargetRegisterClass *SuperRC =
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000621 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbach2973b572010-01-06 16:48:02 +0000622
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000623 ArrayRef<unsigned> Order = RegClassInfo.getOrder(SuperRC);
624 if (Order.empty()) {
David Greene5393b252009-12-24 00:14:25 +0000625 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin54097832009-11-05 01:19:35 +0000626 return false;
627 }
628
David Greene5393b252009-12-24 00:14:25 +0000629 DEBUG(dbgs() << "\tFind Registers:");
David Goodwin3e72d302009-11-19 23:12:37 +0000630
David Goodwin54097832009-11-05 01:19:35 +0000631 if (RenameOrder.count(SuperRC) == 0)
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000632 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
David Goodwin54097832009-11-05 01:19:35 +0000633
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000634 unsigned OrigR = RenameOrder[SuperRC];
635 unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
636 unsigned R = OrigR;
David Goodwin54097832009-11-05 01:19:35 +0000637 do {
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000638 if (R == 0) R = Order.size();
David Goodwin54097832009-11-05 01:19:35 +0000639 --R;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000640 const unsigned NewSuperReg = Order[R];
Jim Grosbach9b041c92010-09-02 17:12:55 +0000641 // Don't consider non-allocatable registers
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000642 if (!RegClassInfo.isAllocatable(NewSuperReg)) continue;
David Goodwin34877712009-10-26 19:32:42 +0000643 // Don't replace a register with itself.
David Goodwin00621ef2009-11-20 23:33:54 +0000644 if (NewSuperReg == SuperReg) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000645
David Greene5393b252009-12-24 00:14:25 +0000646 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin00621ef2009-11-20 23:33:54 +0000647 RenameMap.clear();
648
649 // For each referenced group register (which must be a SuperReg or
650 // a subregister of SuperReg), find the corresponding subregister
651 // of NewSuperReg and make sure it is free to be renamed.
652 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
653 unsigned Reg = Regs[i];
654 unsigned NewReg = 0;
655 if (Reg == SuperReg) {
656 NewReg = NewSuperReg;
657 } else {
658 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
659 if (NewSubRegIdx != 0)
660 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwin34877712009-10-26 19:32:42 +0000661 }
David Goodwin00621ef2009-11-20 23:33:54 +0000662
David Greene5393b252009-12-24 00:14:25 +0000663 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbach2973b572010-01-06 16:48:02 +0000664
David Goodwin00621ef2009-11-20 23:33:54 +0000665 // Check if Reg can be renamed to NewReg.
666 BitVector BV = RenameRegisterMap[Reg];
667 if (!BV.test(NewReg)) {
David Greene5393b252009-12-24 00:14:25 +0000668 DEBUG(dbgs() << "(no rename)");
David Goodwin00621ef2009-11-20 23:33:54 +0000669 goto next_super_reg;
670 }
671
672 // If NewReg is dead and NewReg's most recent def is not before
673 // Regs's kill, it's safe to replace Reg with NewReg. We
674 // must also check all aliases of NewReg, because we can't define a
675 // register when any sub or super is already live.
676 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene5393b252009-12-24 00:14:25 +0000677 DEBUG(dbgs() << "(live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000678 goto next_super_reg;
679 } else {
680 bool found = false;
681 for (const unsigned *Alias = TRI->getAliasSet(NewReg);
682 *Alias; ++Alias) {
683 unsigned AliasReg = *Alias;
Jim Grosbach2973b572010-01-06 16:48:02 +0000684 if (State->IsLive(AliasReg) ||
685 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene5393b252009-12-24 00:14:25 +0000686 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000687 found = true;
688 break;
689 }
690 }
691 if (found)
692 goto next_super_reg;
693 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000694
David Goodwin00621ef2009-11-20 23:33:54 +0000695 // Record that 'Reg' can be renamed to 'NewReg'.
696 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwin34877712009-10-26 19:32:42 +0000697 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000698
David Goodwin00621ef2009-11-20 23:33:54 +0000699 // If we fall-out here, then every register in the group can be
700 // renamed, as recorded in RenameMap.
701 RenameOrder.erase(SuperRC);
702 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene5393b252009-12-24 00:14:25 +0000703 DEBUG(dbgs() << "]\n");
David Goodwin00621ef2009-11-20 23:33:54 +0000704 return true;
705
706 next_super_reg:
David Greene5393b252009-12-24 00:14:25 +0000707 DEBUG(dbgs() << ']');
David Goodwin54097832009-11-05 01:19:35 +0000708 } while (R != EndR);
David Goodwin34877712009-10-26 19:32:42 +0000709
David Greene5393b252009-12-24 00:14:25 +0000710 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000711
712 // No registers are free and available!
713 return false;
714}
715
716/// BreakAntiDependencies - Identifiy anti-dependencies within the
717/// ScheduleDAG and break them by renaming registers.
718///
David Goodwine10deca2009-10-26 22:31:16 +0000719unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman66db3a02010-04-19 23:11:58 +0000720 const std::vector<SUnit>& SUnits,
721 MachineBasicBlock::iterator Begin,
722 MachineBasicBlock::iterator End,
Devang Patele29e8e12011-06-02 21:26:52 +0000723 unsigned InsertPosIndex,
724 DbgValueVector &DbgValues) {
725
Bill Wendling38306d52010-07-15 18:43:09 +0000726 std::vector<unsigned> &KillIndices = State->GetKillIndices();
727 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000728 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000729 RegRefs = State->GetRegRefs();
730
David Goodwin34877712009-10-26 19:32:42 +0000731 // The code below assumes that there is at least one instruction,
732 // so just duck out immediately if the block is empty.
David Goodwin4de099d2009-11-03 20:57:50 +0000733 if (SUnits.empty()) return 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000734
David Goodwin54097832009-11-05 01:19:35 +0000735 // For each regclass the next register to use for renaming.
736 RenameOrderType RenameOrder;
David Goodwin34877712009-10-26 19:32:42 +0000737
738 // ...need a map from MI to SUnit.
Dan Gohman66db3a02010-04-19 23:11:58 +0000739 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwin34877712009-10-26 19:32:42 +0000740 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000741 const SUnit *SU = &SUnits[i];
742 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
743 SU));
David Goodwin34877712009-10-26 19:32:42 +0000744 }
745
David Goodwin87d21b92009-11-13 19:52:48 +0000746 // Track progress along the critical path through the SUnit graph as
747 // we walk the instructions. This is needed for regclasses that only
748 // break critical-path anti-dependencies.
Dan Gohman66db3a02010-04-19 23:11:58 +0000749 const SUnit *CriticalPathSU = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000750 MachineInstr *CriticalPathMI = 0;
751 if (CriticalPathSet.any()) {
752 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000753 const SUnit *SU = &SUnits[i];
Jim Grosbach2973b572010-01-06 16:48:02 +0000754 if (!CriticalPathSU ||
755 ((SU->getDepth() + SU->Latency) >
David Goodwin87d21b92009-11-13 19:52:48 +0000756 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
757 CriticalPathSU = SU;
758 }
759 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000760
David Goodwin87d21b92009-11-13 19:52:48 +0000761 CriticalPathMI = CriticalPathSU->getInstr();
762 }
763
Jim Grosbach2973b572010-01-06 16:48:02 +0000764#ifndef NDEBUG
David Greene5393b252009-12-24 00:14:25 +0000765 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
766 DEBUG(dbgs() << "Available regs:");
David Goodwin557bbe62009-11-20 19:32:48 +0000767 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
768 if (!State->IsLive(Reg))
David Greene5393b252009-12-24 00:14:25 +0000769 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000770 }
David Greene5393b252009-12-24 00:14:25 +0000771 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000772#endif
773
774 // Attempt to break anti-dependence edges. Walk the instructions
775 // from the bottom up, tracking information about liveness as we go
776 // to help determine which registers are available.
777 unsigned Broken = 0;
778 unsigned Count = InsertPosIndex - 1;
779 for (MachineBasicBlock::iterator I = End, E = Begin;
780 I != E; --Count) {
781 MachineInstr *MI = --I;
782
David Greene5393b252009-12-24 00:14:25 +0000783 DEBUG(dbgs() << "Anti: ");
David Goodwin34877712009-10-26 19:32:42 +0000784 DEBUG(MI->dump());
785
786 std::set<unsigned> PassthruRegs;
787 GetPassthruRegs(MI, PassthruRegs);
788
789 // Process the defs in MI...
790 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbach2973b572010-01-06 16:48:02 +0000791
David Goodwin557bbe62009-11-20 19:32:48 +0000792 // The dependence edges that represent anti- and output-
David Goodwin87d21b92009-11-13 19:52:48 +0000793 // dependencies that are candidates for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000794 std::vector<const SDep *> Edges;
795 const SUnit *PathSU = MISUnitMap[MI];
David Goodwin557bbe62009-11-20 19:32:48 +0000796 AntiDepEdges(PathSU, Edges);
David Goodwin87d21b92009-11-13 19:52:48 +0000797
798 // If MI is not on the critical path, then we don't rename
799 // registers in the CriticalPathSet.
800 BitVector *ExcludeRegs = NULL;
801 if (MI == CriticalPathMI) {
802 CriticalPathSU = CriticalPathStep(CriticalPathSU);
803 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000804 } else {
David Goodwin87d21b92009-11-13 19:52:48 +0000805 ExcludeRegs = &CriticalPathSet;
806 }
807
David Goodwin34877712009-10-26 19:32:42 +0000808 // Ignore KILL instructions (they form a group in ScanInstruction
809 // but don't cause any anti-dependence breaking themselves)
Chris Lattner518bb532010-02-09 19:54:29 +0000810 if (!MI->isKill()) {
David Goodwin34877712009-10-26 19:32:42 +0000811 // Attempt to break each anti-dependency...
812 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000813 const SDep *Edge = Edges[i];
David Goodwin34877712009-10-26 19:32:42 +0000814 SUnit *NextSU = Edge->getSUnit();
Jim Grosbach2973b572010-01-06 16:48:02 +0000815
David Goodwin12dd99d2009-11-12 19:08:21 +0000816 if ((Edge->getKind() != SDep::Anti) &&
817 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000818
David Goodwin34877712009-10-26 19:32:42 +0000819 unsigned AntiDepReg = Edge->getReg();
David Greene5393b252009-12-24 00:14:25 +0000820 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwin34877712009-10-26 19:32:42 +0000821 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbach2973b572010-01-06 16:48:02 +0000822
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000823 if (!RegClassInfo.isAllocatable(AntiDepReg)) {
David Goodwin34877712009-10-26 19:32:42 +0000824 // Don't break anti-dependencies on non-allocatable registers.
David Greene5393b252009-12-24 00:14:25 +0000825 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwin34877712009-10-26 19:32:42 +0000826 continue;
David Goodwin87d21b92009-11-13 19:52:48 +0000827 } else if ((ExcludeRegs != NULL) && ExcludeRegs->test(AntiDepReg)) {
828 // Don't break anti-dependencies for critical path registers
829 // if not on the critical path
David Greene5393b252009-12-24 00:14:25 +0000830 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwin87d21b92009-11-13 19:52:48 +0000831 continue;
David Goodwin34877712009-10-26 19:32:42 +0000832 } else if (PassthruRegs.count(AntiDepReg) != 0) {
833 // If the anti-dep register liveness "passes-thru", then
834 // don't try to change it. It will be changed along with
835 // the use if required to break an earlier antidep.
David Greene5393b252009-12-24 00:14:25 +0000836 DEBUG(dbgs() << " (passthru)\n");
David Goodwin34877712009-10-26 19:32:42 +0000837 continue;
838 } else {
839 // No anti-dep breaking for implicit deps
840 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000841 assert(AntiDepOp != NULL &&
842 "Can't find index for defined register operand");
David Goodwin34877712009-10-26 19:32:42 +0000843 if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) {
David Greene5393b252009-12-24 00:14:25 +0000844 DEBUG(dbgs() << " (implicit)\n");
David Goodwin34877712009-10-26 19:32:42 +0000845 continue;
846 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000847
David Goodwin34877712009-10-26 19:32:42 +0000848 // If the SUnit has other dependencies on the SUnit that
849 // it anti-depends on, don't bother breaking the
850 // anti-dependency since those edges would prevent such
851 // units from being scheduled past each other
852 // regardless.
David Goodwin557bbe62009-11-20 19:32:48 +0000853 //
854 // Also, if there are dependencies on other SUnits with the
855 // same register as the anti-dependency, don't attempt to
856 // break it.
Dan Gohman66db3a02010-04-19 23:11:58 +0000857 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin34877712009-10-26 19:32:42 +0000858 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin557bbe62009-11-20 19:32:48 +0000859 if (P->getSUnit() == NextSU ?
860 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
861 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
862 AntiDepReg = 0;
863 break;
864 }
865 }
Dan Gohman66db3a02010-04-19 23:11:58 +0000866 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin557bbe62009-11-20 19:32:48 +0000867 PE = PathSU->Preds.end(); P != PE; ++P) {
868 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
869 (P->getKind() != SDep::Output)) {
David Greene5393b252009-12-24 00:14:25 +0000870 DEBUG(dbgs() << " (real dependency)\n");
David Goodwin34877712009-10-26 19:32:42 +0000871 AntiDepReg = 0;
872 break;
Jim Grosbach2973b572010-01-06 16:48:02 +0000873 } else if ((P->getSUnit() != NextSU) &&
874 (P->getKind() == SDep::Data) &&
David Goodwin557bbe62009-11-20 19:32:48 +0000875 (P->getReg() == AntiDepReg)) {
David Greene5393b252009-12-24 00:14:25 +0000876 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin557bbe62009-11-20 19:32:48 +0000877 AntiDepReg = 0;
878 break;
David Goodwin34877712009-10-26 19:32:42 +0000879 }
880 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000881
David Goodwin34877712009-10-26 19:32:42 +0000882 if (AntiDepReg == 0) continue;
883 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000884
David Goodwin34877712009-10-26 19:32:42 +0000885 assert(AntiDepReg != 0);
886 if (AntiDepReg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000887
David Goodwin34877712009-10-26 19:32:42 +0000888 // Determine AntiDepReg's register group.
David Goodwine10deca2009-10-26 22:31:16 +0000889 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwin34877712009-10-26 19:32:42 +0000890 if (GroupIndex == 0) {
David Greene5393b252009-12-24 00:14:25 +0000891 DEBUG(dbgs() << " (zero group)\n");
David Goodwin34877712009-10-26 19:32:42 +0000892 continue;
893 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000894
David Greene5393b252009-12-24 00:14:25 +0000895 DEBUG(dbgs() << '\n');
Jim Grosbach2973b572010-01-06 16:48:02 +0000896
David Goodwin34877712009-10-26 19:32:42 +0000897 // Look for a suitable register to use to break the anti-dependence.
898 std::map<unsigned, unsigned> RenameMap;
David Goodwin54097832009-11-05 01:19:35 +0000899 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene5393b252009-12-24 00:14:25 +0000900 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwin34877712009-10-26 19:32:42 +0000901 << TRI->getName(AntiDepReg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000902
David Goodwin34877712009-10-26 19:32:42 +0000903 // Handle each group register...
904 for (std::map<unsigned, unsigned>::iterator
905 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
906 unsigned CurrReg = S->first;
907 unsigned NewReg = S->second;
Jim Grosbach2973b572010-01-06 16:48:02 +0000908
909 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
910 TRI->getName(NewReg) << "(" <<
David Goodwin34877712009-10-26 19:32:42 +0000911 RegRefs.count(CurrReg) << " refs)");
Jim Grosbach2973b572010-01-06 16:48:02 +0000912
David Goodwin34877712009-10-26 19:32:42 +0000913 // Update the references to the old register CurrReg to
914 // refer to the new register NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000915 std::pair<std::multimap<unsigned,
916 AggressiveAntiDepState::RegisterReference>::iterator,
David Goodwine10deca2009-10-26 22:31:16 +0000917 std::multimap<unsigned,
Jim Grosbach2973b572010-01-06 16:48:02 +0000918 AggressiveAntiDepState::RegisterReference>::iterator>
David Goodwin34877712009-10-26 19:32:42 +0000919 Range = RegRefs.equal_range(CurrReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000920 for (std::multimap<unsigned,
921 AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000922 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
923 Q->second.Operand->setReg(NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000924 // If the SU for the instruction being updated has debug
925 // information related to the anti-dependency register, make
926 // sure to update that as well.
927 const SUnit *SU = MISUnitMap[Q->second.Operand->getParent()];
Jim Grosbach086723d2010-06-02 15:29:36 +0000928 if (!SU) continue;
Devang Patele29e8e12011-06-02 21:26:52 +0000929 for (DbgValueVector::iterator DVI = DbgValues.begin(),
930 DVE = DbgValues.end(); DVI != DVE; ++DVI)
931 if (DVI->second == Q->second.Operand->getParent())
932 UpdateDbgValue(DVI->first, AntiDepReg, NewReg);
David Goodwin34877712009-10-26 19:32:42 +0000933 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000934
David Goodwin34877712009-10-26 19:32:42 +0000935 // We just went back in time and modified history; the
936 // liveness information for CurrReg is now inconsistent. Set
937 // the state as if it were dead.
David Goodwine10deca2009-10-26 22:31:16 +0000938 State->UnionGroups(NewReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000939 RegRefs.erase(NewReg);
940 DefIndices[NewReg] = DefIndices[CurrReg];
941 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbach2973b572010-01-06 16:48:02 +0000942
David Goodwine10deca2009-10-26 22:31:16 +0000943 State->UnionGroups(CurrReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000944 RegRefs.erase(CurrReg);
945 DefIndices[CurrReg] = KillIndices[CurrReg];
946 KillIndices[CurrReg] = ~0u;
947 assert(((KillIndices[CurrReg] == ~0u) !=
948 (DefIndices[CurrReg] == ~0u)) &&
949 "Kill and Def maps aren't consistent for AntiDepReg!");
950 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000951
David Goodwin34877712009-10-26 19:32:42 +0000952 ++Broken;
David Greene5393b252009-12-24 00:14:25 +0000953 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000954 }
955 }
956 }
957
958 ScanInstruction(MI, Count);
959 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000960
David Goodwin34877712009-10-26 19:32:42 +0000961 return Broken;
962}