blob: 4a310f70769bc0e7d6e5170dbc9285b679c5c091 [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;
Keno Fischer578cf7a2017-01-30 23:37:03 +0000145 struct MBBInfo {
146 // Keeps clearance and domain information for all registers. Note that this
147 // is different from the usual definition notion of liveness. The CPU
148 // doesn't care whether or not we consider a register killed.
149 LiveReg *OutRegs;
150
151 // Whether we have gotten to this block in primary processing yet.
152 bool PrimaryCompleted;
153
154 // The number of predecessors for which primary processing has completed
155 unsigned IncomingProcessed;
156
157 // The value of `IncomingProcessed` at the start of primary processing
158 unsigned PrimaryIncoming;
159
160 // The number of predecessors for which all processing steps are done.
161 unsigned IncomingCompleted;
162
163 MBBInfo()
164 : OutRegs(nullptr), PrimaryCompleted(false), IncomingProcessed(0),
165 PrimaryIncoming(0), IncomingCompleted(0) {}
166 };
167 typedef DenseMap<MachineBasicBlock *, MBBInfo> MBBInfoMap;
168 MBBInfoMap MBBInfos;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000169
Andrew Trickb6d56be2013-10-14 22:19:03 +0000170 /// List of undefined register reads in this block in forward order.
171 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads;
172
173 /// Storage for register unit liveness.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000174 LivePhysRegs LiveRegSet;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000175
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000176 /// Current instruction number.
177 /// The first instruction in each basic block is 0.
178 int CurInstr;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000179public:
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000180 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000181 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000182
Craig Topper4584cd52014-03-07 09:26:03 +0000183 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000184 AU.setPreservesAll();
185 MachineFunctionPass::getAnalysisUsage(AU);
186 }
187
Craig Topper4584cd52014-03-07 09:26:03 +0000188 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000189
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000190 MachineFunctionProperties getRequiredProperties() const override {
191 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000192 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000193 }
194
Mehdi Amini117296c2016-10-01 02:56:57 +0000195 StringRef getPassName() const override { return "Execution dependency fix"; }
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000196
197private:
Matthias Braun8142efa2014-12-17 19:13:47 +0000198 iterator_range<SmallVectorImpl<int>::const_iterator>
Matthias Braun046318b2015-03-06 18:56:20 +0000199 regIndices(unsigned Reg) const;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000200 // DomainValue allocation.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000201 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000202 DomainValue *retain(DomainValue *DV) {
203 if (DV) ++DV->Refs;
204 return DV;
205 }
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000206 void release(DomainValue*);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000207 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000208
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000209 // LiveRegs manipulations.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000210 void setLiveReg(int rx, DomainValue *DV);
211 void kill(int rx);
212 void force(int rx, unsigned domain);
213 void collapse(DomainValue *dv, unsigned domain);
214 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000215
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000216 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000217 void leaveBasicBlock(MachineBasicBlock*);
Keno Fischer578cf7a2017-01-30 23:37:03 +0000218 bool isBlockDone(MachineBasicBlock *);
219 void processBasicBlock(MachineBasicBlock *MBB, bool PrimaryPass);
220 void updateSuccessors(MachineBasicBlock *MBB, bool PrimaryPass);
221 bool visitInstr(MachineInstr *);
222 void processDefs(MachineInstr *, bool breakDependency, bool Kill);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000223 void visitSoftInstr(MachineInstr*, unsigned mask);
224 void visitHardInstr(MachineInstr*, unsigned domain);
Marina Yatsina88f0c312016-08-11 07:32:08 +0000225 void pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
226 unsigned Pref);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000227 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
228 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000229};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000230}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000231
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000232char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000233
Matthias Braun046318b2015-03-06 18:56:20 +0000234/// Translate TRI register number to a list of indices into our smaller tables
Matthias Braun8142efa2014-12-17 19:13:47 +0000235/// of interesting registers.
236iterator_range<SmallVectorImpl<int>::const_iterator>
Matthias Braun046318b2015-03-06 18:56:20 +0000237ExeDepsFix::regIndices(unsigned Reg) const {
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000238 assert(Reg < AliasMap.size() && "Invalid register");
Matthias Braun8142efa2014-12-17 19:13:47 +0000239 const auto &Entry = AliasMap[Reg];
240 return make_range(Entry.begin(), Entry.end());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000241}
242
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000243DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000244 DomainValue *dv = Avail.empty() ?
245 new(Allocator.Allocate()) DomainValue :
246 Avail.pop_back_val();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000247 if (domain >= 0)
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000248 dv->addDomain(domain);
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000249 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000250 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000251 return dv;
252}
253
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000254/// Release a reference to DV. When the last reference is released,
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000255/// collapse if needed.
256void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000257 while (DV) {
258 assert(DV->Refs && "Bad DomainValue");
259 if (--DV->Refs)
260 return;
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000261
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000262 // There are no more DV references. Collapse any contained instructions.
263 if (DV->AvailableDomains && !DV->isCollapsed())
264 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000265
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000266 DomainValue *Next = DV->Next;
267 DV->clear();
268 Avail.push_back(DV);
269 // Also release the next DomainValue in the chain.
270 DV = Next;
271 }
272}
273
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000274/// Follow the chain of dead DomainValues until a live DomainValue is reached.
275/// Update the referenced pointer when necessary.
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000276DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
277 DomainValue *DV = DVRef;
278 if (!DV || !DV->Next)
279 return DV;
280
281 // DV has a chain. Find the end.
282 do DV = DV->Next;
283 while (DV->Next);
284
285 // Update DVRef to point to DV.
286 retain(DV);
287 release(DVRef);
288 DVRef = DV;
289 return DV;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000290}
291
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000292/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000293void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000294 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000295 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000296
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000297 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000298 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000299 if (LiveRegs[rx].Value)
300 release(LiveRegs[rx].Value);
301 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000302}
303
304// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000305void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000306 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000307 assert(LiveRegs && "Must enter basic block first.");
308 if (!LiveRegs[rx].Value)
309 return;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000310
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000311 release(LiveRegs[rx].Value);
Craig Topperc0196b12014-04-14 00:51:57 +0000312 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000313}
314
315/// Force register rx into domain.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000316void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000317 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000318 assert(LiveRegs && "Must enter basic block first.");
319 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000320 if (dv->isCollapsed())
321 dv->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000322 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000323 collapse(dv, domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000324 else {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000325 // This is an incompatible open DomainValue. Collapse it to whatever and
326 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000327 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000328 assert(LiveRegs[rx].Value && "Not live after collapse?");
329 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000330 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000331 } else {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000332 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000333 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000334 }
335}
336
337/// Collapse open DomainValue into given domain. If there are multiple
338/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000339void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000340 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000341
342 // Collapse all the instructions.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000343 while (!dv->Instrs.empty())
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000344 TII->setExecutionDomain(*dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000345 dv->setSingleDomain(domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000346
347 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000348 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000349 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000350 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000351 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000352}
353
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000354/// All instructions and registers in B are moved to A, and B is released.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000355bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000356 assert(!A->isCollapsed() && "Cannot merge into collapsed");
357 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen58ca0a62010-03-31 20:05:12 +0000358 if (A == B)
Jakob Stoklund Olesen4cd58662010-03-31 17:13:16 +0000359 return true;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000360 // Restrict to the domains that A and B have in common.
361 unsigned common = A->getCommonDomains(B->AvailableDomains);
362 if (!common)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000363 return false;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000364 A->AvailableDomains = common;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000365 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000366
367 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000368 B->clear();
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000369 // All uses of B are referred to A.
370 B->Next = retain(A);
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000371
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000372 for (unsigned rx = 0; rx != NumRegs; ++rx) {
373 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000374 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000375 setLiveReg(rx, A);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000376 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000377 return true;
378}
379
Sanjay Patel4297c3f2015-03-15 18:16:04 +0000380/// Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000381void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000382 // Reset instruction counter in each basic block.
383 CurInstr = 0;
384
Andrew Trickb6d56be2013-10-14 22:19:03 +0000385 // Set up UndefReads to track undefined register reads.
386 UndefReads.clear();
Juergen Ributzka310034e2013-12-14 06:52:56 +0000387 LiveRegSet.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000388
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000389 // Set up LiveRegs to represent registers entering MBB.
390 if (!LiveRegs)
391 LiveRegs = new LiveReg[NumRegs];
392
393 // Default values are 'nothing happened a long time ago'.
394 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Craig Topperc0196b12014-04-14 00:51:57 +0000395 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000396 LiveRegs[rx].Def = -(1 << 20);
397 }
398
399 // This is the entry block.
400 if (MBB->pred_empty()) {
Matthias Braund9da1622015-09-09 18:08:03 +0000401 for (const auto &LI : MBB->liveins()) {
402 for (int rx : regIndices(LI.PhysReg)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000403 // Treat function live-ins as if they were defined just before the first
404 // instruction. Usually, function arguments are set up immediately
405 // before the call.
406 LiveRegs[rx].Def = -1;
407 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000408 }
409 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
410 return;
411 }
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000412
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000413 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000414 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
415 pe = MBB->pred_end(); pi != pe; ++pi) {
Keno Fischer578cf7a2017-01-30 23:37:03 +0000416 auto fi = MBBInfos.find(*pi);
417 assert(fi != MBBInfos.end() &&
418 "Should have pre-allocated MBBInfos for all MBBs");
419 LiveReg *Incoming = fi->second.OutRegs;
420 // Incoming is null if this is a backedge from a BB
421 // we haven't processed yet
422 if (Incoming == nullptr) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000423 continue;
424 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000425
426 for (unsigned rx = 0; rx != NumRegs; ++rx) {
427 // Use the most recent predecessor def for each register.
Keno Fischer578cf7a2017-01-30 23:37:03 +0000428 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, Incoming[rx].Def);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000429
Keno Fischer578cf7a2017-01-30 23:37:03 +0000430 DomainValue *pdv = resolve(Incoming[rx].Value);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000431 if (!pdv)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000432 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000433 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000434 setLiveReg(rx, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000435 continue;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000436 }
Chris Lattner503a0ef2010-03-31 20:32:51 +0000437
438 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000439 if (LiveRegs[rx].Value->isCollapsed()) {
Eric Christopher650c8f22014-05-20 17:11:11 +0000440 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000441 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
442 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
443 collapse(pdv, Domain);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000444 continue;
445 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000446
Chris Lattner503a0ef2010-03-31 20:32:51 +0000447 // Currently open, merge in predecessor.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000448 if (!pdv->isCollapsed())
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000449 merge(LiveRegs[rx].Value, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000450 else
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000451 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000452 }
453 }
Keno Fischer578cf7a2017-01-30 23:37:03 +0000454 DEBUG(
455 dbgs() << "BB#" << MBB->getNumber()
456 << (!isBlockDone(MBB) ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000457}
458
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000459void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000460 assert(LiveRegs && "Must enter basic block first.");
Keno Fischer578cf7a2017-01-30 23:37:03 +0000461 LiveReg *OldOutRegs = MBBInfos[MBB].OutRegs;
462 // Save register clearances at end of MBB - used by enterBasicBlock().
463 MBBInfos[MBB].OutRegs = LiveRegs;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000464
Keno Fischer578cf7a2017-01-30 23:37:03 +0000465 // While processing the basic block, we kept `Def` relative to the start
466 // of the basic block for convenience. However, future use of this information
467 // only cares about the clearance from the end of the block, so adjust
468 // everything to be relative to the end of the basic block.
469 for (unsigned i = 0, e = NumRegs; i != e; ++i)
470 LiveRegs[i].Def -= CurInstr;
471 if (OldOutRegs) {
472 // This must be the second pass.
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000473 // Release all the DomainValues instead of keeping them.
474 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Keno Fischer578cf7a2017-01-30 23:37:03 +0000475 release(OldOutRegs[i].Value);
476 delete[] OldOutRegs;
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000477 }
Craig Topperc0196b12014-04-14 00:51:57 +0000478 LiveRegs = nullptr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000479}
480
Keno Fischer578cf7a2017-01-30 23:37:03 +0000481bool ExeDepsFix::visitInstr(MachineInstr *MI) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000482 // Update instructions with explicit execution domains.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000483 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(*MI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000484 if (DomP.first) {
485 if (DomP.second)
486 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000487 else
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000488 visitHardInstr(MI, DomP.first);
489 }
490
Keno Fischer578cf7a2017-01-30 23:37:03 +0000491 return !DomP.first;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000492}
493
Marina Yatsina88f0c312016-08-11 07:32:08 +0000494/// \brief Helps avoid false dependencies on undef registers by updating the
495/// machine instructions' undef operand to use a register that the instruction
496/// is truly dependent on, or use a register with clearance higher than Pref.
497void ExeDepsFix::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx,
498 unsigned Pref) {
499 MachineOperand &MO = MI->getOperand(OpIdx);
500 assert(MO.isUndef() && "Expected undef machine operand");
501
502 unsigned OriginalReg = MO.getReg();
503
504 // Update only undef operands that are mapped to one register.
505 if (AliasMap[OriginalReg].size() != 1)
506 return;
507
508 // Get the undef operand's register class
509 const TargetRegisterClass *OpRC =
510 TII->getRegClass(MI->getDesc(), OpIdx, TRI, *MF);
511
512 // If the instruction has a true dependency, we can hide the false depdency
513 // behind it.
514 for (MachineOperand &CurrMO : MI->operands()) {
515 if (!CurrMO.isReg() || CurrMO.isDef() || CurrMO.isUndef() ||
516 !OpRC->contains(CurrMO.getReg()))
517 continue;
518 // We found a true dependency - replace the undef register with the true
519 // dependency.
520 MO.setReg(CurrMO.getReg());
521 return;
522 }
523
524 // Go over all registers in the register class and find the register with
525 // max clearance or clearance higher than Pref.
526 unsigned MaxClearance = 0;
527 unsigned MaxClearanceReg = OriginalReg;
Marina Yatsina53ce3f92016-08-17 19:07:40 +0000528 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC);
529 for (auto Reg : Order) {
Marina Yatsina4b226422016-08-17 11:40:21 +0000530 assert(AliasMap[Reg].size() == 1 &&
531 "Reg is expected to be mapped to a single index");
532 int RCrx = *regIndices(Reg).begin();
533 unsigned Clearance = CurInstr - LiveRegs[RCrx].Def;
Marina Yatsina88f0c312016-08-11 07:32:08 +0000534 if (Clearance <= MaxClearance)
535 continue;
536 MaxClearance = Clearance;
Marina Yatsina4b226422016-08-17 11:40:21 +0000537 MaxClearanceReg = Reg;
Marina Yatsina88f0c312016-08-11 07:32:08 +0000538
539 if (MaxClearance > Pref)
540 break;
541 }
542
543 // Update the operand if we found a register with better clearance.
544 if (MaxClearanceReg != OriginalReg)
545 MO.setReg(MaxClearanceReg);
546}
547
Andrew Trickb6d56be2013-10-14 22:19:03 +0000548/// \brief Return true to if it makes sense to break dependence on a partial def
549/// or undef use.
550bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
551 unsigned Pref) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000552 unsigned reg = MI->getOperand(OpIdx).getReg();
Matthias Braun046318b2015-03-06 18:56:20 +0000553 for (int rx : regIndices(reg)) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000554 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
555 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000556
Matthias Braun8142efa2014-12-17 19:13:47 +0000557 if (Pref > Clearance) {
558 DEBUG(dbgs() << ": Break dependency.\n");
559 continue;
560 }
Keno Fischer578cf7a2017-01-30 23:37:03 +0000561 DEBUG(dbgs() << ": OK .\n");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000562 return false;
563 }
Matthias Braun8142efa2014-12-17 19:13:47 +0000564 return true;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000565}
566
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000567// Update def-ages for registers defined by MI.
568// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000569//
570// Also break dependencies on partial defs and undef uses.
Keno Fischer578cf7a2017-01-30 23:37:03 +0000571void ExeDepsFix::processDefs(MachineInstr *MI, bool breakDependency,
572 bool Kill) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000573 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000574
575 // Break dependence on undef uses. Do this before updating LiveRegs below.
576 unsigned OpNum;
Keno Fischer578cf7a2017-01-30 23:37:03 +0000577 if (breakDependency) {
578 unsigned Pref = TII->getUndefRegClearance(*MI, OpNum, TRI);
579 if (Pref) {
580 pickBestRegisterForUndef(MI, OpNum, Pref);
581 if (shouldBreakDependence(MI, OpNum, Pref))
582 UndefReads.push_back(std::make_pair(MI, OpNum));
583 }
Andrew Trickb6d56be2013-10-14 22:19:03 +0000584 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000585 const MCInstrDesc &MCID = MI->getDesc();
586 for (unsigned i = 0,
Evan Cheng7f8e5632011-12-07 07:15:52 +0000587 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000588 i != e; ++i) {
589 MachineOperand &MO = MI->getOperand(i);
590 if (!MO.isReg())
591 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000592 if (MO.isUse())
593 continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000594 for (int rx : regIndices(MO.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000595 // This instruction explicitly defines rx.
596 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
597 << '\t' << *MI);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000598
Keno Fischer578cf7a2017-01-30 23:37:03 +0000599 if (breakDependency) {
600 // Check clearance before partial register updates.
601 // Call breakDependence before setting LiveRegs[rx].Def.
602 unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI);
603 if (Pref && shouldBreakDependence(MI, i, Pref))
604 TII->breakPartialRegDependency(*MI, i, TRI);
605 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000606
Matthias Braun8142efa2014-12-17 19:13:47 +0000607 // How many instructions since rx was last written?
608 LiveRegs[rx].Def = CurInstr;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000609
Matthias Braun8142efa2014-12-17 19:13:47 +0000610 // Kill off domains redefined by generic instructions.
611 if (Kill)
612 kill(rx);
613 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000614 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000615 ++CurInstr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000616}
617
Andrew Trickb6d56be2013-10-14 22:19:03 +0000618/// \break Break false dependencies on undefined register reads.
619///
620/// Walk the block backward computing precise liveness. This is expensive, so we
621/// only do it on demand. Note that the occurrence of undefined register reads
622/// that should be broken is very rare, but when they occur we may have many in
623/// a single block.
624void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
625 if (UndefReads.empty())
626 return;
627
628 // Collect this block's live out register units.
Matthias Braun0c989a82016-12-08 00:15:51 +0000629 LiveRegSet.init(*TRI);
Matthias Braun24f26e62016-05-03 00:08:46 +0000630 // We do not need to care about pristine registers as they are just preserved
631 // but not actually used in the function.
Matthias Braund1aabb22016-05-03 00:24:32 +0000632 LiveRegSet.addLiveOutsNoPristines(*MBB);
Juergen Ributzka310034e2013-12-14 06:52:56 +0000633
Andrew Trickb6d56be2013-10-14 22:19:03 +0000634 MachineInstr *UndefMI = UndefReads.back().first;
635 unsigned OpIdx = UndefReads.back().second;
636
Pete Cooper7679afd2015-07-24 21:13:43 +0000637 for (MachineInstr &I : make_range(MBB->rbegin(), MBB->rend())) {
Andrew Trick60cf0ad2013-12-13 22:23:54 +0000638 // Update liveness, including the current instruction's defs.
Pete Cooper7679afd2015-07-24 21:13:43 +0000639 LiveRegSet.stepBackward(I);
Andrew Trick3a996932013-10-15 03:39:43 +0000640
Pete Cooper7679afd2015-07-24 21:13:43 +0000641 if (UndefMI == &I) {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000642 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000643 TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000644
645 UndefReads.pop_back();
646 if (UndefReads.empty())
647 return;
648
649 UndefMI = UndefReads.back().first;
650 OpIdx = UndefReads.back().second;
651 }
Andrew Trickb6d56be2013-10-14 22:19:03 +0000652 }
653}
654
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000655// A hard instruction only works in one domain. All input registers will be
656// forced into that domain.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000657void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000658 // Collapse all uses.
659 for (unsigned i = mi->getDesc().getNumDefs(),
660 e = mi->getDesc().getNumOperands(); i != e; ++i) {
661 MachineOperand &mo = mi->getOperand(i);
662 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000663 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000664 force(rx, domain);
665 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000666 }
667
668 // Kill all defs and force them.
669 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
670 MachineOperand &mo = mi->getOperand(i);
671 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000672 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000673 kill(rx);
674 force(rx, domain);
675 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000676 }
677}
678
679// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000680void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000681 // Bitmask of available domains for this instruction after taking collapsed
682 // operands into account.
683 unsigned available = mask;
684
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000685 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000686 SmallVector<int, 4> used;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000687 if (LiveRegs)
688 for (unsigned i = mi->getDesc().getNumDefs(),
689 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000690 MachineOperand &mo = mi->getOperand(i);
691 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000692 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000693 DomainValue *dv = LiveRegs[rx].Value;
694 if (dv == nullptr)
695 continue;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000696 // Bitmask of domains that dv and available have in common.
697 unsigned common = dv->getCommonDomains(available);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000698 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000699 if (dv->isCollapsed()) {
700 // Restrict available domains to the ones in common with the operand.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000701 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000702 // penalty for this operand.
703 if (common) available = common;
704 } else if (common)
705 // Open DomainValue is compatible, save it for merging.
Chris Lattner503a0ef2010-03-31 20:32:51 +0000706 used.push_back(rx);
707 else
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000708 // Open DomainValue is not compatible with instruction. It is useless
709 // now.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000710 kill(rx);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000711 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000712 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000713
714 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000715 if (isPowerOf2_32(available)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000716 unsigned domain = countTrailingZeros(available);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000717 TII->setExecutionDomain(*mi, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000718 visitHardInstr(mi, domain);
719 return;
720 }
721
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000722 // Kill off any remaining uses that don't match available, and build a list of
723 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000724 SmallVector<LiveReg, 4> Regs;
Craig Topperc446b1f2017-02-24 06:38:24 +0000725 for (int rx : used) {
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000726 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000727 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000728 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000729 if (!LR.Value->getCommonDomains(available)) {
730 kill(rx);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000731 continue;
732 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000733 // Sorted insertion.
734 bool Inserted = false;
Craig Toppere1c1d362013-07-03 05:11:49 +0000735 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000736 i != e && !Inserted; ++i) {
737 if (LR.Def < i->Def) {
738 Inserted = true;
739 Regs.insert(i, LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000740 }
741 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000742 if (!Inserted)
743 Regs.push_back(LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000744 }
745
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000746 // doms are now sorted in order of appearance. Try to merge them all, giving
747 // priority to the latest ones.
Craig Topperc0196b12014-04-14 00:51:57 +0000748 DomainValue *dv = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000749 while (!Regs.empty()) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000750 if (!dv) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000751 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000752 // Force the first dv to match the current instruction.
753 dv->AvailableDomains = dv->getCommonDomains(available);
754 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner503a0ef2010-03-31 20:32:51 +0000755 continue;
756 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000757
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000758 DomainValue *Latest = Regs.pop_back_val().Value;
759 // Skip already merged values.
760 if (Latest == dv || Latest->Next)
761 continue;
762 if (merge(dv, Latest))
763 continue;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000764
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000765 // If latest didn't merge, it is useless now. Kill all registers using it.
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000766 for (int i : used) {
767 assert(LiveRegs && "no space allocated for live registers");
768 if (LiveRegs[i].Value == Latest)
769 kill(i);
770 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000771 }
772
773 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000774 if (!dv) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000775 dv = alloc();
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000776 dv->AvailableDomains = available;
777 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000778 dv->Instrs.push_back(mi);
779
Silviu Baranga3c314992012-10-03 08:29:36 +0000780 // Finally set all defs and non-collapsed uses to dv. We must iterate through
781 // all the operators, including imp-def ones.
782 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
783 ee = mi->operands_end();
784 ii != ee; ++ii) {
785 MachineOperand &mo = *ii;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000786 if (!mo.isReg()) continue;
Matthias Braun046318b2015-03-06 18:56:20 +0000787 for (int rx : regIndices(mo.getReg())) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000788 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
789 kill(rx);
790 setLiveReg(rx, dv);
791 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000792 }
793 }
794}
795
Keno Fischer578cf7a2017-01-30 23:37:03 +0000796void ExeDepsFix::processBasicBlock(MachineBasicBlock *MBB, bool PrimaryPass) {
797 enterBasicBlock(MBB);
798 // If this block is not done, it makes little sense to make any decisions
799 // based on clearance information. We need to make a second pass anyway,
800 // and by then we'll have better information, so we can avoid doing the work
801 // to try and break dependencies now.
802 bool breakDependency = isBlockDone(MBB);
803 for (MachineInstr &MI : *MBB) {
804 if (!MI.isDebugValue()) {
805 bool Kill = false;
806 if (PrimaryPass)
807 Kill = visitInstr(&MI);
808 processDefs(&MI, breakDependency, Kill);
809 }
810 }
811 if (breakDependency)
812 processUndefReads(MBB);
813 leaveBasicBlock(MBB);
814}
815
816bool ExeDepsFix::isBlockDone(MachineBasicBlock *MBB) {
817 return MBBInfos[MBB].PrimaryCompleted &&
818 MBBInfos[MBB].IncomingCompleted == MBBInfos[MBB].PrimaryIncoming &&
819 MBBInfos[MBB].IncomingProcessed == MBB->pred_size();
820}
821
822void ExeDepsFix::updateSuccessors(MachineBasicBlock *MBB, bool Primary) {
823 bool Done = isBlockDone(MBB);
824 for (auto *Succ : MBB->successors()) {
825 if (!isBlockDone(Succ)) {
826 if (Primary) {
827 MBBInfos[Succ].IncomingProcessed++;
828 }
829 if (Done) {
830 MBBInfos[Succ].IncomingCompleted++;
831 }
832 if (isBlockDone(Succ)) {
833 // Perform secondary processing for this successor. See the big comment
834 // in runOnMachineFunction, for an explanation of the iteration order.
835 processBasicBlock(Succ, false);
836 updateSuccessors(Succ, false);
837 }
838 }
839 }
840}
841
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000842bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000843 if (skipFunction(*mf.getFunction()))
844 return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000845 MF = &mf;
Eric Christopherfc6de422014-08-05 02:39:49 +0000846 TII = MF->getSubtarget().getInstrInfo();
847 TRI = MF->getSubtarget().getRegisterInfo();
Marina Yatsina53ce3f92016-08-17 19:07:40 +0000848 RegClassInfo.runOnMachineFunction(mf);
Craig Topperc0196b12014-04-14 00:51:57 +0000849 LiveRegs = nullptr;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000850 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000851
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000852 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
Craig Toppercf0444b2014-11-17 05:50:14 +0000853 << TRI->getRegClassName(RC) << " **********\n");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000854
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000855 // If no relevant registers are used in the function, we can skip it
856 // completely.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000857 bool anyregs = false;
Matthias Braun9912bb82015-07-14 17:52:07 +0000858 const MachineRegisterInfo &MRI = mf.getRegInfo();
Matthias Braund55bcf22015-08-18 18:54:27 +0000859 for (unsigned Reg : *RC) {
860 if (MRI.isPhysRegUsed(Reg)) {
861 anyregs = true;
862 break;
863 }
864 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000865 if (!anyregs) return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000866
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000867 // Initialize the AliasMap on the first use.
868 if (AliasMap.empty()) {
Matthias Braun8142efa2014-12-17 19:13:47 +0000869 // Given a PhysReg, AliasMap[PhysReg] returns a list of indices into RC and
870 // therefore the LiveRegs array.
871 AliasMap.resize(TRI->getNumRegs());
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000872 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000873 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
874 AI.isValid(); ++AI)
Matthias Braun8142efa2014-12-17 19:13:47 +0000875 AliasMap[*AI].push_back(i);
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000876 }
877
Keno Fischer578cf7a2017-01-30 23:37:03 +0000878 // Initialize the MMBInfos
879 for (auto &MBB : mf) {
880 MBBInfo InitialInfo;
881 MBBInfos.insert(std::make_pair(&MBB, InitialInfo));
882 }
883
884 /*
885 * We want to visit every instruction in every basic block in order to update
886 * it's execution domain or break any false dependencies. However, for the
887 * dependency breaking, we need to know clearances from all predecessors
888 * (including any backedges). One way to do so would be to do two complete
889 * passes over all basic blocks/instructions, the first for recording
890 * clearances, the second to break the dependencies. However, for functions
891 * without backedges, or functions with a lot of straight-line code, and
892 * a small loop, that would be a lot of unnecessary work (since only the
893 * BBs that are part of the loop require two passes). As an example,
894 * consider the following loop.
895 *
896 *
897 * PH -> A -> B (xmm<Undef> -> xmm<Def>) -> C -> D -> EXIT
898 * ^ |
899 * +----------------------------------+
900 *
901 * The iteration order is as follows:
902 * Naive: PH A B C D A' B' C' D'
903 * Optimized: PH A B C A' B' C' D
904 *
905 * Note that we avoid processing D twice, because we can entirely process
906 * the predecessors before getting to D. We call a block that is ready
907 * for its second round of processing `done` (isBlockDone). Once we finish
908 * processing some block, we update the counters in MBBInfos and re-process
909 * any successors that are now done.
910 */
911
Duncan P. N. Exon Smith8f11e1a2015-10-09 16:54:49 +0000912 MachineBasicBlock *Entry = &*MF->begin();
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000913 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
914 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
915 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
916 MachineBasicBlock *MBB = *MBBI;
Keno Fischer578cf7a2017-01-30 23:37:03 +0000917 // N.B: IncomingProcessed and IncomingCompleted were already updated while
918 // processing this block's predecessors.
919 MBBInfos[MBB].PrimaryCompleted = true;
920 MBBInfos[MBB].PrimaryIncoming = MBBInfos[MBB].IncomingProcessed;
921 processBasicBlock(MBB, true);
922 updateSuccessors(MBB, true);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000923 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000924
Keno Fischer578cf7a2017-01-30 23:37:03 +0000925 // We need to go through again and finalize any blocks that are not done yet.
926 // This is possible if blocks have dead predecessors, so we didn't visit them
927 // above.
928 for (ReversePostOrderTraversal<MachineBasicBlock *>::rpo_iterator
929 MBBI = RPOT.begin(),
930 MBBE = RPOT.end();
931 MBBI != MBBE; ++MBBI) {
932 MachineBasicBlock *MBB = *MBBI;
933 if (!isBlockDone(MBB)) {
934 processBasicBlock(MBB, false);
935 // Don't update successors here. We'll get to them anyway through this
936 // loop.
937 }
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000938 }
939
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000940 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
941 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
942 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
Keno Fischer578cf7a2017-01-30 23:37:03 +0000943 auto FI = MBBInfos.find(*MBBI);
944 if (FI == MBBInfos.end() || !FI->second.OutRegs)
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000945 continue;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000946 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Keno Fischer578cf7a2017-01-30 23:37:03 +0000947 if (FI->second.OutRegs[i].Value)
948 release(FI->second.OutRegs[i].Value);
949 delete[] FI->second.OutRegs;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000950 }
Keno Fischer578cf7a2017-01-30 23:37:03 +0000951 MBBInfos.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000952 UndefReads.clear();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000953 Avail.clear();
954 Allocator.DestroyAll();
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000955
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000956 return false;
957}
958
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000959FunctionPass *
960llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000961 return new ExeDepsFix(RC);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000962}