35 #define DEBUG_TYPE "legalizer" 41 cl::desc(
"Should enable CSE in Legalizer"),
46 "Legalize the Machine IR a function's Machine IR",
false,
71 case TargetOpcode::G_TRUNC:
72 case TargetOpcode::G_ZEXT:
73 case TargetOpcode::G_ANYEXT:
74 case TargetOpcode::G_SEXT:
75 case TargetOpcode::G_MERGE_VALUES:
76 case TargetOpcode::G_UNMERGE_VALUES:
77 case TargetOpcode::G_CONCAT_VECTORS:
78 case TargetOpcode::G_BUILD_VECTOR:
79 case TargetOpcode::G_EXTRACT:
96 : InstList(Insts), ArtifactList(Arts) {}
113 createdOrChangedInstr(MI);
116 void printNewInstrs() {
118 for (
const auto *MI : NewMIs)
119 dbgs() <<
".. .. New MI: " << *MI;
138 createdOrChangedInstr(MI);
156 for (
auto *MBB : RPOT) {
174 LegalizerWorkListManager WorkListObserver(InstList, ArtifactList);
186 auto RemoveDeadInstFromLists = [&WrapperObserver](
MachineInstr *DeadMI) {
189 bool Changed =
false;
193 assert(RetryList.
empty() &&
"Expected no instructions in RetryList");
194 unsigned NumArtifacts = ArtifactList.
size();
195 while (!InstList.
empty()) {
198 "Expecting generic opcode");
214 LLVM_DEBUG(
dbgs() <<
".. Not legalized, moving to artifacts retry\n");
215 assert(NumArtifacts == 0 &&
216 "Artifacts are only expected in instruction list starting the " 217 "second iteration, but each iteration starting second must " 218 "start with an empty artifacts list");
224 return {Changed, &MI};
226 WorkListObserver.printNewInstrs();
231 if (!RetryList.
empty()) {
232 if (!ArtifactList.
empty()) {
233 while (!RetryList.
empty())
236 LLVM_DEBUG(
dbgs() <<
"No new artifacts created, not retrying!\n");
238 return {Changed, RetryList.
front()};
241 while (!ArtifactList.
empty()) {
244 "Expecting generic opcode");
247 RemoveDeadInstFromLists(&MI);
255 WorkListObserver.printNewInstrs();
256 for (
auto *DeadMI : DeadInstructions) {
258 RemoveDeadInstFromLists(DeadMI);
259 DeadMI->eraseFromParentAndMarkDBGValuesForRemoval();
268 LLVM_DEBUG(
dbgs() <<
".. Not combined, moving to instructions list\n");
272 }
while (!InstList.
empty());
274 return {Changed,
nullptr};
286 getAnalysis<GISelCSEAnalysisWrapperPass>().getCSEWrapper();
289 const size_t NumBlocks = MF.
size();
291 std::unique_ptr<MachineIRBuilder> MIRBuilder;
297 MIRBuilder = std::make_unique<CSEMIRBuilder>();
299 MIRBuilder->setCSEInfo(CSEInfo);
301 MIRBuilder = std::make_unique<MachineIRBuilder>();
304 if (EnableCSE && CSEInfo) {
314 "unable to legalize instruction", *Result.
FailedOn);
319 if (MF.
size() != NumBlocks) {
323 R <<
"inserting blocks is not supported yet";
A simple RAII based CSEInfo installer.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void deferred_insert(MachineInstr *I)
This class represents lattice values for constants.
void getSelectionDAGFallbackAnalysisUsage(AnalysisUsage &AU)
Modify analysis usage so it preserves passes required for the SelectionDAG fallback.
const MachineFunctionProperties & getProperties() const
Get the function properties.
void push_back(const T &Elt)
const MachineInstr * FailedOn
virtual bool isGISelCSEEnabled() const
Check whether continuous CSE should be enabled in GISel passes.
The actual analysis pass wrapper.
INITIALIZE_PASS_BEGIN(Legalizer, DEBUG_TYPE, "Legalize the Machine IR a function's Machine IR", false, false) INITIALIZE_PASS_END(Legalizer
void remove(const MachineInstr *I)
Remove I from the worklist if it exists.
virtual std::unique_ptr< CSEConfigBase > getCSEConfig() const
Returns the CSEConfig object to use for the current optimization level.
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
bool tryCombineInstruction(MachineInstr &MI, SmallVectorImpl< MachineInstr *> &DeadInsts, GISelObserverWrapper &WrapperObserver)
Try to combine away MI.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
MachineIRBuilder & MIRBuilder
Expose MIRBuilder so clients can set their own RecordInsertInstruction functions. ...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Target-Independent Code Generator Pass Configuration Options.
void eraseFromParentAndMarkDBGValuesForRemoval()
Unlink 'this' from the containing basic block and delete it.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
COFF::MachineTypes Machine
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
static MFResult legalizeMachineFunction(MachineFunction &MF, const LegalizerInfo &LI, ArrayRef< GISelChangeObserver *> AuxObservers, MachineIRBuilder &MIRBuilder)
virtual const LegalizerInfo * getLegalizerInfo() const
initializer< Ty > init(const Ty &Val)
Abstract class that contains various methods for clients to notify about changes. ...
unsigned const MachineRegisterInfo * MRI
LegalizeResult legalizeInstrStep(MachineInstr &MI)
Replace MI by a sequence of legal instructions that can implement the same operation.
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.
DISubprogram * getSubprogram() const
Get the attached subprogram.
This file implements a version of MachineIRBuilder which CSEs insts within a MachineBasicBlock.
Helper class to build MachineInstr.
Represent the analysis usage information of a pass.
void stopObservingChanges()
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Some kind of error has occurred and we could not legalize this instruction.
Legalize the Machine IR a function s Machine IR
Simple wrapper that does the following.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
LLVM_NODISCARD T pop_back_val()
MachineInstr * pop_back_val()
void insert(MachineInstr *I)
Add the specified instruction to the worklist if it isn't already in it.
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.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
bool isTriviallyDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
Check whether an instruction MI is dead: it only defines dead virtual registers, and doesn't have oth...
static cl::opt< bool > EnableCSEInLegalizer("enable-cse-in-legalizer", cl::desc("Should enable CSE in Legalizer"), cl::Optional, cl::init(false))
Representation of each machine instruction.
Instruction has been legalized and the MachineFunction changed.
void addObserver(GISelChangeObserver *O)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
LLVM_NODISCARD bool empty() const
static bool isArtifact(const MachineInstr &MI)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool hasProperty(Property P) const
bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel...
print Print MemDeps of function
Simple wrapper observer that takes several observers, and calls each one for each event...
void reportGISelFailure(MachineFunction &MF, const TargetPassConfig &TPC, MachineOptimizationRemarkEmitter &MORE, MachineOptimizationRemarkMissed &R)
Report an ISel error as a missed optimization remark to the LLVMContext's diagnostic stream...