LLVM  16.0.0git
SCCP.cpp
Go to the documentation of this file.
1 //===-- SCCP.cpp ----------------------------------------------------------===//
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 file implements Interprocedural Sparse Conditional Constant Propagation.
10 //
11 //===----------------------------------------------------------------------===//
12 
15 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/InitializePasses.h"
20 #include "llvm/Transforms/IPO.h"
23 
24 using namespace llvm;
25 
27  const DataLayout &DL = M.getDataLayout();
28  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
29  auto GetTLI = [&FAM](Function &F) -> const TargetLibraryInfo & {
31  };
32  auto getAnalysis = [&FAM](Function &F) -> AnalysisResultsForFn {
34  return {
35  std::make_unique<PredicateInfo>(F, DT, FAM.getResult<AssumptionAnalysis>(F)),
37  nullptr};
38  };
39 
40  if (!runIPSCCP(M, DL, GetTLI, getAnalysis))
41  return PreservedAnalyses::all();
42 
47  return PA;
48 }
49 
50 namespace {
51 
52 //===--------------------------------------------------------------------===//
53 //
54 /// IPSCCP Class - This class implements interprocedural Sparse Conditional
55 /// Constant Propagation.
56 ///
57 class IPSCCPLegacyPass : public ModulePass {
58 public:
59  static char ID;
60 
61  IPSCCPLegacyPass() : ModulePass(ID) {
63  }
64 
65  bool runOnModule(Module &M) override {
66  if (skipModule(M))
67  return false;
68  const DataLayout &DL = M.getDataLayout();
69  auto GetTLI = [this](Function &F) -> const TargetLibraryInfo & {
70  return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
71  };
72  auto getAnalysis = [this](Function &F) -> AnalysisResultsForFn {
73  DominatorTree &DT =
74  this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
75  return {
76  std::make_unique<PredicateInfo>(
77  F, DT,
78  this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
79  F)),
80  nullptr, // We cannot preserve the LI, DT or PDT with the legacy pass
81  nullptr, // manager, so set them to nullptr.
82  nullptr};
83  };
84 
85  return runIPSCCP(M, DL, GetTLI, getAnalysis);
86  }
87 
88  void getAnalysisUsage(AnalysisUsage &AU) const override {
92  }
93 };
94 
95 } // end anonymous namespace
96 
97 char IPSCCPLegacyPass::ID = 0;
98 
99 INITIALIZE_PASS_BEGIN(IPSCCPLegacyPass, "ipsccp",
100  "Interprocedural Sparse Conditional Constant Propagation",
101  false, false)
105 INITIALIZE_PASS_END(IPSCCPLegacyPass, "ipsccp",
106  "Interprocedural Sparse Conditional Constant Propagation",
108 
109 // createIPSCCPPass - This is the public interface to this file.
110 ModulePass *llvm::createIPSCCPPass() { return new IPSCCPLegacyPass(); }
111 
113  ModuleAnalysisManager &AM) {
114  const DataLayout &DL = M.getDataLayout();
115  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
116  auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
118  };
119  auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
120  return FAM.getResult<TargetIRAnalysis>(F);
121  };
122  auto GetAC = [&FAM](Function &F) -> AssumptionCache & {
124  };
125  auto GetAnalysis = [&FAM](Function &F) -> AnalysisResultsForFn {
127  return {std::make_unique<PredicateInfo>(
131  };
132 
133  if (!runFunctionSpecialization(M, &FAM, DL, GetTLI, GetTTI, GetAC, GetAnalysis))
134  return PreservedAnalyses::all();
135 
140  return PA;
141 }
142 
143 namespace {
144 struct FunctionSpecializationLegacyPass : public ModulePass {
145  static char ID; // Pass identification, replacement for typeid
146  FunctionSpecializationLegacyPass() : ModulePass(ID) {}
147 
148  void getAnalysisUsage(AnalysisUsage &AU) const override {
153  }
154 
155  bool runOnModule(Module &M) override {
156  if (skipModule(M))
157  return false;
158 
159  const DataLayout &DL = M.getDataLayout();
160  auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
161  return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
162  };
163  auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
164  return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
165  };
166  auto GetAC = [this](Function &F) -> AssumptionCache & {
167  return this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
168  };
169 
170  auto GetAnalysis = [this](Function &F) -> AnalysisResultsForFn {
171  DominatorTree &DT =
172  this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
173  return {
174  std::make_unique<PredicateInfo>(
175  F, DT,
176  this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
177  F)),
178  nullptr, // We cannot preserve the LI, DT, or PDT with the legacy pass
179  nullptr, // manager, so set them to nullptr.
180  nullptr};
181  };
182  return runFunctionSpecialization(M, nullptr, DL, GetTLI, GetTTI, GetAC, GetAnalysis);
183  }
184 };
185 } // namespace
186 
188 
190  FunctionSpecializationLegacyPass, "function-specialization",
191  "Propagate constant arguments by specializing the function", false, false)
192 
197 INITIALIZE_PASS_END(FunctionSpecializationLegacyPass, "function-specialization",
198  "Propagate constant arguments by specializing the function",
199  false, false)
200 
202  return new FunctionSpecializationLegacyPass();
203 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
AssumptionCache.h
llvm::TargetIRAnalysis
Analysis pass providing the TargetTransformInfo.
Definition: TargetTransformInfo.h:2592
constant
we should consider alternate ways to model stack dependencies Lots of things could be done in WebAssemblyTargetTransformInfo cpp there are numerous optimization related hooks that can be overridden in WebAssemblyTargetLowering Instead of the OptimizeReturned which should consider preserving the returned attribute through to MachineInstrs and extending the MemIntrinsicResults pass to do this optimization on calls too That would also let the WebAssemblyPeephole pass clean up dead defs for such as it does for stores Consider implementing and or getMachineCombinerPatterns Find a clean way to fix the problem which leads to the Shrink Wrapping pass being run after the WebAssembly PEI pass When setting multiple variables to the same constant
Definition: README.txt:91
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
specialization
function specialization
Definition: SCCP.cpp:197
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:818
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:774
llvm::AnalysisResultsForFn
Helper struct for bundling up the analysis results per function for IPSCCP.
Definition: SCCPSolver.h:41
llvm::Function
Definition: Function.h:60
llvm::FunctionSpecializationPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: SCCP.cpp:112
function
function Propagate constant arguments by specializing the function
Definition: SCCP.cpp:198
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition: TargetTransformInfo.h:173
SCCP.h
llvm::AA::Interprocedural
@ Interprocedural
Definition: Attributor.h:160
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
FAM
FunctionAnalysisManager FAM
Definition: PassBuilderBindings.cpp:59
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
PostDominators.h
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(IPSCCPLegacyPass, "ipsccp", "Interprocedural Sparse Conditional Constant Propagation", false, false) INITIALIZE_PASS_END(IPSCCPLegacyPass
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
Propagation
Interprocedural Sparse Conditional Constant Propagation
Definition: SCCP.cpp:106
TargetLibraryInfo.h
false
Definition: StackSlotColoring.cpp:141
llvm::IPSCCPPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: SCCP.cpp:26
llvm::runFunctionSpecialization
bool runFunctionSpecialization(Module &M, FunctionAnalysisManager *FAM, const DataLayout &DL, std::function< TargetLibraryInfo &(Function &)> GetTLI, std::function< TargetTransformInfo &(Function &)> GetTTI, std::function< AssumptionCache &(Function &)> GetAC, function_ref< AnalysisResultsForFn(Function &)> GetAnalysis)
Definition: FunctionSpecialization.cpp:827
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:306
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
LoopInfo.h
llvm::runIPSCCP
bool runIPSCCP(Module &M, const DataLayout &DL, std::function< const TargetLibraryInfo &(Function &)> GetTLI, function_ref< AnalysisResultsForFn(Function &)> getAnalysis)
Definition: SCCP.cpp:516
llvm::createIPSCCPPass
ModulePass * createIPSCCPPass()
createIPSCCPPass - This pass propagates constants from call sites into the bodies of functions,...
Definition: SCCP.cpp:110
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::TargetLibraryInfoWrapperPass
Definition: TargetLibraryInfo.h:475
llvm::TargetTransformInfoWrapperPass
Wrapper pass for TargetTransformInfo.
Definition: TargetTransformInfo.h:2648
llvm::AssumptionAnalysis
A function analysis which provides an AssumptionCache.
Definition: AssumptionCache.h:173
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
llvm::PreservedAnalyses::preserve
void preserve()
Mark an analysis as preserved.
Definition: PassManager.h:173
IPO.h
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:66
llvm::AssumptionCacheTracker
An immutable pass that tracks lazily created AssumptionCache objects.
Definition: AssumptionCache.h:202
llvm::AssumptionCache
A cache of @llvm.assume calls within a function.
Definition: AssumptionCache.h:42
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
SCCP.h
SCCPSolver.h
ipsccp
ipsccp
Definition: SCCP.cpp:105
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
llvm::AnalysisManager::getCachedResult
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
Definition: PassManager.h:793
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:226
arguments
we compile this esp call L1 $pb L1 esp je LBB1_2 esp ret but is currently always computed in the entry block It would be better to sink the picbase computation down into the block for the as it is the only one that uses it This happens for a lot of code with early outs Another example is loads of arguments
Definition: README.txt:425
llvm::DominatorTreeAnalysis
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:271
TargetTransformInfo.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::InnerAnalysisManagerProxy
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:931
llvm::PostDominatorTreeAnalysis
Analysis pass which computes a PostDominatorTree.
Definition: PostDominators.h:47
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::initializeIPSCCPLegacyPassPass
void initializeIPSCCPLegacyPassPass(PassRegistry &)
llvm::createFunctionSpecializationPass
ModulePass * createFunctionSpecializationPass()
createFunctionSpecializationPass - This pass propagates constants from call sites to the specialized ...
Definition: SCCP.cpp:201
InitializePasses.h
llvm::TargetLibraryAnalysis
Analysis pass providing the TargetLibraryInfo.
Definition: TargetLibraryInfo.h:450
llvm::LoopAnalysis
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:1268