blob: 4b546768b4f156765ec05d2c5b8e4fbdaf6f34e9 [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
21#define DEBUG_TYPE "sse-domain-fix"
22#include "X86InstrInfo.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/ADT/DepthFirstIterator.h"
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000026#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000029using namespace llvm;
30
Chris Lattner563d83f2010-03-31 20:32:51 +000031/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000032/// of execution domains.
33///
34/// An open DomainValue represents a set of instructions that can still switch
35/// execution domain. Multiple registers may refer to the same open
36/// DomainValue - they will eventually be collapsed to the same execution
37/// domain.
38///
39/// A collapsed DomainValue represents a single register that has been forced
40/// into one of more execution domains. There is a separate collapsed
41/// DomainValue for each register, but it may contain multiple execution
42/// domains. A register value is initially created in a single execution
43/// domain, but if we were forced to pay the penalty of a domain crossing, we
44/// keep track of the fact the the register is now available in multiple
45/// domains.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000046namespace {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000047struct DomainValue {
48 // Basic reference counting.
49 unsigned Refs;
50
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000051 // Bitmask of available domains. For an open DomainValue, it is the still
52 // possible domains for collapsing. For a collapsed DomainValue it is the
53 // domains where the register is available for free.
54 unsigned AvailableDomains;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000055
56 // Position of the last defining instruction.
57 unsigned Dist;
58
59 // Twiddleable instructions using or defining these registers.
60 SmallVector<MachineInstr*, 8> Instrs;
61
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000062 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000063 // track of the domains where the registers are already available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000064 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000065
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000066 // Is domain available?
67 bool hasDomain(unsigned domain) const {
68 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000069 }
70
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000071 // Mark domain as available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000072 void addDomain(unsigned domain) {
73 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000074 }
75
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000076 // Restrict to a single domain available.
77 void setSingleDomain(unsigned domain) {
78 AvailableDomains = 1u << domain;
79 }
80
81 // Return bitmask of domains that are available and in mask.
82 unsigned getCommonDomains(unsigned mask) const {
83 return AvailableDomains & mask;
84 }
85
86 // First domain available.
87 unsigned getFirstDomain() const {
88 return CountTrailingZeros_32(AvailableDomains);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000089 }
90
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000091 DomainValue() { clear(); }
92
93 void clear() {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000094 Refs = AvailableDomains = Dist = 0;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000095 Instrs.clear();
96 }
97};
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000098}
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000099
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000100static const unsigned NumRegs = 16;
101
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
108 MachineFunction *MF;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000109 const X86InstrInfo *TII;
110 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000111 MachineBasicBlock *MBB;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000112 DomainValue **LiveRegs;
113 typedef DenseMap<MachineBasicBlock*,DomainValue**> LiveOutMap;
114 LiveOutMap LiveOuts;
115 unsigned Distance;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000116
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000117public:
118 SSEDomainFixPass() : MachineFunctionPass(&ID) {}
119
120 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
121 AU.setPreservesAll();
122 MachineFunctionPass::getAnalysisUsage(AU);
123 }
124
125 virtual bool runOnMachineFunction(MachineFunction &MF);
126
127 virtual const char *getPassName() const {
128 return "SSE execution domain fixup";
129 }
130
131private:
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000132 // Register mapping.
133 int RegIndex(unsigned Reg);
134
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000135 // DomainValue allocation.
136 DomainValue *Alloc(int domain = -1);
137 void Recycle(DomainValue*);
138
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000139 // LiveRegs manipulations.
140 void SetLiveReg(int rx, DomainValue *DV);
141 void Kill(int rx);
142 void Force(int rx, unsigned domain);
143 void Collapse(DomainValue *dv, unsigned domain);
144 bool Merge(DomainValue *A, DomainValue *B);
145
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000146 void enterBasicBlock();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000147 void visitGenericInstr(MachineInstr*);
148 void visitSoftInstr(MachineInstr*, unsigned mask);
149 void visitHardInstr(MachineInstr*, unsigned domain);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000150};
151}
152
153char SSEDomainFixPass::ID = 0;
154
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000155/// Translate TRI register number to an index into our smaller tables of
156/// interesting registers. Return -1 for boring registers.
157int SSEDomainFixPass::RegIndex(unsigned reg) {
158 // Registers are sorted lexicographically.
159 // We just need them to be consecutive, ordering doesn't matter.
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000160 assert(X86::XMM9 == X86::XMM0+NumRegs-1 && "Unexpected sort");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000161 reg -= X86::XMM0;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000162 return reg < NumRegs ? reg : -1;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000163}
164
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000165DomainValue *SSEDomainFixPass::Alloc(int domain) {
166 DomainValue *dv = Avail.empty() ?
167 new(Allocator.Allocate()) DomainValue :
168 Avail.pop_back_val();
169 dv->Dist = Distance;
170 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000171 dv->addDomain(domain);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000172 return dv;
173}
174
175void SSEDomainFixPass::Recycle(DomainValue *dv) {
176 assert(dv && "Cannot recycle NULL");
177 dv->clear();
178 Avail.push_back(dv);
179}
180
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000181/// Set LiveRegs[rx] = dv, updating reference counts.
182void SSEDomainFixPass::SetLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000183 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000184 if (!LiveRegs) {
185 LiveRegs = new DomainValue*[NumRegs];
186 std::fill(LiveRegs, LiveRegs+NumRegs, (DomainValue*)0);
187 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000188
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000189 if (LiveRegs[rx] == dv)
190 return;
191 if (LiveRegs[rx]) {
192 assert(LiveRegs[rx]->Refs && "Bad refcount");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000193 if (--LiveRegs[rx]->Refs == 0) Recycle(LiveRegs[rx]);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000194 }
195 LiveRegs[rx] = dv;
196 if (dv) ++dv->Refs;
197}
198
199// Kill register rx, recycle or collapse any DomainValue.
200void SSEDomainFixPass::Kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000201 assert(unsigned(rx) < NumRegs && "Invalid index");
202 if (!LiveRegs || !LiveRegs[rx]) return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000203
204 // Before killing the last reference to an open DomainValue, collapse it to
205 // the first available domain.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000206 if (LiveRegs[rx]->Refs == 1 && !LiveRegs[rx]->isCollapsed())
207 Collapse(LiveRegs[rx], LiveRegs[rx]->getFirstDomain());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000208 else
209 SetLiveReg(rx, 0);
210}
211
212/// Force register rx into domain.
213void SSEDomainFixPass::Force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000214 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesend77f8182010-03-30 00:09:32 +0000215 DomainValue *dv;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000216 if (LiveRegs && (dv = LiveRegs[rx])) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000217 if (dv->isCollapsed())
218 dv->addDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000219 else
220 Collapse(dv, domain);
221 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000222 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000223 SetLiveReg(rx, Alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000224 }
225}
226
227/// Collapse open DomainValue into given domain. If there are multiple
228/// registers using dv, they each get a unique collapsed DomainValue.
229void SSEDomainFixPass::Collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000230 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000231
232 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000233 while (!dv->Instrs.empty())
234 TII->SetSSEDomain(dv->Instrs.pop_back_val(), domain);
235 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000236
237 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000238 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000239 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000240 if (LiveRegs[rx] == dv)
241 SetLiveReg(rx, Alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000242}
243
244/// Merge - All instructions and registers in B are moved to A, and B is
245/// released.
246bool SSEDomainFixPass::Merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000247 assert(!A->isCollapsed() && "Cannot merge into collapsed");
248 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000249 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000250 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000251 // Restrict to the domains that A and B have in common.
252 unsigned common = A->getCommonDomains(B->AvailableDomains);
253 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000254 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000255 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000256 A->Dist = std::max(A->Dist, B->Dist);
257 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000258 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000259 if (LiveRegs[rx] == B)
260 SetLiveReg(rx, A);
261 return true;
262}
263
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000264void SSEDomainFixPass::enterBasicBlock() {
265 // Try to coalesce live-out registers from predecessors.
266 for (MachineBasicBlock::const_livein_iterator i = MBB->livein_begin(),
267 e = MBB->livein_end(); i != e; ++i) {
268 int rx = RegIndex(*i);
269 if (rx < 0) continue;
270 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
271 pe = MBB->pred_end(); pi != pe; ++pi) {
Jakob Stoklund Olesenb16df902010-03-31 00:40:08 +0000272 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000273 if (fi == LiveOuts.end()) continue;
274 DomainValue *pdv = fi->second[rx];
275 if (!pdv) continue;
Chris Lattner563d83f2010-03-31 20:32:51 +0000276 if (!LiveRegs || !LiveRegs[rx]) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000277 SetLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000278 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000279 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000280
281 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000282 if (LiveRegs[rx]->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000283 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000284 if (!pdv->isCollapsed())
285 Collapse(pdv, LiveRegs[rx]->getFirstDomain());
Chris Lattner563d83f2010-03-31 20:32:51 +0000286 continue;
287 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000288
Chris Lattner563d83f2010-03-31 20:32:51 +0000289 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000290 if (!pdv->isCollapsed())
Chris Lattner563d83f2010-03-31 20:32:51 +0000291 Merge(LiveRegs[rx], pdv);
292 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000293 Collapse(LiveRegs[rx], pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000294 }
295 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000296}
297
298// A hard instruction only works in one domain. All input registers will be
299// forced into that domain.
300void SSEDomainFixPass::visitHardInstr(MachineInstr *mi, unsigned domain) {
301 // Collapse all uses.
302 for (unsigned i = mi->getDesc().getNumDefs(),
303 e = mi->getDesc().getNumOperands(); i != e; ++i) {
304 MachineOperand &mo = mi->getOperand(i);
305 if (!mo.isReg()) continue;
306 int rx = RegIndex(mo.getReg());
307 if (rx < 0) continue;
308 Force(rx, domain);
309 }
310
311 // Kill all defs and force them.
312 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); 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 Kill(rx);
318 Force(rx, domain);
319 }
320}
321
322// A soft instruction can be changed to work in other domains given by mask.
323void SSEDomainFixPass::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000324 // Bitmask of available domains for this instruction after taking collapsed
325 // operands into account.
326 unsigned available = mask;
327
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000328 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000329 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000330 if (LiveRegs)
331 for (unsigned i = mi->getDesc().getNumDefs(),
332 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000333 MachineOperand &mo = mi->getOperand(i);
334 if (!mo.isReg()) continue;
335 int rx = RegIndex(mo.getReg());
336 if (rx < 0) continue;
337 if (DomainValue *dv = LiveRegs[rx]) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000338 // Bitmask of domains that dv and available have in common.
339 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000340 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000341 if (dv->isCollapsed()) {
342 // Restrict available domains to the ones in common with the operand.
343 // If there are no common domains, we must pay the cross-domain
344 // penalty for this operand.
345 if (common) available = common;
346 } else if (common)
347 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000348 used.push_back(rx);
349 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000350 // Open DomainValue is not compatible with instruction. It is useless
351 // now.
Chris Lattner563d83f2010-03-31 20:32:51 +0000352 Kill(rx);
353 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000354 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000355
356 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000357 if (isPowerOf2_32(available)) {
358 unsigned domain = CountTrailingZeros_32(available);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000359 TII->SetSSEDomain(mi, domain);
360 visitHardInstr(mi, domain);
361 return;
362 }
363
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000364 // Kill off any remaining uses that don't match available, and build a list of
365 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000366 SmallVector<DomainValue*,4> doms;
367 for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
368 int rx = *i;
369 DomainValue *dv = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000370 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000371 if (!dv->getCommonDomains(available)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000372 Kill(*i);
373 continue;
374 }
375 // sorted, uniqued insert.
376 bool inserted = false;
377 for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end();
378 i != e && !inserted; ++i) {
379 if (dv == *i)
380 inserted = true;
381 else if (dv->Dist < (*i)->Dist) {
382 inserted = true;
383 doms.insert(i, dv);
384 }
385 }
386 if (!inserted)
387 doms.push_back(dv);
388 }
389
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000390 // doms are now sorted in order of appearance. Try to merge them all, giving
391 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000392 DomainValue *dv = 0;
393 while (!doms.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000394 if (!dv) {
395 dv = doms.pop_back_val();
396 continue;
397 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000398
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000399 DomainValue *latest = doms.pop_back_val();
400 if (Merge(dv, latest)) continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000401
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000402 // If latest didn't merge, it is useless now. Kill all registers using it.
Chris Lattner563d83f2010-03-31 20:32:51 +0000403 for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000404 if (LiveRegs[*i] == latest)
Chris Lattner563d83f2010-03-31 20:32:51 +0000405 Kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000406 }
407
408 // dv is the DomainValue we are going to use for this instruction.
409 if (!dv)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000410 dv = Alloc();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000411 dv->Dist = Distance;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000412 dv->AvailableDomains = available;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000413 dv->Instrs.push_back(mi);
414
415 // Finally set all defs and non-collapsed uses to dv.
416 for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
417 MachineOperand &mo = mi->getOperand(i);
418 if (!mo.isReg()) continue;
419 int rx = RegIndex(mo.getReg());
420 if (rx < 0) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000421 if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000422 Kill(rx);
423 SetLiveReg(rx, dv);
424 }
425 }
426}
427
428void SSEDomainFixPass::visitGenericInstr(MachineInstr *mi) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000429 // Process explicit defs, kill any XMM registers redefined.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000430 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
431 MachineOperand &mo = mi->getOperand(i);
432 if (!mo.isReg()) continue;
433 int rx = RegIndex(mo.getReg());
434 if (rx < 0) continue;
435 Kill(rx);
436 }
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000437}
438
439bool SSEDomainFixPass::runOnMachineFunction(MachineFunction &mf) {
440 MF = &mf;
441 TII = static_cast<const X86InstrInfo*>(MF->getTarget().getInstrInfo());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000442 TRI = MF->getTarget().getRegisterInfo();
443 MBB = 0;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000444 LiveRegs = 0;
445 Distance = 0;
446 assert(NumRegs == X86::VR128RegClass.getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000447
448 // If no XMM registers are used in the function, we can skip it completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000449 bool anyregs = false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000450 for (TargetRegisterClass::const_iterator I = X86::VR128RegClass.begin(),
451 E = X86::VR128RegClass.end(); I != E; ++I)
452 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000453 anyregs = true;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000454 break;
455 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000456 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000457
458 MachineBasicBlock *Entry = MF->begin();
459 SmallPtrSet<MachineBasicBlock*, 16> Visited;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000460 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 16> >
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000461 DFI = df_ext_begin(Entry, Visited), DFE = df_ext_end(Entry, Visited);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000462 DFI != DFE; ++DFI) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000463 MBB = *DFI;
464 enterBasicBlock();
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000465 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
466 ++I) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000467 MachineInstr *mi = I;
468 if (mi->isDebugValue()) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000469 ++Distance;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000470 std::pair<uint16_t, uint16_t> domp = TII->GetSSEDomain(mi);
471 if (domp.first)
472 if (domp.second)
473 visitSoftInstr(mi, domp.second);
474 else
475 visitHardInstr(mi, domp.first);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000476 else if (LiveRegs)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000477 visitGenericInstr(mi);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000478 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000479
480 // Save live registers at end of MBB - used by enterBasicBlock().
481 if (LiveRegs)
482 LiveOuts.insert(std::make_pair(MBB, LiveRegs));
483 LiveRegs = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000484 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000485
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000486 // Clear the LiveOuts vectors. Should we also collapse any remaining
487 // DomainValues?
488 for (LiveOutMap::const_iterator i = LiveOuts.begin(), e = LiveOuts.end();
489 i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000490 delete[] i->second;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000491 LiveOuts.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000492 Avail.clear();
493 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000494
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000495 return false;
496}
497
498FunctionPass *llvm::createSSEDomainFixPass() {
499 return new SSEDomainFixPass();
500}