blob: b3a22c83a80867f20ffe3f9b9f72ad82c86ca3e5 [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 Hinesebe69fe2015-03-23 12:10:34 -070025#include "llvm/ADT/iterator_range.h"
Stephen Hines36b56882014-04-23 16:57:46 -070026#include "llvm/CodeGen/LivePhysRegs.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000029#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000032#include "llvm/Target/TargetInstrInfo.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080033#include "llvm/Target/TargetSubtargetInfo.h"
34
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000035using namespace llvm;
36
Stephen Hinesdce4a402014-05-29 02:49:00 -070037#define DEBUG_TYPE "execution-fix"
38
Chris Lattner563d83f2010-03-31 20:32:51 +000039/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000040/// of execution domains.
41///
42/// An open DomainValue represents a set of instructions that can still switch
43/// execution domain. Multiple registers may refer to the same open
44/// DomainValue - they will eventually be collapsed to the same execution
45/// domain.
46///
47/// A collapsed DomainValue represents a single register that has been forced
48/// into one of more execution domains. There is a separate collapsed
49/// DomainValue for each register, but it may contain multiple execution
50/// domains. A register value is initially created in a single execution
51/// domain, but if we were forced to pay the penalty of a domain crossing, we
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +000052/// keep track of the fact that the register is now available in multiple
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000053/// domains.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000054namespace {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000055struct DomainValue {
56 // Basic reference counting.
57 unsigned Refs;
58
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000059 // Bitmask of available domains. For an open DomainValue, it is the still
60 // possible domains for collapsing. For a collapsed DomainValue it is the
61 // domains where the register is available for free.
62 unsigned AvailableDomains;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000063
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +000064 // Pointer to the next DomainValue in a chain. When two DomainValues are
65 // merged, Victim.Next is set to point to Victor, so old DomainValue
Benjamin Kramerd9b0b022012-06-02 10:20:22 +000066 // references can be updated by following the chain.
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +000067 DomainValue *Next;
68
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000069 // Twiddleable instructions using or defining these registers.
70 SmallVector<MachineInstr*, 8> Instrs;
71
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000072 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000073 // track of the domains where the registers are already available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000074 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000075
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000076 // Is domain available?
77 bool hasDomain(unsigned domain) const {
Stephen Hinesebe69fe2015-03-23 12:10:34 -070078 assert(domain <
79 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
80 "undefined behavior");
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000081 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000082 }
83
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000084 // Mark domain as available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000085 void addDomain(unsigned domain) {
86 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000087 }
88
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000089 // Restrict to a single domain available.
90 void setSingleDomain(unsigned domain) {
91 AvailableDomains = 1u << domain;
92 }
93
94 // Return bitmask of domains that are available and in mask.
95 unsigned getCommonDomains(unsigned mask) const {
96 return AvailableDomains & mask;
97 }
98
99 // First domain available.
100 unsigned getFirstDomain() const {
Michael J. Spencerc6af2432013-05-24 22:23:49 +0000101 return countTrailingZeros(AvailableDomains);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000102 }
103
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000104 DomainValue() : Refs(0) { clear(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000105
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000106 // Clear this DomainValue and point to next which has all its data.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000107 void clear() {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000108 AvailableDomains = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700109 Next = nullptr;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000110 Instrs.clear();
111 }
112};
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000113}
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000114
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000115namespace {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000116/// LiveReg - Information about a live register.
117struct LiveReg {
118 /// Value currently in this register, or NULL when no value is being tracked.
119 /// This counts as a DomainValue reference.
120 DomainValue *Value;
121
122 /// Instruction that defined this register, relative to the beginning of the
123 /// current basic block. When a LiveReg is used to represent a live-out
124 /// register, this value is relative to the end of the basic block, so it
125 /// will be a negative number.
126 int Def;
127};
128} // anonynous namespace
129
130namespace {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000131class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000132 static char ID;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000133 SpecificBumpPtrAllocator<DomainValue> Allocator;
134 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000135
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000136 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000137 MachineFunction *MF;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000138 const TargetInstrInfo *TII;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000139 const TargetRegisterInfo *TRI;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700140 std::vector<SmallVector<int, 1>> AliasMap;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000141 const unsigned NumRegs;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000142 LiveReg *LiveRegs;
143 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000144 LiveOutMap LiveOuts;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000145
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000146 /// List of undefined register reads in this block in forward order.
147 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads;
148
149 /// Storage for register unit liveness.
Stephen Hines36b56882014-04-23 16:57:46 -0700150 LivePhysRegs LiveRegSet;
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000151
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000152 /// Current instruction number.
153 /// The first instruction in each basic block is 0.
154 int CurInstr;
155
156 /// True when the current block has a predecessor that hasn't been visited
157 /// yet.
158 bool SeenUnknownBackEdge;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000159
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000160public:
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000161 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000162 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000163
Stephen Hines36b56882014-04-23 16:57:46 -0700164 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000165 AU.setPreservesAll();
166 MachineFunctionPass::getAnalysisUsage(AU);
167 }
168
Stephen Hines36b56882014-04-23 16:57:46 -0700169 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000170
Stephen Hines36b56882014-04-23 16:57:46 -0700171 const char *getPassName() const override {
Jakob Stoklund Olesencd7dcad2011-11-07 21:23:39 +0000172 return "Execution dependency fix";
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000173 }
174
175private:
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700176 iterator_range<SmallVectorImpl<int>::const_iterator>
177 regIndizes(unsigned Reg) const;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000178
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000179 // DomainValue allocation.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000180 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000181 DomainValue *retain(DomainValue *DV) {
182 if (DV) ++DV->Refs;
183 return DV;
184 }
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000185 void release(DomainValue*);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000186 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000187
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000188 // LiveRegs manipulations.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000189 void setLiveReg(int rx, DomainValue *DV);
190 void kill(int rx);
191 void force(int rx, unsigned domain);
192 void collapse(DomainValue *dv, unsigned domain);
193 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000194
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000195 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000196 void leaveBasicBlock(MachineBasicBlock*);
197 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000198 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000199 void visitSoftInstr(MachineInstr*, unsigned mask);
200 void visitHardInstr(MachineInstr*, unsigned domain);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000201 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
202 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000203};
204}
205
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000206char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000207
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700208/// Translate TRI register number to a list of indizes into our stmaller tables
209/// of interesting registers.
210iterator_range<SmallVectorImpl<int>::const_iterator>
211ExeDepsFix::regIndizes(unsigned Reg) const {
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000212 assert(Reg < AliasMap.size() && "Invalid register");
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700213 const auto &Entry = AliasMap[Reg];
214 return make_range(Entry.begin(), Entry.end());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000215}
216
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000217DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000218 DomainValue *dv = Avail.empty() ?
219 new(Allocator.Allocate()) DomainValue :
220 Avail.pop_back_val();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000221 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000222 dv->addDomain(domain);
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000223 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000224 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000225 return dv;
226}
227
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000228/// release - Release a reference to DV. When the last reference is released,
229/// collapse if needed.
230void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000231 while (DV) {
232 assert(DV->Refs && "Bad DomainValue");
233 if (--DV->Refs)
234 return;
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000235
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000236 // There are no more DV references. Collapse any contained instructions.
237 if (DV->AvailableDomains && !DV->isCollapsed())
238 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000239
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000240 DomainValue *Next = DV->Next;
241 DV->clear();
242 Avail.push_back(DV);
243 // Also release the next DomainValue in the chain.
244 DV = Next;
245 }
246}
247
248/// resolve - Follow the chain of dead DomainValues until a live DomainValue is
249/// reached. Update the referenced pointer when necessary.
250DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
251 DomainValue *DV = DVRef;
252 if (!DV || !DV->Next)
253 return DV;
254
255 // DV has a chain. Find the end.
256 do DV = DV->Next;
257 while (DV->Next);
258
259 // Update DVRef to point to DV.
260 retain(DV);
261 release(DVRef);
262 DVRef = DV;
263 return DV;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000264}
265
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000266/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000267void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000268 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000269 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000270
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000271 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000272 return;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000273 if (LiveRegs[rx].Value)
274 release(LiveRegs[rx].Value);
275 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000276}
277
278// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000279void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000280 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000281 assert(LiveRegs && "Must enter basic block first.");
282 if (!LiveRegs[rx].Value)
283 return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000284
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000285 release(LiveRegs[rx].Value);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700286 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000287}
288
289/// Force register rx into domain.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000290void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000291 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000292 assert(LiveRegs && "Must enter basic block first.");
293 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000294 if (dv->isCollapsed())
295 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000296 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000297 collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000298 else {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000299 // This is an incompatible open DomainValue. Collapse it to whatever and
300 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000301 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000302 assert(LiveRegs[rx].Value && "Not live after collapse?");
303 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000304 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000305 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000306 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000307 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000308 }
309}
310
311/// Collapse open DomainValue into given domain. If there are multiple
312/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000313void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000314 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000315
316 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000317 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000318 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000319 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000320
321 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000322 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000323 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000324 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000325 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000326}
327
328/// Merge - All instructions and registers in B are moved to A, and B is
329/// released.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000330bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000331 assert(!A->isCollapsed() && "Cannot merge into collapsed");
332 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000333 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000334 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000335 // Restrict to the domains that A and B have in common.
336 unsigned common = A->getCommonDomains(B->AvailableDomains);
337 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000338 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000339 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000340 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000341
342 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000343 B->clear();
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000344 // All uses of B are referred to A.
345 B->Next = retain(A);
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000346
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700347 for (unsigned rx = 0; rx != NumRegs; ++rx) {
348 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000349 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000350 setLiveReg(rx, A);
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700351 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000352 return true;
353}
354
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000355// enterBasicBlock - Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000356void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000357 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000358 SeenUnknownBackEdge = false;
359
360 // Reset instruction counter in each basic block.
361 CurInstr = 0;
362
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000363 // Set up UndefReads to track undefined register reads.
364 UndefReads.clear();
Stephen Hines36b56882014-04-23 16:57:46 -0700365 LiveRegSet.clear();
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000366
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000367 // Set up LiveRegs to represent registers entering MBB.
368 if (!LiveRegs)
369 LiveRegs = new LiveReg[NumRegs];
370
371 // Default values are 'nothing happened a long time ago'.
372 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700373 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000374 LiveRegs[rx].Def = -(1 << 20);
375 }
376
377 // This is the entry block.
378 if (MBB->pred_empty()) {
379 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
380 e = MBB->livein_end(); i != e; ++i) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700381 for (int rx : regIndizes(*i)) {
382 // Treat function live-ins as if they were defined just before the first
383 // instruction. Usually, function arguments are set up immediately
384 // before the call.
385 LiveRegs[rx].Def = -1;
386 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000387 }
388 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
389 return;
390 }
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000391
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000392 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000393 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
394 pe = MBB->pred_end(); pi != pe; ++pi) {
395 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
396 if (fi == LiveOuts.end()) {
397 SeenUnknownBackEdge = true;
398 continue;
399 }
400 assert(fi->second && "Can't have NULL entries");
401
402 for (unsigned rx = 0; rx != NumRegs; ++rx) {
403 // Use the most recent predecessor def for each register.
404 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
405
406 DomainValue *pdv = resolve(fi->second[rx].Value);
407 if (!pdv)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000408 continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000409 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000410 setLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000411 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000412 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000413
414 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000415 if (LiveRegs[rx].Value->isCollapsed()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700416 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000417 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
418 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
419 collapse(pdv, Domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000420 continue;
421 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000422
Chris Lattner563d83f2010-03-31 20:32:51 +0000423 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000424 if (!pdv->isCollapsed())
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000425 merge(LiveRegs[rx].Value, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000426 else
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000427 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000428 }
429 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000430 DEBUG(dbgs() << "BB#" << MBB->getNumber()
431 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000432}
433
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000434void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000435 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000436 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000437 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000438 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
439
440 if (First) {
441 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
442 // the end of this block instead of the beginning.
443 for (unsigned i = 0, e = NumRegs; i != e; ++i)
444 LiveRegs[i].Def -= CurInstr;
445 } else {
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000446 // Insertion failed, this must be the second pass.
447 // Release all the DomainValues instead of keeping them.
448 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000449 release(LiveRegs[i].Value);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000450 delete[] LiveRegs;
451 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700452 LiveRegs = nullptr;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000453}
454
455void ExeDepsFix::visitInstr(MachineInstr *MI) {
456 if (MI->isDebugValue())
457 return;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000458
459 // Update instructions with explicit execution domains.
460 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(MI);
461 if (DomP.first) {
462 if (DomP.second)
463 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000464 else
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000465 visitHardInstr(MI, DomP.first);
466 }
467
468 // Process defs to track register ages, and kill values clobbered by generic
469 // instructions.
470 processDefs(MI, !DomP.first);
471}
472
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000473/// \brief Return true to if it makes sense to break dependence on a partial def
474/// or undef use.
475bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
476 unsigned Pref) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700477 unsigned reg = MI->getOperand(OpIdx).getReg();
478 for (int rx : regIndizes(reg)) {
479 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
480 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000481
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700482 if (Pref > Clearance) {
483 DEBUG(dbgs() << ": Break dependency.\n");
484 continue;
485 }
486 // The current clearance seems OK, but we may be ignoring a def from a
487 // back-edge.
488 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
489 DEBUG(dbgs() << ": OK .\n");
490 return false;
491 }
492 // A def from an unprocessed back-edge may make us break this dependency.
493 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000494 return false;
495 }
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700496 return true;
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000497}
498
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000499// Update def-ages for registers defined by MI.
500// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000501//
502// Also break dependencies on partial defs and undef uses.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000503void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
504 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000505
506 // Break dependence on undef uses. Do this before updating LiveRegs below.
507 unsigned OpNum;
508 unsigned Pref = TII->getUndefRegClearance(MI, OpNum, TRI);
509 if (Pref) {
510 if (shouldBreakDependence(MI, OpNum, Pref))
511 UndefReads.push_back(std::make_pair(MI, OpNum));
512 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000513 const MCInstrDesc &MCID = MI->getDesc();
514 for (unsigned i = 0,
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000515 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000516 i != e; ++i) {
517 MachineOperand &MO = MI->getOperand(i);
518 if (!MO.isReg())
519 continue;
520 if (MO.isImplicit())
521 break;
522 if (MO.isUse())
523 continue;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700524 for (int rx : regIndizes(MO.getReg())) {
525 // This instruction explicitly defines rx.
526 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
527 << '\t' << *MI);
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000528
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700529 // Check clearance before partial register updates.
530 // Call breakDependence before setting LiveRegs[rx].Def.
531 unsigned Pref = TII->getPartialRegUpdateClearance(MI, i, TRI);
532 if (Pref && shouldBreakDependence(MI, i, Pref))
533 TII->breakPartialRegDependency(MI, i, TRI);
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000534
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700535 // How many instructions since rx was last written?
536 LiveRegs[rx].Def = CurInstr;
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000537
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700538 // Kill off domains redefined by generic instructions.
539 if (Kill)
540 kill(rx);
541 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000542 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000543 ++CurInstr;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000544}
545
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000546/// \break Break false dependencies on undefined register reads.
547///
548/// Walk the block backward computing precise liveness. This is expensive, so we
549/// only do it on demand. Note that the occurrence of undefined register reads
550/// that should be broken is very rare, but when they occur we may have many in
551/// a single block.
552void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
553 if (UndefReads.empty())
554 return;
555
556 // Collect this block's live out register units.
Stephen Hines36b56882014-04-23 16:57:46 -0700557 LiveRegSet.init(TRI);
558 LiveRegSet.addLiveOuts(MBB);
559
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000560 MachineInstr *UndefMI = UndefReads.back().first;
561 unsigned OpIdx = UndefReads.back().second;
562
563 for (MachineBasicBlock::reverse_iterator I = MBB->rbegin(), E = MBB->rend();
564 I != E; ++I) {
Stephen Hines36b56882014-04-23 16:57:46 -0700565 // Update liveness, including the current instruction's defs.
566 LiveRegSet.stepBackward(*I);
Andrew Trick51dee242013-10-15 03:39:43 +0000567
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000568 if (UndefMI == &*I) {
Stephen Hines36b56882014-04-23 16:57:46 -0700569 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000570 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI);
571
572 UndefReads.pop_back();
573 if (UndefReads.empty())
574 return;
575
576 UndefMI = UndefReads.back().first;
577 OpIdx = UndefReads.back().second;
578 }
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000579 }
580}
581
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000582// A hard instruction only works in one domain. All input registers will be
583// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000584void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000585 // Collapse all uses.
586 for (unsigned i = mi->getDesc().getNumDefs(),
587 e = mi->getDesc().getNumOperands(); i != e; ++i) {
588 MachineOperand &mo = mi->getOperand(i);
589 if (!mo.isReg()) continue;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700590 for (int rx : regIndizes(mo.getReg())) {
591 force(rx, domain);
592 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000593 }
594
595 // Kill all defs and force them.
596 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
597 MachineOperand &mo = mi->getOperand(i);
598 if (!mo.isReg()) continue;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700599 for (int rx : regIndizes(mo.getReg())) {
600 kill(rx);
601 force(rx, domain);
602 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000603 }
604}
605
606// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000607void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000608 // Bitmask of available domains for this instruction after taking collapsed
609 // operands into account.
610 unsigned available = mask;
611
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000612 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000613 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000614 if (LiveRegs)
615 for (unsigned i = mi->getDesc().getNumDefs(),
616 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000617 MachineOperand &mo = mi->getOperand(i);
618 if (!mo.isReg()) continue;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700619 for (int rx : regIndizes(mo.getReg())) {
620 DomainValue *dv = LiveRegs[rx].Value;
621 if (dv == nullptr)
622 continue;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000623 // Bitmask of domains that dv and available have in common.
624 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000625 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000626 if (dv->isCollapsed()) {
627 // Restrict available domains to the ones in common with the operand.
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000628 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000629 // penalty for this operand.
630 if (common) available = common;
631 } else if (common)
632 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000633 used.push_back(rx);
634 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000635 // Open DomainValue is not compatible with instruction. It is useless
636 // now.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000637 kill(rx);
Chris Lattner563d83f2010-03-31 20:32:51 +0000638 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000639 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000640
641 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000642 if (isPowerOf2_32(available)) {
Michael J. Spencerc6af2432013-05-24 22:23:49 +0000643 unsigned domain = countTrailingZeros(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000644 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000645 visitHardInstr(mi, domain);
646 return;
647 }
648
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000649 // Kill off any remaining uses that don't match available, and build a list of
650 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000651 SmallVector<LiveReg, 4> Regs;
Craig Topperf22fd3f2013-07-03 05:11:49 +0000652 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000653 int rx = *i;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700654 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000655 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000656 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000657 if (!LR.Value->getCommonDomains(available)) {
658 kill(rx);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000659 continue;
660 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000661 // Sorted insertion.
662 bool Inserted = false;
Craig Topperf22fd3f2013-07-03 05:11:49 +0000663 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000664 i != e && !Inserted; ++i) {
665 if (LR.Def < i->Def) {
666 Inserted = true;
667 Regs.insert(i, LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000668 }
669 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000670 if (!Inserted)
671 Regs.push_back(LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000672 }
673
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000674 // doms are now sorted in order of appearance. Try to merge them all, giving
675 // priority to the latest ones.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700676 DomainValue *dv = nullptr;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000677 while (!Regs.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000678 if (!dv) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000679 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000680 // Force the first dv to match the current instruction.
681 dv->AvailableDomains = dv->getCommonDomains(available);
682 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner563d83f2010-03-31 20:32:51 +0000683 continue;
684 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000685
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000686 DomainValue *Latest = Regs.pop_back_val().Value;
687 // Skip already merged values.
688 if (Latest == dv || Latest->Next)
689 continue;
690 if (merge(dv, Latest))
691 continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000692
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000693 // If latest didn't merge, it is useless now. Kill all registers using it.
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700694 for (int i : used) {
695 assert(LiveRegs && "no space allocated for live registers");
696 if (LiveRegs[i].Value == Latest)
697 kill(i);
698 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000699 }
700
701 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000702 if (!dv) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000703 dv = alloc();
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000704 dv->AvailableDomains = available;
705 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000706 dv->Instrs.push_back(mi);
707
Silviu Baranga541a8582012-10-03 08:29:36 +0000708 // Finally set all defs and non-collapsed uses to dv. We must iterate through
709 // all the operators, including imp-def ones.
710 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
711 ee = mi->operands_end();
712 ii != ee; ++ii) {
713 MachineOperand &mo = *ii;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000714 if (!mo.isReg()) continue;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700715 for (int rx : regIndizes(mo.getReg())) {
716 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
717 kill(rx);
718 setLiveReg(rx, dv);
719 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000720 }
721 }
722}
723
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000724bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000725 MF = &mf;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800726 TII = MF->getSubtarget().getInstrInfo();
727 TRI = MF->getSubtarget().getRegisterInfo();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700728 LiveRegs = nullptr;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000729 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000730
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000731 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
Stephen Hines37ed9c12014-12-01 14:51:49 -0800732 << TRI->getRegClassName(RC) << " **********\n");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000733
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000734 // If no relevant registers are used in the function, we can skip it
735 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000736 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000737 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
738 I != E; ++I)
Jakob Stoklund Olesen9aa6e0a2012-10-17 18:44:18 +0000739 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesena2a98fd2011-12-21 19:50:05 +0000740 anyregs = true;
741 break;
742 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000743 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000744
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000745 // Initialize the AliasMap on the first use.
746 if (AliasMap.empty()) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700747 // Given a PhysReg, AliasMap[PhysReg] returns a list of indices into RC and
748 // therefore the LiveRegs array.
749 AliasMap.resize(TRI->getNumRegs());
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000750 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000751 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
752 AI.isValid(); ++AI)
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700753 AliasMap[*AI].push_back(i);
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000754 }
755
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000756 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000757 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000758 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000759 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
760 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
761 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000762 enterBasicBlock(MBB);
763 if (SeenUnknownBackEdge)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000764 Loops.push_back(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000765 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000766 ++I)
767 visitInstr(I);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000768 processUndefReads(MBB);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000769 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000770 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000771
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000772 // Visit all the loop blocks again in order to merge DomainValues from
773 // back-edges.
774 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
775 MachineBasicBlock *MBB = Loops[i];
776 enterBasicBlock(MBB);
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000777 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
778 ++I)
779 if (!I->isDebugValue())
780 processDefs(I, false);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000781 processUndefReads(MBB);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000782 leaveBasicBlock(MBB);
783 }
784
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000785 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
786 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
787 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
788 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000789 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000790 continue;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000791 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000792 if (FI->second[i].Value)
793 release(FI->second[i].Value);
Jakob Stoklund Olesen0fdb05d2011-11-08 22:05:17 +0000794 delete[] FI->second;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000795 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000796 LiveOuts.clear();
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000797 UndefReads.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000798 Avail.clear();
799 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000800
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000801 return false;
802}
803
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000804FunctionPass *
805llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000806 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000807}