blob: 6b30540925621c944c677f2f79f61b9f23ebc689 [file] [log] [blame]
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +00001//===- ExecutionDepsFix.cpp - Fix execution dependecy issues ----*- C++ -*-===//
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +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//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +00009
Matthias Braune6ff30b2017-03-18 05:08:58 +000010#include "llvm/CodeGen/ExecutionDepsFix.h"
11
Chandler Carruthed0881b2012-12-03 16:50:05 +000012#include "llvm/ADT/PostOrderIterator.h"
Matthias Braun8142efa2014-12-17 19:13:47 +000013#include "llvm/ADT/iterator_range.h"
Juergen Ributzka310034e2013-12-14 06:52:56 +000014#include "llvm/CodeGen/LivePhysRegs.h"
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000015#include "llvm/CodeGen/MachineFunctionPass.h"
16#include "llvm/CodeGen/MachineRegisterInfo.h"
Marina Yatsina53ce3f92016-08-17 19:07:40 +000017#include "llvm/CodeGen/RegisterClassInfo.h"
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000018#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Target/TargetInstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000022#include "llvm/Target/TargetSubtargetInfo.h"
23
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000024using namespace llvm;
25
Matthias Braune9f82092017-03-18 05:05:40 +000026#define DEBUG_TYPE "execution-deps-fix"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000027
Matthias Braun046318b2015-03-06 18:56:20 +000028/// Translate TRI register number to a list of indices into our smaller tables
Matthias Braun8142efa2014-12-17 19:13:47 +000029/// of interesting registers.
30iterator_range<SmallVectorImpl<int>::const_iterator>
Matthias Braune9f82092017-03-18 05:05:40 +000031ExecutionDepsFix::regIndices(unsigned Reg) const {
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +000032 assert(Reg < AliasMap.size() && "Invalid register");
Matthias Braun8142efa2014-12-17 19:13:47 +000033 const auto &Entry = AliasMap[Reg];
34 return make_range(Entry.begin(), Entry.end());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000035}
36
Matthias Braune9f82092017-03-18 05:05:40 +000037DomainValue *ExecutionDepsFix::alloc(int domain) {
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000038 DomainValue *dv = Avail.empty() ?
39 new(Allocator.Allocate()) DomainValue :
40 Avail.pop_back_val();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000041 if (domain >= 0)
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000042 dv->addDomain(domain);
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +000043 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000044 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000045 return dv;
46}
47
Sanjay Patel4297c3f2015-03-15 18:16:04 +000048/// Release a reference to DV. When the last reference is released,
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +000049/// collapse if needed.
Matthias Braune9f82092017-03-18 05:05:40 +000050void ExecutionDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000051 while (DV) {
52 assert(DV->Refs && "Bad DomainValue");
53 if (--DV->Refs)
54 return;
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +000055
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000056 // There are no more DV references. Collapse any contained instructions.
57 if (DV->AvailableDomains && !DV->isCollapsed())
58 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +000059
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000060 DomainValue *Next = DV->Next;
61 DV->clear();
62 Avail.push_back(DV);
63 // Also release the next DomainValue in the chain.
64 DV = Next;
65 }
66}
67
Sanjay Patel4297c3f2015-03-15 18:16:04 +000068/// Follow the chain of dead DomainValues until a live DomainValue is reached.
69/// Update the referenced pointer when necessary.
Matthias Braune9f82092017-03-18 05:05:40 +000070DomainValue *ExecutionDepsFix::resolve(DomainValue *&DVRef) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000071 DomainValue *DV = DVRef;
72 if (!DV || !DV->Next)
73 return DV;
74
75 // DV has a chain. Find the end.
76 do DV = DV->Next;
77 while (DV->Next);
78
79 // Update DVRef to point to DV.
80 retain(DV);
81 release(DVRef);
82 DVRef = DV;
83 return DV;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000084}
85
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000086/// Set LiveRegs[rx] = dv, updating reference counts.
Matthias Braune9f82092017-03-18 05:05:40 +000087void ExecutionDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000088 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +000089 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000090
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +000091 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000092 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +000093 if (LiveRegs[rx].Value)
94 release(LiveRegs[rx].Value);
95 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000096}
97
98// Kill register rx, recycle or collapse any DomainValue.
Matthias Braune9f82092017-03-18 05:05:40 +000099void ExecutionDepsFix::kill(int rx) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000100 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000101 assert(LiveRegs && "Must enter basic block first.");
102 if (!LiveRegs[rx].Value)
103 return;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000104
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000105 release(LiveRegs[rx].Value);
Craig Topperc0196b12014-04-14 00:51:57 +0000106 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000107}
108
109/// Force register rx into domain.
Matthias Braune9f82092017-03-18 05:05:40 +0000110void ExecutionDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000111 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000112 assert(LiveRegs && "Must enter basic block first.");
113 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000114 if (dv->isCollapsed())
115 dv->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000116 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000117 collapse(dv, domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000118 else {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000119 // This is an incompatible open DomainValue. Collapse it to whatever and
120 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000121 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000122 assert(LiveRegs[rx].Value && "Not live after collapse?");
123 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000124 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000125 } else {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000126 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000127 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000128 }
129}
130
131/// Collapse open DomainValue into given domain. If there are multiple
132/// registers using dv, they each get a unique collapsed DomainValue.
Matthias Braune9f82092017-03-18 05:05:40 +0000133void ExecutionDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000134 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000135
136 // Collapse all the instructions.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000137 while (!dv->Instrs.empty())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000138 TII->setExecutionDomain(*dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000139 dv->setSingleDomain(domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000140
141 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000142 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000143 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000144 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000145 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000146}
147
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000148/// All instructions and registers in B are moved to A, and B is released.
Matthias Braune9f82092017-03-18 05:05:40 +0000149bool ExecutionDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000150 assert(!A->isCollapsed() && "Cannot merge into collapsed");
151 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen58ca0a62010-03-31 20:05:12 +0000152 if (A == B)
Jakob Stoklund Olesen4cd58662010-03-31 17:13:16 +0000153 return true;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000154 // Restrict to the domains that A and B have in common.
155 unsigned common = A->getCommonDomains(B->AvailableDomains);
156 if (!common)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000157 return false;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000158 A->AvailableDomains = common;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000159 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000160
161 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000162 B->clear();
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000163 // All uses of B are referred to A.
164 B->Next = retain(A);
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000165
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000166 for (unsigned rx = 0; rx != NumRegs; ++rx) {
167 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000168 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000169 setLiveReg(rx, A);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000170 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000171 return true;
172}
173
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000174/// Set up LiveRegs by merging predecessor live-out values.
Matthias Braune9f82092017-03-18 05:05:40 +0000175void ExecutionDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000176 // Reset instruction counter in each basic block.
177 CurInstr = 0;
178
Andrew Trickb6d56be2013-10-14 22:19:03 +0000179 // Set up UndefReads to track undefined register reads.
180 UndefReads.clear();
Juergen Ributzka310034e2013-12-14 06:52:56 +0000181 LiveRegSet.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000182
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000183 // Set up LiveRegs to represent registers entering MBB.
184 if (!LiveRegs)
185 LiveRegs = new LiveReg[NumRegs];
186
187 // Default values are 'nothing happened a long time ago'.
188 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Craig Topperc0196b12014-04-14 00:51:57 +0000189 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000190 LiveRegs[rx].Def = -(1 << 20);
191 }
192
193 // This is the entry block.
194 if (MBB->pred_empty()) {
Matthias Braund9da1622015-09-09 18:08:03 +0000195 for (const auto &LI : MBB->liveins()) {
196 for (int rx : regIndices(LI.PhysReg)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000197 // Treat function live-ins as if they were defined just before the first
198 // instruction. Usually, function arguments are set up immediately
199 // before the call.
200 LiveRegs[rx].Def = -1;
201 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000202 }
203 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
204 return;
205 }
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000206
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000207 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000208 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
209 pe = MBB->pred_end(); pi != pe; ++pi) {
Keno Fischer578cf7a2017-01-30 23:37:03 +0000210 auto fi = MBBInfos.find(*pi);
211 assert(fi != MBBInfos.end() &&
212 "Should have pre-allocated MBBInfos for all MBBs");
213 LiveReg *Incoming = fi->second.OutRegs;
214 // Incoming is null if this is a backedge from a BB
215 // we haven't processed yet
216 if (Incoming == nullptr) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000217 continue;
218 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000219
220 for (unsigned rx = 0; rx != NumRegs; ++rx) {
221 // Use the most recent predecessor def for each register.
Keno Fischer578cf7a2017-01-30 23:37:03 +0000222 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, Incoming[rx].Def);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000223
Keno Fischer578cf7a2017-01-30 23:37:03 +0000224 DomainValue *pdv = resolve(Incoming[rx].Value);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000225 if (!pdv)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000226 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000227 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000228 setLiveReg(rx, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000229 continue;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000230 }
Chris Lattner503a0ef2010-03-31 20:32:51 +0000231
232 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000233 if (LiveRegs[rx].Value->isCollapsed()) {
Eric Christopher650c8f22014-05-20 17:11:11 +0000234 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000235 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
236 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
237 collapse(pdv, Domain);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000238 continue;
239 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000240
Chris Lattner503a0ef2010-03-31 20:32:51 +0000241 // Currently open, merge in predecessor.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000242 if (!pdv->isCollapsed())
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000243 merge(LiveRegs[rx].Value, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000244 else
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000245 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000246 }
247 }
Keno Fischer578cf7a2017-01-30 23:37:03 +0000248 DEBUG(
249 dbgs() << "BB#" << MBB->getNumber()
250 << (!isBlockDone(MBB) ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000251}
252
Matthias Braune9f82092017-03-18 05:05:40 +0000253void ExecutionDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000254 assert(LiveRegs && "Must enter basic block first.");
Keno Fischer578cf7a2017-01-30 23:37:03 +0000255 LiveReg *OldOutRegs = MBBInfos[MBB].OutRegs;
256 // Save register clearances at end of MBB - used by enterBasicBlock().
257 MBBInfos[MBB].OutRegs = LiveRegs;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000258
Keno Fischer578cf7a2017-01-30 23:37:03 +0000259 // While processing the basic block, we kept `Def` relative to the start
260 // of the basic block for convenience. However, future use of this information
261 // only cares about the clearance from the end of the block, so adjust
262 // everything to be relative to the end of the basic block.
263 for (unsigned i = 0, e = NumRegs; i != e; ++i)
264 LiveRegs[i].Def -= CurInstr;
265 if (OldOutRegs) {
266 // This must be the second pass.
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000267 // Release all the DomainValues instead of keeping them.
268 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Keno Fischer578cf7a2017-01-30 23:37:03 +0000269 release(OldOutRegs[i].Value);
270 delete[] OldOutRegs;
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000271 }
Craig Topperc0196b12014-04-14 00:51:57 +0000272 LiveRegs = nullptr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000273}
274
Matthias Braune9f82092017-03-18 05:05:40 +0000275bool ExecutionDepsFix::visitInstr(MachineInstr *MI) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000276 // Update instructions with explicit execution domains.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000277 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(*MI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000278 if (DomP.first) {
279 if (DomP.second)
280 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000281 else
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000282 visitHardInstr(MI, DomP.first);
283 }
284
Keno Fischer578cf7a2017-01-30 23:37:03 +0000285 return !DomP.first;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000286}
287
Marina Yatsina88f0c312016-08-11 07:32:08 +0000288/// \brief Helps avoid false dependencies on undef registers by updating the
289/// machine instructions' undef operand to use a register that the instruction
290/// is truly dependent on, or use a register with clearance higher than Pref.
Matthias Braune9f82092017-03-18 05:05:40 +0000291void ExecutionDepsFix::pickBestRegisterForUndef(MachineInstr *MI,
292 unsigned OpIdx, unsigned Pref) {
Marina Yatsina88f0c312016-08-11 07:32:08 +0000293 MachineOperand &MO = MI->getOperand(OpIdx);
294 assert(MO.isUndef() && "Expected undef machine operand");
295
296 unsigned OriginalReg = MO.getReg();
297
298 // Update only undef operands that are mapped to one register.
299 if (AliasMap[OriginalReg].size() != 1)
300 return;
301
302 // Get the undef operand's register class
303 const TargetRegisterClass *OpRC =
304 TII->getRegClass(MI->getDesc(), OpIdx, TRI, *MF);
305
306 // If the instruction has a true dependency, we can hide the false depdency
307 // behind it.
308 for (MachineOperand &CurrMO : MI->operands()) {
309 if (!CurrMO.isReg() || CurrMO.isDef() || CurrMO.isUndef() ||
310 !OpRC->contains(CurrMO.getReg()))
311 continue;
312 // We found a true dependency - replace the undef register with the true
313 // dependency.
314 MO.setReg(CurrMO.getReg());
315 return;
316 }
317
318 // Go over all registers in the register class and find the register with
319 // max clearance or clearance higher than Pref.
320 unsigned MaxClearance = 0;
321 unsigned MaxClearanceReg = OriginalReg;
Marina Yatsina53ce3f92016-08-17 19:07:40 +0000322 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
323 for (auto Reg : Order) {
Marina Yatsina4b226422016-08-17 11:40:21 +0000324 assert(AliasMap[Reg].size() == 1 &&
325 "Reg is expected to be mapped to a single index");
326 int RCrx = *regIndices(Reg).begin();
327 unsigned Clearance = CurInstr - LiveRegs[RCrx].Def;
Marina Yatsina88f0c312016-08-11 07:32:08 +0000328 if (Clearance <= MaxClearance)
329 continue;
330 MaxClearance = Clearance;
Marina Yatsina4b226422016-08-17 11:40:21 +0000331 MaxClearanceReg = Reg;
Marina Yatsina88f0c312016-08-11 07:32:08 +0000332
333 if (MaxClearance > Pref)
334 break;
335 }
336
337 // Update the operand if we found a register with better clearance.
338 if (MaxClearanceReg != OriginalReg)
339 MO.setReg(MaxClearanceReg);
340}
341
Andrew Trickb6d56be2013-10-14 22:19:03 +0000342/// \brief Return true to if it makes sense to break dependence on a partial def
343/// or undef use.
Matthias Braune9f82092017-03-18 05:05:40 +0000344bool ExecutionDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
345 unsigned Pref) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000346 unsigned reg = MI->getOperand(OpIdx).getReg();
Matthias Braun046318b2015-03-06 18:56:20 +0000347 for (int rx : regIndices(reg)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000348 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
349 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000350
Matthias Braun8142efa2014-12-17 19:13:47 +0000351 if (Pref > Clearance) {
352 DEBUG(dbgs() << ": Break dependency.\n");
353 continue;
354 }
Keno Fischer578cf7a2017-01-30 23:37:03 +0000355 DEBUG(dbgs() << ": OK .\n");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000356 return false;
357 }
Matthias Braun8142efa2014-12-17 19:13:47 +0000358 return true;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000359}
360
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000361// Update def-ages for registers defined by MI.
362// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000363//
364// Also break dependencies on partial defs and undef uses.
Matthias Braune9f82092017-03-18 05:05:40 +0000365void ExecutionDepsFix::processDefs(MachineInstr *MI, bool breakDependency,
366 bool Kill) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000367 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000368
369 // Break dependence on undef uses. Do this before updating LiveRegs below.
370 unsigned OpNum;
Keno Fischer578cf7a2017-01-30 23:37:03 +0000371 if (breakDependency) {
372 unsigned Pref = TII->getUndefRegClearance(*MI, OpNum, TRI);
373 if (Pref) {
374 pickBestRegisterForUndef(MI, OpNum, Pref);
375 if (shouldBreakDependence(MI, OpNum, Pref))
376 UndefReads.push_back(std::make_pair(MI, OpNum));
377 }
Andrew Trickb6d56be2013-10-14 22:19:03 +0000378 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000379 const MCInstrDesc &MCID = MI->getDesc();
380 for (unsigned i = 0,
Evan Cheng7f8e5632011-12-07 07:15:52 +0000381 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000382 i != e; ++i) {
383 MachineOperand &MO = MI->getOperand(i);
384 if (!MO.isReg())
385 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000386 if (MO.isUse())
387 continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000388 for (int rx : regIndices(MO.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000389 // This instruction explicitly defines rx.
390 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
391 << '\t' << *MI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000392
Keno Fischer578cf7a2017-01-30 23:37:03 +0000393 if (breakDependency) {
394 // Check clearance before partial register updates.
395 // Call breakDependence before setting LiveRegs[rx].Def.
396 unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
397 if (Pref && shouldBreakDependence(MI, i, Pref))
398 TII->breakPartialRegDependency(*MI, i, TRI);
399 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000400
Matthias Braun8142efa2014-12-17 19:13:47 +0000401 // How many instructions since rx was last written?
402 LiveRegs[rx].Def = CurInstr;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000403
Matthias Braun8142efa2014-12-17 19:13:47 +0000404 // Kill off domains redefined by generic instructions.
405 if (Kill)
406 kill(rx);
407 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000408 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000409 ++CurInstr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000410}
411
Andrew Trickb6d56be2013-10-14 22:19:03 +0000412/// \break Break false dependencies on undefined register reads.
413///
414/// Walk the block backward computing precise liveness. This is expensive, so we
415/// only do it on demand. Note that the occurrence of undefined register reads
416/// that should be broken is very rare, but when they occur we may have many in
417/// a single block.
Matthias Braune9f82092017-03-18 05:05:40 +0000418void ExecutionDepsFix::processUndefReads(MachineBasicBlock *MBB) {
Andrew Trickb6d56be2013-10-14 22:19:03 +0000419 if (UndefReads.empty())
420 return;
421
422 // Collect this block's live out register units.
Matthias Braun0c989a82016-12-08 00:15:51 +0000423 LiveRegSet.init(*TRI);
Matthias Braun24f26e62016-05-03 00:08:46 +0000424 // We do not need to care about pristine registers as they are just preserved
425 // but not actually used in the function.
Matthias Braund1aabb22016-05-03 00:24:32 +0000426 LiveRegSet.addLiveOutsNoPristines(*MBB);
Juergen Ributzka310034e2013-12-14 06:52:56 +0000427
Andrew Trickb6d56be2013-10-14 22:19:03 +0000428 MachineInstr *UndefMI = UndefReads.back().first;
429 unsigned OpIdx = UndefReads.back().second;
430
Pete Cooper7679afd2015-07-24 21:13:43 +0000431 for (MachineInstr &I : make_range(MBB->rbegin(), MBB->rend())) {
Andrew Trick60cf0ad2013-12-13 22:23:54 +0000432 // Update liveness, including the current instruction's defs.
Pete Cooper7679afd2015-07-24 21:13:43 +0000433 LiveRegSet.stepBackward(I);
Andrew Trick3a996932013-10-15 03:39:43 +0000434
Pete Cooper7679afd2015-07-24 21:13:43 +0000435 if (UndefMI == &I) {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000436 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000437 TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000438
439 UndefReads.pop_back();
440 if (UndefReads.empty())
441 return;
442
443 UndefMI = UndefReads.back().first;
444 OpIdx = UndefReads.back().second;
445 }
Andrew Trickb6d56be2013-10-14 22:19:03 +0000446 }
447}
448
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000449// A hard instruction only works in one domain. All input registers will be
450// forced into that domain.
Matthias Braune9f82092017-03-18 05:05:40 +0000451void ExecutionDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000452 // Collapse all uses.
453 for (unsigned i = mi->getDesc().getNumDefs(),
454 e = mi->getDesc().getNumOperands(); i != e; ++i) {
455 MachineOperand &mo = mi->getOperand(i);
456 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000457 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000458 force(rx, domain);
459 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000460 }
461
462 // Kill all defs and force them.
463 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
464 MachineOperand &mo = mi->getOperand(i);
465 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000466 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000467 kill(rx);
468 force(rx, domain);
469 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000470 }
471}
472
473// A soft instruction can be changed to work in other domains given by mask.
Matthias Braune9f82092017-03-18 05:05:40 +0000474void ExecutionDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000475 // Bitmask of available domains for this instruction after taking collapsed
476 // operands into account.
477 unsigned available = mask;
478
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000479 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000480 SmallVector<int, 4> used;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000481 if (LiveRegs)
482 for (unsigned i = mi->getDesc().getNumDefs(),
483 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000484 MachineOperand &mo = mi->getOperand(i);
485 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000486 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000487 DomainValue *dv = LiveRegs[rx].Value;
488 if (dv == nullptr)
489 continue;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000490 // Bitmask of domains that dv and available have in common.
491 unsigned common = dv->getCommonDomains(available);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000492 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000493 if (dv->isCollapsed()) {
494 // Restrict available domains to the ones in common with the operand.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000495 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000496 // penalty for this operand.
497 if (common) available = common;
498 } else if (common)
499 // Open DomainValue is compatible, save it for merging.
Chris Lattner503a0ef2010-03-31 20:32:51 +0000500 used.push_back(rx);
501 else
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000502 // Open DomainValue is not compatible with instruction. It is useless
503 // now.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000504 kill(rx);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000505 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000506 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000507
508 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000509 if (isPowerOf2_32(available)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000510 unsigned domain = countTrailingZeros(available);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000511 TII->setExecutionDomain(*mi, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000512 visitHardInstr(mi, domain);
513 return;
514 }
515
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000516 // Kill off any remaining uses that don't match available, and build a list of
517 // incoming DomainValues that we want to merge.
Craig Topper3b8aca22017-02-25 18:12:25 +0000518 SmallVector<const LiveReg *, 4> Regs;
Craig Topperc446b1f2017-02-24 06:38:24 +0000519 for (int rx : used) {
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000520 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000521 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000522 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000523 if (!LR.Value->getCommonDomains(available)) {
524 kill(rx);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000525 continue;
526 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000527 // Sorted insertion.
Craig Topper3b8aca22017-02-25 18:12:25 +0000528 auto I = std::upper_bound(Regs.begin(), Regs.end(), &LR,
529 [](const LiveReg *LHS, const LiveReg *RHS) {
530 return LHS->Def < RHS->Def;
531 });
532 Regs.insert(I, &LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000533 }
534
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000535 // doms are now sorted in order of appearance. Try to merge them all, giving
536 // priority to the latest ones.
Craig Topperc0196b12014-04-14 00:51:57 +0000537 DomainValue *dv = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000538 while (!Regs.empty()) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000539 if (!dv) {
Craig Topper3b8aca22017-02-25 18:12:25 +0000540 dv = Regs.pop_back_val()->Value;
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000541 // Force the first dv to match the current instruction.
542 dv->AvailableDomains = dv->getCommonDomains(available);
543 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner503a0ef2010-03-31 20:32:51 +0000544 continue;
545 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000546
Craig Topper3b8aca22017-02-25 18:12:25 +0000547 DomainValue *Latest = Regs.pop_back_val()->Value;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000548 // Skip already merged values.
549 if (Latest == dv || Latest->Next)
550 continue;
551 if (merge(dv, Latest))
552 continue;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000553
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000554 // If latest didn't merge, it is useless now. Kill all registers using it.
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000555 for (int i : used) {
556 assert(LiveRegs && "no space allocated for live registers");
557 if (LiveRegs[i].Value == Latest)
558 kill(i);
559 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000560 }
561
562 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000563 if (!dv) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000564 dv = alloc();
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000565 dv->AvailableDomains = available;
566 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000567 dv->Instrs.push_back(mi);
568
Silviu Baranga3c314992012-10-03 08:29:36 +0000569 // Finally set all defs and non-collapsed uses to dv. We must iterate through
570 // all the operators, including imp-def ones.
571 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
572 ee = mi->operands_end();
573 ii != ee; ++ii) {
574 MachineOperand &mo = *ii;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000575 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000576 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000577 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
578 kill(rx);
579 setLiveReg(rx, dv);
580 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000581 }
582 }
583}
584
Matthias Braune9f82092017-03-18 05:05:40 +0000585void ExecutionDepsFix::processBasicBlock(MachineBasicBlock *MBB,
586 bool PrimaryPass) {
Keno Fischer578cf7a2017-01-30 23:37:03 +0000587 enterBasicBlock(MBB);
588 // If this block is not done, it makes little sense to make any decisions
589 // based on clearance information. We need to make a second pass anyway,
590 // and by then we'll have better information, so we can avoid doing the work
591 // to try and break dependencies now.
592 bool breakDependency = isBlockDone(MBB);
593 for (MachineInstr &MI : *MBB) {
594 if (!MI.isDebugValue()) {
595 bool Kill = false;
596 if (PrimaryPass)
597 Kill = visitInstr(&MI);
598 processDefs(&MI, breakDependency, Kill);
599 }
600 }
601 if (breakDependency)
602 processUndefReads(MBB);
603 leaveBasicBlock(MBB);
604}
605
Matthias Braune9f82092017-03-18 05:05:40 +0000606bool ExecutionDepsFix::isBlockDone(MachineBasicBlock *MBB) {
Keno Fischer578cf7a2017-01-30 23:37:03 +0000607 return MBBInfos[MBB].PrimaryCompleted &&
608 MBBInfos[MBB].IncomingCompleted == MBBInfos[MBB].PrimaryIncoming &&
609 MBBInfos[MBB].IncomingProcessed == MBB->pred_size();
610}
611
Matthias Braune9f82092017-03-18 05:05:40 +0000612void ExecutionDepsFix::updateSuccessors(MachineBasicBlock *MBB, bool Primary) {
Keno Fischer578cf7a2017-01-30 23:37:03 +0000613 bool Done = isBlockDone(MBB);
614 for (auto *Succ : MBB->successors()) {
615 if (!isBlockDone(Succ)) {
616 if (Primary) {
617 MBBInfos[Succ].IncomingProcessed++;
618 }
619 if (Done) {
620 MBBInfos[Succ].IncomingCompleted++;
621 }
622 if (isBlockDone(Succ)) {
623 // Perform secondary processing for this successor. See the big comment
624 // in runOnMachineFunction, for an explanation of the iteration order.
625 processBasicBlock(Succ, false);
626 updateSuccessors(Succ, false);
627 }
628 }
629 }
630}
631
Matthias Braune9f82092017-03-18 05:05:40 +0000632bool ExecutionDepsFix::runOnMachineFunction(MachineFunction &mf) {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000633 if (skipFunction(*mf.getFunction()))
634 return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000635 MF = &mf;
Eric Christopherfc6de422014-08-05 02:39:49 +0000636 TII = MF->getSubtarget().getInstrInfo();
637 TRI = MF->getSubtarget().getRegisterInfo();
Marina Yatsina53ce3f92016-08-17 19:07:40 +0000638 RegClassInfo.runOnMachineFunction(mf);
Craig Topperc0196b12014-04-14 00:51:57 +0000639 LiveRegs = nullptr;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000640 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000641
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000642 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
Craig Toppercf0444b2014-11-17 05:50:14 +0000643 << TRI->getRegClassName(RC) << " **********\n");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000644
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000645 // If no relevant registers are used in the function, we can skip it
646 // completely.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000647 bool anyregs = false;
Matthias Braun9912bb82015-07-14 17:52:07 +0000648 const MachineRegisterInfo &MRI = mf.getRegInfo();
Matthias Braund55bcf22015-08-18 18:54:27 +0000649 for (unsigned Reg : *RC) {
650 if (MRI.isPhysRegUsed(Reg)) {
651 anyregs = true;
652 break;
653 }
654 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000655 if (!anyregs) return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000656
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000657 // Initialize the AliasMap on the first use.
658 if (AliasMap.empty()) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000659 // Given a PhysReg, AliasMap[PhysReg] returns a list of indices into RC and
660 // therefore the LiveRegs array.
661 AliasMap.resize(TRI->getNumRegs());
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000662 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000663 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
664 AI.isValid(); ++AI)
Matthias Braun8142efa2014-12-17 19:13:47 +0000665 AliasMap[*AI].push_back(i);
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000666 }
667
Keno Fischer578cf7a2017-01-30 23:37:03 +0000668 // Initialize the MMBInfos
669 for (auto &MBB : mf) {
670 MBBInfo InitialInfo;
671 MBBInfos.insert(std::make_pair(&MBB, InitialInfo));
672 }
673
674 /*
675 * We want to visit every instruction in every basic block in order to update
676 * it's execution domain or break any false dependencies. However, for the
677 * dependency breaking, we need to know clearances from all predecessors
678 * (including any backedges). One way to do so would be to do two complete
679 * passes over all basic blocks/instructions, the first for recording
680 * clearances, the second to break the dependencies. However, for functions
681 * without backedges, or functions with a lot of straight-line code, and
682 * a small loop, that would be a lot of unnecessary work (since only the
683 * BBs that are part of the loop require two passes). As an example,
684 * consider the following loop.
685 *
686 *
687 * PH -> A -> B (xmm<Undef> -> xmm<Def>) -> C -> D -> EXIT
688 * ^ |
689 * +----------------------------------+
690 *
691 * The iteration order is as follows:
692 * Naive: PH A B C D A' B' C' D'
693 * Optimized: PH A B C A' B' C' D
694 *
695 * Note that we avoid processing D twice, because we can entirely process
696 * the predecessors before getting to D. We call a block that is ready
697 * for its second round of processing `done` (isBlockDone). Once we finish
698 * processing some block, we update the counters in MBBInfos and re-process
699 * any successors that are now done.
700 */
701
Duncan P. N. Exon Smith8f11e1a2015-10-09 16:54:49 +0000702 MachineBasicBlock *Entry = &*MF->begin();
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000703 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
704 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
705 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
706 MachineBasicBlock *MBB = *MBBI;
Keno Fischer578cf7a2017-01-30 23:37:03 +0000707 // N.B: IncomingProcessed and IncomingCompleted were already updated while
708 // processing this block's predecessors.
709 MBBInfos[MBB].PrimaryCompleted = true;
710 MBBInfos[MBB].PrimaryIncoming = MBBInfos[MBB].IncomingProcessed;
711 processBasicBlock(MBB, true);
712 updateSuccessors(MBB, true);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000713 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000714
Keno Fischer578cf7a2017-01-30 23:37:03 +0000715 // We need to go through again and finalize any blocks that are not done yet.
716 // This is possible if blocks have dead predecessors, so we didn't visit them
717 // above.
718 for (ReversePostOrderTraversal<MachineBasicBlock *>::rpo_iterator
719 MBBI = RPOT.begin(),
720 MBBE = RPOT.end();
721 MBBI != MBBE; ++MBBI) {
722 MachineBasicBlock *MBB = *MBBI;
723 if (!isBlockDone(MBB)) {
724 processBasicBlock(MBB, false);
725 // Don't update successors here. We'll get to them anyway through this
726 // loop.
727 }
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000728 }
729
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000730 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
731 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
732 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
Keno Fischer578cf7a2017-01-30 23:37:03 +0000733 auto FI = MBBInfos.find(*MBBI);
734 if (FI == MBBInfos.end() || !FI->second.OutRegs)
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000735 continue;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000736 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Keno Fischer578cf7a2017-01-30 23:37:03 +0000737 if (FI->second.OutRegs[i].Value)
738 release(FI->second.OutRegs[i].Value);
739 delete[] FI->second.OutRegs;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000740 }
Keno Fischer578cf7a2017-01-30 23:37:03 +0000741 MBBInfos.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000742 UndefReads.clear();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000743 Avail.clear();
744 Allocator.DestroyAll();
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000745
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000746 return false;
747}