blob: bd77f655c1444dd93ffaa76078b3bb65828b560d [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 Olesena59ce032011-11-07 21:59:29 +000029#include "llvm/ADT/PostOrderIterator.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 Olesen25265d02011-11-07 21:40:27 +0000152 void leaveBasicBlock(MachineBasicBlock*);
153 void visitInstr(MachineInstr*);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000154 void visitGenericInstr(MachineInstr*);
155 void visitSoftInstr(MachineInstr*, unsigned mask);
156 void visitHardInstr(MachineInstr*, unsigned domain);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000157};
158}
159
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000160char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000161
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000162/// Translate TRI register number to an index into our smaller tables of
163/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000164int ExeDepsFix::RegIndex(unsigned Reg) {
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000165 assert(Reg < AliasMap.size() && "Invalid register");
166 return AliasMap[Reg];
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000167}
168
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000169DomainValue *ExeDepsFix::Alloc(int domain) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000170 DomainValue *dv = Avail.empty() ?
171 new(Allocator.Allocate()) DomainValue :
172 Avail.pop_back_val();
173 dv->Dist = Distance;
174 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000175 dv->addDomain(domain);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000176 return dv;
177}
178
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000179void ExeDepsFix::Recycle(DomainValue *dv) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000180 assert(dv && "Cannot recycle NULL");
181 dv->clear();
182 Avail.push_back(dv);
183}
184
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000185/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000186void ExeDepsFix::SetLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000187 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000188 if (!LiveRegs) {
189 LiveRegs = new DomainValue*[NumRegs];
190 std::fill(LiveRegs, LiveRegs+NumRegs, (DomainValue*)0);
191 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000192
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000193 if (LiveRegs[rx] == dv)
194 return;
195 if (LiveRegs[rx]) {
196 assert(LiveRegs[rx]->Refs && "Bad refcount");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000197 if (--LiveRegs[rx]->Refs == 0) Recycle(LiveRegs[rx]);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000198 }
199 LiveRegs[rx] = dv;
200 if (dv) ++dv->Refs;
201}
202
203// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000204void ExeDepsFix::Kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000205 assert(unsigned(rx) < NumRegs && "Invalid index");
206 if (!LiveRegs || !LiveRegs[rx]) return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000207
208 // Before killing the last reference to an open DomainValue, collapse it to
209 // the first available domain.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000210 if (LiveRegs[rx]->Refs == 1 && !LiveRegs[rx]->isCollapsed())
211 Collapse(LiveRegs[rx], LiveRegs[rx]->getFirstDomain());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000212 else
213 SetLiveReg(rx, 0);
214}
215
216/// Force register rx into domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000217void ExeDepsFix::Force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000218 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesend77f8182010-03-30 00:09:32 +0000219 DomainValue *dv;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000220 if (LiveRegs && (dv = LiveRegs[rx])) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000221 if (dv->isCollapsed())
222 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000223 else if (dv->hasDomain(domain))
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000224 Collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000225 else {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000226 // This is an incompatible open DomainValue. Collapse it to whatever and
227 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000228 Collapse(dv, dv->getFirstDomain());
229 assert(LiveRegs[rx] && "Not live after collapse?");
230 LiveRegs[rx]->addDomain(domain);
231 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000232 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000233 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000234 SetLiveReg(rx, Alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000235 }
236}
237
238/// Collapse open DomainValue into given domain. If there are multiple
239/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000240void ExeDepsFix::Collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000241 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000242
243 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000244 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000245 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000246 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000247
248 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000249 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000250 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000251 if (LiveRegs[rx] == dv)
252 SetLiveReg(rx, Alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000253}
254
255/// Merge - All instructions and registers in B are moved to A, and B is
256/// released.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000257bool ExeDepsFix::Merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000258 assert(!A->isCollapsed() && "Cannot merge into collapsed");
259 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000260 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000261 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000262 // Restrict to the domains that A and B have in common.
263 unsigned common = A->getCommonDomains(B->AvailableDomains);
264 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000265 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000266 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000267 A->Dist = std::max(A->Dist, B->Dist);
268 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000269 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000270 if (LiveRegs[rx] == B)
271 SetLiveReg(rx, A);
272 return true;
273}
274
Jakob Stoklund Olesend8f9f342011-11-07 21:23:42 +0000275void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000276 // Try to coalesce live-out registers from predecessors.
Dan Gohman81bf03e2010-04-13 16:57:55 +0000277 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000278 e = MBB->livein_end(); i != e; ++i) {
279 int rx = RegIndex(*i);
280 if (rx < 0) continue;
281 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
282 pe = MBB->pred_end(); pi != pe; ++pi) {
Jakob Stoklund Olesenb16df902010-03-31 00:40:08 +0000283 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000284 if (fi == LiveOuts.end()) continue;
285 DomainValue *pdv = fi->second[rx];
286 if (!pdv) continue;
Chris Lattner563d83f2010-03-31 20:32:51 +0000287 if (!LiveRegs || !LiveRegs[rx]) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000288 SetLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000289 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000290 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000291
292 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000293 if (LiveRegs[rx]->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000294 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000295 unsigned domain = LiveRegs[rx]->getFirstDomain();
296 if (!pdv->isCollapsed() && pdv->hasDomain(domain))
297 Collapse(pdv, domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000298 continue;
299 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000300
Chris Lattner563d83f2010-03-31 20:32:51 +0000301 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000302 if (!pdv->isCollapsed())
Chris Lattner563d83f2010-03-31 20:32:51 +0000303 Merge(LiveRegs[rx], pdv);
304 else
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000305 Force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000306 }
307 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000308}
309
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000310void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
311 // Save live registers at end of MBB - used by enterBasicBlock().
312 if (LiveRegs)
313 LiveOuts.insert(std::make_pair(MBB, LiveRegs));
314 LiveRegs = 0;
315}
316
317void ExeDepsFix::visitInstr(MachineInstr *MI) {
318 if (MI->isDebugValue())
319 return;
320 ++Distance;
321 std::pair<uint16_t, uint16_t> domp = TII->getExecutionDomain(MI);
322 if (domp.first)
323 if (domp.second)
324 visitSoftInstr(MI, domp.second);
325 else
326 visitHardInstr(MI, domp.first);
327 else if (LiveRegs)
328 visitGenericInstr(MI);
329}
330
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000331// A hard instruction only works in one domain. All input registers will be
332// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000333void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000334 // Collapse all uses.
335 for (unsigned i = mi->getDesc().getNumDefs(),
336 e = mi->getDesc().getNumOperands(); i != e; ++i) {
337 MachineOperand &mo = mi->getOperand(i);
338 if (!mo.isReg()) continue;
339 int rx = RegIndex(mo.getReg());
340 if (rx < 0) continue;
341 Force(rx, domain);
342 }
343
344 // Kill all defs and force them.
345 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
346 MachineOperand &mo = mi->getOperand(i);
347 if (!mo.isReg()) continue;
348 int rx = RegIndex(mo.getReg());
349 if (rx < 0) continue;
350 Kill(rx);
351 Force(rx, domain);
352 }
353}
354
355// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000356void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000357 // Bitmask of available domains for this instruction after taking collapsed
358 // operands into account.
359 unsigned available = mask;
360
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000361 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000362 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000363 if (LiveRegs)
364 for (unsigned i = mi->getDesc().getNumDefs(),
365 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000366 MachineOperand &mo = mi->getOperand(i);
367 if (!mo.isReg()) continue;
368 int rx = RegIndex(mo.getReg());
369 if (rx < 0) continue;
370 if (DomainValue *dv = LiveRegs[rx]) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000371 // Bitmask of domains that dv and available have in common.
372 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000373 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000374 if (dv->isCollapsed()) {
375 // Restrict available domains to the ones in common with the operand.
376 // If there are no common domains, we must pay the cross-domain
377 // penalty for this operand.
378 if (common) available = common;
379 } else if (common)
380 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000381 used.push_back(rx);
382 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000383 // Open DomainValue is not compatible with instruction. It is useless
384 // now.
Chris Lattner563d83f2010-03-31 20:32:51 +0000385 Kill(rx);
386 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000387 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000388
389 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000390 if (isPowerOf2_32(available)) {
391 unsigned domain = CountTrailingZeros_32(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000392 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000393 visitHardInstr(mi, domain);
394 return;
395 }
396
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000397 // Kill off any remaining uses that don't match available, and build a list of
398 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000399 SmallVector<DomainValue*,4> doms;
400 for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
401 int rx = *i;
402 DomainValue *dv = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000403 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000404 if (!dv->getCommonDomains(available)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000405 Kill(*i);
406 continue;
407 }
408 // sorted, uniqued insert.
409 bool inserted = false;
410 for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end();
411 i != e && !inserted; ++i) {
412 if (dv == *i)
413 inserted = true;
414 else if (dv->Dist < (*i)->Dist) {
415 inserted = true;
416 doms.insert(i, dv);
417 }
418 }
419 if (!inserted)
420 doms.push_back(dv);
421 }
422
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000423 // doms are now sorted in order of appearance. Try to merge them all, giving
424 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000425 DomainValue *dv = 0;
426 while (!doms.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000427 if (!dv) {
428 dv = doms.pop_back_val();
429 continue;
430 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000431
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000432 DomainValue *latest = doms.pop_back_val();
433 if (Merge(dv, latest)) continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000434
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000435 // If latest didn't merge, it is useless now. Kill all registers using it.
Chris Lattner563d83f2010-03-31 20:32:51 +0000436 for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000437 if (LiveRegs[*i] == latest)
Chris Lattner563d83f2010-03-31 20:32:51 +0000438 Kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000439 }
440
441 // dv is the DomainValue we are going to use for this instruction.
442 if (!dv)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000443 dv = Alloc();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000444 dv->Dist = Distance;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000445 dv->AvailableDomains = available;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000446 dv->Instrs.push_back(mi);
447
448 // Finally set all defs and non-collapsed uses to dv.
449 for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
450 MachineOperand &mo = mi->getOperand(i);
451 if (!mo.isReg()) continue;
452 int rx = RegIndex(mo.getReg());
453 if (rx < 0) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000454 if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000455 Kill(rx);
456 SetLiveReg(rx, dv);
457 }
458 }
459}
460
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000461void ExeDepsFix::visitGenericInstr(MachineInstr *mi) {
462 // Process explicit defs, kill any relevant registers redefined.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000463 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
464 MachineOperand &mo = mi->getOperand(i);
465 if (!mo.isReg()) continue;
466 int rx = RegIndex(mo.getReg());
467 if (rx < 0) continue;
468 Kill(rx);
469 }
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000470}
471
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000472bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000473 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000474 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000475 TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000476 LiveRegs = 0;
477 Distance = 0;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000478 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000479
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000480 // If no relevant registers are used in the function, we can skip it
481 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000482 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000483 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
484 I != E; ++I)
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000485 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000486 anyregs = true;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000487 break;
488 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000489 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000490
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000491 // Initialize the AliasMap on the first use.
492 if (AliasMap.empty()) {
493 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
494 // or -1.
495 AliasMap.resize(TRI->getNumRegs(), -1);
496 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
497 for (const unsigned *AI = TRI->getOverlaps(RC->getRegister(i)); *AI; ++AI)
498 AliasMap[*AI] = i;
499 }
500
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000501 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000502 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
503 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
504 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
505 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesend8f9f342011-11-07 21:23:42 +0000506 enterBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000507 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000508 ++I)
509 visitInstr(I);
510 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000511 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000512
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000513 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
514 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
515 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
516 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
517 if (FI == LiveOuts.end())
518 continue;
519 assert(FI->second && "Null entry");
520 // The DomainValue is collapsed when the last reference is killed.
521 LiveRegs = FI->second;
522 for (unsigned i = 0, e = NumRegs; i != e; ++i)
523 if (LiveRegs[i])
524 Kill(i);
525 delete[] LiveRegs;
526 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000527 LiveOuts.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000528 Avail.clear();
529 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000530
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000531 return false;
532}
533
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000534FunctionPass *
535llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000536 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000537}