blob: 9d3ba5e0a55e83a3231c6b3d73a716e75951ae78 [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 {
Aaron Ballman0d6a0102014-12-16 14:04:11 +000077 assert(domain <
78 static_cast<unsigned>(std::numeric_limits<unsigned>::digits) &&
Michael Ilsemanaddddc42014-12-15 18:48:43 +000079 "undefined behavior");
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000080 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000081 }
82
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000083 // Mark domain as available.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000084 void addDomain(unsigned domain) {
85 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000086 }
87
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000088 // Restrict to a single domain available.
89 void setSingleDomain(unsigned domain) {
90 AvailableDomains = 1u << domain;
91 }
92
93 // Return bitmask of domains that are available and in mask.
94 unsigned getCommonDomains(unsigned mask) const {
95 return AvailableDomains & mask;
96 }
97
98 // First domain available.
99 unsigned getFirstDomain() const {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000100 return countTrailingZeros(AvailableDomains);
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000101 }
102
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000103 DomainValue() : Refs(0) { clear(); }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000104
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000105 // Clear this DomainValue and point to next which has all its data.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000106 void clear() {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000107 AvailableDomains = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000108 Next = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000109 Instrs.clear();
110 }
111};
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000112}
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000113
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000114namespace {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000115/// LiveReg - Information about a live register.
116struct LiveReg {
117 /// Value currently in this register, or NULL when no value is being tracked.
118 /// This counts as a DomainValue reference.
119 DomainValue *Value;
120
121 /// Instruction that defined this register, relative to the beginning of the
122 /// current basic block. When a LiveReg is used to represent a live-out
123 /// register, this value is relative to the end of the basic block, so it
124 /// will be a negative number.
125 int Def;
126};
127} // anonynous namespace
128
129namespace {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000130class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000131 static char ID;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000132 SpecificBumpPtrAllocator<DomainValue> Allocator;
133 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000134
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000135 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000136 MachineFunction *MF;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000137 const TargetInstrInfo *TII;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000138 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000139 std::vector<int> AliasMap;
140 const unsigned NumRegs;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000141 LiveReg *LiveRegs;
142 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000143 LiveOutMap LiveOuts;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000144
Andrew Trickb6d56be2013-10-14 22:19:03 +0000145 /// List of undefined register reads in this block in forward order.
146 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads;
147
148 /// Storage for register unit liveness.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000149 LivePhysRegs LiveRegSet;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000150
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000151 /// Current instruction number.
152 /// The first instruction in each basic block is 0.
153 int CurInstr;
154
155 /// True when the current block has a predecessor that hasn't been visited
156 /// yet.
157 bool SeenUnknownBackEdge;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000158
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000159public:
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000160 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000161 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000162
Craig Topper4584cd52014-03-07 09:26:03 +0000163 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000164 AU.setPreservesAll();
165 MachineFunctionPass::getAnalysisUsage(AU);
166 }
167
Craig Topper4584cd52014-03-07 09:26:03 +0000168 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000169
Craig Topper4584cd52014-03-07 09:26:03 +0000170 const char *getPassName() const override {
Jakob Stoklund Olesenbaffa7d2011-11-07 21:23:39 +0000171 return "Execution dependency fix";
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000172 }
173
174private:
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000175 // Register mapping.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000176 int regIndex(unsigned Reg);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000177
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000178 // DomainValue allocation.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000179 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000180 DomainValue *retain(DomainValue *DV) {
181 if (DV) ++DV->Refs;
182 return DV;
183 }
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000184 void release(DomainValue*);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000185 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000186
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000187 // LiveRegs manipulations.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000188 void setLiveReg(int rx, DomainValue *DV);
189 void kill(int rx);
190 void force(int rx, unsigned domain);
191 void collapse(DomainValue *dv, unsigned domain);
192 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000193
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000194 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000195 void leaveBasicBlock(MachineBasicBlock*);
196 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000197 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000198 void visitSoftInstr(MachineInstr*, unsigned mask);
199 void visitHardInstr(MachineInstr*, unsigned domain);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000200 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
201 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000202};
203}
204
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000205char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000206
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000207/// Translate TRI register number to an index into our smaller tables of
208/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000209int ExeDepsFix::regIndex(unsigned Reg) {
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000210 assert(Reg < AliasMap.size() && "Invalid register");
211 return AliasMap[Reg];
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000212}
213
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000214DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000215 DomainValue *dv = Avail.empty() ?
216 new(Allocator.Allocate()) DomainValue :
217 Avail.pop_back_val();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000218 if (domain >= 0)
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000219 dv->addDomain(domain);
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000220 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000221 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000222 return dv;
223}
224
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000225/// release - Release a reference to DV. When the last reference is released,
226/// collapse if needed.
227void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000228 while (DV) {
229 assert(DV->Refs && "Bad DomainValue");
230 if (--DV->Refs)
231 return;
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000232
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000233 // There are no more DV references. Collapse any contained instructions.
234 if (DV->AvailableDomains && !DV->isCollapsed())
235 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000236
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000237 DomainValue *Next = DV->Next;
238 DV->clear();
239 Avail.push_back(DV);
240 // Also release the next DomainValue in the chain.
241 DV = Next;
242 }
243}
244
245/// resolve - Follow the chain of dead DomainValues until a live DomainValue is
246/// reached. Update the referenced pointer when necessary.
247DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
248 DomainValue *DV = DVRef;
249 if (!DV || !DV->Next)
250 return DV;
251
252 // DV has a chain. Find the end.
253 do DV = DV->Next;
254 while (DV->Next);
255
256 // Update DVRef to point to DV.
257 retain(DV);
258 release(DVRef);
259 DVRef = DV;
260 return DV;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000261}
262
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000263/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000264void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000265 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000266 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000267
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000268 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000269 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000270 if (LiveRegs[rx].Value)
271 release(LiveRegs[rx].Value);
272 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000273}
274
275// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000276void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000277 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000278 assert(LiveRegs && "Must enter basic block first.");
279 if (!LiveRegs[rx].Value)
280 return;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000281
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000282 release(LiveRegs[rx].Value);
Craig Topperc0196b12014-04-14 00:51:57 +0000283 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000284}
285
286/// Force register rx into domain.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000287void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000288 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000289 assert(LiveRegs && "Must enter basic block first.");
290 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000291 if (dv->isCollapsed())
292 dv->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000293 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000294 collapse(dv, domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000295 else {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000296 // This is an incompatible open DomainValue. Collapse it to whatever and
297 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000298 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000299 assert(LiveRegs[rx].Value && "Not live after collapse?");
300 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000301 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000302 } else {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000303 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000304 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000305 }
306}
307
308/// Collapse open DomainValue into given domain. If there are multiple
309/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000310void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000311 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000312
313 // Collapse all the instructions.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000314 while (!dv->Instrs.empty())
Jakob Stoklund Olesenb48c9942011-09-27 22:57:18 +0000315 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000316 dv->setSingleDomain(domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000317
318 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000319 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000320 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000321 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000322 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000323}
324
325/// Merge - All instructions and registers in B are moved to A, and B is
326/// released.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000327bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000328 assert(!A->isCollapsed() && "Cannot merge into collapsed");
329 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen58ca0a62010-03-31 20:05:12 +0000330 if (A == B)
Jakob Stoklund Olesen4cd58662010-03-31 17:13:16 +0000331 return true;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000332 // Restrict to the domains that A and B have in common.
333 unsigned common = A->getCommonDomains(B->AvailableDomains);
334 if (!common)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000335 return false;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000336 A->AvailableDomains = common;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000337 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000338
339 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000340 B->clear();
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000341 // All uses of B are referred to A.
342 B->Next = retain(A);
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000343
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000344 for (unsigned rx = 0; rx != NumRegs; ++rx) {
345 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000346 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000347 setLiveReg(rx, A);
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000348 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000349 return true;
350}
351
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000352// enterBasicBlock - Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000353void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000354 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000355 SeenUnknownBackEdge = false;
356
357 // Reset instruction counter in each basic block.
358 CurInstr = 0;
359
Andrew Trickb6d56be2013-10-14 22:19:03 +0000360 // Set up UndefReads to track undefined register reads.
361 UndefReads.clear();
Juergen Ributzka310034e2013-12-14 06:52:56 +0000362 LiveRegSet.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000363
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000364 // Set up LiveRegs to represent registers entering MBB.
365 if (!LiveRegs)
366 LiveRegs = new LiveReg[NumRegs];
367
368 // Default values are 'nothing happened a long time ago'.
369 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Craig Topperc0196b12014-04-14 00:51:57 +0000370 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000371 LiveRegs[rx].Def = -(1 << 20);
372 }
373
374 // This is the entry block.
375 if (MBB->pred_empty()) {
376 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
377 e = MBB->livein_end(); i != e; ++i) {
378 int rx = regIndex(*i);
379 if (rx < 0)
380 continue;
381 // Treat function live-ins as if they were defined just before the first
382 // instruction. Usually, function arguments are set up immediately
383 // before the call.
384 LiveRegs[rx].Def = -1;
385 }
386 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
387 return;
388 }
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000389
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000390 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000391 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
392 pe = MBB->pred_end(); pi != pe; ++pi) {
393 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
394 if (fi == LiveOuts.end()) {
395 SeenUnknownBackEdge = true;
396 continue;
397 }
398 assert(fi->second && "Can't have NULL entries");
399
400 for (unsigned rx = 0; rx != NumRegs; ++rx) {
401 // Use the most recent predecessor def for each register.
402 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
403
404 DomainValue *pdv = resolve(fi->second[rx].Value);
405 if (!pdv)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000406 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000407 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000408 setLiveReg(rx, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000409 continue;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000410 }
Chris Lattner503a0ef2010-03-31 20:32:51 +0000411
412 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000413 if (LiveRegs[rx].Value->isCollapsed()) {
Eric Christopher650c8f22014-05-20 17:11:11 +0000414 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000415 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
416 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
417 collapse(pdv, Domain);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000418 continue;
419 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000420
Chris Lattner503a0ef2010-03-31 20:32:51 +0000421 // Currently open, merge in predecessor.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000422 if (!pdv->isCollapsed())
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000423 merge(LiveRegs[rx].Value, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000424 else
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000425 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000426 }
427 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000428 DEBUG(dbgs() << "BB#" << MBB->getNumber()
429 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000430}
431
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000432void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000433 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000434 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000435 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000436 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
437
438 if (First) {
439 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
440 // the end of this block instead of the beginning.
441 for (unsigned i = 0, e = NumRegs; i != e; ++i)
442 LiveRegs[i].Def -= CurInstr;
443 } else {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000444 // Insertion failed, this must be the second pass.
445 // Release all the DomainValues instead of keeping them.
446 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000447 release(LiveRegs[i].Value);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000448 delete[] LiveRegs;
449 }
Craig Topperc0196b12014-04-14 00:51:57 +0000450 LiveRegs = nullptr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000451}
452
453void ExeDepsFix::visitInstr(MachineInstr *MI) {
454 if (MI->isDebugValue())
455 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000456
457 // Update instructions with explicit execution domains.
458 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(MI);
459 if (DomP.first) {
460 if (DomP.second)
461 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000462 else
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000463 visitHardInstr(MI, DomP.first);
464 }
465
466 // Process defs to track register ages, and kill values clobbered by generic
467 // instructions.
468 processDefs(MI, !DomP.first);
469}
470
Andrew Trickb6d56be2013-10-14 22:19:03 +0000471/// \brief Return true to if it makes sense to break dependence on a partial def
472/// or undef use.
473bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
474 unsigned Pref) {
475 int rx = regIndex(MI->getOperand(OpIdx).getReg());
476 if (rx < 0)
477 return false;
478
479 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
480 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
481
482 if (Pref > Clearance) {
483 DEBUG(dbgs() << ": Break dependency.\n");
484 return true;
485 }
486 // The current clearance seems OK, but we may be ignoring a def from a
487 // back-edge.
488 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
489 DEBUG(dbgs() << ": OK .\n");
490 return false;
491 }
492 // A def from an unprocessed back-edge may make us break this dependency.
493 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
494 return false;
495}
496
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000497// Update def-ages for registers defined by MI.
498// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000499//
500// Also break dependencies on partial defs and undef uses.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000501void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
502 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000503
504 // Break dependence on undef uses. Do this before updating LiveRegs below.
505 unsigned OpNum;
506 unsigned Pref = TII->getUndefRegClearance(MI, OpNum, TRI);
507 if (Pref) {
508 if (shouldBreakDependence(MI, OpNum, Pref))
509 UndefReads.push_back(std::make_pair(MI, OpNum));
510 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000511 const MCInstrDesc &MCID = MI->getDesc();
512 for (unsigned i = 0,
Evan Cheng7f8e5632011-12-07 07:15:52 +0000513 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000514 i != e; ++i) {
515 MachineOperand &MO = MI->getOperand(i);
516 if (!MO.isReg())
517 continue;
518 if (MO.isImplicit())
519 break;
520 if (MO.isUse())
521 continue;
522 int rx = regIndex(MO.getReg());
523 if (rx < 0)
524 continue;
525
526 // This instruction explicitly defines rx.
527 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
528 << '\t' << *MI);
529
Andrew Trickb6d56be2013-10-14 22:19:03 +0000530 // Check clearance before partial register updates.
531 // Call breakDependence before setting LiveRegs[rx].Def.
532 unsigned Pref = TII->getPartialRegUpdateClearance(MI, i, TRI);
533 if (Pref && shouldBreakDependence(MI, i, Pref))
534 TII->breakPartialRegDependency(MI, i, TRI);
535
Jakob Stoklund Olesenf8ad3362011-11-15 01:15:30 +0000536 // How many instructions since rx was last written?
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000537 LiveRegs[rx].Def = CurInstr;
538
539 // Kill off domains redefined by generic instructions.
540 if (Kill)
541 kill(rx);
542 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000543 ++CurInstr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000544}
545
Andrew Trickb6d56be2013-10-14 22:19:03 +0000546/// \break Break false dependencies on undefined register reads.
547///
548/// Walk the block backward computing precise liveness. This is expensive, so we
549/// only do it on demand. Note that the occurrence of undefined register reads
550/// that should be broken is very rare, but when they occur we may have many in
551/// a single block.
552void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
553 if (UndefReads.empty())
554 return;
555
556 // Collect this block's live out register units.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000557 LiveRegSet.init(TRI);
558 LiveRegSet.addLiveOuts(MBB);
559
Andrew Trickb6d56be2013-10-14 22:19:03 +0000560 MachineInstr *UndefMI = UndefReads.back().first;
561 unsigned OpIdx = UndefReads.back().second;
562
563 for (MachineBasicBlock::reverse_iterator I = MBB->rbegin(), E = MBB->rend();
564 I != E; ++I) {
Andrew Trick60cf0ad2013-12-13 22:23:54 +0000565 // Update liveness, including the current instruction's defs.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000566 LiveRegSet.stepBackward(*I);
Andrew Trick3a996932013-10-15 03:39:43 +0000567
Andrew Trickb6d56be2013-10-14 22:19:03 +0000568 if (UndefMI == &*I) {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000569 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Andrew Trickb6d56be2013-10-14 22:19:03 +0000570 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI);
571
572 UndefReads.pop_back();
573 if (UndefReads.empty())
574 return;
575
576 UndefMI = UndefReads.back().first;
577 OpIdx = UndefReads.back().second;
578 }
Andrew Trickb6d56be2013-10-14 22:19:03 +0000579 }
580}
581
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000582// A hard instruction only works in one domain. All input registers will be
583// forced into that domain.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000584void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000585 // Collapse all uses.
586 for (unsigned i = mi->getDesc().getNumDefs(),
587 e = mi->getDesc().getNumOperands(); i != e; ++i) {
588 MachineOperand &mo = mi->getOperand(i);
589 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000590 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000591 if (rx < 0) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000592 force(rx, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000593 }
594
595 // Kill all defs and force them.
596 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
597 MachineOperand &mo = mi->getOperand(i);
598 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000599 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000600 if (rx < 0) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000601 kill(rx);
602 force(rx, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000603 }
604}
605
606// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000607void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000608 // Bitmask of available domains for this instruction after taking collapsed
609 // operands into account.
610 unsigned available = mask;
611
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000612 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000613 SmallVector<int, 4> used;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000614 if (LiveRegs)
615 for (unsigned i = mi->getDesc().getNumDefs(),
616 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000617 MachineOperand &mo = mi->getOperand(i);
618 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000619 int rx = regIndex(mo.getReg());
Chris Lattner503a0ef2010-03-31 20:32:51 +0000620 if (rx < 0) continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000621 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000622 // Bitmask of domains that dv and available have in common.
623 unsigned common = dv->getCommonDomains(available);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000624 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000625 if (dv->isCollapsed()) {
626 // Restrict available domains to the ones in common with the operand.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000627 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000628 // penalty for this operand.
629 if (common) available = common;
630 } else if (common)
631 // Open DomainValue is compatible, save it for merging.
Chris Lattner503a0ef2010-03-31 20:32:51 +0000632 used.push_back(rx);
633 else
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000634 // Open DomainValue is not compatible with instruction. It is useless
635 // now.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000636 kill(rx);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000637 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000638 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000639
640 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000641 if (isPowerOf2_32(available)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000642 unsigned domain = countTrailingZeros(available);
Jakob Stoklund Olesenb48c9942011-09-27 22:57:18 +0000643 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000644 visitHardInstr(mi, domain);
645 return;
646 }
647
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000648 // Kill off any remaining uses that don't match available, and build a list of
649 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000650 SmallVector<LiveReg, 4> Regs;
Craig Toppere1c1d362013-07-03 05:11:49 +0000651 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000652 int rx = *i;
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000653 assert(LiveRegs && "no space allocated for live registers");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000654 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000655 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000656 if (!LR.Value->getCommonDomains(available)) {
657 kill(rx);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000658 continue;
659 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000660 // Sorted insertion.
661 bool Inserted = false;
Craig Toppere1c1d362013-07-03 05:11:49 +0000662 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000663 i != e && !Inserted; ++i) {
664 if (LR.Def < i->Def) {
665 Inserted = true;
666 Regs.insert(i, LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000667 }
668 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000669 if (!Inserted)
670 Regs.push_back(LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000671 }
672
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000673 // doms are now sorted in order of appearance. Try to merge them all, giving
674 // priority to the latest ones.
Craig Topperc0196b12014-04-14 00:51:57 +0000675 DomainValue *dv = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000676 while (!Regs.empty()) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000677 if (!dv) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000678 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000679 // Force the first dv to match the current instruction.
680 dv->AvailableDomains = dv->getCommonDomains(available);
681 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner503a0ef2010-03-31 20:32:51 +0000682 continue;
683 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000684
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000685 DomainValue *Latest = Regs.pop_back_val().Value;
686 // Skip already merged values.
687 if (Latest == dv || Latest->Next)
688 continue;
689 if (merge(dv, Latest))
690 continue;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000691
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000692 // If latest didn't merge, it is useless now. Kill all registers using it.
Michael Ilsemanaddddc42014-12-15 18:48:43 +0000693 for (int i : used) {
694 assert(LiveRegs && "no space allocated for live registers");
695 if (LiveRegs[i].Value == Latest)
696 kill(i);
697 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000698 }
699
700 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000701 if (!dv) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000702 dv = alloc();
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000703 dv->AvailableDomains = available;
704 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000705 dv->Instrs.push_back(mi);
706
Silviu Baranga3c314992012-10-03 08:29:36 +0000707 // Finally set all defs and non-collapsed uses to dv. We must iterate through
708 // all the operators, including imp-def ones.
709 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
710 ee = mi->operands_end();
711 ii != ee; ++ii) {
712 MachineOperand &mo = *ii;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000713 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000714 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000715 if (rx < 0) continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000716 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000717 kill(rx);
718 setLiveReg(rx, dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000719 }
720 }
721}
722
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000723bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000724 MF = &mf;
Eric Christopherfc6de422014-08-05 02:39:49 +0000725 TII = MF->getSubtarget().getInstrInfo();
726 TRI = MF->getSubtarget().getRegisterInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000727 LiveRegs = nullptr;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000728 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000729
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000730 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
Craig Toppercf0444b2014-11-17 05:50:14 +0000731 << TRI->getRegClassName(RC) << " **********\n");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000732
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000733 // If no relevant registers are used in the function, we can skip it
734 // completely.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000735 bool anyregs = false;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000736 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
737 I != E; ++I)
Jakob Stoklund Olesen07364422012-10-17 18:44:18 +0000738 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesen3588a432011-12-21 19:50:05 +0000739 anyregs = true;
740 break;
741 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000742 if (!anyregs) return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000743
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000744 // Initialize the AliasMap on the first use.
745 if (AliasMap.empty()) {
746 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
747 // or -1.
748 AliasMap.resize(TRI->getNumRegs(), -1);
749 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000750 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
751 AI.isValid(); ++AI)
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000752 AliasMap[*AI] = i;
753 }
754
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000755 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000756 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000757 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000758 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
759 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
760 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000761 enterBasicBlock(MBB);
762 if (SeenUnknownBackEdge)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000763 Loops.push_back(MBB);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000764 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000765 ++I)
766 visitInstr(I);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000767 processUndefReads(MBB);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000768 leaveBasicBlock(MBB);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000769 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000770
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000771 // Visit all the loop blocks again in order to merge DomainValues from
772 // back-edges.
773 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
774 MachineBasicBlock *MBB = Loops[i];
775 enterBasicBlock(MBB);
Jakob Stoklund Olesenf8ad3362011-11-15 01:15:30 +0000776 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
777 ++I)
778 if (!I->isDebugValue())
779 processDefs(I, false);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000780 processUndefReads(MBB);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000781 leaveBasicBlock(MBB);
782 }
783
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000784 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
785 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
786 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
787 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000788 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000789 continue;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000790 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000791 if (FI->second[i].Value)
792 release(FI->second[i].Value);
Jakob Stoklund Olesen5d082932011-11-08 22:05:17 +0000793 delete[] FI->second;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000794 }
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000795 LiveOuts.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000796 UndefReads.clear();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000797 Avail.clear();
798 Allocator.DestroyAll();
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000799
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000800 return false;
801}
802
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000803FunctionPass *
804llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000805 return new ExeDepsFix(RC);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000806}