blob: a08eb6b5276947866465988f2c376e35ae1c7c95 [file] [log] [blame]
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +00001//===- ExecutionDepsFix.cpp - Fix execution dependecy issues ----*- C++ -*-===//
Jakob Stoklund Olesen352aa502010-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 Olesen56ab8752011-09-28 00:01:56 +000010// This file contains the execution dependency fix pass.
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000011//
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +000012// Some X86 SSE instructions like mov, and, or, xor are available in different
Jakob Stoklund Olesen352aa502010-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 Olesen56ab8752011-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 Olesen352aa502010-03-25 17:25:00 +000018//
19// This pass changes the variant instructions to minimize domain crossings.
20//
21//===----------------------------------------------------------------------===//
22
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +000023#define DEBUG_TYPE "execution-fix"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/CodeGen/Passes.h"
25#include "llvm/ADT/PostOrderIterator.h"
Stephen Hines36b56882014-04-23 16:57:46 -070026#include "llvm/CodeGen/LivePhysRegs.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000029#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000032#include "llvm/Target/TargetInstrInfo.h"
33#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000034using namespace llvm;
35
Chris Lattner563d83f2010-03-31 20:32:51 +000036/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000037/// of execution domains.
38///
39/// An open DomainValue represents a set of instructions that can still switch
40/// execution domain. Multiple registers may refer to the same open
41/// DomainValue - they will eventually be collapsed to the same execution
42/// domain.
43///
44/// A collapsed DomainValue represents a single register that has been forced
45/// into one of more execution domains. There is a separate collapsed
46/// DomainValue for each register, but it may contain multiple execution
47/// domains. A register value is initially created in a single execution
48/// domain, but if we were forced to pay the penalty of a domain crossing, we
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +000049/// keep track of the fact that the register is now available in multiple
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000050/// domains.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000051namespace {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000052struct DomainValue {
53 // Basic reference counting.
54 unsigned Refs;
55
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000056 // Bitmask of available domains. For an open DomainValue, it is the still
57 // possible domains for collapsing. For a collapsed DomainValue it is the
58 // domains where the register is available for free.
59 unsigned AvailableDomains;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000060
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +000061 // Pointer to the next DomainValue in a chain. When two DomainValues are
62 // merged, Victim.Next is set to point to Victor, so old DomainValue
Benjamin Kramerd9b0b022012-06-02 10:20:22 +000063 // references can be updated by following the chain.
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +000064 DomainValue *Next;
65
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000066 // Twiddleable instructions using or defining these registers.
67 SmallVector<MachineInstr*, 8> Instrs;
68
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000069 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000070 // track of the domains where the registers are already available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000071 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000072
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000073 // Is domain available?
74 bool hasDomain(unsigned domain) const {
75 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000076 }
77
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000078 // Mark domain as available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000079 void addDomain(unsigned domain) {
80 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000081 }
82
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000083 // Restrict to a single domain available.
84 void setSingleDomain(unsigned domain) {
85 AvailableDomains = 1u << domain;
86 }
87
88 // Return bitmask of domains that are available and in mask.
89 unsigned getCommonDomains(unsigned mask) const {
90 return AvailableDomains & mask;
91 }
92
93 // First domain available.
94 unsigned getFirstDomain() const {
Michael J. Spencerc6af2432013-05-24 22:23:49 +000095 return countTrailingZeros(AvailableDomains);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000096 }
97
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +000098 DomainValue() : Refs(0) { clear(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000099
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000100 // Clear this DomainValue and point to next which has all its data.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000101 void clear() {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000102 AvailableDomains = 0;
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000103 Next = 0;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000104 Instrs.clear();
105 }
106};
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000107}
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000108
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000109namespace {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000110/// LiveReg - Information about a live register.
111struct LiveReg {
112 /// Value currently in this register, or NULL when no value is being tracked.
113 /// This counts as a DomainValue reference.
114 DomainValue *Value;
115
116 /// Instruction that defined this register, relative to the beginning of the
117 /// current basic block. When a LiveReg is used to represent a live-out
118 /// register, this value is relative to the end of the basic block, so it
119 /// will be a negative number.
120 int Def;
121};
122} // anonynous namespace
123
124namespace {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000125class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000126 static char ID;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000127 SpecificBumpPtrAllocator<DomainValue> Allocator;
128 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000129
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000130 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000131 MachineFunction *MF;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000132 const TargetInstrInfo *TII;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000133 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000134 std::vector<int> AliasMap;
135 const unsigned NumRegs;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000136 LiveReg *LiveRegs;
137 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000138 LiveOutMap LiveOuts;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000139
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000140 /// List of undefined register reads in this block in forward order.
141 std::vector<std::pair<MachineInstr*, unsigned> > UndefReads;
142
143 /// Storage for register unit liveness.
Stephen Hines36b56882014-04-23 16:57:46 -0700144 LivePhysRegs LiveRegSet;
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000145
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000146 /// Current instruction number.
147 /// The first instruction in each basic block is 0.
148 int CurInstr;
149
150 /// True when the current block has a predecessor that hasn't been visited
151 /// yet.
152 bool SeenUnknownBackEdge;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000153
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000154public:
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000155 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000156 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000157
Stephen Hines36b56882014-04-23 16:57:46 -0700158 void getAnalysisUsage(AnalysisUsage &AU) const override {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000159 AU.setPreservesAll();
160 MachineFunctionPass::getAnalysisUsage(AU);
161 }
162
Stephen Hines36b56882014-04-23 16:57:46 -0700163 bool runOnMachineFunction(MachineFunction &MF) override;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000164
Stephen Hines36b56882014-04-23 16:57:46 -0700165 const char *getPassName() const override {
Jakob Stoklund Olesencd7dcad2011-11-07 21:23:39 +0000166 return "Execution dependency fix";
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000167 }
168
169private:
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000170 // Register mapping.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000171 int regIndex(unsigned Reg);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000172
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000173 // DomainValue allocation.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000174 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000175 DomainValue *retain(DomainValue *DV) {
176 if (DV) ++DV->Refs;
177 return DV;
178 }
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000179 void release(DomainValue*);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000180 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000181
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000182 // LiveRegs manipulations.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000183 void setLiveReg(int rx, DomainValue *DV);
184 void kill(int rx);
185 void force(int rx, unsigned domain);
186 void collapse(DomainValue *dv, unsigned domain);
187 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000188
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000189 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000190 void leaveBasicBlock(MachineBasicBlock*);
191 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000192 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000193 void visitSoftInstr(MachineInstr*, unsigned mask);
194 void visitHardInstr(MachineInstr*, unsigned domain);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000195 bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref);
196 void processUndefReads(MachineBasicBlock*);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000197};
198}
199
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000200char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000201
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000202/// Translate TRI register number to an index into our smaller tables of
203/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000204int ExeDepsFix::regIndex(unsigned Reg) {
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000205 assert(Reg < AliasMap.size() && "Invalid register");
206 return AliasMap[Reg];
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000207}
208
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000209DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000210 DomainValue *dv = Avail.empty() ?
211 new(Allocator.Allocate()) DomainValue :
212 Avail.pop_back_val();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000213 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000214 dv->addDomain(domain);
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000215 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000216 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000217 return dv;
218}
219
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000220/// release - Release a reference to DV. When the last reference is released,
221/// collapse if needed.
222void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000223 while (DV) {
224 assert(DV->Refs && "Bad DomainValue");
225 if (--DV->Refs)
226 return;
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000227
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000228 // There are no more DV references. Collapse any contained instructions.
229 if (DV->AvailableDomains && !DV->isCollapsed())
230 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000231
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000232 DomainValue *Next = DV->Next;
233 DV->clear();
234 Avail.push_back(DV);
235 // Also release the next DomainValue in the chain.
236 DV = Next;
237 }
238}
239
240/// resolve - Follow the chain of dead DomainValues until a live DomainValue is
241/// reached. Update the referenced pointer when necessary.
242DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
243 DomainValue *DV = DVRef;
244 if (!DV || !DV->Next)
245 return DV;
246
247 // DV has a chain. Find the end.
248 do DV = DV->Next;
249 while (DV->Next);
250
251 // Update DVRef to point to DV.
252 retain(DV);
253 release(DVRef);
254 DVRef = DV;
255 return DV;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000256}
257
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000258/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000259void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000260 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000261 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000262
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000263 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000264 return;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000265 if (LiveRegs[rx].Value)
266 release(LiveRegs[rx].Value);
267 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000268}
269
270// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000271void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000272 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000273 assert(LiveRegs && "Must enter basic block first.");
274 if (!LiveRegs[rx].Value)
275 return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000276
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000277 release(LiveRegs[rx].Value);
278 LiveRegs[rx].Value = 0;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000279}
280
281/// Force register rx into domain.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000282void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000283 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000284 assert(LiveRegs && "Must enter basic block first.");
285 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000286 if (dv->isCollapsed())
287 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000288 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000289 collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000290 else {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000291 // This is an incompatible open DomainValue. Collapse it to whatever and
292 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000293 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000294 assert(LiveRegs[rx].Value && "Not live after collapse?");
295 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000296 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000297 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000298 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000299 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000300 }
301}
302
303/// Collapse open DomainValue into given domain. If there are multiple
304/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000305void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000306 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000307
308 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000309 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000310 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000311 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000312
313 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000314 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000315 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000316 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000317 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000318}
319
320/// Merge - All instructions and registers in B are moved to A, and B is
321/// released.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000322bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000323 assert(!A->isCollapsed() && "Cannot merge into collapsed");
324 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000325 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000326 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000327 // Restrict to the domains that A and B have in common.
328 unsigned common = A->getCommonDomains(B->AvailableDomains);
329 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000330 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000331 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000332 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000333
334 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000335 B->clear();
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000336 // All uses of B are referred to A.
337 B->Next = retain(A);
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000338
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000339 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000340 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000341 setLiveReg(rx, A);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000342 return true;
343}
344
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000345// enterBasicBlock - Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000346void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000347 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000348 SeenUnknownBackEdge = false;
349
350 // Reset instruction counter in each basic block.
351 CurInstr = 0;
352
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000353 // Set up UndefReads to track undefined register reads.
354 UndefReads.clear();
Stephen Hines36b56882014-04-23 16:57:46 -0700355 LiveRegSet.clear();
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000356
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000357 // Set up LiveRegs to represent registers entering MBB.
358 if (!LiveRegs)
359 LiveRegs = new LiveReg[NumRegs];
360
361 // Default values are 'nothing happened a long time ago'.
362 for (unsigned rx = 0; rx != NumRegs; ++rx) {
363 LiveRegs[rx].Value = 0;
364 LiveRegs[rx].Def = -(1 << 20);
365 }
366
367 // This is the entry block.
368 if (MBB->pred_empty()) {
369 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
370 e = MBB->livein_end(); i != e; ++i) {
371 int rx = regIndex(*i);
372 if (rx < 0)
373 continue;
374 // Treat function live-ins as if they were defined just before the first
375 // instruction. Usually, function arguments are set up immediately
376 // before the call.
377 LiveRegs[rx].Def = -1;
378 }
379 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
380 return;
381 }
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000382
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000383 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000384 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
385 pe = MBB->pred_end(); pi != pe; ++pi) {
386 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
387 if (fi == LiveOuts.end()) {
388 SeenUnknownBackEdge = true;
389 continue;
390 }
391 assert(fi->second && "Can't have NULL entries");
392
393 for (unsigned rx = 0; rx != NumRegs; ++rx) {
394 // Use the most recent predecessor def for each register.
395 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
396
397 DomainValue *pdv = resolve(fi->second[rx].Value);
398 if (!pdv)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000399 continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000400 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000401 setLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000402 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000403 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000404
405 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000406 if (LiveRegs[rx].Value->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000407 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000408 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
409 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
410 collapse(pdv, Domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000411 continue;
412 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000413
Chris Lattner563d83f2010-03-31 20:32:51 +0000414 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000415 if (!pdv->isCollapsed())
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000416 merge(LiveRegs[rx].Value, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000417 else
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000418 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000419 }
420 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000421 DEBUG(dbgs() << "BB#" << MBB->getNumber()
422 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000423}
424
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000425void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000426 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000427 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000428 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000429 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
430
431 if (First) {
432 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
433 // the end of this block instead of the beginning.
434 for (unsigned i = 0, e = NumRegs; i != e; ++i)
435 LiveRegs[i].Def -= CurInstr;
436 } else {
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000437 // Insertion failed, this must be the second pass.
438 // Release all the DomainValues instead of keeping them.
439 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000440 release(LiveRegs[i].Value);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000441 delete[] LiveRegs;
442 }
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000443 LiveRegs = 0;
444}
445
446void ExeDepsFix::visitInstr(MachineInstr *MI) {
447 if (MI->isDebugValue())
448 return;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000449
450 // Update instructions with explicit execution domains.
451 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(MI);
452 if (DomP.first) {
453 if (DomP.second)
454 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000455 else
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000456 visitHardInstr(MI, DomP.first);
457 }
458
459 // Process defs to track register ages, and kill values clobbered by generic
460 // instructions.
461 processDefs(MI, !DomP.first);
462}
463
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000464/// \brief Return true to if it makes sense to break dependence on a partial def
465/// or undef use.
466bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx,
467 unsigned Pref) {
468 int rx = regIndex(MI->getOperand(OpIdx).getReg());
469 if (rx < 0)
470 return false;
471
472 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
473 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
474
475 if (Pref > Clearance) {
476 DEBUG(dbgs() << ": Break dependency.\n");
477 return true;
478 }
479 // The current clearance seems OK, but we may be ignoring a def from a
480 // back-edge.
481 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
482 DEBUG(dbgs() << ": OK .\n");
483 return false;
484 }
485 // A def from an unprocessed back-edge may make us break this dependency.
486 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
487 return false;
488}
489
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000490// Update def-ages for registers defined by MI.
491// If Kill is set, also kill off DomainValues clobbered by the defs.
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000492//
493// Also break dependencies on partial defs and undef uses.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000494void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
495 assert(!MI->isDebugValue() && "Won't process debug values");
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000496
497 // Break dependence on undef uses. Do this before updating LiveRegs below.
498 unsigned OpNum;
499 unsigned Pref = TII->getUndefRegClearance(MI, OpNum, TRI);
500 if (Pref) {
501 if (shouldBreakDependence(MI, OpNum, Pref))
502 UndefReads.push_back(std::make_pair(MI, OpNum));
503 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000504 const MCInstrDesc &MCID = MI->getDesc();
505 for (unsigned i = 0,
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000506 e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000507 i != e; ++i) {
508 MachineOperand &MO = MI->getOperand(i);
509 if (!MO.isReg())
510 continue;
511 if (MO.isImplicit())
512 break;
513 if (MO.isUse())
514 continue;
515 int rx = regIndex(MO.getReg());
516 if (rx < 0)
517 continue;
518
519 // This instruction explicitly defines rx.
520 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
521 << '\t' << *MI);
522
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000523 // Check clearance before partial register updates.
524 // Call breakDependence before setting LiveRegs[rx].Def.
525 unsigned Pref = TII->getPartialRegUpdateClearance(MI, i, TRI);
526 if (Pref && shouldBreakDependence(MI, i, Pref))
527 TII->breakPartialRegDependency(MI, i, TRI);
528
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000529 // How many instructions since rx was last written?
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000530 LiveRegs[rx].Def = CurInstr;
531
532 // Kill off domains redefined by generic instructions.
533 if (Kill)
534 kill(rx);
535 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000536 ++CurInstr;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000537}
538
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000539/// \break Break false dependencies on undefined register reads.
540///
541/// Walk the block backward computing precise liveness. This is expensive, so we
542/// only do it on demand. Note that the occurrence of undefined register reads
543/// that should be broken is very rare, but when they occur we may have many in
544/// a single block.
545void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) {
546 if (UndefReads.empty())
547 return;
548
549 // Collect this block's live out register units.
Stephen Hines36b56882014-04-23 16:57:46 -0700550 LiveRegSet.init(TRI);
551 LiveRegSet.addLiveOuts(MBB);
552
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000553 MachineInstr *UndefMI = UndefReads.back().first;
554 unsigned OpIdx = UndefReads.back().second;
555
556 for (MachineBasicBlock::reverse_iterator I = MBB->rbegin(), E = MBB->rend();
557 I != E; ++I) {
Stephen Hines36b56882014-04-23 16:57:46 -0700558 // Update liveness, including the current instruction's defs.
559 LiveRegSet.stepBackward(*I);
Andrew Trick51dee242013-10-15 03:39:43 +0000560
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000561 if (UndefMI == &*I) {
Stephen Hines36b56882014-04-23 16:57:46 -0700562 if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg()))
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000563 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI);
564
565 UndefReads.pop_back();
566 if (UndefReads.empty())
567 return;
568
569 UndefMI = UndefReads.back().first;
570 OpIdx = UndefReads.back().second;
571 }
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000572 }
573}
574
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000575// A hard instruction only works in one domain. All input registers will be
576// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000577void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000578 // Collapse all uses.
579 for (unsigned i = mi->getDesc().getNumDefs(),
580 e = mi->getDesc().getNumOperands(); i != e; ++i) {
581 MachineOperand &mo = mi->getOperand(i);
582 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000583 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000584 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000585 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000586 }
587
588 // Kill all defs and force them.
589 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
590 MachineOperand &mo = mi->getOperand(i);
591 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000592 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000593 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000594 kill(rx);
595 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000596 }
597}
598
599// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000600void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000601 // Bitmask of available domains for this instruction after taking collapsed
602 // operands into account.
603 unsigned available = mask;
604
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000605 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000606 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000607 if (LiveRegs)
608 for (unsigned i = mi->getDesc().getNumDefs(),
609 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000610 MachineOperand &mo = mi->getOperand(i);
611 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000612 int rx = regIndex(mo.getReg());
Chris Lattner563d83f2010-03-31 20:32:51 +0000613 if (rx < 0) continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000614 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000615 // Bitmask of domains that dv and available have in common.
616 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000617 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000618 if (dv->isCollapsed()) {
619 // Restrict available domains to the ones in common with the operand.
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000620 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000621 // penalty for this operand.
622 if (common) available = common;
623 } else if (common)
624 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000625 used.push_back(rx);
626 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000627 // Open DomainValue is not compatible with instruction. It is useless
628 // now.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000629 kill(rx);
Chris Lattner563d83f2010-03-31 20:32:51 +0000630 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000631 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000632
633 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000634 if (isPowerOf2_32(available)) {
Michael J. Spencerc6af2432013-05-24 22:23:49 +0000635 unsigned domain = countTrailingZeros(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000636 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000637 visitHardInstr(mi, domain);
638 return;
639 }
640
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000641 // Kill off any remaining uses that don't match available, and build a list of
642 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000643 SmallVector<LiveReg, 4> Regs;
Craig Topperf22fd3f2013-07-03 05:11:49 +0000644 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000645 int rx = *i;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000646 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000647 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000648 if (!LR.Value->getCommonDomains(available)) {
649 kill(rx);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000650 continue;
651 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000652 // Sorted insertion.
653 bool Inserted = false;
Craig Topperf22fd3f2013-07-03 05:11:49 +0000654 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000655 i != e && !Inserted; ++i) {
656 if (LR.Def < i->Def) {
657 Inserted = true;
658 Regs.insert(i, LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000659 }
660 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000661 if (!Inserted)
662 Regs.push_back(LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000663 }
664
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000665 // doms are now sorted in order of appearance. Try to merge them all, giving
666 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000667 DomainValue *dv = 0;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000668 while (!Regs.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000669 if (!dv) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000670 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000671 // Force the first dv to match the current instruction.
672 dv->AvailableDomains = dv->getCommonDomains(available);
673 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner563d83f2010-03-31 20:32:51 +0000674 continue;
675 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000676
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000677 DomainValue *Latest = Regs.pop_back_val().Value;
678 // Skip already merged values.
679 if (Latest == dv || Latest->Next)
680 continue;
681 if (merge(dv, Latest))
682 continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000683
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000684 // If latest didn't merge, it is useless now. Kill all registers using it.
Craig Topperf22fd3f2013-07-03 05:11:49 +0000685 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000686 if (LiveRegs[*i].Value == Latest)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000687 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000688 }
689
690 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000691 if (!dv) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000692 dv = alloc();
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000693 dv->AvailableDomains = available;
694 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000695 dv->Instrs.push_back(mi);
696
Silviu Baranga541a8582012-10-03 08:29:36 +0000697 // Finally set all defs and non-collapsed uses to dv. We must iterate through
698 // all the operators, including imp-def ones.
699 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
700 ee = mi->operands_end();
701 ii != ee; ++ii) {
702 MachineOperand &mo = *ii;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000703 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000704 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000705 if (rx < 0) continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000706 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000707 kill(rx);
708 setLiveReg(rx, dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000709 }
710 }
711}
712
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000713bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000714 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000715 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000716 TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000717 LiveRegs = 0;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000718 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000719
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000720 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
721 << RC->getName() << " **********\n");
722
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000723 // If no relevant registers are used in the function, we can skip it
724 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000725 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000726 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
727 I != E; ++I)
Jakob Stoklund Olesen9aa6e0a2012-10-17 18:44:18 +0000728 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesena2a98fd2011-12-21 19:50:05 +0000729 anyregs = true;
730 break;
731 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000732 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000733
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000734 // Initialize the AliasMap on the first use.
735 if (AliasMap.empty()) {
736 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
737 // or -1.
738 AliasMap.resize(TRI->getNumRegs(), -1);
739 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000740 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
741 AI.isValid(); ++AI)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000742 AliasMap[*AI] = i;
743 }
744
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000745 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000746 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000747 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000748 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
749 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
750 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000751 enterBasicBlock(MBB);
752 if (SeenUnknownBackEdge)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000753 Loops.push_back(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000754 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000755 ++I)
756 visitInstr(I);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000757 processUndefReads(MBB);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000758 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000759 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000760
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000761 // Visit all the loop blocks again in order to merge DomainValues from
762 // back-edges.
763 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
764 MachineBasicBlock *MBB = Loops[i];
765 enterBasicBlock(MBB);
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000766 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
767 ++I)
768 if (!I->isDebugValue())
769 processDefs(I, false);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000770 processUndefReads(MBB);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000771 leaveBasicBlock(MBB);
772 }
773
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000774 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
775 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
776 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
777 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000778 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000779 continue;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000780 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000781 if (FI->second[i].Value)
782 release(FI->second[i].Value);
Jakob Stoklund Olesen0fdb05d2011-11-08 22:05:17 +0000783 delete[] FI->second;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000784 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000785 LiveOuts.clear();
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000786 UndefReads.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000787 Avail.clear();
788 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000789
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000790 return false;
791}
792
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000793FunctionPass *
794llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000795 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000796}