blob: cf55b68b16f8d969e635309f5c6c689641e605cc [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
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/CodeGen/Passes.h"
24#include "llvm/ADT/PostOrderIterator.h"
Stephen Hines36b56882014-04-23 16:57:46 -070025#include "llvm/CodeGen/LivePhysRegs.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000028#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000033using namespace llvm;
34
Stephen Hinesdce4a402014-05-29 02:49:00 -070035#define DEBUG_TYPE "execution-fix"
36
Chris Lattner563d83f2010-03-31 20:32:51 +000037/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000038/// of execution domains.
39///
40/// An open DomainValue represents a set of instructions that can still switch
41/// execution domain. Multiple registers may refer to the same open
42/// DomainValue - they will eventually be collapsed to the same execution
43/// domain.
44///
45/// A collapsed DomainValue represents a single register that has been forced
46/// into one of more execution domains. There is a separate collapsed
47/// DomainValue for each register, but it may contain multiple execution
48/// domains. A register value is initially created in a single execution
49/// domain, but if we were forced to pay the penalty of a domain crossing, we
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +000050/// keep track of the fact that the register is now available in multiple
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000051/// domains.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000052namespace {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000053struct DomainValue {
54 // Basic reference counting.
55 unsigned Refs;
56
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000057 // Bitmask of available domains. For an open DomainValue, it is the still
58 // possible domains for collapsing. For a collapsed DomainValue it is the
59 // domains where the register is available for free.
60 unsigned AvailableDomains;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000061
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +000062 // Pointer to the next DomainValue in a chain. When two DomainValues are
63 // merged, Victim.Next is set to point to Victor, so old DomainValue
Benjamin Kramerd9b0b022012-06-02 10:20:22 +000064 // references can be updated by following the chain.
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +000065 DomainValue *Next;
66
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000067 // Twiddleable instructions using or defining these registers.
68 SmallVector<MachineInstr*, 8> Instrs;
69
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000070 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000071 // track of the domains where the registers are already available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000072 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000073
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000074 // Is domain available?
75 bool hasDomain(unsigned domain) const {
76 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000077 }
78
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000079 // Mark domain as available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000080 void addDomain(unsigned domain) {
81 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000082 }
83
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000084 // Restrict to a single domain available.
85 void setSingleDomain(unsigned domain) {
86 AvailableDomains = 1u << domain;
87 }
88
89 // Return bitmask of domains that are available and in mask.
90 unsigned getCommonDomains(unsigned mask) const {
91 return AvailableDomains & mask;
92 }
93
94 // First domain available.
95 unsigned getFirstDomain() const {
Michael J. Spencerc6af2432013-05-24 22:23:49 +000096 return countTrailingZeros(AvailableDomains);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000097 }
98
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +000099 DomainValue() : Refs(0) { clear(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000100
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000101 // Clear this DomainValue and point to next which has all its data.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000102 void clear() {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000103 AvailableDomains = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700104 Next = nullptr;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000105 Instrs.clear();
106 }
107};
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000108}
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000109
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000110namespace {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000111/// LiveReg - Information about a live register.
112struct LiveReg {
113 /// Value currently in this register, or NULL when no value is being tracked.
114 /// This counts as a DomainValue reference.
115 DomainValue *Value;
116
117 /// Instruction that defined this register, relative to the beginning of the
118 /// current basic block. When a LiveReg is used to represent a live-out
119 /// register, this value is relative to the end of the basic block, so it
120 /// will be a negative number.
121 int Def;
122};
123} // anonynous namespace
124
125namespace {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000126class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000127 static char ID;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000128 SpecificBumpPtrAllocator<DomainValue> Allocator;
129 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000130
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000131 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000132 MachineFunction *MF;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000133 const TargetInstrInfo *TII;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000134 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000135 std::vector<int> AliasMap;
136 const unsigned NumRegs;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000137 LiveReg *LiveRegs;
138 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000139 LiveOutMap LiveOuts;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000140
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000141 /// List of undefined register reads in this block in forward order.
142 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads;
143
144 /// Storage for register unit liveness.
Stephen Hines36b56882014-04-23 16:57:46 -0700145 LivePhysRegs LiveRegSet;
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000146
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000147 /// Current instruction number.
148 /// The first instruction in each basic block is 0.
149 int CurInstr;
150
151 /// True when the current block has a predecessor that hasn't been visited
152 /// yet.
153 bool SeenUnknownBackEdge;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000154
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000155public:
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000156 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000157 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000158
Stephen Hines36b56882014-04-23 16:57:46 -0700159 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000160 AU.setPreservesAll();
161 MachineFunctionPass::getAnalysisUsage(AU);
162 }
163
Stephen Hines36b56882014-04-23 16:57:46 -0700164 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000165
Stephen Hines36b56882014-04-23 16:57:46 -0700166 const char *getPassName() const override {
Jakob Stoklund Olesencd7dcad2011-11-07 21:23:39 +0000167 return "Execution dependency fix";
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000168 }
169
170private:
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000171 // Register mapping.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000172 int regIndex(unsigned Reg);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000173
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000174 // DomainValue allocation.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000175 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000176 DomainValue *retain(DomainValue *DV) {
177 if (DV) ++DV->Refs;
178 return DV;
179 }
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000180 void release(DomainValue*);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000181 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000182
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000183 // LiveRegs manipulations.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000184 void setLiveReg(int rx, DomainValue *DV);
185 void kill(int rx);
186 void force(int rx, unsigned domain);
187 void collapse(DomainValue *dv, unsigned domain);
188 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000189
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000190 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000191 void leaveBasicBlock(MachineBasicBlock*);
192 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000193 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000194 void visitSoftInstr(MachineInstr*, unsigned mask);
195 void visitHardInstr(MachineInstr*, unsigned domain);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000196 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
197 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000198};
199}
200
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000201char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000202
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000203/// Translate TRI register number to an index into our smaller tables of
204/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000205int ExeDepsFix::regIndex(unsigned Reg) {
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000206 assert(Reg < AliasMap.size() && "Invalid register");
207 return AliasMap[Reg];
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000208}
209
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000210DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000211 DomainValue *dv = Avail.empty() ?
212 new(Allocator.Allocate()) DomainValue :
213 Avail.pop_back_val();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000214 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000215 dv->addDomain(domain);
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000216 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000217 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000218 return dv;
219}
220
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000221/// release - Release a reference to DV. When the last reference is released,
222/// collapse if needed.
223void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000224 while (DV) {
225 assert(DV->Refs && "Bad DomainValue");
226 if (--DV->Refs)
227 return;
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000228
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000229 // There are no more DV references. Collapse any contained instructions.
230 if (DV->AvailableDomains && !DV->isCollapsed())
231 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000232
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000233 DomainValue *Next = DV->Next;
234 DV->clear();
235 Avail.push_back(DV);
236 // Also release the next DomainValue in the chain.
237 DV = Next;
238 }
239}
240
241/// resolve - Follow the chain of dead DomainValues until a live DomainValue is
242/// reached. Update the referenced pointer when necessary.
243DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
244 DomainValue *DV = DVRef;
245 if (!DV || !DV->Next)
246 return DV;
247
248 // DV has a chain. Find the end.
249 do DV = DV->Next;
250 while (DV->Next);
251
252 // Update DVRef to point to DV.
253 retain(DV);
254 release(DVRef);
255 DVRef = DV;
256 return DV;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000257}
258
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000259/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000260void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000261 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000262 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000263
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000264 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000265 return;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000266 if (LiveRegs[rx].Value)
267 release(LiveRegs[rx].Value);
268 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000269}
270
271// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000272void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000273 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000274 assert(LiveRegs && "Must enter basic block first.");
275 if (!LiveRegs[rx].Value)
276 return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000277
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000278 release(LiveRegs[rx].Value);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700279 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000280}
281
282/// Force register rx into domain.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000283void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000284 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000285 assert(LiveRegs && "Must enter basic block first.");
286 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000287 if (dv->isCollapsed())
288 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000289 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000290 collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000291 else {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000292 // This is an incompatible open DomainValue. Collapse it to whatever and
293 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000294 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000295 assert(LiveRegs[rx].Value && "Not live after collapse?");
296 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000297 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000298 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000299 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000300 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000301 }
302}
303
304/// Collapse open DomainValue into given domain. If there are multiple
305/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000306void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000307 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000308
309 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000310 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000311 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000312 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000313
314 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000315 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000316 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000317 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000318 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000319}
320
321/// Merge - All instructions and registers in B are moved to A, and B is
322/// released.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000323bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000324 assert(!A->isCollapsed() && "Cannot merge into collapsed");
325 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000326 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000327 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000328 // Restrict to the domains that A and B have in common.
329 unsigned common = A->getCommonDomains(B->AvailableDomains);
330 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000331 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000332 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000333 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000334
335 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000336 B->clear();
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000337 // All uses of B are referred to A.
338 B->Next = retain(A);
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000339
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000340 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000341 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000342 setLiveReg(rx, A);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000343 return true;
344}
345
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000346// enterBasicBlock - Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000347void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000348 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000349 SeenUnknownBackEdge = false;
350
351 // Reset instruction counter in each basic block.
352 CurInstr = 0;
353
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000354 // Set up UndefReads to track undefined register reads.
355 UndefReads.clear();
Stephen Hines36b56882014-04-23 16:57:46 -0700356 LiveRegSet.clear();
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000357
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000358 // Set up LiveRegs to represent registers entering MBB.
359 if (!LiveRegs)
360 LiveRegs = new LiveReg[NumRegs];
361
362 // Default values are 'nothing happened a long time ago'.
363 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700364 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000365 LiveRegs[rx].Def = -(1 << 20);
366 }
367
368 // This is the entry block.
369 if (MBB->pred_empty()) {
370 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
371 e = MBB->livein_end(); i != e; ++i) {
372 int rx = regIndex(*i);
373 if (rx < 0)
374 continue;
375 // Treat function live-ins as if they were defined just before the first
376 // instruction. Usually, function arguments are set up immediately
377 // before the call.
378 LiveRegs[rx].Def = -1;
379 }
380 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
381 return;
382 }
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000383
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000384 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000385 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
386 pe = MBB->pred_end(); pi != pe; ++pi) {
387 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
388 if (fi == LiveOuts.end()) {
389 SeenUnknownBackEdge = true;
390 continue;
391 }
392 assert(fi->second && "Can't have NULL entries");
393
394 for (unsigned rx = 0; rx != NumRegs; ++rx) {
395 // Use the most recent predecessor def for each register.
396 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
397
398 DomainValue *pdv = resolve(fi->second[rx].Value);
399 if (!pdv)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000400 continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000401 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000402 setLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000403 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000404 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000405
406 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000407 if (LiveRegs[rx].Value->isCollapsed()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700408 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000409 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
410 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
411 collapse(pdv, Domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000412 continue;
413 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000414
Chris Lattner563d83f2010-03-31 20:32:51 +0000415 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000416 if (!pdv->isCollapsed())
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000417 merge(LiveRegs[rx].Value, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000418 else
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000419 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000420 }
421 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000422 DEBUG(dbgs() << "BB#" << MBB->getNumber()
423 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000424}
425
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000426void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000427 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000428 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000429 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000430 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
431
432 if (First) {
433 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
434 // the end of this block instead of the beginning.
435 for (unsigned i = 0, e = NumRegs; i != e; ++i)
436 LiveRegs[i].Def -= CurInstr;
437 } else {
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000438 // Insertion failed, this must be the second pass.
439 // Release all the DomainValues instead of keeping them.
440 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000441 release(LiveRegs[i].Value);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000442 delete[] LiveRegs;
443 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700444 LiveRegs = nullptr;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000445}
446
447void ExeDepsFix::visitInstr(MachineInstr *MI) {
448 if (MI->isDebugValue())
449 return;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000450
451 // Update instructions with explicit execution domains.
452 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(MI);
453 if (DomP.first) {
454 if (DomP.second)
455 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000456 else
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000457 visitHardInstr(MI, DomP.first);
458 }
459
460 // Process defs to track register ages, and kill values clobbered by generic
461 // instructions.
462 processDefs(MI, !DomP.first);
463}
464
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000465/// \brief Return true to if it makes sense to break dependence on a partial def
466/// or undef use.
467bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
468 unsigned Pref) {
469 int rx = regIndex(MI->getOperand(OpIdx).getReg());
470 if (rx < 0)
471 return false;
472
473 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
474 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
475
476 if (Pref > Clearance) {
477 DEBUG(dbgs() << ": Break dependency.\n");
478 return true;
479 }
480 // The current clearance seems OK, but we may be ignoring a def from a
481 // back-edge.
482 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
483 DEBUG(dbgs() << ": OK .\n");
484 return false;
485 }
486 // A def from an unprocessed back-edge may make us break this dependency.
487 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
488 return false;
489}
490
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000491// Update def-ages for registers defined by MI.
492// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000493//
494// Also break dependencies on partial defs and undef uses.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000495void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
496 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000497
498 // Break dependence on undef uses. Do this before updating LiveRegs below.
499 unsigned OpNum;
500 unsigned Pref = TII->getUndefRegClearance(MI, OpNum, TRI);
501 if (Pref) {
502 if (shouldBreakDependence(MI, OpNum, Pref))
503 UndefReads.push_back(std::make_pair(MI, OpNum));
504 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000505 const MCInstrDesc &MCID = MI->getDesc();
506 for (unsigned i = 0,
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000507 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000508 i != e; ++i) {
509 MachineOperand &MO = MI->getOperand(i);
510 if (!MO.isReg())
511 continue;
512 if (MO.isImplicit())
513 break;
514 if (MO.isUse())
515 continue;
516 int rx = regIndex(MO.getReg());
517 if (rx < 0)
518 continue;
519
520 // This instruction explicitly defines rx.
521 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
522 << '\t' << *MI);
523
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000524 // Check clearance before partial register updates.
525 // Call breakDependence before setting LiveRegs[rx].Def.
526 unsigned Pref = TII->getPartialRegUpdateClearance(MI, i, TRI);
527 if (Pref && shouldBreakDependence(MI, i, Pref))
528 TII->breakPartialRegDependency(MI, i, TRI);
529
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000530 // How many instructions since rx was last written?
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000531 LiveRegs[rx].Def = CurInstr;
532
533 // Kill off domains redefined by generic instructions.
534 if (Kill)
535 kill(rx);
536 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000537 ++CurInstr;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000538}
539
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000540/// \break Break false dependencies on undefined register reads.
541///
542/// Walk the block backward computing precise liveness. This is expensive, so we
543/// only do it on demand. Note that the occurrence of undefined register reads
544/// that should be broken is very rare, but when they occur we may have many in
545/// a single block.
546void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
547 if (UndefReads.empty())
548 return;
549
550 // Collect this block's live out register units.
Stephen Hines36b56882014-04-23 16:57:46 -0700551 LiveRegSet.init(TRI);
552 LiveRegSet.addLiveOuts(MBB);
553
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000554 MachineInstr *UndefMI = UndefReads.back().first;
555 unsigned OpIdx = UndefReads.back().second;
556
557 for (MachineBasicBlock::reverse_iterator I = MBB->rbegin(), E = MBB->rend();
558 I != E; ++I) {
Stephen Hines36b56882014-04-23 16:57:46 -0700559 // Update liveness, including the current instruction's defs.
560 LiveRegSet.stepBackward(*I);
Andrew Trick51dee242013-10-15 03:39:43 +0000561
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000562 if (UndefMI == &*I) {
Stephen Hines36b56882014-04-23 16:57:46 -0700563 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000564 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI);
565
566 UndefReads.pop_back();
567 if (UndefReads.empty())
568 return;
569
570 UndefMI = UndefReads.back().first;
571 OpIdx = UndefReads.back().second;
572 }
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000573 }
574}
575
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000576// A hard instruction only works in one domain. All input registers will be
577// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000578void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000579 // Collapse all uses.
580 for (unsigned i = mi->getDesc().getNumDefs(),
581 e = mi->getDesc().getNumOperands(); i != e; ++i) {
582 MachineOperand &mo = mi->getOperand(i);
583 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000584 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000585 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000586 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000587 }
588
589 // Kill all defs and force them.
590 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
591 MachineOperand &mo = mi->getOperand(i);
592 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000593 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000594 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000595 kill(rx);
596 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000597 }
598}
599
600// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000601void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000602 // Bitmask of available domains for this instruction after taking collapsed
603 // operands into account.
604 unsigned available = mask;
605
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000606 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000607 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000608 if (LiveRegs)
609 for (unsigned i = mi->getDesc().getNumDefs(),
610 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000611 MachineOperand &mo = mi->getOperand(i);
612 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000613 int rx = regIndex(mo.getReg());
Chris Lattner563d83f2010-03-31 20:32:51 +0000614 if (rx < 0) continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000615 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000616 // Bitmask of domains that dv and available have in common.
617 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000618 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000619 if (dv->isCollapsed()) {
620 // Restrict available domains to the ones in common with the operand.
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000621 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000622 // penalty for this operand.
623 if (common) available = common;
624 } else if (common)
625 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000626 used.push_back(rx);
627 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000628 // Open DomainValue is not compatible with instruction. It is useless
629 // now.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000630 kill(rx);
Chris Lattner563d83f2010-03-31 20:32:51 +0000631 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000632 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000633
634 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000635 if (isPowerOf2_32(available)) {
Michael J. Spencerc6af2432013-05-24 22:23:49 +0000636 unsigned domain = countTrailingZeros(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000637 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000638 visitHardInstr(mi, domain);
639 return;
640 }
641
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000642 // Kill off any remaining uses that don't match available, and build a list of
643 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000644 SmallVector<LiveReg, 4> Regs;
Craig Topperf22fd3f2013-07-03 05:11:49 +0000645 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000646 int rx = *i;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000647 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000648 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000649 if (!LR.Value->getCommonDomains(available)) {
650 kill(rx);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000651 continue;
652 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000653 // Sorted insertion.
654 bool Inserted = false;
Craig Topperf22fd3f2013-07-03 05:11:49 +0000655 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000656 i != e && !Inserted; ++i) {
657 if (LR.Def < i->Def) {
658 Inserted = true;
659 Regs.insert(i, LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000660 }
661 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000662 if (!Inserted)
663 Regs.push_back(LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000664 }
665
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000666 // doms are now sorted in order of appearance. Try to merge them all, giving
667 // priority to the latest ones.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700668 DomainValue *dv = nullptr;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000669 while (!Regs.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000670 if (!dv) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000671 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000672 // Force the first dv to match the current instruction.
673 dv->AvailableDomains = dv->getCommonDomains(available);
674 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner563d83f2010-03-31 20:32:51 +0000675 continue;
676 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000677
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000678 DomainValue *Latest = Regs.pop_back_val().Value;
679 // Skip already merged values.
680 if (Latest == dv || Latest->Next)
681 continue;
682 if (merge(dv, Latest))
683 continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000684
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000685 // If latest didn't merge, it is useless now. Kill all registers using it.
Craig Topperf22fd3f2013-07-03 05:11:49 +0000686 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000687 if (LiveRegs[*i].Value == Latest)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000688 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000689 }
690
691 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000692 if (!dv) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000693 dv = alloc();
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000694 dv->AvailableDomains = available;
695 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000696 dv->Instrs.push_back(mi);
697
Silviu Baranga541a8582012-10-03 08:29:36 +0000698 // Finally set all defs and non-collapsed uses to dv. We must iterate through
699 // all the operators, including imp-def ones.
700 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
701 ee = mi->operands_end();
702 ii != ee; ++ii) {
703 MachineOperand &mo = *ii;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000704 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000705 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000706 if (rx < 0) continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000707 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000708 kill(rx);
709 setLiveReg(rx, dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000710 }
711 }
712}
713
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000714bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000715 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000716 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000717 TRI = MF->getTarget().getRegisterInfo();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700718 LiveRegs = nullptr;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000719 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000720
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000721 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
722 << RC->getName() << " **********\n");
723
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000724 // If no relevant registers are used in the function, we can skip it
725 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000726 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000727 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
728 I != E; ++I)
Jakob Stoklund Olesen9aa6e0a2012-10-17 18:44:18 +0000729 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesena2a98fd2011-12-21 19:50:05 +0000730 anyregs = true;
731 break;
732 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000733 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000734
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000735 // Initialize the AliasMap on the first use.
736 if (AliasMap.empty()) {
737 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
738 // or -1.
739 AliasMap.resize(TRI->getNumRegs(), -1);
740 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000741 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
742 AI.isValid(); ++AI)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000743 AliasMap[*AI] = i;
744 }
745
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000746 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000747 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000748 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000749 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
750 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
751 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000752 enterBasicBlock(MBB);
753 if (SeenUnknownBackEdge)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000754 Loops.push_back(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000755 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000756 ++I)
757 visitInstr(I);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000758 processUndefReads(MBB);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000759 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000760 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000761
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000762 // Visit all the loop blocks again in order to merge DomainValues from
763 // back-edges.
764 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
765 MachineBasicBlock *MBB = Loops[i];
766 enterBasicBlock(MBB);
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000767 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
768 ++I)
769 if (!I->isDebugValue())
770 processDefs(I, false);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000771 processUndefReads(MBB);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000772 leaveBasicBlock(MBB);
773 }
774
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000775 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
776 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
777 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
778 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000779 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000780 continue;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000781 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000782 if (FI->second[i].Value)
783 release(FI->second[i].Value);
Jakob Stoklund Olesen0fdb05d2011-11-08 22:05:17 +0000784 delete[] FI->second;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000785 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000786 LiveOuts.clear();
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000787 UndefReads.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000788 Avail.clear();
789 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000790
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000791 return false;
792}
793
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000794FunctionPass *
795llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000796 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000797}