27#define DEBUG_TYPE "gi-combiner"
31STATISTIC(NumOneIteration,
"Number of functions with one iteration");
32STATISTIC(NumTwoIterations,
"Number of functions with two iterations");
34 "Number of functions with three or more iterations");
38 "GlobalISel Combiner",
39 "Control the rules which are enabled. These options all take a comma "
40 "separated list of rules to disable and may be specified by number "
41 "or number range (e.g. 1-10)."
43 " They may also be specified by name."
62 static std::unique_ptr<WorkListMaintainer>
70 dbgs() <<
"Created: " << *
MI;
83template <CombinerInfo::ObserverLevel Lvl>
96 : WorkList(WorkList),
MRI(
MRI) {}
109 if constexpr (Lvl != Level::Basic) {
117 if constexpr (Lvl == Level::Basic)
132 if constexpr (Lvl != Level::Basic)
138 if constexpr (Lvl == Level::Basic)
150 if constexpr (Lvl == Level::Basic)
154 while (!DeferList.
empty()) {
159 if constexpr (Lvl >= Level::SinglePass)
166 while (!LostUses.
empty()) {
177 if constexpr (Lvl >= Level::SinglePass) {
189 for (
auto &
Use :
MI.explicit_uses()) {
190 if (!
Use.isReg() || !
Use.getReg().isVirtual())
197 for (
auto &Def :
MI.defs()) {
208std::unique_ptr<Combiner::WorkListMaintainer>
213 return std::make_unique<WorkListMaintainerImpl<Level::Basic>>(WorkList,
216 return std::make_unique<WorkListMaintainerImpl<Level::DCE>>(WorkList,
MRI);
217 case Level::SinglePass:
218 return std::make_unique<WorkListMaintainerImpl<Level::SinglePass>>(WorkList,
251 MI.eraseFromParent();
273 bool MFChanged =
false;
276 unsigned Iteration = 0;
279 LLVM_DEBUG(
dbgs() <<
"\n\nCombiner iteration #" << Iteration <<
'\n');
284 ObserverWrapper->clearObservers();
286 ObserverWrapper->addObserver(
CSEInfo);
302 if (EnableDCE && tryDCE(CurMI,
MRI))
310 ObserverWrapper->addObserver(WLObserver.get());
312 while (!WorkList.
empty()) {
316 LLVM_DEBUG(WLObserver->reportFullyCreatedInstrs());
317 Changed |= AppliedCombine;
319 WLObserver->appliedCombine();
321 MFChanged |= Changed;
324 LLVM_DEBUG(
dbgs() <<
"\nCombiner reached fixed-point after iteration #"
325 << Iteration <<
'\n');
332 dbgs() <<
"\nCombiner reached iteration limit after iteration #"
333 << Iteration <<
'\n');
340 else if (Iteration == 2)
343 ++NumThreeOrMoreIterations;
349 assert(
false &&
"CSEInfo is not consistent. Likely missing calls to "
350 "observer on mutations.");
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
Provides analysis for continuously CSEing during GISel passes.
This file implements a version of MachineIRBuilder which CSEs insts within a MachineBasicBlock.
Option class for Targets to specify which operations are combined how and when.
This contains the base class for all Combiners generated by TableGen.
This contains common code to allow clients to notify changes to machine instr.
This file declares the MachineIRBuilder class.
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file implements a set that has insertion order iteration characteristics.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
void appliedCombine() override
virtual ~WorkListMaintainerImpl()=default
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
void noteLostUses(MachineInstr &MI)
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
WorkListMaintainerImpl(WorkListTy &WorkList, MachineRegisterInfo &MRI)
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
void addUsersToWorkList(MachineInstr &MI)
This class acts as the glue that joins the CombinerHelper to the overall Combine algorithm.
virtual ~WorkListMaintainer()=default
void reportFullyCreatedInstrs()
virtual void appliedCombine()=0
SmallSetVector< const MachineInstr *, 32 > CreatedInstrs
The instructions that have been created but we want to report once they have their operands.
static std::unique_ptr< WorkListMaintainer > create(Level Lvl, WorkListTy &WorkList, MachineRegisterInfo &MRI)
Defines a builder that does CSE of MachineInstructions using GISelCSEInfo.
Combiner(MachineFunction &MF, CombinerInfo &CInfo, const TargetPassConfig *TPC, GISelKnownBits *KB, GISelCSEInfo *CSEInfo=nullptr)
If CSEInfo is not null, then the Combiner will use CSEInfo as the observer and also create a CSEMIRBu...
bool combineMachineInstrs()
MachineRegisterInfo & MRI
const TargetPassConfig * TPC
GISelChangeObserver & Observer
virtual bool tryCombineAll(MachineInstr &I) const =0
virtual void setupMF(MachineFunction &mf, GISelKnownBits *kb, CodeGenCoverage *covinfo=nullptr, ProfileSummaryInfo *psi=nullptr, BlockFrequencyInfo *bfi=nullptr)
Setup per-MF executor state.
Abstract class that contains various methods for clients to notify about changes.
Simple wrapper observer that takes several observers, and calls each one for each event.
void insert(MachineInstr *I)
Add the specified instruction to the worklist if it isn't already in it.
MachineInstr * pop_back_val()
void deferred_insert(MachineInstr *I)
void remove(const MachineInstr *I)
Remove I from the worklist if it exists.
bool hasProperty(Property P) const
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
const MachineFunctionProperties & getProperties() const
Get the function properties.
Helper class to build MachineInstr.
void setCSEInfo(GISelCSEInfo *Info)
void setMF(MachineFunction &MF)
void setChangeObserver(GISelChangeObserver &Observer)
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
use_instr_nodbg_iterator use_instr_nodbg_begin(Register RegNo) const
bool hasOneNonDBGUser(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug instruction using the specified regis...
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(Register Reg) const
Class to install both of the above.
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
bool remove(const value_type &X)
Remove an item from the set vector.
void clear()
Completely clear the SetVector.
bool empty() const
Determine if the SetVector is empty or not.
bool insert(const value_type &X)
Insert a new element into the SetVector.
value_type pop_back_val()
A SetVector that performs no allocations if smaller than a certain size.
Target-Independent Code Generator Pass Configuration Options.
A Use represents the edge between a Value definition and its users.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)
Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
iterator_range< po_iterator< T > > post_order(const T &G)
auto reverse(ContainerTy &&C)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
cl::OptionCategory GICombinerOptionCategory("GlobalISel Combiner", "Control the rules which are enabled. These options all take a comma " "separated list of rules to disable and may be specified by number " "or number range (e.g. 1-10)." " They may also be specified by name.")
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...
Implement std::hash so that hash_code can be used in STL containers.
unsigned MaxIterations
The maximum number of times the Combiner will iterate over the MachineFunction.
ObserverLevel ObserverLvl
Select how the Combiner acts on MIR changes.
bool EnableFullDCE
Whether dead code elimination is performed before each Combiner iteration.
@ DCE
Enables Observer-based detection of dead instructions.