blob: 3786d57521f151659b6a631047183366581eed99 [file] [log] [blame]
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +00001//===- ExecutionDepsFix.cpp - Fix execution dependecy issues ----*- C++ -*-===//
Jakob Stoklund Olesen352aa502010-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//===----------------------------------------------------------------------===//
9//
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +000010// This file contains the execution dependency fix pass.
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000011//
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +000012// Some X86 SSE instructions like mov, and, or, xor are available in different
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000013// variants for different operand types. These variant instructions are
14// equivalent, but on Nehalem and newer cpus there is extra latency
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +000015// transferring data between integer and floating point domains. ARM cores
16// have similar issues when they are configured with both VFP and NEON
17// pipelines.
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000018//
19// This pass changes the variant instructions to minimize domain crossings.
20//
21//===----------------------------------------------------------------------===//
22
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +000023#define DEBUG_TYPE "execution-fix"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +000026#include "llvm/CodeGen/Passes.h"
27#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000029#include "llvm/ADT/DepthFirstIterator.h"
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000030#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000033using namespace llvm;
34
Chris Lattner563d83f2010-03-31 20:32:51 +000035/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000036/// of execution domains.
37///
38/// An open DomainValue represents a set of instructions that can still switch
39/// execution domain. Multiple registers may refer to the same open
40/// DomainValue - they will eventually be collapsed to the same execution
41/// domain.
42///
43/// A collapsed DomainValue represents a single register that has been forced
44/// into one of more execution domains. There is a separate collapsed
45/// DomainValue for each register, but it may contain multiple execution
46/// domains. A register value is initially created in a single execution
47/// domain, but if we were forced to pay the penalty of a domain crossing, we
48/// keep track of the fact the the register is now available in multiple
49/// domains.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000050namespace {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000051struct DomainValue {
52 // Basic reference counting.
53 unsigned Refs;
54
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000055 // Bitmask of available domains. For an open DomainValue, it is the still
56 // possible domains for collapsing. For a collapsed DomainValue it is the
57 // domains where the register is available for free.
58 unsigned AvailableDomains;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000059
60 // Position of the last defining instruction.
61 unsigned Dist;
62
63 // Twiddleable instructions using or defining these registers.
64 SmallVector<MachineInstr*, 8> Instrs;
65
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000066 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000067 // track of the domains where the registers are already available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000068 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000069
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000070 // Is domain available?
71 bool hasDomain(unsigned domain) const {
72 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000073 }
74
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000075 // Mark domain as available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000076 void addDomain(unsigned domain) {
77 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000078 }
79
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000080 // Restrict to a single domain available.
81 void setSingleDomain(unsigned domain) {
82 AvailableDomains = 1u << domain;
83 }
84
85 // Return bitmask of domains that are available and in mask.
86 unsigned getCommonDomains(unsigned mask) const {
87 return AvailableDomains & mask;
88 }
89
90 // First domain available.
91 unsigned getFirstDomain() const {
92 return CountTrailingZeros_32(AvailableDomains);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000093 }
94
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000095 DomainValue() { clear(); }
96
97 void clear() {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000098 Refs = AvailableDomains = Dist = 0;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000099 Instrs.clear();
100 }
101};
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000102}
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000103
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000104namespace {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000105class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000106 static char ID;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000107 SpecificBumpPtrAllocator<DomainValue> Allocator;
108 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000109
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000110 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000111 MachineFunction *MF;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000112 const TargetInstrInfo *TII;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000113 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000114 MachineBasicBlock *MBB;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000115 std::vector<int> AliasMap;
116 const unsigned NumRegs;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000117 DomainValue **LiveRegs;
118 typedef DenseMap<MachineBasicBlock*,DomainValue**> LiveOutMap;
119 LiveOutMap LiveOuts;
120 unsigned Distance;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000121
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000122public:
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000123 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000124 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000125
126 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
127 AU.setPreservesAll();
128 MachineFunctionPass::getAnalysisUsage(AU);
129 }
130
131 virtual bool runOnMachineFunction(MachineFunction &MF);
132
133 virtual const char *getPassName() const {
Jakob Stoklund Olesencd7dcad2011-11-07 21:23:39 +0000134 return "Execution dependency fix";
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000135 }
136
137private:
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000138 // Register mapping.
139 int RegIndex(unsigned Reg);
140
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000141 // DomainValue allocation.
142 DomainValue *Alloc(int domain = -1);
143 void Recycle(DomainValue*);
144
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000145 // LiveRegs manipulations.
146 void SetLiveReg(int rx, DomainValue *DV);
147 void Kill(int rx);
148 void Force(int rx, unsigned domain);
149 void Collapse(DomainValue *dv, unsigned domain);
150 bool Merge(DomainValue *A, DomainValue *B);
151
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000152 void enterBasicBlock();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000153 void visitGenericInstr(MachineInstr*);
154 void visitSoftInstr(MachineInstr*, unsigned mask);
155 void visitHardInstr(MachineInstr*, unsigned domain);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000156};
157}
158
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000159char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000160
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000161/// Translate TRI register number to an index into our smaller tables of
162/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000163int ExeDepsFix::RegIndex(unsigned Reg) {
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000164 assert(Reg < AliasMap.size() && "Invalid register");
165 return AliasMap[Reg];
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000166}
167
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000168DomainValue *ExeDepsFix::Alloc(int domain) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000169 DomainValue *dv = Avail.empty() ?
170 new(Allocator.Allocate()) DomainValue :
171 Avail.pop_back_val();
172 dv->Dist = Distance;
173 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000174 dv->addDomain(domain);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000175 return dv;
176}
177
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000178void ExeDepsFix::Recycle(DomainValue *dv) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000179 assert(dv && "Cannot recycle NULL");
180 dv->clear();
181 Avail.push_back(dv);
182}
183
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000184/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000185void ExeDepsFix::SetLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000186 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000187 if (!LiveRegs) {
188 LiveRegs = new DomainValue*[NumRegs];
189 std::fill(LiveRegs, LiveRegs+NumRegs, (DomainValue*)0);
190 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000191
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000192 if (LiveRegs[rx] == dv)
193 return;
194 if (LiveRegs[rx]) {
195 assert(LiveRegs[rx]->Refs && "Bad refcount");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000196 if (--LiveRegs[rx]->Refs == 0) Recycle(LiveRegs[rx]);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000197 }
198 LiveRegs[rx] = dv;
199 if (dv) ++dv->Refs;
200}
201
202// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000203void ExeDepsFix::Kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000204 assert(unsigned(rx) < NumRegs && "Invalid index");
205 if (!LiveRegs || !LiveRegs[rx]) return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000206
207 // Before killing the last reference to an open DomainValue, collapse it to
208 // the first available domain.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000209 if (LiveRegs[rx]->Refs == 1 && !LiveRegs[rx]->isCollapsed())
210 Collapse(LiveRegs[rx], LiveRegs[rx]->getFirstDomain());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000211 else
212 SetLiveReg(rx, 0);
213}
214
215/// Force register rx into domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000216void ExeDepsFix::Force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000217 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesend77f8182010-03-30 00:09:32 +0000218 DomainValue *dv;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000219 if (LiveRegs && (dv = LiveRegs[rx])) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000220 if (dv->isCollapsed())
221 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000222 else if (dv->hasDomain(domain))
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000223 Collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000224 else {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000225 // This is an incompatible open DomainValue. Collapse it to whatever and
226 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000227 Collapse(dv, dv->getFirstDomain());
228 assert(LiveRegs[rx] && "Not live after collapse?");
229 LiveRegs[rx]->addDomain(domain);
230 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000231 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000232 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000233 SetLiveReg(rx, Alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000234 }
235}
236
237/// Collapse open DomainValue into given domain. If there are multiple
238/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000239void ExeDepsFix::Collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000240 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000241
242 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000243 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000244 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000245 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000246
247 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000248 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000249 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000250 if (LiveRegs[rx] == dv)
251 SetLiveReg(rx, Alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000252}
253
254/// Merge - All instructions and registers in B are moved to A, and B is
255/// released.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000256bool ExeDepsFix::Merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000257 assert(!A->isCollapsed() && "Cannot merge into collapsed");
258 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000259 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000260 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000261 // Restrict to the domains that A and B have in common.
262 unsigned common = A->getCommonDomains(B->AvailableDomains);
263 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000264 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000265 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000266 A->Dist = std::max(A->Dist, B->Dist);
267 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000268 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000269 if (LiveRegs[rx] == B)
270 SetLiveReg(rx, A);
271 return true;
272}
273
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000274void ExeDepsFix::enterBasicBlock() {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000275 // Try to coalesce live-out registers from predecessors.
Dan Gohman81bf03e2010-04-13 16:57:55 +0000276 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000277 e = MBB->livein_end(); i != e; ++i) {
278 int rx = RegIndex(*i);
279 if (rx < 0) continue;
280 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
281 pe = MBB->pred_end(); pi != pe; ++pi) {
Jakob Stoklund Olesenb16df902010-03-31 00:40:08 +0000282 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000283 if (fi == LiveOuts.end()) continue;
284 DomainValue *pdv = fi->second[rx];
285 if (!pdv) continue;
Chris Lattner563d83f2010-03-31 20:32:51 +0000286 if (!LiveRegs || !LiveRegs[rx]) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000287 SetLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000288 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000289 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000290
291 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000292 if (LiveRegs[rx]->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000293 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000294 unsigned domain = LiveRegs[rx]->getFirstDomain();
295 if (!pdv->isCollapsed() && pdv->hasDomain(domain))
296 Collapse(pdv, domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000297 continue;
298 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000299
Chris Lattner563d83f2010-03-31 20:32:51 +0000300 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000301 if (!pdv->isCollapsed())
Chris Lattner563d83f2010-03-31 20:32:51 +0000302 Merge(LiveRegs[rx], pdv);
303 else
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000304 Force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000305 }
306 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000307}
308
309// A hard instruction only works in one domain. All input registers will be
310// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000311void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000312 // Collapse all uses.
313 for (unsigned i = mi->getDesc().getNumDefs(),
314 e = mi->getDesc().getNumOperands(); i != e; ++i) {
315 MachineOperand &mo = mi->getOperand(i);
316 if (!mo.isReg()) continue;
317 int rx = RegIndex(mo.getReg());
318 if (rx < 0) continue;
319 Force(rx, domain);
320 }
321
322 // Kill all defs and force them.
323 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
324 MachineOperand &mo = mi->getOperand(i);
325 if (!mo.isReg()) continue;
326 int rx = RegIndex(mo.getReg());
327 if (rx < 0) continue;
328 Kill(rx);
329 Force(rx, domain);
330 }
331}
332
333// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000334void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000335 // Bitmask of available domains for this instruction after taking collapsed
336 // operands into account.
337 unsigned available = mask;
338
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000339 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000340 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000341 if (LiveRegs)
342 for (unsigned i = mi->getDesc().getNumDefs(),
343 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000344 MachineOperand &mo = mi->getOperand(i);
345 if (!mo.isReg()) continue;
346 int rx = RegIndex(mo.getReg());
347 if (rx < 0) continue;
348 if (DomainValue *dv = LiveRegs[rx]) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000349 // Bitmask of domains that dv and available have in common.
350 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000351 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000352 if (dv->isCollapsed()) {
353 // Restrict available domains to the ones in common with the operand.
354 // If there are no common domains, we must pay the cross-domain
355 // penalty for this operand.
356 if (common) available = common;
357 } else if (common)
358 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000359 used.push_back(rx);
360 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000361 // Open DomainValue is not compatible with instruction. It is useless
362 // now.
Chris Lattner563d83f2010-03-31 20:32:51 +0000363 Kill(rx);
364 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000365 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000366
367 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000368 if (isPowerOf2_32(available)) {
369 unsigned domain = CountTrailingZeros_32(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000370 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000371 visitHardInstr(mi, domain);
372 return;
373 }
374
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000375 // Kill off any remaining uses that don't match available, and build a list of
376 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000377 SmallVector<DomainValue*,4> doms;
378 for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
379 int rx = *i;
380 DomainValue *dv = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000381 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000382 if (!dv->getCommonDomains(available)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000383 Kill(*i);
384 continue;
385 }
386 // sorted, uniqued insert.
387 bool inserted = false;
388 for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end();
389 i != e && !inserted; ++i) {
390 if (dv == *i)
391 inserted = true;
392 else if (dv->Dist < (*i)->Dist) {
393 inserted = true;
394 doms.insert(i, dv);
395 }
396 }
397 if (!inserted)
398 doms.push_back(dv);
399 }
400
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000401 // doms are now sorted in order of appearance. Try to merge them all, giving
402 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000403 DomainValue *dv = 0;
404 while (!doms.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000405 if (!dv) {
406 dv = doms.pop_back_val();
407 continue;
408 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000409
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000410 DomainValue *latest = doms.pop_back_val();
411 if (Merge(dv, latest)) continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000412
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000413 // If latest didn't merge, it is useless now. Kill all registers using it.
Chris Lattner563d83f2010-03-31 20:32:51 +0000414 for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000415 if (LiveRegs[*i] == latest)
Chris Lattner563d83f2010-03-31 20:32:51 +0000416 Kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000417 }
418
419 // dv is the DomainValue we are going to use for this instruction.
420 if (!dv)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000421 dv = Alloc();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000422 dv->Dist = Distance;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000423 dv->AvailableDomains = available;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000424 dv->Instrs.push_back(mi);
425
426 // Finally set all defs and non-collapsed uses to dv.
427 for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
428 MachineOperand &mo = mi->getOperand(i);
429 if (!mo.isReg()) continue;
430 int rx = RegIndex(mo.getReg());
431 if (rx < 0) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000432 if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000433 Kill(rx);
434 SetLiveReg(rx, dv);
435 }
436 }
437}
438
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000439void ExeDepsFix::visitGenericInstr(MachineInstr *mi) {
440 // Process explicit defs, kill any relevant registers redefined.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000441 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
442 MachineOperand &mo = mi->getOperand(i);
443 if (!mo.isReg()) continue;
444 int rx = RegIndex(mo.getReg());
445 if (rx < 0) continue;
446 Kill(rx);
447 }
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000448}
449
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000450bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000451 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000452 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000453 TRI = MF->getTarget().getRegisterInfo();
454 MBB = 0;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000455 LiveRegs = 0;
456 Distance = 0;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000457 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000458
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000459 // If no relevant registers are used in the function, we can skip it
460 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000461 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000462 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
463 I != E; ++I)
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000464 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000465 anyregs = true;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000466 break;
467 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000468 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000469
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000470 // Initialize the AliasMap on the first use.
471 if (AliasMap.empty()) {
472 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
473 // or -1.
474 AliasMap.resize(TRI->getNumRegs(), -1);
475 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
476 for (const unsigned *AI = TRI->getOverlaps(RC->getRegister(i)); *AI; ++AI)
477 AliasMap[*AI] = i;
478 }
479
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000480 MachineBasicBlock *Entry = MF->begin();
481 SmallPtrSet<MachineBasicBlock*, 16> Visited;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000482 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 16> >
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000483 DFI = df_ext_begin(Entry, Visited), DFE = df_ext_end(Entry, Visited);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000484 DFI != DFE; ++DFI) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000485 MBB = *DFI;
486 enterBasicBlock();
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000487 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
488 ++I) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000489 MachineInstr *mi = I;
490 if (mi->isDebugValue()) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000491 ++Distance;
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000492 std::pair<uint16_t, uint16_t> domp = TII->getExecutionDomain(mi);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000493 if (domp.first)
494 if (domp.second)
495 visitSoftInstr(mi, domp.second);
496 else
497 visitHardInstr(mi, domp.first);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000498 else if (LiveRegs)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000499 visitGenericInstr(mi);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000500 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000501
502 // Save live registers at end of MBB - used by enterBasicBlock().
503 if (LiveRegs)
504 LiveOuts.insert(std::make_pair(MBB, LiveRegs));
505 LiveRegs = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000506 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000507
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000508 // Clear the LiveOuts vectors. Should we also collapse any remaining
509 // DomainValues?
510 for (LiveOutMap::const_iterator i = LiveOuts.begin(), e = LiveOuts.end();
511 i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000512 delete[] i->second;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000513 LiveOuts.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000514 Avail.clear();
515 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000516
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000517 return false;
518}
519
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000520FunctionPass *
521llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000522 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000523}