2012-12-12 05:25:42 +08:00
|
|
|
//===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
2012-12-20 06:10:31 +08:00
|
|
|
/// \brief This pass lowers the pseudo control flow instructions to real
|
|
|
|
/// machine instructions.
|
2012-12-12 05:25:42 +08:00
|
|
|
///
|
2012-12-20 06:10:31 +08:00
|
|
|
/// All control flow is handled using predicated instructions and
|
2012-12-12 05:25:42 +08:00
|
|
|
/// a predicate stack. Each Scalar ALU controls the operations of 64 Vector
|
|
|
|
/// ALUs. The Scalar ALU can update the predicate for any of the Vector ALUs
|
|
|
|
/// by writting to the 64-bit EXEC register (each bit corresponds to a
|
|
|
|
/// single vector ALU). Typically, for predicates, a vector ALU will write
|
|
|
|
/// to its bit of the VCC register (like EXEC VCC is 64-bits, one for each
|
|
|
|
/// Vector ALU) and then the ScalarALU will AND the VCC register with the
|
|
|
|
/// EXEC to update the predicates.
|
|
|
|
///
|
|
|
|
/// For example:
|
|
|
|
/// %VCC = V_CMP_GT_F32 %VGPR1, %VGPR2
|
2012-12-20 06:10:31 +08:00
|
|
|
/// %SGPR0 = SI_IF %VCC
|
2012-12-12 05:25:42 +08:00
|
|
|
/// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0
|
2012-12-20 06:10:31 +08:00
|
|
|
/// %SGPR0 = SI_ELSE %SGPR0
|
2012-12-12 05:25:42 +08:00
|
|
|
/// %VGPR0 = V_SUB_F32 %VGPR0, %VGPR0
|
2012-12-20 06:10:31 +08:00
|
|
|
/// SI_END_CF %SGPR0
|
2012-12-12 05:25:42 +08:00
|
|
|
///
|
|
|
|
/// becomes:
|
|
|
|
///
|
|
|
|
/// %SGPR0 = S_AND_SAVEEXEC_B64 %VCC // Save and update the exec mask
|
|
|
|
/// %SGPR0 = S_XOR_B64 %SGPR0, %EXEC // Clear live bits from saved exec mask
|
2012-12-20 06:10:31 +08:00
|
|
|
/// S_CBRANCH_EXECZ label0 // This instruction is an optional
|
2012-12-12 05:25:42 +08:00
|
|
|
/// // optimization which allows us to
|
|
|
|
/// // branch if all the bits of
|
|
|
|
/// // EXEC are zero.
|
|
|
|
/// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0 // Do the IF block of the branch
|
|
|
|
///
|
|
|
|
/// label0:
|
|
|
|
/// %SGPR0 = S_OR_SAVEEXEC_B64 %EXEC // Restore the exec mask for the Then block
|
|
|
|
/// %EXEC = S_XOR_B64 %SGPR0, %EXEC // Clear live bits from saved exec mask
|
|
|
|
/// S_BRANCH_EXECZ label1 // Use our branch optimization
|
|
|
|
/// // instruction again.
|
|
|
|
/// %VGPR0 = V_SUB_F32 %VGPR0, %VGPR // Do the THEN block
|
|
|
|
/// label1:
|
2012-12-20 06:10:31 +08:00
|
|
|
/// %EXEC = S_OR_B64 %EXEC, %SGPR0 // Re-enable saved exec mask bits
|
2012-12-12 05:25:42 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDGPU.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "AMDGPUSubtarget.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "SIInstrInfo.h"
|
|
|
|
#include "SIMachineFunctionInfo.h"
|
2016-06-23 07:40:57 +08:00
|
|
|
#include "llvm/CodeGen/LivePhysRegs.h"
|
2014-09-15 23:41:53 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-02-12 10:16:10 +08:00
|
|
|
#define DEBUG_TYPE "si-lower-control-flow"
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2016-02-12 10:16:10 +08:00
|
|
|
namespace {
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2016-02-12 10:16:10 +08:00
|
|
|
class SILowerControlFlow : public MachineFunctionPass {
|
2012-12-12 05:25:42 +08:00
|
|
|
private:
|
2014-04-30 23:31:33 +08:00
|
|
|
const SIRegisterInfo *TRI;
|
2014-02-11 00:58:30 +08:00
|
|
|
const SIInstrInfo *TII;
|
2016-08-23 03:33:16 +08:00
|
|
|
LiveIntervals *LIS;
|
2016-09-29 09:44:16 +08:00
|
|
|
MachineRegisterInfo *MRI;
|
2012-12-20 06:10:33 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
void emitIf(MachineInstr &MI);
|
|
|
|
void emitElse(MachineInstr &MI);
|
|
|
|
void emitBreak(MachineInstr &MI);
|
|
|
|
void emitIfBreak(MachineInstr &MI);
|
|
|
|
void emitElseBreak(MachineInstr &MI);
|
|
|
|
void emitLoop(MachineInstr &MI);
|
|
|
|
void emitEndCf(MachineInstr &MI);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2016-11-29 02:58:49 +08:00
|
|
|
void findMaskOperands(MachineInstr &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MachineOperand> &Src) const;
|
|
|
|
|
|
|
|
void combineMasks(MachineInstr &MI);
|
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
public:
|
2016-02-12 10:16:10 +08:00
|
|
|
static char ID;
|
|
|
|
|
|
|
|
SILowerControlFlow() :
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineFunctionPass(ID),
|
|
|
|
TRI(nullptr),
|
|
|
|
TII(nullptr),
|
2016-09-29 09:44:16 +08:00
|
|
|
LIS(nullptr),
|
|
|
|
MRI(nullptr) {}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2014-04-29 15:57:24 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override {
|
2016-02-12 10:16:10 +08:00
|
|
|
return "SI Lower control flow pseudo instructions";
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2016-08-23 03:33:16 +08:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2016-09-29 09:44:16 +08:00
|
|
|
// Should preserve the same set that TwoAddressInstructions does.
|
2016-08-23 03:33:16 +08:00
|
|
|
AU.addPreserved<SlotIndexes>();
|
2016-09-29 09:44:16 +08:00
|
|
|
AU.addPreserved<LiveIntervals>();
|
|
|
|
AU.addPreservedID(LiveVariablesID);
|
|
|
|
AU.addPreservedID(MachineLoopInfoID);
|
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
2016-08-23 03:33:16 +08:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
2016-02-12 10:16:10 +08:00
|
|
|
char SILowerControlFlow::ID = 0;
|
|
|
|
|
|
|
|
INITIALIZE_PASS(SILowerControlFlow, DEBUG_TYPE,
|
2016-08-23 03:33:16 +08:00
|
|
|
"SI lower control flow", false, false)
|
2016-02-12 10:16:10 +08:00
|
|
|
|
2016-09-29 09:44:16 +08:00
|
|
|
static void setImpSCCDefDead(MachineInstr &MI, bool IsDead) {
|
|
|
|
MachineOperand &ImpDefSCC = MI.getOperand(3);
|
|
|
|
assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef());
|
|
|
|
|
|
|
|
ImpDefSCC.setIsDead(IsDead);
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
char &llvm::SILowerControlFlowID = SILowerControlFlow::ID;
|
2016-02-12 10:16:10 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
void SILowerControlFlow::emitIf(MachineInstr &MI) {
|
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
|
|
|
MachineBasicBlock::iterator I(&MI);
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineOperand &SaveExec = MI.getOperand(0);
|
|
|
|
MachineOperand &Cond = MI.getOperand(1);
|
|
|
|
assert(SaveExec.getSubReg() == AMDGPU::NoSubRegister &&
|
|
|
|
Cond.getSubReg() == AMDGPU::NoSubRegister);
|
2012-12-20 06:10:33 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
unsigned SaveExecReg = SaveExec.getReg();
|
2013-01-19 05:15:50 +08:00
|
|
|
|
2016-09-29 09:44:16 +08:00
|
|
|
MachineOperand &ImpDefSCC = MI.getOperand(4);
|
|
|
|
assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef());
|
|
|
|
|
|
|
|
// Add an implicit def of exec to discourage scheduling VALU after this which
|
|
|
|
// will interfere with trying to form s_and_saveexec_b64 later.
|
2016-11-22 09:42:34 +08:00
|
|
|
unsigned CopyReg = MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
|
2016-09-29 09:44:16 +08:00
|
|
|
MachineInstr *CopyExec =
|
2016-11-22 09:42:34 +08:00
|
|
|
BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), CopyReg)
|
2016-09-29 09:44:16 +08:00
|
|
|
.addReg(AMDGPU::EXEC)
|
|
|
|
.addReg(AMDGPU::EXEC, RegState::ImplicitDefine);
|
|
|
|
|
|
|
|
unsigned Tmp = MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
|
|
|
|
|
|
|
|
MachineInstr *And =
|
|
|
|
BuildMI(MBB, I, DL, TII->get(AMDGPU::S_AND_B64), Tmp)
|
2016-11-22 09:42:34 +08:00
|
|
|
.addReg(CopyReg)
|
2016-09-29 09:44:16 +08:00
|
|
|
//.addReg(AMDGPU::EXEC)
|
|
|
|
.addReg(Cond.getReg());
|
|
|
|
setImpSCCDefDead(*And, true);
|
2013-01-19 05:15:50 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineInstr *Xor =
|
|
|
|
BuildMI(MBB, I, DL, TII->get(AMDGPU::S_XOR_B64), SaveExecReg)
|
2016-09-29 09:44:16 +08:00
|
|
|
.addReg(Tmp)
|
2016-11-22 09:42:34 +08:00
|
|
|
.addReg(CopyReg);
|
2016-09-29 09:44:16 +08:00
|
|
|
setImpSCCDefDead(*Xor, ImpDefSCC.isDead());
|
|
|
|
|
|
|
|
// Use a copy that is a terminator to get correct spill code placement it with
|
|
|
|
// fast regalloc.
|
|
|
|
MachineInstr *SetExec =
|
|
|
|
BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64_term), AMDGPU::EXEC)
|
|
|
|
.addReg(Tmp, RegState::Kill);
|
2016-07-13 03:01:23 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
// Insert a pseudo terminator to help keep the verifier happy. This will also
|
|
|
|
// be used later when inserting skips.
|
2017-01-13 17:58:52 +08:00
|
|
|
MachineInstr *NewBr = BuildMI(MBB, I, DL, TII->get(AMDGPU::SI_MASK_BRANCH))
|
|
|
|
.add(MI.getOperand(2));
|
2013-01-19 05:15:50 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
if (!LIS) {
|
|
|
|
MI.eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
2016-07-13 03:01:23 +08:00
|
|
|
|
2016-09-29 09:44:16 +08:00
|
|
|
LIS->InsertMachineInstrInMaps(*CopyExec);
|
|
|
|
|
|
|
|
// Replace with and so we don't need to fix the live interval for condition
|
|
|
|
// register.
|
|
|
|
LIS->ReplaceMachineInstrInMaps(MI, *And);
|
2013-01-19 05:15:50 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
LIS->InsertMachineInstrInMaps(*Xor);
|
2016-09-29 09:44:16 +08:00
|
|
|
LIS->InsertMachineInstrInMaps(*SetExec);
|
2016-08-23 03:33:16 +08:00
|
|
|
LIS->InsertMachineInstrInMaps(*NewBr);
|
2016-07-13 03:01:23 +08:00
|
|
|
|
2016-09-29 09:44:16 +08:00
|
|
|
LIS->removeRegUnit(*MCRegUnitIterator(AMDGPU::EXEC, TRI));
|
2016-08-23 03:33:16 +08:00
|
|
|
MI.eraseFromParent();
|
2016-07-13 03:01:23 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
// FIXME: Is there a better way of adjusting the liveness? It shouldn't be
|
|
|
|
// hard to add another def here but I'm not sure how to correctly update the
|
|
|
|
// valno.
|
|
|
|
LIS->removeInterval(SaveExecReg);
|
|
|
|
LIS->createAndComputeVirtRegInterval(SaveExecReg);
|
2016-09-29 09:44:16 +08:00
|
|
|
LIS->createAndComputeVirtRegInterval(Tmp);
|
2016-11-22 09:42:34 +08:00
|
|
|
LIS->createAndComputeVirtRegInterval(CopyReg);
|
2013-01-19 05:15:50 +08:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
void SILowerControlFlow::emitElse(MachineInstr &MI) {
|
2012-12-20 06:10:31 +08:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
2016-08-23 03:33:16 +08:00
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
unsigned DstReg = MI.getOperand(0).getReg();
|
|
|
|
assert(MI.getOperand(0).getSubReg() == AMDGPU::NoSubRegister);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
bool ExecModified = MI.getOperand(3).getImm() != 0;
|
|
|
|
MachineBasicBlock::iterator Start = MBB.begin();
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2016-09-29 09:44:16 +08:00
|
|
|
// We are running before TwoAddressInstructions, and si_else's operands are
|
|
|
|
// tied. In order to correctly tie the registers, split this into a copy of
|
|
|
|
// the src like it does.
|
2016-11-22 09:42:34 +08:00
|
|
|
unsigned CopyReg = MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass);
|
2017-01-20 05:26:22 +08:00
|
|
|
MachineInstr *CopyExec =
|
|
|
|
BuildMI(MBB, Start, DL, TII->get(AMDGPU::COPY), CopyReg)
|
2017-01-13 17:58:52 +08:00
|
|
|
.add(MI.getOperand(1)); // Saved EXEC
|
2016-09-29 09:44:16 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
// This must be inserted before phis and any spill code inserted before the
|
|
|
|
// else.
|
2016-11-22 09:42:34 +08:00
|
|
|
unsigned SaveReg = ExecModified ?
|
|
|
|
MRI->createVirtualRegister(&AMDGPU::SReg_64RegClass) : DstReg;
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineInstr *OrSaveExec =
|
2016-11-22 09:42:34 +08:00
|
|
|
BuildMI(MBB, Start, DL, TII->get(AMDGPU::S_OR_SAVEEXEC_B64), SaveReg)
|
|
|
|
.addReg(CopyReg);
|
2016-09-29 09:44:16 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineBasicBlock *DestBB = MI.getOperand(2).getMBB();
|
2016-08-11 03:11:42 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineBasicBlock::iterator ElsePt(MI);
|
2012-12-20 06:10:33 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
if (ExecModified) {
|
|
|
|
MachineInstr *And =
|
|
|
|
BuildMI(MBB, ElsePt, DL, TII->get(AMDGPU::S_AND_B64), DstReg)
|
|
|
|
.addReg(AMDGPU::EXEC)
|
2016-11-22 09:42:34 +08:00
|
|
|
.addReg(SaveReg);
|
2016-06-23 04:15:28 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
if (LIS)
|
|
|
|
LIS->InsertMachineInstrInMaps(*And);
|
2016-03-22 04:28:33 +08:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineInstr *Xor =
|
2016-09-29 09:44:16 +08:00
|
|
|
BuildMI(MBB, ElsePt, DL, TII->get(AMDGPU::S_XOR_B64_term), AMDGPU::EXEC)
|
2016-08-23 03:33:16 +08:00
|
|
|
.addReg(AMDGPU::EXEC)
|
|
|
|
.addReg(DstReg);
|
2012-12-20 06:10:33 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineInstr *Branch =
|
2016-09-29 09:44:16 +08:00
|
|
|
BuildMI(MBB, ElsePt, DL, TII->get(AMDGPU::SI_MASK_BRANCH))
|
2016-08-27 08:42:21 +08:00
|
|
|
.addMBB(DestBB);
|
2016-08-23 03:33:16 +08:00
|
|
|
|
|
|
|
if (!LIS) {
|
|
|
|
MI.eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
2016-06-23 04:15:28 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
LIS->RemoveMachineInstrFromMaps(MI);
|
2012-12-20 06:10:31 +08:00
|
|
|
MI.eraseFromParent();
|
|
|
|
|
2017-01-20 05:26:22 +08:00
|
|
|
LIS->InsertMachineInstrInMaps(*CopyExec);
|
2016-08-23 03:33:16 +08:00
|
|
|
LIS->InsertMachineInstrInMaps(*OrSaveExec);
|
2016-07-10 01:18:39 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
LIS->InsertMachineInstrInMaps(*Xor);
|
|
|
|
LIS->InsertMachineInstrInMaps(*Branch);
|
2016-07-10 01:18:39 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
// src reg is tied to dst reg.
|
|
|
|
LIS->removeInterval(DstReg);
|
|
|
|
LIS->createAndComputeVirtRegInterval(DstReg);
|
2016-11-22 09:42:34 +08:00
|
|
|
LIS->createAndComputeVirtRegInterval(CopyReg);
|
|
|
|
if (ExecModified)
|
|
|
|
LIS->createAndComputeVirtRegInterval(SaveReg);
|
2016-07-10 01:18:39 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
// Let this be recomputed.
|
|
|
|
LIS->removeRegUnit(*MCRegUnitIterator(AMDGPU::EXEC, TRI));
|
2016-07-10 01:18:39 +08:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
void SILowerControlFlow::emitBreak(MachineInstr &MI) {
|
2012-12-20 06:10:31 +08:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
2016-08-23 03:33:16 +08:00
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
2012-12-20 06:10:31 +08:00
|
|
|
unsigned Dst = MI.getOperand(0).getReg();
|
2016-02-12 10:16:07 +08:00
|
|
|
|
2017-01-13 17:58:52 +08:00
|
|
|
MachineInstr *Or = BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
|
|
|
|
.addReg(AMDGPU::EXEC)
|
|
|
|
.add(MI.getOperand(1));
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
if (LIS)
|
|
|
|
LIS->ReplaceMachineInstrInMaps(MI, *Or);
|
2012-12-20 06:10:31 +08:00
|
|
|
MI.eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
void SILowerControlFlow::emitIfBreak(MachineInstr &MI) {
|
|
|
|
MI.setDesc(TII->get(AMDGPU::S_OR_B64));
|
|
|
|
}
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
void SILowerControlFlow::emitElseBreak(MachineInstr &MI) {
|
|
|
|
MI.setDesc(TII->get(AMDGPU::S_OR_B64));
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
void SILowerControlFlow::emitLoop(MachineInstr &MI) {
|
2012-12-20 06:10:31 +08:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
2016-08-23 03:33:16 +08:00
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineInstr *AndN2 =
|
2017-01-13 17:58:52 +08:00
|
|
|
BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64_term), AMDGPU::EXEC)
|
|
|
|
.addReg(AMDGPU::EXEC)
|
|
|
|
.add(MI.getOperand(0));
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineInstr *Branch =
|
2017-01-13 17:58:52 +08:00
|
|
|
BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
|
|
|
|
.add(MI.getOperand(1));
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
if (LIS) {
|
|
|
|
LIS->ReplaceMachineInstrInMaps(MI, *AndN2);
|
|
|
|
LIS->InsertMachineInstrInMaps(*Branch);
|
|
|
|
}
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
MI.eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
void SILowerControlFlow::emitEndCf(MachineInstr &MI) {
|
2013-01-19 05:15:50 +08:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
2016-08-23 03:33:16 +08:00
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
2013-01-19 05:15:50 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineBasicBlock::iterator InsPt = MBB.begin();
|
|
|
|
MachineInstr *NewMI =
|
2017-01-13 17:58:52 +08:00
|
|
|
BuildMI(MBB, InsPt, DL, TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
|
|
|
|
.addReg(AMDGPU::EXEC)
|
|
|
|
.add(MI.getOperand(0));
|
2016-07-13 05:41:32 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
if (LIS)
|
|
|
|
LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
|
2016-07-13 05:41:32 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MI.eraseFromParent();
|
2016-07-13 05:41:32 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
if (LIS)
|
|
|
|
LIS->handleMove(*NewMI);
|
2016-07-13 05:41:32 +08:00
|
|
|
}
|
|
|
|
|
2016-11-29 02:58:49 +08:00
|
|
|
// Returns replace operands for a logical operation, either single result
|
|
|
|
// for exec or two operands if source was another equivalent operation.
|
|
|
|
void SILowerControlFlow::findMaskOperands(MachineInstr &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MachineOperand> &Src) const {
|
|
|
|
MachineOperand &Op = MI.getOperand(OpNo);
|
|
|
|
if (!Op.isReg() || !TargetRegisterInfo::isVirtualRegister(Op.getReg())) {
|
|
|
|
Src.push_back(Op);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *Def = MRI->getUniqueVRegDef(Op.getReg());
|
|
|
|
if (!Def || Def->getParent() != MI.getParent() ||
|
|
|
|
!(Def->isFullCopy() || (Def->getOpcode() == MI.getOpcode())))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Make sure we do not modify exec between def and use.
|
|
|
|
// A copy with implcitly defined exec inserted earlier is an exclusion, it
|
|
|
|
// does not really modify exec.
|
|
|
|
for (auto I = Def->getIterator(); I != MI.getIterator(); ++I)
|
|
|
|
if (I->modifiesRegister(AMDGPU::EXEC, TRI) &&
|
|
|
|
!(I->isCopy() && I->getOperand(0).getReg() != AMDGPU::EXEC))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (const auto &SrcOp : Def->explicit_operands())
|
|
|
|
if (SrcOp.isUse() && (!SrcOp.isReg() ||
|
|
|
|
TargetRegisterInfo::isVirtualRegister(SrcOp.getReg()) ||
|
|
|
|
SrcOp.getReg() == AMDGPU::EXEC))
|
|
|
|
Src.push_back(SrcOp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search and combine pairs of equivalent instructions, like
|
|
|
|
// S_AND_B64 x, (S_AND_B64 x, y) => S_AND_B64 x, y
|
|
|
|
// S_OR_B64 x, (S_OR_B64 x, y) => S_OR_B64 x, y
|
|
|
|
// One of the operands is exec mask.
|
|
|
|
void SILowerControlFlow::combineMasks(MachineInstr &MI) {
|
|
|
|
assert(MI.getNumExplicitOperands() == 3);
|
|
|
|
SmallVector<MachineOperand, 4> Ops;
|
|
|
|
unsigned OpToReplace = 1;
|
|
|
|
findMaskOperands(MI, 1, Ops);
|
|
|
|
if (Ops.size() == 1) OpToReplace = 2; // First operand can be exec or its copy
|
|
|
|
findMaskOperands(MI, 2, Ops);
|
|
|
|
if (Ops.size() != 3) return;
|
|
|
|
|
|
|
|
unsigned UniqueOpndIdx;
|
|
|
|
if (Ops[0].isIdenticalTo(Ops[1])) UniqueOpndIdx = 2;
|
|
|
|
else if (Ops[0].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1;
|
|
|
|
else if (Ops[1].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1;
|
|
|
|
else return;
|
|
|
|
|
|
|
|
unsigned Reg = MI.getOperand(OpToReplace).getReg();
|
|
|
|
MI.RemoveOperand(OpToReplace);
|
|
|
|
MI.addOperand(Ops[UniqueOpndIdx]);
|
|
|
|
if (MRI->use_empty(Reg))
|
|
|
|
MRI->getUniqueVRegDef(Reg)->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2016-02-12 10:16:10 +08:00
|
|
|
bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
|
2016-06-24 14:30:11 +08:00
|
|
|
const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
|
|
|
|
TII = ST.getInstrInfo();
|
|
|
|
TRI = &TII->getRegisterInfo();
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
// This doesn't actually need LiveIntervals, but we can preserve them.
|
|
|
|
LIS = getAnalysisIfAvailable<LiveIntervals>();
|
2016-09-29 09:44:16 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2016-06-23 04:15:28 +08:00
|
|
|
MachineFunction::iterator NextBB;
|
|
|
|
for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
|
|
|
|
BI != BE; BI = NextBB) {
|
|
|
|
NextBB = std::next(BI);
|
2012-12-20 06:10:31 +08:00
|
|
|
MachineBasicBlock &MBB = *BI;
|
2016-06-23 04:15:28 +08:00
|
|
|
|
2016-11-29 02:58:49 +08:00
|
|
|
MachineBasicBlock::iterator I, Next, Last;
|
2016-03-22 04:28:33 +08:00
|
|
|
|
2016-11-29 02:58:49 +08:00
|
|
|
for (I = MBB.begin(), Last = MBB.end(); I != MBB.end(); I = Next) {
|
2014-03-02 20:27:27 +08:00
|
|
|
Next = std::next(I);
|
2012-12-12 05:25:42 +08:00
|
|
|
MachineInstr &MI = *I;
|
2014-02-11 00:58:30 +08:00
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
switch (MI.getOpcode()) {
|
2016-08-23 03:33:16 +08:00
|
|
|
case AMDGPU::SI_IF:
|
|
|
|
emitIf(MI);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AMDGPU::SI_ELSE:
|
|
|
|
emitElse(MI);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AMDGPU::SI_BREAK:
|
|
|
|
emitBreak(MI);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AMDGPU::SI_IF_BREAK:
|
|
|
|
emitIfBreak(MI);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AMDGPU::SI_ELSE_BREAK:
|
|
|
|
emitElseBreak(MI);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AMDGPU::SI_LOOP:
|
|
|
|
emitLoop(MI);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AMDGPU::SI_END_CF:
|
|
|
|
emitEndCf(MI);
|
|
|
|
break;
|
|
|
|
|
2016-11-29 02:58:49 +08:00
|
|
|
case AMDGPU::S_AND_B64:
|
|
|
|
case AMDGPU::S_OR_B64:
|
|
|
|
// Cleanup bit manipulations on exec mask
|
|
|
|
combineMasks(MI);
|
|
|
|
Last = I;
|
|
|
|
continue;
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
default:
|
2016-11-29 02:58:49 +08:00
|
|
|
Last = I;
|
|
|
|
continue;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2016-11-29 02:58:49 +08:00
|
|
|
|
|
|
|
// Replay newly inserted code to combine masks
|
|
|
|
Next = (Last == MBB.end()) ? MBB.begin() : Last;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-23 03:33:16 +08:00
|
|
|
|
2012-12-20 06:10:31 +08:00
|
|
|
return true;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|