LLVM  15.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 
18 #include "llvm/InitializePasses.h"
19 #include "llvm/Transforms/IPO.h"
22 
23 using namespace llvm;
24 
26  const DataLayout &DL = M.getDataLayout();
27  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
28  auto GetTLI = [&FAM](Function &F) -> const TargetLibraryInfo & {
30  };
31  auto getAnalysis = [&FAM](Function &F) -> AnalysisResultsForFn {
33  return {
34  std::make_unique<PredicateInfo>(F, DT, FAM.getResult<AssumptionAnalysis>(F)),
36  };
37 
38  if (!runIPSCCP(M, DL, GetTLI, getAnalysis))
39  return PreservedAnalyses::all();
40 
45  return PA;
46 }
47 
48 namespace {
49 
50 //===--------------------------------------------------------------------===//
51 //
52 /// IPSCCP Class - This class implements interprocedural Sparse Conditional
53 /// Constant Propagation.
54 ///
55 class IPSCCPLegacyPass : public ModulePass {
56 public:
57  static char ID;
58 
59  IPSCCPLegacyPass() : ModulePass(ID) {
61  }
62 
63  bool runOnModule(Module &M) override {
64  if (skipModule(M))
65  return false;
66  const DataLayout &DL = M.getDataLayout();
67  auto GetTLI = [this](Function &F) -> const TargetLibraryInfo & {
68  return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
69  };
70  auto getAnalysis = [this](Function &F) -> AnalysisResultsForFn {
71  DominatorTree &DT =
72  this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
73  return {
74  std::make_unique<PredicateInfo>(
75  F, DT,
76  this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
77  F)),
78  nullptr, // We cannot preserve the DT or PDT with the legacy pass
79  nullptr}; // manager, so set them to nullptr.
80  };
81 
82  return runIPSCCP(M, DL, GetTLI, getAnalysis);
83  }
84 
85  void getAnalysisUsage(AnalysisUsage &AU) const override {
89  }
90 };
91 
92 } // end anonymous namespace
93 
94 char IPSCCPLegacyPass::ID = 0;
95 
96 INITIALIZE_PASS_BEGIN(IPSCCPLegacyPass, "ipsccp",
97  "Interprocedural Sparse Conditional Constant Propagation",
98  false, false)
102 INITIALIZE_PASS_END(IPSCCPLegacyPass, "ipsccp",
103  "Interprocedural Sparse Conditional Constant Propagation",
105 
106 // createIPSCCPPass - This is the public interface to this file.
107 ModulePass *llvm::createIPSCCPPass() { return new IPSCCPLegacyPass(); }
108 
110  ModuleAnalysisManager &AM) {
111  const DataLayout &DL = M.getDataLayout();
112  auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
113  auto GetTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
115  };
116  auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
117  return FAM.getResult<TargetIRAnalysis>(F);
118  };
119  auto GetAC = [&FAM](Function &F) -> AssumptionCache & {
121  };
122  auto GetAnalysis = [&FAM](Function &F) -> AnalysisResultsForFn {
124  return {std::make_unique<PredicateInfo>(
127  };
128 
129  if (!runFunctionSpecialization(M, DL, GetTLI, GetTTI, GetAC, GetAnalysis))
130  return PreservedAnalyses::all();
131 
136  return PA;
137 }
138 
139 namespace {
140 struct FunctionSpecializationLegacyPass : public ModulePass {
141  static char ID; // Pass identification, replacement for typeid
142  FunctionSpecializationLegacyPass() : ModulePass(ID) {}
143 
144  void getAnalysisUsage(AnalysisUsage &AU) const override {
149  }
150 
151  virtual bool runOnModule(Module &M) override {
152  if (skipModule(M))
153  return false;
154 
155  const DataLayout &DL = M.getDataLayout();
156  auto GetTLI = [this](Function &F) -> TargetLibraryInfo & {
157  return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
158  };
159  auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
160  return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
161  };
162  auto GetAC = [this](Function &F) -> AssumptionCache & {
163  return this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
164  };
165 
166  auto GetAnalysis = [this](Function &F) -> AnalysisResultsForFn {
167  DominatorTree &DT =
168  this->getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
169  return {
170  std::make_unique<PredicateInfo>(
171  F, DT,
172  this->getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
173  F)),
174  nullptr, // We cannot preserve the DT or PDT with the legacy pass
175  nullptr}; // manager, so set them to nullptr.
176  };
177  return runFunctionSpecialization(M, DL, GetTLI, GetTTI, GetAC, GetAnalysis);
178  }
179 };
180 } // namespace
181 
183 
185  FunctionSpecializationLegacyPass, "function-specialization",
186  "Propagate constant arguments by specializing the function", false, false)
187 
192 INITIALIZE_PASS_END(FunctionSpecializationLegacyPass, "function-specialization",
193  "Propagate constant arguments by specializing the function",
194  false, false)
195 
197  return new FunctionSpecializationLegacyPass();
198 }
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:2485
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:17
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:192
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
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:780
llvm::AnalysisResultsForFn
Helper struct for bundling up the analysis results per function for IPSCCP.
Definition: SCCPSolver.h:40
llvm::Function
Definition: Function.h:60
llvm::FunctionSpecializationPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: SCCP.cpp:109
function
function Propagate constant arguments by specializing the function
Definition: SCCP.cpp:193
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition: TargetTransformInfo.h:168
SCCP.h
llvm::AA::Interprocedural
@ Interprocedural
Definition: Attributor.h:157
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:31
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:103
TargetLibraryInfo.h
false
Definition: StackSlotColoring.cpp:141
llvm::IPSCCPPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: SCCP.cpp:25
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:302
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
llvm::runIPSCCP
bool runIPSCCP(Module &M, const DataLayout &DL, std::function< const TargetLibraryInfo &(Function &)> GetTLI, function_ref< AnalysisResultsForFn(Function &)> getAnalysis)
Definition: SCCP.cpp:467
llvm::createIPSCCPPass
ModulePass * createIPSCCPPass()
createIPSCCPPass - This pass propagates constants from call sites into the bodies of functions,...
Definition: SCCP.cpp:107
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::TargetLibraryInfoWrapperPass
Definition: TargetLibraryInfo.h:468
llvm::TargetTransformInfoWrapperPass
Wrapper pass for TargetTransformInfo.
Definition: TargetTransformInfo.h:2541
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:65
llvm::runFunctionSpecialization
bool runFunctionSpecialization(Module &M, 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:826
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:102
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:799
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:222
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:267
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:937
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:196
InitializePasses.h
llvm::TargetLibraryAnalysis
Analysis pass providing the TargetLibraryInfo.
Definition: TargetLibraryInfo.h:443
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38