blob: 18c82c85e55ddcaed6f9173870373c63db6ee065 [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"
Juergen Ributzka310034e2013-12-14 06:52:56 +000025#include "llvm/CodeGen/LivePhysRegs.h"
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000028#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Target/TargetInstrInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000032#include "llvm/Target/TargetSubtargetInfo.h"
33
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000034using namespace llvm;
35
Chandler Carruth1b9dde02014-04-22 02:02:50 +000036#define DEBUG_TYPE "execution-fix"
37
Chris Lattner503a0ef2010-03-31 20:32:51 +000038/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000039/// of execution domains.
40///
41/// An open DomainValue represents a set of instructions that can still switch
42/// execution domain. Multiple registers may refer to the same open
43/// DomainValue - they will eventually be collapsed to the same execution
44/// domain.
45///
46/// A collapsed DomainValue represents a single register that has been forced
47/// into one of more execution domains. There is a separate collapsed
48/// DomainValue for each register, but it may contain multiple execution
49/// domains. A register value is initially created in a single execution
50/// domain, but if we were forced to pay the penalty of a domain crossing, we
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +000051/// keep track of the fact that the register is now available in multiple
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000052/// domains.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000053namespace {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000054struct DomainValue {
55 // Basic reference counting.
56 unsigned Refs;
57
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000058 // Bitmask of available domains. For an open DomainValue, it is the still
59 // possible domains for collapsing. For a collapsed DomainValue it is the
60 // domains where the register is available for free.
61 unsigned AvailableDomains;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000062
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000063 // Pointer to the next DomainValue in a chain. When two DomainValues are
64 // merged, Victim.Next is set to point to Victor, so old DomainValue
Benjamin Kramerbde91762012-06-02 10:20:22 +000065 // references can be updated by following the chain.
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000066 DomainValue *Next;
67
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000068 // Twiddleable instructions using or defining these registers.
69 SmallVector<MachineInstr*, 8> Instrs;
70
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000071 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000072 // track of the domains where the registers are already available.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000073 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000074
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000075 // Is domain available?
76 bool hasDomain(unsigned domain) const {
Michael Ilsemanaddddc42014-12-15 18:48:43 +000077 assert(domain < std::numeric_limits<unsigned>::digits &&
78 "undefined behavior");
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000079 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000080 }
81
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000082 // Mark domain as available.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000083 void addDomain(unsigned domain) {
84 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000085 }
86
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000087 // Restrict to a single domain available.
88 void setSingleDomain(unsigned domain) {
89 AvailableDomains = 1u << domain;
90 }
91
92 // Return bitmask of domains that are available and in mask.
93 unsigned getCommonDomains(unsigned mask) const {
94 return AvailableDomains & mask;
95 }
96
97 // First domain available.
98 unsigned getFirstDomain() const {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000099 return countTrailingZeros(AvailableDomains);
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000100 }
101
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000102 DomainValue() : Refs(0) { clear(); }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000103
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000104 // Clear this DomainValue and point to next which has all its data.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000105 void clear() {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000106 AvailableDomains = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000107 Next = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000108 Instrs.clear();
109 }
110};
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000111}
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000112
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000113namespace {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000114/// LiveReg - Information about a live register.
115struct LiveReg {
116 /// Value currently in this register, or NULL when no value is being tracked.
117 /// This counts as a DomainValue reference.
118 DomainValue *Value;
119
120 /// Instruction that defined this register, relative to the beginning of the
121 /// current basic block. When a LiveReg is used to represent a live-out
122 /// register, this value is relative to the end of the basic block, so it
123 /// will be a negative number.
124 int Def;
125};
126} // anonynous namespace
127
128namespace {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000129class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000130 static char ID;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000131 SpecificBumpPtrAllocator<DomainValue> Allocator;
132 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000133
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000134 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000135 MachineFunction *MF;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000136 const TargetInstrInfo *TII;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000137 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000138 std::vector<int> AliasMap;
139 const unsigned NumRegs;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000140 LiveReg *LiveRegs;
141 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000142 LiveOutMap LiveOuts;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000143
Andrew Trickb6d56be2013-10-14 22:19:03 +0000144 /// List of undefined register reads in this block in forward order.
145 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads;
146
147 /// Storage for register unit liveness.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000148 LivePhysRegs LiveRegSet;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000149
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000150 /// Current instruction number.
151 /// The first instruction in each basic block is 0.
152 int CurInstr;
153
154 /// True when the current block has a predecessor that hasn't been visited
155 /// yet.
156 bool SeenUnknownBackEdge;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000157
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000158public:
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000159 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000160 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000161
Craig Topper4584cd52014-03-07 09:26:03 +0000162 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000163 AU.setPreservesAll();
164 MachineFunctionPass::getAnalysisUsage(AU);
165 }
166
Craig Topper4584cd52014-03-07 09:26:03 +0000167 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000168
Craig Topper4584cd52014-03-07 09:26:03 +0000169 const char *getPassName() const override {
Jakob Stoklund Olesenbaffa7d2011-11-07 21:23:39 +0000170 return "Execution dependency fix";
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000171 }
172
173private:
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000174 // Register mapping.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000175 int regIndex(unsigned Reg);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000176
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000177 // DomainValue allocation.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000178 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000179 DomainValue *retain(DomainValue *DV) {
180 if (DV) ++DV->Refs;
181 return DV;
182 }
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000183 void release(DomainValue*);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000184 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000185
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000186 // LiveRegs manipulations.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000187 void setLiveReg(int rx, DomainValue *DV);
188 void kill(int rx);
189 void force(int rx, unsigned domain);
190 void collapse(DomainValue *dv, unsigned domain);
191 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000192
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000193 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000194 void leaveBasicBlock(MachineBasicBlock*);
195 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000196 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000197 void visitSoftInstr(MachineInstr*, unsigned mask);
198 void visitHardInstr(MachineInstr*, unsigned domain);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000199 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
200 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000201};
202}
203
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000204char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000205
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000206/// Translate TRI register number to an index into our smaller tables of
207/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000208int ExeDepsFix::regIndex(unsigned Reg) {
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000209 assert(Reg < AliasMap.size() && "Invalid register");
210 return AliasMap[Reg];
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000211}
212
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000213DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000214 DomainValue *dv = Avail.empty() ?
215 new(Allocator.Allocate()) DomainValue :
216 Avail.pop_back_val();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000217 if (domain >= 0)
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000218 dv->addDomain(domain);
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000219 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000220 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000221 return dv;
222}
223
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000224/// release - Release a reference to DV. When the last reference is released,
225/// collapse if needed.
226void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000227 while (DV) {
228 assert(DV->Refs && "Bad DomainValue");
229 if (--DV->Refs)
230 return;
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000231
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000232 // There are no more DV references. Collapse any contained instructions.
233 if (DV->AvailableDomains && !DV->isCollapsed())
234 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000235
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000236 DomainValue *Next = DV->Next;
237 DV->clear();
238 Avail.push_back(DV);
239 // Also release the next DomainValue in the chain.
240 DV = Next;
241 }
242}
243
244/// resolve - Follow the chain of dead DomainValues until a live DomainValue is
245/// reached. Update the referenced pointer when necessary.
246DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
247 DomainValue *DV = DVRef;
248 if (!DV || !DV->Next)
249 return DV;
250
251 // DV has a chain. Find the end.
252 do DV = DV->Next;
253 while (DV->Next);
254
255 // Update DVRef to point to DV.
256 retain(DV);
257 release(DVRef);
258 DVRef = DV;
259 return DV;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000260}
261
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000262/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000263void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000264 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000265 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000266
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000267 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000268 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000269 if (LiveRegs[rx].Value)
270 release(LiveRegs[rx].Value);
271 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000272}
273
274// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000275void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000276 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000277 assert(LiveRegs && "Must enter basic block first.");
278 if (!LiveRegs[rx].Value)
279 return;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000280
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000281 release(LiveRegs[rx].Value);
Craig Topperc0196b12014-04-14 00:51:57 +0000282 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000283}
284
285/// Force register rx into domain.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000286void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000287 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000288 assert(LiveRegs && "Must enter basic block first.");
289 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000290 if (dv->isCollapsed())
291 dv->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000292 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000293 collapse(dv, domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000294 else {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000295 // This is an incompatible open DomainValue. Collapse it to whatever and
296 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000297 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000298 assert(LiveRegs[rx].Value && "Not live after collapse?");
299 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000300 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000301 } else {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000302 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000303 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000304 }
305}
306
307/// Collapse open DomainValue into given domain. If there are multiple
308/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000309void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000310 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000311
312 // Collapse all the instructions.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000313 while (!dv->Instrs.empty())
Jakob Stoklund Olesenb48c9942011-09-27 22:57:18 +0000314 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000315 dv->setSingleDomain(domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000316
317 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000318 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000319 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000320 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000321 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000322}
323
324/// Merge - All instructions and registers in B are moved to A, and B is
325/// released.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000326bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000327 assert(!A->isCollapsed() && "Cannot merge into collapsed");
328 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen58ca0a62010-03-31 20:05:12 +0000329 if (A == B)
Jakob Stoklund Olesen4cd58662010-03-31 17:13:16 +0000330 return true;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000331 // Restrict to the domains that A and B have in common.
332 unsigned common = A->getCommonDomains(B->AvailableDomains);
333 if (!common)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000334 return false;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000335 A->AvailableDomains = common;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000336 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000337
338 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000339 B->clear();
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000340 // All uses of B are referred to A.
341 B->Next = retain(A);
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000342
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000343 for (unsigned rx = 0; rx != NumRegs; ++rx) {
344 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000345 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000346 setLiveReg(rx, A);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000347 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000348 return true;
349}
350
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000351// enterBasicBlock - Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000352void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000353 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000354 SeenUnknownBackEdge = false;
355
356 // Reset instruction counter in each basic block.
357 CurInstr = 0;
358
Andrew Trickb6d56be2013-10-14 22:19:03 +0000359 // Set up UndefReads to track undefined register reads.
360 UndefReads.clear();
Juergen Ributzka310034e2013-12-14 06:52:56 +0000361 LiveRegSet.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000362
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000363 // Set up LiveRegs to represent registers entering MBB.
364 if (!LiveRegs)
365 LiveRegs = new LiveReg[NumRegs];
366
367 // Default values are 'nothing happened a long time ago'.
368 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Craig Topperc0196b12014-04-14 00:51:57 +0000369 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000370 LiveRegs[rx].Def = -(1 << 20);
371 }
372
373 // This is the entry block.
374 if (MBB->pred_empty()) {
375 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
376 e = MBB->livein_end(); i != e; ++i) {
377 int rx = regIndex(*i);
378 if (rx < 0)
379 continue;
380 // Treat function live-ins as if they were defined just before the first
381 // instruction. Usually, function arguments are set up immediately
382 // before the call.
383 LiveRegs[rx].Def = -1;
384 }
385 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
386 return;
387 }
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000388
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000389 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000390 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
391 pe = MBB->pred_end(); pi != pe; ++pi) {
392 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
393 if (fi == LiveOuts.end()) {
394 SeenUnknownBackEdge = true;
395 continue;
396 }
397 assert(fi->second && "Can't have NULL entries");
398
399 for (unsigned rx = 0; rx != NumRegs; ++rx) {
400 // Use the most recent predecessor def for each register.
401 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
402
403 DomainValue *pdv = resolve(fi->second[rx].Value);
404 if (!pdv)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000405 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000406 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000407 setLiveReg(rx, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000408 continue;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000409 }
Chris Lattner503a0ef2010-03-31 20:32:51 +0000410
411 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000412 if (LiveRegs[rx].Value->isCollapsed()) {
Eric Christopher650c8f22014-05-20 17:11:11 +0000413 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000414 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
415 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
416 collapse(pdv, Domain);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000417 continue;
418 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000419
Chris Lattner503a0ef2010-03-31 20:32:51 +0000420 // Currently open, merge in predecessor.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000421 if (!pdv->isCollapsed())
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000422 merge(LiveRegs[rx].Value, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000423 else
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000424 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000425 }
426 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000427 DEBUG(dbgs() << "BB#" << MBB->getNumber()
428 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000429}
430
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000431void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000432 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000433 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000434 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000435 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
436
437 if (First) {
438 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
439 // the end of this block instead of the beginning.
440 for (unsigned i = 0, e = NumRegs; i != e; ++i)
441 LiveRegs[i].Def -= CurInstr;
442 } else {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000443 // Insertion failed, this must be the second pass.
444 // Release all the DomainValues instead of keeping them.
445 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000446 release(LiveRegs[i].Value);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000447 delete[] LiveRegs;
448 }
Craig Topperc0196b12014-04-14 00:51:57 +0000449 LiveRegs = nullptr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000450}
451
452void ExeDepsFix::visitInstr(MachineInstr *MI) {
453 if (MI->isDebugValue())
454 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000455
456 // Update instructions with explicit execution domains.
457 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(MI);
458 if (DomP.first) {
459 if (DomP.second)
460 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000461 else
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000462 visitHardInstr(MI, DomP.first);
463 }
464
465 // Process defs to track register ages, and kill values clobbered by generic
466 // instructions.
467 processDefs(MI, !DomP.first);
468}
469
Andrew Trickb6d56be2013-10-14 22:19:03 +0000470/// \brief Return true to if it makes sense to break dependence on a partial def
471/// or undef use.
472bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
473 unsigned Pref) {
474 int rx = regIndex(MI->getOperand(OpIdx).getReg());
475 if (rx < 0)
476 return false;
477
478 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
479 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
480
481 if (Pref > Clearance) {
482 DEBUG(dbgs() << ": Break dependency.\n");
483 return true;
484 }
485 // The current clearance seems OK, but we may be ignoring a def from a
486 // back-edge.
487 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
488 DEBUG(dbgs() << ": OK .\n");
489 return false;
490 }
491 // A def from an unprocessed back-edge may make us break this dependency.
492 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
493 return false;
494}
495
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000496// Update def-ages for registers defined by MI.
497// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000498//
499// Also break dependencies on partial defs and undef uses.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000500void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
501 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000502
503 // Break dependence on undef uses. Do this before updating LiveRegs below.
504 unsigned OpNum;
505 unsigned Pref = TII->getUndefRegClearance(MI, OpNum, TRI);
506 if (Pref) {
507 if (shouldBreakDependence(MI, OpNum, Pref))
508 UndefReads.push_back(std::make_pair(MI, OpNum));
509 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000510 const MCInstrDesc &MCID = MI->getDesc();
511 for (unsigned i = 0,
Evan Cheng7f8e5632011-12-07 07:15:52 +0000512 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000513 i != e; ++i) {
514 MachineOperand &MO = MI->getOperand(i);
515 if (!MO.isReg())
516 continue;
517 if (MO.isImplicit())
518 break;
519 if (MO.isUse())
520 continue;
521 int rx = regIndex(MO.getReg());
522 if (rx < 0)
523 continue;
524
525 // This instruction explicitly defines rx.
526 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
527 << '\t' << *MI);
528
Andrew Trickb6d56be2013-10-14 22:19:03 +0000529 // Check clearance before partial register updates.
530 // Call breakDependence before setting LiveRegs[rx].Def.
531 unsigned Pref = TII->getPartialRegUpdateClearance(MI, i, TRI);
532 if (Pref && shouldBreakDependence(MI, i, Pref))
533 TII->breakPartialRegDependency(MI, i, TRI);
534
Jakob Stoklund Olesenf8ad3362011-11-15 01:15:30 +0000535 // How many instructions since rx was last written?
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000536 LiveRegs[rx].Def = CurInstr;
537
538 // Kill off domains redefined by generic instructions.
539 if (Kill)
540 kill(rx);
541 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000542 ++CurInstr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000543}
544
Andrew Trickb6d56be2013-10-14 22:19:03 +0000545/// \break Break false dependencies on undefined register reads.
546///
547/// Walk the block backward computing precise liveness. This is expensive, so we
548/// only do it on demand. Note that the occurrence of undefined register reads
549/// that should be broken is very rare, but when they occur we may have many in
550/// a single block.
551void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
552 if (UndefReads.empty())
553 return;
554
555 // Collect this block's live out register units.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000556 LiveRegSet.init(TRI);
557 LiveRegSet.addLiveOuts(MBB);
558
Andrew Trickb6d56be2013-10-14 22:19:03 +0000559 MachineInstr *UndefMI = UndefReads.back().first;
560 unsigned OpIdx = UndefReads.back().second;
561
562 for (MachineBasicBlock::reverse_iterator I = MBB->rbegin(), E = MBB->rend();
563 I != E; ++I) {
Andrew Trick60cf0ad2013-12-13 22:23:54 +0000564 // Update liveness, including the current instruction's defs.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000565 LiveRegSet.stepBackward(*I);
Andrew Trick3a996932013-10-15 03:39:43 +0000566
Andrew Trickb6d56be2013-10-14 22:19:03 +0000567 if (UndefMI == &*I) {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000568 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Andrew Trickb6d56be2013-10-14 22:19:03 +0000569 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI);
570
571 UndefReads.pop_back();
572 if (UndefReads.empty())
573 return;
574
575 UndefMI = UndefReads.back().first;
576 OpIdx = UndefReads.back().second;
577 }
Andrew Trickb6d56be2013-10-14 22:19:03 +0000578 }
579}
580
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000581// A hard instruction only works in one domain. All input registers will be
582// forced into that domain.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000583void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000584 // Collapse all uses.
585 for (unsigned i = mi->getDesc().getNumDefs(),
586 e = mi->getDesc().getNumOperands(); i != e; ++i) {
587 MachineOperand &mo = mi->getOperand(i);
588 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000589 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000590 if (rx < 0) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000591 force(rx, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000592 }
593
594 // Kill all defs and force them.
595 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
596 MachineOperand &mo = mi->getOperand(i);
597 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000598 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000599 if (rx < 0) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000600 kill(rx);
601 force(rx, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000602 }
603}
604
605// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000606void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000607 // Bitmask of available domains for this instruction after taking collapsed
608 // operands into account.
609 unsigned available = mask;
610
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000611 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000612 SmallVector<int, 4> used;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000613 if (LiveRegs)
614 for (unsigned i = mi->getDesc().getNumDefs(),
615 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000616 MachineOperand &mo = mi->getOperand(i);
617 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000618 int rx = regIndex(mo.getReg());
Chris Lattner503a0ef2010-03-31 20:32:51 +0000619 if (rx < 0) continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000620 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000621 // Bitmask of domains that dv and available have in common.
622 unsigned common = dv->getCommonDomains(available);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000623 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000624 if (dv->isCollapsed()) {
625 // Restrict available domains to the ones in common with the operand.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000626 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000627 // penalty for this operand.
628 if (common) available = common;
629 } else if (common)
630 // Open DomainValue is compatible, save it for merging.
Chris Lattner503a0ef2010-03-31 20:32:51 +0000631 used.push_back(rx);
632 else
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000633 // Open DomainValue is not compatible with instruction. It is useless
634 // now.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000635 kill(rx);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000636 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000637 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000638
639 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000640 if (isPowerOf2_32(available)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000641 unsigned domain = countTrailingZeros(available);
Jakob Stoklund Olesenb48c9942011-09-27 22:57:18 +0000642 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000643 visitHardInstr(mi, domain);
644 return;
645 }
646
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000647 // Kill off any remaining uses that don't match available, and build a list of
648 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000649 SmallVector<LiveReg, 4> Regs;
Craig Toppere1c1d362013-07-03 05:11:49 +0000650 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000651 int rx = *i;
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000652 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000653 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000654 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000655 if (!LR.Value->getCommonDomains(available)) {
656 kill(rx);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000657 continue;
658 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000659 // Sorted insertion.
660 bool Inserted = false;
Craig Toppere1c1d362013-07-03 05:11:49 +0000661 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000662 i != e && !Inserted; ++i) {
663 if (LR.Def < i->Def) {
664 Inserted = true;
665 Regs.insert(i, LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000666 }
667 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000668 if (!Inserted)
669 Regs.push_back(LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000670 }
671
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000672 // doms are now sorted in order of appearance. Try to merge them all, giving
673 // priority to the latest ones.
Craig Topperc0196b12014-04-14 00:51:57 +0000674 DomainValue *dv = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000675 while (!Regs.empty()) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000676 if (!dv) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000677 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000678 // Force the first dv to match the current instruction.
679 dv->AvailableDomains = dv->getCommonDomains(available);
680 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner503a0ef2010-03-31 20:32:51 +0000681 continue;
682 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000683
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000684 DomainValue *Latest = Regs.pop_back_val().Value;
685 // Skip already merged values.
686 if (Latest == dv || Latest->Next)
687 continue;
688 if (merge(dv, Latest))
689 continue;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000690
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000691 // If latest didn't merge, it is useless now. Kill all registers using it.
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000692 for (int i : used) {
693 assert(LiveRegs && "no space allocated for live registers");
694 if (LiveRegs[i].Value == Latest)
695 kill(i);
696 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000697 }
698
699 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000700 if (!dv) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000701 dv = alloc();
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000702 dv->AvailableDomains = available;
703 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000704 dv->Instrs.push_back(mi);
705
Silviu Baranga3c314992012-10-03 08:29:36 +0000706 // Finally set all defs and non-collapsed uses to dv. We must iterate through
707 // all the operators, including imp-def ones.
708 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
709 ee = mi->operands_end();
710 ii != ee; ++ii) {
711 MachineOperand &mo = *ii;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000712 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000713 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000714 if (rx < 0) continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000715 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000716 kill(rx);
717 setLiveReg(rx, dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000718 }
719 }
720}
721
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000722bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000723 MF = &mf;
Eric Christopherfc6de422014-08-05 02:39:49 +0000724 TII = MF->getSubtarget().getInstrInfo();
725 TRI = MF->getSubtarget().getRegisterInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000726 LiveRegs = nullptr;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000727 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000728
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000729 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
Craig Toppercf0444b2014-11-17 05:50:14 +0000730 << TRI->getRegClassName(RC) << " **********\n");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000731
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000732 // If no relevant registers are used in the function, we can skip it
733 // completely.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000734 bool anyregs = false;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000735 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
736 I != E; ++I)
Jakob Stoklund Olesen07364422012-10-17 18:44:18 +0000737 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesen3588a432011-12-21 19:50:05 +0000738 anyregs = true;
739 break;
740 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000741 if (!anyregs) return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000742
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000743 // Initialize the AliasMap on the first use.
744 if (AliasMap.empty()) {
745 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
746 // or -1.
747 AliasMap.resize(TRI->getNumRegs(), -1);
748 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000749 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
750 AI.isValid(); ++AI)
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000751 AliasMap[*AI] = i;
752 }
753
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000754 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000755 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000756 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000757 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
758 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
759 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000760 enterBasicBlock(MBB);
761 if (SeenUnknownBackEdge)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000762 Loops.push_back(MBB);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000763 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000764 ++I)
765 visitInstr(I);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000766 processUndefReads(MBB);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000767 leaveBasicBlock(MBB);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000768 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000769
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000770 // Visit all the loop blocks again in order to merge DomainValues from
771 // back-edges.
772 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
773 MachineBasicBlock *MBB = Loops[i];
774 enterBasicBlock(MBB);
Jakob Stoklund Olesenf8ad3362011-11-15 01:15:30 +0000775 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
776 ++I)
777 if (!I->isDebugValue())
778 processDefs(I, false);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000779 processUndefReads(MBB);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000780 leaveBasicBlock(MBB);
781 }
782
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000783 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
784 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
785 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
786 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000787 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000788 continue;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000789 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000790 if (FI->second[i].Value)
791 release(FI->second[i].Value);
Jakob Stoklund Olesen5d082932011-11-08 22:05:17 +0000792 delete[] FI->second;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000793 }
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000794 LiveOuts.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000795 UndefReads.clear();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000796 Avail.clear();
797 Allocator.DestroyAll();
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000798
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000799 return false;
800}
801
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000802FunctionPass *
803llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000804 return new ExeDepsFix(RC);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000805}