blob: 5a75fde2d6d7a49fbbea414b39402d5c3d3eee39 [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.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000138 int regIndex(unsigned Reg);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000139
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000140 // DomainValue allocation.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000141 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000142 void release(DomainValue*);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000143
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000144 // LiveRegs manipulations.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000145 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);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000150
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 Olesen6bcb9a72011-11-08 21:57:47 +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 Olesen6bcb9a72011-11-08 21:57:47 +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 Olesen35e93242011-11-08 21:57:44 +0000179/// release - Release a reference to DV. When the last reference is released,
180/// collapse if needed.
181void ExeDepsFix::release(DomainValue *DV) {
182 assert(DV && DV->Refs && "Bad DomainValue");
183 if (--DV->Refs)
184 return;
185
186 // There are no more DV references. Collapse any contained instructions.
187 if (DV->AvailableDomains && !DV->isCollapsed())
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000188 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000189
190 DV->clear();
191 Avail.push_back(DV);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000192}
193
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000194/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000195void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000196 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000197 if (!LiveRegs) {
198 LiveRegs = new DomainValue*[NumRegs];
199 std::fill(LiveRegs, LiveRegs+NumRegs, (DomainValue*)0);
200 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000201
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000202 if (LiveRegs[rx] == dv)
203 return;
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000204 if (LiveRegs[rx])
205 release(LiveRegs[rx]);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000206 LiveRegs[rx] = dv;
207 if (dv) ++dv->Refs;
208}
209
210// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000211void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000212 assert(unsigned(rx) < NumRegs && "Invalid index");
213 if (!LiveRegs || !LiveRegs[rx]) return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000214
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000215 release(LiveRegs[rx]);
216 LiveRegs[rx] = 0;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000217}
218
219/// Force register rx into domain.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000220void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000221 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesend77f8182010-03-30 00:09:32 +0000222 DomainValue *dv;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000223 if (LiveRegs && (dv = LiveRegs[rx])) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000224 if (dv->isCollapsed())
225 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000226 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000227 collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000228 else {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000229 // This is an incompatible open DomainValue. Collapse it to whatever and
230 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000231 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000232 assert(LiveRegs[rx] && "Not live after collapse?");
233 LiveRegs[rx]->addDomain(domain);
234 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000235 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000236 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000237 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000238 }
239}
240
241/// Collapse open DomainValue into given domain. If there are multiple
242/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000243void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000244 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000245
246 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000247 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000248 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000249 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000250
251 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000252 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000253 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000254 if (LiveRegs[rx] == dv)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000255 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000256}
257
258/// Merge - All instructions and registers in B are moved to A, and B is
259/// released.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000260bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000261 assert(!A->isCollapsed() && "Cannot merge into collapsed");
262 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000263 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000264 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000265 // Restrict to the domains that A and B have in common.
266 unsigned common = A->getCommonDomains(B->AvailableDomains);
267 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000268 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000269 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000270 A->Dist = std::max(A->Dist, B->Dist);
271 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000272
273 // Clear the old DomainValue so we won't try to swizzle instructions twice.
274 B->Instrs.clear();
275 B->AvailableDomains = 0;
276
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000277 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000278 if (LiveRegs[rx] == B)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000279 setLiveReg(rx, A);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000280 return true;
281}
282
Jakob Stoklund Olesend8f9f342011-11-07 21:23:42 +0000283void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000284 // Try to coalesce live-out registers from predecessors.
Dan Gohman81bf03e2010-04-13 16:57:55 +0000285 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000286 e = MBB->livein_end(); i != e; ++i) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000287 int rx = regIndex(*i);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000288 if (rx < 0) continue;
289 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
290 pe = MBB->pred_end(); pi != pe; ++pi) {
Jakob Stoklund Olesenb16df902010-03-31 00:40:08 +0000291 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000292 if (fi == LiveOuts.end()) continue;
293 DomainValue *pdv = fi->second[rx];
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000294 if (!pdv || !pdv->AvailableDomains) continue;
Chris Lattner563d83f2010-03-31 20:32:51 +0000295 if (!LiveRegs || !LiveRegs[rx]) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000296 setLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000297 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000298 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000299
300 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000301 if (LiveRegs[rx]->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000302 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000303 unsigned domain = LiveRegs[rx]->getFirstDomain();
304 if (!pdv->isCollapsed() && pdv->hasDomain(domain))
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000305 collapse(pdv, domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000306 continue;
307 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000308
Chris Lattner563d83f2010-03-31 20:32:51 +0000309 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000310 if (!pdv->isCollapsed())
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000311 merge(LiveRegs[rx], pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000312 else
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000313 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000314 }
315 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000316}
317
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000318void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
319 // Save live registers at end of MBB - used by enterBasicBlock().
320 if (LiveRegs)
321 LiveOuts.insert(std::make_pair(MBB, LiveRegs));
322 LiveRegs = 0;
323}
324
325void ExeDepsFix::visitInstr(MachineInstr *MI) {
326 if (MI->isDebugValue())
327 return;
328 ++Distance;
329 std::pair<uint16_t, uint16_t> domp = TII->getExecutionDomain(MI);
330 if (domp.first)
331 if (domp.second)
332 visitSoftInstr(MI, domp.second);
333 else
334 visitHardInstr(MI, domp.first);
335 else if (LiveRegs)
336 visitGenericInstr(MI);
337}
338
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000339// A hard instruction only works in one domain. All input registers will be
340// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000341void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000342 // Collapse all uses.
343 for (unsigned i = mi->getDesc().getNumDefs(),
344 e = mi->getDesc().getNumOperands(); i != e; ++i) {
345 MachineOperand &mo = mi->getOperand(i);
346 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000347 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000348 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000349 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000350 }
351
352 // Kill all defs and force them.
353 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
354 MachineOperand &mo = mi->getOperand(i);
355 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000356 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000357 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000358 kill(rx);
359 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000360 }
361}
362
363// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000364void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000365 // Bitmask of available domains for this instruction after taking collapsed
366 // operands into account.
367 unsigned available = mask;
368
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000369 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000370 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000371 if (LiveRegs)
372 for (unsigned i = mi->getDesc().getNumDefs(),
373 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000374 MachineOperand &mo = mi->getOperand(i);
375 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000376 int rx = regIndex(mo.getReg());
Chris Lattner563d83f2010-03-31 20:32:51 +0000377 if (rx < 0) continue;
378 if (DomainValue *dv = LiveRegs[rx]) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000379 // Bitmask of domains that dv and available have in common.
380 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000381 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000382 if (dv->isCollapsed()) {
383 // Restrict available domains to the ones in common with the operand.
384 // If there are no common domains, we must pay the cross-domain
385 // penalty for this operand.
386 if (common) available = common;
387 } else if (common)
388 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000389 used.push_back(rx);
390 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000391 // Open DomainValue is not compatible with instruction. It is useless
392 // now.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000393 kill(rx);
Chris Lattner563d83f2010-03-31 20:32:51 +0000394 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000395 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000396
397 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000398 if (isPowerOf2_32(available)) {
399 unsigned domain = CountTrailingZeros_32(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000400 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000401 visitHardInstr(mi, domain);
402 return;
403 }
404
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000405 // Kill off any remaining uses that don't match available, and build a list of
406 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000407 SmallVector<DomainValue*,4> doms;
408 for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
409 int rx = *i;
410 DomainValue *dv = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000411 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000412 if (!dv->getCommonDomains(available)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000413 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000414 continue;
415 }
416 // sorted, uniqued insert.
417 bool inserted = false;
418 for (SmallVector<DomainValue*,4>::iterator i = doms.begin(), e = doms.end();
419 i != e && !inserted; ++i) {
420 if (dv == *i)
421 inserted = true;
422 else if (dv->Dist < (*i)->Dist) {
423 inserted = true;
424 doms.insert(i, dv);
425 }
426 }
427 if (!inserted)
428 doms.push_back(dv);
429 }
430
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000431 // doms are now sorted in order of appearance. Try to merge them all, giving
432 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000433 DomainValue *dv = 0;
434 while (!doms.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000435 if (!dv) {
436 dv = doms.pop_back_val();
437 continue;
438 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000439
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000440 DomainValue *latest = doms.pop_back_val();
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000441 if (merge(dv, latest)) continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000442
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000443 // If latest didn't merge, it is useless now. Kill all registers using it.
Chris Lattner563d83f2010-03-31 20:32:51 +0000444 for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000445 if (LiveRegs[*i] == latest)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000446 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000447 }
448
449 // dv is the DomainValue we are going to use for this instruction.
450 if (!dv)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000451 dv = alloc();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000452 dv->Dist = Distance;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000453 dv->AvailableDomains = available;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000454 dv->Instrs.push_back(mi);
455
456 // Finally set all defs and non-collapsed uses to dv.
457 for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
458 MachineOperand &mo = mi->getOperand(i);
459 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000460 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000461 if (rx < 0) continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000462 if (!LiveRegs || !LiveRegs[rx] || (mo.isDef() && LiveRegs[rx]!=dv)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000463 kill(rx);
464 setLiveReg(rx, dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000465 }
466 }
467}
468
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000469void ExeDepsFix::visitGenericInstr(MachineInstr *mi) {
470 // Process explicit defs, kill any relevant registers redefined.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000471 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
472 MachineOperand &mo = mi->getOperand(i);
473 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000474 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000475 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000476 kill(rx);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000477 }
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000478}
479
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000480bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000481 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000482 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000483 TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000484 LiveRegs = 0;
485 Distance = 0;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000486 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000487
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000488 // If no relevant registers are used in the function, we can skip it
489 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000490 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000491 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
492 I != E; ++I)
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000493 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000494 anyregs = true;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000495 break;
496 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000497 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000498
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000499 // Initialize the AliasMap on the first use.
500 if (AliasMap.empty()) {
501 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
502 // or -1.
503 AliasMap.resize(TRI->getNumRegs(), -1);
504 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
505 for (const unsigned *AI = TRI->getOverlaps(RC->getRegister(i)); *AI; ++AI)
506 AliasMap[*AI] = i;
507 }
508
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000509 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000510 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
511 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
512 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
513 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesend8f9f342011-11-07 21:23:42 +0000514 enterBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000515 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000516 ++I)
517 visitInstr(I);
518 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000519 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000520
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000521 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
522 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
523 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
524 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
525 if (FI == LiveOuts.end())
526 continue;
527 assert(FI->second && "Null entry");
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000528 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen0fdb05d2011-11-08 22:05:17 +0000529 if (FI->second[i])
530 release(FI->second[i]);
531 delete[] FI->second;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000532 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000533 LiveOuts.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000534 Avail.clear();
535 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000536
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000537 return false;
538}
539
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000540FunctionPass *
541llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000542 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000543}