LLVM 18.0.0git
Go to the documentation of this file.
1//===-- lib/CodeGen/GlobalISel/Combiner.cpp -------------------------------===//
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
9// This file constains common code to combine machine functions at generic
10// level.
15#include "llvm/ADT/SetVector.h"
24#include "llvm/Support/Debug.h"
26#define DEBUG_TYPE "gi-combiner"
28using namespace llvm;
30namespace llvm {
32 "GlobalISel Combiner",
33 "Control the rules which are enabled. These options all take a comma "
34 "separated list of rules to disable and may be specified by number "
35 "or number range (e.g. 1-10)."
36#ifndef NDEBUG
37 " They may also be specified by name."
40} // end namespace llvm
42/// This class acts as the glue the joins the CombinerHelper to the overall
43/// Combine algorithm. The CombinerHelper is intended to report the
44/// modifications it makes to the MIR to the GISelChangeObserver and the
45/// observer subclass will act on these events. In this case, instruction
46/// erasure will cancel any future visits to the erased instruction and
47/// instruction creation will schedule that instruction for a future visit.
48/// Other Combiner implementations may require more complex behaviour from
49/// their GISelChangeObserver subclass.
52 WorkListTy &WorkList;
53 /// The instructions that have been created but we want to report once they
54 /// have their operands. This is only maintained if debug output is requested.
55#ifndef NDEBUG
60 WorkListMaintainer(WorkListTy &WorkList) : WorkList(WorkList) {}
61 virtual ~WorkListMaintainer() = default;
63 void erasingInstr(MachineInstr &MI) override {
64 LLVM_DEBUG(dbgs() << "Erasing: " << MI << "\n");
65 WorkList.remove(&MI);
66 }
67 void createdInstr(MachineInstr &MI) override {
68 LLVM_DEBUG(dbgs() << "Creating: " << MI << "\n");
69 WorkList.insert(&MI);
70 LLVM_DEBUG(CreatedInstrs.insert(&MI));
71 }
72 void changingInstr(MachineInstr &MI) override {
73 LLVM_DEBUG(dbgs() << "Changing: " << MI << "\n");
74 WorkList.insert(&MI);
75 }
76 void changedInstr(MachineInstr &MI) override {
77 LLVM_DEBUG(dbgs() << "Changed: " << MI << "\n");
78 WorkList.insert(&MI);
79 }
82 LLVM_DEBUG(for (const auto *MI
83 : CreatedInstrs) {
84 dbgs() << "Created: ";
85 MI->print(dbgs());
86 });
87 LLVM_DEBUG(CreatedInstrs.clear());
88 }
92 const TargetPassConfig *TPC, GISelKnownBits *KB,
93 GISelCSEInfo *CSEInfo)
94 : Builder(CSEInfo ? std::make_unique<CSEMIRBuilder>()
95 : std::make_unique<MachineIRBuilder>()),
96 WLObserver(std::make_unique<WorkListMaintainer>(WorkList)),
97 ObserverWrapper(std::make_unique<GISelObserverWrapper>()), CInfo(CInfo),
98 Observer(*ObserverWrapper), B(*Builder), MF(MF), MRI(MF.getRegInfo()),
99 KB(KB), TPC(TPC), CSEInfo(CSEInfo) {
100 (void)this->TPC; // FIXME: Remove when used.
102 // Setup builder.
103 B.setMF(MF);
104 if (CSEInfo)
107 // Setup observer.
108 ObserverWrapper->addObserver(WLObserver.get());
109 if (CSEInfo)
110 ObserverWrapper->addObserver(CSEInfo);
112 B.setChangeObserver(*ObserverWrapper);
115Combiner::~Combiner() = default;
118 // If the ISel pipeline failed, do not bother running this pass.
119 // FIXME: Should this be here or in individual combiner passes.
122 return false;
124 // We can't call this in the constructor because the derived class is
125 // uninitialized at that time.
126 if (!HasSetupMF) {
127 HasSetupMF = true;
128 setupMF(MF, KB);
129 }
131 LLVM_DEBUG(dbgs() << "Generic MI Combiner for: " << MF.getName() << '\n');
133 MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
135 bool MFChanged = false;
136 bool Changed;
138 do {
139 WorkList.clear();
141 // Collect all instructions. Do a post order traversal for basic blocks and
142 // insert with list bottom up, so while we pop_back_val, we'll traverse top
143 // down RPOT.
144 Changed = false;
146 RAIIDelegateInstaller DelInstall(MF, ObserverWrapper.get());
148 for (MachineInstr &CurMI :
150 // Erase dead insts before even adding to the list.
151 if (isTriviallyDead(CurMI, MRI)) {
152 LLVM_DEBUG(dbgs() << CurMI << "Is dead; erasing.\n");
154 CurMI.eraseFromParent();
155 continue;
156 }
157 WorkList.deferred_insert(&CurMI);
158 }
159 }
160 WorkList.finalize();
161 // Main Loop. Process the instructions here.
162 while (!WorkList.empty()) {
163 MachineInstr *CurrInst = WorkList.pop_back_val();
164 LLVM_DEBUG(dbgs() << "\nTry combining " << *CurrInst;);
165 Changed |= tryCombineAll(*CurrInst);
166 WLObserver->reportFullyCreatedInstrs();
167 }
168 MFChanged |= Changed;
169 } while (Changed);
171#ifndef NDEBUG
172 if (CSEInfo) {
173 if (auto E = CSEInfo->verify()) {
174 errs() << E << '\n';
175 assert(false && "CSEInfo is not consistent. Likely missing calls to "
176 "observer on mutations.");
177 }
178 }
180 return MFChanged;
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
assume Assume Builder
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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.
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This contains common code to allow clients to notify changes to machine instr.
IRTranslator LLVM IR MI
This file declares the MachineIRBuilder class.
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
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 class acts as the glue the joins the CombinerHelper to the overall Combine algorithm.
Definition: Combiner.cpp:50
void changingInstr(MachineInstr &MI) override
This instruction is about to be mutated in some way.
Definition: Combiner.cpp:72
void changedInstr(MachineInstr &MI) override
This instruction was mutated in some way.
Definition: Combiner.cpp:76
void erasingInstr(MachineInstr &MI) override
An instruction is about to be erased.
Definition: Combiner.cpp:63
virtual ~WorkListMaintainer()=default
void createdInstr(MachineInstr &MI) override
An instruction has been created and inserted into the function.
Definition: Combiner.cpp:67
WorkListMaintainer(WorkListTy &WorkList)
Definition: Combiner.cpp:60
Defines a builder that does CSE of MachineInstructions using GISelCSEInfo.
Definition: CSEMIRBuilder.h:32
GISelCSEInfo * CSEInfo
Definition: Combiner.h:72
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...
Definition: Combiner.cpp:91
bool combineMachineInstrs()
Definition: Combiner.cpp:117
MachineRegisterInfo & MRI
Definition: Combiner.h:68
virtual ~Combiner()
MachineIRBuilder & B
Definition: Combiner.h:66
GISelKnownBits * KB
Definition: Combiner.h:69
MachineFunction & MF
Definition: Combiner.h:67
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.
The CSE Analysis object.
Definition: CSEInfo.h:69
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.
Definition: GISelWorkList.h:74
MachineInstr * pop_back_val()
void deferred_insert(MachineInstr *I)
Definition: GISelWorkList.h:50
bool empty() const
Definition: GISelWorkList.h:38
void remove(const MachineInstr *I)
Remove I from the worklist if it exists.
Definition: GISelWorkList.h:83
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.
Definition: MachineInstr.h:68
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:130
A simple RAII based Delegate installer.
A vector that has set insertion semantics.
Definition: SetVector.h:57
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
Target-Independent Code Generator Pass Configuration Options.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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...
Definition: Utils.cpp:1394
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...
Definition: STLExtras.h:666
iterator_range< po_iterator< T > > post_order(const T &G)
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:429
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
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...
Definition: Utils.cpp:219
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define MORE()
Definition: regcomp.c:252
#define NDEBUG
Definition: regutils.h:48