blob: 7f04224720d8dd60f03696f44a5a879853b5a7f7 [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 Olesendf4b35e2011-09-27 23:50:46 +0000114 std::vector<int> AliasMap;
115 const unsigned NumRegs;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000116 DomainValue **LiveRegs;
117 typedef DenseMap<MachineBasicBlock*,DomainValue**> LiveOutMap;
118 LiveOutMap LiveOuts;
119 unsigned Distance;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000120
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000121public:
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000122 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000123 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000124
125 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
126 AU.setPreservesAll();
127 MachineFunctionPass::getAnalysisUsage(AU);
128 }
129
130 virtual bool runOnMachineFunction(MachineFunction &MF);
131
132 virtual const char *getPassName() const {
Jakob Stoklund Olesencd7dcad2011-11-07 21:23:39 +0000133 return "Execution dependency fix";
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000134 }
135
136private:
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000137 // Register mapping.
138 int RegIndex(unsigned Reg);
139
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000140 // DomainValue allocation.
141 DomainValue *Alloc(int domain = -1);
142 void Recycle(DomainValue*);
143
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000144 // LiveRegs manipulations.
145 void SetLiveReg(int rx, DomainValue *DV);
146 void Kill(int rx);
147 void Force(int rx, unsigned domain);
148 void Collapse(DomainValue *dv, unsigned domain);
149 bool Merge(DomainValue *A, DomainValue *B);
150
Jakob Stoklund Olesend8f9f342011-11-07 21:23:42 +0000151 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000152 void visitGenericInstr(MachineInstr*);
153 void visitSoftInstr(MachineInstr*, unsigned mask);
154 void visitHardInstr(MachineInstr*, unsigned domain);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000155};
156}
157
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000158char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000159
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000160/// Translate TRI register number to an index into our smaller tables of
161/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000162int ExeDepsFix::RegIndex(unsigned Reg) {
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000163 assert(Reg < AliasMap.size() && "Invalid register");
164 return AliasMap[Reg];
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000165}
166
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000167DomainValue *ExeDepsFix::Alloc(int domain) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000168 DomainValue *dv = Avail.empty() ?
169 new(Allocator.Allocate()) DomainValue :
170 Avail.pop_back_val();
171 dv->Dist = Distance;
172 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000173 dv->addDomain(domain);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000174 return dv;
175}
176
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000177void ExeDepsFix::Recycle(DomainValue *dv) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000178 assert(dv && "Cannot recycle NULL");
179 dv->clear();
180 Avail.push_back(dv);
181}
182
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000183/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000184void ExeDepsFix::SetLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000185 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000186 if (!LiveRegs) {
187 LiveRegs = new DomainValue*[NumRegs];
188 std::fill(LiveRegs, LiveRegs+NumRegs, (DomainValue*)0);
189 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000190
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000191 if (LiveRegs[rx] == dv)
192 return;
193 if (LiveRegs[rx]) {
194 assert(LiveRegs[rx]->Refs && "Bad refcount");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000195 if (--LiveRegs[rx]->Refs == 0) Recycle(LiveRegs[rx]);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000196 }
197 LiveRegs[rx] = dv;
198 if (dv) ++dv->Refs;
199}
200
201// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000202void ExeDepsFix::Kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000203 assert(unsigned(rx) < NumRegs && "Invalid index");
204 if (!LiveRegs || !LiveRegs[rx]) return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000205
206 // Before killing the last reference to an open DomainValue, collapse it to
207 // the first available domain.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000208 if (LiveRegs[rx]->Refs == 1 && !LiveRegs[rx]->isCollapsed())
209 Collapse(LiveRegs[rx], LiveRegs[rx]->getFirstDomain());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000210 else
211 SetLiveReg(rx, 0);
212}
213
214/// Force register rx into domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000215void ExeDepsFix::Force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000216 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesend77f8182010-03-30 00:09:32 +0000217 DomainValue *dv;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000218 if (LiveRegs && (dv = LiveRegs[rx])) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000219 if (dv->isCollapsed())
220 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000221 else if (dv->hasDomain(domain))
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000222 Collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000223 else {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000224 // This is an incompatible open DomainValue. Collapse it to whatever and
225 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000226 Collapse(dv, dv->getFirstDomain());
227 assert(LiveRegs[rx] && "Not live after collapse?");
228 LiveRegs[rx]->addDomain(domain);
229 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000230 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000231 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000232 SetLiveReg(rx, Alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000233 }
234}
235
236/// Collapse open DomainValue into given domain. If there are multiple
237/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000238void ExeDepsFix::Collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000239 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000240
241 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000242 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000243 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000244 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000245
246 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000247 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000248 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000249 if (LiveRegs[rx] == dv)
250 SetLiveReg(rx, Alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000251}
252
253/// Merge - All instructions and registers in B are moved to A, and B is
254/// released.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000255bool ExeDepsFix::Merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000256 assert(!A->isCollapsed() && "Cannot merge into collapsed");
257 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000258 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000259 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000260 // Restrict to the domains that A and B have in common.
261 unsigned common = A->getCommonDomains(B->AvailableDomains);
262 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000263 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000264 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000265 A->Dist = std::max(A->Dist, B->Dist);
266 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000267 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000268 if (LiveRegs[rx] == B)
269 SetLiveReg(rx, A);
270 return true;
271}
272
Jakob Stoklund Olesend8f9f342011-11-07 21:23:42 +0000273void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000274 // Try to coalesce live-out registers from predecessors.
Dan Gohman81bf03e2010-04-13 16:57:55 +0000275 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000276 e = MBB->livein_end(); i != e; ++i) {
277 int rx = RegIndex(*i);
278 if (rx < 0) continue;
279 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
280 pe = MBB->pred_end(); pi != pe; ++pi) {
Jakob Stoklund Olesenb16df902010-03-31 00:40:08 +0000281 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000282 if (fi == LiveOuts.end()) continue;
283 DomainValue *pdv = fi->second[rx];
284 if (!pdv) continue;
Chris Lattner563d83f2010-03-31 20:32:51 +0000285 if (!LiveRegs || !LiveRegs[rx]) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000286 SetLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000287 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000288 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000289
290 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000291 if (LiveRegs[rx]->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000292 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000293 unsigned domain = LiveRegs[rx]->getFirstDomain();
294 if (!pdv->isCollapsed() && pdv->hasDomain(domain))
295 Collapse(pdv, domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000296 continue;
297 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000298
Chris Lattner563d83f2010-03-31 20:32:51 +0000299 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000300 if (!pdv->isCollapsed())
Chris Lattner563d83f2010-03-31 20:32:51 +0000301 Merge(LiveRegs[rx], pdv);
302 else
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000303 Force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000304 }
305 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000306}
307
308// A hard instruction only works in one domain. All input registers will be
309// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000310void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000311 // Collapse all uses.
312 for (unsigned i = mi->getDesc().getNumDefs(),
313 e = mi->getDesc().getNumOperands(); i != e; ++i) {
314 MachineOperand &mo = mi->getOperand(i);
315 if (!mo.isReg()) continue;
316 int rx = RegIndex(mo.getReg());
317 if (rx < 0) continue;
318 Force(rx, domain);
319 }
320
321 // Kill all defs and force them.
322 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
323 MachineOperand &mo = mi->getOperand(i);
324 if (!mo.isReg()) continue;
325 int rx = RegIndex(mo.getReg());
326 if (rx < 0) continue;
327 Kill(rx);
328 Force(rx, domain);
329 }
330}
331
332// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000333void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000334 // Bitmask of available domains for this instruction after taking collapsed
335 // operands into account.
336 unsigned available = mask;
337
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000338 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000339 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000340 if (LiveRegs)
341 for (unsigned i = mi->getDesc().getNumDefs(),
342 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000343 MachineOperand &mo = mi->getOperand(i);
344 if (!mo.isReg()) continue;
345 int rx = RegIndex(mo.getReg());
346 if (rx < 0) continue;
347 if (DomainValue *dv = LiveRegs[rx]) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000348 // Bitmask of domains that dv and available have in common.
349 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000350 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000351 if (dv->isCollapsed()) {
352 // Restrict available domains to the ones in common with the operand.
353 // If there are no common domains, we must pay the cross-domain
354 // penalty for this operand.
355 if (common) available = common;
356 } else if (common)
357 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000358 used.push_back(rx);
359 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000360 // Open DomainValue is not compatible with instruction. It is useless
361 // now.
Chris Lattner563d83f2010-03-31 20:32:51 +0000362 Kill(rx);
363 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000364 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000365
366 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000367 if (isPowerOf2_32(available)) {
368 unsigned domain = CountTrailingZeros_32(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000369 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000370 visitHardInstr(mi, domain);
371 return;
372 }
373
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000374 // Kill off any remaining uses that don't match available, and build a list of
375 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000376 SmallVector<DomainValue*,4> doms;
377 for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
378 int rx = *i;
379 DomainValue *dv = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000380 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000381 if (!dv->getCommonDomains(available)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000382 Kill(*i);
383 continue;
384 }
385 // sorted, uniqued insert.
386 bool inserted = false;
387 for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end();
388 i != e && !inserted; ++i) {
389 if (dv == *i)
390 inserted = true;
391 else if (dv->Dist < (*i)->Dist) {
392 inserted = true;
393 doms.insert(i, dv);
394 }
395 }
396 if (!inserted)
397 doms.push_back(dv);
398 }
399
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000400 // doms are now sorted in order of appearance. Try to merge them all, giving
401 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000402 DomainValue *dv = 0;
403 while (!doms.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000404 if (!dv) {
405 dv = doms.pop_back_val();
406 continue;
407 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000408
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000409 DomainValue *latest = doms.pop_back_val();
410 if (Merge(dv, latest)) continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000411
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000412 // If latest didn't merge, it is useless now. Kill all registers using it.
Chris Lattner563d83f2010-03-31 20:32:51 +0000413 for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000414 if (LiveRegs[*i] == latest)
Chris Lattner563d83f2010-03-31 20:32:51 +0000415 Kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000416 }
417
418 // dv is the DomainValue we are going to use for this instruction.
419 if (!dv)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000420 dv = Alloc();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000421 dv->Dist = Distance;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000422 dv->AvailableDomains = available;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000423 dv->Instrs.push_back(mi);
424
425 // Finally set all defs and non-collapsed uses to dv.
426 for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
427 MachineOperand &mo = mi->getOperand(i);
428 if (!mo.isReg()) continue;
429 int rx = RegIndex(mo.getReg());
430 if (rx < 0) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000431 if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000432 Kill(rx);
433 SetLiveReg(rx, dv);
434 }
435 }
436}
437
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000438void ExeDepsFix::visitGenericInstr(MachineInstr *mi) {
439 // Process explicit defs, kill any relevant registers redefined.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000440 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
441 MachineOperand &mo = mi->getOperand(i);
442 if (!mo.isReg()) continue;
443 int rx = RegIndex(mo.getReg());
444 if (rx < 0) continue;
445 Kill(rx);
446 }
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000447}
448
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000449bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000450 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000451 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000452 TRI = MF->getTarget().getRegisterInfo();
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
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000457 // If no relevant registers are used in the function, we can skip it
458 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000459 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000460 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
461 I != E; ++I)
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000462 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000463 anyregs = true;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000464 break;
465 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000466 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000467
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000468 // Initialize the AliasMap on the first use.
469 if (AliasMap.empty()) {
470 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
471 // or -1.
472 AliasMap.resize(TRI->getNumRegs(), -1);
473 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
474 for (const unsigned *AI = TRI->getOverlaps(RC->getRegister(i)); *AI; ++AI)
475 AliasMap[*AI] = i;
476 }
477
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000478 MachineBasicBlock *Entry = MF->begin();
479 SmallPtrSet<MachineBasicBlock*, 16> Visited;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000480 for (df_ext_iterator<MachineBasicBlock*, SmallPtrSet<MachineBasicBlock*, 16> >
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000481 DFI = df_ext_begin(Entry, Visited), DFE = df_ext_end(Entry, Visited);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000482 DFI != DFE; ++DFI) {
Jakob Stoklund Olesend8f9f342011-11-07 21:23:42 +0000483 MachineBasicBlock *MBB = *DFI;
484 enterBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000485 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
486 ++I) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000487 MachineInstr *mi = I;
488 if (mi->isDebugValue()) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000489 ++Distance;
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000490 std::pair<uint16_t, uint16_t> domp = TII->getExecutionDomain(mi);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000491 if (domp.first)
492 if (domp.second)
493 visitSoftInstr(mi, domp.second);
494 else
495 visitHardInstr(mi, domp.first);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000496 else if (LiveRegs)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000497 visitGenericInstr(mi);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000498 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000499
500 // Save live registers at end of MBB - used by enterBasicBlock().
501 if (LiveRegs)
502 LiveOuts.insert(std::make_pair(MBB, LiveRegs));
503 LiveRegs = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000504 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000505
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000506 // Clear the LiveOuts vectors. Should we also collapse any remaining
507 // DomainValues?
508 for (LiveOutMap::const_iterator i = LiveOuts.begin(), e = LiveOuts.end();
509 i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000510 delete[] i->second;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000511 LiveOuts.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000512 Avail.clear();
513 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000514
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000515 return false;
516}
517
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000518FunctionPass *
519llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000520 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000521}