blob: 5f91db9251c1f71b9aff729271d1da08937fe789 [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
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000171 MachineFunctionProperties getRequiredProperties() const override {
172 return MachineFunctionProperties().set(
173 MachineFunctionProperties::Property::AllVRegsAllocated);
174 }
175
Craig Topper4584cd52014-03-07 09:26:03 +0000176 const char *getPassName() const override {
Jakob Stoklund Olesenbaffa7d2011-11-07 21:23:39 +0000177 return "Execution dependency fix";
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000178 }
179
180private:
Matthias Braun8142efa2014-12-17 19:13:47 +0000181 iterator_range<SmallVectorImpl<int>::const_iterator>
Matthias Braun046318b2015-03-06 18:56:20 +0000182 regIndices(unsigned Reg) const;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000183
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000184 // DomainValue allocation.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000185 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000186 DomainValue *retain(DomainValue *DV) {
187 if (DV) ++DV->Refs;
188 return DV;
189 }
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000190 void release(DomainValue*);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000191 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000192
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000193 // LiveRegs manipulations.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000194 void setLiveReg(int rx, DomainValue *DV);
195 void kill(int rx);
196 void force(int rx, unsigned domain);
197 void collapse(DomainValue *dv, unsigned domain);
198 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000199
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000200 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000201 void leaveBasicBlock(MachineBasicBlock*);
202 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000203 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000204 void visitSoftInstr(MachineInstr*, unsigned mask);
205 void visitHardInstr(MachineInstr*, unsigned domain);
Marina Yatsina88f0c312016-08-11 07:32:08 +0000206 void pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
207 unsigned Pref);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000208 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
209 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000210};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000211}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000212
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000213char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000214
Matthias Braun046318b2015-03-06 18:56:20 +0000215/// Translate TRI register number to a list of indices into our smaller tables
Matthias Braun8142efa2014-12-17 19:13:47 +0000216/// of interesting registers.
217iterator_range<SmallVectorImpl<int>::const_iterator>
Matthias Braun046318b2015-03-06 18:56:20 +0000218ExeDepsFix::regIndices(unsigned Reg) const {
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000219 assert(Reg < AliasMap.size() && "Invalid register");
Matthias Braun8142efa2014-12-17 19:13:47 +0000220 const auto &Entry = AliasMap[Reg];
221 return make_range(Entry.begin(), Entry.end());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000222}
223
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000224DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000225 DomainValue *dv = Avail.empty() ?
226 new(Allocator.Allocate()) DomainValue :
227 Avail.pop_back_val();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000228 if (domain >= 0)
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000229 dv->addDomain(domain);
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000230 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000231 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000232 return dv;
233}
234
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000235/// Release a reference to DV. When the last reference is released,
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000236/// collapse if needed.
237void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000238 while (DV) {
239 assert(DV->Refs && "Bad DomainValue");
240 if (--DV->Refs)
241 return;
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000242
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000243 // There are no more DV references. Collapse any contained instructions.
244 if (DV->AvailableDomains && !DV->isCollapsed())
245 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000246
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000247 DomainValue *Next = DV->Next;
248 DV->clear();
249 Avail.push_back(DV);
250 // Also release the next DomainValue in the chain.
251 DV = Next;
252 }
253}
254
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000255/// Follow the chain of dead DomainValues until a live DomainValue is reached.
256/// Update the referenced pointer when necessary.
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000257DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
258 DomainValue *DV = DVRef;
259 if (!DV || !DV->Next)
260 return DV;
261
262 // DV has a chain. Find the end.
263 do DV = DV->Next;
264 while (DV->Next);
265
266 // Update DVRef to point to DV.
267 retain(DV);
268 release(DVRef);
269 DVRef = DV;
270 return DV;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000271}
272
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000273/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000274void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000275 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000276 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000277
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000278 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000279 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000280 if (LiveRegs[rx].Value)
281 release(LiveRegs[rx].Value);
282 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000283}
284
285// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000286void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000287 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000288 assert(LiveRegs && "Must enter basic block first.");
289 if (!LiveRegs[rx].Value)
290 return;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000291
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000292 release(LiveRegs[rx].Value);
Craig Topperc0196b12014-04-14 00:51:57 +0000293 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000294}
295
296/// Force register rx into domain.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000297void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000298 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000299 assert(LiveRegs && "Must enter basic block first.");
300 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000301 if (dv->isCollapsed())
302 dv->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000303 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000304 collapse(dv, domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000305 else {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000306 // This is an incompatible open DomainValue. Collapse it to whatever and
307 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000308 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000309 assert(LiveRegs[rx].Value && "Not live after collapse?");
310 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000311 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000312 } else {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000313 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000314 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000315 }
316}
317
318/// Collapse open DomainValue into given domain. If there are multiple
319/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000320void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000321 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000322
323 // Collapse all the instructions.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000324 while (!dv->Instrs.empty())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000325 TII->setExecutionDomain(*dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000326 dv->setSingleDomain(domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000327
328 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000329 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000330 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000331 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000332 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000333}
334
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000335/// All instructions and registers in B are moved to A, and B is released.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000336bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000337 assert(!A->isCollapsed() && "Cannot merge into collapsed");
338 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen58ca0a62010-03-31 20:05:12 +0000339 if (A == B)
Jakob Stoklund Olesen4cd58662010-03-31 17:13:16 +0000340 return true;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000341 // Restrict to the domains that A and B have in common.
342 unsigned common = A->getCommonDomains(B->AvailableDomains);
343 if (!common)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000344 return false;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000345 A->AvailableDomains = common;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000346 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000347
348 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000349 B->clear();
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000350 // All uses of B are referred to A.
351 B->Next = retain(A);
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000352
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000353 for (unsigned rx = 0; rx != NumRegs; ++rx) {
354 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000355 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000356 setLiveReg(rx, A);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000357 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000358 return true;
359}
360
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000361/// Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000362void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000363 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000364 SeenUnknownBackEdge = false;
365
366 // Reset instruction counter in each basic block.
367 CurInstr = 0;
368
Andrew Trickb6d56be2013-10-14 22:19:03 +0000369 // Set up UndefReads to track undefined register reads.
370 UndefReads.clear();
Juergen Ributzka310034e2013-12-14 06:52:56 +0000371 LiveRegSet.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000372
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000373 // Set up LiveRegs to represent registers entering MBB.
374 if (!LiveRegs)
375 LiveRegs = new LiveReg[NumRegs];
376
377 // Default values are 'nothing happened a long time ago'.
378 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Craig Topperc0196b12014-04-14 00:51:57 +0000379 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000380 LiveRegs[rx].Def = -(1 << 20);
381 }
382
383 // This is the entry block.
384 if (MBB->pred_empty()) {
Matthias Braund9da1622015-09-09 18:08:03 +0000385 for (const auto &LI : MBB->liveins()) {
386 for (int rx : regIndices(LI.PhysReg)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000387 // Treat function live-ins as if they were defined just before the first
388 // instruction. Usually, function arguments are set up immediately
389 // before the call.
390 LiveRegs[rx].Def = -1;
391 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000392 }
393 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
394 return;
395 }
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000396
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000397 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000398 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
399 pe = MBB->pred_end(); pi != pe; ++pi) {
400 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
401 if (fi == LiveOuts.end()) {
402 SeenUnknownBackEdge = true;
403 continue;
404 }
405 assert(fi->second && "Can't have NULL entries");
406
407 for (unsigned rx = 0; rx != NumRegs; ++rx) {
408 // Use the most recent predecessor def for each register.
409 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
410
411 DomainValue *pdv = resolve(fi->second[rx].Value);
412 if (!pdv)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000413 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000414 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000415 setLiveReg(rx, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000416 continue;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000417 }
Chris Lattner503a0ef2010-03-31 20:32:51 +0000418
419 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000420 if (LiveRegs[rx].Value->isCollapsed()) {
Eric Christopher650c8f22014-05-20 17:11:11 +0000421 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000422 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
423 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
424 collapse(pdv, Domain);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000425 continue;
426 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000427
Chris Lattner503a0ef2010-03-31 20:32:51 +0000428 // Currently open, merge in predecessor.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000429 if (!pdv->isCollapsed())
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000430 merge(LiveRegs[rx].Value, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000431 else
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000432 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000433 }
434 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000435 DEBUG(dbgs() << "BB#" << MBB->getNumber()
436 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000437}
438
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000439void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000440 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000441 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000442 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000443 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
444
445 if (First) {
446 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
447 // the end of this block instead of the beginning.
448 for (unsigned i = 0, e = NumRegs; i != e; ++i)
449 LiveRegs[i].Def -= CurInstr;
450 } else {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000451 // Insertion failed, this must be the second pass.
452 // Release all the DomainValues instead of keeping them.
453 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000454 release(LiveRegs[i].Value);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000455 delete[] LiveRegs;
456 }
Craig Topperc0196b12014-04-14 00:51:57 +0000457 LiveRegs = nullptr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000458}
459
460void ExeDepsFix::visitInstr(MachineInstr *MI) {
461 if (MI->isDebugValue())
462 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000463
464 // Update instructions with explicit execution domains.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000465 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(*MI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000466 if (DomP.first) {
467 if (DomP.second)
468 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000469 else
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000470 visitHardInstr(MI, DomP.first);
471 }
472
473 // Process defs to track register ages, and kill values clobbered by generic
474 // instructions.
475 processDefs(MI, !DomP.first);
476}
477
Marina Yatsina88f0c312016-08-11 07:32:08 +0000478/// \brief Helps avoid false dependencies on undef registers by updating the
479/// machine instructions' undef operand to use a register that the instruction
480/// is truly dependent on, or use a register with clearance higher than Pref.
481void ExeDepsFix::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
482 unsigned Pref) {
483 MachineOperand &MO = MI->getOperand(OpIdx);
484 assert(MO.isUndef() && "Expected undef machine operand");
485
486 unsigned OriginalReg = MO.getReg();
487
488 // Update only undef operands that are mapped to one register.
489 if (AliasMap[OriginalReg].size() != 1)
490 return;
491
492 // Get the undef operand's register class
493 const TargetRegisterClass *OpRC =
494 TII->getRegClass(MI->getDesc(), OpIdx, TRI, *MF);
495
496 // If the instruction has a true dependency, we can hide the false depdency
497 // behind it.
498 for (MachineOperand &CurrMO : MI->operands()) {
499 if (!CurrMO.isReg() || CurrMO.isDef() || CurrMO.isUndef() ||
500 !OpRC->contains(CurrMO.getReg()))
501 continue;
502 // We found a true dependency - replace the undef register with the true
503 // dependency.
504 MO.setReg(CurrMO.getReg());
505 return;
506 }
507
508 // Go over all registers in the register class and find the register with
509 // max clearance or clearance higher than Pref.
510 unsigned MaxClearance = 0;
511 unsigned MaxClearanceReg = OriginalReg;
512 for (unsigned rx = 0; rx < OpRC->getNumRegs(); ++rx) {
513 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
514 if (Clearance <= MaxClearance)
515 continue;
516 MaxClearance = Clearance;
517 MaxClearanceReg = OpRC->getRegister(rx);
518
519 if (MaxClearance > Pref)
520 break;
521 }
522
523 // Update the operand if we found a register with better clearance.
524 if (MaxClearanceReg != OriginalReg)
525 MO.setReg(MaxClearanceReg);
526}
527
Andrew Trickb6d56be2013-10-14 22:19:03 +0000528/// \brief Return true to if it makes sense to break dependence on a partial def
529/// or undef use.
530bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
531 unsigned Pref) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000532 unsigned reg = MI->getOperand(OpIdx).getReg();
Matthias Braun046318b2015-03-06 18:56:20 +0000533 for (int rx : regIndices(reg)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000534 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
535 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000536
Matthias Braun8142efa2014-12-17 19:13:47 +0000537 if (Pref > Clearance) {
538 DEBUG(dbgs() << ": Break dependency.\n");
539 continue;
540 }
541 // The current clearance seems OK, but we may be ignoring a def from a
542 // back-edge.
543 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
544 DEBUG(dbgs() << ": OK .\n");
545 return false;
546 }
547 // A def from an unprocessed back-edge may make us break this dependency.
548 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000549 return false;
550 }
Matthias Braun8142efa2014-12-17 19:13:47 +0000551 return true;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000552}
553
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000554// Update def-ages for registers defined by MI.
555// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000556//
557// Also break dependencies on partial defs and undef uses.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000558void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
559 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000560
561 // Break dependence on undef uses. Do this before updating LiveRegs below.
562 unsigned OpNum;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000563 unsigned Pref = TII->getUndefRegClearance(*MI, OpNum, TRI);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000564 if (Pref) {
Marina Yatsina88f0c312016-08-11 07:32:08 +0000565 pickBestRegisterForUndef(MI, OpNum, Pref);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000566 if (shouldBreakDependence(MI, OpNum, Pref))
567 UndefReads.push_back(std::make_pair(MI, OpNum));
568 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000569 const MCInstrDesc &MCID = MI->getDesc();
570 for (unsigned i = 0,
Evan Cheng7f8e5632011-12-07 07:15:52 +0000571 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000572 i != e; ++i) {
573 MachineOperand &MO = MI->getOperand(i);
574 if (!MO.isReg())
575 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000576 if (MO.isUse())
577 continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000578 for (int rx : regIndices(MO.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000579 // This instruction explicitly defines rx.
580 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
581 << '\t' << *MI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000582
Matthias Braun8142efa2014-12-17 19:13:47 +0000583 // Check clearance before partial register updates.
584 // Call breakDependence before setting LiveRegs[rx].Def.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000585 unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
Matthias Braun8142efa2014-12-17 19:13:47 +0000586 if (Pref && shouldBreakDependence(MI, i, Pref))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000587 TII->breakPartialRegDependency(*MI, i, TRI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000588
Matthias Braun8142efa2014-12-17 19:13:47 +0000589 // How many instructions since rx was last written?
590 LiveRegs[rx].Def = CurInstr;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000591
Matthias Braun8142efa2014-12-17 19:13:47 +0000592 // Kill off domains redefined by generic instructions.
593 if (Kill)
594 kill(rx);
595 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000596 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000597 ++CurInstr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000598}
599
Andrew Trickb6d56be2013-10-14 22:19:03 +0000600/// \break Break false dependencies on undefined register reads.
601///
602/// Walk the block backward computing precise liveness. This is expensive, so we
603/// only do it on demand. Note that the occurrence of undefined register reads
604/// that should be broken is very rare, but when they occur we may have many in
605/// a single block.
606void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
607 if (UndefReads.empty())
608 return;
609
610 // Collect this block's live out register units.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000611 LiveRegSet.init(TRI);
Matthias Braun24f26e62016-05-03 00:08:46 +0000612 // We do not need to care about pristine registers as they are just preserved
613 // but not actually used in the function.
Matthias Braund1aabb22016-05-03 00:24:32 +0000614 LiveRegSet.addLiveOutsNoPristines(*MBB);
Juergen Ributzka310034e2013-12-14 06:52:56 +0000615
Andrew Trickb6d56be2013-10-14 22:19:03 +0000616 MachineInstr *UndefMI = UndefReads.back().first;
617 unsigned OpIdx = UndefReads.back().second;
618
Pete Cooper7679afd2015-07-24 21:13:43 +0000619 for (MachineInstr &I : make_range(MBB->rbegin(), MBB->rend())) {
Andrew Trick60cf0ad2013-12-13 22:23:54 +0000620 // Update liveness, including the current instruction's defs.
Pete Cooper7679afd2015-07-24 21:13:43 +0000621 LiveRegSet.stepBackward(I);
Andrew Trick3a996932013-10-15 03:39:43 +0000622
Pete Cooper7679afd2015-07-24 21:13:43 +0000623 if (UndefMI == &I) {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000624 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000625 TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000626
627 UndefReads.pop_back();
628 if (UndefReads.empty())
629 return;
630
631 UndefMI = UndefReads.back().first;
632 OpIdx = UndefReads.back().second;
633 }
Andrew Trickb6d56be2013-10-14 22:19:03 +0000634 }
635}
636
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000637// A hard instruction only works in one domain. All input registers will be
638// forced into that domain.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000639void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000640 // Collapse all uses.
641 for (unsigned i = mi->getDesc().getNumDefs(),
642 e = mi->getDesc().getNumOperands(); i != e; ++i) {
643 MachineOperand &mo = mi->getOperand(i);
644 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000645 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000646 force(rx, domain);
647 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000648 }
649
650 // Kill all defs and force them.
651 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
652 MachineOperand &mo = mi->getOperand(i);
653 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000654 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000655 kill(rx);
656 force(rx, domain);
657 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000658 }
659}
660
661// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000662void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000663 // Bitmask of available domains for this instruction after taking collapsed
664 // operands into account.
665 unsigned available = mask;
666
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000667 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000668 SmallVector<int, 4> used;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000669 if (LiveRegs)
670 for (unsigned i = mi->getDesc().getNumDefs(),
671 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000672 MachineOperand &mo = mi->getOperand(i);
673 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000674 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000675 DomainValue *dv = LiveRegs[rx].Value;
676 if (dv == nullptr)
677 continue;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000678 // Bitmask of domains that dv and available have in common.
679 unsigned common = dv->getCommonDomains(available);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000680 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000681 if (dv->isCollapsed()) {
682 // Restrict available domains to the ones in common with the operand.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000683 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000684 // penalty for this operand.
685 if (common) available = common;
686 } else if (common)
687 // Open DomainValue is compatible, save it for merging.
Chris Lattner503a0ef2010-03-31 20:32:51 +0000688 used.push_back(rx);
689 else
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000690 // Open DomainValue is not compatible with instruction. It is useless
691 // now.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000692 kill(rx);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000693 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000694 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000695
696 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000697 if (isPowerOf2_32(available)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000698 unsigned domain = countTrailingZeros(available);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000699 TII->setExecutionDomain(*mi, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000700 visitHardInstr(mi, domain);
701 return;
702 }
703
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000704 // Kill off any remaining uses that don't match available, and build a list of
705 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000706 SmallVector<LiveReg, 4> Regs;
Craig Toppere1c1d362013-07-03 05:11:49 +0000707 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000708 int rx = *i;
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000709 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000710 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000711 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000712 if (!LR.Value->getCommonDomains(available)) {
713 kill(rx);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000714 continue;
715 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000716 // Sorted insertion.
717 bool Inserted = false;
Craig Toppere1c1d362013-07-03 05:11:49 +0000718 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000719 i != e && !Inserted; ++i) {
720 if (LR.Def < i->Def) {
721 Inserted = true;
722 Regs.insert(i, LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000723 }
724 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000725 if (!Inserted)
726 Regs.push_back(LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000727 }
728
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000729 // doms are now sorted in order of appearance. Try to merge them all, giving
730 // priority to the latest ones.
Craig Topperc0196b12014-04-14 00:51:57 +0000731 DomainValue *dv = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000732 while (!Regs.empty()) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000733 if (!dv) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000734 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000735 // Force the first dv to match the current instruction.
736 dv->AvailableDomains = dv->getCommonDomains(available);
737 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner503a0ef2010-03-31 20:32:51 +0000738 continue;
739 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000740
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000741 DomainValue *Latest = Regs.pop_back_val().Value;
742 // Skip already merged values.
743 if (Latest == dv || Latest->Next)
744 continue;
745 if (merge(dv, Latest))
746 continue;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000747
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000748 // If latest didn't merge, it is useless now. Kill all registers using it.
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000749 for (int i : used) {
750 assert(LiveRegs && "no space allocated for live registers");
751 if (LiveRegs[i].Value == Latest)
752 kill(i);
753 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000754 }
755
756 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000757 if (!dv) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000758 dv = alloc();
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000759 dv->AvailableDomains = available;
760 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000761 dv->Instrs.push_back(mi);
762
Silviu Baranga3c314992012-10-03 08:29:36 +0000763 // Finally set all defs and non-collapsed uses to dv. We must iterate through
764 // all the operators, including imp-def ones.
765 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
766 ee = mi->operands_end();
767 ii != ee; ++ii) {
768 MachineOperand &mo = *ii;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000769 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000770 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000771 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
772 kill(rx);
773 setLiveReg(rx, dv);
774 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000775 }
776 }
777}
778
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000779bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000780 if (skipFunction(*mf.getFunction()))
781 return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000782 MF = &mf;
Eric Christopherfc6de422014-08-05 02:39:49 +0000783 TII = MF->getSubtarget().getInstrInfo();
784 TRI = MF->getSubtarget().getRegisterInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000785 LiveRegs = nullptr;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000786 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000787
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000788 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
Craig Toppercf0444b2014-11-17 05:50:14 +0000789 << TRI->getRegClassName(RC) << " **********\n");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000790
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000791 // If no relevant registers are used in the function, we can skip it
792 // completely.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000793 bool anyregs = false;
Matthias Braun9912bb82015-07-14 17:52:07 +0000794 const MachineRegisterInfo &MRI = mf.getRegInfo();
Matthias Braund55bcf22015-08-18 18:54:27 +0000795 for (unsigned Reg : *RC) {
796 if (MRI.isPhysRegUsed(Reg)) {
797 anyregs = true;
798 break;
799 }
800 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000801 if (!anyregs) return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000802
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000803 // Initialize the AliasMap on the first use.
804 if (AliasMap.empty()) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000805 // Given a PhysReg, AliasMap[PhysReg] returns a list of indices into RC and
806 // therefore the LiveRegs array.
807 AliasMap.resize(TRI->getNumRegs());
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000808 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000809 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
810 AI.isValid(); ++AI)
Matthias Braun8142efa2014-12-17 19:13:47 +0000811 AliasMap[*AI].push_back(i);
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000812 }
813
Duncan P. N. Exon Smith8f11e1a2015-10-09 16:54:49 +0000814 MachineBasicBlock *Entry = &*MF->begin();
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000815 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000816 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000817 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
818 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
819 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000820 enterBasicBlock(MBB);
821 if (SeenUnknownBackEdge)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000822 Loops.push_back(MBB);
Sanjay Patel7dd45692015-12-29 17:15:22 +0000823 for (MachineInstr &MI : *MBB)
824 visitInstr(&MI);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000825 processUndefReads(MBB);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000826 leaveBasicBlock(MBB);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000827 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000828
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000829 // Visit all the loop blocks again in order to merge DomainValues from
830 // back-edges.
Sanjay Patel7dd45692015-12-29 17:15:22 +0000831 for (MachineBasicBlock *MBB : Loops) {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000832 enterBasicBlock(MBB);
Sanjay Patel7dd45692015-12-29 17:15:22 +0000833 for (MachineInstr &MI : *MBB)
834 if (!MI.isDebugValue())
835 processDefs(&MI, false);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000836 processUndefReads(MBB);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000837 leaveBasicBlock(MBB);
838 }
839
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000840 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
841 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
842 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
843 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000844 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000845 continue;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000846 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000847 if (FI->second[i].Value)
848 release(FI->second[i].Value);
Jakob Stoklund Olesen5d082932011-11-08 22:05:17 +0000849 delete[] FI->second;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000850 }
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000851 LiveOuts.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000852 UndefReads.clear();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000853 Avail.clear();
854 Allocator.DestroyAll();
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000855
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000856 return false;
857}
858
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000859FunctionPass *
860llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000861 return new ExeDepsFix(RC);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000862}