blob: 400413be5b6ad3db86748f34b22ec316d54119ad [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"
Marina Yatsina53ce3f92016-08-17 19:07:40 +000029#include "llvm/CodeGen/RegisterClassInfo.h"
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000030#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Target/TargetInstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000034#include "llvm/Target/TargetSubtargetInfo.h"
35
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000036using namespace llvm;
37
Chandler Carruth1b9dde02014-04-22 02:02:50 +000038#define DEBUG_TYPE "execution-fix"
39
Chris Lattner503a0ef2010-03-31 20:32:51 +000040/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000041/// of execution domains.
42///
43/// An open DomainValue represents a set of instructions that can still switch
44/// execution domain. Multiple registers may refer to the same open
45/// DomainValue - they will eventually be collapsed to the same execution
46/// domain.
47///
48/// A collapsed DomainValue represents a single register that has been forced
49/// into one of more execution domains. There is a separate collapsed
50/// DomainValue for each register, but it may contain multiple execution
51/// domains. A register value is initially created in a single execution
52/// domain, but if we were forced to pay the penalty of a domain crossing, we
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +000053/// keep track of the fact that the register is now available in multiple
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000054/// domains.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000055namespace {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000056struct DomainValue {
57 // Basic reference counting.
58 unsigned Refs;
59
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000060 // Bitmask of available domains. For an open DomainValue, it is the still
61 // possible domains for collapsing. For a collapsed DomainValue it is the
62 // domains where the register is available for free.
63 unsigned AvailableDomains;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000064
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000065 // Pointer to the next DomainValue in a chain. When two DomainValues are
66 // merged, Victim.Next is set to point to Victor, so old DomainValue
Benjamin Kramerbde91762012-06-02 10:20:22 +000067 // references can be updated by following the chain.
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000068 DomainValue *Next;
69
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000070 // Twiddleable instructions using or defining these registers.
71 SmallVector<MachineInstr*, 8> Instrs;
72
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000073 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000074 // track of the domains where the registers are already available.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000075 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000076
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000077 // Is domain available?
78 bool hasDomain(unsigned domain) const {
Aaron Ballman0d6a0102014-12-16 14:04:11 +000079 assert(domain <
80 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +000081 "undefined behavior");
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000082 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000083 }
84
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000085 // Mark domain as available.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000086 void addDomain(unsigned domain) {
87 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000088 }
89
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000090 // Restrict to a single domain available.
91 void setSingleDomain(unsigned domain) {
92 AvailableDomains = 1u << domain;
93 }
94
95 // Return bitmask of domains that are available and in mask.
96 unsigned getCommonDomains(unsigned mask) const {
97 return AvailableDomains & mask;
98 }
99
100 // First domain available.
101 unsigned getFirstDomain() const {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000102 return countTrailingZeros(AvailableDomains);
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000103 }
104
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000105 DomainValue() : Refs(0) { clear(); }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000106
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000107 // Clear this DomainValue and point to next which has all its data.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000108 void clear() {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000109 AvailableDomains = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000110 Next = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000111 Instrs.clear();
112 }
113};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000114}
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000115
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000116namespace {
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000117/// Information about a live register.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000118struct LiveReg {
119 /// Value currently in this register, or NULL when no value is being tracked.
120 /// This counts as a DomainValue reference.
121 DomainValue *Value;
122
123 /// Instruction that defined this register, relative to the beginning of the
124 /// current basic block. When a LiveReg is used to represent a live-out
125 /// register, this value is relative to the end of the basic block, so it
126 /// will be a negative number.
127 int Def;
128};
Sanjay Patel12fa37f2015-03-15 18:11:35 +0000129} // anonymous namespace
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000130
131namespace {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000132class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000133 static char ID;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000134 SpecificBumpPtrAllocator<DomainValue> Allocator;
135 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000136
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000137 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000138 MachineFunction *MF;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000139 const TargetInstrInfo *TII;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000140 const TargetRegisterInfo *TRI;
Marina Yatsina53ce3f92016-08-17 19:07:40 +0000141 RegisterClassInfo RegClassInfo;
Matthias Braun8142efa2014-12-17 19:13:47 +0000142 std::vector<SmallVector<int, 1>> AliasMap;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000143 const unsigned NumRegs;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000144 LiveReg *LiveRegs;
145 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000146 LiveOutMap LiveOuts;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000147
Andrew Trickb6d56be2013-10-14 22:19:03 +0000148 /// List of undefined register reads in this block in forward order.
149 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads;
150
151 /// Storage for register unit liveness.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000152 LivePhysRegs LiveRegSet;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000153
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000154 /// Current instruction number.
155 /// The first instruction in each basic block is 0.
156 int CurInstr;
157
158 /// True when the current block has a predecessor that hasn't been visited
159 /// yet.
160 bool SeenUnknownBackEdge;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000161
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000162public:
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000163 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000164 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000165
Craig Topper4584cd52014-03-07 09:26:03 +0000166 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000167 AU.setPreservesAll();
168 MachineFunctionPass::getAnalysisUsage(AU);
169 }
170
Craig Topper4584cd52014-03-07 09:26:03 +0000171 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000172
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000173 MachineFunctionProperties getRequiredProperties() const override {
174 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000175 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000176 }
177
Craig Topper4584cd52014-03-07 09:26:03 +0000178 const char *getPassName() const override {
Jakob Stoklund Olesenbaffa7d2011-11-07 21:23:39 +0000179 return "Execution dependency fix";
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000180 }
181
182private:
Matthias Braun8142efa2014-12-17 19:13:47 +0000183 iterator_range<SmallVectorImpl<int>::const_iterator>
Matthias Braun046318b2015-03-06 18:56:20 +0000184 regIndices(unsigned Reg) const;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000185
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000186 // DomainValue allocation.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000187 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000188 DomainValue *retain(DomainValue *DV) {
189 if (DV) ++DV->Refs;
190 return DV;
191 }
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000192 void release(DomainValue*);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000193 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000194
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000195 // LiveRegs manipulations.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000196 void setLiveReg(int rx, DomainValue *DV);
197 void kill(int rx);
198 void force(int rx, unsigned domain);
199 void collapse(DomainValue *dv, unsigned domain);
200 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000201
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000202 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000203 void leaveBasicBlock(MachineBasicBlock*);
204 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000205 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000206 void visitSoftInstr(MachineInstr*, unsigned mask);
207 void visitHardInstr(MachineInstr*, unsigned domain);
Marina Yatsina88f0c312016-08-11 07:32:08 +0000208 void pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
209 unsigned Pref);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000210 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
211 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000212};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000213}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000214
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000215char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000216
Matthias Braun046318b2015-03-06 18:56:20 +0000217/// Translate TRI register number to a list of indices into our smaller tables
Matthias Braun8142efa2014-12-17 19:13:47 +0000218/// of interesting registers.
219iterator_range<SmallVectorImpl<int>::const_iterator>
Matthias Braun046318b2015-03-06 18:56:20 +0000220ExeDepsFix::regIndices(unsigned Reg) const {
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000221 assert(Reg < AliasMap.size() && "Invalid register");
Matthias Braun8142efa2014-12-17 19:13:47 +0000222 const auto &Entry = AliasMap[Reg];
223 return make_range(Entry.begin(), Entry.end());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000224}
225
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000226DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000227 DomainValue *dv = Avail.empty() ?
228 new(Allocator.Allocate()) DomainValue :
229 Avail.pop_back_val();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000230 if (domain >= 0)
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000231 dv->addDomain(domain);
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000232 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000233 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000234 return dv;
235}
236
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000237/// Release a reference to DV. When the last reference is released,
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000238/// collapse if needed.
239void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000240 while (DV) {
241 assert(DV->Refs && "Bad DomainValue");
242 if (--DV->Refs)
243 return;
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000244
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000245 // There are no more DV references. Collapse any contained instructions.
246 if (DV->AvailableDomains && !DV->isCollapsed())
247 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000248
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000249 DomainValue *Next = DV->Next;
250 DV->clear();
251 Avail.push_back(DV);
252 // Also release the next DomainValue in the chain.
253 DV = Next;
254 }
255}
256
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000257/// Follow the chain of dead DomainValues until a live DomainValue is reached.
258/// Update the referenced pointer when necessary.
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000259DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
260 DomainValue *DV = DVRef;
261 if (!DV || !DV->Next)
262 return DV;
263
264 // DV has a chain. Find the end.
265 do DV = DV->Next;
266 while (DV->Next);
267
268 // Update DVRef to point to DV.
269 retain(DV);
270 release(DVRef);
271 DVRef = DV;
272 return DV;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000273}
274
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000275/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000276void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000277 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000278 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000279
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000280 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000281 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000282 if (LiveRegs[rx].Value)
283 release(LiveRegs[rx].Value);
284 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000285}
286
287// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000288void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000289 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000290 assert(LiveRegs && "Must enter basic block first.");
291 if (!LiveRegs[rx].Value)
292 return;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000293
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000294 release(LiveRegs[rx].Value);
Craig Topperc0196b12014-04-14 00:51:57 +0000295 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000296}
297
298/// Force register rx into domain.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000299void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000300 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000301 assert(LiveRegs && "Must enter basic block first.");
302 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000303 if (dv->isCollapsed())
304 dv->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000305 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000306 collapse(dv, domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000307 else {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000308 // This is an incompatible open DomainValue. Collapse it to whatever and
309 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000310 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000311 assert(LiveRegs[rx].Value && "Not live after collapse?");
312 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000313 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000314 } else {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000315 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000316 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000317 }
318}
319
320/// Collapse open DomainValue into given domain. If there are multiple
321/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000322void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000323 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000324
325 // Collapse all the instructions.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000326 while (!dv->Instrs.empty())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000327 TII->setExecutionDomain(*dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000328 dv->setSingleDomain(domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000329
330 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000331 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000332 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000333 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000334 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000335}
336
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000337/// All instructions and registers in B are moved to A, and B is released.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000338bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000339 assert(!A->isCollapsed() && "Cannot merge into collapsed");
340 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen58ca0a62010-03-31 20:05:12 +0000341 if (A == B)
Jakob Stoklund Olesen4cd58662010-03-31 17:13:16 +0000342 return true;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000343 // Restrict to the domains that A and B have in common.
344 unsigned common = A->getCommonDomains(B->AvailableDomains);
345 if (!common)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000346 return false;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000347 A->AvailableDomains = common;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000348 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000349
350 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000351 B->clear();
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000352 // All uses of B are referred to A.
353 B->Next = retain(A);
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000354
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000355 for (unsigned rx = 0; rx != NumRegs; ++rx) {
356 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000357 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000358 setLiveReg(rx, A);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000359 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000360 return true;
361}
362
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000363/// Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000364void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000365 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000366 SeenUnknownBackEdge = false;
367
368 // Reset instruction counter in each basic block.
369 CurInstr = 0;
370
Andrew Trickb6d56be2013-10-14 22:19:03 +0000371 // Set up UndefReads to track undefined register reads.
372 UndefReads.clear();
Juergen Ributzka310034e2013-12-14 06:52:56 +0000373 LiveRegSet.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000374
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000375 // Set up LiveRegs to represent registers entering MBB.
376 if (!LiveRegs)
377 LiveRegs = new LiveReg[NumRegs];
378
379 // Default values are 'nothing happened a long time ago'.
380 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Craig Topperc0196b12014-04-14 00:51:57 +0000381 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000382 LiveRegs[rx].Def = -(1 << 20);
383 }
384
385 // This is the entry block.
386 if (MBB->pred_empty()) {
Matthias Braund9da1622015-09-09 18:08:03 +0000387 for (const auto &LI : MBB->liveins()) {
388 for (int rx : regIndices(LI.PhysReg)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000389 // Treat function live-ins as if they were defined just before the first
390 // instruction. Usually, function arguments are set up immediately
391 // before the call.
392 LiveRegs[rx].Def = -1;
393 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000394 }
395 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
396 return;
397 }
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000398
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000399 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000400 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
401 pe = MBB->pred_end(); pi != pe; ++pi) {
402 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
403 if (fi == LiveOuts.end()) {
404 SeenUnknownBackEdge = true;
405 continue;
406 }
407 assert(fi->second && "Can't have NULL entries");
408
409 for (unsigned rx = 0; rx != NumRegs; ++rx) {
410 // Use the most recent predecessor def for each register.
411 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
412
413 DomainValue *pdv = resolve(fi->second[rx].Value);
414 if (!pdv)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000415 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000416 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000417 setLiveReg(rx, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000418 continue;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000419 }
Chris Lattner503a0ef2010-03-31 20:32:51 +0000420
421 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000422 if (LiveRegs[rx].Value->isCollapsed()) {
Eric Christopher650c8f22014-05-20 17:11:11 +0000423 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000424 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
425 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
426 collapse(pdv, Domain);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000427 continue;
428 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000429
Chris Lattner503a0ef2010-03-31 20:32:51 +0000430 // Currently open, merge in predecessor.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000431 if (!pdv->isCollapsed())
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000432 merge(LiveRegs[rx].Value, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000433 else
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000434 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000435 }
436 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000437 DEBUG(dbgs() << "BB#" << MBB->getNumber()
438 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000439}
440
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000441void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000442 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000443 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000444 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000445 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
446
447 if (First) {
448 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
449 // the end of this block instead of the beginning.
450 for (unsigned i = 0, e = NumRegs; i != e; ++i)
451 LiveRegs[i].Def -= CurInstr;
452 } else {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000453 // Insertion failed, this must be the second pass.
454 // Release all the DomainValues instead of keeping them.
455 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000456 release(LiveRegs[i].Value);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000457 delete[] LiveRegs;
458 }
Craig Topperc0196b12014-04-14 00:51:57 +0000459 LiveRegs = nullptr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000460}
461
462void ExeDepsFix::visitInstr(MachineInstr *MI) {
463 if (MI->isDebugValue())
464 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000465
466 // Update instructions with explicit execution domains.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000467 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(*MI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000468 if (DomP.first) {
469 if (DomP.second)
470 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000471 else
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000472 visitHardInstr(MI, DomP.first);
473 }
474
475 // Process defs to track register ages, and kill values clobbered by generic
476 // instructions.
477 processDefs(MI, !DomP.first);
478}
479
Marina Yatsina88f0c312016-08-11 07:32:08 +0000480/// \brief Helps avoid false dependencies on undef registers by updating the
481/// machine instructions' undef operand to use a register that the instruction
482/// is truly dependent on, or use a register with clearance higher than Pref.
483void ExeDepsFix::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
484 unsigned Pref) {
485 MachineOperand &MO = MI->getOperand(OpIdx);
486 assert(MO.isUndef() && "Expected undef machine operand");
487
488 unsigned OriginalReg = MO.getReg();
489
490 // Update only undef operands that are mapped to one register.
491 if (AliasMap[OriginalReg].size() != 1)
492 return;
493
494 // Get the undef operand's register class
495 const TargetRegisterClass *OpRC =
496 TII->getRegClass(MI->getDesc(), OpIdx, TRI, *MF);
497
498 // If the instruction has a true dependency, we can hide the false depdency
499 // behind it.
500 for (MachineOperand &CurrMO : MI->operands()) {
501 if (!CurrMO.isReg() || CurrMO.isDef() || CurrMO.isUndef() ||
502 !OpRC->contains(CurrMO.getReg()))
503 continue;
504 // We found a true dependency - replace the undef register with the true
505 // dependency.
506 MO.setReg(CurrMO.getReg());
507 return;
508 }
509
510 // Go over all registers in the register class and find the register with
511 // max clearance or clearance higher than Pref.
512 unsigned MaxClearance = 0;
513 unsigned MaxClearanceReg = OriginalReg;
Marina Yatsina53ce3f92016-08-17 19:07:40 +0000514 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
515 for (auto Reg : Order) {
Marina Yatsina4b226422016-08-17 11:40:21 +0000516 assert(AliasMap[Reg].size() == 1 &&
517 "Reg is expected to be mapped to a single index");
518 int RCrx = *regIndices(Reg).begin();
519 unsigned Clearance = CurInstr - LiveRegs[RCrx].Def;
Marina Yatsina88f0c312016-08-11 07:32:08 +0000520 if (Clearance <= MaxClearance)
521 continue;
522 MaxClearance = Clearance;
Marina Yatsina4b226422016-08-17 11:40:21 +0000523 MaxClearanceReg = Reg;
Marina Yatsina88f0c312016-08-11 07:32:08 +0000524
525 if (MaxClearance > Pref)
526 break;
527 }
528
529 // Update the operand if we found a register with better clearance.
530 if (MaxClearanceReg != OriginalReg)
531 MO.setReg(MaxClearanceReg);
532}
533
Andrew Trickb6d56be2013-10-14 22:19:03 +0000534/// \brief Return true to if it makes sense to break dependence on a partial def
535/// or undef use.
536bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
537 unsigned Pref) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000538 unsigned reg = MI->getOperand(OpIdx).getReg();
Matthias Braun046318b2015-03-06 18:56:20 +0000539 for (int rx : regIndices(reg)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000540 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
541 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000542
Matthias Braun8142efa2014-12-17 19:13:47 +0000543 if (Pref > Clearance) {
544 DEBUG(dbgs() << ": Break dependency.\n");
545 continue;
546 }
547 // The current clearance seems OK, but we may be ignoring a def from a
548 // back-edge.
549 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
550 DEBUG(dbgs() << ": OK .\n");
551 return false;
552 }
553 // A def from an unprocessed back-edge may make us break this dependency.
554 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000555 return false;
556 }
Matthias Braun8142efa2014-12-17 19:13:47 +0000557 return true;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000558}
559
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000560// Update def-ages for registers defined by MI.
561// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000562//
563// Also break dependencies on partial defs and undef uses.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000564void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
565 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000566
567 // Break dependence on undef uses. Do this before updating LiveRegs below.
568 unsigned OpNum;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000569 unsigned Pref = TII->getUndefRegClearance(*MI, OpNum, TRI);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000570 if (Pref) {
Marina Yatsina88f0c312016-08-11 07:32:08 +0000571 pickBestRegisterForUndef(MI, OpNum, Pref);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000572 if (shouldBreakDependence(MI, OpNum, Pref))
573 UndefReads.push_back(std::make_pair(MI, OpNum));
574 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000575 const MCInstrDesc &MCID = MI->getDesc();
576 for (unsigned i = 0,
Evan Cheng7f8e5632011-12-07 07:15:52 +0000577 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000578 i != e; ++i) {
579 MachineOperand &MO = MI->getOperand(i);
580 if (!MO.isReg())
581 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000582 if (MO.isUse())
583 continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000584 for (int rx : regIndices(MO.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000585 // This instruction explicitly defines rx.
586 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
587 << '\t' << *MI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000588
Matthias Braun8142efa2014-12-17 19:13:47 +0000589 // Check clearance before partial register updates.
590 // Call breakDependence before setting LiveRegs[rx].Def.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000591 unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
Matthias Braun8142efa2014-12-17 19:13:47 +0000592 if (Pref && shouldBreakDependence(MI, i, Pref))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000593 TII->breakPartialRegDependency(*MI, i, TRI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000594
Matthias Braun8142efa2014-12-17 19:13:47 +0000595 // How many instructions since rx was last written?
596 LiveRegs[rx].Def = CurInstr;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000597
Matthias Braun8142efa2014-12-17 19:13:47 +0000598 // Kill off domains redefined by generic instructions.
599 if (Kill)
600 kill(rx);
601 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000602 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000603 ++CurInstr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000604}
605
Andrew Trickb6d56be2013-10-14 22:19:03 +0000606/// \break Break false dependencies on undefined register reads.
607///
608/// Walk the block backward computing precise liveness. This is expensive, so we
609/// only do it on demand. Note that the occurrence of undefined register reads
610/// that should be broken is very rare, but when they occur we may have many in
611/// a single block.
612void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
613 if (UndefReads.empty())
614 return;
615
616 // Collect this block's live out register units.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000617 LiveRegSet.init(TRI);
Matthias Braun24f26e62016-05-03 00:08:46 +0000618 // We do not need to care about pristine registers as they are just preserved
619 // but not actually used in the function.
Matthias Braund1aabb22016-05-03 00:24:32 +0000620 LiveRegSet.addLiveOutsNoPristines(*MBB);
Juergen Ributzka310034e2013-12-14 06:52:56 +0000621
Andrew Trickb6d56be2013-10-14 22:19:03 +0000622 MachineInstr *UndefMI = UndefReads.back().first;
623 unsigned OpIdx = UndefReads.back().second;
624
Pete Cooper7679afd2015-07-24 21:13:43 +0000625 for (MachineInstr &I : make_range(MBB->rbegin(), MBB->rend())) {
Andrew Trick60cf0ad2013-12-13 22:23:54 +0000626 // Update liveness, including the current instruction's defs.
Pete Cooper7679afd2015-07-24 21:13:43 +0000627 LiveRegSet.stepBackward(I);
Andrew Trick3a996932013-10-15 03:39:43 +0000628
Pete Cooper7679afd2015-07-24 21:13:43 +0000629 if (UndefMI == &I) {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000630 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000631 TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000632
633 UndefReads.pop_back();
634 if (UndefReads.empty())
635 return;
636
637 UndefMI = UndefReads.back().first;
638 OpIdx = UndefReads.back().second;
639 }
Andrew Trickb6d56be2013-10-14 22:19:03 +0000640 }
641}
642
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000643// A hard instruction only works in one domain. All input registers will be
644// forced into that domain.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000645void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000646 // Collapse all uses.
647 for (unsigned i = mi->getDesc().getNumDefs(),
648 e = mi->getDesc().getNumOperands(); i != e; ++i) {
649 MachineOperand &mo = mi->getOperand(i);
650 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000651 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000652 force(rx, domain);
653 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000654 }
655
656 // Kill all defs and force them.
657 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
658 MachineOperand &mo = mi->getOperand(i);
659 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000660 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000661 kill(rx);
662 force(rx, domain);
663 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000664 }
665}
666
667// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000668void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000669 // Bitmask of available domains for this instruction after taking collapsed
670 // operands into account.
671 unsigned available = mask;
672
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000673 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000674 SmallVector<int, 4> used;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000675 if (LiveRegs)
676 for (unsigned i = mi->getDesc().getNumDefs(),
677 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000678 MachineOperand &mo = mi->getOperand(i);
679 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000680 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000681 DomainValue *dv = LiveRegs[rx].Value;
682 if (dv == nullptr)
683 continue;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000684 // Bitmask of domains that dv and available have in common.
685 unsigned common = dv->getCommonDomains(available);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000686 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000687 if (dv->isCollapsed()) {
688 // Restrict available domains to the ones in common with the operand.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000689 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000690 // penalty for this operand.
691 if (common) available = common;
692 } else if (common)
693 // Open DomainValue is compatible, save it for merging.
Chris Lattner503a0ef2010-03-31 20:32:51 +0000694 used.push_back(rx);
695 else
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000696 // Open DomainValue is not compatible with instruction. It is useless
697 // now.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000698 kill(rx);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000699 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000700 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000701
702 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000703 if (isPowerOf2_32(available)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000704 unsigned domain = countTrailingZeros(available);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000705 TII->setExecutionDomain(*mi, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000706 visitHardInstr(mi, domain);
707 return;
708 }
709
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000710 // Kill off any remaining uses that don't match available, and build a list of
711 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000712 SmallVector<LiveReg, 4> Regs;
Craig Toppere1c1d362013-07-03 05:11:49 +0000713 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000714 int rx = *i;
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000715 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000716 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000717 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000718 if (!LR.Value->getCommonDomains(available)) {
719 kill(rx);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000720 continue;
721 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000722 // Sorted insertion.
723 bool Inserted = false;
Craig Toppere1c1d362013-07-03 05:11:49 +0000724 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000725 i != e && !Inserted; ++i) {
726 if (LR.Def < i->Def) {
727 Inserted = true;
728 Regs.insert(i, LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000729 }
730 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000731 if (!Inserted)
732 Regs.push_back(LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000733 }
734
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000735 // doms are now sorted in order of appearance. Try to merge them all, giving
736 // priority to the latest ones.
Craig Topperc0196b12014-04-14 00:51:57 +0000737 DomainValue *dv = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000738 while (!Regs.empty()) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000739 if (!dv) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000740 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000741 // Force the first dv to match the current instruction.
742 dv->AvailableDomains = dv->getCommonDomains(available);
743 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner503a0ef2010-03-31 20:32:51 +0000744 continue;
745 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000746
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000747 DomainValue *Latest = Regs.pop_back_val().Value;
748 // Skip already merged values.
749 if (Latest == dv || Latest->Next)
750 continue;
751 if (merge(dv, Latest))
752 continue;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000753
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000754 // If latest didn't merge, it is useless now. Kill all registers using it.
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000755 for (int i : used) {
756 assert(LiveRegs && "no space allocated for live registers");
757 if (LiveRegs[i].Value == Latest)
758 kill(i);
759 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000760 }
761
762 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000763 if (!dv) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000764 dv = alloc();
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000765 dv->AvailableDomains = available;
766 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000767 dv->Instrs.push_back(mi);
768
Silviu Baranga3c314992012-10-03 08:29:36 +0000769 // Finally set all defs and non-collapsed uses to dv. We must iterate through
770 // all the operators, including imp-def ones.
771 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
772 ee = mi->operands_end();
773 ii != ee; ++ii) {
774 MachineOperand &mo = *ii;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000775 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000776 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000777 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
778 kill(rx);
779 setLiveReg(rx, dv);
780 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000781 }
782 }
783}
784
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000785bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000786 if (skipFunction(*mf.getFunction()))
787 return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000788 MF = &mf;
Eric Christopherfc6de422014-08-05 02:39:49 +0000789 TII = MF->getSubtarget().getInstrInfo();
790 TRI = MF->getSubtarget().getRegisterInfo();
Marina Yatsina53ce3f92016-08-17 19:07:40 +0000791 RegClassInfo.runOnMachineFunction(mf);
Craig Topperc0196b12014-04-14 00:51:57 +0000792 LiveRegs = nullptr;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000793 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000794
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000795 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
Craig Toppercf0444b2014-11-17 05:50:14 +0000796 << TRI->getRegClassName(RC) << " **********\n");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000797
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000798 // If no relevant registers are used in the function, we can skip it
799 // completely.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000800 bool anyregs = false;
Matthias Braun9912bb82015-07-14 17:52:07 +0000801 const MachineRegisterInfo &MRI = mf.getRegInfo();
Matthias Braund55bcf22015-08-18 18:54:27 +0000802 for (unsigned Reg : *RC) {
803 if (MRI.isPhysRegUsed(Reg)) {
804 anyregs = true;
805 break;
806 }
807 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000808 if (!anyregs) return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000809
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000810 // Initialize the AliasMap on the first use.
811 if (AliasMap.empty()) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000812 // Given a PhysReg, AliasMap[PhysReg] returns a list of indices into RC and
813 // therefore the LiveRegs array.
814 AliasMap.resize(TRI->getNumRegs());
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000815 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000816 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
817 AI.isValid(); ++AI)
Matthias Braun8142efa2014-12-17 19:13:47 +0000818 AliasMap[*AI].push_back(i);
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000819 }
820
Duncan P. N. Exon Smith8f11e1a2015-10-09 16:54:49 +0000821 MachineBasicBlock *Entry = &*MF->begin();
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000822 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000823 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000824 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
825 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
826 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000827 enterBasicBlock(MBB);
828 if (SeenUnknownBackEdge)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000829 Loops.push_back(MBB);
Sanjay Patel7dd45692015-12-29 17:15:22 +0000830 for (MachineInstr &MI : *MBB)
831 visitInstr(&MI);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000832 processUndefReads(MBB);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000833 leaveBasicBlock(MBB);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000834 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000835
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000836 // Visit all the loop blocks again in order to merge DomainValues from
837 // back-edges.
Sanjay Patel7dd45692015-12-29 17:15:22 +0000838 for (MachineBasicBlock *MBB : Loops) {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000839 enterBasicBlock(MBB);
Sanjay Patel7dd45692015-12-29 17:15:22 +0000840 for (MachineInstr &MI : *MBB)
841 if (!MI.isDebugValue())
842 processDefs(&MI, false);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000843 processUndefReads(MBB);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000844 leaveBasicBlock(MBB);
845 }
846
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000847 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
848 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
849 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
850 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000851 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000852 continue;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000853 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000854 if (FI->second[i].Value)
855 release(FI->second[i].Value);
Jakob Stoklund Olesen5d082932011-11-08 22:05:17 +0000856 delete[] FI->second;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000857 }
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000858 LiveOuts.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000859 UndefReads.clear();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000860 Avail.clear();
861 Allocator.DestroyAll();
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000862
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000863 return false;
864}
865
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000866FunctionPass *
867llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000868 return new ExeDepsFix(RC);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000869}