LLVM 19.0.0git
SimpleLoopUnswitch.h
Go to the documentation of this file.
1//===- SimpleLoopUnswitch.h - Hoist loop-invariant control flow -*- C++ -*-===//
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#ifndef LLVM_TRANSFORMS_SCALAR_SIMPLELOOPUNSWITCH_H
10#define LLVM_TRANSFORMS_SCALAR_SIMPLELOOPUNSWITCH_H
11
14#include "llvm/IR/PassManager.h"
15
16namespace llvm {
17
18class LPMUpdater;
19class Loop;
20class StringRef;
21class raw_ostream;
22
23/// This pass transforms loops that contain branches or switches on loop-
24/// invariant conditions to have multiple loops. For example, it turns the left
25/// into the right code:
26///
27/// for (...) if (lic)
28/// A for (...)
29/// if (lic) A; B; C
30/// B else
31/// C for (...)
32/// A; C
33///
34/// This can increase the size of the code exponentially (doubling it every time
35/// a loop is unswitched) so we only unswitch if the resultant code will be
36/// smaller than a threshold.
37///
38/// This pass expects LICM to be run before it to hoist invariant conditions out
39/// of the loop, to make the unswitching opportunity obvious.
40///
41/// There is a taxonomy of unswitching that we use to classify different forms
42/// of this transformaiton:
43///
44/// - Trival unswitching: this is when the condition can be unswitched without
45/// cloning any code from inside the loop. A non-trivial unswitch requires
46/// code duplication.
47///
48/// - Full unswitching: this is when the branch or switch is completely moved
49/// from inside the loop to outside the loop. Partial unswitching removes the
50/// branch from the clone of the loop but must leave a (somewhat simplified)
51/// branch in the original loop. While theoretically partial unswitching can
52/// be done for switches, the requirements are extreme - we need the loop
53/// invariant input to the switch to be sufficient to collapse to a single
54/// successor in each clone.
55///
56/// This pass always does trivial, full unswitching for both branches and
57/// switches. For branches, it also always does trivial, partial unswitching.
58///
59/// If enabled (via the constructor's `NonTrivial` parameter), this pass will
60/// additionally do non-trivial, full unswitching for branches and switches, and
61/// will do non-trivial, partial unswitching for branches.
62///
63/// Because partial unswitching of switches is extremely unlikely to be possible
64/// in practice and significantly complicates the implementation, this pass does
65/// not currently implement that in any mode.
66class SimpleLoopUnswitchPass : public PassInfoMixin<SimpleLoopUnswitchPass> {
67 bool NonTrivial;
68 bool Trivial;
69
70public:
71 SimpleLoopUnswitchPass(bool NonTrivial = false, bool Trivial = true)
72 : NonTrivial(NonTrivial), Trivial(Trivial) {}
73
76
78 function_ref<StringRef(StringRef)> MapClassName2PassName);
79};
80
81} // end namespace llvm
82
83#endif // LLVM_TRANSFORMS_SCALAR_SIMPLELOOPUNSWITCH_H
This header provides classes for managing per-loop analyses.
This header defines various interfaces for pass management in LLVM.
raw_pwrite_stream & OS
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:44
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
This pass transforms loops that contain branches or switches on loop- invariant conditions to have mu...
SimpleLoopUnswitchPass(bool NonTrivial=false, bool Trivial=true)
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:91