LLVM 20.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"
22#include <vector>
23
24namespace llvm {
25class Argument;
26class BasicBlock;
27class CallInst;
28class Constant;
29class DataLayout;
30class DominatorTree;
31class Function;
32class GlobalVariable;
33class Instruction;
34class LLVMContext;
35class StructType;
36class TargetLibraryInfo;
37class Value;
38class ValueLatticeElement;
39
40/// Helper struct shared between Function Specialization and SCCP Solver.
41struct ArgInfo {
42 Argument *Formal; // The Formal argument being analysed.
43 Constant *Actual; // A corresponding actual constant argument.
44
46
47 bool operator==(const ArgInfo &Other) const {
48 return Formal == Other.Formal && Actual == Other.Actual;
49 }
50
51 bool operator!=(const ArgInfo &Other) const { return !(*this == Other); }
52
53 friend hash_code hash_value(const ArgInfo &A) {
54 return hash_combine(hash_value(A.Formal), hash_value(A.Actual));
55 }
56};
57
58class SCCPInstVisitor;
59
60//===----------------------------------------------------------------------===//
61//
62/// SCCPSolver - This interface class is a general purpose solver for Sparse
63/// Conditional Constant Propagation (SCCP).
64///
66 std::unique_ptr<SCCPInstVisitor> Visitor;
67
68public:
70 std::function<const TargetLibraryInfo &(Function &)> GetTLI,
71 LLVMContext &Ctx);
72
74
76
77 /// markBlockExecutable - This method can be used by clients to mark all of
78 /// the blocks that are known to be intrinsically live in the processed unit.
79 /// This returns true if the block was not considered live before.
81
83
84 /// trackValueOfGlobalVariable - Clients can use this method to
85 /// inform the SCCPSolver that it should track loads and stores to the
86 /// specified global variable if it can. This is only legal to call if
87 /// performing Interprocedural SCCP.
89
90 /// addTrackedFunction - If the SCCP solver is supposed to track calls into
91 /// and out of the specified function (which cannot have its address taken),
92 /// this method must be called.
94
95 /// Add function to the list of functions whose return cannot be modified.
97
98 /// Returns true if the return of the given function cannot be modified.
100
102
103 /// Returns true if the given function is in the solver's set of
104 /// argument-tracked functions.
106
108
109 /// Solve - Solve for constants and executable blocks.
110 void solve();
111
112 /// resolvedUndefsIn - While solving the dataflow for a function, we assume
113 /// that branches on undef values cannot reach any of their successors.
114 /// However, this is not a safe assumption. After we solve dataflow, this
115 /// method should be use to handle this. If this returns true, the solver
116 /// should be rerun.
118
120
122
124
125 bool isBlockExecutable(BasicBlock *BB) const;
126
127 // isEdgeFeasible - Return true if the control flow edge from the 'From' basic
128 // block to the 'To' basic block is currently feasible.
129 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const;
130
131 std::vector<ValueLatticeElement> getStructLatticeValueFor(Value *V) const;
132
134
135 /// Invalidate the Lattice Value of \p Call and its users after specializing
136 /// the call. Then recompute it.
137 void resetLatticeValueFor(CallBase *Call);
138
140
141 /// getTrackedRetVals - Get the inferred return value map.
143
144 /// getTrackedGlobals - Get and return the set of inferred initializers for
145 /// global variables.
147
148 /// getMRVFunctionsTracked - Get the set of functions which return multiple
149 /// values tracked by the pass.
151
152 /// markOverdefined - Mark the specified value overdefined. This
153 /// works with both scalars and structs.
154 void markOverdefined(Value *V);
155
156 /// trackValueOfArgument - Mark the specified argument overdefined unless it
157 /// have range attribute. This works with both scalars and structs.
159
160 // isStructLatticeConstant - Return true if all the lattice values
161 // corresponding to elements of the structure are constants,
162 // false otherwise.
164
165 /// Helper to return a Constant if \p LV is either a constant or a constant
166 /// range with a single element.
167 Constant *getConstant(const ValueLatticeElement &LV, Type *Ty) const;
168
169 /// Return either a Constant or nullptr for a given Value.
171
172 /// Set the Lattice Value for the arguments of a specialization \p F.
173 /// If an argument is Constant then its lattice value is marked with the
174 /// corresponding actual argument in \p Args. Otherwise, its lattice value
175 /// is inherited (copied) from the corresponding formal argument in \p Args.
177 const SmallVectorImpl<ArgInfo> &Args);
178
179 /// Mark all of the blocks in function \p F non-executable. Clients can used
180 /// this method to erase a function from the module (e.g., if it has been
181 /// completely specialized and is no longer needed).
183
184 void visit(Instruction *I);
185 void visitCall(CallInst &I);
186
188 SmallPtrSetImpl<Value *> &InsertedValues,
189 Statistic &InstRemovedStat,
190 Statistic &InstReplacedStat);
191
193 BasicBlock *&NewUnreachableBB) const;
194
195 void inferReturnAttributes() const;
196 void inferArgAttributes() const;
197
199
200 // Helper to check if \p LV is either a constant or a constant
201 // range with a single element. This should cover exactly the same cases as
202 // the old ValueLatticeElement::isConstant() and is intended to be used in the
203 // transition to ValueLatticeElement.
204 static bool isConstant(const ValueLatticeElement &LV);
205
206 // Helper to check if \p LV is either overdefined or a constant range with
207 // more than a single element. This should cover exactly the same cases as the
208 // old ValueLatticeElement::isOverdefined() and is intended to be used in the
209 // transition to ValueLatticeElement.
210 static bool isOverdefined(const ValueLatticeElement &LV);
211};
212} // namespace llvm
213
214#endif // LLVM_TRANSFORMS_UTILS_SCCPSOLVER_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
BlockVerifier::State From
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#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:31
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1120
This class represents a function call, abstracting a target machine's calling convention.
This is an important base class in LLVM.
Definition: Constant.h:42
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:162
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
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:65
SCCPSolver - This interface class is a general purpose solver for Sparse Conditional Constant Propaga...
Definition: SCCPSolver.h:65
void visitCall(CallInst &I)
const DenseMap< GlobalVariable *, ValueLatticeElement > & getTrackedGlobals()
getTrackedGlobals - Get and return the set of inferred initializers for global variables.
void resetLatticeValueFor(CallBase *Call)
Invalidate the Lattice Value of Call and its users after specializing the call.
void trackValueOfGlobalVariable(GlobalVariable *GV)
trackValueOfGlobalVariable - Clients can use this method to inform the SCCPSolver that it should trac...
bool tryToReplaceWithConstant(Value *V)
Definition: SCCPSolver.cpp:57
void inferArgAttributes() const
Definition: SCCPSolver.cpp:385
bool isStructLatticeConstant(Function *F, StructType *STy)
void addPredicateInfo(Function &F, DominatorTree &DT, AssumptionCache &AC)
void solve()
Solve - Solve for constants and executable blocks.
void visit(Instruction *I)
void trackValueOfArgument(Argument *V)
trackValueOfArgument - Mark the specified argument overdefined unless it have range attribute.
void addTrackedFunction(Function *F)
addTrackedFunction - If the SCCP solver is supposed to track calls into and out of the specified func...
void solveWhileResolvedUndefsIn(Module &M)
const PredicateBase * getPredicateInfoFor(Instruction *I)
const SmallPtrSetImpl< Function * > & getArgumentTrackedFunctions() const
bool resolvedUndefsIn(Function &F)
resolvedUndefsIn - While solving the dataflow for a function, we assume that branches on undef values...
void addArgumentTrackedFunction(Function *F)
void solveWhileResolvedUndefs()
void removeLatticeValueFor(Value *V)
std::vector< ValueLatticeElement > getStructLatticeValueFor(Value *V) const
const SmallPtrSet< Function *, 16 > getMRVFunctionsTracked()
getMRVFunctionsTracked - Get the set of functions which return multiple values tracked by the pass.
Constant * getConstantOrNull(Value *V) const
Return either a Constant or nullptr for a given Value.
bool simplifyInstsInBlock(BasicBlock &BB, SmallPtrSetImpl< Value * > &InsertedValues, Statistic &InstRemovedStat, Statistic &InstReplacedStat)
Definition: SCCPSolver.cpp:235
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.
const ValueLatticeElement & getLatticeValueFor(Value *V) const
void addToMustPreserveReturnsInFunctions(Function *F)
Add function to the list of functions whose return cannot be modified.
bool removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU, BasicBlock *&NewUnreachableBB) const
Definition: SCCPSolver.cpp:259
bool isBlockExecutable(BasicBlock *BB) const
void inferReturnAttributes() const
Definition: SCCPSolver.cpp:380
bool markBlockExecutable(BasicBlock *BB)
markBlockExecutable - This method can be used by clients to mark all of the blocks that are known to ...
void setLatticeValueForSpecializationArguments(Function *F, const SmallVectorImpl< ArgInfo > &Args)
Set the Lattice Value for the arguments of a specialization F.
static bool isConstant(const ValueLatticeElement &LV)
Definition: SCCPSolver.cpp:48
const MapVector< Function *, ValueLatticeElement > & getTrackedRetVals() const
getTrackedRetVals - Get the inferred return value map.
bool isEdgeFeasible(BasicBlock *From, BasicBlock *To) const
bool mustPreserveReturn(Function *F)
Returns true if the return of the given function cannot be modified.
static bool isOverdefined(const ValueLatticeElement &LV)
Definition: SCCPSolver.cpp:53
void markFunctionUnreachable(Function *F)
Mark all of the blocks in function F non-executable.
bool isArgumentTrackedFunction(Function *F)
Returns true if the given function is in the solver's set of argument-tracked functions.
void markOverdefined(Value *V)
markOverdefined - Mark the specified value overdefined.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
Class to represent struct types.
Definition: DerivedTypes.h:218
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.
Definition: ValueLattice.h:26
LLVM Value Representation.
Definition: Value.h:74
An opaque object representing a hash code.
Definition: Hashing.h:75
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Other
Any other memory.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:590
Helper struct shared between Function Specialization and SCCP Solver.
Definition: SCCPSolver.h:41
friend hash_code hash_value(const ArgInfo &A)
Definition: SCCPSolver.h:53
Argument * Formal
Definition: SCCPSolver.h:42
Constant * Actual
Definition: SCCPSolver.h:43
ArgInfo(Argument *F, Constant *A)
Definition: SCCPSolver.h:45
bool operator!=(const ArgInfo &Other) const
Definition: SCCPSolver.h:51
bool operator==(const ArgInfo &Other) const
Definition: SCCPSolver.h:47