blob: 3680498927effd4051e8d94f94e0126663571ac6 [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"
Stephen Hines37ed9c12014-12-01 14:51:49 -080032#include "llvm/Target/TargetSubtargetInfo.h"
33
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000034using namespace llvm;
35
Stephen Hinesdce4a402014-05-29 02:49:00 -070036#define DEBUG_TYPE "execution-fix"
37
Chris Lattner563d83f2010-03-31 20:32:51 +000038/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000039/// of execution domains.
40///
41/// An open DomainValue represents a set of instructions that can still switch
42/// execution domain. Multiple registers may refer to the same open
43/// DomainValue - they will eventually be collapsed to the same execution
44/// domain.
45///
46/// A collapsed DomainValue represents a single register that has been forced
47/// into one of more execution domains. There is a separate collapsed
48/// DomainValue for each register, but it may contain multiple execution
49/// domains. A register value is initially created in a single execution
50/// domain, but if we were forced to pay the penalty of a domain crossing, we
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +000051/// keep track of the fact that the register is now available in multiple
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000052/// domains.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000053namespace {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000054struct DomainValue {
55 // Basic reference counting.
56 unsigned Refs;
57
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000058 // Bitmask of available domains. For an open DomainValue, it is the still
59 // possible domains for collapsing. For a collapsed DomainValue it is the
60 // domains where the register is available for free.
61 unsigned AvailableDomains;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000062
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
Benjamin Kramerd9b0b022012-06-02 10:20:22 +000065 // references can be updated by following the chain.
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +000066 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 {
Michael J. Spencerc6af2432013-05-24 22:23:49 +000097 return countTrailingZeros(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 Olesen2947f732011-11-15 01:15:25 +0000104 AvailableDomains = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700105 Next = nullptr;
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 Olesen2947f732011-11-15 01:15:25 +0000112/// LiveReg - Information about a live register.
113struct LiveReg {
114 /// Value currently in this register, or NULL when no value is being tracked.
115 /// This counts as a DomainValue reference.
116 DomainValue *Value;
117
118 /// Instruction that defined this register, relative to the beginning of the
119 /// current basic block. When a LiveReg is used to represent a live-out
120 /// register, this value is relative to the end of the basic block, so it
121 /// will be a negative number.
122 int Def;
123};
124} // anonynous namespace
125
126namespace {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000127class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000128 static char ID;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000129 SpecificBumpPtrAllocator<DomainValue> Allocator;
130 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000131
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000132 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000133 MachineFunction *MF;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000134 const TargetInstrInfo *TII;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000135 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000136 std::vector<int> AliasMap;
137 const unsigned NumRegs;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000138 LiveReg *LiveRegs;
139 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000140 LiveOutMap LiveOuts;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000141
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000142 /// List of undefined register reads in this block in forward order.
143 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads;
144
145 /// Storage for register unit liveness.
Stephen Hines36b56882014-04-23 16:57:46 -0700146 LivePhysRegs LiveRegSet;
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000147
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000148 /// Current instruction number.
149 /// The first instruction in each basic block is 0.
150 int CurInstr;
151
152 /// True when the current block has a predecessor that hasn't been visited
153 /// yet.
154 bool SeenUnknownBackEdge;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000155
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000156public:
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000157 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000158 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000159
Stephen Hines36b56882014-04-23 16:57:46 -0700160 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000161 AU.setPreservesAll();
162 MachineFunctionPass::getAnalysisUsage(AU);
163 }
164
Stephen Hines36b56882014-04-23 16:57:46 -0700165 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000166
Stephen Hines36b56882014-04-23 16:57:46 -0700167 const char *getPassName() const override {
Jakob Stoklund Olesencd7dcad2011-11-07 21:23:39 +0000168 return "Execution dependency fix";
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000169 }
170
171private:
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000172 // Register mapping.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000173 int regIndex(unsigned Reg);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000174
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000175 // DomainValue allocation.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000176 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000177 DomainValue *retain(DomainValue *DV) {
178 if (DV) ++DV->Refs;
179 return DV;
180 }
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000181 void release(DomainValue*);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000182 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000183
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000184 // LiveRegs manipulations.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000185 void setLiveReg(int rx, DomainValue *DV);
186 void kill(int rx);
187 void force(int rx, unsigned domain);
188 void collapse(DomainValue *dv, unsigned domain);
189 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000190
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000191 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000192 void leaveBasicBlock(MachineBasicBlock*);
193 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000194 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000195 void visitSoftInstr(MachineInstr*, unsigned mask);
196 void visitHardInstr(MachineInstr*, unsigned domain);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000197 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
198 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000199};
200}
201
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000202char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000203
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000204/// Translate TRI register number to an index into our smaller tables of
205/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000206int ExeDepsFix::regIndex(unsigned Reg) {
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000207 assert(Reg < AliasMap.size() && "Invalid register");
208 return AliasMap[Reg];
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000209}
210
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000211DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000212 DomainValue *dv = Avail.empty() ?
213 new(Allocator.Allocate()) DomainValue :
214 Avail.pop_back_val();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000215 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000216 dv->addDomain(domain);
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000217 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000218 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000219 return dv;
220}
221
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000222/// release - Release a reference to DV. When the last reference is released,
223/// collapse if needed.
224void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000225 while (DV) {
226 assert(DV->Refs && "Bad DomainValue");
227 if (--DV->Refs)
228 return;
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000229
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000230 // There are no more DV references. Collapse any contained instructions.
231 if (DV->AvailableDomains && !DV->isCollapsed())
232 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000233
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000234 DomainValue *Next = DV->Next;
235 DV->clear();
236 Avail.push_back(DV);
237 // Also release the next DomainValue in the chain.
238 DV = Next;
239 }
240}
241
242/// resolve - Follow the chain of dead DomainValues until a live DomainValue is
243/// reached. Update the referenced pointer when necessary.
244DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
245 DomainValue *DV = DVRef;
246 if (!DV || !DV->Next)
247 return DV;
248
249 // DV has a chain. Find the end.
250 do DV = DV->Next;
251 while (DV->Next);
252
253 // Update DVRef to point to DV.
254 retain(DV);
255 release(DVRef);
256 DVRef = DV;
257 return DV;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000258}
259
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000260/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000261void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000262 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000263 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000264
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000265 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000266 return;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000267 if (LiveRegs[rx].Value)
268 release(LiveRegs[rx].Value);
269 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000270}
271
272// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000273void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000274 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000275 assert(LiveRegs && "Must enter basic block first.");
276 if (!LiveRegs[rx].Value)
277 return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000278
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000279 release(LiveRegs[rx].Value);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700280 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000281}
282
283/// Force register rx into domain.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000284void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000285 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000286 assert(LiveRegs && "Must enter basic block first.");
287 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000288 if (dv->isCollapsed())
289 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000290 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000291 collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000292 else {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000293 // This is an incompatible open DomainValue. Collapse it to whatever and
294 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000295 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000296 assert(LiveRegs[rx].Value && "Not live after collapse?");
297 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000298 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000299 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000300 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000301 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000302 }
303}
304
305/// Collapse open DomainValue into given domain. If there are multiple
306/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000307void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000308 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000309
310 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000311 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000312 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000313 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000314
315 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000316 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000317 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000318 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000319 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000320}
321
322/// Merge - All instructions and registers in B are moved to A, and B is
323/// released.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000324bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000325 assert(!A->isCollapsed() && "Cannot merge into collapsed");
326 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000327 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000328 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000329 // Restrict to the domains that A and B have in common.
330 unsigned common = A->getCommonDomains(B->AvailableDomains);
331 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000332 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000333 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000334 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000335
336 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000337 B->clear();
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000338 // All uses of B are referred to A.
339 B->Next = retain(A);
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000340
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000341 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000342 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000343 setLiveReg(rx, A);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000344 return true;
345}
346
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000347// enterBasicBlock - Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000348void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000349 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000350 SeenUnknownBackEdge = false;
351
352 // Reset instruction counter in each basic block.
353 CurInstr = 0;
354
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000355 // Set up UndefReads to track undefined register reads.
356 UndefReads.clear();
Stephen Hines36b56882014-04-23 16:57:46 -0700357 LiveRegSet.clear();
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000358
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000359 // Set up LiveRegs to represent registers entering MBB.
360 if (!LiveRegs)
361 LiveRegs = new LiveReg[NumRegs];
362
363 // Default values are 'nothing happened a long time ago'.
364 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700365 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000366 LiveRegs[rx].Def = -(1 << 20);
367 }
368
369 // This is the entry block.
370 if (MBB->pred_empty()) {
371 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
372 e = MBB->livein_end(); i != e; ++i) {
373 int rx = regIndex(*i);
374 if (rx < 0)
375 continue;
376 // Treat function live-ins as if they were defined just before the first
377 // instruction. Usually, function arguments are set up immediately
378 // before the call.
379 LiveRegs[rx].Def = -1;
380 }
381 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
382 return;
383 }
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000384
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000385 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000386 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
387 pe = MBB->pred_end(); pi != pe; ++pi) {
388 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
389 if (fi == LiveOuts.end()) {
390 SeenUnknownBackEdge = true;
391 continue;
392 }
393 assert(fi->second && "Can't have NULL entries");
394
395 for (unsigned rx = 0; rx != NumRegs; ++rx) {
396 // Use the most recent predecessor def for each register.
397 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
398
399 DomainValue *pdv = resolve(fi->second[rx].Value);
400 if (!pdv)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000401 continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000402 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000403 setLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000404 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000405 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000406
407 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000408 if (LiveRegs[rx].Value->isCollapsed()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700409 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000410 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
411 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
412 collapse(pdv, Domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000413 continue;
414 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000415
Chris Lattner563d83f2010-03-31 20:32:51 +0000416 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000417 if (!pdv->isCollapsed())
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000418 merge(LiveRegs[rx].Value, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000419 else
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000420 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000421 }
422 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000423 DEBUG(dbgs() << "BB#" << MBB->getNumber()
424 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000425}
426
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000427void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000428 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000429 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000430 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000431 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
432
433 if (First) {
434 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
435 // the end of this block instead of the beginning.
436 for (unsigned i = 0, e = NumRegs; i != e; ++i)
437 LiveRegs[i].Def -= CurInstr;
438 } else {
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000439 // Insertion failed, this must be the second pass.
440 // Release all the DomainValues instead of keeping them.
441 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000442 release(LiveRegs[i].Value);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000443 delete[] LiveRegs;
444 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700445 LiveRegs = nullptr;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000446}
447
448void ExeDepsFix::visitInstr(MachineInstr *MI) {
449 if (MI->isDebugValue())
450 return;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000451
452 // Update instructions with explicit execution domains.
453 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(MI);
454 if (DomP.first) {
455 if (DomP.second)
456 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000457 else
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000458 visitHardInstr(MI, DomP.first);
459 }
460
461 // Process defs to track register ages, and kill values clobbered by generic
462 // instructions.
463 processDefs(MI, !DomP.first);
464}
465
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000466/// \brief Return true to if it makes sense to break dependence on a partial def
467/// or undef use.
468bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
469 unsigned Pref) {
470 int rx = regIndex(MI->getOperand(OpIdx).getReg());
471 if (rx < 0)
472 return false;
473
474 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
475 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
476
477 if (Pref > Clearance) {
478 DEBUG(dbgs() << ": Break dependency.\n");
479 return true;
480 }
481 // The current clearance seems OK, but we may be ignoring a def from a
482 // back-edge.
483 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
484 DEBUG(dbgs() << ": OK .\n");
485 return false;
486 }
487 // A def from an unprocessed back-edge may make us break this dependency.
488 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
489 return false;
490}
491
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000492// Update def-ages for registers defined by MI.
493// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000494//
495// Also break dependencies on partial defs and undef uses.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000496void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
497 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000498
499 // Break dependence on undef uses. Do this before updating LiveRegs below.
500 unsigned OpNum;
501 unsigned Pref = TII->getUndefRegClearance(MI, OpNum, TRI);
502 if (Pref) {
503 if (shouldBreakDependence(MI, OpNum, Pref))
504 UndefReads.push_back(std::make_pair(MI, OpNum));
505 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000506 const MCInstrDesc &MCID = MI->getDesc();
507 for (unsigned i = 0,
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000508 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000509 i != e; ++i) {
510 MachineOperand &MO = MI->getOperand(i);
511 if (!MO.isReg())
512 continue;
513 if (MO.isImplicit())
514 break;
515 if (MO.isUse())
516 continue;
517 int rx = regIndex(MO.getReg());
518 if (rx < 0)
519 continue;
520
521 // This instruction explicitly defines rx.
522 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
523 << '\t' << *MI);
524
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000525 // Check clearance before partial register updates.
526 // Call breakDependence before setting LiveRegs[rx].Def.
527 unsigned Pref = TII->getPartialRegUpdateClearance(MI, i, TRI);
528 if (Pref && shouldBreakDependence(MI, i, Pref))
529 TII->breakPartialRegDependency(MI, i, TRI);
530
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000531 // How many instructions since rx was last written?
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000532 LiveRegs[rx].Def = CurInstr;
533
534 // Kill off domains redefined by generic instructions.
535 if (Kill)
536 kill(rx);
537 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000538 ++CurInstr;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000539}
540
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000541/// \break Break false dependencies on undefined register reads.
542///
543/// Walk the block backward computing precise liveness. This is expensive, so we
544/// only do it on demand. Note that the occurrence of undefined register reads
545/// that should be broken is very rare, but when they occur we may have many in
546/// a single block.
547void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
548 if (UndefReads.empty())
549 return;
550
551 // Collect this block's live out register units.
Stephen Hines36b56882014-04-23 16:57:46 -0700552 LiveRegSet.init(TRI);
553 LiveRegSet.addLiveOuts(MBB);
554
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000555 MachineInstr *UndefMI = UndefReads.back().first;
556 unsigned OpIdx = UndefReads.back().second;
557
558 for (MachineBasicBlock::reverse_iterator I = MBB->rbegin(), E = MBB->rend();
559 I != E; ++I) {
Stephen Hines36b56882014-04-23 16:57:46 -0700560 // Update liveness, including the current instruction's defs.
561 LiveRegSet.stepBackward(*I);
Andrew Trick51dee242013-10-15 03:39:43 +0000562
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000563 if (UndefMI == &*I) {
Stephen Hines36b56882014-04-23 16:57:46 -0700564 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000565 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI);
566
567 UndefReads.pop_back();
568 if (UndefReads.empty())
569 return;
570
571 UndefMI = UndefReads.back().first;
572 OpIdx = UndefReads.back().second;
573 }
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000574 }
575}
576
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000577// A hard instruction only works in one domain. All input registers will be
578// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000579void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000580 // Collapse all uses.
581 for (unsigned i = mi->getDesc().getNumDefs(),
582 e = mi->getDesc().getNumOperands(); i != e; ++i) {
583 MachineOperand &mo = mi->getOperand(i);
584 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000585 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000586 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000587 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000588 }
589
590 // Kill all defs and force them.
591 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
592 MachineOperand &mo = mi->getOperand(i);
593 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000594 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000595 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000596 kill(rx);
597 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000598 }
599}
600
601// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000602void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000603 // Bitmask of available domains for this instruction after taking collapsed
604 // operands into account.
605 unsigned available = mask;
606
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000607 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000608 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000609 if (LiveRegs)
610 for (unsigned i = mi->getDesc().getNumDefs(),
611 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000612 MachineOperand &mo = mi->getOperand(i);
613 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000614 int rx = regIndex(mo.getReg());
Chris Lattner563d83f2010-03-31 20:32:51 +0000615 if (rx < 0) continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000616 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000617 // Bitmask of domains that dv and available have in common.
618 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000619 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000620 if (dv->isCollapsed()) {
621 // Restrict available domains to the ones in common with the operand.
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000622 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000623 // penalty for this operand.
624 if (common) available = common;
625 } else if (common)
626 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000627 used.push_back(rx);
628 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000629 // Open DomainValue is not compatible with instruction. It is useless
630 // now.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000631 kill(rx);
Chris Lattner563d83f2010-03-31 20:32:51 +0000632 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000633 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000634
635 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000636 if (isPowerOf2_32(available)) {
Michael J. Spencerc6af2432013-05-24 22:23:49 +0000637 unsigned domain = countTrailingZeros(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000638 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000639 visitHardInstr(mi, domain);
640 return;
641 }
642
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000643 // Kill off any remaining uses that don't match available, and build a list of
644 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000645 SmallVector<LiveReg, 4> Regs;
Craig Topperf22fd3f2013-07-03 05:11:49 +0000646 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000647 int rx = *i;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000648 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000649 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000650 if (!LR.Value->getCommonDomains(available)) {
651 kill(rx);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000652 continue;
653 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000654 // Sorted insertion.
655 bool Inserted = false;
Craig Topperf22fd3f2013-07-03 05:11:49 +0000656 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000657 i != e && !Inserted; ++i) {
658 if (LR.Def < i->Def) {
659 Inserted = true;
660 Regs.insert(i, LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000661 }
662 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000663 if (!Inserted)
664 Regs.push_back(LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000665 }
666
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000667 // doms are now sorted in order of appearance. Try to merge them all, giving
668 // priority to the latest ones.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700669 DomainValue *dv = nullptr;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000670 while (!Regs.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000671 if (!dv) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000672 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000673 // Force the first dv to match the current instruction.
674 dv->AvailableDomains = dv->getCommonDomains(available);
675 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner563d83f2010-03-31 20:32:51 +0000676 continue;
677 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000678
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000679 DomainValue *Latest = Regs.pop_back_val().Value;
680 // Skip already merged values.
681 if (Latest == dv || Latest->Next)
682 continue;
683 if (merge(dv, Latest))
684 continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000685
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000686 // If latest didn't merge, it is useless now. Kill all registers using it.
Craig Topperf22fd3f2013-07-03 05:11:49 +0000687 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000688 if (LiveRegs[*i].Value == Latest)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000689 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000690 }
691
692 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000693 if (!dv) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000694 dv = alloc();
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000695 dv->AvailableDomains = available;
696 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000697 dv->Instrs.push_back(mi);
698
Silviu Baranga541a8582012-10-03 08:29:36 +0000699 // Finally set all defs and non-collapsed uses to dv. We must iterate through
700 // all the operators, including imp-def ones.
701 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
702 ee = mi->operands_end();
703 ii != ee; ++ii) {
704 MachineOperand &mo = *ii;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000705 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000706 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000707 if (rx < 0) continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000708 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000709 kill(rx);
710 setLiveReg(rx, dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000711 }
712 }
713}
714
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000715bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000716 MF = &mf;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800717 TII = MF->getSubtarget().getInstrInfo();
718 TRI = MF->getSubtarget().getRegisterInfo();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700719 LiveRegs = nullptr;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000720 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000721
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000722 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
Stephen Hines37ed9c12014-12-01 14:51:49 -0800723 << TRI->getRegClassName(RC) << " **********\n");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000724
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000725 // If no relevant registers are used in the function, we can skip it
726 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000727 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000728 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
729 I != E; ++I)
Jakob Stoklund Olesen9aa6e0a2012-10-17 18:44:18 +0000730 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesena2a98fd2011-12-21 19:50:05 +0000731 anyregs = true;
732 break;
733 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000734 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000735
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000736 // Initialize the AliasMap on the first use.
737 if (AliasMap.empty()) {
738 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
739 // or -1.
740 AliasMap.resize(TRI->getNumRegs(), -1);
741 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000742 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
743 AI.isValid(); ++AI)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000744 AliasMap[*AI] = i;
745 }
746
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000747 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000748 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000749 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000750 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
751 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
752 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000753 enterBasicBlock(MBB);
754 if (SeenUnknownBackEdge)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000755 Loops.push_back(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000756 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000757 ++I)
758 visitInstr(I);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000759 processUndefReads(MBB);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000760 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000761 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000762
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000763 // Visit all the loop blocks again in order to merge DomainValues from
764 // back-edges.
765 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
766 MachineBasicBlock *MBB = Loops[i];
767 enterBasicBlock(MBB);
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000768 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
769 ++I)
770 if (!I->isDebugValue())
771 processDefs(I, false);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000772 processUndefReads(MBB);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000773 leaveBasicBlock(MBB);
774 }
775
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000776 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
777 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
778 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
779 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000780 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000781 continue;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000782 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000783 if (FI->second[i].Value)
784 release(FI->second[i].Value);
Jakob Stoklund Olesen0fdb05d2011-11-08 22:05:17 +0000785 delete[] FI->second;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000786 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000787 LiveOuts.clear();
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000788 UndefReads.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000789 Avail.clear();
790 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000791
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000792 return false;
793}
794
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000795FunctionPass *
796llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000797 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000798}