blob: 031f19c135a97df9a81c83ad9f5f73e0a6e24031 [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"
Andrew Tricka6a9ac52013-10-14 22:19:03 +000026#include "llvm/CodeGen/LiveRegUnits.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.
144 LiveRegUnits LiveUnits;
145
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
158 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
159 AU.setPreservesAll();
160 MachineFunctionPass::getAnalysisUsage(AU);
161 }
162
163 virtual bool runOnMachineFunction(MachineFunction &MF);
164
165 virtual const char *getPassName() const {
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();
355 LiveUnits.clear();
356
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.
550 LiveUnits.init(TRI);
551 for (MachineBasicBlock::const_succ_iterator SI = MBB->succ_begin(),
552 SE = MBB->succ_end(); SI != SE; ++SI) {
553 LiveUnits.addLiveIns(*SI, *TRI);
554 }
555 MachineInstr *UndefMI = UndefReads.back().first;
556 unsigned OpIdx = UndefReads.back().second;
557
558 for (MachineBasicBlock::reverse_iterator I = MBB->rbegin(), E = MBB->rend();
559 I != E; ++I) {
Andrew Trick51dee242013-10-15 03:39:43 +0000560 // Update liveness, including the current instrucion's defs.
561 LiveUnits.stepBackward(*I, *TRI);
562
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000563 if (UndefMI == &*I) {
564 if (!LiveUnits.contains(UndefMI->getOperand(OpIdx).getReg(), *TRI))
565 TII->breakPartialRegDependency(UndefMI, OpIdx, TRI);
566
567 UndefReads.pop_back();
568 if (UndefReads.empty())
569 return;
570
571 UndefMI = UndefReads.back().first;
572 OpIdx = UndefReads.back().second;
573 }
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000574 }
575}
576
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000577// A hard instruction only works in one domain. All input registers will be
578// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000579void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000580 // Collapse all uses.
581 for (unsigned i = mi->getDesc().getNumDefs(),
582 e = mi->getDesc().getNumOperands(); i != e; ++i) {
583 MachineOperand &mo = mi->getOperand(i);
584 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000585 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000586 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000587 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000588 }
589
590 // Kill all defs and force them.
591 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
592 MachineOperand &mo = mi->getOperand(i);
593 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000594 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000595 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000596 kill(rx);
597 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000598 }
599}
600
601// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000602void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000603 // Bitmask of available domains for this instruction after taking collapsed
604 // operands into account.
605 unsigned available = mask;
606
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000607 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000608 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000609 if (LiveRegs)
610 for (unsigned i = mi->getDesc().getNumDefs(),
611 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000612 MachineOperand &mo = mi->getOperand(i);
613 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000614 int rx = regIndex(mo.getReg());
Chris Lattner563d83f2010-03-31 20:32:51 +0000615 if (rx < 0) continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000616 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000617 // Bitmask of domains that dv and available have in common.
618 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000619 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000620 if (dv->isCollapsed()) {
621 // Restrict available domains to the ones in common with the operand.
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000622 // If there are no common domains, we must pay the cross-domain
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000623 // penalty for this operand.
624 if (common) available = common;
625 } else if (common)
626 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000627 used.push_back(rx);
628 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000629 // Open DomainValue is not compatible with instruction. It is useless
630 // now.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000631 kill(rx);
Chris Lattner563d83f2010-03-31 20:32:51 +0000632 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000633 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000634
635 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000636 if (isPowerOf2_32(available)) {
Michael J. Spencerc6af2432013-05-24 22:23:49 +0000637 unsigned domain = countTrailingZeros(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000638 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000639 visitHardInstr(mi, domain);
640 return;
641 }
642
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000643 // Kill off any remaining uses that don't match available, and build a list of
644 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000645 SmallVector<LiveReg, 4> Regs;
Craig Topperf22fd3f2013-07-03 05:11:49 +0000646 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000647 int rx = *i;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000648 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000649 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000650 if (!LR.Value->getCommonDomains(available)) {
651 kill(rx);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000652 continue;
653 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000654 // Sorted insertion.
655 bool Inserted = false;
Craig Topperf22fd3f2013-07-03 05:11:49 +0000656 for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end();
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000657 i != e && !Inserted; ++i) {
658 if (LR.Def < i->Def) {
659 Inserted = true;
660 Regs.insert(i, LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000661 }
662 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000663 if (!Inserted)
664 Regs.push_back(LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000665 }
666
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000667 // doms are now sorted in order of appearance. Try to merge them all, giving
668 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000669 DomainValue *dv = 0;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000670 while (!Regs.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000671 if (!dv) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000672 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000673 // Force the first dv to match the current instruction.
674 dv->AvailableDomains = dv->getCommonDomains(available);
675 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner563d83f2010-03-31 20:32:51 +0000676 continue;
677 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000678
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000679 DomainValue *Latest = Regs.pop_back_val().Value;
680 // Skip already merged values.
681 if (Latest == dv || Latest->Next)
682 continue;
683 if (merge(dv, Latest))
684 continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000685
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000686 // If latest didn't merge, it is useless now. Kill all registers using it.
Craig Topperf22fd3f2013-07-03 05:11:49 +0000687 for (SmallVectorImpl<int>::iterator i=used.begin(), e=used.end(); i!=e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000688 if (LiveRegs[*i].Value == Latest)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000689 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000690 }
691
692 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000693 if (!dv) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000694 dv = alloc();
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000695 dv->AvailableDomains = available;
696 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000697 dv->Instrs.push_back(mi);
698
Silviu Baranga541a8582012-10-03 08:29:36 +0000699 // Finally set all defs and non-collapsed uses to dv. We must iterate through
700 // all the operators, including imp-def ones.
701 for (MachineInstr::mop_iterator ii = mi->operands_begin(),
702 ee = mi->operands_end();
703 ii != ee; ++ii) {
704 MachineOperand &mo = *ii;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000705 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000706 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000707 if (rx < 0) continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000708 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000709 kill(rx);
710 setLiveReg(rx, dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000711 }
712 }
713}
714
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000715bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000716 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000717 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000718 TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000719 LiveRegs = 0;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000720 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000721
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000722 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
723 << RC->getName() << " **********\n");
724
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000725 // If no relevant registers are used in the function, we can skip it
726 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000727 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000728 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
729 I != E; ++I)
Jakob Stoklund Olesen9aa6e0a2012-10-17 18:44:18 +0000730 if (MF->getRegInfo().isPhysRegUsed(*I)) {
Jakob Stoklund Olesena2a98fd2011-12-21 19:50:05 +0000731 anyregs = true;
732 break;
733 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000734 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000735
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000736 // Initialize the AliasMap on the first use.
737 if (AliasMap.empty()) {
738 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
739 // or -1.
740 AliasMap.resize(TRI->getNumRegs(), -1);
741 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000742 for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true);
743 AI.isValid(); ++AI)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000744 AliasMap[*AI] = i;
745 }
746
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000747 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000748 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000749 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000750 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
751 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
752 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000753 enterBasicBlock(MBB);
754 if (SeenUnknownBackEdge)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000755 Loops.push_back(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000756 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000757 ++I)
758 visitInstr(I);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000759 processUndefReads(MBB);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000760 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000761 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000762
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000763 // Visit all the loop blocks again in order to merge DomainValues from
764 // back-edges.
765 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
766 MachineBasicBlock *MBB = Loops[i];
767 enterBasicBlock(MBB);
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000768 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
769 ++I)
770 if (!I->isDebugValue())
771 processDefs(I, false);
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000772 processUndefReads(MBB);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000773 leaveBasicBlock(MBB);
774 }
775
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000776 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
777 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
778 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
779 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000780 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000781 continue;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000782 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000783 if (FI->second[i].Value)
784 release(FI->second[i].Value);
Jakob Stoklund Olesen0fdb05d2011-11-08 22:05:17 +0000785 delete[] FI->second;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000786 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000787 LiveOuts.clear();
Andrew Tricka6a9ac52013-10-14 22:19:03 +0000788 UndefReads.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000789 Avail.clear();
790 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000791
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000792 return false;
793}
794
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000795FunctionPass *
796llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000797 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000798}