blob: 7b2ce3624170d24e99698a04033c3d1935a036b4 [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) {
David Goodwin990d2852009-12-09 17:18:22 +000044 const unsigned BBSize = BB->size();
Bill Wendling9c2a0342010-07-15 19:58:14 +000045 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
46 // Clear out the register class data.
47 Classes[i] = static_cast<const TargetRegisterClass *>(0);
48
49 // Initialize the indices to indicate that no registers are live.
David Goodwin990d2852009-12-09 17:18:22 +000050 KillIndices[i] = ~0u;
51 DefIndices[i] = BBSize;
52 }
David Goodwin2e7be612009-10-26 16:59:04 +000053
54 // Clear "do not change" set.
55 KeepRegs.clear();
56
57 bool IsReturnBlock = (!BB->empty() && BB->back().getDesc().isReturn());
58
59 // Determine the live-out physregs for this block.
60 if (IsReturnBlock) {
61 // In a return block, examine the function live-out regs.
62 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
63 E = MRI.liveout_end(); I != E; ++I) {
64 unsigned Reg = *I;
65 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
66 KillIndices[Reg] = BB->size();
67 DefIndices[Reg] = ~0u;
Bill Wendling9c2a0342010-07-15 19:58:14 +000068
David Goodwin2e7be612009-10-26 16:59:04 +000069 // Repeat, for all aliases.
70 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
71 unsigned AliasReg = *Alias;
72 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
73 KillIndices[AliasReg] = BB->size();
74 DefIndices[AliasReg] = ~0u;
75 }
76 }
David Goodwin2e7be612009-10-26 16:59:04 +000077 }
78
Evan Cheng46df4eb2010-06-16 07:35:02 +000079 // In a non-return block, examine the live-in regs of all successors.
80 // Note a return block can have successors if the return instruction is
81 // predicated.
82 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
83 SE = BB->succ_end(); SI != SE; ++SI)
84 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
85 E = (*SI)->livein_end(); I != E; ++I) {
86 unsigned Reg = *I;
87 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
88 KillIndices[Reg] = BB->size();
89 DefIndices[Reg] = ~0u;
Bill Wendling9c2a0342010-07-15 19:58:14 +000090
Evan Cheng46df4eb2010-06-16 07:35:02 +000091 // 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;
Bill Wendling9c2a0342010-07-15 19:58:14 +0000111
David Goodwin2e7be612009-10-26 16:59:04 +0000112 // Repeat, for all aliases.
113 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
114 unsigned AliasReg = *Alias;
115 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
116 KillIndices[AliasReg] = BB->size();
117 DefIndices[AliasReg] = ~0u;
118 }
119 }
120}
121
122void CriticalAntiDepBreaker::FinishBlock() {
123 RegRefs.clear();
124 KeepRegs.clear();
125}
126
127void CriticalAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
128 unsigned InsertPosIndex) {
Dale Johannesenb0812f12010-03-05 00:02:59 +0000129 if (MI->isDebugValue())
130 return;
David Goodwin2e7be612009-10-26 16:59:04 +0000131 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
132
Bob Wilsonf70007e2010-10-02 01:49:29 +0000133 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
134 if (KillIndices[Reg] != ~0u) {
135 // If Reg is currently live, then mark that it can't be renamed as
136 // we don't know the extent of its live-range anymore (now that it
137 // has been scheduled).
138 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
139 KillIndices[Reg] = Count;
140 } else if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
141 // Any register which was defined within the previous scheduling region
142 // may have been rescheduled and its lifetime may overlap with registers
143 // in ways not reflected in our current liveness state. For each such
144 // register, adjust the liveness state to be conservatively correct.
David Goodwin2e7be612009-10-26 16:59:04 +0000145 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
Bill Wendling9c2a0342010-07-15 19:58:14 +0000146
David Goodwin2e7be612009-10-26 16:59:04 +0000147 // Move the def index to the end of the previous region, to reflect
148 // that the def could theoretically have been scheduled at the end.
149 DefIndices[Reg] = InsertPosIndex;
150 }
Bob Wilsonf70007e2010-10-02 01:49:29 +0000151 }
David Goodwin2e7be612009-10-26 16:59:04 +0000152
153 PrescanInstruction(MI);
154 ScanInstruction(MI, Count);
155}
156
157/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
158/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000159static const SDep *CriticalPathStep(const SUnit *SU) {
160 const SDep *Next = 0;
David Goodwin2e7be612009-10-26 16:59:04 +0000161 unsigned NextDepth = 0;
162 // Find the predecessor edge with the greatest depth.
Dan Gohman66db3a02010-04-19 23:11:58 +0000163 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin2e7be612009-10-26 16:59:04 +0000164 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000165 const SUnit *PredSU = P->getSUnit();
David Goodwin2e7be612009-10-26 16:59:04 +0000166 unsigned PredLatency = P->getLatency();
167 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
168 // In the case of a latency tie, prefer an anti-dependency edge over
169 // other types of edges.
170 if (NextDepth < PredTotalLatency ||
171 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
172 NextDepth = PredTotalLatency;
173 Next = &*P;
174 }
175 }
176 return Next;
177}
178
179void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr *MI) {
Evan Cheng46df4eb2010-06-16 07:35:02 +0000180 // It's not safe to change register allocation for source operands of
181 // that have special allocation requirements. Also assume all registers
182 // used in a call must not be changed (ABI).
183 // FIXME: The issue with predicated instruction is more complex. We are being
Bob Wilson59718a42010-09-10 22:42:21 +0000184 // conservative here because the kill markers cannot be trusted after
Evan Cheng46df4eb2010-06-16 07:35:02 +0000185 // if-conversion:
186 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
187 // ...
188 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
189 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
190 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
191 //
192 // The first R6 kill is not really a kill since it's killed by a predicated
193 // instruction which may not be executed. The second R6 def may or may not
194 // re-define R6 so it's not safe to change it since the last R6 use cannot be
195 // changed.
196 bool Special = MI->getDesc().isCall() ||
197 MI->getDesc().hasExtraSrcRegAllocReq() ||
198 TII->isPredicated(MI);
199
David Goodwin2e7be612009-10-26 16:59:04 +0000200 // Scan the register operands for this instruction and update
201 // Classes and RegRefs.
202 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
203 MachineOperand &MO = MI->getOperand(i);
204 if (!MO.isReg()) continue;
205 unsigned Reg = MO.getReg();
206 if (Reg == 0) continue;
207 const TargetRegisterClass *NewRC = 0;
Jim Grosbach01384ef2010-05-14 21:20:46 +0000208
David Goodwin2e7be612009-10-26 16:59:04 +0000209 if (i < MI->getDesc().getNumOperands())
210 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
211
212 // For now, only allow the register to be changed if its register
213 // class is consistent across all uses.
214 if (!Classes[Reg] && NewRC)
215 Classes[Reg] = NewRC;
216 else if (!NewRC || Classes[Reg] != NewRC)
217 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
218
219 // Now check for aliases.
220 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
221 // If an alias of the reg is used during the live range, give up.
222 // Note that this allows us to skip checking if AntiDepReg
223 // overlaps with any of the aliases, among other things.
224 unsigned AliasReg = *Alias;
225 if (Classes[AliasReg]) {
226 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
227 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
228 }
229 }
230
231 // If we're still willing to consider this register, note the reference.
232 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
233 RegRefs.insert(std::make_pair(Reg, &MO));
234
Evan Cheng46df4eb2010-06-16 07:35:02 +0000235 if (MO.isUse() && Special) {
David Goodwin2e7be612009-10-26 16:59:04 +0000236 if (KeepRegs.insert(Reg)) {
237 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
238 *Subreg; ++Subreg)
239 KeepRegs.insert(*Subreg);
240 }
241 }
242 }
243}
244
245void CriticalAntiDepBreaker::ScanInstruction(MachineInstr *MI,
246 unsigned Count) {
247 // Update liveness.
248 // Proceding upwards, registers that are defed but not used in this
249 // instruction are now dead.
David Goodwin2e7be612009-10-26 16:59:04 +0000250
Evan Cheng46df4eb2010-06-16 07:35:02 +0000251 if (!TII->isPredicated(MI)) {
252 // Predicated defs are modeled as read + write, i.e. similar to two
253 // address updates.
254 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
255 MachineOperand &MO = MI->getOperand(i);
256 if (!MO.isReg()) continue;
257 unsigned Reg = MO.getReg();
258 if (Reg == 0) continue;
259 if (!MO.isDef()) continue;
260 // Ignore two-addr defs.
261 if (MI->isRegTiedToUseOperand(i)) continue;
262
263 DefIndices[Reg] = Count;
264 KillIndices[Reg] = ~0u;
265 assert(((KillIndices[Reg] == ~0u) !=
266 (DefIndices[Reg] == ~0u)) &&
267 "Kill and Def maps aren't consistent for Reg!");
268 KeepRegs.erase(Reg);
269 Classes[Reg] = 0;
270 RegRefs.erase(Reg);
271 // Repeat, for all subregs.
272 for (const unsigned *Subreg = TRI->getSubRegisters(Reg);
273 *Subreg; ++Subreg) {
274 unsigned SubregReg = *Subreg;
275 DefIndices[SubregReg] = Count;
276 KillIndices[SubregReg] = ~0u;
277 KeepRegs.erase(SubregReg);
278 Classes[SubregReg] = 0;
279 RegRefs.erase(SubregReg);
280 }
281 // Conservatively mark super-registers as unusable.
282 for (const unsigned *Super = TRI->getSuperRegisters(Reg);
283 *Super; ++Super) {
284 unsigned SuperReg = *Super;
285 Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
286 }
David Goodwin2e7be612009-10-26 16:59:04 +0000287 }
288 }
289 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
290 MachineOperand &MO = MI->getOperand(i);
291 if (!MO.isReg()) continue;
292 unsigned Reg = MO.getReg();
293 if (Reg == 0) continue;
294 if (!MO.isUse()) continue;
295
296 const TargetRegisterClass *NewRC = 0;
297 if (i < MI->getDesc().getNumOperands())
298 NewRC = MI->getDesc().OpInfo[i].getRegClass(TRI);
299
300 // For now, only allow the register to be changed if its register
301 // class is consistent across all uses.
302 if (!Classes[Reg] && NewRC)
303 Classes[Reg] = NewRC;
304 else if (!NewRC || Classes[Reg] != NewRC)
305 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
306
307 RegRefs.insert(std::make_pair(Reg, &MO));
308
309 // It wasn't previously live but now it is, this is a kill.
310 if (KillIndices[Reg] == ~0u) {
311 KillIndices[Reg] = Count;
312 DefIndices[Reg] = ~0u;
313 assert(((KillIndices[Reg] == ~0u) !=
314 (DefIndices[Reg] == ~0u)) &&
315 "Kill and Def maps aren't consistent for Reg!");
316 }
317 // Repeat, for all aliases.
318 for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
319 unsigned AliasReg = *Alias;
320 if (KillIndices[AliasReg] == ~0u) {
321 KillIndices[AliasReg] = Count;
322 DefIndices[AliasReg] = ~0u;
323 }
324 }
325 }
326}
327
Andrew Trick46388522010-11-02 18:16:45 +0000328// Check all machine instructions that define the antidependent register.
329// Return true if any of these instructions define the new register.
330bool
331CriticalAntiDepBreaker::isNewRegModifiedByRefs(RegRefIter RegRefBegin,
332 RegRefIter RegRefEnd,
333 unsigned NewReg)
334{
335 for (RegRefIter I = RegRefBegin; I != RegRefEnd; ++I ) {
336 MachineOperand *MO = I->second;
Andrew Trick46388522010-11-02 18:16:45 +0000337 if (MO->getParent()->modifiesRegister(NewReg, TRI))
338 return true;
339 }
340 return false;
341}
342
David Goodwin2e7be612009-10-26 16:59:04 +0000343unsigned
Andrew Trick46388522010-11-02 18:16:45 +0000344CriticalAntiDepBreaker::findSuitableFreeRegister(RegRefIter RegRefBegin,
345 RegRefIter RegRefEnd,
Jim Grosbach80c2b0d2010-01-06 22:21:25 +0000346 unsigned AntiDepReg,
David Goodwin2e7be612009-10-26 16:59:04 +0000347 unsigned LastNewReg,
Jim Grosbach2973b572010-01-06 16:48:02 +0000348 const TargetRegisterClass *RC)
349{
David Goodwin2e7be612009-10-26 16:59:04 +0000350 for (TargetRegisterClass::iterator R = RC->allocation_order_begin(MF),
351 RE = RC->allocation_order_end(MF); R != RE; ++R) {
352 unsigned NewReg = *R;
Jim Grosbach9b041c92010-09-02 17:12:55 +0000353 // Don't consider non-allocatable registers
354 if (!AllocatableSet.test(NewReg)) continue;
David Goodwin2e7be612009-10-26 16:59:04 +0000355 // Don't replace a register with itself.
356 if (NewReg == AntiDepReg) continue;
357 // Don't replace a register with one that was recently used to repair
358 // an anti-dependence with this AntiDepReg, because that would
359 // re-introduce that anti-dependence.
360 if (NewReg == LastNewReg) continue;
Andrew Trick46388522010-11-02 18:16:45 +0000361 // If any instructions that define AntiDepReg also define the NewReg, it's
362 // not suitable. For example, Instruction with multiple definitions can
363 // result in this condition.
364 if (isNewRegModifiedByRefs(RegRefBegin, RegRefEnd, NewReg)) continue;
David Goodwin2e7be612009-10-26 16:59:04 +0000365 // If NewReg is dead and NewReg's most recent def is not before
366 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000367 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
368 && "Kill and Def maps aren't consistent for AntiDepReg!");
369 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
370 && "Kill and Def maps aren't consistent for NewReg!");
David Goodwin2e7be612009-10-26 16:59:04 +0000371 if (KillIndices[NewReg] != ~0u ||
372 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
373 KillIndices[AntiDepReg] > DefIndices[NewReg])
374 continue;
375 return NewReg;
376 }
377
378 // No registers are free and available!
379 return 0;
380}
381
382unsigned CriticalAntiDepBreaker::
Dan Gohman66db3a02010-04-19 23:11:58 +0000383BreakAntiDependencies(const std::vector<SUnit>& SUnits,
384 MachineBasicBlock::iterator Begin,
385 MachineBasicBlock::iterator End,
David Goodwin2e7be612009-10-26 16:59:04 +0000386 unsigned InsertPosIndex) {
387 // The code below assumes that there is at least one instruction,
388 // so just duck out immediately if the block is empty.
389 if (SUnits.empty()) return 0;
390
Jim Grosbach533934e2010-06-01 23:48:44 +0000391 // Keep a map of the MachineInstr*'s back to the SUnit representing them.
392 // This is used for updating debug information.
393 DenseMap<MachineInstr*,const SUnit*> MISUnitMap;
394
David Goodwin2e7be612009-10-26 16:59:04 +0000395 // Find the node at the bottom of the critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000396 const SUnit *Max = 0;
David Goodwin2e7be612009-10-26 16:59:04 +0000397 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000398 const SUnit *SU = &SUnits[i];
Jim Grosbach533934e2010-06-01 23:48:44 +0000399 MISUnitMap[SU->getInstr()] = SU;
David Goodwin2e7be612009-10-26 16:59:04 +0000400 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
401 Max = SU;
402 }
403
404#ifndef NDEBUG
405 {
David Greene89d6a242010-01-04 17:47:05 +0000406 DEBUG(dbgs() << "Critical path has total latency "
David Goodwin2e7be612009-10-26 16:59:04 +0000407 << (Max->getDepth() + Max->Latency) << "\n");
David Greene89d6a242010-01-04 17:47:05 +0000408 DEBUG(dbgs() << "Available regs:");
David Goodwin2e7be612009-10-26 16:59:04 +0000409 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
410 if (KillIndices[Reg] == ~0u)
David Greene89d6a242010-01-04 17:47:05 +0000411 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin2e7be612009-10-26 16:59:04 +0000412 }
David Greene89d6a242010-01-04 17:47:05 +0000413 DEBUG(dbgs() << '\n');
David Goodwin2e7be612009-10-26 16:59:04 +0000414 }
415#endif
416
417 // Track progress along the critical path through the SUnit graph as we walk
418 // the instructions.
Dan Gohman66db3a02010-04-19 23:11:58 +0000419 const SUnit *CriticalPathSU = Max;
David Goodwin2e7be612009-10-26 16:59:04 +0000420 MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
421
422 // Consider this pattern:
423 // A = ...
424 // ... = A
425 // A = ...
426 // ... = A
427 // A = ...
428 // ... = A
429 // A = ...
430 // ... = A
431 // There are three anti-dependencies here, and without special care,
432 // we'd break all of them using the same register:
433 // A = ...
434 // ... = A
435 // B = ...
436 // ... = B
437 // B = ...
438 // ... = B
439 // B = ...
440 // ... = B
441 // because at each anti-dependence, B is the first register that
442 // isn't A which is free. This re-introduces anti-dependencies
443 // at all but one of the original anti-dependencies that we were
444 // trying to break. To avoid this, keep track of the most recent
445 // register that each register was replaced with, avoid
446 // using it to repair an anti-dependence on the same register.
447 // This lets us produce this:
448 // A = ...
449 // ... = A
450 // B = ...
451 // ... = B
452 // C = ...
453 // ... = C
454 // B = ...
455 // ... = B
456 // This still has an anti-dependence on B, but at least it isn't on the
457 // original critical path.
458 //
459 // TODO: If we tracked more than one register here, we could potentially
460 // fix that remaining critical edge too. This is a little more involved,
461 // because unlike the most recent register, less recent registers should
462 // still be considered, though only if no other registers are available.
Bill Wendling9c2a0342010-07-15 19:58:14 +0000463 std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);
David Goodwin2e7be612009-10-26 16:59:04 +0000464
465 // Attempt to break anti-dependence edges on the critical path. Walk the
466 // instructions from the bottom up, tracking information about liveness
467 // as we go to help determine which registers are available.
468 unsigned Broken = 0;
469 unsigned Count = InsertPosIndex - 1;
470 for (MachineBasicBlock::iterator I = End, E = Begin;
471 I != E; --Count) {
472 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000473 if (MI->isDebugValue())
474 continue;
David Goodwin2e7be612009-10-26 16:59:04 +0000475
476 // Check if this instruction has a dependence on the critical path that
477 // is an anti-dependence that we may be able to break. If it is, set
478 // AntiDepReg to the non-zero register associated with the anti-dependence.
479 //
480 // We limit our attention to the critical path as a heuristic to avoid
481 // breaking anti-dependence edges that aren't going to significantly
482 // impact the overall schedule. There are a limited number of registers
483 // and we want to save them for the important edges.
Jim Grosbach01384ef2010-05-14 21:20:46 +0000484 //
David Goodwin2e7be612009-10-26 16:59:04 +0000485 // TODO: Instructions with multiple defs could have multiple
486 // anti-dependencies. The current code here only knows how to break one
487 // edge per instruction. Note that we'd have to be able to break all of
488 // the anti-dependencies in an instruction in order to be effective.
489 unsigned AntiDepReg = 0;
490 if (MI == CriticalPathMI) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000491 if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
492 const SUnit *NextSU = Edge->getSUnit();
David Goodwin2e7be612009-10-26 16:59:04 +0000493
494 // Only consider anti-dependence edges.
495 if (Edge->getKind() == SDep::Anti) {
496 AntiDepReg = Edge->getReg();
497 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
498 if (!AllocatableSet.test(AntiDepReg))
499 // Don't break anti-dependencies on non-allocatable registers.
500 AntiDepReg = 0;
501 else if (KeepRegs.count(AntiDepReg))
502 // Don't break anti-dependencies if an use down below requires
503 // this exact register.
504 AntiDepReg = 0;
505 else {
506 // If the SUnit has other dependencies on the SUnit that it
507 // anti-depends on, don't bother breaking the anti-dependency
508 // since those edges would prevent such units from being
509 // scheduled past each other regardless.
510 //
511 // Also, if there are dependencies on other SUnits with the
512 // same register as the anti-dependency, don't attempt to
513 // break it.
Dan Gohman66db3a02010-04-19 23:11:58 +0000514 for (SUnit::const_pred_iterator P = CriticalPathSU->Preds.begin(),
David Goodwin2e7be612009-10-26 16:59:04 +0000515 PE = CriticalPathSU->Preds.end(); P != PE; ++P)
516 if (P->getSUnit() == NextSU ?
517 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
518 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
519 AntiDepReg = 0;
520 break;
521 }
522 }
523 }
524 CriticalPathSU = NextSU;
525 CriticalPathMI = CriticalPathSU->getInstr();
526 } else {
527 // We've reached the end of the critical path.
528 CriticalPathSU = 0;
529 CriticalPathMI = 0;
530 }
531 }
532
533 PrescanInstruction(MI);
534
Evan Cheng46df4eb2010-06-16 07:35:02 +0000535 // If MI's defs have a special allocation requirement, don't allow
536 // any def registers to be changed. Also assume all registers
537 // defined in a call must not be changed (ABI).
538 if (MI->getDesc().isCall() || MI->getDesc().hasExtraDefRegAllocReq() ||
539 TII->isPredicated(MI))
David Goodwin2e7be612009-10-26 16:59:04 +0000540 // If this instruction's defs have special allocation requirement, don't
541 // break this anti-dependency.
542 AntiDepReg = 0;
543 else if (AntiDepReg) {
544 // If this instruction has a use of AntiDepReg, breaking it
545 // is invalid.
546 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
547 MachineOperand &MO = MI->getOperand(i);
548 if (!MO.isReg()) continue;
549 unsigned Reg = MO.getReg();
550 if (Reg == 0) continue;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000551 if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
David Goodwin2e7be612009-10-26 16:59:04 +0000552 AntiDepReg = 0;
553 break;
554 }
555 }
556 }
557
558 // Determine AntiDepReg's register class, if it is live and is
559 // consistently used within a single class.
560 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
561 assert((AntiDepReg == 0 || RC != NULL) &&
562 "Register should be live if it's causing an anti-dependence!");
563 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
564 AntiDepReg = 0;
565
566 // Look for a suitable register to use to break the anti-depenence.
567 //
568 // TODO: Instead of picking the first free register, consider which might
569 // be the best.
570 if (AntiDepReg != 0) {
Andrew Trick46388522010-11-02 18:16:45 +0000571 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
572 std::multimap<unsigned, MachineOperand *>::iterator>
573 Range = RegRefs.equal_range(AntiDepReg);
574 if (unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,
575 AntiDepReg,
David Goodwin2e7be612009-10-26 16:59:04 +0000576 LastNewReg[AntiDepReg],
577 RC)) {
David Greene89d6a242010-01-04 17:47:05 +0000578 DEBUG(dbgs() << "Breaking anti-dependence edge on "
David Goodwin2e7be612009-10-26 16:59:04 +0000579 << TRI->getName(AntiDepReg)
580 << " with " << RegRefs.count(AntiDepReg) << " references"
581 << " using " << TRI->getName(NewReg) << "!\n");
582
583 // Update the references to the old register to refer to the new
584 // register.
David Goodwin2e7be612009-10-26 16:59:04 +0000585 for (std::multimap<unsigned, MachineOperand *>::iterator
Jim Grosbach533934e2010-06-01 23:48:44 +0000586 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
David Goodwin2e7be612009-10-26 16:59:04 +0000587 Q->second->setReg(NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000588 // If the SU for the instruction being updated has debug information
589 // related to the anti-dependency register, make sure to update that
590 // as well.
591 const SUnit *SU = MISUnitMap[Q->second->getParent()];
Jim Grosbach086723d2010-06-02 15:29:36 +0000592 if (!SU) continue;
Jim Grosbach533934e2010-06-01 23:48:44 +0000593 for (unsigned i = 0, e = SU->DbgInstrList.size() ; i < e ; ++i) {
594 MachineInstr *DI = SU->DbgInstrList[i];
595 assert (DI->getNumOperands()==3 && DI->getOperand(0).isReg() &&
596 DI->getOperand(0).getReg()
597 && "Non register dbg_value attached to SUnit!");
598 if (DI->getOperand(0).getReg() == AntiDepReg)
599 DI->getOperand(0).setReg(NewReg);
600 }
601 }
David Goodwin2e7be612009-10-26 16:59:04 +0000602
603 // We just went back in time and modified history; the
Bob Wilsonf70007e2010-10-02 01:49:29 +0000604 // liveness information for the anti-dependence reg is now
David Goodwin2e7be612009-10-26 16:59:04 +0000605 // inconsistent. Set the state as if it were dead.
606 Classes[NewReg] = Classes[AntiDepReg];
607 DefIndices[NewReg] = DefIndices[AntiDepReg];
608 KillIndices[NewReg] = KillIndices[AntiDepReg];
609 assert(((KillIndices[NewReg] == ~0u) !=
610 (DefIndices[NewReg] == ~0u)) &&
611 "Kill and Def maps aren't consistent for NewReg!");
612
613 Classes[AntiDepReg] = 0;
614 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
615 KillIndices[AntiDepReg] = ~0u;
616 assert(((KillIndices[AntiDepReg] == ~0u) !=
617 (DefIndices[AntiDepReg] == ~0u)) &&
618 "Kill and Def maps aren't consistent for AntiDepReg!");
619
620 RegRefs.erase(AntiDepReg);
621 LastNewReg[AntiDepReg] = NewReg;
622 ++Broken;
623 }
624 }
625
626 ScanInstruction(MI, Count);
627 }
628
629 return Broken;
630}