blob: 78b600e1b7416154442f6930e09d81267f61d106 [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"
Bill Wendling75a5b712010-07-15 06:05:18 +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) {
Bill Wendlinge0104092010-07-15 06:04:38 +000045 KillIndices.reserve(TargetRegs);
46 DefIndices.reserve(TargetRegs);
David Goodwin34877712009-10-26 19:32:42 +000047
David Goodwin990d2852009-12-09 17:18:22 +000048 const unsigned BBSize = BB->size();
49 for (unsigned i = 0; i < NumTargetRegs; ++i) {
50 // Initialize all registers to be in their own group. Initially we
51 // assign the register to the same-indexed GroupNode.
52 GroupNodeIndices[i] = i;
53 // Initialize the indices to indicate that no registers are live.
54 KillIndices[i] = ~0u;
55 DefIndices[i] = BBSize;
56 }
David Goodwin34877712009-10-26 19:32:42 +000057}
58
David Goodwine10deca2009-10-26 22:31:16 +000059unsigned AggressiveAntiDepState::GetGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000060{
61 unsigned Node = GroupNodeIndices[Reg];
62 while (GroupNodes[Node] != Node)
63 Node = GroupNodes[Node];
64
65 return Node;
66}
67
David Goodwin87d21b92009-11-13 19:52:48 +000068void AggressiveAntiDepState::GetGroupRegs(
69 unsigned Group,
70 std::vector<unsigned> &Regs,
71 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
David Goodwin34877712009-10-26 19:32:42 +000072{
David Goodwin990d2852009-12-09 17:18:22 +000073 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
David Goodwin87d21b92009-11-13 19:52:48 +000074 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
David Goodwin34877712009-10-26 19:32:42 +000075 Regs.push_back(Reg);
76 }
77}
78
David Goodwine10deca2009-10-26 22:31:16 +000079unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2)
David Goodwin34877712009-10-26 19:32:42 +000080{
81 assert(GroupNodes[0] == 0 && "GroupNode 0 not parent!");
82 assert(GroupNodeIndices[0] == 0 && "Reg 0 not in Group 0!");
Jim Grosbach2973b572010-01-06 16:48:02 +000083
David Goodwin34877712009-10-26 19:32:42 +000084 // find group for each register
85 unsigned Group1 = GetGroup(Reg1);
86 unsigned Group2 = GetGroup(Reg2);
Jim Grosbach2973b572010-01-06 16:48:02 +000087
David Goodwin34877712009-10-26 19:32:42 +000088 // if either group is 0, then that must become the parent
89 unsigned Parent = (Group1 == 0) ? Group1 : Group2;
90 unsigned Other = (Parent == Group1) ? Group2 : Group1;
91 GroupNodes.at(Other) = Parent;
92 return Parent;
93}
Jim Grosbach2973b572010-01-06 16:48:02 +000094
David Goodwine10deca2009-10-26 22:31:16 +000095unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +000096{
97 // Create a new GroupNode for Reg. Reg's existing GroupNode must
98 // stay as is because there could be other GroupNodes referring to
99 // it.
100 unsigned idx = GroupNodes.size();
101 GroupNodes.push_back(idx);
102 GroupNodeIndices[Reg] = idx;
103 return idx;
104}
105
David Goodwine10deca2009-10-26 22:31:16 +0000106bool AggressiveAntiDepState::IsLive(unsigned Reg)
David Goodwin34877712009-10-26 19:32:42 +0000107{
108 // KillIndex must be defined and DefIndex not defined for a register
109 // to be live.
110 return((KillIndices[Reg] != ~0u) && (DefIndices[Reg] == ~0u));
111}
112
David Goodwine10deca2009-10-26 22:31:16 +0000113
114
115AggressiveAntiDepBreaker::
David Goodwin0855dee2009-11-10 00:15:47 +0000116AggressiveAntiDepBreaker(MachineFunction& MFi,
Jim Grosbach2973b572010-01-06 16:48:02 +0000117 TargetSubtarget::RegClassVector& CriticalPathRCs) :
David Goodwine10deca2009-10-26 22:31:16 +0000118 AntiDepBreaker(), MF(MFi),
119 MRI(MF.getRegInfo()),
Evan Cheng46df4eb2010-06-16 07:35:02 +0000120 TII(MF.getTarget().getInstrInfo()),
David Goodwine10deca2009-10-26 22:31:16 +0000121 TRI(MF.getTarget().getRegisterInfo()),
122 AllocatableSet(TRI->getAllocatableSet(MF)),
David Goodwin557bbe62009-11-20 19:32:48 +0000123 State(NULL) {
David Goodwin87d21b92009-11-13 19:52:48 +0000124 /* Collect a bitset of all registers that are only broken if they
125 are on the critical path. */
126 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
127 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
128 if (CriticalPathSet.none())
129 CriticalPathSet = CPSet;
130 else
131 CriticalPathSet |= CPSet;
132 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000133
David Greene5393b252009-12-24 00:14:25 +0000134 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000135 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwin87d21b92009-11-13 19:52:48 +0000136 r = CriticalPathSet.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000137 dbgs() << " " << TRI->getName(r));
138 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000139}
140
141AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
142 delete State;
David Goodwine10deca2009-10-26 22:31:16 +0000143}
144
145void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
146 assert(State == NULL);
David Goodwin990d2852009-12-09 17:18:22 +0000147 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine10deca2009-10-26 22:31:16 +0000148
149 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
Bill Wendlinge0104092010-07-15 06:04:38 +0000150 std::vector<unsigned> &KillIndices = State->GetKillIndices();
151 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwine10deca2009-10-26 22:31:16 +0000152
153 // Determine the live-out physregs for this block.
154 if (IsReturnBlock) {
155 // In a return block, examine the function live-out regs.
156 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
157 E = MRI.liveout_end(); I != E; ++I) {
158 unsigned Reg = *I;
159 State->UnionGroups(Reg, 0);
160 KillIndices[Reg] = BB->size();
161 DefIndices[Reg] = ~0u;
162 // Repeat, for all aliases.
163 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
164 unsigned AliasReg = *Alias;
165 State->UnionGroups(AliasReg, 0);
166 KillIndices[AliasReg] = BB->size();
167 DefIndices[AliasReg] = ~0u;
168 }
169 }
David Goodwine10deca2009-10-26 22:31:16 +0000170 }
171
Evan Cheng46df4eb2010-06-16 07:35:02 +0000172 // In a non-return block, examine the live-in regs of all successors.
173 // Note a return block can have successors if the return instruction is
174 // predicated.
175 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
176 SE = BB->succ_end(); SI != SE; ++SI)
177 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
178 E = (*SI)->livein_end(); I != E; ++I) {
179 unsigned Reg = *I;
180 State->UnionGroups(Reg, 0);
181 KillIndices[Reg] = BB->size();
182 DefIndices[Reg] = ~0u;
183 // Repeat, for all aliases.
184 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
185 unsigned AliasReg = *Alias;
186 State->UnionGroups(AliasReg, 0);
187 KillIndices[AliasReg] = BB->size();
188 DefIndices[AliasReg] = ~0u;
189 }
190 }
191
David Goodwine10deca2009-10-26 22:31:16 +0000192 // Mark live-out callee-saved registers. In a return block this is
193 // all callee-saved registers. In non-return this is any
194 // callee-saved register that is not saved in the prolog.
195 const MachineFrameInfo *MFI = MF.getFrameInfo();
196 BitVector Pristine = MFI->getPristineRegs(BB);
197 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
198 unsigned Reg = *I;
199 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
200 State->UnionGroups(Reg, 0);
201 KillIndices[Reg] = BB->size();
202 DefIndices[Reg] = ~0u;
203 // Repeat, for all aliases.
204 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
205 unsigned AliasReg = *Alias;
206 State->UnionGroups(AliasReg, 0);
207 KillIndices[AliasReg] = BB->size();
208 DefIndices[AliasReg] = ~0u;
209 }
210 }
211}
212
213void AggressiveAntiDepBreaker::FinishBlock() {
214 delete State;
215 State = NULL;
David Goodwine10deca2009-10-26 22:31:16 +0000216}
217
218void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000219 unsigned InsertPosIndex) {
David Goodwine10deca2009-10-26 22:31:16 +0000220 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
221
David Goodwin5b3c3082009-10-29 23:30:59 +0000222 std::set<unsigned> PassthruRegs;
223 GetPassthruRegs(MI, PassthruRegs);
224 PrescanInstruction(MI, Count, PassthruRegs);
225 ScanInstruction(MI, Count);
226
David Greene5393b252009-12-24 00:14:25 +0000227 DEBUG(dbgs() << "Observe: ");
David Goodwine10deca2009-10-26 22:31:16 +0000228 DEBUG(MI->dump());
David Greene5393b252009-12-24 00:14:25 +0000229 DEBUG(dbgs() << "\tRegs:");
David Goodwine10deca2009-10-26 22:31:16 +0000230
Bill Wendlinge0104092010-07-15 06:04:38 +0000231 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwin990d2852009-12-09 17:18:22 +0000232 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine10deca2009-10-26 22:31:16 +0000233 // If Reg is current live, then mark that it can't be renamed as
234 // we don't know the extent of its live-range anymore (now that it
235 // has been scheduled). If it is not live but was defined in the
236 // previous schedule region, then set its def index to the most
237 // conservative location (i.e. the beginning of the previous
238 // schedule region).
239 if (State->IsLive(Reg)) {
240 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbach2973b572010-01-06 16:48:02 +0000241 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine10deca2009-10-26 22:31:16 +0000242 State->GetGroup(Reg) << "->g0(region live-out)");
243 State->UnionGroups(Reg, 0);
Jim Grosbach2973b572010-01-06 16:48:02 +0000244 } else if ((DefIndices[Reg] < InsertPosIndex)
245 && (DefIndices[Reg] >= Count)) {
David Goodwine10deca2009-10-26 22:31:16 +0000246 DefIndices[Reg] = Count;
247 }
248 }
David Greene5393b252009-12-24 00:14:25 +0000249 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000250}
251
David Goodwin34877712009-10-26 19:32:42 +0000252bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000253 MachineOperand& MO)
David Goodwin34877712009-10-26 19:32:42 +0000254{
255 if (!MO.isReg() || !MO.isImplicit())
256 return false;
257
258 unsigned Reg = MO.getReg();
259 if (Reg == 0)
260 return false;
261
262 MachineOperand *Op = NULL;
263 if (MO.isDef())
264 Op = MI->findRegisterUseOperand(Reg, true);
265 else
266 Op = MI->findRegisterDefOperand(Reg);
267
268 return((Op != NULL) && Op->isImplicit());
269}
270
271void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
272 std::set<unsigned>& PassthruRegs) {
273 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
274 MachineOperand &MO = MI->getOperand(i);
275 if (!MO.isReg()) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000276 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
David Goodwin34877712009-10-26 19:32:42 +0000277 IsImplicitDefUse(MI, MO)) {
278 const unsigned Reg = MO.getReg();
279 PassthruRegs.insert(Reg);
280 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
281 *Subreg; ++Subreg) {
282 PassthruRegs.insert(*Subreg);
283 }
284 }
285 }
286}
287
David Goodwin557bbe62009-11-20 19:32:48 +0000288/// AntiDepEdges - Return in Edges the anti- and output- dependencies
289/// in SU that we want to consider for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000290static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin557bbe62009-11-20 19:32:48 +0000291 SmallSet<unsigned, 4> RegSet;
Dan Gohman66db3a02010-04-19 23:11:58 +0000292 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin34877712009-10-26 19:32:42 +0000293 P != PE; ++P) {
David Goodwin12dd99d2009-11-12 19:08:21 +0000294 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Goodwin34877712009-10-26 19:32:42 +0000295 unsigned Reg = P->getReg();
David Goodwin557bbe62009-11-20 19:32:48 +0000296 if (RegSet.count(Reg) == 0) {
David Goodwin34877712009-10-26 19:32:42 +0000297 Edges.push_back(&*P);
David Goodwin557bbe62009-11-20 19:32:48 +0000298 RegSet.insert(Reg);
David Goodwin34877712009-10-26 19:32:42 +0000299 }
300 }
301 }
302}
303
David Goodwin87d21b92009-11-13 19:52:48 +0000304/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
305/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000306static const SUnit *CriticalPathStep(const SUnit *SU) {
307 const SDep *Next = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000308 unsigned NextDepth = 0;
309 // Find the predecessor edge with the greatest depth.
310 if (SU != 0) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000311 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin87d21b92009-11-13 19:52:48 +0000312 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000313 const SUnit *PredSU = P->getSUnit();
David Goodwin87d21b92009-11-13 19:52:48 +0000314 unsigned PredLatency = P->getLatency();
315 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
316 // In the case of a latency tie, prefer an anti-dependency edge over
317 // other types of edges.
318 if (NextDepth < PredTotalLatency ||
319 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
320 NextDepth = PredTotalLatency;
321 Next = &*P;
322 }
323 }
324 }
325
326 return (Next) ? Next->getSUnit() : 0;
327}
328
David Goodwin67a8a7b2009-10-29 19:17:04 +0000329void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbach2973b572010-01-06 16:48:02 +0000330 const char *tag,
331 const char *header,
David Goodwin3e72d302009-11-19 23:12:37 +0000332 const char *footer) {
Bill Wendlinge0104092010-07-15 06:04:38 +0000333 std::vector<unsigned> &KillIndices = State->GetKillIndices();
334 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000335 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin67a8a7b2009-10-29 19:17:04 +0000336 RegRefs = State->GetRegRefs();
337
338 if (!State->IsLive(Reg)) {
339 KillIndices[Reg] = KillIdx;
340 DefIndices[Reg] = ~0u;
341 RegRefs.erase(Reg);
342 State->LeaveGroup(Reg);
David Goodwin3e72d302009-11-19 23:12:37 +0000343 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000344 dbgs() << header << TRI->getName(Reg); header = NULL; });
345 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000346 }
347 // Repeat for subregisters.
348 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
349 *Subreg; ++Subreg) {
350 unsigned SubregReg = *Subreg;
351 if (!State->IsLive(SubregReg)) {
352 KillIndices[SubregReg] = KillIdx;
353 DefIndices[SubregReg] = ~0u;
354 RegRefs.erase(SubregReg);
355 State->LeaveGroup(SubregReg);
David Goodwin3e72d302009-11-19 23:12:37 +0000356 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000357 dbgs() << header << TRI->getName(Reg); header = NULL; });
358 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin67a8a7b2009-10-29 19:17:04 +0000359 State->GetGroup(SubregReg) << tag);
360 }
361 }
David Goodwin3e72d302009-11-19 23:12:37 +0000362
David Greene5393b252009-12-24 00:14:25 +0000363 DEBUG(if ((header == NULL) && (footer != NULL)) dbgs() << footer);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000364}
365
Jim Grosbach2973b572010-01-06 16:48:02 +0000366void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
367 unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000368 std::set<unsigned>& PassthruRegs) {
Bill Wendlinge0104092010-07-15 06:04:38 +0000369 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000370 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000371 RegRefs = State->GetRegRefs();
372
David Goodwin67a8a7b2009-10-29 19:17:04 +0000373 // Handle dead defs by simulating a last-use of the register just
374 // after the def. A dead def can occur because the def is truely
375 // dead, or because only a subregister is live at the def. If we
376 // don't do this the dead def will be incorrectly merged into the
377 // previous def.
David Goodwin34877712009-10-26 19:32:42 +0000378 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
379 MachineOperand &MO = MI->getOperand(i);
380 if (!MO.isReg() || !MO.isDef()) continue;
381 unsigned Reg = MO.getReg();
382 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000383
David Goodwin3e72d302009-11-19 23:12:37 +0000384 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwin34877712009-10-26 19:32:42 +0000385 }
386
David Greene5393b252009-12-24 00:14:25 +0000387 DEBUG(dbgs() << "\tDef Groups:");
David Goodwin34877712009-10-26 19:32:42 +0000388 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
389 MachineOperand &MO = MI->getOperand(i);
390 if (!MO.isReg() || !MO.isDef()) continue;
391 unsigned Reg = MO.getReg();
392 if (Reg == 0) continue;
393
Jim Grosbach2973b572010-01-06 16:48:02 +0000394 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000395
David Goodwin67a8a7b2009-10-29 19:17:04 +0000396 // If MI's defs have a special allocation requirement, don't allow
David Goodwin34877712009-10-26 19:32:42 +0000397 // any def registers to be changed. Also assume all registers
398 // defined in a call must not be changed (ABI).
Evan Cheng46df4eb2010-06-16 07:35:02 +0000399 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq() ||
400 TII->isPredicated(MI)) {
David Greene5393b252009-12-24 00:14:25 +0000401 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000402 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000403 }
404
405 // Any aliased that are live at this point are completely or
David Goodwin67a8a7b2009-10-29 19:17:04 +0000406 // partially defined here, so group those aliases with Reg.
David Goodwin34877712009-10-26 19:32:42 +0000407 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
408 unsigned AliasReg = *Alias;
David Goodwine10deca2009-10-26 22:31:16 +0000409 if (State->IsLive(AliasReg)) {
410 State->UnionGroups(Reg, AliasReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000411 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwin34877712009-10-26 19:32:42 +0000412 TRI->getName(AliasReg) << ")");
413 }
414 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000415
David Goodwin34877712009-10-26 19:32:42 +0000416 // Note register reference...
417 const TargetRegisterClass *RC = NULL;
418 if (i < MI->getDesc().getNumOperands())
419 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000420 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000421 RegRefs.insert(std::make_pair(Reg, RR));
422 }
423
David Greene5393b252009-12-24 00:14:25 +0000424 DEBUG(dbgs() << '\n');
David Goodwin67a8a7b2009-10-29 19:17:04 +0000425
426 // Scan the register defs for this instruction and update
427 // live-ranges.
428 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
429 MachineOperand &MO = MI->getOperand(i);
430 if (!MO.isReg() || !MO.isDef()) continue;
431 unsigned Reg = MO.getReg();
432 if (Reg == 0) continue;
David Goodwin3e72d302009-11-19 23:12:37 +0000433 // Ignore KILLs and passthru registers for liveness...
Chris Lattner518bb532010-02-09 19:54:29 +0000434 if (MI->isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwin3e72d302009-11-19 23:12:37 +0000435 continue;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000436
David Goodwin3e72d302009-11-19 23:12:37 +0000437 // Update def for Reg and aliases.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000438 DefIndices[Reg] = Count;
David Goodwin3e72d302009-11-19 23:12:37 +0000439 for (const unsigned *Alias = TRI->getAliasSet(Reg);
440 *Alias; ++Alias) {
441 unsigned AliasReg = *Alias;
442 DefIndices[AliasReg] = Count;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000443 }
444 }
David Goodwin34877712009-10-26 19:32:42 +0000445}
446
447void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000448 unsigned Count) {
David Greene5393b252009-12-24 00:14:25 +0000449 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000450 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000451 RegRefs = State->GetRegRefs();
David Goodwin34877712009-10-26 19:32:42 +0000452
Evan Cheng46df4eb2010-06-16 07:35:02 +0000453 // If MI's uses have special allocation requirement, don't allow
454 // any use registers to be changed. Also assume all registers
455 // used in a call must not be changed (ABI).
456 // FIXME: The issue with predicated instruction is more complex. We are being
457 // conservatively here because the kill markers cannot be trusted after
458 // if-conversion:
459 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
460 // ...
461 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
462 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
463 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
464 //
465 // The first R6 kill is not really a kill since it's killed by a predicated
466 // instruction which may not be executed. The second R6 def may or may not
467 // re-define R6 so it's not safe to change it since the last R6 use cannot be
468 // changed.
469 bool Special = MI->getDesc().isCall() ||
470 MI->getDesc().hasExtraSrcRegAllocReq() ||
471 TII->isPredicated(MI);
472
David Goodwin34877712009-10-26 19:32:42 +0000473 // Scan the register uses for this instruction and update
474 // live-ranges, groups and RegRefs.
475 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
476 MachineOperand &MO = MI->getOperand(i);
477 if (!MO.isReg() || !MO.isUse()) continue;
478 unsigned Reg = MO.getReg();
479 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000480
481 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
482 State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000483
484 // It wasn't previously live but now it is, this is a kill. Forget
485 // the previous live-range information and start a new live-range
486 // for the register.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000487 HandleLastUse(Reg, Count, "(last-use)");
David Goodwin34877712009-10-26 19:32:42 +0000488
Evan Cheng46df4eb2010-06-16 07:35:02 +0000489 if (Special) {
David Greene5393b252009-12-24 00:14:25 +0000490 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000491 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000492 }
493
494 // Note register reference...
495 const TargetRegisterClass *RC = NULL;
496 if (i < MI->getDesc().getNumOperands())
497 RC = MI->getDesc().OpInfo[i].getRegClass(TRI);
David Goodwine10deca2009-10-26 22:31:16 +0000498 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000499 RegRefs.insert(std::make_pair(Reg, RR));
500 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000501
David Greene5393b252009-12-24 00:14:25 +0000502 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000503
504 // Form a group of all defs and uses of a KILL instruction to ensure
505 // that all registers are renamed as a group.
Chris Lattner518bb532010-02-09 19:54:29 +0000506 if (MI->isKill()) {
David Greene5393b252009-12-24 00:14:25 +0000507 DEBUG(dbgs() << "\tKill Group:");
David Goodwin34877712009-10-26 19:32:42 +0000508
509 unsigned FirstReg = 0;
510 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
511 MachineOperand &MO = MI->getOperand(i);
512 if (!MO.isReg()) continue;
513 unsigned Reg = MO.getReg();
514 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000515
David Goodwin34877712009-10-26 19:32:42 +0000516 if (FirstReg != 0) {
David Greene5393b252009-12-24 00:14:25 +0000517 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine10deca2009-10-26 22:31:16 +0000518 State->UnionGroups(FirstReg, Reg);
David Goodwin34877712009-10-26 19:32:42 +0000519 } else {
David Greene5393b252009-12-24 00:14:25 +0000520 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000521 FirstReg = Reg;
522 }
523 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000524
David Greene5393b252009-12-24 00:14:25 +0000525 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000526 }
527}
528
529BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
530 BitVector BV(TRI->getNumRegs(), false);
531 bool first = true;
532
533 // Check all references that need rewriting for Reg. For each, use
534 // the corresponding register class to narrow the set of registers
535 // that are appropriate for renaming.
Jim Grosbach2973b572010-01-06 16:48:02 +0000536 std::pair<std::multimap<unsigned,
David Goodwine10deca2009-10-26 22:31:16 +0000537 AggressiveAntiDepState::RegisterReference>::iterator,
538 std::multimap<unsigned,
539 AggressiveAntiDepState::RegisterReference>::iterator>
540 Range = State->GetRegRefs().equal_range(Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000541 for (std::multimap<unsigned,
542 AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first,
543 QE = Range.second; Q != QE; ++Q) {
David Goodwin34877712009-10-26 19:32:42 +0000544 const TargetRegisterClass *RC = Q->second.RC;
545 if (RC == NULL) continue;
546
547 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
548 if (first) {
549 BV |= RCBV;
550 first = false;
551 } else {
552 BV &= RCBV;
553 }
554
David Greene5393b252009-12-24 00:14:25 +0000555 DEBUG(dbgs() << " " << RC->getName());
David Goodwin34877712009-10-26 19:32:42 +0000556 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000557
David Goodwin34877712009-10-26 19:32:42 +0000558 return BV;
Jim Grosbach2973b572010-01-06 16:48:02 +0000559}
David Goodwin34877712009-10-26 19:32:42 +0000560
561bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin54097832009-11-05 01:19:35 +0000562 unsigned AntiDepGroupIndex,
563 RenameOrderType& RenameOrder,
564 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendlinge0104092010-07-15 06:04:38 +0000565 std::vector<unsigned> &KillIndices = State->GetKillIndices();
566 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000567 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000568 RegRefs = State->GetRegRefs();
569
David Goodwin87d21b92009-11-13 19:52:48 +0000570 // Collect all referenced registers in the same group as
571 // AntiDepReg. These all need to be renamed together if we are to
572 // break the anti-dependence.
David Goodwin34877712009-10-26 19:32:42 +0000573 std::vector<unsigned> Regs;
David Goodwin87d21b92009-11-13 19:52:48 +0000574 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwin34877712009-10-26 19:32:42 +0000575 assert(Regs.size() > 0 && "Empty register group!");
576 if (Regs.size() == 0)
577 return false;
578
579 // Find the "superest" register in the group. At the same time,
580 // collect the BitVector of registers that can be used to rename
581 // each register.
Jim Grosbach2973b572010-01-06 16:48:02 +0000582 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
583 << ":\n");
David Goodwin34877712009-10-26 19:32:42 +0000584 std::map<unsigned, BitVector> RenameRegisterMap;
585 unsigned SuperReg = 0;
586 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
587 unsigned Reg = Regs[i];
588 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
589 SuperReg = Reg;
590
591 // If Reg has any references, then collect possible rename regs
592 if (RegRefs.count(Reg) > 0) {
David Greene5393b252009-12-24 00:14:25 +0000593 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000594
David Goodwin34877712009-10-26 19:32:42 +0000595 BitVector BV = GetRenameRegisters(Reg);
596 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
597
David Greene5393b252009-12-24 00:14:25 +0000598 DEBUG(dbgs() << " ::");
David Goodwin34877712009-10-26 19:32:42 +0000599 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000600 dbgs() << " " << TRI->getName(r));
601 DEBUG(dbgs() << "\n");
David Goodwin34877712009-10-26 19:32:42 +0000602 }
603 }
604
605 // All group registers should be a subreg of SuperReg.
606 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
607 unsigned Reg = Regs[i];
608 if (Reg == SuperReg) continue;
609 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
610 assert(IsSub && "Expecting group subregister");
611 if (!IsSub)
612 return false;
613 }
614
David Goodwin00621ef2009-11-20 23:33:54 +0000615#ifndef NDEBUG
616 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
617 if (DebugDiv > 0) {
618 static int renamecnt = 0;
619 if (renamecnt++ % DebugDiv != DebugMod)
620 return false;
Jim Grosbach2973b572010-01-06 16:48:02 +0000621
David Greene5393b252009-12-24 00:14:25 +0000622 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin00621ef2009-11-20 23:33:54 +0000623 " for debug ***\n";
624 }
625#endif
626
David Goodwin54097832009-11-05 01:19:35 +0000627 // Check each possible rename register for SuperReg in round-robin
628 // order. If that register is available, and the corresponding
629 // registers are available for the other group subregisters, then we
630 // can use those registers to rename.
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000631
632 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
633 // check every use of the register and find the largest register class
634 // that can be used in all of them.
Jim Grosbach2973b572010-01-06 16:48:02 +0000635 const TargetRegisterClass *SuperRC =
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000636 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbach2973b572010-01-06 16:48:02 +0000637
David Goodwin54097832009-11-05 01:19:35 +0000638 const TargetRegisterClass::iterator RB = SuperRC->allocation_order_begin(MF);
639 const TargetRegisterClass::iterator RE = SuperRC->allocation_order_end(MF);
640 if (RB == RE) {
David Greene5393b252009-12-24 00:14:25 +0000641 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin54097832009-11-05 01:19:35 +0000642 return false;
643 }
644
David Greene5393b252009-12-24 00:14:25 +0000645 DEBUG(dbgs() << "\tFind Registers:");
David Goodwin3e72d302009-11-19 23:12:37 +0000646
David Goodwin54097832009-11-05 01:19:35 +0000647 if (RenameOrder.count(SuperRC) == 0)
648 RenameOrder.insert(RenameOrderType::value_type(SuperRC, RE));
649
David Goodwin98f2f1a2009-11-05 01:45:50 +0000650 const TargetRegisterClass::iterator OrigR = RenameOrder[SuperRC];
David Goodwin54097832009-11-05 01:19:35 +0000651 const TargetRegisterClass::iterator EndR = ((OrigR == RE) ? RB : OrigR);
652 TargetRegisterClass::iterator R = OrigR;
653 do {
654 if (R == RB) R = RE;
655 --R;
David Goodwin00621ef2009-11-20 23:33:54 +0000656 const unsigned NewSuperReg = *R;
David Goodwin34877712009-10-26 19:32:42 +0000657 // Don't replace a register with itself.
David Goodwin00621ef2009-11-20 23:33:54 +0000658 if (NewSuperReg == SuperReg) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000659
David Greene5393b252009-12-24 00:14:25 +0000660 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin00621ef2009-11-20 23:33:54 +0000661 RenameMap.clear();
662
663 // For each referenced group register (which must be a SuperReg or
664 // a subregister of SuperReg), find the corresponding subregister
665 // of NewSuperReg and make sure it is free to be renamed.
666 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
667 unsigned Reg = Regs[i];
668 unsigned NewReg = 0;
669 if (Reg == SuperReg) {
670 NewReg = NewSuperReg;
671 } else {
672 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
673 if (NewSubRegIdx != 0)
674 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwin34877712009-10-26 19:32:42 +0000675 }
David Goodwin00621ef2009-11-20 23:33:54 +0000676
David Greene5393b252009-12-24 00:14:25 +0000677 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbach2973b572010-01-06 16:48:02 +0000678
David Goodwin00621ef2009-11-20 23:33:54 +0000679 // Check if Reg can be renamed to NewReg.
680 BitVector BV = RenameRegisterMap[Reg];
681 if (!BV.test(NewReg)) {
David Greene5393b252009-12-24 00:14:25 +0000682 DEBUG(dbgs() << "(no rename)");
David Goodwin00621ef2009-11-20 23:33:54 +0000683 goto next_super_reg;
684 }
685
686 // If NewReg is dead and NewReg's most recent def is not before
687 // Regs's kill, it's safe to replace Reg with NewReg. We
688 // must also check all aliases of NewReg, because we can't define a
689 // register when any sub or super is already live.
690 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene5393b252009-12-24 00:14:25 +0000691 DEBUG(dbgs() << "(live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000692 goto next_super_reg;
693 } else {
694 bool found = false;
695 for (const unsigned *Alias = TRI->getAliasSet(NewReg);
696 *Alias; ++Alias) {
697 unsigned AliasReg = *Alias;
Jim Grosbach2973b572010-01-06 16:48:02 +0000698 if (State->IsLive(AliasReg) ||
699 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene5393b252009-12-24 00:14:25 +0000700 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000701 found = true;
702 break;
703 }
704 }
705 if (found)
706 goto next_super_reg;
707 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000708
David Goodwin00621ef2009-11-20 23:33:54 +0000709 // Record that 'Reg' can be renamed to 'NewReg'.
710 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwin34877712009-10-26 19:32:42 +0000711 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000712
David Goodwin00621ef2009-11-20 23:33:54 +0000713 // If we fall-out here, then every register in the group can be
714 // renamed, as recorded in RenameMap.
715 RenameOrder.erase(SuperRC);
716 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene5393b252009-12-24 00:14:25 +0000717 DEBUG(dbgs() << "]\n");
David Goodwin00621ef2009-11-20 23:33:54 +0000718 return true;
719
720 next_super_reg:
David Greene5393b252009-12-24 00:14:25 +0000721 DEBUG(dbgs() << ']');
David Goodwin54097832009-11-05 01:19:35 +0000722 } while (R != EndR);
David Goodwin34877712009-10-26 19:32:42 +0000723
David Greene5393b252009-12-24 00:14:25 +0000724 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000725
726 // No registers are free and available!
727 return false;
728}
729
730/// BreakAntiDependencies - Identifiy anti-dependencies within the
731/// ScheduleDAG and break them by renaming registers.
732///
David Goodwine10deca2009-10-26 22:31:16 +0000733unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman66db3a02010-04-19 23:11:58 +0000734 const std::vector<SUnit>& SUnits,
735 MachineBasicBlock::iterator Begin,
736 MachineBasicBlock::iterator End,
David Goodwine10deca2009-10-26 22:31:16 +0000737 unsigned InsertPosIndex) {
Bill Wendlinge0104092010-07-15 06:04:38 +0000738 std::vector<unsigned> &KillIndices = State->GetKillIndices();
739 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000740 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000741 RegRefs = State->GetRegRefs();
742
David Goodwin34877712009-10-26 19:32:42 +0000743 // The code below assumes that there is at least one instruction,
744 // so just duck out immediately if the block is empty.
David Goodwin4de099d2009-11-03 20:57:50 +0000745 if (SUnits.empty()) return 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000746
David Goodwin54097832009-11-05 01:19:35 +0000747 // For each regclass the next register to use for renaming.
748 RenameOrderType RenameOrder;
David Goodwin34877712009-10-26 19:32:42 +0000749
750 // ...need a map from MI to SUnit.
Dan Gohman66db3a02010-04-19 23:11:58 +0000751 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwin34877712009-10-26 19:32:42 +0000752 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000753 const SUnit *SU = &SUnits[i];
754 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
755 SU));
David Goodwin34877712009-10-26 19:32:42 +0000756 }
757
David Goodwin87d21b92009-11-13 19:52:48 +0000758 // Track progress along the critical path through the SUnit graph as
759 // we walk the instructions. This is needed for regclasses that only
760 // break critical-path anti-dependencies.
Dan Gohman66db3a02010-04-19 23:11:58 +0000761 const SUnit *CriticalPathSU = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000762 MachineInstr *CriticalPathMI = 0;
763 if (CriticalPathSet.any()) {
764 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000765 const SUnit *SU = &SUnits[i];
Jim Grosbach2973b572010-01-06 16:48:02 +0000766 if (!CriticalPathSU ||
767 ((SU->getDepth() + SU->Latency) >
David Goodwin87d21b92009-11-13 19:52:48 +0000768 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
769 CriticalPathSU = SU;
770 }
771 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000772
David Goodwin87d21b92009-11-13 19:52:48 +0000773 CriticalPathMI = CriticalPathSU->getInstr();
774 }
775
Jim Grosbach2973b572010-01-06 16:48:02 +0000776#ifndef NDEBUG
David Greene5393b252009-12-24 00:14:25 +0000777 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
778 DEBUG(dbgs() << "Available regs:");
David Goodwin557bbe62009-11-20 19:32:48 +0000779 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
780 if (!State->IsLive(Reg))
David Greene5393b252009-12-24 00:14:25 +0000781 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000782 }
David Greene5393b252009-12-24 00:14:25 +0000783 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000784#endif
785
786 // Attempt to break anti-dependence edges. Walk the instructions
787 // from the bottom up, tracking information about liveness as we go
788 // to help determine which registers are available.
789 unsigned Broken = 0;
790 unsigned Count = InsertPosIndex - 1;
791 for (MachineBasicBlock::iterator I = End, E = Begin;
792 I != E; --Count) {
793 MachineInstr *MI = --I;
794
David Greene5393b252009-12-24 00:14:25 +0000795 DEBUG(dbgs() << "Anti: ");
David Goodwin34877712009-10-26 19:32:42 +0000796 DEBUG(MI->dump());
797
798 std::set<unsigned> PassthruRegs;
799 GetPassthruRegs(MI, PassthruRegs);
800
801 // Process the defs in MI...
802 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbach2973b572010-01-06 16:48:02 +0000803
David Goodwin557bbe62009-11-20 19:32:48 +0000804 // The dependence edges that represent anti- and output-
David Goodwin87d21b92009-11-13 19:52:48 +0000805 // dependencies that are candidates for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000806 std::vector<const SDep *> Edges;
807 const SUnit *PathSU = MISUnitMap[MI];
David Goodwin557bbe62009-11-20 19:32:48 +0000808 AntiDepEdges(PathSU, Edges);
David Goodwin87d21b92009-11-13 19:52:48 +0000809
810 // If MI is not on the critical path, then we don't rename
811 // registers in the CriticalPathSet.
812 BitVector *ExcludeRegs = NULL;
813 if (MI == CriticalPathMI) {
814 CriticalPathSU = CriticalPathStep(CriticalPathSU);
815 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000816 } else {
David Goodwin87d21b92009-11-13 19:52:48 +0000817 ExcludeRegs = &CriticalPathSet;
818 }
819
David Goodwin34877712009-10-26 19:32:42 +0000820 // Ignore KILL instructions (they form a group in ScanInstruction
821 // but don't cause any anti-dependence breaking themselves)
Chris Lattner518bb532010-02-09 19:54:29 +0000822 if (!MI->isKill()) {
David Goodwin34877712009-10-26 19:32:42 +0000823 // Attempt to break each anti-dependency...
824 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000825 const SDep *Edge = Edges[i];
David Goodwin34877712009-10-26 19:32:42 +0000826 SUnit *NextSU = Edge->getSUnit();
Jim Grosbach2973b572010-01-06 16:48:02 +0000827
David Goodwin12dd99d2009-11-12 19:08:21 +0000828 if ((Edge->getKind() != SDep::Anti) &&
829 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000830
David Goodwin34877712009-10-26 19:32:42 +0000831 unsigned AntiDepReg = Edge->getReg();
David Greene5393b252009-12-24 00:14:25 +0000832 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwin34877712009-10-26 19:32:42 +0000833 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbach2973b572010-01-06 16:48:02 +0000834
David Goodwin34877712009-10-26 19:32:42 +0000835 if (!AllocatableSet.test(AntiDepReg)) {
836 // Don't break anti-dependencies on non-allocatable registers.
David Greene5393b252009-12-24 00:14:25 +0000837 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwin34877712009-10-26 19:32:42 +0000838 continue;
David Goodwin87d21b92009-11-13 19:52:48 +0000839 } else if ((ExcludeRegs != NULL) && ExcludeRegs->test(AntiDepReg)) {
840 // Don't break anti-dependencies for critical path registers
841 // if not on the critical path
David Greene5393b252009-12-24 00:14:25 +0000842 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwin87d21b92009-11-13 19:52:48 +0000843 continue;
David Goodwin34877712009-10-26 19:32:42 +0000844 } else if (PassthruRegs.count(AntiDepReg) != 0) {
845 // If the anti-dep register liveness "passes-thru", then
846 // don't try to change it. It will be changed along with
847 // the use if required to break an earlier antidep.
David Greene5393b252009-12-24 00:14:25 +0000848 DEBUG(dbgs() << " (passthru)\n");
David Goodwin34877712009-10-26 19:32:42 +0000849 continue;
850 } else {
851 // No anti-dep breaking for implicit deps
852 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000853 assert(AntiDepOp != NULL &&
854 "Can't find index for defined register operand");
David Goodwin34877712009-10-26 19:32:42 +0000855 if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) {
David Greene5393b252009-12-24 00:14:25 +0000856 DEBUG(dbgs() << " (implicit)\n");
David Goodwin34877712009-10-26 19:32:42 +0000857 continue;
858 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000859
David Goodwin34877712009-10-26 19:32:42 +0000860 // If the SUnit has other dependencies on the SUnit that
861 // it anti-depends on, don't bother breaking the
862 // anti-dependency since those edges would prevent such
863 // units from being scheduled past each other
864 // regardless.
David Goodwin557bbe62009-11-20 19:32:48 +0000865 //
866 // Also, if there are dependencies on other SUnits with the
867 // same register as the anti-dependency, don't attempt to
868 // break it.
Dan Gohman66db3a02010-04-19 23:11:58 +0000869 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin34877712009-10-26 19:32:42 +0000870 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin557bbe62009-11-20 19:32:48 +0000871 if (P->getSUnit() == NextSU ?
872 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
873 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
874 AntiDepReg = 0;
875 break;
876 }
877 }
Dan Gohman66db3a02010-04-19 23:11:58 +0000878 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin557bbe62009-11-20 19:32:48 +0000879 PE = PathSU->Preds.end(); P != PE; ++P) {
880 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
881 (P->getKind() != SDep::Output)) {
David Greene5393b252009-12-24 00:14:25 +0000882 DEBUG(dbgs() << " (real dependency)\n");
David Goodwin34877712009-10-26 19:32:42 +0000883 AntiDepReg = 0;
884 break;
Jim Grosbach2973b572010-01-06 16:48:02 +0000885 } else if ((P->getSUnit() != NextSU) &&
886 (P->getKind() == SDep::Data) &&
David Goodwin557bbe62009-11-20 19:32:48 +0000887 (P->getReg() == AntiDepReg)) {
David Greene5393b252009-12-24 00:14:25 +0000888 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin557bbe62009-11-20 19:32:48 +0000889 AntiDepReg = 0;
890 break;
David Goodwin34877712009-10-26 19:32:42 +0000891 }
892 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000893
David Goodwin34877712009-10-26 19:32:42 +0000894 if (AntiDepReg == 0) continue;
895 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000896
David Goodwin34877712009-10-26 19:32:42 +0000897 assert(AntiDepReg != 0);
898 if (AntiDepReg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000899
David Goodwin34877712009-10-26 19:32:42 +0000900 // Determine AntiDepReg's register group.
David Goodwine10deca2009-10-26 22:31:16 +0000901 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwin34877712009-10-26 19:32:42 +0000902 if (GroupIndex == 0) {
David Greene5393b252009-12-24 00:14:25 +0000903 DEBUG(dbgs() << " (zero group)\n");
David Goodwin34877712009-10-26 19:32:42 +0000904 continue;
905 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000906
David Greene5393b252009-12-24 00:14:25 +0000907 DEBUG(dbgs() << '\n');
Jim Grosbach2973b572010-01-06 16:48:02 +0000908
David Goodwin34877712009-10-26 19:32:42 +0000909 // Look for a suitable register to use to break the anti-dependence.
910 std::map<unsigned, unsigned> RenameMap;
David Goodwin54097832009-11-05 01:19:35 +0000911 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene5393b252009-12-24 00:14:25 +0000912 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwin34877712009-10-26 19:32:42 +0000913 << TRI->getName(AntiDepReg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000914
David Goodwin34877712009-10-26 19:32:42 +0000915 // Handle each group register...
916 for (std::map<unsigned, unsigned>::iterator
917 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
918 unsigned CurrReg = S->first;
919 unsigned NewReg = S->second;
Jim Grosbach2973b572010-01-06 16:48:02 +0000920
921 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
922 TRI->getName(NewReg) << "(" <<
David Goodwin34877712009-10-26 19:32:42 +0000923 RegRefs.count(CurrReg) << " refs)");
Jim Grosbach2973b572010-01-06 16:48:02 +0000924
David Goodwin34877712009-10-26 19:32:42 +0000925 // Update the references to the old register CurrReg to
926 // refer to the new register NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000927 std::pair<std::multimap<unsigned,
928 AggressiveAntiDepState::RegisterReference>::iterator,
David Goodwine10deca2009-10-26 22:31:16 +0000929 std::multimap<unsigned,
Jim Grosbach2973b572010-01-06 16:48:02 +0000930 AggressiveAntiDepState::RegisterReference>::iterator>
David Goodwin34877712009-10-26 19:32:42 +0000931 Range = RegRefs.equal_range(CurrReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000932 for (std::multimap<unsigned,
933 AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000934 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
935 Q->second.Operand->setReg(NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000936 // If the SU for the instruction being updated has debug
937 // information related to the anti-dependency register, make
938 // sure to update that as well.
939 const SUnit *SU = MISUnitMap[Q->second.Operand->getParent()];
Jim Grosbach086723d2010-06-02 15:29:36 +0000940 if (!SU) continue;
Jim Grosbach533934e2010-06-01 23:48:44 +0000941 for (unsigned i = 0, e = SU->DbgInstrList.size() ; i < e ; ++i) {
942 MachineInstr *DI = SU->DbgInstrList[i];
943 assert (DI->getNumOperands()==3 && DI->getOperand(0).isReg() &&
944 DI->getOperand(0).getReg()
945 && "Non register dbg_value attached to SUnit!");
946 if (DI->getOperand(0).getReg() == AntiDepReg)
947 DI->getOperand(0).setReg(NewReg);
948 }
David Goodwin34877712009-10-26 19:32:42 +0000949 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000950
David Goodwin34877712009-10-26 19:32:42 +0000951 // We just went back in time and modified history; the
952 // liveness information for CurrReg is now inconsistent. Set
953 // the state as if it were dead.
David Goodwine10deca2009-10-26 22:31:16 +0000954 State->UnionGroups(NewReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000955 RegRefs.erase(NewReg);
956 DefIndices[NewReg] = DefIndices[CurrReg];
957 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbach2973b572010-01-06 16:48:02 +0000958
David Goodwine10deca2009-10-26 22:31:16 +0000959 State->UnionGroups(CurrReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000960 RegRefs.erase(CurrReg);
961 DefIndices[CurrReg] = KillIndices[CurrReg];
962 KillIndices[CurrReg] = ~0u;
963 assert(((KillIndices[CurrReg] == ~0u) !=
964 (DefIndices[CurrReg] == ~0u)) &&
965 "Kill and Def maps aren't consistent for AntiDepReg!");
966 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000967
David Goodwin34877712009-10-26 19:32:42 +0000968 ++Broken;
David Greene5393b252009-12-24 00:14:25 +0000969 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000970 }
971 }
972 }
973
974 ScanInstruction(MI, Count);
975 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000976
David Goodwin34877712009-10-26 19:32:42 +0000977 return Broken;
978}