blob: 300f037121197f32d9c4d50ed910279d90b5ff36 [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"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +000026#include "llvm/CodeGen/Passes.h"
27#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetMachine.h"
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +000029#include "llvm/ADT/PostOrderIterator.h"
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000030#include "llvm/Support/Allocator.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +000033using namespace llvm;
34
Chris Lattner563d83f2010-03-31 20:32:51 +000035/// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000036/// of execution domains.
37///
38/// An open DomainValue represents a set of instructions that can still switch
39/// execution domain. Multiple registers may refer to the same open
40/// DomainValue - they will eventually be collapsed to the same execution
41/// domain.
42///
43/// A collapsed DomainValue represents a single register that has been forced
44/// into one of more execution domains. There is a separate collapsed
45/// DomainValue for each register, but it may contain multiple execution
46/// domains. A register value is initially created in a single execution
47/// domain, but if we were forced to pay the penalty of a domain crossing, we
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +000048/// keep track of the fact that the register is now available in multiple
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000049/// domains.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +000050namespace {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000051struct DomainValue {
52 // Basic reference counting.
53 unsigned Refs;
54
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000055 // Bitmask of available domains. For an open DomainValue, it is the still
56 // possible domains for collapsing. For a collapsed DomainValue it is the
57 // domains where the register is available for free.
58 unsigned AvailableDomains;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000059
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +000060 // Pointer to the next DomainValue in a chain. When two DomainValues are
61 // merged, Victim.Next is set to point to Victor, so old DomainValue
62 // references can be updated by folowing the chain.
63 DomainValue *Next;
64
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000065 // Twiddleable instructions using or defining these registers.
66 SmallVector<MachineInstr*, 8> Instrs;
67
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000068 // A collapsed DomainValue has no instructions to twiddle - it simply keeps
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000069 // track of the domains where the registers are already available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000070 bool isCollapsed() const { return Instrs.empty(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000071
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000072 // Is domain available?
73 bool hasDomain(unsigned domain) const {
74 return AvailableDomains & (1u << domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000075 }
76
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000077 // Mark domain as available.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000078 void addDomain(unsigned domain) {
79 AvailableDomains |= 1u << domain;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000080 }
81
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +000082 // Restrict to a single domain available.
83 void setSingleDomain(unsigned domain) {
84 AvailableDomains = 1u << domain;
85 }
86
87 // Return bitmask of domains that are available and in mask.
88 unsigned getCommonDomains(unsigned mask) const {
89 return AvailableDomains & mask;
90 }
91
92 // First domain available.
93 unsigned getFirstDomain() const {
94 return CountTrailingZeros_32(AvailableDomains);
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +000095 }
96
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +000097 DomainValue() : Refs(0) { clear(); }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +000098
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +000099 // Clear this DomainValue and point to next which has all its data.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000100 void clear() {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000101 AvailableDomains = 0;
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000102 Next = 0;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000103 Instrs.clear();
104 }
105};
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000106}
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000107
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000108namespace {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000109/// LiveReg - Information about a live register.
110struct LiveReg {
111 /// Value currently in this register, or NULL when no value is being tracked.
112 /// This counts as a DomainValue reference.
113 DomainValue *Value;
114
115 /// Instruction that defined this register, relative to the beginning of the
116 /// current basic block. When a LiveReg is used to represent a live-out
117 /// register, this value is relative to the end of the basic block, so it
118 /// will be a negative number.
119 int Def;
120};
121} // anonynous namespace
122
123namespace {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000124class ExeDepsFix : public MachineFunctionPass {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000125 static char ID;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000126 SpecificBumpPtrAllocator<DomainValue> Allocator;
127 SmallVector<DomainValue*,16> Avail;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000128
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000129 const TargetRegisterClass *const RC;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000130 MachineFunction *MF;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000131 const TargetInstrInfo *TII;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000132 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000133 std::vector<int> AliasMap;
134 const unsigned NumRegs;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000135 LiveReg *LiveRegs;
136 typedef DenseMap<MachineBasicBlock*, LiveReg*> LiveOutMap;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000137 LiveOutMap LiveOuts;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000138
139 /// Current instruction number.
140 /// The first instruction in each basic block is 0.
141 int CurInstr;
142
143 /// True when the current block has a predecessor that hasn't been visited
144 /// yet.
145 bool SeenUnknownBackEdge;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000146
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000147public:
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000148 ExeDepsFix(const TargetRegisterClass *rc)
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000149 : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {}
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000150
151 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
152 AU.setPreservesAll();
153 MachineFunctionPass::getAnalysisUsage(AU);
154 }
155
156 virtual bool runOnMachineFunction(MachineFunction &MF);
157
158 virtual const char *getPassName() const {
Jakob Stoklund Olesencd7dcad2011-11-07 21:23:39 +0000159 return "Execution dependency fix";
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000160 }
161
162private:
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000163 // Register mapping.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000164 int regIndex(unsigned Reg);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000165
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000166 // DomainValue allocation.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000167 DomainValue *alloc(int domain = -1);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000168 DomainValue *retain(DomainValue *DV) {
169 if (DV) ++DV->Refs;
170 return DV;
171 }
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000172 void release(DomainValue*);
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000173 DomainValue *resolve(DomainValue*&);
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000174
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000175 // LiveRegs manipulations.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000176 void setLiveReg(int rx, DomainValue *DV);
177 void kill(int rx);
178 void force(int rx, unsigned domain);
179 void collapse(DomainValue *dv, unsigned domain);
180 bool merge(DomainValue *A, DomainValue *B);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000181
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000182 void enterBasicBlock(MachineBasicBlock*);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000183 void leaveBasicBlock(MachineBasicBlock*);
184 void visitInstr(MachineInstr*);
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000185 void processDefs(MachineInstr*, bool Kill);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000186 void visitSoftInstr(MachineInstr*, unsigned mask);
187 void visitHardInstr(MachineInstr*, unsigned domain);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000188};
189}
190
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000191char ExeDepsFix::ID = 0;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000192
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000193/// Translate TRI register number to an index into our smaller tables of
194/// interesting registers. Return -1 for boring registers.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000195int ExeDepsFix::regIndex(unsigned Reg) {
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000196 assert(Reg < AliasMap.size() && "Invalid register");
197 return AliasMap[Reg];
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000198}
199
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000200DomainValue *ExeDepsFix::alloc(int domain) {
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000201 DomainValue *dv = Avail.empty() ?
202 new(Allocator.Allocate()) DomainValue :
203 Avail.pop_back_val();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000204 if (domain >= 0)
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000205 dv->addDomain(domain);
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000206 assert(dv->Refs == 0 && "Reference count wasn't cleared");
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000207 assert(!dv->Next && "Chained DomainValue shouldn't have been recycled");
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000208 return dv;
209}
210
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000211/// release - Release a reference to DV. When the last reference is released,
212/// collapse if needed.
213void ExeDepsFix::release(DomainValue *DV) {
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000214 while (DV) {
215 assert(DV->Refs && "Bad DomainValue");
216 if (--DV->Refs)
217 return;
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000218
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000219 // There are no more DV references. Collapse any contained instructions.
220 if (DV->AvailableDomains && !DV->isCollapsed())
221 collapse(DV, DV->getFirstDomain());
Jakob Stoklund Olesen35e93242011-11-08 21:57:44 +0000222
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000223 DomainValue *Next = DV->Next;
224 DV->clear();
225 Avail.push_back(DV);
226 // Also release the next DomainValue in the chain.
227 DV = Next;
228 }
229}
230
231/// resolve - Follow the chain of dead DomainValues until a live DomainValue is
232/// reached. Update the referenced pointer when necessary.
233DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) {
234 DomainValue *DV = DVRef;
235 if (!DV || !DV->Next)
236 return DV;
237
238 // DV has a chain. Find the end.
239 do DV = DV->Next;
240 while (DV->Next);
241
242 // Update DVRef to point to DV.
243 retain(DV);
244 release(DVRef);
245 DVRef = DV;
246 return DV;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000247}
248
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000249/// Set LiveRegs[rx] = dv, updating reference counts.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000250void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000251 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000252 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000253
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000254 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000255 return;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000256 if (LiveRegs[rx].Value)
257 release(LiveRegs[rx].Value);
258 LiveRegs[rx].Value = retain(dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000259}
260
261// Kill register rx, recycle or collapse any DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000262void ExeDepsFix::kill(int rx) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000263 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000264 assert(LiveRegs && "Must enter basic block first.");
265 if (!LiveRegs[rx].Value)
266 return;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000267
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000268 release(LiveRegs[rx].Value);
269 LiveRegs[rx].Value = 0;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000270}
271
272/// Force register rx into domain.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000273void ExeDepsFix::force(int rx, unsigned domain) {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000274 assert(unsigned(rx) < NumRegs && "Invalid index");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000275 assert(LiveRegs && "Must enter basic block first.");
276 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000277 if (dv->isCollapsed())
278 dv->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000279 else if (dv->hasDomain(domain))
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000280 collapse(dv, domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000281 else {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000282 // This is an incompatible open DomainValue. Collapse it to whatever and
283 // force the new value into domain. This costs a domain crossing.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000284 collapse(dv, dv->getFirstDomain());
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000285 assert(LiveRegs[rx].Value && "Not live after collapse?");
286 LiveRegs[rx].Value->addDomain(domain);
Jakob Stoklund Olesen8ba1c6a2010-04-06 19:48:56 +0000287 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000288 } else {
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000289 // Set up basic collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000290 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000291 }
292}
293
294/// Collapse open DomainValue into given domain. If there are multiple
295/// registers using dv, they each get a unique collapsed DomainValue.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000296void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000297 assert(dv->hasDomain(domain) && "Cannot collapse");
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000298
299 // Collapse all the instructions.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000300 while (!dv->Instrs.empty())
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000301 TII->setExecutionDomain(dv->Instrs.pop_back_val(), domain);
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000302 dv->setSingleDomain(domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000303
304 // If there are multiple users, give them new, unique DomainValues.
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000305 if (LiveRegs && dv->Refs > 1)
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000306 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000307 if (LiveRegs[rx].Value == dv)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000308 setLiveReg(rx, alloc(domain));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000309}
310
311/// Merge - All instructions and registers in B are moved to A, and B is
312/// released.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000313bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000314 assert(!A->isCollapsed() && "Cannot merge into collapsed");
315 assert(!B->isCollapsed() && "Cannot merge from collapsed");
Jakob Stoklund Olesen85ffee22010-03-31 20:05:12 +0000316 if (A == B)
Jakob Stoklund Olesen5f282b52010-03-31 17:13:16 +0000317 return true;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000318 // Restrict to the domains that A and B have in common.
319 unsigned common = A->getCommonDomains(B->AvailableDomains);
320 if (!common)
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000321 return false;
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000322 A->AvailableDomains = common;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000323 A->Instrs.append(B->Instrs.begin(), B->Instrs.end());
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000324
325 // Clear the old DomainValue so we won't try to swizzle instructions twice.
Jakob Stoklund Olesen737e9a22011-11-08 23:26:00 +0000326 B->clear();
Jakob Stoklund Olesendbc372f2011-11-09 00:06:18 +0000327 // All uses of B are referred to A.
328 B->Next = retain(A);
Jakob Stoklund Olesene1b3e112011-11-08 20:57:04 +0000329
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000330 for (unsigned rx = 0; rx != NumRegs; ++rx)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000331 if (LiveRegs[rx].Value == B)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000332 setLiveReg(rx, A);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000333 return true;
334}
335
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000336// enterBasicBlock - Set up LiveRegs by merging predecessor live-out values.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000337void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000338 // Detect back-edges from predecessors we haven't processed yet.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000339 SeenUnknownBackEdge = false;
340
341 // Reset instruction counter in each basic block.
342 CurInstr = 0;
343
344 // Set up LiveRegs to represent registers entering MBB.
345 if (!LiveRegs)
346 LiveRegs = new LiveReg[NumRegs];
347
348 // Default values are 'nothing happened a long time ago'.
349 for (unsigned rx = 0; rx != NumRegs; ++rx) {
350 LiveRegs[rx].Value = 0;
351 LiveRegs[rx].Def = -(1 << 20);
352 }
353
354 // This is the entry block.
355 if (MBB->pred_empty()) {
356 for (MachineBasicBlock::livein_iterator i = MBB->livein_begin(),
357 e = MBB->livein_end(); i != e; ++i) {
358 int rx = regIndex(*i);
359 if (rx < 0)
360 continue;
361 // Treat function live-ins as if they were defined just before the first
362 // instruction. Usually, function arguments are set up immediately
363 // before the call.
364 LiveRegs[rx].Def = -1;
365 }
366 DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n");
367 return;
368 }
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000369
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000370 // Try to coalesce live-out registers from predecessors.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000371 for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(),
372 pe = MBB->pred_end(); pi != pe; ++pi) {
373 LiveOutMap::const_iterator fi = LiveOuts.find(*pi);
374 if (fi == LiveOuts.end()) {
375 SeenUnknownBackEdge = true;
376 continue;
377 }
378 assert(fi->second && "Can't have NULL entries");
379
380 for (unsigned rx = 0; rx != NumRegs; ++rx) {
381 // Use the most recent predecessor def for each register.
382 LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, fi->second[rx].Def);
383
384 DomainValue *pdv = resolve(fi->second[rx].Value);
385 if (!pdv)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000386 continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000387 if (!LiveRegs[rx].Value) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000388 setLiveReg(rx, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000389 continue;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000390 }
Chris Lattner563d83f2010-03-31 20:32:51 +0000391
392 // We have a live DomainValue from more than one predecessor.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000393 if (LiveRegs[rx].Value->isCollapsed()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000394 // We are already collapsed, but predecessor is not. Force him.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000395 unsigned Domain = LiveRegs[rx].Value->getFirstDomain();
396 if (!pdv->isCollapsed() && pdv->hasDomain(Domain))
397 collapse(pdv, Domain);
Chris Lattner563d83f2010-03-31 20:32:51 +0000398 continue;
399 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000400
Chris Lattner563d83f2010-03-31 20:32:51 +0000401 // Currently open, merge in predecessor.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000402 if (!pdv->isCollapsed())
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000403 merge(LiveRegs[rx].Value, pdv);
Chris Lattner563d83f2010-03-31 20:32:51 +0000404 else
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000405 force(rx, pdv->getFirstDomain());
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000406 }
407 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000408 DEBUG(dbgs() << "BB#" << MBB->getNumber()
409 << (SeenUnknownBackEdge ? ": incomplete\n" : ": all preds known\n"));
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000410}
411
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000412void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000413 assert(LiveRegs && "Must enter basic block first.");
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000414 // Save live registers at end of MBB - used by enterBasicBlock().
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000415 // Also use LiveOuts as a visited set to detect back-edges.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000416 bool First = LiveOuts.insert(std::make_pair(MBB, LiveRegs)).second;
417
418 if (First) {
419 // LiveRegs was inserted in LiveOuts. Adjust all defs to be relative to
420 // the end of this block instead of the beginning.
421 for (unsigned i = 0, e = NumRegs; i != e; ++i)
422 LiveRegs[i].Def -= CurInstr;
423 } else {
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000424 // Insertion failed, this must be the second pass.
425 // Release all the DomainValues instead of keeping them.
426 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000427 release(LiveRegs[i].Value);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000428 delete[] LiveRegs;
429 }
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000430 LiveRegs = 0;
431}
432
433void ExeDepsFix::visitInstr(MachineInstr *MI) {
434 if (MI->isDebugValue())
435 return;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000436
437 // Update instructions with explicit execution domains.
438 std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(MI);
439 if (DomP.first) {
440 if (DomP.second)
441 visitSoftInstr(MI, DomP.second);
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000442 else
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000443 visitHardInstr(MI, DomP.first);
444 }
445
446 // Process defs to track register ages, and kill values clobbered by generic
447 // instructions.
448 processDefs(MI, !DomP.first);
449}
450
451// Update def-ages for registers defined by MI.
452// If Kill is set, also kill off DomainValues clobbered by the defs.
453void ExeDepsFix::processDefs(MachineInstr *MI, bool Kill) {
454 assert(!MI->isDebugValue() && "Won't process debug values");
455 const MCInstrDesc &MCID = MI->getDesc();
456 for (unsigned i = 0,
457 e = MCID.isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
458 i != e; ++i) {
459 MachineOperand &MO = MI->getOperand(i);
460 if (!MO.isReg())
461 continue;
462 if (MO.isImplicit())
463 break;
464 if (MO.isUse())
465 continue;
466 int rx = regIndex(MO.getReg());
467 if (rx < 0)
468 continue;
469
470 // This instruction explicitly defines rx.
471 DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr
472 << '\t' << *MI);
473
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000474 // How many instructions since rx was last written?
475 unsigned Clearance = CurInstr - LiveRegs[rx].Def;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000476 LiveRegs[rx].Def = CurInstr;
477
478 // Kill off domains redefined by generic instructions.
479 if (Kill)
480 kill(rx);
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000481
482 // Verify clearance before partial register updates.
483 unsigned Pref = TII->getPartialRegUpdateClearance(MI, i, TRI);
484 if (!Pref)
485 continue;
486 DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref);
487 if (Pref > Clearance) {
488 DEBUG(dbgs() << ": Break dependency.\n");
489 TII->breakPartialRegDependency(MI, i, TRI);
490 continue;
491 }
492
493 // The current clearance seems OK, but we may be ignoring a def from a
494 // back-edge.
495 if (!SeenUnknownBackEdge || Pref <= unsigned(CurInstr)) {
496 DEBUG(dbgs() << ": OK.\n");
497 continue;
498 }
499
500 // A def from an unprocessed back-edge may make us break this dependency.
501 DEBUG(dbgs() << ": Wait for back-edge to resolve.\n");
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000502 }
503
504 ++CurInstr;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000505}
506
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000507// A hard instruction only works in one domain. All input registers will be
508// forced into that domain.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000509void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) {
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000510 // Collapse all uses.
511 for (unsigned i = mi->getDesc().getNumDefs(),
512 e = mi->getDesc().getNumOperands(); i != e; ++i) {
513 MachineOperand &mo = mi->getOperand(i);
514 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000515 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000516 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000517 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000518 }
519
520 // Kill all defs and force them.
521 for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) {
522 MachineOperand &mo = mi->getOperand(i);
523 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000524 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000525 if (rx < 0) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000526 kill(rx);
527 force(rx, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000528 }
529}
530
531// A soft instruction can be changed to work in other domains given by mask.
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000532void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000533 // Bitmask of available domains for this instruction after taking collapsed
534 // operands into account.
535 unsigned available = mask;
536
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000537 // Scan the explicit use operands for incoming domains.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000538 SmallVector<int, 4> used;
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000539 if (LiveRegs)
540 for (unsigned i = mi->getDesc().getNumDefs(),
541 e = mi->getDesc().getNumOperands(); i != e; ++i) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000542 MachineOperand &mo = mi->getOperand(i);
543 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000544 int rx = regIndex(mo.getReg());
Chris Lattner563d83f2010-03-31 20:32:51 +0000545 if (rx < 0) continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000546 if (DomainValue *dv = LiveRegs[rx].Value) {
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000547 // Bitmask of domains that dv and available have in common.
548 unsigned common = dv->getCommonDomains(available);
Chris Lattner563d83f2010-03-31 20:32:51 +0000549 // Is it possible to use this collapsed register for free?
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000550 if (dv->isCollapsed()) {
551 // Restrict available domains to the ones in common with the operand.
552 // If there are no common domains, we must pay the cross-domain
553 // penalty for this operand.
554 if (common) available = common;
555 } else if (common)
556 // Open DomainValue is compatible, save it for merging.
Chris Lattner563d83f2010-03-31 20:32:51 +0000557 used.push_back(rx);
558 else
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000559 // Open DomainValue is not compatible with instruction. It is useless
560 // now.
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000561 kill(rx);
Chris Lattner563d83f2010-03-31 20:32:51 +0000562 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000563 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000564
565 // If the collapsed operands force a single domain, propagate the collapse.
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000566 if (isPowerOf2_32(available)) {
567 unsigned domain = CountTrailingZeros_32(available);
Jakob Stoklund Olesen98e933f2011-09-27 22:57:18 +0000568 TII->setExecutionDomain(mi, domain);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000569 visitHardInstr(mi, domain);
570 return;
571 }
572
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000573 // Kill off any remaining uses that don't match available, and build a list of
574 // incoming DomainValues that we want to merge.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000575 SmallVector<LiveReg, 4> Regs;
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000576 for (SmallVector<int, 4>::iterator i=used.begin(), e=used.end(); i!=e; ++i) {
577 int rx = *i;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000578 const LiveReg &LR = LiveRegs[rx];
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000579 // This useless DomainValue could have been missed above.
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000580 if (!LR.Value->getCommonDomains(available)) {
581 kill(rx);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000582 continue;
583 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000584 // Sorted insertion.
585 bool Inserted = false;
586 for (SmallVector<LiveReg, 4>::iterator i = Regs.begin(), e = Regs.end();
587 i != e && !Inserted; ++i) {
588 if (LR.Def < i->Def) {
589 Inserted = true;
590 Regs.insert(i, LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000591 }
592 }
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000593 if (!Inserted)
594 Regs.push_back(LR);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000595 }
596
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000597 // doms are now sorted in order of appearance. Try to merge them all, giving
598 // priority to the latest ones.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000599 DomainValue *dv = 0;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000600 while (!Regs.empty()) {
Chris Lattner563d83f2010-03-31 20:32:51 +0000601 if (!dv) {
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000602 dv = Regs.pop_back_val().Value;
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000603 // Force the first dv to match the current instruction.
604 dv->AvailableDomains = dv->getCommonDomains(available);
605 assert(dv->AvailableDomains && "Domain should have been filtered");
Chris Lattner563d83f2010-03-31 20:32:51 +0000606 continue;
607 }
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000608
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000609 DomainValue *Latest = Regs.pop_back_val().Value;
610 // Skip already merged values.
611 if (Latest == dv || Latest->Next)
612 continue;
613 if (merge(dv, Latest))
614 continue;
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000615
Jakob Stoklund Olesene0103f02010-04-04 21:27:26 +0000616 // If latest didn't merge, it is useless now. Kill all registers using it.
Chris Lattner563d83f2010-03-31 20:32:51 +0000617 for (SmallVector<int,4>::iterator i=used.begin(), e=used.end(); i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000618 if (LiveRegs[*i].Value == Latest)
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000619 kill(*i);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000620 }
621
622 // dv is the DomainValue we are going to use for this instruction.
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000623 if (!dv) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000624 dv = alloc();
Jakob Stoklund Olesen7f5e43f2011-11-23 04:03:08 +0000625 dv->AvailableDomains = available;
626 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000627 dv->Instrs.push_back(mi);
628
629 // Finally set all defs and non-collapsed uses to dv.
630 for (unsigned i = 0, e = mi->getDesc().getNumOperands(); i != e; ++i) {
631 MachineOperand &mo = mi->getOperand(i);
632 if (!mo.isReg()) continue;
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000633 int rx = regIndex(mo.getReg());
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000634 if (rx < 0) continue;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000635 if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) {
Jakob Stoklund Olesen6bcb9a72011-11-08 21:57:47 +0000636 kill(rx);
637 setLiveReg(rx, dv);
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000638 }
639 }
640}
641
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000642bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) {
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000643 MF = &mf;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000644 TII = MF->getTarget().getInstrInfo();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000645 TRI = MF->getTarget().getRegisterInfo();
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000646 LiveRegs = 0;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000647 assert(NumRegs == RC->getNumRegs() && "Bad regclass");
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000648
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000649 DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: "
650 << RC->getName() << " **********\n");
651
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000652 // If no relevant registers are used in the function, we can skip it
653 // completely.
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000654 bool anyregs = false;
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000655 for (TargetRegisterClass::const_iterator I = RC->begin(), E = RC->end();
656 I != E; ++I)
Jakob Stoklund Olesend1bfc302011-11-15 08:20:43 +0000657 for (const unsigned *AI = TRI->getOverlaps(*I); *AI; ++AI)
658 if (MF->getRegInfo().isPhysRegUsed(*AI)) {
659 anyregs = true;
660 break;
661 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000662 if (!anyregs) return false;
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000663
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000664 // Initialize the AliasMap on the first use.
665 if (AliasMap.empty()) {
666 // Given a PhysReg, AliasMap[PhysReg] is either the relevant index into RC,
667 // or -1.
668 AliasMap.resize(TRI->getNumRegs(), -1);
669 for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i)
670 for (const unsigned *AI = TRI->getOverlaps(RC->getRegister(i)); *AI; ++AI)
671 AliasMap[*AI] = i;
672 }
673
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000674 MachineBasicBlock *Entry = MF->begin();
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000675 ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000676 SmallVector<MachineBasicBlock*, 16> Loops;
Jakob Stoklund Olesena59ce032011-11-07 21:59:29 +0000677 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
678 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
679 MachineBasicBlock *MBB = *MBBI;
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000680 enterBasicBlock(MBB);
681 if (SeenUnknownBackEdge)
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000682 Loops.push_back(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000683 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
Jakob Stoklund Olesen25265d02011-11-07 21:40:27 +0000684 ++I)
685 visitInstr(I);
686 leaveBasicBlock(MBB);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000687 }
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000688
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000689 // Visit all the loop blocks again in order to merge DomainValues from
690 // back-edges.
691 for (unsigned i = 0, e = Loops.size(); i != e; ++i) {
692 MachineBasicBlock *MBB = Loops[i];
693 enterBasicBlock(MBB);
Jakob Stoklund Olesenc2ecf3e2011-11-15 01:15:30 +0000694 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
695 ++I)
696 if (!I->isDebugValue())
697 processDefs(I, false);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000698 leaveBasicBlock(MBB);
699 }
700
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000701 // Clear the LiveOuts vectors and collapse any remaining DomainValues.
702 for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator
703 MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) {
704 LiveOutMap::const_iterator FI = LiveOuts.find(*MBBI);
Jakob Stoklund Olesenf4c47682011-11-09 01:06:56 +0000705 if (FI == LiveOuts.end() || !FI->second)
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000706 continue;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000707 for (unsigned i = 0, e = NumRegs; i != e; ++i)
Jakob Stoklund Olesen2947f732011-11-15 01:15:25 +0000708 if (FI->second[i].Value)
709 release(FI->second[i].Value);
Jakob Stoklund Olesen0fdb05d2011-11-08 22:05:17 +0000710 delete[] FI->second;
Jakob Stoklund Olesenb26c7722011-11-07 23:08:21 +0000711 }
Jakob Stoklund Olesen1a5d2a82010-03-30 20:04:01 +0000712 LiveOuts.clear();
Jakob Stoklund Olesenbbef8152010-04-04 18:00:21 +0000713 Avail.clear();
714 Allocator.DestroyAll();
Jakob Stoklund Olesene4b94b42010-03-29 23:24:21 +0000715
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000716 return false;
717}
718
Jakob Stoklund Olesendf4b35e2011-09-27 23:50:46 +0000719FunctionPass *
720llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) {
Jakob Stoklund Olesen56ab8752011-09-28 00:01:56 +0000721 return new ExeDepsFix(RC);
Jakob Stoklund Olesen352aa502010-03-25 17:25:00 +0000722}