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"
|
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2014-11-22 06:06:37 +08:00
|
|
|
#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 {
|
|
|
|
|
|
|
|
class SIFoldOperands : public MachineFunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SIFoldOperands() : MachineFunctionPass(ID) {
|
|
|
|
initializeSIFoldOperandsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
const char *getPassName() const override {
|
|
|
|
return "SI Fold Operands";
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<MachineDominatorTree>();
|
2015-09-26 01:21:28 +08:00
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
2014-11-22 06:06:37 +08:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-08 01:42:16 +08:00
|
|
|
struct FoldCandidate {
|
|
|
|
MachineInstr *UseMI;
|
|
|
|
unsigned UseOpNo;
|
|
|
|
MachineOperand *OpToFold;
|
|
|
|
uint64_t ImmToFold;
|
|
|
|
|
|
|
|
FoldCandidate(MachineInstr *MI, unsigned OpNo, MachineOperand *FoldOp) :
|
2015-01-08 06:44:19 +08:00
|
|
|
UseMI(MI), UseOpNo(OpNo) {
|
|
|
|
|
|
|
|
if (FoldOp->isImm()) {
|
|
|
|
OpToFold = nullptr;
|
|
|
|
ImmToFold = FoldOp->getImm();
|
|
|
|
} else {
|
|
|
|
assert(FoldOp->isReg());
|
|
|
|
OpToFold = FoldOp;
|
|
|
|
}
|
|
|
|
}
|
2015-01-08 01:42:16 +08:00
|
|
|
|
|
|
|
bool isImm() const {
|
|
|
|
return !OpToFold;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-22 06:06:37 +08:00
|
|
|
} // End anonymous namespace.
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(SIFoldOperands, DEBUG_TYPE,
|
|
|
|
"SI Fold Operands", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
|
|
|
|
INITIALIZE_PASS_END(SIFoldOperands, DEBUG_TYPE,
|
|
|
|
"SI Fold Operands", false, false)
|
|
|
|
|
|
|
|
char SIFoldOperands::ID = 0;
|
|
|
|
|
|
|
|
char &llvm::SIFoldOperandsID = SIFoldOperands::ID;
|
|
|
|
|
|
|
|
FunctionPass *llvm::createSIFoldOperandsPass() {
|
|
|
|
return new SIFoldOperands();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isSafeToFold(unsigned Opcode) {
|
|
|
|
switch(Opcode) {
|
|
|
|
case AMDGPU::V_MOV_B32_e32:
|
|
|
|
case AMDGPU::V_MOV_B32_e64:
|
2015-01-08 04:27:25 +08:00
|
|
|
case AMDGPU::V_MOV_B64_PSEUDO:
|
2014-11-22 06:06:37 +08:00
|
|
|
case AMDGPU::S_MOV_B32:
|
|
|
|
case AMDGPU::S_MOV_B64:
|
|
|
|
case AMDGPU::COPY:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
Old.ChangeToImmediate(Fold.ImmToFold);
|
2014-11-22 06:06:37 +08:00
|
|
|
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);
|
2014-11-22 06:06:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Handle physical registers.
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-13 23:47:57 +08:00
|
|
|
static bool isUseMIInFoldList(const std::vector<FoldCandidate> &FoldList,
|
|
|
|
const MachineInstr *MI) {
|
|
|
|
for (auto Candidate : FoldList) {
|
|
|
|
if (Candidate.UseMI == MI)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-08 06:44:19 +08:00
|
|
|
static bool tryAddToFoldList(std::vector<FoldCandidate> &FoldList,
|
|
|
|
MachineInstr *MI, unsigned OpNo,
|
|
|
|
MachineOperand *OpToFold,
|
|
|
|
const SIInstrInfo *TII) {
|
|
|
|
if (!TII->isOperandLegal(MI, OpNo, OpToFold)) {
|
2015-07-13 23:47:57 +08:00
|
|
|
|
|
|
|
// Special case for v_mac_f32_e64 if we are trying to fold into src2
|
|
|
|
unsigned Opc = MI->getOpcode();
|
|
|
|
if (Opc == AMDGPU::V_MAC_F32_e64 &&
|
|
|
|
(int)OpNo == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)) {
|
|
|
|
// Check if changing this to a v_mad_f32 instruction will allow us to
|
|
|
|
// fold the operand.
|
|
|
|
MI->setDesc(TII->get(AMDGPU::V_MAD_F32));
|
|
|
|
bool FoldAsMAD = tryAddToFoldList(FoldList, MI, OpNo, OpToFold, TII);
|
|
|
|
if (FoldAsMAD) {
|
|
|
|
MI->untieRegOperand(OpNo);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
MI->setDesc(TII->get(Opc));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2015-01-08 06:44:19 +08:00
|
|
|
bool CanCommute = TII->findCommutedOpIndices(MI, CommuteIdx0, CommuteIdx1);
|
|
|
|
|
|
|
|
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 ||
|
|
|
|
!TII->commuteInstruction(MI, false, CommuteIdx0, CommuteIdx1))
|
2015-01-08 06:44:19 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!TII->isOperandLegal(MI, OpNo, OpToFold))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FoldList.push_back(FoldCandidate(MI, OpNo, OpToFold));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-29 07:45:19 +08:00
|
|
|
static void foldOperand(MachineOperand &OpToFold, MachineInstr *UseMI,
|
|
|
|
unsigned UseOpIdx,
|
|
|
|
std::vector<FoldCandidate> &FoldList,
|
2015-09-10 09:06:06 +08:00
|
|
|
SmallVectorImpl<MachineInstr *> &CopiesToReplace,
|
2015-08-29 07:45:19 +08:00
|
|
|
const SIInstrInfo *TII, const SIRegisterInfo &TRI,
|
|
|
|
MachineRegisterInfo &MRI) {
|
|
|
|
const MachineOperand &UseOp = UseMI->getOperand(UseOpIdx);
|
|
|
|
|
|
|
|
// FIXME: Fold operands with subregs.
|
|
|
|
if (UseOp.isReg() && ((UseOp.getSubReg() && OpToFold.isReg()) ||
|
|
|
|
UseOp.isImplicit())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FoldingImm = OpToFold.isImm();
|
|
|
|
APInt Imm;
|
|
|
|
|
|
|
|
if (FoldingImm) {
|
|
|
|
unsigned UseReg = UseOp.getReg();
|
|
|
|
const TargetRegisterClass *UseRC
|
|
|
|
= TargetRegisterInfo::isVirtualRegister(UseReg) ?
|
|
|
|
MRI.getRegClass(UseReg) :
|
|
|
|
TRI.getPhysRegClass(UseReg);
|
|
|
|
|
|
|
|
Imm = APInt(64, OpToFold.getImm());
|
|
|
|
|
2015-08-29 09:58:21 +08:00
|
|
|
const MCInstrDesc &FoldDesc = TII->get(OpToFold.getParent()->getOpcode());
|
|
|
|
const TargetRegisterClass *FoldRC =
|
|
|
|
TRI.getRegClass(FoldDesc.OpInfo[0].RegClass);
|
|
|
|
|
2015-08-29 07:45:19 +08:00
|
|
|
// Split 64-bit constants into 32-bits for folding.
|
2015-08-29 09:58:21 +08:00
|
|
|
if (FoldRC->getSize() == 8 && UseOp.getSubReg()) {
|
2015-08-29 07:45:19 +08:00
|
|
|
if (UseRC->getSize() != 8)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (UseOp.getSubReg() == AMDGPU::sub0) {
|
|
|
|
Imm = Imm.getLoBits(32);
|
|
|
|
} else {
|
|
|
|
assert(UseOp.getSubReg() == AMDGPU::sub1);
|
|
|
|
Imm = Imm.getHiBits(32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In order to fold immediates into copies, we need to change the
|
|
|
|
// copy to a MOV.
|
|
|
|
if (UseMI->getOpcode() == AMDGPU::COPY) {
|
|
|
|
unsigned DestReg = UseMI->getOperand(0).getReg();
|
|
|
|
const TargetRegisterClass *DestRC
|
|
|
|
= TargetRegisterInfo::isVirtualRegister(DestReg) ?
|
|
|
|
MRI.getRegClass(DestReg) :
|
|
|
|
TRI.getPhysRegClass(DestReg);
|
|
|
|
|
|
|
|
unsigned MovOp = TII->getMovOpcode(DestRC);
|
|
|
|
if (MovOp == AMDGPU::COPY)
|
|
|
|
return;
|
|
|
|
|
|
|
|
UseMI->setDesc(TII->get(MovOp));
|
2015-09-10 09:06:06 +08:00
|
|
|
CopiesToReplace.push_back(UseMI);
|
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.
|
|
|
|
if (UseMI->getOpcode() == AMDGPU::REG_SEQUENCE) {
|
|
|
|
unsigned RegSeqDstReg = UseMI->getOperand(0).getReg();
|
|
|
|
unsigned RegSeqDstSubReg = UseMI->getOperand(UseOpIdx + 1).getImm();
|
|
|
|
|
|
|
|
for (MachineRegisterInfo::use_iterator
|
|
|
|
RSUse = MRI.use_begin(RegSeqDstReg),
|
|
|
|
RSE = MRI.use_end(); RSUse != RSE; ++RSUse) {
|
|
|
|
|
|
|
|
MachineInstr *RSUseMI = RSUse->getParent();
|
|
|
|
if (RSUse->getSubReg() != RegSeqDstSubReg)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
foldOperand(OpToFold, RSUseMI, RSUse.getOperandNo(), FoldList,
|
2015-09-10 09:06:06 +08:00
|
|
|
CopiesToReplace, TII, TRI, MRI);
|
2015-09-09 23:43:26 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-29 07:45:19 +08:00
|
|
|
const MCInstrDesc &UseDesc = UseMI->getDesc();
|
|
|
|
|
|
|
|
// Don't fold into target independent nodes. Target independent opcodes
|
|
|
|
// don't have defined register classes.
|
|
|
|
if (UseDesc.isVariadic() ||
|
|
|
|
UseDesc.OpInfo[UseOpIdx].RegClass == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (FoldingImm) {
|
|
|
|
MachineOperand ImmOp = MachineOperand::CreateImm(Imm.getSExtValue());
|
|
|
|
tryAddToFoldList(FoldList, UseMI, UseOpIdx, &ImmOp, TII);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-22 06:06:37 +08:00
|
|
|
bool SIFoldOperands::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
MachineRegisterInfo &MRI = MF.getRegInfo();
|
|
|
|
const SIInstrInfo *TII =
|
|
|
|
static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
|
|
|
|
const SIRegisterInfo &TRI = TII->getRegisterInfo();
|
|
|
|
|
|
|
|
for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
|
|
|
|
BI != BE; ++BI) {
|
|
|
|
|
|
|
|
MachineBasicBlock &MBB = *BI;
|
|
|
|
MachineBasicBlock::iterator I, Next;
|
|
|
|
for (I = MBB.begin(); I != MBB.end(); I = Next) {
|
|
|
|
Next = std::next(I);
|
|
|
|
MachineInstr &MI = *I;
|
|
|
|
|
|
|
|
if (!isSafeToFold(MI.getOpcode()))
|
|
|
|
continue;
|
|
|
|
|
2015-02-14 03:05:03 +08:00
|
|
|
unsigned OpSize = TII->getOpSize(MI, 1);
|
2014-11-22 06:06:37 +08:00
|
|
|
MachineOperand &OpToFold = MI.getOperand(1);
|
2015-01-14 06:59:41 +08:00
|
|
|
bool FoldingImm = OpToFold.isImm();
|
2015-01-08 06:18:27 +08:00
|
|
|
|
2015-01-08 06:44:19 +08:00
|
|
|
// FIXME: We could also be folding things like FrameIndexes and
|
|
|
|
// TargetIndexes.
|
|
|
|
if (!FoldingImm && !OpToFold.isReg())
|
|
|
|
continue;
|
|
|
|
|
2015-02-01 07:37:27 +08:00
|
|
|
// Folding immediates with more than one use will increase program size.
|
2015-01-08 06:18:27 +08:00
|
|
|
// FIXME: This will also reduce register usage, which may be better
|
|
|
|
// in some cases. A better heuristic is needed.
|
2015-02-14 03:05:03 +08:00
|
|
|
if (FoldingImm && !TII->isInlineConstant(OpToFold, OpSize) &&
|
2015-01-08 06:18:27 +08:00
|
|
|
!MRI.hasOneUse(MI.getOperand(0).getReg()))
|
|
|
|
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;
|
|
|
|
|
2015-09-10 09:06:06 +08:00
|
|
|
// 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;
|
|
|
|
|
2015-01-08 01:42:16 +08:00
|
|
|
std::vector<FoldCandidate> FoldList;
|
2014-11-22 06:06:37 +08:00
|
|
|
for (MachineRegisterInfo::use_iterator
|
|
|
|
Use = MRI.use_begin(MI.getOperand(0).getReg()), E = MRI.use_end();
|
|
|
|
Use != E; ++Use) {
|
|
|
|
|
|
|
|
MachineInstr *UseMI = Use->getParent();
|
|
|
|
|
2015-08-29 07:45:19 +08:00
|
|
|
foldOperand(OpToFold, UseMI, Use.getOperandNo(), FoldList,
|
2015-09-10 09:06:06 +08:00
|
|
|
CopiesToReplace, TII, TRI, MRI);
|
2014-11-22 06:06:37 +08:00
|
|
|
}
|
|
|
|
|
2015-09-10 09:06:06 +08:00
|
|
|
// Make sure we add EXEC uses to any new v_mov instructions created.
|
|
|
|
for (MachineInstr *Copy : CopiesToReplace)
|
|
|
|
Copy->addImplicitDefUseOperands(MF);
|
|
|
|
|
2015-01-08 01:42:16 +08:00
|
|
|
for (FoldCandidate &Fold : FoldList) {
|
|
|
|
if (updateOperand(Fold, TRI)) {
|
2014-11-22 06:06:37 +08:00
|
|
|
// Clear kill flags.
|
2015-01-08 01:42:16 +08:00
|
|
|
if (!Fold.isImm()) {
|
|
|
|
assert(Fold.OpToFold && Fold.OpToFold->isReg());
|
2015-10-22 06:37:50 +08:00
|
|
|
// FIXME: Probably shouldn't bother trying to fold if not an
|
|
|
|
// SGPR. PeepholeOptimizer can eliminate redundant VGPR->VGPR
|
|
|
|
// copies.
|
|
|
|
MRI.clearKillFlags(Fold.OpToFold->getReg());
|
2015-01-08 01:42:16 +08:00
|
|
|
}
|
2014-11-22 06:06:37 +08:00
|
|
|
DEBUG(dbgs() << "Folded source from " << MI << " into OpNo " <<
|
2015-01-08 01:42:16 +08:00
|
|
|
Fold.UseOpNo << " of " << *Fold.UseMI << '\n');
|
2014-11-22 06:06:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|