blob: 9157d8a2abf4d6cabb0074d8319513016be732a8 [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"
32#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000033#include "llvm/Target/TargetSubtargetInfo.h"
34
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +000035using namespace llvm;
36
Chandler Carruth1b9dde02014-04-22 02:02:50 +000037#define DEBUG_TYPE "execution-fix"
38
Chris Lattner503a0ef2010-03-31 20:32:51 +000039/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000040/// of execution domains.
41///
42/// An open DomainValue represents a set of instructions that can still switch
43/// execution domain. Multiple registers may refer to the same open
44/// DomainValue - they will eventually be collapsed to the same execution
45/// domain.
46///
47/// A collapsed DomainValue represents a single register that has been forced
48/// into one of more execution domains. There is a separate collapsed
49/// DomainValue for each register, but it may contain multiple execution
50/// domains. A register value is initially created in a single execution
51/// domain, but if we were forced to pay the penalty of a domain crossing, we
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +000052/// keep track of the fact that the register is now available in multiple
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000053/// domains.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +000054namespace {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000055struct DomainValue {
56 // Basic reference counting.
57 unsigned Refs;
58
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000059 // Bitmask of available domains. For an open DomainValue, it is the still
60 // possible domains for collapsing. For a collapsed DomainValue it is the
61 // domains where the register is available for free.
62 unsigned AvailableDomains;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000063
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000064 // Pointer to the next DomainValue in a chain. When two DomainValues are
65 // merged, Victim.Next is set to point to Victor, so old DomainValue
Benjamin Kramerbde91762012-06-02 10:20:22 +000066 // references can be updated by following the chain.
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +000067 DomainValue *Next;
68
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000069 // Twiddleable instructions using or defining these registers.
70 SmallVector<MachineInstr*, 8> Instrs;
71
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000072 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000073 // track of the domains where the registers are already available.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000074 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000075
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000076 // Is domain available?
77 bool hasDomain(unsigned domain) const {
78 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000079 }
80
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000081 // Mark domain as available.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000082 void addDomain(unsigned domain) {
83 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +000084 }
85
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +000086 // Restrict to a single domain available.
87 void setSingleDomain(unsigned domain) {
88 AvailableDomains = 1u << domain;
89 }
90
91 // Return bitmask of domains that are available and in mask.
92 unsigned getCommonDomains(unsigned mask) const {
93 return AvailableDomains & mask;
94 }
95
96 // First domain available.
97 unsigned getFirstDomain() const {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +000098 return countTrailingZeros(AvailableDomains);
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +000099 }
100
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000101 DomainValue() : Refs(0) { clear(); }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000102
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000103 // Clear this DomainValue and point to next which has all its data.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000104 void clear() {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000105 AvailableDomains = 0;
Craig Topperc0196b12014-04-14 00:51:57 +0000106 Next = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000107 Instrs.clear();
108 }
109};
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000110}
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000111
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000112namespace {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000113/// LiveReg - Information about a live register.
114struct LiveReg {
115 /// Value currently in this register, or NULL when no value is being tracked.
116 /// This counts as a DomainValue reference.
117 DomainValue *Value;
118
119 /// Instruction that defined this register, relative to the beginning of the
120 /// current basic block. When a LiveReg is used to represent a live-out
121 /// register, this value is relative to the end of the basic block, so it
122 /// will be a negative number.
123 int Def;
124};
125} // anonynous namespace
126
127namespace {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000128class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000129 static char ID;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000130 SpecificBumpPtrAllocator<DomainValue> Allocator;
131 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000132
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000133 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000134 MachineFunction *MF;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000135 const TargetInstrInfo *TII;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000136 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000137 std::vector<int> AliasMap;
138 const unsigned NumRegs;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000139 LiveReg *LiveRegs;
140 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000141 LiveOutMap LiveOuts;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000142
Andrew Trickb6d56be2013-10-14 22:19:03 +0000143 /// List of undefined register reads in this block in forward order.
144 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads;
145
146 /// Storage for register unit liveness.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000147 LivePhysRegs LiveRegSet;
Andrew Trickb6d56be2013-10-14 22:19:03 +0000148
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000149 /// Current instruction number.
150 /// The first instruction in each basic block is 0.
151 int CurInstr;
152
153 /// True when the current block has a predecessor that hasn't been visited
154 /// yet.
155 bool SeenUnknownBackEdge;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000156
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000157public:
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000158 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000159 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000160
Craig Topper4584cd52014-03-07 09:26:03 +0000161 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000162 AU.setPreservesAll();
163 MachineFunctionPass::getAnalysisUsage(AU);
164 }
165
Craig Topper4584cd52014-03-07 09:26:03 +0000166 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000167
Craig Topper4584cd52014-03-07 09:26:03 +0000168 const char *getPassName() const override {
Jakob Stoklund Olesenbaffa7d2011-11-07 21:23:39 +0000169 return "Execution dependency fix";
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000170 }
171
172private:
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000173 // Register mapping.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000174 int regIndex(unsigned Reg);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000175
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000176 // DomainValue allocation.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000177 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000178 DomainValue *retain(DomainValue *DV) {
179 if (DV) ++DV->Refs;
180 return DV;
181 }
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000182 void release(DomainValue*);
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000183 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000184
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000185 // LiveRegs manipulations.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000186 void setLiveReg(int rx, DomainValue *DV);
187 void kill(int rx);
188 void force(int rx, unsigned domain);
189 void collapse(DomainValue *dv, unsigned domain);
190 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000191
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000192 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000193 void leaveBasicBlock(MachineBasicBlock*);
194 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000195 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000196 void visitSoftInstr(MachineInstr*, unsigned mask);
197 void visitHardInstr(MachineInstr*, unsigned domain);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000198 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
199 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000200};
201}
202
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000203char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000204
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000205/// Translate TRI register number to an index into our smaller tables of
206/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000207int ExeDepsFix::regIndex(unsigned Reg) {
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000208 assert(Reg < AliasMap.size() && "Invalid register");
209 return AliasMap[Reg];
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000210}
211
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000212DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000213 DomainValue *dv = Avail.empty() ?
214 new(Allocator.Allocate()) DomainValue :
215 Avail.pop_back_val();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000216 if (domain >= 0)
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000217 dv->addDomain(domain);
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000218 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000219 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000220 return dv;
221}
222
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000223/// release - Release a reference to DV. When the last reference is released,
224/// collapse if needed.
225void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000226 while (DV) {
227 assert(DV->Refs && "Bad DomainValue");
228 if (--DV->Refs)
229 return;
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000230
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000231 // There are no more DV references. Collapse any contained instructions.
232 if (DV->AvailableDomains && !DV->isCollapsed())
233 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen1438e192011-11-08 21:57:44 +0000234
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000235 DomainValue *Next = DV->Next;
236 DV->clear();
237 Avail.push_back(DV);
238 // Also release the next DomainValue in the chain.
239 DV = Next;
240 }
241}
242
243/// resolve - Follow the chain of dead DomainValues until a live DomainValue is
244/// reached. Update the referenced pointer when necessary.
245DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
246 DomainValue *DV = DVRef;
247 if (!DV || !DV->Next)
248 return DV;
249
250 // DV has a chain. Find the end.
251 do DV = DV->Next;
252 while (DV->Next);
253
254 // Update DVRef to point to DV.
255 retain(DV);
256 release(DVRef);
257 DVRef = DV;
258 return DV;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000259}
260
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000261/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000262void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000263 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000264 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000265
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000266 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000267 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000268 if (LiveRegs[rx].Value)
269 release(LiveRegs[rx].Value);
270 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000271}
272
273// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000274void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000275 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000276 assert(LiveRegs && "Must enter basic block first.");
277 if (!LiveRegs[rx].Value)
278 return;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000279
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000280 release(LiveRegs[rx].Value);
Craig Topperc0196b12014-04-14 00:51:57 +0000281 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000282}
283
284/// Force register rx into domain.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000285void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000286 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000287 assert(LiveRegs && "Must enter basic block first.");
288 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000289 if (dv->isCollapsed())
290 dv->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000291 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000292 collapse(dv, domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000293 else {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000294 // This is an incompatible open DomainValue. Collapse it to whatever and
295 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000296 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000297 assert(LiveRegs[rx].Value && "Not live after collapse?");
298 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen41051a02010-04-06 19:48:56 +0000299 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000300 } else {
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000301 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000302 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000303 }
304}
305
306/// Collapse open DomainValue into given domain. If there are multiple
307/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000308void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000309 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000310
311 // Collapse all the instructions.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000312 while (!dv->Instrs.empty())
Jakob Stoklund Olesenb48c9942011-09-27 22:57:18 +0000313 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000314 dv->setSingleDomain(domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000315
316 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000317 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000318 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000319 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000320 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000321}
322
323/// Merge - All instructions and registers in B are moved to A, and B is
324/// released.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000325bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000326 assert(!A->isCollapsed() && "Cannot merge into collapsed");
327 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen58ca0a62010-03-31 20:05:12 +0000328 if (A == B)
Jakob Stoklund Olesen4cd58662010-03-31 17:13:16 +0000329 return true;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000330 // Restrict to the domains that A and B have in common.
331 unsigned common = A->getCommonDomains(B->AvailableDomains);
332 if (!common)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000333 return false;
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000334 A->AvailableDomains = common;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000335 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000336
337 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesenb7e44a32011-11-08 23:26:00 +0000338 B->clear();
Jakob Stoklund Olesen53ec9772011-11-09 00:06:18 +0000339 // All uses of B are referred to A.
340 B->Next = retain(A);
Jakob Stoklund Olesen12058812011-11-08 20:57:04 +0000341
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000342 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000343 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000344 setLiveReg(rx, A);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000345 return true;
346}
347
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000348// enterBasicBlock - Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000349void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000350 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000351 SeenUnknownBackEdge = false;
352
353 // Reset instruction counter in each basic block.
354 CurInstr = 0;
355
Andrew Trickb6d56be2013-10-14 22:19:03 +0000356 // Set up UndefReads to track undefined register reads.
357 UndefReads.clear();
Juergen Ributzka310034e2013-12-14 06:52:56 +0000358 LiveRegSet.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000359
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000360 // Set up LiveRegs to represent registers entering MBB.
361 if (!LiveRegs)
362 LiveRegs = new LiveReg[NumRegs];
363
364 // Default values are 'nothing happened a long time ago'.
365 for (unsigned rx = 0; rx != NumRegs; ++rx) {
Craig Topperc0196b12014-04-14 00:51:57 +0000366 LiveRegs[rx].Value = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000367 LiveRegs[rx].Def = -(1 << 20);
368 }
369
370 // This is the entry block.
371 if (MBB->pred_empty()) {
372 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
373 e = MBB->livein_end(); i != e; ++i) {
374 int rx = regIndex(*i);
375 if (rx < 0)
376 continue;
377 // Treat function live-ins as if they were defined just before the first
378 // instruction. Usually, function arguments are set up immediately
379 // before the call.
380 LiveRegs[rx].Def = -1;
381 }
382 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
383 return;
384 }
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000385
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000386 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000387 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
388 pe = MBB->pred_end(); pi != pe; ++pi) {
389 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
390 if (fi == LiveOuts.end()) {
391 SeenUnknownBackEdge = true;
392 continue;
393 }
394 assert(fi->second && "Can't have NULL entries");
395
396 for (unsigned rx = 0; rx != NumRegs; ++rx) {
397 // Use the most recent predecessor def for each register.
398 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
399
400 DomainValue *pdv = resolve(fi->second[rx].Value);
401 if (!pdv)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000402 continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000403 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000404 setLiveReg(rx, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000405 continue;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000406 }
Chris Lattner503a0ef2010-03-31 20:32:51 +0000407
408 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000409 if (LiveRegs[rx].Value->isCollapsed()) {
Eric Christopher650c8f22014-05-20 17:11:11 +0000410 // We are already collapsed, but predecessor is not. Force it.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000411 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
412 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
413 collapse(pdv, Domain);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000414 continue;
415 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000416
Chris Lattner503a0ef2010-03-31 20:32:51 +0000417 // Currently open, merge in predecessor.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000418 if (!pdv->isCollapsed())
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000419 merge(LiveRegs[rx].Value, pdv);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000420 else
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000421 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000422 }
423 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000424 DEBUG(dbgs() << "BB#" << MBB->getNumber()
425 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000426}
427
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000428void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000429 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000430 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000431 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000432 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
433
434 if (First) {
435 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
436 // the end of this block instead of the beginning.
437 for (unsigned i = 0, e = NumRegs; i != e; ++i)
438 LiveRegs[i].Def -= CurInstr;
439 } else {
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000440 // Insertion failed, this must be the second pass.
441 // Release all the DomainValues instead of keeping them.
442 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000443 release(LiveRegs[i].Value);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000444 delete[] LiveRegs;
445 }
Craig Topperc0196b12014-04-14 00:51:57 +0000446 LiveRegs = nullptr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000447}
448
449void ExeDepsFix::visitInstr(MachineInstr *MI) {
450 if (MI->isDebugValue())
451 return;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000452
453 // Update instructions with explicit execution domains.
454 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(MI);
455 if (DomP.first) {
456 if (DomP.second)
457 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000458 else
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000459 visitHardInstr(MI, DomP.first);
460 }
461
462 // Process defs to track register ages, and kill values clobbered by generic
463 // instructions.
464 processDefs(MI, !DomP.first);
465}
466
Andrew Trickb6d56be2013-10-14 22:19:03 +0000467/// \brief Return true to if it makes sense to break dependence on a partial def
468/// or undef use.
469bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
470 unsigned Pref) {
471 int rx = regIndex(MI->getOperand(OpIdx).getReg());
472 if (rx < 0)
473 return false;
474
475 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
476 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
477
478 if (Pref > Clearance) {
479 DEBUG(dbgs() << ": Break dependency.\n");
480 return true;
481 }
482 // The current clearance seems OK, but we may be ignoring a def from a
483 // back-edge.
484 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
485 DEBUG(dbgs() << ": OK .\n");
486 return false;
487 }
488 // A def from an unprocessed back-edge may make us break this dependency.
489 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
490 return false;
491}
492
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000493// Update def-ages for registers defined by MI.
494// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000495//
496// Also break dependencies on partial defs and undef uses.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000497void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
498 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Trickb6d56be2013-10-14 22:19:03 +0000499
500 // Break dependence on undef uses. Do this before updating LiveRegs below.
501 unsigned OpNum;
502 unsigned Pref = TII->getUndefRegClearance(MI, OpNum, TRI);
503 if (Pref) {
504 if (shouldBreakDependence(MI, OpNum, Pref))
505 UndefReads.push_back(std::make_pair(MI, OpNum));
506 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000507 const MCInstrDesc &MCID = MI->getDesc();
508 for (unsigned i = 0,
Evan Cheng7f8e5632011-12-07 07:15:52 +0000509 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000510 i != e; ++i) {
511 MachineOperand &MO = MI->getOperand(i);
512 if (!MO.isReg())
513 continue;
514 if (MO.isImplicit())
515 break;
516 if (MO.isUse())
517 continue;
518 int rx = regIndex(MO.getReg());
519 if (rx < 0)
520 continue;
521
522 // This instruction explicitly defines rx.
523 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
524 << '\t' << *MI);
525
Andrew Trickb6d56be2013-10-14 22:19:03 +0000526 // Check clearance before partial register updates.
527 // Call breakDependence before setting LiveRegs[rx].Def.
528 unsigned Pref = TII->getPartialRegUpdateClearance(MI, i, TRI);
529 if (Pref && shouldBreakDependence(MI, i, Pref))
530 TII->breakPartialRegDependency(MI, i, TRI);
531
Jakob Stoklund Olesenf8ad3362011-11-15 01:15:30 +0000532 // How many instructions since rx was last written?
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000533 LiveRegs[rx].Def = CurInstr;
534
535 // Kill off domains redefined by generic instructions.
536 if (Kill)
537 kill(rx);
538 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000539 ++CurInstr;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000540}
541
Andrew Trickb6d56be2013-10-14 22:19:03 +0000542/// \break Break false dependencies on undefined register reads.
543///
544/// Walk the block backward computing precise liveness. This is expensive, so we
545/// only do it on demand. Note that the occurrence of undefined register reads
546/// that should be broken is very rare, but when they occur we may have many in
547/// a single block.
548void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
549 if (UndefReads.empty())
550 return;
551
552 // Collect this block's live out register units.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000553 LiveRegSet.init(TRI);
554 LiveRegSet.addLiveOuts(MBB);
555
Andrew Trickb6d56be2013-10-14 22:19:03 +0000556 MachineInstr *UndefMI = UndefReads.back().first;
557 unsigned OpIdx = UndefReads.back().second;
558
559 for (MachineBasicBlock::reverse_iterator I = MBB->rbegin(), E = MBB->rend();
560 I != E; ++I) {
Andrew Trick60cf0ad2013-12-13 22:23:54 +0000561 // Update liveness, including the current instruction's defs.
Juergen Ributzka310034e2013-12-14 06:52:56 +0000562 LiveRegSet.stepBackward(*I);
Andrew Trick3a996932013-10-15 03:39:43 +0000563
Andrew Trickb6d56be2013-10-14 22:19:03 +0000564 if (UndefMI == &*I) {
Juergen Ributzka310034e2013-12-14 06:52:56 +0000565 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Andrew Trickb6d56be2013-10-14 22:19:03 +0000566 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI);
567
568 UndefReads.pop_back();
569 if (UndefReads.empty())
570 return;
571
572 UndefMI = UndefReads.back().first;
573 OpIdx = UndefReads.back().second;
574 }
Andrew Trickb6d56be2013-10-14 22:19:03 +0000575 }
576}
577
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000578// A hard instruction only works in one domain. All input registers will be
579// forced into that domain.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000580void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000581 // Collapse all uses.
582 for (unsigned i = mi->getDesc().getNumDefs(),
583 e = mi->getDesc().getNumOperands(); i != e; ++i) {
584 MachineOperand &mo = mi->getOperand(i);
585 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000586 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000587 if (rx < 0) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000588 force(rx, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000589 }
590
591 // Kill all defs and force them.
592 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
593 MachineOperand &mo = mi->getOperand(i);
594 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000595 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000596 if (rx < 0) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000597 kill(rx);
598 force(rx, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000599 }
600}
601
602// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000603void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000604 // Bitmask of available domains for this instruction after taking collapsed
605 // operands into account.
606 unsigned available = mask;
607
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000608 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000609 SmallVector<int, 4> used;
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000610 if (LiveRegs)
611 for (unsigned i = mi->getDesc().getNumDefs(),
612 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000613 MachineOperand &mo = mi->getOperand(i);
614 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000615 int rx = regIndex(mo.getReg());
Chris Lattner503a0ef2010-03-31 20:32:51 +0000616 if (rx < 0) continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000617 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000618 // Bitmask of domains that dv and available have in common.
619 unsigned common = dv->getCommonDomains(available);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000620 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000621 if (dv->isCollapsed()) {
622 // Restrict available domains to the ones in common with the operand.
Andrew Trickb6d56be2013-10-14 22:19:03 +0000623 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000624 // penalty for this operand.
625 if (common) available = common;
626 } else if (common)
627 // Open DomainValue is compatible, save it for merging.
Chris Lattner503a0ef2010-03-31 20:32:51 +0000628 used.push_back(rx);
629 else
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000630 // Open DomainValue is not compatible with instruction. It is useless
631 // now.
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000632 kill(rx);
Chris Lattner503a0ef2010-03-31 20:32:51 +0000633 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000634 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000635
636 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000637 if (isPowerOf2_32(available)) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000638 unsigned domain = countTrailingZeros(available);
Jakob Stoklund Olesenb48c9942011-09-27 22:57:18 +0000639 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000640 visitHardInstr(mi, domain);
641 return;
642 }
643
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000644 // Kill off any remaining uses that don't match available, and build a list of
645 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000646 SmallVector<LiveReg, 4> Regs;
Craig Toppere1c1d362013-07-03 05:11:49 +0000647 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000648 int rx = *i;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000649 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000650 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000651 if (!LR.Value->getCommonDomains(available)) {
652 kill(rx);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000653 continue;
654 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000655 // Sorted insertion.
656 bool Inserted = false;
Craig Toppere1c1d362013-07-03 05:11:49 +0000657 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000658 i != e && !Inserted; ++i) {
659 if (LR.Def < i->Def) {
660 Inserted = true;
661 Regs.insert(i, LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000662 }
663 }
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000664 if (!Inserted)
665 Regs.push_back(LR);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000666 }
667
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000668 // doms are now sorted in order of appearance. Try to merge them all, giving
669 // priority to the latest ones.
Craig Topperc0196b12014-04-14 00:51:57 +0000670 DomainValue *dv = nullptr;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000671 while (!Regs.empty()) {
Chris Lattner503a0ef2010-03-31 20:32:51 +0000672 if (!dv) {
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000673 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000674 // Force the first dv to match the current instruction.
675 dv->AvailableDomains = dv->getCommonDomains(available);
676 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner503a0ef2010-03-31 20:32:51 +0000677 continue;
678 }
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000679
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000680 DomainValue *Latest = Regs.pop_back_val().Value;
681 // Skip already merged values.
682 if (Latest == dv || Latest->Next)
683 continue;
684 if (merge(dv, Latest))
685 continue;
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000686
Jakob Stoklund Olesend03ac952010-04-04 21:27:26 +0000687 // If latest didn't merge, it is useless now. Kill all registers using it.
Craig Toppere1c1d362013-07-03 05:11:49 +0000688 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000689 if (LiveRegs[*i].Value == Latest)
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000690 kill(*i);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000691 }
692
693 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000694 if (!dv) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000695 dv = alloc();
Jakob Stoklund Olesen02845412011-11-23 04:03:08 +0000696 dv->AvailableDomains = available;
697 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000698 dv->Instrs.push_back(mi);
699
Silviu Baranga3c314992012-10-03 08:29:36 +0000700 // Finally set all defs and non-collapsed uses to dv. We must iterate through
701 // all the operators, including imp-def ones.
702 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
703 ee = mi->operands_end();
704 ii != ee; ++ii) {
705 MachineOperand &mo = *ii;
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000706 if (!mo.isReg()) continue;
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000707 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000708 if (rx < 0) continue;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000709 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
Jakob Stoklund Olesen9e338bb2011-11-08 21:57:47 +0000710 kill(rx);
711 setLiveReg(rx, dv);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000712 }
713 }
714}
715
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000716bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000717 MF = &mf;
Eric Christopherd9134482014-08-04 21:25:23 +0000718 TII = MF->getTarget().getSubtargetImpl()->getInstrInfo();
719 TRI = MF->getTarget().getSubtargetImpl()->getRegisterInfo();
Craig Topperc0196b12014-04-14 00:51:57 +0000720 LiveRegs = nullptr;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000721 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000722
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000723 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
724 << RC->getName() << " **********\n");
725
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000726 // If no relevant registers are used in the function, we can skip it
727 // completely.
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000728 bool anyregs = false;
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000729 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
730 I != E; ++I)
Jakob Stoklund Olesen07364422012-10-17 18:44:18 +0000731 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesen3588a432011-12-21 19:50:05 +0000732 anyregs = true;
733 break;
734 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000735 if (!anyregs) return false;
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000736
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000737 // Initialize the AliasMap on the first use.
738 if (AliasMap.empty()) {
739 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
740 // or -1.
741 AliasMap.resize(TRI->getNumRegs(), -1);
742 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000743 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
744 AI.isValid(); ++AI)
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000745 AliasMap[*AI] = i;
746 }
747
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000748 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000749 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000750 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesen68e197e2011-11-07 21:59:29 +0000751 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
752 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
753 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000754 enterBasicBlock(MBB);
755 if (SeenUnknownBackEdge)
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000756 Loops.push_back(MBB);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000757 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000758 ++I)
759 visitInstr(I);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000760 processUndefReads(MBB);
Jakob Stoklund Olesen736cf462011-11-07 21:40:27 +0000761 leaveBasicBlock(MBB);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000762 }
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000763
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000764 // Visit all the loop blocks again in order to merge DomainValues from
765 // back-edges.
766 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
767 MachineBasicBlock *MBB = Loops[i];
768 enterBasicBlock(MBB);
Jakob Stoklund Olesenf8ad3362011-11-15 01:15:30 +0000769 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
770 ++I)
771 if (!I->isDebugValue())
772 processDefs(I, false);
Andrew Trickb6d56be2013-10-14 22:19:03 +0000773 processUndefReads(MBB);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000774 leaveBasicBlock(MBB);
775 }
776
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000777 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
778 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
779 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
780 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesen3dc89c92011-11-09 01:06:56 +0000781 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000782 continue;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000783 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen543bef62011-11-15 01:15:25 +0000784 if (FI->second[i].Value)
785 release(FI->second[i].Value);
Jakob Stoklund Olesen5d082932011-11-08 22:05:17 +0000786 delete[] FI->second;
Jakob Stoklund Olesena70e9412011-11-07 23:08:21 +0000787 }
Jakob Stoklund Olesen3b9af402010-03-30 20:04:01 +0000788 LiveOuts.clear();
Andrew Trickb6d56be2013-10-14 22:19:03 +0000789 UndefReads.clear();
Jakob Stoklund Olesen42caaa42010-04-04 18:00:21 +0000790 Avail.clear();
791 Allocator.DestroyAll();
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +0000792
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000793 return false;
794}
795
Jakob Stoklund Olesen30c81122011-09-27 23:50:46 +0000796FunctionPass *
797llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesenbd5109f2011-09-28 00:01:56 +0000798 return new ExeDepsFix(RC);
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +0000799}