blob: 737e6fa55f62fdea85fae0ef695800922f260c94 [file] [log] [blame]
David Goodwin83704852009-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 Goodwin83704852009-10-26 16:59:04 +000016#include "CriticalAntiDepBreaker.h"
17#include "llvm/CodeGen/MachineBasicBlock.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
David Goodwin83704852009-10-26 16:59:04 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000025#include "llvm/Target/TargetSubtargetInfo.h"
David Goodwin83704852009-10-26 16:59:04 +000026
27using namespace llvm;
28
Chandler Carruth1b9dde02014-04-22 02:02:50 +000029#define DEBUG_TYPE "post-RA-sched"
30
Eric Christopherd9134482014-08-04 21:25:23 +000031CriticalAntiDepBreaker::CriticalAntiDepBreaker(MachineFunction &MFi,
32 const RegisterClassInfo &RCI)
33 : AntiDepBreaker(), MF(MFi), MRI(MF.getRegInfo()),
34 TII(MF.getTarget().getSubtargetImpl()->getInstrInfo()),
35 TRI(MF.getTarget().getSubtargetImpl()->getRegisterInfo()),
36 RegClassInfo(RCI), Classes(TRI->getNumRegs(), nullptr),
37 KillIndices(TRI->getNumRegs(), 0), DefIndices(TRI->getNumRegs(), 0),
38 KeepRegs(TRI->getNumRegs(), false) {}
David Goodwin83704852009-10-26 16:59:04 +000039
40CriticalAntiDepBreaker::~CriticalAntiDepBreaker() {
41}
42
43void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
David Goodwina45fe672009-12-09 17:18:22 +000044 const unsigned BBSize = BB->size();
Bill Wendling51a9c0a2010-07-15 19:58:14 +000045 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
46 // Clear out the register class data.
Craig Topperc0196b12014-04-14 00:51:57 +000047 Classes[i] = nullptr;
Bill Wendling51a9c0a2010-07-15 19:58:14 +000048
49 // Initialize the indices to indicate that no registers are live.
David Goodwina45fe672009-12-09 17:18:22 +000050 KillIndices[i] = ~0u;
51 DefIndices[i] = BBSize;
52 }
David Goodwin83704852009-10-26 16:59:04 +000053
54 // Clear "do not change" set.
Benjamin Kramer5d1bca82012-03-17 20:22:57 +000055 KeepRegs.reset();
David Goodwin83704852009-10-26 16:59:04 +000056
Benjamin Kramer8e5af372012-03-16 15:46:47 +000057 bool IsReturnBlock = (BBSize != 0 && BB->back().isReturn());
David Goodwin83704852009-10-26 16:59:04 +000058
Jakob Stoklund Olesenc3386792013-02-05 18:21:52 +000059 // Examine the live-in regs of all successors.
Evan Chengf128bdc2010-06-16 07:35:02 +000060 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
61 SE = BB->succ_end(); SI != SE; ++SI)
62 for (MachineBasicBlock::livein_iterator I = (*SI)->livein_begin(),
63 E = (*SI)->livein_end(); I != E; ++I) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +000064 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
65 unsigned Reg = *AI;
66 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
67 KillIndices[Reg] = BBSize;
68 DefIndices[Reg] = ~0u;
Evan Chengf128bdc2010-06-16 07:35:02 +000069 }
70 }
71
David Goodwin83704852009-10-26 16:59:04 +000072 // Mark live-out callee-saved registers. In a return block this is
73 // all callee-saved registers. In non-return this is any
74 // callee-saved register that is not saved in the prolog.
75 const MachineFrameInfo *MFI = MF.getFrameInfo();
76 BitVector Pristine = MFI->getPristineRegs(BB);
Craig Topper840beec2014-04-04 05:16:06 +000077 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +000078 if (!IsReturnBlock && !Pristine.test(*I)) continue;
79 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
80 unsigned Reg = *AI;
81 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
82 KillIndices[Reg] = BBSize;
83 DefIndices[Reg] = ~0u;
David Goodwin83704852009-10-26 16:59:04 +000084 }
85 }
86}
87
88void CriticalAntiDepBreaker::FinishBlock() {
89 RegRefs.clear();
Benjamin Kramer5d1bca82012-03-17 20:22:57 +000090 KeepRegs.reset();
David Goodwin83704852009-10-26 16:59:04 +000091}
92
93void CriticalAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count,
94 unsigned InsertPosIndex) {
Dale Johannesen2061c842010-03-05 00:02:59 +000095 if (MI->isDebugValue())
96 return;
David Goodwin83704852009-10-26 16:59:04 +000097 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
98
Bob Wilsonc57c2202010-10-02 01:49:29 +000099 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
100 if (KillIndices[Reg] != ~0u) {
101 // If Reg is currently live, then mark that it can't be renamed as
102 // we don't know the extent of its live-range anymore (now that it
103 // has been scheduled).
104 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
105 KillIndices[Reg] = Count;
106 } else if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
107 // Any register which was defined within the previous scheduling region
108 // may have been rescheduled and its lifetime may overlap with registers
109 // in ways not reflected in our current liveness state. For each such
110 // register, adjust the liveness state to be conservatively correct.
David Goodwin83704852009-10-26 16:59:04 +0000111 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
Bill Wendling51a9c0a2010-07-15 19:58:14 +0000112
David Goodwin83704852009-10-26 16:59:04 +0000113 // Move the def index to the end of the previous region, to reflect
114 // that the def could theoretically have been scheduled at the end.
115 DefIndices[Reg] = InsertPosIndex;
116 }
Bob Wilsonc57c2202010-10-02 01:49:29 +0000117 }
David Goodwin83704852009-10-26 16:59:04 +0000118
119 PrescanInstruction(MI);
120 ScanInstruction(MI, Count);
121}
122
123/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
124/// critical path.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000125static const SDep *CriticalPathStep(const SUnit *SU) {
Craig Topperc0196b12014-04-14 00:51:57 +0000126 const SDep *Next = nullptr;
David Goodwin83704852009-10-26 16:59:04 +0000127 unsigned NextDepth = 0;
128 // Find the predecessor edge with the greatest depth.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000129 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin83704852009-10-26 16:59:04 +0000130 P != PE; ++P) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000131 const SUnit *PredSU = P->getSUnit();
David Goodwin83704852009-10-26 16:59:04 +0000132 unsigned PredLatency = P->getLatency();
133 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
134 // In the case of a latency tie, prefer an anti-dependency edge over
135 // other types of edges.
136 if (NextDepth < PredTotalLatency ||
137 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
138 NextDepth = PredTotalLatency;
139 Next = &*P;
140 }
141 }
142 return Next;
143}
144
145void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr *MI) {
Evan Chengf128bdc2010-06-16 07:35:02 +0000146 // It's not safe to change register allocation for source operands of
Sanjay Patel99475192014-06-24 21:11:51 +0000147 // instructions that have special allocation requirements. Also assume all
148 // registers used in a call must not be changed (ABI).
Evan Chengf128bdc2010-06-16 07:35:02 +0000149 // FIXME: The issue with predicated instruction is more complex. We are being
Bob Wilsonf3ecfd02010-09-10 22:42:21 +0000150 // conservative here because the kill markers cannot be trusted after
Evan Chengf128bdc2010-06-16 07:35:02 +0000151 // if-conversion:
152 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
153 // ...
154 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
155 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
156 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
157 //
158 // The first R6 kill is not really a kill since it's killed by a predicated
159 // instruction which may not be executed. The second R6 def may or may not
160 // re-define R6 so it's not safe to change it since the last R6 use cannot be
161 // changed.
Evan Cheng7f8e5632011-12-07 07:15:52 +0000162 bool Special = MI->isCall() ||
163 MI->hasExtraSrcRegAllocReq() ||
Evan Chengf128bdc2010-06-16 07:35:02 +0000164 TII->isPredicated(MI);
165
David Goodwin83704852009-10-26 16:59:04 +0000166 // Scan the register operands for this instruction and update
167 // Classes and RegRefs.
168 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
169 MachineOperand &MO = MI->getOperand(i);
170 if (!MO.isReg()) continue;
171 unsigned Reg = MO.getReg();
172 if (Reg == 0) continue;
Craig Topperc0196b12014-04-14 00:51:57 +0000173 const TargetRegisterClass *NewRC = nullptr;
Jim Grosbach866b74b2010-05-14 21:20:46 +0000174
David Goodwin83704852009-10-26 16:59:04 +0000175 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000176 NewRC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwin83704852009-10-26 16:59:04 +0000177
178 // For now, only allow the register to be changed if its register
179 // class is consistent across all uses.
180 if (!Classes[Reg] && NewRC)
181 Classes[Reg] = NewRC;
182 else if (!NewRC || Classes[Reg] != NewRC)
183 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
184
185 // Now check for aliases.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000186 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
David Goodwin83704852009-10-26 16:59:04 +0000187 // If an alias of the reg is used during the live range, give up.
188 // Note that this allows us to skip checking if AntiDepReg
189 // overlaps with any of the aliases, among other things.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000190 unsigned AliasReg = *AI;
David Goodwin83704852009-10-26 16:59:04 +0000191 if (Classes[AliasReg]) {
192 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
193 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
194 }
195 }
196
197 // If we're still willing to consider this register, note the reference.
198 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
199 RegRefs.insert(std::make_pair(Reg, &MO));
200
Sanjay Pateldc574ab2014-07-03 15:19:40 +0000201 // If this reg is tied and live (Classes[Reg] is set to -1), we can't change
202 // it or any of its sub or super regs. We need to use KeepRegs to mark the
203 // reg because not all uses of the same reg within an instruction are
204 // necessarily tagged as tied.
205 // Example: an x86 "xor %eax, %eax" will have one source operand tied to the
206 // def register but not the second (see PR20020 for details).
207 // FIXME: can this check be relaxed to account for undef uses
208 // of a register? In the above 'xor' example, the uses of %eax are undef, so
209 // earlier instructions could still replace %eax even though the 'xor'
210 // itself can't be changed.
211 if (MI->isRegTiedToUseOperand(i) &&
212 Classes[Reg] == reinterpret_cast<TargetRegisterClass *>(-1)) {
213 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
214 SubRegs.isValid(); ++SubRegs) {
215 KeepRegs.set(*SubRegs);
216 }
217 for (MCSuperRegIterator SuperRegs(Reg, TRI);
218 SuperRegs.isValid(); ++SuperRegs) {
219 KeepRegs.set(*SuperRegs);
220 }
221 }
222
Evan Chengf128bdc2010-06-16 07:35:02 +0000223 if (MO.isUse() && Special) {
Benjamin Kramer5d1bca82012-03-17 20:22:57 +0000224 if (!KeepRegs.test(Reg)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000225 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
226 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000227 KeepRegs.set(*SubRegs);
David Goodwin83704852009-10-26 16:59:04 +0000228 }
229 }
230 }
231}
232
233void CriticalAntiDepBreaker::ScanInstruction(MachineInstr *MI,
234 unsigned Count) {
235 // Update liveness.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000236 // Proceeding upwards, registers that are defed but not used in this
David Goodwin83704852009-10-26 16:59:04 +0000237 // instruction are now dead.
David Goodwin83704852009-10-26 16:59:04 +0000238
Evan Chengf128bdc2010-06-16 07:35:02 +0000239 if (!TII->isPredicated(MI)) {
240 // Predicated defs are modeled as read + write, i.e. similar to two
241 // address updates.
242 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
243 MachineOperand &MO = MI->getOperand(i);
Jakob Stoklund Olesen38ce8892012-02-23 01:15:26 +0000244
245 if (MO.isRegMask())
246 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
247 if (MO.clobbersPhysReg(i)) {
248 DefIndices[i] = Count;
249 KillIndices[i] = ~0u;
Benjamin Kramer5d1bca82012-03-17 20:22:57 +0000250 KeepRegs.reset(i);
Craig Topperc0196b12014-04-14 00:51:57 +0000251 Classes[i] = nullptr;
Jakob Stoklund Olesen38ce8892012-02-23 01:15:26 +0000252 RegRefs.erase(i);
253 }
254
Evan Chengf128bdc2010-06-16 07:35:02 +0000255 if (!MO.isReg()) continue;
256 unsigned Reg = MO.getReg();
257 if (Reg == 0) continue;
258 if (!MO.isDef()) continue;
Sanjay Pateldc574ab2014-07-03 15:19:40 +0000259
260 // If we've already marked this reg as unchangeable, carry on.
261 if (KeepRegs.test(Reg)) continue;
262
Evan Chengf128bdc2010-06-16 07:35:02 +0000263 // Ignore two-addr defs.
264 if (MI->isRegTiedToUseOperand(i)) continue;
265
Sanjay Pateldc574ab2014-07-03 15:19:40 +0000266 // FIXME: we should use a SubRegIterator that includes self (as above), so
267 // we don't have to repeat all this code for the reg itself.
Evan Chengf128bdc2010-06-16 07:35:02 +0000268 DefIndices[Reg] = Count;
269 KillIndices[Reg] = ~0u;
270 assert(((KillIndices[Reg] == ~0u) !=
271 (DefIndices[Reg] == ~0u)) &&
272 "Kill and Def maps aren't consistent for Reg!");
Benjamin Kramer5d1bca82012-03-17 20:22:57 +0000273 KeepRegs.reset(Reg);
Craig Topperc0196b12014-04-14 00:51:57 +0000274 Classes[Reg] = nullptr;
Evan Chengf128bdc2010-06-16 07:35:02 +0000275 RegRefs.erase(Reg);
276 // Repeat, for all subregs.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000277 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
278 unsigned SubregReg = *SubRegs;
Evan Chengf128bdc2010-06-16 07:35:02 +0000279 DefIndices[SubregReg] = Count;
280 KillIndices[SubregReg] = ~0u;
Benjamin Kramer5d1bca82012-03-17 20:22:57 +0000281 KeepRegs.reset(SubregReg);
Craig Topperc0196b12014-04-14 00:51:57 +0000282 Classes[SubregReg] = nullptr;
Evan Chengf128bdc2010-06-16 07:35:02 +0000283 RegRefs.erase(SubregReg);
284 }
285 // Conservatively mark super-registers as unusable.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000286 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR)
287 Classes[*SR] = reinterpret_cast<TargetRegisterClass *>(-1);
David Goodwin83704852009-10-26 16:59:04 +0000288 }
289 }
290 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
291 MachineOperand &MO = MI->getOperand(i);
292 if (!MO.isReg()) continue;
293 unsigned Reg = MO.getReg();
294 if (Reg == 0) continue;
295 if (!MO.isUse()) continue;
296
Craig Topperc0196b12014-04-14 00:51:57 +0000297 const TargetRegisterClass *NewRC = nullptr;
David Goodwin83704852009-10-26 16:59:04 +0000298 if (i < MI->getDesc().getNumOperands())
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000299 NewRC = TII->getRegClass(MI->getDesc(), i, TRI, MF);
David Goodwin83704852009-10-26 16:59:04 +0000300
301 // For now, only allow the register to be changed if its register
302 // class is consistent across all uses.
303 if (!Classes[Reg] && NewRC)
304 Classes[Reg] = NewRC;
305 else if (!NewRC || Classes[Reg] != NewRC)
306 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
307
308 RegRefs.insert(std::make_pair(Reg, &MO));
309
Sanjay Pateldc574ab2014-07-03 15:19:40 +0000310 // FIXME: we should use an MCRegAliasIterator that includes self so we don't
311 // have to repeat all this code for the reg itself.
312
David Goodwin83704852009-10-26 16:59:04 +0000313 // It wasn't previously live but now it is, this is a kill.
314 if (KillIndices[Reg] == ~0u) {
315 KillIndices[Reg] = Count;
316 DefIndices[Reg] = ~0u;
317 assert(((KillIndices[Reg] == ~0u) !=
318 (DefIndices[Reg] == ~0u)) &&
319 "Kill and Def maps aren't consistent for Reg!");
320 }
321 // Repeat, for all aliases.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000322 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
323 unsigned AliasReg = *AI;
David Goodwin83704852009-10-26 16:59:04 +0000324 if (KillIndices[AliasReg] == ~0u) {
325 KillIndices[AliasReg] = Count;
326 DefIndices[AliasReg] = ~0u;
327 }
328 }
329 }
330}
331
Andrew Trick4b491872011-02-08 17:39:46 +0000332// Check all machine operands that reference the antidependent register and must
333// be replaced by NewReg. Return true if any of their parent instructions may
334// clobber the new register.
335//
336// Note: AntiDepReg may be referenced by a two-address instruction such that
337// it's use operand is tied to a def operand. We guard against the case in which
338// the two-address instruction also defines NewReg, as may happen with
339// pre/postincrement loads. In this case, both the use and def operands are in
340// RegRefs because the def is inserted by PrescanInstruction and not erased
Sanjay Patel99475192014-06-24 21:11:51 +0000341// during ScanInstruction. So checking for an instruction with definitions of
Andrew Trick4b491872011-02-08 17:39:46 +0000342// both NewReg and AntiDepReg covers it.
Andrew Trick82ae9a92010-11-02 18:16:45 +0000343bool
Andrew Trick4b491872011-02-08 17:39:46 +0000344CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
345 RegRefIter RegRefEnd,
346 unsigned NewReg)
Andrew Trick82ae9a92010-11-02 18:16:45 +0000347{
348 for (RegRefIter I = RegRefBegin; I != RegRefEnd; ++I ) {
Andrew Trick4b491872011-02-08 17:39:46 +0000349 MachineOperand *RefOper = I->second;
350
351 // Don't allow the instruction defining AntiDepReg to earlyclobber its
352 // operands, in case they may be assigned to NewReg. In this case antidep
353 // breaking must fail, but it's too rare to bother optimizing.
354 if (RefOper->isDef() && RefOper->isEarlyClobber())
Andrew Trick82ae9a92010-11-02 18:16:45 +0000355 return true;
Andrew Trick4b491872011-02-08 17:39:46 +0000356
Sanjay Patel99475192014-06-24 21:11:51 +0000357 // Handle cases in which this instruction defines NewReg.
Andrew Trick4b491872011-02-08 17:39:46 +0000358 MachineInstr *MI = RefOper->getParent();
359 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
360 const MachineOperand &CheckOper = MI->getOperand(i);
361
Jakob Stoklund Olesen38ce8892012-02-23 01:15:26 +0000362 if (CheckOper.isRegMask() && CheckOper.clobbersPhysReg(NewReg))
363 return true;
364
Andrew Trick4b491872011-02-08 17:39:46 +0000365 if (!CheckOper.isReg() || !CheckOper.isDef() ||
366 CheckOper.getReg() != NewReg)
367 continue;
368
369 // Don't allow the instruction to define NewReg and AntiDepReg.
370 // When AntiDepReg is renamed it will be an illegal op.
371 if (RefOper->isDef())
372 return true;
373
374 // Don't allow an instruction using AntiDepReg to be earlyclobbered by
Sanjay Patel99475192014-06-24 21:11:51 +0000375 // NewReg.
Andrew Trick4b491872011-02-08 17:39:46 +0000376 if (CheckOper.isEarlyClobber())
377 return true;
378
Sanjay Patel99475192014-06-24 21:11:51 +0000379 // Don't allow inline asm to define NewReg at all. Who knows what it's
Andrew Trick4b491872011-02-08 17:39:46 +0000380 // doing with it.
381 if (MI->isInlineAsm())
382 return true;
383 }
Andrew Trick82ae9a92010-11-02 18:16:45 +0000384 }
385 return false;
386}
387
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000388unsigned CriticalAntiDepBreaker::
389findSuitableFreeRegister(RegRefIter RegRefBegin,
390 RegRefIter RegRefEnd,
391 unsigned AntiDepReg,
392 unsigned LastNewReg,
393 const TargetRegisterClass *RC,
Craig Topper72cde632013-07-03 05:16:59 +0000394 SmallVectorImpl<unsigned> &Forbid)
Jim Grosbacheb431da2010-01-06 16:48:02 +0000395{
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000396 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000397 for (unsigned i = 0; i != Order.size(); ++i) {
398 unsigned NewReg = Order[i];
David Goodwin83704852009-10-26 16:59:04 +0000399 // Don't replace a register with itself.
400 if (NewReg == AntiDepReg) continue;
401 // Don't replace a register with one that was recently used to repair
402 // an anti-dependence with this AntiDepReg, because that would
403 // re-introduce that anti-dependence.
404 if (NewReg == LastNewReg) continue;
Andrew Trick82ae9a92010-11-02 18:16:45 +0000405 // If any instructions that define AntiDepReg also define the NewReg, it's
406 // not suitable. For example, Instruction with multiple definitions can
407 // result in this condition.
Andrew Trick4b491872011-02-08 17:39:46 +0000408 if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg)) continue;
David Goodwin83704852009-10-26 16:59:04 +0000409 // If NewReg is dead and NewReg's most recent def is not before
410 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000411 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
412 && "Kill and Def maps aren't consistent for AntiDepReg!");
413 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
414 && "Kill and Def maps aren't consistent for NewReg!");
David Goodwin83704852009-10-26 16:59:04 +0000415 if (KillIndices[NewReg] != ~0u ||
416 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
417 KillIndices[AntiDepReg] > DefIndices[NewReg])
418 continue;
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000419 // If NewReg overlaps any of the forbidden registers, we can't use it.
420 bool Forbidden = false;
Craig Toppere1c1d362013-07-03 05:11:49 +0000421 for (SmallVectorImpl<unsigned>::iterator it = Forbid.begin(),
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000422 ite = Forbid.end(); it != ite; ++it)
423 if (TRI->regsOverlap(NewReg, *it)) {
424 Forbidden = true;
425 break;
426 }
427 if (Forbidden) continue;
David Goodwin83704852009-10-26 16:59:04 +0000428 return NewReg;
429 }
430
431 // No registers are free and available!
432 return 0;
433}
434
435unsigned CriticalAntiDepBreaker::
Dan Gohman35bc4d42010-04-19 23:11:58 +0000436BreakAntiDependencies(const std::vector<SUnit>& SUnits,
437 MachineBasicBlock::iterator Begin,
438 MachineBasicBlock::iterator End,
Devang Patelf02a3762011-06-02 21:26:52 +0000439 unsigned InsertPosIndex,
440 DbgValueVector &DbgValues) {
David Goodwin83704852009-10-26 16:59:04 +0000441 // The code below assumes that there is at least one instruction,
442 // so just duck out immediately if the block is empty.
443 if (SUnits.empty()) return 0;
444
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000445 // Keep a map of the MachineInstr*'s back to the SUnit representing them.
446 // This is used for updating debug information.
Andrew Trick46cc9a42012-02-22 06:08:11 +0000447 //
448 // FIXME: Replace this with the existing map in ScheduleDAGInstrs::MISUnitMap
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000449 DenseMap<MachineInstr*,const SUnit*> MISUnitMap;
450
David Goodwin83704852009-10-26 16:59:04 +0000451 // Find the node at the bottom of the critical path.
Craig Topperc0196b12014-04-14 00:51:57 +0000452 const SUnit *Max = nullptr;
David Goodwin83704852009-10-26 16:59:04 +0000453 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000454 const SUnit *SU = &SUnits[i];
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000455 MISUnitMap[SU->getInstr()] = SU;
David Goodwin83704852009-10-26 16:59:04 +0000456 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
457 Max = SU;
458 }
459
460#ifndef NDEBUG
461 {
David Greene96b90532010-01-04 17:47:05 +0000462 DEBUG(dbgs() << "Critical path has total latency "
David Goodwin83704852009-10-26 16:59:04 +0000463 << (Max->getDepth() + Max->Latency) << "\n");
David Greene96b90532010-01-04 17:47:05 +0000464 DEBUG(dbgs() << "Available regs:");
David Goodwin83704852009-10-26 16:59:04 +0000465 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
466 if (KillIndices[Reg] == ~0u)
David Greene96b90532010-01-04 17:47:05 +0000467 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin83704852009-10-26 16:59:04 +0000468 }
David Greene96b90532010-01-04 17:47:05 +0000469 DEBUG(dbgs() << '\n');
David Goodwin83704852009-10-26 16:59:04 +0000470 }
471#endif
472
473 // Track progress along the critical path through the SUnit graph as we walk
474 // the instructions.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000475 const SUnit *CriticalPathSU = Max;
David Goodwin83704852009-10-26 16:59:04 +0000476 MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
477
478 // Consider this pattern:
479 // A = ...
480 // ... = A
481 // A = ...
482 // ... = A
483 // A = ...
484 // ... = A
485 // A = ...
486 // ... = A
487 // There are three anti-dependencies here, and without special care,
488 // we'd break all of them using the same register:
489 // A = ...
490 // ... = A
491 // B = ...
492 // ... = B
493 // B = ...
494 // ... = B
495 // B = ...
496 // ... = B
497 // because at each anti-dependence, B is the first register that
498 // isn't A which is free. This re-introduces anti-dependencies
499 // at all but one of the original anti-dependencies that we were
500 // trying to break. To avoid this, keep track of the most recent
501 // register that each register was replaced with, avoid
502 // using it to repair an anti-dependence on the same register.
503 // This lets us produce this:
504 // A = ...
505 // ... = A
506 // B = ...
507 // ... = B
508 // C = ...
509 // ... = C
510 // B = ...
511 // ... = B
512 // This still has an anti-dependence on B, but at least it isn't on the
513 // original critical path.
514 //
515 // TODO: If we tracked more than one register here, we could potentially
516 // fix that remaining critical edge too. This is a little more involved,
517 // because unlike the most recent register, less recent registers should
518 // still be considered, though only if no other registers are available.
Bill Wendling51a9c0a2010-07-15 19:58:14 +0000519 std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);
David Goodwin83704852009-10-26 16:59:04 +0000520
521 // Attempt to break anti-dependence edges on the critical path. Walk the
522 // instructions from the bottom up, tracking information about liveness
523 // as we go to help determine which registers are available.
524 unsigned Broken = 0;
525 unsigned Count = InsertPosIndex - 1;
Sanjay Patel99475192014-06-24 21:11:51 +0000526 for (MachineBasicBlock::iterator I = End, E = Begin; I != E; --Count) {
David Goodwin83704852009-10-26 16:59:04 +0000527 MachineInstr *MI = --I;
Dale Johannesen2061c842010-03-05 00:02:59 +0000528 if (MI->isDebugValue())
529 continue;
David Goodwin83704852009-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 Grosbach866b74b2010-05-14 21:20:46 +0000539 //
David Goodwin83704852009-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 Gohman35bc4d42010-04-19 23:11:58 +0000546 if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
547 const SUnit *NextSU = Edge->getSUnit();
David Goodwin83704852009-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 Olesenf67bf3e2012-10-15 22:41:03 +0000553 if (!MRI.isAllocatable(AntiDepReg))
David Goodwin83704852009-10-26 16:59:04 +0000554 // Don't break anti-dependencies on non-allocatable registers.
555 AntiDepReg = 0;
Benjamin Kramer5d1bca82012-03-17 20:22:57 +0000556 else if (KeepRegs.test(AntiDepReg))
Sanjay Patel99475192014-06-24 21:11:51 +0000557 // Don't break anti-dependencies if a use down below requires
David Goodwin83704852009-10-26 16:59:04 +0000558 // 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 Gohman35bc4d42010-04-19 23:11:58 +0000569 for (SUnit::const_pred_iterator P = CriticalPathSU->Preds.begin(),
David Goodwin83704852009-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.
Craig Topperc0196b12014-04-14 00:51:57 +0000583 CriticalPathSU = nullptr;
584 CriticalPathMI = nullptr;
David Goodwin83704852009-10-26 16:59:04 +0000585 }
586 }
587
588 PrescanInstruction(MI);
589
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000590 SmallVector<unsigned, 2> ForbidRegs;
591
Evan Chengf128bdc2010-06-16 07:35:02 +0000592 // If MI's defs have a special allocation requirement, don't allow
593 // any def registers to be changed. Also assume all registers
594 // defined in a call must not be changed (ABI).
Sanjay Patel99475192014-06-24 21:11:51 +0000595 if (MI->isCall() || MI->hasExtraDefRegAllocReq() || TII->isPredicated(MI))
David Goodwin83704852009-10-26 16:59:04 +0000596 // If this instruction's defs have special allocation requirement, don't
597 // break this anti-dependency.
598 AntiDepReg = 0;
599 else if (AntiDepReg) {
600 // If this instruction has a use of AntiDepReg, breaking it
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000601 // is invalid. If the instruction defines other registers,
602 // save a list of them so that we don't pick a new register
603 // that overlaps any of them.
David Goodwin83704852009-10-26 16:59:04 +0000604 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
605 MachineOperand &MO = MI->getOperand(i);
606 if (!MO.isReg()) continue;
607 unsigned Reg = MO.getReg();
608 if (Reg == 0) continue;
Evan Chengf128bdc2010-06-16 07:35:02 +0000609 if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
David Goodwin83704852009-10-26 16:59:04 +0000610 AntiDepReg = 0;
611 break;
612 }
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000613 if (MO.isDef() && Reg != AntiDepReg)
614 ForbidRegs.push_back(Reg);
David Goodwin83704852009-10-26 16:59:04 +0000615 }
616 }
617
618 // Determine AntiDepReg's register class, if it is live and is
619 // consistently used within a single class.
Craig Topperc0196b12014-04-14 00:51:57 +0000620 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg]
621 : nullptr;
622 assert((AntiDepReg == 0 || RC != nullptr) &&
David Goodwin83704852009-10-26 16:59:04 +0000623 "Register should be live if it's causing an anti-dependence!");
624 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
625 AntiDepReg = 0;
626
Alp Tokercb402912014-01-24 17:20:08 +0000627 // Look for a suitable register to use to break the anti-dependence.
David Goodwin83704852009-10-26 16:59:04 +0000628 //
629 // TODO: Instead of picking the first free register, consider which might
630 // be the best.
631 if (AntiDepReg != 0) {
Andrew Trick82ae9a92010-11-02 18:16:45 +0000632 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
633 std::multimap<unsigned, MachineOperand *>::iterator>
634 Range = RegRefs.equal_range(AntiDepReg);
635 if (unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,
636 AntiDepReg,
David Goodwin83704852009-10-26 16:59:04 +0000637 LastNewReg[AntiDepReg],
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000638 RC, ForbidRegs)) {
David Greene96b90532010-01-04 17:47:05 +0000639 DEBUG(dbgs() << "Breaking anti-dependence edge on "
David Goodwin83704852009-10-26 16:59:04 +0000640 << TRI->getName(AntiDepReg)
641 << " with " << RegRefs.count(AntiDepReg) << " references"
642 << " using " << TRI->getName(NewReg) << "!\n");
643
644 // Update the references to the old register to refer to the new
645 // register.
David Goodwin83704852009-10-26 16:59:04 +0000646 for (std::multimap<unsigned, MachineOperand *>::iterator
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000647 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
David Goodwin83704852009-10-26 16:59:04 +0000648 Q->second->setReg(NewReg);
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000649 // If the SU for the instruction being updated has debug information
650 // related to the anti-dependency register, make sure to update that
651 // as well.
652 const SUnit *SU = MISUnitMap[Q->second->getParent()];
Jim Grosbach84854832010-06-02 15:29:36 +0000653 if (!SU) continue;
Devang Patelf02a3762011-06-02 21:26:52 +0000654 for (DbgValueVector::iterator DVI = DbgValues.begin(),
655 DVE = DbgValues.end(); DVI != DVE; ++DVI)
656 if (DVI->second == Q->second->getParent())
657 UpdateDbgValue(DVI->first, AntiDepReg, NewReg);
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000658 }
David Goodwin83704852009-10-26 16:59:04 +0000659
660 // We just went back in time and modified history; the
Bob Wilsonc57c2202010-10-02 01:49:29 +0000661 // liveness information for the anti-dependence reg is now
David Goodwin83704852009-10-26 16:59:04 +0000662 // inconsistent. Set the state as if it were dead.
663 Classes[NewReg] = Classes[AntiDepReg];
664 DefIndices[NewReg] = DefIndices[AntiDepReg];
665 KillIndices[NewReg] = KillIndices[AntiDepReg];
666 assert(((KillIndices[NewReg] == ~0u) !=
667 (DefIndices[NewReg] == ~0u)) &&
668 "Kill and Def maps aren't consistent for NewReg!");
669
Craig Topperc0196b12014-04-14 00:51:57 +0000670 Classes[AntiDepReg] = nullptr;
David Goodwin83704852009-10-26 16:59:04 +0000671 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
672 KillIndices[AntiDepReg] = ~0u;
673 assert(((KillIndices[AntiDepReg] == ~0u) !=
674 (DefIndices[AntiDepReg] == ~0u)) &&
675 "Kill and Def maps aren't consistent for AntiDepReg!");
676
677 RegRefs.erase(AntiDepReg);
678 LastNewReg[AntiDepReg] = NewReg;
679 ++Broken;
680 }
681 }
682
683 ScanInstruction(MI, Count);
684 }
685
686 return Broken;
687}