2012-12-11 22:25:42 +01:00
|
|
|
//===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-12-11 22:25:42 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
2018-05-01 17:54:18 +02:00
|
|
|
/// This pass lowers the pseudo control flow instructions to real
|
2012-12-19 23:10:31 +01:00
|
|
|
/// machine instructions.
|
2012-12-11 22:25:42 +01:00
|
|
|
///
|
2012-12-19 23:10:31 +01:00
|
|
|
/// All control flow is handled using predicated instructions and
|
2012-12-11 22:25:42 +01: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:
|
2017-11-28 18:15:09 +01:00
|
|
|
/// %vcc = V_CMP_GT_F32 %vgpr1, %vgpr2
|
|
|
|
/// %sgpr0 = SI_IF %vcc
|
|
|
|
/// %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0
|
|
|
|
/// %sgpr0 = SI_ELSE %sgpr0
|
|
|
|
/// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr0
|
|
|
|
/// SI_END_CF %sgpr0
|
2012-12-11 22:25:42 +01:00
|
|
|
///
|
|
|
|
/// becomes:
|
|
|
|
///
|
2017-11-28 18:15:09 +01:00
|
|
|
/// %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-19 23:10:31 +01:00
|
|
|
/// S_CBRANCH_EXECZ label0 // This instruction is an optional
|
2012-12-11 22:25:42 +01:00
|
|
|
/// // optimization which allows us to
|
|
|
|
/// // branch if all the bits of
|
|
|
|
/// // EXEC are zero.
|
2017-11-28 18:15:09 +01:00
|
|
|
/// %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0 // Do the IF block of the branch
|
2012-12-11 22:25:42 +01:00
|
|
|
///
|
|
|
|
/// label0:
|
2020-04-06 17:44:08 +02:00
|
|
|
/// %sgpr0 = S_OR_SAVEEXEC_B64 %sgpr0 // Restore the exec mask for the Then block
|
|
|
|
/// %exec = S_XOR_B64 %sgpr0, %exec // Update the exec mask
|
2012-12-11 22:25:42 +01:00
|
|
|
/// S_BRANCH_EXECZ label1 // Use our branch optimization
|
|
|
|
/// // instruction again.
|
2017-11-28 18:15:09 +01:00
|
|
|
/// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr // Do the THEN block
|
2012-12-11 22:25:42 +01:00
|
|
|
/// label1:
|
2017-11-28 18:15:09 +01:00
|
|
|
/// %exec = S_OR_B64 %exec, %sgpr0 // Re-enable saved exec mask bits
|
2012-12-11 22:25:42 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDGPU.h"
|
2021-01-20 13:48:02 +01:00
|
|
|
#include "GCNSubtarget.h"
|
|
|
|
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
|
2020-03-11 21:17:32 +01:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2017-12-13 03:51:04 +01:00
|
|
|
#include "llvm/CodeGen/LiveIntervals.h"
|
2012-12-11 22:25:42 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-02-12 03:16:10 +01:00
|
|
|
#define DEBUG_TYPE "si-lower-control-flow"
|
2012-12-11 22:25:42 +01:00
|
|
|
|
2020-03-13 18:58:19 +01:00
|
|
|
static cl::opt<bool>
|
|
|
|
RemoveRedundantEndcf("amdgpu-remove-redundant-endcf",
|
2020-03-13 20:44:59 +01:00
|
|
|
cl::init(true), cl::ReallyHidden);
|
2020-03-13 18:58:19 +01:00
|
|
|
|
2016-02-12 03:16:10 +01:00
|
|
|
namespace {
|
2012-12-11 22:25:42 +01:00
|
|
|
|
2016-02-12 03:16:10 +01:00
|
|
|
class SILowerControlFlow : public MachineFunctionPass {
|
2012-12-11 22:25:42 +01:00
|
|
|
private:
|
2017-01-20 18:52:16 +01:00
|
|
|
const SIRegisterInfo *TRI = nullptr;
|
|
|
|
const SIInstrInfo *TII = nullptr;
|
2019-08-01 03:25:27 +02:00
|
|
|
LiveIntervals *LIS = nullptr;
|
2019-08-20 19:45:25 +02:00
|
|
|
MachineRegisterInfo *MRI = nullptr;
|
2020-04-07 02:03:40 +02:00
|
|
|
SetVector<MachineInstr*> LoweredEndCf;
|
2020-03-13 20:44:59 +01:00
|
|
|
DenseSet<Register> LoweredIf;
|
2021-02-14 01:52:41 +01:00
|
|
|
SmallSet<MachineBasicBlock *, 4> KillBlocks;
|
2012-12-19 23:10:33 +01:00
|
|
|
|
2019-06-16 19:13:09 +02:00
|
|
|
const TargetRegisterClass *BoolRC = nullptr;
|
|
|
|
unsigned AndOpc;
|
|
|
|
unsigned OrOpc;
|
|
|
|
unsigned XorOpc;
|
|
|
|
unsigned MovTermOpc;
|
|
|
|
unsigned Andn2TermOpc;
|
|
|
|
unsigned XorTermrOpc;
|
2020-09-10 21:49:09 +02:00
|
|
|
unsigned OrTermrOpc;
|
2019-06-16 19:13:09 +02:00
|
|
|
unsigned OrSaveExecOpc;
|
|
|
|
unsigned Exec;
|
|
|
|
|
2021-02-14 01:52:41 +01:00
|
|
|
bool hasKill(const MachineBasicBlock *Begin, const MachineBasicBlock *End);
|
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
void emitIf(MachineInstr &MI);
|
|
|
|
void emitElse(MachineInstr &MI);
|
|
|
|
void emitIfBreak(MachineInstr &MI);
|
|
|
|
void emitLoop(MachineInstr &MI);
|
2020-09-10 21:49:09 +02:00
|
|
|
|
|
|
|
MachineBasicBlock *emitEndCf(MachineInstr &MI);
|
2012-12-11 22:25:42 +01:00
|
|
|
|
2021-01-25 00:31:08 +01:00
|
|
|
void lowerInitExec(MachineBasicBlock *MBB, MachineInstr &MI);
|
|
|
|
|
2016-11-28 19:58:49 +01:00
|
|
|
void findMaskOperands(MachineInstr &MI, unsigned OpNo,
|
|
|
|
SmallVectorImpl<MachineOperand> &Src) const;
|
|
|
|
|
|
|
|
void combineMasks(MachineInstr &MI);
|
|
|
|
|
2020-09-07 17:57:27 +02:00
|
|
|
bool removeMBBifRedundant(MachineBasicBlock &MBB);
|
|
|
|
|
2020-09-10 21:49:09 +02:00
|
|
|
MachineBasicBlock *process(MachineInstr &MI);
|
2020-07-03 05:25:33 +02:00
|
|
|
|
2020-03-11 21:17:32 +01:00
|
|
|
// Skip to the next instruction, ignoring debug instructions, and trivial
|
|
|
|
// block boundaries (blocks that have one (typically fallthrough) successor,
|
|
|
|
// and the successor has one predecessor.
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
skipIgnoreExecInstsTrivialSucc(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator It) const;
|
|
|
|
|
2020-07-23 03:24:21 +02:00
|
|
|
/// Find the insertion point for a new conditional branch.
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
skipToUncondBrOrEnd(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator I) const {
|
|
|
|
assert(I->isTerminator());
|
|
|
|
|
|
|
|
// FIXME: What if we had multiple pre-existing conditional branches?
|
|
|
|
MachineBasicBlock::iterator End = MBB.end();
|
|
|
|
while (I != End && !I->isUnconditionalBranch())
|
|
|
|
++I;
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
|
2020-04-07 02:03:40 +02:00
|
|
|
// Remove redundant SI_END_CF instructions.
|
|
|
|
void optimizeEndCf();
|
|
|
|
|
2012-12-11 22:25:42 +01:00
|
|
|
public:
|
2016-02-12 03:16:10 +01:00
|
|
|
static char ID;
|
|
|
|
|
2017-01-20 18:52:16 +01:00
|
|
|
SILowerControlFlow() : MachineFunctionPass(ID) {}
|
2012-12-11 22:25:42 +01:00
|
|
|
|
2014-04-29 09:57:24 +02:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2012-12-11 22:25:42 +01:00
|
|
|
|
2016-10-01 04:56:57 +02:00
|
|
|
StringRef getPassName() const override {
|
2016-02-12 03:16:10 +01:00
|
|
|
return "SI Lower control flow pseudo instructions";
|
2012-12-11 22:25:42 +01:00
|
|
|
}
|
2016-08-22 21:33:16 +02:00
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2016-09-29 03:44:16 +02:00
|
|
|
// Should preserve the same set that TwoAddressInstructions does.
|
2016-08-22 21:33:16 +02:00
|
|
|
AU.addPreserved<SlotIndexes>();
|
2016-09-29 03:44:16 +02:00
|
|
|
AU.addPreserved<LiveIntervals>();
|
|
|
|
AU.addPreservedID(LiveVariablesID);
|
2016-08-22 21:33:16 +02:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2012-12-11 22:25:42 +01:00
|
|
|
};
|
|
|
|
|
2017-01-20 18:52:16 +01:00
|
|
|
} // end anonymous namespace
|
2012-12-11 22:25:42 +01:00
|
|
|
|
2016-02-12 03:16:10 +01:00
|
|
|
char SILowerControlFlow::ID = 0;
|
|
|
|
|
|
|
|
INITIALIZE_PASS(SILowerControlFlow, DEBUG_TYPE,
|
2016-08-22 21:33:16 +02:00
|
|
|
"SI lower control flow", false, false)
|
2016-02-12 03:16:10 +01:00
|
|
|
|
2016-09-29 03:44:16 +02: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-22 21:33:16 +02:00
|
|
|
char &llvm::SILowerControlFlowID = SILowerControlFlow::ID;
|
2016-02-12 03:16:10 +01:00
|
|
|
|
2021-02-14 01:52:41 +01:00
|
|
|
bool SILowerControlFlow::hasKill(const MachineBasicBlock *Begin,
|
|
|
|
const MachineBasicBlock *End) {
|
2017-08-04 08:58:42 +02:00
|
|
|
DenseSet<const MachineBasicBlock*> Visited;
|
2021-01-03 18:57:45 +01:00
|
|
|
SmallVector<MachineBasicBlock *, 4> Worklist(Begin->successors());
|
2017-08-04 08:58:42 +02:00
|
|
|
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
MachineBasicBlock *MBB = Worklist.pop_back_val();
|
|
|
|
|
2020-07-03 05:25:33 +02:00
|
|
|
if (MBB == End || !Visited.insert(MBB).second)
|
2017-08-04 08:58:42 +02:00
|
|
|
continue;
|
2021-02-14 01:52:41 +01:00
|
|
|
if (KillBlocks.contains(MBB))
|
|
|
|
return true;
|
2017-08-04 08:58:42 +02:00
|
|
|
|
|
|
|
Worklist.append(MBB->succ_begin(), MBB->succ_end());
|
|
|
|
}
|
|
|
|
|
2020-07-03 05:25:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isSimpleIf(const MachineInstr &MI, const MachineRegisterInfo *MRI) {
|
|
|
|
Register SaveExecReg = MI.getOperand(0).getReg();
|
|
|
|
auto U = MRI->use_instr_nodbg_begin(SaveExecReg);
|
|
|
|
|
|
|
|
if (U == MRI->use_instr_nodbg_end() ||
|
|
|
|
std::next(U) != MRI->use_instr_nodbg_end() ||
|
|
|
|
U->getOpcode() != AMDGPU::SI_END_CF)
|
|
|
|
return false;
|
|
|
|
|
2017-08-04 08:58:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
void SILowerControlFlow::emitIf(MachineInstr &MI) {
|
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
|
|
|
MachineBasicBlock::iterator I(&MI);
|
2019-12-27 19:11:06 +01:00
|
|
|
Register SaveExecReg = MI.getOperand(0).getReg();
|
2019-09-17 11:08:58 +02:00
|
|
|
MachineOperand& Cond = MI.getOperand(1);
|
|
|
|
assert(Cond.getSubReg() == AMDGPU::NoSubRegister);
|
2013-01-18 22:15:50 +01:00
|
|
|
|
2016-09-29 03:44:16 +02:00
|
|
|
MachineOperand &ImpDefSCC = MI.getOperand(4);
|
|
|
|
assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef());
|
|
|
|
|
2017-07-26 23:29:15 +02:00
|
|
|
// If there is only one use of save exec register and that use is SI_END_CF,
|
|
|
|
// we can optimize SI_IF by returning the full saved exec mask instead of
|
|
|
|
// just cleared bits.
|
2020-07-03 05:25:33 +02:00
|
|
|
bool SimpleIf = isSimpleIf(MI, MRI);
|
|
|
|
|
2021-02-10 04:56:45 +01:00
|
|
|
if (SimpleIf) {
|
2020-07-03 05:25:33 +02:00
|
|
|
// Check for SI_KILL_*_TERMINATOR on path from if to endif.
|
|
|
|
// if there is any such terminator simplifications are not safe.
|
|
|
|
auto UseMI = MRI->use_instr_nodbg_begin(SaveExecReg);
|
2021-02-14 01:52:41 +01:00
|
|
|
SimpleIf = !hasKill(MI.getParent(), UseMI->getParent());
|
2020-07-03 05:25:33 +02:00
|
|
|
}
|
2017-07-26 23:29:15 +02:00
|
|
|
|
2016-09-29 03:44:16 +02:00
|
|
|
// Add an implicit def of exec to discourage scheduling VALU after this which
|
|
|
|
// will interfere with trying to form s_and_saveexec_b64 later.
|
2019-06-24 17:50:29 +02:00
|
|
|
Register CopyReg = SimpleIf ? SaveExecReg
|
2019-06-16 19:13:09 +02:00
|
|
|
: MRI->createVirtualRegister(BoolRC);
|
2016-09-29 03:44:16 +02:00
|
|
|
MachineInstr *CopyExec =
|
2016-11-22 02:42:34 +01:00
|
|
|
BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), CopyReg)
|
2019-06-16 19:13:09 +02:00
|
|
|
.addReg(Exec)
|
|
|
|
.addReg(Exec, RegState::ImplicitDefine);
|
2020-03-13 20:44:59 +01:00
|
|
|
LoweredIf.insert(CopyReg);
|
2016-09-29 03:44:16 +02:00
|
|
|
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-15 21:22:08 +02:00
|
|
|
Register Tmp = MRI->createVirtualRegister(BoolRC);
|
2016-09-29 03:44:16 +02:00
|
|
|
|
|
|
|
MachineInstr *And =
|
2019-06-16 19:13:09 +02:00
|
|
|
BuildMI(MBB, I, DL, TII->get(AndOpc), Tmp)
|
2016-11-22 02:42:34 +01:00
|
|
|
.addReg(CopyReg)
|
2019-03-05 19:38:00 +01:00
|
|
|
.add(Cond);
|
|
|
|
|
2016-09-29 03:44:16 +02:00
|
|
|
setImpSCCDefDead(*And, true);
|
2013-01-18 22:15:50 +01:00
|
|
|
|
2017-07-26 23:29:15 +02:00
|
|
|
MachineInstr *Xor = nullptr;
|
|
|
|
if (!SimpleIf) {
|
|
|
|
Xor =
|
2019-06-16 19:13:09 +02:00
|
|
|
BuildMI(MBB, I, DL, TII->get(XorOpc), SaveExecReg)
|
2017-07-26 23:29:15 +02:00
|
|
|
.addReg(Tmp)
|
|
|
|
.addReg(CopyReg);
|
|
|
|
setImpSCCDefDead(*Xor, ImpDefSCC.isDead());
|
|
|
|
}
|
2016-09-29 03:44:16 +02:00
|
|
|
|
|
|
|
// Use a copy that is a terminator to get correct spill code placement it with
|
|
|
|
// fast regalloc.
|
|
|
|
MachineInstr *SetExec =
|
2019-06-16 19:13:09 +02:00
|
|
|
BuildMI(MBB, I, DL, TII->get(MovTermOpc), Exec)
|
2016-09-29 03:44:16 +02:00
|
|
|
.addReg(Tmp, RegState::Kill);
|
2016-07-12 21:01:23 +02:00
|
|
|
|
2020-07-23 03:24:21 +02:00
|
|
|
// Skip ahead to the unconditional branch in case there are other terminators
|
|
|
|
// present.
|
|
|
|
I = skipToUncondBrOrEnd(MBB, I);
|
|
|
|
|
2020-01-22 05:07:55 +01:00
|
|
|
// Insert the S_CBRANCH_EXECZ instruction which will be optimized later
|
|
|
|
// during SIRemoveShortExecBranches.
|
|
|
|
MachineInstr *NewBr = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
|
2017-01-13 10:58:52 +01:00
|
|
|
.add(MI.getOperand(2));
|
2013-01-18 22:15:50 +01:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
if (!LIS) {
|
|
|
|
MI.eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
2016-07-12 21:01:23 +02:00
|
|
|
|
2016-09-29 03:44:16 +02: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-18 22:15:50 +01:00
|
|
|
|
2017-07-26 23:29:15 +02:00
|
|
|
if (!SimpleIf)
|
|
|
|
LIS->InsertMachineInstrInMaps(*Xor);
|
2016-09-29 03:44:16 +02:00
|
|
|
LIS->InsertMachineInstrInMaps(*SetExec);
|
2016-08-22 21:33:16 +02:00
|
|
|
LIS->InsertMachineInstrInMaps(*NewBr);
|
2016-07-12 21:01:23 +02:00
|
|
|
|
2019-08-20 19:45:25 +02:00
|
|
|
LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC);
|
2016-08-22 21:33:16 +02:00
|
|
|
MI.eraseFromParent();
|
2016-07-12 21:01:23 +02:00
|
|
|
|
2016-08-22 21:33:16 +02: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 03:44:16 +02:00
|
|
|
LIS->createAndComputeVirtRegInterval(Tmp);
|
2017-07-26 23:29:15 +02:00
|
|
|
if (!SimpleIf)
|
|
|
|
LIS->createAndComputeVirtRegInterval(CopyReg);
|
2013-01-18 22:15:50 +01:00
|
|
|
}
|
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
void SILowerControlFlow::emitElse(MachineInstr &MI) {
|
2012-12-19 23:10:31 +01:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
2016-08-22 21:33:16 +02:00
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
2012-12-19 23:10:31 +01:00
|
|
|
|
2019-12-27 19:11:06 +01:00
|
|
|
Register DstReg = MI.getOperand(0).getReg();
|
2012-12-19 23:10:31 +01:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
MachineBasicBlock::iterator Start = MBB.begin();
|
2012-12-19 23:10:31 +01:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
// This must be inserted before phis and any spill code inserted before the
|
|
|
|
// else.
|
2020-10-20 11:11:51 +02:00
|
|
|
Register SaveReg = MRI->createVirtualRegister(BoolRC);
|
2016-08-22 21:33:16 +02:00
|
|
|
MachineInstr *OrSaveExec =
|
2019-06-16 19:13:09 +02:00
|
|
|
BuildMI(MBB, Start, DL, TII->get(OrSaveExecOpc), SaveReg)
|
2020-09-02 17:36:22 +02:00
|
|
|
.add(MI.getOperand(1)); // Saved EXEC
|
2016-09-29 03:44:16 +02:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
MachineBasicBlock *DestBB = MI.getOperand(2).getMBB();
|
2016-08-10 21:11:42 +02:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
MachineBasicBlock::iterator ElsePt(MI);
|
2012-12-19 23:10:33 +01:00
|
|
|
|
2020-10-20 11:11:51 +02:00
|
|
|
// This accounts for any modification of the EXEC mask within the block and
|
|
|
|
// can be optimized out pre-RA when not required.
|
|
|
|
MachineInstr *And = BuildMI(MBB, ElsePt, DL, TII->get(AndOpc), DstReg)
|
|
|
|
.addReg(Exec)
|
|
|
|
.addReg(SaveReg);
|
2016-06-22 22:15:28 +02:00
|
|
|
|
2020-10-20 11:11:51 +02:00
|
|
|
if (LIS)
|
|
|
|
LIS->InsertMachineInstrInMaps(*And);
|
2016-03-21 21:28:33 +01:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
MachineInstr *Xor =
|
2019-06-16 19:13:09 +02:00
|
|
|
BuildMI(MBB, ElsePt, DL, TII->get(XorTermrOpc), Exec)
|
|
|
|
.addReg(Exec)
|
2016-08-22 21:33:16 +02:00
|
|
|
.addReg(DstReg);
|
2012-12-19 23:10:33 +01:00
|
|
|
|
2020-07-23 03:24:21 +02:00
|
|
|
// Skip ahead to the unconditional branch in case there are other terminators
|
|
|
|
// present.
|
|
|
|
ElsePt = skipToUncondBrOrEnd(MBB, ElsePt);
|
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
MachineInstr *Branch =
|
2020-01-22 05:07:55 +01:00
|
|
|
BuildMI(MBB, ElsePt, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
|
|
|
|
.addMBB(DestBB);
|
2016-08-22 21:33:16 +02:00
|
|
|
|
|
|
|
if (!LIS) {
|
|
|
|
MI.eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
2016-06-22 22:15:28 +02:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
LIS->RemoveMachineInstrFromMaps(MI);
|
2012-12-19 23:10:31 +01:00
|
|
|
MI.eraseFromParent();
|
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
LIS->InsertMachineInstrInMaps(*OrSaveExec);
|
2016-07-09 19:18:39 +02:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
LIS->InsertMachineInstrInMaps(*Xor);
|
|
|
|
LIS->InsertMachineInstrInMaps(*Branch);
|
2016-07-09 19:18:39 +02:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
LIS->removeInterval(DstReg);
|
|
|
|
LIS->createAndComputeVirtRegInterval(DstReg);
|
2020-10-20 11:11:51 +02:00
|
|
|
LIS->createAndComputeVirtRegInterval(SaveReg);
|
2016-07-09 19:18:39 +02:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
// Let this be recomputed.
|
2019-08-20 19:45:25 +02:00
|
|
|
LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC);
|
2016-07-09 19:18:39 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
void SILowerControlFlow::emitIfBreak(MachineInstr &MI) {
|
2018-05-25 09:55:04 +02:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
2019-12-27 19:11:06 +01:00
|
|
|
auto Dst = MI.getOperand(0).getReg();
|
2018-05-25 09:55:04 +02:00
|
|
|
|
|
|
|
// Skip ANDing with exec if the break condition is already masked by exec
|
|
|
|
// because it is a V_CMP in the same basic block. (We know the break
|
|
|
|
// condition operand was an i1 in IR, so if it is a VALU instruction it must
|
|
|
|
// be one with a carry-out.)
|
|
|
|
bool SkipAnding = false;
|
|
|
|
if (MI.getOperand(1).isReg()) {
|
|
|
|
if (MachineInstr *Def = MRI->getUniqueVRegDef(MI.getOperand(1).getReg())) {
|
|
|
|
SkipAnding = Def->getParent() == MI.getParent()
|
|
|
|
&& SIInstrInfo::isVALU(*Def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AND the break condition operand with exec, then OR that into the "loop
|
|
|
|
// exit" mask.
|
|
|
|
MachineInstr *And = nullptr, *Or = nullptr;
|
|
|
|
if (!SkipAnding) {
|
2019-11-18 18:06:48 +01:00
|
|
|
Register AndReg = MRI->createVirtualRegister(BoolRC);
|
|
|
|
And = BuildMI(MBB, &MI, DL, TII->get(AndOpc), AndReg)
|
2019-06-16 19:13:09 +02:00
|
|
|
.addReg(Exec)
|
2018-05-25 09:55:04 +02:00
|
|
|
.add(MI.getOperand(1));
|
2019-06-16 19:13:09 +02:00
|
|
|
Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst)
|
2019-11-18 18:06:48 +01:00
|
|
|
.addReg(AndReg)
|
2018-05-25 09:55:04 +02:00
|
|
|
.add(MI.getOperand(2));
|
2019-11-18 18:06:48 +01:00
|
|
|
if (LIS)
|
|
|
|
LIS->createAndComputeVirtRegInterval(AndReg);
|
2018-05-25 09:55:04 +02:00
|
|
|
} else
|
2019-06-16 19:13:09 +02:00
|
|
|
Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst)
|
2018-05-25 09:55:04 +02:00
|
|
|
.add(MI.getOperand(1))
|
|
|
|
.add(MI.getOperand(2));
|
|
|
|
|
|
|
|
if (LIS) {
|
|
|
|
if (And)
|
|
|
|
LIS->InsertMachineInstrInMaps(*And);
|
|
|
|
LIS->ReplaceMachineInstrInMaps(MI, *Or);
|
|
|
|
}
|
|
|
|
|
|
|
|
MI.eraseFromParent();
|
2016-08-22 21:33:16 +02:00
|
|
|
}
|
2012-12-19 23:10:31 +01:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
void SILowerControlFlow::emitLoop(MachineInstr &MI) {
|
2012-12-19 23:10:31 +01:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
2016-08-22 21:33:16 +02:00
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
2012-12-19 23:10:31 +01:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
MachineInstr *AndN2 =
|
2019-06-16 19:13:09 +02:00
|
|
|
BuildMI(MBB, &MI, DL, TII->get(Andn2TermOpc), Exec)
|
|
|
|
.addReg(Exec)
|
2017-01-13 10:58:52 +01:00
|
|
|
.add(MI.getOperand(0));
|
2012-12-19 23:10:31 +01:00
|
|
|
|
2020-07-23 03:24:21 +02:00
|
|
|
auto BranchPt = skipToUncondBrOrEnd(MBB, MI.getIterator());
|
2016-08-22 21:33:16 +02:00
|
|
|
MachineInstr *Branch =
|
2020-07-23 03:24:21 +02:00
|
|
|
BuildMI(MBB, BranchPt, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
|
2017-01-13 10:58:52 +01:00
|
|
|
.add(MI.getOperand(1));
|
2012-12-19 23:10:31 +01:00
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
if (LIS) {
|
|
|
|
LIS->ReplaceMachineInstrInMaps(MI, *AndN2);
|
|
|
|
LIS->InsertMachineInstrInMaps(*Branch);
|
|
|
|
}
|
2012-12-19 23:10:31 +01:00
|
|
|
|
|
|
|
MI.eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2020-03-11 21:17:32 +01:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
SILowerControlFlow::skipIgnoreExecInstsTrivialSucc(
|
|
|
|
MachineBasicBlock &MBB, MachineBasicBlock::iterator It) const {
|
|
|
|
|
|
|
|
SmallSet<const MachineBasicBlock *, 4> Visited;
|
|
|
|
MachineBasicBlock *B = &MBB;
|
|
|
|
do {
|
|
|
|
if (!Visited.insert(B).second)
|
|
|
|
return MBB.end();
|
|
|
|
|
|
|
|
auto E = B->end();
|
|
|
|
for ( ; It != E; ++It) {
|
|
|
|
if (TII->mayReadEXEC(*MRI, *It))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (It != E)
|
|
|
|
return It;
|
|
|
|
|
|
|
|
if (B->succ_size() != 1)
|
|
|
|
return MBB.end();
|
|
|
|
|
|
|
|
// If there is one trivial successor, advance to the next block.
|
|
|
|
MachineBasicBlock *Succ = *B->succ_begin();
|
|
|
|
|
|
|
|
It = Succ->begin();
|
|
|
|
B = Succ;
|
|
|
|
} while (true);
|
|
|
|
}
|
|
|
|
|
2020-09-10 21:49:09 +02:00
|
|
|
MachineBasicBlock *SILowerControlFlow::emitEndCf(MachineInstr &MI) {
|
2013-01-18 22:15:50 +01:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
2016-08-22 21:33:16 +02:00
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
2013-01-18 22:15:50 +01:00
|
|
|
|
2020-09-10 21:49:09 +02:00
|
|
|
MachineBasicBlock::iterator InsPt = MBB.begin();
|
|
|
|
|
|
|
|
// If we have instructions that aren't prolog instructions, split the block
|
|
|
|
// and emit a terminator instruction. This ensures correct spill placement.
|
|
|
|
// FIXME: We should unconditionally split the block here.
|
|
|
|
bool NeedBlockSplit = false;
|
|
|
|
Register DataReg = MI.getOperand(0).getReg();
|
|
|
|
for (MachineBasicBlock::iterator I = InsPt, E = MI.getIterator();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->modifiesRegister(DataReg, TRI)) {
|
|
|
|
NeedBlockSplit = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Opcode = OrOpc;
|
|
|
|
MachineBasicBlock *SplitBB = &MBB;
|
|
|
|
if (NeedBlockSplit) {
|
2020-09-11 17:42:44 +02:00
|
|
|
SplitBB = MBB.splitAt(MI, /*UpdateLiveIns*/true, LIS);
|
2020-09-10 21:49:09 +02:00
|
|
|
Opcode = OrTermrOpc;
|
|
|
|
InsPt = MI;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *NewMI =
|
|
|
|
BuildMI(MBB, InsPt, DL, TII->get(Opcode), Exec)
|
|
|
|
.addReg(Exec)
|
|
|
|
.add(MI.getOperand(0));
|
2016-07-12 23:41:32 +02:00
|
|
|
|
2020-03-11 21:17:32 +01:00
|
|
|
LoweredEndCf.insert(NewMI);
|
|
|
|
|
2021-02-10 04:56:45 +01:00
|
|
|
if (LIS)
|
2019-08-20 19:45:25 +02:00
|
|
|
LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
|
2016-07-12 23:41:32 +02:00
|
|
|
|
2019-08-20 19:45:25 +02:00
|
|
|
MI.eraseFromParent();
|
2016-07-12 23:41:32 +02:00
|
|
|
|
2019-08-20 19:45:25 +02:00
|
|
|
if (LIS)
|
|
|
|
LIS->handleMove(*NewMI);
|
2020-09-10 21:49:09 +02:00
|
|
|
return SplitBB;
|
2016-07-12 23:41:32 +02:00
|
|
|
}
|
|
|
|
|
2016-11-28 19:58:49 +01: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);
|
2020-08-20 18:46:16 +02:00
|
|
|
if (!Op.isReg() || !Op.getReg().isVirtual()) {
|
2016-11-28 19:58:49 +01:00
|
|
|
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)
|
2019-08-20 19:45:25 +02:00
|
|
|
if (I->modifiesRegister(AMDGPU::EXEC, TRI) &&
|
2019-06-16 19:13:09 +02:00
|
|
|
!(I->isCopy() && I->getOperand(0).getReg() != Exec))
|
2016-11-28 19:58:49 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (const auto &SrcOp : Def->explicit_operands())
|
2018-06-12 02:41:26 +02:00
|
|
|
if (SrcOp.isReg() && SrcOp.isUse() &&
|
2020-08-20 18:46:16 +02:00
|
|
|
(SrcOp.getReg().isVirtual() || SrcOp.getReg() == Exec))
|
2016-11-28 19:58:49 +01:00
|
|
|
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;
|
|
|
|
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-15 21:22:08 +02:00
|
|
|
Register Reg = MI.getOperand(OpToReplace).getReg();
|
2016-11-28 19:58:49 +01:00
|
|
|
MI.RemoveOperand(OpToReplace);
|
|
|
|
MI.addOperand(Ops[UniqueOpndIdx]);
|
|
|
|
if (MRI->use_empty(Reg))
|
|
|
|
MRI->getUniqueVRegDef(Reg)->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2020-04-07 02:03:40 +02:00
|
|
|
void SILowerControlFlow::optimizeEndCf() {
|
|
|
|
// If the only instruction immediately following this END_CF is an another
|
|
|
|
// END_CF in the only successor we can avoid emitting exec mask restore here.
|
|
|
|
if (!RemoveRedundantEndcf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (MachineInstr *MI : LoweredEndCf) {
|
|
|
|
MachineBasicBlock &MBB = *MI->getParent();
|
|
|
|
auto Next =
|
|
|
|
skipIgnoreExecInstsTrivialSucc(MBB, std::next(MI->getIterator()));
|
|
|
|
if (Next == MBB.end() || !LoweredEndCf.count(&*Next))
|
|
|
|
continue;
|
|
|
|
// Only skip inner END_CF if outer ENDCF belongs to SI_IF.
|
|
|
|
// If that belongs to SI_ELSE then saved mask has an inverted value.
|
|
|
|
Register SavedExec
|
|
|
|
= TII->getNamedOperand(*Next, AMDGPU::OpName::src1)->getReg();
|
|
|
|
assert(SavedExec.isVirtual() && "Expected saved exec to be src1!");
|
|
|
|
|
|
|
|
const MachineInstr *Def = MRI->getUniqueVRegDef(SavedExec);
|
|
|
|
if (Def && LoweredIf.count(SavedExec)) {
|
|
|
|
LLVM_DEBUG(dbgs() << "Skip redundant "; MI->dump());
|
|
|
|
if (LIS)
|
|
|
|
LIS->RemoveMachineInstrFromMaps(*MI);
|
|
|
|
MI->eraseFromParent();
|
2020-09-07 17:57:27 +02:00
|
|
|
removeMBBifRedundant(MBB);
|
2020-04-07 02:03:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 21:49:09 +02:00
|
|
|
MachineBasicBlock *SILowerControlFlow::process(MachineInstr &MI) {
|
2020-07-03 05:25:33 +02:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
|
|
|
MachineBasicBlock::iterator I(MI);
|
|
|
|
MachineInstr *Prev = (I != MBB.begin()) ? &*(std::prev(I)) : nullptr;
|
|
|
|
|
2020-09-10 21:49:09 +02:00
|
|
|
MachineBasicBlock *SplitBB = &MBB;
|
|
|
|
|
2020-07-03 05:25:33 +02:00
|
|
|
switch (MI.getOpcode()) {
|
|
|
|
case AMDGPU::SI_IF:
|
|
|
|
emitIf(MI);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AMDGPU::SI_ELSE:
|
|
|
|
emitElse(MI);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AMDGPU::SI_IF_BREAK:
|
|
|
|
emitIfBreak(MI);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AMDGPU::SI_LOOP:
|
|
|
|
emitLoop(MI);
|
|
|
|
break;
|
|
|
|
|
2021-07-06 11:23:06 +02:00
|
|
|
case AMDGPU::SI_WATERFALL_LOOP:
|
|
|
|
MI.setDesc(TII->get(AMDGPU::S_CBRANCH_EXECNZ));
|
|
|
|
break;
|
|
|
|
|
2020-07-03 05:25:33 +02:00
|
|
|
case AMDGPU::SI_END_CF:
|
2020-09-10 21:49:09 +02:00
|
|
|
SplitBB = emitEndCf(MI);
|
2020-07-03 05:25:33 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Attempt to process unsupported instruction");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator Next;
|
|
|
|
for (I = Prev ? Prev->getIterator() : MBB.begin(); I != MBB.end(); I = Next) {
|
|
|
|
Next = std::next(I);
|
|
|
|
MachineInstr &MaskMI = *I;
|
|
|
|
switch (MaskMI.getOpcode()) {
|
|
|
|
case AMDGPU::S_AND_B64:
|
|
|
|
case AMDGPU::S_OR_B64:
|
|
|
|
case AMDGPU::S_AND_B32:
|
|
|
|
case AMDGPU::S_OR_B32:
|
|
|
|
// Cleanup bit manipulations on exec mask
|
|
|
|
combineMasks(MaskMI);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
I = MBB.end();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 21:49:09 +02:00
|
|
|
|
|
|
|
return SplitBB;
|
2020-07-03 05:25:33 +02:00
|
|
|
}
|
|
|
|
|
2021-01-25 00:31:08 +01:00
|
|
|
void SILowerControlFlow::lowerInitExec(MachineBasicBlock *MBB,
|
|
|
|
MachineInstr &MI) {
|
|
|
|
MachineFunction &MF = *MBB->getParent();
|
|
|
|
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
|
|
|
|
bool IsWave32 = ST.isWave32();
|
|
|
|
|
|
|
|
if (MI.getOpcode() == AMDGPU::SI_INIT_EXEC) {
|
|
|
|
// This should be before all vector instructions.
|
|
|
|
BuildMI(*MBB, MBB->begin(), MI.getDebugLoc(),
|
|
|
|
TII->get(IsWave32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64), Exec)
|
|
|
|
.addImm(MI.getOperand(0).getImm());
|
|
|
|
if (LIS)
|
|
|
|
LIS->RemoveMachineInstrFromMaps(MI);
|
|
|
|
MI.eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the thread count from an SGPR input and set EXEC accordingly.
|
|
|
|
// Since BFM can't shift by 64, handle that case with CMP + CMOV.
|
|
|
|
//
|
|
|
|
// S_BFE_U32 count, input, {shift, 7}
|
|
|
|
// S_BFM_B64 exec, count, 0
|
|
|
|
// S_CMP_EQ_U32 count, 64
|
|
|
|
// S_CMOV_B64 exec, -1
|
|
|
|
Register InputReg = MI.getOperand(0).getReg();
|
|
|
|
MachineInstr *FirstMI = &*MBB->begin();
|
|
|
|
if (InputReg.isVirtual()) {
|
|
|
|
MachineInstr *DefInstr = MRI->getVRegDef(InputReg);
|
|
|
|
assert(DefInstr && DefInstr->isCopy());
|
|
|
|
if (DefInstr->getParent() == MBB) {
|
|
|
|
if (DefInstr != FirstMI) {
|
|
|
|
// If the `InputReg` is defined in current block, we also need to
|
|
|
|
// move that instruction to the beginning of the block.
|
|
|
|
DefInstr->removeFromParent();
|
|
|
|
MBB->insert(FirstMI, DefInstr);
|
|
|
|
if (LIS)
|
|
|
|
LIS->handleMove(*DefInstr);
|
|
|
|
} else {
|
|
|
|
// If first instruction is definition then move pointer after it.
|
|
|
|
FirstMI = &*std::next(FirstMI->getIterator());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert instruction sequence at block beginning (before vector operations).
|
|
|
|
const DebugLoc DL = MI.getDebugLoc();
|
|
|
|
const unsigned WavefrontSize = ST.getWavefrontSize();
|
|
|
|
const unsigned Mask = (WavefrontSize << 1) - 1;
|
|
|
|
Register CountReg = MRI->createVirtualRegister(&AMDGPU::SGPR_32RegClass);
|
|
|
|
auto BfeMI = BuildMI(*MBB, FirstMI, DL, TII->get(AMDGPU::S_BFE_U32), CountReg)
|
|
|
|
.addReg(InputReg)
|
|
|
|
.addImm((MI.getOperand(1).getImm() & Mask) | 0x70000);
|
|
|
|
auto BfmMI =
|
|
|
|
BuildMI(*MBB, FirstMI, DL,
|
|
|
|
TII->get(IsWave32 ? AMDGPU::S_BFM_B32 : AMDGPU::S_BFM_B64), Exec)
|
|
|
|
.addReg(CountReg)
|
|
|
|
.addImm(0);
|
|
|
|
auto CmpMI = BuildMI(*MBB, FirstMI, DL, TII->get(AMDGPU::S_CMP_EQ_U32))
|
|
|
|
.addReg(CountReg, RegState::Kill)
|
|
|
|
.addImm(WavefrontSize);
|
|
|
|
auto CmovMI =
|
|
|
|
BuildMI(*MBB, FirstMI, DL,
|
|
|
|
TII->get(IsWave32 ? AMDGPU::S_CMOV_B32 : AMDGPU::S_CMOV_B64),
|
|
|
|
Exec)
|
|
|
|
.addImm(-1);
|
|
|
|
|
|
|
|
if (!LIS) {
|
|
|
|
MI.eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LIS->RemoveMachineInstrFromMaps(MI);
|
|
|
|
MI.eraseFromParent();
|
|
|
|
|
|
|
|
LIS->InsertMachineInstrInMaps(*BfeMI);
|
|
|
|
LIS->InsertMachineInstrInMaps(*BfmMI);
|
|
|
|
LIS->InsertMachineInstrInMaps(*CmpMI);
|
|
|
|
LIS->InsertMachineInstrInMaps(*CmovMI);
|
|
|
|
|
|
|
|
LIS->removeInterval(InputReg);
|
|
|
|
LIS->createAndComputeVirtRegInterval(InputReg);
|
|
|
|
LIS->createAndComputeVirtRegInterval(CountReg);
|
|
|
|
}
|
|
|
|
|
2020-09-07 17:57:27 +02:00
|
|
|
bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) {
|
2020-10-28 17:01:02 +01:00
|
|
|
auto GetFallThroughSucc = [=](MachineBasicBlock *B) -> MachineBasicBlock * {
|
|
|
|
auto *S = B->getNextNode();
|
|
|
|
if (!S)
|
|
|
|
return nullptr;
|
|
|
|
if (B->isSuccessor(S)) {
|
|
|
|
// The only fallthrough candidate
|
|
|
|
MachineBasicBlock::iterator I(B->getFirstInstrTerminator());
|
|
|
|
MachineBasicBlock::iterator E = B->end();
|
|
|
|
for (; I != E; I++) {
|
|
|
|
if (I->isBranch() && TII->getBranchDestBlock(*I) == S)
|
|
|
|
// We have unoptimized branch to layout successor
|
|
|
|
return nullptr;
|
2020-10-14 17:34:07 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-28 17:01:02 +01:00
|
|
|
return S;
|
2020-10-14 17:34:07 +02:00
|
|
|
};
|
2020-10-28 17:01:02 +01:00
|
|
|
|
2020-09-07 17:57:27 +02:00
|
|
|
for (auto &I : MBB.instrs()) {
|
|
|
|
if (!I.isDebugInstr() && !I.isUnconditionalBranch())
|
2020-10-28 17:01:02 +01:00
|
|
|
return false;
|
2020-09-07 17:57:27 +02:00
|
|
|
}
|
2020-10-14 17:34:07 +02:00
|
|
|
|
2020-10-28 17:01:02 +01:00
|
|
|
assert(MBB.succ_size() == 1 && "MBB has more than one successor");
|
|
|
|
|
|
|
|
MachineBasicBlock *Succ = *MBB.succ_begin();
|
|
|
|
MachineBasicBlock *FallThrough = nullptr;
|
|
|
|
|
|
|
|
while (!MBB.predecessors().empty()) {
|
|
|
|
MachineBasicBlock *P = *MBB.pred_begin();
|
|
|
|
if (GetFallThroughSucc(P) == &MBB)
|
|
|
|
FallThrough = P;
|
|
|
|
P->ReplaceUsesOfBlockWith(&MBB, Succ);
|
2020-09-07 17:57:27 +02:00
|
|
|
}
|
2020-10-28 17:01:02 +01:00
|
|
|
MBB.removeSuccessor(Succ);
|
|
|
|
if (LIS) {
|
|
|
|
for (auto &I : MBB.instrs())
|
|
|
|
LIS->RemoveMachineInstrFromMaps(I);
|
|
|
|
}
|
|
|
|
MBB.clear();
|
|
|
|
MBB.eraseFromParent();
|
|
|
|
if (FallThrough && !FallThrough->isLayoutSuccessor(Succ)) {
|
|
|
|
if (!GetFallThroughSucc(Succ)) {
|
|
|
|
MachineFunction *MF = FallThrough->getParent();
|
|
|
|
MachineFunction::iterator FallThroughPos(FallThrough);
|
|
|
|
MF->splice(std::next(FallThroughPos), Succ);
|
|
|
|
} else
|
|
|
|
BuildMI(*FallThrough, FallThrough->end(),
|
|
|
|
FallThrough->findBranchDebugLoc(), TII->get(AMDGPU::S_BRANCH))
|
|
|
|
.addMBB(Succ);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2020-09-07 17:57:27 +02:00
|
|
|
}
|
|
|
|
|
2016-02-12 03:16:10 +01:00
|
|
|
bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
|
2018-07-11 22:59:01 +02:00
|
|
|
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
|
2016-06-24 08:30:11 +02:00
|
|
|
TII = ST.getInstrInfo();
|
|
|
|
TRI = &TII->getRegisterInfo();
|
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
// This doesn't actually need LiveIntervals, but we can preserve them.
|
|
|
|
LIS = getAnalysisIfAvailable<LiveIntervals>();
|
2016-09-29 03:44:16 +02:00
|
|
|
MRI = &MF.getRegInfo();
|
2019-06-16 19:13:09 +02:00
|
|
|
BoolRC = TRI->getBoolRC();
|
|
|
|
|
|
|
|
if (ST.isWave32()) {
|
|
|
|
AndOpc = AMDGPU::S_AND_B32;
|
|
|
|
OrOpc = AMDGPU::S_OR_B32;
|
|
|
|
XorOpc = AMDGPU::S_XOR_B32;
|
|
|
|
MovTermOpc = AMDGPU::S_MOV_B32_term;
|
|
|
|
Andn2TermOpc = AMDGPU::S_ANDN2_B32_term;
|
|
|
|
XorTermrOpc = AMDGPU::S_XOR_B32_term;
|
2020-09-10 21:49:09 +02:00
|
|
|
OrTermrOpc = AMDGPU::S_OR_B32_term;
|
2019-06-16 19:13:09 +02:00
|
|
|
OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B32;
|
|
|
|
Exec = AMDGPU::EXEC_LO;
|
|
|
|
} else {
|
|
|
|
AndOpc = AMDGPU::S_AND_B64;
|
|
|
|
OrOpc = AMDGPU::S_OR_B64;
|
|
|
|
XorOpc = AMDGPU::S_XOR_B64;
|
|
|
|
MovTermOpc = AMDGPU::S_MOV_B64_term;
|
|
|
|
Andn2TermOpc = AMDGPU::S_ANDN2_B64_term;
|
|
|
|
XorTermrOpc = AMDGPU::S_XOR_B64_term;
|
2020-09-10 21:49:09 +02:00
|
|
|
OrTermrOpc = AMDGPU::S_OR_B64_term;
|
2019-06-16 19:13:09 +02:00
|
|
|
OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B64;
|
|
|
|
Exec = AMDGPU::EXEC;
|
|
|
|
}
|
2012-12-11 22:25:42 +01:00
|
|
|
|
2021-02-14 01:52:41 +01:00
|
|
|
// Compute set of blocks with kills
|
|
|
|
const bool CanDemote =
|
|
|
|
MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS;
|
|
|
|
for (auto &MBB : MF) {
|
|
|
|
bool IsKillBlock = false;
|
|
|
|
for (auto &Term : MBB.terminators()) {
|
|
|
|
if (TII->isKillTerminator(Term.getOpcode())) {
|
|
|
|
KillBlocks.insert(&MBB);
|
|
|
|
IsKillBlock = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (CanDemote && !IsKillBlock) {
|
|
|
|
for (auto &MI : MBB) {
|
|
|
|
if (MI.getOpcode() == AMDGPU::SI_DEMOTE_I1) {
|
|
|
|
KillBlocks.insert(&MBB);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-22 22:15:28 +02:00
|
|
|
MachineFunction::iterator NextBB;
|
2020-09-10 21:49:09 +02:00
|
|
|
for (MachineFunction::iterator BI = MF.begin();
|
|
|
|
BI != MF.end(); BI = NextBB) {
|
2016-06-22 22:15:28 +02:00
|
|
|
NextBB = std::next(BI);
|
2020-09-10 21:49:09 +02:00
|
|
|
MachineBasicBlock *MBB = &*BI;
|
2016-06-22 22:15:28 +02:00
|
|
|
|
2020-09-10 21:49:09 +02:00
|
|
|
MachineBasicBlock::iterator I, E, Next;
|
|
|
|
E = MBB->end();
|
|
|
|
for (I = MBB->begin(); I != E; I = Next) {
|
2014-03-02 13:27:27 +01:00
|
|
|
Next = std::next(I);
|
2012-12-11 22:25:42 +01:00
|
|
|
MachineInstr &MI = *I;
|
2020-09-10 21:49:09 +02:00
|
|
|
MachineBasicBlock *SplitMBB = MBB;
|
2014-02-10 17:58:30 +01:00
|
|
|
|
2012-12-11 22:25:42 +01:00
|
|
|
switch (MI.getOpcode()) {
|
2016-08-22 21:33:16 +02:00
|
|
|
case AMDGPU::SI_IF:
|
|
|
|
case AMDGPU::SI_ELSE:
|
|
|
|
case AMDGPU::SI_IF_BREAK:
|
2021-07-06 11:23:06 +02:00
|
|
|
case AMDGPU::SI_WATERFALL_LOOP:
|
2016-08-22 21:33:16 +02:00
|
|
|
case AMDGPU::SI_LOOP:
|
2019-08-20 19:45:25 +02:00
|
|
|
case AMDGPU::SI_END_CF:
|
2021-02-10 04:56:45 +01:00
|
|
|
SplitMBB = process(MI);
|
2019-08-01 03:25:27 +02:00
|
|
|
break;
|
2019-08-20 19:45:25 +02:00
|
|
|
|
2021-01-25 00:31:08 +01:00
|
|
|
// FIXME: find a better place for this
|
|
|
|
case AMDGPU::SI_INIT_EXEC:
|
|
|
|
case AMDGPU::SI_INIT_EXEC_FROM_INPUT:
|
|
|
|
lowerInitExec(MBB, MI);
|
|
|
|
if (LIS)
|
|
|
|
LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC);
|
|
|
|
break;
|
|
|
|
|
2016-08-22 21:33:16 +02:00
|
|
|
default:
|
2020-07-03 05:25:33 +02:00
|
|
|
break;
|
2012-12-11 22:25:42 +01:00
|
|
|
}
|
2020-09-10 21:49:09 +02:00
|
|
|
|
|
|
|
if (SplitMBB != MBB) {
|
|
|
|
MBB = Next->getParent();
|
|
|
|
E = MBB->end();
|
|
|
|
}
|
2012-12-11 22:25:42 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-22 21:33:16 +02:00
|
|
|
|
2020-04-07 02:03:40 +02:00
|
|
|
optimizeEndCf();
|
|
|
|
|
2020-03-11 21:17:32 +01:00
|
|
|
LoweredEndCf.clear();
|
2020-03-13 20:44:59 +01:00
|
|
|
LoweredIf.clear();
|
2021-02-14 01:52:41 +01:00
|
|
|
KillBlocks.clear();
|
2020-03-11 21:17:32 +01:00
|
|
|
|
2012-12-19 23:10:31 +01:00
|
|
|
return true;
|
2012-12-11 22:25:42 +01:00
|
|
|
}
|