blob: fc0b6124641d0ed83184375d92147f05f8585069 [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
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +000063 // Pointer to the next DomainValue in a chain. When two DomainValues are
64 // merged, Victim.Next is set to point to Victor, so old DomainValue
65 // references can be updated by folowing the chain.
66 DomainValue *Next;
67
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000068 // Twiddleable instructions using or defining these registers.
69 SmallVector<MachineInstr*, 8> Instrs;
70
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000071 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000072 // track of the domains where the registers are already available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000073 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000074
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000075 // Is domain available?
76 bool hasDomain(unsigned domain) const {
77 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000078 }
79
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000080 // Mark domain as available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000081 void addDomain(unsigned domain) {
82 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000083 }
84
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000085 // Restrict to a single domain available.
86 void setSingleDomain(unsigned domain) {
87 AvailableDomains = 1u << domain;
88 }
89
90 // Return bitmask of domains that are available and in mask.
91 unsigned getCommonDomains(unsigned mask) const {
92 return AvailableDomains & mask;
93 }
94
95 // First domain available.
96 unsigned getFirstDomain() const {
97 return CountTrailingZeros_32(AvailableDomains);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000098 }
99
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000100 DomainValue() : Refs(0) { clear(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000101
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000102 // Clear this DomainValue and point to next which has all its data.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000103 void clear() {
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000104 AvailableDomains = Dist = 0;
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000105 Next = 0;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000106 Instrs.clear();
107 }
108};
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000109}
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000110
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000111namespace {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000112class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000113 static char ID;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000114 SpecificBumpPtrAllocator<DomainValue> Allocator;
115 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000116
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000117 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000118 MachineFunction *MF;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000119 const TargetInstrInfo *TII;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000120 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000121 std::vector<int> AliasMap;
122 const unsigned NumRegs;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000123 DomainValue **LiveRegs;
124 typedef DenseMap<MachineBasicBlock*,DomainValue**> LiveOutMap;
125 LiveOutMap LiveOuts;
126 unsigned Distance;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000127
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000128public:
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000129 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000130 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000131
132 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
133 AU.setPreservesAll();
134 MachineFunctionPass::getAnalysisUsage(AU);
135 }
136
137 virtual bool runOnMachineFunction(MachineFunction &MF);
138
139 virtual const char *getPassName() const {
Jakob Stoklund Olesencd7dcad2011-11-07 21:23:39 +0000140 return "Execution dependency fix";
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000141 }
142
143private:
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000144 // Register mapping.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000145 int regIndex(unsigned Reg);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000146
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000147 // DomainValue allocation.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000148 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000149 DomainValue *retain(DomainValue *DV) {
150 if (DV) ++DV->Refs;
151 return DV;
152 }
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000153 void release(DomainValue*);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000154 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000155
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000156 // LiveRegs manipulations.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000157 void setLiveReg(int rx, DomainValue *DV);
158 void kill(int rx);
159 void force(int rx, unsigned domain);
160 void collapse(DomainValue *dv, unsigned domain);
161 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000162
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000163 bool enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000164 void leaveBasicBlock(MachineBasicBlock*);
165 void visitInstr(MachineInstr*);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000166 void visitGenericInstr(MachineInstr*);
167 void visitSoftInstr(MachineInstr*, unsigned mask);
168 void visitHardInstr(MachineInstr*, unsigned domain);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000169};
170}
171
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000172char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000173
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000174/// Translate TRI register number to an index into our smaller tables of
175/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000176int ExeDepsFix::regIndex(unsigned Reg) {
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000177 assert(Reg < AliasMap.size() && "Invalid register");
178 return AliasMap[Reg];
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000179}
180
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000181DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000182 DomainValue *dv = Avail.empty() ?
183 new(Allocator.Allocate()) DomainValue :
184 Avail.pop_back_val();
185 dv->Dist = Distance;
186 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000187 dv->addDomain(domain);
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000188 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000189 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000190 return dv;
191}
192
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000193/// release - Release a reference to DV. When the last reference is released,
194/// collapse if needed.
195void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000196 while (DV) {
197 assert(DV->Refs && "Bad DomainValue");
198 if (--DV->Refs)
199 return;
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000200
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000201 // There are no more DV references. Collapse any contained instructions.
202 if (DV->AvailableDomains && !DV->isCollapsed())
203 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000204
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000205 DomainValue *Next = DV->Next;
206 DV->clear();
207 Avail.push_back(DV);
208 // Also release the next DomainValue in the chain.
209 DV = Next;
210 }
211}
212
213/// resolve - Follow the chain of dead DomainValues until a live DomainValue is
214/// reached. Update the referenced pointer when necessary.
215DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
216 DomainValue *DV = DVRef;
217 if (!DV || !DV->Next)
218 return DV;
219
220 // DV has a chain. Find the end.
221 do DV = DV->Next;
222 while (DV->Next);
223
224 // Update DVRef to point to DV.
225 retain(DV);
226 release(DVRef);
227 DVRef = DV;
228 return DV;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000229}
230
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000231/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000232void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000233 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000234 if (!LiveRegs) {
235 LiveRegs = new DomainValue*[NumRegs];
236 std::fill(LiveRegs, LiveRegs+NumRegs, (DomainValue*)0);
237 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000238
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000239 if (LiveRegs[rx] == dv)
240 return;
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000241 if (LiveRegs[rx])
242 release(LiveRegs[rx]);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000243 LiveRegs[rx] = retain(dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000244}
245
246// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000247void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000248 assert(unsigned(rx) < NumRegs && "Invalid index");
249 if (!LiveRegs || !LiveRegs[rx]) return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000250
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000251 release(LiveRegs[rx]);
252 LiveRegs[rx] = 0;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000253}
254
255/// Force register rx into domain.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000256void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000257 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesend77f8182010-03-30 00:09:32 +0000258 DomainValue *dv;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000259 if (LiveRegs && (dv = LiveRegs[rx])) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000260 if (dv->isCollapsed())
261 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000262 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000263 collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000264 else {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000265 // This is an incompatible open DomainValue. Collapse it to whatever and
266 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000267 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000268 assert(LiveRegs[rx] && "Not live after collapse?");
269 LiveRegs[rx]->addDomain(domain);
270 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000271 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000272 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000273 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000274 }
275}
276
277/// Collapse open DomainValue into given domain. If there are multiple
278/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000279void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000280 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000281
282 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000283 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000284 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000285 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000286
287 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000288 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000289 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000290 if (LiveRegs[rx] == dv)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000291 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000292}
293
294/// Merge - All instructions and registers in B are moved to A, and B is
295/// released.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000296bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000297 assert(!A->isCollapsed() && "Cannot merge into collapsed");
298 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000299 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000300 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000301 // Restrict to the domains that A and B have in common.
302 unsigned common = A->getCommonDomains(B->AvailableDomains);
303 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000304 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000305 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000306 A->Dist = std::max(A->Dist, B->Dist);
307 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000308
309 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000310 B->clear();
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000311 // All uses of B are referred to A.
312 B->Next = retain(A);
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000313
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000314 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000315 if (LiveRegs[rx] == B)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000316 setLiveReg(rx, A);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000317 return true;
318}
319
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000320// enterBasicBlock - Set up LiveRegs by merging predecessor live-out values.
321// Return true if some predecessor hasn't been processed yet (like on a loop
322// back-edge).
323bool ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
324 // Detect back-edges from predecessors we haven't processed yet.
325 bool seenBackEdge = false;
326
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000327 // Try to coalesce live-out registers from predecessors.
Dan Gohman81bf03e2010-04-13 16:57:55 +0000328 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000329 e = MBB->livein_end(); i != e; ++i) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000330 int rx = regIndex(*i);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000331 if (rx < 0) continue;
332 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
333 pe = MBB->pred_end(); pi != pe; ++pi) {
Jakob Stoklund Olesenb16df902010-03-31 00:40:08 +0000334 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000335 if (fi == LiveOuts.end()) {
336 seenBackEdge = true;
337 continue;
338 }
339 if (!fi->second)
340 continue;
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000341 DomainValue *pdv = resolve(fi->second[rx]);
342 if (!pdv) continue;
Chris Lattner563d83f2010-03-31 20:32:51 +0000343 if (!LiveRegs || !LiveRegs[rx]) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000344 setLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000345 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000346 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000347
348 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000349 if (LiveRegs[rx]->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000350 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000351 unsigned domain = LiveRegs[rx]->getFirstDomain();
352 if (!pdv->isCollapsed() && pdv->hasDomain(domain))
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000353 collapse(pdv, domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000354 continue;
355 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000356
Chris Lattner563d83f2010-03-31 20:32:51 +0000357 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000358 if (!pdv->isCollapsed())
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000359 merge(LiveRegs[rx], pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000360 else
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000361 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000362 }
363 }
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000364 return seenBackEdge;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000365}
366
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000367void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
368 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000369 // Also use LiveOuts as a visited set to detect back-edges.
370 if (!LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second && LiveRegs) {
371 // Insertion failed, this must be the second pass.
372 // Release all the DomainValues instead of keeping them.
373 for (unsigned i = 0, e = NumRegs; i != e; ++i)
374 release(LiveRegs[i]);
375 delete[] LiveRegs;
376 }
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000377 LiveRegs = 0;
378}
379
380void ExeDepsFix::visitInstr(MachineInstr *MI) {
381 if (MI->isDebugValue())
382 return;
383 ++Distance;
384 std::pair<uint16_t, uint16_t> domp = TII->getExecutionDomain(MI);
385 if (domp.first)
386 if (domp.second)
387 visitSoftInstr(MI, domp.second);
388 else
389 visitHardInstr(MI, domp.first);
390 else if (LiveRegs)
391 visitGenericInstr(MI);
392}
393
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000394// A hard instruction only works in one domain. All input registers will be
395// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000396void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000397 // Collapse all uses.
398 for (unsigned i = mi->getDesc().getNumDefs(),
399 e = mi->getDesc().getNumOperands(); i != e; ++i) {
400 MachineOperand &mo = mi->getOperand(i);
401 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000402 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000403 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000404 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000405 }
406
407 // Kill all defs and force them.
408 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
409 MachineOperand &mo = mi->getOperand(i);
410 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000411 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000412 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000413 kill(rx);
414 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000415 }
416}
417
418// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000419void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000420 // Bitmask of available domains for this instruction after taking collapsed
421 // operands into account.
422 unsigned available = mask;
423
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000424 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000425 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000426 if (LiveRegs)
427 for (unsigned i = mi->getDesc().getNumDefs(),
428 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000429 MachineOperand &mo = mi->getOperand(i);
430 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000431 int rx = regIndex(mo.getReg());
Chris Lattner563d83f2010-03-31 20:32:51 +0000432 if (rx < 0) continue;
433 if (DomainValue *dv = LiveRegs[rx]) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000434 // Bitmask of domains that dv and available have in common.
435 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000436 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000437 if (dv->isCollapsed()) {
438 // Restrict available domains to the ones in common with the operand.
439 // If there are no common domains, we must pay the cross-domain
440 // penalty for this operand.
441 if (common) available = common;
442 } else if (common)
443 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000444 used.push_back(rx);
445 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000446 // Open DomainValue is not compatible with instruction. It is useless
447 // now.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000448 kill(rx);
Chris Lattner563d83f2010-03-31 20:32:51 +0000449 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000450 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000451
452 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000453 if (isPowerOf2_32(available)) {
454 unsigned domain = CountTrailingZeros_32(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000455 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000456 visitHardInstr(mi, domain);
457 return;
458 }
459
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000460 // Kill off any remaining uses that don't match available, and build a list of
461 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000462 SmallVector<DomainValue*,4> doms;
463 for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
464 int rx = *i;
465 DomainValue *dv = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000466 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000467 if (!dv->getCommonDomains(available)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000468 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000469 continue;
470 }
471 // sorted, uniqued insert.
472 bool inserted = false;
473 for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end();
474 i != e && !inserted; ++i) {
475 if (dv == *i)
476 inserted = true;
477 else if (dv->Dist < (*i)->Dist) {
478 inserted = true;
479 doms.insert(i, dv);
480 }
481 }
482 if (!inserted)
483 doms.push_back(dv);
484 }
485
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000486 // doms are now sorted in order of appearance. Try to merge them all, giving
487 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000488 DomainValue *dv = 0;
489 while (!doms.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000490 if (!dv) {
491 dv = doms.pop_back_val();
492 continue;
493 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000494
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000495 DomainValue *latest = doms.pop_back_val();
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000496 if (merge(dv, latest)) continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000497
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000498 // If latest didn't merge, it is useless now. Kill all registers using it.
Chris Lattner563d83f2010-03-31 20:32:51 +0000499 for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000500 if (LiveRegs[*i] == latest)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000501 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000502 }
503
504 // dv is the DomainValue we are going to use for this instruction.
505 if (!dv)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000506 dv = alloc();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000507 dv->Dist = Distance;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000508 dv->AvailableDomains = available;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000509 dv->Instrs.push_back(mi);
510
511 // Finally set all defs and non-collapsed uses to dv.
512 for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
513 MachineOperand &mo = mi->getOperand(i);
514 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000515 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000516 if (rx < 0) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000517 if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000518 kill(rx);
519 setLiveReg(rx, dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000520 }
521 }
522}
523
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000524void ExeDepsFix::visitGenericInstr(MachineInstr *mi) {
525 // Process explicit defs, kill any relevant registers redefined.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000526 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
527 MachineOperand &mo = mi->getOperand(i);
528 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000529 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000530 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000531 kill(rx);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000532 }
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000533}
534
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000535bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000536 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000537 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000538 TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000539 LiveRegs = 0;
540 Distance = 0;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000541 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000542
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000543 // If no relevant registers are used in the function, we can skip it
544 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000545 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000546 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
547 I != E; ++I)
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000548 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000549 anyregs = true;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000550 break;
551 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000552 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000553
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000554 // Initialize the AliasMap on the first use.
555 if (AliasMap.empty()) {
556 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
557 // or -1.
558 AliasMap.resize(TRI->getNumRegs(), -1);
559 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
560 for (const unsigned *AI = TRI->getOverlaps(RC->getRegister(i)); *AI; ++AI)
561 AliasMap[*AI] = i;
562 }
563
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000564 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000565 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000566 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000567 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
568 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
569 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000570 if (enterBasicBlock(MBB))
571 Loops.push_back(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000572 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000573 ++I)
574 visitInstr(I);
575 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000576 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000577
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000578 // Visit all the loop blocks again in order to merge DomainValues from
579 // back-edges.
580 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
581 MachineBasicBlock *MBB = Loops[i];
582 enterBasicBlock(MBB);
583 leaveBasicBlock(MBB);
584 }
585
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000586 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
587 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
588 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
589 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000590 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000591 continue;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000592 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen0fdb05d2011-11-08 22:05:17 +0000593 if (FI->second[i])
594 release(FI->second[i]);
595 delete[] FI->second;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000596 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000597 LiveOuts.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000598 Avail.clear();
599 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000600
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000601 return false;
602}
603
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000604FunctionPass *
605llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000606 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000607}