blob: 9d486651ce0495a50ed33afc271835939672a7d2 [file] [log] [blame]
David Goodwin2e7be612009-10-26 16:59:04 +00001//===----- CriticalAntiDepBreaker.cpp - Anti-dep breaker -------- ---------===//
2//
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//
10// This file implements the CriticalAntiDepBreaker class, which
11// implements register anti-dependence breaking along a blocks
12// critical path during post-RA scheduler.
13//
14//===----------------------------------------------------------------------===//
15
David Goodwin4de099d2009-11-03 20:57:50 +000016#define DEBUG_TYPE "post-RA-sched"
David Goodwin2e7be612009-10-26 16:59:04 +000017#include "CriticalAntiDepBreaker.h"
18#include "llvm/CodeGen/MachineBasicBlock.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/Target/TargetMachine.h"
Evan Cheng46df4eb2010-06-16 07:35:02 +000021#include "llvm/Target/TargetInstrInfo.h"
David Goodwin2e7be612009-10-26 16:59:04 +000022#include "llvm/Target/TargetRegisterInfo.h"
23#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
26
27using namespace llvm;
28
29CriticalAntiDepBreaker::
Jim Grosbach01384ef2010-05-14 21:20:46 +000030CriticalAntiDepBreaker(MachineFunction& MFi) :
David Goodwin2e7be612009-10-26 16:59:04 +000031 AntiDepBreaker(), MF(MFi),
32 MRI(MF.getRegInfo()),
Evan Cheng46df4eb2010-06-16 07:35:02 +000033 TII(MF.getTarget().getInstrInfo()),
David Goodwin2e7be612009-10-26 16:59:04 +000034 TRI(MF.getTarget().getRegisterInfo()),
35 AllocatableSet(TRI->getAllocatableSet(MF))
36{
Bill Wendlingf7f72bc2010-07-15 05:56:32 +000037 Classes.reserve(TRI->getNumRegs());
38 KillIndices.reserve(TRI->getNumRegs());
39 DefIndices.reserve(TRI->getNumRegs());
David Goodwin2e7be612009-10-26 16:59:04 +000040}
41
42CriticalAntiDepBreaker::~CriticalAntiDepBreaker() {
43}
44
45void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
46 // Clear out the register class data.
Bill Wendlingf7f72bc2010-07-15 05:56:32 +000047 Classes.clear();
David Goodwin2e7be612009-10-26 16:59:04 +000048
49 // Initialize the indices to indicate that no registers are live.
David Goodwin990d2852009-12-09 17:18:22 +000050 const unsigned BBSize = BB->size();
51 for (unsigned i = 0; i < TRI->getNumRegs(); ++i) {
52 KillIndices[i] = ~0u;
53 DefIndices[i] = BBSize;
54 }
David Goodwin2e7be612009-10-26 16:59:04 +000055
56 // Clear "do not change" set.
57 KeepRegs.clear();
58
59 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
60
61 // Determine the live-out physregs for this block.
62 if (IsReturnBlock) {
63 // In a return block, examine the function live-out regs.
64 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
65 E = MRI.liveout_end(); I != E; ++I) {
66 unsigned Reg = *I;
67 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
68 KillIndices[Reg] = BB->size();
69 DefIndices[Reg] = ~0u;
70 // Repeat, for all aliases.
71 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
72 unsigned AliasReg = *Alias;
73 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
74 KillIndices[AliasReg] = BB->size();
75 DefIndices[AliasReg] = ~0u;
76 }
77 }
David Goodwin2e7be612009-10-26 16:59:04 +000078 }
79
Evan Cheng46df4eb2010-06-16 07:35:02 +000080 // In a non-return block, examine the live-in regs of all successors.
81 // Note a return block can have successors if the return instruction is
82 // predicated.
83 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
84 SE = BB->succ_end(); SI != SE; ++SI)
85 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
86 E = (*SI)->livein_end(); I != E; ++I) {
87 unsigned Reg = *I;
88 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
89 KillIndices[Reg] = BB->size();
90 DefIndices[Reg] = ~0u;
91 // Repeat, for all aliases.
92 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
93 unsigned AliasReg = *Alias;
94 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
95 KillIndices[AliasReg] = BB->size();
96 DefIndices[AliasReg] = ~0u;
97 }
98 }
99
David Goodwin2e7be612009-10-26 16:59:04 +0000100 // Mark live-out callee-saved registers. In a return block this is
101 // all callee-saved registers. In non-return this is any
102 // callee-saved register that is not saved in the prolog.
103 const MachineFrameInfo *MFI = MF.getFrameInfo();
104 BitVector Pristine = MFI->getPristineRegs(BB);
105 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
106 unsigned Reg = *I;
107 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
108 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
109 KillIndices[Reg] = BB->size();
110 DefIndices[Reg] = ~0u;
111 // Repeat, for all aliases.
112 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
113 unsigned AliasReg = *Alias;
114 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
115 KillIndices[AliasReg] = BB->size();
116 DefIndices[AliasReg] = ~0u;
117 }
118 }
119}
120
121void CriticalAntiDepBreaker::FinishBlock() {
122 RegRefs.clear();
123 KeepRegs.clear();
124}
125
126void CriticalAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
127 unsigned InsertPosIndex) {
Dale Johannesenb0812f12010-03-05 00:02:59 +0000128 if (MI->isDebugValue())
129 return;
David Goodwin2e7be612009-10-26 16:59:04 +0000130 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
131
132 // Any register which was defined within the previous scheduling region
133 // may have been rescheduled and its lifetime may overlap with registers
134 // in ways not reflected in our current liveness state. For each such
135 // register, adjust the liveness state to be conservatively correct.
David Goodwin990d2852009-12-09 17:18:22 +0000136 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg)
David Goodwin2e7be612009-10-26 16:59:04 +0000137 if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
138 assert(KillIndices[Reg] == ~0u && "Clobbered register is live!");
139 // Mark this register to be non-renamable.
140 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
141 // Move the def index to the end of the previous region, to reflect
142 // that the def could theoretically have been scheduled at the end.
143 DefIndices[Reg] = InsertPosIndex;
144 }
145
146 PrescanInstruction(MI);
147 ScanInstruction(MI, Count);
148}
149
150/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
151/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000152static const SDep *CriticalPathStep(const SUnit *SU) {
153 const SDep *Next = 0;
David Goodwin2e7be612009-10-26 16:59:04 +0000154 unsigned NextDepth = 0;
155 // Find the predecessor edge with the greatest depth.
Dan Gohman66db3a02010-04-19 23:11:58 +0000156 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin2e7be612009-10-26 16:59:04 +0000157 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000158 const SUnit *PredSU = P->getSUnit();
David Goodwin2e7be612009-10-26 16:59:04 +0000159 unsigned PredLatency = P->getLatency();
160 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
161 // In the case of a latency tie, prefer an anti-dependency edge over
162 // other types of edges.
163 if (NextDepth < PredTotalLatency ||
164 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
165 NextDepth = PredTotalLatency;
166 Next = &*P;
167 }
168 }
169 return Next;
170}
171
172void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr *MI) {
Evan Cheng46df4eb2010-06-16 07:35:02 +0000173 // It's not safe to change register allocation for source operands of
174 // that have special allocation requirements. Also assume all registers
175 // used in a call must not be changed (ABI).
176 // FIXME: The issue with predicated instruction is more complex. We are being
177 // conservatively here because the kill markers cannot be trusted after
178 // if-conversion:
179 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
180 // ...
181 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
182 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
183 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
184 //
185 // The first R6 kill is not really a kill since it's killed by a predicated
186 // instruction which may not be executed. The second R6 def may or may not
187 // re-define R6 so it's not safe to change it since the last R6 use cannot be
188 // changed.
189 bool Special = MI->getDesc().isCall() ||
190 MI->getDesc().hasExtraSrcRegAllocReq() ||
191 TII->isPredicated(MI);
192
David Goodwin2e7be612009-10-26 16:59:04 +0000193 // Scan the register operands for this instruction and update
194 // Classes and RegRefs.
195 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
196 MachineOperand &MO = MI->getOperand(i);
197 if (!MO.isReg()) continue;
198 unsigned Reg = MO.getReg();
199 if (Reg == 0) continue;
200 const TargetRegisterClass *NewRC = 0;
Jim Grosbach01384ef2010-05-14 21:20:46 +0000201
David Goodwin2e7be612009-10-26 16:59:04 +0000202 if (i < MI->getDesc().getNumOperands())
203 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
204
205 // For now, only allow the register to be changed if its register
206 // class is consistent across all uses.
207 if (!Classes[Reg] && NewRC)
208 Classes[Reg] = NewRC;
209 else if (!NewRC || Classes[Reg] != NewRC)
210 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
211
212 // Now check for aliases.
213 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
214 // If an alias of the reg is used during the live range, give up.
215 // Note that this allows us to skip checking if AntiDepReg
216 // overlaps with any of the aliases, among other things.
217 unsigned AliasReg = *Alias;
218 if (Classes[AliasReg]) {
219 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
220 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
221 }
222 }
223
224 // If we're still willing to consider this register, note the reference.
225 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
226 RegRefs.insert(std::make_pair(Reg, &MO));
227
Evan Cheng46df4eb2010-06-16 07:35:02 +0000228 if (MO.isUse() && Special) {
David Goodwin2e7be612009-10-26 16:59:04 +0000229 if (KeepRegs.insert(Reg)) {
230 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
231 *Subreg; ++Subreg)
232 KeepRegs.insert(*Subreg);
233 }
234 }
235 }
236}
237
238void CriticalAntiDepBreaker::ScanInstruction(MachineInstr *MI,
239 unsigned Count) {
240 // Update liveness.
241 // Proceding upwards, registers that are defed but not used in this
242 // instruction are now dead.
David Goodwin2e7be612009-10-26 16:59:04 +0000243
Evan Cheng46df4eb2010-06-16 07:35:02 +0000244 if (!TII->isPredicated(MI)) {
245 // Predicated defs are modeled as read + write, i.e. similar to two
246 // address updates.
247 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
248 MachineOperand &MO = MI->getOperand(i);
249 if (!MO.isReg()) continue;
250 unsigned Reg = MO.getReg();
251 if (Reg == 0) continue;
252 if (!MO.isDef()) continue;
253 // Ignore two-addr defs.
254 if (MI->isRegTiedToUseOperand(i)) continue;
255
256 DefIndices[Reg] = Count;
257 KillIndices[Reg] = ~0u;
258 assert(((KillIndices[Reg] == ~0u) !=
259 (DefIndices[Reg] == ~0u)) &&
260 "Kill and Def maps aren't consistent for Reg!");
261 KeepRegs.erase(Reg);
262 Classes[Reg] = 0;
263 RegRefs.erase(Reg);
264 // Repeat, for all subregs.
265 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
266 *Subreg; ++Subreg) {
267 unsigned SubregReg = *Subreg;
268 DefIndices[SubregReg] = Count;
269 KillIndices[SubregReg] = ~0u;
270 KeepRegs.erase(SubregReg);
271 Classes[SubregReg] = 0;
272 RegRefs.erase(SubregReg);
273 }
274 // Conservatively mark super-registers as unusable.
275 for (const unsigned *Super = TRI->getSuperRegisters(Reg);
276 *Super; ++Super) {
277 unsigned SuperReg = *Super;
278 Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
279 }
David Goodwin2e7be612009-10-26 16:59:04 +0000280 }
281 }
282 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
283 MachineOperand &MO = MI->getOperand(i);
284 if (!MO.isReg()) continue;
285 unsigned Reg = MO.getReg();
286 if (Reg == 0) continue;
287 if (!MO.isUse()) continue;
288
289 const TargetRegisterClass *NewRC = 0;
290 if (i < MI->getDesc().getNumOperands())
291 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
292
293 // For now, only allow the register to be changed if its register
294 // class is consistent across all uses.
295 if (!Classes[Reg] && NewRC)
296 Classes[Reg] = NewRC;
297 else if (!NewRC || Classes[Reg] != NewRC)
298 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
299
300 RegRefs.insert(std::make_pair(Reg, &MO));
301
302 // It wasn't previously live but now it is, this is a kill.
303 if (KillIndices[Reg] == ~0u) {
304 KillIndices[Reg] = Count;
305 DefIndices[Reg] = ~0u;
306 assert(((KillIndices[Reg] == ~0u) !=
307 (DefIndices[Reg] == ~0u)) &&
308 "Kill and Def maps aren't consistent for Reg!");
309 }
310 // Repeat, for all aliases.
311 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
312 unsigned AliasReg = *Alias;
313 if (KillIndices[AliasReg] == ~0u) {
314 KillIndices[AliasReg] = Count;
315 DefIndices[AliasReg] = ~0u;
316 }
317 }
318 }
319}
320
321unsigned
Jim Grosbach80c2b0d2010-01-06 22:21:25 +0000322CriticalAntiDepBreaker::findSuitableFreeRegister(MachineInstr *MI,
323 unsigned AntiDepReg,
David Goodwin2e7be612009-10-26 16:59:04 +0000324 unsigned LastNewReg,
Jim Grosbach2973b572010-01-06 16:48:02 +0000325 const TargetRegisterClass *RC)
326{
David Goodwin2e7be612009-10-26 16:59:04 +0000327 for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF),
328 RE = RC->allocation_order_end(MF); R != RE; ++R) {
329 unsigned NewReg = *R;
330 // Don't replace a register with itself.
331 if (NewReg == AntiDepReg) continue;
332 // Don't replace a register with one that was recently used to repair
333 // an anti-dependence with this AntiDepReg, because that would
334 // re-introduce that anti-dependence.
335 if (NewReg == LastNewReg) continue;
Jim Grosbach80c2b0d2010-01-06 22:21:25 +0000336 // If the instruction already has a def of the NewReg, it's not suitable.
337 // For example, Instruction with multiple definitions can result in this
338 // condition.
339 if (MI->modifiesRegister(NewReg, TRI)) continue;
David Goodwin2e7be612009-10-26 16:59:04 +0000340 // If NewReg is dead and NewReg's most recent def is not before
341 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000342 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
343 && "Kill and Def maps aren't consistent for AntiDepReg!");
344 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
345 && "Kill and Def maps aren't consistent for NewReg!");
David Goodwin2e7be612009-10-26 16:59:04 +0000346 if (KillIndices[NewReg] != ~0u ||
347 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
348 KillIndices[AntiDepReg] > DefIndices[NewReg])
349 continue;
350 return NewReg;
351 }
352
353 // No registers are free and available!
354 return 0;
355}
356
357unsigned CriticalAntiDepBreaker::
Dan Gohman66db3a02010-04-19 23:11:58 +0000358BreakAntiDependencies(const std::vector<SUnit>& SUnits,
359 MachineBasicBlock::iterator Begin,
360 MachineBasicBlock::iterator End,
David Goodwin2e7be612009-10-26 16:59:04 +0000361 unsigned InsertPosIndex) {
362 // The code below assumes that there is at least one instruction,
363 // so just duck out immediately if the block is empty.
364 if (SUnits.empty()) return 0;
365
Jim Grosbach533934e2010-06-01 23:48:44 +0000366 // Keep a map of the MachineInstr*'s back to the SUnit representing them.
367 // This is used for updating debug information.
368 DenseMap<MachineInstr*,const SUnit*> MISUnitMap;
369
David Goodwin2e7be612009-10-26 16:59:04 +0000370 // Find the node at the bottom of the critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000371 const SUnit *Max = 0;
David Goodwin2e7be612009-10-26 16:59:04 +0000372 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000373 const SUnit *SU = &SUnits[i];
Jim Grosbach533934e2010-06-01 23:48:44 +0000374 MISUnitMap[SU->getInstr()] = SU;
David Goodwin2e7be612009-10-26 16:59:04 +0000375 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
376 Max = SU;
377 }
378
379#ifndef NDEBUG
380 {
David Greene89d6a242010-01-04 17:47:05 +0000381 DEBUG(dbgs() << "Critical path has total latency "
David Goodwin2e7be612009-10-26 16:59:04 +0000382 << (Max->getDepth() + Max->Latency) << "\n");
David Greene89d6a242010-01-04 17:47:05 +0000383 DEBUG(dbgs() << "Available regs:");
David Goodwin2e7be612009-10-26 16:59:04 +0000384 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
385 if (KillIndices[Reg] == ~0u)
David Greene89d6a242010-01-04 17:47:05 +0000386 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin2e7be612009-10-26 16:59:04 +0000387 }
David Greene89d6a242010-01-04 17:47:05 +0000388 DEBUG(dbgs() << '\n');
David Goodwin2e7be612009-10-26 16:59:04 +0000389 }
390#endif
391
392 // Track progress along the critical path through the SUnit graph as we walk
393 // the instructions.
Dan Gohman66db3a02010-04-19 23:11:58 +0000394 const SUnit *CriticalPathSU = Max;
David Goodwin2e7be612009-10-26 16:59:04 +0000395 MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
396
397 // Consider this pattern:
398 // A = ...
399 // ... = A
400 // A = ...
401 // ... = A
402 // A = ...
403 // ... = A
404 // A = ...
405 // ... = A
406 // There are three anti-dependencies here, and without special care,
407 // we'd break all of them using the same register:
408 // A = ...
409 // ... = A
410 // B = ...
411 // ... = B
412 // B = ...
413 // ... = B
414 // B = ...
415 // ... = B
416 // because at each anti-dependence, B is the first register that
417 // isn't A which is free. This re-introduces anti-dependencies
418 // at all but one of the original anti-dependencies that we were
419 // trying to break. To avoid this, keep track of the most recent
420 // register that each register was replaced with, avoid
421 // using it to repair an anti-dependence on the same register.
422 // This lets us produce this:
423 // A = ...
424 // ... = A
425 // B = ...
426 // ... = B
427 // C = ...
428 // ... = C
429 // B = ...
430 // ... = B
431 // This still has an anti-dependence on B, but at least it isn't on the
432 // original critical path.
433 //
434 // TODO: If we tracked more than one register here, we could potentially
435 // fix that remaining critical edge too. This is a little more involved,
436 // because unlike the most recent register, less recent registers should
437 // still be considered, though only if no other registers are available.
438 unsigned LastNewReg[TargetRegisterInfo::FirstVirtualRegister] = {};
439
440 // Attempt to break anti-dependence edges on the critical path. Walk the
441 // instructions from the bottom up, tracking information about liveness
442 // as we go to help determine which registers are available.
443 unsigned Broken = 0;
444 unsigned Count = InsertPosIndex - 1;
445 for (MachineBasicBlock::iterator I = End, E = Begin;
446 I != E; --Count) {
447 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000448 if (MI->isDebugValue())
449 continue;
David Goodwin2e7be612009-10-26 16:59:04 +0000450
451 // Check if this instruction has a dependence on the critical path that
452 // is an anti-dependence that we may be able to break. If it is, set
453 // AntiDepReg to the non-zero register associated with the anti-dependence.
454 //
455 // We limit our attention to the critical path as a heuristic to avoid
456 // breaking anti-dependence edges that aren't going to significantly
457 // impact the overall schedule. There are a limited number of registers
458 // and we want to save them for the important edges.
Jim Grosbach01384ef2010-05-14 21:20:46 +0000459 //
David Goodwin2e7be612009-10-26 16:59:04 +0000460 // TODO: Instructions with multiple defs could have multiple
461 // anti-dependencies. The current code here only knows how to break one
462 // edge per instruction. Note that we'd have to be able to break all of
463 // the anti-dependencies in an instruction in order to be effective.
464 unsigned AntiDepReg = 0;
465 if (MI == CriticalPathMI) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000466 if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
467 const SUnit *NextSU = Edge->getSUnit();
David Goodwin2e7be612009-10-26 16:59:04 +0000468
469 // Only consider anti-dependence edges.
470 if (Edge->getKind() == SDep::Anti) {
471 AntiDepReg = Edge->getReg();
472 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
473 if (!AllocatableSet.test(AntiDepReg))
474 // Don't break anti-dependencies on non-allocatable registers.
475 AntiDepReg = 0;
476 else if (KeepRegs.count(AntiDepReg))
477 // Don't break anti-dependencies if an use down below requires
478 // this exact register.
479 AntiDepReg = 0;
480 else {
481 // If the SUnit has other dependencies on the SUnit that it
482 // anti-depends on, don't bother breaking the anti-dependency
483 // since those edges would prevent such units from being
484 // scheduled past each other regardless.
485 //
486 // Also, if there are dependencies on other SUnits with the
487 // same register as the anti-dependency, don't attempt to
488 // break it.
Dan Gohman66db3a02010-04-19 23:11:58 +0000489 for (SUnit::const_pred_iterator P = CriticalPathSU->Preds.begin(),
David Goodwin2e7be612009-10-26 16:59:04 +0000490 PE = CriticalPathSU->Preds.end(); P != PE; ++P)
491 if (P->getSUnit() == NextSU ?
492 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
493 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
494 AntiDepReg = 0;
495 break;
496 }
497 }
498 }
499 CriticalPathSU = NextSU;
500 CriticalPathMI = CriticalPathSU->getInstr();
501 } else {
502 // We've reached the end of the critical path.
503 CriticalPathSU = 0;
504 CriticalPathMI = 0;
505 }
506 }
507
508 PrescanInstruction(MI);
509
Evan Cheng46df4eb2010-06-16 07:35:02 +0000510 // If MI's defs have a special allocation requirement, don't allow
511 // any def registers to be changed. Also assume all registers
512 // defined in a call must not be changed (ABI).
513 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq() ||
514 TII->isPredicated(MI))
David Goodwin2e7be612009-10-26 16:59:04 +0000515 // If this instruction's defs have special allocation requirement, don't
516 // break this anti-dependency.
517 AntiDepReg = 0;
518 else if (AntiDepReg) {
519 // If this instruction has a use of AntiDepReg, breaking it
520 // is invalid.
521 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
522 MachineOperand &MO = MI->getOperand(i);
523 if (!MO.isReg()) continue;
524 unsigned Reg = MO.getReg();
525 if (Reg == 0) continue;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000526 if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
David Goodwin2e7be612009-10-26 16:59:04 +0000527 AntiDepReg = 0;
528 break;
529 }
530 }
531 }
532
533 // Determine AntiDepReg's register class, if it is live and is
534 // consistently used within a single class.
535 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
536 assert((AntiDepReg == 0 || RC != NULL) &&
537 "Register should be live if it's causing an anti-dependence!");
538 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
539 AntiDepReg = 0;
540
541 // Look for a suitable register to use to break the anti-depenence.
542 //
543 // TODO: Instead of picking the first free register, consider which might
544 // be the best.
545 if (AntiDepReg != 0) {
Jim Grosbach80c2b0d2010-01-06 22:21:25 +0000546 if (unsigned NewReg = findSuitableFreeRegister(MI, AntiDepReg,
David Goodwin2e7be612009-10-26 16:59:04 +0000547 LastNewReg[AntiDepReg],
548 RC)) {
David Greene89d6a242010-01-04 17:47:05 +0000549 DEBUG(dbgs() << "Breaking anti-dependence edge on "
David Goodwin2e7be612009-10-26 16:59:04 +0000550 << TRI->getName(AntiDepReg)
551 << " with " << RegRefs.count(AntiDepReg) << " references"
552 << " using " << TRI->getName(NewReg) << "!\n");
553
554 // Update the references to the old register to refer to the new
555 // register.
556 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
557 std::multimap<unsigned, MachineOperand *>::iterator>
558 Range = RegRefs.equal_range(AntiDepReg);
559 for (std::multimap<unsigned, MachineOperand *>::iterator
Jim Grosbach533934e2010-06-01 23:48:44 +0000560 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
David Goodwin2e7be612009-10-26 16:59:04 +0000561 Q->second->setReg(NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000562 // If the SU for the instruction being updated has debug information
563 // related to the anti-dependency register, make sure to update that
564 // as well.
565 const SUnit *SU = MISUnitMap[Q->second->getParent()];
Jim Grosbach086723d2010-06-02 15:29:36 +0000566 if (!SU) continue;
Jim Grosbach533934e2010-06-01 23:48:44 +0000567 for (unsigned i = 0, e = SU->DbgInstrList.size() ; i < e ; ++i) {
568 MachineInstr *DI = SU->DbgInstrList[i];
569 assert (DI->getNumOperands()==3 && DI->getOperand(0).isReg() &&
570 DI->getOperand(0).getReg()
571 && "Non register dbg_value attached to SUnit!");
572 if (DI->getOperand(0).getReg() == AntiDepReg)
573 DI->getOperand(0).setReg(NewReg);
574 }
575 }
David Goodwin2e7be612009-10-26 16:59:04 +0000576
577 // We just went back in time and modified history; the
578 // liveness information for the anti-depenence reg is now
579 // inconsistent. Set the state as if it were dead.
580 Classes[NewReg] = Classes[AntiDepReg];
581 DefIndices[NewReg] = DefIndices[AntiDepReg];
582 KillIndices[NewReg] = KillIndices[AntiDepReg];
583 assert(((KillIndices[NewReg] == ~0u) !=
584 (DefIndices[NewReg] == ~0u)) &&
585 "Kill and Def maps aren't consistent for NewReg!");
586
587 Classes[AntiDepReg] = 0;
588 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
589 KillIndices[AntiDepReg] = ~0u;
590 assert(((KillIndices[AntiDepReg] == ~0u) !=
591 (DefIndices[AntiDepReg] == ~0u)) &&
592 "Kill and Def maps aren't consistent for AntiDepReg!");
593
594 RegRefs.erase(AntiDepReg);
595 LastNewReg[AntiDepReg] = NewReg;
596 ++Broken;
597 }
598 }
599
600 ScanInstruction(MI, Count);
601 }
602
603 return Broken;
604}