blob: cb15166cd4ae3e795bcff872566f9fb681f70289 [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()),
Bill Wendling9c2a0342010-07-15 19:58:14 +000035 AllocatableSet(TRI->getAllocatableSet(MF)),
36 Classes(TRI->getNumRegs(), static_cast<const TargetRegisterClass *>(0)),
37 KillIndices(TRI->getNumRegs(), 0),
38 DefIndices(TRI->getNumRegs(), 0) {}
David Goodwin2e7be612009-10-26 16:59:04 +000039
40CriticalAntiDepBreaker::~CriticalAntiDepBreaker() {
41}
42
43void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
Bill Wendling9c2a0342010-07-15 19:58:14 +000044 Classes.clear();
David Goodwin2e7be612009-10-26 16:59:04 +000045
David Goodwin990d2852009-12-09 17:18:22 +000046 const unsigned BBSize = BB->size();
Bill Wendling9c2a0342010-07-15 19:58:14 +000047 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
48 // Clear out the register class data.
49 Classes[i] = static_cast<const TargetRegisterClass *>(0);
50
51 // Initialize the indices to indicate that no registers are live.
David Goodwin990d2852009-12-09 17:18:22 +000052 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;
Bill Wendling9c2a0342010-07-15 19:58:14 +000070
David Goodwin2e7be612009-10-26 16:59:04 +000071 // Repeat, for all aliases.
72 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
73 unsigned AliasReg = *Alias;
74 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
75 KillIndices[AliasReg] = BB->size();
76 DefIndices[AliasReg] = ~0u;
77 }
78 }
David Goodwin2e7be612009-10-26 16:59:04 +000079 }
80
Evan Cheng46df4eb2010-06-16 07:35:02 +000081 // In a non-return block, examine the live-in regs of all successors.
82 // Note a return block can have successors if the return instruction is
83 // predicated.
84 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
85 SE = BB->succ_end(); SI != SE; ++SI)
86 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
87 E = (*SI)->livein_end(); I != E; ++I) {
88 unsigned Reg = *I;
89 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
90 KillIndices[Reg] = BB->size();
91 DefIndices[Reg] = ~0u;
Bill Wendling9c2a0342010-07-15 19:58:14 +000092
Evan Cheng46df4eb2010-06-16 07:35:02 +000093 // Repeat, for all aliases.
94 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
95 unsigned AliasReg = *Alias;
96 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
97 KillIndices[AliasReg] = BB->size();
98 DefIndices[AliasReg] = ~0u;
99 }
100 }
101
David Goodwin2e7be612009-10-26 16:59:04 +0000102 // Mark live-out callee-saved registers. In a return block this is
103 // all callee-saved registers. In non-return this is any
104 // callee-saved register that is not saved in the prolog.
105 const MachineFrameInfo *MFI = MF.getFrameInfo();
106 BitVector Pristine = MFI->getPristineRegs(BB);
107 for (const unsigned *I = TRI->getCalleeSavedRegs(); *I; ++I) {
108 unsigned Reg = *I;
109 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
110 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
111 KillIndices[Reg] = BB->size();
112 DefIndices[Reg] = ~0u;
Bill Wendling9c2a0342010-07-15 19:58:14 +0000113
David Goodwin2e7be612009-10-26 16:59:04 +0000114 // Repeat, for all aliases.
115 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
116 unsigned AliasReg = *Alias;
117 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
118 KillIndices[AliasReg] = BB->size();
119 DefIndices[AliasReg] = ~0u;
120 }
121 }
122}
123
124void CriticalAntiDepBreaker::FinishBlock() {
125 RegRefs.clear();
126 KeepRegs.clear();
127}
128
129void CriticalAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
130 unsigned InsertPosIndex) {
Dale Johannesenb0812f12010-03-05 00:02:59 +0000131 if (MI->isDebugValue())
132 return;
David Goodwin2e7be612009-10-26 16:59:04 +0000133 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
134
135 // Any register which was defined within the previous scheduling region
136 // may have been rescheduled and its lifetime may overlap with registers
137 // in ways not reflected in our current liveness state. For each such
138 // register, adjust the liveness state to be conservatively correct.
David Goodwin990d2852009-12-09 17:18:22 +0000139 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg)
David Goodwin2e7be612009-10-26 16:59:04 +0000140 if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
141 assert(KillIndices[Reg] == ~0u && "Clobbered register is live!");
Bill Wendling9c2a0342010-07-15 19:58:14 +0000142
David Goodwin2e7be612009-10-26 16:59:04 +0000143 // Mark this register to be non-renamable.
144 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
Bill Wendling9c2a0342010-07-15 19:58:14 +0000145
David Goodwin2e7be612009-10-26 16:59:04 +0000146 // Move the def index to the end of the previous region, to reflect
147 // that the def could theoretically have been scheduled at the end.
148 DefIndices[Reg] = InsertPosIndex;
149 }
150
151 PrescanInstruction(MI);
152 ScanInstruction(MI, Count);
153}
154
155/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
156/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000157static const SDep *CriticalPathStep(const SUnit *SU) {
158 const SDep *Next = 0;
David Goodwin2e7be612009-10-26 16:59:04 +0000159 unsigned NextDepth = 0;
160 // Find the predecessor edge with the greatest depth.
Dan Gohman66db3a02010-04-19 23:11:58 +0000161 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin2e7be612009-10-26 16:59:04 +0000162 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000163 const SUnit *PredSU = P->getSUnit();
David Goodwin2e7be612009-10-26 16:59:04 +0000164 unsigned PredLatency = P->getLatency();
165 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
166 // In the case of a latency tie, prefer an anti-dependency edge over
167 // other types of edges.
168 if (NextDepth < PredTotalLatency ||
169 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
170 NextDepth = PredTotalLatency;
171 Next = &*P;
172 }
173 }
174 return Next;
175}
176
177void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr *MI) {
Evan Cheng46df4eb2010-06-16 07:35:02 +0000178 // It's not safe to change register allocation for source operands of
179 // that have special allocation requirements. Also assume all registers
180 // used in a call must not be changed (ABI).
181 // FIXME: The issue with predicated instruction is more complex. We are being
182 // conservatively here because the kill markers cannot be trusted after
183 // if-conversion:
184 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
185 // ...
186 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
187 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
188 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
189 //
190 // The first R6 kill is not really a kill since it's killed by a predicated
191 // instruction which may not be executed. The second R6 def may or may not
192 // re-define R6 so it's not safe to change it since the last R6 use cannot be
193 // changed.
194 bool Special = MI->getDesc().isCall() ||
195 MI->getDesc().hasExtraSrcRegAllocReq() ||
196 TII->isPredicated(MI);
197
David Goodwin2e7be612009-10-26 16:59:04 +0000198 // Scan the register operands for this instruction and update
199 // Classes and RegRefs.
200 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
201 MachineOperand &MO = MI->getOperand(i);
202 if (!MO.isReg()) continue;
203 unsigned Reg = MO.getReg();
204 if (Reg == 0) continue;
205 const TargetRegisterClass *NewRC = 0;
Jim Grosbach01384ef2010-05-14 21:20:46 +0000206
David Goodwin2e7be612009-10-26 16:59:04 +0000207 if (i < MI->getDesc().getNumOperands())
208 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
209
210 // For now, only allow the register to be changed if its register
211 // class is consistent across all uses.
212 if (!Classes[Reg] && NewRC)
213 Classes[Reg] = NewRC;
214 else if (!NewRC || Classes[Reg] != NewRC)
215 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
216
217 // Now check for aliases.
218 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
219 // If an alias of the reg is used during the live range, give up.
220 // Note that this allows us to skip checking if AntiDepReg
221 // overlaps with any of the aliases, among other things.
222 unsigned AliasReg = *Alias;
223 if (Classes[AliasReg]) {
224 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
225 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
226 }
227 }
228
229 // If we're still willing to consider this register, note the reference.
230 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
231 RegRefs.insert(std::make_pair(Reg, &MO));
232
Evan Cheng46df4eb2010-06-16 07:35:02 +0000233 if (MO.isUse() && Special) {
David Goodwin2e7be612009-10-26 16:59:04 +0000234 if (KeepRegs.insert(Reg)) {
235 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
236 *Subreg; ++Subreg)
237 KeepRegs.insert(*Subreg);
238 }
239 }
240 }
241}
242
243void CriticalAntiDepBreaker::ScanInstruction(MachineInstr *MI,
244 unsigned Count) {
245 // Update liveness.
246 // Proceding upwards, registers that are defed but not used in this
247 // instruction are now dead.
David Goodwin2e7be612009-10-26 16:59:04 +0000248
Evan Cheng46df4eb2010-06-16 07:35:02 +0000249 if (!TII->isPredicated(MI)) {
250 // Predicated defs are modeled as read + write, i.e. similar to two
251 // address updates.
252 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
253 MachineOperand &MO = MI->getOperand(i);
254 if (!MO.isReg()) continue;
255 unsigned Reg = MO.getReg();
256 if (Reg == 0) continue;
257 if (!MO.isDef()) continue;
258 // Ignore two-addr defs.
259 if (MI->isRegTiedToUseOperand(i)) continue;
260
261 DefIndices[Reg] = Count;
262 KillIndices[Reg] = ~0u;
263 assert(((KillIndices[Reg] == ~0u) !=
264 (DefIndices[Reg] == ~0u)) &&
265 "Kill and Def maps aren't consistent for Reg!");
266 KeepRegs.erase(Reg);
267 Classes[Reg] = 0;
268 RegRefs.erase(Reg);
269 // Repeat, for all subregs.
270 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
271 *Subreg; ++Subreg) {
272 unsigned SubregReg = *Subreg;
273 DefIndices[SubregReg] = Count;
274 KillIndices[SubregReg] = ~0u;
275 KeepRegs.erase(SubregReg);
276 Classes[SubregReg] = 0;
277 RegRefs.erase(SubregReg);
278 }
279 // Conservatively mark super-registers as unusable.
280 for (const unsigned *Super = TRI->getSuperRegisters(Reg);
281 *Super; ++Super) {
282 unsigned SuperReg = *Super;
283 Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
284 }
David Goodwin2e7be612009-10-26 16:59:04 +0000285 }
286 }
287 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
288 MachineOperand &MO = MI->getOperand(i);
289 if (!MO.isReg()) continue;
290 unsigned Reg = MO.getReg();
291 if (Reg == 0) continue;
292 if (!MO.isUse()) continue;
293
294 const TargetRegisterClass *NewRC = 0;
295 if (i < MI->getDesc().getNumOperands())
296 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
297
298 // For now, only allow the register to be changed if its register
299 // class is consistent across all uses.
300 if (!Classes[Reg] && NewRC)
301 Classes[Reg] = NewRC;
302 else if (!NewRC || Classes[Reg] != NewRC)
303 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
304
305 RegRefs.insert(std::make_pair(Reg, &MO));
306
307 // It wasn't previously live but now it is, this is a kill.
308 if (KillIndices[Reg] == ~0u) {
309 KillIndices[Reg] = Count;
310 DefIndices[Reg] = ~0u;
311 assert(((KillIndices[Reg] == ~0u) !=
312 (DefIndices[Reg] == ~0u)) &&
313 "Kill and Def maps aren't consistent for Reg!");
314 }
315 // Repeat, for all aliases.
316 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
317 unsigned AliasReg = *Alias;
318 if (KillIndices[AliasReg] == ~0u) {
319 KillIndices[AliasReg] = Count;
320 DefIndices[AliasReg] = ~0u;
321 }
322 }
323 }
324}
325
326unsigned
Jim Grosbach80c2b0d2010-01-06 22:21:25 +0000327CriticalAntiDepBreaker::findSuitableFreeRegister(MachineInstr *MI,
328 unsigned AntiDepReg,
David Goodwin2e7be612009-10-26 16:59:04 +0000329 unsigned LastNewReg,
Jim Grosbach2973b572010-01-06 16:48:02 +0000330 const TargetRegisterClass *RC)
331{
David Goodwin2e7be612009-10-26 16:59:04 +0000332 for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF),
333 RE = RC->allocation_order_end(MF); R != RE; ++R) {
334 unsigned NewReg = *R;
335 // Don't replace a register with itself.
336 if (NewReg == AntiDepReg) continue;
337 // Don't replace a register with one that was recently used to repair
338 // an anti-dependence with this AntiDepReg, because that would
339 // re-introduce that anti-dependence.
340 if (NewReg == LastNewReg) continue;
Jim Grosbach80c2b0d2010-01-06 22:21:25 +0000341 // If the instruction already has a def of the NewReg, it's not suitable.
342 // For example, Instruction with multiple definitions can result in this
343 // condition.
344 if (MI->modifiesRegister(NewReg, TRI)) continue;
David Goodwin2e7be612009-10-26 16:59:04 +0000345 // If NewReg is dead and NewReg's most recent def is not before
346 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000347 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
348 && "Kill and Def maps aren't consistent for AntiDepReg!");
349 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
350 && "Kill and Def maps aren't consistent for NewReg!");
David Goodwin2e7be612009-10-26 16:59:04 +0000351 if (KillIndices[NewReg] != ~0u ||
352 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
353 KillIndices[AntiDepReg] > DefIndices[NewReg])
354 continue;
355 return NewReg;
356 }
357
358 // No registers are free and available!
359 return 0;
360}
361
362unsigned CriticalAntiDepBreaker::
Dan Gohman66db3a02010-04-19 23:11:58 +0000363BreakAntiDependencies(const std::vector<SUnit>& SUnits,
364 MachineBasicBlock::iterator Begin,
365 MachineBasicBlock::iterator End,
David Goodwin2e7be612009-10-26 16:59:04 +0000366 unsigned InsertPosIndex) {
367 // The code below assumes that there is at least one instruction,
368 // so just duck out immediately if the block is empty.
369 if (SUnits.empty()) return 0;
370
Jim Grosbach533934e2010-06-01 23:48:44 +0000371 // Keep a map of the MachineInstr*'s back to the SUnit representing them.
372 // This is used for updating debug information.
373 DenseMap<MachineInstr*,const SUnit*> MISUnitMap;
374
David Goodwin2e7be612009-10-26 16:59:04 +0000375 // Find the node at the bottom of the critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000376 const SUnit *Max = 0;
David Goodwin2e7be612009-10-26 16:59:04 +0000377 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000378 const SUnit *SU = &SUnits[i];
Jim Grosbach533934e2010-06-01 23:48:44 +0000379 MISUnitMap[SU->getInstr()] = SU;
David Goodwin2e7be612009-10-26 16:59:04 +0000380 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
381 Max = SU;
382 }
383
384#ifndef NDEBUG
385 {
David Greene89d6a242010-01-04 17:47:05 +0000386 DEBUG(dbgs() << "Critical path has total latency "
David Goodwin2e7be612009-10-26 16:59:04 +0000387 << (Max->getDepth() + Max->Latency) << "\n");
David Greene89d6a242010-01-04 17:47:05 +0000388 DEBUG(dbgs() << "Available regs:");
David Goodwin2e7be612009-10-26 16:59:04 +0000389 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
390 if (KillIndices[Reg] == ~0u)
David Greene89d6a242010-01-04 17:47:05 +0000391 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin2e7be612009-10-26 16:59:04 +0000392 }
David Greene89d6a242010-01-04 17:47:05 +0000393 DEBUG(dbgs() << '\n');
David Goodwin2e7be612009-10-26 16:59:04 +0000394 }
395#endif
396
397 // Track progress along the critical path through the SUnit graph as we walk
398 // the instructions.
Dan Gohman66db3a02010-04-19 23:11:58 +0000399 const SUnit *CriticalPathSU = Max;
David Goodwin2e7be612009-10-26 16:59:04 +0000400 MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
401
402 // Consider this pattern:
403 // A = ...
404 // ... = A
405 // A = ...
406 // ... = A
407 // A = ...
408 // ... = A
409 // A = ...
410 // ... = A
411 // There are three anti-dependencies here, and without special care,
412 // we'd break all of them using the same register:
413 // A = ...
414 // ... = A
415 // B = ...
416 // ... = B
417 // B = ...
418 // ... = B
419 // B = ...
420 // ... = B
421 // because at each anti-dependence, B is the first register that
422 // isn't A which is free. This re-introduces anti-dependencies
423 // at all but one of the original anti-dependencies that we were
424 // trying to break. To avoid this, keep track of the most recent
425 // register that each register was replaced with, avoid
426 // using it to repair an anti-dependence on the same register.
427 // This lets us produce this:
428 // A = ...
429 // ... = A
430 // B = ...
431 // ... = B
432 // C = ...
433 // ... = C
434 // B = ...
435 // ... = B
436 // This still has an anti-dependence on B, but at least it isn't on the
437 // original critical path.
438 //
439 // TODO: If we tracked more than one register here, we could potentially
440 // fix that remaining critical edge too. This is a little more involved,
441 // because unlike the most recent register, less recent registers should
442 // still be considered, though only if no other registers are available.
Bill Wendling9c2a0342010-07-15 19:58:14 +0000443 std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);
David Goodwin2e7be612009-10-26 16:59:04 +0000444
445 // Attempt to break anti-dependence edges on the critical path. Walk the
446 // instructions from the bottom up, tracking information about liveness
447 // as we go to help determine which registers are available.
448 unsigned Broken = 0;
449 unsigned Count = InsertPosIndex - 1;
450 for (MachineBasicBlock::iterator I = End, E = Begin;
451 I != E; --Count) {
452 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000453 if (MI->isDebugValue())
454 continue;
David Goodwin2e7be612009-10-26 16:59:04 +0000455
456 // Check if this instruction has a dependence on the critical path that
457 // is an anti-dependence that we may be able to break. If it is, set
458 // AntiDepReg to the non-zero register associated with the anti-dependence.
459 //
460 // We limit our attention to the critical path as a heuristic to avoid
461 // breaking anti-dependence edges that aren't going to significantly
462 // impact the overall schedule. There are a limited number of registers
463 // and we want to save them for the important edges.
Jim Grosbach01384ef2010-05-14 21:20:46 +0000464 //
David Goodwin2e7be612009-10-26 16:59:04 +0000465 // TODO: Instructions with multiple defs could have multiple
466 // anti-dependencies. The current code here only knows how to break one
467 // edge per instruction. Note that we'd have to be able to break all of
468 // the anti-dependencies in an instruction in order to be effective.
469 unsigned AntiDepReg = 0;
470 if (MI == CriticalPathMI) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000471 if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
472 const SUnit *NextSU = Edge->getSUnit();
David Goodwin2e7be612009-10-26 16:59:04 +0000473
474 // Only consider anti-dependence edges.
475 if (Edge->getKind() == SDep::Anti) {
476 AntiDepReg = Edge->getReg();
477 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
478 if (!AllocatableSet.test(AntiDepReg))
479 // Don't break anti-dependencies on non-allocatable registers.
480 AntiDepReg = 0;
481 else if (KeepRegs.count(AntiDepReg))
482 // Don't break anti-dependencies if an use down below requires
483 // this exact register.
484 AntiDepReg = 0;
485 else {
486 // If the SUnit has other dependencies on the SUnit that it
487 // anti-depends on, don't bother breaking the anti-dependency
488 // since those edges would prevent such units from being
489 // scheduled past each other regardless.
490 //
491 // Also, if there are dependencies on other SUnits with the
492 // same register as the anti-dependency, don't attempt to
493 // break it.
Dan Gohman66db3a02010-04-19 23:11:58 +0000494 for (SUnit::const_pred_iterator P = CriticalPathSU->Preds.begin(),
David Goodwin2e7be612009-10-26 16:59:04 +0000495 PE = CriticalPathSU->Preds.end(); P != PE; ++P)
496 if (P->getSUnit() == NextSU ?
497 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
498 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
499 AntiDepReg = 0;
500 break;
501 }
502 }
503 }
504 CriticalPathSU = NextSU;
505 CriticalPathMI = CriticalPathSU->getInstr();
506 } else {
507 // We've reached the end of the critical path.
508 CriticalPathSU = 0;
509 CriticalPathMI = 0;
510 }
511 }
512
513 PrescanInstruction(MI);
514
Evan Cheng46df4eb2010-06-16 07:35:02 +0000515 // If MI's defs have a special allocation requirement, don't allow
516 // any def registers to be changed. Also assume all registers
517 // defined in a call must not be changed (ABI).
518 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq() ||
519 TII->isPredicated(MI))
David Goodwin2e7be612009-10-26 16:59:04 +0000520 // If this instruction's defs have special allocation requirement, don't
521 // break this anti-dependency.
522 AntiDepReg = 0;
523 else if (AntiDepReg) {
524 // If this instruction has a use of AntiDepReg, breaking it
525 // is invalid.
526 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
527 MachineOperand &MO = MI->getOperand(i);
528 if (!MO.isReg()) continue;
529 unsigned Reg = MO.getReg();
530 if (Reg == 0) continue;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000531 if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
David Goodwin2e7be612009-10-26 16:59:04 +0000532 AntiDepReg = 0;
533 break;
534 }
535 }
536 }
537
538 // Determine AntiDepReg's register class, if it is live and is
539 // consistently used within a single class.
540 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
541 assert((AntiDepReg == 0 || RC != NULL) &&
542 "Register should be live if it's causing an anti-dependence!");
543 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
544 AntiDepReg = 0;
545
546 // Look for a suitable register to use to break the anti-depenence.
547 //
548 // TODO: Instead of picking the first free register, consider which might
549 // be the best.
550 if (AntiDepReg != 0) {
Jim Grosbach80c2b0d2010-01-06 22:21:25 +0000551 if (unsigned NewReg = findSuitableFreeRegister(MI, AntiDepReg,
David Goodwin2e7be612009-10-26 16:59:04 +0000552 LastNewReg[AntiDepReg],
553 RC)) {
David Greene89d6a242010-01-04 17:47:05 +0000554 DEBUG(dbgs() << "Breaking anti-dependence edge on "
David Goodwin2e7be612009-10-26 16:59:04 +0000555 << TRI->getName(AntiDepReg)
556 << " with " << RegRefs.count(AntiDepReg) << " references"
557 << " using " << TRI->getName(NewReg) << "!\n");
558
559 // Update the references to the old register to refer to the new
560 // register.
561 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
562 std::multimap<unsigned, MachineOperand *>::iterator>
563 Range = RegRefs.equal_range(AntiDepReg);
564 for (std::multimap<unsigned, MachineOperand *>::iterator
Jim Grosbach533934e2010-06-01 23:48:44 +0000565 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
David Goodwin2e7be612009-10-26 16:59:04 +0000566 Q->second->setReg(NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000567 // If the SU for the instruction being updated has debug information
568 // related to the anti-dependency register, make sure to update that
569 // as well.
570 const SUnit *SU = MISUnitMap[Q->second->getParent()];
Jim Grosbach086723d2010-06-02 15:29:36 +0000571 if (!SU) continue;
Jim Grosbach533934e2010-06-01 23:48:44 +0000572 for (unsigned i = 0, e = SU->DbgInstrList.size() ; i < e ; ++i) {
573 MachineInstr *DI = SU->DbgInstrList[i];
574 assert (DI->getNumOperands()==3 && DI->getOperand(0).isReg() &&
575 DI->getOperand(0).getReg()
576 && "Non register dbg_value attached to SUnit!");
577 if (DI->getOperand(0).getReg() == AntiDepReg)
578 DI->getOperand(0).setReg(NewReg);
579 }
580 }
David Goodwin2e7be612009-10-26 16:59:04 +0000581
582 // We just went back in time and modified history; the
583 // liveness information for the anti-depenence reg is now
584 // inconsistent. Set the state as if it were dead.
585 Classes[NewReg] = Classes[AntiDepReg];
586 DefIndices[NewReg] = DefIndices[AntiDepReg];
587 KillIndices[NewReg] = KillIndices[AntiDepReg];
588 assert(((KillIndices[NewReg] == ~0u) !=
589 (DefIndices[NewReg] == ~0u)) &&
590 "Kill and Def maps aren't consistent for NewReg!");
591
592 Classes[AntiDepReg] = 0;
593 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
594 KillIndices[AntiDepReg] = ~0u;
595 assert(((KillIndices[AntiDepReg] == ~0u) !=
596 (DefIndices[AntiDepReg] == ~0u)) &&
597 "Kill and Def maps aren't consistent for AntiDepReg!");
598
599 RegRefs.erase(AntiDepReg);
600 LastNewReg[AntiDepReg] = NewReg;
601 ++Broken;
602 }
603 }
604
605 ScanInstruction(MI, Count);
606 }
607
608 return Broken;
609}