1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
//===-- lib/CodeGen/GlobalISel/Combiner.cpp -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file constains common code to combine machine functions at generic
// level.
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/GlobalISel/Combiner.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
#include "llvm/CodeGen/GlobalISel/CSEMIRBuilder.h"
#include "llvm/CodeGen/GlobalISel/CombinerInfo.h"
#include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
#include "llvm/CodeGen/GlobalISel/GISelWorkList.h"
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
#include "llvm/CodeGen/GlobalISel/Utils.h"
#include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
#include "llvm/Support/Debug.h"
#define DEBUG_TYPE "gi-combiner"
using namespace llvm;
STATISTIC(NumOneIteration, "Number of functions with one iteration");
STATISTIC(NumTwoIterations, "Number of functions with two iterations");
STATISTIC(NumThreeOrMoreIterations,
"Number of functions with three or more iterations");
namespace llvm {
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)."
#ifndef NDEBUG
" They may also be specified by name."
#endif
);
} // end namespace llvm
/// This class acts as the glue that joins the CombinerHelper to the overall
/// Combine algorithm. The CombinerHelper is intended to report the
/// modifications it makes to the MIR to the GISelChangeObserver and the
/// observer subclass will act on these events.
class Combiner::WorkListMaintainer : public GISelChangeObserver {
protected:
#ifndef NDEBUG
/// The instructions that have been created but we want to report once they
/// have their operands. This is only maintained if debug output is requested.
SmallSetVector<const MachineInstr *, 32> CreatedInstrs;
#endif
using Level = CombinerInfo::ObserverLevel;
public:
static std::unique_ptr<WorkListMaintainer>
create(Level Lvl, WorkListTy &WorkList, MachineRegisterInfo &MRI);
virtual ~WorkListMaintainer() = default;
void reportFullyCreatedInstrs() {
LLVM_DEBUG({
for (auto *MI : CreatedInstrs) {
dbgs() << "Created: " << *MI;
}
CreatedInstrs.clear();
});
}
virtual void reset() = 0;
virtual void appliedCombine() = 0;
};
/// A configurable WorkListMaintainer implementation.
/// The ObserverLevel determines how the WorkListMaintainer reacts to MIR
/// changes.
template <CombinerInfo::ObserverLevel Lvl>
class Combiner::WorkListMaintainerImpl : public Combiner::WorkListMaintainer {
WorkListTy &WorkList;
MachineRegisterInfo &MRI;
// Defer handling these instructions until the combine finishes.
SmallSetVector<MachineInstr *, 32> DeferList;
// Track VRegs that (might) have lost a use.
SmallSetVector<Register, 32> LostUses;
public:
WorkListMaintainerImpl(WorkListTy &WorkList, MachineRegisterInfo &MRI)
: WorkList(WorkList), MRI(MRI) {}
virtual ~WorkListMaintainerImpl() = default;
void reset() override {
DeferList.clear();
LostUses.clear();
}
void erasingInstr(MachineInstr &MI) override {
// MI will become dangling, remove it from all lists.
LLVM_DEBUG(dbgs() << "Erasing: " << MI; CreatedInstrs.remove(&MI));
WorkList.remove(&MI);
if constexpr (Lvl != Level::Basic) {
DeferList.remove(&MI);
noteLostUses(MI);
}
}
void createdInstr(MachineInstr &MI) override {
LLVM_DEBUG(dbgs() << "Creating: " << MI; CreatedInstrs.insert(&MI));
if constexpr (Lvl == Level::Basic)
WorkList.insert(&MI);
else
// Defer handling newly created instructions, because they don't have
// operands yet. We also insert them into the WorkList in reverse
// order so that they will be combined top down.
DeferList.insert(&MI);
}
void changingInstr(MachineInstr &MI) override {
LLVM_DEBUG(dbgs() << "Changing: " << MI);
// Some uses might get dropped when MI is changed.
// For now, overapproximate by assuming all uses will be dropped.
// TODO: Is a more precise heuristic or manual tracking of use count
// decrements worth it?
if constexpr (Lvl != Level::Basic)
noteLostUses(MI);
}
void changedInstr(MachineInstr &MI) override {
LLVM_DEBUG(dbgs() << "Changed: " << MI);
if constexpr (Lvl == Level::Basic)
WorkList.insert(&MI);
else
// Defer this for DCE
DeferList.insert(&MI);
}
// Only track changes during the combine and then walk the def/use-chains once
// the combine is finished, because:
// - instructions might have multiple defs during the combine.
// - use counts aren't accurate during the combine.
void appliedCombine() override {
if constexpr (Lvl == Level::Basic)
return;
// DCE deferred instructions and add them to the WorkList bottom up.
while (!DeferList.empty()) {
MachineInstr &MI = *DeferList.pop_back_val();
if (tryDCE(MI, MRI))
continue;
if constexpr (Lvl >= Level::SinglePass)
addUsersToWorkList(MI);
WorkList.insert(&MI);
}
// Handle instructions that have lost a user.
while (!LostUses.empty()) {
Register Use = LostUses.pop_back_val();
MachineInstr *UseMI = MRI.getVRegDef(Use);
if (!UseMI)
continue;
// If DCE succeeds, UseMI's uses are added back to LostUses by
// erasingInstr.
if (tryDCE(*UseMI, MRI))
continue;
if constexpr (Lvl >= Level::SinglePass) {
// OneUse checks are relatively common, so we might be able to combine
// the single remaining user of this Reg.
if (MRI.hasOneNonDBGUser(Use))
WorkList.insert(&*MRI.use_instr_nodbg_begin(Use));
WorkList.insert(UseMI);
}
}
}
void noteLostUses(MachineInstr &MI) {
for (auto &Use : MI.explicit_uses()) {
if (!Use.isReg() || !Use.getReg().isVirtual())
continue;
LostUses.insert(Use.getReg());
}
}
void addUsersToWorkList(MachineInstr &MI) {
for (auto &Def : MI.defs()) {
Register DefReg = Def.getReg();
if (!DefReg.isVirtual())
continue;
for (auto &UseMI : MRI.use_nodbg_instructions(DefReg)) {
WorkList.insert(&UseMI);
}
}
}
};
std::unique_ptr<Combiner::WorkListMaintainer>
Combiner::WorkListMaintainer::create(Level Lvl, WorkListTy &WorkList,
MachineRegisterInfo &MRI) {
switch (Lvl) {
case Level::Basic:
return std::make_unique<WorkListMaintainerImpl<Level::Basic>>(WorkList,
MRI);
case Level::DCE:
return std::make_unique<WorkListMaintainerImpl<Level::DCE>>(WorkList, MRI);
case Level::SinglePass:
return std::make_unique<WorkListMaintainerImpl<Level::SinglePass>>(WorkList,
MRI);
}
llvm_unreachable("Illegal ObserverLevel");
}
Combiner::Combiner(MachineFunction &MF, CombinerInfo &CInfo,
const TargetPassConfig *TPC, GISelValueTracking *VT,
GISelCSEInfo *CSEInfo)
: Builder(CSEInfo ? std::make_unique<CSEMIRBuilder>()
: std::make_unique<MachineIRBuilder>()),
WLObserver(WorkListMaintainer::create(CInfo.ObserverLvl, WorkList,
MF.getRegInfo())),
ObserverWrapper(std::make_unique<GISelObserverWrapper>()), CInfo(CInfo),
Observer(*ObserverWrapper), B(*Builder), MF(MF), MRI(MF.getRegInfo()),
VT(VT), TPC(TPC), CSEInfo(CSEInfo) {
(void)this->TPC; // FIXME: Remove when used.
// Setup builder.
B.setMF(MF);
if (CSEInfo)
B.setCSEInfo(CSEInfo);
B.setChangeObserver(*ObserverWrapper);
}
Combiner::~Combiner() = default;
bool Combiner::tryDCE(MachineInstr &MI, MachineRegisterInfo &MRI) {
if (!isTriviallyDead(MI, MRI))
return false;
LLVM_DEBUG(dbgs() << "Dead: " << MI);
llvm::salvageDebugInfo(MRI, MI);
MI.eraseFromParent();
return true;
}
bool Combiner::combineMachineInstrs() {
// If the ISel pipeline failed, do not bother running this pass.
// FIXME: Should this be here or in individual combiner passes.
if (MF.getProperties().hasFailedISel())
return false;
// We can't call this in the constructor because the derived class is
// uninitialized at that time.
if (!HasSetupMF) {
HasSetupMF = true;
setupMF(MF, VT);
}
LLVM_DEBUG(dbgs() << "Generic MI Combiner for: " << MF.getName() << '\n');
MachineOptimizationRemarkEmitter MORE(MF, /*MBFI=*/nullptr);
bool MFChanged = false;
bool Changed;
unsigned Iteration = 0;
while (true) {
++Iteration;
LLVM_DEBUG(dbgs() << "\n\nCombiner iteration #" << Iteration << '\n');
Changed = false;
WorkList.clear();
WLObserver->reset();
ObserverWrapper->clearObservers();
if (CSEInfo)
ObserverWrapper->addObserver(CSEInfo);
// If Observer-based DCE is enabled, perform full DCE only before the first
// iteration.
bool EnableDCE = CInfo.ObserverLvl >= CombinerInfo::ObserverLevel::DCE
? CInfo.EnableFullDCE && Iteration == 1
: CInfo.EnableFullDCE;
// Collect all instructions. Do a post order traversal for basic blocks and
// insert with list bottom up, so while we pop_back_val, we'll traverse top
// down RPOT.
RAIIMFObsDelInstaller DelInstall(MF, *ObserverWrapper);
for (MachineBasicBlock *MBB : post_order(&MF)) {
for (MachineInstr &CurMI :
llvm::make_early_inc_range(llvm::reverse(*MBB))) {
// Erase dead insts before even adding to the list.
if (EnableDCE && tryDCE(CurMI, MRI))
continue;
WorkList.deferred_insert(&CurMI);
}
}
WorkList.finalize();
// Only notify WLObserver during actual combines
ObserverWrapper->addObserver(WLObserver.get());
// Main Loop. Process the instructions here.
while (!WorkList.empty()) {
MachineInstr &CurrInst = *WorkList.pop_back_val();
LLVM_DEBUG(dbgs() << "\nTry combining " << CurrInst);
bool AppliedCombine = tryCombineAll(CurrInst);
LLVM_DEBUG(WLObserver->reportFullyCreatedInstrs());
Changed |= AppliedCombine;
if (AppliedCombine)
WLObserver->appliedCombine();
}
MFChanged |= Changed;
if (!Changed) {
LLVM_DEBUG(dbgs() << "\nCombiner reached fixed-point after iteration #"
<< Iteration << '\n');
break;
}
// Iterate until a fixed-point is reached if MaxIterations == 0,
// otherwise limit the number of iterations.
if (CInfo.MaxIterations && Iteration >= CInfo.MaxIterations) {
LLVM_DEBUG(
dbgs() << "\nCombiner reached iteration limit after iteration #"
<< Iteration << '\n');
break;
}
}
if (Iteration == 1)
++NumOneIteration;
else if (Iteration == 2)
++NumTwoIterations;
else
++NumThreeOrMoreIterations;
#ifndef NDEBUG
if (CSEInfo) {
if (auto E = CSEInfo->verify()) {
errs() << E << '\n';
assert(false && "CSEInfo is not consistent. Likely missing calls to "
"observer on mutations.");
}
}
#endif
return MFChanged;
}
|