75 #define DEBUG_TYPE "si-lower-control-flow" 107 return "SI Lower control flow pseudo instructions";
127 "SI lower control flow",
false,
false)
156 while (!Worklist.empty()) {
159 if (MBB == EMBB || !Visited.
insert(MBB).second)
179 Cond.
getSubReg() == AMDGPU::NoSubRegister);
181 unsigned SaveExecReg = SaveExec.
getReg();
193 unsigned CopyReg = SimpleIf ? SaveExecReg
194 :
MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
196 BuildMI(MBB, I, DL,
TII->get(AMDGPU::COPY), CopyReg)
197 .addReg(AMDGPU::EXEC)
200 unsigned Tmp =
MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
203 BuildMI(MBB, I, DL,
TII->get(AMDGPU::S_AND_B64), Tmp)
207 setImpSCCDefDead(*And,
true);
212 BuildMI(MBB, I, DL,
TII->get(AMDGPU::S_XOR_B64), SaveExecReg)
215 setImpSCCDefDead(*Xor, ImpDefSCC.
isDead());
221 BuildMI(MBB, I, DL,
TII->get(AMDGPU::S_MOV_B64_term), AMDGPU::EXEC)
234 LIS->InsertMachineInstrInMaps(*CopyExec);
238 LIS->ReplaceMachineInstrInMaps(MI, *And);
241 LIS->InsertMachineInstrInMaps(*Xor);
242 LIS->InsertMachineInstrInMaps(*SetExec);
243 LIS->InsertMachineInstrInMaps(*NewBr);
251 LIS->removeInterval(SaveExecReg);
252 LIS->createAndComputeVirtRegInterval(SaveExecReg);
253 LIS->createAndComputeVirtRegInterval(Tmp);
255 LIS->createAndComputeVirtRegInterval(CopyReg);
271 unsigned CopyReg =
MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
273 BuildMI(MBB, Start, DL,
TII->get(AMDGPU::COPY), CopyReg)
278 unsigned SaveReg = ExecModified ?
279 MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass) : DstReg;
281 BuildMI(MBB, Start, DL,
TII->get(AMDGPU::S_OR_SAVEEXEC_B64), SaveReg)
290 BuildMI(MBB, ElsePt, DL,
TII->get(AMDGPU::S_AND_B64), DstReg)
291 .addReg(AMDGPU::EXEC)
295 LIS->InsertMachineInstrInMaps(*And);
299 BuildMI(MBB, ElsePt, DL,
TII->get(AMDGPU::S_XOR_B64_term), AMDGPU::EXEC)
300 .addReg(AMDGPU::EXEC)
304 BuildMI(MBB, ElsePt, DL,
TII->get(AMDGPU::SI_MASK_BRANCH))
312 LIS->RemoveMachineInstrFromMaps(MI);
315 LIS->InsertMachineInstrInMaps(*CopyExec);
316 LIS->InsertMachineInstrInMaps(*OrSaveExec);
318 LIS->InsertMachineInstrInMaps(*Xor);
319 LIS->InsertMachineInstrInMaps(*Branch);
322 LIS->removeInterval(DstReg);
323 LIS->createAndComputeVirtRegInterval(DstReg);
324 LIS->createAndComputeVirtRegInterval(CopyReg);
326 LIS->createAndComputeVirtRegInterval(SaveReg);
338 .addReg(AMDGPU::EXEC)
342 LIS->ReplaceMachineInstrInMaps(MI, *Or);
346 void SILowerControlFlow::emitIfBreak(
MachineInstr &MI) {
355 bool SkipAnding =
false;
367 And =
BuildMI(MBB, &MI, DL,
TII->get(AMDGPU::S_AND_B64), Dst)
368 .addReg(AMDGPU::EXEC)
370 Or =
BuildMI(MBB, &MI, DL,
TII->get(AMDGPU::S_OR_B64), Dst)
374 Or =
BuildMI(MBB, &MI, DL,
TII->get(AMDGPU::S_OR_B64), Dst)
380 LIS->InsertMachineInstrInMaps(*And);
381 LIS->ReplaceMachineInstrInMaps(MI, *Or);
387 void SILowerControlFlow::emitElseBreak(
MachineInstr &MI) {
397 BuildMI(MBB, &MI, DL,
TII->get(AMDGPU::S_ANDN2_B64_term), AMDGPU::EXEC)
398 .addReg(AMDGPU::EXEC)
402 BuildMI(MBB, &MI, DL,
TII->get(AMDGPU::S_CBRANCH_EXECNZ))
406 LIS->ReplaceMachineInstrInMaps(MI, *AndN2);
407 LIS->InsertMachineInstrInMaps(*Branch);
419 BuildMI(MBB, InsPt, DL,
TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
420 .addReg(AMDGPU::EXEC)
424 LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
429 LIS->handleMove(*NewMI);
434 void SILowerControlFlow::findMaskOperands(
MachineInstr &MI,
unsigned OpNo,
451 if (
I->modifiesRegister(AMDGPU::EXEC,
TRI) &&
452 !(
I->isCopy() &&
I->getOperand(0).getReg() != AMDGPU::EXEC))
456 if (SrcOp.isReg() && SrcOp.isUse() &&
458 SrcOp.getReg() == AMDGPU::EXEC))
466 void SILowerControlFlow::combineMasks(
MachineInstr &MI) {
469 unsigned OpToReplace = 1;
470 findMaskOperands(MI, 1, Ops);
471 if (Ops.size() == 1) OpToReplace = 2;
472 findMaskOperands(MI, 2, Ops);
473 if (Ops.size() != 3)
return;
475 unsigned UniqueOpndIdx;
476 if (Ops[0].isIdenticalTo(Ops[1])) UniqueOpndIdx = 2;
477 else if (Ops[0].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1;
478 else if (Ops[1].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1;
484 if (
MRI->use_empty(Reg))
485 MRI->getUniqueVRegDef(Reg)->eraseFromParent();
494 LIS = getAnalysisIfAvailable<LiveIntervals>();
499 BI != BE; BI = NextBB) {
500 NextBB = std::next(BI);
505 for (I = MBB.
begin(), Last = MBB.
end(); I != MBB.
end(); I = Next) {
514 case AMDGPU::SI_ELSE:
518 case AMDGPU::SI_BREAK:
522 case AMDGPU::SI_IF_BREAK:
526 case AMDGPU::SI_ELSE_BREAK:
530 case AMDGPU::SI_LOOP:
534 case AMDGPU::SI_END_CF:
538 case AMDGPU::S_AND_B64:
539 case AMDGPU::S_OR_B64:
551 Next = (Last == MBB.
end()) ? MBB.
begin() : Last;
void push_back(const T &Elt)
const MachineInstrBuilder & add(const MachineOperand &MO) const
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
AMDGPU specific subclass of TargetSubtarget.
MachineBasicBlock * getMBB() const
Compute iterated dominance frontiers using a linear time algorithm.
iterator_range< mop_iterator > explicit_operands()
Implements a dense probed hash-table based set.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
unsigned getReg() const
getReg - Returns the register number.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
unsigned getSubReg() const
INITIALIZE_PASS(SILowerControlFlow, DEBUG_TYPE, "SI lower control flow", false, false) static void setImpSCCDefDead(MachineInstr &MI
const SIInstrInfo * getInstrInfo() const override
unsigned const TargetRegisterInfo * TRI
void setIsDead(bool Val=true)
char & MachineLoopInfoID
MachineLoopInfo - This pass is a loop analysis pass.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
iterator_range< iterator > terminators()
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
static bool isVALU(const MachineInstr &MI)
AnalysisUsage & addPreservedID(const void *ID)
char & LiveVariablesID
LiveVariables pass - This pass computes the set of blocks in which each variable is life and sets mac...
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
std::pair< iterator, bool > insert(const ValueT &V)
Represent the analysis usage information of a pass.
self_iterator getIterator()
succ_iterator succ_begin()
unsigned getNumExplicitOperands() const
Returns the number of non-implicit operands.
Iterator for intrusive lists based on ilist_node.
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
char & SILowerControlFlowID
static bool isSimpleIf(const MachineInstr &MI, const MachineRegisterInfo *MRI, const SIInstrInfo *TII)
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Interface definition for SIInstrInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
static use_instr_nodbg_iterator use_instr_nodbg_end()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
use_instr_nodbg_iterator use_instr_nodbg_begin(unsigned RegNo) const
StringRef - Represent a constant reference to a string, i.e.
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
static bool isKillTerminator(unsigned Opcode)
const MachineOperand & getOperand(unsigned i) const
const SIRegisterInfo * getRegisterInfo() const override