comparison lib/CodeGen/CriticalAntiDepBreaker.cpp @ 120:1172e4bd9c6f

update 4.0.0
author mir3636
date Fri, 25 Nov 2016 19:14:25 +0900
parents afa8332a0e37
children 803732b1fca8
comparison
equal deleted inserted replaced
101:34baf5011add 120:1172e4bd9c6f
67 } 67 }
68 68
69 // Mark live-out callee-saved registers. In a return block this is 69 // Mark live-out callee-saved registers. In a return block this is
70 // all callee-saved registers. In non-return this is any 70 // all callee-saved registers. In non-return this is any
71 // callee-saved register that is not saved in the prolog. 71 // callee-saved register that is not saved in the prolog.
72 const MachineFrameInfo *MFI = MF.getFrameInfo(); 72 const MachineFrameInfo &MFI = MF.getFrameInfo();
73 BitVector Pristine = MFI->getPristineRegs(MF); 73 BitVector Pristine = MFI.getPristineRegs(MF);
74 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) { 74 for (const MCPhysReg *I = TRI->getCalleeSavedRegs(&MF); *I; ++I) {
75 if (!IsReturnBlock && !Pristine.test(*I)) continue; 75 if (!IsReturnBlock && !Pristine.test(*I)) continue;
76 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) { 76 for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) {
77 unsigned Reg = *AI; 77 unsigned Reg = *AI;
78 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1); 78 Classes[Reg] = reinterpret_cast<TargetRegisterClass *>(-1);
85 void CriticalAntiDepBreaker::FinishBlock() { 85 void CriticalAntiDepBreaker::FinishBlock() {
86 RegRefs.clear(); 86 RegRefs.clear();
87 KeepRegs.reset(); 87 KeepRegs.reset();
88 } 88 }
89 89
90 void CriticalAntiDepBreaker::Observe(MachineInstr *MI, unsigned Count, 90 void CriticalAntiDepBreaker::Observe(MachineInstr &MI, unsigned Count,
91 unsigned InsertPosIndex) { 91 unsigned InsertPosIndex) {
92 // Kill instructions can define registers but are really nops, and there might 92 // 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 93 // be a real definition earlier that needs to be paired with uses dominated by
94 // this kill. 94 // this kill.
95 95
96 // FIXME: It may be possible to remove the isKill() restriction once PR18663 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 97 // has been properly fixed. There can be value in processing kills as seen in
98 // the AggressiveAntiDepBreaker class. 98 // the AggressiveAntiDepBreaker class.
99 if (MI->isDebugValue() || MI->isKill()) 99 if (MI.isDebugValue() || MI.isKill())
100 return; 100 return;
101 assert(Count < InsertPosIndex && "Instruction index out of expected range!"); 101 assert(Count < InsertPosIndex && "Instruction index out of expected range!");
102 102
103 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) { 103 for (unsigned Reg = 0; Reg != TRI->getNumRegs(); ++Reg) {
104 if (KillIndices[Reg] != ~0u) { 104 if (KillIndices[Reg] != ~0u) {
144 } 144 }
145 } 145 }
146 return Next; 146 return Next;
147 } 147 }
148 148
149 void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr *MI) { 149 void CriticalAntiDepBreaker::PrescanInstruction(MachineInstr &MI) {
150 // It's not safe to change register allocation for source operands of 150 // It's not safe to change register allocation for source operands of
151 // instructions that have special allocation requirements. Also assume all 151 // instructions that have special allocation requirements. Also assume all
152 // registers used in a call must not be changed (ABI). 152 // registers used in a call must not be changed (ABI).
153 // FIXME: The issue with predicated instruction is more complex. We are being 153 // FIXME: The issue with predicated instruction is more complex. We are being
154 // conservative here because the kill markers cannot be trusted after 154 // conservative here because the kill markers cannot be trusted after
161 // 161 //
162 // The first R6 kill is not really a kill since it's killed by a predicated 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 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 164 // re-define R6 so it's not safe to change it since the last R6 use cannot be
165 // changed. 165 // changed.
166 bool Special = MI->isCall() || 166 bool Special =
167 MI->hasExtraSrcRegAllocReq() || 167 MI.isCall() || MI.hasExtraSrcRegAllocReq() || TII->isPredicated(MI);
168 TII->isPredicated(MI);
169 168
170 // Scan the register operands for this instruction and update 169 // Scan the register operands for this instruction and update
171 // Classes and RegRefs. 170 // Classes and RegRefs.
172 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 171 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
173 MachineOperand &MO = MI->getOperand(i); 172 MachineOperand &MO = MI.getOperand(i);
174 if (!MO.isReg()) continue; 173 if (!MO.isReg()) continue;
175 unsigned Reg = MO.getReg(); 174 unsigned Reg = MO.getReg();
176 if (Reg == 0) continue; 175 if (Reg == 0) continue;
177 const TargetRegisterClass *NewRC = nullptr; 176 const TargetRegisterClass *NewRC = nullptr;
178 177
179 if (i < MI->getDesc().getNumOperands()) 178 if (i < MI.getDesc().getNumOperands())
180 NewRC = TII->getRegClass(MI->getDesc(), i, TRI, MF); 179 NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
181 180
182 // For now, only allow the register to be changed if its register 181 // For now, only allow the register to be changed if its register
183 // class is consistent across all uses. 182 // class is consistent across all uses.
184 if (!Classes[Reg] && NewRC) 183 if (!Classes[Reg] && NewRC)
185 Classes[Reg] = NewRC; 184 Classes[Reg] = NewRC;
210 // def register but not the second (see PR20020 for details). 209 // def register but not the second (see PR20020 for details).
211 // FIXME: can this check be relaxed to account for undef uses 210 // FIXME: can this check be relaxed to account for undef uses
212 // of a register? In the above 'xor' example, the uses of %eax are undef, so 211 // of a register? In the above 'xor' example, the uses of %eax are undef, so
213 // earlier instructions could still replace %eax even though the 'xor' 212 // earlier instructions could still replace %eax even though the 'xor'
214 // itself can't be changed. 213 // itself can't be changed.
215 if (MI->isRegTiedToUseOperand(i) && 214 if (MI.isRegTiedToUseOperand(i) &&
216 Classes[Reg] == reinterpret_cast<TargetRegisterClass *>(-1)) { 215 Classes[Reg] == reinterpret_cast<TargetRegisterClass *>(-1)) {
217 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); 216 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
218 SubRegs.isValid(); ++SubRegs) { 217 SubRegs.isValid(); ++SubRegs) {
219 KeepRegs.set(*SubRegs); 218 KeepRegs.set(*SubRegs);
220 } 219 }
232 } 231 }
233 } 232 }
234 } 233 }
235 } 234 }
236 235
237 void CriticalAntiDepBreaker::ScanInstruction(MachineInstr *MI, 236 void CriticalAntiDepBreaker::ScanInstruction(MachineInstr &MI, unsigned Count) {
238 unsigned Count) {
239 // Update liveness. 237 // Update liveness.
240 // Proceeding upwards, registers that are defed but not used in this 238 // Proceeding upwards, registers that are defed but not used in this
241 // instruction are now dead. 239 // instruction are now dead.
242 assert(!MI->isKill() && "Attempting to scan a kill instruction"); 240 assert(!MI.isKill() && "Attempting to scan a kill instruction");
243 241
244 if (!TII->isPredicated(MI)) { 242 if (!TII->isPredicated(MI)) {
245 // Predicated defs are modeled as read + write, i.e. similar to two 243 // Predicated defs are modeled as read + write, i.e. similar to two
246 // address updates. 244 // address updates.
247 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 245 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
248 MachineOperand &MO = MI->getOperand(i); 246 MachineOperand &MO = MI.getOperand(i);
249 247
250 if (MO.isRegMask()) 248 if (MO.isRegMask())
251 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i) 249 for (unsigned i = 0, e = TRI->getNumRegs(); i != e; ++i)
252 if (MO.clobbersPhysReg(i)) { 250 if (MO.clobbersPhysReg(i)) {
253 DefIndices[i] = Count; 251 DefIndices[i] = Count;
260 if (!MO.isReg()) continue; 258 if (!MO.isReg()) continue;
261 unsigned Reg = MO.getReg(); 259 unsigned Reg = MO.getReg();
262 if (Reg == 0) continue; 260 if (Reg == 0) continue;
263 if (!MO.isDef()) continue; 261 if (!MO.isDef()) continue;
264 262
265 // If we've already marked this reg as unchangeable, carry on.
266 if (KeepRegs.test(Reg)) continue;
267
268 // Ignore two-addr defs. 263 // Ignore two-addr defs.
269 if (MI->isRegTiedToUseOperand(i)) continue; 264 if (MI.isRegTiedToUseOperand(i))
265 continue;
266
267 // If we've already marked this reg as unchangeable, don't remove
268 // it or any of its subregs from KeepRegs.
269 bool Keep = KeepRegs.test(Reg);
270 270
271 // For the reg itself and all subregs: update the def to current; 271 // For the reg itself and all subregs: update the def to current;
272 // reset the kill state, any restrictions, and references. 272 // reset the kill state, any restrictions, and references.
273 for (MCSubRegIterator SRI(Reg, TRI, true); SRI.isValid(); ++SRI) { 273 for (MCSubRegIterator SRI(Reg, TRI, true); SRI.isValid(); ++SRI) {
274 unsigned SubregReg = *SRI; 274 unsigned SubregReg = *SRI;
275 DefIndices[SubregReg] = Count; 275 DefIndices[SubregReg] = Count;
276 KillIndices[SubregReg] = ~0u; 276 KillIndices[SubregReg] = ~0u;
277 KeepRegs.reset(SubregReg);
278 Classes[SubregReg] = nullptr; 277 Classes[SubregReg] = nullptr;
279 RegRefs.erase(SubregReg); 278 RegRefs.erase(SubregReg);
279 if (!Keep)
280 KeepRegs.reset(SubregReg);
280 } 281 }
281 // Conservatively mark super-registers as unusable. 282 // Conservatively mark super-registers as unusable.
282 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) 283 for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR)
283 Classes[*SR] = reinterpret_cast<TargetRegisterClass *>(-1); 284 Classes[*SR] = reinterpret_cast<TargetRegisterClass *>(-1);
284 } 285 }
285 } 286 }
286 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 287 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
287 MachineOperand &MO = MI->getOperand(i); 288 MachineOperand &MO = MI.getOperand(i);
288 if (!MO.isReg()) continue; 289 if (!MO.isReg()) continue;
289 unsigned Reg = MO.getReg(); 290 unsigned Reg = MO.getReg();
290 if (Reg == 0) continue; 291 if (Reg == 0) continue;
291 if (!MO.isUse()) continue; 292 if (!MO.isUse()) continue;
292 293
293 const TargetRegisterClass *NewRC = nullptr; 294 const TargetRegisterClass *NewRC = nullptr;
294 if (i < MI->getDesc().getNumOperands()) 295 if (i < MI.getDesc().getNumOperands())
295 NewRC = TII->getRegClass(MI->getDesc(), i, TRI, MF); 296 NewRC = TII->getRegClass(MI.getDesc(), i, TRI, MF);
296 297
297 // For now, only allow the register to be changed if its register 298 // For now, only allow the register to be changed if its register
298 // class is consistent across all uses. 299 // class is consistent across all uses.
299 if (!Classes[Reg] && NewRC) 300 if (!Classes[Reg] && NewRC)
300 Classes[Reg] = NewRC; 301 Classes[Reg] = NewRC;
508 // instructions from the bottom up, tracking information about liveness 509 // instructions from the bottom up, tracking information about liveness
509 // as we go to help determine which registers are available. 510 // as we go to help determine which registers are available.
510 unsigned Broken = 0; 511 unsigned Broken = 0;
511 unsigned Count = InsertPosIndex - 1; 512 unsigned Count = InsertPosIndex - 1;
512 for (MachineBasicBlock::iterator I = End, E = Begin; I != E; --Count) { 513 for (MachineBasicBlock::iterator I = End, E = Begin; I != E; --Count) {
513 MachineInstr *MI = --I; 514 MachineInstr &MI = *--I;
514 // Kill instructions can define registers but are really nops, and there 515 // Kill instructions can define registers but are really nops, and there
515 // might be a real definition earlier that needs to be paired with uses 516 // might be a real definition earlier that needs to be paired with uses
516 // dominated by this kill. 517 // dominated by this kill.
517 518
518 // FIXME: It may be possible to remove the isKill() restriction once PR18663 519 // FIXME: It may be possible to remove the isKill() restriction once PR18663
519 // has been properly fixed. There can be value in processing kills as seen 520 // has been properly fixed. There can be value in processing kills as seen
520 // in the AggressiveAntiDepBreaker class. 521 // in the AggressiveAntiDepBreaker class.
521 if (MI->isDebugValue() || MI->isKill()) 522 if (MI.isDebugValue() || MI.isKill())
522 continue; 523 continue;
523 524
524 // Check if this instruction has a dependence on the critical path that 525 // Check if this instruction has a dependence on the critical path that
525 // is an anti-dependence that we may be able to break. If it is, set 526 // is an anti-dependence that we may be able to break. If it is, set
526 // AntiDepReg to the non-zero register associated with the anti-dependence. 527 // AntiDepReg to the non-zero register associated with the anti-dependence.
533 // TODO: Instructions with multiple defs could have multiple 534 // TODO: Instructions with multiple defs could have multiple
534 // anti-dependencies. The current code here only knows how to break one 535 // anti-dependencies. The current code here only knows how to break one
535 // edge per instruction. Note that we'd have to be able to break all of 536 // edge per instruction. Note that we'd have to be able to break all of
536 // the anti-dependencies in an instruction in order to be effective. 537 // the anti-dependencies in an instruction in order to be effective.
537 unsigned AntiDepReg = 0; 538 unsigned AntiDepReg = 0;
538 if (MI == CriticalPathMI) { 539 if (&MI == CriticalPathMI) {
539 if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) { 540 if (const SDep *Edge = CriticalPathStep(CriticalPathSU)) {
540 const SUnit *NextSU = Edge->getSUnit(); 541 const SUnit *NextSU = Edge->getSUnit();
541 542
542 // Only consider anti-dependence edges. 543 // Only consider anti-dependence edges.
543 if (Edge->getKind() == SDep::Anti) { 544 if (Edge->getKind() == SDep::Anti) {
583 SmallVector<unsigned, 2> ForbidRegs; 584 SmallVector<unsigned, 2> ForbidRegs;
584 585
585 // If MI's defs have a special allocation requirement, don't allow 586 // If MI's defs have a special allocation requirement, don't allow
586 // any def registers to be changed. Also assume all registers 587 // any def registers to be changed. Also assume all registers
587 // defined in a call must not be changed (ABI). 588 // defined in a call must not be changed (ABI).
588 if (MI->isCall() || MI->hasExtraDefRegAllocReq() || TII->isPredicated(MI)) 589 if (MI.isCall() || MI.hasExtraDefRegAllocReq() || TII->isPredicated(MI))
589 // If this instruction's defs have special allocation requirement, don't 590 // If this instruction's defs have special allocation requirement, don't
590 // break this anti-dependency. 591 // break this anti-dependency.
591 AntiDepReg = 0; 592 AntiDepReg = 0;
592 else if (AntiDepReg) { 593 else if (AntiDepReg) {
593 // If this instruction has a use of AntiDepReg, breaking it 594 // If this instruction has a use of AntiDepReg, breaking it
594 // is invalid. If the instruction defines other registers, 595 // is invalid. If the instruction defines other registers,
595 // save a list of them so that we don't pick a new register 596 // save a list of them so that we don't pick a new register
596 // that overlaps any of them. 597 // that overlaps any of them.
597 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 598 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
598 MachineOperand &MO = MI->getOperand(i); 599 MachineOperand &MO = MI.getOperand(i);
599 if (!MO.isReg()) continue; 600 if (!MO.isReg()) continue;
600 unsigned Reg = MO.getReg(); 601 unsigned Reg = MO.getReg();
601 if (Reg == 0) continue; 602 if (Reg == 0) continue;
602 if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) { 603 if (MO.isUse() && TRI->regsOverlap(AntiDepReg, Reg)) {
603 AntiDepReg = 0; 604 AntiDepReg = 0;
645 const SUnit *SU = MISUnitMap[Q->second->getParent()]; 646 const SUnit *SU = MISUnitMap[Q->second->getParent()];
646 if (!SU) continue; 647 if (!SU) continue;
647 for (DbgValueVector::iterator DVI = DbgValues.begin(), 648 for (DbgValueVector::iterator DVI = DbgValues.begin(),
648 DVE = DbgValues.end(); DVI != DVE; ++DVI) 649 DVE = DbgValues.end(); DVI != DVE; ++DVI)
649 if (DVI->second == Q->second->getParent()) 650 if (DVI->second == Q->second->getParent())
650 UpdateDbgValue(DVI->first, AntiDepReg, NewReg); 651 UpdateDbgValue(*DVI->first, AntiDepReg, NewReg);
651 } 652 }
652 653
653 // We just went back in time and modified history; the 654 // We just went back in time and modified history; the
654 // liveness information for the anti-dependence reg is now 655 // liveness information for the anti-dependence reg is now
655 // inconsistent. Set the state as if it were dead. 656 // inconsistent. Set the state as if it were dead.