39#define DEBUG_TYPE "detect-dead-lanes"
44 unsigned NumVirtRegs =
MRI->getNumVirtRegs();
45 VRegInfos = std::unique_ptr<VRegInfo[]>(
new VRegInfo[NumVirtRegs]);
46 WorklistMembers.
resize(NumVirtRegs);
47 DefinedByCopy.
resize(NumVirtRegs);
56 switch (
MI.getOpcode()) {
57 case TargetOpcode::COPY:
58 case TargetOpcode::PHI:
59 case TargetOpcode::INSERT_SUBREG:
60 case TargetOpcode::REG_SEQUENCE:
61 case TargetOpcode::EXTRACT_SUBREG:
80 unsigned DstSubIdx = 0;
81 switch (
MI.getOpcode()) {
82 case TargetOpcode::INSERT_SUBREG:
84 DstSubIdx =
MI.getOperand(3).getImm();
86 case TargetOpcode::REG_SEQUENCE: {
88 DstSubIdx =
MI.getOperand(OpNum+1).getImm();
91 case TargetOpcode::EXTRACT_SUBREG: {
92 unsigned SubReg =
MI.getOperand(2).getImm();
93 SrcSubIdx =
TRI.composeSubRegIndices(
SubReg, SrcSubIdx);
98 if (SrcSubIdx && DstSubIdx)
99 return !
TRI.getCommonSuperRegClass(SrcRC, SrcSubIdx, DstRC, DstSubIdx, PreA,
102 return !
TRI.getMatchingSuperRegClass(SrcRC, DstRC, SrcSubIdx);
104 return !
TRI.getMatchingSuperRegClass(DstRC, SrcRC, DstSubIdx);
105 return !
TRI.getCommonSubClass(SrcRC, DstRC);
108void DeadLaneDetector::addUsedLanesOnOperand(
const MachineOperand &MO,
125 if ((UsedLanes & ~PrevUsedLanes).
none())
129 MORegInfo.
UsedLanes = PrevUsedLanes | UsedLanes;
130 if (DefinedByCopy.
test(MORegIdx))
131 PutInWorklist(MORegIdx);
134void DeadLaneDetector::transferUsedLanesStep(
const MachineInstr &
MI,
140 addUsedLanesOnOperand(MO, UsedOnMO);
152 switch (
MI.getOpcode()) {
153 case TargetOpcode::COPY:
154 case TargetOpcode::PHI:
156 case TargetOpcode::REG_SEQUENCE: {
158 unsigned SubIdx =
MI.getOperand(OpNum + 1).getImm();
161 case TargetOpcode::INSERT_SUBREG: {
162 unsigned SubIdx =
MI.getOperand(3).getImm();
173 MO1UsedLanes = UsedLanes & ~TRI->getSubRegIndexLaneMask(SubIdx);
180 case TargetOpcode::EXTRACT_SUBREG: {
182 unsigned SubIdx =
MI.getOperand(2).getImm();
197 if (
MI.getDesc().getNumDefs() != 1)
201 if (
MI.getOpcode() == TargetOpcode::PATCHPOINT)
208 if (!DefinedByCopy.
test(DefRegIdx))
216 VRegInfo &RegInfo = VRegInfos[DefRegIdx];
217 LaneBitmask PrevDefinedLanes = RegInfo.DefinedLanes;
219 if ((DefinedLanes & ~PrevDefinedLanes).
none())
222 RegInfo.DefinedLanes = PrevDefinedLanes | DefinedLanes;
223 PutInWorklist(DefRegIdx);
230 switch (
MI.getOpcode()) {
231 case TargetOpcode::REG_SEQUENCE: {
232 unsigned SubIdx =
MI.getOperand(OpNum + 1).getImm();
237 case TargetOpcode::INSERT_SUBREG: {
238 unsigned SubIdx =
MI.getOperand(3).getImm();
243 assert(OpNum == 1 &&
"INSERT_SUBREG must have two operands");
245 DefinedLanes &= ~TRI->getSubRegIndexLaneMask(SubIdx);
249 case TargetOpcode::EXTRACT_SUBREG: {
250 unsigned SubIdx =
MI.getOperand(2).getImm();
251 assert(OpNum == 1 &&
"EXTRACT_SUBREG must have one register operand only");
255 case TargetOpcode::COPY:
256 case TargetOpcode::PHI:
262 assert(Def.getSubReg() == 0 &&
263 "Should not have subregister defs in machine SSA phase");
268LaneBitmask DeadLaneDetector::determineInitialDefinedLanes(
unsigned Reg) {
280 DefinedByCopy.
set(RegIdx);
281 PutInWorklist(RegIdx);
317 MOSubReg, MODefinedLanes);
325 if (
DefMI.isImplicitDef() ||
Def.isDead())
329 "Should not have subregister defs in machine SSA phase");
333LaneBitmask DeadLaneDetector::determineInitialUsedLanes(
unsigned Reg) {
352 bool CrossCopy =
false;
383 StringRef getPassName()
const override {
return "Detect Dead Lanes"; }
396 std::pair<bool, bool>
404 bool *CrossCopy)
const;
412char DetectDeadLanes::ID = 0;
417bool DetectDeadLanes::isUndefRegAtInput(
419 unsigned SubReg = MO.getSubReg();
421 return (RegInfo.DefinedLanes & RegInfo.UsedLanes & Mask).none();
426 bool *CrossCopy)
const {
456 for (
unsigned RegIdx = 0; RegIdx < NumVirtRegs; ++RegIdx) {
461 Info.DefinedLanes = determineInitialDefinedLanes(Reg);
462 Info.UsedLanes = determineInitialUsedLanes(Reg);
466 while (!Worklist.empty()) {
467 unsigned RegIdx = Worklist.front();
468 Worklist.pop_front();
469 WorklistMembers.
reset(RegIdx);
476 transferUsedLanesStep(
MI,
Info.UsedLanes);
479 transferDefinedLanesStep(MO,
Info.DefinedLanes);
483 dbgs() <<
"Defined/Used lanes:\n";
484 for (
unsigned RegIdx = 0; RegIdx < NumVirtRegs; ++RegIdx) {
498 bool Changed =
false;
507 if (!Reg.isVirtual())
513 <<
"Marking operand '" << MO <<
"' as dead in " <<
MI);
518 bool CrossCopy =
false;
519 if (isUndefRegAtInput(MO,
RegInfo)) {
521 <<
"Marking operand '" << MO <<
"' as undef in " <<
MI);
524 }
else if (isUndefInput(DLD, MO, &CrossCopy)) {
526 <<
"Marking operand '" << MO <<
"' as undef in " <<
MI);
537 return std::make_pair(Changed, Again);
547 if (!
MRI->subRegLivenessEnabled()) {
552 TRI =
MRI->getTargetRegisterInfo();
556 bool Changed =
false;
561 std::tie(LocalChanged, Again) = modifySubRegisterOperandStatus(DLD, MF);
562 Changed |= LocalChanged;
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
Analysis containing CSE Info
static bool isCrossCopy(const MachineRegisterInfo &MRI, const MachineInstr &MI, const TargetRegisterClass *DstRC, const MachineOperand &MO)
static bool lowersToCopies(const MachineInstr &MI)
Returns true if MI will get lowered to a series of COPY instructions.
Analysis that tracks defined/used subregister lanes across COPY instructions and instructions that ge...
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
bool test(unsigned Idx) const
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
LaneBitmask transferUsedLanes(const MachineInstr &MI, LaneBitmask UsedLanes, const MachineOperand &MO) const
Given a mask UsedLanes used from the output of instruction MI determine which lanes are used from ope...
void computeSubRegisterLaneBitInfo()
Update the DefinedLanes and the UsedLanes for all virtual registers.
DeadLaneDetector(const MachineRegisterInfo *MRI, const TargetRegisterInfo *TRI)
bool isDefinedByCopy(unsigned RegIdx) const
LaneBitmask transferDefinedLanes(const MachineOperand &Def, unsigned OpNum, LaneBitmask DefinedLanes) const
Given a mask DefinedLanes of lanes defined at operand OpNum of COPY-like instruction,...
const VRegInfo & getVRegInfo(unsigned RegIdx) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
bool isImplicitDef() const
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
unsigned getOperandNo() const
Returns the index of this operand in the instruction that it belongs to.
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void setIsDead(bool Val=true)
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
void setIsUndef(bool Val=true)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
iterator_range< use_nodbg_iterator > use_nodbg_operands(Register Reg) const
def_iterator def_begin(Register RegNo) const
bool hasOneDef(Register RegNo) const
Return true if there is exactly one operand defining the specified register.
LaneBitmask getMaxLaneMaskForVReg(Register Reg) const
Returns a mask covering all bits that can appear in lane masks of subregisters of the virtual registe...
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
Wrapper class representing virtual and physical registers.
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
static unsigned virtReg2Index(Register Reg)
Convert a virtual register number to a 0-based index.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
StringRef - Represent a constant reference to a string, i.e.
const LaneBitmask LaneMask
const bool CoveredBySubRegs
Whether a combination of subregisters can cover every register in the class.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
LaneBitmask reverseComposeSubRegIndexLaneMask(unsigned IdxA, LaneBitmask LaneMask) const
Transform a lanemask given for a virtual register to the corresponding lanemask before using subregis...
LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const
Return a bitmask representing the parts of a register that are covered by SubIdx.
LaneBitmask composeSubRegIndexLaneMask(unsigned IdxA, LaneBitmask Mask) const
Transforms a LaneMask computed for one subregister to the lanemask that would have been computed when...
A Use represents the edge between a Value definition and its users.
unsigned getOperandNo() const
Return the operand # of this use in its User.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
NodeAddr< DefNode * > Def
This is an optimization pass for GlobalISel generic memory operations.
Printable PrintLaneMask(LaneBitmask LaneMask)
Create Printable object to print LaneBitmasks on a raw_ostream.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
char & DetectDeadLanesID
This pass adds dead/undef flags after analyzing subregister lanes.
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
Contains a bitmask of which lanes of a given virtual register are defined and which ones are actually...
static constexpr LaneBitmask getAll()
constexpr bool none() const
constexpr bool any() const
static constexpr LaneBitmask getNone()