blob: 3a7145c57320d4e1d91bc8d0bf181f8f2ee0e2a7 [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::
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000030CriticalAntiDepBreaker(MachineFunction& MFi, const RegisterClassInfo &RCI) :
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()),
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000035 RegClassInfo(RCI),
Bill Wendling9c2a0342010-07-15 19:58:14 +000036 Classes(TRI->getNumRegs(), static_cast<const TargetRegisterClass *>(0)),
37 KillIndices(TRI->getNumRegs(), 0),
Benjamin Kramercff4ad72012-03-17 20:22:57 +000038 DefIndices(TRI->getNumRegs(), 0),
39 KeepRegs(TRI->getNumRegs(), false) {}
David Goodwin2e7be612009-10-26 16:59:04 +000040
41CriticalAntiDepBreaker::~CriticalAntiDepBreaker() {
42}
43
44void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
David Goodwin990d2852009-12-09 17:18:22 +000045 const unsigned BBSize = BB->size();
Bill Wendling9c2a0342010-07-15 19:58:14 +000046 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
47 // Clear out the register class data.
48 Classes[i] = static_cast<const TargetRegisterClass *>(0);
49
50 // Initialize the indices to indicate that no registers are live.
David Goodwin990d2852009-12-09 17:18:22 +000051 KillIndices[i] = ~0u;
52 DefIndices[i] = BBSize;
53 }
David Goodwin2e7be612009-10-26 16:59:04 +000054
55 // Clear "do not change" set.
Benjamin Kramercff4ad72012-03-17 20:22:57 +000056 KeepRegs.reset();
David Goodwin2e7be612009-10-26 16:59:04 +000057
Benjamin Kramer87f3dbc2012-03-16 15:46:47 +000058 bool IsReturnBlock = (BBSize != 0 && BB->back().isReturn());
David Goodwin2e7be612009-10-26 16:59:04 +000059
60 // Determine the live-out physregs for this block.
61 if (IsReturnBlock) {
62 // In a return block, examine the function live-out regs.
63 for (MachineRegisterInfo::liveout_iterator I = MRI.liveout_begin(),
64 E = MRI.liveout_end(); I != E; ++I) {
65 unsigned Reg = *I;
66 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
Benjamin Kramer87f3dbc2012-03-16 15:46:47 +000067 KillIndices[Reg] = BBSize;
David Goodwin2e7be612009-10-26 16:59:04 +000068 DefIndices[Reg] = ~0u;
Bill Wendling9c2a0342010-07-15 19:58:14 +000069
David Goodwin2e7be612009-10-26 16:59:04 +000070 // Repeat, for all aliases.
Craig Toppere4fd9072012-03-04 10:43:23 +000071 for (const uint16_t *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
David Goodwin2e7be612009-10-26 16:59:04 +000072 unsigned AliasReg = *Alias;
73 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
Benjamin Kramer87f3dbc2012-03-16 15:46:47 +000074 KillIndices[AliasReg] = BBSize;
David Goodwin2e7be612009-10-26 16:59:04 +000075 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);
Benjamin Kramer87f3dbc2012-03-16 15:46:47 +000089 KillIndices[Reg] = BBSize;
Evan Cheng46df4eb2010-06-16 07:35:02 +000090 DefIndices[Reg] = ~0u;
Bill Wendling9c2a0342010-07-15 19:58:14 +000091
Evan Cheng46df4eb2010-06-16 07:35:02 +000092 // Repeat, for all aliases.
Craig Toppere4fd9072012-03-04 10:43:23 +000093 for (const uint16_t *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
Evan Cheng46df4eb2010-06-16 07:35:02 +000094 unsigned AliasReg = *Alias;
95 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
Benjamin Kramer87f3dbc2012-03-16 15:46:47 +000096 KillIndices[AliasReg] = BBSize;
Evan Cheng46df4eb2010-06-16 07:35:02 +000097 DefIndices[AliasReg] = ~0u;
98 }
99 }
100
David Goodwin2e7be612009-10-26 16:59:04 +0000101 // Mark live-out callee-saved registers. In a return block this is
102 // all callee-saved registers. In non-return this is any
103 // callee-saved register that is not saved in the prolog.
104 const MachineFrameInfo *MFI = MF.getFrameInfo();
105 BitVector Pristine = MFI->getPristineRegs(BB);
Craig Topper015f2282012-03-04 03:33:22 +0000106 for (const uint16_t *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
David Goodwin2e7be612009-10-26 16:59:04 +0000107 unsigned Reg = *I;
108 if (!IsReturnBlock && !Pristine.test(Reg)) continue;
109 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
Benjamin Kramer87f3dbc2012-03-16 15:46:47 +0000110 KillIndices[Reg] = BBSize;
David Goodwin2e7be612009-10-26 16:59:04 +0000111 DefIndices[Reg] = ~0u;
Bill Wendling9c2a0342010-07-15 19:58:14 +0000112
David Goodwin2e7be612009-10-26 16:59:04 +0000113 // Repeat, for all aliases.
Craig Toppere4fd9072012-03-04 10:43:23 +0000114 for (const uint16_t *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
David Goodwin2e7be612009-10-26 16:59:04 +0000115 unsigned AliasReg = *Alias;
116 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
Benjamin Kramer87f3dbc2012-03-16 15:46:47 +0000117 KillIndices[AliasReg] = BBSize;
David Goodwin2e7be612009-10-26 16:59:04 +0000118 DefIndices[AliasReg] = ~0u;
119 }
120 }
121}
122
123void CriticalAntiDepBreaker::FinishBlock() {
124 RegRefs.clear();
Benjamin Kramercff4ad72012-03-17 20:22:57 +0000125 KeepRegs.reset();
David Goodwin2e7be612009-10-26 16:59:04 +0000126}
127
128void CriticalAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
129 unsigned InsertPosIndex) {
Dale Johannesenb0812f12010-03-05 00:02:59 +0000130 if (MI->isDebugValue())
131 return;
David Goodwin2e7be612009-10-26 16:59:04 +0000132 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
133
Bob Wilsonf70007e2010-10-02 01:49:29 +0000134 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
135 if (KillIndices[Reg] != ~0u) {
136 // If Reg is currently live, then mark that it can't be renamed as
137 // we don't know the extent of its live-range anymore (now that it
138 // has been scheduled).
139 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
140 KillIndices[Reg] = Count;
141 } else if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
142 // Any register which was defined within the previous scheduling region
143 // may have been rescheduled and its lifetime may overlap with registers
144 // in ways not reflected in our current liveness state. For each such
145 // register, adjust the liveness state to be conservatively correct.
David Goodwin2e7be612009-10-26 16:59:04 +0000146 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
Bill Wendling9c2a0342010-07-15 19:58:14 +0000147
David Goodwin2e7be612009-10-26 16:59:04 +0000148 // Move the def index to the end of the previous region, to reflect
149 // that the def could theoretically have been scheduled at the end.
150 DefIndices[Reg] = InsertPosIndex;
151 }
Bob Wilsonf70007e2010-10-02 01:49:29 +0000152 }
David Goodwin2e7be612009-10-26 16:59:04 +0000153
154 PrescanInstruction(MI);
155 ScanInstruction(MI, Count);
156}
157
158/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
159/// critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000160static const SDep *CriticalPathStep(const SUnit *SU) {
161 const SDep *Next = 0;
David Goodwin2e7be612009-10-26 16:59:04 +0000162 unsigned NextDepth = 0;
163 // Find the predecessor edge with the greatest depth.
Dan Gohman66db3a02010-04-19 23:11:58 +0000164 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin2e7be612009-10-26 16:59:04 +0000165 P != PE; ++P) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000166 const SUnit *PredSU = P->getSUnit();
David Goodwin2e7be612009-10-26 16:59:04 +0000167 unsigned PredLatency = P->getLatency();
168 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
169 // In the case of a latency tie, prefer an anti-dependency edge over
170 // other types of edges.
171 if (NextDepth < PredTotalLatency ||
172 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
173 NextDepth = PredTotalLatency;
174 Next = &*P;
175 }
176 }
177 return Next;
178}
179
180void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr *MI) {
Evan Cheng46df4eb2010-06-16 07:35:02 +0000181 // It's not safe to change register allocation for source operands of
182 // that have special allocation requirements. Also assume all registers
183 // used in a call must not be changed (ABI).
184 // FIXME: The issue with predicated instruction is more complex. We are being
Bob Wilson59718a42010-09-10 22:42:21 +0000185 // conservative here because the kill markers cannot be trusted after
Evan Cheng46df4eb2010-06-16 07:35:02 +0000186 // if-conversion:
187 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
188 // ...
189 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
190 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
191 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
192 //
193 // The first R6 kill is not really a kill since it's killed by a predicated
194 // instruction which may not be executed. The second R6 def may or may not
195 // re-define R6 so it's not safe to change it since the last R6 use cannot be
196 // changed.
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000197 bool Special = MI->isCall() ||
198 MI->hasExtraSrcRegAllocReq() ||
Evan Cheng46df4eb2010-06-16 07:35:02 +0000199 TII->isPredicated(MI);
200
David Goodwin2e7be612009-10-26 16:59:04 +0000201 // Scan the register operands for this instruction and update
202 // Classes and RegRefs.
203 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
204 MachineOperand &MO = MI->getOperand(i);
205 if (!MO.isReg()) continue;
206 unsigned Reg = MO.getReg();
207 if (Reg == 0) continue;
208 const TargetRegisterClass *NewRC = 0;
Jim Grosbach01384ef2010-05-14 21:20:46 +0000209
David Goodwin2e7be612009-10-26 16:59:04 +0000210 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000211 NewRC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwin2e7be612009-10-26 16:59:04 +0000212
213 // For now, only allow the register to be changed if its register
214 // class is consistent across all uses.
215 if (!Classes[Reg] && NewRC)
216 Classes[Reg] = NewRC;
217 else if (!NewRC || Classes[Reg] != NewRC)
218 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
219
220 // Now check for aliases.
Craig Toppere4fd9072012-03-04 10:43:23 +0000221 for (const uint16_t *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
David Goodwin2e7be612009-10-26 16:59:04 +0000222 // If an alias of the reg is used during the live range, give up.
223 // Note that this allows us to skip checking if AntiDepReg
224 // overlaps with any of the aliases, among other things.
225 unsigned AliasReg = *Alias;
226 if (Classes[AliasReg]) {
227 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
228 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
229 }
230 }
231
232 // If we're still willing to consider this register, note the reference.
233 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
234 RegRefs.insert(std::make_pair(Reg, &MO));
235
Evan Cheng46df4eb2010-06-16 07:35:02 +0000236 if (MO.isUse() && Special) {
Benjamin Kramercff4ad72012-03-17 20:22:57 +0000237 if (!KeepRegs.test(Reg)) {
238 KeepRegs.set(Reg);
Craig Topper9ebfbf82012-03-05 05:37:41 +0000239 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
David Goodwin2e7be612009-10-26 16:59:04 +0000240 *Subreg; ++Subreg)
Benjamin Kramercff4ad72012-03-17 20:22:57 +0000241 KeepRegs.set(*Subreg);
David Goodwin2e7be612009-10-26 16:59:04 +0000242 }
243 }
244 }
245}
246
247void CriticalAntiDepBreaker::ScanInstruction(MachineInstr *MI,
248 unsigned Count) {
249 // Update liveness.
250 // Proceding upwards, registers that are defed but not used in this
251 // instruction are now dead.
David Goodwin2e7be612009-10-26 16:59:04 +0000252
Evan Cheng46df4eb2010-06-16 07:35:02 +0000253 if (!TII->isPredicated(MI)) {
254 // Predicated defs are modeled as read + write, i.e. similar to two
255 // address updates.
256 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
257 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesenbbad2f12012-02-23 01:15:26 +0000258
259 if (MO.isRegMask())
260 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
261 if (MO.clobbersPhysReg(i)) {
262 DefIndices[i] = Count;
263 KillIndices[i] = ~0u;
Benjamin Kramercff4ad72012-03-17 20:22:57 +0000264 KeepRegs.reset(i);
Jakob Stoklund Olesenbbad2f12012-02-23 01:15:26 +0000265 Classes[i] = 0;
266 RegRefs.erase(i);
267 }
268
Evan Cheng46df4eb2010-06-16 07:35:02 +0000269 if (!MO.isReg()) continue;
270 unsigned Reg = MO.getReg();
271 if (Reg == 0) continue;
272 if (!MO.isDef()) continue;
273 // Ignore two-addr defs.
274 if (MI->isRegTiedToUseOperand(i)) continue;
275
276 DefIndices[Reg] = Count;
277 KillIndices[Reg] = ~0u;
278 assert(((KillIndices[Reg] == ~0u) !=
279 (DefIndices[Reg] == ~0u)) &&
280 "Kill and Def maps aren't consistent for Reg!");
Benjamin Kramercff4ad72012-03-17 20:22:57 +0000281 KeepRegs.reset(Reg);
Evan Cheng46df4eb2010-06-16 07:35:02 +0000282 Classes[Reg] = 0;
283 RegRefs.erase(Reg);
284 // Repeat, for all subregs.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000285 for (const uint16_t *Subreg = TRI->getSubRegisters(Reg);
Evan Cheng46df4eb2010-06-16 07:35:02 +0000286 *Subreg; ++Subreg) {
287 unsigned SubregReg = *Subreg;
288 DefIndices[SubregReg] = Count;
289 KillIndices[SubregReg] = ~0u;
Benjamin Kramercff4ad72012-03-17 20:22:57 +0000290 KeepRegs.reset(SubregReg);
Evan Cheng46df4eb2010-06-16 07:35:02 +0000291 Classes[SubregReg] = 0;
292 RegRefs.erase(SubregReg);
293 }
294 // Conservatively mark super-registers as unusable.
Craig Topper9ebfbf82012-03-05 05:37:41 +0000295 for (const uint16_t *Super = TRI->getSuperRegisters(Reg);
Evan Cheng46df4eb2010-06-16 07:35:02 +0000296 *Super; ++Super) {
297 unsigned SuperReg = *Super;
298 Classes[SuperReg] = reinterpret_cast<TargetRegisterClass *>(-1);
299 }
David Goodwin2e7be612009-10-26 16:59:04 +0000300 }
301 }
302 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
303 MachineOperand &MO = MI->getOperand(i);
304 if (!MO.isReg()) continue;
305 unsigned Reg = MO.getReg();
306 if (Reg == 0) continue;
307 if (!MO.isUse()) continue;
308
309 const TargetRegisterClass *NewRC = 0;
310 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000311 NewRC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwin2e7be612009-10-26 16:59:04 +0000312
313 // For now, only allow the register to be changed if its register
314 // class is consistent across all uses.
315 if (!Classes[Reg] && NewRC)
316 Classes[Reg] = NewRC;
317 else if (!NewRC || Classes[Reg] != NewRC)
318 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
319
320 RegRefs.insert(std::make_pair(Reg, &MO));
321
322 // It wasn't previously live but now it is, this is a kill.
323 if (KillIndices[Reg] == ~0u) {
324 KillIndices[Reg] = Count;
325 DefIndices[Reg] = ~0u;
326 assert(((KillIndices[Reg] == ~0u) !=
327 (DefIndices[Reg] == ~0u)) &&
328 "Kill and Def maps aren't consistent for Reg!");
329 }
330 // Repeat, for all aliases.
Craig Toppere4fd9072012-03-04 10:43:23 +0000331 for (const uint16_t *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias) {
David Goodwin2e7be612009-10-26 16:59:04 +0000332 unsigned AliasReg = *Alias;
333 if (KillIndices[AliasReg] == ~0u) {
334 KillIndices[AliasReg] = Count;
335 DefIndices[AliasReg] = ~0u;
336 }
337 }
338 }
339}
340
Andrew Trickbc4bd922011-02-08 17:39:46 +0000341// Check all machine operands that reference the antidependent register and must
342// be replaced by NewReg. Return true if any of their parent instructions may
343// clobber the new register.
344//
345// Note: AntiDepReg may be referenced by a two-address instruction such that
346// it's use operand is tied to a def operand. We guard against the case in which
347// the two-address instruction also defines NewReg, as may happen with
348// pre/postincrement loads. In this case, both the use and def operands are in
349// RegRefs because the def is inserted by PrescanInstruction and not erased
350// during ScanInstruction. So checking for an instructions with definitions of
351// both NewReg and AntiDepReg covers it.
Andrew Trick46388522010-11-02 18:16:45 +0000352bool
Andrew Trickbc4bd922011-02-08 17:39:46 +0000353CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
354 RegRefIter RegRefEnd,
355 unsigned NewReg)
Andrew Trick46388522010-11-02 18:16:45 +0000356{
357 for (RegRefIter I = RegRefBegin; I != RegRefEnd; ++I ) {
Andrew Trickbc4bd922011-02-08 17:39:46 +0000358 MachineOperand *RefOper = I->second;
359
360 // Don't allow the instruction defining AntiDepReg to earlyclobber its
361 // operands, in case they may be assigned to NewReg. In this case antidep
362 // breaking must fail, but it's too rare to bother optimizing.
363 if (RefOper->isDef() && RefOper->isEarlyClobber())
Andrew Trick46388522010-11-02 18:16:45 +0000364 return true;
Andrew Trickbc4bd922011-02-08 17:39:46 +0000365
366 // Handle cases in which this instructions defines NewReg.
367 MachineInstr *MI = RefOper->getParent();
368 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
369 const MachineOperand &CheckOper = MI->getOperand(i);
370
Jakob Stoklund Olesenbbad2f12012-02-23 01:15:26 +0000371 if (CheckOper.isRegMask() && CheckOper.clobbersPhysReg(NewReg))
372 return true;
373
Andrew Trickbc4bd922011-02-08 17:39:46 +0000374 if (!CheckOper.isReg() || !CheckOper.isDef() ||
375 CheckOper.getReg() != NewReg)
376 continue;
377
378 // Don't allow the instruction to define NewReg and AntiDepReg.
379 // When AntiDepReg is renamed it will be an illegal op.
380 if (RefOper->isDef())
381 return true;
382
383 // Don't allow an instruction using AntiDepReg to be earlyclobbered by
384 // NewReg
385 if (CheckOper.isEarlyClobber())
386 return true;
387
388 // Don't allow inline asm to define NewReg at all. Who know what it's
389 // doing with it.
390 if (MI->isInlineAsm())
391 return true;
392 }
Andrew Trick46388522010-11-02 18:16:45 +0000393 }
394 return false;
395}
396
David Goodwin2e7be612009-10-26 16:59:04 +0000397unsigned
Andrew Trick46388522010-11-02 18:16:45 +0000398CriticalAntiDepBreaker::findSuitableFreeRegister(RegRefIter RegRefBegin,
399 RegRefIter RegRefEnd,
Jim Grosbach80c2b0d2010-01-06 22:21:25 +0000400 unsigned AntiDepReg,
David Goodwin2e7be612009-10-26 16:59:04 +0000401 unsigned LastNewReg,
Jim Grosbach2973b572010-01-06 16:48:02 +0000402 const TargetRegisterClass *RC)
403{
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000404 ArrayRef<unsigned> Order = RegClassInfo.getOrder(RC);
405 for (unsigned i = 0; i != Order.size(); ++i) {
406 unsigned NewReg = Order[i];
David Goodwin2e7be612009-10-26 16:59:04 +0000407 // Don't replace a register with itself.
408 if (NewReg == AntiDepReg) continue;
409 // Don't replace a register with one that was recently used to repair
410 // an anti-dependence with this AntiDepReg, because that would
411 // re-introduce that anti-dependence.
412 if (NewReg == LastNewReg) continue;
Andrew Trick46388522010-11-02 18:16:45 +0000413 // If any instructions that define AntiDepReg also define the NewReg, it's
414 // not suitable. For example, Instruction with multiple definitions can
415 // result in this condition.
Andrew Trickbc4bd922011-02-08 17:39:46 +0000416 if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg)) continue;
David Goodwin2e7be612009-10-26 16:59:04 +0000417 // If NewReg is dead and NewReg's most recent def is not before
418 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
Jim Grosbach2973b572010-01-06 16:48:02 +0000419 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
420 && "Kill and Def maps aren't consistent for AntiDepReg!");
421 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
422 && "Kill and Def maps aren't consistent for NewReg!");
David Goodwin2e7be612009-10-26 16:59:04 +0000423 if (KillIndices[NewReg] != ~0u ||
424 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
425 KillIndices[AntiDepReg] > DefIndices[NewReg])
426 continue;
427 return NewReg;
428 }
429
430 // No registers are free and available!
431 return 0;
432}
433
434unsigned CriticalAntiDepBreaker::
Dan Gohman66db3a02010-04-19 23:11:58 +0000435BreakAntiDependencies(const std::vector<SUnit>& SUnits,
436 MachineBasicBlock::iterator Begin,
437 MachineBasicBlock::iterator End,
Devang Patele29e8e12011-06-02 21:26:52 +0000438 unsigned InsertPosIndex,
439 DbgValueVector &DbgValues) {
David Goodwin2e7be612009-10-26 16:59:04 +0000440 // The code below assumes that there is at least one instruction,
441 // so just duck out immediately if the block is empty.
442 if (SUnits.empty()) return 0;
443
Jim Grosbach533934e2010-06-01 23:48:44 +0000444 // Keep a map of the MachineInstr*'s back to the SUnit representing them.
445 // This is used for updating debug information.
Andrew Trickb4566a92012-02-22 06:08:11 +0000446 //
447 // FIXME: Replace this with the existing map in ScheduleDAGInstrs::MISUnitMap
Jim Grosbach533934e2010-06-01 23:48:44 +0000448 DenseMap<MachineInstr*,const SUnit*> MISUnitMap;
449
David Goodwin2e7be612009-10-26 16:59:04 +0000450 // Find the node at the bottom of the critical path.
Dan Gohman66db3a02010-04-19 23:11:58 +0000451 const SUnit *Max = 0;
David Goodwin2e7be612009-10-26 16:59:04 +0000452 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000453 const SUnit *SU = &SUnits[i];
Jim Grosbach533934e2010-06-01 23:48:44 +0000454 MISUnitMap[SU->getInstr()] = SU;
David Goodwin2e7be612009-10-26 16:59:04 +0000455 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
456 Max = SU;
457 }
458
459#ifndef NDEBUG
460 {
David Greene89d6a242010-01-04 17:47:05 +0000461 DEBUG(dbgs() << "Critical path has total latency "
David Goodwin2e7be612009-10-26 16:59:04 +0000462 << (Max->getDepth() + Max->Latency) << "\n");
David Greene89d6a242010-01-04 17:47:05 +0000463 DEBUG(dbgs() << "Available regs:");
David Goodwin2e7be612009-10-26 16:59:04 +0000464 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
465 if (KillIndices[Reg] == ~0u)
David Greene89d6a242010-01-04 17:47:05 +0000466 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin2e7be612009-10-26 16:59:04 +0000467 }
David Greene89d6a242010-01-04 17:47:05 +0000468 DEBUG(dbgs() << '\n');
David Goodwin2e7be612009-10-26 16:59:04 +0000469 }
470#endif
471
472 // Track progress along the critical path through the SUnit graph as we walk
473 // the instructions.
Dan Gohman66db3a02010-04-19 23:11:58 +0000474 const SUnit *CriticalPathSU = Max;
David Goodwin2e7be612009-10-26 16:59:04 +0000475 MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
476
477 // Consider this pattern:
478 // A = ...
479 // ... = A
480 // A = ...
481 // ... = A
482 // A = ...
483 // ... = A
484 // A = ...
485 // ... = A
486 // There are three anti-dependencies here, and without special care,
487 // we'd break all of them using the same register:
488 // A = ...
489 // ... = A
490 // B = ...
491 // ... = B
492 // B = ...
493 // ... = B
494 // B = ...
495 // ... = B
496 // because at each anti-dependence, B is the first register that
497 // isn't A which is free. This re-introduces anti-dependencies
498 // at all but one of the original anti-dependencies that we were
499 // trying to break. To avoid this, keep track of the most recent
500 // register that each register was replaced with, avoid
501 // using it to repair an anti-dependence on the same register.
502 // This lets us produce this:
503 // A = ...
504 // ... = A
505 // B = ...
506 // ... = B
507 // C = ...
508 // ... = C
509 // B = ...
510 // ... = B
511 // This still has an anti-dependence on B, but at least it isn't on the
512 // original critical path.
513 //
514 // TODO: If we tracked more than one register here, we could potentially
515 // fix that remaining critical edge too. This is a little more involved,
516 // because unlike the most recent register, less recent registers should
517 // still be considered, though only if no other registers are available.
Bill Wendling9c2a0342010-07-15 19:58:14 +0000518 std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);
David Goodwin2e7be612009-10-26 16:59:04 +0000519
520 // Attempt to break anti-dependence edges on the critical path. Walk the
521 // instructions from the bottom up, tracking information about liveness
522 // as we go to help determine which registers are available.
523 unsigned Broken = 0;
524 unsigned Count = InsertPosIndex - 1;
525 for (MachineBasicBlock::iterator I = End, E = Begin;
526 I != E; --Count) {
527 MachineInstr *MI = --I;
Dale Johannesenb0812f12010-03-05 00:02:59 +0000528 if (MI->isDebugValue())
529 continue;
David Goodwin2e7be612009-10-26 16:59:04 +0000530
531 // Check if this instruction has a dependence on the critical path that
532 // is an anti-dependence that we may be able to break. If it is, set
533 // AntiDepReg to the non-zero register associated with the anti-dependence.
534 //
535 // We limit our attention to the critical path as a heuristic to avoid
536 // breaking anti-dependence edges that aren't going to significantly
537 // impact the overall schedule. There are a limited number of registers
538 // and we want to save them for the important edges.
Jim Grosbach01384ef2010-05-14 21:20:46 +0000539 //
David Goodwin2e7be612009-10-26 16:59:04 +0000540 // TODO: Instructions with multiple defs could have multiple
541 // anti-dependencies. The current code here only knows how to break one
542 // edge per instruction. Note that we'd have to be able to break all of
543 // the anti-dependencies in an instruction in order to be effective.
544 unsigned AntiDepReg = 0;
545 if (MI == CriticalPathMI) {
Dan Gohman66db3a02010-04-19 23:11:58 +0000546 if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
547 const SUnit *NextSU = Edge->getSUnit();
David Goodwin2e7be612009-10-26 16:59:04 +0000548
549 // Only consider anti-dependence edges.
550 if (Edge->getKind() == SDep::Anti) {
551 AntiDepReg = Edge->getReg();
552 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +0000553 if (!RegClassInfo.isAllocatable(AntiDepReg))
David Goodwin2e7be612009-10-26 16:59:04 +0000554 // Don't break anti-dependencies on non-allocatable registers.
555 AntiDepReg = 0;
Benjamin Kramercff4ad72012-03-17 20:22:57 +0000556 else if (KeepRegs.test(AntiDepReg))
David Goodwin2e7be612009-10-26 16:59:04 +0000557 // Don't break anti-dependencies if an use down below requires
558 // this exact register.
559 AntiDepReg = 0;
560 else {
561 // If the SUnit has other dependencies on the SUnit that it
562 // anti-depends on, don't bother breaking the anti-dependency
563 // since those edges would prevent such units from being
564 // scheduled past each other regardless.
565 //
566 // Also, if there are dependencies on other SUnits with the
567 // same register as the anti-dependency, don't attempt to
568 // break it.
Dan Gohman66db3a02010-04-19 23:11:58 +0000569 for (SUnit::const_pred_iterator P = CriticalPathSU->Preds.begin(),
David Goodwin2e7be612009-10-26 16:59:04 +0000570 PE = CriticalPathSU->Preds.end(); P != PE; ++P)
571 if (P->getSUnit() == NextSU ?
572 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
573 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
574 AntiDepReg = 0;
575 break;
576 }
577 }
578 }
579 CriticalPathSU = NextSU;
580 CriticalPathMI = CriticalPathSU->getInstr();
581 } else {
582 // We've reached the end of the critical path.
583 CriticalPathSU = 0;
584 CriticalPathMI = 0;
585 }
586 }
587
588 PrescanInstruction(MI);
589
Evan Cheng46df4eb2010-06-16 07:35:02 +0000590 // If MI's defs have a special allocation requirement, don't allow
591 // any def registers to be changed. Also assume all registers
592 // defined in a call must not be changed (ABI).
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000593 if (MI->isCall() || MI->hasExtraDefRegAllocReq() ||
Evan Cheng46df4eb2010-06-16 07:35:02 +0000594 TII->isPredicated(MI))
David Goodwin2e7be612009-10-26 16:59:04 +0000595 // If this instruction's defs have special allocation requirement, don't
596 // break this anti-dependency.
597 AntiDepReg = 0;
598 else if (AntiDepReg) {
599 // If this instruction has a use of AntiDepReg, breaking it
600 // is invalid.
601 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
602 MachineOperand &MO = MI->getOperand(i);
603 if (!MO.isReg()) continue;
604 unsigned Reg = MO.getReg();
605 if (Reg == 0) continue;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000606 if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
David Goodwin2e7be612009-10-26 16:59:04 +0000607 AntiDepReg = 0;
608 break;
609 }
610 }
611 }
612
613 // Determine AntiDepReg's register class, if it is live and is
614 // consistently used within a single class.
615 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg] : 0;
616 assert((AntiDepReg == 0 || RC != NULL) &&
617 "Register should be live if it's causing an anti-dependence!");
618 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
619 AntiDepReg = 0;
620
621 // Look for a suitable register to use to break the anti-depenence.
622 //
623 // TODO: Instead of picking the first free register, consider which might
624 // be the best.
625 if (AntiDepReg != 0) {
Andrew Trick46388522010-11-02 18:16:45 +0000626 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
627 std::multimap<unsigned, MachineOperand *>::iterator>
628 Range = RegRefs.equal_range(AntiDepReg);
629 if (unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,
630 AntiDepReg,
David Goodwin2e7be612009-10-26 16:59:04 +0000631 LastNewReg[AntiDepReg],
632 RC)) {
David Greene89d6a242010-01-04 17:47:05 +0000633 DEBUG(dbgs() << "Breaking anti-dependence edge on "
David Goodwin2e7be612009-10-26 16:59:04 +0000634 << TRI->getName(AntiDepReg)
635 << " with " << RegRefs.count(AntiDepReg) << " references"
636 << " using " << TRI->getName(NewReg) << "!\n");
637
638 // Update the references to the old register to refer to the new
639 // register.
David Goodwin2e7be612009-10-26 16:59:04 +0000640 for (std::multimap<unsigned, MachineOperand *>::iterator
Jim Grosbach533934e2010-06-01 23:48:44 +0000641 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
David Goodwin2e7be612009-10-26 16:59:04 +0000642 Q->second->setReg(NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000643 // If the SU for the instruction being updated has debug information
644 // related to the anti-dependency register, make sure to update that
645 // as well.
646 const SUnit *SU = MISUnitMap[Q->second->getParent()];
Jim Grosbach086723d2010-06-02 15:29:36 +0000647 if (!SU) continue;
Devang Patele29e8e12011-06-02 21:26:52 +0000648 for (DbgValueVector::iterator DVI = DbgValues.begin(),
649 DVE = DbgValues.end(); DVI != DVE; ++DVI)
650 if (DVI->second == Q->second->getParent())
651 UpdateDbgValue(DVI->first, AntiDepReg, NewReg);
Jim Grosbach533934e2010-06-01 23:48:44 +0000652 }
David Goodwin2e7be612009-10-26 16:59:04 +0000653
654 // We just went back in time and modified history; the
Bob Wilsonf70007e2010-10-02 01:49:29 +0000655 // liveness information for the anti-dependence reg is now
David Goodwin2e7be612009-10-26 16:59:04 +0000656 // inconsistent. Set the state as if it were dead.
657 Classes[NewReg] = Classes[AntiDepReg];
658 DefIndices[NewReg] = DefIndices[AntiDepReg];
659 KillIndices[NewReg] = KillIndices[AntiDepReg];
660 assert(((KillIndices[NewReg] == ~0u) !=
661 (DefIndices[NewReg] == ~0u)) &&
662 "Kill and Def maps aren't consistent for NewReg!");
663
664 Classes[AntiDepReg] = 0;
665 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
666 KillIndices[AntiDepReg] = ~0u;
667 assert(((KillIndices[AntiDepReg] == ~0u) !=
668 (DefIndices[AntiDepReg] == ~0u)) &&
669 "Kill and Def maps aren't consistent for AntiDepReg!");
670
671 RegRefs.erase(AntiDepReg);
672 LastNewReg[AntiDepReg] = NewReg;
673 ++Broken;
674 }
675 }
676
677 ScanInstruction(MI, Count);
678 }
679
680 return Broken;
681}