2014-11-22 06:06:37 +08:00
|
|
|
//===-- SIFoldOperands.cpp - Fold operands --- ----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "AMDGPU.h"
|
|
|
|
#include "AMDGPUSubtarget.h"
|
|
|
|
#include "SIInstrInfo.h"
|
2017-02-28 03:35:42 +08:00
|
|
|
#include "SIMachineFunctionInfo.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"
|
2017-06-21 02:56:32 +08:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2017-12-13 10:51:04 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervals.h"
|
2014-11-22 06:06:37 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-11-22 06:06:37 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "si-fold-operands"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-01-08 01:42:16 +08:00
|
|
|
struct FoldCandidate {
|
|
|
|
MachineInstr *UseMI;
|
2016-09-14 23:51:33 +08:00
|
|
|
union {
|
|
|
|
MachineOperand *OpToFold;
|
|
|
|
uint64_t ImmToFold;
|
|
|
|
int FrameIndexToFold;
|
|
|
|
};
|
|
|
|
unsigned char UseOpNo;
|
|
|
|
MachineOperand::MachineOperandType Kind;
|
2017-06-03 08:41:52 +08:00
|
|
|
bool Commuted;
|
2015-01-08 01:42:16 +08:00
|
|
|
|
2017-06-03 08:41:52 +08:00
|
|
|
FoldCandidate(MachineInstr *MI, unsigned OpNo, MachineOperand *FoldOp,
|
|
|
|
bool Commuted_ = false) :
|
|
|
|
UseMI(MI), OpToFold(nullptr), UseOpNo(OpNo), Kind(FoldOp->getType()),
|
|
|
|
Commuted(Commuted_) {
|
2015-01-08 06:44:19 +08:00
|
|
|
if (FoldOp->isImm()) {
|
|
|
|
ImmToFold = FoldOp->getImm();
|
2016-09-14 23:51:33 +08:00
|
|
|
} else if (FoldOp->isFI()) {
|
|
|
|
FrameIndexToFold = FoldOp->getIndex();
|
2015-01-08 06:44:19 +08:00
|
|
|
} else {
|
|
|
|
assert(FoldOp->isReg());
|
|
|
|
OpToFold = FoldOp;
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 01:42:16 +08:00
|
|
|
|
2016-09-14 23:51:33 +08:00
|
|
|
bool isFI() const {
|
|
|
|
return Kind == MachineOperand::MO_FrameIndex;
|
|
|
|
}
|
|
|
|
|
2015-01-08 01:42:16 +08:00
|
|
|
bool isImm() const {
|
2016-09-14 23:51:33 +08:00
|
|
|
return Kind == MachineOperand::MO_Immediate;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isReg() const {
|
|
|
|
return Kind == MachineOperand::MO_Register;
|
2015-01-08 01:42:16 +08:00
|
|
|
}
|
2017-06-03 08:41:52 +08:00
|
|
|
|
|
|
|
bool isCommuted() const {
|
|
|
|
return Commuted;
|
|
|
|
}
|
2015-01-08 01:42:16 +08:00
|
|
|
};
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
class SIFoldOperands : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
MachineRegisterInfo *MRI;
|
|
|
|
const SIInstrInfo *TII;
|
|
|
|
const SIRegisterInfo *TRI;
|
AMDGPU: Separate R600 and GCN TableGen files
Summary:
We now have two sets of generated TableGen files, one for R600 and one
for GCN, so each sub-target now has its own tables of instructions,
registers, ISel patterns, etc. This should help reduce compile time
since each sub-target now only has to consider information that
is specific to itself. This will also help prevent the R600
sub-target from slowing down new features for GCN, like disassembler
support, GlobalISel, etc.
Reviewers: arsenm, nhaehnle, jvesely
Reviewed By: arsenm
Subscribers: MatzeB, kzhuravl, wdng, mgorny, yaxunl, dstuttard, tpr, t-tye, javed.absar, llvm-commits
Differential Revision: https://reviews.llvm.org/D46365
llvm-svn: 335942
2018-06-29 07:47:12 +08:00
|
|
|
const AMDGPUSubtarget *ST;
|
2017-01-11 07:32:04 +08:00
|
|
|
|
|
|
|
void foldOperand(MachineOperand &OpToFold,
|
|
|
|
MachineInstr *UseMI,
|
|
|
|
unsigned UseOpIdx,
|
|
|
|
SmallVectorImpl<FoldCandidate> &FoldList,
|
|
|
|
SmallVectorImpl<MachineInstr *> &CopiesToReplace) const;
|
|
|
|
|
|
|
|
void foldInstOperand(MachineInstr &MI, MachineOperand &OpToFold) const;
|
|
|
|
|
2017-02-23 07:27:53 +08:00
|
|
|
const MachineOperand *isClamp(const MachineInstr &MI) const;
|
|
|
|
bool tryFoldClamp(MachineInstr &MI);
|
|
|
|
|
2017-02-28 03:35:42 +08:00
|
|
|
std::pair<const MachineOperand *, int> isOMod(const MachineInstr &MI) const;
|
|
|
|
bool tryFoldOMod(MachineInstr &MI);
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
public:
|
|
|
|
SIFoldOperands() : MachineFunctionPass(ID) {
|
|
|
|
initializeSIFoldOperandsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
StringRef getPassName() const override { return "SI Fold Operands"; }
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-22 06:06:37 +08:00
|
|
|
} // End anonymous namespace.
|
|
|
|
|
2016-02-11 14:15:34 +08:00
|
|
|
INITIALIZE_PASS(SIFoldOperands, DEBUG_TYPE,
|
|
|
|
"SI Fold Operands", false, false)
|
2014-11-22 06:06:37 +08:00
|
|
|
|
|
|
|
char SIFoldOperands::ID = 0;
|
|
|
|
|
|
|
|
char &llvm::SIFoldOperandsID = SIFoldOperands::ID;
|
|
|
|
|
2017-01-12 06:00:02 +08:00
|
|
|
// Wrapper around isInlineConstant that understands special cases when
|
|
|
|
// instruction types are replaced during operand folding.
|
|
|
|
static bool isInlineConstantIfFolded(const SIInstrInfo *TII,
|
|
|
|
const MachineInstr &UseMI,
|
|
|
|
unsigned OpNo,
|
|
|
|
const MachineOperand &OpToFold) {
|
|
|
|
if (TII->isInlineConstant(UseMI, OpNo, OpToFold))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
unsigned Opc = UseMI.getOpcode();
|
|
|
|
switch (Opc) {
|
|
|
|
case AMDGPU::V_MAC_F32_e64:
|
2018-05-01 03:08:16 +08:00
|
|
|
case AMDGPU::V_MAC_F16_e64:
|
|
|
|
case AMDGPU::V_FMAC_F32_e64: {
|
2017-01-12 06:00:02 +08:00
|
|
|
// Special case for mac. Since this is replaced with mad when folded into
|
|
|
|
// src2, we need to check the legality for the final instruction.
|
|
|
|
int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
|
|
|
|
if (static_cast<int>(OpNo) == Src2Idx) {
|
2018-05-01 03:08:16 +08:00
|
|
|
bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e64;
|
2017-01-12 06:00:02 +08:00
|
|
|
bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
|
2018-05-01 03:08:16 +08:00
|
|
|
|
|
|
|
unsigned Opc = IsFMA ?
|
|
|
|
AMDGPU::V_FMA_F32 : (IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16);
|
|
|
|
const MCInstrDesc &MadDesc = TII->get(Opc);
|
2017-01-12 06:00:02 +08:00
|
|
|
return TII->isInlineConstant(OpToFold, MadDesc.OpInfo[OpNo].OperandType);
|
|
|
|
}
|
2017-07-07 18:18:57 +08:00
|
|
|
return false;
|
2017-01-12 06:00:02 +08:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-22 06:06:37 +08:00
|
|
|
FunctionPass *llvm::createSIFoldOperandsPass() {
|
|
|
|
return new SIFoldOperands();
|
|
|
|
}
|
|
|
|
|
2015-01-08 01:42:16 +08:00
|
|
|
static bool updateOperand(FoldCandidate &Fold,
|
2014-11-22 06:06:37 +08:00
|
|
|
const TargetRegisterInfo &TRI) {
|
2015-01-08 01:42:16 +08:00
|
|
|
MachineInstr *MI = Fold.UseMI;
|
|
|
|
MachineOperand &Old = MI->getOperand(Fold.UseOpNo);
|
2014-11-22 06:06:37 +08:00
|
|
|
assert(Old.isReg());
|
|
|
|
|
2015-01-08 01:42:16 +08:00
|
|
|
if (Fold.isImm()) {
|
2018-04-18 07:09:05 +08:00
|
|
|
if (MI->getDesc().TSFlags & SIInstrFlags::IsPacked) {
|
2018-04-20 05:16:50 +08:00
|
|
|
// Set op_sel/op_sel_hi on this operand or bail out if op_sel is
|
|
|
|
// already set.
|
2018-04-18 07:09:05 +08:00
|
|
|
unsigned Opcode = MI->getOpcode();
|
|
|
|
int OpNo = MI->getOperandNo(&Old);
|
|
|
|
int ModIdx = -1;
|
|
|
|
if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0))
|
|
|
|
ModIdx = AMDGPU::OpName::src0_modifiers;
|
|
|
|
else if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1))
|
|
|
|
ModIdx = AMDGPU::OpName::src1_modifiers;
|
|
|
|
else if (OpNo == AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2))
|
|
|
|
ModIdx = AMDGPU::OpName::src2_modifiers;
|
|
|
|
assert(ModIdx != -1);
|
|
|
|
ModIdx = AMDGPU::getNamedOperandIdx(Opcode, ModIdx);
|
|
|
|
MachineOperand &Mod = MI->getOperand(ModIdx);
|
|
|
|
unsigned Val = Mod.getImm();
|
|
|
|
if ((Val & SISrcMods::OP_SEL_0) || !(Val & SISrcMods::OP_SEL_1))
|
|
|
|
return false;
|
2018-04-20 05:16:50 +08:00
|
|
|
// If upper part is all zero we do not need op_sel_hi.
|
|
|
|
if (!isUInt<16>(Fold.ImmToFold)) {
|
|
|
|
if (!(Fold.ImmToFold & 0xffff)) {
|
|
|
|
Mod.setImm(Mod.getImm() | SISrcMods::OP_SEL_0);
|
|
|
|
Mod.setImm(Mod.getImm() & ~SISrcMods::OP_SEL_1);
|
2018-04-25 02:17:55 +08:00
|
|
|
Old.ChangeToImmediate((Fold.ImmToFold >> 16) & 0xffff);
|
2018-04-20 05:16:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Mod.setImm(Mod.getImm() & ~SISrcMods::OP_SEL_1);
|
|
|
|
}
|
2018-04-18 07:09:05 +08:00
|
|
|
}
|
2015-01-08 01:42:16 +08:00
|
|
|
Old.ChangeToImmediate(Fold.ImmToFold);
|
2014-11-22 06:06:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-14 23:51:33 +08:00
|
|
|
if (Fold.isFI()) {
|
|
|
|
Old.ChangeToFrameIndex(Fold.FrameIndexToFold);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-08 01:42:16 +08:00
|
|
|
MachineOperand *New = Fold.OpToFold;
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Old.getReg()) &&
|
|
|
|
TargetRegisterInfo::isVirtualRegister(New->getReg())) {
|
|
|
|
Old.substVirtReg(New->getReg(), New->getSubReg(), TRI);
|
2017-06-21 02:41:31 +08:00
|
|
|
|
|
|
|
Old.setIsUndef(New->isUndef());
|
2014-11-22 06:06:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Handle physical registers.
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
static bool isUseMIInFoldList(ArrayRef<FoldCandidate> FoldList,
|
2015-07-13 23:47:57 +08:00
|
|
|
const MachineInstr *MI) {
|
|
|
|
for (auto Candidate : FoldList) {
|
|
|
|
if (Candidate.UseMI == MI)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
static bool tryAddToFoldList(SmallVectorImpl<FoldCandidate> &FoldList,
|
2015-01-08 06:44:19 +08:00
|
|
|
MachineInstr *MI, unsigned OpNo,
|
|
|
|
MachineOperand *OpToFold,
|
|
|
|
const SIInstrInfo *TII) {
|
2016-06-30 08:01:54 +08:00
|
|
|
if (!TII->isOperandLegal(*MI, OpNo, OpToFold)) {
|
2015-07-13 23:47:57 +08:00
|
|
|
|
2016-11-13 15:01:11 +08:00
|
|
|
// Special case for v_mac_{f16, f32}_e64 if we are trying to fold into src2
|
2015-07-13 23:47:57 +08:00
|
|
|
unsigned Opc = MI->getOpcode();
|
2018-05-01 03:08:16 +08:00
|
|
|
if ((Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64 ||
|
|
|
|
Opc == AMDGPU::V_FMAC_F32_e64) &&
|
2015-07-13 23:47:57 +08:00
|
|
|
(int)OpNo == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)) {
|
2018-05-01 03:08:16 +08:00
|
|
|
bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e64;
|
2017-01-12 06:00:02 +08:00
|
|
|
bool IsF32 = Opc == AMDGPU::V_MAC_F32_e64;
|
2018-05-01 03:08:16 +08:00
|
|
|
unsigned NewOpc = IsFMA ?
|
|
|
|
AMDGPU::V_FMA_F32 : (IsF32 ? AMDGPU::V_MAD_F32 : AMDGPU::V_MAD_F16);
|
2016-11-13 15:01:11 +08:00
|
|
|
|
|
|
|
// Check if changing this to a v_mad_{f16, f32} instruction will allow us
|
|
|
|
// to fold the operand.
|
2018-05-01 03:08:16 +08:00
|
|
|
MI->setDesc(TII->get(NewOpc));
|
2015-07-13 23:47:57 +08:00
|
|
|
bool FoldAsMAD = tryAddToFoldList(FoldList, MI, OpNo, OpToFold, TII);
|
|
|
|
if (FoldAsMAD) {
|
|
|
|
MI->untieRegOperand(OpNo);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
MI->setDesc(TII->get(Opc));
|
|
|
|
}
|
|
|
|
|
2016-12-07 10:42:15 +08:00
|
|
|
// Special case for s_setreg_b32
|
|
|
|
if (Opc == AMDGPU::S_SETREG_B32 && OpToFold->isImm()) {
|
|
|
|
MI->setDesc(TII->get(AMDGPU::S_SETREG_IMM32_B32));
|
|
|
|
FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 23:47:57 +08:00
|
|
|
// If we are already folding into another operand of MI, then
|
|
|
|
// we can't commute the instruction, otherwise we risk making the
|
|
|
|
// other fold illegal.
|
|
|
|
if (isUseMIInFoldList(FoldList, MI))
|
|
|
|
return false;
|
|
|
|
|
2015-01-08 06:44:19 +08:00
|
|
|
// Operand is not legal, so try to commute the instruction to
|
|
|
|
// see if this makes it possible to fold.
|
2015-09-29 04:33:22 +08:00
|
|
|
unsigned CommuteIdx0 = TargetInstrInfo::CommuteAnyOperandIndex;
|
|
|
|
unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex;
|
2016-06-30 08:01:54 +08:00
|
|
|
bool CanCommute = TII->findCommutedOpIndices(*MI, CommuteIdx0, CommuteIdx1);
|
2015-01-08 06:44:19 +08:00
|
|
|
|
|
|
|
if (CanCommute) {
|
|
|
|
if (CommuteIdx0 == OpNo)
|
|
|
|
OpNo = CommuteIdx1;
|
|
|
|
else if (CommuteIdx1 == OpNo)
|
|
|
|
OpNo = CommuteIdx0;
|
|
|
|
}
|
|
|
|
|
2015-09-29 04:33:22 +08:00
|
|
|
// One of operands might be an Imm operand, and OpNo may refer to it after
|
|
|
|
// the call of commuteInstruction() below. Such situations are avoided
|
|
|
|
// here explicitly as OpNo must be a register operand to be a candidate
|
|
|
|
// for memory folding.
|
|
|
|
if (CanCommute && (!MI->getOperand(CommuteIdx0).isReg() ||
|
|
|
|
!MI->getOperand(CommuteIdx1).isReg()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!CanCommute ||
|
2016-06-30 08:01:54 +08:00
|
|
|
!TII->commuteInstruction(*MI, false, CommuteIdx0, CommuteIdx1))
|
2015-01-08 06:44:19 +08:00
|
|
|
return false;
|
|
|
|
|
2017-06-03 08:41:52 +08:00
|
|
|
if (!TII->isOperandLegal(*MI, OpNo, OpToFold)) {
|
|
|
|
TII->commuteInstruction(*MI, false, CommuteIdx0, CommuteIdx1);
|
2015-01-08 06:44:19 +08:00
|
|
|
return false;
|
2017-06-03 08:41:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold, true));
|
|
|
|
return true;
|
2015-01-08 06:44:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-07 02:12:13 +08:00
|
|
|
// If the use operand doesn't care about the value, this may be an operand only
|
|
|
|
// used for register indexing, in which case it is unsafe to fold.
|
2017-05-31 00:49:24 +08:00
|
|
|
static bool isUseSafeToFold(const SIInstrInfo *TII,
|
|
|
|
const MachineInstr &MI,
|
2016-10-07 02:12:13 +08:00
|
|
|
const MachineOperand &UseMO) {
|
2017-05-31 00:49:24 +08:00
|
|
|
return !UseMO.isUndef() && !TII->isSDWA(MI);
|
2016-10-07 02:12:13 +08:00
|
|
|
//return !MI.hasRegisterImplicitUseOperand(UseMO.getReg());
|
|
|
|
}
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
void SIFoldOperands::foldOperand(
|
|
|
|
MachineOperand &OpToFold,
|
|
|
|
MachineInstr *UseMI,
|
|
|
|
unsigned UseOpIdx,
|
|
|
|
SmallVectorImpl<FoldCandidate> &FoldList,
|
|
|
|
SmallVectorImpl<MachineInstr *> &CopiesToReplace) const {
|
2015-08-29 07:45:19 +08:00
|
|
|
const MachineOperand &UseOp = UseMI->getOperand(UseOpIdx);
|
|
|
|
|
2017-05-31 00:49:24 +08:00
|
|
|
if (!isUseSafeToFold(TII, *UseMI, UseOp))
|
2016-10-07 02:12:13 +08:00
|
|
|
return;
|
|
|
|
|
2015-08-29 07:45:19 +08:00
|
|
|
// FIXME: Fold operands with subregs.
|
2016-08-16 00:18:36 +08:00
|
|
|
if (UseOp.isReg() && OpToFold.isReg()) {
|
|
|
|
if (UseOp.isImplicit() || UseOp.getSubReg() != AMDGPU::NoSubRegister)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Don't fold subregister extracts into tied operands, only if it is a full
|
|
|
|
// copy since a subregister use tied to a full register def doesn't really
|
|
|
|
// make sense. e.g. don't fold:
|
|
|
|
//
|
2017-11-30 20:12:19 +08:00
|
|
|
// %1 = COPY %0:sub1
|
|
|
|
// %2<tied3> = V_MAC_{F16, F32} %3, %4, %1<tied0>
|
2016-08-16 00:18:36 +08:00
|
|
|
//
|
|
|
|
// into
|
2017-11-30 20:12:19 +08:00
|
|
|
// %2<tied3> = V_MAC_{F16, F32} %3, %4, %0:sub1<tied0>
|
2016-08-16 00:18:36 +08:00
|
|
|
if (UseOp.isTied() && OpToFold.getSubReg() != AMDGPU::NoSubRegister)
|
|
|
|
return;
|
2015-08-29 07:45:19 +08:00
|
|
|
}
|
|
|
|
|
2015-09-09 23:43:26 +08:00
|
|
|
// Special case for REG_SEQUENCE: We can't fold literals into
|
|
|
|
// REG_SEQUENCE instructions, so we have to fold them into the
|
|
|
|
// uses of REG_SEQUENCE.
|
2016-11-24 05:51:07 +08:00
|
|
|
if (UseMI->isRegSequence()) {
|
2015-09-09 23:43:26 +08:00
|
|
|
unsigned RegSeqDstReg = UseMI->getOperand(0).getReg();
|
|
|
|
unsigned RegSeqDstSubReg = UseMI->getOperand(UseOpIdx + 1).getImm();
|
|
|
|
|
|
|
|
for (MachineRegisterInfo::use_iterator
|
2017-01-11 07:32:04 +08:00
|
|
|
RSUse = MRI->use_begin(RegSeqDstReg), RSE = MRI->use_end();
|
2016-11-24 05:51:07 +08:00
|
|
|
RSUse != RSE; ++RSUse) {
|
2015-09-09 23:43:26 +08:00
|
|
|
|
|
|
|
MachineInstr *RSUseMI = RSUse->getParent();
|
|
|
|
if (RSUse->getSubReg() != RegSeqDstSubReg)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
foldOperand(OpToFold, RSUseMI, RSUse.getOperandNo(), FoldList,
|
2017-01-11 07:32:04 +08:00
|
|
|
CopiesToReplace);
|
2015-09-09 23:43:26 +08:00
|
|
|
}
|
2016-11-24 05:51:07 +08:00
|
|
|
|
2015-09-09 23:43:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-29 07:45:19 +08:00
|
|
|
|
2016-11-24 05:51:07 +08:00
|
|
|
bool FoldingImm = OpToFold.isImm();
|
2015-08-29 07:45:19 +08:00
|
|
|
|
2016-11-24 05:51:07 +08:00
|
|
|
// In order to fold immediates into copies, we need to change the
|
|
|
|
// copy to a MOV.
|
|
|
|
if (FoldingImm && UseMI->isCopy()) {
|
|
|
|
unsigned DestReg = UseMI->getOperand(0).getReg();
|
|
|
|
const TargetRegisterClass *DestRC
|
|
|
|
= TargetRegisterInfo::isVirtualRegister(DestReg) ?
|
2017-01-11 07:32:04 +08:00
|
|
|
MRI->getRegClass(DestReg) :
|
|
|
|
TRI->getPhysRegClass(DestReg);
|
2016-11-24 05:51:07 +08:00
|
|
|
|
|
|
|
unsigned MovOp = TII->getMovOpcode(DestRC);
|
|
|
|
if (MovOp == AMDGPU::COPY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
UseMI->setDesc(TII->get(MovOp));
|
|
|
|
CopiesToReplace.push_back(UseMI);
|
|
|
|
} else {
|
|
|
|
const MCInstrDesc &UseDesc = UseMI->getDesc();
|
|
|
|
|
|
|
|
// Don't fold into target independent nodes. Target independent opcodes
|
|
|
|
// don't have defined register classes.
|
|
|
|
if (UseDesc.isVariadic() ||
|
2018-02-08 09:12:46 +08:00
|
|
|
UseOp.isImplicit() ||
|
2016-11-24 05:51:07 +08:00
|
|
|
UseDesc.OpInfo[UseOpIdx].RegClass == -1)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!FoldingImm) {
|
|
|
|
tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold, TII);
|
|
|
|
|
|
|
|
// FIXME: We could try to change the instruction from 64-bit to 32-bit
|
|
|
|
// to enable more folding opportunites. The shrink operands pass
|
|
|
|
// already does this.
|
2015-08-29 07:45:19 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-24 05:51:07 +08:00
|
|
|
|
|
|
|
const MCInstrDesc &FoldDesc = OpToFold.getParent()->getDesc();
|
|
|
|
const TargetRegisterClass *FoldRC =
|
2017-01-11 07:32:04 +08:00
|
|
|
TRI->getRegClass(FoldDesc.OpInfo[0].RegClass);
|
2016-11-24 05:51:07 +08:00
|
|
|
|
2016-12-10 08:39:12 +08:00
|
|
|
|
2016-11-24 05:51:07 +08:00
|
|
|
// Split 64-bit constants into 32-bits for folding.
|
|
|
|
if (UseOp.getSubReg() && AMDGPU::getRegBitWidth(FoldRC->getID()) == 64) {
|
|
|
|
unsigned UseReg = UseOp.getReg();
|
|
|
|
const TargetRegisterClass *UseRC
|
|
|
|
= TargetRegisterInfo::isVirtualRegister(UseReg) ?
|
2017-01-11 07:32:04 +08:00
|
|
|
MRI->getRegClass(UseReg) :
|
|
|
|
TRI->getPhysRegClass(UseReg);
|
2016-11-24 05:51:07 +08:00
|
|
|
|
|
|
|
if (AMDGPU::getRegBitWidth(UseRC->getID()) != 64)
|
|
|
|
return;
|
|
|
|
|
2017-02-28 06:15:25 +08:00
|
|
|
APInt Imm(64, OpToFold.getImm());
|
2016-11-24 05:51:07 +08:00
|
|
|
if (UseOp.getSubReg() == AMDGPU::sub0) {
|
|
|
|
Imm = Imm.getLoBits(32);
|
|
|
|
} else {
|
|
|
|
assert(UseOp.getSubReg() == AMDGPU::sub1);
|
|
|
|
Imm = Imm.getHiBits(32);
|
|
|
|
}
|
2017-02-28 06:15:25 +08:00
|
|
|
|
|
|
|
MachineOperand ImmOp = MachineOperand::CreateImm(Imm.getSExtValue());
|
|
|
|
tryAddToFoldList(FoldList, UseMI, UseOpIdx, &ImmOp, TII);
|
|
|
|
return;
|
2016-11-24 05:51:07 +08:00
|
|
|
}
|
2015-08-29 07:45:19 +08:00
|
|
|
|
2017-02-28 06:15:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
tryAddToFoldList(FoldList, UseMI, UseOpIdx, &OpToFold, TII);
|
2015-08-29 07:45:19 +08:00
|
|
|
}
|
|
|
|
|
2016-09-14 23:19:03 +08:00
|
|
|
static bool evalBinaryInstruction(unsigned Opcode, int32_t &Result,
|
2017-01-11 07:32:04 +08:00
|
|
|
uint32_t LHS, uint32_t RHS) {
|
2016-09-14 23:19:03 +08:00
|
|
|
switch (Opcode) {
|
|
|
|
case AMDGPU::V_AND_B32_e64:
|
2017-01-11 07:32:04 +08:00
|
|
|
case AMDGPU::V_AND_B32_e32:
|
2016-09-14 23:19:03 +08:00
|
|
|
case AMDGPU::S_AND_B32:
|
|
|
|
Result = LHS & RHS;
|
|
|
|
return true;
|
|
|
|
case AMDGPU::V_OR_B32_e64:
|
2017-01-11 07:32:04 +08:00
|
|
|
case AMDGPU::V_OR_B32_e32:
|
2016-09-14 23:19:03 +08:00
|
|
|
case AMDGPU::S_OR_B32:
|
|
|
|
Result = LHS | RHS;
|
|
|
|
return true;
|
|
|
|
case AMDGPU::V_XOR_B32_e64:
|
2017-01-11 07:32:04 +08:00
|
|
|
case AMDGPU::V_XOR_B32_e32:
|
2016-09-14 23:19:03 +08:00
|
|
|
case AMDGPU::S_XOR_B32:
|
|
|
|
Result = LHS ^ RHS;
|
|
|
|
return true;
|
2017-01-11 07:32:04 +08:00
|
|
|
case AMDGPU::V_LSHL_B32_e64:
|
|
|
|
case AMDGPU::V_LSHL_B32_e32:
|
|
|
|
case AMDGPU::S_LSHL_B32:
|
|
|
|
// The instruction ignores the high bits for out of bounds shifts.
|
|
|
|
Result = LHS << (RHS & 31);
|
|
|
|
return true;
|
|
|
|
case AMDGPU::V_LSHLREV_B32_e64:
|
|
|
|
case AMDGPU::V_LSHLREV_B32_e32:
|
|
|
|
Result = RHS << (LHS & 31);
|
|
|
|
return true;
|
|
|
|
case AMDGPU::V_LSHR_B32_e64:
|
|
|
|
case AMDGPU::V_LSHR_B32_e32:
|
|
|
|
case AMDGPU::S_LSHR_B32:
|
|
|
|
Result = LHS >> (RHS & 31);
|
|
|
|
return true;
|
|
|
|
case AMDGPU::V_LSHRREV_B32_e64:
|
|
|
|
case AMDGPU::V_LSHRREV_B32_e32:
|
|
|
|
Result = RHS >> (LHS & 31);
|
|
|
|
return true;
|
|
|
|
case AMDGPU::V_ASHR_I32_e64:
|
|
|
|
case AMDGPU::V_ASHR_I32_e32:
|
|
|
|
case AMDGPU::S_ASHR_I32:
|
|
|
|
Result = static_cast<int32_t>(LHS) >> (RHS & 31);
|
|
|
|
return true;
|
|
|
|
case AMDGPU::V_ASHRREV_I32_e64:
|
|
|
|
case AMDGPU::V_ASHRREV_I32_e32:
|
|
|
|
Result = static_cast<int32_t>(RHS) >> (LHS & 31);
|
|
|
|
return true;
|
2016-09-14 23:19:03 +08:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getMovOpc(bool IsScalar) {
|
|
|
|
return IsScalar ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32;
|
|
|
|
}
|
|
|
|
|
2016-10-07 01:54:30 +08:00
|
|
|
/// Remove any leftover implicit operands from mutating the instruction. e.g.
|
|
|
|
/// if we replace an s_and_b32 with a copy, we don't need the implicit scc def
|
|
|
|
/// anymore.
|
|
|
|
static void stripExtraCopyOperands(MachineInstr &MI) {
|
|
|
|
const MCInstrDesc &Desc = MI.getDesc();
|
|
|
|
unsigned NumOps = Desc.getNumOperands() +
|
|
|
|
Desc.getNumImplicitUses() +
|
|
|
|
Desc.getNumImplicitDefs();
|
|
|
|
|
|
|
|
for (unsigned I = MI.getNumOperands() - 1; I >= NumOps; --I)
|
|
|
|
MI.RemoveOperand(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mutateCopyOp(MachineInstr &MI, const MCInstrDesc &NewDesc) {
|
|
|
|
MI.setDesc(NewDesc);
|
|
|
|
stripExtraCopyOperands(MI);
|
|
|
|
}
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
static MachineOperand *getImmOrMaterializedImm(MachineRegisterInfo &MRI,
|
|
|
|
MachineOperand &Op) {
|
|
|
|
if (Op.isReg()) {
|
|
|
|
// If this has a subregister, it obviously is a register source.
|
2018-03-11 00:05:35 +08:00
|
|
|
if (Op.getSubReg() != AMDGPU::NoSubRegister ||
|
|
|
|
!TargetRegisterInfo::isVirtualRegister(Op.getReg()))
|
2017-01-11 07:32:04 +08:00
|
|
|
return &Op;
|
|
|
|
|
|
|
|
MachineInstr *Def = MRI.getVRegDef(Op.getReg());
|
2017-06-21 02:28:02 +08:00
|
|
|
if (Def && Def->isMoveImmediate()) {
|
2017-01-11 07:32:04 +08:00
|
|
|
MachineOperand &ImmSrc = Def->getOperand(1);
|
|
|
|
if (ImmSrc.isImm())
|
|
|
|
return &ImmSrc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Op;
|
|
|
|
}
|
|
|
|
|
2016-09-14 23:19:03 +08:00
|
|
|
// Try to simplify operations with a constant that may appear after instruction
|
|
|
|
// selection.
|
2017-01-11 07:32:04 +08:00
|
|
|
// TODO: See if a frame index with a fixed offset can fold.
|
2016-09-14 23:19:03 +08:00
|
|
|
static bool tryConstantFoldOp(MachineRegisterInfo &MRI,
|
|
|
|
const SIInstrInfo *TII,
|
2017-01-11 07:32:04 +08:00
|
|
|
MachineInstr *MI,
|
|
|
|
MachineOperand *ImmOp) {
|
2016-09-14 23:19:03 +08:00
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
if (Opc == AMDGPU::V_NOT_B32_e64 || Opc == AMDGPU::V_NOT_B32_e32 ||
|
|
|
|
Opc == AMDGPU::S_NOT_B32) {
|
2017-01-11 07:32:04 +08:00
|
|
|
MI->getOperand(1).ChangeToImmediate(~ImmOp->getImm());
|
|
|
|
mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_NOT_B32)));
|
|
|
|
return true;
|
2016-09-14 23:19:03 +08:00
|
|
|
}
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1);
|
|
|
|
if (Src1Idx == -1)
|
2016-09-14 23:19:03 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0);
|
2017-01-11 07:32:04 +08:00
|
|
|
MachineOperand *Src0 = getImmOrMaterializedImm(MRI, MI->getOperand(Src0Idx));
|
|
|
|
MachineOperand *Src1 = getImmOrMaterializedImm(MRI, MI->getOperand(Src1Idx));
|
2016-09-14 23:19:03 +08:00
|
|
|
|
|
|
|
if (!Src0->isImm() && !Src1->isImm())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// and k0, k1 -> v_mov_b32 (k0 & k1)
|
|
|
|
// or k0, k1 -> v_mov_b32 (k0 | k1)
|
|
|
|
// xor k0, k1 -> v_mov_b32 (k0 ^ k1)
|
|
|
|
if (Src0->isImm() && Src1->isImm()) {
|
|
|
|
int32_t NewImm;
|
|
|
|
if (!evalBinaryInstruction(Opc, NewImm, Src0->getImm(), Src1->getImm()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const SIRegisterInfo &TRI = TII->getRegisterInfo();
|
|
|
|
bool IsSGPR = TRI.isSGPRReg(MRI, MI->getOperand(0).getReg());
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
// Be careful to change the right operand, src0 may belong to a different
|
|
|
|
// instruction.
|
|
|
|
MI->getOperand(Src0Idx).ChangeToImmediate(NewImm);
|
2016-09-14 23:19:03 +08:00
|
|
|
MI->RemoveOperand(Src1Idx);
|
2016-10-07 01:54:30 +08:00
|
|
|
mutateCopyOp(*MI, TII->get(getMovOpc(IsSGPR)));
|
2016-09-14 23:19:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
if (!MI->isCommutable())
|
|
|
|
return false;
|
|
|
|
|
2016-09-14 23:19:03 +08:00
|
|
|
if (Src0->isImm() && !Src1->isImm()) {
|
|
|
|
std::swap(Src0, Src1);
|
|
|
|
std::swap(Src0Idx, Src1Idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t Src1Val = static_cast<int32_t>(Src1->getImm());
|
2017-01-11 07:32:04 +08:00
|
|
|
if (Opc == AMDGPU::V_OR_B32_e64 ||
|
|
|
|
Opc == AMDGPU::V_OR_B32_e32 ||
|
|
|
|
Opc == AMDGPU::S_OR_B32) {
|
2016-09-14 23:19:03 +08:00
|
|
|
if (Src1Val == 0) {
|
|
|
|
// y = or x, 0 => y = copy x
|
|
|
|
MI->RemoveOperand(Src1Idx);
|
2016-10-07 01:54:30 +08:00
|
|
|
mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
|
2016-09-14 23:19:03 +08:00
|
|
|
} else if (Src1Val == -1) {
|
|
|
|
// y = or x, -1 => y = v_mov_b32 -1
|
|
|
|
MI->RemoveOperand(Src1Idx);
|
2016-10-07 01:54:30 +08:00
|
|
|
mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_OR_B32)));
|
2016-09-14 23:19:03 +08:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MI->getOpcode() == AMDGPU::V_AND_B32_e64 ||
|
2017-01-11 07:32:04 +08:00
|
|
|
MI->getOpcode() == AMDGPU::V_AND_B32_e32 ||
|
2016-09-14 23:19:03 +08:00
|
|
|
MI->getOpcode() == AMDGPU::S_AND_B32) {
|
|
|
|
if (Src1Val == 0) {
|
|
|
|
// y = and x, 0 => y = v_mov_b32 0
|
|
|
|
MI->RemoveOperand(Src0Idx);
|
2016-10-07 01:54:30 +08:00
|
|
|
mutateCopyOp(*MI, TII->get(getMovOpc(Opc == AMDGPU::S_AND_B32)));
|
2016-09-14 23:19:03 +08:00
|
|
|
} else if (Src1Val == -1) {
|
|
|
|
// y = and x, -1 => y = copy x
|
|
|
|
MI->RemoveOperand(Src1Idx);
|
2016-10-07 01:54:30 +08:00
|
|
|
mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
|
|
|
|
stripExtraCopyOperands(*MI);
|
2016-09-14 23:19:03 +08:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MI->getOpcode() == AMDGPU::V_XOR_B32_e64 ||
|
2017-01-11 07:32:04 +08:00
|
|
|
MI->getOpcode() == AMDGPU::V_XOR_B32_e32 ||
|
2016-09-14 23:19:03 +08:00
|
|
|
MI->getOpcode() == AMDGPU::S_XOR_B32) {
|
|
|
|
if (Src1Val == 0) {
|
|
|
|
// y = xor x, 0 => y = copy x
|
|
|
|
MI->RemoveOperand(Src1Idx);
|
2016-10-07 01:54:30 +08:00
|
|
|
mutateCopyOp(*MI, TII->get(AMDGPU::COPY));
|
2017-01-11 07:32:04 +08:00
|
|
|
return true;
|
2016-09-14 23:19:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-25 02:55:20 +08:00
|
|
|
// Try to fold an instruction into a simpler one
|
|
|
|
static bool tryFoldInst(const SIInstrInfo *TII,
|
|
|
|
MachineInstr *MI) {
|
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
|
|
|
|
if (Opc == AMDGPU::V_CNDMASK_B32_e32 ||
|
|
|
|
Opc == AMDGPU::V_CNDMASK_B32_e64 ||
|
|
|
|
Opc == AMDGPU::V_CNDMASK_B64_PSEUDO) {
|
|
|
|
const MachineOperand *Src0 = TII->getNamedOperand(*MI, AMDGPU::OpName::src0);
|
|
|
|
const MachineOperand *Src1 = TII->getNamedOperand(*MI, AMDGPU::OpName::src1);
|
|
|
|
if (Src1->isIdenticalTo(*Src0)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Folded " << *MI << " into ");
|
2017-03-25 02:55:20 +08:00
|
|
|
int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2);
|
|
|
|
if (Src2Idx != -1)
|
|
|
|
MI->RemoveOperand(Src2Idx);
|
|
|
|
MI->RemoveOperand(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1));
|
|
|
|
mutateCopyOp(*MI, TII->get(Src0->isReg() ? (unsigned)AMDGPU::COPY
|
|
|
|
: getMovOpc(false)));
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << *MI << '\n');
|
2017-03-25 02:55:20 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
void SIFoldOperands::foldInstOperand(MachineInstr &MI,
|
|
|
|
MachineOperand &OpToFold) const {
|
|
|
|
// We need mutate the operands of new mov instructions to add implicit
|
|
|
|
// uses of EXEC, but adding them invalidates the use_iterator, so defer
|
|
|
|
// this.
|
|
|
|
SmallVector<MachineInstr *, 4> CopiesToReplace;
|
|
|
|
SmallVector<FoldCandidate, 4> FoldList;
|
|
|
|
MachineOperand &Dst = MI.getOperand(0);
|
|
|
|
|
|
|
|
bool FoldingImm = OpToFold.isImm() || OpToFold.isFI();
|
|
|
|
if (FoldingImm) {
|
|
|
|
unsigned NumLiteralUses = 0;
|
|
|
|
MachineOperand *NonInlineUse = nullptr;
|
|
|
|
int NonInlineUseOpNo = -1;
|
|
|
|
|
2017-10-15 13:35:02 +08:00
|
|
|
MachineRegisterInfo::use_iterator NextUse;
|
2017-01-11 07:32:04 +08:00
|
|
|
for (MachineRegisterInfo::use_iterator
|
|
|
|
Use = MRI->use_begin(Dst.getReg()), E = MRI->use_end();
|
|
|
|
Use != E; Use = NextUse) {
|
|
|
|
NextUse = std::next(Use);
|
|
|
|
MachineInstr *UseMI = Use->getParent();
|
|
|
|
unsigned OpNo = Use.getOperandNo();
|
|
|
|
|
|
|
|
// Folding the immediate may reveal operations that can be constant
|
|
|
|
// folded or replaced with a copy. This can happen for example after
|
|
|
|
// frame indices are lowered to constants or from splitting 64-bit
|
|
|
|
// constants.
|
|
|
|
//
|
|
|
|
// We may also encounter cases where one or both operands are
|
|
|
|
// immediates materialized into a register, which would ordinarily not
|
|
|
|
// be folded due to multiple uses or operand constraints.
|
|
|
|
|
|
|
|
if (OpToFold.isImm() && tryConstantFoldOp(*MRI, TII, UseMI, &OpToFold)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Constant folded " << *UseMI << '\n');
|
2017-01-11 07:32:04 +08:00
|
|
|
|
|
|
|
// Some constant folding cases change the same immediate's use to a new
|
|
|
|
// instruction, e.g. and x, 0 -> 0. Make sure we re-visit the user
|
|
|
|
// again. The same constant folded instruction could also have a second
|
|
|
|
// use operand.
|
|
|
|
NextUse = MRI->use_begin(Dst.getReg());
|
AMDGPU: Fix crash when folding immediates into multiple uses
Summary:
When an immediate is folded by constant folding, we re-scan the entire
use list for two reasons:
1. The constant folding may have created a new use of the same reg.
2. The constant folding may have removed an additional use in the list
we're currently traversing (e.g., constant folding an S_ADD_I32 c, c).
However, this could previously lead to a crash when an unrelated use was
added twice into the FoldList. Since we re-scan the whole list anyway, we
might as well just clear the FoldList again before we do so.
Using a MIR test to show this because real code seems to trigger the issue
only in connection with some really subtle control flow structures.
Fixes GL45-CTS.shading_language_420pack.binding_images on gfx9.
Reviewers: arsenm
Subscribers: kzhuravl, wdng, yaxunl, dstuttard, tpr, llvm-commits, t-tye
Differential Revision: https://reviews.llvm.org/D35416
llvm-svn: 308314
2017-07-18 22:54:41 +08:00
|
|
|
FoldList.clear();
|
2017-01-11 07:32:04 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fold any inline immediate uses, and then only fold other
|
|
|
|
// constants if they have one use.
|
|
|
|
//
|
|
|
|
// The legality of the inline immediate must be checked based on the use
|
|
|
|
// operand, not the defining instruction, because 32-bit instructions
|
|
|
|
// with 32-bit inline immediate sources may be used to materialize
|
|
|
|
// constants used in 16-bit operands.
|
|
|
|
//
|
|
|
|
// e.g. it is unsafe to fold:
|
|
|
|
// s_mov_b32 s0, 1.0 // materializes 0x3f800000
|
|
|
|
// v_add_f16 v0, v1, s0 // 1.0 f16 inline immediate sees 0x00003c00
|
|
|
|
|
|
|
|
// Folding immediates with more than one use will increase program size.
|
|
|
|
// FIXME: This will also reduce register usage, which may be better
|
|
|
|
// in some cases. A better heuristic is needed.
|
2017-01-12 06:00:02 +08:00
|
|
|
if (isInlineConstantIfFolded(TII, *UseMI, OpNo, OpToFold)) {
|
2017-01-11 07:32:04 +08:00
|
|
|
foldOperand(OpToFold, UseMI, OpNo, FoldList, CopiesToReplace);
|
|
|
|
} else {
|
|
|
|
if (++NumLiteralUses == 1) {
|
|
|
|
NonInlineUse = &*Use;
|
|
|
|
NonInlineUseOpNo = OpNo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NumLiteralUses == 1) {
|
|
|
|
MachineInstr *UseMI = NonInlineUse->getParent();
|
|
|
|
foldOperand(OpToFold, UseMI, NonInlineUseOpNo, FoldList, CopiesToReplace);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Folding register.
|
|
|
|
for (MachineRegisterInfo::use_iterator
|
|
|
|
Use = MRI->use_begin(Dst.getReg()), E = MRI->use_end();
|
|
|
|
Use != E; ++Use) {
|
|
|
|
MachineInstr *UseMI = Use->getParent();
|
|
|
|
|
|
|
|
foldOperand(OpToFold, UseMI, Use.getOperandNo(),
|
|
|
|
FoldList, CopiesToReplace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineFunction *MF = MI.getParent()->getParent();
|
|
|
|
// Make sure we add EXEC uses to any new v_mov instructions created.
|
|
|
|
for (MachineInstr *Copy : CopiesToReplace)
|
|
|
|
Copy->addImplicitDefUseOperands(*MF);
|
|
|
|
|
|
|
|
for (FoldCandidate &Fold : FoldList) {
|
|
|
|
if (updateOperand(Fold, *TRI)) {
|
|
|
|
// Clear kill flags.
|
|
|
|
if (Fold.isReg()) {
|
|
|
|
assert(Fold.OpToFold && Fold.OpToFold->isReg());
|
|
|
|
// FIXME: Probably shouldn't bother trying to fold if not an
|
|
|
|
// SGPR. PeepholeOptimizer can eliminate redundant VGPR->VGPR
|
|
|
|
// copies.
|
|
|
|
MRI->clearKillFlags(Fold.OpToFold->getReg());
|
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Folded source from " << MI << " into OpNo "
|
|
|
|
<< static_cast<int>(Fold.UseOpNo) << " of "
|
|
|
|
<< *Fold.UseMI << '\n');
|
2017-03-25 02:55:20 +08:00
|
|
|
tryFoldInst(TII, Fold.UseMI);
|
2017-06-03 08:41:52 +08:00
|
|
|
} else if (Fold.isCommuted()) {
|
|
|
|
// Restoring instruction's original operand order if fold has failed.
|
|
|
|
TII->commuteInstruction(*Fold.UseMI, false);
|
2017-01-11 07:32:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 08:13:20 +08:00
|
|
|
// Clamp patterns are canonically selected to v_max_* instructions, so only
|
|
|
|
// handle them.
|
2017-02-23 07:27:53 +08:00
|
|
|
const MachineOperand *SIFoldOperands::isClamp(const MachineInstr &MI) const {
|
|
|
|
unsigned Op = MI.getOpcode();
|
|
|
|
switch (Op) {
|
|
|
|
case AMDGPU::V_MAX_F32_e64:
|
2017-02-23 07:53:37 +08:00
|
|
|
case AMDGPU::V_MAX_F16_e64:
|
2017-09-01 07:53:50 +08:00
|
|
|
case AMDGPU::V_MAX_F64:
|
|
|
|
case AMDGPU::V_PK_MAX_F16: {
|
2017-02-23 07:27:53 +08:00
|
|
|
if (!TII->getNamedOperand(MI, AMDGPU::OpName::clamp)->getImm())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Make sure sources are identical.
|
|
|
|
const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
|
|
|
|
const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
|
2017-06-05 09:03:04 +08:00
|
|
|
if (!Src0->isReg() || !Src1->isReg() ||
|
2017-10-05 08:13:17 +08:00
|
|
|
Src0->getReg() != Src1->getReg() ||
|
2017-06-05 09:03:04 +08:00
|
|
|
Src0->getSubReg() != Src1->getSubReg() ||
|
2017-02-23 07:27:53 +08:00
|
|
|
Src0->getSubReg() != AMDGPU::NoSubRegister)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Can't fold up if we have modifiers.
|
2017-09-01 07:53:50 +08:00
|
|
|
if (TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
unsigned Src0Mods
|
|
|
|
= TII->getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm();
|
|
|
|
unsigned Src1Mods
|
|
|
|
= TII->getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm();
|
|
|
|
|
|
|
|
// Having a 0 op_sel_hi would require swizzling the output in the source
|
|
|
|
// instruction, which we can't do.
|
|
|
|
unsigned UnsetMods = (Op == AMDGPU::V_PK_MAX_F16) ? SISrcMods::OP_SEL_1 : 0;
|
|
|
|
if (Src0Mods != UnsetMods && Src1Mods != UnsetMods)
|
2017-02-23 07:27:53 +08:00
|
|
|
return nullptr;
|
|
|
|
return Src0;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We obviously have multiple uses in a clamp since the register is used twice
|
|
|
|
// in the same instruction.
|
|
|
|
static bool hasOneNonDBGUseInst(const MachineRegisterInfo &MRI, unsigned Reg) {
|
|
|
|
int Count = 0;
|
|
|
|
for (auto I = MRI.use_instr_nodbg_begin(Reg), E = MRI.use_instr_nodbg_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (++Count > 1)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-21 05:01:24 +08:00
|
|
|
// FIXME: Clamp for v_mad_mixhi_f16 handled during isel.
|
2017-02-23 07:27:53 +08:00
|
|
|
bool SIFoldOperands::tryFoldClamp(MachineInstr &MI) {
|
|
|
|
const MachineOperand *ClampSrc = isClamp(MI);
|
|
|
|
if (!ClampSrc || !hasOneNonDBGUseInst(*MRI, ClampSrc->getReg()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineInstr *Def = MRI->getVRegDef(ClampSrc->getReg());
|
2017-09-01 07:53:50 +08:00
|
|
|
|
|
|
|
// The type of clamp must be compatible.
|
|
|
|
if (TII->getClampMask(*Def) != TII->getClampMask(MI))
|
2017-02-23 07:27:53 +08:00
|
|
|
return false;
|
2017-09-01 07:53:50 +08:00
|
|
|
|
2017-02-23 07:27:53 +08:00
|
|
|
MachineOperand *DefClamp = TII->getNamedOperand(*Def, AMDGPU::OpName::clamp);
|
|
|
|
if (!DefClamp)
|
|
|
|
return false;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Folding clamp " << *DefClamp << " into " << *Def
|
|
|
|
<< '\n');
|
2017-02-23 07:27:53 +08:00
|
|
|
|
|
|
|
// Clamp is applied after omod, so it is OK if omod is set.
|
|
|
|
DefClamp->setImm(1);
|
|
|
|
MRI->replaceRegWith(MI.getOperand(0).getReg(), Def->getOperand(0).getReg());
|
|
|
|
MI.eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-28 03:35:42 +08:00
|
|
|
static int getOModValue(unsigned Opc, int64_t Val) {
|
|
|
|
switch (Opc) {
|
|
|
|
case AMDGPU::V_MUL_F32_e64: {
|
|
|
|
switch (static_cast<uint32_t>(Val)) {
|
|
|
|
case 0x3f000000: // 0.5
|
|
|
|
return SIOutMods::DIV2;
|
|
|
|
case 0x40000000: // 2.0
|
|
|
|
return SIOutMods::MUL2;
|
|
|
|
case 0x40800000: // 4.0
|
|
|
|
return SIOutMods::MUL4;
|
|
|
|
default:
|
|
|
|
return SIOutMods::NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case AMDGPU::V_MUL_F16_e64: {
|
|
|
|
switch (static_cast<uint16_t>(Val)) {
|
|
|
|
case 0x3800: // 0.5
|
|
|
|
return SIOutMods::DIV2;
|
|
|
|
case 0x4000: // 2.0
|
|
|
|
return SIOutMods::MUL2;
|
|
|
|
case 0x4400: // 4.0
|
|
|
|
return SIOutMods::MUL4;
|
|
|
|
default:
|
|
|
|
return SIOutMods::NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
llvm_unreachable("invalid mul opcode");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Does this really not support denormals with f16?
|
|
|
|
// FIXME: Does this need to check IEEE mode bit? SNaNs are generally not
|
|
|
|
// handled, so will anything other than that break?
|
|
|
|
std::pair<const MachineOperand *, int>
|
|
|
|
SIFoldOperands::isOMod(const MachineInstr &MI) const {
|
|
|
|
unsigned Op = MI.getOpcode();
|
|
|
|
switch (Op) {
|
|
|
|
case AMDGPU::V_MUL_F32_e64:
|
|
|
|
case AMDGPU::V_MUL_F16_e64: {
|
|
|
|
// If output denormals are enabled, omod is ignored.
|
|
|
|
if ((Op == AMDGPU::V_MUL_F32_e64 && ST->hasFP32Denormals()) ||
|
|
|
|
(Op == AMDGPU::V_MUL_F16_e64 && ST->hasFP16Denormals()))
|
|
|
|
return std::make_pair(nullptr, SIOutMods::NONE);
|
|
|
|
|
|
|
|
const MachineOperand *RegOp = nullptr;
|
|
|
|
const MachineOperand *ImmOp = nullptr;
|
|
|
|
const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
|
|
|
|
const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
|
|
|
|
if (Src0->isImm()) {
|
|
|
|
ImmOp = Src0;
|
|
|
|
RegOp = Src1;
|
|
|
|
} else if (Src1->isImm()) {
|
|
|
|
ImmOp = Src1;
|
|
|
|
RegOp = Src0;
|
|
|
|
} else
|
|
|
|
return std::make_pair(nullptr, SIOutMods::NONE);
|
|
|
|
|
|
|
|
int OMod = getOModValue(Op, ImmOp->getImm());
|
|
|
|
if (OMod == SIOutMods::NONE ||
|
|
|
|
TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) ||
|
|
|
|
TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) ||
|
|
|
|
TII->hasModifiersSet(MI, AMDGPU::OpName::omod) ||
|
|
|
|
TII->hasModifiersSet(MI, AMDGPU::OpName::clamp))
|
|
|
|
return std::make_pair(nullptr, SIOutMods::NONE);
|
|
|
|
|
|
|
|
return std::make_pair(RegOp, OMod);
|
|
|
|
}
|
|
|
|
case AMDGPU::V_ADD_F32_e64:
|
|
|
|
case AMDGPU::V_ADD_F16_e64: {
|
|
|
|
// If output denormals are enabled, omod is ignored.
|
|
|
|
if ((Op == AMDGPU::V_ADD_F32_e64 && ST->hasFP32Denormals()) ||
|
|
|
|
(Op == AMDGPU::V_ADD_F16_e64 && ST->hasFP16Denormals()))
|
|
|
|
return std::make_pair(nullptr, SIOutMods::NONE);
|
|
|
|
|
|
|
|
// Look through the DAGCombiner canonicalization fmul x, 2 -> fadd x, x
|
|
|
|
const MachineOperand *Src0 = TII->getNamedOperand(MI, AMDGPU::OpName::src0);
|
|
|
|
const MachineOperand *Src1 = TII->getNamedOperand(MI, AMDGPU::OpName::src1);
|
|
|
|
|
|
|
|
if (Src0->isReg() && Src1->isReg() && Src0->getReg() == Src1->getReg() &&
|
|
|
|
Src0->getSubReg() == Src1->getSubReg() &&
|
|
|
|
!TII->hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) &&
|
|
|
|
!TII->hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) &&
|
|
|
|
!TII->hasModifiersSet(MI, AMDGPU::OpName::clamp) &&
|
|
|
|
!TII->hasModifiersSet(MI, AMDGPU::OpName::omod))
|
|
|
|
return std::make_pair(Src0, SIOutMods::MUL2);
|
|
|
|
|
|
|
|
return std::make_pair(nullptr, SIOutMods::NONE);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return std::make_pair(nullptr, SIOutMods::NONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Does this need to check IEEE bit on function?
|
|
|
|
bool SIFoldOperands::tryFoldOMod(MachineInstr &MI) {
|
|
|
|
const MachineOperand *RegOp;
|
|
|
|
int OMod;
|
|
|
|
std::tie(RegOp, OMod) = isOMod(MI);
|
|
|
|
if (OMod == SIOutMods::NONE || !RegOp->isReg() ||
|
|
|
|
RegOp->getSubReg() != AMDGPU::NoSubRegister ||
|
|
|
|
!hasOneNonDBGUseInst(*MRI, RegOp->getReg()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineInstr *Def = MRI->getVRegDef(RegOp->getReg());
|
|
|
|
MachineOperand *DefOMod = TII->getNamedOperand(*Def, AMDGPU::OpName::omod);
|
|
|
|
if (!DefOMod || DefOMod->getImm() != SIOutMods::NONE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Clamp is applied after omod. If the source already has clamp set, don't
|
|
|
|
// fold it.
|
|
|
|
if (TII->hasModifiersSet(*Def, AMDGPU::OpName::clamp))
|
|
|
|
return false;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Folding omod " << MI << " into " << *Def << '\n');
|
2017-02-28 03:35:42 +08:00
|
|
|
|
|
|
|
DefOMod->setImm(OMod);
|
|
|
|
MRI->replaceRegWith(MI.getOperand(0).getReg(), Def->getOperand(0).getReg());
|
|
|
|
MI.eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-22 06:06:37 +08:00
|
|
|
bool SIFoldOperands::runOnMachineFunction(MachineFunction &MF) {
|
2017-12-16 06:22:58 +08:00
|
|
|
if (skipFunction(MF.getFunction()))
|
2016-04-26 06:23:44 +08:00
|
|
|
return false;
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
AMDGPU: Separate R600 and GCN TableGen files
Summary:
We now have two sets of generated TableGen files, one for R600 and one
for GCN, so each sub-target now has its own tables of instructions,
registers, ISel patterns, etc. This should help reduce compile time
since each sub-target now only has to consider information that
is specific to itself. This will also help prevent the R600
sub-target from slowing down new features for GCN, like disassembler
support, GlobalISel, etc.
Reviewers: arsenm, nhaehnle, jvesely
Reviewed By: arsenm
Subscribers: MatzeB, kzhuravl, wdng, mgorny, yaxunl, dstuttard, tpr, t-tye, javed.absar, llvm-commits
Differential Revision: https://reviews.llvm.org/D46365
llvm-svn: 335942
2018-06-29 07:47:12 +08:00
|
|
|
ST = &MF.getSubtarget<AMDGPUSubtarget>();
|
2017-02-23 07:27:53 +08:00
|
|
|
TII = ST->getInstrInfo();
|
2017-01-11 07:32:04 +08:00
|
|
|
TRI = &TII->getRegisterInfo();
|
2014-11-22 06:06:37 +08:00
|
|
|
|
2017-02-28 03:35:42 +08:00
|
|
|
const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
|
|
|
|
|
|
|
|
// omod is ignored by hardware if IEEE bit is enabled. omod also does not
|
|
|
|
// correctly handle signed zeros.
|
|
|
|
//
|
|
|
|
// TODO: Check nsz on instructions when fast math flags are preserved to MI
|
|
|
|
// level.
|
|
|
|
bool IsIEEEMode = ST->enableIEEEBit(MF) || !MFI->hasNoSignedZerosFPMath();
|
|
|
|
|
2017-06-21 02:56:32 +08:00
|
|
|
for (MachineBasicBlock *MBB : depth_first(&MF)) {
|
2014-11-22 06:06:37 +08:00
|
|
|
MachineBasicBlock::iterator I, Next;
|
2017-06-21 02:56:32 +08:00
|
|
|
for (I = MBB->begin(); I != MBB->end(); I = Next) {
|
2014-11-22 06:06:37 +08:00
|
|
|
Next = std::next(I);
|
|
|
|
MachineInstr &MI = *I;
|
|
|
|
|
2017-03-25 02:55:20 +08:00
|
|
|
tryFoldInst(TII, &MI);
|
|
|
|
|
2017-03-31 19:42:43 +08:00
|
|
|
if (!TII->isFoldableCopy(MI)) {
|
2017-02-28 03:35:42 +08:00
|
|
|
if (IsIEEEMode || !tryFoldOMod(MI))
|
|
|
|
tryFoldClamp(MI);
|
2014-11-22 06:06:37 +08:00
|
|
|
continue;
|
2017-02-23 07:27:53 +08:00
|
|
|
}
|
2014-11-22 06:06:37 +08:00
|
|
|
|
|
|
|
MachineOperand &OpToFold = MI.getOperand(1);
|
2016-09-14 23:51:33 +08:00
|
|
|
bool FoldingImm = OpToFold.isImm() || OpToFold.isFI();
|
2015-01-08 06:18:27 +08:00
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
// FIXME: We could also be folding things like TargetIndexes.
|
2015-01-08 06:44:19 +08:00
|
|
|
if (!FoldingImm && !OpToFold.isReg())
|
|
|
|
continue;
|
|
|
|
|
2014-11-22 06:06:37 +08:00
|
|
|
if (OpToFold.isReg() &&
|
2016-01-08 01:10:29 +08:00
|
|
|
!TargetRegisterInfo::isVirtualRegister(OpToFold.getReg()))
|
2014-11-22 06:06:37 +08:00
|
|
|
continue;
|
|
|
|
|
2016-01-13 19:44:29 +08:00
|
|
|
// Prevent folding operands backwards in the function. For example,
|
|
|
|
// the COPY opcode must not be replaced by 1 in this example:
|
|
|
|
//
|
2017-12-07 18:40:31 +08:00
|
|
|
// %3 = COPY %vgpr0; VGPR_32:%3
|
2016-01-13 19:44:29 +08:00
|
|
|
// ...
|
2017-12-07 18:40:31 +08:00
|
|
|
// %vgpr0 = V_MOV_B32_e32 1, implicit %exec
|
2016-01-13 19:44:29 +08:00
|
|
|
MachineOperand &Dst = MI.getOperand(0);
|
|
|
|
if (Dst.isReg() &&
|
|
|
|
!TargetRegisterInfo::isVirtualRegister(Dst.getReg()))
|
|
|
|
continue;
|
|
|
|
|
2017-01-11 07:32:04 +08:00
|
|
|
foldInstOperand(MI, OpToFold);
|
2014-11-22 06:06:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|