LLVM 22.0.0git
SCCPSolver.h
Go to the documentation of this file.
1//===- SCCPSolver.h - SCCP Utility ----------------------------- *- 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// \file
10// This file implements Sparse Conditional Constant Propagation (SCCP) utility.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
15#define LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
16
17#include "llvm/ADT/MapVector.h"
19#include "llvm/ADT/Statistic.h"
23#include <vector>
24
25namespace llvm {
26class Argument;
27class BasicBlock;
28class CallInst;
29class Constant;
30class DataLayout;
31class DominatorTree;
32class Function;
33class GlobalVariable;
34class Instruction;
35class LLVMContext;
36class StructType;
38class Value;
40
41/// Helper struct shared between Function Specialization and SCCP Solver.
42struct ArgInfo {
43 Argument *Formal; // The Formal argument being analysed.
44 Constant *Actual; // A corresponding actual constant argument.
45
47
48 bool operator==(const ArgInfo &Other) const {
49 return Formal == Other.Formal && Actual == Other.Actual;
50 }
51
52 bool operator!=(const ArgInfo &Other) const { return !(*this == Other); }
53
54 friend hash_code hash_value(const ArgInfo &A) {
55 return hash_combine(hash_value(A.Formal), hash_value(A.Actual));
56 }
57};
58
59class SCCPInstVisitor;
60
61//===----------------------------------------------------------------------===//
62//
63/// SCCPSolver - This interface class is a general purpose solver for Sparse
64/// Conditional Constant Propagation (SCCP).
65///
67 std::unique_ptr<SCCPInstVisitor> Visitor;
68
69public:
72 std::function<const TargetLibraryInfo &(Function &)> GetTLI,
73 LLVMContext &Ctx);
74
76
78 AssumptionCache &AC);
79
81
82 /// markBlockExecutable - This method can be used by clients to mark all of
83 /// the blocks that are known to be intrinsically live in the processed unit.
84 /// This returns true if the block was not considered live before.
86
88
89 /// trackValueOfGlobalVariable - Clients can use this method to
90 /// inform the SCCPSolver that it should track loads and stores to the
91 /// specified global variable if it can. This is only legal to call if
92 /// performing Interprocedural SCCP.
94
95 /// addTrackedFunction - If the SCCP solver is supposed to track calls into
96 /// and out of the specified function (which cannot have its address taken),
97 /// this method must be called.
99
100 /// Add function to the list of functions whose return cannot be modified.
102
103 /// Returns true if the return of the given function cannot be modified.
105
107
108 /// Returns true if the given function is in the solver's set of
109 /// argument-tracked functions.
111
114
115 /// Solve - Solve for constants and executable blocks.
116 LLVM_ABI void solve();
117
118 /// resolvedUndefsIn - While solving the dataflow for a function, we assume
119 /// that branches on undef values cannot reach any of their successors.
120 /// However, this is not a safe assumption. After we solve dataflow, this
121 /// method should be use to handle this. If this returns true, the solver
122 /// should be rerun.
124
126
127 LLVM_ABI void
129
131
132 LLVM_ABI bool isBlockExecutable(BasicBlock *BB) const;
133
134 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
135 // block to the 'To' basic block is currently feasible.
136 LLVM_ABI bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
137
138 LLVM_ABI std::vector<ValueLatticeElement>
140
142
143 /// Invalidate the Lattice Value of \p Call and its users after specializing
144 /// the call. Then recompute it.
146
148
149 /// getTrackedRetVals - Get the inferred return value map.
151 getTrackedRetVals() const;
152
153 /// getTrackedGlobals - Get and return the set of inferred initializers for
154 /// global variables.
156 getTrackedGlobals() const;
157
158 /// getMRVFunctionsTracked - Get the set of functions which return multiple
159 /// values tracked by the pass.
161
162 /// markOverdefined - Mark the specified value overdefined. This
163 /// works with both scalars and structs.
165
166 /// trackValueOfArgument - Mark the specified argument overdefined unless it
167 /// have range attribute. This works with both scalars and structs.
169
170 // isStructLatticeConstant - Return true if all the lattice values
171 // corresponding to elements of the structure are constants,
172 // false otherwise.
174
175 /// Helper to return a Constant if \p LV is either a constant or a constant
176 /// range with a single element.
178
179 /// Return either a Constant or nullptr for a given Value.
181
182 /// Set the Lattice Value for the arguments of a specialization \p F.
183 /// If an argument is Constant then its lattice value is marked with the
184 /// corresponding actual argument in \p Args. Otherwise, its lattice value
185 /// is inherited (copied) from the corresponding formal argument in \p Args.
187 Function *F, const SmallVectorImpl<ArgInfo> &Args);
188
189 /// Mark all of the blocks in function \p F non-executable. Clients can used
190 /// this method to erase a function from the module (e.g., if it has been
191 /// completely specialized and is no longer needed).
193
196
198 SmallPtrSetImpl<Value *> &InsertedValues,
199 Statistic &InstRemovedStat,
200 Statistic &InstReplacedStat);
201
203 BasicBlock *&NewUnreachableBB) const;
204
205 LLVM_ABI void inferReturnAttributes() const;
206 LLVM_ABI void inferArgAttributes() const;
207
209
210 // Helper to check if \p LV is either a constant or a constant
211 // range with a single element. This should cover exactly the same cases as
212 // the old ValueLatticeElement::isConstant() and is intended to be used in the
213 // transition to ValueLatticeElement.
214 LLVM_ABI static bool isConstant(const ValueLatticeElement &LV);
215
216 // Helper to check if \p LV is either overdefined or a constant range with
217 // more than a single element. This should cover exactly the same cases as the
218 // old ValueLatticeElement::isOverdefined() and is intended to be used in the
219 // transition to ValueLatticeElement.
220 LLVM_ABI static bool isOverdefined(const ValueLatticeElement &LV);
221};
222} // namespace llvm
223
224#endif // LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:213
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
This file implements a map that provides insertion order iteration.
This file implements the PredicateInfo analysis, which creates an Extended SSA form for operations us...
This file defines the SmallPtrSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
This class represents a function call, abstracting a target machine's calling convention.
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
LLVM_ABI void visitCall(CallInst &I)
LLVM_ABI ~SCCPSolver()
LLVM_ABI void resetLatticeValueFor(CallBase *Call)
Invalidate the Lattice Value of Call and its users after specializing the call.
LLVM_ABI void trackValueOfGlobalVariable(GlobalVariable *GV)
trackValueOfGlobalVariable - Clients can use this method to inform the SCCPSolver that it should trac...
LLVM_ABI bool tryToReplaceWithConstant(Value *V)
LLVM_ABI void inferArgAttributes() const
LLVM_ABI bool isStructLatticeConstant(Function *F, StructType *STy)
LLVM_ABI void addPredicateInfo(Function &F, DominatorTree &DT, AssumptionCache &AC)
LLVM_ABI void solve()
Solve - Solve for constants and executable blocks.
LLVM_ABI void visit(Instruction *I)
LLVM_ABI void trackValueOfArgument(Argument *V)
trackValueOfArgument - Mark the specified argument overdefined unless it have range attribute.
LLVM_ABI const DenseMap< GlobalVariable *, ValueLatticeElement > & getTrackedGlobals() const
getTrackedGlobals - Get and return the set of inferred initializers for global variables.
LLVM_ABI void addTrackedFunction(Function *F)
addTrackedFunction - If the SCCP solver is supposed to track calls into and out of the specified func...
LLVM_ABI void solveWhileResolvedUndefsIn(Module &M)
LLVM_ABI const PredicateBase * getPredicateInfoFor(Instruction *I)
LLVM_ABI const SmallPtrSetImpl< Function * > & getArgumentTrackedFunctions() const
LLVM_ABI const SmallPtrSet< Function *, 16 > & getMRVFunctionsTracked() const
getMRVFunctionsTracked - Get the set of functions which return multiple values tracked by the pass.
LLVM_ABI bool resolvedUndefsIn(Function &F)
resolvedUndefsIn - While solving the dataflow for a function, we assume that branches on undef values...
LLVM_ABI void addArgumentTrackedFunction(Function *F)
LLVM_ABI void solveWhileResolvedUndefs()
LLVM_ABI void removeLatticeValueFor(Value *V)
LLVM_ABI std::vector< ValueLatticeElement > getStructLatticeValueFor(Value *V) const
LLVM_ABI Constant * getConstantOrNull(Value *V) const
Return either a Constant or nullptr for a given Value.
LLVM_ABI bool simplifyInstsInBlock(BasicBlock &BB, SmallPtrSetImpl< Value * > &InsertedValues, Statistic &InstRemovedStat, Statistic &InstReplacedStat)
LLVM_ABI Constant * getConstant(const ValueLatticeElement &LV, Type *Ty) const
Helper to return a Constant if LV is either a constant or a constant range with a single element.
LLVM_ABI const ValueLatticeElement & getLatticeValueFor(Value *V) const
LLVM_ABI void addToMustPreserveReturnsInFunctions(Function *F)
Add function to the list of functions whose return cannot be modified.
LLVM_ABI bool removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU, BasicBlock *&NewUnreachableBB) const
LLVM_ABI bool isBlockExecutable(BasicBlock *BB) const
LLVM_ABI void inferReturnAttributes() const
LLVM_ABI bool markBlockExecutable(BasicBlock *BB)
markBlockExecutable - This method can be used by clients to mark all of the blocks that are known to ...
LLVM_ABI void setLatticeValueForSpecializationArguments(Function *F, const SmallVectorImpl< ArgInfo > &Args)
Set the Lattice Value for the arguments of a specialization F.
static LLVM_ABI bool isConstant(const ValueLatticeElement &LV)
LLVM_ABI const MapVector< Function *, ValueLatticeElement > & getTrackedRetVals() const
getTrackedRetVals - Get the inferred return value map.
LLVM_ABI bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const
LLVM_ABI bool mustPreserveReturn(Function *F)
Returns true if the return of the given function cannot be modified.
static LLVM_ABI bool isOverdefined(const ValueLatticeElement &LV)
LLVM_ABI void markFunctionUnreachable(Function *F)
Mark all of the blocks in function F non-executable.
LLVM_ABI bool isArgumentTrackedFunction(Function *F)
Returns true if the given function is in the solver's set of argument-tracked functions.
LLVM_ABI SCCPSolver(const DataLayout &DL, std::function< const TargetLibraryInfo &(Function &)> GetTLI, LLVMContext &Ctx)
LLVM_ABI void markOverdefined(Value *V)
markOverdefined - Mark the specified value overdefined.
LLVM_ABI void removeSSACopies(Function &F)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Class to represent struct types.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
This class represents lattice values for constants.
LLVM Value Representation.
Definition Value.h:75
An opaque object representing a hash code.
Definition Hashing.h:76
CallInst * Call
This is an optimization pass for GlobalISel generic memory operations.
NoopStatistic Statistic
Definition Statistic.h:162
@ Other
Any other memory.
Definition ModRef.h:68
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:591
friend hash_code hash_value(const ArgInfo &A)
Definition SCCPSolver.h:54
Argument * Formal
Definition SCCPSolver.h:43
Constant * Actual
Definition SCCPSolver.h:44
ArgInfo(Argument *F, Constant *A)
Definition SCCPSolver.h:46
bool operator!=(const ArgInfo &Other) const
Definition SCCPSolver.h:52
bool operator==(const ArgInfo &Other) const
Definition SCCPSolver.h:48