26 #define DEBUG_TYPE "si-shrink-instructions" 29 "Number of 64-bit instruction reduced to 32-bit.");
31 "Number of literal constants folded into 32-bit instructions.");
49 StringRef getPassName()
const override {
return "SI Shrink Instructions"; }
60 "SI Shrink Instructions",
false,
false)
62 char SIShrinkInstructions::
ID = 0;
65 return new SIShrinkInstructions();
85 bool ConstantFolded =
false;
92 Src0.ChangeToImmediate(MovSrc.
getImm());
93 ConstantFolded =
true;
94 }
else if (MovSrc.
isFI()) {
96 Src0.ChangeToFrameIndex(MovSrc.
getIndex());
97 ConstantFolded =
true;
101 ConstantFolded =
true;
104 if (ConstantFolded) {
107 ++NumLiteralConstantsFolded;
116 if (TII->commuteInstruction(MI)) {
121 TII->commuteInstruction(MI);
160 int32_t &ReverseImm) {
164 ReverseImm = reverseBits<int32_t>(
static_cast<int32_t
>(Src.
getImm()));
165 return ReverseImm >= -16 && ReverseImm <= 64;
186 TII->commuteInstruction(MI,
false, 0, 1);
198 if (SOPKOpc == AMDGPU::S_CMPK_EQ_U32 || SOPKOpc == AMDGPU::S_CMPK_LG_U32) {
202 SOPKOpc = (SOPKOpc == AMDGPU::S_CMPK_EQ_U32) ?
203 AMDGPU::S_CMPK_EQ_I32 : AMDGPU::S_CMPK_LG_I32;
223 if (Info->MIMGEncoding != AMDGPU::MIMGEncGfx10NSA)
232 unsigned NewAddrDwords = Info->VAddrDwords;
235 if (Info->VAddrDwords == 2) {
236 RC = &AMDGPU::VReg_64RegClass;
237 }
else if (Info->VAddrDwords == 3) {
238 RC = &AMDGPU::VReg_96RegClass;
239 }
else if (Info->VAddrDwords == 4) {
240 RC = &AMDGPU::VReg_128RegClass;
241 }
else if (Info->VAddrDwords <= 8) {
242 RC = &AMDGPU::VReg_256RegClass;
245 RC = &AMDGPU::VReg_512RegClass;
249 unsigned VgprBase = 0;
251 bool IsKill = NewAddrDwords == Info->VAddrDwords;
252 for (
unsigned i = 0; i < Info->VAddrDwords; ++i) {
258 }
else if (VgprBase + i != Vgpr)
267 if (VgprBase + NewAddrDwords > 256)
277 if (TFEVal || LWEVal) {
285 "found more than one tied implicit operand when expecting only 1");
294 Info->VDataDwords, NewAddrDwords);
295 MI.
setDesc(TII->get(NewOpcode));
300 for (
unsigned i = 1; i < Info->VAddrDwords; ++i)
306 ToUntie - (Info->VAddrDwords - 1));
326 if (SrcImm->
isImm() &&
331 if (Opc == AMDGPU::S_AND_B32) {
334 Opc = AMDGPU::S_BITSET0_B32;
337 Opc = AMDGPU::S_ANDN2_B32;
339 }
else if (Opc == AMDGPU::S_OR_B32) {
342 Opc = AMDGPU::S_BITSET1_B32;
345 Opc = AMDGPU::S_ORN2_B32;
347 }
else if (Opc == AMDGPU::S_XOR_B32) {
350 Opc = AMDGPU::S_XNOR_B32;
356 if ((Opc == AMDGPU::S_ANDN2_B32 || Opc == AMDGPU::S_ORN2_B32) &&
358 if (!TII->commuteInstruction(MI,
false, 1, 2))
371 if (Opc == AMDGPU::S_BITSET0_B32 ||
372 Opc == AMDGPU::S_BITSET1_B32) {
398 if (TRI.regsOverlap(Reg, MO.getReg()))
401 LaneBitmask Overlap = TRI.getSubRegIndexLaneMask(SubReg) &
402 TRI.getSubRegIndexLaneMask(MO.getSubReg());
425 if (TRI.getRegSizeInBits(Reg, MRI) != 32) {
476 if (YTop.getSubReg() != Tsub)
480 if ((MovY.
getOpcode() != AMDGPU::V_MOV_B32_e32 &&
509 (
I->getOpcode() != AMDGPU::V_MOV_B32_e32 &&
510 I->getOpcode() != AMDGPU::COPY) ||
511 I->getOperand(0).getReg() != X ||
512 I->getOperand(0).getSubReg() != Xsub) {
522 LLVM_DEBUG(
dbgs() <<
"Matched v_swap_b32:\n" << MovT << *MovX << MovY);
524 for (
unsigned I = 0;
I <
Size; ++
I) {
529 TII->get(AMDGPU::V_SWAP_B32))
556 unsigned VCCReg = ST.
isWave32() ? AMDGPU::VCC_LO : AMDGPU::VCC;
558 std::vector<unsigned> I1Defs;
565 for (I = MBB.
begin(); I != MBB.
end(); I = Next) {
569 if (MI.
getOpcode() == AMDGPU::V_MOV_B32_e32) {
583 MI.
setDesc(TII->get(AMDGPU::V_BFREV_B32_e32));
592 if (
auto *NextMI =
matchSwap(MI, MRI, TII)) {
593 Next = NextMI->getIterator();
608 (*Next).getOpcode() == AMDGPU::S_NOP &&
609 (*Next).getNumOperands() == 1) {
619 if (Nop0 + Nop1 <= 8) {
632 if (MI.
getOpcode() == AMDGPU::S_ADD_I32 ||
639 if (TII->commuteInstruction(MI,
false, 1, 2))
654 unsigned Opc = (MI.
getOpcode() == AMDGPU::S_ADD_I32) ?
655 AMDGPU::S_ADDK_I32 : AMDGPU::S_MULK_I32;
670 if (MI.
getOpcode() == AMDGPU::S_MOV_B32) {
677 MI.
setDesc(TII->get(AMDGPU::S_MOVK_I32));
679 MI.
setDesc(TII->get(AMDGPU::S_BREV_B32));
688 if (MI.
getOpcode() == AMDGPU::S_AND_B32 ||
703 if (!TII->hasVALU32BitEncoding(MI.
getOpcode()))
706 if (!TII->canShrink(MI, MRI)) {
709 if (!MI.
isCommutable() || !TII->commuteInstruction(MI) ||
710 !TII->canShrink(MI, MRI))
716 if (!TII->hasVALU32BitEncoding(MI.
getOpcode()))
721 if (TII->isVOPC(Op32)) {
735 if (DstReg != VCCReg)
739 if (Op32 == AMDGPU::V_CNDMASK_B32_e32) {
743 TII->getNamedOperand(MI, AMDGPU::OpName::src2);
757 AMDGPU::OpName::sdst);
761 AMDGPU::OpName::src2);
766 if (SDst->
getReg() != VCCReg) {
774 if (Src2 && Src2->
getReg() != VCCReg) {
788 ++NumInstructionsShrunk;
unsigned getTargetFlags() const
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
unsigned getNumImplicitUses() const
Return the number of implicit uses this instruction has.
constexpr bool isUInt< 32 >(uint64_t x)
static bool isReverseInlineImm(const SIInstrInfo *TII, const MachineOperand &Src, int32_t &ReverseImm)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
bool use_nodbg_empty(unsigned RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register...
AMDGPU specific subclass of TargetSubtarget.
instr_iterator instr_end()
LLVM_READONLY const MIMGInfo * getMIMGInfo(unsigned Opc)
unsigned getNumImplicitDefs() const
Return the number of implicit defs this instruct has.
This class represents lattice values for constants.
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
static bool sopkIsZext(const MachineInstr &MI)
const MachineFunctionProperties & getProperties() const
Get the function properties.
unsigned getRegister(unsigned i) const
Return the specified register in the class.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
iterator_range< use_nodbg_iterator > use_nodbg_operands(unsigned Reg) const
Describe properties that are true of each instruction in the target description file.
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
void setIsUndef(bool Val=true)
unsigned getSubReg() const
const SIInstrInfo * getInstrInfo() const override
static bool instModifiesReg(const MachineInstr *MI, unsigned Reg, unsigned SubReg, const SIRegisterInfo &TRI)
constexpr bool isInt< 16 >(int64_t x)
unsigned const TargetRegisterInfo * TRI
bool isInlineConstant(const APInt &Imm) const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
const SIRegisterInfo & getRegisterInfo() const
bool isMoveImmediate(QueryType Type=IgnoreBundle) const
Return true if this instruction is a move immediate (including conditional moves) instruction...
LLVM_READONLY int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
const HexagonInstrInfo * TII
static TargetInstrInfo::RegSubRegPair getSubRegForIndex(unsigned Reg, unsigned Sub, unsigned I, const SIRegisterInfo &TRI, const MachineRegisterInfo &MRI)
unsigned getNumOperands() const
Retuns the total number of operands.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
static bool isKImmOrKUImmOperand(const SIInstrInfo *TII, const MachineOperand &Src, bool &IsUnsigned)
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
static bool instAccessReg(iterator_range< MachineInstr::const_mop_iterator > &&R, unsigned Reg, unsigned SubReg, const SIRegisterInfo &TRI)
unsigned getOpSize(uint16_t Opcode, unsigned OpNo) const
Return the size in bytes of the operand OpNo on the given.
bool isVGPR(const MachineRegisterInfo &MRI, unsigned Reg) const
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
STATISTIC(NumInstructionsShrunk, "Number of 64-bit instruction reduced to 32-bit.")
#define DEBUG_TYPE
The pass tries to use the 32-bit encoding for instructions when possible.
void untieRegOperand(unsigned OpIdx)
Break any tie involving OpIdx.
void setReg(Register Reg)
Change the register this operand corresponds to.
static bool isKUImmOperand(const SIInstrInfo *TII, const MachineOperand &Src)
static unsigned getSubRegFromChannel(unsigned Channel, unsigned NumRegs=1)
Analysis containing CSE Info
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
TargetInstrInfo::RegSubRegPair RegSubRegPair
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
unsigned const MachineRegisterInfo * MRI
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
static void shrinkScalarCompare(const SIInstrInfo *TII, MachineInstr &MI)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
bool isCompare(QueryType Type=IgnoreBundle) const
Return true if this instruction is a comparison.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const GlobalValue * getGlobal() const
Represent the analysis usage information of a pass.
LLVM_READONLY int getSOPKOp(uint16_t Opcode)
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
void setImm(int64_t immVal)
FunctionPass class - This class is used to implement most global optimizations.
Generation getGeneration() const
self_iterator getIterator()
constexpr Type getAsInteger() const
static bool isVOP2(const MachineInstr &MI)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
int getMIMGOpcode(unsigned BaseOpcode, unsigned MIMGEncoding, unsigned VDataDwords, unsigned VAddrDwords)
void setIsKill(bool Val=true)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static bool instReadsReg(const MachineInstr *MI, unsigned Reg, unsigned SubReg, const SIRegisterInfo &TRI)
Iterator for intrusive lists based on ilist_node.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
constexpr bool isInt< 32 >(int64_t x)
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
LLVM_READONLY int getVOPe32(uint16_t Opcode)
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineOperand class - Representation of each machine instruction operand.
void setRegAllocationHint(unsigned VReg, unsigned Type, unsigned PrefReg)
setRegAllocationHint - Specify a register allocation hint for the specified virtual register...
A pair composed of a register and a sub-register index.
static bool isKImmOperand(const SIInstrInfo *TII, const MachineOperand &Src)
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly. ...
void setPreservesCFG()
This function should be called by the pass, iff they do not:
MachineInstr * getUniqueVRegDef(unsigned Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
A range adaptor for a pair of iterators.
static MachineInstr * matchSwap(MachineInstr &MovT, MachineRegisterInfo &MRI, const SIInstrInfo *TII)
bool use_empty(unsigned RegNo) const
use_empty - Return true if there are no instructions using the specified register.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
static void copyExtraImplicitOps(MachineInstr &NewMI, MachineFunction &MF, const MachineInstr &MI)
Copy implicit register operands from specified instruction to this instruction that are not part of t...
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
bool hasInv2PiInlineImm() const
Interface definition for SIInstrInfo.
FunctionPass * createSIShrinkInstructionsPass()
bool hasOneUse(unsigned RegNo) const
hasOneUse - Return true if there is exactly one instruction using the specified register.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
static bool isVOPC(const MachineInstr &MI)
int64_t getOffset() const
Return the offset from the symbol in this operand.
constexpr bool any() const
void ChangeToRegister(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value...
void setSubReg(unsigned subReg)
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
constexpr bool isUInt< 16 >(uint64_t x)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasProperty(Property P) const
static bool shrinkScalarLogicOp(const GCNSubtarget &ST, MachineRegisterInfo &MRI, const SIInstrInfo *TII, MachineInstr &MI)
Attempt to shink AND/OR/XOR operations requiring non-inlineable literals.
bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi)
unsigned countTrailingOnes(T Value, ZeroBehavior ZB=ZB_Width)
Count the number of ones from the least significant bit to the first zero bit.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
unsigned getHWRegIndex(unsigned Reg) const
StringRef - Represent a constant reference to a string, i.e.
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
static bool isVOP1(const MachineInstr &MI)
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
bool isCommutable(QueryType Type=IgnoreBundle) const
Return true if this may be a 2- or 3-address instruction (of the form "X = op Y, Z, ..."), which produces the same result if Y and Z are exchanged.
static bool foldImmediates(MachineInstr &MI, const SIInstrInfo *TII, MachineRegisterInfo &MRI, bool TryToCommute=true)
This function checks MI for operands defined by a move immediate instruction and then folds the liter...
Wrapper class representing virtual and physical registers.
void tieOperands(unsigned DefIdx, unsigned UseIdx)
Add a tie between the register operands at DefIdx and UseIdx.