LLVM 23.0.0git
DetectDeadLanes.h
Go to the documentation of this file.
1//===- DetectDeadLanes.h - SubRegister Lane Usage Analysis --*- C++ -*-----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// Analysis that tracks defined/used subregister lanes across COPY instructions
11/// and instructions that get lowered to a COPY (PHI, REG_SEQUENCE,
12/// INSERT_SUBREG, EXTRACT_SUBREG).
13/// The information is used to detect dead definitions and the usage of
14/// (completely) undefined values and mark the operands as such.
15/// This pass is necessary because the dead/undef status is not obvious anymore
16/// when subregisters are involved.
17///
18/// Example:
19/// %0 = some definition
20/// %1 = IMPLICIT_DEF
21/// %2 = REG_SEQUENCE %0, sub0, %1, sub1
22/// %3 = EXTRACT_SUBREG %2, sub1
23/// = use %3
24/// The %0 definition is dead and %3 contains an undefined value.
25//
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_CODEGEN_DETECTDEADLANES_H
29#define LLVM_CODEGEN_DETECTDEADLANES_H
30
31#include "llvm/ADT/BitVector.h"
33#include "llvm/MC/LaneBitmask.h"
34#include <deque>
35
36namespace llvm {
37
38class MachineInstr;
39class MachineOperand;
41class Register;
43
45public:
46 /// Contains a bitmask of which lanes of a given virtual register are
47 /// defined and which ones are actually used.
52
54 const TargetRegisterInfo *TRI);
55
56 /// Update the \p DefinedLanes and the \p UsedLanes for all virtual registers.
58
59 const VRegInfo &getVRegInfo(unsigned RegIdx) const {
60 return VRegInfos[RegIdx];
61 }
62
63 bool isDefinedByCopy(unsigned RegIdx) const {
64 return DefinedByCopy.test(RegIdx);
65 }
66
67private:
68 /// Add used lane bits on the register used by operand \p MO. This translates
69 /// the bitmask based on the operands subregister, and puts the register into
70 /// the worklist if any new bits were added.
71 void addUsedLanesOnOperand(const MachineOperand &MO, LaneBitmask UsedLanes);
72
73 /// Given a bitmask \p UsedLanes for the used lanes on a def output of a
74 /// COPY-like instruction determine the lanes used on the use operands
75 /// and call addUsedLanesOnOperand() for them.
76 void transferUsedLanesStep(const MachineInstr &MI, LaneBitmask UsedLanes);
77
78 /// Given a use regiser operand \p Use and a mask of defined lanes, check
79 /// if the operand belongs to a lowersToCopies() instruction, transfer the
80 /// mask to the def and put the instruction into the worklist.
81 void transferDefinedLanesStep(const MachineOperand &Use,
82 LaneBitmask DefinedLanes);
83
84public:
85 /// Given a mask \p DefinedLanes of lanes defined at operand \p OpNum
86 /// of COPY-like instruction, determine which lanes are defined at the output
87 /// operand \p Def.
89 unsigned OpNum,
90 LaneBitmask DefinedLanes) const;
91
92 /// Given a mask \p UsedLanes used from the output of instruction \p MI
93 /// determine which lanes are used from operand \p MO of this instruction.
95 LaneBitmask UsedLanes,
96 const MachineOperand &MO) const;
97
98private:
99 LaneBitmask determineInitialDefinedLanes(Register Reg);
100 LaneBitmask determineInitialUsedLanes(Register Reg);
101
102 const MachineRegisterInfo *MRI;
103 const TargetRegisterInfo *TRI;
104
105 void PutInWorklist(unsigned RegIdx) {
106 if (WorklistMembers.test(RegIdx))
107 return;
108 WorklistMembers.set(RegIdx);
109 Worklist.push_back(RegIdx);
110 }
111
112 std::unique_ptr<VRegInfo[]> VRegInfos;
113 /// Worklist containing virtreg indexes.
114 std::deque<unsigned> Worklist;
115 BitVector WorklistMembers;
116 /// This bitvector is set for each vreg index where the vreg is defined
117 /// by an instruction where lowersToCopies()==true.
118 BitVector DefinedByCopy;
119};
120
121class DetectDeadLanesPass : public RequiredPassInfoMixin<DetectDeadLanesPass> {
122public:
125};
126
127} // end namespace llvm
128
129#endif // LLVM_CODEGEN_DETECTDEADLANES_H
This file implements the BitVector class.
#define LLVM_ABI
Definition Compiler.h:213
IRTranslator LLVM IR MI
A common definition of LaneBitmask for use in TableGen and CodeGen.
Register Reg
Register const TargetRegisterInfo * TRI
bool test(unsigned Idx) const
Returns true if bit Idx is set.
Definition BitVector.h:482
BitVector & set()
Set all bits in the bitvector.
Definition BitVector.h:366
LLVM_ABI 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...
LLVM_ABI DeadLaneDetector(const MachineRegisterInfo *MRI, const TargetRegisterInfo *TRI)
bool isDefinedByCopy(unsigned RegIdx) const
LLVM_ABI LaneBitmask transferDefinedLanes(const MachineOperand &Def, unsigned OpNum, LaneBitmask DefinedLanes) const
Given a mask DefinedLanes of lanes defined at operand OpNum of COPY-like instruction,...
LLVM_ABI void computeSubRegisterLaneBitInfo()
Update the DefinedLanes and the UsedLanes for all virtual registers.
const VRegInfo & getVRegInfo(unsigned RegIdx) const
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Wrapper class representing virtual and physical registers.
Definition Register.h:20
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
This is an optimization pass for GlobalISel generic memory operations.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
Contains a bitmask of which lanes of a given virtual register are defined and which ones are actually...
A CRTP mix-in for passes that should not be skipped.