blob: bb908618b6794e7442ab7e2e7517852d115ffd35 [file] [log] [blame]
Jim Grosbacheb431da2010-01-06 16:48:02 +00001//===----- AggressiveAntiDepBreaker.cpp - Anti-dep breaker ----------------===//
David Goodwinde11f362009-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 Goodwinde11f362009-10-26 19:32:42 +000017#include "AggressiveAntiDepBreaker.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineInstr.h"
Andrew Trick05ff4662012-06-06 20:29:31 +000021#include "llvm/CodeGen/RegisterClassInfo.h"
David Goodwine056d102009-10-26 22:31:16 +000022#include "llvm/Support/CommandLine.h"
David Goodwinde11f362009-10-26 19:32:42 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Target/TargetRegisterInfo.h"
David Goodwinde11f362009-10-26 19:32:42 +000028using namespace llvm;
29
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030#define DEBUG_TYPE "post-RA-sched"
31
David Goodwindd1c6192009-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 Wilson67dd3a42010-04-09 21:38:26 +000035 cl::desc("Debug control for aggressive anti-dep breaker"),
36 cl::init(0), cl::Hidden);
David Goodwindd1c6192009-11-19 23:12:37 +000037static cl::opt<int>
38DebugMod("agg-antidep-debugmod",
Bob Wilson67dd3a42010-04-09 21:38:26 +000039 cl::desc("Debug control for aggressive anti-dep breaker"),
40 cl::init(0), cl::Hidden);
David Goodwindd1c6192009-11-19 23:12:37 +000041
David Goodwina45fe672009-12-09 17:18:22 +000042AggressiveAntiDepState::AggressiveAntiDepState(const unsigned TargetRegs,
43 MachineBasicBlock *BB) :
Bill Wendling51a9c0a2010-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 Goodwina45fe672009-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 Goodwinde11f362009-10-26 19:32:42 +000058}
59
Bill Wendling5a8d15c2010-07-15 19:41:20 +000060unsigned AggressiveAntiDepState::GetGroup(unsigned Reg) {
David Goodwinde11f362009-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 Goodwinb9fe5d52009-11-13 19:52:48 +000068void AggressiveAntiDepState::GetGroupRegs(
69 unsigned Group,
70 std::vector<unsigned> &Regs,
71 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference> *RegRefs)
David Goodwinde11f362009-10-26 19:32:42 +000072{
David Goodwina45fe672009-12-09 17:18:22 +000073 for (unsigned Reg = 0; Reg != NumTargetRegs; ++Reg) {
David Goodwinb9fe5d52009-11-13 19:52:48 +000074 if ((GetGroup(Reg) == Group) && (RegRefs->count(Reg) > 0))
David Goodwinde11f362009-10-26 19:32:42 +000075 Regs.push_back(Reg);
76 }
77}
78
David Goodwine056d102009-10-26 22:31:16 +000079unsigned AggressiveAntiDepState::UnionGroups(unsigned Reg1, unsigned Reg2)
David Goodwinde11f362009-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 Grosbacheb431da2010-01-06 16:48:02 +000083
David Goodwinde11f362009-10-26 19:32:42 +000084 // find group for each register
85 unsigned Group1 = GetGroup(Reg1);
86 unsigned Group2 = GetGroup(Reg2);
Jim Grosbacheb431da2010-01-06 16:48:02 +000087
David Goodwinde11f362009-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 Grosbacheb431da2010-01-06 16:48:02 +000094
David Goodwine056d102009-10-26 22:31:16 +000095unsigned AggressiveAntiDepState::LeaveGroup(unsigned Reg)
David Goodwinde11f362009-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 Goodwine056d102009-10-26 22:31:16 +0000106bool AggressiveAntiDepState::IsLive(unsigned Reg)
David Goodwinde11f362009-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
Eric Christopherd9134482014-08-04 21:25:23 +0000113AggressiveAntiDepBreaker::AggressiveAntiDepBreaker(
114 MachineFunction &MFi, const RegisterClassInfo &RCI,
115 TargetSubtargetInfo::RegClassVector &CriticalPathRCs)
116 : AntiDepBreaker(), MF(MFi), MRI(MF.getRegInfo()),
Eric Christopherfc6de422014-08-05 02:39:49 +0000117 TII(MF.getSubtarget().getInstrInfo()),
118 TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
119 State(nullptr) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000120 /* Collect a bitset of all registers that are only broken if they
121 are on the critical path. */
122 for (unsigned i = 0, e = CriticalPathRCs.size(); i < e; ++i) {
123 BitVector CPSet = TRI->getAllocatableSet(MF, CriticalPathRCs[i]);
124 if (CriticalPathSet.none())
125 CriticalPathSet = CPSet;
126 else
127 CriticalPathSet |= CPSet;
128 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000129
David Greene75a2efb2009-12-24 00:14:25 +0000130 DEBUG(dbgs() << "AntiDep Critical-Path Registers:");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000131 DEBUG(for (int r = CriticalPathSet.find_first(); r != -1;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000132 r = CriticalPathSet.find_next(r))
David Greene75a2efb2009-12-24 00:14:25 +0000133 dbgs() << " " << TRI->getName(r));
134 DEBUG(dbgs() << '\n');
David Goodwine056d102009-10-26 22:31:16 +0000135}
136
137AggressiveAntiDepBreaker::~AggressiveAntiDepBreaker() {
138 delete State;
David Goodwine056d102009-10-26 22:31:16 +0000139}
140
141void AggressiveAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
Craig Topperc0196b12014-04-14 00:51:57 +0000142 assert(!State);
David Goodwina45fe672009-12-09 17:18:22 +0000143 State = new AggressiveAntiDepState(TRI->getNumRegs(), BB);
David Goodwine056d102009-10-26 22:31:16 +0000144
Matthias Braunc2d4bef2015-09-25 21:25:19 +0000145 bool IsReturnBlock = BB->isReturnBlock();
Bill Wendling030b0282010-07-15 18:43:09 +0000146 std::vector<unsigned> &KillIndices = State->GetKillIndices();
147 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwine056d102009-10-26 22:31:16 +0000148
Jakob Stoklund Olesenc3386792013-02-05 18:21:52 +0000149 // Examine the live-in regs of all successors.
Evan Chengf128bdc2010-06-16 07:35:02 +0000150 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
151 SE = BB->succ_end(); SI != SE; ++SI)
Matthias Braund9da1622015-09-09 18:08:03 +0000152 for (const auto &LI : (*SI)->liveins()) {
153 for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000154 unsigned Reg = *AI;
Jakob Stoklund Olesenbe1c8d32010-12-14 23:23:15 +0000155 State->UnionGroups(Reg, 0);
156 KillIndices[Reg] = BB->size();
157 DefIndices[Reg] = ~0u;
Evan Chengf128bdc2010-06-16 07:35:02 +0000158 }
159 }
160
David Goodwine056d102009-10-26 22:31:16 +0000161 // Mark live-out callee-saved registers. In a return block this is
162 // all callee-saved registers. In non-return this is any
163 // callee-saved register that is not saved in the prolog.
Matthias Braun941a7052016-07-28 18:40:00 +0000164 const MachineFrameInfo &MFI = MF.getFrameInfo();
165 BitVector Pristine = MFI.getPristineRegs(MF);
Craig Topper840beec2014-04-04 05:16:06 +0000166 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
David Goodwine056d102009-10-26 22:31:16 +0000167 unsigned Reg = *I;
168 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000169 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
170 unsigned AliasReg = *AI;
David Goodwine056d102009-10-26 22:31:16 +0000171 State->UnionGroups(AliasReg, 0);
172 KillIndices[AliasReg] = BB->size();
173 DefIndices[AliasReg] = ~0u;
174 }
175 }
176}
177
178void AggressiveAntiDepBreaker::FinishBlock() {
179 delete State;
Craig Topperc0196b12014-04-14 00:51:57 +0000180 State = nullptr;
David Goodwine056d102009-10-26 22:31:16 +0000181}
182
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000183void AggressiveAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000184 unsigned InsertPosIndex) {
David Goodwine056d102009-10-26 22:31:16 +0000185 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
186
David Goodwinfaa76602009-10-29 23:30:59 +0000187 std::set<unsigned> PassthruRegs;
188 GetPassthruRegs(MI, PassthruRegs);
189 PrescanInstruction(MI, Count, PassthruRegs);
190 ScanInstruction(MI, Count);
191
David Greene75a2efb2009-12-24 00:14:25 +0000192 DEBUG(dbgs() << "Observe: ");
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000193 DEBUG(MI.dump());
David Greene75a2efb2009-12-24 00:14:25 +0000194 DEBUG(dbgs() << "\tRegs:");
David Goodwine056d102009-10-26 22:31:16 +0000195
Bill Wendling030b0282010-07-15 18:43:09 +0000196 std::vector<unsigned> &DefIndices = State->GetDefIndices();
David Goodwina45fe672009-12-09 17:18:22 +0000197 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
David Goodwine056d102009-10-26 22:31:16 +0000198 // If Reg is current live, then mark that it can't be renamed as
199 // we don't know the extent of its live-range anymore (now that it
200 // has been scheduled). If it is not live but was defined in the
201 // previous schedule region, then set its def index to the most
202 // conservative location (i.e. the beginning of the previous
203 // schedule region).
204 if (State->IsLive(Reg)) {
205 DEBUG(if (State->GetGroup(Reg) != 0)
Jim Grosbacheb431da2010-01-06 16:48:02 +0000206 dbgs() << " " << TRI->getName(Reg) << "=g" <<
David Goodwine056d102009-10-26 22:31:16 +0000207 State->GetGroup(Reg) << "->g0(region live-out)");
208 State->UnionGroups(Reg, 0);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000209 } else if ((DefIndices[Reg] < InsertPosIndex)
210 && (DefIndices[Reg] >= Count)) {
David Goodwine056d102009-10-26 22:31:16 +0000211 DefIndices[Reg] = Count;
212 }
213 }
David Greene75a2efb2009-12-24 00:14:25 +0000214 DEBUG(dbgs() << '\n');
David Goodwine056d102009-10-26 22:31:16 +0000215}
216
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000217bool AggressiveAntiDepBreaker::IsImplicitDefUse(MachineInstr &MI,
218 MachineOperand &MO) {
David Goodwinde11f362009-10-26 19:32:42 +0000219 if (!MO.isReg() || !MO.isImplicit())
220 return false;
221
222 unsigned Reg = MO.getReg();
223 if (Reg == 0)
224 return false;
225
Chad Rosier47eba052015-10-09 19:48:48 +0000226 MachineOperand *Op = nullptr;
227 if (MO.isDef())
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000228 Op = MI.findRegisterUseOperand(Reg, true);
Chad Rosier47eba052015-10-09 19:48:48 +0000229 else
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000230 Op = MI.findRegisterDefOperand(Reg);
Chad Rosier47eba052015-10-09 19:48:48 +0000231
Craig Topperc0196b12014-04-14 00:51:57 +0000232 return(Op && Op->isImplicit());
David Goodwinde11f362009-10-26 19:32:42 +0000233}
234
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000235void AggressiveAntiDepBreaker::GetPassthruRegs(
236 MachineInstr &MI, std::set<unsigned> &PassthruRegs) {
237 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
238 MachineOperand &MO = MI.getOperand(i);
David Goodwinde11f362009-10-26 19:32:42 +0000239 if (!MO.isReg()) continue;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000240 if ((MO.isDef() && MI.isRegTiedToUseOperand(i)) ||
David Goodwinde11f362009-10-26 19:32:42 +0000241 IsImplicitDefUse(MI, MO)) {
242 const unsigned Reg = MO.getReg();
Chad Rosierabdb1d62013-05-22 23:17:36 +0000243 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
244 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000245 PassthruRegs.insert(*SubRegs);
David Goodwinde11f362009-10-26 19:32:42 +0000246 }
247 }
248}
249
David Goodwin80a03cc2009-11-20 19:32:48 +0000250/// AntiDepEdges - Return in Edges the anti- and output- dependencies
251/// in SU that we want to consider for breaking.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000252static void AntiDepEdges(const SUnit *SU, std::vector<const SDep*>& Edges) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000253 SmallSet<unsigned, 4> RegSet;
Dan Gohman35bc4d42010-04-19 23:11:58 +0000254 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwinde11f362009-10-26 19:32:42 +0000255 P != PE; ++P) {
David Goodwinda83f7d2009-11-12 19:08:21 +0000256 if ((P->getKind() == SDep::Anti) || (P->getKind() == SDep::Output)) {
David Blaikie70573dc2014-11-19 07:49:26 +0000257 if (RegSet.insert(P->getReg()).second)
David Goodwinde11f362009-10-26 19:32:42 +0000258 Edges.push_back(&*P);
David Goodwinde11f362009-10-26 19:32:42 +0000259 }
260 }
261}
262
David Goodwinb9fe5d52009-11-13 19:52:48 +0000263/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
264/// critical path.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000265static const SUnit *CriticalPathStep(const SUnit *SU) {
Craig Topperc0196b12014-04-14 00:51:57 +0000266 const SDep *Next = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000267 unsigned NextDepth = 0;
268 // Find the predecessor edge with the greatest depth.
Craig Topperc0196b12014-04-14 00:51:57 +0000269 if (SU) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000270 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwinb9fe5d52009-11-13 19:52:48 +0000271 P != PE; ++P) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000272 const SUnit *PredSU = P->getSUnit();
David Goodwinb9fe5d52009-11-13 19:52:48 +0000273 unsigned PredLatency = P->getLatency();
274 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
275 // In the case of a latency tie, prefer an anti-dependency edge over
276 // other types of edges.
277 if (NextDepth < PredTotalLatency ||
278 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
279 NextDepth = PredTotalLatency;
280 Next = &*P;
281 }
282 }
283 }
284
Craig Topperc0196b12014-04-14 00:51:57 +0000285 return (Next) ? Next->getSUnit() : nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000286}
287
David Goodwin9f1b2d42009-10-29 19:17:04 +0000288void AggressiveAntiDepBreaker::HandleLastUse(unsigned Reg, unsigned KillIdx,
Jim Grosbacheb431da2010-01-06 16:48:02 +0000289 const char *tag,
290 const char *header,
David Goodwindd1c6192009-11-19 23:12:37 +0000291 const char *footer) {
Bill Wendling030b0282010-07-15 18:43:09 +0000292 std::vector<unsigned> &KillIndices = State->GetKillIndices();
293 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000294 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwin9f1b2d42009-10-29 19:17:04 +0000295 RegRefs = State->GetRegRefs();
296
Hal Finkel34c94d52015-01-28 14:44:14 +0000297 // FIXME: We must leave subregisters of live super registers as live, so that
298 // we don't clear out the register tracking information for subregisters of
299 // super registers we're still tracking (and with which we're unioning
300 // subregister definitions).
301 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
302 if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI)) {
303 DEBUG(if (!header && footer) dbgs() << footer);
304 return;
305 }
306
David Goodwin9f1b2d42009-10-29 19:17:04 +0000307 if (!State->IsLive(Reg)) {
308 KillIndices[Reg] = KillIdx;
309 DefIndices[Reg] = ~0u;
310 RegRefs.erase(Reg);
311 State->LeaveGroup(Reg);
Craig Topperc0196b12014-04-14 00:51:57 +0000312 DEBUG(if (header) {
313 dbgs() << header << TRI->getName(Reg); header = nullptr; });
David Greene75a2efb2009-12-24 00:14:25 +0000314 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << tag);
Chuang-Yu Cheng35c61812016-04-01 02:05:29 +0000315 // Repeat for subregisters. Note that we only do this if the superregister
316 // was not live because otherwise, regardless whether we have an explicit
317 // use of the subregister, the subregister's contents are needed for the
318 // uses of the superregister.
319 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
320 unsigned SubregReg = *SubRegs;
321 if (!State->IsLive(SubregReg)) {
322 KillIndices[SubregReg] = KillIdx;
323 DefIndices[SubregReg] = ~0u;
324 RegRefs.erase(SubregReg);
325 State->LeaveGroup(SubregReg);
326 DEBUG(if (header) {
327 dbgs() << header << TRI->getName(Reg); header = nullptr; });
328 DEBUG(dbgs() << " " << TRI->getName(SubregReg) << "->g" <<
329 State->GetGroup(SubregReg) << tag);
330 }
David Goodwin9f1b2d42009-10-29 19:17:04 +0000331 }
332 }
David Goodwindd1c6192009-11-19 23:12:37 +0000333
Craig Topperc0196b12014-04-14 00:51:57 +0000334 DEBUG(if (!header && footer) dbgs() << footer);
David Goodwin9f1b2d42009-10-29 19:17:04 +0000335}
336
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000337void AggressiveAntiDepBreaker::PrescanInstruction(
338 MachineInstr &MI, unsigned Count, std::set<unsigned> &PassthruRegs) {
Bill Wendling030b0282010-07-15 18:43:09 +0000339 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000340 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000341 RegRefs = State->GetRegRefs();
342
David Goodwin9f1b2d42009-10-29 19:17:04 +0000343 // Handle dead defs by simulating a last-use of the register just
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000344 // after the def. A dead def can occur because the def is truly
David Goodwin9f1b2d42009-10-29 19:17:04 +0000345 // dead, or because only a subregister is live at the def. If we
346 // don't do this the dead def will be incorrectly merged into the
347 // previous def.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000348 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
349 MachineOperand &MO = MI.getOperand(i);
David Goodwinde11f362009-10-26 19:32:42 +0000350 if (!MO.isReg() || !MO.isDef()) continue;
351 unsigned Reg = MO.getReg();
352 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000353
David Goodwindd1c6192009-11-19 23:12:37 +0000354 HandleLastUse(Reg, Count + 1, "", "\tDead Def: ", "\n");
David Goodwinde11f362009-10-26 19:32:42 +0000355 }
356
David Greene75a2efb2009-12-24 00:14:25 +0000357 DEBUG(dbgs() << "\tDef Groups:");
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000358 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
359 MachineOperand &MO = MI.getOperand(i);
David Goodwinde11f362009-10-26 19:32:42 +0000360 if (!MO.isReg() || !MO.isDef()) continue;
361 unsigned Reg = MO.getReg();
362 if (Reg == 0) continue;
363
Jim Grosbacheb431da2010-01-06 16:48:02 +0000364 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" << State->GetGroup(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000365
David Goodwin9f1b2d42009-10-29 19:17:04 +0000366 // If MI's defs have a special allocation requirement, don't allow
David Goodwinde11f362009-10-26 19:32:42 +0000367 // any def registers to be changed. Also assume all registers
Kyle Buttcf6a8bf2015-12-02 18:58:51 +0000368 // defined in a call must not be changed (ABI). Inline assembly may
369 // reference either system calls or the register directly. Skip it until we
370 // can tell user specified registers from compiler-specified.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000371 if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI) ||
372 MI.isInlineAsm()) {
David Greene75a2efb2009-12-24 00:14:25 +0000373 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine056d102009-10-26 22:31:16 +0000374 State->UnionGroups(Reg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000375 }
376
377 // Any aliased that are live at this point are completely or
David Goodwin9f1b2d42009-10-29 19:17:04 +0000378 // partially defined here, so group those aliases with Reg.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000379 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
380 unsigned AliasReg = *AI;
David Goodwine056d102009-10-26 22:31:16 +0000381 if (State->IsLive(AliasReg)) {
382 State->UnionGroups(Reg, AliasReg);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000383 DEBUG(dbgs() << "->g" << State->GetGroup(Reg) << "(via " <<
David Goodwinde11f362009-10-26 19:32:42 +0000384 TRI->getName(AliasReg) << ")");
385 }
386 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000387
David Goodwinde11f362009-10-26 19:32:42 +0000388 // Note register reference...
Craig Topperc0196b12014-04-14 00:51:57 +0000389 const TargetRegisterClass *RC = nullptr;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000390 if (i < MI.getDesc().getNumOperands())
391 RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
David Goodwine056d102009-10-26 22:31:16 +0000392 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwinde11f362009-10-26 19:32:42 +0000393 RegRefs.insert(std::make_pair(Reg, RR));
394 }
395
David Greene75a2efb2009-12-24 00:14:25 +0000396 DEBUG(dbgs() << '\n');
David Goodwin9f1b2d42009-10-29 19:17:04 +0000397
398 // Scan the register defs for this instruction and update
399 // live-ranges.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000400 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
401 MachineOperand &MO = MI.getOperand(i);
David Goodwin9f1b2d42009-10-29 19:17:04 +0000402 if (!MO.isReg() || !MO.isDef()) continue;
403 unsigned Reg = MO.getReg();
404 if (Reg == 0) continue;
David Goodwindd1c6192009-11-19 23:12:37 +0000405 // Ignore KILLs and passthru registers for liveness...
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000406 if (MI.isKill() || (PassthruRegs.count(Reg) != 0))
David Goodwindd1c6192009-11-19 23:12:37 +0000407 continue;
David Goodwin9f1b2d42009-10-29 19:17:04 +0000408
David Goodwindd1c6192009-11-19 23:12:37 +0000409 // Update def for Reg and aliases.
Hal Finkel121caf62014-02-26 20:20:30 +0000410 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
411 // We need to be careful here not to define already-live super registers.
412 // If the super register is already live, then this definition is not
413 // a definition of the whole super register (just a partial insertion
414 // into it). Earlier subregister definitions (which we've not yet visited
415 // because we're iterating bottom-up) need to be linked to the same group
416 // as this definition.
417 if (TRI->isSuperRegister(Reg, *AI) && State->IsLive(*AI))
418 continue;
419
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000420 DefIndices[*AI] = Count;
Hal Finkel121caf62014-02-26 20:20:30 +0000421 }
David Goodwin9f1b2d42009-10-29 19:17:04 +0000422 }
David Goodwinde11f362009-10-26 19:32:42 +0000423}
424
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000425void AggressiveAntiDepBreaker::ScanInstruction(MachineInstr &MI,
Bob Wilson67dd3a42010-04-09 21:38:26 +0000426 unsigned Count) {
David Greene75a2efb2009-12-24 00:14:25 +0000427 DEBUG(dbgs() << "\tUse Groups:");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000428 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000429 RegRefs = State->GetRegRefs();
David Goodwinde11f362009-10-26 19:32:42 +0000430
Evan Chengf128bdc2010-06-16 07:35:02 +0000431 // If MI's uses have special allocation requirement, don't allow
432 // any use registers to be changed. Also assume all registers
433 // used in a call must not be changed (ABI).
Kyle Buttcf6a8bf2015-12-02 18:58:51 +0000434 // Inline Assembly register uses also cannot be safely changed.
Evan Chengf128bdc2010-06-16 07:35:02 +0000435 // FIXME: The issue with predicated instruction is more complex. We are being
436 // conservatively here because the kill markers cannot be trusted after
437 // if-conversion:
438 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
439 // ...
440 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
441 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
442 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
443 //
444 // The first R6 kill is not really a kill since it's killed by a predicated
445 // instruction which may not be executed. The second R6 def may or may not
446 // re-define R6 so it's not safe to change it since the last R6 use cannot be
447 // changed.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000448 bool Special = MI.isCall() || MI.hasExtraSrcRegAllocReq() ||
449 TII->isPredicated(MI) || MI.isInlineAsm();
Evan Chengf128bdc2010-06-16 07:35:02 +0000450
David Goodwinde11f362009-10-26 19:32:42 +0000451 // Scan the register uses for this instruction and update
452 // live-ranges, groups and RegRefs.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000453 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
454 MachineOperand &MO = MI.getOperand(i);
David Goodwinde11f362009-10-26 19:32:42 +0000455 if (!MO.isReg() || !MO.isUse()) continue;
456 unsigned Reg = MO.getReg();
457 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000458
459 DEBUG(dbgs() << " " << TRI->getName(Reg) << "=g" <<
460 State->GetGroup(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000461
462 // It wasn't previously live but now it is, this is a kill. Forget
463 // the previous live-range information and start a new live-range
464 // for the register.
David Goodwin9f1b2d42009-10-29 19:17:04 +0000465 HandleLastUse(Reg, Count, "(last-use)");
David Goodwinde11f362009-10-26 19:32:42 +0000466
Evan Chengf128bdc2010-06-16 07:35:02 +0000467 if (Special) {
David Greene75a2efb2009-12-24 00:14:25 +0000468 DEBUG(if (State->GetGroup(Reg) != 0) dbgs() << "->g0(alloc-req)");
David Goodwine056d102009-10-26 22:31:16 +0000469 State->UnionGroups(Reg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000470 }
471
472 // Note register reference...
Craig Topperc0196b12014-04-14 00:51:57 +0000473 const TargetRegisterClass *RC = nullptr;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000474 if (i < MI.getDesc().getNumOperands())
475 RC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
David Goodwine056d102009-10-26 22:31:16 +0000476 AggressiveAntiDepState::RegisterReference RR = { &MO, RC };
David Goodwinde11f362009-10-26 19:32:42 +0000477 RegRefs.insert(std::make_pair(Reg, RR));
478 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000479
David Greene75a2efb2009-12-24 00:14:25 +0000480 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000481
482 // Form a group of all defs and uses of a KILL instruction to ensure
483 // that all registers are renamed as a group.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000484 if (MI.isKill()) {
David Greene75a2efb2009-12-24 00:14:25 +0000485 DEBUG(dbgs() << "\tKill Group:");
David Goodwinde11f362009-10-26 19:32:42 +0000486
487 unsigned FirstReg = 0;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000488 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
489 MachineOperand &MO = MI.getOperand(i);
David Goodwinde11f362009-10-26 19:32:42 +0000490 if (!MO.isReg()) continue;
491 unsigned Reg = MO.getReg();
492 if (Reg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000493
David Goodwinde11f362009-10-26 19:32:42 +0000494 if (FirstReg != 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000495 DEBUG(dbgs() << "=" << TRI->getName(Reg));
David Goodwine056d102009-10-26 22:31:16 +0000496 State->UnionGroups(FirstReg, Reg);
David Goodwinde11f362009-10-26 19:32:42 +0000497 } else {
David Greene75a2efb2009-12-24 00:14:25 +0000498 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000499 FirstReg = Reg;
500 }
501 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000502
David Greene75a2efb2009-12-24 00:14:25 +0000503 DEBUG(dbgs() << "->g" << State->GetGroup(FirstReg) << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000504 }
505}
506
507BitVector AggressiveAntiDepBreaker::GetRenameRegisters(unsigned Reg) {
508 BitVector BV(TRI->getNumRegs(), false);
509 bool first = true;
510
511 // Check all references that need rewriting for Reg. For each, use
512 // the corresponding register class to narrow the set of registers
513 // that are appropriate for renaming.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000514 for (const auto &Q : make_range(State->GetRegRefs().equal_range(Reg))) {
515 const TargetRegisterClass *RC = Q.second.RC;
Craig Topperc0196b12014-04-14 00:51:57 +0000516 if (!RC) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000517
518 BitVector RCBV = TRI->getAllocatableSet(MF, RC);
519 if (first) {
520 BV |= RCBV;
521 first = false;
522 } else {
523 BV &= RCBV;
524 }
525
Craig Toppercf0444b2014-11-17 05:50:14 +0000526 DEBUG(dbgs() << " " << TRI->getRegClassName(RC));
David Goodwinde11f362009-10-26 19:32:42 +0000527 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000528
David Goodwinde11f362009-10-26 19:32:42 +0000529 return BV;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000530}
David Goodwinde11f362009-10-26 19:32:42 +0000531
532bool AggressiveAntiDepBreaker::FindSuitableFreeRegisters(
David Goodwin7d8878a2009-11-05 01:19:35 +0000533 unsigned AntiDepGroupIndex,
534 RenameOrderType& RenameOrder,
535 std::map<unsigned, unsigned> &RenameMap) {
Bill Wendling030b0282010-07-15 18:43:09 +0000536 std::vector<unsigned> &KillIndices = State->GetKillIndices();
537 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000538 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000539 RegRefs = State->GetRegRefs();
540
David Goodwinb9fe5d52009-11-13 19:52:48 +0000541 // Collect all referenced registers in the same group as
542 // AntiDepReg. These all need to be renamed together if we are to
543 // break the anti-dependence.
David Goodwinde11f362009-10-26 19:32:42 +0000544 std::vector<unsigned> Regs;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000545 State->GetGroupRegs(AntiDepGroupIndex, Regs, &RegRefs);
David Goodwinde11f362009-10-26 19:32:42 +0000546 assert(Regs.size() > 0 && "Empty register group!");
547 if (Regs.size() == 0)
548 return false;
549
550 // Find the "superest" register in the group. At the same time,
551 // collect the BitVector of registers that can be used to rename
552 // each register.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000553 DEBUG(dbgs() << "\tRename Candidates for Group g" << AntiDepGroupIndex
554 << ":\n");
David Goodwinde11f362009-10-26 19:32:42 +0000555 std::map<unsigned, BitVector> RenameRegisterMap;
556 unsigned SuperReg = 0;
557 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
558 unsigned Reg = Regs[i];
559 if ((SuperReg == 0) || TRI->isSuperRegister(SuperReg, Reg))
560 SuperReg = Reg;
561
562 // If Reg has any references, then collect possible rename regs
563 if (RegRefs.count(Reg) > 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000564 DEBUG(dbgs() << "\t\t" << TRI->getName(Reg) << ":");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000565
Benjamin Kramer7f75e942016-02-13 16:39:39 +0000566 BitVector &BV = RenameRegisterMap[Reg];
567 assert(BV.empty());
568 BV = GetRenameRegisters(Reg);
David Goodwinde11f362009-10-26 19:32:42 +0000569
Benjamin Kramer7f75e942016-02-13 16:39:39 +0000570 DEBUG({
571 dbgs() << " ::";
572 for (int r = BV.find_first(); r != -1; r = BV.find_next(r))
573 dbgs() << " " << TRI->getName(r);
574 dbgs() << "\n";
575 });
David Goodwinde11f362009-10-26 19:32:42 +0000576 }
577 }
578
579 // All group registers should be a subreg of SuperReg.
580 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
581 unsigned Reg = Regs[i];
582 if (Reg == SuperReg) continue;
583 bool IsSub = TRI->isSubRegister(SuperReg, Reg);
Will Schmidt44ff8f02014-07-31 19:50:53 +0000584 // FIXME: remove this once PR18663 has been properly fixed. For now,
585 // return a conservative answer:
586 // assert(IsSub && "Expecting group subregister");
David Goodwinde11f362009-10-26 19:32:42 +0000587 if (!IsSub)
588 return false;
589 }
590
David Goodwin5305dc02009-11-20 23:33:54 +0000591#ifndef NDEBUG
592 // If DebugDiv > 0 then only rename (renamecnt % DebugDiv) == DebugMod
593 if (DebugDiv > 0) {
594 static int renamecnt = 0;
595 if (renamecnt++ % DebugDiv != DebugMod)
596 return false;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000597
David Greene75a2efb2009-12-24 00:14:25 +0000598 dbgs() << "*** Performing rename " << TRI->getName(SuperReg) <<
David Goodwin5305dc02009-11-20 23:33:54 +0000599 " for debug ***\n";
600 }
601#endif
602
David Goodwin7d8878a2009-11-05 01:19:35 +0000603 // Check each possible rename register for SuperReg in round-robin
604 // order. If that register is available, and the corresponding
605 // registers are available for the other group subregisters, then we
606 // can use those registers to rename.
Rafael Espindola871c7242010-07-12 02:55:34 +0000607
608 // FIXME: Using getMinimalPhysRegClass is very conservative. We should
609 // check every use of the register and find the largest register class
610 // that can be used in all of them.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000611 const TargetRegisterClass *SuperRC =
Rafael Espindola871c7242010-07-12 02:55:34 +0000612 TRI->getMinimalPhysRegClass(SuperReg, MVT::Other);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000613
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000614 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(SuperRC);
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000615 if (Order.empty()) {
David Greene75a2efb2009-12-24 00:14:25 +0000616 DEBUG(dbgs() << "\tEmpty Super Regclass!!\n");
David Goodwin7d8878a2009-11-05 01:19:35 +0000617 return false;
618 }
619
David Greene75a2efb2009-12-24 00:14:25 +0000620 DEBUG(dbgs() << "\tFind Registers:");
David Goodwindd1c6192009-11-19 23:12:37 +0000621
Benjamin Kramer2c99e412014-10-10 15:32:50 +0000622 RenameOrder.insert(RenameOrderType::value_type(SuperRC, Order.size()));
David Goodwin7d8878a2009-11-05 01:19:35 +0000623
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000624 unsigned OrigR = RenameOrder[SuperRC];
625 unsigned EndR = ((OrigR == Order.size()) ? 0 : OrigR);
626 unsigned R = OrigR;
David Goodwin7d8878a2009-11-05 01:19:35 +0000627 do {
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000628 if (R == 0) R = Order.size();
David Goodwin7d8878a2009-11-05 01:19:35 +0000629 --R;
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000630 const unsigned NewSuperReg = Order[R];
Jim Grosbach944aece2010-09-02 17:12:55 +0000631 // Don't consider non-allocatable registers
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000632 if (!MRI.isAllocatable(NewSuperReg)) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000633 // Don't replace a register with itself.
David Goodwin5305dc02009-11-20 23:33:54 +0000634 if (NewSuperReg == SuperReg) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000635
David Greene75a2efb2009-12-24 00:14:25 +0000636 DEBUG(dbgs() << " [" << TRI->getName(NewSuperReg) << ':');
David Goodwin5305dc02009-11-20 23:33:54 +0000637 RenameMap.clear();
638
639 // For each referenced group register (which must be a SuperReg or
640 // a subregister of SuperReg), find the corresponding subregister
641 // of NewSuperReg and make sure it is free to be renamed.
642 for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
643 unsigned Reg = Regs[i];
644 unsigned NewReg = 0;
645 if (Reg == SuperReg) {
646 NewReg = NewSuperReg;
647 } else {
648 unsigned NewSubRegIdx = TRI->getSubRegIndex(SuperReg, Reg);
649 if (NewSubRegIdx != 0)
650 NewReg = TRI->getSubReg(NewSuperReg, NewSubRegIdx);
David Goodwinde11f362009-10-26 19:32:42 +0000651 }
David Goodwin5305dc02009-11-20 23:33:54 +0000652
David Greene75a2efb2009-12-24 00:14:25 +0000653 DEBUG(dbgs() << " " << TRI->getName(NewReg));
Jim Grosbacheb431da2010-01-06 16:48:02 +0000654
David Goodwin5305dc02009-11-20 23:33:54 +0000655 // Check if Reg can be renamed to NewReg.
Benjamin Kramer7f75e942016-02-13 16:39:39 +0000656 if (!RenameRegisterMap[Reg].test(NewReg)) {
David Greene75a2efb2009-12-24 00:14:25 +0000657 DEBUG(dbgs() << "(no rename)");
David Goodwin5305dc02009-11-20 23:33:54 +0000658 goto next_super_reg;
659 }
660
661 // If NewReg is dead and NewReg's most recent def is not before
662 // Regs's kill, it's safe to replace Reg with NewReg. We
663 // must also check all aliases of NewReg, because we can't define a
664 // register when any sub or super is already live.
665 if (State->IsLive(NewReg) || (KillIndices[Reg] > DefIndices[NewReg])) {
David Greene75a2efb2009-12-24 00:14:25 +0000666 DEBUG(dbgs() << "(live)");
David Goodwin5305dc02009-11-20 23:33:54 +0000667 goto next_super_reg;
668 } else {
669 bool found = false;
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000670 for (MCRegAliasIterator AI(NewReg, TRI, false); AI.isValid(); ++AI) {
671 unsigned AliasReg = *AI;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000672 if (State->IsLive(AliasReg) ||
673 (KillIndices[Reg] > DefIndices[AliasReg])) {
David Greene75a2efb2009-12-24 00:14:25 +0000674 DEBUG(dbgs() << "(alias " << TRI->getName(AliasReg) << " live)");
David Goodwin5305dc02009-11-20 23:33:54 +0000675 found = true;
676 break;
677 }
678 }
679 if (found)
680 goto next_super_reg;
681 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000682
Hal Finkelc8cf2b82014-12-09 01:00:59 +0000683 // We cannot rename 'Reg' to 'NewReg' if one of the uses of 'Reg' also
684 // defines 'NewReg' via an early-clobber operand.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000685 for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
686 MachineInstr *UseMI = Q.second.Operand->getParent();
Hal Finkelc8cf2b82014-12-09 01:00:59 +0000687 int Idx = UseMI->findRegisterDefOperandIdx(NewReg, false, true, TRI);
688 if (Idx == -1)
689 continue;
690
691 if (UseMI->getOperand(Idx).isEarlyClobber()) {
692 DEBUG(dbgs() << "(ec)");
693 goto next_super_reg;
694 }
695 }
696
Hal Finkele0a28e52015-08-31 07:51:36 +0000697 // Also, we cannot rename 'Reg' to 'NewReg' if the instruction defining
698 // 'Reg' is an early-clobber define and that instruction also uses
699 // 'NewReg'.
700 for (const auto &Q : make_range(RegRefs.equal_range(Reg))) {
701 if (!Q.second.Operand->isDef() || !Q.second.Operand->isEarlyClobber())
702 continue;
703
704 MachineInstr *DefMI = Q.second.Operand->getParent();
705 if (DefMI->readsRegister(NewReg, TRI)) {
706 DEBUG(dbgs() << "(ec)");
707 goto next_super_reg;
708 }
709 }
710
David Goodwin5305dc02009-11-20 23:33:54 +0000711 // Record that 'Reg' can be renamed to 'NewReg'.
712 RenameMap.insert(std::pair<unsigned, unsigned>(Reg, NewReg));
David Goodwinde11f362009-10-26 19:32:42 +0000713 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000714
David Goodwin5305dc02009-11-20 23:33:54 +0000715 // If we fall-out here, then every register in the group can be
716 // renamed, as recorded in RenameMap.
717 RenameOrder.erase(SuperRC);
718 RenameOrder.insert(RenameOrderType::value_type(SuperRC, R));
David Greene75a2efb2009-12-24 00:14:25 +0000719 DEBUG(dbgs() << "]\n");
David Goodwin5305dc02009-11-20 23:33:54 +0000720 return true;
721
722 next_super_reg:
David Greene75a2efb2009-12-24 00:14:25 +0000723 DEBUG(dbgs() << ']');
David Goodwin7d8878a2009-11-05 01:19:35 +0000724 } while (R != EndR);
David Goodwinde11f362009-10-26 19:32:42 +0000725
David Greene75a2efb2009-12-24 00:14:25 +0000726 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000727
728 // No registers are free and available!
729 return false;
730}
731
732/// BreakAntiDependencies - Identifiy anti-dependencies within the
733/// ScheduleDAG and break them by renaming registers.
734///
David Goodwine056d102009-10-26 22:31:16 +0000735unsigned AggressiveAntiDepBreaker::BreakAntiDependencies(
Dan Gohman35bc4d42010-04-19 23:11:58 +0000736 const std::vector<SUnit>& SUnits,
737 MachineBasicBlock::iterator Begin,
738 MachineBasicBlock::iterator End,
Devang Patelf02a3762011-06-02 21:26:52 +0000739 unsigned InsertPosIndex,
740 DbgValueVector &DbgValues) {
741
Bill Wendling030b0282010-07-15 18:43:09 +0000742 std::vector<unsigned> &KillIndices = State->GetKillIndices();
743 std::vector<unsigned> &DefIndices = State->GetDefIndices();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000744 std::multimap<unsigned, AggressiveAntiDepState::RegisterReference>&
David Goodwine056d102009-10-26 22:31:16 +0000745 RegRefs = State->GetRegRefs();
746
David Goodwinde11f362009-10-26 19:32:42 +0000747 // The code below assumes that there is at least one instruction,
748 // so just duck out immediately if the block is empty.
David Goodwin8501dbbe2009-11-03 20:57:50 +0000749 if (SUnits.empty()) return 0;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000750
David Goodwin7d8878a2009-11-05 01:19:35 +0000751 // For each regclass the next register to use for renaming.
752 RenameOrderType RenameOrder;
David Goodwinde11f362009-10-26 19:32:42 +0000753
754 // ...need a map from MI to SUnit.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000755 std::map<MachineInstr *, const SUnit *> MISUnitMap;
David Goodwinde11f362009-10-26 19:32:42 +0000756 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000757 const SUnit *SU = &SUnits[i];
758 MISUnitMap.insert(std::pair<MachineInstr *, const SUnit *>(SU->getInstr(),
759 SU));
David Goodwinde11f362009-10-26 19:32:42 +0000760 }
761
David Goodwinb9fe5d52009-11-13 19:52:48 +0000762 // Track progress along the critical path through the SUnit graph as
763 // we walk the instructions. This is needed for regclasses that only
764 // break critical-path anti-dependencies.
Craig Topperc0196b12014-04-14 00:51:57 +0000765 const SUnit *CriticalPathSU = nullptr;
766 MachineInstr *CriticalPathMI = nullptr;
David Goodwinb9fe5d52009-11-13 19:52:48 +0000767 if (CriticalPathSet.any()) {
768 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000769 const SUnit *SU = &SUnits[i];
Jim Grosbacheb431da2010-01-06 16:48:02 +0000770 if (!CriticalPathSU ||
771 ((SU->getDepth() + SU->Latency) >
David Goodwinb9fe5d52009-11-13 19:52:48 +0000772 (CriticalPathSU->getDepth() + CriticalPathSU->Latency))) {
773 CriticalPathSU = SU;
774 }
775 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000776
David Goodwinb9fe5d52009-11-13 19:52:48 +0000777 CriticalPathMI = CriticalPathSU->getInstr();
778 }
779
Jim Grosbacheb431da2010-01-06 16:48:02 +0000780#ifndef NDEBUG
David Greene75a2efb2009-12-24 00:14:25 +0000781 DEBUG(dbgs() << "\n===== Aggressive anti-dependency breaking\n");
782 DEBUG(dbgs() << "Available regs:");
David Goodwin80a03cc2009-11-20 19:32:48 +0000783 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
784 if (!State->IsLive(Reg))
David Greene75a2efb2009-12-24 00:14:25 +0000785 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwinde11f362009-10-26 19:32:42 +0000786 }
David Greene75a2efb2009-12-24 00:14:25 +0000787 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000788#endif
789
Krzysztof Parzyszek143f6842016-05-26 18:22:53 +0000790 BitVector RegAliases(TRI->getNumRegs());
791
David Goodwinde11f362009-10-26 19:32:42 +0000792 // Attempt to break anti-dependence edges. Walk the instructions
793 // from the bottom up, tracking information about liveness as we go
794 // to help determine which registers are available.
795 unsigned Broken = 0;
796 unsigned Count = InsertPosIndex - 1;
797 for (MachineBasicBlock::iterator I = End, E = Begin;
798 I != E; --Count) {
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000799 MachineInstr &MI = *--I;
David Goodwinde11f362009-10-26 19:32:42 +0000800
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000801 if (MI.isDebugValue())
Hal Finkel8606e3c2012-01-16 22:53:41 +0000802 continue;
803
David Greene75a2efb2009-12-24 00:14:25 +0000804 DEBUG(dbgs() << "Anti: ");
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000805 DEBUG(MI.dump());
David Goodwinde11f362009-10-26 19:32:42 +0000806
807 std::set<unsigned> PassthruRegs;
808 GetPassthruRegs(MI, PassthruRegs);
809
810 // Process the defs in MI...
811 PrescanInstruction(MI, Count, PassthruRegs);
Jim Grosbacheb431da2010-01-06 16:48:02 +0000812
David Goodwin80a03cc2009-11-20 19:32:48 +0000813 // The dependence edges that represent anti- and output-
David Goodwinb9fe5d52009-11-13 19:52:48 +0000814 // dependencies that are candidates for breaking.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000815 std::vector<const SDep *> Edges;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000816 const SUnit *PathSU = MISUnitMap[&MI];
David Goodwin80a03cc2009-11-20 19:32:48 +0000817 AntiDepEdges(PathSU, Edges);
David Goodwinb9fe5d52009-11-13 19:52:48 +0000818
819 // If MI is not on the critical path, then we don't rename
820 // registers in the CriticalPathSet.
Craig Topperc0196b12014-04-14 00:51:57 +0000821 BitVector *ExcludeRegs = nullptr;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000822 if (&MI == CriticalPathMI) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000823 CriticalPathSU = CriticalPathStep(CriticalPathSU);
Craig Topperc0196b12014-04-14 00:51:57 +0000824 CriticalPathMI = (CriticalPathSU) ? CriticalPathSU->getInstr() : nullptr;
Hal Finkel6f1ff8e2013-09-12 04:22:31 +0000825 } else if (CriticalPathSet.any()) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000826 ExcludeRegs = &CriticalPathSet;
827 }
828
David Goodwinde11f362009-10-26 19:32:42 +0000829 // Ignore KILL instructions (they form a group in ScanInstruction
830 // but don't cause any anti-dependence breaking themselves)
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000831 if (!MI.isKill()) {
David Goodwinde11f362009-10-26 19:32:42 +0000832 // Attempt to break each anti-dependency...
833 for (unsigned i = 0, e = Edges.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000834 const SDep *Edge = Edges[i];
David Goodwinde11f362009-10-26 19:32:42 +0000835 SUnit *NextSU = Edge->getSUnit();
Jim Grosbacheb431da2010-01-06 16:48:02 +0000836
David Goodwinda83f7d2009-11-12 19:08:21 +0000837 if ((Edge->getKind() != SDep::Anti) &&
838 (Edge->getKind() != SDep::Output)) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000839
David Goodwinde11f362009-10-26 19:32:42 +0000840 unsigned AntiDepReg = Edge->getReg();
David Greene75a2efb2009-12-24 00:14:25 +0000841 DEBUG(dbgs() << "\tAntidep reg: " << TRI->getName(AntiDepReg));
David Goodwinde11f362009-10-26 19:32:42 +0000842 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000843
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000844 if (!MRI.isAllocatable(AntiDepReg)) {
David Goodwinde11f362009-10-26 19:32:42 +0000845 // Don't break anti-dependencies on non-allocatable registers.
David Greene75a2efb2009-12-24 00:14:25 +0000846 DEBUG(dbgs() << " (non-allocatable)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000847 continue;
Craig Topperc0196b12014-04-14 00:51:57 +0000848 } else if (ExcludeRegs && ExcludeRegs->test(AntiDepReg)) {
David Goodwinb9fe5d52009-11-13 19:52:48 +0000849 // Don't break anti-dependencies for critical path registers
850 // if not on the critical path
David Greene75a2efb2009-12-24 00:14:25 +0000851 DEBUG(dbgs() << " (not critical-path)\n");
David Goodwinb9fe5d52009-11-13 19:52:48 +0000852 continue;
David Goodwinde11f362009-10-26 19:32:42 +0000853 } else if (PassthruRegs.count(AntiDepReg) != 0) {
854 // If the anti-dep register liveness "passes-thru", then
855 // don't try to change it. It will be changed along with
856 // the use if required to break an earlier antidep.
David Greene75a2efb2009-12-24 00:14:25 +0000857 DEBUG(dbgs() << " (passthru)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000858 continue;
859 } else {
860 // No anti-dep breaking for implicit deps
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000861 MachineOperand *AntiDepOp = MI.findRegisterDefOperand(AntiDepReg);
Craig Topperc0196b12014-04-14 00:51:57 +0000862 assert(AntiDepOp && "Can't find index for defined register operand");
863 if (!AntiDepOp || AntiDepOp->isImplicit()) {
David Greene75a2efb2009-12-24 00:14:25 +0000864 DEBUG(dbgs() << " (implicit)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000865 continue;
866 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000867
David Goodwinde11f362009-10-26 19:32:42 +0000868 // If the SUnit has other dependencies on the SUnit that
869 // it anti-depends on, don't bother breaking the
870 // anti-dependency since those edges would prevent such
871 // units from being scheduled past each other
872 // regardless.
David Goodwin80a03cc2009-11-20 19:32:48 +0000873 //
874 // Also, if there are dependencies on other SUnits with the
875 // same register as the anti-dependency, don't attempt to
876 // break it.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000877 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwinde11f362009-10-26 19:32:42 +0000878 PE = PathSU->Preds.end(); P != PE; ++P) {
David Goodwin80a03cc2009-11-20 19:32:48 +0000879 if (P->getSUnit() == NextSU ?
880 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
881 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
882 AntiDepReg = 0;
883 break;
884 }
885 }
Dan Gohman35bc4d42010-04-19 23:11:58 +0000886 for (SUnit::const_pred_iterator P = PathSU->Preds.begin(),
David Goodwin80a03cc2009-11-20 19:32:48 +0000887 PE = PathSU->Preds.end(); P != PE; ++P) {
888 if ((P->getSUnit() == NextSU) && (P->getKind() != SDep::Anti) &&
889 (P->getKind() != SDep::Output)) {
David Greene75a2efb2009-12-24 00:14:25 +0000890 DEBUG(dbgs() << " (real dependency)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000891 AntiDepReg = 0;
892 break;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000893 } else if ((P->getSUnit() != NextSU) &&
894 (P->getKind() == SDep::Data) &&
David Goodwin80a03cc2009-11-20 19:32:48 +0000895 (P->getReg() == AntiDepReg)) {
David Greene75a2efb2009-12-24 00:14:25 +0000896 DEBUG(dbgs() << " (other dependency)\n");
David Goodwin80a03cc2009-11-20 19:32:48 +0000897 AntiDepReg = 0;
898 break;
David Goodwinde11f362009-10-26 19:32:42 +0000899 }
900 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000901
David Goodwinde11f362009-10-26 19:32:42 +0000902 if (AntiDepReg == 0) continue;
Krzysztof Parzyszek143f6842016-05-26 18:22:53 +0000903
904 // If the definition of the anti-dependency register does not start
905 // a new live range, bail out. This can happen if the anti-dep
906 // register is a sub-register of another register whose live range
907 // spans over PathSU. In such case, PathSU defines only a part of
908 // the larger register.
909 RegAliases.reset();
910 for (MCRegAliasIterator AI(AntiDepReg, TRI, true); AI.isValid(); ++AI)
911 RegAliases.set(*AI);
912 for (SDep S : PathSU->Succs) {
913 SDep::Kind K = S.getKind();
914 if (K != SDep::Data && K != SDep::Output && K != SDep::Anti)
915 continue;
916 unsigned R = S.getReg();
917 if (!RegAliases[R])
918 continue;
919 if (R == AntiDepReg || TRI->isSubRegister(AntiDepReg, R))
920 continue;
921 AntiDepReg = 0;
922 break;
923 }
924
925 if (AntiDepReg == 0) continue;
David Goodwinde11f362009-10-26 19:32:42 +0000926 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000927
David Goodwinde11f362009-10-26 19:32:42 +0000928 assert(AntiDepReg != 0);
929 if (AntiDepReg == 0) continue;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000930
David Goodwinde11f362009-10-26 19:32:42 +0000931 // Determine AntiDepReg's register group.
David Goodwine056d102009-10-26 22:31:16 +0000932 const unsigned GroupIndex = State->GetGroup(AntiDepReg);
David Goodwinde11f362009-10-26 19:32:42 +0000933 if (GroupIndex == 0) {
David Greene75a2efb2009-12-24 00:14:25 +0000934 DEBUG(dbgs() << " (zero group)\n");
David Goodwinde11f362009-10-26 19:32:42 +0000935 continue;
936 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000937
David Greene75a2efb2009-12-24 00:14:25 +0000938 DEBUG(dbgs() << '\n');
Jim Grosbacheb431da2010-01-06 16:48:02 +0000939
David Goodwinde11f362009-10-26 19:32:42 +0000940 // Look for a suitable register to use to break the anti-dependence.
941 std::map<unsigned, unsigned> RenameMap;
David Goodwin7d8878a2009-11-05 01:19:35 +0000942 if (FindSuitableFreeRegisters(GroupIndex, RenameOrder, RenameMap)) {
David Greene75a2efb2009-12-24 00:14:25 +0000943 DEBUG(dbgs() << "\tBreaking anti-dependence edge on "
David Goodwinde11f362009-10-26 19:32:42 +0000944 << TRI->getName(AntiDepReg) << ":");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000945
David Goodwinde11f362009-10-26 19:32:42 +0000946 // Handle each group register...
947 for (std::map<unsigned, unsigned>::iterator
948 S = RenameMap.begin(), E = RenameMap.end(); S != E; ++S) {
949 unsigned CurrReg = S->first;
950 unsigned NewReg = S->second;
Jim Grosbacheb431da2010-01-06 16:48:02 +0000951
952 DEBUG(dbgs() << " " << TRI->getName(CurrReg) << "->" <<
953 TRI->getName(NewReg) << "(" <<
David Goodwinde11f362009-10-26 19:32:42 +0000954 RegRefs.count(CurrReg) << " refs)");
Jim Grosbacheb431da2010-01-06 16:48:02 +0000955
David Goodwinde11f362009-10-26 19:32:42 +0000956 // Update the references to the old register CurrReg to
957 // refer to the new register NewReg.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000958 for (const auto &Q : make_range(RegRefs.equal_range(CurrReg))) {
959 Q.second.Operand->setReg(NewReg);
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000960 // If the SU for the instruction being updated has debug
961 // information related to the anti-dependency register, make
962 // sure to update that as well.
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000963 const SUnit *SU = MISUnitMap[Q.second.Operand->getParent()];
Jim Grosbach84854832010-06-02 15:29:36 +0000964 if (!SU) continue;
Devang Patelf02a3762011-06-02 21:26:52 +0000965 for (DbgValueVector::iterator DVI = DbgValues.begin(),
966 DVE = DbgValues.end(); DVI != DVE; ++DVI)
Benjamin Kramerc9436ad2015-07-18 20:05:10 +0000967 if (DVI->second == Q.second.Operand->getParent())
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000968 UpdateDbgValue(*DVI->first, AntiDepReg, NewReg);
David Goodwinde11f362009-10-26 19:32:42 +0000969 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000970
David Goodwinde11f362009-10-26 19:32:42 +0000971 // We just went back in time and modified history; the
972 // liveness information for CurrReg is now inconsistent. Set
973 // the state as if it were dead.
David Goodwine056d102009-10-26 22:31:16 +0000974 State->UnionGroups(NewReg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000975 RegRefs.erase(NewReg);
976 DefIndices[NewReg] = DefIndices[CurrReg];
977 KillIndices[NewReg] = KillIndices[CurrReg];
Jim Grosbacheb431da2010-01-06 16:48:02 +0000978
David Goodwine056d102009-10-26 22:31:16 +0000979 State->UnionGroups(CurrReg, 0);
David Goodwinde11f362009-10-26 19:32:42 +0000980 RegRefs.erase(CurrReg);
981 DefIndices[CurrReg] = KillIndices[CurrReg];
982 KillIndices[CurrReg] = ~0u;
983 assert(((KillIndices[CurrReg] == ~0u) !=
984 (DefIndices[CurrReg] == ~0u)) &&
985 "Kill and Def maps aren't consistent for AntiDepReg!");
986 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000987
David Goodwinde11f362009-10-26 19:32:42 +0000988 ++Broken;
David Greene75a2efb2009-12-24 00:14:25 +0000989 DEBUG(dbgs() << '\n');
David Goodwinde11f362009-10-26 19:32:42 +0000990 }
991 }
992 }
993
994 ScanInstruction(MI, Count);
995 }
Jim Grosbacheb431da2010-01-06 16:48:02 +0000996
David Goodwinde11f362009-10-26 19:32:42 +0000997 return Broken;
998}