LLVM 24.0.0git
LICM.h
Go to the documentation of this file.
1//===- LICM.h - Loop Invariant Code Motion Pass -------*- 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// This pass performs loop invariant code motion, attempting to remove as much
10// code from the body of a loop as possible. It does this by either hoisting
11// code into the preheader block, or by sinking code to the exit blocks if it is
12// safe. This pass also promotes must-aliased memory locations in the loop to
13// live in registers, thus hoisting and sinking "invariant" loads and stores.
14//
15// This pass uses alias analysis for two purposes:
16//
17// 1. Moving loop invariant loads and calls out of loops. If we can determine
18// that a load or call inside of a loop never aliases anything stored to,
19// we can hoist it or sink it like any other instruction.
20// 2. Scalar Promotion of Memory - If there is a store instruction inside of
21// the loop, we try to move the store to happen AFTER the loop instead of
22// inside of the loop. This can only happen if a few conditions are true:
23// A. The pointer stored through is loop invariant
24// B. There are no stores or loads in the loop which _may_ alias the
25// pointer. There are no calls in the loop which mod/ref the pointer.
26// If these conditions are true, we can promote the loads and stores in the
27// loop of the pointer to use a temporary alloca'd variable. We then use
28// the SSAUpdater to construct the appropriate SSA form for the value.
29//
30//===----------------------------------------------------------------------===//
31
32#ifndef LLVM_TRANSFORMS_SCALAR_LICM_H
33#define LLVM_TRANSFORMS_SCALAR_LICM_H
34
36#include "llvm/IR/PassManager.h"
37#include <optional>
38
39namespace llvm {
40
41class LPMUpdater;
42class Loop;
43class LoopNest;
44namespace cl {
46} // namespace cl
47
48/// Returns -licm-mssa-optimization-cap of LibraryOptions, or its global value
49/// if null.
50LLVM_ABI unsigned
51getLicmMssaOptCap(const cl::LibraryOptionsContext *LibraryOptions);
52/// Returns -licm-mssa-max-acc-promotion of LibraryOptions, or its global value
53/// if null.
55 const cl::LibraryOptionsContext *LibraryOptions);
56
58 // Unset means the -licm-mssa-* value of the loop's LLVMContext.
59 std::optional<unsigned> MssaOptCap;
60 std::optional<unsigned> MssaNoAccForPromotionCap;
62
64
70};
71
72/// Performs Loop Invariant Code Motion Pass.
73class LICMPass : public OptionalPassInfoMixin<LICMPass> {
74 LICMOptions Opts;
75
76public:
77 LICMPass(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap,
78 bool AllowSpeculation)
79 : LICMPass(LICMOptions(MssaOptCap, MssaNoAccForPromotionCap,
80 AllowSpeculation)) {}
81 LICMPass(LICMOptions Opts) : Opts(Opts) {}
82
85 LPMUpdater &U);
86
87 LLVM_ABI void
89 function_ref<StringRef(StringRef)> MapClassName2PassName);
90};
91
92/// Performs LoopNest Invariant Code Motion Pass.
93class LNICMPass : public OptionalPassInfoMixin<LNICMPass> {
94 LICMOptions Opts;
95
96public:
97 LNICMPass(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap,
98 bool AllowSpeculation)
99 : LNICMPass(LICMOptions(MssaOptCap, MssaNoAccForPromotionCap,
100 AllowSpeculation)) {}
101 LNICMPass(LICMOptions Opts) : Opts(Opts) {}
102
105 LPMUpdater &U);
106
107 LLVM_ABI void
109 function_ref<StringRef(StringRef)> MapClassName2PassName);
110};
111} // end namespace llvm
112
113#endif // LLVM_TRANSFORMS_SCALAR_LICM_H
#define LLVM_ABI
Definition Compiler.h:211
This header defines various interfaces for pass management in LLVM.
This header provides classes for managing per-loop analyses.
LICMPass(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap, bool AllowSpeculation)
Definition LICM.h:77
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition LICM.cpp:286
LICMPass(LICMOptions Opts)
Definition LICM.h:81
LLVM_ABI PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Definition LICM.cpp:265
LLVM_ABI PreservedAnalyses run(LoopNest &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Definition LICM.cpp:296
LNICMPass(LICMOptions Opts)
Definition LICM.h:101
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
Definition LICM.cpp:325
LNICMPass(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap, bool AllowSpeculation)
Definition LICM.h:97
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
This class represents a loop nest and can be used to query its properties.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:53
This namespace contains all of the command line option processing machinery.
Definition CodeGenData.h:33
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI unsigned getLicmMssaNoAccForPromotionCap(const cl::LibraryOptionsContext *LibraryOptions)
Returns -licm-mssa-max-acc-promotion of LibraryOptions, or its global value if null.
Definition LICM.cpp:125
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
LLVM_ABI unsigned getLicmMssaOptCap(const cl::LibraryOptionsContext *LibraryOptions)
Returns -licm-mssa-optimization-cap of LibraryOptions, or its global value if null.
Definition LICM.cpp:120
std::optional< unsigned > MssaNoAccForPromotionCap
Definition LICM.h:60
bool AllowSpeculation
Definition LICM.h:61
std::optional< unsigned > MssaOptCap
Definition LICM.h:59
LICMOptions(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap, bool AllowSpeculation)
Definition LICM.h:65
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
A CRTP mix-in for passes that can be skipped.