blob: 8d8f5d452df01e28643417652c33be1b9ba59de2 [file] [log] [blame]
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +00001//===- SSEDomainFix.cpp - Use proper int/float domain for SSE ---*- C++ -*-===//
2//
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 contains the SSEDomainFix pass.
11//
12// Some SSE instructions like mov, and, or, xor are available in different
13// variants for different operand types. These variant instructions are
14// equivalent, but on Nehalem and newer cpus there is extra latency
15// transferring data between integer and floating point domains.
16//
17// This pass changes the variant instructions to minimize domain crossings.
18//
19//===----------------------------------------------------------------------===//
20
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +000021#define DEBUG_TYPE "execution-fix"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +000024#include "llvm/CodeGen/Passes.h"
25#include "llvm/Target/TargetInstrInfo.h"
26#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000027#include "llvm/ADT/DepthFirstIterator.h"
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000028#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000031using namespace llvm;
32
Chris Lattner563d83f2010-03-31 20:32:51 +000033/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000034/// of execution domains.
35///
36/// An open DomainValue represents a set of instructions that can still switch
37/// execution domain. Multiple registers may refer to the same open
38/// DomainValue - they will eventually be collapsed to the same execution
39/// domain.
40///
41/// A collapsed DomainValue represents a single register that has been forced
42/// into one of more execution domains. There is a separate collapsed
43/// DomainValue for each register, but it may contain multiple execution
44/// domains. A register value is initially created in a single execution
45/// domain, but if we were forced to pay the penalty of a domain crossing, we
46/// keep track of the fact the the register is now available in multiple
47/// domains.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000048namespace {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000049struct DomainValue {
50 // Basic reference counting.
51 unsigned Refs;
52
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000053 // Bitmask of available domains. For an open DomainValue, it is the still
54 // possible domains for collapsing. For a collapsed DomainValue it is the
55 // domains where the register is available for free.
56 unsigned AvailableDomains;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000057
58 // Position of the last defining instruction.
59 unsigned Dist;
60
61 // Twiddleable instructions using or defining these registers.
62 SmallVector<MachineInstr*, 8> Instrs;
63
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000064 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000065 // track of the domains where the registers are already available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000066 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000067
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000068 // Is domain available?
69 bool hasDomain(unsigned domain) const {
70 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000071 }
72
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000073 // Mark domain as available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000074 void addDomain(unsigned domain) {
75 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000076 }
77
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000078 // Restrict to a single domain available.
79 void setSingleDomain(unsigned domain) {
80 AvailableDomains = 1u << domain;
81 }
82
83 // Return bitmask of domains that are available and in mask.
84 unsigned getCommonDomains(unsigned mask) const {
85 return AvailableDomains & mask;
86 }
87
88 // First domain available.
89 unsigned getFirstDomain() const {
90 return CountTrailingZeros_32(AvailableDomains);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000091 }
92
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000093 DomainValue() { clear(); }
94
95 void clear() {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000096 Refs = AvailableDomains = Dist = 0;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000097 Instrs.clear();
98 }
99};
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000100}
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000101
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000102namespace {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000103class SSEDomainFixPass : public MachineFunctionPass {
104 static char ID;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000105 SpecificBumpPtrAllocator<DomainValue> Allocator;
106 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000107
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000108 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000109 MachineFunction *MF;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000110 const TargetInstrInfo *TII;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000111 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000112 MachineBasicBlock *MBB;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000113 std::vector<int> AliasMap;
114 const unsigned NumRegs;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000115 DomainValue **LiveRegs;
116 typedef DenseMap<MachineBasicBlock*,DomainValue**> LiveOutMap;
117 LiveOutMap LiveOuts;
118 unsigned Distance;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000119
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000120public:
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000121 SSEDomainFixPass(const TargetRegisterClass *rc)
122 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000123
124 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
125 AU.setPreservesAll();
126 MachineFunctionPass::getAnalysisUsage(AU);
127 }
128
129 virtual bool runOnMachineFunction(MachineFunction &MF);
130
131 virtual const char *getPassName() const {
132 return "SSE execution domain fixup";
133 }
134
135private:
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000136 // Register mapping.
137 int RegIndex(unsigned Reg);
138
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000139 // DomainValue allocation.
140 DomainValue *Alloc(int domain = -1);
141 void Recycle(DomainValue*);
142
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000143 // LiveRegs manipulations.
144 void SetLiveReg(int rx, DomainValue *DV);
145 void Kill(int rx);
146 void Force(int rx, unsigned domain);
147 void Collapse(DomainValue *dv, unsigned domain);
148 bool Merge(DomainValue *A, DomainValue *B);
149
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000150 void enterBasicBlock();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000151 void visitGenericInstr(MachineInstr*);
152 void visitSoftInstr(MachineInstr*, unsigned mask);
153 void visitHardInstr(MachineInstr*, unsigned domain);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000154};
155}
156
157char SSEDomainFixPass::ID = 0;
158
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000159/// Translate TRI register number to an index into our smaller tables of
160/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000161int SSEDomainFixPass::RegIndex(unsigned Reg) {
162 assert(Reg < AliasMap.size() && "Invalid register");
163 return AliasMap[Reg];
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000164}
165
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000166DomainValue *SSEDomainFixPass::Alloc(int domain) {
167 DomainValue *dv = Avail.empty() ?
168 new(Allocator.Allocate()) DomainValue :
169 Avail.pop_back_val();
170 dv->Dist = Distance;
171 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000172 dv->addDomain(domain);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000173 return dv;
174}
175
176void SSEDomainFixPass::Recycle(DomainValue *dv) {
177 assert(dv && "Cannot recycle NULL");
178 dv->clear();
179 Avail.push_back(dv);
180}
181
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000182/// Set LiveRegs[rx] = dv, updating reference counts.
183void SSEDomainFixPass::SetLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000184 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000185 if (!LiveRegs) {
186 LiveRegs = new DomainValue*[NumRegs];
187 std::fill(LiveRegs, LiveRegs+NumRegs, (DomainValue*)0);
188 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000189
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000190 if (LiveRegs[rx] == dv)
191 return;
192 if (LiveRegs[rx]) {
193 assert(LiveRegs[rx]->Refs && "Bad refcount");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000194 if (--LiveRegs[rx]->Refs == 0) Recycle(LiveRegs[rx]);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000195 }
196 LiveRegs[rx] = dv;
197 if (dv) ++dv->Refs;
198}
199
200// Kill register rx, recycle or collapse any DomainValue.
201void SSEDomainFixPass::Kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000202 assert(unsigned(rx) < NumRegs && "Invalid index");
203 if (!LiveRegs || !LiveRegs[rx]) return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000204
205 // Before killing the last reference to an open DomainValue, collapse it to
206 // the first available domain.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000207 if (LiveRegs[rx]->Refs == 1 && !LiveRegs[rx]->isCollapsed())
208 Collapse(LiveRegs[rx], LiveRegs[rx]->getFirstDomain());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000209 else
210 SetLiveReg(rx, 0);
211}
212
213/// Force register rx into domain.
214void SSEDomainFixPass::Force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000215 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesend77f8182010-03-30 00:09:32 +0000216 DomainValue *dv;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000217 if (LiveRegs && (dv = LiveRegs[rx])) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000218 if (dv->isCollapsed())
219 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000220 else if (dv->hasDomain(domain))
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000221 Collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000222 else {
223 // This is an incompatible open DomainValue. Collapse it to whatever and force
224 // the new value into domain. This costs a domain crossing.
225 Collapse(dv, dv->getFirstDomain());
226 assert(LiveRegs[rx] && "Not live after collapse?");
227 LiveRegs[rx]->addDomain(domain);
228 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000229 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000230 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000231 SetLiveReg(rx, Alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000232 }
233}
234
235/// Collapse open DomainValue into given domain. If there are multiple
236/// registers using dv, they each get a unique collapsed DomainValue.
237void SSEDomainFixPass::Collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000238 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000239
240 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000241 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000242 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000243 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000244
245 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000246 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000247 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000248 if (LiveRegs[rx] == dv)
249 SetLiveReg(rx, Alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000250}
251
252/// Merge - All instructions and registers in B are moved to A, and B is
253/// released.
254bool SSEDomainFixPass::Merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000255 assert(!A->isCollapsed() && "Cannot merge into collapsed");
256 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000257 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000258 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000259 // Restrict to the domains that A and B have in common.
260 unsigned common = A->getCommonDomains(B->AvailableDomains);
261 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000262 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000263 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000264 A->Dist = std::max(A->Dist, B->Dist);
265 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000266 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000267 if (LiveRegs[rx] == B)
268 SetLiveReg(rx, A);
269 return true;
270}
271
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000272void SSEDomainFixPass::enterBasicBlock() {
273 // Try to coalesce live-out registers from predecessors.
Dan Gohman81bf03e2010-04-13 16:57:55 +0000274 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000275 e = MBB->livein_end(); i != e; ++i) {
276 int rx = RegIndex(*i);
277 if (rx < 0) continue;
278 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
279 pe = MBB->pred_end(); pi != pe; ++pi) {
Jakob Stoklund Olesenb16df902010-03-31 00:40:08 +0000280 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000281 if (fi == LiveOuts.end()) continue;
282 DomainValue *pdv = fi->second[rx];
283 if (!pdv) continue;
Chris Lattner563d83f2010-03-31 20:32:51 +0000284 if (!LiveRegs || !LiveRegs[rx]) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000285 SetLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000286 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000287 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000288
289 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000290 if (LiveRegs[rx]->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000291 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000292 unsigned domain = LiveRegs[rx]->getFirstDomain();
293 if (!pdv->isCollapsed() && pdv->hasDomain(domain))
294 Collapse(pdv, domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000295 continue;
296 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000297
Chris Lattner563d83f2010-03-31 20:32:51 +0000298 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000299 if (!pdv->isCollapsed())
Chris Lattner563d83f2010-03-31 20:32:51 +0000300 Merge(LiveRegs[rx], pdv);
301 else
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000302 Force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000303 }
304 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000305}
306
307// A hard instruction only works in one domain. All input registers will be
308// forced into that domain.
309void SSEDomainFixPass::visitHardInstr(MachineInstr *mi, unsigned domain) {
310 // Collapse all uses.
311 for (unsigned i = mi->getDesc().getNumDefs(),
312 e = mi->getDesc().getNumOperands(); i != e; ++i) {
313 MachineOperand &mo = mi->getOperand(i);
314 if (!mo.isReg()) continue;
315 int rx = RegIndex(mo.getReg());
316 if (rx < 0) continue;
317 Force(rx, domain);
318 }
319
320 // Kill all defs and force them.
321 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
322 MachineOperand &mo = mi->getOperand(i);
323 if (!mo.isReg()) continue;
324 int rx = RegIndex(mo.getReg());
325 if (rx < 0) continue;
326 Kill(rx);
327 Force(rx, domain);
328 }
329}
330
331// A soft instruction can be changed to work in other domains given by mask.
332void SSEDomainFixPass::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000333 // Bitmask of available domains for this instruction after taking collapsed
334 // operands into account.
335 unsigned available = mask;
336
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000337 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000338 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000339 if (LiveRegs)
340 for (unsigned i = mi->getDesc().getNumDefs(),
341 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000342 MachineOperand &mo = mi->getOperand(i);
343 if (!mo.isReg()) continue;
344 int rx = RegIndex(mo.getReg());
345 if (rx < 0) continue;
346 if (DomainValue *dv = LiveRegs[rx]) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000347 // Bitmask of domains that dv and available have in common.
348 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000349 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000350 if (dv->isCollapsed()) {
351 // Restrict available domains to the ones in common with the operand.
352 // If there are no common domains, we must pay the cross-domain
353 // penalty for this operand.
354 if (common) available = common;
355 } else if (common)
356 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000357 used.push_back(rx);
358 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000359 // Open DomainValue is not compatible with instruction. It is useless
360 // now.
Chris Lattner563d83f2010-03-31 20:32:51 +0000361 Kill(rx);
362 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000363 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000364
365 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000366 if (isPowerOf2_32(available)) {
367 unsigned domain = CountTrailingZeros_32(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000368 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000369 visitHardInstr(mi, domain);
370 return;
371 }
372
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000373 // Kill off any remaining uses that don't match available, and build a list of
374 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000375 SmallVector<DomainValue*,4> doms;
376 for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
377 int rx = *i;
378 DomainValue *dv = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000379 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000380 if (!dv->getCommonDomains(available)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000381 Kill(*i);
382 continue;
383 }
384 // sorted, uniqued insert.
385 bool inserted = false;
386 for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end();
387 i != e && !inserted; ++i) {
388 if (dv == *i)
389 inserted = true;
390 else if (dv->Dist < (*i)->Dist) {
391 inserted = true;
392 doms.insert(i, dv);
393 }
394 }
395 if (!inserted)
396 doms.push_back(dv);
397 }
398
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000399 // doms are now sorted in order of appearance. Try to merge them all, giving
400 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000401 DomainValue *dv = 0;
402 while (!doms.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000403 if (!dv) {
404 dv = doms.pop_back_val();
405 continue;
406 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000407
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000408 DomainValue *latest = doms.pop_back_val();
409 if (Merge(dv, latest)) continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000410
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000411 // If latest didn't merge, it is useless now. Kill all registers using it.
Chris Lattner563d83f2010-03-31 20:32:51 +0000412 for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000413 if (LiveRegs[*i] == latest)
Chris Lattner563d83f2010-03-31 20:32:51 +0000414 Kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000415 }
416
417 // dv is the DomainValue we are going to use for this instruction.
418 if (!dv)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000419 dv = Alloc();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000420 dv->Dist = Distance;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000421 dv->AvailableDomains = available;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000422 dv->Instrs.push_back(mi);
423
424 // Finally set all defs and non-collapsed uses to dv.
425 for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
426 MachineOperand &mo = mi->getOperand(i);
427 if (!mo.isReg()) continue;
428 int rx = RegIndex(mo.getReg());
429 if (rx < 0) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000430 if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000431 Kill(rx);
432 SetLiveReg(rx, dv);
433 }
434 }
435}
436
437void SSEDomainFixPass::visitGenericInstr(MachineInstr *mi) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000438 // Process explicit defs, kill any XMM registers redefined.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000439 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
440 MachineOperand &mo = mi->getOperand(i);
441 if (!mo.isReg()) continue;
442 int rx = RegIndex(mo.getReg());
443 if (rx < 0) continue;
444 Kill(rx);
445 }
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000446}
447
448bool SSEDomainFixPass::runOnMachineFunction(MachineFunction &mf) {
449 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000450 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000451 TRI = MF->getTarget().getRegisterInfo();
452 MBB = 0;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000453 LiveRegs = 0;
454 Distance = 0;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000455 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000456
457 // If no XMM registers are used in the function, we can skip it completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000458 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000459 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
460 I != E; ++I)
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000461 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000462 anyregs = true;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000463 break;
464 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000465 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000466
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000467 // Initialize the AliasMap on the first use.
468 if (AliasMap.empty()) {
469 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
470 // or -1.
471 AliasMap.resize(TRI->getNumRegs(), -1);
472 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
473 for (const unsigned *AI = TRI->getOverlaps(RC->getRegister(i)); *AI; ++AI)
474 AliasMap[*AI] = i;
475 }
476
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000477 MachineBasicBlock *Entry = MF->begin();
478 SmallPtrSet<MachineBasicBlock*, 16> Visited;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000479 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 16> >
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000480 DFI = df_ext_begin(Entry, Visited), DFE = df_ext_end(Entry, Visited);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000481 DFI != DFE; ++DFI) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000482 MBB = *DFI;
483 enterBasicBlock();
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000484 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
485 ++I) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000486 MachineInstr *mi = I;
487 if (mi->isDebugValue()) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000488 ++Distance;
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000489 std::pair<uint16_t, uint16_t> domp = TII->getExecutionDomain(mi);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000490 if (domp.first)
491 if (domp.second)
492 visitSoftInstr(mi, domp.second);
493 else
494 visitHardInstr(mi, domp.first);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000495 else if (LiveRegs)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000496 visitGenericInstr(mi);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000497 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000498
499 // Save live registers at end of MBB - used by enterBasicBlock().
500 if (LiveRegs)
501 LiveOuts.insert(std::make_pair(MBB, LiveRegs));
502 LiveRegs = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000503 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000504
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000505 // Clear the LiveOuts vectors. Should we also collapse any remaining
506 // DomainValues?
507 for (LiveOutMap::const_iterator i = LiveOuts.begin(), e = LiveOuts.end();
508 i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000509 delete[] i->second;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000510 LiveOuts.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000511 Avail.clear();
512 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000513
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000514 return false;
515}
516
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000517FunctionPass *
518llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
519 return new SSEDomainFixPass(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000520}