blob: c25f7db26c1111682a75ff12417895a2628608ae [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 Olesend8f9f342011-11-07 21:23:42 +0000163 void 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 Olesend8f9f342011-11-07 21:23:42 +0000320void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000321 // Try to coalesce live-out registers from predecessors.
Dan Gohman81bf03e2010-04-13 16:57:55 +0000322 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000323 e = MBB->livein_end(); i != e; ++i) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000324 int rx = regIndex(*i);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000325 if (rx < 0) continue;
326 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
327 pe = MBB->pred_end(); pi != pe; ++pi) {
Jakob Stoklund Olesenb16df902010-03-31 00:40:08 +0000328 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000329 if (fi == LiveOuts.end()) continue;
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000330 DomainValue *pdv = resolve(fi->second[rx]);
331 if (!pdv) continue;
Chris Lattner563d83f2010-03-31 20:32:51 +0000332 if (!LiveRegs || !LiveRegs[rx]) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000333 setLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000334 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000335 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000336
337 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000338 if (LiveRegs[rx]->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000339 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000340 unsigned domain = LiveRegs[rx]->getFirstDomain();
341 if (!pdv->isCollapsed() && pdv->hasDomain(domain))
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000342 collapse(pdv, domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000343 continue;
344 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000345
Chris Lattner563d83f2010-03-31 20:32:51 +0000346 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000347 if (!pdv->isCollapsed())
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000348 merge(LiveRegs[rx], pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000349 else
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000350 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000351 }
352 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000353}
354
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000355void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
356 // Save live registers at end of MBB - used by enterBasicBlock().
357 if (LiveRegs)
358 LiveOuts.insert(std::make_pair(MBB, LiveRegs));
359 LiveRegs = 0;
360}
361
362void ExeDepsFix::visitInstr(MachineInstr *MI) {
363 if (MI->isDebugValue())
364 return;
365 ++Distance;
366 std::pair<uint16_t, uint16_t> domp = TII->getExecutionDomain(MI);
367 if (domp.first)
368 if (domp.second)
369 visitSoftInstr(MI, domp.second);
370 else
371 visitHardInstr(MI, domp.first);
372 else if (LiveRegs)
373 visitGenericInstr(MI);
374}
375
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000376// A hard instruction only works in one domain. All input registers will be
377// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000378void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000379 // Collapse all uses.
380 for (unsigned i = mi->getDesc().getNumDefs(),
381 e = mi->getDesc().getNumOperands(); i != e; ++i) {
382 MachineOperand &mo = mi->getOperand(i);
383 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000384 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000385 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000386 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000387 }
388
389 // Kill all defs and force them.
390 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
391 MachineOperand &mo = mi->getOperand(i);
392 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000393 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000394 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000395 kill(rx);
396 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000397 }
398}
399
400// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000401void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000402 // Bitmask of available domains for this instruction after taking collapsed
403 // operands into account.
404 unsigned available = mask;
405
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000406 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000407 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000408 if (LiveRegs)
409 for (unsigned i = mi->getDesc().getNumDefs(),
410 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000411 MachineOperand &mo = mi->getOperand(i);
412 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000413 int rx = regIndex(mo.getReg());
Chris Lattner563d83f2010-03-31 20:32:51 +0000414 if (rx < 0) continue;
415 if (DomainValue *dv = LiveRegs[rx]) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000416 // Bitmask of domains that dv and available have in common.
417 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000418 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000419 if (dv->isCollapsed()) {
420 // Restrict available domains to the ones in common with the operand.
421 // If there are no common domains, we must pay the cross-domain
422 // penalty for this operand.
423 if (common) available = common;
424 } else if (common)
425 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000426 used.push_back(rx);
427 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000428 // Open DomainValue is not compatible with instruction. It is useless
429 // now.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000430 kill(rx);
Chris Lattner563d83f2010-03-31 20:32:51 +0000431 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000432 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000433
434 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000435 if (isPowerOf2_32(available)) {
436 unsigned domain = CountTrailingZeros_32(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000437 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000438 visitHardInstr(mi, domain);
439 return;
440 }
441
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000442 // Kill off any remaining uses that don't match available, and build a list of
443 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000444 SmallVector<DomainValue*,4> doms;
445 for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
446 int rx = *i;
447 DomainValue *dv = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000448 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000449 if (!dv->getCommonDomains(available)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000450 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000451 continue;
452 }
453 // sorted, uniqued insert.
454 bool inserted = false;
455 for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end();
456 i != e && !inserted; ++i) {
457 if (dv == *i)
458 inserted = true;
459 else if (dv->Dist < (*i)->Dist) {
460 inserted = true;
461 doms.insert(i, dv);
462 }
463 }
464 if (!inserted)
465 doms.push_back(dv);
466 }
467
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000468 // doms are now sorted in order of appearance. Try to merge them all, giving
469 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000470 DomainValue *dv = 0;
471 while (!doms.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000472 if (!dv) {
473 dv = doms.pop_back_val();
474 continue;
475 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000476
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000477 DomainValue *latest = doms.pop_back_val();
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000478 if (merge(dv, latest)) continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000479
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000480 // If latest didn't merge, it is useless now. Kill all registers using it.
Chris Lattner563d83f2010-03-31 20:32:51 +0000481 for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000482 if (LiveRegs[*i] == latest)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000483 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000484 }
485
486 // dv is the DomainValue we are going to use for this instruction.
487 if (!dv)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000488 dv = alloc();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000489 dv->Dist = Distance;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000490 dv->AvailableDomains = available;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000491 dv->Instrs.push_back(mi);
492
493 // Finally set all defs and non-collapsed uses to dv.
494 for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
495 MachineOperand &mo = mi->getOperand(i);
496 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000497 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000498 if (rx < 0) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000499 if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000500 kill(rx);
501 setLiveReg(rx, dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000502 }
503 }
504}
505
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000506void ExeDepsFix::visitGenericInstr(MachineInstr *mi) {
507 // Process explicit defs, kill any relevant registers redefined.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000508 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
509 MachineOperand &mo = mi->getOperand(i);
510 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000511 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000512 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000513 kill(rx);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000514 }
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000515}
516
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000517bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000518 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000519 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000520 TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000521 LiveRegs = 0;
522 Distance = 0;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000523 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000524
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000525 // If no relevant registers are used in the function, we can skip it
526 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000527 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000528 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
529 I != E; ++I)
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000530 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000531 anyregs = true;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000532 break;
533 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000534 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000535
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000536 // Initialize the AliasMap on the first use.
537 if (AliasMap.empty()) {
538 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
539 // or -1.
540 AliasMap.resize(TRI->getNumRegs(), -1);
541 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
542 for (const unsigned *AI = TRI->getOverlaps(RC->getRegister(i)); *AI; ++AI)
543 AliasMap[*AI] = i;
544 }
545
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000546 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000547 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
548 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
549 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
550 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesend8f9f342011-11-07 21:23:42 +0000551 enterBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000552 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000553 ++I)
554 visitInstr(I);
555 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000556 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000557
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000558 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
559 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
560 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
561 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
562 if (FI == LiveOuts.end())
563 continue;
564 assert(FI->second && "Null entry");
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000565 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen0fdb05d2011-11-08 22:05:17 +0000566 if (FI->second[i])
567 release(FI->second[i]);
568 delete[] FI->second;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000569 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000570 LiveOuts.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000571 Avail.clear();
572 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000573
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000574 return false;
575}
576
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000577FunctionPass *
578llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000579 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000580}