LLVM 23.0.0git
WebAssemblyCFGSort.cpp
Go to the documentation of this file.
1//===-- WebAssemblyCFGSort.cpp - CFG Sorting ------------------------------===//
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/// This file implements a CFG sorting pass.
11///
12/// This pass reorders the blocks in a function to put them into topological
13/// order, ignoring loop backedges, and without any loop or exception being
14/// interrupted by a block not dominated by the its header, with special care
15/// to keep the order as similar as possible to the original order.
16///
17////===----------------------------------------------------------------------===//
18
19#include "WebAssembly.h"
28#include "llvm/CodeGen/Passes.h"
29#include "llvm/Support/Debug.h"
31using namespace llvm;
34
35#define DEBUG_TYPE "wasm-cfg-sort"
36
37// Option to disable EH pad first sorting. Only for testing unwind destination
38// mismatches in CFGStackify.
40 "wasm-disable-ehpad-sort", cl::ReallyHidden,
42 "WebAssembly: Disable EH pad-first sort order. Testing purpose only."),
43 cl::init(false));
44
45namespace {
46
47class WebAssemblyCFGSort final : public MachineFunctionPass {
48 StringRef getPassName() const override { return "WebAssembly CFG Sort"; }
49
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.setPreservesCFG();
52 AU.addRequired<MachineDominatorTreeWrapperPass>();
53 AU.addPreserved<MachineDominatorTreeWrapperPass>();
54 AU.addRequired<MachineLoopInfoWrapperPass>();
55 AU.addPreserved<MachineLoopInfoWrapperPass>();
56 AU.addRequired<WebAssemblyExceptionInfo>();
57 AU.addPreserved<WebAssemblyExceptionInfo>();
59 }
60
61 bool runOnMachineFunction(MachineFunction &MF) override;
62
63public:
64 static char ID; // Pass identification, replacement for typeid
65 WebAssemblyCFGSort() : MachineFunctionPass(ID) {}
66};
67} // end anonymous namespace
68
69char WebAssemblyCFGSort::ID = 0;
70INITIALIZE_PASS(WebAssemblyCFGSort, DEBUG_TYPE,
71 "Reorders blocks in topological order", false, false)
72
74 return new WebAssemblyCFGSort();
75}
76
78#ifndef NDEBUG
79 bool AnyBarrier = false;
80#endif
81 bool AllAnalyzable = true;
82 for (const MachineInstr &Term : MBB->terminators()) {
83#ifndef NDEBUG
84 AnyBarrier |= Term.isBarrier();
85#endif
86 AllAnalyzable &= Term.isBranch() && !Term.isIndirectBranch();
87 }
88 assert((AnyBarrier || AllAnalyzable) &&
89 "analyzeBranch needs to analyze any block with a fallthrough");
90
91 // Find the layout successor from the original block order.
92 MachineFunction *MF = MBB->getParent();
93 MachineBasicBlock *OriginalSuccessor =
94 unsigned(MBB->getNumber() + 1) < MF->getNumBlockIDs()
95 ? MF->getBlockNumbered(MBB->getNumber() + 1)
96 : nullptr;
97
98 if (AllAnalyzable)
99 MBB->updateTerminator(OriginalSuccessor);
100}
101
102namespace {
103// EH pads are selected first regardless of the block comparison order.
104// When only one of the BBs is an EH pad, we give a higher priority to it, to
105// prevent common mismatches between possibly throwing calls and ehpads they
106// unwind to, as in the example below:
107//
108// bb0:
109// call @foo // If this throws, unwind to bb2
110// bb1:
111// call @bar // If this throws, unwind to bb3
112// bb2 (ehpad):
113// handler_bb2
114// bb3 (ehpad):
115// handler_bb3
116// continuing code
117//
118// Because this pass tries to preserve the original BB order, this order will
119// not change. But this will result in this try-catch structure in CFGStackify,
120// resulting in a mismatch:
121// try
122// try
123// call @foo
124// call @bar // This should unwind to bb3, not bb2!
125// catch
126// handler_bb2
127// end
128// catch
129// handler_bb3
130// end
131// continuing code
132//
133// If we give a higher priority to an EH pad whenever it is ready in this
134// example, when both bb1 and bb2 are ready, we would pick up bb2 first.
135
136/// Sort blocks by their number.
137struct CompareBlockNumbers {
138 bool operator()(const MachineBasicBlock *A,
139 const MachineBasicBlock *B) const {
141 if (A->isEHPad() && !B->isEHPad())
142 return false;
143 if (!A->isEHPad() && B->isEHPad())
144 return true;
145 }
146
147 return A->getNumber() > B->getNumber();
148 }
149};
150/// Sort blocks by their number in the opposite order..
151struct CompareBlockNumbersBackwards {
152 bool operator()(const MachineBasicBlock *A,
153 const MachineBasicBlock *B) const {
155 if (A->isEHPad() && !B->isEHPad())
156 return false;
157 if (!A->isEHPad() && B->isEHPad())
158 return true;
159 }
160
161 return A->getNumber() < B->getNumber();
162 }
163};
164/// Bookkeeping for a region to help ensure that we don't mix blocks not
165/// dominated by the its header among its blocks.
166struct Entry {
167 const SortRegion *TheRegion;
168 unsigned NumBlocksLeft;
169
170 /// List of blocks not dominated by Loop's header that are deferred until
171 /// after all of Loop's blocks have been seen.
172 std::vector<MachineBasicBlock *> Deferred;
173
174 explicit Entry(const SortRegion *R)
175 : TheRegion(R), NumBlocksLeft(R->getNumBlocks()) {}
176};
177} // end anonymous namespace
178
179/// Sort the blocks, taking special care to make sure that regions are not
180/// interrupted by blocks not dominated by their header.
181/// TODO: There are many opportunities for improving the heuristics here.
182/// Explore them.
183static void sortBlocks(MachineFunction &MF, const MachineLoopInfo &MLI,
184 const WebAssemblyExceptionInfo &WEI,
186 // Remember original layout ordering, so we can update terminators after
187 // reordering to point to the original layout successor.
188 MF.RenumberBlocks();
189
190 // Prepare for a topological sort: Record the number of predecessors each
191 // block has, ignoring loop backedges.
192 SmallVector<unsigned, 16> NumPredsLeft(MF.getNumBlockIDs(), 0);
193 for (MachineBasicBlock &MBB : MF) {
194 unsigned N = MBB.pred_size();
195 if (MachineLoop *L = MLI.getLoopFor(&MBB))
196 if (L->getHeader() == &MBB)
197 for (const MachineBasicBlock *Pred : MBB.predecessors())
198 if (L->contains(Pred))
199 --N;
200 NumPredsLeft[MBB.getNumber()] = N;
201 }
202
203 // Topological sort the CFG, with additional constraints:
204 // - Between a region header and the last block in the region, there can be
205 // no blocks not dominated by its header.
206 // - It's desirable to preserve the original block order when possible.
207 // We use two ready lists; Preferred and Ready. Preferred has recently
208 // processed successors, to help preserve block sequences from the original
209 // order. Ready has the remaining ready blocks. EH blocks are picked first
210 // from both queues.
212 CompareBlockNumbers>
213 Preferred;
215 CompareBlockNumbersBackwards>
216 Ready;
217
218 SortRegionInfo SRI(MLI, WEI);
219 SmallVector<Entry, 4> Entries;
220 for (MachineBasicBlock *MBB = &MF.front();;) {
221 const SortRegion *R = SRI.getRegionFor(MBB);
222 if (R) {
223 // If MBB is a region header, add it to the active region list. We can't
224 // put any blocks that it doesn't dominate until we see the end of the
225 // region.
226 if (R->getHeader() == MBB)
227 Entries.push_back(Entry(R));
228 // For each active region the block is in, decrement the count. If MBB is
229 // the last block in an active region, take it off the list and pick up
230 // any blocks deferred because the header didn't dominate them.
231 for (Entry &E : Entries)
232 if (E.TheRegion->contains(MBB) && --E.NumBlocksLeft == 0)
233 for (auto *DeferredBlock : E.Deferred)
234 Ready.push(DeferredBlock);
235 while (!Entries.empty() && Entries.back().NumBlocksLeft == 0)
236 Entries.pop_back();
237 }
238 // The main topological sort logic.
239 for (MachineBasicBlock *Succ : MBB->successors()) {
240 // Ignore backedges.
241 if (MachineLoop *SuccL = MLI.getLoopFor(Succ))
242 if (SuccL->getHeader() == Succ && SuccL->contains(MBB))
243 continue;
244 // Decrement the predecessor count. If it's now zero, it's ready.
245 if (--NumPredsLeft[Succ->getNumber()] == 0)
246 Preferred.push(Succ);
247 }
248 // Determine the block to follow MBB. First try to find a preferred block,
249 // to preserve the original block order when possible.
250 MachineBasicBlock *Next = nullptr;
251 while (!Preferred.empty()) {
252 Next = Preferred.top();
253 Preferred.pop();
254 // If X isn't dominated by the top active region header, defer it until
255 // that region is done.
256 if (!Entries.empty() &&
257 !MDT.dominates(Entries.back().TheRegion->getHeader(), Next)) {
258 Entries.back().Deferred.push_back(Next);
259 Next = nullptr;
260 continue;
261 }
262 // If Next was originally ordered before MBB, and it isn't because it was
263 // loop-rotated above the header, it's not preferred.
264 if (Next->getNumber() < MBB->getNumber() &&
265 (WasmDisableEHPadSort || !Next->isEHPad()) &&
266 (!R || !R->contains(Next) ||
267 R->getHeader()->getNumber() < Next->getNumber())) {
268 Ready.push(Next);
269 Next = nullptr;
270 continue;
271 }
272 break;
273 }
274 // If we didn't find a suitable block in the Preferred list, check the
275 // general Ready list.
276 if (!Next) {
277 // If there are no more blocks to process, we're done.
278 if (Ready.empty()) {
280 break;
281 }
282 for (;;) {
283 Next = Ready.top();
284 Ready.pop();
285 // If Next isn't dominated by the top active region header, defer it
286 // until that region is done.
287 if (!Entries.empty() &&
288 !MDT.dominates(Entries.back().TheRegion->getHeader(), Next)) {
289 Entries.back().Deferred.push_back(Next);
290 continue;
291 }
292 break;
293 }
294 }
295 // Move the next block into place and iterate.
296 Next->moveAfter(MBB);
298 MBB = Next;
299 }
300 assert(Entries.empty() && "Active sort region list not finished");
301 MF.RenumberBlocks();
302
303#ifndef NDEBUG
304 for (auto &MBB : MF) {
305 assert(MBB.getNumber() >= 0 && "Renumbered blocks should be non-negative.");
306 const SortRegion *Region = SRI.getRegionFor(&MBB);
307
308 if (Region && &MBB == Region->getHeader()) {
309 // Region header.
310 if (Region->isLoop()) {
311 // Loop header. The loop predecessor should be sorted above, and the
312 // other predecessors should be backedges below.
313 for (auto *Pred : MBB.predecessors())
314 assert(
315 (Pred->getNumber() < MBB.getNumber() || Region->contains(Pred)) &&
316 "Loop header predecessors must be loop predecessors or "
317 "backedges");
318 } else {
319 // Exception header. All predecessors should be sorted above.
320 for (auto *Pred : MBB.predecessors())
321 assert(Pred->getNumber() < MBB.getNumber() &&
322 "Non-loop-header predecessors should be topologically sorted");
323 }
324 } else {
325 // Not a region header. All predecessors should be sorted above.
326 for (auto *Pred : MBB.predecessors())
327 assert(Pred->getNumber() < MBB.getNumber() &&
328 "Non-loop-header predecessors should be topologically sorted");
329 }
330 }
331
333 for (auto &MBB : MF) {
334 const SortRegion *Region = SRI.getRegionFor(&MBB);
335 if (Region)
336 Regions.insert(Region);
337 }
338
339 SmallVector<std::pair<int, int>, 8> RegionIntervals(Regions.size(), {-1, -1});
340
341 unsigned RegionIdx = 0;
342 for (auto *Region : Regions) {
343 assert(Region->getHeader() != &MF.front() &&
344 "The function entry block shouldn't actually be a region header");
345
346 auto *Header = Region->getHeader();
347 auto *Bottom = SRI.getBottom(Region);
348
349 assert(Header && "Regions must have a header");
350 assert(Bottom && "Regions must have a bottom");
351
352 std::pair<int, int> Interval = {Header->getNumber(), Bottom->getNumber()};
353 assert(Interval.first <= Interval.second &&
354 "Region bottoms must be sorted after region headers");
355
356 RegionIntervals[RegionIdx++] = Interval;
357
358 for (auto *MBB : Region->blocks()) {
359 assert(MBB->getNumber() >= Interval.first &&
360 MBB->getNumber() <= Interval.second &&
361 "All blocks within a region must have numbers within the region's "
362 "interval");
363 }
364 }
365
366 for (const auto &IntervalA : RegionIntervals) {
367 for (const auto &IntervalB : RegionIntervals) {
368 auto AContainsB = IntervalA.first <= IntervalB.first &&
369 IntervalA.second >= IntervalB.second;
370 auto BContainsA = IntervalB.first <= IntervalA.first &&
371 IntervalB.second >= IntervalA.second;
372 auto Disjoint = IntervalA.second < IntervalB.first ||
373 IntervalA.first > IntervalB.second;
374 assert((AContainsB || BContainsA || Disjoint) &&
375 "Regions must be fully contained within their parents and not "
376 "overlap their siblings");
377 }
378 }
379#endif
380}
381
382bool WebAssemblyCFGSort::runOnMachineFunction(MachineFunction &MF) {
383 LLVM_DEBUG(dbgs() << "********** CFG Sorting **********\n"
384 "********** Function: "
385 << MF.getName() << '\n');
386
387 const auto &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
388 const auto &WEI = getAnalysis<WebAssemblyExceptionInfo>();
389 auto &MDT = getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
390 // Liveness is not tracked for VALUE_STACK physreg.
392
393 // Sort the blocks, with contiguous sort regions.
394 sortBlocks(MF, MLI, WEI, MDT);
395
396 return true;
397}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define DEBUG_TYPE
std::pair< uint64_t, uint64_t > Interval
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file defines the PriorityQueue class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static void maybeUpdateTerminator(MachineBasicBlock *MBB)
static cl::opt< bool > WasmDisableEHPadSort("wasm-disable-ehpad-sort", cl::ReallyHidden, cl::desc("WebAssembly: Disable EH pad-first sort order. Testing purpose only."), cl::init(false))
This file implements WebAssemblyException information analysis.
This file implements regions used in CFGSort and CFGStackify.
This file contains the declaration of the WebAssembly-specific utility functions.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
const MachineBasicBlock & front() const
Representation of each machine instruction.
void invalidateLiveness()
invalidateLiveness - Indicates that register liveness is no longer being tracked accurately.
PriorityQueue - This class behaves like std::priority_queue and provides a few additional convenience...
block_range blocks()
Returns a range view of the basic blocks in the region.
Definition RegionInfo.h:620
bool contains(const BlockT *BB) const
Check if the region contains a BasicBlock.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
size_type size() const
Definition SmallSet.h:171
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
@ Entry
Definition COFF.h:862
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
bool sortBlocks(Function &F)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
FunctionPass * createWebAssemblyCFGSort()
#define N