2012-12-12 05:25:42 +08:00
|
|
|
//===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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-12 05:25:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
2018-05-01 23:54:18 +08:00
|
|
|
/// This pass lowers the pseudo control flow instructions to real
|
2012-12-20 06:10:31 +08:00
|
|
|
/// 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:
|
2017-11-29 01:15:09 +08: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-12 05:25:42 +08:00
|
|
|
///
|
|
|
|
/// becomes:
|
|
|
|
///
|
2017-11-29 01:15:09 +08: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-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.
|
2017-11-29 01:15:09 +08:00
|
|
|
/// %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0 // Do the IF block of the branch
|
2012-12-12 05:25:42 +08:00
|
|
|
///
|
|
|
|
/// label0:
|
2020-04-06 23:44:08 +08: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-12 05:25:42 +08:00
|
|
|
/// S_BRANCH_EXECZ label1 // Use our branch optimization
|
|
|
|
/// // instruction again.
|
2017-11-29 01:15:09 +08:00
|
|
|
/// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr // Do the THEN block
|
2012-12-12 05:25:42 +08:00
|
|
|
/// label1:
|
2017-11-29 01:15:09 +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"
|
AMDGPU: Remove #include "MCTargetDesc/AMDGPUMCTargetDesc.h" from common headers
Summary:
MCTargetDesc/AMDGPUMCTargetDesc.h contains enums for all the instuction
and register defintions, which are huge so we only want to include
them where needed.
This will also make it easier if we want to split the R600 and GCN
definitions into separate tablegenerated files.
I was unable to remove AMDGPUMCTargetDesc.h from SIMachineFunctionInfo.h
because it uses some enums from the header to initialize default values
for the SIMachineFunction class, so I ended up having to remove includes of
SIMachineFunctionInfo.h from headers too.
Reviewers: arsenm, nhaehnle
Reviewed By: nhaehnle
Subscribers: MatzeB, kzhuravl, wdng, yaxunl, dstuttard, tpr, t-tye, javed.absar, llvm-commits
Differential Revision: https://reviews.llvm.org/D46272
llvm-svn: 332930
2018-05-22 10:03:23 +08:00
|
|
|
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
|
2020-04-07 08:03:40 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2020-03-12 04:17:32 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2017-01-21 01:52:16 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-12-13 10:51:04 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervals.h"
|
2017-01-21 01:52:16 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-01-21 01:52:16 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2017-01-21 01:52:16 +08:00
|
|
|
#include "llvm/CodeGen/MachineOperand.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2017-01-21 01:52:16 +08:00
|
|
|
#include "llvm/CodeGen/SlotIndexes.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
2017-01-21 01:52:16 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <iterator>
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-03-14 01:58:19 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
RemoveRedundantEndcf("amdgpu-remove-redundant-endcf",
|
2020-03-14 03:44:59 +08:00
|
|
|
cl::init(true), cl::ReallyHidden);
|
2020-03-14 01:58:19 +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:
|
2017-01-21 01:52:16 +08:00
|
|
|
const SIRegisterInfo *TRI = nullptr;
|
|
|
|
const SIInstrInfo *TII = nullptr;
|
2019-08-01 09:25:27 +08:00
|
|
|
LiveIntervals *LIS = nullptr;
|
2019-08-21 01:45:25 +08:00
|
|
|
MachineRegisterInfo *MRI = nullptr;
|
2020-04-07 08:03:40 +08:00
|
|
|
SetVector<MachineInstr*> LoweredEndCf;
|
2020-03-14 03:44:59 +08:00
|
|
|
DenseSet<Register> LoweredIf;
|
2020-07-03 11:25:33 +08:00
|
|
|
SmallSet<MachineInstr *, 16> NeedsKillCleanup;
|
2012-12-20 06:10:33 +08:00
|
|
|
|
2019-06-17 01:13:09 +08:00
|
|
|
const TargetRegisterClass *BoolRC = nullptr;
|
2020-07-03 11:25:33 +08:00
|
|
|
bool InsertKillCleanups;
|
2019-06-17 01:13:09 +08:00
|
|
|
unsigned AndOpc;
|
|
|
|
unsigned OrOpc;
|
|
|
|
unsigned XorOpc;
|
|
|
|
unsigned MovTermOpc;
|
|
|
|
unsigned Andn2TermOpc;
|
|
|
|
unsigned XorTermrOpc;
|
2020-09-11 03:49:09 +08:00
|
|
|
unsigned OrTermrOpc;
|
2019-06-17 01:13:09 +08:00
|
|
|
unsigned OrSaveExecOpc;
|
|
|
|
unsigned Exec;
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
void emitIf(MachineInstr &MI);
|
|
|
|
void emitElse(MachineInstr &MI);
|
|
|
|
void emitIfBreak(MachineInstr &MI);
|
|
|
|
void emitLoop(MachineInstr &MI);
|
2020-09-11 03:49:09 +08:00
|
|
|
|
|
|
|
MachineBasicBlock *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);
|
|
|
|
|
2020-09-07 23:57:27 +08:00
|
|
|
bool removeMBBifRedundant(MachineBasicBlock &MBB);
|
|
|
|
|
2020-09-11 03:49:09 +08:00
|
|
|
MachineBasicBlock *process(MachineInstr &MI);
|
2020-07-03 11:25:33 +08:00
|
|
|
|
2020-03-12 04:17:32 +08: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 09:24:21 +08: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 08:03:40 +08:00
|
|
|
// Remove redundant SI_END_CF instructions.
|
|
|
|
void optimizeEndCf();
|
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
public:
|
2016-02-12 10:16:10 +08:00
|
|
|
static char ID;
|
|
|
|
|
2017-01-21 01:52:16 +08:00
|
|
|
SILowerControlFlow() : MachineFunctionPass(ID) {}
|
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);
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
};
|
|
|
|
|
2017-01-21 01:52:16 +08:00
|
|
|
} // end anonymous namespace
|
2012-12-12 05:25:42 +08:00
|
|
|
|
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
|
|
|
|
2020-07-03 11:25:33 +08:00
|
|
|
static bool hasKill(const MachineBasicBlock *Begin,
|
|
|
|
const MachineBasicBlock *End, const SIInstrInfo *TII) {
|
2017-08-04 14:58:42 +08:00
|
|
|
DenseSet<const MachineBasicBlock*> Visited;
|
2020-07-03 11:25:33 +08:00
|
|
|
SmallVector<MachineBasicBlock *, 4> Worklist(Begin->succ_begin(),
|
|
|
|
Begin->succ_end());
|
2017-08-04 14:58:42 +08:00
|
|
|
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
MachineBasicBlock *MBB = Worklist.pop_back_val();
|
|
|
|
|
2020-07-03 11:25:33 +08:00
|
|
|
if (MBB == End || !Visited.insert(MBB).second)
|
2017-08-04 14:58:42 +08:00
|
|
|
continue;
|
2020-07-03 11:25:33 +08:00
|
|
|
for (auto &Term : MBB->terminators())
|
2017-10-24 18:27:13 +08:00
|
|
|
if (TII->isKillTerminator(Term.getOpcode()))
|
2020-07-03 11:25:33 +08:00
|
|
|
return true;
|
2017-08-04 14:58:42 +08:00
|
|
|
|
|
|
|
Worklist.append(MBB->succ_begin(), MBB->succ_end());
|
|
|
|
}
|
|
|
|
|
2020-07-03 11:25:33 +08: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 14:58:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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);
|
2019-12-28 02:11:06 +08:00
|
|
|
Register SaveExecReg = MI.getOperand(0).getReg();
|
2019-09-17 17:08:58 +08:00
|
|
|
MachineOperand& Cond = MI.getOperand(1);
|
|
|
|
assert(Cond.getSubReg() == AMDGPU::NoSubRegister);
|
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());
|
|
|
|
|
2017-07-27 05:29:15 +08: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 11:25:33 +08:00
|
|
|
bool SimpleIf = isSimpleIf(MI, MRI);
|
|
|
|
|
|
|
|
if (InsertKillCleanups) {
|
|
|
|
// Check for SI_KILL_*_TERMINATOR on full path of control flow and
|
|
|
|
// flag the associated SI_END_CF for insertion of a kill cleanup.
|
|
|
|
auto UseMI = MRI->use_instr_nodbg_begin(SaveExecReg);
|
|
|
|
while (UseMI->getOpcode() != AMDGPU::SI_END_CF) {
|
|
|
|
assert(std::next(UseMI) == MRI->use_instr_nodbg_end());
|
|
|
|
assert(UseMI->getOpcode() == AMDGPU::SI_ELSE);
|
|
|
|
MachineOperand &NextExec = UseMI->getOperand(0);
|
|
|
|
Register NextExecReg = NextExec.getReg();
|
|
|
|
if (NextExec.isDead()) {
|
|
|
|
assert(!SimpleIf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
UseMI = MRI->use_instr_nodbg_begin(NextExecReg);
|
|
|
|
}
|
|
|
|
if (UseMI->getOpcode() == AMDGPU::SI_END_CF) {
|
|
|
|
if (hasKill(MI.getParent(), UseMI->getParent(), TII)) {
|
|
|
|
NeedsKillCleanup.insert(&*UseMI);
|
|
|
|
SimpleIf = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (SimpleIf) {
|
|
|
|
// 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);
|
|
|
|
SimpleIf = !hasKill(MI.getParent(), UseMI->getParent(), TII);
|
|
|
|
}
|
2017-07-27 05:29:15 +08:00
|
|
|
|
2016-09-29 09:44:16 +08: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 23:50:29 +08:00
|
|
|
Register CopyReg = SimpleIf ? SaveExecReg
|
2019-06-17 01:13:09 +08:00
|
|
|
: MRI->createVirtualRegister(BoolRC);
|
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)
|
2019-06-17 01:13:09 +08:00
|
|
|
.addReg(Exec)
|
|
|
|
.addReg(Exec, RegState::ImplicitDefine);
|
2020-03-14 03:44:59 +08:00
|
|
|
LoweredIf.insert(CopyReg);
|
2016-09-29 09:44:16 +08: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-16 03:22:08 +08:00
|
|
|
Register Tmp = MRI->createVirtualRegister(BoolRC);
|
2016-09-29 09:44:16 +08:00
|
|
|
|
|
|
|
MachineInstr *And =
|
2019-06-17 01:13:09 +08:00
|
|
|
BuildMI(MBB, I, DL, TII->get(AndOpc), Tmp)
|
2016-11-22 09:42:34 +08:00
|
|
|
.addReg(CopyReg)
|
2019-03-06 02:38:00 +08:00
|
|
|
.add(Cond);
|
|
|
|
|
2016-09-29 09:44:16 +08:00
|
|
|
setImpSCCDefDead(*And, true);
|
2013-01-19 05:15:50 +08:00
|
|
|
|
2017-07-27 05:29:15 +08:00
|
|
|
MachineInstr *Xor = nullptr;
|
|
|
|
if (!SimpleIf) {
|
|
|
|
Xor =
|
2019-06-17 01:13:09 +08:00
|
|
|
BuildMI(MBB, I, DL, TII->get(XorOpc), SaveExecReg)
|
2017-07-27 05:29:15 +08:00
|
|
|
.addReg(Tmp)
|
|
|
|
.addReg(CopyReg);
|
|
|
|
setImpSCCDefDead(*Xor, ImpDefSCC.isDead());
|
|
|
|
}
|
2016-09-29 09:44:16 +08:00
|
|
|
|
|
|
|
// Use a copy that is a terminator to get correct spill code placement it with
|
|
|
|
// fast regalloc.
|
|
|
|
MachineInstr *SetExec =
|
2019-06-17 01:13:09 +08:00
|
|
|
BuildMI(MBB, I, DL, TII->get(MovTermOpc), Exec)
|
2016-09-29 09:44:16 +08:00
|
|
|
.addReg(Tmp, RegState::Kill);
|
2016-07-13 03:01:23 +08:00
|
|
|
|
2020-07-23 09:24:21 +08:00
|
|
|
// Skip ahead to the unconditional branch in case there are other terminators
|
|
|
|
// present.
|
|
|
|
I = skipToUncondBrOrEnd(MBB, I);
|
|
|
|
|
2020-01-22 12:07:55 +08: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 17:58:52 +08:00
|
|
|
.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
|
|
|
|
2017-07-27 05:29:15 +08:00
|
|
|
if (!SimpleIf)
|
|
|
|
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
|
|
|
|
2019-08-21 01:45:25 +08:00
|
|
|
LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC);
|
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);
|
2017-07-27 05:29:15 +08:00
|
|
|
if (!SimpleIf)
|
|
|
|
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
|
|
|
|
2019-12-28 02:11:06 +08:00
|
|
|
Register DstReg = MI.getOperand(0).getReg();
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineBasicBlock::iterator Start = MBB.begin();
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
// This must be inserted before phis and any spill code inserted before the
|
|
|
|
// else.
|
2020-10-20 17:11:51 +08:00
|
|
|
Register SaveReg = MRI->createVirtualRegister(BoolRC);
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineInstr *OrSaveExec =
|
2019-06-17 01:13:09 +08:00
|
|
|
BuildMI(MBB, Start, DL, TII->get(OrSaveExecOpc), SaveReg)
|
2020-09-02 23:36:22 +08:00
|
|
|
.add(MI.getOperand(1)); // Saved EXEC
|
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
|
|
|
|
2020-10-20 17:11:51 +08: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-23 04:15:28 +08:00
|
|
|
|
2020-10-20 17:11:51 +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 =
|
2019-06-17 01:13:09 +08:00
|
|
|
BuildMI(MBB, ElsePt, DL, TII->get(XorTermrOpc), Exec)
|
|
|
|
.addReg(Exec)
|
2016-08-23 03:33:16 +08:00
|
|
|
.addReg(DstReg);
|
2012-12-20 06:10:33 +08:00
|
|
|
|
2020-07-23 09:24:21 +08:00
|
|
|
// Skip ahead to the unconditional branch in case there are other terminators
|
|
|
|
// present.
|
|
|
|
ElsePt = skipToUncondBrOrEnd(MBB, ElsePt);
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineInstr *Branch =
|
2020-01-22 12:07:55 +08:00
|
|
|
BuildMI(MBB, ElsePt, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
|
|
|
|
.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();
|
|
|
|
|
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
|
|
|
LIS->removeInterval(DstReg);
|
|
|
|
LIS->createAndComputeVirtRegInterval(DstReg);
|
2020-10-20 17:11:51 +08:00
|
|
|
LIS->createAndComputeVirtRegInterval(SaveReg);
|
2016-07-10 01:18:39 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
// Let this be recomputed.
|
2019-08-21 01:45:25 +08:00
|
|
|
LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC);
|
2016-07-10 01:18:39 +08:00
|
|
|
}
|
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
void SILowerControlFlow::emitIfBreak(MachineInstr &MI) {
|
2018-05-25 15:55:04 +08:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
|
|
|
const DebugLoc &DL = MI.getDebugLoc();
|
2019-12-28 02:11:06 +08:00
|
|
|
auto Dst = MI.getOperand(0).getReg();
|
2018-05-25 15:55:04 +08: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-19 01:06:48 +08:00
|
|
|
Register AndReg = MRI->createVirtualRegister(BoolRC);
|
|
|
|
And = BuildMI(MBB, &MI, DL, TII->get(AndOpc), AndReg)
|
2019-06-17 01:13:09 +08:00
|
|
|
.addReg(Exec)
|
2018-05-25 15:55:04 +08:00
|
|
|
.add(MI.getOperand(1));
|
2019-06-17 01:13:09 +08:00
|
|
|
Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst)
|
2019-11-19 01:06:48 +08:00
|
|
|
.addReg(AndReg)
|
2018-05-25 15:55:04 +08:00
|
|
|
.add(MI.getOperand(2));
|
2019-11-19 01:06:48 +08:00
|
|
|
if (LIS)
|
|
|
|
LIS->createAndComputeVirtRegInterval(AndReg);
|
2018-05-25 15:55:04 +08:00
|
|
|
} else
|
2019-06-17 01:13:09 +08:00
|
|
|
Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst)
|
2018-05-25 15:55:04 +08:00
|
|
|
.add(MI.getOperand(1))
|
|
|
|
.add(MI.getOperand(2));
|
|
|
|
|
|
|
|
if (LIS) {
|
|
|
|
if (And)
|
|
|
|
LIS->InsertMachineInstrInMaps(*And);
|
|
|
|
LIS->ReplaceMachineInstrInMaps(MI, *Or);
|
|
|
|
}
|
|
|
|
|
|
|
|
MI.eraseFromParent();
|
2016-08-23 03:33:16 +08:00
|
|
|
}
|
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 =
|
2019-06-17 01:13:09 +08:00
|
|
|
BuildMI(MBB, &MI, DL, TII->get(Andn2TermOpc), Exec)
|
|
|
|
.addReg(Exec)
|
2017-01-13 17:58:52 +08:00
|
|
|
.add(MI.getOperand(0));
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2020-07-23 09:24:21 +08:00
|
|
|
auto BranchPt = skipToUncondBrOrEnd(MBB, MI.getIterator());
|
2016-08-23 03:33:16 +08:00
|
|
|
MachineInstr *Branch =
|
2020-07-23 09:24:21 +08:00
|
|
|
BuildMI(MBB, BranchPt, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
|
2017-01-13 17:58:52 +08:00
|
|
|
.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();
|
|
|
|
}
|
|
|
|
|
2020-03-12 04:17:32 +08: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) {
|
2020-07-03 11:25:33 +08:00
|
|
|
if (It->getOpcode() == AMDGPU::SI_KILL_CLEANUP)
|
|
|
|
continue;
|
2020-03-12 04:17:32 +08:00
|
|
|
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-11 03:49:09 +08:00
|
|
|
MachineBasicBlock *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
|
|
|
|
2020-09-11 03:49:09 +08: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 23:42:44 +08:00
|
|
|
SplitBB = MBB.splitAt(MI, /*UpdateLiveIns*/true, LIS);
|
2020-09-11 03:49:09 +08:00
|
|
|
Opcode = OrTermrOpc;
|
|
|
|
InsPt = MI;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *NewMI =
|
|
|
|
BuildMI(MBB, InsPt, DL, TII->get(Opcode), Exec)
|
|
|
|
.addReg(Exec)
|
|
|
|
.add(MI.getOperand(0));
|
2016-07-13 05:41:32 +08:00
|
|
|
|
2020-03-12 04:17:32 +08:00
|
|
|
LoweredEndCf.insert(NewMI);
|
|
|
|
|
2020-07-03 11:25:33 +08:00
|
|
|
// If this ends control flow which contains kills (as flagged in emitIf)
|
|
|
|
// then insert an SI_KILL_CLEANUP immediately following the exec mask
|
|
|
|
// manipulation. This can be lowered to early termination if appropriate.
|
|
|
|
MachineInstr *CleanUpMI = nullptr;
|
|
|
|
if (NeedsKillCleanup.count(&MI))
|
|
|
|
CleanUpMI = BuildMI(MBB, InsPt, DL, TII->get(AMDGPU::SI_KILL_CLEANUP));
|
|
|
|
|
|
|
|
if (LIS) {
|
2019-08-21 01:45:25 +08:00
|
|
|
LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
|
2020-07-03 11:25:33 +08:00
|
|
|
if (CleanUpMI)
|
|
|
|
LIS->InsertMachineInstrInMaps(*CleanUpMI);
|
|
|
|
}
|
2016-07-13 05:41:32 +08:00
|
|
|
|
2019-08-21 01:45:25 +08:00
|
|
|
MI.eraseFromParent();
|
2016-07-13 05:41:32 +08:00
|
|
|
|
2019-08-21 01:45:25 +08:00
|
|
|
if (LIS)
|
|
|
|
LIS->handleMove(*NewMI);
|
2020-09-11 03:49:09 +08:00
|
|
|
return SplitBB;
|
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);
|
2020-08-21 00:46:16 +08:00
|
|
|
if (!Op.isReg() || !Op.getReg().isVirtual()) {
|
2016-11-29 02:58:49 +08: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-21 01:45:25 +08:00
|
|
|
if (I->modifiesRegister(AMDGPU::EXEC, TRI) &&
|
2019-06-17 01:13:09 +08:00
|
|
|
!(I->isCopy() && I->getOperand(0).getReg() != Exec))
|
2016-11-29 02:58:49 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (const auto &SrcOp : Def->explicit_operands())
|
2018-06-12 08:41:26 +08:00
|
|
|
if (SrcOp.isReg() && SrcOp.isUse() &&
|
2020-08-21 00:46:16 +08:00
|
|
|
(SrcOp.getReg().isVirtual() || SrcOp.getReg() == Exec))
|
2016-11-29 02:58:49 +08: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-16 03:22:08 +08:00
|
|
|
Register Reg = MI.getOperand(OpToReplace).getReg();
|
2016-11-29 02:58:49 +08:00
|
|
|
MI.RemoveOperand(OpToReplace);
|
|
|
|
MI.addOperand(Ops[UniqueOpndIdx]);
|
|
|
|
if (MRI->use_empty(Reg))
|
|
|
|
MRI->getUniqueVRegDef(Reg)->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2020-04-07 08:03:40 +08: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 23:57:27 +08:00
|
|
|
removeMBBifRedundant(MBB);
|
2020-04-07 08:03:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 03:49:09 +08:00
|
|
|
MachineBasicBlock *SILowerControlFlow::process(MachineInstr &MI) {
|
2020-07-03 11:25:33 +08:00
|
|
|
MachineBasicBlock &MBB = *MI.getParent();
|
|
|
|
MachineBasicBlock::iterator I(MI);
|
|
|
|
MachineInstr *Prev = (I != MBB.begin()) ? &*(std::prev(I)) : nullptr;
|
|
|
|
|
2020-09-11 03:49:09 +08:00
|
|
|
MachineBasicBlock *SplitBB = &MBB;
|
|
|
|
|
2020-07-03 11:25:33 +08: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;
|
|
|
|
|
|
|
|
case AMDGPU::SI_END_CF:
|
2020-09-11 03:49:09 +08:00
|
|
|
SplitBB = emitEndCf(MI);
|
2020-07-03 11:25:33 +08: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-11 03:49:09 +08:00
|
|
|
|
|
|
|
return SplitBB;
|
2020-07-03 11:25:33 +08:00
|
|
|
}
|
|
|
|
|
2020-09-07 23:57:27 +08:00
|
|
|
bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) {
|
2020-10-29 00:01:02 +08: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 23:34:07 +08:00
|
|
|
}
|
|
|
|
}
|
2020-10-29 00:01:02 +08:00
|
|
|
return S;
|
2020-10-14 23:34:07 +08:00
|
|
|
};
|
2020-10-29 00:01:02 +08:00
|
|
|
|
2020-09-07 23:57:27 +08:00
|
|
|
for (auto &I : MBB.instrs()) {
|
|
|
|
if (!I.isDebugInstr() && !I.isUnconditionalBranch())
|
2020-10-29 00:01:02 +08:00
|
|
|
return false;
|
2020-09-07 23:57:27 +08:00
|
|
|
}
|
2020-10-14 23:34:07 +08:00
|
|
|
|
2020-10-29 00:01:02 +08: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 23:57:27 +08:00
|
|
|
}
|
2020-10-29 00:01:02 +08: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 23:57:27 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 10:16:10 +08:00
|
|
|
bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
|
2018-07-12 04:59:01 +08:00
|
|
|
const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
|
2016-06-24 14:30:11 +08:00
|
|
|
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();
|
2019-06-17 01:13:09 +08:00
|
|
|
BoolRC = TRI->getBoolRC();
|
2020-07-03 11:25:33 +08:00
|
|
|
InsertKillCleanups =
|
|
|
|
MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS;
|
2019-06-17 01:13:09 +08:00
|
|
|
|
|
|
|
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-11 03:49:09 +08:00
|
|
|
OrTermrOpc = AMDGPU::S_OR_B32_term;
|
2019-06-17 01:13:09 +08: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-11 03:49:09 +08:00
|
|
|
OrTermrOpc = AMDGPU::S_OR_B64_term;
|
2019-06-17 01:13:09 +08:00
|
|
|
OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B64;
|
|
|
|
Exec = AMDGPU::EXEC;
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
|
2020-07-03 11:25:33 +08:00
|
|
|
SmallVector<MachineInstr *, 32> Worklist;
|
|
|
|
|
2016-06-23 04:15:28 +08:00
|
|
|
MachineFunction::iterator NextBB;
|
2020-09-11 03:49:09 +08:00
|
|
|
for (MachineFunction::iterator BI = MF.begin();
|
|
|
|
BI != MF.end(); BI = NextBB) {
|
2016-06-23 04:15:28 +08:00
|
|
|
NextBB = std::next(BI);
|
2020-09-11 03:49:09 +08:00
|
|
|
MachineBasicBlock *MBB = &*BI;
|
2016-06-23 04:15:28 +08:00
|
|
|
|
2020-09-11 03:49:09 +08:00
|
|
|
MachineBasicBlock::iterator I, E, Next;
|
|
|
|
E = MBB->end();
|
|
|
|
for (I = MBB->begin(); I != E; 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;
|
2020-09-11 03:49:09 +08:00
|
|
|
MachineBasicBlock *SplitMBB = MBB;
|
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:
|
2020-09-11 03:49:09 +08:00
|
|
|
SplitMBB = process(MI);
|
2016-08-23 03:33:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AMDGPU::SI_ELSE:
|
|
|
|
case AMDGPU::SI_IF_BREAK:
|
|
|
|
case AMDGPU::SI_LOOP:
|
2019-08-21 01:45:25 +08:00
|
|
|
case AMDGPU::SI_END_CF:
|
2020-07-03 11:25:33 +08:00
|
|
|
// Only build worklist if SI_IF instructions must be processed first.
|
|
|
|
if (InsertKillCleanups)
|
|
|
|
Worklist.push_back(&MI);
|
|
|
|
else
|
2020-09-11 03:49:09 +08:00
|
|
|
SplitMBB = process(MI);
|
2019-08-01 09:25:27 +08:00
|
|
|
break;
|
2019-08-21 01:45:25 +08:00
|
|
|
|
2016-08-23 03:33:16 +08:00
|
|
|
default:
|
2020-07-03 11:25:33 +08:00
|
|
|
break;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2020-09-11 03:49:09 +08:00
|
|
|
|
|
|
|
if (SplitMBB != MBB) {
|
|
|
|
MBB = Next->getParent();
|
|
|
|
E = MBB->end();
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-23 03:33:16 +08:00
|
|
|
|
2020-07-03 11:25:33 +08:00
|
|
|
for (MachineInstr *MI : Worklist)
|
|
|
|
process(*MI);
|
|
|
|
|
2020-04-07 08:03:40 +08:00
|
|
|
optimizeEndCf();
|
|
|
|
|
2020-03-12 04:17:32 +08:00
|
|
|
LoweredEndCf.clear();
|
2020-03-14 03:44:59 +08:00
|
|
|
LoweredIf.clear();
|
2020-07-03 11:25:33 +08:00
|
|
|
NeedsKillCleanup.clear();
|
2020-03-12 04:17:32 +08:00
|
|
|
|
2012-12-20 06:10:31 +08:00
|
|
|
return true;
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|