bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) { const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); TII = ST.getInstrInfo(); TRI = &TII->getRegisterInfo(); SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); bool HaveKill = false; bool NeedFlat = false; unsigned Depth = 0; MachineFunction::iterator NextBB; for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE; BI = NextBB) { NextBB = std::next(BI); MachineBasicBlock &MBB = *BI; MachineBasicBlock *EmptyMBBAtEnd = nullptr; MachineBasicBlock::iterator I, Next; bool ExecModified = false; for (I = MBB.begin(); I != MBB.end(); I = Next) { Next = std::next(I); MachineInstr &MI = *I; // Flat uses m0 in case it needs to access LDS. if (TII->isFLAT(MI)) NeedFlat = true; for (const auto &Def : I->defs()) { if (Def.isReg() && Def.isDef() && Def.getReg() == AMDGPU::EXEC) { ExecModified = true; break; } } switch (MI.getOpcode()) { default: break; case AMDGPU::SI_IF: ++Depth; If(MI); break; case AMDGPU::SI_ELSE: Else(MI, ExecModified); break; case AMDGPU::SI_BREAK: Break(MI); break; case AMDGPU::SI_IF_BREAK: IfBreak(MI); break; case AMDGPU::SI_ELSE_BREAK: ElseBreak(MI); break; case AMDGPU::SI_LOOP: ++Depth; Loop(MI); break; case AMDGPU::SI_END_CF: if (--Depth == 0 && HaveKill) { SkipIfDead(MI); HaveKill = false; } EndCf(MI); break; case AMDGPU::SI_KILL: if (Depth == 0) SkipIfDead(MI); else HaveKill = true; Kill(MI); break; case AMDGPU::S_BRANCH: Branch(MI); break; case AMDGPU::SI_INDIRECT_SRC_V1: case AMDGPU::SI_INDIRECT_SRC_V2: case AMDGPU::SI_INDIRECT_SRC_V4: case AMDGPU::SI_INDIRECT_SRC_V8: case AMDGPU::SI_INDIRECT_SRC_V16: if (indirectSrc(MI)) { // The block was split at this point. We can safely skip the middle // inserted block to the following which contains the rest of this // block's instructions. NextBB = std::next(BI); BE = MF.end(); Next = MBB.end(); } break; case AMDGPU::SI_INDIRECT_DST_V1: case AMDGPU::SI_INDIRECT_DST_V2: case AMDGPU::SI_INDIRECT_DST_V4: case AMDGPU::SI_INDIRECT_DST_V8: case AMDGPU::SI_INDIRECT_DST_V16: if (indirectDst(MI)) { // The block was split at this point. We can safely skip the middle // inserted block to the following which contains the rest of this // block's instructions. NextBB = std::next(BI); BE = MF.end(); Next = MBB.end(); } break; case AMDGPU::S_ENDPGM: { if (MF.getInfo<SIMachineFunctionInfo>()->returnsVoid()) break; // Graphics shaders returning non-void shouldn't contain S_ENDPGM, // because external bytecode will be appended at the end. if (BI != --MF.end() || I != MBB.getFirstTerminator()) { // S_ENDPGM is not the last instruction. Add an empty block at // the end and jump there. if (!EmptyMBBAtEnd) { EmptyMBBAtEnd = MF.CreateMachineBasicBlock(); MF.insert(MF.end(), EmptyMBBAtEnd); } MBB.addSuccessor(EmptyMBBAtEnd); BuildMI(*BI, I, MI.getDebugLoc(), TII->get(AMDGPU::S_BRANCH)) .addMBB(EmptyMBBAtEnd); } I->eraseFromParent(); break; } } } } if (NeedFlat && MFI->IsKernel) { // TODO: What to use with function calls? // We will need to Initialize the flat scratch register pair. if (NeedFlat) MFI->setHasFlatInstructions(true); } return true; }
bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) { TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo()); TRI = static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo()); SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); bool HaveKill = false; bool NeedWQM = false; bool NeedFlat = false; unsigned Depth = 0; for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE; ++BI) { MachineBasicBlock &MBB = *BI; MachineBasicBlock::iterator I, Next; for (I = MBB.begin(); I != MBB.end(); I = Next) { Next = std::next(I); MachineInstr &MI = *I; if (TII->isWQM(MI) || TII->isDS(MI)) NeedWQM = true; // Flat uses m0 in case it needs to access LDS. if (TII->isFLAT(MI)) NeedFlat = true; switch (MI.getOpcode()) { default: break; case AMDGPU::SI_IF: ++Depth; If(MI); break; case AMDGPU::SI_ELSE: Else(MI); break; case AMDGPU::SI_BREAK: Break(MI); break; case AMDGPU::SI_IF_BREAK: IfBreak(MI); break; case AMDGPU::SI_ELSE_BREAK: ElseBreak(MI); break; case AMDGPU::SI_LOOP: ++Depth; Loop(MI); break; case AMDGPU::SI_END_CF: if (--Depth == 0 && HaveKill) { SkipIfDead(MI); HaveKill = false; } EndCf(MI); break; case AMDGPU::SI_KILL: if (Depth == 0) SkipIfDead(MI); else HaveKill = true; Kill(MI); break; case AMDGPU::S_BRANCH: Branch(MI); break; case AMDGPU::SI_INDIRECT_SRC_V1: case AMDGPU::SI_INDIRECT_SRC_V2: case AMDGPU::SI_INDIRECT_SRC_V4: case AMDGPU::SI_INDIRECT_SRC_V8: case AMDGPU::SI_INDIRECT_SRC_V16: IndirectSrc(MI); break; case AMDGPU::SI_INDIRECT_DST_V1: case AMDGPU::SI_INDIRECT_DST_V2: case AMDGPU::SI_INDIRECT_DST_V4: case AMDGPU::SI_INDIRECT_DST_V8: case AMDGPU::SI_INDIRECT_DST_V16: IndirectDst(MI); break; } } } if (NeedWQM && MFI->getShaderType() == ShaderType::PIXEL) { MachineBasicBlock &MBB = MF.front(); BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64), AMDGPU::EXEC).addReg(AMDGPU::EXEC); } if (NeedFlat && MFI->IsKernel) { // TODO: What to use with function calls? // We will need to Initialize the flat scratch register pair. if (NeedFlat) MFI->setHasFlatInstructions(true); } return true; }