blob: 8fb725a82965b3097a6234056601541ad456e6fa [file] [log] [blame]
Marina Yatsina0bf841a2018-01-22 10:06:50 +00001//===- ExecutionDomainFix.cpp - Fix execution domain issues ----*- C++ -*--===//
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +00006//
7//===----------------------------------------------------------------------===//
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +00008
Marina Yatsina3d8efa42018-01-22 10:06:33 +00009#include "llvm/CodeGen/ExecutionDomainFix.h"
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000010#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000011#include "llvm/CodeGen/TargetInstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000012
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000013using namespace llvm;
14
Matthias Braune9f82092017-03-18 05:05:40 +000015#define DEBUG_TYPE "execution-deps-fix"
Chandler Carruth1b9dde02014-04-22 02:02:50 +000016
Matthias Braun8142efa2014-12-17 19:13:47 +000017iterator_range<SmallVectorImpl<int>::const_iterator>
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000018ExecutionDomainFix::regIndices(unsigned Reg) const {
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +000019 assert(Reg < AliasMap.size() && "Invalid register");
Matthias Braun8142efa2014-12-17 19:13:47 +000020 const auto &Entry = AliasMap[Reg];
21 return make_range(Entry.begin(), Entry.end());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000022}
23
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000024DomainValue *ExecutionDomainFix::alloc(int domain) {
Marina Yatsinac757c0b2018-01-22 10:06:18 +000025 DomainValue *dv = Avail.empty() ? new (Allocator.Allocate()) DomainValue
26 : Avail.pop_back_val();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000027 if (domain >= 0)
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000028 dv->addDomain(domain);
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +000029 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000030 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000031 return dv;
32}
33
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000034void ExecutionDomainFix::release(DomainValue *DV) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000035 while (DV) {
36 assert(DV->Refs && "Bad DomainValue");
37 if (--DV->Refs)
38 return;
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +000039
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000040 // There are no more DV references. Collapse any contained instructions.
41 if (DV->AvailableDomains && !DV->isCollapsed())
42 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +000043
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000044 DomainValue *Next = DV->Next;
45 DV->clear();
46 Avail.push_back(DV);
47 // Also release the next DomainValue in the chain.
48 DV = Next;
49 }
50}
51
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000052DomainValue *ExecutionDomainFix::resolve(DomainValue *&DVRef) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000053 DomainValue *DV = DVRef;
54 if (!DV || !DV->Next)
55 return DV;
56
57 // DV has a chain. Find the end.
Marina Yatsinac757c0b2018-01-22 10:06:18 +000058 do
59 DV = DV->Next;
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000060 while (DV->Next);
61
62 // Update DVRef to point to DV.
63 retain(DV);
64 release(DVRef);
65 DVRef = DV;
66 return DV;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000067}
68
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000069void ExecutionDomainFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000070 assert(unsigned(rx) < NumRegs && "Invalid index");
Marina Yatsina6daa2d22018-01-22 10:05:53 +000071 assert(!LiveRegs.empty() && "Must enter basic block first.");
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000072
Marina Yatsina273d35d2018-01-22 10:06:01 +000073 if (LiveRegs[rx] == dv)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000074 return;
Marina Yatsina273d35d2018-01-22 10:06:01 +000075 if (LiveRegs[rx])
76 release(LiveRegs[rx]);
77 LiveRegs[rx] = retain(dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000078}
79
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000080void ExecutionDomainFix::kill(int rx) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000081 assert(unsigned(rx) < NumRegs && "Invalid index");
Marina Yatsina6daa2d22018-01-22 10:05:53 +000082 assert(!LiveRegs.empty() && "Must enter basic block first.");
Marina Yatsina273d35d2018-01-22 10:06:01 +000083 if (!LiveRegs[rx])
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +000084 return;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000085
Marina Yatsina273d35d2018-01-22 10:06:01 +000086 release(LiveRegs[rx]);
87 LiveRegs[rx] = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000088}
89
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +000090void ExecutionDomainFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000091 assert(unsigned(rx) < NumRegs && "Invalid index");
Marina Yatsina6daa2d22018-01-22 10:05:53 +000092 assert(!LiveRegs.empty() && "Must enter basic block first.");
Marina Yatsina273d35d2018-01-22 10:06:01 +000093 if (DomainValue *dv = LiveRegs[rx]) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000094 if (dv->isCollapsed())
95 dv->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +000096 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +000097 collapse(dv, domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +000098 else {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +000099 // This is an incompatible open DomainValue. Collapse it to whatever and
100 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000101 collapse(dv, dv->getFirstDomain());
Marina Yatsina273d35d2018-01-22 10:06:01 +0000102 assert(LiveRegs[rx] && "Not live after collapse?");
103 LiveRegs[rx]->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000104 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000105 } else {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000106 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000107 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000108 }
109}
110
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000111void ExecutionDomainFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000112 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000113
114 // Collapse all the instructions.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000115 while (!dv->Instrs.empty())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000116 TII->setExecutionDomain(*dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000117 dv->setSingleDomain(domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000118
119 // If there are multiple users, give them new, unique DomainValues.
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000120 if (!LiveRegs.empty() && dv->Refs > 1)
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000121 for (unsigned rx = 0; rx != NumRegs; ++rx)
Marina Yatsina273d35d2018-01-22 10:06:01 +0000122 if (LiveRegs[rx] == dv)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000123 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000124}
125
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000126bool ExecutionDomainFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000127 assert(!A->isCollapsed() && "Cannot merge into collapsed");
128 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen58ca0a62010-03-31 20:05:12 +0000129 if (A == B)
Jakob Stoklund Olesen4cd58662010-03-31 17:13:16 +0000130 return true;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000131 // Restrict to the domains that A and B have in common.
132 unsigned common = A->getCommonDomains(B->AvailableDomains);
133 if (!common)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000134 return false;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000135 A->AvailableDomains = common;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000136 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000137
138 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000139 B->clear();
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000140 // All uses of B are referred to A.
141 B->Next = retain(A);
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000142
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000143 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000144 assert(!LiveRegs.empty() && "no space allocated for live registers");
Marina Yatsina273d35d2018-01-22 10:06:01 +0000145 if (LiveRegs[rx] == B)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000146 setLiveReg(rx, A);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000147 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000148 return true;
149}
150
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000151void ExecutionDomainFix::enterBasicBlock(
152 const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
153
154 MachineBasicBlock *MBB = TraversedMBB.MBB;
155
156 // Set up LiveRegs to represent registers entering MBB.
Marina Yatsina273d35d2018-01-22 10:06:01 +0000157 // Set default domain values to 'no domain' (nullptr)
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000158 if (LiveRegs.empty())
Marina Yatsina273d35d2018-01-22 10:06:01 +0000159 LiveRegs.assign(NumRegs, nullptr);
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000160
161 // This is the entry block.
162 if (MBB->pred_empty()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000163 LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << ": entry\n");
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000164 return;
165 }
166
167 // Try to coalesce live-out registers from predecessors.
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000168 for (MachineBasicBlock *pred : MBB->predecessors()) {
Marina Yatsinae4d63a42018-01-22 13:24:10 +0000169 assert(unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000170 "Should have pre-allocated MBBInfos for all MBBs");
171 LiveRegsDVInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
Keno Fischer578cf7a2017-01-30 23:37:03 +0000172 // Incoming is null if this is a backedge from a BB
173 // we haven't processed yet
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000174 if (Incoming.empty())
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000175 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000176
177 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Marina Yatsina273d35d2018-01-22 10:06:01 +0000178 DomainValue *pdv = resolve(Incoming[rx]);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000179 if (!pdv)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000180 continue;
Marina Yatsina273d35d2018-01-22 10:06:01 +0000181 if (!LiveRegs[rx]) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000182 setLiveReg(rx, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000183 continue;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000184 }
Chris Lattner503a0ef2010-03-31 20:32:51 +0000185
186 // We have a live DomainValue from more than one predecessor.
Marina Yatsina273d35d2018-01-22 10:06:01 +0000187 if (LiveRegs[rx]->isCollapsed()) {
Eric Christopher650c8f22014-05-20 17:11:11 +0000188 // We are already collapsed, but predecessor is not. Force it.
Marina Yatsina273d35d2018-01-22 10:06:01 +0000189 unsigned Domain = LiveRegs[rx]->getFirstDomain();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000190 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
191 collapse(pdv, Domain);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000192 continue;
193 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000194
Chris Lattner503a0ef2010-03-31 20:32:51 +0000195 // Currently open, merge in predecessor.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000196 if (!pdv->isCollapsed())
Marina Yatsina273d35d2018-01-22 10:06:01 +0000197 merge(LiveRegs[rx], pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000198 else
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000199 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000200 }
201 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000202 LLVM_DEBUG(dbgs() << printMBBReference(*MBB)
203 << (!TraversedMBB.IsDone ? ": incomplete\n"
204 : ": all preds known\n"));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000205}
206
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000207void ExecutionDomainFix::leaveBasicBlock(
208 const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000209 assert(!LiveRegs.empty() && "Must enter basic block first.");
Marina Yatsinae4d63a42018-01-22 13:24:10 +0000210 unsigned MBBNumber = TraversedMBB.MBB->getNumber();
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000211 assert(MBBNumber < MBBOutRegsInfos.size() &&
212 "Unexpected basic block number.");
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000213 // Save register clearances at end of MBB - used by enterBasicBlock().
Marina Yatsina273d35d2018-01-22 10:06:01 +0000214 for (DomainValue *OldLiveReg : MBBOutRegsInfos[MBBNumber]) {
215 release(OldLiveReg);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000216 }
Marina Yatsina273d35d2018-01-22 10:06:01 +0000217 MBBOutRegsInfos[MBBNumber] = LiveRegs;
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000218 LiveRegs.clear();
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000219}
220
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000221bool ExecutionDomainFix::visitInstr(MachineInstr *MI) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000222 // Update instructions with explicit execution domains.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000223 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(*MI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000224 if (DomP.first) {
225 if (DomP.second)
226 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000227 else
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000228 visitHardInstr(MI, DomP.first);
229 }
230
Keno Fischer578cf7a2017-01-30 23:37:03 +0000231 return !DomP.first;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000232}
233
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000234void ExecutionDomainFix::processDefs(MachineInstr *MI, bool Kill) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000235 assert(!MI->isDebugInstr() && "Won't process debug values");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000236 const MCInstrDesc &MCID = MI->getDesc();
237 for (unsigned i = 0,
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000238 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
239 i != e; ++i) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000240 MachineOperand &MO = MI->getOperand(i);
241 if (!MO.isReg())
242 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000243 if (MO.isUse())
244 continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000245 for (int rx : regIndices(MO.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000246 // This instruction explicitly defines rx.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000247 LLVM_DEBUG(dbgs() << printReg(RC->getRegister(rx), TRI) << ":\t" << *MI);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000248
Matthias Braun8142efa2014-12-17 19:13:47 +0000249 // Kill off domains redefined by generic instructions.
250 if (Kill)
251 kill(rx);
252 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000253 }
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000254}
255
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000256void ExecutionDomainFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000257 // Collapse all uses.
258 for (unsigned i = mi->getDesc().getNumDefs(),
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000259 e = mi->getDesc().getNumOperands();
260 i != e; ++i) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000261 MachineOperand &mo = mi->getOperand(i);
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000262 if (!mo.isReg())
263 continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000264 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000265 force(rx, domain);
266 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000267 }
268
269 // Kill all defs and force them.
270 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
271 MachineOperand &mo = mi->getOperand(i);
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000272 if (!mo.isReg())
273 continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000274 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000275 kill(rx);
276 force(rx, domain);
277 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000278 }
279}
280
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000281void ExecutionDomainFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000282 // Bitmask of available domains for this instruction after taking collapsed
283 // operands into account.
284 unsigned available = mask;
285
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000286 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000287 SmallVector<int, 4> used;
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000288 if (!LiveRegs.empty())
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000289 for (unsigned i = mi->getDesc().getNumDefs(),
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000290 e = mi->getDesc().getNumOperands();
291 i != e; ++i) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000292 MachineOperand &mo = mi->getOperand(i);
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000293 if (!mo.isReg())
294 continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000295 for (int rx : regIndices(mo.getReg())) {
Marina Yatsina273d35d2018-01-22 10:06:01 +0000296 DomainValue *dv = LiveRegs[rx];
Matthias Braun8142efa2014-12-17 19:13:47 +0000297 if (dv == nullptr)
298 continue;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000299 // Bitmask of domains that dv and available have in common.
300 unsigned common = dv->getCommonDomains(available);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000301 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000302 if (dv->isCollapsed()) {
303 // Restrict available domains to the ones in common with the operand.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000304 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000305 // penalty for this operand.
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000306 if (common)
307 available = common;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000308 } else if (common)
309 // Open DomainValue is compatible, save it for merging.
Chris Lattner503a0ef2010-03-31 20:32:51 +0000310 used.push_back(rx);
311 else
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000312 // Open DomainValue is not compatible with instruction. It is useless
313 // now.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000314 kill(rx);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000315 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000316 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000317
318 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000319 if (isPowerOf2_32(available)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000320 unsigned domain = countTrailingZeros(available);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000321 TII->setExecutionDomain(*mi, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000322 visitHardInstr(mi, domain);
323 return;
324 }
325
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000326 // Kill off any remaining uses that don't match available, and build a list of
327 // incoming DomainValues that we want to merge.
Marina Yatsina273d35d2018-01-22 10:06:01 +0000328 SmallVector<int, 4> Regs;
Craig Topperc446b1f2017-02-24 06:38:24 +0000329 for (int rx : used) {
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000330 assert(!LiveRegs.empty() && "no space allocated for live registers");
Marina Yatsina273d35d2018-01-22 10:06:01 +0000331 DomainValue *&LR = LiveRegs[rx];
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000332 // This useless DomainValue could have been missed above.
Marina Yatsina273d35d2018-01-22 10:06:01 +0000333 if (!LR->getCommonDomains(available)) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000334 kill(rx);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000335 continue;
336 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000337 // Sorted insertion.
Marina Yatsina273d35d2018-01-22 10:06:01 +0000338 // Enables giving priority to the latest domains during merging.
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000339 auto I = std::upper_bound(
340 Regs.begin(), Regs.end(), rx, [&](int LHS, const int RHS) {
341 return RDA->getReachingDef(mi, RC->getRegister(LHS)) <
342 RDA->getReachingDef(mi, RC->getRegister(RHS));
343 });
Marina Yatsina273d35d2018-01-22 10:06:01 +0000344 Regs.insert(I, rx);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000345 }
346
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000347 // doms are now sorted in order of appearance. Try to merge them all, giving
348 // priority to the latest ones.
Craig Topperc0196b12014-04-14 00:51:57 +0000349 DomainValue *dv = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000350 while (!Regs.empty()) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000351 if (!dv) {
Marina Yatsina273d35d2018-01-22 10:06:01 +0000352 dv = LiveRegs[Regs.pop_back_val()];
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000353 // Force the first dv to match the current instruction.
354 dv->AvailableDomains = dv->getCommonDomains(available);
355 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner503a0ef2010-03-31 20:32:51 +0000356 continue;
357 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000358
Marina Yatsina273d35d2018-01-22 10:06:01 +0000359 DomainValue *Latest = LiveRegs[Regs.pop_back_val()];
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000360 // Skip already merged values.
361 if (Latest == dv || Latest->Next)
362 continue;
363 if (merge(dv, Latest))
364 continue;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000365
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000366 // If latest didn't merge, it is useless now. Kill all registers using it.
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000367 for (int i : used) {
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000368 assert(!LiveRegs.empty() && "no space allocated for live registers");
Marina Yatsina273d35d2018-01-22 10:06:01 +0000369 if (LiveRegs[i] == Latest)
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000370 kill(i);
371 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000372 }
373
374 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000375 if (!dv) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000376 dv = alloc();
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000377 dv->AvailableDomains = available;
378 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000379 dv->Instrs.push_back(mi);
380
Silviu Baranga3c314992012-10-03 08:29:36 +0000381 // Finally set all defs and non-collapsed uses to dv. We must iterate through
382 // all the operators, including imp-def ones.
Marina Yatsina6b34e632018-01-22 10:05:37 +0000383 for (MachineOperand &mo : mi->operands()) {
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000384 if (!mo.isReg())
385 continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000386 for (int rx : regIndices(mo.getReg())) {
Marina Yatsina273d35d2018-01-22 10:06:01 +0000387 if (!LiveRegs[rx] || (mo.isDef() && LiveRegs[rx] != dv)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000388 kill(rx);
389 setLiveReg(rx, dv);
390 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000391 }
392 }
393}
394
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000395void ExecutionDomainFix::processBasicBlock(
396 const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
397 enterBasicBlock(TraversedMBB);
Keno Fischer578cf7a2017-01-30 23:37:03 +0000398 // If this block is not done, it makes little sense to make any decisions
399 // based on clearance information. We need to make a second pass anyway,
400 // and by then we'll have better information, so we can avoid doing the work
401 // to try and break dependencies now.
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000402 for (MachineInstr &MI : *TraversedMBB.MBB) {
Shiva Chen801bf7e2018-05-09 02:42:00 +0000403 if (!MI.isDebugInstr()) {
Keno Fischer578cf7a2017-01-30 23:37:03 +0000404 bool Kill = false;
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000405 if (TraversedMBB.PrimaryPass)
Keno Fischer578cf7a2017-01-30 23:37:03 +0000406 Kill = visitInstr(&MI);
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000407 processDefs(&MI, Kill);
Keno Fischer578cf7a2017-01-30 23:37:03 +0000408 }
409 }
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000410 leaveBasicBlock(TraversedMBB);
Keno Fischer578cf7a2017-01-30 23:37:03 +0000411}
412
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000413bool ExecutionDomainFix::runOnMachineFunction(MachineFunction &mf) {
414 if (skipFunction(mf.getFunction()))
415 return false;
416 MF = &mf;
417 TII = MF->getSubtarget().getInstrInfo();
418 TRI = MF->getSubtarget().getRegisterInfo();
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000419 LiveRegs.clear();
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000420 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
421
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000422 LLVM_DEBUG(dbgs() << "********** FIX EXECUTION DOMAIN: "
423 << TRI->getRegClassName(RC) << " **********\n");
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000424
425 // If no relevant registers are used in the function, we can skip it
426 // completely.
427 bool anyregs = false;
428 const MachineRegisterInfo &MRI = mf.getRegInfo();
429 for (unsigned Reg : *RC) {
430 if (MRI.isPhysRegUsed(Reg)) {
431 anyregs = true;
432 break;
433 }
434 }
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000435 if (!anyregs)
436 return false;
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000437
Marina Yatsina6b34e632018-01-22 10:05:37 +0000438 RDA = &getAnalysis<ReachingDefAnalysis>();
439
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000440 // Initialize the AliasMap on the first use.
441 if (AliasMap.empty()) {
442 // Given a PhysReg, AliasMap[PhysReg] returns a list of indices into RC and
443 // therefore the LiveRegs array.
444 AliasMap.resize(TRI->getNumRegs());
445 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000446 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true); AI.isValid();
447 ++AI)
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000448 AliasMap[*AI].push_back(i);
449 }
450
451 // Initialize the MBBOutRegsInfos
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000452 MBBOutRegsInfos.resize(mf.getNumBlockIDs());
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000453
454 // Traverse the basic blocks.
455 LoopTraversal Traversal;
Marina Yatsina6b34e632018-01-22 10:05:37 +0000456 LoopTraversal::TraversalOrder TraversedMBBOrder = Traversal.traverse(mf);
457 for (LoopTraversal::TraversedMBBInfo TraversedMBB : TraversedMBBOrder) {
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000458 processBasicBlock(TraversedMBB);
459 }
460
Marina Yatsinac757c0b2018-01-22 10:06:18 +0000461 for (LiveRegsDVInfo OutLiveRegs : MBBOutRegsInfos) {
Marina Yatsina273d35d2018-01-22 10:06:01 +0000462 for (DomainValue *OutLiveReg : OutLiveRegs) {
463 if (OutLiveReg)
464 release(OutLiveReg);
Marina Yatsina6daa2d22018-01-22 10:05:53 +0000465 }
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000466 }
Marina Yatsina6fc2aaa2018-01-22 10:05:23 +0000467 MBBOutRegsInfos.clear();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000468 Avail.clear();
469 Allocator.DestroyAll();
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000470
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000471 return false;
472}