blob: 5aa80f9a0674a17ad59534f0640e24ca4f0e7c1d [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 Olesene1b3e112011-11-08 20:57:04 +0000269
270 // Clear the old DomainValue so we won't try to swizzle instructions twice.
271 B->Instrs.clear();
272 B->AvailableDomains = 0;
273
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000274 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000275 if (LiveRegs[rx] == B)
276 SetLiveReg(rx, A);
277 return true;
278}
279
Jakob Stoklund Olesend8f9f342011-11-07 21:23:42 +0000280void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000281 // Try to coalesce live-out registers from predecessors.
Dan Gohman81bf03e2010-04-13 16:57:55 +0000282 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000283 e = MBB->livein_end(); i != e; ++i) {
284 int rx = RegIndex(*i);
285 if (rx < 0) continue;
286 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
287 pe = MBB->pred_end(); pi != pe; ++pi) {
Jakob Stoklund Olesenb16df902010-03-31 00:40:08 +0000288 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000289 if (fi == LiveOuts.end()) continue;
290 DomainValue *pdv = fi->second[rx];
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000291 if (!pdv || !pdv->AvailableDomains) continue;
Chris Lattner563d83f2010-03-31 20:32:51 +0000292 if (!LiveRegs || !LiveRegs[rx]) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000293 SetLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000294 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000295 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000296
297 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000298 if (LiveRegs[rx]->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000299 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000300 unsigned domain = LiveRegs[rx]->getFirstDomain();
301 if (!pdv->isCollapsed() && pdv->hasDomain(domain))
302 Collapse(pdv, domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000303 continue;
304 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000305
Chris Lattner563d83f2010-03-31 20:32:51 +0000306 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000307 if (!pdv->isCollapsed())
Chris Lattner563d83f2010-03-31 20:32:51 +0000308 Merge(LiveRegs[rx], pdv);
309 else
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000310 Force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000311 }
312 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000313}
314
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000315void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
316 // Save live registers at end of MBB - used by enterBasicBlock().
317 if (LiveRegs)
318 LiveOuts.insert(std::make_pair(MBB, LiveRegs));
319 LiveRegs = 0;
320}
321
322void ExeDepsFix::visitInstr(MachineInstr *MI) {
323 if (MI->isDebugValue())
324 return;
325 ++Distance;
326 std::pair<uint16_t, uint16_t> domp = TII->getExecutionDomain(MI);
327 if (domp.first)
328 if (domp.second)
329 visitSoftInstr(MI, domp.second);
330 else
331 visitHardInstr(MI, domp.first);
332 else if (LiveRegs)
333 visitGenericInstr(MI);
334}
335
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000336// A hard instruction only works in one domain. All input registers will be
337// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000338void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000339 // Collapse all uses.
340 for (unsigned i = mi->getDesc().getNumDefs(),
341 e = mi->getDesc().getNumOperands(); i != e; ++i) {
342 MachineOperand &mo = mi->getOperand(i);
343 if (!mo.isReg()) continue;
344 int rx = RegIndex(mo.getReg());
345 if (rx < 0) continue;
346 Force(rx, domain);
347 }
348
349 // Kill all defs and force them.
350 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
351 MachineOperand &mo = mi->getOperand(i);
352 if (!mo.isReg()) continue;
353 int rx = RegIndex(mo.getReg());
354 if (rx < 0) continue;
355 Kill(rx);
356 Force(rx, domain);
357 }
358}
359
360// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000361void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000362 // Bitmask of available domains for this instruction after taking collapsed
363 // operands into account.
364 unsigned available = mask;
365
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000366 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000367 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000368 if (LiveRegs)
369 for (unsigned i = mi->getDesc().getNumDefs(),
370 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000371 MachineOperand &mo = mi->getOperand(i);
372 if (!mo.isReg()) continue;
373 int rx = RegIndex(mo.getReg());
374 if (rx < 0) continue;
375 if (DomainValue *dv = LiveRegs[rx]) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000376 // Bitmask of domains that dv and available have in common.
377 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000378 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000379 if (dv->isCollapsed()) {
380 // Restrict available domains to the ones in common with the operand.
381 // If there are no common domains, we must pay the cross-domain
382 // penalty for this operand.
383 if (common) available = common;
384 } else if (common)
385 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000386 used.push_back(rx);
387 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000388 // Open DomainValue is not compatible with instruction. It is useless
389 // now.
Chris Lattner563d83f2010-03-31 20:32:51 +0000390 Kill(rx);
391 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000392 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000393
394 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000395 if (isPowerOf2_32(available)) {
396 unsigned domain = CountTrailingZeros_32(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000397 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000398 visitHardInstr(mi, domain);
399 return;
400 }
401
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000402 // Kill off any remaining uses that don't match available, and build a list of
403 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000404 SmallVector<DomainValue*,4> doms;
405 for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
406 int rx = *i;
407 DomainValue *dv = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000408 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000409 if (!dv->getCommonDomains(available)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000410 Kill(*i);
411 continue;
412 }
413 // sorted, uniqued insert.
414 bool inserted = false;
415 for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end();
416 i != e && !inserted; ++i) {
417 if (dv == *i)
418 inserted = true;
419 else if (dv->Dist < (*i)->Dist) {
420 inserted = true;
421 doms.insert(i, dv);
422 }
423 }
424 if (!inserted)
425 doms.push_back(dv);
426 }
427
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000428 // doms are now sorted in order of appearance. Try to merge them all, giving
429 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000430 DomainValue *dv = 0;
431 while (!doms.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000432 if (!dv) {
433 dv = doms.pop_back_val();
434 continue;
435 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000436
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000437 DomainValue *latest = doms.pop_back_val();
438 if (Merge(dv, latest)) continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000439
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000440 // If latest didn't merge, it is useless now. Kill all registers using it.
Chris Lattner563d83f2010-03-31 20:32:51 +0000441 for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000442 if (LiveRegs[*i] == latest)
Chris Lattner563d83f2010-03-31 20:32:51 +0000443 Kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000444 }
445
446 // dv is the DomainValue we are going to use for this instruction.
447 if (!dv)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000448 dv = Alloc();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000449 dv->Dist = Distance;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000450 dv->AvailableDomains = available;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000451 dv->Instrs.push_back(mi);
452
453 // Finally set all defs and non-collapsed uses to dv.
454 for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
455 MachineOperand &mo = mi->getOperand(i);
456 if (!mo.isReg()) continue;
457 int rx = RegIndex(mo.getReg());
458 if (rx < 0) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000459 if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000460 Kill(rx);
461 SetLiveReg(rx, dv);
462 }
463 }
464}
465
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000466void ExeDepsFix::visitGenericInstr(MachineInstr *mi) {
467 // Process explicit defs, kill any relevant registers redefined.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000468 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
469 MachineOperand &mo = mi->getOperand(i);
470 if (!mo.isReg()) continue;
471 int rx = RegIndex(mo.getReg());
472 if (rx < 0) continue;
473 Kill(rx);
474 }
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000475}
476
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000477bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000478 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000479 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000480 TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000481 LiveRegs = 0;
482 Distance = 0;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000483 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000484
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000485 // If no relevant registers are used in the function, we can skip it
486 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000487 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000488 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
489 I != E; ++I)
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000490 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000491 anyregs = true;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000492 break;
493 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000494 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000495
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000496 // Initialize the AliasMap on the first use.
497 if (AliasMap.empty()) {
498 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
499 // or -1.
500 AliasMap.resize(TRI->getNumRegs(), -1);
501 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
502 for (const unsigned *AI = TRI->getOverlaps(RC->getRegister(i)); *AI; ++AI)
503 AliasMap[*AI] = i;
504 }
505
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000506 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000507 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
508 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
509 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
510 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesend8f9f342011-11-07 21:23:42 +0000511 enterBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000512 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000513 ++I)
514 visitInstr(I);
515 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000516 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000517
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000518 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
519 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
520 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
521 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
522 if (FI == LiveOuts.end())
523 continue;
524 assert(FI->second && "Null entry");
525 // The DomainValue is collapsed when the last reference is killed.
526 LiveRegs = FI->second;
527 for (unsigned i = 0, e = NumRegs; i != e; ++i)
528 if (LiveRegs[i])
529 Kill(i);
530 delete[] LiveRegs;
531 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000532 LiveOuts.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000533 Avail.clear();
534 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000535
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000536 return false;
537}
538
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000539FunctionPass *
540llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000541 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000542}