Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:41

0001 //===- LICM.h - Loop Invariant Code Motion Pass -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This pass performs loop invariant code motion, attempting to remove as much
0010 // code from the body of a loop as possible.  It does this by either hoisting
0011 // code into the preheader block, or by sinking code to the exit blocks if it is
0012 // safe.  This pass also promotes must-aliased memory locations in the loop to
0013 // live in registers, thus hoisting and sinking "invariant" loads and stores.
0014 //
0015 // This pass uses alias analysis for two purposes:
0016 //
0017 //  1. Moving loop invariant loads and calls out of loops.  If we can determine
0018 //     that a load or call inside of a loop never aliases anything stored to,
0019 //     we can hoist it or sink it like any other instruction.
0020 //  2. Scalar Promotion of Memory - If there is a store instruction inside of
0021 //     the loop, we try to move the store to happen AFTER the loop instead of
0022 //     inside of the loop.  This can only happen if a few conditions are true:
0023 //       A. The pointer stored through is loop invariant
0024 //       B. There are no stores or loads in the loop which _may_ alias the
0025 //          pointer.  There are no calls in the loop which mod/ref the pointer.
0026 //     If these conditions are true, we can promote the loads and stores in the
0027 //     loop of the pointer to use a temporary alloca'd variable.  We then use
0028 //     the SSAUpdater to construct the appropriate SSA form for the value.
0029 //
0030 //===----------------------------------------------------------------------===//
0031 
0032 #ifndef LLVM_TRANSFORMS_SCALAR_LICM_H
0033 #define LLVM_TRANSFORMS_SCALAR_LICM_H
0034 
0035 #include "llvm/Analysis/LoopAnalysisManager.h"
0036 #include "llvm/IR/PassManager.h"
0037 #include "llvm/Support/CommandLine.h"
0038 
0039 namespace llvm {
0040 
0041 class LPMUpdater;
0042 class Loop;
0043 class LoopNest;
0044 
0045 extern cl::opt<unsigned> SetLicmMssaOptCap;
0046 extern cl::opt<unsigned> SetLicmMssaNoAccForPromotionCap;
0047 
0048 struct LICMOptions {
0049   unsigned MssaOptCap;
0050   unsigned MssaNoAccForPromotionCap;
0051   bool AllowSpeculation;
0052 
0053   LICMOptions()
0054       : MssaOptCap(SetLicmMssaOptCap),
0055         MssaNoAccForPromotionCap(SetLicmMssaNoAccForPromotionCap),
0056         AllowSpeculation(true) {}
0057 
0058   LICMOptions(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap,
0059               bool AllowSpeculation)
0060       : MssaOptCap(MssaOptCap),
0061         MssaNoAccForPromotionCap(MssaNoAccForPromotionCap),
0062         AllowSpeculation(AllowSpeculation) {}
0063 };
0064 
0065 /// Performs Loop Invariant Code Motion Pass.
0066 class LICMPass : public PassInfoMixin<LICMPass> {
0067   LICMOptions Opts;
0068 
0069 public:
0070   LICMPass(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap,
0071            bool AllowSpeculation)
0072       : LICMPass(LICMOptions(MssaOptCap, MssaNoAccForPromotionCap,
0073                              AllowSpeculation)) {}
0074   LICMPass(LICMOptions Opts) : Opts(Opts) {}
0075 
0076   PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
0077                         LoopStandardAnalysisResults &AR, LPMUpdater &U);
0078 
0079   void printPipeline(raw_ostream &OS,
0080                      function_ref<StringRef(StringRef)> MapClassName2PassName);
0081 };
0082 
0083 /// Performs LoopNest Invariant Code Motion Pass.
0084 class LNICMPass : public PassInfoMixin<LNICMPass> {
0085   LICMOptions Opts;
0086 
0087 public:
0088   LNICMPass(unsigned MssaOptCap, unsigned MssaNoAccForPromotionCap,
0089             bool AllowSpeculation)
0090       : LNICMPass(LICMOptions(MssaOptCap, MssaNoAccForPromotionCap,
0091                               AllowSpeculation)) {}
0092   LNICMPass(LICMOptions Opts) : Opts(Opts) {}
0093 
0094   PreservedAnalyses run(LoopNest &L, LoopAnalysisManager &AM,
0095                         LoopStandardAnalysisResults &AR, LPMUpdater &U);
0096 
0097   void printPipeline(raw_ostream &OS,
0098                      function_ref<StringRef(StringRef)> MapClassName2PassName);
0099 };
0100 } // end namespace llvm
0101 
0102 #endif // LLVM_TRANSFORMS_SCALAR_LICM_H