blob: e0797079c5eb1b51bbbba9034c94da4619cdb2a7 [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"
Andrew Trick15252602012-06-06 20:29:31 +000022#include "llvm/CodeGen/RegisterClassInfo.h"
David Goodwine10deca2009-10-26 22:31:16 +000023#include "llvm/Support/CommandLine.h"
David Goodwin34877712009-10-26 19:32:42 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000028#include "llvm/Target/TargetMachine.h"
29#include "llvm/Target/TargetRegisterInfo.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) :
Bill Wendling9c2a0342010-07-15 19:58:14 +000044 NumTargetRegs(TargetRegs), GroupNodes(TargetRegs, 0),
45 GroupNodeIndices(TargetRegs, 0),
46 KillIndices(TargetRegs, 0),
47 DefIndices(TargetRegs, 0)
48{
David Goodwin990d2852009-12-09 17:18:22 +000049 const unsigned BBSize = BB->size();
50 for (unsigned i = 0; i < NumTargetRegs; ++i) {
51 // Initialize all registers to be in their own group. Initially we
52 // assign the register to the same-indexed GroupNode.
53 GroupNodeIndices[i] = i;
54 // Initialize the indices to indicate that no registers are live.
55 KillIndices[i] = ~0u;
56 DefIndices[i] = BBSize;
57 }
David Goodwin34877712009-10-26 19:32:42 +000058}
59
Bill Wendlinge4a41472010-07-15 19:41:20 +000060unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) {
David Goodwin34877712009-10-26 19:32:42 +000061 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,
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000117 const RegisterClassInfo &RCI,
Evan Cheng5b1b44892011-07-01 21:01:15 +0000118 TargetSubtargetInfo::RegClassVector& CriticalPathRCs) :
David Goodwine10deca2009-10-26 22:31:16 +0000119 AntiDepBreaker(), MF(MFi),
120 MRI(MF.getRegInfo()),
Evan Cheng46df4eb2010-06-16 07:35:02 +0000121 TII(MF.getTarget().getInstrInfo()),
David Goodwine10deca2009-10-26 22:31:16 +0000122 TRI(MF.getTarget().getRegisterInfo()),
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000123 RegClassInfo(RCI),
David Goodwin557bbe62009-11-20 19:32:48 +0000124 State(NULL) {
David Goodwin87d21b92009-11-13 19:52:48 +0000125 /* Collect a bitset of all registers that are only broken if they
126 are on the critical path. */
127 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
128 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
129 if (CriticalPathSet.none())
130 CriticalPathSet = CPSet;
131 else
132 CriticalPathSet |= CPSet;
133 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000134
David Greene5393b252009-12-24 00:14:25 +0000135 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000136 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwin87d21b92009-11-13 19:52:48 +0000137 r = CriticalPathSet.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000138 dbgs() << " " << TRI->getName(r));
139 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000140}
141
142AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
143 delete State;
David Goodwine10deca2009-10-26 22:31:16 +0000144}
145
146void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
147 assert(State == NULL);
David Goodwin990d2852009-12-09 17:18:22 +0000148 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine10deca2009-10-26 22:31:16 +0000149
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000150 bool IsReturnBlock = (!BB->empty() && BB->back().isReturn());
Bill Wendling38306d52010-07-15 18:43:09 +0000151 std::vector<unsigned> &KillIndices = State->GetKillIndices();
152 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwine10deca2009-10-26 22:31:16 +0000153
Jakob Stoklund Olesenb45e4de2013-02-05 18:21:52 +0000154 // Examine the live-in regs of all successors.
Evan Cheng46df4eb2010-06-16 07:35:02 +0000155 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
156 SE = BB->succ_end(); SI != SE; ++SI)
157 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
158 E = (*SI)->livein_end(); I != E; ++I) {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000159 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
160 unsigned Reg = *AI;
Jakob Stoklund Olesen597faa82010-12-14 23:23:15 +0000161 State->UnionGroups(Reg, 0);
162 KillIndices[Reg] = BB->size();
163 DefIndices[Reg] = ~0u;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000164 }
165 }
166
David Goodwine10deca2009-10-26 22:31:16 +0000167 // Mark live-out callee-saved registers. In a return block this is
168 // all callee-saved registers. In non-return this is any
169 // callee-saved register that is not saved in the prolog.
170 const MachineFrameInfo *MFI = MF.getFrameInfo();
171 BitVector Pristine = MFI->getPristineRegs(BB);
Craig Topper015f2282012-03-04 03:33:22 +0000172 for (const uint16_t *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
David Goodwine10deca2009-10-26 22:31:16 +0000173 unsigned Reg = *I;
174 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000175 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
176 unsigned AliasReg = *AI;
David Goodwine10deca2009-10-26 22:31:16 +0000177 State->UnionGroups(AliasReg, 0);
178 KillIndices[AliasReg] = BB->size();
179 DefIndices[AliasReg] = ~0u;
180 }
181 }
182}
183
184void AggressiveAntiDepBreaker::FinishBlock() {
185 delete State;
186 State = NULL;
David Goodwine10deca2009-10-26 22:31:16 +0000187}
188
189void AggressiveAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000190 unsigned InsertPosIndex) {
David Goodwine10deca2009-10-26 22:31:16 +0000191 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
192
David Goodwin5b3c3082009-10-29 23:30:59 +0000193 std::set<unsigned> PassthruRegs;
194 GetPassthruRegs(MI, PassthruRegs);
195 PrescanInstruction(MI, Count, PassthruRegs);
196 ScanInstruction(MI, Count);
197
David Greene5393b252009-12-24 00:14:25 +0000198 DEBUG(dbgs() << "Observe: ");
David Goodwine10deca2009-10-26 22:31:16 +0000199 DEBUG(MI->dump());
David Greene5393b252009-12-24 00:14:25 +0000200 DEBUG(dbgs() << "\tRegs:");
David Goodwine10deca2009-10-26 22:31:16 +0000201
Bill Wendling38306d52010-07-15 18:43:09 +0000202 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwin990d2852009-12-09 17:18:22 +0000203 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine10deca2009-10-26 22:31:16 +0000204 // If Reg is current live, then mark that it can't be renamed as
205 // we don't know the extent of its live-range anymore (now that it
206 // has been scheduled). If it is not live but was defined in the
207 // previous schedule region, then set its def index to the most
208 // conservative location (i.e. the beginning of the previous
209 // schedule region).
210 if (State->IsLive(Reg)) {
211 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbach2973b572010-01-06 16:48:02 +0000212 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine10deca2009-10-26 22:31:16 +0000213 State->GetGroup(Reg) << "->g0(region live-out)");
214 State->UnionGroups(Reg, 0);
Jim Grosbach2973b572010-01-06 16:48:02 +0000215 } else if ((DefIndices[Reg] < InsertPosIndex)
216 && (DefIndices[Reg] >= Count)) {
David Goodwine10deca2009-10-26 22:31:16 +0000217 DefIndices[Reg] = Count;
218 }
219 }
David Greene5393b252009-12-24 00:14:25 +0000220 DEBUG(dbgs() << '\n');
David Goodwine10deca2009-10-26 22:31:16 +0000221}
222
David Goodwin34877712009-10-26 19:32:42 +0000223bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000224 MachineOperand& MO)
David Goodwin34877712009-10-26 19:32:42 +0000225{
226 if (!MO.isReg() || !MO.isImplicit())
227 return false;
228
229 unsigned Reg = MO.getReg();
230 if (Reg == 0)
231 return false;
232
233 MachineOperand *Op = NULL;
234 if (MO.isDef())
235 Op = MI->findRegisterUseOperand(Reg, true);
236 else
237 Op = MI->findRegisterDefOperand(Reg);
238
239 return((Op != NULL) && Op->isImplicit());
240}
241
242void AggressiveAntiDepBreaker::GetPassthruRegs(MachineInstr *MI,
243 std::set<unsigned>& PassthruRegs) {
244 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
245 MachineOperand &MO = MI->getOperand(i);
246 if (!MO.isReg()) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000247 if ((MO.isDef() && MI->isRegTiedToUseOperand(i)) ||
David Goodwin34877712009-10-26 19:32:42 +0000248 IsImplicitDefUse(MI, MO)) {
249 const unsigned Reg = MO.getReg();
Chad Rosier62c320a2013-05-22 23:17:36 +0000250 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
251 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000252 PassthruRegs.insert(*SubRegs);
David Goodwin34877712009-10-26 19:32:42 +0000253 }
254 }
255}
256
David Goodwin557bbe62009-11-20 19:32:48 +0000257/// AntiDepEdges - Return in Edges the anti- and output- dependencies
258/// in SU that we want to consider for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000259static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin557bbe62009-11-20 19:32:48 +0000260 SmallSet<unsigned, 4> RegSet;
Dan Gohman66db3a02010-04-19 23:11:58 +0000261 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin34877712009-10-26 19:32:42 +0000262 P != PE; ++P) {
David Goodwin12dd99d2009-11-12 19:08:21 +0000263 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Goodwin34877712009-10-26 19:32:42 +0000264 unsigned Reg = P->getReg();
David Goodwin557bbe62009-11-20 19:32:48 +0000265 if (RegSet.count(Reg) == 0) {
David Goodwin34877712009-10-26 19:32:42 +0000266 Edges.push_back(&*P);
David Goodwin557bbe62009-11-20 19:32:48 +0000267 RegSet.insert(Reg);
David Goodwin34877712009-10-26 19:32:42 +0000268 }
269 }
270 }
271}
272
David Goodwin87d21b92009-11-13 19:52:48 +0000273/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
274/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000275static const SUnit *CriticalPathStep(const SUnit *SU) {
276 const SDep *Next = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000277 unsigned NextDepth = 0;
278 // Find the predecessor edge with the greatest depth.
279 if (SU != 0) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000280 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin87d21b92009-11-13 19:52:48 +0000281 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000282 const SUnit *PredSU = P->getSUnit();
David Goodwin87d21b92009-11-13 19:52:48 +0000283 unsigned PredLatency = P->getLatency();
284 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
285 // In the case of a latency tie, prefer an anti-dependency edge over
286 // other types of edges.
287 if (NextDepth < PredTotalLatency ||
288 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
289 NextDepth = PredTotalLatency;
290 Next = &*P;
291 }
292 }
293 }
294
295 return (Next) ? Next->getSUnit() : 0;
296}
297
David Goodwin67a8a7b2009-10-29 19:17:04 +0000298void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbach2973b572010-01-06 16:48:02 +0000299 const char *tag,
300 const char *header,
David Goodwin3e72d302009-11-19 23:12:37 +0000301 const char *footer) {
Bill Wendling38306d52010-07-15 18:43:09 +0000302 std::vector<unsigned> &KillIndices = State->GetKillIndices();
303 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000304 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin67a8a7b2009-10-29 19:17:04 +0000305 RegRefs = State->GetRegRefs();
306
307 if (!State->IsLive(Reg)) {
308 KillIndices[Reg] = KillIdx;
309 DefIndices[Reg] = ~0u;
310 RegRefs.erase(Reg);
311 State->LeaveGroup(Reg);
David Goodwin3e72d302009-11-19 23:12:37 +0000312 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000313 dbgs() << header << TRI->getName(Reg); header = NULL; });
314 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000315 }
316 // Repeat for subregisters.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000317 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
318 unsigned SubregReg = *SubRegs;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000319 if (!State->IsLive(SubregReg)) {
320 KillIndices[SubregReg] = KillIdx;
321 DefIndices[SubregReg] = ~0u;
322 RegRefs.erase(SubregReg);
323 State->LeaveGroup(SubregReg);
David Goodwin3e72d302009-11-19 23:12:37 +0000324 DEBUG(if (header != NULL) {
David Greene5393b252009-12-24 00:14:25 +0000325 dbgs() << header << TRI->getName(Reg); header = NULL; });
326 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
David Goodwin67a8a7b2009-10-29 19:17:04 +0000327 State->GetGroup(SubregReg) << tag);
328 }
329 }
David Goodwin3e72d302009-11-19 23:12:37 +0000330
David Greene5393b252009-12-24 00:14:25 +0000331 DEBUG(if ((header == NULL) && (footer != NULL)) dbgs() << footer);
David Goodwin67a8a7b2009-10-29 19:17:04 +0000332}
333
Jim Grosbach2973b572010-01-06 16:48:02 +0000334void AggressiveAntiDepBreaker::PrescanInstruction(MachineInstr *MI,
335 unsigned Count,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000336 std::set<unsigned>& PassthruRegs) {
Bill Wendling38306d52010-07-15 18:43:09 +0000337 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000338 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000339 RegRefs = State->GetRegRefs();
340
David Goodwin67a8a7b2009-10-29 19:17:04 +0000341 // Handle dead defs by simulating a last-use of the register just
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000342 // after the def. A dead def can occur because the def is truly
David Goodwin67a8a7b2009-10-29 19:17:04 +0000343 // dead, or because only a subregister is live at the def. If we
344 // don't do this the dead def will be incorrectly merged into the
345 // previous def.
David Goodwin34877712009-10-26 19:32:42 +0000346 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
347 MachineOperand &MO = MI->getOperand(i);
348 if (!MO.isReg() || !MO.isDef()) continue;
349 unsigned Reg = MO.getReg();
350 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000351
David Goodwin3e72d302009-11-19 23:12:37 +0000352 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwin34877712009-10-26 19:32:42 +0000353 }
354
David Greene5393b252009-12-24 00:14:25 +0000355 DEBUG(dbgs() << "\tDef Groups:");
David Goodwin34877712009-10-26 19:32:42 +0000356 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
357 MachineOperand &MO = MI->getOperand(i);
358 if (!MO.isReg() || !MO.isDef()) continue;
359 unsigned Reg = MO.getReg();
360 if (Reg == 0) continue;
361
Jim Grosbach2973b572010-01-06 16:48:02 +0000362 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000363
David Goodwin67a8a7b2009-10-29 19:17:04 +0000364 // If MI's defs have a special allocation requirement, don't allow
David Goodwin34877712009-10-26 19:32:42 +0000365 // any def registers to be changed. Also assume all registers
366 // defined in a call must not be changed (ABI).
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000367 if (MI->isCall() || MI->hasExtraDefRegAllocReq() ||
Evan Cheng46df4eb2010-06-16 07:35:02 +0000368 TII->isPredicated(MI)) {
David Greene5393b252009-12-24 00:14:25 +0000369 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000370 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000371 }
372
373 // Any aliased that are live at this point are completely or
David Goodwin67a8a7b2009-10-29 19:17:04 +0000374 // partially defined here, so group those aliases with Reg.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000375 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
376 unsigned AliasReg = *AI;
David Goodwine10deca2009-10-26 22:31:16 +0000377 if (State->IsLive(AliasReg)) {
378 State->UnionGroups(Reg, AliasReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000379 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwin34877712009-10-26 19:32:42 +0000380 TRI->getName(AliasReg) << ")");
381 }
382 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000383
David Goodwin34877712009-10-26 19:32:42 +0000384 // Note register reference...
385 const TargetRegisterClass *RC = NULL;
386 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000387 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwine10deca2009-10-26 22:31:16 +0000388 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000389 RegRefs.insert(std::make_pair(Reg, RR));
390 }
391
David Greene5393b252009-12-24 00:14:25 +0000392 DEBUG(dbgs() << '\n');
David Goodwin67a8a7b2009-10-29 19:17:04 +0000393
394 // Scan the register defs for this instruction and update
395 // live-ranges.
396 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
397 MachineOperand &MO = MI->getOperand(i);
398 if (!MO.isReg() || !MO.isDef()) continue;
399 unsigned Reg = MO.getReg();
400 if (Reg == 0) continue;
David Goodwin3e72d302009-11-19 23:12:37 +0000401 // Ignore KILLs and passthru registers for liveness...
Chris Lattner518bb532010-02-09 19:54:29 +0000402 if (MI->isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwin3e72d302009-11-19 23:12:37 +0000403 continue;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000404
David Goodwin3e72d302009-11-19 23:12:37 +0000405 // Update def for Reg and aliases.
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000406 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
407 DefIndices[*AI] = Count;
David Goodwin67a8a7b2009-10-29 19:17:04 +0000408 }
David Goodwin34877712009-10-26 19:32:42 +0000409}
410
411void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr *MI,
Bob Wilson347fa3f2010-04-09 21:38:26 +0000412 unsigned Count) {
David Greene5393b252009-12-24 00:14:25 +0000413 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbach2973b572010-01-06 16:48:02 +0000414 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000415 RegRefs = State->GetRegRefs();
David Goodwin34877712009-10-26 19:32:42 +0000416
Evan Cheng46df4eb2010-06-16 07:35:02 +0000417 // If MI's uses have special allocation requirement, don't allow
418 // any use registers to be changed. Also assume all registers
419 // used in a call must not be changed (ABI).
420 // FIXME: The issue with predicated instruction is more complex. We are being
421 // conservatively here because the kill markers cannot be trusted after
422 // if-conversion:
423 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
424 // ...
425 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
426 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
427 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
428 //
429 // The first R6 kill is not really a kill since it's killed by a predicated
430 // instruction which may not be executed. The second R6 def may or may not
431 // re-define R6 so it's not safe to change it since the last R6 use cannot be
432 // changed.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000433 bool Special = MI->isCall() ||
434 MI->hasExtraSrcRegAllocReq() ||
Evan Cheng46df4eb2010-06-16 07:35:02 +0000435 TII->isPredicated(MI);
436
David Goodwin34877712009-10-26 19:32:42 +0000437 // Scan the register uses for this instruction and update
438 // live-ranges, groups and RegRefs.
439 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
440 MachineOperand &MO = MI->getOperand(i);
441 if (!MO.isReg() || !MO.isUse()) continue;
442 unsigned Reg = MO.getReg();
443 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000444
445 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
446 State->GetGroup(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000447
448 // It wasn't previously live but now it is, this is a kill. Forget
449 // the previous live-range information and start a new live-range
450 // for the register.
David Goodwin67a8a7b2009-10-29 19:17:04 +0000451 HandleLastUse(Reg, Count, "(last-use)");
David Goodwin34877712009-10-26 19:32:42 +0000452
Evan Cheng46df4eb2010-06-16 07:35:02 +0000453 if (Special) {
David Greene5393b252009-12-24 00:14:25 +0000454 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine10deca2009-10-26 22:31:16 +0000455 State->UnionGroups(Reg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000456 }
457
458 // Note register reference...
459 const TargetRegisterClass *RC = NULL;
460 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000461 RC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwine10deca2009-10-26 22:31:16 +0000462 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwin34877712009-10-26 19:32:42 +0000463 RegRefs.insert(std::make_pair(Reg, RR));
464 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000465
David Greene5393b252009-12-24 00:14:25 +0000466 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000467
468 // Form a group of all defs and uses of a KILL instruction to ensure
469 // that all registers are renamed as a group.
Chris Lattner518bb532010-02-09 19:54:29 +0000470 if (MI->isKill()) {
David Greene5393b252009-12-24 00:14:25 +0000471 DEBUG(dbgs() << "\tKill Group:");
David Goodwin34877712009-10-26 19:32:42 +0000472
473 unsigned FirstReg = 0;
474 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
475 MachineOperand &MO = MI->getOperand(i);
476 if (!MO.isReg()) continue;
477 unsigned Reg = MO.getReg();
478 if (Reg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000479
David Goodwin34877712009-10-26 19:32:42 +0000480 if (FirstReg != 0) {
David Greene5393b252009-12-24 00:14:25 +0000481 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine10deca2009-10-26 22:31:16 +0000482 State->UnionGroups(FirstReg, Reg);
David Goodwin34877712009-10-26 19:32:42 +0000483 } else {
David Greene5393b252009-12-24 00:14:25 +0000484 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000485 FirstReg = Reg;
486 }
487 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000488
David Greene5393b252009-12-24 00:14:25 +0000489 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000490 }
491}
492
493BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
494 BitVector BV(TRI->getNumRegs(), false);
495 bool first = true;
496
497 // Check all references that need rewriting for Reg. For each, use
498 // the corresponding register class to narrow the set of registers
499 // that are appropriate for renaming.
Jim Grosbach2973b572010-01-06 16:48:02 +0000500 std::pair<std::multimap<unsigned,
David Goodwine10deca2009-10-26 22:31:16 +0000501 AggressiveAntiDepState::RegisterReference>::iterator,
502 std::multimap<unsigned,
503 AggressiveAntiDepState::RegisterReference>::iterator>
504 Range = State->GetRegRefs().equal_range(Reg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000505 for (std::multimap<unsigned,
506 AggressiveAntiDepState::RegisterReference>::iterator Q = Range.first,
507 QE = Range.second; Q != QE; ++Q) {
David Goodwin34877712009-10-26 19:32:42 +0000508 const TargetRegisterClass *RC = Q->second.RC;
509 if (RC == NULL) continue;
510
511 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
512 if (first) {
513 BV |= RCBV;
514 first = false;
515 } else {
516 BV &= RCBV;
517 }
518
David Greene5393b252009-12-24 00:14:25 +0000519 DEBUG(dbgs() << " " << RC->getName());
David Goodwin34877712009-10-26 19:32:42 +0000520 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000521
David Goodwin34877712009-10-26 19:32:42 +0000522 return BV;
Jim Grosbach2973b572010-01-06 16:48:02 +0000523}
David Goodwin34877712009-10-26 19:32:42 +0000524
525bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin54097832009-11-05 01:19:35 +0000526 unsigned AntiDepGroupIndex,
527 RenameOrderType& RenameOrder,
528 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendling38306d52010-07-15 18:43:09 +0000529 std::vector<unsigned> &KillIndices = State->GetKillIndices();
530 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000531 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000532 RegRefs = State->GetRegRefs();
533
David Goodwin87d21b92009-11-13 19:52:48 +0000534 // Collect all referenced registers in the same group as
535 // AntiDepReg. These all need to be renamed together if we are to
536 // break the anti-dependence.
David Goodwin34877712009-10-26 19:32:42 +0000537 std::vector<unsigned> Regs;
David Goodwin87d21b92009-11-13 19:52:48 +0000538 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwin34877712009-10-26 19:32:42 +0000539 assert(Regs.size() > 0 && "Empty register group!");
540 if (Regs.size() == 0)
541 return false;
542
543 // Find the "superest" register in the group. At the same time,
544 // collect the BitVector of registers that can be used to rename
545 // each register.
Jim Grosbach2973b572010-01-06 16:48:02 +0000546 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
547 << ":\n");
David Goodwin34877712009-10-26 19:32:42 +0000548 std::map<unsigned, BitVector> RenameRegisterMap;
549 unsigned SuperReg = 0;
550 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
551 unsigned Reg = Regs[i];
552 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
553 SuperReg = Reg;
554
555 // If Reg has any references, then collect possible rename regs
556 if (RegRefs.count(Reg) > 0) {
David Greene5393b252009-12-24 00:14:25 +0000557 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000558
David Goodwin34877712009-10-26 19:32:42 +0000559 BitVector BV = GetRenameRegisters(Reg);
560 RenameRegisterMap.insert(std::pair<unsigned, BitVector>(Reg, BV));
561
David Greene5393b252009-12-24 00:14:25 +0000562 DEBUG(dbgs() << " ::");
David Goodwin34877712009-10-26 19:32:42 +0000563 DEBUG(for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
David Greene5393b252009-12-24 00:14:25 +0000564 dbgs() << " " << TRI->getName(r));
565 DEBUG(dbgs() << "\n");
David Goodwin34877712009-10-26 19:32:42 +0000566 }
567 }
568
569 // All group registers should be a subreg of SuperReg.
570 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
571 unsigned Reg = Regs[i];
572 if (Reg == SuperReg) continue;
573 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
574 assert(IsSub && "Expecting group subregister");
575 if (!IsSub)
576 return false;
577 }
578
David Goodwin00621ef2009-11-20 23:33:54 +0000579#ifndef NDEBUG
580 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
581 if (DebugDiv > 0) {
582 static int renamecnt = 0;
583 if (renamecnt++ % DebugDiv != DebugMod)
584 return false;
Jim Grosbach2973b572010-01-06 16:48:02 +0000585
David Greene5393b252009-12-24 00:14:25 +0000586 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin00621ef2009-11-20 23:33:54 +0000587 " for debug ***\n";
588 }
589#endif
590
David Goodwin54097832009-11-05 01:19:35 +0000591 // Check each possible rename register for SuperReg in round-robin
592 // order. If that register is available, and the corresponding
593 // registers are available for the other group subregisters, then we
594 // can use those registers to rename.
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000595
596 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
597 // check every use of the register and find the largest register class
598 // that can be used in all of them.
Jim Grosbach2973b572010-01-06 16:48:02 +0000599 const TargetRegisterClass *SuperRC =
Rafael Espindola7e1b5662010-07-12 02:55:34 +0000600 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbach2973b572010-01-06 16:48:02 +0000601
Jakob Stoklund Olesen39b5c0c2012-11-29 03:34:17 +0000602 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000603 if (Order.empty()) {
David Greene5393b252009-12-24 00:14:25 +0000604 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin54097832009-11-05 01:19:35 +0000605 return false;
606 }
607
David Greene5393b252009-12-24 00:14:25 +0000608 DEBUG(dbgs() << "\tFind Registers:");
David Goodwin3e72d302009-11-19 23:12:37 +0000609
David Goodwin54097832009-11-05 01:19:35 +0000610 if (RenameOrder.count(SuperRC) == 0)
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000611 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
David Goodwin54097832009-11-05 01:19:35 +0000612
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000613 unsigned OrigR = RenameOrder[SuperRC];
614 unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
615 unsigned R = OrigR;
David Goodwin54097832009-11-05 01:19:35 +0000616 do {
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000617 if (R == 0) R = Order.size();
David Goodwin54097832009-11-05 01:19:35 +0000618 --R;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000619 const unsigned NewSuperReg = Order[R];
Jim Grosbach9b041c92010-09-02 17:12:55 +0000620 // Don't consider non-allocatable registers
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +0000621 if (!MRI.isAllocatable(NewSuperReg)) continue;
David Goodwin34877712009-10-26 19:32:42 +0000622 // Don't replace a register with itself.
David Goodwin00621ef2009-11-20 23:33:54 +0000623 if (NewSuperReg == SuperReg) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000624
David Greene5393b252009-12-24 00:14:25 +0000625 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin00621ef2009-11-20 23:33:54 +0000626 RenameMap.clear();
627
628 // For each referenced group register (which must be a SuperReg or
629 // a subregister of SuperReg), find the corresponding subregister
630 // of NewSuperReg and make sure it is free to be renamed.
631 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
632 unsigned Reg = Regs[i];
633 unsigned NewReg = 0;
634 if (Reg == SuperReg) {
635 NewReg = NewSuperReg;
636 } else {
637 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
638 if (NewSubRegIdx != 0)
639 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwin34877712009-10-26 19:32:42 +0000640 }
David Goodwin00621ef2009-11-20 23:33:54 +0000641
David Greene5393b252009-12-24 00:14:25 +0000642 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbach2973b572010-01-06 16:48:02 +0000643
David Goodwin00621ef2009-11-20 23:33:54 +0000644 // Check if Reg can be renamed to NewReg.
645 BitVector BV = RenameRegisterMap[Reg];
646 if (!BV.test(NewReg)) {
David Greene5393b252009-12-24 00:14:25 +0000647 DEBUG(dbgs() << "(no rename)");
David Goodwin00621ef2009-11-20 23:33:54 +0000648 goto next_super_reg;
649 }
650
651 // If NewReg is dead and NewReg's most recent def is not before
652 // Regs's kill, it's safe to replace Reg with NewReg. We
653 // must also check all aliases of NewReg, because we can't define a
654 // register when any sub or super is already live.
655 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene5393b252009-12-24 00:14:25 +0000656 DEBUG(dbgs() << "(live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000657 goto next_super_reg;
658 } else {
659 bool found = false;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000660 for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
661 unsigned AliasReg = *AI;
Jim Grosbach2973b572010-01-06 16:48:02 +0000662 if (State->IsLive(AliasReg) ||
663 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene5393b252009-12-24 00:14:25 +0000664 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin00621ef2009-11-20 23:33:54 +0000665 found = true;
666 break;
667 }
668 }
669 if (found)
670 goto next_super_reg;
671 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000672
David Goodwin00621ef2009-11-20 23:33:54 +0000673 // Record that 'Reg' can be renamed to 'NewReg'.
674 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwin34877712009-10-26 19:32:42 +0000675 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000676
David Goodwin00621ef2009-11-20 23:33:54 +0000677 // If we fall-out here, then every register in the group can be
678 // renamed, as recorded in RenameMap.
679 RenameOrder.erase(SuperRC);
680 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene5393b252009-12-24 00:14:25 +0000681 DEBUG(dbgs() << "]\n");
David Goodwin00621ef2009-11-20 23:33:54 +0000682 return true;
683
684 next_super_reg:
David Greene5393b252009-12-24 00:14:25 +0000685 DEBUG(dbgs() << ']');
David Goodwin54097832009-11-05 01:19:35 +0000686 } while (R != EndR);
David Goodwin34877712009-10-26 19:32:42 +0000687
David Greene5393b252009-12-24 00:14:25 +0000688 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000689
690 // No registers are free and available!
691 return false;
692}
693
694/// BreakAntiDependencies - Identifiy anti-dependencies within the
695/// ScheduleDAG and break them by renaming registers.
696///
David Goodwine10deca2009-10-26 22:31:16 +0000697unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman66db3a02010-04-19 23:11:58 +0000698 const std::vector<SUnit>& SUnits,
699 MachineBasicBlock::iterator Begin,
700 MachineBasicBlock::iterator End,
Devang Patele29e8e12011-06-02 21:26:52 +0000701 unsigned InsertPosIndex,
702 DbgValueVector &DbgValues) {
703
Bill Wendling38306d52010-07-15 18:43:09 +0000704 std::vector<unsigned> &KillIndices = State->GetKillIndices();
705 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbach2973b572010-01-06 16:48:02 +0000706 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine10deca2009-10-26 22:31:16 +0000707 RegRefs = State->GetRegRefs();
708
David Goodwin34877712009-10-26 19:32:42 +0000709 // The code below assumes that there is at least one instruction,
710 // so just duck out immediately if the block is empty.
David Goodwin4de099d2009-11-03 20:57:50 +0000711 if (SUnits.empty()) return 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000712
David Goodwin54097832009-11-05 01:19:35 +0000713 // For each regclass the next register to use for renaming.
714 RenameOrderType RenameOrder;
David Goodwin34877712009-10-26 19:32:42 +0000715
716 // ...need a map from MI to SUnit.
Dan Gohman66db3a02010-04-19 23:11:58 +0000717 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwin34877712009-10-26 19:32:42 +0000718 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000719 const SUnit *SU = &SUnits[i];
720 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
721 SU));
David Goodwin34877712009-10-26 19:32:42 +0000722 }
723
David Goodwin87d21b92009-11-13 19:52:48 +0000724 // Track progress along the critical path through the SUnit graph as
725 // we walk the instructions. This is needed for regclasses that only
726 // break critical-path anti-dependencies.
Dan Gohman66db3a02010-04-19 23:11:58 +0000727 const SUnit *CriticalPathSU = 0;
David Goodwin87d21b92009-11-13 19:52:48 +0000728 MachineInstr *CriticalPathMI = 0;
729 if (CriticalPathSet.any()) {
730 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000731 const SUnit *SU = &SUnits[i];
Jim Grosbach2973b572010-01-06 16:48:02 +0000732 if (!CriticalPathSU ||
733 ((SU->getDepth() + SU->Latency) >
David Goodwin87d21b92009-11-13 19:52:48 +0000734 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
735 CriticalPathSU = SU;
736 }
737 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000738
David Goodwin87d21b92009-11-13 19:52:48 +0000739 CriticalPathMI = CriticalPathSU->getInstr();
740 }
741
Jim Grosbach2973b572010-01-06 16:48:02 +0000742#ifndef NDEBUG
David Greene5393b252009-12-24 00:14:25 +0000743 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
744 DEBUG(dbgs() << "Available regs:");
David Goodwin557bbe62009-11-20 19:32:48 +0000745 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
746 if (!State->IsLive(Reg))
David Greene5393b252009-12-24 00:14:25 +0000747 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin34877712009-10-26 19:32:42 +0000748 }
David Greene5393b252009-12-24 00:14:25 +0000749 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000750#endif
751
752 // Attempt to break anti-dependence edges. Walk the instructions
753 // from the bottom up, tracking information about liveness as we go
754 // to help determine which registers are available.
755 unsigned Broken = 0;
756 unsigned Count = InsertPosIndex - 1;
757 for (MachineBasicBlock::iterator I = End, E = Begin;
758 I != E; --Count) {
759 MachineInstr *MI = --I;
760
Hal Finkel504d1d22012-01-16 22:53:41 +0000761 if (MI->isDebugValue())
762 continue;
763
David Greene5393b252009-12-24 00:14:25 +0000764 DEBUG(dbgs() << "Anti: ");
David Goodwin34877712009-10-26 19:32:42 +0000765 DEBUG(MI->dump());
766
767 std::set<unsigned> PassthruRegs;
768 GetPassthruRegs(MI, PassthruRegs);
769
770 // Process the defs in MI...
771 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbach2973b572010-01-06 16:48:02 +0000772
David Goodwin557bbe62009-11-20 19:32:48 +0000773 // The dependence edges that represent anti- and output-
David Goodwin87d21b92009-11-13 19:52:48 +0000774 // dependencies that are candidates for breaking.
Dan Gohman66db3a02010-04-19 23:11:58 +0000775 std::vector<const SDep *> Edges;
776 const SUnit *PathSU = MISUnitMap[MI];
David Goodwin557bbe62009-11-20 19:32:48 +0000777 AntiDepEdges(PathSU, Edges);
David Goodwin87d21b92009-11-13 19:52:48 +0000778
779 // If MI is not on the critical path, then we don't rename
780 // registers in the CriticalPathSet.
781 BitVector *ExcludeRegs = NULL;
782 if (MI == CriticalPathMI) {
783 CriticalPathSU = CriticalPathStep(CriticalPathSU);
784 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : 0;
Jim Grosbach2973b572010-01-06 16:48:02 +0000785 } else {
David Goodwin87d21b92009-11-13 19:52:48 +0000786 ExcludeRegs = &CriticalPathSet;
787 }
788
David Goodwin34877712009-10-26 19:32:42 +0000789 // Ignore KILL instructions (they form a group in ScanInstruction
790 // but don't cause any anti-dependence breaking themselves)
Chris Lattner518bb532010-02-09 19:54:29 +0000791 if (!MI->isKill()) {
David Goodwin34877712009-10-26 19:32:42 +0000792 // Attempt to break each anti-dependency...
793 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000794 const SDep *Edge = Edges[i];
David Goodwin34877712009-10-26 19:32:42 +0000795 SUnit *NextSU = Edge->getSUnit();
Jim Grosbach2973b572010-01-06 16:48:02 +0000796
David Goodwin12dd99d2009-11-12 19:08:21 +0000797 if ((Edge->getKind() != SDep::Anti) &&
798 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000799
David Goodwin34877712009-10-26 19:32:42 +0000800 unsigned AntiDepReg = Edge->getReg();
David Greene5393b252009-12-24 00:14:25 +0000801 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwin34877712009-10-26 19:32:42 +0000802 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbach2973b572010-01-06 16:48:02 +0000803
Jakob Stoklund Olesen14d1dd92012-10-15 22:41:03 +0000804 if (!MRI.isAllocatable(AntiDepReg)) {
David Goodwin34877712009-10-26 19:32:42 +0000805 // Don't break anti-dependencies on non-allocatable registers.
David Greene5393b252009-12-24 00:14:25 +0000806 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwin34877712009-10-26 19:32:42 +0000807 continue;
David Goodwin87d21b92009-11-13 19:52:48 +0000808 } else if ((ExcludeRegs != NULL) && ExcludeRegs->test(AntiDepReg)) {
809 // Don't break anti-dependencies for critical path registers
810 // if not on the critical path
David Greene5393b252009-12-24 00:14:25 +0000811 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwin87d21b92009-11-13 19:52:48 +0000812 continue;
David Goodwin34877712009-10-26 19:32:42 +0000813 } else if (PassthruRegs.count(AntiDepReg) != 0) {
814 // If the anti-dep register liveness "passes-thru", then
815 // don't try to change it. It will be changed along with
816 // the use if required to break an earlier antidep.
David Greene5393b252009-12-24 00:14:25 +0000817 DEBUG(dbgs() << " (passthru)\n");
David Goodwin34877712009-10-26 19:32:42 +0000818 continue;
819 } else {
820 // No anti-dep breaking for implicit deps
821 MachineOperand *AntiDepOp = MI->findRegisterDefOperand(AntiDepReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000822 assert(AntiDepOp != NULL &&
823 "Can't find index for defined register operand");
David Goodwin34877712009-10-26 19:32:42 +0000824 if ((AntiDepOp == NULL) || AntiDepOp->isImplicit()) {
David Greene5393b252009-12-24 00:14:25 +0000825 DEBUG(dbgs() << " (implicit)\n");
David Goodwin34877712009-10-26 19:32:42 +0000826 continue;
827 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000828
David Goodwin34877712009-10-26 19:32:42 +0000829 // If the SUnit has other dependencies on the SUnit that
830 // it anti-depends on, don't bother breaking the
831 // anti-dependency since those edges would prevent such
832 // units from being scheduled past each other
833 // regardless.
David Goodwin557bbe62009-11-20 19:32:48 +0000834 //
835 // Also, if there are dependencies on other SUnits with the
836 // same register as the anti-dependency, don't attempt to
837 // break it.
Dan Gohman66db3a02010-04-19 23:11:58 +0000838 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin34877712009-10-26 19:32:42 +0000839 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin557bbe62009-11-20 19:32:48 +0000840 if (P->getSUnit() == NextSU ?
841 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
842 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
843 AntiDepReg = 0;
844 break;
845 }
846 }
Dan Gohman66db3a02010-04-19 23:11:58 +0000847 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin557bbe62009-11-20 19:32:48 +0000848 PE = PathSU->Preds.end(); P != PE; ++P) {
849 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
850 (P->getKind() != SDep::Output)) {
David Greene5393b252009-12-24 00:14:25 +0000851 DEBUG(dbgs() << " (real dependency)\n");
David Goodwin34877712009-10-26 19:32:42 +0000852 AntiDepReg = 0;
853 break;
Jim Grosbach2973b572010-01-06 16:48:02 +0000854 } else if ((P->getSUnit() != NextSU) &&
855 (P->getKind() == SDep::Data) &&
David Goodwin557bbe62009-11-20 19:32:48 +0000856 (P->getReg() == AntiDepReg)) {
David Greene5393b252009-12-24 00:14:25 +0000857 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin557bbe62009-11-20 19:32:48 +0000858 AntiDepReg = 0;
859 break;
David Goodwin34877712009-10-26 19:32:42 +0000860 }
861 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000862
David Goodwin34877712009-10-26 19:32:42 +0000863 if (AntiDepReg == 0) continue;
864 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000865
David Goodwin34877712009-10-26 19:32:42 +0000866 assert(AntiDepReg != 0);
867 if (AntiDepReg == 0) continue;
Jim Grosbach2973b572010-01-06 16:48:02 +0000868
David Goodwin34877712009-10-26 19:32:42 +0000869 // Determine AntiDepReg's register group.
David Goodwine10deca2009-10-26 22:31:16 +0000870 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwin34877712009-10-26 19:32:42 +0000871 if (GroupIndex == 0) {
David Greene5393b252009-12-24 00:14:25 +0000872 DEBUG(dbgs() << " (zero group)\n");
David Goodwin34877712009-10-26 19:32:42 +0000873 continue;
874 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000875
David Greene5393b252009-12-24 00:14:25 +0000876 DEBUG(dbgs() << '\n');
Jim Grosbach2973b572010-01-06 16:48:02 +0000877
David Goodwin34877712009-10-26 19:32:42 +0000878 // Look for a suitable register to use to break the anti-dependence.
879 std::map<unsigned, unsigned> RenameMap;
David Goodwin54097832009-11-05 01:19:35 +0000880 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene5393b252009-12-24 00:14:25 +0000881 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwin34877712009-10-26 19:32:42 +0000882 << TRI->getName(AntiDepReg) << ":");
Jim Grosbach2973b572010-01-06 16:48:02 +0000883
David Goodwin34877712009-10-26 19:32:42 +0000884 // Handle each group register...
885 for (std::map<unsigned, unsigned>::iterator
886 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
887 unsigned CurrReg = S->first;
888 unsigned NewReg = S->second;
Jim Grosbach2973b572010-01-06 16:48:02 +0000889
890 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
891 TRI->getName(NewReg) << "(" <<
David Goodwin34877712009-10-26 19:32:42 +0000892 RegRefs.count(CurrReg) << " refs)");
Jim Grosbach2973b572010-01-06 16:48:02 +0000893
David Goodwin34877712009-10-26 19:32:42 +0000894 // Update the references to the old register CurrReg to
895 // refer to the new register NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000896 std::pair<std::multimap<unsigned,
897 AggressiveAntiDepState::RegisterReference>::iterator,
David Goodwine10deca2009-10-26 22:31:16 +0000898 std::multimap<unsigned,
Jim Grosbach2973b572010-01-06 16:48:02 +0000899 AggressiveAntiDepState::RegisterReference>::iterator>
David Goodwin34877712009-10-26 19:32:42 +0000900 Range = RegRefs.equal_range(CurrReg);
Jim Grosbach2973b572010-01-06 16:48:02 +0000901 for (std::multimap<unsigned,
902 AggressiveAntiDepState::RegisterReference>::iterator
David Goodwin34877712009-10-26 19:32:42 +0000903 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
904 Q->second.Operand->setReg(NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000905 // If the SU for the instruction being updated has debug
906 // information related to the anti-dependency register, make
907 // sure to update that as well.
908 const SUnit *SU = MISUnitMap[Q->second.Operand->getParent()];
Jim Grosbach086723d2010-06-02 15:29:36 +0000909 if (!SU) continue;
Devang Patele29e8e12011-06-02 21:26:52 +0000910 for (DbgValueVector::iterator DVI = DbgValues.begin(),
911 DVE = DbgValues.end(); DVI != DVE; ++DVI)
912 if (DVI->second == Q->second.Operand->getParent())
913 UpdateDbgValue(DVI->first, AntiDepReg, NewReg);
David Goodwin34877712009-10-26 19:32:42 +0000914 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000915
David Goodwin34877712009-10-26 19:32:42 +0000916 // We just went back in time and modified history; the
917 // liveness information for CurrReg is now inconsistent. Set
918 // the state as if it were dead.
David Goodwine10deca2009-10-26 22:31:16 +0000919 State->UnionGroups(NewReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000920 RegRefs.erase(NewReg);
921 DefIndices[NewReg] = DefIndices[CurrReg];
922 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbach2973b572010-01-06 16:48:02 +0000923
David Goodwine10deca2009-10-26 22:31:16 +0000924 State->UnionGroups(CurrReg, 0);
David Goodwin34877712009-10-26 19:32:42 +0000925 RegRefs.erase(CurrReg);
926 DefIndices[CurrReg] = KillIndices[CurrReg];
927 KillIndices[CurrReg] = ~0u;
928 assert(((KillIndices[CurrReg] == ~0u) !=
929 (DefIndices[CurrReg] == ~0u)) &&
930 "Kill and Def maps aren't consistent for AntiDepReg!");
931 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000932
David Goodwin34877712009-10-26 19:32:42 +0000933 ++Broken;
David Greene5393b252009-12-24 00:14:25 +0000934 DEBUG(dbgs() << '\n');
David Goodwin34877712009-10-26 19:32:42 +0000935 }
936 }
937 }
938
939 ScanInstruction(MI, Count);
940 }
Jim Grosbach2973b572010-01-06 16:48:02 +0000941
David Goodwin34877712009-10-26 19:32:42 +0000942 return Broken;
943}