blob: d693af4a0a2d8547bbdbe82d459dc68f1b7aeddf [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000024#include "llvm/Target/TargetSubtargetInfo.h"
David Goodwin83704852009-10-26 16:59:04 +000025
26using namespace llvm;
27
Chandler Carruth1b9dde02014-04-22 02:02:50 +000028#define DEBUG_TYPE "post-RA-sched"
29
Eric Christopherd9134482014-08-04 21:25:23 +000030CriticalAntiDepBreaker::CriticalAntiDepBreaker(MachineFunction &MFi,
31 const RegisterClassInfo &RCI)
32 : AntiDepBreaker(), MF(MFi), MRI(MF.getRegInfo()),
Eric Christopherfc6de422014-08-05 02:39:49 +000033 TII(MF.getSubtarget().getInstrInfo()),
34 TRI(MF.getSubtarget().getRegisterInfo()), RegClassInfo(RCI),
35 Classes(TRI->getNumRegs(), nullptr), KillIndices(TRI->getNumRegs(), 0),
36 DefIndices(TRI->getNumRegs(), 0), KeepRegs(TRI->getNumRegs(), false) {}
David Goodwin83704852009-10-26 16:59:04 +000037
38CriticalAntiDepBreaker::~CriticalAntiDepBreaker() {
39}
40
41void CriticalAntiDepBreaker::StartBlock(MachineBasicBlock *BB) {
David Goodwina45fe672009-12-09 17:18:22 +000042 const unsigned BBSize = BB->size();
Bill Wendling51a9c0a2010-07-15 19:58:14 +000043 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) {
44 // Clear out the register class data.
Craig Topperc0196b12014-04-14 00:51:57 +000045 Classes[i] = nullptr;
Bill Wendling51a9c0a2010-07-15 19:58:14 +000046
47 // Initialize the indices to indicate that no registers are live.
David Goodwina45fe672009-12-09 17:18:22 +000048 KillIndices[i] = ~0u;
49 DefIndices[i] = BBSize;
50 }
David Goodwin83704852009-10-26 16:59:04 +000051
52 // Clear "do not change" set.
Benjamin Kramer5d1bca82012-03-17 20:22:57 +000053 KeepRegs.reset();
David Goodwin83704852009-10-26 16:59:04 +000054
Matthias Braunc2d4bef2015-09-25 21:25:19 +000055 bool IsReturnBlock = BB->isReturnBlock();
David Goodwin83704852009-10-26 16:59:04 +000056
Jakob Stoklund Olesenc3386792013-02-05 18:21:52 +000057 // Examine the live-in regs of all successors.
Evan Chengf128bdc2010-06-16 07:35:02 +000058 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
59 SE = BB->succ_end(); SI != SE; ++SI)
Matthias Braund9da1622015-09-09 18:08:03 +000060 for (const auto &LI : (*SI)->liveins()) {
61 for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +000062 unsigned Reg = *AI;
63 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
64 KillIndices[Reg] = BBSize;
65 DefIndices[Reg] = ~0u;
Evan Chengf128bdc2010-06-16 07:35:02 +000066 }
67 }
68
David Goodwin83704852009-10-26 16:59:04 +000069 // Mark live-out callee-saved registers. In a return block this is
70 // all callee-saved registers. In non-return this is any
71 // callee-saved register that is not saved in the prolog.
72 const MachineFrameInfo *MFI = MF.getFrameInfo();
Matthias Braun111f5d82015-05-28 23:20:35 +000073 BitVector Pristine = MFI->getPristineRegs(MF);
Craig Topper840beec2014-04-04 05:16:06 +000074 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
Jakob Stoklund Olesen92a00832012-06-01 20:36:54 +000075 if (!IsReturnBlock && !Pristine.test(*I)) continue;
76 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
77 unsigned Reg = *AI;
78 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
79 KillIndices[Reg] = BBSize;
80 DefIndices[Reg] = ~0u;
David Goodwin83704852009-10-26 16:59:04 +000081 }
82 }
83}
84
85void CriticalAntiDepBreaker::FinishBlock() {
86 RegRefs.clear();
Benjamin Kramer5d1bca82012-03-17 20:22:57 +000087 KeepRegs.reset();
David Goodwin83704852009-10-26 16:59:04 +000088}
89
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +000090void CriticalAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,
David Goodwin83704852009-10-26 16:59:04 +000091 unsigned InsertPosIndex) {
Sanjay Patelf3cfeef2014-08-20 18:03:00 +000092 // Kill instructions can define registers but are really nops, and there might
93 // be a real definition earlier that needs to be paired with uses dominated by
94 // this kill.
95
96 // FIXME: It may be possible to remove the isKill() restriction once PR18663
97 // has been properly fixed. There can be value in processing kills as seen in
98 // the AggressiveAntiDepBreaker class.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +000099 if (MI.isDebugValue() || MI.isKill())
Dale Johannesen2061c842010-03-05 00:02:59 +0000100 return;
David Goodwin83704852009-10-26 16:59:04 +0000101 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
102
Bob Wilsonc57c2202010-10-02 01:49:29 +0000103 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
104 if (KillIndices[Reg] != ~0u) {
105 // If Reg is currently live, then mark that it can't be renamed as
106 // we don't know the extent of its live-range anymore (now that it
107 // has been scheduled).
108 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
109 KillIndices[Reg] = Count;
110 } else if (DefIndices[Reg] < InsertPosIndex && DefIndices[Reg] >= Count) {
111 // Any register which was defined within the previous scheduling region
112 // may have been rescheduled and its lifetime may overlap with registers
113 // in ways not reflected in our current liveness state. For each such
114 // register, adjust the liveness state to be conservatively correct.
David Goodwin83704852009-10-26 16:59:04 +0000115 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
Bill Wendling51a9c0a2010-07-15 19:58:14 +0000116
David Goodwin83704852009-10-26 16:59:04 +0000117 // Move the def index to the end of the previous region, to reflect
118 // that the def could theoretically have been scheduled at the end.
119 DefIndices[Reg] = InsertPosIndex;
120 }
Bob Wilsonc57c2202010-10-02 01:49:29 +0000121 }
David Goodwin83704852009-10-26 16:59:04 +0000122
123 PrescanInstruction(MI);
124 ScanInstruction(MI, Count);
125}
126
127/// CriticalPathStep - Return the next SUnit after SU on the bottom-up
128/// critical path.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000129static const SDep *CriticalPathStep(const SUnit *SU) {
Craig Topperc0196b12014-04-14 00:51:57 +0000130 const SDep *Next = nullptr;
David Goodwin83704852009-10-26 16:59:04 +0000131 unsigned NextDepth = 0;
132 // Find the predecessor edge with the greatest depth.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000133 for (SUnit::const_pred_iterator P = SU->Preds.begin(), PE = SU->Preds.end();
David Goodwin83704852009-10-26 16:59:04 +0000134 P != PE; ++P) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000135 const SUnit *PredSU = P->getSUnit();
David Goodwin83704852009-10-26 16:59:04 +0000136 unsigned PredLatency = P->getLatency();
137 unsigned PredTotalLatency = PredSU->getDepth() + PredLatency;
138 // In the case of a latency tie, prefer an anti-dependency edge over
139 // other types of edges.
140 if (NextDepth < PredTotalLatency ||
141 (NextDepth == PredTotalLatency && P->getKind() == SDep::Anti)) {
142 NextDepth = PredTotalLatency;
143 Next = &*P;
144 }
145 }
146 return Next;
147}
148
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000149void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr &MI) {
Evan Chengf128bdc2010-06-16 07:35:02 +0000150 // It's not safe to change register allocation for source operands of
Sanjay Patel99475192014-06-24 21:11:51 +0000151 // instructions that have special allocation requirements. Also assume all
152 // registers used in a call must not be changed (ABI).
Evan Chengf128bdc2010-06-16 07:35:02 +0000153 // FIXME: The issue with predicated instruction is more complex. We are being
Bob Wilsonf3ecfd02010-09-10 22:42:21 +0000154 // conservative here because the kill markers cannot be trusted after
Evan Chengf128bdc2010-06-16 07:35:02 +0000155 // if-conversion:
156 // %R6<def> = LDR %SP, %reg0, 92, pred:14, pred:%reg0; mem:LD4[FixedStack14]
157 // ...
158 // STR %R0, %R6<kill>, %reg0, 0, pred:0, pred:%CPSR; mem:ST4[%395]
159 // %R6<def> = LDR %SP, %reg0, 100, pred:0, pred:%CPSR; mem:LD4[FixedStack12]
160 // STR %R0, %R6<kill>, %reg0, 0, pred:14, pred:%reg0; mem:ST4[%396](align=8)
161 //
162 // The first R6 kill is not really a kill since it's killed by a predicated
163 // instruction which may not be executed. The second R6 def may or may not
164 // re-define R6 so it's not safe to change it since the last R6 use cannot be
165 // changed.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000166 bool Special =
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000167 MI.isCall() || MI.hasExtraSrcRegAllocReq() || TII->isPredicated(MI);
Evan Chengf128bdc2010-06-16 07:35:02 +0000168
David Goodwin83704852009-10-26 16:59:04 +0000169 // Scan the register operands for this instruction and update
170 // Classes and RegRefs.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000171 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
172 MachineOperand &MO = MI.getOperand(i);
David Goodwin83704852009-10-26 16:59:04 +0000173 if (!MO.isReg()) continue;
174 unsigned Reg = MO.getReg();
175 if (Reg == 0) continue;
Craig Topperc0196b12014-04-14 00:51:57 +0000176 const TargetRegisterClass *NewRC = nullptr;
Jim Grosbach866b74b2010-05-14 21:20:46 +0000177
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000178 if (i < MI.getDesc().getNumOperands())
179 NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
David Goodwin83704852009-10-26 16:59:04 +0000180
181 // For now, only allow the register to be changed if its register
182 // class is consistent across all uses.
183 if (!Classes[Reg] && NewRC)
184 Classes[Reg] = NewRC;
185 else if (!NewRC || Classes[Reg] != NewRC)
186 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
187
188 // Now check for aliases.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000189 for (MCRegAliasIterator AI(Reg, TRI, false); AI.isValid(); ++AI) {
David Goodwin83704852009-10-26 16:59:04 +0000190 // If an alias of the reg is used during the live range, give up.
191 // Note that this allows us to skip checking if AntiDepReg
192 // overlaps with any of the aliases, among other things.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000193 unsigned AliasReg = *AI;
David Goodwin83704852009-10-26 16:59:04 +0000194 if (Classes[AliasReg]) {
195 Classes[AliasReg] = reinterpret_cast<TargetRegisterClass *>(-1);
196 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
197 }
198 }
199
200 // If we're still willing to consider this register, note the reference.
201 if (Classes[Reg] != reinterpret_cast<TargetRegisterClass *>(-1))
202 RegRefs.insert(std::make_pair(Reg, &MO));
203
Sanjay Pateldc574ab2014-07-03 15:19:40 +0000204 // If this reg is tied and live (Classes[Reg] is set to -1), we can't change
205 // it or any of its sub or super regs. We need to use KeepRegs to mark the
206 // reg because not all uses of the same reg within an instruction are
207 // necessarily tagged as tied.
208 // Example: an x86 "xor %eax, %eax" will have one source operand tied to the
209 // def register but not the second (see PR20020 for details).
210 // FIXME: can this check be relaxed to account for undef uses
211 // of a register? In the above 'xor' example, the uses of %eax are undef, so
212 // earlier instructions could still replace %eax even though the 'xor'
213 // itself can't be changed.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000214 if (MI.isRegTiedToUseOperand(i) &&
Sanjay Pateldc574ab2014-07-03 15:19:40 +0000215 Classes[Reg] == reinterpret_cast<TargetRegisterClass *>(-1)) {
216 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
217 SubRegs.isValid(); ++SubRegs) {
218 KeepRegs.set(*SubRegs);
219 }
220 for (MCSuperRegIterator SuperRegs(Reg, TRI);
221 SuperRegs.isValid(); ++SuperRegs) {
222 KeepRegs.set(*SuperRegs);
223 }
224 }
225
Evan Chengf128bdc2010-06-16 07:35:02 +0000226 if (MO.isUse() && Special) {
Benjamin Kramer5d1bca82012-03-17 20:22:57 +0000227 if (!KeepRegs.test(Reg)) {
Chad Rosierabdb1d62013-05-22 23:17:36 +0000228 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
229 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000230 KeepRegs.set(*SubRegs);
David Goodwin83704852009-10-26 16:59:04 +0000231 }
232 }
233 }
234}
235
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000236void CriticalAntiDepBreaker::ScanInstruction(MachineInstr &MI, unsigned Count) {
David Goodwin83704852009-10-26 16:59:04 +0000237 // Update liveness.
Benjamin Kramerbde91762012-06-02 10:20:22 +0000238 // Proceeding upwards, registers that are defed but not used in this
David Goodwin83704852009-10-26 16:59:04 +0000239 // instruction are now dead.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000240 assert(!MI.isKill() && "Attempting to scan a kill instruction");
David Goodwin83704852009-10-26 16:59:04 +0000241
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000242 if (!TII->isPredicated(MI)) {
Evan Chengf128bdc2010-06-16 07:35:02 +0000243 // Predicated defs are modeled as read + write, i.e. similar to two
244 // address updates.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000245 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
246 MachineOperand &MO = MI.getOperand(i);
Jakob Stoklund Olesen38ce8892012-02-23 01:15:26 +0000247
248 if (MO.isRegMask())
249 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
250 if (MO.clobbersPhysReg(i)) {
251 DefIndices[i] = Count;
252 KillIndices[i] = ~0u;
Benjamin Kramer5d1bca82012-03-17 20:22:57 +0000253 KeepRegs.reset(i);
Craig Topperc0196b12014-04-14 00:51:57 +0000254 Classes[i] = nullptr;
Jakob Stoklund Olesen38ce8892012-02-23 01:15:26 +0000255 RegRefs.erase(i);
256 }
257
Evan Chengf128bdc2010-06-16 07:35:02 +0000258 if (!MO.isReg()) continue;
259 unsigned Reg = MO.getReg();
260 if (Reg == 0) continue;
261 if (!MO.isDef()) continue;
Sanjay Pateldc574ab2014-07-03 15:19:40 +0000262
263 // If we've already marked this reg as unchangeable, carry on.
264 if (KeepRegs.test(Reg)) continue;
265
Evan Chengf128bdc2010-06-16 07:35:02 +0000266 // Ignore two-addr defs.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000267 if (MI.isRegTiedToUseOperand(i))
268 continue;
Evan Chengf128bdc2010-06-16 07:35:02 +0000269
Sanjay Pateld26358e2014-08-06 15:58:15 +0000270 // For the reg itself and all subregs: update the def to current;
271 // reset the kill state, any restrictions, and references.
272 for (MCSubRegIterator SRI(Reg, TRI, true); SRI.isValid(); ++SRI) {
273 unsigned SubregReg = *SRI;
Evan Chengf128bdc2010-06-16 07:35:02 +0000274 DefIndices[SubregReg] = Count;
275 KillIndices[SubregReg] = ~0u;
Benjamin Kramer5d1bca82012-03-17 20:22:57 +0000276 KeepRegs.reset(SubregReg);
Craig Topperc0196b12014-04-14 00:51:57 +0000277 Classes[SubregReg] = nullptr;
Evan Chengf128bdc2010-06-16 07:35:02 +0000278 RegRefs.erase(SubregReg);
279 }
280 // Conservatively mark super-registers as unusable.
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000281 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR)
282 Classes[*SR] = reinterpret_cast<TargetRegisterClass *>(-1);
David Goodwin83704852009-10-26 16:59:04 +0000283 }
284 }
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000285 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
286 MachineOperand &MO = MI.getOperand(i);
David Goodwin83704852009-10-26 16:59:04 +0000287 if (!MO.isReg()) continue;
288 unsigned Reg = MO.getReg();
289 if (Reg == 0) continue;
290 if (!MO.isUse()) continue;
291
Craig Topperc0196b12014-04-14 00:51:57 +0000292 const TargetRegisterClass *NewRC = nullptr;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000293 if (i < MI.getDesc().getNumOperands())
294 NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
David Goodwin83704852009-10-26 16:59:04 +0000295
296 // For now, only allow the register to be changed if its register
297 // class is consistent across all uses.
298 if (!Classes[Reg] && NewRC)
299 Classes[Reg] = NewRC;
300 else if (!NewRC || Classes[Reg] != NewRC)
301 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
302
303 RegRefs.insert(std::make_pair(Reg, &MO));
304
305 // It wasn't previously live but now it is, this is a kill.
Sanjay Pateld26358e2014-08-06 15:58:15 +0000306 // Repeat for all aliases.
307 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +0000308 unsigned AliasReg = *AI;
David Goodwin83704852009-10-26 16:59:04 +0000309 if (KillIndices[AliasReg] == ~0u) {
310 KillIndices[AliasReg] = Count;
311 DefIndices[AliasReg] = ~0u;
312 }
313 }
314 }
315}
316
Andrew Trick4b491872011-02-08 17:39:46 +0000317// Check all machine operands that reference the antidependent register and must
318// be replaced by NewReg. Return true if any of their parent instructions may
319// clobber the new register.
320//
321// Note: AntiDepReg may be referenced by a two-address instruction such that
322// it's use operand is tied to a def operand. We guard against the case in which
323// the two-address instruction also defines NewReg, as may happen with
324// pre/postincrement loads. In this case, both the use and def operands are in
325// RegRefs because the def is inserted by PrescanInstruction and not erased
Sanjay Patel99475192014-06-24 21:11:51 +0000326// during ScanInstruction. So checking for an instruction with definitions of
Andrew Trick4b491872011-02-08 17:39:46 +0000327// both NewReg and AntiDepReg covers it.
Andrew Trick82ae9a92010-11-02 18:16:45 +0000328bool
Andrew Trick4b491872011-02-08 17:39:46 +0000329CriticalAntiDepBreaker::isNewRegClobberedByRefs(RegRefIter RegRefBegin,
330 RegRefIter RegRefEnd,
331 unsigned NewReg)
Andrew Trick82ae9a92010-11-02 18:16:45 +0000332{
333 for (RegRefIter I = RegRefBegin; I != RegRefEnd; ++I ) {
Andrew Trick4b491872011-02-08 17:39:46 +0000334 MachineOperand *RefOper = I->second;
335
336 // Don't allow the instruction defining AntiDepReg to earlyclobber its
337 // operands, in case they may be assigned to NewReg. In this case antidep
338 // breaking must fail, but it's too rare to bother optimizing.
339 if (RefOper->isDef() && RefOper->isEarlyClobber())
Andrew Trick82ae9a92010-11-02 18:16:45 +0000340 return true;
Andrew Trick4b491872011-02-08 17:39:46 +0000341
Sanjay Patel99475192014-06-24 21:11:51 +0000342 // Handle cases in which this instruction defines NewReg.
Andrew Trick4b491872011-02-08 17:39:46 +0000343 MachineInstr *MI = RefOper->getParent();
344 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
345 const MachineOperand &CheckOper = MI->getOperand(i);
346
Jakob Stoklund Olesen38ce8892012-02-23 01:15:26 +0000347 if (CheckOper.isRegMask() && CheckOper.clobbersPhysReg(NewReg))
348 return true;
349
Andrew Trick4b491872011-02-08 17:39:46 +0000350 if (!CheckOper.isReg() || !CheckOper.isDef() ||
351 CheckOper.getReg() != NewReg)
352 continue;
353
354 // Don't allow the instruction to define NewReg and AntiDepReg.
355 // When AntiDepReg is renamed it will be an illegal op.
356 if (RefOper->isDef())
357 return true;
358
359 // Don't allow an instruction using AntiDepReg to be earlyclobbered by
Sanjay Patel99475192014-06-24 21:11:51 +0000360 // NewReg.
Andrew Trick4b491872011-02-08 17:39:46 +0000361 if (CheckOper.isEarlyClobber())
362 return true;
363
Sanjay Patel99475192014-06-24 21:11:51 +0000364 // Don't allow inline asm to define NewReg at all. Who knows what it's
Andrew Trick4b491872011-02-08 17:39:46 +0000365 // doing with it.
366 if (MI->isInlineAsm())
367 return true;
368 }
Andrew Trick82ae9a92010-11-02 18:16:45 +0000369 }
370 return false;
371}
372
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000373unsigned CriticalAntiDepBreaker::
374findSuitableFreeRegister(RegRefIter RegRefBegin,
375 RegRefIter RegRefEnd,
376 unsigned AntiDepReg,
377 unsigned LastNewReg,
378 const TargetRegisterClass *RC,
Craig Topper72cde632013-07-03 05:16:59 +0000379 SmallVectorImpl<unsigned> &Forbid)
Jim Grosbacheb431da2010-01-06 16:48:02 +0000380{
Jakob Stoklund Olesenbdb55e02012-11-29 03:34:17 +0000381 ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(RC);
Jakob Stoklund Olesen4f5f84c2011-06-16 21:56:21 +0000382 for (unsigned i = 0; i != Order.size(); ++i) {
383 unsigned NewReg = Order[i];
David Goodwin83704852009-10-26 16:59:04 +0000384 // Don't replace a register with itself.
385 if (NewReg == AntiDepReg) continue;
386 // Don't replace a register with one that was recently used to repair
387 // an anti-dependence with this AntiDepReg, because that would
388 // re-introduce that anti-dependence.
389 if (NewReg == LastNewReg) continue;
Andrew Trick82ae9a92010-11-02 18:16:45 +0000390 // If any instructions that define AntiDepReg also define the NewReg, it's
391 // not suitable. For example, Instruction with multiple definitions can
392 // result in this condition.
Andrew Trick4b491872011-02-08 17:39:46 +0000393 if (isNewRegClobberedByRefs(RegRefBegin, RegRefEnd, NewReg)) continue;
David Goodwin83704852009-10-26 16:59:04 +0000394 // If NewReg is dead and NewReg's most recent def is not before
395 // AntiDepReg's kill, it's safe to replace AntiDepReg with NewReg.
Jim Grosbacheb431da2010-01-06 16:48:02 +0000396 assert(((KillIndices[AntiDepReg] == ~0u) != (DefIndices[AntiDepReg] == ~0u))
397 && "Kill and Def maps aren't consistent for AntiDepReg!");
398 assert(((KillIndices[NewReg] == ~0u) != (DefIndices[NewReg] == ~0u))
399 && "Kill and Def maps aren't consistent for NewReg!");
David Goodwin83704852009-10-26 16:59:04 +0000400 if (KillIndices[NewReg] != ~0u ||
401 Classes[NewReg] == reinterpret_cast<TargetRegisterClass *>(-1) ||
402 KillIndices[AntiDepReg] > DefIndices[NewReg])
403 continue;
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000404 // If NewReg overlaps any of the forbidden registers, we can't use it.
405 bool Forbidden = false;
Craig Toppere1c1d362013-07-03 05:11:49 +0000406 for (SmallVectorImpl<unsigned>::iterator it = Forbid.begin(),
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000407 ite = Forbid.end(); it != ite; ++it)
408 if (TRI->regsOverlap(NewReg, *it)) {
409 Forbidden = true;
410 break;
411 }
412 if (Forbidden) continue;
David Goodwin83704852009-10-26 16:59:04 +0000413 return NewReg;
414 }
415
416 // No registers are free and available!
417 return 0;
418}
419
420unsigned CriticalAntiDepBreaker::
Dan Gohman35bc4d42010-04-19 23:11:58 +0000421BreakAntiDependencies(const std::vector<SUnit>& SUnits,
422 MachineBasicBlock::iterator Begin,
423 MachineBasicBlock::iterator End,
Devang Patelf02a3762011-06-02 21:26:52 +0000424 unsigned InsertPosIndex,
425 DbgValueVector &DbgValues) {
David Goodwin83704852009-10-26 16:59:04 +0000426 // The code below assumes that there is at least one instruction,
427 // so just duck out immediately if the block is empty.
428 if (SUnits.empty()) return 0;
429
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000430 // Keep a map of the MachineInstr*'s back to the SUnit representing them.
431 // This is used for updating debug information.
Andrew Trick46cc9a42012-02-22 06:08:11 +0000432 //
433 // FIXME: Replace this with the existing map in ScheduleDAGInstrs::MISUnitMap
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000434 DenseMap<MachineInstr*,const SUnit*> MISUnitMap;
435
David Goodwin83704852009-10-26 16:59:04 +0000436 // Find the node at the bottom of the critical path.
Craig Topperc0196b12014-04-14 00:51:57 +0000437 const SUnit *Max = nullptr;
David Goodwin83704852009-10-26 16:59:04 +0000438 for (unsigned i = 0, e = SUnits.size(); i != e; ++i) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000439 const SUnit *SU = &SUnits[i];
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000440 MISUnitMap[SU->getInstr()] = SU;
David Goodwin83704852009-10-26 16:59:04 +0000441 if (!Max || SU->getDepth() + SU->Latency > Max->getDepth() + Max->Latency)
442 Max = SU;
443 }
444
445#ifndef NDEBUG
446 {
David Greene96b90532010-01-04 17:47:05 +0000447 DEBUG(dbgs() << "Critical path has total latency "
David Goodwin83704852009-10-26 16:59:04 +0000448 << (Max->getDepth() + Max->Latency) << "\n");
David Greene96b90532010-01-04 17:47:05 +0000449 DEBUG(dbgs() << "Available regs:");
David Goodwin83704852009-10-26 16:59:04 +0000450 for (unsigned Reg = 0; Reg < TRI->getNumRegs(); ++Reg) {
451 if (KillIndices[Reg] == ~0u)
David Greene96b90532010-01-04 17:47:05 +0000452 DEBUG(dbgs() << " " << TRI->getName(Reg));
David Goodwin83704852009-10-26 16:59:04 +0000453 }
David Greene96b90532010-01-04 17:47:05 +0000454 DEBUG(dbgs() << '\n');
David Goodwin83704852009-10-26 16:59:04 +0000455 }
456#endif
457
458 // Track progress along the critical path through the SUnit graph as we walk
459 // the instructions.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000460 const SUnit *CriticalPathSU = Max;
David Goodwin83704852009-10-26 16:59:04 +0000461 MachineInstr *CriticalPathMI = CriticalPathSU->getInstr();
462
463 // Consider this pattern:
464 // A = ...
465 // ... = A
466 // A = ...
467 // ... = A
468 // A = ...
469 // ... = A
470 // A = ...
471 // ... = A
472 // There are three anti-dependencies here, and without special care,
473 // we'd break all of them using the same register:
474 // A = ...
475 // ... = A
476 // B = ...
477 // ... = B
478 // B = ...
479 // ... = B
480 // B = ...
481 // ... = B
482 // because at each anti-dependence, B is the first register that
483 // isn't A which is free. This re-introduces anti-dependencies
484 // at all but one of the original anti-dependencies that we were
485 // trying to break. To avoid this, keep track of the most recent
486 // register that each register was replaced with, avoid
487 // using it to repair an anti-dependence on the same register.
488 // This lets us produce this:
489 // A = ...
490 // ... = A
491 // B = ...
492 // ... = B
493 // C = ...
494 // ... = C
495 // B = ...
496 // ... = B
497 // This still has an anti-dependence on B, but at least it isn't on the
498 // original critical path.
499 //
500 // TODO: If we tracked more than one register here, we could potentially
501 // fix that remaining critical edge too. This is a little more involved,
502 // because unlike the most recent register, less recent registers should
503 // still be considered, though only if no other registers are available.
Bill Wendling51a9c0a2010-07-15 19:58:14 +0000504 std::vector<unsigned> LastNewReg(TRI->getNumRegs(), 0);
David Goodwin83704852009-10-26 16:59:04 +0000505
506 // Attempt to break anti-dependence edges on the critical path. Walk the
507 // instructions from the bottom up, tracking information about liveness
508 // as we go to help determine which registers are available.
509 unsigned Broken = 0;
510 unsigned Count = InsertPosIndex - 1;
Sanjay Patel99475192014-06-24 21:11:51 +0000511 for (MachineBasicBlock::iterator I = End, E = Begin; I != E; --Count) {
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000512 MachineInstr &MI = *--I;
Sanjay Patelf3cfeef2014-08-20 18:03:00 +0000513 // Kill instructions can define registers but are really nops, and there
514 // might be a real definition earlier that needs to be paired with uses
515 // dominated by this kill.
516
517 // FIXME: It may be possible to remove the isKill() restriction once PR18663
518 // has been properly fixed. There can be value in processing kills as seen
519 // in the AggressiveAntiDepBreaker class.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000520 if (MI.isDebugValue() || MI.isKill())
Dale Johannesen2061c842010-03-05 00:02:59 +0000521 continue;
David Goodwin83704852009-10-26 16:59:04 +0000522
523 // Check if this instruction has a dependence on the critical path that
524 // is an anti-dependence that we may be able to break. If it is, set
525 // AntiDepReg to the non-zero register associated with the anti-dependence.
526 //
527 // We limit our attention to the critical path as a heuristic to avoid
528 // breaking anti-dependence edges that aren't going to significantly
529 // impact the overall schedule. There are a limited number of registers
530 // and we want to save them for the important edges.
Jim Grosbach866b74b2010-05-14 21:20:46 +0000531 //
David Goodwin83704852009-10-26 16:59:04 +0000532 // TODO: Instructions with multiple defs could have multiple
533 // anti-dependencies. The current code here only knows how to break one
534 // edge per instruction. Note that we'd have to be able to break all of
535 // the anti-dependencies in an instruction in order to be effective.
536 unsigned AntiDepReg = 0;
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000537 if (&MI == CriticalPathMI) {
Dan Gohman35bc4d42010-04-19 23:11:58 +0000538 if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
539 const SUnit *NextSU = Edge->getSUnit();
David Goodwin83704852009-10-26 16:59:04 +0000540
541 // Only consider anti-dependence edges.
542 if (Edge->getKind() == SDep::Anti) {
543 AntiDepReg = Edge->getReg();
544 assert(AntiDepReg != 0 && "Anti-dependence on reg0?");
Jakob Stoklund Olesenf67bf3e2012-10-15 22:41:03 +0000545 if (!MRI.isAllocatable(AntiDepReg))
David Goodwin83704852009-10-26 16:59:04 +0000546 // Don't break anti-dependencies on non-allocatable registers.
547 AntiDepReg = 0;
Benjamin Kramer5d1bca82012-03-17 20:22:57 +0000548 else if (KeepRegs.test(AntiDepReg))
Sanjay Patel99475192014-06-24 21:11:51 +0000549 // Don't break anti-dependencies if a use down below requires
David Goodwin83704852009-10-26 16:59:04 +0000550 // this exact register.
551 AntiDepReg = 0;
552 else {
553 // If the SUnit has other dependencies on the SUnit that it
554 // anti-depends on, don't bother breaking the anti-dependency
555 // since those edges would prevent such units from being
556 // scheduled past each other regardless.
557 //
558 // Also, if there are dependencies on other SUnits with the
559 // same register as the anti-dependency, don't attempt to
560 // break it.
Dan Gohman35bc4d42010-04-19 23:11:58 +0000561 for (SUnit::const_pred_iterator P = CriticalPathSU->Preds.begin(),
David Goodwin83704852009-10-26 16:59:04 +0000562 PE = CriticalPathSU->Preds.end(); P != PE; ++P)
563 if (P->getSUnit() == NextSU ?
564 (P->getKind() != SDep::Anti || P->getReg() != AntiDepReg) :
565 (P->getKind() == SDep::Data && P->getReg() == AntiDepReg)) {
566 AntiDepReg = 0;
567 break;
568 }
569 }
570 }
571 CriticalPathSU = NextSU;
572 CriticalPathMI = CriticalPathSU->getInstr();
573 } else {
574 // We've reached the end of the critical path.
Craig Topperc0196b12014-04-14 00:51:57 +0000575 CriticalPathSU = nullptr;
576 CriticalPathMI = nullptr;
David Goodwin83704852009-10-26 16:59:04 +0000577 }
578 }
579
580 PrescanInstruction(MI);
581
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000582 SmallVector<unsigned, 2> ForbidRegs;
583
Evan Chengf128bdc2010-06-16 07:35:02 +0000584 // If MI's defs have a special allocation requirement, don't allow
585 // any def registers to be changed. Also assume all registers
586 // defined in a call must not be changed (ABI).
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000587 if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI))
David Goodwin83704852009-10-26 16:59:04 +0000588 // If this instruction's defs have special allocation requirement, don't
589 // break this anti-dependency.
590 AntiDepReg = 0;
591 else if (AntiDepReg) {
592 // If this instruction has a use of AntiDepReg, breaking it
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000593 // is invalid. If the instruction defines other registers,
594 // save a list of them so that we don't pick a new register
595 // that overlaps any of them.
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000596 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
597 MachineOperand &MO = MI.getOperand(i);
David Goodwin83704852009-10-26 16:59:04 +0000598 if (!MO.isReg()) continue;
599 unsigned Reg = MO.getReg();
600 if (Reg == 0) continue;
Evan Chengf128bdc2010-06-16 07:35:02 +0000601 if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
David Goodwin83704852009-10-26 16:59:04 +0000602 AntiDepReg = 0;
603 break;
604 }
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000605 if (MO.isDef() && Reg != AntiDepReg)
606 ForbidRegs.push_back(Reg);
David Goodwin83704852009-10-26 16:59:04 +0000607 }
608 }
609
610 // Determine AntiDepReg's register class, if it is live and is
611 // consistently used within a single class.
Craig Topperc0196b12014-04-14 00:51:57 +0000612 const TargetRegisterClass *RC = AntiDepReg != 0 ? Classes[AntiDepReg]
613 : nullptr;
614 assert((AntiDepReg == 0 || RC != nullptr) &&
David Goodwin83704852009-10-26 16:59:04 +0000615 "Register should be live if it's causing an anti-dependence!");
616 if (RC == reinterpret_cast<TargetRegisterClass *>(-1))
617 AntiDepReg = 0;
618
Alp Tokercb402912014-01-24 17:20:08 +0000619 // Look for a suitable register to use to break the anti-dependence.
David Goodwin83704852009-10-26 16:59:04 +0000620 //
621 // TODO: Instead of picking the first free register, consider which might
622 // be the best.
623 if (AntiDepReg != 0) {
Andrew Trick82ae9a92010-11-02 18:16:45 +0000624 std::pair<std::multimap<unsigned, MachineOperand *>::iterator,
625 std::multimap<unsigned, MachineOperand *>::iterator>
626 Range = RegRefs.equal_range(AntiDepReg);
627 if (unsigned NewReg = findSuitableFreeRegister(Range.first, Range.second,
628 AntiDepReg,
David Goodwin83704852009-10-26 16:59:04 +0000629 LastNewReg[AntiDepReg],
Bill Schmidt2e4ae4e2013-01-28 18:36:58 +0000630 RC, ForbidRegs)) {
David Greene96b90532010-01-04 17:47:05 +0000631 DEBUG(dbgs() << "Breaking anti-dependence edge on "
David Goodwin83704852009-10-26 16:59:04 +0000632 << TRI->getName(AntiDepReg)
633 << " with " << RegRefs.count(AntiDepReg) << " references"
634 << " using " << TRI->getName(NewReg) << "!\n");
635
636 // Update the references to the old register to refer to the new
637 // register.
David Goodwin83704852009-10-26 16:59:04 +0000638 for (std::multimap<unsigned, MachineOperand *>::iterator
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000639 Q = Range.first, QE = Range.second; Q != QE; ++Q) {
David Goodwin83704852009-10-26 16:59:04 +0000640 Q->second->setReg(NewReg);
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000641 // If the SU for the instruction being updated has debug information
642 // related to the anti-dependency register, make sure to update that
643 // as well.
644 const SUnit *SU = MISUnitMap[Q->second->getParent()];
Jim Grosbach84854832010-06-02 15:29:36 +0000645 if (!SU) continue;
Devang Patelf02a3762011-06-02 21:26:52 +0000646 for (DbgValueVector::iterator DVI = DbgValues.begin(),
647 DVE = DbgValues.end(); DVI != DVE; ++DVI)
648 if (DVI->second == Q->second->getParent())
Duncan P. N. Exon Smith5e6e8c72016-02-27 19:33:37 +0000649 UpdateDbgValue(*DVI->first, AntiDepReg, NewReg);
Jim Grosbach12ac8f02010-06-01 23:48:44 +0000650 }
David Goodwin83704852009-10-26 16:59:04 +0000651
652 // We just went back in time and modified history; the
Bob Wilsonc57c2202010-10-02 01:49:29 +0000653 // liveness information for the anti-dependence reg is now
David Goodwin83704852009-10-26 16:59:04 +0000654 // inconsistent. Set the state as if it were dead.
655 Classes[NewReg] = Classes[AntiDepReg];
656 DefIndices[NewReg] = DefIndices[AntiDepReg];
657 KillIndices[NewReg] = KillIndices[AntiDepReg];
658 assert(((KillIndices[NewReg] == ~0u) !=
659 (DefIndices[NewReg] == ~0u)) &&
660 "Kill and Def maps aren't consistent for NewReg!");
661
Craig Topperc0196b12014-04-14 00:51:57 +0000662 Classes[AntiDepReg] = nullptr;
David Goodwin83704852009-10-26 16:59:04 +0000663 DefIndices[AntiDepReg] = KillIndices[AntiDepReg];
664 KillIndices[AntiDepReg] = ~0u;
665 assert(((KillIndices[AntiDepReg] == ~0u) !=
666 (DefIndices[AntiDepReg] == ~0u)) &&
667 "Kill and Def maps aren't consistent for AntiDepReg!");
668
669 RegRefs.erase(AntiDepReg);
670 LastNewReg[AntiDepReg] = NewReg;
671 ++Broken;
672 }
673 }
674
675 ScanInstruction(MI, Count);
676 }
677
678 return Broken;
679}