blob: deacb233f171e9050980f29a38b7ef482dcd2dad [file] [log] [blame]
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +00001//===- ExecutionDepsFix.cpp - Fix execution dependecy issues ----*- C++ -*-===//
Jakob Stoklund Olesen49e121d2010-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 Olesenbd5109f2011-09-28 00:01:56 +000010// This file contains the execution dependency fix pass.
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000011//
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +000012// Some X86 SSE instructions like mov, and, or, xor are available in different
Jakob Stoklund Olesen49e121d2010-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 Olesenbd5109f2011-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 Olesen49e121d2010-03-25 17:25:00 +000018//
19// This pass changes the variant instructions to minimize domain crossings.
20//
21//===----------------------------------------------------------------------===//
22
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/CodeGen/Passes.h"
24#include "llvm/ADT/PostOrderIterator.h"
Matthias Braun8142efa2014-12-17 19:13:47 +000025#include "llvm/ADT/iterator_range.h"
Juergen Ributzka310034e2013-12-14 06:52:56 +000026#include "llvm/CodeGen/LivePhysRegs.h"
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000029#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Target/TargetInstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000033#include "llvm/Target/TargetSubtargetInfo.h"
34
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000035using namespace llvm;
36
Chandler Carruth1b9dde02014-04-22 02:02:50 +000037#define DEBUG_TYPE "execution-fix"
38
Chris Lattner503a0ef2010-03-31 20:32:51 +000039/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesenb551aa42010-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 Olesen543bef62011-11-15 01:15:25 +000052/// keep track of the fact that the register is now available in multiple
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000053/// domains.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000054namespace {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000055struct DomainValue {
56 // Basic reference counting.
57 unsigned Refs;
58
Jakob Stoklund Olesend03ac952010-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 Olesenb551aa42010-03-29 23:24:21 +000063
Jakob Stoklund Olesen53ec9772011-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 Kramerbde91762012-06-02 10:20:22 +000066 // references can be updated by following the chain.
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000067 DomainValue *Next;
68
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000069 // Twiddleable instructions using or defining these registers.
70 SmallVector<MachineInstr*, 8> Instrs;
71
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000072 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000073 // track of the domains where the registers are already available.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000074 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000075
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000076 // Is domain available?
77 bool hasDomain(unsigned domain) const {
Aaron Ballman0d6a0102014-12-16 14:04:11 +000078 assert(domain <
79 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +000080 "undefined behavior");
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000081 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000082 }
83
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000084 // Mark domain as available.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000085 void addDomain(unsigned domain) {
86 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000087 }
88
Jakob Stoklund Olesend03ac952010-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. Spencerdf1ecbd72013-05-24 22:23:49 +0000101 return countTrailingZeros(AvailableDomains);
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000102 }
103
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000104 DomainValue() : Refs(0) { clear(); }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000105
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000106 // Clear this DomainValue and point to next which has all its data.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000107 void clear() {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000108 AvailableDomains = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000109 Next = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000110 Instrs.clear();
111 }
112};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000113}
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000114
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000115namespace {
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000116/// Information about a live register.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000117struct 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};
Sanjay Patel12fa37f2015-03-15 18:11:35 +0000128} // anonymous namespace
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000129
130namespace {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000131class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000132 static char ID;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000133 SpecificBumpPtrAllocator<DomainValue> Allocator;
134 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000135
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000136 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000137 MachineFunction *MF;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000138 const TargetInstrInfo *TII;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000139 const TargetRegisterInfo *TRI;
Matthias Braun8142efa2014-12-17 19:13:47 +0000140 std::vector<SmallVector<int, 1>> AliasMap;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000141 const unsigned NumRegs;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000142 LiveReg *LiveRegs;
143 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000144 LiveOutMap LiveOuts;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000145
Andrew Trickb6d56be2013-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.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000150 LivePhysRegs LiveRegSet;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000151
Jakob Stoklund Olesen543bef62011-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 Olesenb551aa42010-03-29 23:24:21 +0000159
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000160public:
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000161 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000162 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000163
Craig Topper4584cd52014-03-07 09:26:03 +0000164 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000165 AU.setPreservesAll();
166 MachineFunctionPass::getAnalysisUsage(AU);
167 }
168
Craig Topper4584cd52014-03-07 09:26:03 +0000169 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000170
Craig Topper4584cd52014-03-07 09:26:03 +0000171 const char *getPassName() const override {
Jakob Stoklund Olesenbaffa7d2011-11-07 21:23:39 +0000172 return "Execution dependency fix";
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000173 }
174
175private:
Matthias Braun8142efa2014-12-17 19:13:47 +0000176 iterator_range<SmallVectorImpl<int>::const_iterator>
Matthias Braun046318b2015-03-06 18:56:20 +0000177 regIndices(unsigned Reg) const;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000178
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000179 // DomainValue allocation.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000180 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000181 DomainValue *retain(DomainValue *DV) {
182 if (DV) ++DV->Refs;
183 return DV;
184 }
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000185 void release(DomainValue*);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000186 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000187
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000188 // LiveRegs manipulations.
Jakob Stoklund Olesen9e338bb2011-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 Olesenb551aa42010-03-29 23:24:21 +0000194
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000195 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000196 void leaveBasicBlock(MachineBasicBlock*);
197 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000198 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000199 void visitSoftInstr(MachineInstr*, unsigned mask);
200 void visitHardInstr(MachineInstr*, unsigned domain);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000201 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
202 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000203};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000204}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000205
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000206char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000207
Matthias Braun046318b2015-03-06 18:56:20 +0000208/// Translate TRI register number to a list of indices into our smaller tables
Matthias Braun8142efa2014-12-17 19:13:47 +0000209/// of interesting registers.
210iterator_range<SmallVectorImpl<int>::const_iterator>
Matthias Braun046318b2015-03-06 18:56:20 +0000211ExeDepsFix::regIndices(unsigned Reg) const {
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000212 assert(Reg < AliasMap.size() && "Invalid register");
Matthias Braun8142efa2014-12-17 19:13:47 +0000213 const auto &Entry = AliasMap[Reg];
214 return make_range(Entry.begin(), Entry.end());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000215}
216
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000217DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000218 DomainValue *dv = Avail.empty() ?
219 new(Allocator.Allocate()) DomainValue :
220 Avail.pop_back_val();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000221 if (domain >= 0)
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000222 dv->addDomain(domain);
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000223 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000224 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000225 return dv;
226}
227
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000228/// Release a reference to DV. When the last reference is released,
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000229/// collapse if needed.
230void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000231 while (DV) {
232 assert(DV->Refs && "Bad DomainValue");
233 if (--DV->Refs)
234 return;
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000235
Jakob Stoklund Olesen53ec9772011-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 Olesen1438e192011-11-08 21:57:44 +0000239
Jakob Stoklund Olesen53ec9772011-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
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000248/// Follow the chain of dead DomainValues until a live DomainValue is reached.
249/// Update the referenced pointer when necessary.
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000250DomainValue *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 Olesen42caaa42010-04-04 18:00:21 +0000264}
265
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000266/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000267void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000268 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000269 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000270
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000271 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000272 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000273 if (LiveRegs[rx].Value)
274 release(LiveRegs[rx].Value);
275 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000276}
277
278// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000279void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000280 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000281 assert(LiveRegs && "Must enter basic block first.");
282 if (!LiveRegs[rx].Value)
283 return;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000284
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000285 release(LiveRegs[rx].Value);
Craig Topperc0196b12014-04-14 00:51:57 +0000286 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000287}
288
289/// Force register rx into domain.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000290void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000291 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000292 assert(LiveRegs && "Must enter basic block first.");
293 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000294 if (dv->isCollapsed())
295 dv->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000296 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000297 collapse(dv, domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000298 else {
Jakob Stoklund Olesenbd5109f2011-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 Olesen9e338bb2011-11-08 21:57:47 +0000301 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000302 assert(LiveRegs[rx].Value && "Not live after collapse?");
303 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000304 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000305 } else {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000306 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000307 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-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 Olesen9e338bb2011-11-08 21:57:47 +0000313void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000314 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000315
316 // Collapse all the instructions.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000317 while (!dv->Instrs.empty())
Jakob Stoklund Olesenb48c9942011-09-27 22:57:18 +0000318 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000319 dv->setSingleDomain(domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000320
321 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000322 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000323 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000324 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000325 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000326}
327
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000328/// All instructions and registers in B are moved to A, and B is released.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000329bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000330 assert(!A->isCollapsed() && "Cannot merge into collapsed");
331 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen58ca0a62010-03-31 20:05:12 +0000332 if (A == B)
Jakob Stoklund Olesen4cd58662010-03-31 17:13:16 +0000333 return true;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000334 // Restrict to the domains that A and B have in common.
335 unsigned common = A->getCommonDomains(B->AvailableDomains);
336 if (!common)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000337 return false;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000338 A->AvailableDomains = common;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000339 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000340
341 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000342 B->clear();
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000343 // All uses of B are referred to A.
344 B->Next = retain(A);
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000345
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000346 for (unsigned rx = 0; rx != NumRegs; ++rx) {
347 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000348 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000349 setLiveReg(rx, A);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000350 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000351 return true;
352}
353
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000354/// Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000355void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000356 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000357 SeenUnknownBackEdge = false;
358
359 // Reset instruction counter in each basic block.
360 CurInstr = 0;
361
Andrew Trickb6d56be2013-10-14 22:19:03 +0000362 // Set up UndefReads to track undefined register reads.
363 UndefReads.clear();
Juergen Ributzka310034e2013-12-14 06:52:56 +0000364 LiveRegSet.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000365
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000366 // Set up LiveRegs to represent registers entering MBB.
367 if (!LiveRegs)
368 LiveRegs = new LiveReg[NumRegs];
369
370 // Default values are 'nothing happened a long time ago'.
371 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Craig Topperc0196b12014-04-14 00:51:57 +0000372 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000373 LiveRegs[rx].Def = -(1 << 20);
374 }
375
376 // This is the entry block.
377 if (MBB->pred_empty()) {
Matthias Braund9da1622015-09-09 18:08:03 +0000378 for (const auto &LI : MBB->liveins()) {
379 for (int rx : regIndices(LI.PhysReg)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000380 // Treat function live-ins as if they were defined just before the first
381 // instruction. Usually, function arguments are set up immediately
382 // before the call.
383 LiveRegs[rx].Def = -1;
384 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000385 }
386 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
387 return;
388 }
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000389
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000390 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000391 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
392 pe = MBB->pred_end(); pi != pe; ++pi) {
393 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
394 if (fi == LiveOuts.end()) {
395 SeenUnknownBackEdge = true;
396 continue;
397 }
398 assert(fi->second && "Can't have NULL entries");
399
400 for (unsigned rx = 0; rx != NumRegs; ++rx) {
401 // Use the most recent predecessor def for each register.
402 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
403
404 DomainValue *pdv = resolve(fi->second[rx].Value);
405 if (!pdv)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000406 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000407 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000408 setLiveReg(rx, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000409 continue;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000410 }
Chris Lattner503a0ef2010-03-31 20:32:51 +0000411
412 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000413 if (LiveRegs[rx].Value->isCollapsed()) {
Eric Christopher650c8f22014-05-20 17:11:11 +0000414 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000415 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
416 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
417 collapse(pdv, Domain);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000418 continue;
419 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000420
Chris Lattner503a0ef2010-03-31 20:32:51 +0000421 // Currently open, merge in predecessor.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000422 if (!pdv->isCollapsed())
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000423 merge(LiveRegs[rx].Value, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000424 else
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000425 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000426 }
427 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000428 DEBUG(dbgs() << "BB#" << MBB->getNumber()
429 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000430}
431
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000432void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000433 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000434 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000435 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000436 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
437
438 if (First) {
439 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
440 // the end of this block instead of the beginning.
441 for (unsigned i = 0, e = NumRegs; i != e; ++i)
442 LiveRegs[i].Def -= CurInstr;
443 } else {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000444 // Insertion failed, this must be the second pass.
445 // Release all the DomainValues instead of keeping them.
446 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000447 release(LiveRegs[i].Value);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000448 delete[] LiveRegs;
449 }
Craig Topperc0196b12014-04-14 00:51:57 +0000450 LiveRegs = nullptr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000451}
452
453void ExeDepsFix::visitInstr(MachineInstr *MI) {
454 if (MI->isDebugValue())
455 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000456
457 // Update instructions with explicit execution domains.
458 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(MI);
459 if (DomP.first) {
460 if (DomP.second)
461 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000462 else
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000463 visitHardInstr(MI, DomP.first);
464 }
465
466 // Process defs to track register ages, and kill values clobbered by generic
467 // instructions.
468 processDefs(MI, !DomP.first);
469}
470
Andrew Trickb6d56be2013-10-14 22:19:03 +0000471/// \brief Return true to if it makes sense to break dependence on a partial def
472/// or undef use.
473bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
474 unsigned Pref) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000475 unsigned reg = MI->getOperand(OpIdx).getReg();
Matthias Braun046318b2015-03-06 18:56:20 +0000476 for (int rx : regIndices(reg)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000477 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
478 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000479
Matthias Braun8142efa2014-12-17 19:13:47 +0000480 if (Pref > Clearance) {
481 DEBUG(dbgs() << ": Break dependency.\n");
482 continue;
483 }
484 // The current clearance seems OK, but we may be ignoring a def from a
485 // back-edge.
486 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
487 DEBUG(dbgs() << ": OK .\n");
488 return false;
489 }
490 // A def from an unprocessed back-edge may make us break this dependency.
491 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000492 return false;
493 }
Matthias Braun8142efa2014-12-17 19:13:47 +0000494 return true;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000495}
496
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000497// Update def-ages for registers defined by MI.
498// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000499//
500// Also break dependencies on partial defs and undef uses.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000501void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
502 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000503
504 // Break dependence on undef uses. Do this before updating LiveRegs below.
505 unsigned OpNum;
506 unsigned Pref = TII->getUndefRegClearance(MI, OpNum, TRI);
507 if (Pref) {
508 if (shouldBreakDependence(MI, OpNum, Pref))
509 UndefReads.push_back(std::make_pair(MI, OpNum));
510 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000511 const MCInstrDesc &MCID = MI->getDesc();
512 for (unsigned i = 0,
Evan Cheng7f8e5632011-12-07 07:15:52 +0000513 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000514 i != e; ++i) {
515 MachineOperand &MO = MI->getOperand(i);
516 if (!MO.isReg())
517 continue;
518 if (MO.isImplicit())
519 break;
520 if (MO.isUse())
521 continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000522 for (int rx : regIndices(MO.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000523 // This instruction explicitly defines rx.
524 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
525 << '\t' << *MI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000526
Matthias Braun8142efa2014-12-17 19:13:47 +0000527 // Check clearance before partial register updates.
528 // Call breakDependence before setting LiveRegs[rx].Def.
529 unsigned Pref = TII->getPartialRegUpdateClearance(MI, i, TRI);
530 if (Pref && shouldBreakDependence(MI, i, Pref))
531 TII->breakPartialRegDependency(MI, i, TRI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000532
Matthias Braun8142efa2014-12-17 19:13:47 +0000533 // How many instructions since rx was last written?
534 LiveRegs[rx].Def = CurInstr;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000535
Matthias Braun8142efa2014-12-17 19:13:47 +0000536 // Kill off domains redefined by generic instructions.
537 if (Kill)
538 kill(rx);
539 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000540 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000541 ++CurInstr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000542}
543
Andrew Trickb6d56be2013-10-14 22:19:03 +0000544/// \break Break false dependencies on undefined register reads.
545///
546/// Walk the block backward computing precise liveness. This is expensive, so we
547/// only do it on demand. Note that the occurrence of undefined register reads
548/// that should be broken is very rare, but when they occur we may have many in
549/// a single block.
550void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
551 if (UndefReads.empty())
552 return;
553
554 // Collect this block's live out register units.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000555 LiveRegSet.init(TRI);
556 LiveRegSet.addLiveOuts(MBB);
557
Andrew Trickb6d56be2013-10-14 22:19:03 +0000558 MachineInstr *UndefMI = UndefReads.back().first;
559 unsigned OpIdx = UndefReads.back().second;
560
Pete Cooper7679afd2015-07-24 21:13:43 +0000561 for (MachineInstr &I : make_range(MBB->rbegin(), MBB->rend())) {
Andrew Trick60cf0ad2013-12-13 22:23:54 +0000562 // Update liveness, including the current instruction's defs.
Pete Cooper7679afd2015-07-24 21:13:43 +0000563 LiveRegSet.stepBackward(I);
Andrew Trick3a996932013-10-15 03:39:43 +0000564
Pete Cooper7679afd2015-07-24 21:13:43 +0000565 if (UndefMI == &I) {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000566 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Andrew Trickb6d56be2013-10-14 22:19:03 +0000567 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI);
568
569 UndefReads.pop_back();
570 if (UndefReads.empty())
571 return;
572
573 UndefMI = UndefReads.back().first;
574 OpIdx = UndefReads.back().second;
575 }
Andrew Trickb6d56be2013-10-14 22:19:03 +0000576 }
577}
578
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000579// A hard instruction only works in one domain. All input registers will be
580// forced into that domain.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000581void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000582 // Collapse all uses.
583 for (unsigned i = mi->getDesc().getNumDefs(),
584 e = mi->getDesc().getNumOperands(); i != e; ++i) {
585 MachineOperand &mo = mi->getOperand(i);
586 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000587 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000588 force(rx, domain);
589 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000590 }
591
592 // Kill all defs and force them.
593 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
594 MachineOperand &mo = mi->getOperand(i);
595 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000596 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000597 kill(rx);
598 force(rx, domain);
599 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000600 }
601}
602
603// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000604void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000605 // Bitmask of available domains for this instruction after taking collapsed
606 // operands into account.
607 unsigned available = mask;
608
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000609 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000610 SmallVector<int, 4> used;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000611 if (LiveRegs)
612 for (unsigned i = mi->getDesc().getNumDefs(),
613 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000614 MachineOperand &mo = mi->getOperand(i);
615 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000616 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000617 DomainValue *dv = LiveRegs[rx].Value;
618 if (dv == nullptr)
619 continue;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000620 // Bitmask of domains that dv and available have in common.
621 unsigned common = dv->getCommonDomains(available);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000622 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000623 if (dv->isCollapsed()) {
624 // Restrict available domains to the ones in common with the operand.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000625 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000626 // penalty for this operand.
627 if (common) available = common;
628 } else if (common)
629 // Open DomainValue is compatible, save it for merging.
Chris Lattner503a0ef2010-03-31 20:32:51 +0000630 used.push_back(rx);
631 else
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000632 // Open DomainValue is not compatible with instruction. It is useless
633 // now.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000634 kill(rx);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000635 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000636 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000637
638 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000639 if (isPowerOf2_32(available)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000640 unsigned domain = countTrailingZeros(available);
Jakob Stoklund Olesenb48c9942011-09-27 22:57:18 +0000641 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000642 visitHardInstr(mi, domain);
643 return;
644 }
645
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000646 // Kill off any remaining uses that don't match available, and build a list of
647 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000648 SmallVector<LiveReg, 4> Regs;
Craig Toppere1c1d362013-07-03 05:11:49 +0000649 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000650 int rx = *i;
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000651 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000652 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000653 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000654 if (!LR.Value->getCommonDomains(available)) {
655 kill(rx);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000656 continue;
657 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000658 // Sorted insertion.
659 bool Inserted = false;
Craig Toppere1c1d362013-07-03 05:11:49 +0000660 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000661 i != e && !Inserted; ++i) {
662 if (LR.Def < i->Def) {
663 Inserted = true;
664 Regs.insert(i, LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000665 }
666 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000667 if (!Inserted)
668 Regs.push_back(LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000669 }
670
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000671 // doms are now sorted in order of appearance. Try to merge them all, giving
672 // priority to the latest ones.
Craig Topperc0196b12014-04-14 00:51:57 +0000673 DomainValue *dv = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000674 while (!Regs.empty()) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000675 if (!dv) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000676 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000677 // Force the first dv to match the current instruction.
678 dv->AvailableDomains = dv->getCommonDomains(available);
679 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner503a0ef2010-03-31 20:32:51 +0000680 continue;
681 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000682
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000683 DomainValue *Latest = Regs.pop_back_val().Value;
684 // Skip already merged values.
685 if (Latest == dv || Latest->Next)
686 continue;
687 if (merge(dv, Latest))
688 continue;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000689
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000690 // If latest didn't merge, it is useless now. Kill all registers using it.
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000691 for (int i : used) {
692 assert(LiveRegs && "no space allocated for live registers");
693 if (LiveRegs[i].Value == Latest)
694 kill(i);
695 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000696 }
697
698 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000699 if (!dv) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000700 dv = alloc();
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000701 dv->AvailableDomains = available;
702 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000703 dv->Instrs.push_back(mi);
704
Silviu Baranga3c314992012-10-03 08:29:36 +0000705 // Finally set all defs and non-collapsed uses to dv. We must iterate through
706 // all the operators, including imp-def ones.
707 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
708 ee = mi->operands_end();
709 ii != ee; ++ii) {
710 MachineOperand &mo = *ii;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000711 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000712 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000713 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
714 kill(rx);
715 setLiveReg(rx, dv);
716 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000717 }
718 }
719}
720
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000721bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000722 MF = &mf;
Eric Christopherfc6de422014-08-05 02:39:49 +0000723 TII = MF->getSubtarget().getInstrInfo();
724 TRI = MF->getSubtarget().getRegisterInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000725 LiveRegs = nullptr;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000726 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000727
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000728 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
Craig Toppercf0444b2014-11-17 05:50:14 +0000729 << TRI->getRegClassName(RC) << " **********\n");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000730
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000731 // If no relevant registers are used in the function, we can skip it
732 // completely.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000733 bool anyregs = false;
Matthias Braun9912bb82015-07-14 17:52:07 +0000734 const MachineRegisterInfo &MRI = mf.getRegInfo();
Matthias Braund55bcf22015-08-18 18:54:27 +0000735 for (unsigned Reg : *RC) {
736 if (MRI.isPhysRegUsed(Reg)) {
737 anyregs = true;
738 break;
739 }
740 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000741 if (!anyregs) return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000742
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000743 // Initialize the AliasMap on the first use.
744 if (AliasMap.empty()) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000745 // Given a PhysReg, AliasMap[PhysReg] returns a list of indices into RC and
746 // therefore the LiveRegs array.
747 AliasMap.resize(TRI->getNumRegs());
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000748 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000749 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
750 AI.isValid(); ++AI)
Matthias Braun8142efa2014-12-17 19:13:47 +0000751 AliasMap[*AI].push_back(i);
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000752 }
753
Duncan P. N. Exon Smith8f11e1a2015-10-09 16:54:49 +0000754 MachineBasicBlock *Entry = &*MF->begin();
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000755 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000756 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000757 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
758 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
759 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000760 enterBasicBlock(MBB);
761 if (SeenUnknownBackEdge)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000762 Loops.push_back(MBB);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000763 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000764 ++I)
765 visitInstr(I);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000766 processUndefReads(MBB);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000767 leaveBasicBlock(MBB);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000768 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000769
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000770 // Visit all the loop blocks again in order to merge DomainValues from
771 // back-edges.
772 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
773 MachineBasicBlock *MBB = Loops[i];
774 enterBasicBlock(MBB);
Jakob Stoklund Olesenf8ad3362011-11-15 01:15:30 +0000775 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
776 ++I)
777 if (!I->isDebugValue())
778 processDefs(I, false);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000779 processUndefReads(MBB);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000780 leaveBasicBlock(MBB);
781 }
782
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000783 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
784 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
785 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
786 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000787 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000788 continue;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000789 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000790 if (FI->second[i].Value)
791 release(FI->second[i].Value);
Jakob Stoklund Olesen5d082932011-11-08 22:05:17 +0000792 delete[] FI->second;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000793 }
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000794 LiveOuts.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000795 UndefReads.clear();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000796 Avail.clear();
797 Allocator.DestroyAll();
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000798
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000799 return false;
800}
801
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000802FunctionPass *
803llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000804 return new ExeDepsFix(RC);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000805}